]>
Commit | Line | Data |
---|---|---|
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" | |
18 | ||
19 | static 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 */ | |
27 | static 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; | |
d45bce8f DG |
39 | |
40 | dentry->d_op = &hfsplus_dentry_operations; | |
1da177e4 | 41 | dentry->d_fsdata = NULL; |
dd73a01a | 42 | hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd); |
1da177e4 LT |
43 | hfsplus_cat_build_key(sb, fd.search_key, dir->i_ino, &dentry->d_name); |
44 | again: | |
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) && | |
af8c85bb | 70 | entry.file.user_info.fdCreator == cpu_to_be32(HFSP_HFSPLUS_CREATOR) && |
6af502de CH |
71 | (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) && | |
dd73a01a | 73 | HFSPLUS_SB(sb)->hidden_dir) { |
1da177e4 LT |
74 | struct qstr str; |
75 | char name[32]; | |
76 | ||
77 | if (dentry->d_fsdata) { | |
af8c85bb RZ |
78 | /* |
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; | |
dd73a01a CH |
89 | hfsplus_cat_build_key(sb, fd.search_key, |
90 | HFSPLUS_SB(sb)->hidden_dir->i_ino, &str); | |
af8c85bb | 91 | goto again; |
1da177e4 | 92 | } |
1da177e4 LT |
93 | } else if (!dentry->d_fsdata) |
94 | dentry->d_fsdata = (void *)(unsigned long)cnid; | |
95 | } else { | |
634725a9 | 96 | printk(KERN_ERR "hfs: invalid catalog entry type in lookup\n"); |
1da177e4 LT |
97 | err = -EIO; |
98 | goto fail; | |
99 | } | |
100 | hfs_find_exit(&fd); | |
63525391 DH |
101 | inode = hfsplus_iget(dir->i_sb, cnid); |
102 | if (IS_ERR(inode)) | |
103 | return ERR_CAST(inode); | |
1da177e4 | 104 | if (S_ISREG(inode->i_mode)) |
6af502de | 105 | HFSPLUS_I(inode)->dev = linkid; |
1da177e4 LT |
106 | out: |
107 | d_add(dentry, inode); | |
108 | return NULL; | |
109 | fail: | |
110 | hfs_find_exit(&fd); | |
111 | return ERR_PTR(err); | |
112 | } | |
113 | ||
114 | static int hfsplus_readdir(struct file *filp, void *dirent, filldir_t filldir) | |
115 | { | |
f44ea031 | 116 | struct inode *inode = filp->f_path.dentry->d_inode; |
1da177e4 LT |
117 | 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 | ||
dd73a01a | 128 | hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd); |
1da177e4 LT |
129 | 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) { | |
634725a9 | 144 | printk(KERN_ERR "hfs: bad catalog folder thread\n"); |
1da177e4 LT |
145 | err = -EIO; |
146 | goto out; | |
147 | } | |
148 | if (fd.entrylength < HFSPLUS_MIN_THREAD_SZ) { | |
634725a9 | 149 | printk(KERN_ERR "hfs: truncated catalog thread\n"); |
1da177e4 LT |
150 | 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) { | |
634725a9 | 168 | printk(KERN_ERR "hfs: walked past end of dir\n"); |
1da177e4 LT |
169 | 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)) { | |
634725a9 | 180 | printk(KERN_ERR "hfs: small dir entry\n"); |
1da177e4 LT |
181 | err = -EIO; |
182 | goto out; | |
183 | } | |
dd73a01a CH |
184 | if (HFSPLUS_SB(sb)->hidden_dir && |
185 | HFSPLUS_SB(sb)->hidden_dir->i_ino == | |
186 | be32_to_cpu(entry.folder.id)) | |
1da177e4 LT |
187 | 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)) { | |
634725a9 | 193 | printk(KERN_ERR "hfs: small file entry\n"); |
1da177e4 LT |
194 | 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 { | |
634725a9 | 201 | printk(KERN_ERR "hfs: bad catalog entry type\n"); |
1da177e4 LT |
202 | 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; | |
6af502de | 222 | list_add(&rd->list, &HFSPLUS_I(inode)->open_dir_list); |
1da177e4 LT |
223 | } |
224 | memcpy(&rd->key, fd.key, sizeof(struct hfsplus_cat_key)); | |
225 | out: | |
226 | hfs_find_exit(&fd); | |
227 | return err; | |
228 | } | |
229 | ||
230 | static int hfsplus_dir_release(struct inode *inode, struct file *file) | |
231 | { | |
232 | struct hfsplus_readdir_data *rd = file->private_data; | |
233 | if (rd) { | |
89755dca | 234 | mutex_lock(&inode->i_mutex); |
1da177e4 | 235 | list_del(&rd->list); |
89755dca | 236 | mutex_unlock(&inode->i_mutex); |
1da177e4 LT |
237 | kfree(rd); |
238 | } | |
239 | return 0; | |
240 | } | |
241 | ||
1da177e4 LT |
242 | static int hfsplus_link(struct dentry *src_dentry, struct inode *dst_dir, |
243 | struct dentry *dst_dentry) | |
244 | { | |
dd73a01a | 245 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dst_dir->i_sb); |
1da177e4 LT |
246 | struct inode *inode = src_dentry->d_inode; |
247 | struct inode *src_dir = src_dentry->d_parent->d_inode; | |
248 | struct qstr str; | |
249 | char name[32]; | |
250 | u32 cnid, id; | |
251 | int res; | |
252 | ||
253 | if (HFSPLUS_IS_RSRC(inode)) | |
254 | return -EPERM; | |
255 | ||
7ac9fb9c | 256 | mutex_lock(&sbi->vh_mutex); |
1da177e4 LT |
257 | if (inode->i_ino == (u32)(unsigned long)src_dentry->d_fsdata) { |
258 | for (;;) { | |
259 | get_random_bytes(&id, sizeof(cnid)); | |
260 | id &= 0x3fffffff; | |
261 | str.name = name; | |
262 | str.len = sprintf(name, "iNode%d", id); | |
263 | res = hfsplus_rename_cat(inode->i_ino, | |
264 | src_dir, &src_dentry->d_name, | |
dd73a01a | 265 | sbi->hidden_dir, &str); |
1da177e4 LT |
266 | if (!res) |
267 | break; | |
268 | if (res != -EEXIST) | |
7ac9fb9c | 269 | goto out; |
1da177e4 | 270 | } |
6af502de | 271 | HFSPLUS_I(inode)->dev = id; |
dd73a01a | 272 | cnid = sbi->next_cnid++; |
1da177e4 LT |
273 | src_dentry->d_fsdata = (void *)(unsigned long)cnid; |
274 | res = hfsplus_create_cat(cnid, src_dir, &src_dentry->d_name, inode); | |
275 | if (res) | |
276 | /* panic? */ | |
7ac9fb9c | 277 | goto out; |
dd73a01a | 278 | sbi->file_count++; |
1da177e4 | 279 | } |
dd73a01a | 280 | cnid = sbi->next_cnid++; |
1da177e4 LT |
281 | res = hfsplus_create_cat(cnid, dst_dir, &dst_dentry->d_name, inode); |
282 | if (res) | |
7ac9fb9c | 283 | goto out; |
1da177e4 | 284 | |
d8c76e6f | 285 | inc_nlink(inode); |
1da177e4 LT |
286 | hfsplus_instantiate(dst_dentry, inode, cnid); |
287 | atomic_inc(&inode->i_count); | |
288 | inode->i_ctime = CURRENT_TIME_SEC; | |
289 | mark_inode_dirty(inode); | |
dd73a01a CH |
290 | sbi->file_count++; |
291 | dst_dir->i_sb->s_dirt = 1; | |
7ac9fb9c CH |
292 | out: |
293 | mutex_unlock(&sbi->vh_mutex); | |
294 | return res; | |
1da177e4 LT |
295 | } |
296 | ||
297 | static int hfsplus_unlink(struct inode *dir, struct dentry *dentry) | |
298 | { | |
dd73a01a | 299 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
1da177e4 LT |
300 | struct inode *inode = dentry->d_inode; |
301 | struct qstr str; | |
302 | char name[32]; | |
303 | u32 cnid; | |
304 | int res; | |
305 | ||
306 | if (HFSPLUS_IS_RSRC(inode)) | |
307 | return -EPERM; | |
308 | ||
7ac9fb9c | 309 | mutex_lock(&sbi->vh_mutex); |
1da177e4 LT |
310 | cnid = (u32)(unsigned long)dentry->d_fsdata; |
311 | if (inode->i_ino == cnid && | |
6af502de | 312 | atomic_read(&HFSPLUS_I(inode)->opencnt)) { |
1da177e4 LT |
313 | str.name = name; |
314 | str.len = sprintf(name, "temp%lu", inode->i_ino); | |
315 | res = hfsplus_rename_cat(inode->i_ino, | |
316 | dir, &dentry->d_name, | |
dd73a01a | 317 | sbi->hidden_dir, &str); |
1da177e4 LT |
318 | if (!res) |
319 | inode->i_flags |= S_DEAD; | |
7ac9fb9c | 320 | goto out; |
1da177e4 LT |
321 | } |
322 | res = hfsplus_delete_cat(cnid, dir, &dentry->d_name); | |
323 | if (res) | |
7ac9fb9c | 324 | goto out; |
1da177e4 | 325 | |
af8c85bb | 326 | if (inode->i_nlink > 0) |
9a53c3a7 | 327 | drop_nlink(inode); |
76b0c26a RZ |
328 | if (inode->i_ino == cnid) |
329 | clear_nlink(inode); | |
330 | if (!inode->i_nlink) { | |
331 | if (inode->i_ino != cnid) { | |
dd73a01a | 332 | sbi->file_count--; |
6af502de | 333 | if (!atomic_read(&HFSPLUS_I(inode)->opencnt)) { |
76b0c26a | 334 | res = hfsplus_delete_cat(inode->i_ino, |
dd73a01a | 335 | sbi->hidden_dir, |
76b0c26a RZ |
336 | NULL); |
337 | if (!res) | |
338 | hfsplus_delete_inode(inode); | |
339 | } else | |
340 | inode->i_flags |= S_DEAD; | |
1da177e4 | 341 | } else |
76b0c26a | 342 | hfsplus_delete_inode(inode); |
af8c85bb | 343 | } else |
dd73a01a | 344 | sbi->file_count--; |
1da177e4 LT |
345 | inode->i_ctime = CURRENT_TIME_SEC; |
346 | mark_inode_dirty(inode); | |
7ac9fb9c CH |
347 | out: |
348 | mutex_unlock(&sbi->vh_mutex); | |
1da177e4 LT |
349 | return res; |
350 | } | |
351 | ||
1da177e4 LT |
352 | static int hfsplus_rmdir(struct inode *dir, struct dentry *dentry) |
353 | { | |
7ac9fb9c CH |
354 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
355 | struct inode *inode = dentry->d_inode; | |
1da177e4 LT |
356 | int res; |
357 | ||
1da177e4 LT |
358 | if (inode->i_size != 2) |
359 | return -ENOTEMPTY; | |
7ac9fb9c CH |
360 | |
361 | mutex_lock(&sbi->vh_mutex); | |
1da177e4 LT |
362 | res = hfsplus_delete_cat(inode->i_ino, dir, &dentry->d_name); |
363 | if (res) | |
7ac9fb9c | 364 | goto out; |
ce71ec36 | 365 | clear_nlink(inode); |
1da177e4 LT |
366 | inode->i_ctime = CURRENT_TIME_SEC; |
367 | hfsplus_delete_inode(inode); | |
368 | mark_inode_dirty(inode); | |
7ac9fb9c CH |
369 | out: |
370 | mutex_unlock(&sbi->vh_mutex); | |
371 | return res; | |
1da177e4 LT |
372 | } |
373 | ||
374 | static int hfsplus_symlink(struct inode *dir, struct dentry *dentry, | |
375 | const char *symname) | |
376 | { | |
7ac9fb9c | 377 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
1da177e4 | 378 | struct inode *inode; |
7ac9fb9c | 379 | int res = -ENOSPC; |
1da177e4 | 380 | |
7ac9fb9c | 381 | mutex_lock(&sbi->vh_mutex); |
f17c89bf | 382 | inode = hfsplus_new_inode(dir->i_sb, S_IFLNK | S_IRWXUGO); |
1da177e4 | 383 | if (!inode) |
7ac9fb9c | 384 | goto out; |
1da177e4 LT |
385 | |
386 | res = page_symlink(inode, symname, strlen(symname) + 1); | |
f17c89bf CH |
387 | if (res) |
388 | goto out_err; | |
1da177e4 | 389 | |
1da177e4 | 390 | res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode); |
f17c89bf CH |
391 | if (res) |
392 | goto out_err; | |
1da177e4 | 393 | |
f17c89bf CH |
394 | hfsplus_instantiate(dentry, inode, inode->i_ino); |
395 | mark_inode_dirty(inode); | |
7ac9fb9c | 396 | goto out; |
1da177e4 | 397 | |
f17c89bf CH |
398 | out_err: |
399 | inode->i_nlink = 0; | |
400 | hfsplus_delete_inode(inode); | |
401 | iput(inode); | |
7ac9fb9c CH |
402 | out: |
403 | mutex_unlock(&sbi->vh_mutex); | |
1da177e4 LT |
404 | return res; |
405 | } | |
406 | ||
407 | static int hfsplus_mknod(struct inode *dir, struct dentry *dentry, | |
408 | int mode, dev_t rdev) | |
409 | { | |
7ac9fb9c | 410 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(dir->i_sb); |
1da177e4 | 411 | struct inode *inode; |
7ac9fb9c | 412 | int res = -ENOSPC; |
1da177e4 | 413 | |
7ac9fb9c | 414 | mutex_lock(&sbi->vh_mutex); |
30d3abbe | 415 | inode = hfsplus_new_inode(dir->i_sb, mode); |
1da177e4 | 416 | if (!inode) |
7ac9fb9c | 417 | goto out; |
1da177e4 LT |
418 | |
419 | res = hfsplus_create_cat(inode->i_ino, dir, &dentry->d_name, inode); | |
420 | if (res) { | |
421 | inode->i_nlink = 0; | |
422 | hfsplus_delete_inode(inode); | |
423 | iput(inode); | |
7ac9fb9c | 424 | goto out; |
1da177e4 | 425 | } |
30d3abbe CH |
426 | |
427 | if (S_ISBLK(mode) || S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) | |
428 | init_special_inode(inode, mode, rdev); | |
429 | ||
1da177e4 LT |
430 | hfsplus_instantiate(dentry, inode, inode->i_ino); |
431 | mark_inode_dirty(inode); | |
7ac9fb9c CH |
432 | out: |
433 | mutex_unlock(&sbi->vh_mutex); | |
434 | return res; | |
1da177e4 LT |
435 | } |
436 | ||
30d3abbe CH |
437 | static int hfsplus_create(struct inode *dir, struct dentry *dentry, int mode, |
438 | struct nameidata *nd) | |
439 | { | |
440 | return hfsplus_mknod(dir, dentry, mode, 0); | |
441 | } | |
442 | ||
443 | static int hfsplus_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
444 | { | |
445 | return hfsplus_mknod(dir, dentry, mode | S_IFDIR, 0); | |
446 | } | |
447 | ||
1da177e4 LT |
448 | static int hfsplus_rename(struct inode *old_dir, struct dentry *old_dentry, |
449 | struct inode *new_dir, struct dentry *new_dentry) | |
450 | { | |
451 | int res; | |
452 | ||
453 | /* Unlink destination if it already exists */ | |
454 | if (new_dentry->d_inode) { | |
455 | res = hfsplus_unlink(new_dir, new_dentry); | |
456 | if (res) | |
457 | return res; | |
458 | } | |
459 | ||
460 | res = hfsplus_rename_cat((u32)(unsigned long)old_dentry->d_fsdata, | |
461 | old_dir, &old_dentry->d_name, | |
462 | new_dir, &new_dentry->d_name); | |
463 | if (!res) | |
464 | new_dentry->d_fsdata = old_dentry->d_fsdata; | |
465 | return res; | |
466 | } | |
467 | ||
92e1d5be | 468 | const struct inode_operations hfsplus_dir_inode_operations = { |
1da177e4 LT |
469 | .lookup = hfsplus_lookup, |
470 | .create = hfsplus_create, | |
471 | .link = hfsplus_link, | |
472 | .unlink = hfsplus_unlink, | |
473 | .mkdir = hfsplus_mkdir, | |
474 | .rmdir = hfsplus_rmdir, | |
475 | .symlink = hfsplus_symlink, | |
476 | .mknod = hfsplus_mknod, | |
477 | .rename = hfsplus_rename, | |
478 | }; | |
479 | ||
4b6f5d20 | 480 | const struct file_operations hfsplus_dir_operations = { |
1da177e4 LT |
481 | .read = generic_read_dir, |
482 | .readdir = hfsplus_readdir, | |
7cc4bcc6 | 483 | .unlocked_ioctl = hfsplus_ioctl, |
1da177e4 LT |
484 | .llseek = generic_file_llseek, |
485 | .release = hfsplus_dir_release, | |
486 | }; |