rgrp.c 60 KB

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