rgrp.c 64 KB

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