Whamcloud - gitweb
e2fsck: add support for xattrs in external inodes
[tools/e2fsprogs.git] / lib / ext2fs / i_block.c
1 /*
2  * i_block.c --- Manage the i_block field for i_blocks
3  *
4  * Copyright (C) 2008 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 #include "config.h"
13 #include <stdio.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <time.h>
18 #include <string.h>
19 #if HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22 #if HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25 #include <errno.h>
26
27 #include "ext2_fs.h"
28 #include "ext2fs.h"
29
30 errcode_t ext2fs_iblk_add_blocks(ext2_filsys fs, struct ext2_inode *inode,
31                                  blk64_t num_blocks)
32 {
33         unsigned long long b = inode->i_blocks;
34
35         if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
36                 b += ((long long) inode->osd2.linux2.l_i_blocks_hi) << 32;
37
38         if (!(fs->super->s_feature_ro_compat &
39               EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
40             !(inode->i_flags & EXT4_HUGE_FILE_FL))
41             num_blocks *= fs->blocksize / 512;
42         num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
43
44         b += num_blocks;
45
46         if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
47                 inode->osd2.linux2.l_i_blocks_hi = b >> 32;
48         else if (b > 0xFFFFFFFF)
49                 return EOVERFLOW;
50         inode->i_blocks = b & 0xFFFFFFFF;
51         return 0;
52 }
53
54 errcode_t ext2fs_iblk_sub_blocks(ext2_filsys fs, struct ext2_inode *inode,
55                                  blk64_t num_blocks)
56 {
57         unsigned long long b = inode->i_blocks;
58
59         if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
60                 b += ((long long) inode->osd2.linux2.l_i_blocks_hi) << 32;
61
62         if (!(fs->super->s_feature_ro_compat &
63               EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
64             !(inode->i_flags & EXT4_HUGE_FILE_FL))
65             num_blocks *= fs->blocksize / 512;
66         num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
67
68         if (num_blocks > b)
69                 return EOVERFLOW;
70
71         b -= num_blocks;
72
73         if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
74                 inode->osd2.linux2.l_i_blocks_hi = b >> 32;
75         inode->i_blocks = b & 0xFFFFFFFF;
76         return 0;
77 }
78
79 errcode_t ext2fs_iblk_set(ext2_filsys fs, struct ext2_inode *inode, blk64_t b)
80 {
81         if (!(fs->super->s_feature_ro_compat &
82               EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
83             !(inode->i_flags & EXT4_HUGE_FILE_FL))
84                 b *= fs->blocksize / 512;
85         b *= EXT2FS_CLUSTER_RATIO(fs);
86
87         inode->i_blocks = b & 0xFFFFFFFF;
88         if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
89                 inode->osd2.linux2.l_i_blocks_hi = b >> 32;
90         else if (b >> 32)
91                 return EOVERFLOW;
92         return 0;
93 }