bmap.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 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 v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/gfs2_ondisk.h>
  15. #include <linux/crc32.h>
  16. #include "gfs2.h"
  17. #include "lm_interface.h"
  18. #include "incore.h"
  19. #include "bmap.h"
  20. #include "glock.h"
  21. #include "inode.h"
  22. #include "meta_io.h"
  23. #include "page.h"
  24. #include "quota.h"
  25. #include "rgrp.h"
  26. #include "trans.h"
  27. #include "dir.h"
  28. #include "util.h"
  29. /* This doesn't need to be that large as max 64 bit pointers in a 4k
  30. * block is 512, so __u16 is fine for that. It saves stack space to
  31. * keep it small.
  32. */
  33. struct metapath {
  34. __u16 mp_list[GFS2_MAX_META_HEIGHT];
  35. };
  36. typedef int (*block_call_t) (struct gfs2_inode *ip, struct buffer_head *dibh,
  37. struct buffer_head *bh, uint64_t *top,
  38. uint64_t *bottom, unsigned int height,
  39. void *data);
  40. struct strip_mine {
  41. int sm_first;
  42. unsigned int sm_height;
  43. };
  44. /**
  45. * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
  46. * @ip: The GFS2 inode to unstuff
  47. * @unstuffer: the routine that handles unstuffing a non-zero length file
  48. * @private: private data for the unstuffer
  49. *
  50. * This routine unstuffs a dinode and returns it to a "normal" state such
  51. * that the height can be grown in the traditional way.
  52. *
  53. * Returns: errno
  54. */
  55. int gfs2_unstuff_dinode(struct gfs2_inode *ip, gfs2_unstuffer_t unstuffer,
  56. void *private)
  57. {
  58. struct buffer_head *bh, *dibh;
  59. uint64_t block = 0;
  60. int isdir = gfs2_is_dir(ip);
  61. int error;
  62. down_write(&ip->i_rw_mutex);
  63. error = gfs2_meta_inode_buffer(ip, &dibh);
  64. if (error)
  65. goto out;
  66. if (ip->i_di.di_size) {
  67. /* Get a free block, fill it with the stuffed data,
  68. and write it out to disk */
  69. if (isdir) {
  70. block = gfs2_alloc_meta(ip);
  71. error = gfs2_dir_get_new_buffer(ip, block, &bh);
  72. if (error)
  73. goto out_brelse;
  74. gfs2_buffer_copy_tail(bh,
  75. sizeof(struct gfs2_meta_header),
  76. dibh, sizeof(struct gfs2_dinode));
  77. brelse(bh);
  78. } else {
  79. block = gfs2_alloc_data(ip);
  80. error = unstuffer(ip, dibh, block, private);
  81. if (error)
  82. goto out_brelse;
  83. }
  84. }
  85. /* Set up the pointer to the new block */
  86. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  87. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  88. if (ip->i_di.di_size) {
  89. *(uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode)) =
  90. cpu_to_be64(block);
  91. ip->i_di.di_blocks++;
  92. }
  93. ip->i_di.di_height = 1;
  94. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  95. out_brelse:
  96. brelse(dibh);
  97. out:
  98. up_write(&ip->i_rw_mutex);
  99. return error;
  100. }
  101. /**
  102. * calc_tree_height - Calculate the height of a metadata tree
  103. * @ip: The GFS2 inode
  104. * @size: The proposed size of the file
  105. *
  106. * Work out how tall a metadata tree needs to be in order to accommodate a
  107. * file of a particular size. If size is less than the current size of
  108. * the inode, then the current size of the inode is used instead of the
  109. * supplied one.
  110. *
  111. * Returns: the height the tree should be
  112. */
  113. static unsigned int calc_tree_height(struct gfs2_inode *ip, uint64_t size)
  114. {
  115. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  116. uint64_t *arr;
  117. unsigned int max, height;
  118. if (ip->i_di.di_size > size)
  119. size = ip->i_di.di_size;
  120. if (gfs2_is_dir(ip)) {
  121. arr = sdp->sd_jheightsize;
  122. max = sdp->sd_max_jheight;
  123. } else {
  124. arr = sdp->sd_heightsize;
  125. max = sdp->sd_max_height;
  126. }
  127. for (height = 0; height < max; height++)
  128. if (arr[height] >= size)
  129. break;
  130. return height;
  131. }
  132. /**
  133. * build_height - Build a metadata tree of the requested height
  134. * @ip: The GFS2 inode
  135. * @height: The height to build to
  136. *
  137. *
  138. * Returns: errno
  139. */
  140. static int build_height(struct inode *inode, unsigned height)
  141. {
  142. struct gfs2_inode *ip = GFS2_I(inode);
  143. unsigned new_height = height - ip->i_di.di_height;
  144. struct buffer_head *dibh;
  145. struct buffer_head *blocks[GFS2_MAX_META_HEIGHT];
  146. int error;
  147. u64 *bp;
  148. u64 bn;
  149. unsigned n;
  150. if (height <= ip->i_di.di_height)
  151. return 0;
  152. error = gfs2_meta_inode_buffer(ip, &dibh);
  153. if (error)
  154. return error;
  155. for(n = 0; n < new_height; n++) {
  156. bn = gfs2_alloc_meta(ip);
  157. blocks[n] = gfs2_meta_new(ip->i_gl, bn);
  158. gfs2_trans_add_bh(ip->i_gl, blocks[n], 1);
  159. }
  160. n = 0;
  161. bn = blocks[0]->b_blocknr;
  162. if (new_height > 1) {
  163. for(; n < new_height-1; n++) {
  164. gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN,
  165. GFS2_FORMAT_IN);
  166. gfs2_buffer_clear_tail(blocks[n],
  167. sizeof(struct gfs2_meta_header));
  168. bp = (u64 *)(blocks[n]->b_data +
  169. sizeof(struct gfs2_meta_header));
  170. *bp = cpu_to_be64(blocks[n+1]->b_blocknr);
  171. brelse(blocks[n]);
  172. blocks[n] = NULL;
  173. }
  174. }
  175. gfs2_metatype_set(blocks[n], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  176. gfs2_buffer_copy_tail(blocks[n], sizeof(struct gfs2_meta_header),
  177. dibh, sizeof(struct gfs2_dinode));
  178. brelse(blocks[n]);
  179. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  180. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  181. bp = (u64 *)(dibh->b_data + sizeof(struct gfs2_dinode));
  182. *bp = cpu_to_be64(bn);
  183. ip->i_di.di_height += new_height;
  184. ip->i_di.di_blocks += new_height;
  185. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  186. brelse(dibh);
  187. return error;
  188. }
  189. /**
  190. * find_metapath - Find path through the metadata tree
  191. * @ip: The inode pointer
  192. * @mp: The metapath to return the result in
  193. * @block: The disk block to look up
  194. *
  195. * This routine returns a struct metapath structure that defines a path
  196. * through the metadata of inode "ip" to get to block "block".
  197. *
  198. * Example:
  199. * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
  200. * filesystem with a blocksize of 4096.
  201. *
  202. * find_metapath() would return a struct metapath structure set to:
  203. * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
  204. * and mp_list[2] = 165.
  205. *
  206. * That means that in order to get to the block containing the byte at
  207. * offset 101342453, we would load the indirect block pointed to by pointer
  208. * 0 in the dinode. We would then load the indirect block pointed to by
  209. * pointer 48 in that indirect block. We would then load the data block
  210. * pointed to by pointer 165 in that indirect block.
  211. *
  212. * ----------------------------------------
  213. * | Dinode | |
  214. * | | 4|
  215. * | |0 1 2 3 4 5 9|
  216. * | | 6|
  217. * ----------------------------------------
  218. * |
  219. * |
  220. * V
  221. * ----------------------------------------
  222. * | Indirect Block |
  223. * | 5|
  224. * | 4 4 4 4 4 5 5 1|
  225. * |0 5 6 7 8 9 0 1 2|
  226. * ----------------------------------------
  227. * |
  228. * |
  229. * V
  230. * ----------------------------------------
  231. * | Indirect Block |
  232. * | 1 1 1 1 1 5|
  233. * | 6 6 6 6 6 1|
  234. * |0 3 4 5 6 7 2|
  235. * ----------------------------------------
  236. * |
  237. * |
  238. * V
  239. * ----------------------------------------
  240. * | Data block containing offset |
  241. * | 101342453 |
  242. * | |
  243. * | |
  244. * ----------------------------------------
  245. *
  246. */
  247. static void find_metapath(struct gfs2_inode *ip, uint64_t block,
  248. struct metapath *mp)
  249. {
  250. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  251. uint64_t b = block;
  252. unsigned int i;
  253. for (i = ip->i_di.di_height; i--;)
  254. mp->mp_list[i] = (__u16)do_div(b, sdp->sd_inptrs);
  255. }
  256. /**
  257. * metapointer - Return pointer to start of metadata in a buffer
  258. * @bh: The buffer
  259. * @height: The metadata height (0 = dinode)
  260. * @mp: The metapath
  261. *
  262. * Return a pointer to the block number of the next height of the metadata
  263. * tree given a buffer containing the pointer to the current height of the
  264. * metadata tree.
  265. */
  266. static inline u64 *metapointer(struct buffer_head *bh, int *boundary,
  267. unsigned int height, const struct metapath *mp)
  268. {
  269. unsigned int head_size = (height > 0) ?
  270. sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
  271. u64 *ptr;
  272. *boundary = 0;
  273. ptr = ((u64 *)(bh->b_data + head_size)) + mp->mp_list[height];
  274. if (ptr + 1 == (u64*)(bh->b_data + bh->b_size))
  275. *boundary = 1;
  276. return ptr;
  277. }
  278. /**
  279. * lookup_block - Get the next metadata block in metadata tree
  280. * @ip: The GFS2 inode
  281. * @bh: Buffer containing the pointers to metadata blocks
  282. * @height: The height of the tree (0 = dinode)
  283. * @mp: The metapath
  284. * @create: Non-zero if we may create a new meatdata block
  285. * @new: Used to indicate if we did create a new metadata block
  286. * @block: the returned disk block number
  287. *
  288. * Given a metatree, complete to a particular height, checks to see if the next
  289. * height of the tree exists. If not the next height of the tree is created.
  290. * The block number of the next height of the metadata tree is returned.
  291. *
  292. */
  293. static int lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
  294. unsigned int height, struct metapath *mp, int create,
  295. int *new, uint64_t *block)
  296. {
  297. int boundary;
  298. uint64_t *ptr = metapointer(bh, &boundary, height, mp);
  299. if (*ptr) {
  300. *block = be64_to_cpu(*ptr);
  301. return boundary;
  302. }
  303. *block = 0;
  304. if (!create)
  305. return 0;
  306. if (height == ip->i_di.di_height - 1 && !gfs2_is_dir(ip))
  307. *block = gfs2_alloc_data(ip);
  308. else
  309. *block = gfs2_alloc_meta(ip);
  310. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  311. *ptr = cpu_to_be64(*block);
  312. ip->i_di.di_blocks++;
  313. *new = 1;
  314. return 0;
  315. }
  316. /**
  317. * gfs2_block_pointers - Map a block from an inode to a disk block
  318. * @inode: The inode
  319. * @lblock: The logical block number
  320. * @new: Value/Result argument (1 = may create/did create new blocks)
  321. * @boundary: gets set if we've hit a block boundary
  322. * @mp: metapath to use
  323. *
  324. * Find the block number on the current device which corresponds to an
  325. * inode's block. If the block had to be created, "new" will be set.
  326. *
  327. * Returns: errno
  328. */
  329. static struct buffer_head *gfs2_block_pointers(struct inode *inode, u64 lblock,
  330. int *new, u64 *dblock,
  331. int *boundary,
  332. struct metapath *mp)
  333. {
  334. struct gfs2_inode *ip = GFS2_I(inode);
  335. struct gfs2_sbd *sdp = GFS2_SB(inode);
  336. struct buffer_head *bh;
  337. int create = *new;
  338. unsigned int bsize;
  339. unsigned int height;
  340. unsigned int end_of_metadata;
  341. unsigned int x;
  342. int error = 0;
  343. *new = 0;
  344. *dblock = 0;
  345. if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
  346. goto out;
  347. bsize = (gfs2_is_dir(ip)) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
  348. height = calc_tree_height(ip, (lblock + 1) * bsize);
  349. if (ip->i_di.di_height < height) {
  350. if (!create)
  351. goto out;
  352. error = build_height(inode, height);
  353. if (error)
  354. goto out;
  355. }
  356. find_metapath(ip, lblock, mp);
  357. end_of_metadata = ip->i_di.di_height - 1;
  358. error = gfs2_meta_inode_buffer(ip, &bh);
  359. if (error)
  360. goto out;
  361. for (x = 0; x < end_of_metadata; x++) {
  362. lookup_block(ip, bh, x, mp, create, new, dblock);
  363. brelse(bh);
  364. if (!*dblock)
  365. goto out;
  366. error = gfs2_meta_indirect_buffer(ip, x+1, *dblock, *new, &bh);
  367. if (error)
  368. goto out;
  369. }
  370. *boundary = lookup_block(ip, bh, end_of_metadata, mp, create, new, dblock);
  371. if (*new) {
  372. struct buffer_head *dibh;
  373. error = gfs2_meta_inode_buffer(ip, &dibh);
  374. if (!error) {
  375. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  376. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  377. brelse(dibh);
  378. }
  379. }
  380. return bh;
  381. out:
  382. return ERR_PTR(error);
  383. }
  384. static inline void bmap_lock(struct inode *inode, int create)
  385. {
  386. struct gfs2_inode *ip = GFS2_I(inode);
  387. if (create)
  388. down_write(&ip->i_rw_mutex);
  389. else
  390. down_read(&ip->i_rw_mutex);
  391. }
  392. static inline void bmap_unlock(struct inode *inode, int create)
  393. {
  394. struct gfs2_inode *ip = GFS2_I(inode);
  395. if (create)
  396. up_write(&ip->i_rw_mutex);
  397. else
  398. up_read(&ip->i_rw_mutex);
  399. }
  400. int gfs2_block_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, int *boundary)
  401. {
  402. struct metapath mp;
  403. struct buffer_head *bh;
  404. int create = *new;
  405. bmap_lock(inode, create);
  406. bh = gfs2_block_pointers(inode, lblock, new, dblock, boundary, &mp);
  407. bmap_unlock(inode, create);
  408. if (!bh)
  409. return 0;
  410. if (IS_ERR(bh))
  411. return PTR_ERR(bh);
  412. brelse(bh);
  413. return 0;
  414. }
  415. int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
  416. {
  417. struct gfs2_inode *ip = GFS2_I(inode);
  418. struct gfs2_sbd *sdp = GFS2_SB(inode);
  419. struct metapath mp;
  420. struct buffer_head *bh;
  421. int boundary;
  422. int create = *new;
  423. BUG_ON(!extlen);
  424. BUG_ON(!dblock);
  425. BUG_ON(!new);
  426. bmap_lock(inode, create);
  427. bh = gfs2_block_pointers(inode, lblock, new, dblock, &boundary, &mp);
  428. *extlen = 1;
  429. if (bh && !IS_ERR(bh) && *dblock && !*new) {
  430. u64 tmp_dblock;
  431. int tmp_new;
  432. unsigned int nptrs;
  433. unsigned end_of_metadata = ip->i_di.di_height - 1;
  434. nptrs = (end_of_metadata) ? sdp->sd_inptrs : sdp->sd_diptrs;
  435. while (++mp.mp_list[end_of_metadata] < nptrs) {
  436. lookup_block(ip, bh, end_of_metadata, &mp, 0, &tmp_new, &tmp_dblock);
  437. if (*dblock + *extlen != tmp_dblock)
  438. break;
  439. (*extlen)++;
  440. }
  441. }
  442. bmap_unlock(inode, create);
  443. if (!bh)
  444. return 0;
  445. if (IS_ERR(bh))
  446. return PTR_ERR(bh);
  447. brelse(bh);
  448. return 0;
  449. }
  450. /**
  451. * recursive_scan - recursively scan through the end of a file
  452. * @ip: the inode
  453. * @dibh: the dinode buffer
  454. * @mp: the path through the metadata to the point to start
  455. * @height: the height the recursion is at
  456. * @block: the indirect block to look at
  457. * @first: 1 if this is the first block
  458. * @bc: the call to make for each piece of metadata
  459. * @data: data opaque to this function to pass to @bc
  460. *
  461. * When this is first called @height and @block should be zero and
  462. * @first should be 1.
  463. *
  464. * Returns: errno
  465. */
  466. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  467. struct metapath *mp, unsigned int height,
  468. uint64_t block, int first, block_call_t bc,
  469. void *data)
  470. {
  471. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  472. struct buffer_head *bh = NULL;
  473. uint64_t *top, *bottom;
  474. uint64_t bn;
  475. int error;
  476. int mh_size = sizeof(struct gfs2_meta_header);
  477. if (!height) {
  478. error = gfs2_meta_inode_buffer(ip, &bh);
  479. if (error)
  480. return error;
  481. dibh = bh;
  482. top = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
  483. mp->mp_list[0];
  484. bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
  485. sdp->sd_diptrs;
  486. } else {
  487. error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
  488. if (error)
  489. return error;
  490. top = (uint64_t *)(bh->b_data + mh_size) +
  491. ((first) ? mp->mp_list[height] : 0);
  492. bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  493. }
  494. error = bc(ip, dibh, bh, top, bottom, height, data);
  495. if (error)
  496. goto out;
  497. if (height < ip->i_di.di_height - 1)
  498. for (; top < bottom; top++, first = 0) {
  499. if (!*top)
  500. continue;
  501. bn = be64_to_cpu(*top);
  502. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  503. first, bc, data);
  504. if (error)
  505. break;
  506. }
  507. out:
  508. brelse(bh);
  509. return error;
  510. }
  511. /**
  512. * do_strip - Look for a layer a particular layer of the file and strip it off
  513. * @ip: the inode
  514. * @dibh: the dinode buffer
  515. * @bh: A buffer of pointers
  516. * @top: The first pointer in the buffer
  517. * @bottom: One more than the last pointer
  518. * @height: the height this buffer is at
  519. * @data: a pointer to a struct strip_mine
  520. *
  521. * Returns: errno
  522. */
  523. static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
  524. struct buffer_head *bh, uint64_t *top, uint64_t *bottom,
  525. unsigned int height, void *data)
  526. {
  527. struct strip_mine *sm = data;
  528. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  529. struct gfs2_rgrp_list rlist;
  530. uint64_t bn, bstart;
  531. uint32_t blen;
  532. uint64_t *p;
  533. unsigned int rg_blocks = 0;
  534. int metadata;
  535. unsigned int revokes = 0;
  536. int x;
  537. int error;
  538. if (!*top)
  539. sm->sm_first = 0;
  540. if (height != sm->sm_height)
  541. return 0;
  542. if (sm->sm_first) {
  543. top++;
  544. sm->sm_first = 0;
  545. }
  546. metadata = (height != ip->i_di.di_height - 1);
  547. if (metadata)
  548. revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
  549. error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
  550. if (error)
  551. return error;
  552. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  553. bstart = 0;
  554. blen = 0;
  555. for (p = top; p < bottom; p++) {
  556. if (!*p)
  557. continue;
  558. bn = be64_to_cpu(*p);
  559. if (bstart + blen == bn)
  560. blen++;
  561. else {
  562. if (bstart)
  563. gfs2_rlist_add(sdp, &rlist, bstart);
  564. bstart = bn;
  565. blen = 1;
  566. }
  567. }
  568. if (bstart)
  569. gfs2_rlist_add(sdp, &rlist, bstart);
  570. else
  571. goto out; /* Nothing to do */
  572. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
  573. for (x = 0; x < rlist.rl_rgrps; x++) {
  574. struct gfs2_rgrpd *rgd;
  575. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  576. rg_blocks += rgd->rd_ri.ri_length;
  577. }
  578. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  579. if (error)
  580. goto out_rlist;
  581. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  582. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  583. revokes);
  584. if (error)
  585. goto out_rg_gunlock;
  586. down_write(&ip->i_rw_mutex);
  587. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  588. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  589. bstart = 0;
  590. blen = 0;
  591. for (p = top; p < bottom; p++) {
  592. if (!*p)
  593. continue;
  594. bn = be64_to_cpu(*p);
  595. if (bstart + blen == bn)
  596. blen++;
  597. else {
  598. if (bstart) {
  599. if (metadata)
  600. gfs2_free_meta(ip, bstart, blen);
  601. else
  602. gfs2_free_data(ip, bstart, blen);
  603. }
  604. bstart = bn;
  605. blen = 1;
  606. }
  607. *p = 0;
  608. if (!ip->i_di.di_blocks)
  609. gfs2_consist_inode(ip);
  610. ip->i_di.di_blocks--;
  611. }
  612. if (bstart) {
  613. if (metadata)
  614. gfs2_free_meta(ip, bstart, blen);
  615. else
  616. gfs2_free_data(ip, bstart, blen);
  617. }
  618. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  619. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  620. up_write(&ip->i_rw_mutex);
  621. gfs2_trans_end(sdp);
  622. out_rg_gunlock:
  623. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  624. out_rlist:
  625. gfs2_rlist_free(&rlist);
  626. out:
  627. gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
  628. return error;
  629. }
  630. /**
  631. * do_grow - Make a file look bigger than it is
  632. * @ip: the inode
  633. * @size: the size to set the file to
  634. *
  635. * Called with an exclusive lock on @ip.
  636. *
  637. * Returns: errno
  638. */
  639. static int do_grow(struct gfs2_inode *ip, uint64_t size)
  640. {
  641. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  642. struct gfs2_alloc *al;
  643. struct buffer_head *dibh;
  644. unsigned int h;
  645. int error;
  646. al = gfs2_alloc_get(ip);
  647. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  648. if (error)
  649. goto out;
  650. error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
  651. if (error)
  652. goto out_gunlock_q;
  653. al->al_requested = sdp->sd_max_height + RES_DATA;
  654. error = gfs2_inplace_reserve(ip);
  655. if (error)
  656. goto out_gunlock_q;
  657. error = gfs2_trans_begin(sdp,
  658. sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
  659. RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  660. if (error)
  661. goto out_ipres;
  662. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  663. if (gfs2_is_stuffed(ip)) {
  664. error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
  665. NULL);
  666. if (error)
  667. goto out_end_trans;
  668. }
  669. h = calc_tree_height(ip, size);
  670. if (ip->i_di.di_height < h) {
  671. down_write(&ip->i_rw_mutex);
  672. error = build_height(&ip->i_inode, h);
  673. up_write(&ip->i_rw_mutex);
  674. if (error)
  675. goto out_end_trans;
  676. }
  677. }
  678. ip->i_di.di_size = size;
  679. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  680. error = gfs2_meta_inode_buffer(ip, &dibh);
  681. if (error)
  682. goto out_end_trans;
  683. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  684. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  685. brelse(dibh);
  686. out_end_trans:
  687. gfs2_trans_end(sdp);
  688. out_ipres:
  689. gfs2_inplace_release(ip);
  690. out_gunlock_q:
  691. gfs2_quota_unlock(ip);
  692. out:
  693. gfs2_alloc_put(ip);
  694. return error;
  695. }
  696. static int trunc_start(struct gfs2_inode *ip, uint64_t size)
  697. {
  698. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  699. struct buffer_head *dibh;
  700. int journaled = gfs2_is_jdata(ip);
  701. int error;
  702. error = gfs2_trans_begin(sdp,
  703. RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
  704. if (error)
  705. return error;
  706. error = gfs2_meta_inode_buffer(ip, &dibh);
  707. if (error)
  708. goto out;
  709. if (gfs2_is_stuffed(ip)) {
  710. ip->i_di.di_size = size;
  711. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  712. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  713. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  714. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
  715. error = 1;
  716. } else {
  717. if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
  718. error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
  719. if (!error) {
  720. ip->i_di.di_size = size;
  721. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  722. ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
  723. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  724. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  725. }
  726. }
  727. brelse(dibh);
  728. out:
  729. gfs2_trans_end(sdp);
  730. return error;
  731. }
  732. static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
  733. {
  734. unsigned int height = ip->i_di.di_height;
  735. uint64_t lblock;
  736. struct metapath mp;
  737. int error;
  738. if (!size)
  739. lblock = 0;
  740. else
  741. lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
  742. find_metapath(ip, lblock, &mp);
  743. gfs2_alloc_get(ip);
  744. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  745. if (error)
  746. goto out;
  747. while (height--) {
  748. struct strip_mine sm;
  749. sm.sm_first = !!size;
  750. sm.sm_height = height;
  751. error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
  752. if (error)
  753. break;
  754. }
  755. gfs2_quota_unhold(ip);
  756. out:
  757. gfs2_alloc_put(ip);
  758. return error;
  759. }
  760. static int trunc_end(struct gfs2_inode *ip)
  761. {
  762. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  763. struct buffer_head *dibh;
  764. int error;
  765. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  766. if (error)
  767. return error;
  768. down_write(&ip->i_rw_mutex);
  769. error = gfs2_meta_inode_buffer(ip, &dibh);
  770. if (error)
  771. goto out;
  772. if (!ip->i_di.di_size) {
  773. ip->i_di.di_height = 0;
  774. ip->i_di.di_goal_meta =
  775. ip->i_di.di_goal_data =
  776. ip->i_num.no_addr;
  777. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  778. }
  779. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  780. ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
  781. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  782. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  783. brelse(dibh);
  784. out:
  785. up_write(&ip->i_rw_mutex);
  786. gfs2_trans_end(sdp);
  787. return error;
  788. }
  789. /**
  790. * do_shrink - make a file smaller
  791. * @ip: the inode
  792. * @size: the size to make the file
  793. * @truncator: function to truncate the last partial block
  794. *
  795. * Called with an exclusive lock on @ip.
  796. *
  797. * Returns: errno
  798. */
  799. static int do_shrink(struct gfs2_inode *ip, uint64_t size)
  800. {
  801. int error;
  802. error = trunc_start(ip, size);
  803. if (error < 0)
  804. return error;
  805. if (error > 0)
  806. return 0;
  807. error = trunc_dealloc(ip, size);
  808. if (!error)
  809. error = trunc_end(ip);
  810. return error;
  811. }
  812. /**
  813. * gfs2_truncatei - make a file a given size
  814. * @ip: the inode
  815. * @size: the size to make the file
  816. * @truncator: function to truncate the last partial block
  817. *
  818. * The file size can grow, shrink, or stay the same size.
  819. *
  820. * Returns: errno
  821. */
  822. int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
  823. {
  824. int error;
  825. if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_di.di_mode)))
  826. return -EINVAL;
  827. if (size > ip->i_di.di_size)
  828. error = do_grow(ip, size);
  829. else
  830. error = do_shrink(ip, size);
  831. return error;
  832. }
  833. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  834. {
  835. int error;
  836. error = trunc_dealloc(ip, ip->i_di.di_size);
  837. if (!error)
  838. error = trunc_end(ip);
  839. return error;
  840. }
  841. int gfs2_file_dealloc(struct gfs2_inode *ip)
  842. {
  843. return trunc_dealloc(ip, 0);
  844. }
  845. /**
  846. * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
  847. * @ip: the file
  848. * @len: the number of bytes to be written to the file
  849. * @data_blocks: returns the number of data blocks required
  850. * @ind_blocks: returns the number of indirect blocks required
  851. *
  852. */
  853. void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
  854. unsigned int *data_blocks, unsigned int *ind_blocks)
  855. {
  856. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  857. unsigned int tmp;
  858. if (gfs2_is_dir(ip)) {
  859. *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
  860. *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
  861. } else {
  862. *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
  863. *ind_blocks = 3 * (sdp->sd_max_height - 1);
  864. }
  865. for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
  866. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  867. *ind_blocks += tmp;
  868. }
  869. }
  870. /**
  871. * gfs2_write_alloc_required - figure out if a write will require an allocation
  872. * @ip: the file being written to
  873. * @offset: the offset to write to
  874. * @len: the number of bytes being written
  875. * @alloc_required: set to 1 if an alloc is required, 0 otherwise
  876. *
  877. * Returns: errno
  878. */
  879. int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
  880. unsigned int len, int *alloc_required)
  881. {
  882. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  883. uint64_t lblock, lblock_stop, dblock;
  884. uint32_t extlen;
  885. int new = 0;
  886. int error = 0;
  887. *alloc_required = 0;
  888. if (!len)
  889. return 0;
  890. if (gfs2_is_stuffed(ip)) {
  891. if (offset + len >
  892. sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  893. *alloc_required = 1;
  894. return 0;
  895. }
  896. if (gfs2_is_dir(ip)) {
  897. unsigned int bsize = sdp->sd_jbsize;
  898. lblock = offset;
  899. do_div(lblock, bsize);
  900. lblock_stop = offset + len + bsize - 1;
  901. do_div(lblock_stop, bsize);
  902. } else {
  903. unsigned int shift = sdp->sd_sb.sb_bsize_shift;
  904. lblock = offset >> shift;
  905. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  906. }
  907. for (; lblock < lblock_stop; lblock += extlen) {
  908. error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
  909. if (error)
  910. return error;
  911. if (!dblock) {
  912. *alloc_required = 1;
  913. return 0;
  914. }
  915. }
  916. return 0;
  917. }