rgrp.c 60 KB

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