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