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 mnt_idmap *idmap, 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 mnt_idmap *idmap, struct inode *dir,
65 struct dentry *dentry, umode_t mode, bool excl)
67 return sysv_mknod(&nop_mnt_idmap, dir, dentry, mode, 0);
70 static int sysv_symlink(struct mnt_idmap *idmap, 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_set_ctime_current(inode);
107 inode_inc_link_count(inode);
110 return add_nondir(dentry, inode);
113 static int sysv_mkdir(struct mnt_idmap *idmap, 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);
164 inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
165 inode_dec_link_count(inode);
167 unmap_and_put_page(page, de);
171 static int sysv_rmdir(struct inode * dir, struct dentry * dentry)
173 struct inode *inode = d_inode(dentry);
174 int err = -ENOTEMPTY;
176 if (sysv_empty_dir(inode)) {
177 err = sysv_unlink(dir, dentry);
180 inode_dec_link_count(inode);
181 inode_dec_link_count(dir);
188 * Anybody can rename anything with this: the permission checks are left to the
189 * higher-level routines.
191 static int sysv_rename(struct mnt_idmap *idmap, struct inode *old_dir,
192 struct dentry *old_dentry, struct inode *new_dir,
193 struct dentry *new_dentry, unsigned int flags)
195 struct inode * old_inode = d_inode(old_dentry);
196 struct inode * new_inode = d_inode(new_dentry);
197 struct page * dir_page = NULL;
198 struct sysv_dir_entry * dir_de = NULL;
199 struct page * old_page;
200 struct sysv_dir_entry * old_de;
203 if (flags & ~RENAME_NOREPLACE)
206 old_de = sysv_find_entry(old_dentry, &old_page);
210 if (S_ISDIR(old_inode->i_mode)) {
212 dir_de = sysv_dotdot(old_inode, &dir_page);
218 struct page * new_page;
219 struct sysv_dir_entry * new_de;
222 if (dir_de && !sysv_empty_dir(new_inode))
226 new_de = sysv_find_entry(new_dentry, &new_page);
229 err = sysv_set_link(new_de, new_page, old_inode);
230 unmap_and_put_page(new_page, new_de);
233 inode_set_ctime_current(new_inode);
235 drop_nlink(new_inode);
236 inode_dec_link_count(new_inode);
238 err = sysv_add_link(new_dentry, old_inode);
242 inode_inc_link_count(new_dir);
245 err = sysv_delete_entry(old_de, old_page);
249 mark_inode_dirty(old_inode);
252 err = sysv_set_link(dir_de, dir_page, new_dir);
254 inode_dec_link_count(old_dir);
259 unmap_and_put_page(dir_page, dir_de);
261 unmap_and_put_page(old_page, old_de);
267 * directories can handle most operations...
269 const struct inode_operations sysv_dir_inode_operations = {
270 .create = sysv_create,
271 .lookup = sysv_lookup,
273 .unlink = sysv_unlink,
274 .symlink = sysv_symlink,
278 .rename = sysv_rename,
279 .getattr = sysv_getattr,