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