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 "fat.h" 21 22 static long fat_fallocate(struct file *file, int mode, 23 loff_t offset, loff_t len); 24 25 static int fat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr) 26 { 27 u32 attr; 28 29 inode_lock_shared(inode); 30 attr = fat_make_attrs(inode); 31 inode_unlock_shared(inode); 32 33 return put_user(attr, user_attr); 34 } 35 36 static int fat_ioctl_set_attributes(struct file *file, u32 __user *user_attr) 37 { 38 struct inode *inode = file_inode(file); 39 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 40 int is_dir = S_ISDIR(inode->i_mode); 41 u32 attr, oldattr; 42 struct iattr ia; 43 int err; 44 45 err = get_user(attr, user_attr); 46 if (err) 47 goto out; 48 49 err = mnt_want_write_file(file); 50 if (err) 51 goto out; 52 inode_lock(inode); 53 54 /* 55 * ATTR_VOLUME and ATTR_DIR cannot be changed; this also 56 * prevents the user from turning us into a VFAT 57 * longname entry. Also, we obviously can't set 58 * any of the NTFS attributes in the high 24 bits. 59 */ 60 attr &= 0xff & ~(ATTR_VOLUME | ATTR_DIR); 61 /* Merge in ATTR_VOLUME and ATTR_DIR */ 62 attr |= (MSDOS_I(inode)->i_attrs & ATTR_VOLUME) | 63 (is_dir ? ATTR_DIR : 0); 64 oldattr = fat_make_attrs(inode); 65 66 /* Equivalent to a chmod() */ 67 ia.ia_valid = ATTR_MODE | ATTR_CTIME; 68 ia.ia_ctime = current_time(inode); 69 if (is_dir) 70 ia.ia_mode = fat_make_mode(sbi, attr, S_IRWXUGO); 71 else { 72 ia.ia_mode = fat_make_mode(sbi, attr, 73 S_IRUGO | S_IWUGO | (inode->i_mode & S_IXUGO)); 74 } 75 76 /* The root directory has no attributes */ 77 if (inode->i_ino == MSDOS_ROOT_INO && attr != ATTR_DIR) { 78 err = -EINVAL; 79 goto out_unlock_inode; 80 } 81 82 if (sbi->options.sys_immutable && 83 ((attr | oldattr) & ATTR_SYS) && 84 !capable(CAP_LINUX_IMMUTABLE)) { 85 err = -EPERM; 86 goto out_unlock_inode; 87 } 88 89 /* 90 * The security check is questionable... We single 91 * out the RO attribute for checking by the security 92 * module, just because it maps to a file mode. 93 */ 94 err = security_inode_setattr(file_mnt_idmap(file), 95 file->f_path.dentry, &ia); 96 if (err) 97 goto out_unlock_inode; 98 99 /* This MUST be done before doing anything irreversible... */ 100 err = fat_setattr(file_mnt_idmap(file), file->f_path.dentry, &ia); 101 if (err) 102 goto out_unlock_inode; 103 104 fsnotify_change(file->f_path.dentry, ia.ia_valid); 105 if (sbi->options.sys_immutable) { 106 if (attr & ATTR_SYS) 107 inode->i_flags |= S_IMMUTABLE; 108 else 109 inode->i_flags &= ~S_IMMUTABLE; 110 } 111 112 fat_save_attrs(inode, attr); 113 mark_inode_dirty(inode); 114 out_unlock_inode: 115 inode_unlock(inode); 116 mnt_drop_write_file(file); 117 out: 118 return err; 119 } 120 121 static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr) 122 { 123 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 124 return put_user(sbi->vol_id, user_attr); 125 } 126 127 static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg) 128 { 129 struct super_block *sb = inode->i_sb; 130 struct fstrim_range __user *user_range; 131 struct fstrim_range range; 132 int err; 133 134 if (!capable(CAP_SYS_ADMIN)) 135 return -EPERM; 136 137 if (!bdev_max_discard_sectors(sb->s_bdev)) 138 return -EOPNOTSUPP; 139 140 user_range = (struct fstrim_range __user *)arg; 141 if (copy_from_user(&range, user_range, sizeof(range))) 142 return -EFAULT; 143 144 range.minlen = max(range.minlen, bdev_discard_granularity(sb->s_bdev)); 145 146 err = fat_trim_fs(inode, &range); 147 if (err < 0) 148 return err; 149 150 if (copy_to_user(user_range, &range, sizeof(range))) 151 return -EFAULT; 152 153 return 0; 154 } 155 156 long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 157 { 158 struct inode *inode = file_inode(filp); 159 u32 __user *user_attr = (u32 __user *)arg; 160 161 switch (cmd) { 162 case FAT_IOCTL_GET_ATTRIBUTES: 163 return fat_ioctl_get_attributes(inode, user_attr); 164 case FAT_IOCTL_SET_ATTRIBUTES: 165 return fat_ioctl_set_attributes(filp, user_attr); 166 case FAT_IOCTL_GET_VOLUME_ID: 167 return fat_ioctl_get_volume_id(inode, user_attr); 168 case FITRIM: 169 return fat_ioctl_fitrim(inode, arg); 170 default: 171 return -ENOTTY; /* Inappropriate ioctl for device */ 172 } 173 } 174 175 static int fat_file_release(struct inode *inode, struct file *filp) 176 { 177 if ((filp->f_mode & FMODE_WRITE) && 178 MSDOS_SB(inode->i_sb)->options.flush) { 179 fat_flush_inodes(inode->i_sb, inode, NULL); 180 set_current_state(TASK_UNINTERRUPTIBLE); 181 io_schedule_timeout(HZ/10); 182 } 183 return 0; 184 } 185 186 int fat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) 187 { 188 struct inode *inode = filp->f_mapping->host; 189 struct inode *fat_inode = MSDOS_SB(inode->i_sb)->fat_inode; 190 int err; 191 192 err = mmb_fsync_noflush(filp, &MSDOS_I(inode)->i_metadata_bhs, 193 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 if (wait) { 335 err = fat_sync_inode(inode); 336 if (err) { 337 MSDOS_I(inode)->i_start = i_start; 338 MSDOS_I(inode)->i_logstart = i_logstart; 339 return err; 340 } 341 } else 342 mark_inode_dirty(inode); 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_getattr(struct mnt_idmap *idmap, const struct path *path, 402 struct kstat *stat, u32 request_mask, unsigned int flags) 403 { 404 struct inode *inode = d_inode(path->dentry); 405 struct msdos_sb_info *sbi = MSDOS_SB(inode->i_sb); 406 407 generic_fillattr(idmap, request_mask, inode, stat); 408 stat->blksize = sbi->cluster_size; 409 410 if (sbi->options.nfs == FAT_NFS_NOSTALE_RO) { 411 /* Use i_pos for ino. This is used as fileid of nfs. */ 412 stat->ino = fat_i_pos_read(sbi, inode); 413 } 414 415 if (sbi->options.isvfat && request_mask & STATX_BTIME) { 416 stat->result_mask |= STATX_BTIME; 417 stat->btime = MSDOS_I(inode)->i_crtime; 418 } 419 420 return 0; 421 } 422 EXPORT_SYMBOL_GPL(fat_getattr); 423 424 static int fat_sanitize_mode(const struct msdos_sb_info *sbi, 425 struct inode *inode, umode_t *mode_ptr) 426 { 427 umode_t mask, perm; 428 429 /* 430 * Note, the basic check is already done by a caller of 431 * (attr->ia_mode & ~FAT_VALID_MODE) 432 */ 433 434 if (S_ISREG(inode->i_mode)) 435 mask = sbi->options.fs_fmask; 436 else 437 mask = sbi->options.fs_dmask; 438 439 perm = *mode_ptr & ~(S_IFMT | mask); 440 441 /* 442 * Of the r and x bits, all (subject to umask) must be present. Of the 443 * w bits, either all (subject to umask) or none must be present. 444 * 445 * If fat_mode_can_hold_ro(inode) is false, can't change w bits. 446 */ 447 if ((perm & (S_IRUGO | S_IXUGO)) != (inode->i_mode & (S_IRUGO|S_IXUGO))) 448 return -EPERM; 449 if (fat_mode_can_hold_ro(inode)) { 450 if ((perm & S_IWUGO) && ((perm & S_IWUGO) != (S_IWUGO & ~mask))) 451 return -EPERM; 452 } else { 453 if ((perm & S_IWUGO) != (S_IWUGO & ~mask)) 454 return -EPERM; 455 } 456 457 *mode_ptr &= S_IFMT | perm; 458 459 return 0; 460 } 461 462 static int fat_allow_set_time(struct mnt_idmap *idmap, 463 struct msdos_sb_info *sbi, struct inode *inode) 464 { 465 umode_t allow_utime = sbi->options.allow_utime; 466 467 if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), 468 current_fsuid())) { 469 if (vfsgid_in_group_p(i_gid_into_vfsgid(idmap, inode))) 470 allow_utime >>= 3; 471 if (allow_utime & MAY_WRITE) 472 return 1; 473 } 474 475 /* use a default check */ 476 return 0; 477 } 478 479 #define TIMES_SET_FLAGS (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET) 480 /* valid file mode bits */ 481 #define FAT_VALID_MODE (S_IFREG | S_IFDIR | S_IRWXUGO) 482 483 int fat_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 484 struct iattr *attr) 485 { 486 struct msdos_sb_info *sbi = MSDOS_SB(dentry->d_sb); 487 struct inode *inode = d_inode(dentry); 488 unsigned int ia_valid; 489 int error; 490 491 /* Check for setting the inode time. */ 492 ia_valid = attr->ia_valid; 493 if (ia_valid & TIMES_SET_FLAGS) { 494 if (fat_allow_set_time(idmap, sbi, inode)) 495 attr->ia_valid &= ~TIMES_SET_FLAGS; 496 } 497 498 error = setattr_prepare(idmap, dentry, attr); 499 attr->ia_valid = ia_valid; 500 if (error) { 501 if (sbi->options.quiet) 502 error = 0; 503 goto out; 504 } 505 506 /* 507 * Expand the file. Since inode_setattr() updates ->i_size 508 * before calling the ->truncate(), but FAT needs to fill the 509 * hole before it. XXX: this is no longer true with new truncate 510 * sequence. 511 */ 512 if (attr->ia_valid & ATTR_SIZE) { 513 inode_dio_wait(inode); 514 515 if (attr->ia_size > inode->i_size) { 516 error = fat_cont_expand(inode, attr->ia_size); 517 if (error || attr->ia_valid == ATTR_SIZE) 518 goto out; 519 attr->ia_valid &= ~ATTR_SIZE; 520 } 521 } 522 523 if (((attr->ia_valid & ATTR_UID) && 524 (!uid_eq(from_vfsuid(idmap, i_user_ns(inode), attr->ia_vfsuid), 525 sbi->options.fs_uid))) || 526 ((attr->ia_valid & ATTR_GID) && 527 (!gid_eq(from_vfsgid(idmap, i_user_ns(inode), attr->ia_vfsgid), 528 sbi->options.fs_gid))) || 529 ((attr->ia_valid & ATTR_MODE) && 530 (attr->ia_mode & ~FAT_VALID_MODE))) 531 error = -EPERM; 532 533 if (error) { 534 if (sbi->options.quiet) 535 error = 0; 536 goto out; 537 } 538 539 /* 540 * We don't return -EPERM here. Yes, strange, but this is too 541 * old behavior. 542 */ 543 if (attr->ia_valid & ATTR_MODE) { 544 if (fat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0) 545 attr->ia_valid &= ~ATTR_MODE; 546 } 547 548 if (attr->ia_valid & ATTR_SIZE) { 549 error = fat_block_truncate_page(inode, attr->ia_size); 550 if (error) 551 goto out; 552 down_write(&MSDOS_I(inode)->truncate_lock); 553 truncate_setsize(inode, attr->ia_size); 554 fat_truncate_blocks(inode, attr->ia_size); 555 up_write(&MSDOS_I(inode)->truncate_lock); 556 } 557 558 /* 559 * setattr_copy can't truncate these appropriately, so we'll copy them 560 * ourselves. See fat_truncate_time for the c/mtime logic on fat. 561 */ 562 if (attr->ia_valid & ATTR_ATIME) 563 fat_truncate_time(inode, &attr->ia_atime, FAT_UPDATE_ATIME); 564 if (attr->ia_valid & ATTR_MTIME) 565 fat_truncate_time(inode, &attr->ia_mtime, FAT_UPDATE_CMTIME); 566 attr->ia_valid &= ~(ATTR_ATIME|ATTR_CTIME|ATTR_MTIME); 567 568 setattr_copy(idmap, inode, attr); 569 mark_inode_dirty(inode); 570 out: 571 return error; 572 } 573 EXPORT_SYMBOL_GPL(fat_setattr); 574 575 const struct inode_operations fat_file_inode_operations = { 576 .setattr = fat_setattr, 577 .getattr = fat_getattr, 578 .update_time = fat_update_time, 579 }; 580