bitmap.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274
  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. mappage = kzalloc(PAGE_SIZE, GFP_NOIO);
  66. spin_lock_irq(&bitmap->lock);
  67. if (mappage == NULL) {
  68. pr_debug("md/bitmap: map page allocation failed, hijacking\n");
  69. /* failed - set the hijacked flag so that we can use the
  70. * pointer as a counter */
  71. if (!bitmap->bp[page].map)
  72. bitmap->bp[page].hijacked = 1;
  73. } else if (bitmap->bp[page].map ||
  74. bitmap->bp[page].hijacked) {
  75. /* somebody beat us to getting the page */
  76. kfree(mappage);
  77. return 0;
  78. } else {
  79. /* no page was in place and we have one, so install it */
  80. bitmap->bp[page].map = mappage;
  81. bitmap->missing_pages--;
  82. }
  83. return 0;
  84. }
  85. /* if page is completely empty, put it back on the free list, or dealloc it */
  86. /* if page was hijacked, unmark the flag so it might get alloced next time */
  87. /* Note: lock should be held when calling this */
  88. static void bitmap_checkfree(struct bitmap_counts *bitmap, unsigned long page)
  89. {
  90. char *ptr;
  91. if (bitmap->bp[page].count) /* page is still busy */
  92. return;
  93. /* page is no longer in use, it can be released */
  94. if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
  95. bitmap->bp[page].hijacked = 0;
  96. bitmap->bp[page].map = NULL;
  97. } else {
  98. /* normal case, free the page */
  99. ptr = bitmap->bp[page].map;
  100. bitmap->bp[page].map = NULL;
  101. bitmap->missing_pages++;
  102. kfree(ptr);
  103. }
  104. }
  105. /*
  106. * bitmap file handling - read and write the bitmap file and its superblock
  107. */
  108. /*
  109. * basic page I/O operations
  110. */
  111. /* IO operations when bitmap is stored near all superblocks */
  112. static int read_sb_page(struct mddev *mddev, loff_t offset,
  113. struct page *page,
  114. unsigned long index, int size)
  115. {
  116. /* choose a good rdev and read the page from there */
  117. struct md_rdev *rdev;
  118. sector_t target;
  119. rdev_for_each(rdev, mddev) {
  120. if (! test_bit(In_sync, &rdev->flags)
  121. || test_bit(Faulty, &rdev->flags))
  122. continue;
  123. target = offset + index * (PAGE_SIZE/512);
  124. if (sync_page_io(rdev, target,
  125. roundup(size, bdev_logical_block_size(rdev->bdev)),
  126. page, READ, true)) {
  127. page->index = index;
  128. return 0;
  129. }
  130. }
  131. return -EIO;
  132. }
  133. static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev)
  134. {
  135. /* Iterate the disks of an mddev, using rcu to protect access to the
  136. * linked list, and raising the refcount of devices we return to ensure
  137. * they don't disappear while in use.
  138. * As devices are only added or removed when raid_disk is < 0 and
  139. * nr_pending is 0 and In_sync is clear, the entries we return will
  140. * still be in the same position on the list when we re-enter
  141. * list_for_each_entry_continue_rcu.
  142. */
  143. rcu_read_lock();
  144. if (rdev == NULL)
  145. /* start at the beginning */
  146. rdev = list_entry_rcu(&mddev->disks, struct md_rdev, same_set);
  147. else {
  148. /* release the previous rdev and start from there. */
  149. rdev_dec_pending(rdev, mddev);
  150. }
  151. list_for_each_entry_continue_rcu(rdev, &mddev->disks, same_set) {
  152. if (rdev->raid_disk >= 0 &&
  153. !test_bit(Faulty, &rdev->flags)) {
  154. /* this is a usable devices */
  155. atomic_inc(&rdev->nr_pending);
  156. rcu_read_unlock();
  157. return rdev;
  158. }
  159. }
  160. rcu_read_unlock();
  161. return NULL;
  162. }
  163. static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
  164. {
  165. struct md_rdev *rdev = NULL;
  166. struct block_device *bdev;
  167. struct mddev *mddev = bitmap->mddev;
  168. struct bitmap_storage *store = &bitmap->storage;
  169. while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
  170. int size = PAGE_SIZE;
  171. loff_t offset = mddev->bitmap_info.offset;
  172. bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
  173. if (page->index == store->file_pages-1) {
  174. int last_page_size = store->bytes & (PAGE_SIZE-1);
  175. if (last_page_size == 0)
  176. last_page_size = PAGE_SIZE;
  177. size = roundup(last_page_size,
  178. bdev_logical_block_size(bdev));
  179. }
  180. /* Just make sure we aren't corrupting data or
  181. * metadata
  182. */
  183. if (mddev->external) {
  184. /* Bitmap could be anywhere. */
  185. if (rdev->sb_start + offset + (page->index
  186. * (PAGE_SIZE/512))
  187. > rdev->data_offset
  188. &&
  189. rdev->sb_start + offset
  190. < (rdev->data_offset + mddev->dev_sectors
  191. + (PAGE_SIZE/512)))
  192. goto bad_alignment;
  193. } else if (offset < 0) {
  194. /* DATA BITMAP METADATA */
  195. if (offset
  196. + (long)(page->index * (PAGE_SIZE/512))
  197. + size/512 > 0)
  198. /* bitmap runs in to metadata */
  199. goto bad_alignment;
  200. if (rdev->data_offset + mddev->dev_sectors
  201. > rdev->sb_start + offset)
  202. /* data runs in to bitmap */
  203. goto bad_alignment;
  204. } else if (rdev->sb_start < rdev->data_offset) {
  205. /* METADATA BITMAP DATA */
  206. if (rdev->sb_start
  207. + offset
  208. + page->index*(PAGE_SIZE/512) + size/512
  209. > rdev->data_offset)
  210. /* bitmap runs in to data */
  211. goto bad_alignment;
  212. } else {
  213. /* DATA METADATA BITMAP - no problems */
  214. }
  215. md_super_write(mddev, rdev,
  216. rdev->sb_start + offset
  217. + page->index * (PAGE_SIZE/512),
  218. size,
  219. page);
  220. }
  221. if (wait)
  222. md_super_wait(mddev);
  223. return 0;
  224. bad_alignment:
  225. return -EINVAL;
  226. }
  227. static void bitmap_file_kick(struct bitmap *bitmap);
  228. /*
  229. * write out a page to a file
  230. */
  231. static void write_page(struct bitmap *bitmap, struct page *page, int wait)
  232. {
  233. struct buffer_head *bh;
  234. if (bitmap->storage.file == NULL) {
  235. switch (write_sb_page(bitmap, page, wait)) {
  236. case -EINVAL:
  237. set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
  238. }
  239. } else {
  240. bh = page_buffers(page);
  241. while (bh && bh->b_blocknr) {
  242. atomic_inc(&bitmap->pending_writes);
  243. set_buffer_locked(bh);
  244. set_buffer_mapped(bh);
  245. submit_bh(WRITE | REQ_SYNC, bh);
  246. bh = bh->b_this_page;
  247. }
  248. if (wait)
  249. wait_event(bitmap->write_wait,
  250. atomic_read(&bitmap->pending_writes)==0);
  251. }
  252. if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
  253. bitmap_file_kick(bitmap);
  254. }
  255. static void end_bitmap_write(struct buffer_head *bh, int uptodate)
  256. {
  257. struct bitmap *bitmap = bh->b_private;
  258. if (!uptodate)
  259. set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
  260. if (atomic_dec_and_test(&bitmap->pending_writes))
  261. wake_up(&bitmap->write_wait);
  262. }
  263. /* copied from buffer.c */
  264. static void
  265. __clear_page_buffers(struct page *page)
  266. {
  267. ClearPagePrivate(page);
  268. set_page_private(page, 0);
  269. page_cache_release(page);
  270. }
  271. static void free_buffers(struct page *page)
  272. {
  273. struct buffer_head *bh;
  274. if (!PagePrivate(page))
  275. return;
  276. bh = page_buffers(page);
  277. while (bh) {
  278. struct buffer_head *next = bh->b_this_page;
  279. free_buffer_head(bh);
  280. bh = next;
  281. }
  282. __clear_page_buffers(page);
  283. put_page(page);
  284. }
  285. /* read a page from a file.
  286. * We both read the page, and attach buffers to the page to record the
  287. * address of each block (using bmap). These addresses will be used
  288. * to write the block later, completely bypassing the filesystem.
  289. * This usage is similar to how swap files are handled, and allows us
  290. * to write to a file with no concerns of memory allocation failing.
  291. */
  292. static int read_page(struct file *file, unsigned long index,
  293. struct bitmap *bitmap,
  294. unsigned long count,
  295. struct page *page)
  296. {
  297. int ret = 0;
  298. struct inode *inode = file->f_path.dentry->d_inode;
  299. struct buffer_head *bh;
  300. sector_t block;
  301. pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
  302. (unsigned long long)index << PAGE_SHIFT);
  303. bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
  304. if (!bh) {
  305. ret = -ENOMEM;
  306. goto out;
  307. }
  308. attach_page_buffers(page, bh);
  309. block = index << (PAGE_SHIFT - inode->i_blkbits);
  310. while (bh) {
  311. if (count == 0)
  312. bh->b_blocknr = 0;
  313. else {
  314. bh->b_blocknr = bmap(inode, block);
  315. if (bh->b_blocknr == 0) {
  316. /* Cannot use this file! */
  317. ret = -EINVAL;
  318. goto out;
  319. }
  320. bh->b_bdev = inode->i_sb->s_bdev;
  321. if (count < (1<<inode->i_blkbits))
  322. count = 0;
  323. else
  324. count -= (1<<inode->i_blkbits);
  325. bh->b_end_io = end_bitmap_write;
  326. bh->b_private = bitmap;
  327. atomic_inc(&bitmap->pending_writes);
  328. set_buffer_locked(bh);
  329. set_buffer_mapped(bh);
  330. submit_bh(READ, bh);
  331. }
  332. block++;
  333. bh = bh->b_this_page;
  334. }
  335. page->index = index;
  336. wait_event(bitmap->write_wait,
  337. atomic_read(&bitmap->pending_writes)==0);
  338. if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
  339. ret = -EIO;
  340. out:
  341. if (ret)
  342. printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %d\n",
  343. (int)PAGE_SIZE,
  344. (unsigned long long)index << PAGE_SHIFT,
  345. ret);
  346. return ret;
  347. }
  348. /*
  349. * bitmap file superblock operations
  350. */
  351. /* update the event counter and sync the superblock to disk */
  352. void bitmap_update_sb(struct bitmap *bitmap)
  353. {
  354. bitmap_super_t *sb;
  355. if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
  356. return;
  357. if (bitmap->mddev->bitmap_info.external)
  358. return;
  359. if (!bitmap->storage.sb_page) /* no superblock */
  360. return;
  361. sb = kmap_atomic(bitmap->storage.sb_page);
  362. sb->events = cpu_to_le64(bitmap->mddev->events);
  363. if (bitmap->mddev->events < bitmap->events_cleared)
  364. /* rocking back to read-only */
  365. bitmap->events_cleared = bitmap->mddev->events;
  366. sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
  367. sb->state = cpu_to_le32(bitmap->flags);
  368. /* Just in case these have been changed via sysfs: */
  369. sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
  370. sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
  371. /* This might have been changed by a reshape */
  372. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  373. sb->chunksize = cpu_to_le32(bitmap->mddev->bitmap_info.chunksize);
  374. sb->sectors_reserved = cpu_to_le32(bitmap->mddev->
  375. bitmap_info.space);
  376. kunmap_atomic(sb);
  377. write_page(bitmap, bitmap->storage.sb_page, 1);
  378. }
  379. /* print out the bitmap file superblock */
  380. void bitmap_print_sb(struct bitmap *bitmap)
  381. {
  382. bitmap_super_t *sb;
  383. if (!bitmap || !bitmap->storage.sb_page)
  384. return;
  385. sb = kmap_atomic(bitmap->storage.sb_page);
  386. printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
  387. printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
  388. printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
  389. printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
  390. *(__u32 *)(sb->uuid+0),
  391. *(__u32 *)(sb->uuid+4),
  392. *(__u32 *)(sb->uuid+8),
  393. *(__u32 *)(sb->uuid+12));
  394. printk(KERN_DEBUG " events: %llu\n",
  395. (unsigned long long) le64_to_cpu(sb->events));
  396. printk(KERN_DEBUG "events cleared: %llu\n",
  397. (unsigned long long) le64_to_cpu(sb->events_cleared));
  398. printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
  399. printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
  400. printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
  401. printk(KERN_DEBUG " sync size: %llu KB\n",
  402. (unsigned long long)le64_to_cpu(sb->sync_size)/2);
  403. printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
  404. kunmap_atomic(sb);
  405. }
  406. /*
  407. * bitmap_new_disk_sb
  408. * @bitmap
  409. *
  410. * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
  411. * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
  412. * This function verifies 'bitmap_info' and populates the on-disk bitmap
  413. * structure, which is to be written to disk.
  414. *
  415. * Returns: 0 on success, -Exxx on error
  416. */
  417. static int bitmap_new_disk_sb(struct bitmap *bitmap)
  418. {
  419. bitmap_super_t *sb;
  420. unsigned long chunksize, daemon_sleep, write_behind;
  421. int err = -EINVAL;
  422. bitmap->storage.sb_page = alloc_page(GFP_KERNEL);
  423. if (IS_ERR(bitmap->storage.sb_page)) {
  424. err = PTR_ERR(bitmap->storage.sb_page);
  425. bitmap->storage.sb_page = NULL;
  426. return err;
  427. }
  428. bitmap->storage.sb_page->index = 0;
  429. sb = kmap_atomic(bitmap->storage.sb_page);
  430. sb->magic = cpu_to_le32(BITMAP_MAGIC);
  431. sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
  432. chunksize = bitmap->mddev->bitmap_info.chunksize;
  433. BUG_ON(!chunksize);
  434. if (!is_power_of_2(chunksize)) {
  435. kunmap_atomic(sb);
  436. printk(KERN_ERR "bitmap chunksize not a power of 2\n");
  437. return -EINVAL;
  438. }
  439. sb->chunksize = cpu_to_le32(chunksize);
  440. daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
  441. if (!daemon_sleep ||
  442. (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
  443. printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
  444. daemon_sleep = 5 * HZ;
  445. }
  446. sb->daemon_sleep = cpu_to_le32(daemon_sleep);
  447. bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
  448. /*
  449. * FIXME: write_behind for RAID1. If not specified, what
  450. * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
  451. */
  452. write_behind = bitmap->mddev->bitmap_info.max_write_behind;
  453. if (write_behind > COUNTER_MAX)
  454. write_behind = COUNTER_MAX / 2;
  455. sb->write_behind = cpu_to_le32(write_behind);
  456. bitmap->mddev->bitmap_info.max_write_behind = write_behind;
  457. /* keep the array size field of the bitmap superblock up to date */
  458. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  459. memcpy(sb->uuid, bitmap->mddev->uuid, 16);
  460. set_bit(BITMAP_STALE, &bitmap->flags);
  461. sb->state = cpu_to_le32(bitmap->flags);
  462. bitmap->events_cleared = bitmap->mddev->events;
  463. sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
  464. kunmap_atomic(sb);
  465. return 0;
  466. }
  467. /* read the superblock from the bitmap file and initialize some bitmap fields */
  468. static int bitmap_read_sb(struct bitmap *bitmap)
  469. {
  470. char *reason = NULL;
  471. bitmap_super_t *sb;
  472. unsigned long chunksize, daemon_sleep, write_behind;
  473. unsigned long long events;
  474. unsigned long sectors_reserved = 0;
  475. int err = -EINVAL;
  476. struct page *sb_page;
  477. if (!bitmap->storage.file && !bitmap->mddev->bitmap_info.offset) {
  478. chunksize = 128 * 1024 * 1024;
  479. daemon_sleep = 5 * HZ;
  480. write_behind = 0;
  481. set_bit(BITMAP_STALE, &bitmap->flags);
  482. err = 0;
  483. goto out_no_sb;
  484. }
  485. /* page 0 is the superblock, read it... */
  486. sb_page = alloc_page(GFP_KERNEL);
  487. if (!sb_page)
  488. return -ENOMEM;
  489. bitmap->storage.sb_page = sb_page;
  490. if (bitmap->storage.file) {
  491. loff_t isize = i_size_read(bitmap->storage.file->f_mapping->host);
  492. int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
  493. err = read_page(bitmap->storage.file, 0,
  494. bitmap, bytes, sb_page);
  495. } else {
  496. err = read_sb_page(bitmap->mddev,
  497. bitmap->mddev->bitmap_info.offset,
  498. sb_page,
  499. 0, sizeof(bitmap_super_t));
  500. }
  501. if (err)
  502. return err;
  503. sb = kmap_atomic(sb_page);
  504. chunksize = le32_to_cpu(sb->chunksize);
  505. daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
  506. write_behind = le32_to_cpu(sb->write_behind);
  507. sectors_reserved = le32_to_cpu(sb->sectors_reserved);
  508. /* verify that the bitmap-specific fields are valid */
  509. if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
  510. reason = "bad magic";
  511. else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
  512. le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
  513. reason = "unrecognized superblock version";
  514. else if (chunksize < 512)
  515. reason = "bitmap chunksize too small";
  516. else if (!is_power_of_2(chunksize))
  517. reason = "bitmap chunksize not a power of 2";
  518. else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
  519. reason = "daemon sleep period out of range";
  520. else if (write_behind > COUNTER_MAX)
  521. reason = "write-behind limit out of range (0 - 16383)";
  522. if (reason) {
  523. printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
  524. bmname(bitmap), reason);
  525. goto out;
  526. }
  527. /* keep the array size field of the bitmap superblock up to date */
  528. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  529. if (bitmap->mddev->persistent) {
  530. /*
  531. * We have a persistent array superblock, so compare the
  532. * bitmap's UUID and event counter to the mddev's
  533. */
  534. if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
  535. printk(KERN_INFO
  536. "%s: bitmap superblock UUID mismatch\n",
  537. bmname(bitmap));
  538. goto out;
  539. }
  540. events = le64_to_cpu(sb->events);
  541. if (events < bitmap->mddev->events) {
  542. printk(KERN_INFO
  543. "%s: bitmap file is out of date (%llu < %llu) "
  544. "-- forcing full recovery\n",
  545. bmname(bitmap), events,
  546. (unsigned long long) bitmap->mddev->events);
  547. set_bit(BITMAP_STALE, &bitmap->flags);
  548. }
  549. }
  550. /* assign fields using values from superblock */
  551. bitmap->flags |= le32_to_cpu(sb->state);
  552. if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
  553. set_bit(BITMAP_HOSTENDIAN, &bitmap->flags);
  554. bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
  555. err = 0;
  556. out:
  557. kunmap_atomic(sb);
  558. out_no_sb:
  559. if (test_bit(BITMAP_STALE, &bitmap->flags))
  560. bitmap->events_cleared = bitmap->mddev->events;
  561. bitmap->mddev->bitmap_info.chunksize = chunksize;
  562. bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
  563. bitmap->mddev->bitmap_info.max_write_behind = write_behind;
  564. if (bitmap->mddev->bitmap_info.space == 0 ||
  565. bitmap->mddev->bitmap_info.space > sectors_reserved)
  566. bitmap->mddev->bitmap_info.space = sectors_reserved;
  567. if (err)
  568. bitmap_print_sb(bitmap);
  569. return err;
  570. }
  571. /*
  572. * general bitmap file operations
  573. */
  574. /*
  575. * on-disk bitmap:
  576. *
  577. * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
  578. * file a page at a time. There's a superblock at the start of the file.
  579. */
  580. /* calculate the index of the page that contains this bit */
  581. static inline unsigned long file_page_index(struct bitmap_storage *store,
  582. unsigned long chunk)
  583. {
  584. if (store->sb_page)
  585. chunk += sizeof(bitmap_super_t) << 3;
  586. return chunk >> PAGE_BIT_SHIFT;
  587. }
  588. /* calculate the (bit) offset of this bit within a page */
  589. static inline unsigned long file_page_offset(struct bitmap_storage *store,
  590. unsigned long chunk)
  591. {
  592. if (store->sb_page)
  593. chunk += sizeof(bitmap_super_t) << 3;
  594. return chunk & (PAGE_BITS - 1);
  595. }
  596. /*
  597. * return a pointer to the page in the filemap that contains the given bit
  598. *
  599. * this lookup is complicated by the fact that the bitmap sb might be exactly
  600. * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
  601. * 0 or page 1
  602. */
  603. static inline struct page *filemap_get_page(struct bitmap_storage *store,
  604. unsigned long chunk)
  605. {
  606. if (file_page_index(store, chunk) >= store->file_pages)
  607. return NULL;
  608. return store->filemap[file_page_index(store, chunk)
  609. - file_page_index(store, 0)];
  610. }
  611. static int bitmap_storage_alloc(struct bitmap_storage *store,
  612. unsigned long chunks, int with_super)
  613. {
  614. int pnum;
  615. unsigned long num_pages;
  616. unsigned long bytes;
  617. bytes = DIV_ROUND_UP(chunks, 8);
  618. if (with_super)
  619. bytes += sizeof(bitmap_super_t);
  620. num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
  621. store->filemap = kmalloc(sizeof(struct page *)
  622. * num_pages, GFP_KERNEL);
  623. if (!store->filemap)
  624. return -ENOMEM;
  625. if (with_super && !store->sb_page) {
  626. store->sb_page = alloc_page(GFP_KERNEL|__GFP_ZERO);
  627. if (store->sb_page == NULL)
  628. return -ENOMEM;
  629. store->sb_page->index = 0;
  630. }
  631. pnum = 0;
  632. if (store->sb_page) {
  633. store->filemap[0] = store->sb_page;
  634. pnum = 1;
  635. }
  636. for ( ; pnum < num_pages; pnum++) {
  637. store->filemap[pnum] = alloc_page(GFP_KERNEL|__GFP_ZERO);
  638. if (!store->filemap[pnum]) {
  639. store->file_pages = pnum;
  640. return -ENOMEM;
  641. }
  642. store->filemap[pnum]->index = pnum;
  643. }
  644. store->file_pages = pnum;
  645. /* We need 4 bits per page, rounded up to a multiple
  646. * of sizeof(unsigned long) */
  647. store->filemap_attr = kzalloc(
  648. roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
  649. GFP_KERNEL);
  650. if (!store->filemap_attr)
  651. return -ENOMEM;
  652. store->bytes = bytes;
  653. return 0;
  654. }
  655. static void bitmap_file_unmap(struct bitmap_storage *store)
  656. {
  657. struct page **map, *sb_page;
  658. int pages;
  659. struct file *file;
  660. file = store->file;
  661. map = store->filemap;
  662. pages = store->file_pages;
  663. sb_page = store->sb_page;
  664. while (pages--)
  665. if (map[pages] != sb_page) /* 0 is sb_page, release it below */
  666. free_buffers(map[pages]);
  667. kfree(map);
  668. kfree(store->filemap_attr);
  669. if (sb_page)
  670. free_buffers(sb_page);
  671. if (file) {
  672. struct inode *inode = file->f_path.dentry->d_inode;
  673. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  674. fput(file);
  675. }
  676. }
  677. /*
  678. * bitmap_file_kick - if an error occurs while manipulating the bitmap file
  679. * then it is no longer reliable, so we stop using it and we mark the file
  680. * as failed in the superblock
  681. */
  682. static void bitmap_file_kick(struct bitmap *bitmap)
  683. {
  684. char *path, *ptr = NULL;
  685. if (!test_and_set_bit(BITMAP_STALE, &bitmap->flags)) {
  686. bitmap_update_sb(bitmap);
  687. if (bitmap->storage.file) {
  688. path = kmalloc(PAGE_SIZE, GFP_KERNEL);
  689. if (path)
  690. ptr = d_path(&bitmap->storage.file->f_path,
  691. path, PAGE_SIZE);
  692. printk(KERN_ALERT
  693. "%s: kicking failed bitmap file %s from array!\n",
  694. bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
  695. kfree(path);
  696. } else
  697. printk(KERN_ALERT
  698. "%s: disabling internal bitmap due to errors\n",
  699. bmname(bitmap));
  700. }
  701. }
  702. enum bitmap_page_attr {
  703. BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
  704. BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
  705. * i.e. counter is 1 or 2. */
  706. BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
  707. };
  708. static inline void set_page_attr(struct bitmap *bitmap, int pnum,
  709. enum bitmap_page_attr attr)
  710. {
  711. set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
  712. }
  713. static inline void clear_page_attr(struct bitmap *bitmap, int pnum,
  714. enum bitmap_page_attr attr)
  715. {
  716. clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
  717. }
  718. static inline int test_page_attr(struct bitmap *bitmap, int pnum,
  719. enum bitmap_page_attr attr)
  720. {
  721. return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
  722. }
  723. static inline int test_and_clear_page_attr(struct bitmap *bitmap, int pnum,
  724. enum bitmap_page_attr attr)
  725. {
  726. return test_and_clear_bit((pnum<<2) + attr,
  727. bitmap->storage.filemap_attr);
  728. }
  729. /*
  730. * bitmap_file_set_bit -- called before performing a write to the md device
  731. * to set (and eventually sync) a particular bit in the bitmap file
  732. *
  733. * we set the bit immediately, then we record the page number so that
  734. * when an unplug occurs, we can flush the dirty pages out to disk
  735. */
  736. static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
  737. {
  738. unsigned long bit;
  739. struct page *page;
  740. void *kaddr;
  741. unsigned long chunk = block >> bitmap->counts.chunkshift;
  742. page = filemap_get_page(&bitmap->storage, chunk);
  743. if (!page)
  744. return;
  745. bit = file_page_offset(&bitmap->storage, chunk);
  746. /* set the bit */
  747. kaddr = kmap_atomic(page);
  748. if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
  749. set_bit(bit, kaddr);
  750. else
  751. test_and_set_bit_le(bit, kaddr);
  752. kunmap_atomic(kaddr);
  753. pr_debug("set file bit %lu page %lu\n", bit, page->index);
  754. /* record page number so it gets flushed to disk when unplug occurs */
  755. set_page_attr(bitmap, page->index, BITMAP_PAGE_DIRTY);
  756. }
  757. static void bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block)
  758. {
  759. unsigned long bit;
  760. struct page *page;
  761. void *paddr;
  762. unsigned long chunk = block >> bitmap->counts.chunkshift;
  763. page = filemap_get_page(&bitmap->storage, chunk);
  764. if (!page)
  765. return;
  766. bit = file_page_offset(&bitmap->storage, chunk);
  767. paddr = kmap_atomic(page);
  768. if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
  769. clear_bit(bit, paddr);
  770. else
  771. test_and_clear_bit_le(bit, paddr);
  772. kunmap_atomic(paddr);
  773. if (!test_page_attr(bitmap, page->index, BITMAP_PAGE_NEEDWRITE)) {
  774. set_page_attr(bitmap, page->index, BITMAP_PAGE_PENDING);
  775. bitmap->allclean = 0;
  776. }
  777. }
  778. /* this gets called when the md device is ready to unplug its underlying
  779. * (slave) device queues -- before we let any writes go down, we need to
  780. * sync the dirty pages of the bitmap file to disk */
  781. void bitmap_unplug(struct bitmap *bitmap)
  782. {
  783. unsigned long i;
  784. int dirty, need_write;
  785. int wait = 0;
  786. if (!bitmap || !bitmap->storage.filemap ||
  787. test_bit(BITMAP_STALE, &bitmap->flags))
  788. return;
  789. /* look at each page to see if there are any set bits that need to be
  790. * flushed out to disk */
  791. for (i = 0; i < bitmap->storage.file_pages; i++) {
  792. if (!bitmap->storage.filemap)
  793. return;
  794. dirty = test_and_clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
  795. need_write = test_and_clear_page_attr(bitmap, i,
  796. BITMAP_PAGE_NEEDWRITE);
  797. if (dirty || need_write) {
  798. clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING);
  799. write_page(bitmap, bitmap->storage.filemap[i], 0);
  800. }
  801. if (dirty)
  802. wait = 1;
  803. }
  804. if (wait) { /* if any writes were performed, we need to wait on them */
  805. if (bitmap->storage.file)
  806. wait_event(bitmap->write_wait,
  807. atomic_read(&bitmap->pending_writes)==0);
  808. else
  809. md_super_wait(bitmap->mddev);
  810. }
  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. mddev->bitmap = NULL; /* disconnect from the md device */
  1433. mutex_unlock(&mddev->bitmap_info.mutex);
  1434. if (mddev->thread)
  1435. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1436. if (bitmap->sysfs_can_clear)
  1437. sysfs_put(bitmap->sysfs_can_clear);
  1438. bitmap_free(bitmap);
  1439. }
  1440. /*
  1441. * initialize the bitmap structure
  1442. * if this returns an error, bitmap_destroy must be called to do clean up
  1443. */
  1444. int bitmap_create(struct mddev *mddev)
  1445. {
  1446. struct bitmap *bitmap;
  1447. sector_t blocks = mddev->resync_max_sectors;
  1448. struct file *file = mddev->bitmap_info.file;
  1449. int err;
  1450. struct sysfs_dirent *bm = NULL;
  1451. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1452. BUG_ON(file && mddev->bitmap_info.offset);
  1453. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1454. if (!bitmap)
  1455. return -ENOMEM;
  1456. spin_lock_init(&bitmap->counts.lock);
  1457. atomic_set(&bitmap->pending_writes, 0);
  1458. init_waitqueue_head(&bitmap->write_wait);
  1459. init_waitqueue_head(&bitmap->overflow_wait);
  1460. init_waitqueue_head(&bitmap->behind_wait);
  1461. bitmap->mddev = mddev;
  1462. if (mddev->kobj.sd)
  1463. bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
  1464. if (bm) {
  1465. bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
  1466. sysfs_put(bm);
  1467. } else
  1468. bitmap->sysfs_can_clear = NULL;
  1469. bitmap->storage.file = file;
  1470. if (file) {
  1471. get_file(file);
  1472. /* As future accesses to this file will use bmap,
  1473. * and bypass the page cache, we must sync the file
  1474. * first.
  1475. */
  1476. vfs_fsync(file, 1);
  1477. }
  1478. /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
  1479. if (!mddev->bitmap_info.external) {
  1480. /*
  1481. * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
  1482. * instructing us to create a new on-disk bitmap instance.
  1483. */
  1484. if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
  1485. err = bitmap_new_disk_sb(bitmap);
  1486. else
  1487. err = bitmap_read_sb(bitmap);
  1488. } else {
  1489. err = 0;
  1490. if (mddev->bitmap_info.chunksize == 0 ||
  1491. mddev->bitmap_info.daemon_sleep == 0)
  1492. /* chunksize and time_base need to be
  1493. * set first. */
  1494. err = -EINVAL;
  1495. }
  1496. if (err)
  1497. goto error;
  1498. bitmap->daemon_lastrun = jiffies;
  1499. err = bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1);
  1500. if (err)
  1501. goto error;
  1502. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1503. bitmap->counts.pages, bmname(bitmap));
  1504. mddev->bitmap = bitmap;
  1505. return test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0;
  1506. error:
  1507. bitmap_free(bitmap);
  1508. return err;
  1509. }
  1510. int bitmap_load(struct mddev *mddev)
  1511. {
  1512. int err = 0;
  1513. sector_t start = 0;
  1514. sector_t sector = 0;
  1515. struct bitmap *bitmap = mddev->bitmap;
  1516. if (!bitmap)
  1517. goto out;
  1518. /* Clear out old bitmap info first: Either there is none, or we
  1519. * are resuming after someone else has possibly changed things,
  1520. * so we should forget old cached info.
  1521. * All chunks should be clean, but some might need_sync.
  1522. */
  1523. while (sector < mddev->resync_max_sectors) {
  1524. sector_t blocks;
  1525. bitmap_start_sync(bitmap, sector, &blocks, 0);
  1526. sector += blocks;
  1527. }
  1528. bitmap_close_sync(bitmap);
  1529. if (mddev->degraded == 0
  1530. || bitmap->events_cleared == mddev->events)
  1531. /* no need to keep dirty bits to optimise a
  1532. * re-add of a missing device */
  1533. start = mddev->recovery_cp;
  1534. mutex_lock(&mddev->bitmap_info.mutex);
  1535. err = bitmap_init_from_disk(bitmap, start);
  1536. mutex_unlock(&mddev->bitmap_info.mutex);
  1537. if (err)
  1538. goto out;
  1539. clear_bit(BITMAP_STALE, &bitmap->flags);
  1540. /* Kick recovery in case any bits were set */
  1541. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  1542. mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
  1543. md_wakeup_thread(mddev->thread);
  1544. bitmap_update_sb(bitmap);
  1545. if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
  1546. err = -EIO;
  1547. out:
  1548. return err;
  1549. }
  1550. EXPORT_SYMBOL_GPL(bitmap_load);
  1551. void bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
  1552. {
  1553. unsigned long chunk_kb;
  1554. struct bitmap_counts *counts;
  1555. if (!bitmap)
  1556. return;
  1557. counts = &bitmap->counts;
  1558. chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
  1559. seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
  1560. "%lu%s chunk",
  1561. counts->pages - counts->missing_pages,
  1562. counts->pages,
  1563. (counts->pages - counts->missing_pages)
  1564. << (PAGE_SHIFT - 10),
  1565. chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
  1566. chunk_kb ? "KB" : "B");
  1567. if (bitmap->storage.file) {
  1568. seq_printf(seq, ", file: ");
  1569. seq_path(seq, &bitmap->storage.file->f_path, " \t\n");
  1570. }
  1571. seq_printf(seq, "\n");
  1572. }
  1573. int bitmap_resize(struct bitmap *bitmap, sector_t blocks,
  1574. int chunksize, int init)
  1575. {
  1576. /* If chunk_size is 0, choose an appropriate chunk size.
  1577. * Then possibly allocate new storage space.
  1578. * Then quiesce, copy bits, replace bitmap, and re-start
  1579. *
  1580. * This function is called both to set up the initial bitmap
  1581. * and to resize the bitmap while the array is active.
  1582. * If this happens as a result of the array being resized,
  1583. * chunksize will be zero, and we need to choose a suitable
  1584. * chunksize, otherwise we use what we are given.
  1585. */
  1586. struct bitmap_storage store;
  1587. struct bitmap_counts old_counts;
  1588. unsigned long chunks;
  1589. sector_t block;
  1590. sector_t old_blocks, new_blocks;
  1591. int chunkshift;
  1592. int ret = 0;
  1593. long pages;
  1594. struct bitmap_page *new_bp;
  1595. if (chunksize == 0) {
  1596. /* If there is enough space, leave the chunk size unchanged,
  1597. * else increase by factor of two until there is enough space.
  1598. */
  1599. long bytes;
  1600. long space = bitmap->mddev->bitmap_info.space;
  1601. if (space == 0) {
  1602. /* We don't know how much space there is, so limit
  1603. * to current size - in sectors.
  1604. */
  1605. bytes = DIV_ROUND_UP(bitmap->counts.chunks, 8);
  1606. if (!bitmap->mddev->bitmap_info.external)
  1607. bytes += sizeof(bitmap_super_t);
  1608. space = DIV_ROUND_UP(bytes, 512);
  1609. bitmap->mddev->bitmap_info.space = space;
  1610. }
  1611. chunkshift = bitmap->counts.chunkshift;
  1612. chunkshift--;
  1613. do {
  1614. /* 'chunkshift' is shift from block size to chunk size */
  1615. chunkshift++;
  1616. chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
  1617. bytes = DIV_ROUND_UP(chunks, 8);
  1618. if (!bitmap->mddev->bitmap_info.external)
  1619. bytes += sizeof(bitmap_super_t);
  1620. } while (bytes > (space << 9));
  1621. } else
  1622. chunkshift = ffz(~chunksize) - BITMAP_BLOCK_SHIFT;
  1623. chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
  1624. memset(&store, 0, sizeof(store));
  1625. if (bitmap->mddev->bitmap_info.offset || bitmap->mddev->bitmap_info.file)
  1626. ret = bitmap_storage_alloc(&store, chunks,
  1627. !bitmap->mddev->bitmap_info.external);
  1628. if (ret)
  1629. goto err;
  1630. pages = DIV_ROUND_UP(chunks, PAGE_COUNTER_RATIO);
  1631. new_bp = kzalloc(pages * sizeof(*new_bp), GFP_KERNEL);
  1632. ret = -ENOMEM;
  1633. if (!new_bp) {
  1634. bitmap_file_unmap(&store);
  1635. goto err;
  1636. }
  1637. if (!init)
  1638. bitmap->mddev->pers->quiesce(bitmap->mddev, 1);
  1639. store.file = bitmap->storage.file;
  1640. bitmap->storage.file = NULL;
  1641. if (store.sb_page && bitmap->storage.sb_page)
  1642. memcpy(page_address(store.sb_page),
  1643. page_address(bitmap->storage.sb_page),
  1644. sizeof(bitmap_super_t));
  1645. bitmap_file_unmap(&bitmap->storage);
  1646. bitmap->storage = store;
  1647. old_counts = bitmap->counts;
  1648. bitmap->counts.bp = new_bp;
  1649. bitmap->counts.pages = pages;
  1650. bitmap->counts.missing_pages = pages;
  1651. bitmap->counts.chunkshift = chunkshift;
  1652. bitmap->counts.chunks = chunks;
  1653. bitmap->mddev->bitmap_info.chunksize = 1 << (chunkshift +
  1654. BITMAP_BLOCK_SHIFT);
  1655. blocks = min(old_counts.chunks << old_counts.chunkshift,
  1656. chunks << chunkshift);
  1657. spin_lock_irq(&bitmap->counts.lock);
  1658. for (block = 0; block < blocks; ) {
  1659. bitmap_counter_t *bmc_old, *bmc_new;
  1660. int set;
  1661. bmc_old = bitmap_get_counter(&old_counts, block,
  1662. &old_blocks, 0);
  1663. set = bmc_old && NEEDED(*bmc_old);
  1664. if (set) {
  1665. bmc_new = bitmap_get_counter(&bitmap->counts, block,
  1666. &new_blocks, 1);
  1667. if (*bmc_new == 0) {
  1668. /* need to set on-disk bits too. */
  1669. sector_t end = block + new_blocks;
  1670. sector_t start = block >> chunkshift;
  1671. start <<= chunkshift;
  1672. while (start < end) {
  1673. bitmap_file_set_bit(bitmap, block);
  1674. start += 1 << chunkshift;
  1675. }
  1676. *bmc_new = 2;
  1677. bitmap_count_page(&bitmap->counts,
  1678. block, 1);
  1679. bitmap_set_pending(&bitmap->counts,
  1680. block);
  1681. }
  1682. *bmc_new |= NEEDED_MASK;
  1683. if (new_blocks < old_blocks)
  1684. old_blocks = new_blocks;
  1685. }
  1686. block += old_blocks;
  1687. }
  1688. if (!init) {
  1689. int i;
  1690. while (block < (chunks << chunkshift)) {
  1691. bitmap_counter_t *bmc;
  1692. bmc = bitmap_get_counter(&bitmap->counts, block,
  1693. &new_blocks, 1);
  1694. if (bmc) {
  1695. /* new space. It needs to be resynced, so
  1696. * we set NEEDED_MASK.
  1697. */
  1698. if (*bmc == 0) {
  1699. *bmc = NEEDED_MASK | 2;
  1700. bitmap_count_page(&bitmap->counts,
  1701. block, 1);
  1702. bitmap_set_pending(&bitmap->counts,
  1703. block);
  1704. }
  1705. }
  1706. block += new_blocks;
  1707. }
  1708. for (i = 0; i < bitmap->storage.file_pages; i++)
  1709. set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
  1710. }
  1711. spin_unlock_irq(&bitmap->counts.lock);
  1712. if (!init) {
  1713. bitmap_unplug(bitmap);
  1714. bitmap->mddev->pers->quiesce(bitmap->mddev, 0);
  1715. }
  1716. ret = 0;
  1717. err:
  1718. return ret;
  1719. }
  1720. EXPORT_SYMBOL_GPL(bitmap_resize);
  1721. static ssize_t
  1722. location_show(struct mddev *mddev, char *page)
  1723. {
  1724. ssize_t len;
  1725. if (mddev->bitmap_info.file)
  1726. len = sprintf(page, "file");
  1727. else if (mddev->bitmap_info.offset)
  1728. len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
  1729. else
  1730. len = sprintf(page, "none");
  1731. len += sprintf(page+len, "\n");
  1732. return len;
  1733. }
  1734. static ssize_t
  1735. location_store(struct mddev *mddev, const char *buf, size_t len)
  1736. {
  1737. if (mddev->pers) {
  1738. if (!mddev->pers->quiesce)
  1739. return -EBUSY;
  1740. if (mddev->recovery || mddev->sync_thread)
  1741. return -EBUSY;
  1742. }
  1743. if (mddev->bitmap || mddev->bitmap_info.file ||
  1744. mddev->bitmap_info.offset) {
  1745. /* bitmap already configured. Only option is to clear it */
  1746. if (strncmp(buf, "none", 4) != 0)
  1747. return -EBUSY;
  1748. if (mddev->pers) {
  1749. mddev->pers->quiesce(mddev, 1);
  1750. bitmap_destroy(mddev);
  1751. mddev->pers->quiesce(mddev, 0);
  1752. }
  1753. mddev->bitmap_info.offset = 0;
  1754. if (mddev->bitmap_info.file) {
  1755. struct file *f = mddev->bitmap_info.file;
  1756. mddev->bitmap_info.file = NULL;
  1757. restore_bitmap_write_access(f);
  1758. fput(f);
  1759. }
  1760. } else {
  1761. /* No bitmap, OK to set a location */
  1762. long long offset;
  1763. if (strncmp(buf, "none", 4) == 0)
  1764. /* nothing to be done */;
  1765. else if (strncmp(buf, "file:", 5) == 0) {
  1766. /* Not supported yet */
  1767. return -EINVAL;
  1768. } else {
  1769. int rv;
  1770. if (buf[0] == '+')
  1771. rv = strict_strtoll(buf+1, 10, &offset);
  1772. else
  1773. rv = strict_strtoll(buf, 10, &offset);
  1774. if (rv)
  1775. return rv;
  1776. if (offset == 0)
  1777. return -EINVAL;
  1778. if (mddev->bitmap_info.external == 0 &&
  1779. mddev->major_version == 0 &&
  1780. offset != mddev->bitmap_info.default_offset)
  1781. return -EINVAL;
  1782. mddev->bitmap_info.offset = offset;
  1783. if (mddev->pers) {
  1784. mddev->pers->quiesce(mddev, 1);
  1785. rv = bitmap_create(mddev);
  1786. if (!rv)
  1787. rv = bitmap_load(mddev);
  1788. if (rv) {
  1789. bitmap_destroy(mddev);
  1790. mddev->bitmap_info.offset = 0;
  1791. }
  1792. mddev->pers->quiesce(mddev, 0);
  1793. if (rv)
  1794. return rv;
  1795. }
  1796. }
  1797. }
  1798. if (!mddev->external) {
  1799. /* Ensure new bitmap info is stored in
  1800. * metadata promptly.
  1801. */
  1802. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1803. md_wakeup_thread(mddev->thread);
  1804. }
  1805. return len;
  1806. }
  1807. static struct md_sysfs_entry bitmap_location =
  1808. __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
  1809. /* 'bitmap/space' is the space available at 'location' for the
  1810. * bitmap. This allows the kernel to know when it is safe to
  1811. * resize the bitmap to match a resized array.
  1812. */
  1813. static ssize_t
  1814. space_show(struct mddev *mddev, char *page)
  1815. {
  1816. return sprintf(page, "%lu\n", mddev->bitmap_info.space);
  1817. }
  1818. static ssize_t
  1819. space_store(struct mddev *mddev, const char *buf, size_t len)
  1820. {
  1821. unsigned long sectors;
  1822. int rv;
  1823. rv = kstrtoul(buf, 10, &sectors);
  1824. if (rv)
  1825. return rv;
  1826. if (sectors == 0)
  1827. return -EINVAL;
  1828. if (mddev->bitmap &&
  1829. sectors < (mddev->bitmap->storage.bytes + 511) >> 9)
  1830. return -EFBIG; /* Bitmap is too big for this small space */
  1831. /* could make sure it isn't too big, but that isn't really
  1832. * needed - user-space should be careful.
  1833. */
  1834. mddev->bitmap_info.space = sectors;
  1835. return len;
  1836. }
  1837. static struct md_sysfs_entry bitmap_space =
  1838. __ATTR(space, S_IRUGO|S_IWUSR, space_show, space_store);
  1839. static ssize_t
  1840. timeout_show(struct mddev *mddev, char *page)
  1841. {
  1842. ssize_t len;
  1843. unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
  1844. unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
  1845. len = sprintf(page, "%lu", secs);
  1846. if (jifs)
  1847. len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
  1848. len += sprintf(page+len, "\n");
  1849. return len;
  1850. }
  1851. static ssize_t
  1852. timeout_store(struct mddev *mddev, const char *buf, size_t len)
  1853. {
  1854. /* timeout can be set at any time */
  1855. unsigned long timeout;
  1856. int rv = strict_strtoul_scaled(buf, &timeout, 4);
  1857. if (rv)
  1858. return rv;
  1859. /* just to make sure we don't overflow... */
  1860. if (timeout >= LONG_MAX / HZ)
  1861. return -EINVAL;
  1862. timeout = timeout * HZ / 10000;
  1863. if (timeout >= MAX_SCHEDULE_TIMEOUT)
  1864. timeout = MAX_SCHEDULE_TIMEOUT-1;
  1865. if (timeout < 1)
  1866. timeout = 1;
  1867. mddev->bitmap_info.daemon_sleep = timeout;
  1868. if (mddev->thread) {
  1869. /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
  1870. * the bitmap is all clean and we don't need to
  1871. * adjust the timeout right now
  1872. */
  1873. if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
  1874. mddev->thread->timeout = timeout;
  1875. md_wakeup_thread(mddev->thread);
  1876. }
  1877. }
  1878. return len;
  1879. }
  1880. static struct md_sysfs_entry bitmap_timeout =
  1881. __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
  1882. static ssize_t
  1883. backlog_show(struct mddev *mddev, char *page)
  1884. {
  1885. return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
  1886. }
  1887. static ssize_t
  1888. backlog_store(struct mddev *mddev, const char *buf, size_t len)
  1889. {
  1890. unsigned long backlog;
  1891. int rv = strict_strtoul(buf, 10, &backlog);
  1892. if (rv)
  1893. return rv;
  1894. if (backlog > COUNTER_MAX)
  1895. return -EINVAL;
  1896. mddev->bitmap_info.max_write_behind = backlog;
  1897. return len;
  1898. }
  1899. static struct md_sysfs_entry bitmap_backlog =
  1900. __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
  1901. static ssize_t
  1902. chunksize_show(struct mddev *mddev, char *page)
  1903. {
  1904. return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
  1905. }
  1906. static ssize_t
  1907. chunksize_store(struct mddev *mddev, const char *buf, size_t len)
  1908. {
  1909. /* Can only be changed when no bitmap is active */
  1910. int rv;
  1911. unsigned long csize;
  1912. if (mddev->bitmap)
  1913. return -EBUSY;
  1914. rv = strict_strtoul(buf, 10, &csize);
  1915. if (rv)
  1916. return rv;
  1917. if (csize < 512 ||
  1918. !is_power_of_2(csize))
  1919. return -EINVAL;
  1920. mddev->bitmap_info.chunksize = csize;
  1921. return len;
  1922. }
  1923. static struct md_sysfs_entry bitmap_chunksize =
  1924. __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
  1925. static ssize_t metadata_show(struct mddev *mddev, char *page)
  1926. {
  1927. return sprintf(page, "%s\n", (mddev->bitmap_info.external
  1928. ? "external" : "internal"));
  1929. }
  1930. static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
  1931. {
  1932. if (mddev->bitmap ||
  1933. mddev->bitmap_info.file ||
  1934. mddev->bitmap_info.offset)
  1935. return -EBUSY;
  1936. if (strncmp(buf, "external", 8) == 0)
  1937. mddev->bitmap_info.external = 1;
  1938. else if (strncmp(buf, "internal", 8) == 0)
  1939. mddev->bitmap_info.external = 0;
  1940. else
  1941. return -EINVAL;
  1942. return len;
  1943. }
  1944. static struct md_sysfs_entry bitmap_metadata =
  1945. __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
  1946. static ssize_t can_clear_show(struct mddev *mddev, char *page)
  1947. {
  1948. int len;
  1949. if (mddev->bitmap)
  1950. len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
  1951. "false" : "true"));
  1952. else
  1953. len = sprintf(page, "\n");
  1954. return len;
  1955. }
  1956. static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
  1957. {
  1958. if (mddev->bitmap == NULL)
  1959. return -ENOENT;
  1960. if (strncmp(buf, "false", 5) == 0)
  1961. mddev->bitmap->need_sync = 1;
  1962. else if (strncmp(buf, "true", 4) == 0) {
  1963. if (mddev->degraded)
  1964. return -EBUSY;
  1965. mddev->bitmap->need_sync = 0;
  1966. } else
  1967. return -EINVAL;
  1968. return len;
  1969. }
  1970. static struct md_sysfs_entry bitmap_can_clear =
  1971. __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
  1972. static ssize_t
  1973. behind_writes_used_show(struct mddev *mddev, char *page)
  1974. {
  1975. if (mddev->bitmap == NULL)
  1976. return sprintf(page, "0\n");
  1977. return sprintf(page, "%lu\n",
  1978. mddev->bitmap->behind_writes_used);
  1979. }
  1980. static ssize_t
  1981. behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
  1982. {
  1983. if (mddev->bitmap)
  1984. mddev->bitmap->behind_writes_used = 0;
  1985. return len;
  1986. }
  1987. static struct md_sysfs_entry max_backlog_used =
  1988. __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
  1989. behind_writes_used_show, behind_writes_used_reset);
  1990. static struct attribute *md_bitmap_attrs[] = {
  1991. &bitmap_location.attr,
  1992. &bitmap_space.attr,
  1993. &bitmap_timeout.attr,
  1994. &bitmap_backlog.attr,
  1995. &bitmap_chunksize.attr,
  1996. &bitmap_metadata.attr,
  1997. &bitmap_can_clear.attr,
  1998. &max_backlog_used.attr,
  1999. NULL
  2000. };
  2001. struct attribute_group md_bitmap_group = {
  2002. .name = "bitmap",
  2003. .attrs = md_bitmap_attrs,
  2004. };