bitmap.c 40 KB

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