blob: 4a4fea0026735c8fb365d031c57165888e9bb079 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include "hfsplus_fs.h"
17#include "hfsplus_raw.h"
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -080018#include "xattr.h"
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -070019#include "acl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21static inline void hfsplus_instantiate(struct dentry *dentry,
22 struct inode *inode, u32 cnid)
23{
24 dentry->d_fsdata = (void *)(unsigned long)cnid;
25 d_instantiate(dentry, inode);
26}
27
28/* Find the entry inside dir named dentry->d_name */
29static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -040030 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
32 struct inode *inode = NULL;
33 struct hfs_find_data fd;
34 struct super_block *sb;
35 hfsplus_cat_entry entry;
36 int err;
37 u32 cnid, linkid = 0;
38 u16 type;
39
40 sb = dir->i_sb;
Duane Griffind45bce82007-07-15 23:41:23 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 dentry->d_fsdata = NULL;
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +040043 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
44 if (err)
45 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name);
47again:
48 err = hfs_brec_read(&fd, &entry, sizeof(entry));
49 if (err) {
50 if (err == -ENOENT) {
51 hfs_find_exit(&fd);
52 /* No such entry */
53 inode = NULL;
54 goto out;
55 }
56 goto fail;
57 }
58 type = be16_to_cpu(entry.type);
59 if (type == HFSPLUS_FOLDER) {
60 if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
61 err = -EIO;
62 goto fail;
63 }
64 cnid = be32_to_cpu(entry.folder.id);
65 dentry->d_fsdata = (void *)(unsigned long)cnid;
66 } else if (type == HFSPLUS_FILE) {
67 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
68 err = -EIO;
69 goto fail;
70 }
71 cnid = be32_to_cpu(entry.file.id);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020072 if (entry.file.user_info.fdType ==
73 cpu_to_be32(HFSP_HARDLINK_TYPE) &&
74 entry.file.user_info.fdCreator ==
75 cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
76 (entry.file.create_date ==
77 HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->
78 create_date ||
79 entry.file.create_date ==
80 HFSPLUS_I(sb->s_root->d_inode)->
81 create_date) &&
82 HFSPLUS_SB(sb)->hidden_dir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 struct qstr str;
84 char name[32];
85
86 if (dentry->d_fsdata) {
Roman Zippelaf8c85b2006-01-18 17:43:10 -080087 /*
88 * We found a link pointing to another link,
89 * so ignore it and treat it as regular file.
90 */
91 cnid = (unsigned long)dentry->d_fsdata;
92 linkid = 0;
93 } else {
94 dentry->d_fsdata = (void *)(unsigned long)cnid;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020095 linkid =
96 be32_to_cpu(entry.file.permissions.dev);
Roman Zippelaf8c85b2006-01-18 17:43:10 -080097 str.len = sprintf(name, "iNode%d", linkid);
98 str.name = name;
Christoph Hellwigdd73a012010-10-01 05:42:59 +020099 hfsplus_cat_build_key(sb, fd.search_key,
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200100 HFSPLUS_SB(sb)->hidden_dir->i_ino,
101 &str);
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800102 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 } else if (!dentry->d_fsdata)
105 dentry->d_fsdata = (void *)(unsigned long)cnid;
106 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700107 pr_err("invalid catalog entry type in lookup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 err = -EIO;
109 goto fail;
110 }
111 hfs_find_exit(&fd);
David Howells63525392008-02-07 00:15:40 -0800112 inode = hfsplus_iget(dir->i_sb, cnid);
113 if (IS_ERR(inode))
114 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (S_ISREG(inode->i_mode))
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400116 HFSPLUS_I(inode)->linkid = linkid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117out:
118 d_add(dentry, inode);
119 return NULL;
120fail:
121 hfs_find_exit(&fd);
122 return ERR_PTR(err);
123}
124
Al Viroe72514e2013-05-22 14:59:39 -0400125static int hfsplus_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Al Viroe72514e2013-05-22 14:59:39 -0400127 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 struct super_block *sb = inode->i_sb;
129 int len, err;
130 char strbuf[HFSPLUS_MAX_STRLEN + 1];
131 hfsplus_cat_entry entry;
132 struct hfs_find_data fd;
133 struct hfsplus_readdir_data *rd;
134 u16 type;
135
Al Viroe72514e2013-05-22 14:59:39 -0400136 if (file->f_pos >= inode->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return 0;
138
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +0400139 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
140 if (err)
141 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800143 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (err)
145 goto out;
146
Al Viroe72514e2013-05-22 14:59:39 -0400147 if (ctx->pos == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* This is completely artificial... */
Al Viroe72514e2013-05-22 14:59:39 -0400149 if (!dir_emit_dot(file, ctx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 goto out;
Al Viroe72514e2013-05-22 14:59:39 -0400151 ctx->pos = 1;
152 }
153 if (ctx->pos == 1) {
Greg Kroah-Hartman6f24f892012-05-04 12:09:39 -0700154 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
155 err = -EIO;
156 goto out;
157 }
158
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200159 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
160 fd.entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
Joe Perchesd6142672013-04-30 15:27:55 -0700162 pr_err("bad catalog folder thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 err = -EIO;
164 goto out;
165 }
166 if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
Joe Perchesd6142672013-04-30 15:27:55 -0700167 pr_err("truncated catalog thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 err = -EIO;
169 goto out;
170 }
Al Viroe72514e2013-05-22 14:59:39 -0400171 if (!dir_emit(ctx, "..", 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 be32_to_cpu(entry.thread.parentID), DT_DIR))
173 goto out;
Al Viroe72514e2013-05-22 14:59:39 -0400174 ctx->pos = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
Al Viroe72514e2013-05-22 14:59:39 -0400176 if (ctx->pos >= inode->i_size)
177 goto out;
178 err = hfs_brec_goto(&fd, ctx->pos - 1);
179 if (err)
180 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 for (;;) {
182 if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
Joe Perchesd6142672013-04-30 15:27:55 -0700183 pr_err("walked past end of dir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 err = -EIO;
185 goto out;
186 }
Greg Kroah-Hartman6f24f892012-05-04 12:09:39 -0700187
188 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
189 err = -EIO;
190 goto out;
191 }
192
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200193 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
194 fd.entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 type = be16_to_cpu(entry.type);
196 len = HFSPLUS_MAX_STRLEN;
197 err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
198 if (err)
199 goto out;
200 if (type == HFSPLUS_FOLDER) {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200201 if (fd.entrylength <
202 sizeof(struct hfsplus_cat_folder)) {
Joe Perchesd6142672013-04-30 15:27:55 -0700203 pr_err("small dir entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 err = -EIO;
205 goto out;
206 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200207 if (HFSPLUS_SB(sb)->hidden_dir &&
208 HFSPLUS_SB(sb)->hidden_dir->i_ino ==
209 be32_to_cpu(entry.folder.id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 goto next;
Al Viroe72514e2013-05-22 14:59:39 -0400211 if (!dir_emit(ctx, strbuf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 be32_to_cpu(entry.folder.id), DT_DIR))
213 break;
214 } else if (type == HFSPLUS_FILE) {
215 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
Joe Perchesd6142672013-04-30 15:27:55 -0700216 pr_err("small file entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 err = -EIO;
218 goto out;
219 }
Al Viroe72514e2013-05-22 14:59:39 -0400220 if (!dir_emit(ctx, strbuf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 be32_to_cpu(entry.file.id), DT_REG))
222 break;
223 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700224 pr_err("bad catalog entry type\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 err = -EIO;
226 goto out;
227 }
Anton Salikhmetov20b76432010-12-16 18:08:40 +0200228next:
Al Viroe72514e2013-05-22 14:59:39 -0400229 ctx->pos++;
230 if (ctx->pos >= inode->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 goto out;
232 err = hfs_brec_goto(&fd, 1);
233 if (err)
234 goto out;
235 }
Al Viroe72514e2013-05-22 14:59:39 -0400236 rd = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (!rd) {
238 rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
239 if (!rd) {
240 err = -ENOMEM;
241 goto out;
242 }
Al Viroe72514e2013-05-22 14:59:39 -0400243 file->private_data = rd;
244 rd->file = file;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200245 list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247 memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
248out:
249 hfs_find_exit(&fd);
250 return err;
251}
252
253static int hfsplus_dir_release(struct inode *inode, struct file *file)
254{
255 struct hfsplus_readdir_data *rd = file->private_data;
256 if (rd) {
Christoph Hellwig89755dc2010-10-01 05:45:25 +0200257 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 list_del(&rd->list);
Christoph Hellwig89755dc2010-10-01 05:45:25 +0200259 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 kfree(rd);
261 }
262 return 0;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
266 struct dentry *dst_dentry)
267{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200268 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct inode *inode = src_dentry->d_inode;
270 struct inode *src_dir = src_dentry->d_parent->d_inode;
271 struct qstr str;
272 char name[32];
273 u32 cnid, id;
274 int res;
275
276 if (HFSPLUS_IS_RSRC(inode))
277 return -EPERM;
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400278 if (!S_ISREG(inode->i_mode))
279 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200281 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
283 for (;;) {
284 get_random_bytes(&id, sizeof(cnid));
285 id &= 0x3fffffff;
286 str.name = name;
287 str.len = sprintf(name, "iNode%d", id);
288 res = hfsplus_rename_cat(inode->i_ino,
289 src_dir, &src_dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200290 sbi->hidden_dir, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (!res)
292 break;
293 if (res != -EEXIST)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200294 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400296 HFSPLUS_I(inode)->linkid = id;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200297 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 src_dentry->d_fsdata = (void *)(unsigned long)cnid;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200299 res = hfsplus_create_cat(cnid, src_dir,
300 &src_dentry->d_name, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (res)
302 /* panic? */
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200303 goto out;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200304 sbi->file_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200306 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
308 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200309 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Dave Hansend8c76e62006-09-30 23:29:04 -0700311 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 hfsplus_instantiate(dst_dentry, inode, cnid);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400313 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 inode->i_ctime = CURRENT_TIME_SEC;
315 mark_inode_dirty(inode);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200316 sbi->file_count++;
Artem Bityutskiy9e6c5822012-07-12 17:26:31 +0300317 hfsplus_mark_mdb_dirty(dst_dir->i_sb);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200318out:
319 mutex_unlock(&sbi->vh_mutex);
320 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
324{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200325 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct inode *inode = dentry->d_inode;
327 struct qstr str;
328 char name[32];
329 u32 cnid;
330 int res;
331
332 if (HFSPLUS_IS_RSRC(inode))
333 return -EPERM;
334
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200335 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 cnid = (u32)(unsigned long)dentry->d_fsdata;
337 if (inode->i_ino == cnid &&
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200338 atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 str.name = name;
340 str.len = sprintf(name, "temp%lu", inode->i_ino);
341 res = hfsplus_rename_cat(inode->i_ino,
342 dir, &dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200343 sbi->hidden_dir, &str);
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200344 if (!res) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 inode->i_flags |= S_DEAD;
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200346 drop_nlink(inode);
347 }
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200348 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
351 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800354 if (inode->i_nlink > 0)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700355 drop_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200356 if (inode->i_ino == cnid)
Dave Hansence71ec32006-09-30 23:29:06 -0700357 clear_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200358 if (!inode->i_nlink) {
359 if (inode->i_ino != cnid) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200360 sbi->file_count--;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200361 if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Roman Zippel76b0c262008-04-09 17:44:07 +0200362 res = hfsplus_delete_cat(inode->i_ino,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200363 sbi->hidden_dir,
Roman Zippel76b0c262008-04-09 17:44:07 +0200364 NULL);
365 if (!res)
366 hfsplus_delete_inode(inode);
367 } else
368 inode->i_flags |= S_DEAD;
369 } else
370 hfsplus_delete_inode(inode);
371 } else
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200372 sbi->file_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 inode->i_ctime = CURRENT_TIME_SEC;
374 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200375out:
376 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return res;
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
381{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200382 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
383 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 int res;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (inode->i_size != 2)
387 return -ENOTEMPTY;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200388
389 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
391 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200392 goto out;
Dave Hansence71ec32006-09-30 23:29:06 -0700393 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 inode->i_ctime = CURRENT_TIME_SEC;
395 hfsplus_delete_inode(inode);
396 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200397out:
398 mutex_unlock(&sbi->vh_mutex);
399 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
403 const char *symname)
404{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200405 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 struct inode *inode;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200407 int res = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200409 mutex_lock(&sbi->vh_mutex);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200410 inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200412 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 res = page_symlink(inode, symname, strlen(symname) + 1);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200415 if (res)
416 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200419 if (res)
420 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800422 res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
423 if (res == -EOPNOTSUPP)
424 res = 0; /* Operation is not supported. */
425 else if (res) {
426 /* Try to delete anyway without error analysis. */
427 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
428 goto out_err;
429 }
430
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200431 hfsplus_instantiate(dentry, inode, inode->i_ino);
432 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200433 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200435out_err:
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200436 clear_nlink(inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200437 hfsplus_delete_inode(inode);
438 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200439out:
440 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return res;
442}
443
444static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400445 umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200447 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 struct inode *inode;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200449 int res = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200451 mutex_lock(&sbi->vh_mutex);
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200452 inode = hfsplus_new_inode(dir->i_sb, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200454 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Christoph Hellwig90e61692010-10-14 09:54:39 -0400456 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
457 init_special_inode(inode, mode, rdev);
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800460 if (res)
461 goto failed_mknod;
462
463 res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
464 if (res == -EOPNOTSUPP)
465 res = 0; /* Operation is not supported. */
466 else if (res) {
467 /* Try to delete anyway without error analysis. */
468 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
469 goto failed_mknod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 hfsplus_instantiate(dentry, inode, inode->i_ino);
473 mark_inode_dirty(inode);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800474 goto out;
475
476failed_mknod:
477 clear_nlink(inode);
478 hfsplus_delete_inode(inode);
479 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200480out:
481 mutex_unlock(&sbi->vh_mutex);
482 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Al Viro4acdaf22011-07-26 01:42:34 -0400485static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400486 bool excl)
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200487{
488 return hfsplus_mknod(dir, dentry, mode, 0);
489}
490
Al Viro18bb1db2011-07-26 01:41:39 -0400491static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200492{
493 return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
497 struct inode *new_dir, struct dentry *new_dentry)
498{
499 int res;
500
501 /* Unlink destination if it already exists */
502 if (new_dentry->d_inode) {
Sage Weile3911782011-05-27 13:42:06 -0700503 if (S_ISDIR(new_dentry->d_inode->i_mode))
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200504 res = hfsplus_rmdir(new_dir, new_dentry);
Sage Weile3911782011-05-27 13:42:06 -0700505 else
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200506 res = hfsplus_unlink(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (res)
508 return res;
509 }
510
511 res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
512 old_dir, &old_dentry->d_name,
513 new_dir, &new_dentry->d_name);
514 if (!res)
515 new_dentry->d_fsdata = old_dentry->d_fsdata;
516 return res;
517}
518
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800519const struct inode_operations hfsplus_dir_inode_operations = {
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800520 .lookup = hfsplus_lookup,
521 .create = hfsplus_create,
522 .link = hfsplus_link,
523 .unlink = hfsplus_unlink,
524 .mkdir = hfsplus_mkdir,
525 .rmdir = hfsplus_rmdir,
526 .symlink = hfsplus_symlink,
527 .mknod = hfsplus_mknod,
528 .rename = hfsplus_rename,
529 .setxattr = generic_setxattr,
530 .getxattr = generic_getxattr,
531 .listxattr = hfsplus_listxattr,
532 .removexattr = hfsplus_removexattr,
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -0700533#ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
534 .get_acl = hfsplus_get_posix_acl,
535#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536};
537
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800538const struct file_operations hfsplus_dir_operations = {
Christoph Hellwigeb29d662010-11-23 14:38:10 +0100539 .fsync = hfsplus_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 .read = generic_read_dir,
Al Viroe72514e2013-05-22 14:59:39 -0400541 .iterate = hfsplus_readdir,
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +0200542 .unlocked_ioctl = hfsplus_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 .llseek = generic_file_llseek,
544 .release = hfsplus_dir_release,
545};