rgrp.c 46 KB

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