Linux-2.6.12-rc2
[linux-2.6/kvm.git] / fs / hfs / inode.c
blob75191232609454d67e0e126314c15ef782bdd398
1 /*
2 * linux/fs/hfs/inode.c
4 * Copyright (C) 1995-1997 Paul H. Hargrove
5 * (C) 2003 Ardis Technologies <roman@ardistech.com>
6 * This file may be distributed under the terms of the GNU General Public License.
8 * This file contains inode-related functions which do not depend on
9 * which scheme is being used to represent forks.
11 * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
14 #include <linux/pagemap.h>
15 #include <linux/version.h>
16 #include <linux/mpage.h>
18 #include "hfs_fs.h"
19 #include "btree.h"
21 static struct file_operations hfs_file_operations;
22 static struct inode_operations hfs_file_inode_operations;
24 /*================ Variable-like macros ================*/
26 #define HFS_VALID_MODE_BITS (S_IFREG | S_IFDIR | S_IRWXUGO)
28 static int hfs_writepage(struct page *page, struct writeback_control *wbc)
30 return block_write_full_page(page, hfs_get_block, wbc);
33 static int hfs_readpage(struct file *file, struct page *page)
35 return block_read_full_page(page, hfs_get_block);
38 static int hfs_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
40 return cont_prepare_write(page, from, to, hfs_get_block,
41 &HFS_I(page->mapping->host)->phys_size);
44 static sector_t hfs_bmap(struct address_space *mapping, sector_t block)
46 return generic_block_bmap(mapping, block, hfs_get_block);
49 static int hfs_releasepage(struct page *page, int mask)
51 struct inode *inode = page->mapping->host;
52 struct super_block *sb = inode->i_sb;
53 struct hfs_btree *tree;
54 struct hfs_bnode *node;
55 u32 nidx;
56 int i, res = 1;
58 switch (inode->i_ino) {
59 case HFS_EXT_CNID:
60 tree = HFS_SB(sb)->ext_tree;
61 break;
62 case HFS_CAT_CNID:
63 tree = HFS_SB(sb)->cat_tree;
64 break;
65 default:
66 BUG();
67 return 0;
69 if (tree->node_size >= PAGE_CACHE_SIZE) {
70 nidx = page->index >> (tree->node_size_shift - PAGE_CACHE_SHIFT);
71 spin_lock(&tree->hash_lock);
72 node = hfs_bnode_findhash(tree, nidx);
73 if (!node)
75 else if (atomic_read(&node->refcnt))
76 res = 0;
77 if (res && node) {
78 hfs_bnode_unhash(node);
79 hfs_bnode_free(node);
81 spin_unlock(&tree->hash_lock);
82 } else {
83 nidx = page->index << (PAGE_CACHE_SHIFT - tree->node_size_shift);
84 i = 1 << (PAGE_CACHE_SHIFT - tree->node_size_shift);
85 spin_lock(&tree->hash_lock);
86 do {
87 node = hfs_bnode_findhash(tree, nidx++);
88 if (!node)
89 continue;
90 if (atomic_read(&node->refcnt)) {
91 res = 0;
92 break;
94 hfs_bnode_unhash(node);
95 hfs_bnode_free(node);
96 } while (--i && nidx < tree->node_count);
97 spin_unlock(&tree->hash_lock);
99 //printk("releasepage: %lu,%x = %d\n", page->index, mask, res);
100 return res ? try_to_free_buffers(page) : 0;
103 static int hfs_get_blocks(struct inode *inode, sector_t iblock, unsigned long max_blocks,
104 struct buffer_head *bh_result, int create)
106 int ret;
108 ret = hfs_get_block(inode, iblock, bh_result, create);
109 if (!ret)
110 bh_result->b_size = (1 << inode->i_blkbits);
111 return ret;
114 static ssize_t hfs_direct_IO(int rw, struct kiocb *iocb,
115 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
117 struct file *file = iocb->ki_filp;
118 struct inode *inode = file->f_dentry->d_inode->i_mapping->host;
120 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
121 offset, nr_segs, hfs_get_blocks, NULL);
124 static int hfs_writepages(struct address_space *mapping,
125 struct writeback_control *wbc)
127 return mpage_writepages(mapping, wbc, hfs_get_block);
130 struct address_space_operations hfs_btree_aops = {
131 .readpage = hfs_readpage,
132 .writepage = hfs_writepage,
133 .sync_page = block_sync_page,
134 .prepare_write = hfs_prepare_write,
135 .commit_write = generic_commit_write,
136 .bmap = hfs_bmap,
137 .releasepage = hfs_releasepage,
140 struct address_space_operations hfs_aops = {
141 .readpage = hfs_readpage,
142 .writepage = hfs_writepage,
143 .sync_page = block_sync_page,
144 .prepare_write = hfs_prepare_write,
145 .commit_write = generic_commit_write,
146 .bmap = hfs_bmap,
147 .direct_IO = hfs_direct_IO,
148 .writepages = hfs_writepages,
152 * hfs_new_inode
154 struct inode *hfs_new_inode(struct inode *dir, struct qstr *name, int mode)
156 struct super_block *sb = dir->i_sb;
157 struct inode *inode = new_inode(sb);
158 if (!inode)
159 return NULL;
161 init_MUTEX(&HFS_I(inode)->extents_lock);
162 INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list);
163 hfs_cat_build_key((btree_key *)&HFS_I(inode)->cat_key, dir->i_ino, name);
164 inode->i_ino = HFS_SB(sb)->next_id++;
165 inode->i_mode = mode;
166 inode->i_uid = current->fsuid;
167 inode->i_gid = current->fsgid;
168 inode->i_nlink = 1;
169 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
170 inode->i_blksize = HFS_SB(sb)->alloc_blksz;
171 HFS_I(inode)->flags = 0;
172 HFS_I(inode)->rsrc_inode = NULL;
173 HFS_I(inode)->fs_blocks = 0;
174 if (S_ISDIR(mode)) {
175 inode->i_size = 2;
176 HFS_SB(sb)->folder_count++;
177 if (dir->i_ino == HFS_ROOT_CNID)
178 HFS_SB(sb)->root_dirs++;
179 inode->i_op = &hfs_dir_inode_operations;
180 inode->i_fop = &hfs_dir_operations;
181 inode->i_mode |= S_IRWXUGO;
182 inode->i_mode &= ~HFS_SB(inode->i_sb)->s_dir_umask;
183 } else if (S_ISREG(mode)) {
184 HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks;
185 HFS_SB(sb)->file_count++;
186 if (dir->i_ino == HFS_ROOT_CNID)
187 HFS_SB(sb)->root_files++;
188 inode->i_op = &hfs_file_inode_operations;
189 inode->i_fop = &hfs_file_operations;
190 inode->i_mapping->a_ops = &hfs_aops;
191 inode->i_mode |= S_IRUGO|S_IXUGO;
192 if (mode & S_IWUSR)
193 inode->i_mode |= S_IWUGO;
194 inode->i_mode &= ~HFS_SB(inode->i_sb)->s_file_umask;
195 HFS_I(inode)->phys_size = 0;
196 HFS_I(inode)->alloc_blocks = 0;
197 HFS_I(inode)->first_blocks = 0;
198 HFS_I(inode)->cached_start = 0;
199 HFS_I(inode)->cached_blocks = 0;
200 memset(HFS_I(inode)->first_extents, 0, sizeof(hfs_extent_rec));
201 memset(HFS_I(inode)->cached_extents, 0, sizeof(hfs_extent_rec));
203 insert_inode_hash(inode);
204 mark_inode_dirty(inode);
205 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
206 sb->s_dirt = 1;
208 return inode;
211 void hfs_delete_inode(struct inode *inode)
213 struct super_block *sb = inode->i_sb;
215 dprint(DBG_INODE, "delete_inode: %lu\n", inode->i_ino);
216 if (S_ISDIR(inode->i_mode)) {
217 HFS_SB(sb)->folder_count--;
218 if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID))
219 HFS_SB(sb)->root_dirs--;
220 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
221 sb->s_dirt = 1;
222 return;
224 HFS_SB(sb)->file_count--;
225 if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID))
226 HFS_SB(sb)->root_files--;
227 if (S_ISREG(inode->i_mode)) {
228 if (!inode->i_nlink) {
229 inode->i_size = 0;
230 hfs_file_truncate(inode);
233 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
234 sb->s_dirt = 1;
237 void hfs_inode_read_fork(struct inode *inode, struct hfs_extent *ext,
238 __be32 __log_size, __be32 phys_size, u32 clump_size)
240 struct super_block *sb = inode->i_sb;
241 u32 log_size = be32_to_cpu(__log_size);
242 u16 count;
243 int i;
245 memcpy(HFS_I(inode)->first_extents, ext, sizeof(hfs_extent_rec));
246 for (count = 0, i = 0; i < 3; i++)
247 count += be16_to_cpu(ext[i].count);
248 HFS_I(inode)->first_blocks = count;
250 inode->i_size = HFS_I(inode)->phys_size = log_size;
251 HFS_I(inode)->fs_blocks = (log_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
252 inode_set_bytes(inode, HFS_I(inode)->fs_blocks << sb->s_blocksize_bits);
253 HFS_I(inode)->alloc_blocks = be32_to_cpu(phys_size) /
254 HFS_SB(sb)->alloc_blksz;
255 HFS_I(inode)->clump_blocks = clump_size / HFS_SB(sb)->alloc_blksz;
256 if (!HFS_I(inode)->clump_blocks)
257 HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks;
260 struct hfs_iget_data {
261 struct hfs_cat_key *key;
262 hfs_cat_rec *rec;
265 static int hfs_test_inode(struct inode *inode, void *data)
267 struct hfs_iget_data *idata = data;
268 hfs_cat_rec *rec;
270 rec = idata->rec;
271 switch (rec->type) {
272 case HFS_CDR_DIR:
273 return inode->i_ino == be32_to_cpu(rec->dir.DirID);
274 case HFS_CDR_FIL:
275 return inode->i_ino == be32_to_cpu(rec->file.FlNum);
276 default:
277 BUG();
278 return 1;
283 * hfs_read_inode
285 static int hfs_read_inode(struct inode *inode, void *data)
287 struct hfs_iget_data *idata = data;
288 struct hfs_sb_info *hsb = HFS_SB(inode->i_sb);
289 hfs_cat_rec *rec;
291 HFS_I(inode)->flags = 0;
292 HFS_I(inode)->rsrc_inode = NULL;
293 init_MUTEX(&HFS_I(inode)->extents_lock);
294 INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list);
296 /* Initialize the inode */
297 inode->i_uid = hsb->s_uid;
298 inode->i_gid = hsb->s_gid;
299 inode->i_nlink = 1;
300 inode->i_blksize = HFS_SB(inode->i_sb)->alloc_blksz;
302 if (idata->key)
303 HFS_I(inode)->cat_key = *idata->key;
304 else
305 HFS_I(inode)->flags |= HFS_FLG_RSRC;
306 HFS_I(inode)->tz_secondswest = sys_tz.tz_minuteswest * 60;
308 rec = idata->rec;
309 switch (rec->type) {
310 case HFS_CDR_FIL:
311 if (!HFS_IS_RSRC(inode)) {
312 hfs_inode_read_fork(inode, rec->file.ExtRec, rec->file.LgLen,
313 rec->file.PyLen, be16_to_cpu(rec->file.ClpSize));
314 } else {
315 hfs_inode_read_fork(inode, rec->file.RExtRec, rec->file.RLgLen,
316 rec->file.RPyLen, be16_to_cpu(rec->file.ClpSize));
319 inode->i_ino = be32_to_cpu(rec->file.FlNum);
320 inode->i_mode = S_IRUGO | S_IXUGO;
321 if (!(rec->file.Flags & HFS_FIL_LOCK))
322 inode->i_mode |= S_IWUGO;
323 inode->i_mode &= ~hsb->s_file_umask;
324 inode->i_mode |= S_IFREG;
325 inode->i_ctime = inode->i_atime = inode->i_mtime =
326 hfs_m_to_utime(rec->file.MdDat);
327 inode->i_op = &hfs_file_inode_operations;
328 inode->i_fop = &hfs_file_operations;
329 inode->i_mapping->a_ops = &hfs_aops;
330 break;
331 case HFS_CDR_DIR:
332 inode->i_ino = be32_to_cpu(rec->dir.DirID);
333 inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
334 HFS_I(inode)->fs_blocks = 0;
335 inode->i_mode = S_IFDIR | (S_IRWXUGO & ~hsb->s_dir_umask);
336 inode->i_ctime = inode->i_atime = inode->i_mtime =
337 hfs_m_to_utime(rec->dir.MdDat);
338 inode->i_op = &hfs_dir_inode_operations;
339 inode->i_fop = &hfs_dir_operations;
340 break;
341 default:
342 make_bad_inode(inode);
344 return 0;
348 * __hfs_iget()
350 * Given the MDB for a HFS filesystem, a 'key' and an 'entry' in
351 * the catalog B-tree and the 'type' of the desired file return the
352 * inode for that file/directory or NULL. Note that 'type' indicates
353 * whether we want the actual file or directory, or the corresponding
354 * metadata (AppleDouble header file or CAP metadata file).
356 struct inode *hfs_iget(struct super_block *sb, struct hfs_cat_key *key, hfs_cat_rec *rec)
358 struct hfs_iget_data data = { key, rec };
359 struct inode *inode;
360 u32 cnid;
362 switch (rec->type) {
363 case HFS_CDR_DIR:
364 cnid = be32_to_cpu(rec->dir.DirID);
365 break;
366 case HFS_CDR_FIL:
367 cnid = be32_to_cpu(rec->file.FlNum);
368 break;
369 default:
370 return NULL;
372 inode = iget5_locked(sb, cnid, hfs_test_inode, hfs_read_inode, &data);
373 if (inode && (inode->i_state & I_NEW))
374 unlock_new_inode(inode);
375 return inode;
378 void hfs_inode_write_fork(struct inode *inode, struct hfs_extent *ext,
379 __be32 *log_size, __be32 *phys_size)
381 memcpy(ext, HFS_I(inode)->first_extents, sizeof(hfs_extent_rec));
383 if (log_size)
384 *log_size = cpu_to_be32(inode->i_size);
385 if (phys_size)
386 *phys_size = cpu_to_be32(HFS_I(inode)->alloc_blocks *
387 HFS_SB(inode->i_sb)->alloc_blksz);
390 int hfs_write_inode(struct inode *inode, int unused)
392 struct inode *main_inode = inode;
393 struct hfs_find_data fd;
394 hfs_cat_rec rec;
396 dprint(DBG_INODE, "hfs_write_inode: %lu\n", inode->i_ino);
397 hfs_ext_write_extent(inode);
399 if (inode->i_ino < HFS_FIRSTUSER_CNID) {
400 switch (inode->i_ino) {
401 case HFS_ROOT_CNID:
402 break;
403 case HFS_EXT_CNID:
404 hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);
405 return 0;
406 case HFS_CAT_CNID:
407 hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
408 return 0;
409 default:
410 BUG();
411 return -EIO;
415 if (HFS_IS_RSRC(inode))
416 main_inode = HFS_I(inode)->rsrc_inode;
418 if (!main_inode->i_nlink)
419 return 0;
421 if (hfs_find_init(HFS_SB(main_inode->i_sb)->cat_tree, &fd))
422 /* panic? */
423 return -EIO;
425 fd.search_key->cat = HFS_I(main_inode)->cat_key;
426 if (hfs_brec_find(&fd))
427 /* panic? */
428 goto out;
430 if (S_ISDIR(main_inode->i_mode)) {
431 if (fd.entrylength < sizeof(struct hfs_cat_dir))
432 /* panic? */;
433 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
434 sizeof(struct hfs_cat_dir));
435 if (rec.type != HFS_CDR_DIR ||
436 be32_to_cpu(rec.dir.DirID) != inode->i_ino) {
439 rec.dir.MdDat = hfs_u_to_mtime(inode->i_mtime);
440 rec.dir.Val = cpu_to_be16(inode->i_size - 2);
442 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
443 sizeof(struct hfs_cat_dir));
444 } else if (HFS_IS_RSRC(inode)) {
445 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
446 sizeof(struct hfs_cat_file));
447 hfs_inode_write_fork(inode, rec.file.RExtRec,
448 &rec.file.RLgLen, &rec.file.RPyLen);
449 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
450 sizeof(struct hfs_cat_file));
451 } else {
452 if (fd.entrylength < sizeof(struct hfs_cat_file))
453 /* panic? */;
454 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
455 sizeof(struct hfs_cat_file));
456 if (rec.type != HFS_CDR_FIL ||
457 be32_to_cpu(rec.file.FlNum) != inode->i_ino) {
460 if (inode->i_mode & S_IWUSR)
461 rec.file.Flags &= ~HFS_FIL_LOCK;
462 else
463 rec.file.Flags |= HFS_FIL_LOCK;
464 hfs_inode_write_fork(inode, rec.file.ExtRec, &rec.file.LgLen, &rec.file.PyLen);
465 rec.file.MdDat = hfs_u_to_mtime(inode->i_mtime);
467 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
468 sizeof(struct hfs_cat_file));
470 out:
471 hfs_find_exit(&fd);
472 return 0;
475 static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry,
476 struct nameidata *nd)
478 struct inode *inode = NULL;
479 hfs_cat_rec rec;
480 struct hfs_find_data fd;
481 int res;
483 if (HFS_IS_RSRC(dir) || strcmp(dentry->d_name.name, "rsrc"))
484 goto out;
486 inode = HFS_I(dir)->rsrc_inode;
487 if (inode)
488 goto out;
490 inode = new_inode(dir->i_sb);
491 if (!inode)
492 return ERR_PTR(-ENOMEM);
494 hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd);
495 fd.search_key->cat = HFS_I(dir)->cat_key;
496 res = hfs_brec_read(&fd, &rec, sizeof(rec));
497 if (!res) {
498 struct hfs_iget_data idata = { NULL, &rec };
499 hfs_read_inode(inode, &idata);
501 hfs_find_exit(&fd);
502 if (res) {
503 iput(inode);
504 return ERR_PTR(res);
506 HFS_I(inode)->rsrc_inode = dir;
507 HFS_I(dir)->rsrc_inode = inode;
508 igrab(dir);
509 hlist_add_head(&inode->i_hash, &HFS_SB(dir->i_sb)->rsrc_inodes);
510 mark_inode_dirty(inode);
511 out:
512 d_add(dentry, inode);
513 return NULL;
516 void hfs_clear_inode(struct inode *inode)
518 if (HFS_IS_RSRC(inode) && HFS_I(inode)->rsrc_inode) {
519 HFS_I(HFS_I(inode)->rsrc_inode)->rsrc_inode = NULL;
520 iput(HFS_I(inode)->rsrc_inode);
524 static int hfs_permission(struct inode *inode, int mask,
525 struct nameidata *nd)
527 if (S_ISREG(inode->i_mode) && mask & MAY_EXEC)
528 return 0;
529 return generic_permission(inode, mask, NULL);
532 static int hfs_file_open(struct inode *inode, struct file *file)
534 if (HFS_IS_RSRC(inode))
535 inode = HFS_I(inode)->rsrc_inode;
536 if (atomic_read(&file->f_count) != 1)
537 return 0;
538 atomic_inc(&HFS_I(inode)->opencnt);
539 return 0;
542 static int hfs_file_release(struct inode *inode, struct file *file)
544 //struct super_block *sb = inode->i_sb;
546 if (HFS_IS_RSRC(inode))
547 inode = HFS_I(inode)->rsrc_inode;
548 if (atomic_read(&file->f_count) != 0)
549 return 0;
550 if (atomic_dec_and_test(&HFS_I(inode)->opencnt)) {
551 down(&inode->i_sem);
552 hfs_file_truncate(inode);
553 //if (inode->i_flags & S_DEAD) {
554 // hfs_delete_cat(inode->i_ino, HFSPLUS_SB(sb).hidden_dir, NULL);
555 // hfs_delete_inode(inode);
557 up(&inode->i_sem);
559 return 0;
563 * hfs_notify_change()
565 * Based very closely on fs/msdos/inode.c by Werner Almesberger
567 * This is the notify_change() field in the super_operations structure
568 * for HFS file systems. The purpose is to take that changes made to
569 * an inode and apply then in a filesystem-dependent manner. In this
570 * case the process has a few of tasks to do:
571 * 1) prevent changes to the i_uid and i_gid fields.
572 * 2) map file permissions to the closest allowable permissions
573 * 3) Since multiple Linux files can share the same on-disk inode under
574 * HFS (for instance the data and resource forks of a file) a change
575 * to permissions must be applied to all other in-core inodes which
576 * correspond to the same HFS file.
579 int hfs_inode_setattr(struct dentry *dentry, struct iattr * attr)
581 struct inode *inode = dentry->d_inode;
582 struct hfs_sb_info *hsb = HFS_SB(inode->i_sb);
583 int error;
585 error = inode_change_ok(inode, attr); /* basic permission checks */
586 if (error)
587 return error;
589 /* no uig/gid changes and limit which mode bits can be set */
590 if (((attr->ia_valid & ATTR_UID) &&
591 (attr->ia_uid != hsb->s_uid)) ||
592 ((attr->ia_valid & ATTR_GID) &&
593 (attr->ia_gid != hsb->s_gid)) ||
594 ((attr->ia_valid & ATTR_MODE) &&
595 ((S_ISDIR(inode->i_mode) &&
596 (attr->ia_mode != inode->i_mode)) ||
597 (attr->ia_mode & ~HFS_VALID_MODE_BITS)))) {
598 return hsb->s_quiet ? 0 : error;
601 if (attr->ia_valid & ATTR_MODE) {
602 /* Only the 'w' bits can ever change and only all together. */
603 if (attr->ia_mode & S_IWUSR)
604 attr->ia_mode = inode->i_mode | S_IWUGO;
605 else
606 attr->ia_mode = inode->i_mode & ~S_IWUGO;
607 attr->ia_mode &= S_ISDIR(inode->i_mode) ? ~hsb->s_dir_umask: ~hsb->s_file_umask;
609 error = inode_setattr(inode, attr);
610 if (error)
611 return error;
613 return 0;
617 static struct file_operations hfs_file_operations = {
618 .llseek = generic_file_llseek,
619 .read = generic_file_read,
620 .write = generic_file_write,
621 .mmap = generic_file_mmap,
622 .sendfile = generic_file_sendfile,
623 .fsync = file_fsync,
624 .open = hfs_file_open,
625 .release = hfs_file_release,
628 static struct inode_operations hfs_file_inode_operations = {
629 .lookup = hfs_file_lookup,
630 .truncate = hfs_file_truncate,
631 .setattr = hfs_inode_setattr,
632 .permission = hfs_permission,
633 .setxattr = hfs_setxattr,
634 .getxattr = hfs_getxattr,
635 .listxattr = hfs_listxattr,