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 inode-related functions which do not depend on
8 * which 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 /*================ Variable-like macros ================*/
26 #define HFS_VALID_MODE_BITS (S_IFREG | S_IFDIR | S_IRWXUGO)
28 /*================ File-local functions ================*/
33 * Given an HFS catalog entry initialize an inode for a file.
35 static void init_file_inode(struct inode
*inode
, hfs_u8 fork
)
38 struct hfs_cat_entry
*entry
= HFS_I(inode
)->entry
;
40 if (!IS_NOEXEC(inode
) && (fork
== HFS_FK_DATA
)) {
41 inode
->i_mode
= S_IRWXUGO
| S_IFREG
;
43 inode
->i_mode
= S_IRUGO
| S_IWUGO
| S_IFREG
;
46 if (fork
== HFS_FK_DATA
) {
47 #if 0 /* XXX: disable crlf translations for now */
48 hfs_u32 type
= hfs_get_nl(entry
->info
.file
.finfo
.fdType
);
50 HFS_I(inode
)->convert
=
51 ((HFS_SB(inode
->i_sb
)->s_conv
== 't') ||
52 ((HFS_SB(inode
->i_sb
)->s_conv
== 'a') &&
53 ((type
== htonl(0x54455854)) || /* "TEXT" */
54 (type
== htonl(0x7474726f))))); /* "ttro" */
56 HFS_I(inode
)->convert
= 0;
58 fk
= &entry
->u
.file
.data_fork
;
60 fk
= &entry
->u
.file
.rsrc_fork
;
61 HFS_I(inode
)->convert
= 0;
63 HFS_I(inode
)->fork
= fk
;
64 inode
->i_size
= fk
->lsize
;
65 inode
->i_blocks
= fk
->psize
;
69 /*================ Global functions ================*/
74 * This is the put_inode() entry in the super_operations for HFS
75 * filesystems. The purpose is to perform any filesystem-dependent
76 * cleanup necessary when the use-count of an inode falls to zero.
78 void hfs_put_inode(struct inode
* inode
)
80 struct hfs_cat_entry
*entry
= HFS_I(inode
)->entry
;
83 if (inode
->i_count
== 1) {
84 struct hfs_hdr_layout
*tmp
= HFS_I(inode
)->layout
;
87 HFS_I(inode
)->layout
= NULL
;
96 * Based very closely on fs/msdos/inode.c by Werner Almesberger
98 * This is the notify_change() field in the super_operations structure
99 * for HFS file systems. The purpose is to take that changes made to
100 * an inode and apply then in a filesystem-dependent manner. In this
101 * case the process has a few of tasks to do:
102 * 1) prevent changes to the i_uid and i_gid fields.
103 * 2) map file permissions to the closest allowable permissions
104 * 3) Since multiple Linux files can share the same on-disk inode under
105 * HFS (for instance the data and resource forks of a file) a change
106 * to permissions must be applied to all other in-core inodes which
107 * correspond to the same HFS file.
109 enum {HFS_NORM
, HFS_HDR
, HFS_CAP
};
111 static int __hfs_notify_change(struct dentry
*dentry
, struct iattr
* attr
, int kind
)
113 struct inode
*inode
= dentry
->d_inode
;
114 struct hfs_cat_entry
*entry
= HFS_I(inode
)->entry
;
115 struct dentry
**de
= entry
->sys_entry
;
116 struct hfs_sb_info
*hsb
= HFS_SB(inode
->i_sb
);
119 error
= inode_change_ok(inode
, attr
); /* basic permission checks */
121 /* Let netatalk's afpd think chmod() always succeeds */
123 (attr
->ia_valid
== (ATTR_MODE
| ATTR_CTIME
))) {
130 /* no uig/gid changes and limit which mode bits can be set */
131 if (((attr
->ia_valid
& ATTR_UID
) &&
132 (attr
->ia_uid
!= hsb
->s_uid
)) ||
133 ((attr
->ia_valid
& ATTR_GID
) &&
134 (attr
->ia_gid
!= hsb
->s_gid
)) ||
135 ((attr
->ia_valid
& ATTR_MODE
) &&
136 (((entry
->type
== HFS_CDR_DIR
) &&
137 (attr
->ia_mode
!= inode
->i_mode
))||
138 (attr
->ia_mode
& ~HFS_VALID_MODE_BITS
)))) {
139 return hsb
->s_quiet
? 0 : error
;
142 if (entry
->type
== HFS_CDR_DIR
) {
143 attr
->ia_valid
&= ~ATTR_MODE
;
144 } else if (attr
->ia_valid
& ATTR_MODE
) {
145 /* Only the 'w' bits can ever change and only all together. */
146 if (attr
->ia_mode
& S_IWUSR
) {
147 attr
->ia_mode
= inode
->i_mode
| S_IWUGO
;
149 attr
->ia_mode
= inode
->i_mode
& ~S_IWUGO
;
151 attr
->ia_mode
&= ~hsb
->s_umask
;
154 * Normal files handle size change in normal way.
155 * Oddballs are served here.
157 if (attr
->ia_valid
& ATTR_SIZE
) {
158 if (kind
== HFS_CAP
) {
159 inode
->i_size
= attr
->ia_size
;
160 if (inode
->i_size
> HFS_FORK_MAX
)
161 inode
->i_size
= HFS_FORK_MAX
;
162 mark_inode_dirty(inode
);
163 attr
->ia_valid
&= ~ATTR_SIZE
;
164 } else if (kind
== HFS_HDR
) {
165 hdr_truncate(inode
, attr
->ia_size
);
166 attr
->ia_valid
&= ~ATTR_SIZE
;
169 inode_setattr(inode
, attr
);
171 /* We wouldn't want to mess with the sizes of the other fork */
172 attr
->ia_valid
&= ~ATTR_SIZE
;
174 /* We must change all in-core inodes corresponding to this file. */
175 for (i
= 0; i
< 4; ++i
) {
176 if (de
[i
] && (de
[i
] != dentry
)) {
177 inode_setattr(de
[i
]->d_inode
, attr
);
181 /* Change the catalog entry if needed */
182 if (attr
->ia_valid
& ATTR_MTIME
) {
183 entry
->modify_date
= hfs_u_to_mtime(inode
->i_mtime
);
184 hfs_cat_mark_dirty(entry
);
186 if (attr
->ia_valid
& ATTR_MODE
) {
189 if (inode
->i_mode
& S_IWUSR
) {
190 new_flags
= entry
->u
.file
.flags
& ~HFS_FIL_LOCK
;
192 new_flags
= entry
->u
.file
.flags
| HFS_FIL_LOCK
;
195 if (new_flags
!= entry
->u
.file
.flags
) {
196 entry
->u
.file
.flags
= new_flags
;
197 hfs_cat_mark_dirty(entry
);
200 /* size changes handled in hfs_extent_adj() */
205 int hfs_notify_change(struct dentry
*dentry
, struct iattr
* attr
)
207 return __hfs_notify_change(dentry
, attr
, HFS_NORM
);
210 int hfs_notify_change_cap(struct dentry
*dentry
, struct iattr
* attr
)
212 return __hfs_notify_change(dentry
, attr
, HFS_CAP
);
215 int hfs_notify_change_hdr(struct dentry
*dentry
, struct iattr
* attr
)
217 return __hfs_notify_change(dentry
, attr
, HFS_HDR
);
220 static int hfs_writepage(struct dentry
*dentry
, struct page
*page
)
222 return block_write_full_page(page
,hfs_get_block
);
224 static int hfs_readpage(struct dentry
*dentry
, struct page
*page
)
226 return block_read_full_page(page
,hfs_get_block
);
228 static int hfs_prepare_write(struct page
*page
, unsigned from
, unsigned to
)
230 return cont_prepare_write(page
,from
,to
,hfs_get_block
,
231 &((struct inode
*)page
->mapping
->host
)->u
.hfs_i
.mmu_private
);
233 static int hfs_bmap(struct address_space
*mapping
, long block
)
235 return generic_block_bmap(mapping
,block
,hfs_get_block
);
237 struct address_space_operations hfs_aops
= {
238 readpage
: hfs_readpage
,
239 writepage
: hfs_writepage
,
240 prepare_write
: hfs_prepare_write
,
241 commit_write
: generic_commit_write
,
248 * Given the MDB for a HFS filesystem, a 'key' and an 'entry' in
249 * the catalog B-tree and the 'type' of the desired file return the
250 * inode for that file/directory or NULL. Note that 'type' indicates
251 * whether we want the actual file or directory, or the corresponding
252 * metadata (AppleDouble header file or CAP metadata file).
254 * In an ideal world we could call iget() and would not need this
255 * function. However, since there is no way to even know the inode
256 * number until we've found the file/directory in the catalog B-tree
257 * that simply won't happen.
259 * The main idea here is to look in the catalog B-tree to get the
260 * vital info about the file or directory (including the file id which
261 * becomes the inode number) and then to call iget() and return the
262 * inode if it is complete. If it is not then we use the catalog
263 * entry to fill in the missing info, by calling the appropriate
264 * 'fillin' function. Note that these fillin functions are
265 * essentially hfs_*_read_inode() functions, but since there is no way
266 * to pass the catalog entry through iget() to such a read_inode()
267 * function, we have to call them after iget() returns an incomplete
268 * inode to us. This is pretty much the same problem faced in the NFS
269 * code, and pretty much the same solution. The SMB filesystem deals
270 * with this in a different way: by using the address of the
271 * kmalloc()'d space which holds the data as the inode number.
273 * XXX: Both this function and NFS's corresponding nfs_fhget() would
274 * benefit from a way to pass an additional (void *) through iget() to
275 * the VFS read_inode() function.
277 * this will hfs_cat_put() the entry if it fails.
279 struct inode
*hfs_iget(struct hfs_cat_entry
*entry
, ino_t type
,
280 struct dentry
*dentry
)
282 struct dentry
**sys_entry
;
283 struct super_block
*sb
;
290 /* If there are several processes all calling __iget() for
291 the same inode then they will all get the same one back.
292 The first one to return from __iget() will notice that the
293 i_mode field of the inode is blank and KNOW that it is
294 the first to return. Therefore, it will set the appropriate
295 'sys_entry' field in the entry and initialize the inode.
296 All the initialization must be done without sleeping,
297 or else other processes could end up using a partially
298 initialized inode. */
300 sb
= entry
->mdb
->sys_mdb
;
301 sys_entry
= &entry
->sys_entry
[HFS_ITYPE_TO_INT(type
)];
303 if (!(inode
= iget(sb
, ntohl(entry
->cnid
) | type
))) {
308 if (inode
->i_dev
!= sb
->s_dev
) {
309 iput(inode
); /* automatically does an hfs_cat_put */
311 } else if (!inode
->i_mode
|| (*sys_entry
== NULL
)) {
312 /* Initialize the inode */
313 struct hfs_sb_info
*hsb
= HFS_SB(sb
);
316 inode
->i_ctime
= inode
->i_atime
= inode
->i_mtime
=
317 hfs_m_to_utime(entry
->modify_date
);
318 inode
->i_blksize
= HFS_SECTOR_SIZE
;
319 inode
->i_uid
= hsb
->s_uid
;
320 inode
->i_gid
= hsb
->s_gid
;
322 memset(HFS_I(inode
), 0, sizeof(struct hfs_inode_info
));
323 HFS_I(inode
)->magic
= HFS_INO_MAGIC
;
324 HFS_I(inode
)->entry
= entry
;
325 HFS_I(inode
)->tz_secondswest
= hfs_to_utc(0);
327 hsb
->s_ifill(inode
, type
, hsb
->s_version
);
328 if (!hsb
->s_afpd
&& (entry
->type
== HFS_CDR_FIL
) &&
329 (entry
->u
.file
.flags
& HFS_FIL_LOCK
)) {
330 inode
->i_mode
&= ~S_IWUGO
;
332 inode
->i_mode
&= ~hsb
->s_umask
;
334 if (!inode
->i_mode
) {
335 iput(inode
); /* does an hfs_cat_put */
338 *sys_entry
= dentry
; /* cache dentry */
345 /*================ Scheme-specific functions ================*/
350 * This function serves the same purpose as a read_inode() function does
351 * in other filesystems. It is called by __hfs_iget() to fill in
352 * the missing fields of an uninitialized inode under the CAP scheme.
354 void hfs_cap_ifill(struct inode
* inode
, ino_t type
, const int version
)
356 struct hfs_cat_entry
*entry
= HFS_I(inode
)->entry
;
358 HFS_I(inode
)->d_drop_op
= hfs_cap_drop_dentry
;
359 if (type
== HFS_CAP_FNDR
) {
360 inode
->i_size
= sizeof(struct hfs_cap_info
);
363 inode
->i_mode
= S_IRUGO
| S_IWUGO
| S_IFREG
;
364 inode
->i_op
= &hfs_cap_info_inode_operations
;
365 inode
->i_fop
= &hfs_cap_info_operations
;
366 } else if (entry
->type
== HFS_CDR_FIL
) {
367 init_file_inode(inode
, (type
== HFS_CAP_DATA
) ?
368 HFS_FK_DATA
: HFS_FK_RSRC
);
369 inode
->i_op
= &hfs_file_inode_operations
;
370 inode
->i_fop
= &hfs_file_operations
;
371 inode
->i_mapping
->a_ops
= &hfs_aops
;
372 inode
->u
.hfs_i
.mmu_private
= inode
->i_size
;
373 } else { /* Directory */
374 struct hfs_dir
*hdir
= &entry
->u
.dir
;
377 inode
->i_size
= hdir
->files
+ hdir
->dirs
+ 5;
378 HFS_I(inode
)->dir_size
= 1;
379 if (type
== HFS_CAP_NDIR
) {
380 inode
->i_mode
= S_IRWXUGO
| S_IFDIR
;
381 inode
->i_nlink
= hdir
->dirs
+ 4;
382 inode
->i_op
= &hfs_cap_ndir_inode_operations
;
383 inode
->i_fop
= &hfs_cap_dir_operations
;
384 HFS_I(inode
)->file_type
= HFS_CAP_NORM
;
385 } else if (type
== HFS_CAP_FDIR
) {
386 inode
->i_mode
= S_IRUGO
| S_IXUGO
| S_IFDIR
;
388 inode
->i_op
= &hfs_cap_fdir_inode_operations
;
389 inode
->i_fop
= &hfs_cap_dir_operations
;
390 HFS_I(inode
)->file_type
= HFS_CAP_FNDR
;
391 } else if (type
== HFS_CAP_RDIR
) {
392 inode
->i_mode
= S_IRUGO
| S_IXUGO
| S_IFDIR
;
394 inode
->i_op
= &hfs_cap_rdir_inode_operations
;
395 inode
->i_fop
= &hfs_cap_dir_operations
;
396 HFS_I(inode
)->file_type
= HFS_CAP_RSRC
;
404 * This function serves the same purpose as a read_inode() function does
405 * in other filesystems. It is called by __hfs_iget() to fill in
406 * the missing fields of an uninitialized inode under the AppleDouble
409 void hfs_dbl_ifill(struct inode
* inode
, ino_t type
, const int version
)
411 struct hfs_cat_entry
*entry
= HFS_I(inode
)->entry
;
413 HFS_I(inode
)->d_drop_op
= hfs_dbl_drop_dentry
;
414 if (type
== HFS_DBL_HDR
) {
415 if (entry
->type
== HFS_CDR_FIL
) {
416 init_file_inode(inode
, HFS_FK_RSRC
);
417 inode
->i_size
+= HFS_DBL_HDR_LEN
;
418 HFS_I(inode
)->default_layout
= &hfs_dbl_fil_hdr_layout
;
420 inode
->i_size
= HFS_DBL_HDR_LEN
;
421 inode
->i_mode
= S_IRUGO
| S_IWUGO
| S_IFREG
;
423 HFS_I(inode
)->default_layout
= &hfs_dbl_dir_hdr_layout
;
425 inode
->i_op
= &hfs_hdr_inode_operations
;
426 inode
->i_fop
= &hfs_hdr_operations
;
427 } else if (entry
->type
== HFS_CDR_FIL
) {
428 init_file_inode(inode
, HFS_FK_DATA
);
429 inode
->i_op
= &hfs_file_inode_operations
;
430 inode
->i_fop
= &hfs_file_operations
;
431 inode
->i_mapping
->a_ops
= &hfs_aops
;
432 inode
->u
.hfs_i
.mmu_private
= inode
->i_size
;
433 } else { /* Directory */
434 struct hfs_dir
*hdir
= &entry
->u
.dir
;
437 inode
->i_nlink
= hdir
->dirs
+ 2;
438 inode
->i_size
= 3 + 2 * (hdir
->dirs
+ hdir
->files
);
439 inode
->i_mode
= S_IRWXUGO
| S_IFDIR
;
440 inode
->i_op
= &hfs_dbl_dir_inode_operations
;
441 inode
->i_fop
= &hfs_dbl_dir_operations
;
442 HFS_I(inode
)->file_type
= HFS_DBL_NORM
;
443 HFS_I(inode
)->dir_size
= 2;
450 * This function serves the same purpose as a read_inode() function does
451 * in other filesystems. It is called by __hfs_iget() to fill in
452 * the missing fields of an uninitialized inode under the Netatalk
455 void hfs_nat_ifill(struct inode
* inode
, ino_t type
, const int version
)
457 struct hfs_cat_entry
*entry
= HFS_I(inode
)->entry
;
459 HFS_I(inode
)->d_drop_op
= hfs_nat_drop_dentry
;
460 if (type
== HFS_NAT_HDR
) {
461 if (entry
->type
== HFS_CDR_FIL
) {
462 init_file_inode(inode
, HFS_FK_RSRC
);
463 inode
->i_size
+= HFS_NAT_HDR_LEN
;
465 inode
->i_size
= HFS_NAT_HDR_LEN
;
466 inode
->i_mode
= S_IRUGO
| S_IWUGO
| S_IFREG
;
469 inode
->i_op
= &hfs_hdr_inode_operations
;
470 inode
->i_fop
= &hfs_hdr_operations
;
471 HFS_I(inode
)->default_layout
= (version
== 2) ?
472 &hfs_nat2_hdr_layout
: &hfs_nat_hdr_layout
;
473 } else if (entry
->type
== HFS_CDR_FIL
) {
474 init_file_inode(inode
, HFS_FK_DATA
);
475 inode
->i_op
= &hfs_file_inode_operations
;
476 inode
->i_fop
= &hfs_file_operations
;
477 inode
->i_mapping
->a_ops
= &hfs_aops
;
478 inode
->u
.hfs_i
.mmu_private
= inode
->i_size
;
479 } else { /* Directory */
480 struct hfs_dir
*hdir
= &entry
->u
.dir
;
483 inode
->i_size
= hdir
->files
+ hdir
->dirs
+ 4;
484 inode
->i_mode
= S_IRWXUGO
| S_IFDIR
;
485 HFS_I(inode
)->dir_size
= 1;
486 if (type
== HFS_NAT_NDIR
) {
487 inode
->i_nlink
= hdir
->dirs
+ 3;
488 inode
->i_op
= &hfs_nat_ndir_inode_operations
;
489 HFS_I(inode
)->file_type
= HFS_NAT_NORM
;
490 } else if (type
== HFS_NAT_HDIR
) {
492 inode
->i_op
= &hfs_nat_hdir_inode_operations
;
493 HFS_I(inode
)->file_type
= HFS_NAT_HDR
;
495 inode
->i_fop
= &hfs_nat_dir_operations
;