bitmap.c 64 KB

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