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