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 directory-related functions independent of which
8 * scheme is being used to represent forks.
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 /*================ File-local functions ================*/
29 * Build a key for a file by the given name in the given directory.
30 * If the name matches one of the reserved names returns 1 otherwise 0.
32 static int build_key(struct hfs_cat_key
*key
, struct inode
*dir
,
33 const char *name
, int len
)
35 struct hfs_name cname
;
36 const struct hfs_name
*reserved
;
39 hfs_nameout(dir
, &cname
, name
, len
);
41 /* check against reserved names */
42 reserved
= HFS_SB(dir
->i_sb
)->s_reserved1
;
43 while (reserved
->Len
) {
44 if (hfs_streq(reserved
->Name
, reserved
->Len
,
45 cname
.Name
, cname
.Len
)) {
51 /* check against the names reserved only in the root directory */
52 if (HFS_I(dir
)->entry
->cnid
== htonl(HFS_ROOT_CNID
)) {
53 reserved
= HFS_SB(dir
->i_sb
)->s_reserved2
;
54 while (reserved
->Len
) {
55 if (hfs_streq(reserved
->Name
, reserved
->Len
,
56 cname
.Name
, cname
.Len
)) {
64 hfs_cat_build_key(HFS_I(dir
)->entry
->cnid
, &cname
, key
);
72 * Update the fields 'i_size', 'i_nlink', 'i_ctime', 'i_mtime' and
73 * 'i_version' of the inodes associated with a directory that has
74 * had a file ('is_dir'==0) or directory ('is_dir'!=0) added to it.
76 static inline void update_dirs_plus(struct hfs_cat_entry
*dir
, int is_dir
)
80 for (i
= 0; i
< 4; ++i
) {
81 struct dentry
*de
= dir
->sys_entry
[i
];
83 struct inode
*tmp
= de
->d_inode
;
84 if (S_ISDIR(tmp
->i_mode
)) {
86 (i
== HFS_ITYPE_TO_INT(HFS_ITYPE_NORM
))) {
87 /* In "normal" directory only */
90 tmp
->i_size
+= HFS_I(tmp
)->dir_size
;
91 tmp
->i_version
= ++event
;
93 tmp
->i_ctime
= tmp
->i_mtime
= CURRENT_TIME
;
94 mark_inode_dirty(tmp
);
100 * update_dirs_minus()
102 * Update the fields 'i_size', 'i_nlink', 'i_ctime', 'i_mtime' and
103 * 'i_version' of the inodes associated with a directory that has
104 * had a file ('is_dir'==0) or directory ('is_dir'!=0) removed.
106 static inline void update_dirs_minus(struct hfs_cat_entry
*dir
, int is_dir
)
110 for (i
= 0; i
< 4; ++i
) {
111 struct dentry
*de
= dir
->sys_entry
[i
];
113 struct inode
*tmp
= de
->d_inode
;
114 if (S_ISDIR(tmp
->i_mode
)) {
116 (i
== HFS_ITYPE_TO_INT(HFS_ITYPE_NORM
))) {
117 /* In "normal" directory only */
120 tmp
->i_size
-= HFS_I(tmp
)->dir_size
;
121 tmp
->i_version
= ++event
;
123 tmp
->i_ctime
= tmp
->i_mtime
= CURRENT_TIME
;
124 mark_inode_dirty(tmp
);
130 * mark_inodes_deleted()
132 * Update inodes associated with a deleted entry to reflect its deletion.
133 * Well, we really just drop the dentry.
135 * XXX: we should be using delete_inode for some of this stuff.
137 static inline void mark_inodes_deleted(struct hfs_cat_entry
*entry
,
138 struct dentry
*dentry
)
144 for (i
= 0; i
< 4; ++i
) {
145 if ((de
= entry
->sys_entry
[i
]) && (dentry
!= de
)) {
149 tmp
->i_ctime
= CURRENT_TIME
;
150 mark_inode_dirty(tmp
);
157 /*================ Global functions ================*/
162 * This is the create() entry in the inode_operations structure for
163 * regular HFS directories. The purpose is to create a new file in
164 * a directory and return a corresponding inode, given the inode for
165 * the directory and the name (and its length) of the new file.
167 int hfs_create(struct inode
* dir
, struct dentry
*dentry
, int mode
)
169 struct hfs_cat_entry
*entry
= HFS_I(dir
)->entry
;
170 struct hfs_cat_entry
*new;
171 struct hfs_cat_key key
;
175 /* build the key, checking against reserved names */
176 if (build_key(&key
, dir
, dentry
->d_name
.name
, dentry
->d_name
.len
))
179 if ((error
= hfs_cat_create(entry
, &key
,
180 (mode
& S_IWUSR
) ? 0 : HFS_FIL_LOCK
,
181 HFS_SB(dir
->i_sb
)->s_type
,
182 HFS_SB(dir
->i_sb
)->s_creator
, &new)))
185 /* create an inode for the new file. back out if we run
187 new->count
++; /* hfs_iget() eats one */
188 if (!(inode
= hfs_iget(new, HFS_I(dir
)->file_type
, dentry
))) {
189 hfs_cat_delete(entry
, new, 1);
195 update_dirs_plus(entry
, 0);
196 /* toss any relevant negative dentries */
197 if (HFS_I(dir
)->d_drop_op
)
198 HFS_I(dir
)->d_drop_op(dentry
, HFS_I(dir
)->file_type
);
199 mark_inode_dirty(inode
);
200 d_instantiate(dentry
, inode
);
207 * This is the mkdir() entry in the inode_operations structure for
208 * regular HFS directories. The purpose is to create a new directory
209 * in a directory, given the inode for the parent directory and the
210 * name (and its length) of the new directory.
212 int hfs_mkdir(struct inode
* parent
, struct dentry
*dentry
, int mode
)
214 struct hfs_cat_entry
*entry
= HFS_I(parent
)->entry
;
215 struct hfs_cat_entry
*new;
216 struct hfs_cat_key key
;
220 /* build the key, checking against reserved names */
221 if (build_key(&key
, parent
, dentry
->d_name
.name
,
225 /* try to create the directory */
226 if ((error
= hfs_cat_mkdir(entry
, &key
, &new)))
229 /* back out if we run into trouble */
230 new->count
++; /* hfs_iget eats one */
231 if (!(inode
= hfs_iget(new, HFS_I(parent
)->file_type
, dentry
))) {
232 hfs_cat_delete(entry
, new, 1);
238 update_dirs_plus(entry
, 1);
239 mark_inode_dirty(inode
);
240 d_instantiate(dentry
, inode
);
247 * This is the unlink() entry in the inode_operations structure for
248 * regular HFS directories. The purpose is to delete an existing
249 * file, given the inode for the parent directory and the name
250 * (and its length) of the existing file.
252 int hfs_unlink(struct inode
* dir
, struct dentry
*dentry
)
254 struct hfs_cat_entry
*entry
= HFS_I(dir
)->entry
;
255 struct hfs_cat_entry
*victim
= NULL
;
256 struct hfs_cat_key key
;
259 if (build_key(&key
, dir
, dentry
->d_name
.name
,
263 if (!(victim
= hfs_cat_get(entry
->mdb
, &key
)))
267 if (victim
->type
!= HFS_CDR_FIL
)
270 if (!(error
= hfs_cat_delete(entry
, victim
, 1))) {
271 struct inode
*inode
= dentry
->d_inode
;
273 mark_inodes_deleted(victim
, dentry
);
275 inode
->i_ctime
= CURRENT_TIME
;
276 mark_inode_dirty(inode
);
278 update_dirs_minus(entry
, 0);
282 hfs_cat_put(victim
); /* Note that hfs_cat_put(NULL) is safe. */
289 * This is the rmdir() entry in the inode_operations structure for
290 * regular HFS directories. The purpose is to delete an existing
291 * directory, given the inode for the parent directory and the name
292 * (and its length) of the existing directory.
294 int hfs_rmdir(struct inode
* parent
, struct dentry
*dentry
)
296 struct hfs_cat_entry
*entry
= HFS_I(parent
)->entry
;
297 struct hfs_cat_entry
*victim
= NULL
;
298 struct inode
*inode
= dentry
->d_inode
;
299 struct hfs_cat_key key
;
302 if (build_key(&key
, parent
, dentry
->d_name
.name
,
306 if (!(victim
= hfs_cat_get(entry
->mdb
, &key
)))
310 if (victim
->type
!= HFS_CDR_DIR
)
314 if (!list_empty(&dentry
->d_hash
))
317 if (/* we only have to worry about 2 and 3 for mount points */
318 (victim
->sys_entry
[2] &&
319 (victim
->sys_entry
[2]->d_mounts
!=
320 victim
->sys_entry
[2]->d_covers
)) ||
321 (victim
->sys_entry
[3] &&
322 (victim
->sys_entry
[3]->d_mounts
!=
323 victim
->sys_entry
[3]->d_covers
))
328 if ((error
= hfs_cat_delete(entry
, victim
, 1)))
331 mark_inodes_deleted(victim
, dentry
);
333 inode
->i_ctime
= CURRENT_TIME
;
334 mark_inode_dirty(inode
);
336 update_dirs_minus(entry
, 1);
339 hfs_cat_put(victim
); /* Note that hfs_cat_put(NULL) is safe. */
346 * This is the rename() entry in the inode_operations structure for
347 * regular HFS directories. The purpose is to rename an existing
348 * file or directory, given the inode for the current directory and
349 * the name (and its length) of the existing file/directory and the
350 * inode for the new directory and the name (and its length) of the
351 * new file/directory.
352 * XXX: how do you handle must_be dir?
354 int hfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
355 struct inode
*new_dir
, struct dentry
*new_dentry
)
357 struct hfs_cat_entry
*old_parent
= HFS_I(old_dir
)->entry
;
358 struct hfs_cat_entry
*new_parent
= HFS_I(new_dir
)->entry
;
359 struct hfs_cat_entry
*victim
= NULL
;
360 struct hfs_cat_entry
*deleted
;
361 struct hfs_cat_key key
;
364 if (build_key(&key
, old_dir
, old_dentry
->d_name
.name
,
365 old_dentry
->d_name
.len
) ||
366 (HFS_ITYPE(old_dir
->i_ino
) != HFS_ITYPE(new_dir
->i_ino
)))
369 if (!(victim
= hfs_cat_get(old_parent
->mdb
, &key
)))
373 if (build_key(&key
, new_dir
, new_dentry
->d_name
.name
,
374 new_dentry
->d_name
.len
))
377 if (!(error
= hfs_cat_move(old_parent
, new_parent
,
378 victim
, &key
, &deleted
))) {
379 int is_dir
= (victim
->type
== HFS_CDR_DIR
);
381 /* drop the old dentries */
382 mark_inodes_deleted(victim
, old_dentry
);
383 update_dirs_minus(old_parent
, is_dir
);
385 mark_inodes_deleted(deleted
, new_dentry
);
386 hfs_cat_put(deleted
);
388 /* no existing inodes. just drop negative dentries */
389 if (HFS_I(new_dir
)->d_drop_op
)
390 HFS_I(new_dir
)->d_drop_op(new_dentry
,
391 HFS_I(new_dir
)->file_type
);
392 update_dirs_plus(new_parent
, is_dir
);
398 hfs_cat_put(victim
); /* Note that hfs_cat_put(NULL) is safe. */