perf beauty: Switch from GPL v2.0 to LGPL v2.1
[linux/fpc-iii.git] / fs / sysv / namei.c
blob4d5d20491ffd89a0c034a537d87d57b428a416cc
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/sysv/namei.c
5 * minix/namei.c
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * coh/namei.c
9 * Copyright (C) 1993 Pascal Haible, Bruno Haible
11 * sysv/namei.c
12 * Copyright (C) 1993 Bruno Haible
13 * Copyright (C) 1997, 1998 Krzysztof G. Baranowski
16 #include <linux/pagemap.h>
17 #include "sysv.h"
19 static int add_nondir(struct dentry *dentry, struct inode *inode)
21 int err = sysv_add_link(dentry, inode);
22 if (!err) {
23 d_instantiate(dentry, inode);
24 return 0;
26 inode_dec_link_count(inode);
27 iput(inode);
28 return err;
31 static int sysv_hash(const struct dentry *dentry, struct qstr *qstr)
33 /* Truncate the name in place, avoids having to define a compare
34 function. */
35 if (qstr->len > SYSV_NAMELEN) {
36 qstr->len = SYSV_NAMELEN;
37 qstr->hash = full_name_hash(dentry, qstr->name, qstr->len);
39 return 0;
42 const struct dentry_operations sysv_dentry_operations = {
43 .d_hash = sysv_hash,
46 static struct dentry *sysv_lookup(struct inode * dir, struct dentry * dentry, unsigned int flags)
48 struct inode * inode = NULL;
49 ino_t ino;
51 if (dentry->d_name.len > SYSV_NAMELEN)
52 return ERR_PTR(-ENAMETOOLONG);
53 ino = sysv_inode_by_name(dentry);
54 if (ino)
55 inode = sysv_iget(dir->i_sb, ino);
56 return d_splice_alias(inode, dentry);
59 static int sysv_mknod(struct inode * dir, struct dentry * dentry, umode_t mode, dev_t rdev)
61 struct inode * inode;
62 int err;
64 if (!old_valid_dev(rdev))
65 return -EINVAL;
67 inode = sysv_new_inode(dir, mode);
68 err = PTR_ERR(inode);
70 if (!IS_ERR(inode)) {
71 sysv_set_inode(inode, rdev);
72 mark_inode_dirty(inode);
73 err = add_nondir(dentry, inode);
75 return err;
78 static int sysv_create(struct inode * dir, struct dentry * dentry, umode_t mode, bool excl)
80 return sysv_mknod(dir, dentry, mode, 0);
83 static int sysv_symlink(struct inode * dir, struct dentry * dentry,
84 const char * symname)
86 int err = -ENAMETOOLONG;
87 int l = strlen(symname)+1;
88 struct inode * inode;
90 if (l > dir->i_sb->s_blocksize)
91 goto out;
93 inode = sysv_new_inode(dir, S_IFLNK|0777);
94 err = PTR_ERR(inode);
95 if (IS_ERR(inode))
96 goto out;
98 sysv_set_inode(inode, 0);
99 err = page_symlink(inode, symname, l);
100 if (err)
101 goto out_fail;
103 mark_inode_dirty(inode);
104 err = add_nondir(dentry, inode);
105 out:
106 return err;
108 out_fail:
109 inode_dec_link_count(inode);
110 iput(inode);
111 goto out;
114 static int sysv_link(struct dentry * old_dentry, struct inode * dir,
115 struct dentry * dentry)
117 struct inode *inode = d_inode(old_dentry);
119 inode->i_ctime = current_time(inode);
120 inode_inc_link_count(inode);
121 ihold(inode);
123 return add_nondir(dentry, inode);
126 static int sysv_mkdir(struct inode * dir, struct dentry *dentry, umode_t mode)
128 struct inode * inode;
129 int err;
131 inode_inc_link_count(dir);
133 inode = sysv_new_inode(dir, S_IFDIR|mode);
134 err = PTR_ERR(inode);
135 if (IS_ERR(inode))
136 goto out_dir;
138 sysv_set_inode(inode, 0);
140 inode_inc_link_count(inode);
142 err = sysv_make_empty(inode, dir);
143 if (err)
144 goto out_fail;
146 err = sysv_add_link(dentry, inode);
147 if (err)
148 goto out_fail;
150 d_instantiate(dentry, inode);
151 out:
152 return err;
154 out_fail:
155 inode_dec_link_count(inode);
156 inode_dec_link_count(inode);
157 iput(inode);
158 out_dir:
159 inode_dec_link_count(dir);
160 goto out;
163 static int sysv_unlink(struct inode * dir, struct dentry * dentry)
165 struct inode * inode = d_inode(dentry);
166 struct page * page;
167 struct sysv_dir_entry * de;
168 int err = -ENOENT;
170 de = sysv_find_entry(dentry, &page);
171 if (!de)
172 goto out;
174 err = sysv_delete_entry (de, page);
175 if (err)
176 goto out;
178 inode->i_ctime = dir->i_ctime;
179 inode_dec_link_count(inode);
180 out:
181 return err;
184 static int sysv_rmdir(struct inode * dir, struct dentry * dentry)
186 struct inode *inode = d_inode(dentry);
187 int err = -ENOTEMPTY;
189 if (sysv_empty_dir(inode)) {
190 err = sysv_unlink(dir, dentry);
191 if (!err) {
192 inode->i_size = 0;
193 inode_dec_link_count(inode);
194 inode_dec_link_count(dir);
197 return err;
201 * Anybody can rename anything with this: the permission checks are left to the
202 * higher-level routines.
204 static int sysv_rename(struct inode * old_dir, struct dentry * old_dentry,
205 struct inode * new_dir, struct dentry * new_dentry,
206 unsigned int flags)
208 struct inode * old_inode = d_inode(old_dentry);
209 struct inode * new_inode = d_inode(new_dentry);
210 struct page * dir_page = NULL;
211 struct sysv_dir_entry * dir_de = NULL;
212 struct page * old_page;
213 struct sysv_dir_entry * old_de;
214 int err = -ENOENT;
216 if (flags & ~RENAME_NOREPLACE)
217 return -EINVAL;
219 old_de = sysv_find_entry(old_dentry, &old_page);
220 if (!old_de)
221 goto out;
223 if (S_ISDIR(old_inode->i_mode)) {
224 err = -EIO;
225 dir_de = sysv_dotdot(old_inode, &dir_page);
226 if (!dir_de)
227 goto out_old;
230 if (new_inode) {
231 struct page * new_page;
232 struct sysv_dir_entry * new_de;
234 err = -ENOTEMPTY;
235 if (dir_de && !sysv_empty_dir(new_inode))
236 goto out_dir;
238 err = -ENOENT;
239 new_de = sysv_find_entry(new_dentry, &new_page);
240 if (!new_de)
241 goto out_dir;
242 sysv_set_link(new_de, new_page, old_inode);
243 new_inode->i_ctime = current_time(new_inode);
244 if (dir_de)
245 drop_nlink(new_inode);
246 inode_dec_link_count(new_inode);
247 } else {
248 err = sysv_add_link(new_dentry, old_inode);
249 if (err)
250 goto out_dir;
251 if (dir_de)
252 inode_inc_link_count(new_dir);
255 sysv_delete_entry(old_de, old_page);
256 mark_inode_dirty(old_inode);
258 if (dir_de) {
259 sysv_set_link(dir_de, dir_page, new_dir);
260 inode_dec_link_count(old_dir);
262 return 0;
264 out_dir:
265 if (dir_de) {
266 kunmap(dir_page);
267 put_page(dir_page);
269 out_old:
270 kunmap(old_page);
271 put_page(old_page);
272 out:
273 return err;
277 * directories can handle most operations...
279 const struct inode_operations sysv_dir_inode_operations = {
280 .create = sysv_create,
281 .lookup = sysv_lookup,
282 .link = sysv_link,
283 .unlink = sysv_unlink,
284 .symlink = sysv_symlink,
285 .mkdir = sysv_mkdir,
286 .rmdir = sysv_rmdir,
287 .mknod = sysv_mknod,
288 .rename = sysv_rename,
289 .getattr = sysv_getattr,