rgrp.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  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) >= i_size_read(inode))
  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 = i_size_read(inode);
  510. struct gfs2_rgrpd *rgd;
  511. unsigned int max_data = 0;
  512. int error;
  513. do_div(rgrp_count, sizeof(struct gfs2_rindex));
  514. clear_rgrpdi(sdp);
  515. file_ra_state_init(&ra_state, inode->i_mapping);
  516. for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
  517. error = read_rindex_entry(ip, &ra_state);
  518. if (error) {
  519. clear_rgrpdi(sdp);
  520. return error;
  521. }
  522. }
  523. list_for_each_entry(rgd, &sdp->sd_rindex_list, rd_list)
  524. if (rgd->rd_data > max_data)
  525. max_data = rgd->rd_data;
  526. sdp->sd_max_rg_data = max_data;
  527. sdp->sd_rindex_uptodate = 1;
  528. return 0;
  529. }
  530. /**
  531. * gfs2_ri_update_special - Pull in a new resource index from the disk
  532. *
  533. * This is a special version that's safe to call from gfs2_inplace_reserve_i.
  534. * In this case we know that we don't have any resource groups in memory yet.
  535. *
  536. * @ip: pointer to the rindex inode
  537. *
  538. * Returns: 0 on successful update, error code otherwise
  539. */
  540. static int gfs2_ri_update_special(struct gfs2_inode *ip)
  541. {
  542. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  543. struct inode *inode = &ip->i_inode;
  544. struct file_ra_state ra_state;
  545. struct gfs2_rgrpd *rgd;
  546. unsigned int max_data = 0;
  547. int error;
  548. file_ra_state_init(&ra_state, inode->i_mapping);
  549. for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
  550. /* Ignore partials */
  551. if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
  552. i_size_read(inode))
  553. break;
  554. error = read_rindex_entry(ip, &ra_state);
  555. if (error) {
  556. clear_rgrpdi(sdp);
  557. return error;
  558. }
  559. }
  560. list_for_each_entry(rgd, &sdp->sd_rindex_list, rd_list)
  561. if (rgd->rd_data > max_data)
  562. max_data = rgd->rd_data;
  563. sdp->sd_max_rg_data = max_data;
  564. sdp->sd_rindex_uptodate = 1;
  565. return 0;
  566. }
  567. /**
  568. * gfs2_rindex_hold - Grab a lock on the rindex
  569. * @sdp: The GFS2 superblock
  570. * @ri_gh: the glock holder
  571. *
  572. * We grab a lock on the rindex inode to make sure that it doesn't
  573. * change whilst we are performing an operation. We keep this lock
  574. * for quite long periods of time compared to other locks. This
  575. * doesn't matter, since it is shared and it is very, very rarely
  576. * accessed in the exclusive mode (i.e. only when expanding the filesystem).
  577. *
  578. * This makes sure that we're using the latest copy of the resource index
  579. * special file, which might have been updated if someone expanded the
  580. * filesystem (via gfs2_grow utility), which adds new resource groups.
  581. *
  582. * Returns: 0 on success, error code otherwise
  583. */
  584. int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
  585. {
  586. struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
  587. struct gfs2_glock *gl = ip->i_gl;
  588. int error;
  589. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
  590. if (error)
  591. return error;
  592. /* Read new copy from disk if we don't have the latest */
  593. if (!sdp->sd_rindex_uptodate) {
  594. mutex_lock(&sdp->sd_rindex_mutex);
  595. if (!sdp->sd_rindex_uptodate) {
  596. error = gfs2_ri_update(ip);
  597. if (error)
  598. gfs2_glock_dq_uninit(ri_gh);
  599. }
  600. mutex_unlock(&sdp->sd_rindex_mutex);
  601. }
  602. return error;
  603. }
  604. static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
  605. {
  606. const struct gfs2_rgrp *str = buf;
  607. u32 rg_flags;
  608. rg_flags = be32_to_cpu(str->rg_flags);
  609. rg_flags &= ~GFS2_RDF_MASK;
  610. rgd->rd_flags &= GFS2_RDF_MASK;
  611. rgd->rd_flags |= rg_flags;
  612. rgd->rd_free = be32_to_cpu(str->rg_free);
  613. rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
  614. rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
  615. }
  616. static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
  617. {
  618. struct gfs2_rgrp *str = buf;
  619. str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
  620. str->rg_free = cpu_to_be32(rgd->rd_free);
  621. str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
  622. str->__pad = cpu_to_be32(0);
  623. str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
  624. memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
  625. }
  626. /**
  627. * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
  628. * @rgd: the struct gfs2_rgrpd describing the RG to read in
  629. *
  630. * Read in all of a Resource Group's header and bitmap blocks.
  631. * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
  632. *
  633. * Returns: errno
  634. */
  635. int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
  636. {
  637. struct gfs2_sbd *sdp = rgd->rd_sbd;
  638. struct gfs2_glock *gl = rgd->rd_gl;
  639. unsigned int length = rgd->rd_length;
  640. struct gfs2_bitmap *bi;
  641. unsigned int x, y;
  642. int error;
  643. mutex_lock(&rgd->rd_mutex);
  644. spin_lock(&sdp->sd_rindex_spin);
  645. if (rgd->rd_bh_count) {
  646. rgd->rd_bh_count++;
  647. spin_unlock(&sdp->sd_rindex_spin);
  648. mutex_unlock(&rgd->rd_mutex);
  649. return 0;
  650. }
  651. spin_unlock(&sdp->sd_rindex_spin);
  652. for (x = 0; x < length; x++) {
  653. bi = rgd->rd_bits + x;
  654. error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
  655. if (error)
  656. goto fail;
  657. }
  658. for (y = length; y--;) {
  659. bi = rgd->rd_bits + y;
  660. error = gfs2_meta_wait(sdp, bi->bi_bh);
  661. if (error)
  662. goto fail;
  663. if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
  664. GFS2_METATYPE_RG)) {
  665. error = -EIO;
  666. goto fail;
  667. }
  668. }
  669. if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
  670. for (x = 0; x < length; x++)
  671. clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
  672. gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
  673. rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
  674. }
  675. spin_lock(&sdp->sd_rindex_spin);
  676. rgd->rd_free_clone = rgd->rd_free;
  677. rgd->rd_bh_count++;
  678. spin_unlock(&sdp->sd_rindex_spin);
  679. mutex_unlock(&rgd->rd_mutex);
  680. return 0;
  681. fail:
  682. while (x--) {
  683. bi = rgd->rd_bits + x;
  684. brelse(bi->bi_bh);
  685. bi->bi_bh = NULL;
  686. gfs2_assert_warn(sdp, !bi->bi_clone);
  687. }
  688. mutex_unlock(&rgd->rd_mutex);
  689. return error;
  690. }
  691. void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
  692. {
  693. struct gfs2_sbd *sdp = rgd->rd_sbd;
  694. spin_lock(&sdp->sd_rindex_spin);
  695. gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
  696. rgd->rd_bh_count++;
  697. spin_unlock(&sdp->sd_rindex_spin);
  698. }
  699. /**
  700. * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
  701. * @rgd: the struct gfs2_rgrpd describing the RG to read in
  702. *
  703. */
  704. void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
  705. {
  706. struct gfs2_sbd *sdp = rgd->rd_sbd;
  707. int x, length = rgd->rd_length;
  708. spin_lock(&sdp->sd_rindex_spin);
  709. gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
  710. if (--rgd->rd_bh_count) {
  711. spin_unlock(&sdp->sd_rindex_spin);
  712. return;
  713. }
  714. for (x = 0; x < length; x++) {
  715. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  716. kfree(bi->bi_clone);
  717. bi->bi_clone = NULL;
  718. brelse(bi->bi_bh);
  719. bi->bi_bh = NULL;
  720. }
  721. spin_unlock(&sdp->sd_rindex_spin);
  722. }
  723. static void gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
  724. const struct gfs2_bitmap *bi)
  725. {
  726. struct super_block *sb = sdp->sd_vfs;
  727. struct block_device *bdev = sb->s_bdev;
  728. const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize /
  729. bdev_logical_block_size(sb->s_bdev);
  730. u64 blk;
  731. sector_t start = 0;
  732. sector_t nr_sects = 0;
  733. int rv;
  734. unsigned int x;
  735. for (x = 0; x < bi->bi_len; x++) {
  736. const u8 *orig = bi->bi_bh->b_data + bi->bi_offset + x;
  737. const u8 *clone = bi->bi_clone + bi->bi_offset + x;
  738. u8 diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
  739. diff &= 0x55;
  740. if (diff == 0)
  741. continue;
  742. blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
  743. blk *= sects_per_blk; /* convert to sectors */
  744. while(diff) {
  745. if (diff & 1) {
  746. if (nr_sects == 0)
  747. goto start_new_extent;
  748. if ((start + nr_sects) != blk) {
  749. rv = blkdev_issue_discard(bdev, start,
  750. nr_sects, GFP_NOFS,
  751. 0);
  752. if (rv)
  753. goto fail;
  754. nr_sects = 0;
  755. start_new_extent:
  756. start = blk;
  757. }
  758. nr_sects += sects_per_blk;
  759. }
  760. diff >>= 2;
  761. blk += sects_per_blk;
  762. }
  763. }
  764. if (nr_sects) {
  765. rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 0);
  766. if (rv)
  767. goto fail;
  768. }
  769. return;
  770. fail:
  771. fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem", rv);
  772. sdp->sd_args.ar_discard = 0;
  773. }
  774. void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
  775. {
  776. struct gfs2_sbd *sdp = rgd->rd_sbd;
  777. unsigned int length = rgd->rd_length;
  778. unsigned int x;
  779. for (x = 0; x < length; x++) {
  780. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  781. if (!bi->bi_clone)
  782. continue;
  783. if (sdp->sd_args.ar_discard)
  784. gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bi);
  785. clear_bit(GBF_FULL, &bi->bi_flags);
  786. memcpy(bi->bi_clone + bi->bi_offset,
  787. bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
  788. }
  789. spin_lock(&sdp->sd_rindex_spin);
  790. rgd->rd_free_clone = rgd->rd_free;
  791. spin_unlock(&sdp->sd_rindex_spin);
  792. }
  793. /**
  794. * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
  795. * @ip: the incore GFS2 inode structure
  796. *
  797. * Returns: the struct gfs2_alloc
  798. */
  799. struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
  800. {
  801. BUG_ON(ip->i_alloc != NULL);
  802. ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_NOFS);
  803. return ip->i_alloc;
  804. }
  805. /**
  806. * try_rgrp_fit - See if a given reservation will fit in a given RG
  807. * @rgd: the RG data
  808. * @al: the struct gfs2_alloc structure describing the reservation
  809. *
  810. * If there's room for the requested blocks to be allocated from the RG:
  811. * Sets the $al_rgd field in @al.
  812. *
  813. * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
  814. */
  815. static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
  816. {
  817. struct gfs2_sbd *sdp = rgd->rd_sbd;
  818. int ret = 0;
  819. if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR))
  820. return 0;
  821. spin_lock(&sdp->sd_rindex_spin);
  822. if (rgd->rd_free_clone >= al->al_requested) {
  823. al->al_rgd = rgd;
  824. ret = 1;
  825. }
  826. spin_unlock(&sdp->sd_rindex_spin);
  827. return ret;
  828. }
  829. /**
  830. * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
  831. * @rgd: The rgrp
  832. *
  833. * Returns: 0 if no error
  834. * The inode, if one has been found, in inode.
  835. */
  836. static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
  837. {
  838. u32 goal = 0, block;
  839. u64 no_addr;
  840. struct gfs2_sbd *sdp = rgd->rd_sbd;
  841. unsigned int n;
  842. struct gfs2_glock *gl;
  843. struct gfs2_inode *ip;
  844. int error;
  845. int found = 0;
  846. while (goal < rgd->rd_data) {
  847. down_write(&sdp->sd_log_flush_lock);
  848. n = 1;
  849. block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
  850. GFS2_BLKST_UNLINKED, &n);
  851. up_write(&sdp->sd_log_flush_lock);
  852. if (block == BFITNOENT)
  853. break;
  854. /* rgblk_search can return a block < goal, so we need to
  855. keep it marching forward. */
  856. no_addr = block + rgd->rd_data0;
  857. goal++;
  858. if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
  859. continue;
  860. if (no_addr == skip)
  861. continue;
  862. *last_unlinked = no_addr;
  863. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &gl);
  864. if (error)
  865. continue;
  866. /* If the inode is already in cache, we can ignore it here
  867. * because the existing inode disposal code will deal with
  868. * it when all refs have gone away. Accessing gl_object like
  869. * this is not safe in general. Here it is ok because we do
  870. * not dereference the pointer, and we only need an approx
  871. * answer to whether it is NULL or not.
  872. */
  873. ip = gl->gl_object;
  874. if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
  875. gfs2_glock_put(gl);
  876. else
  877. found++;
  878. /* Limit reclaim to sensible number of tasks */
  879. if (found > 2*NR_CPUS)
  880. return;
  881. }
  882. rgd->rd_flags &= ~GFS2_RDF_CHECK;
  883. return;
  884. }
  885. /**
  886. * recent_rgrp_next - get next RG from "recent" list
  887. * @cur_rgd: current rgrp
  888. *
  889. * Returns: The next rgrp in the recent list
  890. */
  891. static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd)
  892. {
  893. struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
  894. struct list_head *head;
  895. struct gfs2_rgrpd *rgd;
  896. spin_lock(&sdp->sd_rindex_spin);
  897. head = &sdp->sd_rindex_mru_list;
  898. if (unlikely(cur_rgd->rd_list_mru.next == head)) {
  899. spin_unlock(&sdp->sd_rindex_spin);
  900. return NULL;
  901. }
  902. rgd = list_entry(cur_rgd->rd_list_mru.next, struct gfs2_rgrpd, rd_list_mru);
  903. spin_unlock(&sdp->sd_rindex_spin);
  904. return rgd;
  905. }
  906. /**
  907. * forward_rgrp_get - get an rgrp to try next from full list
  908. * @sdp: The GFS2 superblock
  909. *
  910. * Returns: The rgrp to try next
  911. */
  912. static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
  913. {
  914. struct gfs2_rgrpd *rgd;
  915. unsigned int journals = gfs2_jindex_size(sdp);
  916. unsigned int rg = 0, x;
  917. spin_lock(&sdp->sd_rindex_spin);
  918. rgd = sdp->sd_rindex_forward;
  919. if (!rgd) {
  920. if (sdp->sd_rgrps >= journals)
  921. rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
  922. for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
  923. x++, rgd = gfs2_rgrpd_get_next(rgd))
  924. /* Do Nothing */;
  925. sdp->sd_rindex_forward = rgd;
  926. }
  927. spin_unlock(&sdp->sd_rindex_spin);
  928. return rgd;
  929. }
  930. /**
  931. * forward_rgrp_set - set the forward rgrp pointer
  932. * @sdp: the filesystem
  933. * @rgd: The new forward rgrp
  934. *
  935. */
  936. static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
  937. {
  938. spin_lock(&sdp->sd_rindex_spin);
  939. sdp->sd_rindex_forward = rgd;
  940. spin_unlock(&sdp->sd_rindex_spin);
  941. }
  942. /**
  943. * get_local_rgrp - Choose and lock a rgrp for allocation
  944. * @ip: the inode to reserve space for
  945. * @rgp: the chosen and locked rgrp
  946. *
  947. * Try to acquire rgrp in way which avoids contending with others.
  948. *
  949. * Returns: errno
  950. */
  951. static int get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
  952. {
  953. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  954. struct gfs2_rgrpd *rgd, *begin = NULL;
  955. struct gfs2_alloc *al = ip->i_alloc;
  956. int flags = LM_FLAG_TRY;
  957. int skipped = 0;
  958. int loops = 0;
  959. int error, rg_locked;
  960. rgd = gfs2_blk2rgrpd(sdp, ip->i_goal);
  961. while (rgd) {
  962. rg_locked = 0;
  963. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  964. rg_locked = 1;
  965. error = 0;
  966. } else {
  967. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
  968. LM_FLAG_TRY, &al->al_rgd_gh);
  969. }
  970. switch (error) {
  971. case 0:
  972. if (try_rgrp_fit(rgd, al))
  973. goto out;
  974. if (rgd->rd_flags & GFS2_RDF_CHECK)
  975. try_rgrp_unlink(rgd, last_unlinked, ip->i_no_addr);
  976. if (!rg_locked)
  977. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  978. /* fall through */
  979. case GLR_TRYFAILED:
  980. rgd = recent_rgrp_next(rgd);
  981. break;
  982. default:
  983. return error;
  984. }
  985. }
  986. /* Go through full list of rgrps */
  987. begin = rgd = forward_rgrp_get(sdp);
  988. for (;;) {
  989. rg_locked = 0;
  990. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  991. rg_locked = 1;
  992. error = 0;
  993. } else {
  994. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
  995. &al->al_rgd_gh);
  996. }
  997. switch (error) {
  998. case 0:
  999. if (try_rgrp_fit(rgd, al))
  1000. goto out;
  1001. if (rgd->rd_flags & GFS2_RDF_CHECK)
  1002. try_rgrp_unlink(rgd, last_unlinked, ip->i_no_addr);
  1003. if (!rg_locked)
  1004. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  1005. break;
  1006. case GLR_TRYFAILED:
  1007. skipped++;
  1008. break;
  1009. default:
  1010. return error;
  1011. }
  1012. rgd = gfs2_rgrpd_get_next(rgd);
  1013. if (!rgd)
  1014. rgd = gfs2_rgrpd_get_first(sdp);
  1015. if (rgd == begin) {
  1016. if (++loops >= 3)
  1017. return -ENOSPC;
  1018. if (!skipped)
  1019. loops++;
  1020. flags = 0;
  1021. if (loops == 2)
  1022. gfs2_log_flush(sdp, NULL);
  1023. }
  1024. }
  1025. out:
  1026. if (begin) {
  1027. spin_lock(&sdp->sd_rindex_spin);
  1028. list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
  1029. spin_unlock(&sdp->sd_rindex_spin);
  1030. rgd = gfs2_rgrpd_get_next(rgd);
  1031. if (!rgd)
  1032. rgd = gfs2_rgrpd_get_first(sdp);
  1033. forward_rgrp_set(sdp, rgd);
  1034. }
  1035. return 0;
  1036. }
  1037. /**
  1038. * gfs2_inplace_reserve_i - Reserve space in the filesystem
  1039. * @ip: the inode to reserve space for
  1040. *
  1041. * Returns: errno
  1042. */
  1043. int gfs2_inplace_reserve_i(struct gfs2_inode *ip, int hold_rindex,
  1044. char *file, unsigned int line)
  1045. {
  1046. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1047. struct gfs2_alloc *al = ip->i_alloc;
  1048. int error = 0;
  1049. u64 last_unlinked = NO_BLOCK;
  1050. int tries = 0;
  1051. if (gfs2_assert_warn(sdp, al->al_requested))
  1052. return -EINVAL;
  1053. if (hold_rindex) {
  1054. /* We need to hold the rindex unless the inode we're using is
  1055. the rindex itself, in which case it's already held. */
  1056. if (ip != GFS2_I(sdp->sd_rindex))
  1057. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  1058. else if (!sdp->sd_rgrps) /* We may not have the rindex read
  1059. in, so: */
  1060. error = gfs2_ri_update_special(ip);
  1061. if (error)
  1062. return error;
  1063. }
  1064. do {
  1065. error = get_local_rgrp(ip, &last_unlinked);
  1066. /* If there is no space, flushing the log may release some */
  1067. if (error)
  1068. gfs2_log_flush(sdp, NULL);
  1069. } while (error && tries++ < 3);
  1070. if (error) {
  1071. if (hold_rindex && ip != GFS2_I(sdp->sd_rindex))
  1072. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1073. return error;
  1074. }
  1075. /* no error, so we have the rgrp set in the inode's allocation. */
  1076. al->al_file = file;
  1077. al->al_line = line;
  1078. return 0;
  1079. }
  1080. /**
  1081. * gfs2_inplace_release - release an inplace reservation
  1082. * @ip: the inode the reservation was taken out on
  1083. *
  1084. * Release a reservation made by gfs2_inplace_reserve().
  1085. */
  1086. void gfs2_inplace_release(struct gfs2_inode *ip)
  1087. {
  1088. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1089. struct gfs2_alloc *al = ip->i_alloc;
  1090. if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
  1091. fs_warn(sdp, "al_alloced = %u, al_requested = %u "
  1092. "al_file = %s, al_line = %u\n",
  1093. al->al_alloced, al->al_requested, al->al_file,
  1094. al->al_line);
  1095. al->al_rgd = NULL;
  1096. if (al->al_rgd_gh.gh_gl)
  1097. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  1098. if (ip != GFS2_I(sdp->sd_rindex) && al->al_ri_gh.gh_gl)
  1099. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1100. }
  1101. /**
  1102. * gfs2_get_block_type - Check a block in a RG is of given type
  1103. * @rgd: the resource group holding the block
  1104. * @block: the block number
  1105. *
  1106. * Returns: The block type (GFS2_BLKST_*)
  1107. */
  1108. static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
  1109. {
  1110. struct gfs2_bitmap *bi = NULL;
  1111. u32 length, rgrp_block, buf_block;
  1112. unsigned int buf;
  1113. unsigned char type;
  1114. length = rgd->rd_length;
  1115. rgrp_block = block - rgd->rd_data0;
  1116. for (buf = 0; buf < length; buf++) {
  1117. bi = rgd->rd_bits + buf;
  1118. if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1119. break;
  1120. }
  1121. gfs2_assert(rgd->rd_sbd, buf < length);
  1122. buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
  1123. type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
  1124. bi->bi_len, buf_block);
  1125. return type;
  1126. }
  1127. /**
  1128. * rgblk_search - find a block in @old_state, change allocation
  1129. * state to @new_state
  1130. * @rgd: the resource group descriptor
  1131. * @goal: the goal block within the RG (start here to search for avail block)
  1132. * @old_state: GFS2_BLKST_XXX the before-allocation state to find
  1133. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1134. * @n: The extent length
  1135. *
  1136. * Walk rgrp's bitmap to find bits that represent a block in @old_state.
  1137. * Add the found bitmap buffer to the transaction.
  1138. * Set the found bits to @new_state to change block's allocation state.
  1139. *
  1140. * This function never fails, because we wouldn't call it unless we
  1141. * know (from reservation results, etc.) that a block is available.
  1142. *
  1143. * Scope of @goal and returned block is just within rgrp, not the whole
  1144. * filesystem.
  1145. *
  1146. * Returns: the block number allocated
  1147. */
  1148. static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
  1149. unsigned char old_state, unsigned char new_state,
  1150. unsigned int *n)
  1151. {
  1152. struct gfs2_bitmap *bi = NULL;
  1153. const u32 length = rgd->rd_length;
  1154. u32 blk = BFITNOENT;
  1155. unsigned int buf, x;
  1156. const unsigned int elen = *n;
  1157. const u8 *buffer = NULL;
  1158. *n = 0;
  1159. /* Find bitmap block that contains bits for goal block */
  1160. for (buf = 0; buf < length; buf++) {
  1161. bi = rgd->rd_bits + buf;
  1162. /* Convert scope of "goal" from rgrp-wide to within found bit block */
  1163. if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
  1164. goal -= bi->bi_start * GFS2_NBBY;
  1165. goto do_search;
  1166. }
  1167. }
  1168. buf = 0;
  1169. goal = 0;
  1170. do_search:
  1171. /* Search (up to entire) bitmap in this rgrp for allocatable block.
  1172. "x <= length", instead of "x < length", because we typically start
  1173. the search in the middle of a bit block, but if we can't find an
  1174. allocatable block anywhere else, we want to be able wrap around and
  1175. search in the first part of our first-searched bit block. */
  1176. for (x = 0; x <= length; x++) {
  1177. bi = rgd->rd_bits + buf;
  1178. if (test_bit(GBF_FULL, &bi->bi_flags) &&
  1179. (old_state == GFS2_BLKST_FREE))
  1180. goto skip;
  1181. /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
  1182. bitmaps, so we must search the originals for that. */
  1183. buffer = bi->bi_bh->b_data + bi->bi_offset;
  1184. if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
  1185. buffer = bi->bi_clone + bi->bi_offset;
  1186. blk = gfs2_bitfit(buffer, bi->bi_len, goal, old_state);
  1187. if (blk != BFITNOENT)
  1188. break;
  1189. if ((goal == 0) && (old_state == GFS2_BLKST_FREE))
  1190. set_bit(GBF_FULL, &bi->bi_flags);
  1191. /* Try next bitmap block (wrap back to rgrp header if at end) */
  1192. skip:
  1193. buf++;
  1194. buf %= length;
  1195. goal = 0;
  1196. }
  1197. if (blk == BFITNOENT)
  1198. return blk;
  1199. *n = 1;
  1200. if (old_state == new_state)
  1201. goto out;
  1202. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1203. gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
  1204. bi->bi_len, blk, new_state);
  1205. goal = blk;
  1206. while (*n < elen) {
  1207. goal++;
  1208. if (goal >= (bi->bi_len * GFS2_NBBY))
  1209. break;
  1210. if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
  1211. GFS2_BLKST_FREE)
  1212. break;
  1213. gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
  1214. bi->bi_len, goal, new_state);
  1215. (*n)++;
  1216. }
  1217. out:
  1218. return (bi->bi_start * GFS2_NBBY) + blk;
  1219. }
  1220. /**
  1221. * rgblk_free - Change alloc state of given block(s)
  1222. * @sdp: the filesystem
  1223. * @bstart: the start of a run of blocks to free
  1224. * @blen: the length of the block run (all must lie within ONE RG!)
  1225. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1226. *
  1227. * Returns: Resource group containing the block(s)
  1228. */
  1229. static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
  1230. u32 blen, unsigned char new_state)
  1231. {
  1232. struct gfs2_rgrpd *rgd;
  1233. struct gfs2_bitmap *bi = NULL;
  1234. u32 length, rgrp_blk, buf_blk;
  1235. unsigned int buf;
  1236. rgd = gfs2_blk2rgrpd(sdp, bstart);
  1237. if (!rgd) {
  1238. if (gfs2_consist(sdp))
  1239. fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
  1240. return NULL;
  1241. }
  1242. length = rgd->rd_length;
  1243. rgrp_blk = bstart - rgd->rd_data0;
  1244. while (blen--) {
  1245. for (buf = 0; buf < length; buf++) {
  1246. bi = rgd->rd_bits + buf;
  1247. if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1248. break;
  1249. }
  1250. gfs2_assert(rgd->rd_sbd, buf < length);
  1251. buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
  1252. rgrp_blk++;
  1253. if (!bi->bi_clone) {
  1254. bi->bi_clone = kmalloc(bi->bi_bh->b_size,
  1255. GFP_NOFS | __GFP_NOFAIL);
  1256. memcpy(bi->bi_clone + bi->bi_offset,
  1257. bi->bi_bh->b_data + bi->bi_offset,
  1258. bi->bi_len);
  1259. }
  1260. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1261. gfs2_setbit(rgd, bi->bi_bh->b_data, NULL, bi->bi_offset,
  1262. bi->bi_len, buf_blk, new_state);
  1263. }
  1264. return rgd;
  1265. }
  1266. /**
  1267. * gfs2_rgrp_dump - print out an rgrp
  1268. * @seq: The iterator
  1269. * @gl: The glock in question
  1270. *
  1271. */
  1272. int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl)
  1273. {
  1274. const struct gfs2_rgrpd *rgd = gl->gl_object;
  1275. if (rgd == NULL)
  1276. return 0;
  1277. gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u\n",
  1278. (unsigned long long)rgd->rd_addr, rgd->rd_flags,
  1279. rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes);
  1280. return 0;
  1281. }
  1282. static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
  1283. {
  1284. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1285. fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
  1286. (unsigned long long)rgd->rd_addr);
  1287. fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
  1288. gfs2_rgrp_dump(NULL, rgd->rd_gl);
  1289. rgd->rd_flags |= GFS2_RDF_ERROR;
  1290. }
  1291. /**
  1292. * gfs2_alloc_block - Allocate one or more blocks
  1293. * @ip: the inode to allocate the block for
  1294. * @bn: Used to return the starting block number
  1295. * @n: requested number of blocks/extent length (value/result)
  1296. *
  1297. * Returns: 0 or error
  1298. */
  1299. int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n)
  1300. {
  1301. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1302. struct buffer_head *dibh;
  1303. struct gfs2_alloc *al = ip->i_alloc;
  1304. struct gfs2_rgrpd *rgd;
  1305. u32 goal, blk;
  1306. u64 block;
  1307. int error;
  1308. /* Only happens if there is a bug in gfs2, return something distinctive
  1309. * to ensure that it is noticed.
  1310. */
  1311. if (al == NULL)
  1312. return -ECANCELED;
  1313. rgd = al->al_rgd;
  1314. if (rgrp_contains_block(rgd, ip->i_goal))
  1315. goal = ip->i_goal - rgd->rd_data0;
  1316. else
  1317. goal = rgd->rd_last_alloc;
  1318. blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n);
  1319. /* Since all blocks are reserved in advance, this shouldn't happen */
  1320. if (blk == BFITNOENT)
  1321. goto rgrp_error;
  1322. rgd->rd_last_alloc = blk;
  1323. block = rgd->rd_data0 + blk;
  1324. ip->i_goal = block;
  1325. error = gfs2_meta_inode_buffer(ip, &dibh);
  1326. if (error == 0) {
  1327. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  1328. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1329. di->di_goal_meta = di->di_goal_data = cpu_to_be64(ip->i_goal);
  1330. brelse(dibh);
  1331. }
  1332. if (rgd->rd_free < *n)
  1333. goto rgrp_error;
  1334. rgd->rd_free -= *n;
  1335. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1336. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1337. al->al_alloced += *n;
  1338. gfs2_statfs_change(sdp, 0, -(s64)*n, 0);
  1339. gfs2_quota_change(ip, *n, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1340. spin_lock(&sdp->sd_rindex_spin);
  1341. rgd->rd_free_clone -= *n;
  1342. spin_unlock(&sdp->sd_rindex_spin);
  1343. trace_gfs2_block_alloc(ip, block, *n, GFS2_BLKST_USED);
  1344. *bn = block;
  1345. return 0;
  1346. rgrp_error:
  1347. gfs2_rgrp_error(rgd);
  1348. return -EIO;
  1349. }
  1350. /**
  1351. * gfs2_alloc_di - Allocate a dinode
  1352. * @dip: the directory that the inode is going in
  1353. * @bn: the block number which is allocated
  1354. * @generation: the generation number of the inode
  1355. *
  1356. * Returns: 0 on success or error
  1357. */
  1358. int gfs2_alloc_di(struct gfs2_inode *dip, u64 *bn, u64 *generation)
  1359. {
  1360. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1361. struct gfs2_alloc *al = dip->i_alloc;
  1362. struct gfs2_rgrpd *rgd = al->al_rgd;
  1363. u32 blk;
  1364. u64 block;
  1365. unsigned int n = 1;
  1366. blk = rgblk_search(rgd, rgd->rd_last_alloc,
  1367. GFS2_BLKST_FREE, GFS2_BLKST_DINODE, &n);
  1368. /* Since all blocks are reserved in advance, this shouldn't happen */
  1369. if (blk == BFITNOENT)
  1370. goto rgrp_error;
  1371. rgd->rd_last_alloc = blk;
  1372. block = rgd->rd_data0 + blk;
  1373. if (rgd->rd_free == 0)
  1374. goto rgrp_error;
  1375. rgd->rd_free--;
  1376. rgd->rd_dinodes++;
  1377. *generation = rgd->rd_igeneration++;
  1378. if (*generation == 0)
  1379. *generation = rgd->rd_igeneration++;
  1380. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1381. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1382. al->al_alloced++;
  1383. gfs2_statfs_change(sdp, 0, -1, +1);
  1384. gfs2_trans_add_unrevoke(sdp, block, 1);
  1385. spin_lock(&sdp->sd_rindex_spin);
  1386. rgd->rd_free_clone--;
  1387. spin_unlock(&sdp->sd_rindex_spin);
  1388. trace_gfs2_block_alloc(dip, block, 1, GFS2_BLKST_DINODE);
  1389. *bn = block;
  1390. return 0;
  1391. rgrp_error:
  1392. gfs2_rgrp_error(rgd);
  1393. return -EIO;
  1394. }
  1395. /**
  1396. * gfs2_free_data - free a contiguous run of data block(s)
  1397. * @ip: the inode these blocks are being freed from
  1398. * @bstart: first block of a run of contiguous blocks
  1399. * @blen: the length of the block run
  1400. *
  1401. */
  1402. void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
  1403. {
  1404. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1405. struct gfs2_rgrpd *rgd;
  1406. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1407. if (!rgd)
  1408. return;
  1409. trace_gfs2_block_alloc(ip, bstart, blen, GFS2_BLKST_FREE);
  1410. rgd->rd_free += blen;
  1411. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1412. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1413. gfs2_trans_add_rg(rgd);
  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. /**
  1418. * gfs2_free_meta - free a contiguous run of data block(s)
  1419. * @ip: the inode these blocks are being freed from
  1420. * @bstart: first block of a run of contiguous blocks
  1421. * @blen: the length of the block run
  1422. *
  1423. */
  1424. void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
  1425. {
  1426. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1427. struct gfs2_rgrpd *rgd;
  1428. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1429. if (!rgd)
  1430. return;
  1431. trace_gfs2_block_alloc(ip, bstart, blen, GFS2_BLKST_FREE);
  1432. rgd->rd_free += blen;
  1433. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1434. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1435. gfs2_trans_add_rg(rgd);
  1436. gfs2_statfs_change(sdp, 0, +blen, 0);
  1437. gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1438. gfs2_meta_wipe(ip, bstart, blen);
  1439. }
  1440. void gfs2_unlink_di(struct inode *inode)
  1441. {
  1442. struct gfs2_inode *ip = GFS2_I(inode);
  1443. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1444. struct gfs2_rgrpd *rgd;
  1445. u64 blkno = ip->i_no_addr;
  1446. rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
  1447. if (!rgd)
  1448. return;
  1449. trace_gfs2_block_alloc(ip, blkno, 1, GFS2_BLKST_UNLINKED);
  1450. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1451. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1452. gfs2_trans_add_rg(rgd);
  1453. }
  1454. static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
  1455. {
  1456. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1457. struct gfs2_rgrpd *tmp_rgd;
  1458. tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
  1459. if (!tmp_rgd)
  1460. return;
  1461. gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
  1462. if (!rgd->rd_dinodes)
  1463. gfs2_consist_rgrpd(rgd);
  1464. rgd->rd_dinodes--;
  1465. rgd->rd_free++;
  1466. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1467. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1468. gfs2_statfs_change(sdp, 0, +1, -1);
  1469. gfs2_trans_add_rg(rgd);
  1470. }
  1471. void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
  1472. {
  1473. gfs2_free_uninit_di(rgd, ip->i_no_addr);
  1474. trace_gfs2_block_alloc(ip, ip->i_no_addr, 1, GFS2_BLKST_FREE);
  1475. gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1476. gfs2_meta_wipe(ip, ip->i_no_addr, 1);
  1477. }
  1478. /**
  1479. * gfs2_check_blk_type - Check the type of a block
  1480. * @sdp: The superblock
  1481. * @no_addr: The block number to check
  1482. * @type: The block type we are looking for
  1483. *
  1484. * Returns: 0 if the block type matches the expected type
  1485. * -ESTALE if it doesn't match
  1486. * or -ve errno if something went wrong while checking
  1487. */
  1488. int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
  1489. {
  1490. struct gfs2_rgrpd *rgd;
  1491. struct gfs2_holder ri_gh, rgd_gh;
  1492. struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
  1493. int ri_locked = 0;
  1494. int error;
  1495. if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
  1496. error = gfs2_rindex_hold(sdp, &ri_gh);
  1497. if (error)
  1498. goto fail;
  1499. ri_locked = 1;
  1500. }
  1501. error = -EINVAL;
  1502. rgd = gfs2_blk2rgrpd(sdp, no_addr);
  1503. if (!rgd)
  1504. goto fail_rindex;
  1505. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
  1506. if (error)
  1507. goto fail_rindex;
  1508. if (gfs2_get_block_type(rgd, no_addr) != type)
  1509. error = -ESTALE;
  1510. gfs2_glock_dq_uninit(&rgd_gh);
  1511. fail_rindex:
  1512. if (ri_locked)
  1513. gfs2_glock_dq_uninit(&ri_gh);
  1514. fail:
  1515. return error;
  1516. }
  1517. /**
  1518. * gfs2_rlist_add - add a RG to a list of RGs
  1519. * @sdp: the filesystem
  1520. * @rlist: the list of resource groups
  1521. * @block: the block
  1522. *
  1523. * Figure out what RG a block belongs to and add that RG to the list
  1524. *
  1525. * FIXME: Don't use NOFAIL
  1526. *
  1527. */
  1528. void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
  1529. u64 block)
  1530. {
  1531. struct gfs2_rgrpd *rgd;
  1532. struct gfs2_rgrpd **tmp;
  1533. unsigned int new_space;
  1534. unsigned int x;
  1535. if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
  1536. return;
  1537. rgd = gfs2_blk2rgrpd(sdp, block);
  1538. if (!rgd) {
  1539. if (gfs2_consist(sdp))
  1540. fs_err(sdp, "block = %llu\n", (unsigned long long)block);
  1541. return;
  1542. }
  1543. for (x = 0; x < rlist->rl_rgrps; x++)
  1544. if (rlist->rl_rgd[x] == rgd)
  1545. return;
  1546. if (rlist->rl_rgrps == rlist->rl_space) {
  1547. new_space = rlist->rl_space + 10;
  1548. tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
  1549. GFP_NOFS | __GFP_NOFAIL);
  1550. if (rlist->rl_rgd) {
  1551. memcpy(tmp, rlist->rl_rgd,
  1552. rlist->rl_space * sizeof(struct gfs2_rgrpd *));
  1553. kfree(rlist->rl_rgd);
  1554. }
  1555. rlist->rl_space = new_space;
  1556. rlist->rl_rgd = tmp;
  1557. }
  1558. rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
  1559. }
  1560. /**
  1561. * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
  1562. * and initialize an array of glock holders for them
  1563. * @rlist: the list of resource groups
  1564. * @state: the lock state to acquire the RG lock in
  1565. * @flags: the modifier flags for the holder structures
  1566. *
  1567. * FIXME: Don't use NOFAIL
  1568. *
  1569. */
  1570. void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
  1571. {
  1572. unsigned int x;
  1573. rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
  1574. GFP_NOFS | __GFP_NOFAIL);
  1575. for (x = 0; x < rlist->rl_rgrps; x++)
  1576. gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
  1577. state, 0,
  1578. &rlist->rl_ghs[x]);
  1579. }
  1580. /**
  1581. * gfs2_rlist_free - free a resource group list
  1582. * @list: the list of resource groups
  1583. *
  1584. */
  1585. void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
  1586. {
  1587. unsigned int x;
  1588. kfree(rlist->rl_rgd);
  1589. if (rlist->rl_ghs) {
  1590. for (x = 0; x < rlist->rl_rgrps; x++)
  1591. gfs2_holder_uninit(&rlist->rl_ghs[x]);
  1592. kfree(rlist->rl_ghs);
  1593. }
  1594. }