rgrp.c 47 KB

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