bitmap.c 65 KB

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