bitmap.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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. #if 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. /*
  213. * write out a page
  214. */
  215. static int write_page(struct page *page, int wait)
  216. {
  217. int ret = -ENOMEM;
  218. lock_page(page);
  219. if (page->mapping == NULL)
  220. goto unlock_out;
  221. else if (i_size_read(page->mapping->host) < page->index << PAGE_SHIFT) {
  222. ret = -ENOENT;
  223. goto unlock_out;
  224. }
  225. ret = page->mapping->a_ops->prepare_write(NULL, page, 0, PAGE_SIZE);
  226. if (!ret)
  227. ret = page->mapping->a_ops->commit_write(NULL, page, 0,
  228. PAGE_SIZE);
  229. if (ret) {
  230. unlock_out:
  231. unlock_page(page);
  232. return ret;
  233. }
  234. set_page_dirty(page); /* force it to be written out */
  235. return write_one_page(page, wait);
  236. }
  237. /* read a page from a file, pinning it into cache, and return bytes_read */
  238. static struct page *read_page(struct file *file, unsigned long index,
  239. unsigned long *bytes_read)
  240. {
  241. struct inode *inode = file->f_mapping->host;
  242. struct page *page = NULL;
  243. loff_t isize = i_size_read(inode);
  244. unsigned long end_index = isize >> PAGE_CACHE_SHIFT;
  245. PRINTK("read bitmap file (%dB @ %Lu)\n", (int)PAGE_CACHE_SIZE,
  246. (unsigned long long)index << PAGE_CACHE_SHIFT);
  247. page = read_cache_page(inode->i_mapping, index,
  248. (filler_t *)inode->i_mapping->a_ops->readpage, file);
  249. if (IS_ERR(page))
  250. goto out;
  251. wait_on_page_locked(page);
  252. if (!PageUptodate(page) || PageError(page)) {
  253. page_cache_release(page);
  254. page = ERR_PTR(-EIO);
  255. goto out;
  256. }
  257. if (index > end_index) /* we have read beyond EOF */
  258. *bytes_read = 0;
  259. else if (index == end_index) /* possible short read */
  260. *bytes_read = isize & ~PAGE_CACHE_MASK;
  261. else
  262. *bytes_read = PAGE_CACHE_SIZE; /* got a full page */
  263. out:
  264. if (IS_ERR(page))
  265. printk(KERN_ALERT "md: bitmap read error: (%dB @ %Lu): %ld\n",
  266. (int)PAGE_CACHE_SIZE,
  267. (unsigned long long)index << PAGE_CACHE_SHIFT,
  268. PTR_ERR(page));
  269. return page;
  270. }
  271. /*
  272. * bitmap file superblock operations
  273. */
  274. /* update the event counter and sync the superblock to disk */
  275. int bitmap_update_sb(struct bitmap *bitmap)
  276. {
  277. bitmap_super_t *sb;
  278. unsigned long flags;
  279. if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
  280. return 0;
  281. spin_lock_irqsave(&bitmap->lock, flags);
  282. if (!bitmap->sb_page) { /* no superblock */
  283. spin_unlock_irqrestore(&bitmap->lock, flags);
  284. return 0;
  285. }
  286. page_cache_get(bitmap->sb_page);
  287. spin_unlock_irqrestore(&bitmap->lock, flags);
  288. sb = (bitmap_super_t *)kmap(bitmap->sb_page);
  289. sb->events = cpu_to_le64(bitmap->mddev->events);
  290. if (!bitmap->mddev->degraded)
  291. sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
  292. kunmap(bitmap->sb_page);
  293. write_page(bitmap->sb_page, 0);
  294. return 0;
  295. }
  296. /* print out the bitmap file superblock */
  297. void bitmap_print_sb(struct bitmap *bitmap)
  298. {
  299. bitmap_super_t *sb;
  300. if (!bitmap || !bitmap->sb_page)
  301. return;
  302. sb = (bitmap_super_t *)kmap(bitmap->sb_page);
  303. printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
  304. printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
  305. printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
  306. printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
  307. *(__u32 *)(sb->uuid+0),
  308. *(__u32 *)(sb->uuid+4),
  309. *(__u32 *)(sb->uuid+8),
  310. *(__u32 *)(sb->uuid+12));
  311. printk(KERN_DEBUG " events: %llu\n",
  312. (unsigned long long) le64_to_cpu(sb->events));
  313. printk(KERN_DEBUG "events_clred: %llu\n",
  314. (unsigned long long) le64_to_cpu(sb->events_cleared));
  315. printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
  316. printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
  317. printk(KERN_DEBUG "daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
  318. printk(KERN_DEBUG " sync size: %llu KB\n",
  319. (unsigned long long)le64_to_cpu(sb->sync_size));
  320. kunmap(bitmap->sb_page);
  321. }
  322. /* read the superblock from the bitmap file and initialize some bitmap fields */
  323. static int bitmap_read_sb(struct bitmap *bitmap)
  324. {
  325. char *reason = NULL;
  326. bitmap_super_t *sb;
  327. unsigned long chunksize, daemon_sleep;
  328. unsigned long bytes_read;
  329. unsigned long long events;
  330. int err = -EINVAL;
  331. /* page 0 is the superblock, read it... */
  332. bitmap->sb_page = read_page(bitmap->file, 0, &bytes_read);
  333. if (IS_ERR(bitmap->sb_page)) {
  334. err = PTR_ERR(bitmap->sb_page);
  335. bitmap->sb_page = NULL;
  336. return err;
  337. }
  338. sb = (bitmap_super_t *)kmap(bitmap->sb_page);
  339. if (bytes_read < sizeof(*sb)) { /* short read */
  340. printk(KERN_INFO "%s: bitmap file superblock truncated\n",
  341. bmname(bitmap));
  342. err = -ENOSPC;
  343. goto out;
  344. }
  345. chunksize = le32_to_cpu(sb->chunksize);
  346. daemon_sleep = le32_to_cpu(sb->daemon_sleep);
  347. /* verify that the bitmap-specific fields are valid */
  348. if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
  349. reason = "bad magic";
  350. else if (sb->version != cpu_to_le32(BITMAP_MAJOR))
  351. reason = "unrecognized superblock version";
  352. else if (chunksize < 512 || chunksize > (1024 * 1024 * 4))
  353. reason = "bitmap chunksize out of range (512B - 4MB)";
  354. else if ((1 << ffz(~chunksize)) != chunksize)
  355. reason = "bitmap chunksize not a power of 2";
  356. else if (daemon_sleep < 1 || daemon_sleep > 15)
  357. reason = "daemon sleep period out of range";
  358. if (reason) {
  359. printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
  360. bmname(bitmap), reason);
  361. goto out;
  362. }
  363. /* keep the array size field of the bitmap superblock up to date */
  364. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  365. if (!bitmap->mddev->persistent)
  366. goto success;
  367. /*
  368. * if we have a persistent array superblock, compare the
  369. * bitmap's UUID and event counter to the mddev's
  370. */
  371. if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
  372. printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
  373. bmname(bitmap));
  374. goto out;
  375. }
  376. events = le64_to_cpu(sb->events);
  377. if (events < bitmap->mddev->events) {
  378. printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
  379. "-- forcing full recovery\n", bmname(bitmap), events,
  380. (unsigned long long) bitmap->mddev->events);
  381. sb->state |= BITMAP_STALE;
  382. }
  383. success:
  384. /* assign fields using values from superblock */
  385. bitmap->chunksize = chunksize;
  386. bitmap->daemon_sleep = daemon_sleep;
  387. bitmap->flags |= sb->state;
  388. bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
  389. err = 0;
  390. out:
  391. kunmap(bitmap->sb_page);
  392. if (err)
  393. bitmap_print_sb(bitmap);
  394. return err;
  395. }
  396. enum bitmap_mask_op {
  397. MASK_SET,
  398. MASK_UNSET
  399. };
  400. /* record the state of the bitmap in the superblock */
  401. static void bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
  402. enum bitmap_mask_op op)
  403. {
  404. bitmap_super_t *sb;
  405. unsigned long flags;
  406. spin_lock_irqsave(&bitmap->lock, flags);
  407. if (!bitmap || !bitmap->sb_page) { /* can't set the state */
  408. spin_unlock_irqrestore(&bitmap->lock, flags);
  409. return;
  410. }
  411. page_cache_get(bitmap->sb_page);
  412. spin_unlock_irqrestore(&bitmap->lock, flags);
  413. sb = (bitmap_super_t *)kmap(bitmap->sb_page);
  414. switch (op) {
  415. case MASK_SET: sb->state |= bits;
  416. break;
  417. case MASK_UNSET: sb->state &= ~bits;
  418. break;
  419. default: BUG();
  420. }
  421. kunmap(bitmap->sb_page);
  422. page_cache_release(bitmap->sb_page);
  423. }
  424. /*
  425. * general bitmap file operations
  426. */
  427. /* calculate the index of the page that contains this bit */
  428. static inline unsigned long file_page_index(unsigned long chunk)
  429. {
  430. return CHUNK_BIT_OFFSET(chunk) >> PAGE_BIT_SHIFT;
  431. }
  432. /* calculate the (bit) offset of this bit within a page */
  433. static inline unsigned long file_page_offset(unsigned long chunk)
  434. {
  435. return CHUNK_BIT_OFFSET(chunk) & (PAGE_BITS - 1);
  436. }
  437. /*
  438. * return a pointer to the page in the filemap that contains the given bit
  439. *
  440. * this lookup is complicated by the fact that the bitmap sb might be exactly
  441. * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
  442. * 0 or page 1
  443. */
  444. static inline struct page *filemap_get_page(struct bitmap *bitmap,
  445. unsigned long chunk)
  446. {
  447. return bitmap->filemap[file_page_index(chunk) - file_page_index(0)];
  448. }
  449. static void bitmap_file_unmap(struct bitmap *bitmap)
  450. {
  451. struct page **map, *sb_page;
  452. unsigned long *attr;
  453. int pages;
  454. unsigned long flags;
  455. spin_lock_irqsave(&bitmap->lock, flags);
  456. map = bitmap->filemap;
  457. bitmap->filemap = NULL;
  458. attr = bitmap->filemap_attr;
  459. bitmap->filemap_attr = NULL;
  460. pages = bitmap->file_pages;
  461. bitmap->file_pages = 0;
  462. sb_page = bitmap->sb_page;
  463. bitmap->sb_page = NULL;
  464. spin_unlock_irqrestore(&bitmap->lock, flags);
  465. while (pages--)
  466. if (map[pages]->index != 0) /* 0 is sb_page, release it below */
  467. page_cache_release(map[pages]);
  468. kfree(map);
  469. kfree(attr);
  470. if (sb_page)
  471. page_cache_release(sb_page);
  472. }
  473. static void bitmap_stop_daemons(struct bitmap *bitmap);
  474. /* dequeue the next item in a page list -- don't call from irq context */
  475. static struct page_list *dequeue_page(struct bitmap *bitmap,
  476. struct list_head *head)
  477. {
  478. struct page_list *item = NULL;
  479. spin_lock(&bitmap->write_lock);
  480. if (list_empty(head))
  481. goto out;
  482. item = list_entry(head->prev, struct page_list, list);
  483. list_del(head->prev);
  484. out:
  485. spin_unlock(&bitmap->write_lock);
  486. return item;
  487. }
  488. static void drain_write_queues(struct bitmap *bitmap)
  489. {
  490. struct list_head *queues[] = { &bitmap->complete_pages, NULL };
  491. struct list_head *head;
  492. struct page_list *item;
  493. int i;
  494. for (i = 0; queues[i]; i++) {
  495. head = queues[i];
  496. while ((item = dequeue_page(bitmap, head))) {
  497. page_cache_release(item->page);
  498. mempool_free(item, bitmap->write_pool);
  499. }
  500. }
  501. spin_lock(&bitmap->write_lock);
  502. bitmap->writes_pending = 0; /* make sure waiters continue */
  503. wake_up(&bitmap->write_wait);
  504. spin_unlock(&bitmap->write_lock);
  505. }
  506. static void bitmap_file_put(struct bitmap *bitmap)
  507. {
  508. struct file *file;
  509. struct inode *inode;
  510. unsigned long flags;
  511. spin_lock_irqsave(&bitmap->lock, flags);
  512. file = bitmap->file;
  513. bitmap->file = NULL;
  514. spin_unlock_irqrestore(&bitmap->lock, flags);
  515. bitmap_stop_daemons(bitmap);
  516. drain_write_queues(bitmap);
  517. bitmap_file_unmap(bitmap);
  518. if (file) {
  519. inode = file->f_mapping->host;
  520. spin_lock(&inode->i_lock);
  521. atomic_set(&inode->i_writecount, 1); /* allow writes again */
  522. spin_unlock(&inode->i_lock);
  523. fput(file);
  524. }
  525. }
  526. /*
  527. * bitmap_file_kick - if an error occurs while manipulating the bitmap file
  528. * then it is no longer reliable, so we stop using it and we mark the file
  529. * as failed in the superblock
  530. */
  531. static void bitmap_file_kick(struct bitmap *bitmap)
  532. {
  533. char *path, *ptr = NULL;
  534. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET);
  535. bitmap_update_sb(bitmap);
  536. path = kmalloc(PAGE_SIZE, GFP_KERNEL);
  537. if (path)
  538. ptr = file_path(bitmap->file, path, PAGE_SIZE);
  539. printk(KERN_ALERT "%s: kicking failed bitmap file %s from array!\n",
  540. bmname(bitmap), ptr ? ptr : "");
  541. kfree(path);
  542. bitmap_file_put(bitmap);
  543. return;
  544. }
  545. enum bitmap_page_attr {
  546. BITMAP_PAGE_DIRTY = 1, // there are set bits that need to be synced
  547. BITMAP_PAGE_CLEAN = 2, // there are bits that might need to be cleared
  548. BITMAP_PAGE_NEEDWRITE=4, // there are cleared bits that need to be synced
  549. };
  550. static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
  551. enum bitmap_page_attr attr)
  552. {
  553. bitmap->filemap_attr[page->index] |= attr;
  554. }
  555. static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
  556. enum bitmap_page_attr attr)
  557. {
  558. bitmap->filemap_attr[page->index] &= ~attr;
  559. }
  560. static inline unsigned long get_page_attr(struct bitmap *bitmap, struct page *page)
  561. {
  562. return bitmap->filemap_attr[page->index];
  563. }
  564. /*
  565. * bitmap_file_set_bit -- called before performing a write to the md device
  566. * to set (and eventually sync) a particular bit in the bitmap file
  567. *
  568. * we set the bit immediately, then we record the page number so that
  569. * when an unplug occurs, we can flush the dirty pages out to disk
  570. */
  571. static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
  572. {
  573. unsigned long bit;
  574. struct page *page;
  575. void *kaddr;
  576. unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
  577. if (!bitmap->file || !bitmap->filemap) {
  578. return;
  579. }
  580. page = filemap_get_page(bitmap, chunk);
  581. bit = file_page_offset(chunk);
  582. /* make sure the page stays cached until it gets written out */
  583. if (! (get_page_attr(bitmap, page) & BITMAP_PAGE_DIRTY))
  584. page_cache_get(page);
  585. /* set the bit */
  586. kaddr = kmap_atomic(page, KM_USER0);
  587. set_bit(bit, kaddr);
  588. kunmap_atomic(kaddr, KM_USER0);
  589. PRINTK("set file bit %lu page %lu\n", bit, page->index);
  590. /* record page number so it gets flushed to disk when unplug occurs */
  591. set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  592. }
  593. /* this gets called when the md device is ready to unplug its underlying
  594. * (slave) device queues -- before we let any writes go down, we need to
  595. * sync the dirty pages of the bitmap file to disk */
  596. int bitmap_unplug(struct bitmap *bitmap)
  597. {
  598. unsigned long i, attr, flags;
  599. struct page *page;
  600. int wait = 0;
  601. if (!bitmap)
  602. return 0;
  603. /* look at each page to see if there are any set bits that need to be
  604. * flushed out to disk */
  605. for (i = 0; i < bitmap->file_pages; i++) {
  606. spin_lock_irqsave(&bitmap->lock, flags);
  607. if (!bitmap->file || !bitmap->filemap) {
  608. spin_unlock_irqrestore(&bitmap->lock, flags);
  609. return 0;
  610. }
  611. page = bitmap->filemap[i];
  612. attr = get_page_attr(bitmap, page);
  613. clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  614. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  615. if ((attr & BITMAP_PAGE_DIRTY))
  616. wait = 1;
  617. spin_unlock_irqrestore(&bitmap->lock, flags);
  618. if (attr & (BITMAP_PAGE_DIRTY | BITMAP_PAGE_NEEDWRITE))
  619. write_page(page, 0);
  620. }
  621. if (wait) { /* if any writes were performed, we need to wait on them */
  622. spin_lock_irq(&bitmap->write_lock);
  623. wait_event_lock_irq(bitmap->write_wait,
  624. bitmap->writes_pending == 0, bitmap->write_lock,
  625. wake_up_process(bitmap->writeback_daemon->tsk));
  626. spin_unlock_irq(&bitmap->write_lock);
  627. }
  628. return 0;
  629. }
  630. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset,
  631. unsigned long sectors, int in_sync);
  632. /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
  633. * the in-memory bitmap from the on-disk bitmap -- also, sets up the
  634. * memory mapping of the bitmap file
  635. * Special cases:
  636. * if there's no bitmap file, or if the bitmap file had been
  637. * previously kicked from the array, we mark all the bits as
  638. * 1's in order to cause a full resync.
  639. */
  640. static int bitmap_init_from_disk(struct bitmap *bitmap, int in_sync)
  641. {
  642. unsigned long i, chunks, index, oldindex, bit;
  643. struct page *page = NULL, *oldpage = NULL;
  644. unsigned long num_pages, bit_cnt = 0;
  645. struct file *file;
  646. unsigned long bytes, offset, dummy;
  647. int outofdate;
  648. int ret = -ENOSPC;
  649. chunks = bitmap->chunks;
  650. file = bitmap->file;
  651. BUG_ON(!file);
  652. #if INJECT_FAULTS_3
  653. outofdate = 1;
  654. #else
  655. outofdate = bitmap->flags & BITMAP_STALE;
  656. #endif
  657. if (outofdate)
  658. printk(KERN_INFO "%s: bitmap file is out of date, doing full "
  659. "recovery\n", bmname(bitmap));
  660. bytes = (chunks + 7) / 8;
  661. num_pages = (bytes + sizeof(bitmap_super_t) + PAGE_SIZE - 1) / PAGE_SIZE;
  662. if (i_size_read(file->f_mapping->host) < bytes + sizeof(bitmap_super_t)) {
  663. printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
  664. bmname(bitmap),
  665. (unsigned long) i_size_read(file->f_mapping->host),
  666. bytes + sizeof(bitmap_super_t));
  667. goto out;
  668. }
  669. ret = -ENOMEM;
  670. bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  671. if (!bitmap->filemap)
  672. goto out;
  673. bitmap->filemap_attr = kmalloc(sizeof(long) * num_pages, GFP_KERNEL);
  674. if (!bitmap->filemap_attr)
  675. goto out;
  676. memset(bitmap->filemap_attr, 0, sizeof(long) * num_pages);
  677. oldindex = ~0L;
  678. for (i = 0; i < chunks; i++) {
  679. index = file_page_index(i);
  680. bit = file_page_offset(i);
  681. if (index != oldindex) { /* this is a new page, read it in */
  682. /* unmap the old page, we're done with it */
  683. if (oldpage != NULL)
  684. kunmap(oldpage);
  685. if (index == 0) {
  686. /*
  687. * if we're here then the superblock page
  688. * contains some bits (PAGE_SIZE != sizeof sb)
  689. * we've already read it in, so just use it
  690. */
  691. page = bitmap->sb_page;
  692. offset = sizeof(bitmap_super_t);
  693. } else {
  694. page = read_page(file, index, &dummy);
  695. if (IS_ERR(page)) { /* read error */
  696. ret = PTR_ERR(page);
  697. goto out;
  698. }
  699. offset = 0;
  700. }
  701. oldindex = index;
  702. oldpage = page;
  703. kmap(page);
  704. if (outofdate) {
  705. /*
  706. * if bitmap is out of date, dirty the
  707. * whole page and write it out
  708. */
  709. memset(page_address(page) + offset, 0xff,
  710. PAGE_SIZE - offset);
  711. ret = write_page(page, 1);
  712. if (ret) {
  713. kunmap(page);
  714. /* release, page not in filemap yet */
  715. page_cache_release(page);
  716. goto out;
  717. }
  718. }
  719. bitmap->filemap[bitmap->file_pages++] = page;
  720. }
  721. if (test_bit(bit, page_address(page))) {
  722. /* if the disk bit is set, set the memory bit */
  723. bitmap_set_memory_bits(bitmap,
  724. i << CHUNK_BLOCK_SHIFT(bitmap), 1, in_sync);
  725. bit_cnt++;
  726. }
  727. }
  728. /* everything went OK */
  729. ret = 0;
  730. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
  731. if (page) /* unmap the last page */
  732. kunmap(page);
  733. if (bit_cnt) { /* Kick recovery if any bits were set */
  734. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  735. md_wakeup_thread(bitmap->mddev->thread);
  736. }
  737. out:
  738. printk(KERN_INFO "%s: bitmap initialized from disk: "
  739. "read %lu/%lu pages, set %lu bits, status: %d\n",
  740. bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, ret);
  741. return ret;
  742. }
  743. static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
  744. {
  745. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  746. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  747. bitmap->bp[page].count += inc;
  748. /*
  749. if (page == 0) printk("count page 0, offset %llu: %d gives %d\n",
  750. (unsigned long long)offset, inc, bitmap->bp[page].count);
  751. */
  752. bitmap_checkfree(bitmap, page);
  753. }
  754. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  755. sector_t offset, int *blocks,
  756. int create);
  757. /*
  758. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  759. * out to disk
  760. */
  761. int bitmap_daemon_work(struct bitmap *bitmap)
  762. {
  763. unsigned long bit, j;
  764. unsigned long flags;
  765. struct page *page = NULL, *lastpage = NULL;
  766. int err = 0;
  767. int blocks;
  768. int attr;
  769. if (bitmap == NULL)
  770. return 0;
  771. if (time_before(jiffies, bitmap->daemon_lastrun + bitmap->daemon_sleep*HZ))
  772. return 0;
  773. bitmap->daemon_lastrun = jiffies;
  774. for (j = 0; j < bitmap->chunks; j++) {
  775. bitmap_counter_t *bmc;
  776. spin_lock_irqsave(&bitmap->lock, flags);
  777. if (!bitmap->file || !bitmap->filemap) {
  778. /* error or shutdown */
  779. spin_unlock_irqrestore(&bitmap->lock, flags);
  780. break;
  781. }
  782. page = filemap_get_page(bitmap, j);
  783. /* skip this page unless it's marked as needing cleaning */
  784. if (!((attr=get_page_attr(bitmap, page)) & BITMAP_PAGE_CLEAN)) {
  785. if (attr & BITMAP_PAGE_NEEDWRITE) {
  786. page_cache_get(page);
  787. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  788. }
  789. spin_unlock_irqrestore(&bitmap->lock, flags);
  790. if (attr & BITMAP_PAGE_NEEDWRITE) {
  791. if (write_page(page, 0))
  792. bitmap_file_kick(bitmap);
  793. page_cache_release(page);
  794. }
  795. continue;
  796. }
  797. bit = file_page_offset(j);
  798. if (page != lastpage) {
  799. /* grab the new page, sync and release the old */
  800. page_cache_get(page);
  801. if (lastpage != NULL) {
  802. if (get_page_attr(bitmap, lastpage) & BITMAP_PAGE_NEEDWRITE) {
  803. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  804. spin_unlock_irqrestore(&bitmap->lock, flags);
  805. write_page(lastpage, 0);
  806. } else {
  807. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  808. spin_unlock_irqrestore(&bitmap->lock, flags);
  809. }
  810. kunmap(lastpage);
  811. page_cache_release(lastpage);
  812. if (err)
  813. bitmap_file_kick(bitmap);
  814. } else
  815. spin_unlock_irqrestore(&bitmap->lock, flags);
  816. lastpage = page;
  817. kmap(page);
  818. /*
  819. printk("bitmap clean at page %lu\n", j);
  820. */
  821. spin_lock_irqsave(&bitmap->lock, flags);
  822. clear_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  823. }
  824. bmc = bitmap_get_counter(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
  825. &blocks, 0);
  826. if (bmc) {
  827. /*
  828. if (j < 100) printk("bitmap: j=%lu, *bmc = 0x%x\n", j, *bmc);
  829. */
  830. if (*bmc == 2) {
  831. *bmc=1; /* maybe clear the bit next time */
  832. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  833. } else if (*bmc == 1) {
  834. /* we can clear the bit */
  835. *bmc = 0;
  836. bitmap_count_page(bitmap, j << CHUNK_BLOCK_SHIFT(bitmap),
  837. -1);
  838. /* clear the bit */
  839. clear_bit(bit, page_address(page));
  840. }
  841. }
  842. spin_unlock_irqrestore(&bitmap->lock, flags);
  843. }
  844. /* now sync the final page */
  845. if (lastpage != NULL) {
  846. kunmap(lastpage);
  847. spin_lock_irqsave(&bitmap->lock, flags);
  848. if (get_page_attr(bitmap, lastpage) &BITMAP_PAGE_NEEDWRITE) {
  849. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  850. spin_unlock_irqrestore(&bitmap->lock, flags);
  851. write_page(lastpage, 0);
  852. } else {
  853. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  854. spin_unlock_irqrestore(&bitmap->lock, flags);
  855. }
  856. page_cache_release(lastpage);
  857. }
  858. return err;
  859. }
  860. static void daemon_exit(struct bitmap *bitmap, mdk_thread_t **daemon)
  861. {
  862. mdk_thread_t *dmn;
  863. unsigned long flags;
  864. /* if no one is waiting on us, we'll free the md thread struct
  865. * and exit, otherwise we let the waiter clean things up */
  866. spin_lock_irqsave(&bitmap->lock, flags);
  867. if ((dmn = *daemon)) { /* no one is waiting, cleanup and exit */
  868. *daemon = NULL;
  869. spin_unlock_irqrestore(&bitmap->lock, flags);
  870. kfree(dmn);
  871. complete_and_exit(NULL, 0); /* do_exit not exported */
  872. }
  873. spin_unlock_irqrestore(&bitmap->lock, flags);
  874. }
  875. static void bitmap_writeback_daemon(mddev_t *mddev)
  876. {
  877. struct bitmap *bitmap = mddev->bitmap;
  878. struct page *page;
  879. struct page_list *item;
  880. int err = 0;
  881. while (1) {
  882. PRINTK("%s: bitmap writeback daemon waiting...\n", bmname(bitmap));
  883. down_interruptible(&bitmap->write_done);
  884. if (signal_pending(current)) {
  885. printk(KERN_INFO
  886. "%s: bitmap writeback daemon got signal, exiting...\n",
  887. bmname(bitmap));
  888. break;
  889. }
  890. PRINTK("%s: bitmap writeback daemon woke up...\n", bmname(bitmap));
  891. /* wait on bitmap page writebacks */
  892. while ((item = dequeue_page(bitmap, &bitmap->complete_pages))) {
  893. page = item->page;
  894. mempool_free(item, bitmap->write_pool);
  895. PRINTK("wait on page writeback: %p %lu\n", page, bitmap->writes_pending);
  896. wait_on_page_writeback(page);
  897. PRINTK("finished page writeback: %p %lu\n", page, bitmap->writes_pending);
  898. spin_lock(&bitmap->write_lock);
  899. if (!--bitmap->writes_pending)
  900. wake_up(&bitmap->write_wait);
  901. spin_unlock(&bitmap->write_lock);
  902. err = PageError(page);
  903. page_cache_release(page);
  904. if (err) {
  905. printk(KERN_WARNING "%s: bitmap file writeback "
  906. "failed (page %lu): %d\n",
  907. bmname(bitmap), page->index, err);
  908. bitmap_file_kick(bitmap);
  909. goto out;
  910. }
  911. }
  912. }
  913. out:
  914. if (err) {
  915. printk(KERN_INFO "%s: bitmap writeback daemon exiting (%d)\n",
  916. bmname(bitmap), err);
  917. daemon_exit(bitmap, &bitmap->writeback_daemon);
  918. }
  919. return;
  920. }
  921. static int bitmap_start_daemon(struct bitmap *bitmap, mdk_thread_t **ptr,
  922. void (*func)(mddev_t *), char *name)
  923. {
  924. mdk_thread_t *daemon;
  925. unsigned long flags;
  926. char namebuf[32];
  927. spin_lock_irqsave(&bitmap->lock, flags);
  928. *ptr = NULL;
  929. if (!bitmap->file) /* no need for daemon if there's no backing file */
  930. goto out_unlock;
  931. spin_unlock_irqrestore(&bitmap->lock, flags);
  932. #if INJECT_FATAL_FAULT_2
  933. daemon = NULL;
  934. #else
  935. sprintf(namebuf, "%%s_%s", name);
  936. daemon = md_register_thread(func, bitmap->mddev, namebuf);
  937. #endif
  938. if (!daemon) {
  939. printk(KERN_ERR "%s: failed to start bitmap daemon\n",
  940. bmname(bitmap));
  941. return -ECHILD;
  942. }
  943. spin_lock_irqsave(&bitmap->lock, flags);
  944. *ptr = daemon;
  945. md_wakeup_thread(daemon); /* start it running */
  946. PRINTK("%s: %s daemon (pid %d) started...\n",
  947. bmname(bitmap), name, daemon->tsk->pid);
  948. out_unlock:
  949. spin_unlock_irqrestore(&bitmap->lock, flags);
  950. return 0;
  951. }
  952. static int bitmap_start_daemons(struct bitmap *bitmap)
  953. {
  954. int err = bitmap_start_daemon(bitmap, &bitmap->writeback_daemon,
  955. bitmap_writeback_daemon, "bitmap_wb");
  956. return err;
  957. }
  958. static void bitmap_stop_daemon(struct bitmap *bitmap, mdk_thread_t **ptr)
  959. {
  960. mdk_thread_t *daemon;
  961. unsigned long flags;
  962. spin_lock_irqsave(&bitmap->lock, flags);
  963. daemon = *ptr;
  964. *ptr = NULL;
  965. spin_unlock_irqrestore(&bitmap->lock, flags);
  966. if (daemon)
  967. md_unregister_thread(daemon); /* destroy the thread */
  968. }
  969. static void bitmap_stop_daemons(struct bitmap *bitmap)
  970. {
  971. /* the daemons can't stop themselves... they'll just exit instead... */
  972. if (bitmap->writeback_daemon &&
  973. current->pid != bitmap->writeback_daemon->tsk->pid)
  974. bitmap_stop_daemon(bitmap, &bitmap->writeback_daemon);
  975. }
  976. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  977. sector_t offset, int *blocks,
  978. int create)
  979. {
  980. /* If 'create', we might release the lock and reclaim it.
  981. * The lock must have been taken with interrupts enabled.
  982. * If !create, we don't release the lock.
  983. */
  984. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  985. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  986. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  987. sector_t csize;
  988. if (bitmap_checkpage(bitmap, page, create) < 0) {
  989. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  990. *blocks = csize - (offset & (csize- 1));
  991. return NULL;
  992. }
  993. /* now locked ... */
  994. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  995. /* should we use the first or second counter field
  996. * of the hijacked pointer? */
  997. int hi = (pageoff > PAGE_COUNTER_MASK);
  998. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
  999. PAGE_COUNTER_SHIFT - 1);
  1000. *blocks = csize - (offset & (csize- 1));
  1001. return &((bitmap_counter_t *)
  1002. &bitmap->bp[page].map)[hi];
  1003. } else { /* page is allocated */
  1004. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1005. *blocks = csize - (offset & (csize- 1));
  1006. return (bitmap_counter_t *)
  1007. &(bitmap->bp[page].map[pageoff]);
  1008. }
  1009. }
  1010. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors)
  1011. {
  1012. if (!bitmap) return 0;
  1013. while (sectors) {
  1014. int blocks;
  1015. bitmap_counter_t *bmc;
  1016. spin_lock_irq(&bitmap->lock);
  1017. bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
  1018. if (!bmc) {
  1019. spin_unlock_irq(&bitmap->lock);
  1020. return 0;
  1021. }
  1022. switch(*bmc) {
  1023. case 0:
  1024. bitmap_file_set_bit(bitmap, offset);
  1025. bitmap_count_page(bitmap,offset, 1);
  1026. blk_plug_device(bitmap->mddev->queue);
  1027. /* fall through */
  1028. case 1:
  1029. *bmc = 2;
  1030. }
  1031. if ((*bmc & COUNTER_MAX) == COUNTER_MAX) BUG();
  1032. (*bmc)++;
  1033. spin_unlock_irq(&bitmap->lock);
  1034. offset += blocks;
  1035. if (sectors > blocks)
  1036. sectors -= blocks;
  1037. else sectors = 0;
  1038. }
  1039. return 0;
  1040. }
  1041. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1042. int success)
  1043. {
  1044. if (!bitmap) return;
  1045. while (sectors) {
  1046. int blocks;
  1047. unsigned long flags;
  1048. bitmap_counter_t *bmc;
  1049. spin_lock_irqsave(&bitmap->lock, flags);
  1050. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1051. if (!bmc) {
  1052. spin_unlock_irqrestore(&bitmap->lock, flags);
  1053. return;
  1054. }
  1055. if (!success && ! (*bmc & NEEDED_MASK))
  1056. *bmc |= NEEDED_MASK;
  1057. (*bmc)--;
  1058. if (*bmc <= 2) {
  1059. set_page_attr(bitmap,
  1060. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1061. BITMAP_PAGE_CLEAN);
  1062. }
  1063. spin_unlock_irqrestore(&bitmap->lock, flags);
  1064. offset += blocks;
  1065. if (sectors > blocks)
  1066. sectors -= blocks;
  1067. else sectors = 0;
  1068. }
  1069. }
  1070. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, int *blocks)
  1071. {
  1072. bitmap_counter_t *bmc;
  1073. int rv;
  1074. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1075. *blocks = 1024;
  1076. return 1; /* always resync if no bitmap */
  1077. }
  1078. spin_lock_irq(&bitmap->lock);
  1079. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1080. rv = 0;
  1081. if (bmc) {
  1082. /* locked */
  1083. if (RESYNC(*bmc))
  1084. rv = 1;
  1085. else if (NEEDED(*bmc)) {
  1086. rv = 1;
  1087. *bmc |= RESYNC_MASK;
  1088. *bmc &= ~NEEDED_MASK;
  1089. }
  1090. }
  1091. spin_unlock_irq(&bitmap->lock);
  1092. return rv;
  1093. }
  1094. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, int *blocks, int aborted)
  1095. {
  1096. bitmap_counter_t *bmc;
  1097. unsigned long flags;
  1098. /*
  1099. if (offset == 0) printk("bitmap_end_sync 0 (%d)\n", aborted);
  1100. */ if (bitmap == NULL) {
  1101. *blocks = 1024;
  1102. return;
  1103. }
  1104. spin_lock_irqsave(&bitmap->lock, flags);
  1105. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1106. if (bmc == NULL)
  1107. goto unlock;
  1108. /* locked */
  1109. /*
  1110. if (offset == 0) printk("bitmap_end sync found 0x%x, blocks %d\n", *bmc, *blocks);
  1111. */
  1112. if (RESYNC(*bmc)) {
  1113. *bmc &= ~RESYNC_MASK;
  1114. if (!NEEDED(*bmc) && aborted)
  1115. *bmc |= NEEDED_MASK;
  1116. else {
  1117. if (*bmc <= 2) {
  1118. set_page_attr(bitmap,
  1119. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1120. BITMAP_PAGE_CLEAN);
  1121. }
  1122. }
  1123. }
  1124. unlock:
  1125. spin_unlock_irqrestore(&bitmap->lock, flags);
  1126. }
  1127. void bitmap_close_sync(struct bitmap *bitmap)
  1128. {
  1129. /* Sync has finished, and any bitmap chunks that weren't synced
  1130. * properly have been aborted. It remains to us to clear the
  1131. * RESYNC bit wherever it is still on
  1132. */
  1133. sector_t sector = 0;
  1134. int blocks;
  1135. if (!bitmap) return;
  1136. while (sector < bitmap->mddev->resync_max_sectors) {
  1137. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1138. /*
  1139. if (sector < 500) printk("bitmap_close_sync: sec %llu blks %d\n",
  1140. (unsigned long long)sector, blocks);
  1141. */ sector += blocks;
  1142. }
  1143. }
  1144. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset,
  1145. unsigned long sectors, int in_sync)
  1146. {
  1147. /* For each chunk covered by any of these sectors, set the
  1148. * counter to 1 and set resync_needed unless in_sync. They should all
  1149. * be 0 at this point
  1150. */
  1151. while (sectors) {
  1152. int secs;
  1153. bitmap_counter_t *bmc;
  1154. spin_lock_irq(&bitmap->lock);
  1155. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1156. if (!bmc) {
  1157. spin_unlock_irq(&bitmap->lock);
  1158. return;
  1159. }
  1160. if (! *bmc) {
  1161. struct page *page;
  1162. *bmc = 1 | (in_sync? 0 : NEEDED_MASK);
  1163. bitmap_count_page(bitmap, offset, 1);
  1164. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1165. set_page_attr(bitmap, page, BITMAP_PAGE_CLEAN);
  1166. }
  1167. spin_unlock_irq(&bitmap->lock);
  1168. if (sectors > secs)
  1169. sectors -= secs;
  1170. else
  1171. sectors = 0;
  1172. }
  1173. }
  1174. /* dirty the entire bitmap */
  1175. int bitmap_setallbits(struct bitmap *bitmap)
  1176. {
  1177. unsigned long flags;
  1178. unsigned long j;
  1179. /* dirty the in-memory bitmap */
  1180. bitmap_set_memory_bits(bitmap, 0, bitmap->chunks << CHUNK_BLOCK_SHIFT(bitmap), 1);
  1181. /* dirty the bitmap file */
  1182. for (j = 0; j < bitmap->file_pages; j++) {
  1183. struct page *page = bitmap->filemap[j];
  1184. spin_lock_irqsave(&bitmap->lock, flags);
  1185. page_cache_get(page);
  1186. spin_unlock_irqrestore(&bitmap->lock, flags);
  1187. memset(kmap(page), 0xff, PAGE_SIZE);
  1188. kunmap(page);
  1189. write_page(page, 0);
  1190. }
  1191. return 0;
  1192. }
  1193. /*
  1194. * free memory that was allocated
  1195. */
  1196. void bitmap_destroy(mddev_t *mddev)
  1197. {
  1198. unsigned long k, pages;
  1199. struct bitmap_page *bp;
  1200. struct bitmap *bitmap = mddev->bitmap;
  1201. if (!bitmap) /* there was no bitmap */
  1202. return;
  1203. mddev->bitmap = NULL; /* disconnect from the md device */
  1204. /* release the bitmap file and kill the daemon */
  1205. bitmap_file_put(bitmap);
  1206. bp = bitmap->bp;
  1207. pages = bitmap->pages;
  1208. /* free all allocated memory */
  1209. mempool_destroy(bitmap->write_pool);
  1210. if (bp) /* deallocate the page memory */
  1211. for (k = 0; k < pages; k++)
  1212. if (bp[k].map && !bp[k].hijacked)
  1213. kfree(bp[k].map);
  1214. kfree(bp);
  1215. kfree(bitmap);
  1216. }
  1217. /*
  1218. * initialize the bitmap structure
  1219. * if this returns an error, bitmap_destroy must be called to do clean up
  1220. */
  1221. int bitmap_create(mddev_t *mddev)
  1222. {
  1223. struct bitmap *bitmap;
  1224. unsigned long blocks = mddev->resync_max_sectors;
  1225. unsigned long chunks;
  1226. unsigned long pages;
  1227. struct file *file = mddev->bitmap_file;
  1228. int err;
  1229. BUG_ON(sizeof(bitmap_super_t) != 256);
  1230. if (!file) /* bitmap disabled, nothing to do */
  1231. return 0;
  1232. bitmap = kmalloc(sizeof(*bitmap), GFP_KERNEL);
  1233. if (!bitmap)
  1234. return -ENOMEM;
  1235. memset(bitmap, 0, sizeof(*bitmap));
  1236. spin_lock_init(&bitmap->lock);
  1237. bitmap->mddev = mddev;
  1238. mddev->bitmap = bitmap;
  1239. spin_lock_init(&bitmap->write_lock);
  1240. init_MUTEX_LOCKED(&bitmap->write_done);
  1241. INIT_LIST_HEAD(&bitmap->complete_pages);
  1242. init_waitqueue_head(&bitmap->write_wait);
  1243. bitmap->write_pool = mempool_create(WRITE_POOL_SIZE, write_pool_alloc,
  1244. write_pool_free, NULL);
  1245. if (!bitmap->write_pool)
  1246. return -ENOMEM;
  1247. bitmap->file = file;
  1248. get_file(file);
  1249. /* read superblock from bitmap file (this sets bitmap->chunksize) */
  1250. err = bitmap_read_sb(bitmap);
  1251. if (err)
  1252. return err;
  1253. bitmap->chunkshift = find_first_bit(&bitmap->chunksize,
  1254. sizeof(bitmap->chunksize));
  1255. /* now that chunksize and chunkshift are set, we can use these macros */
  1256. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) /
  1257. CHUNK_BLOCK_RATIO(bitmap);
  1258. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1259. BUG_ON(!pages);
  1260. bitmap->chunks = chunks;
  1261. bitmap->pages = pages;
  1262. bitmap->missing_pages = pages;
  1263. bitmap->counter_bits = COUNTER_BITS;
  1264. bitmap->syncchunk = ~0UL;
  1265. #if INJECT_FATAL_FAULT_1
  1266. bitmap->bp = NULL;
  1267. #else
  1268. bitmap->bp = kmalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1269. #endif
  1270. if (!bitmap->bp)
  1271. return -ENOMEM;
  1272. memset(bitmap->bp, 0, pages * sizeof(*bitmap->bp));
  1273. bitmap->flags |= BITMAP_ACTIVE;
  1274. /* now that we have some pages available, initialize the in-memory
  1275. * bitmap from the on-disk bitmap */
  1276. err = bitmap_init_from_disk(bitmap, mddev->recovery_cp == MaxSector);
  1277. if (err)
  1278. return err;
  1279. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1280. pages, bmname(bitmap));
  1281. /* kick off the bitmap daemons */
  1282. err = bitmap_start_daemons(bitmap);
  1283. if (err)
  1284. return err;
  1285. return bitmap_update_sb(bitmap);
  1286. }
  1287. /* the bitmap API -- for raid personalities */
  1288. EXPORT_SYMBOL(bitmap_startwrite);
  1289. EXPORT_SYMBOL(bitmap_endwrite);
  1290. EXPORT_SYMBOL(bitmap_start_sync);
  1291. EXPORT_SYMBOL(bitmap_end_sync);
  1292. EXPORT_SYMBOL(bitmap_unplug);
  1293. EXPORT_SYMBOL(bitmap_close_sync);
  1294. EXPORT_SYMBOL(bitmap_daemon_work);