rgrp.c 63 KB

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