bitmap.c 55 KB

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