blob: f36c3c47d480cd0dbb75311cdb559d2b29783eb5 [file] [log] [blame]
Theodore Ts'o1ca10592008-04-09 11:39:11 -04001/*
2 * i_block.c --- Manage the i_block field for i_blocks
3 *
4 * Copyright (C) 2008 Theodore Ts'o.
5 *
6 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'o1ca10592008-04-09 11:39:11 -04009 * %End-Header%
10 */
11
12#include <stdio.h>
13#if HAVE_UNISTD_H
14#include <unistd.h>
15#endif
16#include <time.h>
17#include <string.h>
18#if HAVE_SYS_STAT_H
19#include <sys/stat.h>
20#endif
21#if HAVE_SYS_TYPES_H
22#include <sys/types.h>
23#endif
Theodore Ts'o5d108072009-10-21 01:46:58 -040024#include <errno.h>
Theodore Ts'o1ca10592008-04-09 11:39:11 -040025
26#include "ext2_fs.h"
27#include "ext2fs.h"
28
29errcode_t ext2fs_iblk_add_blocks(ext2_filsys fs, struct ext2_inode *inode,
30 blk64_t num_blocks)
31{
Theodore Ts'o5d108072009-10-21 01:46:58 -040032 unsigned long long b = inode->i_blocks;
Theodore Ts'o1ca10592008-04-09 11:39:11 -040033
Theodore Ts'o47e155d2010-07-21 16:10:10 -040034 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
35 b += ((long long) inode->osd2.linux2.l_i_blocks_hi) << 32;
36
Theodore Ts'o5d108072009-10-21 01:46:58 -040037 if (!(fs->super->s_feature_ro_compat &
38 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
39 !(inode->i_flags & EXT4_HUGE_FILE_FL))
40 num_blocks *= fs->blocksize / 512;
Theodore Ts'ob2e6c862011-06-16 10:12:43 -040041 num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
Theodore Ts'o5d108072009-10-21 01:46:58 -040042
43 b += num_blocks;
44
Theodore Ts'o47e155d2010-07-21 16:10:10 -040045 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
Theodore Ts'o1ca10592008-04-09 11:39:11 -040046 inode->osd2.linux2.l_i_blocks_hi = b >> 32;
Theodore Ts'o47e155d2010-07-21 16:10:10 -040047 else if (b > 0xFFFFFFFF)
Theodore Ts'o5d108072009-10-21 01:46:58 -040048 return EOVERFLOW;
49 inode->i_blocks = b & 0xFFFFFFFF;
Theodore Ts'o1ca10592008-04-09 11:39:11 -040050 return 0;
51}
52
Theodore Ts'o1ca10592008-04-09 11:39:11 -040053errcode_t ext2fs_iblk_sub_blocks(ext2_filsys fs, struct ext2_inode *inode,
54 blk64_t num_blocks)
55{
Theodore Ts'o5d108072009-10-21 01:46:58 -040056 unsigned long long b = inode->i_blocks;
Theodore Ts'o1ca10592008-04-09 11:39:11 -040057
Theodore Ts'o47e155d2010-07-21 16:10:10 -040058 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
59 b += ((long long) inode->osd2.linux2.l_i_blocks_hi) << 32;
60
Theodore Ts'o5d108072009-10-21 01:46:58 -040061 if (!(fs->super->s_feature_ro_compat &
62 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
63 !(inode->i_flags & EXT4_HUGE_FILE_FL))
64 num_blocks *= fs->blocksize / 512;
Theodore Ts'ob2e6c862011-06-16 10:12:43 -040065 num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
Theodore Ts'o5d108072009-10-21 01:46:58 -040066
67 if (num_blocks > b)
68 return EOVERFLOW;
69
70 b -= num_blocks;
71
Theodore Ts'o47e155d2010-07-21 16:10:10 -040072 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
Theodore Ts'o1ca10592008-04-09 11:39:11 -040073 inode->osd2.linux2.l_i_blocks_hi = b >> 32;
Theodore Ts'o5d108072009-10-21 01:46:58 -040074 inode->i_blocks = b & 0xFFFFFFFF;
Theodore Ts'o1ca10592008-04-09 11:39:11 -040075 return 0;
76}
77
78errcode_t ext2fs_iblk_set(ext2_filsys fs, struct ext2_inode *inode, blk64_t b)
79{
Theodore Ts'o5d108072009-10-21 01:46:58 -040080 if (!(fs->super->s_feature_ro_compat &
81 EXT4_FEATURE_RO_COMPAT_HUGE_FILE) ||
82 !(inode->i_flags & EXT4_HUGE_FILE_FL))
83 b *= fs->blocksize / 512;
Theodore Ts'ob2e6c862011-06-16 10:12:43 -040084 b *= EXT2FS_CLUSTER_RATIO(fs);
Theodore Ts'o5d108072009-10-21 01:46:58 -040085
86 inode->i_blocks = b & 0xFFFFFFFF;
87 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_HUGE_FILE)
Theodore Ts'o1ca10592008-04-09 11:39:11 -040088 inode->osd2.linux2.l_i_blocks_hi = b >> 32;
Theodore Ts'o5d108072009-10-21 01:46:58 -040089 else if (b >> 32)
90 return EOVERFLOW;
Theodore Ts'o1ca10592008-04-09 11:39:11 -040091 return 0;
92}