| 1 | |
| 2 | |
| 3 | * fs/f2fs/file.c
|
| 4 | *
|
| 5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd.
|
| 6 | * http://www.samsung.com/
|
| 7 |
|
| 8 | #include <linux/blk-crypto.h>
|
| 9 | #include <linux/fs.h>
|
| 10 | #include <linux/f2fs_fs.h>
|
| 11 | #include <linux/stat.h>
|
| 12 | #include <linux/writeback.h>
|
| 13 | #include <linux/blkdev.h>
|
| 14 | #include <linux/falloc.h>
|
| 15 | #include <linux/filelock.h>
|
| 16 | #include <linux/types.h>
|
| 17 | #include <linux/compat.h>
|
| 18 | #include <linux/uaccess.h>
|
| 19 | #include <linux/mount.h>
|
| 20 | #include <linux/uio.h>
|
| 21 | #include <linux/uuid.h>
|
| 22 | #include <linux/file.h>
|
| 23 | #include <linux/nls.h>
|
| 24 | #include <linux/sched/signal.h>
|
| 25 | #include <linux/fileattr.h>
|
| 26 | #include <linux/fadvise.h>
|
| 27 | #include <linux/iomap.h>
|
| 28 |
|
| 29 | #include "f2fs.h"
|
| 30 | #include "node.h"
|
| 31 | #include "segment.h"
|
| 32 | #include "xattr.h"
|
| 33 | #include "acl.h"
|
| 34 | #include "gc.h"
|
| 35 | #include "iostat.h"
|
| 36 | #include <trace/events/f2fs.h>
|
| 37 | #include <uapi/linux/f2fs.h>
|
| 38 |
|
| 39 | static int fill_zero(struct inode *inode, pgoff_t index,
|
| 40 | loff_t start, loff_t len);
|
| 41 |
|
| 42 | static int do_zero_post_eof_page(struct inode *inode, loff_t new_size)
|
| 43 | {
|
| 44 | loff_t old_size = i_size_read(inode);
|
| 45 | unsigned int offset, len;
|
| 46 | pgoff_t index;
|
| 47 | int err;
|
| 48 |
|
| 49 | offset = old_size & (PAGE_SIZE - 1);
|
| 50 |
|
| 51 | if (!offset)
|
| 52 | return 0;
|
| 53 |
|
| 54 | len = min_t(loff_t, PAGE_SIZE - offset, new_size - old_size);
|
| 55 | index = old_size >> PAGE_SHIFT;
|
| 56 |
|
| 57 | if (f2fs_has_inline_data(inode)) {
|
| 58 |
|
| 59 | if (new_size <= MAX_INLINE_DATA(inode))
|
| 60 | return 0;
|
| 61 | err = f2fs_convert_inline_inode(inode);
|
| 62 | if (err)
|
| 63 | return err;
|
| 64 | }
|
| 65 |
|
| 66 | err = fill_zero(inode, index, offset, len);
|
| 67 | if (err)
|
| 68 | return err;
|
| 69 | return filemap_write_and_wait_range(inode->i_mapping,
|
| 70 | old_size, old_size + len - 1);
|
| 71 | }
|
| 72 |
|
| 73 | static int f2fs_zero_post_eof_page(struct inode *inode,
|
| 74 | loff_t new_size, bool lock, bool writeback)
|
| 75 | {
|
| 76 | loff_t old_size = i_size_read(inode);
|
| 77 | bool strict =
|
| 78 | F2FS_OPTION(F2FS_I_SB(inode)).fsync_mode == FSYNC_MODE_STRICT;
|
| 79 |
|
| 80 | if (old_size >= new_size)
|
| 81 | return 0;
|
| 82 |
|
| 83 | if (!strict && mapping_empty(inode->i_mapping))
|
| 84 | return 0;
|
| 85 |
|
| 86 | if (lock)
|
| 87 | filemap_invalidate_lock(inode->i_mapping);
|
| 88 |
|
| 89 | truncate_inode_pages_range(inode->i_mapping, old_size, new_size - 1);
|
| 90 | if (lock)
|
| 91 | filemap_invalidate_unlock(inode->i_mapping);
|
| 92 |
|
| 93 | if (!writeback || !strict)
|
| 94 | return 0;
|
| 95 | |
| 96 | * In fsync_mode=strict, when we expand an unaligned EOF size, we
|
| 97 | * should zero post EOF data and writeback the data immediately,
|
| 98 | * so that it can avoid exposing stale data after metadata flush
|
| 99 | * and POR.
|
| 100 |
|
| 101 | return do_zero_post_eof_page(inode, new_size);
|
| 102 | }
|
| 103 |
|
| 104 | static vm_fault_t f2fs_filemap_fault(struct vm_fault *vmf)
|
| 105 | {
|
| 106 | struct inode *inode = file_inode(vmf->vma->vm_file);
|
| 107 | vm_flags_t flags = vmf->vma->vm_flags;
|
| 108 | vm_fault_t ret;
|
| 109 |
|
| 110 | ret = filemap_fault(vmf);
|
| 111 | if (ret & VM_FAULT_LOCKED)
|
| 112 | f2fs_update_iostat(F2FS_I_SB(inode), inode,
|
| 113 | APP_MAPPED_READ_IO, F2FS_BLKSIZE);
|
| 114 |
|
| 115 | trace_f2fs_filemap_fault(inode, vmf->pgoff, flags, ret);
|
| 116 |
|
| 117 | return ret;
|
| 118 | }
|
| 119 |
|
| 120 | static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)
|
| 121 | {
|
| 122 | struct folio *folio = page_folio(vmf->page);
|
| 123 | struct inode *inode = file_inode(vmf->vma->vm_file);
|
| 124 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 125 | struct dnode_of_data dn;
|
| 126 | bool need_alloc = !f2fs_is_pinned_file(inode);
|
| 127 | int err = 0;
|
| 128 | vm_fault_t ret;
|
| 129 |
|
| 130 | |
| 131 | * We only support large folio on the read case.
|
| 132 | * Don't make any dirty pages.
|
| 133 |
|
| 134 | if (unlikely(IS_IMMUTABLE(inode)) ||
|
| 135 | mapping_large_folio_support(inode->i_mapping)) {
|
| 136 | f2fs_err(sbi, "Not expected: immutable: %d large_folio: %d",
|
| 137 | IS_IMMUTABLE(inode),
|
| 138 | mapping_large_folio_support(inode->i_mapping));
|
| 139 | return VM_FAULT_SIGBUS;
|
| 140 | }
|
| 141 |
|
| 142 | if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
|
| 143 | err = -EIO;
|
| 144 | goto out;
|
| 145 | }
|
| 146 |
|
| 147 | if (unlikely(f2fs_cp_error(sbi))) {
|
| 148 | err = -EIO;
|
| 149 | goto out;
|
| 150 | }
|
| 151 |
|
| 152 | if (!f2fs_is_checkpoint_ready(sbi)) {
|
| 153 | err = -ENOSPC;
|
| 154 | goto out;
|
| 155 | }
|
| 156 |
|
| 157 | err = f2fs_convert_inline_inode(inode);
|
| 158 | if (err)
|
| 159 | goto out;
|
| 160 |
|
| 161 | #ifdef CONFIG_F2FS_FS_COMPRESSION
|
| 162 | if (f2fs_compressed_file(inode)) {
|
| 163 | int ret = f2fs_is_compressed_cluster(inode, folio->index);
|
| 164 |
|
| 165 | if (ret < 0) {
|
| 166 | err = ret;
|
| 167 | goto out;
|
| 168 | } else if (ret) {
|
| 169 | need_alloc = false;
|
| 170 | }
|
| 171 | }
|
| 172 | #endif
|
| 173 |
|
| 174 | if (need_alloc)
|
| 175 | f2fs_balance_fs(sbi, true);
|
| 176 |
|
| 177 | sb_start_pagefault(inode->i_sb);
|
| 178 |
|
| 179 | f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
|
| 180 |
|
| 181 | err = f2fs_zero_post_eof_page(inode,
|
| 182 | (folio->index + 1) << PAGE_SHIFT, true, false);
|
| 183 | if (err)
|
| 184 | goto out_pagefault;
|
| 185 |
|
| 186 | file_update_time(vmf->vma->vm_file);
|
| 187 | filemap_invalidate_lock_shared(inode->i_mapping);
|
| 188 |
|
| 189 | folio_lock(folio);
|
| 190 | if (unlikely(folio->mapping != inode->i_mapping ||
|
| 191 | folio_pos(folio) > i_size_read(inode) ||
|
| 192 | !folio_test_uptodate(folio))) {
|
| 193 | folio_unlock(folio);
|
| 194 | err = -EFAULT;
|
| 195 | goto out_sem;
|
| 196 | }
|
| 197 |
|
| 198 | set_new_dnode(&dn, inode, NULL, NULL, 0);
|
| 199 | if (need_alloc) {
|
| 200 |
|
| 201 | err = f2fs_get_block_locked(&dn, folio->index);
|
| 202 | } else {
|
| 203 | err = f2fs_get_dnode_of_data(&dn, folio->index, LOOKUP_NODE);
|
| 204 | f2fs_put_dnode(&dn);
|
| 205 | if (f2fs_is_pinned_file(inode) &&
|
| 206 | !__is_valid_data_blkaddr(dn.data_blkaddr))
|
| 207 | err = -EIO;
|
| 208 | }
|
| 209 |
|
| 210 | if (err) {
|
| 211 | folio_unlock(folio);
|
| 212 | goto out_sem;
|
| 213 | }
|
| 214 |
|
| 215 | f2fs_folio_wait_writeback(folio, DATA, false, true);
|
| 216 |
|
| 217 |
|
| 218 | f2fs_wait_on_block_writeback(inode, dn.data_blkaddr);
|
| 219 |
|
| 220 | |
| 221 | * check to see if the page is mapped already (no holes)
|
| 222 |
|
| 223 | if (folio_test_mappedtodisk(folio))
|
| 224 | goto out_sem;
|
| 225 |
|
| 226 |
|
| 227 | if (((loff_t)(folio->index + 1) << PAGE_SHIFT) >
|
| 228 | i_size_read(inode)) {
|
| 229 | loff_t offset;
|
| 230 |
|
| 231 | offset = i_size_read(inode) & ~PAGE_MASK;
|
| 232 | folio_zero_segment(folio, offset, folio_size(folio));
|
| 233 | }
|
| 234 | folio_mark_dirty(folio);
|
| 235 |
|
| 236 | f2fs_update_iostat(sbi, inode, APP_MAPPED_IO, F2FS_BLKSIZE);
|
| 237 | f2fs_update_time(sbi, REQ_TIME);
|
| 238 |
|
| 239 | out_sem:
|
| 240 | filemap_invalidate_unlock_shared(inode->i_mapping);
|
| 241 | out_pagefault:
|
| 242 | sb_end_pagefault(inode->i_sb);
|
| 243 | out:
|
| 244 | ret = vmf_fs_error(err);
|
| 245 |
|
| 246 | trace_f2fs_vm_page_mkwrite(inode, folio->index, vmf->vma->vm_flags, ret);
|
| 247 | return ret;
|
| 248 | }
|
| 249 |
|
| 250 | static const struct vm_operations_struct f2fs_file_vm_ops = {
|
| 251 | .fault = f2fs_filemap_fault,
|
| 252 | .map_pages = filemap_map_pages,
|
| 253 | .page_mkwrite = f2fs_vm_page_mkwrite,
|
| 254 | };
|
| 255 |
|
| 256 | static int get_parent_ino(struct inode *inode, nid_t *pino)
|
| 257 | {
|
| 258 | struct dentry *dentry;
|
| 259 |
|
| 260 | |
| 261 | * Make sure to get the non-deleted alias. The alias associated with
|
| 262 | * the open file descriptor being fsync()'ed may be deleted already.
|
| 263 |
|
| 264 | dentry = d_find_alias(inode);
|
| 265 | if (!dentry)
|
| 266 | return 0;
|
| 267 |
|
| 268 | *pino = d_parent_ino(dentry);
|
| 269 | dput(dentry);
|
| 270 | return 1;
|
| 271 | }
|
| 272 |
|
| 273 | static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
|
| 274 | {
|
| 275 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 276 | enum cp_reason_type cp_reason = CP_NO_NEEDED;
|
| 277 |
|
| 278 | if (!S_ISREG(inode->i_mode))
|
| 279 | cp_reason = CP_NON_REGULAR;
|
| 280 | else if (f2fs_compressed_file(inode))
|
| 281 | cp_reason = CP_COMPRESSED;
|
| 282 | else if (inode->i_nlink != 1)
|
| 283 | cp_reason = CP_HARDLINK;
|
| 284 | else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
|
| 285 | cp_reason = CP_SB_NEED_CP;
|
| 286 | else if (file_wrong_pino(inode))
|
| 287 | cp_reason = CP_WRONG_PINO;
|
| 288 | else if (!f2fs_space_for_roll_forward(sbi))
|
| 289 | cp_reason = CP_NO_SPC_ROLL;
|
| 290 | else if (!f2fs_is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
|
| 291 | cp_reason = CP_NODE_NEED_CP;
|
| 292 | else if (test_opt(sbi, FASTBOOT))
|
| 293 | cp_reason = CP_FASTBOOT_MODE;
|
| 294 | else if (F2FS_OPTION(sbi).active_logs == 2)
|
| 295 | cp_reason = CP_SPEC_LOG_NUM;
|
| 296 | else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT &&
|
| 297 | f2fs_need_dentry_mark(sbi, inode->i_ino) &&
|
| 298 | f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
|
| 299 | TRANS_DIR_INO))
|
| 300 | cp_reason = CP_RECOVER_DIR;
|
| 301 | else if (f2fs_exist_written_data(sbi, F2FS_I(inode)->i_pino,
|
| 302 | XATTR_DIR_INO))
|
| 303 | cp_reason = CP_XATTR_DIR;
|
| 304 |
|
| 305 | return cp_reason;
|
| 306 | }
|
| 307 |
|
| 308 | static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
|
| 309 | {
|
| 310 | struct folio *i = filemap_get_folio(NODE_MAPPING(sbi), ino);
|
| 311 | bool ret = false;
|
| 312 |
|
| 313 | if ((!IS_ERR(i) && folio_test_dirty(i)) ||
|
| 314 | f2fs_need_inode_block_update(sbi, ino))
|
| 315 | ret = true;
|
| 316 | f2fs_folio_put(i, false);
|
| 317 | return ret;
|
| 318 | }
|
| 319 |
|
| 320 | static void try_to_fix_pino(struct inode *inode)
|
| 321 | {
|
| 322 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 323 | nid_t pino;
|
| 324 |
|
| 325 | f2fs_down_write(&fi->i_sem);
|
| 326 | if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
|
| 327 | get_parent_ino(inode, &pino)) {
|
| 328 | f2fs_i_pino_write(inode, pino);
|
| 329 | file_got_pino(inode);
|
| 330 | }
|
| 331 | f2fs_up_write(&fi->i_sem);
|
| 332 | }
|
| 333 |
|
| 334 | static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
|
| 335 | int datasync, bool atomic)
|
| 336 | {
|
| 337 | struct inode *inode = file->f_mapping->host;
|
| 338 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 339 | nid_t ino = inode->i_ino;
|
| 340 | int ret = 0;
|
| 341 | enum cp_reason_type cp_reason = 0;
|
| 342 | struct writeback_control wbc = {
|
| 343 | .sync_mode = WB_SYNC_ALL,
|
| 344 | .nr_to_write = LONG_MAX,
|
| 345 | };
|
| 346 | unsigned int seq_id = 0;
|
| 347 |
|
| 348 | if (unlikely(f2fs_readonly(inode->i_sb)))
|
| 349 | return 0;
|
| 350 |
|
| 351 | trace_f2fs_sync_file_enter(inode);
|
| 352 |
|
| 353 | if (S_ISDIR(inode->i_mode))
|
| 354 | goto go_write;
|
| 355 |
|
| 356 |
|
| 357 | if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
|
| 358 | set_inode_flag(inode, FI_NEED_IPU);
|
| 359 | ret = file_write_and_wait_range(file, start, end);
|
| 360 | clear_inode_flag(inode, FI_NEED_IPU);
|
| 361 |
|
| 362 | if (ret || is_sbi_flag_set(sbi, SBI_CP_DISABLED)) {
|
| 363 | trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
|
| 364 | return ret;
|
| 365 | }
|
| 366 |
|
| 367 |
|
| 368 | if (!f2fs_skip_inode_update(inode, datasync)) {
|
| 369 | f2fs_write_inode(inode, NULL);
|
| 370 | goto go_write;
|
| 371 | }
|
| 372 |
|
| 373 | |
| 374 | * if there is no written data, don't waste time to write recovery info.
|
| 375 |
|
| 376 | if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
|
| 377 | !f2fs_exist_written_data(sbi, ino, APPEND_INO)) {
|
| 378 |
|
| 379 |
|
| 380 | if (need_inode_page_update(sbi, ino))
|
| 381 | goto go_write;
|
| 382 |
|
| 383 | if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
|
| 384 | f2fs_exist_written_data(sbi, ino, UPDATE_INO))
|
| 385 | goto flush_out;
|
| 386 | goto out;
|
| 387 | } else {
|
| 388 | |
| 389 | * for OPU case, during fsync(), node can be persisted before
|
| 390 | * data when lower device doesn't support write barrier, result
|
| 391 | * in data corruption after SPO.
|
| 392 | * So for strict fsync mode, force to use atomic write semantics
|
| 393 | * to keep write order in between data/node and last node to
|
| 394 | * avoid potential data corruption.
|
| 395 |
|
| 396 | if (F2FS_OPTION(sbi).fsync_mode ==
|
| 397 | FSYNC_MODE_STRICT && !atomic)
|
| 398 | atomic = true;
|
| 399 | }
|
| 400 | go_write:
|
| 401 | |
| 402 | * Both of fdatasync() and fsync() are able to be recovered from
|
| 403 | * sudden-power-off.
|
| 404 |
|
| 405 | f2fs_down_read(&F2FS_I(inode)->i_sem);
|
| 406 | cp_reason = need_do_checkpoint(inode);
|
| 407 | f2fs_up_read(&F2FS_I(inode)->i_sem);
|
| 408 |
|
| 409 | if (cp_reason) {
|
| 410 |
|
| 411 | ret = f2fs_sync_fs(inode->i_sb, 1);
|
| 412 |
|
| 413 | |
| 414 | * We've secured consistency through sync_fs. Following pino
|
| 415 | * will be used only for fsynced inodes after checkpoint.
|
| 416 |
|
| 417 | try_to_fix_pino(inode);
|
| 418 | clear_inode_flag(inode, FI_APPEND_WRITE);
|
| 419 | clear_inode_flag(inode, FI_UPDATE_WRITE);
|
| 420 | goto out;
|
| 421 | }
|
| 422 | sync_nodes:
|
| 423 | atomic_inc(&sbi->wb_sync_req[NODE]);
|
| 424 | ret = f2fs_fsync_node_pages(sbi, inode, &wbc, atomic, &seq_id);
|
| 425 | atomic_dec(&sbi->wb_sync_req[NODE]);
|
| 426 | if (ret)
|
| 427 | goto out;
|
| 428 |
|
| 429 |
|
| 430 | if (unlikely(f2fs_cp_error(sbi))) {
|
| 431 | ret = -EIO;
|
| 432 | goto out;
|
| 433 | }
|
| 434 |
|
| 435 | if (f2fs_need_inode_block_update(sbi, ino)) {
|
| 436 | f2fs_mark_inode_dirty_sync(inode, true);
|
| 437 | f2fs_write_inode(inode, NULL);
|
| 438 | goto sync_nodes;
|
| 439 | }
|
| 440 |
|
| 441 | |
| 442 | * If it's atomic_write, it's just fine to keep write ordering. So
|
| 443 | * here we don't need to wait for node write completion, since we use
|
| 444 | * node chain which serializes node blocks. If one of node writes are
|
| 445 | * reordered, we can see simply broken chain, resulting in stopping
|
| 446 | * roll-forward recovery. It means we'll recover all or none node blocks
|
| 447 | * given fsync mark.
|
| 448 |
|
| 449 | if (!atomic) {
|
| 450 | ret = f2fs_wait_on_node_pages_writeback(sbi, seq_id);
|
| 451 | if (ret)
|
| 452 | goto out;
|
| 453 | }
|
| 454 |
|
| 455 |
|
| 456 | f2fs_remove_ino_entry(sbi, ino, APPEND_INO);
|
| 457 | clear_inode_flag(inode, FI_APPEND_WRITE);
|
| 458 | flush_out:
|
| 459 | if (!atomic && F2FS_OPTION(sbi).fsync_mode != FSYNC_MODE_NOBARRIER)
|
| 460 | ret = f2fs_issue_flush(sbi, inode->i_ino);
|
| 461 | if (!ret) {
|
| 462 | f2fs_remove_ino_entry(sbi, ino, UPDATE_INO);
|
| 463 | clear_inode_flag(inode, FI_UPDATE_WRITE);
|
| 464 | f2fs_remove_ino_entry(sbi, ino, FLUSH_INO);
|
| 465 | }
|
| 466 | f2fs_update_time(sbi, REQ_TIME);
|
| 467 | out:
|
| 468 | trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
|
| 469 | return ret;
|
| 470 | }
|
| 471 |
|
| 472 | int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
|
| 473 | {
|
| 474 | if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
|
| 475 | return -EIO;
|
| 476 | return f2fs_do_sync_file(file, start, end, datasync, false);
|
| 477 | }
|
| 478 |
|
| 479 | static bool __found_offset(struct address_space *mapping,
|
| 480 | struct dnode_of_data *dn, pgoff_t index, int whence)
|
| 481 | {
|
| 482 | block_t blkaddr = f2fs_data_blkaddr(dn);
|
| 483 | struct inode *inode = mapping->host;
|
| 484 | bool compressed_cluster = false;
|
| 485 |
|
| 486 | if (f2fs_compressed_file(inode)) {
|
| 487 | block_t first_blkaddr = data_blkaddr(dn->inode, dn->node_folio,
|
| 488 | ALIGN_DOWN(dn->ofs_in_node, F2FS_I(inode)->i_cluster_size));
|
| 489 |
|
| 490 | compressed_cluster = first_blkaddr == COMPRESS_ADDR;
|
| 491 | }
|
| 492 |
|
| 493 | switch (whence) {
|
| 494 | case SEEK_DATA:
|
| 495 | if (__is_valid_data_blkaddr(blkaddr))
|
| 496 | return true;
|
| 497 | if (blkaddr == NEW_ADDR &&
|
| 498 | xa_get_mark(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY))
|
| 499 | return true;
|
| 500 | if (compressed_cluster)
|
| 501 | return true;
|
| 502 | break;
|
| 503 | case SEEK_HOLE:
|
| 504 | if (compressed_cluster)
|
| 505 | return false;
|
| 506 | if (blkaddr == NULL_ADDR)
|
| 507 | return true;
|
| 508 | break;
|
| 509 | }
|
| 510 | return false;
|
| 511 | }
|
| 512 |
|
| 513 | static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
|
| 514 | {
|
| 515 | struct inode *inode = file->f_mapping->host;
|
| 516 | loff_t maxbytes = F2FS_BLK_TO_BYTES(max_file_blocks(inode));
|
| 517 | struct dnode_of_data dn;
|
| 518 | pgoff_t pgofs, end_offset;
|
| 519 | loff_t data_ofs = offset;
|
| 520 | loff_t isize;
|
| 521 | int err = 0;
|
| 522 |
|
| 523 | inode_lock_shared(inode);
|
| 524 |
|
| 525 | isize = i_size_read(inode);
|
| 526 | if (offset >= isize)
|
| 527 | goto fail;
|
| 528 |
|
| 529 |
|
| 530 | if (f2fs_has_inline_data(inode)) {
|
| 531 | if (whence == SEEK_HOLE) {
|
| 532 | data_ofs = isize;
|
| 533 | goto found;
|
| 534 | } else if (whence == SEEK_DATA) {
|
| 535 | data_ofs = offset;
|
| 536 | goto found;
|
| 537 | }
|
| 538 | }
|
| 539 |
|
| 540 | pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
|
| 541 |
|
| 542 | for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
|
| 543 | set_new_dnode(&dn, inode, NULL, NULL, 0);
|
| 544 | err = f2fs_get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
|
| 545 | if (err && err != -ENOENT) {
|
| 546 | goto fail;
|
| 547 | } else if (err == -ENOENT) {
|
| 548 |
|
| 549 | if (whence == SEEK_DATA) {
|
| 550 | pgofs = f2fs_get_next_page_offset(&dn, pgofs);
|
| 551 | continue;
|
| 552 | } else {
|
| 553 | goto found;
|
| 554 | }
|
| 555 | }
|
| 556 |
|
| 557 | end_offset = ADDRS_PER_PAGE(dn.node_folio, inode);
|
| 558 |
|
| 559 |
|
| 560 | for (; dn.ofs_in_node < end_offset;
|
| 561 | dn.ofs_in_node++, pgofs++,
|
| 562 | data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
|
| 563 | block_t blkaddr;
|
| 564 |
|
| 565 | blkaddr = f2fs_data_blkaddr(&dn);
|
| 566 |
|
| 567 | if (__is_valid_data_blkaddr(blkaddr) &&
|
| 568 | !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
|
| 569 | blkaddr, DATA_GENERIC_ENHANCE)) {
|
| 570 | f2fs_put_dnode(&dn);
|
| 571 | goto fail;
|
| 572 | }
|
| 573 |
|
| 574 | if (__found_offset(file->f_mapping, &dn,
|
| 575 | pgofs, whence)) {
|
| 576 | f2fs_put_dnode(&dn);
|
| 577 | goto found;
|
| 578 | }
|
| 579 | }
|
| 580 | f2fs_put_dnode(&dn);
|
| 581 | }
|
| 582 |
|
| 583 | if (whence == SEEK_DATA)
|
| 584 | goto fail;
|
| 585 | found:
|
| 586 | if (whence == SEEK_HOLE && data_ofs > isize)
|
| 587 | data_ofs = isize;
|
| 588 | inode_unlock_shared(inode);
|
| 589 | return vfs_setpos(file, data_ofs, maxbytes);
|
| 590 | fail:
|
| 591 | inode_unlock_shared(inode);
|
| 592 | return -ENXIO;
|
| 593 | }
|
| 594 |
|
| 595 | static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
|
| 596 | {
|
| 597 | struct inode *inode = file->f_mapping->host;
|
| 598 | loff_t maxbytes = F2FS_BLK_TO_BYTES(max_file_blocks(inode));
|
| 599 |
|
| 600 | switch (whence) {
|
| 601 | case SEEK_SET:
|
| 602 | case SEEK_CUR:
|
| 603 | case SEEK_END:
|
| 604 | return generic_file_llseek_size(file, offset, whence,
|
| 605 | maxbytes, i_size_read(inode));
|
| 606 | case SEEK_DATA:
|
| 607 | case SEEK_HOLE:
|
| 608 | if (offset < 0)
|
| 609 | return -ENXIO;
|
| 610 | return f2fs_seek_block(file, offset, whence);
|
| 611 | }
|
| 612 |
|
| 613 | return -EINVAL;
|
| 614 | }
|
| 615 |
|
| 616 | static int f2fs_file_mmap_prepare(struct vm_area_desc *desc)
|
| 617 | {
|
| 618 | struct file *file = desc->file;
|
| 619 | struct inode *inode = file_inode(file);
|
| 620 |
|
| 621 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
|
| 622 | return -EIO;
|
| 623 |
|
| 624 | if (!f2fs_is_compress_backend_ready(inode))
|
| 625 | return -EOPNOTSUPP;
|
| 626 |
|
| 627 | file_accessed(file);
|
| 628 | desc->vm_ops = &f2fs_file_vm_ops;
|
| 629 |
|
| 630 | f2fs_down_read(&F2FS_I(inode)->i_sem);
|
| 631 | set_inode_flag(inode, FI_MMAP_FILE);
|
| 632 | f2fs_up_read(&F2FS_I(inode)->i_sem);
|
| 633 |
|
| 634 | return 0;
|
| 635 | }
|
| 636 |
|
| 637 | static int finish_preallocate_blocks(struct inode *inode)
|
| 638 | {
|
| 639 | int ret = 0;
|
| 640 | bool opened;
|
| 641 |
|
| 642 | f2fs_down_read(&F2FS_I(inode)->i_sem);
|
| 643 | opened = is_inode_flag_set(inode, FI_OPENED_FILE);
|
| 644 | f2fs_up_read(&F2FS_I(inode)->i_sem);
|
| 645 | if (opened)
|
| 646 | return 0;
|
| 647 |
|
| 648 | inode_lock(inode);
|
| 649 | if (is_inode_flag_set(inode, FI_OPENED_FILE))
|
| 650 | goto out_unlock;
|
| 651 |
|
| 652 | if (!file_should_truncate(inode))
|
| 653 | goto out_update;
|
| 654 |
|
| 655 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 656 | filemap_invalidate_lock(inode->i_mapping);
|
| 657 |
|
| 658 | truncate_setsize(inode, i_size_read(inode));
|
| 659 | ret = f2fs_truncate(inode);
|
| 660 |
|
| 661 | filemap_invalidate_unlock(inode->i_mapping);
|
| 662 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 663 | if (ret)
|
| 664 | goto out_unlock;
|
| 665 |
|
| 666 | file_dont_truncate(inode);
|
| 667 | out_update:
|
| 668 | f2fs_down_write(&F2FS_I(inode)->i_sem);
|
| 669 | set_inode_flag(inode, FI_OPENED_FILE);
|
| 670 | f2fs_up_write(&F2FS_I(inode)->i_sem);
|
| 671 | out_unlock:
|
| 672 | inode_unlock(inode);
|
| 673 | return ret;
|
| 674 | }
|
| 675 |
|
| 676 | static int f2fs_file_open(struct inode *inode, struct file *filp)
|
| 677 | {
|
| 678 | int err = fscrypt_file_open(inode, filp);
|
| 679 |
|
| 680 | if (err)
|
| 681 | return err;
|
| 682 |
|
| 683 | if (!f2fs_is_compress_backend_ready(inode))
|
| 684 | return -EOPNOTSUPP;
|
| 685 |
|
| 686 | if (mapping_large_folio_support(inode->i_mapping) &&
|
| 687 | filp->f_mode & FMODE_WRITE)
|
| 688 | return -EOPNOTSUPP;
|
| 689 |
|
| 690 | err = fsverity_file_open(inode, filp);
|
| 691 | if (err)
|
| 692 | return err;
|
| 693 |
|
| 694 | filp->f_mode |= FMODE_NOWAIT;
|
| 695 | filp->f_mode |= FMODE_CAN_ODIRECT;
|
| 696 |
|
| 697 | err = dquot_file_open(inode, filp);
|
| 698 | if (err)
|
| 699 | return err;
|
| 700 |
|
| 701 | err = finish_preallocate_blocks(inode);
|
| 702 | if (!err)
|
| 703 | atomic_inc(&F2FS_I(inode)->open_count);
|
| 704 | return err;
|
| 705 | }
|
| 706 |
|
| 707 | void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
|
| 708 | {
|
| 709 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
|
| 710 | int nr_free = 0, ofs = dn->ofs_in_node, len = count;
|
| 711 | __le32 *addr;
|
| 712 | bool compressed_cluster = false;
|
| 713 | int cluster_index = 0, valid_blocks = 0;
|
| 714 | int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
|
| 715 | bool released = !atomic_read(&F2FS_I(dn->inode)->i_compr_blocks);
|
| 716 | block_t blkstart;
|
| 717 | int blklen = 0;
|
| 718 |
|
| 719 | addr = get_dnode_addr(dn->inode, dn->node_folio) + ofs;
|
| 720 | blkstart = le32_to_cpu(*addr);
|
| 721 |
|
| 722 |
|
| 723 | for (; count > 0; count--, addr++, dn->ofs_in_node++, cluster_index++) {
|
| 724 | block_t blkaddr = le32_to_cpu(*addr);
|
| 725 |
|
| 726 | if (f2fs_compressed_file(dn->inode) &&
|
| 727 | !(cluster_index & (cluster_size - 1))) {
|
| 728 | if (compressed_cluster)
|
| 729 | f2fs_i_compr_blocks_update(dn->inode,
|
| 730 | valid_blocks, false);
|
| 731 | compressed_cluster = (blkaddr == COMPRESS_ADDR);
|
| 732 | valid_blocks = 0;
|
| 733 | }
|
| 734 |
|
| 735 | if (blkaddr == NULL_ADDR)
|
| 736 | goto next;
|
| 737 |
|
| 738 | f2fs_set_data_blkaddr(dn, NULL_ADDR);
|
| 739 |
|
| 740 | if (__is_valid_data_blkaddr(blkaddr)) {
|
| 741 | if (time_to_inject(sbi, FAULT_BLKADDR_CONSISTENCE))
|
| 742 | goto next;
|
| 743 | if (!f2fs_is_valid_blkaddr_raw(sbi, blkaddr,
|
| 744 | DATA_GENERIC_ENHANCE))
|
| 745 | goto next;
|
| 746 | if (compressed_cluster)
|
| 747 | valid_blocks++;
|
| 748 | }
|
| 749 |
|
| 750 | if (blkstart + blklen == blkaddr) {
|
| 751 | blklen++;
|
| 752 | } else {
|
| 753 | f2fs_invalidate_blocks(sbi, blkstart, blklen);
|
| 754 | blkstart = blkaddr;
|
| 755 | blklen = 1;
|
| 756 | }
|
| 757 |
|
| 758 | if (!released || blkaddr != COMPRESS_ADDR)
|
| 759 | nr_free++;
|
| 760 |
|
| 761 | continue;
|
| 762 |
|
| 763 | next:
|
| 764 | if (blklen)
|
| 765 | f2fs_invalidate_blocks(sbi, blkstart, blklen);
|
| 766 |
|
| 767 | blkstart = le32_to_cpu(*(addr + 1));
|
| 768 | blklen = 0;
|
| 769 | }
|
| 770 |
|
| 771 | if (blklen)
|
| 772 | f2fs_invalidate_blocks(sbi, blkstart, blklen);
|
| 773 |
|
| 774 | if (compressed_cluster)
|
| 775 | f2fs_i_compr_blocks_update(dn->inode, valid_blocks, false);
|
| 776 |
|
| 777 | if (nr_free) {
|
| 778 | pgoff_t fofs;
|
| 779 | |
| 780 | * once we invalidate valid blkaddr in range [ofs, ofs + count],
|
| 781 | * we will invalidate all blkaddr in the whole range.
|
| 782 |
|
| 783 | fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_folio),
|
| 784 | dn->inode) + ofs;
|
| 785 | f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
|
| 786 | f2fs_update_age_extent_cache_range(dn, fofs, len);
|
| 787 | dec_valid_block_count(sbi, dn->inode, nr_free);
|
| 788 | }
|
| 789 | dn->ofs_in_node = ofs;
|
| 790 |
|
| 791 | f2fs_update_time(sbi, REQ_TIME);
|
| 792 | trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
|
| 793 | dn->ofs_in_node, nr_free);
|
| 794 | }
|
| 795 |
|
| 796 | static int truncate_partial_data_page(struct inode *inode, u64 from,
|
| 797 | bool cache_only)
|
| 798 | {
|
| 799 | loff_t offset = from & (PAGE_SIZE - 1);
|
| 800 | pgoff_t index = from >> PAGE_SHIFT;
|
| 801 | struct address_space *mapping = inode->i_mapping;
|
| 802 | struct folio *folio;
|
| 803 |
|
| 804 | if (!offset && !cache_only)
|
| 805 | return 0;
|
| 806 |
|
| 807 | if (cache_only) {
|
| 808 | folio = filemap_lock_folio(mapping, index);
|
| 809 | if (IS_ERR(folio))
|
| 810 | return 0;
|
| 811 | if (folio_test_uptodate(folio))
|
| 812 | goto truncate_out;
|
| 813 | f2fs_folio_put(folio, true);
|
| 814 | return 0;
|
| 815 | }
|
| 816 |
|
| 817 | folio = f2fs_get_lock_data_folio(inode, index, true);
|
| 818 | if (IS_ERR(folio))
|
| 819 | return PTR_ERR(folio) == -ENOENT ? 0 : PTR_ERR(folio);
|
| 820 | truncate_out:
|
| 821 | f2fs_folio_wait_writeback(folio, DATA, true, true);
|
| 822 | folio_zero_segment(folio, offset, folio_size(folio));
|
| 823 |
|
| 824 |
|
| 825 | f2fs_bug_on(F2FS_I_SB(inode), cache_only && IS_ENCRYPTED(inode));
|
| 826 | if (!cache_only)
|
| 827 | folio_mark_dirty(folio);
|
| 828 | f2fs_folio_put(folio, true);
|
| 829 | return 0;
|
| 830 | }
|
| 831 |
|
| 832 | int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock)
|
| 833 | {
|
| 834 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 835 | struct dnode_of_data dn;
|
| 836 | struct f2fs_lock_context lc;
|
| 837 | pgoff_t free_from;
|
| 838 | int count = 0, err = 0;
|
| 839 | struct folio *ifolio;
|
| 840 | bool truncate_page = false;
|
| 841 |
|
| 842 | trace_f2fs_truncate_blocks_enter(inode, from);
|
| 843 |
|
| 844 | if (IS_DEVICE_ALIASING(inode) && from) {
|
| 845 | err = -EINVAL;
|
| 846 | goto out_err;
|
| 847 | }
|
| 848 |
|
| 849 | free_from = (pgoff_t)F2FS_BLK_ALIGN(from);
|
| 850 |
|
| 851 | if (free_from >= max_file_blocks(inode))
|
| 852 | goto free_partial;
|
| 853 |
|
| 854 | if (lock)
|
| 855 | f2fs_lock_op(sbi, &lc);
|
| 856 |
|
| 857 | ifolio = f2fs_get_inode_folio(sbi, inode->i_ino);
|
| 858 | if (IS_ERR(ifolio)) {
|
| 859 | err = PTR_ERR(ifolio);
|
| 860 | goto out;
|
| 861 | }
|
| 862 |
|
| 863 | if (IS_DEVICE_ALIASING(inode)) {
|
| 864 | struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
|
| 865 | struct extent_info ei;
|
| 866 |
|
| 867 | read_lock(&et->lock);
|
| 868 | ei = et->largest;
|
| 869 | read_unlock(&et->lock);
|
| 870 |
|
| 871 | f2fs_invalidate_blocks(sbi, ei.blk, ei.len);
|
| 872 |
|
| 873 | dec_valid_block_count(sbi, inode, ei.len);
|
| 874 | f2fs_update_time(sbi, REQ_TIME);
|
| 875 |
|
| 876 | f2fs_drop_extent_tree(inode);
|
| 877 |
|
| 878 | f2fs_folio_put(ifolio, true);
|
| 879 | goto out;
|
| 880 | }
|
| 881 |
|
| 882 | if (f2fs_has_inline_data(inode)) {
|
| 883 | f2fs_truncate_inline_inode(inode, ifolio, from);
|
| 884 | f2fs_folio_put(ifolio, true);
|
| 885 | truncate_page = true;
|
| 886 | goto out;
|
| 887 | }
|
| 888 |
|
| 889 | set_new_dnode(&dn, inode, ifolio, NULL, 0);
|
| 890 | err = f2fs_get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
|
| 891 | if (err) {
|
| 892 | if (err == -ENOENT)
|
| 893 | goto free_next;
|
| 894 | goto out;
|
| 895 | }
|
| 896 |
|
| 897 | count = ADDRS_PER_PAGE(dn.node_folio, inode);
|
| 898 |
|
| 899 | count -= dn.ofs_in_node;
|
| 900 | f2fs_bug_on(sbi, count < 0);
|
| 901 |
|
| 902 | if (dn.ofs_in_node || IS_INODE(dn.node_folio)) {
|
| 903 | f2fs_truncate_data_blocks_range(&dn, count);
|
| 904 | free_from += count;
|
| 905 | }
|
| 906 |
|
| 907 | f2fs_put_dnode(&dn);
|
| 908 | free_next:
|
| 909 | err = f2fs_truncate_inode_blocks(inode, free_from);
|
| 910 | out:
|
| 911 | if (lock)
|
| 912 | f2fs_unlock_op(sbi, &lc);
|
| 913 | free_partial:
|
| 914 |
|
| 915 | if (!err)
|
| 916 | err = truncate_partial_data_page(inode, from, truncate_page);
|
| 917 | out_err:
|
| 918 | trace_f2fs_truncate_blocks_exit(inode, err);
|
| 919 | return err;
|
| 920 | }
|
| 921 |
|
| 922 | int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock)
|
| 923 | {
|
| 924 | u64 free_from = from;
|
| 925 | int err;
|
| 926 |
|
| 927 | #ifdef CONFIG_F2FS_FS_COMPRESSION
|
| 928 | |
| 929 | * for compressed file, only support cluster size
|
| 930 | * aligned truncation.
|
| 931 |
|
| 932 | if (f2fs_compressed_file(inode))
|
| 933 | free_from = round_up(from,
|
| 934 | F2FS_I(inode)->i_cluster_size << PAGE_SHIFT);
|
| 935 | #endif
|
| 936 |
|
| 937 | err = f2fs_do_truncate_blocks(inode, free_from, lock);
|
| 938 | if (err)
|
| 939 | return err;
|
| 940 |
|
| 941 | #ifdef CONFIG_F2FS_FS_COMPRESSION
|
| 942 | |
| 943 | * For compressed file, after release compress blocks, don't allow write
|
| 944 | * direct, but we should allow write direct after truncate to zero.
|
| 945 |
|
| 946 | if (f2fs_compressed_file(inode) && !free_from
|
| 947 | && is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
|
| 948 | clear_inode_flag(inode, FI_COMPRESS_RELEASED);
|
| 949 |
|
| 950 | if (from != free_from) {
|
| 951 | err = f2fs_truncate_partial_cluster(inode, from, lock);
|
| 952 | if (err)
|
| 953 | return err;
|
| 954 | }
|
| 955 | #endif
|
| 956 |
|
| 957 | return 0;
|
| 958 | }
|
| 959 |
|
| 960 | int f2fs_truncate(struct inode *inode)
|
| 961 | {
|
| 962 | int err;
|
| 963 |
|
| 964 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
|
| 965 | return -EIO;
|
| 966 |
|
| 967 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
|
| 968 | S_ISLNK(inode->i_mode)))
|
| 969 | return 0;
|
| 970 |
|
| 971 | trace_f2fs_truncate(inode);
|
| 972 |
|
| 973 | if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE))
|
| 974 | return -EIO;
|
| 975 |
|
| 976 | err = f2fs_dquot_initialize(inode);
|
| 977 | if (err)
|
| 978 | return err;
|
| 979 |
|
| 980 |
|
| 981 | if (!f2fs_may_inline_data(inode)) {
|
| 982 | err = f2fs_convert_inline_inode(inode);
|
| 983 | if (err) {
|
| 984 | |
| 985 | * Always truncate page #0 to avoid page cache
|
| 986 | * leak in evict() path.
|
| 987 |
|
| 988 | truncate_inode_pages_range(inode->i_mapping,
|
| 989 | F2FS_BLK_TO_BYTES(0),
|
| 990 | F2FS_BLK_END_BYTES(0));
|
| 991 | return err;
|
| 992 | }
|
| 993 | }
|
| 994 |
|
| 995 | err = f2fs_truncate_blocks(inode, i_size_read(inode), true);
|
| 996 | if (err)
|
| 997 | return err;
|
| 998 |
|
| 999 | inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
|
| 1000 | f2fs_mark_inode_dirty_sync(inode, false);
|
| 1001 | return 0;
|
| 1002 | }
|
| 1003 |
|
| 1004 | static bool f2fs_force_buffered_io(struct inode *inode, int rw)
|
| 1005 | {
|
| 1006 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 1007 |
|
| 1008 | if (fsverity_active(inode))
|
| 1009 | return true;
|
| 1010 | if (f2fs_compressed_file(inode))
|
| 1011 | return true;
|
| 1012 | |
| 1013 | * only force direct read to use buffered IO, for direct write,
|
| 1014 | * it expects inline data conversion before committing IO.
|
| 1015 |
|
| 1016 | if (f2fs_has_inline_data(inode) && rw == READ)
|
| 1017 | return true;
|
| 1018 |
|
| 1019 |
|
| 1020 | if (f2fs_is_multi_device(sbi) && !sbi->aligned_blksize)
|
| 1021 | return true;
|
| 1022 | |
| 1023 | * for blkzoned device, fallback direct IO to buffered IO, so
|
| 1024 | * all IOs can be serialized by log-structured write.
|
| 1025 |
|
| 1026 | if (f2fs_sb_has_blkzoned(sbi) && (rw == WRITE) &&
|
| 1027 | !f2fs_is_pinned_file(inode))
|
| 1028 | return true;
|
| 1029 | if (is_sbi_flag_set(sbi, SBI_CP_DISABLED))
|
| 1030 | return true;
|
| 1031 |
|
| 1032 | return false;
|
| 1033 | }
|
| 1034 |
|
| 1035 | int f2fs_getattr(const struct mnt_idmap *idmap, const struct path *path,
|
| 1036 | struct kstat *stat, u32 request_mask, unsigned int query_flags)
|
| 1037 | {
|
| 1038 | struct inode *inode = d_inode(path->dentry);
|
| 1039 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 1040 | struct f2fs_inode *ri = NULL;
|
| 1041 | unsigned int flags;
|
| 1042 |
|
| 1043 | if (f2fs_has_extra_attr(inode) &&
|
| 1044 | f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
|
| 1045 | F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
|
| 1046 | stat->result_mask |= STATX_BTIME;
|
| 1047 | stat->btime.tv_sec = fi->i_crtime.tv_sec;
|
| 1048 | stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
|
| 1049 | }
|
| 1050 |
|
| 1051 | |
| 1052 | * Return the DIO alignment restrictions if requested.
|
| 1053 | *
|
| 1054 | * f2fs sometimes supports DIO reads but not DIO writes. STATX_DIOALIGN
|
| 1055 | * cannot represent that, so in that case we report no DIO support.
|
| 1056 |
|
| 1057 | if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
|
| 1058 | unsigned int bsize = i_blocksize(inode);
|
| 1059 |
|
| 1060 | stat->result_mask |= STATX_DIOALIGN;
|
| 1061 | if (!f2fs_force_buffered_io(inode, WRITE)) {
|
| 1062 | stat->dio_mem_align = bsize;
|
| 1063 | stat->dio_offset_align = bsize;
|
| 1064 | }
|
| 1065 | }
|
| 1066 |
|
| 1067 | flags = fi->i_flags;
|
| 1068 | if (flags & F2FS_COMPR_FL)
|
| 1069 | stat->attributes |= STATX_ATTR_COMPRESSED;
|
| 1070 | if (flags & F2FS_APPEND_FL)
|
| 1071 | stat->attributes |= STATX_ATTR_APPEND;
|
| 1072 | if (IS_ENCRYPTED(inode))
|
| 1073 | stat->attributes |= STATX_ATTR_ENCRYPTED;
|
| 1074 | if (flags & F2FS_IMMUTABLE_FL)
|
| 1075 | stat->attributes |= STATX_ATTR_IMMUTABLE;
|
| 1076 | if (flags & F2FS_NODUMP_FL)
|
| 1077 | stat->attributes |= STATX_ATTR_NODUMP;
|
| 1078 |
|
| 1079 | stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
|
| 1080 | STATX_ATTR_APPEND |
|
| 1081 | STATX_ATTR_ENCRYPTED |
|
| 1082 | STATX_ATTR_IMMUTABLE |
|
| 1083 | STATX_ATTR_NODUMP);
|
| 1084 |
|
| 1085 | generic_fillattr(idmap, request_mask, inode, stat);
|
| 1086 |
|
| 1087 |
|
| 1088 | if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
|
| 1089 | f2fs_has_inline_dentry(inode))
|
| 1090 | stat->blocks += (stat->size + 511) >> 9;
|
| 1091 |
|
| 1092 | return 0;
|
| 1093 | }
|
| 1094 |
|
| 1095 | #ifdef CONFIG_F2FS_FS_POSIX_ACL
|
| 1096 | static void __setattr_copy(const struct mnt_idmap *idmap,
|
| 1097 | struct inode *inode, const struct iattr *attr)
|
| 1098 | {
|
| 1099 | unsigned int ia_valid = attr->ia_valid;
|
| 1100 |
|
| 1101 | i_uid_update(idmap, attr, inode);
|
| 1102 | i_gid_update(idmap, attr, inode);
|
| 1103 | if (ia_valid & ATTR_ATIME)
|
| 1104 | inode_set_atime_to_ts(inode, attr->ia_atime);
|
| 1105 | if (ia_valid & ATTR_MTIME)
|
| 1106 | inode_set_mtime_to_ts(inode, attr->ia_mtime);
|
| 1107 | if (ia_valid & ATTR_CTIME)
|
| 1108 | inode_set_ctime_to_ts(inode, attr->ia_ctime);
|
| 1109 | if (ia_valid & ATTR_MODE) {
|
| 1110 | umode_t mode = attr->ia_mode;
|
| 1111 |
|
| 1112 | if (!in_group_or_capable(idmap, inode, i_gid_into_vfsgid(idmap, inode)))
|
| 1113 | mode &= ~S_ISGID;
|
| 1114 | set_acl_inode(inode, mode);
|
| 1115 | }
|
| 1116 | }
|
| 1117 | #else
|
| 1118 | #define __setattr_copy setattr_copy
|
| 1119 | #endif
|
| 1120 |
|
| 1121 | int f2fs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
|
| 1122 | struct iattr *attr)
|
| 1123 | {
|
| 1124 | struct inode *inode = d_inode(dentry);
|
| 1125 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 1126 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 1127 | int err;
|
| 1128 |
|
| 1129 | if (unlikely(f2fs_cp_error(sbi)))
|
| 1130 | return -EIO;
|
| 1131 |
|
| 1132 | err = setattr_prepare(idmap, dentry, attr);
|
| 1133 | if (err)
|
| 1134 | return err;
|
| 1135 |
|
| 1136 | err = fscrypt_prepare_setattr(dentry, attr);
|
| 1137 | if (err)
|
| 1138 | return err;
|
| 1139 |
|
| 1140 | if (unlikely(IS_IMMUTABLE(inode)))
|
| 1141 | return -EPERM;
|
| 1142 |
|
| 1143 | if (unlikely(IS_APPEND(inode) &&
|
| 1144 | (attr->ia_valid & (ATTR_MODE | ATTR_UID |
|
| 1145 | ATTR_GID | ATTR_TIMES_SET))))
|
| 1146 | return -EPERM;
|
| 1147 |
|
| 1148 | if ((attr->ia_valid & ATTR_SIZE)) {
|
| 1149 | if (mapping_large_folio_support(inode->i_mapping))
|
| 1150 | return -EOPNOTSUPP;
|
| 1151 | if (IS_DEVICE_ALIASING(inode))
|
| 1152 | return -EPERM;
|
| 1153 | if (!f2fs_is_compress_backend_ready(inode))
|
| 1154 | return -EOPNOTSUPP;
|
| 1155 | if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED) &&
|
| 1156 | !IS_ALIGNED(attr->ia_size,
|
| 1157 | F2FS_BLK_TO_BYTES(fi->i_cluster_size)))
|
| 1158 | return -EINVAL;
|
| 1159 |
|
| 1160 | if (f2fs_is_pinned_file(inode)) {
|
| 1161 | |
| 1162 | * It may break section-aligned fallocate recovery
|
| 1163 | * mechanism, so do not allow larger size truncation.
|
| 1164 |
|
| 1165 | if (attr->ia_size > i_size_read(inode))
|
| 1166 | return -EINVAL;
|
| 1167 | |
| 1168 | * To prevent scattered pin block generation, we don't
|
| 1169 | * allow smaller/equal size unaligned truncation for
|
| 1170 | * pinned file.
|
| 1171 |
|
| 1172 | else if (!IS_ALIGNED(attr->ia_size,
|
| 1173 | F2FS_BLK_TO_BYTES(CAP_BLKS_PER_SEC(sbi))))
|
| 1174 | return -EINVAL;
|
| 1175 | }
|
| 1176 | }
|
| 1177 |
|
| 1178 | if (is_quota_modification(idmap, inode, attr)) {
|
| 1179 | err = f2fs_dquot_initialize(inode);
|
| 1180 | if (err)
|
| 1181 | return err;
|
| 1182 | }
|
| 1183 | if (i_uid_needs_update(idmap, attr, inode) ||
|
| 1184 | i_gid_needs_update(idmap, attr, inode)) {
|
| 1185 | struct f2fs_lock_context lc;
|
| 1186 |
|
| 1187 | f2fs_lock_op(sbi, &lc);
|
| 1188 | err = dquot_transfer(idmap, inode, attr);
|
| 1189 | if (err) {
|
| 1190 | set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
|
| 1191 | f2fs_unlock_op(sbi, &lc);
|
| 1192 | return err;
|
| 1193 | }
|
| 1194 | |
| 1195 | * update uid/gid under lock_op(), so that dquot and inode can
|
| 1196 | * be updated atomically.
|
| 1197 |
|
| 1198 | i_uid_update(idmap, attr, inode);
|
| 1199 | i_gid_update(idmap, attr, inode);
|
| 1200 | f2fs_mark_inode_dirty_sync(inode, true);
|
| 1201 | f2fs_unlock_op(sbi, &lc);
|
| 1202 | }
|
| 1203 |
|
| 1204 | if (attr->ia_valid & ATTR_SIZE) {
|
| 1205 | loff_t old_size = i_size_read(inode);
|
| 1206 |
|
| 1207 | if (attr->ia_size > MAX_INLINE_DATA(inode)) {
|
| 1208 | |
| 1209 | * should convert inline inode before i_size_write to
|
| 1210 | * keep smaller than inline_data size with inline flag.
|
| 1211 |
|
| 1212 | err = f2fs_convert_inline_inode(inode);
|
| 1213 | if (err)
|
| 1214 | return err;
|
| 1215 | }
|
| 1216 |
|
| 1217 | |
| 1218 | * wait for inflight dio, blocks should be removed after
|
| 1219 | * IO completion.
|
| 1220 |
|
| 1221 | if (attr->ia_size < old_size)
|
| 1222 | inode_dio_wait(inode);
|
| 1223 |
|
| 1224 | f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
|
| 1225 | filemap_invalidate_lock(inode->i_mapping);
|
| 1226 |
|
| 1227 | if (attr->ia_size > old_size) {
|
| 1228 | err = f2fs_zero_post_eof_page(inode,
|
| 1229 | attr->ia_size, false, true);
|
| 1230 | if (err)
|
| 1231 | goto err_out;
|
| 1232 | }
|
| 1233 | truncate_setsize(inode, attr->ia_size);
|
| 1234 |
|
| 1235 | if (attr->ia_size <= old_size)
|
| 1236 | err = f2fs_truncate(inode);
|
| 1237 | |
| 1238 | * do not trim all blocks after i_size if target size is
|
| 1239 | * larger than i_size.
|
| 1240 |
|
| 1241 | err_out:
|
| 1242 | filemap_invalidate_unlock(inode->i_mapping);
|
| 1243 | f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
|
| 1244 | if (err)
|
| 1245 | return err;
|
| 1246 |
|
| 1247 | spin_lock(&fi->i_size_lock);
|
| 1248 | inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
|
| 1249 | fi->last_disk_size = i_size_read(inode);
|
| 1250 | spin_unlock(&fi->i_size_lock);
|
| 1251 | }
|
| 1252 |
|
| 1253 | __setattr_copy(idmap, inode, attr);
|
| 1254 |
|
| 1255 | if (attr->ia_valid & ATTR_MODE) {
|
| 1256 | err = posix_acl_chmod(idmap, dentry, f2fs_get_inode_mode(inode));
|
| 1257 |
|
| 1258 | if (is_inode_flag_set(inode, FI_ACL_MODE)) {
|
| 1259 | if (!err)
|
| 1260 | inode->i_mode = fi->i_acl_mode;
|
| 1261 | clear_inode_flag(inode, FI_ACL_MODE);
|
| 1262 | }
|
| 1263 | }
|
| 1264 |
|
| 1265 |
|
| 1266 | f2fs_mark_inode_dirty_sync(inode, true);
|
| 1267 |
|
| 1268 |
|
| 1269 | f2fs_balance_fs(sbi, true);
|
| 1270 |
|
| 1271 | return err;
|
| 1272 | }
|
| 1273 |
|
| 1274 | const struct inode_operations f2fs_file_inode_operations = {
|
| 1275 | .getattr = f2fs_getattr,
|
| 1276 | .setattr = f2fs_setattr,
|
| 1277 | .get_inode_acl = f2fs_get_acl,
|
| 1278 | .set_acl = f2fs_set_acl,
|
| 1279 | .listxattr = f2fs_listxattr,
|
| 1280 | .fiemap = f2fs_fiemap,
|
| 1281 | .fileattr_get = f2fs_fileattr_get,
|
| 1282 | .fileattr_set = f2fs_fileattr_set,
|
| 1283 | };
|
| 1284 |
|
| 1285 | static int fill_zero(struct inode *inode, pgoff_t index,
|
| 1286 | loff_t start, loff_t len)
|
| 1287 | {
|
| 1288 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 1289 | struct folio *folio;
|
| 1290 | struct f2fs_lock_context lc;
|
| 1291 |
|
| 1292 | if (!len)
|
| 1293 | return 0;
|
| 1294 |
|
| 1295 | f2fs_balance_fs(sbi, true);
|
| 1296 |
|
| 1297 | f2fs_lock_op(sbi, &lc);
|
| 1298 | folio = f2fs_get_new_data_folio(inode, NULL, index, false);
|
| 1299 | f2fs_unlock_op(sbi, &lc);
|
| 1300 |
|
| 1301 | if (IS_ERR(folio))
|
| 1302 | return PTR_ERR(folio);
|
| 1303 |
|
| 1304 | f2fs_folio_wait_writeback(folio, DATA, true, true);
|
| 1305 | folio_zero_range(folio, start, len);
|
| 1306 | folio_mark_dirty(folio);
|
| 1307 | f2fs_folio_put(folio, true);
|
| 1308 | return 0;
|
| 1309 | }
|
| 1310 |
|
| 1311 | int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
|
| 1312 | {
|
| 1313 | int err;
|
| 1314 |
|
| 1315 | while (pg_start < pg_end) {
|
| 1316 | struct dnode_of_data dn;
|
| 1317 | pgoff_t end_offset, count;
|
| 1318 |
|
| 1319 | set_new_dnode(&dn, inode, NULL, NULL, 0);
|
| 1320 | err = f2fs_get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
|
| 1321 | if (err) {
|
| 1322 | if (err == -ENOENT) {
|
| 1323 | pg_start = f2fs_get_next_page_offset(&dn,
|
| 1324 | pg_start);
|
| 1325 | continue;
|
| 1326 | }
|
| 1327 | return err;
|
| 1328 | }
|
| 1329 |
|
| 1330 | end_offset = ADDRS_PER_PAGE(dn.node_folio, inode);
|
| 1331 | count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
|
| 1332 |
|
| 1333 | f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
|
| 1334 |
|
| 1335 | f2fs_truncate_data_blocks_range(&dn, count);
|
| 1336 | f2fs_put_dnode(&dn);
|
| 1337 |
|
| 1338 | pg_start += count;
|
| 1339 | }
|
| 1340 | return 0;
|
| 1341 | }
|
| 1342 |
|
| 1343 | static int f2fs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
|
| 1344 | {
|
| 1345 | pgoff_t pg_start, pg_end;
|
| 1346 | loff_t off_start, off_end;
|
| 1347 | int ret;
|
| 1348 |
|
| 1349 | ret = f2fs_convert_inline_inode(inode);
|
| 1350 | if (ret)
|
| 1351 | return ret;
|
| 1352 |
|
| 1353 | ret = f2fs_zero_post_eof_page(inode, offset + len, true, false);
|
| 1354 | if (ret)
|
| 1355 | return ret;
|
| 1356 |
|
| 1357 | pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
|
| 1358 | pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
|
| 1359 |
|
| 1360 | off_start = offset & (PAGE_SIZE - 1);
|
| 1361 | off_end = (offset + len) & (PAGE_SIZE - 1);
|
| 1362 |
|
| 1363 | if (pg_start == pg_end) {
|
| 1364 | ret = fill_zero(inode, pg_start, off_start,
|
| 1365 | off_end - off_start);
|
| 1366 | if (ret)
|
| 1367 | return ret;
|
| 1368 | } else {
|
| 1369 | if (off_start) {
|
| 1370 | ret = fill_zero(inode, pg_start++, off_start,
|
| 1371 | PAGE_SIZE - off_start);
|
| 1372 | if (ret)
|
| 1373 | return ret;
|
| 1374 | }
|
| 1375 | if (off_end) {
|
| 1376 | ret = fill_zero(inode, pg_end, 0, off_end);
|
| 1377 | if (ret)
|
| 1378 | return ret;
|
| 1379 | }
|
| 1380 |
|
| 1381 | if (pg_start < pg_end) {
|
| 1382 | loff_t blk_start, blk_end;
|
| 1383 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 1384 | struct f2fs_lock_context lc;
|
| 1385 |
|
| 1386 | f2fs_balance_fs(sbi, true);
|
| 1387 |
|
| 1388 | blk_start = (loff_t)pg_start << PAGE_SHIFT;
|
| 1389 | blk_end = (loff_t)pg_end << PAGE_SHIFT;
|
| 1390 |
|
| 1391 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 1392 | filemap_invalidate_lock(inode->i_mapping);
|
| 1393 |
|
| 1394 | truncate_pagecache_range(inode, blk_start, blk_end - 1);
|
| 1395 |
|
| 1396 | f2fs_lock_op(sbi, &lc);
|
| 1397 | ret = f2fs_truncate_hole(inode, pg_start, pg_end);
|
| 1398 | f2fs_unlock_op(sbi, &lc);
|
| 1399 |
|
| 1400 | filemap_invalidate_unlock(inode->i_mapping);
|
| 1401 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 1402 | }
|
| 1403 | }
|
| 1404 |
|
| 1405 | return ret;
|
| 1406 | }
|
| 1407 |
|
| 1408 | static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
|
| 1409 | int *do_replace, pgoff_t off, pgoff_t len)
|
| 1410 | {
|
| 1411 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 1412 | struct dnode_of_data dn;
|
| 1413 | int ret, done, i;
|
| 1414 |
|
| 1415 | next_dnode:
|
| 1416 | set_new_dnode(&dn, inode, NULL, NULL, 0);
|
| 1417 | ret = f2fs_get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
|
| 1418 | if (ret && ret != -ENOENT) {
|
| 1419 | return ret;
|
| 1420 | } else if (ret == -ENOENT) {
|
| 1421 | if (dn.max_level == 0)
|
| 1422 | return -ENOENT;
|
| 1423 | done = min((pgoff_t)ADDRS_PER_BLOCK(inode) -
|
| 1424 | dn.ofs_in_node, len);
|
| 1425 | blkaddr += done;
|
| 1426 | do_replace += done;
|
| 1427 | goto next;
|
| 1428 | }
|
| 1429 |
|
| 1430 | done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_folio, inode) -
|
| 1431 | dn.ofs_in_node, len);
|
| 1432 | for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
|
| 1433 | *blkaddr = f2fs_data_blkaddr(&dn);
|
| 1434 |
|
| 1435 | if (__is_valid_data_blkaddr(*blkaddr) &&
|
| 1436 | !f2fs_is_valid_blkaddr(sbi, *blkaddr,
|
| 1437 | DATA_GENERIC_ENHANCE)) {
|
| 1438 | f2fs_put_dnode(&dn);
|
| 1439 | return -EFSCORRUPTED;
|
| 1440 | }
|
| 1441 |
|
| 1442 | if (!f2fs_is_checkpointed_data(sbi, *blkaddr)) {
|
| 1443 |
|
| 1444 | if (f2fs_lfs_mode(sbi)) {
|
| 1445 | f2fs_put_dnode(&dn);
|
| 1446 | return -EOPNOTSUPP;
|
| 1447 | }
|
| 1448 |
|
| 1449 |
|
| 1450 | f2fs_update_data_blkaddr(&dn, NULL_ADDR);
|
| 1451 | *do_replace = 1;
|
| 1452 | }
|
| 1453 | }
|
| 1454 | f2fs_put_dnode(&dn);
|
| 1455 | next:
|
| 1456 | len -= done;
|
| 1457 | off += done;
|
| 1458 | if (len)
|
| 1459 | goto next_dnode;
|
| 1460 | return 0;
|
| 1461 | }
|
| 1462 |
|
| 1463 | static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
|
| 1464 | int *do_replace, pgoff_t off, int len)
|
| 1465 | {
|
| 1466 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 1467 | struct dnode_of_data dn;
|
| 1468 | int ret, i;
|
| 1469 |
|
| 1470 | for (i = 0; i < len; i++, do_replace++, blkaddr++) {
|
| 1471 | if (*do_replace == 0)
|
| 1472 | continue;
|
| 1473 |
|
| 1474 | set_new_dnode(&dn, inode, NULL, NULL, 0);
|
| 1475 | ret = f2fs_get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
|
| 1476 | if (ret) {
|
| 1477 | dec_valid_block_count(sbi, inode, 1);
|
| 1478 | f2fs_invalidate_blocks(sbi, *blkaddr, 1);
|
| 1479 | } else {
|
| 1480 | f2fs_update_data_blkaddr(&dn, *blkaddr);
|
| 1481 | }
|
| 1482 | f2fs_put_dnode(&dn);
|
| 1483 | }
|
| 1484 | return 0;
|
| 1485 | }
|
| 1486 |
|
| 1487 | static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
|
| 1488 | block_t *blkaddr, int *do_replace,
|
| 1489 | pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
|
| 1490 | {
|
| 1491 | struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
|
| 1492 | pgoff_t i = 0;
|
| 1493 | int ret;
|
| 1494 |
|
| 1495 | while (i < len) {
|
| 1496 | if (blkaddr[i] == NULL_ADDR && !full) {
|
| 1497 | i++;
|
| 1498 | continue;
|
| 1499 | }
|
| 1500 |
|
| 1501 | if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
|
| 1502 | struct dnode_of_data dn;
|
| 1503 | struct node_info ni;
|
| 1504 | size_t new_size;
|
| 1505 | pgoff_t ilen;
|
| 1506 |
|
| 1507 | set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
|
| 1508 | ret = f2fs_get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
|
| 1509 | if (ret)
|
| 1510 | return ret;
|
| 1511 |
|
| 1512 | ret = f2fs_get_node_info(sbi, dn.nid, &ni, false);
|
| 1513 | if (ret) {
|
| 1514 | f2fs_put_dnode(&dn);
|
| 1515 | return ret;
|
| 1516 | }
|
| 1517 |
|
| 1518 | ilen = min((pgoff_t)
|
| 1519 | ADDRS_PER_PAGE(dn.node_folio, dst_inode) -
|
| 1520 | dn.ofs_in_node, len - i);
|
| 1521 | do {
|
| 1522 | dn.data_blkaddr = f2fs_data_blkaddr(&dn);
|
| 1523 | f2fs_truncate_data_blocks_range(&dn, 1);
|
| 1524 |
|
| 1525 | if (do_replace[i]) {
|
| 1526 | f2fs_i_blocks_write(src_inode,
|
| 1527 | 1, false, false);
|
| 1528 | f2fs_i_blocks_write(dst_inode,
|
| 1529 | 1, true, false);
|
| 1530 | f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
|
| 1531 | blkaddr[i], ni.version, true, false);
|
| 1532 |
|
| 1533 | do_replace[i] = 0;
|
| 1534 | }
|
| 1535 | dn.ofs_in_node++;
|
| 1536 | i++;
|
| 1537 | new_size = (loff_t)(dst + i) << PAGE_SHIFT;
|
| 1538 | if (dst_inode->i_size < new_size)
|
| 1539 | f2fs_i_size_write(dst_inode, new_size);
|
| 1540 | } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
|
| 1541 |
|
| 1542 | f2fs_put_dnode(&dn);
|
| 1543 | } else {
|
| 1544 | struct folio *fsrc, *fdst;
|
| 1545 |
|
| 1546 | fsrc = f2fs_get_lock_data_folio(src_inode,
|
| 1547 | src + i, true);
|
| 1548 | if (IS_ERR(fsrc))
|
| 1549 | return PTR_ERR(fsrc);
|
| 1550 | fdst = f2fs_get_new_data_folio(dst_inode, NULL, dst + i,
|
| 1551 | true);
|
| 1552 | if (IS_ERR(fdst)) {
|
| 1553 | f2fs_folio_put(fsrc, true);
|
| 1554 | return PTR_ERR(fdst);
|
| 1555 | }
|
| 1556 |
|
| 1557 | f2fs_folio_wait_writeback(fdst, DATA, true, true);
|
| 1558 |
|
| 1559 | memcpy_folio(fdst, 0, fsrc, 0, PAGE_SIZE);
|
| 1560 | folio_mark_dirty(fdst);
|
| 1561 | folio_set_f2fs_gcing(fdst);
|
| 1562 | f2fs_folio_put(fdst, true);
|
| 1563 | f2fs_folio_put(fsrc, true);
|
| 1564 |
|
| 1565 | ret = f2fs_truncate_hole(src_inode,
|
| 1566 | src + i, src + i + 1);
|
| 1567 | if (ret)
|
| 1568 | return ret;
|
| 1569 | i++;
|
| 1570 | }
|
| 1571 | }
|
| 1572 | return 0;
|
| 1573 | }
|
| 1574 |
|
| 1575 | static int __exchange_data_block(struct inode *src_inode,
|
| 1576 | struct inode *dst_inode, pgoff_t src, pgoff_t dst,
|
| 1577 | pgoff_t len, bool full)
|
| 1578 | {
|
| 1579 | block_t *src_blkaddr;
|
| 1580 | int *do_replace;
|
| 1581 | pgoff_t olen;
|
| 1582 | int ret;
|
| 1583 |
|
| 1584 | while (len) {
|
| 1585 | olen = min((pgoff_t)4 * ADDRS_PER_BLOCK(src_inode), len);
|
| 1586 |
|
| 1587 | src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
|
| 1588 | array_size(olen, sizeof(block_t)),
|
| 1589 | GFP_NOFS);
|
| 1590 | if (!src_blkaddr)
|
| 1591 | return -ENOMEM;
|
| 1592 |
|
| 1593 | do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
|
| 1594 | array_size(olen, sizeof(int)),
|
| 1595 | GFP_NOFS);
|
| 1596 | if (!do_replace) {
|
| 1597 | kvfree(src_blkaddr);
|
| 1598 | return -ENOMEM;
|
| 1599 | }
|
| 1600 |
|
| 1601 | ret = __read_out_blkaddrs(src_inode, src_blkaddr,
|
| 1602 | do_replace, src, olen);
|
| 1603 | if (ret)
|
| 1604 | goto roll_back;
|
| 1605 |
|
| 1606 | ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
|
| 1607 | do_replace, src, dst, olen, full);
|
| 1608 | if (ret)
|
| 1609 | goto roll_back;
|
| 1610 |
|
| 1611 | src += olen;
|
| 1612 | dst += olen;
|
| 1613 | len -= olen;
|
| 1614 |
|
| 1615 | kvfree(src_blkaddr);
|
| 1616 | kvfree(do_replace);
|
| 1617 | }
|
| 1618 | return 0;
|
| 1619 |
|
| 1620 | roll_back:
|
| 1621 | __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, olen);
|
| 1622 | kvfree(src_blkaddr);
|
| 1623 | kvfree(do_replace);
|
| 1624 | return ret;
|
| 1625 | }
|
| 1626 |
|
| 1627 | static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len)
|
| 1628 | {
|
| 1629 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 1630 | struct f2fs_lock_context lc;
|
| 1631 | pgoff_t nrpages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
|
| 1632 | pgoff_t start = offset >> PAGE_SHIFT;
|
| 1633 | pgoff_t end = (offset + len) >> PAGE_SHIFT;
|
| 1634 | int ret;
|
| 1635 |
|
| 1636 | f2fs_balance_fs(sbi, true);
|
| 1637 |
|
| 1638 |
|
| 1639 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 1640 | filemap_invalidate_lock(inode->i_mapping);
|
| 1641 |
|
| 1642 | ret = f2fs_zero_post_eof_page(inode, offset + len, false, false);
|
| 1643 | if (ret)
|
| 1644 | goto out_unlock;
|
| 1645 |
|
| 1646 | f2fs_lock_op(sbi, &lc);
|
| 1647 | f2fs_drop_extent_tree(inode);
|
| 1648 | truncate_pagecache(inode, offset);
|
| 1649 | ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
|
| 1650 | f2fs_unlock_op(sbi, &lc);
|
| 1651 |
|
| 1652 | out_unlock:
|
| 1653 | filemap_invalidate_unlock(inode->i_mapping);
|
| 1654 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 1655 | return ret;
|
| 1656 | }
|
| 1657 |
|
| 1658 | static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
|
| 1659 | {
|
| 1660 | loff_t new_size;
|
| 1661 | int ret;
|
| 1662 |
|
| 1663 | if (offset + len >= i_size_read(inode))
|
| 1664 | return -EINVAL;
|
| 1665 |
|
| 1666 |
|
| 1667 | if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
|
| 1668 | return -EINVAL;
|
| 1669 |
|
| 1670 | ret = f2fs_convert_inline_inode(inode);
|
| 1671 | if (ret)
|
| 1672 | return ret;
|
| 1673 |
|
| 1674 |
|
| 1675 | ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
|
| 1676 | if (ret)
|
| 1677 | return ret;
|
| 1678 |
|
| 1679 | ret = f2fs_do_collapse(inode, offset, len);
|
| 1680 | if (ret)
|
| 1681 | return ret;
|
| 1682 |
|
| 1683 |
|
| 1684 | filemap_invalidate_lock(inode->i_mapping);
|
| 1685 | ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
|
| 1686 | if (ret)
|
| 1687 | goto out_unlock;
|
| 1688 | truncate_pagecache(inode, offset);
|
| 1689 |
|
| 1690 | new_size = i_size_read(inode) - len;
|
| 1691 | ret = f2fs_truncate_blocks(inode, new_size, true);
|
| 1692 | out_unlock:
|
| 1693 | filemap_invalidate_unlock(inode->i_mapping);
|
| 1694 | if (!ret)
|
| 1695 | f2fs_i_size_write(inode, new_size);
|
| 1696 | return ret;
|
| 1697 | }
|
| 1698 |
|
| 1699 | static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
|
| 1700 | pgoff_t end)
|
| 1701 | {
|
| 1702 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
|
| 1703 | pgoff_t index = start;
|
| 1704 | unsigned int ofs_in_node = dn->ofs_in_node;
|
| 1705 | blkcnt_t count = 0;
|
| 1706 | int ret;
|
| 1707 |
|
| 1708 | for (; index < end; index++, dn->ofs_in_node++) {
|
| 1709 | if (f2fs_data_blkaddr(dn) == NULL_ADDR)
|
| 1710 | count++;
|
| 1711 | }
|
| 1712 |
|
| 1713 | dn->ofs_in_node = ofs_in_node;
|
| 1714 | ret = f2fs_reserve_new_blocks(dn, count);
|
| 1715 | if (ret)
|
| 1716 | return ret;
|
| 1717 |
|
| 1718 | dn->ofs_in_node = ofs_in_node;
|
| 1719 | for (index = start; index < end; index++, dn->ofs_in_node++) {
|
| 1720 | dn->data_blkaddr = f2fs_data_blkaddr(dn);
|
| 1721 | |
| 1722 | * f2fs_reserve_new_blocks will not guarantee entire block
|
| 1723 | * allocation.
|
| 1724 |
|
| 1725 | if (dn->data_blkaddr == NULL_ADDR) {
|
| 1726 | ret = -ENOSPC;
|
| 1727 | break;
|
| 1728 | }
|
| 1729 |
|
| 1730 | if (dn->data_blkaddr == NEW_ADDR)
|
| 1731 | continue;
|
| 1732 |
|
| 1733 | if (!f2fs_is_valid_blkaddr(sbi, dn->data_blkaddr,
|
| 1734 | DATA_GENERIC_ENHANCE)) {
|
| 1735 | ret = -EFSCORRUPTED;
|
| 1736 | break;
|
| 1737 | }
|
| 1738 |
|
| 1739 | f2fs_invalidate_blocks(sbi, dn->data_blkaddr, 1);
|
| 1740 | f2fs_set_data_blkaddr(dn, NEW_ADDR);
|
| 1741 | }
|
| 1742 |
|
| 1743 | if (index > start) {
|
| 1744 | f2fs_update_read_extent_cache_range(dn, start, 0,
|
| 1745 | index - start);
|
| 1746 | f2fs_update_age_extent_cache_range(dn, start, index - start);
|
| 1747 | }
|
| 1748 |
|
| 1749 | return ret;
|
| 1750 | }
|
| 1751 |
|
| 1752 | static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
|
| 1753 | int mode)
|
| 1754 | {
|
| 1755 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 1756 | struct address_space *mapping = inode->i_mapping;
|
| 1757 | pgoff_t index, pg_start, pg_end;
|
| 1758 | loff_t new_size = i_size_read(inode);
|
| 1759 | loff_t off_start, off_end;
|
| 1760 | int ret = 0;
|
| 1761 |
|
| 1762 | ret = inode_newsize_ok(inode, (len + offset));
|
| 1763 | if (ret)
|
| 1764 | return ret;
|
| 1765 |
|
| 1766 | ret = f2fs_convert_inline_inode(inode);
|
| 1767 | if (ret)
|
| 1768 | return ret;
|
| 1769 |
|
| 1770 | ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
|
| 1771 | if (ret)
|
| 1772 | return ret;
|
| 1773 |
|
| 1774 | ret = f2fs_zero_post_eof_page(inode, offset + len, true, false);
|
| 1775 | if (ret)
|
| 1776 | return ret;
|
| 1777 |
|
| 1778 | pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
|
| 1779 | pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
|
| 1780 |
|
| 1781 | off_start = offset & (PAGE_SIZE - 1);
|
| 1782 | off_end = (offset + len) & (PAGE_SIZE - 1);
|
| 1783 |
|
| 1784 | if (pg_start == pg_end) {
|
| 1785 | ret = fill_zero(inode, pg_start, off_start,
|
| 1786 | off_end - off_start);
|
| 1787 | if (ret)
|
| 1788 | return ret;
|
| 1789 |
|
| 1790 | new_size = max_t(loff_t, new_size, offset + len);
|
| 1791 | } else {
|
| 1792 | if (off_start) {
|
| 1793 | ret = fill_zero(inode, pg_start++, off_start,
|
| 1794 | PAGE_SIZE - off_start);
|
| 1795 | if (ret)
|
| 1796 | return ret;
|
| 1797 |
|
| 1798 | new_size = max_t(loff_t, new_size,
|
| 1799 | (loff_t)pg_start << PAGE_SHIFT);
|
| 1800 | }
|
| 1801 |
|
| 1802 | for (index = pg_start; index < pg_end;) {
|
| 1803 | struct dnode_of_data dn;
|
| 1804 | struct f2fs_lock_context lc;
|
| 1805 | unsigned int end_offset;
|
| 1806 | pgoff_t end;
|
| 1807 |
|
| 1808 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 1809 | filemap_invalidate_lock(mapping);
|
| 1810 |
|
| 1811 | truncate_pagecache_range(inode,
|
| 1812 | (loff_t)index << PAGE_SHIFT,
|
| 1813 | ((loff_t)pg_end << PAGE_SHIFT) - 1);
|
| 1814 |
|
| 1815 | f2fs_lock_op(sbi, &lc);
|
| 1816 |
|
| 1817 | set_new_dnode(&dn, inode, NULL, NULL, 0);
|
| 1818 | ret = f2fs_get_dnode_of_data(&dn, index, ALLOC_NODE);
|
| 1819 | if (ret) {
|
| 1820 | f2fs_unlock_op(sbi, &lc);
|
| 1821 | filemap_invalidate_unlock(mapping);
|
| 1822 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 1823 | goto out;
|
| 1824 | }
|
| 1825 |
|
| 1826 | end_offset = ADDRS_PER_PAGE(dn.node_folio, inode);
|
| 1827 | end = min(pg_end, end_offset - dn.ofs_in_node + index);
|
| 1828 |
|
| 1829 | ret = f2fs_do_zero_range(&dn, index, end);
|
| 1830 | f2fs_put_dnode(&dn);
|
| 1831 |
|
| 1832 | f2fs_unlock_op(sbi, &lc);
|
| 1833 | filemap_invalidate_unlock(mapping);
|
| 1834 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 1835 |
|
| 1836 | f2fs_balance_fs(sbi, dn.node_changed);
|
| 1837 |
|
| 1838 | if (ret)
|
| 1839 | goto out;
|
| 1840 |
|
| 1841 | index = end;
|
| 1842 | new_size = max_t(loff_t, new_size,
|
| 1843 | (loff_t)index << PAGE_SHIFT);
|
| 1844 | }
|
| 1845 |
|
| 1846 | if (off_end) {
|
| 1847 | ret = fill_zero(inode, pg_end, 0, off_end);
|
| 1848 | if (ret)
|
| 1849 | goto out;
|
| 1850 |
|
| 1851 | new_size = max_t(loff_t, new_size, offset + len);
|
| 1852 | }
|
| 1853 | }
|
| 1854 |
|
| 1855 | out:
|
| 1856 | if (new_size > i_size_read(inode)) {
|
| 1857 | if (mode & FALLOC_FL_KEEP_SIZE)
|
| 1858 | file_set_keep_isize(inode);
|
| 1859 | else
|
| 1860 | f2fs_i_size_write(inode, new_size);
|
| 1861 | }
|
| 1862 | return ret;
|
| 1863 | }
|
| 1864 |
|
| 1865 | static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
|
| 1866 | {
|
| 1867 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 1868 | struct address_space *mapping = inode->i_mapping;
|
| 1869 | pgoff_t nr, pg_start, pg_end, delta, idx;
|
| 1870 | loff_t new_size;
|
| 1871 | int ret = 0;
|
| 1872 |
|
| 1873 | new_size = i_size_read(inode) + len;
|
| 1874 | ret = inode_newsize_ok(inode, new_size);
|
| 1875 | if (ret)
|
| 1876 | return ret;
|
| 1877 |
|
| 1878 | if (offset >= i_size_read(inode))
|
| 1879 | return -EINVAL;
|
| 1880 |
|
| 1881 |
|
| 1882 | if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
|
| 1883 | return -EINVAL;
|
| 1884 |
|
| 1885 | ret = f2fs_convert_inline_inode(inode);
|
| 1886 | if (ret)
|
| 1887 | return ret;
|
| 1888 |
|
| 1889 | f2fs_balance_fs(sbi, true);
|
| 1890 |
|
| 1891 | filemap_invalidate_lock(mapping);
|
| 1892 | ret = f2fs_truncate_blocks(inode, i_size_read(inode), true);
|
| 1893 | filemap_invalidate_unlock(mapping);
|
| 1894 | if (ret)
|
| 1895 | return ret;
|
| 1896 |
|
| 1897 |
|
| 1898 | ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
|
| 1899 | if (ret)
|
| 1900 | return ret;
|
| 1901 |
|
| 1902 | pg_start = offset >> PAGE_SHIFT;
|
| 1903 | pg_end = (offset + len) >> PAGE_SHIFT;
|
| 1904 | delta = pg_end - pg_start;
|
| 1905 | idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
|
| 1906 |
|
| 1907 |
|
| 1908 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 1909 | filemap_invalidate_lock(mapping);
|
| 1910 |
|
| 1911 | ret = f2fs_zero_post_eof_page(inode, offset + len, false, false);
|
| 1912 | if (ret)
|
| 1913 | goto out_unlock;
|
| 1914 | truncate_pagecache(inode, offset);
|
| 1915 |
|
| 1916 | while (!ret && idx > pg_start) {
|
| 1917 | struct f2fs_lock_context lc;
|
| 1918 |
|
| 1919 | nr = idx - pg_start;
|
| 1920 | if (nr > delta)
|
| 1921 | nr = delta;
|
| 1922 | idx -= nr;
|
| 1923 |
|
| 1924 | f2fs_lock_op(sbi, &lc);
|
| 1925 | f2fs_drop_extent_tree(inode);
|
| 1926 |
|
| 1927 | ret = __exchange_data_block(inode, inode, idx,
|
| 1928 | idx + delta, nr, false);
|
| 1929 | f2fs_unlock_op(sbi, &lc);
|
| 1930 | }
|
| 1931 | out_unlock:
|
| 1932 | filemap_invalidate_unlock(mapping);
|
| 1933 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 1934 | if (ret)
|
| 1935 | return ret;
|
| 1936 |
|
| 1937 |
|
| 1938 | filemap_invalidate_lock(mapping);
|
| 1939 | ret = filemap_write_and_wait_range(mapping, offset, LLONG_MAX);
|
| 1940 | truncate_pagecache(inode, offset);
|
| 1941 | filemap_invalidate_unlock(mapping);
|
| 1942 |
|
| 1943 | if (!ret)
|
| 1944 | f2fs_i_size_write(inode, new_size);
|
| 1945 | return ret;
|
| 1946 | }
|
| 1947 |
|
| 1948 | static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
|
| 1949 | loff_t len, int mode)
|
| 1950 | {
|
| 1951 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 1952 | struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
|
| 1953 | .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE,
|
| 1954 | .m_may_create = true };
|
| 1955 | struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
|
| 1956 | .init_gc_type = FG_GC,
|
| 1957 | .should_migrate_blocks = false,
|
| 1958 | .err_gc_skipped = true,
|
| 1959 | .nr_free_secs = 0 };
|
| 1960 | pgoff_t pg_start, pg_end;
|
| 1961 | loff_t new_size;
|
| 1962 | loff_t off_end;
|
| 1963 | block_t expanded = 0;
|
| 1964 | int err;
|
| 1965 |
|
| 1966 | err = inode_newsize_ok(inode, (len + offset));
|
| 1967 | if (err)
|
| 1968 | return err;
|
| 1969 |
|
| 1970 | err = f2fs_convert_inline_inode(inode);
|
| 1971 | if (err)
|
| 1972 | return err;
|
| 1973 |
|
| 1974 | err = f2fs_zero_post_eof_page(inode, offset + len, true, true);
|
| 1975 | if (err)
|
| 1976 | return err;
|
| 1977 |
|
| 1978 | f2fs_balance_fs(sbi, true);
|
| 1979 |
|
| 1980 | pg_start = ((unsigned long long)offset) >> PAGE_SHIFT;
|
| 1981 | pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
|
| 1982 | off_end = (offset + len) & (PAGE_SIZE - 1);
|
| 1983 |
|
| 1984 | map.m_lblk = pg_start;
|
| 1985 | map.m_len = pg_end - pg_start;
|
| 1986 | if (off_end)
|
| 1987 | map.m_len++;
|
| 1988 |
|
| 1989 | if (!map.m_len)
|
| 1990 | return 0;
|
| 1991 |
|
| 1992 | if (f2fs_is_pinned_file(inode)) {
|
| 1993 | block_t sec_blks = CAP_BLKS_PER_SEC(sbi);
|
| 1994 | block_t sec_len;
|
| 1995 |
|
| 1996 | if (map.m_lblk % sec_blks) {
|
| 1997 | pg_start = rounddown(map.m_lblk, sec_blks);
|
| 1998 | map.m_lblk = pg_start;
|
| 1999 | map.m_len = pg_end - pg_start;
|
| 2000 | if (off_end)
|
| 2001 | map.m_len++;
|
| 2002 | }
|
| 2003 | sec_len = roundup(map.m_len, sec_blks);
|
| 2004 | map.m_len = sec_blks;
|
| 2005 | next_alloc:
|
| 2006 | f2fs_down_write(&sbi->pin_sem);
|
| 2007 |
|
| 2008 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
|
| 2009 | if (has_not_enough_free_secs(sbi, 0, 0)) {
|
| 2010 | f2fs_up_write(&sbi->pin_sem);
|
| 2011 | err = -ENOSPC;
|
| 2012 | f2fs_warn_ratelimited(sbi,
|
| 2013 | "ino:%llu, start:%lu, end:%lu, need to trigger GC to "
|
| 2014 | "reclaim enough free segment when checkpoint is enabled",
|
| 2015 | inode->i_ino, pg_start, pg_end);
|
| 2016 | goto out_err;
|
| 2017 | }
|
| 2018 | }
|
| 2019 |
|
| 2020 | if (has_not_enough_free_secs(sbi, 0,
|
| 2021 | sbi->reserved_pin_section)) {
|
| 2022 | f2fs_down_write_trace(&sbi->gc_lock, &gc_control.lc);
|
| 2023 | stat_inc_gc_call_count(sbi, FOREGROUND);
|
| 2024 | err = f2fs_gc(sbi, &gc_control);
|
| 2025 | if (err && err != -ENODATA) {
|
| 2026 | f2fs_up_write(&sbi->pin_sem);
|
| 2027 | goto out_err;
|
| 2028 | }
|
| 2029 | }
|
| 2030 |
|
| 2031 | err = f2fs_allocate_pinning_section(sbi);
|
| 2032 | if (err) {
|
| 2033 | f2fs_up_write(&sbi->pin_sem);
|
| 2034 | goto out_err;
|
| 2035 | }
|
| 2036 |
|
| 2037 | map.m_seg_type = CURSEG_COLD_DATA_PINNED;
|
| 2038 | err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_DIO);
|
| 2039 | file_dont_truncate(inode);
|
| 2040 |
|
| 2041 | f2fs_up_write(&sbi->pin_sem);
|
| 2042 |
|
| 2043 | expanded += map.m_len;
|
| 2044 | sec_len -= map.m_len;
|
| 2045 | map.m_lblk += map.m_len;
|
| 2046 | if (!err && sec_len)
|
| 2047 | goto next_alloc;
|
| 2048 |
|
| 2049 | map.m_len = expanded;
|
| 2050 | } else {
|
| 2051 | err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_AIO);
|
| 2052 | expanded = map.m_len;
|
| 2053 | }
|
| 2054 | out_err:
|
| 2055 | if (err) {
|
| 2056 | pgoff_t last_off;
|
| 2057 |
|
| 2058 | if (!expanded)
|
| 2059 | return err;
|
| 2060 |
|
| 2061 | last_off = pg_start + expanded - 1;
|
| 2062 |
|
| 2063 |
|
| 2064 | new_size = (last_off == pg_end) ? offset + len :
|
| 2065 | (loff_t)(last_off + 1) << PAGE_SHIFT;
|
| 2066 | } else {
|
| 2067 | new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
|
| 2068 | }
|
| 2069 |
|
| 2070 | if (new_size > i_size_read(inode)) {
|
| 2071 | if (mode & FALLOC_FL_KEEP_SIZE)
|
| 2072 | file_set_keep_isize(inode);
|
| 2073 | else
|
| 2074 | f2fs_i_size_write(inode, new_size);
|
| 2075 | }
|
| 2076 |
|
| 2077 | return err;
|
| 2078 | }
|
| 2079 |
|
| 2080 | static long f2fs_fallocate(struct file *file, int mode,
|
| 2081 | loff_t offset, loff_t len)
|
| 2082 | {
|
| 2083 | struct inode *inode = file_inode(file);
|
| 2084 | long ret = 0;
|
| 2085 |
|
| 2086 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
|
| 2087 | return -EIO;
|
| 2088 | if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
|
| 2089 | return -ENOSPC;
|
| 2090 | if (!f2fs_is_compress_backend_ready(inode) || IS_DEVICE_ALIASING(inode))
|
| 2091 | return -EOPNOTSUPP;
|
| 2092 |
|
| 2093 |
|
| 2094 | if (!S_ISREG(inode->i_mode))
|
| 2095 | return -EINVAL;
|
| 2096 |
|
| 2097 | if (IS_ENCRYPTED(inode) &&
|
| 2098 | (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
|
| 2099 | return -EOPNOTSUPP;
|
| 2100 |
|
| 2101 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
|
| 2102 | FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
|
| 2103 | FALLOC_FL_INSERT_RANGE))
|
| 2104 | return -EOPNOTSUPP;
|
| 2105 |
|
| 2106 | inode_lock(inode);
|
| 2107 |
|
| 2108 | |
| 2109 | * Pinned file should not support partial truncation since the block
|
| 2110 | * can be used by applications.
|
| 2111 |
|
| 2112 | if ((f2fs_compressed_file(inode) || f2fs_is_pinned_file(inode)) &&
|
| 2113 | (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
|
| 2114 | FALLOC_FL_ZERO_RANGE | FALLOC_FL_INSERT_RANGE))) {
|
| 2115 | ret = -EOPNOTSUPP;
|
| 2116 | goto out;
|
| 2117 | }
|
| 2118 |
|
| 2119 | ret = file_modified(file);
|
| 2120 | if (ret)
|
| 2121 | goto out;
|
| 2122 |
|
| 2123 | |
| 2124 | * wait for inflight dio, blocks should be removed after IO
|
| 2125 | * completion.
|
| 2126 |
|
| 2127 | inode_dio_wait(inode);
|
| 2128 |
|
| 2129 | if (mode & FALLOC_FL_PUNCH_HOLE) {
|
| 2130 | if (offset >= inode->i_size)
|
| 2131 | goto out;
|
| 2132 |
|
| 2133 | ret = f2fs_punch_hole(inode, offset, len);
|
| 2134 | } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
|
| 2135 | ret = f2fs_collapse_range(inode, offset, len);
|
| 2136 | } else if (mode & FALLOC_FL_ZERO_RANGE) {
|
| 2137 | ret = f2fs_zero_range(inode, offset, len, mode);
|
| 2138 | } else if (mode & FALLOC_FL_INSERT_RANGE) {
|
| 2139 | ret = f2fs_insert_range(inode, offset, len);
|
| 2140 | } else {
|
| 2141 | ret = f2fs_expand_inode_data(inode, offset, len, mode);
|
| 2142 | }
|
| 2143 |
|
| 2144 | if (!ret) {
|
| 2145 | inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
|
| 2146 | f2fs_mark_inode_dirty_sync(inode, false);
|
| 2147 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
|
| 2148 | }
|
| 2149 |
|
| 2150 | out:
|
| 2151 | inode_unlock(inode);
|
| 2152 |
|
| 2153 | trace_f2fs_fallocate(inode, mode, offset, len, ret);
|
| 2154 | return ret;
|
| 2155 | }
|
| 2156 |
|
| 2157 | static int f2fs_release_file(struct inode *inode, struct file *filp)
|
| 2158 | {
|
| 2159 | if (atomic_dec_and_test(&F2FS_I(inode)->open_count))
|
| 2160 | f2fs_remove_donate_inode(inode);
|
| 2161 |
|
| 2162 | |
| 2163 | * f2fs_release_file is called at every close calls. So we should
|
| 2164 | * not drop any inmemory pages by close called by other process.
|
| 2165 |
|
| 2166 | if (!(filp->f_mode & FMODE_WRITE) ||
|
| 2167 | atomic_read(&inode->i_writecount) != 1)
|
| 2168 | return 0;
|
| 2169 |
|
| 2170 | inode_lock(inode);
|
| 2171 | f2fs_abort_atomic_write(inode, true);
|
| 2172 | inode_unlock(inode);
|
| 2173 |
|
| 2174 | return 0;
|
| 2175 | }
|
| 2176 |
|
| 2177 | static int f2fs_file_flush(struct file *file, fl_owner_t id)
|
| 2178 | {
|
| 2179 | struct inode *inode = file_inode(file);
|
| 2180 |
|
| 2181 | |
| 2182 | * If the process doing a transaction is crashed, we should do
|
| 2183 | * roll-back. Otherwise, other reader/write can see corrupted database
|
| 2184 | * until all the writers close its file. Since this should be done
|
| 2185 | * before dropping file lock, it needs to do in ->flush.
|
| 2186 |
|
| 2187 | if (F2FS_I(inode)->atomic_write_task == current &&
|
| 2188 | (current->flags & PF_EXITING)) {
|
| 2189 | inode_lock(inode);
|
| 2190 | f2fs_abort_atomic_write(inode, true);
|
| 2191 | inode_unlock(inode);
|
| 2192 | }
|
| 2193 |
|
| 2194 | return 0;
|
| 2195 | }
|
| 2196 |
|
| 2197 | static int f2fs_setflags_common(struct inode *inode, u32 iflags, u32 mask)
|
| 2198 | {
|
| 2199 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 2200 | u32 masked_flags = fi->i_flags & mask;
|
| 2201 |
|
| 2202 |
|
| 2203 | iflags &= mask;
|
| 2204 |
|
| 2205 |
|
| 2206 | if (IS_NOQUOTA(inode))
|
| 2207 | return -EPERM;
|
| 2208 |
|
| 2209 | if (IS_DEVICE_ALIASING(inode))
|
| 2210 | return -EPERM;
|
| 2211 |
|
| 2212 | if ((iflags ^ masked_flags) & F2FS_CASEFOLD_FL) {
|
| 2213 | if (!f2fs_sb_has_casefold(F2FS_I_SB(inode)))
|
| 2214 | return -EOPNOTSUPP;
|
| 2215 | if (!f2fs_empty_dir(inode))
|
| 2216 | return -ENOTEMPTY;
|
| 2217 | }
|
| 2218 |
|
| 2219 | if (iflags & (F2FS_COMPR_FL | F2FS_NOCOMP_FL)) {
|
| 2220 | if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
|
| 2221 | return -EOPNOTSUPP;
|
| 2222 | if ((iflags & F2FS_COMPR_FL) && (iflags & F2FS_NOCOMP_FL))
|
| 2223 | return -EINVAL;
|
| 2224 | }
|
| 2225 |
|
| 2226 | if ((iflags ^ masked_flags) & F2FS_COMPR_FL) {
|
| 2227 | if (masked_flags & F2FS_COMPR_FL) {
|
| 2228 | if (!f2fs_disable_compressed_file(inode))
|
| 2229 | return -EINVAL;
|
| 2230 | } else {
|
| 2231 |
|
| 2232 | int err = f2fs_convert_inline_inode(inode);
|
| 2233 | if (err)
|
| 2234 | return err;
|
| 2235 |
|
| 2236 | f2fs_down_write(&fi->i_sem);
|
| 2237 | if (!f2fs_may_compress(inode) ||
|
| 2238 | atomic_read(&fi->writeback) ||
|
| 2239 | (S_ISREG(inode->i_mode) &&
|
| 2240 | F2FS_HAS_BLOCKS(inode))) {
|
| 2241 | f2fs_up_write(&fi->i_sem);
|
| 2242 | return -EINVAL;
|
| 2243 | }
|
| 2244 | err = set_compress_context(inode);
|
| 2245 | f2fs_up_write(&fi->i_sem);
|
| 2246 |
|
| 2247 | if (err)
|
| 2248 | return err;
|
| 2249 | }
|
| 2250 | }
|
| 2251 |
|
| 2252 | fi->i_flags = iflags | (fi->i_flags & ~mask);
|
| 2253 | f2fs_bug_on(F2FS_I_SB(inode), (fi->i_flags & F2FS_COMPR_FL) &&
|
| 2254 | (fi->i_flags & F2FS_NOCOMP_FL));
|
| 2255 |
|
| 2256 | if (fi->i_flags & F2FS_PROJINHERIT_FL)
|
| 2257 | set_inode_flag(inode, FI_PROJ_INHERIT);
|
| 2258 | else
|
| 2259 | clear_inode_flag(inode, FI_PROJ_INHERIT);
|
| 2260 |
|
| 2261 | inode_set_ctime_current(inode);
|
| 2262 | f2fs_set_inode_flags(inode);
|
| 2263 | f2fs_mark_inode_dirty_sync(inode, true);
|
| 2264 | return 0;
|
| 2265 | }
|
| 2266 |
|
| 2267 |
|
| 2268 |
|
| 2269 | |
| 2270 | * To make a new on-disk f2fs i_flag gettable via FS_IOC_GETFLAGS, add an entry
|
| 2271 | * for it to f2fs_fsflags_map[], and add its FS_*_FL equivalent to
|
| 2272 | * F2FS_GETTABLE_FS_FL. To also make it settable via FS_IOC_SETFLAGS, also add
|
| 2273 | * its FS_*_FL equivalent to F2FS_SETTABLE_FS_FL.
|
| 2274 | *
|
| 2275 | * Translating flags to fsx_flags value used by FS_IOC_FSGETXATTR and
|
| 2276 | * FS_IOC_FSSETXATTR is done by the VFS.
|
| 2277 |
|
| 2278 |
|
| 2279 | static const struct {
|
| 2280 | u32 iflag;
|
| 2281 | u32 fsflag;
|
| 2282 | } f2fs_fsflags_map[] = {
|
| 2283 | { F2FS_COMPR_FL, FS_COMPR_FL },
|
| 2284 | { F2FS_SYNC_FL, FS_SYNC_FL },
|
| 2285 | { F2FS_IMMUTABLE_FL, FS_IMMUTABLE_FL },
|
| 2286 | { F2FS_APPEND_FL, FS_APPEND_FL },
|
| 2287 | { F2FS_NODUMP_FL, FS_NODUMP_FL },
|
| 2288 | { F2FS_NOATIME_FL, FS_NOATIME_FL },
|
| 2289 | { F2FS_NOCOMP_FL, FS_NOCOMP_FL },
|
| 2290 | { F2FS_INDEX_FL, FS_INDEX_FL },
|
| 2291 | { F2FS_DIRSYNC_FL, FS_DIRSYNC_FL },
|
| 2292 | { F2FS_PROJINHERIT_FL, FS_PROJINHERIT_FL },
|
| 2293 | { F2FS_CASEFOLD_FL, FS_CASEFOLD_FL },
|
| 2294 | };
|
| 2295 |
|
| 2296 | #define F2FS_GETTABLE_FS_FL ( \
|
| 2297 | FS_COMPR_FL | \
|
| 2298 | FS_SYNC_FL | \
|
| 2299 | FS_IMMUTABLE_FL | \
|
| 2300 | FS_APPEND_FL | \
|
| 2301 | FS_NODUMP_FL | \
|
| 2302 | FS_NOATIME_FL | \
|
| 2303 | FS_NOCOMP_FL | \
|
| 2304 | FS_INDEX_FL | \
|
| 2305 | FS_DIRSYNC_FL | \
|
| 2306 | FS_PROJINHERIT_FL | \
|
| 2307 | FS_ENCRYPT_FL | \
|
| 2308 | FS_INLINE_DATA_FL | \
|
| 2309 | FS_NOCOW_FL | \
|
| 2310 | FS_VERITY_FL | \
|
| 2311 | FS_CASEFOLD_FL)
|
| 2312 |
|
| 2313 | #define F2FS_SETTABLE_FS_FL ( \
|
| 2314 | FS_COMPR_FL | \
|
| 2315 | FS_SYNC_FL | \
|
| 2316 | FS_IMMUTABLE_FL | \
|
| 2317 | FS_APPEND_FL | \
|
| 2318 | FS_NODUMP_FL | \
|
| 2319 | FS_NOATIME_FL | \
|
| 2320 | FS_NOCOMP_FL | \
|
| 2321 | FS_DIRSYNC_FL | \
|
| 2322 | FS_PROJINHERIT_FL | \
|
| 2323 | FS_CASEFOLD_FL)
|
| 2324 |
|
| 2325 |
|
| 2326 | static inline u32 f2fs_iflags_to_fsflags(u32 iflags)
|
| 2327 | {
|
| 2328 | u32 fsflags = 0;
|
| 2329 | int i;
|
| 2330 |
|
| 2331 | for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
|
| 2332 | if (iflags & f2fs_fsflags_map[i].iflag)
|
| 2333 | fsflags |= f2fs_fsflags_map[i].fsflag;
|
| 2334 |
|
| 2335 | return fsflags;
|
| 2336 | }
|
| 2337 |
|
| 2338 |
|
| 2339 | static inline u32 f2fs_fsflags_to_iflags(u32 fsflags)
|
| 2340 | {
|
| 2341 | u32 iflags = 0;
|
| 2342 | int i;
|
| 2343 |
|
| 2344 | for (i = 0; i < ARRAY_SIZE(f2fs_fsflags_map); i++)
|
| 2345 | if (fsflags & f2fs_fsflags_map[i].fsflag)
|
| 2346 | iflags |= f2fs_fsflags_map[i].iflag;
|
| 2347 |
|
| 2348 | return iflags;
|
| 2349 | }
|
| 2350 |
|
| 2351 | static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
|
| 2352 | {
|
| 2353 | struct inode *inode = file_inode(filp);
|
| 2354 |
|
| 2355 | return put_user(inode->i_generation, (int __user *)arg);
|
| 2356 | }
|
| 2357 |
|
| 2358 | static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)
|
| 2359 | {
|
| 2360 | struct inode *inode = file_inode(filp);
|
| 2361 | const struct mnt_idmap *idmap = file_mnt_idmap(filp);
|
| 2362 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 2363 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 2364 | loff_t isize;
|
| 2365 | int ret;
|
| 2366 |
|
| 2367 | if (!(filp->f_mode & FMODE_WRITE))
|
| 2368 | return -EBADF;
|
| 2369 |
|
| 2370 | if (!inode_owner_or_capable(idmap, inode))
|
| 2371 | return -EACCES;
|
| 2372 |
|
| 2373 | if (!S_ISREG(inode->i_mode))
|
| 2374 | return -EINVAL;
|
| 2375 |
|
| 2376 | if (filp->f_flags & O_DIRECT)
|
| 2377 | return -EINVAL;
|
| 2378 |
|
| 2379 | ret = mnt_want_write_file(filp);
|
| 2380 | if (ret)
|
| 2381 | return ret;
|
| 2382 |
|
| 2383 | inode_lock(inode);
|
| 2384 |
|
| 2385 | if (!f2fs_disable_compressed_file(inode) ||
|
| 2386 | f2fs_is_pinned_file(inode)) {
|
| 2387 | ret = -EINVAL;
|
| 2388 | goto out;
|
| 2389 | }
|
| 2390 |
|
| 2391 | if (f2fs_is_atomic_file(inode))
|
| 2392 | goto out;
|
| 2393 |
|
| 2394 | ret = f2fs_convert_inline_inode(inode);
|
| 2395 | if (ret)
|
| 2396 | goto out;
|
| 2397 |
|
| 2398 | f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
|
| 2399 | f2fs_down_write(&fi->i_gc_rwsem[READ]);
|
| 2400 |
|
| 2401 | |
| 2402 | * Should wait end_io to count F2FS_WB_CP_DATA correctly by
|
| 2403 | * f2fs_is_atomic_file.
|
| 2404 |
|
| 2405 | if (get_dirty_pages(inode))
|
| 2406 | f2fs_warn(sbi, "Unexpected flush for atomic writes: ino=%llu, npages=%u",
|
| 2407 | inode->i_ino, get_dirty_pages(inode));
|
| 2408 | ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
|
| 2409 | if (ret)
|
| 2410 | goto out_unlock;
|
| 2411 |
|
| 2412 |
|
| 2413 | if (fi->cow_inode == NULL) {
|
| 2414 |
|
| 2415 | struct dentry *dentry = file_dentry(filp);
|
| 2416 | struct inode *dir = d_inode(dentry->d_parent);
|
| 2417 |
|
| 2418 | ret = f2fs_get_tmpfile(idmap, dir, &fi->cow_inode);
|
| 2419 | if (ret)
|
| 2420 | goto out_unlock;
|
| 2421 |
|
| 2422 | set_inode_flag(fi->cow_inode, FI_COW_FILE);
|
| 2423 | clear_inode_flag(fi->cow_inode, FI_INLINE_DATA);
|
| 2424 |
|
| 2425 |
|
| 2426 | F2FS_I(fi->cow_inode)->atomic_inode = inode;
|
| 2427 | } else {
|
| 2428 |
|
| 2429 | f2fs_bug_on(sbi, get_dirty_pages(fi->cow_inode));
|
| 2430 |
|
| 2431 | invalidate_mapping_pages(fi->cow_inode->i_mapping, 0, -1);
|
| 2432 |
|
| 2433 | ret = f2fs_do_truncate_blocks(fi->cow_inode, 0, true);
|
| 2434 | if (ret)
|
| 2435 | goto out_unlock;
|
| 2436 | }
|
| 2437 |
|
| 2438 | f2fs_write_inode(inode, NULL);
|
| 2439 |
|
| 2440 | stat_inc_atomic_inode(inode);
|
| 2441 |
|
| 2442 | set_inode_flag(inode, FI_ATOMIC_FILE);
|
| 2443 |
|
| 2444 | isize = i_size_read(inode);
|
| 2445 | fi->original_i_size = isize;
|
| 2446 | if (truncate) {
|
| 2447 | set_inode_flag(inode, FI_ATOMIC_REPLACE);
|
| 2448 | truncate_inode_pages_final(inode->i_mapping);
|
| 2449 | f2fs_i_size_write(inode, 0);
|
| 2450 | isize = 0;
|
| 2451 | }
|
| 2452 | f2fs_i_size_write(fi->cow_inode, isize);
|
| 2453 |
|
| 2454 | out_unlock:
|
| 2455 | f2fs_up_write(&fi->i_gc_rwsem[READ]);
|
| 2456 | f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
|
| 2457 | if (ret)
|
| 2458 | goto out;
|
| 2459 |
|
| 2460 | f2fs_update_time(sbi, REQ_TIME);
|
| 2461 | fi->atomic_write_task = current;
|
| 2462 | stat_update_max_atomic_write(inode);
|
| 2463 | fi->atomic_write_cnt = 0;
|
| 2464 | out:
|
| 2465 | inode_unlock(inode);
|
| 2466 | mnt_drop_write_file(filp);
|
| 2467 | return ret;
|
| 2468 | }
|
| 2469 |
|
| 2470 | static int f2fs_ioc_commit_atomic_write(struct file *filp)
|
| 2471 | {
|
| 2472 | struct inode *inode = file_inode(filp);
|
| 2473 | const struct mnt_idmap *idmap = file_mnt_idmap(filp);
|
| 2474 | int ret;
|
| 2475 |
|
| 2476 | if (!(filp->f_mode & FMODE_WRITE))
|
| 2477 | return -EBADF;
|
| 2478 |
|
| 2479 | if (!inode_owner_or_capable(idmap, inode))
|
| 2480 | return -EACCES;
|
| 2481 |
|
| 2482 | ret = mnt_want_write_file(filp);
|
| 2483 | if (ret)
|
| 2484 | return ret;
|
| 2485 |
|
| 2486 | f2fs_balance_fs(F2FS_I_SB(inode), true);
|
| 2487 |
|
| 2488 | inode_lock(inode);
|
| 2489 |
|
| 2490 | if (f2fs_is_atomic_file(inode)) {
|
| 2491 | ret = f2fs_commit_atomic_write(inode);
|
| 2492 | if (!ret)
|
| 2493 | ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
|
| 2494 |
|
| 2495 | f2fs_abort_atomic_write(inode, ret);
|
| 2496 | } else {
|
| 2497 | ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
|
| 2498 | }
|
| 2499 |
|
| 2500 | inode_unlock(inode);
|
| 2501 | mnt_drop_write_file(filp);
|
| 2502 | return ret;
|
| 2503 | }
|
| 2504 |
|
| 2505 | static int f2fs_ioc_abort_atomic_write(struct file *filp)
|
| 2506 | {
|
| 2507 | struct inode *inode = file_inode(filp);
|
| 2508 | const struct mnt_idmap *idmap = file_mnt_idmap(filp);
|
| 2509 | int ret;
|
| 2510 |
|
| 2511 | if (!(filp->f_mode & FMODE_WRITE))
|
| 2512 | return -EBADF;
|
| 2513 |
|
| 2514 | if (!inode_owner_or_capable(idmap, inode))
|
| 2515 | return -EACCES;
|
| 2516 |
|
| 2517 | ret = mnt_want_write_file(filp);
|
| 2518 | if (ret)
|
| 2519 | return ret;
|
| 2520 |
|
| 2521 | inode_lock(inode);
|
| 2522 |
|
| 2523 | f2fs_abort_atomic_write(inode, true);
|
| 2524 |
|
| 2525 | inode_unlock(inode);
|
| 2526 |
|
| 2527 | mnt_drop_write_file(filp);
|
| 2528 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
|
| 2529 | return ret;
|
| 2530 | }
|
| 2531 |
|
| 2532 | int f2fs_do_shutdown(struct f2fs_sb_info *sbi, unsigned int flag,
|
| 2533 | bool readonly, bool need_lock)
|
| 2534 | {
|
| 2535 | struct super_block *sb = sbi->sb;
|
| 2536 | int ret = 0;
|
| 2537 |
|
| 2538 | switch (flag) {
|
| 2539 | case F2FS_GOING_DOWN_FULLSYNC:
|
| 2540 | ret = bdev_freeze(sb->s_bdev);
|
| 2541 | if (ret)
|
| 2542 | goto out;
|
| 2543 | f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
|
| 2544 | bdev_thaw(sb->s_bdev);
|
| 2545 | break;
|
| 2546 | case F2FS_GOING_DOWN_METASYNC:
|
| 2547 |
|
| 2548 | ret = f2fs_sync_fs(sb, 1);
|
| 2549 | if (ret) {
|
| 2550 | if (ret == -EIO)
|
| 2551 | ret = 0;
|
| 2552 | goto out;
|
| 2553 | }
|
| 2554 | f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
|
| 2555 | break;
|
| 2556 | case F2FS_GOING_DOWN_NOSYNC:
|
| 2557 | f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
|
| 2558 | break;
|
| 2559 | case F2FS_GOING_DOWN_METAFLUSH:
|
| 2560 | f2fs_sync_meta_pages(sbi, LONG_MAX, FS_META_IO);
|
| 2561 | f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_SHUTDOWN);
|
| 2562 | break;
|
| 2563 | case F2FS_GOING_DOWN_NEED_FSCK:
|
| 2564 | set_sbi_flag(sbi, SBI_NEED_FSCK);
|
| 2565 | set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
|
| 2566 | set_sbi_flag(sbi, SBI_IS_DIRTY);
|
| 2567 |
|
| 2568 | ret = f2fs_sync_fs(sb, 1);
|
| 2569 | if (ret == -EIO)
|
| 2570 | ret = 0;
|
| 2571 | goto out;
|
| 2572 | default:
|
| 2573 | ret = -EINVAL;
|
| 2574 | goto out;
|
| 2575 | }
|
| 2576 |
|
| 2577 | if (readonly)
|
| 2578 | goto out;
|
| 2579 |
|
| 2580 | |
| 2581 | * grab sb->s_umount to avoid racing w/ remount() and other shutdown
|
| 2582 | * paths.
|
| 2583 |
|
| 2584 | if (need_lock)
|
| 2585 | down_write(&sbi->sb->s_umount);
|
| 2586 |
|
| 2587 | f2fs_stop_gc_thread(sbi);
|
| 2588 | f2fs_stop_discard_thread(sbi);
|
| 2589 |
|
| 2590 | f2fs_drop_discard_cmd(sbi);
|
| 2591 | clear_opt(sbi, DISCARD);
|
| 2592 |
|
| 2593 | if (need_lock)
|
| 2594 | up_write(&sbi->sb->s_umount);
|
| 2595 |
|
| 2596 | f2fs_update_time(sbi, REQ_TIME);
|
| 2597 | out:
|
| 2598 |
|
| 2599 | trace_f2fs_shutdown(sbi, flag, ret);
|
| 2600 |
|
| 2601 | return ret;
|
| 2602 | }
|
| 2603 |
|
| 2604 | static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
|
| 2605 | {
|
| 2606 | struct inode *inode = file_inode(filp);
|
| 2607 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 2608 | __u32 in;
|
| 2609 | int ret;
|
| 2610 | bool need_drop = false, readonly = false;
|
| 2611 |
|
| 2612 | if (!capable(CAP_SYS_ADMIN))
|
| 2613 | return -EPERM;
|
| 2614 |
|
| 2615 | if (get_user(in, (__u32 __user *)arg))
|
| 2616 | return -EFAULT;
|
| 2617 |
|
| 2618 | if (in != F2FS_GOING_DOWN_FULLSYNC) {
|
| 2619 | ret = mnt_want_write_file(filp);
|
| 2620 | if (ret) {
|
| 2621 | if (ret != -EROFS)
|
| 2622 | return ret;
|
| 2623 |
|
| 2624 |
|
| 2625 | in = F2FS_GOING_DOWN_NOSYNC;
|
| 2626 | readonly = true;
|
| 2627 | } else {
|
| 2628 | need_drop = true;
|
| 2629 | }
|
| 2630 | }
|
| 2631 |
|
| 2632 | ret = f2fs_do_shutdown(sbi, in, readonly, true);
|
| 2633 |
|
| 2634 | if (need_drop)
|
| 2635 | mnt_drop_write_file(filp);
|
| 2636 |
|
| 2637 | return ret;
|
| 2638 | }
|
| 2639 |
|
| 2640 | static int f2fs_keep_noreuse_range(struct inode *inode,
|
| 2641 | loff_t offset, loff_t len)
|
| 2642 | {
|
| 2643 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 2644 | u64 max_bytes = F2FS_BLK_TO_BYTES(max_file_blocks(inode));
|
| 2645 | u64 start, end;
|
| 2646 | int ret = 0;
|
| 2647 |
|
| 2648 | if (!S_ISREG(inode->i_mode))
|
| 2649 | return 0;
|
| 2650 |
|
| 2651 | if (offset >= max_bytes || len > max_bytes ||
|
| 2652 | (offset + len) > max_bytes)
|
| 2653 | return 0;
|
| 2654 |
|
| 2655 | start = offset >> PAGE_SHIFT;
|
| 2656 | end = DIV_ROUND_UP(offset + len, PAGE_SIZE);
|
| 2657 |
|
| 2658 | inode_lock(inode);
|
| 2659 | if (f2fs_is_atomic_file(inode)) {
|
| 2660 | inode_unlock(inode);
|
| 2661 | return 0;
|
| 2662 | }
|
| 2663 |
|
| 2664 | spin_lock(&sbi->inode_lock[DONATE_INODE]);
|
| 2665 |
|
| 2666 | if (!len) {
|
| 2667 | if (!list_empty(&F2FS_I(inode)->gdonate_list)) {
|
| 2668 | list_del_init(&F2FS_I(inode)->gdonate_list);
|
| 2669 | sbi->donate_files--;
|
| 2670 | if (is_inode_flag_set(inode, FI_DONATE_FINISHED))
|
| 2671 | ret = -EALREADY;
|
| 2672 | else
|
| 2673 | set_inode_flag(inode, FI_DONATE_FINISHED);
|
| 2674 | } else
|
| 2675 | ret = -ENOENT;
|
| 2676 | } else {
|
| 2677 | if (list_empty(&F2FS_I(inode)->gdonate_list)) {
|
| 2678 | list_add_tail(&F2FS_I(inode)->gdonate_list,
|
| 2679 | &sbi->inode_list[DONATE_INODE]);
|
| 2680 | sbi->donate_files++;
|
| 2681 | } else {
|
| 2682 | list_move_tail(&F2FS_I(inode)->gdonate_list,
|
| 2683 | &sbi->inode_list[DONATE_INODE]);
|
| 2684 | }
|
| 2685 | F2FS_I(inode)->donate_start = start;
|
| 2686 | F2FS_I(inode)->donate_end = end - 1;
|
| 2687 | clear_inode_flag(inode, FI_DONATE_FINISHED);
|
| 2688 | }
|
| 2689 | spin_unlock(&sbi->inode_lock[DONATE_INODE]);
|
| 2690 | inode_unlock(inode);
|
| 2691 |
|
| 2692 | return ret;
|
| 2693 | }
|
| 2694 |
|
| 2695 | static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
|
| 2696 | {
|
| 2697 | struct inode *inode = file_inode(filp);
|
| 2698 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 2699 | struct fstrim_range range;
|
| 2700 | int ret;
|
| 2701 |
|
| 2702 | if (!capable(CAP_SYS_ADMIN))
|
| 2703 | return -EPERM;
|
| 2704 |
|
| 2705 | if (!f2fs_hw_support_discard(sbi))
|
| 2706 | return -EOPNOTSUPP;
|
| 2707 |
|
| 2708 | if (copy_from_user(&range, (struct fstrim_range __user *)arg,
|
| 2709 | sizeof(range)))
|
| 2710 | return -EFAULT;
|
| 2711 |
|
| 2712 | ret = mnt_want_write_file(filp);
|
| 2713 | if (ret)
|
| 2714 | return ret;
|
| 2715 |
|
| 2716 | range.minlen = max_t(unsigned int, range.minlen,
|
| 2717 | f2fs_hw_discard_granularity(sbi));
|
| 2718 | ret = f2fs_trim_fs(sbi, &range);
|
| 2719 | mnt_drop_write_file(filp);
|
| 2720 | if (ret < 0)
|
| 2721 | return ret;
|
| 2722 |
|
| 2723 | if (copy_to_user((struct fstrim_range __user *)arg, &range,
|
| 2724 | sizeof(range)))
|
| 2725 | return -EFAULT;
|
| 2726 | f2fs_update_time(sbi, REQ_TIME);
|
| 2727 | return 0;
|
| 2728 | }
|
| 2729 |
|
| 2730 | static bool uuid_is_nonzero(__u8 u[16])
|
| 2731 | {
|
| 2732 | int i;
|
| 2733 |
|
| 2734 | for (i = 0; i < 16; i++)
|
| 2735 | if (u[i])
|
| 2736 | return true;
|
| 2737 | return false;
|
| 2738 | }
|
| 2739 |
|
| 2740 | static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
|
| 2741 | {
|
| 2742 | struct inode *inode = file_inode(filp);
|
| 2743 | int ret;
|
| 2744 |
|
| 2745 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(inode)))
|
| 2746 | return -EOPNOTSUPP;
|
| 2747 |
|
| 2748 | ret = fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
|
| 2749 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
|
| 2750 | return ret;
|
| 2751 | }
|
| 2752 |
|
| 2753 | static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
|
| 2754 | {
|
| 2755 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
|
| 2756 | return -EOPNOTSUPP;
|
| 2757 | return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
|
| 2758 | }
|
| 2759 |
|
| 2760 | static int f2fs_ioc_get_dev_alias_status(struct file *filp, unsigned long arg)
|
| 2761 | {
|
| 2762 | struct inode *inode = file_inode(filp);
|
| 2763 |
|
| 2764 | if (!IS_DEVICE_ALIASING(inode))
|
| 2765 | return -EINVAL;
|
| 2766 |
|
| 2767 | return put_user(F2FS_HAS_BLOCKS(inode) ? F2FS_DEV_ALIAS_STATUS_RESERVED :
|
| 2768 | F2FS_DEV_ALIAS_STATUS_RELEASED, (u32 __user *)arg);
|
| 2769 | }
|
| 2770 |
|
| 2771 | static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
|
| 2772 | {
|
| 2773 | struct inode *inode = file_inode(filp);
|
| 2774 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 2775 | u8 encrypt_pw_salt[16];
|
| 2776 | int err;
|
| 2777 |
|
| 2778 | if (!f2fs_sb_has_encrypt(sbi))
|
| 2779 | return -EOPNOTSUPP;
|
| 2780 |
|
| 2781 | err = mnt_want_write_file(filp);
|
| 2782 | if (err)
|
| 2783 | return err;
|
| 2784 |
|
| 2785 | f2fs_down_write(&sbi->sb_lock);
|
| 2786 |
|
| 2787 | if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
|
| 2788 | goto got_it;
|
| 2789 |
|
| 2790 |
|
| 2791 | generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
|
| 2792 |
|
| 2793 | err = f2fs_commit_super(sbi, false);
|
| 2794 | if (err) {
|
| 2795 |
|
| 2796 | memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
|
| 2797 | goto out_err;
|
| 2798 | }
|
| 2799 | got_it:
|
| 2800 | memcpy(encrypt_pw_salt, sbi->raw_super->encrypt_pw_salt, 16);
|
| 2801 | out_err:
|
| 2802 | f2fs_up_write(&sbi->sb_lock);
|
| 2803 | mnt_drop_write_file(filp);
|
| 2804 |
|
| 2805 | if (!err && copy_to_user((__u8 __user *)arg, encrypt_pw_salt, 16))
|
| 2806 | err = -EFAULT;
|
| 2807 |
|
| 2808 | return err;
|
| 2809 | }
|
| 2810 |
|
| 2811 | static int f2fs_ioc_get_encryption_policy_ex(struct file *filp,
|
| 2812 | unsigned long arg)
|
| 2813 | {
|
| 2814 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
|
| 2815 | return -EOPNOTSUPP;
|
| 2816 |
|
| 2817 | return fscrypt_ioctl_get_policy_ex(filp, (void __user *)arg);
|
| 2818 | }
|
| 2819 |
|
| 2820 | static int f2fs_ioc_add_encryption_key(struct file *filp, unsigned long arg)
|
| 2821 | {
|
| 2822 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
|
| 2823 | return -EOPNOTSUPP;
|
| 2824 |
|
| 2825 | return fscrypt_ioctl_add_key(filp, (void __user *)arg);
|
| 2826 | }
|
| 2827 |
|
| 2828 | static int f2fs_ioc_remove_encryption_key(struct file *filp, unsigned long arg)
|
| 2829 | {
|
| 2830 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
|
| 2831 | return -EOPNOTSUPP;
|
| 2832 |
|
| 2833 | return fscrypt_ioctl_remove_key(filp, (void __user *)arg);
|
| 2834 | }
|
| 2835 |
|
| 2836 | static int f2fs_ioc_remove_encryption_key_all_users(struct file *filp,
|
| 2837 | unsigned long arg)
|
| 2838 | {
|
| 2839 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
|
| 2840 | return -EOPNOTSUPP;
|
| 2841 |
|
| 2842 | return fscrypt_ioctl_remove_key_all_users(filp, (void __user *)arg);
|
| 2843 | }
|
| 2844 |
|
| 2845 | static int f2fs_ioc_get_encryption_key_status(struct file *filp,
|
| 2846 | unsigned long arg)
|
| 2847 | {
|
| 2848 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
|
| 2849 | return -EOPNOTSUPP;
|
| 2850 |
|
| 2851 | return fscrypt_ioctl_get_key_status(filp, (void __user *)arg);
|
| 2852 | }
|
| 2853 |
|
| 2854 | static int f2fs_ioc_get_encryption_nonce(struct file *filp, unsigned long arg)
|
| 2855 | {
|
| 2856 | if (!f2fs_sb_has_encrypt(F2FS_I_SB(file_inode(filp))))
|
| 2857 | return -EOPNOTSUPP;
|
| 2858 |
|
| 2859 | return fscrypt_ioctl_get_nonce(filp, (void __user *)arg);
|
| 2860 | }
|
| 2861 |
|
| 2862 | static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
|
| 2863 | {
|
| 2864 | struct inode *inode = file_inode(filp);
|
| 2865 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 2866 | struct f2fs_gc_control gc_control = { .victim_segno = NULL_SEGNO,
|
| 2867 | .no_bg_gc = false,
|
| 2868 | .should_migrate_blocks = false,
|
| 2869 | .nr_free_secs = 0 };
|
| 2870 | __u32 sync;
|
| 2871 | int ret;
|
| 2872 |
|
| 2873 | if (!capable(CAP_SYS_ADMIN))
|
| 2874 | return -EPERM;
|
| 2875 |
|
| 2876 | if (get_user(sync, (__u32 __user *)arg))
|
| 2877 | return -EFAULT;
|
| 2878 |
|
| 2879 | if (f2fs_readonly(sbi->sb))
|
| 2880 | return -EROFS;
|
| 2881 |
|
| 2882 | ret = mnt_want_write_file(filp);
|
| 2883 | if (ret)
|
| 2884 | return ret;
|
| 2885 |
|
| 2886 | if (!sync) {
|
| 2887 | if (!f2fs_down_write_trylock_trace(&sbi->gc_lock,
|
| 2888 | &gc_control.lc)) {
|
| 2889 | ret = -EBUSY;
|
| 2890 | goto out;
|
| 2891 | }
|
| 2892 | } else {
|
| 2893 | f2fs_down_write_trace(&sbi->gc_lock, &gc_control.lc);
|
| 2894 | }
|
| 2895 |
|
| 2896 | gc_control.init_gc_type = sync ? FG_GC : BG_GC;
|
| 2897 | gc_control.err_gc_skipped = sync;
|
| 2898 | stat_inc_gc_call_count(sbi, FOREGROUND);
|
| 2899 | ret = f2fs_gc(sbi, &gc_control);
|
| 2900 | out:
|
| 2901 | mnt_drop_write_file(filp);
|
| 2902 | return ret;
|
| 2903 | }
|
| 2904 |
|
| 2905 | static int __f2fs_ioc_gc_range(struct file *filp, struct f2fs_gc_range *range)
|
| 2906 | {
|
| 2907 | struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
|
| 2908 | struct f2fs_gc_control gc_control = {
|
| 2909 | .init_gc_type = range->sync ? FG_GC : BG_GC,
|
| 2910 | .no_bg_gc = false,
|
| 2911 | .should_migrate_blocks = false,
|
| 2912 | .err_gc_skipped = range->sync,
|
| 2913 | .nr_free_secs = 0 };
|
| 2914 | u64 end;
|
| 2915 | int ret;
|
| 2916 |
|
| 2917 | if (!capable(CAP_SYS_ADMIN))
|
| 2918 | return -EPERM;
|
| 2919 | if (f2fs_readonly(sbi->sb))
|
| 2920 | return -EROFS;
|
| 2921 |
|
| 2922 | end = range->start + range->len;
|
| 2923 | if (end < range->start || range->start < MAIN_BLKADDR(sbi) ||
|
| 2924 | end >= MAX_BLKADDR(sbi))
|
| 2925 | return -EINVAL;
|
| 2926 |
|
| 2927 | ret = mnt_want_write_file(filp);
|
| 2928 | if (ret)
|
| 2929 | return ret;
|
| 2930 |
|
| 2931 | do_more:
|
| 2932 | if (!range->sync) {
|
| 2933 | if (!f2fs_down_write_trylock_trace(&sbi->gc_lock, &gc_control.lc)) {
|
| 2934 | ret = -EBUSY;
|
| 2935 | goto out;
|
| 2936 | }
|
| 2937 | } else {
|
| 2938 | f2fs_down_write_trace(&sbi->gc_lock, &gc_control.lc);
|
| 2939 | }
|
| 2940 |
|
| 2941 | gc_control.victim_segno = GET_SEGNO(sbi, range->start);
|
| 2942 | stat_inc_gc_call_count(sbi, FOREGROUND);
|
| 2943 | ret = f2fs_gc(sbi, &gc_control);
|
| 2944 | if (ret) {
|
| 2945 | if (ret == -EBUSY)
|
| 2946 | ret = -EAGAIN;
|
| 2947 | goto out;
|
| 2948 | }
|
| 2949 | range->start += CAP_BLKS_PER_SEC(sbi);
|
| 2950 | if (range->start <= end)
|
| 2951 | goto do_more;
|
| 2952 | out:
|
| 2953 | mnt_drop_write_file(filp);
|
| 2954 | return ret;
|
| 2955 | }
|
| 2956 |
|
| 2957 | static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
|
| 2958 | {
|
| 2959 | struct f2fs_gc_range range;
|
| 2960 |
|
| 2961 | if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
|
| 2962 | sizeof(range)))
|
| 2963 | return -EFAULT;
|
| 2964 | return __f2fs_ioc_gc_range(filp, &range);
|
| 2965 | }
|
| 2966 |
|
| 2967 | static int f2fs_ioc_write_checkpoint(struct file *filp)
|
| 2968 | {
|
| 2969 | struct inode *inode = file_inode(filp);
|
| 2970 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 2971 | int ret;
|
| 2972 |
|
| 2973 | if (!capable(CAP_SYS_ADMIN))
|
| 2974 | return -EPERM;
|
| 2975 |
|
| 2976 | if (f2fs_readonly(sbi->sb))
|
| 2977 | return -EROFS;
|
| 2978 |
|
| 2979 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
|
| 2980 | f2fs_info(sbi, "Skipping Checkpoint. Checkpoints currently disabled.");
|
| 2981 | return -EINVAL;
|
| 2982 | }
|
| 2983 |
|
| 2984 | ret = mnt_want_write_file(filp);
|
| 2985 | if (ret)
|
| 2986 | return ret;
|
| 2987 |
|
| 2988 | ret = f2fs_sync_fs(sbi->sb, 1);
|
| 2989 |
|
| 2990 | mnt_drop_write_file(filp);
|
| 2991 | return ret;
|
| 2992 | }
|
| 2993 |
|
| 2994 | static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
|
| 2995 | struct file *filp,
|
| 2996 | struct f2fs_defragment *range)
|
| 2997 | {
|
| 2998 | struct inode *inode = file_inode(filp);
|
| 2999 | struct f2fs_map_blocks map = { .m_next_extent = NULL,
|
| 3000 | .m_seg_type = NO_CHECK_TYPE,
|
| 3001 | .m_may_create = false };
|
| 3002 | struct extent_info ei = {};
|
| 3003 | pgoff_t pg_start, pg_end, next_pgofs;
|
| 3004 | unsigned int total = 0, sec_num;
|
| 3005 | block_t blk_end = 0;
|
| 3006 | bool fragmented = false;
|
| 3007 | int err;
|
| 3008 |
|
| 3009 | f2fs_balance_fs(sbi, true);
|
| 3010 |
|
| 3011 | inode_lock(inode);
|
| 3012 | pg_start = range->start >> PAGE_SHIFT;
|
| 3013 | pg_end = min_t(pgoff_t,
|
| 3014 | (range->start + range->len) >> PAGE_SHIFT,
|
| 3015 | DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE));
|
| 3016 |
|
| 3017 | if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED) ||
|
| 3018 | f2fs_is_atomic_file(inode)) {
|
| 3019 | err = -EINVAL;
|
| 3020 | goto unlock_out;
|
| 3021 | }
|
| 3022 |
|
| 3023 |
|
| 3024 | set_inode_flag(inode, FI_OPU_WRITE);
|
| 3025 | if (f2fs_should_update_inplace(inode, NULL)) {
|
| 3026 | err = -EINVAL;
|
| 3027 | goto out;
|
| 3028 | }
|
| 3029 |
|
| 3030 |
|
| 3031 | err = filemap_write_and_wait_range(inode->i_mapping,
|
| 3032 | pg_start << PAGE_SHIFT,
|
| 3033 | (pg_end << PAGE_SHIFT) - 1);
|
| 3034 | if (err)
|
| 3035 | goto out;
|
| 3036 |
|
| 3037 | |
| 3038 | * lookup mapping info in extent cache, skip defragmenting if physical
|
| 3039 | * block addresses are continuous.
|
| 3040 |
|
| 3041 | if (f2fs_lookup_read_extent_cache(inode, pg_start, &ei)) {
|
| 3042 | if ((pgoff_t)ei.fofs + ei.len >= pg_end)
|
| 3043 | goto out;
|
| 3044 | }
|
| 3045 |
|
| 3046 | map.m_lblk = pg_start;
|
| 3047 | map.m_next_pgofs = &next_pgofs;
|
| 3048 |
|
| 3049 | |
| 3050 | * lookup mapping info in dnode page cache, skip defragmenting if all
|
| 3051 | * physical block addresses are continuous even if there are hole(s)
|
| 3052 | * in logical blocks.
|
| 3053 |
|
| 3054 | while (map.m_lblk < pg_end) {
|
| 3055 | map.m_len = pg_end - map.m_lblk;
|
| 3056 | err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
|
| 3057 | if (err)
|
| 3058 | goto out;
|
| 3059 |
|
| 3060 | if (!(map.m_flags & F2FS_MAP_FLAGS)) {
|
| 3061 | map.m_lblk = next_pgofs;
|
| 3062 | continue;
|
| 3063 | }
|
| 3064 |
|
| 3065 | if (blk_end && blk_end != map.m_pblk)
|
| 3066 | fragmented = true;
|
| 3067 |
|
| 3068 |
|
| 3069 | total += map.m_len;
|
| 3070 |
|
| 3071 | blk_end = map.m_pblk + map.m_len;
|
| 3072 |
|
| 3073 | map.m_lblk += map.m_len;
|
| 3074 | }
|
| 3075 |
|
| 3076 | if (!fragmented) {
|
| 3077 | total = 0;
|
| 3078 | goto out;
|
| 3079 | }
|
| 3080 |
|
| 3081 | sec_num = DIV_ROUND_UP(total, CAP_BLKS_PER_SEC(sbi));
|
| 3082 |
|
| 3083 | |
| 3084 | * make sure there are enough free section for LFS allocation, this can
|
| 3085 | * avoid defragment running in SSR mode when free section are allocated
|
| 3086 | * intensively
|
| 3087 |
|
| 3088 | if (has_not_enough_free_secs(sbi, 0, sec_num)) {
|
| 3089 | err = -EAGAIN;
|
| 3090 | goto out;
|
| 3091 | }
|
| 3092 |
|
| 3093 | map.m_lblk = pg_start;
|
| 3094 | map.m_len = pg_end - pg_start;
|
| 3095 | total = 0;
|
| 3096 |
|
| 3097 | while (map.m_lblk < pg_end) {
|
| 3098 | pgoff_t idx;
|
| 3099 | int cnt = 0;
|
| 3100 |
|
| 3101 | do_map:
|
| 3102 | map.m_len = pg_end - map.m_lblk;
|
| 3103 | err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_DEFAULT);
|
| 3104 | if (err)
|
| 3105 | goto clear_out;
|
| 3106 |
|
| 3107 | if (!(map.m_flags & F2FS_MAP_FLAGS)) {
|
| 3108 | map.m_lblk = next_pgofs;
|
| 3109 | goto check;
|
| 3110 | }
|
| 3111 |
|
| 3112 | set_inode_flag(inode, FI_SKIP_WRITES);
|
| 3113 |
|
| 3114 | idx = map.m_lblk;
|
| 3115 | while (idx < map.m_lblk + map.m_len &&
|
| 3116 | cnt < BLKS_PER_SEG(sbi)) {
|
| 3117 | struct folio *folio;
|
| 3118 |
|
| 3119 | folio = f2fs_get_lock_data_folio(inode, idx, true);
|
| 3120 | if (IS_ERR(folio)) {
|
| 3121 | err = PTR_ERR(folio);
|
| 3122 | goto clear_out;
|
| 3123 | }
|
| 3124 |
|
| 3125 | f2fs_folio_wait_writeback(folio, DATA, true, true);
|
| 3126 |
|
| 3127 | folio_mark_dirty(folio);
|
| 3128 | folio_set_f2fs_gcing(folio);
|
| 3129 | f2fs_folio_put(folio, true);
|
| 3130 |
|
| 3131 | idx++;
|
| 3132 | cnt++;
|
| 3133 | total++;
|
| 3134 | }
|
| 3135 |
|
| 3136 | map.m_lblk = idx;
|
| 3137 | check:
|
| 3138 | if (map.m_lblk < pg_end && cnt < BLKS_PER_SEG(sbi))
|
| 3139 | goto do_map;
|
| 3140 |
|
| 3141 | clear_inode_flag(inode, FI_SKIP_WRITES);
|
| 3142 |
|
| 3143 | err = filemap_fdatawrite(inode->i_mapping);
|
| 3144 | if (err)
|
| 3145 | goto out;
|
| 3146 | }
|
| 3147 | clear_out:
|
| 3148 | clear_inode_flag(inode, FI_SKIP_WRITES);
|
| 3149 | out:
|
| 3150 | clear_inode_flag(inode, FI_OPU_WRITE);
|
| 3151 | unlock_out:
|
| 3152 | inode_unlock(inode);
|
| 3153 | if (!err) {
|
| 3154 | range->len = (u64)total << PAGE_SHIFT;
|
| 3155 | stat_inc_defrag_blk_count(sbi, total);
|
| 3156 | }
|
| 3157 | return err;
|
| 3158 | }
|
| 3159 |
|
| 3160 | static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
|
| 3161 | {
|
| 3162 | struct inode *inode = file_inode(filp);
|
| 3163 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 3164 | struct f2fs_defragment range;
|
| 3165 | int err;
|
| 3166 |
|
| 3167 | if (!capable(CAP_SYS_ADMIN))
|
| 3168 | return -EPERM;
|
| 3169 |
|
| 3170 | if (!S_ISREG(inode->i_mode))
|
| 3171 | return -EINVAL;
|
| 3172 |
|
| 3173 | if (f2fs_readonly(sbi->sb))
|
| 3174 | return -EROFS;
|
| 3175 |
|
| 3176 | if (IS_DEVICE_ALIASING(inode))
|
| 3177 | return -EOPNOTSUPP;
|
| 3178 |
|
| 3179 | if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
|
| 3180 | sizeof(range)))
|
| 3181 | return -EFAULT;
|
| 3182 |
|
| 3183 |
|
| 3184 | if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
|
| 3185 | return -EINVAL;
|
| 3186 |
|
| 3187 | if (unlikely((range.start + range.len) >> PAGE_SHIFT >
|
| 3188 | max_file_blocks(inode)))
|
| 3189 | return -EINVAL;
|
| 3190 |
|
| 3191 | err = mnt_want_write_file(filp);
|
| 3192 | if (err)
|
| 3193 | return err;
|
| 3194 |
|
| 3195 | err = f2fs_defragment_range(sbi, filp, &range);
|
| 3196 | mnt_drop_write_file(filp);
|
| 3197 |
|
| 3198 | if (range.len)
|
| 3199 | f2fs_update_time(sbi, REQ_TIME);
|
| 3200 | if (err < 0)
|
| 3201 | return err;
|
| 3202 |
|
| 3203 | if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
|
| 3204 | sizeof(range)))
|
| 3205 | return -EFAULT;
|
| 3206 |
|
| 3207 | return 0;
|
| 3208 | }
|
| 3209 |
|
| 3210 | static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
|
| 3211 | struct file *file_out, loff_t pos_out, size_t len)
|
| 3212 | {
|
| 3213 | struct inode *src = file_inode(file_in);
|
| 3214 | struct inode *dst = file_inode(file_out);
|
| 3215 | struct f2fs_sb_info *sbi = F2FS_I_SB(src);
|
| 3216 | struct f2fs_lock_context lc;
|
| 3217 | size_t olen = len;
|
| 3218 | loff_t dst_max_i_size = 0;
|
| 3219 | loff_t dst_osize, dst_end;
|
| 3220 | int ret;
|
| 3221 |
|
| 3222 | if (file_in->f_path.mnt != file_out->f_path.mnt ||
|
| 3223 | src->i_sb != dst->i_sb)
|
| 3224 | return -EXDEV;
|
| 3225 |
|
| 3226 | if (unlikely(f2fs_readonly(src->i_sb)))
|
| 3227 | return -EROFS;
|
| 3228 |
|
| 3229 | if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
|
| 3230 | return -EINVAL;
|
| 3231 |
|
| 3232 | if (IS_ENCRYPTED(src) || IS_ENCRYPTED(dst) ||
|
| 3233 | IS_DEVICE_ALIASING(src) || IS_DEVICE_ALIASING(dst))
|
| 3234 | return -EOPNOTSUPP;
|
| 3235 |
|
| 3236 | if (pos_out < 0 || pos_in < 0)
|
| 3237 | return -EINVAL;
|
| 3238 |
|
| 3239 | if (src == dst) {
|
| 3240 | if (pos_in == pos_out)
|
| 3241 | return 0;
|
| 3242 | }
|
| 3243 |
|
| 3244 | inode_lock(src);
|
| 3245 | if (src != dst) {
|
| 3246 | ret = -EBUSY;
|
| 3247 | if (!inode_trylock(dst))
|
| 3248 | goto out;
|
| 3249 | }
|
| 3250 |
|
| 3251 | if (f2fs_compressed_file(src) || f2fs_compressed_file(dst) ||
|
| 3252 | f2fs_is_pinned_file(src) || f2fs_is_pinned_file(dst)) {
|
| 3253 | ret = -EOPNOTSUPP;
|
| 3254 | goto out_unlock;
|
| 3255 | }
|
| 3256 |
|
| 3257 | if (f2fs_is_atomic_file(src) || f2fs_is_atomic_file(dst)) {
|
| 3258 | ret = -EINVAL;
|
| 3259 | goto out_unlock;
|
| 3260 | }
|
| 3261 |
|
| 3262 | ret = -EINVAL;
|
| 3263 | if (pos_in + len > src->i_size || pos_in + len < pos_in)
|
| 3264 | goto out_unlock;
|
| 3265 | if (len == 0)
|
| 3266 | olen = len = src->i_size - pos_in;
|
| 3267 | if (src == dst && pos_out > pos_in && pos_out < pos_in + len)
|
| 3268 | goto out_unlock;
|
| 3269 | if (pos_in + len == src->i_size)
|
| 3270 | len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
|
| 3271 | if (len == 0) {
|
| 3272 | ret = 0;
|
| 3273 | goto out_unlock;
|
| 3274 | }
|
| 3275 |
|
| 3276 | dst_osize = dst->i_size;
|
| 3277 | if (olen > LLONG_MAX - pos_out)
|
| 3278 | goto out_unlock;
|
| 3279 | dst_end = pos_out + olen;
|
| 3280 | if (dst_end > dst->i_size) {
|
| 3281 | ret = inode_newsize_ok(dst, dst_end);
|
| 3282 | if (ret)
|
| 3283 | goto out_unlock;
|
| 3284 | dst_max_i_size = dst_end;
|
| 3285 | }
|
| 3286 |
|
| 3287 |
|
| 3288 | if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
|
| 3289 | !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
|
| 3290 | !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
|
| 3291 | goto out_unlock;
|
| 3292 |
|
| 3293 | ret = f2fs_convert_inline_inode(src);
|
| 3294 | if (ret)
|
| 3295 | goto out_unlock;
|
| 3296 |
|
| 3297 | ret = f2fs_convert_inline_inode(dst);
|
| 3298 | if (ret)
|
| 3299 | goto out_unlock;
|
| 3300 |
|
| 3301 |
|
| 3302 | ret = filemap_write_and_wait_range(src->i_mapping,
|
| 3303 | pos_in, pos_in + len);
|
| 3304 | if (ret)
|
| 3305 | goto out_unlock;
|
| 3306 |
|
| 3307 | ret = filemap_write_and_wait_range(dst->i_mapping,
|
| 3308 | pos_out, pos_out + len);
|
| 3309 | if (ret)
|
| 3310 | goto out_unlock;
|
| 3311 |
|
| 3312 | f2fs_balance_fs(sbi, true);
|
| 3313 |
|
| 3314 | f2fs_down_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
|
| 3315 | if (src != dst) {
|
| 3316 | ret = -EBUSY;
|
| 3317 | if (!f2fs_down_write_trylock(&F2FS_I(dst)->i_gc_rwsem[WRITE]))
|
| 3318 | goto out_src;
|
| 3319 | }
|
| 3320 |
|
| 3321 | f2fs_lock_op(sbi, &lc);
|
| 3322 | ret = __exchange_data_block(src, dst, F2FS_BYTES_TO_BLK(pos_in),
|
| 3323 | F2FS_BYTES_TO_BLK(pos_out),
|
| 3324 | F2FS_BYTES_TO_BLK(len), false);
|
| 3325 |
|
| 3326 | if (!ret) {
|
| 3327 | if (dst_max_i_size)
|
| 3328 | f2fs_i_size_write(dst, dst_max_i_size);
|
| 3329 | else if (dst_osize != dst->i_size)
|
| 3330 | f2fs_i_size_write(dst, dst_osize);
|
| 3331 | }
|
| 3332 | f2fs_unlock_op(sbi, &lc);
|
| 3333 |
|
| 3334 | if (src != dst)
|
| 3335 | f2fs_up_write(&F2FS_I(dst)->i_gc_rwsem[WRITE]);
|
| 3336 | out_src:
|
| 3337 | f2fs_up_write(&F2FS_I(src)->i_gc_rwsem[WRITE]);
|
| 3338 | if (ret)
|
| 3339 | goto out_unlock;
|
| 3340 |
|
| 3341 | inode_set_mtime_to_ts(src, inode_set_ctime_current(src));
|
| 3342 | f2fs_mark_inode_dirty_sync(src, false);
|
| 3343 | if (src != dst) {
|
| 3344 | inode_set_mtime_to_ts(dst, inode_set_ctime_current(dst));
|
| 3345 | f2fs_mark_inode_dirty_sync(dst, false);
|
| 3346 | }
|
| 3347 | f2fs_update_time(sbi, REQ_TIME);
|
| 3348 |
|
| 3349 | out_unlock:
|
| 3350 | if (src != dst)
|
| 3351 | inode_unlock(dst);
|
| 3352 | out:
|
| 3353 | inode_unlock(src);
|
| 3354 | return ret;
|
| 3355 | }
|
| 3356 |
|
| 3357 | static int __f2fs_ioc_move_range(struct file *filp,
|
| 3358 | struct f2fs_move_range *range)
|
| 3359 | {
|
| 3360 | int err;
|
| 3361 |
|
| 3362 | if (!(filp->f_mode & FMODE_READ) ||
|
| 3363 | !(filp->f_mode & FMODE_WRITE))
|
| 3364 | return -EBADF;
|
| 3365 |
|
| 3366 | CLASS(fd, dst)(range->dst_fd);
|
| 3367 | if (fd_empty(dst))
|
| 3368 | return -EBADF;
|
| 3369 |
|
| 3370 | if (!(fd_file(dst)->f_mode & FMODE_WRITE))
|
| 3371 | return -EBADF;
|
| 3372 |
|
| 3373 | err = mnt_want_write_file(filp);
|
| 3374 | if (err)
|
| 3375 | return err;
|
| 3376 |
|
| 3377 | err = f2fs_move_file_range(filp, range->pos_in, fd_file(dst),
|
| 3378 | range->pos_out, range->len);
|
| 3379 |
|
| 3380 | mnt_drop_write_file(filp);
|
| 3381 | return err;
|
| 3382 | }
|
| 3383 |
|
| 3384 | static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
|
| 3385 | {
|
| 3386 | struct f2fs_move_range range;
|
| 3387 |
|
| 3388 | if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
|
| 3389 | sizeof(range)))
|
| 3390 | return -EFAULT;
|
| 3391 | return __f2fs_ioc_move_range(filp, &range);
|
| 3392 | }
|
| 3393 |
|
| 3394 | static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
|
| 3395 | {
|
| 3396 | struct inode *inode = file_inode(filp);
|
| 3397 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 3398 | struct sit_info *sm = SIT_I(sbi);
|
| 3399 | unsigned int start_segno = 0, end_segno = 0;
|
| 3400 | unsigned int dev_start_segno = 0, dev_end_segno = 0;
|
| 3401 | struct f2fs_flush_device range;
|
| 3402 | struct f2fs_gc_control gc_control = {
|
| 3403 | .init_gc_type = FG_GC,
|
| 3404 | .should_migrate_blocks = true,
|
| 3405 | .err_gc_skipped = true,
|
| 3406 | .nr_free_secs = 0 };
|
| 3407 | int ret;
|
| 3408 |
|
| 3409 | if (!capable(CAP_SYS_ADMIN))
|
| 3410 | return -EPERM;
|
| 3411 |
|
| 3412 | if (f2fs_readonly(sbi->sb))
|
| 3413 | return -EROFS;
|
| 3414 |
|
| 3415 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
|
| 3416 | return -EINVAL;
|
| 3417 |
|
| 3418 | if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
|
| 3419 | sizeof(range)))
|
| 3420 | return -EFAULT;
|
| 3421 |
|
| 3422 | if (!f2fs_is_multi_device(sbi) || sbi->s_ndevs - 1 <= range.dev_num ||
|
| 3423 | __is_large_section(sbi)) {
|
| 3424 | f2fs_warn(sbi, "Can't flush %u in %d for SEGS_PER_SEC %u != 1",
|
| 3425 | range.dev_num, sbi->s_ndevs, SEGS_PER_SEC(sbi));
|
| 3426 | return -EINVAL;
|
| 3427 | }
|
| 3428 |
|
| 3429 | ret = mnt_want_write_file(filp);
|
| 3430 | if (ret)
|
| 3431 | return ret;
|
| 3432 |
|
| 3433 | if (range.dev_num != 0)
|
| 3434 | dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
|
| 3435 | dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
|
| 3436 |
|
| 3437 | start_segno = sm->last_victim[FLUSH_DEVICE];
|
| 3438 | if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
|
| 3439 | start_segno = dev_start_segno;
|
| 3440 | end_segno = min(start_segno + range.segments, dev_end_segno);
|
| 3441 |
|
| 3442 | while (start_segno < end_segno) {
|
| 3443 | if (!f2fs_down_write_trylock_trace(&sbi->gc_lock, &gc_control.lc)) {
|
| 3444 | ret = -EBUSY;
|
| 3445 | goto out;
|
| 3446 | }
|
| 3447 | sm->last_victim[GC_CB] = end_segno + 1;
|
| 3448 | sm->last_victim[GC_GREEDY] = end_segno + 1;
|
| 3449 | sm->last_victim[ALLOC_NEXT] = end_segno + 1;
|
| 3450 |
|
| 3451 | gc_control.victim_segno = start_segno;
|
| 3452 | stat_inc_gc_call_count(sbi, FOREGROUND);
|
| 3453 | ret = f2fs_gc(sbi, &gc_control);
|
| 3454 | if (ret == -EAGAIN)
|
| 3455 | ret = 0;
|
| 3456 | else if (ret < 0)
|
| 3457 | break;
|
| 3458 | start_segno++;
|
| 3459 | }
|
| 3460 | out:
|
| 3461 | mnt_drop_write_file(filp);
|
| 3462 | return ret;
|
| 3463 | }
|
| 3464 |
|
| 3465 | static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
|
| 3466 | {
|
| 3467 | struct inode *inode = file_inode(filp);
|
| 3468 | u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
|
| 3469 |
|
| 3470 |
|
| 3471 | sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
|
| 3472 |
|
| 3473 | return put_user(sb_feature, (u32 __user *)arg);
|
| 3474 | }
|
| 3475 |
|
| 3476 | #ifdef CONFIG_QUOTA
|
| 3477 | int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
|
| 3478 | {
|
| 3479 | struct dquot *transfer_to[MAXQUOTAS] = {};
|
| 3480 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 3481 | struct super_block *sb = sbi->sb;
|
| 3482 | int err;
|
| 3483 |
|
| 3484 | transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
|
| 3485 | if (IS_ERR(transfer_to[PRJQUOTA]))
|
| 3486 | return PTR_ERR(transfer_to[PRJQUOTA]);
|
| 3487 |
|
| 3488 | err = __dquot_transfer(inode, transfer_to);
|
| 3489 | if (err)
|
| 3490 | set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
|
| 3491 | dqput(transfer_to[PRJQUOTA]);
|
| 3492 | return err;
|
| 3493 | }
|
| 3494 |
|
| 3495 | static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
|
| 3496 | {
|
| 3497 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 3498 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 3499 | struct f2fs_inode *ri = NULL;
|
| 3500 | struct f2fs_lock_context lc;
|
| 3501 | kprojid_t kprojid;
|
| 3502 | int err;
|
| 3503 |
|
| 3504 | if (!f2fs_sb_has_project_quota(sbi)) {
|
| 3505 | if (projid != F2FS_DEF_PROJID)
|
| 3506 | return -EOPNOTSUPP;
|
| 3507 | else
|
| 3508 | return 0;
|
| 3509 | }
|
| 3510 |
|
| 3511 | if (!f2fs_has_extra_attr(inode))
|
| 3512 | return -EOPNOTSUPP;
|
| 3513 |
|
| 3514 | kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
|
| 3515 |
|
| 3516 | if (projid_eq(kprojid, fi->i_projid))
|
| 3517 | return 0;
|
| 3518 |
|
| 3519 | err = -EPERM;
|
| 3520 |
|
| 3521 | if (IS_NOQUOTA(inode))
|
| 3522 | return err;
|
| 3523 |
|
| 3524 | if (!F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
|
| 3525 | return -EOVERFLOW;
|
| 3526 |
|
| 3527 | err = f2fs_dquot_initialize(inode);
|
| 3528 | if (err)
|
| 3529 | return err;
|
| 3530 |
|
| 3531 | f2fs_lock_op(sbi, &lc);
|
| 3532 | err = f2fs_transfer_project_quota(inode, kprojid);
|
| 3533 | if (err)
|
| 3534 | goto out_unlock;
|
| 3535 |
|
| 3536 | fi->i_projid = kprojid;
|
| 3537 | inode_set_ctime_current(inode);
|
| 3538 | f2fs_mark_inode_dirty_sync(inode, true);
|
| 3539 | out_unlock:
|
| 3540 | f2fs_unlock_op(sbi, &lc);
|
| 3541 | return err;
|
| 3542 | }
|
| 3543 | #else
|
| 3544 | int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid)
|
| 3545 | {
|
| 3546 | return 0;
|
| 3547 | }
|
| 3548 |
|
| 3549 | static int f2fs_ioc_setproject(struct inode *inode, __u32 projid)
|
| 3550 | {
|
| 3551 | if (projid != F2FS_DEF_PROJID)
|
| 3552 | return -EOPNOTSUPP;
|
| 3553 | return 0;
|
| 3554 | }
|
| 3555 | #endif
|
| 3556 |
|
| 3557 | int f2fs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
|
| 3558 | {
|
| 3559 | struct inode *inode = d_inode(dentry);
|
| 3560 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 3561 | u32 fsflags = f2fs_iflags_to_fsflags(fi->i_flags);
|
| 3562 |
|
| 3563 | if (IS_ENCRYPTED(inode))
|
| 3564 | fsflags |= FS_ENCRYPT_FL;
|
| 3565 | if (IS_VERITY(inode))
|
| 3566 | fsflags |= FS_VERITY_FL;
|
| 3567 | if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode))
|
| 3568 | fsflags |= FS_INLINE_DATA_FL;
|
| 3569 | if (is_inode_flag_set(inode, FI_PIN_FILE))
|
| 3570 | fsflags |= FS_NOCOW_FL;
|
| 3571 |
|
| 3572 | fileattr_fill_flags(fa, fsflags & F2FS_GETTABLE_FS_FL);
|
| 3573 |
|
| 3574 | if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)))
|
| 3575 | fa->fsx_projid = from_kprojid(&init_user_ns, fi->i_projid);
|
| 3576 |
|
| 3577 | return 0;
|
| 3578 | }
|
| 3579 |
|
| 3580 | int f2fs_fileattr_set(const struct mnt_idmap *idmap,
|
| 3581 | struct dentry *dentry, struct file_kattr *fa)
|
| 3582 | {
|
| 3583 | struct inode *inode = d_inode(dentry);
|
| 3584 | u32 fsflags = fa->flags, mask = F2FS_SETTABLE_FS_FL;
|
| 3585 | u32 iflags;
|
| 3586 | int err;
|
| 3587 |
|
| 3588 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
|
| 3589 | return -EIO;
|
| 3590 | if (!f2fs_is_checkpoint_ready(F2FS_I_SB(inode)))
|
| 3591 | return -ENOSPC;
|
| 3592 | if (fsflags & ~F2FS_GETTABLE_FS_FL)
|
| 3593 | return -EOPNOTSUPP;
|
| 3594 | fsflags &= F2FS_SETTABLE_FS_FL;
|
| 3595 | if (!fa->flags_valid)
|
| 3596 | mask &= FS_COMMON_FL;
|
| 3597 |
|
| 3598 | iflags = f2fs_fsflags_to_iflags(fsflags);
|
| 3599 | if (f2fs_mask_flags(inode->i_mode, iflags) != iflags)
|
| 3600 | return -EOPNOTSUPP;
|
| 3601 |
|
| 3602 | err = f2fs_setflags_common(inode, iflags, f2fs_fsflags_to_iflags(mask));
|
| 3603 | if (!err)
|
| 3604 | err = f2fs_ioc_setproject(inode, fa->fsx_projid);
|
| 3605 |
|
| 3606 | return err;
|
| 3607 | }
|
| 3608 |
|
| 3609 | int f2fs_pin_file_control(struct inode *inode, bool inc)
|
| 3610 | {
|
| 3611 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 3612 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 3613 |
|
| 3614 | if (IS_DEVICE_ALIASING(inode))
|
| 3615 | return -EINVAL;
|
| 3616 |
|
| 3617 | if (fi->i_gc_failures >= sbi->gc_pin_file_threshold) {
|
| 3618 | f2fs_warn(sbi, "%s: Enable GC = ino %llx after %x GC trials",
|
| 3619 | __func__, inode->i_ino, fi->i_gc_failures);
|
| 3620 | clear_inode_flag(inode, FI_PIN_FILE);
|
| 3621 | return -EAGAIN;
|
| 3622 | }
|
| 3623 |
|
| 3624 |
|
| 3625 | if (inc)
|
| 3626 | f2fs_i_gc_failures_write(inode, fi->i_gc_failures + 1);
|
| 3627 |
|
| 3628 | return 0;
|
| 3629 | }
|
| 3630 |
|
| 3631 | static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
|
| 3632 | {
|
| 3633 | struct inode *inode = file_inode(filp);
|
| 3634 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 3635 | __u32 pin;
|
| 3636 | int ret = 0;
|
| 3637 |
|
| 3638 | if (get_user(pin, (__u32 __user *)arg))
|
| 3639 | return -EFAULT;
|
| 3640 |
|
| 3641 | if (!S_ISREG(inode->i_mode))
|
| 3642 | return -EINVAL;
|
| 3643 |
|
| 3644 | if (f2fs_readonly(sbi->sb))
|
| 3645 | return -EROFS;
|
| 3646 |
|
| 3647 | if (!pin && IS_DEVICE_ALIASING(inode))
|
| 3648 | return -EOPNOTSUPP;
|
| 3649 |
|
| 3650 | ret = mnt_want_write_file(filp);
|
| 3651 | if (ret)
|
| 3652 | return ret;
|
| 3653 |
|
| 3654 | inode_lock(inode);
|
| 3655 |
|
| 3656 | if (f2fs_is_atomic_file(inode)) {
|
| 3657 | ret = -EINVAL;
|
| 3658 | goto out;
|
| 3659 | }
|
| 3660 |
|
| 3661 | if (!pin) {
|
| 3662 | clear_inode_flag(inode, FI_PIN_FILE);
|
| 3663 | f2fs_i_gc_failures_write(inode, 0);
|
| 3664 | goto done;
|
| 3665 | } else if (f2fs_is_pinned_file(inode)) {
|
| 3666 | goto done;
|
| 3667 | }
|
| 3668 |
|
| 3669 | if (F2FS_HAS_BLOCKS(inode)) {
|
| 3670 | ret = -EFBIG;
|
| 3671 | goto out;
|
| 3672 | }
|
| 3673 |
|
| 3674 |
|
| 3675 | if (!f2fs_sb_has_blkzoned(sbi) &&
|
| 3676 | f2fs_should_update_outplace(inode, NULL)) {
|
| 3677 | ret = -EINVAL;
|
| 3678 | goto out;
|
| 3679 | }
|
| 3680 |
|
| 3681 | if (f2fs_pin_file_control(inode, false)) {
|
| 3682 | ret = -EAGAIN;
|
| 3683 | goto out;
|
| 3684 | }
|
| 3685 |
|
| 3686 | ret = f2fs_convert_inline_inode(inode);
|
| 3687 | if (ret)
|
| 3688 | goto out;
|
| 3689 |
|
| 3690 | if (!f2fs_disable_compressed_file(inode)) {
|
| 3691 | ret = -EOPNOTSUPP;
|
| 3692 | goto out;
|
| 3693 | }
|
| 3694 |
|
| 3695 | set_inode_flag(inode, FI_PIN_FILE);
|
| 3696 | ret = F2FS_I(inode)->i_gc_failures;
|
| 3697 | done:
|
| 3698 | f2fs_update_time(sbi, REQ_TIME);
|
| 3699 | out:
|
| 3700 | inode_unlock(inode);
|
| 3701 | mnt_drop_write_file(filp);
|
| 3702 | return ret;
|
| 3703 | }
|
| 3704 |
|
| 3705 | static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
|
| 3706 | {
|
| 3707 | struct inode *inode = file_inode(filp);
|
| 3708 | __u32 pin = 0;
|
| 3709 |
|
| 3710 | if (is_inode_flag_set(inode, FI_PIN_FILE))
|
| 3711 | pin = F2FS_I(inode)->i_gc_failures;
|
| 3712 | return put_user(pin, (u32 __user *)arg);
|
| 3713 | }
|
| 3714 |
|
| 3715 | static int f2fs_ioc_get_dev_alias_file(struct file *filp, unsigned long arg)
|
| 3716 | {
|
| 3717 | return put_user(IS_DEVICE_ALIASING(file_inode(filp)) ? 1 : 0,
|
| 3718 | (u32 __user *)arg);
|
| 3719 | }
|
| 3720 |
|
| 3721 | static bool f2fs_get_dev_alias_extent(struct f2fs_sb_info *sbi,
|
| 3722 | struct dentry *dentry,
|
| 3723 | struct extent_info *ei)
|
| 3724 | {
|
| 3725 | int i;
|
| 3726 |
|
| 3727 | for (i = 1; i < sbi->s_ndevs; i++) {
|
| 3728 | char *name = strrchr(FDEV(i).path, '/');
|
| 3729 |
|
| 3730 | name = name ? name + 1 : FDEV(i).path;
|
| 3731 | if (strcmp(name, dentry->d_name.name))
|
| 3732 | continue;
|
| 3733 |
|
| 3734 | ei->blk = FDEV(i).start_blk;
|
| 3735 | ei->len = FDEV(i).total_segments << sbi->log_blocks_per_seg;
|
| 3736 | ei->fofs = 0;
|
| 3737 | return true;
|
| 3738 | }
|
| 3739 | return false;
|
| 3740 | }
|
| 3741 |
|
| 3742 | static int f2fs_ioc_reserve_dev_alias(struct file *filp)
|
| 3743 | {
|
| 3744 | struct inode *inode = file_inode(filp);
|
| 3745 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 3746 | struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
|
| 3747 | struct extent_info ei;
|
| 3748 | struct cp_control cpc = { CP_SYNC, 0, 0, 0 };
|
| 3749 | struct f2fs_lock_context lc, glc;
|
| 3750 | blkcnt_t count;
|
| 3751 | unsigned int start, end;
|
| 3752 | int type, err;
|
| 3753 |
|
| 3754 | if (!capable(CAP_SYS_ADMIN))
|
| 3755 | return -EPERM;
|
| 3756 |
|
| 3757 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
|
| 3758 | return -EINVAL;
|
| 3759 |
|
| 3760 | err = mnt_want_write_file(filp);
|
| 3761 | if (err)
|
| 3762 | return err;
|
| 3763 |
|
| 3764 | inode_lock(inode);
|
| 3765 |
|
| 3766 | if (!IS_DEVICE_ALIASING(inode)) {
|
| 3767 | err = -EINVAL;
|
| 3768 | goto out_inode_unlock;
|
| 3769 | }
|
| 3770 |
|
| 3771 | if (F2FS_HAS_BLOCKS(inode)) {
|
| 3772 | err = 0;
|
| 3773 | goto out_inode_unlock;
|
| 3774 | }
|
| 3775 |
|
| 3776 | if (!f2fs_get_dev_alias_extent(sbi, filp->f_path.dentry, &ei)) {
|
| 3777 | f2fs_warn(sbi, "device alias file (%s, ino=%llu) has no matching device",
|
| 3778 | filp->f_path.dentry->d_name.name,
|
| 3779 | (unsigned long long)inode->i_ino);
|
| 3780 | set_sbi_flag(sbi, SBI_NEED_FSCK);
|
| 3781 | f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
|
| 3782 | err = -EFSCORRUPTED;
|
| 3783 | goto out_inode_unlock;
|
| 3784 | }
|
| 3785 |
|
| 3786 | spin_lock(&sbi->stat_lock);
|
| 3787 | if (sbi->total_valid_block_count + ei.len >
|
| 3788 | get_available_block_count(sbi, inode, true)) {
|
| 3789 | spin_unlock(&sbi->stat_lock);
|
| 3790 | err = -ENOSPC;
|
| 3791 | goto out_inode_unlock;
|
| 3792 | }
|
| 3793 | sbi->alias_reserved_blocks += ei.len;
|
| 3794 | spin_unlock(&sbi->stat_lock);
|
| 3795 |
|
| 3796 | spin_lock(&FREE_I(sbi)->segmap_lock);
|
| 3797 | FDEV(f2fs_target_device_index(sbi, ei.blk)).is_reserving = true;
|
| 3798 | spin_unlock(&FREE_I(sbi)->segmap_lock);
|
| 3799 |
|
| 3800 | start = GET_SEGNO(sbi, ei.blk);
|
| 3801 | end = GET_SEGNO(sbi, ei.blk + ei.len - 1);
|
| 3802 |
|
| 3803 |
|
| 3804 | f2fs_down_write_trace(&sbi->gc_lock, &glc);
|
| 3805 |
|
| 3806 |
|
| 3807 | f2fs_reset_gc_victim_resource(sbi, start, end);
|
| 3808 |
|
| 3809 |
|
| 3810 | for (type = CURSEG_HOT_DATA; type < NR_CURSEG_PERSIST_TYPE; type++) {
|
| 3811 | err = f2fs_allocate_segment_for_resize(sbi, type, start, end);
|
| 3812 | if (err)
|
| 3813 | goto out_gc_unlock;
|
| 3814 | }
|
| 3815 |
|
| 3816 | f2fs_lock_op(sbi, &lc);
|
| 3817 |
|
| 3818 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
|
| 3819 | err = -EINVAL;
|
| 3820 | f2fs_unlock_op(sbi, &lc);
|
| 3821 | goto out_gc_unlock;
|
| 3822 | }
|
| 3823 |
|
| 3824 |
|
| 3825 | err = f2fs_gc_range(sbi, start, end, false, 0, false);
|
| 3826 | if (err) {
|
| 3827 | f2fs_unlock_op(sbi, &lc);
|
| 3828 | goto out_gc_unlock;
|
| 3829 | }
|
| 3830 |
|
| 3831 | count = ei.len;
|
| 3832 | err = inc_valid_block_count(sbi, inode, &count, false, true);
|
| 3833 | if (err) {
|
| 3834 | f2fs_unlock_op(sbi, &lc);
|
| 3835 | goto out_gc_unlock;
|
| 3836 | }
|
| 3837 |
|
| 3838 | write_lock(&et->lock);
|
| 3839 | et->largest = ei;
|
| 3840 | write_unlock(&et->lock);
|
| 3841 | clear_inode_flag(inode, FI_NO_EXTENT);
|
| 3842 |
|
| 3843 | f2fs_reserve_device_alias(sbi, ei.blk, ei.len);
|
| 3844 |
|
| 3845 | i_size_write(inode, (loff_t)ei.len << sbi->log_blocksize);
|
| 3846 | f2fs_update_inode_page(inode);
|
| 3847 |
|
| 3848 | spin_lock(&FREE_I(sbi)->segmap_lock);
|
| 3849 | FDEV(f2fs_target_device_index(sbi, ei.blk)).is_reserving = false;
|
| 3850 | spin_unlock(&FREE_I(sbi)->segmap_lock);
|
| 3851 |
|
| 3852 | f2fs_unlock_op(sbi, &lc);
|
| 3853 | f2fs_up_write_trace(&sbi->gc_lock, &glc);
|
| 3854 |
|
| 3855 | inode_unlock(inode);
|
| 3856 | mnt_drop_write_file(filp);
|
| 3857 |
|
| 3858 | return f2fs_write_checkpoint(sbi, &cpc);
|
| 3859 |
|
| 3860 | out_gc_unlock:
|
| 3861 | spin_lock(&sbi->stat_lock);
|
| 3862 | sbi->alias_reserved_blocks -= ei.len;
|
| 3863 | spin_unlock(&sbi->stat_lock);
|
| 3864 |
|
| 3865 | spin_lock(&FREE_I(sbi)->segmap_lock);
|
| 3866 | FDEV(f2fs_target_device_index(sbi, ei.blk)).is_reserving = false;
|
| 3867 | spin_unlock(&FREE_I(sbi)->segmap_lock);
|
| 3868 | f2fs_up_write_trace(&sbi->gc_lock, &glc);
|
| 3869 |
|
| 3870 | out_inode_unlock:
|
| 3871 | inode_unlock(inode);
|
| 3872 | mnt_drop_write_file(filp);
|
| 3873 | return err;
|
| 3874 | }
|
| 3875 |
|
| 3876 | static int f2fs_ioc_release_dev_alias(struct file *filp)
|
| 3877 | {
|
| 3878 | struct inode *inode = file_inode(filp);
|
| 3879 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 3880 | struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
|
| 3881 | struct extent_info ei = {0, };
|
| 3882 | struct cp_control cpc = { CP_SYNC, 0, 0, 0 };
|
| 3883 | struct f2fs_lock_context lc, glc;
|
| 3884 | int err;
|
| 3885 |
|
| 3886 | if (!capable(CAP_SYS_ADMIN))
|
| 3887 | return -EPERM;
|
| 3888 |
|
| 3889 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
|
| 3890 | return -EINVAL;
|
| 3891 |
|
| 3892 | err = mnt_want_write_file(filp);
|
| 3893 | if (err)
|
| 3894 | return err;
|
| 3895 |
|
| 3896 | inode_lock(inode);
|
| 3897 |
|
| 3898 | if (!IS_DEVICE_ALIASING(inode)) {
|
| 3899 | err = -EINVAL;
|
| 3900 | goto out_inode_unlock;
|
| 3901 | }
|
| 3902 |
|
| 3903 | if (!F2FS_HAS_BLOCKS(inode)) {
|
| 3904 | err = 0;
|
| 3905 | goto out_inode_unlock;
|
| 3906 | }
|
| 3907 |
|
| 3908 | err = filemap_write_and_wait(inode->i_mapping);
|
| 3909 | if (err)
|
| 3910 | goto out_inode_unlock;
|
| 3911 |
|
| 3912 | read_lock(&et->lock);
|
| 3913 | ei = et->largest;
|
| 3914 | read_unlock(&et->lock);
|
| 3915 |
|
| 3916 | f2fs_down_write_trace(&sbi->gc_lock, &glc);
|
| 3917 | f2fs_lock_op(sbi, &lc);
|
| 3918 |
|
| 3919 | if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
|
| 3920 | err = -EINVAL;
|
| 3921 | f2fs_unlock_op(sbi, &lc);
|
| 3922 | f2fs_up_write_trace(&sbi->gc_lock, &glc);
|
| 3923 | goto out_inode_unlock;
|
| 3924 | }
|
| 3925 |
|
| 3926 | filemap_invalidate_lock(inode->i_mapping);
|
| 3927 | truncate_setsize(inode, 0);
|
| 3928 |
|
| 3929 | err = f2fs_truncate_blocks(inode, 0, false);
|
| 3930 | if (err)
|
| 3931 | i_size_write(inode, (loff_t)ei.len << sbi->log_blocksize);
|
| 3932 | filemap_invalidate_unlock(inode->i_mapping);
|
| 3933 |
|
| 3934 | if (err) {
|
| 3935 | f2fs_unlock_op(sbi, &lc);
|
| 3936 | f2fs_up_write_trace(&sbi->gc_lock, &glc);
|
| 3937 | goto out_inode_unlock;
|
| 3938 | }
|
| 3939 |
|
| 3940 | f2fs_update_inode_page(inode);
|
| 3941 |
|
| 3942 | f2fs_unlock_op(sbi, &lc);
|
| 3943 | f2fs_up_write_trace(&sbi->gc_lock, &glc);
|
| 3944 |
|
| 3945 | inode_unlock(inode);
|
| 3946 | mnt_drop_write_file(filp);
|
| 3947 |
|
| 3948 | return f2fs_write_checkpoint(sbi, &cpc);
|
| 3949 |
|
| 3950 | out_inode_unlock:
|
| 3951 | inode_unlock(inode);
|
| 3952 | mnt_drop_write_file(filp);
|
| 3953 | return err;
|
| 3954 | }
|
| 3955 |
|
| 3956 | static int f2fs_ioc_io_prio(struct file *filp, unsigned long arg)
|
| 3957 | {
|
| 3958 | struct inode *inode = file_inode(filp);
|
| 3959 | __u32 level;
|
| 3960 |
|
| 3961 | if (get_user(level, (__u32 __user *)arg))
|
| 3962 | return -EFAULT;
|
| 3963 |
|
| 3964 | if (!S_ISREG(inode->i_mode) || level >= F2FS_IOPRIO_MAX)
|
| 3965 | return -EINVAL;
|
| 3966 |
|
| 3967 | inode_lock(inode);
|
| 3968 | F2FS_I(inode)->ioprio_hint = level;
|
| 3969 | inode_unlock(inode);
|
| 3970 | return 0;
|
| 3971 | }
|
| 3972 |
|
| 3973 | int f2fs_precache_extents(struct inode *inode)
|
| 3974 | {
|
| 3975 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 3976 | struct f2fs_map_blocks map;
|
| 3977 | pgoff_t m_next_extent;
|
| 3978 | loff_t end;
|
| 3979 | int err;
|
| 3980 |
|
| 3981 | if (is_inode_flag_set(inode, FI_NO_EXTENT))
|
| 3982 | return -EOPNOTSUPP;
|
| 3983 |
|
| 3984 | map.m_lblk = 0;
|
| 3985 | map.m_pblk = 0;
|
| 3986 | map.m_next_pgofs = NULL;
|
| 3987 | map.m_next_extent = &m_next_extent;
|
| 3988 | map.m_seg_type = NO_CHECK_TYPE;
|
| 3989 | map.m_may_create = false;
|
| 3990 | end = F2FS_BLK_ALIGN(i_size_read(inode));
|
| 3991 |
|
| 3992 | while (map.m_lblk < end) {
|
| 3993 | map.m_len = end - map.m_lblk;
|
| 3994 |
|
| 3995 | f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
|
| 3996 | err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRECACHE);
|
| 3997 | f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
|
| 3998 | if (err || !map.m_len)
|
| 3999 | return err;
|
| 4000 |
|
| 4001 | map.m_lblk = m_next_extent;
|
| 4002 | }
|
| 4003 |
|
| 4004 | return 0;
|
| 4005 | }
|
| 4006 |
|
| 4007 | static int f2fs_ioc_precache_extents(struct file *filp)
|
| 4008 | {
|
| 4009 | return f2fs_precache_extents(file_inode(filp));
|
| 4010 | }
|
| 4011 |
|
| 4012 | static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
|
| 4013 | {
|
| 4014 | struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
|
| 4015 | __u64 block_count;
|
| 4016 |
|
| 4017 | if (!capable(CAP_SYS_ADMIN))
|
| 4018 | return -EPERM;
|
| 4019 |
|
| 4020 | if (f2fs_readonly(sbi->sb))
|
| 4021 | return -EROFS;
|
| 4022 |
|
| 4023 | if (copy_from_user(&block_count, (void __user *)arg,
|
| 4024 | sizeof(block_count)))
|
| 4025 | return -EFAULT;
|
| 4026 |
|
| 4027 | return f2fs_resize_fs(filp, block_count);
|
| 4028 | }
|
| 4029 |
|
| 4030 | static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)
|
| 4031 | {
|
| 4032 | struct inode *inode = file_inode(filp);
|
| 4033 |
|
| 4034 | f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
|
| 4035 |
|
| 4036 | if (!f2fs_sb_has_verity(F2FS_I_SB(inode))) {
|
| 4037 | f2fs_warn(F2FS_I_SB(inode),
|
| 4038 | "Can't enable fs-verity on inode %llu: the verity feature is not enabled on this filesystem",
|
| 4039 | inode->i_ino);
|
| 4040 | return -EOPNOTSUPP;
|
| 4041 | }
|
| 4042 |
|
| 4043 | return fsverity_ioctl_enable(filp, (const void __user *)arg);
|
| 4044 | }
|
| 4045 |
|
| 4046 | static int f2fs_ioc_measure_verity(struct file *filp, unsigned long arg)
|
| 4047 | {
|
| 4048 | if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
|
| 4049 | return -EOPNOTSUPP;
|
| 4050 |
|
| 4051 | return fsverity_ioctl_measure(filp, (void __user *)arg);
|
| 4052 | }
|
| 4053 |
|
| 4054 | static int f2fs_ioc_read_verity_metadata(struct file *filp, unsigned long arg)
|
| 4055 | {
|
| 4056 | if (!f2fs_sb_has_verity(F2FS_I_SB(file_inode(filp))))
|
| 4057 | return -EOPNOTSUPP;
|
| 4058 |
|
| 4059 | return fsverity_ioctl_read_metadata(filp, (const void __user *)arg);
|
| 4060 | }
|
| 4061 |
|
| 4062 | static int f2fs_ioc_getfslabel(struct file *filp, unsigned long arg)
|
| 4063 | {
|
| 4064 | struct inode *inode = file_inode(filp);
|
| 4065 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 4066 | char *vbuf;
|
| 4067 | int count;
|
| 4068 | int err = 0;
|
| 4069 |
|
| 4070 | vbuf = f2fs_kzalloc(sbi, MAX_VOLUME_NAME, GFP_KERNEL);
|
| 4071 | if (!vbuf)
|
| 4072 | return -ENOMEM;
|
| 4073 |
|
| 4074 | f2fs_down_read(&sbi->sb_lock);
|
| 4075 | count = utf16s_to_utf8s(sbi->raw_super->volume_name,
|
| 4076 | ARRAY_SIZE(sbi->raw_super->volume_name),
|
| 4077 | UTF16_LITTLE_ENDIAN, vbuf, MAX_VOLUME_NAME);
|
| 4078 | f2fs_up_read(&sbi->sb_lock);
|
| 4079 |
|
| 4080 | if (copy_to_user((char __user *)arg, vbuf,
|
| 4081 | min(FSLABEL_MAX, count)))
|
| 4082 | err = -EFAULT;
|
| 4083 |
|
| 4084 | kfree(vbuf);
|
| 4085 | return err;
|
| 4086 | }
|
| 4087 |
|
| 4088 | static int f2fs_ioc_setfslabel(struct file *filp, unsigned long arg)
|
| 4089 | {
|
| 4090 | struct inode *inode = file_inode(filp);
|
| 4091 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 4092 | char *vbuf;
|
| 4093 | int err = 0;
|
| 4094 |
|
| 4095 | if (!capable(CAP_SYS_ADMIN))
|
| 4096 | return -EPERM;
|
| 4097 |
|
| 4098 | vbuf = strndup_user((const char __user *)arg, FSLABEL_MAX);
|
| 4099 | if (IS_ERR(vbuf))
|
| 4100 | return PTR_ERR(vbuf);
|
| 4101 |
|
| 4102 | err = mnt_want_write_file(filp);
|
| 4103 | if (err)
|
| 4104 | goto out;
|
| 4105 |
|
| 4106 | f2fs_down_write(&sbi->sb_lock);
|
| 4107 |
|
| 4108 | memset(sbi->raw_super->volume_name, 0,
|
| 4109 | sizeof(sbi->raw_super->volume_name));
|
| 4110 | utf8s_to_utf16s(vbuf, strlen(vbuf), UTF16_LITTLE_ENDIAN,
|
| 4111 | sbi->raw_super->volume_name,
|
| 4112 | ARRAY_SIZE(sbi->raw_super->volume_name));
|
| 4113 |
|
| 4114 | err = f2fs_commit_super(sbi, false);
|
| 4115 |
|
| 4116 | f2fs_up_write(&sbi->sb_lock);
|
| 4117 |
|
| 4118 | mnt_drop_write_file(filp);
|
| 4119 | out:
|
| 4120 | kfree(vbuf);
|
| 4121 | return err;
|
| 4122 | }
|
| 4123 |
|
| 4124 | static int f2fs_get_compress_blocks(struct inode *inode, __u64 *blocks)
|
| 4125 | {
|
| 4126 | if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
|
| 4127 | return -EOPNOTSUPP;
|
| 4128 |
|
| 4129 | if (!f2fs_compressed_file(inode))
|
| 4130 | return -EINVAL;
|
| 4131 |
|
| 4132 | *blocks = atomic_read(&F2FS_I(inode)->i_compr_blocks);
|
| 4133 |
|
| 4134 | return 0;
|
| 4135 | }
|
| 4136 |
|
| 4137 | static int f2fs_ioc_get_compress_blocks(struct file *filp, unsigned long arg)
|
| 4138 | {
|
| 4139 | struct inode *inode = file_inode(filp);
|
| 4140 | __u64 blocks;
|
| 4141 | int ret;
|
| 4142 |
|
| 4143 | ret = f2fs_get_compress_blocks(inode, &blocks);
|
| 4144 | if (ret < 0)
|
| 4145 | return ret;
|
| 4146 |
|
| 4147 | return put_user(blocks, (u64 __user *)arg);
|
| 4148 | }
|
| 4149 |
|
| 4150 | static int release_compress_blocks(struct dnode_of_data *dn, pgoff_t count)
|
| 4151 | {
|
| 4152 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
|
| 4153 | unsigned int released_blocks = 0;
|
| 4154 | int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
|
| 4155 | block_t blkaddr;
|
| 4156 | int i;
|
| 4157 |
|
| 4158 | for (i = 0; i < count; i++) {
|
| 4159 | blkaddr = data_blkaddr(dn->inode, dn->node_folio,
|
| 4160 | dn->ofs_in_node + i);
|
| 4161 |
|
| 4162 | if (!__is_valid_data_blkaddr(blkaddr))
|
| 4163 | continue;
|
| 4164 | if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
|
| 4165 | DATA_GENERIC_ENHANCE)))
|
| 4166 | return -EFSCORRUPTED;
|
| 4167 | }
|
| 4168 |
|
| 4169 | while (count) {
|
| 4170 | int compr_blocks = 0;
|
| 4171 |
|
| 4172 | for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
|
| 4173 | blkaddr = f2fs_data_blkaddr(dn);
|
| 4174 |
|
| 4175 | if (i == 0) {
|
| 4176 | if (blkaddr == COMPRESS_ADDR)
|
| 4177 | continue;
|
| 4178 | dn->ofs_in_node += cluster_size;
|
| 4179 | goto next;
|
| 4180 | }
|
| 4181 |
|
| 4182 | if (__is_valid_data_blkaddr(blkaddr))
|
| 4183 | compr_blocks++;
|
| 4184 |
|
| 4185 | if (blkaddr != NEW_ADDR)
|
| 4186 | continue;
|
| 4187 |
|
| 4188 | f2fs_set_data_blkaddr(dn, NULL_ADDR);
|
| 4189 | }
|
| 4190 |
|
| 4191 | f2fs_i_compr_blocks_update(dn->inode, compr_blocks, false);
|
| 4192 | dec_valid_block_count(sbi, dn->inode,
|
| 4193 | cluster_size - compr_blocks);
|
| 4194 |
|
| 4195 | released_blocks += cluster_size - compr_blocks;
|
| 4196 | next:
|
| 4197 | count -= cluster_size;
|
| 4198 | }
|
| 4199 |
|
| 4200 | return released_blocks;
|
| 4201 | }
|
| 4202 |
|
| 4203 | static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)
|
| 4204 | {
|
| 4205 | struct inode *inode = file_inode(filp);
|
| 4206 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 4207 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 4208 | struct f2fs_lock_context lc;
|
| 4209 | pgoff_t page_idx = 0, last_idx;
|
| 4210 | unsigned int released_blocks = 0;
|
| 4211 | int ret;
|
| 4212 | int writecount;
|
| 4213 |
|
| 4214 | if (!f2fs_sb_has_compression(sbi))
|
| 4215 | return -EOPNOTSUPP;
|
| 4216 |
|
| 4217 | if (f2fs_readonly(sbi->sb))
|
| 4218 | return -EROFS;
|
| 4219 |
|
| 4220 | ret = mnt_want_write_file(filp);
|
| 4221 | if (ret)
|
| 4222 | return ret;
|
| 4223 |
|
| 4224 | f2fs_balance_fs(sbi, true);
|
| 4225 |
|
| 4226 | inode_lock(inode);
|
| 4227 |
|
| 4228 | writecount = atomic_read(&inode->i_writecount);
|
| 4229 | if ((filp->f_mode & FMODE_WRITE && writecount != 1) ||
|
| 4230 | (!(filp->f_mode & FMODE_WRITE) && writecount)) {
|
| 4231 | ret = -EBUSY;
|
| 4232 | goto out;
|
| 4233 | }
|
| 4234 |
|
| 4235 | if (!f2fs_compressed_file(inode) ||
|
| 4236 | is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
|
| 4237 | ret = -EINVAL;
|
| 4238 | goto out;
|
| 4239 | }
|
| 4240 |
|
| 4241 | ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
|
| 4242 | if (ret)
|
| 4243 | goto out;
|
| 4244 |
|
| 4245 | if (!atomic_read(&fi->i_compr_blocks)) {
|
| 4246 | ret = -EPERM;
|
| 4247 | goto out;
|
| 4248 | }
|
| 4249 |
|
| 4250 | set_inode_flag(inode, FI_COMPRESS_RELEASED);
|
| 4251 | inode_set_ctime_current(inode);
|
| 4252 | f2fs_mark_inode_dirty_sync(inode, true);
|
| 4253 |
|
| 4254 | f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
|
| 4255 | filemap_invalidate_lock(inode->i_mapping);
|
| 4256 |
|
| 4257 | last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
|
| 4258 |
|
| 4259 | while (page_idx < last_idx) {
|
| 4260 | struct dnode_of_data dn;
|
| 4261 | pgoff_t end_offset, count;
|
| 4262 |
|
| 4263 | f2fs_lock_op(sbi, &lc);
|
| 4264 |
|
| 4265 | set_new_dnode(&dn, inode, NULL, NULL, 0);
|
| 4266 | ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
|
| 4267 | if (ret) {
|
| 4268 | f2fs_unlock_op(sbi, &lc);
|
| 4269 | if (ret == -ENOENT) {
|
| 4270 | page_idx = f2fs_get_next_page_offset(&dn,
|
| 4271 | page_idx);
|
| 4272 | ret = 0;
|
| 4273 | continue;
|
| 4274 | }
|
| 4275 | break;
|
| 4276 | }
|
| 4277 |
|
| 4278 | end_offset = ADDRS_PER_PAGE(dn.node_folio, inode);
|
| 4279 | count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
|
| 4280 | count = round_up(count, fi->i_cluster_size);
|
| 4281 |
|
| 4282 | ret = release_compress_blocks(&dn, count);
|
| 4283 |
|
| 4284 | f2fs_put_dnode(&dn);
|
| 4285 |
|
| 4286 | f2fs_unlock_op(sbi, &lc);
|
| 4287 |
|
| 4288 | if (ret < 0)
|
| 4289 | break;
|
| 4290 |
|
| 4291 | page_idx += count;
|
| 4292 | released_blocks += ret;
|
| 4293 | }
|
| 4294 |
|
| 4295 | filemap_invalidate_unlock(inode->i_mapping);
|
| 4296 | f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
|
| 4297 | out:
|
| 4298 | if (released_blocks)
|
| 4299 | f2fs_update_time(sbi, REQ_TIME);
|
| 4300 | inode_unlock(inode);
|
| 4301 |
|
| 4302 | mnt_drop_write_file(filp);
|
| 4303 |
|
| 4304 | if (ret >= 0) {
|
| 4305 | ret = put_user(released_blocks, (u64 __user *)arg);
|
| 4306 | } else if (released_blocks &&
|
| 4307 | atomic_read(&fi->i_compr_blocks)) {
|
| 4308 | set_sbi_flag(sbi, SBI_NEED_FSCK);
|
| 4309 | f2fs_warn(sbi, "%s: partial blocks were released i_ino=%llx "
|
| 4310 | "iblocks=%llu, released=%u, compr_blocks=%u, "
|
| 4311 | "run fsck to fix.",
|
| 4312 | __func__, inode->i_ino, inode->i_blocks,
|
| 4313 | released_blocks,
|
| 4314 | atomic_read(&fi->i_compr_blocks));
|
| 4315 | }
|
| 4316 |
|
| 4317 | return ret;
|
| 4318 | }
|
| 4319 |
|
| 4320 | static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count,
|
| 4321 | unsigned int *reserved_blocks)
|
| 4322 | {
|
| 4323 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
|
| 4324 | int cluster_size = F2FS_I(dn->inode)->i_cluster_size;
|
| 4325 | block_t blkaddr;
|
| 4326 | int i;
|
| 4327 |
|
| 4328 | for (i = 0; i < count; i++) {
|
| 4329 | blkaddr = data_blkaddr(dn->inode, dn->node_folio,
|
| 4330 | dn->ofs_in_node + i);
|
| 4331 |
|
| 4332 | if (!__is_valid_data_blkaddr(blkaddr))
|
| 4333 | continue;
|
| 4334 | if (unlikely(!f2fs_is_valid_blkaddr(sbi, blkaddr,
|
| 4335 | DATA_GENERIC_ENHANCE)))
|
| 4336 | return -EFSCORRUPTED;
|
| 4337 | }
|
| 4338 |
|
| 4339 | while (count) {
|
| 4340 | int compr_blocks = 0;
|
| 4341 | blkcnt_t reserved = 0;
|
| 4342 | blkcnt_t to_reserved;
|
| 4343 | int ret;
|
| 4344 |
|
| 4345 | for (i = 0; i < cluster_size; i++) {
|
| 4346 | blkaddr = data_blkaddr(dn->inode, dn->node_folio,
|
| 4347 | dn->ofs_in_node + i);
|
| 4348 |
|
| 4349 | if (i == 0) {
|
| 4350 | if (blkaddr != COMPRESS_ADDR) {
|
| 4351 | dn->ofs_in_node += cluster_size;
|
| 4352 | goto next;
|
| 4353 | }
|
| 4354 | continue;
|
| 4355 | }
|
| 4356 |
|
| 4357 | |
| 4358 | * compressed cluster was not released due to it
|
| 4359 | * fails in release_compress_blocks(), so NEW_ADDR
|
| 4360 | * is a possible case.
|
| 4361 |
|
| 4362 | if (blkaddr == NEW_ADDR) {
|
| 4363 | reserved++;
|
| 4364 | continue;
|
| 4365 | }
|
| 4366 | if (__is_valid_data_blkaddr(blkaddr)) {
|
| 4367 | compr_blocks++;
|
| 4368 | continue;
|
| 4369 | }
|
| 4370 | }
|
| 4371 |
|
| 4372 | to_reserved = cluster_size - compr_blocks - reserved;
|
| 4373 |
|
| 4374 |
|
| 4375 | if (reserved && to_reserved == 1) {
|
| 4376 | dn->ofs_in_node += cluster_size;
|
| 4377 | goto next;
|
| 4378 | }
|
| 4379 |
|
| 4380 | ret = inc_valid_block_count(sbi, dn->inode,
|
| 4381 | &to_reserved, false, false);
|
| 4382 | if (unlikely(ret))
|
| 4383 | return ret;
|
| 4384 |
|
| 4385 | for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) {
|
| 4386 | if (f2fs_data_blkaddr(dn) == NULL_ADDR)
|
| 4387 | f2fs_set_data_blkaddr(dn, NEW_ADDR);
|
| 4388 | }
|
| 4389 |
|
| 4390 | f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true);
|
| 4391 |
|
| 4392 | *reserved_blocks += to_reserved;
|
| 4393 | next:
|
| 4394 | count -= cluster_size;
|
| 4395 | }
|
| 4396 |
|
| 4397 | return 0;
|
| 4398 | }
|
| 4399 |
|
| 4400 | static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)
|
| 4401 | {
|
| 4402 | struct inode *inode = file_inode(filp);
|
| 4403 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 4404 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 4405 | pgoff_t page_idx = 0, last_idx;
|
| 4406 | unsigned int reserved_blocks = 0;
|
| 4407 | int ret;
|
| 4408 |
|
| 4409 | if (!f2fs_sb_has_compression(sbi))
|
| 4410 | return -EOPNOTSUPP;
|
| 4411 |
|
| 4412 | if (f2fs_readonly(sbi->sb))
|
| 4413 | return -EROFS;
|
| 4414 |
|
| 4415 | ret = mnt_want_write_file(filp);
|
| 4416 | if (ret)
|
| 4417 | return ret;
|
| 4418 |
|
| 4419 | f2fs_balance_fs(sbi, true);
|
| 4420 |
|
| 4421 | inode_lock(inode);
|
| 4422 |
|
| 4423 | if (!f2fs_compressed_file(inode) ||
|
| 4424 | !is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
|
| 4425 | ret = -EINVAL;
|
| 4426 | goto unlock_inode;
|
| 4427 | }
|
| 4428 |
|
| 4429 | if (atomic_read(&fi->i_compr_blocks))
|
| 4430 | goto unlock_inode;
|
| 4431 |
|
| 4432 | f2fs_down_write(&fi->i_gc_rwsem[WRITE]);
|
| 4433 | filemap_invalidate_lock(inode->i_mapping);
|
| 4434 |
|
| 4435 | last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
|
| 4436 |
|
| 4437 | while (page_idx < last_idx) {
|
| 4438 | struct dnode_of_data dn;
|
| 4439 | struct f2fs_lock_context lc;
|
| 4440 | pgoff_t end_offset, count;
|
| 4441 |
|
| 4442 | f2fs_lock_op(sbi, &lc);
|
| 4443 |
|
| 4444 | set_new_dnode(&dn, inode, NULL, NULL, 0);
|
| 4445 | ret = f2fs_get_dnode_of_data(&dn, page_idx, LOOKUP_NODE);
|
| 4446 | if (ret) {
|
| 4447 | f2fs_unlock_op(sbi, &lc);
|
| 4448 | if (ret == -ENOENT) {
|
| 4449 | page_idx = f2fs_get_next_page_offset(&dn,
|
| 4450 | page_idx);
|
| 4451 | ret = 0;
|
| 4452 | continue;
|
| 4453 | }
|
| 4454 | break;
|
| 4455 | }
|
| 4456 |
|
| 4457 | end_offset = ADDRS_PER_PAGE(dn.node_folio, inode);
|
| 4458 | count = min(end_offset - dn.ofs_in_node, last_idx - page_idx);
|
| 4459 | count = round_up(count, fi->i_cluster_size);
|
| 4460 |
|
| 4461 | ret = reserve_compress_blocks(&dn, count, &reserved_blocks);
|
| 4462 |
|
| 4463 | f2fs_put_dnode(&dn);
|
| 4464 |
|
| 4465 | f2fs_unlock_op(sbi, &lc);
|
| 4466 |
|
| 4467 | if (ret < 0)
|
| 4468 | break;
|
| 4469 |
|
| 4470 | page_idx += count;
|
| 4471 | }
|
| 4472 |
|
| 4473 | filemap_invalidate_unlock(inode->i_mapping);
|
| 4474 | f2fs_up_write(&fi->i_gc_rwsem[WRITE]);
|
| 4475 |
|
| 4476 | if (!ret) {
|
| 4477 | clear_inode_flag(inode, FI_COMPRESS_RELEASED);
|
| 4478 | inode_set_ctime_current(inode);
|
| 4479 | f2fs_mark_inode_dirty_sync(inode, true);
|
| 4480 | }
|
| 4481 | unlock_inode:
|
| 4482 | if (reserved_blocks)
|
| 4483 | f2fs_update_time(sbi, REQ_TIME);
|
| 4484 | inode_unlock(inode);
|
| 4485 | mnt_drop_write_file(filp);
|
| 4486 |
|
| 4487 | if (!ret) {
|
| 4488 | ret = put_user(reserved_blocks, (u64 __user *)arg);
|
| 4489 | } else if (reserved_blocks &&
|
| 4490 | atomic_read(&fi->i_compr_blocks)) {
|
| 4491 | set_sbi_flag(sbi, SBI_NEED_FSCK);
|
| 4492 | f2fs_warn(sbi, "%s: partial blocks were reserved i_ino=%llx "
|
| 4493 | "iblocks=%llu, reserved=%u, compr_blocks=%u, "
|
| 4494 | "run fsck to fix.",
|
| 4495 | __func__, inode->i_ino, inode->i_blocks,
|
| 4496 | reserved_blocks,
|
| 4497 | atomic_read(&fi->i_compr_blocks));
|
| 4498 | }
|
| 4499 |
|
| 4500 | return ret;
|
| 4501 | }
|
| 4502 |
|
| 4503 | static int f2fs_secure_erase(struct block_device *bdev, struct inode *inode,
|
| 4504 | pgoff_t off, block_t block, block_t len, u32 flags)
|
| 4505 | {
|
| 4506 | sector_t sector = SECTOR_FROM_BLOCK(block);
|
| 4507 | sector_t nr_sects = SECTOR_FROM_BLOCK(len);
|
| 4508 | int ret = 0;
|
| 4509 |
|
| 4510 | if (flags & F2FS_TRIM_FILE_DISCARD) {
|
| 4511 | if (bdev_max_secure_erase_sectors(bdev))
|
| 4512 | ret = blkdev_issue_secure_erase(bdev, sector, nr_sects,
|
| 4513 | GFP_NOFS);
|
| 4514 | else
|
| 4515 | ret = blkdev_issue_discard(bdev, sector, nr_sects,
|
| 4516 | GFP_NOFS);
|
| 4517 | }
|
| 4518 |
|
| 4519 | if (!ret && (flags & F2FS_TRIM_FILE_ZEROOUT)) {
|
| 4520 | if (IS_ENCRYPTED(inode))
|
| 4521 | ret = fscrypt_zeroout_range(inode,
|
| 4522 | (loff_t)off << inode->i_blkbits, sector,
|
| 4523 | (u64)len << inode->i_blkbits);
|
| 4524 | else
|
| 4525 | ret = blkdev_issue_zeroout(bdev, sector, nr_sects,
|
| 4526 | GFP_NOFS, 0);
|
| 4527 | }
|
| 4528 |
|
| 4529 | return ret;
|
| 4530 | }
|
| 4531 |
|
| 4532 | static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
|
| 4533 | {
|
| 4534 | struct inode *inode = file_inode(filp);
|
| 4535 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 4536 | struct address_space *mapping = inode->i_mapping;
|
| 4537 | struct block_device *prev_bdev = NULL;
|
| 4538 | struct f2fs_sectrim_range range;
|
| 4539 | pgoff_t index, pg_end, prev_index = 0;
|
| 4540 | block_t prev_block = 0, len = 0;
|
| 4541 | loff_t end_addr;
|
| 4542 | bool to_end = false;
|
| 4543 | int ret = 0;
|
| 4544 |
|
| 4545 | if (!(filp->f_mode & FMODE_WRITE))
|
| 4546 | return -EBADF;
|
| 4547 |
|
| 4548 | if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
|
| 4549 | sizeof(range)))
|
| 4550 | return -EFAULT;
|
| 4551 |
|
| 4552 | if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
|
| 4553 | !S_ISREG(inode->i_mode))
|
| 4554 | return -EINVAL;
|
| 4555 |
|
| 4556 | if (((range.flags & F2FS_TRIM_FILE_DISCARD) &&
|
| 4557 | !f2fs_hw_support_discard(sbi)) ||
|
| 4558 | ((range.flags & F2FS_TRIM_FILE_ZEROOUT) &&
|
| 4559 | IS_ENCRYPTED(inode) && f2fs_is_multi_device(sbi)))
|
| 4560 | return -EOPNOTSUPP;
|
| 4561 |
|
| 4562 | ret = mnt_want_write_file(filp);
|
| 4563 | if (ret)
|
| 4564 | return ret;
|
| 4565 | inode_lock(inode);
|
| 4566 |
|
| 4567 | if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode) ||
|
| 4568 | range.start >= inode->i_size) {
|
| 4569 | ret = -EINVAL;
|
| 4570 | goto err;
|
| 4571 | }
|
| 4572 |
|
| 4573 | if (range.len == 0)
|
| 4574 | goto err;
|
| 4575 |
|
| 4576 | if (inode->i_size - range.start > range.len) {
|
| 4577 | end_addr = range.start + range.len;
|
| 4578 | } else {
|
| 4579 | end_addr = range.len == (u64)-1 ?
|
| 4580 | sbi->sb->s_maxbytes : inode->i_size;
|
| 4581 | to_end = true;
|
| 4582 | }
|
| 4583 |
|
| 4584 | if (!IS_ALIGNED(range.start, F2FS_BLKSIZE) ||
|
| 4585 | (!to_end && !IS_ALIGNED(end_addr, F2FS_BLKSIZE))) {
|
| 4586 | ret = -EINVAL;
|
| 4587 | goto err;
|
| 4588 | }
|
| 4589 |
|
| 4590 | index = F2FS_BYTES_TO_BLK(range.start);
|
| 4591 | pg_end = DIV_ROUND_UP(end_addr, F2FS_BLKSIZE);
|
| 4592 |
|
| 4593 | ret = f2fs_convert_inline_inode(inode);
|
| 4594 | if (ret)
|
| 4595 | goto err;
|
| 4596 |
|
| 4597 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 4598 | filemap_invalidate_lock(mapping);
|
| 4599 |
|
| 4600 | ret = filemap_write_and_wait_range(mapping, range.start,
|
| 4601 | to_end ? LLONG_MAX : end_addr - 1);
|
| 4602 | if (ret)
|
| 4603 | goto out;
|
| 4604 |
|
| 4605 | truncate_inode_pages_range(mapping, range.start,
|
| 4606 | to_end ? -1 : end_addr - 1);
|
| 4607 |
|
| 4608 | while (index < pg_end) {
|
| 4609 | struct dnode_of_data dn;
|
| 4610 | pgoff_t end_offset, count;
|
| 4611 | int i;
|
| 4612 |
|
| 4613 | set_new_dnode(&dn, inode, NULL, NULL, 0);
|
| 4614 | ret = f2fs_get_dnode_of_data(&dn, index, LOOKUP_NODE);
|
| 4615 | if (ret) {
|
| 4616 | if (ret == -ENOENT) {
|
| 4617 | index = f2fs_get_next_page_offset(&dn, index);
|
| 4618 | continue;
|
| 4619 | }
|
| 4620 | goto out;
|
| 4621 | }
|
| 4622 |
|
| 4623 | end_offset = ADDRS_PER_PAGE(dn.node_folio, inode);
|
| 4624 | count = min(end_offset - dn.ofs_in_node, pg_end - index);
|
| 4625 | for (i = 0; i < count; i++, index++, dn.ofs_in_node++) {
|
| 4626 | struct block_device *cur_bdev;
|
| 4627 | block_t blkaddr = f2fs_data_blkaddr(&dn);
|
| 4628 |
|
| 4629 | if (!__is_valid_data_blkaddr(blkaddr))
|
| 4630 | continue;
|
| 4631 |
|
| 4632 | if (!f2fs_is_valid_blkaddr(sbi, blkaddr,
|
| 4633 | DATA_GENERIC_ENHANCE)) {
|
| 4634 | ret = -EFSCORRUPTED;
|
| 4635 | f2fs_put_dnode(&dn);
|
| 4636 | goto out;
|
| 4637 | }
|
| 4638 |
|
| 4639 | cur_bdev = f2fs_target_device(sbi, blkaddr, NULL);
|
| 4640 | if (f2fs_is_multi_device(sbi)) {
|
| 4641 | int di = f2fs_target_device_index(sbi, blkaddr);
|
| 4642 |
|
| 4643 | blkaddr -= FDEV(di).start_blk;
|
| 4644 | }
|
| 4645 |
|
| 4646 | if (len) {
|
| 4647 | if (prev_bdev == cur_bdev &&
|
| 4648 | index == prev_index + len &&
|
| 4649 | blkaddr == prev_block + len) {
|
| 4650 | len++;
|
| 4651 | } else {
|
| 4652 | ret = f2fs_secure_erase(prev_bdev,
|
| 4653 | inode, prev_index, prev_block,
|
| 4654 | len, range.flags);
|
| 4655 | if (ret) {
|
| 4656 | f2fs_put_dnode(&dn);
|
| 4657 | goto out;
|
| 4658 | }
|
| 4659 |
|
| 4660 | len = 0;
|
| 4661 | }
|
| 4662 | }
|
| 4663 |
|
| 4664 | if (!len) {
|
| 4665 | prev_bdev = cur_bdev;
|
| 4666 | prev_index = index;
|
| 4667 | prev_block = blkaddr;
|
| 4668 | len = 1;
|
| 4669 | }
|
| 4670 | }
|
| 4671 |
|
| 4672 | f2fs_put_dnode(&dn);
|
| 4673 |
|
| 4674 | if (fatal_signal_pending(current)) {
|
| 4675 | ret = -EINTR;
|
| 4676 | goto out;
|
| 4677 | }
|
| 4678 | cond_resched();
|
| 4679 | }
|
| 4680 |
|
| 4681 | if (len)
|
| 4682 | ret = f2fs_secure_erase(prev_bdev, inode, prev_index,
|
| 4683 | prev_block, len, range.flags);
|
| 4684 | f2fs_update_time(sbi, REQ_TIME);
|
| 4685 | out:
|
| 4686 | filemap_invalidate_unlock(mapping);
|
| 4687 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 4688 | err:
|
| 4689 | inode_unlock(inode);
|
| 4690 | mnt_drop_write_file(filp);
|
| 4691 |
|
| 4692 | return ret;
|
| 4693 | }
|
| 4694 |
|
| 4695 | static int f2fs_ioc_get_compress_option(struct file *filp, unsigned long arg)
|
| 4696 | {
|
| 4697 | struct inode *inode = file_inode(filp);
|
| 4698 | struct f2fs_comp_option option;
|
| 4699 |
|
| 4700 | if (!f2fs_sb_has_compression(F2FS_I_SB(inode)))
|
| 4701 | return -EOPNOTSUPP;
|
| 4702 |
|
| 4703 | inode_lock_shared(inode);
|
| 4704 |
|
| 4705 | if (!f2fs_compressed_file(inode)) {
|
| 4706 | inode_unlock_shared(inode);
|
| 4707 | return -ENODATA;
|
| 4708 | }
|
| 4709 |
|
| 4710 | option.algorithm = F2FS_I(inode)->i_compress_algorithm;
|
| 4711 | option.log_cluster_size = F2FS_I(inode)->i_log_cluster_size;
|
| 4712 |
|
| 4713 | inode_unlock_shared(inode);
|
| 4714 |
|
| 4715 | if (copy_to_user((struct f2fs_comp_option __user *)arg, &option,
|
| 4716 | sizeof(option)))
|
| 4717 | return -EFAULT;
|
| 4718 |
|
| 4719 | return 0;
|
| 4720 | }
|
| 4721 |
|
| 4722 | static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg)
|
| 4723 | {
|
| 4724 | struct inode *inode = file_inode(filp);
|
| 4725 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 4726 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 4727 | struct f2fs_comp_option option;
|
| 4728 | int ret = 0;
|
| 4729 |
|
| 4730 | if (!f2fs_sb_has_compression(sbi))
|
| 4731 | return -EOPNOTSUPP;
|
| 4732 |
|
| 4733 | if (!(filp->f_mode & FMODE_WRITE))
|
| 4734 | return -EBADF;
|
| 4735 |
|
| 4736 | if (copy_from_user(&option, (struct f2fs_comp_option __user *)arg,
|
| 4737 | sizeof(option)))
|
| 4738 | return -EFAULT;
|
| 4739 |
|
| 4740 | if (option.log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
|
| 4741 | option.log_cluster_size > MAX_COMPRESS_LOG_SIZE ||
|
| 4742 | option.algorithm >= COMPRESS_MAX)
|
| 4743 | return -EINVAL;
|
| 4744 |
|
| 4745 | ret = mnt_want_write_file(filp);
|
| 4746 | if (ret)
|
| 4747 | return ret;
|
| 4748 | inode_lock(inode);
|
| 4749 |
|
| 4750 | f2fs_down_write(&F2FS_I(inode)->i_sem);
|
| 4751 | if (!f2fs_compressed_file(inode)) {
|
| 4752 | ret = -EINVAL;
|
| 4753 | goto out;
|
| 4754 | }
|
| 4755 |
|
| 4756 | if (f2fs_is_mmap_file(inode) || get_dirty_pages(inode)) {
|
| 4757 | ret = -EBUSY;
|
| 4758 | goto out;
|
| 4759 | }
|
| 4760 |
|
| 4761 | if (F2FS_HAS_BLOCKS(inode)) {
|
| 4762 | ret = -EFBIG;
|
| 4763 | goto out;
|
| 4764 | }
|
| 4765 |
|
| 4766 | fi->i_compress_algorithm = option.algorithm;
|
| 4767 | fi->i_log_cluster_size = option.log_cluster_size;
|
| 4768 | fi->i_cluster_size = BIT(option.log_cluster_size);
|
| 4769 |
|
| 4770 | if (fi->i_compress_algorithm == COMPRESS_ZSTD)
|
| 4771 | fi->i_compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
|
| 4772 | else
|
| 4773 | fi->i_compress_level = 0;
|
| 4774 |
|
| 4775 | if (option.algorithm == F2FS_OPTION(sbi).compress_algorithm &&
|
| 4776 | F2FS_OPTION(sbi).compress_level)
|
| 4777 | fi->i_compress_level = F2FS_OPTION(sbi).compress_level;
|
| 4778 | f2fs_mark_inode_dirty_sync(inode, true);
|
| 4779 |
|
| 4780 | if (!f2fs_is_compress_backend_ready(inode))
|
| 4781 | f2fs_warn(sbi, "compression algorithm is successfully set, "
|
| 4782 | "but current kernel doesn't support this algorithm.");
|
| 4783 | out:
|
| 4784 | f2fs_up_write(&fi->i_sem);
|
| 4785 | inode_unlock(inode);
|
| 4786 | mnt_drop_write_file(filp);
|
| 4787 |
|
| 4788 | return ret;
|
| 4789 | }
|
| 4790 |
|
| 4791 | static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)
|
| 4792 | {
|
| 4793 | DEFINE_READAHEAD(ractl, NULL, NULL, inode->i_mapping, page_idx);
|
| 4794 | struct address_space *mapping = inode->i_mapping;
|
| 4795 | struct folio *folio;
|
| 4796 | pgoff_t redirty_idx = page_idx;
|
| 4797 | int page_len = 0, ret = 0;
|
| 4798 |
|
| 4799 | filemap_invalidate_lock_shared(mapping);
|
| 4800 | page_cache_ra_unbounded(&ractl, len, 0);
|
| 4801 | filemap_invalidate_unlock_shared(mapping);
|
| 4802 |
|
| 4803 | do {
|
| 4804 | folio = read_cache_folio(mapping, page_idx, NULL, NULL);
|
| 4805 | if (IS_ERR(folio)) {
|
| 4806 | ret = PTR_ERR(folio);
|
| 4807 | break;
|
| 4808 | }
|
| 4809 | page_len += folio_nr_pages(folio) - (page_idx - folio->index);
|
| 4810 | page_idx = folio_next_index(folio);
|
| 4811 | } while (page_len < len);
|
| 4812 |
|
| 4813 | while (redirty_idx < page_idx) {
|
| 4814 | folio = filemap_lock_folio(mapping, redirty_idx);
|
| 4815 |
|
| 4816 |
|
| 4817 | f2fs_bug_on(F2FS_I_SB(inode), IS_ERR(folio));
|
| 4818 |
|
| 4819 | f2fs_folio_wait_writeback(folio, DATA, true, true);
|
| 4820 |
|
| 4821 | folio_mark_dirty(folio);
|
| 4822 | folio_set_f2fs_gcing(folio);
|
| 4823 | redirty_idx = folio_next_index(folio);
|
| 4824 | folio_unlock(folio);
|
| 4825 | folio_put_refs(folio, 2);
|
| 4826 | }
|
| 4827 |
|
| 4828 | return ret;
|
| 4829 | }
|
| 4830 |
|
| 4831 | static int f2fs_ioc_decompress_file(struct file *filp)
|
| 4832 | {
|
| 4833 | struct inode *inode = file_inode(filp);
|
| 4834 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 4835 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 4836 | pgoff_t page_idx = 0, last_idx, cluster_idx;
|
| 4837 | int ret;
|
| 4838 |
|
| 4839 | if (!f2fs_sb_has_compression(sbi) ||
|
| 4840 | F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
|
| 4841 | return -EOPNOTSUPP;
|
| 4842 |
|
| 4843 | if (!(filp->f_mode & FMODE_WRITE))
|
| 4844 | return -EBADF;
|
| 4845 |
|
| 4846 | f2fs_balance_fs(sbi, true);
|
| 4847 |
|
| 4848 | ret = mnt_want_write_file(filp);
|
| 4849 | if (ret)
|
| 4850 | return ret;
|
| 4851 | inode_lock(inode);
|
| 4852 |
|
| 4853 | if (!f2fs_is_compress_backend_ready(inode)) {
|
| 4854 | ret = -EOPNOTSUPP;
|
| 4855 | goto out;
|
| 4856 | }
|
| 4857 |
|
| 4858 | if (!f2fs_compressed_file(inode) ||
|
| 4859 | is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
|
| 4860 | ret = -EINVAL;
|
| 4861 | goto out;
|
| 4862 | }
|
| 4863 |
|
| 4864 | ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
|
| 4865 | if (ret)
|
| 4866 | goto out;
|
| 4867 |
|
| 4868 | if (!atomic_read(&fi->i_compr_blocks))
|
| 4869 | goto out;
|
| 4870 |
|
| 4871 | last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
|
| 4872 | last_idx >>= fi->i_log_cluster_size;
|
| 4873 |
|
| 4874 | for (cluster_idx = 0; cluster_idx < last_idx; cluster_idx++) {
|
| 4875 | page_idx = cluster_idx << fi->i_log_cluster_size;
|
| 4876 |
|
| 4877 | if (!f2fs_is_compressed_cluster(inode, page_idx))
|
| 4878 | continue;
|
| 4879 |
|
| 4880 | ret = redirty_blocks(inode, page_idx, fi->i_cluster_size);
|
| 4881 | if (ret < 0)
|
| 4882 | break;
|
| 4883 |
|
| 4884 | if (get_dirty_pages(inode) >= BLKS_PER_SEG(sbi)) {
|
| 4885 | ret = filemap_fdatawrite(inode->i_mapping);
|
| 4886 | if (ret < 0)
|
| 4887 | break;
|
| 4888 | }
|
| 4889 |
|
| 4890 | cond_resched();
|
| 4891 | if (fatal_signal_pending(current)) {
|
| 4892 | ret = -EINTR;
|
| 4893 | break;
|
| 4894 | }
|
| 4895 | }
|
| 4896 |
|
| 4897 | if (!ret)
|
| 4898 | ret = filemap_write_and_wait_range(inode->i_mapping, 0,
|
| 4899 | LLONG_MAX);
|
| 4900 |
|
| 4901 | if (ret)
|
| 4902 | f2fs_warn(sbi, "%s: The file might be partially decompressed (errno=%d). Please delete the file.",
|
| 4903 | __func__, ret);
|
| 4904 | f2fs_update_time(sbi, REQ_TIME);
|
| 4905 | out:
|
| 4906 | inode_unlock(inode);
|
| 4907 | mnt_drop_write_file(filp);
|
| 4908 |
|
| 4909 | return ret;
|
| 4910 | }
|
| 4911 |
|
| 4912 | static int f2fs_ioc_compress_file(struct file *filp)
|
| 4913 | {
|
| 4914 | struct inode *inode = file_inode(filp);
|
| 4915 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 4916 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 4917 | pgoff_t page_idx = 0, last_idx, cluster_idx;
|
| 4918 | int ret;
|
| 4919 |
|
| 4920 | if (!f2fs_sb_has_compression(sbi) ||
|
| 4921 | F2FS_OPTION(sbi).compress_mode != COMPR_MODE_USER)
|
| 4922 | return -EOPNOTSUPP;
|
| 4923 |
|
| 4924 | if (!(filp->f_mode & FMODE_WRITE))
|
| 4925 | return -EBADF;
|
| 4926 |
|
| 4927 | f2fs_balance_fs(sbi, true);
|
| 4928 |
|
| 4929 | ret = mnt_want_write_file(filp);
|
| 4930 | if (ret)
|
| 4931 | return ret;
|
| 4932 | inode_lock(inode);
|
| 4933 |
|
| 4934 | if (!f2fs_is_compress_backend_ready(inode)) {
|
| 4935 | ret = -EOPNOTSUPP;
|
| 4936 | goto out;
|
| 4937 | }
|
| 4938 |
|
| 4939 | if (!f2fs_compressed_file(inode) ||
|
| 4940 | is_inode_flag_set(inode, FI_COMPRESS_RELEASED)) {
|
| 4941 | ret = -EINVAL;
|
| 4942 | goto out;
|
| 4943 | }
|
| 4944 |
|
| 4945 | ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
|
| 4946 | if (ret)
|
| 4947 | goto out;
|
| 4948 |
|
| 4949 | set_inode_flag(inode, FI_ENABLE_COMPRESS);
|
| 4950 |
|
| 4951 | last_idx = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
|
| 4952 | last_idx >>= fi->i_log_cluster_size;
|
| 4953 |
|
| 4954 | for (cluster_idx = 0; cluster_idx < last_idx; cluster_idx++) {
|
| 4955 | page_idx = cluster_idx << fi->i_log_cluster_size;
|
| 4956 |
|
| 4957 | if (f2fs_is_sparse_cluster(inode, page_idx))
|
| 4958 | continue;
|
| 4959 |
|
| 4960 | ret = redirty_blocks(inode, page_idx, fi->i_cluster_size);
|
| 4961 | if (ret < 0)
|
| 4962 | break;
|
| 4963 |
|
| 4964 | if (get_dirty_pages(inode) >= BLKS_PER_SEG(sbi)) {
|
| 4965 | ret = filemap_fdatawrite(inode->i_mapping);
|
| 4966 | if (ret < 0)
|
| 4967 | break;
|
| 4968 | }
|
| 4969 |
|
| 4970 | cond_resched();
|
| 4971 | if (fatal_signal_pending(current)) {
|
| 4972 | ret = -EINTR;
|
| 4973 | break;
|
| 4974 | }
|
| 4975 | }
|
| 4976 |
|
| 4977 | if (!ret)
|
| 4978 | ret = filemap_write_and_wait_range(inode->i_mapping, 0,
|
| 4979 | LLONG_MAX);
|
| 4980 |
|
| 4981 | clear_inode_flag(inode, FI_ENABLE_COMPRESS);
|
| 4982 |
|
| 4983 | if (ret)
|
| 4984 | f2fs_warn(sbi, "%s: The file might be partially compressed (errno=%d). Please delete the file.",
|
| 4985 | __func__, ret);
|
| 4986 | f2fs_update_time(sbi, REQ_TIME);
|
| 4987 | out:
|
| 4988 | inode_unlock(inode);
|
| 4989 | mnt_drop_write_file(filp);
|
| 4990 |
|
| 4991 | return ret;
|
| 4992 | }
|
| 4993 |
|
| 4994 | static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
| 4995 | {
|
| 4996 | switch (cmd) {
|
| 4997 | case FS_IOC_GETVERSION:
|
| 4998 | return f2fs_ioc_getversion(filp, arg);
|
| 4999 | case F2FS_IOC_START_ATOMIC_WRITE:
|
| 5000 | return f2fs_ioc_start_atomic_write(filp, false);
|
| 5001 | case F2FS_IOC_START_ATOMIC_REPLACE:
|
| 5002 | return f2fs_ioc_start_atomic_write(filp, true);
|
| 5003 | case F2FS_IOC_COMMIT_ATOMIC_WRITE:
|
| 5004 | return f2fs_ioc_commit_atomic_write(filp);
|
| 5005 | case F2FS_IOC_ABORT_ATOMIC_WRITE:
|
| 5006 | return f2fs_ioc_abort_atomic_write(filp);
|
| 5007 | case F2FS_IOC_START_VOLATILE_WRITE:
|
| 5008 | case F2FS_IOC_RELEASE_VOLATILE_WRITE:
|
| 5009 | return -EOPNOTSUPP;
|
| 5010 | case F2FS_IOC_SHUTDOWN:
|
| 5011 | return f2fs_ioc_shutdown(filp, arg);
|
| 5012 | case FITRIM:
|
| 5013 | return f2fs_ioc_fitrim(filp, arg);
|
| 5014 | case FS_IOC_SET_ENCRYPTION_POLICY:
|
| 5015 | return f2fs_ioc_set_encryption_policy(filp, arg);
|
| 5016 | case FS_IOC_GET_ENCRYPTION_POLICY:
|
| 5017 | return f2fs_ioc_get_encryption_policy(filp, arg);
|
| 5018 | case FS_IOC_GET_ENCRYPTION_PWSALT:
|
| 5019 | return f2fs_ioc_get_encryption_pwsalt(filp, arg);
|
| 5020 | case FS_IOC_GET_ENCRYPTION_POLICY_EX:
|
| 5021 | return f2fs_ioc_get_encryption_policy_ex(filp, arg);
|
| 5022 | case FS_IOC_ADD_ENCRYPTION_KEY:
|
| 5023 | return f2fs_ioc_add_encryption_key(filp, arg);
|
| 5024 | case FS_IOC_REMOVE_ENCRYPTION_KEY:
|
| 5025 | return f2fs_ioc_remove_encryption_key(filp, arg);
|
| 5026 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
|
| 5027 | return f2fs_ioc_remove_encryption_key_all_users(filp, arg);
|
| 5028 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
|
| 5029 | return f2fs_ioc_get_encryption_key_status(filp, arg);
|
| 5030 | case FS_IOC_GET_ENCRYPTION_NONCE:
|
| 5031 | return f2fs_ioc_get_encryption_nonce(filp, arg);
|
| 5032 | case F2FS_IOC_GARBAGE_COLLECT:
|
| 5033 | return f2fs_ioc_gc(filp, arg);
|
| 5034 | case F2FS_IOC_GARBAGE_COLLECT_RANGE:
|
| 5035 | return f2fs_ioc_gc_range(filp, arg);
|
| 5036 | case F2FS_IOC_WRITE_CHECKPOINT:
|
| 5037 | return f2fs_ioc_write_checkpoint(filp);
|
| 5038 | case F2FS_IOC_DEFRAGMENT:
|
| 5039 | return f2fs_ioc_defragment(filp, arg);
|
| 5040 | case F2FS_IOC_MOVE_RANGE:
|
| 5041 | return f2fs_ioc_move_range(filp, arg);
|
| 5042 | case F2FS_IOC_FLUSH_DEVICE:
|
| 5043 | return f2fs_ioc_flush_device(filp, arg);
|
| 5044 | case F2FS_IOC_GET_FEATURES:
|
| 5045 | return f2fs_ioc_get_features(filp, arg);
|
| 5046 | case F2FS_IOC_GET_PIN_FILE:
|
| 5047 | return f2fs_ioc_get_pin_file(filp, arg);
|
| 5048 | case F2FS_IOC_SET_PIN_FILE:
|
| 5049 | return f2fs_ioc_set_pin_file(filp, arg);
|
| 5050 | case F2FS_IOC_PRECACHE_EXTENTS:
|
| 5051 | return f2fs_ioc_precache_extents(filp);
|
| 5052 | case F2FS_IOC_RESIZE_FS:
|
| 5053 | return f2fs_ioc_resize_fs(filp, arg);
|
| 5054 | case FS_IOC_ENABLE_VERITY:
|
| 5055 | return f2fs_ioc_enable_verity(filp, arg);
|
| 5056 | case FS_IOC_MEASURE_VERITY:
|
| 5057 | return f2fs_ioc_measure_verity(filp, arg);
|
| 5058 | case FS_IOC_READ_VERITY_METADATA:
|
| 5059 | return f2fs_ioc_read_verity_metadata(filp, arg);
|
| 5060 | case FS_IOC_GETFSLABEL:
|
| 5061 | return f2fs_ioc_getfslabel(filp, arg);
|
| 5062 | case FS_IOC_SETFSLABEL:
|
| 5063 | return f2fs_ioc_setfslabel(filp, arg);
|
| 5064 | case F2FS_IOC_GET_COMPRESS_BLOCKS:
|
| 5065 | return f2fs_ioc_get_compress_blocks(filp, arg);
|
| 5066 | case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
|
| 5067 | return f2fs_release_compress_blocks(filp, arg);
|
| 5068 | case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
|
| 5069 | return f2fs_reserve_compress_blocks(filp, arg);
|
| 5070 | case F2FS_IOC_SEC_TRIM_FILE:
|
| 5071 | return f2fs_sec_trim_file(filp, arg);
|
| 5072 | case F2FS_IOC_GET_COMPRESS_OPTION:
|
| 5073 | return f2fs_ioc_get_compress_option(filp, arg);
|
| 5074 | case F2FS_IOC_SET_COMPRESS_OPTION:
|
| 5075 | return f2fs_ioc_set_compress_option(filp, arg);
|
| 5076 | case F2FS_IOC_DECOMPRESS_FILE:
|
| 5077 | return f2fs_ioc_decompress_file(filp);
|
| 5078 | case F2FS_IOC_COMPRESS_FILE:
|
| 5079 | return f2fs_ioc_compress_file(filp);
|
| 5080 | case F2FS_IOC_GET_DEV_ALIAS_FILE:
|
| 5081 | return f2fs_ioc_get_dev_alias_file(filp, arg);
|
| 5082 | case F2FS_IOC_GET_DEV_ALIAS_STATUS:
|
| 5083 | return f2fs_ioc_get_dev_alias_status(filp, arg);
|
| 5084 | case F2FS_IOC_IO_PRIO:
|
| 5085 | return f2fs_ioc_io_prio(filp, arg);
|
| 5086 | case F2FS_IOC_RESERVE_DEV_ALIAS:
|
| 5087 | return f2fs_ioc_reserve_dev_alias(filp);
|
| 5088 | case F2FS_IOC_RELEASE_DEV_ALIAS:
|
| 5089 | return f2fs_ioc_release_dev_alias(filp);
|
| 5090 | default:
|
| 5091 | return -ENOTTY;
|
| 5092 | }
|
| 5093 | }
|
| 5094 |
|
| 5095 | long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
| 5096 | {
|
| 5097 | if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
|
| 5098 | return -EIO;
|
| 5099 | if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(filp))))
|
| 5100 | return -ENOSPC;
|
| 5101 |
|
| 5102 | return __f2fs_ioctl(filp, cmd, arg);
|
| 5103 | }
|
| 5104 |
|
| 5105 | |
| 5106 | * Return %true if the given read or write request should use direct I/O, or
|
| 5107 | * %false if it should use buffered I/O.
|
| 5108 |
|
| 5109 | static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb,
|
| 5110 | struct iov_iter *iter)
|
| 5111 | {
|
| 5112 | unsigned int align;
|
| 5113 |
|
| 5114 | if (!(iocb->ki_flags & IOCB_DIRECT))
|
| 5115 | return false;
|
| 5116 |
|
| 5117 | if (f2fs_force_buffered_io(inode, iov_iter_rw(iter)))
|
| 5118 | return false;
|
| 5119 |
|
| 5120 | |
| 5121 | * Direct I/O not aligned to the disk's logical_block_size will be
|
| 5122 | * attempted, but will fail with -EINVAL.
|
| 5123 | *
|
| 5124 | * f2fs additionally requires that direct I/O be aligned to the
|
| 5125 | * filesystem block size, which is often a stricter requirement.
|
| 5126 | * However, f2fs traditionally falls back to buffered I/O on requests
|
| 5127 | * that are logical_block_size-aligned but not fs-block aligned.
|
| 5128 | *
|
| 5129 | * The below logic implements this behavior.
|
| 5130 |
|
| 5131 | align = iocb->ki_pos | iov_iter_alignment(iter);
|
| 5132 | if (!IS_ALIGNED(align, i_blocksize(inode)) &&
|
| 5133 | IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev)))
|
| 5134 | return false;
|
| 5135 |
|
| 5136 | return true;
|
| 5137 | }
|
| 5138 |
|
| 5139 | #ifdef CONFIG_F2FS_IOSTAT
|
| 5140 | static void f2fs_dio_end_bio(struct bio *bio)
|
| 5141 | {
|
| 5142 | struct bio_iostat_ctx *iostat_ctx = bio->bi_private;
|
| 5143 | void *orig_bi_private = iostat_ctx->post_read_ctx;
|
| 5144 |
|
| 5145 | iostat_update_and_unbind_ctx(bio);
|
| 5146 | bio->bi_private = orig_bi_private;
|
| 5147 | iomap_dio_bio_end_io(bio);
|
| 5148 | }
|
| 5149 |
|
| 5150 | static void f2fs_dio_iostat_start(struct f2fs_sb_info *sbi, struct bio *bio)
|
| 5151 | {
|
| 5152 | void *bi_private = bio->bi_private;
|
| 5153 |
|
| 5154 | if (!sbi->iostat_enable)
|
| 5155 | return;
|
| 5156 |
|
| 5157 | iostat_alloc_and_bind_ctx(sbi, bio, bi_private);
|
| 5158 | iostat_update_submit_ctx(bio, DATA);
|
| 5159 | bio->bi_end_io = f2fs_dio_end_bio;
|
| 5160 | }
|
| 5161 | #else
|
| 5162 | static inline void f2fs_dio_iostat_start(struct f2fs_sb_info *sbi,
|
| 5163 | struct bio *bio) {}
|
| 5164 | #endif
|
| 5165 |
|
| 5166 | static int f2fs_dio_read_end_io(struct kiocb *iocb, ssize_t size, int error,
|
| 5167 | unsigned int flags)
|
| 5168 | {
|
| 5169 | struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
|
| 5170 |
|
| 5171 | dec_page_count(sbi, F2FS_DIO_READ);
|
| 5172 | if (error)
|
| 5173 | return error;
|
| 5174 | f2fs_update_iostat(sbi, NULL, APP_DIRECT_READ_IO, size);
|
| 5175 | return 0;
|
| 5176 | }
|
| 5177 |
|
| 5178 | static void f2fs_dio_read_submit_io(const struct iomap_iter *iter,
|
| 5179 | struct bio *bio, loff_t file_offset)
|
| 5180 | {
|
| 5181 | struct f2fs_sb_info *sbi = F2FS_I_SB(iter->inode);
|
| 5182 |
|
| 5183 | f2fs_dio_iostat_start(sbi, bio);
|
| 5184 | blk_crypto_submit_bio(bio);
|
| 5185 | }
|
| 5186 |
|
| 5187 | static const struct iomap_dio_ops f2fs_iomap_dio_read_ops = {
|
| 5188 | .end_io = f2fs_dio_read_end_io,
|
| 5189 | .submit_io = f2fs_dio_read_submit_io,
|
| 5190 | };
|
| 5191 |
|
| 5192 | static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
|
| 5193 | {
|
| 5194 | struct file *file = iocb->ki_filp;
|
| 5195 | struct inode *inode = file_inode(file);
|
| 5196 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 5197 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 5198 | const loff_t pos = iocb->ki_pos;
|
| 5199 | const size_t count = iov_iter_count(to);
|
| 5200 | struct iomap_dio *dio;
|
| 5201 | ssize_t ret;
|
| 5202 |
|
| 5203 | if (count == 0)
|
| 5204 | return 0;
|
| 5205 |
|
| 5206 | trace_f2fs_direct_IO_enter(inode, iocb, count, READ);
|
| 5207 |
|
| 5208 | if (iocb->ki_flags & IOCB_NOWAIT) {
|
| 5209 | if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
|
| 5210 | ret = -EAGAIN;
|
| 5211 | goto out;
|
| 5212 | }
|
| 5213 | } else {
|
| 5214 | f2fs_down_read(&fi->i_gc_rwsem[READ]);
|
| 5215 | }
|
| 5216 |
|
| 5217 |
|
| 5218 | if (f2fs_is_atomic_file(inode)) {
|
| 5219 | f2fs_up_read(&fi->i_gc_rwsem[READ]);
|
| 5220 | ret = -EOPNOTSUPP;
|
| 5221 | goto out;
|
| 5222 | }
|
| 5223 |
|
| 5224 | |
| 5225 | * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
|
| 5226 | * the higher-level function iomap_dio_rw() in order to ensure that the
|
| 5227 | * F2FS_DIO_READ counter will be decremented correctly in all cases.
|
| 5228 |
|
| 5229 | inc_page_count(sbi, F2FS_DIO_READ);
|
| 5230 | dio = __iomap_dio_rw(iocb, to, &f2fs_iomap_ops,
|
| 5231 | &f2fs_iomap_dio_read_ops, 0, NULL, 0);
|
| 5232 | if (IS_ERR_OR_NULL(dio)) {
|
| 5233 | ret = PTR_ERR_OR_ZERO(dio);
|
| 5234 | if (ret != -EIOCBQUEUED)
|
| 5235 | dec_page_count(sbi, F2FS_DIO_READ);
|
| 5236 | } else {
|
| 5237 | ret = iomap_dio_complete(dio);
|
| 5238 | }
|
| 5239 |
|
| 5240 | f2fs_up_read(&fi->i_gc_rwsem[READ]);
|
| 5241 |
|
| 5242 | file_accessed(file);
|
| 5243 | out:
|
| 5244 | trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret);
|
| 5245 | return ret;
|
| 5246 | }
|
| 5247 |
|
| 5248 | static void f2fs_trace_rw_file_path(struct file *file, loff_t pos, size_t count,
|
| 5249 | int rw)
|
| 5250 | {
|
| 5251 | struct inode *inode = file_inode(file);
|
| 5252 | char *buf, *path;
|
| 5253 |
|
| 5254 | buf = f2fs_getname(F2FS_I_SB(inode));
|
| 5255 | if (!buf)
|
| 5256 | return;
|
| 5257 | path = dentry_path_raw(file_dentry(file), buf, PATH_MAX);
|
| 5258 | if (IS_ERR(path))
|
| 5259 | goto free_buf;
|
| 5260 | if (rw == WRITE)
|
| 5261 | trace_f2fs_datawrite_start(inode, pos, count,
|
| 5262 | current->pid, path, current->comm);
|
| 5263 | else
|
| 5264 | trace_f2fs_dataread_start(inode, pos, count,
|
| 5265 | current->pid, path, current->comm);
|
| 5266 | free_buf:
|
| 5267 | f2fs_putname(buf);
|
| 5268 | }
|
| 5269 |
|
| 5270 | static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
|
| 5271 | {
|
| 5272 | struct inode *inode = file_inode(iocb->ki_filp);
|
| 5273 | const loff_t pos = iocb->ki_pos;
|
| 5274 | ssize_t ret;
|
| 5275 | bool dio;
|
| 5276 |
|
| 5277 | if (!f2fs_is_compress_backend_ready(inode))
|
| 5278 | return -EOPNOTSUPP;
|
| 5279 |
|
| 5280 | if (trace_f2fs_dataread_start_enabled())
|
| 5281 | f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
|
| 5282 | iov_iter_count(to), READ);
|
| 5283 |
|
| 5284 | dio = f2fs_should_use_dio(inode, iocb, to);
|
| 5285 |
|
| 5286 |
|
| 5287 | if (f2fs_lfs_mode(F2FS_I_SB(inode)) &&
|
| 5288 | get_pages(F2FS_I_SB(inode), F2FS_DIO_WRITE) &&
|
| 5289 | (!f2fs_is_pinned_file(inode) || !dio))
|
| 5290 | inode_dio_wait(inode);
|
| 5291 |
|
| 5292 | if (dio) {
|
| 5293 | ret = f2fs_dio_read_iter(iocb, to);
|
| 5294 | } else {
|
| 5295 | ret = filemap_read(iocb, to, 0);
|
| 5296 | if (ret > 0)
|
| 5297 | f2fs_update_iostat(F2FS_I_SB(inode), inode,
|
| 5298 | APP_BUFFERED_READ_IO, ret);
|
| 5299 | }
|
| 5300 | trace_f2fs_dataread_end(inode, pos, ret);
|
| 5301 | return ret;
|
| 5302 | }
|
| 5303 |
|
| 5304 | static ssize_t f2fs_file_splice_read(struct file *in, loff_t *ppos,
|
| 5305 | struct pipe_inode_info *pipe,
|
| 5306 | size_t len, unsigned int flags)
|
| 5307 | {
|
| 5308 | struct inode *inode = file_inode(in);
|
| 5309 | const loff_t pos = *ppos;
|
| 5310 | ssize_t ret;
|
| 5311 |
|
| 5312 | if (!f2fs_is_compress_backend_ready(inode))
|
| 5313 | return -EOPNOTSUPP;
|
| 5314 |
|
| 5315 | if (trace_f2fs_dataread_start_enabled())
|
| 5316 | f2fs_trace_rw_file_path(in, pos, len, READ);
|
| 5317 |
|
| 5318 | ret = filemap_splice_read(in, ppos, pipe, len, flags);
|
| 5319 | if (ret > 0)
|
| 5320 | f2fs_update_iostat(F2FS_I_SB(inode), inode,
|
| 5321 | APP_BUFFERED_READ_IO, ret);
|
| 5322 |
|
| 5323 | trace_f2fs_dataread_end(inode, pos, ret);
|
| 5324 | return ret;
|
| 5325 | }
|
| 5326 |
|
| 5327 | static ssize_t f2fs_write_checks(struct kiocb *iocb, struct iov_iter *from)
|
| 5328 | {
|
| 5329 | struct file *file = iocb->ki_filp;
|
| 5330 | struct inode *inode = file_inode(file);
|
| 5331 | ssize_t count;
|
| 5332 | int err;
|
| 5333 |
|
| 5334 | if (IS_IMMUTABLE(inode))
|
| 5335 | return -EPERM;
|
| 5336 |
|
| 5337 | if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
|
| 5338 | return -EPERM;
|
| 5339 |
|
| 5340 | count = generic_write_checks(iocb, from);
|
| 5341 | if (count <= 0)
|
| 5342 | return count;
|
| 5343 |
|
| 5344 | err = file_modified(file);
|
| 5345 | if (err)
|
| 5346 | return err;
|
| 5347 |
|
| 5348 | err = f2fs_zero_post_eof_page(inode,
|
| 5349 | iocb->ki_pos + iov_iter_count(from), true, true);
|
| 5350 | if (err)
|
| 5351 | return err;
|
| 5352 | return count;
|
| 5353 | }
|
| 5354 |
|
| 5355 | |
| 5356 | * Preallocate blocks for a write request, if it is possible and helpful to do
|
| 5357 | * so. Returns a positive number if blocks may have been preallocated, 0 if no
|
| 5358 | * blocks were preallocated, or a negative errno value if something went
|
| 5359 | * seriously wrong. Also sets FI_PREALLOCATED_ALL on the inode if *all* the
|
| 5360 | * requested blocks (not just some of them) have been allocated.
|
| 5361 |
|
| 5362 | static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,
|
| 5363 | bool dio)
|
| 5364 | {
|
| 5365 | struct inode *inode = file_inode(iocb->ki_filp);
|
| 5366 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 5367 | const loff_t pos = iocb->ki_pos;
|
| 5368 | const size_t count = iov_iter_count(iter);
|
| 5369 | struct f2fs_map_blocks map = {};
|
| 5370 | int flag;
|
| 5371 | int ret;
|
| 5372 |
|
| 5373 |
|
| 5374 | if (dio && f2fs_lfs_mode(sbi))
|
| 5375 | return 0;
|
| 5376 | |
| 5377 | * Don't preallocate holes aligned to DIO_SKIP_HOLES which turns into
|
| 5378 | * buffered IO, if DIO meets any holes.
|
| 5379 |
|
| 5380 | if (dio && i_size_read(inode) &&
|
| 5381 | (F2FS_BYTES_TO_BLK(pos) < F2FS_BLK_ALIGN(i_size_read(inode))))
|
| 5382 | return 0;
|
| 5383 |
|
| 5384 |
|
| 5385 | if (iocb->ki_flags & IOCB_NOWAIT)
|
| 5386 | return 0;
|
| 5387 |
|
| 5388 |
|
| 5389 | if (fault_in_iov_iter_readable(iter, count))
|
| 5390 | return 0;
|
| 5391 |
|
| 5392 | if (f2fs_has_inline_data(inode)) {
|
| 5393 |
|
| 5394 | if (pos + count <= MAX_INLINE_DATA(inode))
|
| 5395 | return 0;
|
| 5396 | ret = f2fs_convert_inline_inode(inode);
|
| 5397 | if (ret)
|
| 5398 | return ret;
|
| 5399 | }
|
| 5400 |
|
| 5401 |
|
| 5402 | map.m_lblk = F2FS_BLK_ALIGN(pos);
|
| 5403 | map.m_len = F2FS_BYTES_TO_BLK(pos + count);
|
| 5404 | if (map.m_len > map.m_lblk)
|
| 5405 | map.m_len -= map.m_lblk;
|
| 5406 | else
|
| 5407 | return 0;
|
| 5408 |
|
| 5409 | if (!IS_DEVICE_ALIASING(inode))
|
| 5410 | map.m_may_create = true;
|
| 5411 | if (dio) {
|
| 5412 | map.m_seg_type = f2fs_rw_hint_to_seg_type(sbi,
|
| 5413 | inode->i_write_hint);
|
| 5414 | flag = F2FS_GET_BLOCK_PRE_DIO;
|
| 5415 | } else {
|
| 5416 | map.m_seg_type = NO_CHECK_TYPE;
|
| 5417 | flag = F2FS_GET_BLOCK_PRE_AIO;
|
| 5418 | }
|
| 5419 |
|
| 5420 | ret = f2fs_map_blocks(inode, &map, flag);
|
| 5421 |
|
| 5422 | if (ret < 0 && !((ret == -ENOSPC || ret == -EDQUOT) && map.m_len > 0))
|
| 5423 | return ret;
|
| 5424 | if (ret == 0)
|
| 5425 | set_inode_flag(inode, FI_PREALLOCATED_ALL);
|
| 5426 | return map.m_len;
|
| 5427 | }
|
| 5428 |
|
| 5429 | static ssize_t f2fs_buffered_write_iter(struct kiocb *iocb,
|
| 5430 | struct iov_iter *from)
|
| 5431 | {
|
| 5432 | struct file *file = iocb->ki_filp;
|
| 5433 | struct inode *inode = file_inode(file);
|
| 5434 | ssize_t ret;
|
| 5435 |
|
| 5436 | if (iocb->ki_flags & IOCB_NOWAIT)
|
| 5437 | return -EOPNOTSUPP;
|
| 5438 |
|
| 5439 | ret = generic_perform_write(iocb, from);
|
| 5440 |
|
| 5441 | if (ret > 0) {
|
| 5442 | f2fs_update_iostat(F2FS_I_SB(inode), inode,
|
| 5443 | APP_BUFFERED_IO, ret);
|
| 5444 | }
|
| 5445 | return ret;
|
| 5446 | }
|
| 5447 |
|
| 5448 | static int f2fs_dio_write_end_io(struct kiocb *iocb, ssize_t size, int error,
|
| 5449 | unsigned int flags)
|
| 5450 | {
|
| 5451 | struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(iocb->ki_filp));
|
| 5452 |
|
| 5453 | dec_page_count(sbi, F2FS_DIO_WRITE);
|
| 5454 | if (error)
|
| 5455 | return error;
|
| 5456 | f2fs_update_time(sbi, REQ_TIME);
|
| 5457 | f2fs_update_iostat(sbi, NULL, APP_DIRECT_IO, size);
|
| 5458 | return 0;
|
| 5459 | }
|
| 5460 |
|
| 5461 | static bool f2fs_valid_write_stream(struct f2fs_sb_info *sbi, u8 write_stream)
|
| 5462 | {
|
| 5463 | int i;
|
| 5464 |
|
| 5465 | if (!write_stream)
|
| 5466 | return true;
|
| 5467 | if (!f2fs_is_multi_device(sbi))
|
| 5468 | return write_stream <= bdev_max_write_streams(sbi->sb->s_bdev);
|
| 5469 |
|
| 5470 | for (i = 0; i < sbi->s_ndevs; i++)
|
| 5471 | if (write_stream > bdev_max_write_streams(FDEV(i).bdev))
|
| 5472 | return false;
|
| 5473 | return true;
|
| 5474 | }
|
| 5475 |
|
| 5476 | static void f2fs_dio_write_submit_io(const struct iomap_iter *iter,
|
| 5477 | struct bio *bio, loff_t file_offset)
|
| 5478 | {
|
| 5479 | struct inode *inode = iter->inode;
|
| 5480 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 5481 | struct kiocb *iocb = iter->private;
|
| 5482 | enum log_type type = f2fs_rw_hint_to_seg_type(sbi, inode->i_write_hint);
|
| 5483 | enum temp_type temp = f2fs_get_segment_temp(sbi, type);
|
| 5484 |
|
| 5485 | bio->bi_write_hint = f2fs_io_type_to_rw_hint(sbi, DATA, temp);
|
| 5486 | bio->bi_write_stream =
|
| 5487 | iocb->ki_write_stream ? iocb->ki_write_stream :
|
| 5488 | f2fs_io_type_to_write_stream(bio->bi_bdev, DATA, temp);
|
| 5489 | f2fs_dio_iostat_start(sbi, bio);
|
| 5490 | blk_crypto_submit_bio(bio);
|
| 5491 | }
|
| 5492 |
|
| 5493 | static const struct iomap_dio_ops f2fs_iomap_dio_write_ops = {
|
| 5494 | .end_io = f2fs_dio_write_end_io,
|
| 5495 | .submit_io = f2fs_dio_write_submit_io,
|
| 5496 | };
|
| 5497 |
|
| 5498 | static void f2fs_flush_buffered_write(struct address_space *mapping,
|
| 5499 | loff_t start_pos, loff_t end_pos)
|
| 5500 | {
|
| 5501 | int ret;
|
| 5502 |
|
| 5503 | ret = filemap_write_and_wait_range(mapping, start_pos, end_pos);
|
| 5504 | if (ret < 0)
|
| 5505 | return;
|
| 5506 | invalidate_mapping_pages(mapping,
|
| 5507 | start_pos >> PAGE_SHIFT,
|
| 5508 | end_pos >> PAGE_SHIFT);
|
| 5509 | }
|
| 5510 |
|
| 5511 | static ssize_t f2fs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from,
|
| 5512 | bool *may_need_sync)
|
| 5513 | {
|
| 5514 | struct file *file = iocb->ki_filp;
|
| 5515 | struct inode *inode = file_inode(file);
|
| 5516 | struct f2fs_inode_info *fi = F2FS_I(inode);
|
| 5517 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
|
| 5518 | const bool do_opu = f2fs_lfs_mode(sbi);
|
| 5519 | const loff_t pos = iocb->ki_pos;
|
| 5520 | const ssize_t count = iov_iter_count(from);
|
| 5521 | unsigned int dio_flags;
|
| 5522 | struct iomap_dio *dio;
|
| 5523 | ssize_t ret;
|
| 5524 |
|
| 5525 | trace_f2fs_direct_IO_enter(inode, iocb, count, WRITE);
|
| 5526 |
|
| 5527 | if (!f2fs_valid_write_stream(sbi, iocb->ki_write_stream)) {
|
| 5528 | ret = -EINVAL;
|
| 5529 | goto out;
|
| 5530 | }
|
| 5531 |
|
| 5532 | if (iocb->ki_flags & IOCB_NOWAIT) {
|
| 5533 |
|
| 5534 | if (f2fs_has_inline_data(inode) ||
|
| 5535 | !f2fs_overwrite_io(inode, pos, count)) {
|
| 5536 | ret = -EAGAIN;
|
| 5537 | goto out;
|
| 5538 | }
|
| 5539 |
|
| 5540 | if (!f2fs_down_read_trylock(&fi->i_gc_rwsem[WRITE])) {
|
| 5541 | ret = -EAGAIN;
|
| 5542 | goto out;
|
| 5543 | }
|
| 5544 | if (do_opu && !f2fs_down_read_trylock(&fi->i_gc_rwsem[READ])) {
|
| 5545 | f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
|
| 5546 | ret = -EAGAIN;
|
| 5547 | goto out;
|
| 5548 | }
|
| 5549 | } else {
|
| 5550 | ret = f2fs_convert_inline_inode(inode);
|
| 5551 | if (ret)
|
| 5552 | goto out;
|
| 5553 |
|
| 5554 | f2fs_down_read(&fi->i_gc_rwsem[WRITE]);
|
| 5555 | if (do_opu)
|
| 5556 | f2fs_down_read(&fi->i_gc_rwsem[READ]);
|
| 5557 | }
|
| 5558 |
|
| 5559 | |
| 5560 | * We have to use __iomap_dio_rw() and iomap_dio_complete() instead of
|
| 5561 | * the higher-level function iomap_dio_rw() in order to ensure that the
|
| 5562 | * F2FS_DIO_WRITE counter will be decremented correctly in all cases.
|
| 5563 |
|
| 5564 | inc_page_count(sbi, F2FS_DIO_WRITE);
|
| 5565 | dio_flags = 0;
|
| 5566 | if (pos + count > inode->i_size)
|
| 5567 | dio_flags |= IOMAP_DIO_FORCE_WAIT;
|
| 5568 | dio = __iomap_dio_rw(iocb, from, &f2fs_iomap_ops,
|
| 5569 | &f2fs_iomap_dio_write_ops, dio_flags, iocb, 0);
|
| 5570 | if (IS_ERR_OR_NULL(dio)) {
|
| 5571 | ret = PTR_ERR_OR_ZERO(dio);
|
| 5572 | if (ret == -ENOTBLK)
|
| 5573 | ret = 0;
|
| 5574 | if (ret != -EIOCBQUEUED)
|
| 5575 | dec_page_count(sbi, F2FS_DIO_WRITE);
|
| 5576 | } else {
|
| 5577 | ret = iomap_dio_complete(dio);
|
| 5578 | }
|
| 5579 |
|
| 5580 | if (do_opu)
|
| 5581 | f2fs_up_read(&fi->i_gc_rwsem[READ]);
|
| 5582 | f2fs_up_read(&fi->i_gc_rwsem[WRITE]);
|
| 5583 |
|
| 5584 | if (ret < 0)
|
| 5585 | goto out;
|
| 5586 | if (pos + ret > inode->i_size)
|
| 5587 | f2fs_i_size_write(inode, pos + ret);
|
| 5588 | if (!do_opu)
|
| 5589 | set_inode_flag(inode, FI_UPDATE_WRITE);
|
| 5590 |
|
| 5591 | if (iov_iter_count(from)) {
|
| 5592 | ssize_t ret2;
|
| 5593 | loff_t bufio_start_pos = iocb->ki_pos;
|
| 5594 |
|
| 5595 | |
| 5596 | * The direct write was partial, so we need to fall back to a
|
| 5597 | * buffered write for the remainder.
|
| 5598 |
|
| 5599 |
|
| 5600 | ret2 = f2fs_buffered_write_iter(iocb, from);
|
| 5601 | if (iov_iter_count(from))
|
| 5602 | f2fs_write_failed(inode, iocb->ki_pos);
|
| 5603 | if (ret2 < 0)
|
| 5604 | goto out;
|
| 5605 |
|
| 5606 | |
| 5607 | * Ensure that the pagecache pages are written to disk and
|
| 5608 | * invalidated to preserve the expected O_DIRECT semantics.
|
| 5609 |
|
| 5610 | if (ret2 > 0) {
|
| 5611 | loff_t bufio_end_pos = bufio_start_pos + ret2 - 1;
|
| 5612 |
|
| 5613 | ret += ret2;
|
| 5614 |
|
| 5615 | f2fs_flush_buffered_write(file->f_mapping,
|
| 5616 | bufio_start_pos,
|
| 5617 | bufio_end_pos);
|
| 5618 | }
|
| 5619 | } else {
|
| 5620 |
|
| 5621 | *may_need_sync = false;
|
| 5622 | }
|
| 5623 | out:
|
| 5624 | trace_f2fs_direct_IO_exit(inode, pos, count, WRITE, ret);
|
| 5625 | return ret;
|
| 5626 | }
|
| 5627 |
|
| 5628 | static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
|
| 5629 | {
|
| 5630 | struct inode *inode = file_inode(iocb->ki_filp);
|
| 5631 | const loff_t orig_pos = iocb->ki_pos;
|
| 5632 | const size_t orig_count = iov_iter_count(from);
|
| 5633 | loff_t target_size;
|
| 5634 | bool dio;
|
| 5635 | bool may_need_sync = true;
|
| 5636 | int preallocated;
|
| 5637 | ssize_t ret;
|
| 5638 | loff_t bufio_start_pos;
|
| 5639 |
|
| 5640 | if (unlikely(f2fs_cp_error(F2FS_I_SB(inode)))) {
|
| 5641 | ret = -EIO;
|
| 5642 | goto out;
|
| 5643 | }
|
| 5644 |
|
| 5645 | if (!f2fs_is_compress_backend_ready(inode)) {
|
| 5646 | ret = -EOPNOTSUPP;
|
| 5647 | goto out;
|
| 5648 | }
|
| 5649 |
|
| 5650 | if (iocb->ki_flags & IOCB_NOWAIT) {
|
| 5651 | if (!inode_trylock(inode)) {
|
| 5652 | ret = -EAGAIN;
|
| 5653 | goto out;
|
| 5654 | }
|
| 5655 | } else {
|
| 5656 | inode_lock(inode);
|
| 5657 | }
|
| 5658 |
|
| 5659 | ret = f2fs_write_checks(iocb, from);
|
| 5660 | if (ret <= 0)
|
| 5661 | goto out_unlock;
|
| 5662 |
|
| 5663 | if (f2fs_is_pinned_file(inode) &&
|
| 5664 | !f2fs_overwrite_io(inode, iocb->ki_pos, iov_iter_count(from))) {
|
| 5665 | ret = -EIO;
|
| 5666 | goto out_unlock;
|
| 5667 | }
|
| 5668 |
|
| 5669 | bufio_start_pos = iocb->ki_pos;
|
| 5670 |
|
| 5671 |
|
| 5672 | dio = f2fs_should_use_dio(inode, iocb, from);
|
| 5673 |
|
| 5674 |
|
| 5675 | if (dio && f2fs_is_atomic_file(inode)) {
|
| 5676 | ret = -EOPNOTSUPP;
|
| 5677 | goto out_unlock;
|
| 5678 | }
|
| 5679 |
|
| 5680 |
|
| 5681 | target_size = iocb->ki_pos + iov_iter_count(from);
|
| 5682 | preallocated = f2fs_preallocate_blocks(iocb, from, dio);
|
| 5683 | if (preallocated < 0) {
|
| 5684 | ret = preallocated;
|
| 5685 | } else {
|
| 5686 | if (trace_f2fs_datawrite_start_enabled())
|
| 5687 | f2fs_trace_rw_file_path(iocb->ki_filp, iocb->ki_pos,
|
| 5688 | orig_count, WRITE);
|
| 5689 |
|
| 5690 |
|
| 5691 | ret = dio ?
|
| 5692 | f2fs_dio_write_iter(iocb, from, &may_need_sync) :
|
| 5693 | f2fs_buffered_write_iter(iocb, from);
|
| 5694 |
|
| 5695 | trace_f2fs_datawrite_end(inode, orig_pos, ret);
|
| 5696 | }
|
| 5697 |
|
| 5698 |
|
| 5699 | if (preallocated && i_size_read(inode) < target_size) {
|
| 5700 | f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 5701 | filemap_invalidate_lock(inode->i_mapping);
|
| 5702 | if (!f2fs_truncate(inode))
|
| 5703 | file_dont_truncate(inode);
|
| 5704 | filemap_invalidate_unlock(inode->i_mapping);
|
| 5705 | f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
|
| 5706 | } else {
|
| 5707 | file_dont_truncate(inode);
|
| 5708 | }
|
| 5709 |
|
| 5710 | clear_inode_flag(inode, FI_PREALLOCATED_ALL);
|
| 5711 | out_unlock:
|
| 5712 | inode_unlock(inode);
|
| 5713 | out:
|
| 5714 | trace_f2fs_file_write_iter(inode, orig_pos, orig_count, ret);
|
| 5715 |
|
| 5716 | if (ret > 0 && may_need_sync)
|
| 5717 | ret = generic_write_sync(iocb, ret);
|
| 5718 |
|
| 5719 | |
| 5720 | * the page cache to preserve O_DIRECT semantics
|
| 5721 |
|
| 5722 | if (ret > 0 && !dio && (iocb->ki_flags & IOCB_DIRECT))
|
| 5723 | f2fs_flush_buffered_write(iocb->ki_filp->f_mapping,
|
| 5724 | bufio_start_pos,
|
| 5725 | bufio_start_pos + ret - 1);
|
| 5726 |
|
| 5727 | return ret;
|
| 5728 | }
|
| 5729 |
|
| 5730 | static int f2fs_file_fadvise(struct file *filp, loff_t offset, loff_t len,
|
| 5731 | int advice)
|
| 5732 | {
|
| 5733 | struct address_space *mapping;
|
| 5734 | struct backing_dev_info *bdi;
|
| 5735 | struct inode *inode = file_inode(filp);
|
| 5736 | int err;
|
| 5737 |
|
| 5738 | trace_f2fs_fadvise(inode, offset, len, advice);
|
| 5739 |
|
| 5740 | if (advice == POSIX_FADV_SEQUENTIAL) {
|
| 5741 | if (S_ISFIFO(inode->i_mode))
|
| 5742 | return -ESPIPE;
|
| 5743 |
|
| 5744 | mapping = filp->f_mapping;
|
| 5745 | if (!mapping || len < 0)
|
| 5746 | return -EINVAL;
|
| 5747 |
|
| 5748 | bdi = inode_to_bdi(mapping->host);
|
| 5749 | filp->f_ra.ra_pages = bdi->ra_pages *
|
| 5750 | F2FS_I_SB(inode)->seq_file_ra_mul;
|
| 5751 | spin_lock(&filp->f_lock);
|
| 5752 | filp->f_mode &= ~FMODE_RANDOM;
|
| 5753 | spin_unlock(&filp->f_lock);
|
| 5754 | return 0;
|
| 5755 | } else if (advice == POSIX_FADV_WILLNEED && offset == 0) {
|
| 5756 |
|
| 5757 | f2fs_precache_extents(inode);
|
| 5758 | }
|
| 5759 |
|
| 5760 | err = generic_fadvise(filp, offset, len, advice);
|
| 5761 | if (err)
|
| 5762 | return err;
|
| 5763 |
|
| 5764 | if (advice == POSIX_FADV_DONTNEED &&
|
| 5765 | (test_opt(F2FS_I_SB(inode), COMPRESS_CACHE) &&
|
| 5766 | f2fs_compressed_file(inode)))
|
| 5767 | f2fs_invalidate_compress_pages(F2FS_I_SB(inode), inode->i_ino);
|
| 5768 | else if (advice == POSIX_FADV_NOREUSE)
|
| 5769 | err = f2fs_keep_noreuse_range(inode, offset, len);
|
| 5770 | return err;
|
| 5771 | }
|
| 5772 |
|
| 5773 | #ifdef CONFIG_COMPAT
|
| 5774 | struct compat_f2fs_gc_range {
|
| 5775 | u32 sync;
|
| 5776 | compat_u64 start;
|
| 5777 | compat_u64 len;
|
| 5778 | };
|
| 5779 | #define F2FS_IOC32_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11,\
|
| 5780 | struct compat_f2fs_gc_range)
|
| 5781 |
|
| 5782 | static int f2fs_compat_ioc_gc_range(struct file *file, unsigned long arg)
|
| 5783 | {
|
| 5784 | struct compat_f2fs_gc_range __user *urange;
|
| 5785 | struct f2fs_gc_range range;
|
| 5786 | int err;
|
| 5787 |
|
| 5788 | urange = compat_ptr(arg);
|
| 5789 | err = get_user(range.sync, &urange->sync);
|
| 5790 | err |= get_user(range.start, &urange->start);
|
| 5791 | err |= get_user(range.len, &urange->len);
|
| 5792 | if (err)
|
| 5793 | return -EFAULT;
|
| 5794 |
|
| 5795 | return __f2fs_ioc_gc_range(file, &range);
|
| 5796 | }
|
| 5797 |
|
| 5798 | struct compat_f2fs_move_range {
|
| 5799 | u32 dst_fd;
|
| 5800 | compat_u64 pos_in;
|
| 5801 | compat_u64 pos_out;
|
| 5802 | compat_u64 len;
|
| 5803 | };
|
| 5804 | #define F2FS_IOC32_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
|
| 5805 | struct compat_f2fs_move_range)
|
| 5806 |
|
| 5807 | static int f2fs_compat_ioc_move_range(struct file *file, unsigned long arg)
|
| 5808 | {
|
| 5809 | struct compat_f2fs_move_range __user *urange;
|
| 5810 | struct f2fs_move_range range;
|
| 5811 | int err;
|
| 5812 |
|
| 5813 | urange = compat_ptr(arg);
|
| 5814 | err = get_user(range.dst_fd, &urange->dst_fd);
|
| 5815 | err |= get_user(range.pos_in, &urange->pos_in);
|
| 5816 | err |= get_user(range.pos_out, &urange->pos_out);
|
| 5817 | err |= get_user(range.len, &urange->len);
|
| 5818 | if (err)
|
| 5819 | return -EFAULT;
|
| 5820 |
|
| 5821 | return __f2fs_ioc_move_range(file, &range);
|
| 5822 | }
|
| 5823 |
|
| 5824 | long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
|
| 5825 | {
|
| 5826 | if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
|
| 5827 | return -EIO;
|
| 5828 | if (!f2fs_is_checkpoint_ready(F2FS_I_SB(file_inode(file))))
|
| 5829 | return -ENOSPC;
|
| 5830 |
|
| 5831 | switch (cmd) {
|
| 5832 | case FS_IOC32_GETVERSION:
|
| 5833 | cmd = FS_IOC_GETVERSION;
|
| 5834 | break;
|
| 5835 | case F2FS_IOC32_GARBAGE_COLLECT_RANGE:
|
| 5836 | return f2fs_compat_ioc_gc_range(file, arg);
|
| 5837 | case F2FS_IOC32_MOVE_RANGE:
|
| 5838 | return f2fs_compat_ioc_move_range(file, arg);
|
| 5839 | case F2FS_IOC_START_ATOMIC_WRITE:
|
| 5840 | case F2FS_IOC_START_ATOMIC_REPLACE:
|
| 5841 | case F2FS_IOC_COMMIT_ATOMIC_WRITE:
|
| 5842 | case F2FS_IOC_START_VOLATILE_WRITE:
|
| 5843 | case F2FS_IOC_RELEASE_VOLATILE_WRITE:
|
| 5844 | case F2FS_IOC_ABORT_ATOMIC_WRITE:
|
| 5845 | case F2FS_IOC_SHUTDOWN:
|
| 5846 | case FITRIM:
|
| 5847 | case FS_IOC_SET_ENCRYPTION_POLICY:
|
| 5848 | case FS_IOC_GET_ENCRYPTION_PWSALT:
|
| 5849 | case FS_IOC_GET_ENCRYPTION_POLICY:
|
| 5850 | case FS_IOC_GET_ENCRYPTION_POLICY_EX:
|
| 5851 | case FS_IOC_ADD_ENCRYPTION_KEY:
|
| 5852 | case FS_IOC_REMOVE_ENCRYPTION_KEY:
|
| 5853 | case FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS:
|
| 5854 | case FS_IOC_GET_ENCRYPTION_KEY_STATUS:
|
| 5855 | case FS_IOC_GET_ENCRYPTION_NONCE:
|
| 5856 | case F2FS_IOC_GARBAGE_COLLECT:
|
| 5857 | case F2FS_IOC_WRITE_CHECKPOINT:
|
| 5858 | case F2FS_IOC_DEFRAGMENT:
|
| 5859 | case F2FS_IOC_FLUSH_DEVICE:
|
| 5860 | case F2FS_IOC_GET_FEATURES:
|
| 5861 | case F2FS_IOC_GET_PIN_FILE:
|
| 5862 | case F2FS_IOC_SET_PIN_FILE:
|
| 5863 | case F2FS_IOC_PRECACHE_EXTENTS:
|
| 5864 | case F2FS_IOC_RESIZE_FS:
|
| 5865 | case FS_IOC_ENABLE_VERITY:
|
| 5866 | case FS_IOC_MEASURE_VERITY:
|
| 5867 | case FS_IOC_READ_VERITY_METADATA:
|
| 5868 | case FS_IOC_GETFSLABEL:
|
| 5869 | case FS_IOC_SETFSLABEL:
|
| 5870 | case F2FS_IOC_GET_COMPRESS_BLOCKS:
|
| 5871 | case F2FS_IOC_RELEASE_COMPRESS_BLOCKS:
|
| 5872 | case F2FS_IOC_RESERVE_COMPRESS_BLOCKS:
|
| 5873 | case F2FS_IOC_SEC_TRIM_FILE:
|
| 5874 | case F2FS_IOC_GET_COMPRESS_OPTION:
|
| 5875 | case F2FS_IOC_SET_COMPRESS_OPTION:
|
| 5876 | case F2FS_IOC_DECOMPRESS_FILE:
|
| 5877 | case F2FS_IOC_COMPRESS_FILE:
|
| 5878 | case F2FS_IOC_GET_DEV_ALIAS_FILE:
|
| 5879 | case F2FS_IOC_GET_DEV_ALIAS_STATUS:
|
| 5880 | case F2FS_IOC_IO_PRIO:
|
| 5881 | case F2FS_IOC_RESERVE_DEV_ALIAS:
|
| 5882 | case F2FS_IOC_RELEASE_DEV_ALIAS:
|
| 5883 | break;
|
| 5884 | default:
|
| 5885 | return -ENOIOCTLCMD;
|
| 5886 | }
|
| 5887 | return __f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
|
| 5888 | }
|
| 5889 | #endif
|
| 5890 |
|
| 5891 | const struct file_operations f2fs_file_operations = {
|
| 5892 | .llseek = f2fs_llseek,
|
| 5893 | .read_iter = f2fs_file_read_iter,
|
| 5894 | .write_iter = f2fs_file_write_iter,
|
| 5895 | .iopoll = iocb_bio_iopoll,
|
| 5896 | .open = f2fs_file_open,
|
| 5897 | .release = f2fs_release_file,
|
| 5898 | .mmap_prepare = f2fs_file_mmap_prepare,
|
| 5899 | .flush = f2fs_file_flush,
|
| 5900 | .fsync = f2fs_sync_file,
|
| 5901 | .fallocate = f2fs_fallocate,
|
| 5902 | .unlocked_ioctl = f2fs_ioctl,
|
| 5903 | #ifdef CONFIG_COMPAT
|
| 5904 | .compat_ioctl = f2fs_compat_ioctl,
|
| 5905 | #endif
|
| 5906 | .splice_read = f2fs_file_splice_read,
|
| 5907 | .splice_write = iter_file_splice_write,
|
| 5908 | .fadvise = f2fs_file_fadvise,
|
| 5909 | .fop_flags = FOP_BUFFER_RASYNC,
|
| 5910 | .setlease = generic_setlease,
|
| 5911 | };
|