rgrp.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2008 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. kmem_cache_free(gfs2_rgrpd_cachep, 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 = kmem_cache_zalloc(gfs2_rgrpd_cachep, 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_rgrpd *rgd, const void *buf)
  564. {
  565. const struct gfs2_rgrp *str = buf;
  566. struct gfs2_rgrp_host *rg = &rgd->rd_rg;
  567. u32 rg_flags;
  568. rg_flags = be32_to_cpu(str->rg_flags);
  569. if (rg_flags & GFS2_RGF_NOALLOC)
  570. rgd->rd_flags |= GFS2_RDF_NOALLOC;
  571. else
  572. rgd->rd_flags &= ~GFS2_RDF_NOALLOC;
  573. rg->rg_free = be32_to_cpu(str->rg_free);
  574. rg->rg_dinodes = be32_to_cpu(str->rg_dinodes);
  575. rg->rg_igeneration = be64_to_cpu(str->rg_igeneration);
  576. }
  577. static void gfs2_rgrp_out(struct gfs2_rgrpd *rgd, void *buf)
  578. {
  579. struct gfs2_rgrp *str = buf;
  580. struct gfs2_rgrp_host *rg = &rgd->rd_rg;
  581. u32 rg_flags = 0;
  582. if (rgd->rd_flags & GFS2_RDF_NOALLOC)
  583. rg_flags |= GFS2_RGF_NOALLOC;
  584. str->rg_flags = cpu_to_be32(rg_flags);
  585. str->rg_free = cpu_to_be32(rg->rg_free);
  586. str->rg_dinodes = cpu_to_be32(rg->rg_dinodes);
  587. str->__pad = cpu_to_be32(0);
  588. str->rg_igeneration = cpu_to_be64(rg->rg_igeneration);
  589. memset(&str->rg_reserved, 0, sizeof(str->rg_reserved));
  590. }
  591. /**
  592. * gfs2_rgrp_bh_get - Read in a RG's header and bitmaps
  593. * @rgd: the struct gfs2_rgrpd describing the RG to read in
  594. *
  595. * Read in all of a Resource Group's header and bitmap blocks.
  596. * Caller must eventually call gfs2_rgrp_relse() to free the bitmaps.
  597. *
  598. * Returns: errno
  599. */
  600. int gfs2_rgrp_bh_get(struct gfs2_rgrpd *rgd)
  601. {
  602. struct gfs2_sbd *sdp = rgd->rd_sbd;
  603. struct gfs2_glock *gl = rgd->rd_gl;
  604. unsigned int length = rgd->rd_length;
  605. struct gfs2_bitmap *bi;
  606. unsigned int x, y;
  607. int error;
  608. mutex_lock(&rgd->rd_mutex);
  609. spin_lock(&sdp->sd_rindex_spin);
  610. if (rgd->rd_bh_count) {
  611. rgd->rd_bh_count++;
  612. spin_unlock(&sdp->sd_rindex_spin);
  613. mutex_unlock(&rgd->rd_mutex);
  614. return 0;
  615. }
  616. spin_unlock(&sdp->sd_rindex_spin);
  617. for (x = 0; x < length; x++) {
  618. bi = rgd->rd_bits + x;
  619. error = gfs2_meta_read(gl, rgd->rd_addr + x, 0, &bi->bi_bh);
  620. if (error)
  621. goto fail;
  622. }
  623. for (y = length; y--;) {
  624. bi = rgd->rd_bits + y;
  625. error = gfs2_meta_wait(sdp, bi->bi_bh);
  626. if (error)
  627. goto fail;
  628. if (gfs2_metatype_check(sdp, bi->bi_bh, y ? GFS2_METATYPE_RB :
  629. GFS2_METATYPE_RG)) {
  630. error = -EIO;
  631. goto fail;
  632. }
  633. }
  634. if (rgd->rd_rg_vn != gl->gl_vn) {
  635. gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
  636. rgd->rd_rg_vn = gl->gl_vn;
  637. }
  638. spin_lock(&sdp->sd_rindex_spin);
  639. rgd->rd_free_clone = rgd->rd_rg.rg_free;
  640. rgd->rd_bh_count++;
  641. spin_unlock(&sdp->sd_rindex_spin);
  642. mutex_unlock(&rgd->rd_mutex);
  643. return 0;
  644. fail:
  645. while (x--) {
  646. bi = rgd->rd_bits + x;
  647. brelse(bi->bi_bh);
  648. bi->bi_bh = NULL;
  649. gfs2_assert_warn(sdp, !bi->bi_clone);
  650. }
  651. mutex_unlock(&rgd->rd_mutex);
  652. return error;
  653. }
  654. void gfs2_rgrp_bh_hold(struct gfs2_rgrpd *rgd)
  655. {
  656. struct gfs2_sbd *sdp = rgd->rd_sbd;
  657. spin_lock(&sdp->sd_rindex_spin);
  658. gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
  659. rgd->rd_bh_count++;
  660. spin_unlock(&sdp->sd_rindex_spin);
  661. }
  662. /**
  663. * gfs2_rgrp_bh_put - Release RG bitmaps read in with gfs2_rgrp_bh_get()
  664. * @rgd: the struct gfs2_rgrpd describing the RG to read in
  665. *
  666. */
  667. void gfs2_rgrp_bh_put(struct gfs2_rgrpd *rgd)
  668. {
  669. struct gfs2_sbd *sdp = rgd->rd_sbd;
  670. int x, length = rgd->rd_length;
  671. spin_lock(&sdp->sd_rindex_spin);
  672. gfs2_assert_warn(rgd->rd_sbd, rgd->rd_bh_count);
  673. if (--rgd->rd_bh_count) {
  674. spin_unlock(&sdp->sd_rindex_spin);
  675. return;
  676. }
  677. for (x = 0; x < length; x++) {
  678. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  679. kfree(bi->bi_clone);
  680. bi->bi_clone = NULL;
  681. brelse(bi->bi_bh);
  682. bi->bi_bh = NULL;
  683. }
  684. spin_unlock(&sdp->sd_rindex_spin);
  685. }
  686. void gfs2_rgrp_repolish_clones(struct gfs2_rgrpd *rgd)
  687. {
  688. struct gfs2_sbd *sdp = rgd->rd_sbd;
  689. unsigned int length = rgd->rd_length;
  690. unsigned int x;
  691. for (x = 0; x < length; x++) {
  692. struct gfs2_bitmap *bi = rgd->rd_bits + x;
  693. if (!bi->bi_clone)
  694. continue;
  695. memcpy(bi->bi_clone + bi->bi_offset,
  696. bi->bi_bh->b_data + bi->bi_offset, bi->bi_len);
  697. }
  698. spin_lock(&sdp->sd_rindex_spin);
  699. rgd->rd_free_clone = rgd->rd_rg.rg_free;
  700. spin_unlock(&sdp->sd_rindex_spin);
  701. }
  702. /**
  703. * gfs2_alloc_get - get the struct gfs2_alloc structure for an inode
  704. * @ip: the incore GFS2 inode structure
  705. *
  706. * Returns: the struct gfs2_alloc
  707. */
  708. struct gfs2_alloc *gfs2_alloc_get(struct gfs2_inode *ip)
  709. {
  710. BUG_ON(ip->i_alloc != NULL);
  711. ip->i_alloc = kzalloc(sizeof(struct gfs2_alloc), GFP_KERNEL);
  712. return ip->i_alloc;
  713. }
  714. /**
  715. * try_rgrp_fit - See if a given reservation will fit in a given RG
  716. * @rgd: the RG data
  717. * @al: the struct gfs2_alloc structure describing the reservation
  718. *
  719. * If there's room for the requested blocks to be allocated from the RG:
  720. * Sets the $al_rgd field in @al.
  721. *
  722. * Returns: 1 on success (it fits), 0 on failure (it doesn't fit)
  723. */
  724. static int try_rgrp_fit(struct gfs2_rgrpd *rgd, struct gfs2_alloc *al)
  725. {
  726. struct gfs2_sbd *sdp = rgd->rd_sbd;
  727. int ret = 0;
  728. if (rgd->rd_flags & GFS2_RDF_NOALLOC)
  729. return 0;
  730. spin_lock(&sdp->sd_rindex_spin);
  731. if (rgd->rd_free_clone >= al->al_requested) {
  732. al->al_rgd = rgd;
  733. ret = 1;
  734. }
  735. spin_unlock(&sdp->sd_rindex_spin);
  736. return ret;
  737. }
  738. /**
  739. * try_rgrp_unlink - Look for any unlinked, allocated, but unused inodes
  740. * @rgd: The rgrp
  741. *
  742. * Returns: The inode, if one has been found
  743. */
  744. static struct inode *try_rgrp_unlink(struct gfs2_rgrpd *rgd, u64 *last_unlinked)
  745. {
  746. struct inode *inode;
  747. u32 goal = 0, block;
  748. u64 no_addr;
  749. struct gfs2_sbd *sdp = rgd->rd_sbd;
  750. for(;;) {
  751. if (goal >= rgd->rd_data)
  752. break;
  753. down_write(&sdp->sd_log_flush_lock);
  754. block = rgblk_search(rgd, goal, GFS2_BLKST_UNLINKED,
  755. GFS2_BLKST_UNLINKED);
  756. up_write(&sdp->sd_log_flush_lock);
  757. if (block == BFITNOENT)
  758. break;
  759. /* rgblk_search can return a block < goal, so we need to
  760. keep it marching forward. */
  761. no_addr = block + rgd->rd_data0;
  762. goal++;
  763. if (*last_unlinked != NO_BLOCK && no_addr <= *last_unlinked)
  764. continue;
  765. *last_unlinked = no_addr;
  766. inode = gfs2_inode_lookup(rgd->rd_sbd->sd_vfs, DT_UNKNOWN,
  767. no_addr, -1, 1);
  768. if (!IS_ERR(inode))
  769. return inode;
  770. }
  771. rgd->rd_flags &= ~GFS2_RDF_CHECK;
  772. return NULL;
  773. }
  774. /**
  775. * recent_rgrp_first - get first RG from "recent" list
  776. * @sdp: The GFS2 superblock
  777. * @rglast: address of the rgrp used last
  778. *
  779. * Returns: The first rgrp in the recent list
  780. */
  781. static struct gfs2_rgrpd *recent_rgrp_first(struct gfs2_sbd *sdp,
  782. u64 rglast)
  783. {
  784. struct gfs2_rgrpd *rgd = NULL;
  785. spin_lock(&sdp->sd_rindex_spin);
  786. if (list_empty(&sdp->sd_rindex_recent_list))
  787. goto out;
  788. if (!rglast)
  789. goto first;
  790. list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
  791. if (rgd->rd_addr == rglast)
  792. goto out;
  793. }
  794. first:
  795. rgd = list_entry(sdp->sd_rindex_recent_list.next, struct gfs2_rgrpd,
  796. rd_recent);
  797. out:
  798. spin_unlock(&sdp->sd_rindex_spin);
  799. return rgd;
  800. }
  801. /**
  802. * recent_rgrp_next - get next RG from "recent" list
  803. * @cur_rgd: current rgrp
  804. * @remove:
  805. *
  806. * Returns: The next rgrp in the recent list
  807. */
  808. static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
  809. int remove)
  810. {
  811. struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
  812. struct list_head *head;
  813. struct gfs2_rgrpd *rgd;
  814. spin_lock(&sdp->sd_rindex_spin);
  815. head = &sdp->sd_rindex_recent_list;
  816. list_for_each_entry(rgd, head, rd_recent) {
  817. if (rgd == cur_rgd) {
  818. if (cur_rgd->rd_recent.next != head)
  819. rgd = list_entry(cur_rgd->rd_recent.next,
  820. struct gfs2_rgrpd, rd_recent);
  821. else
  822. rgd = NULL;
  823. if (remove)
  824. list_del(&cur_rgd->rd_recent);
  825. goto out;
  826. }
  827. }
  828. rgd = NULL;
  829. if (!list_empty(head))
  830. rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
  831. out:
  832. spin_unlock(&sdp->sd_rindex_spin);
  833. return rgd;
  834. }
  835. /**
  836. * recent_rgrp_add - add an RG to tail of "recent" list
  837. * @new_rgd: The rgrp to add
  838. *
  839. */
  840. static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
  841. {
  842. struct gfs2_sbd *sdp = new_rgd->rd_sbd;
  843. struct gfs2_rgrpd *rgd;
  844. unsigned int count = 0;
  845. unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
  846. spin_lock(&sdp->sd_rindex_spin);
  847. list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
  848. if (rgd == new_rgd)
  849. goto out;
  850. if (++count >= max)
  851. goto out;
  852. }
  853. list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
  854. out:
  855. spin_unlock(&sdp->sd_rindex_spin);
  856. }
  857. /**
  858. * forward_rgrp_get - get an rgrp to try next from full list
  859. * @sdp: The GFS2 superblock
  860. *
  861. * Returns: The rgrp to try next
  862. */
  863. static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
  864. {
  865. struct gfs2_rgrpd *rgd;
  866. unsigned int journals = gfs2_jindex_size(sdp);
  867. unsigned int rg = 0, x;
  868. spin_lock(&sdp->sd_rindex_spin);
  869. rgd = sdp->sd_rindex_forward;
  870. if (!rgd) {
  871. if (sdp->sd_rgrps >= journals)
  872. rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
  873. for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
  874. x++, rgd = gfs2_rgrpd_get_next(rgd))
  875. /* Do Nothing */;
  876. sdp->sd_rindex_forward = rgd;
  877. }
  878. spin_unlock(&sdp->sd_rindex_spin);
  879. return rgd;
  880. }
  881. /**
  882. * forward_rgrp_set - set the forward rgrp pointer
  883. * @sdp: the filesystem
  884. * @rgd: The new forward rgrp
  885. *
  886. */
  887. static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
  888. {
  889. spin_lock(&sdp->sd_rindex_spin);
  890. sdp->sd_rindex_forward = rgd;
  891. spin_unlock(&sdp->sd_rindex_spin);
  892. }
  893. /**
  894. * get_local_rgrp - Choose and lock a rgrp for allocation
  895. * @ip: the inode to reserve space for
  896. * @rgp: the chosen and locked rgrp
  897. *
  898. * Try to acquire rgrp in way which avoids contending with others.
  899. *
  900. * Returns: errno
  901. */
  902. static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
  903. {
  904. struct inode *inode = NULL;
  905. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  906. struct gfs2_rgrpd *rgd, *begin = NULL;
  907. struct gfs2_alloc *al = ip->i_alloc;
  908. int flags = LM_FLAG_TRY;
  909. int skipped = 0;
  910. int loops = 0;
  911. int error, rg_locked;
  912. /* Try recently successful rgrps */
  913. rgd = recent_rgrp_first(sdp, ip->i_last_rg_alloc);
  914. while (rgd) {
  915. rg_locked = 0;
  916. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  917. rg_locked = 1;
  918. error = 0;
  919. } else {
  920. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
  921. LM_FLAG_TRY, &al->al_rgd_gh);
  922. }
  923. switch (error) {
  924. case 0:
  925. if (try_rgrp_fit(rgd, al))
  926. goto out;
  927. if (rgd->rd_flags & GFS2_RDF_CHECK)
  928. inode = try_rgrp_unlink(rgd, last_unlinked);
  929. if (!rg_locked)
  930. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  931. if (inode)
  932. return inode;
  933. rgd = recent_rgrp_next(rgd, 1);
  934. break;
  935. case GLR_TRYFAILED:
  936. rgd = recent_rgrp_next(rgd, 0);
  937. break;
  938. default:
  939. return ERR_PTR(error);
  940. }
  941. }
  942. /* Go through full list of rgrps */
  943. begin = rgd = forward_rgrp_get(sdp);
  944. for (;;) {
  945. rg_locked = 0;
  946. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  947. rg_locked = 1;
  948. error = 0;
  949. } else {
  950. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
  951. &al->al_rgd_gh);
  952. }
  953. switch (error) {
  954. case 0:
  955. if (try_rgrp_fit(rgd, al))
  956. goto out;
  957. if (rgd->rd_flags & GFS2_RDF_CHECK)
  958. inode = try_rgrp_unlink(rgd, last_unlinked);
  959. if (!rg_locked)
  960. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  961. if (inode)
  962. return inode;
  963. break;
  964. case GLR_TRYFAILED:
  965. skipped++;
  966. break;
  967. default:
  968. return ERR_PTR(error);
  969. }
  970. rgd = gfs2_rgrpd_get_next(rgd);
  971. if (!rgd)
  972. rgd = gfs2_rgrpd_get_first(sdp);
  973. if (rgd == begin) {
  974. if (++loops >= 3)
  975. return ERR_PTR(-ENOSPC);
  976. if (!skipped)
  977. loops++;
  978. flags = 0;
  979. if (loops == 2)
  980. gfs2_log_flush(sdp, NULL);
  981. }
  982. }
  983. out:
  984. ip->i_last_rg_alloc = rgd->rd_addr;
  985. if (begin) {
  986. recent_rgrp_add(rgd);
  987. rgd = gfs2_rgrpd_get_next(rgd);
  988. if (!rgd)
  989. rgd = gfs2_rgrpd_get_first(sdp);
  990. forward_rgrp_set(sdp, rgd);
  991. }
  992. return NULL;
  993. }
  994. /**
  995. * gfs2_inplace_reserve_i - Reserve space in the filesystem
  996. * @ip: the inode to reserve space for
  997. *
  998. * Returns: errno
  999. */
  1000. int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
  1001. {
  1002. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1003. struct gfs2_alloc *al = ip->i_alloc;
  1004. struct inode *inode;
  1005. int error = 0;
  1006. u64 last_unlinked = NO_BLOCK;
  1007. if (gfs2_assert_warn(sdp, al->al_requested))
  1008. return -EINVAL;
  1009. try_again:
  1010. /* We need to hold the rindex unless the inode we're using is
  1011. the rindex itself, in which case it's already held. */
  1012. if (ip != GFS2_I(sdp->sd_rindex))
  1013. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  1014. else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
  1015. error = gfs2_ri_update_special(ip);
  1016. if (error)
  1017. return error;
  1018. inode = get_local_rgrp(ip, &last_unlinked);
  1019. if (inode) {
  1020. if (ip != GFS2_I(sdp->sd_rindex))
  1021. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1022. if (IS_ERR(inode))
  1023. return PTR_ERR(inode);
  1024. iput(inode);
  1025. gfs2_log_flush(sdp, NULL);
  1026. goto try_again;
  1027. }
  1028. al->al_file = file;
  1029. al->al_line = line;
  1030. return 0;
  1031. }
  1032. /**
  1033. * gfs2_inplace_release - release an inplace reservation
  1034. * @ip: the inode the reservation was taken out on
  1035. *
  1036. * Release a reservation made by gfs2_inplace_reserve().
  1037. */
  1038. void gfs2_inplace_release(struct gfs2_inode *ip)
  1039. {
  1040. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1041. struct gfs2_alloc *al = ip->i_alloc;
  1042. if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
  1043. fs_warn(sdp, "al_alloced = %u, al_requested = %u "
  1044. "al_file = %s, al_line = %u\n",
  1045. al->al_alloced, al->al_requested, al->al_file,
  1046. al->al_line);
  1047. al->al_rgd = NULL;
  1048. if (al->al_rgd_gh.gh_gl)
  1049. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  1050. if (ip != GFS2_I(sdp->sd_rindex))
  1051. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1052. }
  1053. /**
  1054. * gfs2_get_block_type - Check a block in a RG is of given type
  1055. * @rgd: the resource group holding the block
  1056. * @block: the block number
  1057. *
  1058. * Returns: The block type (GFS2_BLKST_*)
  1059. */
  1060. unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
  1061. {
  1062. struct gfs2_bitmap *bi = NULL;
  1063. u32 length, rgrp_block, buf_block;
  1064. unsigned int buf;
  1065. unsigned char type;
  1066. length = rgd->rd_length;
  1067. rgrp_block = block - rgd->rd_data0;
  1068. for (buf = 0; buf < length; buf++) {
  1069. bi = rgd->rd_bits + buf;
  1070. if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1071. break;
  1072. }
  1073. gfs2_assert(rgd->rd_sbd, buf < length);
  1074. buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
  1075. type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
  1076. bi->bi_len, buf_block);
  1077. return type;
  1078. }
  1079. /**
  1080. * rgblk_search - find a block in @old_state, change allocation
  1081. * state to @new_state
  1082. * @rgd: the resource group descriptor
  1083. * @goal: the goal block within the RG (start here to search for avail block)
  1084. * @old_state: GFS2_BLKST_XXX the before-allocation state to find
  1085. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1086. *
  1087. * Walk rgrp's bitmap to find bits that represent a block in @old_state.
  1088. * Add the found bitmap buffer to the transaction.
  1089. * Set the found bits to @new_state to change block's allocation state.
  1090. *
  1091. * This function never fails, because we wouldn't call it unless we
  1092. * know (from reservation results, etc.) that a block is available.
  1093. *
  1094. * Scope of @goal and returned block is just within rgrp, not the whole
  1095. * filesystem.
  1096. *
  1097. * Returns: the block number allocated
  1098. */
  1099. static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
  1100. unsigned char old_state, unsigned char new_state)
  1101. {
  1102. struct gfs2_bitmap *bi = NULL;
  1103. u32 length = rgd->rd_length;
  1104. u32 blk = 0;
  1105. unsigned int buf, x;
  1106. /* Find bitmap block that contains bits for goal block */
  1107. for (buf = 0; buf < length; buf++) {
  1108. bi = rgd->rd_bits + buf;
  1109. if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1110. break;
  1111. }
  1112. gfs2_assert(rgd->rd_sbd, buf < length);
  1113. /* Convert scope of "goal" from rgrp-wide to within found bit block */
  1114. goal -= bi->bi_start * GFS2_NBBY;
  1115. /* Search (up to entire) bitmap in this rgrp for allocatable block.
  1116. "x <= length", instead of "x < length", because we typically start
  1117. the search in the middle of a bit block, but if we can't find an
  1118. allocatable block anywhere else, we want to be able wrap around and
  1119. search in the first part of our first-searched bit block. */
  1120. for (x = 0; x <= length; x++) {
  1121. /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
  1122. bitmaps, so we must search the originals for that. */
  1123. if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
  1124. blk = gfs2_bitfit(bi->bi_clone + bi->bi_offset,
  1125. bi->bi_len, goal, old_state);
  1126. else
  1127. blk = gfs2_bitfit(bi->bi_bh->b_data + bi->bi_offset,
  1128. bi->bi_len, goal, old_state);
  1129. if (blk != BFITNOENT)
  1130. break;
  1131. /* Try next bitmap block (wrap back to rgrp header if at end) */
  1132. buf = (buf + 1) % length;
  1133. bi = rgd->rd_bits + buf;
  1134. goal = 0;
  1135. }
  1136. if (blk != BFITNOENT && old_state != new_state) {
  1137. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1138. gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
  1139. bi->bi_len, blk, new_state);
  1140. if (bi->bi_clone)
  1141. gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
  1142. bi->bi_len, blk, new_state);
  1143. }
  1144. return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
  1145. }
  1146. /**
  1147. * rgblk_free - Change alloc state of given block(s)
  1148. * @sdp: the filesystem
  1149. * @bstart: the start of a run of blocks to free
  1150. * @blen: the length of the block run (all must lie within ONE RG!)
  1151. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1152. *
  1153. * Returns: Resource group containing the block(s)
  1154. */
  1155. static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
  1156. u32 blen, unsigned char new_state)
  1157. {
  1158. struct gfs2_rgrpd *rgd;
  1159. struct gfs2_bitmap *bi = NULL;
  1160. u32 length, rgrp_blk, buf_blk;
  1161. unsigned int buf;
  1162. rgd = gfs2_blk2rgrpd(sdp, bstart);
  1163. if (!rgd) {
  1164. if (gfs2_consist(sdp))
  1165. fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
  1166. return NULL;
  1167. }
  1168. length = rgd->rd_length;
  1169. rgrp_blk = bstart - rgd->rd_data0;
  1170. while (blen--) {
  1171. for (buf = 0; buf < length; buf++) {
  1172. bi = rgd->rd_bits + buf;
  1173. if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1174. break;
  1175. }
  1176. gfs2_assert(rgd->rd_sbd, buf < length);
  1177. buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
  1178. rgrp_blk++;
  1179. if (!bi->bi_clone) {
  1180. bi->bi_clone = kmalloc(bi->bi_bh->b_size,
  1181. GFP_NOFS | __GFP_NOFAIL);
  1182. memcpy(bi->bi_clone + bi->bi_offset,
  1183. bi->bi_bh->b_data + bi->bi_offset,
  1184. bi->bi_len);
  1185. }
  1186. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1187. gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
  1188. bi->bi_len, buf_blk, new_state);
  1189. }
  1190. return rgd;
  1191. }
  1192. /**
  1193. * gfs2_alloc_data - Allocate a data block
  1194. * @ip: the inode to allocate the data block for
  1195. *
  1196. * Returns: the allocated block
  1197. */
  1198. u64 gfs2_alloc_data(struct gfs2_inode *ip)
  1199. {
  1200. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1201. struct gfs2_alloc *al = ip->i_alloc;
  1202. struct gfs2_rgrpd *rgd = al->al_rgd;
  1203. u32 goal, blk;
  1204. u64 block;
  1205. if (rgrp_contains_block(rgd, ip->i_di.di_goal_data))
  1206. goal = ip->i_di.di_goal_data - rgd->rd_data0;
  1207. else
  1208. goal = rgd->rd_last_alloc_data;
  1209. blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
  1210. BUG_ON(blk == BFITNOENT);
  1211. rgd->rd_last_alloc_data = blk;
  1212. block = rgd->rd_data0 + blk;
  1213. ip->i_di.di_goal_data = block;
  1214. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
  1215. rgd->rd_rg.rg_free--;
  1216. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1217. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1218. al->al_alloced++;
  1219. gfs2_statfs_change(sdp, 0, -1, 0);
  1220. gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1221. spin_lock(&sdp->sd_rindex_spin);
  1222. rgd->rd_free_clone--;
  1223. spin_unlock(&sdp->sd_rindex_spin);
  1224. return block;
  1225. }
  1226. /**
  1227. * gfs2_alloc_meta - Allocate a metadata block
  1228. * @ip: the inode to allocate the metadata block for
  1229. *
  1230. * Returns: the allocated block
  1231. */
  1232. u64 gfs2_alloc_meta(struct gfs2_inode *ip)
  1233. {
  1234. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1235. struct gfs2_alloc *al = ip->i_alloc;
  1236. struct gfs2_rgrpd *rgd = al->al_rgd;
  1237. u32 goal, blk;
  1238. u64 block;
  1239. if (rgrp_contains_block(rgd, ip->i_di.di_goal_meta))
  1240. goal = ip->i_di.di_goal_meta - rgd->rd_data0;
  1241. else
  1242. goal = rgd->rd_last_alloc_meta;
  1243. blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
  1244. BUG_ON(blk == BFITNOENT);
  1245. rgd->rd_last_alloc_meta = blk;
  1246. block = rgd->rd_data0 + blk;
  1247. ip->i_di.di_goal_meta = block;
  1248. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
  1249. rgd->rd_rg.rg_free--;
  1250. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1251. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1252. al->al_alloced++;
  1253. gfs2_statfs_change(sdp, 0, -1, 0);
  1254. gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1255. gfs2_trans_add_unrevoke(sdp, block);
  1256. spin_lock(&sdp->sd_rindex_spin);
  1257. rgd->rd_free_clone--;
  1258. spin_unlock(&sdp->sd_rindex_spin);
  1259. return block;
  1260. }
  1261. /**
  1262. * gfs2_alloc_di - Allocate a dinode
  1263. * @dip: the directory that the inode is going in
  1264. *
  1265. * Returns: the block allocated
  1266. */
  1267. u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
  1268. {
  1269. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1270. struct gfs2_alloc *al = dip->i_alloc;
  1271. struct gfs2_rgrpd *rgd = al->al_rgd;
  1272. u32 blk;
  1273. u64 block;
  1274. blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
  1275. GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
  1276. BUG_ON(blk == BFITNOENT);
  1277. rgd->rd_last_alloc_meta = blk;
  1278. block = rgd->rd_data0 + blk;
  1279. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
  1280. rgd->rd_rg.rg_free--;
  1281. rgd->rd_rg.rg_dinodes++;
  1282. *generation = rgd->rd_rg.rg_igeneration++;
  1283. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1284. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1285. al->al_alloced++;
  1286. gfs2_statfs_change(sdp, 0, -1, +1);
  1287. gfs2_trans_add_unrevoke(sdp, block);
  1288. spin_lock(&sdp->sd_rindex_spin);
  1289. rgd->rd_free_clone--;
  1290. spin_unlock(&sdp->sd_rindex_spin);
  1291. return block;
  1292. }
  1293. /**
  1294. * gfs2_free_data - free a contiguous run of data block(s)
  1295. * @ip: the inode these blocks are being freed from
  1296. * @bstart: first block of a run of contiguous blocks
  1297. * @blen: the length of the block run
  1298. *
  1299. */
  1300. void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
  1301. {
  1302. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1303. struct gfs2_rgrpd *rgd;
  1304. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1305. if (!rgd)
  1306. return;
  1307. rgd->rd_rg.rg_free += blen;
  1308. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1309. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1310. gfs2_trans_add_rg(rgd);
  1311. gfs2_statfs_change(sdp, 0, +blen, 0);
  1312. gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1313. }
  1314. /**
  1315. * gfs2_free_meta - free a contiguous run of data block(s)
  1316. * @ip: the inode these blocks are being freed from
  1317. * @bstart: first block of a run of contiguous blocks
  1318. * @blen: the length of the block run
  1319. *
  1320. */
  1321. void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
  1322. {
  1323. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1324. struct gfs2_rgrpd *rgd;
  1325. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1326. if (!rgd)
  1327. return;
  1328. rgd->rd_rg.rg_free += blen;
  1329. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1330. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1331. gfs2_trans_add_rg(rgd);
  1332. gfs2_statfs_change(sdp, 0, +blen, 0);
  1333. gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1334. gfs2_meta_wipe(ip, bstart, blen);
  1335. }
  1336. void gfs2_unlink_di(struct inode *inode)
  1337. {
  1338. struct gfs2_inode *ip = GFS2_I(inode);
  1339. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1340. struct gfs2_rgrpd *rgd;
  1341. u64 blkno = ip->i_no_addr;
  1342. rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
  1343. if (!rgd)
  1344. return;
  1345. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1346. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1347. gfs2_trans_add_rg(rgd);
  1348. }
  1349. static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
  1350. {
  1351. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1352. struct gfs2_rgrpd *tmp_rgd;
  1353. tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
  1354. if (!tmp_rgd)
  1355. return;
  1356. gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
  1357. if (!rgd->rd_rg.rg_dinodes)
  1358. gfs2_consist_rgrpd(rgd);
  1359. rgd->rd_rg.rg_dinodes--;
  1360. rgd->rd_rg.rg_free++;
  1361. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1362. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1363. gfs2_statfs_change(sdp, 0, +1, -1);
  1364. gfs2_trans_add_rg(rgd);
  1365. }
  1366. void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
  1367. {
  1368. gfs2_free_uninit_di(rgd, ip->i_no_addr);
  1369. gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1370. gfs2_meta_wipe(ip, ip->i_no_addr, 1);
  1371. }
  1372. /**
  1373. * gfs2_rlist_add - add a RG to a list of RGs
  1374. * @sdp: the filesystem
  1375. * @rlist: the list of resource groups
  1376. * @block: the block
  1377. *
  1378. * Figure out what RG a block belongs to and add that RG to the list
  1379. *
  1380. * FIXME: Don't use NOFAIL
  1381. *
  1382. */
  1383. void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
  1384. u64 block)
  1385. {
  1386. struct gfs2_rgrpd *rgd;
  1387. struct gfs2_rgrpd **tmp;
  1388. unsigned int new_space;
  1389. unsigned int x;
  1390. if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
  1391. return;
  1392. rgd = gfs2_blk2rgrpd(sdp, block);
  1393. if (!rgd) {
  1394. if (gfs2_consist(sdp))
  1395. fs_err(sdp, "block = %llu\n", (unsigned long long)block);
  1396. return;
  1397. }
  1398. for (x = 0; x < rlist->rl_rgrps; x++)
  1399. if (rlist->rl_rgd[x] == rgd)
  1400. return;
  1401. if (rlist->rl_rgrps == rlist->rl_space) {
  1402. new_space = rlist->rl_space + 10;
  1403. tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
  1404. GFP_NOFS | __GFP_NOFAIL);
  1405. if (rlist->rl_rgd) {
  1406. memcpy(tmp, rlist->rl_rgd,
  1407. rlist->rl_space * sizeof(struct gfs2_rgrpd *));
  1408. kfree(rlist->rl_rgd);
  1409. }
  1410. rlist->rl_space = new_space;
  1411. rlist->rl_rgd = tmp;
  1412. }
  1413. rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
  1414. }
  1415. /**
  1416. * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
  1417. * and initialize an array of glock holders for them
  1418. * @rlist: the list of resource groups
  1419. * @state: the lock state to acquire the RG lock in
  1420. * @flags: the modifier flags for the holder structures
  1421. *
  1422. * FIXME: Don't use NOFAIL
  1423. *
  1424. */
  1425. void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
  1426. {
  1427. unsigned int x;
  1428. rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
  1429. GFP_NOFS | __GFP_NOFAIL);
  1430. for (x = 0; x < rlist->rl_rgrps; x++)
  1431. gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
  1432. state, 0,
  1433. &rlist->rl_ghs[x]);
  1434. }
  1435. /**
  1436. * gfs2_rlist_free - free a resource group list
  1437. * @list: the list of resource groups
  1438. *
  1439. */
  1440. void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
  1441. {
  1442. unsigned int x;
  1443. kfree(rlist->rl_rgd);
  1444. if (rlist->rl_ghs) {
  1445. for (x = 0; x < rlist->rl_rgrps; x++)
  1446. gfs2_holder_uninit(&rlist->rl_ghs[x]);
  1447. kfree(rlist->rl_ghs);
  1448. }
  1449. }