Import 2.4.0-test2pre6
[davej-history.git] / fs / hfs / inode.c
blob371538b9d26bc9e0ab88c55914e3a5246f2afbf6
1 /*
2 * linux/fs/hfs/inode.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 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.
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 /*================ Variable-like macros ================*/
26 #define HFS_VALID_MODE_BITS (S_IFREG | S_IFDIR | S_IRWXUGO)
28 /*================ File-local functions ================*/
31 * init_file_inode()
33 * Given an HFS catalog entry initialize an inode for a file.
35 static void init_file_inode(struct inode *inode, hfs_u8 fork)
37 struct hfs_fork *fk;
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;
42 } else {
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" */
55 #else
56 HFS_I(inode)->convert = 0;
57 #endif
58 fk = &entry->u.file.data_fork;
59 } else {
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;
66 inode->i_nlink = 1;
69 /*================ Global functions ================*/
72 * hfs_put_inode()
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;
82 hfs_cat_put(entry);
83 if (atomic_read(&inode->i_count) == 1) {
84 struct hfs_hdr_layout *tmp = HFS_I(inode)->layout;
86 if (tmp) {
87 HFS_I(inode)->layout = NULL;
88 HFS_DELETE(tmp);
94 * hfs_notify_change()
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);
117 int error, i;
119 error = inode_change_ok(inode, attr); /* basic permission checks */
120 if (error) {
121 /* Let netatalk's afpd think chmod() always succeeds */
122 if (hsb->s_afpd &&
123 (attr->ia_valid == (ATTR_MODE | ATTR_CTIME))) {
124 return 0;
125 } else {
126 return error;
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;
148 } else {
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) {
187 hfs_u8 new_flags;
189 if (inode->i_mode & S_IWUSR) {
190 new_flags = entry->u.file.flags & ~HFS_FIL_LOCK;
191 } else {
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() */
202 return 0;
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 file *file, struct page *page)
222 return block_write_full_page(page,hfs_get_block);
224 static int hfs_readpage(struct file *file, struct page *page)
226 return block_read_full_page(page,hfs_get_block);
228 static int hfs_prepare_write(struct file *file, 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 sync_page: block_sync_page,
241 prepare_write: hfs_prepare_write,
242 commit_write: generic_commit_write,
243 bmap: hfs_bmap
247 * __hfs_iget()
249 * Given the MDB for a HFS filesystem, a 'key' and an 'entry' in
250 * the catalog B-tree and the 'type' of the desired file return the
251 * inode for that file/directory or NULL. Note that 'type' indicates
252 * whether we want the actual file or directory, or the corresponding
253 * metadata (AppleDouble header file or CAP metadata file).
255 * In an ideal world we could call iget() and would not need this
256 * function. However, since there is no way to even know the inode
257 * number until we've found the file/directory in the catalog B-tree
258 * that simply won't happen.
260 * The main idea here is to look in the catalog B-tree to get the
261 * vital info about the file or directory (including the file id which
262 * becomes the inode number) and then to call iget() and return the
263 * inode if it is complete. If it is not then we use the catalog
264 * entry to fill in the missing info, by calling the appropriate
265 * 'fillin' function. Note that these fillin functions are
266 * essentially hfs_*_read_inode() functions, but since there is no way
267 * to pass the catalog entry through iget() to such a read_inode()
268 * function, we have to call them after iget() returns an incomplete
269 * inode to us. This is pretty much the same problem faced in the NFS
270 * code, and pretty much the same solution. The SMB filesystem deals
271 * with this in a different way: by using the address of the
272 * kmalloc()'d space which holds the data as the inode number.
274 * XXX: Both this function and NFS's corresponding nfs_fhget() would
275 * benefit from a way to pass an additional (void *) through iget() to
276 * the VFS read_inode() function.
278 * this will hfs_cat_put() the entry if it fails.
280 struct inode *hfs_iget(struct hfs_cat_entry *entry, ino_t type,
281 struct dentry *dentry)
283 struct dentry **sys_entry;
284 struct super_block *sb;
285 struct inode *inode;
287 if (!entry) {
288 return NULL;
291 /* If there are several processes all calling __iget() for
292 the same inode then they will all get the same one back.
293 The first one to return from __iget() will notice that the
294 i_mode field of the inode is blank and KNOW that it is
295 the first to return. Therefore, it will set the appropriate
296 'sys_entry' field in the entry and initialize the inode.
297 All the initialization must be done without sleeping,
298 or else other processes could end up using a partially
299 initialized inode. */
301 sb = entry->mdb->sys_mdb;
302 sys_entry = &entry->sys_entry[HFS_ITYPE_TO_INT(type)];
304 if (!(inode = iget(sb, ntohl(entry->cnid) | type))) {
305 hfs_cat_put(entry);
306 return NULL;
309 if (inode->i_dev != sb->s_dev) {
310 iput(inode); /* automatically does an hfs_cat_put */
311 inode = NULL;
312 } else if (!inode->i_mode || (*sys_entry == NULL)) {
313 /* Initialize the inode */
314 struct hfs_sb_info *hsb = HFS_SB(sb);
316 inode->i_rdev = 0;
317 inode->i_ctime = inode->i_atime = inode->i_mtime =
318 hfs_m_to_utime(entry->modify_date);
319 inode->i_blksize = HFS_SECTOR_SIZE;
320 inode->i_uid = hsb->s_uid;
321 inode->i_gid = hsb->s_gid;
323 memset(HFS_I(inode), 0, sizeof(struct hfs_inode_info));
324 HFS_I(inode)->magic = HFS_INO_MAGIC;
325 HFS_I(inode)->entry = entry;
326 HFS_I(inode)->tz_secondswest = hfs_to_utc(0);
328 hsb->s_ifill(inode, type, hsb->s_version);
329 if (!hsb->s_afpd && (entry->type == HFS_CDR_FIL) &&
330 (entry->u.file.flags & HFS_FIL_LOCK)) {
331 inode->i_mode &= ~S_IWUGO;
333 inode->i_mode &= ~hsb->s_umask;
335 if (!inode->i_mode) {
336 iput(inode); /* does an hfs_cat_put */
337 inode = NULL;
338 } else
339 *sys_entry = dentry; /* cache dentry */
343 return inode;
346 /*================ Scheme-specific functions ================*/
349 * hfs_cap_ifill()
351 * This function serves the same purpose as a read_inode() function does
352 * in other filesystems. It is called by __hfs_iget() to fill in
353 * the missing fields of an uninitialized inode under the CAP scheme.
355 void hfs_cap_ifill(struct inode * inode, ino_t type, const int version)
357 struct hfs_cat_entry *entry = HFS_I(inode)->entry;
359 HFS_I(inode)->d_drop_op = hfs_cap_drop_dentry;
360 if (type == HFS_CAP_FNDR) {
361 inode->i_size = sizeof(struct hfs_cap_info);
362 inode->i_blocks = 0;
363 inode->i_nlink = 1;
364 inode->i_mode = S_IRUGO | S_IWUGO | S_IFREG;
365 inode->i_op = &hfs_cap_info_inode_operations;
366 inode->i_fop = &hfs_cap_info_operations;
367 } else if (entry->type == HFS_CDR_FIL) {
368 init_file_inode(inode, (type == HFS_CAP_DATA) ?
369 HFS_FK_DATA : HFS_FK_RSRC);
370 inode->i_op = &hfs_file_inode_operations;
371 inode->i_fop = &hfs_file_operations;
372 inode->i_mapping->a_ops = &hfs_aops;
373 inode->u.hfs_i.mmu_private = inode->i_size;
374 } else { /* Directory */
375 struct hfs_dir *hdir = &entry->u.dir;
377 inode->i_blocks = 0;
378 inode->i_size = hdir->files + hdir->dirs + 5;
379 HFS_I(inode)->dir_size = 1;
380 if (type == HFS_CAP_NDIR) {
381 inode->i_mode = S_IRWXUGO | S_IFDIR;
382 inode->i_nlink = hdir->dirs + 4;
383 inode->i_op = &hfs_cap_ndir_inode_operations;
384 inode->i_fop = &hfs_cap_dir_operations;
385 HFS_I(inode)->file_type = HFS_CAP_NORM;
386 } else if (type == HFS_CAP_FDIR) {
387 inode->i_mode = S_IRUGO | S_IXUGO | S_IFDIR;
388 inode->i_nlink = 2;
389 inode->i_op = &hfs_cap_fdir_inode_operations;
390 inode->i_fop = &hfs_cap_dir_operations;
391 HFS_I(inode)->file_type = HFS_CAP_FNDR;
392 } else if (type == HFS_CAP_RDIR) {
393 inode->i_mode = S_IRUGO | S_IXUGO | S_IFDIR;
394 inode->i_nlink = 2;
395 inode->i_op = &hfs_cap_rdir_inode_operations;
396 inode->i_fop = &hfs_cap_dir_operations;
397 HFS_I(inode)->file_type = HFS_CAP_RSRC;
403 * hfs_dbl_ifill()
405 * This function serves the same purpose as a read_inode() function does
406 * in other filesystems. It is called by __hfs_iget() to fill in
407 * the missing fields of an uninitialized inode under the AppleDouble
408 * scheme.
410 void hfs_dbl_ifill(struct inode * inode, ino_t type, const int version)
412 struct hfs_cat_entry *entry = HFS_I(inode)->entry;
414 HFS_I(inode)->d_drop_op = hfs_dbl_drop_dentry;
415 if (type == HFS_DBL_HDR) {
416 if (entry->type == HFS_CDR_FIL) {
417 init_file_inode(inode, HFS_FK_RSRC);
418 inode->i_size += HFS_DBL_HDR_LEN;
419 HFS_I(inode)->default_layout = &hfs_dbl_fil_hdr_layout;
420 } else {
421 inode->i_size = HFS_DBL_HDR_LEN;
422 inode->i_mode = S_IRUGO | S_IWUGO | S_IFREG;
423 inode->i_nlink = 1;
424 HFS_I(inode)->default_layout = &hfs_dbl_dir_hdr_layout;
426 inode->i_op = &hfs_hdr_inode_operations;
427 inode->i_fop = &hfs_hdr_operations;
428 } else if (entry->type == HFS_CDR_FIL) {
429 init_file_inode(inode, HFS_FK_DATA);
430 inode->i_op = &hfs_file_inode_operations;
431 inode->i_fop = &hfs_file_operations;
432 inode->i_mapping->a_ops = &hfs_aops;
433 inode->u.hfs_i.mmu_private = inode->i_size;
434 } else { /* Directory */
435 struct hfs_dir *hdir = &entry->u.dir;
437 inode->i_blocks = 0;
438 inode->i_nlink = hdir->dirs + 2;
439 inode->i_size = 3 + 2 * (hdir->dirs + hdir->files);
440 inode->i_mode = S_IRWXUGO | S_IFDIR;
441 inode->i_op = &hfs_dbl_dir_inode_operations;
442 inode->i_fop = &hfs_dbl_dir_operations;
443 HFS_I(inode)->file_type = HFS_DBL_NORM;
444 HFS_I(inode)->dir_size = 2;
449 * hfs_nat_ifill()
451 * This function serves the same purpose as a read_inode() function does
452 * in other filesystems. It is called by __hfs_iget() to fill in
453 * the missing fields of an uninitialized inode under the Netatalk
454 * scheme.
456 void hfs_nat_ifill(struct inode * inode, ino_t type, const int version)
458 struct hfs_cat_entry *entry = HFS_I(inode)->entry;
460 HFS_I(inode)->d_drop_op = hfs_nat_drop_dentry;
461 if (type == HFS_NAT_HDR) {
462 if (entry->type == HFS_CDR_FIL) {
463 init_file_inode(inode, HFS_FK_RSRC);
464 inode->i_size += HFS_NAT_HDR_LEN;
465 } else {
466 inode->i_size = HFS_NAT_HDR_LEN;
467 inode->i_mode = S_IRUGO | S_IWUGO | S_IFREG;
468 inode->i_nlink = 1;
470 inode->i_op = &hfs_hdr_inode_operations;
471 inode->i_fop = &hfs_hdr_operations;
472 HFS_I(inode)->default_layout = (version == 2) ?
473 &hfs_nat2_hdr_layout : &hfs_nat_hdr_layout;
474 } else if (entry->type == HFS_CDR_FIL) {
475 init_file_inode(inode, HFS_FK_DATA);
476 inode->i_op = &hfs_file_inode_operations;
477 inode->i_fop = &hfs_file_operations;
478 inode->i_mapping->a_ops = &hfs_aops;
479 inode->u.hfs_i.mmu_private = inode->i_size;
480 } else { /* Directory */
481 struct hfs_dir *hdir = &entry->u.dir;
483 inode->i_blocks = 0;
484 inode->i_size = hdir->files + hdir->dirs + 4;
485 inode->i_mode = S_IRWXUGO | S_IFDIR;
486 HFS_I(inode)->dir_size = 1;
487 if (type == HFS_NAT_NDIR) {
488 inode->i_nlink = hdir->dirs + 3;
489 inode->i_op = &hfs_nat_ndir_inode_operations;
490 HFS_I(inode)->file_type = HFS_NAT_NORM;
491 } else if (type == HFS_NAT_HDIR) {
492 inode->i_nlink = 2;
493 inode->i_op = &hfs_nat_hdir_inode_operations;
494 HFS_I(inode)->file_type = HFS_NAT_HDR;
496 inode->i_fop = &hfs_nat_dir_operations;