bmap.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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. * This routine makes sure that the metadata tree is tall enough to hold
  139. * "size" bytes of data.
  140. *
  141. * Returns: errno
  142. */
  143. static int build_height(struct gfs2_inode *ip, int height)
  144. {
  145. struct gfs2_sbd *sdp = ip->i_sbd;
  146. struct buffer_head *bh, *dibh;
  147. uint64_t block = 0, *bp;
  148. unsigned int x;
  149. int new_block;
  150. int error;
  151. while (ip->i_di.di_height < height) {
  152. error = gfs2_meta_inode_buffer(ip, &dibh);
  153. if (error)
  154. return error;
  155. new_block = 0;
  156. bp = (uint64_t *)(dibh->b_data + sizeof(struct gfs2_dinode));
  157. for (x = 0; x < sdp->sd_diptrs; x++, bp++)
  158. if (*bp) {
  159. new_block = 1;
  160. break;
  161. }
  162. if (new_block) {
  163. /* Get a new block, fill it with the old direct
  164. pointers, and write it out */
  165. block = gfs2_alloc_meta(ip);
  166. bh = gfs2_meta_new(ip->i_gl, block);
  167. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  168. gfs2_metatype_set(bh,
  169. GFS2_METATYPE_IN,
  170. GFS2_FORMAT_IN);
  171. gfs2_buffer_copy_tail(bh,
  172. sizeof(struct gfs2_meta_header),
  173. dibh, sizeof(struct gfs2_dinode));
  174. brelse(bh);
  175. }
  176. /* Set up the new direct pointer and write it out to disk */
  177. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  178. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  179. if (new_block) {
  180. *(uint64_t *)(dibh->b_data +
  181. sizeof(struct gfs2_dinode)) =
  182. cpu_to_be64(block);
  183. ip->i_di.di_blocks++;
  184. }
  185. ip->i_di.di_height++;
  186. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  187. brelse(dibh);
  188. }
  189. return 0;
  190. }
  191. /**
  192. * find_metapath - Find path through the metadata tree
  193. * @ip: The inode pointer
  194. * @mp: The metapath to return the result in
  195. * @block: The disk block to look up
  196. *
  197. * This routine returns a struct metapath structure that defines a path
  198. * through the metadata of inode "ip" to get to block "block".
  199. *
  200. * Example:
  201. * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
  202. * filesystem with a blocksize of 4096.
  203. *
  204. * find_metapath() would return a struct metapath structure set to:
  205. * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
  206. * and mp_list[2] = 165.
  207. *
  208. * That means that in order to get to the block containing the byte at
  209. * offset 101342453, we would load the indirect block pointed to by pointer
  210. * 0 in the dinode. We would then load the indirect block pointed to by
  211. * pointer 48 in that indirect block. We would then load the data block
  212. * pointed to by pointer 165 in that indirect block.
  213. *
  214. * ----------------------------------------
  215. * | Dinode | |
  216. * | | 4|
  217. * | |0 1 2 3 4 5 9|
  218. * | | 6|
  219. * ----------------------------------------
  220. * |
  221. * |
  222. * V
  223. * ----------------------------------------
  224. * | Indirect Block |
  225. * | 5|
  226. * | 4 4 4 4 4 5 5 1|
  227. * |0 5 6 7 8 9 0 1 2|
  228. * ----------------------------------------
  229. * |
  230. * |
  231. * V
  232. * ----------------------------------------
  233. * | Indirect Block |
  234. * | 1 1 1 1 1 5|
  235. * | 6 6 6 6 6 1|
  236. * |0 3 4 5 6 7 2|
  237. * ----------------------------------------
  238. * |
  239. * |
  240. * V
  241. * ----------------------------------------
  242. * | Data block containing offset |
  243. * | 101342453 |
  244. * | |
  245. * | |
  246. * ----------------------------------------
  247. *
  248. */
  249. static void find_metapath(struct gfs2_inode *ip, uint64_t block,
  250. struct metapath *mp)
  251. {
  252. struct gfs2_sbd *sdp = ip->i_sbd;
  253. uint64_t b = block;
  254. unsigned int i;
  255. for (i = ip->i_di.di_height; i--;)
  256. mp->mp_list[i] = (__u16)do_div(b, sdp->sd_inptrs);
  257. }
  258. /**
  259. * metapointer - Return pointer to start of metadata in a buffer
  260. * @bh: The buffer
  261. * @height: The metadata height (0 = dinode)
  262. * @mp: The metapath
  263. *
  264. * Return a pointer to the block number of the next height of the metadata
  265. * tree given a buffer containing the pointer to the current height of the
  266. * metadata tree.
  267. */
  268. static inline uint64_t *metapointer(struct buffer_head *bh,
  269. unsigned int height, struct metapath *mp)
  270. {
  271. unsigned int head_size = (height > 0) ?
  272. sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
  273. return ((uint64_t *)(bh->b_data + head_size)) + mp->mp_list[height];
  274. }
  275. /**
  276. * lookup_block - Get the next metadata block in metadata tree
  277. * @ip: The GFS2 inode
  278. * @bh: Buffer containing the pointers to metadata blocks
  279. * @height: The height of the tree (0 = dinode)
  280. * @mp: The metapath
  281. * @create: Non-zero if we may create a new meatdata block
  282. * @new: Used to indicate if we did create a new metadata block
  283. * @block: the returned disk block number
  284. *
  285. * Given a metatree, complete to a particular height, checks to see if the next
  286. * height of the tree exists. If not the next height of the tree is created.
  287. * The block number of the next height of the metadata tree is returned.
  288. *
  289. */
  290. static void lookup_block(struct gfs2_inode *ip, struct buffer_head *bh,
  291. unsigned int height, struct metapath *mp, int create,
  292. int *new, uint64_t *block)
  293. {
  294. uint64_t *ptr = metapointer(bh, height, mp);
  295. if (*ptr) {
  296. *block = be64_to_cpu(*ptr);
  297. return;
  298. }
  299. *block = 0;
  300. if (!create)
  301. return;
  302. if (height == ip->i_di.di_height - 1 &&
  303. !gfs2_is_dir(ip))
  304. *block = gfs2_alloc_data(ip);
  305. else
  306. *block = gfs2_alloc_meta(ip);
  307. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  308. *ptr = cpu_to_be64(*block);
  309. ip->i_di.di_blocks++;
  310. *new = 1;
  311. }
  312. /**
  313. * gfs2_block_map - Map a block from an inode to a disk block
  314. * @ip: The GFS2 inode
  315. * @lblock: The logical block number
  316. * @new: Value/Result argument (1 = may create/did create new blocks)
  317. * @dblock: the disk block number of the start of an extent
  318. * @extlen: the size of the extent
  319. *
  320. * Find the block number on the current device which corresponds to an
  321. * inode's block. If the block had to be created, "new" will be set.
  322. *
  323. * Returns: errno
  324. */
  325. int gfs2_block_map(struct gfs2_inode *ip, uint64_t lblock, int *new,
  326. uint64_t *dblock, uint32_t *extlen)
  327. {
  328. struct gfs2_sbd *sdp = ip->i_sbd;
  329. struct buffer_head *bh;
  330. struct metapath mp;
  331. int create = *new;
  332. unsigned int bsize;
  333. unsigned int height;
  334. unsigned int end_of_metadata;
  335. unsigned int x;
  336. int error = 0;
  337. *new = 0;
  338. *dblock = 0;
  339. if (extlen)
  340. *extlen = 0;
  341. if (create)
  342. down_write(&ip->i_rw_mutex);
  343. else
  344. down_read(&ip->i_rw_mutex);
  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(ip, 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. lookup_block(ip, bh, end_of_metadata, &mp, create, new, dblock);
  371. if (extlen && *dblock) {
  372. *extlen = 1;
  373. if (!*new) {
  374. uint64_t tmp_dblock;
  375. int tmp_new;
  376. unsigned int nptrs;
  377. nptrs = (end_of_metadata) ? sdp->sd_inptrs :
  378. sdp->sd_diptrs;
  379. while (++mp.mp_list[end_of_metadata] < nptrs) {
  380. lookup_block(ip, bh, end_of_metadata, &mp,
  381. 0, &tmp_new, &tmp_dblock);
  382. if (*dblock + *extlen != tmp_dblock)
  383. break;
  384. (*extlen)++;
  385. }
  386. }
  387. }
  388. brelse(bh);
  389. if (*new) {
  390. error = gfs2_meta_inode_buffer(ip, &bh);
  391. if (!error) {
  392. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  393. gfs2_dinode_out(&ip->i_di, bh->b_data);
  394. brelse(bh);
  395. }
  396. }
  397. out:
  398. if (create)
  399. up_write(&ip->i_rw_mutex);
  400. else
  401. up_read(&ip->i_rw_mutex);
  402. return error;
  403. }
  404. /**
  405. * recursive_scan - recursively scan through the end of a file
  406. * @ip: the inode
  407. * @dibh: the dinode buffer
  408. * @mp: the path through the metadata to the point to start
  409. * @height: the height the recursion is at
  410. * @block: the indirect block to look at
  411. * @first: 1 if this is the first block
  412. * @bc: the call to make for each piece of metadata
  413. * @data: data opaque to this function to pass to @bc
  414. *
  415. * When this is first called @height and @block should be zero and
  416. * @first should be 1.
  417. *
  418. * Returns: errno
  419. */
  420. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  421. struct metapath *mp, unsigned int height,
  422. uint64_t block, int first, block_call_t bc,
  423. void *data)
  424. {
  425. struct gfs2_sbd *sdp = ip->i_sbd;
  426. struct buffer_head *bh = NULL;
  427. uint64_t *top, *bottom;
  428. uint64_t bn;
  429. int error;
  430. int mh_size = sizeof(struct gfs2_meta_header);
  431. if (!height) {
  432. error = gfs2_meta_inode_buffer(ip, &bh);
  433. if (error)
  434. return error;
  435. dibh = bh;
  436. top = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
  437. mp->mp_list[0];
  438. bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
  439. sdp->sd_diptrs;
  440. } else {
  441. error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
  442. if (error)
  443. return error;
  444. top = (uint64_t *)(bh->b_data + mh_size) +
  445. ((first) ? mp->mp_list[height] : 0);
  446. bottom = (uint64_t *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  447. }
  448. error = bc(ip, dibh, bh, top, bottom, height, data);
  449. if (error)
  450. goto out;
  451. if (height < ip->i_di.di_height - 1)
  452. for (; top < bottom; top++, first = 0) {
  453. if (!*top)
  454. continue;
  455. bn = be64_to_cpu(*top);
  456. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  457. first, bc, data);
  458. if (error)
  459. break;
  460. }
  461. out:
  462. brelse(bh);
  463. return error;
  464. }
  465. /**
  466. * do_strip - Look for a layer a particular layer of the file and strip it off
  467. * @ip: the inode
  468. * @dibh: the dinode buffer
  469. * @bh: A buffer of pointers
  470. * @top: The first pointer in the buffer
  471. * @bottom: One more than the last pointer
  472. * @height: the height this buffer is at
  473. * @data: a pointer to a struct strip_mine
  474. *
  475. * Returns: errno
  476. */
  477. static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
  478. struct buffer_head *bh, uint64_t *top, uint64_t *bottom,
  479. unsigned int height, void *data)
  480. {
  481. struct strip_mine *sm = (struct strip_mine *)data;
  482. struct gfs2_sbd *sdp = ip->i_sbd;
  483. struct gfs2_rgrp_list rlist;
  484. uint64_t bn, bstart;
  485. uint32_t blen;
  486. uint64_t *p;
  487. unsigned int rg_blocks = 0;
  488. int metadata;
  489. unsigned int revokes = 0;
  490. int x;
  491. int error;
  492. if (!*top)
  493. sm->sm_first = 0;
  494. if (height != sm->sm_height)
  495. return 0;
  496. if (sm->sm_first) {
  497. top++;
  498. sm->sm_first = 0;
  499. }
  500. metadata = (height != ip->i_di.di_height - 1);
  501. if (metadata)
  502. revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
  503. error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
  504. if (error)
  505. return error;
  506. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  507. bstart = 0;
  508. blen = 0;
  509. for (p = top; p < bottom; p++) {
  510. if (!*p)
  511. continue;
  512. bn = be64_to_cpu(*p);
  513. if (bstart + blen == bn)
  514. blen++;
  515. else {
  516. if (bstart)
  517. gfs2_rlist_add(sdp, &rlist, bstart);
  518. bstart = bn;
  519. blen = 1;
  520. }
  521. }
  522. if (bstart)
  523. gfs2_rlist_add(sdp, &rlist, bstart);
  524. else
  525. goto out; /* Nothing to do */
  526. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
  527. for (x = 0; x < rlist.rl_rgrps; x++) {
  528. struct gfs2_rgrpd *rgd;
  529. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  530. rg_blocks += rgd->rd_ri.ri_length;
  531. }
  532. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  533. if (error)
  534. goto out_rlist;
  535. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  536. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  537. revokes);
  538. if (error)
  539. goto out_rg_gunlock;
  540. down_write(&ip->i_rw_mutex);
  541. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  542. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  543. bstart = 0;
  544. blen = 0;
  545. for (p = top; p < bottom; p++) {
  546. if (!*p)
  547. continue;
  548. bn = be64_to_cpu(*p);
  549. if (bstart + blen == bn)
  550. blen++;
  551. else {
  552. if (bstart) {
  553. if (metadata)
  554. gfs2_free_meta(ip, bstart, blen);
  555. else
  556. gfs2_free_data(ip, bstart, blen);
  557. }
  558. bstart = bn;
  559. blen = 1;
  560. }
  561. *p = 0;
  562. if (!ip->i_di.di_blocks)
  563. gfs2_consist_inode(ip);
  564. ip->i_di.di_blocks--;
  565. }
  566. if (bstart) {
  567. if (metadata)
  568. gfs2_free_meta(ip, bstart, blen);
  569. else
  570. gfs2_free_data(ip, bstart, blen);
  571. }
  572. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  573. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  574. up_write(&ip->i_rw_mutex);
  575. gfs2_trans_end(sdp);
  576. out_rg_gunlock:
  577. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  578. out_rlist:
  579. gfs2_rlist_free(&rlist);
  580. out:
  581. gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
  582. return error;
  583. }
  584. /**
  585. * do_grow - Make a file look bigger than it is
  586. * @ip: the inode
  587. * @size: the size to set the file to
  588. *
  589. * Called with an exclusive lock on @ip.
  590. *
  591. * Returns: errno
  592. */
  593. static int do_grow(struct gfs2_inode *ip, uint64_t size)
  594. {
  595. struct gfs2_sbd *sdp = ip->i_sbd;
  596. struct gfs2_alloc *al;
  597. struct buffer_head *dibh;
  598. unsigned int h;
  599. int error;
  600. al = gfs2_alloc_get(ip);
  601. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  602. if (error)
  603. goto out;
  604. error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
  605. if (error)
  606. goto out_gunlock_q;
  607. al->al_requested = sdp->sd_max_height + RES_DATA;
  608. error = gfs2_inplace_reserve(ip);
  609. if (error)
  610. goto out_gunlock_q;
  611. error = gfs2_trans_begin(sdp,
  612. sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
  613. RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  614. if (error)
  615. goto out_ipres;
  616. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  617. if (gfs2_is_stuffed(ip)) {
  618. error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
  619. NULL);
  620. if (error)
  621. goto out_end_trans;
  622. }
  623. h = calc_tree_height(ip, size);
  624. if (ip->i_di.di_height < h) {
  625. down_write(&ip->i_rw_mutex);
  626. error = build_height(ip, h);
  627. up_write(&ip->i_rw_mutex);
  628. if (error)
  629. goto out_end_trans;
  630. }
  631. }
  632. ip->i_di.di_size = size;
  633. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  634. error = gfs2_meta_inode_buffer(ip, &dibh);
  635. if (error)
  636. goto out_end_trans;
  637. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  638. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  639. brelse(dibh);
  640. out_end_trans:
  641. gfs2_trans_end(sdp);
  642. out_ipres:
  643. gfs2_inplace_release(ip);
  644. out_gunlock_q:
  645. gfs2_quota_unlock(ip);
  646. out:
  647. gfs2_alloc_put(ip);
  648. return error;
  649. }
  650. static int trunc_start(struct gfs2_inode *ip, uint64_t size)
  651. {
  652. struct gfs2_sbd *sdp = ip->i_sbd;
  653. struct buffer_head *dibh;
  654. int journaled = gfs2_is_jdata(ip);
  655. int error;
  656. error = gfs2_trans_begin(sdp,
  657. RES_DINODE + ((journaled) ? RES_JDATA : 0), 0);
  658. if (error)
  659. return error;
  660. error = gfs2_meta_inode_buffer(ip, &dibh);
  661. if (error)
  662. goto out;
  663. if (gfs2_is_stuffed(ip)) {
  664. ip->i_di.di_size = size;
  665. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  666. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  667. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  668. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
  669. error = 1;
  670. } else {
  671. if (size & (uint64_t)(sdp->sd_sb.sb_bsize - 1))
  672. error = gfs2_block_truncate_page(ip->i_vnode->i_mapping);
  673. if (!error) {
  674. ip->i_di.di_size = size;
  675. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  676. ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
  677. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  678. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  679. }
  680. }
  681. brelse(dibh);
  682. out:
  683. gfs2_trans_end(sdp);
  684. return error;
  685. }
  686. static int trunc_dealloc(struct gfs2_inode *ip, uint64_t size)
  687. {
  688. unsigned int height = ip->i_di.di_height;
  689. uint64_t lblock;
  690. struct metapath mp;
  691. int error;
  692. if (!size)
  693. lblock = 0;
  694. else
  695. lblock = (size - 1) >> ip->i_sbd->sd_sb.sb_bsize_shift;
  696. find_metapath(ip, lblock, &mp);
  697. gfs2_alloc_get(ip);
  698. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  699. if (error)
  700. goto out;
  701. while (height--) {
  702. struct strip_mine sm;
  703. sm.sm_first = !!size;
  704. sm.sm_height = height;
  705. error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
  706. if (error)
  707. break;
  708. }
  709. gfs2_quota_unhold(ip);
  710. out:
  711. gfs2_alloc_put(ip);
  712. return error;
  713. }
  714. static int trunc_end(struct gfs2_inode *ip)
  715. {
  716. struct gfs2_sbd *sdp = ip->i_sbd;
  717. struct buffer_head *dibh;
  718. int error;
  719. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  720. if (error)
  721. return error;
  722. down_write(&ip->i_rw_mutex);
  723. error = gfs2_meta_inode_buffer(ip, &dibh);
  724. if (error)
  725. goto out;
  726. if (!ip->i_di.di_size) {
  727. ip->i_di.di_height = 0;
  728. ip->i_di.di_goal_meta =
  729. ip->i_di.di_goal_data =
  730. ip->i_num.no_addr;
  731. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  732. }
  733. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  734. ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
  735. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  736. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  737. brelse(dibh);
  738. out:
  739. up_write(&ip->i_rw_mutex);
  740. gfs2_trans_end(sdp);
  741. return error;
  742. }
  743. /**
  744. * do_shrink - make a file smaller
  745. * @ip: the inode
  746. * @size: the size to make the file
  747. * @truncator: function to truncate the last partial block
  748. *
  749. * Called with an exclusive lock on @ip.
  750. *
  751. * Returns: errno
  752. */
  753. static int do_shrink(struct gfs2_inode *ip, uint64_t size)
  754. {
  755. int error;
  756. error = trunc_start(ip, size);
  757. if (error < 0)
  758. return error;
  759. if (error > 0)
  760. return 0;
  761. error = trunc_dealloc(ip, size);
  762. if (!error)
  763. error = trunc_end(ip);
  764. return error;
  765. }
  766. /**
  767. * gfs2_truncatei - make a file a given size
  768. * @ip: the inode
  769. * @size: the size to make the file
  770. * @truncator: function to truncate the last partial block
  771. *
  772. * The file size can grow, shrink, or stay the same size.
  773. *
  774. * Returns: errno
  775. */
  776. int gfs2_truncatei(struct gfs2_inode *ip, uint64_t size)
  777. {
  778. int error;
  779. if (gfs2_assert_warn(ip->i_sbd, S_ISREG(ip->i_di.di_mode)))
  780. return -EINVAL;
  781. if (size > ip->i_di.di_size)
  782. error = do_grow(ip, size);
  783. else
  784. error = do_shrink(ip, size);
  785. return error;
  786. }
  787. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  788. {
  789. int error;
  790. error = trunc_dealloc(ip, ip->i_di.di_size);
  791. if (!error)
  792. error = trunc_end(ip);
  793. return error;
  794. }
  795. int gfs2_file_dealloc(struct gfs2_inode *ip)
  796. {
  797. return trunc_dealloc(ip, 0);
  798. }
  799. /**
  800. * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
  801. * @ip: the file
  802. * @len: the number of bytes to be written to the file
  803. * @data_blocks: returns the number of data blocks required
  804. * @ind_blocks: returns the number of indirect blocks required
  805. *
  806. */
  807. void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
  808. unsigned int *data_blocks, unsigned int *ind_blocks)
  809. {
  810. struct gfs2_sbd *sdp = ip->i_sbd;
  811. unsigned int tmp;
  812. if (gfs2_is_dir(ip)) {
  813. *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
  814. *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
  815. } else {
  816. *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
  817. *ind_blocks = 3 * (sdp->sd_max_height - 1);
  818. }
  819. for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
  820. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  821. *ind_blocks += tmp;
  822. }
  823. }
  824. /**
  825. * gfs2_write_alloc_required - figure out if a write will require an allocation
  826. * @ip: the file being written to
  827. * @offset: the offset to write to
  828. * @len: the number of bytes being written
  829. * @alloc_required: set to 1 if an alloc is required, 0 otherwise
  830. *
  831. * Returns: errno
  832. */
  833. int gfs2_write_alloc_required(struct gfs2_inode *ip, uint64_t offset,
  834. unsigned int len, int *alloc_required)
  835. {
  836. struct gfs2_sbd *sdp = ip->i_sbd;
  837. uint64_t lblock, lblock_stop, dblock;
  838. uint32_t extlen;
  839. int new = 0;
  840. int error = 0;
  841. *alloc_required = 0;
  842. if (!len)
  843. return 0;
  844. if (gfs2_is_stuffed(ip)) {
  845. if (offset + len >
  846. sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  847. *alloc_required = 1;
  848. return 0;
  849. }
  850. if (gfs2_is_dir(ip)) {
  851. unsigned int bsize = sdp->sd_jbsize;
  852. lblock = offset;
  853. do_div(lblock, bsize);
  854. lblock_stop = offset + len + bsize - 1;
  855. do_div(lblock_stop, bsize);
  856. } else {
  857. unsigned int shift = sdp->sd_sb.sb_bsize_shift;
  858. lblock = offset >> shift;
  859. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  860. }
  861. for (; lblock < lblock_stop; lblock += extlen) {
  862. error = gfs2_block_map(ip, lblock, &new, &dblock, &extlen);
  863. if (error)
  864. return error;
  865. if (!dblock) {
  866. *alloc_required = 1;
  867. return 0;
  868. }
  869. }
  870. return 0;
  871. }