rgrp.c 69 KB

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