expanddir.c revision 9f8046fc6dfc13eee2f5c363214e60b533872cac
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 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#include "ext2_fs.h"
19#include "ext2fs.h"
20
21struct expand_dir_struct {
22	int		done;
23	int		newblocks;
24	errcode_t	err;
25};
26
27static int expand_dir_proc(ext2_filsys		fs,
28			   blk_t		*blocknr,
29			   e2_blkcnt_t		blockcnt,
30			   blk_t		ref_block,
31			   int			ref_offset,
32			   void			*priv_data)
33{
34	struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
35	blk_t	new_blk;
36	static blk_t	last_blk = 0;
37	char		*block;
38	errcode_t	retval;
39	int		group;
40
41	if (*blocknr) {
42		last_blk = *blocknr;
43		return 0;
44	}
45	retval = ext2fs_new_block(fs, last_blk, 0, &new_blk);
46	if (retval) {
47		es->err = retval;
48		return BLOCK_ABORT;
49	}
50	if (blockcnt > 0) {
51		retval = ext2fs_new_dir_block(fs, 0, 0, &block);
52		if (retval) {
53			es->err = retval;
54			return BLOCK_ABORT;
55		}
56		es->done = 1;
57		retval = ext2fs_write_dir_block(fs, new_blk, block);
58	} else {
59		retval = ext2fs_get_mem(fs->blocksize, (void **) &block);
60		if (retval) {
61			es->err = retval;
62			return BLOCK_ABORT;
63		}
64		memset(block, 0, fs->blocksize);
65		retval = io_channel_write_blk(fs->io, new_blk, 1, block);
66	}
67	if (retval) {
68		es->err = retval;
69		return BLOCK_ABORT;
70	}
71	ext2fs_free_mem((void **) &block);
72	*blocknr = new_blk;
73	ext2fs_mark_block_bitmap(fs->block_map, new_blk);
74	ext2fs_mark_bb_dirty(fs);
75	group = ext2fs_group_of_blk(fs, new_blk);
76	fs->group_desc[group].bg_free_blocks_count--;
77	fs->super->s_free_blocks_count--;
78	ext2fs_mark_super_dirty(fs);
79	es->newblocks++;
80
81	if (es->done)
82		return (BLOCK_CHANGED | BLOCK_ABORT);
83	else
84		return BLOCK_CHANGED;
85}
86
87errcode_t ext2fs_expand_dir(ext2_filsys fs, ext2_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	es.newblocks = 0;
108
109	retval = ext2fs_block_iterate2(fs, dir, BLOCK_FLAG_APPEND,
110				       0, expand_dir_proc, &es);
111
112	if (es.err)
113		return es.err;
114	if (!es.done)
115		return EXT2_ET_EXPAND_DIR_ERR;
116
117	/*
118	 * Update the size and block count fields in the inode.
119	 */
120	retval = ext2fs_read_inode(fs, dir, &inode);
121	if (retval)
122		return retval;
123
124	inode.i_size += fs->blocksize;
125	inode.i_blocks += (fs->blocksize / 512) * es.newblocks;
126
127	retval = ext2fs_write_inode(fs, dir, &inode);
128	if (retval)
129		return retval;
130
131	return 0;
132}
133