Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Christoph Hellwig | 34a2d31 | 2010-11-23 14:38:21 +0100 | [diff] [blame] | 12 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/mm.h> |
| 14 | #include <linux/fs.h> |
| 15 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/mpage.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 17 | #include <linux/sched.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 18 | #include <linux/cred.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 19 | #include <linux/uio.h> |
Miklos Szeredi | 9cbae74 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 20 | #include <linux/fileattr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #include "hfsplus_fs.h" |
| 23 | #include "hfsplus_raw.h" |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 24 | #include "xattr.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | static int hfsplus_readpage(struct file *file, struct page *page) |
| 27 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | return block_read_full_page(page, hfsplus_get_block); |
| 29 | } |
| 30 | |
| 31 | static int hfsplus_writepage(struct page *page, struct writeback_control *wbc) |
| 32 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | return block_write_full_page(page, hfsplus_get_block, wbc); |
| 34 | } |
| 35 | |
Marco Stornelli | d506848 | 2012-12-15 11:55:07 +0100 | [diff] [blame] | 36 | static void hfsplus_write_failed(struct address_space *mapping, loff_t to) |
| 37 | { |
| 38 | struct inode *inode = mapping->host; |
| 39 | |
| 40 | if (to > inode->i_size) { |
Kirill A. Shutemov | 7caef26 | 2013-09-12 15:13:56 -0700 | [diff] [blame] | 41 | truncate_pagecache(inode, inode->i_size); |
Marco Stornelli | d506848 | 2012-12-15 11:55:07 +0100 | [diff] [blame] | 42 | hfsplus_file_truncate(inode); |
| 43 | } |
| 44 | } |
| 45 | |
Nick Piggin | 7c0efc6 | 2007-10-16 01:25:09 -0700 | [diff] [blame] | 46 | static int hfsplus_write_begin(struct file *file, struct address_space *mapping, |
| 47 | loff_t pos, unsigned len, unsigned flags, |
| 48 | struct page **pagep, void **fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Christoph Hellwig | 282dc17 | 2010-06-04 11:29:55 +0200 | [diff] [blame] | 50 | int ret; |
| 51 | |
Nick Piggin | 7c0efc6 | 2007-10-16 01:25:09 -0700 | [diff] [blame] | 52 | *pagep = NULL; |
Christoph Hellwig | 282dc17 | 2010-06-04 11:29:55 +0200 | [diff] [blame] | 53 | ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata, |
Nick Piggin | 7c0efc6 | 2007-10-16 01:25:09 -0700 | [diff] [blame] | 54 | hfsplus_get_block, |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 55 | &HFSPLUS_I(mapping->host)->phys_size); |
Marco Stornelli | d506848 | 2012-12-15 11:55:07 +0100 | [diff] [blame] | 56 | if (unlikely(ret)) |
| 57 | hfsplus_write_failed(mapping, pos + len); |
Christoph Hellwig | 282dc17 | 2010-06-04 11:29:55 +0200 | [diff] [blame] | 58 | |
| 59 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static sector_t hfsplus_bmap(struct address_space *mapping, sector_t block) |
| 63 | { |
| 64 | return generic_block_bmap(mapping, block, hfsplus_get_block); |
| 65 | } |
| 66 | |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 67 | static int hfsplus_releasepage(struct page *page, gfp_t mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | struct inode *inode = page->mapping->host; |
| 70 | struct super_block *sb = inode->i_sb; |
| 71 | struct hfs_btree *tree; |
| 72 | struct hfs_bnode *node; |
| 73 | u32 nidx; |
| 74 | int i, res = 1; |
| 75 | |
| 76 | switch (inode->i_ino) { |
| 77 | case HFSPLUS_EXT_CNID: |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 78 | tree = HFSPLUS_SB(sb)->ext_tree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | break; |
| 80 | case HFSPLUS_CAT_CNID: |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 81 | tree = HFSPLUS_SB(sb)->cat_tree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | break; |
| 83 | case HFSPLUS_ATTR_CNID: |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 84 | tree = HFSPLUS_SB(sb)->attr_tree; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | break; |
| 86 | default: |
| 87 | BUG(); |
| 88 | return 0; |
| 89 | } |
Eric Sesterhenn | 7063224 | 2008-05-12 14:02:21 -0700 | [diff] [blame] | 90 | if (!tree) |
| 91 | return 0; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 92 | if (tree->node_size >= PAGE_SIZE) { |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 93 | nidx = page->index >> |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 94 | (tree->node_size_shift - PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | spin_lock(&tree->hash_lock); |
| 96 | node = hfs_bnode_findhash(tree, nidx); |
| 97 | if (!node) |
| 98 | ; |
| 99 | else if (atomic_read(&node->refcnt)) |
| 100 | res = 0; |
| 101 | if (res && node) { |
| 102 | hfs_bnode_unhash(node); |
| 103 | hfs_bnode_free(node); |
| 104 | } |
| 105 | spin_unlock(&tree->hash_lock); |
| 106 | } else { |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 107 | nidx = page->index << |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 108 | (PAGE_SHIFT - tree->node_size_shift); |
| 109 | i = 1 << (PAGE_SHIFT - tree->node_size_shift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | spin_lock(&tree->hash_lock); |
| 111 | do { |
| 112 | node = hfs_bnode_findhash(tree, nidx++); |
| 113 | if (!node) |
| 114 | continue; |
| 115 | if (atomic_read(&node->refcnt)) { |
| 116 | res = 0; |
| 117 | break; |
| 118 | } |
| 119 | hfs_bnode_unhash(node); |
| 120 | hfs_bnode_free(node); |
| 121 | } while (--i && nidx < tree->node_count); |
| 122 | spin_unlock(&tree->hash_lock); |
| 123 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | return res ? try_to_free_buffers(page) : 0; |
| 125 | } |
| 126 | |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 127 | static ssize_t hfsplus_direct_IO(struct kiocb *iocb, struct iov_iter *iter) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | struct file *file = iocb->ki_filp; |
Marco Stornelli | d506848 | 2012-12-15 11:55:07 +0100 | [diff] [blame] | 130 | struct address_space *mapping = file->f_mapping; |
Al Viro | 93c76a3 | 2015-12-04 23:45:44 -0500 | [diff] [blame] | 131 | struct inode *inode = mapping->host; |
Al Viro | a6cbcd4 | 2014-03-04 22:38:00 -0500 | [diff] [blame] | 132 | size_t count = iov_iter_count(iter); |
Christoph Hellwig | eafdc7d | 2010-06-04 11:29:53 +0200 | [diff] [blame] | 133 | ssize_t ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 135 | ret = blockdev_direct_IO(iocb, inode, iter, hfsplus_get_block); |
Christoph Hellwig | eafdc7d | 2010-06-04 11:29:53 +0200 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * In case of error extending write may have instantiated a few |
| 139 | * blocks outside i_size. Trim these off again. |
| 140 | */ |
Omar Sandoval | 6f67376 | 2015-03-16 04:33:52 -0700 | [diff] [blame] | 141 | if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) { |
Christoph Hellwig | eafdc7d | 2010-06-04 11:29:53 +0200 | [diff] [blame] | 142 | loff_t isize = i_size_read(inode); |
Christoph Hellwig | c8b8e32 | 2016-04-07 08:51:58 -0700 | [diff] [blame] | 143 | loff_t end = iocb->ki_pos + count; |
Christoph Hellwig | eafdc7d | 2010-06-04 11:29:53 +0200 | [diff] [blame] | 144 | |
| 145 | if (end > isize) |
Marco Stornelli | d506848 | 2012-12-15 11:55:07 +0100 | [diff] [blame] | 146 | hfsplus_write_failed(mapping, end); |
Christoph Hellwig | eafdc7d | 2010-06-04 11:29:53 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static int hfsplus_writepages(struct address_space *mapping, |
| 153 | struct writeback_control *wbc) |
| 154 | { |
| 155 | return mpage_writepages(mapping, wbc, hfsplus_get_block); |
| 156 | } |
| 157 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 158 | const struct address_space_operations hfsplus_btree_aops = { |
Christoph Hellwig | 0af5737 | 2021-06-28 19:36:12 -0700 | [diff] [blame] | 159 | .set_page_dirty = __set_page_dirty_buffers, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | .readpage = hfsplus_readpage, |
| 161 | .writepage = hfsplus_writepage, |
Nick Piggin | 7c0efc6 | 2007-10-16 01:25:09 -0700 | [diff] [blame] | 162 | .write_begin = hfsplus_write_begin, |
| 163 | .write_end = generic_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | .bmap = hfsplus_bmap, |
| 165 | .releasepage = hfsplus_releasepage, |
| 166 | }; |
| 167 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 168 | const struct address_space_operations hfsplus_aops = { |
Christoph Hellwig | 0af5737 | 2021-06-28 19:36:12 -0700 | [diff] [blame] | 169 | .set_page_dirty = __set_page_dirty_buffers, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | .readpage = hfsplus_readpage, |
| 171 | .writepage = hfsplus_writepage, |
Nick Piggin | 7c0efc6 | 2007-10-16 01:25:09 -0700 | [diff] [blame] | 172 | .write_begin = hfsplus_write_begin, |
| 173 | .write_end = generic_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | .bmap = hfsplus_bmap, |
| 175 | .direct_IO = hfsplus_direct_IO, |
| 176 | .writepages = hfsplus_writepages, |
| 177 | }; |
| 178 | |
Al Viro | e16404e | 2009-02-20 05:55:13 +0000 | [diff] [blame] | 179 | const struct dentry_operations hfsplus_dentry_operations = { |
Duane Griffin | d45bce8 | 2007-07-15 23:41:23 -0700 | [diff] [blame] | 180 | .d_hash = hfsplus_hash_dentry, |
| 181 | .d_compare = hfsplus_compare_dentry, |
| 182 | }; |
| 183 | |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 184 | static void hfsplus_get_perms(struct inode *inode, |
| 185 | struct hfsplus_perm *perms, int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 187 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | u16 mode; |
| 189 | |
| 190 | mode = be16_to_cpu(perms->mode); |
| 191 | |
Eric W. Biederman | 16525e3 | 2012-02-07 16:27:17 -0800 | [diff] [blame] | 192 | i_uid_write(inode, be32_to_cpu(perms->owner)); |
| 193 | if (!i_uid_read(inode) && !mode) |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 194 | inode->i_uid = sbi->uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Eric W. Biederman | 16525e3 | 2012-02-07 16:27:17 -0800 | [diff] [blame] | 196 | i_gid_write(inode, be32_to_cpu(perms->group)); |
| 197 | if (!i_gid_read(inode) && !mode) |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 198 | inode->i_gid = sbi->gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | if (dir) { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 201 | mode = mode ? (mode & S_IALLUGO) : (S_IRWXUGO & ~(sbi->umask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | mode |= S_IFDIR; |
| 203 | } else if (!mode) |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 204 | mode = S_IFREG | ((S_IRUGO|S_IWUGO) & ~(sbi->umask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | inode->i_mode = mode; |
| 206 | |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 207 | HFSPLUS_I(inode)->userflags = perms->userflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | if (perms->rootflags & HFSPLUS_FLG_IMMUTABLE) |
| 209 | inode->i_flags |= S_IMMUTABLE; |
| 210 | else |
| 211 | inode->i_flags &= ~S_IMMUTABLE; |
| 212 | if (perms->rootflags & HFSPLUS_FLG_APPEND) |
| 213 | inode->i_flags |= S_APPEND; |
| 214 | else |
| 215 | inode->i_flags &= ~S_APPEND; |
| 216 | } |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | static int hfsplus_file_open(struct inode *inode, struct file *file) |
| 219 | { |
| 220 | if (HFSPLUS_IS_RSRC(inode)) |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 221 | inode = HFSPLUS_I(inode)->rsrc_inode; |
Alan Cox | 6e71529 | 2008-10-18 20:28:01 -0700 | [diff] [blame] | 222 | if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS) |
| 223 | return -EOVERFLOW; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 224 | atomic_inc(&HFSPLUS_I(inode)->opencnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static int hfsplus_file_release(struct inode *inode, struct file *file) |
| 229 | { |
| 230 | struct super_block *sb = inode->i_sb; |
| 231 | |
| 232 | if (HFSPLUS_IS_RSRC(inode)) |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 233 | inode = HFSPLUS_I(inode)->rsrc_inode; |
| 234 | if (atomic_dec_and_test(&HFSPLUS_I(inode)->opencnt)) { |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 235 | inode_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | hfsplus_file_truncate(inode); |
| 237 | if (inode->i_flags & S_DEAD) { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 238 | hfsplus_delete_cat(inode->i_ino, |
| 239 | HFSPLUS_SB(sb)->hidden_dir, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | hfsplus_delete_inode(inode); |
| 241 | } |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 242 | inode_unlock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | return 0; |
| 245 | } |
| 246 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 247 | static int hfsplus_setattr(struct user_namespace *mnt_userns, |
| 248 | struct dentry *dentry, struct iattr *attr) |
Christoph Hellwig | d39aae9 | 2010-06-04 11:29:59 +0200 | [diff] [blame] | 249 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 250 | struct inode *inode = d_inode(dentry); |
Christoph Hellwig | d39aae9 | 2010-06-04 11:29:59 +0200 | [diff] [blame] | 251 | int error; |
| 252 | |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 253 | error = setattr_prepare(&init_user_ns, dentry, attr); |
Christoph Hellwig | d39aae9 | 2010-06-04 11:29:59 +0200 | [diff] [blame] | 254 | if (error) |
| 255 | return error; |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 256 | |
| 257 | if ((attr->ia_valid & ATTR_SIZE) && |
| 258 | attr->ia_size != i_size_read(inode)) { |
Christoph Hellwig | 562c72aa5 | 2011-06-24 14:29:45 -0400 | [diff] [blame] | 259 | inode_dio_wait(inode); |
Sergei Antonov | 059a704 | 2015-04-16 12:47:15 -0700 | [diff] [blame] | 260 | if (attr->ia_size > inode->i_size) { |
| 261 | error = generic_cont_expand_simple(inode, |
| 262 | attr->ia_size); |
| 263 | if (error) |
| 264 | return error; |
| 265 | } |
Marco Stornelli | d506848 | 2012-12-15 11:55:07 +0100 | [diff] [blame] | 266 | truncate_setsize(inode, attr->ia_size); |
| 267 | hfsplus_file_truncate(inode); |
Ernesto A. Fernández | dc8844a | 2018-10-30 15:06:27 -0700 | [diff] [blame] | 268 | inode->i_mtime = inode->i_ctime = current_time(inode); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Christian Brauner | 2f221d6 | 2021-01-21 14:19:26 +0100 | [diff] [blame] | 271 | setattr_copy(&init_user_ns, inode, attr); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 272 | mark_inode_dirty(inode); |
Vyacheslav Dubeyko | b4c1107 | 2013-09-11 14:24:30 -0700 | [diff] [blame] | 273 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 274 | return 0; |
Christoph Hellwig | d39aae9 | 2010-06-04 11:29:59 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 277 | int hfsplus_getattr(struct user_namespace *mnt_userns, const struct path *path, |
| 278 | struct kstat *stat, u32 request_mask, |
| 279 | unsigned int query_flags) |
Ernesto A. Fernández | f93ca1e | 2019-01-03 15:27:46 -0800 | [diff] [blame] | 280 | { |
| 281 | struct inode *inode = d_inode(path->dentry); |
| 282 | struct hfsplus_inode_info *hip = HFSPLUS_I(inode); |
| 283 | |
Chung-Chiang Cheng | c3eb840 | 2021-06-30 18:56:40 -0700 | [diff] [blame] | 284 | if (request_mask & STATX_BTIME) { |
| 285 | stat->result_mask |= STATX_BTIME; |
| 286 | stat->btime = hfsp_mt2ut(hip->create_date); |
| 287 | } |
| 288 | |
Ernesto A. Fernández | f93ca1e | 2019-01-03 15:27:46 -0800 | [diff] [blame] | 289 | if (inode->i_flags & S_APPEND) |
| 290 | stat->attributes |= STATX_ATTR_APPEND; |
| 291 | if (inode->i_flags & S_IMMUTABLE) |
| 292 | stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 293 | if (hip->userflags & HFSPLUS_FLG_NODUMP) |
| 294 | stat->attributes |= STATX_ATTR_NODUMP; |
| 295 | |
| 296 | stat->attributes_mask |= STATX_ATTR_APPEND | STATX_ATTR_IMMUTABLE | |
| 297 | STATX_ATTR_NODUMP; |
| 298 | |
Christian Brauner | 0d56a45 | 2021-01-21 14:19:30 +0100 | [diff] [blame] | 299 | generic_fillattr(&init_user_ns, inode, stat); |
Ernesto A. Fernández | f93ca1e | 2019-01-03 15:27:46 -0800 | [diff] [blame] | 300 | return 0; |
| 301 | } |
| 302 | |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 303 | int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end, |
| 304 | int datasync) |
Al Viro | b5fc510 | 2010-07-04 12:24:09 +0400 | [diff] [blame] | 305 | { |
Christoph Hellwig | 2814697 | 2010-11-23 14:38:06 +0100 | [diff] [blame] | 306 | struct inode *inode = file->f_mapping->host; |
Christoph Hellwig | e349470 | 2010-11-23 14:38:15 +0100 | [diff] [blame] | 307 | struct hfsplus_inode_info *hip = HFSPLUS_I(inode); |
Christoph Hellwig | 2814697 | 2010-11-23 14:38:06 +0100 | [diff] [blame] | 308 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb); |
Christoph Hellwig | e349470 | 2010-11-23 14:38:15 +0100 | [diff] [blame] | 309 | int error = 0, error2; |
Al Viro | b5fc510 | 2010-07-04 12:24:09 +0400 | [diff] [blame] | 310 | |
Jeff Layton | 3b49c9a | 2017-07-07 15:20:52 -0400 | [diff] [blame] | 311 | error = file_write_and_wait_range(file, start, end); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 312 | if (error) |
| 313 | return error; |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 314 | inode_lock(inode); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 315 | |
Christoph Hellwig | 2814697 | 2010-11-23 14:38:06 +0100 | [diff] [blame] | 316 | /* |
| 317 | * Sync inode metadata into the catalog and extent trees. |
| 318 | */ |
| 319 | sync_inode_metadata(inode, 1); |
Al Viro | b5fc510 | 2010-07-04 12:24:09 +0400 | [diff] [blame] | 320 | |
Christoph Hellwig | 2814697 | 2010-11-23 14:38:06 +0100 | [diff] [blame] | 321 | /* |
| 322 | * And explicitly write out the btrees. |
| 323 | */ |
Christoph Hellwig | e349470 | 2010-11-23 14:38:15 +0100 | [diff] [blame] | 324 | if (test_and_clear_bit(HFSPLUS_I_CAT_DIRTY, &hip->flags)) |
| 325 | error = filemap_write_and_wait(sbi->cat_tree->inode->i_mapping); |
| 326 | |
| 327 | if (test_and_clear_bit(HFSPLUS_I_EXT_DIRTY, &hip->flags)) { |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 328 | error2 = |
| 329 | filemap_write_and_wait(sbi->ext_tree->inode->i_mapping); |
Christoph Hellwig | e349470 | 2010-11-23 14:38:15 +0100 | [diff] [blame] | 330 | if (!error) |
| 331 | error = error2; |
| 332 | } |
| 333 | |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 334 | if (test_and_clear_bit(HFSPLUS_I_ATTR_DIRTY, &hip->flags)) { |
| 335 | if (sbi->attr_tree) { |
| 336 | error2 = |
| 337 | filemap_write_and_wait( |
| 338 | sbi->attr_tree->inode->i_mapping); |
| 339 | if (!error) |
| 340 | error = error2; |
| 341 | } else { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 342 | pr_err("sync non-existent attributes tree\n"); |
Vyacheslav Dubeyko | 324ef39 | 2013-02-27 17:03:04 -0800 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Christoph Hellwig | e349470 | 2010-11-23 14:38:15 +0100 | [diff] [blame] | 346 | if (test_and_clear_bit(HFSPLUS_I_ALLOC_DIRTY, &hip->flags)) { |
| 347 | error2 = filemap_write_and_wait(sbi->alloc_file->i_mapping); |
| 348 | if (!error) |
| 349 | error = error2; |
| 350 | } |
| 351 | |
Christoph Hellwig | 34a2d31 | 2010-11-23 14:38:21 +0100 | [diff] [blame] | 352 | if (!test_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags)) |
Christoph Hellwig | c6bf3f0 | 2021-01-26 15:52:35 +0100 | [diff] [blame] | 353 | blkdev_issue_flush(inode->i_sb->s_bdev); |
Christoph Hellwig | 34a2d31 | 2010-11-23 14:38:21 +0100 | [diff] [blame] | 354 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 355 | inode_unlock(inode); |
Josef Bacik | 02c24a8 | 2011-07-16 20:44:56 -0400 | [diff] [blame] | 356 | |
Christoph Hellwig | 2814697 | 2010-11-23 14:38:06 +0100 | [diff] [blame] | 357 | return error; |
Al Viro | b5fc510 | 2010-07-04 12:24:09 +0400 | [diff] [blame] | 358 | } |
| 359 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 360 | static const struct inode_operations hfsplus_file_inode_operations = { |
Christoph Hellwig | d39aae9 | 2010-06-04 11:29:59 +0200 | [diff] [blame] | 361 | .setattr = hfsplus_setattr, |
Ernesto A. Fernández | f93ca1e | 2019-01-03 15:27:46 -0800 | [diff] [blame] | 362 | .getattr = hfsplus_getattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | .listxattr = hfsplus_listxattr, |
Miklos Szeredi | 9cbae74 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 364 | .fileattr_get = hfsplus_fileattr_get, |
| 365 | .fileattr_set = hfsplus_fileattr_set, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | }; |
| 367 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 368 | static const struct file_operations hfsplus_file_operations = { |
Anton Salikhmetov | 20b7643 | 2010-12-16 18:08:40 +0200 | [diff] [blame] | 369 | .llseek = generic_file_llseek, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 370 | .read_iter = generic_file_read_iter, |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 371 | .write_iter = generic_file_write_iter, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | .mmap = generic_file_mmap, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 373 | .splice_read = generic_file_splice_read, |
Al Viro | b5fc510 | 2010-07-04 12:24:09 +0400 | [diff] [blame] | 374 | .fsync = hfsplus_file_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | .open = hfsplus_file_open, |
| 376 | .release = hfsplus_file_release, |
Arnd Bergmann | 7cc4bcc | 2010-04-27 16:24:20 +0200 | [diff] [blame] | 377 | .unlocked_ioctl = hfsplus_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | }; |
| 379 | |
Ernesto A. Fernandez | b0cd38c | 2018-02-06 15:49:02 -0800 | [diff] [blame] | 380 | struct inode *hfsplus_new_inode(struct super_block *sb, struct inode *dir, |
| 381 | umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 383 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | struct inode *inode = new_inode(sb); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 385 | struct hfsplus_inode_info *hip; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (!inode) |
| 388 | return NULL; |
| 389 | |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 390 | inode->i_ino = sbi->next_cnid++; |
Christian Brauner | 21cb47b | 2021-01-21 14:19:25 +0100 | [diff] [blame] | 391 | inode_init_owner(&init_user_ns, inode, dir, mode); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 392 | set_nlink(inode, 1); |
Deepa Dinamani | 02027d4 | 2016-09-14 07:48:05 -0700 | [diff] [blame] | 393 | inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 394 | |
| 395 | hip = HFSPLUS_I(inode); |
| 396 | INIT_LIST_HEAD(&hip->open_dir_list); |
Al Viro | 323ee8f | 2016-05-12 20:02:09 -0400 | [diff] [blame] | 397 | spin_lock_init(&hip->open_dir_lock); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 398 | mutex_init(&hip->extents_lock); |
| 399 | atomic_set(&hip->opencnt, 0); |
Christoph Hellwig | b33b792 | 2010-11-23 14:38:13 +0100 | [diff] [blame] | 400 | hip->extent_state = 0; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 401 | hip->flags = 0; |
Matthew Garrett | f392238 | 2012-03-13 12:10:34 -0400 | [diff] [blame] | 402 | hip->userflags = 0; |
Sergei Antonov | d7d673a | 2014-03-10 15:49:51 -0700 | [diff] [blame] | 403 | hip->subfolders = 0; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | if (S_ISDIR(inode->i_mode)) { |
| 414 | inode->i_size = 2; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 415 | sbi->folder_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | inode->i_op = &hfsplus_dir_inode_operations; |
| 417 | inode->i_fop = &hfsplus_dir_operations; |
| 418 | } else if (S_ISREG(inode->i_mode)) { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 419 | sbi->file_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | inode->i_op = &hfsplus_file_inode_operations; |
| 421 | inode->i_fop = &hfsplus_file_operations; |
| 422 | inode->i_mapping->a_ops = &hfsplus_aops; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 423 | hip->clump_blocks = sbi->data_clump_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | } else if (S_ISLNK(inode->i_mode)) { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 425 | sbi->file_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | inode->i_op = &page_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 427 | inode_nohighmem(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | inode->i_mapping->a_ops = &hfsplus_aops; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 429 | hip->clump_blocks = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } else |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 431 | sbi->file_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | insert_inode_hash(inode); |
| 433 | mark_inode_dirty(inode); |
Artem Bityutskiy | 9e6c582 | 2012-07-12 17:26:31 +0300 | [diff] [blame] | 434 | hfsplus_mark_mdb_dirty(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
| 436 | return inode; |
| 437 | } |
| 438 | |
| 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)) { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 444 | HFSPLUS_SB(sb)->folder_count--; |
Artem Bityutskiy | 9e6c582 | 2012-07-12 17:26:31 +0300 | [diff] [blame] | 445 | hfsplus_mark_mdb_dirty(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | return; |
| 447 | } |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 448 | HFSPLUS_SB(sb)->file_count--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Artem Bityutskiy | 9e6c582 | 2012-07-12 17:26:31 +0300 | [diff] [blame] | 458 | hfsplus_mark_mdb_dirty(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void hfsplus_inode_read_fork(struct inode *inode, struct hfsplus_fork_raw *fork) |
| 462 | { |
| 463 | struct super_block *sb = inode->i_sb; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 464 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 465 | struct hfsplus_inode_info *hip = HFSPLUS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | u32 count; |
| 467 | int i; |
| 468 | |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 469 | memcpy(&hip->first_extents, &fork->extents, sizeof(hfsplus_extent_rec)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | for (count = 0, i = 0; i < 8; i++) |
| 471 | count += be32_to_cpu(fork->extents[i].block_count); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 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 = |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 483 | be32_to_cpu(fork->clump_size) >> sbi->alloc_blksz_shift; |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 484 | if (!hip->clump_blocks) { |
| 485 | hip->clump_blocks = HFSPLUS_IS_RSRC(inode) ? |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 486 | sbi->rsrc_clump_blocks : |
| 487 | sbi->data_clump_blocks; |
| 488 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 491 | void hfsplus_inode_write_fork(struct inode *inode, |
| 492 | struct hfsplus_fork_raw *fork) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 494 | memcpy(&fork->extents, &HFSPLUS_I(inode)->first_extents, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | sizeof(hfsplus_extent_rec)); |
| 496 | fork->total_size = cpu_to_be64(inode->i_size); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 497 | fork->total_blocks = cpu_to_be32(HFSPLUS_I(inode)->alloc_blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 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 | |
Christoph Hellwig | f6089ff | 2010-10-14 09:54:28 -0400 | [diff] [blame] | 508 | HFSPLUS_I(inode)->linkid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | if (type == HFSPLUS_FOLDER) { |
| 510 | struct hfsplus_cat_folder *folder = &entry.folder; |
| 511 | |
Arnd Bergmann | 55d1cbb | 2021-11-08 18:35:04 -0800 | [diff] [blame] | 512 | WARN_ON(fd->entrylength < sizeof(struct hfsplus_cat_folder)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | hfs_bnode_read(fd->bnode, &entry, fd->entryoffset, |
| 514 | sizeof(struct hfsplus_cat_folder)); |
| 515 | hfsplus_get_perms(inode, &folder->permissions, 1); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 516 | set_nlink(inode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | inode->i_size = 2 + be32_to_cpu(folder->valence); |
Arnd Bergmann | 4ddfc3dc | 2018-06-20 09:47:26 +0200 | [diff] [blame] | 518 | inode->i_atime = hfsp_mt2ut(folder->access_date); |
| 519 | inode->i_mtime = hfsp_mt2ut(folder->content_mod_date); |
| 520 | inode->i_ctime = hfsp_mt2ut(folder->attribute_mod_date); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 521 | HFSPLUS_I(inode)->create_date = folder->create_date; |
| 522 | HFSPLUS_I(inode)->fs_blocks = 0; |
Sergei Antonov | d7d673a | 2014-03-10 15:49:51 -0700 | [diff] [blame] | 523 | if (folder->flags & cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT)) { |
| 524 | HFSPLUS_I(inode)->subfolders = |
| 525 | be32_to_cpu(folder->subfolders); |
| 526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | inode->i_op = &hfsplus_dir_inode_operations; |
| 528 | inode->i_fop = &hfsplus_dir_operations; |
| 529 | } else if (type == HFSPLUS_FILE) { |
| 530 | struct hfsplus_cat_file *file = &entry.file; |
| 531 | |
Arnd Bergmann | 55d1cbb | 2021-11-08 18:35:04 -0800 | [diff] [blame] | 532 | WARN_ON(fd->entrylength < sizeof(struct hfsplus_cat_file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | hfs_bnode_read(fd->bnode, &entry, fd->entryoffset, |
| 534 | sizeof(struct hfsplus_cat_file)); |
| 535 | |
Christoph Hellwig | b33b792 | 2010-11-23 14:38:13 +0100 | [diff] [blame] | 536 | hfsplus_inode_read_fork(inode, HFSPLUS_IS_RSRC(inode) ? |
| 537 | &file->rsrc_fork : &file->data_fork); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | hfsplus_get_perms(inode, &file->permissions, 0); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 539 | set_nlink(inode, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | if (S_ISREG(inode->i_mode)) { |
| 541 | if (file->permissions.dev) |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 542 | set_nlink(inode, |
| 543 | be32_to_cpu(file->permissions.dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | inode->i_op = &hfsplus_file_inode_operations; |
| 545 | inode->i_fop = &hfsplus_file_operations; |
| 546 | inode->i_mapping->a_ops = &hfsplus_aops; |
| 547 | } else if (S_ISLNK(inode->i_mode)) { |
| 548 | inode->i_op = &page_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 549 | inode_nohighmem(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | inode->i_mapping->a_ops = &hfsplus_aops; |
| 551 | } else { |
| 552 | init_special_inode(inode, inode->i_mode, |
| 553 | be32_to_cpu(file->permissions.dev)); |
| 554 | } |
Arnd Bergmann | 4ddfc3dc | 2018-06-20 09:47:26 +0200 | [diff] [blame] | 555 | inode->i_atime = hfsp_mt2ut(file->access_date); |
| 556 | inode->i_mtime = hfsp_mt2ut(file->content_mod_date); |
| 557 | inode->i_ctime = hfsp_mt2ut(file->attribute_mod_date); |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 558 | HFSPLUS_I(inode)->create_date = file->create_date; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } else { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 560 | pr_err("bad catalog entry used to create inode\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | res = -EIO; |
| 562 | } |
| 563 | return res; |
| 564 | } |
| 565 | |
| 566 | int hfsplus_cat_write_inode(struct inode *inode) |
| 567 | { |
| 568 | struct inode *main_inode = inode; |
| 569 | struct hfs_find_data fd; |
| 570 | hfsplus_cat_entry entry; |
| 571 | |
| 572 | if (HFSPLUS_IS_RSRC(inode)) |
Christoph Hellwig | 6af502d | 2010-10-01 05:43:31 +0200 | [diff] [blame] | 573 | main_inode = HFSPLUS_I(inode)->rsrc_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
| 575 | if (!main_inode->i_nlink) |
| 576 | return 0; |
| 577 | |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 578 | if (hfs_find_init(HFSPLUS_SB(main_inode->i_sb)->cat_tree, &fd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | /* panic? */ |
| 580 | return -EIO; |
| 581 | |
| 582 | if (hfsplus_find_cat(main_inode->i_sb, main_inode->i_ino, &fd)) |
| 583 | /* panic? */ |
| 584 | goto out; |
| 585 | |
| 586 | if (S_ISDIR(main_inode->i_mode)) { |
| 587 | struct hfsplus_cat_folder *folder = &entry.folder; |
| 588 | |
Arnd Bergmann | 55d1cbb | 2021-11-08 18:35:04 -0800 | [diff] [blame] | 589 | WARN_ON(fd.entrylength < sizeof(struct hfsplus_cat_folder)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, |
| 591 | sizeof(struct hfsplus_cat_folder)); |
| 592 | /* simple node checks? */ |
Christoph Hellwig | 90e6169 | 2010-10-14 09:54:39 -0400 | [diff] [blame] | 593 | hfsplus_cat_set_perms(inode, &folder->permissions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | folder->access_date = hfsp_ut2mt(inode->i_atime); |
| 595 | folder->content_mod_date = hfsp_ut2mt(inode->i_mtime); |
| 596 | folder->attribute_mod_date = hfsp_ut2mt(inode->i_ctime); |
| 597 | folder->valence = cpu_to_be32(inode->i_size - 2); |
Sergei Antonov | d7d673a | 2014-03-10 15:49:51 -0700 | [diff] [blame] | 598 | if (folder->flags & cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT)) { |
| 599 | folder->subfolders = |
| 600 | cpu_to_be32(HFSPLUS_I(inode)->subfolders); |
| 601 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | hfs_bnode_write(fd.bnode, &entry, fd.entryoffset, |
| 603 | sizeof(struct hfsplus_cat_folder)); |
| 604 | } else if (HFSPLUS_IS_RSRC(inode)) { |
| 605 | struct hfsplus_cat_file *file = &entry.file; |
| 606 | hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, |
| 607 | sizeof(struct hfsplus_cat_file)); |
| 608 | hfsplus_inode_write_fork(inode, &file->rsrc_fork); |
| 609 | hfs_bnode_write(fd.bnode, &entry, fd.entryoffset, |
| 610 | sizeof(struct hfsplus_cat_file)); |
| 611 | } else { |
| 612 | struct hfsplus_cat_file *file = &entry.file; |
| 613 | |
Arnd Bergmann | 55d1cbb | 2021-11-08 18:35:04 -0800 | [diff] [blame] | 614 | WARN_ON(fd.entrylength < sizeof(struct hfsplus_cat_file)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, |
| 616 | sizeof(struct hfsplus_cat_file)); |
| 617 | hfsplus_inode_write_fork(inode, &file->data_fork); |
Christoph Hellwig | 90e6169 | 2010-10-14 09:54:39 -0400 | [diff] [blame] | 618 | hfsplus_cat_set_perms(inode, &file->permissions); |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 619 | if (HFSPLUS_FLG_IMMUTABLE & |
| 620 | (file->permissions.rootflags | |
| 621 | file->permissions.userflags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED); |
| 623 | else |
| 624 | file->flags &= cpu_to_be16(~HFSPLUS_FILE_LOCKED); |
| 625 | file->access_date = hfsp_ut2mt(inode->i_atime); |
| 626 | file->content_mod_date = hfsp_ut2mt(inode->i_mtime); |
| 627 | file->attribute_mod_date = hfsp_ut2mt(inode->i_ctime); |
| 628 | hfs_bnode_write(fd.bnode, &entry, fd.entryoffset, |
| 629 | sizeof(struct hfsplus_cat_file)); |
| 630 | } |
Christoph Hellwig | e349470 | 2010-11-23 14:38:15 +0100 | [diff] [blame] | 631 | |
| 632 | set_bit(HFSPLUS_I_CAT_DIRTY, &HFSPLUS_I(inode)->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | out: |
| 634 | hfs_find_exit(&fd); |
| 635 | return 0; |
| 636 | } |
Miklos Szeredi | 9cbae74 | 2021-04-07 14:36:44 +0200 | [diff] [blame] | 637 | |
| 638 | int hfsplus_fileattr_get(struct dentry *dentry, struct fileattr *fa) |
| 639 | { |
| 640 | struct inode *inode = d_inode(dentry); |
| 641 | struct hfsplus_inode_info *hip = HFSPLUS_I(inode); |
| 642 | unsigned int flags = 0; |
| 643 | |
| 644 | if (inode->i_flags & S_IMMUTABLE) |
| 645 | flags |= FS_IMMUTABLE_FL; |
| 646 | if (inode->i_flags & S_APPEND) |
| 647 | flags |= FS_APPEND_FL; |
| 648 | if (hip->userflags & HFSPLUS_FLG_NODUMP) |
| 649 | flags |= FS_NODUMP_FL; |
| 650 | |
| 651 | fileattr_fill_flags(fa, flags); |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | int hfsplus_fileattr_set(struct user_namespace *mnt_userns, |
| 657 | struct dentry *dentry, struct fileattr *fa) |
| 658 | { |
| 659 | struct inode *inode = d_inode(dentry); |
| 660 | struct hfsplus_inode_info *hip = HFSPLUS_I(inode); |
| 661 | unsigned int new_fl = 0; |
| 662 | |
| 663 | if (fileattr_has_fsx(fa)) |
| 664 | return -EOPNOTSUPP; |
| 665 | |
| 666 | /* don't silently ignore unsupported ext2 flags */ |
| 667 | if (fa->flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL)) |
| 668 | return -EOPNOTSUPP; |
| 669 | |
| 670 | if (fa->flags & FS_IMMUTABLE_FL) |
| 671 | new_fl |= S_IMMUTABLE; |
| 672 | |
| 673 | if (fa->flags & FS_APPEND_FL) |
| 674 | new_fl |= S_APPEND; |
| 675 | |
| 676 | inode_set_flags(inode, new_fl, S_IMMUTABLE | S_APPEND); |
| 677 | |
| 678 | if (fa->flags & FS_NODUMP_FL) |
| 679 | hip->userflags |= HFSPLUS_FLG_NODUMP; |
| 680 | else |
| 681 | hip->userflags &= ~HFSPLUS_FLG_NODUMP; |
| 682 | |
| 683 | inode->i_ctime = current_time(inode); |
| 684 | mark_inode_dirty(inode); |
| 685 | |
| 686 | return 0; |
| 687 | } |