Konstantin Komarov | 4342306 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 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 | #include <linux/backing-dev.h> |
| 9 | #include <linux/buffer_head.h> |
| 10 | #include <linux/compat.h> |
| 11 | #include <linux/falloc.h> |
| 12 | #include <linux/fiemap.h> |
| 13 | #include <linux/msdos_fs.h> /* FAT_IOCTL_XXX */ |
| 14 | #include <linux/nls.h> |
| 15 | |
| 16 | #include "debug.h" |
| 17 | #include "ntfs.h" |
| 18 | #include "ntfs_fs.h" |
| 19 | |
| 20 | static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg) |
| 21 | { |
| 22 | struct fstrim_range __user *user_range; |
| 23 | struct fstrim_range range; |
| 24 | struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev); |
| 25 | int err; |
| 26 | |
| 27 | if (!capable(CAP_SYS_ADMIN)) |
| 28 | return -EPERM; |
| 29 | |
| 30 | if (!blk_queue_discard(q)) |
| 31 | return -EOPNOTSUPP; |
| 32 | |
| 33 | user_range = (struct fstrim_range __user *)arg; |
| 34 | if (copy_from_user(&range, user_range, sizeof(range))) |
| 35 | return -EFAULT; |
| 36 | |
| 37 | range.minlen = max_t(u32, range.minlen, q->limits.discard_granularity); |
| 38 | |
| 39 | err = ntfs_trim_fs(sbi, &range); |
| 40 | if (err < 0) |
| 41 | return err; |
| 42 | |
| 43 | if (copy_to_user(user_range, &range, sizeof(range))) |
| 44 | return -EFAULT; |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg) |
| 50 | { |
| 51 | struct inode *inode = file_inode(filp); |
| 52 | struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; |
| 53 | u32 __user *user_attr = (u32 __user *)arg; |
| 54 | |
| 55 | switch (cmd) { |
| 56 | case FAT_IOCTL_GET_ATTRIBUTES: |
| 57 | return put_user(le32_to_cpu(ntfs_i(inode)->std_fa), user_attr); |
| 58 | |
| 59 | case FAT_IOCTL_GET_VOLUME_ID: |
| 60 | return put_user(sbi->volume.ser_num, user_attr); |
| 61 | |
| 62 | case FITRIM: |
| 63 | return ntfs_ioctl_fitrim(sbi, arg); |
| 64 | } |
| 65 | return -ENOTTY; /* Inappropriate ioctl for device */ |
| 66 | } |
| 67 | |
| 68 | #ifdef CONFIG_COMPAT |
| 69 | static long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg) |
| 70 | |
| 71 | { |
| 72 | return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); |
| 73 | } |
| 74 | #endif |
| 75 | |
| 76 | /* |
| 77 | * inode_operations::getattr |
| 78 | */ |
| 79 | int ntfs_getattr(struct user_namespace *mnt_userns, const struct path *path, |
| 80 | struct kstat *stat, u32 request_mask, u32 flags) |
| 81 | { |
| 82 | struct inode *inode = d_inode(path->dentry); |
| 83 | struct ntfs_inode *ni = ntfs_i(inode); |
| 84 | |
| 85 | if (is_compressed(ni)) |
| 86 | stat->attributes |= STATX_ATTR_COMPRESSED; |
| 87 | |
| 88 | if (is_encrypted(ni)) |
| 89 | stat->attributes |= STATX_ATTR_ENCRYPTED; |
| 90 | |
| 91 | stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED; |
| 92 | |
| 93 | generic_fillattr(mnt_userns, inode, stat); |
| 94 | |
| 95 | stat->result_mask |= STATX_BTIME; |
| 96 | stat->btime = ni->i_crtime; |
| 97 | stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */ |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int ntfs_extend_initialized_size(struct file *file, |
| 103 | struct ntfs_inode *ni, |
| 104 | const loff_t valid, |
| 105 | const loff_t new_valid) |
| 106 | { |
| 107 | struct inode *inode = &ni->vfs_inode; |
| 108 | struct address_space *mapping = inode->i_mapping; |
| 109 | struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; |
| 110 | loff_t pos = valid; |
| 111 | int err; |
| 112 | |
| 113 | if (is_resident(ni)) { |
| 114 | ni->i_valid = new_valid; |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | WARN_ON(is_compressed(ni)); |
| 119 | WARN_ON(valid >= new_valid); |
| 120 | |
| 121 | for (;;) { |
| 122 | u32 zerofrom, len; |
| 123 | struct page *page; |
| 124 | void *fsdata; |
| 125 | u8 bits; |
| 126 | CLST vcn, lcn, clen; |
| 127 | |
| 128 | if (is_sparsed(ni)) { |
| 129 | bits = sbi->cluster_bits; |
| 130 | vcn = pos >> bits; |
| 131 | |
| 132 | err = attr_data_get_block(ni, vcn, 0, &lcn, &clen, |
| 133 | NULL); |
| 134 | if (err) |
| 135 | goto out; |
| 136 | |
| 137 | if (lcn == SPARSE_LCN) { |
| 138 | loff_t vbo = (loff_t)vcn << bits; |
| 139 | loff_t to = vbo + ((loff_t)clen << bits); |
| 140 | |
| 141 | if (to <= new_valid) { |
| 142 | ni->i_valid = to; |
| 143 | pos = to; |
| 144 | goto next; |
| 145 | } |
| 146 | |
| 147 | if (vbo < pos) { |
| 148 | pos = vbo; |
| 149 | } else { |
| 150 | to = (new_valid >> bits) << bits; |
| 151 | if (pos < to) { |
| 152 | ni->i_valid = to; |
| 153 | pos = to; |
| 154 | goto next; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | zerofrom = pos & (PAGE_SIZE - 1); |
| 161 | len = PAGE_SIZE - zerofrom; |
| 162 | |
| 163 | if (pos + len > new_valid) |
| 164 | len = new_valid - pos; |
| 165 | |
| 166 | err = pagecache_write_begin(file, mapping, pos, len, 0, &page, |
| 167 | &fsdata); |
| 168 | if (err) |
| 169 | goto out; |
| 170 | |
| 171 | zero_user_segment(page, zerofrom, PAGE_SIZE); |
| 172 | |
| 173 | /* this function in any case puts page*/ |
| 174 | err = pagecache_write_end(file, mapping, pos, len, len, page, |
| 175 | fsdata); |
| 176 | if (err < 0) |
| 177 | goto out; |
| 178 | pos += len; |
| 179 | |
| 180 | next: |
| 181 | if (pos >= new_valid) |
| 182 | break; |
| 183 | |
| 184 | balance_dirty_pages_ratelimited(mapping); |
| 185 | cond_resched(); |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | |
| 190 | out: |
| 191 | ni->i_valid = valid; |
| 192 | ntfs_inode_warn(inode, "failed to extend initialized size to %llx.", |
| 193 | new_valid); |
| 194 | return err; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * ntfs_zero_range |
| 199 | * |
| 200 | * Helper function for punch_hole. |
| 201 | * It zeroes a range [vbo, vbo_to) |
| 202 | */ |
| 203 | static int ntfs_zero_range(struct inode *inode, u64 vbo, u64 vbo_to) |
| 204 | { |
| 205 | int err = 0; |
| 206 | struct address_space *mapping = inode->i_mapping; |
| 207 | u32 blocksize = 1 << inode->i_blkbits; |
| 208 | pgoff_t idx = vbo >> PAGE_SHIFT; |
| 209 | u32 z_start = vbo & (PAGE_SIZE - 1); |
| 210 | pgoff_t idx_end = (vbo_to + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 211 | loff_t page_off; |
| 212 | struct buffer_head *head, *bh; |
| 213 | u32 bh_next, bh_off, z_end; |
| 214 | sector_t iblock; |
| 215 | struct page *page; |
| 216 | |
| 217 | for (; idx < idx_end; idx += 1, z_start = 0) { |
| 218 | page_off = (loff_t)idx << PAGE_SHIFT; |
| 219 | z_end = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off) |
| 220 | : PAGE_SIZE; |
| 221 | iblock = page_off >> inode->i_blkbits; |
| 222 | |
| 223 | page = find_or_create_page(mapping, idx, |
| 224 | mapping_gfp_constraint(mapping, |
| 225 | ~__GFP_FS)); |
| 226 | if (!page) |
| 227 | return -ENOMEM; |
| 228 | |
| 229 | if (!page_has_buffers(page)) |
| 230 | create_empty_buffers(page, blocksize, 0); |
| 231 | |
| 232 | bh = head = page_buffers(page); |
| 233 | bh_off = 0; |
| 234 | do { |
| 235 | bh_next = bh_off + blocksize; |
| 236 | |
| 237 | if (bh_next <= z_start || bh_off >= z_end) |
| 238 | continue; |
| 239 | |
| 240 | if (!buffer_mapped(bh)) { |
| 241 | ntfs_get_block(inode, iblock, bh, 0); |
| 242 | /* unmapped? It's a hole - nothing to do */ |
| 243 | if (!buffer_mapped(bh)) |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | /* Ok, it's mapped. Make sure it's up-to-date */ |
| 248 | if (PageUptodate(page)) |
| 249 | set_buffer_uptodate(bh); |
| 250 | |
| 251 | if (!buffer_uptodate(bh)) { |
| 252 | lock_buffer(bh); |
| 253 | bh->b_end_io = end_buffer_read_sync; |
| 254 | get_bh(bh); |
| 255 | submit_bh(REQ_OP_READ, 0, bh); |
| 256 | |
| 257 | wait_on_buffer(bh); |
| 258 | if (!buffer_uptodate(bh)) { |
| 259 | unlock_page(page); |
| 260 | put_page(page); |
| 261 | err = -EIO; |
| 262 | goto out; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | mark_buffer_dirty(bh); |
| 267 | |
| 268 | } while (bh_off = bh_next, iblock += 1, |
| 269 | head != (bh = bh->b_this_page)); |
| 270 | |
| 271 | zero_user_segment(page, z_start, z_end); |
| 272 | |
| 273 | unlock_page(page); |
| 274 | put_page(page); |
| 275 | cond_resched(); |
| 276 | } |
| 277 | out: |
| 278 | mark_inode_dirty(inode); |
| 279 | return err; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * ntfs_sparse_cluster |
| 284 | * |
| 285 | * Helper function to zero a new allocated clusters |
| 286 | * NOTE: 512 <= cluster size <= 2M |
| 287 | */ |
| 288 | void ntfs_sparse_cluster(struct inode *inode, struct page *page0, CLST vcn, |
| 289 | CLST len) |
| 290 | { |
| 291 | struct address_space *mapping = inode->i_mapping; |
| 292 | struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; |
| 293 | u64 vbo = (u64)vcn << sbi->cluster_bits; |
| 294 | u64 bytes = (u64)len << sbi->cluster_bits; |
| 295 | u32 blocksize = 1 << inode->i_blkbits; |
| 296 | pgoff_t idx0 = page0 ? page0->index : -1; |
| 297 | loff_t vbo_clst = vbo & sbi->cluster_mask_inv; |
| 298 | loff_t end = ntfs_up_cluster(sbi, vbo + bytes); |
| 299 | pgoff_t idx = vbo_clst >> PAGE_SHIFT; |
| 300 | u32 from = vbo_clst & (PAGE_SIZE - 1); |
| 301 | pgoff_t idx_end = (end + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 302 | loff_t page_off; |
| 303 | u32 to; |
| 304 | bool partial; |
| 305 | struct page *page; |
| 306 | |
| 307 | for (; idx < idx_end; idx += 1, from = 0) { |
| 308 | page = idx == idx0 ? page0 : grab_cache_page(mapping, idx); |
| 309 | |
| 310 | if (!page) |
| 311 | continue; |
| 312 | |
| 313 | page_off = (loff_t)idx << PAGE_SHIFT; |
| 314 | to = (page_off + PAGE_SIZE) > end ? (end - page_off) |
| 315 | : PAGE_SIZE; |
| 316 | partial = false; |
| 317 | |
| 318 | if ((from || PAGE_SIZE != to) && |
| 319 | likely(!page_has_buffers(page))) { |
| 320 | create_empty_buffers(page, blocksize, 0); |
| 321 | } |
| 322 | |
| 323 | if (page_has_buffers(page)) { |
| 324 | struct buffer_head *head, *bh; |
| 325 | u32 bh_off = 0; |
| 326 | |
| 327 | bh = head = page_buffers(page); |
| 328 | do { |
| 329 | u32 bh_next = bh_off + blocksize; |
| 330 | |
| 331 | if (from <= bh_off && bh_next <= to) { |
| 332 | set_buffer_uptodate(bh); |
| 333 | mark_buffer_dirty(bh); |
| 334 | } else if (!buffer_uptodate(bh)) { |
| 335 | partial = true; |
| 336 | } |
| 337 | bh_off = bh_next; |
| 338 | } while (head != (bh = bh->b_this_page)); |
| 339 | } |
| 340 | |
| 341 | zero_user_segment(page, from, to); |
| 342 | |
| 343 | if (!partial) { |
| 344 | if (!PageUptodate(page)) |
| 345 | SetPageUptodate(page); |
| 346 | set_page_dirty(page); |
| 347 | } |
| 348 | |
| 349 | if (idx != idx0) { |
| 350 | unlock_page(page); |
| 351 | put_page(page); |
| 352 | } |
| 353 | cond_resched(); |
| 354 | } |
| 355 | mark_inode_dirty(inode); |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * file_operations::mmap |
| 360 | */ |
| 361 | static int ntfs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 362 | { |
| 363 | struct address_space *mapping = file->f_mapping; |
| 364 | struct inode *inode = mapping->host; |
| 365 | struct ntfs_inode *ni = ntfs_i(inode); |
| 366 | u64 from = ((u64)vma->vm_pgoff << PAGE_SHIFT); |
| 367 | bool rw = vma->vm_flags & VM_WRITE; |
| 368 | int err; |
| 369 | |
| 370 | if (is_encrypted(ni)) { |
| 371 | ntfs_inode_warn(inode, "mmap encrypted not supported"); |
| 372 | return -EOPNOTSUPP; |
| 373 | } |
| 374 | |
| 375 | if (is_dedup(ni)) { |
| 376 | ntfs_inode_warn(inode, "mmap deduplicated not supported"); |
| 377 | return -EOPNOTSUPP; |
| 378 | } |
| 379 | |
| 380 | if (is_compressed(ni) && rw) { |
| 381 | ntfs_inode_warn(inode, "mmap(write) compressed not supported"); |
| 382 | return -EOPNOTSUPP; |
| 383 | } |
| 384 | |
| 385 | if (rw) { |
| 386 | u64 to = min_t(loff_t, i_size_read(inode), |
| 387 | from + vma->vm_end - vma->vm_start); |
| 388 | |
| 389 | if (is_sparsed(ni)) { |
| 390 | /* allocate clusters for rw map */ |
| 391 | struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; |
| 392 | CLST lcn, len; |
| 393 | CLST vcn = from >> sbi->cluster_bits; |
| 394 | CLST end = bytes_to_cluster(sbi, to); |
| 395 | bool new; |
| 396 | |
| 397 | for (; vcn < end; vcn += len) { |
| 398 | err = attr_data_get_block(ni, vcn, 1, &lcn, |
| 399 | &len, &new); |
| 400 | if (err) |
| 401 | goto out; |
| 402 | |
| 403 | if (!new) |
| 404 | continue; |
| 405 | ntfs_sparse_cluster(inode, NULL, vcn, 1); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if (ni->i_valid < to) { |
| 410 | if (!inode_trylock(inode)) { |
| 411 | err = -EAGAIN; |
| 412 | goto out; |
| 413 | } |
| 414 | err = ntfs_extend_initialized_size(file, ni, |
| 415 | ni->i_valid, to); |
| 416 | inode_unlock(inode); |
| 417 | if (err) |
| 418 | goto out; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | err = generic_file_mmap(file, vma); |
| 423 | out: |
| 424 | return err; |
| 425 | } |
| 426 | |
| 427 | static int ntfs_extend(struct inode *inode, loff_t pos, size_t count, |
| 428 | struct file *file) |
| 429 | { |
| 430 | struct ntfs_inode *ni = ntfs_i(inode); |
| 431 | struct address_space *mapping = inode->i_mapping; |
| 432 | loff_t end = pos + count; |
| 433 | bool extend_init = file && pos > ni->i_valid; |
| 434 | int err; |
| 435 | |
| 436 | if (end <= inode->i_size && !extend_init) |
| 437 | return 0; |
| 438 | |
| 439 | /*mark rw ntfs as dirty. it will be cleared at umount*/ |
| 440 | ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY); |
| 441 | |
| 442 | if (end > inode->i_size) { |
| 443 | err = ntfs_set_size(inode, end); |
| 444 | if (err) |
| 445 | goto out; |
| 446 | inode->i_size = end; |
| 447 | } |
| 448 | |
| 449 | if (extend_init && !is_compressed(ni)) { |
| 450 | err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos); |
| 451 | if (err) |
| 452 | goto out; |
| 453 | } else { |
| 454 | err = 0; |
| 455 | } |
| 456 | |
| 457 | inode->i_ctime = inode->i_mtime = current_time(inode); |
| 458 | mark_inode_dirty(inode); |
| 459 | |
| 460 | if (IS_SYNC(inode)) { |
| 461 | int err2; |
| 462 | |
| 463 | err = filemap_fdatawrite_range(mapping, pos, end - 1); |
| 464 | err2 = sync_mapping_buffers(mapping); |
| 465 | if (!err) |
| 466 | err = err2; |
| 467 | err2 = write_inode_now(inode, 1); |
| 468 | if (!err) |
| 469 | err = err2; |
| 470 | if (!err) |
| 471 | err = filemap_fdatawait_range(mapping, pos, end - 1); |
| 472 | } |
| 473 | |
| 474 | out: |
| 475 | return err; |
| 476 | } |
| 477 | |
| 478 | static int ntfs_truncate(struct inode *inode, loff_t new_size) |
| 479 | { |
| 480 | struct super_block *sb = inode->i_sb; |
| 481 | struct ntfs_inode *ni = ntfs_i(inode); |
| 482 | int err, dirty = 0; |
| 483 | u64 new_valid; |
| 484 | |
| 485 | if (!S_ISREG(inode->i_mode)) |
| 486 | return 0; |
| 487 | |
| 488 | if (is_compressed(ni)) { |
| 489 | if (ni->i_valid > new_size) |
| 490 | ni->i_valid = new_size; |
| 491 | } else { |
| 492 | err = block_truncate_page(inode->i_mapping, new_size, |
| 493 | ntfs_get_block); |
| 494 | if (err) |
| 495 | return err; |
| 496 | } |
| 497 | |
| 498 | new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size)); |
| 499 | |
| 500 | ni_lock(ni); |
| 501 | |
| 502 | truncate_setsize(inode, new_size); |
| 503 | |
| 504 | down_write(&ni->file.run_lock); |
| 505 | err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size, |
| 506 | &new_valid, true, NULL); |
| 507 | up_write(&ni->file.run_lock); |
| 508 | |
| 509 | if (new_valid < ni->i_valid) |
| 510 | ni->i_valid = new_valid; |
| 511 | |
| 512 | ni_unlock(ni); |
| 513 | |
| 514 | ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE; |
| 515 | inode->i_ctime = inode->i_mtime = current_time(inode); |
| 516 | if (!IS_DIRSYNC(inode)) { |
| 517 | dirty = 1; |
| 518 | } else { |
| 519 | err = ntfs_sync_inode(inode); |
| 520 | if (err) |
| 521 | return err; |
| 522 | } |
| 523 | |
| 524 | if (dirty) |
| 525 | mark_inode_dirty(inode); |
| 526 | |
| 527 | /*ntfs_flush_inodes(inode->i_sb, inode, NULL);*/ |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Preallocate space for a file. This implements ntfs's fallocate file |
| 534 | * operation, which gets called from sys_fallocate system call. User |
| 535 | * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set |
| 536 | * we just allocate clusters without zeroing them out. Otherwise we |
| 537 | * allocate and zero out clusters via an expanding truncate. |
| 538 | */ |
| 539 | static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len) |
| 540 | { |
| 541 | struct inode *inode = file->f_mapping->host; |
| 542 | struct super_block *sb = inode->i_sb; |
| 543 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 544 | struct ntfs_inode *ni = ntfs_i(inode); |
| 545 | loff_t end = vbo + len; |
| 546 | loff_t vbo_down = round_down(vbo, PAGE_SIZE); |
| 547 | loff_t i_size; |
| 548 | int err; |
| 549 | |
| 550 | /* No support for dir */ |
| 551 | if (!S_ISREG(inode->i_mode)) |
| 552 | return -EOPNOTSUPP; |
| 553 | |
| 554 | /* Return error if mode is not supported */ |
| 555 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | |
| 556 | FALLOC_FL_COLLAPSE_RANGE)) { |
| 557 | ntfs_inode_warn(inode, "fallocate(0x%x) is not supported", |
| 558 | mode); |
| 559 | return -EOPNOTSUPP; |
| 560 | } |
| 561 | |
| 562 | ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); |
| 563 | |
| 564 | inode_lock(inode); |
| 565 | i_size = inode->i_size; |
| 566 | |
| 567 | if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { |
| 568 | /* should never be here, see ntfs_file_open*/ |
| 569 | err = -EOPNOTSUPP; |
| 570 | goto out; |
| 571 | } |
| 572 | |
| 573 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
| 574 | u32 frame_size; |
| 575 | loff_t mask, vbo_a, end_a, tmp; |
| 576 | |
| 577 | if (!(mode & FALLOC_FL_KEEP_SIZE)) { |
| 578 | err = -EINVAL; |
| 579 | goto out; |
| 580 | } |
| 581 | |
| 582 | err = filemap_write_and_wait_range(inode->i_mapping, vbo, |
| 583 | end - 1); |
| 584 | if (err) |
| 585 | goto out; |
| 586 | |
| 587 | err = filemap_write_and_wait_range(inode->i_mapping, end, |
| 588 | LLONG_MAX); |
| 589 | if (err) |
| 590 | goto out; |
| 591 | |
| 592 | inode_dio_wait(inode); |
| 593 | |
| 594 | truncate_pagecache(inode, vbo_down); |
| 595 | |
| 596 | if (!is_sparsed(ni) && !is_compressed(ni)) { |
| 597 | /* normal file */ |
| 598 | err = ntfs_zero_range(inode, vbo, end); |
| 599 | goto out; |
| 600 | } |
| 601 | |
| 602 | ni_lock(ni); |
| 603 | err = attr_punch_hole(ni, vbo, len, &frame_size); |
| 604 | ni_unlock(ni); |
| 605 | if (err != E_NTFS_NOTALIGNED) |
| 606 | goto out; |
| 607 | |
| 608 | /* process not aligned punch */ |
| 609 | mask = frame_size - 1; |
| 610 | vbo_a = (vbo + mask) & ~mask; |
| 611 | end_a = end & ~mask; |
| 612 | |
| 613 | tmp = min(vbo_a, end); |
| 614 | if (tmp > vbo) { |
| 615 | err = ntfs_zero_range(inode, vbo, tmp); |
| 616 | if (err) |
| 617 | goto out; |
| 618 | } |
| 619 | |
| 620 | if (vbo < end_a && end_a < end) { |
| 621 | err = ntfs_zero_range(inode, end_a, end); |
| 622 | if (err) |
| 623 | goto out; |
| 624 | } |
| 625 | |
| 626 | /* Aligned punch_hole */ |
| 627 | if (end_a > vbo_a) { |
| 628 | ni_lock(ni); |
| 629 | err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL); |
| 630 | ni_unlock(ni); |
| 631 | } |
| 632 | } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { |
| 633 | if (mode & ~FALLOC_FL_COLLAPSE_RANGE) { |
| 634 | err = -EINVAL; |
| 635 | goto out; |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Write tail of the last page before removed range since |
| 640 | * it will get removed from the page cache below. |
| 641 | */ |
| 642 | err = filemap_write_and_wait_range(inode->i_mapping, vbo_down, |
| 643 | vbo); |
| 644 | if (err) |
| 645 | goto out; |
| 646 | |
| 647 | /* |
| 648 | * Write data that will be shifted to preserve them |
| 649 | * when discarding page cache below |
| 650 | */ |
| 651 | err = filemap_write_and_wait_range(inode->i_mapping, end, |
| 652 | LLONG_MAX); |
| 653 | if (err) |
| 654 | goto out; |
| 655 | |
| 656 | /* Wait for existing dio to complete */ |
| 657 | inode_dio_wait(inode); |
| 658 | |
| 659 | truncate_pagecache(inode, vbo_down); |
| 660 | |
| 661 | ni_lock(ni); |
| 662 | err = attr_collapse_range(ni, vbo, len); |
| 663 | ni_unlock(ni); |
| 664 | } else { |
| 665 | /* |
| 666 | * normal file: allocate clusters, do not change 'valid' size |
| 667 | */ |
| 668 | err = ntfs_set_size(inode, max(end, i_size)); |
| 669 | if (err) |
| 670 | goto out; |
| 671 | |
| 672 | if (is_sparsed(ni) || is_compressed(ni)) { |
| 673 | CLST vcn_v = ni->i_valid >> sbi->cluster_bits; |
| 674 | CLST vcn = vbo >> sbi->cluster_bits; |
| 675 | CLST cend = bytes_to_cluster(sbi, end); |
| 676 | CLST lcn, clen; |
| 677 | bool new; |
| 678 | |
| 679 | /* |
| 680 | * allocate but not zero new clusters (see below comments) |
| 681 | * this breaks security (one can read unused on-disk areas) |
| 682 | * zeroing these clusters may be too long |
| 683 | * may be we should check here for root rights? |
| 684 | */ |
| 685 | for (; vcn < cend; vcn += clen) { |
| 686 | err = attr_data_get_block(ni, vcn, cend - vcn, |
| 687 | &lcn, &clen, &new); |
| 688 | if (err) |
| 689 | goto out; |
| 690 | if (!new || vcn >= vcn_v) |
| 691 | continue; |
| 692 | |
| 693 | /* |
| 694 | * Unwritten area |
| 695 | * NTFS is not able to store several unwritten areas |
| 696 | * Activate 'ntfs_sparse_cluster' to zero new allocated clusters |
| 697 | * |
| 698 | * Dangerous in case: |
| 699 | * 1G of sparsed clusters + 1 cluster of data => |
| 700 | * valid_size == 1G + 1 cluster |
| 701 | * fallocate(1G) will zero 1G and this can be very long |
| 702 | * xfstest 016/086 will fail without 'ntfs_sparse_cluster' |
| 703 | */ |
| 704 | ntfs_sparse_cluster(inode, NULL, vcn, |
| 705 | min(vcn_v - vcn, clen)); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | if (mode & FALLOC_FL_KEEP_SIZE) { |
| 710 | ni_lock(ni); |
| 711 | /*true - keep preallocated*/ |
| 712 | err = attr_set_size(ni, ATTR_DATA, NULL, 0, |
| 713 | &ni->file.run, i_size, &ni->i_valid, |
| 714 | true, NULL); |
| 715 | ni_unlock(ni); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | out: |
| 720 | if (err == -EFBIG) |
| 721 | err = -ENOSPC; |
| 722 | |
| 723 | if (!err) { |
| 724 | inode->i_ctime = inode->i_mtime = current_time(inode); |
| 725 | mark_inode_dirty(inode); |
| 726 | } |
| 727 | |
| 728 | inode_unlock(inode); |
| 729 | return err; |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * inode_operations::setattr |
| 734 | */ |
| 735 | int ntfs3_setattr(struct user_namespace *mnt_userns, struct dentry *dentry, |
| 736 | struct iattr *attr) |
| 737 | { |
| 738 | struct super_block *sb = dentry->d_sb; |
| 739 | struct ntfs_sb_info *sbi = sb->s_fs_info; |
| 740 | struct inode *inode = d_inode(dentry); |
| 741 | struct ntfs_inode *ni = ntfs_i(inode); |
| 742 | u32 ia_valid = attr->ia_valid; |
| 743 | umode_t mode = inode->i_mode; |
| 744 | int err; |
| 745 | |
| 746 | if (sbi->options.no_acs_rules) { |
| 747 | /* "no access rules" - force any changes of time etc. */ |
| 748 | attr->ia_valid |= ATTR_FORCE; |
| 749 | /* and disable for editing some attributes */ |
| 750 | attr->ia_valid &= ~(ATTR_UID | ATTR_GID | ATTR_MODE); |
| 751 | ia_valid = attr->ia_valid; |
| 752 | } |
| 753 | |
| 754 | err = setattr_prepare(mnt_userns, dentry, attr); |
| 755 | if (err) |
| 756 | goto out; |
| 757 | |
| 758 | if (ia_valid & ATTR_SIZE) { |
| 759 | loff_t oldsize = inode->i_size; |
| 760 | |
| 761 | if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { |
| 762 | /* should never be here, see ntfs_file_open*/ |
| 763 | err = -EOPNOTSUPP; |
| 764 | goto out; |
| 765 | } |
| 766 | inode_dio_wait(inode); |
| 767 | |
| 768 | if (attr->ia_size < oldsize) |
| 769 | err = ntfs_truncate(inode, attr->ia_size); |
| 770 | else if (attr->ia_size > oldsize) |
| 771 | err = ntfs_extend(inode, attr->ia_size, 0, NULL); |
| 772 | |
| 773 | if (err) |
| 774 | goto out; |
| 775 | |
| 776 | ni->ni_flags |= NI_FLAG_UPDATE_PARENT; |
| 777 | } |
| 778 | |
| 779 | setattr_copy(mnt_userns, inode, attr); |
| 780 | |
| 781 | if (mode != inode->i_mode) { |
| 782 | err = ntfs_acl_chmod(mnt_userns, inode); |
| 783 | if (err) |
| 784 | goto out; |
| 785 | |
| 786 | /* linux 'w' -> windows 'ro' */ |
| 787 | if (0222 & inode->i_mode) |
| 788 | ni->std_fa &= ~FILE_ATTRIBUTE_READONLY; |
| 789 | else |
| 790 | ni->std_fa |= FILE_ATTRIBUTE_READONLY; |
| 791 | } |
| 792 | |
| 793 | if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) |
| 794 | ntfs_save_wsl_perm(inode); |
| 795 | mark_inode_dirty(inode); |
| 796 | out: |
| 797 | return err; |
| 798 | } |
| 799 | |
| 800 | static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 801 | { |
| 802 | ssize_t err; |
| 803 | size_t count = iov_iter_count(iter); |
| 804 | struct file *file = iocb->ki_filp; |
| 805 | struct inode *inode = file->f_mapping->host; |
| 806 | struct ntfs_inode *ni = ntfs_i(inode); |
| 807 | |
| 808 | if (is_encrypted(ni)) { |
| 809 | ntfs_inode_warn(inode, "encrypted i/o not supported"); |
| 810 | return -EOPNOTSUPP; |
| 811 | } |
| 812 | |
| 813 | if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) { |
| 814 | ntfs_inode_warn(inode, "direct i/o + compressed not supported"); |
| 815 | return -EOPNOTSUPP; |
| 816 | } |
| 817 | |
| 818 | #ifndef CONFIG_NTFS3_LZX_XPRESS |
| 819 | if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) { |
| 820 | ntfs_inode_warn( |
| 821 | inode, |
| 822 | "activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files"); |
| 823 | return -EOPNOTSUPP; |
| 824 | } |
| 825 | #endif |
| 826 | |
| 827 | if (is_dedup(ni)) { |
| 828 | ntfs_inode_warn(inode, "read deduplicated not supported"); |
| 829 | return -EOPNOTSUPP; |
| 830 | } |
| 831 | |
| 832 | err = count ? generic_file_read_iter(iocb, iter) : 0; |
| 833 | |
| 834 | return err; |
| 835 | } |
| 836 | |
| 837 | /* returns array of locked pages */ |
| 838 | static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index, |
| 839 | struct page **pages, u32 pages_per_frame, |
| 840 | bool *frame_uptodate) |
| 841 | { |
| 842 | gfp_t gfp_mask = mapping_gfp_mask(mapping); |
| 843 | u32 npages; |
| 844 | |
| 845 | *frame_uptodate = true; |
| 846 | |
| 847 | for (npages = 0; npages < pages_per_frame; npages++, index++) { |
| 848 | struct page *page; |
| 849 | |
| 850 | page = find_or_create_page(mapping, index, gfp_mask); |
| 851 | if (!page) { |
| 852 | while (npages--) { |
| 853 | page = pages[npages]; |
| 854 | unlock_page(page); |
| 855 | put_page(page); |
| 856 | } |
| 857 | |
| 858 | return -ENOMEM; |
| 859 | } |
| 860 | |
| 861 | if (!PageUptodate(page)) |
| 862 | *frame_uptodate = false; |
| 863 | |
| 864 | pages[npages] = page; |
| 865 | } |
| 866 | |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | /*helper for ntfs_file_write_iter (compressed files)*/ |
| 871 | static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from) |
| 872 | { |
| 873 | int err; |
| 874 | struct file *file = iocb->ki_filp; |
| 875 | size_t count = iov_iter_count(from); |
| 876 | loff_t pos = iocb->ki_pos; |
| 877 | struct inode *inode = file_inode(file); |
| 878 | loff_t i_size = inode->i_size; |
| 879 | struct address_space *mapping = inode->i_mapping; |
| 880 | struct ntfs_inode *ni = ntfs_i(inode); |
| 881 | u64 valid = ni->i_valid; |
| 882 | struct ntfs_sb_info *sbi = ni->mi.sbi; |
| 883 | struct page *page, **pages = NULL; |
| 884 | size_t written = 0; |
| 885 | u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits; |
| 886 | u32 frame_size = 1u << frame_bits; |
| 887 | u32 pages_per_frame = frame_size >> PAGE_SHIFT; |
| 888 | u32 ip, off; |
| 889 | CLST frame; |
| 890 | u64 frame_vbo; |
| 891 | pgoff_t index; |
| 892 | bool frame_uptodate; |
| 893 | |
| 894 | if (frame_size < PAGE_SIZE) { |
| 895 | /* |
| 896 | * frame_size == 8K if cluster 512 |
| 897 | * frame_size == 64K if cluster 4096 |
| 898 | */ |
| 899 | ntfs_inode_warn(inode, "page size is bigger than frame size"); |
| 900 | return -EOPNOTSUPP; |
| 901 | } |
| 902 | |
Kari Argillander | 345482b | 2021-08-24 21:37:08 +0300 | [diff] [blame^] | 903 | pages = kmalloc_array(pages_per_frame, sizeof(struct page *), GFP_NOFS); |
Konstantin Komarov | 4342306 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 904 | if (!pages) |
| 905 | return -ENOMEM; |
| 906 | |
| 907 | current->backing_dev_info = inode_to_bdi(inode); |
| 908 | err = file_remove_privs(file); |
| 909 | if (err) |
| 910 | goto out; |
| 911 | |
| 912 | err = file_update_time(file); |
| 913 | if (err) |
| 914 | goto out; |
| 915 | |
| 916 | /* zero range [valid : pos) */ |
| 917 | while (valid < pos) { |
| 918 | CLST lcn, clen; |
| 919 | |
| 920 | frame = valid >> frame_bits; |
| 921 | frame_vbo = valid & ~(frame_size - 1); |
| 922 | off = valid & (frame_size - 1); |
| 923 | |
| 924 | err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 0, &lcn, |
| 925 | &clen, NULL); |
| 926 | if (err) |
| 927 | goto out; |
| 928 | |
| 929 | if (lcn == SPARSE_LCN) { |
| 930 | ni->i_valid = valid = |
| 931 | frame_vbo + ((u64)clen << sbi->cluster_bits); |
| 932 | continue; |
| 933 | } |
| 934 | |
| 935 | /* Load full frame */ |
| 936 | err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT, |
| 937 | pages, pages_per_frame, |
| 938 | &frame_uptodate); |
| 939 | if (err) |
| 940 | goto out; |
| 941 | |
| 942 | if (!frame_uptodate && off) { |
| 943 | err = ni_read_frame(ni, frame_vbo, pages, |
| 944 | pages_per_frame); |
| 945 | if (err) { |
| 946 | for (ip = 0; ip < pages_per_frame; ip++) { |
| 947 | page = pages[ip]; |
| 948 | unlock_page(page); |
| 949 | put_page(page); |
| 950 | } |
| 951 | goto out; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | ip = off >> PAGE_SHIFT; |
| 956 | off = offset_in_page(valid); |
| 957 | for (; ip < pages_per_frame; ip++, off = 0) { |
| 958 | page = pages[ip]; |
| 959 | zero_user_segment(page, off, PAGE_SIZE); |
| 960 | flush_dcache_page(page); |
| 961 | SetPageUptodate(page); |
| 962 | } |
| 963 | |
| 964 | ni_lock(ni); |
| 965 | err = ni_write_frame(ni, pages, pages_per_frame); |
| 966 | ni_unlock(ni); |
| 967 | |
| 968 | for (ip = 0; ip < pages_per_frame; ip++) { |
| 969 | page = pages[ip]; |
| 970 | SetPageUptodate(page); |
| 971 | unlock_page(page); |
| 972 | put_page(page); |
| 973 | } |
| 974 | |
| 975 | if (err) |
| 976 | goto out; |
| 977 | |
| 978 | ni->i_valid = valid = frame_vbo + frame_size; |
| 979 | } |
| 980 | |
| 981 | /* copy user data [pos : pos + count) */ |
| 982 | while (count) { |
| 983 | size_t copied, bytes; |
| 984 | |
| 985 | off = pos & (frame_size - 1); |
| 986 | bytes = frame_size - off; |
| 987 | if (bytes > count) |
| 988 | bytes = count; |
| 989 | |
| 990 | frame = pos >> frame_bits; |
| 991 | frame_vbo = pos & ~(frame_size - 1); |
| 992 | index = frame_vbo >> PAGE_SHIFT; |
| 993 | |
| 994 | if (unlikely(iov_iter_fault_in_readable(from, bytes))) { |
| 995 | err = -EFAULT; |
| 996 | goto out; |
| 997 | } |
| 998 | |
| 999 | /* Load full frame */ |
| 1000 | err = ntfs_get_frame_pages(mapping, index, pages, |
| 1001 | pages_per_frame, &frame_uptodate); |
| 1002 | if (err) |
| 1003 | goto out; |
| 1004 | |
| 1005 | if (!frame_uptodate) { |
| 1006 | loff_t to = pos + bytes; |
| 1007 | |
| 1008 | if (off || (to < i_size && (to & (frame_size - 1)))) { |
| 1009 | err = ni_read_frame(ni, frame_vbo, pages, |
| 1010 | pages_per_frame); |
| 1011 | if (err) { |
| 1012 | for (ip = 0; ip < pages_per_frame; |
| 1013 | ip++) { |
| 1014 | page = pages[ip]; |
| 1015 | unlock_page(page); |
| 1016 | put_page(page); |
| 1017 | } |
| 1018 | goto out; |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | WARN_ON(!bytes); |
| 1024 | copied = 0; |
| 1025 | ip = off >> PAGE_SHIFT; |
| 1026 | off = offset_in_page(pos); |
| 1027 | |
| 1028 | /* copy user data to pages */ |
| 1029 | for (;;) { |
| 1030 | size_t cp, tail = PAGE_SIZE - off; |
| 1031 | |
| 1032 | page = pages[ip]; |
| 1033 | cp = copy_page_from_iter_atomic(page, off, |
| 1034 | min(tail, bytes), from); |
| 1035 | flush_dcache_page(page); |
| 1036 | |
| 1037 | copied += cp; |
| 1038 | bytes -= cp; |
| 1039 | if (!bytes || !cp) |
| 1040 | break; |
| 1041 | |
| 1042 | if (cp < tail) { |
| 1043 | off += cp; |
| 1044 | } else { |
| 1045 | ip++; |
| 1046 | off = 0; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | ni_lock(ni); |
| 1051 | err = ni_write_frame(ni, pages, pages_per_frame); |
| 1052 | ni_unlock(ni); |
| 1053 | |
| 1054 | for (ip = 0; ip < pages_per_frame; ip++) { |
| 1055 | page = pages[ip]; |
| 1056 | ClearPageDirty(page); |
| 1057 | SetPageUptodate(page); |
| 1058 | unlock_page(page); |
| 1059 | put_page(page); |
| 1060 | } |
| 1061 | |
| 1062 | if (err) |
| 1063 | goto out; |
| 1064 | |
| 1065 | /* |
| 1066 | * We can loop for a long time in here. Be nice and allow |
| 1067 | * us to schedule out to avoid softlocking if preempt |
| 1068 | * is disabled. |
| 1069 | */ |
| 1070 | cond_resched(); |
| 1071 | |
| 1072 | pos += copied; |
| 1073 | written += copied; |
| 1074 | |
| 1075 | count = iov_iter_count(from); |
| 1076 | } |
| 1077 | |
| 1078 | out: |
Kari Argillander | 195c52b | 2021-08-24 21:37:07 +0300 | [diff] [blame] | 1079 | kfree(pages); |
Konstantin Komarov | 4342306 | 2021-08-13 17:21:29 +0300 | [diff] [blame] | 1080 | |
| 1081 | current->backing_dev_info = NULL; |
| 1082 | |
| 1083 | if (err < 0) |
| 1084 | return err; |
| 1085 | |
| 1086 | iocb->ki_pos += written; |
| 1087 | if (iocb->ki_pos > ni->i_valid) |
| 1088 | ni->i_valid = iocb->ki_pos; |
| 1089 | |
| 1090 | return written; |
| 1091 | } |
| 1092 | |
| 1093 | /* |
| 1094 | * file_operations::write_iter |
| 1095 | */ |
| 1096 | static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) |
| 1097 | { |
| 1098 | struct file *file = iocb->ki_filp; |
| 1099 | struct address_space *mapping = file->f_mapping; |
| 1100 | struct inode *inode = mapping->host; |
| 1101 | ssize_t ret; |
| 1102 | struct ntfs_inode *ni = ntfs_i(inode); |
| 1103 | |
| 1104 | if (is_encrypted(ni)) { |
| 1105 | ntfs_inode_warn(inode, "encrypted i/o not supported"); |
| 1106 | return -EOPNOTSUPP; |
| 1107 | } |
| 1108 | |
| 1109 | if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) { |
| 1110 | ntfs_inode_warn(inode, "direct i/o + compressed not supported"); |
| 1111 | return -EOPNOTSUPP; |
| 1112 | } |
| 1113 | |
| 1114 | if (is_dedup(ni)) { |
| 1115 | ntfs_inode_warn(inode, "write into deduplicated not supported"); |
| 1116 | return -EOPNOTSUPP; |
| 1117 | } |
| 1118 | |
| 1119 | if (!inode_trylock(inode)) { |
| 1120 | if (iocb->ki_flags & IOCB_NOWAIT) |
| 1121 | return -EAGAIN; |
| 1122 | inode_lock(inode); |
| 1123 | } |
| 1124 | |
| 1125 | ret = generic_write_checks(iocb, from); |
| 1126 | if (ret <= 0) |
| 1127 | goto out; |
| 1128 | |
| 1129 | if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) { |
| 1130 | /* should never be here, see ntfs_file_open*/ |
| 1131 | ret = -EOPNOTSUPP; |
| 1132 | goto out; |
| 1133 | } |
| 1134 | |
| 1135 | ret = ntfs_extend(inode, iocb->ki_pos, ret, file); |
| 1136 | if (ret) |
| 1137 | goto out; |
| 1138 | |
| 1139 | ret = is_compressed(ni) ? ntfs_compress_write(iocb, from) |
| 1140 | : __generic_file_write_iter(iocb, from); |
| 1141 | |
| 1142 | out: |
| 1143 | inode_unlock(inode); |
| 1144 | |
| 1145 | if (ret > 0) |
| 1146 | ret = generic_write_sync(iocb, ret); |
| 1147 | |
| 1148 | return ret; |
| 1149 | } |
| 1150 | |
| 1151 | /* |
| 1152 | * file_operations::open |
| 1153 | */ |
| 1154 | int ntfs_file_open(struct inode *inode, struct file *file) |
| 1155 | { |
| 1156 | struct ntfs_inode *ni = ntfs_i(inode); |
| 1157 | |
| 1158 | if (unlikely((is_compressed(ni) || is_encrypted(ni)) && |
| 1159 | (file->f_flags & O_DIRECT))) { |
| 1160 | return -EOPNOTSUPP; |
| 1161 | } |
| 1162 | |
| 1163 | /* Decompress "external compressed" file if opened for rw */ |
| 1164 | if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) && |
| 1165 | (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) { |
| 1166 | #ifdef CONFIG_NTFS3_LZX_XPRESS |
| 1167 | int err = ni_decompress_file(ni); |
| 1168 | |
| 1169 | if (err) |
| 1170 | return err; |
| 1171 | #else |
| 1172 | ntfs_inode_warn( |
| 1173 | inode, |
| 1174 | "activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files"); |
| 1175 | return -EOPNOTSUPP; |
| 1176 | #endif |
| 1177 | } |
| 1178 | |
| 1179 | return generic_file_open(inode, file); |
| 1180 | } |
| 1181 | |
| 1182 | /* |
| 1183 | * file_operations::release |
| 1184 | */ |
| 1185 | static int ntfs_file_release(struct inode *inode, struct file *file) |
| 1186 | { |
| 1187 | struct ntfs_inode *ni = ntfs_i(inode); |
| 1188 | struct ntfs_sb_info *sbi = ni->mi.sbi; |
| 1189 | int err = 0; |
| 1190 | |
| 1191 | /* if we are the last writer on the inode, drop the block reservation */ |
| 1192 | if (sbi->options.prealloc && ((file->f_mode & FMODE_WRITE) && |
| 1193 | atomic_read(&inode->i_writecount) == 1)) { |
| 1194 | ni_lock(ni); |
| 1195 | down_write(&ni->file.run_lock); |
| 1196 | |
| 1197 | err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, |
| 1198 | inode->i_size, &ni->i_valid, false, NULL); |
| 1199 | |
| 1200 | up_write(&ni->file.run_lock); |
| 1201 | ni_unlock(ni); |
| 1202 | } |
| 1203 | return err; |
| 1204 | } |
| 1205 | |
| 1206 | /* file_operations::fiemap */ |
| 1207 | int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, |
| 1208 | __u64 start, __u64 len) |
| 1209 | { |
| 1210 | int err; |
| 1211 | struct ntfs_inode *ni = ntfs_i(inode); |
| 1212 | |
| 1213 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) |
| 1214 | return -EOPNOTSUPP; |
| 1215 | |
| 1216 | ni_lock(ni); |
| 1217 | |
| 1218 | err = ni_fiemap(ni, fieinfo, start, len); |
| 1219 | |
| 1220 | ni_unlock(ni); |
| 1221 | |
| 1222 | return err; |
| 1223 | } |
| 1224 | |
| 1225 | // clang-format off |
| 1226 | const struct inode_operations ntfs_file_inode_operations = { |
| 1227 | .getattr = ntfs_getattr, |
| 1228 | .setattr = ntfs3_setattr, |
| 1229 | .listxattr = ntfs_listxattr, |
| 1230 | .permission = ntfs_permission, |
| 1231 | .get_acl = ntfs_get_acl, |
| 1232 | .set_acl = ntfs_set_acl, |
| 1233 | .fiemap = ntfs_fiemap, |
| 1234 | }; |
| 1235 | |
| 1236 | const struct file_operations ntfs_file_operations = { |
| 1237 | .llseek = generic_file_llseek, |
| 1238 | .read_iter = ntfs_file_read_iter, |
| 1239 | .write_iter = ntfs_file_write_iter, |
| 1240 | .unlocked_ioctl = ntfs_ioctl, |
| 1241 | #ifdef CONFIG_COMPAT |
| 1242 | .compat_ioctl = ntfs_compat_ioctl, |
| 1243 | #endif |
| 1244 | .splice_read = generic_file_splice_read, |
| 1245 | .mmap = ntfs_file_mmap, |
| 1246 | .open = ntfs_file_open, |
| 1247 | .fsync = generic_file_fsync, |
| 1248 | .splice_write = iter_file_splice_write, |
| 1249 | .fallocate = ntfs_fallocate, |
| 1250 | .release = ntfs_file_release, |
| 1251 | }; |
| 1252 | // clang-format on |