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 INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list); 200 spin_lock_init(&HFS_I(inode)->open_dir_lock); 201 hfs_cat_build_key(sb, (btree_key *)&HFS_I(inode)->cat_key, dir->i_ino, name); 202 next_id = atomic64_inc_return(&HFS_SB(sb)->next_id); 203 if (next_id > U32_MAX) { 204 atomic64_dec(&HFS_SB(sb)->next_id); 205 pr_err("cannot create new inode: next CNID exceeds limit\n"); 206 goto out_discard; 207 } 208 inode->i_ino = (u32)next_id; 209 inode->i_mode = mode; 210 inode->i_uid = current_fsuid(); 211 inode->i_gid = current_fsgid(); 212 set_nlink(inode, 1); 213 simple_inode_init_ts(inode); 214 HFS_I(inode)->flags = 0; 215 HFS_I(inode)->rsrc_inode = NULL; 216 HFS_I(inode)->fs_blocks = 0; 217 HFS_I(inode)->tz_secondswest = sys_tz.tz_minuteswest * 60; 218 if (S_ISDIR(mode)) { 219 inode->i_size = 2; 220 folder_count = atomic64_inc_return(&HFS_SB(sb)->folder_count); 221 if (folder_count> U32_MAX) { 222 atomic64_dec(&HFS_SB(sb)->folder_count); 223 pr_err("cannot create new inode: folder count exceeds limit\n"); 224 goto out_discard; 225 } 226 if (dir->i_ino == HFS_ROOT_CNID) 227 HFS_SB(sb)->root_dirs++; 228 inode->i_op = &hfs_dir_inode_operations; 229 inode->i_fop = &hfs_dir_operations; 230 inode->i_mode |= S_IRWXUGO; 231 inode->i_mode &= ~HFS_SB(inode->i_sb)->s_dir_umask; 232 } else if (S_ISREG(mode)) { 233 HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks; 234 file_count = atomic64_inc_return(&HFS_SB(sb)->file_count); 235 if (file_count > U32_MAX) { 236 atomic64_dec(&HFS_SB(sb)->file_count); 237 pr_err("cannot create new inode: file count exceeds limit\n"); 238 goto out_discard; 239 } 240 if (dir->i_ino == HFS_ROOT_CNID) 241 HFS_SB(sb)->root_files++; 242 inode->i_op = &hfs_file_inode_operations; 243 inode->i_fop = &hfs_file_operations; 244 inode->i_mapping->a_ops = &hfs_aops; 245 inode->i_mode |= S_IRUGO|S_IXUGO; 246 if (mode & S_IWUSR) 247 inode->i_mode |= S_IWUGO; 248 inode->i_mode &= ~HFS_SB(inode->i_sb)->s_file_umask; 249 HFS_I(inode)->phys_size = 0; 250 HFS_I(inode)->alloc_blocks = 0; 251 HFS_I(inode)->first_blocks = 0; 252 HFS_I(inode)->cached_start = 0; 253 HFS_I(inode)->cached_blocks = 0; 254 memset(HFS_I(inode)->first_extents, 0, sizeof(hfs_extent_rec)); 255 memset(HFS_I(inode)->cached_extents, 0, sizeof(hfs_extent_rec)); 256 } 257 insert_inode_hash(inode); 258 mark_inode_dirty(inode); 259 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags); 260 hfs_mark_mdb_dirty(sb); 261 262 return inode; 263 264 out_discard: 265 iput(inode); 266 out_err: 267 return ERR_PTR(err); 268 } 269 270 void hfs_delete_inode(struct inode *inode) 271 { 272 struct super_block *sb = inode->i_sb; 273 274 hfs_dbg("ino %llu\n", inode->i_ino); 275 if (S_ISDIR(inode->i_mode)) { 276 atomic64_dec(&HFS_SB(sb)->folder_count); 277 if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID)) 278 HFS_SB(sb)->root_dirs--; 279 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags); 280 hfs_mark_mdb_dirty(sb); 281 return; 282 } 283 284 atomic64_dec(&HFS_SB(sb)->file_count); 285 if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID)) 286 HFS_SB(sb)->root_files--; 287 if (S_ISREG(inode->i_mode)) { 288 if (!inode->i_nlink) { 289 inode->i_size = 0; 290 hfs_file_truncate(inode); 291 } 292 } 293 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags); 294 hfs_mark_mdb_dirty(sb); 295 } 296 297 void hfs_inode_read_fork(struct inode *inode, struct hfs_extent *ext, 298 __be32 __log_size, __be32 phys_size, u32 clump_size) 299 { 300 struct super_block *sb = inode->i_sb; 301 u32 log_size = be32_to_cpu(__log_size); 302 u16 count; 303 int i; 304 305 memcpy(HFS_I(inode)->first_extents, ext, sizeof(hfs_extent_rec)); 306 for (count = 0, i = 0; i < 3; i++) 307 count += be16_to_cpu(ext[i].count); 308 HFS_I(inode)->first_blocks = count; 309 HFS_I(inode)->cached_start = 0; 310 HFS_I(inode)->cached_blocks = 0; 311 312 inode->i_size = HFS_I(inode)->phys_size = log_size; 313 HFS_I(inode)->fs_blocks = (log_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits; 314 inode_set_bytes(inode, HFS_I(inode)->fs_blocks << sb->s_blocksize_bits); 315 HFS_I(inode)->alloc_blocks = be32_to_cpu(phys_size) / 316 HFS_SB(sb)->alloc_blksz; 317 HFS_I(inode)->clump_blocks = clump_size / HFS_SB(sb)->alloc_blksz; 318 if (!HFS_I(inode)->clump_blocks) 319 HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks; 320 } 321 322 struct hfs_iget_data { 323 struct hfs_cat_key *key; 324 hfs_cat_rec *rec; 325 }; 326 327 static int hfs_test_inode(struct inode *inode, void *data) 328 { 329 struct hfs_iget_data *idata = data; 330 hfs_cat_rec *rec; 331 332 rec = idata->rec; 333 switch (rec->type) { 334 case HFS_CDR_DIR: 335 return inode->i_ino == be32_to_cpu(rec->dir.DirID); 336 case HFS_CDR_FIL: 337 return inode->i_ino == be32_to_cpu(rec->file.FlNum); 338 default: 339 BUG(); 340 return 1; 341 } 342 } 343 344 /* 345 * hfs_read_inode 346 */ 347 static int hfs_read_inode(struct inode *inode, void *data) 348 { 349 struct hfs_iget_data *idata = data; 350 struct hfs_sb_info *hsb = HFS_SB(inode->i_sb); 351 hfs_cat_rec *rec; 352 353 HFS_I(inode)->flags = 0; 354 HFS_I(inode)->rsrc_inode = NULL; 355 mutex_init(&HFS_I(inode)->extents_lock); 356 INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list); 357 spin_lock_init(&HFS_I(inode)->open_dir_lock); 358 359 /* Initialize the inode */ 360 inode->i_uid = hsb->s_uid; 361 inode->i_gid = hsb->s_gid; 362 set_nlink(inode, 1); 363 364 if (idata->key) 365 HFS_I(inode)->cat_key = *idata->key; 366 else 367 HFS_I(inode)->flags |= HFS_FLG_RSRC; 368 HFS_I(inode)->tz_secondswest = sys_tz.tz_minuteswest * 60; 369 370 rec = idata->rec; 371 switch (rec->type) { 372 case HFS_CDR_FIL: 373 if (!HFS_IS_RSRC(inode)) { 374 hfs_inode_read_fork(inode, rec->file.ExtRec, rec->file.LgLen, 375 rec->file.PyLen, be16_to_cpu(rec->file.ClpSize)); 376 } else { 377 hfs_inode_read_fork(inode, rec->file.RExtRec, rec->file.RLgLen, 378 rec->file.RPyLen, be16_to_cpu(rec->file.ClpSize)); 379 } 380 381 inode->i_ino = be32_to_cpu(rec->file.FlNum); 382 inode->i_mode = S_IRUGO | S_IXUGO; 383 if (!(rec->file.Flags & HFS_FIL_LOCK)) 384 inode->i_mode |= S_IWUGO; 385 inode->i_mode &= ~hsb->s_file_umask; 386 inode->i_mode |= S_IFREG; 387 inode_set_mtime_to_ts(inode, 388 inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, hfs_m_to_utime(rec->file.MdDat)))); 389 inode->i_op = &hfs_file_inode_operations; 390 inode->i_fop = &hfs_file_operations; 391 inode->i_mapping->a_ops = &hfs_aops; 392 break; 393 case HFS_CDR_DIR: 394 inode->i_ino = be32_to_cpu(rec->dir.DirID); 395 inode->i_size = be16_to_cpu(rec->dir.Val) + 2; 396 HFS_I(inode)->fs_blocks = 0; 397 inode->i_mode = S_IFDIR | (S_IRWXUGO & ~hsb->s_dir_umask); 398 inode_set_mtime_to_ts(inode, 399 inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, hfs_m_to_utime(rec->dir.MdDat)))); 400 inode->i_op = &hfs_dir_inode_operations; 401 inode->i_fop = &hfs_dir_operations; 402 break; 403 default: 404 make_bad_inode(inode); 405 } 406 return 0; 407 } 408 409 /* 410 * __hfs_iget() 411 * 412 * Given the MDB for a HFS filesystem, a 'key' and an 'entry' in 413 * the catalog B-tree and the 'type' of the desired file return the 414 * inode for that file/directory or NULL. Note that 'type' indicates 415 * whether we want the actual file or directory, or the corresponding 416 * metadata (AppleDouble header file or CAP metadata file). 417 */ 418 struct inode *hfs_iget(struct super_block *sb, struct hfs_cat_key *key, hfs_cat_rec *rec) 419 { 420 struct hfs_iget_data data = { key, rec }; 421 struct inode *inode; 422 u32 cnid; 423 424 switch (rec->type) { 425 case HFS_CDR_DIR: 426 cnid = be32_to_cpu(rec->dir.DirID); 427 break; 428 case HFS_CDR_FIL: 429 cnid = be32_to_cpu(rec->file.FlNum); 430 break; 431 default: 432 return NULL; 433 } 434 inode = iget5_locked(sb, cnid, hfs_test_inode, hfs_read_inode, &data); 435 if (inode && (inode_state_read_once(inode) & I_NEW)) 436 unlock_new_inode(inode); 437 return inode; 438 } 439 440 void hfs_inode_write_fork(struct inode *inode, struct hfs_extent *ext, 441 __be32 *log_size, __be32 *phys_size) 442 { 443 memcpy(ext, HFS_I(inode)->first_extents, sizeof(hfs_extent_rec)); 444 445 if (log_size) 446 *log_size = cpu_to_be32(inode->i_size); 447 if (phys_size) 448 *phys_size = cpu_to_be32(HFS_I(inode)->alloc_blocks * 449 HFS_SB(inode->i_sb)->alloc_blksz); 450 } 451 452 int hfs_write_inode(struct inode *inode, struct writeback_control *wbc) 453 { 454 struct inode *main_inode = inode; 455 struct hfs_find_data fd; 456 hfs_cat_rec rec; 457 int res; 458 459 hfs_dbg("ino %llu\n", inode->i_ino); 460 res = hfs_ext_write_extent(inode); 461 if (res) 462 return res; 463 464 if (inode->i_ino < HFS_FIRSTUSER_CNID) { 465 switch (inode->i_ino) { 466 case HFS_ROOT_CNID: 467 break; 468 case HFS_EXT_CNID: 469 hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree); 470 return 0; 471 case HFS_CAT_CNID: 472 hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree); 473 return 0; 474 default: 475 BUG(); 476 return -EIO; 477 } 478 } 479 480 if (HFS_IS_RSRC(inode)) 481 main_inode = HFS_I(inode)->rsrc_inode; 482 483 if (!main_inode->i_nlink) 484 return 0; 485 486 if (hfs_find_init(HFS_SB(main_inode->i_sb)->cat_tree, &fd)) 487 /* panic? */ 488 return -EIO; 489 490 res = -EIO; 491 if (HFS_I(main_inode)->cat_key.CName.len > HFS_NAMELEN) 492 goto out; 493 fd.search_key->cat = HFS_I(main_inode)->cat_key; 494 if (hfs_brec_find(&fd)) 495 goto out; 496 497 if (S_ISDIR(main_inode->i_mode)) { 498 if (fd.entrylength < sizeof(struct hfs_cat_dir)) 499 goto out; 500 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, 501 sizeof(struct hfs_cat_dir)); 502 if (rec.type != HFS_CDR_DIR || 503 be32_to_cpu(rec.dir.DirID) != inode->i_ino) { 504 } 505 506 rec.dir.MdDat = hfs_u_to_mtime(inode_get_mtime(inode)); 507 rec.dir.Val = cpu_to_be16(inode->i_size - 2); 508 509 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset, 510 sizeof(struct hfs_cat_dir)); 511 } else if (HFS_IS_RSRC(inode)) { 512 if (fd.entrylength < sizeof(struct hfs_cat_file)) 513 goto out; 514 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, 515 sizeof(struct hfs_cat_file)); 516 hfs_inode_write_fork(inode, rec.file.RExtRec, 517 &rec.file.RLgLen, &rec.file.RPyLen); 518 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset, 519 sizeof(struct hfs_cat_file)); 520 } else { 521 if (fd.entrylength < sizeof(struct hfs_cat_file)) 522 goto out; 523 hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, 524 sizeof(struct hfs_cat_file)); 525 if (rec.type != HFS_CDR_FIL || 526 be32_to_cpu(rec.file.FlNum) != inode->i_ino) { 527 } 528 529 if (inode->i_mode & S_IWUSR) 530 rec.file.Flags &= ~HFS_FIL_LOCK; 531 else 532 rec.file.Flags |= HFS_FIL_LOCK; 533 hfs_inode_write_fork(inode, rec.file.ExtRec, &rec.file.LgLen, &rec.file.PyLen); 534 rec.file.MdDat = hfs_u_to_mtime(inode_get_mtime(inode)); 535 536 hfs_bnode_write(fd.bnode, &rec, fd.entryoffset, 537 sizeof(struct hfs_cat_file)); 538 } 539 res = 0; 540 out: 541 hfs_find_exit(&fd); 542 return res; 543 } 544 545 static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry, 546 unsigned int flags) 547 { 548 struct inode *inode = NULL; 549 hfs_cat_rec rec; 550 struct hfs_find_data fd; 551 int res; 552 553 if (HFS_IS_RSRC(dir) || strcmp(dentry->d_name.name, "rsrc")) 554 goto out; 555 556 inode = HFS_I(dir)->rsrc_inode; 557 if (inode) 558 goto out; 559 560 inode = new_inode(dir->i_sb); 561 if (!inode) 562 return ERR_PTR(-ENOMEM); 563 564 res = hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd); 565 if (res) { 566 iput(inode); 567 return ERR_PTR(res); 568 } 569 fd.search_key->cat = HFS_I(dir)->cat_key; 570 res = hfs_brec_read(&fd, &rec, sizeof(rec)); 571 if (!res) { 572 struct hfs_iget_data idata = { NULL, &rec }; 573 hfs_read_inode(inode, &idata); 574 } 575 hfs_find_exit(&fd); 576 if (res) { 577 iput(inode); 578 return ERR_PTR(res); 579 } 580 HFS_I(inode)->rsrc_inode = dir; 581 HFS_I(dir)->rsrc_inode = inode; 582 igrab(dir); 583 inode_fake_hash(inode); 584 mark_inode_dirty(inode); 585 dont_mount(dentry); 586 out: 587 return d_splice_alias(inode, dentry); 588 } 589 590 void hfs_evict_inode(struct inode *inode) 591 { 592 truncate_inode_pages_final(&inode->i_data); 593 clear_inode(inode); 594 if (HFS_IS_RSRC(inode) && HFS_I(inode)->rsrc_inode) { 595 HFS_I(HFS_I(inode)->rsrc_inode)->rsrc_inode = NULL; 596 iput(HFS_I(inode)->rsrc_inode); 597 } 598 } 599 600 static int hfs_file_open(struct inode *inode, struct file *file) 601 { 602 if (HFS_IS_RSRC(inode)) 603 inode = HFS_I(inode)->rsrc_inode; 604 atomic_inc(&HFS_I(inode)->opencnt); 605 return 0; 606 } 607 608 static int hfs_file_release(struct inode *inode, struct file *file) 609 { 610 //struct super_block *sb = inode->i_sb; 611 612 if (HFS_IS_RSRC(inode)) 613 inode = HFS_I(inode)->rsrc_inode; 614 if (atomic_dec_and_test(&HFS_I(inode)->opencnt)) { 615 inode_lock(inode); 616 hfs_file_truncate(inode); 617 //if (inode->i_flags & S_DEAD) { 618 // hfs_delete_cat(inode->i_ino, HFSPLUS_SB(sb).hidden_dir, NULL); 619 // hfs_delete_inode(inode); 620 //} 621 inode_unlock(inode); 622 } 623 return 0; 624 } 625 626 int hfs_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 627 struct iattr *attr) 628 { 629 struct inode *inode = d_inode(dentry); 630 struct hfs_sb_info *hsb = HFS_SB(inode->i_sb); 631 int error; 632 633 error = setattr_prepare(&nop_mnt_idmap, dentry, attr); 634 if (error) 635 return error; 636 637 /* no uig/gid changes and limit which mode bits can be set */ 638 if (((attr->ia_valid & ATTR_UID) && 639 (!uid_eq(attr->ia_uid, hsb->s_uid))) || 640 ((attr->ia_valid & ATTR_GID) && 641 (!gid_eq(attr->ia_gid, hsb->s_gid))) || 642 ((attr->ia_valid & ATTR_MODE) && 643 ((S_ISDIR(inode->i_mode) && 644 (attr->ia_mode != inode->i_mode)) || 645 (attr->ia_mode & ~HFS_VALID_MODE_BITS)))) { 646 return hsb->s_quiet ? 0 : error; 647 } 648 649 /* map file permissions to the closest allowable permissions in HFS */ 650 if (attr->ia_valid & ATTR_MODE) { 651 /* Only the 'w' bits can ever change and only all together. */ 652 if (attr->ia_mode & S_IWUSR) 653 attr->ia_mode = inode->i_mode | S_IWUGO; 654 else 655 attr->ia_mode = inode->i_mode & ~S_IWUGO; 656 attr->ia_mode &= S_ISDIR(inode->i_mode) ? ~hsb->s_dir_umask: ~hsb->s_file_umask; 657 } 658 659 if ((attr->ia_valid & ATTR_SIZE) && 660 attr->ia_size != i_size_read(inode)) { 661 inode_dio_wait(inode); 662 663 error = inode_newsize_ok(inode, attr->ia_size); 664 if (error) 665 return error; 666 667 truncate_setsize(inode, attr->ia_size); 668 hfs_file_truncate(inode); 669 simple_inode_init_ts(inode); 670 } 671 672 setattr_copy(&nop_mnt_idmap, inode, attr); 673 mark_inode_dirty(inode); 674 return 0; 675 } 676 677 static int hfs_file_fsync(struct file *filp, loff_t start, loff_t end, 678 int datasync) 679 { 680 struct inode *inode = filp->f_mapping->host; 681 struct super_block * sb; 682 int ret, err; 683 684 ret = file_write_and_wait_range(filp, start, end); 685 if (ret) 686 return ret; 687 inode_lock(inode); 688 689 /* sync the inode to buffers */ 690 ret = write_inode_now(inode, 0); 691 692 /* sync the superblock to buffers */ 693 sb = inode->i_sb; 694 flush_delayed_work(&HFS_SB(sb)->mdb_work); 695 /* .. finally sync the buffers to disk */ 696 err = sync_blockdev(sb->s_bdev); 697 if (!ret) 698 ret = err; 699 inode_unlock(inode); 700 return ret; 701 } 702 703 int hfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa) 704 { 705 /* 706 * HFS compares filenames using Mac OS Roman case folding, so 707 * lookup is always case-insensitive. Names are stored on disk 708 * with case intact; CASENONPRESERVING stays clear. 709 */ 710 fa->fsx_xflags |= FS_XFLAG_CASEFOLD; 711 fa->flags |= FS_CASEFOLD_FL; 712 return 0; 713 } 714 715 static const struct file_operations hfs_file_operations = { 716 .llseek = generic_file_llseek, 717 .read_iter = generic_file_read_iter, 718 .write_iter = generic_file_write_iter, 719 .mmap_prepare = generic_file_mmap_prepare, 720 .splice_read = filemap_splice_read, 721 .splice_write = iter_file_splice_write, 722 .fsync = hfs_file_fsync, 723 .open = hfs_file_open, 724 .release = hfs_file_release, 725 }; 726 727 static const struct inode_operations hfs_file_inode_operations = { 728 .lookup = hfs_file_lookup, 729 .setattr = hfs_inode_setattr, 730 .listxattr = generic_listxattr, 731 .fileattr_get = hfs_fileattr_get, 732 }; 733