rgrp.c 61 KB

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