blob: 610a3260bef1d96b868c9de8920b29b41d11eddf [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name);
48again:
49 err = hfs_brec_read(&fd, &entry, sizeof(entry));
50 if (err) {
51 if (err == -ENOENT) {
52 hfs_find_exit(&fd);
53 /* No such entry */
54 inode = NULL;
55 goto out;
56 }
57 goto fail;
58 }
59 type = be16_to_cpu(entry.type);
60 if (type == HFSPLUS_FOLDER) {
61 if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
62 err = -EIO;
63 goto fail;
64 }
65 cnid = be32_to_cpu(entry.folder.id);
66 dentry->d_fsdata = (void *)(unsigned long)cnid;
67 } else if (type == HFSPLUS_FILE) {
68 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
69 err = -EIO;
70 goto fail;
71 }
72 cnid = be32_to_cpu(entry.file.id);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020073 if (entry.file.user_info.fdType ==
74 cpu_to_be32(HFSP_HARDLINK_TYPE) &&
75 entry.file.user_info.fdCreator ==
76 cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
77 (entry.file.create_date ==
78 HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->
79 create_date ||
80 entry.file.create_date ==
81 HFSPLUS_I(sb->s_root->d_inode)->
82 create_date) &&
83 HFSPLUS_SB(sb)->hidden_dir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 struct qstr str;
85 char name[32];
86
87 if (dentry->d_fsdata) {
Roman Zippelaf8c85b2006-01-18 17:43:10 -080088 /*
89 * We found a link pointing to another link,
90 * so ignore it and treat it as regular file.
91 */
92 cnid = (unsigned long)dentry->d_fsdata;
93 linkid = 0;
94 } else {
95 dentry->d_fsdata = (void *)(unsigned long)cnid;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020096 linkid =
97 be32_to_cpu(entry.file.permissions.dev);
Roman Zippelaf8c85b2006-01-18 17:43:10 -080098 str.len = sprintf(name, "iNode%d", linkid);
99 str.name = name;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200100 hfsplus_cat_build_key(sb, fd.search_key,
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200101 HFSPLUS_SB(sb)->hidden_dir->i_ino,
102 &str);
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800103 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 } else if (!dentry->d_fsdata)
106 dentry->d_fsdata = (void *)(unsigned long)cnid;
107 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700108 pr_err("invalid catalog entry type in lookup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 err = -EIO;
110 goto fail;
111 }
112 hfs_find_exit(&fd);
David Howells63525392008-02-07 00:15:40 -0800113 inode = hfsplus_iget(dir->i_sb, cnid);
114 if (IS_ERR(inode))
115 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (S_ISREG(inode->i_mode))
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400117 HFSPLUS_I(inode)->linkid = linkid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118out:
119 d_add(dentry, inode);
120 return NULL;
121fail:
122 hfs_find_exit(&fd);
123 return ERR_PTR(err);
124}
125
Al Viroe72514e2013-05-22 14:59:39 -0400126static int hfsplus_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Al Viroe72514e2013-05-22 14:59:39 -0400128 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 struct super_block *sb = inode->i_sb;
130 int len, err;
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700131 char *strbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 hfsplus_cat_entry entry;
133 struct hfs_find_data fd;
134 struct hfsplus_readdir_data *rd;
135 u16 type;
136
Al Viroe72514e2013-05-22 14:59:39 -0400137 if (file->f_pos >= inode->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
139
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +0400140 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
141 if (err)
142 return err;
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700143 strbuf = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN + 1, GFP_KERNEL);
144 if (!strbuf) {
145 err = -ENOMEM;
146 goto out;
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800149 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (err)
151 goto out;
152
Al Viroe72514e2013-05-22 14:59:39 -0400153 if (ctx->pos == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /* This is completely artificial... */
Al Viroe72514e2013-05-22 14:59:39 -0400155 if (!dir_emit_dot(file, ctx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 goto out;
Al Viroe72514e2013-05-22 14:59:39 -0400157 ctx->pos = 1;
158 }
159 if (ctx->pos == 1) {
Greg Kroah-Hartman6f24f892012-05-04 12:09:39 -0700160 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
161 err = -EIO;
162 goto out;
163 }
164
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200165 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
166 fd.entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
Joe Perchesd6142672013-04-30 15:27:55 -0700168 pr_err("bad catalog folder thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 err = -EIO;
170 goto out;
171 }
172 if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
Joe Perchesd6142672013-04-30 15:27:55 -0700173 pr_err("truncated catalog thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 err = -EIO;
175 goto out;
176 }
Al Viroe72514e2013-05-22 14:59:39 -0400177 if (!dir_emit(ctx, "..", 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 be32_to_cpu(entry.thread.parentID), DT_DIR))
179 goto out;
Al Viroe72514e2013-05-22 14:59:39 -0400180 ctx->pos = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Al Viroe72514e2013-05-22 14:59:39 -0400182 if (ctx->pos >= inode->i_size)
183 goto out;
184 err = hfs_brec_goto(&fd, ctx->pos - 1);
185 if (err)
186 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 for (;;) {
188 if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
Joe Perchesd6142672013-04-30 15:27:55 -0700189 pr_err("walked past end of dir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 err = -EIO;
191 goto out;
192 }
Greg Kroah-Hartman6f24f892012-05-04 12:09:39 -0700193
194 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
195 err = -EIO;
196 goto out;
197 }
198
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200199 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
200 fd.entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 type = be16_to_cpu(entry.type);
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700202 len = NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
204 if (err)
205 goto out;
206 if (type == HFSPLUS_FOLDER) {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200207 if (fd.entrylength <
208 sizeof(struct hfsplus_cat_folder)) {
Joe Perchesd6142672013-04-30 15:27:55 -0700209 pr_err("small dir entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 err = -EIO;
211 goto out;
212 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200213 if (HFSPLUS_SB(sb)->hidden_dir &&
214 HFSPLUS_SB(sb)->hidden_dir->i_ino ==
215 be32_to_cpu(entry.folder.id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 goto next;
Al Viroe72514e2013-05-22 14:59:39 -0400217 if (!dir_emit(ctx, strbuf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 be32_to_cpu(entry.folder.id), DT_DIR))
219 break;
220 } else if (type == HFSPLUS_FILE) {
Sergei Antonov97a62ea2014-06-06 14:36:24 -0700221 u16 mode;
222 unsigned type = DT_UNKNOWN;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
Joe Perchesd6142672013-04-30 15:27:55 -0700225 pr_err("small file entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 err = -EIO;
227 goto out;
228 }
Sergei Antonov97a62ea2014-06-06 14:36:24 -0700229
230 mode = be16_to_cpu(entry.file.permissions.mode);
231 if (S_ISREG(mode))
232 type = DT_REG;
233 else if (S_ISLNK(mode))
234 type = DT_LNK;
235 else if (S_ISFIFO(mode))
236 type = DT_FIFO;
237 else if (S_ISCHR(mode))
238 type = DT_CHR;
239 else if (S_ISBLK(mode))
240 type = DT_BLK;
241 else if (S_ISSOCK(mode))
242 type = DT_SOCK;
243
Al Viroe72514e2013-05-22 14:59:39 -0400244 if (!dir_emit(ctx, strbuf, len,
Sergei Antonov97a62ea2014-06-06 14:36:24 -0700245 be32_to_cpu(entry.file.id), type))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 break;
247 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700248 pr_err("bad catalog entry type\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 err = -EIO;
250 goto out;
251 }
Anton Salikhmetov20b76432010-12-16 18:08:40 +0200252next:
Al Viroe72514e2013-05-22 14:59:39 -0400253 ctx->pos++;
254 if (ctx->pos >= inode->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 goto out;
256 err = hfs_brec_goto(&fd, 1);
257 if (err)
258 goto out;
259 }
Al Viroe72514e2013-05-22 14:59:39 -0400260 rd = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!rd) {
262 rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
263 if (!rd) {
264 err = -ENOMEM;
265 goto out;
266 }
Al Viroe72514e2013-05-22 14:59:39 -0400267 file->private_data = rd;
268 rd->file = file;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200269 list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271 memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
272out:
Hin-Tak Leung017f8da2014-06-06 14:36:21 -0700273 kfree(strbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 hfs_find_exit(&fd);
275 return err;
276}
277
278static int hfsplus_dir_release(struct inode *inode, struct file *file)
279{
280 struct hfsplus_readdir_data *rd = file->private_data;
281 if (rd) {
Christoph Hellwig89755dc2010-10-01 05:45:25 +0200282 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 list_del(&rd->list);
Christoph Hellwig89755dc2010-10-01 05:45:25 +0200284 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 kfree(rd);
286 }
287 return 0;
288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
291 struct dentry *dst_dentry)
292{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200293 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 struct inode *inode = src_dentry->d_inode;
295 struct inode *src_dir = src_dentry->d_parent->d_inode;
296 struct qstr str;
297 char name[32];
298 u32 cnid, id;
299 int res;
300
301 if (HFSPLUS_IS_RSRC(inode))
302 return -EPERM;
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400303 if (!S_ISREG(inode->i_mode))
304 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200306 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
308 for (;;) {
309 get_random_bytes(&id, sizeof(cnid));
310 id &= 0x3fffffff;
311 str.name = name;
312 str.len = sprintf(name, "iNode%d", id);
313 res = hfsplus_rename_cat(inode->i_ino,
314 src_dir, &src_dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200315 sbi->hidden_dir, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (!res)
317 break;
318 if (res != -EEXIST)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200319 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400321 HFSPLUS_I(inode)->linkid = id;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200322 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 src_dentry->d_fsdata = (void *)(unsigned long)cnid;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200324 res = hfsplus_create_cat(cnid, src_dir,
325 &src_dentry->d_name, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (res)
327 /* panic? */
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200328 goto out;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200329 sbi->file_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200331 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
333 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200334 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Dave Hansend8c76e62006-09-30 23:29:04 -0700336 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 hfsplus_instantiate(dst_dentry, inode, cnid);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400338 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 inode->i_ctime = CURRENT_TIME_SEC;
340 mark_inode_dirty(inode);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200341 sbi->file_count++;
Artem Bityutskiy9e6c5822012-07-12 17:26:31 +0300342 hfsplus_mark_mdb_dirty(dst_dir->i_sb);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200343out:
344 mutex_unlock(&sbi->vh_mutex);
345 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
348static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
349{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200350 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct inode *inode = dentry->d_inode;
352 struct qstr str;
353 char name[32];
354 u32 cnid;
355 int res;
356
357 if (HFSPLUS_IS_RSRC(inode))
358 return -EPERM;
359
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200360 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 cnid = (u32)(unsigned long)dentry->d_fsdata;
362 if (inode->i_ino == cnid &&
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200363 atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 str.name = name;
365 str.len = sprintf(name, "temp%lu", inode->i_ino);
366 res = hfsplus_rename_cat(inode->i_ino,
367 dir, &dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200368 sbi->hidden_dir, &str);
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200369 if (!res) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 inode->i_flags |= S_DEAD;
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200371 drop_nlink(inode);
372 }
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200373 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375 res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
376 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200377 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800379 if (inode->i_nlink > 0)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700380 drop_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200381 if (inode->i_ino == cnid)
Dave Hansence71ec32006-09-30 23:29:06 -0700382 clear_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200383 if (!inode->i_nlink) {
384 if (inode->i_ino != cnid) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200385 sbi->file_count--;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200386 if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Roman Zippel76b0c262008-04-09 17:44:07 +0200387 res = hfsplus_delete_cat(inode->i_ino,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200388 sbi->hidden_dir,
Roman Zippel76b0c262008-04-09 17:44:07 +0200389 NULL);
390 if (!res)
391 hfsplus_delete_inode(inode);
392 } else
393 inode->i_flags |= S_DEAD;
394 } else
395 hfsplus_delete_inode(inode);
396 } else
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200397 sbi->file_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 inode->i_ctime = CURRENT_TIME_SEC;
399 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200400out:
401 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return res;
403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
406{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200407 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
408 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 int res;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (inode->i_size != 2)
412 return -ENOTEMPTY;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200413
414 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
416 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200417 goto out;
Dave Hansence71ec32006-09-30 23:29:06 -0700418 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 inode->i_ctime = CURRENT_TIME_SEC;
420 hfsplus_delete_inode(inode);
421 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200422out:
423 mutex_unlock(&sbi->vh_mutex);
424 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
428 const char *symname)
429{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200430 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 struct inode *inode;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200432 int res = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200434 mutex_lock(&sbi->vh_mutex);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200435 inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200437 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 res = page_symlink(inode, symname, strlen(symname) + 1);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200440 if (res)
441 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200444 if (res)
445 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800447 res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
448 if (res == -EOPNOTSUPP)
449 res = 0; /* Operation is not supported. */
450 else if (res) {
451 /* Try to delete anyway without error analysis. */
452 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
453 goto out_err;
454 }
455
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200456 hfsplus_instantiate(dentry, inode, inode->i_ino);
457 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200458 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200460out_err:
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200461 clear_nlink(inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200462 hfsplus_delete_inode(inode);
463 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200464out:
465 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return res;
467}
468
469static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400470 umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200472 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct inode *inode;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200474 int res = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200476 mutex_lock(&sbi->vh_mutex);
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200477 inode = hfsplus_new_inode(dir->i_sb, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200479 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Christoph Hellwig90e61692010-10-14 09:54:39 -0400481 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
482 init_special_inode(inode, mode, rdev);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800485 if (res)
486 goto failed_mknod;
487
488 res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
489 if (res == -EOPNOTSUPP)
490 res = 0; /* Operation is not supported. */
491 else if (res) {
492 /* Try to delete anyway without error analysis. */
493 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
494 goto failed_mknod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 hfsplus_instantiate(dentry, inode, inode->i_ino);
498 mark_inode_dirty(inode);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800499 goto out;
500
501failed_mknod:
502 clear_nlink(inode);
503 hfsplus_delete_inode(inode);
504 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200505out:
506 mutex_unlock(&sbi->vh_mutex);
507 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Al Viro4acdaf22011-07-26 01:42:34 -0400510static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400511 bool excl)
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200512{
513 return hfsplus_mknod(dir, dentry, mode, 0);
514}
515
Al Viro18bb1db2011-07-26 01:41:39 -0400516static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200517{
518 return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
522 struct inode *new_dir, struct dentry *new_dentry)
523{
524 int res;
525
526 /* Unlink destination if it already exists */
527 if (new_dentry->d_inode) {
Sage Weile3911782011-05-27 13:42:06 -0700528 if (S_ISDIR(new_dentry->d_inode->i_mode))
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200529 res = hfsplus_rmdir(new_dir, new_dentry);
Sage Weile3911782011-05-27 13:42:06 -0700530 else
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200531 res = hfsplus_unlink(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (res)
533 return res;
534 }
535
536 res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
537 old_dir, &old_dentry->d_name,
538 new_dir, &new_dentry->d_name);
539 if (!res)
540 new_dentry->d_fsdata = old_dentry->d_fsdata;
541 return res;
542}
543
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800544const struct inode_operations hfsplus_dir_inode_operations = {
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800545 .lookup = hfsplus_lookup,
546 .create = hfsplus_create,
547 .link = hfsplus_link,
548 .unlink = hfsplus_unlink,
549 .mkdir = hfsplus_mkdir,
550 .rmdir = hfsplus_rmdir,
551 .symlink = hfsplus_symlink,
552 .mknod = hfsplus_mknod,
553 .rename = hfsplus_rename,
554 .setxattr = generic_setxattr,
555 .getxattr = generic_getxattr,
556 .listxattr = hfsplus_listxattr,
Christoph Hellwigb168fff2014-01-29 23:59:19 -0800557 .removexattr = generic_removexattr,
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -0700558#ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
559 .get_acl = hfsplus_get_posix_acl,
Christoph Hellwigb0a7ab52013-12-20 05:16:46 -0800560 .set_acl = hfsplus_set_posix_acl,
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -0700561#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562};
563
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800564const struct file_operations hfsplus_dir_operations = {
Christoph Hellwigeb29d662010-11-23 14:38:10 +0100565 .fsync = hfsplus_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 .read = generic_read_dir,
Al Viroe72514e2013-05-22 14:59:39 -0400567 .iterate = hfsplus_readdir,
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +0200568 .unlocked_ioctl = hfsplus_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 .llseek = generic_file_llseek,
570 .release = hfsplus_dir_release,
571};