bitmap.c 60 KB

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