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 inode
* dir
, struct dentry
* dentry
, umode_t mode
, dev_t rdev
)
49 if (!old_valid_dev(rdev
))
52 inode
= sysv_new_inode(dir
, mode
);
56 sysv_set_inode(inode
, rdev
);
57 mark_inode_dirty(inode
);
58 err
= add_nondir(dentry
, inode
);
63 static int sysv_create(struct inode
* dir
, struct dentry
* dentry
, umode_t mode
, bool excl
)
65 return sysv_mknod(dir
, dentry
, mode
, 0);
68 static int sysv_symlink(struct inode
* dir
, struct dentry
* dentry
,
71 int err
= -ENAMETOOLONG
;
72 int l
= strlen(symname
)+1;
75 if (l
> dir
->i_sb
->s_blocksize
)
78 inode
= sysv_new_inode(dir
, S_IFLNK
|0777);
83 sysv_set_inode(inode
, 0);
84 err
= page_symlink(inode
, symname
, l
);
88 mark_inode_dirty(inode
);
89 err
= add_nondir(dentry
, inode
);
94 inode_dec_link_count(inode
);
99 static int sysv_link(struct dentry
* old_dentry
, struct inode
* dir
,
100 struct dentry
* dentry
)
102 struct inode
*inode
= d_inode(old_dentry
);
104 inode
->i_ctime
= current_time(inode
);
105 inode_inc_link_count(inode
);
108 return add_nondir(dentry
, inode
);
111 static int sysv_mkdir(struct inode
* dir
, struct dentry
*dentry
, umode_t mode
)
113 struct inode
* inode
;
116 inode_inc_link_count(dir
);
118 inode
= sysv_new_inode(dir
, S_IFDIR
|mode
);
119 err
= PTR_ERR(inode
);
123 sysv_set_inode(inode
, 0);
125 inode_inc_link_count(inode
);
127 err
= sysv_make_empty(inode
, dir
);
131 err
= sysv_add_link(dentry
, inode
);
135 d_instantiate(dentry
, inode
);
140 inode_dec_link_count(inode
);
141 inode_dec_link_count(inode
);
144 inode_dec_link_count(dir
);
148 static int sysv_unlink(struct inode
* dir
, struct dentry
* dentry
)
150 struct inode
* inode
= d_inode(dentry
);
152 struct sysv_dir_entry
* de
;
155 de
= sysv_find_entry(dentry
, &page
);
159 err
= sysv_delete_entry (de
, page
);
163 inode
->i_ctime
= dir
->i_ctime
;
164 inode_dec_link_count(inode
);
169 static int sysv_rmdir(struct inode
* dir
, struct dentry
* dentry
)
171 struct inode
*inode
= d_inode(dentry
);
172 int err
= -ENOTEMPTY
;
174 if (sysv_empty_dir(inode
)) {
175 err
= sysv_unlink(dir
, dentry
);
178 inode_dec_link_count(inode
);
179 inode_dec_link_count(dir
);
186 * Anybody can rename anything with this: the permission checks are left to the
187 * higher-level routines.
189 static int sysv_rename(struct inode
* old_dir
, struct dentry
* old_dentry
,
190 struct inode
* new_dir
, struct dentry
* new_dentry
,
193 struct inode
* old_inode
= d_inode(old_dentry
);
194 struct inode
* new_inode
= d_inode(new_dentry
);
195 struct page
* dir_page
= NULL
;
196 struct sysv_dir_entry
* dir_de
= NULL
;
197 struct page
* old_page
;
198 struct sysv_dir_entry
* old_de
;
201 if (flags
& ~RENAME_NOREPLACE
)
204 old_de
= sysv_find_entry(old_dentry
, &old_page
);
208 if (S_ISDIR(old_inode
->i_mode
)) {
210 dir_de
= sysv_dotdot(old_inode
, &dir_page
);
216 struct page
* new_page
;
217 struct sysv_dir_entry
* new_de
;
220 if (dir_de
&& !sysv_empty_dir(new_inode
))
224 new_de
= sysv_find_entry(new_dentry
, &new_page
);
227 sysv_set_link(new_de
, new_page
, old_inode
);
228 new_inode
->i_ctime
= current_time(new_inode
);
230 drop_nlink(new_inode
);
231 inode_dec_link_count(new_inode
);
233 err
= sysv_add_link(new_dentry
, old_inode
);
237 inode_inc_link_count(new_dir
);
240 sysv_delete_entry(old_de
, old_page
);
241 mark_inode_dirty(old_inode
);
244 sysv_set_link(dir_de
, dir_page
, new_dir
);
245 inode_dec_link_count(old_dir
);
262 * directories can handle most operations...
264 const struct inode_operations sysv_dir_inode_operations
= {
265 .create
= sysv_create
,
266 .lookup
= sysv_lookup
,
268 .unlink
= sysv_unlink
,
269 .symlink
= sysv_symlink
,
273 .rename
= sysv_rename
,
274 .getattr
= sysv_getattr
,