rgrp.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/slab.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/completion.h>
  10. #include <linux/buffer_head.h>
  11. #include <linux/fs.h>
  12. #include <linux/gfs2_ondisk.h>
  13. #include <linux/prefetch.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/rbtree.h>
  16. #include <linux/random.h>
  17. #include "gfs2.h"
  18. #include "incore.h"
  19. #include "glock.h"
  20. #include "glops.h"
  21. #include "lops.h"
  22. #include "meta_io.h"
  23. #include "quota.h"
  24. #include "rgrp.h"
  25. #include "super.h"
  26. #include "trans.h"
  27. #include "util.h"
  28. #include "log.h"
  29. #include "inode.h"
  30. #include "trace_gfs2.h"
  31. #include "dir.h"
  32. #define BFITNOENT ((u32)~0)
  33. #define NO_BLOCK ((u64)~0)
  34. struct gfs2_rbm {
  35. struct gfs2_rgrpd *rgd;
  36. u32 offset; /* The offset is bitmap relative */
  37. int bii; /* Bitmap index */
  38. };
  39. static inline struct gfs2_bitmap *rbm_bi(const struct gfs2_rbm *rbm)
  40. {
  41. return rbm->rgd->rd_bits + rbm->bii;
  42. }
  43. static inline u64 gfs2_rbm_to_block(const struct gfs2_rbm *rbm)
  44. {
  45. BUG_ON(rbm->offset >= rbm->rgd->rd_data);
  46. return rbm->rgd->rd_data0 + (rbm_bi(rbm)->bi_start * GFS2_NBBY) +
  47. rbm->offset;
  48. }
  49. /*
  50. * These routines are used by the resource group routines (rgrp.c)
  51. * to keep track of block allocation. Each block is represented by two
  52. * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
  53. *
  54. * 0 = Free
  55. * 1 = Used (not metadata)
  56. * 2 = Unlinked (still in use) inode
  57. * 3 = Used (metadata)
  58. */
  59. struct gfs2_extent {
  60. struct gfs2_rbm rbm;
  61. u32 len;
  62. };
  63. static const char valid_change[16] = {
  64. /* current */
  65. /* n */ 0, 1, 1, 1,
  66. /* e */ 1, 0, 0, 0,
  67. /* w */ 0, 0, 0, 1,
  68. 1, 0, 0, 0
  69. };
  70. static int gfs2_rbm_find(struct gfs2_rbm *rbm, u8 state, u32 *minext,
  71. struct gfs2_blkreserv *rs, bool nowrap);
  72. /**
  73. * gfs2_setbit - Set a bit in the bitmaps
  74. * @rbm: The position of the bit to set
  75. * @do_clone: Also set the clone bitmap, if it exists
  76. * @new_state: the new state of the block
  77. *
  78. */
  79. static inline void gfs2_setbit(const struct gfs2_rbm *rbm, bool do_clone,
  80. unsigned char new_state)
  81. {
  82. unsigned char *byte1, *byte2, *end, cur_state;
  83. struct gfs2_bitmap *bi = rbm_bi(rbm);
  84. unsigned int buflen = bi->bi_bytes;
  85. const unsigned int bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE;
  86. byte1 = bi->bi_bh->b_data + bi->bi_offset + (rbm->offset / GFS2_NBBY);
  87. end = bi->bi_bh->b_data + bi->bi_offset + buflen;
  88. BUG_ON(byte1 >= end);
  89. cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
  90. if (unlikely(!valid_change[new_state * 4 + cur_state])) {
  91. struct gfs2_sbd *sdp = rbm->rgd->rd_sbd;
  92. fs_warn(sdp, "buf_blk = 0x%x old_state=%d, new_state=%d\n",
  93. rbm->offset, cur_state, new_state);
  94. fs_warn(sdp, "rgrp=0x%llx bi_start=0x%x biblk: 0x%llx\n",
  95. (unsigned long long)rbm->rgd->rd_addr, bi->bi_start,
  96. (unsigned long long)bi->bi_bh->b_blocknr);
  97. fs_warn(sdp, "bi_offset=0x%x bi_bytes=0x%x block=0x%llx\n",
  98. bi->bi_offset, bi->bi_bytes,
  99. (unsigned long long)gfs2_rbm_to_block(rbm));
  100. dump_stack();
  101. gfs2_consist_rgrpd(rbm->rgd);
  102. return;
  103. }
  104. *byte1 ^= (cur_state ^ new_state) << bit;
  105. if (do_clone && bi->bi_clone) {
  106. byte2 = bi->bi_clone + bi->bi_offset + (rbm->offset / GFS2_NBBY);
  107. cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
  108. *byte2 ^= (cur_state ^ new_state) << bit;
  109. }
  110. }
  111. /**
  112. * gfs2_testbit - test a bit in the bitmaps
  113. * @rbm: The bit to test
  114. * @use_clone: If true, test the clone bitmap, not the official bitmap.
  115. *
  116. * Some callers like gfs2_unaligned_extlen need to test the clone bitmaps,
  117. * not the "real" bitmaps, to avoid allocating recently freed blocks.
  118. *
  119. * Returns: The two bit block state of the requested bit
  120. */
  121. static inline u8 gfs2_testbit(const struct gfs2_rbm *rbm, bool use_clone)
  122. {
  123. struct gfs2_bitmap *bi = rbm_bi(rbm);
  124. const u8 *buffer;
  125. const u8 *byte;
  126. unsigned int bit;
  127. if (use_clone && bi->bi_clone)
  128. buffer = bi->bi_clone;
  129. else
  130. buffer = bi->bi_bh->b_data;
  131. buffer += bi->bi_offset;
  132. byte = buffer + (rbm->offset / GFS2_NBBY);
  133. bit = (rbm->offset % GFS2_NBBY) * GFS2_BIT_SIZE;
  134. return (*byte >> bit) & GFS2_BIT_MASK;
  135. }
  136. /**
  137. * gfs2_bit_search - search bitmap for a state
  138. * @ptr: Pointer to bitmap data
  139. * @mask: Mask to use (normally 0x55555.... but adjusted for search start)
  140. * @state: The state we are searching for
  141. *
  142. * We xor the bitmap data with a pattern which is the bitwise opposite
  143. * of what we are looking for. This gives rise to a pattern of ones
  144. * wherever there is a match. Since we have two bits per entry, we
  145. * take this pattern, shift it down by one place and then and it with
  146. * the original. All the even bit positions (0,2,4, etc) then represent
  147. * successful matches, so we mask with 0x55555..... to remove the unwanted
  148. * odd bit positions.
  149. *
  150. * This allows searching of a whole u64 at once (32 blocks) with a
  151. * single test (on 64 bit arches).
  152. */
  153. static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
  154. {
  155. u64 tmp;
  156. static const u64 search[] = {
  157. [0] = 0xffffffffffffffffULL,
  158. [1] = 0xaaaaaaaaaaaaaaaaULL,
  159. [2] = 0x5555555555555555ULL,
  160. [3] = 0x0000000000000000ULL,
  161. };
  162. tmp = le64_to_cpu(*ptr) ^ search[state];
  163. tmp &= (tmp >> 1);
  164. tmp &= mask;
  165. return tmp;
  166. }
  167. /**
  168. * rs_cmp - multi-block reservation range compare
  169. * @start: start of the new reservation
  170. * @len: number of blocks in the new reservation
  171. * @rs: existing reservation to compare against
  172. *
  173. * returns: 1 if the block range is beyond the reach of the reservation
  174. * -1 if the block range is before the start of the reservation
  175. * 0 if the block range overlaps with the reservation
  176. */
  177. static inline int rs_cmp(u64 start, u32 len, struct gfs2_blkreserv *rs)
  178. {
  179. if (start >= rs->rs_start + rs->rs_requested)
  180. return 1;
  181. if (rs->rs_start >= start + len)
  182. return -1;
  183. return 0;
  184. }
  185. /**
  186. * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
  187. * a block in a given allocation state.
  188. * @buf: the buffer that holds the bitmaps
  189. * @len: the length (in bytes) of the buffer
  190. * @goal: start search at this block's bit-pair (within @buffer)
  191. * @state: GFS2_BLKST_XXX the state of the block we're looking for.
  192. *
  193. * Scope of @goal and returned block number is only within this bitmap buffer,
  194. * not entire rgrp or filesystem. @buffer will be offset from the actual
  195. * beginning of a bitmap block buffer, skipping any header structures, but
  196. * headers are always a multiple of 64 bits long so that the buffer is
  197. * always aligned to a 64 bit boundary.
  198. *
  199. * The size of the buffer is in bytes, but is it assumed that it is
  200. * always ok to read a complete multiple of 64 bits at the end
  201. * of the block in case the end is no aligned to a natural boundary.
  202. *
  203. * Return: the block number (bitmap buffer scope) that was found
  204. */
  205. static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
  206. u32 goal, u8 state)
  207. {
  208. u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1);
  209. const __le64 *ptr = ((__le64 *)buf) + (goal >> 5);
  210. const __le64 *end = (__le64 *)(buf + ALIGN(len, sizeof(u64)));
  211. u64 tmp;
  212. u64 mask = 0x5555555555555555ULL;
  213. u32 bit;
  214. /* Mask off bits we don't care about at the start of the search */
  215. mask <<= spoint;
  216. tmp = gfs2_bit_search(ptr, mask, state);
  217. ptr++;
  218. while(tmp == 0 && ptr < end) {
  219. tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
  220. ptr++;
  221. }
  222. /* Mask off any bits which are more than len bytes from the start */
  223. if (ptr == end && (len & (sizeof(u64) - 1)))
  224. tmp &= (((u64)~0) >> (64 - 8*(len & (sizeof(u64) - 1))));
  225. /* Didn't find anything, so return */
  226. if (tmp == 0)
  227. return BFITNOENT;
  228. ptr--;
  229. bit = __ffs64(tmp);
  230. bit /= 2; /* two bits per entry in the bitmap */
  231. return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
  232. }
  233. /**
  234. * gfs2_rbm_from_block - Set the rbm based upon rgd and block number
  235. * @rbm: The rbm with rgd already set correctly
  236. * @block: The block number (filesystem relative)
  237. *
  238. * This sets the bi and offset members of an rbm based on a
  239. * resource group and a filesystem relative block number. The
  240. * resource group must be set in the rbm on entry, the bi and
  241. * offset members will be set by this function.
  242. *
  243. * Returns: 0 on success, or an error code
  244. */
  245. static int gfs2_rbm_from_block(struct gfs2_rbm *rbm, u64 block)
  246. {
  247. if (!rgrp_contains_block(rbm->rgd, block))
  248. return -E2BIG;
  249. rbm->bii = 0;
  250. rbm->offset = block - rbm->rgd->rd_data0;
  251. /* Check if the block is within the first block */
  252. if (rbm->offset < rbm_bi(rbm)->bi_blocks)
  253. return 0;
  254. /* Adjust for the size diff between gfs2_meta_header and gfs2_rgrp */
  255. rbm->offset += (sizeof(struct gfs2_rgrp) -
  256. sizeof(struct gfs2_meta_header)) * GFS2_NBBY;
  257. rbm->bii = rbm->offset / rbm->rgd->rd_sbd->sd_blocks_per_bitmap;
  258. rbm->offset -= rbm->bii * rbm->rgd->rd_sbd->sd_blocks_per_bitmap;
  259. return 0;
  260. }
  261. /**
  262. * gfs2_rbm_add - add a number of blocks to an rbm
  263. * @rbm: The rbm with rgd already set correctly
  264. * @blocks: The number of blocks to add to rpm
  265. *
  266. * This function takes an existing rbm structure and adds a number of blocks to
  267. * it.
  268. *
  269. * Returns: True if the new rbm would point past the end of the rgrp.
  270. */
  271. static bool gfs2_rbm_add(struct gfs2_rbm *rbm, u32 blocks)
  272. {
  273. struct gfs2_rgrpd *rgd = rbm->rgd;
  274. struct gfs2_bitmap *bi = rgd->rd_bits + rbm->bii;
  275. if (rbm->offset + blocks < bi->bi_blocks) {
  276. rbm->offset += blocks;
  277. return false;
  278. }
  279. blocks -= bi->bi_blocks - rbm->offset;
  280. for(;;) {
  281. bi++;
  282. if (bi == rgd->rd_bits + rgd->rd_length)
  283. return true;
  284. if (blocks < bi->bi_blocks) {
  285. rbm->offset = blocks;
  286. rbm->bii = bi - rgd->rd_bits;
  287. return false;
  288. }
  289. blocks -= bi->bi_blocks;
  290. }
  291. }
  292. /**
  293. * gfs2_unaligned_extlen - Look for free blocks which are not byte aligned
  294. * @rbm: Position to search (value/result)
  295. * @n_unaligned: Number of unaligned blocks to check
  296. * @len: Decremented for each block found (terminate on zero)
  297. *
  298. * Returns: true if a non-free block is encountered or the end of the resource
  299. * group is reached.
  300. */
  301. static bool gfs2_unaligned_extlen(struct gfs2_rbm *rbm, u32 n_unaligned, u32 *len)
  302. {
  303. u32 n;
  304. u8 res;
  305. for (n = 0; n < n_unaligned; n++) {
  306. res = gfs2_testbit(rbm, true);
  307. if (res != GFS2_BLKST_FREE)
  308. return true;
  309. (*len)--;
  310. if (*len == 0)
  311. return true;
  312. if (gfs2_rbm_add(rbm, 1))
  313. return true;
  314. }
  315. return false;
  316. }
  317. /**
  318. * gfs2_free_extlen - Return extent length of free blocks
  319. * @rrbm: Starting position
  320. * @len: Max length to check
  321. *
  322. * Starting at the block specified by the rbm, see how many free blocks
  323. * there are, not reading more than len blocks ahead. This can be done
  324. * using memchr_inv when the blocks are byte aligned, but has to be done
  325. * on a block by block basis in case of unaligned blocks. Also this
  326. * function can cope with bitmap boundaries (although it must stop on
  327. * a resource group boundary)
  328. *
  329. * Returns: Number of free blocks in the extent
  330. */
  331. static u32 gfs2_free_extlen(const struct gfs2_rbm *rrbm, u32 len)
  332. {
  333. struct gfs2_rbm rbm = *rrbm;
  334. u32 n_unaligned = rbm.offset & 3;
  335. u32 size = len;
  336. u32 bytes;
  337. u32 chunk_size;
  338. u8 *ptr, *start, *end;
  339. u64 block;
  340. struct gfs2_bitmap *bi;
  341. if (n_unaligned &&
  342. gfs2_unaligned_extlen(&rbm, 4 - n_unaligned, &len))
  343. goto out;
  344. n_unaligned = len & 3;
  345. /* Start is now byte aligned */
  346. while (len > 3) {
  347. bi = rbm_bi(&rbm);
  348. start = bi->bi_bh->b_data;
  349. if (bi->bi_clone)
  350. start = bi->bi_clone;
  351. start += bi->bi_offset;
  352. end = start + bi->bi_bytes;
  353. BUG_ON(rbm.offset & 3);
  354. start += (rbm.offset / GFS2_NBBY);
  355. bytes = min_t(u32, len / GFS2_NBBY, (end - start));
  356. ptr = memchr_inv(start, 0, bytes);
  357. chunk_size = ((ptr == NULL) ? bytes : (ptr - start));
  358. chunk_size *= GFS2_NBBY;
  359. BUG_ON(len < chunk_size);
  360. len -= chunk_size;
  361. block = gfs2_rbm_to_block(&rbm);
  362. if (gfs2_rbm_from_block(&rbm, block + chunk_size)) {
  363. n_unaligned = 0;
  364. break;
  365. }
  366. if (ptr) {
  367. n_unaligned = 3;
  368. break;
  369. }
  370. n_unaligned = len & 3;
  371. }
  372. /* Deal with any bits left over at the end */
  373. if (n_unaligned)
  374. gfs2_unaligned_extlen(&rbm, n_unaligned, &len);
  375. out:
  376. return size - len;
  377. }
  378. /**
  379. * gfs2_bitcount - count the number of bits in a certain state
  380. * @rgd: the resource group descriptor
  381. * @buffer: the buffer that holds the bitmaps
  382. * @buflen: the length (in bytes) of the buffer
  383. * @state: the state of the block we're looking for
  384. *
  385. * Returns: The number of bits
  386. */
  387. static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
  388. unsigned int buflen, u8 state)
  389. {
  390. const u8 *byte = buffer;
  391. const u8 *end = buffer + buflen;
  392. const u8 state1 = state << 2;
  393. const u8 state2 = state << 4;
  394. const u8 state3 = state << 6;
  395. u32 count = 0;
  396. for (; byte < end; byte++) {
  397. if (((*byte) & 0x03) == state)
  398. count++;
  399. if (((*byte) & 0x0C) == state1)
  400. count++;
  401. if (((*byte) & 0x30) == state2)
  402. count++;
  403. if (((*byte) & 0xC0) == state3)
  404. count++;
  405. }
  406. return count;
  407. }
  408. /**
  409. * gfs2_rgrp_verify - Verify that a resource group is consistent
  410. * @rgd: the rgrp
  411. *
  412. */
  413. void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
  414. {
  415. struct gfs2_sbd *sdp = rgd->rd_sbd;
  416. struct gfs2_bitmap *bi = NULL;
  417. u32 length = rgd->rd_length;
  418. u32 count[4], tmp;
  419. int buf, x;
  420. memset(count, 0, 4 * sizeof(u32));
  421. /* Count # blocks in each of 4 possible allocation states */
  422. for (buf = 0; buf < length; buf++) {
  423. bi = rgd->rd_bits + buf;
  424. for (x = 0; x < 4; x++)
  425. count[x] += gfs2_bitcount(rgd,
  426. bi->bi_bh->b_data +
  427. bi->bi_offset,
  428. bi->bi_bytes, x);
  429. }
  430. if (count[0] != rgd->rd_free) {
  431. gfs2_lm(sdp, "free data mismatch: %u != %u\n",
  432. count[0], rgd->rd_free);
  433. gfs2_consist_rgrpd(rgd);
  434. return;
  435. }
  436. tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
  437. if (count[1] != tmp) {
  438. gfs2_lm(sdp, "used data mismatch: %u != %u\n",
  439. count[1], tmp);
  440. gfs2_consist_rgrpd(rgd);
  441. return;
  442. }
  443. if (count[2] + count[3] != rgd->rd_dinodes) {
  444. gfs2_lm(sdp, "used metadata mismatch: %u != %u\n",
  445. count[2] + count[3], rgd->rd_dinodes);
  446. gfs2_consist_rgrpd(rgd);
  447. return;
  448. }
  449. }
  450. /**
  451. * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
  452. * @sdp: The GFS2 superblock
  453. * @blk: The data block number
  454. * @exact: True if this needs to be an exact match
  455. *
  456. * The @exact argument should be set to true by most callers. The exception
  457. * is when we need to match blocks which are not represented by the rgrp
  458. * bitmap, but which are part of the rgrp (i.e. padding blocks) which are
  459. * there for alignment purposes. Another way of looking at it is that @exact
  460. * matches only valid data/metadata blocks, but with @exact false, it will
  461. * match any block within the extent of the rgrp.
  462. *
  463. * Returns: The resource group, or NULL if not found
  464. */
  465. struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk, bool exact)
  466. {
  467. struct rb_node *n, *next;
  468. struct gfs2_rgrpd *cur;
  469. spin_lock(&sdp->sd_rindex_spin);
  470. n = sdp->sd_rindex_tree.rb_node;
  471. while (n) {
  472. cur = rb_entry(n, struct gfs2_rgrpd, rd_node);
  473. next = NULL;
  474. if (blk < cur->rd_addr)
  475. next = n->rb_left;
  476. else if (blk >= cur->rd_data0 + cur->rd_data)
  477. next = n->rb_right;
  478. if (next == NULL) {
  479. spin_unlock(&sdp->sd_rindex_spin);
  480. if (exact) {
  481. if (blk < cur->rd_addr)
  482. return NULL;
  483. if (blk >= cur->rd_data0 + cur->rd_data)
  484. return NULL;
  485. }
  486. return cur;
  487. }
  488. n = next;
  489. }
  490. spin_unlock(&sdp->sd_rindex_spin);
  491. return NULL;
  492. }
  493. /**
  494. * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
  495. * @sdp: The GFS2 superblock
  496. *
  497. * Returns: The first rgrp in the filesystem
  498. */
  499. struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
  500. {
  501. const struct rb_node *n;
  502. struct gfs2_rgrpd *rgd;
  503. spin_lock(&sdp->sd_rindex_spin);
  504. n = rb_first(&sdp->sd_rindex_tree);
  505. rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
  506. spin_unlock(&sdp->sd_rindex_spin);
  507. return rgd;
  508. }
  509. /**
  510. * gfs2_rgrpd_get_next - get the next RG
  511. * @rgd: the resource group descriptor
  512. *
  513. * Returns: The next rgrp
  514. */
  515. struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
  516. {
  517. struct gfs2_sbd *sdp = rgd->rd_sbd;
  518. const struct rb_node *n;
  519. spin_lock(&sdp->sd_rindex_spin);
  520. n = rb_next(&rgd->rd_node);
  521. if (n == NULL)
  522. n = rb_first(&sdp->sd_rindex_tree);
  523. if (unlikely(&rgd->rd_node == n)) {
  524. spin_unlock(&sdp->sd_rindex_spin);
  525. return NULL;
  526. }
  527. rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
  528. spin_unlock(&sdp->sd_rindex_spin);
  529. return rgd;
  530. }
  531. void check_and_update_goal(struct gfs2_inode *ip)
  532. {
  533. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  534. if (!ip->i_goal || gfs2_blk2rgrpd(sdp, ip->i_goal, 1) == NULL)
  535. ip->i_goal = ip->i_no_addr;
  536. }
  537. void gfs2_free_clones(struct gfs2_rgrpd *rgd)
  538. {
  539. int x;
  540. for (x = 0; x < rgd->rd_length; x++) {
  541. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  542. kfree(bi->bi_clone);
  543. bi->bi_clone = NULL;
  544. }
  545. }
  546. static void dump_rs(struct seq_file *seq, const struct gfs2_blkreserv *rs,
  547. const char *fs_id_buf)
  548. {
  549. struct gfs2_inode *ip = container_of(rs, struct gfs2_inode, i_res);
  550. gfs2_print_dbg(seq, "%s B: n:%llu s:%llu f:%u\n",
  551. fs_id_buf,
  552. (unsigned long long)ip->i_no_addr,
  553. (unsigned long long)rs->rs_start,
  554. rs->rs_requested);
  555. }
  556. /**
  557. * __rs_deltree - remove a multi-block reservation from the rgd tree
  558. * @rs: The reservation to remove
  559. *
  560. */
  561. static void __rs_deltree(struct gfs2_blkreserv *rs)
  562. {
  563. struct gfs2_rgrpd *rgd;
  564. if (!gfs2_rs_active(rs))
  565. return;
  566. rgd = rs->rs_rgd;
  567. trace_gfs2_rs(rs, TRACE_RS_TREEDEL);
  568. rb_erase(&rs->rs_node, &rgd->rd_rstree);
  569. RB_CLEAR_NODE(&rs->rs_node);
  570. if (rs->rs_requested) {
  571. /* return requested blocks to the rgrp */
  572. BUG_ON(rs->rs_rgd->rd_requested < rs->rs_requested);
  573. rs->rs_rgd->rd_requested -= rs->rs_requested;
  574. /* The rgrp extent failure point is likely not to increase;
  575. it will only do so if the freed blocks are somehow
  576. contiguous with a span of free blocks that follows. Still,
  577. it will force the number to be recalculated later. */
  578. rgd->rd_extfail_pt += rs->rs_requested;
  579. rs->rs_requested = 0;
  580. }
  581. }
  582. /**
  583. * gfs2_rs_deltree - remove a multi-block reservation from the rgd tree
  584. * @rs: The reservation to remove
  585. *
  586. */
  587. void gfs2_rs_deltree(struct gfs2_blkreserv *rs)
  588. {
  589. struct gfs2_rgrpd *rgd;
  590. rgd = rs->rs_rgd;
  591. if (rgd) {
  592. spin_lock(&rgd->rd_rsspin);
  593. __rs_deltree(rs);
  594. BUG_ON(rs->rs_requested);
  595. spin_unlock(&rgd->rd_rsspin);
  596. }
  597. }
  598. /**
  599. * gfs2_rs_delete - delete a multi-block reservation
  600. * @ip: The inode for this reservation
  601. *
  602. */
  603. void gfs2_rs_delete(struct gfs2_inode *ip)
  604. {
  605. struct inode *inode = &ip->i_inode;
  606. down_write(&ip->i_rw_mutex);
  607. if (atomic_read(&inode->i_writecount) <= 1)
  608. gfs2_rs_deltree(&ip->i_res);
  609. up_write(&ip->i_rw_mutex);
  610. }
  611. /**
  612. * return_all_reservations - return all reserved blocks back to the rgrp.
  613. * @rgd: the rgrp that needs its space back
  614. *
  615. * We previously reserved a bunch of blocks for allocation. Now we need to
  616. * give them back. This leave the reservation structures in tact, but removes
  617. * all of their corresponding "no-fly zones".
  618. */
  619. static void return_all_reservations(struct gfs2_rgrpd *rgd)
  620. {
  621. struct rb_node *n;
  622. struct gfs2_blkreserv *rs;
  623. spin_lock(&rgd->rd_rsspin);
  624. while ((n = rb_first(&rgd->rd_rstree))) {
  625. rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
  626. __rs_deltree(rs);
  627. }
  628. spin_unlock(&rgd->rd_rsspin);
  629. }
  630. void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
  631. {
  632. struct rb_node *n;
  633. struct gfs2_rgrpd *rgd;
  634. struct gfs2_glock *gl;
  635. while ((n = rb_first(&sdp->sd_rindex_tree))) {
  636. rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
  637. gl = rgd->rd_gl;
  638. rb_erase(n, &sdp->sd_rindex_tree);
  639. if (gl) {
  640. if (gl->gl_state != LM_ST_UNLOCKED) {
  641. gfs2_glock_cb(gl, LM_ST_UNLOCKED);
  642. flush_delayed_work(&gl->gl_work);
  643. }
  644. gfs2_rgrp_brelse(rgd);
  645. glock_clear_object(gl, rgd);
  646. gfs2_glock_put(gl);
  647. }
  648. gfs2_free_clones(rgd);
  649. return_all_reservations(rgd);
  650. kfree(rgd->rd_bits);
  651. rgd->rd_bits = NULL;
  652. kmem_cache_free(gfs2_rgrpd_cachep, rgd);
  653. }
  654. }
  655. /**
  656. * compute_bitstructs - Compute the bitmap sizes
  657. * @rgd: The resource group descriptor
  658. *
  659. * Calculates bitmap descriptors, one for each block that contains bitmap data
  660. *
  661. * Returns: errno
  662. */
  663. static int compute_bitstructs(struct gfs2_rgrpd *rgd)
  664. {
  665. struct gfs2_sbd *sdp = rgd->rd_sbd;
  666. struct gfs2_bitmap *bi;
  667. u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
  668. u32 bytes_left, bytes;
  669. int x;
  670. if (!length)
  671. return -EINVAL;
  672. rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
  673. if (!rgd->rd_bits)
  674. return -ENOMEM;
  675. bytes_left = rgd->rd_bitbytes;
  676. for (x = 0; x < length; x++) {
  677. bi = rgd->rd_bits + x;
  678. bi->bi_flags = 0;
  679. /* small rgrp; bitmap stored completely in header block */
  680. if (length == 1) {
  681. bytes = bytes_left;
  682. bi->bi_offset = sizeof(struct gfs2_rgrp);
  683. bi->bi_start = 0;
  684. bi->bi_bytes = bytes;
  685. bi->bi_blocks = bytes * GFS2_NBBY;
  686. /* header block */
  687. } else if (x == 0) {
  688. bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
  689. bi->bi_offset = sizeof(struct gfs2_rgrp);
  690. bi->bi_start = 0;
  691. bi->bi_bytes = bytes;
  692. bi->bi_blocks = bytes * GFS2_NBBY;
  693. /* last block */
  694. } else if (x + 1 == length) {
  695. bytes = bytes_left;
  696. bi->bi_offset = sizeof(struct gfs2_meta_header);
  697. bi->bi_start = rgd->rd_bitbytes - bytes_left;
  698. bi->bi_bytes = bytes;
  699. bi->bi_blocks = bytes * GFS2_NBBY;
  700. /* other blocks */
  701. } else {
  702. bytes = sdp->sd_sb.sb_bsize -
  703. sizeof(struct gfs2_meta_header);
  704. bi->bi_offset = sizeof(struct gfs2_meta_header);
  705. bi->bi_start = rgd->rd_bitbytes - bytes_left;
  706. bi->bi_bytes = bytes;
  707. bi->bi_blocks = bytes * GFS2_NBBY;
  708. }
  709. bytes_left -= bytes;
  710. }
  711. if (bytes_left) {
  712. gfs2_consist_rgrpd(rgd);
  713. return -EIO;
  714. }
  715. bi = rgd->rd_bits + (length - 1);
  716. if ((bi->bi_start + bi->bi_bytes) * GFS2_NBBY != rgd->rd_data) {
  717. gfs2_lm(sdp,
  718. "ri_addr=%llu "
  719. "ri_length=%u "
  720. "ri_data0=%llu "
  721. "ri_data=%u "
  722. "ri_bitbytes=%u "
  723. "start=%u len=%u offset=%u\n",
  724. (unsigned long long)rgd->rd_addr,
  725. rgd->rd_length,
  726. (unsigned long long)rgd->rd_data0,
  727. rgd->rd_data,
  728. rgd->rd_bitbytes,
  729. bi->bi_start, bi->bi_bytes, bi->bi_offset);
  730. gfs2_consist_rgrpd(rgd);
  731. return -EIO;
  732. }
  733. return 0;
  734. }
  735. /**
  736. * gfs2_ri_total - Total up the file system space, according to the rindex.
  737. * @sdp: the filesystem
  738. *
  739. */
  740. u64 gfs2_ri_total(struct gfs2_sbd *sdp)
  741. {
  742. u64 total_data = 0;
  743. struct inode *inode = sdp->sd_rindex;
  744. struct gfs2_inode *ip = GFS2_I(inode);
  745. char buf[sizeof(struct gfs2_rindex)];
  746. int error, rgrps;
  747. for (rgrps = 0;; rgrps++) {
  748. loff_t pos = rgrps * sizeof(struct gfs2_rindex);
  749. if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
  750. break;
  751. error = gfs2_internal_read(ip, buf, &pos,
  752. sizeof(struct gfs2_rindex));
  753. if (error != sizeof(struct gfs2_rindex))
  754. break;
  755. total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
  756. }
  757. return total_data;
  758. }
  759. static int rgd_insert(struct gfs2_rgrpd *rgd)
  760. {
  761. struct gfs2_sbd *sdp = rgd->rd_sbd;
  762. struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL;
  763. /* Figure out where to put new node */
  764. while (*newn) {
  765. struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd,
  766. rd_node);
  767. parent = *newn;
  768. if (rgd->rd_addr < cur->rd_addr)
  769. newn = &((*newn)->rb_left);
  770. else if (rgd->rd_addr > cur->rd_addr)
  771. newn = &((*newn)->rb_right);
  772. else
  773. return -EEXIST;
  774. }
  775. rb_link_node(&rgd->rd_node, parent, newn);
  776. rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree);
  777. sdp->sd_rgrps++;
  778. return 0;
  779. }
  780. /**
  781. * read_rindex_entry - Pull in a new resource index entry from the disk
  782. * @ip: Pointer to the rindex inode
  783. *
  784. * Returns: 0 on success, > 0 on EOF, error code otherwise
  785. */
  786. static int read_rindex_entry(struct gfs2_inode *ip)
  787. {
  788. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  789. loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
  790. struct gfs2_rindex buf;
  791. int error;
  792. struct gfs2_rgrpd *rgd;
  793. if (pos >= i_size_read(&ip->i_inode))
  794. return 1;
  795. error = gfs2_internal_read(ip, (char *)&buf, &pos,
  796. sizeof(struct gfs2_rindex));
  797. if (error != sizeof(struct gfs2_rindex))
  798. return (error == 0) ? 1 : error;
  799. rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
  800. error = -ENOMEM;
  801. if (!rgd)
  802. return error;
  803. rgd->rd_sbd = sdp;
  804. rgd->rd_addr = be64_to_cpu(buf.ri_addr);
  805. rgd->rd_length = be32_to_cpu(buf.ri_length);
  806. rgd->rd_data0 = be64_to_cpu(buf.ri_data0);
  807. rgd->rd_data = be32_to_cpu(buf.ri_data);
  808. rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes);
  809. spin_lock_init(&rgd->rd_rsspin);
  810. mutex_init(&rgd->rd_mutex);
  811. error = gfs2_glock_get(sdp, rgd->rd_addr,
  812. &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
  813. if (error)
  814. goto fail;
  815. error = compute_bitstructs(rgd);
  816. if (error)
  817. goto fail_glock;
  818. rgd->rd_rgl = (struct gfs2_rgrp_lvb *)rgd->rd_gl->gl_lksb.sb_lvbptr;
  819. rgd->rd_flags &= ~GFS2_RDF_PREFERRED;
  820. if (rgd->rd_data > sdp->sd_max_rg_data)
  821. sdp->sd_max_rg_data = rgd->rd_data;
  822. spin_lock(&sdp->sd_rindex_spin);
  823. error = rgd_insert(rgd);
  824. spin_unlock(&sdp->sd_rindex_spin);
  825. if (!error) {
  826. glock_set_object(rgd->rd_gl, rgd);
  827. return 0;
  828. }
  829. error = 0; /* someone else read in the rgrp; free it and ignore it */
  830. fail_glock:
  831. gfs2_glock_put(rgd->rd_gl);
  832. fail:
  833. kfree(rgd->rd_bits);
  834. rgd->rd_bits = NULL;
  835. kmem_cache_free(gfs2_rgrpd_cachep, rgd);
  836. return error;
  837. }
  838. /**
  839. * set_rgrp_preferences - Run all the rgrps, selecting some we prefer to use
  840. * @sdp: the GFS2 superblock
  841. *
  842. * The purpose of this function is to select a subset of the resource groups
  843. * and mark them as PREFERRED. We do it in such a way that each node prefers
  844. * to use a unique set of rgrps to minimize glock contention.
  845. */
  846. static void set_rgrp_preferences(struct gfs2_sbd *sdp)
  847. {
  848. struct gfs2_rgrpd *rgd, *first;
  849. int i;
  850. /* Skip an initial number of rgrps, based on this node's journal ID.
  851. That should start each node out on its own set. */
  852. rgd = gfs2_rgrpd_get_first(sdp);
  853. for (i = 0; i < sdp->sd_lockstruct.ls_jid; i++)
  854. rgd = gfs2_rgrpd_get_next(rgd);
  855. first = rgd;
  856. do {
  857. rgd->rd_flags |= GFS2_RDF_PREFERRED;
  858. for (i = 0; i < sdp->sd_journals; i++) {
  859. rgd = gfs2_rgrpd_get_next(rgd);
  860. if (!rgd || rgd == first)
  861. break;
  862. }
  863. } while (rgd && rgd != first);
  864. }
  865. /**
  866. * gfs2_ri_update - Pull in a new resource index from the disk
  867. * @ip: pointer to the rindex inode
  868. *
  869. * Returns: 0 on successful update, error code otherwise
  870. */
  871. static int gfs2_ri_update(struct gfs2_inode *ip)
  872. {
  873. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  874. int error;
  875. do {
  876. error = read_rindex_entry(ip);
  877. } while (error == 0);
  878. if (error < 0)
  879. return error;
  880. if (RB_EMPTY_ROOT(&sdp->sd_rindex_tree)) {
  881. fs_err(sdp, "no resource groups found in the file system.\n");
  882. return -ENOENT;
  883. }
  884. set_rgrp_preferences(sdp);
  885. sdp->sd_rindex_uptodate = 1;
  886. return 0;
  887. }
  888. /**
  889. * gfs2_rindex_update - Update the rindex if required
  890. * @sdp: The GFS2 superblock
  891. *
  892. * We grab a lock on the rindex inode to make sure that it doesn't
  893. * change whilst we are performing an operation. We keep this lock
  894. * for quite long periods of time compared to other locks. This
  895. * doesn't matter, since it is shared and it is very, very rarely
  896. * accessed in the exclusive mode (i.e. only when expanding the filesystem).
  897. *
  898. * This makes sure that we're using the latest copy of the resource index
  899. * special file, which might have been updated if someone expanded the
  900. * filesystem (via gfs2_grow utility), which adds new resource groups.
  901. *
  902. * Returns: 0 on succeess, error code otherwise
  903. */
  904. int gfs2_rindex_update(struct gfs2_sbd *sdp)
  905. {
  906. struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
  907. struct gfs2_glock *gl = ip->i_gl;
  908. struct gfs2_holder ri_gh;
  909. int error = 0;
  910. int unlock_required = 0;
  911. /* Read new copy from disk if we don't have the latest */
  912. if (!sdp->sd_rindex_uptodate) {
  913. if (!gfs2_glock_is_locked_by_me(gl)) {
  914. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &ri_gh);
  915. if (error)
  916. return error;
  917. unlock_required = 1;
  918. }
  919. if (!sdp->sd_rindex_uptodate)
  920. error = gfs2_ri_update(ip);
  921. if (unlock_required)
  922. gfs2_glock_dq_uninit(&ri_gh);
  923. }
  924. return error;
  925. }
  926. static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
  927. {
  928. const struct gfs2_rgrp *str = buf;
  929. u32 rg_flags;
  930. rg_flags = be32_to_cpu(str->rg_flags);
  931. rg_flags &= ~GFS2_RDF_MASK;
  932. rgd->rd_flags &= GFS2_RDF_MASK;
  933. rgd->rd_flags |= rg_flags;
  934. rgd->rd_free = be32_to_cpu(str->rg_free);
  935. rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
  936. rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
  937. /* rd_data0, rd_data and rd_bitbytes already set from rindex */
  938. }
  939. static void gfs2_rgrp_ondisk2lvb(struct gfs2_rgrp_lvb *rgl, const void *buf)
  940. {
  941. const struct gfs2_rgrp *str = buf;
  942. rgl->rl_magic = cpu_to_be32(GFS2_MAGIC);
  943. rgl->rl_flags = str->rg_flags;
  944. rgl->rl_free = str->rg_free;
  945. rgl->rl_dinodes = str->rg_dinodes;
  946. rgl->rl_igeneration = str->rg_igeneration;
  947. rgl->__pad = 0UL;
  948. }
  949. static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
  950. {
  951. struct gfs2_rgrpd *next = gfs2_rgrpd_get_next(rgd);
  952. struct gfs2_rgrp *str = buf;
  953. u32 crc;
  954. str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
  955. str->rg_free = cpu_to_be32(rgd->rd_free);
  956. str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
  957. if (next == NULL)
  958. str->rg_skip = 0;
  959. else if (next->rd_addr > rgd->rd_addr)
  960. str->rg_skip = cpu_to_be32(next->rd_addr - rgd->rd_addr);
  961. str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
  962. str->rg_data0 = cpu_to_be64(rgd->rd_data0);
  963. str->rg_data = cpu_to_be32(rgd->rd_data);
  964. str->rg_bitbytes = cpu_to_be32(rgd->rd_bitbytes);
  965. str->rg_crc = 0;
  966. crc = gfs2_disk_hash(buf, sizeof(struct gfs2_rgrp));
  967. str->rg_crc = cpu_to_be32(crc);
  968. memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
  969. gfs2_rgrp_ondisk2lvb(rgd->rd_rgl, buf);
  970. }
  971. static int gfs2_rgrp_lvb_valid(struct gfs2_rgrpd *rgd)
  972. {
  973. struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
  974. struct gfs2_rgrp *str = (struct gfs2_rgrp *)rgd->rd_bits[0].bi_bh->b_data;
  975. struct gfs2_sbd *sdp = rgd->rd_sbd;
  976. int valid = 1;
  977. if (rgl->rl_flags != str->rg_flags) {
  978. fs_warn(sdp, "GFS2: rgd: %llu lvb flag mismatch %u/%u",
  979. (unsigned long long)rgd->rd_addr,
  980. be32_to_cpu(rgl->rl_flags), be32_to_cpu(str->rg_flags));
  981. valid = 0;
  982. }
  983. if (rgl->rl_free != str->rg_free) {
  984. fs_warn(sdp, "GFS2: rgd: %llu lvb free mismatch %u/%u",
  985. (unsigned long long)rgd->rd_addr,
  986. be32_to_cpu(rgl->rl_free), be32_to_cpu(str->rg_free));
  987. valid = 0;
  988. }
  989. if (rgl->rl_dinodes != str->rg_dinodes) {
  990. fs_warn(sdp, "GFS2: rgd: %llu lvb dinode mismatch %u/%u",
  991. (unsigned long long)rgd->rd_addr,
  992. be32_to_cpu(rgl->rl_dinodes),
  993. be32_to_cpu(str->rg_dinodes));
  994. valid = 0;
  995. }
  996. if (rgl->rl_igeneration != str->rg_igeneration) {
  997. fs_warn(sdp, "GFS2: rgd: %llu lvb igen mismatch %llu/%llu",
  998. (unsigned long long)rgd->rd_addr,
  999. (unsigned long long)be64_to_cpu(rgl->rl_igeneration),
  1000. (unsigned long long)be64_to_cpu(str->rg_igeneration));
  1001. valid = 0;
  1002. }
  1003. return valid;
  1004. }
  1005. static u32 count_unlinked(struct gfs2_rgrpd *rgd)
  1006. {
  1007. struct gfs2_bitmap *bi;
  1008. const u32 length = rgd->rd_length;
  1009. const u8 *buffer = NULL;
  1010. u32 i, goal, count = 0;
  1011. for (i = 0, bi = rgd->rd_bits; i < length; i++, bi++) {
  1012. goal = 0;
  1013. buffer = bi->bi_bh->b_data + bi->bi_offset;
  1014. WARN_ON(!buffer_uptodate(bi->bi_bh));
  1015. while (goal < bi->bi_blocks) {
  1016. goal = gfs2_bitfit(buffer, bi->bi_bytes, goal,
  1017. GFS2_BLKST_UNLINKED);
  1018. if (goal == BFITNOENT)
  1019. break;
  1020. count++;
  1021. goal++;
  1022. }
  1023. }
  1024. return count;
  1025. }
  1026. static void rgrp_set_bitmap_flags(struct gfs2_rgrpd *rgd)
  1027. {
  1028. struct gfs2_bitmap *bi;
  1029. int x;
  1030. if (rgd->rd_free) {
  1031. for (x = 0; x < rgd->rd_length; x++) {
  1032. bi = rgd->rd_bits + x;
  1033. clear_bit(GBF_FULL, &bi->bi_flags);
  1034. }
  1035. } else {
  1036. for (x = 0; x < rgd->rd_length; x++) {
  1037. bi = rgd->rd_bits + x;
  1038. set_bit(GBF_FULL, &bi->bi_flags);
  1039. }
  1040. }
  1041. }
  1042. /**
  1043. * gfs2_rgrp_go_instantiate - Read in a RG's header and bitmaps
  1044. * @gl: the glock representing the rgrpd to read in
  1045. *
  1046. * Read in all of a Resource Group's header and bitmap blocks.
  1047. * Caller must eventually call gfs2_rgrp_brelse() to free the bitmaps.
  1048. *
  1049. * Returns: errno
  1050. */
  1051. int gfs2_rgrp_go_instantiate(struct gfs2_glock *gl)
  1052. {
  1053. struct gfs2_rgrpd *rgd = gl->gl_object;
  1054. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1055. unsigned int length = rgd->rd_length;
  1056. struct gfs2_bitmap *bi;
  1057. unsigned int x, y;
  1058. int error;
  1059. if (rgd->rd_bits[0].bi_bh != NULL)
  1060. return 0;
  1061. for (x = 0; x < length; x++) {
  1062. bi = rgd->rd_bits + x;
  1063. error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, 0, &bi->bi_bh);
  1064. if (error)
  1065. goto fail;
  1066. }
  1067. for (y = length; y--;) {
  1068. bi = rgd->rd_bits + y;
  1069. error = gfs2_meta_wait(sdp, bi->bi_bh);
  1070. if (error)
  1071. goto fail;
  1072. if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
  1073. GFS2_METATYPE_RG)) {
  1074. error = -EIO;
  1075. goto fail;
  1076. }
  1077. }
  1078. gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
  1079. rgrp_set_bitmap_flags(rgd);
  1080. rgd->rd_flags |= GFS2_RDF_CHECK;
  1081. rgd->rd_free_clone = rgd->rd_free;
  1082. GLOCK_BUG_ON(rgd->rd_gl, rgd->rd_reserved);
  1083. /* max out the rgrp allocation failure point */
  1084. rgd->rd_extfail_pt = rgd->rd_free;
  1085. if (cpu_to_be32(GFS2_MAGIC) != rgd->rd_rgl->rl_magic) {
  1086. rgd->rd_rgl->rl_unlinked = cpu_to_be32(count_unlinked(rgd));
  1087. gfs2_rgrp_ondisk2lvb(rgd->rd_rgl,
  1088. rgd->rd_bits[0].bi_bh->b_data);
  1089. } else if (sdp->sd_args.ar_rgrplvb) {
  1090. if (!gfs2_rgrp_lvb_valid(rgd)){
  1091. gfs2_consist_rgrpd(rgd);
  1092. error = -EIO;
  1093. goto fail;
  1094. }
  1095. if (rgd->rd_rgl->rl_unlinked == 0)
  1096. rgd->rd_flags &= ~GFS2_RDF_CHECK;
  1097. }
  1098. return 0;
  1099. fail:
  1100. while (x--) {
  1101. bi = rgd->rd_bits + x;
  1102. brelse(bi->bi_bh);
  1103. bi->bi_bh = NULL;
  1104. gfs2_assert_warn(sdp, !bi->bi_clone);
  1105. }
  1106. return error;
  1107. }
  1108. static int update_rgrp_lvb(struct gfs2_rgrpd *rgd, struct gfs2_holder *gh)
  1109. {
  1110. u32 rl_flags;
  1111. if (!test_bit(GLF_INSTANTIATE_NEEDED, &gh->gh_gl->gl_flags))
  1112. return 0;
  1113. if (cpu_to_be32(GFS2_MAGIC) != rgd->rd_rgl->rl_magic)
  1114. return gfs2_instantiate(gh);
  1115. rl_flags = be32_to_cpu(rgd->rd_rgl->rl_flags);
  1116. rl_flags &= ~GFS2_RDF_MASK;
  1117. rgd->rd_flags &= GFS2_RDF_MASK;
  1118. rgd->rd_flags |= (rl_flags | GFS2_RDF_CHECK);
  1119. if (rgd->rd_rgl->rl_unlinked == 0)
  1120. rgd->rd_flags &= ~GFS2_RDF_CHECK;
  1121. rgd->rd_free = be32_to_cpu(rgd->rd_rgl->rl_free);
  1122. rgrp_set_bitmap_flags(rgd);
  1123. rgd->rd_free_clone = rgd->rd_free;
  1124. GLOCK_BUG_ON(rgd->rd_gl, rgd->rd_reserved);
  1125. /* max out the rgrp allocation failure point */
  1126. rgd->rd_extfail_pt = rgd->rd_free;
  1127. rgd->rd_dinodes = be32_to_cpu(rgd->rd_rgl->rl_dinodes);
  1128. rgd->rd_igeneration = be64_to_cpu(rgd->rd_rgl->rl_igeneration);
  1129. return 0;
  1130. }
  1131. /**
  1132. * gfs2_rgrp_brelse - Release RG bitmaps read in with gfs2_rgrp_bh_get()
  1133. * @rgd: The resource group
  1134. *
  1135. */
  1136. void gfs2_rgrp_brelse(struct gfs2_rgrpd *rgd)
  1137. {
  1138. int x, length = rgd->rd_length;
  1139. for (x = 0; x < length; x++) {
  1140. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  1141. if (bi->bi_bh) {
  1142. brelse(bi->bi_bh);
  1143. bi->bi_bh = NULL;
  1144. }
  1145. }
  1146. set_bit(GLF_INSTANTIATE_NEEDED, &rgd->rd_gl->gl_flags);
  1147. }
  1148. int gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
  1149. struct buffer_head *bh,
  1150. const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed)
  1151. {
  1152. struct super_block *sb = sdp->sd_vfs;
  1153. u64 blk;
  1154. sector_t start = 0;
  1155. sector_t nr_blks = 0;
  1156. int rv = -EIO;
  1157. unsigned int x;
  1158. u32 trimmed = 0;
  1159. u8 diff;
  1160. for (x = 0; x < bi->bi_bytes; x++) {
  1161. const u8 *clone = bi->bi_clone ? bi->bi_clone : bi->bi_bh->b_data;
  1162. clone += bi->bi_offset;
  1163. clone += x;
  1164. if (bh) {
  1165. const u8 *orig = bh->b_data + bi->bi_offset + x;
  1166. diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
  1167. } else {
  1168. diff = ~(*clone | (*clone >> 1));
  1169. }
  1170. diff &= 0x55;
  1171. if (diff == 0)
  1172. continue;
  1173. blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
  1174. while(diff) {
  1175. if (diff & 1) {
  1176. if (nr_blks == 0)
  1177. goto start_new_extent;
  1178. if ((start + nr_blks) != blk) {
  1179. if (nr_blks >= minlen) {
  1180. rv = sb_issue_discard(sb,
  1181. start, nr_blks,
  1182. GFP_NOFS, 0);
  1183. if (rv)
  1184. goto fail;
  1185. trimmed += nr_blks;
  1186. }
  1187. nr_blks = 0;
  1188. start_new_extent:
  1189. start = blk;
  1190. }
  1191. nr_blks++;
  1192. }
  1193. diff >>= 2;
  1194. blk++;
  1195. }
  1196. }
  1197. if (nr_blks >= minlen) {
  1198. rv = sb_issue_discard(sb, start, nr_blks, GFP_NOFS, 0);
  1199. if (rv)
  1200. goto fail;
  1201. trimmed += nr_blks;
  1202. }
  1203. if (ptrimmed)
  1204. *ptrimmed = trimmed;
  1205. return 0;
  1206. fail:
  1207. if (sdp->sd_args.ar_discard)
  1208. fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem\n", rv);
  1209. sdp->sd_args.ar_discard = 0;
  1210. return rv;
  1211. }
  1212. /**
  1213. * gfs2_fitrim - Generate discard requests for unused bits of the filesystem
  1214. * @filp: Any file on the filesystem
  1215. * @argp: Pointer to the arguments (also used to pass result)
  1216. *
  1217. * Returns: 0 on success, otherwise error code
  1218. */
  1219. int gfs2_fitrim(struct file *filp, void __user *argp)
  1220. {
  1221. struct inode *inode = file_inode(filp);
  1222. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1223. struct block_device *bdev = sdp->sd_vfs->s_bdev;
  1224. struct buffer_head *bh;
  1225. struct gfs2_rgrpd *rgd;
  1226. struct gfs2_rgrpd *rgd_end;
  1227. struct gfs2_holder gh;
  1228. struct fstrim_range r;
  1229. int ret = 0;
  1230. u64 amt;
  1231. u64 trimmed = 0;
  1232. u64 start, end, minlen;
  1233. unsigned int x;
  1234. unsigned bs_shift = sdp->sd_sb.sb_bsize_shift;
  1235. if (!capable(CAP_SYS_ADMIN))
  1236. return -EPERM;
  1237. if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
  1238. return -EROFS;
  1239. if (!bdev_max_discard_sectors(bdev))
  1240. return -EOPNOTSUPP;
  1241. if (copy_from_user(&r, argp, sizeof(r)))
  1242. return -EFAULT;
  1243. ret = gfs2_rindex_update(sdp);
  1244. if (ret)
  1245. return ret;
  1246. start = r.start >> bs_shift;
  1247. end = start + (r.len >> bs_shift);
  1248. minlen = max_t(u64, r.minlen, sdp->sd_sb.sb_bsize);
  1249. minlen = max_t(u64, minlen, bdev_discard_granularity(bdev)) >> bs_shift;
  1250. if (end <= start || minlen > sdp->sd_max_rg_data)
  1251. return -EINVAL;
  1252. rgd = gfs2_blk2rgrpd(sdp, start, 0);
  1253. rgd_end = gfs2_blk2rgrpd(sdp, end, 0);
  1254. if ((gfs2_rgrpd_get_first(sdp) == gfs2_rgrpd_get_next(rgd_end))
  1255. && (start > rgd_end->rd_data0 + rgd_end->rd_data))
  1256. return -EINVAL; /* start is beyond the end of the fs */
  1257. while (1) {
  1258. ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
  1259. LM_FLAG_NODE_SCOPE, &gh);
  1260. if (ret)
  1261. goto out;
  1262. if (!(rgd->rd_flags & GFS2_RGF_TRIMMED)) {
  1263. /* Trim each bitmap in the rgrp */
  1264. for (x = 0; x < rgd->rd_length; x++) {
  1265. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  1266. rgrp_lock_local(rgd);
  1267. ret = gfs2_rgrp_send_discards(sdp,
  1268. rgd->rd_data0, NULL, bi, minlen,
  1269. &amt);
  1270. rgrp_unlock_local(rgd);
  1271. if (ret) {
  1272. gfs2_glock_dq_uninit(&gh);
  1273. goto out;
  1274. }
  1275. trimmed += amt;
  1276. }
  1277. /* Mark rgrp as having been trimmed */
  1278. ret = gfs2_trans_begin(sdp, RES_RG_HDR, 0);
  1279. if (ret == 0) {
  1280. bh = rgd->rd_bits[0].bi_bh;
  1281. rgrp_lock_local(rgd);
  1282. rgd->rd_flags |= GFS2_RGF_TRIMMED;
  1283. gfs2_trans_add_meta(rgd->rd_gl, bh);
  1284. gfs2_rgrp_out(rgd, bh->b_data);
  1285. rgrp_unlock_local(rgd);
  1286. gfs2_trans_end(sdp);
  1287. }
  1288. }
  1289. gfs2_glock_dq_uninit(&gh);
  1290. if (rgd == rgd_end)
  1291. break;
  1292. rgd = gfs2_rgrpd_get_next(rgd);
  1293. }
  1294. out:
  1295. r.len = trimmed << bs_shift;
  1296. if (copy_to_user(argp, &r, sizeof(r)))
  1297. return -EFAULT;
  1298. return ret;
  1299. }
  1300. /**
  1301. * rs_insert - insert a new multi-block reservation into the rgrp's rb_tree
  1302. * @ip: the inode structure
  1303. *
  1304. */
  1305. static void rs_insert(struct gfs2_inode *ip)
  1306. {
  1307. struct rb_node **newn, *parent = NULL;
  1308. int rc;
  1309. struct gfs2_blkreserv *rs = &ip->i_res;
  1310. struct gfs2_rgrpd *rgd = rs->rs_rgd;
  1311. BUG_ON(gfs2_rs_active(rs));
  1312. spin_lock(&rgd->rd_rsspin);
  1313. newn = &rgd->rd_rstree.rb_node;
  1314. while (*newn) {
  1315. struct gfs2_blkreserv *cur =
  1316. rb_entry(*newn, struct gfs2_blkreserv, rs_node);
  1317. parent = *newn;
  1318. rc = rs_cmp(rs->rs_start, rs->rs_requested, cur);
  1319. if (rc > 0)
  1320. newn = &((*newn)->rb_right);
  1321. else if (rc < 0)
  1322. newn = &((*newn)->rb_left);
  1323. else {
  1324. spin_unlock(&rgd->rd_rsspin);
  1325. WARN_ON(1);
  1326. return;
  1327. }
  1328. }
  1329. rb_link_node(&rs->rs_node, parent, newn);
  1330. rb_insert_color(&rs->rs_node, &rgd->rd_rstree);
  1331. /* Do our rgrp accounting for the reservation */
  1332. rgd->rd_requested += rs->rs_requested; /* blocks requested */
  1333. spin_unlock(&rgd->rd_rsspin);
  1334. trace_gfs2_rs(rs, TRACE_RS_INSERT);
  1335. }
  1336. /**
  1337. * rgd_free - return the number of free blocks we can allocate
  1338. * @rgd: the resource group
  1339. * @rs: The reservation to free
  1340. *
  1341. * This function returns the number of free blocks for an rgrp.
  1342. * That's the clone-free blocks (blocks that are free, not including those
  1343. * still being used for unlinked files that haven't been deleted.)
  1344. *
  1345. * It also subtracts any blocks reserved by someone else, but does not
  1346. * include free blocks that are still part of our current reservation,
  1347. * because obviously we can (and will) allocate them.
  1348. */
  1349. static inline u32 rgd_free(struct gfs2_rgrpd *rgd, struct gfs2_blkreserv *rs)
  1350. {
  1351. u32 tot_reserved, tot_free;
  1352. if (WARN_ON_ONCE(rgd->rd_requested < rs->rs_requested))
  1353. return 0;
  1354. tot_reserved = rgd->rd_requested - rs->rs_requested;
  1355. if (rgd->rd_free_clone < tot_reserved)
  1356. tot_reserved = 0;
  1357. tot_free = rgd->rd_free_clone - tot_reserved;
  1358. return tot_free;
  1359. }
  1360. /**
  1361. * rg_mblk_search - find a group of multiple free blocks to form a reservation
  1362. * @rgd: the resource group descriptor
  1363. * @ip: pointer to the inode for which we're reserving blocks
  1364. * @ap: the allocation parameters
  1365. *
  1366. */
  1367. static void rg_mblk_search(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip,
  1368. const struct gfs2_alloc_parms *ap)
  1369. {
  1370. struct gfs2_rbm rbm = { .rgd = rgd, };
  1371. u64 goal;
  1372. struct gfs2_blkreserv *rs = &ip->i_res;
  1373. u32 extlen;
  1374. u32 free_blocks, blocks_available;
  1375. int ret;
  1376. struct inode *inode = &ip->i_inode;
  1377. spin_lock(&rgd->rd_rsspin);
  1378. free_blocks = rgd_free(rgd, rs);
  1379. if (rgd->rd_free_clone < rgd->rd_requested)
  1380. free_blocks = 0;
  1381. blocks_available = rgd->rd_free_clone - rgd->rd_reserved;
  1382. if (rgd == rs->rs_rgd)
  1383. blocks_available += rs->rs_reserved;
  1384. spin_unlock(&rgd->rd_rsspin);
  1385. if (S_ISDIR(inode->i_mode))
  1386. extlen = 1;
  1387. else {
  1388. extlen = max_t(u32, atomic_read(&ip->i_sizehint), ap->target);
  1389. extlen = clamp(extlen, (u32)RGRP_RSRV_MINBLKS, free_blocks);
  1390. }
  1391. if (free_blocks < extlen || blocks_available < extlen)
  1392. return;
  1393. /* Find bitmap block that contains bits for goal block */
  1394. if (rgrp_contains_block(rgd, ip->i_goal))
  1395. goal = ip->i_goal;
  1396. else
  1397. goal = rgd->rd_last_alloc + rgd->rd_data0;
  1398. if (WARN_ON(gfs2_rbm_from_block(&rbm, goal)))
  1399. return;
  1400. ret = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, &extlen, &ip->i_res, true);
  1401. if (ret == 0) {
  1402. rs->rs_start = gfs2_rbm_to_block(&rbm);
  1403. rs->rs_requested = extlen;
  1404. rs_insert(ip);
  1405. } else {
  1406. if (goal == rgd->rd_last_alloc + rgd->rd_data0)
  1407. rgd->rd_last_alloc = 0;
  1408. }
  1409. }
  1410. /**
  1411. * gfs2_next_unreserved_block - Return next block that is not reserved
  1412. * @rgd: The resource group
  1413. * @block: The starting block
  1414. * @length: The required length
  1415. * @ignore_rs: Reservation to ignore
  1416. *
  1417. * If the block does not appear in any reservation, then return the
  1418. * block number unchanged. If it does appear in the reservation, then
  1419. * keep looking through the tree of reservations in order to find the
  1420. * first block number which is not reserved.
  1421. */
  1422. static u64 gfs2_next_unreserved_block(struct gfs2_rgrpd *rgd, u64 block,
  1423. u32 length,
  1424. struct gfs2_blkreserv *ignore_rs)
  1425. {
  1426. struct gfs2_blkreserv *rs;
  1427. struct rb_node *n;
  1428. int rc;
  1429. spin_lock(&rgd->rd_rsspin);
  1430. n = rgd->rd_rstree.rb_node;
  1431. while (n) {
  1432. rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
  1433. rc = rs_cmp(block, length, rs);
  1434. if (rc < 0)
  1435. n = n->rb_left;
  1436. else if (rc > 0)
  1437. n = n->rb_right;
  1438. else
  1439. break;
  1440. }
  1441. if (n) {
  1442. while (rs_cmp(block, length, rs) == 0 && rs != ignore_rs) {
  1443. block = rs->rs_start + rs->rs_requested;
  1444. n = n->rb_right;
  1445. if (n == NULL)
  1446. break;
  1447. rs = rb_entry(n, struct gfs2_blkreserv, rs_node);
  1448. }
  1449. }
  1450. spin_unlock(&rgd->rd_rsspin);
  1451. return block;
  1452. }
  1453. /**
  1454. * gfs2_reservation_check_and_update - Check for reservations during block alloc
  1455. * @rbm: The current position in the resource group
  1456. * @rs: Our own reservation
  1457. * @minext: The minimum extent length
  1458. * @maxext: A pointer to the maximum extent structure
  1459. *
  1460. * This checks the current position in the rgrp to see whether there is
  1461. * a reservation covering this block. If not then this function is a
  1462. * no-op. If there is, then the position is moved to the end of the
  1463. * contiguous reservation(s) so that we are pointing at the first
  1464. * non-reserved block.
  1465. *
  1466. * Returns: 0 if no reservation, 1 if @rbm has changed, otherwise an error
  1467. */
  1468. static int gfs2_reservation_check_and_update(struct gfs2_rbm *rbm,
  1469. struct gfs2_blkreserv *rs,
  1470. u32 minext,
  1471. struct gfs2_extent *maxext)
  1472. {
  1473. u64 block = gfs2_rbm_to_block(rbm);
  1474. u32 extlen = 1;
  1475. u64 nblock;
  1476. /*
  1477. * If we have a minimum extent length, then skip over any extent
  1478. * which is less than the min extent length in size.
  1479. */
  1480. if (minext > 1) {
  1481. extlen = gfs2_free_extlen(rbm, minext);
  1482. if (extlen <= maxext->len)
  1483. goto fail;
  1484. }
  1485. /*
  1486. * Check the extent which has been found against the reservations
  1487. * and skip if parts of it are already reserved
  1488. */
  1489. nblock = gfs2_next_unreserved_block(rbm->rgd, block, extlen, rs);
  1490. if (nblock == block) {
  1491. if (!minext || extlen >= minext)
  1492. return 0;
  1493. if (extlen > maxext->len) {
  1494. maxext->len = extlen;
  1495. maxext->rbm = *rbm;
  1496. }
  1497. } else {
  1498. u64 len = nblock - block;
  1499. if (len >= (u64)1 << 32)
  1500. return -E2BIG;
  1501. extlen = len;
  1502. }
  1503. fail:
  1504. if (gfs2_rbm_add(rbm, extlen))
  1505. return -E2BIG;
  1506. return 1;
  1507. }
  1508. /**
  1509. * gfs2_rbm_find - Look for blocks of a particular state
  1510. * @rbm: Value/result starting position and final position
  1511. * @state: The state which we want to find
  1512. * @minext: Pointer to the requested extent length
  1513. * This is updated to be the actual reservation size.
  1514. * @rs: Our own reservation (NULL to skip checking for reservations)
  1515. * @nowrap: Stop looking at the end of the rgrp, rather than wrapping
  1516. * around until we've reached the starting point.
  1517. *
  1518. * Side effects:
  1519. * - If looking for free blocks, we set GBF_FULL on each bitmap which
  1520. * has no free blocks in it.
  1521. * - If looking for free blocks, we set rd_extfail_pt on each rgrp which
  1522. * has come up short on a free block search.
  1523. *
  1524. * Returns: 0 on success, -ENOSPC if there is no block of the requested state
  1525. */
  1526. static int gfs2_rbm_find(struct gfs2_rbm *rbm, u8 state, u32 *minext,
  1527. struct gfs2_blkreserv *rs, bool nowrap)
  1528. {
  1529. bool scan_from_start = rbm->bii == 0 && rbm->offset == 0;
  1530. struct buffer_head *bh;
  1531. int last_bii;
  1532. u32 offset;
  1533. u8 *buffer;
  1534. bool wrapped = false;
  1535. int ret;
  1536. struct gfs2_bitmap *bi;
  1537. struct gfs2_extent maxext = { .rbm.rgd = rbm->rgd, };
  1538. /*
  1539. * Determine the last bitmap to search. If we're not starting at the
  1540. * beginning of a bitmap, we need to search that bitmap twice to scan
  1541. * the entire resource group.
  1542. */
  1543. last_bii = rbm->bii - (rbm->offset == 0);
  1544. while(1) {
  1545. bi = rbm_bi(rbm);
  1546. if (test_bit(GBF_FULL, &bi->bi_flags) &&
  1547. (state == GFS2_BLKST_FREE))
  1548. goto next_bitmap;
  1549. bh = bi->bi_bh;
  1550. buffer = bh->b_data + bi->bi_offset;
  1551. WARN_ON(!buffer_uptodate(bh));
  1552. if (state != GFS2_BLKST_UNLINKED && bi->bi_clone)
  1553. buffer = bi->bi_clone + bi->bi_offset;
  1554. offset = gfs2_bitfit(buffer, bi->bi_bytes, rbm->offset, state);
  1555. if (offset == BFITNOENT) {
  1556. if (state == GFS2_BLKST_FREE && rbm->offset == 0)
  1557. set_bit(GBF_FULL, &bi->bi_flags);
  1558. goto next_bitmap;
  1559. }
  1560. rbm->offset = offset;
  1561. if (!rs || !minext)
  1562. return 0;
  1563. ret = gfs2_reservation_check_and_update(rbm, rs, *minext,
  1564. &maxext);
  1565. if (ret == 0)
  1566. return 0;
  1567. if (ret > 0)
  1568. goto next_iter;
  1569. if (ret == -E2BIG) {
  1570. rbm->bii = 0;
  1571. rbm->offset = 0;
  1572. goto res_covered_end_of_rgrp;
  1573. }
  1574. return ret;
  1575. next_bitmap: /* Find next bitmap in the rgrp */
  1576. rbm->offset = 0;
  1577. rbm->bii++;
  1578. if (rbm->bii == rbm->rgd->rd_length)
  1579. rbm->bii = 0;
  1580. res_covered_end_of_rgrp:
  1581. if (rbm->bii == 0) {
  1582. if (wrapped)
  1583. break;
  1584. wrapped = true;
  1585. if (nowrap)
  1586. break;
  1587. }
  1588. next_iter:
  1589. /* Have we scanned the entire resource group? */
  1590. if (wrapped && rbm->bii > last_bii)
  1591. break;
  1592. }
  1593. if (state != GFS2_BLKST_FREE)
  1594. return -ENOSPC;
  1595. /* If the extent was too small, and it's smaller than the smallest
  1596. to have failed before, remember for future reference that it's
  1597. useless to search this rgrp again for this amount or more. */
  1598. if (wrapped && (scan_from_start || rbm->bii > last_bii) &&
  1599. *minext < rbm->rgd->rd_extfail_pt)
  1600. rbm->rgd->rd_extfail_pt = *minext - 1;
  1601. /* If the maximum extent we found is big enough to fulfill the
  1602. minimum requirements, use it anyway. */
  1603. if (maxext.len) {
  1604. *rbm = maxext.rbm;
  1605. *minext = maxext.len;
  1606. return 0;
  1607. }
  1608. return -ENOSPC;
  1609. }
  1610. /**
  1611. * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
  1612. * @rgd: The rgrp
  1613. * @last_unlinked: block address of the last dinode we unlinked
  1614. * @skip: block address we should explicitly not unlink
  1615. *
  1616. * Returns: 0 if no error
  1617. * The inode, if one has been found, in inode.
  1618. */
  1619. static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
  1620. {
  1621. u64 block;
  1622. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1623. struct gfs2_glock *gl;
  1624. struct gfs2_inode *ip;
  1625. int error;
  1626. int found = 0;
  1627. struct gfs2_rbm rbm = { .rgd = rgd, .bii = 0, .offset = 0 };
  1628. while (1) {
  1629. error = gfs2_rbm_find(&rbm, GFS2_BLKST_UNLINKED, NULL, NULL,
  1630. true);
  1631. if (error == -ENOSPC)
  1632. break;
  1633. if (WARN_ON_ONCE(error))
  1634. break;
  1635. block = gfs2_rbm_to_block(&rbm);
  1636. if (gfs2_rbm_from_block(&rbm, block + 1))
  1637. break;
  1638. if (*last_unlinked != NO_BLOCK && block <= *last_unlinked)
  1639. continue;
  1640. if (block == skip)
  1641. continue;
  1642. *last_unlinked = block;
  1643. error = gfs2_glock_get(sdp, block, &gfs2_iopen_glops, CREATE, &gl);
  1644. if (error)
  1645. continue;
  1646. /* If the inode is already in cache, we can ignore it here
  1647. * because the existing inode disposal code will deal with
  1648. * it when all refs have gone away. Accessing gl_object like
  1649. * this is not safe in general. Here it is ok because we do
  1650. * not dereference the pointer, and we only need an approx
  1651. * answer to whether it is NULL or not.
  1652. */
  1653. ip = gl->gl_object;
  1654. if (ip || !gfs2_queue_verify_delete(gl, false))
  1655. gfs2_glock_put(gl);
  1656. else
  1657. found++;
  1658. /* Limit reclaim to sensible number of tasks */
  1659. if (found > NR_CPUS)
  1660. return;
  1661. }
  1662. rgd->rd_flags &= ~GFS2_RDF_CHECK;
  1663. return;
  1664. }
  1665. /**
  1666. * gfs2_rgrp_congested - Use stats to figure out whether an rgrp is congested
  1667. * @rgd: The rgrp in question
  1668. * @loops: An indication of how picky we can be (0=very, 1=less so)
  1669. *
  1670. * This function uses the recently added glock statistics in order to
  1671. * figure out whether a parciular resource group is suffering from
  1672. * contention from multiple nodes. This is done purely on the basis
  1673. * of timings, since this is the only data we have to work with and
  1674. * our aim here is to reject a resource group which is highly contended
  1675. * but (very important) not to do this too often in order to ensure that
  1676. * we do not land up introducing fragmentation by changing resource
  1677. * groups when not actually required.
  1678. *
  1679. * The calculation is fairly simple, we want to know whether the SRTTB
  1680. * (i.e. smoothed round trip time for blocking operations) to acquire
  1681. * the lock for this rgrp's glock is significantly greater than the
  1682. * time taken for resource groups on average. We introduce a margin in
  1683. * the form of the variable @var which is computed as the sum of the two
  1684. * respective variences, and multiplied by a factor depending on @loops
  1685. * and whether we have a lot of data to base the decision on. This is
  1686. * then tested against the square difference of the means in order to
  1687. * decide whether the result is statistically significant or not.
  1688. *
  1689. * Returns: A boolean verdict on the congestion status
  1690. */
  1691. static bool gfs2_rgrp_congested(const struct gfs2_rgrpd *rgd, int loops)
  1692. {
  1693. const struct gfs2_glock *gl = rgd->rd_gl;
  1694. const struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  1695. struct gfs2_lkstats *st;
  1696. u64 r_dcount, l_dcount;
  1697. u64 l_srttb, a_srttb = 0;
  1698. s64 srttb_diff;
  1699. u64 sqr_diff;
  1700. u64 var;
  1701. int cpu, nonzero = 0;
  1702. preempt_disable();
  1703. for_each_present_cpu(cpu) {
  1704. st = &per_cpu_ptr(sdp->sd_lkstats, cpu)->lkstats[LM_TYPE_RGRP];
  1705. if (st->stats[GFS2_LKS_SRTTB]) {
  1706. a_srttb += st->stats[GFS2_LKS_SRTTB];
  1707. nonzero++;
  1708. }
  1709. }
  1710. st = &this_cpu_ptr(sdp->sd_lkstats)->lkstats[LM_TYPE_RGRP];
  1711. if (nonzero)
  1712. do_div(a_srttb, nonzero);
  1713. r_dcount = st->stats[GFS2_LKS_DCOUNT];
  1714. var = st->stats[GFS2_LKS_SRTTVARB] +
  1715. gl->gl_stats.stats[GFS2_LKS_SRTTVARB];
  1716. preempt_enable();
  1717. l_srttb = gl->gl_stats.stats[GFS2_LKS_SRTTB];
  1718. l_dcount = gl->gl_stats.stats[GFS2_LKS_DCOUNT];
  1719. if ((l_dcount < 1) || (r_dcount < 1) || (a_srttb == 0))
  1720. return false;
  1721. srttb_diff = a_srttb - l_srttb;
  1722. sqr_diff = srttb_diff * srttb_diff;
  1723. var *= 2;
  1724. if (l_dcount < 8 || r_dcount < 8)
  1725. var *= 2;
  1726. if (loops == 1)
  1727. var *= 2;
  1728. return ((srttb_diff < 0) && (sqr_diff > var));
  1729. }
  1730. /**
  1731. * gfs2_rgrp_used_recently - test if an rgrp has been used recently
  1732. * @rs: The block reservation with the rgrp to test
  1733. * @msecs: The time limit in milliseconds
  1734. *
  1735. * Returns: True if the rgrp glock has been used within the time limit
  1736. */
  1737. static bool gfs2_rgrp_used_recently(const struct gfs2_blkreserv *rs,
  1738. u64 msecs)
  1739. {
  1740. u64 tdiff;
  1741. tdiff = ktime_to_ns(ktime_sub(ktime_get_real(),
  1742. rs->rs_rgd->rd_gl->gl_dstamp));
  1743. return tdiff > (msecs * 1000 * 1000);
  1744. }
  1745. static u32 gfs2_orlov_skip(const struct gfs2_inode *ip)
  1746. {
  1747. const struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1748. u32 skip;
  1749. get_random_bytes(&skip, sizeof(skip));
  1750. return skip % sdp->sd_rgrps;
  1751. }
  1752. static bool gfs2_select_rgrp(struct gfs2_rgrpd **pos, const struct gfs2_rgrpd *begin)
  1753. {
  1754. struct gfs2_rgrpd *rgd = *pos;
  1755. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1756. rgd = gfs2_rgrpd_get_next(rgd);
  1757. if (rgd == NULL)
  1758. rgd = gfs2_rgrpd_get_first(sdp);
  1759. *pos = rgd;
  1760. if (rgd != begin) /* If we didn't wrap */
  1761. return true;
  1762. return false;
  1763. }
  1764. /**
  1765. * fast_to_acquire - determine if a resource group will be fast to acquire
  1766. * @rgd: The rgrp
  1767. *
  1768. * If this is one of our preferred rgrps, it should be quicker to acquire,
  1769. * because we tried to set ourselves up as dlm lock master.
  1770. */
  1771. static inline int fast_to_acquire(struct gfs2_rgrpd *rgd)
  1772. {
  1773. struct gfs2_glock *gl = rgd->rd_gl;
  1774. if (gl->gl_state != LM_ST_UNLOCKED && list_empty(&gl->gl_holders) &&
  1775. !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) &&
  1776. !test_bit(GLF_DEMOTE, &gl->gl_flags))
  1777. return 1;
  1778. if (rgd->rd_flags & GFS2_RDF_PREFERRED)
  1779. return 1;
  1780. return 0;
  1781. }
  1782. /**
  1783. * gfs2_inplace_reserve - Reserve space in the filesystem
  1784. * @ip: the inode to reserve space for
  1785. * @ap: the allocation parameters
  1786. *
  1787. * We try our best to find an rgrp that has at least ap->target blocks
  1788. * available. After a couple of passes (loops == 2), the prospects of finding
  1789. * such an rgrp diminish. At this stage, we return the first rgrp that has
  1790. * at least ap->min_target blocks available.
  1791. *
  1792. * Returns: 0 on success,
  1793. * -ENOMEM if a suitable rgrp can't be found
  1794. * errno otherwise
  1795. */
  1796. int gfs2_inplace_reserve(struct gfs2_inode *ip, struct gfs2_alloc_parms *ap)
  1797. {
  1798. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1799. struct gfs2_rgrpd *begin = NULL;
  1800. struct gfs2_blkreserv *rs = &ip->i_res;
  1801. int error = 0, flags = LM_FLAG_NODE_SCOPE;
  1802. bool rg_locked;
  1803. u64 last_unlinked = NO_BLOCK;
  1804. u32 target = ap->target;
  1805. int loops = 0;
  1806. u32 free_blocks, blocks_available, skip = 0;
  1807. BUG_ON(rs->rs_reserved);
  1808. if (sdp->sd_args.ar_rgrplvb)
  1809. flags |= GL_SKIP;
  1810. if (gfs2_assert_warn(sdp, target))
  1811. return -EINVAL;
  1812. if (gfs2_rs_active(rs)) {
  1813. begin = rs->rs_rgd;
  1814. } else if (rs->rs_rgd &&
  1815. rgrp_contains_block(rs->rs_rgd, ip->i_goal)) {
  1816. begin = rs->rs_rgd;
  1817. } else {
  1818. check_and_update_goal(ip);
  1819. rs->rs_rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1);
  1820. }
  1821. if (S_ISDIR(ip->i_inode.i_mode) && (ap->aflags & GFS2_AF_ORLOV))
  1822. skip = gfs2_orlov_skip(ip);
  1823. if (rs->rs_rgd == NULL)
  1824. return -EBADSLT;
  1825. while (loops < 3) {
  1826. struct gfs2_rgrpd *rgd;
  1827. rg_locked = gfs2_glock_is_locked_by_me(rs->rs_rgd->rd_gl);
  1828. if (rg_locked) {
  1829. rgrp_lock_local(rs->rs_rgd);
  1830. } else {
  1831. if (skip && skip--)
  1832. goto next_rgrp;
  1833. if (!gfs2_rs_active(rs)) {
  1834. if (loops == 0 &&
  1835. !fast_to_acquire(rs->rs_rgd))
  1836. goto next_rgrp;
  1837. if ((loops < 2) &&
  1838. gfs2_rgrp_used_recently(rs, 1000) &&
  1839. gfs2_rgrp_congested(rs->rs_rgd, loops))
  1840. goto next_rgrp;
  1841. }
  1842. error = gfs2_glock_nq_init(rs->rs_rgd->rd_gl,
  1843. LM_ST_EXCLUSIVE, flags,
  1844. &ip->i_rgd_gh);
  1845. if (unlikely(error))
  1846. return error;
  1847. rgrp_lock_local(rs->rs_rgd);
  1848. if (!gfs2_rs_active(rs) && (loops < 2) &&
  1849. gfs2_rgrp_congested(rs->rs_rgd, loops))
  1850. goto skip_rgrp;
  1851. if (sdp->sd_args.ar_rgrplvb) {
  1852. error = update_rgrp_lvb(rs->rs_rgd,
  1853. &ip->i_rgd_gh);
  1854. if (unlikely(error)) {
  1855. rgrp_unlock_local(rs->rs_rgd);
  1856. gfs2_glock_dq_uninit(&ip->i_rgd_gh);
  1857. return error;
  1858. }
  1859. }
  1860. }
  1861. /* Skip unusable resource groups */
  1862. if ((rs->rs_rgd->rd_flags & (GFS2_RGF_NOALLOC |
  1863. GFS2_RDF_ERROR)) ||
  1864. (loops == 0 && target > rs->rs_rgd->rd_extfail_pt))
  1865. goto skip_rgrp;
  1866. if (sdp->sd_args.ar_rgrplvb) {
  1867. error = gfs2_instantiate(&ip->i_rgd_gh);
  1868. if (error)
  1869. goto skip_rgrp;
  1870. }
  1871. /* Get a reservation if we don't already have one */
  1872. if (!gfs2_rs_active(rs))
  1873. rg_mblk_search(rs->rs_rgd, ip, ap);
  1874. /* Skip rgrps when we can't get a reservation on first pass */
  1875. if (!gfs2_rs_active(rs) && (loops < 1))
  1876. goto check_rgrp;
  1877. /* If rgrp has enough free space, use it */
  1878. rgd = rs->rs_rgd;
  1879. spin_lock(&rgd->rd_rsspin);
  1880. free_blocks = rgd_free(rgd, rs);
  1881. blocks_available = rgd->rd_free_clone - rgd->rd_reserved;
  1882. if (free_blocks < target || blocks_available < target) {
  1883. spin_unlock(&rgd->rd_rsspin);
  1884. goto check_rgrp;
  1885. }
  1886. rs->rs_reserved = ap->target;
  1887. if (rs->rs_reserved > blocks_available)
  1888. rs->rs_reserved = blocks_available;
  1889. rgd->rd_reserved += rs->rs_reserved;
  1890. spin_unlock(&rgd->rd_rsspin);
  1891. rgrp_unlock_local(rs->rs_rgd);
  1892. return 0;
  1893. check_rgrp:
  1894. /* Check for unlinked inodes which can be reclaimed */
  1895. if (rs->rs_rgd->rd_flags & GFS2_RDF_CHECK)
  1896. try_rgrp_unlink(rs->rs_rgd, &last_unlinked,
  1897. ip->i_no_addr);
  1898. skip_rgrp:
  1899. rgrp_unlock_local(rs->rs_rgd);
  1900. /* Drop reservation, if we couldn't use reserved rgrp */
  1901. if (gfs2_rs_active(rs))
  1902. gfs2_rs_deltree(rs);
  1903. /* Unlock rgrp if required */
  1904. if (!rg_locked)
  1905. gfs2_glock_dq_uninit(&ip->i_rgd_gh);
  1906. next_rgrp:
  1907. /* Find the next rgrp, and continue looking */
  1908. if (gfs2_select_rgrp(&rs->rs_rgd, begin))
  1909. continue;
  1910. if (skip)
  1911. continue;
  1912. /* If we've scanned all the rgrps, but found no free blocks
  1913. * then this checks for some less likely conditions before
  1914. * trying again.
  1915. */
  1916. loops++;
  1917. /* Check that fs hasn't grown if writing to rindex */
  1918. if (ip == GFS2_I(sdp->sd_rindex) && !sdp->sd_rindex_uptodate) {
  1919. error = gfs2_ri_update(ip);
  1920. if (error)
  1921. return error;
  1922. }
  1923. /* Flushing the log may release space */
  1924. if (loops == 2) {
  1925. if (ap->min_target)
  1926. target = ap->min_target;
  1927. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  1928. GFS2_LFC_INPLACE_RESERVE);
  1929. }
  1930. }
  1931. return -ENOSPC;
  1932. }
  1933. /**
  1934. * gfs2_inplace_release - release an inplace reservation
  1935. * @ip: the inode the reservation was taken out on
  1936. *
  1937. * Release a reservation made by gfs2_inplace_reserve().
  1938. */
  1939. void gfs2_inplace_release(struct gfs2_inode *ip)
  1940. {
  1941. struct gfs2_blkreserv *rs = &ip->i_res;
  1942. if (rs->rs_reserved) {
  1943. struct gfs2_rgrpd *rgd = rs->rs_rgd;
  1944. spin_lock(&rgd->rd_rsspin);
  1945. GLOCK_BUG_ON(rgd->rd_gl, rgd->rd_reserved < rs->rs_reserved);
  1946. rgd->rd_reserved -= rs->rs_reserved;
  1947. spin_unlock(&rgd->rd_rsspin);
  1948. rs->rs_reserved = 0;
  1949. }
  1950. if (gfs2_holder_initialized(&ip->i_rgd_gh))
  1951. gfs2_glock_dq_uninit(&ip->i_rgd_gh);
  1952. }
  1953. /**
  1954. * gfs2_alloc_extent - allocate an extent from a given bitmap
  1955. * @rbm: the resource group information
  1956. * @dinode: TRUE if the first block we allocate is for a dinode
  1957. * @n: The extent length (value/result)
  1958. *
  1959. * Add the bitmap buffer to the transaction.
  1960. * Set the found bits to @new_state to change block's allocation state.
  1961. */
  1962. static void gfs2_alloc_extent(const struct gfs2_rbm *rbm, bool dinode,
  1963. unsigned int *n)
  1964. {
  1965. struct gfs2_rbm pos = { .rgd = rbm->rgd, };
  1966. const unsigned int elen = *n;
  1967. u64 block;
  1968. int ret;
  1969. *n = 1;
  1970. block = gfs2_rbm_to_block(rbm);
  1971. gfs2_trans_add_meta(rbm->rgd->rd_gl, rbm_bi(rbm)->bi_bh);
  1972. gfs2_setbit(rbm, true, dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
  1973. block++;
  1974. while (*n < elen) {
  1975. ret = gfs2_rbm_from_block(&pos, block);
  1976. if (ret || gfs2_testbit(&pos, true) != GFS2_BLKST_FREE)
  1977. break;
  1978. gfs2_trans_add_meta(pos.rgd->rd_gl, rbm_bi(&pos)->bi_bh);
  1979. gfs2_setbit(&pos, true, GFS2_BLKST_USED);
  1980. (*n)++;
  1981. block++;
  1982. }
  1983. }
  1984. /**
  1985. * rgblk_free - Change alloc state of given block(s)
  1986. * @sdp: the filesystem
  1987. * @rgd: the resource group the blocks are in
  1988. * @bstart: the start of a run of blocks to free
  1989. * @blen: the length of the block run (all must lie within ONE RG!)
  1990. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1991. */
  1992. static void rgblk_free(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd,
  1993. u64 bstart, u32 blen, unsigned char new_state)
  1994. {
  1995. struct gfs2_rbm rbm;
  1996. struct gfs2_bitmap *bi, *bi_prev = NULL;
  1997. rbm.rgd = rgd;
  1998. if (WARN_ON_ONCE(gfs2_rbm_from_block(&rbm, bstart)))
  1999. return;
  2000. while (blen--) {
  2001. bi = rbm_bi(&rbm);
  2002. if (bi != bi_prev) {
  2003. if (!bi->bi_clone) {
  2004. bi->bi_clone = kmalloc(bi->bi_bh->b_size,
  2005. GFP_NOFS | __GFP_NOFAIL);
  2006. memcpy(bi->bi_clone + bi->bi_offset,
  2007. bi->bi_bh->b_data + bi->bi_offset,
  2008. bi->bi_bytes);
  2009. }
  2010. gfs2_trans_add_meta(rbm.rgd->rd_gl, bi->bi_bh);
  2011. bi_prev = bi;
  2012. }
  2013. gfs2_setbit(&rbm, false, new_state);
  2014. gfs2_rbm_add(&rbm, 1);
  2015. }
  2016. }
  2017. /**
  2018. * gfs2_rgrp_dump - print out an rgrp
  2019. * @seq: The iterator
  2020. * @rgd: The rgrp in question
  2021. * @fs_id_buf: pointer to file system id (if requested)
  2022. *
  2023. */
  2024. void gfs2_rgrp_dump(struct seq_file *seq, struct gfs2_rgrpd *rgd,
  2025. const char *fs_id_buf)
  2026. {
  2027. struct gfs2_blkreserv *trs;
  2028. const struct rb_node *n;
  2029. spin_lock(&rgd->rd_rsspin);
  2030. gfs2_print_dbg(seq, "%s R: n:%llu f:%02x b:%u/%u i:%u q:%u r:%u e:%u\n",
  2031. fs_id_buf,
  2032. (unsigned long long)rgd->rd_addr, rgd->rd_flags,
  2033. rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes,
  2034. rgd->rd_requested, rgd->rd_reserved, rgd->rd_extfail_pt);
  2035. if (rgd->rd_sbd->sd_args.ar_rgrplvb && rgd->rd_rgl) {
  2036. struct gfs2_rgrp_lvb *rgl = rgd->rd_rgl;
  2037. gfs2_print_dbg(seq, "%s L: f:%02x b:%u i:%u\n", fs_id_buf,
  2038. be32_to_cpu(rgl->rl_flags),
  2039. be32_to_cpu(rgl->rl_free),
  2040. be32_to_cpu(rgl->rl_dinodes));
  2041. }
  2042. for (n = rb_first(&rgd->rd_rstree); n; n = rb_next(&trs->rs_node)) {
  2043. trs = rb_entry(n, struct gfs2_blkreserv, rs_node);
  2044. dump_rs(seq, trs, fs_id_buf);
  2045. }
  2046. spin_unlock(&rgd->rd_rsspin);
  2047. }
  2048. static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
  2049. {
  2050. struct gfs2_sbd *sdp = rgd->rd_sbd;
  2051. char fs_id_buf[sizeof(sdp->sd_fsname) + 7];
  2052. fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
  2053. (unsigned long long)rgd->rd_addr);
  2054. fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
  2055. sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname);
  2056. gfs2_rgrp_dump(NULL, rgd, fs_id_buf);
  2057. rgd->rd_flags |= GFS2_RDF_ERROR;
  2058. }
  2059. /**
  2060. * gfs2_adjust_reservation - Adjust (or remove) a reservation after allocation
  2061. * @ip: The inode we have just allocated blocks for
  2062. * @rbm: The start of the allocated blocks
  2063. * @len: The extent length
  2064. *
  2065. * Adjusts a reservation after an allocation has taken place. If the
  2066. * reservation does not match the allocation, or if it is now empty
  2067. * then it is removed.
  2068. */
  2069. static void gfs2_adjust_reservation(struct gfs2_inode *ip,
  2070. const struct gfs2_rbm *rbm, unsigned len)
  2071. {
  2072. struct gfs2_blkreserv *rs = &ip->i_res;
  2073. struct gfs2_rgrpd *rgd = rbm->rgd;
  2074. BUG_ON(rs->rs_reserved < len);
  2075. rs->rs_reserved -= len;
  2076. if (gfs2_rs_active(rs)) {
  2077. u64 start = gfs2_rbm_to_block(rbm);
  2078. if (rs->rs_start == start) {
  2079. unsigned int rlen;
  2080. rs->rs_start += len;
  2081. rlen = min(rs->rs_requested, len);
  2082. rs->rs_requested -= rlen;
  2083. rgd->rd_requested -= rlen;
  2084. trace_gfs2_rs(rs, TRACE_RS_CLAIM);
  2085. if (rs->rs_start < rgd->rd_data0 + rgd->rd_data &&
  2086. rs->rs_requested)
  2087. return;
  2088. /* We used up our block reservation, so we should
  2089. reserve more blocks next time. */
  2090. atomic_add(RGRP_RSRV_ADDBLKS, &ip->i_sizehint);
  2091. }
  2092. __rs_deltree(rs);
  2093. }
  2094. }
  2095. /**
  2096. * gfs2_set_alloc_start - Set starting point for block allocation
  2097. * @rbm: The rbm which will be set to the required location
  2098. * @ip: The gfs2 inode
  2099. * @dinode: Flag to say if allocation includes a new inode
  2100. *
  2101. * This sets the starting point from the reservation if one is active
  2102. * otherwise it falls back to guessing a start point based on the
  2103. * inode's goal block or the last allocation point in the rgrp.
  2104. */
  2105. static void gfs2_set_alloc_start(struct gfs2_rbm *rbm,
  2106. const struct gfs2_inode *ip, bool dinode)
  2107. {
  2108. u64 goal;
  2109. if (gfs2_rs_active(&ip->i_res)) {
  2110. goal = ip->i_res.rs_start;
  2111. } else {
  2112. if (!dinode && rgrp_contains_block(rbm->rgd, ip->i_goal))
  2113. goal = ip->i_goal;
  2114. else
  2115. goal = rbm->rgd->rd_last_alloc + rbm->rgd->rd_data0;
  2116. }
  2117. if (WARN_ON_ONCE(gfs2_rbm_from_block(rbm, goal))) {
  2118. rbm->bii = 0;
  2119. rbm->offset = 0;
  2120. }
  2121. }
  2122. /**
  2123. * gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode
  2124. * @ip: the inode to allocate the block for
  2125. * @bn: Used to return the starting block number
  2126. * @nblocks: requested number of blocks/extent length (value/result)
  2127. * @dinode: 1 if we're allocating a dinode block, else 0
  2128. *
  2129. * Returns: 0 or error
  2130. */
  2131. int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks,
  2132. bool dinode)
  2133. {
  2134. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  2135. struct buffer_head *dibh;
  2136. struct gfs2_rbm rbm = { .rgd = ip->i_res.rs_rgd, };
  2137. u64 block; /* block, within the file system scope */
  2138. u32 minext = 1;
  2139. int error = -ENOSPC;
  2140. BUG_ON(ip->i_res.rs_reserved < *nblocks);
  2141. rgrp_lock_local(rbm.rgd);
  2142. if (gfs2_rs_active(&ip->i_res)) {
  2143. gfs2_set_alloc_start(&rbm, ip, dinode);
  2144. error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, &minext, &ip->i_res, false);
  2145. }
  2146. if (error == -ENOSPC) {
  2147. gfs2_set_alloc_start(&rbm, ip, dinode);
  2148. error = gfs2_rbm_find(&rbm, GFS2_BLKST_FREE, &minext, NULL, false);
  2149. }
  2150. /* Since all blocks are reserved in advance, this shouldn't happen */
  2151. if (error) {
  2152. fs_warn(sdp, "inum=%llu error=%d, nblocks=%u, full=%d fail_pt=%d\n",
  2153. (unsigned long long)ip->i_no_addr, error, *nblocks,
  2154. test_bit(GBF_FULL, &rbm.rgd->rd_bits->bi_flags),
  2155. rbm.rgd->rd_extfail_pt);
  2156. goto rgrp_error;
  2157. }
  2158. gfs2_alloc_extent(&rbm, dinode, nblocks);
  2159. block = gfs2_rbm_to_block(&rbm);
  2160. rbm.rgd->rd_last_alloc = block - rbm.rgd->rd_data0;
  2161. if (!dinode) {
  2162. ip->i_goal = block + *nblocks - 1;
  2163. error = gfs2_meta_inode_buffer(ip, &dibh);
  2164. if (error == 0) {
  2165. struct gfs2_dinode *di =
  2166. (struct gfs2_dinode *)dibh->b_data;
  2167. gfs2_trans_add_meta(ip->i_gl, dibh);
  2168. di->di_goal_meta = di->di_goal_data =
  2169. cpu_to_be64(ip->i_goal);
  2170. brelse(dibh);
  2171. }
  2172. }
  2173. spin_lock(&rbm.rgd->rd_rsspin);
  2174. gfs2_adjust_reservation(ip, &rbm, *nblocks);
  2175. if (rbm.rgd->rd_free < *nblocks || rbm.rgd->rd_reserved < *nblocks) {
  2176. fs_warn(sdp, "nblocks=%u\n", *nblocks);
  2177. spin_unlock(&rbm.rgd->rd_rsspin);
  2178. goto rgrp_error;
  2179. }
  2180. GLOCK_BUG_ON(rbm.rgd->rd_gl, rbm.rgd->rd_reserved < *nblocks);
  2181. GLOCK_BUG_ON(rbm.rgd->rd_gl, rbm.rgd->rd_free_clone < *nblocks);
  2182. GLOCK_BUG_ON(rbm.rgd->rd_gl, rbm.rgd->rd_free < *nblocks);
  2183. rbm.rgd->rd_reserved -= *nblocks;
  2184. rbm.rgd->rd_free_clone -= *nblocks;
  2185. rbm.rgd->rd_free -= *nblocks;
  2186. spin_unlock(&rbm.rgd->rd_rsspin);
  2187. if (dinode) {
  2188. u64 generation;
  2189. rbm.rgd->rd_dinodes++;
  2190. generation = rbm.rgd->rd_igeneration++;
  2191. if (generation == 0)
  2192. generation = rbm.rgd->rd_igeneration++;
  2193. ip->i_generation = generation;
  2194. }
  2195. gfs2_trans_add_meta(rbm.rgd->rd_gl, rbm.rgd->rd_bits[0].bi_bh);
  2196. gfs2_rgrp_out(rbm.rgd, rbm.rgd->rd_bits[0].bi_bh->b_data);
  2197. rgrp_unlock_local(rbm.rgd);
  2198. gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0);
  2199. if (dinode)
  2200. gfs2_trans_remove_revoke(sdp, block, *nblocks);
  2201. gfs2_quota_change(ip, *nblocks, ip->i_inode.i_uid, ip->i_inode.i_gid);
  2202. trace_gfs2_block_alloc(ip, rbm.rgd, block, *nblocks,
  2203. dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
  2204. *bn = block;
  2205. return 0;
  2206. rgrp_error:
  2207. rgrp_unlock_local(rbm.rgd);
  2208. gfs2_rgrp_error(rbm.rgd);
  2209. return -EIO;
  2210. }
  2211. /**
  2212. * __gfs2_free_blocks - free a contiguous run of block(s)
  2213. * @ip: the inode these blocks are being freed from
  2214. * @rgd: the resource group the blocks are in
  2215. * @bstart: first block of a run of contiguous blocks
  2216. * @blen: the length of the block run
  2217. * @meta: 1 if the blocks represent metadata
  2218. *
  2219. */
  2220. void __gfs2_free_blocks(struct gfs2_inode *ip, struct gfs2_rgrpd *rgd,
  2221. u64 bstart, u32 blen, int meta)
  2222. {
  2223. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  2224. rgrp_lock_local(rgd);
  2225. rgblk_free(sdp, rgd, bstart, blen, GFS2_BLKST_FREE);
  2226. trace_gfs2_block_alloc(ip, rgd, bstart, blen, GFS2_BLKST_FREE);
  2227. rgd->rd_free += blen;
  2228. rgd->rd_flags &= ~GFS2_RGF_TRIMMED;
  2229. gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
  2230. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  2231. rgrp_unlock_local(rgd);
  2232. /* Directories keep their data in the metadata address space */
  2233. if (meta || ip->i_depth || gfs2_is_jdata(ip))
  2234. gfs2_journal_wipe(ip, bstart, blen);
  2235. }
  2236. /**
  2237. * gfs2_free_meta - free a contiguous run of data block(s)
  2238. * @ip: the inode these blocks are being freed from
  2239. * @rgd: the resource group the blocks are in
  2240. * @bstart: first block of a run of contiguous blocks
  2241. * @blen: the length of the block run
  2242. *
  2243. */
  2244. void gfs2_free_meta(struct gfs2_inode *ip, struct gfs2_rgrpd *rgd,
  2245. u64 bstart, u32 blen)
  2246. {
  2247. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  2248. __gfs2_free_blocks(ip, rgd, bstart, blen, 1);
  2249. gfs2_statfs_change(sdp, 0, +blen, 0);
  2250. gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
  2251. }
  2252. void gfs2_unlink_di(struct inode *inode)
  2253. {
  2254. struct gfs2_inode *ip = GFS2_I(inode);
  2255. struct gfs2_sbd *sdp = GFS2_SB(inode);
  2256. struct gfs2_rgrpd *rgd;
  2257. u64 blkno = ip->i_no_addr;
  2258. rgd = gfs2_blk2rgrpd(sdp, blkno, true);
  2259. if (!rgd)
  2260. return;
  2261. rgrp_lock_local(rgd);
  2262. rgblk_free(sdp, rgd, blkno, 1, GFS2_BLKST_UNLINKED);
  2263. trace_gfs2_block_alloc(ip, rgd, blkno, 1, GFS2_BLKST_UNLINKED);
  2264. gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
  2265. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  2266. be32_add_cpu(&rgd->rd_rgl->rl_unlinked, 1);
  2267. rgrp_unlock_local(rgd);
  2268. }
  2269. void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
  2270. {
  2271. struct gfs2_sbd *sdp = rgd->rd_sbd;
  2272. rgrp_lock_local(rgd);
  2273. rgblk_free(sdp, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE);
  2274. if (!rgd->rd_dinodes)
  2275. gfs2_consist_rgrpd(rgd);
  2276. rgd->rd_dinodes--;
  2277. rgd->rd_free++;
  2278. gfs2_trans_add_meta(rgd->rd_gl, rgd->rd_bits[0].bi_bh);
  2279. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  2280. be32_add_cpu(&rgd->rd_rgl->rl_unlinked, -1);
  2281. rgrp_unlock_local(rgd);
  2282. gfs2_statfs_change(sdp, 0, +1, -1);
  2283. trace_gfs2_block_alloc(ip, rgd, ip->i_no_addr, 1, GFS2_BLKST_FREE);
  2284. gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
  2285. gfs2_journal_wipe(ip, ip->i_no_addr, 1);
  2286. }
  2287. /**
  2288. * gfs2_check_blk_type - Check the type of a block
  2289. * @sdp: The superblock
  2290. * @no_addr: The block number to check
  2291. * @type: The block type we are looking for
  2292. *
  2293. * The inode glock of @no_addr must be held. The @type to check for is either
  2294. * GFS2_BLKST_DINODE or GFS2_BLKST_UNLINKED; checking for type GFS2_BLKST_FREE
  2295. * or GFS2_BLKST_USED would make no sense.
  2296. *
  2297. * Returns: 0 if the block type matches the expected type
  2298. * -ESTALE if it doesn't match
  2299. * or -ve errno if something went wrong while checking
  2300. */
  2301. int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
  2302. {
  2303. struct gfs2_rgrpd *rgd;
  2304. struct gfs2_holder rgd_gh;
  2305. struct gfs2_rbm rbm;
  2306. int error = -EINVAL;
  2307. rgd = gfs2_blk2rgrpd(sdp, no_addr, 1);
  2308. if (!rgd)
  2309. goto fail;
  2310. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
  2311. if (error)
  2312. goto fail;
  2313. rbm.rgd = rgd;
  2314. error = gfs2_rbm_from_block(&rbm, no_addr);
  2315. if (!WARN_ON_ONCE(error)) {
  2316. /*
  2317. * No need to take the local resource group lock here; the
  2318. * inode glock of @no_addr provides the necessary
  2319. * synchronization in case the block is an inode. (In case
  2320. * the block is not an inode, the block type will not match
  2321. * the @type we are looking for.)
  2322. */
  2323. if (gfs2_testbit(&rbm, false) != type)
  2324. error = -ESTALE;
  2325. }
  2326. gfs2_glock_dq_uninit(&rgd_gh);
  2327. fail:
  2328. return error;
  2329. }
  2330. /**
  2331. * gfs2_rlist_add - add a RG to a list of RGs
  2332. * @ip: the inode
  2333. * @rlist: the list of resource groups
  2334. * @block: the block
  2335. *
  2336. * Figure out what RG a block belongs to and add that RG to the list
  2337. *
  2338. * FIXME: Don't use NOFAIL
  2339. *
  2340. */
  2341. void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist,
  2342. u64 block)
  2343. {
  2344. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  2345. struct gfs2_rgrpd *rgd;
  2346. struct gfs2_rgrpd **tmp;
  2347. unsigned int new_space;
  2348. unsigned int x;
  2349. if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
  2350. return;
  2351. /*
  2352. * The resource group last accessed is kept in the last position.
  2353. */
  2354. if (rlist->rl_rgrps) {
  2355. rgd = rlist->rl_rgd[rlist->rl_rgrps - 1];
  2356. if (rgrp_contains_block(rgd, block))
  2357. return;
  2358. rgd = gfs2_blk2rgrpd(sdp, block, 1);
  2359. } else {
  2360. rgd = ip->i_res.rs_rgd;
  2361. if (!rgd || !rgrp_contains_block(rgd, block))
  2362. rgd = gfs2_blk2rgrpd(sdp, block, 1);
  2363. }
  2364. if (!rgd) {
  2365. fs_err(sdp, "rlist_add: no rgrp for block %llu\n",
  2366. (unsigned long long)block);
  2367. return;
  2368. }
  2369. for (x = 0; x < rlist->rl_rgrps; x++) {
  2370. if (rlist->rl_rgd[x] == rgd) {
  2371. swap(rlist->rl_rgd[x],
  2372. rlist->rl_rgd[rlist->rl_rgrps - 1]);
  2373. return;
  2374. }
  2375. }
  2376. if (rlist->rl_rgrps == rlist->rl_space) {
  2377. new_space = rlist->rl_space + 10;
  2378. tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
  2379. GFP_NOFS | __GFP_NOFAIL);
  2380. if (rlist->rl_rgd) {
  2381. memcpy(tmp, rlist->rl_rgd,
  2382. rlist->rl_space * sizeof(struct gfs2_rgrpd *));
  2383. kfree(rlist->rl_rgd);
  2384. }
  2385. rlist->rl_space = new_space;
  2386. rlist->rl_rgd = tmp;
  2387. }
  2388. rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
  2389. }
  2390. /**
  2391. * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
  2392. * and initialize an array of glock holders for them
  2393. * @rlist: the list of resource groups
  2394. * @state: the state we're requesting
  2395. * @flags: the modifier flags
  2396. *
  2397. * FIXME: Don't use NOFAIL
  2398. *
  2399. */
  2400. void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist,
  2401. unsigned int state, u16 flags)
  2402. {
  2403. unsigned int x;
  2404. rlist->rl_ghs = kmalloc_array(rlist->rl_rgrps,
  2405. sizeof(struct gfs2_holder),
  2406. GFP_NOFS | __GFP_NOFAIL);
  2407. for (x = 0; x < rlist->rl_rgrps; x++)
  2408. gfs2_holder_init(rlist->rl_rgd[x]->rd_gl, state, flags,
  2409. &rlist->rl_ghs[x]);
  2410. }
  2411. /**
  2412. * gfs2_rlist_free - free a resource group list
  2413. * @rlist: the list of resource groups
  2414. *
  2415. */
  2416. void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
  2417. {
  2418. unsigned int x;
  2419. kfree(rlist->rl_rgd);
  2420. if (rlist->rl_ghs) {
  2421. for (x = 0; x < rlist->rl_rgrps; x++)
  2422. gfs2_holder_uninit(&rlist->rl_ghs[x]);
  2423. kfree(rlist->rl_ghs);
  2424. rlist->rl_ghs = NULL;
  2425. }
  2426. }
  2427. void rgrp_lock_local(struct gfs2_rgrpd *rgd)
  2428. {
  2429. mutex_lock(&rgd->rd_mutex);
  2430. }
  2431. void rgrp_unlock_local(struct gfs2_rgrpd *rgd)
  2432. {
  2433. mutex_unlock(&rgd->rd_mutex);
  2434. }