bitmap.c 55 KB

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