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