]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/hfsplus/dir.c | |
3 | * | |
4 | * Copyright (C) 2001 | |
5 | * Brad Boyer ([email protected]) | |
6 | * (C) 2003 Ardis Technologies <[email protected]> | |
7 | * | |
8 | * Handling of directories | |
9 | */ | |
10 | ||
11 | #include <linux/errno.h> | |
12 | #include <linux/fs.h> | |
1da177e4 LT |
13 | #include <linux/slab.h> |
14 | #include <linux/random.h> | |
017f8da4 | 15 | #include <linux/nls.h> |
1da177e4 LT |
16 | |
17 | #include "hfsplus_fs.h" | |
18 | #include "hfsplus_raw.h" | |
324ef39a | 19 | #include "xattr.h" |
b4c1107c | 20 | #include "acl.h" |
1da177e4 LT |
21 | |
22 | static inline void hfsplus_instantiate(struct dentry *dentry, | |
23 | struct inode *inode, u32 cnid) | |
24 | { | |
25 | dentry->d_fsdata = (void *)(unsigned long)cnid; | |
26 | d_instantiate(dentry, inode); | |
27 | } | |
28 | ||
29 | /* Find the entry inside dir named dentry->d_name */ | |
30 | static struct dentry *hfsplus_lookup(struct inode *dir, struct dentry *dentry, | |
00cd8dd3 | 31 | unsigned int flags) |
1da177e4 LT |
32 | { |
33 | struct inode *inode = NULL; | |
34 | struct hfs_find_data fd; | |
35 | struct super_block *sb; | |
36 | hfsplus_cat_entry entry; | |
37 | int err; | |
38 | u32 cnid, linkid = 0; | |
39 | u16 type; | |
40 | ||
41 | sb = dir->i_sb; | |
d45bce8f | 42 | |
1da177e4 | 43 | dentry->d_fsdata = NULL; |
5bd9d99d AK |
44 | err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd); |
45 | if (err) | |
46 | return ERR_PTR(err); | |
89ac9b4d SS |
47 | err = hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, |
48 | &dentry->d_name); | |
49 | if (unlikely(err < 0)) | |
50 | goto fail; | |
1da177e4 LT |
51 | again: |
52 | err = hfs_brec_read(&fd, &entry, sizeof(entry)); | |
53 | if (err) { | |
54 | if (err == -ENOENT) { | |
55 | hfs_find_exit(&fd); | |
56 | /* No such entry */ | |
57 | inode = NULL; | |
58 | goto out; | |
59 | } | |
60 | goto fail; | |
61 | } | |
62 | type = be16_to_cpu(entry.type); | |
63 | if (type == HFSPLUS_FOLDER) { | |
64 | if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) { | |
65 | err = -EIO; | |
66 | goto fail; | |
67 | } | |
68 | cnid = be32_to_cpu(entry.folder.id); | |
69 | dentry->d_fsdata = (void *)(unsigned long)cnid; | |
70 | } else if (type == HFSPLUS_FILE) { | |
71 | if (fd.entrylength < sizeof(struct hfsplus_cat_file)) { | |
72 | err = -EIO; | |
73 | goto fail; | |
74 | } | |
75 | cnid = be32_to_cpu(entry.file.id); | |
2753cc28 AS |
76 | if (entry.file.user_info.fdType == |
77 | cpu_to_be32(HFSP_HARDLINK_TYPE) && | |
78 | entry.file.user_info.fdCreator == | |
79 | cpu_to_be32(HFSP_HFSPLUS_CREATOR) && | |
80 | (entry.file.create_date == | |
81 | HFSPLUS_I(HFSPLUS_SB(sb)->hidden_dir)-> | |
82 | create_date || | |
83 | entry.file.create_date == | |
2b0143b5 | 84 | HFSPLUS_I(d_inode(sb->s_root))-> |
2753cc28 AS |
85 | create_date) && |
86 | HFSPLUS_SB(sb)->hidden_dir) { | |
1da177e4 LT |
87 | struct qstr str; |
88 | char name[32]; | |
89 | ||
90 | if (dentry->d_fsdata) { | |
af8c85bb RZ |
91 | /* |
92 | * We found a link pointing to another link, | |
93 | * so ignore it and treat it as regular file. | |
94 | */ | |
95 | cnid = (unsigned long)dentry->d_fsdata; | |
96 | linkid = 0; | |
97 | } else { | |
98 | dentry->d_fsdata = (void *)(unsigned long)cnid; | |
2753cc28 AS |
99 | linkid = |
100 | be32_to_cpu(entry.file.permissions.dev); | |
af8c85bb RZ |
101 | str.len = sprintf(name, "iNode%d", linkid); |
102 | str.name = name; | |
89ac9b4d | 103 | err = hfsplus_cat_build_key(sb, fd.search_key, |
2753cc28 AS |
104 | HFSPLUS_SB(sb)->hidden_dir->i_ino, |
105 | &str); | |
89ac9b4d SS |
106 | if (unlikely(err < 0)) |
107 | goto fail; | |
af8c85bb | 108 | goto again; |
1da177e4 | 109 | } |
1da177e4 LT |
110 | } else if (!dentry->d_fsdata) |
111 | dentry->d_fsdata = (void *)(unsigned long)cnid; | |
112 | } else { | |
d6142673 | 113 | pr_err("invalid catalog entry type in lookup\n"); |
1da177e4 LT |
114 | err = -EIO; |
115 | goto fail; | |
116 | } | |
117 | hfs_find_exit(&fd); | |
63525391 DH |
118 | inode = hfsplus_iget(dir->i_sb, cnid); |
119 | if (IS_ERR(inode)) | |
120 | return ERR_CAST(inode); | |
1da177e4 | 121 | if (S_ISREG(inode->i_mode)) |
f6089ff8 | 122 | HFSPLUS_I(inode)->linkid = linkid; |
1da177e4 LT |
123 | out: |
124 | d_add(dentry, inode); | |
125 | return NULL; | |
126 | fail: | |
127 | hfs_find_exit(&fd); | |
128 | return ERR_PTR(err); | |
129 | } | |
130 | ||
e72514e7 | 131 | static int hfsplus_readdir(struct file *file, struct dir_context *ctx) |
1da177e4 | 132 | { |
e72514e7 | 133 | struct inode *inode = file_inode(file); |
1da177e4 LT |
134 | struct super_block *sb = inode->i_sb; |
135 | int len, err; | |
017f8da4 | 136 | char *strbuf; |
1da177e4 LT |
137 | hfsplus_cat_entry entry; |
138 | struct hfs_find_data fd; | |
139 | struct hfsplus_readdir_data *rd; | |
140 | u16 type; | |
141 | ||
e72514e7 | 142 | if (file->f_pos >= inode->i_size) |
1da177e4 LT |
143 | return 0; |
144 | ||
5bd9d99d AK |
145 | err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd); |
146 | if (err) | |
147 | return err; | |
017f8da4 HTL |
148 | strbuf = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN + 1, GFP_KERNEL); |
149 | if (!strbuf) { | |
150 | err = -ENOMEM; | |
151 | goto out; | |
152 | } | |
89ac9b4d | 153 | hfsplus_cat_build_key_with_cnid(sb, fd.search_key, inode->i_ino); |
324ef39a | 154 | err = hfs_brec_find(&fd, hfs_find_rec_by_key); |
1da177e4 LT |
155 | if (err) |
156 | goto out; | |
157 | ||
e72514e7 | 158 | if (ctx->pos == 0) { |
1da177e4 | 159 | /* This is completely artificial... */ |
e72514e7 | 160 | if (!dir_emit_dot(file, ctx)) |
1da177e4 | 161 | goto out; |
e72514e7 AV |
162 | ctx->pos = 1; |
163 | } | |
164 | if (ctx->pos == 1) { | |
6f24f892 GKH |
165 | if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) { |
166 | err = -EIO; | |
167 | goto out; | |
168 | } | |
169 | ||
2753cc28 AS |
170 | hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, |
171 | fd.entrylength); | |
1da177e4 | 172 | if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) { |
d6142673 | 173 | pr_err("bad catalog folder thread\n"); |
1da177e4 LT |
174 | err = -EIO; |
175 | goto out; | |
176 | } | |
177 | if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) { | |
d6142673 | 178 | pr_err("truncated catalog thread\n"); |
1da177e4 LT |
179 | err = -EIO; |
180 | goto out; | |
181 | } | |
e72514e7 | 182 | if (!dir_emit(ctx, "..", 2, |
1da177e4 LT |
183 | be32_to_cpu(entry.thread.parentID), DT_DIR)) |
184 | goto out; | |
e72514e7 | 185 | ctx->pos = 2; |
1da177e4 | 186 | } |
e72514e7 AV |
187 | if (ctx->pos >= inode->i_size) |
188 | goto out; | |
189 | err = hfs_brec_goto(&fd, ctx->pos - 1); | |
190 | if (err) | |
191 | goto out; | |
1da177e4 LT |
192 | for (;;) { |
193 | if (be32_to_cpu(fd.key->cat.parent) != inode->i_ino) { | |
d6142673 | 194 | pr_err("walked past end of dir\n"); |
1da177e4 LT |
195 | err = -EIO; |
196 | goto out; | |
197 | } | |
6f24f892 GKH |
198 | |
199 | if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) { | |
200 | err = -EIO; | |
201 | goto out; | |
202 | } | |
203 | ||
2753cc28 AS |
204 | hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, |
205 | fd.entrylength); | |
1da177e4 | 206 | type = be16_to_cpu(entry.type); |
017f8da4 | 207 | len = NLS_MAX_CHARSET_SIZE * HFSPLUS_MAX_STRLEN; |
1da177e4 LT |
208 | err = hfsplus_uni2asc(sb, &fd.key->cat.name, strbuf, &len); |
209 | if (err) | |
210 | goto out; | |
211 | if (type == HFSPLUS_FOLDER) { | |
2753cc28 AS |
212 | if (fd.entrylength < |
213 | sizeof(struct hfsplus_cat_folder)) { | |
d6142673 | 214 | pr_err("small dir entry\n"); |
1da177e4 LT |
215 | err = -EIO; |
216 | goto out; | |
217 | } | |
dd73a01a CH |
218 | if (HFSPLUS_SB(sb)->hidden_dir && |
219 | HFSPLUS_SB(sb)->hidden_dir->i_ino == | |
220 | be32_to_cpu(entry.folder.id)) | |
1da177e4 | 221 | goto next; |
e72514e7 | 222 | if (!dir_emit(ctx, strbuf, len, |
1da177e4 LT |
223 | be32_to_cpu(entry.folder.id), DT_DIR)) |
224 | break; | |
225 | } else if (type == HFSPLUS_FILE) { | |
97a62eae SA |
226 | u16 mode; |
227 | unsigned type = DT_UNKNOWN; | |
228 | ||
1da177e4 | 229 | if (fd.entrylength < sizeof(struct hfsplus_cat_file)) { |
d6142673 | 230 | pr_err("small file entry\n"); |
1da177e4 LT |
231 | err = -EIO; |
232 | goto out; | |
233 | } | |
97a62eae SA |
234 | |
235 | mode = be16_to_cpu(entry.file.permissions.mode); | |
236 | if (S_ISREG(mode)) | |
237 | type = DT_REG; | |
238 | else if (S_ISLNK(mode)) | |
239 | type = DT_LNK; | |
240 | else if (S_ISFIFO(mode)) | |
241 | type = DT_FIFO; | |
242 | else if (S_ISCHR(mode)) | |
243 | type = DT_CHR; | |
244 | else if (S_ISBLK(mode)) | |
245 | type = DT_BLK; | |
246 | else if (S_ISSOCK(mode)) | |
247 | type = DT_SOCK; | |
248 | ||
e72514e7 | 249 | if (!dir_emit(ctx, strbuf, len, |
97a62eae | 250 | be32_to_cpu(entry.file.id), type)) |
1da177e4 LT |
251 | break; |
252 | } else { | |
d6142673 | 253 | pr_err("bad catalog entry type\n"); |
1da177e4 LT |
254 | err = -EIO; |
255 | goto out; | |
256 | } | |
20b7643d | 257 | next: |
e72514e7 AV |
258 | ctx->pos++; |
259 | if (ctx->pos >= inode->i_size) | |
1da177e4 LT |
260 | goto out; |
261 | err = hfs_brec_goto(&fd, 1); | |
262 | if (err) | |
263 | goto out; | |
264 | } | |
e72514e7 | 265 | rd = file->private_data; |
1da177e4 LT |
266 | if (!rd) { |
267 | rd = kmalloc(sizeof(struct hfsplus_readdir_data), GFP_KERNEL); | |
268 | if (!rd) { | |
269 | err = -ENOMEM; | |
270 | goto out; | |
271 | } | |
e72514e7 AV |
272 | file->private_data = rd; |
273 | rd->file = file; | |
323ee8fc | 274 | spin_lock(&HFSPLUS_I(inode)->open_dir_lock); |
6af502de | 275 | list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list); |
323ee8fc | 276 | spin_unlock(&HFSPLUS_I(inode)->open_dir_lock); |
1da177e4 | 277 | } |
323ee8fc AV |
278 | /* |
279 | * Can be done after the list insertion; exclusion with | |
280 | * hfsplus_delete_cat() is provided by directory lock. | |
281 | */ | |
1da177e4 LT |
282 | memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key)); |
283 | out: | |
017f8da4 | 284 | kfree(strbuf); |
1da177e4 LT |
285 | hfs_find_exit(&fd); |
286 | return err; | |
287 | } | |
288 | ||
289 | static int hfsplus_dir_release(struct inode *inode, struct file *file) | |
290 | { | |
291 | struct hfsplus_readdir_data *rd = file->private_data; | |
292 | if (rd) { | |
323ee8fc | 293 | spin_lock(&HFSPLUS_I(inode)->open_dir_lock); |
1da177e4 | 294 | list_del(&rd->list); |
323ee8fc | 295 | spin_unlock(&HFSPLUS_I(inode)->open_dir_lock); |
1da177e4 LT |
296 | kfree(rd); |
297 | } | |
298 | return 0; | |
299 | } | |
300 | ||
1da177e4 LT |
301 | static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir, |
302 | struct dentry *dst_dentry) | |
303 | { | |
dd73a01a | 304 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb); |
2b0143b5 DH |
305 | struct inode *inode = d_inode(src_dentry); |
306 | struct inode *src_dir = d_inode(src_dentry->d_parent); | |
1da177e4 LT |
307 | struct qstr str; |
308 | char name[32]; | |
309 | u32 cnid, id; | |
310 | int res; | |
311 | ||
312 | if (HFSPLUS_IS_RSRC(inode)) | |
313 | return -EPERM; | |
f6089ff8 CH |
314 | if (!S_ISREG(inode->i_mode)) |
315 | return -EPERM; | |
1da177e4 | 316 | |
7ac9fb9c | 317 | mutex_lock(&sbi->vh_mutex); |
1da177e4 LT |
318 | if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) { |
319 | for (;;) { | |
320 | get_random_bytes(&id, sizeof(cnid)); | |
321 | id &= 0x3fffffff; | |
322 | str.name = name; | |
323 | str.len = sprintf(name, "iNode%d", id); | |
324 | res = hfsplus_rename_cat(inode->i_ino, | |
325 | src_dir, &src_dentry->d_name, | |
dd73a01a | 326 | sbi->hidden_dir, &str); |
1da177e4 LT |
327 | if (!res) |
328 | break; | |
329 | if (res != -EEXIST) | |
7ac9fb9c | 330 | goto out; |
1da177e4 | 331 | } |
f6089ff8 | 332 | HFSPLUS_I(inode)->linkid = id; |
dd73a01a | 333 | cnid = sbi->next_cnid++; |
1da177e4 | 334 | src_dentry->d_fsdata = (void *)(unsigned long)cnid; |
2753cc28 AS |
335 | res = hfsplus_create_cat(cnid, src_dir, |
336 | &src_dentry->d_name, inode); | |
1da177e4 LT |
337 | if (res) |
338 | /* panic? */ | |
7ac9fb9c | 339 | goto out; |
dd73a01a | 340 | sbi->file_count++; |
1da177e4 | 341 | } |
dd73a01a | 342 | cnid = sbi->next_cnid++; |
1da177e4 LT |
343 | res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode); |
344 | if (res) | |
7ac9fb9c | 345 | goto out; |
1da177e4 | 346 | |
d8c76e6f | 347 | inc_nlink(inode); |
1da177e4 | 348 | hfsplus_instantiate(dst_dentry, inode, cnid); |
7de9c6ee | 349 | ihold(inode); |
02027d42 | 350 | inode->i_ctime = current_time(inode); |
1da177e4 | 351 | mark_inode_dirty(inode); |
dd73a01a | 352 | sbi->file_count++; |
9e6c5829 | 353 | hfsplus_mark_mdb_dirty(dst_dir->i_sb); |
7ac9fb9c CH |
354 | out: |
355 | mutex_unlock(&sbi->vh_mutex); | |
356 | return res; | |
1da177e4 LT |
357 | } |
358 | ||
359 | static int hfsplus_unlink(struct inode *dir, struct dentry *dentry) | |
360 | { | |
dd73a01a | 361 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
2b0143b5 | 362 | struct inode *inode = d_inode(dentry); |
1da177e4 LT |
363 | struct qstr str; |
364 | char name[32]; | |
365 | u32 cnid; | |
366 | int res; | |
367 | ||
368 | if (HFSPLUS_IS_RSRC(inode)) | |
369 | return -EPERM; | |
370 | ||
7ac9fb9c | 371 | mutex_lock(&sbi->vh_mutex); |
1da177e4 LT |
372 | cnid = (u32)(unsigned long)dentry->d_fsdata; |
373 | if (inode->i_ino == cnid && | |
6af502de | 374 | atomic_read(&HFSPLUS_I(inode)->opencnt)) { |
1da177e4 LT |
375 | str.name = name; |
376 | str.len = sprintf(name, "temp%lu", inode->i_ino); | |
377 | res = hfsplus_rename_cat(inode->i_ino, | |
378 | dir, &dentry->d_name, | |
dd73a01a | 379 | sbi->hidden_dir, &str); |
85b8fe8c | 380 | if (!res) { |
1da177e4 | 381 | inode->i_flags |= S_DEAD; |
85b8fe8c CH |
382 | drop_nlink(inode); |
383 | } | |
7ac9fb9c | 384 | goto out; |
1da177e4 LT |
385 | } |
386 | res = hfsplus_delete_cat(cnid, dir, &dentry->d_name); | |
387 | if (res) | |
7ac9fb9c | 388 | goto out; |
1da177e4 | 389 | |
af8c85bb | 390 | if (inode->i_nlink > 0) |
9a53c3a7 | 391 | drop_nlink(inode); |
76b0c26a RZ |
392 | if (inode->i_ino == cnid) |
393 | clear_nlink(inode); | |
394 | if (!inode->i_nlink) { | |
395 | if (inode->i_ino != cnid) { | |
dd73a01a | 396 | sbi->file_count--; |
6af502de | 397 | if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) { |
76b0c26a | 398 | res = hfsplus_delete_cat(inode->i_ino, |
dd73a01a | 399 | sbi->hidden_dir, |
76b0c26a RZ |
400 | NULL); |
401 | if (!res) | |
402 | hfsplus_delete_inode(inode); | |
403 | } else | |
404 | inode->i_flags |= S_DEAD; | |
1da177e4 | 405 | } else |
76b0c26a | 406 | hfsplus_delete_inode(inode); |
af8c85bb | 407 | } else |
dd73a01a | 408 | sbi->file_count--; |
02027d42 | 409 | inode->i_ctime = current_time(inode); |
1da177e4 | 410 | mark_inode_dirty(inode); |
7ac9fb9c CH |
411 | out: |
412 | mutex_unlock(&sbi->vh_mutex); | |
1da177e4 LT |
413 | return res; |
414 | } | |
415 | ||
1da177e4 LT |
416 | static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry) |
417 | { | |
7ac9fb9c | 418 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
2b0143b5 | 419 | struct inode *inode = d_inode(dentry); |
1da177e4 LT |
420 | int res; |
421 | ||
1da177e4 LT |
422 | if (inode->i_size != 2) |
423 | return -ENOTEMPTY; | |
7ac9fb9c CH |
424 | |
425 | mutex_lock(&sbi->vh_mutex); | |
1da177e4 LT |
426 | res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name); |
427 | if (res) | |
7ac9fb9c | 428 | goto out; |
ce71ec36 | 429 | clear_nlink(inode); |
02027d42 | 430 | inode->i_ctime = current_time(inode); |
1da177e4 LT |
431 | hfsplus_delete_inode(inode); |
432 | mark_inode_dirty(inode); | |
7ac9fb9c CH |
433 | out: |
434 | mutex_unlock(&sbi->vh_mutex); | |
435 | return res; | |
1da177e4 LT |
436 | } |
437 | ||
438 | static int hfsplus_symlink(struct inode *dir, struct dentry *dentry, | |
439 | const char *symname) | |
440 | { | |
7ac9fb9c | 441 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
1da177e4 | 442 | struct inode *inode; |
27a4e388 | 443 | int res = -ENOMEM; |
1da177e4 | 444 | |
7ac9fb9c | 445 | mutex_lock(&sbi->vh_mutex); |
f17c89bf | 446 | inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO); |
1da177e4 | 447 | if (!inode) |
7ac9fb9c | 448 | goto out; |
1da177e4 LT |
449 | |
450 | res = page_symlink(inode, symname, strlen(symname) + 1); | |
f17c89bf CH |
451 | if (res) |
452 | goto out_err; | |
1da177e4 | 453 | |
1da177e4 | 454 | res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode); |
f17c89bf CH |
455 | if (res) |
456 | goto out_err; | |
1da177e4 | 457 | |
324ef39a VD |
458 | res = hfsplus_init_inode_security(inode, dir, &dentry->d_name); |
459 | if (res == -EOPNOTSUPP) | |
460 | res = 0; /* Operation is not supported. */ | |
461 | else if (res) { | |
462 | /* Try to delete anyway without error analysis. */ | |
463 | hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name); | |
464 | goto out_err; | |
465 | } | |
466 | ||
f17c89bf CH |
467 | hfsplus_instantiate(dentry, inode, inode->i_ino); |
468 | mark_inode_dirty(inode); | |
7ac9fb9c | 469 | goto out; |
1da177e4 | 470 | |
f17c89bf | 471 | out_err: |
6d6b77f1 | 472 | clear_nlink(inode); |
f17c89bf CH |
473 | hfsplus_delete_inode(inode); |
474 | iput(inode); | |
7ac9fb9c CH |
475 | out: |
476 | mutex_unlock(&sbi->vh_mutex); | |
1da177e4 LT |
477 | return res; |
478 | } | |
479 | ||
480 | static int hfsplus_mknod(struct inode *dir, struct dentry *dentry, | |
1a67aafb | 481 | umode_t mode, dev_t rdev) |
1da177e4 | 482 | { |
7ac9fb9c | 483 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
1da177e4 | 484 | struct inode *inode; |
27a4e388 | 485 | int res = -ENOMEM; |
1da177e4 | 486 | |
7ac9fb9c | 487 | mutex_lock(&sbi->vh_mutex); |
30d3abbe | 488 | inode = hfsplus_new_inode(dir->i_sb, mode); |
1da177e4 | 489 | if (!inode) |
7ac9fb9c | 490 | goto out; |
1da177e4 | 491 | |
90e61690 CH |
492 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) |
493 | init_special_inode(inode, mode, rdev); | |
494 | ||
1da177e4 | 495 | res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode); |
324ef39a VD |
496 | if (res) |
497 | goto failed_mknod; | |
498 | ||
499 | res = hfsplus_init_inode_security(inode, dir, &dentry->d_name); | |
500 | if (res == -EOPNOTSUPP) | |
501 | res = 0; /* Operation is not supported. */ | |
502 | else if (res) { | |
503 | /* Try to delete anyway without error analysis. */ | |
504 | hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name); | |
505 | goto failed_mknod; | |
1da177e4 | 506 | } |
30d3abbe | 507 | |
1da177e4 LT |
508 | hfsplus_instantiate(dentry, inode, inode->i_ino); |
509 | mark_inode_dirty(inode); | |
324ef39a VD |
510 | goto out; |
511 | ||
512 | failed_mknod: | |
513 | clear_nlink(inode); | |
514 | hfsplus_delete_inode(inode); | |
515 | iput(inode); | |
7ac9fb9c CH |
516 | out: |
517 | mutex_unlock(&sbi->vh_mutex); | |
518 | return res; | |
1da177e4 LT |
519 | } |
520 | ||
4acdaf27 | 521 | static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
ebfc3b49 | 522 | bool excl) |
30d3abbe CH |
523 | { |
524 | return hfsplus_mknod(dir, dentry, mode, 0); | |
525 | } | |
526 | ||
18bb1db3 | 527 | static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
30d3abbe CH |
528 | { |
529 | return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0); | |
530 | } | |
531 | ||
1da177e4 | 532 | static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry, |
f03b8ad8 MS |
533 | struct inode *new_dir, struct dentry *new_dentry, |
534 | unsigned int flags) | |
1da177e4 LT |
535 | { |
536 | int res; | |
537 | ||
f03b8ad8 MS |
538 | if (flags & ~RENAME_NOREPLACE) |
539 | return -EINVAL; | |
540 | ||
1da177e4 | 541 | /* Unlink destination if it already exists */ |
2b0143b5 | 542 | if (d_really_is_positive(new_dentry)) { |
e36cb0b8 | 543 | if (d_is_dir(new_dentry)) |
40de9a7c | 544 | res = hfsplus_rmdir(new_dir, new_dentry); |
e3911785 | 545 | else |
40de9a7c | 546 | res = hfsplus_unlink(new_dir, new_dentry); |
1da177e4 LT |
547 | if (res) |
548 | return res; | |
549 | } | |
550 | ||
551 | res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata, | |
552 | old_dir, &old_dentry->d_name, | |
553 | new_dir, &new_dentry->d_name); | |
554 | if (!res) | |
555 | new_dentry->d_fsdata = old_dentry->d_fsdata; | |
556 | return res; | |
557 | } | |
558 | ||
92e1d5be | 559 | const struct inode_operations hfsplus_dir_inode_operations = { |
324ef39a VD |
560 | .lookup = hfsplus_lookup, |
561 | .create = hfsplus_create, | |
562 | .link = hfsplus_link, | |
563 | .unlink = hfsplus_unlink, | |
564 | .mkdir = hfsplus_mkdir, | |
565 | .rmdir = hfsplus_rmdir, | |
566 | .symlink = hfsplus_symlink, | |
567 | .mknod = hfsplus_mknod, | |
568 | .rename = hfsplus_rename, | |
324ef39a | 569 | .listxattr = hfsplus_listxattr, |
b4c1107c VD |
570 | #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL |
571 | .get_acl = hfsplus_get_posix_acl, | |
b0a7ab57 | 572 | .set_acl = hfsplus_set_posix_acl, |
b4c1107c | 573 | #endif |
1da177e4 LT |
574 | }; |
575 | ||
4b6f5d20 | 576 | const struct file_operations hfsplus_dir_operations = { |
eb29d66d | 577 | .fsync = hfsplus_file_fsync, |
1da177e4 | 578 | .read = generic_read_dir, |
323ee8fc | 579 | .iterate_shared = hfsplus_readdir, |
7cc4bcc6 | 580 | .unlocked_ioctl = hfsplus_ioctl, |
1da177e4 LT |
581 | .llseek = generic_file_llseek, |
582 | .release = hfsplus_dir_release, | |
583 | }; |