2 * link.c --- create links in a ext2fs directory
4 * Copyright (C) 1993, 1994 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
29 unsigned int blocksize;
31 struct ext2_super_block *sb;
34 static int link_proc(struct ext2_dir_entry *dirent,
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;
45 struct ext2_dir_entry_tail *t;
50 rec_len = EXT2_DIR_REC_LEN(ls->namelen);
52 ls->err = ext2fs_get_rec_len(ls->fs, dirent, &curr_rec_len);
56 if (ext2fs_has_feature_metadata_csum(ls->fs->super))
57 csum_size = sizeof(struct ext2_dir_entry_tail);
59 * See if the following directory entry (if any) is unused;
60 * if so, absorb it into this one.
62 next = (struct ext2_dir_entry *) (buf + offset + curr_rec_len);
63 if ((offset + (int) curr_rec_len < blocksize - (8 + csum_size)) &&
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);
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.
80 /* De-convert a dx_node block */
82 curr_rec_len == ls->fs->blocksize &&
84 curr_rec_len -= csum_size;
85 ls->err = ext2fs_set_rec_len(ls->fs, curr_rec_len, dirent);
88 t = EXT2_DIRENT_TAIL(buf, ls->fs->blocksize);
89 ext2fs_initialize_dirent_tail(ls->fs, t);
93 /* De-convert a dx_root block */
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);
102 t = EXT2_DIRENT_TAIL(buf, ls->fs->blocksize);
103 ext2fs_initialize_dirent_tail(ls->fs, t);
104 ret = DIRENT_CHANGED;
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.
113 min_rec_len = EXT2_DIR_REC_LEN(ext2fs_dirent_name_len(dirent));
114 if (curr_rec_len < (min_rec_len + rec_len))
116 rec_len = curr_rec_len - min_rec_len;
117 ls->err = ext2fs_set_rec_len(ls->fs, min_rec_len, dirent);
120 next = (struct ext2_dir_entry *) (buf + offset +
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);
128 return DIRENT_CHANGED;
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.
135 if (curr_rec_len < rec_len)
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);
144 return DIRENT_ABORT|DIRENT_CHANGED;
148 * Note: the low 3 bits of the flags field are used as the directory
154 errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name,
155 ext2_ino_t ino, int flags)
158 struct link_struct ls;
159 struct ext2_inode inode;
161 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
163 if (!(fs->flags & EXT2_FLAG_RW))
164 return EXT2_ET_RO_FILSYS;
168 ls.namelen = name ? strlen(name) : 0;
173 ls.blocksize = fs->blocksize;
176 retval = ext2fs_dir_iterate(fs, dir, DIRENT_FLAG_INCLUDE_EMPTY,
184 return EXT2_ET_DIR_NO_SPACE;
186 if ((retval = ext2fs_read_inode(fs, dir, &inode)) != 0)
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.
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)