Whamcloud - gitweb
ChangeLog, message.c:
[tools/e2fsprogs.git] / lib / ext2fs / expanddir.c
1 /*
2  * expand.c --- expand an ext2fs directory
3  * 
4  * Copyright (C) 1993, 1994, 1995, 1996 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17
18 #if EXT2_FLAT_INCLUDES
19 #include "ext2_fs.h"
20 #else
21 #include <linux/ext2_fs.h>
22 #endif
23
24 #include "ext2fs.h"
25
26 struct expand_dir_struct {
27         int     done;
28         errcode_t       err;
29 };
30
31 static int expand_dir_proc(ext2_filsys          fs,
32                            blk_t                *blocknr,
33                            e2_blkcnt_t          blockcnt,
34                            blk_t                ref_block,
35                            int                  ref_offset,
36                            void                 *priv_data)
37 {
38         struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
39         blk_t   new_blk;
40         static blk_t    last_blk = 0;
41         char            *block;
42         errcode_t       retval;
43         int             group;
44         
45         if (*blocknr) {
46                 last_blk = *blocknr;
47                 return 0;
48         }
49         retval = ext2fs_new_block(fs, last_blk, 0, &new_blk);
50         if (retval) {
51                 es->err = retval;
52                 return BLOCK_ABORT;
53         }
54         if (blockcnt > 0) {
55                 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
56                 if (retval) {
57                         es->err = retval;
58                         return BLOCK_ABORT;
59                 }
60                 es->done = 1;
61                 retval = ext2fs_write_dir_block(fs, new_blk, block);
62         } else {
63                 retval = ext2fs_get_mem(fs->blocksize, (void **) &block);
64                 if (retval) {
65                         es->err = retval;
66                         return BLOCK_ABORT;
67                 }
68                 memset(block, 0, fs->blocksize);
69                 retval = io_channel_write_blk(fs->io, new_blk, 1, block);
70         }       
71         if (retval) {
72                 es->err = retval;
73                 return BLOCK_ABORT;
74         }
75         ext2fs_free_mem((void **) &block);
76         *blocknr = new_blk;
77         ext2fs_mark_block_bitmap(fs->block_map, new_blk);
78         ext2fs_mark_bb_dirty(fs);
79         group = ext2fs_group_of_blk(fs, new_blk);
80         fs->group_desc[group].bg_free_blocks_count--;
81         fs->super->s_free_blocks_count--;
82         ext2fs_mark_super_dirty(fs);
83         if (es->done)
84                 return (BLOCK_CHANGED | BLOCK_ABORT);
85         else
86                 return BLOCK_CHANGED;
87 }
88
89 errcode_t ext2fs_expand_dir(ext2_filsys fs, ino_t dir)
90 {
91         errcode_t       retval;
92         struct expand_dir_struct es;
93         struct ext2_inode       inode;
94         
95         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
96
97         if (!(fs->flags & EXT2_FLAG_RW))
98                 return EXT2_ET_RO_FILSYS;
99
100         if (!fs->block_map)
101                 return EXT2_ET_NO_BLOCK_BITMAP;
102
103         retval = ext2fs_check_directory(fs, dir);
104         if (retval)
105                 return retval;
106         
107         es.done = 0;
108         es.err = 0;
109         
110         retval = ext2fs_block_iterate2(fs, dir, BLOCK_FLAG_APPEND,
111                                        0, expand_dir_proc, &es);
112
113         if (es.err)
114                 return es.err;
115         if (!es.done)
116                 return EXT2_ET_EXPAND_DIR_ERR;
117
118         /*
119          * Update the size and block count fields in the inode.
120          */
121         retval = ext2fs_read_inode(fs, dir, &inode);
122         if (retval)
123                 return retval;
124         
125         inode.i_size += fs->blocksize;
126         inode.i_blocks += fs->blocksize / 512;
127
128         retval = ext2fs_write_inode(fs, dir, &inode);
129         if (retval)
130                 return retval;
131
132         return 0;
133 }