rgrp.c 60 KB

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