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