bmap.c 29 KB

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