md-bitmap.c 71 KB

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