rgrp.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799
  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. #if BITS_PER_LONG == 32
  35. #define LBITMASK (0x55555555UL)
  36. #define LBITSKIP55 (0x55555555UL)
  37. #define LBITSKIP00 (0x00000000UL)
  38. #else
  39. #define LBITMASK (0x5555555555555555UL)
  40. #define LBITSKIP55 (0x5555555555555555UL)
  41. #define LBITSKIP00 (0x0000000000000000UL)
  42. #endif
  43. /*
  44. * These routines are used by the resource group routines (rgrp.c)
  45. * to keep track of block allocation. Each block is represented by two
  46. * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
  47. *
  48. * 0 = Free
  49. * 1 = Used (not metadata)
  50. * 2 = Unlinked (still in use) inode
  51. * 3 = Used (metadata)
  52. */
  53. static const char valid_change[16] = {
  54. /* current */
  55. /* n */ 0, 1, 1, 1,
  56. /* e */ 1, 0, 0, 0,
  57. /* w */ 0, 0, 0, 1,
  58. 1, 0, 0, 0
  59. };
  60. static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
  61. unsigned char old_state,
  62. struct gfs2_bitmap **rbi);
  63. /**
  64. * gfs2_setbit - Set a bit in the bitmaps
  65. * @rgd: the resource group descriptor
  66. * @buf1: the primary buffer that holds the bitmaps
  67. * @buf2: the clone buffer that holds the bitmaps
  68. * @bi: the bitmap structure
  69. * @block: the block to set
  70. * @new_state: the new state of the block
  71. *
  72. */
  73. static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf1,
  74. unsigned char *buf2, struct gfs2_bitmap *bi,
  75. u32 block, unsigned char new_state)
  76. {
  77. unsigned char *byte1, *byte2, *end, cur_state;
  78. unsigned int buflen = bi->bi_len;
  79. const unsigned int bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
  80. byte1 = buf1 + bi->bi_offset + (block / GFS2_NBBY);
  81. end = buf1 + bi->bi_offset + buflen;
  82. BUG_ON(byte1 >= end);
  83. cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
  84. if (unlikely(!valid_change[new_state * 4 + cur_state])) {
  85. printk(KERN_WARNING "GFS2: buf_blk = 0x%llx old_state=%d, "
  86. "new_state=%d\n",
  87. (unsigned long long)block, cur_state, new_state);
  88. printk(KERN_WARNING "GFS2: rgrp=0x%llx bi_start=0x%lx\n",
  89. (unsigned long long)rgd->rd_addr,
  90. (unsigned long)bi->bi_start);
  91. printk(KERN_WARNING "GFS2: bi_offset=0x%lx bi_len=0x%lx\n",
  92. (unsigned long)bi->bi_offset,
  93. (unsigned long)bi->bi_len);
  94. dump_stack();
  95. gfs2_consist_rgrpd(rgd);
  96. return;
  97. }
  98. *byte1 ^= (cur_state ^ new_state) << bit;
  99. if (buf2) {
  100. byte2 = buf2 + bi->bi_offset + (block / GFS2_NBBY);
  101. cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
  102. *byte2 ^= (cur_state ^ new_state) << bit;
  103. }
  104. }
  105. /**
  106. * gfs2_testbit - test a bit in the bitmaps
  107. * @rgd: the resource group descriptor
  108. * @buffer: the buffer that holds the bitmaps
  109. * @buflen: the length (in bytes) of the buffer
  110. * @block: the block to read
  111. *
  112. */
  113. static inline unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd,
  114. const unsigned char *buffer,
  115. unsigned int buflen, u32 block)
  116. {
  117. const unsigned char *byte, *end;
  118. unsigned char cur_state;
  119. unsigned int bit;
  120. byte = buffer + (block / GFS2_NBBY);
  121. bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
  122. end = buffer + buflen;
  123. gfs2_assert(rgd->rd_sbd, byte < end);
  124. cur_state = (*byte >> bit) & GFS2_BIT_MASK;
  125. return cur_state;
  126. }
  127. /**
  128. * gfs2_bit_search
  129. * @ptr: Pointer to bitmap data
  130. * @mask: Mask to use (normally 0x55555.... but adjusted for search start)
  131. * @state: The state we are searching for
  132. *
  133. * We xor the bitmap data with a patter which is the bitwise opposite
  134. * of what we are looking for, this gives rise to a pattern of ones
  135. * wherever there is a match. Since we have two bits per entry, we
  136. * take this pattern, shift it down by one place and then and it with
  137. * the original. All the even bit positions (0,2,4, etc) then represent
  138. * successful matches, so we mask with 0x55555..... to remove the unwanted
  139. * odd bit positions.
  140. *
  141. * This allows searching of a whole u64 at once (32 blocks) with a
  142. * single test (on 64 bit arches).
  143. */
  144. static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
  145. {
  146. u64 tmp;
  147. static const u64 search[] = {
  148. [0] = 0xffffffffffffffffULL,
  149. [1] = 0xaaaaaaaaaaaaaaaaULL,
  150. [2] = 0x5555555555555555ULL,
  151. [3] = 0x0000000000000000ULL,
  152. };
  153. tmp = le64_to_cpu(*ptr) ^ search[state];
  154. tmp &= (tmp >> 1);
  155. tmp &= mask;
  156. return tmp;
  157. }
  158. /**
  159. * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
  160. * a block in a given allocation state.
  161. * @buf: the buffer that holds the bitmaps
  162. * @len: the length (in bytes) of the buffer
  163. * @goal: start search at this block's bit-pair (within @buffer)
  164. * @state: GFS2_BLKST_XXX the state of the block we're looking for.
  165. *
  166. * Scope of @goal and returned block number is only within this bitmap buffer,
  167. * not entire rgrp or filesystem. @buffer will be offset from the actual
  168. * beginning of a bitmap block buffer, skipping any header structures, but
  169. * headers are always a multiple of 64 bits long so that the buffer is
  170. * always aligned to a 64 bit boundary.
  171. *
  172. * The size of the buffer is in bytes, but is it assumed that it is
  173. * always ok to read a complete multiple of 64 bits at the end
  174. * of the block in case the end is no aligned to a natural boundary.
  175. *
  176. * Return: the block number (bitmap buffer scope) that was found
  177. */
  178. static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
  179. u32 goal, u8 state)
  180. {
  181. u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1);
  182. const __le64 *ptr = ((__le64 *)buf) + (goal >> 5);
  183. const __le64 *end = (__le64 *)(buf + ALIGN(len, sizeof(u64)));
  184. u64 tmp;
  185. u64 mask = 0x5555555555555555ULL;
  186. u32 bit;
  187. BUG_ON(state > 3);
  188. /* Mask off bits we don't care about at the start of the search */
  189. mask <<= spoint;
  190. tmp = gfs2_bit_search(ptr, mask, state);
  191. ptr++;
  192. while(tmp == 0 && ptr < end) {
  193. tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
  194. ptr++;
  195. }
  196. /* Mask off any bits which are more than len bytes from the start */
  197. if (ptr == end && (len & (sizeof(u64) - 1)))
  198. tmp &= (((u64)~0) >> (64 - 8*(len & (sizeof(u64) - 1))));
  199. /* Didn't find anything, so return */
  200. if (tmp == 0)
  201. return BFITNOENT;
  202. ptr--;
  203. bit = __ffs64(tmp);
  204. bit /= 2; /* two bits per entry in the bitmap */
  205. return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
  206. }
  207. /**
  208. * gfs2_bitcount - count the number of bits in a certain state
  209. * @rgd: the resource group descriptor
  210. * @buffer: the buffer that holds the bitmaps
  211. * @buflen: the length (in bytes) of the buffer
  212. * @state: the state of the block we're looking for
  213. *
  214. * Returns: The number of bits
  215. */
  216. static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
  217. unsigned int buflen, u8 state)
  218. {
  219. const u8 *byte = buffer;
  220. const u8 *end = buffer + buflen;
  221. const u8 state1 = state << 2;
  222. const u8 state2 = state << 4;
  223. const u8 state3 = state << 6;
  224. u32 count = 0;
  225. for (; byte < end; byte++) {
  226. if (((*byte) & 0x03) == state)
  227. count++;
  228. if (((*byte) & 0x0C) == state1)
  229. count++;
  230. if (((*byte) & 0x30) == state2)
  231. count++;
  232. if (((*byte) & 0xC0) == state3)
  233. count++;
  234. }
  235. return count;
  236. }
  237. /**
  238. * gfs2_rgrp_verify - Verify that a resource group is consistent
  239. * @rgd: the rgrp
  240. *
  241. */
  242. void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
  243. {
  244. struct gfs2_sbd *sdp = rgd->rd_sbd;
  245. struct gfs2_bitmap *bi = NULL;
  246. u32 length = rgd->rd_length;
  247. u32 count[4], tmp;
  248. int buf, x;
  249. memset(count, 0, 4 * sizeof(u32));
  250. /* Count # blocks in each of 4 possible allocation states */
  251. for (buf = 0; buf < length; buf++) {
  252. bi = rgd->rd_bits + buf;
  253. for (x = 0; x < 4; x++)
  254. count[x] += gfs2_bitcount(rgd,
  255. bi->bi_bh->b_data +
  256. bi->bi_offset,
  257. bi->bi_len, x);
  258. }
  259. if (count[0] != rgd->rd_free) {
  260. if (gfs2_consist_rgrpd(rgd))
  261. fs_err(sdp, "free data mismatch: %u != %u\n",
  262. count[0], rgd->rd_free);
  263. return;
  264. }
  265. tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
  266. if (count[1] != tmp) {
  267. if (gfs2_consist_rgrpd(rgd))
  268. fs_err(sdp, "used data mismatch: %u != %u\n",
  269. count[1], tmp);
  270. return;
  271. }
  272. if (count[2] + count[3] != rgd->rd_dinodes) {
  273. if (gfs2_consist_rgrpd(rgd))
  274. fs_err(sdp, "used metadata mismatch: %u != %u\n",
  275. count[2] + count[3], rgd->rd_dinodes);
  276. return;
  277. }
  278. }
  279. static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
  280. {
  281. u64 first = rgd->rd_data0;
  282. u64 last = first + rgd->rd_data;
  283. return first <= block && block < last;
  284. }
  285. /**
  286. * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
  287. * @sdp: The GFS2 superblock
  288. * @blk: The data block number
  289. * @exact: True if this needs to be an exact match
  290. *
  291. * Returns: The resource group, or NULL if not found
  292. */
  293. struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk, bool exact)
  294. {
  295. struct rb_node *n, *next;
  296. struct gfs2_rgrpd *cur;
  297. spin_lock(&sdp->sd_rindex_spin);
  298. n = sdp->sd_rindex_tree.rb_node;
  299. while (n) {
  300. cur = rb_entry(n, struct gfs2_rgrpd, rd_node);
  301. next = NULL;
  302. if (blk < cur->rd_addr)
  303. next = n->rb_left;
  304. else if (blk >= cur->rd_data0 + cur->rd_data)
  305. next = n->rb_right;
  306. if (next == NULL) {
  307. spin_unlock(&sdp->sd_rindex_spin);
  308. if (exact) {
  309. if (blk < cur->rd_addr)
  310. return NULL;
  311. if (blk >= cur->rd_data0 + cur->rd_data)
  312. return NULL;
  313. }
  314. return cur;
  315. }
  316. n = next;
  317. }
  318. spin_unlock(&sdp->sd_rindex_spin);
  319. return NULL;
  320. }
  321. /**
  322. * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
  323. * @sdp: The GFS2 superblock
  324. *
  325. * Returns: The first rgrp in the filesystem
  326. */
  327. struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
  328. {
  329. const struct rb_node *n;
  330. struct gfs2_rgrpd *rgd;
  331. spin_lock(&sdp->sd_rindex_spin);
  332. n = rb_first(&sdp->sd_rindex_tree);
  333. rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
  334. spin_unlock(&sdp->sd_rindex_spin);
  335. return rgd;
  336. }
  337. /**
  338. * gfs2_rgrpd_get_next - get the next RG
  339. * @rgd: the resource group descriptor
  340. *
  341. * Returns: The next rgrp
  342. */
  343. struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
  344. {
  345. struct gfs2_sbd *sdp = rgd->rd_sbd;
  346. const struct rb_node *n;
  347. spin_lock(&sdp->sd_rindex_spin);
  348. n = rb_next(&rgd->rd_node);
  349. if (n == NULL)
  350. n = rb_first(&sdp->sd_rindex_tree);
  351. if (unlikely(&rgd->rd_node == n)) {
  352. spin_unlock(&sdp->sd_rindex_spin);
  353. return NULL;
  354. }
  355. rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
  356. spin_unlock(&sdp->sd_rindex_spin);
  357. return rgd;
  358. }
  359. void gfs2_free_clones(struct gfs2_rgrpd *rgd)
  360. {
  361. int x;
  362. for (x = 0; x < rgd->rd_length; x++) {
  363. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  364. kfree(bi->bi_clone);
  365. bi->bi_clone = NULL;
  366. }
  367. }
  368. void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
  369. {
  370. struct rb_node *n;
  371. struct gfs2_rgrpd *rgd;
  372. struct gfs2_glock *gl;
  373. while ((n = rb_first(&sdp->sd_rindex_tree))) {
  374. rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
  375. gl = rgd->rd_gl;
  376. rb_erase(n, &sdp->sd_rindex_tree);
  377. if (gl) {
  378. spin_lock(&gl->gl_spin);
  379. gl->gl_object = NULL;
  380. spin_unlock(&gl->gl_spin);
  381. gfs2_glock_add_to_lru(gl);
  382. gfs2_glock_put(gl);
  383. }
  384. gfs2_free_clones(rgd);
  385. kfree(rgd->rd_bits);
  386. kmem_cache_free(gfs2_rgrpd_cachep, rgd);
  387. }
  388. }
  389. static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
  390. {
  391. printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
  392. printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
  393. printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
  394. printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
  395. printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
  396. }
  397. /**
  398. * gfs2_compute_bitstructs - Compute the bitmap sizes
  399. * @rgd: The resource group descriptor
  400. *
  401. * Calculates bitmap descriptors, one for each block that contains bitmap data
  402. *
  403. * Returns: errno
  404. */
  405. static int compute_bitstructs(struct gfs2_rgrpd *rgd)
  406. {
  407. struct gfs2_sbd *sdp = rgd->rd_sbd;
  408. struct gfs2_bitmap *bi;
  409. u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
  410. u32 bytes_left, bytes;
  411. int x;
  412. if (!length)
  413. return -EINVAL;
  414. rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
  415. if (!rgd->rd_bits)
  416. return -ENOMEM;
  417. bytes_left = rgd->rd_bitbytes;
  418. for (x = 0; x < length; x++) {
  419. bi = rgd->rd_bits + x;
  420. bi->bi_flags = 0;
  421. /* small rgrp; bitmap stored completely in header block */
  422. if (length == 1) {
  423. bytes = bytes_left;
  424. bi->bi_offset = sizeof(struct gfs2_rgrp);
  425. bi->bi_start = 0;
  426. bi->bi_len = bytes;
  427. /* header block */
  428. } else if (x == 0) {
  429. bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
  430. bi->bi_offset = sizeof(struct gfs2_rgrp);
  431. bi->bi_start = 0;
  432. bi->bi_len = bytes;
  433. /* last block */
  434. } else if (x + 1 == length) {
  435. bytes = bytes_left;
  436. bi->bi_offset = sizeof(struct gfs2_meta_header);
  437. bi->bi_start = rgd->rd_bitbytes - bytes_left;
  438. bi->bi_len = bytes;
  439. /* other blocks */
  440. } else {
  441. bytes = sdp->sd_sb.sb_bsize -
  442. sizeof(struct gfs2_meta_header);
  443. bi->bi_offset = sizeof(struct gfs2_meta_header);
  444. bi->bi_start = rgd->rd_bitbytes - bytes_left;
  445. bi->bi_len = bytes;
  446. }
  447. bytes_left -= bytes;
  448. }
  449. if (bytes_left) {
  450. gfs2_consist_rgrpd(rgd);
  451. return -EIO;
  452. }
  453. bi = rgd->rd_bits + (length - 1);
  454. if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
  455. if (gfs2_consist_rgrpd(rgd)) {
  456. gfs2_rindex_print(rgd);
  457. fs_err(sdp, "start=%u len=%u offset=%u\n",
  458. bi->bi_start, bi->bi_len, bi->bi_offset);
  459. }
  460. return -EIO;
  461. }
  462. return 0;
  463. }
  464. /**
  465. * gfs2_ri_total - Total up the file system space, according to the rindex.
  466. * @sdp: the filesystem
  467. *
  468. */
  469. u64 gfs2_ri_total(struct gfs2_sbd *sdp)
  470. {
  471. u64 total_data = 0;
  472. struct inode *inode = sdp->sd_rindex;
  473. struct gfs2_inode *ip = GFS2_I(inode);
  474. char buf[sizeof(struct gfs2_rindex)];
  475. struct file_ra_state ra_state;
  476. int error, rgrps;
  477. file_ra_state_init(&ra_state, inode->i_mapping);
  478. for (rgrps = 0;; rgrps++) {
  479. loff_t pos = rgrps * sizeof(struct gfs2_rindex);
  480. if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
  481. break;
  482. error = gfs2_internal_read(ip, &ra_state, buf, &pos,
  483. sizeof(struct gfs2_rindex));
  484. if (error != sizeof(struct gfs2_rindex))
  485. break;
  486. total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
  487. }
  488. return total_data;
  489. }
  490. static int rgd_insert(struct gfs2_rgrpd *rgd)
  491. {
  492. struct gfs2_sbd *sdp = rgd->rd_sbd;
  493. struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL;
  494. /* Figure out where to put new node */
  495. while (*newn) {
  496. struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd,
  497. rd_node);
  498. parent = *newn;
  499. if (rgd->rd_addr < cur->rd_addr)
  500. newn = &((*newn)->rb_left);
  501. else if (rgd->rd_addr > cur->rd_addr)
  502. newn = &((*newn)->rb_right);
  503. else
  504. return -EEXIST;
  505. }
  506. rb_link_node(&rgd->rd_node, parent, newn);
  507. rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree);
  508. sdp->sd_rgrps++;
  509. return 0;
  510. }
  511. /**
  512. * read_rindex_entry - Pull in a new resource index entry from the disk
  513. * @ip: The GFS2 inode
  514. * @ra_state: The read-ahead state
  515. *
  516. * Returns: 0 on success, > 0 on EOF, error code otherwise
  517. */
  518. static int read_rindex_entry(struct gfs2_inode *ip,
  519. struct file_ra_state *ra_state)
  520. {
  521. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  522. loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
  523. struct gfs2_rindex buf;
  524. int error;
  525. struct gfs2_rgrpd *rgd;
  526. if (pos >= i_size_read(&ip->i_inode))
  527. return 1;
  528. error = gfs2_internal_read(ip, ra_state, (char *)&buf, &pos,
  529. sizeof(struct gfs2_rindex));
  530. if (error != sizeof(struct gfs2_rindex))
  531. return (error == 0) ? 1 : error;
  532. rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
  533. error = -ENOMEM;
  534. if (!rgd)
  535. return error;
  536. rgd->rd_sbd = sdp;
  537. rgd->rd_addr = be64_to_cpu(buf.ri_addr);
  538. rgd->rd_length = be32_to_cpu(buf.ri_length);
  539. rgd->rd_data0 = be64_to_cpu(buf.ri_data0);
  540. rgd->rd_data = be32_to_cpu(buf.ri_data);
  541. rgd->rd_bitbytes = be32_to_cpu(buf.ri_bitbytes);
  542. error = compute_bitstructs(rgd);
  543. if (error)
  544. goto fail;
  545. error = gfs2_glock_get(sdp, rgd->rd_addr,
  546. &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
  547. if (error)
  548. goto fail;
  549. rgd->rd_gl->gl_object = rgd;
  550. rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
  551. if (rgd->rd_data > sdp->sd_max_rg_data)
  552. sdp->sd_max_rg_data = rgd->rd_data;
  553. spin_lock(&sdp->sd_rindex_spin);
  554. error = rgd_insert(rgd);
  555. spin_unlock(&sdp->sd_rindex_spin);
  556. if (!error)
  557. return 0;
  558. error = 0; /* someone else read in the rgrp; free it and ignore it */
  559. gfs2_glock_put(rgd->rd_gl);
  560. fail:
  561. kfree(rgd->rd_bits);
  562. kmem_cache_free(gfs2_rgrpd_cachep, rgd);
  563. return error;
  564. }
  565. /**
  566. * gfs2_ri_update - Pull in a new resource index from the disk
  567. * @ip: pointer to the rindex inode
  568. *
  569. * Returns: 0 on successful update, error code otherwise
  570. */
  571. static int gfs2_ri_update(struct gfs2_inode *ip)
  572. {
  573. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  574. struct inode *inode = &ip->i_inode;
  575. struct file_ra_state ra_state;
  576. int error;
  577. file_ra_state_init(&ra_state, inode->i_mapping);
  578. do {
  579. error = read_rindex_entry(ip, &ra_state);
  580. } while (error == 0);
  581. if (error < 0)
  582. return error;
  583. sdp->sd_rindex_uptodate = 1;
  584. return 0;
  585. }
  586. /**
  587. * gfs2_rindex_update - Update the rindex if required
  588. * @sdp: The GFS2 superblock
  589. *
  590. * We grab a lock on the rindex inode to make sure that it doesn't
  591. * change whilst we are performing an operation. We keep this lock
  592. * for quite long periods of time compared to other locks. This
  593. * doesn't matter, since it is shared and it is very, very rarely
  594. * accessed in the exclusive mode (i.e. only when expanding the filesystem).
  595. *
  596. * This makes sure that we're using the latest copy of the resource index
  597. * special file, which might have been updated if someone expanded the
  598. * filesystem (via gfs2_grow utility), which adds new resource groups.
  599. *
  600. * Returns: 0 on succeess, error code otherwise
  601. */
  602. int gfs2_rindex_update(struct gfs2_sbd *sdp)
  603. {
  604. struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
  605. struct gfs2_glock *gl = ip->i_gl;
  606. struct gfs2_holder ri_gh;
  607. int error = 0;
  608. int unlock_required = 0;
  609. /* Read new copy from disk if we don't have the latest */
  610. if (!sdp->sd_rindex_uptodate) {
  611. if (!gfs2_glock_is_locked_by_me(gl)) {
  612. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, &ri_gh);
  613. if (error)
  614. return error;
  615. unlock_required = 1;
  616. }
  617. if (!sdp->sd_rindex_uptodate)
  618. error = gfs2_ri_update(ip);
  619. if (unlock_required)
  620. gfs2_glock_dq_uninit(&ri_gh);
  621. }
  622. return error;
  623. }
  624. static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
  625. {
  626. const struct gfs2_rgrp *str = buf;
  627. u32 rg_flags;
  628. rg_flags = be32_to_cpu(str->rg_flags);
  629. rg_flags &= ~GFS2_RDF_MASK;
  630. rgd->rd_flags &= GFS2_RDF_MASK;
  631. rgd->rd_flags |= rg_flags;
  632. rgd->rd_free = be32_to_cpu(str->rg_free);
  633. rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
  634. rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
  635. }
  636. static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
  637. {
  638. struct gfs2_rgrp *str = buf;
  639. str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
  640. str->rg_free = cpu_to_be32(rgd->rd_free);
  641. str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
  642. str->__pad = cpu_to_be32(0);
  643. str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
  644. memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
  645. }
  646. /**
  647. * gfs2_rgrp_go_lock - Read in a RG's header and bitmaps
  648. * @gh: The glock holder for the resource group
  649. *
  650. * Read in all of a Resource Group's header and bitmap blocks.
  651. * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
  652. *
  653. * Returns: errno
  654. */
  655. int gfs2_rgrp_go_lock(struct gfs2_holder *gh)
  656. {
  657. struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
  658. struct gfs2_sbd *sdp = rgd->rd_sbd;
  659. struct gfs2_glock *gl = rgd->rd_gl;
  660. unsigned int length = rgd->rd_length;
  661. struct gfs2_bitmap *bi;
  662. unsigned int x, y;
  663. int error;
  664. for (x = 0; x < length; x++) {
  665. bi = rgd->rd_bits + x;
  666. error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
  667. if (error)
  668. goto fail;
  669. }
  670. for (y = length; y--;) {
  671. bi = rgd->rd_bits + y;
  672. error = gfs2_meta_wait(sdp, bi->bi_bh);
  673. if (error)
  674. goto fail;
  675. if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
  676. GFS2_METATYPE_RG)) {
  677. error = -EIO;
  678. goto fail;
  679. }
  680. }
  681. if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
  682. for (x = 0; x < length; x++)
  683. clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
  684. gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
  685. rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
  686. rgd->rd_free_clone = rgd->rd_free;
  687. }
  688. return 0;
  689. fail:
  690. while (x--) {
  691. bi = rgd->rd_bits + x;
  692. brelse(bi->bi_bh);
  693. bi->bi_bh = NULL;
  694. gfs2_assert_warn(sdp, !bi->bi_clone);
  695. }
  696. return error;
  697. }
  698. /**
  699. * gfs2_rgrp_go_unlock - Release RG bitmaps read in with gfs2_rgrp_bh_get()
  700. * @gh: The glock holder for the resource group
  701. *
  702. */
  703. void gfs2_rgrp_go_unlock(struct gfs2_holder *gh)
  704. {
  705. struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
  706. int x, length = rgd->rd_length;
  707. for (x = 0; x < length; x++) {
  708. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  709. brelse(bi->bi_bh);
  710. bi->bi_bh = NULL;
  711. }
  712. }
  713. int gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
  714. struct buffer_head *bh,
  715. const struct gfs2_bitmap *bi, unsigned minlen, u64 *ptrimmed)
  716. {
  717. struct super_block *sb = sdp->sd_vfs;
  718. struct block_device *bdev = sb->s_bdev;
  719. const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize /
  720. bdev_logical_block_size(sb->s_bdev);
  721. u64 blk;
  722. sector_t start = 0;
  723. sector_t nr_sects = 0;
  724. int rv;
  725. unsigned int x;
  726. u32 trimmed = 0;
  727. u8 diff;
  728. for (x = 0; x < bi->bi_len; x++) {
  729. const u8 *clone = bi->bi_clone ? bi->bi_clone : bi->bi_bh->b_data;
  730. clone += bi->bi_offset;
  731. clone += x;
  732. if (bh) {
  733. const u8 *orig = bh->b_data + bi->bi_offset + x;
  734. diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
  735. } else {
  736. diff = ~(*clone | (*clone >> 1));
  737. }
  738. diff &= 0x55;
  739. if (diff == 0)
  740. continue;
  741. blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
  742. blk *= sects_per_blk; /* convert to sectors */
  743. while(diff) {
  744. if (diff & 1) {
  745. if (nr_sects == 0)
  746. goto start_new_extent;
  747. if ((start + nr_sects) != blk) {
  748. if (nr_sects >= minlen) {
  749. rv = blkdev_issue_discard(bdev,
  750. start, nr_sects,
  751. GFP_NOFS, 0);
  752. if (rv)
  753. goto fail;
  754. trimmed += nr_sects;
  755. }
  756. nr_sects = 0;
  757. start_new_extent:
  758. start = blk;
  759. }
  760. nr_sects += sects_per_blk;
  761. }
  762. diff >>= 2;
  763. blk += sects_per_blk;
  764. }
  765. }
  766. if (nr_sects >= minlen) {
  767. rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 0);
  768. if (rv)
  769. goto fail;
  770. trimmed += nr_sects;
  771. }
  772. if (ptrimmed)
  773. *ptrimmed = trimmed;
  774. return 0;
  775. fail:
  776. if (sdp->sd_args.ar_discard)
  777. fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem", rv);
  778. sdp->sd_args.ar_discard = 0;
  779. return -EIO;
  780. }
  781. /**
  782. * gfs2_fitrim - Generate discard requests for unused bits of the filesystem
  783. * @filp: Any file on the filesystem
  784. * @argp: Pointer to the arguments (also used to pass result)
  785. *
  786. * Returns: 0 on success, otherwise error code
  787. */
  788. int gfs2_fitrim(struct file *filp, void __user *argp)
  789. {
  790. struct inode *inode = filp->f_dentry->d_inode;
  791. struct gfs2_sbd *sdp = GFS2_SB(inode);
  792. struct request_queue *q = bdev_get_queue(sdp->sd_vfs->s_bdev);
  793. struct buffer_head *bh;
  794. struct gfs2_rgrpd *rgd;
  795. struct gfs2_rgrpd *rgd_end;
  796. struct gfs2_holder gh;
  797. struct fstrim_range r;
  798. int ret = 0;
  799. u64 amt;
  800. u64 trimmed = 0;
  801. unsigned int x;
  802. if (!capable(CAP_SYS_ADMIN))
  803. return -EPERM;
  804. if (!blk_queue_discard(q))
  805. return -EOPNOTSUPP;
  806. if (argp == NULL) {
  807. r.start = 0;
  808. r.len = ULLONG_MAX;
  809. r.minlen = 0;
  810. } else if (copy_from_user(&r, argp, sizeof(r)))
  811. return -EFAULT;
  812. ret = gfs2_rindex_update(sdp);
  813. if (ret)
  814. return ret;
  815. rgd = gfs2_blk2rgrpd(sdp, r.start, 0);
  816. rgd_end = gfs2_blk2rgrpd(sdp, r.start + r.len, 0);
  817. while (1) {
  818. ret = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
  819. if (ret)
  820. goto out;
  821. if (!(rgd->rd_flags & GFS2_RGF_TRIMMED)) {
  822. /* Trim each bitmap in the rgrp */
  823. for (x = 0; x < rgd->rd_length; x++) {
  824. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  825. ret = gfs2_rgrp_send_discards(sdp, rgd->rd_data0, NULL, bi, r.minlen, &amt);
  826. if (ret) {
  827. gfs2_glock_dq_uninit(&gh);
  828. goto out;
  829. }
  830. trimmed += amt;
  831. }
  832. /* Mark rgrp as having been trimmed */
  833. ret = gfs2_trans_begin(sdp, RES_RG_HDR, 0);
  834. if (ret == 0) {
  835. bh = rgd->rd_bits[0].bi_bh;
  836. rgd->rd_flags |= GFS2_RGF_TRIMMED;
  837. gfs2_trans_add_bh(rgd->rd_gl, bh, 1);
  838. gfs2_rgrp_out(rgd, bh->b_data);
  839. gfs2_trans_end(sdp);
  840. }
  841. }
  842. gfs2_glock_dq_uninit(&gh);
  843. if (rgd == rgd_end)
  844. break;
  845. rgd = gfs2_rgrpd_get_next(rgd);
  846. }
  847. out:
  848. r.len = trimmed << 9;
  849. if (argp && copy_to_user(argp, &r, sizeof(r)))
  850. return -EFAULT;
  851. return ret;
  852. }
  853. /**
  854. * gfs2_qadata_get - get the struct gfs2_qadata structure for an inode
  855. * @ip: the incore GFS2 inode structure
  856. *
  857. * Returns: the struct gfs2_qadata
  858. */
  859. struct gfs2_qadata *gfs2_qadata_get(struct gfs2_inode *ip)
  860. {
  861. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  862. int error;
  863. BUG_ON(ip->i_qadata != NULL);
  864. ip->i_qadata = kzalloc(sizeof(struct gfs2_qadata), GFP_NOFS);
  865. error = gfs2_rindex_update(sdp);
  866. if (error)
  867. fs_warn(sdp, "rindex update returns %d\n", error);
  868. return ip->i_qadata;
  869. }
  870. /**
  871. * gfs2_blkrsv_get - get the struct gfs2_blkreserv structure for an inode
  872. * @ip: the incore GFS2 inode structure
  873. *
  874. * Returns: the struct gfs2_qadata
  875. */
  876. static int gfs2_blkrsv_get(struct gfs2_inode *ip)
  877. {
  878. BUG_ON(ip->i_res != NULL);
  879. ip->i_res = kmem_cache_zalloc(gfs2_rsrv_cachep, GFP_NOFS);
  880. if (!ip->i_res)
  881. return -ENOMEM;
  882. return 0;
  883. }
  884. /**
  885. * try_rgrp_fit - See if a given reservation will fit in a given RG
  886. * @rgd: the RG data
  887. * @ip: the inode
  888. *
  889. * If there's room for the requested blocks to be allocated from the RG:
  890. *
  891. * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
  892. */
  893. static int try_rgrp_fit(const struct gfs2_rgrpd *rgd, const struct gfs2_inode *ip)
  894. {
  895. const struct gfs2_blkreserv *rs = ip->i_res;
  896. if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR))
  897. return 0;
  898. if (rgd->rd_free_clone >= rs->rs_requested)
  899. return 1;
  900. return 0;
  901. }
  902. static inline u32 gfs2_bi2rgd_blk(struct gfs2_bitmap *bi, u32 blk)
  903. {
  904. return (bi->bi_start * GFS2_NBBY) + blk;
  905. }
  906. /**
  907. * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
  908. * @rgd: The rgrp
  909. * @last_unlinked: block address of the last dinode we unlinked
  910. * @skip: block address we should explicitly not unlink
  911. *
  912. * Returns: 0 if no error
  913. * The inode, if one has been found, in inode.
  914. */
  915. static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
  916. {
  917. u32 goal = 0, block;
  918. u64 no_addr;
  919. struct gfs2_sbd *sdp = rgd->rd_sbd;
  920. struct gfs2_glock *gl;
  921. struct gfs2_inode *ip;
  922. int error;
  923. int found = 0;
  924. struct gfs2_bitmap *bi;
  925. while (goal < rgd->rd_data) {
  926. down_write(&sdp->sd_log_flush_lock);
  927. block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED, &bi);
  928. up_write(&sdp->sd_log_flush_lock);
  929. if (block == BFITNOENT)
  930. break;
  931. block = gfs2_bi2rgd_blk(bi, block);
  932. /* rgblk_search can return a block < goal, so we need to
  933. keep it marching forward. */
  934. no_addr = block + rgd->rd_data0;
  935. goal = max(block + 1, goal + 1);
  936. if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
  937. continue;
  938. if (no_addr == skip)
  939. continue;
  940. *last_unlinked = no_addr;
  941. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &gl);
  942. if (error)
  943. continue;
  944. /* If the inode is already in cache, we can ignore it here
  945. * because the existing inode disposal code will deal with
  946. * it when all refs have gone away. Accessing gl_object like
  947. * this is not safe in general. Here it is ok because we do
  948. * not dereference the pointer, and we only need an approx
  949. * answer to whether it is NULL or not.
  950. */
  951. ip = gl->gl_object;
  952. if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
  953. gfs2_glock_put(gl);
  954. else
  955. found++;
  956. /* Limit reclaim to sensible number of tasks */
  957. if (found > NR_CPUS)
  958. return;
  959. }
  960. rgd->rd_flags &= ~GFS2_RDF_CHECK;
  961. return;
  962. }
  963. /**
  964. * get_local_rgrp - Choose and lock a rgrp for allocation
  965. * @ip: the inode to reserve space for
  966. * @last_unlinked: the last unlinked block
  967. *
  968. * Try to acquire rgrp in way which avoids contending with others.
  969. *
  970. * Returns: errno
  971. */
  972. static int get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
  973. {
  974. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  975. struct gfs2_rgrpd *rgd, *begin = NULL;
  976. struct gfs2_blkreserv *rs = ip->i_res;
  977. int error, rg_locked, flags = LM_FLAG_TRY;
  978. int loops = 0;
  979. if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, ip->i_goal))
  980. rgd = begin = ip->i_rgd;
  981. else
  982. rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal, 1);
  983. if (rgd == NULL)
  984. return -EBADSLT;
  985. while (loops < 3) {
  986. rg_locked = 0;
  987. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  988. rg_locked = 1;
  989. error = 0;
  990. } else {
  991. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
  992. flags, &rs->rs_rgd_gh);
  993. }
  994. switch (error) {
  995. case 0:
  996. if (try_rgrp_fit(rgd, ip)) {
  997. ip->i_rgd = rgd;
  998. return 0;
  999. }
  1000. if (rgd->rd_flags & GFS2_RDF_CHECK)
  1001. try_rgrp_unlink(rgd, last_unlinked, ip->i_no_addr);
  1002. if (!rg_locked)
  1003. gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
  1004. /* fall through */
  1005. case GLR_TRYFAILED:
  1006. rgd = gfs2_rgrpd_get_next(rgd);
  1007. if (rgd == begin) {
  1008. flags = 0;
  1009. loops++;
  1010. }
  1011. break;
  1012. default:
  1013. return error;
  1014. }
  1015. }
  1016. return -ENOSPC;
  1017. }
  1018. static void gfs2_blkrsv_put(struct gfs2_inode *ip)
  1019. {
  1020. BUG_ON(ip->i_res == NULL);
  1021. kmem_cache_free(gfs2_rsrv_cachep, ip->i_res);
  1022. ip->i_res = NULL;
  1023. }
  1024. /**
  1025. * gfs2_inplace_reserve - Reserve space in the filesystem
  1026. * @ip: the inode to reserve space for
  1027. * @requested: the number of blocks to be reserved
  1028. *
  1029. * Returns: errno
  1030. */
  1031. int gfs2_inplace_reserve(struct gfs2_inode *ip, u32 requested)
  1032. {
  1033. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1034. struct gfs2_blkreserv *rs;
  1035. int error;
  1036. u64 last_unlinked = NO_BLOCK;
  1037. int tries = 0;
  1038. error = gfs2_blkrsv_get(ip);
  1039. if (error)
  1040. return error;
  1041. rs = ip->i_res;
  1042. rs->rs_requested = requested;
  1043. if (gfs2_assert_warn(sdp, requested)) {
  1044. error = -EINVAL;
  1045. goto out;
  1046. }
  1047. do {
  1048. error = get_local_rgrp(ip, &last_unlinked);
  1049. if (error != -ENOSPC)
  1050. break;
  1051. /* Check that fs hasn't grown if writing to rindex */
  1052. if (ip == GFS2_I(sdp->sd_rindex) && !sdp->sd_rindex_uptodate) {
  1053. error = gfs2_ri_update(ip);
  1054. if (error)
  1055. break;
  1056. continue;
  1057. }
  1058. /* Flushing the log may release space */
  1059. gfs2_log_flush(sdp, NULL);
  1060. } while (tries++ < 3);
  1061. out:
  1062. if (error)
  1063. gfs2_blkrsv_put(ip);
  1064. return error;
  1065. }
  1066. /**
  1067. * gfs2_inplace_release - release an inplace reservation
  1068. * @ip: the inode the reservation was taken out on
  1069. *
  1070. * Release a reservation made by gfs2_inplace_reserve().
  1071. */
  1072. void gfs2_inplace_release(struct gfs2_inode *ip)
  1073. {
  1074. struct gfs2_blkreserv *rs = ip->i_res;
  1075. if (rs->rs_rgd_gh.gh_gl)
  1076. gfs2_glock_dq_uninit(&rs->rs_rgd_gh);
  1077. gfs2_blkrsv_put(ip);
  1078. }
  1079. /**
  1080. * gfs2_get_block_type - Check a block in a RG is of given type
  1081. * @rgd: the resource group holding the block
  1082. * @block: the block number
  1083. *
  1084. * Returns: The block type (GFS2_BLKST_*)
  1085. */
  1086. static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
  1087. {
  1088. struct gfs2_bitmap *bi = NULL;
  1089. u32 length, rgrp_block, buf_block;
  1090. unsigned int buf;
  1091. unsigned char type;
  1092. length = rgd->rd_length;
  1093. rgrp_block = block - rgd->rd_data0;
  1094. for (buf = 0; buf < length; buf++) {
  1095. bi = rgd->rd_bits + buf;
  1096. if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1097. break;
  1098. }
  1099. gfs2_assert(rgd->rd_sbd, buf < length);
  1100. buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
  1101. type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
  1102. bi->bi_len, buf_block);
  1103. return type;
  1104. }
  1105. /**
  1106. * rgblk_search - find a block in @state
  1107. * @rgd: the resource group descriptor
  1108. * @goal: the goal block within the RG (start here to search for avail block)
  1109. * @state: GFS2_BLKST_XXX the before-allocation state to find
  1110. * @rbi: address of the pointer to the bitmap containing the block found
  1111. *
  1112. * Walk rgrp's bitmap to find bits that represent a block in @state.
  1113. *
  1114. * This function never fails, because we wouldn't call it unless we
  1115. * know (from reservation results, etc.) that a block is available.
  1116. *
  1117. * Scope of @goal is just within rgrp, not the whole filesystem.
  1118. * Scope of @returned block is just within bitmap, not the whole filesystem.
  1119. *
  1120. * Returns: the block number found relative to the bitmap rbi
  1121. */
  1122. static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal, unsigned char state,
  1123. struct gfs2_bitmap **rbi)
  1124. {
  1125. struct gfs2_bitmap *bi = NULL;
  1126. const u32 length = rgd->rd_length;
  1127. u32 biblk = BFITNOENT;
  1128. unsigned int buf, x;
  1129. const u8 *buffer = NULL;
  1130. *rbi = NULL;
  1131. /* Find bitmap block that contains bits for goal block */
  1132. for (buf = 0; buf < length; buf++) {
  1133. bi = rgd->rd_bits + buf;
  1134. /* Convert scope of "goal" from rgrp-wide to within found bit block */
  1135. if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
  1136. goal -= bi->bi_start * GFS2_NBBY;
  1137. goto do_search;
  1138. }
  1139. }
  1140. buf = 0;
  1141. goal = 0;
  1142. do_search:
  1143. /* Search (up to entire) bitmap in this rgrp for allocatable block.
  1144. "x <= length", instead of "x < length", because we typically start
  1145. the search in the middle of a bit block, but if we can't find an
  1146. allocatable block anywhere else, we want to be able wrap around and
  1147. search in the first part of our first-searched bit block. */
  1148. for (x = 0; x <= length; x++) {
  1149. bi = rgd->rd_bits + buf;
  1150. if (test_bit(GBF_FULL, &bi->bi_flags) &&
  1151. (state == GFS2_BLKST_FREE))
  1152. goto skip;
  1153. /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
  1154. bitmaps, so we must search the originals for that. */
  1155. buffer = bi->bi_bh->b_data + bi->bi_offset;
  1156. WARN_ON(!buffer_uptodate(bi->bi_bh));
  1157. if (state != GFS2_BLKST_UNLINKED && bi->bi_clone)
  1158. buffer = bi->bi_clone + bi->bi_offset;
  1159. biblk = gfs2_bitfit(buffer, bi->bi_len, goal, state);
  1160. if (biblk != BFITNOENT)
  1161. break;
  1162. if ((goal == 0) && (state == GFS2_BLKST_FREE))
  1163. set_bit(GBF_FULL, &bi->bi_flags);
  1164. /* Try next bitmap block (wrap back to rgrp header if at end) */
  1165. skip:
  1166. buf++;
  1167. buf %= length;
  1168. goal = 0;
  1169. }
  1170. if (biblk != BFITNOENT)
  1171. *rbi = bi;
  1172. return biblk;
  1173. }
  1174. /**
  1175. * gfs2_alloc_extent - allocate an extent from a given bitmap
  1176. * @rgd: the resource group descriptor
  1177. * @bi: the bitmap within the rgrp
  1178. * @blk: the block within the bitmap
  1179. * @dinode: TRUE if the first block we allocate is for a dinode
  1180. * @n: The extent length
  1181. *
  1182. * Add the found bitmap buffer to the transaction.
  1183. * Set the found bits to @new_state to change block's allocation state.
  1184. * Returns: starting block number of the extent (fs scope)
  1185. */
  1186. static u64 gfs2_alloc_extent(struct gfs2_rgrpd *rgd, struct gfs2_bitmap *bi,
  1187. u32 blk, bool dinode, unsigned int *n)
  1188. {
  1189. const unsigned int elen = *n;
  1190. u32 goal;
  1191. const u8 *buffer = NULL;
  1192. *n = 0;
  1193. buffer = bi->bi_bh->b_data + bi->bi_offset;
  1194. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1195. gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone,
  1196. bi, blk, dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
  1197. (*n)++;
  1198. goal = blk;
  1199. while (*n < elen) {
  1200. goal++;
  1201. if (goal >= (bi->bi_len * GFS2_NBBY))
  1202. break;
  1203. if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
  1204. GFS2_BLKST_FREE)
  1205. break;
  1206. gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone,
  1207. bi, goal, GFS2_BLKST_USED);
  1208. (*n)++;
  1209. }
  1210. blk = gfs2_bi2rgd_blk(bi, blk);
  1211. rgd->rd_last_alloc = blk + *n - 1;
  1212. return rgd->rd_data0 + blk;
  1213. }
  1214. /**
  1215. * rgblk_free - Change alloc state of given block(s)
  1216. * @sdp: the filesystem
  1217. * @bstart: the start of a run of blocks to free
  1218. * @blen: the length of the block run (all must lie within ONE RG!)
  1219. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1220. *
  1221. * Returns: Resource group containing the block(s)
  1222. */
  1223. static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
  1224. u32 blen, unsigned char new_state)
  1225. {
  1226. struct gfs2_rgrpd *rgd;
  1227. struct gfs2_bitmap *bi = NULL;
  1228. u32 length, rgrp_blk, buf_blk;
  1229. unsigned int buf;
  1230. rgd = gfs2_blk2rgrpd(sdp, bstart, 1);
  1231. if (!rgd) {
  1232. if (gfs2_consist(sdp))
  1233. fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
  1234. return NULL;
  1235. }
  1236. length = rgd->rd_length;
  1237. rgrp_blk = bstart - rgd->rd_data0;
  1238. while (blen--) {
  1239. for (buf = 0; buf < length; buf++) {
  1240. bi = rgd->rd_bits + buf;
  1241. if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1242. break;
  1243. }
  1244. gfs2_assert(rgd->rd_sbd, buf < length);
  1245. buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
  1246. rgrp_blk++;
  1247. if (!bi->bi_clone) {
  1248. bi->bi_clone = kmalloc(bi->bi_bh->b_size,
  1249. GFP_NOFS | __GFP_NOFAIL);
  1250. memcpy(bi->bi_clone + bi->bi_offset,
  1251. bi->bi_bh->b_data + bi->bi_offset,
  1252. bi->bi_len);
  1253. }
  1254. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1255. gfs2_setbit(rgd, bi->bi_bh->b_data, NULL,
  1256. bi, buf_blk, new_state);
  1257. }
  1258. return rgd;
  1259. }
  1260. /**
  1261. * gfs2_rgrp_dump - print out an rgrp
  1262. * @seq: The iterator
  1263. * @gl: The glock in question
  1264. *
  1265. */
  1266. int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl)
  1267. {
  1268. const struct gfs2_rgrpd *rgd = gl->gl_object;
  1269. if (rgd == NULL)
  1270. return 0;
  1271. gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u\n",
  1272. (unsigned long long)rgd->rd_addr, rgd->rd_flags,
  1273. rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes);
  1274. return 0;
  1275. }
  1276. static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
  1277. {
  1278. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1279. fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
  1280. (unsigned long long)rgd->rd_addr);
  1281. fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
  1282. gfs2_rgrp_dump(NULL, rgd->rd_gl);
  1283. rgd->rd_flags |= GFS2_RDF_ERROR;
  1284. }
  1285. /**
  1286. * gfs2_alloc_blocks - Allocate one or more blocks of data and/or a dinode
  1287. * @ip: the inode to allocate the block for
  1288. * @bn: Used to return the starting block number
  1289. * @ndata: requested number of blocks/extent length (value/result)
  1290. * @dinode: 1 if we're allocating a dinode block, else 0
  1291. * @generation: the generation number of the inode
  1292. *
  1293. * Returns: 0 or error
  1294. */
  1295. int gfs2_alloc_blocks(struct gfs2_inode *ip, u64 *bn, unsigned int *nblocks,
  1296. bool dinode, u64 *generation)
  1297. {
  1298. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1299. struct buffer_head *dibh;
  1300. struct gfs2_rgrpd *rgd;
  1301. unsigned int ndata;
  1302. u32 goal, blk; /* block, within the rgrp scope */
  1303. u64 block; /* block, within the file system scope */
  1304. int error;
  1305. struct gfs2_bitmap *bi;
  1306. /* Only happens if there is a bug in gfs2, return something distinctive
  1307. * to ensure that it is noticed.
  1308. */
  1309. if (ip->i_res == NULL)
  1310. return -ECANCELED;
  1311. rgd = ip->i_rgd;
  1312. if (!dinode && rgrp_contains_block(rgd, ip->i_goal))
  1313. goal = ip->i_goal - rgd->rd_data0;
  1314. else
  1315. goal = rgd->rd_last_alloc;
  1316. blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, &bi);
  1317. /* Since all blocks are reserved in advance, this shouldn't happen */
  1318. if (blk == BFITNOENT)
  1319. goto rgrp_error;
  1320. block = gfs2_alloc_extent(rgd, bi, blk, dinode, nblocks);
  1321. ndata = *nblocks;
  1322. if (dinode)
  1323. ndata--;
  1324. if (!dinode) {
  1325. ip->i_goal = block + ndata - 1;
  1326. error = gfs2_meta_inode_buffer(ip, &dibh);
  1327. if (error == 0) {
  1328. struct gfs2_dinode *di =
  1329. (struct gfs2_dinode *)dibh->b_data;
  1330. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1331. di->di_goal_meta = di->di_goal_data =
  1332. cpu_to_be64(ip->i_goal);
  1333. brelse(dibh);
  1334. }
  1335. }
  1336. if (rgd->rd_free < *nblocks)
  1337. goto rgrp_error;
  1338. rgd->rd_free -= *nblocks;
  1339. if (dinode) {
  1340. rgd->rd_dinodes++;
  1341. *generation = rgd->rd_igeneration++;
  1342. if (*generation == 0)
  1343. *generation = rgd->rd_igeneration++;
  1344. }
  1345. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1346. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1347. gfs2_statfs_change(sdp, 0, -(s64)*nblocks, dinode ? 1 : 0);
  1348. if (dinode)
  1349. gfs2_trans_add_unrevoke(sdp, block, 1);
  1350. /*
  1351. * This needs reviewing to see why we cannot do the quota change
  1352. * at this point in the dinode case.
  1353. */
  1354. if (ndata)
  1355. gfs2_quota_change(ip, ndata, ip->i_inode.i_uid,
  1356. ip->i_inode.i_gid);
  1357. rgd->rd_free_clone -= *nblocks;
  1358. trace_gfs2_block_alloc(ip, block, *nblocks,
  1359. dinode ? GFS2_BLKST_DINODE : GFS2_BLKST_USED);
  1360. *bn = block;
  1361. return 0;
  1362. rgrp_error:
  1363. gfs2_rgrp_error(rgd);
  1364. return -EIO;
  1365. }
  1366. /**
  1367. * __gfs2_free_blocks - free a contiguous run of block(s)
  1368. * @ip: the inode these blocks are being freed from
  1369. * @bstart: first block of a run of contiguous blocks
  1370. * @blen: the length of the block run
  1371. * @meta: 1 if the blocks represent metadata
  1372. *
  1373. */
  1374. void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta)
  1375. {
  1376. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1377. struct gfs2_rgrpd *rgd;
  1378. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1379. if (!rgd)
  1380. return;
  1381. trace_gfs2_block_alloc(ip, bstart, blen, GFS2_BLKST_FREE);
  1382. rgd->rd_free += blen;
  1383. rgd->rd_flags &= ~GFS2_RGF_TRIMMED;
  1384. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1385. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1386. /* Directories keep their data in the metadata address space */
  1387. if (meta || ip->i_depth)
  1388. gfs2_meta_wipe(ip, bstart, blen);
  1389. }
  1390. /**
  1391. * gfs2_free_meta - free a contiguous run of data block(s)
  1392. * @ip: the inode these blocks are being freed from
  1393. * @bstart: first block of a run of contiguous blocks
  1394. * @blen: the length of the block run
  1395. *
  1396. */
  1397. void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
  1398. {
  1399. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1400. __gfs2_free_blocks(ip, bstart, blen, 1);
  1401. gfs2_statfs_change(sdp, 0, +blen, 0);
  1402. gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1403. }
  1404. void gfs2_unlink_di(struct inode *inode)
  1405. {
  1406. struct gfs2_inode *ip = GFS2_I(inode);
  1407. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1408. struct gfs2_rgrpd *rgd;
  1409. u64 blkno = ip->i_no_addr;
  1410. rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
  1411. if (!rgd)
  1412. return;
  1413. trace_gfs2_block_alloc(ip, blkno, 1, GFS2_BLKST_UNLINKED);
  1414. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1415. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1416. }
  1417. static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
  1418. {
  1419. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1420. struct gfs2_rgrpd *tmp_rgd;
  1421. tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
  1422. if (!tmp_rgd)
  1423. return;
  1424. gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
  1425. if (!rgd->rd_dinodes)
  1426. gfs2_consist_rgrpd(rgd);
  1427. rgd->rd_dinodes--;
  1428. rgd->rd_free++;
  1429. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1430. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1431. gfs2_statfs_change(sdp, 0, +1, -1);
  1432. }
  1433. void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
  1434. {
  1435. gfs2_free_uninit_di(rgd, ip->i_no_addr);
  1436. trace_gfs2_block_alloc(ip, ip->i_no_addr, 1, GFS2_BLKST_FREE);
  1437. gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1438. gfs2_meta_wipe(ip, ip->i_no_addr, 1);
  1439. }
  1440. /**
  1441. * gfs2_check_blk_type - Check the type of a block
  1442. * @sdp: The superblock
  1443. * @no_addr: The block number to check
  1444. * @type: The block type we are looking for
  1445. *
  1446. * Returns: 0 if the block type matches the expected type
  1447. * -ESTALE if it doesn't match
  1448. * or -ve errno if something went wrong while checking
  1449. */
  1450. int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
  1451. {
  1452. struct gfs2_rgrpd *rgd;
  1453. struct gfs2_holder rgd_gh;
  1454. int error = -EINVAL;
  1455. rgd = gfs2_blk2rgrpd(sdp, no_addr, 1);
  1456. if (!rgd)
  1457. goto fail;
  1458. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
  1459. if (error)
  1460. goto fail;
  1461. if (gfs2_get_block_type(rgd, no_addr) != type)
  1462. error = -ESTALE;
  1463. gfs2_glock_dq_uninit(&rgd_gh);
  1464. fail:
  1465. return error;
  1466. }
  1467. /**
  1468. * gfs2_rlist_add - add a RG to a list of RGs
  1469. * @ip: the inode
  1470. * @rlist: the list of resource groups
  1471. * @block: the block
  1472. *
  1473. * Figure out what RG a block belongs to and add that RG to the list
  1474. *
  1475. * FIXME: Don't use NOFAIL
  1476. *
  1477. */
  1478. void gfs2_rlist_add(struct gfs2_inode *ip, struct gfs2_rgrp_list *rlist,
  1479. u64 block)
  1480. {
  1481. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1482. struct gfs2_rgrpd *rgd;
  1483. struct gfs2_rgrpd **tmp;
  1484. unsigned int new_space;
  1485. unsigned int x;
  1486. if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
  1487. return;
  1488. if (ip->i_rgd && rgrp_contains_block(ip->i_rgd, block))
  1489. rgd = ip->i_rgd;
  1490. else
  1491. rgd = gfs2_blk2rgrpd(sdp, block, 1);
  1492. if (!rgd) {
  1493. fs_err(sdp, "rlist_add: no rgrp for block %llu\n", (unsigned long long)block);
  1494. return;
  1495. }
  1496. ip->i_rgd = rgd;
  1497. for (x = 0; x < rlist->rl_rgrps; x++)
  1498. if (rlist->rl_rgd[x] == rgd)
  1499. return;
  1500. if (rlist->rl_rgrps == rlist->rl_space) {
  1501. new_space = rlist->rl_space + 10;
  1502. tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
  1503. GFP_NOFS | __GFP_NOFAIL);
  1504. if (rlist->rl_rgd) {
  1505. memcpy(tmp, rlist->rl_rgd,
  1506. rlist->rl_space * sizeof(struct gfs2_rgrpd *));
  1507. kfree(rlist->rl_rgd);
  1508. }
  1509. rlist->rl_space = new_space;
  1510. rlist->rl_rgd = tmp;
  1511. }
  1512. rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
  1513. }
  1514. /**
  1515. * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
  1516. * and initialize an array of glock holders for them
  1517. * @rlist: the list of resource groups
  1518. * @state: the lock state to acquire the RG lock in
  1519. *
  1520. * FIXME: Don't use NOFAIL
  1521. *
  1522. */
  1523. void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
  1524. {
  1525. unsigned int x;
  1526. rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
  1527. GFP_NOFS | __GFP_NOFAIL);
  1528. for (x = 0; x < rlist->rl_rgrps; x++)
  1529. gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
  1530. state, 0,
  1531. &rlist->rl_ghs[x]);
  1532. }
  1533. /**
  1534. * gfs2_rlist_free - free a resource group list
  1535. * @list: the list of resource groups
  1536. *
  1537. */
  1538. void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
  1539. {
  1540. unsigned int x;
  1541. kfree(rlist->rl_rgd);
  1542. if (rlist->rl_ghs) {
  1543. for (x = 0; x < rlist->rl_rgrps; x++)
  1544. gfs2_holder_uninit(&rlist->rl_ghs[x]);
  1545. kfree(rlist->rl_ghs);
  1546. }
  1547. }