blob: d8ce4bd17fc5f43058eaae416532b2e4019cc870 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20static inline void hfsplus_instantiate(struct dentry *dentry,
21 struct inode *inode, u32 cnid)
22{
23 dentry->d_fsdata = (void *)(unsigned long)cnid;
24 d_instantiate(dentry, inode);
25}
26
27/* Find the entry inside dir named dentry->d_name */
28static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -040029 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030{
31 struct inode *inode = NULL;
32 struct hfs_find_data fd;
33 struct super_block *sb;
34 hfsplus_cat_entry entry;
35 int err;
36 u32 cnid, linkid = 0;
37 u16 type;
38
39 sb = dir->i_sb;
Duane Griffind45bce82007-07-15 23:41:23 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 dentry->d_fsdata = NULL;
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +040042 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
43 if (err)
44 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name);
46again:
47 err = hfs_brec_read(&fd, &entry, sizeof(entry));
48 if (err) {
49 if (err == -ENOENT) {
50 hfs_find_exit(&fd);
51 /* No such entry */
52 inode = NULL;
53 goto out;
54 }
55 goto fail;
56 }
57 type = be16_to_cpu(entry.type);
58 if (type == HFSPLUS_FOLDER) {
59 if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
60 err = -EIO;
61 goto fail;
62 }
63 cnid = be32_to_cpu(entry.folder.id);
64 dentry->d_fsdata = (void *)(unsigned long)cnid;
65 } else if (type == HFSPLUS_FILE) {
66 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
67 err = -EIO;
68 goto fail;
69 }
70 cnid = be32_to_cpu(entry.file.id);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020071 if (entry.file.user_info.fdType ==
72 cpu_to_be32(HFSP_HARDLINK_TYPE) &&
73 entry.file.user_info.fdCreator ==
74 cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
75 (entry.file.create_date ==
76 HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->
77 create_date ||
78 entry.file.create_date ==
79 HFSPLUS_I(sb->s_root->d_inode)->
80 create_date) &&
81 HFSPLUS_SB(sb)->hidden_dir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct qstr str;
83 char name[32];
84
85 if (dentry->d_fsdata) {
Roman Zippelaf8c85b2006-01-18 17:43:10 -080086 /*
87 * We found a link pointing to another link,
88 * so ignore it and treat it as regular file.
89 */
90 cnid = (unsigned long)dentry->d_fsdata;
91 linkid = 0;
92 } else {
93 dentry->d_fsdata = (void *)(unsigned long)cnid;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020094 linkid =
95 be32_to_cpu(entry.file.permissions.dev);
Roman Zippelaf8c85b2006-01-18 17:43:10 -080096 str.len = sprintf(name, "iNode%d", linkid);
97 str.name = name;
Christoph Hellwigdd73a012010-10-01 05:42:59 +020098 hfsplus_cat_build_key(sb, fd.search_key,
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020099 HFSPLUS_SB(sb)->hidden_dir->i_ino,
100 &str);
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800101 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 } else if (!dentry->d_fsdata)
104 dentry->d_fsdata = (void *)(unsigned long)cnid;
105 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700106 pr_err("invalid catalog entry type in lookup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 err = -EIO;
108 goto fail;
109 }
110 hfs_find_exit(&fd);
David Howells63525392008-02-07 00:15:40 -0800111 inode = hfsplus_iget(dir->i_sb, cnid);
112 if (IS_ERR(inode))
113 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (S_ISREG(inode->i_mode))
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400115 HFSPLUS_I(inode)->linkid = linkid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116out:
117 d_add(dentry, inode);
118 return NULL;
119fail:
120 hfs_find_exit(&fd);
121 return ERR_PTR(err);
122}
123
Al Viroe72514e2013-05-22 14:59:39 -0400124static int hfsplus_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Al Viroe72514e2013-05-22 14:59:39 -0400126 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 struct super_block *sb = inode->i_sb;
128 int len, err;
129 char strbuf[HFSPLUS_MAX_STRLEN + 1];
130 hfsplus_cat_entry entry;
131 struct hfs_find_data fd;
132 struct hfsplus_readdir_data *rd;
133 u16 type;
134
Al Viroe72514e2013-05-22 14:59:39 -0400135 if (file->f_pos >= inode->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return 0;
137
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +0400138 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
139 if (err)
140 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800142 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (err)
144 goto out;
145
Al Viroe72514e2013-05-22 14:59:39 -0400146 if (ctx->pos == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /* This is completely artificial... */
Al Viroe72514e2013-05-22 14:59:39 -0400148 if (!dir_emit_dot(file, ctx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto out;
Al Viroe72514e2013-05-22 14:59:39 -0400150 ctx->pos = 1;
151 }
152 if (ctx->pos == 1) {
Greg Kroah-Hartman6f24f892012-05-04 12:09:39 -0700153 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
154 err = -EIO;
155 goto out;
156 }
157
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200158 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
159 fd.entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
Joe Perchesd6142672013-04-30 15:27:55 -0700161 pr_err("bad catalog folder thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 err = -EIO;
163 goto out;
164 }
165 if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
Joe Perchesd6142672013-04-30 15:27:55 -0700166 pr_err("truncated catalog thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 err = -EIO;
168 goto out;
169 }
Al Viroe72514e2013-05-22 14:59:39 -0400170 if (!dir_emit(ctx, "..", 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 be32_to_cpu(entry.thread.parentID), DT_DIR))
172 goto out;
Al Viroe72514e2013-05-22 14:59:39 -0400173 ctx->pos = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
Al Viroe72514e2013-05-22 14:59:39 -0400175 if (ctx->pos >= inode->i_size)
176 goto out;
177 err = hfs_brec_goto(&fd, ctx->pos - 1);
178 if (err)
179 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 for (;;) {
181 if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
Joe Perchesd6142672013-04-30 15:27:55 -0700182 pr_err("walked past end of dir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 err = -EIO;
184 goto out;
185 }
Greg Kroah-Hartman6f24f892012-05-04 12:09:39 -0700186
187 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
188 err = -EIO;
189 goto out;
190 }
191
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200192 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
193 fd.entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 type = be16_to_cpu(entry.type);
195 len = HFSPLUS_MAX_STRLEN;
196 err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
197 if (err)
198 goto out;
199 if (type == HFSPLUS_FOLDER) {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200200 if (fd.entrylength <
201 sizeof(struct hfsplus_cat_folder)) {
Joe Perchesd6142672013-04-30 15:27:55 -0700202 pr_err("small dir entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 err = -EIO;
204 goto out;
205 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200206 if (HFSPLUS_SB(sb)->hidden_dir &&
207 HFSPLUS_SB(sb)->hidden_dir->i_ino ==
208 be32_to_cpu(entry.folder.id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto next;
Al Viroe72514e2013-05-22 14:59:39 -0400210 if (!dir_emit(ctx, strbuf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 be32_to_cpu(entry.folder.id), DT_DIR))
212 break;
213 } else if (type == HFSPLUS_FILE) {
214 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
Joe Perchesd6142672013-04-30 15:27:55 -0700215 pr_err("small file entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 err = -EIO;
217 goto out;
218 }
Al Viroe72514e2013-05-22 14:59:39 -0400219 if (!dir_emit(ctx, strbuf, len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 be32_to_cpu(entry.file.id), DT_REG))
221 break;
222 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700223 pr_err("bad catalog entry type\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 err = -EIO;
225 goto out;
226 }
Anton Salikhmetov20b76432010-12-16 18:08:40 +0200227next:
Al Viroe72514e2013-05-22 14:59:39 -0400228 ctx->pos++;
229 if (ctx->pos >= inode->i_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 goto out;
231 err = hfs_brec_goto(&fd, 1);
232 if (err)
233 goto out;
234 }
Al Viroe72514e2013-05-22 14:59:39 -0400235 rd = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (!rd) {
237 rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
238 if (!rd) {
239 err = -ENOMEM;
240 goto out;
241 }
Al Viroe72514e2013-05-22 14:59:39 -0400242 file->private_data = rd;
243 rd->file = file;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200244 list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
247out:
248 hfs_find_exit(&fd);
249 return err;
250}
251
252static int hfsplus_dir_release(struct inode *inode, struct file *file)
253{
254 struct hfsplus_readdir_data *rd = file->private_data;
255 if (rd) {
Christoph Hellwig89755dc2010-10-01 05:45:25 +0200256 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 list_del(&rd->list);
Christoph Hellwig89755dc2010-10-01 05:45:25 +0200258 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 kfree(rd);
260 }
261 return 0;
262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
265 struct dentry *dst_dentry)
266{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200267 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct inode *inode = src_dentry->d_inode;
269 struct inode *src_dir = src_dentry->d_parent->d_inode;
270 struct qstr str;
271 char name[32];
272 u32 cnid, id;
273 int res;
274
275 if (HFSPLUS_IS_RSRC(inode))
276 return -EPERM;
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400277 if (!S_ISREG(inode->i_mode))
278 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200280 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
282 for (;;) {
283 get_random_bytes(&id, sizeof(cnid));
284 id &= 0x3fffffff;
285 str.name = name;
286 str.len = sprintf(name, "iNode%d", id);
287 res = hfsplus_rename_cat(inode->i_ino,
288 src_dir, &src_dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200289 sbi->hidden_dir, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (!res)
291 break;
292 if (res != -EEXIST)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200293 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400295 HFSPLUS_I(inode)->linkid = id;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200296 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 src_dentry->d_fsdata = (void *)(unsigned long)cnid;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200298 res = hfsplus_create_cat(cnid, src_dir,
299 &src_dentry->d_name, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (res)
301 /* panic? */
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200302 goto out;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200303 sbi->file_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200305 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
307 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200308 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Dave Hansend8c76e62006-09-30 23:29:04 -0700310 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 hfsplus_instantiate(dst_dentry, inode, cnid);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400312 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 inode->i_ctime = CURRENT_TIME_SEC;
314 mark_inode_dirty(inode);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200315 sbi->file_count++;
Artem Bityutskiy9e6c5822012-07-12 17:26:31 +0300316 hfsplus_mark_mdb_dirty(dst_dir->i_sb);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200317out:
318 mutex_unlock(&sbi->vh_mutex);
319 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
323{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200324 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 struct inode *inode = dentry->d_inode;
326 struct qstr str;
327 char name[32];
328 u32 cnid;
329 int res;
330
331 if (HFSPLUS_IS_RSRC(inode))
332 return -EPERM;
333
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200334 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 cnid = (u32)(unsigned long)dentry->d_fsdata;
336 if (inode->i_ino == cnid &&
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200337 atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 str.name = name;
339 str.len = sprintf(name, "temp%lu", inode->i_ino);
340 res = hfsplus_rename_cat(inode->i_ino,
341 dir, &dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200342 sbi->hidden_dir, &str);
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200343 if (!res) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 inode->i_flags |= S_DEAD;
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200345 drop_nlink(inode);
346 }
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200347 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
350 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200351 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800353 if (inode->i_nlink > 0)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700354 drop_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200355 if (inode->i_ino == cnid)
Dave Hansence71ec32006-09-30 23:29:06 -0700356 clear_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200357 if (!inode->i_nlink) {
358 if (inode->i_ino != cnid) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200359 sbi->file_count--;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200360 if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Roman Zippel76b0c262008-04-09 17:44:07 +0200361 res = hfsplus_delete_cat(inode->i_ino,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200362 sbi->hidden_dir,
Roman Zippel76b0c262008-04-09 17:44:07 +0200363 NULL);
364 if (!res)
365 hfsplus_delete_inode(inode);
366 } else
367 inode->i_flags |= S_DEAD;
368 } else
369 hfsplus_delete_inode(inode);
370 } else
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200371 sbi->file_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 inode->i_ctime = CURRENT_TIME_SEC;
373 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200374out:
375 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return res;
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
380{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200381 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
382 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 int res;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (inode->i_size != 2)
386 return -ENOTEMPTY;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200387
388 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
390 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200391 goto out;
Dave Hansence71ec32006-09-30 23:29:06 -0700392 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 inode->i_ctime = CURRENT_TIME_SEC;
394 hfsplus_delete_inode(inode);
395 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200396out:
397 mutex_unlock(&sbi->vh_mutex);
398 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
401static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
402 const char *symname)
403{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200404 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct inode *inode;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200406 int res = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200408 mutex_lock(&sbi->vh_mutex);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200409 inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200411 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 res = page_symlink(inode, symname, strlen(symname) + 1);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200414 if (res)
415 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200418 if (res)
419 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800421 res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
422 if (res == -EOPNOTSUPP)
423 res = 0; /* Operation is not supported. */
424 else if (res) {
425 /* Try to delete anyway without error analysis. */
426 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
427 goto out_err;
428 }
429
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200430 hfsplus_instantiate(dentry, inode, inode->i_ino);
431 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200432 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200434out_err:
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200435 clear_nlink(inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200436 hfsplus_delete_inode(inode);
437 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200438out:
439 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return res;
441}
442
443static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400444 umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200446 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct inode *inode;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200448 int res = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200450 mutex_lock(&sbi->vh_mutex);
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200451 inode = hfsplus_new_inode(dir->i_sb, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200453 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Christoph Hellwig90e61692010-10-14 09:54:39 -0400455 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
456 init_special_inode(inode, mode, rdev);
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800459 if (res)
460 goto failed_mknod;
461
462 res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
463 if (res == -EOPNOTSUPP)
464 res = 0; /* Operation is not supported. */
465 else if (res) {
466 /* Try to delete anyway without error analysis. */
467 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
468 goto failed_mknod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 hfsplus_instantiate(dentry, inode, inode->i_ino);
472 mark_inode_dirty(inode);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800473 goto out;
474
475failed_mknod:
476 clear_nlink(inode);
477 hfsplus_delete_inode(inode);
478 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200479out:
480 mutex_unlock(&sbi->vh_mutex);
481 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Al Viro4acdaf22011-07-26 01:42:34 -0400484static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400485 bool excl)
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200486{
487 return hfsplus_mknod(dir, dentry, mode, 0);
488}
489
Al Viro18bb1db2011-07-26 01:41:39 -0400490static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200491{
492 return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
496 struct inode *new_dir, struct dentry *new_dentry)
497{
498 int res;
499
500 /* Unlink destination if it already exists */
501 if (new_dentry->d_inode) {
Sage Weile3911782011-05-27 13:42:06 -0700502 if (S_ISDIR(new_dentry->d_inode->i_mode))
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200503 res = hfsplus_rmdir(new_dir, new_dentry);
Sage Weile3911782011-05-27 13:42:06 -0700504 else
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200505 res = hfsplus_unlink(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (res)
507 return res;
508 }
509
510 res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
511 old_dir, &old_dentry->d_name,
512 new_dir, &new_dentry->d_name);
513 if (!res)
514 new_dentry->d_fsdata = old_dentry->d_fsdata;
515 return res;
516}
517
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800518const struct inode_operations hfsplus_dir_inode_operations = {
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800519 .lookup = hfsplus_lookup,
520 .create = hfsplus_create,
521 .link = hfsplus_link,
522 .unlink = hfsplus_unlink,
523 .mkdir = hfsplus_mkdir,
524 .rmdir = hfsplus_rmdir,
525 .symlink = hfsplus_symlink,
526 .mknod = hfsplus_mknod,
527 .rename = hfsplus_rename,
528 .setxattr = generic_setxattr,
529 .getxattr = generic_getxattr,
530 .listxattr = hfsplus_listxattr,
531 .removexattr = hfsplus_removexattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532};
533
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800534const struct file_operations hfsplus_dir_operations = {
Christoph Hellwigeb29d662010-11-23 14:38:10 +0100535 .fsync = hfsplus_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 .read = generic_read_dir,
Al Viroe72514e2013-05-22 14:59:39 -0400537 .iterate = hfsplus_readdir,
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +0200538 .unlocked_ioctl = hfsplus_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 .llseek = generic_file_llseek,
540 .release = hfsplus_dir_release,
541};