Home | History | Annotate | Download | only in ext2fs
      1 /*
      2  * link.c --- create links in a ext2fs directory
      3  *
      4  * Copyright (C) 1993, 1994 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 
     22 struct link_struct  {
     23 	ext2_filsys	fs;
     24 	const char	*name;
     25 	int		namelen;
     26 	ext2_ino_t	inode;
     27 	int		flags;
     28 	int		done;
     29 	unsigned int	blocksize;
     30 	errcode_t	err;
     31 	struct ext2_super_block *sb;
     32 };
     33 
     34 static int link_proc(struct ext2_dir_entry *dirent,
     35 		     int	offset,
     36 		     int	blocksize,
     37 		     char	*buf,
     38 		     void	*priv_data)
     39 {
     40 	struct link_struct *ls = (struct link_struct *) priv_data;
     41 	struct ext2_dir_entry *next;
     42 	unsigned int rec_len, min_rec_len, curr_rec_len;
     43 	int ret = 0;
     44 	int csum_size = 0;
     45 	struct ext2_dir_entry_tail *t;
     46 
     47 	if (ls->done)
     48 		return DIRENT_ABORT;
     49 
     50 	rec_len = EXT2_DIR_REC_LEN(ls->namelen);
     51 
     52 	ls->err = ext2fs_get_rec_len(ls->fs, dirent, &curr_rec_len);
     53 	if (ls->err)
     54 		return DIRENT_ABORT;
     55 
     56 	if (ext2fs_has_feature_metadata_csum(ls->fs->super))
     57 		csum_size = sizeof(struct ext2_dir_entry_tail);
     58 	/*
     59 	 * See if the following directory entry (if any) is unused;
     60 	 * if so, absorb it into this one.
     61 	 */
     62 	next = (struct ext2_dir_entry *) (buf + offset + curr_rec_len);
     63 	if ((offset + (int) curr_rec_len < blocksize - (8 + csum_size)) &&
     64 	    (next->inode == 0) &&
     65 	    (offset + (int) curr_rec_len + (int) next->rec_len <= blocksize)) {
     66 		curr_rec_len += next->rec_len;
     67 		ls->err = ext2fs_set_rec_len(ls->fs, curr_rec_len, dirent);
     68 		if (ls->err)
     69 			return DIRENT_ABORT;
     70 		ret = DIRENT_CHANGED;
     71 	}
     72 
     73 	/*
     74 	 * Since ext2fs_link blows away htree data, we need to be
     75 	 * careful -- if metadata_csum is enabled and we're passed in
     76 	 * a dirent that contains htree data, we need to create the
     77 	 * fake entry at the end of the block that hides the checksum.
     78 	 */
     79 
     80 	/* De-convert a dx_node block */
     81 	if (csum_size &&
     82 	    curr_rec_len == ls->fs->blocksize &&
     83 	    !dirent->inode) {
     84 		curr_rec_len -= csum_size;
     85 		ls->err = ext2fs_set_rec_len(ls->fs, curr_rec_len, dirent);
     86 		if (ls->err)
     87 			return DIRENT_ABORT;
     88 		t = EXT2_DIRENT_TAIL(buf, ls->fs->blocksize);
     89 		ext2fs_initialize_dirent_tail(ls->fs, t);
     90 		ret = DIRENT_CHANGED;
     91 	}
     92 
     93 	/* De-convert a dx_root block */
     94 	if (csum_size &&
     95 	    curr_rec_len == ls->fs->blocksize - EXT2_DIR_REC_LEN(1) &&
     96 	    offset == EXT2_DIR_REC_LEN(1) &&
     97 	    dirent->name[0] == '.' && dirent->name[1] == '.') {
     98 		curr_rec_len -= csum_size;
     99 		ls->err = ext2fs_set_rec_len(ls->fs, curr_rec_len, dirent);
    100 		if (ls->err)
    101 			return DIRENT_ABORT;
    102 		t = EXT2_DIRENT_TAIL(buf, ls->fs->blocksize);
    103 		ext2fs_initialize_dirent_tail(ls->fs, t);
    104 		ret = DIRENT_CHANGED;
    105 	}
    106 
    107 	/*
    108 	 * If the directory entry is used, see if we can split the
    109 	 * directory entry to make room for the new name.  If so,
    110 	 * truncate it and return.
    111 	 */
    112 	if (dirent->inode) {
    113 		min_rec_len = EXT2_DIR_REC_LEN(ext2fs_dirent_name_len(dirent));
    114 		if (curr_rec_len < (min_rec_len + rec_len))
    115 			return ret;
    116 		rec_len = curr_rec_len - min_rec_len;
    117 		ls->err = ext2fs_set_rec_len(ls->fs, min_rec_len, dirent);
    118 		if (ls->err)
    119 			return DIRENT_ABORT;
    120 		next = (struct ext2_dir_entry *) (buf + offset +
    121 						  dirent->rec_len);
    122 		next->inode = 0;
    123 		ext2fs_dirent_set_name_len(next, 0);
    124 		ext2fs_dirent_set_file_type(next, 0);
    125 		ls->err = ext2fs_set_rec_len(ls->fs, rec_len, next);
    126 		if (ls->err)
    127 			return DIRENT_ABORT;
    128 		return DIRENT_CHANGED;
    129 	}
    130 
    131 	/*
    132 	 * If we get this far, then the directory entry is not used.
    133 	 * See if we can fit the request entry in.  If so, do it.
    134 	 */
    135 	if (curr_rec_len < rec_len)
    136 		return ret;
    137 	dirent->inode = ls->inode;
    138 	ext2fs_dirent_set_name_len(dirent, ls->namelen);
    139 	strncpy(dirent->name, ls->name, ls->namelen);
    140 	if (ext2fs_has_feature_filetype(ls->sb))
    141 		ext2fs_dirent_set_file_type(dirent, ls->flags & 0x7);
    142 
    143 	ls->done++;
    144 	return DIRENT_ABORT|DIRENT_CHANGED;
    145 }
    146 
    147 /*
    148  * Note: the low 3 bits of the flags field are used as the directory
    149  * entry filetype.
    150  */
    151 #ifdef __TURBOC__
    152  #pragma argsused
    153 #endif
    154 errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name,
    155 		      ext2_ino_t ino, int flags)
    156 {
    157 	errcode_t		retval;
    158 	struct link_struct	ls;
    159 	struct ext2_inode	inode;
    160 
    161 	EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
    162 
    163 	if (!(fs->flags & EXT2_FLAG_RW))
    164 		return EXT2_ET_RO_FILSYS;
    165 
    166 	ls.fs = fs;
    167 	ls.name = name;
    168 	ls.namelen = name ? strlen(name) : 0;
    169 	ls.inode = ino;
    170 	ls.flags = flags;
    171 	ls.done = 0;
    172 	ls.sb = fs->super;
    173 	ls.blocksize = fs->blocksize;
    174 	ls.err = 0;
    175 
    176 	retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY,
    177 				    0, link_proc, &ls);
    178 	if (retval)
    179 		return retval;
    180 	if (ls.err)
    181 		return ls.err;
    182 
    183 	if (!ls.done)
    184 		return EXT2_ET_DIR_NO_SPACE;
    185 
    186 	if ((retval = ext2fs_read_inode(fs, dir, &inode)) != 0)
    187 		return retval;
    188 
    189 	/*
    190 	 * If this function changes to preserve the htree, remove the
    191 	 * two hunks in link_proc that shove checksum tails into the
    192 	 * former dx_root/dx_node blocks.
    193 	 */
    194 	if (inode.i_flags & EXT2_INDEX_FL) {
    195 		inode.i_flags &= ~EXT2_INDEX_FL;
    196 		if ((retval = ext2fs_write_inode(fs, dir, &inode)) != 0)
    197 			return retval;
    198 	}
    199 
    200 	return 0;
    201 }
    202