Whamcloud - gitweb
ChangeLog, bitops.h, ext2fs.h, icount.c, version.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                            int  blockcnt,
34                            void *priv_data)
35 {
36         struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
37         blk_t   new_blk;
38         static blk_t    last_blk = 0;
39         char            *block;
40         errcode_t       retval;
41         int             group;
42         
43         if (*blocknr) {
44                 last_blk = *blocknr;
45                 return 0;
46         }
47         retval = ext2fs_new_block(fs, last_blk, 0, &new_blk);
48         if (retval) {
49                 es->err = retval;
50                 return BLOCK_ABORT;
51         }
52         if (blockcnt > 0) {
53                 retval = ext2fs_new_dir_block(fs, 0, 0, &block);
54                 if (retval) {
55                         es->err = retval;
56                         return BLOCK_ABORT;
57                 }
58                 es->done = 1;
59                 retval = ext2fs_write_dir_block(fs, new_blk, block);
60         } else {
61                 retval = ext2fs_get_mem(fs->blocksize, (void **) &block);
62                 if (retval) {
63                         es->err = retval;
64                         return BLOCK_ABORT;
65                 }
66                 memset(block, 0, fs->blocksize);
67                 retval = io_channel_write_blk(fs->io, new_blk, 1, block);
68         }       
69         if (retval) {
70                 es->err = retval;
71                 return BLOCK_ABORT;
72         }
73         ext2fs_free_mem((void **) &block);
74         *blocknr = new_blk;
75         ext2fs_mark_block_bitmap(fs->block_map, new_blk);
76         ext2fs_mark_bb_dirty(fs);
77         group = ext2fs_group_of_blk(fs, new_blk);
78         fs->group_desc[group].bg_free_blocks_count--;
79         fs->super->s_free_blocks_count--;
80         ext2fs_mark_super_dirty(fs);
81         if (es->done)
82                 return (BLOCK_CHANGED | BLOCK_ABORT);
83         else
84                 return BLOCK_CHANGED;
85 }
86
87 errcode_t ext2fs_expand_dir(ext2_filsys fs, ino_t dir)
88 {
89         errcode_t       retval;
90         struct expand_dir_struct es;
91         struct ext2_inode       inode;
92         
93         EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
94
95         if (!(fs->flags & EXT2_FLAG_RW))
96                 return EXT2_ET_RO_FILSYS;
97
98         if (!fs->block_map)
99                 return EXT2_ET_NO_BLOCK_BITMAP;
100
101         retval = ext2fs_check_directory(fs, dir);
102         if (retval)
103                 return retval;
104         
105         es.done = 0;
106         es.err = 0;
107         
108         retval = ext2fs_block_iterate(fs, dir, BLOCK_FLAG_APPEND,
109                                       0, expand_dir_proc, &es);
110
111         if (es.err)
112                 return es.err;
113         if (!es.done)
114                 return EXT2_ET_EXPAND_DIR_ERR;
115
116         /*
117          * Update the size and block count fields in the inode.
118          */
119         retval = ext2fs_read_inode(fs, dir, &inode);
120         if (retval)
121                 return retval;
122         
123         inode.i_size += fs->blocksize;
124         inode.i_blocks += fs->blocksize / 512;
125
126         retval = ext2fs_write_inode(fs, dir, &inode);
127         if (retval)
128                 return retval;
129
130         return 0;
131 }