cifs: Allow passwords which begin with a delimitor
[deliverable/linux.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>
1da177e4
LT
15
16#include "hfsplus_fs.h"
17#include "hfsplus_raw.h"
324ef39a 18#include "xattr.h"
1da177e4
LT
19
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,
00cd8dd3 29 unsigned int flags)
1da177e4
LT
30{
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;
d45bce8f 40
1da177e4 41 dentry->d_fsdata = NULL;
5bd9d99d
AK
42 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
43 if (err)
44 return ERR_PTR(err);
1da177e4
LT
45 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);
2753cc28
AS
71 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) {
1da177e4
LT
82 struct qstr str;
83 char name[32];
84
85 if (dentry->d_fsdata) {
af8c85bb
RZ
86 /*
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;
2753cc28
AS
94 linkid =
95 be32_to_cpu(entry.file.permissions.dev);
af8c85bb
RZ
96 str.len = sprintf(name, "iNode%d", linkid);
97 str.name = name;
dd73a01a 98 hfsplus_cat_build_key(sb, fd.search_key,
2753cc28
AS
99 HFSPLUS_SB(sb)->hidden_dir->i_ino,
100 &str);
af8c85bb 101 goto again;
1da177e4 102 }
1da177e4
LT
103 } else if (!dentry->d_fsdata)
104 dentry->d_fsdata = (void *)(unsigned long)cnid;
105 } else {
634725a9 106 printk(KERN_ERR "hfs: invalid catalog entry type in lookup\n");
1da177e4
LT
107 err = -EIO;
108 goto fail;
109 }
110 hfs_find_exit(&fd);
63525391
DH
111 inode = hfsplus_iget(dir->i_sb, cnid);
112 if (IS_ERR(inode))
113 return ERR_CAST(inode);
1da177e4 114 if (S_ISREG(inode->i_mode))
f6089ff8 115 HFSPLUS_I(inode)->linkid = linkid;
1da177e4
LT
116out:
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{
496ad9aa 126 struct inode *inode = file_inode(filp);
1da177e4
LT
127 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
5bd9d99d
AK
138 err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
139 if (err)
140 return err;
1da177e4 141 hfsplus_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
324ef39a 142 err = hfs_brec_find(&fd, hfs_find_rec_by_key);
1da177e4
LT
143 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:
6f24f892
GKH
154 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
155 err = -EIO;
156 goto out;
157 }
158
2753cc28
AS
159 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
160 fd.entrylength);
1da177e4 161 if (be16_to_cpu(entry.type) != HFSPLUS_FOLDER_THREAD) {
634725a9 162 printk(KERN_ERR "hfs: bad catalog folder thread\n");
1da177e4
LT
163 err = -EIO;
164 goto out;
165 }
166 if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) {
634725a9 167 printk(KERN_ERR "hfs: truncated catalog thread\n");
1da177e4
LT
168 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) {
634725a9 186 printk(KERN_ERR "hfs: walked past end of dir\n");
1da177e4
LT
187 err = -EIO;
188 goto out;
189 }
6f24f892
GKH
190
191 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
192 err = -EIO;
193 goto out;
194 }
195
2753cc28
AS
196 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
197 fd.entrylength);
1da177e4
LT
198 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) {
2753cc28
AS
204 if (fd.entrylength <
205 sizeof(struct hfsplus_cat_folder)) {
634725a9 206 printk(KERN_ERR "hfs: small dir entry\n");
1da177e4
LT
207 err = -EIO;
208 goto out;
209 }
dd73a01a
CH
210 if (HFSPLUS_SB(sb)->hidden_dir &&
211 HFSPLUS_SB(sb)->hidden_dir->i_ino ==
212 be32_to_cpu(entry.folder.id))
1da177e4
LT
213 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)) {
634725a9 219 printk(KERN_ERR "hfs: small file entry\n");
1da177e4
LT
220 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 {
634725a9 227 printk(KERN_ERR "hfs: bad catalog entry type\n");
1da177e4
LT
228 err = -EIO;
229 goto out;
230 }
20b7643d 231next:
1da177e4
LT
232 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;
6af502de 248 list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list);
1da177e4
LT
249 }
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) {
89755dca 260 mutex_lock(&inode->i_mutex);
1da177e4 261 list_del(&rd->list);
89755dca 262 mutex_unlock(&inode->i_mutex);
1da177e4
LT
263 kfree(rd);
264 }
265 return 0;
266}
267
1da177e4
LT
268static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir,
269 struct dentry *dst_dentry)
270{
dd73a01a 271 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb);
1da177e4
LT
272 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;
f6089ff8
CH
281 if (!S_ISREG(inode->i_mode))
282 return -EPERM;
1da177e4 283
7ac9fb9c 284 mutex_lock(&sbi->vh_mutex);
1da177e4
LT
285 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,
dd73a01a 293 sbi->hidden_dir, &str);
1da177e4
LT
294 if (!res)
295 break;
296 if (res != -EEXIST)
7ac9fb9c 297 goto out;
1da177e4 298 }
f6089ff8 299 HFSPLUS_I(inode)->linkid = id;
dd73a01a 300 cnid = sbi->next_cnid++;
1da177e4 301 src_dentry->d_fsdata = (void *)(unsigned long)cnid;
2753cc28
AS
302 res = hfsplus_create_cat(cnid, src_dir,
303 &src_dentry->d_name, inode);
1da177e4
LT
304 if (res)
305 /* panic? */
7ac9fb9c 306 goto out;
dd73a01a 307 sbi->file_count++;
1da177e4 308 }
dd73a01a 309 cnid = sbi->next_cnid++;
1da177e4
LT
310 res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode);
311 if (res)
7ac9fb9c 312 goto out;
1da177e4 313
d8c76e6f 314 inc_nlink(inode);
1da177e4 315 hfsplus_instantiate(dst_dentry, inode, cnid);
7de9c6ee 316 ihold(inode);
1da177e4
LT
317 inode->i_ctime = CURRENT_TIME_SEC;
318 mark_inode_dirty(inode);
dd73a01a 319 sbi->file_count++;
9e6c5829 320 hfsplus_mark_mdb_dirty(dst_dir->i_sb);
7ac9fb9c
CH
321out:
322 mutex_unlock(&sbi->vh_mutex);
323 return res;
1da177e4
LT
324}
325
326static int hfsplus_unlink(struct inode *dir, struct dentry *dentry)
327{
dd73a01a 328 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
1da177e4
LT
329 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
7ac9fb9c 338 mutex_lock(&sbi->vh_mutex);
1da177e4
LT
339 cnid = (u32)(unsigned long)dentry->d_fsdata;
340 if (inode->i_ino == cnid &&
6af502de 341 atomic_read(&HFSPLUS_I(inode)->opencnt)) {
1da177e4
LT
342 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,
dd73a01a 346 sbi->hidden_dir, &str);
85b8fe8c 347 if (!res) {
1da177e4 348 inode->i_flags |= S_DEAD;
85b8fe8c
CH
349 drop_nlink(inode);
350 }
7ac9fb9c 351 goto out;
1da177e4
LT
352 }
353 res = hfsplus_delete_cat(cnid, dir, &dentry->d_name);
354 if (res)
7ac9fb9c 355 goto out;
1da177e4 356
af8c85bb 357 if (inode->i_nlink > 0)
9a53c3a7 358 drop_nlink(inode);
76b0c26a
RZ
359 if (inode->i_ino == cnid)
360 clear_nlink(inode);
361 if (!inode->i_nlink) {
362 if (inode->i_ino != cnid) {
dd73a01a 363 sbi->file_count--;
6af502de 364 if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) {
76b0c26a 365 res = hfsplus_delete_cat(inode->i_ino,
dd73a01a 366 sbi->hidden_dir,
76b0c26a
RZ
367 NULL);
368 if (!res)
369 hfsplus_delete_inode(inode);
370 } else
371 inode->i_flags |= S_DEAD;
1da177e4 372 } else
76b0c26a 373 hfsplus_delete_inode(inode);
af8c85bb 374 } else
dd73a01a 375 sbi->file_count--;
1da177e4
LT
376 inode->i_ctime = CURRENT_TIME_SEC;
377 mark_inode_dirty(inode);
7ac9fb9c
CH
378out:
379 mutex_unlock(&sbi->vh_mutex);
1da177e4
LT
380 return res;
381}
382
1da177e4
LT
383static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry)
384{
7ac9fb9c
CH
385 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
386 struct inode *inode = dentry->d_inode;
1da177e4
LT
387 int res;
388
1da177e4
LT
389 if (inode->i_size != 2)
390 return -ENOTEMPTY;
7ac9fb9c
CH
391
392 mutex_lock(&sbi->vh_mutex);
1da177e4
LT
393 res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name);
394 if (res)
7ac9fb9c 395 goto out;
ce71ec36 396 clear_nlink(inode);
1da177e4
LT
397 inode->i_ctime = CURRENT_TIME_SEC;
398 hfsplus_delete_inode(inode);
399 mark_inode_dirty(inode);
7ac9fb9c
CH
400out:
401 mutex_unlock(&sbi->vh_mutex);
402 return res;
1da177e4
LT
403}
404
405static int hfsplus_symlink(struct inode *dir, struct dentry *dentry,
406 const char *symname)
407{
7ac9fb9c 408 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
1da177e4 409 struct inode *inode;
7ac9fb9c 410 int res = -ENOSPC;
1da177e4 411
7ac9fb9c 412 mutex_lock(&sbi->vh_mutex);
f17c89bf 413 inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO);
1da177e4 414 if (!inode)
7ac9fb9c 415 goto out;
1da177e4
LT
416
417 res = page_symlink(inode, symname, strlen(symname) + 1);
f17c89bf
CH
418 if (res)
419 goto out_err;
1da177e4 420
1da177e4 421 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
f17c89bf
CH
422 if (res)
423 goto out_err;
1da177e4 424
324ef39a
VD
425 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
f17c89bf
CH
434 hfsplus_instantiate(dentry, inode, inode->i_ino);
435 mark_inode_dirty(inode);
7ac9fb9c 436 goto out;
1da177e4 437
f17c89bf 438out_err:
6d6b77f1 439 clear_nlink(inode);
f17c89bf
CH
440 hfsplus_delete_inode(inode);
441 iput(inode);
7ac9fb9c
CH
442out:
443 mutex_unlock(&sbi->vh_mutex);
1da177e4
LT
444 return res;
445}
446
447static int hfsplus_mknod(struct inode *dir, struct dentry *dentry,
1a67aafb 448 umode_t mode, dev_t rdev)
1da177e4 449{
7ac9fb9c 450 struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb);
1da177e4 451 struct inode *inode;
7ac9fb9c 452 int res = -ENOSPC;
1da177e4 453
7ac9fb9c 454 mutex_lock(&sbi->vh_mutex);
30d3abbe 455 inode = hfsplus_new_inode(dir->i_sb, mode);
1da177e4 456 if (!inode)
7ac9fb9c 457 goto out;
1da177e4 458
90e61690
CH
459 if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode))
460 init_special_inode(inode, mode, rdev);
461
1da177e4 462 res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode);
324ef39a
VD
463 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;
1da177e4 473 }
30d3abbe 474
1da177e4
LT
475 hfsplus_instantiate(dentry, inode, inode->i_ino);
476 mark_inode_dirty(inode);
324ef39a
VD
477 goto out;
478
479failed_mknod:
480 clear_nlink(inode);
481 hfsplus_delete_inode(inode);
482 iput(inode);
7ac9fb9c
CH
483out:
484 mutex_unlock(&sbi->vh_mutex);
485 return res;
1da177e4
LT
486}
487
4acdaf27 488static int hfsplus_create(struct inode *dir, struct dentry *dentry, umode_t mode,
ebfc3b49 489 bool excl)
30d3abbe
CH
490{
491 return hfsplus_mknod(dir, dentry, mode, 0);
492}
493
18bb1db3 494static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
30d3abbe
CH
495{
496 return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0);
497}
498
1da177e4
LT
499static 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) {
e3911785 506 if (S_ISDIR(new_dentry->d_inode->i_mode))
40de9a7c 507 res = hfsplus_rmdir(new_dir, new_dentry);
e3911785 508 else
40de9a7c 509 res = hfsplus_unlink(new_dir, new_dentry);
1da177e4
LT
510 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
92e1d5be 522const struct inode_operations hfsplus_dir_inode_operations = {
324ef39a
VD
523 .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,
1da177e4
LT
536};
537
4b6f5d20 538const struct file_operations hfsplus_dir_operations = {
eb29d66d 539 .fsync = hfsplus_file_fsync,
1da177e4
LT
540 .read = generic_read_dir,
541 .readdir = hfsplus_readdir,
7cc4bcc6 542 .unlocked_ioctl = hfsplus_ioctl,
1da177e4
LT
543 .llseek = generic_file_llseek,
544 .release = hfsplus_dir_release,
545};
This page took 0.667398 seconds and 5 git commands to generate.