VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   32 Branches   54 Tags
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   1) /*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   2)  *  linux/fs/hfs/dir.c
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   3)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   4)  * Copyright (C) 1995-1997  Paul H. Hargrove
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   5)  * (C) 2003 Ardis Technologies <roman@ardistech.com>
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   6)  * This file may be distributed under the terms of the GNU General Public License.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   7)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   8)  * This file contains directory-related functions independent of which
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700   9)  * scheme is being used to represent forks.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  10)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  11)  * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  12)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  13) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  14) #include "hfs_fs.h"
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  15) #include "btree.h"
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  16) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  17) /*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  18)  * hfs_lookup()
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  19)  */
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  20) static struct dentry *hfs_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3bf95f (Al Viro            2012-06-10 17:13:09 -0400  21) 				 unsigned int flags)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  22) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  23) 	hfs_cat_rec rec;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  24) 	struct hfs_find_data fd;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  25) 	struct inode *inode = NULL;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  26) 	int res;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  27) 
9509f17851da2 (Alexey Khoroshilov 2013-04-30 15:27:52 -0700  28) 	res = hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd);
9509f17851da2 (Alexey Khoroshilov 2013-04-30 15:27:52 -0700  29) 	if (res)
9509f17851da2 (Alexey Khoroshilov 2013-04-30 15:27:52 -0700  30) 		return ERR_PTR(res);
328b922786502 (Roman Zippel       2005-09-06 15:18:49 -0700  31) 	hfs_cat_build_key(dir->i_sb, fd.search_key, dir->i_ino, &dentry->d_name);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  32) 	res = hfs_brec_read(&fd, &rec, sizeof(rec));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  33) 	if (res) {
6b9cceead0d1f (Al Viro            2018-04-30 19:51:43 -0400  34) 		if (res != -ENOENT)
6b9cceead0d1f (Al Viro            2018-04-30 19:51:43 -0400  35) 			inode = ERR_PTR(res);
6b9cceead0d1f (Al Viro            2018-04-30 19:51:43 -0400  36) 	} else {
6b9cceead0d1f (Al Viro            2018-04-30 19:51:43 -0400  37) 		inode = hfs_iget(dir->i_sb, &fd.search_key->cat, &rec);
6b9cceead0d1f (Al Viro            2018-04-30 19:51:43 -0400  38) 		if (!inode)
6b9cceead0d1f (Al Viro            2018-04-30 19:51:43 -0400  39) 			inode = ERR_PTR(-EACCES);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  40) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  41) 	hfs_find_exit(&fd);
6b9cceead0d1f (Al Viro            2018-04-30 19:51:43 -0400  42) 	return d_splice_alias(inode, dentry);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  43) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  44) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  45) /*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  46)  * hfs_readdir
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  47)  */
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400  48) static int hfs_readdir(struct file *file, struct dir_context *ctx)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  49) {
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400  50) 	struct inode *inode = file_inode(file);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  51) 	struct super_block *sb = inode->i_sb;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  52) 	int len, err;
328b922786502 (Roman Zippel       2005-09-06 15:18:49 -0700  53) 	char strbuf[HFS_MAX_NAMELEN];
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  54) 	union hfs_cat_rec entry;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  55) 	struct hfs_find_data fd;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  56) 	struct hfs_readdir_data *rd;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  57) 	u16 type;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  58) 
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400  59) 	if (ctx->pos >= inode->i_size)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  60) 		return 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  61) 
9509f17851da2 (Alexey Khoroshilov 2013-04-30 15:27:52 -0700  62) 	err = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
9509f17851da2 (Alexey Khoroshilov 2013-04-30 15:27:52 -0700  63) 	if (err)
9509f17851da2 (Alexey Khoroshilov 2013-04-30 15:27:52 -0700  64) 		return err;
328b922786502 (Roman Zippel       2005-09-06 15:18:49 -0700  65) 	hfs_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  66) 	err = hfs_brec_find(&fd);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  67) 	if (err)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  68) 		goto out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  69) 
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400  70) 	if (ctx->pos == 0) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  71) 		/* This is completely artificial... */
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400  72) 		if (!dir_emit_dot(file, ctx))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  73) 			goto out;
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400  74) 		ctx->pos = 1;
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400  75) 	}
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400  76) 	if (ctx->pos == 1) {
ec81aecb29668 (Amerigo Wang       2009-12-14 17:57:37 -0800  77) 		if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
ec81aecb29668 (Amerigo Wang       2009-12-14 17:57:37 -0800  78) 			err = -EIO;
ec81aecb29668 (Amerigo Wang       2009-12-14 17:57:37 -0800  79) 			goto out;
ec81aecb29668 (Amerigo Wang       2009-12-14 17:57:37 -0800  80) 		}
ec81aecb29668 (Amerigo Wang       2009-12-14 17:57:37 -0800  81) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  82) 		hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  83) 		if (entry.type != HFS_CDR_THD) {
d614267329f2b (Joe Perches        2013-04-30 15:27:55 -0700  84) 			pr_err("bad catalog folder thread\n");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  85) 			err = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  86) 			goto out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  87) 		}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  88) 		//if (fd.entrylength < HFS_MIN_THREAD_SZ) {
d614267329f2b (Joe Perches        2013-04-30 15:27:55 -0700  89) 		//	pr_err("truncated catalog thread\n");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  90) 		//	err = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  91) 		//	goto out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  92) 		//}
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400  93) 		if (!dir_emit(ctx, "..", 2,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  94) 			    be32_to_cpu(entry.thread.ParID), DT_DIR))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  95) 			goto out;
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400  96) 		ctx->pos = 2;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700  97) 	}
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400  98) 	if (ctx->pos >= inode->i_size)
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400  99) 		goto out;
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400 100) 	err = hfs_brec_goto(&fd, ctx->pos - 1);
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400 101) 	if (err)
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400 102) 		goto out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 103) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 104) 	for (;;) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 105) 		if (be32_to_cpu(fd.key->cat.ParID) != inode->i_ino) {
d614267329f2b (Joe Perches        2013-04-30 15:27:55 -0700 106) 			pr_err("walked past end of dir\n");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 107) 			err = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 108) 			goto out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 109) 		}
ec81aecb29668 (Amerigo Wang       2009-12-14 17:57:37 -0800 110) 
ec81aecb29668 (Amerigo Wang       2009-12-14 17:57:37 -0800 111) 		if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
ec81aecb29668 (Amerigo Wang       2009-12-14 17:57:37 -0800 112) 			err = -EIO;
ec81aecb29668 (Amerigo Wang       2009-12-14 17:57:37 -0800 113) 			goto out;
ec81aecb29668 (Amerigo Wang       2009-12-14 17:57:37 -0800 114) 		}
ec81aecb29668 (Amerigo Wang       2009-12-14 17:57:37 -0800 115) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 116) 		hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 117) 		type = entry.type;
328b922786502 (Roman Zippel       2005-09-06 15:18:49 -0700 118) 		len = hfs_mac2asc(sb, strbuf, &fd.key->cat.CName);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 119) 		if (type == HFS_CDR_DIR) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 120) 			if (fd.entrylength < sizeof(struct hfs_cat_dir)) {
d614267329f2b (Joe Perches        2013-04-30 15:27:55 -0700 121) 				pr_err("small dir entry\n");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 122) 				err = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 123) 				goto out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 124) 			}
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400 125) 			if (!dir_emit(ctx, strbuf, len,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 126) 				    be32_to_cpu(entry.dir.DirID), DT_DIR))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 127) 				break;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 128) 		} else if (type == HFS_CDR_FIL) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 129) 			if (fd.entrylength < sizeof(struct hfs_cat_file)) {
d614267329f2b (Joe Perches        2013-04-30 15:27:55 -0700 130) 				pr_err("small file entry\n");
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 131) 				err = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 132) 				goto out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 133) 			}
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400 134) 			if (!dir_emit(ctx, strbuf, len,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 135) 				    be32_to_cpu(entry.file.FlNum), DT_REG))
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 136) 				break;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 137) 		} else {
d614267329f2b (Joe Perches        2013-04-30 15:27:55 -0700 138) 			pr_err("bad catalog entry type %d\n", type);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 139) 			err = -EIO;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 140) 			goto out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 141) 		}
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400 142) 		ctx->pos++;
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400 143) 		if (ctx->pos >= inode->i_size)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 144) 			goto out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 145) 		err = hfs_brec_goto(&fd, 1);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 146) 		if (err)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 147) 			goto out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 148) 	}
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400 149) 	rd = file->private_data;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 150) 	if (!rd) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 151) 		rd = kmalloc(sizeof(struct hfs_readdir_data), GFP_KERNEL);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 152) 		if (!rd) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 153) 			err = -ENOMEM;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 154) 			goto out;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 155) 		}
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400 156) 		file->private_data = rd;
002f8bec85443 (Al Viro            2013-05-22 14:29:35 -0400 157) 		rd->file = file;
9717a91b01fed (Al Viro            2016-05-12 20:13:50 -0400 158) 		spin_lock(&HFS_I(inode)->open_dir_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 159) 		list_add(&rd->list, &HFS_I(inode)->open_dir_list);
9717a91b01fed (Al Viro            2016-05-12 20:13:50 -0400 160) 		spin_unlock(&HFS_I(inode)->open_dir_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 161) 	}
9717a91b01fed (Al Viro            2016-05-12 20:13:50 -0400 162) 	/*
9717a91b01fed (Al Viro            2016-05-12 20:13:50 -0400 163) 	 * Can be done after the list insertion; exclusion with
9717a91b01fed (Al Viro            2016-05-12 20:13:50 -0400 164) 	 * hfs_delete_cat() is provided by directory lock.
9717a91b01fed (Al Viro            2016-05-12 20:13:50 -0400 165) 	 */
eec11535ca3d3 (Dan Carpenter      2017-01-18 14:13:20 +0300 166) 	memcpy(&rd->key, &fd.key->cat, sizeof(struct hfs_cat_key));
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 167) out:
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 168) 	hfs_find_exit(&fd);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 169) 	return err;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 170) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 171) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 172) static int hfs_dir_release(struct inode *inode, struct file *file)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 173) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 174) 	struct hfs_readdir_data *rd = file->private_data;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 175) 	if (rd) {
9717a91b01fed (Al Viro            2016-05-12 20:13:50 -0400 176) 		spin_lock(&HFS_I(inode)->open_dir_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 177) 		list_del(&rd->list);
9717a91b01fed (Al Viro            2016-05-12 20:13:50 -0400 178) 		spin_unlock(&HFS_I(inode)->open_dir_lock);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 179) 		kfree(rd);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 180) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 181) 	return 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 182) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 183) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 184) /*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 185)  * hfs_create()
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 186)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 187)  * This is the create() entry in the inode_operations structure for
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 188)  * regular HFS directories.  The purpose is to create a new file in
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 189)  * a directory and return a corresponding inode, given the inode for
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 190)  * the directory and the name (and its length) of the new file.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 191)  */
549c7297717c3 (Christian Brauner  2021-01-21 14:19:43 +0100 192) static int hfs_create(struct user_namespace *mnt_userns, struct inode *dir,
549c7297717c3 (Christian Brauner  2021-01-21 14:19:43 +0100 193) 		      struct dentry *dentry, umode_t mode, bool excl)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 194) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 195) 	struct inode *inode;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 196) 	int res;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 197) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 198) 	inode = hfs_new_inode(dir, &dentry->d_name, mode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 199) 	if (!inode)
13f244852f119 (Chengyu Song       2015-04-16 12:46:53 -0700 200) 		return -ENOMEM;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 201) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 202) 	res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 203) 	if (res) {
6d6b77f163c7e (Miklos Szeredi     2011-10-28 14:13:28 +0200 204) 		clear_nlink(inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 205) 		hfs_delete_inode(inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 206) 		iput(inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 207) 		return res;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 208) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 209) 	d_instantiate(dentry, inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 210) 	mark_inode_dirty(inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 211) 	return 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 212) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 213) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 214) /*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 215)  * hfs_mkdir()
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 216)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 217)  * This is the mkdir() entry in the inode_operations structure for
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 218)  * regular HFS directories.  The purpose is to create a new directory
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 219)  * in a directory, given the inode for the parent directory and the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 220)  * name (and its length) of the new directory.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 221)  */
549c7297717c3 (Christian Brauner  2021-01-21 14:19:43 +0100 222) static int hfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
549c7297717c3 (Christian Brauner  2021-01-21 14:19:43 +0100 223) 		     struct dentry *dentry, umode_t mode)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 224) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 225) 	struct inode *inode;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 226) 	int res;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 227) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 228) 	inode = hfs_new_inode(dir, &dentry->d_name, S_IFDIR | mode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 229) 	if (!inode)
13f244852f119 (Chengyu Song       2015-04-16 12:46:53 -0700 230) 		return -ENOMEM;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 231) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 232) 	res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 233) 	if (res) {
6d6b77f163c7e (Miklos Szeredi     2011-10-28 14:13:28 +0200 234) 		clear_nlink(inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 235) 		hfs_delete_inode(inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 236) 		iput(inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 237) 		return res;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 238) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 239) 	d_instantiate(dentry, inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 240) 	mark_inode_dirty(inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 241) 	return 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 242) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 243) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 244) /*
69102e9b4b61f (Al Viro            2011-03-02 23:46:51 -0500 245)  * hfs_remove()
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 246)  *
69102e9b4b61f (Al Viro            2011-03-02 23:46:51 -0500 247)  * This serves as both unlink() and rmdir() in the inode_operations
69102e9b4b61f (Al Viro            2011-03-02 23:46:51 -0500 248)  * structure for regular HFS directories.  The purpose is to delete
69102e9b4b61f (Al Viro            2011-03-02 23:46:51 -0500 249)  * an existing child, given the inode for the parent directory and
69102e9b4b61f (Al Viro            2011-03-02 23:46:51 -0500 250)  * the name (and its length) of the existing directory.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 251)  *
69102e9b4b61f (Al Viro            2011-03-02 23:46:51 -0500 252)  * HFS does not have hardlinks, so both rmdir and unlink set the
69102e9b4b61f (Al Viro            2011-03-02 23:46:51 -0500 253)  * link count to 0.  The only difference is the emptiness check.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 254)  */
69102e9b4b61f (Al Viro            2011-03-02 23:46:51 -0500 255) static int hfs_remove(struct inode *dir, struct dentry *dentry)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 256) {
2b0143b5c986b (David Howells      2015-03-17 22:25:59 +0000 257) 	struct inode *inode = d_inode(dentry);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 258) 	int res;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 259) 
69102e9b4b61f (Al Viro            2011-03-02 23:46:51 -0500 260) 	if (S_ISDIR(inode->i_mode) && inode->i_size != 2)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 261) 		return -ENOTEMPTY;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 262) 	res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 263) 	if (res)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 264) 		return res;
ce71ec3684036 (Dave Hansen        2006-09-30 23:29:06 -0700 265) 	clear_nlink(inode);
02027d42c3f74 (Deepa Dinamani     2016-09-14 07:48:05 -0700 266) 	inode->i_ctime = current_time(inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 267) 	hfs_delete_inode(inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 268) 	mark_inode_dirty(inode);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 269) 	return 0;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 270) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 271) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 272) /*
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 273)  * hfs_rename()
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 274)  *
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 275)  * This is the rename() entry in the inode_operations structure for
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 276)  * regular HFS directories.  The purpose is to rename an existing
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 277)  * file or directory, given the inode for the current directory and
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 278)  * the name (and its length) of the existing file/directory and the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 279)  * inode for the new directory and the name (and its length) of the
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 280)  * new file/directory.
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 281)  * XXX: how do you handle must_be dir?
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 282)  */
549c7297717c3 (Christian Brauner  2021-01-21 14:19:43 +0100 283) static int hfs_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
549c7297717c3 (Christian Brauner  2021-01-21 14:19:43 +0100 284) 		      struct dentry *old_dentry, struct inode *new_dir,
549c7297717c3 (Christian Brauner  2021-01-21 14:19:43 +0100 285) 		      struct dentry *new_dentry, unsigned int flags)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 286) {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 287) 	int res;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 288) 
f03b8ad8d3863 (Miklos Szeredi     2016-09-27 11:03:57 +0200 289) 	if (flags & ~RENAME_NOREPLACE)
f03b8ad8d3863 (Miklos Szeredi     2016-09-27 11:03:57 +0200 290) 		return -EINVAL;
f03b8ad8d3863 (Miklos Szeredi     2016-09-27 11:03:57 +0200 291) 
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 292) 	/* Unlink destination if it already exists */
2b0143b5c986b (David Howells      2015-03-17 22:25:59 +0000 293) 	if (d_really_is_positive(new_dentry)) {
69102e9b4b61f (Al Viro            2011-03-02 23:46:51 -0500 294) 		res = hfs_remove(new_dir, new_dentry);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 295) 		if (res)
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 296) 			return res;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 297) 	}
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 298) 
2b0143b5c986b (David Howells      2015-03-17 22:25:59 +0000 299) 	res = hfs_cat_move(d_inode(old_dentry)->i_ino,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 300) 			   old_dir, &old_dentry->d_name,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 301) 			   new_dir, &new_dentry->d_name);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 302) 	if (!res)
328b922786502 (Roman Zippel       2005-09-06 15:18:49 -0700 303) 		hfs_cat_build_key(old_dir->i_sb,
2b0143b5c986b (David Howells      2015-03-17 22:25:59 +0000 304) 				  (btree_key *)&HFS_I(d_inode(old_dentry))->cat_key,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 305) 				  new_dir->i_ino, &new_dentry->d_name);
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 306) 	return res;
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 307) }
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 308) 
4b6f5d20b04dc (Arjan van de Ven   2006-03-28 01:56:42 -0800 309) const struct file_operations hfs_dir_operations = {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 310) 	.read		= generic_read_dir,
9717a91b01fed (Al Viro            2016-05-12 20:13:50 -0400 311) 	.iterate_shared	= hfs_readdir,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 312) 	.llseek		= generic_file_llseek,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 313) 	.release	= hfs_dir_release,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 314) };
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 315) 
92e1d5be91a0e (Arjan van de Ven   2007-02-12 00:55:39 -0800 316) const struct inode_operations hfs_dir_inode_operations = {
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 317) 	.create		= hfs_create,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 318) 	.lookup		= hfs_lookup,
69102e9b4b61f (Al Viro            2011-03-02 23:46:51 -0500 319) 	.unlink		= hfs_remove,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 320) 	.mkdir		= hfs_mkdir,
69102e9b4b61f (Al Viro            2011-03-02 23:46:51 -0500 321) 	.rmdir		= hfs_remove,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 322) 	.rename		= hfs_rename,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 323) 	.setattr	= hfs_inode_setattr,
^1da177e4c3f4 (Linus Torvalds     2005-04-16 15:20:36 -0700 324) };