xref: /linux/fs/ufs/namei.c (revision fcb3ad4366b9c810cbb9da34c076a9a52d8aa1e0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/fs/ufs/namei.c
4  *
5  * Migration to usage of "page cache" on May 2006 by
6  * Evgeniy Dushistov <dushistov@mail.ru> based on ext2 code base.
7  *
8  * Copyright (C) 1998
9  * Daniel Pirkl <daniel.pirkl@email.cz>
10  * Charles University, Faculty of Mathematics and Physics
11  *
12  *  from
13  *
14  *  linux/fs/ext2/namei.c
15  *
16  * Copyright (C) 1992, 1993, 1994, 1995
17  * Remy Card (card@masi.ibp.fr)
18  * Laboratoire MASI - Institut Blaise Pascal
19  * Universite Pierre et Marie Curie (Paris VI)
20  *
21  *  from
22  *
23  *  linux/fs/minix/namei.c
24  *
25  *  Copyright (C) 1991, 1992  Linus Torvalds
26  *
27  *  Big-endian to little-endian byte-swapping/bitmaps by
28  *        David S. Miller (davem@caip.rutgers.edu), 1995
29  */
30 
31 #include <linux/time.h>
32 #include <linux/fs.h>
33 
34 #include "ufs_fs.h"
35 #include "ufs.h"
36 #include "util.h"
37 
38 static inline int ufs_add_nondir(struct dentry *dentry, struct inode *inode)
39 {
40 	int err = ufs_add_link(dentry, inode);
41 	if (!err) {
42 		d_instantiate_new(dentry, inode);
43 		return 0;
44 	}
45 	inode_dec_link_count(inode);
46 	discard_new_inode(inode);
47 	return err;
48 }
49 
50 static struct dentry *ufs_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
51 {
52 	struct inode * inode = NULL;
53 	ino_t ino;
54 
55 	if (dentry->d_name.len > UFS_MAXNAMLEN)
56 		return ERR_PTR(-ENAMETOOLONG);
57 
58 	ino = ufs_inode_by_name(dir, &dentry->d_name);
59 	if (ino)
60 		inode = ufs_iget(dir->i_sb, ino);
61 	return d_splice_alias(inode, dentry);
62 }
63 
64 /*
65  * By the time this is called, we already have created
66  * the directory cache entry for the new file, but it
67  * is so far negative - it has no inode.
68  *
69  * If the create succeeds, we fill in the inode information
70  * with d_instantiate().
71  */
72 static int ufs_create (struct mnt_idmap * idmap,
73 		struct inode * dir, struct dentry * dentry, umode_t mode,
74 		bool excl)
75 {
76 	struct inode *inode;
77 
78 	inode = ufs_new_inode(dir, mode);
79 	if (IS_ERR(inode))
80 		return PTR_ERR(inode);
81 
82 	inode->i_op = &ufs_file_inode_operations;
83 	inode->i_fop = &ufs_file_operations;
84 	inode->i_mapping->a_ops = &ufs_aops;
85 	mark_inode_dirty(inode);
86 	return ufs_add_nondir(dentry, inode);
87 }
88 
89 static int ufs_mknod(struct mnt_idmap *idmap, struct inode *dir,
90 		     struct dentry *dentry, umode_t mode, dev_t rdev)
91 {
92 	struct inode *inode;
93 	int err;
94 
95 	if (!old_valid_dev(rdev))
96 		return -EINVAL;
97 
98 	inode = ufs_new_inode(dir, mode);
99 	err = PTR_ERR(inode);
100 	if (!IS_ERR(inode)) {
101 		init_special_inode(inode, mode, rdev);
102 		ufs_set_inode_dev(inode->i_sb, UFS_I(inode), rdev);
103 		mark_inode_dirty(inode);
104 		err = ufs_add_nondir(dentry, inode);
105 	}
106 	return err;
107 }
108 
109 static int ufs_symlink (struct mnt_idmap * idmap, struct inode * dir,
110 	struct dentry * dentry, const char * symname)
111 {
112 	struct super_block * sb = dir->i_sb;
113 	int err;
114 	unsigned l = strlen(symname)+1;
115 	struct inode * inode;
116 
117 	if (l > sb->s_blocksize)
118 		return -ENAMETOOLONG;
119 
120 	inode = ufs_new_inode(dir, S_IFLNK | S_IRWXUGO);
121 	err = PTR_ERR(inode);
122 	if (IS_ERR(inode))
123 		return err;
124 
125 	if (l > UFS_SB(sb)->s_uspi->s_maxsymlinklen) {
126 		/* slow symlink */
127 		inode->i_op = &page_symlink_inode_operations;
128 		inode_nohighmem(inode);
129 		inode->i_mapping->a_ops = &ufs_aops;
130 		err = page_symlink(inode, symname, l);
131 		if (err)
132 			goto out_fail;
133 	} else {
134 		/* fast symlink */
135 		inode->i_op = &simple_symlink_inode_operations;
136 		inode->i_link = (char *)UFS_I(inode)->i_u1.i_symlink;
137 		memcpy(inode->i_link, symname, l);
138 		inode->i_size = l-1;
139 	}
140 	mark_inode_dirty(inode);
141 
142 	return ufs_add_nondir(dentry, inode);
143 
144 out_fail:
145 	inode_dec_link_count(inode);
146 	discard_new_inode(inode);
147 	return err;
148 }
149 
150 static int ufs_link (struct dentry * old_dentry, struct inode * dir,
151 	struct dentry *dentry)
152 {
153 	struct inode *inode = d_inode(old_dentry);
154 	int error;
155 
156 	inode_set_ctime_current(inode);
157 	inode_inc_link_count(inode);
158 	ihold(inode);
159 
160 	error = ufs_add_link(dentry, inode);
161 	if (error) {
162 		inode_dec_link_count(inode);
163 		iput(inode);
164 	} else
165 		d_instantiate(dentry, inode);
166 	return error;
167 }
168 
169 static int ufs_mkdir(struct mnt_idmap * idmap, struct inode * dir,
170 	struct dentry * dentry, umode_t mode)
171 {
172 	struct inode * inode;
173 	int err;
174 
175 	inode_inc_link_count(dir);
176 
177 	inode = ufs_new_inode(dir, S_IFDIR|mode);
178 	err = PTR_ERR(inode);
179 	if (IS_ERR(inode))
180 		goto out_dir;
181 
182 	inode->i_op = &ufs_dir_inode_operations;
183 	inode->i_fop = &ufs_dir_operations;
184 	inode->i_mapping->a_ops = &ufs_aops;
185 
186 	inode_inc_link_count(inode);
187 
188 	err = ufs_make_empty(inode, dir);
189 	if (err)
190 		goto out_fail;
191 
192 	err = ufs_add_link(dentry, inode);
193 	if (err)
194 		goto out_fail;
195 
196 	d_instantiate_new(dentry, inode);
197 	return 0;
198 
199 out_fail:
200 	inode_dec_link_count(inode);
201 	inode_dec_link_count(inode);
202 	discard_new_inode(inode);
203 out_dir:
204 	inode_dec_link_count(dir);
205 	return err;
206 }
207 
208 static int ufs_unlink(struct inode *dir, struct dentry *dentry)
209 {
210 	struct inode * inode = d_inode(dentry);
211 	struct ufs_dir_entry *de;
212 	struct folio *folio;
213 	int err;
214 
215 	de = ufs_find_entry(dir, &dentry->d_name, &folio);
216 	if (!de)
217 		return -ENOENT;
218 
219 	err = ufs_delete_entry(dir, de, folio);
220 	if (!err) {
221 		inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
222 		inode_dec_link_count(inode);
223 	}
224 	folio_release_kmap(folio, de);
225 	return err;
226 }
227 
228 static int ufs_rmdir (struct inode * dir, struct dentry *dentry)
229 {
230 	struct inode * inode = d_inode(dentry);
231 	int err= -ENOTEMPTY;
232 
233 	if (ufs_empty_dir (inode)) {
234 		err = ufs_unlink(dir, dentry);
235 		if (!err) {
236 			inode->i_size = 0;
237 			inode_dec_link_count(inode);
238 			inode_dec_link_count(dir);
239 		}
240 	}
241 	return err;
242 }
243 
244 static int ufs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
245 		      struct dentry *old_dentry, struct inode *new_dir,
246 		      struct dentry *new_dentry, unsigned int flags)
247 {
248 	struct inode *old_inode = d_inode(old_dentry);
249 	struct inode *new_inode = d_inode(new_dentry);
250 	struct folio *dir_folio = NULL;
251 	struct ufs_dir_entry * dir_de = NULL;
252 	struct folio *old_folio;
253 	struct ufs_dir_entry *old_de;
254 	int err;
255 
256 	if (flags & ~RENAME_NOREPLACE)
257 		return -EINVAL;
258 
259 	old_de = ufs_find_entry(old_dir, &old_dentry->d_name, &old_folio);
260 	if (!old_de)
261 		return -ENOENT;
262 
263 	if (S_ISDIR(old_inode->i_mode)) {
264 		err = -EIO;
265 		dir_de = ufs_dotdot(old_inode, &dir_folio);
266 		if (!dir_de)
267 			goto out_old;
268 	}
269 
270 	if (new_inode) {
271 		struct folio *new_folio;
272 		struct ufs_dir_entry *new_de;
273 
274 		err = -ENOTEMPTY;
275 		if (dir_de && !ufs_empty_dir(new_inode))
276 			goto out_dir;
277 
278 		err = -ENOENT;
279 		new_de = ufs_find_entry(new_dir, &new_dentry->d_name, &new_folio);
280 		if (!new_de)
281 			goto out_dir;
282 		err = ufs_set_link(new_dir, new_de, new_folio, old_inode, 1);
283 		folio_release_kmap(new_folio, new_de);
284 		if (err)
285 			goto out_dir;
286 		inode_set_ctime_current(new_inode);
287 		if (dir_de)
288 			drop_nlink(new_inode);
289 		inode_dec_link_count(new_inode);
290 	} else {
291 		err = ufs_add_link(new_dentry, old_inode);
292 		if (err)
293 			goto out_dir;
294 		if (dir_de)
295 			inode_inc_link_count(new_dir);
296 	}
297 
298 	/*
299 	 * Like most other Unix systems, set the ctime for inodes on a
300  	 * rename.
301 	 */
302 	inode_set_ctime_current(old_inode);
303 	mark_inode_dirty(old_inode);
304 
305 	err = ufs_delete_entry(old_dir, old_de, old_folio);
306 	if (!err && dir_de) {
307 		if (old_dir != new_dir)
308 			err = ufs_set_link(old_inode, dir_de, dir_folio,
309 					   new_dir, 0);
310 		inode_dec_link_count(old_dir);
311 	}
312 out_dir:
313 	if (dir_de)
314 		folio_release_kmap(dir_folio, dir_de);
315 out_old:
316 	folio_release_kmap(old_folio, old_de);
317 	return err;
318 }
319 
320 const struct inode_operations ufs_dir_inode_operations = {
321 	.create		= ufs_create,
322 	.lookup		= ufs_lookup,
323 	.link		= ufs_link,
324 	.unlink		= ufs_unlink,
325 	.symlink	= ufs_symlink,
326 	.mkdir		= ufs_mkdir,
327 	.rmdir		= ufs_rmdir,
328 	.mknod		= ufs_mknod,
329 	.rename		= ufs_rename,
330 };
331