blob: 29a9dcfbe81f72e24efea8f683fed86b955123b3 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/hfsplus/dir.c
4 *
5 * Copyright (C) 2001
6 * Brad Boyer (flar@allandria.com)
7 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 *
9 * Handling of directories
10 */
11
12#include <linux/errno.h>
13#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/slab.h>
15#include <linux/random.h>
Hin-Tak Leung017f8da2014-06-06 14:36:21 -070016#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include "hfsplus_fs.h"
19#include "hfsplus_raw.h"
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -080020#include "xattr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22static inline void hfsplus_instantiate(struct dentry *dentry,
23 struct inode *inode, u32 cnid)
24{
25 dentry->d_fsdata = (void *)(unsigned long)cnid;
26 d_instantiate(dentry, inode);
27}
28
29/* Find the entry inside dir named dentry->d_name */
30static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -040031 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 struct inode *inode = NULL;
34 struct hfs_find_data fd;
35 struct super_block *sb;
36 hfsplus_cat_entry entry;
37 int err;
38 u32 cnid, linkid = 0;
39 u16 type;
40
41 sb = dir->i_sb;
Duane Griffind45bce82007-07-15 23:41:23 -070042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 dentry->d_fsdata = NULL;
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +040044 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
45 if (err)
46 return ERR_PTR(err);
Sougata Santra89ac9b42014-12-18 16:17:12 -080047 err = hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino,
48 &dentry->d_name);
49 if (unlikely(err < 0))
50 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051again:
52 err = hfs_brec_read(&fd, &entry, sizeof(entry));
53 if (err) {
54 if (err == -ENOENT) {
55 hfs_find_exit(&fd);
56 /* No such entry */
57 inode = NULL;
58 goto out;
59 }
60 goto fail;
61 }
62 type = be16_to_cpu(entry.type);
63 if (type == HFSPLUS_FOLDER) {
64 if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
65 err = -EIO;
66 goto fail;
67 }
68 cnid = be32_to_cpu(entry.folder.id);
69 dentry->d_fsdata = (void *)(unsigned long)cnid;
70 } else if (type == HFSPLUS_FILE) {
71 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
72 err = -EIO;
73 goto fail;
74 }
75 cnid = be32_to_cpu(entry.file.id);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020076 if (entry.file.user_info.fdType ==
77 cpu_to_be32(HFSP_HARDLINK_TYPE) &&
78 entry.file.user_info.fdCreator ==
79 cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
Ernesto A. Fernándeza7ec7a42018-08-23 17:00:25 -070080 HFSPLUS_SB(sb)->hidden_dir &&
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020081 (entry.file.create_date ==
82 HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->
83 create_date ||
84 entry.file.create_date ==
David Howells2b0143b2015-03-17 22:25:59 +000085 HFSPLUS_I(d_inode(sb->s_root))->
Ernesto A. Fernándeza7ec7a42018-08-23 17:00:25 -070086 create_date)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 struct qstr str;
88 char name[32];
89
90 if (dentry->d_fsdata) {
Roman Zippelaf8c85b2006-01-18 17:43:10 -080091 /*
92 * We found a link pointing to another link,
93 * so ignore it and treat it as regular file.
94 */
95 cnid = (unsigned long)dentry->d_fsdata;
96 linkid = 0;
97 } else {
98 dentry->d_fsdata = (void *)(unsigned long)cnid;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020099 linkid =
100 be32_to_cpu(entry.file.permissions.dev);
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800101 str.len = sprintf(name, "iNode%d", linkid);
102 str.name = name;
Sougata Santra89ac9b42014-12-18 16:17:12 -0800103 err = hfsplus_cat_build_key(sb, fd.search_key,
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200104 HFSPLUS_SB(sb)->hidden_dir->i_ino,
105 &str);
Sougata Santra89ac9b42014-12-18 16:17:12 -0800106 if (unlikely(err < 0))
107 goto fail;
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800108 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 } else if (!dentry->d_fsdata)
111 dentry->d_fsdata = (void *)(unsigned long)cnid;
112 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700113 pr_err("invalid catalog entry type in lookup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 err = -EIO;
115 goto fail;
116 }
117 hfs_find_exit(&fd);
David Howells63525392008-02-07 00:15:40 -0800118 inode = hfsplus_iget(dir->i_sb, cnid);
119 if (IS_ERR(inode))
120 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (S_ISREG(inode->i_mode))
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400122 HFSPLUS_I(inode)->linkid = linkid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123out:
Al Viro293542d2018-05-03 09:49:02 -0400124 return d_splice_alias(inode, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125fail:
126 hfs_find_exit(&fd);
127 return ERR_PTR(err);
128}
129
Al Viroe72514e2013-05-22 14:59:39 -0400130static int hfsplus_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Al Viroe72514e2013-05-22 14:59:39 -0400132 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct super_block *sb = inode->i_sb;
134 int len, err;
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700135 char *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 hfsplus_cat_entry entry;
137 struct hfs_find_data fd;
138 struct hfsplus_readdir_data *rd;
139 u16 type;
140
Al Viroe72514e2013-05-22 14:59:39 -0400141 if (file->f_pos >= inode->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return 0;
143
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +0400144 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
145 if (err)
146 return err;
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700147 strbuf = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN + 1, GFP_KERNEL);
148 if (!strbuf) {
149 err = -ENOMEM;
150 goto out;
151 }
Sougata Santra89ac9b42014-12-18 16:17:12 -0800152 hfsplus_cat_build_key_with_cnid(sb, fd.search_key, inode->i_ino);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800153 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (err)
155 goto out;
156
Al Viroe72514e2013-05-22 14:59:39 -0400157 if (ctx->pos == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* This is completely artificial... */
Al Viroe72514e2013-05-22 14:59:39 -0400159 if (!dir_emit_dot(file, ctx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 goto out;
Al Viroe72514e2013-05-22 14:59:39 -0400161 ctx->pos = 1;
162 }
163 if (ctx->pos == 1) {
Greg Kroah-Hartman6f24f892012-05-04 12:09:39 -0700164 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
165 err = -EIO;
166 goto out;
167 }
168
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200169 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
170 fd.entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
Joe Perchesd6142672013-04-30 15:27:55 -0700172 pr_err("bad catalog folder thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 err = -EIO;
174 goto out;
175 }
176 if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
Joe Perchesd6142672013-04-30 15:27:55 -0700177 pr_err("truncated catalog thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 err = -EIO;
179 goto out;
180 }
Al Viroe72514e2013-05-22 14:59:39 -0400181 if (!dir_emit(ctx, "..", 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 be32_to_cpu(entry.thread.parentID), DT_DIR))
183 goto out;
Al Viroe72514e2013-05-22 14:59:39 -0400184 ctx->pos = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
Al Viroe72514e2013-05-22 14:59:39 -0400186 if (ctx->pos >= inode->i_size)
187 goto out;
188 err = hfs_brec_goto(&fd, ctx->pos - 1);
189 if (err)
190 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 for (;;) {
192 if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
Joe Perchesd6142672013-04-30 15:27:55 -0700193 pr_err("walked past end of dir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 err = -EIO;
195 goto out;
196 }
Greg Kroah-Hartman6f24f892012-05-04 12:09:39 -0700197
198 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
199 err = -EIO;
200 goto out;
201 }
202
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200203 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
204 fd.entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 type = be16_to_cpu(entry.type);
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700206 len = NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
208 if (err)
209 goto out;
210 if (type == HFSPLUS_FOLDER) {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200211 if (fd.entrylength <
212 sizeof(struct hfsplus_cat_folder)) {
Joe Perchesd6142672013-04-30 15:27:55 -0700213 pr_err("small dir entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 err = -EIO;
215 goto out;
216 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200217 if (HFSPLUS_SB(sb)->hidden_dir &&
218 HFSPLUS_SB(sb)->hidden_dir->i_ino ==
219 be32_to_cpu(entry.folder.id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 goto next;
Al Viroe72514e2013-05-22 14:59:39 -0400221 if (!dir_emit(ctx, strbuf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 be32_to_cpu(entry.folder.id), DT_DIR))
223 break;
224 } else if (type == HFSPLUS_FILE) {
Sergei Antonov97a62ea2014-06-06 14:36:24 -0700225 u16 mode;
226 unsigned type = DT_UNKNOWN;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
Joe Perchesd6142672013-04-30 15:27:55 -0700229 pr_err("small file entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 err = -EIO;
231 goto out;
232 }
Sergei Antonov97a62ea2014-06-06 14:36:24 -0700233
234 mode = be16_to_cpu(entry.file.permissions.mode);
235 if (S_ISREG(mode))
236 type = DT_REG;
237 else if (S_ISLNK(mode))
238 type = DT_LNK;
239 else if (S_ISFIFO(mode))
240 type = DT_FIFO;
241 else if (S_ISCHR(mode))
242 type = DT_CHR;
243 else if (S_ISBLK(mode))
244 type = DT_BLK;
245 else if (S_ISSOCK(mode))
246 type = DT_SOCK;
247
Al Viroe72514e2013-05-22 14:59:39 -0400248 if (!dir_emit(ctx, strbuf, len,
Sergei Antonov97a62ea2014-06-06 14:36:24 -0700249 be32_to_cpu(entry.file.id), type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700252 pr_err("bad catalog entry type\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 err = -EIO;
254 goto out;
255 }
Anton Salikhmetov20b76432010-12-16 18:08:40 +0200256next:
Al Viroe72514e2013-05-22 14:59:39 -0400257 ctx->pos++;
258 if (ctx->pos >= inode->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto out;
260 err = hfs_brec_goto(&fd, 1);
261 if (err)
262 goto out;
263 }
Al Viroe72514e2013-05-22 14:59:39 -0400264 rd = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (!rd) {
266 rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
267 if (!rd) {
268 err = -ENOMEM;
269 goto out;
270 }
Al Viroe72514e2013-05-22 14:59:39 -0400271 file->private_data = rd;
272 rd->file = file;
Al Viro323ee8f2016-05-12 20:02:09 -0400273 spin_lock(&HFSPLUS_I(inode)->open_dir_lock);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200274 list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
Al Viro323ee8f2016-05-12 20:02:09 -0400275 spin_unlock(&HFSPLUS_I(inode)->open_dir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 }
Al Viro323ee8f2016-05-12 20:02:09 -0400277 /*
278 * Can be done after the list insertion; exclusion with
279 * hfsplus_delete_cat() is provided by directory lock.
280 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
282out:
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700283 kfree(strbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 hfs_find_exit(&fd);
285 return err;
286}
287
288static int hfsplus_dir_release(struct inode *inode, struct file *file)
289{
290 struct hfsplus_readdir_data *rd = file->private_data;
291 if (rd) {
Al Viro323ee8f2016-05-12 20:02:09 -0400292 spin_lock(&HFSPLUS_I(inode)->open_dir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 list_del(&rd->list);
Al Viro323ee8f2016-05-12 20:02:09 -0400294 spin_unlock(&HFSPLUS_I(inode)->open_dir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 kfree(rd);
296 }
297 return 0;
298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
301 struct dentry *dst_dentry)
302{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200303 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000304 struct inode *inode = d_inode(src_dentry);
305 struct inode *src_dir = d_inode(src_dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct qstr str;
307 char name[32];
308 u32 cnid, id;
309 int res;
310
311 if (HFSPLUS_IS_RSRC(inode))
312 return -EPERM;
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400313 if (!S_ISREG(inode->i_mode))
314 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200316 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
318 for (;;) {
319 get_random_bytes(&id, sizeof(cnid));
320 id &= 0x3fffffff;
321 str.name = name;
322 str.len = sprintf(name, "iNode%d", id);
323 res = hfsplus_rename_cat(inode->i_ino,
324 src_dir, &src_dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200325 sbi->hidden_dir, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!res)
327 break;
328 if (res != -EEXIST)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200329 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400331 HFSPLUS_I(inode)->linkid = id;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200332 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 src_dentry->d_fsdata = (void *)(unsigned long)cnid;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200334 res = hfsplus_create_cat(cnid, src_dir,
335 &src_dentry->d_name, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (res)
337 /* panic? */
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200338 goto out;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200339 sbi->file_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200341 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
343 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200344 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Dave Hansend8c76e62006-09-30 23:29:04 -0700346 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 hfsplus_instantiate(dst_dentry, inode, cnid);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400348 ihold(inode);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700349 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 mark_inode_dirty(inode);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200351 sbi->file_count++;
Artem Bityutskiy9e6c5822012-07-12 17:26:31 +0300352 hfsplus_mark_mdb_dirty(dst_dir->i_sb);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200353out:
354 mutex_unlock(&sbi->vh_mutex);
355 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
359{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200360 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000361 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct qstr str;
363 char name[32];
364 u32 cnid;
365 int res;
366
367 if (HFSPLUS_IS_RSRC(inode))
368 return -EPERM;
369
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200370 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 cnid = (u32)(unsigned long)dentry->d_fsdata;
372 if (inode->i_ino == cnid &&
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200373 atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 str.name = name;
375 str.len = sprintf(name, "temp%lu", inode->i_ino);
376 res = hfsplus_rename_cat(inode->i_ino,
377 dir, &dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200378 sbi->hidden_dir, &str);
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200379 if (!res) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 inode->i_flags |= S_DEAD;
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200381 drop_nlink(inode);
382 }
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200383 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385 res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
386 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200387 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800389 if (inode->i_nlink > 0)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700390 drop_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200391 if (inode->i_ino == cnid)
Dave Hansence71ec32006-09-30 23:29:06 -0700392 clear_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200393 if (!inode->i_nlink) {
394 if (inode->i_ino != cnid) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200395 sbi->file_count--;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200396 if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Roman Zippel76b0c262008-04-09 17:44:07 +0200397 res = hfsplus_delete_cat(inode->i_ino,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200398 sbi->hidden_dir,
Roman Zippel76b0c262008-04-09 17:44:07 +0200399 NULL);
400 if (!res)
401 hfsplus_delete_inode(inode);
402 } else
403 inode->i_flags |= S_DEAD;
404 } else
405 hfsplus_delete_inode(inode);
406 } else
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200407 sbi->file_count--;
Deepa Dinamani02027d42016-09-14 07:48:05 -0700408 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200410out:
411 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return res;
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
416{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200417 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000418 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 int res;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (inode->i_size != 2)
422 return -ENOTEMPTY;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200423
424 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
426 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200427 goto out;
Dave Hansence71ec32006-09-30 23:29:06 -0700428 clear_nlink(inode);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700429 inode->i_ctime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 hfsplus_delete_inode(inode);
431 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200432out:
433 mutex_unlock(&sbi->vh_mutex);
434 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
438 const char *symname)
439{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200440 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct inode *inode;
Chengyu Song27a4e382015-04-16 12:47:12 -0700442 int res = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200444 mutex_lock(&sbi->vh_mutex);
Ernesto A. Fernandezb0cd38c2018-02-06 15:49:02 -0800445 inode = hfsplus_new_inode(dir->i_sb, dir, S_IFLNK | S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200447 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 res = page_symlink(inode, symname, strlen(symname) + 1);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200450 if (res)
451 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200454 if (res)
455 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Ernesto A. Fernándezf168d9f2018-08-21 21:59:23 -0700457 res = hfsplus_init_security(inode, dir, &dentry->d_name);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800458 if (res == -EOPNOTSUPP)
459 res = 0; /* Operation is not supported. */
460 else if (res) {
461 /* Try to delete anyway without error analysis. */
462 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
463 goto out_err;
464 }
465
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200466 hfsplus_instantiate(dentry, inode, inode->i_ino);
467 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200468 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200470out_err:
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200471 clear_nlink(inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200472 hfsplus_delete_inode(inode);
473 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200474out:
475 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return res;
477}
478
479static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400480 umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200482 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 struct inode *inode;
Chengyu Song27a4e382015-04-16 12:47:12 -0700484 int res = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200486 mutex_lock(&sbi->vh_mutex);
Ernesto A. Fernandezb0cd38c2018-02-06 15:49:02 -0800487 inode = hfsplus_new_inode(dir->i_sb, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Christoph Hellwig90e61692010-10-14 09:54:39 -0400491 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
492 init_special_inode(inode, mode, rdev);
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800495 if (res)
496 goto failed_mknod;
497
Ernesto A. Fernándezf168d9f2018-08-21 21:59:23 -0700498 res = hfsplus_init_security(inode, dir, &dentry->d_name);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800499 if (res == -EOPNOTSUPP)
500 res = 0; /* Operation is not supported. */
501 else if (res) {
502 /* Try to delete anyway without error analysis. */
503 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
504 goto failed_mknod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 hfsplus_instantiate(dentry, inode, inode->i_ino);
508 mark_inode_dirty(inode);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800509 goto out;
510
511failed_mknod:
512 clear_nlink(inode);
513 hfsplus_delete_inode(inode);
514 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200515out:
516 mutex_unlock(&sbi->vh_mutex);
517 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Al Viro4acdaf22011-07-26 01:42:34 -0400520static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400521 bool excl)
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200522{
523 return hfsplus_mknod(dir, dentry, mode, 0);
524}
525
Al Viro18bb1db2011-07-26 01:41:39 -0400526static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200527{
528 return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200532 struct inode *new_dir, struct dentry *new_dentry,
533 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 int res;
536
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200537 if (flags & ~RENAME_NOREPLACE)
538 return -EINVAL;
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* Unlink destination if it already exists */
David Howells2b0143b2015-03-17 22:25:59 +0000541 if (d_really_is_positive(new_dentry)) {
David Howellse36cb0b2015-01-29 12:02:35 +0000542 if (d_is_dir(new_dentry))
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200543 res = hfsplus_rmdir(new_dir, new_dentry);
Sage Weile3911782011-05-27 13:42:06 -0700544 else
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200545 res = hfsplus_unlink(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (res)
547 return res;
548 }
549
550 res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
551 old_dir, &old_dentry->d_name,
552 new_dir, &new_dentry->d_name);
553 if (!res)
554 new_dentry->d_fsdata = old_dentry->d_fsdata;
555 return res;
556}
557
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800558const struct inode_operations hfsplus_dir_inode_operations = {
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800559 .lookup = hfsplus_lookup,
560 .create = hfsplus_create,
561 .link = hfsplus_link,
562 .unlink = hfsplus_unlink,
563 .mkdir = hfsplus_mkdir,
564 .rmdir = hfsplus_rmdir,
565 .symlink = hfsplus_symlink,
566 .mknod = hfsplus_mknod,
567 .rename = hfsplus_rename,
Ernesto A. Fernándezf93ca1e2019-01-03 15:27:46 -0800568 .getattr = hfsplus_getattr,
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800569 .listxattr = hfsplus_listxattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570};
571
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800572const struct file_operations hfsplus_dir_operations = {
Christoph Hellwigeb29d662010-11-23 14:38:10 +0100573 .fsync = hfsplus_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 .read = generic_read_dir,
Al Viro323ee8f2016-05-12 20:02:09 -0400575 .iterate_shared = hfsplus_readdir,
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +0200576 .unlocked_ioctl = hfsplus_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 .llseek = generic_file_llseek,
578 .release = hfsplus_dir_release,
579};