1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/sysv/namei.c
6 * Copyright (C) 1991, 1992 Linus Torvalds
9 * Copyright (C) 1993 Pascal Haible, Bruno Haible
12 * Copyright (C) 1993 Bruno Haible
13 * Copyright (C) 1997, 1998 Krzysztof G. Baranowski
16 #include <linux/pagemap.h>
19 static int add_nondir(struct dentry *dentry, struct inode *inode)
21 int err = sysv_add_link(dentry, inode);
23 d_instantiate(dentry, inode);
26 inode_dec_link_count(inode);
31 static struct dentry *sysv_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
33 struct inode * inode = NULL;
36 if (dentry->d_name.len > SYSV_NAMELEN)
37 return ERR_PTR(-ENAMETOOLONG);
38 ino = sysv_inode_by_name(dentry);
40 inode = sysv_iget(dir->i_sb, ino);
41 return d_splice_alias(inode, dentry);
44 static int sysv_mknod(struct user_namespace *mnt_userns, struct inode *dir,
45 struct dentry *dentry, umode_t mode, dev_t rdev)
50 if (!old_valid_dev(rdev))
53 inode = sysv_new_inode(dir, mode);
57 sysv_set_inode(inode, rdev);
58 mark_inode_dirty(inode);
59 err = add_nondir(dentry, inode);
64 static int sysv_create(struct user_namespace *mnt_userns, struct inode *dir,
65 struct dentry *dentry, umode_t mode, bool excl)
67 return sysv_mknod(&init_user_ns, dir, dentry, mode, 0);
70 static int sysv_symlink(struct user_namespace *mnt_userns, struct inode *dir,
71 struct dentry *dentry, const char *symname)
73 int err = -ENAMETOOLONG;
74 int l = strlen(symname)+1;
77 if (l > dir->i_sb->s_blocksize)
80 inode = sysv_new_inode(dir, S_IFLNK|0777);
85 sysv_set_inode(inode, 0);
86 err = page_symlink(inode, symname, l);
90 mark_inode_dirty(inode);
91 err = add_nondir(dentry, inode);
96 inode_dec_link_count(inode);
101 static int sysv_link(struct dentry * old_dentry, struct inode * dir,
102 struct dentry * dentry)
104 struct inode *inode = d_inode(old_dentry);
106 inode->i_ctime = current_time(inode);
107 inode_inc_link_count(inode);
110 return add_nondir(dentry, inode);
113 static int sysv_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
114 struct dentry *dentry, umode_t mode)
116 struct inode * inode;
119 inode_inc_link_count(dir);
121 inode = sysv_new_inode(dir, S_IFDIR|mode);
122 err = PTR_ERR(inode);
126 sysv_set_inode(inode, 0);
128 inode_inc_link_count(inode);
130 err = sysv_make_empty(inode, dir);
134 err = sysv_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 sysv_unlink(struct inode * dir, struct dentry * dentry)
153 struct inode * inode = d_inode(dentry);
155 struct sysv_dir_entry * de;
158 de = sysv_find_entry(dentry, &page);
162 err = sysv_delete_entry (de, page);
166 inode->i_ctime = dir->i_ctime;
167 inode_dec_link_count(inode);
172 static int sysv_rmdir(struct inode * dir, struct dentry * dentry)
174 struct inode *inode = d_inode(dentry);
175 int err = -ENOTEMPTY;
177 if (sysv_empty_dir(inode)) {
178 err = sysv_unlink(dir, dentry);
181 inode_dec_link_count(inode);
182 inode_dec_link_count(dir);
189 * Anybody can rename anything with this: the permission checks are left to the
190 * higher-level routines.
192 static int sysv_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
193 struct dentry *old_dentry, struct inode *new_dir,
194 struct dentry *new_dentry, unsigned int flags)
196 struct inode * old_inode = d_inode(old_dentry);
197 struct inode * new_inode = d_inode(new_dentry);
198 struct page * dir_page = NULL;
199 struct sysv_dir_entry * dir_de = NULL;
200 struct page * old_page;
201 struct sysv_dir_entry * old_de;
204 if (flags & ~RENAME_NOREPLACE)
207 old_de = sysv_find_entry(old_dentry, &old_page);
211 if (S_ISDIR(old_inode->i_mode)) {
213 dir_de = sysv_dotdot(old_inode, &dir_page);
219 struct page * new_page;
220 struct sysv_dir_entry * new_de;
223 if (dir_de && !sysv_empty_dir(new_inode))
227 new_de = sysv_find_entry(new_dentry, &new_page);
230 sysv_set_link(new_de, new_page, old_inode);
231 new_inode->i_ctime = current_time(new_inode);
233 drop_nlink(new_inode);
234 inode_dec_link_count(new_inode);
236 err = sysv_add_link(new_dentry, old_inode);
240 inode_inc_link_count(new_dir);
243 sysv_delete_entry(old_de, old_page);
244 mark_inode_dirty(old_inode);
247 sysv_set_link(dir_de, dir_page, new_dir);
248 inode_dec_link_count(old_dir);
265 * directories can handle most operations...
267 const struct inode_operations sysv_dir_inode_operations = {
268 .create = sysv_create,
269 .lookup = sysv_lookup,
271 .unlink = sysv_unlink,
272 .symlink = sysv_symlink,
276 .rename = sysv_rename,
277 .getattr = sysv_getattr,