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 General 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.
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_unlink(struct inode *, struct dentry *);
31 static int dbl_rmdir(struct inode *, struct dentry *);
32 static int dbl_rename(struct inode *, struct dentry *,
33 struct inode *, struct dentry *);
35 /*================ Global variables ================*/
39 #define ROOTINFO_LEN 8
40 #define PCNT_ROOTINFO_LEN 9
42 const struct hfs_name hfs_dbl_reserved1[] = {
48 const struct hfs_name hfs_dbl_reserved2[] = {
49 {ROOTINFO_LEN, "RootInfo"},
50 {PCNT_ROOTINFO_LEN, "%RootInfo"},
54 #define DOT (&hfs_dbl_reserved1[0])
55 #define DOT_DOT (&hfs_dbl_reserved1[1])
56 #define ROOTINFO (&hfs_dbl_reserved2[0])
57 #define PCNT_ROOTINFO (&hfs_dbl_reserved2[1])
59 struct file_operations hfs_dbl_dir_operations = {
60 read: generic_read_dir,
65 struct inode_operations hfs_dbl_dir_inode_operations = {
72 setattr: hfs_notify_change,
76 /*================ File-local functions ================*/
81 static int is_hdr(struct inode *dir, const char *name, int len)
86 struct hfs_cat_entry *entry = HFS_I(dir)->entry;
87 struct hfs_cat_entry *victim;
88 struct hfs_name cname;
89 struct hfs_cat_key key;
91 hfs_nameout(dir, &cname, name+1, len-1);
92 hfs_cat_build_key(entry->cnid, &cname, &key);
93 if ((victim = hfs_cat_get(entry->mdb, &key))) {
104 * This is the lookup() entry in the inode_operations structure for
105 * HFS directories in the AppleDouble scheme. The purpose is to
106 * generate the inode corresponding to an entry in a directory, given
107 * the inode for the directory and the name (and its length) of the
110 static struct dentry *dbl_lookup(struct inode * dir, struct dentry *dentry)
112 struct hfs_name cname;
113 struct hfs_cat_entry *entry;
114 struct hfs_cat_key key;
115 struct inode *inode = NULL;
117 dentry->d_op = &hfs_dentry_operations;
118 entry = HFS_I(dir)->entry;
120 /* Perform name-mangling */
121 hfs_nameout(dir, &cname, dentry->d_name.name, dentry->d_name.len);
123 /* no need to check for "." or ".." */
125 /* Check for "%RootInfo" if in the root directory. */
126 if ((entry->cnid == htonl(HFS_ROOT_CNID)) &&
127 hfs_streq(cname.Name, cname.Len,
128 PCNT_ROOTINFO->Name, PCNT_ROOTINFO_LEN)) {
129 ++entry->count; /* __hfs_iget() eats one */
130 inode = hfs_iget(entry, HFS_DBL_HDR, dentry);
134 /* Do an hfs_iget() on the mangled name. */
135 hfs_cat_build_key(entry->cnid, &cname, &key);
136 inode = hfs_iget(hfs_cat_get(entry->mdb, &key), HFS_DBL_NORM, dentry);
138 /* Try as a header if not found and first character is '%' */
139 if (!inode && (dentry->d_name.name[0] == '%')) {
140 hfs_nameout(dir, &cname, dentry->d_name.name+1,
141 dentry->d_name.len-1);
142 hfs_cat_build_key(entry->cnid, &cname, &key);
143 inode = hfs_iget(hfs_cat_get(entry->mdb, &key),
144 HFS_DBL_HDR, dentry);
148 d_add(dentry, inode);
155 * This is the readdir() entry in the file_operations structure for
156 * HFS directories in the AppleDouble scheme. The purpose is to
157 * enumerate the entries in a directory, given the inode of the
158 * directory and a (struct file *), the 'f_pos' field of which
159 * indicates the location in the directory. The (struct file *) is
160 * updated so that the next call with the same 'dir' and 'filp'
161 * arguments will produce the next directory entry. The entries are
162 * returned in 'dirent', which is "filled-in" by calling filldir().
163 * This allows the same readdir() function be used for different
164 * formats. We try to read in as many entries as we can before
165 * filldir() refuses to take any more.
167 * XXX: In the future it may be a good idea to consider not generating
168 * metadata files for covered directories since the data doesn't
169 * correspond to the mounted directory. However this requires an
170 * iget() for every directory which could be considered an excessive
171 * amount of overhead. Since the inode for a mount point is always
172 * in-core this is another argument for a call to get an inode if it
173 * is in-core or NULL if it is not.
175 static int dbl_readdir(struct file * filp,
176 void * dirent, filldir_t filldir)
178 struct hfs_brec brec;
179 struct hfs_cat_entry *entry;
180 struct inode *dir = filp->f_dentry->d_inode;
182 entry = HFS_I(dir)->entry;
184 if (filp->f_pos == 0) {
185 /* Entry 0 is for "." */
186 if (filldir(dirent, DOT->Name, DOT_LEN, 0, dir->i_ino,
193 if (filp->f_pos == 1) {
194 /* Entry 1 is for ".." */
195 if (filldir(dirent, DOT_DOT->Name, DOT_DOT_LEN, 1,
196 hfs_get_hl(entry->key.ParID), DT_DIR)) {
202 if (filp->f_pos < (dir->i_size - 1)) {
206 if (hfs_cat_open(entry, &brec) ||
207 hfs_cat_next(entry, &brec, (filp->f_pos - 1) >> 1,
212 while (filp->f_pos < (dir->i_size - 1)) {
213 unsigned char tmp_name[HFS_NAMEMAX + 1];
215 int is_hdr = (filp->f_pos & 1);
219 ino = ntohl(cnid) | HFS_DBL_HDR;
221 len = 1 + hfs_namein(dir, tmp_name + 1,
222 &((struct hfs_cat_key *)brec.key)->CName);
224 if (hfs_cat_next(entry, &brec, 1,
229 len = hfs_namein(dir, tmp_name,
230 &((struct hfs_cat_key *)brec.key)->CName);
233 if (filldir(dirent, tmp_name, len, filp->f_pos, ino,
235 hfs_cat_close(entry, &brec);
240 hfs_cat_close(entry, &brec);
243 if (filp->f_pos == (dir->i_size - 1)) {
244 if (entry->cnid == htonl(HFS_ROOT_CNID)) {
245 /* In root dir last entry is for "%RootInfo" */
246 if (filldir(dirent, PCNT_ROOTINFO->Name,
247 PCNT_ROOTINFO_LEN, filp->f_pos,
248 ntohl(entry->cnid) | HFS_DBL_HDR,
262 * This is the create() entry in the inode_operations structure for
263 * AppleDouble directories. The purpose is to create a new file in
264 * a directory and return a corresponding inode, given the inode for
265 * the directory and the name (and its length) of the new file.
267 static int dbl_create(struct inode * dir, struct dentry *dentry,
272 if (is_hdr(dir, dentry->d_name.name, dentry->d_name.len)) {
275 error = hfs_create(dir, dentry, mode);
283 * This is the mkdir() entry in the inode_operations structure for
284 * AppleDouble directories. The purpose is to create a new directory
285 * in a directory, given the inode for the parent directory and the
286 * name (and its length) of the new directory.
288 static int dbl_mkdir(struct inode * parent, struct dentry *dentry,
293 if (is_hdr(parent, dentry->d_name.name, dentry->d_name.len)) {
296 error = hfs_mkdir(parent, dentry, mode);
304 * This is the unlink() entry in the inode_operations structure for
305 * AppleDouble directories. The purpose is to delete an existing
306 * file, given the inode for the parent directory and the name
307 * (and its length) of the existing file.
309 static int dbl_unlink(struct inode * dir, struct dentry *dentry)
313 error = hfs_unlink(dir, dentry);
314 if ((error == -ENOENT) && is_hdr(dir, dentry->d_name.name,
315 dentry->d_name.len)) {
324 * This is the rmdir() entry in the inode_operations structure for
325 * AppleDouble directories. The purpose is to delete an existing
326 * directory, given the inode for the parent directory and the name
327 * (and its length) of the existing directory.
329 static int dbl_rmdir(struct inode * parent, struct dentry *dentry)
333 error = hfs_rmdir(parent, dentry);
334 if ((error == -ENOENT) && is_hdr(parent, dentry->d_name.name,
335 dentry->d_name.len)) {
344 * This is the rename() entry in the inode_operations structure for
345 * AppleDouble directories. The purpose is to rename an existing
346 * file or directory, given the inode for the current directory and
347 * the name (and its length) of the existing file/directory and the
348 * inode for the new directory and the name (and its length) of the
349 * new file/directory.
351 * XXX: how do we handle must_be_dir?
353 static int dbl_rename(struct inode *old_dir, struct dentry *old_dentry,
354 struct inode *new_dir, struct dentry *new_dentry)
358 if (is_hdr(new_dir, new_dentry->d_name.name,
359 new_dentry->d_name.len)) {
362 error = hfs_rename(old_dir, old_dentry,
363 new_dir, new_dentry);
364 if ((error == -ENOENT) /*&& !must_be_dir*/ &&
365 is_hdr(old_dir, old_dentry->d_name.name,
366 old_dentry->d_name.len)) {
374 /* due to the dcache caching negative dentries for non-existent files,
375 * we need to drop those entries when a file silently gets created.
376 * as far as i can tell, the calls that need to do this are the file
377 * related calls (create, rename, and mknod). the directory calls
378 * should be immune. the relevant calls in dir.c call drop_dentry
379 * upon successful completion. */
380 void hfs_dbl_drop_dentry(struct dentry *dentry, const ino_t type)
382 unsigned char tmp_name[HFS_NAMEMAX + 1];
383 struct dentry *de = NULL;
387 /* given %name, look for name. i don't think this happens. */
388 de = hfs_lookup_dentry(dentry->d_parent,
389 dentry->d_name.name + 1, dentry->d_name.len - 1);
392 /* given name, look for %name */
394 strncpy(tmp_name + 1, dentry->d_name.name, HFS_NAMELEN - 1);
395 de = hfs_lookup_dentry(dentry->d_parent,
396 tmp_name, dentry->d_name.len + 1);