bitmap.c 60 KB

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