rgrp.c 70 KB

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