rgrp.c 66 KB

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