rgrp.c 44 KB

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