rgrp.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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/lm_interface.h>
  16. #include <linux/prefetch.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_bitfit - Search an rgrp's bitmap buffer to find a bit-pair representing
  115. * a block in a given allocation state.
  116. * @buffer: the buffer that holds the bitmaps
  117. * @buflen: the length (in bytes) of the buffer
  118. * @goal: start search at this block's bit-pair (within @buffer)
  119. * @old_state: GFS2_BLKST_XXX the state of the block we're looking for.
  120. *
  121. * Scope of @goal and returned block number is only within this bitmap buffer,
  122. * not entire rgrp or filesystem. @buffer will be offset from the actual
  123. * beginning of a bitmap block buffer, skipping any header structures.
  124. *
  125. * Return: the block number (bitmap buffer scope) that was found
  126. */
  127. static u32 gfs2_bitfit(const u8 *buffer, unsigned int buflen, u32 goal,
  128. u8 old_state)
  129. {
  130. const u8 *byte, *start, *end;
  131. int bit, startbit;
  132. u32 g1, g2, misaligned;
  133. unsigned long *plong;
  134. unsigned long lskipval;
  135. lskipval = (old_state & GFS2_BLKST_USED) ? LBITSKIP00 : LBITSKIP55;
  136. g1 = (goal / GFS2_NBBY);
  137. start = buffer + g1;
  138. byte = start;
  139. end = buffer + buflen;
  140. g2 = ALIGN(g1, sizeof(unsigned long));
  141. plong = (unsigned long *)(buffer + g2);
  142. startbit = bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
  143. misaligned = g2 - g1;
  144. if (!misaligned)
  145. goto ulong_aligned;
  146. /* parse the bitmap a byte at a time */
  147. misaligned:
  148. while (byte < end) {
  149. if (((*byte >> bit) & GFS2_BIT_MASK) == old_state) {
  150. return goal +
  151. (((byte - start) * GFS2_NBBY) +
  152. ((bit - startbit) >> 1));
  153. }
  154. bit += GFS2_BIT_SIZE;
  155. if (bit >= GFS2_NBBY * GFS2_BIT_SIZE) {
  156. bit = 0;
  157. byte++;
  158. misaligned--;
  159. if (!misaligned) {
  160. plong = (unsigned long *)byte;
  161. goto ulong_aligned;
  162. }
  163. }
  164. }
  165. return BFITNOENT;
  166. /* parse the bitmap a unsigned long at a time */
  167. ulong_aligned:
  168. /* Stop at "end - 1" or else prefetch can go past the end and segfault.
  169. We could "if" it but we'd lose some of the performance gained.
  170. This way will only slow down searching the very last 4/8 bytes
  171. depending on architecture. I've experimented with several ways
  172. of writing this section such as using an else before the goto
  173. but this one seems to be the fastest. */
  174. while ((unsigned char *)plong < end - 1) {
  175. prefetch(plong + 1);
  176. if (((*plong) & LBITMASK) != lskipval)
  177. break;
  178. plong++;
  179. }
  180. if ((unsigned char *)plong < end) {
  181. byte = (const u8 *)plong;
  182. misaligned += sizeof(unsigned long) - 1;
  183. goto misaligned;
  184. }
  185. return BFITNOENT;
  186. }
  187. /**
  188. * gfs2_bitcount - count the number of bits in a certain state
  189. * @buffer: the buffer that holds the bitmaps
  190. * @buflen: the length (in bytes) of the buffer
  191. * @state: the state of the block we're looking for
  192. *
  193. * Returns: The number of bits
  194. */
  195. static u32 gfs2_bitcount(struct gfs2_rgrpd *rgd, const u8 *buffer,
  196. unsigned int buflen, u8 state)
  197. {
  198. const u8 *byte = buffer;
  199. const u8 *end = buffer + buflen;
  200. const u8 state1 = state << 2;
  201. const u8 state2 = state << 4;
  202. const u8 state3 = state << 6;
  203. u32 count = 0;
  204. for (; byte < end; byte++) {
  205. if (((*byte) & 0x03) == state)
  206. count++;
  207. if (((*byte) & 0x0C) == state1)
  208. count++;
  209. if (((*byte) & 0x30) == state2)
  210. count++;
  211. if (((*byte) & 0xC0) == state3)
  212. count++;
  213. }
  214. return count;
  215. }
  216. /**
  217. * gfs2_rgrp_verify - Verify that a resource group is consistent
  218. * @sdp: the filesystem
  219. * @rgd: the rgrp
  220. *
  221. */
  222. void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
  223. {
  224. struct gfs2_sbd *sdp = rgd->rd_sbd;
  225. struct gfs2_bitmap *bi = NULL;
  226. u32 length = rgd->rd_length;
  227. u32 count[4], tmp;
  228. int buf, x;
  229. memset(count, 0, 4 * sizeof(u32));
  230. /* Count # blocks in each of 4 possible allocation states */
  231. for (buf = 0; buf < length; buf++) {
  232. bi = rgd->rd_bits + buf;
  233. for (x = 0; x < 4; x++)
  234. count[x] += gfs2_bitcount(rgd,
  235. bi->bi_bh->b_data +
  236. bi->bi_offset,
  237. bi->bi_len, x);
  238. }
  239. if (count[0] != rgd->rd_rg.rg_free) {
  240. if (gfs2_consist_rgrpd(rgd))
  241. fs_err(sdp, "free data mismatch: %u != %u\n",
  242. count[0], rgd->rd_rg.rg_free);
  243. return;
  244. }
  245. tmp = rgd->rd_data -
  246. rgd->rd_rg.rg_free -
  247. rgd->rd_rg.rg_dinodes;
  248. if (count[1] + count[2] != tmp) {
  249. if (gfs2_consist_rgrpd(rgd))
  250. fs_err(sdp, "used data mismatch: %u != %u\n",
  251. count[1], tmp);
  252. return;
  253. }
  254. if (count[3] != rgd->rd_rg.rg_dinodes) {
  255. if (gfs2_consist_rgrpd(rgd))
  256. fs_err(sdp, "used metadata mismatch: %u != %u\n",
  257. count[3], rgd->rd_rg.rg_dinodes);
  258. return;
  259. }
  260. if (count[2] > count[3]) {
  261. if (gfs2_consist_rgrpd(rgd))
  262. fs_err(sdp, "unlinked inodes > inodes: %u\n",
  263. count[2]);
  264. return;
  265. }
  266. }
  267. static inline int rgrp_contains_block(struct gfs2_rgrpd *rgd, u64 block)
  268. {
  269. u64 first = rgd->rd_data0;
  270. u64 last = first + rgd->rd_data;
  271. return first <= block && block < last;
  272. }
  273. /**
  274. * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
  275. * @sdp: The GFS2 superblock
  276. * @n: The data block number
  277. *
  278. * Returns: The resource group, or NULL if not found
  279. */
  280. struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, u64 blk)
  281. {
  282. struct gfs2_rgrpd *rgd;
  283. spin_lock(&sdp->sd_rindex_spin);
  284. list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
  285. if (rgrp_contains_block(rgd, blk)) {
  286. list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
  287. spin_unlock(&sdp->sd_rindex_spin);
  288. return rgd;
  289. }
  290. }
  291. spin_unlock(&sdp->sd_rindex_spin);
  292. return NULL;
  293. }
  294. /**
  295. * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
  296. * @sdp: The GFS2 superblock
  297. *
  298. * Returns: The first rgrp in the filesystem
  299. */
  300. struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
  301. {
  302. gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
  303. return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
  304. }
  305. /**
  306. * gfs2_rgrpd_get_next - get the next RG
  307. * @rgd: A RG
  308. *
  309. * Returns: The next rgrp
  310. */
  311. struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
  312. {
  313. if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
  314. return NULL;
  315. return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
  316. }
  317. static void clear_rgrpdi(struct gfs2_sbd *sdp)
  318. {
  319. struct list_head *head;
  320. struct gfs2_rgrpd *rgd;
  321. struct gfs2_glock *gl;
  322. spin_lock(&sdp->sd_rindex_spin);
  323. sdp->sd_rindex_forward = NULL;
  324. head = &sdp->sd_rindex_recent_list;
  325. while (!list_empty(head)) {
  326. rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
  327. list_del(&rgd->rd_recent);
  328. }
  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_di.di_size)
  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_di.di_size;
  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_di.di_size)
  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. struct gfs2_rgrp_host *rg = &rgd->rd_rg;
  606. u32 rg_flags;
  607. rg_flags = be32_to_cpu(str->rg_flags);
  608. if (rg_flags & GFS2_RGF_NOALLOC)
  609. rgd->rd_flags |= GFS2_RDF_NOALLOC;
  610. else
  611. rgd->rd_flags &= ~GFS2_RDF_NOALLOC;
  612. rg->rg_free = be32_to_cpu(str->rg_free);
  613. rg->rg_dinodes = be32_to_cpu(str->rg_dinodes);
  614. rg->rg_igeneration = be64_to_cpu(str->rg_igeneration);
  615. }
  616. static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
  617. {
  618. struct gfs2_rgrp *str = buf;
  619. struct gfs2_rgrp_host *rg = &rgd->rd_rg;
  620. u32 rg_flags = 0;
  621. if (rgd->rd_flags & GFS2_RDF_NOALLOC)
  622. rg_flags |= GFS2_RGF_NOALLOC;
  623. str->rg_flags = cpu_to_be32(rg_flags);
  624. str->rg_free = cpu_to_be32(rg->rg_free);
  625. str->rg_dinodes = cpu_to_be32(rg->rg_dinodes);
  626. str->__pad = cpu_to_be32(0);
  627. str->rg_igeneration = cpu_to_be64(rg->rg_igeneration);
  628. memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
  629. }
  630. /**
  631. * gfs2_rgrp_bh_get - 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_bh_get(struct gfs2_rgrpd *rgd)
  640. {
  641. struct gfs2_sbd *sdp = rgd->rd_sbd;
  642. struct gfs2_glock *gl = rgd->rd_gl;
  643. unsigned int length = rgd->rd_length;
  644. struct gfs2_bitmap *bi;
  645. unsigned int x, y;
  646. int error;
  647. mutex_lock(&rgd->rd_mutex);
  648. spin_lock(&sdp->sd_rindex_spin);
  649. if (rgd->rd_bh_count) {
  650. rgd->rd_bh_count++;
  651. spin_unlock(&sdp->sd_rindex_spin);
  652. mutex_unlock(&rgd->rd_mutex);
  653. return 0;
  654. }
  655. spin_unlock(&sdp->sd_rindex_spin);
  656. for (x = 0; x < length; x++) {
  657. bi = rgd->rd_bits + x;
  658. error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
  659. if (error)
  660. goto fail;
  661. }
  662. for (y = length; y--;) {
  663. bi = rgd->rd_bits + y;
  664. error = gfs2_meta_wait(sdp, bi->bi_bh);
  665. if (error)
  666. goto fail;
  667. if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
  668. GFS2_METATYPE_RG)) {
  669. error = -EIO;
  670. goto fail;
  671. }
  672. }
  673. if (!(rgd->rd_flags & GFS2_RDF_UPTODATE)) {
  674. gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
  675. rgd->rd_flags |= GFS2_RDF_UPTODATE;
  676. }
  677. spin_lock(&sdp->sd_rindex_spin);
  678. rgd->rd_free_clone = rgd->rd_rg.rg_free;
  679. rgd->rd_bh_count++;
  680. spin_unlock(&sdp->sd_rindex_spin);
  681. mutex_unlock(&rgd->rd_mutex);
  682. return 0;
  683. fail:
  684. while (x--) {
  685. bi = rgd->rd_bits + x;
  686. brelse(bi->bi_bh);
  687. bi->bi_bh = NULL;
  688. gfs2_assert_warn(sdp, !bi->bi_clone);
  689. }
  690. mutex_unlock(&rgd->rd_mutex);
  691. return error;
  692. }
  693. void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
  694. {
  695. struct gfs2_sbd *sdp = rgd->rd_sbd;
  696. spin_lock(&sdp->sd_rindex_spin);
  697. gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
  698. rgd->rd_bh_count++;
  699. spin_unlock(&sdp->sd_rindex_spin);
  700. }
  701. /**
  702. * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
  703. * @rgd: the struct gfs2_rgrpd describing the RG to read in
  704. *
  705. */
  706. void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
  707. {
  708. struct gfs2_sbd *sdp = rgd->rd_sbd;
  709. int x, length = rgd->rd_length;
  710. spin_lock(&sdp->sd_rindex_spin);
  711. gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
  712. if (--rgd->rd_bh_count) {
  713. spin_unlock(&sdp->sd_rindex_spin);
  714. return;
  715. }
  716. for (x = 0; x < length; x++) {
  717. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  718. kfree(bi->bi_clone);
  719. bi->bi_clone = NULL;
  720. brelse(bi->bi_bh);
  721. bi->bi_bh = NULL;
  722. }
  723. spin_unlock(&sdp->sd_rindex_spin);
  724. }
  725. void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
  726. {
  727. struct gfs2_sbd *sdp = rgd->rd_sbd;
  728. unsigned int length = rgd->rd_length;
  729. unsigned int x;
  730. for (x = 0; x < length; x++) {
  731. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  732. if (!bi->bi_clone)
  733. continue;
  734. memcpy(bi->bi_clone + bi->bi_offset,
  735. bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
  736. }
  737. spin_lock(&sdp->sd_rindex_spin);
  738. rgd->rd_free_clone = rgd->rd_rg.rg_free;
  739. spin_unlock(&sdp->sd_rindex_spin);
  740. }
  741. /**
  742. * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
  743. * @ip: the incore GFS2 inode structure
  744. *
  745. * Returns: the struct gfs2_alloc
  746. */
  747. struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
  748. {
  749. BUG_ON(ip->i_alloc != NULL);
  750. ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_KERNEL);
  751. return ip->i_alloc;
  752. }
  753. /**
  754. * try_rgrp_fit - See if a given reservation will fit in a given RG
  755. * @rgd: the RG data
  756. * @al: the struct gfs2_alloc structure describing the reservation
  757. *
  758. * If there's room for the requested blocks to be allocated from the RG:
  759. * Sets the $al_rgd field in @al.
  760. *
  761. * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
  762. */
  763. static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
  764. {
  765. struct gfs2_sbd *sdp = rgd->rd_sbd;
  766. int ret = 0;
  767. if (rgd->rd_flags & GFS2_RDF_NOALLOC)
  768. return 0;
  769. spin_lock(&sdp->sd_rindex_spin);
  770. if (rgd->rd_free_clone >= al->al_requested) {
  771. al->al_rgd = rgd;
  772. ret = 1;
  773. }
  774. spin_unlock(&sdp->sd_rindex_spin);
  775. return ret;
  776. }
  777. /**
  778. * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
  779. * @rgd: The rgrp
  780. *
  781. * Returns: The inode, if one has been found
  782. */
  783. static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
  784. {
  785. struct inode *inode;
  786. u32 goal = 0, block;
  787. u64 no_addr;
  788. struct gfs2_sbd *sdp = rgd->rd_sbd;
  789. unsigned int n;
  790. for(;;) {
  791. if (goal >= rgd->rd_data)
  792. break;
  793. down_write(&sdp->sd_log_flush_lock);
  794. n = 1;
  795. block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
  796. GFS2_BLKST_UNLINKED, &n);
  797. up_write(&sdp->sd_log_flush_lock);
  798. if (block == BFITNOENT)
  799. break;
  800. /* rgblk_search can return a block < goal, so we need to
  801. keep it marching forward. */
  802. no_addr = block + rgd->rd_data0;
  803. goal++;
  804. if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
  805. continue;
  806. *last_unlinked = no_addr;
  807. inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, DT_UNKNOWN,
  808. no_addr, -1, 1);
  809. if (!IS_ERR(inode))
  810. return inode;
  811. }
  812. rgd->rd_flags &= ~GFS2_RDF_CHECK;
  813. return NULL;
  814. }
  815. /**
  816. * recent_rgrp_first - get first RG from "recent" list
  817. * @sdp: The GFS2 superblock
  818. * @rglast: address of the rgrp used last
  819. *
  820. * Returns: The first rgrp in the recent list
  821. */
  822. static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
  823. u64 rglast)
  824. {
  825. struct gfs2_rgrpd *rgd;
  826. spin_lock(&sdp->sd_rindex_spin);
  827. if (rglast) {
  828. list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
  829. if (rgrp_contains_block(rgd, rglast))
  830. goto out;
  831. }
  832. }
  833. rgd = NULL;
  834. if (!list_empty(&sdp->sd_rindex_recent_list))
  835. rgd = list_entry(sdp->sd_rindex_recent_list.next,
  836. struct gfs2_rgrpd, rd_recent);
  837. out:
  838. spin_unlock(&sdp->sd_rindex_spin);
  839. return rgd;
  840. }
  841. /**
  842. * recent_rgrp_next - get next RG from "recent" list
  843. * @cur_rgd: current rgrp
  844. * @remove:
  845. *
  846. * Returns: The next rgrp in the recent list
  847. */
  848. static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
  849. int remove)
  850. {
  851. struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
  852. struct list_head *head;
  853. struct gfs2_rgrpd *rgd;
  854. spin_lock(&sdp->sd_rindex_spin);
  855. head = &sdp->sd_rindex_recent_list;
  856. list_for_each_entry(rgd, head, rd_recent) {
  857. if (rgd == cur_rgd) {
  858. if (cur_rgd->rd_recent.next != head)
  859. rgd = list_entry(cur_rgd->rd_recent.next,
  860. struct gfs2_rgrpd, rd_recent);
  861. else
  862. rgd = NULL;
  863. if (remove)
  864. list_del(&cur_rgd->rd_recent);
  865. goto out;
  866. }
  867. }
  868. rgd = NULL;
  869. if (!list_empty(head))
  870. rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
  871. out:
  872. spin_unlock(&sdp->sd_rindex_spin);
  873. return rgd;
  874. }
  875. /**
  876. * recent_rgrp_add - add an RG to tail of "recent" list
  877. * @new_rgd: The rgrp to add
  878. *
  879. */
  880. static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
  881. {
  882. struct gfs2_sbd *sdp = new_rgd->rd_sbd;
  883. struct gfs2_rgrpd *rgd;
  884. unsigned int count = 0;
  885. unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
  886. spin_lock(&sdp->sd_rindex_spin);
  887. list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
  888. if (rgd == new_rgd)
  889. goto out;
  890. if (++count >= max)
  891. goto out;
  892. }
  893. list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
  894. out:
  895. spin_unlock(&sdp->sd_rindex_spin);
  896. }
  897. /**
  898. * forward_rgrp_get - get an rgrp to try next from full list
  899. * @sdp: The GFS2 superblock
  900. *
  901. * Returns: The rgrp to try next
  902. */
  903. static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
  904. {
  905. struct gfs2_rgrpd *rgd;
  906. unsigned int journals = gfs2_jindex_size(sdp);
  907. unsigned int rg = 0, x;
  908. spin_lock(&sdp->sd_rindex_spin);
  909. rgd = sdp->sd_rindex_forward;
  910. if (!rgd) {
  911. if (sdp->sd_rgrps >= journals)
  912. rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
  913. for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
  914. x++, rgd = gfs2_rgrpd_get_next(rgd))
  915. /* Do Nothing */;
  916. sdp->sd_rindex_forward = rgd;
  917. }
  918. spin_unlock(&sdp->sd_rindex_spin);
  919. return rgd;
  920. }
  921. /**
  922. * forward_rgrp_set - set the forward rgrp pointer
  923. * @sdp: the filesystem
  924. * @rgd: The new forward rgrp
  925. *
  926. */
  927. static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
  928. {
  929. spin_lock(&sdp->sd_rindex_spin);
  930. sdp->sd_rindex_forward = rgd;
  931. spin_unlock(&sdp->sd_rindex_spin);
  932. }
  933. /**
  934. * get_local_rgrp - Choose and lock a rgrp for allocation
  935. * @ip: the inode to reserve space for
  936. * @rgp: the chosen and locked rgrp
  937. *
  938. * Try to acquire rgrp in way which avoids contending with others.
  939. *
  940. * Returns: errno
  941. */
  942. static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
  943. {
  944. struct inode *inode = NULL;
  945. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  946. struct gfs2_rgrpd *rgd, *begin = NULL;
  947. struct gfs2_alloc *al = ip->i_alloc;
  948. int flags = LM_FLAG_TRY;
  949. int skipped = 0;
  950. int loops = 0;
  951. int error, rg_locked;
  952. /* Try recently successful rgrps */
  953. rgd = recent_rgrp_first(sdp, ip->i_goal);
  954. while (rgd) {
  955. rg_locked = 0;
  956. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  957. rg_locked = 1;
  958. error = 0;
  959. } else {
  960. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
  961. LM_FLAG_TRY, &al->al_rgd_gh);
  962. }
  963. switch (error) {
  964. case 0:
  965. if (try_rgrp_fit(rgd, al))
  966. goto out;
  967. if (rgd->rd_flags & GFS2_RDF_CHECK)
  968. inode = try_rgrp_unlink(rgd, last_unlinked);
  969. if (!rg_locked)
  970. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  971. if (inode)
  972. return inode;
  973. rgd = recent_rgrp_next(rgd, 1);
  974. break;
  975. case GLR_TRYFAILED:
  976. rgd = recent_rgrp_next(rgd, 0);
  977. break;
  978. default:
  979. return ERR_PTR(error);
  980. }
  981. }
  982. /* Go through full list of rgrps */
  983. begin = rgd = forward_rgrp_get(sdp);
  984. for (;;) {
  985. rg_locked = 0;
  986. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  987. rg_locked = 1;
  988. error = 0;
  989. } else {
  990. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
  991. &al->al_rgd_gh);
  992. }
  993. switch (error) {
  994. case 0:
  995. if (try_rgrp_fit(rgd, al))
  996. goto out;
  997. if (rgd->rd_flags & GFS2_RDF_CHECK)
  998. inode = try_rgrp_unlink(rgd, last_unlinked);
  999. if (!rg_locked)
  1000. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  1001. if (inode)
  1002. return inode;
  1003. break;
  1004. case GLR_TRYFAILED:
  1005. skipped++;
  1006. break;
  1007. default:
  1008. return ERR_PTR(error);
  1009. }
  1010. rgd = gfs2_rgrpd_get_next(rgd);
  1011. if (!rgd)
  1012. rgd = gfs2_rgrpd_get_first(sdp);
  1013. if (rgd == begin) {
  1014. if (++loops >= 3)
  1015. return ERR_PTR(-ENOSPC);
  1016. if (!skipped)
  1017. loops++;
  1018. flags = 0;
  1019. if (loops == 2)
  1020. gfs2_log_flush(sdp, NULL);
  1021. }
  1022. }
  1023. out:
  1024. if (begin) {
  1025. recent_rgrp_add(rgd);
  1026. rgd = gfs2_rgrpd_get_next(rgd);
  1027. if (!rgd)
  1028. rgd = gfs2_rgrpd_get_first(sdp);
  1029. forward_rgrp_set(sdp, rgd);
  1030. }
  1031. return NULL;
  1032. }
  1033. /**
  1034. * gfs2_inplace_reserve_i - Reserve space in the filesystem
  1035. * @ip: the inode to reserve space for
  1036. *
  1037. * Returns: errno
  1038. */
  1039. int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
  1040. {
  1041. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1042. struct gfs2_alloc *al = ip->i_alloc;
  1043. struct inode *inode;
  1044. int error = 0;
  1045. u64 last_unlinked = NO_BLOCK;
  1046. if (gfs2_assert_warn(sdp, al->al_requested))
  1047. return -EINVAL;
  1048. try_again:
  1049. /* We need to hold the rindex unless the inode we're using is
  1050. the rindex itself, in which case it's already held. */
  1051. if (ip != GFS2_I(sdp->sd_rindex))
  1052. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  1053. else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
  1054. error = gfs2_ri_update_special(ip);
  1055. if (error)
  1056. return error;
  1057. inode = get_local_rgrp(ip, &last_unlinked);
  1058. if (inode) {
  1059. if (ip != GFS2_I(sdp->sd_rindex))
  1060. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1061. if (IS_ERR(inode))
  1062. return PTR_ERR(inode);
  1063. iput(inode);
  1064. gfs2_log_flush(sdp, NULL);
  1065. goto try_again;
  1066. }
  1067. al->al_file = file;
  1068. al->al_line = line;
  1069. return 0;
  1070. }
  1071. /**
  1072. * gfs2_inplace_release - release an inplace reservation
  1073. * @ip: the inode the reservation was taken out on
  1074. *
  1075. * Release a reservation made by gfs2_inplace_reserve().
  1076. */
  1077. void gfs2_inplace_release(struct gfs2_inode *ip)
  1078. {
  1079. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1080. struct gfs2_alloc *al = ip->i_alloc;
  1081. if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
  1082. fs_warn(sdp, "al_alloced = %u, al_requested = %u "
  1083. "al_file = %s, al_line = %u\n",
  1084. al->al_alloced, al->al_requested, al->al_file,
  1085. al->al_line);
  1086. al->al_rgd = NULL;
  1087. if (al->al_rgd_gh.gh_gl)
  1088. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  1089. if (ip != GFS2_I(sdp->sd_rindex))
  1090. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1091. }
  1092. /**
  1093. * gfs2_get_block_type - Check a block in a RG is of given type
  1094. * @rgd: the resource group holding the block
  1095. * @block: the block number
  1096. *
  1097. * Returns: The block type (GFS2_BLKST_*)
  1098. */
  1099. unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
  1100. {
  1101. struct gfs2_bitmap *bi = NULL;
  1102. u32 length, rgrp_block, buf_block;
  1103. unsigned int buf;
  1104. unsigned char type;
  1105. length = rgd->rd_length;
  1106. rgrp_block = block - rgd->rd_data0;
  1107. for (buf = 0; buf < length; buf++) {
  1108. bi = rgd->rd_bits + buf;
  1109. if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1110. break;
  1111. }
  1112. gfs2_assert(rgd->rd_sbd, buf < length);
  1113. buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
  1114. type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
  1115. bi->bi_len, buf_block);
  1116. return type;
  1117. }
  1118. /**
  1119. * rgblk_search - find a block in @old_state, change allocation
  1120. * state to @new_state
  1121. * @rgd: the resource group descriptor
  1122. * @goal: the goal block within the RG (start here to search for avail block)
  1123. * @old_state: GFS2_BLKST_XXX the before-allocation state to find
  1124. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1125. * @n: The extent length
  1126. *
  1127. * Walk rgrp's bitmap to find bits that represent a block in @old_state.
  1128. * Add the found bitmap buffer to the transaction.
  1129. * Set the found bits to @new_state to change block's allocation state.
  1130. *
  1131. * This function never fails, because we wouldn't call it unless we
  1132. * know (from reservation results, etc.) that a block is available.
  1133. *
  1134. * Scope of @goal and returned block is just within rgrp, not the whole
  1135. * filesystem.
  1136. *
  1137. * Returns: the block number allocated
  1138. */
  1139. static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
  1140. unsigned char old_state, unsigned char new_state,
  1141. unsigned int *n)
  1142. {
  1143. struct gfs2_bitmap *bi = NULL;
  1144. const u32 length = rgd->rd_length;
  1145. u32 blk = 0;
  1146. unsigned int buf, x;
  1147. const unsigned int elen = *n;
  1148. const u8 *buffer;
  1149. *n = 0;
  1150. /* Find bitmap block that contains bits for goal block */
  1151. for (buf = 0; buf < length; buf++) {
  1152. bi = rgd->rd_bits + buf;
  1153. if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1154. break;
  1155. }
  1156. gfs2_assert(rgd->rd_sbd, buf < length);
  1157. /* Convert scope of "goal" from rgrp-wide to within found bit block */
  1158. goal -= bi->bi_start * GFS2_NBBY;
  1159. /* Search (up to entire) bitmap in this rgrp for allocatable block.
  1160. "x <= length", instead of "x < length", because we typically start
  1161. the search in the middle of a bit block, but if we can't find an
  1162. allocatable block anywhere else, we want to be able wrap around and
  1163. search in the first part of our first-searched bit block. */
  1164. for (x = 0; x <= length; x++) {
  1165. /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
  1166. bitmaps, so we must search the originals for that. */
  1167. buffer = bi->bi_bh->b_data + bi->bi_offset;
  1168. if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
  1169. buffer = bi->bi_clone + bi->bi_offset;
  1170. blk = gfs2_bitfit(buffer, bi->bi_len, goal, old_state);
  1171. if (blk != BFITNOENT)
  1172. break;
  1173. /* Try next bitmap block (wrap back to rgrp header if at end) */
  1174. buf = (buf + 1) % length;
  1175. bi = rgd->rd_bits + buf;
  1176. goal = 0;
  1177. }
  1178. if (blk != BFITNOENT && old_state != new_state) {
  1179. *n = 1;
  1180. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1181. gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone, bi->bi_offset,
  1182. bi->bi_len, blk, new_state);
  1183. goal = blk;
  1184. while (*n < elen) {
  1185. goal++;
  1186. if (goal >= (bi->bi_len * GFS2_NBBY))
  1187. break;
  1188. if (gfs2_testbit(rgd, buffer, bi->bi_len, goal) !=
  1189. GFS2_BLKST_FREE)
  1190. break;
  1191. gfs2_setbit(rgd, bi->bi_bh->b_data, bi->bi_clone,
  1192. bi->bi_offset, bi->bi_len, goal,
  1193. new_state);
  1194. (*n)++;
  1195. }
  1196. }
  1197. return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
  1198. }
  1199. /**
  1200. * rgblk_free - Change alloc state of given block(s)
  1201. * @sdp: the filesystem
  1202. * @bstart: the start of a run of blocks to free
  1203. * @blen: the length of the block run (all must lie within ONE RG!)
  1204. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1205. *
  1206. * Returns: Resource group containing the block(s)
  1207. */
  1208. static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
  1209. u32 blen, unsigned char new_state)
  1210. {
  1211. struct gfs2_rgrpd *rgd;
  1212. struct gfs2_bitmap *bi = NULL;
  1213. u32 length, rgrp_blk, buf_blk;
  1214. unsigned int buf;
  1215. rgd = gfs2_blk2rgrpd(sdp, bstart);
  1216. if (!rgd) {
  1217. if (gfs2_consist(sdp))
  1218. fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
  1219. return NULL;
  1220. }
  1221. length = rgd->rd_length;
  1222. rgrp_blk = bstart - rgd->rd_data0;
  1223. while (blen--) {
  1224. for (buf = 0; buf < length; buf++) {
  1225. bi = rgd->rd_bits + buf;
  1226. if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1227. break;
  1228. }
  1229. gfs2_assert(rgd->rd_sbd, buf < length);
  1230. buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
  1231. rgrp_blk++;
  1232. if (!bi->bi_clone) {
  1233. bi->bi_clone = kmalloc(bi->bi_bh->b_size,
  1234. GFP_NOFS | __GFP_NOFAIL);
  1235. memcpy(bi->bi_clone + bi->bi_offset,
  1236. bi->bi_bh->b_data + bi->bi_offset,
  1237. bi->bi_len);
  1238. }
  1239. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1240. gfs2_setbit(rgd, bi->bi_bh->b_data, NULL, bi->bi_offset,
  1241. bi->bi_len, buf_blk, new_state);
  1242. }
  1243. return rgd;
  1244. }
  1245. /**
  1246. * gfs2_alloc_block - Allocate a block
  1247. * @ip: the inode to allocate the block for
  1248. *
  1249. * Returns: the allocated block
  1250. */
  1251. u64 gfs2_alloc_block(struct gfs2_inode *ip, unsigned int *n)
  1252. {
  1253. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1254. struct gfs2_alloc *al = ip->i_alloc;
  1255. struct gfs2_rgrpd *rgd = al->al_rgd;
  1256. u32 goal, blk;
  1257. u64 block;
  1258. if (rgrp_contains_block(rgd, ip->i_goal))
  1259. goal = ip->i_goal - rgd->rd_data0;
  1260. else
  1261. goal = rgd->rd_last_alloc;
  1262. blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED, n);
  1263. BUG_ON(blk == BFITNOENT);
  1264. rgd->rd_last_alloc = blk;
  1265. block = rgd->rd_data0 + blk;
  1266. ip->i_goal = block;
  1267. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free >= *n);
  1268. rgd->rd_rg.rg_free -= *n;
  1269. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1270. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1271. al->al_alloced += *n;
  1272. gfs2_statfs_change(sdp, 0, -*n, 0);
  1273. gfs2_quota_change(ip, *n, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1274. spin_lock(&sdp->sd_rindex_spin);
  1275. rgd->rd_free_clone -= *n;
  1276. spin_unlock(&sdp->sd_rindex_spin);
  1277. return block;
  1278. }
  1279. /**
  1280. * gfs2_alloc_di - Allocate a dinode
  1281. * @dip: the directory that the inode is going in
  1282. *
  1283. * Returns: the block allocated
  1284. */
  1285. u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
  1286. {
  1287. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1288. struct gfs2_alloc *al = dip->i_alloc;
  1289. struct gfs2_rgrpd *rgd = al->al_rgd;
  1290. u32 blk;
  1291. u64 block;
  1292. unsigned int n = 1;
  1293. blk = rgblk_search(rgd, rgd->rd_last_alloc,
  1294. GFS2_BLKST_FREE, GFS2_BLKST_DINODE, &n);
  1295. BUG_ON(blk == BFITNOENT);
  1296. rgd->rd_last_alloc = blk;
  1297. block = rgd->rd_data0 + blk;
  1298. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
  1299. rgd->rd_rg.rg_free--;
  1300. rgd->rd_rg.rg_dinodes++;
  1301. *generation = rgd->rd_rg.rg_igeneration++;
  1302. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1303. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1304. al->al_alloced++;
  1305. gfs2_statfs_change(sdp, 0, -1, +1);
  1306. gfs2_trans_add_unrevoke(sdp, block, 1);
  1307. spin_lock(&sdp->sd_rindex_spin);
  1308. rgd->rd_free_clone--;
  1309. spin_unlock(&sdp->sd_rindex_spin);
  1310. return block;
  1311. }
  1312. /**
  1313. * gfs2_free_data - free a contiguous run of data block(s)
  1314. * @ip: the inode these blocks are being freed from
  1315. * @bstart: first block of a run of contiguous blocks
  1316. * @blen: the length of the block run
  1317. *
  1318. */
  1319. void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
  1320. {
  1321. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1322. struct gfs2_rgrpd *rgd;
  1323. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1324. if (!rgd)
  1325. return;
  1326. rgd->rd_rg.rg_free += blen;
  1327. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1328. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1329. gfs2_trans_add_rg(rgd);
  1330. gfs2_statfs_change(sdp, 0, +blen, 0);
  1331. gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1332. }
  1333. /**
  1334. * gfs2_free_meta - free a contiguous run of data block(s)
  1335. * @ip: the inode these blocks are being freed from
  1336. * @bstart: first block of a run of contiguous blocks
  1337. * @blen: the length of the block run
  1338. *
  1339. */
  1340. void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
  1341. {
  1342. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1343. struct gfs2_rgrpd *rgd;
  1344. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1345. if (!rgd)
  1346. return;
  1347. rgd->rd_rg.rg_free += blen;
  1348. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1349. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1350. gfs2_trans_add_rg(rgd);
  1351. gfs2_statfs_change(sdp, 0, +blen, 0);
  1352. gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1353. gfs2_meta_wipe(ip, bstart, blen);
  1354. }
  1355. void gfs2_unlink_di(struct inode *inode)
  1356. {
  1357. struct gfs2_inode *ip = GFS2_I(inode);
  1358. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1359. struct gfs2_rgrpd *rgd;
  1360. u64 blkno = ip->i_no_addr;
  1361. rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
  1362. if (!rgd)
  1363. return;
  1364. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1365. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1366. gfs2_trans_add_rg(rgd);
  1367. }
  1368. static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
  1369. {
  1370. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1371. struct gfs2_rgrpd *tmp_rgd;
  1372. tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
  1373. if (!tmp_rgd)
  1374. return;
  1375. gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
  1376. if (!rgd->rd_rg.rg_dinodes)
  1377. gfs2_consist_rgrpd(rgd);
  1378. rgd->rd_rg.rg_dinodes--;
  1379. rgd->rd_rg.rg_free++;
  1380. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1381. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1382. gfs2_statfs_change(sdp, 0, +1, -1);
  1383. gfs2_trans_add_rg(rgd);
  1384. }
  1385. void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
  1386. {
  1387. gfs2_free_uninit_di(rgd, ip->i_no_addr);
  1388. gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1389. gfs2_meta_wipe(ip, ip->i_no_addr, 1);
  1390. }
  1391. /**
  1392. * gfs2_rlist_add - add a RG to a list of RGs
  1393. * @sdp: the filesystem
  1394. * @rlist: the list of resource groups
  1395. * @block: the block
  1396. *
  1397. * Figure out what RG a block belongs to and add that RG to the list
  1398. *
  1399. * FIXME: Don't use NOFAIL
  1400. *
  1401. */
  1402. void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
  1403. u64 block)
  1404. {
  1405. struct gfs2_rgrpd *rgd;
  1406. struct gfs2_rgrpd **tmp;
  1407. unsigned int new_space;
  1408. unsigned int x;
  1409. if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
  1410. return;
  1411. rgd = gfs2_blk2rgrpd(sdp, block);
  1412. if (!rgd) {
  1413. if (gfs2_consist(sdp))
  1414. fs_err(sdp, "block = %llu\n", (unsigned long long)block);
  1415. return;
  1416. }
  1417. for (x = 0; x < rlist->rl_rgrps; x++)
  1418. if (rlist->rl_rgd[x] == rgd)
  1419. return;
  1420. if (rlist->rl_rgrps == rlist->rl_space) {
  1421. new_space = rlist->rl_space + 10;
  1422. tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
  1423. GFP_NOFS | __GFP_NOFAIL);
  1424. if (rlist->rl_rgd) {
  1425. memcpy(tmp, rlist->rl_rgd,
  1426. rlist->rl_space * sizeof(struct gfs2_rgrpd *));
  1427. kfree(rlist->rl_rgd);
  1428. }
  1429. rlist->rl_space = new_space;
  1430. rlist->rl_rgd = tmp;
  1431. }
  1432. rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
  1433. }
  1434. /**
  1435. * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
  1436. * and initialize an array of glock holders for them
  1437. * @rlist: the list of resource groups
  1438. * @state: the lock state to acquire the RG lock in
  1439. * @flags: the modifier flags for the holder structures
  1440. *
  1441. * FIXME: Don't use NOFAIL
  1442. *
  1443. */
  1444. void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
  1445. {
  1446. unsigned int x;
  1447. rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
  1448. GFP_NOFS | __GFP_NOFAIL);
  1449. for (x = 0; x < rlist->rl_rgrps; x++)
  1450. gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
  1451. state, 0,
  1452. &rlist->rl_ghs[x]);
  1453. }
  1454. /**
  1455. * gfs2_rlist_free - free a resource group list
  1456. * @list: the list of resource groups
  1457. *
  1458. */
  1459. void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
  1460. {
  1461. unsigned int x;
  1462. kfree(rlist->rl_rgd);
  1463. if (rlist->rl_ghs) {
  1464. for (x = 0; x < rlist->rl_rgrps; x++)
  1465. gfs2_holder_uninit(&rlist->rl_ghs[x]);
  1466. kfree(rlist->rl_ghs);
  1467. }
  1468. }