bitmap.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148
  1. /*
  2. * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
  3. *
  4. * bitmap_create - sets up the bitmap structure
  5. * bitmap_destroy - destroys the bitmap structure
  6. *
  7. * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
  8. * - added disk storage for bitmap
  9. * - changes to allow various bitmap chunk sizes
  10. */
  11. /*
  12. * Still to do:
  13. *
  14. * flush after percent set rather than just time based. (maybe both).
  15. */
  16. #include <linux/blkdev.h>
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/slab.h>
  20. #include <linux/init.h>
  21. #include <linux/timer.h>
  22. #include <linux/sched.h>
  23. #include <linux/list.h>
  24. #include <linux/file.h>
  25. #include <linux/mount.h>
  26. #include <linux/buffer_head.h>
  27. #include "md.h"
  28. #include "bitmap.h"
  29. /* debug macros */
  30. #define DEBUG 0
  31. #if DEBUG
  32. /* these are for debugging purposes only! */
  33. /* define one and only one of these */
  34. #define INJECT_FAULTS_1 0 /* cause bitmap_alloc_page to fail always */
  35. #define INJECT_FAULTS_2 0 /* cause bitmap file to be kicked when first bit set*/
  36. #define INJECT_FAULTS_3 0 /* treat bitmap file as kicked at init time */
  37. #define INJECT_FAULTS_4 0 /* undef */
  38. #define INJECT_FAULTS_5 0 /* undef */
  39. #define INJECT_FAULTS_6 0
  40. /* if these are defined, the driver will fail! debug only */
  41. #define INJECT_FATAL_FAULT_1 0 /* fail kmalloc, causing bitmap_create to fail */
  42. #define INJECT_FATAL_FAULT_2 0 /* undef */
  43. #define INJECT_FATAL_FAULT_3 0 /* undef */
  44. #endif
  45. static inline char *bmname(struct bitmap *bitmap)
  46. {
  47. return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
  48. }
  49. /*
  50. * just a placeholder - calls kmalloc for bitmap pages
  51. */
  52. static unsigned char *bitmap_alloc_page(struct bitmap *bitmap)
  53. {
  54. unsigned char *page;
  55. #ifdef INJECT_FAULTS_1
  56. page = NULL;
  57. #else
  58. page = kzalloc(PAGE_SIZE, GFP_NOIO);
  59. #endif
  60. if (!page)
  61. printk("%s: bitmap_alloc_page FAILED\n", bmname(bitmap));
  62. else
  63. pr_debug("%s: bitmap_alloc_page: allocated page at %p\n",
  64. bmname(bitmap), page);
  65. return page;
  66. }
  67. /*
  68. * for now just a placeholder -- just calls kfree for bitmap pages
  69. */
  70. static void bitmap_free_page(struct bitmap *bitmap, unsigned char *page)
  71. {
  72. pr_debug("%s: bitmap_free_page: free page %p\n", bmname(bitmap), page);
  73. kfree(page);
  74. }
  75. /*
  76. * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
  77. *
  78. * 1) check to see if this page is allocated, if it's not then try to alloc
  79. * 2) if the alloc fails, set the page's hijacked flag so we'll use the
  80. * page pointer directly as a counter
  81. *
  82. * if we find our page, we increment the page's refcount so that it stays
  83. * allocated while we're using it
  84. */
  85. static int bitmap_checkpage(struct bitmap *bitmap,
  86. unsigned long page, int create)
  87. __releases(bitmap->lock)
  88. __acquires(bitmap->lock)
  89. {
  90. unsigned char *mappage;
  91. if (page >= bitmap->pages) {
  92. /* This can happen if bitmap_start_sync goes beyond
  93. * End-of-device while looking for a whole page.
  94. * It is harmless.
  95. */
  96. return -EINVAL;
  97. }
  98. if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
  99. return 0;
  100. if (bitmap->bp[page].map) /* page is already allocated, just return */
  101. return 0;
  102. if (!create)
  103. return -ENOENT;
  104. /* this page has not been allocated yet */
  105. spin_unlock_irq(&bitmap->lock);
  106. mappage = bitmap_alloc_page(bitmap);
  107. spin_lock_irq(&bitmap->lock);
  108. if (mappage == NULL) {
  109. pr_debug("%s: bitmap map page allocation failed, hijacking\n",
  110. bmname(bitmap));
  111. /* failed - set the hijacked flag so that we can use the
  112. * pointer as a counter */
  113. if (!bitmap->bp[page].map)
  114. bitmap->bp[page].hijacked = 1;
  115. } else if (bitmap->bp[page].map ||
  116. bitmap->bp[page].hijacked) {
  117. /* somebody beat us to getting the page */
  118. bitmap_free_page(bitmap, mappage);
  119. return 0;
  120. } else {
  121. /* no page was in place and we have one, so install it */
  122. bitmap->bp[page].map = mappage;
  123. bitmap->missing_pages--;
  124. }
  125. return 0;
  126. }
  127. /* if page is completely empty, put it back on the free list, or dealloc it */
  128. /* if page was hijacked, unmark the flag so it might get alloced next time */
  129. /* Note: lock should be held when calling this */
  130. static void bitmap_checkfree(struct bitmap *bitmap, unsigned long page)
  131. {
  132. char *ptr;
  133. if (bitmap->bp[page].count) /* page is still busy */
  134. return;
  135. /* page is no longer in use, it can be released */
  136. if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
  137. bitmap->bp[page].hijacked = 0;
  138. bitmap->bp[page].map = NULL;
  139. } else {
  140. /* normal case, free the page */
  141. ptr = bitmap->bp[page].map;
  142. bitmap->bp[page].map = NULL;
  143. bitmap->missing_pages++;
  144. bitmap_free_page(bitmap, ptr);
  145. }
  146. }
  147. /*
  148. * bitmap file handling - read and write the bitmap file and its superblock
  149. */
  150. /*
  151. * basic page I/O operations
  152. */
  153. /* IO operations when bitmap is stored near all superblocks */
  154. static struct page *read_sb_page(mddev_t *mddev, loff_t offset,
  155. struct page *page,
  156. unsigned long index, int size)
  157. {
  158. /* choose a good rdev and read the page from there */
  159. struct md_rdev *rdev;
  160. sector_t target;
  161. int did_alloc = 0;
  162. if (!page) {
  163. page = alloc_page(GFP_KERNEL);
  164. if (!page)
  165. return ERR_PTR(-ENOMEM);
  166. did_alloc = 1;
  167. }
  168. list_for_each_entry(rdev, &mddev->disks, same_set) {
  169. if (! test_bit(In_sync, &rdev->flags)
  170. || test_bit(Faulty, &rdev->flags))
  171. continue;
  172. target = offset + index * (PAGE_SIZE/512);
  173. if (sync_page_io(rdev, target,
  174. roundup(size, bdev_logical_block_size(rdev->bdev)),
  175. page, READ, true)) {
  176. page->index = index;
  177. attach_page_buffers(page, NULL); /* so that free_buffer will
  178. * quietly no-op */
  179. return page;
  180. }
  181. }
  182. if (did_alloc)
  183. put_page(page);
  184. return ERR_PTR(-EIO);
  185. }
  186. static struct md_rdev *next_active_rdev(struct md_rdev *rdev, mddev_t *mddev)
  187. {
  188. /* Iterate the disks of an mddev, using rcu to protect access to the
  189. * linked list, and raising the refcount of devices we return to ensure
  190. * they don't disappear while in use.
  191. * As devices are only added or removed when raid_disk is < 0 and
  192. * nr_pending is 0 and In_sync is clear, the entries we return will
  193. * still be in the same position on the list when we re-enter
  194. * list_for_each_continue_rcu.
  195. */
  196. struct list_head *pos;
  197. rcu_read_lock();
  198. if (rdev == NULL)
  199. /* start at the beginning */
  200. pos = &mddev->disks;
  201. else {
  202. /* release the previous rdev and start from there. */
  203. rdev_dec_pending(rdev, mddev);
  204. pos = &rdev->same_set;
  205. }
  206. list_for_each_continue_rcu(pos, &mddev->disks) {
  207. rdev = list_entry(pos, struct md_rdev, same_set);
  208. if (rdev->raid_disk >= 0 &&
  209. !test_bit(Faulty, &rdev->flags)) {
  210. /* this is a usable devices */
  211. atomic_inc(&rdev->nr_pending);
  212. rcu_read_unlock();
  213. return rdev;
  214. }
  215. }
  216. rcu_read_unlock();
  217. return NULL;
  218. }
  219. static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
  220. {
  221. struct md_rdev *rdev = NULL;
  222. struct block_device *bdev;
  223. mddev_t *mddev = bitmap->mddev;
  224. while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
  225. int size = PAGE_SIZE;
  226. loff_t offset = mddev->bitmap_info.offset;
  227. bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
  228. if (page->index == bitmap->file_pages-1)
  229. size = roundup(bitmap->last_page_size,
  230. bdev_logical_block_size(bdev));
  231. /* Just make sure we aren't corrupting data or
  232. * metadata
  233. */
  234. if (mddev->external) {
  235. /* Bitmap could be anywhere. */
  236. if (rdev->sb_start + offset + (page->index
  237. * (PAGE_SIZE/512))
  238. > rdev->data_offset
  239. &&
  240. rdev->sb_start + offset
  241. < (rdev->data_offset + mddev->dev_sectors
  242. + (PAGE_SIZE/512)))
  243. goto bad_alignment;
  244. } else if (offset < 0) {
  245. /* DATA BITMAP METADATA */
  246. if (offset
  247. + (long)(page->index * (PAGE_SIZE/512))
  248. + size/512 > 0)
  249. /* bitmap runs in to metadata */
  250. goto bad_alignment;
  251. if (rdev->data_offset + mddev->dev_sectors
  252. > rdev->sb_start + offset)
  253. /* data runs in to bitmap */
  254. goto bad_alignment;
  255. } else if (rdev->sb_start < rdev->data_offset) {
  256. /* METADATA BITMAP DATA */
  257. if (rdev->sb_start
  258. + offset
  259. + page->index*(PAGE_SIZE/512) + size/512
  260. > rdev->data_offset)
  261. /* bitmap runs in to data */
  262. goto bad_alignment;
  263. } else {
  264. /* DATA METADATA BITMAP - no problems */
  265. }
  266. md_super_write(mddev, rdev,
  267. rdev->sb_start + offset
  268. + page->index * (PAGE_SIZE/512),
  269. size,
  270. page);
  271. }
  272. if (wait)
  273. md_super_wait(mddev);
  274. return 0;
  275. bad_alignment:
  276. return -EINVAL;
  277. }
  278. static void bitmap_file_kick(struct bitmap *bitmap);
  279. /*
  280. * write out a page to a file
  281. */
  282. static void write_page(struct bitmap *bitmap, struct page *page, int wait)
  283. {
  284. struct buffer_head *bh;
  285. if (bitmap->file == NULL) {
  286. switch (write_sb_page(bitmap, page, wait)) {
  287. case -EINVAL:
  288. bitmap->flags |= BITMAP_WRITE_ERROR;
  289. }
  290. } else {
  291. bh = page_buffers(page);
  292. while (bh && bh->b_blocknr) {
  293. atomic_inc(&bitmap->pending_writes);
  294. set_buffer_locked(bh);
  295. set_buffer_mapped(bh);
  296. submit_bh(WRITE | REQ_SYNC, bh);
  297. bh = bh->b_this_page;
  298. }
  299. if (wait)
  300. wait_event(bitmap->write_wait,
  301. atomic_read(&bitmap->pending_writes)==0);
  302. }
  303. if (bitmap->flags & BITMAP_WRITE_ERROR)
  304. bitmap_file_kick(bitmap);
  305. }
  306. static void end_bitmap_write(struct buffer_head *bh, int uptodate)
  307. {
  308. struct bitmap *bitmap = bh->b_private;
  309. unsigned long flags;
  310. if (!uptodate) {
  311. spin_lock_irqsave(&bitmap->lock, flags);
  312. bitmap->flags |= BITMAP_WRITE_ERROR;
  313. spin_unlock_irqrestore(&bitmap->lock, flags);
  314. }
  315. if (atomic_dec_and_test(&bitmap->pending_writes))
  316. wake_up(&bitmap->write_wait);
  317. }
  318. /* copied from buffer.c */
  319. static void
  320. __clear_page_buffers(struct page *page)
  321. {
  322. ClearPagePrivate(page);
  323. set_page_private(page, 0);
  324. page_cache_release(page);
  325. }
  326. static void free_buffers(struct page *page)
  327. {
  328. struct buffer_head *bh = page_buffers(page);
  329. while (bh) {
  330. struct buffer_head *next = bh->b_this_page;
  331. free_buffer_head(bh);
  332. bh = next;
  333. }
  334. __clear_page_buffers(page);
  335. put_page(page);
  336. }
  337. /* read a page from a file.
  338. * We both read the page, and attach buffers to the page to record the
  339. * address of each block (using bmap). These addresses will be used
  340. * to write the block later, completely bypassing the filesystem.
  341. * This usage is similar to how swap files are handled, and allows us
  342. * to write to a file with no concerns of memory allocation failing.
  343. */
  344. static struct page *read_page(struct file *file, unsigned long index,
  345. struct bitmap *bitmap,
  346. unsigned long count)
  347. {
  348. struct page *page = NULL;
  349. struct inode *inode = file->f_path.dentry->d_inode;
  350. struct buffer_head *bh;
  351. sector_t block;
  352. pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
  353. (unsigned long long)index << PAGE_SHIFT);
  354. page = alloc_page(GFP_KERNEL);
  355. if (!page)
  356. page = ERR_PTR(-ENOMEM);
  357. if (IS_ERR(page))
  358. goto out;
  359. bh = alloc_page_buffers(page, 1<<inode->i_blkbits, 0);
  360. if (!bh) {
  361. put_page(page);
  362. page = ERR_PTR(-ENOMEM);
  363. goto out;
  364. }
  365. attach_page_buffers(page, bh);
  366. block = index << (PAGE_SHIFT - inode->i_blkbits);
  367. while (bh) {
  368. if (count == 0)
  369. bh->b_blocknr = 0;
  370. else {
  371. bh->b_blocknr = bmap(inode, block);
  372. if (bh->b_blocknr == 0) {
  373. /* Cannot use this file! */
  374. free_buffers(page);
  375. page = ERR_PTR(-EINVAL);
  376. goto out;
  377. }
  378. bh->b_bdev = inode->i_sb->s_bdev;
  379. if (count < (1<<inode->i_blkbits))
  380. count = 0;
  381. else
  382. count -= (1<<inode->i_blkbits);
  383. bh->b_end_io = end_bitmap_write;
  384. bh->b_private = bitmap;
  385. atomic_inc(&bitmap->pending_writes);
  386. set_buffer_locked(bh);
  387. set_buffer_mapped(bh);
  388. submit_bh(READ, bh);
  389. }
  390. block++;
  391. bh = bh->b_this_page;
  392. }
  393. page->index = index;
  394. wait_event(bitmap->write_wait,
  395. atomic_read(&bitmap->pending_writes)==0);
  396. if (bitmap->flags & BITMAP_WRITE_ERROR) {
  397. free_buffers(page);
  398. page = ERR_PTR(-EIO);
  399. }
  400. out:
  401. if (IS_ERR(page))
  402. printk(KERN_ALERT "md: bitmap read error: (%dB @ %llu): %ld\n",
  403. (int)PAGE_SIZE,
  404. (unsigned long long)index << PAGE_SHIFT,
  405. PTR_ERR(page));
  406. return page;
  407. }
  408. /*
  409. * bitmap file superblock operations
  410. */
  411. /* update the event counter and sync the superblock to disk */
  412. void bitmap_update_sb(struct bitmap *bitmap)
  413. {
  414. bitmap_super_t *sb;
  415. unsigned long flags;
  416. if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
  417. return;
  418. if (bitmap->mddev->bitmap_info.external)
  419. return;
  420. spin_lock_irqsave(&bitmap->lock, flags);
  421. if (!bitmap->sb_page) { /* no superblock */
  422. spin_unlock_irqrestore(&bitmap->lock, flags);
  423. return;
  424. }
  425. spin_unlock_irqrestore(&bitmap->lock, flags);
  426. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  427. sb->events = cpu_to_le64(bitmap->mddev->events);
  428. if (bitmap->mddev->events < bitmap->events_cleared)
  429. /* rocking back to read-only */
  430. bitmap->events_cleared = bitmap->mddev->events;
  431. sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
  432. sb->state = cpu_to_le32(bitmap->flags);
  433. /* Just in case these have been changed via sysfs: */
  434. sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
  435. sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
  436. kunmap_atomic(sb, KM_USER0);
  437. write_page(bitmap, bitmap->sb_page, 1);
  438. }
  439. /* print out the bitmap file superblock */
  440. void bitmap_print_sb(struct bitmap *bitmap)
  441. {
  442. bitmap_super_t *sb;
  443. if (!bitmap || !bitmap->sb_page)
  444. return;
  445. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  446. printk(KERN_DEBUG "%s: bitmap file superblock:\n", bmname(bitmap));
  447. printk(KERN_DEBUG " magic: %08x\n", le32_to_cpu(sb->magic));
  448. printk(KERN_DEBUG " version: %d\n", le32_to_cpu(sb->version));
  449. printk(KERN_DEBUG " uuid: %08x.%08x.%08x.%08x\n",
  450. *(__u32 *)(sb->uuid+0),
  451. *(__u32 *)(sb->uuid+4),
  452. *(__u32 *)(sb->uuid+8),
  453. *(__u32 *)(sb->uuid+12));
  454. printk(KERN_DEBUG " events: %llu\n",
  455. (unsigned long long) le64_to_cpu(sb->events));
  456. printk(KERN_DEBUG "events cleared: %llu\n",
  457. (unsigned long long) le64_to_cpu(sb->events_cleared));
  458. printk(KERN_DEBUG " state: %08x\n", le32_to_cpu(sb->state));
  459. printk(KERN_DEBUG " chunksize: %d B\n", le32_to_cpu(sb->chunksize));
  460. printk(KERN_DEBUG " daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
  461. printk(KERN_DEBUG " sync size: %llu KB\n",
  462. (unsigned long long)le64_to_cpu(sb->sync_size)/2);
  463. printk(KERN_DEBUG "max write behind: %d\n", le32_to_cpu(sb->write_behind));
  464. kunmap_atomic(sb, KM_USER0);
  465. }
  466. /*
  467. * bitmap_new_disk_sb
  468. * @bitmap
  469. *
  470. * This function is somewhat the reverse of bitmap_read_sb. bitmap_read_sb
  471. * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
  472. * This function verifies 'bitmap_info' and populates the on-disk bitmap
  473. * structure, which is to be written to disk.
  474. *
  475. * Returns: 0 on success, -Exxx on error
  476. */
  477. static int bitmap_new_disk_sb(struct bitmap *bitmap)
  478. {
  479. bitmap_super_t *sb;
  480. unsigned long chunksize, daemon_sleep, write_behind;
  481. int err = -EINVAL;
  482. bitmap->sb_page = alloc_page(GFP_KERNEL);
  483. if (IS_ERR(bitmap->sb_page)) {
  484. err = PTR_ERR(bitmap->sb_page);
  485. bitmap->sb_page = NULL;
  486. return err;
  487. }
  488. bitmap->sb_page->index = 0;
  489. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  490. sb->magic = cpu_to_le32(BITMAP_MAGIC);
  491. sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
  492. chunksize = bitmap->mddev->bitmap_info.chunksize;
  493. BUG_ON(!chunksize);
  494. if (!is_power_of_2(chunksize)) {
  495. kunmap_atomic(sb, KM_USER0);
  496. printk(KERN_ERR "bitmap chunksize not a power of 2\n");
  497. return -EINVAL;
  498. }
  499. sb->chunksize = cpu_to_le32(chunksize);
  500. daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
  501. if (!daemon_sleep ||
  502. (daemon_sleep < 1) || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
  503. printk(KERN_INFO "Choosing daemon_sleep default (5 sec)\n");
  504. daemon_sleep = 5 * HZ;
  505. }
  506. sb->daemon_sleep = cpu_to_le32(daemon_sleep);
  507. bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
  508. /*
  509. * FIXME: write_behind for RAID1. If not specified, what
  510. * is a good choice? We choose COUNTER_MAX / 2 arbitrarily.
  511. */
  512. write_behind = bitmap->mddev->bitmap_info.max_write_behind;
  513. if (write_behind > COUNTER_MAX)
  514. write_behind = COUNTER_MAX / 2;
  515. sb->write_behind = cpu_to_le32(write_behind);
  516. bitmap->mddev->bitmap_info.max_write_behind = write_behind;
  517. /* keep the array size field of the bitmap superblock up to date */
  518. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  519. memcpy(sb->uuid, bitmap->mddev->uuid, 16);
  520. bitmap->flags |= BITMAP_STALE;
  521. sb->state |= cpu_to_le32(BITMAP_STALE);
  522. bitmap->events_cleared = bitmap->mddev->events;
  523. sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
  524. bitmap->flags |= BITMAP_HOSTENDIAN;
  525. sb->version = cpu_to_le32(BITMAP_MAJOR_HOSTENDIAN);
  526. kunmap_atomic(sb, KM_USER0);
  527. return 0;
  528. }
  529. /* read the superblock from the bitmap file and initialize some bitmap fields */
  530. static int bitmap_read_sb(struct bitmap *bitmap)
  531. {
  532. char *reason = NULL;
  533. bitmap_super_t *sb;
  534. unsigned long chunksize, daemon_sleep, write_behind;
  535. unsigned long long events;
  536. int err = -EINVAL;
  537. /* page 0 is the superblock, read it... */
  538. if (bitmap->file) {
  539. loff_t isize = i_size_read(bitmap->file->f_mapping->host);
  540. int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
  541. bitmap->sb_page = read_page(bitmap->file, 0, bitmap, bytes);
  542. } else {
  543. bitmap->sb_page = read_sb_page(bitmap->mddev,
  544. bitmap->mddev->bitmap_info.offset,
  545. NULL,
  546. 0, sizeof(bitmap_super_t));
  547. }
  548. if (IS_ERR(bitmap->sb_page)) {
  549. err = PTR_ERR(bitmap->sb_page);
  550. bitmap->sb_page = NULL;
  551. return err;
  552. }
  553. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  554. chunksize = le32_to_cpu(sb->chunksize);
  555. daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
  556. write_behind = le32_to_cpu(sb->write_behind);
  557. /* verify that the bitmap-specific fields are valid */
  558. if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
  559. reason = "bad magic";
  560. else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
  561. le32_to_cpu(sb->version) > BITMAP_MAJOR_HI)
  562. reason = "unrecognized superblock version";
  563. else if (chunksize < 512)
  564. reason = "bitmap chunksize too small";
  565. else if (!is_power_of_2(chunksize))
  566. reason = "bitmap chunksize not a power of 2";
  567. else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
  568. reason = "daemon sleep period out of range";
  569. else if (write_behind > COUNTER_MAX)
  570. reason = "write-behind limit out of range (0 - 16383)";
  571. if (reason) {
  572. printk(KERN_INFO "%s: invalid bitmap file superblock: %s\n",
  573. bmname(bitmap), reason);
  574. goto out;
  575. }
  576. /* keep the array size field of the bitmap superblock up to date */
  577. sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
  578. if (!bitmap->mddev->persistent)
  579. goto success;
  580. /*
  581. * if we have a persistent array superblock, compare the
  582. * bitmap's UUID and event counter to the mddev's
  583. */
  584. if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
  585. printk(KERN_INFO "%s: bitmap superblock UUID mismatch\n",
  586. bmname(bitmap));
  587. goto out;
  588. }
  589. events = le64_to_cpu(sb->events);
  590. if (events < bitmap->mddev->events) {
  591. printk(KERN_INFO "%s: bitmap file is out of date (%llu < %llu) "
  592. "-- forcing full recovery\n", bmname(bitmap), events,
  593. (unsigned long long) bitmap->mddev->events);
  594. sb->state |= cpu_to_le32(BITMAP_STALE);
  595. }
  596. success:
  597. /* assign fields using values from superblock */
  598. bitmap->mddev->bitmap_info.chunksize = chunksize;
  599. bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
  600. bitmap->mddev->bitmap_info.max_write_behind = write_behind;
  601. bitmap->flags |= le32_to_cpu(sb->state);
  602. if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
  603. bitmap->flags |= BITMAP_HOSTENDIAN;
  604. bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
  605. if (bitmap->flags & BITMAP_STALE)
  606. bitmap->events_cleared = bitmap->mddev->events;
  607. err = 0;
  608. out:
  609. kunmap_atomic(sb, KM_USER0);
  610. if (err)
  611. bitmap_print_sb(bitmap);
  612. return err;
  613. }
  614. enum bitmap_mask_op {
  615. MASK_SET,
  616. MASK_UNSET
  617. };
  618. /* record the state of the bitmap in the superblock. Return the old value */
  619. static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
  620. enum bitmap_mask_op op)
  621. {
  622. bitmap_super_t *sb;
  623. unsigned long flags;
  624. int old;
  625. spin_lock_irqsave(&bitmap->lock, flags);
  626. if (!bitmap->sb_page) { /* can't set the state */
  627. spin_unlock_irqrestore(&bitmap->lock, flags);
  628. return 0;
  629. }
  630. spin_unlock_irqrestore(&bitmap->lock, flags);
  631. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  632. old = le32_to_cpu(sb->state) & bits;
  633. switch (op) {
  634. case MASK_SET:
  635. sb->state |= cpu_to_le32(bits);
  636. bitmap->flags |= bits;
  637. break;
  638. case MASK_UNSET:
  639. sb->state &= cpu_to_le32(~bits);
  640. bitmap->flags &= ~bits;
  641. break;
  642. default:
  643. BUG();
  644. }
  645. kunmap_atomic(sb, KM_USER0);
  646. return old;
  647. }
  648. /*
  649. * general bitmap file operations
  650. */
  651. /*
  652. * on-disk bitmap:
  653. *
  654. * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
  655. * file a page at a time. There's a superblock at the start of the file.
  656. */
  657. /* calculate the index of the page that contains this bit */
  658. static inline unsigned long file_page_index(struct bitmap *bitmap, unsigned long chunk)
  659. {
  660. if (!bitmap->mddev->bitmap_info.external)
  661. chunk += sizeof(bitmap_super_t) << 3;
  662. return chunk >> PAGE_BIT_SHIFT;
  663. }
  664. /* calculate the (bit) offset of this bit within a page */
  665. static inline unsigned long file_page_offset(struct bitmap *bitmap, unsigned long chunk)
  666. {
  667. if (!bitmap->mddev->bitmap_info.external)
  668. chunk += sizeof(bitmap_super_t) << 3;
  669. return chunk & (PAGE_BITS - 1);
  670. }
  671. /*
  672. * return a pointer to the page in the filemap that contains the given bit
  673. *
  674. * this lookup is complicated by the fact that the bitmap sb might be exactly
  675. * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
  676. * 0 or page 1
  677. */
  678. static inline struct page *filemap_get_page(struct bitmap *bitmap,
  679. unsigned long chunk)
  680. {
  681. if (file_page_index(bitmap, chunk) >= bitmap->file_pages)
  682. return NULL;
  683. return bitmap->filemap[file_page_index(bitmap, chunk)
  684. - file_page_index(bitmap, 0)];
  685. }
  686. static void bitmap_file_unmap(struct bitmap *bitmap)
  687. {
  688. struct page **map, *sb_page;
  689. unsigned long *attr;
  690. int pages;
  691. unsigned long flags;
  692. spin_lock_irqsave(&bitmap->lock, flags);
  693. map = bitmap->filemap;
  694. bitmap->filemap = NULL;
  695. attr = bitmap->filemap_attr;
  696. bitmap->filemap_attr = NULL;
  697. pages = bitmap->file_pages;
  698. bitmap->file_pages = 0;
  699. sb_page = bitmap->sb_page;
  700. bitmap->sb_page = NULL;
  701. spin_unlock_irqrestore(&bitmap->lock, flags);
  702. while (pages--)
  703. if (map[pages] != sb_page) /* 0 is sb_page, release it below */
  704. free_buffers(map[pages]);
  705. kfree(map);
  706. kfree(attr);
  707. if (sb_page)
  708. free_buffers(sb_page);
  709. }
  710. static void bitmap_file_put(struct bitmap *bitmap)
  711. {
  712. struct file *file;
  713. unsigned long flags;
  714. spin_lock_irqsave(&bitmap->lock, flags);
  715. file = bitmap->file;
  716. bitmap->file = NULL;
  717. spin_unlock_irqrestore(&bitmap->lock, flags);
  718. if (file)
  719. wait_event(bitmap->write_wait,
  720. atomic_read(&bitmap->pending_writes)==0);
  721. bitmap_file_unmap(bitmap);
  722. if (file) {
  723. struct inode *inode = file->f_path.dentry->d_inode;
  724. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  725. fput(file);
  726. }
  727. }
  728. /*
  729. * bitmap_file_kick - if an error occurs while manipulating the bitmap file
  730. * then it is no longer reliable, so we stop using it and we mark the file
  731. * as failed in the superblock
  732. */
  733. static void bitmap_file_kick(struct bitmap *bitmap)
  734. {
  735. char *path, *ptr = NULL;
  736. if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
  737. bitmap_update_sb(bitmap);
  738. if (bitmap->file) {
  739. path = kmalloc(PAGE_SIZE, GFP_KERNEL);
  740. if (path)
  741. ptr = d_path(&bitmap->file->f_path, path,
  742. PAGE_SIZE);
  743. printk(KERN_ALERT
  744. "%s: kicking failed bitmap file %s from array!\n",
  745. bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
  746. kfree(path);
  747. } else
  748. printk(KERN_ALERT
  749. "%s: disabling internal bitmap due to errors\n",
  750. bmname(bitmap));
  751. }
  752. bitmap_file_put(bitmap);
  753. return;
  754. }
  755. enum bitmap_page_attr {
  756. BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
  757. BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
  758. * i.e. counter is 1 or 2. */
  759. BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
  760. };
  761. static inline void set_page_attr(struct bitmap *bitmap, struct page *page,
  762. enum bitmap_page_attr attr)
  763. {
  764. __set_bit((page->index<<2) + attr, bitmap->filemap_attr);
  765. }
  766. static inline void clear_page_attr(struct bitmap *bitmap, struct page *page,
  767. enum bitmap_page_attr attr)
  768. {
  769. __clear_bit((page->index<<2) + attr, bitmap->filemap_attr);
  770. }
  771. static inline unsigned long test_page_attr(struct bitmap *bitmap, struct page *page,
  772. enum bitmap_page_attr attr)
  773. {
  774. return test_bit((page->index<<2) + attr, bitmap->filemap_attr);
  775. }
  776. /*
  777. * bitmap_file_set_bit -- called before performing a write to the md device
  778. * to set (and eventually sync) a particular bit in the bitmap file
  779. *
  780. * we set the bit immediately, then we record the page number so that
  781. * when an unplug occurs, we can flush the dirty pages out to disk
  782. */
  783. static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
  784. {
  785. unsigned long bit;
  786. struct page *page;
  787. void *kaddr;
  788. unsigned long chunk = block >> CHUNK_BLOCK_SHIFT(bitmap);
  789. if (!bitmap->filemap)
  790. return;
  791. page = filemap_get_page(bitmap, chunk);
  792. if (!page)
  793. return;
  794. bit = file_page_offset(bitmap, chunk);
  795. /* set the bit */
  796. kaddr = kmap_atomic(page, KM_USER0);
  797. if (bitmap->flags & BITMAP_HOSTENDIAN)
  798. set_bit(bit, kaddr);
  799. else
  800. __set_bit_le(bit, kaddr);
  801. kunmap_atomic(kaddr, KM_USER0);
  802. pr_debug("set file bit %lu page %lu\n", bit, page->index);
  803. /* record page number so it gets flushed to disk when unplug occurs */
  804. set_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  805. }
  806. /* this gets called when the md device is ready to unplug its underlying
  807. * (slave) device queues -- before we let any writes go down, we need to
  808. * sync the dirty pages of the bitmap file to disk */
  809. void bitmap_unplug(struct bitmap *bitmap)
  810. {
  811. unsigned long i, flags;
  812. int dirty, need_write;
  813. struct page *page;
  814. int wait = 0;
  815. if (!bitmap)
  816. return;
  817. /* look at each page to see if there are any set bits that need to be
  818. * flushed out to disk */
  819. for (i = 0; i < bitmap->file_pages; i++) {
  820. spin_lock_irqsave(&bitmap->lock, flags);
  821. if (!bitmap->filemap) {
  822. spin_unlock_irqrestore(&bitmap->lock, flags);
  823. return;
  824. }
  825. page = bitmap->filemap[i];
  826. dirty = test_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  827. need_write = test_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  828. clear_page_attr(bitmap, page, BITMAP_PAGE_DIRTY);
  829. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  830. if (dirty)
  831. wait = 1;
  832. spin_unlock_irqrestore(&bitmap->lock, flags);
  833. if (dirty || need_write)
  834. write_page(bitmap, page, 0);
  835. }
  836. if (wait) { /* if any writes were performed, we need to wait on them */
  837. if (bitmap->file)
  838. wait_event(bitmap->write_wait,
  839. atomic_read(&bitmap->pending_writes)==0);
  840. else
  841. md_super_wait(bitmap->mddev);
  842. }
  843. if (bitmap->flags & BITMAP_WRITE_ERROR)
  844. bitmap_file_kick(bitmap);
  845. }
  846. EXPORT_SYMBOL(bitmap_unplug);
  847. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
  848. /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
  849. * the in-memory bitmap from the on-disk bitmap -- also, sets up the
  850. * memory mapping of the bitmap file
  851. * Special cases:
  852. * if there's no bitmap file, or if the bitmap file had been
  853. * previously kicked from the array, we mark all the bits as
  854. * 1's in order to cause a full resync.
  855. *
  856. * We ignore all bits for sectors that end earlier than 'start'.
  857. * This is used when reading an out-of-date bitmap...
  858. */
  859. static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
  860. {
  861. unsigned long i, chunks, index, oldindex, bit;
  862. struct page *page = NULL, *oldpage = NULL;
  863. unsigned long num_pages, bit_cnt = 0;
  864. struct file *file;
  865. unsigned long bytes, offset;
  866. int outofdate;
  867. int ret = -ENOSPC;
  868. void *paddr;
  869. chunks = bitmap->chunks;
  870. file = bitmap->file;
  871. BUG_ON(!file && !bitmap->mddev->bitmap_info.offset);
  872. #ifdef INJECT_FAULTS_3
  873. outofdate = 1;
  874. #else
  875. outofdate = bitmap->flags & BITMAP_STALE;
  876. #endif
  877. if (outofdate)
  878. printk(KERN_INFO "%s: bitmap file is out of date, doing full "
  879. "recovery\n", bmname(bitmap));
  880. bytes = DIV_ROUND_UP(bitmap->chunks, 8);
  881. if (!bitmap->mddev->bitmap_info.external)
  882. bytes += sizeof(bitmap_super_t);
  883. num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
  884. if (file && i_size_read(file->f_mapping->host) < bytes) {
  885. printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
  886. bmname(bitmap),
  887. (unsigned long) i_size_read(file->f_mapping->host),
  888. bytes);
  889. goto err;
  890. }
  891. ret = -ENOMEM;
  892. bitmap->filemap = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
  893. if (!bitmap->filemap)
  894. goto err;
  895. /* We need 4 bits per page, rounded up to a multiple of sizeof(unsigned long) */
  896. bitmap->filemap_attr = kzalloc(
  897. roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
  898. GFP_KERNEL);
  899. if (!bitmap->filemap_attr)
  900. goto err;
  901. oldindex = ~0L;
  902. for (i = 0; i < chunks; i++) {
  903. int b;
  904. index = file_page_index(bitmap, i);
  905. bit = file_page_offset(bitmap, i);
  906. if (index != oldindex) { /* this is a new page, read it in */
  907. int count;
  908. /* unmap the old page, we're done with it */
  909. if (index == num_pages-1)
  910. count = bytes - index * PAGE_SIZE;
  911. else
  912. count = PAGE_SIZE;
  913. if (index == 0 && bitmap->sb_page) {
  914. /*
  915. * if we're here then the superblock page
  916. * contains some bits (PAGE_SIZE != sizeof sb)
  917. * we've already read it in, so just use it
  918. */
  919. page = bitmap->sb_page;
  920. offset = sizeof(bitmap_super_t);
  921. if (!file)
  922. page = read_sb_page(
  923. bitmap->mddev,
  924. bitmap->mddev->bitmap_info.offset,
  925. page,
  926. index, count);
  927. } else if (file) {
  928. page = read_page(file, index, bitmap, count);
  929. offset = 0;
  930. } else {
  931. page = read_sb_page(bitmap->mddev,
  932. bitmap->mddev->bitmap_info.offset,
  933. NULL,
  934. index, count);
  935. offset = 0;
  936. }
  937. if (IS_ERR(page)) { /* read error */
  938. ret = PTR_ERR(page);
  939. goto err;
  940. }
  941. oldindex = index;
  942. oldpage = page;
  943. bitmap->filemap[bitmap->file_pages++] = page;
  944. bitmap->last_page_size = count;
  945. if (outofdate) {
  946. /*
  947. * if bitmap is out of date, dirty the
  948. * whole page and write it out
  949. */
  950. paddr = kmap_atomic(page, KM_USER0);
  951. memset(paddr + offset, 0xff,
  952. PAGE_SIZE - offset);
  953. kunmap_atomic(paddr, KM_USER0);
  954. write_page(bitmap, page, 1);
  955. ret = -EIO;
  956. if (bitmap->flags & BITMAP_WRITE_ERROR)
  957. goto err;
  958. }
  959. }
  960. paddr = kmap_atomic(page, KM_USER0);
  961. if (bitmap->flags & BITMAP_HOSTENDIAN)
  962. b = test_bit(bit, paddr);
  963. else
  964. b = test_bit_le(bit, paddr);
  965. kunmap_atomic(paddr, KM_USER0);
  966. if (b) {
  967. /* if the disk bit is set, set the memory bit */
  968. int needed = ((sector_t)(i+1) << (CHUNK_BLOCK_SHIFT(bitmap))
  969. >= start);
  970. bitmap_set_memory_bits(bitmap,
  971. (sector_t)i << CHUNK_BLOCK_SHIFT(bitmap),
  972. needed);
  973. bit_cnt++;
  974. }
  975. }
  976. /* everything went OK */
  977. ret = 0;
  978. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
  979. if (bit_cnt) { /* Kick recovery if any bits were set */
  980. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  981. md_wakeup_thread(bitmap->mddev->thread);
  982. }
  983. printk(KERN_INFO "%s: bitmap initialized from disk: "
  984. "read %lu/%lu pages, set %lu of %lu bits\n",
  985. bmname(bitmap), bitmap->file_pages, num_pages, bit_cnt, chunks);
  986. return 0;
  987. err:
  988. printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
  989. bmname(bitmap), ret);
  990. return ret;
  991. }
  992. void bitmap_write_all(struct bitmap *bitmap)
  993. {
  994. /* We don't actually write all bitmap blocks here,
  995. * just flag them as needing to be written
  996. */
  997. int i;
  998. for (i = 0; i < bitmap->file_pages; i++)
  999. set_page_attr(bitmap, bitmap->filemap[i],
  1000. BITMAP_PAGE_NEEDWRITE);
  1001. bitmap->allclean = 0;
  1002. }
  1003. static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
  1004. {
  1005. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  1006. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1007. bitmap->bp[page].count += inc;
  1008. bitmap_checkfree(bitmap, page);
  1009. }
  1010. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1011. sector_t offset, sector_t *blocks,
  1012. int create);
  1013. /*
  1014. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  1015. * out to disk
  1016. */
  1017. void bitmap_daemon_work(mddev_t *mddev)
  1018. {
  1019. struct bitmap *bitmap;
  1020. unsigned long j;
  1021. unsigned long flags;
  1022. struct page *page = NULL, *lastpage = NULL;
  1023. sector_t blocks;
  1024. void *paddr;
  1025. /* Use a mutex to guard daemon_work against
  1026. * bitmap_destroy.
  1027. */
  1028. mutex_lock(&mddev->bitmap_info.mutex);
  1029. bitmap = mddev->bitmap;
  1030. if (bitmap == NULL) {
  1031. mutex_unlock(&mddev->bitmap_info.mutex);
  1032. return;
  1033. }
  1034. if (time_before(jiffies, bitmap->daemon_lastrun
  1035. + bitmap->mddev->bitmap_info.daemon_sleep))
  1036. goto done;
  1037. bitmap->daemon_lastrun = jiffies;
  1038. if (bitmap->allclean) {
  1039. bitmap->mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1040. goto done;
  1041. }
  1042. bitmap->allclean = 1;
  1043. spin_lock_irqsave(&bitmap->lock, flags);
  1044. for (j = 0; j < bitmap->chunks; j++) {
  1045. bitmap_counter_t *bmc;
  1046. if (!bitmap->filemap)
  1047. /* error or shutdown */
  1048. break;
  1049. page = filemap_get_page(bitmap, j);
  1050. if (page != lastpage) {
  1051. /* skip this page unless it's marked as needing cleaning */
  1052. if (!test_page_attr(bitmap, page, BITMAP_PAGE_PENDING)) {
  1053. int need_write = test_page_attr(bitmap, page,
  1054. BITMAP_PAGE_NEEDWRITE);
  1055. if (need_write)
  1056. clear_page_attr(bitmap, page, BITMAP_PAGE_NEEDWRITE);
  1057. spin_unlock_irqrestore(&bitmap->lock, flags);
  1058. if (need_write)
  1059. write_page(bitmap, page, 0);
  1060. spin_lock_irqsave(&bitmap->lock, flags);
  1061. j |= (PAGE_BITS - 1);
  1062. continue;
  1063. }
  1064. /* grab the new page, sync and release the old */
  1065. if (lastpage != NULL) {
  1066. if (test_page_attr(bitmap, lastpage,
  1067. BITMAP_PAGE_NEEDWRITE)) {
  1068. clear_page_attr(bitmap, lastpage,
  1069. BITMAP_PAGE_NEEDWRITE);
  1070. spin_unlock_irqrestore(&bitmap->lock, flags);
  1071. write_page(bitmap, lastpage, 0);
  1072. } else {
  1073. set_page_attr(bitmap, lastpage,
  1074. BITMAP_PAGE_NEEDWRITE);
  1075. bitmap->allclean = 0;
  1076. spin_unlock_irqrestore(&bitmap->lock, flags);
  1077. }
  1078. } else
  1079. spin_unlock_irqrestore(&bitmap->lock, flags);
  1080. lastpage = page;
  1081. /* We are possibly going to clear some bits, so make
  1082. * sure that events_cleared is up-to-date.
  1083. */
  1084. if (bitmap->need_sync &&
  1085. bitmap->mddev->bitmap_info.external == 0) {
  1086. bitmap_super_t *sb;
  1087. bitmap->need_sync = 0;
  1088. sb = kmap_atomic(bitmap->sb_page, KM_USER0);
  1089. sb->events_cleared =
  1090. cpu_to_le64(bitmap->events_cleared);
  1091. kunmap_atomic(sb, KM_USER0);
  1092. write_page(bitmap, bitmap->sb_page, 1);
  1093. }
  1094. spin_lock_irqsave(&bitmap->lock, flags);
  1095. if (!bitmap->need_sync)
  1096. clear_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
  1097. else
  1098. bitmap->allclean = 0;
  1099. }
  1100. bmc = bitmap_get_counter(bitmap,
  1101. (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
  1102. &blocks, 0);
  1103. if (!bmc)
  1104. j |= PAGE_COUNTER_MASK;
  1105. else if (*bmc) {
  1106. if (*bmc == 1 && !bitmap->need_sync) {
  1107. /* we can clear the bit */
  1108. *bmc = 0;
  1109. bitmap_count_page(bitmap,
  1110. (sector_t)j << CHUNK_BLOCK_SHIFT(bitmap),
  1111. -1);
  1112. /* clear the bit */
  1113. paddr = kmap_atomic(page, KM_USER0);
  1114. if (bitmap->flags & BITMAP_HOSTENDIAN)
  1115. clear_bit(file_page_offset(bitmap, j),
  1116. paddr);
  1117. else
  1118. __clear_bit_le(
  1119. file_page_offset(bitmap,
  1120. j),
  1121. paddr);
  1122. kunmap_atomic(paddr, KM_USER0);
  1123. } else if (*bmc <= 2) {
  1124. *bmc = 1; /* maybe clear the bit next time */
  1125. set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
  1126. bitmap->allclean = 0;
  1127. }
  1128. }
  1129. }
  1130. spin_unlock_irqrestore(&bitmap->lock, flags);
  1131. /* now sync the final page */
  1132. if (lastpage != NULL) {
  1133. spin_lock_irqsave(&bitmap->lock, flags);
  1134. if (test_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE)) {
  1135. clear_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1136. spin_unlock_irqrestore(&bitmap->lock, flags);
  1137. write_page(bitmap, lastpage, 0);
  1138. } else {
  1139. set_page_attr(bitmap, lastpage, BITMAP_PAGE_NEEDWRITE);
  1140. bitmap->allclean = 0;
  1141. spin_unlock_irqrestore(&bitmap->lock, flags);
  1142. }
  1143. }
  1144. done:
  1145. if (bitmap->allclean == 0)
  1146. bitmap->mddev->thread->timeout =
  1147. bitmap->mddev->bitmap_info.daemon_sleep;
  1148. mutex_unlock(&mddev->bitmap_info.mutex);
  1149. }
  1150. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1151. sector_t offset, sector_t *blocks,
  1152. int create)
  1153. __releases(bitmap->lock)
  1154. __acquires(bitmap->lock)
  1155. {
  1156. /* If 'create', we might release the lock and reclaim it.
  1157. * The lock must have been taken with interrupts enabled.
  1158. * If !create, we don't release the lock.
  1159. */
  1160. sector_t chunk = offset >> CHUNK_BLOCK_SHIFT(bitmap);
  1161. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1162. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1163. sector_t csize;
  1164. int err;
  1165. err = bitmap_checkpage(bitmap, page, create);
  1166. if (bitmap->bp[page].hijacked ||
  1167. bitmap->bp[page].map == NULL)
  1168. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap) +
  1169. PAGE_COUNTER_SHIFT - 1);
  1170. else
  1171. csize = ((sector_t)1) << (CHUNK_BLOCK_SHIFT(bitmap));
  1172. *blocks = csize - (offset & (csize - 1));
  1173. if (err < 0)
  1174. return NULL;
  1175. /* now locked ... */
  1176. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1177. /* should we use the first or second counter field
  1178. * of the hijacked pointer? */
  1179. int hi = (pageoff > PAGE_COUNTER_MASK);
  1180. return &((bitmap_counter_t *)
  1181. &bitmap->bp[page].map)[hi];
  1182. } else /* page is allocated */
  1183. return (bitmap_counter_t *)
  1184. &(bitmap->bp[page].map[pageoff]);
  1185. }
  1186. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1187. {
  1188. if (!bitmap)
  1189. return 0;
  1190. if (behind) {
  1191. int bw;
  1192. atomic_inc(&bitmap->behind_writes);
  1193. bw = atomic_read(&bitmap->behind_writes);
  1194. if (bw > bitmap->behind_writes_used)
  1195. bitmap->behind_writes_used = bw;
  1196. pr_debug("inc write-behind count %d/%lu\n",
  1197. bw, bitmap->mddev->bitmap_info.max_write_behind);
  1198. }
  1199. while (sectors) {
  1200. sector_t blocks;
  1201. bitmap_counter_t *bmc;
  1202. spin_lock_irq(&bitmap->lock);
  1203. bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
  1204. if (!bmc) {
  1205. spin_unlock_irq(&bitmap->lock);
  1206. return 0;
  1207. }
  1208. if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
  1209. DEFINE_WAIT(__wait);
  1210. /* note that it is safe to do the prepare_to_wait
  1211. * after the test as long as we do it before dropping
  1212. * the spinlock.
  1213. */
  1214. prepare_to_wait(&bitmap->overflow_wait, &__wait,
  1215. TASK_UNINTERRUPTIBLE);
  1216. spin_unlock_irq(&bitmap->lock);
  1217. io_schedule();
  1218. finish_wait(&bitmap->overflow_wait, &__wait);
  1219. continue;
  1220. }
  1221. switch (*bmc) {
  1222. case 0:
  1223. bitmap_file_set_bit(bitmap, offset);
  1224. bitmap_count_page(bitmap, offset, 1);
  1225. /* fall through */
  1226. case 1:
  1227. *bmc = 2;
  1228. }
  1229. (*bmc)++;
  1230. spin_unlock_irq(&bitmap->lock);
  1231. offset += blocks;
  1232. if (sectors > blocks)
  1233. sectors -= blocks;
  1234. else
  1235. sectors = 0;
  1236. }
  1237. return 0;
  1238. }
  1239. EXPORT_SYMBOL(bitmap_startwrite);
  1240. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1241. int success, int behind)
  1242. {
  1243. if (!bitmap)
  1244. return;
  1245. if (behind) {
  1246. if (atomic_dec_and_test(&bitmap->behind_writes))
  1247. wake_up(&bitmap->behind_wait);
  1248. pr_debug("dec write-behind count %d/%lu\n",
  1249. atomic_read(&bitmap->behind_writes),
  1250. bitmap->mddev->bitmap_info.max_write_behind);
  1251. }
  1252. if (bitmap->mddev->degraded)
  1253. /* Never clear bits or update events_cleared when degraded */
  1254. success = 0;
  1255. while (sectors) {
  1256. sector_t blocks;
  1257. unsigned long flags;
  1258. bitmap_counter_t *bmc;
  1259. spin_lock_irqsave(&bitmap->lock, flags);
  1260. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1261. if (!bmc) {
  1262. spin_unlock_irqrestore(&bitmap->lock, flags);
  1263. return;
  1264. }
  1265. if (success &&
  1266. bitmap->events_cleared < bitmap->mddev->events) {
  1267. bitmap->events_cleared = bitmap->mddev->events;
  1268. bitmap->need_sync = 1;
  1269. sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
  1270. }
  1271. if (!success && !NEEDED(*bmc))
  1272. *bmc |= NEEDED_MASK;
  1273. if (COUNTER(*bmc) == COUNTER_MAX)
  1274. wake_up(&bitmap->overflow_wait);
  1275. (*bmc)--;
  1276. if (*bmc <= 2) {
  1277. set_page_attr(bitmap,
  1278. filemap_get_page(
  1279. bitmap,
  1280. offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1281. BITMAP_PAGE_PENDING);
  1282. bitmap->allclean = 0;
  1283. }
  1284. spin_unlock_irqrestore(&bitmap->lock, flags);
  1285. offset += blocks;
  1286. if (sectors > blocks)
  1287. sectors -= blocks;
  1288. else
  1289. sectors = 0;
  1290. }
  1291. }
  1292. EXPORT_SYMBOL(bitmap_endwrite);
  1293. static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1294. int degraded)
  1295. {
  1296. bitmap_counter_t *bmc;
  1297. int rv;
  1298. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1299. *blocks = 1024;
  1300. return 1; /* always resync if no bitmap */
  1301. }
  1302. spin_lock_irq(&bitmap->lock);
  1303. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1304. rv = 0;
  1305. if (bmc) {
  1306. /* locked */
  1307. if (RESYNC(*bmc))
  1308. rv = 1;
  1309. else if (NEEDED(*bmc)) {
  1310. rv = 1;
  1311. if (!degraded) { /* don't set/clear bits if degraded */
  1312. *bmc |= RESYNC_MASK;
  1313. *bmc &= ~NEEDED_MASK;
  1314. }
  1315. }
  1316. }
  1317. spin_unlock_irq(&bitmap->lock);
  1318. return rv;
  1319. }
  1320. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1321. int degraded)
  1322. {
  1323. /* bitmap_start_sync must always report on multiples of whole
  1324. * pages, otherwise resync (which is very PAGE_SIZE based) will
  1325. * get confused.
  1326. * So call __bitmap_start_sync repeatedly (if needed) until
  1327. * At least PAGE_SIZE>>9 blocks are covered.
  1328. * Return the 'or' of the result.
  1329. */
  1330. int rv = 0;
  1331. sector_t blocks1;
  1332. *blocks = 0;
  1333. while (*blocks < (PAGE_SIZE>>9)) {
  1334. rv |= __bitmap_start_sync(bitmap, offset,
  1335. &blocks1, degraded);
  1336. offset += blocks1;
  1337. *blocks += blocks1;
  1338. }
  1339. return rv;
  1340. }
  1341. EXPORT_SYMBOL(bitmap_start_sync);
  1342. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
  1343. {
  1344. bitmap_counter_t *bmc;
  1345. unsigned long flags;
  1346. if (bitmap == NULL) {
  1347. *blocks = 1024;
  1348. return;
  1349. }
  1350. spin_lock_irqsave(&bitmap->lock, flags);
  1351. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1352. if (bmc == NULL)
  1353. goto unlock;
  1354. /* locked */
  1355. if (RESYNC(*bmc)) {
  1356. *bmc &= ~RESYNC_MASK;
  1357. if (!NEEDED(*bmc) && aborted)
  1358. *bmc |= NEEDED_MASK;
  1359. else {
  1360. if (*bmc <= 2) {
  1361. set_page_attr(bitmap,
  1362. filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap)),
  1363. BITMAP_PAGE_PENDING);
  1364. bitmap->allclean = 0;
  1365. }
  1366. }
  1367. }
  1368. unlock:
  1369. spin_unlock_irqrestore(&bitmap->lock, flags);
  1370. }
  1371. EXPORT_SYMBOL(bitmap_end_sync);
  1372. void bitmap_close_sync(struct bitmap *bitmap)
  1373. {
  1374. /* Sync has finished, and any bitmap chunks that weren't synced
  1375. * properly have been aborted. It remains to us to clear the
  1376. * RESYNC bit wherever it is still on
  1377. */
  1378. sector_t sector = 0;
  1379. sector_t blocks;
  1380. if (!bitmap)
  1381. return;
  1382. while (sector < bitmap->mddev->resync_max_sectors) {
  1383. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1384. sector += blocks;
  1385. }
  1386. }
  1387. EXPORT_SYMBOL(bitmap_close_sync);
  1388. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1389. {
  1390. sector_t s = 0;
  1391. sector_t blocks;
  1392. if (!bitmap)
  1393. return;
  1394. if (sector == 0) {
  1395. bitmap->last_end_sync = jiffies;
  1396. return;
  1397. }
  1398. if (time_before(jiffies, (bitmap->last_end_sync
  1399. + bitmap->mddev->bitmap_info.daemon_sleep)))
  1400. return;
  1401. wait_event(bitmap->mddev->recovery_wait,
  1402. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1403. bitmap->mddev->curr_resync_completed = sector;
  1404. set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
  1405. sector &= ~((1ULL << CHUNK_BLOCK_SHIFT(bitmap)) - 1);
  1406. s = 0;
  1407. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1408. bitmap_end_sync(bitmap, s, &blocks, 0);
  1409. s += blocks;
  1410. }
  1411. bitmap->last_end_sync = jiffies;
  1412. sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
  1413. }
  1414. EXPORT_SYMBOL(bitmap_cond_end_sync);
  1415. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1416. {
  1417. /* For each chunk covered by any of these sectors, set the
  1418. * counter to 1 and set resync_needed. They should all
  1419. * be 0 at this point
  1420. */
  1421. sector_t secs;
  1422. bitmap_counter_t *bmc;
  1423. spin_lock_irq(&bitmap->lock);
  1424. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1425. if (!bmc) {
  1426. spin_unlock_irq(&bitmap->lock);
  1427. return;
  1428. }
  1429. if (!*bmc) {
  1430. struct page *page;
  1431. *bmc = 1 | (needed ? NEEDED_MASK : 0);
  1432. bitmap_count_page(bitmap, offset, 1);
  1433. page = filemap_get_page(bitmap, offset >> CHUNK_BLOCK_SHIFT(bitmap));
  1434. set_page_attr(bitmap, page, BITMAP_PAGE_PENDING);
  1435. bitmap->allclean = 0;
  1436. }
  1437. spin_unlock_irq(&bitmap->lock);
  1438. }
  1439. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1440. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1441. {
  1442. unsigned long chunk;
  1443. for (chunk = s; chunk <= e; chunk++) {
  1444. sector_t sec = (sector_t)chunk << CHUNK_BLOCK_SHIFT(bitmap);
  1445. bitmap_set_memory_bits(bitmap, sec, 1);
  1446. bitmap_file_set_bit(bitmap, sec);
  1447. if (sec < bitmap->mddev->recovery_cp)
  1448. /* We are asserting that the array is dirty,
  1449. * so move the recovery_cp address back so
  1450. * that it is obvious that it is dirty
  1451. */
  1452. bitmap->mddev->recovery_cp = sec;
  1453. }
  1454. }
  1455. /*
  1456. * flush out any pending updates
  1457. */
  1458. void bitmap_flush(mddev_t *mddev)
  1459. {
  1460. struct bitmap *bitmap = mddev->bitmap;
  1461. long sleep;
  1462. if (!bitmap) /* there was no bitmap */
  1463. return;
  1464. /* run the daemon_work three time to ensure everything is flushed
  1465. * that can be
  1466. */
  1467. sleep = mddev->bitmap_info.daemon_sleep * 2;
  1468. bitmap->daemon_lastrun -= sleep;
  1469. bitmap_daemon_work(mddev);
  1470. bitmap->daemon_lastrun -= sleep;
  1471. bitmap_daemon_work(mddev);
  1472. bitmap->daemon_lastrun -= sleep;
  1473. bitmap_daemon_work(mddev);
  1474. bitmap_update_sb(bitmap);
  1475. }
  1476. /*
  1477. * free memory that was allocated
  1478. */
  1479. static void bitmap_free(struct bitmap *bitmap)
  1480. {
  1481. unsigned long k, pages;
  1482. struct bitmap_page *bp;
  1483. if (!bitmap) /* there was no bitmap */
  1484. return;
  1485. /* release the bitmap file and kill the daemon */
  1486. bitmap_file_put(bitmap);
  1487. bp = bitmap->bp;
  1488. pages = bitmap->pages;
  1489. /* free all allocated memory */
  1490. if (bp) /* deallocate the page memory */
  1491. for (k = 0; k < pages; k++)
  1492. if (bp[k].map && !bp[k].hijacked)
  1493. kfree(bp[k].map);
  1494. kfree(bp);
  1495. kfree(bitmap);
  1496. }
  1497. void bitmap_destroy(mddev_t *mddev)
  1498. {
  1499. struct bitmap *bitmap = mddev->bitmap;
  1500. if (!bitmap) /* there was no bitmap */
  1501. return;
  1502. mutex_lock(&mddev->bitmap_info.mutex);
  1503. mddev->bitmap = NULL; /* disconnect from the md device */
  1504. mutex_unlock(&mddev->bitmap_info.mutex);
  1505. if (mddev->thread)
  1506. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1507. if (bitmap->sysfs_can_clear)
  1508. sysfs_put(bitmap->sysfs_can_clear);
  1509. bitmap_free(bitmap);
  1510. }
  1511. /*
  1512. * initialize the bitmap structure
  1513. * if this returns an error, bitmap_destroy must be called to do clean up
  1514. */
  1515. int bitmap_create(mddev_t *mddev)
  1516. {
  1517. struct bitmap *bitmap;
  1518. sector_t blocks = mddev->resync_max_sectors;
  1519. unsigned long chunks;
  1520. unsigned long pages;
  1521. struct file *file = mddev->bitmap_info.file;
  1522. int err;
  1523. struct sysfs_dirent *bm = NULL;
  1524. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1525. if (!file
  1526. && !mddev->bitmap_info.offset) /* bitmap disabled, nothing to do */
  1527. return 0;
  1528. BUG_ON(file && mddev->bitmap_info.offset);
  1529. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1530. if (!bitmap)
  1531. return -ENOMEM;
  1532. spin_lock_init(&bitmap->lock);
  1533. atomic_set(&bitmap->pending_writes, 0);
  1534. init_waitqueue_head(&bitmap->write_wait);
  1535. init_waitqueue_head(&bitmap->overflow_wait);
  1536. init_waitqueue_head(&bitmap->behind_wait);
  1537. bitmap->mddev = mddev;
  1538. if (mddev->kobj.sd)
  1539. bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
  1540. if (bm) {
  1541. bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
  1542. sysfs_put(bm);
  1543. } else
  1544. bitmap->sysfs_can_clear = NULL;
  1545. bitmap->file = file;
  1546. if (file) {
  1547. get_file(file);
  1548. /* As future accesses to this file will use bmap,
  1549. * and bypass the page cache, we must sync the file
  1550. * first.
  1551. */
  1552. vfs_fsync(file, 1);
  1553. }
  1554. /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
  1555. if (!mddev->bitmap_info.external) {
  1556. /*
  1557. * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
  1558. * instructing us to create a new on-disk bitmap instance.
  1559. */
  1560. if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
  1561. err = bitmap_new_disk_sb(bitmap);
  1562. else
  1563. err = bitmap_read_sb(bitmap);
  1564. } else {
  1565. err = 0;
  1566. if (mddev->bitmap_info.chunksize == 0 ||
  1567. mddev->bitmap_info.daemon_sleep == 0)
  1568. /* chunksize and time_base need to be
  1569. * set first. */
  1570. err = -EINVAL;
  1571. }
  1572. if (err)
  1573. goto error;
  1574. bitmap->daemon_lastrun = jiffies;
  1575. bitmap->chunkshift = ffz(~mddev->bitmap_info.chunksize);
  1576. /* now that chunksize and chunkshift are set, we can use these macros */
  1577. chunks = (blocks + CHUNK_BLOCK_RATIO(bitmap) - 1) >>
  1578. CHUNK_BLOCK_SHIFT(bitmap);
  1579. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1580. BUG_ON(!pages);
  1581. bitmap->chunks = chunks;
  1582. bitmap->pages = pages;
  1583. bitmap->missing_pages = pages;
  1584. #ifdef INJECT_FATAL_FAULT_1
  1585. bitmap->bp = NULL;
  1586. #else
  1587. bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1588. #endif
  1589. err = -ENOMEM;
  1590. if (!bitmap->bp)
  1591. goto error;
  1592. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1593. pages, bmname(bitmap));
  1594. mddev->bitmap = bitmap;
  1595. return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
  1596. error:
  1597. bitmap_free(bitmap);
  1598. return err;
  1599. }
  1600. int bitmap_load(mddev_t *mddev)
  1601. {
  1602. int err = 0;
  1603. sector_t start = 0;
  1604. sector_t sector = 0;
  1605. struct bitmap *bitmap = mddev->bitmap;
  1606. if (!bitmap)
  1607. goto out;
  1608. /* Clear out old bitmap info first: Either there is none, or we
  1609. * are resuming after someone else has possibly changed things,
  1610. * so we should forget old cached info.
  1611. * All chunks should be clean, but some might need_sync.
  1612. */
  1613. while (sector < mddev->resync_max_sectors) {
  1614. sector_t blocks;
  1615. bitmap_start_sync(bitmap, sector, &blocks, 0);
  1616. sector += blocks;
  1617. }
  1618. bitmap_close_sync(bitmap);
  1619. if (mddev->degraded == 0
  1620. || bitmap->events_cleared == mddev->events)
  1621. /* no need to keep dirty bits to optimise a
  1622. * re-add of a missing device */
  1623. start = mddev->recovery_cp;
  1624. err = bitmap_init_from_disk(bitmap, start);
  1625. if (err)
  1626. goto out;
  1627. mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
  1628. md_wakeup_thread(mddev->thread);
  1629. bitmap_update_sb(bitmap);
  1630. if (bitmap->flags & BITMAP_WRITE_ERROR)
  1631. err = -EIO;
  1632. out:
  1633. return err;
  1634. }
  1635. EXPORT_SYMBOL_GPL(bitmap_load);
  1636. static ssize_t
  1637. location_show(mddev_t *mddev, char *page)
  1638. {
  1639. ssize_t len;
  1640. if (mddev->bitmap_info.file)
  1641. len = sprintf(page, "file");
  1642. else if (mddev->bitmap_info.offset)
  1643. len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
  1644. else
  1645. len = sprintf(page, "none");
  1646. len += sprintf(page+len, "\n");
  1647. return len;
  1648. }
  1649. static ssize_t
  1650. location_store(mddev_t *mddev, const char *buf, size_t len)
  1651. {
  1652. if (mddev->pers) {
  1653. if (!mddev->pers->quiesce)
  1654. return -EBUSY;
  1655. if (mddev->recovery || mddev->sync_thread)
  1656. return -EBUSY;
  1657. }
  1658. if (mddev->bitmap || mddev->bitmap_info.file ||
  1659. mddev->bitmap_info.offset) {
  1660. /* bitmap already configured. Only option is to clear it */
  1661. if (strncmp(buf, "none", 4) != 0)
  1662. return -EBUSY;
  1663. if (mddev->pers) {
  1664. mddev->pers->quiesce(mddev, 1);
  1665. bitmap_destroy(mddev);
  1666. mddev->pers->quiesce(mddev, 0);
  1667. }
  1668. mddev->bitmap_info.offset = 0;
  1669. if (mddev->bitmap_info.file) {
  1670. struct file *f = mddev->bitmap_info.file;
  1671. mddev->bitmap_info.file = NULL;
  1672. restore_bitmap_write_access(f);
  1673. fput(f);
  1674. }
  1675. } else {
  1676. /* No bitmap, OK to set a location */
  1677. long long offset;
  1678. if (strncmp(buf, "none", 4) == 0)
  1679. /* nothing to be done */;
  1680. else if (strncmp(buf, "file:", 5) == 0) {
  1681. /* Not supported yet */
  1682. return -EINVAL;
  1683. } else {
  1684. int rv;
  1685. if (buf[0] == '+')
  1686. rv = strict_strtoll(buf+1, 10, &offset);
  1687. else
  1688. rv = strict_strtoll(buf, 10, &offset);
  1689. if (rv)
  1690. return rv;
  1691. if (offset == 0)
  1692. return -EINVAL;
  1693. if (mddev->bitmap_info.external == 0 &&
  1694. mddev->major_version == 0 &&
  1695. offset != mddev->bitmap_info.default_offset)
  1696. return -EINVAL;
  1697. mddev->bitmap_info.offset = offset;
  1698. if (mddev->pers) {
  1699. mddev->pers->quiesce(mddev, 1);
  1700. rv = bitmap_create(mddev);
  1701. if (rv) {
  1702. bitmap_destroy(mddev);
  1703. mddev->bitmap_info.offset = 0;
  1704. }
  1705. mddev->pers->quiesce(mddev, 0);
  1706. if (rv)
  1707. return rv;
  1708. }
  1709. }
  1710. }
  1711. if (!mddev->external) {
  1712. /* Ensure new bitmap info is stored in
  1713. * metadata promptly.
  1714. */
  1715. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1716. md_wakeup_thread(mddev->thread);
  1717. }
  1718. return len;
  1719. }
  1720. static struct md_sysfs_entry bitmap_location =
  1721. __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
  1722. static ssize_t
  1723. timeout_show(mddev_t *mddev, char *page)
  1724. {
  1725. ssize_t len;
  1726. unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
  1727. unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
  1728. len = sprintf(page, "%lu", secs);
  1729. if (jifs)
  1730. len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
  1731. len += sprintf(page+len, "\n");
  1732. return len;
  1733. }
  1734. static ssize_t
  1735. timeout_store(mddev_t *mddev, const char *buf, size_t len)
  1736. {
  1737. /* timeout can be set at any time */
  1738. unsigned long timeout;
  1739. int rv = strict_strtoul_scaled(buf, &timeout, 4);
  1740. if (rv)
  1741. return rv;
  1742. /* just to make sure we don't overflow... */
  1743. if (timeout >= LONG_MAX / HZ)
  1744. return -EINVAL;
  1745. timeout = timeout * HZ / 10000;
  1746. if (timeout >= MAX_SCHEDULE_TIMEOUT)
  1747. timeout = MAX_SCHEDULE_TIMEOUT-1;
  1748. if (timeout < 1)
  1749. timeout = 1;
  1750. mddev->bitmap_info.daemon_sleep = timeout;
  1751. if (mddev->thread) {
  1752. /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
  1753. * the bitmap is all clean and we don't need to
  1754. * adjust the timeout right now
  1755. */
  1756. if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
  1757. mddev->thread->timeout = timeout;
  1758. md_wakeup_thread(mddev->thread);
  1759. }
  1760. }
  1761. return len;
  1762. }
  1763. static struct md_sysfs_entry bitmap_timeout =
  1764. __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
  1765. static ssize_t
  1766. backlog_show(mddev_t *mddev, char *page)
  1767. {
  1768. return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
  1769. }
  1770. static ssize_t
  1771. backlog_store(mddev_t *mddev, const char *buf, size_t len)
  1772. {
  1773. unsigned long backlog;
  1774. int rv = strict_strtoul(buf, 10, &backlog);
  1775. if (rv)
  1776. return rv;
  1777. if (backlog > COUNTER_MAX)
  1778. return -EINVAL;
  1779. mddev->bitmap_info.max_write_behind = backlog;
  1780. return len;
  1781. }
  1782. static struct md_sysfs_entry bitmap_backlog =
  1783. __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
  1784. static ssize_t
  1785. chunksize_show(mddev_t *mddev, char *page)
  1786. {
  1787. return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
  1788. }
  1789. static ssize_t
  1790. chunksize_store(mddev_t *mddev, const char *buf, size_t len)
  1791. {
  1792. /* Can only be changed when no bitmap is active */
  1793. int rv;
  1794. unsigned long csize;
  1795. if (mddev->bitmap)
  1796. return -EBUSY;
  1797. rv = strict_strtoul(buf, 10, &csize);
  1798. if (rv)
  1799. return rv;
  1800. if (csize < 512 ||
  1801. !is_power_of_2(csize))
  1802. return -EINVAL;
  1803. mddev->bitmap_info.chunksize = csize;
  1804. return len;
  1805. }
  1806. static struct md_sysfs_entry bitmap_chunksize =
  1807. __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
  1808. static ssize_t metadata_show(mddev_t *mddev, char *page)
  1809. {
  1810. return sprintf(page, "%s\n", (mddev->bitmap_info.external
  1811. ? "external" : "internal"));
  1812. }
  1813. static ssize_t metadata_store(mddev_t *mddev, const char *buf, size_t len)
  1814. {
  1815. if (mddev->bitmap ||
  1816. mddev->bitmap_info.file ||
  1817. mddev->bitmap_info.offset)
  1818. return -EBUSY;
  1819. if (strncmp(buf, "external", 8) == 0)
  1820. mddev->bitmap_info.external = 1;
  1821. else if (strncmp(buf, "internal", 8) == 0)
  1822. mddev->bitmap_info.external = 0;
  1823. else
  1824. return -EINVAL;
  1825. return len;
  1826. }
  1827. static struct md_sysfs_entry bitmap_metadata =
  1828. __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
  1829. static ssize_t can_clear_show(mddev_t *mddev, char *page)
  1830. {
  1831. int len;
  1832. if (mddev->bitmap)
  1833. len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
  1834. "false" : "true"));
  1835. else
  1836. len = sprintf(page, "\n");
  1837. return len;
  1838. }
  1839. static ssize_t can_clear_store(mddev_t *mddev, const char *buf, size_t len)
  1840. {
  1841. if (mddev->bitmap == NULL)
  1842. return -ENOENT;
  1843. if (strncmp(buf, "false", 5) == 0)
  1844. mddev->bitmap->need_sync = 1;
  1845. else if (strncmp(buf, "true", 4) == 0) {
  1846. if (mddev->degraded)
  1847. return -EBUSY;
  1848. mddev->bitmap->need_sync = 0;
  1849. } else
  1850. return -EINVAL;
  1851. return len;
  1852. }
  1853. static struct md_sysfs_entry bitmap_can_clear =
  1854. __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
  1855. static ssize_t
  1856. behind_writes_used_show(mddev_t *mddev, char *page)
  1857. {
  1858. if (mddev->bitmap == NULL)
  1859. return sprintf(page, "0\n");
  1860. return sprintf(page, "%lu\n",
  1861. mddev->bitmap->behind_writes_used);
  1862. }
  1863. static ssize_t
  1864. behind_writes_used_reset(mddev_t *mddev, const char *buf, size_t len)
  1865. {
  1866. if (mddev->bitmap)
  1867. mddev->bitmap->behind_writes_used = 0;
  1868. return len;
  1869. }
  1870. static struct md_sysfs_entry max_backlog_used =
  1871. __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
  1872. behind_writes_used_show, behind_writes_used_reset);
  1873. static struct attribute *md_bitmap_attrs[] = {
  1874. &bitmap_location.attr,
  1875. &bitmap_timeout.attr,
  1876. &bitmap_backlog.attr,
  1877. &bitmap_chunksize.attr,
  1878. &bitmap_metadata.attr,
  1879. &bitmap_can_clear.attr,
  1880. &max_backlog_used.attr,
  1881. NULL
  1882. };
  1883. struct attribute_group md_bitmap_group = {
  1884. .name = "bitmap",
  1885. .attrs = md_bitmap_attrs,
  1886. };