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