bitmap.c 42 KB

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