rgrp.c 39 KB

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