rgrp.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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, *parent = NULL;
  293. spin_lock(&sdp->sd_rindex_spin);
  294. newn = &sdp->sd_rindex_tree.rb_node;
  295. /* Figure out where to put new node */
  296. while (*newn) {
  297. struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd,
  298. rd_node);
  299. parent = *newn;
  300. if (blk < cur->rd_addr)
  301. newn = &((*newn)->rb_left);
  302. else if (blk > cur->rd_data0 + cur->rd_data)
  303. newn = &((*newn)->rb_right);
  304. else {
  305. spin_unlock(&sdp->sd_rindex_spin);
  306. return cur;
  307. }
  308. }
  309. spin_unlock(&sdp->sd_rindex_spin);
  310. return NULL;
  311. }
  312. /**
  313. * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
  314. * @sdp: The GFS2 superblock
  315. *
  316. * Returns: The first rgrp in the filesystem
  317. */
  318. struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
  319. {
  320. const struct rb_node *n;
  321. struct gfs2_rgrpd *rgd;
  322. n = rb_first(&sdp->sd_rindex_tree);
  323. rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
  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. static void clear_rgrpdi(struct gfs2_sbd *sdp)
  349. {
  350. struct rb_node *n;
  351. struct gfs2_rgrpd *rgd;
  352. struct gfs2_glock *gl;
  353. while ((n = rb_first(&sdp->sd_rindex_tree))) {
  354. rgd = rb_entry(n, struct gfs2_rgrpd, rd_node);
  355. gl = rgd->rd_gl;
  356. rb_erase(n, &sdp->sd_rindex_tree);
  357. if (gl) {
  358. gl->gl_object = NULL;
  359. gfs2_glock_add_to_lru(gl);
  360. gfs2_glock_put(gl);
  361. }
  362. kfree(rgd->rd_bits);
  363. kmem_cache_free(gfs2_rgrpd_cachep, rgd);
  364. }
  365. }
  366. void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
  367. {
  368. mutex_lock(&sdp->sd_rindex_mutex);
  369. clear_rgrpdi(sdp);
  370. mutex_unlock(&sdp->sd_rindex_mutex);
  371. }
  372. static void gfs2_rindex_print(const struct gfs2_rgrpd *rgd)
  373. {
  374. printk(KERN_INFO " ri_addr = %llu\n", (unsigned long long)rgd->rd_addr);
  375. printk(KERN_INFO " ri_length = %u\n", rgd->rd_length);
  376. printk(KERN_INFO " ri_data0 = %llu\n", (unsigned long long)rgd->rd_data0);
  377. printk(KERN_INFO " ri_data = %u\n", rgd->rd_data);
  378. printk(KERN_INFO " ri_bitbytes = %u\n", rgd->rd_bitbytes);
  379. }
  380. /**
  381. * gfs2_compute_bitstructs - Compute the bitmap sizes
  382. * @rgd: The resource group descriptor
  383. *
  384. * Calculates bitmap descriptors, one for each block that contains bitmap data
  385. *
  386. * Returns: errno
  387. */
  388. static int compute_bitstructs(struct gfs2_rgrpd *rgd)
  389. {
  390. struct gfs2_sbd *sdp = rgd->rd_sbd;
  391. struct gfs2_bitmap *bi;
  392. u32 length = rgd->rd_length; /* # blocks in hdr & bitmap */
  393. u32 bytes_left, bytes;
  394. int x;
  395. if (!length)
  396. return -EINVAL;
  397. rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_NOFS);
  398. if (!rgd->rd_bits)
  399. return -ENOMEM;
  400. bytes_left = rgd->rd_bitbytes;
  401. for (x = 0; x < length; x++) {
  402. bi = rgd->rd_bits + x;
  403. bi->bi_flags = 0;
  404. /* small rgrp; bitmap stored completely in header block */
  405. if (length == 1) {
  406. bytes = bytes_left;
  407. bi->bi_offset = sizeof(struct gfs2_rgrp);
  408. bi->bi_start = 0;
  409. bi->bi_len = bytes;
  410. /* header block */
  411. } else if (x == 0) {
  412. bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
  413. bi->bi_offset = sizeof(struct gfs2_rgrp);
  414. bi->bi_start = 0;
  415. bi->bi_len = bytes;
  416. /* last block */
  417. } else if (x + 1 == length) {
  418. bytes = bytes_left;
  419. bi->bi_offset = sizeof(struct gfs2_meta_header);
  420. bi->bi_start = rgd->rd_bitbytes - bytes_left;
  421. bi->bi_len = bytes;
  422. /* other blocks */
  423. } else {
  424. bytes = sdp->sd_sb.sb_bsize -
  425. sizeof(struct gfs2_meta_header);
  426. bi->bi_offset = sizeof(struct gfs2_meta_header);
  427. bi->bi_start = rgd->rd_bitbytes - bytes_left;
  428. bi->bi_len = bytes;
  429. }
  430. bytes_left -= bytes;
  431. }
  432. if (bytes_left) {
  433. gfs2_consist_rgrpd(rgd);
  434. return -EIO;
  435. }
  436. bi = rgd->rd_bits + (length - 1);
  437. if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_data) {
  438. if (gfs2_consist_rgrpd(rgd)) {
  439. gfs2_rindex_print(rgd);
  440. fs_err(sdp, "start=%u len=%u offset=%u\n",
  441. bi->bi_start, bi->bi_len, bi->bi_offset);
  442. }
  443. return -EIO;
  444. }
  445. return 0;
  446. }
  447. /**
  448. * gfs2_ri_total - Total up the file system space, according to the rindex.
  449. *
  450. */
  451. u64 gfs2_ri_total(struct gfs2_sbd *sdp)
  452. {
  453. u64 total_data = 0;
  454. struct inode *inode = sdp->sd_rindex;
  455. struct gfs2_inode *ip = GFS2_I(inode);
  456. char buf[sizeof(struct gfs2_rindex)];
  457. struct file_ra_state ra_state;
  458. int error, rgrps;
  459. mutex_lock(&sdp->sd_rindex_mutex);
  460. file_ra_state_init(&ra_state, inode->i_mapping);
  461. for (rgrps = 0;; rgrps++) {
  462. loff_t pos = rgrps * sizeof(struct gfs2_rindex);
  463. if (pos + sizeof(struct gfs2_rindex) > i_size_read(inode))
  464. break;
  465. error = gfs2_internal_read(ip, &ra_state, buf, &pos,
  466. sizeof(struct gfs2_rindex));
  467. if (error != sizeof(struct gfs2_rindex))
  468. break;
  469. total_data += be32_to_cpu(((struct gfs2_rindex *)buf)->ri_data);
  470. }
  471. mutex_unlock(&sdp->sd_rindex_mutex);
  472. return total_data;
  473. }
  474. static void gfs2_rindex_in(struct gfs2_rgrpd *rgd, const void *buf)
  475. {
  476. const struct gfs2_rindex *str = buf;
  477. rgd->rd_addr = be64_to_cpu(str->ri_addr);
  478. rgd->rd_length = be32_to_cpu(str->ri_length);
  479. rgd->rd_data0 = be64_to_cpu(str->ri_data0);
  480. rgd->rd_data = be32_to_cpu(str->ri_data);
  481. rgd->rd_bitbytes = be32_to_cpu(str->ri_bitbytes);
  482. }
  483. static void rgd_insert(struct gfs2_rgrpd *rgd)
  484. {
  485. struct gfs2_sbd *sdp = rgd->rd_sbd;
  486. struct rb_node **newn = &sdp->sd_rindex_tree.rb_node, *parent = NULL;
  487. /* Figure out where to put new node */
  488. while (*newn) {
  489. struct gfs2_rgrpd *cur = rb_entry(*newn, struct gfs2_rgrpd,
  490. rd_node);
  491. parent = *newn;
  492. if (rgd->rd_addr < cur->rd_addr)
  493. newn = &((*newn)->rb_left);
  494. else if (rgd->rd_addr > cur->rd_addr)
  495. newn = &((*newn)->rb_right);
  496. else
  497. return;
  498. }
  499. rb_link_node(&rgd->rd_node, parent, newn);
  500. rb_insert_color(&rgd->rd_node, &sdp->sd_rindex_tree);
  501. }
  502. /**
  503. * read_rindex_entry - Pull in a new resource index entry from the disk
  504. * @gl: The glock covering the rindex inode
  505. *
  506. * Returns: 0 on success, error code otherwise
  507. */
  508. static int read_rindex_entry(struct gfs2_inode *ip,
  509. struct file_ra_state *ra_state)
  510. {
  511. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  512. loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
  513. char buf[sizeof(struct gfs2_rindex)];
  514. int error;
  515. struct gfs2_rgrpd *rgd;
  516. error = gfs2_internal_read(ip, ra_state, buf, &pos,
  517. sizeof(struct gfs2_rindex));
  518. if (!error)
  519. return 0;
  520. if (error != sizeof(struct gfs2_rindex)) {
  521. if (error > 0)
  522. error = -EIO;
  523. return error;
  524. }
  525. rgd = kmem_cache_zalloc(gfs2_rgrpd_cachep, GFP_NOFS);
  526. error = -ENOMEM;
  527. if (!rgd)
  528. return error;
  529. rgd->rd_sbd = sdp;
  530. gfs2_rindex_in(rgd, buf);
  531. rgd_insert(rgd);
  532. error = compute_bitstructs(rgd);
  533. if (error)
  534. return error;
  535. error = gfs2_glock_get(sdp, rgd->rd_addr,
  536. &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
  537. if (error)
  538. return error;
  539. rgd->rd_gl->gl_object = rgd;
  540. rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
  541. if (rgd->rd_data > sdp->sd_max_rg_data)
  542. sdp->sd_max_rg_data = rgd->rd_data;
  543. return error;
  544. }
  545. /**
  546. * gfs2_ri_update - Pull in a new resource index from the disk
  547. * @ip: pointer to the rindex inode
  548. *
  549. * Returns: 0 on successful update, error code otherwise
  550. */
  551. int gfs2_ri_update(struct gfs2_inode *ip)
  552. {
  553. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  554. struct inode *inode = &ip->i_inode;
  555. struct file_ra_state ra_state;
  556. u64 rgrp_count = i_size_read(inode);
  557. int error;
  558. do_div(rgrp_count, sizeof(struct gfs2_rindex));
  559. clear_rgrpdi(sdp);
  560. file_ra_state_init(&ra_state, inode->i_mapping);
  561. for (sdp->sd_rgrps = 0; sdp->sd_rgrps < rgrp_count; sdp->sd_rgrps++) {
  562. error = read_rindex_entry(ip, &ra_state);
  563. if (error) {
  564. clear_rgrpdi(sdp);
  565. return error;
  566. }
  567. }
  568. sdp->sd_rindex_uptodate = 1;
  569. return 0;
  570. }
  571. /**
  572. * gfs2_rindex_hold - Grab a lock on the rindex
  573. * @sdp: The GFS2 superblock
  574. * @ri_gh: the glock holder
  575. *
  576. * We grab a lock on the rindex inode to make sure that it doesn't
  577. * change whilst we are performing an operation. We keep this lock
  578. * for quite long periods of time compared to other locks. This
  579. * doesn't matter, since it is shared and it is very, very rarely
  580. * accessed in the exclusive mode (i.e. only when expanding the filesystem).
  581. *
  582. * This makes sure that we're using the latest copy of the resource index
  583. * special file, which might have been updated if someone expanded the
  584. * filesystem (via gfs2_grow utility), which adds new resource groups.
  585. *
  586. * Returns: 0 on success, error code otherwise
  587. */
  588. int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
  589. {
  590. struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
  591. struct gfs2_glock *gl = ip->i_gl;
  592. int error;
  593. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
  594. if (error)
  595. return error;
  596. /* Read new copy from disk if we don't have the latest */
  597. if (!sdp->sd_rindex_uptodate) {
  598. mutex_lock(&sdp->sd_rindex_mutex);
  599. if (!sdp->sd_rindex_uptodate) {
  600. error = gfs2_ri_update(ip);
  601. if (error)
  602. gfs2_glock_dq_uninit(ri_gh);
  603. }
  604. mutex_unlock(&sdp->sd_rindex_mutex);
  605. }
  606. return error;
  607. }
  608. static void gfs2_rgrp_in(struct gfs2_rgrpd *rgd, const void *buf)
  609. {
  610. const struct gfs2_rgrp *str = buf;
  611. u32 rg_flags;
  612. rg_flags = be32_to_cpu(str->rg_flags);
  613. rg_flags &= ~GFS2_RDF_MASK;
  614. rgd->rd_flags &= GFS2_RDF_MASK;
  615. rgd->rd_flags |= rg_flags;
  616. rgd->rd_free = be32_to_cpu(str->rg_free);
  617. rgd->rd_dinodes = be32_to_cpu(str->rg_dinodes);
  618. rgd->rd_igeneration = be64_to_cpu(str->rg_igeneration);
  619. }
  620. static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
  621. {
  622. struct gfs2_rgrp *str = buf;
  623. str->rg_flags = cpu_to_be32(rgd->rd_flags & ~GFS2_RDF_MASK);
  624. str->rg_free = cpu_to_be32(rgd->rd_free);
  625. str->rg_dinodes = cpu_to_be32(rgd->rd_dinodes);
  626. str->__pad = cpu_to_be32(0);
  627. str->rg_igeneration = cpu_to_be64(rgd->rd_igeneration);
  628. memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
  629. }
  630. /**
  631. * gfs2_rgrp_go_lock - Read in a RG's header and bitmaps
  632. * @rgd: the struct gfs2_rgrpd describing the RG to read in
  633. *
  634. * Read in all of a Resource Group's header and bitmap blocks.
  635. * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
  636. *
  637. * Returns: errno
  638. */
  639. int gfs2_rgrp_go_lock(struct gfs2_holder *gh)
  640. {
  641. struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
  642. struct gfs2_sbd *sdp = rgd->rd_sbd;
  643. struct gfs2_glock *gl = rgd->rd_gl;
  644. unsigned int length = rgd->rd_length;
  645. struct gfs2_bitmap *bi;
  646. unsigned int x, y;
  647. int error;
  648. for (x = 0; x < length; x++) {
  649. bi = rgd->rd_bits + x;
  650. error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
  651. if (error)
  652. goto fail;
  653. }
  654. for (y = length; y--;) {
  655. bi = rgd->rd_bits + y;
  656. error = gfs2_meta_wait(sdp, bi->bi_bh);
  657. if (error)
  658. goto fail;
  659. if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
  660. GFS2_METATYPE_RG)) {
  661. error = -EIO;
  662. goto fail;
  663. }
  664. }
  665. if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
  666. for (x = 0; x < length; x++)
  667. clear_bit(GBF_FULL, &rgd->rd_bits[x].bi_flags);
  668. gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
  669. rgd->rd_flags |= (GFS2_RDF_UPTODATE | GFS2_RDF_CHECK);
  670. rgd->rd_free_clone = rgd->rd_free;
  671. }
  672. return 0;
  673. fail:
  674. while (x--) {
  675. bi = rgd->rd_bits + x;
  676. brelse(bi->bi_bh);
  677. bi->bi_bh = NULL;
  678. gfs2_assert_warn(sdp, !bi->bi_clone);
  679. }
  680. return error;
  681. }
  682. /**
  683. * gfs2_rgrp_go_unlock - Release RG bitmaps read in with gfs2_rgrp_bh_get()
  684. * @rgd: the struct gfs2_rgrpd describing the RG to read in
  685. *
  686. */
  687. void gfs2_rgrp_go_unlock(struct gfs2_holder *gh)
  688. {
  689. struct gfs2_rgrpd *rgd = gh->gh_gl->gl_object;
  690. int x, length = rgd->rd_length;
  691. for (x = 0; x < length; x++) {
  692. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  693. brelse(bi->bi_bh);
  694. bi->bi_bh = NULL;
  695. }
  696. }
  697. void gfs2_rgrp_send_discards(struct gfs2_sbd *sdp, u64 offset,
  698. struct buffer_head *bh,
  699. const struct gfs2_bitmap *bi)
  700. {
  701. struct super_block *sb = sdp->sd_vfs;
  702. struct block_device *bdev = sb->s_bdev;
  703. const unsigned int sects_per_blk = sdp->sd_sb.sb_bsize /
  704. bdev_logical_block_size(sb->s_bdev);
  705. u64 blk;
  706. sector_t start = 0;
  707. sector_t nr_sects = 0;
  708. int rv;
  709. unsigned int x;
  710. for (x = 0; x < bi->bi_len; x++) {
  711. const u8 *orig = bh->b_data + bi->bi_offset + x;
  712. const u8 *clone = bi->bi_clone + bi->bi_offset + x;
  713. u8 diff = ~(*orig | (*orig >> 1)) & (*clone | (*clone >> 1));
  714. diff &= 0x55;
  715. if (diff == 0)
  716. continue;
  717. blk = offset + ((bi->bi_start + x) * GFS2_NBBY);
  718. blk *= sects_per_blk; /* convert to sectors */
  719. while(diff) {
  720. if (diff & 1) {
  721. if (nr_sects == 0)
  722. goto start_new_extent;
  723. if ((start + nr_sects) != blk) {
  724. rv = blkdev_issue_discard(bdev, start,
  725. nr_sects, GFP_NOFS,
  726. 0);
  727. if (rv)
  728. goto fail;
  729. nr_sects = 0;
  730. start_new_extent:
  731. start = blk;
  732. }
  733. nr_sects += sects_per_blk;
  734. }
  735. diff >>= 2;
  736. blk += sects_per_blk;
  737. }
  738. }
  739. if (nr_sects) {
  740. rv = blkdev_issue_discard(bdev, start, nr_sects, GFP_NOFS, 0);
  741. if (rv)
  742. goto fail;
  743. }
  744. return;
  745. fail:
  746. fs_warn(sdp, "error %d on discard request, turning discards off for this filesystem", rv);
  747. sdp->sd_args.ar_discard = 0;
  748. }
  749. /**
  750. * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
  751. * @ip: the incore GFS2 inode structure
  752. *
  753. * Returns: the struct gfs2_alloc
  754. */
  755. struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
  756. {
  757. BUG_ON(ip->i_alloc != NULL);
  758. ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_NOFS);
  759. return ip->i_alloc;
  760. }
  761. /**
  762. * try_rgrp_fit - See if a given reservation will fit in a given RG
  763. * @rgd: the RG data
  764. * @al: the struct gfs2_alloc structure describing the reservation
  765. *
  766. * If there's room for the requested blocks to be allocated from the RG:
  767. * Sets the $al_rgd field in @al.
  768. *
  769. * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
  770. */
  771. static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
  772. {
  773. if (rgd->rd_flags & (GFS2_RGF_NOALLOC | GFS2_RDF_ERROR))
  774. return 0;
  775. if (rgd->rd_free_clone >= al->al_requested) {
  776. al->al_rgd = rgd;
  777. return 1;
  778. }
  779. return 0;
  780. }
  781. /**
  782. * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
  783. * @rgd: The rgrp
  784. *
  785. * Returns: 0 if no error
  786. * The inode, if one has been found, in inode.
  787. */
  788. static void try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked, u64 skip)
  789. {
  790. u32 goal = 0, block;
  791. u64 no_addr;
  792. struct gfs2_sbd *sdp = rgd->rd_sbd;
  793. unsigned int n;
  794. struct gfs2_glock *gl;
  795. struct gfs2_inode *ip;
  796. int error;
  797. int found = 0;
  798. while (goal < rgd->rd_data) {
  799. down_write(&sdp->sd_log_flush_lock);
  800. n = 1;
  801. block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
  802. GFS2_BLKST_UNLINKED, &n);
  803. up_write(&sdp->sd_log_flush_lock);
  804. if (block == BFITNOENT)
  805. break;
  806. /* rgblk_search can return a block < goal, so we need to
  807. keep it marching forward. */
  808. no_addr = block + rgd->rd_data0;
  809. goal = max(block + 1, goal + 1);
  810. if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
  811. continue;
  812. if (no_addr == skip)
  813. continue;
  814. *last_unlinked = no_addr;
  815. error = gfs2_glock_get(sdp, no_addr, &gfs2_inode_glops, CREATE, &gl);
  816. if (error)
  817. continue;
  818. /* If the inode is already in cache, we can ignore it here
  819. * because the existing inode disposal code will deal with
  820. * it when all refs have gone away. Accessing gl_object like
  821. * this is not safe in general. Here it is ok because we do
  822. * not dereference the pointer, and we only need an approx
  823. * answer to whether it is NULL or not.
  824. */
  825. ip = gl->gl_object;
  826. if (ip || queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
  827. gfs2_glock_put(gl);
  828. else
  829. found++;
  830. /* Limit reclaim to sensible number of tasks */
  831. if (found > NR_CPUS)
  832. return;
  833. }
  834. rgd->rd_flags &= ~GFS2_RDF_CHECK;
  835. return;
  836. }
  837. /**
  838. * get_local_rgrp - Choose and lock a rgrp for allocation
  839. * @ip: the inode to reserve space for
  840. * @rgp: the chosen and locked rgrp
  841. *
  842. * Try to acquire rgrp in way which avoids contending with others.
  843. *
  844. * Returns: errno
  845. */
  846. static int get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
  847. {
  848. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  849. struct gfs2_rgrpd *rgd, *begin = NULL;
  850. struct gfs2_alloc *al = ip->i_alloc;
  851. int error, rg_locked;
  852. int loops = 0;
  853. rgd = begin = gfs2_blk2rgrpd(sdp, ip->i_goal);
  854. if (rgd == NULL)
  855. return -EBADSLT;
  856. while (loops < 3) {
  857. rg_locked = 0;
  858. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  859. rg_locked = 1;
  860. error = 0;
  861. } else {
  862. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
  863. LM_FLAG_TRY, &al->al_rgd_gh);
  864. }
  865. switch (error) {
  866. case 0:
  867. if (try_rgrp_fit(rgd, al))
  868. return 0;
  869. if (rgd->rd_flags & GFS2_RDF_CHECK)
  870. try_rgrp_unlink(rgd, last_unlinked, ip->i_no_addr);
  871. if (!rg_locked)
  872. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  873. /* fall through */
  874. case GLR_TRYFAILED:
  875. rgd = gfs2_rgrpd_get_next(rgd);
  876. if (rgd == begin)
  877. loops++;
  878. break;
  879. default:
  880. return error;
  881. }
  882. }
  883. return -ENOSPC;
  884. }
  885. /**
  886. * gfs2_inplace_reserve_i - Reserve space in the filesystem
  887. * @ip: the inode to reserve space for
  888. *
  889. * Returns: errno
  890. */
  891. int gfs2_inplace_reserve_i(struct gfs2_inode *ip, int hold_rindex,
  892. char *file, unsigned int line)
  893. {
  894. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  895. struct gfs2_alloc *al = ip->i_alloc;
  896. int error = 0;
  897. u64 last_unlinked = NO_BLOCK;
  898. int tries = 0;
  899. if (gfs2_assert_warn(sdp, al->al_requested))
  900. return -EINVAL;
  901. if (hold_rindex) {
  902. /* We need to hold the rindex unless the inode we're using is
  903. the rindex itself, in which case it's already held. */
  904. if (ip != GFS2_I(sdp->sd_rindex))
  905. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  906. else if (!sdp->sd_rgrps) /* We may not have the rindex read
  907. in, so: */
  908. error = gfs2_ri_update(ip);
  909. if (error)
  910. return error;
  911. }
  912. try_again:
  913. do {
  914. error = get_local_rgrp(ip, &last_unlinked);
  915. /* If there is no space, flushing the log may release some */
  916. if (error) {
  917. if (ip == GFS2_I(sdp->sd_rindex) &&
  918. !sdp->sd_rindex_uptodate) {
  919. error = gfs2_ri_update(ip);
  920. if (error)
  921. return error;
  922. goto try_again;
  923. }
  924. gfs2_log_flush(sdp, NULL);
  925. }
  926. } while (error && tries++ < 3);
  927. if (error) {
  928. if (hold_rindex && ip != GFS2_I(sdp->sd_rindex))
  929. gfs2_glock_dq_uninit(&al->al_ri_gh);
  930. return error;
  931. }
  932. /* no error, so we have the rgrp set in the inode's allocation. */
  933. al->al_file = file;
  934. al->al_line = line;
  935. return 0;
  936. }
  937. /**
  938. * gfs2_inplace_release - release an inplace reservation
  939. * @ip: the inode the reservation was taken out on
  940. *
  941. * Release a reservation made by gfs2_inplace_reserve().
  942. */
  943. void gfs2_inplace_release(struct gfs2_inode *ip)
  944. {
  945. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  946. struct gfs2_alloc *al = ip->i_alloc;
  947. if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
  948. fs_warn(sdp, "al_alloced = %u, al_requested = %u "
  949. "al_file = %s, al_line = %u\n",
  950. al->al_alloced, al->al_requested, al->al_file,
  951. al->al_line);
  952. al->al_rgd = NULL;
  953. if (al->al_rgd_gh.gh_gl)
  954. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  955. if (ip != GFS2_I(sdp->sd_rindex) && al->al_ri_gh.gh_gl)
  956. gfs2_glock_dq_uninit(&al->al_ri_gh);
  957. }
  958. /**
  959. * gfs2_get_block_type - Check a block in a RG is of given type
  960. * @rgd: the resource group holding the block
  961. * @block: the block number
  962. *
  963. * Returns: The block type (GFS2_BLKST_*)
  964. */
  965. static unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
  966. {
  967. struct gfs2_bitmap *bi = NULL;
  968. u32 length, rgrp_block, buf_block;
  969. unsigned int buf;
  970. unsigned char type;
  971. length = rgd->rd_length;
  972. rgrp_block = block - rgd->rd_data0;
  973. for (buf = 0; buf < length; buf++) {
  974. bi = rgd->rd_bits + buf;
  975. if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  976. break;
  977. }
  978. gfs2_assert(rgd->rd_sbd, buf < length);
  979. buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
  980. type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
  981. bi->bi_len, buf_block);
  982. return type;
  983. }
  984. /**
  985. * rgblk_search - find a block in @old_state, change allocation
  986. * state to @new_state
  987. * @rgd: the resource group descriptor
  988. * @goal: the goal block within the RG (start here to search for avail block)
  989. * @old_state: GFS2_BLKST_XXX the before-allocation state to find
  990. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  991. * @n: The extent length
  992. *
  993. * Walk rgrp's bitmap to find bits that represent a block in @old_state.
  994. * Add the found bitmap buffer to the transaction.
  995. * Set the found bits to @new_state to change block's allocation state.
  996. *
  997. * This function never fails, because we wouldn't call it unless we
  998. * know (from reservation results, etc.) that a block is available.
  999. *
  1000. * Scope of @goal and returned block is just within rgrp, not the whole
  1001. * filesystem.
  1002. *
  1003. * Returns: the block number allocated
  1004. */
  1005. static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
  1006. unsigned char old_state, unsigned char new_state,
  1007. unsigned int *n)
  1008. {
  1009. struct gfs2_bitmap *bi = NULL;
  1010. const u32 length = rgd->rd_length;
  1011. u32 blk = BFITNOENT;
  1012. unsigned int buf, x;
  1013. const unsigned int elen = *n;
  1014. const u8 *buffer = NULL;
  1015. *n = 0;
  1016. /* Find bitmap block that contains bits for goal block */
  1017. for (buf = 0; buf < length; buf++) {
  1018. bi = rgd->rd_bits + buf;
  1019. /* Convert scope of "goal" from rgrp-wide to within found bit block */
  1020. if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY) {
  1021. goal -= bi->bi_start * GFS2_NBBY;
  1022. goto do_search;
  1023. }
  1024. }
  1025. buf = 0;
  1026. goal = 0;
  1027. do_search:
  1028. /* Search (up to entire) bitmap in this rgrp for allocatable block.
  1029. "x <= length", instead of "x < length", because we typically start
  1030. the search in the middle of a bit block, but if we can't find an
  1031. allocatable block anywhere else, we want to be able wrap around and
  1032. search in the first part of our first-searched bit block. */
  1033. for (x = 0; x <= length; x++) {
  1034. bi = rgd->rd_bits + buf;
  1035. if (test_bit(GBF_FULL, &bi->bi_flags) &&
  1036. (old_state == GFS2_BLKST_FREE))
  1037. goto skip;
  1038. /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
  1039. bitmaps, so we must search the originals for that. */
  1040. buffer = bi->bi_bh->b_data + bi->bi_offset;
  1041. WARN_ON(!buffer_uptodate(bi->bi_bh));
  1042. if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
  1043. buffer = bi->bi_clone + bi->bi_offset;
  1044. blk = gfs2_bitfit(buffer, bi->bi_len, goal, old_state);
  1045. if (blk != BFITNOENT)
  1046. break;
  1047. if ((goal == 0) && (old_state == GFS2_BLKST_FREE))
  1048. set_bit(GBF_FULL, &bi->bi_flags);
  1049. /* Try next bitmap block (wrap back to rgrp header if at end) */
  1050. skip:
  1051. buf++;
  1052. buf %= length;
  1053. goal = 0;
  1054. }
  1055. if (blk == BFITNOENT)
  1056. return blk;
  1057. *n = 1;
  1058. if (old_state == new_state)
  1059. goto out;
  1060. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1061. gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
  1062. bi, blk, new_state);
  1063. goal = blk;
  1064. while (*n < elen) {
  1065. goal++;
  1066. if (goal >= (bi->bi_len * GFS2_NBBY))
  1067. break;
  1068. if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
  1069. GFS2_BLKST_FREE)
  1070. break;
  1071. gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
  1072. bi, goal, new_state);
  1073. (*n)++;
  1074. }
  1075. out:
  1076. return (bi->bi_start * GFS2_NBBY) + blk;
  1077. }
  1078. /**
  1079. * rgblk_free - Change alloc state of given block(s)
  1080. * @sdp: the filesystem
  1081. * @bstart: the start of a run of blocks to free
  1082. * @blen: the length of the block run (all must lie within ONE RG!)
  1083. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1084. *
  1085. * Returns: Resource group containing the block(s)
  1086. */
  1087. static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
  1088. u32 blen, unsigned char new_state)
  1089. {
  1090. struct gfs2_rgrpd *rgd;
  1091. struct gfs2_bitmap *bi = NULL;
  1092. u32 length, rgrp_blk, buf_blk;
  1093. unsigned int buf;
  1094. rgd = gfs2_blk2rgrpd(sdp, bstart);
  1095. if (!rgd) {
  1096. if (gfs2_consist(sdp))
  1097. fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
  1098. return NULL;
  1099. }
  1100. length = rgd->rd_length;
  1101. rgrp_blk = bstart - rgd->rd_data0;
  1102. while (blen--) {
  1103. for (buf = 0; buf < length; buf++) {
  1104. bi = rgd->rd_bits + buf;
  1105. if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1106. break;
  1107. }
  1108. gfs2_assert(rgd->rd_sbd, buf < length);
  1109. buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
  1110. rgrp_blk++;
  1111. if (!bi->bi_clone) {
  1112. bi->bi_clone = kmalloc(bi->bi_bh->b_size,
  1113. GFP_NOFS | __GFP_NOFAIL);
  1114. memcpy(bi->bi_clone + bi->bi_offset,
  1115. bi->bi_bh->b_data + bi->bi_offset,
  1116. bi->bi_len);
  1117. }
  1118. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1119. gfs2_setbit(rgd, bi->bi_bh->b_data, NULL, bi->bi_offset,
  1120. bi, buf_blk, new_state);
  1121. }
  1122. return rgd;
  1123. }
  1124. /**
  1125. * gfs2_rgrp_dump - print out an rgrp
  1126. * @seq: The iterator
  1127. * @gl: The glock in question
  1128. *
  1129. */
  1130. int gfs2_rgrp_dump(struct seq_file *seq, const struct gfs2_glock *gl)
  1131. {
  1132. const struct gfs2_rgrpd *rgd = gl->gl_object;
  1133. if (rgd == NULL)
  1134. return 0;
  1135. gfs2_print_dbg(seq, " R: n:%llu f:%02x b:%u/%u i:%u\n",
  1136. (unsigned long long)rgd->rd_addr, rgd->rd_flags,
  1137. rgd->rd_free, rgd->rd_free_clone, rgd->rd_dinodes);
  1138. return 0;
  1139. }
  1140. static void gfs2_rgrp_error(struct gfs2_rgrpd *rgd)
  1141. {
  1142. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1143. fs_warn(sdp, "rgrp %llu has an error, marking it readonly until umount\n",
  1144. (unsigned long long)rgd->rd_addr);
  1145. fs_warn(sdp, "umount on all nodes and run fsck.gfs2 to fix the error\n");
  1146. gfs2_rgrp_dump(NULL, rgd->rd_gl);
  1147. rgd->rd_flags |= GFS2_RDF_ERROR;
  1148. }
  1149. /**
  1150. * gfs2_alloc_block - Allocate one or more blocks
  1151. * @ip: the inode to allocate the block for
  1152. * @bn: Used to return the starting block number
  1153. * @n: requested number of blocks/extent length (value/result)
  1154. *
  1155. * Returns: 0 or error
  1156. */
  1157. int gfs2_alloc_block(struct gfs2_inode *ip, u64 *bn, unsigned int *n)
  1158. {
  1159. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1160. struct buffer_head *dibh;
  1161. struct gfs2_alloc *al = ip->i_alloc;
  1162. struct gfs2_rgrpd *rgd;
  1163. u32 goal, blk;
  1164. u64 block;
  1165. int error;
  1166. /* Only happens if there is a bug in gfs2, return something distinctive
  1167. * to ensure that it is noticed.
  1168. */
  1169. if (al == NULL)
  1170. return -ECANCELED;
  1171. rgd = al->al_rgd;
  1172. if (rgrp_contains_block(rgd, ip->i_goal))
  1173. goal = ip->i_goal - rgd->rd_data0;
  1174. else
  1175. goal = rgd->rd_last_alloc;
  1176. blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n);
  1177. /* Since all blocks are reserved in advance, this shouldn't happen */
  1178. if (blk == BFITNOENT)
  1179. goto rgrp_error;
  1180. rgd->rd_last_alloc = blk;
  1181. block = rgd->rd_data0 + blk;
  1182. ip->i_goal = block;
  1183. error = gfs2_meta_inode_buffer(ip, &dibh);
  1184. if (error == 0) {
  1185. struct gfs2_dinode *di = (struct gfs2_dinode *)dibh->b_data;
  1186. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  1187. di->di_goal_meta = di->di_goal_data = cpu_to_be64(ip->i_goal);
  1188. brelse(dibh);
  1189. }
  1190. if (rgd->rd_free < *n)
  1191. goto rgrp_error;
  1192. rgd->rd_free -= *n;
  1193. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1194. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1195. al->al_alloced += *n;
  1196. gfs2_statfs_change(sdp, 0, -(s64)*n, 0);
  1197. gfs2_quota_change(ip, *n, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1198. rgd->rd_free_clone -= *n;
  1199. trace_gfs2_block_alloc(ip, block, *n, GFS2_BLKST_USED);
  1200. *bn = block;
  1201. return 0;
  1202. rgrp_error:
  1203. gfs2_rgrp_error(rgd);
  1204. return -EIO;
  1205. }
  1206. /**
  1207. * gfs2_alloc_di - Allocate a dinode
  1208. * @dip: the directory that the inode is going in
  1209. * @bn: the block number which is allocated
  1210. * @generation: the generation number of the inode
  1211. *
  1212. * Returns: 0 on success or error
  1213. */
  1214. int gfs2_alloc_di(struct gfs2_inode *dip, u64 *bn, u64 *generation)
  1215. {
  1216. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1217. struct gfs2_alloc *al = dip->i_alloc;
  1218. struct gfs2_rgrpd *rgd = al->al_rgd;
  1219. u32 blk;
  1220. u64 block;
  1221. unsigned int n = 1;
  1222. blk = rgblk_search(rgd, rgd->rd_last_alloc,
  1223. GFS2_BLKST_FREE, GFS2_BLKST_DINODE, &n);
  1224. /* Since all blocks are reserved in advance, this shouldn't happen */
  1225. if (blk == BFITNOENT)
  1226. goto rgrp_error;
  1227. rgd->rd_last_alloc = blk;
  1228. block = rgd->rd_data0 + blk;
  1229. if (rgd->rd_free == 0)
  1230. goto rgrp_error;
  1231. rgd->rd_free--;
  1232. rgd->rd_dinodes++;
  1233. *generation = rgd->rd_igeneration++;
  1234. if (*generation == 0)
  1235. *generation = rgd->rd_igeneration++;
  1236. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1237. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1238. al->al_alloced++;
  1239. gfs2_statfs_change(sdp, 0, -1, +1);
  1240. gfs2_trans_add_unrevoke(sdp, block, 1);
  1241. rgd->rd_free_clone--;
  1242. trace_gfs2_block_alloc(dip, block, 1, GFS2_BLKST_DINODE);
  1243. *bn = block;
  1244. return 0;
  1245. rgrp_error:
  1246. gfs2_rgrp_error(rgd);
  1247. return -EIO;
  1248. }
  1249. /**
  1250. * __gfs2_free_blocks - free a contiguous run of block(s)
  1251. * @ip: the inode these blocks are being freed from
  1252. * @bstart: first block of a run of contiguous blocks
  1253. * @blen: the length of the block run
  1254. * @meta: 1 if the blocks represent metadata
  1255. *
  1256. */
  1257. void __gfs2_free_blocks(struct gfs2_inode *ip, u64 bstart, u32 blen, int meta)
  1258. {
  1259. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1260. struct gfs2_rgrpd *rgd;
  1261. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1262. if (!rgd)
  1263. return;
  1264. trace_gfs2_block_alloc(ip, bstart, blen, GFS2_BLKST_FREE);
  1265. rgd->rd_free += blen;
  1266. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1267. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1268. /* Directories keep their data in the metadata address space */
  1269. if (meta || ip->i_depth)
  1270. gfs2_meta_wipe(ip, bstart, blen);
  1271. }
  1272. /**
  1273. * gfs2_free_meta - free a contiguous run of data block(s)
  1274. * @ip: the inode these blocks are being freed from
  1275. * @bstart: first block of a run of contiguous blocks
  1276. * @blen: the length of the block run
  1277. *
  1278. */
  1279. void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
  1280. {
  1281. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1282. __gfs2_free_blocks(ip, bstart, blen, 1);
  1283. gfs2_statfs_change(sdp, 0, +blen, 0);
  1284. gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1285. }
  1286. void gfs2_unlink_di(struct inode *inode)
  1287. {
  1288. struct gfs2_inode *ip = GFS2_I(inode);
  1289. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1290. struct gfs2_rgrpd *rgd;
  1291. u64 blkno = ip->i_no_addr;
  1292. rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
  1293. if (!rgd)
  1294. return;
  1295. trace_gfs2_block_alloc(ip, blkno, 1, GFS2_BLKST_UNLINKED);
  1296. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1297. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1298. }
  1299. static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
  1300. {
  1301. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1302. struct gfs2_rgrpd *tmp_rgd;
  1303. tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
  1304. if (!tmp_rgd)
  1305. return;
  1306. gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
  1307. if (!rgd->rd_dinodes)
  1308. gfs2_consist_rgrpd(rgd);
  1309. rgd->rd_dinodes--;
  1310. rgd->rd_free++;
  1311. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1312. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1313. gfs2_statfs_change(sdp, 0, +1, -1);
  1314. }
  1315. void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
  1316. {
  1317. gfs2_free_uninit_di(rgd, ip->i_no_addr);
  1318. trace_gfs2_block_alloc(ip, ip->i_no_addr, 1, GFS2_BLKST_FREE);
  1319. gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1320. gfs2_meta_wipe(ip, ip->i_no_addr, 1);
  1321. }
  1322. /**
  1323. * gfs2_check_blk_type - Check the type of a block
  1324. * @sdp: The superblock
  1325. * @no_addr: The block number to check
  1326. * @type: The block type we are looking for
  1327. *
  1328. * Returns: 0 if the block type matches the expected type
  1329. * -ESTALE if it doesn't match
  1330. * or -ve errno if something went wrong while checking
  1331. */
  1332. int gfs2_check_blk_type(struct gfs2_sbd *sdp, u64 no_addr, unsigned int type)
  1333. {
  1334. struct gfs2_rgrpd *rgd;
  1335. struct gfs2_holder ri_gh, rgd_gh;
  1336. struct gfs2_inode *ip = GFS2_I(sdp->sd_rindex);
  1337. int ri_locked = 0;
  1338. int error;
  1339. if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
  1340. error = gfs2_rindex_hold(sdp, &ri_gh);
  1341. if (error)
  1342. goto fail;
  1343. ri_locked = 1;
  1344. }
  1345. error = -EINVAL;
  1346. rgd = gfs2_blk2rgrpd(sdp, no_addr);
  1347. if (!rgd)
  1348. goto fail_rindex;
  1349. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
  1350. if (error)
  1351. goto fail_rindex;
  1352. if (gfs2_get_block_type(rgd, no_addr) != type)
  1353. error = -ESTALE;
  1354. gfs2_glock_dq_uninit(&rgd_gh);
  1355. fail_rindex:
  1356. if (ri_locked)
  1357. gfs2_glock_dq_uninit(&ri_gh);
  1358. fail:
  1359. return error;
  1360. }
  1361. /**
  1362. * gfs2_rlist_add - add a RG to a list of RGs
  1363. * @sdp: the filesystem
  1364. * @rlist: the list of resource groups
  1365. * @block: the block
  1366. *
  1367. * Figure out what RG a block belongs to and add that RG to the list
  1368. *
  1369. * FIXME: Don't use NOFAIL
  1370. *
  1371. */
  1372. void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
  1373. u64 block)
  1374. {
  1375. struct gfs2_rgrpd *rgd;
  1376. struct gfs2_rgrpd **tmp;
  1377. unsigned int new_space;
  1378. unsigned int x;
  1379. if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
  1380. return;
  1381. rgd = gfs2_blk2rgrpd(sdp, block);
  1382. if (!rgd) {
  1383. if (gfs2_consist(sdp))
  1384. fs_err(sdp, "block = %llu\n", (unsigned long long)block);
  1385. return;
  1386. }
  1387. for (x = 0; x < rlist->rl_rgrps; x++)
  1388. if (rlist->rl_rgd[x] == rgd)
  1389. return;
  1390. if (rlist->rl_rgrps == rlist->rl_space) {
  1391. new_space = rlist->rl_space + 10;
  1392. tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
  1393. GFP_NOFS | __GFP_NOFAIL);
  1394. if (rlist->rl_rgd) {
  1395. memcpy(tmp, rlist->rl_rgd,
  1396. rlist->rl_space * sizeof(struct gfs2_rgrpd *));
  1397. kfree(rlist->rl_rgd);
  1398. }
  1399. rlist->rl_space = new_space;
  1400. rlist->rl_rgd = tmp;
  1401. }
  1402. rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
  1403. }
  1404. /**
  1405. * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
  1406. * and initialize an array of glock holders for them
  1407. * @rlist: the list of resource groups
  1408. * @state: the lock state to acquire the RG lock in
  1409. * @flags: the modifier flags for the holder structures
  1410. *
  1411. * FIXME: Don't use NOFAIL
  1412. *
  1413. */
  1414. void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
  1415. {
  1416. unsigned int x;
  1417. rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
  1418. GFP_NOFS | __GFP_NOFAIL);
  1419. for (x = 0; x < rlist->rl_rgrps; x++)
  1420. gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
  1421. state, 0,
  1422. &rlist->rl_ghs[x]);
  1423. }
  1424. /**
  1425. * gfs2_rlist_free - free a resource group list
  1426. * @list: the list of resource groups
  1427. *
  1428. */
  1429. void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
  1430. {
  1431. unsigned int x;
  1432. kfree(rlist->rl_rgd);
  1433. if (rlist->rl_ghs) {
  1434. for (x = 0; x < rlist->rl_rgrps; x++)
  1435. gfs2_holder_uninit(&rlist->rl_ghs[x]);
  1436. kfree(rlist->rl_ghs);
  1437. }
  1438. }