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