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 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>
23 #include <linux/smp_lock.h>
25 /*================ Variable-like macros ================*/
27 #define HFS_VALID_MODE_BITS (S_IFREG | S_IFDIR | S_IRWXUGO)
29 /*================ File-local functions ================*/
34 * Given an HFS catalog entry initialize an inode for a file.
36 static void init_file_inode(struct inode *inode, hfs_u8 fork)
39 struct hfs_cat_entry *entry = HFS_I(inode)->entry;
41 if (fork == HFS_FK_DATA) {
42 inode->i_mode = S_IRWXUGO | S_IFREG;
44 inode->i_mode = S_IRUGO | S_IWUGO | S_IFREG;
47 if (fork == HFS_FK_DATA) {
48 #if 0 /* XXX: disable crlf translations for now */
49 hfs_u32 type = hfs_get_nl(entry->info.file.finfo.fdType);
51 HFS_I(inode)->convert =
52 ((HFS_SB(inode->i_sb)->s_conv == 't') ||
53 ((HFS_SB(inode->i_sb)->s_conv == 'a') &&
54 ((type == htonl(0x54455854)) || /* "TEXT" */
55 (type == htonl(0x7474726f))))); /* "ttro" */
57 HFS_I(inode)->convert = 0;
59 fk = &entry->u.file.data_fork;
61 fk = &entry->u.file.rsrc_fork;
62 HFS_I(inode)->convert = 0;
64 HFS_I(inode)->fork = fk;
65 inode->i_size = fk->lsize;
66 inode->i_blocks = fk->psize;
70 /*================ Global functions ================*/
75 * This is the put_inode() entry in the super_operations for HFS
76 * filesystems. The purpose is to perform any filesystem-dependent
77 * cleanup necessary when the use-count of an inode falls to zero.
79 void hfs_put_inode(struct inode * inode)
81 struct hfs_cat_entry *entry = HFS_I(inode)->entry;
85 if (atomic_read(&inode->i_count) == 1) {
86 struct hfs_hdr_layout *tmp = HFS_I(inode)->layout;
89 HFS_I(inode)->layout = NULL;
99 * Based very closely on fs/msdos/inode.c by Werner Almesberger
101 * This is the notify_change() field in the super_operations structure
102 * for HFS file systems. The purpose is to take that changes made to
103 * an inode and apply then in a filesystem-dependent manner. In this
104 * case the process has a few of tasks to do:
105 * 1) prevent changes to the i_uid and i_gid fields.
106 * 2) map file permissions to the closest allowable permissions
107 * 3) Since multiple Linux files can share the same on-disk inode under
108 * HFS (for instance the data and resource forks of a file) a change
109 * to permissions must be applied to all other in-core inodes which
110 * correspond to the same HFS file.
112 enum {HFS_NORM, HFS_HDR, HFS_CAP};
114 static int __hfs_notify_change(struct dentry *dentry, struct iattr * attr, int kind)
116 struct inode *inode = dentry->d_inode;
117 struct hfs_cat_entry *entry = HFS_I(inode)->entry;
118 struct dentry **de = entry->sys_entry;
119 struct hfs_sb_info *hsb = HFS_SB(inode->i_sb);
122 error = inode_change_ok(inode, attr); /* basic permission checks */
124 /* Let netatalk's afpd think chmod() always succeeds */
126 (attr->ia_valid == (ATTR_MODE | ATTR_CTIME))) {
133 /* no uig/gid changes and limit which mode bits can be set */
134 if (((attr->ia_valid & ATTR_UID) &&
135 (attr->ia_uid != hsb->s_uid)) ||
136 ((attr->ia_valid & ATTR_GID) &&
137 (attr->ia_gid != hsb->s_gid)) ||
138 ((attr->ia_valid & ATTR_MODE) &&
139 (((entry->type == HFS_CDR_DIR) &&
140 (attr->ia_mode != inode->i_mode))||
141 (attr->ia_mode & ~HFS_VALID_MODE_BITS)))) {
142 return hsb->s_quiet ? 0 : error;
145 if (entry->type == HFS_CDR_DIR) {
146 attr->ia_valid &= ~ATTR_MODE;
147 } else if (attr->ia_valid & ATTR_MODE) {
148 /* Only the 'w' bits can ever change and only all together. */
149 if (attr->ia_mode & S_IWUSR) {
150 attr->ia_mode = inode->i_mode | S_IWUGO;
152 attr->ia_mode = inode->i_mode & ~S_IWUGO;
154 attr->ia_mode &= ~hsb->s_umask;
157 * Normal files handle size change in normal way.
158 * Oddballs are served here.
160 if (attr->ia_valid & ATTR_SIZE) {
161 if (kind == HFS_CAP) {
162 inode->i_size = attr->ia_size;
163 if (inode->i_size > HFS_FORK_MAX)
164 inode->i_size = HFS_FORK_MAX;
165 mark_inode_dirty(inode);
166 attr->ia_valid &= ~ATTR_SIZE;
167 } else if (kind == HFS_HDR) {
168 hdr_truncate(inode, attr->ia_size);
169 attr->ia_valid &= ~ATTR_SIZE;
172 error = inode_setattr(inode, attr);
176 /* We wouldn't want to mess with the sizes of the other fork */
177 attr->ia_valid &= ~ATTR_SIZE;
179 /* We must change all in-core inodes corresponding to this file. */
180 for (i = 0; i < 4; ++i) {
181 if (de[i] && (de[i] != dentry)) {
182 inode_setattr(de[i]->d_inode, attr);
186 /* Change the catalog entry if needed */
187 if (attr->ia_valid & ATTR_MTIME) {
188 entry->modify_date = hfs_u_to_mtime(inode->i_mtime);
189 hfs_cat_mark_dirty(entry);
191 if (attr->ia_valid & ATTR_MODE) {
194 if (inode->i_mode & S_IWUSR) {
195 new_flags = entry->u.file.flags & ~HFS_FIL_LOCK;
197 new_flags = entry->u.file.flags | HFS_FIL_LOCK;
200 if (new_flags != entry->u.file.flags) {
201 entry->u.file.flags = new_flags;
202 hfs_cat_mark_dirty(entry);
205 /* size changes handled in hfs_extent_adj() */
210 int hfs_notify_change(struct dentry *dentry, struct iattr * attr)
212 return __hfs_notify_change(dentry, attr, HFS_NORM);
215 int hfs_notify_change_cap(struct dentry *dentry, struct iattr * attr)
217 return __hfs_notify_change(dentry, attr, HFS_CAP);
220 int hfs_notify_change_hdr(struct dentry *dentry, struct iattr * attr)
222 return __hfs_notify_change(dentry, attr, HFS_HDR);
225 static int hfs_writepage(struct page *page)
227 return block_write_full_page(page,hfs_get_block);
229 static int hfs_readpage(struct file *file, struct page *page)
231 return block_read_full_page(page,hfs_get_block);
233 static int hfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
235 return cont_prepare_write(page,from,to,hfs_get_block,
236 &page->mapping->host->u.hfs_i.mmu_private);
238 static int hfs_bmap(struct address_space *mapping, long block)
240 return generic_block_bmap(mapping,block,hfs_get_block);
242 struct address_space_operations hfs_aops = {
243 readpage: hfs_readpage,
244 writepage: hfs_writepage,
245 sync_page: block_sync_page,
246 prepare_write: hfs_prepare_write,
247 commit_write: generic_commit_write,
254 * Given the MDB for a HFS filesystem, a 'key' and an 'entry' in
255 * the catalog B-tree and the 'type' of the desired file return the
256 * inode for that file/directory or NULL. Note that 'type' indicates
257 * whether we want the actual file or directory, or the corresponding
258 * metadata (AppleDouble header file or CAP metadata file).
260 * In an ideal world we could call iget() and would not need this
261 * function. However, since there is no way to even know the inode
262 * number until we've found the file/directory in the catalog B-tree
263 * that simply won't happen.
265 * The main idea here is to look in the catalog B-tree to get the
266 * vital info about the file or directory (including the file id which
267 * becomes the inode number) and then to call iget() and return the
268 * inode if it is complete. If it is not then we use the catalog
269 * entry to fill in the missing info, by calling the appropriate
270 * 'fillin' function. Note that these fillin functions are
271 * essentially hfs_*_read_inode() functions, but since there is no way
272 * to pass the catalog entry through iget() to such a read_inode()
273 * function, we have to call them after iget() returns an incomplete
274 * inode to us. This is pretty much the same problem faced in the NFS
275 * code, and pretty much the same solution. The SMB filesystem deals
276 * with this in a different way: by using the address of the
277 * kmalloc()'d space which holds the data as the inode number.
279 * XXX: Both this function and NFS's corresponding nfs_fhget() would
280 * benefit from a way to pass an additional (void *) through iget() to
281 * the VFS read_inode() function.
283 * this will hfs_cat_put() the entry if it fails.
285 struct inode *hfs_iget(struct hfs_cat_entry *entry, ino_t type,
286 struct dentry *dentry)
288 struct dentry **sys_entry;
289 struct super_block *sb;
296 /* If there are several processes all calling __iget() for
297 the same inode then they will all get the same one back.
298 The first one to return from __iget() will notice that the
299 i_mode field of the inode is blank and KNOW that it is
300 the first to return. Therefore, it will set the appropriate
301 'sys_entry' field in the entry and initialize the inode.
302 All the initialization must be done without sleeping,
303 or else other processes could end up using a partially
304 initialized inode. */
306 sb = entry->mdb->sys_mdb;
307 sys_entry = &entry->sys_entry[HFS_ITYPE_TO_INT(type)];
309 if (!(inode = iget(sb, ntohl(entry->cnid) | type))) {
314 if (inode->i_dev != sb->s_dev) {
315 iput(inode); /* automatically does an hfs_cat_put */
317 } else if (!inode->i_mode || (*sys_entry == NULL)) {
318 /* Initialize the inode */
319 struct hfs_sb_info *hsb = HFS_SB(sb);
322 inode->i_ctime = inode->i_atime = inode->i_mtime =
323 hfs_m_to_utime(entry->modify_date);
324 inode->i_blksize = HFS_SECTOR_SIZE;
325 inode->i_uid = hsb->s_uid;
326 inode->i_gid = hsb->s_gid;
328 memset(HFS_I(inode), 0, sizeof(struct hfs_inode_info));
329 HFS_I(inode)->magic = HFS_INO_MAGIC;
330 HFS_I(inode)->entry = entry;
331 HFS_I(inode)->tz_secondswest = hfs_to_utc(0);
333 hsb->s_ifill(inode, type, hsb->s_version);
334 if (!hsb->s_afpd && (entry->type == HFS_CDR_FIL) &&
335 (entry->u.file.flags & HFS_FIL_LOCK)) {
336 inode->i_mode &= ~S_IWUGO;
338 inode->i_mode &= ~hsb->s_umask;
340 if (!inode->i_mode) {
341 iput(inode); /* does an hfs_cat_put */
344 *sys_entry = dentry; /* cache dentry */
351 /*================ Scheme-specific functions ================*/
356 * This function serves the same purpose as a read_inode() function does
357 * in other filesystems. It is called by __hfs_iget() to fill in
358 * the missing fields of an uninitialized inode under the CAP scheme.
360 void hfs_cap_ifill(struct inode * inode, ino_t type, const int version)
362 struct hfs_cat_entry *entry = HFS_I(inode)->entry;
364 HFS_I(inode)->d_drop_op = hfs_cap_drop_dentry;
365 if (type == HFS_CAP_FNDR) {
366 inode->i_size = sizeof(struct hfs_cap_info);
369 inode->i_mode = S_IRUGO | S_IWUGO | S_IFREG;
370 inode->i_op = &hfs_cap_info_inode_operations;
371 inode->i_fop = &hfs_cap_info_operations;
372 } else if (entry->type == HFS_CDR_FIL) {
373 init_file_inode(inode, (type == HFS_CAP_DATA) ?
374 HFS_FK_DATA : HFS_FK_RSRC);
375 inode->i_op = &hfs_file_inode_operations;
376 inode->i_fop = &hfs_file_operations;
377 inode->i_mapping->a_ops = &hfs_aops;
378 inode->u.hfs_i.mmu_private = inode->i_size;
379 } else { /* Directory */
380 struct hfs_dir *hdir = &entry->u.dir;
383 inode->i_size = hdir->files + hdir->dirs + 5;
384 HFS_I(inode)->dir_size = 1;
385 if (type == HFS_CAP_NDIR) {
386 inode->i_mode = S_IRWXUGO | S_IFDIR;
387 inode->i_nlink = hdir->dirs + 4;
388 inode->i_op = &hfs_cap_ndir_inode_operations;
389 inode->i_fop = &hfs_cap_dir_operations;
390 HFS_I(inode)->file_type = HFS_CAP_NORM;
391 } else if (type == HFS_CAP_FDIR) {
392 inode->i_mode = S_IRUGO | S_IXUGO | S_IFDIR;
394 inode->i_op = &hfs_cap_fdir_inode_operations;
395 inode->i_fop = &hfs_cap_dir_operations;
396 HFS_I(inode)->file_type = HFS_CAP_FNDR;
397 } else if (type == HFS_CAP_RDIR) {
398 inode->i_mode = S_IRUGO | S_IXUGO | S_IFDIR;
400 inode->i_op = &hfs_cap_rdir_inode_operations;
401 inode->i_fop = &hfs_cap_dir_operations;
402 HFS_I(inode)->file_type = HFS_CAP_RSRC;
410 * This function serves the same purpose as a read_inode() function does
411 * in other filesystems. It is called by __hfs_iget() to fill in
412 * the missing fields of an uninitialized inode under the AppleDouble
415 void hfs_dbl_ifill(struct inode * inode, ino_t type, const int version)
417 struct hfs_cat_entry *entry = HFS_I(inode)->entry;
419 HFS_I(inode)->d_drop_op = hfs_dbl_drop_dentry;
420 if (type == HFS_DBL_HDR) {
421 if (entry->type == HFS_CDR_FIL) {
422 init_file_inode(inode, HFS_FK_RSRC);
423 inode->i_size += HFS_DBL_HDR_LEN;
424 HFS_I(inode)->default_layout = &hfs_dbl_fil_hdr_layout;
426 inode->i_size = HFS_DBL_HDR_LEN;
427 inode->i_mode = S_IRUGO | S_IWUGO | S_IFREG;
429 HFS_I(inode)->default_layout = &hfs_dbl_dir_hdr_layout;
431 inode->i_op = &hfs_hdr_inode_operations;
432 inode->i_fop = &hfs_hdr_operations;
433 } else if (entry->type == HFS_CDR_FIL) {
434 init_file_inode(inode, HFS_FK_DATA);
435 inode->i_op = &hfs_file_inode_operations;
436 inode->i_fop = &hfs_file_operations;
437 inode->i_mapping->a_ops = &hfs_aops;
438 inode->u.hfs_i.mmu_private = inode->i_size;
439 } else { /* Directory */
440 struct hfs_dir *hdir = &entry->u.dir;
443 inode->i_nlink = hdir->dirs + 2;
444 inode->i_size = 3 + 2 * (hdir->dirs + hdir->files);
445 inode->i_mode = S_IRWXUGO | S_IFDIR;
446 inode->i_op = &hfs_dbl_dir_inode_operations;
447 inode->i_fop = &hfs_dbl_dir_operations;
448 HFS_I(inode)->file_type = HFS_DBL_NORM;
449 HFS_I(inode)->dir_size = 2;
456 * This function serves the same purpose as a read_inode() function does
457 * in other filesystems. It is called by __hfs_iget() to fill in
458 * the missing fields of an uninitialized inode under the Netatalk
461 void hfs_nat_ifill(struct inode * inode, ino_t type, const int version)
463 struct hfs_cat_entry *entry = HFS_I(inode)->entry;
465 HFS_I(inode)->d_drop_op = hfs_nat_drop_dentry;
466 if (type == HFS_NAT_HDR) {
467 if (entry->type == HFS_CDR_FIL) {
468 init_file_inode(inode, HFS_FK_RSRC);
469 inode->i_size += HFS_NAT_HDR_LEN;
471 inode->i_size = HFS_NAT_HDR_LEN;
472 inode->i_mode = S_IRUGO | S_IWUGO | S_IFREG;
475 inode->i_op = &hfs_hdr_inode_operations;
476 inode->i_fop = &hfs_hdr_operations;
477 HFS_I(inode)->default_layout = (version == 2) ?
478 &hfs_nat2_hdr_layout : &hfs_nat_hdr_layout;
479 } else if (entry->type == HFS_CDR_FIL) {
480 init_file_inode(inode, HFS_FK_DATA);
481 inode->i_op = &hfs_file_inode_operations;
482 inode->i_fop = &hfs_file_operations;
483 inode->i_mapping->a_ops = &hfs_aops;
484 inode->u.hfs_i.mmu_private = inode->i_size;
485 } else { /* Directory */
486 struct hfs_dir *hdir = &entry->u.dir;
489 inode->i_size = hdir->files + hdir->dirs + 4;
490 inode->i_mode = S_IRWXUGO | S_IFDIR;
491 HFS_I(inode)->dir_size = 1;
492 if (type == HFS_NAT_NDIR) {
493 inode->i_nlink = hdir->dirs + 3;
494 inode->i_op = &hfs_nat_ndir_inode_operations;
495 HFS_I(inode)->file_type = HFS_NAT_NORM;
496 } else if (type == HFS_NAT_HDIR) {
498 inode->i_op = &hfs_nat_hdir_inode_operations;
499 HFS_I(inode)->file_type = HFS_NAT_HDR;
501 inode->i_fop = &hfs_nat_dir_operations;