rgrp.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  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 "gfs2.h"
  18. #include "incore.h"
  19. #include "glock.h"
  20. #include "glops.h"
  21. #include "lops.h"
  22. #include "meta_io.h"
  23. #include "quota.h"
  24. #include "rgrp.h"
  25. #include "super.h"
  26. #include "trans.h"
  27. #include "util.h"
  28. #include "log.h"
  29. #include "inode.h"
  30. #include "trace_gfs2.h"
  31. #define BFITNOENT ((u32)~0)
  32. #define NO_BLOCK ((u64)~0)
  33. #if BITS_PER_LONG == 32
  34. #define LBITMASK (0x55555555UL)
  35. #define LBITSKIP55 (0x55555555UL)
  36. #define LBITSKIP00 (0x00000000UL)
  37. #else
  38. #define LBITMASK (0x5555555555555555UL)
  39. #define LBITSKIP55 (0x5555555555555555UL)
  40. #define LBITSKIP00 (0x0000000000000000UL)
  41. #endif
  42. /*
  43. * These routines are used by the resource group routines (rgrp.c)
  44. * to keep track of block allocation. Each block is represented by two
  45. * bits. So, each byte represents GFS2_NBBY (i.e. 4) blocks.
  46. *
  47. * 0 = Free
  48. * 1 = Used (not metadata)
  49. * 2 = Unlinked (still in use) inode
  50. * 3 = Used (metadata)
  51. */
  52. static const char valid_change[16] = {
  53. /* current */
  54. /* n */ 0, 1, 1, 1,
  55. /* e */ 1, 0, 0, 0,
  56. /* w */ 0, 0, 0, 1,
  57. 1, 0, 0, 0
  58. };
  59. static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
  60. unsigned char old_state, unsigned char new_state,
  61. unsigned int *n);
  62. /**
  63. * gfs2_setbit - Set a bit in the bitmaps
  64. * @buffer: the buffer that holds the bitmaps
  65. * @buflen: the length (in bytes) of the buffer
  66. * @block: the block to set
  67. * @new_state: the new state of the block
  68. *
  69. */
  70. static inline void gfs2_setbit(struct gfs2_rgrpd *rgd, unsigned char *buf1,
  71. unsigned char *buf2, unsigned int offset,
  72. struct gfs2_bitmap *bi, u32 block,
  73. unsigned char new_state)
  74. {
  75. unsigned char *byte1, *byte2, *end, cur_state;
  76. unsigned int buflen = bi->bi_len;
  77. const unsigned int bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
  78. byte1 = buf1 + offset + (block / GFS2_NBBY);
  79. end = buf1 + offset + buflen;
  80. BUG_ON(byte1 >= end);
  81. cur_state = (*byte1 >> bit) & GFS2_BIT_MASK;
  82. if (unlikely(!valid_change[new_state * 4 + cur_state])) {
  83. printk(KERN_WARNING "GFS2: buf_blk = 0x%llx old_state=%d, "
  84. "new_state=%d\n",
  85. (unsigned long long)block, cur_state, new_state);
  86. printk(KERN_WARNING "GFS2: rgrp=0x%llx bi_start=0x%lx\n",
  87. (unsigned long long)rgd->rd_addr,
  88. (unsigned long)bi->bi_start);
  89. printk(KERN_WARNING "GFS2: bi_offset=0x%lx bi_len=0x%lx\n",
  90. (unsigned long)bi->bi_offset,
  91. (unsigned long)bi->bi_len);
  92. dump_stack();
  93. gfs2_consist_rgrpd(rgd);
  94. return;
  95. }
  96. *byte1 ^= (cur_state ^ new_state) << bit;
  97. if (buf2) {
  98. byte2 = buf2 + offset + (block / GFS2_NBBY);
  99. cur_state = (*byte2 >> bit) & GFS2_BIT_MASK;
  100. *byte2 ^= (cur_state ^ new_state) << bit;
  101. }
  102. }
  103. /**
  104. * gfs2_testbit - test a bit in the bitmaps
  105. * @buffer: the buffer that holds the bitmaps
  106. * @buflen: the length (in bytes) of the buffer
  107. * @block: the block to read
  108. *
  109. */
  110. static inline unsigned char gfs2_testbit(struct gfs2_rgrpd *rgd,
  111. const unsigned char *buffer,
  112. unsigned int buflen, u32 block)
  113. {
  114. const unsigned char *byte, *end;
  115. unsigned char cur_state;
  116. unsigned int bit;
  117. byte = buffer + (block / GFS2_NBBY);
  118. bit = (block % GFS2_NBBY) * GFS2_BIT_SIZE;
  119. end = buffer + buflen;
  120. gfs2_assert(rgd->rd_sbd, byte < end);
  121. cur_state = (*byte >> bit) & GFS2_BIT_MASK;
  122. return cur_state;
  123. }
  124. /**
  125. * gfs2_bit_search
  126. * @ptr: Pointer to bitmap data
  127. * @mask: Mask to use (normally 0x55555.... but adjusted for search start)
  128. * @state: The state we are searching for
  129. *
  130. * We xor the bitmap data with a patter which is the bitwise opposite
  131. * of what we are looking for, this gives rise to a pattern of ones
  132. * wherever there is a match. Since we have two bits per entry, we
  133. * take this pattern, shift it down by one place and then and it with
  134. * the original. All the even bit positions (0,2,4, etc) then represent
  135. * successful matches, so we mask with 0x55555..... to remove the unwanted
  136. * odd bit positions.
  137. *
  138. * This allows searching of a whole u64 at once (32 blocks) with a
  139. * single test (on 64 bit arches).
  140. */
  141. static inline u64 gfs2_bit_search(const __le64 *ptr, u64 mask, u8 state)
  142. {
  143. u64 tmp;
  144. static const u64 search[] = {
  145. [0] = 0xffffffffffffffffULL,
  146. [1] = 0xaaaaaaaaaaaaaaaaULL,
  147. [2] = 0x5555555555555555ULL,
  148. [3] = 0x0000000000000000ULL,
  149. };
  150. tmp = le64_to_cpu(*ptr) ^ search[state];
  151. tmp &= (tmp >> 1);
  152. tmp &= mask;
  153. return tmp;
  154. }
  155. /**
  156. * gfs2_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
  157. * a block in a given allocation state.
  158. * @buffer: the buffer that holds the bitmaps
  159. * @len: the length (in bytes) of the buffer
  160. * @goal: start search at this block's bit-pair (within @buffer)
  161. * @state: GFS2_BLKST_XXX the state of the block we're looking for.
  162. *
  163. * Scope of @goal and returned block number is only within this bitmap buffer,
  164. * not entire rgrp or filesystem. @buffer will be offset from the actual
  165. * beginning of a bitmap block buffer, skipping any header structures, but
  166. * headers are always a multiple of 64 bits long so that the buffer is
  167. * always aligned to a 64 bit boundary.
  168. *
  169. * The size of the buffer is in bytes, but is it assumed that it is
  170. * always ok to read a complete multiple of 64 bits at the end
  171. * of the block in case the end is no aligned to a natural boundary.
  172. *
  173. * Return: the block number (bitmap buffer scope) that was found
  174. */
  175. static u32 gfs2_bitfit(const u8 *buf, const unsigned int len,
  176. u32 goal, u8 state)
  177. {
  178. u32 spoint = (goal << 1) & ((8*sizeof(u64)) - 1);
  179. const __le64 *ptr = ((__le64 *)buf) + (goal >> 5);
  180. const __le64 *end = (__le64 *)(buf + ALIGN(len, sizeof(u64)));
  181. u64 tmp;
  182. u64 mask = 0x5555555555555555ULL;
  183. u32 bit;
  184. BUG_ON(state > 3);
  185. /* Mask off bits we don't care about at the start of the search */
  186. mask <<= spoint;
  187. tmp = gfs2_bit_search(ptr, mask, state);
  188. ptr++;
  189. while(tmp == 0 && ptr < end) {
  190. tmp = gfs2_bit_search(ptr, 0x5555555555555555ULL, state);
  191. ptr++;
  192. }
  193. /* Mask off any bits which are more than len bytes from the start */
  194. if (ptr == end && (len & (sizeof(u64) - 1)))
  195. tmp &= (((u64)~0) >> (64 - 8*(len & (sizeof(u64) - 1))));
  196. /* Didn't find anything, so return */
  197. if (tmp == 0)
  198. return BFITNOENT;
  199. ptr--;
  200. bit = __ffs64(tmp);
  201. bit /= 2; /* two bits per entry in the bitmap */
  202. return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
  203. }
  204. /**
  205. * gfs2_bitcount - count the number of bits in a certain state
  206. * @buffer: the buffer that holds the bitmaps
  207. * @buflen: the length (in bytes) of the buffer
  208. * @state: the state of the block we're looking for
  209. *
  210. * Returns: The number of bits
  211. */
  212. static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
  213. unsigned int buflen, u8 state)
  214. {
  215. const u8 *byte = buffer;
  216. const u8 *end = buffer + buflen;
  217. const u8 state1 = state << 2;
  218. const u8 state2 = state << 4;
  219. const u8 state3 = state << 6;
  220. u32 count = 0;
  221. for (; byte < end; byte++) {
  222. if (((*byte) & 0x03) == state)
  223. count++;
  224. if (((*byte) & 0x0C) == state1)
  225. count++;
  226. if (((*byte) & 0x30) == state2)
  227. count++;
  228. if (((*byte) & 0xC0) == state3)
  229. count++;
  230. }
  231. return count;
  232. }
  233. /**
  234. * gfs2_rgrp_verify - Verify that a resource group is consistent
  235. * @sdp: the filesystem
  236. * @rgd: the rgrp
  237. *
  238. */
  239. void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
  240. {
  241. struct gfs2_sbd *sdp = rgd->rd_sbd;
  242. struct gfs2_bitmap *bi = NULL;
  243. u32 length = rgd->rd_length;
  244. u32 count[4], tmp;
  245. int buf, x;
  246. memset(count, 0, 4 * sizeof(u32));
  247. /* Count # blocks in each of 4 possible allocation states */
  248. for (buf = 0; buf < length; buf++) {
  249. bi = rgd->rd_bits + buf;
  250. for (x = 0; x < 4; x++)
  251. count[x] += gfs2_bitcount(rgd,
  252. bi->bi_bh->b_data +
  253. bi->bi_offset,
  254. bi->bi_len, x);
  255. }
  256. if (count[0] != rgd->rd_free) {
  257. if (gfs2_consist_rgrpd(rgd))
  258. fs_err(sdp, "free data mismatch: %u != %u\n",
  259. count[0], rgd->rd_free);
  260. return;
  261. }
  262. tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
  263. if (count[1] != tmp) {
  264. if (gfs2_consist_rgrpd(rgd))
  265. fs_err(sdp, "used data mismatch: %u != %u\n",
  266. count[1], tmp);
  267. return;
  268. }
  269. if (count[2] + count[3] != rgd->rd_dinodes) {
  270. if (gfs2_consist_rgrpd(rgd))
  271. fs_err(sdp, "used metadata mismatch: %u != %u\n",
  272. count[2] + count[3], rgd->rd_dinodes);
  273. return;
  274. }
  275. }
  276. static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
  277. {
  278. u64 first = rgd->rd_data0;
  279. u64 last = first + rgd->rd_data;
  280. return first <= block && block < last;
  281. }
  282. /**
  283. * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
  284. * @sdp: The GFS2 superblock
  285. * @n: The data block number
  286. *
  287. * Returns: The resource group, or NULL if not found
  288. */
  289. struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
  290. {
  291. struct gfs2_rgrpd *rgd;
  292. spin_lock(&sdp->sd_rindex_spin);
  293. list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
  294. if (rgrp_contains_block(rgd, blk)) {
  295. list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
  296. spin_unlock(&sdp->sd_rindex_spin);
  297. return rgd;
  298. }
  299. }
  300. spin_unlock(&sdp->sd_rindex_spin);
  301. return NULL;
  302. }
  303. /**
  304. * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
  305. * @sdp: The GFS2 superblock
  306. *
  307. * Returns: The first rgrp in the filesystem
  308. */
  309. struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
  310. {
  311. gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
  312. return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
  313. }
  314. /**
  315. * gfs2_rgrpd_get_next - get the next RG
  316. * @rgd: A RG
  317. *
  318. * Returns: The next rgrp
  319. */
  320. struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
  321. {
  322. if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
  323. return NULL;
  324. return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
  325. }
  326. static void clear_rgrpdi(struct gfs2_sbd *sdp)
  327. {
  328. struct list_head *head;
  329. struct gfs2_rgrpd *rgd;
  330. struct gfs2_glock *gl;
  331. spin_lock(&sdp->sd_rindex_spin);
  332. sdp->sd_rindex_forward = NULL;
  333. spin_unlock(&sdp->sd_rindex_spin);
  334. head = &sdp->sd_rindex_list;
  335. while (!list_empty(head)) {
  336. rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
  337. gl = rgd->rd_gl;
  338. list_del(&rgd->rd_list);
  339. list_del(&rgd->rd_list_mru);
  340. if (gl) {
  341. gl->gl_object = NULL;
  342. gfs2_glock_add_to_lru(gl);
  343. gfs2_glock_put(gl);
  344. }
  345. kfree(rgd->rd_bits);
  346. kmem_cache_free(gfs2_rgrpd_cachep, rgd);
  347. }
  348. }
  349. void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
  350. {
  351. mutex_lock(&sdp->sd_rindex_mutex);
  352. clear_rgrpdi(sdp);
  353. mutex_unlock(&sdp->sd_rindex_mutex);
  354. }
  355. static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
  356. {
  357. printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
  358. printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
  359. printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
  360. printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
  361. printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
  362. }
  363. /**
  364. * gfs2_compute_bitstructs - Compute the bitmap sizes
  365. * @rgd: The resource group descriptor
  366. *
  367. * Calculates bitmap descriptors, one for each block that contains bitmap data
  368. *
  369. * Returns: errno
  370. */
  371. static int compute_bitstructs(struct gfs2_rgrpd *rgd)
  372. {
  373. struct gfs2_sbd *sdp = rgd->rd_sbd;
  374. struct gfs2_bitmap *bi;
  375. u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
  376. u32 bytes_left, bytes;
  377. int x;
  378. if (!length)
  379. return -EINVAL;
  380. rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
  381. if (!rgd->rd_bits)
  382. return -ENOMEM;
  383. bytes_left = rgd->rd_bitbytes;
  384. for (x = 0; x < length; x++) {
  385. bi = rgd->rd_bits + x;
  386. bi->bi_flags = 0;
  387. /* small rgrp; bitmap stored completely in header block */
  388. if (length == 1) {
  389. bytes = bytes_left;
  390. bi->bi_offset = sizeof(struct gfs2_rgrp);
  391. bi->bi_start = 0;
  392. bi->bi_len = bytes;
  393. /* header block */
  394. } else if (x == 0) {
  395. bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
  396. bi->bi_offset = sizeof(struct gfs2_rgrp);
  397. bi->bi_start = 0;
  398. bi->bi_len = bytes;
  399. /* last block */
  400. } else if (x + 1 == length) {
  401. bytes = bytes_left;
  402. bi->bi_offset = sizeof(struct gfs2_meta_header);
  403. bi->bi_start = rgd->rd_bitbytes - bytes_left;
  404. bi->bi_len = bytes;
  405. /* other blocks */
  406. } else {
  407. bytes = sdp->sd_sb.sb_bsize -
  408. sizeof(struct gfs2_meta_header);
  409. bi->bi_offset = sizeof(struct gfs2_meta_header);
  410. bi->bi_start = rgd->rd_bitbytes - bytes_left;
  411. bi->bi_len = bytes;
  412. }
  413. bytes_left -= bytes;
  414. }
  415. if (bytes_left) {
  416. gfs2_consist_rgrpd(rgd);
  417. return -EIO;
  418. }
  419. bi = rgd->rd_bits + (length - 1);
  420. if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
  421. if (gfs2_consist_rgrpd(rgd)) {
  422. gfs2_rindex_print(rgd);
  423. fs_err(sdp, "start=%u len=%u offset=%u\n",
  424. bi->bi_start, bi->bi_len, bi->bi_offset);
  425. }
  426. return -EIO;
  427. }
  428. return 0;
  429. }
  430. /**
  431. * gfs2_ri_total - Total up the file system space, according to the rindex.
  432. *
  433. */
  434. u64 gfs2_ri_total(struct gfs2_sbd *sdp)
  435. {
  436. u64 total_data = 0;
  437. struct inode *inode = sdp->sd_rindex;
  438. struct gfs2_inode *ip = GFS2_I(inode);
  439. char buf[sizeof(struct gfs2_rindex)];
  440. struct file_ra_state ra_state;
  441. int error, rgrps;
  442. mutex_lock(&sdp->sd_rindex_mutex);
  443. file_ra_state_init(&ra_state, inode->i_mapping);
  444. for (rgrps = 0;; rgrps++) {
  445. loff_t pos = rgrps * sizeof(struct gfs2_rindex);
  446. if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
  447. break;
  448. error = gfs2_internal_read(ip, &ra_state, buf, &pos,
  449. sizeof(struct gfs2_rindex));
  450. if (error != sizeof(struct gfs2_rindex))
  451. break;
  452. total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
  453. }
  454. mutex_unlock(&sdp->sd_rindex_mutex);
  455. return total_data;
  456. }
  457. static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
  458. {
  459. const struct gfs2_rindex *str = buf;
  460. rgd->rd_addr = be64_to_cpu(str->ri_addr);
  461. rgd->rd_length = be32_to_cpu(str->ri_length);
  462. rgd->rd_data0 = be64_to_cpu(str->ri_data0);
  463. rgd->rd_data = be32_to_cpu(str->ri_data);
  464. rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
  465. }
  466. /**
  467. * read_rindex_entry - Pull in a new resource index entry from the disk
  468. * @gl: The glock covering the rindex inode
  469. *
  470. * Returns: 0 on success, error code otherwise
  471. */
  472. static int read_rindex_entry(struct gfs2_inode *ip,
  473. struct file_ra_state *ra_state)
  474. {
  475. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  476. loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
  477. char buf[sizeof(struct gfs2_rindex)];
  478. int error;
  479. struct gfs2_rgrpd *rgd;
  480. error = gfs2_internal_read(ip, ra_state, buf, &pos,
  481. sizeof(struct gfs2_rindex));
  482. if (!error)
  483. return 0;
  484. if (error != sizeof(struct gfs2_rindex)) {
  485. if (error > 0)
  486. error = -EIO;
  487. return error;
  488. }
  489. rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
  490. error = -ENOMEM;
  491. if (!rgd)
  492. return error;
  493. mutex_init(&rgd->rd_mutex);
  494. lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
  495. rgd->rd_sbd = sdp;
  496. list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
  497. list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
  498. gfs2_rindex_in(rgd, buf);
  499. error = compute_bitstructs(rgd);
  500. if (error)
  501. return error;
  502. error = gfs2_glock_get(sdp, rgd->rd_addr,
  503. &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
  504. if (error)
  505. return error;
  506. rgd->rd_gl->gl_object = rgd;
  507. rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
  508. return error;
  509. }
  510. /**
  511. * gfs2_ri_update - Pull in a new resource index from the disk
  512. * @ip: pointer to the rindex inode
  513. *
  514. * Returns: 0 on successful update, error code otherwise
  515. */
  516. int gfs2_ri_update(struct gfs2_inode *ip)
  517. {
  518. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  519. struct inode *inode = &ip->i_inode;
  520. struct file_ra_state ra_state;
  521. u64 rgrp_count = i_size_read(inode);
  522. struct gfs2_rgrpd *rgd;
  523. unsigned int max_data = 0;
  524. int error;
  525. do_div(rgrp_count, sizeof(struct gfs2_rindex));
  526. clear_rgrpdi(sdp);
  527. file_ra_state_init(&ra_state, inode->i_mapping);
  528. for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
  529. error = read_rindex_entry(ip, &ra_state);
  530. if (error) {
  531. clear_rgrpdi(sdp);
  532. return error;
  533. }
  534. }
  535. list_for_each_entry(rgd, &sdp->sd_rindex_list, rd_list)
  536. if (rgd->rd_data > max_data)
  537. max_data = rgd->rd_data;
  538. sdp->sd_max_rg_data = max_data;
  539. sdp->sd_rindex_uptodate = 1;
  540. return 0;
  541. }
  542. /**
  543. * gfs2_rindex_hold - Grab a lock on the rindex
  544. * @sdp: The GFS2 superblock
  545. * @ri_gh: the glock holder
  546. *
  547. * We grab a lock on the rindex inode to make sure that it doesn't
  548. * change whilst we are performing an operation. We keep this lock
  549. * for quite long periods of time compared to other locks. This
  550. * doesn't matter, since it is shared and it is very, very rarely
  551. * accessed in the exclusive mode (i.e. only when expanding the filesystem).
  552. *
  553. * This makes sure that we're using the latest copy of the resource index
  554. * special file, which might have been updated if someone expanded the
  555. * filesystem (via gfs2_grow utility), which adds new resource groups.
  556. *
  557. * Returns: 0 on success, error code otherwise
  558. */
  559. int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
  560. {
  561. struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
  562. struct gfs2_glock *gl = ip->i_gl;
  563. int error;
  564. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
  565. if (error)
  566. return error;
  567. /* Read new copy from disk if we don't have the latest */
  568. if (!sdp->sd_rindex_uptodate) {
  569. mutex_lock(&sdp->sd_rindex_mutex);
  570. if (!sdp->sd_rindex_uptodate) {
  571. error = gfs2_ri_update(ip);
  572. if (error)
  573. gfs2_glock_dq_uninit(ri_gh);
  574. }
  575. mutex_unlock(&sdp->sd_rindex_mutex);
  576. }
  577. return error;
  578. }
  579. static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
  580. {
  581. const struct gfs2_rgrp *str = buf;
  582. u32 rg_flags;
  583. rg_flags = be32_to_cpu(str->rg_flags);
  584. rg_flags &= ~GFS2_RDF_MASK;
  585. rgd->rd_flags &= GFS2_RDF_MASK;
  586. rgd->rd_flags |= rg_flags;
  587. rgd->rd_free = be32_to_cpu(str->rg_free);
  588. rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
  589. rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
  590. }
  591. static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
  592. {
  593. struct gfs2_rgrp *str = buf;
  594. str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
  595. str->rg_free = cpu_to_be32(rgd->rd_free);
  596. str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
  597. str->__pad = cpu_to_be32(0);
  598. str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
  599. memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
  600. }
  601. /**
  602. * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
  603. * @rgd: the struct gfs2_rgrpd describing the RG to read in
  604. *
  605. * Read in all of a Resource Group's header and bitmap blocks.
  606. * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
  607. *
  608. * Returns: errno
  609. */
  610. int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
  611. {
  612. struct gfs2_sbd *sdp = rgd->rd_sbd;
  613. struct gfs2_glock *gl = rgd->rd_gl;
  614. unsigned int length = rgd->rd_length;
  615. struct gfs2_bitmap *bi;
  616. unsigned int x, y;
  617. int error;
  618. mutex_lock(&rgd->rd_mutex);
  619. spin_lock(&sdp->sd_rindex_spin);
  620. if (rgd->rd_bh_count) {
  621. rgd->rd_bh_count++;
  622. spin_unlock(&sdp->sd_rindex_spin);
  623. mutex_unlock(&rgd->rd_mutex);
  624. return 0;
  625. }
  626. spin_unlock(&sdp->sd_rindex_spin);
  627. for (x = 0; x < length; x++) {
  628. bi = rgd->rd_bits + x;
  629. error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
  630. if (error)
  631. goto fail;
  632. }
  633. for (y = length; y--;) {
  634. bi = rgd->rd_bits + y;
  635. error = gfs2_meta_wait(sdp, bi->bi_bh);
  636. if (error)
  637. goto fail;
  638. if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
  639. GFS2_METATYPE_RG)) {
  640. error = -EIO;
  641. goto fail;
  642. }
  643. }
  644. if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
  645. for (x = 0; x < length; x++)
  646. clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
  647. gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
  648. rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
  649. }
  650. spin_lock(&sdp->sd_rindex_spin);
  651. rgd->rd_free_clone = rgd->rd_free;
  652. rgd->rd_bh_count++;
  653. spin_unlock(&sdp->sd_rindex_spin);
  654. mutex_unlock(&rgd->rd_mutex);
  655. return 0;
  656. fail:
  657. while (x--) {
  658. bi = rgd->rd_bits + x;
  659. brelse(bi->bi_bh);
  660. bi->bi_bh = NULL;
  661. gfs2_assert_warn(sdp, !bi->bi_clone);
  662. }
  663. mutex_unlock(&rgd->rd_mutex);
  664. return error;
  665. }
  666. void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
  667. {
  668. struct gfs2_sbd *sdp = rgd->rd_sbd;
  669. spin_lock(&sdp->sd_rindex_spin);
  670. gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
  671. rgd->rd_bh_count++;
  672. spin_unlock(&sdp->sd_rindex_spin);
  673. }
  674. /**
  675. * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
  676. * @rgd: the struct gfs2_rgrpd describing the RG to read in
  677. *
  678. */
  679. void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
  680. {
  681. struct gfs2_sbd *sdp = rgd->rd_sbd;
  682. int x, length = rgd->rd_length;
  683. spin_lock(&sdp->sd_rindex_spin);
  684. gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
  685. if (--rgd->rd_bh_count) {
  686. spin_unlock(&sdp->sd_rindex_spin);
  687. return;
  688. }
  689. for (x = 0; x < length; x++) {
  690. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  691. kfree(bi->bi_clone);
  692. bi->bi_clone = NULL;
  693. brelse(bi->bi_bh);
  694. bi->bi_bh = NULL;
  695. }
  696. spin_unlock(&sdp->sd_rindex_spin);
  697. }
  698. static void gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
  699. const struct gfs2_bitmap *bi)
  700. {
  701. struct super_block *sb = sdp->sd_vfs;
  702. struct block_device *bdev = sb->s_bdev;
  703. const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize /
  704. bdev_logical_block_size(sb->s_bdev);
  705. u64 blk;
  706. sector_t start = 0;
  707. sector_t nr_sects = 0;
  708. int rv;
  709. unsigned int x;
  710. for (x = 0; x < bi->bi_len; x++) {
  711. const u8 *orig = bi->bi_bh->b_data + bi->bi_offset + x;
  712. const u8 *clone = bi->bi_clone + bi->bi_offset + x;
  713. u8 diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
  714. diff &= 0x55;
  715. if (diff == 0)
  716. continue;
  717. blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
  718. blk *= sects_per_blk; /* convert to sectors */
  719. while(diff) {
  720. if (diff & 1) {
  721. if (nr_sects == 0)
  722. goto start_new_extent;
  723. if ((start + nr_sects) != blk) {
  724. rv = blkdev_issue_discard(bdev, start,
  725. nr_sects, GFP_NOFS,
  726. 0);
  727. if (rv)
  728. goto fail;
  729. nr_sects = 0;
  730. start_new_extent:
  731. start = blk;
  732. }
  733. nr_sects += sects_per_blk;
  734. }
  735. diff >>= 2;
  736. blk += sects_per_blk;
  737. }
  738. }
  739. if (nr_sects) {
  740. rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 0);
  741. if (rv)
  742. goto fail;
  743. }
  744. return;
  745. fail:
  746. fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem", rv);
  747. sdp->sd_args.ar_discard = 0;
  748. }
  749. void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
  750. {
  751. struct gfs2_sbd *sdp = rgd->rd_sbd;
  752. unsigned int length = rgd->rd_length;
  753. unsigned int x;
  754. for (x = 0; x < length; x++) {
  755. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  756. if (!bi->bi_clone)
  757. continue;
  758. if (sdp->sd_args.ar_discard)
  759. gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bi);
  760. clear_bit(GBF_FULL, &bi->bi_flags);
  761. memcpy(bi->bi_clone + bi->bi_offset,
  762. bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
  763. }
  764. spin_lock(&sdp->sd_rindex_spin);
  765. rgd->rd_free_clone = rgd->rd_free;
  766. spin_unlock(&sdp->sd_rindex_spin);
  767. }
  768. /**
  769. * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
  770. * @ip: the incore GFS2 inode structure
  771. *
  772. * Returns: the struct gfs2_alloc
  773. */
  774. struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
  775. {
  776. BUG_ON(ip->i_alloc != NULL);
  777. ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_NOFS);
  778. return ip->i_alloc;
  779. }
  780. /**
  781. * try_rgrp_fit - See if a given reservation will fit in a given RG
  782. * @rgd: the RG data
  783. * @al: the struct gfs2_alloc structure describing the reservation
  784. *
  785. * If there's room for the requested blocks to be allocated from the RG:
  786. * Sets the $al_rgd field in @al.
  787. *
  788. * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
  789. */
  790. static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
  791. {
  792. struct gfs2_sbd *sdp = rgd->rd_sbd;
  793. int ret = 0;
  794. if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR))
  795. return 0;
  796. spin_lock(&sdp->sd_rindex_spin);
  797. if (rgd->rd_free_clone >= al->al_requested) {
  798. al->al_rgd = rgd;
  799. ret = 1;
  800. }
  801. spin_unlock(&sdp->sd_rindex_spin);
  802. return ret;
  803. }
  804. /**
  805. * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
  806. * @rgd: The rgrp
  807. *
  808. * Returns: 0 if no error
  809. * The inode, if one has been found, in inode.
  810. */
  811. static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
  812. {
  813. u32 goal = 0, block;
  814. u64 no_addr;
  815. struct gfs2_sbd *sdp = rgd->rd_sbd;
  816. unsigned int n;
  817. struct gfs2_glock *gl;
  818. struct gfs2_inode *ip;
  819. int error;
  820. int found = 0;
  821. while (goal < rgd->rd_data) {
  822. down_write(&sdp->sd_log_flush_lock);
  823. n = 1;
  824. block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
  825. GFS2_BLKST_UNLINKED, &n);
  826. up_write(&sdp->sd_log_flush_lock);
  827. if (block == BFITNOENT)
  828. break;
  829. /* rgblk_search can return a block < goal, so we need to
  830. keep it marching forward. */
  831. no_addr = block + rgd->rd_data0;
  832. goal = max(block + 1, goal + 1);
  833. if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
  834. continue;
  835. if (no_addr == skip)
  836. continue;
  837. *last_unlinked = no_addr;
  838. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &gl);
  839. if (error)
  840. continue;
  841. /* If the inode is already in cache, we can ignore it here
  842. * because the existing inode disposal code will deal with
  843. * it when all refs have gone away. Accessing gl_object like
  844. * this is not safe in general. Here it is ok because we do
  845. * not dereference the pointer, and we only need an approx
  846. * answer to whether it is NULL or not.
  847. */
  848. ip = gl->gl_object;
  849. if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
  850. gfs2_glock_put(gl);
  851. else
  852. found++;
  853. /* Limit reclaim to sensible number of tasks */
  854. if (found > NR_CPUS)
  855. return;
  856. }
  857. rgd->rd_flags &= ~GFS2_RDF_CHECK;
  858. return;
  859. }
  860. /**
  861. * recent_rgrp_next - get next RG from "recent" list
  862. * @cur_rgd: current rgrp
  863. *
  864. * Returns: The next rgrp in the recent list
  865. */
  866. static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd)
  867. {
  868. struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
  869. struct list_head *head;
  870. struct gfs2_rgrpd *rgd;
  871. spin_lock(&sdp->sd_rindex_spin);
  872. head = &sdp->sd_rindex_mru_list;
  873. if (unlikely(cur_rgd->rd_list_mru.next == head)) {
  874. spin_unlock(&sdp->sd_rindex_spin);
  875. return NULL;
  876. }
  877. rgd = list_entry(cur_rgd->rd_list_mru.next, struct gfs2_rgrpd, rd_list_mru);
  878. spin_unlock(&sdp->sd_rindex_spin);
  879. return rgd;
  880. }
  881. /**
  882. * forward_rgrp_get - get an rgrp to try next from full list
  883. * @sdp: The GFS2 superblock
  884. *
  885. * Returns: The rgrp to try next
  886. */
  887. static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
  888. {
  889. struct gfs2_rgrpd *rgd;
  890. unsigned int journals = gfs2_jindex_size(sdp);
  891. unsigned int rg = 0, x;
  892. spin_lock(&sdp->sd_rindex_spin);
  893. rgd = sdp->sd_rindex_forward;
  894. if (!rgd) {
  895. if (sdp->sd_rgrps >= journals)
  896. rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
  897. for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
  898. x++, rgd = gfs2_rgrpd_get_next(rgd))
  899. /* Do Nothing */;
  900. sdp->sd_rindex_forward = rgd;
  901. }
  902. spin_unlock(&sdp->sd_rindex_spin);
  903. return rgd;
  904. }
  905. /**
  906. * forward_rgrp_set - set the forward rgrp pointer
  907. * @sdp: the filesystem
  908. * @rgd: The new forward rgrp
  909. *
  910. */
  911. static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
  912. {
  913. spin_lock(&sdp->sd_rindex_spin);
  914. sdp->sd_rindex_forward = rgd;
  915. spin_unlock(&sdp->sd_rindex_spin);
  916. }
  917. /**
  918. * get_local_rgrp - Choose and lock a rgrp for allocation
  919. * @ip: the inode to reserve space for
  920. * @rgp: the chosen and locked rgrp
  921. *
  922. * Try to acquire rgrp in way which avoids contending with others.
  923. *
  924. * Returns: errno
  925. */
  926. static int get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
  927. {
  928. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  929. struct gfs2_rgrpd *rgd, *begin = NULL;
  930. struct gfs2_alloc *al = ip->i_alloc;
  931. int flags = LM_FLAG_TRY;
  932. int skipped = 0;
  933. int loops = 0;
  934. int error, rg_locked;
  935. rgd = gfs2_blk2rgrpd(sdp, ip->i_goal);
  936. while (rgd) {
  937. rg_locked = 0;
  938. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  939. rg_locked = 1;
  940. error = 0;
  941. } else {
  942. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
  943. LM_FLAG_TRY, &al->al_rgd_gh);
  944. }
  945. switch (error) {
  946. case 0:
  947. if (try_rgrp_fit(rgd, al))
  948. goto out;
  949. if (rgd->rd_flags & GFS2_RDF_CHECK)
  950. try_rgrp_unlink(rgd, last_unlinked, ip->i_no_addr);
  951. if (!rg_locked)
  952. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  953. /* fall through */
  954. case GLR_TRYFAILED:
  955. rgd = recent_rgrp_next(rgd);
  956. break;
  957. default:
  958. return error;
  959. }
  960. }
  961. /* Go through full list of rgrps */
  962. begin = rgd = forward_rgrp_get(sdp);
  963. for (;;) {
  964. rg_locked = 0;
  965. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  966. rg_locked = 1;
  967. error = 0;
  968. } else {
  969. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
  970. &al->al_rgd_gh);
  971. }
  972. switch (error) {
  973. case 0:
  974. if (try_rgrp_fit(rgd, al))
  975. goto out;
  976. if (rgd->rd_flags & GFS2_RDF_CHECK)
  977. try_rgrp_unlink(rgd, last_unlinked, ip->i_no_addr);
  978. if (!rg_locked)
  979. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  980. break;
  981. case GLR_TRYFAILED:
  982. skipped++;
  983. break;
  984. default:
  985. return error;
  986. }
  987. rgd = gfs2_rgrpd_get_next(rgd);
  988. if (!rgd)
  989. rgd = gfs2_rgrpd_get_first(sdp);
  990. if (rgd == begin) {
  991. if (++loops >= 3)
  992. return -ENOSPC;
  993. if (!skipped)
  994. loops++;
  995. flags = 0;
  996. if (loops == 2)
  997. gfs2_log_flush(sdp, NULL);
  998. }
  999. }
  1000. out:
  1001. if (begin) {
  1002. spin_lock(&sdp->sd_rindex_spin);
  1003. list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
  1004. spin_unlock(&sdp->sd_rindex_spin);
  1005. rgd = gfs2_rgrpd_get_next(rgd);
  1006. if (!rgd)
  1007. rgd = gfs2_rgrpd_get_first(sdp);
  1008. forward_rgrp_set(sdp, rgd);
  1009. }
  1010. return 0;
  1011. }
  1012. /**
  1013. * gfs2_inplace_reserve_i - Reserve space in the filesystem
  1014. * @ip: the inode to reserve space for
  1015. *
  1016. * Returns: errno
  1017. */
  1018. int gfs2_inplace_reserve_i(struct gfs2_inode *ip, int hold_rindex,
  1019. char *file, unsigned int line)
  1020. {
  1021. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1022. struct gfs2_alloc *al = ip->i_alloc;
  1023. int error = 0;
  1024. u64 last_unlinked = NO_BLOCK;
  1025. int tries = 0;
  1026. if (gfs2_assert_warn(sdp, al->al_requested))
  1027. return -EINVAL;
  1028. if (hold_rindex) {
  1029. /* We need to hold the rindex unless the inode we're using is
  1030. the rindex itself, in which case it's already held. */
  1031. if (ip != GFS2_I(sdp->sd_rindex))
  1032. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  1033. else if (!sdp->sd_rgrps) /* We may not have the rindex read
  1034. in, so: */
  1035. error = gfs2_ri_update(ip);
  1036. if (error)
  1037. return error;
  1038. }
  1039. try_again:
  1040. do {
  1041. error = get_local_rgrp(ip, &last_unlinked);
  1042. /* If there is no space, flushing the log may release some */
  1043. if (error) {
  1044. if (ip == GFS2_I(sdp->sd_rindex) &&
  1045. !sdp->sd_rindex_uptodate) {
  1046. error = gfs2_ri_update(ip);
  1047. if (error)
  1048. return error;
  1049. goto try_again;
  1050. }
  1051. gfs2_log_flush(sdp, NULL);
  1052. }
  1053. } while (error && tries++ < 3);
  1054. if (error) {
  1055. if (hold_rindex && ip != GFS2_I(sdp->sd_rindex))
  1056. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1057. return error;
  1058. }
  1059. /* no error, so we have the rgrp set in the inode's allocation. */
  1060. al->al_file = file;
  1061. al->al_line = line;
  1062. return 0;
  1063. }
  1064. /**
  1065. * gfs2_inplace_release - release an inplace reservation
  1066. * @ip: the inode the reservation was taken out on
  1067. *
  1068. * Release a reservation made by gfs2_inplace_reserve().
  1069. */
  1070. void gfs2_inplace_release(struct gfs2_inode *ip)
  1071. {
  1072. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1073. struct gfs2_alloc *al = ip->i_alloc;
  1074. if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
  1075. fs_warn(sdp, "al_alloced = %u, al_requested = %u "
  1076. "al_file = %s, al_line = %u\n",
  1077. al->al_alloced, al->al_requested, al->al_file,
  1078. al->al_line);
  1079. al->al_rgd = NULL;
  1080. if (al->al_rgd_gh.gh_gl)
  1081. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  1082. if (ip != GFS2_I(sdp->sd_rindex) && al->al_ri_gh.gh_gl)
  1083. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1084. }
  1085. /**
  1086. * gfs2_get_block_type - Check a block in a RG is of given type
  1087. * @rgd: the resource group holding the block
  1088. * @block: the block number
  1089. *
  1090. * Returns: The block type (GFS2_BLKST_*)
  1091. */
  1092. static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
  1093. {
  1094. struct gfs2_bitmap *bi = NULL;
  1095. u32 length, rgrp_block, buf_block;
  1096. unsigned int buf;
  1097. unsigned char type;
  1098. length = rgd->rd_length;
  1099. rgrp_block = block - rgd->rd_data0;
  1100. for (buf = 0; buf < length; buf++) {
  1101. bi = rgd->rd_bits + buf;
  1102. if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1103. break;
  1104. }
  1105. gfs2_assert(rgd->rd_sbd, buf < length);
  1106. buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
  1107. type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
  1108. bi->bi_len, buf_block);
  1109. return type;
  1110. }
  1111. /**
  1112. * rgblk_search - find a block in @old_state, change allocation
  1113. * state to @new_state
  1114. * @rgd: the resource group descriptor
  1115. * @goal: the goal block within the RG (start here to search for avail block)
  1116. * @old_state: GFS2_BLKST_XXX the before-allocation state to find
  1117. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1118. * @n: The extent length
  1119. *
  1120. * Walk rgrp's bitmap to find bits that represent a block in @old_state.
  1121. * Add the found bitmap buffer to the transaction.
  1122. * Set the found bits to @new_state to change block's allocation state.
  1123. *
  1124. * This function never fails, because we wouldn't call it unless we
  1125. * know (from reservation results, etc.) that a block is available.
  1126. *
  1127. * Scope of @goal and returned block is just within rgrp, not the whole
  1128. * filesystem.
  1129. *
  1130. * Returns: the block number allocated
  1131. */
  1132. static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
  1133. unsigned char old_state, unsigned char new_state,
  1134. unsigned int *n)
  1135. {
  1136. struct gfs2_bitmap *bi = NULL;
  1137. const u32 length = rgd->rd_length;
  1138. u32 blk = BFITNOENT;
  1139. unsigned int buf, x;
  1140. const unsigned int elen = *n;
  1141. const u8 *buffer = NULL;
  1142. *n = 0;
  1143. /* Find bitmap block that contains bits for goal block */
  1144. for (buf = 0; buf < length; buf++) {
  1145. bi = rgd->rd_bits + buf;
  1146. /* Convert scope of "goal" from rgrp-wide to within found bit block */
  1147. if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
  1148. goal -= bi->bi_start * GFS2_NBBY;
  1149. goto do_search;
  1150. }
  1151. }
  1152. buf = 0;
  1153. goal = 0;
  1154. do_search:
  1155. /* Search (up to entire) bitmap in this rgrp for allocatable block.
  1156. "x <= length", instead of "x < length", because we typically start
  1157. the search in the middle of a bit block, but if we can't find an
  1158. allocatable block anywhere else, we want to be able wrap around and
  1159. search in the first part of our first-searched bit block. */
  1160. for (x = 0; x <= length; x++) {
  1161. bi = rgd->rd_bits + buf;
  1162. if (test_bit(GBF_FULL, &bi->bi_flags) &&
  1163. (old_state == GFS2_BLKST_FREE))
  1164. goto skip;
  1165. /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
  1166. bitmaps, so we must search the originals for that. */
  1167. buffer = bi->bi_bh->b_data + bi->bi_offset;
  1168. if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
  1169. buffer = bi->bi_clone + bi->bi_offset;
  1170. blk = gfs2_bitfit(buffer, bi->bi_len, goal, old_state);
  1171. if (blk != BFITNOENT)
  1172. break;
  1173. if ((goal == 0) && (old_state == GFS2_BLKST_FREE))
  1174. set_bit(GBF_FULL, &bi->bi_flags);
  1175. /* Try next bitmap block (wrap back to rgrp header if at end) */
  1176. skip:
  1177. buf++;
  1178. buf %= length;
  1179. goal = 0;
  1180. }
  1181. if (blk == BFITNOENT)
  1182. return blk;
  1183. *n = 1;
  1184. if (old_state == new_state)
  1185. goto out;
  1186. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1187. gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
  1188. bi, blk, new_state);
  1189. goal = blk;
  1190. while (*n < elen) {
  1191. goal++;
  1192. if (goal >= (bi->bi_len * GFS2_NBBY))
  1193. break;
  1194. if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
  1195. GFS2_BLKST_FREE)
  1196. break;
  1197. gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
  1198. bi, goal, new_state);
  1199. (*n)++;
  1200. }
  1201. out:
  1202. return (bi->bi_start * GFS2_NBBY) + blk;
  1203. }
  1204. /**
  1205. * rgblk_free - Change alloc state of given block(s)
  1206. * @sdp: the filesystem
  1207. * @bstart: the start of a run of blocks to free
  1208. * @blen: the length of the block run (all must lie within ONE RG!)
  1209. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1210. *
  1211. * Returns: Resource group containing the block(s)
  1212. */
  1213. static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
  1214. u32 blen, unsigned char new_state)
  1215. {
  1216. struct gfs2_rgrpd *rgd;
  1217. struct gfs2_bitmap *bi = NULL;
  1218. u32 length, rgrp_blk, buf_blk;
  1219. unsigned int buf;
  1220. rgd = gfs2_blk2rgrpd(sdp, bstart);
  1221. if (!rgd) {
  1222. if (gfs2_consist(sdp))
  1223. fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
  1224. return NULL;
  1225. }
  1226. length = rgd->rd_length;
  1227. rgrp_blk = bstart - rgd->rd_data0;
  1228. while (blen--) {
  1229. for (buf = 0; buf < length; buf++) {
  1230. bi = rgd->rd_bits + buf;
  1231. if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1232. break;
  1233. }
  1234. gfs2_assert(rgd->rd_sbd, buf < length);
  1235. buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
  1236. rgrp_blk++;
  1237. if (!bi->bi_clone) {
  1238. bi->bi_clone = kmalloc(bi->bi_bh->b_size,
  1239. GFP_NOFS | __GFP_NOFAIL);
  1240. memcpy(bi->bi_clone + bi->bi_offset,
  1241. bi->bi_bh->b_data + bi->bi_offset,
  1242. bi->bi_len);
  1243. }
  1244. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1245. gfs2_setbit(rgd, bi->bi_bh->b_data, NULL, bi->bi_offset,
  1246. bi, buf_blk, new_state);
  1247. }
  1248. return rgd;
  1249. }
  1250. /**
  1251. * gfs2_rgrp_dump - print out an rgrp
  1252. * @seq: The iterator
  1253. * @gl: The glock in question
  1254. *
  1255. */
  1256. int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl)
  1257. {
  1258. const struct gfs2_rgrpd *rgd = gl->gl_object;
  1259. if (rgd == NULL)
  1260. return 0;
  1261. gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u\n",
  1262. (unsigned long long)rgd->rd_addr, rgd->rd_flags,
  1263. rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes);
  1264. return 0;
  1265. }
  1266. static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
  1267. {
  1268. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1269. fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
  1270. (unsigned long long)rgd->rd_addr);
  1271. fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
  1272. gfs2_rgrp_dump(NULL, rgd->rd_gl);
  1273. rgd->rd_flags |= GFS2_RDF_ERROR;
  1274. }
  1275. /**
  1276. * gfs2_alloc_block - Allocate one or more blocks
  1277. * @ip: the inode to allocate the block for
  1278. * @bn: Used to return the starting block number
  1279. * @n: requested number of blocks/extent length (value/result)
  1280. *
  1281. * Returns: 0 or error
  1282. */
  1283. int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n)
  1284. {
  1285. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1286. struct buffer_head *dibh;
  1287. struct gfs2_alloc *al = ip->i_alloc;
  1288. struct gfs2_rgrpd *rgd;
  1289. u32 goal, blk;
  1290. u64 block;
  1291. int error;
  1292. /* Only happens if there is a bug in gfs2, return something distinctive
  1293. * to ensure that it is noticed.
  1294. */
  1295. if (al == NULL)
  1296. return -ECANCELED;
  1297. rgd = al->al_rgd;
  1298. if (rgrp_contains_block(rgd, ip->i_goal))
  1299. goal = ip->i_goal - rgd->rd_data0;
  1300. else
  1301. goal = rgd->rd_last_alloc;
  1302. blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n);
  1303. /* Since all blocks are reserved in advance, this shouldn't happen */
  1304. if (blk == BFITNOENT)
  1305. goto rgrp_error;
  1306. rgd->rd_last_alloc = blk;
  1307. block = rgd->rd_data0 + blk;
  1308. ip->i_goal = block;
  1309. error = gfs2_meta_inode_buffer(ip, &dibh);
  1310. if (error == 0) {
  1311. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  1312. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1313. di->di_goal_meta = di->di_goal_data = cpu_to_be64(ip->i_goal);
  1314. brelse(dibh);
  1315. }
  1316. if (rgd->rd_free < *n)
  1317. goto rgrp_error;
  1318. rgd->rd_free -= *n;
  1319. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1320. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1321. al->al_alloced += *n;
  1322. gfs2_statfs_change(sdp, 0, -(s64)*n, 0);
  1323. gfs2_quota_change(ip, *n, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1324. spin_lock(&sdp->sd_rindex_spin);
  1325. rgd->rd_free_clone -= *n;
  1326. spin_unlock(&sdp->sd_rindex_spin);
  1327. trace_gfs2_block_alloc(ip, block, *n, GFS2_BLKST_USED);
  1328. *bn = block;
  1329. return 0;
  1330. rgrp_error:
  1331. gfs2_rgrp_error(rgd);
  1332. return -EIO;
  1333. }
  1334. /**
  1335. * gfs2_alloc_di - Allocate a dinode
  1336. * @dip: the directory that the inode is going in
  1337. * @bn: the block number which is allocated
  1338. * @generation: the generation number of the inode
  1339. *
  1340. * Returns: 0 on success or error
  1341. */
  1342. int gfs2_alloc_di(struct gfs2_inode *dip, u64 *bn, u64 *generation)
  1343. {
  1344. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1345. struct gfs2_alloc *al = dip->i_alloc;
  1346. struct gfs2_rgrpd *rgd = al->al_rgd;
  1347. u32 blk;
  1348. u64 block;
  1349. unsigned int n = 1;
  1350. blk = rgblk_search(rgd, rgd->rd_last_alloc,
  1351. GFS2_BLKST_FREE, GFS2_BLKST_DINODE, &n);
  1352. /* Since all blocks are reserved in advance, this shouldn't happen */
  1353. if (blk == BFITNOENT)
  1354. goto rgrp_error;
  1355. rgd->rd_last_alloc = blk;
  1356. block = rgd->rd_data0 + blk;
  1357. if (rgd->rd_free == 0)
  1358. goto rgrp_error;
  1359. rgd->rd_free--;
  1360. rgd->rd_dinodes++;
  1361. *generation = rgd->rd_igeneration++;
  1362. if (*generation == 0)
  1363. *generation = rgd->rd_igeneration++;
  1364. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1365. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1366. al->al_alloced++;
  1367. gfs2_statfs_change(sdp, 0, -1, +1);
  1368. gfs2_trans_add_unrevoke(sdp, block, 1);
  1369. spin_lock(&sdp->sd_rindex_spin);
  1370. rgd->rd_free_clone--;
  1371. spin_unlock(&sdp->sd_rindex_spin);
  1372. trace_gfs2_block_alloc(dip, block, 1, GFS2_BLKST_DINODE);
  1373. *bn = block;
  1374. return 0;
  1375. rgrp_error:
  1376. gfs2_rgrp_error(rgd);
  1377. return -EIO;
  1378. }
  1379. /**
  1380. * __gfs2_free_blocks - free a contiguous run of block(s)
  1381. * @ip: the inode these blocks are being freed from
  1382. * @bstart: first block of a run of contiguous blocks
  1383. * @blen: the length of the block run
  1384. * @meta: 1 if the blocks represent metadata
  1385. *
  1386. */
  1387. void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta)
  1388. {
  1389. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1390. struct gfs2_rgrpd *rgd;
  1391. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1392. if (!rgd)
  1393. return;
  1394. trace_gfs2_block_alloc(ip, bstart, blen, GFS2_BLKST_FREE);
  1395. rgd->rd_free += blen;
  1396. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1397. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1398. gfs2_trans_add_rg(rgd);
  1399. /* Directories keep their data in the metadata address space */
  1400. if (meta || ip->i_depth)
  1401. gfs2_meta_wipe(ip, bstart, blen);
  1402. }
  1403. /**
  1404. * gfs2_free_meta - free a contiguous run of data block(s)
  1405. * @ip: the inode these blocks are being freed from
  1406. * @bstart: first block of a run of contiguous blocks
  1407. * @blen: the length of the block run
  1408. *
  1409. */
  1410. void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
  1411. {
  1412. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1413. __gfs2_free_blocks(ip, bstart, blen, 1);
  1414. gfs2_statfs_change(sdp, 0, +blen, 0);
  1415. gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1416. }
  1417. void gfs2_unlink_di(struct inode *inode)
  1418. {
  1419. struct gfs2_inode *ip = GFS2_I(inode);
  1420. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1421. struct gfs2_rgrpd *rgd;
  1422. u64 blkno = ip->i_no_addr;
  1423. rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
  1424. if (!rgd)
  1425. return;
  1426. trace_gfs2_block_alloc(ip, blkno, 1, GFS2_BLKST_UNLINKED);
  1427. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1428. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1429. gfs2_trans_add_rg(rgd);
  1430. }
  1431. static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
  1432. {
  1433. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1434. struct gfs2_rgrpd *tmp_rgd;
  1435. tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
  1436. if (!tmp_rgd)
  1437. return;
  1438. gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
  1439. if (!rgd->rd_dinodes)
  1440. gfs2_consist_rgrpd(rgd);
  1441. rgd->rd_dinodes--;
  1442. rgd->rd_free++;
  1443. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1444. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1445. gfs2_statfs_change(sdp, 0, +1, -1);
  1446. gfs2_trans_add_rg(rgd);
  1447. }
  1448. void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
  1449. {
  1450. gfs2_free_uninit_di(rgd, ip->i_no_addr);
  1451. trace_gfs2_block_alloc(ip, ip->i_no_addr, 1, GFS2_BLKST_FREE);
  1452. gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1453. gfs2_meta_wipe(ip, ip->i_no_addr, 1);
  1454. }
  1455. /**
  1456. * gfs2_check_blk_type - Check the type of a block
  1457. * @sdp: The superblock
  1458. * @no_addr: The block number to check
  1459. * @type: The block type we are looking for
  1460. *
  1461. * Returns: 0 if the block type matches the expected type
  1462. * -ESTALE if it doesn't match
  1463. * or -ve errno if something went wrong while checking
  1464. */
  1465. int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
  1466. {
  1467. struct gfs2_rgrpd *rgd;
  1468. struct gfs2_holder ri_gh, rgd_gh;
  1469. struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
  1470. int ri_locked = 0;
  1471. int error;
  1472. if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
  1473. error = gfs2_rindex_hold(sdp, &ri_gh);
  1474. if (error)
  1475. goto fail;
  1476. ri_locked = 1;
  1477. }
  1478. error = -EINVAL;
  1479. rgd = gfs2_blk2rgrpd(sdp, no_addr);
  1480. if (!rgd)
  1481. goto fail_rindex;
  1482. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
  1483. if (error)
  1484. goto fail_rindex;
  1485. if (gfs2_get_block_type(rgd, no_addr) != type)
  1486. error = -ESTALE;
  1487. gfs2_glock_dq_uninit(&rgd_gh);
  1488. fail_rindex:
  1489. if (ri_locked)
  1490. gfs2_glock_dq_uninit(&ri_gh);
  1491. fail:
  1492. return error;
  1493. }
  1494. /**
  1495. * gfs2_rlist_add - add a RG to a list of RGs
  1496. * @sdp: the filesystem
  1497. * @rlist: the list of resource groups
  1498. * @block: the block
  1499. *
  1500. * Figure out what RG a block belongs to and add that RG to the list
  1501. *
  1502. * FIXME: Don't use NOFAIL
  1503. *
  1504. */
  1505. void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
  1506. u64 block)
  1507. {
  1508. struct gfs2_rgrpd *rgd;
  1509. struct gfs2_rgrpd **tmp;
  1510. unsigned int new_space;
  1511. unsigned int x;
  1512. if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
  1513. return;
  1514. rgd = gfs2_blk2rgrpd(sdp, block);
  1515. if (!rgd) {
  1516. if (gfs2_consist(sdp))
  1517. fs_err(sdp, "block = %llu\n", (unsigned long long)block);
  1518. return;
  1519. }
  1520. for (x = 0; x < rlist->rl_rgrps; x++)
  1521. if (rlist->rl_rgd[x] == rgd)
  1522. return;
  1523. if (rlist->rl_rgrps == rlist->rl_space) {
  1524. new_space = rlist->rl_space + 10;
  1525. tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
  1526. GFP_NOFS | __GFP_NOFAIL);
  1527. if (rlist->rl_rgd) {
  1528. memcpy(tmp, rlist->rl_rgd,
  1529. rlist->rl_space * sizeof(struct gfs2_rgrpd *));
  1530. kfree(rlist->rl_rgd);
  1531. }
  1532. rlist->rl_space = new_space;
  1533. rlist->rl_rgd = tmp;
  1534. }
  1535. rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
  1536. }
  1537. /**
  1538. * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
  1539. * and initialize an array of glock holders for them
  1540. * @rlist: the list of resource groups
  1541. * @state: the lock state to acquire the RG lock in
  1542. * @flags: the modifier flags for the holder structures
  1543. *
  1544. * FIXME: Don't use NOFAIL
  1545. *
  1546. */
  1547. void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
  1548. {
  1549. unsigned int x;
  1550. rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
  1551. GFP_NOFS | __GFP_NOFAIL);
  1552. for (x = 0; x < rlist->rl_rgrps; x++)
  1553. gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
  1554. state, 0,
  1555. &rlist->rl_ghs[x]);
  1556. }
  1557. /**
  1558. * gfs2_rlist_free - free a resource group list
  1559. * @list: the list of resource groups
  1560. *
  1561. */
  1562. void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
  1563. {
  1564. unsigned int x;
  1565. kfree(rlist->rl_rgd);
  1566. if (rlist->rl_ghs) {
  1567. for (x = 0; x < rlist->rl_rgrps; x++)
  1568. gfs2_holder_uninit(&rlist->rl_ghs[x]);
  1569. kfree(rlist->rl_ghs);
  1570. }
  1571. }