bitmap.c 56 KB

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