rgrp.c 60 KB

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