rgrp.c 45 KB

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