rgrp.c 40 KB

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