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