Coverage Report

Created: 2024-09-08 06:27

/src/e2fsprogs/lib/ext2fs/i_block.c
Line
Count
Source (jump to first uncovered line)
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
0
{
33
0
  unsigned long long b = inode->i_blocks;
34
35
0
  if (ext2fs_has_feature_huge_file(fs->super))
36
0
    b += ((long long) inode->osd2.linux2.l_i_blocks_hi) << 32;
37
38
0
  if (!ext2fs_has_feature_huge_file(fs->super) ||
39
0
      !(inode->i_flags & EXT4_HUGE_FILE_FL))
40
0
      num_blocks *= fs->blocksize / 512;
41
0
  num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
42
43
0
  b += num_blocks;
44
45
0
  if (ext2fs_has_feature_huge_file(fs->super))
46
0
    inode->osd2.linux2.l_i_blocks_hi = b >> 32;
47
0
  else if (b > 0xFFFFFFFF)
48
0
    return EOVERFLOW;
49
0
  inode->i_blocks = b & 0xFFFFFFFF;
50
0
  return 0;
51
0
}
52
53
errcode_t ext2fs_iblk_sub_blocks(ext2_filsys fs, struct ext2_inode *inode,
54
         blk64_t num_blocks)
55
0
{
56
0
  unsigned long long b = inode->i_blocks;
57
58
0
  if (ext2fs_has_feature_huge_file(fs->super))
59
0
    b += ((long long) inode->osd2.linux2.l_i_blocks_hi) << 32;
60
61
0
  if (!ext2fs_has_feature_huge_file(fs->super) ||
62
0
      !(inode->i_flags & EXT4_HUGE_FILE_FL))
63
0
      num_blocks *= fs->blocksize / 512;
64
0
  num_blocks *= EXT2FS_CLUSTER_RATIO(fs);
65
66
0
  if (num_blocks > b)
67
0
    return EOVERFLOW;
68
69
0
  b -= num_blocks;
70
71
0
  if (ext2fs_has_feature_huge_file(fs->super))
72
0
    inode->osd2.linux2.l_i_blocks_hi = b >> 32;
73
0
  inode->i_blocks = b & 0xFFFFFFFF;
74
0
  return 0;
75
0
}
76
77
errcode_t ext2fs_iblk_set(ext2_filsys fs, struct ext2_inode *inode, blk64_t b)
78
0
{
79
0
  if (!ext2fs_has_feature_huge_file(fs->super) ||
80
0
      !(inode->i_flags & EXT4_HUGE_FILE_FL))
81
0
    b *= fs->blocksize / 512;
82
0
  b *= EXT2FS_CLUSTER_RATIO(fs);
83
84
0
  inode->i_blocks = b & 0xFFFFFFFF;
85
0
  if (ext2fs_has_feature_huge_file(fs->super))
86
0
    inode->osd2.linux2.l_i_blocks_hi = b >> 32;
87
0
  else if (b >> 32)
88
0
    return EOVERFLOW;
89
0
  return 0;
90
0
}