bitmap.c 56 KB

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