1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/minix/namei.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
10 static int add_nondir(struct dentry *dentry, struct inode *inode)
12 int err = minix_add_link(dentry, inode);
14 d_instantiate(dentry, inode);
17 inode_dec_link_count(inode);
22 static struct dentry *minix_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
24 struct inode * inode = NULL;
27 if (dentry->d_name.len > minix_sb(dir->i_sb)->s_namelen)
28 return ERR_PTR(-ENAMETOOLONG);
30 ino = minix_inode_by_name(dentry);
32 inode = minix_iget(dir->i_sb, ino);
33 return d_splice_alias(inode, dentry);
36 static int minix_mknod(struct user_namespace *mnt_userns, struct inode *dir,
37 struct dentry *dentry, umode_t mode, dev_t rdev)
42 if (!old_valid_dev(rdev))
45 inode = minix_new_inode(dir, mode, &error);
48 minix_set_inode(inode, rdev);
49 mark_inode_dirty(inode);
50 error = add_nondir(dentry, inode);
55 static int minix_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
56 struct dentry *dentry, umode_t mode)
59 struct inode *inode = minix_new_inode(dir, mode, &error);
61 minix_set_inode(inode, 0);
62 mark_inode_dirty(inode);
63 d_tmpfile(dentry, inode);
68 static int minix_create(struct user_namespace *mnt_userns, struct inode *dir,
69 struct dentry *dentry, umode_t mode, bool excl)
71 return minix_mknod(mnt_userns, dir, dentry, mode, 0);
74 static int minix_symlink(struct user_namespace *mnt_userns, struct inode *dir,
75 struct dentry *dentry, const char *symname)
77 int err = -ENAMETOOLONG;
78 int i = strlen(symname)+1;
81 if (i > dir->i_sb->s_blocksize)
84 inode = minix_new_inode(dir, S_IFLNK | 0777, &err);
88 minix_set_inode(inode, 0);
89 err = page_symlink(inode, symname, i);
93 err = add_nondir(dentry, inode);
98 inode_dec_link_count(inode);
103 static int minix_link(struct dentry * old_dentry, struct inode * dir,
104 struct dentry *dentry)
106 struct inode *inode = d_inode(old_dentry);
108 inode->i_ctime = current_time(inode);
109 inode_inc_link_count(inode);
111 return add_nondir(dentry, inode);
114 static int minix_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
115 struct dentry *dentry, umode_t mode)
117 struct inode * inode;
120 inode_inc_link_count(dir);
122 inode = minix_new_inode(dir, S_IFDIR | mode, &err);
126 minix_set_inode(inode, 0);
128 inode_inc_link_count(inode);
130 err = minix_make_empty(inode, dir);
134 err = minix_add_link(dentry, inode);
138 d_instantiate(dentry, inode);
143 inode_dec_link_count(inode);
144 inode_dec_link_count(inode);
147 inode_dec_link_count(dir);
151 static int minix_unlink(struct inode * dir, struct dentry *dentry)
154 struct inode * inode = d_inode(dentry);
156 struct minix_dir_entry * de;
158 de = minix_find_entry(dentry, &page);
162 err = minix_delete_entry(de, page);
166 inode->i_ctime = dir->i_ctime;
167 inode_dec_link_count(inode);
172 static int minix_rmdir(struct inode * dir, struct dentry *dentry)
174 struct inode * inode = d_inode(dentry);
175 int err = -ENOTEMPTY;
177 if (minix_empty_dir(inode)) {
178 err = minix_unlink(dir, dentry);
180 inode_dec_link_count(dir);
181 inode_dec_link_count(inode);
187 static int minix_rename(struct user_namespace *mnt_userns,
188 struct inode *old_dir, struct dentry *old_dentry,
189 struct inode *new_dir, struct dentry *new_dentry,
192 struct inode * old_inode = d_inode(old_dentry);
193 struct inode * new_inode = d_inode(new_dentry);
194 struct page * dir_page = NULL;
195 struct minix_dir_entry * dir_de = NULL;
196 struct page * old_page;
197 struct minix_dir_entry * old_de;
200 if (flags & ~RENAME_NOREPLACE)
203 old_de = minix_find_entry(old_dentry, &old_page);
207 if (S_ISDIR(old_inode->i_mode)) {
209 dir_de = minix_dotdot(old_inode, &dir_page);
215 struct page * new_page;
216 struct minix_dir_entry * new_de;
219 if (dir_de && !minix_empty_dir(new_inode))
223 new_de = minix_find_entry(new_dentry, &new_page);
226 minix_set_link(new_de, new_page, old_inode);
227 new_inode->i_ctime = current_time(new_inode);
229 drop_nlink(new_inode);
230 inode_dec_link_count(new_inode);
232 err = minix_add_link(new_dentry, old_inode);
236 inode_inc_link_count(new_dir);
239 minix_delete_entry(old_de, old_page);
240 mark_inode_dirty(old_inode);
243 minix_set_link(dir_de, dir_page, new_dir);
244 inode_dec_link_count(old_dir);
261 * directories can handle most operations...
263 const struct inode_operations minix_dir_inode_operations = {
264 .create = minix_create,
265 .lookup = minix_lookup,
267 .unlink = minix_unlink,
268 .symlink = minix_symlink,
269 .mkdir = minix_mkdir,
270 .rmdir = minix_rmdir,
271 .mknod = minix_mknod,
272 .rename = minix_rename,
273 .getattr = minix_getattr,
274 .tmpfile = minix_tmpfile,