bmap.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  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 v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/gfs2_ondisk.h>
  15. #include <linux/crc32.h>
  16. #include "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 && !IS_ERR(bh) && *dblock && !*new) {
  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 = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
  529. mp->mp_list[0];
  530. bottom = (uint64_t *)(bh->b_data + sizeof(struct gfs2_dinode)) +
  531. sdp->sd_diptrs;
  532. } else {
  533. error = gfs2_meta_indirect_buffer(ip, height, block, 0, &bh);
  534. if (error)
  535. return error;
  536. top = (uint64_t *)(bh->b_data + mh_size) +
  537. ((first) ? mp->mp_list[height] : 0);
  538. bottom = (uint64_t *)(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, uint64_t *top, uint64_t *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. uint64_t bn, bstart;
  577. uint32_t blen;
  578. uint64_t *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, uint64_t 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, uint64_t 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 & (uint64_t)(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, uint64_t size)
  841. {
  842. unsigned int height = ip->i_di.di_height;
  843. uint64_t 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, uint64_t 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, uint64_t 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, uint64_t offset,
  988. unsigned int len, int *alloc_required)
  989. {
  990. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  991. uint64_t lblock, lblock_stop, dblock;
  992. uint32_t 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. }