blob: 1c81eedcab0121f012910f880604cf9160dc1c01 [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
40 dentry->d_op = &hfsplus_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 dentry->d_fsdata = NULL;
Christoph Hellwigdd73a012010-10-01 05:42:59 +020042 hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name);
44again:
45 err = hfs_brec_read(&fd, &entry, sizeof(entry));
46 if (err) {
47 if (err == -ENOENT) {
48 hfs_find_exit(&fd);
49 /* No such entry */
50 inode = NULL;
51 goto out;
52 }
53 goto fail;
54 }
55 type = be16_to_cpu(entry.type);
56 if (type == HFSPLUS_FOLDER) {
57 if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
58 err = -EIO;
59 goto fail;
60 }
61 cnid = be32_to_cpu(entry.folder.id);
62 dentry->d_fsdata = (void *)(unsigned long)cnid;
63 } else if (type == HFSPLUS_FILE) {
64 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
65 err = -EIO;
66 goto fail;
67 }
68 cnid = be32_to_cpu(entry.file.id);
69 if (entry.file.user_info.fdType == cpu_to_be32(HFSP_HARDLINK_TYPE) &&
Roman Zippelaf8c85b2006-01-18 17:43:10 -080070 entry.file.user_info.fdCreator == cpu_to_be32(HFSP_HFSPLUS_CREATOR) &&
Christoph Hellwig6af502d2010-10-01 05:43:31 +020071 (entry.file.create_date == HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)->create_date ||
72 entry.file.create_date == HFSPLUS_I(sb->s_root->d_inode)->create_date) &&
Christoph Hellwigdd73a012010-10-01 05:42:59 +020073 HFSPLUS_SB(sb)->hidden_dir) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 struct qstr str;
75 char name[32];
76
77 if (dentry->d_fsdata) {
Roman Zippelaf8c85b2006-01-18 17:43:10 -080078 /*
79 * We found a link pointing to another link,
80 * so ignore it and treat it as regular file.
81 */
82 cnid = (unsigned long)dentry->d_fsdata;
83 linkid = 0;
84 } else {
85 dentry->d_fsdata = (void *)(unsigned long)cnid;
86 linkid = be32_to_cpu(entry.file.permissions.dev);
87 str.len = sprintf(name, "iNode%d", linkid);
88 str.name = name;
Christoph Hellwigdd73a012010-10-01 05:42:59 +020089 hfsplus_cat_build_key(sb, fd.search_key,
90 HFSPLUS_SB(sb)->hidden_dir->i_ino, &str);
Roman Zippelaf8c85b2006-01-18 17:43:10 -080091 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 } else if (!dentry->d_fsdata)
94 dentry->d_fsdata = (void *)(unsigned long)cnid;
95 } else {
Roman Zippel634725a2006-01-18 17:43:05 -080096 printk(KERN_ERR "hfs: invalid catalog entry type in lookup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 err = -EIO;
98 goto fail;
99 }
100 hfs_find_exit(&fd);
David Howells63525392008-02-07 00:15:40 -0800101 inode = hfsplus_iget(dir->i_sb, cnid);
102 if (IS_ERR(inode))
103 return ERR_CAST(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (S_ISREG(inode->i_mode))
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200105 HFSPLUS_I(inode)->dev = linkid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106out:
107 d_add(dentry, inode);
108 return NULL;
109fail:
110 hfs_find_exit(&fd);
111 return ERR_PTR(err);
112}
113
114static int hfsplus_readdir(struct file *filp, void *dirent, filldir_t filldir)
115{
Josef Sipekf44ea032006-12-08 02:37:04 -0800116 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct super_block *sb = inode->i_sb;
118 int len, err;
119 char strbuf[HFSPLUS_MAX_STRLEN + 1];
120 hfsplus_cat_entry entry;
121 struct hfs_find_data fd;
122 struct hfsplus_readdir_data *rd;
123 u16 type;
124
125 if (filp->f_pos >= inode->i_size)
126 return 0;
127
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200128 hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
130 err = hfs_brec_find(&fd);
131 if (err)
132 goto out;
133
134 switch ((u32)filp->f_pos) {
135 case 0:
136 /* This is completely artificial... */
137 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
138 goto out;
139 filp->f_pos++;
140 /* fall through */
141 case 1:
142 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
143 if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
Roman Zippel634725a2006-01-18 17:43:05 -0800144 printk(KERN_ERR "hfs: bad catalog folder thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 err = -EIO;
146 goto out;
147 }
148 if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
Roman Zippel634725a2006-01-18 17:43:05 -0800149 printk(KERN_ERR "hfs: truncated catalog thread\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 err = -EIO;
151 goto out;
152 }
153 if (filldir(dirent, "..", 2, 1,
154 be32_to_cpu(entry.thread.parentID), DT_DIR))
155 goto out;
156 filp->f_pos++;
157 /* fall through */
158 default:
159 if (filp->f_pos >= inode->i_size)
160 goto out;
161 err = hfs_brec_goto(&fd, filp->f_pos - 1);
162 if (err)
163 goto out;
164 }
165
166 for (;;) {
167 if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) {
Roman Zippel634725a2006-01-18 17:43:05 -0800168 printk(KERN_ERR "hfs: walked past end of dir\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 err = -EIO;
170 goto out;
171 }
172 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
173 type = be16_to_cpu(entry.type);
174 len = HFSPLUS_MAX_STRLEN;
175 err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len);
176 if (err)
177 goto out;
178 if (type == HFSPLUS_FOLDER) {
179 if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800180 printk(KERN_ERR "hfs: small dir entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 err = -EIO;
182 goto out;
183 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200184 if (HFSPLUS_SB(sb)->hidden_dir &&
185 HFSPLUS_SB(sb)->hidden_dir->i_ino ==
186 be32_to_cpu(entry.folder.id))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 goto next;
188 if (filldir(dirent, strbuf, len, filp->f_pos,
189 be32_to_cpu(entry.folder.id), DT_DIR))
190 break;
191 } else if (type == HFSPLUS_FILE) {
192 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
Roman Zippel634725a2006-01-18 17:43:05 -0800193 printk(KERN_ERR "hfs: small file entry\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 err = -EIO;
195 goto out;
196 }
197 if (filldir(dirent, strbuf, len, filp->f_pos,
198 be32_to_cpu(entry.file.id), DT_REG))
199 break;
200 } else {
Roman Zippel634725a2006-01-18 17:43:05 -0800201 printk(KERN_ERR "hfs: bad catalog entry type\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 err = -EIO;
203 goto out;
204 }
205 next:
206 filp->f_pos++;
207 if (filp->f_pos >= inode->i_size)
208 goto out;
209 err = hfs_brec_goto(&fd, 1);
210 if (err)
211 goto out;
212 }
213 rd = filp->private_data;
214 if (!rd) {
215 rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL);
216 if (!rd) {
217 err = -ENOMEM;
218 goto out;
219 }
220 filp->private_data = rd;
221 rd->file = filp;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200222 list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key));
225out:
226 hfs_find_exit(&fd);
227 return err;
228}
229
230static int hfsplus_dir_release(struct inode *inode, struct file *file)
231{
232 struct hfsplus_readdir_data *rd = file->private_data;
233 if (rd) {
234 list_del(&rd->list);
235 kfree(rd);
236 }
237 return 0;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
241 struct dentry *dst_dentry)
242{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200243 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct inode *inode = src_dentry->d_inode;
245 struct inode *src_dir = src_dentry->d_parent->d_inode;
246 struct qstr str;
247 char name[32];
248 u32 cnid, id;
249 int res;
250
251 if (HFSPLUS_IS_RSRC(inode))
252 return -EPERM;
253
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200254 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) {
256 for (;;) {
257 get_random_bytes(&id, sizeof(cnid));
258 id &= 0x3fffffff;
259 str.name = name;
260 str.len = sprintf(name, "iNode%d", id);
261 res = hfsplus_rename_cat(inode->i_ino,
262 src_dir, &src_dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200263 sbi->hidden_dir, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!res)
265 break;
266 if (res != -EEXIST)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200267 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200269 HFSPLUS_I(inode)->dev = id;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200270 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 src_dentry->d_fsdata = (void *)(unsigned long)cnid;
272 res = hfsplus_create_cat(cnid, src_dir, &src_dentry->d_name, inode);
273 if (res)
274 /* panic? */
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200275 goto out;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200276 sbi->file_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200278 cnid = sbi->next_cnid++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
280 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200281 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Dave Hansend8c76e62006-09-30 23:29:04 -0700283 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 hfsplus_instantiate(dst_dentry, inode, cnid);
285 atomic_inc(&inode->i_count);
286 inode->i_ctime = CURRENT_TIME_SEC;
287 mark_inode_dirty(inode);
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200288 sbi->file_count++;
289 dst_dir->i_sb->s_dirt = 1;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200290out:
291 mutex_unlock(&sbi->vh_mutex);
292 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
296{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200297 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct inode *inode = dentry->d_inode;
299 struct qstr str;
300 char name[32];
301 u32 cnid;
302 int res;
303
304 if (HFSPLUS_IS_RSRC(inode))
305 return -EPERM;
306
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200307 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 cnid = (u32)(unsigned long)dentry->d_fsdata;
309 if (inode->i_ino == cnid &&
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200310 atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 str.name = name;
312 str.len = sprintf(name, "temp%lu", inode->i_ino);
313 res = hfsplus_rename_cat(inode->i_ino,
314 dir, &dentry->d_name,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200315 sbi->hidden_dir, &str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (!res)
317 inode->i_flags |= S_DEAD;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200318 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320 res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
321 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200322 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Roman Zippelaf8c85b2006-01-18 17:43:10 -0800324 if (inode->i_nlink > 0)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700325 drop_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200326 if (inode->i_ino == cnid)
Dave Hansence71ec32006-09-30 23:29:06 -0700327 clear_nlink(inode);
Roman Zippel76b0c262008-04-09 17:44:07 +0200328 if (!inode->i_nlink) {
329 if (inode->i_ino != cnid) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200330 sbi->file_count--;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200331 if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
Roman Zippel76b0c262008-04-09 17:44:07 +0200332 res = hfsplus_delete_cat(inode->i_ino,
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200333 sbi->hidden_dir,
Roman Zippel76b0c262008-04-09 17:44:07 +0200334 NULL);
335 if (!res)
336 hfsplus_delete_inode(inode);
337 } else
338 inode->i_flags |= S_DEAD;
339 } else
340 hfsplus_delete_inode(inode);
341 } else
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200342 sbi->file_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 inode->i_ctime = CURRENT_TIME_SEC;
344 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200345out:
346 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return res;
348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
351{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200352 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
353 struct inode *inode = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int res;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (inode->i_size != 2)
357 return -ENOTEMPTY;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200358
359 mutex_lock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
361 if (res)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200362 goto out;
Dave Hansence71ec32006-09-30 23:29:06 -0700363 clear_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 inode->i_ctime = CURRENT_TIME_SEC;
365 hfsplus_delete_inode(inode);
366 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200367out:
368 mutex_unlock(&sbi->vh_mutex);
369 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
373 const char *symname)
374{
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200375 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct inode *inode;
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200377 int res = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200379 mutex_lock(&sbi->vh_mutex);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200380 inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (!inode)
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200382 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 res = page_symlink(inode, symname, strlen(symname) + 1);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200385 if (res)
386 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200389 if (res)
390 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200392 hfsplus_instantiate(dentry, inode, inode->i_ino);
393 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200394 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Christoph Hellwigf17c89b2010-10-01 05:43:54 +0200396out_err:
397 inode->i_nlink = 0;
398 hfsplus_delete_inode(inode);
399 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200400out:
401 mutex_unlock(&sbi->vh_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return res;
403}
404
405static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
406 int mode, dev_t rdev)
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 Hellwig30d3abb2010-10-01 05:43:50 +0200413 inode = hfsplus_new_inode(dir->i_sb, mode);
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 = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
418 if (res) {
419 inode->i_nlink = 0;
420 hfsplus_delete_inode(inode);
421 iput(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200422 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200424
425 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
426 init_special_inode(inode, mode, rdev);
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 hfsplus_instantiate(dentry, inode, inode->i_ino);
429 mark_inode_dirty(inode);
Christoph Hellwig7ac9fb92010-10-01 05:45:08 +0200430out:
431 mutex_unlock(&sbi->vh_mutex);
432 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Christoph Hellwig30d3abb2010-10-01 05:43:50 +0200435static int hfsplus_create(struct inode *dir, struct dentry *dentry, int mode,
436 struct nameidata *nd)
437{
438 return hfsplus_mknod(dir, dentry, mode, 0);
439}
440
441static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, int mode)
442{
443 return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
444}
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry,
447 struct inode *new_dir, struct dentry *new_dentry)
448{
449 int res;
450
451 /* Unlink destination if it already exists */
452 if (new_dentry->d_inode) {
453 res = hfsplus_unlink(new_dir, new_dentry);
454 if (res)
455 return res;
456 }
457
458 res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata,
459 old_dir, &old_dentry->d_name,
460 new_dir, &new_dentry->d_name);
461 if (!res)
462 new_dentry->d_fsdata = old_dentry->d_fsdata;
463 return res;
464}
465
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800466const struct inode_operations hfsplus_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 .lookup = hfsplus_lookup,
468 .create = hfsplus_create,
469 .link = hfsplus_link,
470 .unlink = hfsplus_unlink,
471 .mkdir = hfsplus_mkdir,
472 .rmdir = hfsplus_rmdir,
473 .symlink = hfsplus_symlink,
474 .mknod = hfsplus_mknod,
475 .rename = hfsplus_rename,
476};
477
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800478const struct file_operations hfsplus_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 .read = generic_read_dir,
480 .readdir = hfsplus_readdir,
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +0200481 .unlocked_ioctl = hfsplus_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 .llseek = generic_file_llseek,
483 .release = hfsplus_dir_release,
484};