xref: /openbmc/linux/fs/hfsplus/dir.c (revision 5bd9d99d107c56ff7b35a29e930d85f91a07b2fd)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/hfsplus/dir.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2001
51da177e4SLinus Torvalds  * Brad Boyer (flar@allandria.com)
61da177e4SLinus Torvalds  * (C) 2003 Ardis Technologies <roman@ardistech.com>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Handling of directories
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/errno.h>
121da177e4SLinus Torvalds #include <linux/fs.h>
131da177e4SLinus Torvalds #include <linux/slab.h>
141da177e4SLinus Torvalds #include <linux/random.h>
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include "hfsplus_fs.h"
171da177e4SLinus Torvalds #include "hfsplus_raw.h"
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds static inline void hfsplus_instantiate(struct dentry *dentry,
201da177e4SLinus Torvalds 				       struct inode *inode, u32 cnid)
211da177e4SLinus Torvalds {
221da177e4SLinus Torvalds 	dentry->d_fsdata = (void *)(unsigned long)cnid;
231da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
241da177e4SLinus Torvalds }
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds /* Find the entry inside dir named dentry->d_name */
271da177e4SLinus Torvalds static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry,
281da177e4SLinus Torvalds 				     struct nameidata *nd)
291da177e4SLinus Torvalds {
301da177e4SLinus Torvalds 	struct inode *inode = NULL;
311da177e4SLinus Torvalds 	struct hfs_find_data fd;
321da177e4SLinus Torvalds 	struct super_block *sb;
331da177e4SLinus Torvalds 	hfsplus_cat_entry entry;
341da177e4SLinus Torvalds 	int err;
351da177e4SLinus Torvalds 	u32 cnid, linkid = 0;
361da177e4SLinus Torvalds 	u16 type;
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds 	sb = dir->i_sb;
39d45bce8fSDuane Griffin 
401da177e4SLinus Torvalds 	dentry->d_fsdata = NULL;
41*5bd9d99dSAlexey Khoroshilov 	err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
42*5bd9d99dSAlexey Khoroshilov 	if (err)
43*5bd9d99dSAlexey Khoroshilov 		return ERR_PTR(err);
441da177e4SLinus Torvalds 	hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name);
451da177e4SLinus Torvalds again:
461da177e4SLinus Torvalds 	err = hfs_brec_read(&fd, &entry, sizeof(entry));
471da177e4SLinus Torvalds 	if (err) {
481da177e4SLinus Torvalds 		if (err == -ENOENT) {
491da177e4SLinus Torvalds 			hfs_find_exit(&fd);
501da177e4SLinus Torvalds 			/* No such entry */
511da177e4SLinus Torvalds 			inode = NULL;
521da177e4SLinus Torvalds 			goto out;
531da177e4SLinus Torvalds 		}
541da177e4SLinus Torvalds 		goto fail;
551da177e4SLinus Torvalds 	}
561da177e4SLinus Torvalds 	type = be16_to_cpu(entry.type);
571da177e4SLinus Torvalds 	if (type == HFSPLUS_FOLDER) {
581da177e4SLinus Torvalds 		if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
591da177e4SLinus Torvalds 			err = -EIO;
601da177e4SLinus Torvalds 			goto fail;
611da177e4SLinus Torvalds 		}
621da177e4SLinus Torvalds 		cnid = be32_to_cpu(entry.folder.id);
631da177e4SLinus Torvalds 		dentry->d_fsdata = (void *)(unsigned long)cnid;
641da177e4SLinus Torvalds 	} else if (type == HFSPLUS_FILE) {
651da177e4SLinus Torvalds 		if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
661da177e4SLinus Torvalds 			err = -EIO;
671da177e4SLinus Torvalds 			goto fail;
681da177e4SLinus Torvalds 		}
691da177e4SLinus Torvalds 		cnid = be32_to_cpu(entry.file.id);
702753cc28SAnton Salikhmetov 		if (entry.file.user_info.fdType ==
712753cc28SAnton Salikhmetov 				cpu_to_be32(HFSP_HARDLINK_TYPE) &&
722753cc28SAnton Salikhmetov 				entry.file.user_info.fdCreator ==
732753cc28SAnton Salikhmetov 				cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
742753cc28SAnton Salikhmetov 				(entry.file.create_date ==
752753cc28SAnton Salikhmetov 					HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->
762753cc28SAnton Salikhmetov 						create_date ||
772753cc28SAnton Salikhmetov 				entry.file.create_date ==
782753cc28SAnton Salikhmetov 					HFSPLUS_I(sb->s_root->d_inode)->
792753cc28SAnton Salikhmetov 						create_date) &&
80dd73a01aSChristoph Hellwig 				HFSPLUS_SB(sb)->hidden_dir) {
811da177e4SLinus Torvalds 			struct qstr str;
821da177e4SLinus Torvalds 			char name[32];
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 			if (dentry->d_fsdata) {
85af8c85bbSRoman Zippel 				/*
86af8c85bbSRoman Zippel 				 * We found a link pointing to another link,
87af8c85bbSRoman Zippel 				 * so ignore it and treat it as regular file.
88af8c85bbSRoman Zippel 				 */
89af8c85bbSRoman Zippel 				cnid = (unsigned long)dentry->d_fsdata;
90af8c85bbSRoman Zippel 				linkid = 0;
91af8c85bbSRoman Zippel 			} else {
921da177e4SLinus Torvalds 				dentry->d_fsdata = (void *)(unsigned long)cnid;
932753cc28SAnton Salikhmetov 				linkid =
942753cc28SAnton Salikhmetov 					be32_to_cpu(entry.file.permissions.dev);
951da177e4SLinus Torvalds 				str.len = sprintf(name, "iNode%d", linkid);
961da177e4SLinus Torvalds 				str.name = name;
97dd73a01aSChristoph Hellwig 				hfsplus_cat_build_key(sb, fd.search_key,
982753cc28SAnton Salikhmetov 					HFSPLUS_SB(sb)->hidden_dir->i_ino,
992753cc28SAnton Salikhmetov 					&str);
1001da177e4SLinus Torvalds 				goto again;
101af8c85bbSRoman Zippel 			}
1021da177e4SLinus Torvalds 		} else if (!dentry->d_fsdata)
1031da177e4SLinus Torvalds 			dentry->d_fsdata = (void *)(unsigned long)cnid;
1041da177e4SLinus Torvalds 	} else {
105634725a9SRoman Zippel 		printk(KERN_ERR "hfs: invalid catalog entry type in lookup\n");
1061da177e4SLinus Torvalds 		err = -EIO;
1071da177e4SLinus Torvalds 		goto fail;
1081da177e4SLinus Torvalds 	}
1091da177e4SLinus Torvalds 	hfs_find_exit(&fd);
11063525391SDavid Howells 	inode = hfsplus_iget(dir->i_sb, cnid);
11163525391SDavid Howells 	if (IS_ERR(inode))
11263525391SDavid Howells 		return ERR_CAST(inode);
1131da177e4SLinus Torvalds 	if (S_ISREG(inode->i_mode))
114f6089ff8SChristoph Hellwig 		HFSPLUS_I(inode)->linkid = linkid;
1151da177e4SLinus Torvalds out:
1161da177e4SLinus Torvalds 	d_add(dentry, inode);
1171da177e4SLinus Torvalds 	return NULL;
1181da177e4SLinus Torvalds fail:
1191da177e4SLinus Torvalds 	hfs_find_exit(&fd);
1201da177e4SLinus Torvalds 	return ERR_PTR(err);
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds static int hfsplus_readdir(struct file *filp, void *dirent, filldir_t filldir)
1241da177e4SLinus Torvalds {
125f44ea031SJosef Sipek 	struct inode *inode = filp->f_path.dentry->d_inode;
1261da177e4SLinus Torvalds 	struct super_block *sb = inode->i_sb;
1271da177e4SLinus Torvalds 	int len, err;
1281da177e4SLinus Torvalds 	char strbuf[HFSPLUS_MAX_STRLEN + 1];
1291da177e4SLinus Torvalds 	hfsplus_cat_entry entry;
1301da177e4SLinus Torvalds 	struct hfs_find_data fd;
1311da177e4SLinus Torvalds 	struct hfsplus_readdir_data *rd;
1321da177e4SLinus Torvalds 	u16 type;
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	if (filp->f_pos >= inode->i_size)
1351da177e4SLinus Torvalds 		return 0;
1361da177e4SLinus Torvalds 
137*5bd9d99dSAlexey Khoroshilov 	err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
138*5bd9d99dSAlexey Khoroshilov 	if (err)
139*5bd9d99dSAlexey Khoroshilov 		return err;
1401da177e4SLinus Torvalds 	hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
1411da177e4SLinus Torvalds 	err = hfs_brec_find(&fd);
1421da177e4SLinus Torvalds 	if (err)
1431da177e4SLinus Torvalds 		goto out;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	switch ((u32)filp->f_pos) {
1461da177e4SLinus Torvalds 	case 0:
1471da177e4SLinus Torvalds 		/* This is completely artificial... */
1481da177e4SLinus Torvalds 		if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
1491da177e4SLinus Torvalds 			goto out;
1501da177e4SLinus Torvalds 		filp->f_pos++;
1511da177e4SLinus Torvalds 		/* fall through */
1521da177e4SLinus Torvalds 	case 1:
1532753cc28SAnton Salikhmetov 		hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
1542753cc28SAnton Salikhmetov 			fd.entrylength);
1551da177e4SLinus Torvalds 		if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
156634725a9SRoman Zippel 			printk(KERN_ERR "hfs: bad catalog folder thread\n");
1571da177e4SLinus Torvalds 			err = -EIO;
1581da177e4SLinus Torvalds 			goto out;
1591da177e4SLinus Torvalds 		}
1601da177e4SLinus Torvalds 		if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
161634725a9SRoman Zippel 			printk(KERN_ERR "hfs: truncated catalog thread\n");
1621da177e4SLinus Torvalds 			err = -EIO;
1631da177e4SLinus Torvalds 			goto out;
1641da177e4SLinus Torvalds 		}
1651da177e4SLinus Torvalds 		if (filldir(dirent, "..", 2, 1,
1661da177e4SLinus Torvalds 			    be32_to_cpu(entry.thread.parentID), DT_DIR))
1671da177e4SLinus Torvalds 			goto out;
1681da177e4SLinus Torvalds 		filp->f_pos++;
1691da177e4SLinus Torvalds 		/* fall through */
1701da177e4SLinus Torvalds 	default:
1711da177e4SLinus Torvalds 		if (filp->f_pos >= inode->i_size)
1721da177e4SLinus Torvalds 			goto out;
1731da177e4SLinus Torvalds 		err = hfs_brec_goto(&fd, filp->f_pos - 1);
1741da177e4SLinus Torvalds 		if (err)
1751da177e4SLinus Torvalds 			goto out;
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	for (;;) {
1791da177e4SLinus Torvalds 		if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
180634725a9SRoman Zippel 			printk(KERN_ERR "hfs: walked past end of dir\n");
1811da177e4SLinus Torvalds 			err = -EIO;
1821da177e4SLinus Torvalds 			goto out;
1831da177e4SLinus Torvalds 		}
1842753cc28SAnton Salikhmetov 		hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
1852753cc28SAnton Salikhmetov 			fd.entrylength);
1861da177e4SLinus Torvalds 		type = be16_to_cpu(entry.type);
1871da177e4SLinus Torvalds 		len = HFSPLUS_MAX_STRLEN;
1881da177e4SLinus Torvalds 		err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
1891da177e4SLinus Torvalds 		if (err)
1901da177e4SLinus Torvalds 			goto out;
1911da177e4SLinus Torvalds 		if (type == HFSPLUS_FOLDER) {
1922753cc28SAnton Salikhmetov 			if (fd.entrylength <
1932753cc28SAnton Salikhmetov 					sizeof(struct hfsplus_cat_folder)) {
194634725a9SRoman Zippel 				printk(KERN_ERR "hfs: small dir entry\n");
1951da177e4SLinus Torvalds 				err = -EIO;
1961da177e4SLinus Torvalds 				goto out;
1971da177e4SLinus Torvalds 			}
198dd73a01aSChristoph Hellwig 			if (HFSPLUS_SB(sb)->hidden_dir &&
199dd73a01aSChristoph Hellwig 			    HFSPLUS_SB(sb)->hidden_dir->i_ino ==
200dd73a01aSChristoph Hellwig 					be32_to_cpu(entry.folder.id))
2011da177e4SLinus Torvalds 				goto next;
2021da177e4SLinus Torvalds 			if (filldir(dirent, strbuf, len, filp->f_pos,
2031da177e4SLinus Torvalds 				    be32_to_cpu(entry.folder.id), DT_DIR))
2041da177e4SLinus Torvalds 				break;
2051da177e4SLinus Torvalds 		} else if (type == HFSPLUS_FILE) {
2061da177e4SLinus Torvalds 			if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
207634725a9SRoman Zippel 				printk(KERN_ERR "hfs: small file entry\n");
2081da177e4SLinus Torvalds 				err = -EIO;
2091da177e4SLinus Torvalds 				goto out;
2101da177e4SLinus Torvalds 			}
2111da177e4SLinus Torvalds 			if (filldir(dirent, strbuf, len, filp->f_pos,
2121da177e4SLinus Torvalds 				    be32_to_cpu(entry.file.id), DT_REG))
2131da177e4SLinus Torvalds 				break;
2141da177e4SLinus Torvalds 		} else {
215634725a9SRoman Zippel 			printk(KERN_ERR "hfs: bad catalog entry type\n");
2161da177e4SLinus Torvalds 			err = -EIO;
2171da177e4SLinus Torvalds 			goto out;
2181da177e4SLinus Torvalds 		}
2191da177e4SLinus Torvalds next:
2201da177e4SLinus Torvalds 		filp->f_pos++;
2211da177e4SLinus Torvalds 		if (filp->f_pos >= inode->i_size)
2221da177e4SLinus Torvalds 			goto out;
2231da177e4SLinus Torvalds 		err = hfs_brec_goto(&fd, 1);
2241da177e4SLinus Torvalds 		if (err)
2251da177e4SLinus Torvalds 			goto out;
2261da177e4SLinus Torvalds 	}
2271da177e4SLinus Torvalds 	rd = filp->private_data;
2281da177e4SLinus Torvalds 	if (!rd) {
2291da177e4SLinus Torvalds 		rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
2301da177e4SLinus Torvalds 		if (!rd) {
2311da177e4SLinus Torvalds 			err = -ENOMEM;
2321da177e4SLinus Torvalds 			goto out;
2331da177e4SLinus Torvalds 		}
2341da177e4SLinus Torvalds 		filp->private_data = rd;
2351da177e4SLinus Torvalds 		rd->file = filp;
2366af502deSChristoph Hellwig 		list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
2371da177e4SLinus Torvalds 	}
2381da177e4SLinus Torvalds 	memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
2391da177e4SLinus Torvalds out:
2401da177e4SLinus Torvalds 	hfs_find_exit(&fd);
2411da177e4SLinus Torvalds 	return err;
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds static int hfsplus_dir_release(struct inode *inode, struct file *file)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	struct hfsplus_readdir_data *rd = file->private_data;
2471da177e4SLinus Torvalds 	if (rd) {
24889755dcaSChristoph Hellwig 		mutex_lock(&inode->i_mutex);
2491da177e4SLinus Torvalds 		list_del(&rd->list);
25089755dcaSChristoph Hellwig 		mutex_unlock(&inode->i_mutex);
2511da177e4SLinus Torvalds 		kfree(rd);
2521da177e4SLinus Torvalds 	}
2531da177e4SLinus Torvalds 	return 0;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
2571da177e4SLinus Torvalds 			struct dentry *dst_dentry)
2581da177e4SLinus Torvalds {
259dd73a01aSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
2601da177e4SLinus Torvalds 	struct inode *inode = src_dentry->d_inode;
2611da177e4SLinus Torvalds 	struct inode *src_dir = src_dentry->d_parent->d_inode;
2621da177e4SLinus Torvalds 	struct qstr str;
2631da177e4SLinus Torvalds 	char name[32];
2641da177e4SLinus Torvalds 	u32 cnid, id;
2651da177e4SLinus Torvalds 	int res;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 	if (HFSPLUS_IS_RSRC(inode))
2681da177e4SLinus Torvalds 		return -EPERM;
269f6089ff8SChristoph Hellwig 	if (!S_ISREG(inode->i_mode))
270f6089ff8SChristoph Hellwig 		return -EPERM;
2711da177e4SLinus Torvalds 
2727ac9fb9cSChristoph Hellwig 	mutex_lock(&sbi->vh_mutex);
2731da177e4SLinus Torvalds 	if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
2741da177e4SLinus Torvalds 		for (;;) {
2751da177e4SLinus Torvalds 			get_random_bytes(&id, sizeof(cnid));
2761da177e4SLinus Torvalds 			id &= 0x3fffffff;
2771da177e4SLinus Torvalds 			str.name = name;
2781da177e4SLinus Torvalds 			str.len = sprintf(name, "iNode%d", id);
2791da177e4SLinus Torvalds 			res = hfsplus_rename_cat(inode->i_ino,
2801da177e4SLinus Torvalds 						 src_dir, &src_dentry->d_name,
281dd73a01aSChristoph Hellwig 						 sbi->hidden_dir, &str);
2821da177e4SLinus Torvalds 			if (!res)
2831da177e4SLinus Torvalds 				break;
2841da177e4SLinus Torvalds 			if (res != -EEXIST)
2857ac9fb9cSChristoph Hellwig 				goto out;
2861da177e4SLinus Torvalds 		}
287f6089ff8SChristoph Hellwig 		HFSPLUS_I(inode)->linkid = id;
288dd73a01aSChristoph Hellwig 		cnid = sbi->next_cnid++;
2891da177e4SLinus Torvalds 		src_dentry->d_fsdata = (void *)(unsigned long)cnid;
2902753cc28SAnton Salikhmetov 		res = hfsplus_create_cat(cnid, src_dir,
2912753cc28SAnton Salikhmetov 			&src_dentry->d_name, inode);
2921da177e4SLinus Torvalds 		if (res)
2931da177e4SLinus Torvalds 			/* panic? */
2947ac9fb9cSChristoph Hellwig 			goto out;
295dd73a01aSChristoph Hellwig 		sbi->file_count++;
2961da177e4SLinus Torvalds 	}
297dd73a01aSChristoph Hellwig 	cnid = sbi->next_cnid++;
2981da177e4SLinus Torvalds 	res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
2991da177e4SLinus Torvalds 	if (res)
3007ac9fb9cSChristoph Hellwig 		goto out;
3011da177e4SLinus Torvalds 
302d8c76e6fSDave Hansen 	inc_nlink(inode);
3031da177e4SLinus Torvalds 	hfsplus_instantiate(dst_dentry, inode, cnid);
3047de9c6eeSAl Viro 	ihold(inode);
3051da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME_SEC;
3061da177e4SLinus Torvalds 	mark_inode_dirty(inode);
307dd73a01aSChristoph Hellwig 	sbi->file_count++;
308dd73a01aSChristoph Hellwig 	dst_dir->i_sb->s_dirt = 1;
3097ac9fb9cSChristoph Hellwig out:
3107ac9fb9cSChristoph Hellwig 	mutex_unlock(&sbi->vh_mutex);
3117ac9fb9cSChristoph Hellwig 	return res;
3121da177e4SLinus Torvalds }
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
3151da177e4SLinus Torvalds {
316dd73a01aSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
3171da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
3181da177e4SLinus Torvalds 	struct qstr str;
3191da177e4SLinus Torvalds 	char name[32];
3201da177e4SLinus Torvalds 	u32 cnid;
3211da177e4SLinus Torvalds 	int res;
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	if (HFSPLUS_IS_RSRC(inode))
3241da177e4SLinus Torvalds 		return -EPERM;
3251da177e4SLinus Torvalds 
3267ac9fb9cSChristoph Hellwig 	mutex_lock(&sbi->vh_mutex);
3271da177e4SLinus Torvalds 	cnid = (u32)(unsigned long)dentry->d_fsdata;
3281da177e4SLinus Torvalds 	if (inode->i_ino == cnid &&
3296af502deSChristoph Hellwig 	    atomic_read(&HFSPLUS_I(inode)->opencnt)) {
3301da177e4SLinus Torvalds 		str.name = name;
3311da177e4SLinus Torvalds 		str.len = sprintf(name, "temp%lu", inode->i_ino);
3321da177e4SLinus Torvalds 		res = hfsplus_rename_cat(inode->i_ino,
3331da177e4SLinus Torvalds 					 dir, &dentry->d_name,
334dd73a01aSChristoph Hellwig 					 sbi->hidden_dir, &str);
33585b8fe8cSChristoph Hellwig 		if (!res) {
3361da177e4SLinus Torvalds 			inode->i_flags |= S_DEAD;
33785b8fe8cSChristoph Hellwig 			drop_nlink(inode);
33885b8fe8cSChristoph Hellwig 		}
3397ac9fb9cSChristoph Hellwig 		goto out;
3401da177e4SLinus Torvalds 	}
3411da177e4SLinus Torvalds 	res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
3421da177e4SLinus Torvalds 	if (res)
3437ac9fb9cSChristoph Hellwig 		goto out;
3441da177e4SLinus Torvalds 
345af8c85bbSRoman Zippel 	if (inode->i_nlink > 0)
3469a53c3a7SDave Hansen 		drop_nlink(inode);
34776b0c26aSRoman Zippel 	if (inode->i_ino == cnid)
34876b0c26aSRoman Zippel 		clear_nlink(inode);
34976b0c26aSRoman Zippel 	if (!inode->i_nlink) {
35076b0c26aSRoman Zippel 		if (inode->i_ino != cnid) {
351dd73a01aSChristoph Hellwig 			sbi->file_count--;
3526af502deSChristoph Hellwig 			if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
35376b0c26aSRoman Zippel 				res = hfsplus_delete_cat(inode->i_ino,
354dd73a01aSChristoph Hellwig 							 sbi->hidden_dir,
35576b0c26aSRoman Zippel 							 NULL);
3561da177e4SLinus Torvalds 				if (!res)
3571da177e4SLinus Torvalds 					hfsplus_delete_inode(inode);
3581da177e4SLinus Torvalds 			} else
3591da177e4SLinus Torvalds 				inode->i_flags |= S_DEAD;
360af8c85bbSRoman Zippel 		} else
36176b0c26aSRoman Zippel 			hfsplus_delete_inode(inode);
36276b0c26aSRoman Zippel 	} else
363dd73a01aSChristoph Hellwig 		sbi->file_count--;
3641da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME_SEC;
3651da177e4SLinus Torvalds 	mark_inode_dirty(inode);
3667ac9fb9cSChristoph Hellwig out:
3677ac9fb9cSChristoph Hellwig 	mutex_unlock(&sbi->vh_mutex);
3681da177e4SLinus Torvalds 	return res;
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
3721da177e4SLinus Torvalds {
3737ac9fb9cSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
3747ac9fb9cSChristoph Hellwig 	struct inode *inode = dentry->d_inode;
3751da177e4SLinus Torvalds 	int res;
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	if (inode->i_size != 2)
3781da177e4SLinus Torvalds 		return -ENOTEMPTY;
3797ac9fb9cSChristoph Hellwig 
3807ac9fb9cSChristoph Hellwig 	mutex_lock(&sbi->vh_mutex);
3811da177e4SLinus Torvalds 	res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
3821da177e4SLinus Torvalds 	if (res)
3837ac9fb9cSChristoph Hellwig 		goto out;
384ce71ec36SDave Hansen 	clear_nlink(inode);
3851da177e4SLinus Torvalds 	inode->i_ctime = CURRENT_TIME_SEC;
3861da177e4SLinus Torvalds 	hfsplus_delete_inode(inode);
3871da177e4SLinus Torvalds 	mark_inode_dirty(inode);
3887ac9fb9cSChristoph Hellwig out:
3897ac9fb9cSChristoph Hellwig 	mutex_unlock(&sbi->vh_mutex);
3907ac9fb9cSChristoph Hellwig 	return res;
3911da177e4SLinus Torvalds }
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
3941da177e4SLinus Torvalds 			   const char *symname)
3951da177e4SLinus Torvalds {
3967ac9fb9cSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
3971da177e4SLinus Torvalds 	struct inode *inode;
3987ac9fb9cSChristoph Hellwig 	int res = -ENOSPC;
3991da177e4SLinus Torvalds 
4007ac9fb9cSChristoph Hellwig 	mutex_lock(&sbi->vh_mutex);
401f17c89bfSChristoph Hellwig 	inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
4021da177e4SLinus Torvalds 	if (!inode)
4037ac9fb9cSChristoph Hellwig 		goto out;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	res = page_symlink(inode, symname, strlen(symname) + 1);
406f17c89bfSChristoph Hellwig 	if (res)
407f17c89bfSChristoph Hellwig 		goto out_err;
408f17c89bfSChristoph Hellwig 
409f17c89bfSChristoph Hellwig 	res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
410f17c89bfSChristoph Hellwig 	if (res)
411f17c89bfSChristoph Hellwig 		goto out_err;
412f17c89bfSChristoph Hellwig 
413f17c89bfSChristoph Hellwig 	hfsplus_instantiate(dentry, inode, inode->i_ino);
414f17c89bfSChristoph Hellwig 	mark_inode_dirty(inode);
4157ac9fb9cSChristoph Hellwig 	goto out;
416f17c89bfSChristoph Hellwig 
417f17c89bfSChristoph Hellwig out_err:
4181da177e4SLinus Torvalds 	inode->i_nlink = 0;
4191da177e4SLinus Torvalds 	hfsplus_delete_inode(inode);
4201da177e4SLinus Torvalds 	iput(inode);
4217ac9fb9cSChristoph Hellwig out:
4227ac9fb9cSChristoph Hellwig 	mutex_unlock(&sbi->vh_mutex);
4231da177e4SLinus Torvalds 	return res;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
4271da177e4SLinus Torvalds 			 int mode, dev_t rdev)
4281da177e4SLinus Torvalds {
4297ac9fb9cSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
4301da177e4SLinus Torvalds 	struct inode *inode;
4317ac9fb9cSChristoph Hellwig 	int res = -ENOSPC;
4321da177e4SLinus Torvalds 
4337ac9fb9cSChristoph Hellwig 	mutex_lock(&sbi->vh_mutex);
43430d3abbeSChristoph Hellwig 	inode = hfsplus_new_inode(dir->i_sb, mode);
4351da177e4SLinus Torvalds 	if (!inode)
4367ac9fb9cSChristoph Hellwig 		goto out;
4371da177e4SLinus Torvalds 
43890e61690SChristoph Hellwig 	if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
43990e61690SChristoph Hellwig 		init_special_inode(inode, mode, rdev);
44090e61690SChristoph Hellwig 
4411da177e4SLinus Torvalds 	res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
4421da177e4SLinus Torvalds 	if (res) {
4431da177e4SLinus Torvalds 		inode->i_nlink = 0;
4441da177e4SLinus Torvalds 		hfsplus_delete_inode(inode);
4451da177e4SLinus Torvalds 		iput(inode);
4467ac9fb9cSChristoph Hellwig 		goto out;
4471da177e4SLinus Torvalds 	}
44830d3abbeSChristoph Hellwig 
4491da177e4SLinus Torvalds 	hfsplus_instantiate(dentry, inode, inode->i_ino);
4501da177e4SLinus Torvalds 	mark_inode_dirty(inode);
4517ac9fb9cSChristoph Hellwig out:
4527ac9fb9cSChristoph Hellwig 	mutex_unlock(&sbi->vh_mutex);
4537ac9fb9cSChristoph Hellwig 	return res;
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
45630d3abbeSChristoph Hellwig static int hfsplus_create(struct inode *dir, struct dentry *dentry, int mode,
45730d3abbeSChristoph Hellwig 			  struct nameidata *nd)
45830d3abbeSChristoph Hellwig {
45930d3abbeSChristoph Hellwig 	return hfsplus_mknod(dir, dentry, mode, 0);
46030d3abbeSChristoph Hellwig }
46130d3abbeSChristoph Hellwig 
46230d3abbeSChristoph Hellwig static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, int mode)
46330d3abbeSChristoph Hellwig {
46430d3abbeSChristoph Hellwig 	return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
46530d3abbeSChristoph Hellwig }
46630d3abbeSChristoph Hellwig 
4671da177e4SLinus Torvalds static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
4681da177e4SLinus Torvalds 			  struct inode *new_dir, struct dentry *new_dentry)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	int res;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	/* Unlink destination if it already exists */
4731da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
474e3911785SSage Weil 		if (S_ISDIR(new_dentry->d_inode->i_mode))
47540de9a7cSChristoph Hellwig 			res = hfsplus_rmdir(new_dir, new_dentry);
476e3911785SSage Weil 		else
4771da177e4SLinus Torvalds 			res = hfsplus_unlink(new_dir, new_dentry);
4781da177e4SLinus Torvalds 		if (res)
4791da177e4SLinus Torvalds 			return res;
4801da177e4SLinus Torvalds 	}
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
4831da177e4SLinus Torvalds 				 old_dir, &old_dentry->d_name,
4841da177e4SLinus Torvalds 				 new_dir, &new_dentry->d_name);
4851da177e4SLinus Torvalds 	if (!res)
4861da177e4SLinus Torvalds 		new_dentry->d_fsdata = old_dentry->d_fsdata;
4871da177e4SLinus Torvalds 	return res;
4881da177e4SLinus Torvalds }
4891da177e4SLinus Torvalds 
49092e1d5beSArjan van de Ven const struct inode_operations hfsplus_dir_inode_operations = {
4911da177e4SLinus Torvalds 	.lookup		= hfsplus_lookup,
4921da177e4SLinus Torvalds 	.create		= hfsplus_create,
4931da177e4SLinus Torvalds 	.link		= hfsplus_link,
4941da177e4SLinus Torvalds 	.unlink		= hfsplus_unlink,
4951da177e4SLinus Torvalds 	.mkdir		= hfsplus_mkdir,
4961da177e4SLinus Torvalds 	.rmdir		= hfsplus_rmdir,
4971da177e4SLinus Torvalds 	.symlink	= hfsplus_symlink,
4981da177e4SLinus Torvalds 	.mknod		= hfsplus_mknod,
4991da177e4SLinus Torvalds 	.rename		= hfsplus_rename,
5001da177e4SLinus Torvalds };
5011da177e4SLinus Torvalds 
5024b6f5d20SArjan van de Ven const struct file_operations hfsplus_dir_operations = {
503eb29d66dSChristoph Hellwig 	.fsync		= hfsplus_file_fsync,
5041da177e4SLinus Torvalds 	.read		= generic_read_dir,
5051da177e4SLinus Torvalds 	.readdir	= hfsplus_readdir,
5067cc4bcc6SArnd Bergmann 	.unlocked_ioctl = hfsplus_ioctl,
5071da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
5081da177e4SLinus Torvalds 	.release	= hfsplus_dir_release,
5091da177e4SLinus Torvalds };
510