1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/fs/hfsplus/dir.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 2001
61da177e4SLinus Torvalds * Brad Boyer (flar@allandria.com)
71da177e4SLinus Torvalds * (C) 2003 Ardis Technologies <roman@ardistech.com>
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Handling of directories
101da177e4SLinus Torvalds */
111da177e4SLinus Torvalds
121da177e4SLinus Torvalds #include <linux/errno.h>
131da177e4SLinus Torvalds #include <linux/fs.h>
141da177e4SLinus Torvalds #include <linux/slab.h>
151da177e4SLinus Torvalds #include <linux/random.h>
16017f8da4SHin-Tak Leung #include <linux/nls.h>
171da177e4SLinus Torvalds
181da177e4SLinus Torvalds #include "hfsplus_fs.h"
191da177e4SLinus Torvalds #include "hfsplus_raw.h"
20324ef39aSVyacheslav Dubeyko #include "xattr.h"
211da177e4SLinus Torvalds
hfsplus_instantiate(struct dentry * dentry,struct inode * inode,u32 cnid)221da177e4SLinus Torvalds static inline void hfsplus_instantiate(struct dentry *dentry,
231da177e4SLinus Torvalds struct inode *inode, u32 cnid)
241da177e4SLinus Torvalds {
251da177e4SLinus Torvalds dentry->d_fsdata = (void *)(unsigned long)cnid;
261da177e4SLinus Torvalds d_instantiate(dentry, inode);
271da177e4SLinus Torvalds }
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds /* Find the entry inside dir named dentry->d_name */
hfsplus_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)301da177e4SLinus Torvalds static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry,
3100cd8dd3SAl Viro unsigned int flags)
321da177e4SLinus Torvalds {
331da177e4SLinus Torvalds struct inode *inode = NULL;
341da177e4SLinus Torvalds struct hfs_find_data fd;
351da177e4SLinus Torvalds struct super_block *sb;
361da177e4SLinus Torvalds hfsplus_cat_entry entry;
371da177e4SLinus Torvalds int err;
381da177e4SLinus Torvalds u32 cnid, linkid = 0;
391da177e4SLinus Torvalds u16 type;
401da177e4SLinus Torvalds
411da177e4SLinus Torvalds sb = dir->i_sb;
42d45bce8fSDuane Griffin
431da177e4SLinus Torvalds dentry->d_fsdata = NULL;
445bd9d99dSAlexey Khoroshilov err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
455bd9d99dSAlexey Khoroshilov if (err)
465bd9d99dSAlexey Khoroshilov return ERR_PTR(err);
4789ac9b4dSSougata Santra err = hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino,
4889ac9b4dSSougata Santra &dentry->d_name);
4989ac9b4dSSougata Santra if (unlikely(err < 0))
5089ac9b4dSSougata Santra goto fail;
511da177e4SLinus Torvalds again:
521da177e4SLinus Torvalds err = hfs_brec_read(&fd, &entry, sizeof(entry));
531da177e4SLinus Torvalds if (err) {
541da177e4SLinus Torvalds if (err == -ENOENT) {
551da177e4SLinus Torvalds hfs_find_exit(&fd);
561da177e4SLinus Torvalds /* No such entry */
571da177e4SLinus Torvalds inode = NULL;
581da177e4SLinus Torvalds goto out;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds goto fail;
611da177e4SLinus Torvalds }
621da177e4SLinus Torvalds type = be16_to_cpu(entry.type);
631da177e4SLinus Torvalds if (type == HFSPLUS_FOLDER) {
641da177e4SLinus Torvalds if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
651da177e4SLinus Torvalds err = -EIO;
661da177e4SLinus Torvalds goto fail;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds cnid = be32_to_cpu(entry.folder.id);
691da177e4SLinus Torvalds dentry->d_fsdata = (void *)(unsigned long)cnid;
701da177e4SLinus Torvalds } else if (type == HFSPLUS_FILE) {
711da177e4SLinus Torvalds if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
721da177e4SLinus Torvalds err = -EIO;
731da177e4SLinus Torvalds goto fail;
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds cnid = be32_to_cpu(entry.file.id);
762753cc28SAnton Salikhmetov if (entry.file.user_info.fdType ==
772753cc28SAnton Salikhmetov cpu_to_be32(HFSP_HARDLINK_TYPE) &&
782753cc28SAnton Salikhmetov entry.file.user_info.fdCreator ==
792753cc28SAnton Salikhmetov cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
80a7ec7a41SErnesto A. Fernández HFSPLUS_SB(sb)->hidden_dir &&
812753cc28SAnton Salikhmetov (entry.file.create_date ==
822753cc28SAnton Salikhmetov HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->
832753cc28SAnton Salikhmetov create_date ||
842753cc28SAnton Salikhmetov entry.file.create_date ==
852b0143b5SDavid Howells HFSPLUS_I(d_inode(sb->s_root))->
86a7ec7a41SErnesto A. Fernández create_date)) {
871da177e4SLinus Torvalds struct qstr str;
881da177e4SLinus Torvalds char name[32];
891da177e4SLinus Torvalds
901da177e4SLinus Torvalds if (dentry->d_fsdata) {
91af8c85bbSRoman Zippel /*
92af8c85bbSRoman Zippel * We found a link pointing to another link,
93af8c85bbSRoman Zippel * so ignore it and treat it as regular file.
94af8c85bbSRoman Zippel */
95af8c85bbSRoman Zippel cnid = (unsigned long)dentry->d_fsdata;
96af8c85bbSRoman Zippel linkid = 0;
97af8c85bbSRoman Zippel } else {
981da177e4SLinus Torvalds dentry->d_fsdata = (void *)(unsigned long)cnid;
992753cc28SAnton Salikhmetov linkid =
1002753cc28SAnton Salikhmetov be32_to_cpu(entry.file.permissions.dev);
1011da177e4SLinus Torvalds str.len = sprintf(name, "iNode%d", linkid);
1021da177e4SLinus Torvalds str.name = name;
10389ac9b4dSSougata Santra err = hfsplus_cat_build_key(sb, fd.search_key,
1042753cc28SAnton Salikhmetov HFSPLUS_SB(sb)->hidden_dir->i_ino,
1052753cc28SAnton Salikhmetov &str);
10689ac9b4dSSougata Santra if (unlikely(err < 0))
10789ac9b4dSSougata Santra goto fail;
1081da177e4SLinus Torvalds goto again;
109af8c85bbSRoman Zippel }
1101da177e4SLinus Torvalds } else if (!dentry->d_fsdata)
1111da177e4SLinus Torvalds dentry->d_fsdata = (void *)(unsigned long)cnid;
1121da177e4SLinus Torvalds } else {
113d6142673SJoe Perches pr_err("invalid catalog entry type in lookup\n");
1141da177e4SLinus Torvalds err = -EIO;
1151da177e4SLinus Torvalds goto fail;
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds hfs_find_exit(&fd);
11863525391SDavid Howells inode = hfsplus_iget(dir->i_sb, cnid);
11963525391SDavid Howells if (IS_ERR(inode))
12063525391SDavid Howells return ERR_CAST(inode);
1211da177e4SLinus Torvalds if (S_ISREG(inode->i_mode))
122f6089ff8SChristoph Hellwig HFSPLUS_I(inode)->linkid = linkid;
1231da177e4SLinus Torvalds out:
124293542d8SAl Viro return d_splice_alias(inode, dentry);
1251da177e4SLinus Torvalds fail:
1261da177e4SLinus Torvalds hfs_find_exit(&fd);
1271da177e4SLinus Torvalds return ERR_PTR(err);
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds
hfsplus_readdir(struct file * file,struct dir_context * ctx)130e72514e7SAl Viro static int hfsplus_readdir(struct file *file, struct dir_context *ctx)
1311da177e4SLinus Torvalds {
132e72514e7SAl Viro struct inode *inode = file_inode(file);
1331da177e4SLinus Torvalds struct super_block *sb = inode->i_sb;
1341da177e4SLinus Torvalds int len, err;
135017f8da4SHin-Tak Leung char *strbuf;
1361da177e4SLinus Torvalds hfsplus_cat_entry entry;
1371da177e4SLinus Torvalds struct hfs_find_data fd;
1381da177e4SLinus Torvalds struct hfsplus_readdir_data *rd;
1391da177e4SLinus Torvalds u16 type;
1401da177e4SLinus Torvalds
141e72514e7SAl Viro if (file->f_pos >= inode->i_size)
1421da177e4SLinus Torvalds return 0;
1431da177e4SLinus Torvalds
1445bd9d99dSAlexey Khoroshilov err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
1455bd9d99dSAlexey Khoroshilov if (err)
1465bd9d99dSAlexey Khoroshilov return err;
147017f8da4SHin-Tak Leung strbuf = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN + 1, GFP_KERNEL);
148017f8da4SHin-Tak Leung if (!strbuf) {
149017f8da4SHin-Tak Leung err = -ENOMEM;
150017f8da4SHin-Tak Leung goto out;
151017f8da4SHin-Tak Leung }
15289ac9b4dSSougata Santra hfsplus_cat_build_key_with_cnid(sb, fd.search_key, inode->i_ino);
153324ef39aSVyacheslav Dubeyko err = hfs_brec_find(&fd, hfs_find_rec_by_key);
1541da177e4SLinus Torvalds if (err)
1551da177e4SLinus Torvalds goto out;
1561da177e4SLinus Torvalds
157e72514e7SAl Viro if (ctx->pos == 0) {
1581da177e4SLinus Torvalds /* This is completely artificial... */
159e72514e7SAl Viro if (!dir_emit_dot(file, ctx))
1601da177e4SLinus Torvalds goto out;
161e72514e7SAl Viro ctx->pos = 1;
162e72514e7SAl Viro }
163e72514e7SAl Viro if (ctx->pos == 1) {
1646f24f892SGreg Kroah-Hartman if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
1656f24f892SGreg Kroah-Hartman err = -EIO;
1666f24f892SGreg Kroah-Hartman goto out;
1676f24f892SGreg Kroah-Hartman }
1686f24f892SGreg Kroah-Hartman
1692753cc28SAnton Salikhmetov hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
1702753cc28SAnton Salikhmetov fd.entrylength);
1711da177e4SLinus Torvalds if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
172d6142673SJoe Perches pr_err("bad catalog folder thread\n");
1731da177e4SLinus Torvalds err = -EIO;
1741da177e4SLinus Torvalds goto out;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
177d6142673SJoe Perches pr_err("truncated catalog thread\n");
1781da177e4SLinus Torvalds err = -EIO;
1791da177e4SLinus Torvalds goto out;
1801da177e4SLinus Torvalds }
181e72514e7SAl Viro if (!dir_emit(ctx, "..", 2,
1821da177e4SLinus Torvalds be32_to_cpu(entry.thread.parentID), DT_DIR))
1831da177e4SLinus Torvalds goto out;
184e72514e7SAl Viro ctx->pos = 2;
185e72514e7SAl Viro }
186e72514e7SAl Viro if (ctx->pos >= inode->i_size)
1871da177e4SLinus Torvalds goto out;
188e72514e7SAl Viro err = hfs_brec_goto(&fd, ctx->pos - 1);
1891da177e4SLinus Torvalds if (err)
1901da177e4SLinus Torvalds goto out;
1911da177e4SLinus Torvalds for (;;) {
1921da177e4SLinus Torvalds if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
193d6142673SJoe Perches pr_err("walked past end of dir\n");
1941da177e4SLinus Torvalds err = -EIO;
1951da177e4SLinus Torvalds goto out;
1961da177e4SLinus Torvalds }
1976f24f892SGreg Kroah-Hartman
1986f24f892SGreg Kroah-Hartman if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
1996f24f892SGreg Kroah-Hartman err = -EIO;
2006f24f892SGreg Kroah-Hartman goto out;
2016f24f892SGreg Kroah-Hartman }
2026f24f892SGreg Kroah-Hartman
2032753cc28SAnton Salikhmetov hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
2042753cc28SAnton Salikhmetov fd.entrylength);
2051da177e4SLinus Torvalds type = be16_to_cpu(entry.type);
206017f8da4SHin-Tak Leung len = NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN;
2071da177e4SLinus Torvalds err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
2081da177e4SLinus Torvalds if (err)
2091da177e4SLinus Torvalds goto out;
2101da177e4SLinus Torvalds if (type == HFSPLUS_FOLDER) {
2112753cc28SAnton Salikhmetov if (fd.entrylength <
2122753cc28SAnton Salikhmetov sizeof(struct hfsplus_cat_folder)) {
213d6142673SJoe Perches pr_err("small dir entry\n");
2141da177e4SLinus Torvalds err = -EIO;
2151da177e4SLinus Torvalds goto out;
2161da177e4SLinus Torvalds }
217dd73a01aSChristoph Hellwig if (HFSPLUS_SB(sb)->hidden_dir &&
218dd73a01aSChristoph Hellwig HFSPLUS_SB(sb)->hidden_dir->i_ino ==
219dd73a01aSChristoph Hellwig be32_to_cpu(entry.folder.id))
2201da177e4SLinus Torvalds goto next;
221e72514e7SAl Viro if (!dir_emit(ctx, strbuf, len,
2221da177e4SLinus Torvalds be32_to_cpu(entry.folder.id), DT_DIR))
2231da177e4SLinus Torvalds break;
2241da177e4SLinus Torvalds } else if (type == HFSPLUS_FILE) {
22597a62eaeSSergei Antonov u16 mode;
22697a62eaeSSergei Antonov unsigned type = DT_UNKNOWN;
22797a62eaeSSergei Antonov
2281da177e4SLinus Torvalds if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
229d6142673SJoe Perches pr_err("small file entry\n");
2301da177e4SLinus Torvalds err = -EIO;
2311da177e4SLinus Torvalds goto out;
2321da177e4SLinus Torvalds }
23397a62eaeSSergei Antonov
23497a62eaeSSergei Antonov mode = be16_to_cpu(entry.file.permissions.mode);
23597a62eaeSSergei Antonov if (S_ISREG(mode))
23697a62eaeSSergei Antonov type = DT_REG;
23797a62eaeSSergei Antonov else if (S_ISLNK(mode))
23897a62eaeSSergei Antonov type = DT_LNK;
23997a62eaeSSergei Antonov else if (S_ISFIFO(mode))
24097a62eaeSSergei Antonov type = DT_FIFO;
24197a62eaeSSergei Antonov else if (S_ISCHR(mode))
24297a62eaeSSergei Antonov type = DT_CHR;
24397a62eaeSSergei Antonov else if (S_ISBLK(mode))
24497a62eaeSSergei Antonov type = DT_BLK;
24597a62eaeSSergei Antonov else if (S_ISSOCK(mode))
24697a62eaeSSergei Antonov type = DT_SOCK;
24797a62eaeSSergei Antonov
248e72514e7SAl Viro if (!dir_emit(ctx, strbuf, len,
24997a62eaeSSergei Antonov be32_to_cpu(entry.file.id), type))
2501da177e4SLinus Torvalds break;
2511da177e4SLinus Torvalds } else {
252d6142673SJoe Perches pr_err("bad catalog entry type\n");
2531da177e4SLinus Torvalds err = -EIO;
2541da177e4SLinus Torvalds goto out;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds next:
257e72514e7SAl Viro ctx->pos++;
258e72514e7SAl Viro if (ctx->pos >= inode->i_size)
2591da177e4SLinus Torvalds goto out;
2601da177e4SLinus Torvalds err = hfs_brec_goto(&fd, 1);
2611da177e4SLinus Torvalds if (err)
2621da177e4SLinus Torvalds goto out;
2631da177e4SLinus Torvalds }
264e72514e7SAl Viro rd = file->private_data;
2651da177e4SLinus Torvalds if (!rd) {
2661da177e4SLinus Torvalds rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
2671da177e4SLinus Torvalds if (!rd) {
2681da177e4SLinus Torvalds err = -ENOMEM;
2691da177e4SLinus Torvalds goto out;
2701da177e4SLinus Torvalds }
271e72514e7SAl Viro file->private_data = rd;
272e72514e7SAl Viro rd->file = file;
273323ee8fcSAl Viro spin_lock(&HFSPLUS_I(inode)->open_dir_lock);
2746af502deSChristoph Hellwig list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
275323ee8fcSAl Viro spin_unlock(&HFSPLUS_I(inode)->open_dir_lock);
2761da177e4SLinus Torvalds }
277323ee8fcSAl Viro /*
278323ee8fcSAl Viro * Can be done after the list insertion; exclusion with
279323ee8fcSAl Viro * hfsplus_delete_cat() is provided by directory lock.
280323ee8fcSAl Viro */
2811da177e4SLinus Torvalds memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
2821da177e4SLinus Torvalds out:
283017f8da4SHin-Tak Leung kfree(strbuf);
2841da177e4SLinus Torvalds hfs_find_exit(&fd);
2851da177e4SLinus Torvalds return err;
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds
hfsplus_dir_release(struct inode * inode,struct file * file)2881da177e4SLinus Torvalds static int hfsplus_dir_release(struct inode *inode, struct file *file)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds struct hfsplus_readdir_data *rd = file->private_data;
2911da177e4SLinus Torvalds if (rd) {
292323ee8fcSAl Viro spin_lock(&HFSPLUS_I(inode)->open_dir_lock);
2931da177e4SLinus Torvalds list_del(&rd->list);
294323ee8fcSAl Viro spin_unlock(&HFSPLUS_I(inode)->open_dir_lock);
2951da177e4SLinus Torvalds kfree(rd);
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds return 0;
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds
hfsplus_link(struct dentry * src_dentry,struct inode * dst_dir,struct dentry * dst_dentry)3001da177e4SLinus Torvalds static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
3011da177e4SLinus Torvalds struct dentry *dst_dentry)
3021da177e4SLinus Torvalds {
303dd73a01aSChristoph Hellwig struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
3042b0143b5SDavid Howells struct inode *inode = d_inode(src_dentry);
3052b0143b5SDavid Howells struct inode *src_dir = d_inode(src_dentry->d_parent);
3061da177e4SLinus Torvalds struct qstr str;
3071da177e4SLinus Torvalds char name[32];
3081da177e4SLinus Torvalds u32 cnid, id;
3091da177e4SLinus Torvalds int res;
3101da177e4SLinus Torvalds
3111da177e4SLinus Torvalds if (HFSPLUS_IS_RSRC(inode))
3121da177e4SLinus Torvalds return -EPERM;
313f6089ff8SChristoph Hellwig if (!S_ISREG(inode->i_mode))
314f6089ff8SChristoph Hellwig return -EPERM;
3151da177e4SLinus Torvalds
3167ac9fb9cSChristoph Hellwig mutex_lock(&sbi->vh_mutex);
3171da177e4SLinus Torvalds if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
3181da177e4SLinus Torvalds for (;;) {
3191da177e4SLinus Torvalds get_random_bytes(&id, sizeof(cnid));
3201da177e4SLinus Torvalds id &= 0x3fffffff;
3211da177e4SLinus Torvalds str.name = name;
3221da177e4SLinus Torvalds str.len = sprintf(name, "iNode%d", id);
3231da177e4SLinus Torvalds res = hfsplus_rename_cat(inode->i_ino,
3241da177e4SLinus Torvalds src_dir, &src_dentry->d_name,
325dd73a01aSChristoph Hellwig sbi->hidden_dir, &str);
3261da177e4SLinus Torvalds if (!res)
3271da177e4SLinus Torvalds break;
3281da177e4SLinus Torvalds if (res != -EEXIST)
3297ac9fb9cSChristoph Hellwig goto out;
3301da177e4SLinus Torvalds }
331f6089ff8SChristoph Hellwig HFSPLUS_I(inode)->linkid = id;
332dd73a01aSChristoph Hellwig cnid = sbi->next_cnid++;
3331da177e4SLinus Torvalds src_dentry->d_fsdata = (void *)(unsigned long)cnid;
3342753cc28SAnton Salikhmetov res = hfsplus_create_cat(cnid, src_dir,
3352753cc28SAnton Salikhmetov &src_dentry->d_name, inode);
3361da177e4SLinus Torvalds if (res)
3371da177e4SLinus Torvalds /* panic? */
3387ac9fb9cSChristoph Hellwig goto out;
339dd73a01aSChristoph Hellwig sbi->file_count++;
3401da177e4SLinus Torvalds }
341dd73a01aSChristoph Hellwig cnid = sbi->next_cnid++;
3421da177e4SLinus Torvalds res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
3431da177e4SLinus Torvalds if (res)
3447ac9fb9cSChristoph Hellwig goto out;
3451da177e4SLinus Torvalds
346d8c76e6fSDave Hansen inc_nlink(inode);
3471da177e4SLinus Torvalds hfsplus_instantiate(dst_dentry, inode, cnid);
3487de9c6eeSAl Viro ihold(inode);
349*101fa821SJeff Layton inode_set_ctime_current(inode);
3501da177e4SLinus Torvalds mark_inode_dirty(inode);
351dd73a01aSChristoph Hellwig sbi->file_count++;
3529e6c5829SArtem Bityutskiy hfsplus_mark_mdb_dirty(dst_dir->i_sb);
3537ac9fb9cSChristoph Hellwig out:
3547ac9fb9cSChristoph Hellwig mutex_unlock(&sbi->vh_mutex);
3557ac9fb9cSChristoph Hellwig return res;
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds
hfsplus_unlink(struct inode * dir,struct dentry * dentry)3581da177e4SLinus Torvalds static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
3591da177e4SLinus Torvalds {
360dd73a01aSChristoph Hellwig struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
3612b0143b5SDavid Howells struct inode *inode = d_inode(dentry);
3621da177e4SLinus Torvalds struct qstr str;
3631da177e4SLinus Torvalds char name[32];
3641da177e4SLinus Torvalds u32 cnid;
3651da177e4SLinus Torvalds int res;
3661da177e4SLinus Torvalds
3671da177e4SLinus Torvalds if (HFSPLUS_IS_RSRC(inode))
3681da177e4SLinus Torvalds return -EPERM;
3691da177e4SLinus Torvalds
3707ac9fb9cSChristoph Hellwig mutex_lock(&sbi->vh_mutex);
3711da177e4SLinus Torvalds cnid = (u32)(unsigned long)dentry->d_fsdata;
3721da177e4SLinus Torvalds if (inode->i_ino == cnid &&
3736af502deSChristoph Hellwig atomic_read(&HFSPLUS_I(inode)->opencnt)) {
3741da177e4SLinus Torvalds str.name = name;
3751da177e4SLinus Torvalds str.len = sprintf(name, "temp%lu", inode->i_ino);
3761da177e4SLinus Torvalds res = hfsplus_rename_cat(inode->i_ino,
3771da177e4SLinus Torvalds dir, &dentry->d_name,
378dd73a01aSChristoph Hellwig sbi->hidden_dir, &str);
37985b8fe8cSChristoph Hellwig if (!res) {
3801da177e4SLinus Torvalds inode->i_flags |= S_DEAD;
38185b8fe8cSChristoph Hellwig drop_nlink(inode);
38285b8fe8cSChristoph Hellwig }
3837ac9fb9cSChristoph Hellwig goto out;
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
3861da177e4SLinus Torvalds if (res)
3877ac9fb9cSChristoph Hellwig goto out;
3881da177e4SLinus Torvalds
389af8c85bbSRoman Zippel if (inode->i_nlink > 0)
3909a53c3a7SDave Hansen drop_nlink(inode);
39176b0c26aSRoman Zippel if (inode->i_ino == cnid)
39276b0c26aSRoman Zippel clear_nlink(inode);
39376b0c26aSRoman Zippel if (!inode->i_nlink) {
39476b0c26aSRoman Zippel if (inode->i_ino != cnid) {
395dd73a01aSChristoph Hellwig sbi->file_count--;
3966af502deSChristoph Hellwig if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
39776b0c26aSRoman Zippel res = hfsplus_delete_cat(inode->i_ino,
398dd73a01aSChristoph Hellwig sbi->hidden_dir,
39976b0c26aSRoman Zippel NULL);
4001da177e4SLinus Torvalds if (!res)
4011da177e4SLinus Torvalds hfsplus_delete_inode(inode);
4021da177e4SLinus Torvalds } else
4031da177e4SLinus Torvalds inode->i_flags |= S_DEAD;
404af8c85bbSRoman Zippel } else
40576b0c26aSRoman Zippel hfsplus_delete_inode(inode);
40676b0c26aSRoman Zippel } else
407dd73a01aSChristoph Hellwig sbi->file_count--;
408*101fa821SJeff Layton inode_set_ctime_current(inode);
4091da177e4SLinus Torvalds mark_inode_dirty(inode);
4107ac9fb9cSChristoph Hellwig out:
4117ac9fb9cSChristoph Hellwig mutex_unlock(&sbi->vh_mutex);
4121da177e4SLinus Torvalds return res;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds
hfsplus_rmdir(struct inode * dir,struct dentry * dentry)4151da177e4SLinus Torvalds static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
4161da177e4SLinus Torvalds {
4177ac9fb9cSChristoph Hellwig struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
4182b0143b5SDavid Howells struct inode *inode = d_inode(dentry);
4191da177e4SLinus Torvalds int res;
4201da177e4SLinus Torvalds
4211da177e4SLinus Torvalds if (inode->i_size != 2)
4221da177e4SLinus Torvalds return -ENOTEMPTY;
4237ac9fb9cSChristoph Hellwig
4247ac9fb9cSChristoph Hellwig mutex_lock(&sbi->vh_mutex);
4251da177e4SLinus Torvalds res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
4261da177e4SLinus Torvalds if (res)
4277ac9fb9cSChristoph Hellwig goto out;
428ce71ec36SDave Hansen clear_nlink(inode);
429*101fa821SJeff Layton inode_set_ctime_current(inode);
4301da177e4SLinus Torvalds hfsplus_delete_inode(inode);
4311da177e4SLinus Torvalds mark_inode_dirty(inode);
4327ac9fb9cSChristoph Hellwig out:
4337ac9fb9cSChristoph Hellwig mutex_unlock(&sbi->vh_mutex);
4347ac9fb9cSChristoph Hellwig return res;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds
hfsplus_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)4377a77db95SChristian Brauner static int hfsplus_symlink(struct mnt_idmap *idmap, struct inode *dir,
438549c7297SChristian Brauner struct dentry *dentry, const char *symname)
4391da177e4SLinus Torvalds {
4407ac9fb9cSChristoph Hellwig struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
4411da177e4SLinus Torvalds struct inode *inode;
44227a4e388SChengyu Song int res = -ENOMEM;
4431da177e4SLinus Torvalds
4447ac9fb9cSChristoph Hellwig mutex_lock(&sbi->vh_mutex);
445b0cd38c7SErnesto A. Fernandez inode = hfsplus_new_inode(dir->i_sb, dir, S_IFLNK | S_IRWXUGO);
4461da177e4SLinus Torvalds if (!inode)
4477ac9fb9cSChristoph Hellwig goto out;
4481da177e4SLinus Torvalds
4491da177e4SLinus Torvalds res = page_symlink(inode, symname, strlen(symname) + 1);
450f17c89bfSChristoph Hellwig if (res)
451f17c89bfSChristoph Hellwig goto out_err;
452f17c89bfSChristoph Hellwig
453f17c89bfSChristoph Hellwig res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
454f17c89bfSChristoph Hellwig if (res)
455f17c89bfSChristoph Hellwig goto out_err;
456f17c89bfSChristoph Hellwig
457f168d9fdSErnesto A. Fernández res = hfsplus_init_security(inode, dir, &dentry->d_name);
458324ef39aSVyacheslav Dubeyko if (res == -EOPNOTSUPP)
459324ef39aSVyacheslav Dubeyko res = 0; /* Operation is not supported. */
460324ef39aSVyacheslav Dubeyko else if (res) {
461324ef39aSVyacheslav Dubeyko /* Try to delete anyway without error analysis. */
462324ef39aSVyacheslav Dubeyko hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
463324ef39aSVyacheslav Dubeyko goto out_err;
464324ef39aSVyacheslav Dubeyko }
465324ef39aSVyacheslav Dubeyko
466f17c89bfSChristoph Hellwig hfsplus_instantiate(dentry, inode, inode->i_ino);
467f17c89bfSChristoph Hellwig mark_inode_dirty(inode);
4687ac9fb9cSChristoph Hellwig goto out;
469f17c89bfSChristoph Hellwig
470f17c89bfSChristoph Hellwig out_err:
4716d6b77f1SMiklos Szeredi clear_nlink(inode);
4721da177e4SLinus Torvalds hfsplus_delete_inode(inode);
4731da177e4SLinus Torvalds iput(inode);
4747ac9fb9cSChristoph Hellwig out:
4757ac9fb9cSChristoph Hellwig mutex_unlock(&sbi->vh_mutex);
4761da177e4SLinus Torvalds return res;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds
hfsplus_mknod(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)4795ebb29beSChristian Brauner static int hfsplus_mknod(struct mnt_idmap *idmap, struct inode *dir,
480549c7297SChristian Brauner struct dentry *dentry, umode_t mode, dev_t rdev)
4811da177e4SLinus Torvalds {
4827ac9fb9cSChristoph Hellwig struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
4831da177e4SLinus Torvalds struct inode *inode;
48427a4e388SChengyu Song int res = -ENOMEM;
4851da177e4SLinus Torvalds
4867ac9fb9cSChristoph Hellwig mutex_lock(&sbi->vh_mutex);
487b0cd38c7SErnesto A. Fernandez inode = hfsplus_new_inode(dir->i_sb, dir, mode);
4881da177e4SLinus Torvalds if (!inode)
4897ac9fb9cSChristoph Hellwig goto out;
4901da177e4SLinus Torvalds
49190e61690SChristoph Hellwig if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
49290e61690SChristoph Hellwig init_special_inode(inode, mode, rdev);
49390e61690SChristoph Hellwig
4941da177e4SLinus Torvalds res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
495324ef39aSVyacheslav Dubeyko if (res)
496324ef39aSVyacheslav Dubeyko goto failed_mknod;
497324ef39aSVyacheslav Dubeyko
498f168d9fdSErnesto A. Fernández res = hfsplus_init_security(inode, dir, &dentry->d_name);
499324ef39aSVyacheslav Dubeyko if (res == -EOPNOTSUPP)
500324ef39aSVyacheslav Dubeyko res = 0; /* Operation is not supported. */
501324ef39aSVyacheslav Dubeyko else if (res) {
502324ef39aSVyacheslav Dubeyko /* Try to delete anyway without error analysis. */
503324ef39aSVyacheslav Dubeyko hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
504324ef39aSVyacheslav Dubeyko goto failed_mknod;
5051da177e4SLinus Torvalds }
50630d3abbeSChristoph Hellwig
5071da177e4SLinus Torvalds hfsplus_instantiate(dentry, inode, inode->i_ino);
5081da177e4SLinus Torvalds mark_inode_dirty(inode);
509324ef39aSVyacheslav Dubeyko goto out;
510324ef39aSVyacheslav Dubeyko
511324ef39aSVyacheslav Dubeyko failed_mknod:
512324ef39aSVyacheslav Dubeyko clear_nlink(inode);
513324ef39aSVyacheslav Dubeyko hfsplus_delete_inode(inode);
514324ef39aSVyacheslav Dubeyko iput(inode);
5157ac9fb9cSChristoph Hellwig out:
5167ac9fb9cSChristoph Hellwig mutex_unlock(&sbi->vh_mutex);
5177ac9fb9cSChristoph Hellwig return res;
5181da177e4SLinus Torvalds }
5191da177e4SLinus Torvalds
hfsplus_create(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode,bool excl)5206c960e68SChristian Brauner static int hfsplus_create(struct mnt_idmap *idmap, struct inode *dir,
521549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl)
52230d3abbeSChristoph Hellwig {
5235ebb29beSChristian Brauner return hfsplus_mknod(&nop_mnt_idmap, dir, dentry, mode, 0);
52430d3abbeSChristoph Hellwig }
52530d3abbeSChristoph Hellwig
hfsplus_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)526c54bd91eSChristian Brauner static int hfsplus_mkdir(struct mnt_idmap *idmap, struct inode *dir,
527549c7297SChristian Brauner struct dentry *dentry, umode_t mode)
52830d3abbeSChristoph Hellwig {
5295ebb29beSChristian Brauner return hfsplus_mknod(&nop_mnt_idmap, dir, dentry, mode | S_IFDIR, 0);
53030d3abbeSChristoph Hellwig }
53130d3abbeSChristoph Hellwig
hfsplus_rename(struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)532e18275aeSChristian Brauner static int hfsplus_rename(struct mnt_idmap *idmap,
533549c7297SChristian Brauner struct inode *old_dir, struct dentry *old_dentry,
534f03b8ad8SMiklos Szeredi struct inode *new_dir, struct dentry *new_dentry,
535f03b8ad8SMiklos Szeredi unsigned int flags)
5361da177e4SLinus Torvalds {
5371da177e4SLinus Torvalds int res;
5381da177e4SLinus Torvalds
539f03b8ad8SMiklos Szeredi if (flags & ~RENAME_NOREPLACE)
540f03b8ad8SMiklos Szeredi return -EINVAL;
541f03b8ad8SMiklos Szeredi
5421da177e4SLinus Torvalds /* Unlink destination if it already exists */
5432b0143b5SDavid Howells if (d_really_is_positive(new_dentry)) {
544e36cb0b8SDavid Howells if (d_is_dir(new_dentry))
54540de9a7cSChristoph Hellwig res = hfsplus_rmdir(new_dir, new_dentry);
546e3911785SSage Weil else
5471da177e4SLinus Torvalds res = hfsplus_unlink(new_dir, new_dentry);
5481da177e4SLinus Torvalds if (res)
5491da177e4SLinus Torvalds return res;
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds
5521da177e4SLinus Torvalds res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
5531da177e4SLinus Torvalds old_dir, &old_dentry->d_name,
5541da177e4SLinus Torvalds new_dir, &new_dentry->d_name);
5551da177e4SLinus Torvalds if (!res)
5561da177e4SLinus Torvalds new_dentry->d_fsdata = old_dentry->d_fsdata;
5571da177e4SLinus Torvalds return res;
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds
56092e1d5beSArjan van de Ven const struct inode_operations hfsplus_dir_inode_operations = {
5611da177e4SLinus Torvalds .lookup = hfsplus_lookup,
5621da177e4SLinus Torvalds .create = hfsplus_create,
5631da177e4SLinus Torvalds .link = hfsplus_link,
5641da177e4SLinus Torvalds .unlink = hfsplus_unlink,
5651da177e4SLinus Torvalds .mkdir = hfsplus_mkdir,
5661da177e4SLinus Torvalds .rmdir = hfsplus_rmdir,
5671da177e4SLinus Torvalds .symlink = hfsplus_symlink,
5681da177e4SLinus Torvalds .mknod = hfsplus_mknod,
5691da177e4SLinus Torvalds .rename = hfsplus_rename,
570f93ca1edSErnesto A. Fernández .getattr = hfsplus_getattr,
571324ef39aSVyacheslav Dubeyko .listxattr = hfsplus_listxattr,
5729cbae748SMiklos Szeredi .fileattr_get = hfsplus_fileattr_get,
5739cbae748SMiklos Szeredi .fileattr_set = hfsplus_fileattr_set,
5741da177e4SLinus Torvalds };
5751da177e4SLinus Torvalds
5764b6f5d20SArjan van de Ven const struct file_operations hfsplus_dir_operations = {
577eb29d66dSChristoph Hellwig .fsync = hfsplus_file_fsync,
5781da177e4SLinus Torvalds .read = generic_read_dir,
579323ee8fcSAl Viro .iterate_shared = hfsplus_readdir,
5807cc4bcc6SArnd Bergmann .unlocked_ioctl = hfsplus_ioctl,
5811da177e4SLinus Torvalds .llseek = generic_file_llseek,
5821da177e4SLinus Torvalds .release = hfsplus_dir_release,
5831da177e4SLinus Torvalds };
584