]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/hfs/dir.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[mirror_ubuntu-bionic-kernel.git] / fs / hfs / dir.c
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 */
20 static struct dentry *hfs_lookup(struct inode *dir, struct dentry *dentry,
21 struct nameidata *nd)
22 {
23 hfs_cat_rec rec;
24 struct hfs_find_data fd;
25 struct inode *inode = NULL;
26 int res;
27
28 dentry->d_op = &hfs_dentry_operations;
29
30 hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd);
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 hfs_find_exit(&fd);
35 if (res == -ENOENT) {
36 /* No such entry */
37 inode = NULL;
38 goto done;
39 }
40 return ERR_PTR(res);
41 }
42 inode = hfs_iget(dir->i_sb, &fd.search_key->cat, &rec);
43 hfs_find_exit(&fd);
44 if (!inode)
45 return ERR_PTR(-EACCES);
46 done:
47 d_add(dentry, inode);
48 return NULL;
49 }
50
51 /*
52 * hfs_readdir
53 */
54 static int hfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
55 {
56 struct inode *inode = filp->f_path.dentry->d_inode;
57 struct super_block *sb = inode->i_sb;
58 int len, err;
59 char strbuf[HFS_MAX_NAMELEN];
60 union hfs_cat_rec entry;
61 struct hfs_find_data fd;
62 struct hfs_readdir_data *rd;
63 u16 type;
64
65 if (filp->f_pos >= inode->i_size)
66 return 0;
67
68 hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
69 hfs_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
70 err = hfs_brec_find(&fd);
71 if (err)
72 goto out;
73
74 switch ((u32)filp->f_pos) {
75 case 0:
76 /* This is completely artificial... */
77 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR))
78 goto out;
79 filp->f_pos++;
80 /* fall through */
81 case 1:
82 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
83 if (entry.type != HFS_CDR_THD) {
84 printk(KERN_ERR "hfs: bad catalog folder thread\n");
85 err = -EIO;
86 goto out;
87 }
88 //if (fd.entrylength < HFS_MIN_THREAD_SZ) {
89 // printk(KERN_ERR "hfs: truncated catalog thread\n");
90 // err = -EIO;
91 // goto out;
92 //}
93 if (filldir(dirent, "..", 2, 1,
94 be32_to_cpu(entry.thread.ParID), DT_DIR))
95 goto out;
96 filp->f_pos++;
97 /* fall through */
98 default:
99 if (filp->f_pos >= inode->i_size)
100 goto out;
101 err = hfs_brec_goto(&fd, filp->f_pos - 1);
102 if (err)
103 goto out;
104 }
105
106 for (;;) {
107 if (be32_to_cpu(fd.key->cat.ParID) != inode->i_ino) {
108 printk(KERN_ERR "hfs: walked past end of dir\n");
109 err = -EIO;
110 goto out;
111 }
112 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
113 type = entry.type;
114 len = hfs_mac2asc(sb, strbuf, &fd.key->cat.CName);
115 if (type == HFS_CDR_DIR) {
116 if (fd.entrylength < sizeof(struct hfs_cat_dir)) {
117 printk(KERN_ERR "hfs: small dir entry\n");
118 err = -EIO;
119 goto out;
120 }
121 if (filldir(dirent, strbuf, len, filp->f_pos,
122 be32_to_cpu(entry.dir.DirID), DT_DIR))
123 break;
124 } else if (type == HFS_CDR_FIL) {
125 if (fd.entrylength < sizeof(struct hfs_cat_file)) {
126 printk(KERN_ERR "hfs: small file entry\n");
127 err = -EIO;
128 goto out;
129 }
130 if (filldir(dirent, strbuf, len, filp->f_pos,
131 be32_to_cpu(entry.file.FlNum), DT_REG))
132 break;
133 } else {
134 printk(KERN_ERR "hfs: bad catalog entry type %d\n", type);
135 err = -EIO;
136 goto out;
137 }
138 filp->f_pos++;
139 if (filp->f_pos >= inode->i_size)
140 goto out;
141 err = hfs_brec_goto(&fd, 1);
142 if (err)
143 goto out;
144 }
145 rd = filp->private_data;
146 if (!rd) {
147 rd = kmalloc(sizeof(struct hfs_readdir_data), GFP_KERNEL);
148 if (!rd) {
149 err = -ENOMEM;
150 goto out;
151 }
152 filp->private_data = rd;
153 rd->file = filp;
154 list_add(&rd->list, &HFS_I(inode)->open_dir_list);
155 }
156 memcpy(&rd->key, &fd.key, sizeof(struct hfs_cat_key));
157 out:
158 hfs_find_exit(&fd);
159 return err;
160 }
161
162 static int hfs_dir_release(struct inode *inode, struct file *file)
163 {
164 struct hfs_readdir_data *rd = file->private_data;
165 if (rd) {
166 list_del(&rd->list);
167 kfree(rd);
168 }
169 return 0;
170 }
171
172 /*
173 * hfs_create()
174 *
175 * This is the create() entry in the inode_operations structure for
176 * regular HFS directories. The purpose is to create a new file in
177 * a directory and return a corresponding inode, given the inode for
178 * the directory and the name (and its length) of the new file.
179 */
180 static int hfs_create(struct inode *dir, struct dentry *dentry, int mode,
181 struct nameidata *nd)
182 {
183 struct inode *inode;
184 int res;
185
186 inode = hfs_new_inode(dir, &dentry->d_name, mode);
187 if (!inode)
188 return -ENOSPC;
189
190 res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
191 if (res) {
192 inode->i_nlink = 0;
193 hfs_delete_inode(inode);
194 iput(inode);
195 return res;
196 }
197 d_instantiate(dentry, inode);
198 mark_inode_dirty(inode);
199 return 0;
200 }
201
202 /*
203 * hfs_mkdir()
204 *
205 * This is the mkdir() entry in the inode_operations structure for
206 * regular HFS directories. The purpose is to create a new directory
207 * in a directory, given the inode for the parent directory and the
208 * name (and its length) of the new directory.
209 */
210 static int hfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
211 {
212 struct inode *inode;
213 int res;
214
215 inode = hfs_new_inode(dir, &dentry->d_name, S_IFDIR | mode);
216 if (!inode)
217 return -ENOSPC;
218
219 res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
220 if (res) {
221 inode->i_nlink = 0;
222 hfs_delete_inode(inode);
223 iput(inode);
224 return res;
225 }
226 d_instantiate(dentry, inode);
227 mark_inode_dirty(inode);
228 return 0;
229 }
230
231 /*
232 * hfs_unlink()
233 *
234 * This is the unlink() entry in the inode_operations structure for
235 * regular HFS directories. The purpose is to delete an existing
236 * file, given the inode for the parent directory and the name
237 * (and its length) of the existing file.
238 */
239 static int hfs_unlink(struct inode *dir, struct dentry *dentry)
240 {
241 struct inode *inode;
242 int res;
243
244 inode = dentry->d_inode;
245 res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name);
246 if (res)
247 return res;
248
249 drop_nlink(inode);
250 hfs_delete_inode(inode);
251 inode->i_ctime = CURRENT_TIME_SEC;
252 mark_inode_dirty(inode);
253
254 return res;
255 }
256
257 /*
258 * hfs_rmdir()
259 *
260 * This is the rmdir() entry in the inode_operations structure for
261 * regular HFS directories. The purpose is to delete an existing
262 * directory, given the inode for the parent directory and the name
263 * (and its length) of the existing directory.
264 */
265 static int hfs_rmdir(struct inode *dir, struct dentry *dentry)
266 {
267 struct inode *inode;
268 int res;
269
270 inode = dentry->d_inode;
271 if (inode->i_size != 2)
272 return -ENOTEMPTY;
273 res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name);
274 if (res)
275 return res;
276 clear_nlink(inode);
277 inode->i_ctime = CURRENT_TIME_SEC;
278 hfs_delete_inode(inode);
279 mark_inode_dirty(inode);
280 return 0;
281 }
282
283 /*
284 * hfs_rename()
285 *
286 * This is the rename() entry in the inode_operations structure for
287 * regular HFS directories. The purpose is to rename an existing
288 * file or directory, given the inode for the current directory and
289 * the name (and its length) of the existing file/directory and the
290 * inode for the new directory and the name (and its length) of the
291 * new file/directory.
292 * XXX: how do you handle must_be dir?
293 */
294 static int hfs_rename(struct inode *old_dir, struct dentry *old_dentry,
295 struct inode *new_dir, struct dentry *new_dentry)
296 {
297 int res;
298
299 /* Unlink destination if it already exists */
300 if (new_dentry->d_inode) {
301 res = hfs_unlink(new_dir, new_dentry);
302 if (res)
303 return res;
304 }
305
306 res = hfs_cat_move(old_dentry->d_inode->i_ino,
307 old_dir, &old_dentry->d_name,
308 new_dir, &new_dentry->d_name);
309 if (!res)
310 hfs_cat_build_key(old_dir->i_sb,
311 (btree_key *)&HFS_I(old_dentry->d_inode)->cat_key,
312 new_dir->i_ino, &new_dentry->d_name);
313 return res;
314 }
315
316 const struct file_operations hfs_dir_operations = {
317 .read = generic_read_dir,
318 .readdir = hfs_readdir,
319 .llseek = generic_file_llseek,
320 .release = hfs_dir_release,
321 };
322
323 const struct inode_operations hfs_dir_inode_operations = {
324 .create = hfs_create,
325 .lookup = hfs_lookup,
326 .unlink = hfs_unlink,
327 .mkdir = hfs_mkdir,
328 .rmdir = hfs_rmdir,
329 .rename = hfs_rename,
330 .setattr = hfs_inode_setattr,
331 };