blob: 88e155f895c6f0376db9e387b9f56f521bbbeb2f [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"
18
19static inline void hfsplus_instantiate(struct dentry *dentry,
20 struct inode *inode, u32 cnid)
21{
22 dentry->d_fsdata = (void *)(unsigned long)cnid;
23 d_instantiate(dentry, inode);
24}
25
26/* Find the entry inside dir named dentry->d_name */
27static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry,
28 struct nameidata *nd)
29{
30 struct inode *inode = NULL;
31 struct hfs_find_data fd;
32 struct super_block *sb;
33 hfsplus_cat_entry entry;
34 int err;
35 u32 cnid, linkid = 0;
36 u16 type;
37
38 sb = dir->i_sb;
Duane Griffind45bce82007-07-15 23:41:23 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 dentry->d_fsdata = NULL;
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +040041 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
42 if (err)
43 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name);
45again:
46 err = hfs_brec_read(&fd, &entry, sizeof(entry));
47 if (err) {
48 if (err == -ENOENT) {
49 hfs_find_exit(&fd);
50 /* No such entry */
51 inode = NULL;
52 goto out;
53 }
54 goto fail;
55 }
56 type = be16_to_cpu(entry.type);
57 if (type == HFSPLUS_FOLDER) {
58 if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
59 err = -EIO;
60 goto fail;
61 }
62 cnid = be32_to_cpu(entry.folder.id);
63 dentry->d_fsdata = (void *)(unsigned long)cnid;
64 } else if (type == HFSPLUS_FILE) {
65 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
66 err = -EIO;
67 goto fail;
68 }
69 cnid = be32_to_cpu(entry.file.id);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020070 if (entry.file.user_info.fdType ==
71 cpu_to_be32(HFSP_HARDLINK_TYPE) &&
72 entry.file.user_info.fdCreator ==
73 cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
74 (entry.file.create_date ==
75 HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->
76 create_date ||
77 entry.file.create_date ==
78 HFSPLUS_I(sb->s_root->d_inode)->
79 create_date) &&
80 HFSPLUS_SB(sb)->hidden_dir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 struct qstr str;
82 char name[32];
83
84 if (dentry->d_fsdata) {
Roman Zippelaf8c85b2006-01-18 17:43:10 -080085 /*
86 * We found a link pointing to another link,
87 * so ignore it and treat it as regular file.
88 */
89 cnid = (unsigned long)dentry->d_fsdata;
90 linkid = 0;
91 } else {
92 dentry->d_fsdata = (void *)(unsigned long)cnid;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020093 linkid =
94 be32_to_cpu(entry.file.permissions.dev);
Roman Zippelaf8c85b2006-01-18 17:43:10 -080095 str.len = sprintf(name, "iNode%d", linkid);
96 str.name = name;
Christoph Hellwigdd73a012010-10-01 05:42:59 +020097 hfsplus_cat_build_key(sb, fd.search_key,
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020098 HFSPLUS_SB(sb)->hidden_dir->i_ino,
99 &str);
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800100 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 } else if (!dentry->d_fsdata)
103 dentry->d_fsdata = (void *)(unsigned long)cnid;
104 } else {
Roman Zippel634725a2006-01-18 17:43:05 -0800105 printk(KERN_ERR "hfs: invalid catalog entry type in lookup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 err = -EIO;
107 goto fail;
108 }
109 hfs_find_exit(&fd);
David Howells63525392008-02-07 00:15:40 -0800110 inode = hfsplus_iget(dir->i_sb, cnid);
111 if (IS_ERR(inode))
112 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (S_ISREG(inode->i_mode))
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400114 HFSPLUS_I(inode)->linkid = linkid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115out:
116 d_add(dentry, inode);
117 return NULL;
118fail:
119 hfs_find_exit(&fd);
120 return ERR_PTR(err);
121}
122
123static int hfsplus_readdir(struct file *filp, void *dirent, filldir_t filldir)
124{
Josef Sipekf44ea032006-12-08 02:37:04 -0800125 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 struct super_block *sb = inode->i_sb;
127 int len, err;
128 char strbuf[HFSPLUS_MAX_STRLEN + 1];
129 hfsplus_cat_entry entry;
130 struct hfs_find_data fd;
131 struct hfsplus_readdir_data *rd;
132 u16 type;
133
134 if (filp->f_pos >= inode->i_size)
135 return 0;
136
Alexey Khoroshilov5bd9d992011-07-06 02:29:59 +0400137 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
138 if (err)
139 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
141 err = hfs_brec_find(&fd);
142 if (err)
143 goto out;
144
145 switch ((u32)filp->f_pos) {
146 case 0:
147 /* This is completely artificial... */
148 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
149 goto out;
150 filp->f_pos++;
151 /* fall through */
152 case 1:
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200153 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
154 fd.entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
Roman Zippel634725a2006-01-18 17:43:05 -0800156 printk(KERN_ERR "hfs: bad catalog folder thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 err = -EIO;
158 goto out;
159 }
160 if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
Roman Zippel634725a2006-01-18 17:43:05 -0800161 printk(KERN_ERR "hfs: truncated catalog thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 err = -EIO;
163 goto out;
164 }
165 if (filldir(dirent, "..", 2, 1,
166 be32_to_cpu(entry.thread.parentID), DT_DIR))
167 goto out;
168 filp->f_pos++;
169 /* fall through */
170 default:
171 if (filp->f_pos >= inode->i_size)
172 goto out;
173 err = hfs_brec_goto(&fd, filp->f_pos - 1);
174 if (err)
175 goto out;
176 }
177
178 for (;;) {
179 if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
Roman Zippel634725a2006-01-18 17:43:05 -0800180 printk(KERN_ERR "hfs: walked past end of dir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 err = -EIO;
182 goto out;
183 }
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200184 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
185 fd.entrylength);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 type = be16_to_cpu(entry.type);
187 len = HFSPLUS_MAX_STRLEN;
188 err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
189 if (err)
190 goto out;
191 if (type == HFSPLUS_FOLDER) {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200192 if (fd.entrylength <
193 sizeof(struct hfsplus_cat_folder)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800194 printk(KERN_ERR "hfs: small dir entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 err = -EIO;
196 goto out;
197 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200198 if (HFSPLUS_SB(sb)->hidden_dir &&
199 HFSPLUS_SB(sb)->hidden_dir->i_ino ==
200 be32_to_cpu(entry.folder.id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 goto next;
202 if (filldir(dirent, strbuf, len, filp->f_pos,
203 be32_to_cpu(entry.folder.id), DT_DIR))
204 break;
205 } else if (type == HFSPLUS_FILE) {
206 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800207 printk(KERN_ERR "hfs: small file entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 err = -EIO;
209 goto out;
210 }
211 if (filldir(dirent, strbuf, len, filp->f_pos,
212 be32_to_cpu(entry.file.id), DT_REG))
213 break;
214 } else {
Roman Zippel634725a2006-01-18 17:43:05 -0800215 printk(KERN_ERR "hfs: bad catalog entry type\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 err = -EIO;
217 goto out;
218 }
Anton Salikhmetov20b76432010-12-16 18:08:40 +0200219next:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 filp->f_pos++;
221 if (filp->f_pos >= inode->i_size)
222 goto out;
223 err = hfs_brec_goto(&fd, 1);
224 if (err)
225 goto out;
226 }
227 rd = filp->private_data;
228 if (!rd) {
229 rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
230 if (!rd) {
231 err = -ENOMEM;
232 goto out;
233 }
234 filp->private_data = rd;
235 rd->file = filp;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200236 list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238 memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
239out:
240 hfs_find_exit(&fd);
241 return err;
242}
243
244static int hfsplus_dir_release(struct inode *inode, struct file *file)
245{
246 struct hfsplus_readdir_data *rd = file->private_data;
247 if (rd) {
Christoph Hellwig89755dc2010-10-01 05:45:25 +0200248 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 list_del(&rd->list);
Christoph Hellwig89755dc2010-10-01 05:45:25 +0200250 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 kfree(rd);
252 }
253 return 0;
254}
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
257 struct dentry *dst_dentry)
258{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200259 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 struct inode *inode = src_dentry->d_inode;
261 struct inode *src_dir = src_dentry->d_parent->d_inode;
262 struct qstr str;
263 char name[32];
264 u32 cnid, id;
265 int res;
266
267 if (HFSPLUS_IS_RSRC(inode))
268 return -EPERM;
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400269 if (!S_ISREG(inode->i_mode))
270 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200272 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
274 for (;;) {
275 get_random_bytes(&id, sizeof(cnid));
276 id &= 0x3fffffff;
277 str.name = name;
278 str.len = sprintf(name, "iNode%d", id);
279 res = hfsplus_rename_cat(inode->i_ino,
280 src_dir, &src_dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200281 sbi->hidden_dir, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (!res)
283 break;
284 if (res != -EEXIST)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200285 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Christoph Hellwigf6089ff2010-10-14 09:54:28 -0400287 HFSPLUS_I(inode)->linkid = id;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200288 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 src_dentry->d_fsdata = (void *)(unsigned long)cnid;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200290 res = hfsplus_create_cat(cnid, src_dir,
291 &src_dentry->d_name, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (res)
293 /* panic? */
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200294 goto out;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200295 sbi->file_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200297 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
299 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200300 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Dave Hansend8c76e62006-09-30 23:29:04 -0700302 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 hfsplus_instantiate(dst_dentry, inode, cnid);
Al Viro7de9c6ee2010-10-23 11:11:40 -0400304 ihold(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 inode->i_ctime = CURRENT_TIME_SEC;
306 mark_inode_dirty(inode);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200307 sbi->file_count++;
308 dst_dir->i_sb->s_dirt = 1;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200309out:
310 mutex_unlock(&sbi->vh_mutex);
311 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
315{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200316 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct inode *inode = dentry->d_inode;
318 struct qstr str;
319 char name[32];
320 u32 cnid;
321 int res;
322
323 if (HFSPLUS_IS_RSRC(inode))
324 return -EPERM;
325
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200326 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 cnid = (u32)(unsigned long)dentry->d_fsdata;
328 if (inode->i_ino == cnid &&
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200329 atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 str.name = name;
331 str.len = sprintf(name, "temp%lu", inode->i_ino);
332 res = hfsplus_rename_cat(inode->i_ino,
333 dir, &dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200334 sbi->hidden_dir, &str);
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200335 if (!res) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 inode->i_flags |= S_DEAD;
Christoph Hellwig85b8fe82010-10-27 13:45:50 +0200337 drop_nlink(inode);
338 }
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200339 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341 res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
342 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200343 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800345 if (inode->i_nlink > 0)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700346 drop_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200347 if (inode->i_ino == cnid)
Dave Hansence71ec32006-09-30 23:29:06 -0700348 clear_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200349 if (!inode->i_nlink) {
350 if (inode->i_ino != cnid) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200351 sbi->file_count--;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200352 if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Roman Zippel76b0c262008-04-09 17:44:07 +0200353 res = hfsplus_delete_cat(inode->i_ino,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200354 sbi->hidden_dir,
Roman Zippel76b0c262008-04-09 17:44:07 +0200355 NULL);
356 if (!res)
357 hfsplus_delete_inode(inode);
358 } else
359 inode->i_flags |= S_DEAD;
360 } else
361 hfsplus_delete_inode(inode);
362 } else
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200363 sbi->file_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 inode->i_ctime = CURRENT_TIME_SEC;
365 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200366out:
367 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return res;
369}
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
372{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200373 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
374 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 int res;
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (inode->i_size != 2)
378 return -ENOTEMPTY;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200379
380 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
382 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200383 goto out;
Dave Hansence71ec32006-09-30 23:29:06 -0700384 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 inode->i_ctime = CURRENT_TIME_SEC;
386 hfsplus_delete_inode(inode);
387 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200388out:
389 mutex_unlock(&sbi->vh_mutex);
390 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
393static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
394 const char *symname)
395{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200396 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct inode *inode;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200398 int res = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200400 mutex_lock(&sbi->vh_mutex);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200401 inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200403 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 res = page_symlink(inode, symname, strlen(symname) + 1);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200406 if (res)
407 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200410 if (res)
411 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200413 hfsplus_instantiate(dentry, inode, inode->i_ino);
414 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200415 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200417out_err:
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200418 clear_nlink(inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200419 hfsplus_delete_inode(inode);
420 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200421out:
422 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return res;
424}
425
426static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -0400427 umode_t mode, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200429 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 struct inode *inode;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200431 int res = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200433 mutex_lock(&sbi->vh_mutex);
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200434 inode = hfsplus_new_inode(dir->i_sb, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200436 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Christoph Hellwig90e61692010-10-14 09:54:39 -0400438 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
439 init_special_inode(inode, mode, rdev);
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
442 if (res) {
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +0200443 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 hfsplus_delete_inode(inode);
445 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 hfsplus_instantiate(dentry, inode, inode->i_ino);
450 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200451out:
452 mutex_unlock(&sbi->vh_mutex);
453 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Al Viro4acdaf22011-07-26 01:42:34 -0400456static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200457 struct nameidata *nd)
458{
459 return hfsplus_mknod(dir, dentry, mode, 0);
460}
461
Al Viro18bb1db2011-07-26 01:41:39 -0400462static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200463{
464 return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
468 struct inode *new_dir, struct dentry *new_dentry)
469{
470 int res;
471
472 /* Unlink destination if it already exists */
473 if (new_dentry->d_inode) {
Sage Weile3911782011-05-27 13:42:06 -0700474 if (S_ISDIR(new_dentry->d_inode->i_mode))
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200475 res = hfsplus_rmdir(new_dir, new_dentry);
Sage Weile3911782011-05-27 13:42:06 -0700476 else
Christoph Hellwig40de9a72010-10-01 09:12:08 +0200477 res = hfsplus_unlink(new_dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (res)
479 return res;
480 }
481
482 res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
483 old_dir, &old_dentry->d_name,
484 new_dir, &new_dentry->d_name);
485 if (!res)
486 new_dentry->d_fsdata = old_dentry->d_fsdata;
487 return res;
488}
489
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800490const struct inode_operations hfsplus_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 .lookup = hfsplus_lookup,
492 .create = hfsplus_create,
493 .link = hfsplus_link,
494 .unlink = hfsplus_unlink,
495 .mkdir = hfsplus_mkdir,
496 .rmdir = hfsplus_rmdir,
497 .symlink = hfsplus_symlink,
498 .mknod = hfsplus_mknod,
499 .rename = hfsplus_rename,
500};
501
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800502const struct file_operations hfsplus_dir_operations = {
Christoph Hellwigeb29d662010-11-23 14:38:10 +0100503 .fsync = hfsplus_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 .read = generic_read_dir,
505 .readdir = hfsplus_readdir,
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +0200506 .unlocked_ioctl = hfsplus_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 .llseek = generic_file_llseek,
508 .release = hfsplus_dir_release,
509};