1 /* 2 * linux/fs/hfs/inode.c 3 * 4 * Copyright (C) 1995-1997 Paul H. Hargrove 5 * (C) 2003 Ardis Technologies <roman@ardistech.com> 6 * This file may be distributed under the terms of the GNU General Public License. 7 * 8 * This file contains inode-related functions which do not depend on 9 * which scheme is being used to represent forks. 10 * 11 * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds 12 */ 13 14 #include <linux/pagemap.h> 15 #include <linux/mpage.h> 16 #include <linux/sched.h> 17 #include <linux/cred.h> 18 #include <linux/uio.h> 19 #include <linux/xattr.h> 20 #include <linux/blkdev.h> 21 #include <linux/fileattr.h> 22 23 #include "hfs_fs.h" 24 #include "btree.h" 25 26 static const struct file_operations hfs_file_operations; 27 static const struct inode_operations hfs_file_inode_operations; 28 29 /*================ Variable-like macros ================*/ 30 31 #define HFS_VALID_MODE_BITS (S_IFREG | S_IFDIR | S_IRWXUGO) 32 33 static int hfs_read_folio(struct file *file, struct folio *folio) 34 { 35 return block_read_full_folio(folio, hfs_get_block); 36 } 37 38 static void hfs_write_failed(struct address_space *mapping, loff_t to) 39 { 40 struct inode *inode = mapping->host; 41 42 if (to > inode->i_size) { 43 truncate_pagecache(inode, inode->i_size); 44 hfs_file_truncate(inode); 45 } 46 } 47 48 int hfs_write_begin(const struct kiocb *iocb, struct address_space *mapping, 49 loff_t pos, unsigned int len, struct folio **foliop, 50 void **fsdata) 51 { 52 int ret; 53 54 ret = cont_write_begin(iocb, mapping, pos, len, foliop, fsdata, 55 hfs_get_block, 56 &HFS_I(mapping->host)->phys_size); 57 if (unlikely(ret)) 58 hfs_write_failed(mapping, pos + len); 59 60 return ret; 61 } 62 63 static sector_t hfs_bmap(struct address_space *mapping, sector_t block) 64 { 65 return generic_block_bmap(mapping, block, hfs_get_block); 66 } 67 68 static bool hfs_release_folio(struct folio *folio, gfp_t mask) 69 { 70 struct inode *inode = folio->mapping->host; 71 struct super_block *sb = inode->i_sb; 72 struct hfs_btree *tree; 73 struct hfs_bnode *node; 74 u32 nidx; 75 int i; 76 bool res = true; 77 78 switch (inode->i_ino) { 79 case HFS_EXT_CNID: 80 tree = HFS_SB(sb)->ext_tree; 81 break; 82 case HFS_CAT_CNID: 83 tree = HFS_SB(sb)->cat_tree; 84 break; 85 default: 86 BUG(); 87 return false; 88 } 89 90 if (!tree) 91 return false; 92 93 if (tree->node_size >= PAGE_SIZE) { 94 nidx = folio->index >> (tree->node_size_shift - PAGE_SHIFT); 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 = false; 101 if (res && node) { 102 hfs_bnode_unhash(node); 103 hfs_bnode_free(node); 104 } 105 spin_unlock(&tree->hash_lock); 106 } else { 107 nidx = folio->index << (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 = false; 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(folio) : false; 124 } 125 126 static ssize_t hfs_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, hfs_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 hfs_write_failed(mapping, end); 146 } 147 148 return ret; 149 } 150 151 static int hfs_writepages(struct address_space *mapping, 152 struct writeback_control *wbc) 153 { 154 return mpage_writepages(mapping, wbc, hfs_get_block); 155 } 156 157 const struct address_space_operations hfs_btree_aops = { 158 .dirty_folio = block_dirty_folio, 159 .invalidate_folio = block_invalidate_folio, 160 .read_folio = hfs_read_folio, 161 .writepages = hfs_writepages, 162 .write_begin = hfs_write_begin, 163 .write_end = generic_write_end, 164 .migrate_folio = buffer_migrate_folio, 165 .bmap = hfs_bmap, 166 .release_folio = hfs_release_folio, 167 }; 168 169 const struct address_space_operations hfs_aops = { 170 .dirty_folio = block_dirty_folio, 171 .invalidate_folio = block_invalidate_folio, 172 .read_folio = hfs_read_folio, 173 .write_begin = hfs_write_begin, 174 .write_end = generic_write_end, 175 .bmap = hfs_bmap, 176 .direct_IO = hfs_direct_IO, 177 .writepages = hfs_writepages, 178 .migrate_folio = buffer_migrate_folio, 179 }; 180 181 /* 182 * hfs_new_inode 183 */ 184 struct inode *hfs_new_inode(struct inode *dir, const struct qstr *name, umode_t mode) 185 { 186 struct super_block *sb = dir->i_sb; 187 struct inode *inode = new_inode(sb); 188 s64 next_id; 189 s64 file_count; 190 s64 folder_count; 191 int err = -ENOMEM; 192 193 if (!inode) 194 goto out_err; 195 196 err = -ERANGE; 197 198 mutex_init(&HFS_I(inode)->extents_lock); 199 hfs_cat_build_key(sb, (btree_key *)&HFS_I(inode)->cat_key, dir->i_ino, name); 200 next_id = atomic64_inc_return(&HFS_SB(sb)->next_id); 201 if (next_id > U32_MAX) { 202 atomic64_dec(&HFS_SB(sb)->next_id); 203 pr_err("cannot create new inode: next CNID exceeds limit\n"); 204 goto out_discard; 205 } 206 inode->i_ino = (u32)next_id - 1; 207 inode->i_mode = mode; 208 inode->i_uid = current_fsuid(); 209 inode->i_gid = current_fsgid(); 210 set_nlink(inode, 1); 211 simple_inode_init_ts(inode); 212 HFS_I(inode)->flags = 0; 213 HFS_I(inode)->rsrc_inode = NULL; 214 HFS_I(inode)->fs_blocks = 0; 215 HFS_I(inode)->tz_secondswest = sys_tz.tz_minuteswest * 60; 216 if (S_ISDIR(mode)) { 217 inode->i_size = 2; 218 folder_count = atomic64_inc_return(&HFS_SB(sb)->folder_count); 219 if (folder_count> U32_MAX) { 220 atomic64_dec(&HFS_SB(sb)->folder_count); 221 pr_err("cannot create new inode: folder count exceeds limit\n"); 222 goto out_discard; 223 } 224 if (dir->i_ino == HFS_ROOT_CNID) 225 HFS_SB(sb)->root_dirs++; 226 inode->i_op = &hfs_dir_inode_operations; 227 inode->i_fop = &hfs_dir_operations; 228 inode->i_mode |= S_IRWXUGO; 229 inode->i_mode &= ~HFS_SB(inode->i_sb)->s_dir_umask; 230 } else if (S_ISREG(mode)) { 231 HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks; 232 file_count = atomic64_inc_return(&HFS_SB(sb)->file_count); 233 if (file_count > U32_MAX) { 234 atomic64_dec(&HFS_SB(sb)->file_count); 235 pr_err("cannot create new inode: file count exceeds limit\n"); 236 goto out_discard; 237 } 238 if (dir->i_ino == HFS_ROOT_CNID) 239 HFS_SB(sb)->root_files++; 240 inode->i_op = &hfs_file_inode_operations; 241 inode->i_fop = &hfs_file_operations; 242 inode->i_mapping->a_ops = &hfs_aops; 243 inode->i_mode |= S_IRUGO|S_IXUGO; 244 if (mode & S_IWUSR) 245 inode->i_mode |= S_IWUGO; 246 inode->i_mode &= ~HFS_SB(inode->i_sb)->s_file_umask; 247 HFS_I(inode)->phys_size = 0; 248 HFS_I(inode)->alloc_blocks = 0; 249 HFS_I(inode)->first_blocks = 0; 250 HFS_I(inode)->cached_start = 0; 251 HFS_I(inode)->cached_blocks = 0; 252 memset(HFS_I(inode)->first_extents, 0, sizeof(hfs_extent_rec)); 253 memset(HFS_I(inode)->cached_extents, 0, sizeof(hfs_extent_rec)); 254 } 255 insert_inode_hash(inode); 256 mark_inode_dirty(inode); 257 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags); 258 hfs_mark_mdb_dirty(sb); 259 260 return inode; 261 262 out_discard: 263 iput(inode); 264 out_err: 265 return ERR_PTR(err); 266 } 267 268 void hfs_delete_inode(struct inode *inode) 269 { 270 struct super_block *sb = inode->i_sb; 271 272 hfs_dbg("ino %llu\n", inode->i_ino); 273 if (S_ISDIR(inode->i_mode)) { 274 atomic64_dec(&HFS_SB(sb)->folder_count); 275 if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID)) 276 HFS_SB(sb)->root_dirs--; 277 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags); 278 hfs_mark_mdb_dirty(sb); 279 return; 280 } 281 282 atomic64_dec(&HFS_SB(sb)->file_count); 283 if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID)) 284 HFS_SB(sb)->root_files--; 285 if (S_ISREG(inode->i_mode)) { 286 if (!inode->i_nlink) { 287 inode->i_size = 0; 288 hfs_file_truncate(inode); 289 } 290 } 291 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags); 292 hfs_mark_mdb_dirty(sb); 293 } 294 295 void hfs_inode_read_fork(struct inode *inode, struct hfs_extent *ext, 296 __be32 __log_size, __be32 phys_size, u32 clump_size) 297 { 298 struct super_block *sb = inode->i_sb; 299 u32 log_size = be32_to_cpu(__log_size); 300 u16 count; 301 int i; 302 303 memcpy(HFS_I(inode)->first_extents, ext, sizeof(hfs_extent_rec)); 304 for (count = 0, i = 0; i < 3; i++) 305 count += be16_to_cpu(ext[i].count); 306 HFS_I(inode)->first_blocks = count; 307 HFS_I(inode)->cached_start = 0; 308 HFS_I(inode)->cached_blocks = 0; 309 310 inode->i_size = HFS_I(inode)->phys_size = log_size; 311 HFS_I(inode)->fs_blocks = (log_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits; 312 inode_set_bytes(inode, HFS_I(inode)->fs_blocks << sb->s_blocksize_bits); 313 HFS_I(inode)->alloc_blocks = be32_to_cpu(phys_size) / 314 HFS_SB(sb)->alloc_blksz; 315 HFS_I(inode)->clump_blocks = clump_size / HFS_SB(sb)->alloc_blksz; 316 if (!HFS_I(inode)->clump_blocks) 317 HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks; 318 } 319 320 struct hfs_iget_data { 321 struct hfs_cat_key *key; 322 hfs_cat_rec *rec; 323 }; 324 325 static int hfs_test_inode(struct inode *inode, void *data) 326 { 327 struct hfs_iget_data *idata = data; 328 hfs_cat_rec *rec; 329 330 rec = idata->rec; 331 switch (rec->type) { 332 case HFS_CDR_DIR: 333 return inode->i_ino == be32_to_cpu(rec->dir.DirID); 334 case HFS_CDR_FIL: 335 return inode->i_ino == be32_to_cpu(rec->file.FlNum); 336 default: 337 BUG(); 338 return 1; 339 } 340 } 341 342 /* 343 * hfs_read_inode 344 */ 345 static int hfs_read_inode(struct inode *inode, void *data) 346 { 347 struct hfs_iget_data *idata = data; 348 struct hfs_sb_info *hsb = HFS_SB(inode->i_sb); 349 hfs_cat_rec *rec; 350 struct timespec64 mtime; 351 352 HFS_I(inode)->flags = 0; 353 HFS_I(inode)->rsrc_inode = NULL; 354 mutex_init(&HFS_I(inode)->extents_lock); 355 356 /* Initialize the inode */ 357 inode->i_uid = hsb->s_uid; 358 inode->i_gid = hsb->s_gid; 359 set_nlink(inode, 1); 360 361 if (idata->key) 362 HFS_I(inode)->cat_key = *idata->key; 363 else 364 HFS_I(inode)->flags |= HFS_FLG_RSRC; 365 HFS_I(inode)->tz_secondswest = sys_tz.tz_minuteswest * 60; 366 367 rec = idata->rec; 368 switch (rec->type) { 369 case HFS_CDR_FIL: 370 if (!HFS_IS_RSRC(inode)) { 371 hfs_inode_read_fork(inode, rec->file.ExtRec, rec->file.LgLen, 372 rec->file.PyLen, be16_to_cpu(rec->file.ClpSize)); 373 } else { 374 hfs_inode_read_fork(inode, rec->file.RExtRec, rec->file.RLgLen, 375 rec->file.RPyLen, be16_to_cpu(rec->file.ClpSize)); 376 } 377 378 inode->i_ino = be32_to_cpu(rec->file.FlNum); 379 inode->i_mode = S_IRUGO | S_IXUGO; 380 if (!(rec->file.Flags & HFS_FIL_LOCK)) 381 inode->i_mode |= S_IWUGO; 382 inode->i_mode &= ~hsb->s_file_umask; 383 inode->i_mode |= S_IFREG; 384 mtime = hfs_m_to_utime(rec->file.MdDat); 385 inode_set_ctime_to_ts(inode, mtime); 386 inode_set_atime_to_ts(inode, mtime); 387 inode_set_mtime_to_ts(inode, mtime); 388 inode->i_op = &hfs_file_inode_operations; 389 inode->i_fop = &hfs_file_operations; 390 inode->i_mapping->a_ops = &hfs_aops; 391 break; 392 case HFS_CDR_DIR: 393 inode->i_ino = be32_to_cpu(rec->dir.DirID); 394 inode->i_size = be16_to_cpu(rec->dir.Val) + 2; 395 HFS_I(inode)->fs_blocks = 0; 396 inode->i_mode = S_IFDIR | (S_IRWXUGO & ~hsb->s_dir_umask); 397 mtime = hfs_m_to_utime(rec->dir.MdDat); 398 inode_set_ctime_to_ts(inode, mtime); 399 inode_set_atime_to_ts(inode, mtime); 400 inode_set_mtime_to_ts(inode, mtime); 401 inode->i_op = &hfs_dir_inode_operations; 402 inode->i_fop = &hfs_dir_operations; 403 break; 404 default: 405 make_bad_inode(inode); 406 } 407 return 0; 408 } 409 410 /* 411 * __hfs_iget() 412 * 413 * Given the MDB for a HFS filesystem, a 'key' and an 'entry' in 414 * the catalog B-tree and the 'type' of the desired file return the 415 * inode for that file/directory or NULL. Note that 'type' indicates 416 * whether we want the actual file or directory, or the corresponding 417 * metadata (AppleDouble header file or CAP metadata file). 418 */ 419 struct inode *hfs_iget(struct super_block *sb, struct hfs_cat_key *key, hfs_cat_rec *rec) 420 { 421 struct hfs_iget_data data = { key, rec }; 422 struct inode *inode; 423 u32 cnid; 424 425 switch (rec->type) { 426 case HFS_CDR_DIR: 427 cnid = be32_to_cpu(rec->dir.DirID); 428 break; 429 case HFS_CDR_FIL: 430 cnid = be32_to_cpu(rec->file.FlNum); 431 break; 432 default: 433 return NULL; 434 } 435 inode = iget5_locked(sb, cnid, hfs_test_inode, hfs_read_inode, &data); 436 if (inode && (inode_state_read_once(inode) & I_NEW)) 437 unlock_new_inode(inode); 438 return inode; 439 } 440 441 void hfs_inode_write_fork(struct inode *inode, struct hfs_extent *ext, 442 __be32 *log_size, __be32 *phys_size) 443 { 444 memcpy(ext, HFS_I(inode)->first_extents, sizeof(hfs_extent_rec)); 445 446 if (log_size) 447 *log_size = cpu_to_be32(inode->i_size); 448 if (phys_size) 449 *phys_size = cpu_to_be32(HFS_I(inode)->alloc_blocks * 450 HFS_SB(inode->i_sb)->alloc_blksz); 451 } 452 453 int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) 454 { 455 struct inode *main_inode = inode; 456 struct hfs_find_data fd; 457 hfs_cat_rec rec; 458 int res; 459 460 hfs_dbg("ino %llu\n", inode->i_ino); 461 res = hfs_ext_write_extent(inode); 462 if (res) 463 return res; 464 465 if (inode->i_ino < HFS_FIRSTUSER_CNID) { 466 switch (inode->i_ino) { 467 case HFS_ROOT_CNID: 468 break; 469 case HFS_EXT_CNID: 470 hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree); 471 return 0; 472 case HFS_CAT_CNID: 473 hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree); 474 return 0; 475 default: 476 BUG(); 477 return -EIO; 478 } 479 } 480 481 if (HFS_IS_RSRC(inode)) 482 main_inode = HFS_I(inode)->rsrc_inode; 483 484 if (!main_inode->i_nlink) 485 return 0; 486 487 if (hfs_find_init(HFS_SB(main_inode->i_sb)->cat_tree, &fd)) 488 /* panic? */ 489 return -EIO; 490 491 res = -EIO; 492 if (HFS_I(main_inode)->cat_key.CName.len > HFS_NAMELEN) 493 goto out; 494 fd.search_key->cat = HFS_I(main_inode)->cat_key; 495 if (hfs_brec_find(&fd)) 496 goto out; 497 498 if (S_ISDIR(main_inode->i_mode)) { 499 if (fd.entrylength < sizeof(struct hfs_cat_dir)) 500 goto out; 501 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, 502 sizeof(struct hfs_cat_dir)); 503 if (rec.type != HFS_CDR_DIR || 504 be32_to_cpu(rec.dir.DirID) != inode->i_ino) { 505 } 506 507 rec.dir.MdDat = hfs_u_to_mtime(inode_get_mtime(inode)); 508 rec.dir.Val = cpu_to_be16(inode->i_size - 2); 509 510 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset, 511 sizeof(struct hfs_cat_dir)); 512 } else if (HFS_IS_RSRC(inode)) { 513 if (fd.entrylength < sizeof(struct hfs_cat_file)) 514 goto out; 515 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, 516 sizeof(struct hfs_cat_file)); 517 hfs_inode_write_fork(inode, rec.file.RExtRec, 518 &rec.file.RLgLen, &rec.file.RPyLen); 519 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset, 520 sizeof(struct hfs_cat_file)); 521 } else { 522 if (fd.entrylength < sizeof(struct hfs_cat_file)) 523 goto out; 524 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, 525 sizeof(struct hfs_cat_file)); 526 if (rec.type != HFS_CDR_FIL || 527 be32_to_cpu(rec.file.FlNum) != inode->i_ino) { 528 } 529 530 if (inode->i_mode & S_IWUSR) 531 rec.file.Flags &= ~HFS_FIL_LOCK; 532 else 533 rec.file.Flags |= HFS_FIL_LOCK; 534 hfs_inode_write_fork(inode, rec.file.ExtRec, &rec.file.LgLen, &rec.file.PyLen); 535 rec.file.MdDat = hfs_u_to_mtime(inode_get_mtime(inode)); 536 537 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset, 538 sizeof(struct hfs_cat_file)); 539 } 540 res = 0; 541 out: 542 hfs_find_exit(&fd); 543 return res; 544 } 545 546 static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry, 547 unsigned int flags) 548 { 549 struct inode *inode = NULL; 550 hfs_cat_rec rec; 551 struct hfs_find_data fd; 552 int res; 553 554 if (HFS_IS_RSRC(dir) || strcmp(dentry->d_name.name, "rsrc")) 555 goto out; 556 557 inode = HFS_I(dir)->rsrc_inode; 558 if (inode) 559 goto out; 560 561 inode = new_inode(dir->i_sb); 562 if (!inode) 563 return ERR_PTR(-ENOMEM); 564 565 res = hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd); 566 if (res) { 567 iput(inode); 568 return ERR_PTR(res); 569 } 570 fd.search_key->cat = HFS_I(dir)->cat_key; 571 res = hfs_brec_read(&fd, &rec, sizeof(rec)); 572 if (!res) { 573 struct hfs_iget_data idata = { NULL, &rec }; 574 hfs_read_inode(inode, &idata); 575 } 576 hfs_find_exit(&fd); 577 if (res) { 578 iput(inode); 579 return ERR_PTR(res); 580 } 581 HFS_I(inode)->rsrc_inode = dir; 582 HFS_I(dir)->rsrc_inode = inode; 583 igrab(dir); 584 inode_fake_hash(inode); 585 mark_inode_dirty(inode); 586 dont_mount(dentry); 587 out: 588 return d_splice_alias(inode, dentry); 589 } 590 591 void hfs_evict_inode(struct inode *inode) 592 { 593 truncate_inode_pages_final(&inode->i_data); 594 clear_inode(inode); 595 if (HFS_IS_RSRC(inode) && HFS_I(inode)->rsrc_inode) { 596 HFS_I(HFS_I(inode)->rsrc_inode)->rsrc_inode = NULL; 597 iput(HFS_I(inode)->rsrc_inode); 598 } 599 } 600 601 static int hfs_file_open(struct inode *inode, struct file *file) 602 { 603 if (HFS_IS_RSRC(inode)) 604 inode = HFS_I(inode)->rsrc_inode; 605 atomic_inc(&HFS_I(inode)->opencnt); 606 return 0; 607 } 608 609 static int hfs_file_release(struct inode *inode, struct file *file) 610 { 611 //struct super_block *sb = inode->i_sb; 612 613 if (HFS_IS_RSRC(inode)) 614 inode = HFS_I(inode)->rsrc_inode; 615 if (atomic_dec_and_test(&HFS_I(inode)->opencnt)) { 616 inode_lock(inode); 617 hfs_file_truncate(inode); 618 //if (inode->i_flags & S_DEAD) { 619 // hfs_delete_cat(inode->i_ino, HFSPLUS_SB(sb).hidden_dir, NULL); 620 // hfs_delete_inode(inode); 621 //} 622 inode_unlock(inode); 623 } 624 return 0; 625 } 626 627 int hfs_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 628 struct iattr *attr) 629 { 630 struct inode *inode = d_inode(dentry); 631 struct hfs_sb_info *hsb = HFS_SB(inode->i_sb); 632 int error; 633 634 error = setattr_prepare(&nop_mnt_idmap, dentry, attr); 635 if (error) 636 return error; 637 638 /* no uig/gid changes and limit which mode bits can be set */ 639 if (((attr->ia_valid & ATTR_UID) && 640 (!uid_eq(attr->ia_uid, hsb->s_uid))) || 641 ((attr->ia_valid & ATTR_GID) && 642 (!gid_eq(attr->ia_gid, hsb->s_gid))) || 643 ((attr->ia_valid & ATTR_MODE) && 644 ((S_ISDIR(inode->i_mode) && 645 (attr->ia_mode != inode->i_mode)) || 646 (attr->ia_mode & ~HFS_VALID_MODE_BITS)))) { 647 return hsb->s_quiet ? 0 : error; 648 } 649 650 /* map file permissions to the closest allowable permissions in HFS */ 651 if (attr->ia_valid & ATTR_MODE) { 652 /* Only the 'w' bits can ever change and only all together. */ 653 if (attr->ia_mode & S_IWUSR) 654 attr->ia_mode = inode->i_mode | S_IWUGO; 655 else 656 attr->ia_mode = inode->i_mode & ~S_IWUGO; 657 attr->ia_mode &= S_ISDIR(inode->i_mode) ? ~hsb->s_dir_umask: ~hsb->s_file_umask; 658 } 659 660 if ((attr->ia_valid & ATTR_SIZE) && 661 attr->ia_size != i_size_read(inode)) { 662 inode_dio_wait(inode); 663 664 error = inode_newsize_ok(inode, attr->ia_size); 665 if (error) 666 return error; 667 668 truncate_setsize(inode, attr->ia_size); 669 hfs_file_truncate(inode); 670 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 671 } 672 673 setattr_copy(&nop_mnt_idmap, inode, attr); 674 mark_inode_dirty(inode); 675 return 0; 676 } 677 678 static int hfs_file_fsync(struct file *filp, loff_t start, loff_t end, 679 int datasync) 680 { 681 struct inode *inode = filp->f_mapping->host; 682 struct super_block * sb; 683 int ret, err; 684 685 ret = file_write_and_wait_range(filp, start, end); 686 if (ret) 687 return ret; 688 inode_lock(inode); 689 690 /* sync the inode to buffers */ 691 ret = write_inode_now(inode, 0); 692 693 /* sync the superblock to buffers */ 694 sb = inode->i_sb; 695 flush_delayed_work(&HFS_SB(sb)->mdb_work); 696 /* .. finally sync the buffers to disk */ 697 err = sync_blockdev(sb->s_bdev); 698 if (!ret) 699 ret = err; 700 inode_unlock(inode); 701 return ret; 702 } 703 704 int hfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa) 705 { 706 /* 707 * HFS compares filenames using Mac OS Roman case folding, so 708 * lookup is always case-insensitive. Names are stored on disk 709 * with case intact; CASENONPRESERVING stays clear. 710 */ 711 fa->fsx_xflags |= FS_XFLAG_CASEFOLD; 712 fa->flags |= FS_CASEFOLD_FL; 713 return 0; 714 } 715 716 static const struct file_operations hfs_file_operations = { 717 .llseek = generic_file_llseek, 718 .read_iter = generic_file_read_iter, 719 .write_iter = generic_file_write_iter, 720 .mmap_prepare = generic_file_mmap_prepare, 721 .splice_read = filemap_splice_read, 722 .splice_write = iter_file_splice_write, 723 .fsync = hfs_file_fsync, 724 .open = hfs_file_open, 725 .release = hfs_file_release, 726 }; 727 728 static const struct inode_operations hfs_file_inode_operations = { 729 .lookup = hfs_file_lookup, 730 .setattr = hfs_inode_setattr, 731 .listxattr = generic_listxattr, 732 .fileattr_get = hfs_fileattr_get, 733 }; 734