rgrp.c 45 KB

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