OMAP3 SRF: Generic shared resource f/w
[linux-ginger.git] / fs / hfs / inode.c
bloba1cbff2b4d99066718f9dd0a44c86cd4ec20de48
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/mpage.h>
16 #include <linux/sched.h>
18 #include "hfs_fs.h"
19 #include "btree.h"
21 static const struct file_operations hfs_file_operations;
22 static const 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_write_begin(struct file *file, struct address_space *mapping,
39 loff_t pos, unsigned len, unsigned flags,
40 struct page **pagep, void **fsdata)
42 *pagep = NULL;
43 return cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
44 hfs_get_block,
45 &HFS_I(mapping->host)->phys_size);
48 static sector_t hfs_bmap(struct address_space *mapping, sector_t block)
50 return generic_block_bmap(mapping, block, hfs_get_block);
53 static int hfs_releasepage(struct page *page, gfp_t mask)
55 struct inode *inode = page->mapping->host;
56 struct super_block *sb = inode->i_sb;
57 struct hfs_btree *tree;
58 struct hfs_bnode *node;
59 u32 nidx;
60 int i, res = 1;
62 switch (inode->i_ino) {
63 case HFS_EXT_CNID:
64 tree = HFS_SB(sb)->ext_tree;
65 break;
66 case HFS_CAT_CNID:
67 tree = HFS_SB(sb)->cat_tree;
68 break;
69 default:
70 BUG();
71 return 0;
74 if (!tree)
75 return 0;
77 if (tree->node_size >= PAGE_CACHE_SIZE) {
78 nidx = page->index >> (tree->node_size_shift - PAGE_CACHE_SHIFT);
79 spin_lock(&tree->hash_lock);
80 node = hfs_bnode_findhash(tree, nidx);
81 if (!node)
83 else if (atomic_read(&node->refcnt))
84 res = 0;
85 if (res && node) {
86 hfs_bnode_unhash(node);
87 hfs_bnode_free(node);
89 spin_unlock(&tree->hash_lock);
90 } else {
91 nidx = page->index << (PAGE_CACHE_SHIFT - tree->node_size_shift);
92 i = 1 << (PAGE_CACHE_SHIFT - tree->node_size_shift);
93 spin_lock(&tree->hash_lock);
94 do {
95 node = hfs_bnode_findhash(tree, nidx++);
96 if (!node)
97 continue;
98 if (atomic_read(&node->refcnt)) {
99 res = 0;
100 break;
102 hfs_bnode_unhash(node);
103 hfs_bnode_free(node);
104 } while (--i && nidx < tree->node_count);
105 spin_unlock(&tree->hash_lock);
107 return res ? try_to_free_buffers(page) : 0;
110 static ssize_t hfs_direct_IO(int rw, struct kiocb *iocb,
111 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
113 struct file *file = iocb->ki_filp;
114 struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host;
116 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
117 offset, nr_segs, hfs_get_block, NULL);
120 static int hfs_writepages(struct address_space *mapping,
121 struct writeback_control *wbc)
123 return mpage_writepages(mapping, wbc, hfs_get_block);
126 const struct address_space_operations hfs_btree_aops = {
127 .readpage = hfs_readpage,
128 .writepage = hfs_writepage,
129 .sync_page = block_sync_page,
130 .write_begin = hfs_write_begin,
131 .write_end = generic_write_end,
132 .bmap = hfs_bmap,
133 .releasepage = hfs_releasepage,
136 const struct address_space_operations hfs_aops = {
137 .readpage = hfs_readpage,
138 .writepage = hfs_writepage,
139 .sync_page = block_sync_page,
140 .write_begin = hfs_write_begin,
141 .write_end = generic_write_end,
142 .bmap = hfs_bmap,
143 .direct_IO = hfs_direct_IO,
144 .writepages = hfs_writepages,
148 * hfs_new_inode
150 struct inode *hfs_new_inode(struct inode *dir, struct qstr *name, int mode)
152 struct super_block *sb = dir->i_sb;
153 struct inode *inode = new_inode(sb);
154 if (!inode)
155 return NULL;
157 mutex_init(&HFS_I(inode)->extents_lock);
158 INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list);
159 hfs_cat_build_key(sb, (btree_key *)&HFS_I(inode)->cat_key, dir->i_ino, name);
160 inode->i_ino = HFS_SB(sb)->next_id++;
161 inode->i_mode = mode;
162 inode->i_uid = current_fsuid();
163 inode->i_gid = current_fsgid();
164 inode->i_nlink = 1;
165 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
166 HFS_I(inode)->flags = 0;
167 HFS_I(inode)->rsrc_inode = NULL;
168 HFS_I(inode)->fs_blocks = 0;
169 if (S_ISDIR(mode)) {
170 inode->i_size = 2;
171 HFS_SB(sb)->folder_count++;
172 if (dir->i_ino == HFS_ROOT_CNID)
173 HFS_SB(sb)->root_dirs++;
174 inode->i_op = &hfs_dir_inode_operations;
175 inode->i_fop = &hfs_dir_operations;
176 inode->i_mode |= S_IRWXUGO;
177 inode->i_mode &= ~HFS_SB(inode->i_sb)->s_dir_umask;
178 } else if (S_ISREG(mode)) {
179 HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks;
180 HFS_SB(sb)->file_count++;
181 if (dir->i_ino == HFS_ROOT_CNID)
182 HFS_SB(sb)->root_files++;
183 inode->i_op = &hfs_file_inode_operations;
184 inode->i_fop = &hfs_file_operations;
185 inode->i_mapping->a_ops = &hfs_aops;
186 inode->i_mode |= S_IRUGO|S_IXUGO;
187 if (mode & S_IWUSR)
188 inode->i_mode |= S_IWUGO;
189 inode->i_mode &= ~HFS_SB(inode->i_sb)->s_file_umask;
190 HFS_I(inode)->phys_size = 0;
191 HFS_I(inode)->alloc_blocks = 0;
192 HFS_I(inode)->first_blocks = 0;
193 HFS_I(inode)->cached_start = 0;
194 HFS_I(inode)->cached_blocks = 0;
195 memset(HFS_I(inode)->first_extents, 0, sizeof(hfs_extent_rec));
196 memset(HFS_I(inode)->cached_extents, 0, sizeof(hfs_extent_rec));
198 insert_inode_hash(inode);
199 mark_inode_dirty(inode);
200 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
201 sb->s_dirt = 1;
203 return inode;
206 void hfs_delete_inode(struct inode *inode)
208 struct super_block *sb = inode->i_sb;
210 dprint(DBG_INODE, "delete_inode: %lu\n", inode->i_ino);
211 if (S_ISDIR(inode->i_mode)) {
212 HFS_SB(sb)->folder_count--;
213 if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID))
214 HFS_SB(sb)->root_dirs--;
215 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
216 sb->s_dirt = 1;
217 return;
219 HFS_SB(sb)->file_count--;
220 if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID))
221 HFS_SB(sb)->root_files--;
222 if (S_ISREG(inode->i_mode)) {
223 if (!inode->i_nlink) {
224 inode->i_size = 0;
225 hfs_file_truncate(inode);
228 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
229 sb->s_dirt = 1;
232 void hfs_inode_read_fork(struct inode *inode, struct hfs_extent *ext,
233 __be32 __log_size, __be32 phys_size, u32 clump_size)
235 struct super_block *sb = inode->i_sb;
236 u32 log_size = be32_to_cpu(__log_size);
237 u16 count;
238 int i;
240 memcpy(HFS_I(inode)->first_extents, ext, sizeof(hfs_extent_rec));
241 for (count = 0, i = 0; i < 3; i++)
242 count += be16_to_cpu(ext[i].count);
243 HFS_I(inode)->first_blocks = count;
245 inode->i_size = HFS_I(inode)->phys_size = log_size;
246 HFS_I(inode)->fs_blocks = (log_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
247 inode_set_bytes(inode, HFS_I(inode)->fs_blocks << sb->s_blocksize_bits);
248 HFS_I(inode)->alloc_blocks = be32_to_cpu(phys_size) /
249 HFS_SB(sb)->alloc_blksz;
250 HFS_I(inode)->clump_blocks = clump_size / HFS_SB(sb)->alloc_blksz;
251 if (!HFS_I(inode)->clump_blocks)
252 HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks;
255 struct hfs_iget_data {
256 struct hfs_cat_key *key;
257 hfs_cat_rec *rec;
260 static int hfs_test_inode(struct inode *inode, void *data)
262 struct hfs_iget_data *idata = data;
263 hfs_cat_rec *rec;
265 rec = idata->rec;
266 switch (rec->type) {
267 case HFS_CDR_DIR:
268 return inode->i_ino == be32_to_cpu(rec->dir.DirID);
269 case HFS_CDR_FIL:
270 return inode->i_ino == be32_to_cpu(rec->file.FlNum);
271 default:
272 BUG();
273 return 1;
278 * hfs_read_inode
280 static int hfs_read_inode(struct inode *inode, void *data)
282 struct hfs_iget_data *idata = data;
283 struct hfs_sb_info *hsb = HFS_SB(inode->i_sb);
284 hfs_cat_rec *rec;
286 HFS_I(inode)->flags = 0;
287 HFS_I(inode)->rsrc_inode = NULL;
288 mutex_init(&HFS_I(inode)->extents_lock);
289 INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list);
291 /* Initialize the inode */
292 inode->i_uid = hsb->s_uid;
293 inode->i_gid = hsb->s_gid;
294 inode->i_nlink = 1;
296 if (idata->key)
297 HFS_I(inode)->cat_key = *idata->key;
298 else
299 HFS_I(inode)->flags |= HFS_FLG_RSRC;
300 HFS_I(inode)->tz_secondswest = sys_tz.tz_minuteswest * 60;
302 rec = idata->rec;
303 switch (rec->type) {
304 case HFS_CDR_FIL:
305 if (!HFS_IS_RSRC(inode)) {
306 hfs_inode_read_fork(inode, rec->file.ExtRec, rec->file.LgLen,
307 rec->file.PyLen, be16_to_cpu(rec->file.ClpSize));
308 } else {
309 hfs_inode_read_fork(inode, rec->file.RExtRec, rec->file.RLgLen,
310 rec->file.RPyLen, be16_to_cpu(rec->file.ClpSize));
313 inode->i_ino = be32_to_cpu(rec->file.FlNum);
314 inode->i_mode = S_IRUGO | S_IXUGO;
315 if (!(rec->file.Flags & HFS_FIL_LOCK))
316 inode->i_mode |= S_IWUGO;
317 inode->i_mode &= ~hsb->s_file_umask;
318 inode->i_mode |= S_IFREG;
319 inode->i_ctime = inode->i_atime = inode->i_mtime =
320 hfs_m_to_utime(rec->file.MdDat);
321 inode->i_op = &hfs_file_inode_operations;
322 inode->i_fop = &hfs_file_operations;
323 inode->i_mapping->a_ops = &hfs_aops;
324 break;
325 case HFS_CDR_DIR:
326 inode->i_ino = be32_to_cpu(rec->dir.DirID);
327 inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
328 HFS_I(inode)->fs_blocks = 0;
329 inode->i_mode = S_IFDIR | (S_IRWXUGO & ~hsb->s_dir_umask);
330 inode->i_ctime = inode->i_atime = inode->i_mtime =
331 hfs_m_to_utime(rec->dir.MdDat);
332 inode->i_op = &hfs_dir_inode_operations;
333 inode->i_fop = &hfs_dir_operations;
334 break;
335 default:
336 make_bad_inode(inode);
338 return 0;
342 * __hfs_iget()
344 * Given the MDB for a HFS filesystem, a 'key' and an 'entry' in
345 * the catalog B-tree and the 'type' of the desired file return the
346 * inode for that file/directory or NULL. Note that 'type' indicates
347 * whether we want the actual file or directory, or the corresponding
348 * metadata (AppleDouble header file or CAP metadata file).
350 struct inode *hfs_iget(struct super_block *sb, struct hfs_cat_key *key, hfs_cat_rec *rec)
352 struct hfs_iget_data data = { key, rec };
353 struct inode *inode;
354 u32 cnid;
356 switch (rec->type) {
357 case HFS_CDR_DIR:
358 cnid = be32_to_cpu(rec->dir.DirID);
359 break;
360 case HFS_CDR_FIL:
361 cnid = be32_to_cpu(rec->file.FlNum);
362 break;
363 default:
364 return NULL;
366 inode = iget5_locked(sb, cnid, hfs_test_inode, hfs_read_inode, &data);
367 if (inode && (inode->i_state & I_NEW))
368 unlock_new_inode(inode);
369 return inode;
372 void hfs_inode_write_fork(struct inode *inode, struct hfs_extent *ext,
373 __be32 *log_size, __be32 *phys_size)
375 memcpy(ext, HFS_I(inode)->first_extents, sizeof(hfs_extent_rec));
377 if (log_size)
378 *log_size = cpu_to_be32(inode->i_size);
379 if (phys_size)
380 *phys_size = cpu_to_be32(HFS_I(inode)->alloc_blocks *
381 HFS_SB(inode->i_sb)->alloc_blksz);
384 int hfs_write_inode(struct inode *inode, int unused)
386 struct inode *main_inode = inode;
387 struct hfs_find_data fd;
388 hfs_cat_rec rec;
390 dprint(DBG_INODE, "hfs_write_inode: %lu\n", inode->i_ino);
391 hfs_ext_write_extent(inode);
393 if (inode->i_ino < HFS_FIRSTUSER_CNID) {
394 switch (inode->i_ino) {
395 case HFS_ROOT_CNID:
396 break;
397 case HFS_EXT_CNID:
398 hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);
399 return 0;
400 case HFS_CAT_CNID:
401 hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
402 return 0;
403 default:
404 BUG();
405 return -EIO;
409 if (HFS_IS_RSRC(inode))
410 main_inode = HFS_I(inode)->rsrc_inode;
412 if (!main_inode->i_nlink)
413 return 0;
415 if (hfs_find_init(HFS_SB(main_inode->i_sb)->cat_tree, &fd))
416 /* panic? */
417 return -EIO;
419 fd.search_key->cat = HFS_I(main_inode)->cat_key;
420 if (hfs_brec_find(&fd))
421 /* panic? */
422 goto out;
424 if (S_ISDIR(main_inode->i_mode)) {
425 if (fd.entrylength < sizeof(struct hfs_cat_dir))
426 /* panic? */;
427 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
428 sizeof(struct hfs_cat_dir));
429 if (rec.type != HFS_CDR_DIR ||
430 be32_to_cpu(rec.dir.DirID) != inode->i_ino) {
433 rec.dir.MdDat = hfs_u_to_mtime(inode->i_mtime);
434 rec.dir.Val = cpu_to_be16(inode->i_size - 2);
436 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
437 sizeof(struct hfs_cat_dir));
438 } else if (HFS_IS_RSRC(inode)) {
439 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
440 sizeof(struct hfs_cat_file));
441 hfs_inode_write_fork(inode, rec.file.RExtRec,
442 &rec.file.RLgLen, &rec.file.RPyLen);
443 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
444 sizeof(struct hfs_cat_file));
445 } else {
446 if (fd.entrylength < sizeof(struct hfs_cat_file))
447 /* panic? */;
448 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
449 sizeof(struct hfs_cat_file));
450 if (rec.type != HFS_CDR_FIL ||
451 be32_to_cpu(rec.file.FlNum) != inode->i_ino) {
454 if (inode->i_mode & S_IWUSR)
455 rec.file.Flags &= ~HFS_FIL_LOCK;
456 else
457 rec.file.Flags |= HFS_FIL_LOCK;
458 hfs_inode_write_fork(inode, rec.file.ExtRec, &rec.file.LgLen, &rec.file.PyLen);
459 rec.file.MdDat = hfs_u_to_mtime(inode->i_mtime);
461 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
462 sizeof(struct hfs_cat_file));
464 out:
465 hfs_find_exit(&fd);
466 return 0;
469 static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry,
470 struct nameidata *nd)
472 struct inode *inode = NULL;
473 hfs_cat_rec rec;
474 struct hfs_find_data fd;
475 int res;
477 if (HFS_IS_RSRC(dir) || strcmp(dentry->d_name.name, "rsrc"))
478 goto out;
480 inode = HFS_I(dir)->rsrc_inode;
481 if (inode)
482 goto out;
484 inode = new_inode(dir->i_sb);
485 if (!inode)
486 return ERR_PTR(-ENOMEM);
488 hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd);
489 fd.search_key->cat = HFS_I(dir)->cat_key;
490 res = hfs_brec_read(&fd, &rec, sizeof(rec));
491 if (!res) {
492 struct hfs_iget_data idata = { NULL, &rec };
493 hfs_read_inode(inode, &idata);
495 hfs_find_exit(&fd);
496 if (res) {
497 iput(inode);
498 return ERR_PTR(res);
500 HFS_I(inode)->rsrc_inode = dir;
501 HFS_I(dir)->rsrc_inode = inode;
502 igrab(dir);
503 hlist_add_head(&inode->i_hash, &HFS_SB(dir->i_sb)->rsrc_inodes);
504 mark_inode_dirty(inode);
505 out:
506 d_add(dentry, inode);
507 return NULL;
510 void hfs_clear_inode(struct inode *inode)
512 if (HFS_IS_RSRC(inode) && HFS_I(inode)->rsrc_inode) {
513 HFS_I(HFS_I(inode)->rsrc_inode)->rsrc_inode = NULL;
514 iput(HFS_I(inode)->rsrc_inode);
518 static int hfs_file_open(struct inode *inode, struct file *file)
520 if (HFS_IS_RSRC(inode))
521 inode = HFS_I(inode)->rsrc_inode;
522 atomic_inc(&HFS_I(inode)->opencnt);
523 return 0;
526 static int hfs_file_release(struct inode *inode, struct file *file)
528 //struct super_block *sb = inode->i_sb;
530 if (HFS_IS_RSRC(inode))
531 inode = HFS_I(inode)->rsrc_inode;
532 if (atomic_dec_and_test(&HFS_I(inode)->opencnt)) {
533 mutex_lock(&inode->i_mutex);
534 hfs_file_truncate(inode);
535 //if (inode->i_flags & S_DEAD) {
536 // hfs_delete_cat(inode->i_ino, HFSPLUS_SB(sb).hidden_dir, NULL);
537 // hfs_delete_inode(inode);
539 mutex_unlock(&inode->i_mutex);
541 return 0;
545 * hfs_notify_change()
547 * Based very closely on fs/msdos/inode.c by Werner Almesberger
549 * This is the notify_change() field in the super_operations structure
550 * for HFS file systems. The purpose is to take that changes made to
551 * an inode and apply then in a filesystem-dependent manner. In this
552 * case the process has a few of tasks to do:
553 * 1) prevent changes to the i_uid and i_gid fields.
554 * 2) map file permissions to the closest allowable permissions
555 * 3) Since multiple Linux files can share the same on-disk inode under
556 * HFS (for instance the data and resource forks of a file) a change
557 * to permissions must be applied to all other in-core inodes which
558 * correspond to the same HFS file.
561 int hfs_inode_setattr(struct dentry *dentry, struct iattr * attr)
563 struct inode *inode = dentry->d_inode;
564 struct hfs_sb_info *hsb = HFS_SB(inode->i_sb);
565 int error;
567 error = inode_change_ok(inode, attr); /* basic permission checks */
568 if (error)
569 return error;
571 /* no uig/gid changes and limit which mode bits can be set */
572 if (((attr->ia_valid & ATTR_UID) &&
573 (attr->ia_uid != hsb->s_uid)) ||
574 ((attr->ia_valid & ATTR_GID) &&
575 (attr->ia_gid != hsb->s_gid)) ||
576 ((attr->ia_valid & ATTR_MODE) &&
577 ((S_ISDIR(inode->i_mode) &&
578 (attr->ia_mode != inode->i_mode)) ||
579 (attr->ia_mode & ~HFS_VALID_MODE_BITS)))) {
580 return hsb->s_quiet ? 0 : error;
583 if (attr->ia_valid & ATTR_MODE) {
584 /* Only the 'w' bits can ever change and only all together. */
585 if (attr->ia_mode & S_IWUSR)
586 attr->ia_mode = inode->i_mode | S_IWUGO;
587 else
588 attr->ia_mode = inode->i_mode & ~S_IWUGO;
589 attr->ia_mode &= S_ISDIR(inode->i_mode) ? ~hsb->s_dir_umask: ~hsb->s_file_umask;
591 error = inode_setattr(inode, attr);
592 if (error)
593 return error;
595 return 0;
599 static const struct file_operations hfs_file_operations = {
600 .llseek = generic_file_llseek,
601 .read = do_sync_read,
602 .aio_read = generic_file_aio_read,
603 .write = do_sync_write,
604 .aio_write = generic_file_aio_write,
605 .mmap = generic_file_mmap,
606 .splice_read = generic_file_splice_read,
607 .fsync = file_fsync,
608 .open = hfs_file_open,
609 .release = hfs_file_release,
612 static const struct inode_operations hfs_file_inode_operations = {
613 .lookup = hfs_file_lookup,
614 .truncate = hfs_file_truncate,
615 .setattr = hfs_inode_setattr,
616 .setxattr = hfs_setxattr,
617 .getxattr = hfs_getxattr,
618 .listxattr = hfs_listxattr,