rgrp.c 59 KB

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