bitmap.c 53 KB

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