bmap.c 26 KB

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