]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/hfs/dir.c | |
3 | * | |
4 | * Copyright (C) 1995-1997 Paul H. Hargrove | |
5 | * (C) 2003 Ardis Technologies <[email protected]> | |
6 | * This file may be distributed under the terms of the GNU General Public License. | |
7 | * | |
8 | * This file contains directory-related functions independent of which | |
9 | * scheme is being used to represent forks. | |
10 | * | |
11 | * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds | |
12 | */ | |
13 | ||
14 | #include "hfs_fs.h" | |
15 | #include "btree.h" | |
16 | ||
17 | /* | |
18 | * hfs_lookup() | |
19 | */ | |
20 | static struct dentry *hfs_lookup(struct inode *dir, struct dentry *dentry, | |
00cd8dd3 | 21 | unsigned int flags) |
1da177e4 LT |
22 | { |
23 | hfs_cat_rec rec; | |
24 | struct hfs_find_data fd; | |
25 | struct inode *inode = NULL; | |
26 | int res; | |
27 | ||
9509f178 AK |
28 | res = hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd); |
29 | if (res) | |
30 | return ERR_PTR(res); | |
328b9227 | 31 | hfs_cat_build_key(dir->i_sb, fd.search_key, dir->i_ino, &dentry->d_name); |
1da177e4 LT |
32 | res = hfs_brec_read(&fd, &rec, sizeof(rec)); |
33 | if (res) { | |
6b9cceea AV |
34 | if (res != -ENOENT) |
35 | inode = ERR_PTR(res); | |
36 | } else { | |
37 | inode = hfs_iget(dir->i_sb, &fd.search_key->cat, &rec); | |
38 | if (!inode) | |
39 | inode = ERR_PTR(-EACCES); | |
1da177e4 | 40 | } |
1da177e4 | 41 | hfs_find_exit(&fd); |
6b9cceea | 42 | return d_splice_alias(inode, dentry); |
1da177e4 LT |
43 | } |
44 | ||
45 | /* | |
46 | * hfs_readdir | |
47 | */ | |
002f8bec | 48 | static int hfs_readdir(struct file *file, struct dir_context *ctx) |
1da177e4 | 49 | { |
002f8bec | 50 | struct inode *inode = file_inode(file); |
1da177e4 LT |
51 | struct super_block *sb = inode->i_sb; |
52 | int len, err; | |
328b9227 | 53 | char strbuf[HFS_MAX_NAMELEN]; |
1da177e4 LT |
54 | union hfs_cat_rec entry; |
55 | struct hfs_find_data fd; | |
56 | struct hfs_readdir_data *rd; | |
57 | u16 type; | |
58 | ||
002f8bec | 59 | if (ctx->pos >= inode->i_size) |
1da177e4 LT |
60 | return 0; |
61 | ||
9509f178 AK |
62 | err = hfs_find_init(HFS_SB(sb)->cat_tree, &fd); |
63 | if (err) | |
64 | return err; | |
328b9227 | 65 | hfs_cat_build_key(sb, fd.search_key, inode->i_ino, NULL); |
1da177e4 LT |
66 | err = hfs_brec_find(&fd); |
67 | if (err) | |
68 | goto out; | |
69 | ||
002f8bec | 70 | if (ctx->pos == 0) { |
1da177e4 | 71 | /* This is completely artificial... */ |
002f8bec | 72 | if (!dir_emit_dot(file, ctx)) |
1da177e4 | 73 | goto out; |
002f8bec AV |
74 | ctx->pos = 1; |
75 | } | |
76 | if (ctx->pos == 1) { | |
ec81aecb AW |
77 | if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) { |
78 | err = -EIO; | |
79 | goto out; | |
80 | } | |
81 | ||
1da177e4 LT |
82 | hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength); |
83 | if (entry.type != HFS_CDR_THD) { | |
d6142673 | 84 | pr_err("bad catalog folder thread\n"); |
1da177e4 LT |
85 | err = -EIO; |
86 | goto out; | |
87 | } | |
88 | //if (fd.entrylength < HFS_MIN_THREAD_SZ) { | |
d6142673 | 89 | // pr_err("truncated catalog thread\n"); |
1da177e4 LT |
90 | // err = -EIO; |
91 | // goto out; | |
92 | //} | |
002f8bec | 93 | if (!dir_emit(ctx, "..", 2, |
1da177e4 LT |
94 | be32_to_cpu(entry.thread.ParID), DT_DIR)) |
95 | goto out; | |
002f8bec | 96 | ctx->pos = 2; |
1da177e4 | 97 | } |
002f8bec AV |
98 | if (ctx->pos >= inode->i_size) |
99 | goto out; | |
100 | err = hfs_brec_goto(&fd, ctx->pos - 1); | |
101 | if (err) | |
102 | goto out; | |
1da177e4 LT |
103 | |
104 | for (;;) { | |
105 | if (be32_to_cpu(fd.key->cat.ParID) != inode->i_ino) { | |
d6142673 | 106 | pr_err("walked past end of dir\n"); |
1da177e4 LT |
107 | err = -EIO; |
108 | goto out; | |
109 | } | |
ec81aecb AW |
110 | |
111 | if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) { | |
112 | err = -EIO; | |
113 | goto out; | |
114 | } | |
115 | ||
1da177e4 LT |
116 | hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength); |
117 | type = entry.type; | |
328b9227 | 118 | len = hfs_mac2asc(sb, strbuf, &fd.key->cat.CName); |
1da177e4 LT |
119 | if (type == HFS_CDR_DIR) { |
120 | if (fd.entrylength < sizeof(struct hfs_cat_dir)) { | |
d6142673 | 121 | pr_err("small dir entry\n"); |
1da177e4 LT |
122 | err = -EIO; |
123 | goto out; | |
124 | } | |
002f8bec | 125 | if (!dir_emit(ctx, strbuf, len, |
1da177e4 LT |
126 | be32_to_cpu(entry.dir.DirID), DT_DIR)) |
127 | break; | |
128 | } else if (type == HFS_CDR_FIL) { | |
129 | if (fd.entrylength < sizeof(struct hfs_cat_file)) { | |
d6142673 | 130 | pr_err("small file entry\n"); |
1da177e4 LT |
131 | err = -EIO; |
132 | goto out; | |
133 | } | |
002f8bec | 134 | if (!dir_emit(ctx, strbuf, len, |
1da177e4 LT |
135 | be32_to_cpu(entry.file.FlNum), DT_REG)) |
136 | break; | |
137 | } else { | |
d6142673 | 138 | pr_err("bad catalog entry type %d\n", type); |
1da177e4 LT |
139 | err = -EIO; |
140 | goto out; | |
141 | } | |
002f8bec AV |
142 | ctx->pos++; |
143 | if (ctx->pos >= inode->i_size) | |
1da177e4 LT |
144 | goto out; |
145 | err = hfs_brec_goto(&fd, 1); | |
146 | if (err) | |
147 | goto out; | |
148 | } | |
002f8bec | 149 | rd = file->private_data; |
1da177e4 LT |
150 | if (!rd) { |
151 | rd = kmalloc(sizeof(struct hfs_readdir_data), GFP_KERNEL); | |
152 | if (!rd) { | |
153 | err = -ENOMEM; | |
154 | goto out; | |
155 | } | |
002f8bec AV |
156 | file->private_data = rd; |
157 | rd->file = file; | |
9717a91b | 158 | spin_lock(&HFS_I(inode)->open_dir_lock); |
1da177e4 | 159 | list_add(&rd->list, &HFS_I(inode)->open_dir_list); |
9717a91b | 160 | spin_unlock(&HFS_I(inode)->open_dir_lock); |
1da177e4 | 161 | } |
9717a91b AV |
162 | /* |
163 | * Can be done after the list insertion; exclusion with | |
164 | * hfs_delete_cat() is provided by directory lock. | |
165 | */ | |
eec11535 | 166 | memcpy(&rd->key, &fd.key->cat, sizeof(struct hfs_cat_key)); |
1da177e4 LT |
167 | out: |
168 | hfs_find_exit(&fd); | |
169 | return err; | |
170 | } | |
171 | ||
172 | static int hfs_dir_release(struct inode *inode, struct file *file) | |
173 | { | |
174 | struct hfs_readdir_data *rd = file->private_data; | |
175 | if (rd) { | |
9717a91b | 176 | spin_lock(&HFS_I(inode)->open_dir_lock); |
1da177e4 | 177 | list_del(&rd->list); |
9717a91b | 178 | spin_unlock(&HFS_I(inode)->open_dir_lock); |
1da177e4 LT |
179 | kfree(rd); |
180 | } | |
181 | return 0; | |
182 | } | |
183 | ||
184 | /* | |
185 | * hfs_create() | |
186 | * | |
187 | * This is the create() entry in the inode_operations structure for | |
188 | * regular HFS directories. The purpose is to create a new file in | |
189 | * a directory and return a corresponding inode, given the inode for | |
190 | * the directory and the name (and its length) of the new file. | |
191 | */ | |
4acdaf27 | 192 | static int hfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
ebfc3b49 | 193 | bool excl) |
1da177e4 LT |
194 | { |
195 | struct inode *inode; | |
196 | int res; | |
197 | ||
198 | inode = hfs_new_inode(dir, &dentry->d_name, mode); | |
199 | if (!inode) | |
13f24485 | 200 | return -ENOMEM; |
1da177e4 LT |
201 | |
202 | res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode); | |
203 | if (res) { | |
6d6b77f1 | 204 | clear_nlink(inode); |
1da177e4 LT |
205 | hfs_delete_inode(inode); |
206 | iput(inode); | |
207 | return res; | |
208 | } | |
209 | d_instantiate(dentry, inode); | |
210 | mark_inode_dirty(inode); | |
211 | return 0; | |
212 | } | |
213 | ||
214 | /* | |
215 | * hfs_mkdir() | |
216 | * | |
217 | * This is the mkdir() entry in the inode_operations structure for | |
218 | * regular HFS directories. The purpose is to create a new directory | |
219 | * in a directory, given the inode for the parent directory and the | |
220 | * name (and its length) of the new directory. | |
221 | */ | |
18bb1db3 | 222 | static int hfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
1da177e4 LT |
223 | { |
224 | struct inode *inode; | |
225 | int res; | |
226 | ||
227 | inode = hfs_new_inode(dir, &dentry->d_name, S_IFDIR | mode); | |
228 | if (!inode) | |
13f24485 | 229 | return -ENOMEM; |
1da177e4 LT |
230 | |
231 | res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode); | |
232 | if (res) { | |
6d6b77f1 | 233 | clear_nlink(inode); |
1da177e4 LT |
234 | hfs_delete_inode(inode); |
235 | iput(inode); | |
236 | return res; | |
237 | } | |
238 | d_instantiate(dentry, inode); | |
239 | mark_inode_dirty(inode); | |
240 | return 0; | |
241 | } | |
242 | ||
243 | /* | |
69102e9b | 244 | * hfs_remove() |
1da177e4 | 245 | * |
69102e9b AV |
246 | * This serves as both unlink() and rmdir() in the inode_operations |
247 | * structure for regular HFS directories. The purpose is to delete | |
248 | * an existing child, given the inode for the parent directory and | |
249 | * the name (and its length) of the existing directory. | |
1da177e4 | 250 | * |
69102e9b AV |
251 | * HFS does not have hardlinks, so both rmdir and unlink set the |
252 | * link count to 0. The only difference is the emptiness check. | |
1da177e4 | 253 | */ |
69102e9b | 254 | static int hfs_remove(struct inode *dir, struct dentry *dentry) |
1da177e4 | 255 | { |
2b0143b5 | 256 | struct inode *inode = d_inode(dentry); |
1da177e4 LT |
257 | int res; |
258 | ||
69102e9b | 259 | if (S_ISDIR(inode->i_mode) && inode->i_size != 2) |
1da177e4 LT |
260 | return -ENOTEMPTY; |
261 | res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name); | |
262 | if (res) | |
263 | return res; | |
ce71ec36 | 264 | clear_nlink(inode); |
02027d42 | 265 | inode->i_ctime = current_time(inode); |
1da177e4 LT |
266 | hfs_delete_inode(inode); |
267 | mark_inode_dirty(inode); | |
268 | return 0; | |
269 | } | |
270 | ||
271 | /* | |
272 | * hfs_rename() | |
273 | * | |
274 | * This is the rename() entry in the inode_operations structure for | |
275 | * regular HFS directories. The purpose is to rename an existing | |
276 | * file or directory, given the inode for the current directory and | |
277 | * the name (and its length) of the existing file/directory and the | |
278 | * inode for the new directory and the name (and its length) of the | |
279 | * new file/directory. | |
280 | * XXX: how do you handle must_be dir? | |
281 | */ | |
282 | static int hfs_rename(struct inode *old_dir, struct dentry *old_dentry, | |
f03b8ad8 MS |
283 | struct inode *new_dir, struct dentry *new_dentry, |
284 | unsigned int flags) | |
1da177e4 LT |
285 | { |
286 | int res; | |
287 | ||
f03b8ad8 MS |
288 | if (flags & ~RENAME_NOREPLACE) |
289 | return -EINVAL; | |
290 | ||
1da177e4 | 291 | /* Unlink destination if it already exists */ |
2b0143b5 | 292 | if (d_really_is_positive(new_dentry)) { |
69102e9b | 293 | res = hfs_remove(new_dir, new_dentry); |
1da177e4 LT |
294 | if (res) |
295 | return res; | |
296 | } | |
297 | ||
2b0143b5 | 298 | res = hfs_cat_move(d_inode(old_dentry)->i_ino, |
1da177e4 LT |
299 | old_dir, &old_dentry->d_name, |
300 | new_dir, &new_dentry->d_name); | |
301 | if (!res) | |
328b9227 | 302 | hfs_cat_build_key(old_dir->i_sb, |
2b0143b5 | 303 | (btree_key *)&HFS_I(d_inode(old_dentry))->cat_key, |
1da177e4 LT |
304 | new_dir->i_ino, &new_dentry->d_name); |
305 | return res; | |
306 | } | |
307 | ||
4b6f5d20 | 308 | const struct file_operations hfs_dir_operations = { |
1da177e4 | 309 | .read = generic_read_dir, |
9717a91b | 310 | .iterate_shared = hfs_readdir, |
1da177e4 LT |
311 | .llseek = generic_file_llseek, |
312 | .release = hfs_dir_release, | |
313 | }; | |
314 | ||
92e1d5be | 315 | const struct inode_operations hfs_dir_inode_operations = { |
1da177e4 LT |
316 | .create = hfs_create, |
317 | .lookup = hfs_lookup, | |
69102e9b | 318 | .unlink = hfs_remove, |
1da177e4 | 319 | .mkdir = hfs_mkdir, |
69102e9b | 320 | .rmdir = hfs_remove, |
1da177e4 LT |
321 | .rename = hfs_rename, |
322 | .setattr = hfs_inode_setattr, | |
323 | }; |