bitmap.c 55 KB

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