md-bitmap.c 80 KB

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