bitmap.c 67 KB

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