| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* |
| 4 |
* Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved. |
| 5 |
* |
| 6 |
* Regular file handling primitives for NTFS-based filesystems. |
| 7 |
* |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <linux/backing-dev.h> |
| 11 |
#include <linux/blkdev.h> |
| 12 |
#include <linux/buffer_head.h> |
| 13 |
#include <linux/compat.h> |
| 14 |
#include <linux/falloc.h> |
| 15 |
#include <linux/fiemap.h> |
| 16 |
#include <linux/fileattr.h> |
| 17 |
#include <linux/filelock.h> |
| 18 |
#include <linux/iomap.h> |
| 19 |
|
| 20 |
#include "debug.h" |
| 21 |
#include "ntfs.h" |
| 22 |
#include "ntfs_fs.h" |
| 23 |
|
| 24 |
/* |
| 25 |
* cifx, btrfs, exfat, ext4, f2fs use this constant. |
| 26 |
* Hope this value will become common to all fs. |
| 27 |
*/ |
| 28 |
#define NTFS3_IOC_SHUTDOWN _IOR('X', 125, __u32) |
| 29 |
|
| 30 |
/* |
| 31 |
* Helper for ntfs_should_use_dio. |
| 32 |
*/ |
| 33 |
static u32 ntfs_dio_alignment(struct inode *inode) |
| 34 |
{ |
| 35 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 36 |
|
| 37 |
if (is_resident(ni)) { |
| 38 |
/* Check delalloc. */ |
| 39 |
if (!ni->file.run_da.count) |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
/* In most cases this is bdev_logical_block_size(bdev). */ |
| 44 |
return ni->mi.sbi->bdev_blocksize; |
| 45 |
} |
| 46 |
|
| 47 |
/* |
| 48 |
* Returns %true if the given DIO request should be attempted with DIO, or |
| 49 |
* %false if it should fall back to buffered I/O. |
| 50 |
*/ |
| 51 |
static bool ntfs_should_use_dio(struct kiocb *iocb, struct iov_iter *iter) |
| 52 |
{ |
| 53 |
struct inode *inode = file_inode(iocb->ki_filp); |
| 54 |
u32 dio_align = ntfs_dio_alignment(inode); |
| 55 |
|
| 56 |
if (!dio_align) |
| 57 |
return false; |
| 58 |
|
| 59 |
return IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), dio_align); |
| 60 |
} |
| 61 |
|
| 62 |
static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg) |
| 63 |
{ |
| 64 |
struct fstrim_range __user *user_range; |
| 65 |
struct fstrim_range range; |
| 66 |
struct block_device *dev; |
| 67 |
int err; |
| 68 |
|
| 69 |
if (!capable(CAP_SYS_ADMIN)) |
| 70 |
return -EPERM; |
| 71 |
|
| 72 |
dev = sbi->sb->s_bdev; |
| 73 |
if (!bdev_max_discard_sectors(dev)) |
| 74 |
return -EOPNOTSUPP; |
| 75 |
|
| 76 |
user_range = (struct fstrim_range __user *)arg; |
| 77 |
if (copy_from_user(&range, user_range, sizeof(range))) |
| 78 |
return -EFAULT; |
| 79 |
|
| 80 |
range.minlen = max_t(u32, range.minlen, bdev_discard_granularity(dev)); |
| 81 |
|
| 82 |
err = ntfs_trim_fs(sbi, &range); |
| 83 |
if (err < 0) |
| 84 |
return err; |
| 85 |
|
| 86 |
if (copy_to_user(user_range, &range, sizeof(range))) |
| 87 |
return -EFAULT; |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
static int ntfs_ioctl_get_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf) |
| 93 |
{ |
| 94 |
if (copy_to_user(buf, sbi->volume.label, FSLABEL_MAX)) |
| 95 |
return -EFAULT; |
| 96 |
|
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
static int ntfs_ioctl_set_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf) |
| 101 |
{ |
| 102 |
u8 user[FSLABEL_MAX] = { 0 }; |
| 103 |
int len; |
| 104 |
|
| 105 |
if (!capable(CAP_SYS_ADMIN)) |
| 106 |
return -EPERM; |
| 107 |
|
| 108 |
if (copy_from_user(user, buf, FSLABEL_MAX)) |
| 109 |
return -EFAULT; |
| 110 |
|
| 111 |
len = strnlen(user, FSLABEL_MAX); |
| 112 |
|
| 113 |
return ntfs_set_label(sbi, user, len); |
| 114 |
} |
| 115 |
|
| 116 |
/* |
| 117 |
* ntfs_force_shutdown - helper function. Called from ioctl |
| 118 |
*/ |
| 119 |
static int ntfs_force_shutdown(struct super_block *sb, u32 flags) |
| 120 |
{ |
| 121 |
int err; |
| 122 |
struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 123 |
|
| 124 |
if (unlikely(ntfs3_forced_shutdown(sb))) |
| 125 |
return 0; |
| 126 |
|
| 127 |
/* No additional options yet (flags). */ |
| 128 |
err = bdev_freeze(sb->s_bdev); |
| 129 |
if (err) |
| 130 |
return err; |
| 131 |
set_bit(NTFS_FLAGS_SHUTDOWN_BIT, &sbi->flags); |
| 132 |
bdev_thaw(sb->s_bdev); |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
static int ntfs_ioctl_shutdown(struct super_block *sb, unsigned long arg) |
| 137 |
{ |
| 138 |
u32 flags; |
| 139 |
|
| 140 |
if (!capable(CAP_SYS_ADMIN)) |
| 141 |
return -EPERM; |
| 142 |
|
| 143 |
if (get_user(flags, (__u32 __user *)arg)) |
| 144 |
return -EFAULT; |
| 145 |
|
| 146 |
return ntfs_force_shutdown(sb, flags); |
| 147 |
} |
| 148 |
|
| 149 |
/* |
| 150 |
* ntfs_ioctl - file_operations::unlocked_ioctl |
| 151 |
*/ |
| 152 |
long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg) |
| 153 |
{ |
| 154 |
struct inode *inode = file_inode(filp); |
| 155 |
struct super_block *sb = inode->i_sb; |
| 156 |
struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 157 |
|
| 158 |
/* Avoid any operation if inode is bad. */ |
| 159 |
if (unlikely(is_bad_ni(ntfs_i(inode)))) |
| 160 |
return -EINVAL; |
| 161 |
|
| 162 |
switch (cmd) { |
| 163 |
case FITRIM: |
| 164 |
return ntfs_ioctl_fitrim(sbi, arg); |
| 165 |
case FS_IOC_GETFSLABEL: |
| 166 |
return ntfs_ioctl_get_volume_label(sbi, (u8 __user *)arg); |
| 167 |
case FS_IOC_SETFSLABEL: |
| 168 |
return ntfs_ioctl_set_volume_label(sbi, (u8 __user *)arg); |
| 169 |
case NTFS3_IOC_SHUTDOWN: |
| 170 |
return ntfs_ioctl_shutdown(sb, arg); |
| 171 |
} |
| 172 |
return -ENOTTY; /* Inappropriate ioctl for device. */ |
| 173 |
} |
| 174 |
|
| 175 |
#ifdef CONFIG_COMPAT |
| 176 |
long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg) |
| 177 |
|
| 178 |
{ |
| 179 |
return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); |
| 180 |
} |
| 181 |
#endif |
| 182 |
|
| 183 |
/* |
| 184 |
* ntfs_getattr - inode_operations::getattr |
| 185 |
*/ |
| 186 |
int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path, |
| 187 |
struct kstat *stat, u32 request_mask, u32 flags) |
| 188 |
{ |
| 189 |
struct inode *inode = d_inode(path->dentry); |
| 190 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 191 |
|
| 192 |
/* Avoid any operation if inode is bad. */ |
| 193 |
if (unlikely(is_bad_ni(ni))) |
| 194 |
return -EINVAL; |
| 195 |
|
| 196 |
stat->result_mask |= STATX_BTIME; |
| 197 |
stat->btime = ni->i_crtime; |
| 198 |
stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */ |
| 199 |
|
| 200 |
if (inode->i_flags & S_IMMUTABLE) |
| 201 |
stat->attributes |= STATX_ATTR_IMMUTABLE; |
| 202 |
|
| 203 |
if (inode->i_flags & S_APPEND) |
| 204 |
stat->attributes |= STATX_ATTR_APPEND; |
| 205 |
|
| 206 |
if (is_compressed(ni)) |
| 207 |
stat->attributes |= STATX_ATTR_COMPRESSED; |
| 208 |
|
| 209 |
if (is_encrypted(ni)) |
| 210 |
stat->attributes |= STATX_ATTR_ENCRYPTED; |
| 211 |
|
| 212 |
stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED | |
| 213 |
STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND; |
| 214 |
|
| 215 |
generic_fillattr(idmap, request_mask, inode, stat); |
| 216 |
|
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
static int ntfs_extend_initialized_size(struct file *file, |
| 221 |
struct ntfs_inode *ni, |
| 222 |
const loff_t new_valid) |
| 223 |
{ |
| 224 |
struct inode *inode = &ni->vfs_inode; |
| 225 |
const loff_t valid = ni->i_valid; |
| 226 |
int err; |
| 227 |
|
| 228 |
if (valid >= new_valid) |
| 229 |
return 0; |
| 230 |
|
| 231 |
if (is_resident(ni)) { |
| 232 |
ni->i_valid = new_valid; |
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
err = iomap_zero_range(inode, valid, new_valid - valid, NULL, |
| 237 |
&ntfs_iomap_ops, &ntfs_iomap_folio_ops, NULL); |
| 238 |
if (err) { |
| 239 |
ni->i_valid = valid; |
| 240 |
ntfs_inode_warn(inode, |
| 241 |
"failed to extend initialized size to %llx.", |
| 242 |
new_valid); |
| 243 |
return err; |
| 244 |
} |
| 245 |
|
| 246 |
return 0; |
| 247 |
} |
| 248 |
|
| 249 |
static void ntfs_filemap_close(struct vm_area_struct *vma) |
| 250 |
{ |
| 251 |
struct inode *inode = file_inode(vma->vm_file); |
| 252 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 253 |
u64 from = (u64)vma->vm_pgoff << PAGE_SHIFT; |
| 254 |
u64 to = min_t(u64, i_size_read(inode), |
| 255 |
from + vma->vm_end - vma->vm_start); |
| 256 |
|
| 257 |
if (ni->i_valid < to) { |
| 258 |
ni->i_valid = to; |
| 259 |
mark_inode_dirty(inode); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/* Copy of generic_file_vm_ops. */ |
| 264 |
static const struct vm_operations_struct ntfs_file_vm_ops = { |
| 265 |
.close = ntfs_filemap_close, |
| 266 |
.fault = filemap_fault, |
| 267 |
.map_pages = filemap_map_pages, |
| 268 |
.page_mkwrite = filemap_page_mkwrite, |
| 269 |
}; |
| 270 |
|
| 271 |
/* |
| 272 |
* ntfs_file_mmap_prepare - file_operations::mmap_prepare |
| 273 |
*/ |
| 274 |
static int ntfs_file_mmap_prepare(struct vm_area_desc *desc) |
| 275 |
{ |
| 276 |
struct file *file = desc->file; |
| 277 |
struct inode *inode = file_inode(file); |
| 278 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 279 |
const bool rw = vma_desc_test_flags(desc, VMA_WRITE_BIT); |
| 280 |
int err; |
| 281 |
|
| 282 |
/* Avoid any operation if inode is bad. */ |
| 283 |
if (unlikely(is_bad_ni(ni))) |
| 284 |
return -EINVAL; |
| 285 |
|
| 286 |
if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) |
| 287 |
return -EIO; |
| 288 |
|
| 289 |
if (is_encrypted(ni)) { |
| 290 |
ntfs_inode_warn(inode, "mmap encrypted not supported"); |
| 291 |
return -EOPNOTSUPP; |
| 292 |
} |
| 293 |
|
| 294 |
if (is_dedup(ni)) { |
| 295 |
ntfs_inode_warn(inode, "mmap deduplicated not supported"); |
| 296 |
return -EOPNOTSUPP; |
| 297 |
} |
| 298 |
|
| 299 |
if (is_compressed(ni)) { |
| 300 |
if (rw) { |
| 301 |
ntfs_inode_warn(inode, |
| 302 |
"mmap(write) compressed not supported"); |
| 303 |
return -EOPNOTSUPP; |
| 304 |
} |
| 305 |
/* Turn off readahead for compressed files. */ |
| 306 |
file->f_ra.ra_pages = 0; |
| 307 |
} |
| 308 |
|
| 309 |
if (rw) { |
| 310 |
u64 from = (u64)desc->pgoff << PAGE_SHIFT; |
| 311 |
u64 to = min_t(u64, i_size_read(inode), |
| 312 |
from + vma_desc_size(desc)); |
| 313 |
|
| 314 |
if (is_sparsed(ni)) { |
| 315 |
/* Allocate clusters for rw map. */ |
| 316 |
struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; |
| 317 |
CLST lcn, len; |
| 318 |
CLST vcn = from >> sbi->cluster_bits; |
| 319 |
CLST end = bytes_to_cluster(sbi, to); |
| 320 |
bool new; |
| 321 |
|
| 322 |
for (; vcn < end; vcn += len) { |
| 323 |
err = attr_data_get_block(ni, vcn, 1, &lcn, |
| 324 |
&len, &new, true, |
| 325 |
NULL, false); |
| 326 |
if (err) |
| 327 |
goto out; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
if (ni->i_valid < to) { |
| 332 |
if (!inode_trylock(inode)) { |
| 333 |
err = -EAGAIN; |
| 334 |
goto out; |
| 335 |
} |
| 336 |
err = ntfs_extend_initialized_size(file, ni, to); |
| 337 |
inode_unlock(inode); |
| 338 |
if (err) |
| 339 |
goto out; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
err = generic_file_mmap_prepare(desc); |
| 344 |
if (!err && rw) |
| 345 |
desc->vm_ops = &ntfs_file_vm_ops; |
| 346 |
out: |
| 347 |
return err; |
| 348 |
} |
| 349 |
|
| 350 |
static int ntfs_extend(struct inode *inode, loff_t pos, size_t count, |
| 351 |
struct file *file) |
| 352 |
{ |
| 353 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 354 |
struct address_space *mapping = inode->i_mapping; |
| 355 |
loff_t end = pos + count; |
| 356 |
bool extend_init = file && pos > ni->i_valid; |
| 357 |
int err; |
| 358 |
|
| 359 |
if (end <= inode->i_size && !extend_init) |
| 360 |
return 0; |
| 361 |
|
| 362 |
/* Mark rw ntfs as dirty. It will be cleared at umount. */ |
| 363 |
ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY); |
| 364 |
|
| 365 |
if (end > inode->i_size) { |
| 366 |
/* |
| 367 |
* Normal files: increase file size, allocate space. |
| 368 |
* Sparse/Compressed: increase file size. No space allocated. |
| 369 |
*/ |
| 370 |
err = ntfs_set_size(inode, end); |
| 371 |
if (err) |
| 372 |
goto out; |
| 373 |
} |
| 374 |
|
| 375 |
if (extend_init && !is_compressed(ni)) { |
| 376 |
err = ntfs_extend_initialized_size(file, ni, pos); |
| 377 |
if (err) |
| 378 |
goto out; |
| 379 |
} else { |
| 380 |
err = 0; |
| 381 |
} |
| 382 |
|
| 383 |
inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); |
| 384 |
mark_inode_dirty(inode); |
| 385 |
|
| 386 |
if (IS_SYNC(inode)) { |
| 387 |
int err2; |
| 388 |
|
| 389 |
err = filemap_fdatawrite_range(mapping, pos, end - 1); |
| 390 |
err2 = sync_mapping_buffers(mapping); |
| 391 |
if (!err) |
| 392 |
err = err2; |
| 393 |
err2 = write_inode_now(inode, 1); |
| 394 |
if (!err) |
| 395 |
err = err2; |
| 396 |
if (!err) |
| 397 |
err = filemap_fdatawait_range(mapping, pos, end - 1); |
| 398 |
} |
| 399 |
|
| 400 |
out: |
| 401 |
return err; |
| 402 |
} |
| 403 |
|
| 404 |
static int ntfs_truncate(struct inode *inode, loff_t new_size) |
| 405 |
{ |
| 406 |
int err; |
| 407 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 408 |
u64 new_valid = min_t(u64, ni->i_valid, new_size); |
| 409 |
|
| 410 |
truncate_setsize(inode, new_size); |
| 411 |
|
| 412 |
ni_lock(ni); |
| 413 |
|
| 414 |
down_write(&ni->file.run_lock); |
| 415 |
err = attr_set_size_ex(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size, |
| 416 |
&new_valid, ni->mi.sbi->options->prealloc, NULL, |
| 417 |
false); |
| 418 |
up_write(&ni->file.run_lock); |
| 419 |
|
| 420 |
ni->i_valid = new_valid; |
| 421 |
|
| 422 |
ni_unlock(ni); |
| 423 |
|
| 424 |
if (err) |
| 425 |
return err; |
| 426 |
|
| 427 |
ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE; |
| 428 |
inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); |
| 429 |
if (!IS_DIRSYNC(inode)) { |
| 430 |
mark_inode_dirty(inode); |
| 431 |
} else { |
| 432 |
err = ntfs_sync_inode(inode); |
| 433 |
if (err) |
| 434 |
return err; |
| 435 |
} |
| 436 |
|
| 437 |
return 0; |
| 438 |
} |
| 439 |
|
| 440 |
/* |
| 441 |
* ntfs_fallocate - file_operations::ntfs_fallocate |
| 442 |
* |
| 443 |
* Preallocate space for a file. This implements ntfs's fallocate file |
| 444 |
* operation, which gets called from sys_fallocate system call. User |
| 445 |
* space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set |
| 446 |
* we just allocate clusters without zeroing them out. Otherwise we |
| 447 |
* allocate and zero out clusters via an expanding truncate. |
| 448 |
*/ |
| 449 |
static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len) |
| 450 |
{ |
| 451 |
struct inode *inode = file_inode(file); |
| 452 |
struct address_space *mapping = inode->i_mapping; |
| 453 |
struct super_block *sb = inode->i_sb; |
| 454 |
struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 455 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 456 |
loff_t end = vbo + len; |
| 457 |
loff_t vbo_down = round_down(vbo, max_t(unsigned long, |
| 458 |
sbi->cluster_size, PAGE_SIZE)); |
| 459 |
bool is_supported_holes = is_sparsed(ni) || is_compressed(ni); |
| 460 |
loff_t i_size, new_size; |
| 461 |
bool map_locked; |
| 462 |
int err; |
| 463 |
|
| 464 |
/* No support for dir. */ |
| 465 |
if (!S_ISREG(inode->i_mode)) |
| 466 |
return -EOPNOTSUPP; |
| 467 |
|
| 468 |
/* |
| 469 |
* vfs_fallocate checks all possible combinations of mode. |
| 470 |
* Do additional checks here before ntfs_set_state(dirty). |
| 471 |
*/ |
| 472 |
if (mode & FALLOC_FL_PUNCH_HOLE) { |
| 473 |
if (!is_supported_holes) |
| 474 |
return -EOPNOTSUPP; |
| 475 |
} else if (mode & FALLOC_FL_COLLAPSE_RANGE) { |
| 476 |
} else if (mode & FALLOC_FL_INSERT_RANGE) { |
| 477 |
if (!is_supported_holes) |
| 478 |
return -EOPNOTSUPP; |
| 479 |
} else if (mode & |
| 480 |
~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | |
| 481 |
FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) { |
| 482 |
ntfs_inode_warn(inode, "fallocate(0x%x) is not supported", |
| 483 |
mode); |
| 484 |
return -EOPNOTSUPP; |
| 485 |
} |
| 486 |
|
| 487 |
ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); |
| 488 |
|
| 489 |
inode_lock(inode); |
| 490 |
i_size = inode->i_size; |
| 491 |
new_size = max(end, i_size); |
| 492 |
map_locked = false; |
| 493 |
|
| 494 |
if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { |
| 495 |
/* Should never be here, see ntfs_file_open. */ |
| 496 |
err = -EOPNOTSUPP; |
| 497 |
goto out; |
| 498 |
} |
| 499 |
|
| 500 |
if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE | |
| 501 |
FALLOC_FL_INSERT_RANGE)) { |
| 502 |
inode_dio_wait(inode); |
| 503 |
filemap_invalidate_lock(mapping); |
| 504 |
map_locked = true; |
| 505 |
} |
| 506 |
|
| 507 |
if (mode & FALLOC_FL_PUNCH_HOLE) { |
| 508 |
u32 frame_size; |
| 509 |
loff_t mask, vbo_a, end_a, tmp, from; |
| 510 |
|
| 511 |
err = filemap_write_and_wait_range(mapping, vbo_down, |
| 512 |
LLONG_MAX); |
| 513 |
if (err) |
| 514 |
goto out; |
| 515 |
|
| 516 |
truncate_pagecache(inode, vbo_down); |
| 517 |
|
| 518 |
ni_lock(ni); |
| 519 |
err = attr_punch_hole(ni, vbo, len, &frame_size); |
| 520 |
ni_unlock(ni); |
| 521 |
if (!err) |
| 522 |
goto ok; |
| 523 |
|
| 524 |
if (err != E_NTFS_NOTALIGNED) |
| 525 |
goto out; |
| 526 |
|
| 527 |
/* Process not aligned punch. */ |
| 528 |
err = 0; |
| 529 |
if (end > i_size) |
| 530 |
end = i_size; |
| 531 |
mask = frame_size - 1; |
| 532 |
vbo_a = (vbo + mask) & ~mask; |
| 533 |
end_a = end & ~mask; |
| 534 |
|
| 535 |
tmp = min(vbo_a, end); |
| 536 |
from = min_t(loff_t, ni->i_valid, vbo); |
| 537 |
/* Zero head of punch. */ |
| 538 |
if (tmp > from) { |
| 539 |
err = iomap_zero_range(inode, from, tmp - from, NULL, |
| 540 |
&ntfs_iomap_ops, |
| 541 |
&ntfs_iomap_folio_ops, NULL); |
| 542 |
if (err) |
| 543 |
goto out; |
| 544 |
} |
| 545 |
|
| 546 |
/* Aligned punch_hole. Deallocate clusters. */ |
| 547 |
if (end_a > vbo_a) { |
| 548 |
ni_lock(ni); |
| 549 |
err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL); |
| 550 |
ni_unlock(ni); |
| 551 |
if (err) |
| 552 |
goto out; |
| 553 |
} |
| 554 |
|
| 555 |
/* Zero tail of punch. */ |
| 556 |
if (vbo < end_a && end_a < end) { |
| 557 |
err = iomap_zero_range(inode, end_a, end - end_a, NULL, |
| 558 |
&ntfs_iomap_ops, |
| 559 |
&ntfs_iomap_folio_ops, NULL); |
| 560 |
if (err) |
| 561 |
goto out; |
| 562 |
} |
| 563 |
} else if (mode & FALLOC_FL_COLLAPSE_RANGE) { |
| 564 |
/* |
| 565 |
* Write tail of the last page before removed range since |
| 566 |
* it will get removed from the page cache below. |
| 567 |
*/ |
| 568 |
err = filemap_write_and_wait_range(mapping, vbo_down, vbo); |
| 569 |
if (err) |
| 570 |
goto out; |
| 571 |
|
| 572 |
/* |
| 573 |
* Write data that will be shifted to preserve them |
| 574 |
* when discarding page cache below. |
| 575 |
*/ |
| 576 |
err = filemap_write_and_wait_range(mapping, end, LLONG_MAX); |
| 577 |
if (err) |
| 578 |
goto out; |
| 579 |
|
| 580 |
truncate_pagecache(inode, vbo_down); |
| 581 |
|
| 582 |
ni_lock(ni); |
| 583 |
err = attr_collapse_range(ni, vbo, len); |
| 584 |
ni_unlock(ni); |
| 585 |
if (err) |
| 586 |
goto out; |
| 587 |
} else if (mode & FALLOC_FL_INSERT_RANGE) { |
| 588 |
/* Check new size. */ |
| 589 |
err = inode_newsize_ok(inode, new_size); |
| 590 |
if (err) |
| 591 |
goto out; |
| 592 |
|
| 593 |
/* Write out all dirty pages. */ |
| 594 |
err = filemap_write_and_wait_range(mapping, vbo_down, |
| 595 |
LLONG_MAX); |
| 596 |
if (err) |
| 597 |
goto out; |
| 598 |
truncate_pagecache(inode, vbo_down); |
| 599 |
|
| 600 |
ni_lock(ni); |
| 601 |
err = attr_insert_range(ni, vbo, len); |
| 602 |
ni_unlock(ni); |
| 603 |
if (err) |
| 604 |
goto out; |
| 605 |
} else { |
| 606 |
/* Check new size. */ |
| 607 |
u8 cluster_bits = sbi->cluster_bits; |
| 608 |
|
| 609 |
/* Be sure file is non resident. */ |
| 610 |
if (is_resident(ni)) { |
| 611 |
ni_lock(ni); |
| 612 |
err = attr_force_nonresident(ni); |
| 613 |
ni_unlock(ni); |
| 614 |
if (err) |
| 615 |
goto out; |
| 616 |
} |
| 617 |
|
| 618 |
/* generic/213: expected -ENOSPC instead of -EFBIG. */ |
| 619 |
if (!is_supported_holes) { |
| 620 |
loff_t to_alloc = new_size - inode_get_bytes(inode); |
| 621 |
|
| 622 |
if (to_alloc > 0 && |
| 623 |
(to_alloc >> cluster_bits) > |
| 624 |
wnd_zeroes(&sbi->used.bitmap)) { |
| 625 |
err = -ENOSPC; |
| 626 |
goto out; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
err = inode_newsize_ok(inode, new_size); |
| 631 |
if (err) |
| 632 |
goto out; |
| 633 |
|
| 634 |
if (new_size > i_size) { |
| 635 |
/* |
| 636 |
* Allocate clusters, do not change 'valid' size. |
| 637 |
*/ |
| 638 |
err = ntfs_set_size(inode, new_size); |
| 639 |
if (err) |
| 640 |
goto out; |
| 641 |
} |
| 642 |
|
| 643 |
if (is_supported_holes) { |
| 644 |
CLST vcn = vbo >> cluster_bits; |
| 645 |
CLST cend = bytes_to_cluster(sbi, end); |
| 646 |
CLST cend_v = bytes_to_cluster(sbi, ni->i_valid); |
| 647 |
CLST lcn, clen; |
| 648 |
bool new; |
| 649 |
|
| 650 |
if (cend_v > cend) |
| 651 |
cend_v = cend; |
| 652 |
|
| 653 |
/* |
| 654 |
* Allocate and zero new clusters. |
| 655 |
* Zeroing these clusters may be too long. |
| 656 |
*/ |
| 657 |
for (; vcn < cend_v; vcn += clen) { |
| 658 |
err = attr_data_get_block(ni, vcn, cend_v - vcn, |
| 659 |
&lcn, &clen, &new, |
| 660 |
true, NULL, false); |
| 661 |
if (err) |
| 662 |
goto out; |
| 663 |
} |
| 664 |
|
| 665 |
/* |
| 666 |
* Moving up 'valid size'. |
| 667 |
*/ |
| 668 |
err = ntfs_extend_initialized_size( |
| 669 |
file, ni, (u64)cend_v << cluster_bits); |
| 670 |
if (err) |
| 671 |
goto out; |
| 672 |
|
| 673 |
/* |
| 674 |
* Allocate but not zero new clusters. |
| 675 |
*/ |
| 676 |
for (; vcn < cend; vcn += clen) { |
| 677 |
err = attr_data_get_block(ni, vcn, cend - vcn, |
| 678 |
&lcn, &clen, &new, |
| 679 |
false, NULL, false); |
| 680 |
if (err) |
| 681 |
goto out; |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
if (mode & FALLOC_FL_KEEP_SIZE) { |
| 686 |
ni_lock(ni); |
| 687 |
/* True - Keep preallocated. */ |
| 688 |
err = attr_set_size(ni, ATTR_DATA, NULL, 0, |
| 689 |
&ni->file.run, i_size, &ni->i_valid, |
| 690 |
true); |
| 691 |
ni_unlock(ni); |
| 692 |
if (err) |
| 693 |
goto out; |
| 694 |
i_size_write(inode, i_size); |
| 695 |
} else if (new_size > i_size) { |
| 696 |
i_size_write(inode, new_size); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
ok: |
| 701 |
err = file_modified(file); |
| 702 |
if (err) |
| 703 |
goto out; |
| 704 |
|
| 705 |
out: |
| 706 |
if (map_locked) |
| 707 |
filemap_invalidate_unlock(mapping); |
| 708 |
|
| 709 |
if (!err) { |
| 710 |
inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); |
| 711 |
mark_inode_dirty(inode); |
| 712 |
} |
| 713 |
|
| 714 |
inode_unlock(inode); |
| 715 |
return err; |
| 716 |
} |
| 717 |
|
| 718 |
/* |
| 719 |
* ntfs_setattr - inode_operations::setattr |
| 720 |
*/ |
| 721 |
int ntfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, |
| 722 |
struct iattr *attr) |
| 723 |
{ |
| 724 |
struct inode *inode = d_inode(dentry); |
| 725 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 726 |
u32 ia_valid = attr->ia_valid; |
| 727 |
umode_t mode = inode->i_mode; |
| 728 |
int err; |
| 729 |
|
| 730 |
/* Avoid any operation if inode is bad. */ |
| 731 |
if (unlikely(is_bad_ni(ni))) |
| 732 |
return -EINVAL; |
| 733 |
|
| 734 |
if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) |
| 735 |
return -EIO; |
| 736 |
|
| 737 |
err = setattr_prepare(idmap, dentry, attr); |
| 738 |
if (err) |
| 739 |
goto out; |
| 740 |
|
| 741 |
if (ia_valid & ATTR_SIZE) { |
| 742 |
loff_t newsize, oldsize; |
| 743 |
|
| 744 |
if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { |
| 745 |
/* Should never be here, see ntfs_file_open(). */ |
| 746 |
err = -EOPNOTSUPP; |
| 747 |
goto out; |
| 748 |
} |
| 749 |
inode_dio_wait(inode); |
| 750 |
oldsize = i_size_read(inode); |
| 751 |
newsize = attr->ia_size; |
| 752 |
|
| 753 |
if (newsize <= oldsize) |
| 754 |
err = ntfs_truncate(inode, newsize); |
| 755 |
else |
| 756 |
err = ntfs_extend(inode, newsize, 0, NULL); |
| 757 |
|
| 758 |
if (err) |
| 759 |
goto out; |
| 760 |
|
| 761 |
ni->ni_flags |= NI_FLAG_UPDATE_PARENT; |
| 762 |
i_size_write(inode, newsize); |
| 763 |
} |
| 764 |
|
| 765 |
setattr_copy(idmap, inode, attr); |
| 766 |
|
| 767 |
if (mode != inode->i_mode) { |
| 768 |
err = ntfs_acl_chmod(idmap, dentry); |
| 769 |
if (err) |
| 770 |
goto out; |
| 771 |
|
| 772 |
/* Linux 'w' -> Windows 'ro'. */ |
| 773 |
if (0222 & inode->i_mode) |
| 774 |
ni->std_fa &= ~FILE_ATTRIBUTE_READONLY; |
| 775 |
else |
| 776 |
ni->std_fa |= FILE_ATTRIBUTE_READONLY; |
| 777 |
} |
| 778 |
|
| 779 |
if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) |
| 780 |
ntfs_save_wsl_perm(inode, NULL); |
| 781 |
mark_inode_dirty(inode); |
| 782 |
out: |
| 783 |
return err; |
| 784 |
} |
| 785 |
|
| 786 |
/* |
| 787 |
* check_read_restriction: |
| 788 |
* common code for ntfs_file_read_iter and ntfs_file_splice_read |
| 789 |
*/ |
| 790 |
static int check_read_restriction(struct inode *inode) |
| 791 |
{ |
| 792 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 793 |
|
| 794 |
/* Avoid any operation if inode is bad. */ |
| 795 |
if (unlikely(is_bad_ni(ni))) |
| 796 |
return -EINVAL; |
| 797 |
|
| 798 |
if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) |
| 799 |
return -EIO; |
| 800 |
|
| 801 |
if (is_encrypted(ni)) { |
| 802 |
ntfs_inode_warn(inode, "encrypted i/o not supported"); |
| 803 |
return -EOPNOTSUPP; |
| 804 |
} |
| 805 |
|
| 806 |
#ifndef CONFIG_NTFS3_LZX_XPRESS |
| 807 |
if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) { |
| 808 |
ntfs_inode_warn( |
| 809 |
inode, |
| 810 |
"activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files"); |
| 811 |
return -EOPNOTSUPP; |
| 812 |
} |
| 813 |
#endif |
| 814 |
|
| 815 |
if (is_dedup(ni)) { |
| 816 |
ntfs_inode_warn(inode, "read deduplicated not supported"); |
| 817 |
return -EOPNOTSUPP; |
| 818 |
} |
| 819 |
|
| 820 |
return 0; |
| 821 |
} |
| 822 |
|
| 823 |
/* |
| 824 |
* ntfs_file_read_iter - file_operations::read_iter |
| 825 |
*/ |
| 826 |
static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 827 |
{ |
| 828 |
struct file *file = iocb->ki_filp; |
| 829 |
struct inode *inode = file_inode(file); |
| 830 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 831 |
size_t bytes = iov_iter_count(iter); |
| 832 |
loff_t valid, i_size, vbo, end; |
| 833 |
unsigned int dio_flags; |
| 834 |
ssize_t err; |
| 835 |
|
| 836 |
err = check_read_restriction(inode); |
| 837 |
if (err) |
| 838 |
return err; |
| 839 |
|
| 840 |
if (!bytes) |
| 841 |
return 0; /* skip atime */ |
| 842 |
|
| 843 |
if (is_compressed(ni)) { |
| 844 |
if (iocb->ki_flags & IOCB_DIRECT) { |
| 845 |
ntfs_inode_warn( |
| 846 |
inode, "direct i/o + compressed not supported"); |
| 847 |
return -EOPNOTSUPP; |
| 848 |
} |
| 849 |
/* Turn off readahead for compressed files. */ |
| 850 |
file->f_ra.ra_pages = 0; |
| 851 |
} |
| 852 |
|
| 853 |
/* Fallback to buffered I/O if the inode does not support direct I/O. */ |
| 854 |
if (!(iocb->ki_flags & IOCB_DIRECT) || |
| 855 |
!ntfs_should_use_dio(iocb, iter)) { |
| 856 |
iocb->ki_flags &= ~IOCB_DIRECT; |
| 857 |
return generic_file_read_iter(iocb, iter); |
| 858 |
} |
| 859 |
|
| 860 |
if (iocb->ki_flags & IOCB_NOWAIT) { |
| 861 |
if (!inode_trylock_shared(inode)) |
| 862 |
return -EAGAIN; |
| 863 |
} else { |
| 864 |
inode_lock_shared(inode); |
| 865 |
} |
| 866 |
|
| 867 |
vbo = iocb->ki_pos; |
| 868 |
end = vbo + bytes; |
| 869 |
dio_flags = 0; |
| 870 |
valid = ni->i_valid; |
| 871 |
i_size = inode->i_size; |
| 872 |
|
| 873 |
if (vbo < valid) { |
| 874 |
if (valid < end) { |
| 875 |
/* read cross 'valid' size. */ |
| 876 |
dio_flags |= IOMAP_DIO_FORCE_WAIT; |
| 877 |
} |
| 878 |
|
| 879 |
if (ni->file.run_da.count) { |
| 880 |
/* Direct I/O is not compatible with delalloc. */ |
| 881 |
err = ni_allocate_da_blocks(ni); |
| 882 |
if (err) |
| 883 |
goto out; |
| 884 |
} |
| 885 |
|
| 886 |
err = iomap_dio_rw(iocb, iter, &ntfs_iomap_ops, NULL, dio_flags, |
| 887 |
NULL, 0); |
| 888 |
|
| 889 |
if (err <= 0) |
| 890 |
goto out; |
| 891 |
end = vbo + err; |
| 892 |
if (valid < end) { |
| 893 |
size_t to_zero = end - valid; |
| 894 |
/* Fix iter. */ |
| 895 |
iov_iter_revert(iter, to_zero); |
| 896 |
iov_iter_zero(to_zero, iter); |
| 897 |
} |
| 898 |
} else if (vbo < i_size) { |
| 899 |
if (end > i_size) |
| 900 |
bytes = i_size - vbo; |
| 901 |
iov_iter_zero(bytes, iter); |
| 902 |
iocb->ki_pos += bytes; |
| 903 |
err = bytes; |
| 904 |
} |
| 905 |
|
| 906 |
out: |
| 907 |
inode_unlock_shared(inode); |
| 908 |
file_accessed(iocb->ki_filp); |
| 909 |
return err; |
| 910 |
} |
| 911 |
|
| 912 |
/* |
| 913 |
* ntfs_file_splice_read - file_operations::splice_read |
| 914 |
*/ |
| 915 |
static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos, |
| 916 |
struct pipe_inode_info *pipe, size_t len, |
| 917 |
unsigned int flags) |
| 918 |
{ |
| 919 |
struct inode *inode = file_inode(in); |
| 920 |
ssize_t err; |
| 921 |
|
| 922 |
err = check_read_restriction(inode); |
| 923 |
if (err) |
| 924 |
return err; |
| 925 |
|
| 926 |
if (is_compressed(ntfs_i(inode))) { |
| 927 |
/* Turn off readahead for compressed files. */ |
| 928 |
in->f_ra.ra_pages = 0; |
| 929 |
} |
| 930 |
|
| 931 |
return filemap_splice_read(in, ppos, pipe, len, flags); |
| 932 |
} |
| 933 |
|
| 934 |
/* |
| 935 |
* ntfs_get_frame_pages |
| 936 |
* |
| 937 |
* Return: Array of locked pages. |
| 938 |
*/ |
| 939 |
static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index, |
| 940 |
struct page **pages, u32 pages_per_frame, |
| 941 |
bool *frame_uptodate) |
| 942 |
{ |
| 943 |
gfp_t gfp_mask = mapping_gfp_mask(mapping); |
| 944 |
u32 npages; |
| 945 |
|
| 946 |
*frame_uptodate = true; |
| 947 |
|
| 948 |
for (npages = 0; npages < pages_per_frame; npages++, index++) { |
| 949 |
struct folio *folio; |
| 950 |
|
| 951 |
folio = __filemap_get_folio(mapping, index, |
| 952 |
FGP_LOCK | FGP_ACCESSED | FGP_CREAT, |
| 953 |
gfp_mask | __GFP_ZERO); |
| 954 |
if (IS_ERR(folio)) { |
| 955 |
while (npages--) { |
| 956 |
folio = page_folio(pages[npages]); |
| 957 |
folio_unlock(folio); |
| 958 |
folio_put(folio); |
| 959 |
} |
| 960 |
|
| 961 |
return -ENOMEM; |
| 962 |
} |
| 963 |
|
| 964 |
if (!folio_test_uptodate(folio)) |
| 965 |
*frame_uptodate = false; |
| 966 |
|
| 967 |
pages[npages] = &folio->page; |
| 968 |
} |
| 969 |
|
| 970 |
return 0; |
| 971 |
} |
| 972 |
|
| 973 |
/* |
| 974 |
* ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files). |
| 975 |
*/ |
| 976 |
static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from) |
| 977 |
{ |
| 978 |
int err; |
| 979 |
struct file *file = iocb->ki_filp; |
| 980 |
size_t count = iov_iter_count(from); |
| 981 |
loff_t pos = iocb->ki_pos; |
| 982 |
struct inode *inode = file_inode(file); |
| 983 |
loff_t i_size = i_size_read(inode); |
| 984 |
struct address_space *mapping = inode->i_mapping; |
| 985 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 986 |
u64 valid = ni->i_valid; |
| 987 |
struct ntfs_sb_info *sbi = ni->mi.sbi; |
| 988 |
struct page **pages = NULL; |
| 989 |
struct folio *folio; |
| 990 |
size_t written = 0; |
| 991 |
u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits; |
| 992 |
u32 frame_size = 1u << frame_bits; |
| 993 |
u32 pages_per_frame = frame_size >> PAGE_SHIFT; |
| 994 |
u32 ip, off; |
| 995 |
CLST frame; |
| 996 |
u64 frame_vbo; |
| 997 |
pgoff_t index; |
| 998 |
bool frame_uptodate; |
| 999 |
|
| 1000 |
if (frame_size < PAGE_SIZE) { |
| 1001 |
/* |
| 1002 |
* frame_size == 8K if cluster 512 |
| 1003 |
* frame_size == 64K if cluster 4096 |
| 1004 |
*/ |
| 1005 |
ntfs_inode_warn(inode, "page size is bigger than frame size"); |
| 1006 |
return -EOPNOTSUPP; |
| 1007 |
} |
| 1008 |
|
| 1009 |
pages = kmalloc_objs(struct page *, pages_per_frame, GFP_NOFS); |
| 1010 |
if (!pages) |
| 1011 |
return -ENOMEM; |
| 1012 |
|
| 1013 |
err = file_remove_privs(file); |
| 1014 |
if (err) |
| 1015 |
goto out; |
| 1016 |
|
| 1017 |
err = file_update_time(file); |
| 1018 |
if (err) |
| 1019 |
goto out; |
| 1020 |
|
| 1021 |
/* Zero range [valid : pos). */ |
| 1022 |
while (valid < pos) { |
| 1023 |
CLST lcn, clen; |
| 1024 |
|
| 1025 |
frame = valid >> frame_bits; |
| 1026 |
frame_vbo = valid & ~(frame_size - 1); |
| 1027 |
off = valid & (frame_size - 1); |
| 1028 |
|
| 1029 |
err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 1, &lcn, |
| 1030 |
&clen, NULL, false, NULL, false); |
| 1031 |
if (err) |
| 1032 |
goto out; |
| 1033 |
|
| 1034 |
if (lcn == SPARSE_LCN) { |
| 1035 |
ni->i_valid = valid = |
| 1036 |
frame_vbo + ((u64)clen << sbi->cluster_bits); |
| 1037 |
continue; |
| 1038 |
} |
| 1039 |
|
| 1040 |
/* Load full frame. */ |
| 1041 |
err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT, |
| 1042 |
pages, pages_per_frame, |
| 1043 |
&frame_uptodate); |
| 1044 |
if (err) |
| 1045 |
goto out; |
| 1046 |
|
| 1047 |
if (!frame_uptodate && off) { |
| 1048 |
err = ni_read_frame(ni, frame_vbo, pages, |
| 1049 |
pages_per_frame, 0); |
| 1050 |
if (err) { |
| 1051 |
for (ip = 0; ip < pages_per_frame; ip++) { |
| 1052 |
folio = page_folio(pages[ip]); |
| 1053 |
folio_unlock(folio); |
| 1054 |
folio_put(folio); |
| 1055 |
} |
| 1056 |
goto out; |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
ip = off >> PAGE_SHIFT; |
| 1061 |
off = offset_in_page(valid); |
| 1062 |
for (; ip < pages_per_frame; ip++, off = 0) { |
| 1063 |
folio = page_folio(pages[ip]); |
| 1064 |
folio_zero_segment(folio, off, PAGE_SIZE); |
| 1065 |
flush_dcache_folio(folio); |
| 1066 |
folio_mark_uptodate(folio); |
| 1067 |
} |
| 1068 |
|
| 1069 |
ni_lock(ni); |
| 1070 |
err = ni_write_frame(ni, pages, pages_per_frame); |
| 1071 |
ni_unlock(ni); |
| 1072 |
|
| 1073 |
for (ip = 0; ip < pages_per_frame; ip++) { |
| 1074 |
folio = page_folio(pages[ip]); |
| 1075 |
folio_mark_uptodate(folio); |
| 1076 |
folio_unlock(folio); |
| 1077 |
folio_put(folio); |
| 1078 |
} |
| 1079 |
|
| 1080 |
if (err) |
| 1081 |
goto out; |
| 1082 |
|
| 1083 |
ni->i_valid = valid = frame_vbo + frame_size; |
| 1084 |
} |
| 1085 |
|
| 1086 |
/* Copy user data [pos : pos + count). */ |
| 1087 |
while (count) { |
| 1088 |
size_t copied, bytes; |
| 1089 |
|
| 1090 |
off = pos & (frame_size - 1); |
| 1091 |
bytes = frame_size - off; |
| 1092 |
if (bytes > count) |
| 1093 |
bytes = count; |
| 1094 |
|
| 1095 |
frame_vbo = pos & ~(frame_size - 1); |
| 1096 |
index = frame_vbo >> PAGE_SHIFT; |
| 1097 |
|
| 1098 |
if (unlikely(fault_in_iov_iter_readable(from, bytes))) { |
| 1099 |
err = -EFAULT; |
| 1100 |
goto out; |
| 1101 |
} |
| 1102 |
|
| 1103 |
/* Load full frame. */ |
| 1104 |
err = ntfs_get_frame_pages(mapping, index, pages, |
| 1105 |
pages_per_frame, &frame_uptodate); |
| 1106 |
if (err) |
| 1107 |
goto out; |
| 1108 |
|
| 1109 |
if (!frame_uptodate) { |
| 1110 |
loff_t to = pos + bytes; |
| 1111 |
|
| 1112 |
if (off || (to < i_size && (to & (frame_size - 1)))) { |
| 1113 |
err = ni_read_frame(ni, frame_vbo, pages, |
| 1114 |
pages_per_frame, 0); |
| 1115 |
if (err) { |
| 1116 |
for (ip = 0; ip < pages_per_frame; |
| 1117 |
ip++) { |
| 1118 |
folio = page_folio(pages[ip]); |
| 1119 |
folio_unlock(folio); |
| 1120 |
folio_put(folio); |
| 1121 |
} |
| 1122 |
goto out; |
| 1123 |
} |
| 1124 |
} |
| 1125 |
} |
| 1126 |
|
| 1127 |
WARN_ON(!bytes); |
| 1128 |
copied = 0; |
| 1129 |
ip = off >> PAGE_SHIFT; |
| 1130 |
off = offset_in_page(pos); |
| 1131 |
|
| 1132 |
/* Copy user data to pages. */ |
| 1133 |
for (;;) { |
| 1134 |
size_t cp, tail = PAGE_SIZE - off; |
| 1135 |
|
| 1136 |
folio = page_folio(pages[ip]); |
| 1137 |
cp = copy_folio_from_iter_atomic( |
| 1138 |
folio, off, min(tail, bytes), from); |
| 1139 |
flush_dcache_folio(folio); |
| 1140 |
|
| 1141 |
copied += cp; |
| 1142 |
bytes -= cp; |
| 1143 |
if (!bytes || !cp) |
| 1144 |
break; |
| 1145 |
|
| 1146 |
if (cp < tail) { |
| 1147 |
off += cp; |
| 1148 |
} else { |
| 1149 |
ip++; |
| 1150 |
off = 0; |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
ni_lock(ni); |
| 1155 |
err = ni_write_frame(ni, pages, pages_per_frame); |
| 1156 |
ni_unlock(ni); |
| 1157 |
|
| 1158 |
for (ip = 0; ip < pages_per_frame; ip++) { |
| 1159 |
folio = page_folio(pages[ip]); |
| 1160 |
folio_clear_dirty(folio); |
| 1161 |
folio_mark_uptodate(folio); |
| 1162 |
folio_unlock(folio); |
| 1163 |
folio_put(folio); |
| 1164 |
} |
| 1165 |
|
| 1166 |
if (err) |
| 1167 |
goto out; |
| 1168 |
|
| 1169 |
/* |
| 1170 |
* We can loop for a long time in here. Be nice and allow |
| 1171 |
* us to schedule out to avoid softlocking if preempt |
| 1172 |
* is disabled. |
| 1173 |
*/ |
| 1174 |
cond_resched(); |
| 1175 |
|
| 1176 |
pos += copied; |
| 1177 |
written += copied; |
| 1178 |
|
| 1179 |
count = iov_iter_count(from); |
| 1180 |
} |
| 1181 |
|
| 1182 |
out: |
| 1183 |
kfree(pages); |
| 1184 |
|
| 1185 |
if (err < 0) |
| 1186 |
return err; |
| 1187 |
|
| 1188 |
iocb->ki_pos += written; |
| 1189 |
if (iocb->ki_pos > ni->i_valid) |
| 1190 |
ni->i_valid = iocb->ki_pos; |
| 1191 |
if (iocb->ki_pos > i_size) |
| 1192 |
i_size_write(inode, iocb->ki_pos); |
| 1193 |
|
| 1194 |
return written; |
| 1195 |
} |
| 1196 |
|
| 1197 |
/* |
| 1198 |
* check_write_restriction: |
| 1199 |
* common code for ntfs_file_write_iter and ntfs_file_splice_write |
| 1200 |
*/ |
| 1201 |
static int check_write_restriction(struct inode *inode) |
| 1202 |
{ |
| 1203 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 1204 |
|
| 1205 |
/* Avoid any operation if inode is bad. */ |
| 1206 |
if (unlikely(is_bad_ni(ni))) |
| 1207 |
return -EINVAL; |
| 1208 |
|
| 1209 |
if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) |
| 1210 |
return -EIO; |
| 1211 |
|
| 1212 |
if (is_encrypted(ni)) { |
| 1213 |
ntfs_inode_warn(inode, "encrypted i/o not supported"); |
| 1214 |
return -EOPNOTSUPP; |
| 1215 |
} |
| 1216 |
|
| 1217 |
if (is_dedup(ni)) { |
| 1218 |
ntfs_inode_warn(inode, "write into deduplicated not supported"); |
| 1219 |
return -EOPNOTSUPP; |
| 1220 |
} |
| 1221 |
|
| 1222 |
if (unlikely(IS_IMMUTABLE(inode))) |
| 1223 |
return -EPERM; |
| 1224 |
|
| 1225 |
return 0; |
| 1226 |
} |
| 1227 |
|
| 1228 |
/* |
| 1229 |
* ntfs_file_write_iter - file_operations::write_iter |
| 1230 |
*/ |
| 1231 |
static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
| 1232 |
{ |
| 1233 |
struct file *file = iocb->ki_filp; |
| 1234 |
struct inode *inode = file_inode(file); |
| 1235 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 1236 |
ssize_t ret, err; |
| 1237 |
|
| 1238 |
if (!inode_trylock(inode)) { |
| 1239 |
if (iocb->ki_flags & IOCB_NOWAIT) |
| 1240 |
return -EAGAIN; |
| 1241 |
inode_lock(inode); |
| 1242 |
} |
| 1243 |
|
| 1244 |
ret = check_write_restriction(inode); |
| 1245 |
if (ret) |
| 1246 |
goto out; |
| 1247 |
|
| 1248 |
if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) { |
| 1249 |
ntfs_inode_warn(inode, "direct i/o + compressed not supported"); |
| 1250 |
ret = -EOPNOTSUPP; |
| 1251 |
goto out; |
| 1252 |
} |
| 1253 |
|
| 1254 |
ret = generic_write_checks(iocb, from); |
| 1255 |
if (ret <= 0) |
| 1256 |
goto out; |
| 1257 |
|
| 1258 |
err = file_modified(iocb->ki_filp); |
| 1259 |
if (err) { |
| 1260 |
ret = err; |
| 1261 |
goto out; |
| 1262 |
} |
| 1263 |
|
| 1264 |
if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { |
| 1265 |
/* Should never be here, see ntfs_file_open(). */ |
| 1266 |
ret = -EOPNOTSUPP; |
| 1267 |
goto out; |
| 1268 |
} |
| 1269 |
|
| 1270 |
ret = ntfs_extend(inode, iocb->ki_pos, ret, file); |
| 1271 |
if (ret) |
| 1272 |
goto out; |
| 1273 |
|
| 1274 |
if (is_compressed(ni)) { |
| 1275 |
ret = ntfs_compress_write(iocb, from); |
| 1276 |
goto out; |
| 1277 |
} |
| 1278 |
|
| 1279 |
/* Fallback to buffered I/O if the inode does not support direct I/O. */ |
| 1280 |
if (!(iocb->ki_flags & IOCB_DIRECT) || |
| 1281 |
!ntfs_should_use_dio(iocb, from)) { |
| 1282 |
iocb->ki_flags &= ~IOCB_DIRECT; |
| 1283 |
|
| 1284 |
ret = iomap_file_buffered_write(iocb, from, &ntfs_iomap_ops, |
| 1285 |
&ntfs_iomap_folio_ops, NULL); |
| 1286 |
inode_unlock(inode); |
| 1287 |
|
| 1288 |
if (likely(ret > 0)) |
| 1289 |
ret = generic_write_sync(iocb, ret); |
| 1290 |
|
| 1291 |
return ret; |
| 1292 |
} |
| 1293 |
|
| 1294 |
if (ni->file.run_da.count) { |
| 1295 |
/* Direct I/O is not compatible with delalloc. */ |
| 1296 |
ret = ni_allocate_da_blocks(ni); |
| 1297 |
if (ret) |
| 1298 |
goto out; |
| 1299 |
} |
| 1300 |
|
| 1301 |
ret = iomap_dio_rw(iocb, from, &ntfs_iomap_ops, NULL, 0, NULL, 0); |
| 1302 |
|
| 1303 |
if (ret == -ENOTBLK) { |
| 1304 |
/* Returns -ENOTBLK in case of a page invalidation failure for writes.*/ |
| 1305 |
/* The callers needs to fall back to buffered I/O in this case. */ |
| 1306 |
ret = 0; |
| 1307 |
} |
| 1308 |
|
| 1309 |
if (ret >= 0 && iov_iter_count(from)) { |
| 1310 |
loff_t offset = iocb->ki_pos, endbyte; |
| 1311 |
|
| 1312 |
iocb->ki_flags &= ~IOCB_DIRECT; |
| 1313 |
err = iomap_file_buffered_write(iocb, from, &ntfs_iomap_ops, |
| 1314 |
&ntfs_iomap_folio_ops, NULL); |
| 1315 |
if (err < 0) { |
| 1316 |
ret = err; |
| 1317 |
goto out; |
| 1318 |
} |
| 1319 |
|
| 1320 |
/* |
| 1321 |
* We need to ensure that the pages within the page cache for |
| 1322 |
* the range covered by this I/O are written to disk and |
| 1323 |
* invalidated. This is in attempt to preserve the expected |
| 1324 |
* direct I/O semantics in the case we fallback to buffered I/O |
| 1325 |
* to complete off the I/O request. |
| 1326 |
*/ |
| 1327 |
ret += err; |
| 1328 |
endbyte = offset + err - 1; |
| 1329 |
err = filemap_write_and_wait_range(inode->i_mapping, offset, |
| 1330 |
endbyte); |
| 1331 |
if (err) { |
| 1332 |
ret = err; |
| 1333 |
goto out; |
| 1334 |
} |
| 1335 |
|
| 1336 |
invalidate_mapping_pages(inode->i_mapping, offset >> PAGE_SHIFT, |
| 1337 |
endbyte >> PAGE_SHIFT); |
| 1338 |
} |
| 1339 |
|
| 1340 |
out: |
| 1341 |
inode_unlock(inode); |
| 1342 |
|
| 1343 |
return ret; |
| 1344 |
} |
| 1345 |
|
| 1346 |
/* |
| 1347 |
* ntfs_file_open - file_operations::open |
| 1348 |
*/ |
| 1349 |
int ntfs_file_open(struct inode *inode, struct file *file) |
| 1350 |
{ |
| 1351 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 1352 |
|
| 1353 |
/* Avoid any operation if inode is bad. */ |
| 1354 |
if (unlikely(is_bad_ni(ni))) |
| 1355 |
return -EINVAL; |
| 1356 |
|
| 1357 |
if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) |
| 1358 |
return -EIO; |
| 1359 |
|
| 1360 |
if (unlikely((is_compressed(ni) || is_encrypted(ni)) && |
| 1361 |
(file->f_flags & O_DIRECT))) { |
| 1362 |
return -EOPNOTSUPP; |
| 1363 |
} |
| 1364 |
|
| 1365 |
/* Decompress "external compressed" file if opened for rw. */ |
| 1366 |
if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) && |
| 1367 |
(file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) { |
| 1368 |
#ifdef CONFIG_NTFS3_LZX_XPRESS |
| 1369 |
int err = ni_decompress_file(ni); |
| 1370 |
|
| 1371 |
if (err) |
| 1372 |
return err; |
| 1373 |
#else |
| 1374 |
ntfs_inode_warn( |
| 1375 |
inode, |
| 1376 |
"activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files"); |
| 1377 |
return -EOPNOTSUPP; |
| 1378 |
#endif |
| 1379 |
} |
| 1380 |
|
| 1381 |
file->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT; |
| 1382 |
|
| 1383 |
return generic_file_open(inode, file); |
| 1384 |
} |
| 1385 |
|
| 1386 |
/* |
| 1387 |
* ntfs_file_release - file_operations::release |
| 1388 |
* |
| 1389 |
* Called when an inode is released. Note that this is different |
| 1390 |
* from ntfs_file_open: open gets called at every open, but release |
| 1391 |
* gets called only when /all/ the files are closed. |
| 1392 |
*/ |
| 1393 |
static int ntfs_file_release(struct inode *inode, struct file *file) |
| 1394 |
{ |
| 1395 |
int err; |
| 1396 |
struct ntfs_inode *ni; |
| 1397 |
|
| 1398 |
if (!(file->f_mode & FMODE_WRITE) || |
| 1399 |
atomic_read(&inode->i_writecount) != 1 || |
| 1400 |
inode->i_ino == MFT_REC_MFT) { |
| 1401 |
return 0; |
| 1402 |
} |
| 1403 |
|
| 1404 |
/* Close the last writer on the inode. */ |
| 1405 |
ni = ntfs_i(inode); |
| 1406 |
|
| 1407 |
/* Allocate delayed blocks (clusters). */ |
| 1408 |
err = ni_allocate_da_blocks(ni); |
| 1409 |
if (err) |
| 1410 |
goto out; |
| 1411 |
|
| 1412 |
if (ni->mi.sbi->options->prealloc) { |
| 1413 |
ni_lock(ni); |
| 1414 |
down_write(&ni->file.run_lock); |
| 1415 |
|
| 1416 |
/* Deallocate preallocated. */ |
| 1417 |
err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, |
| 1418 |
inode->i_size, &ni->i_valid, false); |
| 1419 |
|
| 1420 |
up_write(&ni->file.run_lock); |
| 1421 |
ni_unlock(ni); |
| 1422 |
} |
| 1423 |
out: |
| 1424 |
return err; |
| 1425 |
} |
| 1426 |
|
| 1427 |
/* |
| 1428 |
* ntfs_fiemap - inode_operations::fiemap |
| 1429 |
*/ |
| 1430 |
int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 1431 |
__u64 start, __u64 len) |
| 1432 |
{ |
| 1433 |
int err; |
| 1434 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 1435 |
|
| 1436 |
/* Avoid any operation if inode is bad. */ |
| 1437 |
if (unlikely(is_bad_ni(ni))) |
| 1438 |
return -EINVAL; |
| 1439 |
|
| 1440 |
if (is_compressed(ni)) { |
| 1441 |
/* Unfortunately cp -r incorrectly treats compressed clusters. */ |
| 1442 |
ntfs_inode_warn(inode, |
| 1443 |
"fiemap is not supported for compressed file"); |
| 1444 |
return -EOPNOTSUPP; |
| 1445 |
} |
| 1446 |
|
| 1447 |
if (S_ISDIR(inode->i_mode)) { |
| 1448 |
/* TODO: add support for dirs (ATTR_ALLOC). */ |
| 1449 |
ntfs_inode_warn(inode, |
| 1450 |
"fiemap is not supported for directories"); |
| 1451 |
return -EOPNOTSUPP; |
| 1452 |
} |
| 1453 |
|
| 1454 |
if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { |
| 1455 |
ntfs_inode_warn(inode, "fiemap(xattr) is not supported"); |
| 1456 |
return -EOPNOTSUPP; |
| 1457 |
} |
| 1458 |
|
| 1459 |
inode_lock_shared(inode); |
| 1460 |
|
| 1461 |
err = iomap_fiemap(inode, fieinfo, start, len, &ntfs_iomap_ops); |
| 1462 |
|
| 1463 |
inode_unlock_shared(inode); |
| 1464 |
return err; |
| 1465 |
} |
| 1466 |
|
| 1467 |
/* |
| 1468 |
* ntfs_file_splice_write - file_operations::splice_write |
| 1469 |
*/ |
| 1470 |
static ssize_t ntfs_file_splice_write(struct pipe_inode_info *pipe, |
| 1471 |
struct file *file, loff_t *ppos, |
| 1472 |
size_t len, unsigned int flags) |
| 1473 |
{ |
| 1474 |
ssize_t err; |
| 1475 |
struct inode *inode = file_inode(file); |
| 1476 |
|
| 1477 |
err = check_write_restriction(inode); |
| 1478 |
if (err) |
| 1479 |
return err; |
| 1480 |
|
| 1481 |
return iter_file_splice_write(pipe, file, ppos, len, flags); |
| 1482 |
} |
| 1483 |
|
| 1484 |
/* |
| 1485 |
* ntfs_file_fsync - file_operations::fsync |
| 1486 |
*/ |
| 1487 |
int ntfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
| 1488 |
{ |
| 1489 |
struct inode *inode = file_inode(file); |
| 1490 |
struct super_block *sb = inode->i_sb; |
| 1491 |
struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 1492 |
int err, ret; |
| 1493 |
|
| 1494 |
if (unlikely(ntfs3_forced_shutdown(sb))) |
| 1495 |
return -EIO; |
| 1496 |
|
| 1497 |
ret = file_write_and_wait_range(file, start, end); |
| 1498 |
if (ret) |
| 1499 |
return ret; |
| 1500 |
|
| 1501 |
ret = write_inode_now(inode, !datasync); |
| 1502 |
|
| 1503 |
if (!ret) { |
| 1504 |
ret = ni_write_parents(ntfs_i(inode), !datasync); |
| 1505 |
} |
| 1506 |
|
| 1507 |
if (!ret) { |
| 1508 |
ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); |
| 1509 |
ntfs_update_mftmirr(sbi); |
| 1510 |
} |
| 1511 |
|
| 1512 |
err = sync_blockdev(sb->s_bdev); |
| 1513 |
if (unlikely(err && !ret)) |
| 1514 |
ret = err; |
| 1515 |
if (!ret) |
| 1516 |
blkdev_issue_flush(sb->s_bdev); |
| 1517 |
return ret; |
| 1518 |
} |
| 1519 |
|
| 1520 |
/* |
| 1521 |
* ntfs_llseek - file_operations::llseek |
| 1522 |
*/ |
| 1523 |
static loff_t ntfs_llseek(struct file *file, loff_t offset, int whence) |
| 1524 |
{ |
| 1525 |
struct inode *inode = file->f_mapping->host; |
| 1526 |
struct ntfs_inode *ni = ntfs_i(inode); |
| 1527 |
loff_t maxbytes = ntfs_get_maxbytes(ni); |
| 1528 |
loff_t ret; |
| 1529 |
|
| 1530 |
if (whence == SEEK_DATA || whence == SEEK_HOLE) { |
| 1531 |
inode_lock_shared(inode); |
| 1532 |
/* Scan file for hole or data. */ |
| 1533 |
ret = ni_seek_data_or_hole(ni, offset, whence == SEEK_DATA); |
| 1534 |
inode_unlock_shared(inode); |
| 1535 |
|
| 1536 |
if (ret >= 0) |
| 1537 |
ret = vfs_setpos(file, ret, maxbytes); |
| 1538 |
} else { |
| 1539 |
ret = generic_file_llseek_size(file, offset, whence, maxbytes, |
| 1540 |
i_size_read(inode)); |
| 1541 |
} |
| 1542 |
return ret; |
| 1543 |
} |
| 1544 |
|
| 1545 |
// clang-format off |
| 1546 |
const struct inode_operations ntfs_file_inode_operations = { |
| 1547 |
.getattr = ntfs_getattr, |
| 1548 |
.setattr = ntfs_setattr, |
| 1549 |
.listxattr = ntfs_listxattr, |
| 1550 |
.get_acl = ntfs_get_acl, |
| 1551 |
.set_acl = ntfs_set_acl, |
| 1552 |
.fiemap = ntfs_fiemap, |
| 1553 |
}; |
| 1554 |
|
| 1555 |
const struct file_operations ntfs_file_operations = { |
| 1556 |
.llseek = ntfs_llseek, |
| 1557 |
.read_iter = ntfs_file_read_iter, |
| 1558 |
.write_iter = ntfs_file_write_iter, |
| 1559 |
.unlocked_ioctl = ntfs_ioctl, |
| 1560 |
#ifdef CONFIG_COMPAT |
| 1561 |
.compat_ioctl = ntfs_compat_ioctl, |
| 1562 |
#endif |
| 1563 |
.splice_read = ntfs_file_splice_read, |
| 1564 |
.splice_write = ntfs_file_splice_write, |
| 1565 |
.mmap_prepare = ntfs_file_mmap_prepare, |
| 1566 |
.open = ntfs_file_open, |
| 1567 |
.fsync = ntfs_file_fsync, |
| 1568 |
.fallocate = ntfs_fallocate, |
| 1569 |
.release = ntfs_file_release, |
| 1570 |
.setlease = generic_setlease, |
| 1571 |
}; |
| 1572 |
|
| 1573 |
#if IS_ENABLED(CONFIG_NTFS_FS) |
| 1574 |
const struct file_operations ntfs_legacy_file_operations = { |
| 1575 |
.llseek = generic_file_llseek, |
| 1576 |
.read_iter = ntfs_file_read_iter, |
| 1577 |
.splice_read = ntfs_file_splice_read, |
| 1578 |
.open = ntfs_file_open, |
| 1579 |
.release = ntfs_file_release, |
| 1580 |
.setlease = generic_setlease, |
| 1581 |
}; |
| 1582 |
#endif |
| 1583 |
// clang-format on |