rgrp.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 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 v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/fs.h>
  15. #include <linux/gfs2_ondisk.h>
  16. #include <asm/semaphore.h>
  17. #include "gfs2.h"
  18. #include "lm_interface.h"
  19. #include "incore.h"
  20. #include "bits.h"
  21. #include "glock.h"
  22. #include "glops.h"
  23. #include "lops.h"
  24. #include "meta_io.h"
  25. #include "quota.h"
  26. #include "rgrp.h"
  27. #include "super.h"
  28. #include "trans.h"
  29. #include "ops_file.h"
  30. #include "util.h"
  31. /**
  32. * gfs2_rgrp_verify - Verify that a resource group is consistent
  33. * @sdp: the filesystem
  34. * @rgd: the rgrp
  35. *
  36. */
  37. void gfs2_rgrp_verify(struct gfs2_rgrpd *rgd)
  38. {
  39. struct gfs2_sbd *sdp = rgd->rd_sbd;
  40. struct gfs2_bitmap *bi = NULL;
  41. uint32_t length = rgd->rd_ri.ri_length;
  42. uint32_t count[4], tmp;
  43. int buf, x;
  44. memset(count, 0, 4 * sizeof(uint32_t));
  45. /* Count # blocks in each of 4 possible allocation states */
  46. for (buf = 0; buf < length; buf++) {
  47. bi = rgd->rd_bits + buf;
  48. for (x = 0; x < 4; x++)
  49. count[x] += gfs2_bitcount(rgd,
  50. bi->bi_bh->b_data +
  51. bi->bi_offset,
  52. bi->bi_len, x);
  53. }
  54. if (count[0] != rgd->rd_rg.rg_free) {
  55. if (gfs2_consist_rgrpd(rgd))
  56. fs_err(sdp, "free data mismatch: %u != %u\n",
  57. count[0], rgd->rd_rg.rg_free);
  58. return;
  59. }
  60. tmp = rgd->rd_ri.ri_data -
  61. rgd->rd_rg.rg_free -
  62. rgd->rd_rg.rg_dinodes;
  63. if (count[1] != tmp) {
  64. if (gfs2_consist_rgrpd(rgd))
  65. fs_err(sdp, "used data mismatch: %u != %u\n",
  66. count[1], tmp);
  67. return;
  68. }
  69. if (count[2]) {
  70. if (gfs2_consist_rgrpd(rgd))
  71. fs_err(sdp, "free metadata mismatch: %u != 0\n",
  72. count[2]);
  73. return;
  74. }
  75. if (count[3] != rgd->rd_rg.rg_dinodes) {
  76. if (gfs2_consist_rgrpd(rgd))
  77. fs_err(sdp, "used metadata mismatch: %u != %u\n",
  78. count[3], rgd->rd_rg.rg_dinodes);
  79. return;
  80. }
  81. }
  82. static inline int rgrp_contains_block(struct gfs2_rindex *ri, uint64_t block)
  83. {
  84. uint64_t first = ri->ri_data0;
  85. uint64_t last = first + ri->ri_data;
  86. return !!(first <= block && block < last);
  87. }
  88. /**
  89. * gfs2_blk2rgrpd - Find resource group for a given data/meta block number
  90. * @sdp: The GFS2 superblock
  91. * @n: The data block number
  92. *
  93. * Returns: The resource group, or NULL if not found
  94. */
  95. struct gfs2_rgrpd *gfs2_blk2rgrpd(struct gfs2_sbd *sdp, uint64_t blk)
  96. {
  97. struct gfs2_rgrpd *rgd;
  98. spin_lock(&sdp->sd_rindex_spin);
  99. list_for_each_entry(rgd, &sdp->sd_rindex_mru_list, rd_list_mru) {
  100. if (rgrp_contains_block(&rgd->rd_ri, blk)) {
  101. list_move(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
  102. spin_unlock(&sdp->sd_rindex_spin);
  103. return rgd;
  104. }
  105. }
  106. spin_unlock(&sdp->sd_rindex_spin);
  107. return NULL;
  108. }
  109. /**
  110. * gfs2_rgrpd_get_first - get the first Resource Group in the filesystem
  111. * @sdp: The GFS2 superblock
  112. *
  113. * Returns: The first rgrp in the filesystem
  114. */
  115. struct gfs2_rgrpd *gfs2_rgrpd_get_first(struct gfs2_sbd *sdp)
  116. {
  117. gfs2_assert(sdp, !list_empty(&sdp->sd_rindex_list));
  118. return list_entry(sdp->sd_rindex_list.next, struct gfs2_rgrpd, rd_list);
  119. }
  120. /**
  121. * gfs2_rgrpd_get_next - get the next RG
  122. * @rgd: A RG
  123. *
  124. * Returns: The next rgrp
  125. */
  126. struct gfs2_rgrpd *gfs2_rgrpd_get_next(struct gfs2_rgrpd *rgd)
  127. {
  128. if (rgd->rd_list.next == &rgd->rd_sbd->sd_rindex_list)
  129. return NULL;
  130. return list_entry(rgd->rd_list.next, struct gfs2_rgrpd, rd_list);
  131. }
  132. static void clear_rgrpdi(struct gfs2_sbd *sdp)
  133. {
  134. struct list_head *head;
  135. struct gfs2_rgrpd *rgd;
  136. struct gfs2_glock *gl;
  137. spin_lock(&sdp->sd_rindex_spin);
  138. sdp->sd_rindex_forward = NULL;
  139. head = &sdp->sd_rindex_recent_list;
  140. while (!list_empty(head)) {
  141. rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
  142. list_del(&rgd->rd_recent);
  143. }
  144. spin_unlock(&sdp->sd_rindex_spin);
  145. head = &sdp->sd_rindex_list;
  146. while (!list_empty(head)) {
  147. rgd = list_entry(head->next, struct gfs2_rgrpd, rd_list);
  148. gl = rgd->rd_gl;
  149. list_del(&rgd->rd_list);
  150. list_del(&rgd->rd_list_mru);
  151. if (gl) {
  152. gl->gl_object = NULL;
  153. gfs2_glock_put(gl);
  154. }
  155. kfree(rgd->rd_bits);
  156. kfree(rgd);
  157. }
  158. }
  159. void gfs2_clear_rgrpd(struct gfs2_sbd *sdp)
  160. {
  161. mutex_lock(&sdp->sd_rindex_mutex);
  162. clear_rgrpdi(sdp);
  163. mutex_unlock(&sdp->sd_rindex_mutex);
  164. }
  165. /**
  166. * gfs2_compute_bitstructs - Compute the bitmap sizes
  167. * @rgd: The resource group descriptor
  168. *
  169. * Calculates bitmap descriptors, one for each block that contains bitmap data
  170. *
  171. * Returns: errno
  172. */
  173. static int compute_bitstructs(struct gfs2_rgrpd *rgd)
  174. {
  175. struct gfs2_sbd *sdp = rgd->rd_sbd;
  176. struct gfs2_bitmap *bi;
  177. uint32_t length = rgd->rd_ri.ri_length; /* # blocks in hdr & bitmap */
  178. uint32_t bytes_left, bytes;
  179. int x;
  180. rgd->rd_bits = kcalloc(length, sizeof(struct gfs2_bitmap), GFP_KERNEL);
  181. if (!rgd->rd_bits)
  182. return -ENOMEM;
  183. bytes_left = rgd->rd_ri.ri_bitbytes;
  184. for (x = 0; x < length; x++) {
  185. bi = rgd->rd_bits + x;
  186. /* small rgrp; bitmap stored completely in header block */
  187. if (length == 1) {
  188. bytes = bytes_left;
  189. bi->bi_offset = sizeof(struct gfs2_rgrp);
  190. bi->bi_start = 0;
  191. bi->bi_len = bytes;
  192. /* header block */
  193. } else if (x == 0) {
  194. bytes = sdp->sd_sb.sb_bsize - sizeof(struct gfs2_rgrp);
  195. bi->bi_offset = sizeof(struct gfs2_rgrp);
  196. bi->bi_start = 0;
  197. bi->bi_len = bytes;
  198. /* last block */
  199. } else if (x + 1 == length) {
  200. bytes = bytes_left;
  201. bi->bi_offset = sizeof(struct gfs2_meta_header);
  202. bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
  203. bi->bi_len = bytes;
  204. /* other blocks */
  205. } else {
  206. bytes = sdp->sd_sb.sb_bsize -
  207. sizeof(struct gfs2_meta_header);
  208. bi->bi_offset = sizeof(struct gfs2_meta_header);
  209. bi->bi_start = rgd->rd_ri.ri_bitbytes - bytes_left;
  210. bi->bi_len = bytes;
  211. }
  212. bytes_left -= bytes;
  213. }
  214. if (bytes_left) {
  215. gfs2_consist_rgrpd(rgd);
  216. return -EIO;
  217. }
  218. bi = rgd->rd_bits + (length - 1);
  219. if ((bi->bi_start + bi->bi_len) * GFS2_NBBY != rgd->rd_ri.ri_data) {
  220. if (gfs2_consist_rgrpd(rgd)) {
  221. gfs2_rindex_print(&rgd->rd_ri);
  222. fs_err(sdp, "start=%u len=%u offset=%u\n",
  223. bi->bi_start, bi->bi_len, bi->bi_offset);
  224. }
  225. return -EIO;
  226. }
  227. return 0;
  228. }
  229. /**
  230. * gfs2_ri_update - Pull in a new resource index from the disk
  231. * @gl: The glock covering the rindex inode
  232. *
  233. * Returns: 0 on successful update, error code otherwise
  234. */
  235. static int gfs2_ri_update(struct gfs2_inode *ip)
  236. {
  237. struct gfs2_sbd *sdp = ip->i_sbd;
  238. struct inode *inode = ip->i_vnode;
  239. struct gfs2_rgrpd *rgd;
  240. char buf[sizeof(struct gfs2_rindex)];
  241. struct file_ra_state ra_state;
  242. uint64_t junk = ip->i_di.di_size;
  243. int error;
  244. if (do_div(junk, sizeof(struct gfs2_rindex))) {
  245. gfs2_consist_inode(ip);
  246. return -EIO;
  247. }
  248. clear_rgrpdi(sdp);
  249. file_ra_state_init(&ra_state, inode->i_mapping);
  250. for (sdp->sd_rgrps = 0;; sdp->sd_rgrps++) {
  251. loff_t pos = sdp->sd_rgrps * sizeof(struct gfs2_rindex);
  252. error = gfs2_internal_read(ip, &ra_state, buf, &pos,
  253. sizeof(struct gfs2_rindex));
  254. if (!error)
  255. break;
  256. if (error != sizeof(struct gfs2_rindex)) {
  257. if (error > 0)
  258. error = -EIO;
  259. goto fail;
  260. }
  261. rgd = kzalloc(sizeof(struct gfs2_rgrpd), GFP_KERNEL);
  262. error = -ENOMEM;
  263. if (!rgd)
  264. goto fail;
  265. mutex_init(&rgd->rd_mutex);
  266. lops_init_le(&rgd->rd_le, &gfs2_rg_lops);
  267. rgd->rd_sbd = sdp;
  268. list_add_tail(&rgd->rd_list, &sdp->sd_rindex_list);
  269. list_add_tail(&rgd->rd_list_mru, &sdp->sd_rindex_mru_list);
  270. gfs2_rindex_in(&rgd->rd_ri, buf);
  271. error = compute_bitstructs(rgd);
  272. if (error)
  273. goto fail;
  274. error = gfs2_glock_get(sdp, rgd->rd_ri.ri_addr,
  275. &gfs2_rgrp_glops, CREATE, &rgd->rd_gl);
  276. if (error)
  277. goto fail;
  278. rgd->rd_gl->gl_object = rgd;
  279. rgd->rd_rg_vn = rgd->rd_gl->gl_vn - 1;
  280. }
  281. sdp->sd_rindex_vn = ip->i_gl->gl_vn;
  282. return 0;
  283. fail:
  284. clear_rgrpdi(sdp);
  285. return error;
  286. }
  287. /**
  288. * gfs2_rindex_hold - Grab a lock on the rindex
  289. * @sdp: The GFS2 superblock
  290. * @ri_gh: the glock holder
  291. *
  292. * We grab a lock on the rindex inode to make sure that it doesn't
  293. * change whilst we are performing an operation. We keep this lock
  294. * for quite long periods of time compared to other locks. This
  295. * doesn't matter, since it is shared and it is very, very rarely
  296. * accessed in the exclusive mode (i.e. only when expanding the filesystem).
  297. *
  298. * This makes sure that we're using the latest copy of the resource index
  299. * special file, which might have been updated if someone expanded the
  300. * filesystem (via gfs2_grow utility), which adds new resource groups.
  301. *
  302. * Returns: 0 on success, error code otherwise
  303. */
  304. int gfs2_rindex_hold(struct gfs2_sbd *sdp, struct gfs2_holder *ri_gh)
  305. {
  306. struct gfs2_inode *ip = sdp->sd_rindex->u.generic_ip;
  307. struct gfs2_glock *gl = ip->i_gl;
  308. int error;
  309. error = gfs2_glock_nq_init(gl, LM_ST_SHARED, 0, ri_gh);
  310. if (error)
  311. return error;
  312. /* Read new copy from disk if we don't have the latest */
  313. if (sdp->sd_rindex_vn != gl->gl_vn) {
  314. mutex_lock(&sdp->sd_rindex_mutex);
  315. if (sdp->sd_rindex_vn != gl->gl_vn) {
  316. error = gfs2_ri_update(ip);
  317. if (error)
  318. gfs2_glock_dq_uninit(ri_gh);
  319. }
  320. mutex_unlock(&sdp->sd_rindex_mutex);
  321. }
  322. return error;
  323. }
  324. /**
  325. * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
  326. * @rgd: the struct gfs2_rgrpd describing the RG to read in
  327. *
  328. * Read in all of a Resource Group's header and bitmap blocks.
  329. * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
  330. *
  331. * Returns: errno
  332. */
  333. int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
  334. {
  335. struct gfs2_sbd *sdp = rgd->rd_sbd;
  336. struct gfs2_glock *gl = rgd->rd_gl;
  337. unsigned int length = rgd->rd_ri.ri_length;
  338. struct gfs2_bitmap *bi;
  339. unsigned int x, y;
  340. int error;
  341. mutex_lock(&rgd->rd_mutex);
  342. spin_lock(&sdp->sd_rindex_spin);
  343. if (rgd->rd_bh_count) {
  344. rgd->rd_bh_count++;
  345. spin_unlock(&sdp->sd_rindex_spin);
  346. mutex_unlock(&rgd->rd_mutex);
  347. return 0;
  348. }
  349. spin_unlock(&sdp->sd_rindex_spin);
  350. for (x = 0; x < length; x++) {
  351. bi = rgd->rd_bits + x;
  352. error = gfs2_meta_read(gl, rgd->rd_ri.ri_addr + x, DIO_START,
  353. &bi->bi_bh);
  354. if (error)
  355. goto fail;
  356. }
  357. for (y = length; y--;) {
  358. bi = rgd->rd_bits + y;
  359. error = gfs2_meta_reread(sdp, bi->bi_bh, DIO_WAIT);
  360. if (error)
  361. goto fail;
  362. if (gfs2_metatype_check(sdp, bi->bi_bh,
  363. (y) ? GFS2_METATYPE_RB :
  364. GFS2_METATYPE_RG)) {
  365. error = -EIO;
  366. goto fail;
  367. }
  368. }
  369. if (rgd->rd_rg_vn != gl->gl_vn) {
  370. gfs2_rgrp_in(&rgd->rd_rg, (rgd->rd_bits[0].bi_bh)->b_data);
  371. rgd->rd_rg_vn = gl->gl_vn;
  372. }
  373. spin_lock(&sdp->sd_rindex_spin);
  374. rgd->rd_free_clone = rgd->rd_rg.rg_free;
  375. rgd->rd_bh_count++;
  376. spin_unlock(&sdp->sd_rindex_spin);
  377. mutex_unlock(&rgd->rd_mutex);
  378. return 0;
  379. fail:
  380. while (x--) {
  381. bi = rgd->rd_bits + x;
  382. brelse(bi->bi_bh);
  383. bi->bi_bh = NULL;
  384. gfs2_assert_warn(sdp, !bi->bi_clone);
  385. }
  386. mutex_unlock(&rgd->rd_mutex);
  387. return error;
  388. }
  389. void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
  390. {
  391. struct gfs2_sbd *sdp = rgd->rd_sbd;
  392. spin_lock(&sdp->sd_rindex_spin);
  393. gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
  394. rgd->rd_bh_count++;
  395. spin_unlock(&sdp->sd_rindex_spin);
  396. }
  397. /**
  398. * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
  399. * @rgd: the struct gfs2_rgrpd describing the RG to read in
  400. *
  401. */
  402. void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
  403. {
  404. struct gfs2_sbd *sdp = rgd->rd_sbd;
  405. int x, length = rgd->rd_ri.ri_length;
  406. spin_lock(&sdp->sd_rindex_spin);
  407. gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
  408. if (--rgd->rd_bh_count) {
  409. spin_unlock(&sdp->sd_rindex_spin);
  410. return;
  411. }
  412. for (x = 0; x < length; x++) {
  413. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  414. kfree(bi->bi_clone);
  415. bi->bi_clone = NULL;
  416. brelse(bi->bi_bh);
  417. bi->bi_bh = NULL;
  418. }
  419. spin_unlock(&sdp->sd_rindex_spin);
  420. }
  421. void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
  422. {
  423. struct gfs2_sbd *sdp = rgd->rd_sbd;
  424. unsigned int length = rgd->rd_ri.ri_length;
  425. unsigned int x;
  426. for (x = 0; x < length; x++) {
  427. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  428. if (!bi->bi_clone)
  429. continue;
  430. memcpy(bi->bi_clone + bi->bi_offset,
  431. bi->bi_bh->b_data + bi->bi_offset,
  432. bi->bi_len);
  433. }
  434. spin_lock(&sdp->sd_rindex_spin);
  435. rgd->rd_free_clone = rgd->rd_rg.rg_free;
  436. spin_unlock(&sdp->sd_rindex_spin);
  437. }
  438. /**
  439. * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
  440. * @ip: the incore GFS2 inode structure
  441. *
  442. * Returns: the struct gfs2_alloc
  443. */
  444. struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
  445. {
  446. struct gfs2_alloc *al = &ip->i_alloc;
  447. /* FIXME: Should assert that the correct locks are held here... */
  448. memset(al, 0, sizeof(*al));
  449. return al;
  450. }
  451. /**
  452. * gfs2_alloc_put - throw away the struct gfs2_alloc for an inode
  453. * @ip: the inode
  454. *
  455. */
  456. void gfs2_alloc_put(struct gfs2_inode *ip)
  457. {
  458. return;
  459. }
  460. /**
  461. * try_rgrp_fit - See if a given reservation will fit in a given RG
  462. * @rgd: the RG data
  463. * @al: the struct gfs2_alloc structure describing the reservation
  464. *
  465. * If there's room for the requested blocks to be allocated from the RG:
  466. * Sets the $al_reserved_data field in @al.
  467. * Sets the $al_reserved_meta field in @al.
  468. * Sets the $al_rgd field in @al.
  469. *
  470. * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
  471. */
  472. static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
  473. {
  474. struct gfs2_sbd *sdp = rgd->rd_sbd;
  475. int ret = 0;
  476. spin_lock(&sdp->sd_rindex_spin);
  477. if (rgd->rd_free_clone >= al->al_requested) {
  478. al->al_rgd = rgd;
  479. ret = 1;
  480. }
  481. spin_unlock(&sdp->sd_rindex_spin);
  482. return ret;
  483. }
  484. /**
  485. * recent_rgrp_first - get first RG from "recent" list
  486. * @sdp: The GFS2 superblock
  487. * @rglast: address of the rgrp used last
  488. *
  489. * Returns: The first rgrp in the recent list
  490. */
  491. static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
  492. uint64_t rglast)
  493. {
  494. struct gfs2_rgrpd *rgd = NULL;
  495. spin_lock(&sdp->sd_rindex_spin);
  496. if (list_empty(&sdp->sd_rindex_recent_list))
  497. goto out;
  498. if (!rglast)
  499. goto first;
  500. list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
  501. if (rgd->rd_ri.ri_addr == rglast)
  502. goto out;
  503. }
  504. first:
  505. rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
  506. rd_recent);
  507. out:
  508. spin_unlock(&sdp->sd_rindex_spin);
  509. return rgd;
  510. }
  511. /**
  512. * recent_rgrp_next - get next RG from "recent" list
  513. * @cur_rgd: current rgrp
  514. * @remove:
  515. *
  516. * Returns: The next rgrp in the recent list
  517. */
  518. static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
  519. int remove)
  520. {
  521. struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
  522. struct list_head *head;
  523. struct gfs2_rgrpd *rgd;
  524. spin_lock(&sdp->sd_rindex_spin);
  525. head = &sdp->sd_rindex_recent_list;
  526. list_for_each_entry(rgd, head, rd_recent) {
  527. if (rgd == cur_rgd) {
  528. if (cur_rgd->rd_recent.next != head)
  529. rgd = list_entry(cur_rgd->rd_recent.next,
  530. struct gfs2_rgrpd, rd_recent);
  531. else
  532. rgd = NULL;
  533. if (remove)
  534. list_del(&cur_rgd->rd_recent);
  535. goto out;
  536. }
  537. }
  538. rgd = NULL;
  539. if (!list_empty(head))
  540. rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
  541. out:
  542. spin_unlock(&sdp->sd_rindex_spin);
  543. return rgd;
  544. }
  545. /**
  546. * recent_rgrp_add - add an RG to tail of "recent" list
  547. * @new_rgd: The rgrp to add
  548. *
  549. */
  550. static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
  551. {
  552. struct gfs2_sbd *sdp = new_rgd->rd_sbd;
  553. struct gfs2_rgrpd *rgd;
  554. unsigned int count = 0;
  555. unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
  556. spin_lock(&sdp->sd_rindex_spin);
  557. list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
  558. if (rgd == new_rgd)
  559. goto out;
  560. if (++count >= max)
  561. goto out;
  562. }
  563. list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
  564. out:
  565. spin_unlock(&sdp->sd_rindex_spin);
  566. }
  567. /**
  568. * forward_rgrp_get - get an rgrp to try next from full list
  569. * @sdp: The GFS2 superblock
  570. *
  571. * Returns: The rgrp to try next
  572. */
  573. static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
  574. {
  575. struct gfs2_rgrpd *rgd;
  576. unsigned int journals = gfs2_jindex_size(sdp);
  577. unsigned int rg = 0, x;
  578. spin_lock(&sdp->sd_rindex_spin);
  579. rgd = sdp->sd_rindex_forward;
  580. if (!rgd) {
  581. if (sdp->sd_rgrps >= journals)
  582. rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
  583. for (x = 0, rgd = gfs2_rgrpd_get_first(sdp);
  584. x < rg;
  585. x++, rgd = gfs2_rgrpd_get_next(rgd))
  586. /* Do Nothing */;
  587. sdp->sd_rindex_forward = rgd;
  588. }
  589. spin_unlock(&sdp->sd_rindex_spin);
  590. return rgd;
  591. }
  592. /**
  593. * forward_rgrp_set - set the forward rgrp pointer
  594. * @sdp: the filesystem
  595. * @rgd: The new forward rgrp
  596. *
  597. */
  598. static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
  599. {
  600. spin_lock(&sdp->sd_rindex_spin);
  601. sdp->sd_rindex_forward = rgd;
  602. spin_unlock(&sdp->sd_rindex_spin);
  603. }
  604. /**
  605. * get_local_rgrp - Choose and lock a rgrp for allocation
  606. * @ip: the inode to reserve space for
  607. * @rgp: the chosen and locked rgrp
  608. *
  609. * Try to acquire rgrp in way which avoids contending with others.
  610. *
  611. * Returns: errno
  612. */
  613. static int get_local_rgrp(struct gfs2_inode *ip)
  614. {
  615. struct gfs2_sbd *sdp = ip->i_sbd;
  616. struct gfs2_rgrpd *rgd, *begin = NULL;
  617. struct gfs2_alloc *al = &ip->i_alloc;
  618. int flags = LM_FLAG_TRY;
  619. int skipped = 0;
  620. int loops = 0;
  621. int error;
  622. /* Try recently successful rgrps */
  623. rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
  624. while (rgd) {
  625. error = gfs2_glock_nq_init(rgd->rd_gl,
  626. LM_ST_EXCLUSIVE, LM_FLAG_TRY,
  627. &al->al_rgd_gh);
  628. switch (error) {
  629. case 0:
  630. if (try_rgrp_fit(rgd, al))
  631. goto out;
  632. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  633. rgd = recent_rgrp_next(rgd, 1);
  634. break;
  635. case GLR_TRYFAILED:
  636. rgd = recent_rgrp_next(rgd, 0);
  637. break;
  638. default:
  639. return error;
  640. }
  641. }
  642. /* Go through full list of rgrps */
  643. begin = rgd = forward_rgrp_get(sdp);
  644. for (;;) {
  645. error = gfs2_glock_nq_init(rgd->rd_gl,
  646. LM_ST_EXCLUSIVE, flags,
  647. &al->al_rgd_gh);
  648. switch (error) {
  649. case 0:
  650. if (try_rgrp_fit(rgd, al))
  651. goto out;
  652. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  653. break;
  654. case GLR_TRYFAILED:
  655. skipped++;
  656. break;
  657. default:
  658. return error;
  659. }
  660. rgd = gfs2_rgrpd_get_next(rgd);
  661. if (!rgd)
  662. rgd = gfs2_rgrpd_get_first(sdp);
  663. if (rgd == begin) {
  664. if (++loops >= 2 || !skipped)
  665. return -ENOSPC;
  666. flags = 0;
  667. }
  668. }
  669. out:
  670. ip->i_last_rg_alloc = rgd->rd_ri.ri_addr;
  671. if (begin) {
  672. recent_rgrp_add(rgd);
  673. rgd = gfs2_rgrpd_get_next(rgd);
  674. if (!rgd)
  675. rgd = gfs2_rgrpd_get_first(sdp);
  676. forward_rgrp_set(sdp, rgd);
  677. }
  678. return 0;
  679. }
  680. /**
  681. * gfs2_inplace_reserve_i - Reserve space in the filesystem
  682. * @ip: the inode to reserve space for
  683. *
  684. * Returns: errno
  685. */
  686. int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
  687. {
  688. struct gfs2_sbd *sdp = ip->i_sbd;
  689. struct gfs2_alloc *al = &ip->i_alloc;
  690. int error;
  691. if (gfs2_assert_warn(sdp, al->al_requested))
  692. return -EINVAL;
  693. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  694. if (error)
  695. return error;
  696. error = get_local_rgrp(ip);
  697. if (error) {
  698. gfs2_glock_dq_uninit(&al->al_ri_gh);
  699. return error;
  700. }
  701. al->al_file = file;
  702. al->al_line = line;
  703. return 0;
  704. }
  705. /**
  706. * gfs2_inplace_release - release an inplace reservation
  707. * @ip: the inode the reservation was taken out on
  708. *
  709. * Release a reservation made by gfs2_inplace_reserve().
  710. */
  711. void gfs2_inplace_release(struct gfs2_inode *ip)
  712. {
  713. struct gfs2_sbd *sdp = ip->i_sbd;
  714. struct gfs2_alloc *al = &ip->i_alloc;
  715. if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
  716. fs_warn(sdp, "al_alloced = %u, al_requested = %u "
  717. "al_file = %s, al_line = %u\n",
  718. al->al_alloced, al->al_requested, al->al_file,
  719. al->al_line);
  720. al->al_rgd = NULL;
  721. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  722. gfs2_glock_dq_uninit(&al->al_ri_gh);
  723. }
  724. /**
  725. * gfs2_get_block_type - Check a block in a RG is of given type
  726. * @rgd: the resource group holding the block
  727. * @block: the block number
  728. *
  729. * Returns: The block type (GFS2_BLKST_*)
  730. */
  731. unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, uint64_t block)
  732. {
  733. struct gfs2_bitmap *bi = NULL;
  734. uint32_t length, rgrp_block, buf_block;
  735. unsigned int buf;
  736. unsigned char type;
  737. length = rgd->rd_ri.ri_length;
  738. rgrp_block = block - rgd->rd_ri.ri_data0;
  739. for (buf = 0; buf < length; buf++) {
  740. bi = rgd->rd_bits + buf;
  741. if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  742. break;
  743. }
  744. gfs2_assert(rgd->rd_sbd, buf < length);
  745. buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
  746. type = gfs2_testbit(rgd,
  747. bi->bi_bh->b_data + bi->bi_offset,
  748. bi->bi_len, buf_block);
  749. return type;
  750. }
  751. /**
  752. * rgblk_search - find a block in @old_state, change allocation
  753. * state to @new_state
  754. * @rgd: the resource group descriptor
  755. * @goal: the goal block within the RG (start here to search for avail block)
  756. * @old_state: GFS2_BLKST_XXX the before-allocation state to find
  757. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  758. *
  759. * Walk rgrp's bitmap to find bits that represent a block in @old_state.
  760. * Add the found bitmap buffer to the transaction.
  761. * Set the found bits to @new_state to change block's allocation state.
  762. *
  763. * This function never fails, because we wouldn't call it unless we
  764. * know (from reservation results, etc.) that a block is available.
  765. *
  766. * Scope of @goal and returned block is just within rgrp, not the whole
  767. * filesystem.
  768. *
  769. * Returns: the block number allocated
  770. */
  771. static uint32_t rgblk_search(struct gfs2_rgrpd *rgd, uint32_t goal,
  772. unsigned char old_state, unsigned char new_state)
  773. {
  774. struct gfs2_bitmap *bi = NULL;
  775. uint32_t length = rgd->rd_ri.ri_length;
  776. uint32_t blk = 0;
  777. unsigned int buf, x;
  778. /* Find bitmap block that contains bits for goal block */
  779. for (buf = 0; buf < length; buf++) {
  780. bi = rgd->rd_bits + buf;
  781. if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  782. break;
  783. }
  784. gfs2_assert(rgd->rd_sbd, buf < length);
  785. /* Convert scope of "goal" from rgrp-wide to within found bit block */
  786. goal -= bi->bi_start * GFS2_NBBY;
  787. /* Search (up to entire) bitmap in this rgrp for allocatable block.
  788. "x <= length", instead of "x < length", because we typically start
  789. the search in the middle of a bit block, but if we can't find an
  790. allocatable block anywhere else, we want to be able wrap around and
  791. search in the first part of our first-searched bit block. */
  792. for (x = 0; x <= length; x++) {
  793. if (bi->bi_clone)
  794. blk = gfs2_bitfit(rgd,
  795. bi->bi_clone + bi->bi_offset,
  796. bi->bi_len, goal, old_state);
  797. else
  798. blk = gfs2_bitfit(rgd,
  799. bi->bi_bh->b_data + bi->bi_offset,
  800. bi->bi_len, goal, old_state);
  801. if (blk != BFITNOENT)
  802. break;
  803. /* Try next bitmap block (wrap back to rgrp header if at end) */
  804. buf = (buf + 1) % length;
  805. bi = rgd->rd_bits + buf;
  806. goal = 0;
  807. }
  808. if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
  809. blk = 0;
  810. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  811. gfs2_setbit(rgd,
  812. bi->bi_bh->b_data + bi->bi_offset,
  813. bi->bi_len, blk, new_state);
  814. if (bi->bi_clone)
  815. gfs2_setbit(rgd,
  816. bi->bi_clone + bi->bi_offset,
  817. bi->bi_len, blk, new_state);
  818. return bi->bi_start * GFS2_NBBY + blk;
  819. }
  820. /**
  821. * rgblk_free - Change alloc state of given block(s)
  822. * @sdp: the filesystem
  823. * @bstart: the start of a run of blocks to free
  824. * @blen: the length of the block run (all must lie within ONE RG!)
  825. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  826. *
  827. * Returns: Resource group containing the block(s)
  828. */
  829. static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, uint64_t bstart,
  830. uint32_t blen, unsigned char new_state)
  831. {
  832. struct gfs2_rgrpd *rgd;
  833. struct gfs2_bitmap *bi = NULL;
  834. uint32_t length, rgrp_blk, buf_blk;
  835. unsigned int buf;
  836. rgd = gfs2_blk2rgrpd(sdp, bstart);
  837. if (!rgd) {
  838. if (gfs2_consist(sdp))
  839. fs_err(sdp, "block = %llu\n", bstart);
  840. return NULL;
  841. }
  842. length = rgd->rd_ri.ri_length;
  843. rgrp_blk = bstart - rgd->rd_ri.ri_data0;
  844. while (blen--) {
  845. for (buf = 0; buf < length; buf++) {
  846. bi = rgd->rd_bits + buf;
  847. if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  848. break;
  849. }
  850. gfs2_assert(rgd->rd_sbd, buf < length);
  851. buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
  852. rgrp_blk++;
  853. if (!bi->bi_clone) {
  854. bi->bi_clone = kmalloc(bi->bi_bh->b_size,
  855. GFP_KERNEL | __GFP_NOFAIL);
  856. memcpy(bi->bi_clone + bi->bi_offset,
  857. bi->bi_bh->b_data + bi->bi_offset,
  858. bi->bi_len);
  859. }
  860. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  861. gfs2_setbit(rgd,
  862. bi->bi_bh->b_data + bi->bi_offset,
  863. bi->bi_len, buf_blk, new_state);
  864. }
  865. return rgd;
  866. }
  867. /**
  868. * gfs2_alloc_data - Allocate a data block
  869. * @ip: the inode to allocate the data block for
  870. *
  871. * Returns: the allocated block
  872. */
  873. uint64_t gfs2_alloc_data(struct gfs2_inode *ip)
  874. {
  875. struct gfs2_sbd *sdp = ip->i_sbd;
  876. struct gfs2_alloc *al = &ip->i_alloc;
  877. struct gfs2_rgrpd *rgd = al->al_rgd;
  878. uint32_t goal, blk;
  879. uint64_t block;
  880. if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_data))
  881. goal = ip->i_di.di_goal_data - rgd->rd_ri.ri_data0;
  882. else
  883. goal = rgd->rd_last_alloc_data;
  884. blk = rgblk_search(rgd, goal,
  885. GFS2_BLKST_FREE, GFS2_BLKST_USED);
  886. rgd->rd_last_alloc_data = blk;
  887. block = rgd->rd_ri.ri_data0 + blk;
  888. ip->i_di.di_goal_data = block;
  889. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
  890. rgd->rd_rg.rg_free--;
  891. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  892. gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
  893. al->al_alloced++;
  894. gfs2_statfs_change(sdp, 0, -1, 0);
  895. gfs2_quota_change(ip, +1, ip->i_di.di_uid, ip->i_di.di_gid);
  896. spin_lock(&sdp->sd_rindex_spin);
  897. rgd->rd_free_clone--;
  898. spin_unlock(&sdp->sd_rindex_spin);
  899. return block;
  900. }
  901. /**
  902. * gfs2_alloc_meta - Allocate a metadata block
  903. * @ip: the inode to allocate the metadata block for
  904. *
  905. * Returns: the allocated block
  906. */
  907. uint64_t gfs2_alloc_meta(struct gfs2_inode *ip)
  908. {
  909. struct gfs2_sbd *sdp = ip->i_sbd;
  910. struct gfs2_alloc *al = &ip->i_alloc;
  911. struct gfs2_rgrpd *rgd = al->al_rgd;
  912. uint32_t goal, blk;
  913. uint64_t block;
  914. if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_meta))
  915. goal = ip->i_di.di_goal_meta - rgd->rd_ri.ri_data0;
  916. else
  917. goal = rgd->rd_last_alloc_meta;
  918. blk = rgblk_search(rgd, goal,
  919. GFS2_BLKST_FREE, GFS2_BLKST_USED);
  920. rgd->rd_last_alloc_meta = blk;
  921. block = rgd->rd_ri.ri_data0 + blk;
  922. ip->i_di.di_goal_meta = block;
  923. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
  924. rgd->rd_rg.rg_free--;
  925. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  926. gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
  927. al->al_alloced++;
  928. gfs2_statfs_change(sdp, 0, -1, 0);
  929. gfs2_quota_change(ip, +1, ip->i_di.di_uid, ip->i_di.di_gid);
  930. gfs2_trans_add_unrevoke(sdp, block);
  931. spin_lock(&sdp->sd_rindex_spin);
  932. rgd->rd_free_clone--;
  933. spin_unlock(&sdp->sd_rindex_spin);
  934. return block;
  935. }
  936. /**
  937. * gfs2_alloc_di - Allocate a dinode
  938. * @dip: the directory that the inode is going in
  939. *
  940. * Returns: the block allocated
  941. */
  942. uint64_t gfs2_alloc_di(struct gfs2_inode *dip)
  943. {
  944. struct gfs2_sbd *sdp = dip->i_sbd;
  945. struct gfs2_alloc *al = &dip->i_alloc;
  946. struct gfs2_rgrpd *rgd = al->al_rgd;
  947. uint32_t blk;
  948. uint64_t block;
  949. blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
  950. GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
  951. rgd->rd_last_alloc_meta = blk;
  952. block = rgd->rd_ri.ri_data0 + blk;
  953. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
  954. rgd->rd_rg.rg_free--;
  955. rgd->rd_rg.rg_dinodes++;
  956. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  957. gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
  958. al->al_alloced++;
  959. gfs2_statfs_change(sdp, 0, -1, +1);
  960. gfs2_trans_add_unrevoke(sdp, block);
  961. spin_lock(&sdp->sd_rindex_spin);
  962. rgd->rd_free_clone--;
  963. spin_unlock(&sdp->sd_rindex_spin);
  964. return block;
  965. }
  966. /**
  967. * gfs2_free_data - free a contiguous run of data block(s)
  968. * @ip: the inode these blocks are being freed from
  969. * @bstart: first block of a run of contiguous blocks
  970. * @blen: the length of the block run
  971. *
  972. */
  973. void gfs2_free_data(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
  974. {
  975. struct gfs2_sbd *sdp = ip->i_sbd;
  976. struct gfs2_rgrpd *rgd;
  977. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  978. if (!rgd)
  979. return;
  980. rgd->rd_rg.rg_free += blen;
  981. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  982. gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
  983. gfs2_trans_add_rg(rgd);
  984. gfs2_statfs_change(sdp, 0, +blen, 0);
  985. gfs2_quota_change(ip, -(int64_t)blen,
  986. ip->i_di.di_uid, ip->i_di.di_gid);
  987. }
  988. /**
  989. * gfs2_free_meta - free a contiguous run of data block(s)
  990. * @ip: the inode these blocks are being freed from
  991. * @bstart: first block of a run of contiguous blocks
  992. * @blen: the length of the block run
  993. *
  994. */
  995. void gfs2_free_meta(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
  996. {
  997. struct gfs2_sbd *sdp = ip->i_sbd;
  998. struct gfs2_rgrpd *rgd;
  999. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1000. if (!rgd)
  1001. return;
  1002. rgd->rd_rg.rg_free += blen;
  1003. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1004. gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
  1005. gfs2_trans_add_rg(rgd);
  1006. gfs2_statfs_change(sdp, 0, +blen, 0);
  1007. gfs2_quota_change(ip, -(int64_t)blen,
  1008. ip->i_di.di_uid, ip->i_di.di_gid);
  1009. gfs2_meta_wipe(ip, bstart, blen);
  1010. }
  1011. void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, uint64_t blkno)
  1012. {
  1013. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1014. struct gfs2_rgrpd *tmp_rgd;
  1015. tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
  1016. if (!tmp_rgd)
  1017. return;
  1018. gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
  1019. if (!rgd->rd_rg.rg_dinodes)
  1020. gfs2_consist_rgrpd(rgd);
  1021. rgd->rd_rg.rg_dinodes--;
  1022. rgd->rd_rg.rg_free++;
  1023. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1024. gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
  1025. gfs2_statfs_change(sdp, 0, +1, -1);
  1026. gfs2_trans_add_rg(rgd);
  1027. }
  1028. /**
  1029. * gfs2_free_uninit_di - free a dinode block
  1030. * @rgd: the resource group that contains the dinode
  1031. * @ip: the inode
  1032. *
  1033. */
  1034. void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
  1035. {
  1036. gfs2_free_uninit_di(rgd, ip->i_num.no_addr);
  1037. gfs2_quota_change(ip, -1, ip->i_di.di_uid, ip->i_di.di_gid);
  1038. gfs2_meta_wipe(ip, ip->i_num.no_addr, 1);
  1039. }
  1040. /**
  1041. * gfs2_rlist_add - add a RG to a list of RGs
  1042. * @sdp: the filesystem
  1043. * @rlist: the list of resource groups
  1044. * @block: the block
  1045. *
  1046. * Figure out what RG a block belongs to and add that RG to the list
  1047. *
  1048. * FIXME: Don't use NOFAIL
  1049. *
  1050. */
  1051. void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
  1052. uint64_t block)
  1053. {
  1054. struct gfs2_rgrpd *rgd;
  1055. struct gfs2_rgrpd **tmp;
  1056. unsigned int new_space;
  1057. unsigned int x;
  1058. if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
  1059. return;
  1060. rgd = gfs2_blk2rgrpd(sdp, block);
  1061. if (!rgd) {
  1062. if (gfs2_consist(sdp))
  1063. fs_err(sdp, "block = %llu\n", block);
  1064. return;
  1065. }
  1066. for (x = 0; x < rlist->rl_rgrps; x++)
  1067. if (rlist->rl_rgd[x] == rgd)
  1068. return;
  1069. if (rlist->rl_rgrps == rlist->rl_space) {
  1070. new_space = rlist->rl_space + 10;
  1071. tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
  1072. GFP_KERNEL | __GFP_NOFAIL);
  1073. if (rlist->rl_rgd) {
  1074. memcpy(tmp, rlist->rl_rgd,
  1075. rlist->rl_space * sizeof(struct gfs2_rgrpd *));
  1076. kfree(rlist->rl_rgd);
  1077. }
  1078. rlist->rl_space = new_space;
  1079. rlist->rl_rgd = tmp;
  1080. }
  1081. rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
  1082. }
  1083. /**
  1084. * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
  1085. * and initialize an array of glock holders for them
  1086. * @rlist: the list of resource groups
  1087. * @state: the lock state to acquire the RG lock in
  1088. * @flags: the modifier flags for the holder structures
  1089. *
  1090. * FIXME: Don't use NOFAIL
  1091. *
  1092. */
  1093. void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
  1094. int flags)
  1095. {
  1096. unsigned int x;
  1097. rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
  1098. GFP_KERNEL | __GFP_NOFAIL);
  1099. for (x = 0; x < rlist->rl_rgrps; x++)
  1100. gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
  1101. state, flags,
  1102. &rlist->rl_ghs[x]);
  1103. }
  1104. /**
  1105. * gfs2_rlist_free - free a resource group list
  1106. * @list: the list of resource groups
  1107. *
  1108. */
  1109. void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
  1110. {
  1111. unsigned int x;
  1112. kfree(rlist->rl_rgd);
  1113. if (rlist->rl_ghs) {
  1114. for (x = 0; x < rlist->rl_rgrps; x++)
  1115. gfs2_holder_uninit(&rlist->rl_ghs[x]);
  1116. kfree(rlist->rl_ghs);
  1117. }
  1118. }