Import 2.2.8pre2
[davej-history.git] / fs / hfs / dir_cap.c
blob0ab81d9663322750ffac2caae4b19f119b42d663
1 /*
2 * Copyright (C) 1995-1997 Paul H. Hargrove
3 * This file may be distributed under the terms of the GNU Public License.
5 * This file contains the inode_operations and file_operations
6 * structures for HFS directories under the CAP scheme.
8 * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
10 * The source code distribution of the Columbia AppleTalk Package for
11 * UNIX, version 6.0, (CAP) was used as a specification of the
12 * location and format of files used by CAP's Aufs. No code from CAP
13 * appears in hfs_fs. hfs_fs is not a work ``derived'' from CAP in
14 * the sense of intellectual property law.
16 * "XXX" in a comment is a note to myself to consider changing something.
18 * In function preconditions the term "valid" applied to a pointer to
19 * a structure means that the pointer is non-NULL and the structure it
20 * points to has all fields initialized to consistent values.
23 #include "hfs.h"
24 #include <linux/hfs_fs_sb.h>
25 #include <linux/hfs_fs_i.h>
26 #include <linux/hfs_fs.h>
28 /*================ Forward declarations ================*/
30 static struct dentry *cap_lookup(struct inode *, struct dentry *);
31 static int cap_readdir(struct file *, void *, filldir_t);
33 /*================ Global variables ================*/
35 #define DOT_LEN 1
36 #define DOT_DOT_LEN 2
37 #define DOT_RESOURCE_LEN 9
38 #define DOT_FINDERINFO_LEN 11
39 #define DOT_ROOTINFO_LEN 9
41 const struct hfs_name hfs_cap_reserved1[] = {
42 {DOT_LEN, "."},
43 {DOT_DOT_LEN, ".."},
44 {DOT_RESOURCE_LEN, ".resource"},
45 {DOT_FINDERINFO_LEN, ".finderinfo"},
46 {0, ""},
49 const struct hfs_name hfs_cap_reserved2[] = {
50 {DOT_ROOTINFO_LEN, ".rootinfo"},
51 {0, ""},
54 #define DOT (&hfs_cap_reserved1[0])
55 #define DOT_DOT (&hfs_cap_reserved1[1])
56 #define DOT_RESOURCE (&hfs_cap_reserved1[2])
57 #define DOT_FINDERINFO (&hfs_cap_reserved1[3])
58 #define DOT_ROOTINFO (&hfs_cap_reserved2[0])
60 static struct file_operations hfs_cap_dir_operations = {
61 NULL, /* lseek - default */
62 hfs_dir_read, /* read - invalid */
63 NULL, /* write - bad */
64 cap_readdir, /* readdir */
65 NULL, /* select - default */
66 NULL, /* ioctl - default */
67 NULL, /* mmap - none */
68 NULL, /* no special open code */
69 NULL, /* flush */
70 NULL, /* no special release code */
71 file_fsync, /* fsync - default */
72 NULL, /* fasync - default */
73 NULL, /* check_media_change - none */
74 NULL /* revalidate - none */
77 struct inode_operations hfs_cap_ndir_inode_operations = {
78 &hfs_cap_dir_operations,/* default directory file-ops */
79 hfs_create, /* create */
80 cap_lookup, /* lookup */
81 NULL, /* link */
82 hfs_unlink, /* unlink */
83 NULL, /* symlink */
84 hfs_mkdir, /* mkdir */
85 hfs_rmdir, /* rmdir */
86 hfs_mknod, /* mknod */
87 hfs_rename, /* rename */
88 NULL, /* readlink */
89 NULL, /* follow_link */
90 NULL, /* readpage */
91 NULL, /* writepage */
92 NULL, /* bmap */
93 NULL, /* truncate */
94 NULL, /* permission */
95 NULL /* smap */
98 struct inode_operations hfs_cap_fdir_inode_operations = {
99 &hfs_cap_dir_operations,/* default directory file-ops */
100 NULL, /* create */
101 cap_lookup, /* lookup */
102 NULL, /* link */
103 NULL, /* unlink */
104 NULL, /* symlink */
105 NULL, /* mkdir */
106 NULL, /* rmdir */
107 NULL, /* mknod */
108 NULL, /* rename */
109 NULL, /* readlink */
110 NULL, /* follow_link */
111 NULL, /* readpage */
112 NULL, /* writepage */
113 NULL, /* bmap */
114 NULL, /* truncate */
115 NULL, /* permission */
116 NULL /* smap */
119 struct inode_operations hfs_cap_rdir_inode_operations = {
120 &hfs_cap_dir_operations,/* default directory file-ops */
121 hfs_create, /* create */
122 cap_lookup, /* lookup */
123 NULL, /* link */
124 NULL, /* unlink */
125 NULL, /* symlink */
126 NULL, /* mkdir */
127 NULL, /* rmdir */
128 NULL, /* mknod */
129 NULL, /* rename */
130 NULL, /* readlink */
131 NULL, /* follow_link */
132 NULL, /* readpage */
133 NULL, /* writepage */
134 NULL, /* bmap */
135 NULL, /* truncate */
136 NULL, /* permission */
137 NULL /* smap */
140 /*================ File-local functions ================*/
143 * cap_lookup()
145 * This is the lookup() entry in the inode_operations structure for
146 * HFS directories in the CAP scheme. The purpose is to generate the
147 * inode corresponding to an entry in a directory, given the inode for
148 * the directory and the name (and its length) of the entry.
150 static struct dentry *cap_lookup(struct inode * dir, struct dentry *dentry)
152 ino_t dtype;
153 struct hfs_name cname;
154 struct hfs_cat_entry *entry;
155 struct hfs_cat_key key;
156 struct inode *inode = NULL;
158 dentry->d_op = &hfs_dentry_operations;
159 entry = HFS_I(dir)->entry;
160 dtype = HFS_ITYPE(dir->i_ino);
162 /* Perform name-mangling */
163 hfs_nameout(dir, &cname, dentry->d_name.name,
164 dentry->d_name.len);
166 /* no need to check for "." or ".." */
168 /* Check for special directories if in a normal directory.
169 Note that cap_dupdir() does an iput(dir). */
170 if (dtype==HFS_CAP_NDIR) {
171 /* Check for ".resource", ".finderinfo" and ".rootinfo" */
172 if (hfs_streq(cname.Name, cname.Len,
173 DOT_RESOURCE->Name, DOT_RESOURCE_LEN)) {
174 ++entry->count; /* __hfs_iget() eats one */
175 inode = hfs_iget(entry, HFS_CAP_RDIR, dentry);
176 goto done;
177 } else if (hfs_streq(cname.Name, cname.Len,
178 DOT_FINDERINFO->Name,
179 DOT_FINDERINFO_LEN)) {
180 ++entry->count; /* __hfs_iget() eats one */
181 inode = hfs_iget(entry, HFS_CAP_FDIR, dentry);
182 goto done;
183 } else if ((entry->cnid == htonl(HFS_ROOT_CNID)) &&
184 hfs_streq(cname.Name, cname.Len,
185 DOT_ROOTINFO->Name, DOT_ROOTINFO_LEN)) {
186 ++entry->count; /* __hfs_iget() eats one */
187 inode = hfs_iget(entry, HFS_CAP_FNDR, dentry);
188 goto done;
192 /* Do an hfs_iget() on the mangled name. */
193 hfs_cat_build_key(entry->cnid, &cname, &key);
194 inode = hfs_iget(hfs_cat_get(entry->mdb, &key),
195 HFS_I(dir)->file_type, dentry);
197 /* Don't return a resource fork for a directory */
198 if (inode && (dtype == HFS_CAP_RDIR) &&
199 (HFS_I(inode)->entry->type == HFS_CDR_DIR)) {
200 iput(inode); /* this does an hfs_cat_put */
201 inode = NULL;
204 done:
205 d_add(dentry, inode);
206 return NULL;
210 * cap_readdir()
212 * This is the readdir() entry in the file_operations structure for
213 * HFS directories in the CAP scheme. The purpose is to enumerate the
214 * entries in a directory, given the inode of the directory and a
215 * (struct file *), the 'f_pos' field of which indicates the location
216 * in the directory. The (struct file *) is updated so that the next
217 * call with the same 'dir' and 'filp' arguments will produce the next
218 * directory entry. The entries are returned in 'dirent', which is
219 * "filled-in" by calling filldir(). This allows the same readdir()
220 * function be used for different dirent formats. We try to read in
221 * as many entries as we can before filldir() refuses to take any more.
223 * XXX: In the future it may be a good idea to consider not generating
224 * metadata files for covered directories since the data doesn't
225 * correspond to the mounted directory. However this requires an
226 * iget() for every directory which could be considered an excessive
227 * amount of overhead. Since the inode for a mount point is always
228 * in-core this is another argument for a call to get an inode if it
229 * is in-core or NULL if it is not.
231 static int cap_readdir(struct file * filp,
232 void * dirent, filldir_t filldir)
234 ino_t type;
235 int skip_dirs;
236 struct hfs_brec brec;
237 struct hfs_cat_entry *entry;
238 struct inode *dir = filp->f_dentry->d_inode;
240 if (!dir || !dir->i_sb || !S_ISDIR(dir->i_mode)) {
241 return -EBADF;
244 entry = HFS_I(dir)->entry;
245 type = HFS_ITYPE(dir->i_ino);
246 skip_dirs = (type == HFS_CAP_RDIR);
248 if (filp->f_pos == 0) {
249 /* Entry 0 is for "." */
250 if (filldir(dirent, DOT->Name, DOT_LEN, 0, dir->i_ino)) {
251 return 0;
253 filp->f_pos = 1;
256 if (filp->f_pos == 1) {
257 /* Entry 1 is for ".." */
258 hfs_u32 cnid;
260 if (type == HFS_CAP_NDIR) {
261 cnid = hfs_get_nl(entry->key.ParID);
262 } else {
263 cnid = entry->cnid;
266 if (filldir(dirent, DOT_DOT->Name,
267 DOT_DOT_LEN, 1, ntohl(cnid))) {
268 return 0;
270 filp->f_pos = 2;
273 if (filp->f_pos < (dir->i_size - 3)) {
274 hfs_u32 cnid;
275 hfs_u8 type;
277 if (hfs_cat_open(entry, &brec) ||
278 hfs_cat_next(entry, &brec, filp->f_pos - 2, &cnid, &type)) {
279 return 0;
281 while (filp->f_pos < (dir->i_size - 3)) {
282 if (hfs_cat_next(entry, &brec, 1, &cnid, &type)) {
283 return 0;
285 if (!skip_dirs || (type != HFS_CDR_DIR)) {
286 ino_t ino;
287 unsigned int len;
288 unsigned char tmp_name[HFS_NAMEMAX];
290 ino = ntohl(cnid) | HFS_I(dir)->file_type;
291 len = hfs_namein(dir, tmp_name,
292 &((struct hfs_cat_key *)brec.key)->CName);
293 if (filldir(dirent, tmp_name, len,
294 filp->f_pos, ino)) {
295 hfs_cat_close(entry, &brec);
296 return 0;
299 ++filp->f_pos;
301 hfs_cat_close(entry, &brec);
304 if (filp->f_pos == (dir->i_size - 3)) {
305 if ((entry->cnid == htonl(HFS_ROOT_CNID)) &&
306 (type == HFS_CAP_NDIR)) {
307 /* In root dir last-2 entry is for ".rootinfo" */
308 if (filldir(dirent, DOT_ROOTINFO->Name,
309 DOT_ROOTINFO_LEN, filp->f_pos,
310 ntohl(entry->cnid) | HFS_CAP_FNDR)) {
311 return 0;
314 ++filp->f_pos;
317 if (filp->f_pos == (dir->i_size - 2)) {
318 if (type == HFS_CAP_NDIR) {
319 /* In normal dirs last-1 entry is for ".finderinfo" */
320 if (filldir(dirent, DOT_FINDERINFO->Name,
321 DOT_FINDERINFO_LEN, filp->f_pos,
322 ntohl(entry->cnid) | HFS_CAP_FDIR)) {
323 return 0;
326 ++filp->f_pos;
329 if (filp->f_pos == (dir->i_size - 1)) {
330 if (type == HFS_CAP_NDIR) {
331 /* In normal dirs last entry is for ".resource" */
332 if (filldir(dirent, DOT_RESOURCE->Name,
333 DOT_RESOURCE_LEN, filp->f_pos,
334 ntohl(entry->cnid) | HFS_CAP_RDIR)) {
335 return 0;
338 ++filp->f_pos;
341 return 0;
345 /* due to the dcache caching negative dentries for non-existent files,
346 * we need to drop those entries when a file silently gets created.
347 * as far as i can tell, the calls that need to do this are the file
348 * related calls (create, rename, and mknod). the directory calls
349 * should be immune. the relevant calls in dir.c call drop_dentry
350 * upon successful completion. */
351 void hfs_cap_drop_dentry(struct dentry *dentry, const ino_t type)
353 if (type == HFS_CAP_DATA) { /* given name */
354 hfs_drop_special(dentry->d_parent, DOT_FINDERINFO, dentry);
355 hfs_drop_special(dentry->d_parent, DOT_RESOURCE, dentry);
356 } else {
357 struct dentry *de;
359 /* given {.resource,.finderinfo}/name, look for name */
360 if ((de = hfs_lookup_dentry(dentry->d_parent->d_parent,
361 dentry->d_name.name, dentry->d_name.len))) {
362 if (!de->d_inode)
363 d_drop(de);
364 dput(de);
367 switch (type) {
368 case HFS_CAP_RSRC: /* given .resource/name */
369 /* look for .finderinfo/name */
370 hfs_drop_special(dentry->d_parent->d_parent, DOT_FINDERINFO,
371 dentry);
372 break;
373 case HFS_CAP_FNDR: /* given .finderinfo/name. i don't this
374 * happens. */
375 /* look for .resource/name */
376 hfs_drop_special(dentry->d_parent->d_parent, DOT_RESOURCE,
377 dentry);
378 break;