1 /*
2 * linux/fs/hfs/dir.c
3 *
4 * Copyright (C) 1995-1997 Paul H. Hargrove
5 * (C) 2003 Ardis Technologies <roman@ardistech.com>
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 */
hfs_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)20 static struct dentry *hfs_lookup(struct inode *dir, struct dentry *dentry,
21 unsigned int flags)
22 {
23 hfs_cat_rec rec;
24 struct hfs_find_data fd;
25 struct inode *inode = NULL;
26 int res;
27
28 res = hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd);
29 if (res)
30 return ERR_PTR(res);
31 hfs_cat_build_key(dir->i_sb, fd.search_key, dir->i_ino, &dentry->d_name);
32 res = hfs_brec_read(&fd, &rec, sizeof(rec));
33 if (res) {
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);
40 }
41 hfs_find_exit(&fd);
42 return d_splice_alias(inode, dentry);
43 }
44
45 /*
46 * hfs_readdir
47 */
hfs_readdir(struct file * file,struct dir_context * ctx)48 static int hfs_readdir(struct file *file, struct dir_context *ctx)
49 {
50 struct inode *inode = file_inode(file);
51 struct super_block *sb = inode->i_sb;
52 int len, err;
53 char strbuf[HFS_MAX_NAMELEN];
54 union hfs_cat_rec entry;
55 struct hfs_find_data fd;
56 struct hfs_readdir_data *rd;
57 u16 type;
58
59 if (ctx->pos >= inode->i_size)
60 return 0;
61
62 err = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
63 if (err)
64 return err;
65 hfs_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
66 err = hfs_brec_find(&fd);
67 if (err)
68 goto out;
69
70 if (ctx->pos == 0) {
71 /* This is completely artificial... */
72 if (!dir_emit_dot(file, ctx))
73 goto out;
74 ctx->pos = 1;
75 }
76 if (ctx->pos == 1) {
77 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
78 err = -EIO;
79 goto out;
80 }
81
82 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
83 if (entry.type != HFS_CDR_THD) {
84 pr_err("bad catalog folder thread\n");
85 err = -EIO;
86 goto out;
87 }
88 //if (fd.entrylength < HFS_MIN_THREAD_SZ) {
89 // pr_err("truncated catalog thread\n");
90 // err = -EIO;
91 // goto out;
92 //}
93 if (!dir_emit(ctx, "..", 2,
94 be32_to_cpu(entry.thread.ParID), DT_DIR))
95 goto out;
96 ctx->pos = 2;
97 }
98 if (ctx->pos >= inode->i_size)
99 goto out;
100 rd = file->private_data;
101 if (rd && rd->pos == ctx->pos) {
102 memcpy(fd.search_key, &rd->key, sizeof(struct hfs_cat_key));
103 err = hfs_brec_find(&fd);
104 if (err == -ENOENT)
105 err = hfs_brec_goto(&fd, 1);
106 } else {
107 err = hfs_brec_goto(&fd, ctx->pos - 1);
108 }
109 if (err)
110 goto out;
111
112 for (;;) {
113 if (be32_to_cpu(fd.key->cat.ParID) != inode->i_ino) {
114 pr_err("walked past end of dir\n");
115 err = -EIO;
116 goto out;
117 }
118
119 if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
120 err = -EIO;
121 goto out;
122 }
123
124 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
125 type = entry.type;
126 len = hfs_mac2asc(sb, strbuf, &fd.key->cat.CName);
127 if (type == HFS_CDR_DIR) {
128 if (fd.entrylength < sizeof(struct hfs_cat_dir)) {
129 pr_err("small dir entry\n");
130 err = -EIO;
131 goto out;
132 }
133 if (!dir_emit(ctx, strbuf, len,
134 be32_to_cpu(entry.dir.DirID), DT_DIR))
135 break;
136 } else if (type == HFS_CDR_FIL) {
137 if (fd.entrylength < sizeof(struct hfs_cat_file)) {
138 pr_err("small file entry\n");
139 err = -EIO;
140 goto out;
141 }
142 if (!dir_emit(ctx, strbuf, len,
143 be32_to_cpu(entry.file.FlNum), DT_REG))
144 break;
145 } else {
146 pr_err("bad catalog entry type %d\n", type);
147 err = -EIO;
148 goto out;
149 }
150 ctx->pos++;
151 if (ctx->pos >= inode->i_size)
152 goto out;
153 err = hfs_brec_goto(&fd, 1);
154 if (err)
155 goto out;
156 }
157 if (!rd) {
158 rd = kmalloc_obj(struct hfs_readdir_data);
159 if (!rd) {
160 err = -ENOMEM;
161 goto out;
162 }
163 file->private_data = rd;
164 }
165 rd->pos = ctx->pos;
166 memcpy(&rd->key, &fd.key->cat, sizeof(struct hfs_cat_key));
167 out:
168 hfs_find_exit(&fd);
169 return err;
170 }
171
hfs_dir_release(struct inode * inode,struct file * file)172 static int hfs_dir_release(struct inode *inode, struct file *file)
173 {
174 kfree(file->private_data);
175 return 0;
176 }
177
178 /*
179 * hfs_create()
180 *
181 * This is the create() entry in the inode_operations structure for
182 * regular HFS directories. The purpose is to create a new file in
183 * a directory and return a corresponding inode, given the inode for
184 * the directory and the name (and its length) of the new file.
185 */
hfs_create(const struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)186 static int hfs_create(const struct mnt_idmap *idmap, struct inode *dir,
187 struct dentry *dentry, umode_t mode)
188 {
189 struct inode *inode;
190 int res;
191
192 inode = hfs_new_inode(dir, &dentry->d_name, mode);
193 if (IS_ERR(inode))
194 return PTR_ERR(inode);
195
196 res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
197 if (res) {
198 clear_nlink(inode);
199 hfs_delete_inode(inode);
200 iput(inode);
201 return res;
202 }
203 d_instantiate(dentry, inode);
204 mark_inode_dirty(inode);
205 return 0;
206 }
207
208 /*
209 * hfs_mkdir()
210 *
211 * This is the mkdir() entry in the inode_operations structure for
212 * regular HFS directories. The purpose is to create a new directory
213 * in a directory, given the inode for the parent directory and the
214 * name (and its length) of the new directory.
215 */
hfs_mkdir(const struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)216 static struct dentry *hfs_mkdir(const struct mnt_idmap *idmap, struct inode *dir,
217 struct dentry *dentry, umode_t mode)
218 {
219 struct inode *inode;
220 int res;
221
222 inode = hfs_new_inode(dir, &dentry->d_name, mode);
223 if (IS_ERR(inode))
224 return ERR_CAST(inode);
225
226 res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
227 if (res) {
228 clear_nlink(inode);
229 hfs_delete_inode(inode);
230 iput(inode);
231 return ERR_PTR(res);
232 }
233 d_instantiate(dentry, inode);
234 mark_inode_dirty(inode);
235 return NULL;
236 }
237
238 /*
239 * hfs_remove()
240 *
241 * This serves as both unlink() and rmdir() in the inode_operations
242 * structure for regular HFS directories. The purpose is to delete
243 * an existing child, given the inode for the parent directory and
244 * the name (and its length) of the existing directory.
245 *
246 * HFS does not have hardlinks, so both rmdir and unlink set the
247 * link count to 0. The only difference is the emptiness check.
248 */
hfs_remove(struct inode * dir,struct dentry * dentry)249 static int hfs_remove(struct inode *dir, struct dentry *dentry)
250 {
251 struct super_block *sb = dir->i_sb;
252 struct inode *inode = d_inode(dentry);
253 int res;
254
255 if (S_ISDIR(inode->i_mode) && inode->i_size != 2)
256 return -ENOTEMPTY;
257
258 if (unlikely(!is_hfs_cnid_counts_valid(sb))) {
259 pr_err("cannot remove file/folder\n");
260 return -ERANGE;
261 }
262
263 res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name);
264 if (res)
265 return res;
266 clear_nlink(inode);
267 inode_set_ctime_current(inode);
268 hfs_delete_inode(inode);
269 mark_inode_dirty(inode);
270 return 0;
271 }
272
273 /*
274 * hfs_rename()
275 *
276 * This is the rename() entry in the inode_operations structure for
277 * regular HFS directories. The purpose is to rename an existing
278 * file or directory, given the inode for the current directory and
279 * the name (and its length) of the existing file/directory and the
280 * inode for the new directory and the name (and its length) of the
281 * new file/directory.
282 * XXX: how do you handle must_be dir?
283 */
hfs_rename(const struct mnt_idmap * idmap,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,unsigned int flags)284 static int hfs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
285 struct dentry *old_dentry, struct inode *new_dir,
286 struct dentry *new_dentry, unsigned int flags)
287 {
288 int res;
289
290 if (flags & ~RENAME_NOREPLACE)
291 return -EINVAL;
292
293 /* Unlink destination if it already exists */
294 if (d_really_is_positive(new_dentry)) {
295 res = hfs_remove(new_dir, new_dentry);
296 if (res)
297 return res;
298 }
299
300 res = hfs_cat_move(d_inode(old_dentry)->i_ino,
301 old_dir, &old_dentry->d_name,
302 new_dir, &new_dentry->d_name);
303 if (!res) {
304 struct inode *inode = d_inode(old_dentry);
305
306 hfs_cat_build_key(old_dir->i_sb,
307 (btree_key *)&HFS_I(inode)->cat_key,
308 new_dir->i_ino, &new_dentry->d_name);
309 inode_set_ctime_current(inode);
310 mark_inode_dirty(inode);
311 }
312 return res;
313 }
314
315 const struct file_operations hfs_dir_operations = {
316 .read = generic_read_dir,
317 .iterate_shared = hfs_readdir,
318 .llseek = generic_file_llseek,
319 .release = hfs_dir_release,
320 };
321
322 const struct inode_operations hfs_dir_inode_operations = {
323 .create = hfs_create,
324 .lookup = hfs_lookup,
325 .unlink = hfs_remove,
326 .mkdir = hfs_mkdir,
327 .rmdir = hfs_remove,
328 .rename = hfs_rename,
329 .setattr = hfs_inode_setattr,
330 .fileattr_get = hfs_fileattr_get,
331 };
332