bitmap.c 67 KB

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