]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/hfsplus/inode.c
[PATCH] mark f_ops const in the inode
[net-next-2.6.git] / fs / hfsplus / inode.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/hfsplus/inode.c
3 *
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
7 *
8 * Inode handling routines
9 */
10
11#include <linux/mm.h>
12#include <linux/fs.h>
13#include <linux/pagemap.h>
1da177e4
LT
14#include <linux/mpage.h>
15
16#include "hfsplus_fs.h"
17#include "hfsplus_raw.h"
18
19static int hfsplus_readpage(struct file *file, struct page *page)
20{
1da177e4
LT
21 return block_read_full_page(page, hfsplus_get_block);
22}
23
24static int hfsplus_writepage(struct page *page, struct writeback_control *wbc)
25{
1da177e4
LT
26 return block_write_full_page(page, hfsplus_get_block, wbc);
27}
28
29static int hfsplus_prepare_write(struct file *file, struct page *page, unsigned from, unsigned to)
30{
31 return cont_prepare_write(page, from, to, hfsplus_get_block,
32 &HFSPLUS_I(page->mapping->host).phys_size);
33}
34
35static sector_t hfsplus_bmap(struct address_space *mapping, sector_t block)
36{
37 return generic_block_bmap(mapping, block, hfsplus_get_block);
38}
39
27496a8c 40static int hfsplus_releasepage(struct page *page, gfp_t mask)
1da177e4
LT
41{
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;
48
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;
62 }
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)
68 ;
69 else if (atomic_read(&node->refcnt))
70 res = 0;
71 if (res && node) {
72 hfs_bnode_unhash(node);
73 hfs_bnode_free(node);
74 }
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;
87 }
88 hfs_bnode_unhash(node);
89 hfs_bnode_free(node);
90 } while (--i && nidx < tree->node_count);
91 spin_unlock(&tree->hash_lock);
92 }
1da177e4
LT
93 return res ? try_to_free_buffers(page) : 0;
94}
95
1da177e4
LT
96static ssize_t hfsplus_direct_IO(int rw, struct kiocb *iocb,
97 const struct iovec *iov, loff_t offset, unsigned long nr_segs)
98{
99 struct file *file = iocb->ki_filp;
100 struct inode *inode = file->f_dentry->d_inode->i_mapping->host;
101
102 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
1d8fa7a2 103 offset, nr_segs, hfsplus_get_block, NULL);
1da177e4
LT
104}
105
106static int hfsplus_writepages(struct address_space *mapping,
107 struct writeback_control *wbc)
108{
109 return mpage_writepages(mapping, wbc, hfsplus_get_block);
110}
111
112struct address_space_operations hfsplus_btree_aops = {
113 .readpage = hfsplus_readpage,
114 .writepage = hfsplus_writepage,
115 .sync_page = block_sync_page,
116 .prepare_write = hfsplus_prepare_write,
117 .commit_write = generic_commit_write,
118 .bmap = hfsplus_bmap,
119 .releasepage = hfsplus_releasepage,
120};
121
122struct address_space_operations hfsplus_aops = {
123 .readpage = hfsplus_readpage,
124 .writepage = hfsplus_writepage,
125 .sync_page = block_sync_page,
126 .prepare_write = hfsplus_prepare_write,
127 .commit_write = generic_commit_write,
128 .bmap = hfsplus_bmap,
129 .direct_IO = hfsplus_direct_IO,
130 .writepages = hfsplus_writepages,
131};
132
133static struct dentry *hfsplus_file_lookup(struct inode *dir, struct dentry *dentry,
134 struct nameidata *nd)
135{
136 struct hfs_find_data fd;
137 struct super_block *sb = dir->i_sb;
138 struct inode *inode = NULL;
139 int err;
140
141 if (HFSPLUS_IS_RSRC(dir) || strcmp(dentry->d_name.name, "rsrc"))
142 goto out;
143
144 inode = HFSPLUS_I(dir).rsrc_inode;
145 if (inode)
146 goto out;
147
148 inode = new_inode(sb);
149 if (!inode)
150 return ERR_PTR(-ENOMEM);
151
152 inode->i_ino = dir->i_ino;
153 INIT_LIST_HEAD(&HFSPLUS_I(inode).open_dir_list);
154 init_MUTEX(&HFSPLUS_I(inode).extents_lock);
155 HFSPLUS_I(inode).flags = HFSPLUS_FLG_RSRC;
156
157 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
158 err = hfsplus_find_cat(sb, dir->i_ino, &fd);
159 if (!err)
160 err = hfsplus_cat_read_inode(inode, &fd);
161 hfs_find_exit(&fd);
162 if (err) {
163 iput(inode);
164 return ERR_PTR(err);
165 }
166 HFSPLUS_I(inode).rsrc_inode = dir;
167 HFSPLUS_I(dir).rsrc_inode = inode;
168 igrab(dir);
169 hlist_add_head(&inode->i_hash, &HFSPLUS_SB(sb).rsrc_inodes);
170 mark_inode_dirty(inode);
1da177e4
LT
171out:
172 d_add(dentry, inode);
173 return NULL;
174}
175
176static void hfsplus_get_perms(struct inode *inode, struct hfsplus_perm *perms, int dir)
177{
178 struct super_block *sb = inode->i_sb;
179 u16 mode;
180
181 mode = be16_to_cpu(perms->mode);
182
183 inode->i_uid = be32_to_cpu(perms->owner);
184 if (!inode->i_uid && !mode)
185 inode->i_uid = HFSPLUS_SB(sb).uid;
186
187 inode->i_gid = be32_to_cpu(perms->group);
188 if (!inode->i_gid && !mode)
189 inode->i_gid = HFSPLUS_SB(sb).gid;
190
191 if (dir) {
192 mode = mode ? (mode & S_IALLUGO) :
193 (S_IRWXUGO & ~(HFSPLUS_SB(sb).umask));
194 mode |= S_IFDIR;
195 } else if (!mode)
196 mode = S_IFREG | ((S_IRUGO|S_IWUGO) &
197 ~(HFSPLUS_SB(sb).umask));
198 inode->i_mode = mode;
199
200 HFSPLUS_I(inode).rootflags = perms->rootflags;
201 HFSPLUS_I(inode).userflags = perms->userflags;
202 if (perms->rootflags & HFSPLUS_FLG_IMMUTABLE)
203 inode->i_flags |= S_IMMUTABLE;
204 else
205 inode->i_flags &= ~S_IMMUTABLE;
206 if (perms->rootflags & HFSPLUS_FLG_APPEND)
207 inode->i_flags |= S_APPEND;
208 else
209 inode->i_flags &= ~S_APPEND;
210}
211
212static void hfsplus_set_perms(struct inode *inode, struct hfsplus_perm *perms)
213{
214 if (inode->i_flags & S_IMMUTABLE)
215 perms->rootflags |= HFSPLUS_FLG_IMMUTABLE;
216 else
217 perms->rootflags &= ~HFSPLUS_FLG_IMMUTABLE;
218 if (inode->i_flags & S_APPEND)
219 perms->rootflags |= HFSPLUS_FLG_APPEND;
220 else
221 perms->rootflags &= ~HFSPLUS_FLG_APPEND;
222 perms->userflags = HFSPLUS_I(inode).userflags;
223 perms->mode = cpu_to_be16(inode->i_mode);
224 perms->owner = cpu_to_be32(inode->i_uid);
225 perms->group = cpu_to_be32(inode->i_gid);
226 perms->dev = cpu_to_be32(HFSPLUS_I(inode).dev);
227}
228
229static int hfsplus_permission(struct inode *inode, int mask, struct nameidata *nd)
230{
231 /* MAY_EXEC is also used for lookup, if no x bit is set allow lookup,
232 * open_exec has the same test, so it's still not executable, if a x bit
233 * is set fall back to standard permission check.
234 */
235 if (S_ISREG(inode->i_mode) && mask & MAY_EXEC && !(inode->i_mode & 0111))
236 return 0;
237 return generic_permission(inode, mask, NULL);
238}
239
240
241static int hfsplus_file_open(struct inode *inode, struct file *file)
242{
243 if (HFSPLUS_IS_RSRC(inode))
244 inode = HFSPLUS_I(inode).rsrc_inode;
245 if (atomic_read(&file->f_count) != 1)
246 return 0;
247 atomic_inc(&HFSPLUS_I(inode).opencnt);
248 return 0;
249}
250
251static int hfsplus_file_release(struct inode *inode, struct file *file)
252{
253 struct super_block *sb = inode->i_sb;
254
255 if (HFSPLUS_IS_RSRC(inode))
256 inode = HFSPLUS_I(inode).rsrc_inode;
257 if (atomic_read(&file->f_count) != 0)
258 return 0;
259 if (atomic_dec_and_test(&HFSPLUS_I(inode).opencnt)) {
1b1dcc1b 260 mutex_lock(&inode->i_mutex);
1da177e4
LT
261 hfsplus_file_truncate(inode);
262 if (inode->i_flags & S_DEAD) {
263 hfsplus_delete_cat(inode->i_ino, HFSPLUS_SB(sb).hidden_dir, NULL);
264 hfsplus_delete_inode(inode);
265 }
1b1dcc1b 266 mutex_unlock(&inode->i_mutex);
1da177e4
LT
267 }
268 return 0;
269}
270
271extern struct inode_operations hfsplus_dir_inode_operations;
272extern struct file_operations hfsplus_dir_operations;
273
274static struct inode_operations hfsplus_file_inode_operations = {
275 .lookup = hfsplus_file_lookup,
276 .truncate = hfsplus_file_truncate,
277 .permission = hfsplus_permission,
278 .setxattr = hfsplus_setxattr,
279 .getxattr = hfsplus_getxattr,
280 .listxattr = hfsplus_listxattr,
281};
282
283static struct file_operations hfsplus_file_operations = {
284 .llseek = generic_file_llseek,
285 .read = generic_file_read,
286 .write = generic_file_write,
287 .mmap = generic_file_mmap,
288 .sendfile = generic_file_sendfile,
289 .fsync = file_fsync,
290 .open = hfsplus_file_open,
291 .release = hfsplus_file_release,
292 .ioctl = hfsplus_ioctl,
293};
294
295struct inode *hfsplus_new_inode(struct super_block *sb, int mode)
296{
297 struct inode *inode = new_inode(sb);
298 if (!inode)
299 return NULL;
300
1da177e4
LT
301 inode->i_ino = HFSPLUS_SB(sb).next_cnid++;
302 inode->i_mode = mode;
303 inode->i_uid = current->fsuid;
304 inode->i_gid = current->fsgid;
305 inode->i_nlink = 1;
306 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
307 inode->i_blksize = HFSPLUS_SB(sb).alloc_blksz;
308 INIT_LIST_HEAD(&HFSPLUS_I(inode).open_dir_list);
309 init_MUTEX(&HFSPLUS_I(inode).extents_lock);
310 atomic_set(&HFSPLUS_I(inode).opencnt, 0);
311 HFSPLUS_I(inode).flags = 0;
312 memset(HFSPLUS_I(inode).first_extents, 0, sizeof(hfsplus_extent_rec));
313 memset(HFSPLUS_I(inode).cached_extents, 0, sizeof(hfsplus_extent_rec));
314 HFSPLUS_I(inode).alloc_blocks = 0;
315 HFSPLUS_I(inode).first_blocks = 0;
316 HFSPLUS_I(inode).cached_start = 0;
317 HFSPLUS_I(inode).cached_blocks = 0;
318 HFSPLUS_I(inode).phys_size = 0;
319 HFSPLUS_I(inode).fs_blocks = 0;
320 HFSPLUS_I(inode).rsrc_inode = NULL;
321 if (S_ISDIR(inode->i_mode)) {
322 inode->i_size = 2;
323 HFSPLUS_SB(sb).folder_count++;
324 inode->i_op = &hfsplus_dir_inode_operations;
325 inode->i_fop = &hfsplus_dir_operations;
326 } else if (S_ISREG(inode->i_mode)) {
327 HFSPLUS_SB(sb).file_count++;
328 inode->i_op = &hfsplus_file_inode_operations;
329 inode->i_fop = &hfsplus_file_operations;
330 inode->i_mapping->a_ops = &hfsplus_aops;
331 HFSPLUS_I(inode).clump_blocks = HFSPLUS_SB(sb).data_clump_blocks;
332 } else if (S_ISLNK(inode->i_mode)) {
333 HFSPLUS_SB(sb).file_count++;
334 inode->i_op = &page_symlink_inode_operations;
335 inode->i_mapping->a_ops = &hfsplus_aops;
336 HFSPLUS_I(inode).clump_blocks = 1;
337 } else
338 HFSPLUS_SB(sb).file_count++;
339 insert_inode_hash(inode);
340 mark_inode_dirty(inode);
341 sb->s_dirt = 1;
342
343 return inode;
344}
345
346void hfsplus_delete_inode(struct inode *inode)
347{
348 struct super_block *sb = inode->i_sb;
349
350 if (S_ISDIR(inode->i_mode)) {
351 HFSPLUS_SB(sb).folder_count--;
352 sb->s_dirt = 1;
353 return;
354 }
355 HFSPLUS_SB(sb).file_count--;
356 if (S_ISREG(inode->i_mode)) {
357 if (!inode->i_nlink) {
358 inode->i_size = 0;
359 hfsplus_file_truncate(inode);
360 }
361 } else if (S_ISLNK(inode->i_mode)) {
362 inode->i_size = 0;
363 hfsplus_file_truncate(inode);
364 }
365 sb->s_dirt = 1;
366}
367
368void hfsplus_inode_read_fork(struct inode *inode, struct hfsplus_fork_raw *fork)
369{
370 struct super_block *sb = inode->i_sb;
371 u32 count;
372 int i;
373
374 memcpy(&HFSPLUS_I(inode).first_extents, &fork->extents,
375 sizeof(hfsplus_extent_rec));
376 for (count = 0, i = 0; i < 8; i++)
377 count += be32_to_cpu(fork->extents[i].block_count);
378 HFSPLUS_I(inode).first_blocks = count;
379 memset(HFSPLUS_I(inode).cached_extents, 0, sizeof(hfsplus_extent_rec));
380 HFSPLUS_I(inode).cached_start = 0;
381 HFSPLUS_I(inode).cached_blocks = 0;
382
383 HFSPLUS_I(inode).alloc_blocks = be32_to_cpu(fork->total_blocks);
384 inode->i_size = HFSPLUS_I(inode).phys_size = be64_to_cpu(fork->total_size);
385 HFSPLUS_I(inode).fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
386 inode_set_bytes(inode, HFSPLUS_I(inode).fs_blocks << sb->s_blocksize_bits);
387 HFSPLUS_I(inode).clump_blocks = be32_to_cpu(fork->clump_size) >> HFSPLUS_SB(sb).alloc_blksz_shift;
388 if (!HFSPLUS_I(inode).clump_blocks)
389 HFSPLUS_I(inode).clump_blocks = HFSPLUS_IS_RSRC(inode) ? HFSPLUS_SB(sb).rsrc_clump_blocks :
390 HFSPLUS_SB(sb).data_clump_blocks;
391}
392
393void hfsplus_inode_write_fork(struct inode *inode, struct hfsplus_fork_raw *fork)
394{
395 memcpy(&fork->extents, &HFSPLUS_I(inode).first_extents,
396 sizeof(hfsplus_extent_rec));
397 fork->total_size = cpu_to_be64(inode->i_size);
398 fork->total_blocks = cpu_to_be32(HFSPLUS_I(inode).alloc_blocks);
399}
400
401int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd)
402{
403 hfsplus_cat_entry entry;
404 int res = 0;
405 u16 type;
406
407 type = hfs_bnode_read_u16(fd->bnode, fd->entryoffset);
408
409 HFSPLUS_I(inode).dev = 0;
410 inode->i_blksize = HFSPLUS_SB(inode->i_sb).alloc_blksz;
411 if (type == HFSPLUS_FOLDER) {
412 struct hfsplus_cat_folder *folder = &entry.folder;
413
414 if (fd->entrylength < sizeof(struct hfsplus_cat_folder))
415 /* panic? */;
416 hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
417 sizeof(struct hfsplus_cat_folder));
418 hfsplus_get_perms(inode, &folder->permissions, 1);
419 inode->i_nlink = 1;
420 inode->i_size = 2 + be32_to_cpu(folder->valence);
421 inode->i_atime = hfsp_mt2ut(folder->access_date);
422 inode->i_mtime = hfsp_mt2ut(folder->content_mod_date);
9a4cad95
RZ
423 inode->i_ctime = hfsp_mt2ut(folder->attribute_mod_date);
424 HFSPLUS_I(inode).create_date = folder->create_date;
1da177e4
LT
425 HFSPLUS_I(inode).fs_blocks = 0;
426 inode->i_op = &hfsplus_dir_inode_operations;
427 inode->i_fop = &hfsplus_dir_operations;
428 } else if (type == HFSPLUS_FILE) {
429 struct hfsplus_cat_file *file = &entry.file;
430
431 if (fd->entrylength < sizeof(struct hfsplus_cat_file))
432 /* panic? */;
433 hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
434 sizeof(struct hfsplus_cat_file));
435
436 hfsplus_inode_read_fork(inode, HFSPLUS_IS_DATA(inode) ?
437 &file->data_fork : &file->rsrc_fork);
438 hfsplus_get_perms(inode, &file->permissions, 0);
439 inode->i_nlink = 1;
440 if (S_ISREG(inode->i_mode)) {
441 if (file->permissions.dev)
442 inode->i_nlink = be32_to_cpu(file->permissions.dev);
443 inode->i_op = &hfsplus_file_inode_operations;
444 inode->i_fop = &hfsplus_file_operations;
445 inode->i_mapping->a_ops = &hfsplus_aops;
446 } else if (S_ISLNK(inode->i_mode)) {
447 inode->i_op = &page_symlink_inode_operations;
448 inode->i_mapping->a_ops = &hfsplus_aops;
449 } else {
450 init_special_inode(inode, inode->i_mode,
451 be32_to_cpu(file->permissions.dev));
452 }
453 inode->i_atime = hfsp_mt2ut(file->access_date);
454 inode->i_mtime = hfsp_mt2ut(file->content_mod_date);
9a4cad95
RZ
455 inode->i_ctime = hfsp_mt2ut(file->attribute_mod_date);
456 HFSPLUS_I(inode).create_date = file->create_date;
1da177e4 457 } else {
634725a9 458 printk(KERN_ERR "hfs: bad catalog entry used to create inode\n");
1da177e4
LT
459 res = -EIO;
460 }
461 return res;
462}
463
464int hfsplus_cat_write_inode(struct inode *inode)
465{
466 struct inode *main_inode = inode;
467 struct hfs_find_data fd;
468 hfsplus_cat_entry entry;
469
470 if (HFSPLUS_IS_RSRC(inode))
471 main_inode = HFSPLUS_I(inode).rsrc_inode;
472
473 if (!main_inode->i_nlink)
474 return 0;
475
476 if (hfs_find_init(HFSPLUS_SB(main_inode->i_sb).cat_tree, &fd))
477 /* panic? */
478 return -EIO;
479
480 if (hfsplus_find_cat(main_inode->i_sb, main_inode->i_ino, &fd))
481 /* panic? */
482 goto out;
483
484 if (S_ISDIR(main_inode->i_mode)) {
485 struct hfsplus_cat_folder *folder = &entry.folder;
486
487 if (fd.entrylength < sizeof(struct hfsplus_cat_folder))
488 /* panic? */;
489 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
490 sizeof(struct hfsplus_cat_folder));
491 /* simple node checks? */
492 hfsplus_set_perms(inode, &folder->permissions);
493 folder->access_date = hfsp_ut2mt(inode->i_atime);
494 folder->content_mod_date = hfsp_ut2mt(inode->i_mtime);
495 folder->attribute_mod_date = hfsp_ut2mt(inode->i_ctime);
496 folder->valence = cpu_to_be32(inode->i_size - 2);
497 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
498 sizeof(struct hfsplus_cat_folder));
499 } else if (HFSPLUS_IS_RSRC(inode)) {
500 struct hfsplus_cat_file *file = &entry.file;
501 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
502 sizeof(struct hfsplus_cat_file));
503 hfsplus_inode_write_fork(inode, &file->rsrc_fork);
504 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
505 sizeof(struct hfsplus_cat_file));
506 } else {
507 struct hfsplus_cat_file *file = &entry.file;
508
509 if (fd.entrylength < sizeof(struct hfsplus_cat_file))
510 /* panic? */;
511 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
512 sizeof(struct hfsplus_cat_file));
513 hfsplus_inode_write_fork(inode, &file->data_fork);
514 if (S_ISREG(inode->i_mode))
515 HFSPLUS_I(inode).dev = inode->i_nlink;
516 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode))
517 HFSPLUS_I(inode).dev = kdev_t_to_nr(inode->i_rdev);
518 hfsplus_set_perms(inode, &file->permissions);
519 if ((file->permissions.rootflags | file->permissions.userflags) & HFSPLUS_FLG_IMMUTABLE)
520 file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
521 else
522 file->flags &= cpu_to_be16(~HFSPLUS_FILE_LOCKED);
523 file->access_date = hfsp_ut2mt(inode->i_atime);
524 file->content_mod_date = hfsp_ut2mt(inode->i_mtime);
525 file->attribute_mod_date = hfsp_ut2mt(inode->i_ctime);
526 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
527 sizeof(struct hfsplus_cat_file));
528 }
529out:
530 hfs_find_exit(&fd);
531 return 0;
532}