md-bitmap.c 69 KB

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