xref: /openbmc/linux/fs/ext2/namei.c (revision e18275ae55e07a2937e48134589c2f4c1d99a369)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/fs/ext2/namei.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Rewrite to pagecache. Almost all code had been changed, so blame me
61da177e4SLinus Torvalds  * if the things go wrong. Please, send bug reports to
71da177e4SLinus Torvalds  * viro@parcelfarce.linux.theplanet.co.uk
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * Stuff here is basically a glue between the VFS and generic UNIXish
101da177e4SLinus Torvalds  * filesystem that keeps everything in pagecache. All knowledge of the
111da177e4SLinus Torvalds  * directory layout is in fs/ext2/dir.c - it turned out to be easily separatable
121da177e4SLinus Torvalds  * and it's easier to debug that way. In principle we might want to
131da177e4SLinus Torvalds  * generalize that a bit and turn it into a library. Or not.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * The only non-static object here is ext2_dir_inode_operations.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * TODO: get rid of kmap() use, add readahead.
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * Copyright (C) 1992, 1993, 1994, 1995
201da177e4SLinus Torvalds  * Remy Card (card@masi.ibp.fr)
211da177e4SLinus Torvalds  * Laboratoire MASI - Institut Blaise Pascal
221da177e4SLinus Torvalds  * Universite Pierre et Marie Curie (Paris VI)
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  *  from
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  *  linux/fs/minix/namei.c
271da177e4SLinus Torvalds  *
281da177e4SLinus Torvalds  *  Copyright (C) 1991, 1992  Linus Torvalds
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  *  Big-endian to little-endian byte-swapping/bitmaps by
311da177e4SLinus Torvalds  *        David S. Miller (davem@caip.rutgers.edu), 1995
321da177e4SLinus Torvalds  */
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include <linux/pagemap.h>
35907f4554SChristoph Hellwig #include <linux/quotaops.h>
361da177e4SLinus Torvalds #include "ext2.h"
371da177e4SLinus Torvalds #include "xattr.h"
381da177e4SLinus Torvalds #include "acl.h"
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode)
411da177e4SLinus Torvalds {
421da177e4SLinus Torvalds 	int err = ext2_add_link(dentry, inode);
431da177e4SLinus Torvalds 	if (!err) {
441e2e547aSAl Viro 		d_instantiate_new(dentry, inode);
451da177e4SLinus Torvalds 		return 0;
461da177e4SLinus Torvalds 	}
47a513b035SAlexey Dobriyan 	inode_dec_link_count(inode);
482e5afe54SAl Viro 	discard_new_inode(inode);
491da177e4SLinus Torvalds 	return err;
501da177e4SLinus Torvalds }
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  * Methods themselves.
541da177e4SLinus Torvalds  */
551da177e4SLinus Torvalds 
5600cd8dd3SAl Viro static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, unsigned int flags)
571da177e4SLinus Torvalds {
581da177e4SLinus Torvalds 	struct inode * inode;
59a43850a3Szhangyi (F) 	ino_t ino;
60b4962091Szhangyi (F) 	int res;
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	if (dentry->d_name.len > EXT2_NAME_LEN)
631da177e4SLinus Torvalds 		return ERR_PTR(-ENAMETOOLONG);
641da177e4SLinus Torvalds 
65b4962091Szhangyi (F) 	res = ext2_inode_by_name(dir, &dentry->d_name, &ino);
66a43850a3Szhangyi (F) 	if (res) {
67a43850a3Szhangyi (F) 		if (res != -ENOENT)
68b4962091Szhangyi (F) 			return ERR_PTR(res);
691da177e4SLinus Torvalds 		inode = NULL;
70a43850a3Szhangyi (F) 	} else {
7152fcf703SDavid Howells 		inode = ext2_iget(dir->i_sb, ino);
72a9049376SAl Viro 		if (inode == ERR_PTR(-ESTALE)) {
734d6c13f8SBryan Donlan 			ext2_error(dir->i_sb, __func__,
744d6c13f8SBryan Donlan 					"deleted inode referenced: %lu",
75a4255e4cSHeiko Carstens 					(unsigned long) ino);
764d6c13f8SBryan Donlan 			return ERR_PTR(-EIO);
774d6c13f8SBryan Donlan 		}
784d6c13f8SBryan Donlan 	}
791da177e4SLinus Torvalds 	return d_splice_alias(inode, dentry);
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds struct dentry *ext2_get_parent(struct dentry *child)
831da177e4SLinus Torvalds {
84a43850a3Szhangyi (F) 	ino_t ino;
85b4962091Szhangyi (F) 	int res;
86b4962091Szhangyi (F) 
8780e5d1ffSAl Viro 	res = ext2_inode_by_name(d_inode(child), &dotdot_name, &ino);
88b4962091Szhangyi (F) 	if (res)
89b4962091Szhangyi (F) 		return ERR_PTR(res);
90a43850a3Szhangyi (F) 
91fc64005cSAl Viro 	return d_obtain_alias(ext2_iget(child->d_sb, ino));
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /*
951da177e4SLinus Torvalds  * By the time this is called, we already have created
961da177e4SLinus Torvalds  * the directory cache entry for the new file, but it
971da177e4SLinus Torvalds  * is so far negative - it has no inode.
981da177e4SLinus Torvalds  *
991da177e4SLinus Torvalds  * If the create succeeds, we fill in the inode information
1001da177e4SLinus Torvalds  * with d_instantiate().
1011da177e4SLinus Torvalds  */
1026c960e68SChristian Brauner static int ext2_create (struct mnt_idmap * idmap,
103549c7297SChristian Brauner 			struct inode * dir, struct dentry * dentry,
104549c7297SChristian Brauner 			umode_t mode, bool excl)
1051da177e4SLinus Torvalds {
106907f4554SChristoph Hellwig 	struct inode *inode;
107c2edb305SJan Kara 	int err;
108907f4554SChristoph Hellwig 
109c2edb305SJan Kara 	err = dquot_initialize(dir);
110c2edb305SJan Kara 	if (err)
111c2edb305SJan Kara 		return err;
112907f4554SChristoph Hellwig 
1132a7dba39SEric Paris 	inode = ext2_new_inode(dir, mode, &dentry->d_name);
114907f4554SChristoph Hellwig 	if (IS_ERR(inode))
115907f4554SChristoph Hellwig 		return PTR_ERR(inode);
116907f4554SChristoph Hellwig 
117fb094c90SDan Williams 	ext2_set_file_ops(inode);
1181da177e4SLinus Torvalds 	mark_inode_dirty(inode);
119907f4554SChristoph Hellwig 	return ext2_add_nondir(dentry, inode);
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
122549c7297SChristian Brauner static int ext2_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
123863f144fSMiklos Szeredi 			struct file *file, umode_t mode)
12460545d0dSAl Viro {
12560545d0dSAl Viro 	struct inode *inode = ext2_new_inode(dir, mode, NULL);
12660545d0dSAl Viro 	if (IS_ERR(inode))
12760545d0dSAl Viro 		return PTR_ERR(inode);
12860545d0dSAl Viro 
129fb094c90SDan Williams 	ext2_set_file_ops(inode);
13060545d0dSAl Viro 	mark_inode_dirty(inode);
131863f144fSMiklos Szeredi 	d_tmpfile(file, inode);
13260545d0dSAl Viro 	unlock_new_inode(inode);
133863f144fSMiklos Szeredi 	return finish_open_simple(file, 0);
13460545d0dSAl Viro }
13560545d0dSAl Viro 
1365ebb29beSChristian Brauner static int ext2_mknod (struct mnt_idmap * idmap, struct inode * dir,
137549c7297SChristian Brauner 	struct dentry *dentry, umode_t mode, dev_t rdev)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	struct inode * inode;
1401da177e4SLinus Torvalds 	int err;
1411da177e4SLinus Torvalds 
142c2edb305SJan Kara 	err = dquot_initialize(dir);
143c2edb305SJan Kara 	if (err)
144c2edb305SJan Kara 		return err;
145907f4554SChristoph Hellwig 
1462a7dba39SEric Paris 	inode = ext2_new_inode (dir, mode, &dentry->d_name);
1471da177e4SLinus Torvalds 	err = PTR_ERR(inode);
1481da177e4SLinus Torvalds 	if (!IS_ERR(inode)) {
1491da177e4SLinus Torvalds 		init_special_inode(inode, inode->i_mode, rdev);
1501da177e4SLinus Torvalds 		inode->i_op = &ext2_special_inode_operations;
1511da177e4SLinus Torvalds 		mark_inode_dirty(inode);
1521da177e4SLinus Torvalds 		err = ext2_add_nondir(dentry, inode);
1531da177e4SLinus Torvalds 	}
1541da177e4SLinus Torvalds 	return err;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1577a77db95SChristian Brauner static int ext2_symlink (struct mnt_idmap * idmap, struct inode * dir,
158549c7297SChristian Brauner 	struct dentry * dentry, const char * symname)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds 	struct super_block * sb = dir->i_sb;
1611da177e4SLinus Torvalds 	int err = -ENAMETOOLONG;
1621da177e4SLinus Torvalds 	unsigned l = strlen(symname)+1;
1631da177e4SLinus Torvalds 	struct inode * inode;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	if (l > sb->s_blocksize)
1661da177e4SLinus Torvalds 		goto out;
1671da177e4SLinus Torvalds 
168c2edb305SJan Kara 	err = dquot_initialize(dir);
169c2edb305SJan Kara 	if (err)
170c2edb305SJan Kara 		goto out;
171907f4554SChristoph Hellwig 
1722a7dba39SEric Paris 	inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO, &dentry->d_name);
1731da177e4SLinus Torvalds 	err = PTR_ERR(inode);
1741da177e4SLinus Torvalds 	if (IS_ERR(inode))
1751da177e4SLinus Torvalds 		goto out;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	if (l > sizeof (EXT2_I(inode)->i_data)) {
1781da177e4SLinus Torvalds 		/* slow symlink */
1791da177e4SLinus Torvalds 		inode->i_op = &ext2_symlink_inode_operations;
18021fc61c7SAl Viro 		inode_nohighmem(inode);
1811da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &ext2_aops;
1821da177e4SLinus Torvalds 		err = page_symlink(inode, symname, l);
1831da177e4SLinus Torvalds 		if (err)
1841da177e4SLinus Torvalds 			goto out_fail;
1851da177e4SLinus Torvalds 	} else {
1861da177e4SLinus Torvalds 		/* fast symlink */
1871da177e4SLinus Torvalds 		inode->i_op = &ext2_fast_symlink_inode_operations;
188cbe0fa38SAl Viro 		inode->i_link = (char*)EXT2_I(inode)->i_data;
189cbe0fa38SAl Viro 		memcpy(inode->i_link, symname, l);
1901da177e4SLinus Torvalds 		inode->i_size = l-1;
1911da177e4SLinus Torvalds 	}
1921da177e4SLinus Torvalds 	mark_inode_dirty(inode);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	err = ext2_add_nondir(dentry, inode);
1951da177e4SLinus Torvalds out:
1961da177e4SLinus Torvalds 	return err;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds out_fail:
199a513b035SAlexey Dobriyan 	inode_dec_link_count(inode);
2002e5afe54SAl Viro 	discard_new_inode(inode);
2011da177e4SLinus Torvalds 	goto out;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds static int ext2_link (struct dentry * old_dentry, struct inode * dir,
2051da177e4SLinus Torvalds 	struct dentry *dentry)
2061da177e4SLinus Torvalds {
2072b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
20841080b5aSAl Viro 	int err;
2091da177e4SLinus Torvalds 
210c2edb305SJan Kara 	err = dquot_initialize(dir);
211c2edb305SJan Kara 	if (err)
212c2edb305SJan Kara 		return err;
213907f4554SChristoph Hellwig 
21402027d42SDeepa Dinamani 	inode->i_ctime = current_time(inode);
215a513b035SAlexey Dobriyan 	inode_inc_link_count(inode);
2167de9c6eeSAl Viro 	ihold(inode);
2171da177e4SLinus Torvalds 
21841080b5aSAl Viro 	err = ext2_add_link(dentry, inode);
21941080b5aSAl Viro 	if (!err) {
22041080b5aSAl Viro 		d_instantiate(dentry, inode);
22141080b5aSAl Viro 		return 0;
22241080b5aSAl Viro 	}
22341080b5aSAl Viro 	inode_dec_link_count(inode);
22441080b5aSAl Viro 	iput(inode);
22541080b5aSAl Viro 	return err;
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
228c54bd91eSChristian Brauner static int ext2_mkdir(struct mnt_idmap * idmap,
229549c7297SChristian Brauner 	struct inode * dir, struct dentry * dentry, umode_t mode)
2301da177e4SLinus Torvalds {
2311da177e4SLinus Torvalds 	struct inode * inode;
2328de52778SAl Viro 	int err;
2331da177e4SLinus Torvalds 
234c2edb305SJan Kara 	err = dquot_initialize(dir);
235c2edb305SJan Kara 	if (err)
236c2edb305SJan Kara 		return err;
237907f4554SChristoph Hellwig 
238a513b035SAlexey Dobriyan 	inode_inc_link_count(dir);
2391da177e4SLinus Torvalds 
2402a7dba39SEric Paris 	inode = ext2_new_inode(dir, S_IFDIR | mode, &dentry->d_name);
2411da177e4SLinus Torvalds 	err = PTR_ERR(inode);
2421da177e4SLinus Torvalds 	if (IS_ERR(inode))
2431da177e4SLinus Torvalds 		goto out_dir;
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 	inode->i_op = &ext2_dir_inode_operations;
2461da177e4SLinus Torvalds 	inode->i_fop = &ext2_dir_operations;
2471da177e4SLinus Torvalds 	inode->i_mapping->a_ops = &ext2_aops;
2481da177e4SLinus Torvalds 
249a513b035SAlexey Dobriyan 	inode_inc_link_count(inode);
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	err = ext2_make_empty(inode, dir);
2521da177e4SLinus Torvalds 	if (err)
2531da177e4SLinus Torvalds 		goto out_fail;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	err = ext2_add_link(dentry, inode);
2561da177e4SLinus Torvalds 	if (err)
2571da177e4SLinus Torvalds 		goto out_fail;
2581da177e4SLinus Torvalds 
2591e2e547aSAl Viro 	d_instantiate_new(dentry, inode);
2601da177e4SLinus Torvalds out:
2611da177e4SLinus Torvalds 	return err;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds out_fail:
264a513b035SAlexey Dobriyan 	inode_dec_link_count(inode);
265a513b035SAlexey Dobriyan 	inode_dec_link_count(inode);
2662e5afe54SAl Viro 	discard_new_inode(inode);
2671da177e4SLinus Torvalds out_dir:
268a513b035SAlexey Dobriyan 	inode_dec_link_count(dir);
2691da177e4SLinus Torvalds 	goto out;
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds static int ext2_unlink(struct inode * dir, struct dentry *dentry)
2731da177e4SLinus Torvalds {
2742b0143b5SDavid Howells 	struct inode * inode = d_inode(dentry);
2751da177e4SLinus Torvalds 	struct ext2_dir_entry_2 * de;
2761da177e4SLinus Torvalds 	struct page * page;
277782b76d7SIra Weiny 	void *page_addr;
278c2edb305SJan Kara 	int err;
2791da177e4SLinus Torvalds 
280c2edb305SJan Kara 	err = dquot_initialize(dir);
281c2edb305SJan Kara 	if (err)
282c2edb305SJan Kara 		goto out;
283907f4554SChristoph Hellwig 
284782b76d7SIra Weiny 	de = ext2_find_entry(dir, &dentry->d_name, &page, &page_addr);
285b4962091Szhangyi (F) 	if (IS_ERR(de)) {
286b4962091Szhangyi (F) 		err = PTR_ERR(de);
287b4962091Szhangyi (F) 		goto out;
288b4962091Szhangyi (F) 	}
2891da177e4SLinus Torvalds 
290728d392fSJavier Pello 	err = ext2_delete_entry (de, page, page_addr);
291782b76d7SIra Weiny 	ext2_put_page(page, page_addr);
2921da177e4SLinus Torvalds 	if (err)
2931da177e4SLinus Torvalds 		goto out;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime;
296a513b035SAlexey Dobriyan 	inode_dec_link_count(inode);
2971da177e4SLinus Torvalds 	err = 0;
2981da177e4SLinus Torvalds out:
2991da177e4SLinus Torvalds 	return err;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds static int ext2_rmdir (struct inode * dir, struct dentry *dentry)
3031da177e4SLinus Torvalds {
3042b0143b5SDavid Howells 	struct inode * inode = d_inode(dentry);
3051da177e4SLinus Torvalds 	int err = -ENOTEMPTY;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	if (ext2_empty_dir(inode)) {
3081da177e4SLinus Torvalds 		err = ext2_unlink(dir, dentry);
3091da177e4SLinus Torvalds 		if (!err) {
3101da177e4SLinus Torvalds 			inode->i_size = 0;
311a513b035SAlexey Dobriyan 			inode_dec_link_count(inode);
312a513b035SAlexey Dobriyan 			inode_dec_link_count(dir);
3131da177e4SLinus Torvalds 		}
3141da177e4SLinus Torvalds 	}
3151da177e4SLinus Torvalds 	return err;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds 
318*e18275aeSChristian Brauner static int ext2_rename (struct mnt_idmap * idmap,
319549c7297SChristian Brauner 			struct inode * old_dir, struct dentry * old_dentry,
320f03b8ad8SMiklos Szeredi 			struct inode * new_dir, struct dentry * new_dentry,
321f03b8ad8SMiklos Szeredi 			unsigned int flags)
3221da177e4SLinus Torvalds {
3232b0143b5SDavid Howells 	struct inode * old_inode = d_inode(old_dentry);
3242b0143b5SDavid Howells 	struct inode * new_inode = d_inode(new_dentry);
3251da177e4SLinus Torvalds 	struct page * dir_page = NULL;
326782b76d7SIra Weiny 	void *dir_page_addr;
3271da177e4SLinus Torvalds 	struct ext2_dir_entry_2 * dir_de = NULL;
3281da177e4SLinus Torvalds 	struct page * old_page;
329782b76d7SIra Weiny 	void *old_page_addr;
3301da177e4SLinus Torvalds 	struct ext2_dir_entry_2 * old_de;
331c2edb305SJan Kara 	int err;
3321da177e4SLinus Torvalds 
333f03b8ad8SMiklos Szeredi 	if (flags & ~RENAME_NOREPLACE)
334f03b8ad8SMiklos Szeredi 		return -EINVAL;
335f03b8ad8SMiklos Szeredi 
336c2edb305SJan Kara 	err = dquot_initialize(old_dir);
337c2edb305SJan Kara 	if (err)
338c2edb305SJan Kara 		goto out;
339c2edb305SJan Kara 
340c2edb305SJan Kara 	err = dquot_initialize(new_dir);
341c2edb305SJan Kara 	if (err)
342c2edb305SJan Kara 		goto out;
343907f4554SChristoph Hellwig 
344782b76d7SIra Weiny 	old_de = ext2_find_entry(old_dir, &old_dentry->d_name, &old_page,
345782b76d7SIra Weiny 				 &old_page_addr);
346b4962091Szhangyi (F) 	if (IS_ERR(old_de)) {
347b4962091Szhangyi (F) 		err = PTR_ERR(old_de);
348b4962091Szhangyi (F) 		goto out;
349b4962091Szhangyi (F) 	}
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	if (S_ISDIR(old_inode->i_mode)) {
3521da177e4SLinus Torvalds 		err = -EIO;
353782b76d7SIra Weiny 		dir_de = ext2_dotdot(old_inode, &dir_page, &dir_page_addr);
3541da177e4SLinus Torvalds 		if (!dir_de)
3551da177e4SLinus Torvalds 			goto out_old;
3561da177e4SLinus Torvalds 	}
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	if (new_inode) {
359782b76d7SIra Weiny 		void *page_addr;
3601da177e4SLinus Torvalds 		struct page *new_page;
3611da177e4SLinus Torvalds 		struct ext2_dir_entry_2 *new_de;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 		err = -ENOTEMPTY;
3641da177e4SLinus Torvalds 		if (dir_de && !ext2_empty_dir (new_inode))
3651da177e4SLinus Torvalds 			goto out_dir;
3661da177e4SLinus Torvalds 
367782b76d7SIra Weiny 		new_de = ext2_find_entry(new_dir, &new_dentry->d_name,
368782b76d7SIra Weiny 					 &new_page, &page_addr);
369b4962091Szhangyi (F) 		if (IS_ERR(new_de)) {
370b4962091Szhangyi (F) 			err = PTR_ERR(new_de);
371b4962091Szhangyi (F) 			goto out_dir;
372b4962091Szhangyi (F) 		}
373782b76d7SIra Weiny 		ext2_set_link(new_dir, new_de, new_page, page_addr, old_inode, 1);
374782b76d7SIra Weiny 		ext2_put_page(new_page, page_addr);
37502027d42SDeepa Dinamani 		new_inode->i_ctime = current_time(new_inode);
3761da177e4SLinus Torvalds 		if (dir_de)
3779a53c3a7SDave Hansen 			drop_nlink(new_inode);
378a513b035SAlexey Dobriyan 		inode_dec_link_count(new_inode);
3791da177e4SLinus Torvalds 	} else {
3801da177e4SLinus Torvalds 		err = ext2_add_link(new_dentry, old_inode);
381e8a80c6fSJosh Hunt 		if (err)
3821da177e4SLinus Torvalds 			goto out_dir;
3831da177e4SLinus Torvalds 		if (dir_de)
384a513b035SAlexey Dobriyan 			inode_inc_link_count(new_dir);
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/*
3881da177e4SLinus Torvalds 	 * Like most other Unix systems, set the ctime for inodes on a
3891da177e4SLinus Torvalds  	 * rename.
3901da177e4SLinus Torvalds 	 */
39102027d42SDeepa Dinamani 	old_inode->i_ctime = current_time(old_inode);
392e8a80c6fSJosh Hunt 	mark_inode_dirty(old_inode);
3931da177e4SLinus Torvalds 
394728d392fSJavier Pello 	ext2_delete_entry(old_de, old_page, old_page_addr);
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	if (dir_de) {
39739fe7557SJan Kara 		if (old_dir != new_dir)
398782b76d7SIra Weiny 			ext2_set_link(old_inode, dir_de, dir_page,
399782b76d7SIra Weiny 				      dir_page_addr, new_dir, 0);
400e2ebb123SIra Weiny 
401782b76d7SIra Weiny 		ext2_put_page(dir_page, dir_page_addr);
402a513b035SAlexey Dobriyan 		inode_dec_link_count(old_dir);
4031da177e4SLinus Torvalds 	}
4041da177e4SLinus Torvalds 
405782b76d7SIra Weiny 	ext2_put_page(old_page, old_page_addr);
406782b76d7SIra Weiny 	return 0;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds out_dir:
409a6fbd0abSIra Weiny 	if (dir_de)
410782b76d7SIra Weiny 		ext2_put_page(dir_page, dir_page_addr);
4111da177e4SLinus Torvalds out_old:
412782b76d7SIra Weiny 	ext2_put_page(old_page, old_page_addr);
4131da177e4SLinus Torvalds out:
4141da177e4SLinus Torvalds 	return err;
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds 
417754661f1SArjan van de Ven const struct inode_operations ext2_dir_inode_operations = {
4181da177e4SLinus Torvalds 	.create		= ext2_create,
4191da177e4SLinus Torvalds 	.lookup		= ext2_lookup,
4201da177e4SLinus Torvalds 	.link		= ext2_link,
4211da177e4SLinus Torvalds 	.unlink		= ext2_unlink,
4221da177e4SLinus Torvalds 	.symlink	= ext2_symlink,
4231da177e4SLinus Torvalds 	.mkdir		= ext2_mkdir,
4241da177e4SLinus Torvalds 	.rmdir		= ext2_rmdir,
4251da177e4SLinus Torvalds 	.mknod		= ext2_mknod,
4261da177e4SLinus Torvalds 	.rename		= ext2_rename,
4271da177e4SLinus Torvalds 	.listxattr	= ext2_listxattr,
42893bc420eSyangerkun 	.getattr	= ext2_getattr,
4291da177e4SLinus Torvalds 	.setattr	= ext2_setattr,
430cac2f8b8SChristian Brauner 	.get_inode_acl	= ext2_get_acl,
43164e178a7SChristoph Hellwig 	.set_acl	= ext2_set_acl,
43260545d0dSAl Viro 	.tmpfile	= ext2_tmpfile,
433aba405e3SMiklos Szeredi 	.fileattr_get	= ext2_fileattr_get,
434aba405e3SMiklos Szeredi 	.fileattr_set	= ext2_fileattr_set,
4351da177e4SLinus Torvalds };
4361da177e4SLinus Torvalds 
437754661f1SArjan van de Ven const struct inode_operations ext2_special_inode_operations = {
4381da177e4SLinus Torvalds 	.listxattr	= ext2_listxattr,
43993bc420eSyangerkun 	.getattr	= ext2_getattr,
4401da177e4SLinus Torvalds 	.setattr	= ext2_setattr,
441cac2f8b8SChristian Brauner 	.get_inode_acl	= ext2_get_acl,
44264e178a7SChristoph Hellwig 	.set_acl	= ext2_set_acl,
4431da177e4SLinus Torvalds };
444