Home | History | Annotate | Download | only in ext2fs
      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 <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 
     21 struct expand_dir_struct {
     22 	int		done;
     23 	int		newblocks;
     24 	blk64_t		goal;
     25 	errcode_t	err;
     26 };
     27 
     28 static int expand_dir_proc(ext2_filsys	fs,
     29 			   blk64_t	*blocknr,
     30 			   e2_blkcnt_t	blockcnt,
     31 			   blk64_t	ref_block EXT2FS_ATTR((unused)),
     32 			   int		ref_offset EXT2FS_ATTR((unused)),
     33 			   void		*priv_data)
     34 {
     35 	struct expand_dir_struct *es = (struct expand_dir_struct *) priv_data;
     36 	blk64_t	new_blk;
     37 	char		*block;
     38 	errcode_t	retval;
     39 
     40 	if (*blocknr) {
     41 		if (blockcnt >= 0)
     42 			es->goal = *blocknr;
     43 		return 0;
     44 	}
     45 	if (blockcnt &&
     46 	    (EXT2FS_B2C(fs, es->goal) == EXT2FS_B2C(fs, es->goal+1)))
     47 		new_blk = es->goal+1;
     48 	else {
     49 		es->goal &= ~EXT2FS_CLUSTER_MASK(fs);
     50 		retval = ext2fs_new_block2(fs, es->goal, 0, &new_blk);
     51 		if (retval) {
     52 			es->err = retval;
     53 			return BLOCK_ABORT;
     54 		}
     55 		es->newblocks++;
     56 		ext2fs_block_alloc_stats2(fs, new_blk, +1);
     57 	}
     58 	if (blockcnt > 0) {
     59 		retval = ext2fs_new_dir_block(fs, 0, 0, &block);
     60 		if (retval) {
     61 			es->err = retval;
     62 			return BLOCK_ABORT;
     63 		}
     64 		es->done = 1;
     65 		retval = ext2fs_write_dir_block(fs, new_blk, block);
     66 	} else {
     67 		retval = ext2fs_get_mem(fs->blocksize, &block);
     68 		if (retval) {
     69 			es->err = retval;
     70 			return BLOCK_ABORT;
     71 		}
     72 		memset(block, 0, fs->blocksize);
     73 		retval = io_channel_write_blk64(fs->io, new_blk, 1, block);
     74 	}
     75 	if (blockcnt >= 0)
     76 		es->goal = new_blk;
     77 	if (retval) {
     78 		es->err = retval;
     79 		return BLOCK_ABORT;
     80 	}
     81 	ext2fs_free_mem(&block);
     82 	*blocknr = new_blk;
     83 
     84 	if (es->done)
     85 		return (BLOCK_CHANGED | BLOCK_ABORT);
     86 	else
     87 		return BLOCK_CHANGED;
     88 }
     89 
     90 errcode_t ext2fs_expand_dir(ext2_filsys fs, ext2_ino_t dir)
     91 {
     92 	errcode_t	retval;
     93 	struct expand_dir_struct es;
     94 	struct ext2_inode	inode;
     95 
     96 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
     97 
     98 	if (!(fs->flags & EXT2_FLAG_RW))
     99 		return EXT2_ET_RO_FILSYS;
    100 
    101 	if (!fs->block_map)
    102 		return EXT2_ET_NO_BLOCK_BITMAP;
    103 
    104 	retval = ext2fs_check_directory(fs, dir);
    105 	if (retval)
    106 		return retval;
    107 
    108 	es.done = 0;
    109 	es.err = 0;
    110 	es.goal = 0;
    111 	es.newblocks = 0;
    112 
    113 	retval = ext2fs_block_iterate3(fs, dir, BLOCK_FLAG_APPEND,
    114 				       0, expand_dir_proc, &es);
    115 
    116 	if (es.err)
    117 		return es.err;
    118 	if (!es.done)
    119 		return EXT2_ET_EXPAND_DIR_ERR;
    120 
    121 	/*
    122 	 * Update the size and block count fields in the inode.
    123 	 */
    124 	retval = ext2fs_read_inode(fs, dir, &inode);
    125 	if (retval)
    126 		return retval;
    127 
    128 	inode.i_size += fs->blocksize;
    129 	ext2fs_iblk_add_blocks(fs, &inode, es.newblocks);
    130 
    131 	retval = ext2fs_write_inode(fs, dir, &inode);
    132 	if (retval)
    133 		return retval;
    134 
    135 	return 0;
    136 }
    137