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 = mmb_fsync_noflush(filp, &MSDOS_I(inode)->i_metadata_bhs, 194 start, end, datasync); 195 if (err) 196 return err; 197 198 err = mmb_sync(&MSDOS_I(fat_inode)->i_metadata_bhs); 199 if (err) 200 return err; 201 202 return blkdev_issue_flush(inode->i_sb->s_bdev); 203 } 204 205 206 const struct file_operations fat_file_operations = { 207 .llseek = generic_file_llseek, 208 .read_iter = generic_file_read_iter, 209 .write_iter = generic_file_write_iter, 210 .mmap_prepare = generic_file_mmap_prepare, 211 .release = fat_file_release, 212 .unlocked_ioctl = fat_generic_ioctl, 213 .compat_ioctl = compat_ptr_ioctl, 214 .fsync = fat_file_fsync, 215 .splice_read = filemap_splice_read, 216 .splice_write = iter_file_splice_write, 217 .fallocate = fat_fallocate, 218 .setlease = generic_setlease, 219 }; 220 221 static int fat_cont_expand(struct inode *inode, loff_t size) 222 { 223 struct address_space *mapping = inode->i_mapping; 224 loff_t start = inode->i_size, count = size - inode->i_size; 225 int err; 226 227 err = generic_cont_expand_simple(inode, size); 228 if (err) 229 goto out; 230 231 fat_truncate_time(inode, NULL, FAT_UPDATE_CMTIME); 232 mark_inode_dirty(inode); 233 if (IS_SYNC(inode)) { 234 int err2; 235 236 /* 237 * Opencode syncing since we don't have a file open to use 238 * standard fsync path. 239 */ 240 err = filemap_fdatawrite_range(mapping, start, 241 start + count - 1); 242 err2 = mmb_sync(&MSDOS_I(inode)->i_metadata_bhs); 243 if (!err) 244 err = err2; 245 err2 = write_inode_now(inode, 1); 246 if (!err) 247 err = err2; 248 if (!err) { 249 err = filemap_fdatawait_range(mapping, start, 250 start + count - 1); 251 } 252 } 253 out: 254 return err; 255 } 256 257 /* 258 * Preallocate space for a file. This implements fat's fallocate file 259 * operation, which gets called from sys_fallocate system call. User 260 * space requests len bytes at offset. If FALLOC_FL_KEEP_SIZE is set 261 * we just allocate clusters without zeroing them out. Otherwise we 262 * allocate and zero out clusters via an expanding truncate. 263 */ 264 static long fat_fallocate(struct file *file, int mode, 265 loff_t offset, loff_t len) 266 { 267 int nr_cluster; /* Number of clusters to be allocated */ 268 loff_t mm_bytes; /* Number of bytes to be allocated for file */ 269 loff_t ondisksize; /* block aligned on-disk size in bytes*/ 270 struct inode *inode = file->f_mapping->host; 271 struct super_block *sb = inode->i_sb; 272 struct msdos_sb_info *sbi = MSDOS_SB(sb); 273 int err = 0; 274 275 /* No support for hole punch or other fallocate flags. */ 276 if (mode & ~FALLOC_FL_KEEP_SIZE) 277 return -EOPNOTSUPP; 278 279 /* No support for dir */ 280 if (!S_ISREG(inode->i_mode)) 281 return -EOPNOTSUPP; 282 283 inode_lock(inode); 284 if (mode & FALLOC_FL_KEEP_SIZE) { 285 ondisksize = inode->i_blocks << 9; 286 if ((offset + len) <= ondisksize) 287 goto error; 288 289 /* First compute the number of clusters to be allocated */ 290 mm_bytes = offset + len - ondisksize; 291 nr_cluster = (mm_bytes + (sbi->cluster_size - 1)) >> 292 sbi->cluster_bits; 293 294 /* Start the allocation.We are not zeroing out the clusters */ 295 while (nr_cluster-- > 0) { 296 err = fat_add_cluster(inode); 297 if (err) 298 goto error; 299 } 300 } else { 301 if ((offset + len) <= i_size_read(inode)) 302 goto error; 303 304 /* This is just an expanding truncate */ 305 err = fat_cont_expand(inode, (offset + len)); 306 } 307 308 error: 309 inode_unlock(inode); 310 return err; 311 } 312 313 /* Free all clusters after the skip'th cluster. */ 314 static int fat_free(struct inode *inode, int skip) 315 { 316 struct super_block *sb = inode->i_sb; 317 int err, wait, free_start, i_start, i_logstart; 318 319 if (MSDOS_I(inode)->i_start == 0) 320 return 0; 321 322 fat_cache_inval_inode(inode); 323 324 wait = IS_DIRSYNC(inode); 325 i_start = free_start = MSDOS_I(inode)->i_start; 326 i_logstart = MSDOS_I(inode)->i_logstart; 327 328 /* First, we write the new file size. */ 329 if (!skip) { 330 MSDOS_I(inode)->i_start = 0; 331 MSDOS_I(inode)->i_logstart = 0; 332 } 333 MSDOS_I(inode)->i_attrs |= ATTR_ARCH; 334 fat_truncate_time(inode, NULL, FAT_UPDATE_CMTIME); 335 if (wait) { 336 err = fat_sync_inode(inode); 337 if (err) { 338 MSDOS_I(inode)->i_start = i_start; 339 MSDOS_I(inode)->i_logstart = i_logstart; 340 return err; 341 } 342 } else 343 mark_inode_dirty(inode); 344 345 /* Write a new EOF, and get the remaining cluster chain for freeing. */ 346 if (skip) { 347 struct fat_entry fatent; 348 int ret, fclus, dclus; 349 350 ret = fat_get_cluster(inode, skip - 1, &fclus, &dclus); 351 if (ret < 0) 352 return ret; 353 else if (ret == FAT_ENT_EOF) 354 return 0; 355 356 fatent_init(&fatent); 357 ret = fat_ent_read(inode, &fatent, dclus); 358 if (ret == FAT_ENT_EOF) { 359 fatent_brelse(&fatent); 360 return 0; 361 } else if (ret == FAT_ENT_FREE) { 362 fat_fs_error(sb, 363 "%s: invalid cluster chain (i_pos %lld)", 364 __func__, MSDOS_I(inode)->i_pos); 365 ret = -EIO; 366 } else if (ret > 0) { 367 err = fat_ent_write(inode, &fatent, FAT_ENT_EOF, wait); 368 if (err) 369 ret = err; 370 } 371 fatent_brelse(&fatent); 372 if (ret < 0) 373 return ret; 374 375 free_start = ret; 376 } 377 inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9); 378 379 /* Freeing the remained cluster chain */ 380 return fat_free_clusters(inode, free_start); 381 } 382 383 void fat_truncate_blocks(struct inode *inode, loff_t offset) 384 { 385 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 386 const unsigned int cluster_size = sbi->cluster_size; 387 int nr_clusters; 388 389 /* 390 * This protects against truncating a file bigger than it was then 391 * trying to write into the hole. 392 */ 393 if (MSDOS_I(inode)->mmu_private > offset) 394 MSDOS_I(inode)->mmu_private = offset; 395 396 nr_clusters = (offset + (cluster_size - 1)) >> sbi->cluster_bits; 397 398 fat_free(inode, nr_clusters); 399 fat_flush_inodes(inode->i_sb, inode, NULL); 400 } 401 402 int fat_fileattr_get(struct dentry *dentry, struct file_kattr *fa) 403 { 404 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); 405 bool case_sensitive; 406 407 /* 408 * FAT filesystems are case-insensitive by default. VFAT 409 * becomes case-sensitive when mounted with 'check=strict', 410 * which installs vfat_dentry_ops. MSDOS has no such option; 411 * its 'nocase' mount option selects case-sensitive matching. 412 * 413 * VFAT long filename entries preserve case. Without VFAT, only 414 * uppercased 8.3 short names are stored. MSDOS with 'nocase' 415 * also preserves case. 416 */ 417 if (sbi->options.isvfat) 418 case_sensitive = sbi->options.name_check == 's'; 419 else 420 case_sensitive = sbi->options.nocase; 421 422 if (!case_sensitive) { 423 fa->fsx_xflags |= FS_XFLAG_CASEFOLD; 424 fa->flags |= FS_CASEFOLD_FL; 425 if (!sbi->options.isvfat) 426 fa->fsx_xflags |= FS_XFLAG_CASENONPRESERVING; 427 } 428 if (d_inode(dentry)->i_flags & S_IMMUTABLE) { 429 fa->fsx_xflags |= FS_XFLAG_IMMUTABLE; 430 fa->flags |= FS_IMMUTABLE_FL; 431 } 432 return 0; 433 } 434 EXPORT_SYMBOL_GPL(fat_fileattr_get); 435 436 int fat_getattr(struct mnt_idmap *idmap, const struct path *path, 437 struct kstat *stat, u32 request_mask, unsigned int flags) 438 { 439 struct inode *inode = d_inode(path->dentry); 440 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 441 442 generic_fillattr(idmap, request_mask, inode, stat); 443 stat->blksize = sbi->cluster_size; 444 445 if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) { 446 /* Use i_pos for ino. This is used as fileid of nfs. */ 447 stat->ino = fat_i_pos_read(sbi, inode); 448 } 449 450 if (sbi->options.isvfat && request_mask & STATX_BTIME) { 451 stat->result_mask |= STATX_BTIME; 452 stat->btime = MSDOS_I(inode)->i_crtime; 453 } 454 455 return 0; 456 } 457 EXPORT_SYMBOL_GPL(fat_getattr); 458 459 static int fat_sanitize_mode(const struct msdos_sb_info *sbi, 460 struct inode *inode, umode_t *mode_ptr) 461 { 462 umode_t mask, perm; 463 464 /* 465 * Note, the basic check is already done by a caller of 466 * (attr->ia_mode & ~FAT_VALID_MODE) 467 */ 468 469 if (S_ISREG(inode->i_mode)) 470 mask = sbi->options.fs_fmask; 471 else 472 mask = sbi->options.fs_dmask; 473 474 perm = *mode_ptr & ~(S_IFMT | mask); 475 476 /* 477 * Of the r and x bits, all (subject to umask) must be present. Of the 478 * w bits, either all (subject to umask) or none must be present. 479 * 480 * If fat_mode_can_hold_ro(inode) is false, can't change w bits. 481 */ 482 if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO))) 483 return -EPERM; 484 if (fat_mode_can_hold_ro(inode)) { 485 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask))) 486 return -EPERM; 487 } else { 488 if ((perm & S_IWUGO) != (S_IWUGO & ~mask)) 489 return -EPERM; 490 } 491 492 *mode_ptr &= S_IFMT | perm; 493 494 return 0; 495 } 496 497 static int fat_allow_set_time(struct mnt_idmap *idmap, 498 struct msdos_sb_info *sbi, struct inode *inode) 499 { 500 umode_t allow_utime = sbi->options.allow_utime; 501 502 if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), 503 current_fsuid())) { 504 if (vfsgid_in_group_p(i_gid_into_vfsgid(idmap, inode))) 505 allow_utime >>= 3; 506 if (allow_utime & MAY_WRITE) 507 return 1; 508 } 509 510 /* use a default check */ 511 return 0; 512 } 513 514 #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET) 515 /* valid file mode bits */ 516 #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO) 517 518 int fat_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 519 struct iattr *attr) 520 { 521 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); 522 struct inode *inode = d_inode(dentry); 523 unsigned int ia_valid; 524 int error; 525 526 /* Check for setting the inode time. */ 527 ia_valid = attr->ia_valid; 528 if (ia_valid & TIMES_SET_FLAGS) { 529 if (fat_allow_set_time(idmap, sbi, inode)) 530 attr->ia_valid &= ~TIMES_SET_FLAGS; 531 } 532 533 error = setattr_prepare(idmap, dentry, attr); 534 attr->ia_valid = ia_valid; 535 if (error) { 536 if (sbi->options.quiet) 537 error = 0; 538 goto out; 539 } 540 541 /* 542 * Expand the file. Since inode_setattr() updates ->i_size 543 * before calling the ->truncate(), but FAT needs to fill the 544 * hole before it. XXX: this is no longer true with new truncate 545 * sequence. 546 */ 547 if (attr->ia_valid & ATTR_SIZE) { 548 inode_dio_wait(inode); 549 550 if (attr->ia_size > inode->i_size) { 551 error = fat_cont_expand(inode, attr->ia_size); 552 if (error || attr->ia_valid == ATTR_SIZE) 553 goto out; 554 attr->ia_valid &= ~ATTR_SIZE; 555 } 556 } 557 558 if (((attr->ia_valid & ATTR_UID) && 559 (!uid_eq(from_vfsuid(idmap, i_user_ns(inode), attr->ia_vfsuid), 560 sbi->options.fs_uid))) || 561 ((attr->ia_valid & ATTR_GID) && 562 (!gid_eq(from_vfsgid(idmap, i_user_ns(inode), attr->ia_vfsgid), 563 sbi->options.fs_gid))) || 564 ((attr->ia_valid & ATTR_MODE) && 565 (attr->ia_mode & ~FAT_VALID_MODE))) 566 error = -EPERM; 567 568 if (error) { 569 if (sbi->options.quiet) 570 error = 0; 571 goto out; 572 } 573 574 /* 575 * We don't return -EPERM here. Yes, strange, but this is too 576 * old behavior. 577 */ 578 if (attr->ia_valid & ATTR_MODE) { 579 if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0) 580 attr->ia_valid &= ~ATTR_MODE; 581 } 582 583 if (attr->ia_valid & ATTR_SIZE) { 584 error = fat_block_truncate_page(inode, attr->ia_size); 585 if (error) 586 goto out; 587 down_write(&MSDOS_I(inode)->truncate_lock); 588 truncate_setsize(inode, attr->ia_size); 589 fat_truncate_blocks(inode, attr->ia_size); 590 up_write(&MSDOS_I(inode)->truncate_lock); 591 } 592 593 /* 594 * setattr_copy can't truncate these appropriately, so we'll copy them 595 * ourselves. See fat_truncate_time for the c/mtime logic on fat. 596 */ 597 if (attr->ia_valid & ATTR_ATIME) 598 fat_truncate_time(inode, &attr->ia_atime, FAT_UPDATE_ATIME); 599 if (attr->ia_valid & ATTR_MTIME) 600 fat_truncate_time(inode, &attr->ia_mtime, FAT_UPDATE_CMTIME); 601 attr->ia_valid &= ~(ATTR_ATIME|ATTR_CTIME|ATTR_MTIME); 602 603 setattr_copy(idmap, inode, attr); 604 mark_inode_dirty(inode); 605 out: 606 return error; 607 } 608 EXPORT_SYMBOL_GPL(fat_setattr); 609 610 const struct inode_operations fat_file_inode_operations = { 611 .setattr = fat_setattr, 612 .getattr = fat_getattr, 613 .fileattr_get = fat_fileattr_get, 614 .update_time = fat_update_time, 615 }; 616