rgrp.c 41 KB

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