bitmap.c 42 KB

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