rgrp.c 63 KB

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