xfs: fix type usage
[linux/fpc-iii.git] / fs / sysv / namei.c
blob250b0755b9085378997767740afffafbfcbbe174
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);
55 if (ino) {
56 inode = sysv_iget(dir->i_sb, ino);
57 if (IS_ERR(inode))
58 return ERR_CAST(inode);
60 d_add(dentry, inode);
61 return NULL;
64 static int sysv_mknod(struct inode * dir, struct dentry * dentry, umode_t mode, dev_t rdev)
66 struct inode * inode;
67 int err;
69 if (!old_valid_dev(rdev))
70 return -EINVAL;
72 inode = sysv_new_inode(dir, mode);
73 err = PTR_ERR(inode);
75 if (!IS_ERR(inode)) {
76 sysv_set_inode(inode, rdev);
77 mark_inode_dirty(inode);
78 err = add_nondir(dentry, inode);
80 return err;
83 static int sysv_create(struct inode * dir, struct dentry * dentry, umode_t mode, bool excl)
85 return sysv_mknod(dir, dentry, mode, 0);
88 static int sysv_symlink(struct inode * dir, struct dentry * dentry,
89 const char * symname)
91 int err = -ENAMETOOLONG;
92 int l = strlen(symname)+1;
93 struct inode * inode;
95 if (l > dir->i_sb->s_blocksize)
96 goto out;
98 inode = sysv_new_inode(dir, S_IFLNK|0777);
99 err = PTR_ERR(inode);
100 if (IS_ERR(inode))
101 goto out;
103 sysv_set_inode(inode, 0);
104 err = page_symlink(inode, symname, l);
105 if (err)
106 goto out_fail;
108 mark_inode_dirty(inode);
109 err = add_nondir(dentry, inode);
110 out:
111 return err;
113 out_fail:
114 inode_dec_link_count(inode);
115 iput(inode);
116 goto out;
119 static int sysv_link(struct dentry * old_dentry, struct inode * dir,
120 struct dentry * dentry)
122 struct inode *inode = d_inode(old_dentry);
124 inode->i_ctime = current_time(inode);
125 inode_inc_link_count(inode);
126 ihold(inode);
128 return add_nondir(dentry, inode);
131 static int sysv_mkdir(struct inode * dir, struct dentry *dentry, umode_t mode)
133 struct inode * inode;
134 int err;
136 inode_inc_link_count(dir);
138 inode = sysv_new_inode(dir, S_IFDIR|mode);
139 err = PTR_ERR(inode);
140 if (IS_ERR(inode))
141 goto out_dir;
143 sysv_set_inode(inode, 0);
145 inode_inc_link_count(inode);
147 err = sysv_make_empty(inode, dir);
148 if (err)
149 goto out_fail;
151 err = sysv_add_link(dentry, inode);
152 if (err)
153 goto out_fail;
155 d_instantiate(dentry, inode);
156 out:
157 return err;
159 out_fail:
160 inode_dec_link_count(inode);
161 inode_dec_link_count(inode);
162 iput(inode);
163 out_dir:
164 inode_dec_link_count(dir);
165 goto out;
168 static int sysv_unlink(struct inode * dir, struct dentry * dentry)
170 struct inode * inode = d_inode(dentry);
171 struct page * page;
172 struct sysv_dir_entry * de;
173 int err = -ENOENT;
175 de = sysv_find_entry(dentry, &page);
176 if (!de)
177 goto out;
179 err = sysv_delete_entry (de, page);
180 if (err)
181 goto out;
183 inode->i_ctime = dir->i_ctime;
184 inode_dec_link_count(inode);
185 out:
186 return err;
189 static int sysv_rmdir(struct inode * dir, struct dentry * dentry)
191 struct inode *inode = d_inode(dentry);
192 int err = -ENOTEMPTY;
194 if (sysv_empty_dir(inode)) {
195 err = sysv_unlink(dir, dentry);
196 if (!err) {
197 inode->i_size = 0;
198 inode_dec_link_count(inode);
199 inode_dec_link_count(dir);
202 return err;
206 * Anybody can rename anything with this: the permission checks are left to the
207 * higher-level routines.
209 static int sysv_rename(struct inode * old_dir, struct dentry * old_dentry,
210 struct inode * new_dir, struct dentry * new_dentry,
211 unsigned int flags)
213 struct inode * old_inode = d_inode(old_dentry);
214 struct inode * new_inode = d_inode(new_dentry);
215 struct page * dir_page = NULL;
216 struct sysv_dir_entry * dir_de = NULL;
217 struct page * old_page;
218 struct sysv_dir_entry * old_de;
219 int err = -ENOENT;
221 if (flags & ~RENAME_NOREPLACE)
222 return -EINVAL;
224 old_de = sysv_find_entry(old_dentry, &old_page);
225 if (!old_de)
226 goto out;
228 if (S_ISDIR(old_inode->i_mode)) {
229 err = -EIO;
230 dir_de = sysv_dotdot(old_inode, &dir_page);
231 if (!dir_de)
232 goto out_old;
235 if (new_inode) {
236 struct page * new_page;
237 struct sysv_dir_entry * new_de;
239 err = -ENOTEMPTY;
240 if (dir_de && !sysv_empty_dir(new_inode))
241 goto out_dir;
243 err = -ENOENT;
244 new_de = sysv_find_entry(new_dentry, &new_page);
245 if (!new_de)
246 goto out_dir;
247 sysv_set_link(new_de, new_page, old_inode);
248 new_inode->i_ctime = current_time(new_inode);
249 if (dir_de)
250 drop_nlink(new_inode);
251 inode_dec_link_count(new_inode);
252 } else {
253 err = sysv_add_link(new_dentry, old_inode);
254 if (err)
255 goto out_dir;
256 if (dir_de)
257 inode_inc_link_count(new_dir);
260 sysv_delete_entry(old_de, old_page);
261 mark_inode_dirty(old_inode);
263 if (dir_de) {
264 sysv_set_link(dir_de, dir_page, new_dir);
265 inode_dec_link_count(old_dir);
267 return 0;
269 out_dir:
270 if (dir_de) {
271 kunmap(dir_page);
272 put_page(dir_page);
274 out_old:
275 kunmap(old_page);
276 put_page(old_page);
277 out:
278 return err;
282 * directories can handle most operations...
284 const struct inode_operations sysv_dir_inode_operations = {
285 .create = sysv_create,
286 .lookup = sysv_lookup,
287 .link = sysv_link,
288 .unlink = sysv_unlink,
289 .symlink = sysv_symlink,
290 .mkdir = sysv_mkdir,
291 .rmdir = sysv_rmdir,
292 .mknod = sysv_mknod,
293 .rename = sysv_rename,
294 .getattr = sysv_getattr,