1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/hfsplus/inode.c
4 *
5 * Copyright (C) 2001
6 * Brad Boyer (flar@allandria.com)
7 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 *
9 * Inode handling routines
10 */
11
12 #include <linux/blkdev.h>
13 #include <linux/mm.h>
14 #include <linux/fs.h>
15 #include <linux/pagemap.h>
16 #include <linux/mpage.h>
17 #include <linux/sched.h>
18 #include <linux/cred.h>
19 #include <linux/uio.h>
20 #include <linux/fileattr.h>
21
22 #include "hfsplus_fs.h"
23 #include "hfsplus_raw.h"
24 #include "xattr.h"
25
hfsplus_read_folio(struct file * file,struct folio * folio)26 static int hfsplus_read_folio(struct file *file, struct folio *folio)
27 {
28 return block_read_full_folio(folio, hfsplus_get_block);
29 }
30
hfsplus_write_failed(struct address_space * mapping,loff_t to)31 static void hfsplus_write_failed(struct address_space *mapping, loff_t to)
32 {
33 struct inode *inode = mapping->host;
34
35 if (to > inode->i_size) {
36 truncate_pagecache(inode, inode->i_size);
37 hfsplus_file_truncate(inode);
38 }
39 }
40
hfsplus_write_begin(const struct kiocb * iocb,struct address_space * mapping,loff_t pos,unsigned len,struct folio ** foliop,void ** fsdata)41 int hfsplus_write_begin(const struct kiocb *iocb,
42 struct address_space *mapping, loff_t pos,
43 unsigned len, struct folio **foliop,
44 void **fsdata)
45 {
46 int ret;
47
48 ret = cont_write_begin(iocb, mapping, pos, len, foliop, fsdata,
49 hfsplus_get_block,
50 &HFSPLUS_I(mapping->host)->phys_size);
51 if (unlikely(ret))
52 hfsplus_write_failed(mapping, pos + len);
53
54 return ret;
55 }
56
hfsplus_bmap(struct address_space * mapping,sector_t block)57 static sector_t hfsplus_bmap(struct address_space *mapping, sector_t block)
58 {
59 return generic_block_bmap(mapping, block, hfsplus_get_block);
60 }
61
hfsplus_release_folio(struct folio * folio,gfp_t mask)62 static bool hfsplus_release_folio(struct folio *folio, gfp_t mask)
63 {
64 struct inode *inode = folio->mapping->host;
65 struct super_block *sb = inode->i_sb;
66 struct hfs_btree *tree;
67 struct hfs_bnode *node;
68 u32 nidx;
69 int i;
70 bool res = true;
71
72 switch (inode->i_ino) {
73 case HFSPLUS_EXT_CNID:
74 tree = HFSPLUS_SB(sb)->ext_tree;
75 break;
76 case HFSPLUS_CAT_CNID:
77 tree = HFSPLUS_SB(sb)->cat_tree;
78 break;
79 case HFSPLUS_ATTR_CNID:
80 tree = HFSPLUS_SB(sb)->attr_tree;
81 break;
82 default:
83 BUG();
84 return false;
85 }
86 if (!tree)
87 return false;
88 if (tree->node_size >= PAGE_SIZE) {
89 nidx = folio->index >>
90 (tree->node_size_shift - PAGE_SHIFT);
91 spin_lock(&tree->hash_lock);
92 node = hfs_bnode_findhash(tree, nidx);
93 if (!node)
94 ;
95 else if (atomic_read(&node->refcnt))
96 res = false;
97 if (res && node) {
98 hfs_bnode_unhash(node);
99 hfs_bnode_free(node);
100 }
101 spin_unlock(&tree->hash_lock);
102 } else {
103 nidx = folio->index <<
104 (PAGE_SHIFT - tree->node_size_shift);
105 i = 1 << (PAGE_SHIFT - tree->node_size_shift);
106 spin_lock(&tree->hash_lock);
107 do {
108 node = hfs_bnode_findhash(tree, nidx++);
109 if (!node)
110 continue;
111 if (atomic_read(&node->refcnt)) {
112 res = false;
113 break;
114 }
115 hfs_bnode_unhash(node);
116 hfs_bnode_free(node);
117 } while (--i && nidx < tree->node_count);
118 spin_unlock(&tree->hash_lock);
119 }
120 return res ? try_to_free_buffers(folio) : false;
121 }
122
hfsplus_direct_IO(struct kiocb * iocb,struct iov_iter * iter)123 static ssize_t hfsplus_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
124 {
125 struct file *file = iocb->ki_filp;
126 struct address_space *mapping = file->f_mapping;
127 struct inode *inode = mapping->host;
128 size_t count = iov_iter_count(iter);
129 ssize_t ret;
130
131 ret = blockdev_direct_IO(iocb, inode, iter, hfsplus_get_block);
132
133 /*
134 * In case of error extending write may have instantiated a few
135 * blocks outside i_size. Trim these off again.
136 */
137 if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) {
138 loff_t isize = i_size_read(inode);
139 loff_t end = iocb->ki_pos + count;
140
141 if (end > isize)
142 hfsplus_write_failed(mapping, end);
143 }
144
145 return ret;
146 }
147
hfsplus_writepages(struct address_space * mapping,struct writeback_control * wbc)148 static int hfsplus_writepages(struct address_space *mapping,
149 struct writeback_control *wbc)
150 {
151 return mpage_writepages(mapping, wbc, hfsplus_get_block);
152 }
153
154 const struct address_space_operations hfsplus_btree_aops = {
155 .dirty_folio = block_dirty_folio,
156 .invalidate_folio = block_invalidate_folio,
157 .read_folio = hfsplus_read_folio,
158 .writepages = hfsplus_writepages,
159 .write_begin = hfsplus_write_begin,
160 .write_end = generic_write_end,
161 .migrate_folio = buffer_migrate_folio,
162 .bmap = hfsplus_bmap,
163 .release_folio = hfsplus_release_folio,
164 };
165
166 const struct address_space_operations hfsplus_aops = {
167 .dirty_folio = block_dirty_folio,
168 .invalidate_folio = block_invalidate_folio,
169 .read_folio = hfsplus_read_folio,
170 .write_begin = hfsplus_write_begin,
171 .write_end = generic_write_end,
172 .bmap = hfsplus_bmap,
173 .direct_IO = hfsplus_direct_IO,
174 .writepages = hfsplus_writepages,
175 .migrate_folio = buffer_migrate_folio,
176 };
177
178 const struct dentry_operations hfsplus_dentry_operations = {
179 .d_hash = hfsplus_hash_dentry,
180 .d_compare = hfsplus_compare_dentry,
181 };
182
hfsplus_get_perms(struct inode * inode,struct hfsplus_perm * perms,int dir)183 static void hfsplus_get_perms(struct inode *inode,
184 struct hfsplus_perm *perms, int dir)
185 {
186 struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
187 u16 mode;
188
189 mode = be16_to_cpu(perms->mode);
190
191 i_uid_write(inode, be32_to_cpu(perms->owner));
192 if ((test_bit(HFSPLUS_SB_UID, &sbi->flags)) || (!i_uid_read(inode) && !mode))
193 inode->i_uid = sbi->uid;
194
195 i_gid_write(inode, be32_to_cpu(perms->group));
196 if ((test_bit(HFSPLUS_SB_GID, &sbi->flags)) || (!i_gid_read(inode) && !mode))
197 inode->i_gid = sbi->gid;
198
199 if (dir) {
200 mode = mode ? (mode & S_IALLUGO) : (S_IRWXUGO & ~(sbi->umask));
201 mode |= S_IFDIR;
202 } else if (!mode)
203 mode = S_IFREG | ((S_IRUGO|S_IWUGO) & ~(sbi->umask));
204 inode->i_mode = mode;
205
206 HFSPLUS_I(inode)->userflags = perms->userflags;
207 if (perms->rootflags & HFSPLUS_FLG_IMMUTABLE)
208 inode->i_flags |= S_IMMUTABLE;
209 else
210 inode->i_flags &= ~S_IMMUTABLE;
211 if (perms->rootflags & HFSPLUS_FLG_APPEND)
212 inode->i_flags |= S_APPEND;
213 else
214 inode->i_flags &= ~S_APPEND;
215 }
216
hfsplus_file_open(struct inode * inode,struct file * file)217 static int hfsplus_file_open(struct inode *inode, struct file *file)
218 {
219 if (HFSPLUS_IS_RSRC(inode))
220 inode = HFSPLUS_I(inode)->rsrc_inode;
221 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
222 return -EOVERFLOW;
223 atomic_inc(&HFSPLUS_I(inode)->opencnt);
224 return 0;
225 }
226
hfsplus_file_release(struct inode * inode,struct file * file)227 static int hfsplus_file_release(struct inode *inode, struct file *file)
228 {
229 struct super_block *sb = inode->i_sb;
230
231 if (HFSPLUS_IS_RSRC(inode))
232 inode = HFSPLUS_I(inode)->rsrc_inode;
233 if (atomic_dec_and_test(&HFSPLUS_I(inode)->opencnt)) {
234 inode_lock(inode);
235 hfsplus_file_truncate(inode);
236 if (inode->i_flags & S_DEAD) {
237 hfsplus_delete_cat(inode->i_ino,
238 HFSPLUS_SB(sb)->hidden_dir, NULL);
239 hfsplus_delete_inode(inode);
240 }
241 inode_unlock(inode);
242 }
243 return 0;
244 }
245
hfsplus_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)246 static int hfsplus_setattr(struct mnt_idmap *idmap,
247 struct dentry *dentry, struct iattr *attr)
248 {
249 struct inode *inode = d_inode(dentry);
250 int error;
251
252 error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
253 if (error)
254 return error;
255
256 if ((attr->ia_valid & ATTR_SIZE) &&
257 attr->ia_size != i_size_read(inode)) {
258 inode_dio_wait(inode);
259 if (attr->ia_size > inode->i_size) {
260 error = generic_cont_expand_simple(inode,
261 attr->ia_size);
262 if (error)
263 return error;
264 }
265 truncate_setsize(inode, attr->ia_size);
266 hfsplus_file_truncate(inode);
267 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
268 }
269
270 setattr_copy(&nop_mnt_idmap, inode, attr);
271 mark_inode_dirty(inode);
272
273 return 0;
274 }
275
hfsplus_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)276 int hfsplus_getattr(struct mnt_idmap *idmap, const struct path *path,
277 struct kstat *stat, u32 request_mask,
278 unsigned int query_flags)
279 {
280 struct inode *inode = d_inode(path->dentry);
281 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
282
283 if (request_mask & STATX_BTIME) {
284 stat->result_mask |= STATX_BTIME;
285 stat->btime = hfsp_mt2ut(hip->create_date);
286 }
287
288 if (inode->i_flags & S_APPEND)
289 stat->attributes |= STATX_ATTR_APPEND;
290 if (inode->i_flags & S_IMMUTABLE)
291 stat->attributes |= STATX_ATTR_IMMUTABLE;
292 if (hip->userflags & HFSPLUS_FLG_NODUMP)
293 stat->attributes |= STATX_ATTR_NODUMP;
294
295 stat->attributes_mask |= STATX_ATTR_APPEND | STATX_ATTR_IMMUTABLE |
296 STATX_ATTR_NODUMP;
297
298 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
299 return 0;
300 }
301
hfsplus_file_fsync(struct file * file,loff_t start,loff_t end,int datasync)302 int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end,
303 int datasync)
304 {
305 struct inode *inode = file->f_mapping->host;
306 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
307 struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
308 int error = 0, error2;
309
310 error = file_write_and_wait_range(file, start, end);
311 if (error)
312 return error;
313 inode_lock(inode);
314
315 /*
316 * Sync inode metadata into the catalog and extent trees.
317 */
318 sync_inode_metadata(inode, 1);
319
320 /*
321 * And explicitly write out the btrees.
322 */
323 if (test_and_clear_bit(HFSPLUS_I_CAT_DIRTY, &hip->flags))
324 error = filemap_write_and_wait(sbi->cat_tree->inode->i_mapping);
325
326 if (test_and_clear_bit(HFSPLUS_I_EXT_DIRTY, &hip->flags)) {
327 error2 =
328 filemap_write_and_wait(sbi->ext_tree->inode->i_mapping);
329 if (!error)
330 error = error2;
331 }
332
333 if (test_and_clear_bit(HFSPLUS_I_ATTR_DIRTY, &hip->flags)) {
334 if (sbi->attr_tree) {
335 error2 =
336 filemap_write_and_wait(
337 sbi->attr_tree->inode->i_mapping);
338 if (!error)
339 error = error2;
340 } else {
341 pr_err("sync non-existent attributes tree\n");
342 }
343 }
344
345 if (test_and_clear_bit(HFSPLUS_I_ALLOC_DIRTY, &hip->flags)) {
346 error2 = filemap_write_and_wait(sbi->alloc_file->i_mapping);
347 if (!error)
348 error = error2;
349 }
350
351 if (!test_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags))
352 blkdev_issue_flush(inode->i_sb->s_bdev);
353
354 inode_unlock(inode);
355
356 return error;
357 }
358
359 static const struct inode_operations hfsplus_file_inode_operations = {
360 .setattr = hfsplus_setattr,
361 .getattr = hfsplus_getattr,
362 .listxattr = hfsplus_listxattr,
363 .fileattr_get = hfsplus_fileattr_get,
364 .fileattr_set = hfsplus_fileattr_set,
365 };
366
367 static const struct file_operations hfsplus_file_operations = {
368 .llseek = generic_file_llseek,
369 .read_iter = generic_file_read_iter,
370 .write_iter = generic_file_write_iter,
371 .mmap_prepare = generic_file_mmap_prepare,
372 .splice_read = filemap_splice_read,
373 .splice_write = iter_file_splice_write,
374 .fsync = hfsplus_file_fsync,
375 .open = hfsplus_file_open,
376 .release = hfsplus_file_release,
377 .unlocked_ioctl = hfsplus_ioctl,
378 };
379
hfsplus_new_inode(struct super_block * sb,struct inode * dir,umode_t mode)380 struct inode *hfsplus_new_inode(struct super_block *sb, struct inode *dir,
381 umode_t mode)
382 {
383 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
384 struct inode *inode = new_inode(sb);
385 struct hfsplus_inode_info *hip;
386
387 if (!inode)
388 return NULL;
389
390 inode->i_ino = sbi->next_cnid++;
391 inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
392 set_nlink(inode, 1);
393 simple_inode_init_ts(inode);
394
395 hip = HFSPLUS_I(inode);
396 INIT_LIST_HEAD(&hip->open_dir_list);
397 spin_lock_init(&hip->open_dir_lock);
398 mutex_init(&hip->extents_lock);
399 atomic_set(&hip->opencnt, 0);
400 hip->extent_state = 0;
401 hip->flags = 0;
402 hip->userflags = 0;
403 hip->subfolders = 0;
404 memset(hip->first_extents, 0, sizeof(hfsplus_extent_rec));
405 memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
406 hip->alloc_blocks = 0;
407 hip->first_blocks = 0;
408 hip->cached_start = 0;
409 hip->cached_blocks = 0;
410 hip->phys_size = 0;
411 hip->fs_blocks = 0;
412 hip->rsrc_inode = NULL;
413 if (S_ISDIR(inode->i_mode)) {
414 inode->i_size = 2;
415 sbi->folder_count++;
416 inode->i_op = &hfsplus_dir_inode_operations;
417 inode->i_fop = &hfsplus_dir_operations;
418 } else if (S_ISREG(inode->i_mode)) {
419 sbi->file_count++;
420 inode->i_op = &hfsplus_file_inode_operations;
421 inode->i_fop = &hfsplus_file_operations;
422 inode->i_mapping->a_ops = &hfsplus_aops;
423 hip->clump_blocks = sbi->data_clump_blocks;
424 } else if (S_ISLNK(inode->i_mode)) {
425 sbi->file_count++;
426 inode->i_op = &page_symlink_inode_operations;
427 inode_nohighmem(inode);
428 inode->i_mapping->a_ops = &hfsplus_aops;
429 hip->clump_blocks = 1;
430 } else
431 sbi->file_count++;
432 insert_inode_hash(inode);
433 mark_inode_dirty(inode);
434 hfsplus_mark_mdb_dirty(sb);
435
436 return inode;
437 }
438
hfsplus_delete_inode(struct inode * inode)439 void hfsplus_delete_inode(struct inode *inode)
440 {
441 struct super_block *sb = inode->i_sb;
442
443 if (S_ISDIR(inode->i_mode)) {
444 HFSPLUS_SB(sb)->folder_count--;
445 hfsplus_mark_mdb_dirty(sb);
446 return;
447 }
448 HFSPLUS_SB(sb)->file_count--;
449 if (S_ISREG(inode->i_mode)) {
450 if (!inode->i_nlink) {
451 inode->i_size = 0;
452 hfsplus_file_truncate(inode);
453 }
454 } else if (S_ISLNK(inode->i_mode)) {
455 inode->i_size = 0;
456 hfsplus_file_truncate(inode);
457 }
458 hfsplus_mark_mdb_dirty(sb);
459 }
460
hfsplus_inode_read_fork(struct inode * inode,struct hfsplus_fork_raw * fork)461 void hfsplus_inode_read_fork(struct inode *inode, struct hfsplus_fork_raw *fork)
462 {
463 struct super_block *sb = inode->i_sb;
464 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
465 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
466 u32 count;
467 int i;
468
469 memcpy(&hip->first_extents, &fork->extents, sizeof(hfsplus_extent_rec));
470 for (count = 0, i = 0; i < 8; i++)
471 count += be32_to_cpu(fork->extents[i].block_count);
472 hip->first_blocks = count;
473 memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
474 hip->cached_start = 0;
475 hip->cached_blocks = 0;
476
477 hip->alloc_blocks = be32_to_cpu(fork->total_blocks);
478 hip->phys_size = inode->i_size = be64_to_cpu(fork->total_size);
479 hip->fs_blocks =
480 (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
481 inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits);
482 hip->clump_blocks =
483 be32_to_cpu(fork->clump_size) >> sbi->alloc_blksz_shift;
484 if (!hip->clump_blocks) {
485 hip->clump_blocks = HFSPLUS_IS_RSRC(inode) ?
486 sbi->rsrc_clump_blocks :
487 sbi->data_clump_blocks;
488 }
489 }
490
hfsplus_inode_write_fork(struct inode * inode,struct hfsplus_fork_raw * fork)491 void hfsplus_inode_write_fork(struct inode *inode,
492 struct hfsplus_fork_raw *fork)
493 {
494 memcpy(&fork->extents, &HFSPLUS_I(inode)->first_extents,
495 sizeof(hfsplus_extent_rec));
496 fork->total_size = cpu_to_be64(inode->i_size);
497 fork->total_blocks = cpu_to_be32(HFSPLUS_I(inode)->alloc_blocks);
498 }
499
hfsplus_cat_read_inode(struct inode * inode,struct hfs_find_data * fd)500 int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd)
501 {
502 hfsplus_cat_entry entry;
503 int res = 0;
504 u16 type;
505
506 type = hfs_bnode_read_u16(fd->bnode, fd->entryoffset);
507
508 HFSPLUS_I(inode)->linkid = 0;
509 if (type == HFSPLUS_FOLDER) {
510 struct hfsplus_cat_folder *folder = &entry.folder;
511
512 if (fd->entrylength < sizeof(struct hfsplus_cat_folder)) {
513 pr_err("bad catalog folder entry\n");
514 res = -EIO;
515 goto out;
516 }
517 hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
518 sizeof(struct hfsplus_cat_folder));
519 hfsplus_get_perms(inode, &folder->permissions, 1);
520 set_nlink(inode, 1);
521 inode->i_size = 2 + be32_to_cpu(folder->valence);
522 inode_set_atime_to_ts(inode, hfsp_mt2ut(folder->access_date));
523 inode_set_mtime_to_ts(inode,
524 hfsp_mt2ut(folder->content_mod_date));
525 inode_set_ctime_to_ts(inode,
526 hfsp_mt2ut(folder->attribute_mod_date));
527 HFSPLUS_I(inode)->create_date = folder->create_date;
528 HFSPLUS_I(inode)->fs_blocks = 0;
529 if (folder->flags & cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT)) {
530 HFSPLUS_I(inode)->subfolders =
531 be32_to_cpu(folder->subfolders);
532 }
533 inode->i_op = &hfsplus_dir_inode_operations;
534 inode->i_fop = &hfsplus_dir_operations;
535 } else if (type == HFSPLUS_FILE) {
536 struct hfsplus_cat_file *file = &entry.file;
537
538 if (fd->entrylength < sizeof(struct hfsplus_cat_file)) {
539 pr_err("bad catalog file entry\n");
540 res = -EIO;
541 goto out;
542 }
543 hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
544 sizeof(struct hfsplus_cat_file));
545
546 hfsplus_inode_read_fork(inode, HFSPLUS_IS_RSRC(inode) ?
547 &file->rsrc_fork : &file->data_fork);
548 hfsplus_get_perms(inode, &file->permissions, 0);
549 set_nlink(inode, 1);
550 if (S_ISREG(inode->i_mode)) {
551 if (file->permissions.dev)
552 set_nlink(inode,
553 be32_to_cpu(file->permissions.dev));
554 inode->i_op = &hfsplus_file_inode_operations;
555 inode->i_fop = &hfsplus_file_operations;
556 inode->i_mapping->a_ops = &hfsplus_aops;
557 } else if (S_ISLNK(inode->i_mode)) {
558 inode->i_op = &page_symlink_inode_operations;
559 inode_nohighmem(inode);
560 inode->i_mapping->a_ops = &hfsplus_aops;
561 } else {
562 init_special_inode(inode, inode->i_mode,
563 be32_to_cpu(file->permissions.dev));
564 }
565 inode_set_atime_to_ts(inode, hfsp_mt2ut(file->access_date));
566 inode_set_mtime_to_ts(inode,
567 hfsp_mt2ut(file->content_mod_date));
568 inode_set_ctime_to_ts(inode,
569 hfsp_mt2ut(file->attribute_mod_date));
570 HFSPLUS_I(inode)->create_date = file->create_date;
571 } else {
572 pr_err("bad catalog entry used to create inode\n");
573 res = -EIO;
574 }
575 out:
576 return res;
577 }
578
hfsplus_cat_write_inode(struct inode * inode)579 int hfsplus_cat_write_inode(struct inode *inode)
580 {
581 struct inode *main_inode = inode;
582 struct hfs_find_data fd;
583 hfsplus_cat_entry entry;
584 int res = 0;
585
586 if (HFSPLUS_IS_RSRC(inode))
587 main_inode = HFSPLUS_I(inode)->rsrc_inode;
588
589 if (!main_inode->i_nlink)
590 return 0;
591
592 if (hfs_find_init(HFSPLUS_SB(main_inode->i_sb)->cat_tree, &fd))
593 /* panic? */
594 return -EIO;
595
596 if (hfsplus_find_cat(main_inode->i_sb, main_inode->i_ino, &fd))
597 /* panic? */
598 goto out;
599
600 if (S_ISDIR(main_inode->i_mode)) {
601 struct hfsplus_cat_folder *folder = &entry.folder;
602
603 if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
604 pr_err("bad catalog folder entry\n");
605 res = -EIO;
606 goto out;
607 }
608 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
609 sizeof(struct hfsplus_cat_folder));
610 /* simple node checks? */
611 hfsplus_cat_set_perms(inode, &folder->permissions);
612 folder->access_date = hfsp_ut2mt(inode_get_atime(inode));
613 folder->content_mod_date = hfsp_ut2mt(inode_get_mtime(inode));
614 folder->attribute_mod_date = hfsp_ut2mt(inode_get_ctime(inode));
615 folder->valence = cpu_to_be32(inode->i_size - 2);
616 if (folder->flags & cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT)) {
617 folder->subfolders =
618 cpu_to_be32(HFSPLUS_I(inode)->subfolders);
619 }
620 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
621 sizeof(struct hfsplus_cat_folder));
622 } else if (HFSPLUS_IS_RSRC(inode)) {
623 struct hfsplus_cat_file *file = &entry.file;
624 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
625 sizeof(struct hfsplus_cat_file));
626 hfsplus_inode_write_fork(inode, &file->rsrc_fork);
627 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
628 sizeof(struct hfsplus_cat_file));
629 } else {
630 struct hfsplus_cat_file *file = &entry.file;
631
632 if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
633 pr_err("bad catalog file entry\n");
634 res = -EIO;
635 goto out;
636 }
637 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
638 sizeof(struct hfsplus_cat_file));
639 hfsplus_inode_write_fork(inode, &file->data_fork);
640 hfsplus_cat_set_perms(inode, &file->permissions);
641 if (HFSPLUS_FLG_IMMUTABLE &
642 (file->permissions.rootflags |
643 file->permissions.userflags))
644 file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
645 else
646 file->flags &= cpu_to_be16(~HFSPLUS_FILE_LOCKED);
647 file->access_date = hfsp_ut2mt(inode_get_atime(inode));
648 file->content_mod_date = hfsp_ut2mt(inode_get_mtime(inode));
649 file->attribute_mod_date = hfsp_ut2mt(inode_get_ctime(inode));
650 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
651 sizeof(struct hfsplus_cat_file));
652 }
653
654 set_bit(HFSPLUS_I_CAT_DIRTY, &HFSPLUS_I(inode)->flags);
655 out:
656 hfs_find_exit(&fd);
657 return res;
658 }
659
hfsplus_fileattr_get(struct dentry * dentry,struct file_kattr * fa)660 int hfsplus_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
661 {
662 struct inode *inode = d_inode(dentry);
663 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
664 unsigned int flags = 0;
665
666 if (inode->i_flags & S_IMMUTABLE)
667 flags |= FS_IMMUTABLE_FL;
668 if (inode->i_flags & S_APPEND)
669 flags |= FS_APPEND_FL;
670 if (hip->userflags & HFSPLUS_FLG_NODUMP)
671 flags |= FS_NODUMP_FL;
672
673 fileattr_fill_flags(fa, flags);
674
675 return 0;
676 }
677
hfsplus_fileattr_set(struct mnt_idmap * idmap,struct dentry * dentry,struct file_kattr * fa)678 int hfsplus_fileattr_set(struct mnt_idmap *idmap,
679 struct dentry *dentry, struct file_kattr *fa)
680 {
681 struct inode *inode = d_inode(dentry);
682 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
683 unsigned int new_fl = 0;
684
685 if (fileattr_has_fsx(fa))
686 return -EOPNOTSUPP;
687
688 /* don't silently ignore unsupported ext2 flags */
689 if (fa->flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL))
690 return -EOPNOTSUPP;
691
692 if (fa->flags & FS_IMMUTABLE_FL)
693 new_fl |= S_IMMUTABLE;
694
695 if (fa->flags & FS_APPEND_FL)
696 new_fl |= S_APPEND;
697
698 inode_set_flags(inode, new_fl, S_IMMUTABLE | S_APPEND);
699
700 if (fa->flags & FS_NODUMP_FL)
701 hip->userflags |= HFSPLUS_FLG_NODUMP;
702 else
703 hip->userflags &= ~HFSPLUS_FLG_NODUMP;
704
705 inode_set_ctime_current(inode);
706 mark_inode_dirty(inode);
707
708 return 0;
709 }
710