/src/e2fsprogs/lib/ext2fs/expanddir.c
Line | Count | Source |
1 | | /* |
2 | | * expand.c --- expand an ext2fs directory |
3 | | * |
4 | | * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 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 | | #include <string.h> |
15 | | #if HAVE_UNISTD_H |
16 | | #include <unistd.h> |
17 | | #endif |
18 | | |
19 | | #include "ext2_fs.h" |
20 | | #include "ext2fs.h" |
21 | | #include "ext2fsP.h" |
22 | | |
23 | | struct expand_dir_struct { |
24 | | int done; |
25 | | int newblocks; |
26 | | blk64_t goal; |
27 | | errcode_t err; |
28 | | ext2_ino_t dir; |
29 | | }; |
30 | | |
31 | | static int expand_dir_proc(ext2_filsys fs, |
32 | | blk64_t *blocknr, |
33 | | e2_blkcnt_t blockcnt, |
34 | | blk64_t ref_block EXT2FS_ATTR((unused)), |
35 | | int ref_offset EXT2FS_ATTR((unused)), |
36 | | void *priv_data) |
37 | 0 | { |
38 | 0 | struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data; |
39 | 0 | blk64_t new_blk; |
40 | 0 | char *block; |
41 | 0 | errcode_t retval; |
42 | |
|
43 | 0 | if (*blocknr) { |
44 | 0 | if (blockcnt >= 0) |
45 | 0 | es->goal = *blocknr; |
46 | 0 | return 0; |
47 | 0 | } |
48 | 0 | if (blockcnt && |
49 | 0 | (EXT2FS_B2C(fs, es->goal) == EXT2FS_B2C(fs, es->goal+1))) |
50 | 0 | new_blk = es->goal+1; |
51 | 0 | else { |
52 | 0 | es->goal &= ~EXT2FS_CLUSTER_MASK(fs); |
53 | 0 | retval = ext2fs_new_block2(fs, es->goal, 0, &new_blk); |
54 | 0 | if (retval) { |
55 | 0 | es->err = retval; |
56 | 0 | return BLOCK_ABORT; |
57 | 0 | } |
58 | 0 | es->newblocks++; |
59 | 0 | ext2fs_block_alloc_stats2(fs, new_blk, +1); |
60 | 0 | } |
61 | 0 | if (blockcnt > 0) { |
62 | 0 | retval = ext2fs_new_dir_block(fs, 0, 0, &block); |
63 | 0 | if (retval) { |
64 | 0 | es->err = retval; |
65 | 0 | return BLOCK_ABORT; |
66 | 0 | } |
67 | 0 | es->done = 1; |
68 | 0 | retval = ext2fs_write_dir_block4(fs, new_blk, block, 0, |
69 | 0 | es->dir); |
70 | 0 | ext2fs_free_mem(&block); |
71 | 0 | } else |
72 | 0 | retval = ext2fs_zero_blocks2(fs, new_blk, 1, NULL, NULL); |
73 | 0 | if (blockcnt >= 0) |
74 | 0 | es->goal = new_blk; |
75 | 0 | if (retval) { |
76 | 0 | es->err = retval; |
77 | 0 | return BLOCK_ABORT; |
78 | 0 | } |
79 | 0 | *blocknr = new_blk; |
80 | |
|
81 | 0 | if (es->done) |
82 | 0 | return (BLOCK_CHANGED | BLOCK_ABORT); |
83 | 0 | else |
84 | 0 | return BLOCK_CHANGED; |
85 | 0 | } |
86 | | |
87 | | errcode_t ext2fs_expand_dir(ext2_filsys fs, ext2_ino_t dir) |
88 | 0 | { |
89 | 0 | errcode_t retval; |
90 | 0 | struct expand_dir_struct es; |
91 | 0 | struct ext2_inode inode; |
92 | |
|
93 | 0 | EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); |
94 | | |
95 | 0 | if (!(fs->flags & EXT2_FLAG_RW)) |
96 | 0 | return EXT2_ET_RO_FILSYS; |
97 | | |
98 | 0 | if (!fs->block_map) |
99 | 0 | return EXT2_ET_NO_BLOCK_BITMAP; |
100 | | |
101 | 0 | retval = ext2fs_check_directory(fs, dir); |
102 | 0 | if (retval) |
103 | 0 | return retval; |
104 | | |
105 | 0 | retval = ext2fs_read_inode(fs, dir, &inode); |
106 | 0 | if (retval) |
107 | 0 | return retval; |
108 | | |
109 | 0 | es.done = 0; |
110 | 0 | es.err = 0; |
111 | 0 | es.goal = ext2fs_find_inode_goal(fs, dir, &inode, 0); |
112 | 0 | es.newblocks = 0; |
113 | 0 | es.dir = dir; |
114 | |
|
115 | 0 | retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_APPEND, |
116 | 0 | 0, expand_dir_proc, &es); |
117 | 0 | if (retval == EXT2_ET_INLINE_DATA_CANT_ITERATE) |
118 | 0 | return ext2fs_inline_data_expand(fs, dir); |
119 | | |
120 | 0 | if (es.err) |
121 | 0 | return es.err; |
122 | 0 | if (!es.done) |
123 | 0 | return EXT2_ET_EXPAND_DIR_ERR; |
124 | | |
125 | | /* |
126 | | * Update the size and block count fields in the inode. |
127 | | */ |
128 | 0 | retval = ext2fs_read_inode(fs, dir, &inode); |
129 | 0 | if (retval) |
130 | 0 | return retval; |
131 | | |
132 | 0 | retval = ext2fs_inode_size_set(fs, &inode, |
133 | 0 | EXT2_I_SIZE(&inode) + fs->blocksize); |
134 | 0 | if (retval) |
135 | 0 | return retval; |
136 | 0 | ext2fs_iblk_add_blocks(fs, &inode, es.newblocks); |
137 | |
|
138 | 0 | retval = ext2fs_write_inode(fs, dir, &inode); |
139 | 0 | if (retval) |
140 | 0 | return retval; |
141 | | |
142 | 0 | return 0; |
143 | 0 | } |