MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / fs / hfsplus / inode.c
blobb25671d58aaade7c9b9602bfa5855cc6ea3a7c16
1 /*
2 * linux/fs/hfsplus/inode.c
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Inode handling routines
9 */
11 #include <linux/mm.h>
12 #include <linux/fs.h>
13 #include <linux/pagemap.h>
14 #include <linux/mpage.h>
16 #include "hfsplus_fs.h"
17 #include "hfsplus_raw.h"
19 static int hfsplus_readpage(struct file *file, struct page *page)
21 return block_read_full_page(page, hfsplus_get_block);
24 static int hfsplus_writepage(struct page *page, struct writeback_control *wbc)
26 return block_write_full_page(page, hfsplus_get_block, wbc);
29 static int hfsplus_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
31 return cont_prepare_write(page, from, to, hfsplus_get_block,
32 &HFSPLUS_I(page->mapping->host).phys_size);
35 static sector_t hfsplus_bmap(struct address_space *mapping, sector_t block)
37 return generic_block_bmap(mapping, block, hfsplus_get_block);
40 static int hfsplus_releasepage(struct page *page, gfp_t mask)
42 struct inode *inode = page->mapping->host;
43 struct super_block *sb = inode->i_sb;
44 struct hfs_btree *tree;
45 struct hfs_bnode *node;
46 u32 nidx;
47 int i, res = 1;
49 switch (inode->i_ino) {
50 case HFSPLUS_EXT_CNID:
51 tree = HFSPLUS_SB(sb).ext_tree;
52 break;
53 case HFSPLUS_CAT_CNID:
54 tree = HFSPLUS_SB(sb).cat_tree;
55 break;
56 case HFSPLUS_ATTR_CNID:
57 tree = HFSPLUS_SB(sb).attr_tree;
58 break;
59 default:
60 BUG();
61 return 0;
63 if (tree->node_size >= PAGE_CACHE_SIZE) {
64 nidx = page->index >> (tree->node_size_shift - PAGE_CACHE_SHIFT);
65 spin_lock(&tree->hash_lock);
66 node = hfs_bnode_findhash(tree, nidx);
67 if (!node)
69 else if (atomic_read(&node->refcnt))
70 res = 0;
71 if (res && node) {
72 hfs_bnode_unhash(node);
73 hfs_bnode_free(node);
75 spin_unlock(&tree->hash_lock);
76 } else {
77 nidx = page->index << (PAGE_CACHE_SHIFT - tree->node_size_shift);
78 i = 1 << (PAGE_CACHE_SHIFT - tree->node_size_shift);
79 spin_lock(&tree->hash_lock);
80 do {
81 node = hfs_bnode_findhash(tree, nidx++);
82 if (!node)
83 continue;
84 if (atomic_read(&node->refcnt)) {
85 res = 0;
86 break;
88 hfs_bnode_unhash(node);
89 hfs_bnode_free(node);
90 } while (--i && nidx < tree->node_count);
91 spin_unlock(&tree->hash_lock);
93 return res ? try_to_free_buffers(page) : 0;
96 #ifdef CONFIG_DIRECTIO
97 static ssize_t hfsplus_direct_IO(int rw, struct kiocb *iocb,
98 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
100 struct file *file = iocb->ki_filp;
101 struct inode *inode = file->f_dentry->d_inode->i_mapping->host;
103 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
104 offset, nr_segs, hfsplus_get_block, NULL);
106 #endif
108 static int hfsplus_writepages(struct address_space *mapping,
109 struct writeback_control *wbc)
111 return mpage_writepages(mapping, wbc, hfsplus_get_block);
114 const struct address_space_operations hfsplus_btree_aops = {
115 .readpage = hfsplus_readpage,
116 .writepage = hfsplus_writepage,
117 .sync_page = block_sync_page,
118 .prepare_write = hfsplus_prepare_write,
119 .commit_write = generic_commit_write,
120 .bmap = hfsplus_bmap,
121 .releasepage = hfsplus_releasepage,
124 const struct address_space_operations hfsplus_aops = {
125 .readpage = hfsplus_readpage,
126 .writepage = hfsplus_writepage,
127 .sync_page = block_sync_page,
128 .prepare_write = hfsplus_prepare_write,
129 .commit_write = generic_commit_write,
130 .bmap = hfsplus_bmap,
131 #ifdef CONFIG_DIRECTIO
132 .direct_IO = hfsplus_direct_IO,
133 #endif
134 .writepages = hfsplus_writepages,
137 static struct dentry *hfsplus_file_lookup(struct inode *dir, struct dentry *dentry,
138 struct nameidata *nd)
140 struct hfs_find_data fd;
141 struct super_block *sb = dir->i_sb;
142 struct inode *inode = NULL;
143 int err;
145 if (HFSPLUS_IS_RSRC(dir) || strcmp(dentry->d_name.name, "rsrc"))
146 goto out;
148 inode = HFSPLUS_I(dir).rsrc_inode;
149 if (inode)
150 goto out;
152 inode = new_inode(sb);
153 if (!inode)
154 return ERR_PTR(-ENOMEM);
156 inode->i_ino = dir->i_ino;
157 INIT_LIST_HEAD(&HFSPLUS_I(inode).open_dir_list);
158 init_MUTEX(&HFSPLUS_I(inode).extents_lock);
159 HFSPLUS_I(inode).flags = HFSPLUS_FLG_RSRC;
161 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
162 err = hfsplus_find_cat(sb, dir->i_ino, &fd);
163 if (!err)
164 err = hfsplus_cat_read_inode(inode, &fd);
165 hfs_find_exit(&fd);
166 if (err) {
167 iput(inode);
168 return ERR_PTR(err);
170 HFSPLUS_I(inode).rsrc_inode = dir;
171 HFSPLUS_I(dir).rsrc_inode = inode;
172 igrab(dir);
173 hlist_add_head(&inode->i_hash, &HFSPLUS_SB(sb).rsrc_inodes);
174 mark_inode_dirty(inode);
175 out:
176 d_add(dentry, inode);
177 return NULL;
180 static void hfsplus_get_perms(struct inode *inode, struct hfsplus_perm *perms, int dir)
182 struct super_block *sb = inode->i_sb;
183 u16 mode;
185 mode = be16_to_cpu(perms->mode);
187 inode->i_uid = be32_to_cpu(perms->owner);
188 if (!inode->i_uid && !mode)
189 inode->i_uid = HFSPLUS_SB(sb).uid;
191 inode->i_gid = be32_to_cpu(perms->group);
192 if (!inode->i_gid && !mode)
193 inode->i_gid = HFSPLUS_SB(sb).gid;
195 if (dir) {
196 mode = mode ? (mode & S_IALLUGO) :
197 (S_IRWXUGO & ~(HFSPLUS_SB(sb).umask));
198 mode |= S_IFDIR;
199 } else if (!mode)
200 mode = S_IFREG | ((S_IRUGO|S_IWUGO) &
201 ~(HFSPLUS_SB(sb).umask));
202 inode->i_mode = mode;
204 HFSPLUS_I(inode).rootflags = perms->rootflags;
205 HFSPLUS_I(inode).userflags = perms->userflags;
206 if (perms->rootflags & HFSPLUS_FLG_IMMUTABLE)
207 inode->i_flags |= S_IMMUTABLE;
208 else
209 inode->i_flags &= ~S_IMMUTABLE;
210 if (perms->rootflags & HFSPLUS_FLG_APPEND)
211 inode->i_flags |= S_APPEND;
212 else
213 inode->i_flags &= ~S_APPEND;
216 static void hfsplus_set_perms(struct inode *inode, struct hfsplus_perm *perms)
218 if (inode->i_flags & S_IMMUTABLE)
219 perms->rootflags |= HFSPLUS_FLG_IMMUTABLE;
220 else
221 perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
222 if (inode->i_flags & S_APPEND)
223 perms->rootflags |= HFSPLUS_FLG_APPEND;
224 else
225 perms->rootflags &= ~HFSPLUS_FLG_APPEND;
226 perms->userflags = HFSPLUS_I(inode).userflags;
227 perms->mode = cpu_to_be16(inode->i_mode);
228 perms->owner = cpu_to_be32(inode->i_uid);
229 perms->group = cpu_to_be32(inode->i_gid);
230 perms->dev = cpu_to_be32(HFSPLUS_I(inode).dev);
233 static int hfsplus_permission(struct inode *inode, int mask, struct nameidata *nd)
235 /* MAY_EXEC is also used for lookup, if no x bit is set allow lookup,
236 * open_exec has the same test, so it's still not executable, if a x bit
237 * is set fall back to standard permission check.
239 if (S_ISREG(inode->i_mode) && mask & MAY_EXEC && !(inode->i_mode & 0111))
240 return 0;
241 return generic_permission(inode, mask, NULL);
245 static int hfsplus_file_open(struct inode *inode, struct file *file)
247 if (HFSPLUS_IS_RSRC(inode))
248 inode = HFSPLUS_I(inode).rsrc_inode;
249 if (atomic_read(&file->f_count) != 1)
250 return 0;
251 atomic_inc(&HFSPLUS_I(inode).opencnt);
252 return 0;
255 static int hfsplus_file_release(struct inode *inode, struct file *file)
257 struct super_block *sb = inode->i_sb;
259 if (HFSPLUS_IS_RSRC(inode))
260 inode = HFSPLUS_I(inode).rsrc_inode;
261 if (atomic_read(&file->f_count) != 0)
262 return 0;
263 if (atomic_dec_and_test(&HFSPLUS_I(inode).opencnt)) {
264 mutex_lock(&inode->i_mutex);
265 hfsplus_file_truncate(inode);
266 if (inode->i_flags & S_DEAD) {
267 hfsplus_delete_cat(inode->i_ino, HFSPLUS_SB(sb).hidden_dir, NULL);
268 hfsplus_delete_inode(inode);
270 mutex_unlock(&inode->i_mutex);
272 return 0;
275 extern struct inode_operations hfsplus_dir_inode_operations;
276 extern struct file_operations hfsplus_dir_operations;
278 static struct inode_operations hfsplus_file_inode_operations = {
279 .lookup = hfsplus_file_lookup,
280 .truncate = hfsplus_file_truncate,
281 .permission = hfsplus_permission,
282 .setxattr = hfsplus_setxattr,
283 .getxattr = hfsplus_getxattr,
284 .listxattr = hfsplus_listxattr,
287 static const struct file_operations hfsplus_file_operations = {
288 .llseek = generic_file_llseek,
289 .read = do_sync_read,
290 .aio_read = generic_file_aio_read,
291 .write = do_sync_write,
292 .aio_write = generic_file_aio_write,
293 .mmap = generic_file_mmap,
294 .sendfile = generic_file_sendfile,
295 .fsync = file_fsync,
296 .open = hfsplus_file_open,
297 .release = hfsplus_file_release,
298 .ioctl = hfsplus_ioctl,
301 struct inode *hfsplus_new_inode(struct super_block *sb, int mode)
303 struct inode *inode = new_inode(sb);
304 if (!inode)
305 return NULL;
307 inode->i_ino = HFSPLUS_SB(sb).next_cnid++;
308 inode->i_mode = mode;
309 inode->i_uid = current->fsuid;
310 inode->i_gid = current->fsgid;
311 inode->i_nlink = 1;
312 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
313 INIT_LIST_HEAD(&HFSPLUS_I(inode).open_dir_list);
314 init_MUTEX(&HFSPLUS_I(inode).extents_lock);
315 atomic_set(&HFSPLUS_I(inode).opencnt, 0);
316 HFSPLUS_I(inode).flags = 0;
317 memset(HFSPLUS_I(inode).first_extents, 0, sizeof(hfsplus_extent_rec));
318 memset(HFSPLUS_I(inode).cached_extents, 0, sizeof(hfsplus_extent_rec));
319 HFSPLUS_I(inode).alloc_blocks = 0;
320 HFSPLUS_I(inode).first_blocks = 0;
321 HFSPLUS_I(inode).cached_start = 0;
322 HFSPLUS_I(inode).cached_blocks = 0;
323 HFSPLUS_I(inode).phys_size = 0;
324 HFSPLUS_I(inode).fs_blocks = 0;
325 HFSPLUS_I(inode).rsrc_inode = NULL;
326 if (S_ISDIR(inode->i_mode)) {
327 inode->i_size = 2;
328 HFSPLUS_SB(sb).folder_count++;
329 inode->i_op = &hfsplus_dir_inode_operations;
330 inode->i_fop = &hfsplus_dir_operations;
331 } else if (S_ISREG(inode->i_mode)) {
332 HFSPLUS_SB(sb).file_count++;
333 inode->i_op = &hfsplus_file_inode_operations;
334 inode->i_fop = &hfsplus_file_operations;
335 inode->i_mapping->a_ops = &hfsplus_aops;
336 HFSPLUS_I(inode).clump_blocks = HFSPLUS_SB(sb).data_clump_blocks;
337 } else if (S_ISLNK(inode->i_mode)) {
338 HFSPLUS_SB(sb).file_count++;
339 inode->i_op = &page_symlink_inode_operations;
340 inode->i_mapping->a_ops = &hfsplus_aops;
341 HFSPLUS_I(inode).clump_blocks = 1;
342 } else
343 HFSPLUS_SB(sb).file_count++;
344 insert_inode_hash(inode);
345 mark_inode_dirty(inode);
346 sb->s_dirt = 1;
348 return inode;
351 void hfsplus_delete_inode(struct inode *inode)
353 struct super_block *sb = inode->i_sb;
355 if (S_ISDIR(inode->i_mode)) {
356 HFSPLUS_SB(sb).folder_count--;
357 sb->s_dirt = 1;
358 return;
360 HFSPLUS_SB(sb).file_count--;
361 if (S_ISREG(inode->i_mode)) {
362 if (!inode->i_nlink) {
363 inode->i_size = 0;
364 hfsplus_file_truncate(inode);
366 } else if (S_ISLNK(inode->i_mode)) {
367 inode->i_size = 0;
368 hfsplus_file_truncate(inode);
370 sb->s_dirt = 1;
373 void hfsplus_inode_read_fork(struct inode *inode, struct hfsplus_fork_raw *fork)
375 struct super_block *sb = inode->i_sb;
376 u32 count;
377 int i;
379 memcpy(&HFSPLUS_I(inode).first_extents, &fork->extents,
380 sizeof(hfsplus_extent_rec));
381 for (count = 0, i = 0; i < 8; i++)
382 count += be32_to_cpu(fork->extents[i].block_count);
383 HFSPLUS_I(inode).first_blocks = count;
384 memset(HFSPLUS_I(inode).cached_extents, 0, sizeof(hfsplus_extent_rec));
385 HFSPLUS_I(inode).cached_start = 0;
386 HFSPLUS_I(inode).cached_blocks = 0;
388 HFSPLUS_I(inode).alloc_blocks = be32_to_cpu(fork->total_blocks);
389 inode->i_size = HFSPLUS_I(inode).phys_size = be64_to_cpu(fork->total_size);
390 HFSPLUS_I(inode).fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
391 inode_set_bytes(inode, HFSPLUS_I(inode).fs_blocks << sb->s_blocksize_bits);
392 HFSPLUS_I(inode).clump_blocks = be32_to_cpu(fork->clump_size) >> HFSPLUS_SB(sb).alloc_blksz_shift;
393 if (!HFSPLUS_I(inode).clump_blocks)
394 HFSPLUS_I(inode).clump_blocks = HFSPLUS_IS_RSRC(inode) ? HFSPLUS_SB(sb).rsrc_clump_blocks :
395 HFSPLUS_SB(sb).data_clump_blocks;
398 void hfsplus_inode_write_fork(struct inode *inode, struct hfsplus_fork_raw *fork)
400 memcpy(&fork->extents, &HFSPLUS_I(inode).first_extents,
401 sizeof(hfsplus_extent_rec));
402 fork->total_size = cpu_to_be64(inode->i_size);
403 fork->total_blocks = cpu_to_be32(HFSPLUS_I(inode).alloc_blocks);
406 int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd)
408 hfsplus_cat_entry entry;
409 int res = 0;
410 u16 type;
412 type = hfs_bnode_read_u16(fd->bnode, fd->entryoffset);
414 HFSPLUS_I(inode).dev = 0;
415 if (type == HFSPLUS_FOLDER) {
416 struct hfsplus_cat_folder *folder = &entry.folder;
418 if (fd->entrylength < sizeof(struct hfsplus_cat_folder))
419 /* panic? */;
420 hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
421 sizeof(struct hfsplus_cat_folder));
422 hfsplus_get_perms(inode, &folder->permissions, 1);
423 inode->i_nlink = 1;
424 inode->i_size = 2 + be32_to_cpu(folder->valence);
425 inode->i_atime = hfsp_mt2ut(folder->access_date);
426 inode->i_mtime = hfsp_mt2ut(folder->content_mod_date);
427 inode->i_ctime = hfsp_mt2ut(folder->attribute_mod_date);
428 HFSPLUS_I(inode).create_date = folder->create_date;
429 HFSPLUS_I(inode).fs_blocks = 0;
430 inode->i_op = &hfsplus_dir_inode_operations;
431 inode->i_fop = &hfsplus_dir_operations;
432 } else if (type == HFSPLUS_FILE) {
433 struct hfsplus_cat_file *file = &entry.file;
435 if (fd->entrylength < sizeof(struct hfsplus_cat_file))
436 /* panic? */;
437 hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
438 sizeof(struct hfsplus_cat_file));
440 hfsplus_inode_read_fork(inode, HFSPLUS_IS_DATA(inode) ?
441 &file->data_fork : &file->rsrc_fork);
442 hfsplus_get_perms(inode, &file->permissions, 0);
443 inode->i_nlink = 1;
444 if (S_ISREG(inode->i_mode)) {
445 if (file->permissions.dev)
446 inode->i_nlink = be32_to_cpu(file->permissions.dev);
447 inode->i_op = &hfsplus_file_inode_operations;
448 inode->i_fop = &hfsplus_file_operations;
449 inode->i_mapping->a_ops = &hfsplus_aops;
450 } else if (S_ISLNK(inode->i_mode)) {
451 inode->i_op = &page_symlink_inode_operations;
452 inode->i_mapping->a_ops = &hfsplus_aops;
453 } else {
454 init_special_inode(inode, inode->i_mode,
455 be32_to_cpu(file->permissions.dev));
457 inode->i_atime = hfsp_mt2ut(file->access_date);
458 inode->i_mtime = hfsp_mt2ut(file->content_mod_date);
459 inode->i_ctime = hfsp_mt2ut(file->attribute_mod_date);
460 HFSPLUS_I(inode).create_date = file->create_date;
461 } else {
462 printk(KERN_ERR "hfs: bad catalog entry used to create inode\n");
463 res = -EIO;
465 return res;
468 int hfsplus_cat_write_inode(struct inode *inode)
470 struct inode *main_inode = inode;
471 struct hfs_find_data fd;
472 hfsplus_cat_entry entry;
474 if (HFSPLUS_IS_RSRC(inode))
475 main_inode = HFSPLUS_I(inode).rsrc_inode;
477 if (!main_inode->i_nlink)
478 return 0;
480 if (hfs_find_init(HFSPLUS_SB(main_inode->i_sb).cat_tree, &fd))
481 /* panic? */
482 return -EIO;
484 if (hfsplus_find_cat(main_inode->i_sb, main_inode->i_ino, &fd))
485 /* panic? */
486 goto out;
488 if (S_ISDIR(main_inode->i_mode)) {
489 struct hfsplus_cat_folder *folder = &entry.folder;
491 if (fd.entrylength < sizeof(struct hfsplus_cat_folder))
492 /* panic? */;
493 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
494 sizeof(struct hfsplus_cat_folder));
495 /* simple node checks? */
496 hfsplus_set_perms(inode, &folder->permissions);
497 folder->access_date = hfsp_ut2mt(inode->i_atime);
498 folder->content_mod_date = hfsp_ut2mt(inode->i_mtime);
499 folder->attribute_mod_date = hfsp_ut2mt(inode->i_ctime);
500 folder->valence = cpu_to_be32(inode->i_size - 2);
501 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
502 sizeof(struct hfsplus_cat_folder));
503 } else if (HFSPLUS_IS_RSRC(inode)) {
504 struct hfsplus_cat_file *file = &entry.file;
505 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
506 sizeof(struct hfsplus_cat_file));
507 hfsplus_inode_write_fork(inode, &file->rsrc_fork);
508 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
509 sizeof(struct hfsplus_cat_file));
510 } else {
511 struct hfsplus_cat_file *file = &entry.file;
513 if (fd.entrylength < sizeof(struct hfsplus_cat_file))
514 /* panic? */;
515 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
516 sizeof(struct hfsplus_cat_file));
517 hfsplus_inode_write_fork(inode, &file->data_fork);
518 if (S_ISREG(inode->i_mode))
519 HFSPLUS_I(inode).dev = inode->i_nlink;
520 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
521 HFSPLUS_I(inode).dev = kdev_t_to_nr(inode->i_rdev);
522 hfsplus_set_perms(inode, &file->permissions);
523 if ((file->permissions.rootflags | file->permissions.userflags) & HFSPLUS_FLG_IMMUTABLE)
524 file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
525 else
526 file->flags &= cpu_to_be16(~HFSPLUS_FILE_LOCKED);
527 file->access_date = hfsp_ut2mt(inode->i_atime);
528 file->content_mod_date = hfsp_ut2mt(inode->i_mtime);
529 file->attribute_mod_date = hfsp_ut2mt(inode->i_ctime);
530 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
531 sizeof(struct hfsplus_cat_file));
533 out:
534 hfs_find_exit(&fd);
535 return 0;