rgrp.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  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(const u8 *buffer, unsigned int buflen, u32 goal,
  110. u8 old_state)
  111. {
  112. const u8 *byte;
  113. u32 blk = goal;
  114. unsigned int bit, bitlong;
  115. const unsigned long *plong;
  116. #if BITS_PER_LONG == 32
  117. const unsigned long plong55 = 0x55555555;
  118. #else
  119. const unsigned long plong55 = 0x5555555555555555;
  120. #endif
  121. byte = buffer + (goal / GFS2_NBBY);
  122. plong = (const unsigned long *)(buffer + (goal / GFS2_NBBY));
  123. bit = (goal % GFS2_NBBY) * GFS2_BIT_SIZE;
  124. bitlong = bit;
  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, const u8 *buffer,
  157. unsigned int buflen, u8 state)
  158. {
  159. const u8 *byte = buffer;
  160. const u8 *end = buffer + buflen;
  161. const u8 state1 = state << 2;
  162. const u8 state2 = state << 4;
  163. const u8 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_flags &= ~GFS2_RDF_UPTODATE;
  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_uptodate = 1;
  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_uptodate = 1;
  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_uptodate) {
  553. mutex_lock(&sdp->sd_rindex_mutex);
  554. if (!sdp->sd_rindex_uptodate) {
  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_flags & GFS2_RDF_UPTODATE)) {
  635. gfs2_rgrp_in(rgd, (rgd->rd_bits[0].bi_bh)->b_data);
  636. rgd->rd_flags |= GFS2_RDF_UPTODATE;
  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;
  785. spin_lock(&sdp->sd_rindex_spin);
  786. if (rglast) {
  787. list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
  788. if (rgrp_contains_block(rgd, rglast))
  789. goto out;
  790. }
  791. }
  792. rgd = NULL;
  793. if (!list_empty(&sdp->sd_rindex_recent_list))
  794. rgd = list_entry(sdp->sd_rindex_recent_list.next,
  795. struct gfs2_rgrpd, rd_recent);
  796. out:
  797. spin_unlock(&sdp->sd_rindex_spin);
  798. return rgd;
  799. }
  800. /**
  801. * recent_rgrp_next - get next RG from "recent" list
  802. * @cur_rgd: current rgrp
  803. * @remove:
  804. *
  805. * Returns: The next rgrp in the recent list
  806. */
  807. static struct gfs2_rgrpd *recent_rgrp_next(struct gfs2_rgrpd *cur_rgd,
  808. int remove)
  809. {
  810. struct gfs2_sbd *sdp = cur_rgd->rd_sbd;
  811. struct list_head *head;
  812. struct gfs2_rgrpd *rgd;
  813. spin_lock(&sdp->sd_rindex_spin);
  814. head = &sdp->sd_rindex_recent_list;
  815. list_for_each_entry(rgd, head, rd_recent) {
  816. if (rgd == cur_rgd) {
  817. if (cur_rgd->rd_recent.next != head)
  818. rgd = list_entry(cur_rgd->rd_recent.next,
  819. struct gfs2_rgrpd, rd_recent);
  820. else
  821. rgd = NULL;
  822. if (remove)
  823. list_del(&cur_rgd->rd_recent);
  824. goto out;
  825. }
  826. }
  827. rgd = NULL;
  828. if (!list_empty(head))
  829. rgd = list_entry(head->next, struct gfs2_rgrpd, rd_recent);
  830. out:
  831. spin_unlock(&sdp->sd_rindex_spin);
  832. return rgd;
  833. }
  834. /**
  835. * recent_rgrp_add - add an RG to tail of "recent" list
  836. * @new_rgd: The rgrp to add
  837. *
  838. */
  839. static void recent_rgrp_add(struct gfs2_rgrpd *new_rgd)
  840. {
  841. struct gfs2_sbd *sdp = new_rgd->rd_sbd;
  842. struct gfs2_rgrpd *rgd;
  843. unsigned int count = 0;
  844. unsigned int max = sdp->sd_rgrps / gfs2_jindex_size(sdp);
  845. spin_lock(&sdp->sd_rindex_spin);
  846. list_for_each_entry(rgd, &sdp->sd_rindex_recent_list, rd_recent) {
  847. if (rgd == new_rgd)
  848. goto out;
  849. if (++count >= max)
  850. goto out;
  851. }
  852. list_add_tail(&new_rgd->rd_recent, &sdp->sd_rindex_recent_list);
  853. out:
  854. spin_unlock(&sdp->sd_rindex_spin);
  855. }
  856. /**
  857. * forward_rgrp_get - get an rgrp to try next from full list
  858. * @sdp: The GFS2 superblock
  859. *
  860. * Returns: The rgrp to try next
  861. */
  862. static struct gfs2_rgrpd *forward_rgrp_get(struct gfs2_sbd *sdp)
  863. {
  864. struct gfs2_rgrpd *rgd;
  865. unsigned int journals = gfs2_jindex_size(sdp);
  866. unsigned int rg = 0, x;
  867. spin_lock(&sdp->sd_rindex_spin);
  868. rgd = sdp->sd_rindex_forward;
  869. if (!rgd) {
  870. if (sdp->sd_rgrps >= journals)
  871. rg = sdp->sd_rgrps * sdp->sd_jdesc->jd_jid / journals;
  872. for (x = 0, rgd = gfs2_rgrpd_get_first(sdp); x < rg;
  873. x++, rgd = gfs2_rgrpd_get_next(rgd))
  874. /* Do Nothing */;
  875. sdp->sd_rindex_forward = rgd;
  876. }
  877. spin_unlock(&sdp->sd_rindex_spin);
  878. return rgd;
  879. }
  880. /**
  881. * forward_rgrp_set - set the forward rgrp pointer
  882. * @sdp: the filesystem
  883. * @rgd: The new forward rgrp
  884. *
  885. */
  886. static void forward_rgrp_set(struct gfs2_sbd *sdp, struct gfs2_rgrpd *rgd)
  887. {
  888. spin_lock(&sdp->sd_rindex_spin);
  889. sdp->sd_rindex_forward = rgd;
  890. spin_unlock(&sdp->sd_rindex_spin);
  891. }
  892. /**
  893. * get_local_rgrp - Choose and lock a rgrp for allocation
  894. * @ip: the inode to reserve space for
  895. * @rgp: the chosen and locked rgrp
  896. *
  897. * Try to acquire rgrp in way which avoids contending with others.
  898. *
  899. * Returns: errno
  900. */
  901. static struct inode *get_local_rgrp(struct gfs2_inode *ip, u64 *last_unlinked)
  902. {
  903. struct inode *inode = NULL;
  904. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  905. struct gfs2_rgrpd *rgd, *begin = NULL;
  906. struct gfs2_alloc *al = ip->i_alloc;
  907. int flags = LM_FLAG_TRY;
  908. int skipped = 0;
  909. int loops = 0;
  910. int error, rg_locked;
  911. /* Try recently successful rgrps */
  912. rgd = recent_rgrp_first(sdp, ip->i_goal);
  913. while (rgd) {
  914. rg_locked = 0;
  915. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  916. rg_locked = 1;
  917. error = 0;
  918. } else {
  919. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE,
  920. LM_FLAG_TRY, &al->al_rgd_gh);
  921. }
  922. switch (error) {
  923. case 0:
  924. if (try_rgrp_fit(rgd, al))
  925. goto out;
  926. if (rgd->rd_flags & GFS2_RDF_CHECK)
  927. inode = try_rgrp_unlink(rgd, last_unlinked);
  928. if (!rg_locked)
  929. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  930. if (inode)
  931. return inode;
  932. rgd = recent_rgrp_next(rgd, 1);
  933. break;
  934. case GLR_TRYFAILED:
  935. rgd = recent_rgrp_next(rgd, 0);
  936. break;
  937. default:
  938. return ERR_PTR(error);
  939. }
  940. }
  941. /* Go through full list of rgrps */
  942. begin = rgd = forward_rgrp_get(sdp);
  943. for (;;) {
  944. rg_locked = 0;
  945. if (gfs2_glock_is_locked_by_me(rgd->rd_gl)) {
  946. rg_locked = 1;
  947. error = 0;
  948. } else {
  949. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, flags,
  950. &al->al_rgd_gh);
  951. }
  952. switch (error) {
  953. case 0:
  954. if (try_rgrp_fit(rgd, al))
  955. goto out;
  956. if (rgd->rd_flags & GFS2_RDF_CHECK)
  957. inode = try_rgrp_unlink(rgd, last_unlinked);
  958. if (!rg_locked)
  959. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  960. if (inode)
  961. return inode;
  962. break;
  963. case GLR_TRYFAILED:
  964. skipped++;
  965. break;
  966. default:
  967. return ERR_PTR(error);
  968. }
  969. rgd = gfs2_rgrpd_get_next(rgd);
  970. if (!rgd)
  971. rgd = gfs2_rgrpd_get_first(sdp);
  972. if (rgd == begin) {
  973. if (++loops >= 3)
  974. return ERR_PTR(-ENOSPC);
  975. if (!skipped)
  976. loops++;
  977. flags = 0;
  978. if (loops == 2)
  979. gfs2_log_flush(sdp, NULL);
  980. }
  981. }
  982. out:
  983. if (begin) {
  984. recent_rgrp_add(rgd);
  985. rgd = gfs2_rgrpd_get_next(rgd);
  986. if (!rgd)
  987. rgd = gfs2_rgrpd_get_first(sdp);
  988. forward_rgrp_set(sdp, rgd);
  989. }
  990. return NULL;
  991. }
  992. /**
  993. * gfs2_inplace_reserve_i - Reserve space in the filesystem
  994. * @ip: the inode to reserve space for
  995. *
  996. * Returns: errno
  997. */
  998. int gfs2_inplace_reserve_i(struct gfs2_inode *ip, char *file, unsigned int line)
  999. {
  1000. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1001. struct gfs2_alloc *al = ip->i_alloc;
  1002. struct inode *inode;
  1003. int error = 0;
  1004. u64 last_unlinked = NO_BLOCK;
  1005. if (gfs2_assert_warn(sdp, al->al_requested))
  1006. return -EINVAL;
  1007. try_again:
  1008. /* We need to hold the rindex unless the inode we're using is
  1009. the rindex itself, in which case it's already held. */
  1010. if (ip != GFS2_I(sdp->sd_rindex))
  1011. error = gfs2_rindex_hold(sdp, &al->al_ri_gh);
  1012. else if (!sdp->sd_rgrps) /* We may not have the rindex read in, so: */
  1013. error = gfs2_ri_update_special(ip);
  1014. if (error)
  1015. return error;
  1016. inode = get_local_rgrp(ip, &last_unlinked);
  1017. if (inode) {
  1018. if (ip != GFS2_I(sdp->sd_rindex))
  1019. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1020. if (IS_ERR(inode))
  1021. return PTR_ERR(inode);
  1022. iput(inode);
  1023. gfs2_log_flush(sdp, NULL);
  1024. goto try_again;
  1025. }
  1026. al->al_file = file;
  1027. al->al_line = line;
  1028. return 0;
  1029. }
  1030. /**
  1031. * gfs2_inplace_release - release an inplace reservation
  1032. * @ip: the inode the reservation was taken out on
  1033. *
  1034. * Release a reservation made by gfs2_inplace_reserve().
  1035. */
  1036. void gfs2_inplace_release(struct gfs2_inode *ip)
  1037. {
  1038. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1039. struct gfs2_alloc *al = ip->i_alloc;
  1040. if (gfs2_assert_warn(sdp, al->al_alloced <= al->al_requested) == -1)
  1041. fs_warn(sdp, "al_alloced = %u, al_requested = %u "
  1042. "al_file = %s, al_line = %u\n",
  1043. al->al_alloced, al->al_requested, al->al_file,
  1044. al->al_line);
  1045. al->al_rgd = NULL;
  1046. if (al->al_rgd_gh.gh_gl)
  1047. gfs2_glock_dq_uninit(&al->al_rgd_gh);
  1048. if (ip != GFS2_I(sdp->sd_rindex))
  1049. gfs2_glock_dq_uninit(&al->al_ri_gh);
  1050. }
  1051. /**
  1052. * gfs2_get_block_type - Check a block in a RG is of given type
  1053. * @rgd: the resource group holding the block
  1054. * @block: the block number
  1055. *
  1056. * Returns: The block type (GFS2_BLKST_*)
  1057. */
  1058. unsigned char gfs2_get_block_type(struct gfs2_rgrpd *rgd, u64 block)
  1059. {
  1060. struct gfs2_bitmap *bi = NULL;
  1061. u32 length, rgrp_block, buf_block;
  1062. unsigned int buf;
  1063. unsigned char type;
  1064. length = rgd->rd_length;
  1065. rgrp_block = block - rgd->rd_data0;
  1066. for (buf = 0; buf < length; buf++) {
  1067. bi = rgd->rd_bits + buf;
  1068. if (rgrp_block < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1069. break;
  1070. }
  1071. gfs2_assert(rgd->rd_sbd, buf < length);
  1072. buf_block = rgrp_block - bi->bi_start * GFS2_NBBY;
  1073. type = gfs2_testbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
  1074. bi->bi_len, buf_block);
  1075. return type;
  1076. }
  1077. /**
  1078. * rgblk_search - find a block in @old_state, change allocation
  1079. * state to @new_state
  1080. * @rgd: the resource group descriptor
  1081. * @goal: the goal block within the RG (start here to search for avail block)
  1082. * @old_state: GFS2_BLKST_XXX the before-allocation state to find
  1083. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1084. *
  1085. * Walk rgrp's bitmap to find bits that represent a block in @old_state.
  1086. * Add the found bitmap buffer to the transaction.
  1087. * Set the found bits to @new_state to change block's allocation state.
  1088. *
  1089. * This function never fails, because we wouldn't call it unless we
  1090. * know (from reservation results, etc.) that a block is available.
  1091. *
  1092. * Scope of @goal and returned block is just within rgrp, not the whole
  1093. * filesystem.
  1094. *
  1095. * Returns: the block number allocated
  1096. */
  1097. static u32 rgblk_search(struct gfs2_rgrpd *rgd, u32 goal,
  1098. unsigned char old_state, unsigned char new_state)
  1099. {
  1100. struct gfs2_bitmap *bi = NULL;
  1101. u32 length = rgd->rd_length;
  1102. u32 blk = 0;
  1103. unsigned int buf, x;
  1104. /* Find bitmap block that contains bits for goal block */
  1105. for (buf = 0; buf < length; buf++) {
  1106. bi = rgd->rd_bits + buf;
  1107. if (goal < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1108. break;
  1109. }
  1110. gfs2_assert(rgd->rd_sbd, buf < length);
  1111. /* Convert scope of "goal" from rgrp-wide to within found bit block */
  1112. goal -= bi->bi_start * GFS2_NBBY;
  1113. /* Search (up to entire) bitmap in this rgrp for allocatable block.
  1114. "x <= length", instead of "x < length", because we typically start
  1115. the search in the middle of a bit block, but if we can't find an
  1116. allocatable block anywhere else, we want to be able wrap around and
  1117. search in the first part of our first-searched bit block. */
  1118. for (x = 0; x <= length; x++) {
  1119. /* The GFS2_BLKST_UNLINKED state doesn't apply to the clone
  1120. bitmaps, so we must search the originals for that. */
  1121. const u8 *buffer = bi->bi_bh->b_data + bi->bi_offset;
  1122. if (old_state != GFS2_BLKST_UNLINKED && bi->bi_clone)
  1123. buffer = bi->bi_clone + bi->bi_offset;
  1124. blk = gfs2_bitfit(buffer, bi->bi_len, goal, old_state);
  1125. if (blk != BFITNOENT)
  1126. break;
  1127. /* Try next bitmap block (wrap back to rgrp header if at end) */
  1128. buf = (buf + 1) % length;
  1129. bi = rgd->rd_bits + buf;
  1130. goal = 0;
  1131. }
  1132. if (blk != BFITNOENT && old_state != new_state) {
  1133. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1134. gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
  1135. bi->bi_len, blk, new_state);
  1136. if (bi->bi_clone)
  1137. gfs2_setbit(rgd, bi->bi_clone + bi->bi_offset,
  1138. bi->bi_len, blk, new_state);
  1139. }
  1140. return (blk == BFITNOENT) ? blk : (bi->bi_start * GFS2_NBBY) + blk;
  1141. }
  1142. /**
  1143. * rgblk_free - Change alloc state of given block(s)
  1144. * @sdp: the filesystem
  1145. * @bstart: the start of a run of blocks to free
  1146. * @blen: the length of the block run (all must lie within ONE RG!)
  1147. * @new_state: GFS2_BLKST_XXX the after-allocation block state
  1148. *
  1149. * Returns: Resource group containing the block(s)
  1150. */
  1151. static struct gfs2_rgrpd *rgblk_free(struct gfs2_sbd *sdp, u64 bstart,
  1152. u32 blen, unsigned char new_state)
  1153. {
  1154. struct gfs2_rgrpd *rgd;
  1155. struct gfs2_bitmap *bi = NULL;
  1156. u32 length, rgrp_blk, buf_blk;
  1157. unsigned int buf;
  1158. rgd = gfs2_blk2rgrpd(sdp, bstart);
  1159. if (!rgd) {
  1160. if (gfs2_consist(sdp))
  1161. fs_err(sdp, "block = %llu\n", (unsigned long long)bstart);
  1162. return NULL;
  1163. }
  1164. length = rgd->rd_length;
  1165. rgrp_blk = bstart - rgd->rd_data0;
  1166. while (blen--) {
  1167. for (buf = 0; buf < length; buf++) {
  1168. bi = rgd->rd_bits + buf;
  1169. if (rgrp_blk < (bi->bi_start + bi->bi_len) * GFS2_NBBY)
  1170. break;
  1171. }
  1172. gfs2_assert(rgd->rd_sbd, buf < length);
  1173. buf_blk = rgrp_blk - bi->bi_start * GFS2_NBBY;
  1174. rgrp_blk++;
  1175. if (!bi->bi_clone) {
  1176. bi->bi_clone = kmalloc(bi->bi_bh->b_size,
  1177. GFP_NOFS | __GFP_NOFAIL);
  1178. memcpy(bi->bi_clone + bi->bi_offset,
  1179. bi->bi_bh->b_data + bi->bi_offset,
  1180. bi->bi_len);
  1181. }
  1182. gfs2_trans_add_bh(rgd->rd_gl, bi->bi_bh, 1);
  1183. gfs2_setbit(rgd, bi->bi_bh->b_data + bi->bi_offset,
  1184. bi->bi_len, buf_blk, new_state);
  1185. }
  1186. return rgd;
  1187. }
  1188. /**
  1189. * gfs2_alloc_data - Allocate a data block
  1190. * @ip: the inode to allocate the data block for
  1191. *
  1192. * Returns: the allocated block
  1193. */
  1194. u64 gfs2_alloc_data(struct gfs2_inode *ip)
  1195. {
  1196. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1197. struct gfs2_alloc *al = ip->i_alloc;
  1198. struct gfs2_rgrpd *rgd = al->al_rgd;
  1199. u32 goal, blk;
  1200. u64 block;
  1201. if (rgrp_contains_block(rgd, ip->i_goal))
  1202. goal = ip->i_goal - rgd->rd_data0;
  1203. else
  1204. goal = rgd->rd_last_alloc_data;
  1205. blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
  1206. BUG_ON(blk == BFITNOENT);
  1207. rgd->rd_last_alloc_data = blk;
  1208. block = rgd->rd_data0 + blk;
  1209. ip->i_goal = block;
  1210. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
  1211. rgd->rd_rg.rg_free--;
  1212. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1213. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1214. al->al_alloced++;
  1215. gfs2_statfs_change(sdp, 0, -1, 0);
  1216. gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1217. spin_lock(&sdp->sd_rindex_spin);
  1218. rgd->rd_free_clone--;
  1219. spin_unlock(&sdp->sd_rindex_spin);
  1220. return block;
  1221. }
  1222. /**
  1223. * gfs2_alloc_meta - Allocate a metadata block
  1224. * @ip: the inode to allocate the metadata block for
  1225. *
  1226. * Returns: the allocated block
  1227. */
  1228. u64 gfs2_alloc_meta(struct gfs2_inode *ip)
  1229. {
  1230. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1231. struct gfs2_alloc *al = ip->i_alloc;
  1232. struct gfs2_rgrpd *rgd = al->al_rgd;
  1233. u32 goal, blk;
  1234. u64 block;
  1235. if (rgrp_contains_block(rgd, ip->i_goal))
  1236. goal = ip->i_goal - rgd->rd_data0;
  1237. else
  1238. goal = rgd->rd_last_alloc_meta;
  1239. blk = rgblk_search(rgd, goal, GFS2_BLKST_FREE, GFS2_BLKST_USED);
  1240. BUG_ON(blk == BFITNOENT);
  1241. rgd->rd_last_alloc_meta = blk;
  1242. block = rgd->rd_data0 + blk;
  1243. ip->i_goal = block;
  1244. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
  1245. rgd->rd_rg.rg_free--;
  1246. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1247. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1248. al->al_alloced++;
  1249. gfs2_statfs_change(sdp, 0, -1, 0);
  1250. gfs2_quota_change(ip, +1, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1251. gfs2_trans_add_unrevoke(sdp, block);
  1252. spin_lock(&sdp->sd_rindex_spin);
  1253. rgd->rd_free_clone--;
  1254. spin_unlock(&sdp->sd_rindex_spin);
  1255. return block;
  1256. }
  1257. /**
  1258. * gfs2_alloc_di - Allocate a dinode
  1259. * @dip: the directory that the inode is going in
  1260. *
  1261. * Returns: the block allocated
  1262. */
  1263. u64 gfs2_alloc_di(struct gfs2_inode *dip, u64 *generation)
  1264. {
  1265. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  1266. struct gfs2_alloc *al = dip->i_alloc;
  1267. struct gfs2_rgrpd *rgd = al->al_rgd;
  1268. u32 blk;
  1269. u64 block;
  1270. blk = rgblk_search(rgd, rgd->rd_last_alloc_meta,
  1271. GFS2_BLKST_FREE, GFS2_BLKST_DINODE);
  1272. BUG_ON(blk == BFITNOENT);
  1273. rgd->rd_last_alloc_meta = blk;
  1274. block = rgd->rd_data0 + blk;
  1275. gfs2_assert_withdraw(sdp, rgd->rd_rg.rg_free);
  1276. rgd->rd_rg.rg_free--;
  1277. rgd->rd_rg.rg_dinodes++;
  1278. *generation = rgd->rd_rg.rg_igeneration++;
  1279. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1280. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1281. al->al_alloced++;
  1282. gfs2_statfs_change(sdp, 0, -1, +1);
  1283. gfs2_trans_add_unrevoke(sdp, block);
  1284. spin_lock(&sdp->sd_rindex_spin);
  1285. rgd->rd_free_clone--;
  1286. spin_unlock(&sdp->sd_rindex_spin);
  1287. return block;
  1288. }
  1289. /**
  1290. * gfs2_free_data - free a contiguous run of data block(s)
  1291. * @ip: the inode these blocks are being freed from
  1292. * @bstart: first block of a run of contiguous blocks
  1293. * @blen: the length of the block run
  1294. *
  1295. */
  1296. void gfs2_free_data(struct gfs2_inode *ip, u64 bstart, u32 blen)
  1297. {
  1298. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1299. struct gfs2_rgrpd *rgd;
  1300. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1301. if (!rgd)
  1302. return;
  1303. rgd->rd_rg.rg_free += blen;
  1304. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1305. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1306. gfs2_trans_add_rg(rgd);
  1307. gfs2_statfs_change(sdp, 0, +blen, 0);
  1308. gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1309. }
  1310. /**
  1311. * gfs2_free_meta - free a contiguous run of data block(s)
  1312. * @ip: the inode these blocks are being freed from
  1313. * @bstart: first block of a run of contiguous blocks
  1314. * @blen: the length of the block run
  1315. *
  1316. */
  1317. void gfs2_free_meta(struct gfs2_inode *ip, u64 bstart, u32 blen)
  1318. {
  1319. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1320. struct gfs2_rgrpd *rgd;
  1321. rgd = rgblk_free(sdp, bstart, blen, GFS2_BLKST_FREE);
  1322. if (!rgd)
  1323. return;
  1324. rgd->rd_rg.rg_free += blen;
  1325. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1326. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1327. gfs2_trans_add_rg(rgd);
  1328. gfs2_statfs_change(sdp, 0, +blen, 0);
  1329. gfs2_quota_change(ip, -(s64)blen, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1330. gfs2_meta_wipe(ip, bstart, blen);
  1331. }
  1332. void gfs2_unlink_di(struct inode *inode)
  1333. {
  1334. struct gfs2_inode *ip = GFS2_I(inode);
  1335. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1336. struct gfs2_rgrpd *rgd;
  1337. u64 blkno = ip->i_no_addr;
  1338. rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_UNLINKED);
  1339. if (!rgd)
  1340. return;
  1341. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1342. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1343. gfs2_trans_add_rg(rgd);
  1344. }
  1345. static void gfs2_free_uninit_di(struct gfs2_rgrpd *rgd, u64 blkno)
  1346. {
  1347. struct gfs2_sbd *sdp = rgd->rd_sbd;
  1348. struct gfs2_rgrpd *tmp_rgd;
  1349. tmp_rgd = rgblk_free(sdp, blkno, 1, GFS2_BLKST_FREE);
  1350. if (!tmp_rgd)
  1351. return;
  1352. gfs2_assert_withdraw(sdp, rgd == tmp_rgd);
  1353. if (!rgd->rd_rg.rg_dinodes)
  1354. gfs2_consist_rgrpd(rgd);
  1355. rgd->rd_rg.rg_dinodes--;
  1356. rgd->rd_rg.rg_free++;
  1357. gfs2_trans_add_bh(rgd->rd_gl, rgd->rd_bits[0].bi_bh, 1);
  1358. gfs2_rgrp_out(rgd, rgd->rd_bits[0].bi_bh->b_data);
  1359. gfs2_statfs_change(sdp, 0, +1, -1);
  1360. gfs2_trans_add_rg(rgd);
  1361. }
  1362. void gfs2_free_di(struct gfs2_rgrpd *rgd, struct gfs2_inode *ip)
  1363. {
  1364. gfs2_free_uninit_di(rgd, ip->i_no_addr);
  1365. gfs2_quota_change(ip, -1, ip->i_inode.i_uid, ip->i_inode.i_gid);
  1366. gfs2_meta_wipe(ip, ip->i_no_addr, 1);
  1367. }
  1368. /**
  1369. * gfs2_rlist_add - add a RG to a list of RGs
  1370. * @sdp: the filesystem
  1371. * @rlist: the list of resource groups
  1372. * @block: the block
  1373. *
  1374. * Figure out what RG a block belongs to and add that RG to the list
  1375. *
  1376. * FIXME: Don't use NOFAIL
  1377. *
  1378. */
  1379. void gfs2_rlist_add(struct gfs2_sbd *sdp, struct gfs2_rgrp_list *rlist,
  1380. u64 block)
  1381. {
  1382. struct gfs2_rgrpd *rgd;
  1383. struct gfs2_rgrpd **tmp;
  1384. unsigned int new_space;
  1385. unsigned int x;
  1386. if (gfs2_assert_warn(sdp, !rlist->rl_ghs))
  1387. return;
  1388. rgd = gfs2_blk2rgrpd(sdp, block);
  1389. if (!rgd) {
  1390. if (gfs2_consist(sdp))
  1391. fs_err(sdp, "block = %llu\n", (unsigned long long)block);
  1392. return;
  1393. }
  1394. for (x = 0; x < rlist->rl_rgrps; x++)
  1395. if (rlist->rl_rgd[x] == rgd)
  1396. return;
  1397. if (rlist->rl_rgrps == rlist->rl_space) {
  1398. new_space = rlist->rl_space + 10;
  1399. tmp = kcalloc(new_space, sizeof(struct gfs2_rgrpd *),
  1400. GFP_NOFS | __GFP_NOFAIL);
  1401. if (rlist->rl_rgd) {
  1402. memcpy(tmp, rlist->rl_rgd,
  1403. rlist->rl_space * sizeof(struct gfs2_rgrpd *));
  1404. kfree(rlist->rl_rgd);
  1405. }
  1406. rlist->rl_space = new_space;
  1407. rlist->rl_rgd = tmp;
  1408. }
  1409. rlist->rl_rgd[rlist->rl_rgrps++] = rgd;
  1410. }
  1411. /**
  1412. * gfs2_rlist_alloc - all RGs have been added to the rlist, now allocate
  1413. * and initialize an array of glock holders for them
  1414. * @rlist: the list of resource groups
  1415. * @state: the lock state to acquire the RG lock in
  1416. * @flags: the modifier flags for the holder structures
  1417. *
  1418. * FIXME: Don't use NOFAIL
  1419. *
  1420. */
  1421. void gfs2_rlist_alloc(struct gfs2_rgrp_list *rlist, unsigned int state)
  1422. {
  1423. unsigned int x;
  1424. rlist->rl_ghs = kcalloc(rlist->rl_rgrps, sizeof(struct gfs2_holder),
  1425. GFP_NOFS | __GFP_NOFAIL);
  1426. for (x = 0; x < rlist->rl_rgrps; x++)
  1427. gfs2_holder_init(rlist->rl_rgd[x]->rd_gl,
  1428. state, 0,
  1429. &rlist->rl_ghs[x]);
  1430. }
  1431. /**
  1432. * gfs2_rlist_free - free a resource group list
  1433. * @list: the list of resource groups
  1434. *
  1435. */
  1436. void gfs2_rlist_free(struct gfs2_rgrp_list *rlist)
  1437. {
  1438. unsigned int x;
  1439. kfree(rlist->rl_rgd);
  1440. if (rlist->rl_ghs) {
  1441. for (x = 0; x < rlist->rl_rgrps; x++)
  1442. gfs2_holder_uninit(&rlist->rl_ghs[x]);
  1443. kfree(rlist->rl_ghs);
  1444. }
  1445. }