md-bitmap.c 69 KB

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