rgrp.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  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, bi->bi_clone + bi->bi_offset,
  795. bi->bi_len, goal, old_state);
  796. else
  797. blk = gfs2_bitfit(rgd,
  798. bi->bi_bh->b_data + bi->bi_offset,
  799. bi->bi_len, goal, old_state);
  800. if (blk != BFITNOENT)
  801. break;
  802. /* Try next bitmap block (wrap back to rgrp header if at end) */
  803. buf = (buf + 1) % length;
  804. bi = rgd->rd_bits + buf;
  805. goal = 0;
  806. }
  807. if (gfs2_assert_withdraw(rgd->rd_sbd, x <= length))
  808. blk = 0;
  809. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  810. gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
  811. bi->bi_len, blk, new_state);
  812. if (bi->bi_clone)
  813. gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
  814. bi->bi_len, blk, new_state);
  815. return bi->bi_start * GFS2_NBBY + blk;
  816. }
  817. /**
  818. * rgblk_free - Change alloc state of given block(s)
  819. * @sdp: the filesystem
  820. * @bstart: the start of a run of blocks to free
  821. * @blen: the length of the block run (all must lie within ONE RG!)
  822. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  823. *
  824. * Returns: Resource group containing the block(s)
  825. */
  826. static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, uint64_t bstart,
  827. uint32_t blen, unsigned char new_state)
  828. {
  829. struct gfs2_rgrpd *rgd;
  830. struct gfs2_bitmap *bi = NULL;
  831. uint32_t length, rgrp_blk, buf_blk;
  832. unsigned int buf;
  833. rgd = gfs2_blk2rgrpd(sdp, bstart);
  834. if (!rgd) {
  835. if (gfs2_consist(sdp))
  836. fs_err(sdp, "block = %llu\n", bstart);
  837. return NULL;
  838. }
  839. length = rgd->rd_ri.ri_length;
  840. rgrp_blk = bstart - rgd->rd_ri.ri_data0;
  841. while (blen--) {
  842. for (buf = 0; buf < length; buf++) {
  843. bi = rgd->rd_bits + buf;
  844. if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  845. break;
  846. }
  847. gfs2_assert(rgd->rd_sbd, buf < length);
  848. buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
  849. rgrp_blk++;
  850. if (!bi->bi_clone) {
  851. bi->bi_clone = kmalloc(bi->bi_bh->b_size,
  852. GFP_KERNEL | __GFP_NOFAIL);
  853. memcpy(bi->bi_clone + bi->bi_offset,
  854. bi->bi_bh->b_data + bi->bi_offset,
  855. bi->bi_len);
  856. }
  857. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  858. gfs2_setbit(rgd,
  859. bi->bi_bh->b_data + bi->bi_offset,
  860. bi->bi_len, buf_blk, new_state);
  861. }
  862. return rgd;
  863. }
  864. /**
  865. * gfs2_alloc_data - Allocate a data block
  866. * @ip: the inode to allocate the data block for
  867. *
  868. * Returns: the allocated block
  869. */
  870. uint64_t gfs2_alloc_data(struct gfs2_inode *ip)
  871. {
  872. struct gfs2_sbd *sdp = ip->i_sbd;
  873. struct gfs2_alloc *al = &ip->i_alloc;
  874. struct gfs2_rgrpd *rgd = al->al_rgd;
  875. uint32_t goal, blk;
  876. uint64_t block;
  877. if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_data))
  878. goal = ip->i_di.di_goal_data - rgd->rd_ri.ri_data0;
  879. else
  880. goal = rgd->rd_last_alloc_data;
  881. blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
  882. rgd->rd_last_alloc_data = blk;
  883. block = rgd->rd_ri.ri_data0 + blk;
  884. ip->i_di.di_goal_data = block;
  885. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
  886. rgd->rd_rg.rg_free--;
  887. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  888. gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
  889. al->al_alloced++;
  890. gfs2_statfs_change(sdp, 0, -1, 0);
  891. gfs2_quota_change(ip, +1, ip->i_di.di_uid, ip->i_di.di_gid);
  892. spin_lock(&sdp->sd_rindex_spin);
  893. rgd->rd_free_clone--;
  894. spin_unlock(&sdp->sd_rindex_spin);
  895. return block;
  896. }
  897. /**
  898. * gfs2_alloc_meta - Allocate a metadata block
  899. * @ip: the inode to allocate the metadata block for
  900. *
  901. * Returns: the allocated block
  902. */
  903. uint64_t gfs2_alloc_meta(struct gfs2_inode *ip)
  904. {
  905. struct gfs2_sbd *sdp = ip->i_sbd;
  906. struct gfs2_alloc *al = &ip->i_alloc;
  907. struct gfs2_rgrpd *rgd = al->al_rgd;
  908. uint32_t goal, blk;
  909. uint64_t block;
  910. if (rgrp_contains_block(&rgd->rd_ri, ip->i_di.di_goal_meta))
  911. goal = ip->i_di.di_goal_meta - rgd->rd_ri.ri_data0;
  912. else
  913. goal = rgd->rd_last_alloc_meta;
  914. blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
  915. rgd->rd_last_alloc_meta = blk;
  916. block = rgd->rd_ri.ri_data0 + blk;
  917. ip->i_di.di_goal_meta = block;
  918. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
  919. rgd->rd_rg.rg_free--;
  920. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  921. gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
  922. al->al_alloced++;
  923. gfs2_statfs_change(sdp, 0, -1, 0);
  924. gfs2_quota_change(ip, +1, ip->i_di.di_uid, ip->i_di.di_gid);
  925. gfs2_trans_add_unrevoke(sdp, block);
  926. spin_lock(&sdp->sd_rindex_spin);
  927. rgd->rd_free_clone--;
  928. spin_unlock(&sdp->sd_rindex_spin);
  929. return block;
  930. }
  931. /**
  932. * gfs2_alloc_di - Allocate a dinode
  933. * @dip: the directory that the inode is going in
  934. *
  935. * Returns: the block allocated
  936. */
  937. uint64_t gfs2_alloc_di(struct gfs2_inode *dip)
  938. {
  939. struct gfs2_sbd *sdp = dip->i_sbd;
  940. struct gfs2_alloc *al = &dip->i_alloc;
  941. struct gfs2_rgrpd *rgd = al->al_rgd;
  942. uint32_t blk;
  943. uint64_t block;
  944. blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
  945. GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
  946. rgd->rd_last_alloc_meta = blk;
  947. block = rgd->rd_ri.ri_data0 + blk;
  948. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
  949. rgd->rd_rg.rg_free--;
  950. rgd->rd_rg.rg_dinodes++;
  951. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  952. gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
  953. al->al_alloced++;
  954. gfs2_statfs_change(sdp, 0, -1, +1);
  955. gfs2_trans_add_unrevoke(sdp, block);
  956. spin_lock(&sdp->sd_rindex_spin);
  957. rgd->rd_free_clone--;
  958. spin_unlock(&sdp->sd_rindex_spin);
  959. return block;
  960. }
  961. /**
  962. * gfs2_free_data - free a contiguous run of data block(s)
  963. * @ip: the inode these blocks are being freed from
  964. * @bstart: first block of a run of contiguous blocks
  965. * @blen: the length of the block run
  966. *
  967. */
  968. void gfs2_free_data(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
  969. {
  970. struct gfs2_sbd *sdp = ip->i_sbd;
  971. struct gfs2_rgrpd *rgd;
  972. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  973. if (!rgd)
  974. return;
  975. rgd->rd_rg.rg_free += blen;
  976. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  977. gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
  978. gfs2_trans_add_rg(rgd);
  979. gfs2_statfs_change(sdp, 0, +blen, 0);
  980. gfs2_quota_change(ip, -(int64_t)blen,
  981. ip->i_di.di_uid, ip->i_di.di_gid);
  982. }
  983. /**
  984. * gfs2_free_meta - free a contiguous run of data block(s)
  985. * @ip: the inode these blocks are being freed from
  986. * @bstart: first block of a run of contiguous blocks
  987. * @blen: the length of the block run
  988. *
  989. */
  990. void gfs2_free_meta(struct gfs2_inode *ip, uint64_t bstart, uint32_t blen)
  991. {
  992. struct gfs2_sbd *sdp = ip->i_sbd;
  993. struct gfs2_rgrpd *rgd;
  994. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  995. if (!rgd)
  996. return;
  997. rgd->rd_rg.rg_free += blen;
  998. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  999. gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
  1000. gfs2_trans_add_rg(rgd);
  1001. gfs2_statfs_change(sdp, 0, +blen, 0);
  1002. gfs2_quota_change(ip, -(int64_t)blen,
  1003. ip->i_di.di_uid, ip->i_di.di_gid);
  1004. gfs2_meta_wipe(ip, bstart, blen);
  1005. }
  1006. void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, uint64_t blkno)
  1007. {
  1008. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1009. struct gfs2_rgrpd *tmp_rgd;
  1010. tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
  1011. if (!tmp_rgd)
  1012. return;
  1013. gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
  1014. if (!rgd->rd_rg.rg_dinodes)
  1015. gfs2_consist_rgrpd(rgd);
  1016. rgd->rd_rg.rg_dinodes--;
  1017. rgd->rd_rg.rg_free++;
  1018. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1019. gfs2_rgrp_out(&rgd->rd_rg, rgd->rd_bits[0].bi_bh->b_data);
  1020. gfs2_statfs_change(sdp, 0, +1, -1);
  1021. gfs2_trans_add_rg(rgd);
  1022. }
  1023. /**
  1024. * gfs2_free_uninit_di - free a dinode block
  1025. * @rgd: the resource group that contains the dinode
  1026. * @ip: the inode
  1027. *
  1028. */
  1029. void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
  1030. {
  1031. gfs2_free_uninit_di(rgd, ip->i_num.no_addr);
  1032. gfs2_quota_change(ip, -1, ip->i_di.di_uid, ip->i_di.di_gid);
  1033. gfs2_meta_wipe(ip, ip->i_num.no_addr, 1);
  1034. }
  1035. /**
  1036. * gfs2_rlist_add - add a RG to a list of RGs
  1037. * @sdp: the filesystem
  1038. * @rlist: the list of resource groups
  1039. * @block: the block
  1040. *
  1041. * Figure out what RG a block belongs to and add that RG to the list
  1042. *
  1043. * FIXME: Don't use NOFAIL
  1044. *
  1045. */
  1046. void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
  1047. uint64_t block)
  1048. {
  1049. struct gfs2_rgrpd *rgd;
  1050. struct gfs2_rgrpd **tmp;
  1051. unsigned int new_space;
  1052. unsigned int x;
  1053. if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
  1054. return;
  1055. rgd = gfs2_blk2rgrpd(sdp, block);
  1056. if (!rgd) {
  1057. if (gfs2_consist(sdp))
  1058. fs_err(sdp, "block = %llu\n", block);
  1059. return;
  1060. }
  1061. for (x = 0; x < rlist->rl_rgrps; x++)
  1062. if (rlist->rl_rgd[x] == rgd)
  1063. return;
  1064. if (rlist->rl_rgrps == rlist->rl_space) {
  1065. new_space = rlist->rl_space + 10;
  1066. tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
  1067. GFP_KERNEL | __GFP_NOFAIL);
  1068. if (rlist->rl_rgd) {
  1069. memcpy(tmp, rlist->rl_rgd,
  1070. rlist->rl_space * sizeof(struct gfs2_rgrpd *));
  1071. kfree(rlist->rl_rgd);
  1072. }
  1073. rlist->rl_space = new_space;
  1074. rlist->rl_rgd = tmp;
  1075. }
  1076. rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
  1077. }
  1078. /**
  1079. * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
  1080. * and initialize an array of glock holders for them
  1081. * @rlist: the list of resource groups
  1082. * @state: the lock state to acquire the RG lock in
  1083. * @flags: the modifier flags for the holder structures
  1084. *
  1085. * FIXME: Don't use NOFAIL
  1086. *
  1087. */
  1088. void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state,
  1089. int flags)
  1090. {
  1091. unsigned int x;
  1092. rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
  1093. GFP_KERNEL | __GFP_NOFAIL);
  1094. for (x = 0; x < rlist->rl_rgrps; x++)
  1095. gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
  1096. state, flags,
  1097. &rlist->rl_ghs[x]);
  1098. }
  1099. /**
  1100. * gfs2_rlist_free - free a resource group list
  1101. * @list: the list of resource groups
  1102. *
  1103. */
  1104. void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
  1105. {
  1106. unsigned int x;
  1107. kfree(rlist->rl_rgd);
  1108. if (rlist->rl_ghs) {
  1109. for (x = 0; x < rlist->rl_rgrps; x++)
  1110. gfs2_holder_uninit(&rlist->rl_ghs[x]);
  1111. kfree(rlist->rl_ghs);
  1112. }
  1113. }