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