rgrp.c 42 KB

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