bitmap.c 64 KB

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