blob: 031c24e5052182fb31351e8b79ba5166959485b7 [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 {
Roman Zippel634725a2006-01-18 17:43:05 -0800106 printk(KERN_ERR "hfs: 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
124static int hfsplus_readdir(struct file *filp, void *dirent, filldir_t filldir)
125{
Al Viro496ad9a2013-01-23 17:07:38 -0500126 struct inode *inode = file_inode(filp);
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
135 if (filp->f_pos >= inode->i_size)
136 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
146 switch ((u32)filp->f_pos) {
147 case 0:
148 /* This is completely artificial... */
149 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
150 goto out;
151 filp->f_pos++;
152 /* fall through */
153 case 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) {
Roman Zippel634725a2006-01-18 17:43:05 -0800162 printk(KERN_ERR "hfs: 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) {
Roman Zippel634725a2006-01-18 17:43:05 -0800167 printk(KERN_ERR "hfs: truncated catalog thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 err = -EIO;
169 goto out;
170 }
171 if (filldir(dirent, "..", 2, 1,
172 be32_to_cpu(entry.thread.parentID), DT_DIR))
173 goto out;
174 filp->f_pos++;
175 /* fall through */
176 default:
177 if (filp->f_pos >= inode->i_size)
178 goto out;
179 err = hfs_brec_goto(&fd, filp->f_pos - 1);
180 if (err)
181 goto out;
182 }
183
184 for (;;) {
185 if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
Roman Zippel634725a2006-01-18 17:43:05 -0800186 printk(KERN_ERR "hfs: walked past end of dir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 err = -EIO;
188 goto out;
189 }
Greg Kroah-Hartman6f24f892012-05-04 12:09:39 -0700190
191 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
192 err = -EIO;
193 goto out;
194 }
195
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200196 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
197 fd.entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 type = be16_to_cpu(entry.type);
199 len = HFSPLUS_MAX_STRLEN;
200 err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
201 if (err)
202 goto out;
203 if (type == HFSPLUS_FOLDER) {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200204 if (fd.entrylength <
205 sizeof(struct hfsplus_cat_folder)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800206 printk(KERN_ERR "hfs: small dir entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 err = -EIO;
208 goto out;
209 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200210 if (HFSPLUS_SB(sb)->hidden_dir &&
211 HFSPLUS_SB(sb)->hidden_dir->i_ino ==
212 be32_to_cpu(entry.folder.id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 goto next;
214 if (filldir(dirent, strbuf, len, filp->f_pos,
215 be32_to_cpu(entry.folder.id), DT_DIR))
216 break;
217 } else if (type == HFSPLUS_FILE) {
218 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800219 printk(KERN_ERR "hfs: small file entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 err = -EIO;
221 goto out;
222 }
223 if (filldir(dirent, strbuf, len, filp->f_pos,
224 be32_to_cpu(entry.file.id), DT_REG))
225 break;
226 } else {
Roman Zippel634725a2006-01-18 17:43:05 -0800227 printk(KERN_ERR "hfs: bad catalog entry type\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 err = -EIO;
229 goto out;
230 }
Anton Salikhmetov20b76432010-12-16 18:08:40 +0200231next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 filp->f_pos++;
233 if (filp->f_pos >= inode->i_size)
234 goto out;
235 err = hfs_brec_goto(&fd, 1);
236 if (err)
237 goto out;
238 }
239 rd = filp->private_data;
240 if (!rd) {
241 rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
242 if (!rd) {
243 err = -ENOMEM;
244 goto out;
245 }
246 filp->private_data = rd;
247 rd->file = filp;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200248 list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250 memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
251out:
252 hfs_find_exit(&fd);
253 return err;
254}
255
256static int hfsplus_dir_release(struct inode *inode, struct file *file)
257{
258 struct hfsplus_readdir_data *rd = file->private_data;
259 if (rd) {
Christoph Hellwig89755dc2010-10-01 05:45:25 +0200260 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 list_del(&rd->list);
Christoph Hellwig89755dc2010-10-01 05:45:25 +0200262 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 kfree(rd);
264 }
265 return 0;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
269 struct dentry *dst_dentry)
270{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200271 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 struct inode *inode = src_dentry->d_inode;
273 struct inode *src_dir = src_dentry->d_parent->d_inode;
274 struct qstr str;
275 char name[32];
276 u32 cnid, id;
277 int res;
278
279 if (HFSPLUS_IS_RSRC(inode))
280 return -EPERM;
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400281 if (!S_ISREG(inode->i_mode))
282 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200284 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
286 for (;;) {
287 get_random_bytes(&id, sizeof(cnid));
288 id &= 0x3fffffff;
289 str.name = name;
290 str.len = sprintf(name, "iNode%d", id);
291 res = hfsplus_rename_cat(inode->i_ino,
292 src_dir, &src_dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200293 sbi->hidden_dir, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (!res)
295 break;
296 if (res != -EEXIST)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200297 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400299 HFSPLUS_I(inode)->linkid = id;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200300 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 src_dentry->d_fsdata = (void *)(unsigned long)cnid;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200302 res = hfsplus_create_cat(cnid, src_dir,
303 &src_dentry->d_name, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (res)
305 /* panic? */
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200306 goto out;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200307 sbi->file_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200309 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
311 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200312 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Dave Hansend8c76e62006-09-30 23:29:04 -0700314 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 hfsplus_instantiate(dst_dentry, inode, cnid);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400316 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 inode->i_ctime = CURRENT_TIME_SEC;
318 mark_inode_dirty(inode);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200319 sbi->file_count++;
Artem Bityutskiy9e6c5822012-07-12 17:26:31 +0300320 hfsplus_mark_mdb_dirty(dst_dir->i_sb);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200321out:
322 mutex_unlock(&sbi->vh_mutex);
323 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
327{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200328 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct inode *inode = dentry->d_inode;
330 struct qstr str;
331 char name[32];
332 u32 cnid;
333 int res;
334
335 if (HFSPLUS_IS_RSRC(inode))
336 return -EPERM;
337
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200338 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 cnid = (u32)(unsigned long)dentry->d_fsdata;
340 if (inode->i_ino == cnid &&
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200341 atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 str.name = name;
343 str.len = sprintf(name, "temp%lu", inode->i_ino);
344 res = hfsplus_rename_cat(inode->i_ino,
345 dir, &dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200346 sbi->hidden_dir, &str);
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200347 if (!res) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 inode->i_flags |= S_DEAD;
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200349 drop_nlink(inode);
350 }
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200351 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
354 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200355 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800357 if (inode->i_nlink > 0)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700358 drop_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200359 if (inode->i_ino == cnid)
Dave Hansence71ec32006-09-30 23:29:06 -0700360 clear_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200361 if (!inode->i_nlink) {
362 if (inode->i_ino != cnid) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200363 sbi->file_count--;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200364 if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Roman Zippel76b0c262008-04-09 17:44:07 +0200365 res = hfsplus_delete_cat(inode->i_ino,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200366 sbi->hidden_dir,
Roman Zippel76b0c262008-04-09 17:44:07 +0200367 NULL);
368 if (!res)
369 hfsplus_delete_inode(inode);
370 } else
371 inode->i_flags |= S_DEAD;
372 } else
373 hfsplus_delete_inode(inode);
374 } else
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200375 sbi->file_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 inode->i_ctime = CURRENT_TIME_SEC;
377 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200378out:
379 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return res;
381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
384{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200385 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
386 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 int res;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (inode->i_size != 2)
390 return -ENOTEMPTY;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200391
392 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
394 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200395 goto out;
Dave Hansence71ec32006-09-30 23:29:06 -0700396 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 inode->i_ctime = CURRENT_TIME_SEC;
398 hfsplus_delete_inode(inode);
399 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200400out:
401 mutex_unlock(&sbi->vh_mutex);
402 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
406 const char *symname)
407{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200408 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 struct inode *inode;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200410 int res = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200412 mutex_lock(&sbi->vh_mutex);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200413 inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200415 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 res = page_symlink(inode, symname, strlen(symname) + 1);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200418 if (res)
419 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200422 if (res)
423 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800425 res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
426 if (res == -EOPNOTSUPP)
427 res = 0; /* Operation is not supported. */
428 else if (res) {
429 /* Try to delete anyway without error analysis. */
430 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
431 goto out_err;
432 }
433
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200434 hfsplus_instantiate(dentry, inode, inode->i_ino);
435 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200436 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200438out_err:
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200439 clear_nlink(inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200440 hfsplus_delete_inode(inode);
441 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200442out:
443 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return res;
445}
446
447static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400448 umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200450 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct inode *inode;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200452 int res = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200454 mutex_lock(&sbi->vh_mutex);
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200455 inode = hfsplus_new_inode(dir->i_sb, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200457 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Christoph Hellwig90e61692010-10-14 09:54:39 -0400459 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
460 init_special_inode(inode, mode, rdev);
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800463 if (res)
464 goto failed_mknod;
465
466 res = hfsplus_init_inode_security(inode, dir, &dentry->d_name);
467 if (res == -EOPNOTSUPP)
468 res = 0; /* Operation is not supported. */
469 else if (res) {
470 /* Try to delete anyway without error analysis. */
471 hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
472 goto failed_mknod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 hfsplus_instantiate(dentry, inode, inode->i_ino);
476 mark_inode_dirty(inode);
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800477 goto out;
478
479failed_mknod:
480 clear_nlink(inode);
481 hfsplus_delete_inode(inode);
482 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200483out:
484 mutex_unlock(&sbi->vh_mutex);
485 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Al Viro4acdaf22011-07-26 01:42:34 -0400488static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400489 bool excl)
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200490{
491 return hfsplus_mknod(dir, dentry, mode, 0);
492}
493
Al Viro18bb1db2011-07-26 01:41:39 -0400494static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200495{
496 return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
500 struct inode *new_dir, struct dentry *new_dentry)
501{
502 int res;
503
504 /* Unlink destination if it already exists */
505 if (new_dentry->d_inode) {
Sage Weile3911782011-05-27 13:42:06 -0700506 if (S_ISDIR(new_dentry->d_inode->i_mode))
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200507 res = hfsplus_rmdir(new_dir, new_dentry);
Sage Weile3911782011-05-27 13:42:06 -0700508 else
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200509 res = hfsplus_unlink(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (res)
511 return res;
512 }
513
514 res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
515 old_dir, &old_dentry->d_name,
516 new_dir, &new_dentry->d_name);
517 if (!res)
518 new_dentry->d_fsdata = old_dentry->d_fsdata;
519 return res;
520}
521
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800522const struct inode_operations hfsplus_dir_inode_operations = {
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800523 .lookup = hfsplus_lookup,
524 .create = hfsplus_create,
525 .link = hfsplus_link,
526 .unlink = hfsplus_unlink,
527 .mkdir = hfsplus_mkdir,
528 .rmdir = hfsplus_rmdir,
529 .symlink = hfsplus_symlink,
530 .mknod = hfsplus_mknod,
531 .rename = hfsplus_rename,
532 .setxattr = generic_setxattr,
533 .getxattr = generic_getxattr,
534 .listxattr = hfsplus_listxattr,
535 .removexattr = hfsplus_removexattr,
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,
541 .readdir = 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};