rgrp.c 41 KB

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