bitmap.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194
  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_STALE);
  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. sb->state |= cpu_to_le32(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. enum bitmap_mask_op {
  570. MASK_SET,
  571. MASK_UNSET
  572. };
  573. /* record the state of the bitmap in the superblock. Return the old value */
  574. static int bitmap_mask_state(struct bitmap *bitmap, enum bitmap_state bits,
  575. enum bitmap_mask_op op)
  576. {
  577. bitmap_super_t *sb;
  578. int old;
  579. if (!bitmap->storage.sb_page) /* can't set the state */
  580. return 0;
  581. sb = kmap_atomic(bitmap->storage.sb_page);
  582. old = le32_to_cpu(sb->state) & bits;
  583. switch (op) {
  584. case MASK_SET:
  585. sb->state |= cpu_to_le32(bits);
  586. bitmap->flags |= bits;
  587. break;
  588. case MASK_UNSET:
  589. sb->state &= cpu_to_le32(~bits);
  590. bitmap->flags &= ~bits;
  591. break;
  592. default:
  593. BUG();
  594. }
  595. kunmap_atomic(sb);
  596. return old;
  597. }
  598. /*
  599. * general bitmap file operations
  600. */
  601. /*
  602. * on-disk bitmap:
  603. *
  604. * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
  605. * file a page at a time. There's a superblock at the start of the file.
  606. */
  607. /* calculate the index of the page that contains this bit */
  608. static inline unsigned long file_page_index(struct bitmap_storage *store,
  609. unsigned long chunk)
  610. {
  611. if (store->sb_page)
  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_storage *store,
  617. unsigned long chunk)
  618. {
  619. if (store->sb_page)
  620. chunk += sizeof(bitmap_super_t) << 3;
  621. return chunk & (PAGE_BITS - 1);
  622. }
  623. /*
  624. * return a pointer to the page in the filemap that contains the given bit
  625. *
  626. * this lookup is complicated by the fact that the bitmap sb might be exactly
  627. * 1 page (e.g., x86) or less than 1 page -- so the bitmap might start on page
  628. * 0 or page 1
  629. */
  630. static inline struct page *filemap_get_page(struct bitmap_storage *store,
  631. unsigned long chunk)
  632. {
  633. if (file_page_index(store, chunk) >= store->file_pages)
  634. return NULL;
  635. return store->filemap[file_page_index(store, chunk)
  636. - file_page_index(store, 0)];
  637. }
  638. static int bitmap_storage_alloc(struct bitmap_storage *store,
  639. unsigned long chunks, int with_super)
  640. {
  641. int pnum;
  642. unsigned long num_pages;
  643. unsigned long bytes;
  644. bytes = DIV_ROUND_UP(chunks, 8);
  645. if (with_super)
  646. bytes += sizeof(bitmap_super_t);
  647. num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
  648. store->filemap = kmalloc(sizeof(struct page *)
  649. * num_pages, GFP_KERNEL);
  650. if (!store->filemap)
  651. return -ENOMEM;
  652. if (with_super && !store->sb_page) {
  653. store->sb_page = alloc_page(GFP_KERNEL);
  654. if (store->sb_page == NULL)
  655. return -ENOMEM;
  656. store->sb_page->index = 0;
  657. }
  658. pnum = 0;
  659. if (store->sb_page) {
  660. store->filemap[0] = store->sb_page;
  661. pnum = 1;
  662. }
  663. for ( ; pnum < num_pages; pnum++) {
  664. store->filemap[pnum] = alloc_page(GFP_KERNEL);
  665. if (!store->filemap[pnum]) {
  666. store->file_pages = pnum;
  667. return -ENOMEM;
  668. }
  669. store->filemap[pnum]->index = pnum;
  670. }
  671. store->file_pages = pnum;
  672. /* We need 4 bits per page, rounded up to a multiple
  673. * of sizeof(unsigned long) */
  674. store->filemap_attr = kzalloc(
  675. roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
  676. GFP_KERNEL);
  677. if (!store->filemap_attr)
  678. return -ENOMEM;
  679. store->bytes = bytes;
  680. return 0;
  681. }
  682. static void bitmap_file_unmap(struct bitmap *bitmap)
  683. {
  684. struct page **map, *sb_page;
  685. unsigned long *attr;
  686. int pages;
  687. unsigned long flags;
  688. struct bitmap_storage *store = &bitmap->storage;
  689. spin_lock_irqsave(&bitmap->lock, flags);
  690. map = store->filemap;
  691. store->filemap = NULL;
  692. attr = store->filemap_attr;
  693. store->filemap_attr = NULL;
  694. pages = store->file_pages;
  695. store->file_pages = 0;
  696. sb_page = store->sb_page;
  697. store->sb_page = NULL;
  698. spin_unlock_irqrestore(&bitmap->lock, flags);
  699. while (pages--)
  700. if (map[pages] != sb_page) /* 0 is sb_page, release it below */
  701. free_buffers(map[pages]);
  702. kfree(map);
  703. kfree(attr);
  704. if (sb_page)
  705. free_buffers(sb_page);
  706. }
  707. static void bitmap_file_put(struct bitmap *bitmap)
  708. {
  709. struct file *file;
  710. unsigned long flags;
  711. spin_lock_irqsave(&bitmap->lock, flags);
  712. file = bitmap->storage.file;
  713. bitmap->storage.file = NULL;
  714. spin_unlock_irqrestore(&bitmap->lock, flags);
  715. if (file)
  716. wait_event(bitmap->write_wait,
  717. atomic_read(&bitmap->pending_writes)==0);
  718. bitmap_file_unmap(bitmap);
  719. if (file) {
  720. struct inode *inode = file->f_path.dentry->d_inode;
  721. invalidate_mapping_pages(inode->i_mapping, 0, -1);
  722. fput(file);
  723. }
  724. }
  725. /*
  726. * bitmap_file_kick - if an error occurs while manipulating the bitmap file
  727. * then it is no longer reliable, so we stop using it and we mark the file
  728. * as failed in the superblock
  729. */
  730. static void bitmap_file_kick(struct bitmap *bitmap)
  731. {
  732. char *path, *ptr = NULL;
  733. if (bitmap_mask_state(bitmap, BITMAP_STALE, MASK_SET) == 0) {
  734. bitmap_update_sb(bitmap);
  735. if (bitmap->storage.file) {
  736. path = kmalloc(PAGE_SIZE, GFP_KERNEL);
  737. if (path)
  738. ptr = d_path(&bitmap->storage.file->f_path,
  739. path, PAGE_SIZE);
  740. printk(KERN_ALERT
  741. "%s: kicking failed bitmap file %s from array!\n",
  742. bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
  743. kfree(path);
  744. } else
  745. printk(KERN_ALERT
  746. "%s: disabling internal bitmap due to errors\n",
  747. bmname(bitmap));
  748. }
  749. bitmap_file_put(bitmap);
  750. return;
  751. }
  752. enum bitmap_page_attr {
  753. BITMAP_PAGE_DIRTY = 0, /* there are set bits that need to be synced */
  754. BITMAP_PAGE_PENDING = 1, /* there are bits that are being cleaned.
  755. * i.e. counter is 1 or 2. */
  756. BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
  757. };
  758. static inline void set_page_attr(struct bitmap *bitmap, int pnum,
  759. enum bitmap_page_attr attr)
  760. {
  761. __set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
  762. }
  763. static inline void clear_page_attr(struct bitmap *bitmap, int pnum,
  764. enum bitmap_page_attr attr)
  765. {
  766. __clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
  767. }
  768. static inline unsigned long test_page_attr(struct bitmap *bitmap, int pnum,
  769. enum bitmap_page_attr attr)
  770. {
  771. return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
  772. }
  773. /*
  774. * bitmap_file_set_bit -- called before performing a write to the md device
  775. * to set (and eventually sync) a particular bit in the bitmap file
  776. *
  777. * we set the bit immediately, then we record the page number so that
  778. * when an unplug occurs, we can flush the dirty pages out to disk
  779. */
  780. static void bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
  781. {
  782. unsigned long bit;
  783. struct page *page;
  784. void *kaddr;
  785. unsigned long chunk = block >> bitmap->chunkshift;
  786. page = filemap_get_page(&bitmap->storage, chunk);
  787. if (!page)
  788. return;
  789. bit = file_page_offset(&bitmap->storage, chunk);
  790. /* set the bit */
  791. kaddr = kmap_atomic(page);
  792. if (bitmap->flags & BITMAP_HOSTENDIAN)
  793. set_bit(bit, kaddr);
  794. else
  795. __set_bit_le(bit, kaddr);
  796. kunmap_atomic(kaddr);
  797. pr_debug("set file bit %lu page %lu\n", bit, page->index);
  798. /* record page number so it gets flushed to disk when unplug occurs */
  799. set_page_attr(bitmap, page->index, BITMAP_PAGE_DIRTY);
  800. }
  801. static void bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block)
  802. {
  803. unsigned long bit;
  804. struct page *page;
  805. void *paddr;
  806. unsigned long chunk = block >> bitmap->chunkshift;
  807. page = filemap_get_page(&bitmap->storage, chunk);
  808. if (!page)
  809. return;
  810. bit = file_page_offset(&bitmap->storage, chunk);
  811. paddr = kmap_atomic(page);
  812. if (bitmap->flags & BITMAP_HOSTENDIAN)
  813. clear_bit(bit, paddr);
  814. else
  815. __clear_bit_le(bit, paddr);
  816. kunmap_atomic(paddr);
  817. if (!test_page_attr(bitmap, page->index, BITMAP_PAGE_NEEDWRITE)) {
  818. set_page_attr(bitmap, page->index, BITMAP_PAGE_PENDING);
  819. bitmap->allclean = 0;
  820. }
  821. }
  822. /* this gets called when the md device is ready to unplug its underlying
  823. * (slave) device queues -- before we let any writes go down, we need to
  824. * sync the dirty pages of the bitmap file to disk */
  825. void bitmap_unplug(struct bitmap *bitmap)
  826. {
  827. unsigned long i, flags;
  828. int dirty, need_write;
  829. int wait = 0;
  830. if (!bitmap || !bitmap->storage.filemap)
  831. return;
  832. /* look at each page to see if there are any set bits that need to be
  833. * flushed out to disk */
  834. for (i = 0; i < bitmap->storage.file_pages; i++) {
  835. spin_lock_irqsave(&bitmap->lock, flags);
  836. if (!bitmap->storage.filemap) {
  837. spin_unlock_irqrestore(&bitmap->lock, flags);
  838. return;
  839. }
  840. dirty = test_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
  841. need_write = test_page_attr(bitmap, i, BITMAP_PAGE_NEEDWRITE);
  842. clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
  843. clear_page_attr(bitmap, i, BITMAP_PAGE_NEEDWRITE);
  844. if (dirty || need_write)
  845. clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING);
  846. if (dirty)
  847. wait = 1;
  848. spin_unlock_irqrestore(&bitmap->lock, flags);
  849. if (dirty || need_write)
  850. write_page(bitmap, bitmap->storage.filemap[i], 0);
  851. }
  852. if (wait) { /* if any writes were performed, we need to wait on them */
  853. if (bitmap->storage.file)
  854. wait_event(bitmap->write_wait,
  855. atomic_read(&bitmap->pending_writes)==0);
  856. else
  857. md_super_wait(bitmap->mddev);
  858. }
  859. if (bitmap->flags & BITMAP_WRITE_ERROR)
  860. bitmap_file_kick(bitmap);
  861. }
  862. EXPORT_SYMBOL(bitmap_unplug);
  863. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
  864. /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
  865. * the in-memory bitmap from the on-disk bitmap -- also, sets up the
  866. * memory mapping of the bitmap file
  867. * Special cases:
  868. * if there's no bitmap file, or if the bitmap file had been
  869. * previously kicked from the array, we mark all the bits as
  870. * 1's in order to cause a full resync.
  871. *
  872. * We ignore all bits for sectors that end earlier than 'start'.
  873. * This is used when reading an out-of-date bitmap...
  874. */
  875. static int bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
  876. {
  877. unsigned long i, chunks, index, oldindex, bit;
  878. struct page *page = NULL;
  879. unsigned long bit_cnt = 0;
  880. struct file *file;
  881. unsigned long offset;
  882. int outofdate;
  883. int ret = -ENOSPC;
  884. void *paddr;
  885. struct bitmap_storage *store = &bitmap->storage;
  886. chunks = bitmap->chunks;
  887. file = store->file;
  888. if (!file && !bitmap->mddev->bitmap_info.offset) {
  889. /* No permanent bitmap - fill with '1s'. */
  890. store->filemap = NULL;
  891. store->file_pages = 0;
  892. for (i = 0; i < chunks ; i++) {
  893. /* if the disk bit is set, set the memory bit */
  894. int needed = ((sector_t)(i+1) << (bitmap->chunkshift)
  895. >= start);
  896. bitmap_set_memory_bits(bitmap,
  897. (sector_t)i << bitmap->chunkshift,
  898. needed);
  899. }
  900. return 0;
  901. }
  902. outofdate = bitmap->flags & BITMAP_STALE;
  903. if (outofdate)
  904. printk(KERN_INFO "%s: bitmap file is out of date, doing full "
  905. "recovery\n", bmname(bitmap));
  906. if (file && i_size_read(file->f_mapping->host) < store->bytes) {
  907. printk(KERN_INFO "%s: bitmap file too short %lu < %lu\n",
  908. bmname(bitmap),
  909. (unsigned long) i_size_read(file->f_mapping->host),
  910. store->bytes);
  911. goto err;
  912. }
  913. ret = bitmap_storage_alloc(&bitmap->storage, bitmap->chunks,
  914. !bitmap->mddev->bitmap_info.external);
  915. if (ret)
  916. goto err;
  917. oldindex = ~0L;
  918. offset = 0;
  919. if (!bitmap->mddev->bitmap_info.external)
  920. offset = sizeof(bitmap_super_t);
  921. for (i = 0; i < chunks; i++) {
  922. int b;
  923. index = file_page_index(&bitmap->storage, i);
  924. bit = file_page_offset(&bitmap->storage, i);
  925. if (index != oldindex) { /* this is a new page, read it in */
  926. int count;
  927. /* unmap the old page, we're done with it */
  928. if (index == store->file_pages-1)
  929. count = store->bytes - index * PAGE_SIZE;
  930. else
  931. count = PAGE_SIZE;
  932. page = store->filemap[index];
  933. if (file)
  934. ret = read_page(file, index, bitmap,
  935. count, page);
  936. else
  937. ret = read_sb_page(
  938. bitmap->mddev,
  939. bitmap->mddev->bitmap_info.offset,
  940. page,
  941. index, count);
  942. if (ret)
  943. goto err;
  944. oldindex = index;
  945. if (outofdate) {
  946. /*
  947. * if bitmap is out of date, dirty the
  948. * whole page and write it out
  949. */
  950. paddr = kmap_atomic(page);
  951. memset(paddr + offset, 0xff,
  952. PAGE_SIZE - offset);
  953. kunmap_atomic(paddr);
  954. write_page(bitmap, page, 1);
  955. ret = -EIO;
  956. if (bitmap->flags & BITMAP_WRITE_ERROR)
  957. goto err;
  958. }
  959. }
  960. paddr = kmap_atomic(page);
  961. if (bitmap->flags & BITMAP_HOSTENDIAN)
  962. b = test_bit(bit, paddr);
  963. else
  964. b = test_bit_le(bit, paddr);
  965. kunmap_atomic(paddr);
  966. if (b) {
  967. /* if the disk bit is set, set the memory bit */
  968. int needed = ((sector_t)(i+1) << bitmap->chunkshift
  969. >= start);
  970. bitmap_set_memory_bits(bitmap,
  971. (sector_t)i << bitmap->chunkshift,
  972. needed);
  973. bit_cnt++;
  974. }
  975. offset = 0;
  976. }
  977. printk(KERN_INFO "%s: bitmap initialized from disk: "
  978. "read %lu pages, set %lu of %lu bits\n",
  979. bmname(bitmap), store->file_pages,
  980. bit_cnt, chunks);
  981. return 0;
  982. err:
  983. printk(KERN_INFO "%s: bitmap initialisation failed: %d\n",
  984. bmname(bitmap), ret);
  985. return ret;
  986. }
  987. void bitmap_write_all(struct bitmap *bitmap)
  988. {
  989. /* We don't actually write all bitmap blocks here,
  990. * just flag them as needing to be written
  991. */
  992. int i;
  993. if (!bitmap || !bitmap->storage.filemap)
  994. return;
  995. if (bitmap->storage.file)
  996. /* Only one copy, so nothing needed */
  997. return;
  998. spin_lock_irq(&bitmap->lock);
  999. for (i = 0; i < bitmap->storage.file_pages; i++)
  1000. set_page_attr(bitmap, i,
  1001. BITMAP_PAGE_NEEDWRITE);
  1002. bitmap->allclean = 0;
  1003. spin_unlock_irq(&bitmap->lock);
  1004. }
  1005. static void bitmap_count_page(struct bitmap *bitmap, sector_t offset, int inc)
  1006. {
  1007. sector_t chunk = offset >> bitmap->chunkshift;
  1008. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1009. bitmap->bp[page].count += inc;
  1010. bitmap_checkfree(bitmap, page);
  1011. }
  1012. static void bitmap_set_pending(struct bitmap *bitmap, sector_t offset)
  1013. {
  1014. sector_t chunk = offset >> bitmap->chunkshift;
  1015. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1016. struct bitmap_page *bp = &bitmap->bp[page];
  1017. if (!bp->pending)
  1018. bp->pending = 1;
  1019. }
  1020. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1021. sector_t offset, sector_t *blocks,
  1022. int create);
  1023. /*
  1024. * bitmap daemon -- periodically wakes up to clean bits and flush pages
  1025. * out to disk
  1026. */
  1027. void bitmap_daemon_work(struct mddev *mddev)
  1028. {
  1029. struct bitmap *bitmap;
  1030. unsigned long j;
  1031. unsigned long nextpage;
  1032. unsigned long flags;
  1033. sector_t blocks;
  1034. /* Use a mutex to guard daemon_work against
  1035. * bitmap_destroy.
  1036. */
  1037. mutex_lock(&mddev->bitmap_info.mutex);
  1038. bitmap = mddev->bitmap;
  1039. if (bitmap == NULL) {
  1040. mutex_unlock(&mddev->bitmap_info.mutex);
  1041. return;
  1042. }
  1043. if (time_before(jiffies, bitmap->daemon_lastrun
  1044. + mddev->bitmap_info.daemon_sleep))
  1045. goto done;
  1046. bitmap->daemon_lastrun = jiffies;
  1047. if (bitmap->allclean) {
  1048. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1049. goto done;
  1050. }
  1051. bitmap->allclean = 1;
  1052. /* Any file-page which is PENDING now needs to be written.
  1053. * So set NEEDWRITE now, then after we make any last-minute changes
  1054. * we will write it.
  1055. */
  1056. spin_lock_irqsave(&bitmap->lock, flags);
  1057. for (j = 0; j < bitmap->storage.file_pages; j++)
  1058. if (test_page_attr(bitmap, j,
  1059. BITMAP_PAGE_PENDING)) {
  1060. set_page_attr(bitmap, j,
  1061. BITMAP_PAGE_NEEDWRITE);
  1062. clear_page_attr(bitmap, j,
  1063. BITMAP_PAGE_PENDING);
  1064. }
  1065. if (bitmap->need_sync &&
  1066. mddev->bitmap_info.external == 0) {
  1067. /* Arrange for superblock update as well as
  1068. * other changes */
  1069. bitmap_super_t *sb;
  1070. bitmap->need_sync = 0;
  1071. if (bitmap->storage.filemap) {
  1072. sb = kmap_atomic(bitmap->storage.sb_page);
  1073. sb->events_cleared =
  1074. cpu_to_le64(bitmap->events_cleared);
  1075. kunmap_atomic(sb);
  1076. set_page_attr(bitmap, 0,
  1077. BITMAP_PAGE_NEEDWRITE);
  1078. }
  1079. }
  1080. /* Now look at the bitmap counters and if any are '2' or '1',
  1081. * decrement and handle accordingly.
  1082. */
  1083. nextpage = 0;
  1084. for (j = 0; j < bitmap->chunks; j++) {
  1085. bitmap_counter_t *bmc;
  1086. sector_t block = (sector_t)j << bitmap->chunkshift;
  1087. if (j == nextpage) {
  1088. nextpage += PAGE_COUNTER_RATIO;
  1089. if (!bitmap->bp[j >> PAGE_COUNTER_SHIFT].pending) {
  1090. j |= PAGE_COUNTER_MASK;
  1091. continue;
  1092. }
  1093. bitmap->bp[j >> PAGE_COUNTER_SHIFT].pending = 0;
  1094. }
  1095. bmc = bitmap_get_counter(bitmap,
  1096. block,
  1097. &blocks, 0);
  1098. if (!bmc) {
  1099. j |= PAGE_COUNTER_MASK;
  1100. continue;
  1101. }
  1102. if (*bmc == 1 && !bitmap->need_sync) {
  1103. /* We can clear the bit */
  1104. *bmc = 0;
  1105. bitmap_count_page(bitmap, block, -1);
  1106. bitmap_file_clear_bit(bitmap, block);
  1107. } else if (*bmc && *bmc <= 2) {
  1108. *bmc = 1;
  1109. bitmap_set_pending(bitmap, block);
  1110. bitmap->allclean = 0;
  1111. }
  1112. }
  1113. /* Now start writeout on any page in NEEDWRITE that isn't DIRTY.
  1114. * DIRTY pages need to be written by bitmap_unplug so it can wait
  1115. * for them.
  1116. * If we find any DIRTY page we stop there and let bitmap_unplug
  1117. * handle all the rest. This is important in the case where
  1118. * the first blocking holds the superblock and it has been updated.
  1119. * We mustn't write any other blocks before the superblock.
  1120. */
  1121. for (j = 0; j < bitmap->storage.file_pages; j++) {
  1122. if (test_page_attr(bitmap, j,
  1123. BITMAP_PAGE_DIRTY))
  1124. /* bitmap_unplug will handle the rest */
  1125. break;
  1126. if (test_page_attr(bitmap, j,
  1127. BITMAP_PAGE_NEEDWRITE)) {
  1128. clear_page_attr(bitmap, j,
  1129. BITMAP_PAGE_NEEDWRITE);
  1130. spin_unlock_irqrestore(&bitmap->lock, flags);
  1131. write_page(bitmap, bitmap->storage.filemap[j], 0);
  1132. spin_lock_irqsave(&bitmap->lock, flags);
  1133. if (!bitmap->storage.filemap)
  1134. break;
  1135. }
  1136. }
  1137. spin_unlock_irqrestore(&bitmap->lock, flags);
  1138. done:
  1139. if (bitmap->allclean == 0)
  1140. mddev->thread->timeout =
  1141. mddev->bitmap_info.daemon_sleep;
  1142. mutex_unlock(&mddev->bitmap_info.mutex);
  1143. }
  1144. static bitmap_counter_t *bitmap_get_counter(struct bitmap *bitmap,
  1145. sector_t offset, sector_t *blocks,
  1146. int create)
  1147. __releases(bitmap->lock)
  1148. __acquires(bitmap->lock)
  1149. {
  1150. /* If 'create', we might release the lock and reclaim it.
  1151. * The lock must have been taken with interrupts enabled.
  1152. * If !create, we don't release the lock.
  1153. */
  1154. sector_t chunk = offset >> bitmap->chunkshift;
  1155. unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
  1156. unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
  1157. sector_t csize;
  1158. int err;
  1159. err = bitmap_checkpage(bitmap, page, create);
  1160. if (bitmap->bp[page].hijacked ||
  1161. bitmap->bp[page].map == NULL)
  1162. csize = ((sector_t)1) << (bitmap->chunkshift +
  1163. PAGE_COUNTER_SHIFT - 1);
  1164. else
  1165. csize = ((sector_t)1) << bitmap->chunkshift;
  1166. *blocks = csize - (offset & (csize - 1));
  1167. if (err < 0)
  1168. return NULL;
  1169. /* now locked ... */
  1170. if (bitmap->bp[page].hijacked) { /* hijacked pointer */
  1171. /* should we use the first or second counter field
  1172. * of the hijacked pointer? */
  1173. int hi = (pageoff > PAGE_COUNTER_MASK);
  1174. return &((bitmap_counter_t *)
  1175. &bitmap->bp[page].map)[hi];
  1176. } else /* page is allocated */
  1177. return (bitmap_counter_t *)
  1178. &(bitmap->bp[page].map[pageoff]);
  1179. }
  1180. int bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
  1181. {
  1182. if (!bitmap)
  1183. return 0;
  1184. if (behind) {
  1185. int bw;
  1186. atomic_inc(&bitmap->behind_writes);
  1187. bw = atomic_read(&bitmap->behind_writes);
  1188. if (bw > bitmap->behind_writes_used)
  1189. bitmap->behind_writes_used = bw;
  1190. pr_debug("inc write-behind count %d/%lu\n",
  1191. bw, bitmap->mddev->bitmap_info.max_write_behind);
  1192. }
  1193. while (sectors) {
  1194. sector_t blocks;
  1195. bitmap_counter_t *bmc;
  1196. spin_lock_irq(&bitmap->lock);
  1197. bmc = bitmap_get_counter(bitmap, offset, &blocks, 1);
  1198. if (!bmc) {
  1199. spin_unlock_irq(&bitmap->lock);
  1200. return 0;
  1201. }
  1202. if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
  1203. DEFINE_WAIT(__wait);
  1204. /* note that it is safe to do the prepare_to_wait
  1205. * after the test as long as we do it before dropping
  1206. * the spinlock.
  1207. */
  1208. prepare_to_wait(&bitmap->overflow_wait, &__wait,
  1209. TASK_UNINTERRUPTIBLE);
  1210. spin_unlock_irq(&bitmap->lock);
  1211. io_schedule();
  1212. finish_wait(&bitmap->overflow_wait, &__wait);
  1213. continue;
  1214. }
  1215. switch (*bmc) {
  1216. case 0:
  1217. bitmap_file_set_bit(bitmap, offset);
  1218. bitmap_count_page(bitmap, offset, 1);
  1219. /* fall through */
  1220. case 1:
  1221. *bmc = 2;
  1222. }
  1223. (*bmc)++;
  1224. spin_unlock_irq(&bitmap->lock);
  1225. offset += blocks;
  1226. if (sectors > blocks)
  1227. sectors -= blocks;
  1228. else
  1229. sectors = 0;
  1230. }
  1231. return 0;
  1232. }
  1233. EXPORT_SYMBOL(bitmap_startwrite);
  1234. void bitmap_endwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors,
  1235. int success, int behind)
  1236. {
  1237. if (!bitmap)
  1238. return;
  1239. if (behind) {
  1240. if (atomic_dec_and_test(&bitmap->behind_writes))
  1241. wake_up(&bitmap->behind_wait);
  1242. pr_debug("dec write-behind count %d/%lu\n",
  1243. atomic_read(&bitmap->behind_writes),
  1244. bitmap->mddev->bitmap_info.max_write_behind);
  1245. }
  1246. while (sectors) {
  1247. sector_t blocks;
  1248. unsigned long flags;
  1249. bitmap_counter_t *bmc;
  1250. spin_lock_irqsave(&bitmap->lock, flags);
  1251. bmc = bitmap_get_counter(bitmap, offset, &blocks, 0);
  1252. if (!bmc) {
  1253. spin_unlock_irqrestore(&bitmap->lock, flags);
  1254. return;
  1255. }
  1256. if (success && !bitmap->mddev->degraded &&
  1257. bitmap->events_cleared < bitmap->mddev->events) {
  1258. bitmap->events_cleared = bitmap->mddev->events;
  1259. bitmap->need_sync = 1;
  1260. sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
  1261. }
  1262. if (!success && !NEEDED(*bmc))
  1263. *bmc |= NEEDED_MASK;
  1264. if (COUNTER(*bmc) == COUNTER_MAX)
  1265. wake_up(&bitmap->overflow_wait);
  1266. (*bmc)--;
  1267. if (*bmc <= 2) {
  1268. bitmap_set_pending(bitmap, offset);
  1269. bitmap->allclean = 0;
  1270. }
  1271. spin_unlock_irqrestore(&bitmap->lock, flags);
  1272. offset += blocks;
  1273. if (sectors > blocks)
  1274. sectors -= blocks;
  1275. else
  1276. sectors = 0;
  1277. }
  1278. }
  1279. EXPORT_SYMBOL(bitmap_endwrite);
  1280. static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1281. int degraded)
  1282. {
  1283. bitmap_counter_t *bmc;
  1284. int rv;
  1285. if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
  1286. *blocks = 1024;
  1287. return 1; /* always resync if no bitmap */
  1288. }
  1289. spin_lock_irq(&bitmap->lock);
  1290. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1291. rv = 0;
  1292. if (bmc) {
  1293. /* locked */
  1294. if (RESYNC(*bmc))
  1295. rv = 1;
  1296. else if (NEEDED(*bmc)) {
  1297. rv = 1;
  1298. if (!degraded) { /* don't set/clear bits if degraded */
  1299. *bmc |= RESYNC_MASK;
  1300. *bmc &= ~NEEDED_MASK;
  1301. }
  1302. }
  1303. }
  1304. spin_unlock_irq(&bitmap->lock);
  1305. return rv;
  1306. }
  1307. int bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
  1308. int degraded)
  1309. {
  1310. /* bitmap_start_sync must always report on multiples of whole
  1311. * pages, otherwise resync (which is very PAGE_SIZE based) will
  1312. * get confused.
  1313. * So call __bitmap_start_sync repeatedly (if needed) until
  1314. * At least PAGE_SIZE>>9 blocks are covered.
  1315. * Return the 'or' of the result.
  1316. */
  1317. int rv = 0;
  1318. sector_t blocks1;
  1319. *blocks = 0;
  1320. while (*blocks < (PAGE_SIZE>>9)) {
  1321. rv |= __bitmap_start_sync(bitmap, offset,
  1322. &blocks1, degraded);
  1323. offset += blocks1;
  1324. *blocks += blocks1;
  1325. }
  1326. return rv;
  1327. }
  1328. EXPORT_SYMBOL(bitmap_start_sync);
  1329. void bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
  1330. {
  1331. bitmap_counter_t *bmc;
  1332. unsigned long flags;
  1333. if (bitmap == NULL) {
  1334. *blocks = 1024;
  1335. return;
  1336. }
  1337. spin_lock_irqsave(&bitmap->lock, flags);
  1338. bmc = bitmap_get_counter(bitmap, offset, blocks, 0);
  1339. if (bmc == NULL)
  1340. goto unlock;
  1341. /* locked */
  1342. if (RESYNC(*bmc)) {
  1343. *bmc &= ~RESYNC_MASK;
  1344. if (!NEEDED(*bmc) && aborted)
  1345. *bmc |= NEEDED_MASK;
  1346. else {
  1347. if (*bmc <= 2) {
  1348. bitmap_set_pending(bitmap, offset);
  1349. bitmap->allclean = 0;
  1350. }
  1351. }
  1352. }
  1353. unlock:
  1354. spin_unlock_irqrestore(&bitmap->lock, flags);
  1355. }
  1356. EXPORT_SYMBOL(bitmap_end_sync);
  1357. void bitmap_close_sync(struct bitmap *bitmap)
  1358. {
  1359. /* Sync has finished, and any bitmap chunks that weren't synced
  1360. * properly have been aborted. It remains to us to clear the
  1361. * RESYNC bit wherever it is still on
  1362. */
  1363. sector_t sector = 0;
  1364. sector_t blocks;
  1365. if (!bitmap)
  1366. return;
  1367. while (sector < bitmap->mddev->resync_max_sectors) {
  1368. bitmap_end_sync(bitmap, sector, &blocks, 0);
  1369. sector += blocks;
  1370. }
  1371. }
  1372. EXPORT_SYMBOL(bitmap_close_sync);
  1373. void bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector)
  1374. {
  1375. sector_t s = 0;
  1376. sector_t blocks;
  1377. if (!bitmap)
  1378. return;
  1379. if (sector == 0) {
  1380. bitmap->last_end_sync = jiffies;
  1381. return;
  1382. }
  1383. if (time_before(jiffies, (bitmap->last_end_sync
  1384. + bitmap->mddev->bitmap_info.daemon_sleep)))
  1385. return;
  1386. wait_event(bitmap->mddev->recovery_wait,
  1387. atomic_read(&bitmap->mddev->recovery_active) == 0);
  1388. bitmap->mddev->curr_resync_completed = sector;
  1389. set_bit(MD_CHANGE_CLEAN, &bitmap->mddev->flags);
  1390. sector &= ~((1ULL << bitmap->chunkshift) - 1);
  1391. s = 0;
  1392. while (s < sector && s < bitmap->mddev->resync_max_sectors) {
  1393. bitmap_end_sync(bitmap, s, &blocks, 0);
  1394. s += blocks;
  1395. }
  1396. bitmap->last_end_sync = jiffies;
  1397. sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
  1398. }
  1399. EXPORT_SYMBOL(bitmap_cond_end_sync);
  1400. static void bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
  1401. {
  1402. /* For each chunk covered by any of these sectors, set the
  1403. * counter to 2 and possibly set resync_needed. They should all
  1404. * be 0 at this point
  1405. */
  1406. sector_t secs;
  1407. bitmap_counter_t *bmc;
  1408. spin_lock_irq(&bitmap->lock);
  1409. bmc = bitmap_get_counter(bitmap, offset, &secs, 1);
  1410. if (!bmc) {
  1411. spin_unlock_irq(&bitmap->lock);
  1412. return;
  1413. }
  1414. if (!*bmc) {
  1415. *bmc = 2 | (needed ? NEEDED_MASK : 0);
  1416. bitmap_count_page(bitmap, offset, 1);
  1417. bitmap_set_pending(bitmap, offset);
  1418. bitmap->allclean = 0;
  1419. }
  1420. spin_unlock_irq(&bitmap->lock);
  1421. }
  1422. /* dirty the memory and file bits for bitmap chunks "s" to "e" */
  1423. void bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
  1424. {
  1425. unsigned long chunk;
  1426. for (chunk = s; chunk <= e; chunk++) {
  1427. sector_t sec = (sector_t)chunk << bitmap->chunkshift;
  1428. bitmap_set_memory_bits(bitmap, sec, 1);
  1429. spin_lock_irq(&bitmap->lock);
  1430. bitmap_file_set_bit(bitmap, sec);
  1431. spin_unlock_irq(&bitmap->lock);
  1432. if (sec < bitmap->mddev->recovery_cp)
  1433. /* We are asserting that the array is dirty,
  1434. * so move the recovery_cp address back so
  1435. * that it is obvious that it is dirty
  1436. */
  1437. bitmap->mddev->recovery_cp = sec;
  1438. }
  1439. }
  1440. /*
  1441. * flush out any pending updates
  1442. */
  1443. void bitmap_flush(struct mddev *mddev)
  1444. {
  1445. struct bitmap *bitmap = mddev->bitmap;
  1446. long sleep;
  1447. if (!bitmap) /* there was no bitmap */
  1448. return;
  1449. /* run the daemon_work three time to ensure everything is flushed
  1450. * that can be
  1451. */
  1452. sleep = mddev->bitmap_info.daemon_sleep * 2;
  1453. bitmap->daemon_lastrun -= sleep;
  1454. bitmap_daemon_work(mddev);
  1455. bitmap->daemon_lastrun -= sleep;
  1456. bitmap_daemon_work(mddev);
  1457. bitmap->daemon_lastrun -= sleep;
  1458. bitmap_daemon_work(mddev);
  1459. bitmap_update_sb(bitmap);
  1460. }
  1461. /*
  1462. * free memory that was allocated
  1463. */
  1464. static void bitmap_free(struct bitmap *bitmap)
  1465. {
  1466. unsigned long k, pages;
  1467. struct bitmap_page *bp;
  1468. if (!bitmap) /* there was no bitmap */
  1469. return;
  1470. /* release the bitmap file and kill the daemon */
  1471. bitmap_file_put(bitmap);
  1472. bp = bitmap->bp;
  1473. pages = bitmap->pages;
  1474. /* free all allocated memory */
  1475. if (bp) /* deallocate the page memory */
  1476. for (k = 0; k < pages; k++)
  1477. if (bp[k].map && !bp[k].hijacked)
  1478. kfree(bp[k].map);
  1479. kfree(bp);
  1480. kfree(bitmap);
  1481. }
  1482. void bitmap_destroy(struct mddev *mddev)
  1483. {
  1484. struct bitmap *bitmap = mddev->bitmap;
  1485. if (!bitmap) /* there was no bitmap */
  1486. return;
  1487. mutex_lock(&mddev->bitmap_info.mutex);
  1488. mddev->bitmap = NULL; /* disconnect from the md device */
  1489. mutex_unlock(&mddev->bitmap_info.mutex);
  1490. if (mddev->thread)
  1491. mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
  1492. if (bitmap->sysfs_can_clear)
  1493. sysfs_put(bitmap->sysfs_can_clear);
  1494. bitmap_free(bitmap);
  1495. }
  1496. /*
  1497. * initialize the bitmap structure
  1498. * if this returns an error, bitmap_destroy must be called to do clean up
  1499. */
  1500. int bitmap_create(struct mddev *mddev)
  1501. {
  1502. struct bitmap *bitmap;
  1503. sector_t blocks = mddev->resync_max_sectors;
  1504. unsigned long chunks;
  1505. unsigned long pages;
  1506. struct file *file = mddev->bitmap_info.file;
  1507. int err;
  1508. struct sysfs_dirent *bm = NULL;
  1509. BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
  1510. BUG_ON(file && mddev->bitmap_info.offset);
  1511. bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
  1512. if (!bitmap)
  1513. return -ENOMEM;
  1514. spin_lock_init(&bitmap->lock);
  1515. atomic_set(&bitmap->pending_writes, 0);
  1516. init_waitqueue_head(&bitmap->write_wait);
  1517. init_waitqueue_head(&bitmap->overflow_wait);
  1518. init_waitqueue_head(&bitmap->behind_wait);
  1519. bitmap->mddev = mddev;
  1520. if (mddev->kobj.sd)
  1521. bm = sysfs_get_dirent(mddev->kobj.sd, NULL, "bitmap");
  1522. if (bm) {
  1523. bitmap->sysfs_can_clear = sysfs_get_dirent(bm, NULL, "can_clear");
  1524. sysfs_put(bm);
  1525. } else
  1526. bitmap->sysfs_can_clear = NULL;
  1527. bitmap->storage.file = file;
  1528. if (file) {
  1529. get_file(file);
  1530. /* As future accesses to this file will use bmap,
  1531. * and bypass the page cache, we must sync the file
  1532. * first.
  1533. */
  1534. vfs_fsync(file, 1);
  1535. }
  1536. /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
  1537. if (!mddev->bitmap_info.external) {
  1538. /*
  1539. * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
  1540. * instructing us to create a new on-disk bitmap instance.
  1541. */
  1542. if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
  1543. err = bitmap_new_disk_sb(bitmap);
  1544. else
  1545. err = bitmap_read_sb(bitmap);
  1546. } else {
  1547. err = 0;
  1548. if (mddev->bitmap_info.chunksize == 0 ||
  1549. mddev->bitmap_info.daemon_sleep == 0)
  1550. /* chunksize and time_base need to be
  1551. * set first. */
  1552. err = -EINVAL;
  1553. }
  1554. if (err)
  1555. goto error;
  1556. bitmap->daemon_lastrun = jiffies;
  1557. bitmap->chunkshift = (ffz(~mddev->bitmap_info.chunksize)
  1558. - BITMAP_BLOCK_SHIFT);
  1559. chunks = (blocks + (1 << bitmap->chunkshift) - 1) >>
  1560. bitmap->chunkshift;
  1561. pages = (chunks + PAGE_COUNTER_RATIO - 1) / PAGE_COUNTER_RATIO;
  1562. BUG_ON(!pages);
  1563. bitmap->chunks = chunks;
  1564. bitmap->pages = pages;
  1565. bitmap->missing_pages = pages;
  1566. bitmap->bp = kzalloc(pages * sizeof(*bitmap->bp), GFP_KERNEL);
  1567. err = -ENOMEM;
  1568. if (!bitmap->bp)
  1569. goto error;
  1570. printk(KERN_INFO "created bitmap (%lu pages) for device %s\n",
  1571. pages, bmname(bitmap));
  1572. mddev->bitmap = bitmap;
  1573. return (bitmap->flags & BITMAP_WRITE_ERROR) ? -EIO : 0;
  1574. error:
  1575. bitmap_free(bitmap);
  1576. return err;
  1577. }
  1578. int bitmap_load(struct mddev *mddev)
  1579. {
  1580. int err = 0;
  1581. sector_t start = 0;
  1582. sector_t sector = 0;
  1583. struct bitmap *bitmap = mddev->bitmap;
  1584. if (!bitmap)
  1585. goto out;
  1586. /* Clear out old bitmap info first: Either there is none, or we
  1587. * are resuming after someone else has possibly changed things,
  1588. * so we should forget old cached info.
  1589. * All chunks should be clean, but some might need_sync.
  1590. */
  1591. while (sector < mddev->resync_max_sectors) {
  1592. sector_t blocks;
  1593. bitmap_start_sync(bitmap, sector, &blocks, 0);
  1594. sector += blocks;
  1595. }
  1596. bitmap_close_sync(bitmap);
  1597. if (mddev->degraded == 0
  1598. || bitmap->events_cleared == mddev->events)
  1599. /* no need to keep dirty bits to optimise a
  1600. * re-add of a missing device */
  1601. start = mddev->recovery_cp;
  1602. mutex_lock(&mddev->bitmap_info.mutex);
  1603. err = bitmap_init_from_disk(bitmap, start);
  1604. mutex_unlock(&mddev->bitmap_info.mutex);
  1605. if (err)
  1606. goto out;
  1607. bitmap_mask_state(bitmap, BITMAP_STALE, MASK_UNSET);
  1608. /* Kick recovery in case any bits were set */
  1609. set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
  1610. mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
  1611. md_wakeup_thread(mddev->thread);
  1612. bitmap_update_sb(bitmap);
  1613. if (bitmap->flags & BITMAP_WRITE_ERROR)
  1614. err = -EIO;
  1615. out:
  1616. return err;
  1617. }
  1618. EXPORT_SYMBOL_GPL(bitmap_load);
  1619. void bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
  1620. {
  1621. unsigned long chunk_kb;
  1622. unsigned long flags;
  1623. if (!bitmap)
  1624. return;
  1625. spin_lock_irqsave(&bitmap->lock, flags);
  1626. chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
  1627. seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
  1628. "%lu%s chunk",
  1629. bitmap->pages - bitmap->missing_pages,
  1630. bitmap->pages,
  1631. (bitmap->pages - bitmap->missing_pages)
  1632. << (PAGE_SHIFT - 10),
  1633. chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
  1634. chunk_kb ? "KB" : "B");
  1635. if (bitmap->storage.file) {
  1636. seq_printf(seq, ", file: ");
  1637. seq_path(seq, &bitmap->storage.file->f_path, " \t\n");
  1638. }
  1639. seq_printf(seq, "\n");
  1640. spin_unlock_irqrestore(&bitmap->lock, flags);
  1641. }
  1642. static ssize_t
  1643. location_show(struct mddev *mddev, char *page)
  1644. {
  1645. ssize_t len;
  1646. if (mddev->bitmap_info.file)
  1647. len = sprintf(page, "file");
  1648. else if (mddev->bitmap_info.offset)
  1649. len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
  1650. else
  1651. len = sprintf(page, "none");
  1652. len += sprintf(page+len, "\n");
  1653. return len;
  1654. }
  1655. static ssize_t
  1656. location_store(struct mddev *mddev, const char *buf, size_t len)
  1657. {
  1658. if (mddev->pers) {
  1659. if (!mddev->pers->quiesce)
  1660. return -EBUSY;
  1661. if (mddev->recovery || mddev->sync_thread)
  1662. return -EBUSY;
  1663. }
  1664. if (mddev->bitmap || mddev->bitmap_info.file ||
  1665. mddev->bitmap_info.offset) {
  1666. /* bitmap already configured. Only option is to clear it */
  1667. if (strncmp(buf, "none", 4) != 0)
  1668. return -EBUSY;
  1669. if (mddev->pers) {
  1670. mddev->pers->quiesce(mddev, 1);
  1671. bitmap_destroy(mddev);
  1672. mddev->pers->quiesce(mddev, 0);
  1673. }
  1674. mddev->bitmap_info.offset = 0;
  1675. if (mddev->bitmap_info.file) {
  1676. struct file *f = mddev->bitmap_info.file;
  1677. mddev->bitmap_info.file = NULL;
  1678. restore_bitmap_write_access(f);
  1679. fput(f);
  1680. }
  1681. } else {
  1682. /* No bitmap, OK to set a location */
  1683. long long offset;
  1684. if (strncmp(buf, "none", 4) == 0)
  1685. /* nothing to be done */;
  1686. else if (strncmp(buf, "file:", 5) == 0) {
  1687. /* Not supported yet */
  1688. return -EINVAL;
  1689. } else {
  1690. int rv;
  1691. if (buf[0] == '+')
  1692. rv = strict_strtoll(buf+1, 10, &offset);
  1693. else
  1694. rv = strict_strtoll(buf, 10, &offset);
  1695. if (rv)
  1696. return rv;
  1697. if (offset == 0)
  1698. return -EINVAL;
  1699. if (mddev->bitmap_info.external == 0 &&
  1700. mddev->major_version == 0 &&
  1701. offset != mddev->bitmap_info.default_offset)
  1702. return -EINVAL;
  1703. mddev->bitmap_info.offset = offset;
  1704. if (mddev->pers) {
  1705. mddev->pers->quiesce(mddev, 1);
  1706. rv = bitmap_create(mddev);
  1707. if (!rv)
  1708. rv = bitmap_load(mddev);
  1709. if (rv) {
  1710. bitmap_destroy(mddev);
  1711. mddev->bitmap_info.offset = 0;
  1712. }
  1713. mddev->pers->quiesce(mddev, 0);
  1714. if (rv)
  1715. return rv;
  1716. }
  1717. }
  1718. }
  1719. if (!mddev->external) {
  1720. /* Ensure new bitmap info is stored in
  1721. * metadata promptly.
  1722. */
  1723. set_bit(MD_CHANGE_DEVS, &mddev->flags);
  1724. md_wakeup_thread(mddev->thread);
  1725. }
  1726. return len;
  1727. }
  1728. static struct md_sysfs_entry bitmap_location =
  1729. __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
  1730. /* 'bitmap/space' is the space available at 'location' for the
  1731. * bitmap. This allows the kernel to know when it is safe to
  1732. * resize the bitmap to match a resized array.
  1733. */
  1734. static ssize_t
  1735. space_show(struct mddev *mddev, char *page)
  1736. {
  1737. return sprintf(page, "%lu\n", mddev->bitmap_info.space);
  1738. }
  1739. static ssize_t
  1740. space_store(struct mddev *mddev, const char *buf, size_t len)
  1741. {
  1742. unsigned long sectors;
  1743. int rv;
  1744. rv = kstrtoul(buf, 10, &sectors);
  1745. if (rv)
  1746. return rv;
  1747. if (sectors == 0)
  1748. return -EINVAL;
  1749. if (mddev->bitmap &&
  1750. sectors < (mddev->bitmap->storage.bytes + 511) >> 9)
  1751. return -EFBIG; /* Bitmap is too big for this small space */
  1752. /* could make sure it isn't too big, but that isn't really
  1753. * needed - user-space should be careful.
  1754. */
  1755. mddev->bitmap_info.space = sectors;
  1756. return len;
  1757. }
  1758. static struct md_sysfs_entry bitmap_space =
  1759. __ATTR(space, S_IRUGO|S_IWUSR, space_show, space_store);
  1760. static ssize_t
  1761. timeout_show(struct mddev *mddev, char *page)
  1762. {
  1763. ssize_t len;
  1764. unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
  1765. unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
  1766. len = sprintf(page, "%lu", secs);
  1767. if (jifs)
  1768. len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
  1769. len += sprintf(page+len, "\n");
  1770. return len;
  1771. }
  1772. static ssize_t
  1773. timeout_store(struct mddev *mddev, const char *buf, size_t len)
  1774. {
  1775. /* timeout can be set at any time */
  1776. unsigned long timeout;
  1777. int rv = strict_strtoul_scaled(buf, &timeout, 4);
  1778. if (rv)
  1779. return rv;
  1780. /* just to make sure we don't overflow... */
  1781. if (timeout >= LONG_MAX / HZ)
  1782. return -EINVAL;
  1783. timeout = timeout * HZ / 10000;
  1784. if (timeout >= MAX_SCHEDULE_TIMEOUT)
  1785. timeout = MAX_SCHEDULE_TIMEOUT-1;
  1786. if (timeout < 1)
  1787. timeout = 1;
  1788. mddev->bitmap_info.daemon_sleep = timeout;
  1789. if (mddev->thread) {
  1790. /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
  1791. * the bitmap is all clean and we don't need to
  1792. * adjust the timeout right now
  1793. */
  1794. if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
  1795. mddev->thread->timeout = timeout;
  1796. md_wakeup_thread(mddev->thread);
  1797. }
  1798. }
  1799. return len;
  1800. }
  1801. static struct md_sysfs_entry bitmap_timeout =
  1802. __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
  1803. static ssize_t
  1804. backlog_show(struct mddev *mddev, char *page)
  1805. {
  1806. return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
  1807. }
  1808. static ssize_t
  1809. backlog_store(struct mddev *mddev, const char *buf, size_t len)
  1810. {
  1811. unsigned long backlog;
  1812. int rv = strict_strtoul(buf, 10, &backlog);
  1813. if (rv)
  1814. return rv;
  1815. if (backlog > COUNTER_MAX)
  1816. return -EINVAL;
  1817. mddev->bitmap_info.max_write_behind = backlog;
  1818. return len;
  1819. }
  1820. static struct md_sysfs_entry bitmap_backlog =
  1821. __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
  1822. static ssize_t
  1823. chunksize_show(struct mddev *mddev, char *page)
  1824. {
  1825. return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
  1826. }
  1827. static ssize_t
  1828. chunksize_store(struct mddev *mddev, const char *buf, size_t len)
  1829. {
  1830. /* Can only be changed when no bitmap is active */
  1831. int rv;
  1832. unsigned long csize;
  1833. if (mddev->bitmap)
  1834. return -EBUSY;
  1835. rv = strict_strtoul(buf, 10, &csize);
  1836. if (rv)
  1837. return rv;
  1838. if (csize < 512 ||
  1839. !is_power_of_2(csize))
  1840. return -EINVAL;
  1841. mddev->bitmap_info.chunksize = csize;
  1842. return len;
  1843. }
  1844. static struct md_sysfs_entry bitmap_chunksize =
  1845. __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
  1846. static ssize_t metadata_show(struct mddev *mddev, char *page)
  1847. {
  1848. return sprintf(page, "%s\n", (mddev->bitmap_info.external
  1849. ? "external" : "internal"));
  1850. }
  1851. static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
  1852. {
  1853. if (mddev->bitmap ||
  1854. mddev->bitmap_info.file ||
  1855. mddev->bitmap_info.offset)
  1856. return -EBUSY;
  1857. if (strncmp(buf, "external", 8) == 0)
  1858. mddev->bitmap_info.external = 1;
  1859. else if (strncmp(buf, "internal", 8) == 0)
  1860. mddev->bitmap_info.external = 0;
  1861. else
  1862. return -EINVAL;
  1863. return len;
  1864. }
  1865. static struct md_sysfs_entry bitmap_metadata =
  1866. __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
  1867. static ssize_t can_clear_show(struct mddev *mddev, char *page)
  1868. {
  1869. int len;
  1870. if (mddev->bitmap)
  1871. len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
  1872. "false" : "true"));
  1873. else
  1874. len = sprintf(page, "\n");
  1875. return len;
  1876. }
  1877. static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
  1878. {
  1879. if (mddev->bitmap == NULL)
  1880. return -ENOENT;
  1881. if (strncmp(buf, "false", 5) == 0)
  1882. mddev->bitmap->need_sync = 1;
  1883. else if (strncmp(buf, "true", 4) == 0) {
  1884. if (mddev->degraded)
  1885. return -EBUSY;
  1886. mddev->bitmap->need_sync = 0;
  1887. } else
  1888. return -EINVAL;
  1889. return len;
  1890. }
  1891. static struct md_sysfs_entry bitmap_can_clear =
  1892. __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
  1893. static ssize_t
  1894. behind_writes_used_show(struct mddev *mddev, char *page)
  1895. {
  1896. if (mddev->bitmap == NULL)
  1897. return sprintf(page, "0\n");
  1898. return sprintf(page, "%lu\n",
  1899. mddev->bitmap->behind_writes_used);
  1900. }
  1901. static ssize_t
  1902. behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
  1903. {
  1904. if (mddev->bitmap)
  1905. mddev->bitmap->behind_writes_used = 0;
  1906. return len;
  1907. }
  1908. static struct md_sysfs_entry max_backlog_used =
  1909. __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
  1910. behind_writes_used_show, behind_writes_used_reset);
  1911. static struct attribute *md_bitmap_attrs[] = {
  1912. &bitmap_location.attr,
  1913. &bitmap_space.attr,
  1914. &bitmap_timeout.attr,
  1915. &bitmap_backlog.attr,
  1916. &bitmap_chunksize.attr,
  1917. &bitmap_metadata.attr,
  1918. &bitmap_can_clear.attr,
  1919. &max_backlog_used.attr,
  1920. NULL
  1921. };
  1922. struct attribute_group md_bitmap_group = {
  1923. .name = "bitmap",
  1924. .attrs = md_bitmap_attrs,
  1925. };