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