bitmap.c 39 KB

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