rgrp.c 70 KB

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