1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/fs/fat/file.c 4 * 5 * Written 1992,1993 by Werner Almesberger 6 * 7 * regular file handling primitives for fat-based filesystems 8 */ 9 10 #include <linux/capability.h> 11 #include <linux/module.h> 12 #include <linux/compat.h> 13 #include <linux/mount.h> 14 #include <linux/blkdev.h> 15 #include <linux/backing-dev.h> 16 #include <linux/filelock.h> 17 #include <linux/fsnotify.h> 18 #include <linux/security.h> 19 #include <linux/falloc.h> 20 #include <linux/fileattr.h> 21 #include "fat.h" 22 23 static long fat_fallocate(struct file *file, int mode, 24 loff_t offset, loff_t len); 25 26 static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr) 27 { 28 u32 attr; 29 30 inode_lock_shared(inode); 31 attr = fat_make_attrs(inode); 32 inode_unlock_shared(inode); 33 34 return put_user(attr, user_attr); 35 } 36 37 static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr) 38 { 39 struct inode *inode = file_inode(file); 40 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 41 int is_dir = S_ISDIR(inode->i_mode); 42 u32 attr, oldattr; 43 struct iattr ia; 44 int err; 45 46 err = get_user(attr, user_attr); 47 if (err) 48 goto out; 49 50 err = mnt_want_write_file(file); 51 if (err) 52 goto out; 53 inode_lock(inode); 54 55 /* 56 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also 57 * prevents the user from turning us into a VFAT 58 * longname entry. Also, we obviously can't set 59 * any of the NTFS attributes in the high 24 bits. 60 */ 61 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR); 62 /* Merge in ATTR_VOLUME and ATTR_DIR */ 63 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) | 64 (is_dir ? ATTR_DIR : 0); 65 oldattr = fat_make_attrs(inode); 66 67 /* Equivalent to a chmod() */ 68 ia.ia_valid = ATTR_MODE | ATTR_CTIME; 69 ia.ia_ctime = current_time(inode); 70 if (is_dir) 71 ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO); 72 else { 73 ia.ia_mode = fat_make_mode(sbi, attr, 74 S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO)); 75 } 76 77 /* The root directory has no attributes */ 78 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) { 79 err = -EINVAL; 80 goto out_unlock_inode; 81 } 82 83 if (sbi->options.sys_immutable && 84 ((attr | oldattr) & ATTR_SYS) && 85 !capable(CAP_LINUX_IMMUTABLE)) { 86 err = -EPERM; 87 goto out_unlock_inode; 88 } 89 90 /* 91 * The security check is questionable... We single 92 * out the RO attribute for checking by the security 93 * module, just because it maps to a file mode. 94 */ 95 err = security_inode_setattr(file_mnt_idmap(file), 96 file->f_path.dentry, &ia); 97 if (err) 98 goto out_unlock_inode; 99 100 /* This MUST be done before doing anything irreversible... */ 101 err = fat_setattr(file_mnt_idmap(file), file->f_path.dentry, &ia); 102 if (err) 103 goto out_unlock_inode; 104 105 fsnotify_change(file->f_path.dentry, ia.ia_valid); 106 if (sbi->options.sys_immutable) { 107 if (attr & ATTR_SYS) 108 inode->i_flags |= S_IMMUTABLE; 109 else 110 inode->i_flags &= ~S_IMMUTABLE; 111 } 112 113 fat_save_attrs(inode, attr); 114 mark_inode_dirty(inode); 115 out_unlock_inode: 116 inode_unlock(inode); 117 mnt_drop_write_file(file); 118 out: 119 return err; 120 } 121 122 static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr) 123 { 124 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 125 return put_user(sbi->vol_id, user_attr); 126 } 127 128 static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) 129 { 130 struct super_block *sb = inode->i_sb; 131 struct fstrim_range __user *user_range; 132 struct fstrim_range range; 133 int err; 134 135 if (!capable(CAP_SYS_ADMIN)) 136 return -EPERM; 137 138 if (!bdev_max_discard_sectors(sb->s_bdev)) 139 return -EOPNOTSUPP; 140 141 user_range = (struct fstrim_range __user *)arg; 142 if (copy_from_user(&range, user_range, sizeof(range))) 143 return -EFAULT; 144 145 range.minlen = max(range.minlen, bdev_discard_granularity(sb->s_bdev)); 146 147 err = fat_trim_fs(inode, &range); 148 if (err < 0) 149 return err; 150 151 if (copy_to_user(user_range, &range, sizeof(range))) 152 return -EFAULT; 153 154 return 0; 155 } 156 157 long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 158 { 159 struct inode *inode = file_inode(filp); 160 u32 __user *user_attr = (u32 __user *)arg; 161 162 switch (cmd) { 163 case FAT_IOCTL_GET_ATTRIBUTES: 164 return fat_ioctl_get_attributes(inode, user_attr); 165 case FAT_IOCTL_SET_ATTRIBUTES: 166 return fat_ioctl_set_attributes(filp, user_attr); 167 case FAT_IOCTL_GET_VOLUME_ID: 168 return fat_ioctl_get_volume_id(inode, user_attr); 169 case FITRIM: 170 return fat_ioctl_fitrim(inode, arg); 171 default: 172 return -ENOTTY; /* Inappropriate ioctl for device */ 173 } 174 } 175 176 static int fat_file_release(struct inode *inode, struct file *filp) 177 { 178 if ((filp->f_mode & FMODE_WRITE) && 179 MSDOS_SB(inode->i_sb)->options.flush) { 180 fat_flush_inodes(inode->i_sb, inode, NULL); 181 set_current_state(TASK_UNINTERRUPTIBLE); 182 io_schedule_timeout(HZ/10); 183 } 184 return 0; 185 } 186 187 int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) 188 { 189 struct inode *inode = filp->f_mapping->host; 190 struct inode *fat_inode = MSDOS_SB(inode->i_sb)->fat_inode; 191 int err; 192 193 err = simple_fsync_noflush(filp, start, end, datasync); 194 if (err) 195 return err; 196 197 err = mmb_sync(&MSDOS_I(fat_inode)->i_metadata_bhs); 198 if (err) 199 return err; 200 201 return blkdev_issue_flush(inode->i_sb->s_bdev); 202 } 203 204 205 const struct file_operations fat_file_operations = { 206 .llseek = generic_file_llseek, 207 .read_iter = generic_file_read_iter, 208 .write_iter = generic_file_write_iter, 209 .mmap_prepare = generic_file_mmap_prepare, 210 .release = fat_file_release, 211 .unlocked_ioctl = fat_generic_ioctl, 212 .compat_ioctl = compat_ptr_ioctl, 213 .fsync = fat_file_fsync, 214 .splice_read = filemap_splice_read, 215 .splice_write = iter_file_splice_write, 216 .fallocate = fat_fallocate, 217 .setlease = generic_setlease, 218 }; 219 220 static int fat_cont_expand(struct inode *inode, loff_t size) 221 { 222 struct address_space *mapping = inode->i_mapping; 223 loff_t start = inode->i_size, count = size - inode->i_size; 224 int err; 225 226 err = generic_cont_expand_simple(inode, size); 227 if (err) 228 goto out; 229 230 fat_truncate_time(inode, NULL, FAT_UPDATE_CMTIME); 231 mark_inode_dirty(inode); 232 if (IS_SYNC(inode)) { 233 int err2; 234 235 /* 236 * Opencode syncing since we don't have a file open to use 237 * standard fsync path. 238 */ 239 err = filemap_fdatawrite_range(mapping, start, 240 start + count - 1); 241 err2 = mmb_sync(&MSDOS_I(inode)->i_metadata_bhs); 242 if (!err) 243 err = err2; 244 err2 = write_inode_now(inode, 1); 245 if (!err) 246 err = err2; 247 if (!err) { 248 err = filemap_fdatawait_range(mapping, start, 249 start + count - 1); 250 } 251 } 252 out: 253 return err; 254 } 255 256 /* 257 * Preallocate space for a file. This implements fat's fallocate file 258 * operation, which gets called from sys_fallocate system call. User 259 * space requests len bytes at offset. If FALLOC_FL_KEEP_SIZE is set 260 * we just allocate clusters without zeroing them out. Otherwise we 261 * allocate and zero out clusters via an expanding truncate. 262 */ 263 static long fat_fallocate(struct file *file, int mode, 264 loff_t offset, loff_t len) 265 { 266 int nr_cluster; /* Number of clusters to be allocated */ 267 loff_t mm_bytes; /* Number of bytes to be allocated for file */ 268 loff_t ondisksize; /* block aligned on-disk size in bytes*/ 269 struct inode *inode = file->f_mapping->host; 270 struct super_block *sb = inode->i_sb; 271 struct msdos_sb_info *sbi = MSDOS_SB(sb); 272 int err = 0; 273 274 /* No support for hole punch or other fallocate flags. */ 275 if (mode & ~FALLOC_FL_KEEP_SIZE) 276 return -EOPNOTSUPP; 277 278 /* No support for dir */ 279 if (!S_ISREG(inode->i_mode)) 280 return -EOPNOTSUPP; 281 282 inode_lock(inode); 283 if (mode & FALLOC_FL_KEEP_SIZE) { 284 ondisksize = inode->i_blocks << 9; 285 if ((offset + len) <= ondisksize) 286 goto error; 287 288 /* First compute the number of clusters to be allocated */ 289 mm_bytes = offset + len - ondisksize; 290 nr_cluster = (mm_bytes + (sbi->cluster_size - 1)) >> 291 sbi->cluster_bits; 292 293 /* Start the allocation.We are not zeroing out the clusters */ 294 while (nr_cluster-- > 0) { 295 err = fat_add_cluster(inode); 296 if (err) 297 goto error; 298 } 299 } else { 300 if ((offset + len) <= i_size_read(inode)) 301 goto error; 302 303 /* This is just an expanding truncate */ 304 err = fat_cont_expand(inode, (offset + len)); 305 } 306 307 error: 308 inode_unlock(inode); 309 return err; 310 } 311 312 /* Free all clusters after the skip'th cluster. */ 313 static int fat_free(struct inode *inode, int skip) 314 { 315 struct super_block *sb = inode->i_sb; 316 int err, wait, free_start, i_start, i_logstart; 317 318 if (MSDOS_I(inode)->i_start == 0) 319 return 0; 320 321 fat_cache_inval_inode(inode); 322 323 wait = IS_DIRSYNC(inode); 324 i_start = free_start = MSDOS_I(inode)->i_start; 325 i_logstart = MSDOS_I(inode)->i_logstart; 326 327 /* First, we write the new file size. */ 328 if (!skip) { 329 MSDOS_I(inode)->i_start = 0; 330 MSDOS_I(inode)->i_logstart = 0; 331 } 332 MSDOS_I(inode)->i_attrs |= ATTR_ARCH; 333 fat_truncate_time(inode, NULL, FAT_UPDATE_CMTIME); 334 mark_inode_dirty(inode); 335 if (wait) { 336 err = sync_inode_metadata(inode, 1); 337 if (err) { 338 MSDOS_I(inode)->i_start = i_start; 339 MSDOS_I(inode)->i_logstart = i_logstart; 340 return err; 341 } 342 } 343 344 /* Write a new EOF, and get the remaining cluster chain for freeing. */ 345 if (skip) { 346 struct fat_entry fatent; 347 int ret, fclus, dclus; 348 349 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus); 350 if (ret < 0) 351 return ret; 352 else if (ret == FAT_ENT_EOF) 353 return 0; 354 355 fatent_init(&fatent); 356 ret = fat_ent_read(inode, &fatent, dclus); 357 if (ret == FAT_ENT_EOF) { 358 fatent_brelse(&fatent); 359 return 0; 360 } else if (ret == FAT_ENT_FREE) { 361 fat_fs_error(sb, 362 "%s: invalid cluster chain (i_pos %lld)", 363 __func__, MSDOS_I(inode)->i_pos); 364 ret = -EIO; 365 } else if (ret > 0) { 366 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait); 367 if (err) 368 ret = err; 369 } 370 fatent_brelse(&fatent); 371 if (ret < 0) 372 return ret; 373 374 free_start = ret; 375 } 376 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9); 377 378 /* Freeing the remained cluster chain */ 379 return fat_free_clusters(inode, free_start); 380 } 381 382 void fat_truncate_blocks(struct inode *inode, loff_t offset) 383 { 384 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 385 const unsigned int cluster_size = sbi->cluster_size; 386 int nr_clusters; 387 388 /* 389 * This protects against truncating a file bigger than it was then 390 * trying to write into the hole. 391 */ 392 if (MSDOS_I(inode)->mmu_private > offset) 393 MSDOS_I(inode)->mmu_private = offset; 394 395 nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits; 396 397 fat_free(inode, nr_clusters); 398 fat_flush_inodes(inode->i_sb, inode, NULL); 399 } 400 401 int fat_fileattr_get(struct dentry *dentry, struct file_kattr *fa) 402 { 403 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); 404 bool case_sensitive; 405 406 /* 407 * FAT filesystems are case-insensitive by default. VFAT 408 * becomes case-sensitive when mounted with 'check=strict', 409 * which installs vfat_dentry_ops. MSDOS has no such option; 410 * its 'nocase' mount option selects case-sensitive matching. 411 * 412 * VFAT long filename entries preserve case. Without VFAT, only 413 * uppercased 8.3 short names are stored. MSDOS with 'nocase' 414 * also preserves case. 415 */ 416 if (sbi->options.isvfat) 417 case_sensitive = sbi->options.name_check == 's'; 418 else 419 case_sensitive = sbi->options.nocase; 420 421 if (!case_sensitive) { 422 fa->fsx_xflags |= FS_XFLAG_CASEFOLD; 423 fa->flags |= FS_CASEFOLD_FL; 424 if (!sbi->options.isvfat) 425 fa->fsx_xflags |= FS_XFLAG_CASENONPRESERVING; 426 } 427 if (d_inode(dentry)->i_flags & S_IMMUTABLE) { 428 fa->fsx_xflags |= FS_XFLAG_IMMUTABLE; 429 fa->flags |= FS_IMMUTABLE_FL; 430 } 431 return 0; 432 } 433 EXPORT_SYMBOL_GPL(fat_fileattr_get); 434 435 int fat_getattr(struct mnt_idmap *idmap, const struct path *path, 436 struct kstat *stat, u32 request_mask, unsigned int flags) 437 { 438 struct inode *inode = d_inode(path->dentry); 439 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 440 441 generic_fillattr(idmap, request_mask, inode, stat); 442 stat->blksize = sbi->cluster_size; 443 444 if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) { 445 /* Use i_pos for ino. This is used as fileid of nfs. */ 446 stat->ino = fat_i_pos_read(sbi, inode); 447 } 448 449 if (sbi->options.isvfat && request_mask & STATX_BTIME) { 450 stat->result_mask |= STATX_BTIME; 451 stat->btime = MSDOS_I(inode)->i_crtime; 452 } 453 454 return 0; 455 } 456 EXPORT_SYMBOL_GPL(fat_getattr); 457 458 static int fat_sanitize_mode(const struct msdos_sb_info *sbi, 459 struct inode *inode, umode_t *mode_ptr) 460 { 461 umode_t mask, perm; 462 463 /* 464 * Note, the basic check is already done by a caller of 465 * (attr->ia_mode & ~FAT_VALID_MODE) 466 */ 467 468 if (S_ISREG(inode->i_mode)) 469 mask = sbi->options.fs_fmask; 470 else 471 mask = sbi->options.fs_dmask; 472 473 perm = *mode_ptr & ~(S_IFMT | mask); 474 475 /* 476 * Of the r and x bits, all (subject to umask) must be present. Of the 477 * w bits, either all (subject to umask) or none must be present. 478 * 479 * If fat_mode_can_hold_ro(inode) is false, can't change w bits. 480 */ 481 if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO))) 482 return -EPERM; 483 if (fat_mode_can_hold_ro(inode)) { 484 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask))) 485 return -EPERM; 486 } else { 487 if ((perm & S_IWUGO) != (S_IWUGO & ~mask)) 488 return -EPERM; 489 } 490 491 *mode_ptr &= S_IFMT | perm; 492 493 return 0; 494 } 495 496 static int fat_allow_set_time(struct mnt_idmap *idmap, 497 struct msdos_sb_info *sbi, struct inode *inode) 498 { 499 umode_t allow_utime = sbi->options.allow_utime; 500 501 if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), 502 current_fsuid())) { 503 if (vfsgid_in_group_p(i_gid_into_vfsgid(idmap, inode))) 504 allow_utime >>= 3; 505 if (allow_utime & MAY_WRITE) 506 return 1; 507 } 508 509 /* use a default check */ 510 return 0; 511 } 512 513 #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET) 514 /* valid file mode bits */ 515 #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO) 516 517 int fat_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 518 struct iattr *attr) 519 { 520 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); 521 struct inode *inode = d_inode(dentry); 522 unsigned int ia_valid; 523 int error; 524 525 /* Check for setting the inode time. */ 526 ia_valid = attr->ia_valid; 527 if (ia_valid & TIMES_SET_FLAGS) { 528 if (fat_allow_set_time(idmap, sbi, inode)) 529 attr->ia_valid &= ~TIMES_SET_FLAGS; 530 } 531 532 error = setattr_prepare(idmap, dentry, attr); 533 attr->ia_valid = ia_valid; 534 if (error) { 535 if (sbi->options.quiet) 536 error = 0; 537 goto out; 538 } 539 540 /* 541 * Expand the file. Since inode_setattr() updates ->i_size 542 * before calling the ->truncate(), but FAT needs to fill the 543 * hole before it. XXX: this is no longer true with new truncate 544 * sequence. 545 */ 546 if (attr->ia_valid & ATTR_SIZE) { 547 inode_dio_wait(inode); 548 549 if (attr->ia_size > inode->i_size) { 550 error = fat_cont_expand(inode, attr->ia_size); 551 if (error || attr->ia_valid == ATTR_SIZE) 552 goto out; 553 attr->ia_valid &= ~ATTR_SIZE; 554 } 555 } 556 557 if (((attr->ia_valid & ATTR_UID) && 558 (!uid_eq(from_vfsuid(idmap, i_user_ns(inode), attr->ia_vfsuid), 559 sbi->options.fs_uid))) || 560 ((attr->ia_valid & ATTR_GID) && 561 (!gid_eq(from_vfsgid(idmap, i_user_ns(inode), attr->ia_vfsgid), 562 sbi->options.fs_gid))) || 563 ((attr->ia_valid & ATTR_MODE) && 564 (attr->ia_mode & ~FAT_VALID_MODE))) 565 error = -EPERM; 566 567 if (error) { 568 if (sbi->options.quiet) 569 error = 0; 570 goto out; 571 } 572 573 /* 574 * We don't return -EPERM here. Yes, strange, but this is too 575 * old behavior. 576 */ 577 if (attr->ia_valid & ATTR_MODE) { 578 if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0) 579 attr->ia_valid &= ~ATTR_MODE; 580 } 581 582 if (attr->ia_valid & ATTR_SIZE) { 583 error = fat_block_truncate_page(inode, attr->ia_size); 584 if (error) 585 goto out; 586 down_write(&MSDOS_I(inode)->truncate_lock); 587 truncate_setsize(inode, attr->ia_size); 588 fat_truncate_blocks(inode, attr->ia_size); 589 up_write(&MSDOS_I(inode)->truncate_lock); 590 } 591 592 /* 593 * setattr_copy can't truncate these appropriately, so we'll copy them 594 * ourselves. See fat_truncate_time for the c/mtime logic on fat. 595 */ 596 if (attr->ia_valid & ATTR_ATIME) 597 fat_truncate_time(inode, &attr->ia_atime, FAT_UPDATE_ATIME); 598 if (attr->ia_valid & ATTR_MTIME) 599 fat_truncate_time(inode, &attr->ia_mtime, FAT_UPDATE_CMTIME); 600 attr->ia_valid &= ~(ATTR_ATIME|ATTR_CTIME|ATTR_MTIME); 601 602 setattr_copy(idmap, inode, attr); 603 mark_inode_dirty(inode); 604 out: 605 return error; 606 } 607 EXPORT_SYMBOL_GPL(fat_setattr); 608 609 const struct inode_operations fat_file_inode_operations = { 610 .setattr = fat_setattr, 611 .getattr = fat_getattr, 612 .fileattr_get = fat_fileattr_get, 613 .update_time = fat_update_time, 614 }; 615