rgrp.c 63 KB

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