rgrp.c 31 KB

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