blob: 6fef67c2a9f0908290bf34ec1437019e3b0b1fc4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Hellwig34a2d312010-11-23 14:38:21 +010012#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/mm.h>
14#include <linux/fs.h>
15#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/mpage.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040017#include <linux/sched.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +010018#include <linux/cred.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080019#include <linux/uio.h>
Miklos Szeredi9cbae742021-04-07 14:36:44 +020020#include <linux/fileattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include "hfsplus_fs.h"
23#include "hfsplus_raw.h"
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -080024#include "xattr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static int hfsplus_readpage(struct file *file, struct page *page)
27{
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 return block_read_full_page(page, hfsplus_get_block);
29}
30
31static int hfsplus_writepage(struct page *page, struct writeback_control *wbc)
32{
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 return block_write_full_page(page, hfsplus_get_block, wbc);
34}
35
Marco Stornellid5068482012-12-15 11:55:07 +010036static 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. Shutemov7caef262013-09-12 15:13:56 -070041 truncate_pagecache(inode, inode->i_size);
Marco Stornellid5068482012-12-15 11:55:07 +010042 hfsplus_file_truncate(inode);
43 }
44}
45
Nick Piggin7c0efc62007-10-16 01:25:09 -070046static 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 Torvalds1da177e2005-04-16 15:20:36 -070049{
Christoph Hellwig282dc172010-06-04 11:29:55 +020050 int ret;
51
Nick Piggin7c0efc62007-10-16 01:25:09 -070052 *pagep = NULL;
Christoph Hellwig282dc172010-06-04 11:29:55 +020053 ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
Nick Piggin7c0efc62007-10-16 01:25:09 -070054 hfsplus_get_block,
Christoph Hellwig6af502d2010-10-01 05:43:31 +020055 &HFSPLUS_I(mapping->host)->phys_size);
Marco Stornellid5068482012-12-15 11:55:07 +010056 if (unlikely(ret))
57 hfsplus_write_failed(mapping, pos + len);
Christoph Hellwig282dc172010-06-04 11:29:55 +020058
59 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
62static 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 Viro27496a82005-10-21 03:20:48 -040067static int hfsplus_releasepage(struct page *page, gfp_t mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
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 Hellwigdd73a012010-10-01 05:42:59 +020078 tree = HFSPLUS_SB(sb)->ext_tree;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 break;
80 case HFSPLUS_CAT_CNID:
Christoph Hellwigdd73a012010-10-01 05:42:59 +020081 tree = HFSPLUS_SB(sb)->cat_tree;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break;
83 case HFSPLUS_ATTR_CNID:
Christoph Hellwigdd73a012010-10-01 05:42:59 +020084 tree = HFSPLUS_SB(sb)->attr_tree;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 break;
86 default:
87 BUG();
88 return 0;
89 }
Eric Sesterhenn70632242008-05-12 14:02:21 -070090 if (!tree)
91 return 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030092 if (tree->node_size >= PAGE_SIZE) {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020093 nidx = page->index >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030094 (tree->node_size_shift - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 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 Salikhmetov2753cc22010-12-16 18:08:38 +0200107 nidx = page->index <<
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300108 (PAGE_SHIFT - tree->node_size_shift);
109 i = 1 << (PAGE_SHIFT - tree->node_size_shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 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 Torvalds1da177e2005-04-16 15:20:36 -0700124 return res ? try_to_free_buffers(page) : 0;
125}
126
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700127static ssize_t hfsplus_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct file *file = iocb->ki_filp;
Marco Stornellid5068482012-12-15 11:55:07 +0100130 struct address_space *mapping = file->f_mapping;
Al Viro93c76a32015-12-04 23:45:44 -0500131 struct inode *inode = mapping->host;
Al Viroa6cbcd42014-03-04 22:38:00 -0500132 size_t count = iov_iter_count(iter);
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +0200133 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700135 ret = blockdev_direct_IO(iocb, inode, iter, hfsplus_get_block);
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +0200136
137 /*
138 * In case of error extending write may have instantiated a few
139 * blocks outside i_size. Trim these off again.
140 */
Omar Sandoval6f673762015-03-16 04:33:52 -0700141 if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) {
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +0200142 loff_t isize = i_size_read(inode);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -0700143 loff_t end = iocb->ki_pos + count;
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +0200144
145 if (end > isize)
Marco Stornellid5068482012-12-15 11:55:07 +0100146 hfsplus_write_failed(mapping, end);
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +0200147 }
148
149 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152static 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 Hellwigf5e54d62006-06-28 04:26:44 -0700158const struct address_space_operations hfsplus_btree_aops = {
Christoph Hellwig0af57372021-06-28 19:36:12 -0700159 .set_page_dirty = __set_page_dirty_buffers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 .readpage = hfsplus_readpage,
161 .writepage = hfsplus_writepage,
Nick Piggin7c0efc62007-10-16 01:25:09 -0700162 .write_begin = hfsplus_write_begin,
163 .write_end = generic_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 .bmap = hfsplus_bmap,
165 .releasepage = hfsplus_releasepage,
166};
167
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700168const struct address_space_operations hfsplus_aops = {
Christoph Hellwig0af57372021-06-28 19:36:12 -0700169 .set_page_dirty = __set_page_dirty_buffers,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .readpage = hfsplus_readpage,
171 .writepage = hfsplus_writepage,
Nick Piggin7c0efc62007-10-16 01:25:09 -0700172 .write_begin = hfsplus_write_begin,
173 .write_end = generic_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 .bmap = hfsplus_bmap,
175 .direct_IO = hfsplus_direct_IO,
176 .writepages = hfsplus_writepages,
177};
178
Al Viroe16404e2009-02-20 05:55:13 +0000179const struct dentry_operations hfsplus_dentry_operations = {
Duane Griffind45bce82007-07-15 23:41:23 -0700180 .d_hash = hfsplus_hash_dentry,
181 .d_compare = hfsplus_compare_dentry,
182};
183
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200184static void hfsplus_get_perms(struct inode *inode,
185 struct hfsplus_perm *perms, int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200187 struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 u16 mode;
189
190 mode = be16_to_cpu(perms->mode);
191
Eric W. Biederman16525e32012-02-07 16:27:17 -0800192 i_uid_write(inode, be32_to_cpu(perms->owner));
193 if (!i_uid_read(inode) && !mode)
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200194 inode->i_uid = sbi->uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Eric W. Biederman16525e32012-02-07 16:27:17 -0800196 i_gid_write(inode, be32_to_cpu(perms->group));
197 if (!i_gid_read(inode) && !mode)
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200198 inode->i_gid = sbi->gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 if (dir) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200201 mode = mode ? (mode & S_IALLUGO) : (S_IRWXUGO & ~(sbi->umask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 mode |= S_IFDIR;
203 } else if (!mode)
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200204 mode = S_IFREG | ((S_IRUGO|S_IWUGO) & ~(sbi->umask));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 inode->i_mode = mode;
206
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200207 HFSPLUS_I(inode)->userflags = perms->userflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 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 Torvalds1da177e2005-04-16 15:20:36 -0700218static int hfsplus_file_open(struct inode *inode, struct file *file)
219{
220 if (HFSPLUS_IS_RSRC(inode))
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200221 inode = HFSPLUS_I(inode)->rsrc_inode;
Alan Cox6e715292008-10-18 20:28:01 -0700222 if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
223 return -EOVERFLOW;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200224 atomic_inc(&HFSPLUS_I(inode)->opencnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return 0;
226}
227
228static 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 Hellwig6af502d2010-10-01 05:43:31 +0200233 inode = HFSPLUS_I(inode)->rsrc_inode;
234 if (atomic_dec_and_test(&HFSPLUS_I(inode)->opencnt)) {
Al Viro59551022016-01-22 15:40:57 -0500235 inode_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 hfsplus_file_truncate(inode);
237 if (inode->i_flags & S_DEAD) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200238 hfsplus_delete_cat(inode->i_ino,
239 HFSPLUS_SB(sb)->hidden_dir, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 hfsplus_delete_inode(inode);
241 }
Al Viro59551022016-01-22 15:40:57 -0500242 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 return 0;
245}
246
Christian Brauner549c7292021-01-21 14:19:43 +0100247static int hfsplus_setattr(struct user_namespace *mnt_userns,
248 struct dentry *dentry, struct iattr *attr)
Christoph Hellwigd39aae92010-06-04 11:29:59 +0200249{
David Howells2b0143b2015-03-17 22:25:59 +0000250 struct inode *inode = d_inode(dentry);
Christoph Hellwigd39aae92010-06-04 11:29:59 +0200251 int error;
252
Christian Brauner2f221d62021-01-21 14:19:26 +0100253 error = setattr_prepare(&init_user_ns, dentry, attr);
Christoph Hellwigd39aae92010-06-04 11:29:59 +0200254 if (error)
255 return error;
Christoph Hellwig10257742010-06-04 11:30:02 +0200256
257 if ((attr->ia_valid & ATTR_SIZE) &&
258 attr->ia_size != i_size_read(inode)) {
Christoph Hellwig562c72aa52011-06-24 14:29:45 -0400259 inode_dio_wait(inode);
Sergei Antonov059a7042015-04-16 12:47:15 -0700260 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 Stornellid5068482012-12-15 11:55:07 +0100266 truncate_setsize(inode, attr->ia_size);
267 hfsplus_file_truncate(inode);
Ernesto A. Fernándezdc8844a2018-10-30 15:06:27 -0700268 inode->i_mtime = inode->i_ctime = current_time(inode);
Christoph Hellwig10257742010-06-04 11:30:02 +0200269 }
270
Christian Brauner2f221d62021-01-21 14:19:26 +0100271 setattr_copy(&init_user_ns, inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200272 mark_inode_dirty(inode);
Vyacheslav Dubeykob4c11072013-09-11 14:24:30 -0700273
Christoph Hellwig10257742010-06-04 11:30:02 +0200274 return 0;
Christoph Hellwigd39aae92010-06-04 11:29:59 +0200275}
276
Christian Brauner549c7292021-01-21 14:19:43 +0100277int 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ándezf93ca1e2019-01-03 15:27:46 -0800280{
281 struct inode *inode = d_inode(path->dentry);
282 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
283
Chung-Chiang Chengc3eb8402021-06-30 18:56:40 -0700284 if (request_mask & STATX_BTIME) {
285 stat->result_mask |= STATX_BTIME;
286 stat->btime = hfsp_mt2ut(hip->create_date);
287 }
288
Ernesto A. Fernándezf93ca1e2019-01-03 15:27:46 -0800289 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 Brauner0d56a452021-01-21 14:19:30 +0100299 generic_fillattr(&init_user_ns, inode, stat);
Ernesto A. Fernándezf93ca1e2019-01-03 15:27:46 -0800300 return 0;
301}
302
Josef Bacik02c24a82011-07-16 20:44:56 -0400303int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end,
304 int datasync)
Al Virob5fc5102010-07-04 12:24:09 +0400305{
Christoph Hellwig28146972010-11-23 14:38:06 +0100306 struct inode *inode = file->f_mapping->host;
Christoph Hellwige3494702010-11-23 14:38:15 +0100307 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
Christoph Hellwig28146972010-11-23 14:38:06 +0100308 struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
Christoph Hellwige3494702010-11-23 14:38:15 +0100309 int error = 0, error2;
Al Virob5fc5102010-07-04 12:24:09 +0400310
Jeff Layton3b49c9a2017-07-07 15:20:52 -0400311 error = file_write_and_wait_range(file, start, end);
Josef Bacik02c24a82011-07-16 20:44:56 -0400312 if (error)
313 return error;
Al Viro59551022016-01-22 15:40:57 -0500314 inode_lock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400315
Christoph Hellwig28146972010-11-23 14:38:06 +0100316 /*
317 * Sync inode metadata into the catalog and extent trees.
318 */
319 sync_inode_metadata(inode, 1);
Al Virob5fc5102010-07-04 12:24:09 +0400320
Christoph Hellwig28146972010-11-23 14:38:06 +0100321 /*
322 * And explicitly write out the btrees.
323 */
Christoph Hellwige3494702010-11-23 14:38:15 +0100324 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 Salikhmetov2753cc22010-12-16 18:08:38 +0200328 error2 =
329 filemap_write_and_wait(sbi->ext_tree->inode->i_mapping);
Christoph Hellwige3494702010-11-23 14:38:15 +0100330 if (!error)
331 error = error2;
332 }
333
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800334 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 Perchesd6142672013-04-30 15:27:55 -0700342 pr_err("sync non-existent attributes tree\n");
Vyacheslav Dubeyko324ef392013-02-27 17:03:04 -0800343 }
344 }
345
Christoph Hellwige3494702010-11-23 14:38:15 +0100346 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 Hellwig34a2d312010-11-23 14:38:21 +0100352 if (!test_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags))
Christoph Hellwigc6bf3f02021-01-26 15:52:35 +0100353 blkdev_issue_flush(inode->i_sb->s_bdev);
Christoph Hellwig34a2d312010-11-23 14:38:21 +0100354
Al Viro59551022016-01-22 15:40:57 -0500355 inode_unlock(inode);
Josef Bacik02c24a82011-07-16 20:44:56 -0400356
Christoph Hellwig28146972010-11-23 14:38:06 +0100357 return error;
Al Virob5fc5102010-07-04 12:24:09 +0400358}
359
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800360static const struct inode_operations hfsplus_file_inode_operations = {
Christoph Hellwigd39aae92010-06-04 11:29:59 +0200361 .setattr = hfsplus_setattr,
Ernesto A. Fernándezf93ca1e2019-01-03 15:27:46 -0800362 .getattr = hfsplus_getattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 .listxattr = hfsplus_listxattr,
Miklos Szeredi9cbae742021-04-07 14:36:44 +0200364 .fileattr_get = hfsplus_fileattr_get,
365 .fileattr_set = hfsplus_fileattr_set,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366};
367
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800368static const struct file_operations hfsplus_file_operations = {
Anton Salikhmetov20b76432010-12-16 18:08:40 +0200369 .llseek = generic_file_llseek,
Al Viroaad4f8b2014-04-02 14:33:16 -0400370 .read_iter = generic_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -0400371 .write_iter = generic_file_write_iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 .mmap = generic_file_mmap,
Jens Axboe5ffc4ef2007-06-01 11:49:19 +0200373 .splice_read = generic_file_splice_read,
Al Virob5fc5102010-07-04 12:24:09 +0400374 .fsync = hfsplus_file_fsync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 .open = hfsplus_file_open,
376 .release = hfsplus_file_release,
Arnd Bergmann7cc4bcc2010-04-27 16:24:20 +0200377 .unlocked_ioctl = hfsplus_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378};
379
Ernesto A. Fernandezb0cd38c2018-02-06 15:49:02 -0800380struct inode *hfsplus_new_inode(struct super_block *sb, struct inode *dir,
381 umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200383 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct inode *inode = new_inode(sb);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200385 struct hfsplus_inode_info *hip;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!inode)
388 return NULL;
389
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200390 inode->i_ino = sbi->next_cnid++;
Christian Brauner21cb47b2021-01-21 14:19:25 +0100391 inode_init_owner(&init_user_ns, inode, dir, mode);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200392 set_nlink(inode, 1);
Deepa Dinamani02027d42016-09-14 07:48:05 -0700393 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200394
395 hip = HFSPLUS_I(inode);
396 INIT_LIST_HEAD(&hip->open_dir_list);
Al Viro323ee8f2016-05-12 20:02:09 -0400397 spin_lock_init(&hip->open_dir_lock);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200398 mutex_init(&hip->extents_lock);
399 atomic_set(&hip->opencnt, 0);
Christoph Hellwigb33b7922010-11-23 14:38:13 +0100400 hip->extent_state = 0;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200401 hip->flags = 0;
Matthew Garrettf3922382012-03-13 12:10:34 -0400402 hip->userflags = 0;
Sergei Antonovd7d673a2014-03-10 15:49:51 -0700403 hip->subfolders = 0;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200404 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 Torvalds1da177e2005-04-16 15:20:36 -0700413 if (S_ISDIR(inode->i_mode)) {
414 inode->i_size = 2;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200415 sbi->folder_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 inode->i_op = &hfsplus_dir_inode_operations;
417 inode->i_fop = &hfsplus_dir_operations;
418 } else if (S_ISREG(inode->i_mode)) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200419 sbi->file_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 inode->i_op = &hfsplus_file_inode_operations;
421 inode->i_fop = &hfsplus_file_operations;
422 inode->i_mapping->a_ops = &hfsplus_aops;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200423 hip->clump_blocks = sbi->data_clump_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 } else if (S_ISLNK(inode->i_mode)) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200425 sbi->file_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 inode->i_op = &page_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500427 inode_nohighmem(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 inode->i_mapping->a_ops = &hfsplus_aops;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200429 hip->clump_blocks = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 } else
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200431 sbi->file_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 insert_inode_hash(inode);
433 mark_inode_dirty(inode);
Artem Bityutskiy9e6c5822012-07-12 17:26:31 +0300434 hfsplus_mark_mdb_dirty(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 return inode;
437}
438
439void hfsplus_delete_inode(struct inode *inode)
440{
441 struct super_block *sb = inode->i_sb;
442
443 if (S_ISDIR(inode->i_mode)) {
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200444 HFSPLUS_SB(sb)->folder_count--;
Artem Bityutskiy9e6c5822012-07-12 17:26:31 +0300445 hfsplus_mark_mdb_dirty(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return;
447 }
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200448 HFSPLUS_SB(sb)->file_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 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 Bityutskiy9e6c5822012-07-12 17:26:31 +0300458 hfsplus_mark_mdb_dirty(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461void hfsplus_inode_read_fork(struct inode *inode, struct hfsplus_fork_raw *fork)
462{
463 struct super_block *sb = inode->i_sb;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200464 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200465 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 u32 count;
467 int i;
468
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200469 memcpy(&hip->first_extents, &fork->extents, sizeof(hfsplus_extent_rec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 for (count = 0, i = 0; i < 8; i++)
471 count += be32_to_cpu(fork->extents[i].block_count);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200472 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 Torvalds1da177e2005-04-16 15:20:36 -0700476
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200477 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 Hellwigdd73a012010-10-01 05:42:59 +0200483 be32_to_cpu(fork->clump_size) >> sbi->alloc_blksz_shift;
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200484 if (!hip->clump_blocks) {
485 hip->clump_blocks = HFSPLUS_IS_RSRC(inode) ?
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200486 sbi->rsrc_clump_blocks :
487 sbi->data_clump_blocks;
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200491void hfsplus_inode_write_fork(struct inode *inode,
492 struct hfsplus_fork_raw *fork)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200494 memcpy(&fork->extents, &HFSPLUS_I(inode)->first_extents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 sizeof(hfsplus_extent_rec));
496 fork->total_size = cpu_to_be64(inode->i_size);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200497 fork->total_blocks = cpu_to_be32(HFSPLUS_I(inode)->alloc_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500int 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 Hellwigf6089ff2010-10-14 09:54:28 -0400508 HFSPLUS_I(inode)->linkid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (type == HFSPLUS_FOLDER) {
510 struct hfsplus_cat_folder *folder = &entry.folder;
511
512 if (fd->entrylength < sizeof(struct hfsplus_cat_folder))
513 /* panic? */;
514 hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
515 sizeof(struct hfsplus_cat_folder));
516 hfsplus_get_perms(inode, &folder->permissions, 1);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200517 set_nlink(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 inode->i_size = 2 + be32_to_cpu(folder->valence);
Arnd Bergmann4ddfc3dc2018-06-20 09:47:26 +0200519 inode->i_atime = hfsp_mt2ut(folder->access_date);
520 inode->i_mtime = hfsp_mt2ut(folder->content_mod_date);
521 inode->i_ctime = hfsp_mt2ut(folder->attribute_mod_date);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200522 HFSPLUS_I(inode)->create_date = folder->create_date;
523 HFSPLUS_I(inode)->fs_blocks = 0;
Sergei Antonovd7d673a2014-03-10 15:49:51 -0700524 if (folder->flags & cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT)) {
525 HFSPLUS_I(inode)->subfolders =
526 be32_to_cpu(folder->subfolders);
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 inode->i_op = &hfsplus_dir_inode_operations;
529 inode->i_fop = &hfsplus_dir_operations;
530 } else if (type == HFSPLUS_FILE) {
531 struct hfsplus_cat_file *file = &entry.file;
532
533 if (fd->entrylength < sizeof(struct hfsplus_cat_file))
534 /* panic? */;
535 hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
536 sizeof(struct hfsplus_cat_file));
537
Christoph Hellwigb33b7922010-11-23 14:38:13 +0100538 hfsplus_inode_read_fork(inode, HFSPLUS_IS_RSRC(inode) ?
539 &file->rsrc_fork : &file->data_fork);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 hfsplus_get_perms(inode, &file->permissions, 0);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200541 set_nlink(inode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (S_ISREG(inode->i_mode)) {
543 if (file->permissions.dev)
Miklos Szeredibfe86842011-10-28 14:13:29 +0200544 set_nlink(inode,
545 be32_to_cpu(file->permissions.dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 inode->i_op = &hfsplus_file_inode_operations;
547 inode->i_fop = &hfsplus_file_operations;
548 inode->i_mapping->a_ops = &hfsplus_aops;
549 } else if (S_ISLNK(inode->i_mode)) {
550 inode->i_op = &page_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -0500551 inode_nohighmem(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 inode->i_mapping->a_ops = &hfsplus_aops;
553 } else {
554 init_special_inode(inode, inode->i_mode,
555 be32_to_cpu(file->permissions.dev));
556 }
Arnd Bergmann4ddfc3dc2018-06-20 09:47:26 +0200557 inode->i_atime = hfsp_mt2ut(file->access_date);
558 inode->i_mtime = hfsp_mt2ut(file->content_mod_date);
559 inode->i_ctime = hfsp_mt2ut(file->attribute_mod_date);
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200560 HFSPLUS_I(inode)->create_date = file->create_date;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 } else {
Joe Perchesd6142672013-04-30 15:27:55 -0700562 pr_err("bad catalog entry used to create inode\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 res = -EIO;
564 }
565 return res;
566}
567
568int hfsplus_cat_write_inode(struct inode *inode)
569{
570 struct inode *main_inode = inode;
571 struct hfs_find_data fd;
572 hfsplus_cat_entry entry;
573
574 if (HFSPLUS_IS_RSRC(inode))
Christoph Hellwig6af502d2010-10-01 05:43:31 +0200575 main_inode = HFSPLUS_I(inode)->rsrc_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 if (!main_inode->i_nlink)
578 return 0;
579
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200580 if (hfs_find_init(HFSPLUS_SB(main_inode->i_sb)->cat_tree, &fd))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* panic? */
582 return -EIO;
583
584 if (hfsplus_find_cat(main_inode->i_sb, main_inode->i_ino, &fd))
585 /* panic? */
586 goto out;
587
588 if (S_ISDIR(main_inode->i_mode)) {
589 struct hfsplus_cat_folder *folder = &entry.folder;
590
591 if (fd.entrylength < sizeof(struct hfsplus_cat_folder))
592 /* panic? */;
593 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
594 sizeof(struct hfsplus_cat_folder));
595 /* simple node checks? */
Christoph Hellwig90e61692010-10-14 09:54:39 -0400596 hfsplus_cat_set_perms(inode, &folder->permissions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 folder->access_date = hfsp_ut2mt(inode->i_atime);
598 folder->content_mod_date = hfsp_ut2mt(inode->i_mtime);
599 folder->attribute_mod_date = hfsp_ut2mt(inode->i_ctime);
600 folder->valence = cpu_to_be32(inode->i_size - 2);
Sergei Antonovd7d673a2014-03-10 15:49:51 -0700601 if (folder->flags & cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT)) {
602 folder->subfolders =
603 cpu_to_be32(HFSPLUS_I(inode)->subfolders);
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
606 sizeof(struct hfsplus_cat_folder));
607 } else if (HFSPLUS_IS_RSRC(inode)) {
608 struct hfsplus_cat_file *file = &entry.file;
609 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
610 sizeof(struct hfsplus_cat_file));
611 hfsplus_inode_write_fork(inode, &file->rsrc_fork);
612 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
613 sizeof(struct hfsplus_cat_file));
614 } else {
615 struct hfsplus_cat_file *file = &entry.file;
616
617 if (fd.entrylength < sizeof(struct hfsplus_cat_file))
618 /* panic? */;
619 hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
620 sizeof(struct hfsplus_cat_file));
621 hfsplus_inode_write_fork(inode, &file->data_fork);
Christoph Hellwig90e61692010-10-14 09:54:39 -0400622 hfsplus_cat_set_perms(inode, &file->permissions);
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200623 if (HFSPLUS_FLG_IMMUTABLE &
624 (file->permissions.rootflags |
625 file->permissions.userflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
627 else
628 file->flags &= cpu_to_be16(~HFSPLUS_FILE_LOCKED);
629 file->access_date = hfsp_ut2mt(inode->i_atime);
630 file->content_mod_date = hfsp_ut2mt(inode->i_mtime);
631 file->attribute_mod_date = hfsp_ut2mt(inode->i_ctime);
632 hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
633 sizeof(struct hfsplus_cat_file));
634 }
Christoph Hellwige3494702010-11-23 14:38:15 +0100635
636 set_bit(HFSPLUS_I_CAT_DIRTY, &HFSPLUS_I(inode)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637out:
638 hfs_find_exit(&fd);
639 return 0;
640}
Miklos Szeredi9cbae742021-04-07 14:36:44 +0200641
642int hfsplus_fileattr_get(struct dentry *dentry, struct fileattr *fa)
643{
644 struct inode *inode = d_inode(dentry);
645 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
646 unsigned int flags = 0;
647
648 if (inode->i_flags & S_IMMUTABLE)
649 flags |= FS_IMMUTABLE_FL;
650 if (inode->i_flags & S_APPEND)
651 flags |= FS_APPEND_FL;
652 if (hip->userflags & HFSPLUS_FLG_NODUMP)
653 flags |= FS_NODUMP_FL;
654
655 fileattr_fill_flags(fa, flags);
656
657 return 0;
658}
659
660int hfsplus_fileattr_set(struct user_namespace *mnt_userns,
661 struct dentry *dentry, struct fileattr *fa)
662{
663 struct inode *inode = d_inode(dentry);
664 struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
665 unsigned int new_fl = 0;
666
667 if (fileattr_has_fsx(fa))
668 return -EOPNOTSUPP;
669
670 /* don't silently ignore unsupported ext2 flags */
671 if (fa->flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL))
672 return -EOPNOTSUPP;
673
674 if (fa->flags & FS_IMMUTABLE_FL)
675 new_fl |= S_IMMUTABLE;
676
677 if (fa->flags & FS_APPEND_FL)
678 new_fl |= S_APPEND;
679
680 inode_set_flags(inode, new_fl, S_IMMUTABLE | S_APPEND);
681
682 if (fa->flags & FS_NODUMP_FL)
683 hip->userflags |= HFSPLUS_FLG_NODUMP;
684 else
685 hip->userflags &= ~HFSPLUS_FLG_NODUMP;
686
687 inode->i_ctime = current_time(inode);
688 mark_inode_dirty(inode);
689
690 return 0;
691}