md-bitmap.c 81 KB

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