rgrp.c 41 KB

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