blob: ca64a75f12b3a83064f19b1ea46f321710f73942 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/hfsplus/dir.c
3 *
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
7 *
8 * Handling of directories
9 */
10
11#include <linux/errno.h>
12#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
14#include <linux/random.h>
Hin-Tak Leung017f8da2014-06-06 14:36:21 -070015#include <linux/nls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include "hfsplus_fs.h"
18#include "hfsplus_raw.h"
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -080019#include "xattr.h"
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -070020#include "acl.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) &&
80 (entry.file.create_date ==
81 HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->
82 create_date ||
83 entry.file.create_date ==
David Howells2b0143b2015-03-17 22:25:59 +000084 HFSPLUS_I(d_inode(sb->s_root))->
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020085 create_date) &&
86 HFSPLUS_SB(sb)->hidden_dir) {
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:
124 d_add(dentry, inode);
125 return NULL;
126fail:
127 hfs_find_exit(&fd);
128 return ERR_PTR(err);
129}
130
Al Viroe72514e2013-05-22 14:59:39 -0400131static int hfsplus_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Al Viroe72514e2013-05-22 14:59:39 -0400133 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct super_block *sb = inode->i_sb;
135 int len, err;
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700136 char *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 hfsplus_cat_entry entry;
138 struct hfs_find_data fd;
139 struct hfsplus_readdir_data *rd;
140 u16 type;
141
Al Viroe72514e2013-05-22 14:59:39 -0400142 if (file->f_pos >= inode->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return 0;
144
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +0400145 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
146 if (err)
147 return err;
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700148 strbuf = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN + 1, GFP_KERNEL);
149 if (!strbuf) {
150 err = -ENOMEM;
151 goto out;
152 }
Sougata Santra89ac9b42014-12-18 16:17:12 -0800153 hfsplus_cat_build_key_with_cnid(sb, fd.search_key, inode->i_ino);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800154 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (err)
156 goto out;
157
Al Viroe72514e2013-05-22 14:59:39 -0400158 if (ctx->pos == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* This is completely artificial... */
Al Viroe72514e2013-05-22 14:59:39 -0400160 if (!dir_emit_dot(file, ctx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 goto out;
Al Viroe72514e2013-05-22 14:59:39 -0400162 ctx->pos = 1;
163 }
164 if (ctx->pos == 1) {
Greg Kroah-Hartman6f24f892012-05-04 12:09:39 -0700165 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
166 err = -EIO;
167 goto out;
168 }
169
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200170 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
171 fd.entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
Joe Perchesd6142672013-04-30 15:27:55 -0700173 pr_err("bad catalog folder thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 err = -EIO;
175 goto out;
176 }
177 if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
Joe Perchesd6142672013-04-30 15:27:55 -0700178 pr_err("truncated catalog thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 err = -EIO;
180 goto out;
181 }
Al Viroe72514e2013-05-22 14:59:39 -0400182 if (!dir_emit(ctx, "..", 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 be32_to_cpu(entry.thread.parentID), DT_DIR))
184 goto out;
Al Viroe72514e2013-05-22 14:59:39 -0400185 ctx->pos = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
Al Viroe72514e2013-05-22 14:59:39 -0400187 if (ctx->pos >= inode->i_size)
188 goto out;
189 err = hfs_brec_goto(&fd, ctx->pos - 1);
190 if (err)
191 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 for (;;) {
193 if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
Joe Perchesd6142672013-04-30 15:27:55 -0700194 pr_err("walked past end of dir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 err = -EIO;
196 goto out;
197 }
Greg Kroah-Hartman6f24f892012-05-04 12:09:39 -0700198
199 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
200 err = -EIO;
201 goto out;
202 }
203
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200204 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
205 fd.entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 type = be16_to_cpu(entry.type);
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700207 len = NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
209 if (err)
210 goto out;
211 if (type == HFSPLUS_FOLDER) {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200212 if (fd.entrylength <
213 sizeof(struct hfsplus_cat_folder)) {
Joe Perchesd6142672013-04-30 15:27:55 -0700214 pr_err("small dir entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 err = -EIO;
216 goto out;
217 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200218 if (HFSPLUS_SB(sb)->hidden_dir &&
219 HFSPLUS_SB(sb)->hidden_dir->i_ino ==
220 be32_to_cpu(entry.folder.id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 goto next;
Al Viroe72514e2013-05-22 14:59:39 -0400222 if (!dir_emit(ctx, strbuf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 be32_to_cpu(entry.folder.id), DT_DIR))
224 break;
225 } else if (type == HFSPLUS_FILE) {
Sergei Antonov97a62ea2014-06-06 14:36:24 -0700226 u16 mode;
227 unsigned type = DT_UNKNOWN;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
Joe Perchesd6142672013-04-30 15:27:55 -0700230 pr_err("small file entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 err = -EIO;
232 goto out;
233 }
Sergei Antonov97a62ea2014-06-06 14:36:24 -0700234
235 mode = be16_to_cpu(entry.file.permissions.mode);
236 if (S_ISREG(mode))
237 type = DT_REG;
238 else if (S_ISLNK(mode))
239 type = DT_LNK;
240 else if (S_ISFIFO(mode))
241 type = DT_FIFO;
242 else if (S_ISCHR(mode))
243 type = DT_CHR;
244 else if (S_ISBLK(mode))
245 type = DT_BLK;
246 else if (S_ISSOCK(mode))
247 type = DT_SOCK;
248
Al Viroe72514e2013-05-22 14:59:39 -0400249 if (!dir_emit(ctx, strbuf, len,
Sergei Antonov97a62ea2014-06-06 14:36:24 -0700250 be32_to_cpu(entry.file.id), type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 break;
252 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700253 pr_err("bad catalog entry type\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 err = -EIO;
255 goto out;
256 }
Anton Salikhmetov20b76432010-12-16 18:08:40 +0200257next:
Al Viroe72514e2013-05-22 14:59:39 -0400258 ctx->pos++;
259 if (ctx->pos >= inode->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 goto out;
261 err = hfs_brec_goto(&fd, 1);
262 if (err)
263 goto out;
264 }
Al Viroe72514e2013-05-22 14:59:39 -0400265 rd = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (!rd) {
267 rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
268 if (!rd) {
269 err = -ENOMEM;
270 goto out;
271 }
Al Viroe72514e2013-05-22 14:59:39 -0400272 file->private_data = rd;
273 rd->file = file;
Al Viro323ee8f2016-05-12 20:02:09 -0400274 spin_lock(&HFSPLUS_I(inode)->open_dir_lock);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200275 list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
Al Viro323ee8f2016-05-12 20:02:09 -0400276 spin_unlock(&HFSPLUS_I(inode)->open_dir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Al Viro323ee8f2016-05-12 20:02:09 -0400278 /*
279 * Can be done after the list insertion; exclusion with
280 * hfsplus_delete_cat() is provided by directory lock.
281 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
283out:
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700284 kfree(strbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 hfs_find_exit(&fd);
286 return err;
287}
288
289static int hfsplus_dir_release(struct inode *inode, struct file *file)
290{
291 struct hfsplus_readdir_data *rd = file->private_data;
292 if (rd) {
Al Viro323ee8f2016-05-12 20:02:09 -0400293 spin_lock(&HFSPLUS_I(inode)->open_dir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 list_del(&rd->list);
Al Viro323ee8f2016-05-12 20:02:09 -0400295 spin_unlock(&HFSPLUS_I(inode)->open_dir_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 kfree(rd);
297 }
298 return 0;
299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
302 struct dentry *dst_dentry)
303{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200304 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000305 struct inode *inode = d_inode(src_dentry);
306 struct inode *src_dir = d_inode(src_dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct qstr str;
308 char name[32];
309 u32 cnid, id;
310 int res;
311
312 if (HFSPLUS_IS_RSRC(inode))
313 return -EPERM;
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400314 if (!S_ISREG(inode->i_mode))
315 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200317 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
319 for (;;) {
320 get_random_bytes(&id, sizeof(cnid));
321 id &= 0x3fffffff;
322 str.name = name;
323 str.len = sprintf(name, "iNode%d", id);
324 res = hfsplus_rename_cat(inode->i_ino,
325 src_dir, &src_dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200326 sbi->hidden_dir, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!res)
328 break;
329 if (res != -EEXIST)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200330 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400332 HFSPLUS_I(inode)->linkid = id;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200333 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 src_dentry->d_fsdata = (void *)(unsigned long)cnid;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200335 res = hfsplus_create_cat(cnid, src_dir,
336 &src_dentry->d_name, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (res)
338 /* panic? */
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200339 goto out;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200340 sbi->file_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200342 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
344 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200345 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Dave Hansend8c76e62006-09-30 23:29:04 -0700347 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 hfsplus_instantiate(dst_dentry, inode, cnid);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400349 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 inode->i_ctime = CURRENT_TIME_SEC;
351 mark_inode_dirty(inode);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200352 sbi->file_count++;
Artem Bityutskiy9e6c5822012-07-12 17:26:31 +0300353 hfsplus_mark_mdb_dirty(dst_dir->i_sb);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200354out:
355 mutex_unlock(&sbi->vh_mutex);
356 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
360{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200361 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000362 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct qstr str;
364 char name[32];
365 u32 cnid;
366 int res;
367
368 if (HFSPLUS_IS_RSRC(inode))
369 return -EPERM;
370
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200371 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 cnid = (u32)(unsigned long)dentry->d_fsdata;
373 if (inode->i_ino == cnid &&
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200374 atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 str.name = name;
376 str.len = sprintf(name, "temp%lu", inode->i_ino);
377 res = hfsplus_rename_cat(inode->i_ino,
378 dir, &dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200379 sbi->hidden_dir, &str);
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200380 if (!res) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 inode->i_flags |= S_DEAD;
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200382 drop_nlink(inode);
383 }
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200384 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386 res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
387 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800390 if (inode->i_nlink > 0)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700391 drop_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200392 if (inode->i_ino == cnid)
Dave Hansence71ec32006-09-30 23:29:06 -0700393 clear_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200394 if (!inode->i_nlink) {
395 if (inode->i_ino != cnid) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200396 sbi->file_count--;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200397 if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Roman Zippel76b0c262008-04-09 17:44:07 +0200398 res = hfsplus_delete_cat(inode->i_ino,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200399 sbi->hidden_dir,
Roman Zippel76b0c262008-04-09 17:44:07 +0200400 NULL);
401 if (!res)
402 hfsplus_delete_inode(inode);
403 } else
404 inode->i_flags |= S_DEAD;
405 } else
406 hfsplus_delete_inode(inode);
407 } else
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200408 sbi->file_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 inode->i_ctime = CURRENT_TIME_SEC;
410 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200411out:
412 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return res;
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
417{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200418 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
David Howells2b0143b2015-03-17 22:25:59 +0000419 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 int res;
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (inode->i_size != 2)
423 return -ENOTEMPTY;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200424
425 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
427 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200428 goto out;
Dave Hansence71ec32006-09-30 23:29:06 -0700429 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 inode->i_ctime = CURRENT_TIME_SEC;
431 hfsplus_delete_inode(inode);
432 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200433out:
434 mutex_unlock(&sbi->vh_mutex);
435 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
438static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
439 const char *symname)
440{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200441 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 struct inode *inode;
Chengyu Song27a4e382015-04-16 12:47:12 -0700443 int res = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200445 mutex_lock(&sbi->vh_mutex);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200446 inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200448 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 res = page_symlink(inode, symname, strlen(symname) + 1);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200451 if (res)
452 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200455 if (res)
456 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800458 res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
459 if (res == -EOPNOTSUPP)
460 res = 0; /* Operation is not supported. */
461 else if (res) {
462 /* Try to delete anyway without error analysis. */
463 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
464 goto out_err;
465 }
466
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200467 hfsplus_instantiate(dentry, inode, inode->i_ino);
468 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200469 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200471out_err:
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200472 clear_nlink(inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200473 hfsplus_delete_inode(inode);
474 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200475out:
476 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return res;
478}
479
480static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400481 umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200483 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct inode *inode;
Chengyu Song27a4e382015-04-16 12:47:12 -0700485 int res = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200487 mutex_lock(&sbi->vh_mutex);
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200488 inode = hfsplus_new_inode(dir->i_sb, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200490 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Christoph Hellwig90e61692010-10-14 09:54:39 -0400492 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
493 init_special_inode(inode, mode, rdev);
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800496 if (res)
497 goto failed_mknod;
498
499 res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
500 if (res == -EOPNOTSUPP)
501 res = 0; /* Operation is not supported. */
502 else if (res) {
503 /* Try to delete anyway without error analysis. */
504 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
505 goto failed_mknod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 hfsplus_instantiate(dentry, inode, inode->i_ino);
509 mark_inode_dirty(inode);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800510 goto out;
511
512failed_mknod:
513 clear_nlink(inode);
514 hfsplus_delete_inode(inode);
515 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200516out:
517 mutex_unlock(&sbi->vh_mutex);
518 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Al Viro4acdaf22011-07-26 01:42:34 -0400521static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400522 bool excl)
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200523{
524 return hfsplus_mknod(dir, dentry, mode, 0);
525}
526
Al Viro18bb1db2011-07-26 01:41:39 -0400527static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200528{
529 return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
530}
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200533 struct inode *new_dir, struct dentry *new_dentry,
534 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 int res;
537
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200538 if (flags & ~RENAME_NOREPLACE)
539 return -EINVAL;
540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* Unlink destination if it already exists */
David Howells2b0143b2015-03-17 22:25:59 +0000542 if (d_really_is_positive(new_dentry)) {
David Howellse36cb0b2015-01-29 12:02:35 +0000543 if (d_is_dir(new_dentry))
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200544 res = hfsplus_rmdir(new_dir, new_dentry);
Sage Weile3911782011-05-27 13:42:06 -0700545 else
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200546 res = hfsplus_unlink(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (res)
548 return res;
549 }
550
551 res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
552 old_dir, &old_dentry->d_name,
553 new_dir, &new_dentry->d_name);
554 if (!res)
555 new_dentry->d_fsdata = old_dentry->d_fsdata;
556 return res;
557}
558
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800559const struct inode_operations hfsplus_dir_inode_operations = {
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800560 .lookup = hfsplus_lookup,
561 .create = hfsplus_create,
562 .link = hfsplus_link,
563 .unlink = hfsplus_unlink,
564 .mkdir = hfsplus_mkdir,
565 .rmdir = hfsplus_rmdir,
566 .symlink = hfsplus_symlink,
567 .mknod = hfsplus_mknod,
Miklos Szeredif03b8ad2016-09-27 11:03:57 +0200568 .rename2 = hfsplus_rename,
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800569 .setxattr = generic_setxattr,
570 .getxattr = generic_getxattr,
571 .listxattr = hfsplus_listxattr,
Christoph Hellwigb168fff2014-01-29 23:59:19 -0800572 .removexattr = generic_removexattr,
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -0700573#ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
574 .get_acl = hfsplus_get_posix_acl,
Christoph Hellwigb0a7ab52013-12-20 05:16:46 -0800575 .set_acl = hfsplus_set_posix_acl,
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -0700576#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577};
578
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800579const struct file_operations hfsplus_dir_operations = {
Christoph Hellwigeb29d662010-11-23 14:38:10 +0100580 .fsync = hfsplus_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 .read = generic_read_dir,
Al Viro323ee8f2016-05-12 20:02:09 -0400582 .iterate_shared = hfsplus_readdir,
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +0200583 .unlocked_ioctl = hfsplus_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 .llseek = generic_file_llseek,
585 .release = hfsplus_dir_release,
586};