rgrp.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722
  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 "ops_address.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 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 = fls64(tmp);
  190. bit--; /* fls64 always adds one to the bit count */
  191. bit /= 2; /* two bits per entry in the bitmap */
  192. return (((const unsigned char *)ptr - buf) * GFS2_NBBY) + bit;
  193. }
  194. /**
  195. * gfs2_bitcount - count the number of bits in a certain state
  196. * @buffer: the buffer that holds the bitmaps
  197. * @buflen: the length (in bytes) of the buffer
  198. * @state: the state of the block we're looking for
  199. *
  200. * Returns: The number of bits
  201. */
  202. static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
  203. unsigned int buflen, u8 state)
  204. {
  205. const u8 *byte = buffer;
  206. const u8 *end = buffer + buflen;
  207. const u8 state1 = state << 2;
  208. const u8 state2 = state << 4;
  209. const u8 state3 = state << 6;
  210. u32 count = 0;
  211. for (; byte < end; byte++) {
  212. if (((*byte) & 0x03) == state)
  213. count++;
  214. if (((*byte) & 0x0C) == state1)
  215. count++;
  216. if (((*byte) & 0x30) == state2)
  217. count++;
  218. if (((*byte) & 0xC0) == state3)
  219. count++;
  220. }
  221. return count;
  222. }
  223. /**
  224. * gfs2_rgrp_verify - Verify that a resource group is consistent
  225. * @sdp: the filesystem
  226. * @rgd: the rgrp
  227. *
  228. */
  229. void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
  230. {
  231. struct gfs2_sbd *sdp = rgd->rd_sbd;
  232. struct gfs2_bitmap *bi = NULL;
  233. u32 length = rgd->rd_length;
  234. u32 count[4], tmp;
  235. int buf, x;
  236. memset(count, 0, 4 * sizeof(u32));
  237. /* Count # blocks in each of 4 possible allocation states */
  238. for (buf = 0; buf < length; buf++) {
  239. bi = rgd->rd_bits + buf;
  240. for (x = 0; x < 4; x++)
  241. count[x] += gfs2_bitcount(rgd,
  242. bi->bi_bh->b_data +
  243. bi->bi_offset,
  244. bi->bi_len, x);
  245. }
  246. if (count[0] != rgd->rd_free) {
  247. if (gfs2_consist_rgrpd(rgd))
  248. fs_err(sdp, "free data mismatch: %u != %u\n",
  249. count[0], rgd->rd_free);
  250. return;
  251. }
  252. tmp = rgd->rd_data - rgd->rd_free - rgd->rd_dinodes;
  253. if (count[1] + count[2] != tmp) {
  254. if (gfs2_consist_rgrpd(rgd))
  255. fs_err(sdp, "used data mismatch: %u != %u\n",
  256. count[1], tmp);
  257. return;
  258. }
  259. if (count[3] != rgd->rd_dinodes) {
  260. if (gfs2_consist_rgrpd(rgd))
  261. fs_err(sdp, "used metadata mismatch: %u != %u\n",
  262. count[3], rgd->rd_dinodes);
  263. return;
  264. }
  265. if (count[2] > count[3]) {
  266. if (gfs2_consist_rgrpd(rgd))
  267. fs_err(sdp, "unlinked inodes > inodes: %u\n",
  268. count[2]);
  269. return;
  270. }
  271. }
  272. static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
  273. {
  274. u64 first = rgd->rd_data0;
  275. u64 last = first + rgd->rd_data;
  276. return first <= block && block < last;
  277. }
  278. /**
  279. * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
  280. * @sdp: The GFS2 superblock
  281. * @n: The data block number
  282. *
  283. * Returns: The resource group, or NULL if not found
  284. */
  285. struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
  286. {
  287. struct gfs2_rgrpd *rgd;
  288. spin_lock(&sdp->sd_rindex_spin);
  289. list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
  290. if (rgrp_contains_block(rgd, blk)) {
  291. list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
  292. spin_unlock(&sdp->sd_rindex_spin);
  293. return rgd;
  294. }
  295. }
  296. spin_unlock(&sdp->sd_rindex_spin);
  297. return NULL;
  298. }
  299. /**
  300. * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
  301. * @sdp: The GFS2 superblock
  302. *
  303. * Returns: The first rgrp in the filesystem
  304. */
  305. struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
  306. {
  307. gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
  308. return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
  309. }
  310. /**
  311. * gfs2_rgrpd_get_next - get the next RG
  312. * @rgd: A RG
  313. *
  314. * Returns: The next rgrp
  315. */
  316. struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
  317. {
  318. if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
  319. return NULL;
  320. return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
  321. }
  322. static void clear_rgrpdi(struct gfs2_sbd *sdp)
  323. {
  324. struct list_head *head;
  325. struct gfs2_rgrpd *rgd;
  326. struct gfs2_glock *gl;
  327. spin_lock(&sdp->sd_rindex_spin);
  328. sdp->sd_rindex_forward = NULL;
  329. spin_unlock(&sdp->sd_rindex_spin);
  330. head = &sdp->sd_rindex_list;
  331. while (!list_empty(head)) {
  332. rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
  333. gl = rgd->rd_gl;
  334. list_del(&rgd->rd_list);
  335. list_del(&rgd->rd_list_mru);
  336. if (gl) {
  337. gl->gl_object = NULL;
  338. gfs2_glock_put(gl);
  339. }
  340. kfree(rgd->rd_bits);
  341. kmem_cache_free(gfs2_rgrpd_cachep, rgd);
  342. }
  343. }
  344. void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
  345. {
  346. mutex_lock(&sdp->sd_rindex_mutex);
  347. clear_rgrpdi(sdp);
  348. mutex_unlock(&sdp->sd_rindex_mutex);
  349. }
  350. static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
  351. {
  352. printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
  353. printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
  354. printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
  355. printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
  356. printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
  357. }
  358. /**
  359. * gfs2_compute_bitstructs - Compute the bitmap sizes
  360. * @rgd: The resource group descriptor
  361. *
  362. * Calculates bitmap descriptors, one for each block that contains bitmap data
  363. *
  364. * Returns: errno
  365. */
  366. static int compute_bitstructs(struct gfs2_rgrpd *rgd)
  367. {
  368. struct gfs2_sbd *sdp = rgd->rd_sbd;
  369. struct gfs2_bitmap *bi;
  370. u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
  371. u32 bytes_left, bytes;
  372. int x;
  373. if (!length)
  374. return -EINVAL;
  375. rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
  376. if (!rgd->rd_bits)
  377. return -ENOMEM;
  378. bytes_left = rgd->rd_bitbytes;
  379. for (x = 0; x < length; x++) {
  380. bi = rgd->rd_bits + x;
  381. /* small rgrp; bitmap stored completely in header block */
  382. if (length == 1) {
  383. bytes = bytes_left;
  384. bi->bi_offset = sizeof(struct gfs2_rgrp);
  385. bi->bi_start = 0;
  386. bi->bi_len = bytes;
  387. /* header block */
  388. } else if (x == 0) {
  389. bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
  390. bi->bi_offset = sizeof(struct gfs2_rgrp);
  391. bi->bi_start = 0;
  392. bi->bi_len = bytes;
  393. /* last block */
  394. } else if (x + 1 == length) {
  395. bytes = bytes_left;
  396. bi->bi_offset = sizeof(struct gfs2_meta_header);
  397. bi->bi_start = rgd->rd_bitbytes - bytes_left;
  398. bi->bi_len = bytes;
  399. /* other blocks */
  400. } else {
  401. bytes = sdp->sd_sb.sb_bsize -
  402. sizeof(struct gfs2_meta_header);
  403. bi->bi_offset = sizeof(struct gfs2_meta_header);
  404. bi->bi_start = rgd->rd_bitbytes - bytes_left;
  405. bi->bi_len = bytes;
  406. }
  407. bytes_left -= bytes;
  408. }
  409. if (bytes_left) {
  410. gfs2_consist_rgrpd(rgd);
  411. return -EIO;
  412. }
  413. bi = rgd->rd_bits + (length - 1);
  414. if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
  415. if (gfs2_consist_rgrpd(rgd)) {
  416. gfs2_rindex_print(rgd);
  417. fs_err(sdp, "start=%u len=%u offset=%u\n",
  418. bi->bi_start, bi->bi_len, bi->bi_offset);
  419. }
  420. return -EIO;
  421. }
  422. return 0;
  423. }
  424. /**
  425. * gfs2_ri_total - Total up the file system space, according to the rindex.
  426. *
  427. */
  428. u64 gfs2_ri_total(struct gfs2_sbd *sdp)
  429. {
  430. u64 total_data = 0;
  431. struct inode *inode = sdp->sd_rindex;
  432. struct gfs2_inode *ip = GFS2_I(inode);
  433. char buf[sizeof(struct gfs2_rindex)];
  434. struct file_ra_state ra_state;
  435. int error, rgrps;
  436. mutex_lock(&sdp->sd_rindex_mutex);
  437. file_ra_state_init(&ra_state, inode->i_mapping);
  438. for (rgrps = 0;; rgrps++) {
  439. loff_t pos = rgrps * sizeof(struct gfs2_rindex);
  440. if (pos + sizeof(struct gfs2_rindex) >= ip->i_disksize)
  441. break;
  442. error = gfs2_internal_read(ip, &ra_state, buf, &pos,
  443. sizeof(struct gfs2_rindex));
  444. if (error != sizeof(struct gfs2_rindex))
  445. break;
  446. total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
  447. }
  448. mutex_unlock(&sdp->sd_rindex_mutex);
  449. return total_data;
  450. }
  451. static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
  452. {
  453. const struct gfs2_rindex *str = buf;
  454. rgd->rd_addr = be64_to_cpu(str->ri_addr);
  455. rgd->rd_length = be32_to_cpu(str->ri_length);
  456. rgd->rd_data0 = be64_to_cpu(str->ri_data0);
  457. rgd->rd_data = be32_to_cpu(str->ri_data);
  458. rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
  459. }
  460. /**
  461. * read_rindex_entry - Pull in a new resource index entry from the disk
  462. * @gl: The glock covering the rindex inode
  463. *
  464. * Returns: 0 on success, error code otherwise
  465. */
  466. static int read_rindex_entry(struct gfs2_inode *ip,
  467. struct file_ra_state *ra_state)
  468. {
  469. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  470. loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
  471. char buf[sizeof(struct gfs2_rindex)];
  472. int error;
  473. struct gfs2_rgrpd *rgd;
  474. error = gfs2_internal_read(ip, ra_state, buf, &pos,
  475. sizeof(struct gfs2_rindex));
  476. if (!error)
  477. return 0;
  478. if (error != sizeof(struct gfs2_rindex)) {
  479. if (error > 0)
  480. error = -EIO;
  481. return error;
  482. }
  483. rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
  484. error = -ENOMEM;
  485. if (!rgd)
  486. return error;
  487. mutex_init(&rgd->rd_mutex);
  488. lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
  489. rgd->rd_sbd = sdp;
  490. list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
  491. list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
  492. gfs2_rindex_in(rgd, buf);
  493. error = compute_bitstructs(rgd);
  494. if (error)
  495. return error;
  496. error = gfs2_glock_get(sdp, rgd->rd_addr,
  497. &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
  498. if (error)
  499. return error;
  500. rgd->rd_gl->gl_object = rgd;
  501. rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
  502. rgd->rd_flags |= GFS2_RDF_CHECK;
  503. return error;
  504. }
  505. /**
  506. * gfs2_ri_update - Pull in a new resource index from the disk
  507. * @ip: pointer to the rindex inode
  508. *
  509. * Returns: 0 on successful update, error code otherwise
  510. */
  511. static int gfs2_ri_update(struct gfs2_inode *ip)
  512. {
  513. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  514. struct inode *inode = &ip->i_inode;
  515. struct file_ra_state ra_state;
  516. u64 rgrp_count = ip->i_disksize;
  517. int error;
  518. if (do_div(rgrp_count, sizeof(struct gfs2_rindex))) {
  519. gfs2_consist_inode(ip);
  520. return -EIO;
  521. }
  522. clear_rgrpdi(sdp);
  523. file_ra_state_init(&ra_state, inode->i_mapping);
  524. for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
  525. error = read_rindex_entry(ip, &ra_state);
  526. if (error) {
  527. clear_rgrpdi(sdp);
  528. return error;
  529. }
  530. }
  531. sdp->sd_rindex_uptodate = 1;
  532. return 0;
  533. }
  534. /**
  535. * gfs2_ri_update_special - Pull in a new resource index from the disk
  536. *
  537. * This is a special version that's safe to call from gfs2_inplace_reserve_i.
  538. * In this case we know that we don't have any resource groups in memory yet.
  539. *
  540. * @ip: pointer to the rindex inode
  541. *
  542. * Returns: 0 on successful update, error code otherwise
  543. */
  544. static int gfs2_ri_update_special(struct gfs2_inode *ip)
  545. {
  546. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  547. struct inode *inode = &ip->i_inode;
  548. struct file_ra_state ra_state;
  549. int error;
  550. file_ra_state_init(&ra_state, inode->i_mapping);
  551. for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
  552. /* Ignore partials */
  553. if ((sdp->sd_rgrps + 1) * sizeof(struct gfs2_rindex) >
  554. ip->i_disksize)
  555. break;
  556. error = read_rindex_entry(ip, &ra_state);
  557. if (error) {
  558. clear_rgrpdi(sdp);
  559. return error;
  560. }
  561. }
  562. sdp->sd_rindex_uptodate = 1;
  563. return 0;
  564. }
  565. /**
  566. * gfs2_rindex_hold - Grab a lock on the rindex
  567. * @sdp: The GFS2 superblock
  568. * @ri_gh: the glock holder
  569. *
  570. * We grab a lock on the rindex inode to make sure that it doesn't
  571. * change whilst we are performing an operation. We keep this lock
  572. * for quite long periods of time compared to other locks. This
  573. * doesn't matter, since it is shared and it is very, very rarely
  574. * accessed in the exclusive mode (i.e. only when expanding the filesystem).
  575. *
  576. * This makes sure that we're using the latest copy of the resource index
  577. * special file, which might have been updated if someone expanded the
  578. * filesystem (via gfs2_grow utility), which adds new resource groups.
  579. *
  580. * Returns: 0 on success, error code otherwise
  581. */
  582. int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
  583. {
  584. struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
  585. struct gfs2_glock *gl = ip->i_gl;
  586. int error;
  587. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
  588. if (error)
  589. return error;
  590. /* Read new copy from disk if we don't have the latest */
  591. if (!sdp->sd_rindex_uptodate) {
  592. mutex_lock(&sdp->sd_rindex_mutex);
  593. if (!sdp->sd_rindex_uptodate) {
  594. error = gfs2_ri_update(ip);
  595. if (error)
  596. gfs2_glock_dq_uninit(ri_gh);
  597. }
  598. mutex_unlock(&sdp->sd_rindex_mutex);
  599. }
  600. return error;
  601. }
  602. static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
  603. {
  604. const struct gfs2_rgrp *str = buf;
  605. u32 rg_flags;
  606. rg_flags = be32_to_cpu(str->rg_flags);
  607. if (rg_flags & GFS2_RGF_NOALLOC)
  608. rgd->rd_flags |= GFS2_RDF_NOALLOC;
  609. else
  610. rgd->rd_flags &= ~GFS2_RDF_NOALLOC;
  611. rgd->rd_free = be32_to_cpu(str->rg_free);
  612. rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
  613. rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
  614. }
  615. static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
  616. {
  617. struct gfs2_rgrp *str = buf;
  618. u32 rg_flags = 0;
  619. if (rgd->rd_flags & GFS2_RDF_NOALLOC)
  620. rg_flags |= GFS2_RGF_NOALLOC;
  621. str->rg_flags = cpu_to_be32(rg_flags);
  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_bh_get - 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_bh_get(struct gfs2_rgrpd *rgd)
  638. {
  639. struct gfs2_sbd *sdp = rgd->rd_sbd;
  640. struct gfs2_glock *gl = rgd->rd_gl;
  641. unsigned int length = rgd->rd_length;
  642. struct gfs2_bitmap *bi;
  643. unsigned int x, y;
  644. int error;
  645. mutex_lock(&rgd->rd_mutex);
  646. spin_lock(&sdp->sd_rindex_spin);
  647. if (rgd->rd_bh_count) {
  648. rgd->rd_bh_count++;
  649. spin_unlock(&sdp->sd_rindex_spin);
  650. mutex_unlock(&rgd->rd_mutex);
  651. return 0;
  652. }
  653. spin_unlock(&sdp->sd_rindex_spin);
  654. for (x = 0; x < length; x++) {
  655. bi = rgd->rd_bits + x;
  656. error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
  657. if (error)
  658. goto fail;
  659. }
  660. for (y = length; y--;) {
  661. bi = rgd->rd_bits + y;
  662. error = gfs2_meta_wait(sdp, bi->bi_bh);
  663. if (error)
  664. goto fail;
  665. if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
  666. GFS2_METATYPE_RG)) {
  667. error = -EIO;
  668. goto fail;
  669. }
  670. }
  671. if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
  672. gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
  673. rgd->rd_flags |= GFS2_RDF_UPTODATE;
  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_hardsect_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. if (rv)
  752. goto fail;
  753. nr_sects = 0;
  754. start_new_extent:
  755. start = blk;
  756. }
  757. nr_sects += sects_per_blk;
  758. }
  759. diff >>= 2;
  760. blk += sects_per_blk;
  761. }
  762. }
  763. if (nr_sects) {
  764. rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS);
  765. if (rv)
  766. goto fail;
  767. }
  768. return;
  769. fail:
  770. fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem", rv);
  771. sdp->sd_args.ar_discard = 0;
  772. }
  773. void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
  774. {
  775. struct gfs2_sbd *sdp = rgd->rd_sbd;
  776. unsigned int length = rgd->rd_length;
  777. unsigned int x;
  778. for (x = 0; x < length; x++) {
  779. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  780. if (!bi->bi_clone)
  781. continue;
  782. if (sdp->sd_args.ar_discard)
  783. gfs2_rgrp_send_discards(sdp, rgd->rd_data0, bi);
  784. memcpy(bi->bi_clone + bi->bi_offset,
  785. bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
  786. }
  787. spin_lock(&sdp->sd_rindex_spin);
  788. rgd->rd_free_clone = rgd->rd_free;
  789. spin_unlock(&sdp->sd_rindex_spin);
  790. }
  791. /**
  792. * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
  793. * @ip: the incore GFS2 inode structure
  794. *
  795. * Returns: the struct gfs2_alloc
  796. */
  797. struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
  798. {
  799. BUG_ON(ip->i_alloc != NULL);
  800. ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_KERNEL);
  801. return ip->i_alloc;
  802. }
  803. /**
  804. * try_rgrp_fit - See if a given reservation will fit in a given RG
  805. * @rgd: the RG data
  806. * @al: the struct gfs2_alloc structure describing the reservation
  807. *
  808. * If there's room for the requested blocks to be allocated from the RG:
  809. * Sets the $al_rgd field in @al.
  810. *
  811. * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
  812. */
  813. static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
  814. {
  815. struct gfs2_sbd *sdp = rgd->rd_sbd;
  816. int ret = 0;
  817. if (rgd->rd_flags & GFS2_RDF_NOALLOC)
  818. return 0;
  819. spin_lock(&sdp->sd_rindex_spin);
  820. if (rgd->rd_free_clone >= al->al_requested) {
  821. al->al_rgd = rgd;
  822. ret = 1;
  823. }
  824. spin_unlock(&sdp->sd_rindex_spin);
  825. return ret;
  826. }
  827. /**
  828. * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
  829. * @rgd: The rgrp
  830. *
  831. * Returns: The inode, if one has been found
  832. */
  833. static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
  834. {
  835. struct inode *inode;
  836. u32 goal = 0, block;
  837. u64 no_addr;
  838. struct gfs2_sbd *sdp = rgd->rd_sbd;
  839. unsigned int n;
  840. for(;;) {
  841. if (goal >= rgd->rd_data)
  842. break;
  843. down_write(&sdp->sd_log_flush_lock);
  844. n = 1;
  845. block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
  846. GFS2_BLKST_UNLINKED, &n);
  847. up_write(&sdp->sd_log_flush_lock);
  848. if (block == BFITNOENT)
  849. break;
  850. /* rgblk_search can return a block < goal, so we need to
  851. keep it marching forward. */
  852. no_addr = block + rgd->rd_data0;
  853. goal++;
  854. if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
  855. continue;
  856. *last_unlinked = no_addr;
  857. inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, DT_UNKNOWN,
  858. no_addr, -1, 1);
  859. if (!IS_ERR(inode))
  860. return inode;
  861. }
  862. rgd->rd_flags &= ~GFS2_RDF_CHECK;
  863. return NULL;
  864. }
  865. /**
  866. * recent_rgrp_next - get next RG from "recent" list
  867. * @cur_rgd: current rgrp
  868. *
  869. * Returns: The next rgrp in the recent list
  870. */
  871. static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd)
  872. {
  873. struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
  874. struct list_head *head;
  875. struct gfs2_rgrpd *rgd;
  876. spin_lock(&sdp->sd_rindex_spin);
  877. head = &sdp->sd_rindex_mru_list;
  878. if (unlikely(cur_rgd->rd_list_mru.next == head)) {
  879. spin_unlock(&sdp->sd_rindex_spin);
  880. return NULL;
  881. }
  882. rgd = list_entry(cur_rgd->rd_list_mru.next, struct gfs2_rgrpd, rd_list_mru);
  883. spin_unlock(&sdp->sd_rindex_spin);
  884. return rgd;
  885. }
  886. /**
  887. * forward_rgrp_get - get an rgrp to try next from full list
  888. * @sdp: The GFS2 superblock
  889. *
  890. * Returns: The rgrp to try next
  891. */
  892. static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
  893. {
  894. struct gfs2_rgrpd *rgd;
  895. unsigned int journals = gfs2_jindex_size(sdp);
  896. unsigned int rg = 0, x;
  897. spin_lock(&sdp->sd_rindex_spin);
  898. rgd = sdp->sd_rindex_forward;
  899. if (!rgd) {
  900. if (sdp->sd_rgrps >= journals)
  901. rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
  902. for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
  903. x++, rgd = gfs2_rgrpd_get_next(rgd))
  904. /* Do Nothing */;
  905. sdp->sd_rindex_forward = rgd;
  906. }
  907. spin_unlock(&sdp->sd_rindex_spin);
  908. return rgd;
  909. }
  910. /**
  911. * forward_rgrp_set - set the forward rgrp pointer
  912. * @sdp: the filesystem
  913. * @rgd: The new forward rgrp
  914. *
  915. */
  916. static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
  917. {
  918. spin_lock(&sdp->sd_rindex_spin);
  919. sdp->sd_rindex_forward = rgd;
  920. spin_unlock(&sdp->sd_rindex_spin);
  921. }
  922. /**
  923. * get_local_rgrp - Choose and lock a rgrp for allocation
  924. * @ip: the inode to reserve space for
  925. * @rgp: the chosen and locked rgrp
  926. *
  927. * Try to acquire rgrp in way which avoids contending with others.
  928. *
  929. * Returns: errno
  930. */
  931. static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
  932. {
  933. struct inode *inode = NULL;
  934. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  935. struct gfs2_rgrpd *rgd, *begin = NULL;
  936. struct gfs2_alloc *al = ip->i_alloc;
  937. int flags = LM_FLAG_TRY;
  938. int skipped = 0;
  939. int loops = 0;
  940. int error, rg_locked;
  941. rgd = gfs2_blk2rgrpd(sdp, ip->i_goal);
  942. while (rgd) {
  943. rg_locked = 0;
  944. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  945. rg_locked = 1;
  946. error = 0;
  947. } else {
  948. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
  949. LM_FLAG_TRY, &al->al_rgd_gh);
  950. }
  951. switch (error) {
  952. case 0:
  953. if (try_rgrp_fit(rgd, al))
  954. goto out;
  955. if (rgd->rd_flags & GFS2_RDF_CHECK)
  956. inode = try_rgrp_unlink(rgd, last_unlinked);
  957. if (!rg_locked)
  958. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  959. if (inode)
  960. return inode;
  961. /* fall through */
  962. case GLR_TRYFAILED:
  963. rgd = recent_rgrp_next(rgd);
  964. break;
  965. default:
  966. return ERR_PTR(error);
  967. }
  968. }
  969. /* Go through full list of rgrps */
  970. begin = rgd = forward_rgrp_get(sdp);
  971. for (;;) {
  972. rg_locked = 0;
  973. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  974. rg_locked = 1;
  975. error = 0;
  976. } else {
  977. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
  978. &al->al_rgd_gh);
  979. }
  980. switch (error) {
  981. case 0:
  982. if (try_rgrp_fit(rgd, al))
  983. goto out;
  984. if (rgd->rd_flags & GFS2_RDF_CHECK)
  985. inode = try_rgrp_unlink(rgd, last_unlinked);
  986. if (!rg_locked)
  987. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  988. if (inode)
  989. return inode;
  990. break;
  991. case GLR_TRYFAILED:
  992. skipped++;
  993. break;
  994. default:
  995. return ERR_PTR(error);
  996. }
  997. rgd = gfs2_rgrpd_get_next(rgd);
  998. if (!rgd)
  999. rgd = gfs2_rgrpd_get_first(sdp);
  1000. if (rgd == begin) {
  1001. if (++loops >= 3)
  1002. return ERR_PTR(-ENOSPC);
  1003. if (!skipped)
  1004. loops++;
  1005. flags = 0;
  1006. if (loops == 2)
  1007. gfs2_log_flush(sdp, NULL);
  1008. }
  1009. }
  1010. out:
  1011. if (begin) {
  1012. spin_lock(&sdp->sd_rindex_spin);
  1013. list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
  1014. spin_unlock(&sdp->sd_rindex_spin);
  1015. rgd = gfs2_rgrpd_get_next(rgd);
  1016. if (!rgd)
  1017. rgd = gfs2_rgrpd_get_first(sdp);
  1018. forward_rgrp_set(sdp, rgd);
  1019. }
  1020. return NULL;
  1021. }
  1022. /**
  1023. * gfs2_inplace_reserve_i - Reserve space in the filesystem
  1024. * @ip: the inode to reserve space for
  1025. *
  1026. * Returns: errno
  1027. */
  1028. int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
  1029. {
  1030. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1031. struct gfs2_alloc *al = ip->i_alloc;
  1032. struct inode *inode;
  1033. int error = 0;
  1034. u64 last_unlinked = NO_BLOCK;
  1035. if (gfs2_assert_warn(sdp, al->al_requested))
  1036. return -EINVAL;
  1037. try_again:
  1038. /* We need to hold the rindex unless the inode we're using is
  1039. the rindex itself, in which case it's already held. */
  1040. if (ip != GFS2_I(sdp->sd_rindex))
  1041. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  1042. else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
  1043. error = gfs2_ri_update_special(ip);
  1044. if (error)
  1045. return error;
  1046. inode = get_local_rgrp(ip, &last_unlinked);
  1047. if (inode) {
  1048. if (ip != GFS2_I(sdp->sd_rindex))
  1049. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1050. if (IS_ERR(inode))
  1051. return PTR_ERR(inode);
  1052. iput(inode);
  1053. gfs2_log_flush(sdp, NULL);
  1054. goto try_again;
  1055. }
  1056. al->al_file = file;
  1057. al->al_line = line;
  1058. return 0;
  1059. }
  1060. /**
  1061. * gfs2_inplace_release - release an inplace reservation
  1062. * @ip: the inode the reservation was taken out on
  1063. *
  1064. * Release a reservation made by gfs2_inplace_reserve().
  1065. */
  1066. void gfs2_inplace_release(struct gfs2_inode *ip)
  1067. {
  1068. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1069. struct gfs2_alloc *al = ip->i_alloc;
  1070. if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
  1071. fs_warn(sdp, "al_alloced = %u, al_requested = %u "
  1072. "al_file = %s, al_line = %u\n",
  1073. al->al_alloced, al->al_requested, al->al_file,
  1074. al->al_line);
  1075. al->al_rgd = NULL;
  1076. if (al->al_rgd_gh.gh_gl)
  1077. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  1078. if (ip != GFS2_I(sdp->sd_rindex))
  1079. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1080. }
  1081. /**
  1082. * gfs2_get_block_type - Check a block in a RG is of given type
  1083. * @rgd: the resource group holding the block
  1084. * @block: the block number
  1085. *
  1086. * Returns: The block type (GFS2_BLKST_*)
  1087. */
  1088. unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
  1089. {
  1090. struct gfs2_bitmap *bi = NULL;
  1091. u32 length, rgrp_block, buf_block;
  1092. unsigned int buf;
  1093. unsigned char type;
  1094. length = rgd->rd_length;
  1095. rgrp_block = block - rgd->rd_data0;
  1096. for (buf = 0; buf < length; buf++) {
  1097. bi = rgd->rd_bits + buf;
  1098. if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1099. break;
  1100. }
  1101. gfs2_assert(rgd->rd_sbd, buf < length);
  1102. buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
  1103. type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
  1104. bi->bi_len, buf_block);
  1105. return type;
  1106. }
  1107. /**
  1108. * rgblk_search - find a block in @old_state, change allocation
  1109. * state to @new_state
  1110. * @rgd: the resource group descriptor
  1111. * @goal: the goal block within the RG (start here to search for avail block)
  1112. * @old_state: GFS2_BLKST_XXX the before-allocation state to find
  1113. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1114. * @n: The extent length
  1115. *
  1116. * Walk rgrp's bitmap to find bits that represent a block in @old_state.
  1117. * Add the found bitmap buffer to the transaction.
  1118. * Set the found bits to @new_state to change block's allocation state.
  1119. *
  1120. * This function never fails, because we wouldn't call it unless we
  1121. * know (from reservation results, etc.) that a block is available.
  1122. *
  1123. * Scope of @goal and returned block is just within rgrp, not the whole
  1124. * filesystem.
  1125. *
  1126. * Returns: the block number allocated
  1127. */
  1128. static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
  1129. unsigned char old_state, unsigned char new_state,
  1130. unsigned int *n)
  1131. {
  1132. struct gfs2_bitmap *bi = NULL;
  1133. const u32 length = rgd->rd_length;
  1134. u32 blk = 0;
  1135. unsigned int buf, x;
  1136. const unsigned int elen = *n;
  1137. const u8 *buffer;
  1138. *n = 0;
  1139. /* Find bitmap block that contains bits for goal block */
  1140. for (buf = 0; buf < length; buf++) {
  1141. bi = rgd->rd_bits + buf;
  1142. if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1143. break;
  1144. }
  1145. gfs2_assert(rgd->rd_sbd, buf < length);
  1146. /* Convert scope of "goal" from rgrp-wide to within found bit block */
  1147. goal -= bi->bi_start * GFS2_NBBY;
  1148. /* Search (up to entire) bitmap in this rgrp for allocatable block.
  1149. "x <= length", instead of "x < length", because we typically start
  1150. the search in the middle of a bit block, but if we can't find an
  1151. allocatable block anywhere else, we want to be able wrap around and
  1152. search in the first part of our first-searched bit block. */
  1153. for (x = 0; x <= length; x++) {
  1154. /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
  1155. bitmaps, so we must search the originals for that. */
  1156. buffer = bi->bi_bh->b_data + bi->bi_offset;
  1157. if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
  1158. buffer = bi->bi_clone + bi->bi_offset;
  1159. blk = gfs2_bitfit(buffer, bi->bi_len, goal, old_state);
  1160. if (blk != BFITNOENT)
  1161. break;
  1162. /* Try next bitmap block (wrap back to rgrp header if at end) */
  1163. buf = (buf + 1) % length;
  1164. bi = rgd->rd_bits + buf;
  1165. goal = 0;
  1166. }
  1167. if (blk != BFITNOENT && old_state != new_state) {
  1168. *n = 1;
  1169. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1170. gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
  1171. bi->bi_len, blk, new_state);
  1172. goal = blk;
  1173. while (*n < elen) {
  1174. goal++;
  1175. if (goal >= (bi->bi_len * GFS2_NBBY))
  1176. break;
  1177. if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
  1178. GFS2_BLKST_FREE)
  1179. break;
  1180. gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone,
  1181. bi->bi_offset, bi->bi_len, goal,
  1182. new_state);
  1183. (*n)++;
  1184. }
  1185. }
  1186. return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
  1187. }
  1188. /**
  1189. * rgblk_free - Change alloc state of given block(s)
  1190. * @sdp: the filesystem
  1191. * @bstart: the start of a run of blocks to free
  1192. * @blen: the length of the block run (all must lie within ONE RG!)
  1193. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1194. *
  1195. * Returns: Resource group containing the block(s)
  1196. */
  1197. static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
  1198. u32 blen, unsigned char new_state)
  1199. {
  1200. struct gfs2_rgrpd *rgd;
  1201. struct gfs2_bitmap *bi = NULL;
  1202. u32 length, rgrp_blk, buf_blk;
  1203. unsigned int buf;
  1204. rgd = gfs2_blk2rgrpd(sdp, bstart);
  1205. if (!rgd) {
  1206. if (gfs2_consist(sdp))
  1207. fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
  1208. return NULL;
  1209. }
  1210. length = rgd->rd_length;
  1211. rgrp_blk = bstart - rgd->rd_data0;
  1212. while (blen--) {
  1213. for (buf = 0; buf < length; buf++) {
  1214. bi = rgd->rd_bits + buf;
  1215. if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1216. break;
  1217. }
  1218. gfs2_assert(rgd->rd_sbd, buf < length);
  1219. buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
  1220. rgrp_blk++;
  1221. if (!bi->bi_clone) {
  1222. bi->bi_clone = kmalloc(bi->bi_bh->b_size,
  1223. GFP_NOFS | __GFP_NOFAIL);
  1224. memcpy(bi->bi_clone + bi->bi_offset,
  1225. bi->bi_bh->b_data + bi->bi_offset,
  1226. bi->bi_len);
  1227. }
  1228. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1229. gfs2_setbit(rgd, bi->bi_bh->b_data, NULL, bi->bi_offset,
  1230. bi->bi_len, buf_blk, new_state);
  1231. }
  1232. return rgd;
  1233. }
  1234. /**
  1235. * gfs2_alloc_block - Allocate a block
  1236. * @ip: the inode to allocate the block for
  1237. *
  1238. * Returns: the allocated block
  1239. */
  1240. u64 gfs2_alloc_block(struct gfs2_inode *ip, unsigned int *n)
  1241. {
  1242. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1243. struct gfs2_alloc *al = ip->i_alloc;
  1244. struct gfs2_rgrpd *rgd = al->al_rgd;
  1245. u32 goal, blk;
  1246. u64 block;
  1247. if (rgrp_contains_block(rgd, ip->i_goal))
  1248. goal = ip->i_goal - rgd->rd_data0;
  1249. else
  1250. goal = rgd->rd_last_alloc;
  1251. blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n);
  1252. BUG_ON(blk == BFITNOENT);
  1253. rgd->rd_last_alloc = blk;
  1254. block = rgd->rd_data0 + blk;
  1255. ip->i_goal = block;
  1256. gfs2_assert_withdraw(sdp, rgd->rd_free >= *n);
  1257. rgd->rd_free -= *n;
  1258. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1259. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1260. al->al_alloced += *n;
  1261. gfs2_statfs_change(sdp, 0, -(s64)*n, 0);
  1262. gfs2_quota_change(ip, *n, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1263. spin_lock(&sdp->sd_rindex_spin);
  1264. rgd->rd_free_clone -= *n;
  1265. spin_unlock(&sdp->sd_rindex_spin);
  1266. return block;
  1267. }
  1268. /**
  1269. * gfs2_alloc_di - Allocate a dinode
  1270. * @dip: the directory that the inode is going in
  1271. *
  1272. * Returns: the block allocated
  1273. */
  1274. u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
  1275. {
  1276. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1277. struct gfs2_alloc *al = dip->i_alloc;
  1278. struct gfs2_rgrpd *rgd = al->al_rgd;
  1279. u32 blk;
  1280. u64 block;
  1281. unsigned int n = 1;
  1282. blk = rgblk_search(rgd, rgd->rd_last_alloc,
  1283. GFS2_BLKST_FREE, GFS2_BLKST_DINODE, &n);
  1284. BUG_ON(blk == BFITNOENT);
  1285. rgd->rd_last_alloc = blk;
  1286. block = rgd->rd_data0 + blk;
  1287. gfs2_assert_withdraw(sdp, rgd->rd_free);
  1288. rgd->rd_free--;
  1289. rgd->rd_dinodes++;
  1290. *generation = rgd->rd_igeneration++;
  1291. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1292. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1293. al->al_alloced++;
  1294. gfs2_statfs_change(sdp, 0, -1, +1);
  1295. gfs2_trans_add_unrevoke(sdp, block, 1);
  1296. spin_lock(&sdp->sd_rindex_spin);
  1297. rgd->rd_free_clone--;
  1298. spin_unlock(&sdp->sd_rindex_spin);
  1299. return block;
  1300. }
  1301. /**
  1302. * gfs2_free_data - free a contiguous run of data block(s)
  1303. * @ip: the inode these blocks are being freed from
  1304. * @bstart: first block of a run of contiguous blocks
  1305. * @blen: the length of the block run
  1306. *
  1307. */
  1308. void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
  1309. {
  1310. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1311. struct gfs2_rgrpd *rgd;
  1312. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1313. if (!rgd)
  1314. return;
  1315. rgd->rd_free += blen;
  1316. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1317. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1318. gfs2_trans_add_rg(rgd);
  1319. gfs2_statfs_change(sdp, 0, +blen, 0);
  1320. gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1321. }
  1322. /**
  1323. * gfs2_free_meta - free a contiguous run of data block(s)
  1324. * @ip: the inode these blocks are being freed from
  1325. * @bstart: first block of a run of contiguous blocks
  1326. * @blen: the length of the block run
  1327. *
  1328. */
  1329. void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
  1330. {
  1331. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1332. struct gfs2_rgrpd *rgd;
  1333. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1334. if (!rgd)
  1335. return;
  1336. rgd->rd_free += blen;
  1337. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1338. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1339. gfs2_trans_add_rg(rgd);
  1340. gfs2_statfs_change(sdp, 0, +blen, 0);
  1341. gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1342. gfs2_meta_wipe(ip, bstart, blen);
  1343. }
  1344. void gfs2_unlink_di(struct inode *inode)
  1345. {
  1346. struct gfs2_inode *ip = GFS2_I(inode);
  1347. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1348. struct gfs2_rgrpd *rgd;
  1349. u64 blkno = ip->i_no_addr;
  1350. rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
  1351. if (!rgd)
  1352. return;
  1353. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1354. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1355. gfs2_trans_add_rg(rgd);
  1356. }
  1357. static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
  1358. {
  1359. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1360. struct gfs2_rgrpd *tmp_rgd;
  1361. tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
  1362. if (!tmp_rgd)
  1363. return;
  1364. gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
  1365. if (!rgd->rd_dinodes)
  1366. gfs2_consist_rgrpd(rgd);
  1367. rgd->rd_dinodes--;
  1368. rgd->rd_free++;
  1369. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1370. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1371. gfs2_statfs_change(sdp, 0, +1, -1);
  1372. gfs2_trans_add_rg(rgd);
  1373. }
  1374. void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
  1375. {
  1376. gfs2_free_uninit_di(rgd, ip->i_no_addr);
  1377. gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1378. gfs2_meta_wipe(ip, ip->i_no_addr, 1);
  1379. }
  1380. /**
  1381. * gfs2_rlist_add - add a RG to a list of RGs
  1382. * @sdp: the filesystem
  1383. * @rlist: the list of resource groups
  1384. * @block: the block
  1385. *
  1386. * Figure out what RG a block belongs to and add that RG to the list
  1387. *
  1388. * FIXME: Don't use NOFAIL
  1389. *
  1390. */
  1391. void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
  1392. u64 block)
  1393. {
  1394. struct gfs2_rgrpd *rgd;
  1395. struct gfs2_rgrpd **tmp;
  1396. unsigned int new_space;
  1397. unsigned int x;
  1398. if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
  1399. return;
  1400. rgd = gfs2_blk2rgrpd(sdp, block);
  1401. if (!rgd) {
  1402. if (gfs2_consist(sdp))
  1403. fs_err(sdp, "block = %llu\n", (unsigned long long)block);
  1404. return;
  1405. }
  1406. for (x = 0; x < rlist->rl_rgrps; x++)
  1407. if (rlist->rl_rgd[x] == rgd)
  1408. return;
  1409. if (rlist->rl_rgrps == rlist->rl_space) {
  1410. new_space = rlist->rl_space + 10;
  1411. tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
  1412. GFP_NOFS | __GFP_NOFAIL);
  1413. if (rlist->rl_rgd) {
  1414. memcpy(tmp, rlist->rl_rgd,
  1415. rlist->rl_space * sizeof(struct gfs2_rgrpd *));
  1416. kfree(rlist->rl_rgd);
  1417. }
  1418. rlist->rl_space = new_space;
  1419. rlist->rl_rgd = tmp;
  1420. }
  1421. rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
  1422. }
  1423. /**
  1424. * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
  1425. * and initialize an array of glock holders for them
  1426. * @rlist: the list of resource groups
  1427. * @state: the lock state to acquire the RG lock in
  1428. * @flags: the modifier flags for the holder structures
  1429. *
  1430. * FIXME: Don't use NOFAIL
  1431. *
  1432. */
  1433. void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
  1434. {
  1435. unsigned int x;
  1436. rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
  1437. GFP_NOFS | __GFP_NOFAIL);
  1438. for (x = 0; x < rlist->rl_rgrps; x++)
  1439. gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
  1440. state, 0,
  1441. &rlist->rl_ghs[x]);
  1442. }
  1443. /**
  1444. * gfs2_rlist_free - free a resource group list
  1445. * @list: the list of resource groups
  1446. *
  1447. */
  1448. void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
  1449. {
  1450. unsigned int x;
  1451. kfree(rlist->rl_rgd);
  1452. if (rlist->rl_ghs) {
  1453. for (x = 0; x < rlist->rl_rgrps; x++)
  1454. gfs2_holder_uninit(&rlist->rl_ghs[x]);
  1455. kfree(rlist->rl_ghs);
  1456. }
  1457. }