1/*
2 * linux/fs/ext2/namei.c
3 *
4 * Rewrite to pagecache. Almost all code had been changed, so blame me
5 * if the things go wrong. Please, send bug reports to
6 * viro@parcelfarce.linux.theplanet.co.uk
7 *
8 * Stuff here is basically a glue between the VFS and generic UNIXish
9 * filesystem that keeps everything in pagecache. All knowledge of the
10 * directory layout is in fs/ext2/dir.c - it turned out to be easily separatable
11 * and it's easier to debug that way. In principle we might want to
12 * generalize that a bit and turn it into a library. Or not.
13 *
14 * The only non-static object here is ext2_dir_inode_operations.
15 *
16 * TODO: get rid of kmap() use, add readahead.
17 *
18 * Copyright (C) 1992, 1993, 1994, 1995
19 * Remy Card (card@masi.ibp.fr)
20 * Laboratoire MASI - Institut Blaise Pascal
21 * Universite Pierre et Marie Curie (Paris VI)
22 *
23 *  from
24 *
25 *  linux/fs/minix/namei.c
26 *
27 *  Copyright (C) 1991, 1992  Linus Torvalds
28 *
29 *  Big-endian to little-endian byte-swapping/bitmaps by
30 *        David S. Miller (davem@caip.rutgers.edu), 1995
31 */
32
33#include <linux/pagemap.h>
34#include <linux/quotaops.h>
35#include "ext2.h"
36#include "xattr.h"
37#include "acl.h"
38
39static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
40{
41	int err = ext2_add_link(dentry, inode);
42	if (!err) {
43		unlock_new_inode(inode);
44		d_instantiate(dentry, inode);
45		return 0;
46	}
47	inode_dec_link_count(inode);
48	unlock_new_inode(inode);
49	iput(inode);
50	return err;
51}
52
53/*
54 * Methods themselves.
55 */
56
57static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
58{
59	struct inode * inode;
60	ino_t ino;
61
62	if (dentry->d_name.len > EXT2_NAME_LEN)
63		return ERR_PTR(-ENAMETOOLONG);
64
65	ino = ext2_inode_by_name(dir, &dentry->d_name);
66	inode = NULL;
67	if (ino) {
68		inode = ext2_iget(dir->i_sb, ino);
69		if (inode == ERR_PTR(-ESTALE)) {
70			ext2_error(dir->i_sb, __func__,
71					"deleted inode referenced: %lu",
72					(unsigned long) ino);
73			return ERR_PTR(-EIO);
74		}
75	}
76	return d_splice_alias(inode, dentry);
77}
78
79struct dentry *ext2_get_parent(struct dentry *child)
80{
81	struct qstr dotdot = QSTR_INIT("..", 2);
82	unsigned long ino = ext2_inode_by_name(d_inode(child), &dotdot);
83	if (!ino)
84		return ERR_PTR(-ENOENT);
85	return d_obtain_alias(ext2_iget(d_inode(child)->i_sb, ino));
86}
87
88/*
89 * By the time this is called, we already have created
90 * the directory cache entry for the new file, but it
91 * is so far negative - it has no inode.
92 *
93 * If the create succeeds, we fill in the inode information
94 * with d_instantiate().
95 */
96static int ext2_create (struct inode * dir, struct dentry * dentry, umode_t mode, bool excl)
97{
98	struct inode *inode;
99
100	dquot_initialize(dir);
101
102	inode = ext2_new_inode(dir, mode, &dentry->d_name);
103	if (IS_ERR(inode))
104		return PTR_ERR(inode);
105
106	inode->i_op = &ext2_file_inode_operations;
107	if (test_opt(inode->i_sb, NOBH)) {
108		inode->i_mapping->a_ops = &ext2_nobh_aops;
109		inode->i_fop = &ext2_file_operations;
110	} else {
111		inode->i_mapping->a_ops = &ext2_aops;
112		inode->i_fop = &ext2_file_operations;
113	}
114	mark_inode_dirty(inode);
115	return ext2_add_nondir(dentry, inode);
116}
117
118static int ext2_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
119{
120	struct inode *inode = ext2_new_inode(dir, mode, NULL);
121	if (IS_ERR(inode))
122		return PTR_ERR(inode);
123
124	inode->i_op = &ext2_file_inode_operations;
125	if (test_opt(inode->i_sb, NOBH)) {
126		inode->i_mapping->a_ops = &ext2_nobh_aops;
127		inode->i_fop = &ext2_file_operations;
128	} else {
129		inode->i_mapping->a_ops = &ext2_aops;
130		inode->i_fop = &ext2_file_operations;
131	}
132	mark_inode_dirty(inode);
133	d_tmpfile(dentry, inode);
134	unlock_new_inode(inode);
135	return 0;
136}
137
138static int ext2_mknod (struct inode * dir, struct dentry *dentry, umode_t mode, dev_t rdev)
139{
140	struct inode * inode;
141	int err;
142
143	if (!new_valid_dev(rdev))
144		return -EINVAL;
145
146	dquot_initialize(dir);
147
148	inode = ext2_new_inode (dir, mode, &dentry->d_name);
149	err = PTR_ERR(inode);
150	if (!IS_ERR(inode)) {
151		init_special_inode(inode, inode->i_mode, rdev);
152#ifdef CONFIG_EXT2_FS_XATTR
153		inode->i_op = &ext2_special_inode_operations;
154#endif
155		mark_inode_dirty(inode);
156		err = ext2_add_nondir(dentry, inode);
157	}
158	return err;
159}
160
161static int ext2_symlink (struct inode * dir, struct dentry * dentry,
162	const char * symname)
163{
164	struct super_block * sb = dir->i_sb;
165	int err = -ENAMETOOLONG;
166	unsigned l = strlen(symname)+1;
167	struct inode * inode;
168
169	if (l > sb->s_blocksize)
170		goto out;
171
172	dquot_initialize(dir);
173
174	inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO, &dentry->d_name);
175	err = PTR_ERR(inode);
176	if (IS_ERR(inode))
177		goto out;
178
179	if (l > sizeof (EXT2_I(inode)->i_data)) {
180		/* slow symlink */
181		inode->i_op = &ext2_symlink_inode_operations;
182		if (test_opt(inode->i_sb, NOBH))
183			inode->i_mapping->a_ops = &ext2_nobh_aops;
184		else
185			inode->i_mapping->a_ops = &ext2_aops;
186		err = page_symlink(inode, symname, l);
187		if (err)
188			goto out_fail;
189	} else {
190		/* fast symlink */
191		inode->i_op = &ext2_fast_symlink_inode_operations;
192		memcpy((char*)(EXT2_I(inode)->i_data),symname,l);
193		inode->i_size = l-1;
194	}
195	mark_inode_dirty(inode);
196
197	err = ext2_add_nondir(dentry, inode);
198out:
199	return err;
200
201out_fail:
202	inode_dec_link_count(inode);
203	unlock_new_inode(inode);
204	iput (inode);
205	goto out;
206}
207
208static int ext2_link (struct dentry * old_dentry, struct inode * dir,
209	struct dentry *dentry)
210{
211	struct inode *inode = d_inode(old_dentry);
212	int err;
213
214	dquot_initialize(dir);
215
216	inode->i_ctime = CURRENT_TIME_SEC;
217	inode_inc_link_count(inode);
218	ihold(inode);
219
220	err = ext2_add_link(dentry, inode);
221	if (!err) {
222		d_instantiate(dentry, inode);
223		return 0;
224	}
225	inode_dec_link_count(inode);
226	iput(inode);
227	return err;
228}
229
230static int ext2_mkdir(struct inode * dir, struct dentry * dentry, umode_t mode)
231{
232	struct inode * inode;
233	int err;
234
235	dquot_initialize(dir);
236
237	inode_inc_link_count(dir);
238
239	inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name);
240	err = PTR_ERR(inode);
241	if (IS_ERR(inode))
242		goto out_dir;
243
244	inode->i_op = &ext2_dir_inode_operations;
245	inode->i_fop = &ext2_dir_operations;
246	if (test_opt(inode->i_sb, NOBH))
247		inode->i_mapping->a_ops = &ext2_nobh_aops;
248	else
249		inode->i_mapping->a_ops = &ext2_aops;
250
251	inode_inc_link_count(inode);
252
253	err = ext2_make_empty(inode, dir);
254	if (err)
255		goto out_fail;
256
257	err = ext2_add_link(dentry, inode);
258	if (err)
259		goto out_fail;
260
261	unlock_new_inode(inode);
262	d_instantiate(dentry, inode);
263out:
264	return err;
265
266out_fail:
267	inode_dec_link_count(inode);
268	inode_dec_link_count(inode);
269	unlock_new_inode(inode);
270	iput(inode);
271out_dir:
272	inode_dec_link_count(dir);
273	goto out;
274}
275
276static int ext2_unlink(struct inode * dir, struct dentry *dentry)
277{
278	struct inode * inode = d_inode(dentry);
279	struct ext2_dir_entry_2 * de;
280	struct page * page;
281	int err = -ENOENT;
282
283	dquot_initialize(dir);
284
285	de = ext2_find_entry (dir, &dentry->d_name, &page);
286	if (!de)
287		goto out;
288
289	err = ext2_delete_entry (de, page);
290	if (err)
291		goto out;
292
293	inode->i_ctime = dir->i_ctime;
294	inode_dec_link_count(inode);
295	err = 0;
296out:
297	return err;
298}
299
300static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
301{
302	struct inode * inode = d_inode(dentry);
303	int err = -ENOTEMPTY;
304
305	if (ext2_empty_dir(inode)) {
306		err = ext2_unlink(dir, dentry);
307		if (!err) {
308			inode->i_size = 0;
309			inode_dec_link_count(inode);
310			inode_dec_link_count(dir);
311		}
312	}
313	return err;
314}
315
316static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry,
317	struct inode * new_dir,	struct dentry * new_dentry )
318{
319	struct inode * old_inode = d_inode(old_dentry);
320	struct inode * new_inode = d_inode(new_dentry);
321	struct page * dir_page = NULL;
322	struct ext2_dir_entry_2 * dir_de = NULL;
323	struct page * old_page;
324	struct ext2_dir_entry_2 * old_de;
325	int err = -ENOENT;
326
327	dquot_initialize(old_dir);
328	dquot_initialize(new_dir);
329
330	old_de = ext2_find_entry (old_dir, &old_dentry->d_name, &old_page);
331	if (!old_de)
332		goto out;
333
334	if (S_ISDIR(old_inode->i_mode)) {
335		err = -EIO;
336		dir_de = ext2_dotdot(old_inode, &dir_page);
337		if (!dir_de)
338			goto out_old;
339	}
340
341	if (new_inode) {
342		struct page *new_page;
343		struct ext2_dir_entry_2 *new_de;
344
345		err = -ENOTEMPTY;
346		if (dir_de && !ext2_empty_dir (new_inode))
347			goto out_dir;
348
349		err = -ENOENT;
350		new_de = ext2_find_entry (new_dir, &new_dentry->d_name, &new_page);
351		if (!new_de)
352			goto out_dir;
353		ext2_set_link(new_dir, new_de, new_page, old_inode, 1);
354		new_inode->i_ctime = CURRENT_TIME_SEC;
355		if (dir_de)
356			drop_nlink(new_inode);
357		inode_dec_link_count(new_inode);
358	} else {
359		err = ext2_add_link(new_dentry, old_inode);
360		if (err)
361			goto out_dir;
362		if (dir_de)
363			inode_inc_link_count(new_dir);
364	}
365
366	/*
367	 * Like most other Unix systems, set the ctime for inodes on a
368 	 * rename.
369	 */
370	old_inode->i_ctime = CURRENT_TIME_SEC;
371	mark_inode_dirty(old_inode);
372
373	ext2_delete_entry (old_de, old_page);
374
375	if (dir_de) {
376		if (old_dir != new_dir)
377			ext2_set_link(old_inode, dir_de, dir_page, new_dir, 0);
378		else {
379			kunmap(dir_page);
380			page_cache_release(dir_page);
381		}
382		inode_dec_link_count(old_dir);
383	}
384	return 0;
385
386
387out_dir:
388	if (dir_de) {
389		kunmap(dir_page);
390		page_cache_release(dir_page);
391	}
392out_old:
393	kunmap(old_page);
394	page_cache_release(old_page);
395out:
396	return err;
397}
398
399const struct inode_operations ext2_dir_inode_operations = {
400	.create		= ext2_create,
401	.lookup		= ext2_lookup,
402	.link		= ext2_link,
403	.unlink		= ext2_unlink,
404	.symlink	= ext2_symlink,
405	.mkdir		= ext2_mkdir,
406	.rmdir		= ext2_rmdir,
407	.mknod		= ext2_mknod,
408	.rename		= ext2_rename,
409#ifdef CONFIG_EXT2_FS_XATTR
410	.setxattr	= generic_setxattr,
411	.getxattr	= generic_getxattr,
412	.listxattr	= ext2_listxattr,
413	.removexattr	= generic_removexattr,
414#endif
415	.setattr	= ext2_setattr,
416	.get_acl	= ext2_get_acl,
417	.set_acl	= ext2_set_acl,
418	.tmpfile	= ext2_tmpfile,
419};
420
421const struct inode_operations ext2_special_inode_operations = {
422#ifdef CONFIG_EXT2_FS_XATTR
423	.setxattr	= generic_setxattr,
424	.getxattr	= generic_getxattr,
425	.listxattr	= ext2_listxattr,
426	.removexattr	= generic_removexattr,
427#endif
428	.setattr	= ext2_setattr,
429	.get_acl	= ext2_get_acl,
430	.set_acl	= ext2_set_acl,
431};
432