bitmap.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  1. /*
  2. * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
  3. *
  4. * bitmap_create - sets up the bitmap structure
  5. * bitmap_destroy - destroys the bitmap structure
  6. *
  7. * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
  8. * - added disk storage for bitmap
  9. * - changes to allow various bitmap chunk sizes
  10. */
  11. /*
  12. * Still to do:
  13. *
  14. * flush after percent set rather than just time based. (maybe both).
  15. */
  16. #include <linux/blkdev.h>
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/slab.h>
  20. #include <linux/init.h>
  21. #include <linux/timer.h>
  22. #include <linux/sched.h>
  23. #include <linux/list.h>
  24. #include <linux/file.h>
  25. #include <linux/mount.h>
  26. #include <linux/buffer_head.h>
  27. #include <linux/seq_file.h>
  28. #include "md.h"
  29. #include "bitmap.h"
  30. static inline char *bmname(struct bitmap *bitmap)
  31. {
  32. return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
  33. }
  34. /*
  35. * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
  36. *
  37. * 1) check to see if this page is allocated, if it's not then try to alloc
  38. * 2) if the alloc fails, set the page's hijacked flag so we'll use the
  39. * page pointer directly as a counter
  40. *
  41. * if we find our page, we increment the page's refcount so that it stays
  42. * allocated while we're using it
  43. */
  44. static int bitmap_checkpage(struct bitmap_counts *bitmap,
  45. unsigned long page, int create)
  46. __releases(bitmap->lock)
  47. __acquires(bitmap->lock)
  48. {
  49. unsigned char *mappage;
  50. if (page >= bitmap->pages) {
  51. /* This can happen if bitmap_start_sync goes beyond
  52. * End-of-device while looking for a whole page.
  53. * It is harmless.
  54. */
  55. return -EINVAL;
  56. }
  57. if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
  58. return 0;
  59. if (bitmap->bp[page].map) /* page is already allocated, just return */
  60. return 0;
  61. if (!create)
  62. return -ENOENT;
  63. /* this page has not been allocated yet */
  64. spin_unlock_irq(&bitmap->lock);
  65. /* It is possible that this is being called inside a
  66. * prepare_to_wait/finish_wait loop from raid5c:make_request().
  67. * In general it is not permitted to sleep in that context as it
  68. * can cause the loop to spin freely.
  69. * That doesn't apply here as we can only reach this point
  70. * once with any loop.
  71. * When this function completes, either bp[page].map or
  72. * bp[page].hijacked. In either case, this function will
  73. * abort before getting to this point again. So there is
  74. * no risk of a free-spin, and so it is safe to assert
  75. * that sleeping here is allowed.
  76. */
  77. sched_annotate_sleep();
  78. mappage = kzalloc(PAGE_SIZE, GFP_NOIO);
  79. spin_lock_irq(&bitmap->lock);
  80. if (mappage == NULL) {
  81. pr_debug("md/bitmap: map page allocation failed, hijacking\n");
  82. /* failed - set the hijacked flag so that we can use the
  83. * pointer as a counter */
  84. if (!bitmap->bp[page].map)
  85. bitmap->bp[page].hijacked = 1;
  86. } else if (bitmap->bp[page].map ||
  87. bitmap->bp[page].hijacked) {
  88. /* somebody beat us to getting the page */
  89. kfree(mappage);
  90. return 0;
  91. } else {
  92. /* no page was in place and we have one, so install it */
  93. bitmap->bp[page].map = mappage;
  94. bitmap->missing_pages--;
  95. }
  96. return 0;
  97. }
  98. /* if page is completely empty, put it back on the free list, or dealloc it */
  99. /* if page was hijacked, unmark the flag so it might get alloced next time */
  100. /* Note: lock should be held when calling this */
  101. static void bitmap_checkfree(struct bitmap_counts *bitmap, unsigned long page)
  102. {
  103. char *ptr;
  104. if (bitmap->bp[page].count) /* page is still busy */
  105. return;
  106. /* page is no longer in use, it can be released */
  107. if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
  108. bitmap->bp[page].hijacked = 0;
  109. bitmap->bp[page].map = NULL;
  110. } else {
  111. /* normal case, free the page */
  112. ptr = bitmap->bp[page].map;
  113. bitmap->bp[page].map = NULL;
  114. bitmap->missing_pages++;
  115. kfree(ptr);
  116. }
  117. }
  118. /*
  119. * bitmap file handling - read and write the bitmap file and its superblock
  120. */
  121. /*
  122. * basic page I/O operations
  123. */
  124. /* IO operations when bitmap is stored near all superblocks */
  125. static int read_sb_page(struct mddev *mddev, loff_t offset,
  126. struct page *page,
  127. unsigned long index, int size)
  128. {
  129. /* choose a good rdev and read the page from there */
  130. struct md_rdev *rdev;
  131. sector_t target;
  132. rdev_for_each(rdev, mddev) {
  133. if (! test_bit(In_sync, &rdev->flags)
  134. || test_bit(Faulty, &rdev->flags))
  135. continue;
  136. target = offset + index * (PAGE_SIZE/512);
  137. if (sync_page_io(rdev, target,
  138. roundup(size, bdev_logical_block_size(rdev->bdev)),
  139. page, READ, true)) {
  140. page->index = index;
  141. return 0;
  142. }
  143. }
  144. return -EIO;
  145. }
  146. static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev)
  147. {
  148. /* Iterate the disks of an mddev, using rcu to protect access to the
  149. * linked list, and raising the refcount of devices we return to ensure
  150. * they don't disappear while in use.
  151. * As devices are only added or removed when raid_disk is < 0 and
  152. * nr_pending is 0 and In_sync is clear, the entries we return will
  153. * still be in the same position on the list when we re-enter
  154. * list_for_each_entry_continue_rcu.
  155. *
  156. * Note that if entered with 'rdev == NULL' to start at the
  157. * beginning, we temporarily assign 'rdev' to an address which
  158. * isn't really an rdev, but which can be used by
  159. * list_for_each_entry_continue_rcu() to find the first entry.
  160. */
  161. rcu_read_lock();
  162. if (rdev == NULL)
  163. /* start at the beginning */
  164. rdev = list_entry(&mddev->disks, struct md_rdev, same_set);
  165. else {
  166. /* release the previous rdev and start from there. */
  167. rdev_dec_pending(rdev, mddev);
  168. }
  169. list_for_each_entry_continue_rcu(rdev, &mddev->disks, same_set) {
  170. if (rdev->raid_disk >= 0 &&
  171. !test_bit(Faulty, &rdev->flags)) {
  172. /* this is a usable devices */
  173. atomic_inc(&rdev->nr_pending);
  174. rcu_read_unlock();
  175. return rdev;
  176. }
  177. }
  178. rcu_read_unlock();
  179. return NULL;
  180. }
  181. static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
  182. {
  183. struct md_rdev *rdev = NULL;
  184. struct block_device *bdev;
  185. struct mddev *mddev = bitmap->mddev;
  186. struct bitmap_storage *store = &bitmap->storage;
  187. int node_offset = 0;
  188. if (mddev_is_clustered(bitmap->mddev))
  189. node_offset = bitmap->cluster_slot * store->file_pages;
  190. while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
  191. int size = PAGE_SIZE;
  192. loff_t offset = mddev->bitmap_info.offset;
  193. bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
  194. if (page->index == store->file_pages-1) {
  195. int last_page_size = store->bytes & (PAGE_SIZE-1);
  196. if (last_page_size == 0)
  197. last_page_size = PAGE_SIZE;
  198. size = roundup(last_page_size,
  199. bdev_logical_block_size(bdev));
  200. }
  201. /* Just make sure we aren't corrupting data or
  202. * metadata
  203. */
  204. if (mddev->external) {
  205. /* Bitmap could be anywhere. */
  206. if (rdev->sb_start + offset + (page->index
  207. * (PAGE_SIZE/512))
  208. > rdev->data_offset
  209. &&
  210. rdev->sb_start + offset
  211. < (rdev->data_offset + mddev->dev_sectors
  212. + (PAGE_SIZE/512)))
  213. goto bad_alignment;
  214. } else if (offset < 0) {
  215. /* DATA BITMAP METADATA */
  216. if (offset
  217. + (long)(page->index * (PAGE_SIZE/512))
  218. + size/512 > 0)
  219. /* bitmap runs in to metadata */
  220. goto bad_alignment;
  221. if (rdev->data_offset + mddev->dev_sectors
  222. > rdev->sb_start + offset)
  223. /* data runs in to bitmap */
  224. goto bad_alignment;
  225. } else if (rdev->sb_start < rdev->data_offset) {
  226. /* METADATA BITMAP DATA */
  227. if (rdev->sb_start
  228. + offset
  229. + page->index*(PAGE_SIZE/512) + size/512
  230. > rdev->data_offset)
  231. /* bitmap runs in to data */
  232. goto bad_alignment;
  233. } else {
  234. /* DATA METADATA BITMAP - no problems */
  235. }
  236. md_super_write(mddev, rdev,
  237. rdev->sb_start + offset
  238. + page->index * (PAGE_SIZE/512),
  239. size,
  240. page);
  241. }
  242. if (wait)
  243. md_super_wait(mddev);
  244. return 0;
  245. bad_alignment:
  246. return -EINVAL;
  247. }
  248. static void bitmap_file_kick(struct bitmap *bitmap);
  249. /*
  250. * write out a page to a file
  251. */
  252. static void write_page(struct bitmap *bitmap, struct page *page, int wait)
  253. {
  254. struct buffer_head *bh;
  255. if (bitmap->storage.file == NULL) {
  256. switch (write_sb_page(bitmap, page, wait)) {
  257. case -EINVAL:
  258. set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
  259. }
  260. } else {
  261. bh = page_buffers(page);
  262. while (bh && bh->b_blocknr) {
  263. atomic_inc(&bitmap->pending_writes);
  264. set_buffer_locked(bh);
  265. set_buffer_mapped(bh);
  266. submit_bh(WRITE | REQ_SYNC, bh);
  267. bh = bh->b_this_page;
  268. }
  269. if (wait)
  270. wait_event(bitmap->write_wait,
  271. atomic_read(&bitmap->pending_writes)==0);
  272. }
  273. if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
  274. bitmap_file_kick(bitmap);
  275. }
  276. static void end_bitmap_write(struct buffer_head *bh, int uptodate)
  277. {
  278. struct bitmap *bitmap = bh->b_private;
  279. if (!uptodate)
  280. set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
  281. if (atomic_dec_and_test(&bitmap->pending_writes))
  282. wake_up(&bitmap->write_wait);
  283. }
  284. /* copied from buffer.c */
  285. static void
  286. __clear_page_buffers(struct page *page)
  287. {
  288. ClearPagePrivate(page);
  289. set_page_private(page, 0);
  290. page_cache_release(page);
  291. }
  292. static void free_buffers(struct page *page)
  293. {
  294. struct buffer_head *bh;
  295. if (!PagePrivate(page))
  296. return;
  297. bh = page_buffers(page);
  298. while (bh) {
  299. struct buffer_head *next = bh->b_this_page;
  300. free_buffer_head(bh);
  301. bh = next;
  302. }
  303. __clear_page_buffers(page);
  304. put_page(page);
  305. }
  306. /* read a page from a file.
  307. * We both read the page, and attach buffers to the page to record the
  308. * address of each block (using bmap). These addresses will be used
  309. * to write the block later, completely bypassing the filesystem.
  310. * This usage is similar to how swap files are handled, and allows us
  311. * to write to a file with no concerns of memory allocation failing.
  312. */
  313. static int read_page(struct file *file, unsigned long index,
  314. struct bitmap *bitmap,
  315. unsigned long count,
  316. struct page *page)
  317. {
  318. int ret = 0;
  319. struct inode *inode = file_inode(file);
  320. struct buffer_head *bh;
  321. sector_t block;
  322. pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
  323. (unsigned long long)index << PAGE_SHIFT);
  324. bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
  325. if (!bh) {
  326. ret = -ENOMEM;
  327. goto out;
  328. }
  329. attach_page_buffers(page, bh);
  330. block = index << (PAGE_SHIFT - inode->i_blkbits);
  331. while (bh) {
  332. if (count == 0)
  333. bh->b_blocknr = 0;
  334. else {
  335. bh->b_blocknr = bmap(inode, block);
  336. if (bh->b_blocknr == 0) {
  337. /* Cannot use this file! */
  338. ret = -EINVAL;
  339. goto out;
  340. }
  341. bh->b_bdev = inode->i_sb->s_bdev;
  342. if (count < (1<<inode->i_blkbits))
  343. count = 0;
  344. else
  345. count -= (1<<inode->i_blkbits);
  346. bh->b_end_io = end_bitmap_write;
  347. bh->b_private = bitmap;
  348. atomic_inc(&bitmap->pending_writes);
  349. set_buffer_locked(bh);
  350. set_buffer_mapped(bh);
  351. submit_bh(READ, bh);
  352. }
  353. block++;
  354. bh = bh->b_this_page;
  355. }
  356. page->index = index;
  357. wait_event(bitmap->write_wait,
  358. atomic_read(&bitmap->pending_writes)==0);
  359. if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
  360. ret = -EIO;
  361. out:
  362. if (ret)
  363. printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %d\n",
  364. (int)PAGE_SIZE,
  365. (unsigned long long)index << PAGE_SHIFT,
  366. ret);
  367. return ret;
  368. }
  369. /*
  370. * bitmap file superblock operations
  371. */
  372. /* update the event counter and sync the superblock to disk */
  373. void bitmap_update_sb(struct bitmap *bitmap)
  374. {
  375. bitmap_super_t *sb;
  376. if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
  377. return;
  378. if (bitmap->mddev->bitmap_info.external)
  379. return;
  380. if (!bitmap->storage.sb_page) /* no superblock */
  381. return;
  382. sb = kmap_atomic(bitmap->storage.sb_page);
  383. sb->events = cpu_to_le64(bitmap->mddev->events);
  384. if (bitmap->mddev->events < bitmap->events_cleared)
  385. /* rocking back to read-only */
  386. bitmap->events_cleared = bitmap->mddev->events;
  387. sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
  388. sb->state = cpu_to_le32(bitmap->flags);
  389. /* Just in case these have been changed via sysfs: */
  390. sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
  391. sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
  392. /* This might have been changed by a reshape */
  393. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  394. sb->chunksize = cpu_to_le32(bitmap->mddev->bitmap_info.chunksize);
  395. sb->nodes = cpu_to_le32(bitmap->mddev->bitmap_info.nodes);
  396. sb->sectors_reserved = cpu_to_le32(bitmap->mddev->
  397. bitmap_info.space);
  398. kunmap_atomic(sb);
  399. write_page(bitmap, bitmap->storage.sb_page, 1);
  400. }
  401. /* print out the bitmap file superblock */
  402. void bitmap_print_sb(struct bitmap *bitmap)
  403. {
  404. bitmap_super_t *sb;
  405. if (!bitmap || !bitmap->storage.sb_page)
  406. return;
  407. sb = kmap_atomic(bitmap->storage.sb_page);
  408. printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
  409. printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
  410. printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
  411. printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
  412. *(__u32 *)(sb->uuid+0),
  413. *(__u32 *)(sb->uuid+4),
  414. *(__u32 *)(sb->uuid+8),
  415. *(__u32 *)(sb->uuid+12));
  416. printk(KERN_DEBUG " events: %llu\n",
  417. (unsigned long long) le64_to_cpu(sb->events));
  418. printk(KERN_DEBUG "events cleared: %llu\n",
  419. (unsigned long long) le64_to_cpu(sb->events_cleared));
  420. printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
  421. printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
  422. printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
  423. printk(KERN_DEBUG " sync size: %llu KB\n",
  424. (unsigned long long)le64_to_cpu(sb->sync_size)/2);
  425. printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
  426. kunmap_atomic(sb);
  427. }
  428. /*
  429. * bitmap_new_disk_sb
  430. * @bitmap
  431. *
  432. * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
  433. * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
  434. * This function verifies 'bitmap_info' and populates the on-disk bitmap
  435. * structure, which is to be written to disk.
  436. *
  437. * Returns: 0 on success, -Exxx on error
  438. */
  439. static int bitmap_new_disk_sb(struct bitmap *bitmap)
  440. {
  441. bitmap_super_t *sb;
  442. unsigned long chunksize, daemon_sleep, write_behind;
  443. bitmap->storage.sb_page = alloc_page(GFP_KERNEL);
  444. if (bitmap->storage.sb_page == NULL)
  445. return -ENOMEM;
  446. bitmap->storage.sb_page->index = 0;
  447. sb = kmap_atomic(bitmap->storage.sb_page);
  448. sb->magic = cpu_to_le32(BITMAP_MAGIC);
  449. sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
  450. chunksize = bitmap->mddev->bitmap_info.chunksize;
  451. BUG_ON(!chunksize);
  452. if (!is_power_of_2(chunksize)) {
  453. kunmap_atomic(sb);
  454. printk(KERN_ERR "bitmap chunksize not a power of 2\n");
  455. return -EINVAL;
  456. }
  457. sb->chunksize = cpu_to_le32(chunksize);
  458. daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
  459. if (!daemon_sleep ||
  460. (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
  461. printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
  462. daemon_sleep = 5 * HZ;
  463. }
  464. sb->daemon_sleep = cpu_to_le32(daemon_sleep);
  465. bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
  466. /*
  467. * FIXME: write_behind for RAID1. If not specified, what
  468. * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
  469. */
  470. write_behind = bitmap->mddev->bitmap_info.max_write_behind;
  471. if (write_behind > COUNTER_MAX)
  472. write_behind = COUNTER_MAX / 2;
  473. sb->write_behind = cpu_to_le32(write_behind);
  474. bitmap->mddev->bitmap_info.max_write_behind = write_behind;
  475. /* keep the array size field of the bitmap superblock up to date */
  476. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  477. memcpy(sb->uuid, bitmap->mddev->uuid, 16);
  478. set_bit(BITMAP_STALE, &bitmap->flags);
  479. sb->state = cpu_to_le32(bitmap->flags);
  480. bitmap->events_cleared = bitmap->mddev->events;
  481. sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
  482. kunmap_atomic(sb);
  483. return 0;
  484. }
  485. /* read the superblock from the bitmap file and initialize some bitmap fields */
  486. static int bitmap_read_sb(struct bitmap *bitmap)
  487. {
  488. char *reason = NULL;
  489. bitmap_super_t *sb;
  490. unsigned long chunksize, daemon_sleep, write_behind;
  491. unsigned long long events;
  492. int nodes = 0;
  493. unsigned long sectors_reserved = 0;
  494. int err = -EINVAL;
  495. struct page *sb_page;
  496. if (!bitmap->storage.file && !bitmap->mddev->bitmap_info.offset) {
  497. chunksize = 128 * 1024 * 1024;
  498. daemon_sleep = 5 * HZ;
  499. write_behind = 0;
  500. set_bit(BITMAP_STALE, &bitmap->flags);
  501. err = 0;
  502. goto out_no_sb;
  503. }
  504. /* page 0 is the superblock, read it... */
  505. sb_page = alloc_page(GFP_KERNEL);
  506. if (!sb_page)
  507. return -ENOMEM;
  508. bitmap->storage.sb_page = sb_page;
  509. re_read:
  510. /* If cluster_slot is set, the cluster is setup */
  511. if (bitmap->cluster_slot >= 0) {
  512. sector_t bm_blocks = bitmap->mddev->resync_max_sectors;
  513. sector_div(bm_blocks,
  514. bitmap->mddev->bitmap_info.chunksize >> 9);
  515. /* bits to bytes */
  516. bm_blocks = ((bm_blocks+7) >> 3) + sizeof(bitmap_super_t);
  517. /* to 4k blocks */
  518. bm_blocks = DIV_ROUND_UP_SECTOR_T(bm_blocks, 4096);
  519. bitmap->mddev->bitmap_info.offset += bitmap->cluster_slot * (bm_blocks << 3);
  520. pr_info("%s:%d bm slot: %d offset: %llu\n", __func__, __LINE__,
  521. bitmap->cluster_slot, (unsigned long long)bitmap->mddev->bitmap_info.offset);
  522. }
  523. if (bitmap->storage.file) {
  524. loff_t isize = i_size_read(bitmap->storage.file->f_mapping->host);
  525. int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
  526. err = read_page(bitmap->storage.file, 0,
  527. bitmap, bytes, sb_page);
  528. } else {
  529. err = read_sb_page(bitmap->mddev,
  530. bitmap->mddev->bitmap_info.offset,
  531. sb_page,
  532. 0, sizeof(bitmap_super_t));
  533. }
  534. if (err)
  535. return err;
  536. err = -EINVAL;
  537. sb = kmap_atomic(sb_page);
  538. chunksize = le32_to_cpu(sb->chunksize);
  539. daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
  540. write_behind = le32_to_cpu(sb->write_behind);
  541. sectors_reserved = le32_to_cpu(sb->sectors_reserved);
  542. nodes = le32_to_cpu(sb->nodes);
  543. strlcpy(bitmap->mddev->bitmap_info.cluster_name, sb->cluster_name, 64);
  544. /* verify that the bitmap-specific fields are valid */
  545. if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
  546. reason = "bad magic";
  547. else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
  548. le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
  549. reason = "unrecognized superblock version";
  550. else if (chunksize < 512)
  551. reason = "bitmap chunksize too small";
  552. else if (!is_power_of_2(chunksize))
  553. reason = "bitmap chunksize not a power of 2";
  554. else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
  555. reason = "daemon sleep period out of range";
  556. else if (write_behind > COUNTER_MAX)
  557. reason = "write-behind limit out of range (0 - 16383)";
  558. if (reason) {
  559. printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
  560. bmname(bitmap), reason);
  561. goto out;
  562. }
  563. /* keep the array size field of the bitmap superblock up to date */
  564. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  565. if (bitmap->mddev->persistent) {
  566. /*
  567. * We have a persistent array superblock, so compare the
  568. * bitmap's UUID and event counter to the mddev's
  569. */
  570. if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
  571. printk(KERN_INFO
  572. "%s: bitmap superblock UUID mismatch\n",
  573. bmname(bitmap));
  574. goto out;
  575. }
  576. events = le64_to_cpu(sb->events);
  577. if (!nodes && (events < bitmap->mddev->events)) {
  578. printk(KERN_INFO
  579. "%s: bitmap file is out of date (%llu < %llu) "
  580. "-- forcing full recovery\n",
  581. bmname(bitmap), events,
  582. (unsigned long long) bitmap->mddev->events);
  583. set_bit(BITMAP_STALE, &bitmap->flags);
  584. }
  585. }
  586. /* assign fields using values from superblock */
  587. bitmap->flags |= le32_to_cpu(sb->state);
  588. if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
  589. set_bit(BITMAP_HOSTENDIAN, &bitmap->flags);
  590. bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
  591. strlcpy(bitmap->mddev->bitmap_info.cluster_name, sb->cluster_name, 64);
  592. err = 0;
  593. out:
  594. kunmap_atomic(sb);
  595. /* Assiging chunksize is required for "re_read" */
  596. bitmap->mddev->bitmap_info.chunksize = chunksize;
  597. if (nodes && (bitmap->cluster_slot < 0)) {
  598. err = md_setup_cluster(bitmap->mddev, nodes);
  599. if (err) {
  600. pr_err("%s: Could not setup cluster service (%d)\n",
  601. bmname(bitmap), err);
  602. goto out_no_sb;
  603. }
  604. bitmap->cluster_slot = md_cluster_ops->slot_number(bitmap->mddev);
  605. goto re_read;
  606. }
  607. out_no_sb:
  608. if (test_bit(BITMAP_STALE, &bitmap->flags))
  609. bitmap->events_cleared = bitmap->mddev->events;
  610. bitmap->mddev->bitmap_info.chunksize = chunksize;
  611. bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
  612. bitmap->mddev->bitmap_info.max_write_behind = write_behind;
  613. bitmap->mddev->bitmap_info.nodes = nodes;
  614. if (bitmap->mddev->bitmap_info.space == 0 ||
  615. bitmap->mddev->bitmap_info.space > sectors_reserved)
  616. bitmap->mddev->bitmap_info.space = sectors_reserved;
  617. if (err) {
  618. bitmap_print_sb(bitmap);
  619. if (bitmap->cluster_slot < 0)
  620. md_cluster_stop(bitmap->mddev);
  621. }
  622. return err;
  623. }
  624. /*
  625. * general bitmap file operations
  626. */
  627. /*
  628. * on-disk bitmap:
  629. *
  630. * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
  631. * file a page at a time. There's a superblock at the start of the file.
  632. */
  633. /* calculate the index of the page that contains this bit */
  634. static inline unsigned long file_page_index(struct bitmap_storage *store,
  635. unsigned long chunk)
  636. {
  637. if (store->sb_page)
  638. chunk += sizeof(bitmap_super_t) << 3;
  639. return chunk >> PAGE_BIT_SHIFT;
  640. }
  641. /* calculate the (bit) offset of this bit within a page */
  642. static inline unsigned long file_page_offset(struct bitmap_storage *store,
  643. unsigned long chunk)
  644. {
  645. if (store->sb_page)
  646. chunk += sizeof(bitmap_super_t) << 3;
  647. return chunk & (PAGE_BITS - 1);
  648. }
  649. /*
  650. * return a pointer to the page in the filemap that contains the given bit
  651. *
  652. */
  653. static inline struct page *filemap_get_page(struct bitmap_storage *store,
  654. unsigned long chunk)
  655. {
  656. if (file_page_index(store, chunk) >= store->file_pages)
  657. return NULL;
  658. return store->filemap[file_page_index(store, chunk)];
  659. }
  660. static int bitmap_storage_alloc(struct bitmap_storage *store,
  661. unsigned long chunks, int with_super,
  662. int slot_number)
  663. {
  664. int pnum, offset = 0;
  665. unsigned long num_pages;
  666. unsigned long bytes;
  667. bytes = DIV_ROUND_UP(chunks, 8);
  668. if (with_super)
  669. bytes += sizeof(bitmap_super_t);
  670. num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
  671. offset = slot_number * (num_pages - 1);
  672. store->filemap = kmalloc(sizeof(struct page *)
  673. * num_pages, GFP_KERNEL);
  674. if (!store->filemap)
  675. return -ENOMEM;
  676. if (with_super && !store->sb_page) {
  677. store->sb_page = alloc_page(GFP_KERNEL|__GFP_ZERO);
  678. if (store->sb_page == NULL)
  679. return -ENOMEM;
  680. }
  681. pnum = 0;
  682. if (store->sb_page) {
  683. store->filemap[0] = store->sb_page;
  684. pnum = 1;
  685. store->sb_page->index = offset;
  686. }
  687. for ( ; pnum < num_pages; pnum++) {
  688. store->filemap[pnum] = alloc_page(GFP_KERNEL|__GFP_ZERO);
  689. if (!store->filemap[pnum]) {
  690. store->file_pages = pnum;
  691. return -ENOMEM;
  692. }
  693. store->filemap[pnum]->index = pnum + offset;
  694. }
  695. store->file_pages = pnum;
  696. /* We need 4 bits per page, rounded up to a multiple
  697. * of sizeof(unsigned long) */
  698. store->filemap_attr = kzalloc(
  699. roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
  700. GFP_KERNEL);
  701. if (!store->filemap_attr)
  702. return -ENOMEM;
  703. store->bytes = bytes;
  704. return 0;
  705. }
  706. static void bitmap_file_unmap(struct bitmap_storage *store)
  707. {
  708. struct page **map, *sb_page;
  709. int pages;
  710. struct file *file;
  711. file = store->file;
  712. map = store->filemap;
  713. pages = store->file_pages;
  714. sb_page = store->sb_page;
  715. while (pages--)
  716. if (map[pages] != sb_page) /* 0 is sb_page, release it below */
  717. free_buffers(map[pages]);
  718. kfree(map);
  719. kfree(store->filemap_attr);
  720. if (sb_page)
  721. free_buffers(sb_page);
  722. if (file) {
  723. struct inode *inode = file_inode(file);
  724. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  725. fput(file);
  726. }
  727. }
  728. /*
  729. * bitmap_file_kick - if an error occurs while manipulating the bitmap file
  730. * then it is no longer reliable, so we stop using it and we mark the file
  731. * as failed in the superblock
  732. */
  733. static void bitmap_file_kick(struct bitmap *bitmap)
  734. {
  735. char *path, *ptr = NULL;
  736. if (!test_and_set_bit(BITMAP_STALE, &bitmap->flags)) {
  737. bitmap_update_sb(bitmap);
  738. if (bitmap->storage.file) {
  739. path = kmalloc(PAGE_SIZE, GFP_KERNEL);
  740. if (path)
  741. ptr = file_path(bitmap->storage.file,
  742. path, PAGE_SIZE);
  743. printk(KERN_ALERT
  744. "%s: kicking failed bitmap file %s from array!\n",
  745. bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
  746. kfree(path);
  747. } else
  748. printk(KERN_ALERT
  749. "%s: disabling internal bitmap due to errors\n",
  750. bmname(bitmap));
  751. }
  752. }
  753. enum bitmap_page_attr {
  754. BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
  755. BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
  756. * i.e. counter is 1 or 2. */
  757. BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
  758. };
  759. static inline void set_page_attr(struct bitmap *bitmap, int pnum,
  760. enum bitmap_page_attr attr)
  761. {
  762. set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
  763. }
  764. static inline void clear_page_attr(struct bitmap *bitmap, int pnum,
  765. enum bitmap_page_attr attr)
  766. {
  767. clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
  768. }
  769. static inline int test_page_attr(struct bitmap *bitmap, int pnum,
  770. enum bitmap_page_attr attr)
  771. {
  772. return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
  773. }
  774. static inline int test_and_clear_page_attr(struct bitmap *bitmap, int pnum,
  775. enum bitmap_page_attr attr)
  776. {
  777. return test_and_clear_bit((pnum<<2) + attr,
  778. bitmap->storage.filemap_attr);
  779. }
  780. /*
  781. * bitmap_file_set_bit -- called before performing a write to the md device
  782. * to set (and eventually sync) a particular bit in the bitmap file
  783. *
  784. * we set the bit immediately, then we record the page number so that
  785. * when an unplug occurs, we can flush the dirty pages out to disk
  786. */
  787. static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
  788. {
  789. unsigned long bit;
  790. struct page *page;
  791. void *kaddr;
  792. unsigned long chunk = block >> bitmap->counts.chunkshift;
  793. page = filemap_get_page(&bitmap->storage, chunk);
  794. if (!page)
  795. return;
  796. bit = file_page_offset(&bitmap->storage, chunk);
  797. /* set the bit */
  798. kaddr = kmap_atomic(page);
  799. if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
  800. set_bit(bit, kaddr);
  801. else
  802. set_bit_le(bit, kaddr);
  803. kunmap_atomic(kaddr);
  804. pr_debug("set file bit %lu page %lu\n", bit, page->index);
  805. /* record page number so it gets flushed to disk when unplug occurs */
  806. set_page_attr(bitmap, page->index, BITMAP_PAGE_DIRTY);
  807. }
  808. static void bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block)
  809. {
  810. unsigned long bit;
  811. struct page *page;
  812. void *paddr;
  813. unsigned long chunk = block >> bitmap->counts.chunkshift;
  814. page = filemap_get_page(&bitmap->storage, chunk);
  815. if (!page)
  816. return;
  817. bit = file_page_offset(&bitmap->storage, chunk);
  818. paddr = kmap_atomic(page);
  819. if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
  820. clear_bit(bit, paddr);
  821. else
  822. clear_bit_le(bit, paddr);
  823. kunmap_atomic(paddr);
  824. if (!test_page_attr(bitmap, page->index, BITMAP_PAGE_NEEDWRITE)) {
  825. set_page_attr(bitmap, page->index, BITMAP_PAGE_PENDING);
  826. bitmap->allclean = 0;
  827. }
  828. }
  829. static int bitmap_file_test_bit(struct bitmap *bitmap, sector_t block)
  830. {
  831. unsigned long bit;
  832. struct page *page;
  833. void *paddr;
  834. unsigned long chunk = block >> bitmap->counts.chunkshift;
  835. int set = 0;
  836. page = filemap_get_page(&bitmap->storage, chunk);
  837. if (!page)
  838. return -EINVAL;
  839. bit = file_page_offset(&bitmap->storage, chunk);
  840. paddr = kmap_atomic(page);
  841. if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
  842. set = test_bit(bit, paddr);
  843. else
  844. set = test_bit_le(bit, paddr);
  845. kunmap_atomic(paddr);
  846. return set;
  847. }
  848. /* this gets called when the md device is ready to unplug its underlying
  849. * (slave) device queues -- before we let any writes go down, we need to
  850. * sync the dirty pages of the bitmap file to disk */
  851. void bitmap_unplug(struct bitmap *bitmap)
  852. {
  853. unsigned long i;
  854. int dirty, need_write;
  855. if (!bitmap || !bitmap->storage.filemap ||
  856. test_bit(BITMAP_STALE, &bitmap->flags))
  857. return;
  858. /* look at each page to see if there are any set bits that need to be
  859. * flushed out to disk */
  860. for (i = 0; i < bitmap->storage.file_pages; i++) {
  861. if (!bitmap->storage.filemap)
  862. return;
  863. dirty = test_and_clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
  864. need_write = test_and_clear_page_attr(bitmap, i,
  865. BITMAP_PAGE_NEEDWRITE);
  866. if (dirty || need_write) {
  867. clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING);
  868. write_page(bitmap, bitmap->storage.filemap[i], 0);
  869. }
  870. }
  871. if (bitmap->storage.file)
  872. wait_event(bitmap->write_wait,
  873. atomic_read(&bitmap->pending_writes)==0);
  874. else
  875. md_super_wait(bitmap->mddev);
  876. if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
  877. bitmap_file_kick(bitmap);
  878. }
  879. EXPORT_SYMBOL(bitmap_unplug);
  880. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
  881. /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
  882. * the in-memory bitmap from the on-disk bitmap -- also, sets up the
  883. * memory mapping of the bitmap file
  884. * Special cases:
  885. * if there's no bitmap file, or if the bitmap file had been
  886. * previously kicked from the array, we mark all the bits as
  887. * 1's in order to cause a full resync.
  888. *
  889. * We ignore all bits for sectors that end earlier than 'start'.
  890. * This is used when reading an out-of-date bitmap...
  891. */
  892. static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
  893. {
  894. unsigned long i, chunks, index, oldindex, bit, node_offset = 0;
  895. struct page *page = NULL;
  896. unsigned long bit_cnt = 0;
  897. struct file *file;
  898. unsigned long offset;
  899. int outofdate;
  900. int ret = -ENOSPC;
  901. void *paddr;
  902. struct bitmap_storage *store = &bitmap->storage;
  903. chunks = bitmap->counts.chunks;
  904. file = store->file;
  905. if (!file && !bitmap->mddev->bitmap_info.offset) {
  906. /* No permanent bitmap - fill with '1s'. */
  907. store->filemap = NULL;
  908. store->file_pages = 0;
  909. for (i = 0; i < chunks ; i++) {
  910. /* if the disk bit is set, set the memory bit */
  911. int needed = ((sector_t)(i+1) << (bitmap->counts.chunkshift)
  912. >= start);
  913. bitmap_set_memory_bits(bitmap,
  914. (sector_t)i << bitmap->counts.chunkshift,
  915. needed);
  916. }
  917. return 0;
  918. }
  919. outofdate = test_bit(BITMAP_STALE, &bitmap->flags);
  920. if (outofdate)
  921. printk(KERN_INFO "%s: bitmap file is out of date, doing full "
  922. "recovery\n", bmname(bitmap));
  923. if (file && i_size_read(file->f_mapping->host) < store->bytes) {
  924. printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
  925. bmname(bitmap),
  926. (unsigned long) i_size_read(file->f_mapping->host),
  927. store->bytes);
  928. goto err;
  929. }
  930. oldindex = ~0L;
  931. offset = 0;
  932. if (!bitmap->mddev->bitmap_info.external)
  933. offset = sizeof(bitmap_super_t);
  934. if (mddev_is_clustered(bitmap->mddev))
  935. node_offset = bitmap->cluster_slot * (DIV_ROUND_UP(store->bytes, PAGE_SIZE));
  936. for (i = 0; i < chunks; i++) {
  937. int b;
  938. index = file_page_index(&bitmap->storage, i);
  939. bit = file_page_offset(&bitmap->storage, i);
  940. if (index != oldindex) { /* this is a new page, read it in */
  941. int count;
  942. /* unmap the old page, we're done with it */
  943. if (index == store->file_pages-1)
  944. count = store->bytes - index * PAGE_SIZE;
  945. else
  946. count = PAGE_SIZE;
  947. page = store->filemap[index];
  948. if (file)
  949. ret = read_page(file, index, bitmap,
  950. count, page);
  951. else
  952. ret = read_sb_page(
  953. bitmap->mddev,
  954. bitmap->mddev->bitmap_info.offset,
  955. page,
  956. index + node_offset, count);
  957. if (ret)
  958. goto err;
  959. oldindex = index;
  960. if (outofdate) {
  961. /*
  962. * if bitmap is out of date, dirty the
  963. * whole page and write it out
  964. */
  965. paddr = kmap_atomic(page);
  966. memset(paddr + offset, 0xff,
  967. PAGE_SIZE - offset);
  968. kunmap_atomic(paddr);
  969. write_page(bitmap, page, 1);
  970. ret = -EIO;
  971. if (test_bit(BITMAP_WRITE_ERROR,
  972. &bitmap->flags))
  973. goto err;
  974. }
  975. }
  976. paddr = kmap_atomic(page);
  977. if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
  978. b = test_bit(bit, paddr);
  979. else
  980. b = test_bit_le(bit, paddr);
  981. kunmap_atomic(paddr);
  982. if (b) {
  983. /* if the disk bit is set, set the memory bit */
  984. int needed = ((sector_t)(i+1) << bitmap->counts.chunkshift
  985. >= start);
  986. bitmap_set_memory_bits(bitmap,
  987. (sector_t)i << bitmap->counts.chunkshift,
  988. needed);
  989. bit_cnt++;
  990. }
  991. offset = 0;
  992. }
  993. printk(KERN_INFO "%s: bitmap initialized from disk: "
  994. "read %lu pages, set %lu of %lu bits\n",
  995. bmname(bitmap), store->file_pages,
  996. bit_cnt, chunks);
  997. return 0;
  998. err:
  999. printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
  1000. bmname(bitmap), ret);
  1001. return ret;
  1002. }
  1003. void bitmap_write_all(struct bitmap *bitmap)
  1004. {
  1005. /* We don't actually write all bitmap blocks here,
  1006. * just flag them as needing to be written
  1007. */
  1008. int i;
  1009. if (!bitmap || !bitmap->storage.filemap)
  1010. return;
  1011. if (bitmap->storage.file)
  1012. /* Only one copy, so nothing needed */
  1013. return;
  1014. for (i = 0; i < bitmap->storage.file_pages; i++)
  1015. set_page_attr(bitmap, i,
  1016. BITMAP_PAGE_NEEDWRITE);
  1017. bitmap->allclean = 0;
  1018. }
  1019. static void bitmap_count_page(struct bitmap_counts *bitmap,
  1020. sector_t offset, int inc)
  1021. {
  1022. sector_t chunk = offset >> bitmap->chunkshift;
  1023. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1024. bitmap->bp[page].count += inc;
  1025. bitmap_checkfree(bitmap, page);
  1026. }
  1027. static void bitmap_set_pending(struct bitmap_counts *bitmap, sector_t offset)
  1028. {
  1029. sector_t chunk = offset >> bitmap->chunkshift;
  1030. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1031. struct bitmap_page *bp = &bitmap->bp[page];
  1032. if (!bp->pending)
  1033. bp->pending = 1;
  1034. }
  1035. static bitmap_counter_t *bitmap_get_counter(struct bitmap_counts *bitmap,
  1036. sector_t offset, sector_t *blocks,
  1037. int create);
  1038. /*
  1039. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  1040. * out to disk
  1041. */
  1042. void bitmap_daemon_work(struct mddev *mddev)
  1043. {
  1044. struct bitmap *bitmap;
  1045. unsigned long j;
  1046. unsigned long nextpage;
  1047. sector_t blocks;
  1048. struct bitmap_counts *counts;
  1049. /* Use a mutex to guard daemon_work against
  1050. * bitmap_destroy.
  1051. */
  1052. mutex_lock(&mddev->bitmap_info.mutex);
  1053. bitmap = mddev->bitmap;
  1054. if (bitmap == NULL) {
  1055. mutex_unlock(&mddev->bitmap_info.mutex);
  1056. return;
  1057. }
  1058. if (time_before(jiffies, bitmap->daemon_lastrun
  1059. + mddev->bitmap_info.daemon_sleep))
  1060. goto done;
  1061. bitmap->daemon_lastrun = jiffies;
  1062. if (bitmap->allclean) {
  1063. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1064. goto done;
  1065. }
  1066. bitmap->allclean = 1;
  1067. /* Any file-page which is PENDING now needs to be written.
  1068. * So set NEEDWRITE now, then after we make any last-minute changes
  1069. * we will write it.
  1070. */
  1071. for (j = 0; j < bitmap->storage.file_pages; j++)
  1072. if (test_and_clear_page_attr(bitmap, j,
  1073. BITMAP_PAGE_PENDING))
  1074. set_page_attr(bitmap, j,
  1075. BITMAP_PAGE_NEEDWRITE);
  1076. if (bitmap->need_sync &&
  1077. mddev->bitmap_info.external == 0) {
  1078. /* Arrange for superblock update as well as
  1079. * other changes */
  1080. bitmap_super_t *sb;
  1081. bitmap->need_sync = 0;
  1082. if (bitmap->storage.filemap) {
  1083. sb = kmap_atomic(bitmap->storage.sb_page);
  1084. sb->events_cleared =
  1085. cpu_to_le64(bitmap->events_cleared);
  1086. kunmap_atomic(sb);
  1087. set_page_attr(bitmap, 0,
  1088. BITMAP_PAGE_NEEDWRITE);
  1089. }
  1090. }
  1091. /* Now look at the bitmap counters and if any are '2' or '1',
  1092. * decrement and handle accordingly.
  1093. */
  1094. counts = &bitmap->counts;
  1095. spin_lock_irq(&counts->lock);
  1096. nextpage = 0;
  1097. for (j = 0; j < counts->chunks; j++) {
  1098. bitmap_counter_t *bmc;
  1099. sector_t block = (sector_t)j << counts->chunkshift;
  1100. if (j == nextpage) {
  1101. nextpage += PAGE_COUNTER_RATIO;
  1102. if (!counts->bp[j >> PAGE_COUNTER_SHIFT].pending) {
  1103. j |= PAGE_COUNTER_MASK;
  1104. continue;
  1105. }
  1106. counts->bp[j >> PAGE_COUNTER_SHIFT].pending = 0;
  1107. }
  1108. bmc = bitmap_get_counter(counts,
  1109. block,
  1110. &blocks, 0);
  1111. if (!bmc) {
  1112. j |= PAGE_COUNTER_MASK;
  1113. continue;
  1114. }
  1115. if (*bmc == 1 && !bitmap->need_sync) {
  1116. /* We can clear the bit */
  1117. *bmc = 0;
  1118. bitmap_count_page(counts, block, -1);
  1119. bitmap_file_clear_bit(bitmap, block);
  1120. } else if (*bmc && *bmc <= 2) {
  1121. *bmc = 1;
  1122. bitmap_set_pending(counts, block);
  1123. bitmap->allclean = 0;
  1124. }
  1125. }
  1126. spin_unlock_irq(&counts->lock);
  1127. /* Now start writeout on any page in NEEDWRITE that isn't DIRTY.
  1128. * DIRTY pages need to be written by bitmap_unplug so it can wait
  1129. * for them.
  1130. * If we find any DIRTY page we stop there and let bitmap_unplug
  1131. * handle all the rest. This is important in the case where
  1132. * the first blocking holds the superblock and it has been updated.
  1133. * We mustn't write any other blocks before the superblock.
  1134. */
  1135. for (j = 0;
  1136. j < bitmap->storage.file_pages
  1137. && !test_bit(BITMAP_STALE, &bitmap->flags);
  1138. j++) {
  1139. if (test_page_attr(bitmap, j,
  1140. BITMAP_PAGE_DIRTY))
  1141. /* bitmap_unplug will handle the rest */
  1142. break;
  1143. if (test_and_clear_page_attr(bitmap, j,
  1144. BITMAP_PAGE_NEEDWRITE)) {
  1145. write_page(bitmap, bitmap->storage.filemap[j], 0);
  1146. }
  1147. }
  1148. done:
  1149. if (bitmap->allclean == 0)
  1150. mddev->thread->timeout =
  1151. mddev->bitmap_info.daemon_sleep;
  1152. mutex_unlock(&mddev->bitmap_info.mutex);
  1153. }
  1154. static bitmap_counter_t *bitmap_get_counter(struct bitmap_counts *bitmap,
  1155. sector_t offset, sector_t *blocks,
  1156. int create)
  1157. __releases(bitmap->lock)
  1158. __acquires(bitmap->lock)
  1159. {
  1160. /* If 'create', we might release the lock and reclaim it.
  1161. * The lock must have been taken with interrupts enabled.
  1162. * If !create, we don't release the lock.
  1163. */
  1164. sector_t chunk = offset >> bitmap->chunkshift;
  1165. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1166. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1167. sector_t csize;
  1168. int err;
  1169. err = bitmap_checkpage(bitmap, page, create);
  1170. if (bitmap->bp[page].hijacked ||
  1171. bitmap->bp[page].map == NULL)
  1172. csize = ((sector_t)1) << (bitmap->chunkshift +
  1173. PAGE_COUNTER_SHIFT - 1);
  1174. else
  1175. csize = ((sector_t)1) << bitmap->chunkshift;
  1176. *blocks = csize - (offset & (csize - 1));
  1177. if (err < 0)
  1178. return NULL;
  1179. /* now locked ... */
  1180. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1181. /* should we use the first or second counter field
  1182. * of the hijacked pointer? */
  1183. int hi = (pageoff > PAGE_COUNTER_MASK);
  1184. return &((bitmap_counter_t *)
  1185. &bitmap->bp[page].map)[hi];
  1186. } else /* page is allocated */
  1187. return (bitmap_counter_t *)
  1188. &(bitmap->bp[page].map[pageoff]);
  1189. }
  1190. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1191. {
  1192. if (!bitmap)
  1193. return 0;
  1194. if (behind) {
  1195. int bw;
  1196. atomic_inc(&bitmap->behind_writes);
  1197. bw = atomic_read(&bitmap->behind_writes);
  1198. if (bw > bitmap->behind_writes_used)
  1199. bitmap->behind_writes_used = bw;
  1200. pr_debug("inc write-behind count %d/%lu\n",
  1201. bw, bitmap->mddev->bitmap_info.max_write_behind);
  1202. }
  1203. while (sectors) {
  1204. sector_t blocks;
  1205. bitmap_counter_t *bmc;
  1206. spin_lock_irq(&bitmap->counts.lock);
  1207. bmc = bitmap_get_counter(&bitmap->counts, offset, &blocks, 1);
  1208. if (!bmc) {
  1209. spin_unlock_irq(&bitmap->counts.lock);
  1210. return 0;
  1211. }
  1212. if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
  1213. DEFINE_WAIT(__wait);
  1214. /* note that it is safe to do the prepare_to_wait
  1215. * after the test as long as we do it before dropping
  1216. * the spinlock.
  1217. */
  1218. prepare_to_wait(&bitmap->overflow_wait, &__wait,
  1219. TASK_UNINTERRUPTIBLE);
  1220. spin_unlock_irq(&bitmap->counts.lock);
  1221. schedule();
  1222. finish_wait(&bitmap->overflow_wait, &__wait);
  1223. continue;
  1224. }
  1225. switch (*bmc) {
  1226. case 0:
  1227. bitmap_file_set_bit(bitmap, offset);
  1228. bitmap_count_page(&bitmap->counts, offset, 1);
  1229. /* fall through */
  1230. case 1:
  1231. *bmc = 2;
  1232. }
  1233. (*bmc)++;
  1234. spin_unlock_irq(&bitmap->counts.lock);
  1235. offset += blocks;
  1236. if (sectors > blocks)
  1237. sectors -= blocks;
  1238. else
  1239. sectors = 0;
  1240. }
  1241. return 0;
  1242. }
  1243. EXPORT_SYMBOL(bitmap_startwrite);
  1244. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1245. int success, int behind)
  1246. {
  1247. if (!bitmap)
  1248. return;
  1249. if (behind) {
  1250. if (atomic_dec_and_test(&bitmap->behind_writes))
  1251. wake_up(&bitmap->behind_wait);
  1252. pr_debug("dec write-behind count %d/%lu\n",
  1253. atomic_read(&bitmap->behind_writes),
  1254. bitmap->mddev->bitmap_info.max_write_behind);
  1255. }
  1256. while (sectors) {
  1257. sector_t blocks;
  1258. unsigned long flags;
  1259. bitmap_counter_t *bmc;
  1260. spin_lock_irqsave(&bitmap->counts.lock, flags);
  1261. bmc = bitmap_get_counter(&bitmap->counts, offset, &blocks, 0);
  1262. if (!bmc) {
  1263. spin_unlock_irqrestore(&bitmap->counts.lock, flags);
  1264. return;
  1265. }
  1266. if (success && !bitmap->mddev->degraded &&
  1267. bitmap->events_cleared < bitmap->mddev->events) {
  1268. bitmap->events_cleared = bitmap->mddev->events;
  1269. bitmap->need_sync = 1;
  1270. sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
  1271. }
  1272. if (!success && !NEEDED(*bmc))
  1273. *bmc |= NEEDED_MASK;
  1274. if (COUNTER(*bmc) == COUNTER_MAX)
  1275. wake_up(&bitmap->overflow_wait);
  1276. (*bmc)--;
  1277. if (*bmc <= 2) {
  1278. bitmap_set_pending(&bitmap->counts, offset);
  1279. bitmap->allclean = 0;
  1280. }
  1281. spin_unlock_irqrestore(&bitmap->counts.lock, flags);
  1282. offset += blocks;
  1283. if (sectors > blocks)
  1284. sectors -= blocks;
  1285. else
  1286. sectors = 0;
  1287. }
  1288. }
  1289. EXPORT_SYMBOL(bitmap_endwrite);
  1290. static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1291. int degraded)
  1292. {
  1293. bitmap_counter_t *bmc;
  1294. int rv;
  1295. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1296. *blocks = 1024;
  1297. return 1; /* always resync if no bitmap */
  1298. }
  1299. spin_lock_irq(&bitmap->counts.lock);
  1300. bmc = bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
  1301. rv = 0;
  1302. if (bmc) {
  1303. /* locked */
  1304. if (RESYNC(*bmc))
  1305. rv = 1;
  1306. else if (NEEDED(*bmc)) {
  1307. rv = 1;
  1308. if (!degraded) { /* don't set/clear bits if degraded */
  1309. *bmc |= RESYNC_MASK;
  1310. *bmc &= ~NEEDED_MASK;
  1311. }
  1312. }
  1313. }
  1314. spin_unlock_irq(&bitmap->counts.lock);
  1315. return rv;
  1316. }
  1317. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1318. int degraded)
  1319. {
  1320. /* bitmap_start_sync must always report on multiples of whole
  1321. * pages, otherwise resync (which is very PAGE_SIZE based) will
  1322. * get confused.
  1323. * So call __bitmap_start_sync repeatedly (if needed) until
  1324. * At least PAGE_SIZE>>9 blocks are covered.
  1325. * Return the 'or' of the result.
  1326. */
  1327. int rv = 0;
  1328. sector_t blocks1;
  1329. *blocks = 0;
  1330. while (*blocks < (PAGE_SIZE>>9)) {
  1331. rv |= __bitmap_start_sync(bitmap, offset,
  1332. &blocks1, degraded);
  1333. offset += blocks1;
  1334. *blocks += blocks1;
  1335. }
  1336. return rv;
  1337. }
  1338. EXPORT_SYMBOL(bitmap_start_sync);
  1339. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
  1340. {
  1341. bitmap_counter_t *bmc;
  1342. unsigned long flags;
  1343. if (bitmap == NULL) {
  1344. *blocks = 1024;
  1345. return;
  1346. }
  1347. spin_lock_irqsave(&bitmap->counts.lock, flags);
  1348. bmc = bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
  1349. if (bmc == NULL)
  1350. goto unlock;
  1351. /* locked */
  1352. if (RESYNC(*bmc)) {
  1353. *bmc &= ~RESYNC_MASK;
  1354. if (!NEEDED(*bmc) && aborted)
  1355. *bmc |= NEEDED_MASK;
  1356. else {
  1357. if (*bmc <= 2) {
  1358. bitmap_set_pending(&bitmap->counts, offset);
  1359. bitmap->allclean = 0;
  1360. }
  1361. }
  1362. }
  1363. unlock:
  1364. spin_unlock_irqrestore(&bitmap->counts.lock, flags);
  1365. }
  1366. EXPORT_SYMBOL(bitmap_end_sync);
  1367. void bitmap_close_sync(struct bitmap *bitmap)
  1368. {
  1369. /* Sync has finished, and any bitmap chunks that weren't synced
  1370. * properly have been aborted. It remains to us to clear the
  1371. * RESYNC bit wherever it is still on
  1372. */
  1373. sector_t sector = 0;
  1374. sector_t blocks;
  1375. if (!bitmap)
  1376. return;
  1377. while (sector < bitmap->mddev->resync_max_sectors) {
  1378. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1379. sector += blocks;
  1380. }
  1381. }
  1382. EXPORT_SYMBOL(bitmap_close_sync);
  1383. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1384. {
  1385. sector_t s = 0;
  1386. sector_t blocks;
  1387. if (!bitmap)
  1388. return;
  1389. if (sector == 0) {
  1390. bitmap->last_end_sync = jiffies;
  1391. return;
  1392. }
  1393. if (time_before(jiffies, (bitmap->last_end_sync
  1394. + bitmap->mddev->bitmap_info.daemon_sleep)))
  1395. return;
  1396. wait_event(bitmap->mddev->recovery_wait,
  1397. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1398. bitmap->mddev->curr_resync_completed = sector;
  1399. set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
  1400. sector &= ~((1ULL << bitmap->counts.chunkshift) - 1);
  1401. s = 0;
  1402. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1403. bitmap_end_sync(bitmap, s, &blocks, 0);
  1404. s += blocks;
  1405. }
  1406. bitmap->last_end_sync = jiffies;
  1407. sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
  1408. }
  1409. EXPORT_SYMBOL(bitmap_cond_end_sync);
  1410. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1411. {
  1412. /* For each chunk covered by any of these sectors, set the
  1413. * counter to 2 and possibly set resync_needed. They should all
  1414. * be 0 at this point
  1415. */
  1416. sector_t secs;
  1417. bitmap_counter_t *bmc;
  1418. spin_lock_irq(&bitmap->counts.lock);
  1419. bmc = bitmap_get_counter(&bitmap->counts, offset, &secs, 1);
  1420. if (!bmc) {
  1421. spin_unlock_irq(&bitmap->counts.lock);
  1422. return;
  1423. }
  1424. if (!*bmc) {
  1425. *bmc = 2;
  1426. bitmap_count_page(&bitmap->counts, offset, 1);
  1427. bitmap_set_pending(&bitmap->counts, offset);
  1428. bitmap->allclean = 0;
  1429. }
  1430. if (needed)
  1431. *bmc |= NEEDED_MASK;
  1432. spin_unlock_irq(&bitmap->counts.lock);
  1433. }
  1434. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1435. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1436. {
  1437. unsigned long chunk;
  1438. for (chunk = s; chunk <= e; chunk++) {
  1439. sector_t sec = (sector_t)chunk << bitmap->counts.chunkshift;
  1440. bitmap_set_memory_bits(bitmap, sec, 1);
  1441. bitmap_file_set_bit(bitmap, sec);
  1442. if (sec < bitmap->mddev->recovery_cp)
  1443. /* We are asserting that the array is dirty,
  1444. * so move the recovery_cp address back so
  1445. * that it is obvious that it is dirty
  1446. */
  1447. bitmap->mddev->recovery_cp = sec;
  1448. }
  1449. }
  1450. /*
  1451. * flush out any pending updates
  1452. */
  1453. void bitmap_flush(struct mddev *mddev)
  1454. {
  1455. struct bitmap *bitmap = mddev->bitmap;
  1456. long sleep;
  1457. if (!bitmap) /* there was no bitmap */
  1458. return;
  1459. /* run the daemon_work three time to ensure everything is flushed
  1460. * that can be
  1461. */
  1462. sleep = mddev->bitmap_info.daemon_sleep * 2;
  1463. bitmap->daemon_lastrun -= sleep;
  1464. bitmap_daemon_work(mddev);
  1465. bitmap->daemon_lastrun -= sleep;
  1466. bitmap_daemon_work(mddev);
  1467. bitmap->daemon_lastrun -= sleep;
  1468. bitmap_daemon_work(mddev);
  1469. bitmap_update_sb(bitmap);
  1470. }
  1471. /*
  1472. * free memory that was allocated
  1473. */
  1474. static void bitmap_free(struct bitmap *bitmap)
  1475. {
  1476. unsigned long k, pages;
  1477. struct bitmap_page *bp;
  1478. if (!bitmap) /* there was no bitmap */
  1479. return;
  1480. if (mddev_is_clustered(bitmap->mddev) && bitmap->mddev->cluster_info &&
  1481. bitmap->cluster_slot == md_cluster_ops->slot_number(bitmap->mddev))
  1482. md_cluster_stop(bitmap->mddev);
  1483. /* Shouldn't be needed - but just in case.... */
  1484. wait_event(bitmap->write_wait,
  1485. atomic_read(&bitmap->pending_writes) == 0);
  1486. /* release the bitmap file */
  1487. bitmap_file_unmap(&bitmap->storage);
  1488. bp = bitmap->counts.bp;
  1489. pages = bitmap->counts.pages;
  1490. /* free all allocated memory */
  1491. if (bp) /* deallocate the page memory */
  1492. for (k = 0; k < pages; k++)
  1493. if (bp[k].map && !bp[k].hijacked)
  1494. kfree(bp[k].map);
  1495. kfree(bp);
  1496. kfree(bitmap);
  1497. }
  1498. void bitmap_destroy(struct mddev *mddev)
  1499. {
  1500. struct bitmap *bitmap = mddev->bitmap;
  1501. if (!bitmap) /* there was no bitmap */
  1502. return;
  1503. mutex_lock(&mddev->bitmap_info.mutex);
  1504. spin_lock(&mddev->lock);
  1505. mddev->bitmap = NULL; /* disconnect from the md device */
  1506. spin_unlock(&mddev->lock);
  1507. mutex_unlock(&mddev->bitmap_info.mutex);
  1508. if (mddev->thread)
  1509. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1510. if (bitmap->sysfs_can_clear)
  1511. sysfs_put(bitmap->sysfs_can_clear);
  1512. bitmap_free(bitmap);
  1513. }
  1514. /*
  1515. * initialize the bitmap structure
  1516. * if this returns an error, bitmap_destroy must be called to do clean up
  1517. */
  1518. struct bitmap *bitmap_create(struct mddev *mddev, int slot)
  1519. {
  1520. struct bitmap *bitmap;
  1521. sector_t blocks = mddev->resync_max_sectors;
  1522. struct file *file = mddev->bitmap_info.file;
  1523. int err;
  1524. struct kernfs_node *bm = NULL;
  1525. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1526. BUG_ON(file && mddev->bitmap_info.offset);
  1527. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1528. if (!bitmap)
  1529. return ERR_PTR(-ENOMEM);
  1530. spin_lock_init(&bitmap->counts.lock);
  1531. atomic_set(&bitmap->pending_writes, 0);
  1532. init_waitqueue_head(&bitmap->write_wait);
  1533. init_waitqueue_head(&bitmap->overflow_wait);
  1534. init_waitqueue_head(&bitmap->behind_wait);
  1535. bitmap->mddev = mddev;
  1536. bitmap->cluster_slot = slot;
  1537. if (mddev->kobj.sd)
  1538. bm = sysfs_get_dirent(mddev->kobj.sd, "bitmap");
  1539. if (bm) {
  1540. bitmap->sysfs_can_clear = sysfs_get_dirent(bm, "can_clear");
  1541. sysfs_put(bm);
  1542. } else
  1543. bitmap->sysfs_can_clear = NULL;
  1544. bitmap->storage.file = file;
  1545. if (file) {
  1546. get_file(file);
  1547. /* As future accesses to this file will use bmap,
  1548. * and bypass the page cache, we must sync the file
  1549. * first.
  1550. */
  1551. vfs_fsync(file, 1);
  1552. }
  1553. /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
  1554. if (!mddev->bitmap_info.external) {
  1555. /*
  1556. * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
  1557. * instructing us to create a new on-disk bitmap instance.
  1558. */
  1559. if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
  1560. err = bitmap_new_disk_sb(bitmap);
  1561. else
  1562. err = bitmap_read_sb(bitmap);
  1563. } else {
  1564. err = 0;
  1565. if (mddev->bitmap_info.chunksize == 0 ||
  1566. mddev->bitmap_info.daemon_sleep == 0)
  1567. /* chunksize and time_base need to be
  1568. * set first. */
  1569. err = -EINVAL;
  1570. }
  1571. if (err)
  1572. goto error;
  1573. bitmap->daemon_lastrun = jiffies;
  1574. err = bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1);
  1575. if (err)
  1576. goto error;
  1577. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1578. bitmap->counts.pages, bmname(bitmap));
  1579. err = test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0;
  1580. if (err)
  1581. goto error;
  1582. return bitmap;
  1583. error:
  1584. bitmap_free(bitmap);
  1585. return ERR_PTR(err);
  1586. }
  1587. int bitmap_load(struct mddev *mddev)
  1588. {
  1589. int err = 0;
  1590. sector_t start = 0;
  1591. sector_t sector = 0;
  1592. struct bitmap *bitmap = mddev->bitmap;
  1593. if (!bitmap)
  1594. goto out;
  1595. /* Clear out old bitmap info first: Either there is none, or we
  1596. * are resuming after someone else has possibly changed things,
  1597. * so we should forget old cached info.
  1598. * All chunks should be clean, but some might need_sync.
  1599. */
  1600. while (sector < mddev->resync_max_sectors) {
  1601. sector_t blocks;
  1602. bitmap_start_sync(bitmap, sector, &blocks, 0);
  1603. sector += blocks;
  1604. }
  1605. bitmap_close_sync(bitmap);
  1606. if (mddev->degraded == 0
  1607. || bitmap->events_cleared == mddev->events)
  1608. /* no need to keep dirty bits to optimise a
  1609. * re-add of a missing device */
  1610. start = mddev->recovery_cp;
  1611. mutex_lock(&mddev->bitmap_info.mutex);
  1612. err = bitmap_init_from_disk(bitmap, start);
  1613. mutex_unlock(&mddev->bitmap_info.mutex);
  1614. if (err)
  1615. goto out;
  1616. clear_bit(BITMAP_STALE, &bitmap->flags);
  1617. /* Kick recovery in case any bits were set */
  1618. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  1619. mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
  1620. md_wakeup_thread(mddev->thread);
  1621. bitmap_update_sb(bitmap);
  1622. if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
  1623. err = -EIO;
  1624. out:
  1625. return err;
  1626. }
  1627. EXPORT_SYMBOL_GPL(bitmap_load);
  1628. /* Loads the bitmap associated with slot and copies the resync information
  1629. * to our bitmap
  1630. */
  1631. int bitmap_copy_from_slot(struct mddev *mddev, int slot,
  1632. sector_t *low, sector_t *high, bool clear_bits)
  1633. {
  1634. int rv = 0, i, j;
  1635. sector_t block, lo = 0, hi = 0;
  1636. struct bitmap_counts *counts;
  1637. struct bitmap *bitmap = bitmap_create(mddev, slot);
  1638. if (IS_ERR(bitmap))
  1639. return PTR_ERR(bitmap);
  1640. rv = bitmap_read_sb(bitmap);
  1641. if (rv)
  1642. goto err;
  1643. rv = bitmap_init_from_disk(bitmap, 0);
  1644. if (rv)
  1645. goto err;
  1646. counts = &bitmap->counts;
  1647. for (j = 0; j < counts->chunks; j++) {
  1648. block = (sector_t)j << counts->chunkshift;
  1649. if (bitmap_file_test_bit(bitmap, block)) {
  1650. if (!lo)
  1651. lo = block;
  1652. hi = block;
  1653. bitmap_file_clear_bit(bitmap, block);
  1654. bitmap_set_memory_bits(mddev->bitmap, block, 1);
  1655. bitmap_file_set_bit(mddev->bitmap, block);
  1656. }
  1657. }
  1658. if (clear_bits) {
  1659. bitmap_update_sb(bitmap);
  1660. /* Setting this for the ev_page should be enough.
  1661. * And we do not require both write_all and PAGE_DIRT either
  1662. */
  1663. for (i = 0; i < bitmap->storage.file_pages; i++)
  1664. set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
  1665. bitmap_write_all(bitmap);
  1666. bitmap_unplug(bitmap);
  1667. }
  1668. *low = lo;
  1669. *high = hi;
  1670. err:
  1671. bitmap_free(bitmap);
  1672. return rv;
  1673. }
  1674. EXPORT_SYMBOL_GPL(bitmap_copy_from_slot);
  1675. void bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
  1676. {
  1677. unsigned long chunk_kb;
  1678. struct bitmap_counts *counts;
  1679. if (!bitmap)
  1680. return;
  1681. counts = &bitmap->counts;
  1682. chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
  1683. seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
  1684. "%lu%s chunk",
  1685. counts->pages - counts->missing_pages,
  1686. counts->pages,
  1687. (counts->pages - counts->missing_pages)
  1688. << (PAGE_SHIFT - 10),
  1689. chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
  1690. chunk_kb ? "KB" : "B");
  1691. if (bitmap->storage.file) {
  1692. seq_printf(seq, ", file: ");
  1693. seq_file_path(seq, bitmap->storage.file, " \t\n");
  1694. }
  1695. seq_printf(seq, "\n");
  1696. }
  1697. int bitmap_resize(struct bitmap *bitmap, sector_t blocks,
  1698. int chunksize, int init)
  1699. {
  1700. /* If chunk_size is 0, choose an appropriate chunk size.
  1701. * Then possibly allocate new storage space.
  1702. * Then quiesce, copy bits, replace bitmap, and re-start
  1703. *
  1704. * This function is called both to set up the initial bitmap
  1705. * and to resize the bitmap while the array is active.
  1706. * If this happens as a result of the array being resized,
  1707. * chunksize will be zero, and we need to choose a suitable
  1708. * chunksize, otherwise we use what we are given.
  1709. */
  1710. struct bitmap_storage store;
  1711. struct bitmap_counts old_counts;
  1712. unsigned long chunks;
  1713. sector_t block;
  1714. sector_t old_blocks, new_blocks;
  1715. int chunkshift;
  1716. int ret = 0;
  1717. long pages;
  1718. struct bitmap_page *new_bp;
  1719. if (chunksize == 0) {
  1720. /* If there is enough space, leave the chunk size unchanged,
  1721. * else increase by factor of two until there is enough space.
  1722. */
  1723. long bytes;
  1724. long space = bitmap->mddev->bitmap_info.space;
  1725. if (space == 0) {
  1726. /* We don't know how much space there is, so limit
  1727. * to current size - in sectors.
  1728. */
  1729. bytes = DIV_ROUND_UP(bitmap->counts.chunks, 8);
  1730. if (!bitmap->mddev->bitmap_info.external)
  1731. bytes += sizeof(bitmap_super_t);
  1732. space = DIV_ROUND_UP(bytes, 512);
  1733. bitmap->mddev->bitmap_info.space = space;
  1734. }
  1735. chunkshift = bitmap->counts.chunkshift;
  1736. chunkshift--;
  1737. do {
  1738. /* 'chunkshift' is shift from block size to chunk size */
  1739. chunkshift++;
  1740. chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
  1741. bytes = DIV_ROUND_UP(chunks, 8);
  1742. if (!bitmap->mddev->bitmap_info.external)
  1743. bytes += sizeof(bitmap_super_t);
  1744. } while (bytes > (space << 9));
  1745. } else
  1746. chunkshift = ffz(~chunksize) - BITMAP_BLOCK_SHIFT;
  1747. chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
  1748. memset(&store, 0, sizeof(store));
  1749. if (bitmap->mddev->bitmap_info.offset || bitmap->mddev->bitmap_info.file)
  1750. ret = bitmap_storage_alloc(&store, chunks,
  1751. !bitmap->mddev->bitmap_info.external,
  1752. bitmap->cluster_slot);
  1753. if (ret)
  1754. goto err;
  1755. pages = DIV_ROUND_UP(chunks, PAGE_COUNTER_RATIO);
  1756. new_bp = kzalloc(pages * sizeof(*new_bp), GFP_KERNEL);
  1757. ret = -ENOMEM;
  1758. if (!new_bp) {
  1759. bitmap_file_unmap(&store);
  1760. goto err;
  1761. }
  1762. if (!init)
  1763. bitmap->mddev->pers->quiesce(bitmap->mddev, 1);
  1764. store.file = bitmap->storage.file;
  1765. bitmap->storage.file = NULL;
  1766. if (store.sb_page && bitmap->storage.sb_page)
  1767. memcpy(page_address(store.sb_page),
  1768. page_address(bitmap->storage.sb_page),
  1769. sizeof(bitmap_super_t));
  1770. bitmap_file_unmap(&bitmap->storage);
  1771. bitmap->storage = store;
  1772. old_counts = bitmap->counts;
  1773. bitmap->counts.bp = new_bp;
  1774. bitmap->counts.pages = pages;
  1775. bitmap->counts.missing_pages = pages;
  1776. bitmap->counts.chunkshift = chunkshift;
  1777. bitmap->counts.chunks = chunks;
  1778. bitmap->mddev->bitmap_info.chunksize = 1 << (chunkshift +
  1779. BITMAP_BLOCK_SHIFT);
  1780. blocks = min(old_counts.chunks << old_counts.chunkshift,
  1781. chunks << chunkshift);
  1782. spin_lock_irq(&bitmap->counts.lock);
  1783. for (block = 0; block < blocks; ) {
  1784. bitmap_counter_t *bmc_old, *bmc_new;
  1785. int set;
  1786. bmc_old = bitmap_get_counter(&old_counts, block,
  1787. &old_blocks, 0);
  1788. set = bmc_old && NEEDED(*bmc_old);
  1789. if (set) {
  1790. bmc_new = bitmap_get_counter(&bitmap->counts, block,
  1791. &new_blocks, 1);
  1792. if (*bmc_new == 0) {
  1793. /* need to set on-disk bits too. */
  1794. sector_t end = block + new_blocks;
  1795. sector_t start = block >> chunkshift;
  1796. start <<= chunkshift;
  1797. while (start < end) {
  1798. bitmap_file_set_bit(bitmap, block);
  1799. start += 1 << chunkshift;
  1800. }
  1801. *bmc_new = 2;
  1802. bitmap_count_page(&bitmap->counts,
  1803. block, 1);
  1804. bitmap_set_pending(&bitmap->counts,
  1805. block);
  1806. }
  1807. *bmc_new |= NEEDED_MASK;
  1808. if (new_blocks < old_blocks)
  1809. old_blocks = new_blocks;
  1810. }
  1811. block += old_blocks;
  1812. }
  1813. if (!init) {
  1814. int i;
  1815. while (block < (chunks << chunkshift)) {
  1816. bitmap_counter_t *bmc;
  1817. bmc = bitmap_get_counter(&bitmap->counts, block,
  1818. &new_blocks, 1);
  1819. if (bmc) {
  1820. /* new space. It needs to be resynced, so
  1821. * we set NEEDED_MASK.
  1822. */
  1823. if (*bmc == 0) {
  1824. *bmc = NEEDED_MASK | 2;
  1825. bitmap_count_page(&bitmap->counts,
  1826. block, 1);
  1827. bitmap_set_pending(&bitmap->counts,
  1828. block);
  1829. }
  1830. }
  1831. block += new_blocks;
  1832. }
  1833. for (i = 0; i < bitmap->storage.file_pages; i++)
  1834. set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
  1835. }
  1836. spin_unlock_irq(&bitmap->counts.lock);
  1837. if (!init) {
  1838. bitmap_unplug(bitmap);
  1839. bitmap->mddev->pers->quiesce(bitmap->mddev, 0);
  1840. }
  1841. ret = 0;
  1842. err:
  1843. return ret;
  1844. }
  1845. EXPORT_SYMBOL_GPL(bitmap_resize);
  1846. static ssize_t
  1847. location_show(struct mddev *mddev, char *page)
  1848. {
  1849. ssize_t len;
  1850. if (mddev->bitmap_info.file)
  1851. len = sprintf(page, "file");
  1852. else if (mddev->bitmap_info.offset)
  1853. len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
  1854. else
  1855. len = sprintf(page, "none");
  1856. len += sprintf(page+len, "\n");
  1857. return len;
  1858. }
  1859. static ssize_t
  1860. location_store(struct mddev *mddev, const char *buf, size_t len)
  1861. {
  1862. if (mddev->pers) {
  1863. if (!mddev->pers->quiesce)
  1864. return -EBUSY;
  1865. if (mddev->recovery || mddev->sync_thread)
  1866. return -EBUSY;
  1867. }
  1868. if (mddev->bitmap || mddev->bitmap_info.file ||
  1869. mddev->bitmap_info.offset) {
  1870. /* bitmap already configured. Only option is to clear it */
  1871. if (strncmp(buf, "none", 4) != 0)
  1872. return -EBUSY;
  1873. if (mddev->pers) {
  1874. mddev->pers->quiesce(mddev, 1);
  1875. bitmap_destroy(mddev);
  1876. mddev->pers->quiesce(mddev, 0);
  1877. }
  1878. mddev->bitmap_info.offset = 0;
  1879. if (mddev->bitmap_info.file) {
  1880. struct file *f = mddev->bitmap_info.file;
  1881. mddev->bitmap_info.file = NULL;
  1882. fput(f);
  1883. }
  1884. } else {
  1885. /* No bitmap, OK to set a location */
  1886. long long offset;
  1887. if (strncmp(buf, "none", 4) == 0)
  1888. /* nothing to be done */;
  1889. else if (strncmp(buf, "file:", 5) == 0) {
  1890. /* Not supported yet */
  1891. return -EINVAL;
  1892. } else {
  1893. int rv;
  1894. if (buf[0] == '+')
  1895. rv = kstrtoll(buf+1, 10, &offset);
  1896. else
  1897. rv = kstrtoll(buf, 10, &offset);
  1898. if (rv)
  1899. return rv;
  1900. if (offset == 0)
  1901. return -EINVAL;
  1902. if (mddev->bitmap_info.external == 0 &&
  1903. mddev->major_version == 0 &&
  1904. offset != mddev->bitmap_info.default_offset)
  1905. return -EINVAL;
  1906. mddev->bitmap_info.offset = offset;
  1907. if (mddev->pers) {
  1908. struct bitmap *bitmap;
  1909. mddev->pers->quiesce(mddev, 1);
  1910. bitmap = bitmap_create(mddev, -1);
  1911. if (IS_ERR(bitmap))
  1912. rv = PTR_ERR(bitmap);
  1913. else {
  1914. mddev->bitmap = bitmap;
  1915. rv = bitmap_load(mddev);
  1916. if (rv) {
  1917. bitmap_destroy(mddev);
  1918. mddev->bitmap_info.offset = 0;
  1919. }
  1920. }
  1921. mddev->pers->quiesce(mddev, 0);
  1922. if (rv)
  1923. return rv;
  1924. }
  1925. }
  1926. }
  1927. if (!mddev->external) {
  1928. /* Ensure new bitmap info is stored in
  1929. * metadata promptly.
  1930. */
  1931. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1932. md_wakeup_thread(mddev->thread);
  1933. }
  1934. return len;
  1935. }
  1936. static struct md_sysfs_entry bitmap_location =
  1937. __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
  1938. /* 'bitmap/space' is the space available at 'location' for the
  1939. * bitmap. This allows the kernel to know when it is safe to
  1940. * resize the bitmap to match a resized array.
  1941. */
  1942. static ssize_t
  1943. space_show(struct mddev *mddev, char *page)
  1944. {
  1945. return sprintf(page, "%lu\n", mddev->bitmap_info.space);
  1946. }
  1947. static ssize_t
  1948. space_store(struct mddev *mddev, const char *buf, size_t len)
  1949. {
  1950. unsigned long sectors;
  1951. int rv;
  1952. rv = kstrtoul(buf, 10, &sectors);
  1953. if (rv)
  1954. return rv;
  1955. if (sectors == 0)
  1956. return -EINVAL;
  1957. if (mddev->bitmap &&
  1958. sectors < (mddev->bitmap->storage.bytes + 511) >> 9)
  1959. return -EFBIG; /* Bitmap is too big for this small space */
  1960. /* could make sure it isn't too big, but that isn't really
  1961. * needed - user-space should be careful.
  1962. */
  1963. mddev->bitmap_info.space = sectors;
  1964. return len;
  1965. }
  1966. static struct md_sysfs_entry bitmap_space =
  1967. __ATTR(space, S_IRUGO|S_IWUSR, space_show, space_store);
  1968. static ssize_t
  1969. timeout_show(struct mddev *mddev, char *page)
  1970. {
  1971. ssize_t len;
  1972. unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
  1973. unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
  1974. len = sprintf(page, "%lu", secs);
  1975. if (jifs)
  1976. len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
  1977. len += sprintf(page+len, "\n");
  1978. return len;
  1979. }
  1980. static ssize_t
  1981. timeout_store(struct mddev *mddev, const char *buf, size_t len)
  1982. {
  1983. /* timeout can be set at any time */
  1984. unsigned long timeout;
  1985. int rv = strict_strtoul_scaled(buf, &timeout, 4);
  1986. if (rv)
  1987. return rv;
  1988. /* just to make sure we don't overflow... */
  1989. if (timeout >= LONG_MAX / HZ)
  1990. return -EINVAL;
  1991. timeout = timeout * HZ / 10000;
  1992. if (timeout >= MAX_SCHEDULE_TIMEOUT)
  1993. timeout = MAX_SCHEDULE_TIMEOUT-1;
  1994. if (timeout < 1)
  1995. timeout = 1;
  1996. mddev->bitmap_info.daemon_sleep = timeout;
  1997. if (mddev->thread) {
  1998. /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
  1999. * the bitmap is all clean and we don't need to
  2000. * adjust the timeout right now
  2001. */
  2002. if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
  2003. mddev->thread->timeout = timeout;
  2004. md_wakeup_thread(mddev->thread);
  2005. }
  2006. }
  2007. return len;
  2008. }
  2009. static struct md_sysfs_entry bitmap_timeout =
  2010. __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
  2011. static ssize_t
  2012. backlog_show(struct mddev *mddev, char *page)
  2013. {
  2014. return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
  2015. }
  2016. static ssize_t
  2017. backlog_store(struct mddev *mddev, const char *buf, size_t len)
  2018. {
  2019. unsigned long backlog;
  2020. int rv = kstrtoul(buf, 10, &backlog);
  2021. if (rv)
  2022. return rv;
  2023. if (backlog > COUNTER_MAX)
  2024. return -EINVAL;
  2025. mddev->bitmap_info.max_write_behind = backlog;
  2026. return len;
  2027. }
  2028. static struct md_sysfs_entry bitmap_backlog =
  2029. __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
  2030. static ssize_t
  2031. chunksize_show(struct mddev *mddev, char *page)
  2032. {
  2033. return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
  2034. }
  2035. static ssize_t
  2036. chunksize_store(struct mddev *mddev, const char *buf, size_t len)
  2037. {
  2038. /* Can only be changed when no bitmap is active */
  2039. int rv;
  2040. unsigned long csize;
  2041. if (mddev->bitmap)
  2042. return -EBUSY;
  2043. rv = kstrtoul(buf, 10, &csize);
  2044. if (rv)
  2045. return rv;
  2046. if (csize < 512 ||
  2047. !is_power_of_2(csize))
  2048. return -EINVAL;
  2049. mddev->bitmap_info.chunksize = csize;
  2050. return len;
  2051. }
  2052. static struct md_sysfs_entry bitmap_chunksize =
  2053. __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
  2054. static ssize_t metadata_show(struct mddev *mddev, char *page)
  2055. {
  2056. if (mddev_is_clustered(mddev))
  2057. return sprintf(page, "clustered\n");
  2058. return sprintf(page, "%s\n", (mddev->bitmap_info.external
  2059. ? "external" : "internal"));
  2060. }
  2061. static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
  2062. {
  2063. if (mddev->bitmap ||
  2064. mddev->bitmap_info.file ||
  2065. mddev->bitmap_info.offset)
  2066. return -EBUSY;
  2067. if (strncmp(buf, "external", 8) == 0)
  2068. mddev->bitmap_info.external = 1;
  2069. else if ((strncmp(buf, "internal", 8) == 0) ||
  2070. (strncmp(buf, "clustered", 9) == 0))
  2071. mddev->bitmap_info.external = 0;
  2072. else
  2073. return -EINVAL;
  2074. return len;
  2075. }
  2076. static struct md_sysfs_entry bitmap_metadata =
  2077. __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
  2078. static ssize_t can_clear_show(struct mddev *mddev, char *page)
  2079. {
  2080. int len;
  2081. spin_lock(&mddev->lock);
  2082. if (mddev->bitmap)
  2083. len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
  2084. "false" : "true"));
  2085. else
  2086. len = sprintf(page, "\n");
  2087. spin_unlock(&mddev->lock);
  2088. return len;
  2089. }
  2090. static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
  2091. {
  2092. if (mddev->bitmap == NULL)
  2093. return -ENOENT;
  2094. if (strncmp(buf, "false", 5) == 0)
  2095. mddev->bitmap->need_sync = 1;
  2096. else if (strncmp(buf, "true", 4) == 0) {
  2097. if (mddev->degraded)
  2098. return -EBUSY;
  2099. mddev->bitmap->need_sync = 0;
  2100. } else
  2101. return -EINVAL;
  2102. return len;
  2103. }
  2104. static struct md_sysfs_entry bitmap_can_clear =
  2105. __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
  2106. static ssize_t
  2107. behind_writes_used_show(struct mddev *mddev, char *page)
  2108. {
  2109. ssize_t ret;
  2110. spin_lock(&mddev->lock);
  2111. if (mddev->bitmap == NULL)
  2112. ret = sprintf(page, "0\n");
  2113. else
  2114. ret = sprintf(page, "%lu\n",
  2115. mddev->bitmap->behind_writes_used);
  2116. spin_unlock(&mddev->lock);
  2117. return ret;
  2118. }
  2119. static ssize_t
  2120. behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
  2121. {
  2122. if (mddev->bitmap)
  2123. mddev->bitmap->behind_writes_used = 0;
  2124. return len;
  2125. }
  2126. static struct md_sysfs_entry max_backlog_used =
  2127. __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
  2128. behind_writes_used_show, behind_writes_used_reset);
  2129. static struct attribute *md_bitmap_attrs[] = {
  2130. &bitmap_location.attr,
  2131. &bitmap_space.attr,
  2132. &bitmap_timeout.attr,
  2133. &bitmap_backlog.attr,
  2134. &bitmap_chunksize.attr,
  2135. &bitmap_metadata.attr,
  2136. &bitmap_can_clear.attr,
  2137. &max_backlog_used.attr,
  2138. NULL
  2139. };
  2140. struct attribute_group md_bitmap_group = {
  2141. .name = "bitmap",
  2142. .attrs = md_bitmap_attrs,
  2143. };