Import 2.2.8pre2
[davej-history.git] / fs / hfs / dir_dbl.c
blob80e9906273511f09cc3474131e4609a7b526811d
1 /*
2 * linux/fs/hfs/dir_dbl.c
4 * Copyright (C) 1995-1997 Paul H. Hargrove
5 * This file may be distributed under the terms of the GNU Public License.
7 * This file contains the inode_operations and file_operations
8 * structures for HFS directories.
10 * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
12 * "XXX" in a comment is a note to myself to consider changing something.
14 * In function preconditions the term "valid" applied to a pointer to
15 * a structure means that the pointer is non-NULL and the structure it
16 * points to has all fields initialized to consistent values.
19 #include "hfs.h"
20 #include <linux/hfs_fs_sb.h>
21 #include <linux/hfs_fs_i.h>
22 #include <linux/hfs_fs.h>
24 /*================ Forward declarations ================*/
26 static struct dentry *dbl_lookup(struct inode *, struct dentry *);
27 static int dbl_readdir(struct file *, void *, filldir_t);
28 static int dbl_create(struct inode *, struct dentry *, int);
29 static int dbl_mkdir(struct inode *, struct dentry *, int);
30 static int dbl_mknod(struct inode *, struct dentry *, int, int);
31 static int dbl_unlink(struct inode *, struct dentry *);
32 static int dbl_rmdir(struct inode *, struct dentry *);
33 static int dbl_rename(struct inode *, struct dentry *,
34 struct inode *, struct dentry *);
36 /*================ Global variables ================*/
38 #define DOT_LEN 1
39 #define DOT_DOT_LEN 2
40 #define ROOTINFO_LEN 8
41 #define PCNT_ROOTINFO_LEN 9
43 const struct hfs_name hfs_dbl_reserved1[] = {
44 {DOT_LEN, "."},
45 {DOT_DOT_LEN, ".."},
46 {0, ""},
49 const struct hfs_name hfs_dbl_reserved2[] = {
50 {ROOTINFO_LEN, "RootInfo"},
51 {PCNT_ROOTINFO_LEN, "%RootInfo"},
52 {0, ""},
55 #define DOT (&hfs_dbl_reserved1[0])
56 #define DOT_DOT (&hfs_dbl_reserved1[1])
57 #define ROOTINFO (&hfs_dbl_reserved2[0])
58 #define PCNT_ROOTINFO (&hfs_dbl_reserved2[1])
60 static struct file_operations hfs_dbl_dir_operations = {
61 NULL, /* lseek - default */
62 hfs_dir_read, /* read - invalid */
63 NULL, /* write - bad */
64 dbl_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_dbl_dir_inode_operations = {
78 &hfs_dbl_dir_operations,/* default directory file-ops */
79 dbl_create, /* create */
80 dbl_lookup, /* lookup */
81 NULL, /* link */
82 dbl_unlink, /* unlink */
83 NULL, /* symlink */
84 dbl_mkdir, /* mkdir */
85 dbl_rmdir, /* rmdir */
86 dbl_mknod, /* mknod */
87 dbl_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 */
99 /*================ File-local functions ================*/
102 * is_hdr()
104 static int is_hdr(struct inode *dir, const char *name, int len)
106 int retval = 0;
108 if (name[0] == '%') {
109 struct hfs_cat_entry *entry = HFS_I(dir)->entry;
110 struct hfs_cat_entry *victim;
111 struct hfs_name cname;
112 struct hfs_cat_key key;
114 hfs_nameout(dir, &cname, name+1, len-1);
115 hfs_cat_build_key(entry->cnid, &cname, &key);
116 if ((victim = hfs_cat_get(entry->mdb, &key))) {
117 hfs_cat_put(victim);
118 retval = 1;
121 return retval;
125 * dbl_lookup()
127 * This is the lookup() entry in the inode_operations structure for
128 * HFS directories in the AppleDouble scheme. The purpose is to
129 * generate the inode corresponding to an entry in a directory, given
130 * the inode for the directory and the name (and its length) of the
131 * entry.
133 static struct dentry *dbl_lookup(struct inode * dir, struct dentry *dentry)
135 struct hfs_name cname;
136 struct hfs_cat_entry *entry;
137 struct hfs_cat_key key;
138 struct inode *inode = NULL;
140 dentry->d_op = &hfs_dentry_operations;
141 entry = HFS_I(dir)->entry;
143 /* Perform name-mangling */
144 hfs_nameout(dir, &cname, dentry->d_name.name, dentry->d_name.len);
146 /* no need to check for "." or ".." */
148 /* Check for "%RootInfo" if in the root directory. */
149 if ((entry->cnid == htonl(HFS_ROOT_CNID)) &&
150 hfs_streq(cname.Name, cname.Len,
151 PCNT_ROOTINFO->Name, PCNT_ROOTINFO_LEN)) {
152 ++entry->count; /* __hfs_iget() eats one */
153 inode = hfs_iget(entry, HFS_DBL_HDR, dentry);
154 goto done;
157 /* Do an hfs_iget() on the mangled name. */
158 hfs_cat_build_key(entry->cnid, &cname, &key);
159 inode = hfs_iget(hfs_cat_get(entry->mdb, &key), HFS_DBL_NORM, dentry);
161 /* Try as a header if not found and first character is '%' */
162 if (!inode && (dentry->d_name.name[0] == '%')) {
163 hfs_nameout(dir, &cname, dentry->d_name.name+1,
164 dentry->d_name.len-1);
165 hfs_cat_build_key(entry->cnid, &cname, &key);
166 inode = hfs_iget(hfs_cat_get(entry->mdb, &key),
167 HFS_DBL_HDR, dentry);
170 done:
171 d_add(dentry, inode);
172 return NULL;
176 * dbl_readdir()
178 * This is the readdir() entry in the file_operations structure for
179 * HFS directories in the AppleDouble scheme. The purpose is to
180 * enumerate the entries in a directory, given the inode of the
181 * directory and a (struct file *), the 'f_pos' field of which
182 * indicates the location in the directory. The (struct file *) is
183 * updated so that the next call with the same 'dir' and 'filp'
184 * arguments will produce the next directory entry. The entries are
185 * returned in 'dirent', which is "filled-in" by calling filldir().
186 * This allows the same readdir() function be used for different
187 * formats. We try to read in as many entries as we can before
188 * filldir() refuses to take any more.
190 * XXX: In the future it may be a good idea to consider not generating
191 * metadata files for covered directories since the data doesn't
192 * correspond to the mounted directory. However this requires an
193 * iget() for every directory which could be considered an excessive
194 * amount of overhead. Since the inode for a mount point is always
195 * in-core this is another argument for a call to get an inode if it
196 * is in-core or NULL if it is not.
198 static int dbl_readdir(struct file * filp,
199 void * dirent, filldir_t filldir)
201 struct hfs_brec brec;
202 struct hfs_cat_entry *entry;
203 struct inode *dir = filp->f_dentry->d_inode;
205 if (!dir || !dir->i_sb || !S_ISDIR(dir->i_mode)) {
206 return -EBADF;
209 entry = HFS_I(dir)->entry;
211 if (filp->f_pos == 0) {
212 /* Entry 0 is for "." */
213 if (filldir(dirent, DOT->Name, DOT_LEN, 0, dir->i_ino)) {
214 return 0;
216 filp->f_pos = 1;
219 if (filp->f_pos == 1) {
220 /* Entry 1 is for ".." */
221 if (filldir(dirent, DOT_DOT->Name, DOT_DOT_LEN, 1,
222 hfs_get_hl(entry->key.ParID))) {
223 return 0;
225 filp->f_pos = 2;
228 if (filp->f_pos < (dir->i_size - 1)) {
229 hfs_u32 cnid;
230 hfs_u8 type;
232 if (hfs_cat_open(entry, &brec) ||
233 hfs_cat_next(entry, &brec, (filp->f_pos - 1) >> 1,
234 &cnid, &type)) {
235 return 0;
238 while (filp->f_pos < (dir->i_size - 1)) {
239 unsigned char tmp_name[HFS_NAMEMAX + 1];
240 ino_t ino;
241 int is_hdr = (filp->f_pos & 1);
242 unsigned int len;
244 if (is_hdr) {
245 ino = ntohl(cnid) | HFS_DBL_HDR;
246 tmp_name[0] = '%';
247 len = 1 + hfs_namein(dir, tmp_name + 1,
248 &((struct hfs_cat_key *)brec.key)->CName);
249 } else {
250 if (hfs_cat_next(entry, &brec, 1,
251 &cnid, &type)) {
252 return 0;
254 ino = ntohl(cnid);
255 len = hfs_namein(dir, tmp_name,
256 &((struct hfs_cat_key *)brec.key)->CName);
259 if (filldir(dirent, tmp_name, len, filp->f_pos, ino)) {
260 hfs_cat_close(entry, &brec);
261 return 0;
263 ++filp->f_pos;
265 hfs_cat_close(entry, &brec);
268 if (filp->f_pos == (dir->i_size - 1)) {
269 if (entry->cnid == htonl(HFS_ROOT_CNID)) {
270 /* In root dir last entry is for "%RootInfo" */
271 if (filldir(dirent, PCNT_ROOTINFO->Name,
272 PCNT_ROOTINFO_LEN, filp->f_pos,
273 ntohl(entry->cnid) | HFS_DBL_HDR)) {
274 return 0;
277 ++filp->f_pos;
280 return 0;
284 * dbl_create()
286 * This is the create() entry in the inode_operations structure for
287 * AppleDouble directories. The purpose is to create a new file in
288 * a directory and return a corresponding inode, given the inode for
289 * the directory and the name (and its length) of the new file.
291 static int dbl_create(struct inode * dir, struct dentry *dentry,
292 int mode)
294 int error;
296 if (is_hdr(dir, dentry->d_name.name, dentry->d_name.len)) {
297 error = -EEXIST;
298 } else {
299 error = hfs_create(dir, dentry, mode);
301 return error;
305 * dbl_mkdir()
307 * This is the mkdir() entry in the inode_operations structure for
308 * AppleDouble directories. The purpose is to create a new directory
309 * in a directory, given the inode for the parent directory and the
310 * name (and its length) of the new directory.
312 static int dbl_mkdir(struct inode * parent, struct dentry *dentry,
313 int mode)
315 int error;
317 if (is_hdr(parent, dentry->d_name.name, dentry->d_name.len)) {
318 error = -EEXIST;
319 } else {
320 error = hfs_mkdir(parent, dentry, mode);
322 return error;
326 * dbl_mknod()
328 * This is the mknod() entry in the inode_operations structure for
329 * regular HFS directories. The purpose is to create a new entry
330 * in a directory, given the inode for the parent directory and the
331 * name (and its length) and the mode of the new entry (and the device
332 * number if the entry is to be a device special file).
334 static int dbl_mknod(struct inode *dir, struct dentry *dentry,
335 int mode, int rdev)
337 int error;
339 if (is_hdr(dir, dentry->d_name.name, dentry->d_name.len)) {
340 error = -EEXIST;
341 } else {
342 error = hfs_mknod(dir, dentry, mode, rdev);
344 return error;
348 * dbl_unlink()
350 * This is the unlink() entry in the inode_operations structure for
351 * AppleDouble directories. The purpose is to delete an existing
352 * file, given the inode for the parent directory and the name
353 * (and its length) of the existing file.
355 static int dbl_unlink(struct inode * dir, struct dentry *dentry)
357 int error;
359 error = hfs_unlink(dir, dentry);
360 if ((error == -ENOENT) && is_hdr(dir, dentry->d_name.name,
361 dentry->d_name.len)) {
362 error = -EPERM;
364 return error;
368 * dbl_rmdir()
370 * This is the rmdir() entry in the inode_operations structure for
371 * AppleDouble directories. The purpose is to delete an existing
372 * directory, given the inode for the parent directory and the name
373 * (and its length) of the existing directory.
375 static int dbl_rmdir(struct inode * parent, struct dentry *dentry)
377 int error;
379 error = hfs_rmdir(parent, dentry);
380 if ((error == -ENOENT) && is_hdr(parent, dentry->d_name.name,
381 dentry->d_name.len)) {
382 error = -ENOTDIR;
384 return error;
388 * dbl_rename()
390 * This is the rename() entry in the inode_operations structure for
391 * AppleDouble directories. The purpose is to rename an existing
392 * file or directory, given the inode for the current directory and
393 * the name (and its length) of the existing file/directory and the
394 * inode for the new directory and the name (and its length) of the
395 * new file/directory.
397 * XXX: how do we handle must_be_dir?
399 static int dbl_rename(struct inode *old_dir, struct dentry *old_dentry,
400 struct inode *new_dir, struct dentry *new_dentry)
402 int error;
404 if (is_hdr(new_dir, new_dentry->d_name.name,
405 new_dentry->d_name.len)) {
406 error = -EPERM;
407 } else {
408 error = hfs_rename(old_dir, old_dentry,
409 new_dir, new_dentry);
410 if ((error == -ENOENT) /*&& !must_be_dir*/ &&
411 is_hdr(old_dir, old_dentry->d_name.name,
412 old_dentry->d_name.len)) {
413 error = -EPERM;
416 return error;
420 /* due to the dcache caching negative dentries for non-existent files,
421 * we need to drop those entries when a file silently gets created.
422 * as far as i can tell, the calls that need to do this are the file
423 * related calls (create, rename, and mknod). the directory calls
424 * should be immune. the relevant calls in dir.c call drop_dentry
425 * upon successful completion. */
426 void hfs_dbl_drop_dentry(struct dentry *dentry, const ino_t type)
428 unsigned char tmp_name[HFS_NAMEMAX + 1];
429 struct dentry *de = NULL;
431 switch (type) {
432 case HFS_DBL_HDR:
433 /* given %name, look for name. i don't think this happens. */
434 de = hfs_lookup_dentry(dentry->d_parent,
435 dentry->d_name.name + 1, dentry->d_name.len - 1);
436 break;
437 case HFS_DBL_DATA:
438 /* given name, look for %name */
439 tmp_name[0] = '%';
440 strncpy(tmp_name + 1, dentry->d_name.name, HFS_NAMELEN - 1);
441 de = hfs_lookup_dentry(dentry->d_parent,
442 tmp_name, dentry->d_name.len + 1);
445 if (de) {
446 if (!de->d_inode)
447 d_drop(de);
448 dput(de);