bitmap.c 69 KB

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