rgrp.c 59 KB

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