bitmap.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  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. * wait if count gets too high, wake when it drops to half.
  16. */
  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/raid/md.h>
  28. #include <linux/raid/bitmap.h>
  29. /* debug macros */
  30. #define DEBUG 0
  31. #if DEBUG
  32. /* these are for debugging purposes only! */
  33. /* define one and only one of these */
  34. #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
  35. #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
  36. #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
  37. #define INJECT_FAULTS_4 0 /* undef */
  38. #define INJECT_FAULTS_5 0 /* undef */
  39. #define INJECT_FAULTS_6 0
  40. /* if these are defined, the driver will fail! debug only */
  41. #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
  42. #define INJECT_FATAL_FAULT_2 0 /* undef */
  43. #define INJECT_FATAL_FAULT_3 0 /* undef */
  44. #endif
  45. //#define DPRINTK PRINTK /* set this NULL to avoid verbose debug output */
  46. #define DPRINTK(x...) do { } while(0)
  47. #ifndef PRINTK
  48. # if DEBUG > 0
  49. # define PRINTK(x...) printk(KERN_DEBUG x)
  50. # else
  51. # define PRINTK(x...)
  52. # endif
  53. #endif
  54. static inline char * bmname(struct bitmap *bitmap)
  55. {
  56. return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
  57. }
  58. /*
  59. * just a placeholder - calls kmalloc for bitmap pages
  60. */
  61. static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
  62. {
  63. unsigned char *page;
  64. #ifdef INJECT_FAULTS_1
  65. page = NULL;
  66. #else
  67. page = kmalloc(PAGE_SIZE, GFP_NOIO);
  68. #endif
  69. if (!page)
  70. printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
  71. else
  72. PRINTK("%s: bitmap_alloc_page: allocated page at %p\n",
  73. bmname(bitmap), page);
  74. return page;
  75. }
  76. /*
  77. * for now just a placeholder -- just calls kfree for bitmap pages
  78. */
  79. static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
  80. {
  81. PRINTK("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
  82. kfree(page);
  83. }
  84. /*
  85. * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
  86. *
  87. * 1) check to see if this page is allocated, if it's not then try to alloc
  88. * 2) if the alloc fails, set the page's hijacked flag so we'll use the
  89. * page pointer directly as a counter
  90. *
  91. * if we find our page, we increment the page's refcount so that it stays
  92. * allocated while we're using it
  93. */
  94. static int bitmap_checkpage(struct bitmap *bitmap, unsigned long page, int create)
  95. {
  96. unsigned char *mappage;
  97. if (page >= bitmap->pages) {
  98. printk(KERN_ALERT
  99. "%s: invalid bitmap page request: %lu (> %lu)\n",
  100. bmname(bitmap), page, bitmap->pages-1);
  101. return -EINVAL;
  102. }
  103. if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
  104. return 0;
  105. if (bitmap->bp[page].map) /* page is already allocated, just return */
  106. return 0;
  107. if (!create)
  108. return -ENOENT;
  109. spin_unlock_irq(&bitmap->lock);
  110. /* this page has not been allocated yet */
  111. if ((mappage = bitmap_alloc_page(bitmap)) == NULL) {
  112. PRINTK("%s: bitmap map page allocation failed, hijacking\n",
  113. bmname(bitmap));
  114. /* failed - set the hijacked flag so that we can use the
  115. * pointer as a counter */
  116. spin_lock_irq(&bitmap->lock);
  117. if (!bitmap->bp[page].map)
  118. bitmap->bp[page].hijacked = 1;
  119. goto out;
  120. }
  121. /* got a page */
  122. spin_lock_irq(&bitmap->lock);
  123. /* recheck the page */
  124. if (bitmap->bp[page].map || bitmap->bp[page].hijacked) {
  125. /* somebody beat us to getting the page */
  126. bitmap_free_page(bitmap, mappage);
  127. return 0;
  128. }
  129. /* no page was in place and we have one, so install it */
  130. memset(mappage, 0, PAGE_SIZE);
  131. bitmap->bp[page].map = mappage;
  132. bitmap->missing_pages--;
  133. out:
  134. return 0;
  135. }
  136. /* if page is completely empty, put it back on the free list, or dealloc it */
  137. /* if page was hijacked, unmark the flag so it might get alloced next time */
  138. /* Note: lock should be held when calling this */
  139. static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
  140. {
  141. char *ptr;
  142. if (bitmap->bp[page].count) /* page is still busy */
  143. return;
  144. /* page is no longer in use, it can be released */
  145. if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
  146. bitmap->bp[page].hijacked = 0;
  147. bitmap->bp[page].map = NULL;
  148. return;
  149. }
  150. /* normal case, free the page */
  151. #if 0
  152. /* actually ... let's not. We will probably need the page again exactly when
  153. * memory is tight and we are flusing to disk
  154. */
  155. return;
  156. #else
  157. ptr = bitmap->bp[page].map;
  158. bitmap->bp[page].map = NULL;
  159. bitmap->missing_pages++;
  160. bitmap_free_page(bitmap, ptr);
  161. return;
  162. #endif
  163. }
  164. /*
  165. * bitmap file handling - read and write the bitmap file and its superblock
  166. */
  167. /*
  168. * basic page I/O operations
  169. */
  170. /* IO operations when bitmap is stored near all superblocks */
  171. static struct page *read_sb_page(mddev_t *mddev, long offset,
  172. struct page *page,
  173. unsigned long index, int size)
  174. {
  175. /* choose a good rdev and read the page from there */
  176. mdk_rdev_t *rdev;
  177. sector_t target;
  178. if (!page)
  179. page = alloc_page(GFP_KERNEL);
  180. if (!page)
  181. return ERR_PTR(-ENOMEM);
  182. list_for_each_entry(rdev, &mddev->disks, same_set) {
  183. if (! test_bit(In_sync, &rdev->flags)
  184. || test_bit(Faulty, &rdev->flags))
  185. continue;
  186. target = rdev->sb_start + offset + index * (PAGE_SIZE/512);
  187. if (sync_page_io(rdev->bdev, target,
  188. roundup(size, bdev_hardsect_size(rdev->bdev)),
  189. page, READ)) {
  190. page->index = index;
  191. attach_page_buffers(page, NULL); /* so that free_buffer will
  192. * quietly no-op */
  193. return page;
  194. }
  195. }
  196. return ERR_PTR(-EIO);
  197. }
  198. static mdk_rdev_t *next_active_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
  199. {
  200. /* Iterate the disks of an mddev, using rcu to protect access to the
  201. * linked list, and raising the refcount of devices we return to ensure
  202. * they don't disappear while in use.
  203. * As devices are only added or removed when raid_disk is < 0 and
  204. * nr_pending is 0 and In_sync is clear, the entries we return will
  205. * still be in the same position on the list when we re-enter
  206. * list_for_each_continue_rcu.
  207. */
  208. struct list_head *pos;
  209. rcu_read_lock();
  210. if (rdev == NULL)
  211. /* start at the beginning */
  212. pos = &mddev->disks;
  213. else {
  214. /* release the previous rdev and start from there. */
  215. rdev_dec_pending(rdev, mddev);
  216. pos = &rdev->same_set;
  217. }
  218. list_for_each_continue_rcu(pos, &mddev->disks) {
  219. rdev = list_entry(pos, mdk_rdev_t, same_set);
  220. if (rdev->raid_disk >= 0 &&
  221. test_bit(In_sync, &rdev->flags) &&
  222. !test_bit(Faulty, &rdev->flags)) {
  223. /* this is a usable devices */
  224. atomic_inc(&rdev->nr_pending);
  225. rcu_read_unlock();
  226. return rdev;
  227. }
  228. }
  229. rcu_read_unlock();
  230. return NULL;
  231. }
  232. static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
  233. {
  234. mdk_rdev_t *rdev = NULL;
  235. mddev_t *mddev = bitmap->mddev;
  236. while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
  237. int size = PAGE_SIZE;
  238. if (page->index == bitmap->file_pages-1)
  239. size = roundup(bitmap->last_page_size,
  240. bdev_hardsect_size(rdev->bdev));
  241. /* Just make sure we aren't corrupting data or
  242. * metadata
  243. */
  244. if (bitmap->offset < 0) {
  245. /* DATA BITMAP METADATA */
  246. if (bitmap->offset
  247. + (long)(page->index * (PAGE_SIZE/512))
  248. + size/512 > 0)
  249. /* bitmap runs in to metadata */
  250. goto bad_alignment;
  251. if (rdev->data_offset + mddev->size*2
  252. > rdev->sb_start + bitmap->offset)
  253. /* data runs in to bitmap */
  254. goto bad_alignment;
  255. } else if (rdev->sb_start < rdev->data_offset) {
  256. /* METADATA BITMAP DATA */
  257. if (rdev->sb_start
  258. + bitmap->offset
  259. + page->index*(PAGE_SIZE/512) + size/512
  260. > rdev->data_offset)
  261. /* bitmap runs in to data */
  262. goto bad_alignment;
  263. } else {
  264. /* DATA METADATA BITMAP - no problems */
  265. }
  266. md_super_write(mddev, rdev,
  267. rdev->sb_start + bitmap->offset
  268. + page->index * (PAGE_SIZE/512),
  269. size,
  270. page);
  271. }
  272. if (wait)
  273. md_super_wait(mddev);
  274. return 0;
  275. bad_alignment:
  276. rcu_read_unlock();
  277. return -EINVAL;
  278. }
  279. static void bitmap_file_kick(struct bitmap *bitmap);
  280. /*
  281. * write out a page to a file
  282. */
  283. static void write_page(struct bitmap *bitmap, struct page *page, int wait)
  284. {
  285. struct buffer_head *bh;
  286. if (bitmap->file == NULL) {
  287. switch (write_sb_page(bitmap, page, wait)) {
  288. case -EINVAL:
  289. bitmap->flags |= BITMAP_WRITE_ERROR;
  290. }
  291. } else {
  292. bh = page_buffers(page);
  293. while (bh && bh->b_blocknr) {
  294. atomic_inc(&bitmap->pending_writes);
  295. set_buffer_locked(bh);
  296. set_buffer_mapped(bh);
  297. submit_bh(WRITE, bh);
  298. bh = bh->b_this_page;
  299. }
  300. if (wait) {
  301. wait_event(bitmap->write_wait,
  302. atomic_read(&bitmap->pending_writes)==0);
  303. }
  304. }
  305. if (bitmap->flags & BITMAP_WRITE_ERROR)
  306. bitmap_file_kick(bitmap);
  307. }
  308. static void end_bitmap_write(struct buffer_head *bh, int uptodate)
  309. {
  310. struct bitmap *bitmap = bh->b_private;
  311. unsigned long flags;
  312. if (!uptodate) {
  313. spin_lock_irqsave(&bitmap->lock, flags);
  314. bitmap->flags |= BITMAP_WRITE_ERROR;
  315. spin_unlock_irqrestore(&bitmap->lock, flags);
  316. }
  317. if (atomic_dec_and_test(&bitmap->pending_writes))
  318. wake_up(&bitmap->write_wait);
  319. }
  320. /* copied from buffer.c */
  321. static void
  322. __clear_page_buffers(struct page *page)
  323. {
  324. ClearPagePrivate(page);
  325. set_page_private(page, 0);
  326. page_cache_release(page);
  327. }
  328. static void free_buffers(struct page *page)
  329. {
  330. struct buffer_head *bh = page_buffers(page);
  331. while (bh) {
  332. struct buffer_head *next = bh->b_this_page;
  333. free_buffer_head(bh);
  334. bh = next;
  335. }
  336. __clear_page_buffers(page);
  337. put_page(page);
  338. }
  339. /* read a page from a file.
  340. * We both read the page, and attach buffers to the page to record the
  341. * address of each block (using bmap). These addresses will be used
  342. * to write the block later, completely bypassing the filesystem.
  343. * This usage is similar to how swap files are handled, and allows us
  344. * to write to a file with no concerns of memory allocation failing.
  345. */
  346. static struct page *read_page(struct file *file, unsigned long index,
  347. struct bitmap *bitmap,
  348. unsigned long count)
  349. {
  350. struct page *page = NULL;
  351. struct inode *inode = file->f_path.dentry->d_inode;
  352. struct buffer_head *bh;
  353. sector_t block;
  354. PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_SIZE,
  355. (unsigned long long)index << PAGE_SHIFT);
  356. page = alloc_page(GFP_KERNEL);
  357. if (!page)
  358. page = ERR_PTR(-ENOMEM);
  359. if (IS_ERR(page))
  360. goto out;
  361. bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
  362. if (!bh) {
  363. put_page(page);
  364. page = ERR_PTR(-ENOMEM);
  365. goto out;
  366. }
  367. attach_page_buffers(page, bh);
  368. block = index << (PAGE_SHIFT - inode->i_blkbits);
  369. while (bh) {
  370. if (count == 0)
  371. bh->b_blocknr = 0;
  372. else {
  373. bh->b_blocknr = bmap(inode, block);
  374. if (bh->b_blocknr == 0) {
  375. /* Cannot use this file! */
  376. free_buffers(page);
  377. page = ERR_PTR(-EINVAL);
  378. goto out;
  379. }
  380. bh->b_bdev = inode->i_sb->s_bdev;
  381. if (count < (1<<inode->i_blkbits))
  382. count = 0;
  383. else
  384. count -= (1<<inode->i_blkbits);
  385. bh->b_end_io = end_bitmap_write;
  386. bh->b_private = bitmap;
  387. atomic_inc(&bitmap->pending_writes);
  388. set_buffer_locked(bh);
  389. set_buffer_mapped(bh);
  390. submit_bh(READ, bh);
  391. }
  392. block++;
  393. bh = bh->b_this_page;
  394. }
  395. page->index = index;
  396. wait_event(bitmap->write_wait,
  397. atomic_read(&bitmap->pending_writes)==0);
  398. if (bitmap->flags & BITMAP_WRITE_ERROR) {
  399. free_buffers(page);
  400. page = ERR_PTR(-EIO);
  401. }
  402. out:
  403. if (IS_ERR(page))
  404. printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
  405. (int)PAGE_SIZE,
  406. (unsigned long long)index << PAGE_SHIFT,
  407. PTR_ERR(page));
  408. return page;
  409. }
  410. /*
  411. * bitmap file superblock operations
  412. */
  413. /* update the event counter and sync the superblock to disk */
  414. void bitmap_update_sb(struct bitmap *bitmap)
  415. {
  416. bitmap_super_t *sb;
  417. unsigned long flags;
  418. if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
  419. return;
  420. spin_lock_irqsave(&bitmap->lock, flags);
  421. if (!bitmap->sb_page) { /* no superblock */
  422. spin_unlock_irqrestore(&bitmap->lock, flags);
  423. return;
  424. }
  425. spin_unlock_irqrestore(&bitmap->lock, flags);
  426. sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
  427. sb->events = cpu_to_le64(bitmap->mddev->events);
  428. if (bitmap->mddev->events < bitmap->events_cleared) {
  429. /* rocking back to read-only */
  430. bitmap->events_cleared = bitmap->mddev->events;
  431. sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
  432. }
  433. kunmap_atomic(sb, KM_USER0);
  434. write_page(bitmap, bitmap->sb_page, 1);
  435. }
  436. /* print out the bitmap file superblock */
  437. void bitmap_print_sb(struct bitmap *bitmap)
  438. {
  439. bitmap_super_t *sb;
  440. if (!bitmap || !bitmap->sb_page)
  441. return;
  442. sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
  443. printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
  444. printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
  445. printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
  446. printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
  447. *(__u32 *)(sb->uuid+0),
  448. *(__u32 *)(sb->uuid+4),
  449. *(__u32 *)(sb->uuid+8),
  450. *(__u32 *)(sb->uuid+12));
  451. printk(KERN_DEBUG " events: %llu\n",
  452. (unsigned long long) le64_to_cpu(sb->events));
  453. printk(KERN_DEBUG "events cleared: %llu\n",
  454. (unsigned long long) le64_to_cpu(sb->events_cleared));
  455. printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
  456. printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
  457. printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
  458. printk(KERN_DEBUG " sync size: %llu KB\n",
  459. (unsigned long long)le64_to_cpu(sb->sync_size)/2);
  460. printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
  461. kunmap_atomic(sb, KM_USER0);
  462. }
  463. /* read the superblock from the bitmap file and initialize some bitmap fields */
  464. static int bitmap_read_sb(struct bitmap *bitmap)
  465. {
  466. char *reason = NULL;
  467. bitmap_super_t *sb;
  468. unsigned long chunksize, daemon_sleep, write_behind;
  469. unsigned long long events;
  470. int err = -EINVAL;
  471. /* page 0 is the superblock, read it... */
  472. if (bitmap->file) {
  473. loff_t isize = i_size_read(bitmap->file->f_mapping->host);
  474. int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
  475. bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
  476. } else {
  477. bitmap->sb_page = read_sb_page(bitmap->mddev, bitmap->offset,
  478. NULL,
  479. 0, sizeof(bitmap_super_t));
  480. }
  481. if (IS_ERR(bitmap->sb_page)) {
  482. err = PTR_ERR(bitmap->sb_page);
  483. bitmap->sb_page = NULL;
  484. return err;
  485. }
  486. sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
  487. chunksize = le32_to_cpu(sb->chunksize);
  488. daemon_sleep = le32_to_cpu(sb->daemon_sleep);
  489. write_behind = le32_to_cpu(sb->write_behind);
  490. /* verify that the bitmap-specific fields are valid */
  491. if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
  492. reason = "bad magic";
  493. else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
  494. le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
  495. reason = "unrecognized superblock version";
  496. else if (chunksize < PAGE_SIZE)
  497. reason = "bitmap chunksize too small";
  498. else if ((1 << ffz(~chunksize)) != chunksize)
  499. reason = "bitmap chunksize not a power of 2";
  500. else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT / HZ)
  501. reason = "daemon sleep period out of range";
  502. else if (write_behind > COUNTER_MAX)
  503. reason = "write-behind limit out of range (0 - 16383)";
  504. if (reason) {
  505. printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
  506. bmname(bitmap), reason);
  507. goto out;
  508. }
  509. /* keep the array size field of the bitmap superblock up to date */
  510. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  511. if (!bitmap->mddev->persistent)
  512. goto success;
  513. /*
  514. * if we have a persistent array superblock, compare the
  515. * bitmap's UUID and event counter to the mddev's
  516. */
  517. if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
  518. printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
  519. bmname(bitmap));
  520. goto out;
  521. }
  522. events = le64_to_cpu(sb->events);
  523. if (events < bitmap->mddev->events) {
  524. printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
  525. "-- forcing full recovery\n", bmname(bitmap), events,
  526. (unsigned long long) bitmap->mddev->events);
  527. sb->state |= cpu_to_le32(BITMAP_STALE);
  528. }
  529. success:
  530. /* assign fields using values from superblock */
  531. bitmap->chunksize = chunksize;
  532. bitmap->daemon_sleep = daemon_sleep;
  533. bitmap->daemon_lastrun = jiffies;
  534. bitmap->max_write_behind = write_behind;
  535. bitmap->flags |= le32_to_cpu(sb->state);
  536. if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
  537. bitmap->flags |= BITMAP_HOSTENDIAN;
  538. bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
  539. if (sb->state & cpu_to_le32(BITMAP_STALE))
  540. bitmap->events_cleared = bitmap->mddev->events;
  541. err = 0;
  542. out:
  543. kunmap_atomic(sb, KM_USER0);
  544. if (err)
  545. bitmap_print_sb(bitmap);
  546. return err;
  547. }
  548. enum bitmap_mask_op {
  549. MASK_SET,
  550. MASK_UNSET
  551. };
  552. /* record the state of the bitmap in the superblock. Return the old value */
  553. static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
  554. enum bitmap_mask_op op)
  555. {
  556. bitmap_super_t *sb;
  557. unsigned long flags;
  558. int old;
  559. spin_lock_irqsave(&bitmap->lock, flags);
  560. if (!bitmap->sb_page) { /* can't set the state */
  561. spin_unlock_irqrestore(&bitmap->lock, flags);
  562. return 0;
  563. }
  564. spin_unlock_irqrestore(&bitmap->lock, flags);
  565. sb = (bitmap_super_t *)kmap_atomic(bitmap->sb_page, KM_USER0);
  566. old = le32_to_cpu(sb->state) & bits;
  567. switch (op) {
  568. case MASK_SET: sb->state |= cpu_to_le32(bits);
  569. break;
  570. case MASK_UNSET: sb->state &= cpu_to_le32(~bits);
  571. break;
  572. default: BUG();
  573. }
  574. kunmap_atomic(sb, KM_USER0);
  575. return old;
  576. }
  577. /*
  578. * general bitmap file operations
  579. */
  580. /* calculate the index of the page that contains this bit */
  581. static inline unsigned long file_page_index(unsigned long chunk)
  582. {
  583. return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
  584. }
  585. /* calculate the (bit) offset of this bit within a page */
  586. static inline unsigned long file_page_offset(unsigned long chunk)
  587. {
  588. return CHUNK_BIT_OFFSET(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 *bitmap,
  598. unsigned long chunk)
  599. {
  600. if (file_page_index(chunk) >= bitmap->file_pages) return NULL;
  601. return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
  602. }
  603. static void bitmap_file_unmap(struct bitmap *bitmap)
  604. {
  605. struct page **map, *sb_page;
  606. unsigned long *attr;
  607. int pages;
  608. unsigned long flags;
  609. spin_lock_irqsave(&bitmap->lock, flags);
  610. map = bitmap->filemap;
  611. bitmap->filemap = NULL;
  612. attr = bitmap->filemap_attr;
  613. bitmap->filemap_attr = NULL;
  614. pages = bitmap->file_pages;
  615. bitmap->file_pages = 0;
  616. sb_page = bitmap->sb_page;
  617. bitmap->sb_page = NULL;
  618. spin_unlock_irqrestore(&bitmap->lock, flags);
  619. while (pages--)
  620. if (map[pages]->index != 0) /* 0 is sb_page, release it below */
  621. free_buffers(map[pages]);
  622. kfree(map);
  623. kfree(attr);
  624. if (sb_page)
  625. free_buffers(sb_page);
  626. }
  627. static void bitmap_file_put(struct bitmap *bitmap)
  628. {
  629. struct file *file;
  630. unsigned long flags;
  631. spin_lock_irqsave(&bitmap->lock, flags);
  632. file = bitmap->file;
  633. bitmap->file = NULL;
  634. spin_unlock_irqrestore(&bitmap->lock, flags);
  635. if (file)
  636. wait_event(bitmap->write_wait,
  637. atomic_read(&bitmap->pending_writes)==0);
  638. bitmap_file_unmap(bitmap);
  639. if (file) {
  640. struct inode *inode = file->f_path.dentry->d_inode;
  641. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  642. fput(file);
  643. }
  644. }
  645. /*
  646. * bitmap_file_kick - if an error occurs while manipulating the bitmap file
  647. * then it is no longer reliable, so we stop using it and we mark the file
  648. * as failed in the superblock
  649. */
  650. static void bitmap_file_kick(struct bitmap *bitmap)
  651. {
  652. char *path, *ptr = NULL;
  653. if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
  654. bitmap_update_sb(bitmap);
  655. if (bitmap->file) {
  656. path = kmalloc(PAGE_SIZE, GFP_KERNEL);
  657. if (path)
  658. ptr = d_path(&bitmap->file->f_path, path,
  659. PAGE_SIZE);
  660. printk(KERN_ALERT
  661. "%s: kicking failed bitmap file %s from array!\n",
  662. bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
  663. kfree(path);
  664. } else
  665. printk(KERN_ALERT
  666. "%s: disabling internal bitmap due to errors\n",
  667. bmname(bitmap));
  668. }
  669. bitmap_file_put(bitmap);
  670. return;
  671. }
  672. enum bitmap_page_attr {
  673. BITMAP_PAGE_DIRTY = 0, // there are set bits that need to be synced
  674. BITMAP_PAGE_CLEAN = 1, // there are bits that might need to be cleared
  675. BITMAP_PAGE_NEEDWRITE=2, // there are cleared bits that need to be synced
  676. };
  677. static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
  678. enum bitmap_page_attr attr)
  679. {
  680. __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
  681. }
  682. static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
  683. enum bitmap_page_attr attr)
  684. {
  685. __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
  686. }
  687. static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
  688. enum bitmap_page_attr attr)
  689. {
  690. return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
  691. }
  692. /*
  693. * bitmap_file_set_bit -- called before performing a write to the md device
  694. * to set (and eventually sync) a particular bit in the bitmap file
  695. *
  696. * we set the bit immediately, then we record the page number so that
  697. * when an unplug occurs, we can flush the dirty pages out to disk
  698. */
  699. static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
  700. {
  701. unsigned long bit;
  702. struct page *page;
  703. void *kaddr;
  704. unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
  705. if (!bitmap->filemap) {
  706. return;
  707. }
  708. page = filemap_get_page(bitmap, chunk);
  709. if (!page) return;
  710. bit = file_page_offset(chunk);
  711. /* set the bit */
  712. kaddr = kmap_atomic(page, KM_USER0);
  713. if (bitmap->flags & BITMAP_HOSTENDIAN)
  714. set_bit(bit, kaddr);
  715. else
  716. ext2_set_bit(bit, kaddr);
  717. kunmap_atomic(kaddr, KM_USER0);
  718. PRINTK("set file bit %lu page %lu\n", bit, page->index);
  719. /* record page number so it gets flushed to disk when unplug occurs */
  720. set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  721. }
  722. /* this gets called when the md device is ready to unplug its underlying
  723. * (slave) device queues -- before we let any writes go down, we need to
  724. * sync the dirty pages of the bitmap file to disk */
  725. void bitmap_unplug(struct bitmap *bitmap)
  726. {
  727. unsigned long i, flags;
  728. int dirty, need_write;
  729. struct page *page;
  730. int wait = 0;
  731. if (!bitmap)
  732. return;
  733. /* look at each page to see if there are any set bits that need to be
  734. * flushed out to disk */
  735. for (i = 0; i < bitmap->file_pages; i++) {
  736. spin_lock_irqsave(&bitmap->lock, flags);
  737. if (!bitmap->filemap) {
  738. spin_unlock_irqrestore(&bitmap->lock, flags);
  739. return;
  740. }
  741. page = bitmap->filemap[i];
  742. dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  743. need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  744. clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  745. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  746. if (dirty)
  747. wait = 1;
  748. spin_unlock_irqrestore(&bitmap->lock, flags);
  749. if (dirty | need_write)
  750. write_page(bitmap, page, 0);
  751. }
  752. if (wait) { /* if any writes were performed, we need to wait on them */
  753. if (bitmap->file)
  754. wait_event(bitmap->write_wait,
  755. atomic_read(&bitmap->pending_writes)==0);
  756. else
  757. md_super_wait(bitmap->mddev);
  758. }
  759. if (bitmap->flags & BITMAP_WRITE_ERROR)
  760. bitmap_file_kick(bitmap);
  761. }
  762. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
  763. /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
  764. * the in-memory bitmap from the on-disk bitmap -- also, sets up the
  765. * memory mapping of the bitmap file
  766. * Special cases:
  767. * if there's no bitmap file, or if the bitmap file had been
  768. * previously kicked from the array, we mark all the bits as
  769. * 1's in order to cause a full resync.
  770. *
  771. * We ignore all bits for sectors that end earlier than 'start'.
  772. * This is used when reading an out-of-date bitmap...
  773. */
  774. static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
  775. {
  776. unsigned long i, chunks, index, oldindex, bit;
  777. struct page *page = NULL, *oldpage = NULL;
  778. unsigned long num_pages, bit_cnt = 0;
  779. struct file *file;
  780. unsigned long bytes, offset;
  781. int outofdate;
  782. int ret = -ENOSPC;
  783. void *paddr;
  784. chunks = bitmap->chunks;
  785. file = bitmap->file;
  786. BUG_ON(!file && !bitmap->offset);
  787. #ifdef INJECT_FAULTS_3
  788. outofdate = 1;
  789. #else
  790. outofdate = bitmap->flags & BITMAP_STALE;
  791. #endif
  792. if (outofdate)
  793. printk(KERN_INFO "%s: bitmap file is out of date, doing full "
  794. "recovery\n", bmname(bitmap));
  795. bytes = (chunks + 7) / 8;
  796. num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
  797. if (file && i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
  798. printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
  799. bmname(bitmap),
  800. (unsigned long) i_size_read(file->f_mapping->host),
  801. bytes + sizeof(bitmap_super_t));
  802. goto err;
  803. }
  804. ret = -ENOMEM;
  805. bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  806. if (!bitmap->filemap)
  807. goto err;
  808. /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
  809. bitmap->filemap_attr = kzalloc(
  810. roundup( DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
  811. GFP_KERNEL);
  812. if (!bitmap->filemap_attr)
  813. goto err;
  814. oldindex = ~0L;
  815. for (i = 0; i < chunks; i++) {
  816. int b;
  817. index = file_page_index(i);
  818. bit = file_page_offset(i);
  819. if (index != oldindex) { /* this is a new page, read it in */
  820. int count;
  821. /* unmap the old page, we're done with it */
  822. if (index == num_pages-1)
  823. count = bytes + sizeof(bitmap_super_t)
  824. - index * PAGE_SIZE;
  825. else
  826. count = PAGE_SIZE;
  827. if (index == 0) {
  828. /*
  829. * if we're here then the superblock page
  830. * contains some bits (PAGE_SIZE != sizeof sb)
  831. * we've already read it in, so just use it
  832. */
  833. page = bitmap->sb_page;
  834. offset = sizeof(bitmap_super_t);
  835. if (!file)
  836. read_sb_page(bitmap->mddev,
  837. bitmap->offset,
  838. page,
  839. index, count);
  840. } else if (file) {
  841. page = read_page(file, index, bitmap, count);
  842. offset = 0;
  843. } else {
  844. page = read_sb_page(bitmap->mddev, bitmap->offset,
  845. NULL,
  846. index, count);
  847. offset = 0;
  848. }
  849. if (IS_ERR(page)) { /* read error */
  850. ret = PTR_ERR(page);
  851. goto err;
  852. }
  853. oldindex = index;
  854. oldpage = page;
  855. if (outofdate) {
  856. /*
  857. * if bitmap is out of date, dirty the
  858. * whole page and write it out
  859. */
  860. paddr = kmap_atomic(page, KM_USER0);
  861. memset(paddr + offset, 0xff,
  862. PAGE_SIZE - offset);
  863. kunmap_atomic(paddr, KM_USER0);
  864. write_page(bitmap, page, 1);
  865. ret = -EIO;
  866. if (bitmap->flags & BITMAP_WRITE_ERROR) {
  867. /* release, page not in filemap yet */
  868. put_page(page);
  869. goto err;
  870. }
  871. }
  872. bitmap->filemap[bitmap->file_pages++] = page;
  873. bitmap->last_page_size = count;
  874. }
  875. paddr = kmap_atomic(page, KM_USER0);
  876. if (bitmap->flags & BITMAP_HOSTENDIAN)
  877. b = test_bit(bit, paddr);
  878. else
  879. b = ext2_test_bit(bit, paddr);
  880. kunmap_atomic(paddr, KM_USER0);
  881. if (b) {
  882. /* if the disk bit is set, set the memory bit */
  883. bitmap_set_memory_bits(bitmap, i << CHUNK_BLOCK_SHIFT(bitmap),
  884. ((i+1) << (CHUNK_BLOCK_SHIFT(bitmap)) >= start)
  885. );
  886. bit_cnt++;
  887. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  888. }
  889. }
  890. /* everything went OK */
  891. ret = 0;
  892. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
  893. if (bit_cnt) { /* Kick recovery if any bits were set */
  894. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  895. md_wakeup_thread(bitmap->mddev->thread);
  896. }
  897. printk(KERN_INFO "%s: bitmap initialized from disk: "
  898. "read %lu/%lu pages, set %lu bits\n",
  899. bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt);
  900. return 0;
  901. err:
  902. printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
  903. bmname(bitmap), ret);
  904. return ret;
  905. }
  906. void bitmap_write_all(struct bitmap *bitmap)
  907. {
  908. /* We don't actually write all bitmap blocks here,
  909. * just flag them as needing to be written
  910. */
  911. int i;
  912. for (i=0; i < bitmap->file_pages; i++)
  913. set_page_attr(bitmap, bitmap->filemap[i],
  914. BITMAP_PAGE_NEEDWRITE);
  915. }
  916. static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
  917. {
  918. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  919. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  920. bitmap->bp[page].count += inc;
  921. /*
  922. if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
  923. (unsigned long long)offset, inc, bitmap->bp[page].count);
  924. */
  925. bitmap_checkfree(bitmap, page);
  926. }
  927. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  928. sector_t offset, int *blocks,
  929. int create);
  930. /*
  931. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  932. * out to disk
  933. */
  934. void bitmap_daemon_work(struct bitmap *bitmap)
  935. {
  936. unsigned long j;
  937. unsigned long flags;
  938. struct page *page = NULL, *lastpage = NULL;
  939. int blocks;
  940. void *paddr;
  941. if (bitmap == NULL)
  942. return;
  943. if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
  944. goto done;
  945. bitmap->daemon_lastrun = jiffies;
  946. if (bitmap->allclean) {
  947. bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  948. return;
  949. }
  950. bitmap->allclean = 1;
  951. for (j = 0; j < bitmap->chunks; j++) {
  952. bitmap_counter_t *bmc;
  953. spin_lock_irqsave(&bitmap->lock, flags);
  954. if (!bitmap->filemap) {
  955. /* error or shutdown */
  956. spin_unlock_irqrestore(&bitmap->lock, flags);
  957. break;
  958. }
  959. page = filemap_get_page(bitmap, j);
  960. if (page != lastpage) {
  961. /* skip this page unless it's marked as needing cleaning */
  962. if (!test_page_attr(bitmap, page, BITMAP_PAGE_CLEAN)) {
  963. int need_write = test_page_attr(bitmap, page,
  964. BITMAP_PAGE_NEEDWRITE);
  965. if (need_write)
  966. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  967. spin_unlock_irqrestore(&bitmap->lock, flags);
  968. if (need_write) {
  969. write_page(bitmap, page, 0);
  970. bitmap->allclean = 0;
  971. }
  972. continue;
  973. }
  974. /* grab the new page, sync and release the old */
  975. if (lastpage != NULL) {
  976. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  977. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  978. spin_unlock_irqrestore(&bitmap->lock, flags);
  979. write_page(bitmap, lastpage, 0);
  980. } else {
  981. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  982. spin_unlock_irqrestore(&bitmap->lock, flags);
  983. }
  984. } else
  985. spin_unlock_irqrestore(&bitmap->lock, flags);
  986. lastpage = page;
  987. /* We are possibly going to clear some bits, so make
  988. * sure that events_cleared is up-to-date.
  989. */
  990. if (bitmap->need_sync) {
  991. bitmap_super_t *sb;
  992. bitmap->need_sync = 0;
  993. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  994. sb->events_cleared =
  995. cpu_to_le64(bitmap->events_cleared);
  996. kunmap_atomic(sb, KM_USER0);
  997. write_page(bitmap, bitmap->sb_page, 1);
  998. }
  999. spin_lock_irqsave(&bitmap->lock, flags);
  1000. clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1001. }
  1002. bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
  1003. &blocks, 0);
  1004. if (bmc) {
  1005. /*
  1006. if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
  1007. */
  1008. if (*bmc)
  1009. bitmap->allclean = 0;
  1010. if (*bmc == 2) {
  1011. *bmc=1; /* maybe clear the bit next time */
  1012. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1013. } else if (*bmc == 1) {
  1014. /* we can clear the bit */
  1015. *bmc = 0;
  1016. bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
  1017. -1);
  1018. /* clear the bit */
  1019. paddr = kmap_atomic(page, KM_USER0);
  1020. if (bitmap->flags & BITMAP_HOSTENDIAN)
  1021. clear_bit(file_page_offset(j), paddr);
  1022. else
  1023. ext2_clear_bit(file_page_offset(j), paddr);
  1024. kunmap_atomic(paddr, KM_USER0);
  1025. }
  1026. }
  1027. spin_unlock_irqrestore(&bitmap->lock, flags);
  1028. }
  1029. /* now sync the final page */
  1030. if (lastpage != NULL) {
  1031. spin_lock_irqsave(&bitmap->lock, flags);
  1032. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  1033. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1034. spin_unlock_irqrestore(&bitmap->lock, flags);
  1035. write_page(bitmap, lastpage, 0);
  1036. } else {
  1037. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1038. spin_unlock_irqrestore(&bitmap->lock, flags);
  1039. }
  1040. }
  1041. done:
  1042. if (bitmap->allclean == 0)
  1043. bitmap->mddev->thread->timeout = bitmap->daemon_sleep * HZ;
  1044. }
  1045. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1046. sector_t offset, int *blocks,
  1047. int create)
  1048. {
  1049. /* If 'create', we might release the lock and reclaim it.
  1050. * The lock must have been taken with interrupts enabled.
  1051. * If !create, we don't release the lock.
  1052. */
  1053. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  1054. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1055. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1056. sector_t csize;
  1057. if (bitmap_checkpage(bitmap, page, create) < 0) {
  1058. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1059. *blocks = csize - (offset & (csize- 1));
  1060. return NULL;
  1061. }
  1062. /* now locked ... */
  1063. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1064. /* should we use the first or second counter field
  1065. * of the hijacked pointer? */
  1066. int hi = (pageoff > PAGE_COUNTER_MASK);
  1067. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
  1068. PAGE_COUNTER_SHIFT - 1);
  1069. *blocks = csize - (offset & (csize- 1));
  1070. return &((bitmap_counter_t *)
  1071. &bitmap->bp[page].map)[hi];
  1072. } else { /* page is allocated */
  1073. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1074. *blocks = csize - (offset & (csize- 1));
  1075. return (bitmap_counter_t *)
  1076. &(bitmap->bp[page].map[pageoff]);
  1077. }
  1078. }
  1079. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1080. {
  1081. if (!bitmap) return 0;
  1082. if (behind) {
  1083. atomic_inc(&bitmap->behind_writes);
  1084. PRINTK(KERN_DEBUG "inc write-behind count %d/%d\n",
  1085. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1086. }
  1087. while (sectors) {
  1088. int blocks;
  1089. bitmap_counter_t *bmc;
  1090. spin_lock_irq(&bitmap->lock);
  1091. bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
  1092. if (!bmc) {
  1093. spin_unlock_irq(&bitmap->lock);
  1094. return 0;
  1095. }
  1096. if (unlikely((*bmc & COUNTER_MAX) == COUNTER_MAX)) {
  1097. DEFINE_WAIT(__wait);
  1098. /* note that it is safe to do the prepare_to_wait
  1099. * after the test as long as we do it before dropping
  1100. * the spinlock.
  1101. */
  1102. prepare_to_wait(&bitmap->overflow_wait, &__wait,
  1103. TASK_UNINTERRUPTIBLE);
  1104. spin_unlock_irq(&bitmap->lock);
  1105. blk_unplug(bitmap->mddev->queue);
  1106. schedule();
  1107. finish_wait(&bitmap->overflow_wait, &__wait);
  1108. continue;
  1109. }
  1110. switch(*bmc) {
  1111. case 0:
  1112. bitmap_file_set_bit(bitmap, offset);
  1113. bitmap_count_page(bitmap,offset, 1);
  1114. blk_plug_device_unlocked(bitmap->mddev->queue);
  1115. /* fall through */
  1116. case 1:
  1117. *bmc = 2;
  1118. }
  1119. (*bmc)++;
  1120. spin_unlock_irq(&bitmap->lock);
  1121. offset += blocks;
  1122. if (sectors > blocks)
  1123. sectors -= blocks;
  1124. else sectors = 0;
  1125. }
  1126. bitmap->allclean = 0;
  1127. return 0;
  1128. }
  1129. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1130. int success, int behind)
  1131. {
  1132. if (!bitmap) return;
  1133. if (behind) {
  1134. atomic_dec(&bitmap->behind_writes);
  1135. PRINTK(KERN_DEBUG "dec write-behind count %d/%d\n",
  1136. atomic_read(&bitmap->behind_writes), bitmap->max_write_behind);
  1137. }
  1138. while (sectors) {
  1139. int blocks;
  1140. unsigned long flags;
  1141. bitmap_counter_t *bmc;
  1142. spin_lock_irqsave(&bitmap->lock, flags);
  1143. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1144. if (!bmc) {
  1145. spin_unlock_irqrestore(&bitmap->lock, flags);
  1146. return;
  1147. }
  1148. if (success &&
  1149. bitmap->events_cleared < bitmap->mddev->events) {
  1150. bitmap->events_cleared = bitmap->mddev->events;
  1151. bitmap->need_sync = 1;
  1152. }
  1153. if (!success && ! (*bmc & NEEDED_MASK))
  1154. *bmc |= NEEDED_MASK;
  1155. if ((*bmc & COUNTER_MAX) == COUNTER_MAX)
  1156. wake_up(&bitmap->overflow_wait);
  1157. (*bmc)--;
  1158. if (*bmc <= 2) {
  1159. set_page_attr(bitmap,
  1160. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1161. BITMAP_PAGE_CLEAN);
  1162. }
  1163. spin_unlock_irqrestore(&bitmap->lock, flags);
  1164. offset += blocks;
  1165. if (sectors > blocks)
  1166. sectors -= blocks;
  1167. else sectors = 0;
  1168. }
  1169. }
  1170. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks,
  1171. int degraded)
  1172. {
  1173. bitmap_counter_t *bmc;
  1174. int rv;
  1175. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1176. *blocks = 1024;
  1177. return 1; /* always resync if no bitmap */
  1178. }
  1179. spin_lock_irq(&bitmap->lock);
  1180. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1181. rv = 0;
  1182. if (bmc) {
  1183. /* locked */
  1184. if (RESYNC(*bmc))
  1185. rv = 1;
  1186. else if (NEEDED(*bmc)) {
  1187. rv = 1;
  1188. if (!degraded) { /* don't set/clear bits if degraded */
  1189. *bmc |= RESYNC_MASK;
  1190. *bmc &= ~NEEDED_MASK;
  1191. }
  1192. }
  1193. }
  1194. spin_unlock_irq(&bitmap->lock);
  1195. bitmap->allclean = 0;
  1196. return rv;
  1197. }
  1198. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
  1199. {
  1200. bitmap_counter_t *bmc;
  1201. unsigned long flags;
  1202. /*
  1203. if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
  1204. */ if (bitmap == NULL) {
  1205. *blocks = 1024;
  1206. return;
  1207. }
  1208. spin_lock_irqsave(&bitmap->lock, flags);
  1209. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1210. if (bmc == NULL)
  1211. goto unlock;
  1212. /* locked */
  1213. /*
  1214. if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
  1215. */
  1216. if (RESYNC(*bmc)) {
  1217. *bmc &= ~RESYNC_MASK;
  1218. if (!NEEDED(*bmc) && aborted)
  1219. *bmc |= NEEDED_MASK;
  1220. else {
  1221. if (*bmc <= 2) {
  1222. set_page_attr(bitmap,
  1223. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1224. BITMAP_PAGE_CLEAN);
  1225. }
  1226. }
  1227. }
  1228. unlock:
  1229. spin_unlock_irqrestore(&bitmap->lock, flags);
  1230. bitmap->allclean = 0;
  1231. }
  1232. void bitmap_close_sync(struct bitmap *bitmap)
  1233. {
  1234. /* Sync has finished, and any bitmap chunks that weren't synced
  1235. * properly have been aborted. It remains to us to clear the
  1236. * RESYNC bit wherever it is still on
  1237. */
  1238. sector_t sector = 0;
  1239. int blocks;
  1240. if (!bitmap)
  1241. return;
  1242. while (sector < bitmap->mddev->resync_max_sectors) {
  1243. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1244. sector += blocks;
  1245. }
  1246. }
  1247. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1248. {
  1249. sector_t s = 0;
  1250. int blocks;
  1251. if (!bitmap)
  1252. return;
  1253. if (sector == 0) {
  1254. bitmap->last_end_sync = jiffies;
  1255. return;
  1256. }
  1257. if (time_before(jiffies, (bitmap->last_end_sync
  1258. + bitmap->daemon_sleep * HZ)))
  1259. return;
  1260. wait_event(bitmap->mddev->recovery_wait,
  1261. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1262. sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
  1263. s = 0;
  1264. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1265. bitmap_end_sync(bitmap, s, &blocks, 0);
  1266. s += blocks;
  1267. }
  1268. bitmap->last_end_sync = jiffies;
  1269. }
  1270. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1271. {
  1272. /* For each chunk covered by any of these sectors, set the
  1273. * counter to 1 and set resync_needed. They should all
  1274. * be 0 at this point
  1275. */
  1276. int secs;
  1277. bitmap_counter_t *bmc;
  1278. spin_lock_irq(&bitmap->lock);
  1279. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1280. if (!bmc) {
  1281. spin_unlock_irq(&bitmap->lock);
  1282. return;
  1283. }
  1284. if (! *bmc) {
  1285. struct page *page;
  1286. *bmc = 1 | (needed?NEEDED_MASK:0);
  1287. bitmap_count_page(bitmap, offset, 1);
  1288. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1289. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1290. }
  1291. spin_unlock_irq(&bitmap->lock);
  1292. bitmap->allclean = 0;
  1293. }
  1294. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1295. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1296. {
  1297. unsigned long chunk;
  1298. for (chunk = s; chunk <= e; chunk++) {
  1299. sector_t sec = chunk << CHUNK_BLOCK_SHIFT(bitmap);
  1300. bitmap_set_memory_bits(bitmap, sec, 1);
  1301. bitmap_file_set_bit(bitmap, sec);
  1302. }
  1303. }
  1304. /*
  1305. * flush out any pending updates
  1306. */
  1307. void bitmap_flush(mddev_t *mddev)
  1308. {
  1309. struct bitmap *bitmap = mddev->bitmap;
  1310. int sleep;
  1311. if (!bitmap) /* there was no bitmap */
  1312. return;
  1313. /* run the daemon_work three time to ensure everything is flushed
  1314. * that can be
  1315. */
  1316. sleep = bitmap->daemon_sleep;
  1317. bitmap->daemon_sleep = 0;
  1318. bitmap_daemon_work(bitmap);
  1319. bitmap_daemon_work(bitmap);
  1320. bitmap_daemon_work(bitmap);
  1321. bitmap->daemon_sleep = sleep;
  1322. bitmap_update_sb(bitmap);
  1323. }
  1324. /*
  1325. * free memory that was allocated
  1326. */
  1327. static void bitmap_free(struct bitmap *bitmap)
  1328. {
  1329. unsigned long k, pages;
  1330. struct bitmap_page *bp;
  1331. if (!bitmap) /* there was no bitmap */
  1332. return;
  1333. /* release the bitmap file and kill the daemon */
  1334. bitmap_file_put(bitmap);
  1335. bp = bitmap->bp;
  1336. pages = bitmap->pages;
  1337. /* free all allocated memory */
  1338. if (bp) /* deallocate the page memory */
  1339. for (k = 0; k < pages; k++)
  1340. if (bp[k].map && !bp[k].hijacked)
  1341. kfree(bp[k].map);
  1342. kfree(bp);
  1343. kfree(bitmap);
  1344. }
  1345. void bitmap_destroy(mddev_t *mddev)
  1346. {
  1347. struct bitmap *bitmap = mddev->bitmap;
  1348. if (!bitmap) /* there was no bitmap */
  1349. return;
  1350. mddev->bitmap = NULL; /* disconnect from the md device */
  1351. if (mddev->thread)
  1352. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1353. bitmap_free(bitmap);
  1354. }
  1355. /*
  1356. * initialize the bitmap structure
  1357. * if this returns an error, bitmap_destroy must be called to do clean up
  1358. */
  1359. int bitmap_create(mddev_t *mddev)
  1360. {
  1361. struct bitmap *bitmap;
  1362. unsigned long blocks = mddev->resync_max_sectors;
  1363. unsigned long chunks;
  1364. unsigned long pages;
  1365. struct file *file = mddev->bitmap_file;
  1366. int err;
  1367. sector_t start;
  1368. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1369. if (!file && !mddev->bitmap_offset) /* bitmap disabled, nothing to do */
  1370. return 0;
  1371. BUG_ON(file && mddev->bitmap_offset);
  1372. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1373. if (!bitmap)
  1374. return -ENOMEM;
  1375. spin_lock_init(&bitmap->lock);
  1376. atomic_set(&bitmap->pending_writes, 0);
  1377. init_waitqueue_head(&bitmap->write_wait);
  1378. init_waitqueue_head(&bitmap->overflow_wait);
  1379. bitmap->mddev = mddev;
  1380. bitmap->file = file;
  1381. bitmap->offset = mddev->bitmap_offset;
  1382. if (file) {
  1383. get_file(file);
  1384. do_sync_mapping_range(file->f_mapping, 0, LLONG_MAX,
  1385. SYNC_FILE_RANGE_WAIT_BEFORE |
  1386. SYNC_FILE_RANGE_WRITE |
  1387. SYNC_FILE_RANGE_WAIT_AFTER);
  1388. }
  1389. /* read superblock from bitmap file (this sets bitmap->chunksize) */
  1390. err = bitmap_read_sb(bitmap);
  1391. if (err)
  1392. goto error;
  1393. bitmap->chunkshift = ffz(~bitmap->chunksize);
  1394. /* now that chunksize and chunkshift are set, we can use these macros */
  1395. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
  1396. CHUNK_BLOCK_RATIO(bitmap);
  1397. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1398. BUG_ON(!pages);
  1399. bitmap->chunks = chunks;
  1400. bitmap->pages = pages;
  1401. bitmap->missing_pages = pages;
  1402. bitmap->counter_bits = COUNTER_BITS;
  1403. bitmap->syncchunk = ~0UL;
  1404. #ifdef INJECT_FATAL_FAULT_1
  1405. bitmap->bp = NULL;
  1406. #else
  1407. bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1408. #endif
  1409. err = -ENOMEM;
  1410. if (!bitmap->bp)
  1411. goto error;
  1412. /* now that we have some pages available, initialize the in-memory
  1413. * bitmap from the on-disk bitmap */
  1414. start = 0;
  1415. if (mddev->degraded == 0
  1416. || bitmap->events_cleared == mddev->events)
  1417. /* no need to keep dirty bits to optimise a re-add of a missing device */
  1418. start = mddev->recovery_cp;
  1419. err = bitmap_init_from_disk(bitmap, start);
  1420. if (err)
  1421. goto error;
  1422. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1423. pages, bmname(bitmap));
  1424. mddev->bitmap = bitmap;
  1425. mddev->thread->timeout = bitmap->daemon_sleep * HZ;
  1426. bitmap_update_sb(bitmap);
  1427. return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
  1428. error:
  1429. bitmap_free(bitmap);
  1430. return err;
  1431. }
  1432. /* the bitmap API -- for raid personalities */
  1433. EXPORT_SYMBOL(bitmap_startwrite);
  1434. EXPORT_SYMBOL(bitmap_endwrite);
  1435. EXPORT_SYMBOL(bitmap_start_sync);
  1436. EXPORT_SYMBOL(bitmap_end_sync);
  1437. EXPORT_SYMBOL(bitmap_unplug);
  1438. EXPORT_SYMBOL(bitmap_close_sync);
  1439. EXPORT_SYMBOL(bitmap_cond_end_sync);