| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. |
| 4 |
*/ |
| 5 |
|
| 6 |
#include <linux/slab.h> |
| 7 |
#include <linux/compat.h> |
| 8 |
#include <linux/cred.h> |
| 9 |
#include <linux/buffer_head.h> |
| 10 |
#include <linux/blkdev.h> |
| 11 |
#include <linux/fsnotify.h> |
| 12 |
#include <linux/security.h> |
| 13 |
#include <linux/msdos_fs.h> |
| 14 |
#include <linux/writeback.h> |
| 15 |
#include <linux/filelock.h> |
| 16 |
|
| 17 |
#include "exfat_raw.h" |
| 18 |
#include "exfat_fs.h" |
| 19 |
|
| 20 |
static int exfat_cont_expand(struct inode *inode, loff_t size) |
| 21 |
{ |
| 22 |
int ret; |
| 23 |
unsigned int num_clusters, new_num_clusters, last_clu; |
| 24 |
struct exfat_inode_info *ei = EXFAT_I(inode); |
| 25 |
struct super_block *sb = inode->i_sb; |
| 26 |
struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 27 |
struct exfat_chain clu; |
| 28 |
|
| 29 |
truncate_pagecache(inode, i_size_read(inode)); |
| 30 |
|
| 31 |
ret = inode_newsize_ok(inode, size); |
| 32 |
if (ret) |
| 33 |
return ret; |
| 34 |
|
| 35 |
num_clusters = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi); |
| 36 |
new_num_clusters = EXFAT_B_TO_CLU_ROUND_UP(size, sbi); |
| 37 |
|
| 38 |
if (new_num_clusters == num_clusters) |
| 39 |
goto out; |
| 40 |
|
| 41 |
if (num_clusters) { |
| 42 |
exfat_chain_set(&clu, ei->start_clu, num_clusters, ei->flags); |
| 43 |
ret = exfat_find_last_cluster(sb, &clu, &last_clu); |
| 44 |
if (ret) |
| 45 |
return ret; |
| 46 |
|
| 47 |
clu.dir = last_clu + 1; |
| 48 |
} else { |
| 49 |
last_clu = EXFAT_EOF_CLUSTER; |
| 50 |
clu.dir = EXFAT_EOF_CLUSTER; |
| 51 |
} |
| 52 |
|
| 53 |
clu.size = 0; |
| 54 |
clu.flags = ei->flags; |
| 55 |
|
| 56 |
ret = exfat_alloc_cluster(inode, new_num_clusters - num_clusters, |
| 57 |
&clu, inode_needs_sync(inode)); |
| 58 |
if (ret) |
| 59 |
return ret; |
| 60 |
|
| 61 |
/* Append new clusters to chain */ |
| 62 |
if (num_clusters) { |
| 63 |
if (clu.flags != ei->flags) |
| 64 |
if (exfat_chain_cont_cluster(sb, ei->start_clu, num_clusters)) |
| 65 |
goto free_clu; |
| 66 |
|
| 67 |
if (clu.flags == ALLOC_FAT_CHAIN) |
| 68 |
if (exfat_ent_set(sb, last_clu, clu.dir)) |
| 69 |
goto free_clu; |
| 70 |
} else |
| 71 |
ei->start_clu = clu.dir; |
| 72 |
|
| 73 |
ei->flags = clu.flags; |
| 74 |
|
| 75 |
out: |
| 76 |
inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); |
| 77 |
/* Expanded range not zeroed, do not update valid_size */ |
| 78 |
i_size_write(inode, size); |
| 79 |
|
| 80 |
inode->i_blocks = round_up(size, sbi->cluster_size) >> 9; |
| 81 |
mark_inode_dirty(inode); |
| 82 |
|
| 83 |
if (IS_SYNC(inode)) |
| 84 |
return write_inode_now(inode, 1); |
| 85 |
|
| 86 |
return 0; |
| 87 |
|
| 88 |
free_clu: |
| 89 |
exfat_free_cluster(inode, &clu); |
| 90 |
return -EIO; |
| 91 |
} |
| 92 |
|
| 93 |
static bool exfat_allow_set_time(struct mnt_idmap *idmap, |
| 94 |
struct exfat_sb_info *sbi, struct inode *inode) |
| 95 |
{ |
| 96 |
mode_t allow_utime = sbi->options.allow_utime; |
| 97 |
|
| 98 |
if (!vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), |
| 99 |
current_fsuid())) { |
| 100 |
if (vfsgid_in_group_p(i_gid_into_vfsgid(idmap, inode))) |
| 101 |
allow_utime >>= 3; |
| 102 |
if (allow_utime & MAY_WRITE) |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
/* use a default check */ |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
static int exfat_sanitize_mode(const struct exfat_sb_info *sbi, |
| 111 |
struct inode *inode, umode_t *mode_ptr) |
| 112 |
{ |
| 113 |
mode_t i_mode, mask, perm; |
| 114 |
|
| 115 |
i_mode = inode->i_mode; |
| 116 |
|
| 117 |
mask = (S_ISREG(i_mode) || S_ISLNK(i_mode)) ? |
| 118 |
sbi->options.fs_fmask : sbi->options.fs_dmask; |
| 119 |
perm = *mode_ptr & ~(S_IFMT | mask); |
| 120 |
|
| 121 |
/* Of the r and x bits, all (subject to umask) must be present.*/ |
| 122 |
if ((perm & 0555) != (i_mode & 0555)) |
| 123 |
return -EPERM; |
| 124 |
|
| 125 |
if (exfat_mode_can_hold_ro(inode)) { |
| 126 |
/* |
| 127 |
* Of the w bits, either all (subject to umask) or none must |
| 128 |
* be present. |
| 129 |
*/ |
| 130 |
if ((perm & 0222) && ((perm & 0222) != (0222 & ~mask))) |
| 131 |
return -EPERM; |
| 132 |
} else { |
| 133 |
/* |
| 134 |
* If exfat_mode_can_hold_ro(inode) is false, can't change |
| 135 |
* w bits. |
| 136 |
*/ |
| 137 |
if ((perm & 0222) != (0222 & ~mask)) |
| 138 |
return -EPERM; |
| 139 |
} |
| 140 |
|
| 141 |
*mode_ptr &= S_IFMT | perm; |
| 142 |
|
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
/* resize the file length */ |
| 147 |
int __exfat_truncate(struct inode *inode) |
| 148 |
{ |
| 149 |
unsigned int num_clusters_new, num_clusters_phys; |
| 150 |
unsigned int last_clu = EXFAT_FREE_CLUSTER; |
| 151 |
struct exfat_chain clu; |
| 152 |
struct super_block *sb = inode->i_sb; |
| 153 |
struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 154 |
struct exfat_inode_info *ei = EXFAT_I(inode); |
| 155 |
|
| 156 |
/* check if the given file ID is opened */ |
| 157 |
if (ei->type != TYPE_FILE && ei->type != TYPE_DIR) |
| 158 |
return -EPERM; |
| 159 |
|
| 160 |
exfat_set_volume_dirty(sb); |
| 161 |
|
| 162 |
num_clusters_new = EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi); |
| 163 |
num_clusters_phys = EXFAT_B_TO_CLU(exfat_ondisk_size(inode), sbi); |
| 164 |
|
| 165 |
exfat_chain_set(&clu, ei->start_clu, num_clusters_phys, ei->flags); |
| 166 |
|
| 167 |
if (i_size_read(inode) > 0) { |
| 168 |
/* |
| 169 |
* Truncate FAT chain num_clusters after the first cluster |
| 170 |
* num_clusters = min(new, phys); |
| 171 |
*/ |
| 172 |
unsigned int num_clusters = |
| 173 |
min(num_clusters_new, num_clusters_phys); |
| 174 |
|
| 175 |
/* |
| 176 |
* Follow FAT chain |
| 177 |
* (defensive coding - works fine even with corrupted FAT table |
| 178 |
*/ |
| 179 |
if (clu.flags == ALLOC_NO_FAT_CHAIN) { |
| 180 |
clu.dir += num_clusters; |
| 181 |
clu.size -= num_clusters; |
| 182 |
} else { |
| 183 |
while (num_clusters > 0) { |
| 184 |
last_clu = clu.dir; |
| 185 |
if (exfat_get_next_cluster(sb, &(clu.dir))) |
| 186 |
return -EIO; |
| 187 |
|
| 188 |
num_clusters--; |
| 189 |
clu.size--; |
| 190 |
} |
| 191 |
} |
| 192 |
} else { |
| 193 |
ei->flags = ALLOC_NO_FAT_CHAIN; |
| 194 |
ei->start_clu = EXFAT_EOF_CLUSTER; |
| 195 |
} |
| 196 |
|
| 197 |
if (i_size_read(inode) < ei->valid_size) |
| 198 |
ei->valid_size = i_size_read(inode); |
| 199 |
|
| 200 |
if (ei->type == TYPE_FILE) |
| 201 |
ei->attr |= EXFAT_ATTR_ARCHIVE; |
| 202 |
|
| 203 |
/* |
| 204 |
* update the directory entry |
| 205 |
* |
| 206 |
* If the directory entry is updated by mark_inode_dirty(), the |
| 207 |
* directory entry will be written after a writeback cycle of |
| 208 |
* updating the bitmap/FAT, which may result in clusters being |
| 209 |
* freed but referenced by the directory entry in the event of a |
| 210 |
* sudden power failure. |
| 211 |
* __exfat_write_inode() is called for directory entry, bitmap |
| 212 |
* and FAT to be written in a same writeback. |
| 213 |
*/ |
| 214 |
if (__exfat_write_inode(inode, inode_needs_sync(inode))) |
| 215 |
return -EIO; |
| 216 |
|
| 217 |
/* cut off from the FAT chain */ |
| 218 |
if (ei->flags == ALLOC_FAT_CHAIN && last_clu != EXFAT_FREE_CLUSTER && |
| 219 |
last_clu != EXFAT_EOF_CLUSTER) { |
| 220 |
if (exfat_ent_set(sb, last_clu, EXFAT_EOF_CLUSTER)) |
| 221 |
return -EIO; |
| 222 |
} |
| 223 |
|
| 224 |
/* invalidate cache and free the clusters */ |
| 225 |
/* clear exfat cache */ |
| 226 |
exfat_cache_inval_inode(inode); |
| 227 |
|
| 228 |
/* hint information */ |
| 229 |
ei->hint_bmap.off = EXFAT_EOF_CLUSTER; |
| 230 |
ei->hint_bmap.clu = EXFAT_EOF_CLUSTER; |
| 231 |
|
| 232 |
/* hint_stat will be used if this is directory. */ |
| 233 |
ei->hint_stat.eidx = 0; |
| 234 |
ei->hint_stat.clu = ei->start_clu; |
| 235 |
ei->hint_femp.eidx = EXFAT_HINT_NONE; |
| 236 |
|
| 237 |
/* free the clusters */ |
| 238 |
if (exfat_free_cluster(inode, &clu)) |
| 239 |
return -EIO; |
| 240 |
|
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
void exfat_truncate(struct inode *inode) |
| 245 |
{ |
| 246 |
struct super_block *sb = inode->i_sb; |
| 247 |
struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 248 |
struct exfat_inode_info *ei = EXFAT_I(inode); |
| 249 |
int err; |
| 250 |
|
| 251 |
mutex_lock(&sbi->s_lock); |
| 252 |
if (ei->start_clu == 0) { |
| 253 |
/* |
| 254 |
* Empty start_clu != ~0 (not allocated) |
| 255 |
*/ |
| 256 |
exfat_fs_error(sb, "tried to truncate zeroed cluster."); |
| 257 |
goto write_size; |
| 258 |
} |
| 259 |
|
| 260 |
err = __exfat_truncate(inode); |
| 261 |
if (err) |
| 262 |
goto write_size; |
| 263 |
|
| 264 |
inode->i_blocks = round_up(i_size_read(inode), sbi->cluster_size) >> 9; |
| 265 |
write_size: |
| 266 |
mutex_unlock(&sbi->s_lock); |
| 267 |
} |
| 268 |
|
| 269 |
int exfat_getattr(struct mnt_idmap *idmap, const struct path *path, |
| 270 |
struct kstat *stat, unsigned int request_mask, |
| 271 |
unsigned int query_flags) |
| 272 |
{ |
| 273 |
struct inode *inode = d_backing_inode(path->dentry); |
| 274 |
struct exfat_inode_info *ei = EXFAT_I(inode); |
| 275 |
|
| 276 |
generic_fillattr(idmap, request_mask, inode, stat); |
| 277 |
exfat_truncate_atime(&stat->atime); |
| 278 |
stat->result_mask |= STATX_BTIME; |
| 279 |
stat->btime.tv_sec = ei->i_crtime.tv_sec; |
| 280 |
stat->btime.tv_nsec = ei->i_crtime.tv_nsec; |
| 281 |
stat->blksize = EXFAT_SB(inode->i_sb)->cluster_size; |
| 282 |
return 0; |
| 283 |
} |
| 284 |
|
| 285 |
int exfat_setattr(struct mnt_idmap *idmap, struct dentry *dentry, |
| 286 |
struct iattr *attr) |
| 287 |
{ |
| 288 |
struct exfat_sb_info *sbi = EXFAT_SB(dentry->d_sb); |
| 289 |
struct inode *inode = dentry->d_inode; |
| 290 |
unsigned int ia_valid; |
| 291 |
int error; |
| 292 |
|
| 293 |
if (unlikely(exfat_forced_shutdown(inode->i_sb))) |
| 294 |
return -EIO; |
| 295 |
|
| 296 |
if ((attr->ia_valid & ATTR_SIZE) && |
| 297 |
attr->ia_size > i_size_read(inode)) { |
| 298 |
error = exfat_cont_expand(inode, attr->ia_size); |
| 299 |
if (error || attr->ia_valid == ATTR_SIZE) |
| 300 |
return error; |
| 301 |
attr->ia_valid &= ~ATTR_SIZE; |
| 302 |
} |
| 303 |
|
| 304 |
/* Check for setting the inode time. */ |
| 305 |
ia_valid = attr->ia_valid; |
| 306 |
if ((ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) && |
| 307 |
exfat_allow_set_time(idmap, sbi, inode)) { |
| 308 |
attr->ia_valid &= ~(ATTR_MTIME_SET | ATTR_ATIME_SET | |
| 309 |
ATTR_TIMES_SET); |
| 310 |
} |
| 311 |
|
| 312 |
error = setattr_prepare(idmap, dentry, attr); |
| 313 |
attr->ia_valid = ia_valid; |
| 314 |
if (error) |
| 315 |
goto out; |
| 316 |
|
| 317 |
if (((attr->ia_valid & ATTR_UID) && |
| 318 |
(!uid_eq(from_vfsuid(idmap, i_user_ns(inode), attr->ia_vfsuid), |
| 319 |
sbi->options.fs_uid))) || |
| 320 |
((attr->ia_valid & ATTR_GID) && |
| 321 |
(!gid_eq(from_vfsgid(idmap, i_user_ns(inode), attr->ia_vfsgid), |
| 322 |
sbi->options.fs_gid))) || |
| 323 |
((attr->ia_valid & ATTR_MODE) && |
| 324 |
(attr->ia_mode & ~(S_IFREG | S_IFLNK | S_IFDIR | 0777)))) { |
| 325 |
error = -EPERM; |
| 326 |
goto out; |
| 327 |
} |
| 328 |
|
| 329 |
/* |
| 330 |
* We don't return -EPERM here. Yes, strange, but this is too |
| 331 |
* old behavior. |
| 332 |
*/ |
| 333 |
if (attr->ia_valid & ATTR_MODE) { |
| 334 |
if (exfat_sanitize_mode(sbi, inode, &attr->ia_mode) < 0) |
| 335 |
attr->ia_valid &= ~ATTR_MODE; |
| 336 |
} |
| 337 |
|
| 338 |
if (attr->ia_valid & ATTR_SIZE) |
| 339 |
inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); |
| 340 |
|
| 341 |
setattr_copy(idmap, inode, attr); |
| 342 |
exfat_truncate_inode_atime(inode); |
| 343 |
|
| 344 |
if (attr->ia_valid & ATTR_SIZE) { |
| 345 |
error = exfat_block_truncate_page(inode, attr->ia_size); |
| 346 |
if (error) |
| 347 |
goto out; |
| 348 |
|
| 349 |
down_write(&EXFAT_I(inode)->truncate_lock); |
| 350 |
truncate_setsize(inode, attr->ia_size); |
| 351 |
|
| 352 |
/* |
| 353 |
* __exfat_write_inode() is called from exfat_truncate(), inode |
| 354 |
* is already written by it, so mark_inode_dirty() is unneeded. |
| 355 |
*/ |
| 356 |
exfat_truncate(inode); |
| 357 |
up_write(&EXFAT_I(inode)->truncate_lock); |
| 358 |
} else |
| 359 |
mark_inode_dirty(inode); |
| 360 |
|
| 361 |
out: |
| 362 |
return error; |
| 363 |
} |
| 364 |
|
| 365 |
/* |
| 366 |
* modified ioctls from fat/file.c by Welmer Almesberger |
| 367 |
*/ |
| 368 |
static int exfat_ioctl_get_attributes(struct inode *inode, u32 __user *user_attr) |
| 369 |
{ |
| 370 |
u32 attr; |
| 371 |
|
| 372 |
inode_lock_shared(inode); |
| 373 |
attr = exfat_make_attr(inode); |
| 374 |
inode_unlock_shared(inode); |
| 375 |
|
| 376 |
return put_user(attr, user_attr); |
| 377 |
} |
| 378 |
|
| 379 |
static int exfat_ioctl_set_attributes(struct file *file, u32 __user *user_attr) |
| 380 |
{ |
| 381 |
struct inode *inode = file_inode(file); |
| 382 |
struct exfat_sb_info *sbi = EXFAT_SB(inode->i_sb); |
| 383 |
int is_dir = S_ISDIR(inode->i_mode); |
| 384 |
u32 attr, oldattr; |
| 385 |
struct iattr ia; |
| 386 |
int err; |
| 387 |
|
| 388 |
err = get_user(attr, user_attr); |
| 389 |
if (err) |
| 390 |
goto out; |
| 391 |
|
| 392 |
err = mnt_want_write_file(file); |
| 393 |
if (err) |
| 394 |
goto out; |
| 395 |
inode_lock(inode); |
| 396 |
|
| 397 |
oldattr = exfat_make_attr(inode); |
| 398 |
|
| 399 |
/* |
| 400 |
* Mask attributes so we don't set reserved fields. |
| 401 |
*/ |
| 402 |
attr &= (EXFAT_ATTR_READONLY | EXFAT_ATTR_HIDDEN | EXFAT_ATTR_SYSTEM | |
| 403 |
EXFAT_ATTR_ARCHIVE); |
| 404 |
attr |= (is_dir ? EXFAT_ATTR_SUBDIR : 0); |
| 405 |
|
| 406 |
/* Equivalent to a chmod() */ |
| 407 |
ia.ia_valid = ATTR_MODE | ATTR_CTIME; |
| 408 |
ia.ia_ctime = current_time(inode); |
| 409 |
if (is_dir) |
| 410 |
ia.ia_mode = exfat_make_mode(sbi, attr, 0777); |
| 411 |
else |
| 412 |
ia.ia_mode = exfat_make_mode(sbi, attr, 0666 | (inode->i_mode & 0111)); |
| 413 |
|
| 414 |
/* The root directory has no attributes */ |
| 415 |
if (inode->i_ino == EXFAT_ROOT_INO && attr != EXFAT_ATTR_SUBDIR) { |
| 416 |
err = -EINVAL; |
| 417 |
goto out_unlock_inode; |
| 418 |
} |
| 419 |
|
| 420 |
if (((attr | oldattr) & EXFAT_ATTR_SYSTEM) && |
| 421 |
!capable(CAP_LINUX_IMMUTABLE)) { |
| 422 |
err = -EPERM; |
| 423 |
goto out_unlock_inode; |
| 424 |
} |
| 425 |
|
| 426 |
/* |
| 427 |
* The security check is questionable... We single |
| 428 |
* out the RO attribute for checking by the security |
| 429 |
* module, just because it maps to a file mode. |
| 430 |
*/ |
| 431 |
err = security_inode_setattr(file_mnt_idmap(file), |
| 432 |
file->f_path.dentry, &ia); |
| 433 |
if (err) |
| 434 |
goto out_unlock_inode; |
| 435 |
|
| 436 |
/* This MUST be done before doing anything irreversible... */ |
| 437 |
err = exfat_setattr(file_mnt_idmap(file), file->f_path.dentry, &ia); |
| 438 |
if (err) |
| 439 |
goto out_unlock_inode; |
| 440 |
|
| 441 |
fsnotify_change(file->f_path.dentry, ia.ia_valid); |
| 442 |
|
| 443 |
exfat_save_attr(inode, attr); |
| 444 |
mark_inode_dirty(inode); |
| 445 |
out_unlock_inode: |
| 446 |
inode_unlock(inode); |
| 447 |
mnt_drop_write_file(file); |
| 448 |
out: |
| 449 |
return err; |
| 450 |
} |
| 451 |
|
| 452 |
static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg) |
| 453 |
{ |
| 454 |
struct fstrim_range range; |
| 455 |
int ret = 0; |
| 456 |
|
| 457 |
if (!capable(CAP_SYS_ADMIN)) |
| 458 |
return -EPERM; |
| 459 |
|
| 460 |
if (!bdev_max_discard_sectors(inode->i_sb->s_bdev)) |
| 461 |
return -EOPNOTSUPP; |
| 462 |
|
| 463 |
if (copy_from_user(&range, (struct fstrim_range __user *)arg, sizeof(range))) |
| 464 |
return -EFAULT; |
| 465 |
|
| 466 |
range.minlen = max_t(unsigned int, range.minlen, |
| 467 |
bdev_discard_granularity(inode->i_sb->s_bdev)); |
| 468 |
|
| 469 |
ret = exfat_trim_fs(inode, &range); |
| 470 |
if (ret < 0) |
| 471 |
return ret; |
| 472 |
|
| 473 |
if (copy_to_user((struct fstrim_range __user *)arg, &range, sizeof(range))) |
| 474 |
return -EFAULT; |
| 475 |
|
| 476 |
return 0; |
| 477 |
} |
| 478 |
|
| 479 |
static int exfat_ioctl_shutdown(struct super_block *sb, unsigned long arg) |
| 480 |
{ |
| 481 |
u32 flags; |
| 482 |
|
| 483 |
if (!capable(CAP_SYS_ADMIN)) |
| 484 |
return -EPERM; |
| 485 |
|
| 486 |
if (get_user(flags, (__u32 __user *)arg)) |
| 487 |
return -EFAULT; |
| 488 |
|
| 489 |
return exfat_force_shutdown(sb, flags); |
| 490 |
} |
| 491 |
|
| 492 |
static int exfat_ioctl_get_volume_label(struct super_block *sb, unsigned long arg) |
| 493 |
{ |
| 494 |
int ret; |
| 495 |
char label[FSLABEL_MAX] = {0}; |
| 496 |
struct exfat_uni_name uniname; |
| 497 |
|
| 498 |
ret = exfat_read_volume_label(sb, &uniname); |
| 499 |
if (ret < 0) |
| 500 |
return ret; |
| 501 |
|
| 502 |
ret = exfat_utf16_to_nls(sb, &uniname, label, uniname.name_len); |
| 503 |
if (ret < 0) |
| 504 |
return ret; |
| 505 |
|
| 506 |
if (copy_to_user((char __user *)arg, label, ret + 1)) |
| 507 |
return -EFAULT; |
| 508 |
|
| 509 |
return 0; |
| 510 |
} |
| 511 |
|
| 512 |
static int exfat_ioctl_set_volume_label(struct super_block *sb, |
| 513 |
unsigned long arg) |
| 514 |
{ |
| 515 |
int ret = 0, lossy, label_len; |
| 516 |
char label[FSLABEL_MAX] = {0}; |
| 517 |
struct exfat_uni_name uniname; |
| 518 |
|
| 519 |
if (!capable(CAP_SYS_ADMIN)) |
| 520 |
return -EPERM; |
| 521 |
|
| 522 |
if (copy_from_user(label, (char __user *)arg, FSLABEL_MAX)) |
| 523 |
return -EFAULT; |
| 524 |
|
| 525 |
memset(&uniname, 0, sizeof(uniname)); |
| 526 |
label_len = strnlen(label, FSLABEL_MAX - 1); |
| 527 |
if (label[0]) { |
| 528 |
ret = exfat_nls_to_utf16(sb, label, label_len, |
| 529 |
&uniname, &lossy); |
| 530 |
if (ret < 0) |
| 531 |
return ret; |
| 532 |
else if (lossy & NLS_NAME_LOSSY) |
| 533 |
return -EINVAL; |
| 534 |
} |
| 535 |
|
| 536 |
uniname.name_len = ret; |
| 537 |
|
| 538 |
return exfat_write_volume_label(sb, &uniname); |
| 539 |
} |
| 540 |
|
| 541 |
long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 542 |
{ |
| 543 |
struct inode *inode = file_inode(filp); |
| 544 |
u32 __user *user_attr = (u32 __user *)arg; |
| 545 |
|
| 546 |
switch (cmd) { |
| 547 |
case FAT_IOCTL_GET_ATTRIBUTES: |
| 548 |
return exfat_ioctl_get_attributes(inode, user_attr); |
| 549 |
case FAT_IOCTL_SET_ATTRIBUTES: |
| 550 |
return exfat_ioctl_set_attributes(filp, user_attr); |
| 551 |
case EXFAT_IOC_SHUTDOWN: |
| 552 |
return exfat_ioctl_shutdown(inode->i_sb, arg); |
| 553 |
case FITRIM: |
| 554 |
return exfat_ioctl_fitrim(inode, arg); |
| 555 |
case FS_IOC_GETFSLABEL: |
| 556 |
return exfat_ioctl_get_volume_label(inode->i_sb, arg); |
| 557 |
case FS_IOC_SETFSLABEL: |
| 558 |
return exfat_ioctl_set_volume_label(inode->i_sb, arg); |
| 559 |
default: |
| 560 |
return -ENOTTY; |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
#ifdef CONFIG_COMPAT |
| 565 |
long exfat_compat_ioctl(struct file *filp, unsigned int cmd, |
| 566 |
unsigned long arg) |
| 567 |
{ |
| 568 |
return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); |
| 569 |
} |
| 570 |
#endif |
| 571 |
|
| 572 |
int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync) |
| 573 |
{ |
| 574 |
struct inode *inode = filp->f_mapping->host; |
| 575 |
int err; |
| 576 |
|
| 577 |
if (unlikely(exfat_forced_shutdown(inode->i_sb))) |
| 578 |
return -EIO; |
| 579 |
|
| 580 |
err = __generic_file_fsync(filp, start, end, datasync); |
| 581 |
if (err) |
| 582 |
return err; |
| 583 |
|
| 584 |
err = sync_blockdev(inode->i_sb->s_bdev); |
| 585 |
if (err) |
| 586 |
return err; |
| 587 |
|
| 588 |
return blkdev_issue_flush(inode->i_sb->s_bdev); |
| 589 |
} |
| 590 |
|
| 591 |
static int exfat_extend_valid_size(struct inode *inode, loff_t new_valid_size) |
| 592 |
{ |
| 593 |
int err; |
| 594 |
loff_t pos; |
| 595 |
struct exfat_inode_info *ei = EXFAT_I(inode); |
| 596 |
struct address_space *mapping = inode->i_mapping; |
| 597 |
const struct address_space_operations *ops = mapping->a_ops; |
| 598 |
|
| 599 |
pos = ei->valid_size; |
| 600 |
while (pos < new_valid_size) { |
| 601 |
u32 len; |
| 602 |
struct folio *folio; |
| 603 |
unsigned long off; |
| 604 |
|
| 605 |
len = PAGE_SIZE - (pos & (PAGE_SIZE - 1)); |
| 606 |
if (pos + len > new_valid_size) |
| 607 |
len = new_valid_size - pos; |
| 608 |
|
| 609 |
err = ops->write_begin(NULL, mapping, pos, len, &folio, NULL); |
| 610 |
if (err) |
| 611 |
goto out; |
| 612 |
|
| 613 |
off = offset_in_folio(folio, pos); |
| 614 |
folio_zero_new_buffers(folio, off, off + len); |
| 615 |
|
| 616 |
err = ops->write_end(NULL, mapping, pos, len, len, folio, NULL); |
| 617 |
if (err < 0) |
| 618 |
goto out; |
| 619 |
pos += len; |
| 620 |
|
| 621 |
balance_dirty_pages_ratelimited(mapping); |
| 622 |
cond_resched(); |
| 623 |
} |
| 624 |
|
| 625 |
return 0; |
| 626 |
|
| 627 |
out: |
| 628 |
return err; |
| 629 |
} |
| 630 |
|
| 631 |
static ssize_t exfat_file_write_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 632 |
{ |
| 633 |
ssize_t ret; |
| 634 |
struct file *file = iocb->ki_filp; |
| 635 |
struct inode *inode = file_inode(file); |
| 636 |
struct exfat_inode_info *ei = EXFAT_I(inode); |
| 637 |
loff_t pos = iocb->ki_pos; |
| 638 |
loff_t valid_size; |
| 639 |
|
| 640 |
if (unlikely(exfat_forced_shutdown(inode->i_sb))) |
| 641 |
return -EIO; |
| 642 |
|
| 643 |
inode_lock(inode); |
| 644 |
|
| 645 |
if (pos > i_size_read(inode)) |
| 646 |
truncate_pagecache(inode, i_size_read(inode)); |
| 647 |
|
| 648 |
valid_size = ei->valid_size; |
| 649 |
|
| 650 |
ret = generic_write_checks(iocb, iter); |
| 651 |
if (ret <= 0) |
| 652 |
goto unlock; |
| 653 |
|
| 654 |
if (iocb->ki_flags & IOCB_DIRECT) { |
| 655 |
unsigned long align = pos | iov_iter_alignment(iter); |
| 656 |
|
| 657 |
if (!IS_ALIGNED(align, i_blocksize(inode)) && |
| 658 |
!IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev))) { |
| 659 |
ret = -EINVAL; |
| 660 |
goto unlock; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
if (pos > valid_size) { |
| 665 |
ret = exfat_extend_valid_size(inode, pos); |
| 666 |
if (ret < 0 && ret != -ENOSPC) { |
| 667 |
exfat_err(inode->i_sb, |
| 668 |
"write: fail to zero from %llu to %llu(%zd)", |
| 669 |
valid_size, pos, ret); |
| 670 |
} |
| 671 |
if (ret < 0) |
| 672 |
goto unlock; |
| 673 |
} |
| 674 |
|
| 675 |
ret = __generic_file_write_iter(iocb, iter); |
| 676 |
if (ret < 0) |
| 677 |
goto unlock; |
| 678 |
|
| 679 |
inode_unlock(inode); |
| 680 |
|
| 681 |
if (pos > valid_size) |
| 682 |
pos = valid_size; |
| 683 |
|
| 684 |
if (iocb->ki_pos > pos) { |
| 685 |
ssize_t err = generic_write_sync(iocb, iocb->ki_pos - pos); |
| 686 |
|
| 687 |
if (err < 0) |
| 688 |
return err; |
| 689 |
} |
| 690 |
|
| 691 |
return ret; |
| 692 |
|
| 693 |
unlock: |
| 694 |
inode_unlock(inode); |
| 695 |
|
| 696 |
return ret; |
| 697 |
} |
| 698 |
|
| 699 |
static ssize_t exfat_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 700 |
{ |
| 701 |
struct inode *inode = file_inode(iocb->ki_filp); |
| 702 |
|
| 703 |
if (unlikely(exfat_forced_shutdown(inode->i_sb))) |
| 704 |
return -EIO; |
| 705 |
|
| 706 |
return generic_file_read_iter(iocb, iter); |
| 707 |
} |
| 708 |
|
| 709 |
static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf) |
| 710 |
{ |
| 711 |
int err; |
| 712 |
struct inode *inode = file_inode(vmf->vma->vm_file); |
| 713 |
struct exfat_inode_info *ei = EXFAT_I(inode); |
| 714 |
loff_t new_valid_size; |
| 715 |
|
| 716 |
if (!inode_trylock(inode)) |
| 717 |
return VM_FAULT_RETRY; |
| 718 |
|
| 719 |
new_valid_size = ((loff_t)vmf->pgoff + 1) << PAGE_SHIFT; |
| 720 |
new_valid_size = min(new_valid_size, i_size_read(inode)); |
| 721 |
|
| 722 |
if (ei->valid_size < new_valid_size) { |
| 723 |
err = exfat_extend_valid_size(inode, new_valid_size); |
| 724 |
if (err < 0) { |
| 725 |
inode_unlock(inode); |
| 726 |
return vmf_fs_error(err); |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
inode_unlock(inode); |
| 731 |
|
| 732 |
return filemap_page_mkwrite(vmf); |
| 733 |
} |
| 734 |
|
| 735 |
static const struct vm_operations_struct exfat_file_vm_ops = { |
| 736 |
.fault = filemap_fault, |
| 737 |
.map_pages = filemap_map_pages, |
| 738 |
.page_mkwrite = exfat_page_mkwrite, |
| 739 |
}; |
| 740 |
|
| 741 |
static int exfat_file_mmap_prepare(struct vm_area_desc *desc) |
| 742 |
{ |
| 743 |
struct file *file = desc->file; |
| 744 |
|
| 745 |
if (unlikely(exfat_forced_shutdown(file_inode(desc->file)->i_sb))) |
| 746 |
return -EIO; |
| 747 |
|
| 748 |
file_accessed(file); |
| 749 |
desc->vm_ops = &exfat_file_vm_ops; |
| 750 |
return 0; |
| 751 |
} |
| 752 |
|
| 753 |
static ssize_t exfat_splice_read(struct file *in, loff_t *ppos, |
| 754 |
struct pipe_inode_info *pipe, size_t len, unsigned int flags) |
| 755 |
{ |
| 756 |
if (unlikely(exfat_forced_shutdown(file_inode(in)->i_sb))) |
| 757 |
return -EIO; |
| 758 |
|
| 759 |
return filemap_splice_read(in, ppos, pipe, len, flags); |
| 760 |
} |
| 761 |
|
| 762 |
const struct file_operations exfat_file_operations = { |
| 763 |
.llseek = generic_file_llseek, |
| 764 |
.read_iter = exfat_file_read_iter, |
| 765 |
.write_iter = exfat_file_write_iter, |
| 766 |
.unlocked_ioctl = exfat_ioctl, |
| 767 |
#ifdef CONFIG_COMPAT |
| 768 |
.compat_ioctl = exfat_compat_ioctl, |
| 769 |
#endif |
| 770 |
.mmap_prepare = exfat_file_mmap_prepare, |
| 771 |
.fsync = exfat_file_fsync, |
| 772 |
.splice_read = exfat_splice_read, |
| 773 |
.splice_write = iter_file_splice_write, |
| 774 |
.setlease = generic_setlease, |
| 775 |
}; |
| 776 |
|
| 777 |
const struct inode_operations exfat_file_inode_operations = { |
| 778 |
.setattr = exfat_setattr, |
| 779 |
.getattr = exfat_getattr, |
| 780 |
}; |