rgrp.c 35 KB

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