bmap.c 25 KB

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