bmap.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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/spinlock.h>
  10. #include <linux/completion.h>
  11. #include <linux/buffer_head.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/gfs2_ondisk.h>
  14. #include <linux/crc32.h>
  15. #include "gfs2.h"
  16. #include "incore.h"
  17. #include "bmap.h"
  18. #include "glock.h"
  19. #include "inode.h"
  20. #include "meta_io.h"
  21. #include "quota.h"
  22. #include "rgrp.h"
  23. #include "log.h"
  24. #include "super.h"
  25. #include "trans.h"
  26. #include "dir.h"
  27. #include "util.h"
  28. #include "trace_gfs2.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. struct buffer_head *mp_bh[GFS2_MAX_META_HEIGHT];
  35. __u16 mp_list[GFS2_MAX_META_HEIGHT];
  36. };
  37. struct strip_mine {
  38. int sm_first;
  39. unsigned int sm_height;
  40. };
  41. /**
  42. * gfs2_unstuffer_page - unstuff a stuffed inode into a block cached by a page
  43. * @ip: the inode
  44. * @dibh: the dinode buffer
  45. * @block: the block number that was allocated
  46. * @page: The (optional) page. This is looked up if @page is NULL
  47. *
  48. * Returns: errno
  49. */
  50. static int gfs2_unstuffer_page(struct gfs2_inode *ip, struct buffer_head *dibh,
  51. u64 block, struct page *page)
  52. {
  53. struct inode *inode = &ip->i_inode;
  54. struct buffer_head *bh;
  55. int release = 0;
  56. if (!page || page->index) {
  57. page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
  58. if (!page)
  59. return -ENOMEM;
  60. release = 1;
  61. }
  62. if (!PageUptodate(page)) {
  63. void *kaddr = kmap(page);
  64. u64 dsize = i_size_read(inode);
  65. if (dsize > (dibh->b_size - sizeof(struct gfs2_dinode)))
  66. dsize = dibh->b_size - sizeof(struct gfs2_dinode);
  67. memcpy(kaddr, dibh->b_data + sizeof(struct gfs2_dinode), dsize);
  68. memset(kaddr + dsize, 0, PAGE_CACHE_SIZE - dsize);
  69. kunmap(page);
  70. SetPageUptodate(page);
  71. }
  72. if (!page_has_buffers(page))
  73. create_empty_buffers(page, 1 << inode->i_blkbits,
  74. (1 << BH_Uptodate));
  75. bh = page_buffers(page);
  76. if (!buffer_mapped(bh))
  77. map_bh(bh, inode->i_sb, block);
  78. set_buffer_uptodate(bh);
  79. if (!gfs2_is_jdata(ip))
  80. mark_buffer_dirty(bh);
  81. if (!gfs2_is_writeback(ip))
  82. gfs2_trans_add_data(ip->i_gl, bh);
  83. if (release) {
  84. unlock_page(page);
  85. page_cache_release(page);
  86. }
  87. return 0;
  88. }
  89. /**
  90. * gfs2_unstuff_dinode - Unstuff a dinode when the data has grown too big
  91. * @ip: The GFS2 inode to unstuff
  92. * @page: The (optional) page. This is looked up if the @page is NULL
  93. *
  94. * This routine unstuffs a dinode and returns it to a "normal" state such
  95. * that the height can be grown in the traditional way.
  96. *
  97. * Returns: errno
  98. */
  99. int gfs2_unstuff_dinode(struct gfs2_inode *ip, struct page *page)
  100. {
  101. struct buffer_head *bh, *dibh;
  102. struct gfs2_dinode *di;
  103. u64 block = 0;
  104. int isdir = gfs2_is_dir(ip);
  105. int error;
  106. down_write(&ip->i_rw_mutex);
  107. error = gfs2_meta_inode_buffer(ip, &dibh);
  108. if (error)
  109. goto out;
  110. if (i_size_read(&ip->i_inode)) {
  111. /* Get a free block, fill it with the stuffed data,
  112. and write it out to disk */
  113. unsigned int n = 1;
  114. error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
  115. if (error)
  116. goto out_brelse;
  117. if (isdir) {
  118. gfs2_trans_add_unrevoke(GFS2_SB(&ip->i_inode), block, 1);
  119. error = gfs2_dir_get_new_buffer(ip, block, &bh);
  120. if (error)
  121. goto out_brelse;
  122. gfs2_buffer_copy_tail(bh, sizeof(struct gfs2_meta_header),
  123. dibh, sizeof(struct gfs2_dinode));
  124. brelse(bh);
  125. } else {
  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_meta(ip->i_gl, dibh);
  133. di = (struct gfs2_dinode *)dibh->b_data;
  134. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  135. if (i_size_read(&ip->i_inode)) {
  136. *(__be64 *)(di + 1) = cpu_to_be64(block);
  137. gfs2_add_inode_blocks(&ip->i_inode, 1);
  138. di->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
  139. }
  140. ip->i_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. * find_metapath - Find path through the metadata tree
  150. * @sdp: The superblock
  151. * @mp: The metapath to return the result in
  152. * @block: The disk block to look up
  153. * @height: The pre-calculated height of the metadata tree
  154. *
  155. * This routine returns a struct metapath structure that defines a path
  156. * through the metadata of inode "ip" to get to block "block".
  157. *
  158. * Example:
  159. * Given: "ip" is a height 3 file, "offset" is 101342453, and this is a
  160. * filesystem with a blocksize of 4096.
  161. *
  162. * find_metapath() would return a struct metapath structure set to:
  163. * mp_offset = 101342453, mp_height = 3, mp_list[0] = 0, mp_list[1] = 48,
  164. * and mp_list[2] = 165.
  165. *
  166. * That means that in order to get to the block containing the byte at
  167. * offset 101342453, we would load the indirect block pointed to by pointer
  168. * 0 in the dinode. We would then load the indirect block pointed to by
  169. * pointer 48 in that indirect block. We would then load the data block
  170. * pointed to by pointer 165 in that indirect block.
  171. *
  172. * ----------------------------------------
  173. * | Dinode | |
  174. * | | 4|
  175. * | |0 1 2 3 4 5 9|
  176. * | | 6|
  177. * ----------------------------------------
  178. * |
  179. * |
  180. * V
  181. * ----------------------------------------
  182. * | Indirect Block |
  183. * | 5|
  184. * | 4 4 4 4 4 5 5 1|
  185. * |0 5 6 7 8 9 0 1 2|
  186. * ----------------------------------------
  187. * |
  188. * |
  189. * V
  190. * ----------------------------------------
  191. * | Indirect Block |
  192. * | 1 1 1 1 1 5|
  193. * | 6 6 6 6 6 1|
  194. * |0 3 4 5 6 7 2|
  195. * ----------------------------------------
  196. * |
  197. * |
  198. * V
  199. * ----------------------------------------
  200. * | Data block containing offset |
  201. * | 101342453 |
  202. * | |
  203. * | |
  204. * ----------------------------------------
  205. *
  206. */
  207. static void find_metapath(const struct gfs2_sbd *sdp, u64 block,
  208. struct metapath *mp, unsigned int height)
  209. {
  210. unsigned int i;
  211. for (i = height; i--;)
  212. mp->mp_list[i] = do_div(block, sdp->sd_inptrs);
  213. }
  214. static inline unsigned int metapath_branch_start(const struct metapath *mp)
  215. {
  216. if (mp->mp_list[0] == 0)
  217. return 2;
  218. return 1;
  219. }
  220. /**
  221. * metapointer - Return pointer to start of metadata in a buffer
  222. * @height: The metadata height (0 = dinode)
  223. * @mp: The metapath
  224. *
  225. * Return a pointer to the block number of the next height of the metadata
  226. * tree given a buffer containing the pointer to the current height of the
  227. * metadata tree.
  228. */
  229. static inline __be64 *metapointer(unsigned int height, const struct metapath *mp)
  230. {
  231. struct buffer_head *bh = mp->mp_bh[height];
  232. unsigned int head_size = (height > 0) ?
  233. sizeof(struct gfs2_meta_header) : sizeof(struct gfs2_dinode);
  234. return ((__be64 *)(bh->b_data + head_size)) + mp->mp_list[height];
  235. }
  236. static void gfs2_metapath_ra(struct gfs2_glock *gl,
  237. const struct buffer_head *bh, const __be64 *pos)
  238. {
  239. struct buffer_head *rabh;
  240. const __be64 *endp = (const __be64 *)(bh->b_data + bh->b_size);
  241. const __be64 *t;
  242. for (t = pos; t < endp; t++) {
  243. if (!*t)
  244. continue;
  245. rabh = gfs2_getbuf(gl, be64_to_cpu(*t), CREATE);
  246. if (trylock_buffer(rabh)) {
  247. if (!buffer_uptodate(rabh)) {
  248. rabh->b_end_io = end_buffer_read_sync;
  249. submit_bh(READA | REQ_META, rabh);
  250. continue;
  251. }
  252. unlock_buffer(rabh);
  253. }
  254. brelse(rabh);
  255. }
  256. }
  257. /**
  258. * lookup_metapath - Walk the metadata tree to a specific point
  259. * @ip: The inode
  260. * @mp: The metapath
  261. *
  262. * Assumes that the inode's buffer has already been looked up and
  263. * hooked onto mp->mp_bh[0] and that the metapath has been initialised
  264. * by find_metapath().
  265. *
  266. * If this function encounters part of the tree which has not been
  267. * allocated, it returns the current height of the tree at the point
  268. * at which it found the unallocated block. Blocks which are found are
  269. * added to the mp->mp_bh[] list.
  270. *
  271. * Returns: error or height of metadata tree
  272. */
  273. static int lookup_metapath(struct gfs2_inode *ip, struct metapath *mp)
  274. {
  275. unsigned int end_of_metadata = ip->i_height - 1;
  276. unsigned int x;
  277. __be64 *ptr;
  278. u64 dblock;
  279. int ret;
  280. for (x = 0; x < end_of_metadata; x++) {
  281. ptr = metapointer(x, mp);
  282. dblock = be64_to_cpu(*ptr);
  283. if (!dblock)
  284. return x + 1;
  285. ret = gfs2_meta_indirect_buffer(ip, x+1, dblock, &mp->mp_bh[x+1]);
  286. if (ret)
  287. return ret;
  288. }
  289. return ip->i_height;
  290. }
  291. static inline void release_metapath(struct metapath *mp)
  292. {
  293. int i;
  294. for (i = 0; i < GFS2_MAX_META_HEIGHT; i++) {
  295. if (mp->mp_bh[i] == NULL)
  296. break;
  297. brelse(mp->mp_bh[i]);
  298. }
  299. }
  300. /**
  301. * gfs2_extent_length - Returns length of an extent of blocks
  302. * @start: Start of the buffer
  303. * @len: Length of the buffer in bytes
  304. * @ptr: Current position in the buffer
  305. * @limit: Max extent length to return (0 = unlimited)
  306. * @eob: Set to 1 if we hit "end of block"
  307. *
  308. * If the first block is zero (unallocated) it will return the number of
  309. * unallocated blocks in the extent, otherwise it will return the number
  310. * of contiguous blocks in the extent.
  311. *
  312. * Returns: The length of the extent (minimum of one block)
  313. */
  314. static inline unsigned int gfs2_extent_length(void *start, unsigned int len, __be64 *ptr, unsigned limit, int *eob)
  315. {
  316. const __be64 *end = (start + len);
  317. const __be64 *first = ptr;
  318. u64 d = be64_to_cpu(*ptr);
  319. *eob = 0;
  320. do {
  321. ptr++;
  322. if (ptr >= end)
  323. break;
  324. if (limit && --limit == 0)
  325. break;
  326. if (d)
  327. d++;
  328. } while(be64_to_cpu(*ptr) == d);
  329. if (ptr >= end)
  330. *eob = 1;
  331. return (ptr - first);
  332. }
  333. static inline void bmap_lock(struct gfs2_inode *ip, int create)
  334. {
  335. if (create)
  336. down_write(&ip->i_rw_mutex);
  337. else
  338. down_read(&ip->i_rw_mutex);
  339. }
  340. static inline void bmap_unlock(struct gfs2_inode *ip, int create)
  341. {
  342. if (create)
  343. up_write(&ip->i_rw_mutex);
  344. else
  345. up_read(&ip->i_rw_mutex);
  346. }
  347. static inline __be64 *gfs2_indirect_init(struct metapath *mp,
  348. struct gfs2_glock *gl, unsigned int i,
  349. unsigned offset, u64 bn)
  350. {
  351. __be64 *ptr = (__be64 *)(mp->mp_bh[i - 1]->b_data +
  352. ((i > 1) ? sizeof(struct gfs2_meta_header) :
  353. sizeof(struct gfs2_dinode)));
  354. BUG_ON(i < 1);
  355. BUG_ON(mp->mp_bh[i] != NULL);
  356. mp->mp_bh[i] = gfs2_meta_new(gl, bn);
  357. gfs2_trans_add_meta(gl, mp->mp_bh[i]);
  358. gfs2_metatype_set(mp->mp_bh[i], GFS2_METATYPE_IN, GFS2_FORMAT_IN);
  359. gfs2_buffer_clear_tail(mp->mp_bh[i], sizeof(struct gfs2_meta_header));
  360. ptr += offset;
  361. *ptr = cpu_to_be64(bn);
  362. return ptr;
  363. }
  364. enum alloc_state {
  365. ALLOC_DATA = 0,
  366. ALLOC_GROW_DEPTH = 1,
  367. ALLOC_GROW_HEIGHT = 2,
  368. /* ALLOC_UNSTUFF = 3, TBD and rather complicated */
  369. };
  370. /**
  371. * gfs2_bmap_alloc - Build a metadata tree of the requested height
  372. * @inode: The GFS2 inode
  373. * @lblock: The logical starting block of the extent
  374. * @bh_map: This is used to return the mapping details
  375. * @mp: The metapath
  376. * @sheight: The starting height (i.e. whats already mapped)
  377. * @height: The height to build to
  378. * @maxlen: The max number of data blocks to alloc
  379. *
  380. * In this routine we may have to alloc:
  381. * i) Indirect blocks to grow the metadata tree height
  382. * ii) Indirect blocks to fill in lower part of the metadata tree
  383. * iii) Data blocks
  384. *
  385. * The function is in two parts. The first part works out the total
  386. * number of blocks which we need. The second part does the actual
  387. * allocation asking for an extent at a time (if enough contiguous free
  388. * blocks are available, there will only be one request per bmap call)
  389. * and uses the state machine to initialise the blocks in order.
  390. *
  391. * Returns: errno on error
  392. */
  393. static int gfs2_bmap_alloc(struct inode *inode, const sector_t lblock,
  394. struct buffer_head *bh_map, struct metapath *mp,
  395. const unsigned int sheight,
  396. const unsigned int height,
  397. const unsigned int maxlen)
  398. {
  399. struct gfs2_inode *ip = GFS2_I(inode);
  400. struct gfs2_sbd *sdp = GFS2_SB(inode);
  401. struct super_block *sb = sdp->sd_vfs;
  402. struct buffer_head *dibh = mp->mp_bh[0];
  403. u64 bn, dblock = 0;
  404. unsigned n, i, blks, alloced = 0, iblks = 0, branch_start = 0;
  405. unsigned dblks = 0;
  406. unsigned ptrs_per_blk;
  407. const unsigned end_of_metadata = height - 1;
  408. int ret;
  409. int eob = 0;
  410. enum alloc_state state;
  411. __be64 *ptr;
  412. __be64 zero_bn = 0;
  413. BUG_ON(sheight < 1);
  414. BUG_ON(dibh == NULL);
  415. gfs2_trans_add_meta(ip->i_gl, dibh);
  416. if (height == sheight) {
  417. struct buffer_head *bh;
  418. /* Bottom indirect block exists, find unalloced extent size */
  419. ptr = metapointer(end_of_metadata, mp);
  420. bh = mp->mp_bh[end_of_metadata];
  421. dblks = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen,
  422. &eob);
  423. BUG_ON(dblks < 1);
  424. state = ALLOC_DATA;
  425. } else {
  426. /* Need to allocate indirect blocks */
  427. ptrs_per_blk = height > 1 ? sdp->sd_inptrs : sdp->sd_diptrs;
  428. dblks = min(maxlen, ptrs_per_blk - mp->mp_list[end_of_metadata]);
  429. if (height == ip->i_height) {
  430. /* Writing into existing tree, extend tree down */
  431. iblks = height - sheight;
  432. state = ALLOC_GROW_DEPTH;
  433. } else {
  434. /* Building up tree height */
  435. state = ALLOC_GROW_HEIGHT;
  436. iblks = height - ip->i_height;
  437. branch_start = metapath_branch_start(mp);
  438. iblks += (height - branch_start);
  439. }
  440. }
  441. /* start of the second part of the function (state machine) */
  442. blks = dblks + iblks;
  443. i = sheight;
  444. do {
  445. int error;
  446. n = blks - alloced;
  447. error = gfs2_alloc_blocks(ip, &bn, &n, 0, NULL);
  448. if (error)
  449. return error;
  450. alloced += n;
  451. if (state != ALLOC_DATA || gfs2_is_jdata(ip))
  452. gfs2_trans_add_unrevoke(sdp, bn, n);
  453. switch (state) {
  454. /* Growing height of tree */
  455. case ALLOC_GROW_HEIGHT:
  456. if (i == 1) {
  457. ptr = (__be64 *)(dibh->b_data +
  458. sizeof(struct gfs2_dinode));
  459. zero_bn = *ptr;
  460. }
  461. for (; i - 1 < height - ip->i_height && n > 0; i++, n--)
  462. gfs2_indirect_init(mp, ip->i_gl, i, 0, bn++);
  463. if (i - 1 == height - ip->i_height) {
  464. i--;
  465. gfs2_buffer_copy_tail(mp->mp_bh[i],
  466. sizeof(struct gfs2_meta_header),
  467. dibh, sizeof(struct gfs2_dinode));
  468. gfs2_buffer_clear_tail(dibh,
  469. sizeof(struct gfs2_dinode) +
  470. sizeof(__be64));
  471. ptr = (__be64 *)(mp->mp_bh[i]->b_data +
  472. sizeof(struct gfs2_meta_header));
  473. *ptr = zero_bn;
  474. state = ALLOC_GROW_DEPTH;
  475. for(i = branch_start; i < height; i++) {
  476. if (mp->mp_bh[i] == NULL)
  477. break;
  478. brelse(mp->mp_bh[i]);
  479. mp->mp_bh[i] = NULL;
  480. }
  481. i = branch_start;
  482. }
  483. if (n == 0)
  484. break;
  485. /* Branching from existing tree */
  486. case ALLOC_GROW_DEPTH:
  487. if (i > 1 && i < height)
  488. gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[i-1]);
  489. for (; i < height && n > 0; i++, n--)
  490. gfs2_indirect_init(mp, ip->i_gl, i,
  491. mp->mp_list[i-1], bn++);
  492. if (i == height)
  493. state = ALLOC_DATA;
  494. if (n == 0)
  495. break;
  496. /* Tree complete, adding data blocks */
  497. case ALLOC_DATA:
  498. BUG_ON(n > dblks);
  499. BUG_ON(mp->mp_bh[end_of_metadata] == NULL);
  500. gfs2_trans_add_meta(ip->i_gl, mp->mp_bh[end_of_metadata]);
  501. dblks = n;
  502. ptr = metapointer(end_of_metadata, mp);
  503. dblock = bn;
  504. while (n-- > 0)
  505. *ptr++ = cpu_to_be64(bn++);
  506. if (buffer_zeronew(bh_map)) {
  507. ret = sb_issue_zeroout(sb, dblock, dblks,
  508. GFP_NOFS);
  509. if (ret) {
  510. fs_err(sdp,
  511. "Failed to zero data buffers\n");
  512. clear_buffer_zeronew(bh_map);
  513. }
  514. }
  515. break;
  516. }
  517. } while ((state != ALLOC_DATA) || !dblock);
  518. ip->i_height = height;
  519. gfs2_add_inode_blocks(&ip->i_inode, alloced);
  520. gfs2_dinode_out(ip, mp->mp_bh[0]->b_data);
  521. map_bh(bh_map, inode->i_sb, dblock);
  522. bh_map->b_size = dblks << inode->i_blkbits;
  523. set_buffer_new(bh_map);
  524. return 0;
  525. }
  526. /**
  527. * gfs2_block_map - Map a block from an inode to a disk block
  528. * @inode: The inode
  529. * @lblock: The logical block number
  530. * @bh_map: The bh to be mapped
  531. * @create: True if its ok to alloc blocks to satify the request
  532. *
  533. * Sets buffer_mapped() if successful, sets buffer_boundary() if a
  534. * read of metadata will be required before the next block can be
  535. * mapped. Sets buffer_new() if new blocks were allocated.
  536. *
  537. * Returns: errno
  538. */
  539. int gfs2_block_map(struct inode *inode, sector_t lblock,
  540. struct buffer_head *bh_map, int create)
  541. {
  542. struct gfs2_inode *ip = GFS2_I(inode);
  543. struct gfs2_sbd *sdp = GFS2_SB(inode);
  544. unsigned int bsize = sdp->sd_sb.sb_bsize;
  545. const unsigned int maxlen = bh_map->b_size >> inode->i_blkbits;
  546. const u64 *arr = sdp->sd_heightsize;
  547. __be64 *ptr;
  548. u64 size;
  549. struct metapath mp;
  550. int ret;
  551. int eob;
  552. unsigned int len;
  553. struct buffer_head *bh;
  554. u8 height;
  555. BUG_ON(maxlen == 0);
  556. memset(mp.mp_bh, 0, sizeof(mp.mp_bh));
  557. bmap_lock(ip, create);
  558. clear_buffer_mapped(bh_map);
  559. clear_buffer_new(bh_map);
  560. clear_buffer_boundary(bh_map);
  561. trace_gfs2_bmap(ip, bh_map, lblock, create, 1);
  562. if (gfs2_is_dir(ip)) {
  563. bsize = sdp->sd_jbsize;
  564. arr = sdp->sd_jheightsize;
  565. }
  566. ret = gfs2_meta_inode_buffer(ip, &mp.mp_bh[0]);
  567. if (ret)
  568. goto out;
  569. height = ip->i_height;
  570. size = (lblock + 1) * bsize;
  571. while (size > arr[height])
  572. height++;
  573. find_metapath(sdp, lblock, &mp, height);
  574. ret = 1;
  575. if (height > ip->i_height || gfs2_is_stuffed(ip))
  576. goto do_alloc;
  577. ret = lookup_metapath(ip, &mp);
  578. if (ret < 0)
  579. goto out;
  580. if (ret != ip->i_height)
  581. goto do_alloc;
  582. ptr = metapointer(ip->i_height - 1, &mp);
  583. if (*ptr == 0)
  584. goto do_alloc;
  585. map_bh(bh_map, inode->i_sb, be64_to_cpu(*ptr));
  586. bh = mp.mp_bh[ip->i_height - 1];
  587. len = gfs2_extent_length(bh->b_data, bh->b_size, ptr, maxlen, &eob);
  588. bh_map->b_size = (len << inode->i_blkbits);
  589. if (eob)
  590. set_buffer_boundary(bh_map);
  591. ret = 0;
  592. out:
  593. release_metapath(&mp);
  594. trace_gfs2_bmap(ip, bh_map, lblock, create, ret);
  595. bmap_unlock(ip, create);
  596. return ret;
  597. do_alloc:
  598. /* All allocations are done here, firstly check create flag */
  599. if (!create) {
  600. BUG_ON(gfs2_is_stuffed(ip));
  601. ret = 0;
  602. goto out;
  603. }
  604. /* At this point ret is the tree depth of already allocated blocks */
  605. ret = gfs2_bmap_alloc(inode, lblock, bh_map, &mp, ret, height, maxlen);
  606. goto out;
  607. }
  608. /*
  609. * Deprecated: do not use in new code
  610. */
  611. int gfs2_extent_map(struct inode *inode, u64 lblock, int *new, u64 *dblock, unsigned *extlen)
  612. {
  613. struct buffer_head bh = { .b_state = 0, .b_blocknr = 0 };
  614. int ret;
  615. int create = *new;
  616. BUG_ON(!extlen);
  617. BUG_ON(!dblock);
  618. BUG_ON(!new);
  619. bh.b_size = 1 << (inode->i_blkbits + (create ? 0 : 5));
  620. ret = gfs2_block_map(inode, lblock, &bh, create);
  621. *extlen = bh.b_size >> inode->i_blkbits;
  622. *dblock = bh.b_blocknr;
  623. if (buffer_new(&bh))
  624. *new = 1;
  625. else
  626. *new = 0;
  627. return ret;
  628. }
  629. /**
  630. * do_strip - Look for a layer a particular layer of the file and strip it off
  631. * @ip: the inode
  632. * @dibh: the dinode buffer
  633. * @bh: A buffer of pointers
  634. * @top: The first pointer in the buffer
  635. * @bottom: One more than the last pointer
  636. * @height: the height this buffer is at
  637. * @data: a pointer to a struct strip_mine
  638. *
  639. * Returns: errno
  640. */
  641. static int do_strip(struct gfs2_inode *ip, struct buffer_head *dibh,
  642. struct buffer_head *bh, __be64 *top, __be64 *bottom,
  643. unsigned int height, struct strip_mine *sm)
  644. {
  645. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  646. struct gfs2_rgrp_list rlist;
  647. u64 bn, bstart;
  648. u32 blen, btotal;
  649. __be64 *p;
  650. unsigned int rg_blocks = 0;
  651. int metadata;
  652. unsigned int revokes = 0;
  653. int x;
  654. int error;
  655. error = gfs2_rindex_update(sdp);
  656. if (error)
  657. return error;
  658. if (!*top)
  659. sm->sm_first = 0;
  660. if (height != sm->sm_height)
  661. return 0;
  662. if (sm->sm_first) {
  663. top++;
  664. sm->sm_first = 0;
  665. }
  666. metadata = (height != ip->i_height - 1);
  667. if (metadata)
  668. revokes = (height) ? sdp->sd_inptrs : sdp->sd_diptrs;
  669. else if (ip->i_depth)
  670. revokes = sdp->sd_inptrs;
  671. memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
  672. bstart = 0;
  673. blen = 0;
  674. for (p = top; p < bottom; p++) {
  675. if (!*p)
  676. continue;
  677. bn = be64_to_cpu(*p);
  678. if (bstart + blen == bn)
  679. blen++;
  680. else {
  681. if (bstart)
  682. gfs2_rlist_add(ip, &rlist, bstart);
  683. bstart = bn;
  684. blen = 1;
  685. }
  686. }
  687. if (bstart)
  688. gfs2_rlist_add(ip, &rlist, bstart);
  689. else
  690. goto out; /* Nothing to do */
  691. gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
  692. for (x = 0; x < rlist.rl_rgrps; x++) {
  693. struct gfs2_rgrpd *rgd;
  694. rgd = rlist.rl_ghs[x].gh_gl->gl_object;
  695. rg_blocks += rgd->rd_length;
  696. }
  697. error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
  698. if (error)
  699. goto out_rlist;
  700. if (gfs2_rs_active(ip->i_res)) /* needs to be done with the rgrp glock held */
  701. gfs2_rs_deltree(ip, ip->i_res);
  702. error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE +
  703. RES_INDIRECT + RES_STATFS + RES_QUOTA,
  704. revokes);
  705. if (error)
  706. goto out_rg_gunlock;
  707. down_write(&ip->i_rw_mutex);
  708. gfs2_trans_add_meta(ip->i_gl, dibh);
  709. gfs2_trans_add_meta(ip->i_gl, bh);
  710. bstart = 0;
  711. blen = 0;
  712. btotal = 0;
  713. for (p = top; p < bottom; p++) {
  714. if (!*p)
  715. continue;
  716. bn = be64_to_cpu(*p);
  717. if (bstart + blen == bn)
  718. blen++;
  719. else {
  720. if (bstart) {
  721. __gfs2_free_blocks(ip, bstart, blen, metadata);
  722. btotal += blen;
  723. }
  724. bstart = bn;
  725. blen = 1;
  726. }
  727. *p = 0;
  728. gfs2_add_inode_blocks(&ip->i_inode, -1);
  729. }
  730. if (bstart) {
  731. __gfs2_free_blocks(ip, bstart, blen, metadata);
  732. btotal += blen;
  733. }
  734. gfs2_statfs_change(sdp, 0, +btotal, 0);
  735. gfs2_quota_change(ip, -(s64)btotal, ip->i_inode.i_uid,
  736. ip->i_inode.i_gid);
  737. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  738. gfs2_dinode_out(ip, dibh->b_data);
  739. up_write(&ip->i_rw_mutex);
  740. gfs2_trans_end(sdp);
  741. out_rg_gunlock:
  742. gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
  743. out_rlist:
  744. gfs2_rlist_free(&rlist);
  745. out:
  746. return error;
  747. }
  748. /**
  749. * recursive_scan - recursively scan through the end of a file
  750. * @ip: the inode
  751. * @dibh: the dinode buffer
  752. * @mp: the path through the metadata to the point to start
  753. * @height: the height the recursion is at
  754. * @block: the indirect block to look at
  755. * @first: 1 if this is the first block
  756. * @sm: data opaque to this function to pass to @bc
  757. *
  758. * When this is first called @height and @block should be zero and
  759. * @first should be 1.
  760. *
  761. * Returns: errno
  762. */
  763. static int recursive_scan(struct gfs2_inode *ip, struct buffer_head *dibh,
  764. struct metapath *mp, unsigned int height,
  765. u64 block, int first, struct strip_mine *sm)
  766. {
  767. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  768. struct buffer_head *bh = NULL;
  769. __be64 *top, *bottom;
  770. u64 bn;
  771. int error;
  772. int mh_size = sizeof(struct gfs2_meta_header);
  773. if (!height) {
  774. error = gfs2_meta_inode_buffer(ip, &bh);
  775. if (error)
  776. return error;
  777. dibh = bh;
  778. top = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + mp->mp_list[0];
  779. bottom = (__be64 *)(bh->b_data + sizeof(struct gfs2_dinode)) + sdp->sd_diptrs;
  780. } else {
  781. error = gfs2_meta_indirect_buffer(ip, height, block, &bh);
  782. if (error)
  783. return error;
  784. top = (__be64 *)(bh->b_data + mh_size) +
  785. (first ? mp->mp_list[height] : 0);
  786. bottom = (__be64 *)(bh->b_data + mh_size) + sdp->sd_inptrs;
  787. }
  788. error = do_strip(ip, dibh, bh, top, bottom, height, sm);
  789. if (error)
  790. goto out;
  791. if (height < ip->i_height - 1) {
  792. gfs2_metapath_ra(ip->i_gl, bh, top);
  793. for (; top < bottom; top++, first = 0) {
  794. if (!*top)
  795. continue;
  796. bn = be64_to_cpu(*top);
  797. error = recursive_scan(ip, dibh, mp, height + 1, bn,
  798. first, sm);
  799. if (error)
  800. break;
  801. }
  802. }
  803. out:
  804. brelse(bh);
  805. return error;
  806. }
  807. /**
  808. * gfs2_block_truncate_page - Deal with zeroing out data for truncate
  809. *
  810. * This is partly borrowed from ext3.
  811. */
  812. static int gfs2_block_truncate_page(struct address_space *mapping, loff_t from)
  813. {
  814. struct inode *inode = mapping->host;
  815. struct gfs2_inode *ip = GFS2_I(inode);
  816. unsigned long index = from >> PAGE_CACHE_SHIFT;
  817. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  818. unsigned blocksize, iblock, length, pos;
  819. struct buffer_head *bh;
  820. struct page *page;
  821. int err;
  822. page = find_or_create_page(mapping, index, GFP_NOFS);
  823. if (!page)
  824. return 0;
  825. blocksize = inode->i_sb->s_blocksize;
  826. length = blocksize - (offset & (blocksize - 1));
  827. iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
  828. if (!page_has_buffers(page))
  829. create_empty_buffers(page, blocksize, 0);
  830. /* Find the buffer that contains "offset" */
  831. bh = page_buffers(page);
  832. pos = blocksize;
  833. while (offset >= pos) {
  834. bh = bh->b_this_page;
  835. iblock++;
  836. pos += blocksize;
  837. }
  838. err = 0;
  839. if (!buffer_mapped(bh)) {
  840. gfs2_block_map(inode, iblock, bh, 0);
  841. /* unmapped? It's a hole - nothing to do */
  842. if (!buffer_mapped(bh))
  843. goto unlock;
  844. }
  845. /* Ok, it's mapped. Make sure it's up-to-date */
  846. if (PageUptodate(page))
  847. set_buffer_uptodate(bh);
  848. if (!buffer_uptodate(bh)) {
  849. err = -EIO;
  850. ll_rw_block(READ, 1, &bh);
  851. wait_on_buffer(bh);
  852. /* Uhhuh. Read error. Complain and punt. */
  853. if (!buffer_uptodate(bh))
  854. goto unlock;
  855. err = 0;
  856. }
  857. if (!gfs2_is_writeback(ip))
  858. gfs2_trans_add_data(ip->i_gl, bh);
  859. zero_user(page, offset, length);
  860. mark_buffer_dirty(bh);
  861. unlock:
  862. unlock_page(page);
  863. page_cache_release(page);
  864. return err;
  865. }
  866. /**
  867. * gfs2_journaled_truncate - Wrapper for truncate_pagecache for jdata files
  868. * @inode: The inode being truncated
  869. * @oldsize: The original (larger) size
  870. * @newsize: The new smaller size
  871. *
  872. * With jdata files, we have to journal a revoke for each block which is
  873. * truncated. As a result, we need to split this into separate transactions
  874. * if the number of pages being truncated gets too large.
  875. */
  876. #define GFS2_JTRUNC_REVOKES 8192
  877. static int gfs2_journaled_truncate(struct inode *inode, u64 oldsize, u64 newsize)
  878. {
  879. struct gfs2_sbd *sdp = GFS2_SB(inode);
  880. u64 max_chunk = GFS2_JTRUNC_REVOKES * sdp->sd_vfs->s_blocksize;
  881. u64 chunk;
  882. int error;
  883. while (oldsize != newsize) {
  884. chunk = oldsize - newsize;
  885. if (chunk > max_chunk)
  886. chunk = max_chunk;
  887. truncate_pagecache(inode, oldsize, oldsize - chunk);
  888. oldsize -= chunk;
  889. gfs2_trans_end(sdp);
  890. error = gfs2_trans_begin(sdp, RES_DINODE, GFS2_JTRUNC_REVOKES);
  891. if (error)
  892. return error;
  893. }
  894. return 0;
  895. }
  896. static int trunc_start(struct inode *inode, u64 oldsize, u64 newsize)
  897. {
  898. struct gfs2_inode *ip = GFS2_I(inode);
  899. struct gfs2_sbd *sdp = GFS2_SB(inode);
  900. struct address_space *mapping = inode->i_mapping;
  901. struct buffer_head *dibh;
  902. int journaled = gfs2_is_jdata(ip);
  903. int error;
  904. if (journaled)
  905. error = gfs2_trans_begin(sdp, RES_DINODE + RES_JDATA, GFS2_JTRUNC_REVOKES);
  906. else
  907. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  908. if (error)
  909. return error;
  910. error = gfs2_meta_inode_buffer(ip, &dibh);
  911. if (error)
  912. goto out;
  913. gfs2_trans_add_meta(ip->i_gl, dibh);
  914. if (gfs2_is_stuffed(ip)) {
  915. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode) + newsize);
  916. } else {
  917. if (newsize & (u64)(sdp->sd_sb.sb_bsize - 1)) {
  918. error = gfs2_block_truncate_page(mapping, newsize);
  919. if (error)
  920. goto out_brelse;
  921. }
  922. ip->i_diskflags |= GFS2_DIF_TRUNC_IN_PROG;
  923. }
  924. i_size_write(inode, newsize);
  925. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  926. gfs2_dinode_out(ip, dibh->b_data);
  927. if (journaled)
  928. error = gfs2_journaled_truncate(inode, oldsize, newsize);
  929. else
  930. truncate_pagecache(inode, oldsize, newsize);
  931. if (error) {
  932. brelse(dibh);
  933. return error;
  934. }
  935. out_brelse:
  936. brelse(dibh);
  937. out:
  938. gfs2_trans_end(sdp);
  939. return error;
  940. }
  941. static int trunc_dealloc(struct gfs2_inode *ip, u64 size)
  942. {
  943. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  944. unsigned int height = ip->i_height;
  945. u64 lblock;
  946. struct metapath mp;
  947. int error;
  948. if (!size)
  949. lblock = 0;
  950. else
  951. lblock = (size - 1) >> sdp->sd_sb.sb_bsize_shift;
  952. find_metapath(sdp, lblock, &mp, ip->i_height);
  953. error = gfs2_rindex_update(sdp);
  954. if (error)
  955. return error;
  956. error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
  957. if (error)
  958. return error;
  959. while (height--) {
  960. struct strip_mine sm;
  961. sm.sm_first = !!size;
  962. sm.sm_height = height;
  963. error = recursive_scan(ip, NULL, &mp, 0, 0, 1, &sm);
  964. if (error)
  965. break;
  966. }
  967. gfs2_quota_unhold(ip);
  968. return error;
  969. }
  970. static int trunc_end(struct gfs2_inode *ip)
  971. {
  972. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  973. struct buffer_head *dibh;
  974. int error;
  975. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  976. if (error)
  977. return error;
  978. down_write(&ip->i_rw_mutex);
  979. error = gfs2_meta_inode_buffer(ip, &dibh);
  980. if (error)
  981. goto out;
  982. if (!i_size_read(&ip->i_inode)) {
  983. ip->i_height = 0;
  984. ip->i_goal = ip->i_no_addr;
  985. gfs2_buffer_clear_tail(dibh, sizeof(struct gfs2_dinode));
  986. gfs2_ordered_del_inode(ip);
  987. }
  988. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  989. ip->i_diskflags &= ~GFS2_DIF_TRUNC_IN_PROG;
  990. gfs2_trans_add_meta(ip->i_gl, dibh);
  991. gfs2_dinode_out(ip, dibh->b_data);
  992. brelse(dibh);
  993. out:
  994. up_write(&ip->i_rw_mutex);
  995. gfs2_trans_end(sdp);
  996. return error;
  997. }
  998. /**
  999. * do_shrink - make a file smaller
  1000. * @inode: the inode
  1001. * @oldsize: the current inode size
  1002. * @newsize: the size to make the file
  1003. *
  1004. * Called with an exclusive lock on @inode. The @size must
  1005. * be equal to or smaller than the current inode size.
  1006. *
  1007. * Returns: errno
  1008. */
  1009. static int do_shrink(struct inode *inode, u64 oldsize, u64 newsize)
  1010. {
  1011. struct gfs2_inode *ip = GFS2_I(inode);
  1012. int error;
  1013. error = trunc_start(inode, oldsize, newsize);
  1014. if (error < 0)
  1015. return error;
  1016. if (gfs2_is_stuffed(ip))
  1017. return 0;
  1018. error = trunc_dealloc(ip, newsize);
  1019. if (error == 0)
  1020. error = trunc_end(ip);
  1021. return error;
  1022. }
  1023. void gfs2_trim_blocks(struct inode *inode)
  1024. {
  1025. u64 size = inode->i_size;
  1026. int ret;
  1027. ret = do_shrink(inode, size, size);
  1028. WARN_ON(ret != 0);
  1029. }
  1030. /**
  1031. * do_grow - Touch and update inode size
  1032. * @inode: The inode
  1033. * @size: The new size
  1034. *
  1035. * This function updates the timestamps on the inode and
  1036. * may also increase the size of the inode. This function
  1037. * must not be called with @size any smaller than the current
  1038. * inode size.
  1039. *
  1040. * Although it is not strictly required to unstuff files here,
  1041. * earlier versions of GFS2 have a bug in the stuffed file reading
  1042. * code which will result in a buffer overrun if the size is larger
  1043. * than the max stuffed file size. In order to prevent this from
  1044. * occurring, such files are unstuffed, but in other cases we can
  1045. * just update the inode size directly.
  1046. *
  1047. * Returns: 0 on success, or -ve on error
  1048. */
  1049. static int do_grow(struct inode *inode, u64 size)
  1050. {
  1051. struct gfs2_inode *ip = GFS2_I(inode);
  1052. struct gfs2_sbd *sdp = GFS2_SB(inode);
  1053. struct buffer_head *dibh;
  1054. int error;
  1055. int unstuff = 0;
  1056. if (gfs2_is_stuffed(ip) &&
  1057. (size > (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)))) {
  1058. error = gfs2_quota_lock_check(ip);
  1059. if (error)
  1060. return error;
  1061. error = gfs2_inplace_reserve(ip, 1, 0);
  1062. if (error)
  1063. goto do_grow_qunlock;
  1064. unstuff = 1;
  1065. }
  1066. error = gfs2_trans_begin(sdp, RES_DINODE + RES_STATFS + RES_RG_BIT, 0);
  1067. if (error)
  1068. goto do_grow_release;
  1069. if (unstuff) {
  1070. error = gfs2_unstuff_dinode(ip, NULL);
  1071. if (error)
  1072. goto do_end_trans;
  1073. }
  1074. error = gfs2_meta_inode_buffer(ip, &dibh);
  1075. if (error)
  1076. goto do_end_trans;
  1077. i_size_write(inode, size);
  1078. ip->i_inode.i_mtime = ip->i_inode.i_ctime = CURRENT_TIME;
  1079. gfs2_trans_add_meta(ip->i_gl, dibh);
  1080. gfs2_dinode_out(ip, dibh->b_data);
  1081. brelse(dibh);
  1082. do_end_trans:
  1083. gfs2_trans_end(sdp);
  1084. do_grow_release:
  1085. if (unstuff) {
  1086. gfs2_inplace_release(ip);
  1087. do_grow_qunlock:
  1088. gfs2_quota_unlock(ip);
  1089. }
  1090. return error;
  1091. }
  1092. /**
  1093. * gfs2_setattr_size - make a file a given size
  1094. * @inode: the inode
  1095. * @newsize: the size to make the file
  1096. *
  1097. * The file size can grow, shrink, or stay the same size. This
  1098. * is called holding i_mutex and an exclusive glock on the inode
  1099. * in question.
  1100. *
  1101. * Returns: errno
  1102. */
  1103. int gfs2_setattr_size(struct inode *inode, u64 newsize)
  1104. {
  1105. int ret;
  1106. u64 oldsize;
  1107. BUG_ON(!S_ISREG(inode->i_mode));
  1108. ret = inode_newsize_ok(inode, newsize);
  1109. if (ret)
  1110. return ret;
  1111. inode_dio_wait(inode);
  1112. ret = gfs2_rs_alloc(GFS2_I(inode));
  1113. if (ret)
  1114. return ret;
  1115. oldsize = inode->i_size;
  1116. if (newsize >= oldsize)
  1117. return do_grow(inode, newsize);
  1118. return do_shrink(inode, oldsize, newsize);
  1119. }
  1120. int gfs2_truncatei_resume(struct gfs2_inode *ip)
  1121. {
  1122. int error;
  1123. error = trunc_dealloc(ip, i_size_read(&ip->i_inode));
  1124. if (!error)
  1125. error = trunc_end(ip);
  1126. return error;
  1127. }
  1128. int gfs2_file_dealloc(struct gfs2_inode *ip)
  1129. {
  1130. return trunc_dealloc(ip, 0);
  1131. }
  1132. /**
  1133. * gfs2_write_alloc_required - figure out if a write will require an allocation
  1134. * @ip: the file being written to
  1135. * @offset: the offset to write to
  1136. * @len: the number of bytes being written
  1137. *
  1138. * Returns: 1 if an alloc is required, 0 otherwise
  1139. */
  1140. int gfs2_write_alloc_required(struct gfs2_inode *ip, u64 offset,
  1141. unsigned int len)
  1142. {
  1143. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  1144. struct buffer_head bh;
  1145. unsigned int shift;
  1146. u64 lblock, lblock_stop, size;
  1147. u64 end_of_file;
  1148. if (!len)
  1149. return 0;
  1150. if (gfs2_is_stuffed(ip)) {
  1151. if (offset + len >
  1152. sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode))
  1153. return 1;
  1154. return 0;
  1155. }
  1156. shift = sdp->sd_sb.sb_bsize_shift;
  1157. BUG_ON(gfs2_is_dir(ip));
  1158. end_of_file = (i_size_read(&ip->i_inode) + sdp->sd_sb.sb_bsize - 1) >> shift;
  1159. lblock = offset >> shift;
  1160. lblock_stop = (offset + len + sdp->sd_sb.sb_bsize - 1) >> shift;
  1161. if (lblock_stop > end_of_file)
  1162. return 1;
  1163. size = (lblock_stop - lblock) << shift;
  1164. do {
  1165. bh.b_state = 0;
  1166. bh.b_size = size;
  1167. gfs2_block_map(&ip->i_inode, lblock, &bh, 0);
  1168. if (!buffer_mapped(&bh))
  1169. return 1;
  1170. size -= bh.b_size;
  1171. lblock += (bh.b_size >> ip->i_inode.i_blkbits);
  1172. } while(size > 0);
  1173. return 0;
  1174. }