bmap.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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, 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. u64 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. *(u64 *)(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, u64 size)
  160. {
  161. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  162. u64 *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, u64 block,
  294. struct metapath *mp)
  295. {
  296. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  297. u64 b = block;
  298. unsigned int i;
  299. for (i = ip->i_di.di_height; i--;)
  300. mp->mp_list[i] = 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, u64 *block)
  342. {
  343. int boundary;
  344. u64 *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. * @map_bh: The bh to be mapped
  367. * @mp: metapath to use
  368. *
  369. * Find the block number on the current device which corresponds to an
  370. * inode's block. If the block had to be created, "new" will be set.
  371. *
  372. * Returns: errno
  373. */
  374. static int gfs2_block_pointers(struct inode *inode, u64 lblock, int create,
  375. struct buffer_head *bh_map, struct metapath *mp,
  376. unsigned int maxlen)
  377. {
  378. struct gfs2_inode *ip = GFS2_I(inode);
  379. struct gfs2_sbd *sdp = GFS2_SB(inode);
  380. struct buffer_head *bh;
  381. unsigned int bsize;
  382. unsigned int height;
  383. unsigned int end_of_metadata;
  384. unsigned int x;
  385. int error = 0;
  386. int new = 0;
  387. u64 dblock = 0;
  388. int boundary;
  389. if (gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
  390. return 0;
  391. bsize = gfs2_is_dir(ip) ? sdp->sd_jbsize : sdp->sd_sb.sb_bsize;
  392. height = calc_tree_height(ip, (lblock + 1) * bsize);
  393. if (ip->i_di.di_height < height) {
  394. if (!create)
  395. return 0;
  396. error = build_height(inode, height);
  397. if (error)
  398. return error;
  399. }
  400. find_metapath(ip, lblock, mp);
  401. end_of_metadata = ip->i_di.di_height - 1;
  402. error = gfs2_meta_inode_buffer(ip, &bh);
  403. if (error)
  404. return error;
  405. for (x = 0; x < end_of_metadata; x++) {
  406. lookup_block(ip, bh, x, mp, create, &new, &dblock);
  407. brelse(bh);
  408. if (!dblock)
  409. return 0;
  410. error = gfs2_meta_indirect_buffer(ip, x+1, dblock, new, &bh);
  411. if (error)
  412. return error;
  413. }
  414. boundary = lookup_block(ip, bh, end_of_metadata, mp, create, &new, &dblock);
  415. clear_buffer_mapped(bh_map);
  416. clear_buffer_new(bh_map);
  417. clear_buffer_boundary(bh_map);
  418. if (dblock) {
  419. map_bh(bh_map, inode->i_sb, dblock);
  420. if (boundary)
  421. set_buffer_boundary(bh);
  422. if (new) {
  423. struct buffer_head *dibh;
  424. error = gfs2_meta_inode_buffer(ip, &dibh);
  425. if (!error) {
  426. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  427. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  428. brelse(dibh);
  429. }
  430. set_buffer_new(bh_map);
  431. goto out_brelse;
  432. }
  433. while(--maxlen && !buffer_boundary(bh_map)) {
  434. u64 eblock;
  435. mp->mp_list[end_of_metadata]++;
  436. boundary = lookup_block(ip, bh, end_of_metadata, mp, 0, &new, &eblock);
  437. if (eblock != ++dblock)
  438. break;
  439. bh_map->b_size += (1 << inode->i_blkbits);
  440. if (boundary)
  441. set_buffer_boundary(bh_map);
  442. }
  443. }
  444. out_brelse:
  445. brelse(bh);
  446. return 0;
  447. }
  448. static inline void bmap_lock(struct inode *inode, int create)
  449. {
  450. struct gfs2_inode *ip = GFS2_I(inode);
  451. if (create)
  452. down_write(&ip->i_rw_mutex);
  453. else
  454. down_read(&ip->i_rw_mutex);
  455. }
  456. static inline void bmap_unlock(struct inode *inode, int create)
  457. {
  458. struct gfs2_inode *ip = GFS2_I(inode);
  459. if (create)
  460. up_write(&ip->i_rw_mutex);
  461. else
  462. up_read(&ip->i_rw_mutex);
  463. }
  464. int gfs2_block_map(struct inode *inode, u64 lblock, int create,
  465. struct buffer_head *bh, unsigned int maxlen)
  466. {
  467. struct metapath mp;
  468. int ret;
  469. bmap_lock(inode, create);
  470. ret = gfs2_block_pointers(inode, lblock, create, bh, &mp, maxlen);
  471. bmap_unlock(inode, create);
  472. return ret;
  473. }
  474. int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
  475. {
  476. struct metapath mp;
  477. struct buffer_head bh = { .b_state = 0, .b_blocknr = 0, .b_size = 0 };
  478. int ret;
  479. int create = *new;
  480. BUG_ON(!extlen);
  481. BUG_ON(!dblock);
  482. BUG_ON(!new);
  483. bmap_lock(inode, create);
  484. ret = gfs2_block_pointers(inode, lblock, create, &bh, &mp, *extlen);
  485. bmap_unlock(inode, create);
  486. *extlen = bh.b_size >> inode->i_blkbits;
  487. *dblock = bh.b_blocknr;
  488. if (buffer_new(&bh))
  489. *new = 1;
  490. else
  491. *new = 0;
  492. return ret;
  493. }
  494. /**
  495. * recursive_scan - recursively scan through the end of a file
  496. * @ip: the inode
  497. * @dibh: the dinode buffer
  498. * @mp: the path through the metadata to the point to start
  499. * @height: the height the recursion is at
  500. * @block: the indirect block to look at
  501. * @first: 1 if this is the first block
  502. * @bc: the call to make for each piece of metadata
  503. * @data: data opaque to this function to pass to @bc
  504. *
  505. * When this is first called @height and @block should be zero and
  506. * @first should be 1.
  507. *
  508. * Returns: errno
  509. */
  510. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  511. struct metapath *mp, unsigned int height,
  512. u64 block, int first, block_call_t bc,
  513. void *data)
  514. {
  515. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  516. struct buffer_head *bh = NULL;
  517. u64 *top, *bottom;
  518. u64 bn;
  519. int error;
  520. int mh_size = sizeof(struct gfs2_meta_header);
  521. if (!height) {
  522. error = gfs2_meta_inode_buffer(ip, &bh);
  523. if (error)
  524. return error;
  525. dibh = bh;
  526. top = (u64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
  527. bottom = (u64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
  528. } else {
  529. error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
  530. if (error)
  531. return error;
  532. top = (u64 *)(bh->b_data + mh_size) +
  533. (first ? mp->mp_list[height] : 0);
  534. bottom = (u64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  535. }
  536. error = bc(ip, dibh, bh, top, bottom, height, data);
  537. if (error)
  538. goto out;
  539. if (height < ip->i_di.di_height - 1)
  540. for (; top < bottom; top++, first = 0) {
  541. if (!*top)
  542. continue;
  543. bn = be64_to_cpu(*top);
  544. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  545. first, bc, data);
  546. if (error)
  547. break;
  548. }
  549. out:
  550. brelse(bh);
  551. return error;
  552. }
  553. /**
  554. * do_strip - Look for a layer a particular layer of the file and strip it off
  555. * @ip: the inode
  556. * @dibh: the dinode buffer
  557. * @bh: A buffer of pointers
  558. * @top: The first pointer in the buffer
  559. * @bottom: One more than the last pointer
  560. * @height: the height this buffer is at
  561. * @data: a pointer to a struct strip_mine
  562. *
  563. * Returns: errno
  564. */
  565. static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
  566. struct buffer_head *bh, u64 *top, u64 *bottom,
  567. unsigned int height, void *data)
  568. {
  569. struct strip_mine *sm = data;
  570. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  571. struct gfs2_rgrp_list rlist;
  572. u64 bn, bstart;
  573. u32 blen;
  574. u64 *p;
  575. unsigned int rg_blocks = 0;
  576. int metadata;
  577. unsigned int revokes = 0;
  578. int x;
  579. int error;
  580. if (!*top)
  581. sm->sm_first = 0;
  582. if (height != sm->sm_height)
  583. return 0;
  584. if (sm->sm_first) {
  585. top++;
  586. sm->sm_first = 0;
  587. }
  588. metadata = (height != ip->i_di.di_height - 1);
  589. if (metadata)
  590. revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
  591. error = gfs2_rindex_hold(sdp, &ip->i_alloc.al_ri_gh);
  592. if (error)
  593. return error;
  594. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  595. bstart = 0;
  596. blen = 0;
  597. for (p = top; p < bottom; p++) {
  598. if (!*p)
  599. continue;
  600. bn = be64_to_cpu(*p);
  601. if (bstart + blen == bn)
  602. blen++;
  603. else {
  604. if (bstart)
  605. gfs2_rlist_add(sdp, &rlist, bstart);
  606. bstart = bn;
  607. blen = 1;
  608. }
  609. }
  610. if (bstart)
  611. gfs2_rlist_add(sdp, &rlist, bstart);
  612. else
  613. goto out; /* Nothing to do */
  614. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE, 0);
  615. for (x = 0; x < rlist.rl_rgrps; x++) {
  616. struct gfs2_rgrpd *rgd;
  617. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  618. rg_blocks += rgd->rd_ri.ri_length;
  619. }
  620. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  621. if (error)
  622. goto out_rlist;
  623. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  624. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  625. revokes);
  626. if (error)
  627. goto out_rg_gunlock;
  628. down_write(&ip->i_rw_mutex);
  629. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  630. gfs2_trans_add_bh(ip->i_gl, bh, 1);
  631. bstart = 0;
  632. blen = 0;
  633. for (p = top; p < bottom; p++) {
  634. if (!*p)
  635. continue;
  636. bn = be64_to_cpu(*p);
  637. if (bstart + blen == bn)
  638. blen++;
  639. else {
  640. if (bstart) {
  641. if (metadata)
  642. gfs2_free_meta(ip, bstart, blen);
  643. else
  644. gfs2_free_data(ip, bstart, blen);
  645. }
  646. bstart = bn;
  647. blen = 1;
  648. }
  649. *p = 0;
  650. if (!ip->i_di.di_blocks)
  651. gfs2_consist_inode(ip);
  652. ip->i_di.di_blocks--;
  653. }
  654. if (bstart) {
  655. if (metadata)
  656. gfs2_free_meta(ip, bstart, blen);
  657. else
  658. gfs2_free_data(ip, bstart, blen);
  659. }
  660. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  661. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  662. up_write(&ip->i_rw_mutex);
  663. gfs2_trans_end(sdp);
  664. out_rg_gunlock:
  665. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  666. out_rlist:
  667. gfs2_rlist_free(&rlist);
  668. out:
  669. gfs2_glock_dq_uninit(&ip->i_alloc.al_ri_gh);
  670. return error;
  671. }
  672. /**
  673. * do_grow - Make a file look bigger than it is
  674. * @ip: the inode
  675. * @size: the size to set the file to
  676. *
  677. * Called with an exclusive lock on @ip.
  678. *
  679. * Returns: errno
  680. */
  681. static int do_grow(struct gfs2_inode *ip, u64 size)
  682. {
  683. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  684. struct gfs2_alloc *al;
  685. struct buffer_head *dibh;
  686. unsigned int h;
  687. int error;
  688. al = gfs2_alloc_get(ip);
  689. error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  690. if (error)
  691. goto out;
  692. error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
  693. if (error)
  694. goto out_gunlock_q;
  695. al->al_requested = sdp->sd_max_height + RES_DATA;
  696. error = gfs2_inplace_reserve(ip);
  697. if (error)
  698. goto out_gunlock_q;
  699. error = gfs2_trans_begin(sdp,
  700. sdp->sd_max_height + al->al_rgd->rd_ri.ri_length +
  701. RES_JDATA + RES_DINODE + RES_STATFS + RES_QUOTA, 0);
  702. if (error)
  703. goto out_ipres;
  704. if (size > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
  705. if (gfs2_is_stuffed(ip)) {
  706. error = gfs2_unstuff_dinode(ip, NULL);
  707. if (error)
  708. goto out_end_trans;
  709. }
  710. h = calc_tree_height(ip, size);
  711. if (ip->i_di.di_height < h) {
  712. down_write(&ip->i_rw_mutex);
  713. error = build_height(&ip->i_inode, h);
  714. up_write(&ip->i_rw_mutex);
  715. if (error)
  716. goto out_end_trans;
  717. }
  718. }
  719. ip->i_di.di_size = size;
  720. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  721. error = gfs2_meta_inode_buffer(ip, &dibh);
  722. if (error)
  723. goto out_end_trans;
  724. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  725. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  726. brelse(dibh);
  727. out_end_trans:
  728. gfs2_trans_end(sdp);
  729. out_ipres:
  730. gfs2_inplace_release(ip);
  731. out_gunlock_q:
  732. gfs2_quota_unlock(ip);
  733. out:
  734. gfs2_alloc_put(ip);
  735. return error;
  736. }
  737. /**
  738. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  739. *
  740. * This is partly borrowed from ext3.
  741. */
  742. static int gfs2_block_truncate_page(struct address_space *mapping)
  743. {
  744. struct inode *inode = mapping->host;
  745. struct gfs2_inode *ip = GFS2_I(inode);
  746. struct gfs2_sbd *sdp = GFS2_SB(inode);
  747. loff_t from = inode->i_size;
  748. unsigned long index = from >> PAGE_CACHE_SHIFT;
  749. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  750. unsigned blocksize, iblock, length, pos;
  751. struct buffer_head *bh;
  752. struct page *page;
  753. void *kaddr;
  754. int err;
  755. page = grab_cache_page(mapping, index);
  756. if (!page)
  757. return 0;
  758. blocksize = inode->i_sb->s_blocksize;
  759. length = blocksize - (offset & (blocksize - 1));
  760. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  761. if (!page_has_buffers(page))
  762. create_empty_buffers(page, blocksize, 0);
  763. /* Find the buffer that contains "offset" */
  764. bh = page_buffers(page);
  765. pos = blocksize;
  766. while (offset >= pos) {
  767. bh = bh->b_this_page;
  768. iblock++;
  769. pos += blocksize;
  770. }
  771. err = 0;
  772. if (!buffer_mapped(bh)) {
  773. gfs2_get_block(inode, iblock, bh, 0);
  774. /* unmapped? It's a hole - nothing to do */
  775. if (!buffer_mapped(bh))
  776. goto unlock;
  777. }
  778. /* Ok, it's mapped. Make sure it's up-to-date */
  779. if (PageUptodate(page))
  780. set_buffer_uptodate(bh);
  781. if (!buffer_uptodate(bh)) {
  782. err = -EIO;
  783. ll_rw_block(READ, 1, &bh);
  784. wait_on_buffer(bh);
  785. /* Uhhuh. Read error. Complain and punt. */
  786. if (!buffer_uptodate(bh))
  787. goto unlock;
  788. }
  789. if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip))
  790. gfs2_trans_add_bh(ip->i_gl, bh, 0);
  791. kaddr = kmap_atomic(page, KM_USER0);
  792. memset(kaddr + offset, 0, length);
  793. flush_dcache_page(page);
  794. kunmap_atomic(kaddr, KM_USER0);
  795. unlock:
  796. unlock_page(page);
  797. page_cache_release(page);
  798. return err;
  799. }
  800. static int trunc_start(struct gfs2_inode *ip, u64 size)
  801. {
  802. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  803. struct buffer_head *dibh;
  804. int journaled = gfs2_is_jdata(ip);
  805. int error;
  806. error = gfs2_trans_begin(sdp,
  807. RES_DINODE + (journaled ? RES_JDATA : 0), 0);
  808. if (error)
  809. return error;
  810. error = gfs2_meta_inode_buffer(ip, &dibh);
  811. if (error)
  812. goto out;
  813. if (gfs2_is_stuffed(ip)) {
  814. ip->i_di.di_size = size;
  815. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  816. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  817. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  818. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + size);
  819. error = 1;
  820. } else {
  821. if (size & (u64)(sdp->sd_sb.sb_bsize - 1))
  822. error = gfs2_block_truncate_page(ip->i_inode.i_mapping);
  823. if (!error) {
  824. ip->i_di.di_size = size;
  825. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  826. ip->i_di.di_flags |= GFS2_DIF_TRUNC_IN_PROG;
  827. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  828. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  829. }
  830. }
  831. brelse(dibh);
  832. out:
  833. gfs2_trans_end(sdp);
  834. return error;
  835. }
  836. static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
  837. {
  838. unsigned int height = ip->i_di.di_height;
  839. u64 lblock;
  840. struct metapath mp;
  841. int error;
  842. if (!size)
  843. lblock = 0;
  844. else
  845. lblock = (size - 1) >> GFS2_SB(&ip->i_inode)->sd_sb.sb_bsize_shift;
  846. find_metapath(ip, lblock, &mp);
  847. gfs2_alloc_get(ip);
  848. error = gfs2_quota_hold(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
  849. if (error)
  850. goto out;
  851. while (height--) {
  852. struct strip_mine sm;
  853. sm.sm_first = !!size;
  854. sm.sm_height = height;
  855. error = recursive_scan(ip, NULL, &mp, 0, 0, 1, do_strip, &sm);
  856. if (error)
  857. break;
  858. }
  859. gfs2_quota_unhold(ip);
  860. out:
  861. gfs2_alloc_put(ip);
  862. return error;
  863. }
  864. static int trunc_end(struct gfs2_inode *ip)
  865. {
  866. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  867. struct buffer_head *dibh;
  868. int error;
  869. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  870. if (error)
  871. return error;
  872. down_write(&ip->i_rw_mutex);
  873. error = gfs2_meta_inode_buffer(ip, &dibh);
  874. if (error)
  875. goto out;
  876. if (!ip->i_di.di_size) {
  877. ip->i_di.di_height = 0;
  878. ip->i_di.di_goal_meta =
  879. ip->i_di.di_goal_data =
  880. ip->i_num.no_addr;
  881. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  882. }
  883. ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
  884. ip->i_di.di_flags &= ~GFS2_DIF_TRUNC_IN_PROG;
  885. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  886. gfs2_dinode_out(&ip->i_di, dibh->b_data);
  887. brelse(dibh);
  888. out:
  889. up_write(&ip->i_rw_mutex);
  890. gfs2_trans_end(sdp);
  891. return error;
  892. }
  893. /**
  894. * do_shrink - make a file smaller
  895. * @ip: the inode
  896. * @size: the size to make the file
  897. * @truncator: function to truncate the last partial block
  898. *
  899. * Called with an exclusive lock on @ip.
  900. *
  901. * Returns: errno
  902. */
  903. static int do_shrink(struct gfs2_inode *ip, u64 size)
  904. {
  905. int error;
  906. error = trunc_start(ip, size);
  907. if (error < 0)
  908. return error;
  909. if (error > 0)
  910. return 0;
  911. error = trunc_dealloc(ip, size);
  912. if (!error)
  913. error = trunc_end(ip);
  914. return error;
  915. }
  916. /**
  917. * gfs2_truncatei - make a file a given size
  918. * @ip: the inode
  919. * @size: the size to make the file
  920. * @truncator: function to truncate the last partial block
  921. *
  922. * The file size can grow, shrink, or stay the same size.
  923. *
  924. * Returns: errno
  925. */
  926. int gfs2_truncatei(struct gfs2_inode *ip, u64 size)
  927. {
  928. int error;
  929. if (gfs2_assert_warn(GFS2_SB(&ip->i_inode), S_ISREG(ip->i_di.di_mode)))
  930. return -EINVAL;
  931. if (size > ip->i_di.di_size)
  932. error = do_grow(ip, size);
  933. else
  934. error = do_shrink(ip, size);
  935. return error;
  936. }
  937. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  938. {
  939. int error;
  940. error = trunc_dealloc(ip, ip->i_di.di_size);
  941. if (!error)
  942. error = trunc_end(ip);
  943. return error;
  944. }
  945. int gfs2_file_dealloc(struct gfs2_inode *ip)
  946. {
  947. return trunc_dealloc(ip, 0);
  948. }
  949. /**
  950. * gfs2_write_calc_reserv - calculate number of blocks needed to write to a file
  951. * @ip: the file
  952. * @len: the number of bytes to be written to the file
  953. * @data_blocks: returns the number of data blocks required
  954. * @ind_blocks: returns the number of indirect blocks required
  955. *
  956. */
  957. void gfs2_write_calc_reserv(struct gfs2_inode *ip, unsigned int len,
  958. unsigned int *data_blocks, unsigned int *ind_blocks)
  959. {
  960. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  961. unsigned int tmp;
  962. if (gfs2_is_dir(ip)) {
  963. *data_blocks = DIV_ROUND_UP(len, sdp->sd_jbsize) + 2;
  964. *ind_blocks = 3 * (sdp->sd_max_jheight - 1);
  965. } else {
  966. *data_blocks = (len >> sdp->sd_sb.sb_bsize_shift) + 3;
  967. *ind_blocks = 3 * (sdp->sd_max_height - 1);
  968. }
  969. for (tmp = *data_blocks; tmp > sdp->sd_diptrs;) {
  970. tmp = DIV_ROUND_UP(tmp, sdp->sd_inptrs);
  971. *ind_blocks += tmp;
  972. }
  973. }
  974. /**
  975. * gfs2_write_alloc_required - figure out if a write will require an allocation
  976. * @ip: the file being written to
  977. * @offset: the offset to write to
  978. * @len: the number of bytes being written
  979. * @alloc_required: set to 1 if an alloc is required, 0 otherwise
  980. *
  981. * Returns: errno
  982. */
  983. int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
  984. unsigned int len, int *alloc_required)
  985. {
  986. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  987. u64 lblock, lblock_stop, dblock;
  988. u32 extlen;
  989. int new = 0;
  990. int error = 0;
  991. *alloc_required = 0;
  992. if (!len)
  993. return 0;
  994. if (gfs2_is_stuffed(ip)) {
  995. if (offset + len >
  996. sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  997. *alloc_required = 1;
  998. return 0;
  999. }
  1000. if (gfs2_is_dir(ip)) {
  1001. unsigned int bsize = sdp->sd_jbsize;
  1002. lblock = offset;
  1003. do_div(lblock, bsize);
  1004. lblock_stop = offset + len + bsize - 1;
  1005. do_div(lblock_stop, bsize);
  1006. } else {
  1007. unsigned int shift = sdp->sd_sb.sb_bsize_shift;
  1008. lblock = offset >> shift;
  1009. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  1010. }
  1011. for (; lblock < lblock_stop; lblock += extlen) {
  1012. error = gfs2_extent_map(&ip->i_inode, lblock, &new, &dblock, &extlen);
  1013. if (error)
  1014. return error;
  1015. if (!dblock) {
  1016. *alloc_required = 1;
  1017. return 0;
  1018. }
  1019. }
  1020. return 0;
  1021. }