strparser: initialize all callbacks
[linux/fpc-iii.git] / fs / f2fs / file.c
blob2706130c261b060f46cb526b8e7ac501789808a7
1 /*
2 * fs/f2fs/file.c
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
13 #include <linux/stat.h>
14 #include <linux/buffer_head.h>
15 #include <linux/writeback.h>
16 #include <linux/blkdev.h>
17 #include <linux/falloc.h>
18 #include <linux/types.h>
19 #include <linux/compat.h>
20 #include <linux/uaccess.h>
21 #include <linux/mount.h>
22 #include <linux/pagevec.h>
23 #include <linux/uio.h>
24 #include <linux/uuid.h>
25 #include <linux/file.h>
27 #include "f2fs.h"
28 #include "node.h"
29 #include "segment.h"
30 #include "xattr.h"
31 #include "acl.h"
32 #include "gc.h"
33 #include "trace.h"
34 #include <trace/events/f2fs.h>
36 static int f2fs_filemap_fault(struct vm_fault *vmf)
38 struct inode *inode = file_inode(vmf->vma->vm_file);
39 int err;
41 down_read(&F2FS_I(inode)->i_mmap_sem);
42 err = filemap_fault(vmf);
43 up_read(&F2FS_I(inode)->i_mmap_sem);
45 return err;
48 static int f2fs_vm_page_mkwrite(struct vm_fault *vmf)
50 struct page *page = vmf->page;
51 struct inode *inode = file_inode(vmf->vma->vm_file);
52 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
53 struct dnode_of_data dn;
54 int err;
56 sb_start_pagefault(inode->i_sb);
58 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
60 /* block allocation */
61 f2fs_lock_op(sbi);
62 set_new_dnode(&dn, inode, NULL, NULL, 0);
63 err = f2fs_reserve_block(&dn, page->index);
64 if (err) {
65 f2fs_unlock_op(sbi);
66 goto out;
68 f2fs_put_dnode(&dn);
69 f2fs_unlock_op(sbi);
71 f2fs_balance_fs(sbi, dn.node_changed);
73 file_update_time(vmf->vma->vm_file);
74 down_read(&F2FS_I(inode)->i_mmap_sem);
75 lock_page(page);
76 if (unlikely(page->mapping != inode->i_mapping ||
77 page_offset(page) > i_size_read(inode) ||
78 !PageUptodate(page))) {
79 unlock_page(page);
80 err = -EFAULT;
81 goto out_sem;
85 * check to see if the page is mapped already (no holes)
87 if (PageMappedToDisk(page))
88 goto mapped;
90 /* page is wholly or partially inside EOF */
91 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
92 i_size_read(inode)) {
93 unsigned offset;
94 offset = i_size_read(inode) & ~PAGE_MASK;
95 zero_user_segment(page, offset, PAGE_SIZE);
97 set_page_dirty(page);
98 if (!PageUptodate(page))
99 SetPageUptodate(page);
101 trace_f2fs_vm_page_mkwrite(page, DATA);
102 mapped:
103 /* fill the page */
104 f2fs_wait_on_page_writeback(page, DATA, false);
106 /* wait for GCed encrypted page writeback */
107 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
108 f2fs_wait_on_encrypted_page_writeback(sbi, dn.data_blkaddr);
110 out_sem:
111 up_read(&F2FS_I(inode)->i_mmap_sem);
112 out:
113 sb_end_pagefault(inode->i_sb);
114 f2fs_update_time(sbi, REQ_TIME);
115 return block_page_mkwrite_return(err);
118 static const struct vm_operations_struct f2fs_file_vm_ops = {
119 .fault = f2fs_filemap_fault,
120 .map_pages = filemap_map_pages,
121 .page_mkwrite = f2fs_vm_page_mkwrite,
124 static int get_parent_ino(struct inode *inode, nid_t *pino)
126 struct dentry *dentry;
128 inode = igrab(inode);
129 dentry = d_find_any_alias(inode);
130 iput(inode);
131 if (!dentry)
132 return 0;
134 *pino = parent_ino(dentry);
135 dput(dentry);
136 return 1;
139 static inline bool need_do_checkpoint(struct inode *inode)
141 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
142 bool need_cp = false;
144 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
145 need_cp = true;
146 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
147 need_cp = true;
148 else if (file_wrong_pino(inode))
149 need_cp = true;
150 else if (!space_for_roll_forward(sbi))
151 need_cp = true;
152 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
153 need_cp = true;
154 else if (test_opt(sbi, FASTBOOT))
155 need_cp = true;
156 else if (sbi->active_logs == 2)
157 need_cp = true;
159 return need_cp;
162 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
164 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
165 bool ret = false;
166 /* But we need to avoid that there are some inode updates */
167 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
168 ret = true;
169 f2fs_put_page(i, 0);
170 return ret;
173 static void try_to_fix_pino(struct inode *inode)
175 struct f2fs_inode_info *fi = F2FS_I(inode);
176 nid_t pino;
178 down_write(&fi->i_sem);
179 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
180 get_parent_ino(inode, &pino)) {
181 f2fs_i_pino_write(inode, pino);
182 file_got_pino(inode);
184 up_write(&fi->i_sem);
187 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
188 int datasync, bool atomic)
190 struct inode *inode = file->f_mapping->host;
191 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
192 nid_t ino = inode->i_ino;
193 int ret = 0;
194 bool need_cp = false;
195 struct writeback_control wbc = {
196 .sync_mode = WB_SYNC_ALL,
197 .nr_to_write = LONG_MAX,
198 .for_reclaim = 0,
201 if (unlikely(f2fs_readonly(inode->i_sb)))
202 return 0;
204 trace_f2fs_sync_file_enter(inode);
206 /* if fdatasync is triggered, let's do in-place-update */
207 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
208 set_inode_flag(inode, FI_NEED_IPU);
209 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
210 clear_inode_flag(inode, FI_NEED_IPU);
212 if (ret) {
213 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
214 return ret;
217 /* if the inode is dirty, let's recover all the time */
218 if (!f2fs_skip_inode_update(inode, datasync)) {
219 f2fs_write_inode(inode, NULL);
220 goto go_write;
224 * if there is no written data, don't waste time to write recovery info.
226 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
227 !exist_written_data(sbi, ino, APPEND_INO)) {
229 /* it may call write_inode just prior to fsync */
230 if (need_inode_page_update(sbi, ino))
231 goto go_write;
233 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
234 exist_written_data(sbi, ino, UPDATE_INO))
235 goto flush_out;
236 goto out;
238 go_write:
240 * Both of fdatasync() and fsync() are able to be recovered from
241 * sudden-power-off.
243 down_read(&F2FS_I(inode)->i_sem);
244 need_cp = need_do_checkpoint(inode);
245 up_read(&F2FS_I(inode)->i_sem);
247 if (need_cp) {
248 /* all the dirty node pages should be flushed for POR */
249 ret = f2fs_sync_fs(inode->i_sb, 1);
252 * We've secured consistency through sync_fs. Following pino
253 * will be used only for fsynced inodes after checkpoint.
255 try_to_fix_pino(inode);
256 clear_inode_flag(inode, FI_APPEND_WRITE);
257 clear_inode_flag(inode, FI_UPDATE_WRITE);
258 goto out;
260 sync_nodes:
261 ret = fsync_node_pages(sbi, inode, &wbc, atomic);
262 if (ret)
263 goto out;
265 /* if cp_error was enabled, we should avoid infinite loop */
266 if (unlikely(f2fs_cp_error(sbi))) {
267 ret = -EIO;
268 goto out;
271 if (need_inode_block_update(sbi, ino)) {
272 f2fs_mark_inode_dirty_sync(inode, true);
273 f2fs_write_inode(inode, NULL);
274 goto sync_nodes;
277 ret = wait_on_node_pages_writeback(sbi, ino);
278 if (ret)
279 goto out;
281 /* once recovery info is written, don't need to tack this */
282 remove_ino_entry(sbi, ino, APPEND_INO);
283 clear_inode_flag(inode, FI_APPEND_WRITE);
284 flush_out:
285 remove_ino_entry(sbi, ino, UPDATE_INO);
286 clear_inode_flag(inode, FI_UPDATE_WRITE);
287 if (!atomic)
288 ret = f2fs_issue_flush(sbi);
289 f2fs_update_time(sbi, REQ_TIME);
290 out:
291 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
292 f2fs_trace_ios(NULL, 1);
293 return ret;
296 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
298 return f2fs_do_sync_file(file, start, end, datasync, false);
301 static pgoff_t __get_first_dirty_index(struct address_space *mapping,
302 pgoff_t pgofs, int whence)
304 struct pagevec pvec;
305 int nr_pages;
307 if (whence != SEEK_DATA)
308 return 0;
310 /* find first dirty page index */
311 pagevec_init(&pvec, 0);
312 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
313 PAGECACHE_TAG_DIRTY, 1);
314 pgofs = nr_pages ? pvec.pages[0]->index : ULONG_MAX;
315 pagevec_release(&pvec);
316 return pgofs;
319 static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
320 int whence)
322 switch (whence) {
323 case SEEK_DATA:
324 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
325 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
326 return true;
327 break;
328 case SEEK_HOLE:
329 if (blkaddr == NULL_ADDR)
330 return true;
331 break;
333 return false;
336 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
338 struct inode *inode = file->f_mapping->host;
339 loff_t maxbytes = inode->i_sb->s_maxbytes;
340 struct dnode_of_data dn;
341 pgoff_t pgofs, end_offset, dirty;
342 loff_t data_ofs = offset;
343 loff_t isize;
344 int err = 0;
346 inode_lock(inode);
348 isize = i_size_read(inode);
349 if (offset >= isize)
350 goto fail;
352 /* handle inline data case */
353 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
354 if (whence == SEEK_HOLE)
355 data_ofs = isize;
356 goto found;
359 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
361 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
363 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
364 set_new_dnode(&dn, inode, NULL, NULL, 0);
365 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
366 if (err && err != -ENOENT) {
367 goto fail;
368 } else if (err == -ENOENT) {
369 /* direct node does not exists */
370 if (whence == SEEK_DATA) {
371 pgofs = get_next_page_offset(&dn, pgofs);
372 continue;
373 } else {
374 goto found;
378 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
380 /* find data/hole in dnode block */
381 for (; dn.ofs_in_node < end_offset;
382 dn.ofs_in_node++, pgofs++,
383 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
384 block_t blkaddr;
385 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
387 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
388 f2fs_put_dnode(&dn);
389 goto found;
392 f2fs_put_dnode(&dn);
395 if (whence == SEEK_DATA)
396 goto fail;
397 found:
398 if (whence == SEEK_HOLE && data_ofs > isize)
399 data_ofs = isize;
400 inode_unlock(inode);
401 return vfs_setpos(file, data_ofs, maxbytes);
402 fail:
403 inode_unlock(inode);
404 return -ENXIO;
407 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
409 struct inode *inode = file->f_mapping->host;
410 loff_t maxbytes = inode->i_sb->s_maxbytes;
412 switch (whence) {
413 case SEEK_SET:
414 case SEEK_CUR:
415 case SEEK_END:
416 return generic_file_llseek_size(file, offset, whence,
417 maxbytes, i_size_read(inode));
418 case SEEK_DATA:
419 case SEEK_HOLE:
420 if (offset < 0)
421 return -ENXIO;
422 return f2fs_seek_block(file, offset, whence);
425 return -EINVAL;
428 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
430 struct inode *inode = file_inode(file);
431 int err;
433 /* we don't need to use inline_data strictly */
434 err = f2fs_convert_inline_inode(inode);
435 if (err)
436 return err;
438 file_accessed(file);
439 vma->vm_ops = &f2fs_file_vm_ops;
440 return 0;
443 static int f2fs_file_open(struct inode *inode, struct file *filp)
445 struct dentry *dir;
447 if (f2fs_encrypted_inode(inode)) {
448 int ret = fscrypt_get_encryption_info(inode);
449 if (ret)
450 return -EACCES;
451 if (!fscrypt_has_encryption_key(inode))
452 return -ENOKEY;
454 dir = dget_parent(file_dentry(filp));
455 if (f2fs_encrypted_inode(d_inode(dir)) &&
456 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
457 dput(dir);
458 return -EPERM;
460 dput(dir);
461 return dquot_file_open(inode, filp);
464 int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
466 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
467 struct f2fs_node *raw_node;
468 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
469 __le32 *addr;
471 raw_node = F2FS_NODE(dn->node_page);
472 addr = blkaddr_in_node(raw_node) + ofs;
474 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
475 block_t blkaddr = le32_to_cpu(*addr);
476 if (blkaddr == NULL_ADDR)
477 continue;
479 dn->data_blkaddr = NULL_ADDR;
480 set_data_blkaddr(dn);
481 invalidate_blocks(sbi, blkaddr);
482 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
483 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
484 nr_free++;
487 if (nr_free) {
488 pgoff_t fofs;
490 * once we invalidate valid blkaddr in range [ofs, ofs + count],
491 * we will invalidate all blkaddr in the whole range.
493 fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
494 dn->inode) + ofs;
495 f2fs_update_extent_cache_range(dn, fofs, 0, len);
496 dec_valid_block_count(sbi, dn->inode, nr_free);
498 dn->ofs_in_node = ofs;
500 f2fs_update_time(sbi, REQ_TIME);
501 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
502 dn->ofs_in_node, nr_free);
503 return nr_free;
506 void truncate_data_blocks(struct dnode_of_data *dn)
508 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
511 static int truncate_partial_data_page(struct inode *inode, u64 from,
512 bool cache_only)
514 unsigned offset = from & (PAGE_SIZE - 1);
515 pgoff_t index = from >> PAGE_SHIFT;
516 struct address_space *mapping = inode->i_mapping;
517 struct page *page;
519 if (!offset && !cache_only)
520 return 0;
522 if (cache_only) {
523 page = find_lock_page(mapping, index);
524 if (page && PageUptodate(page))
525 goto truncate_out;
526 f2fs_put_page(page, 1);
527 return 0;
530 page = get_lock_data_page(inode, index, true);
531 if (IS_ERR(page))
532 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
533 truncate_out:
534 f2fs_wait_on_page_writeback(page, DATA, true);
535 zero_user(page, offset, PAGE_SIZE - offset);
537 /* An encrypted inode should have a key and truncate the last page. */
538 f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode));
539 if (!cache_only)
540 set_page_dirty(page);
541 f2fs_put_page(page, 1);
542 return 0;
545 int truncate_blocks(struct inode *inode, u64 from, bool lock)
547 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
548 unsigned int blocksize = inode->i_sb->s_blocksize;
549 struct dnode_of_data dn;
550 pgoff_t free_from;
551 int count = 0, err = 0;
552 struct page *ipage;
553 bool truncate_page = false;
555 trace_f2fs_truncate_blocks_enter(inode, from);
557 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
559 if (free_from >= sbi->max_file_blocks)
560 goto free_partial;
562 if (lock)
563 f2fs_lock_op(sbi);
565 ipage = get_node_page(sbi, inode->i_ino);
566 if (IS_ERR(ipage)) {
567 err = PTR_ERR(ipage);
568 goto out;
571 if (f2fs_has_inline_data(inode)) {
572 truncate_inline_inode(inode, ipage, from);
573 f2fs_put_page(ipage, 1);
574 truncate_page = true;
575 goto out;
578 set_new_dnode(&dn, inode, ipage, NULL, 0);
579 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
580 if (err) {
581 if (err == -ENOENT)
582 goto free_next;
583 goto out;
586 count = ADDRS_PER_PAGE(dn.node_page, inode);
588 count -= dn.ofs_in_node;
589 f2fs_bug_on(sbi, count < 0);
591 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
592 truncate_data_blocks_range(&dn, count);
593 free_from += count;
596 f2fs_put_dnode(&dn);
597 free_next:
598 err = truncate_inode_blocks(inode, free_from);
599 out:
600 if (lock)
601 f2fs_unlock_op(sbi);
602 free_partial:
603 /* lastly zero out the first data page */
604 if (!err)
605 err = truncate_partial_data_page(inode, from, truncate_page);
607 trace_f2fs_truncate_blocks_exit(inode, err);
608 return err;
611 int f2fs_truncate(struct inode *inode)
613 int err;
615 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
616 S_ISLNK(inode->i_mode)))
617 return 0;
619 trace_f2fs_truncate(inode);
621 #ifdef CONFIG_F2FS_FAULT_INJECTION
622 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
623 f2fs_show_injection_info(FAULT_TRUNCATE);
624 return -EIO;
626 #endif
627 /* we should check inline_data size */
628 if (!f2fs_may_inline_data(inode)) {
629 err = f2fs_convert_inline_inode(inode);
630 if (err)
631 return err;
634 err = truncate_blocks(inode, i_size_read(inode), true);
635 if (err)
636 return err;
638 inode->i_mtime = inode->i_ctime = current_time(inode);
639 f2fs_mark_inode_dirty_sync(inode, false);
640 return 0;
643 int f2fs_getattr(const struct path *path, struct kstat *stat,
644 u32 request_mask, unsigned int query_flags)
646 struct inode *inode = d_inode(path->dentry);
647 struct f2fs_inode_info *fi = F2FS_I(inode);
648 unsigned int flags;
650 flags = fi->i_flags & FS_FL_USER_VISIBLE;
651 if (flags & FS_APPEND_FL)
652 stat->attributes |= STATX_ATTR_APPEND;
653 if (flags & FS_COMPR_FL)
654 stat->attributes |= STATX_ATTR_COMPRESSED;
655 if (f2fs_encrypted_inode(inode))
656 stat->attributes |= STATX_ATTR_ENCRYPTED;
657 if (flags & FS_IMMUTABLE_FL)
658 stat->attributes |= STATX_ATTR_IMMUTABLE;
659 if (flags & FS_NODUMP_FL)
660 stat->attributes |= STATX_ATTR_NODUMP;
662 stat->attributes_mask |= (STATX_ATTR_APPEND |
663 STATX_ATTR_COMPRESSED |
664 STATX_ATTR_ENCRYPTED |
665 STATX_ATTR_IMMUTABLE |
666 STATX_ATTR_NODUMP);
668 generic_fillattr(inode, stat);
669 return 0;
672 #ifdef CONFIG_F2FS_FS_POSIX_ACL
673 static void __setattr_copy(struct inode *inode, const struct iattr *attr)
675 unsigned int ia_valid = attr->ia_valid;
677 if (ia_valid & ATTR_UID)
678 inode->i_uid = attr->ia_uid;
679 if (ia_valid & ATTR_GID)
680 inode->i_gid = attr->ia_gid;
681 if (ia_valid & ATTR_ATIME)
682 inode->i_atime = timespec_trunc(attr->ia_atime,
683 inode->i_sb->s_time_gran);
684 if (ia_valid & ATTR_MTIME)
685 inode->i_mtime = timespec_trunc(attr->ia_mtime,
686 inode->i_sb->s_time_gran);
687 if (ia_valid & ATTR_CTIME)
688 inode->i_ctime = timespec_trunc(attr->ia_ctime,
689 inode->i_sb->s_time_gran);
690 if (ia_valid & ATTR_MODE) {
691 umode_t mode = attr->ia_mode;
693 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
694 mode &= ~S_ISGID;
695 set_acl_inode(inode, mode);
698 #else
699 #define __setattr_copy setattr_copy
700 #endif
702 int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
704 struct inode *inode = d_inode(dentry);
705 int err;
706 bool size_changed = false;
708 err = setattr_prepare(dentry, attr);
709 if (err)
710 return err;
712 if (is_quota_modification(inode, attr)) {
713 err = dquot_initialize(inode);
714 if (err)
715 return err;
717 if ((attr->ia_valid & ATTR_UID &&
718 !uid_eq(attr->ia_uid, inode->i_uid)) ||
719 (attr->ia_valid & ATTR_GID &&
720 !gid_eq(attr->ia_gid, inode->i_gid))) {
721 err = dquot_transfer(inode, attr);
722 if (err)
723 return err;
726 if (attr->ia_valid & ATTR_SIZE) {
727 if (f2fs_encrypted_inode(inode)) {
728 err = fscrypt_get_encryption_info(inode);
729 if (err)
730 return err;
731 if (!fscrypt_has_encryption_key(inode))
732 return -ENOKEY;
735 if (attr->ia_size <= i_size_read(inode)) {
736 down_write(&F2FS_I(inode)->i_mmap_sem);
737 truncate_setsize(inode, attr->ia_size);
738 err = f2fs_truncate(inode);
739 up_write(&F2FS_I(inode)->i_mmap_sem);
740 if (err)
741 return err;
742 } else {
744 * do not trim all blocks after i_size if target size is
745 * larger than i_size.
747 down_write(&F2FS_I(inode)->i_mmap_sem);
748 truncate_setsize(inode, attr->ia_size);
749 up_write(&F2FS_I(inode)->i_mmap_sem);
751 /* should convert inline inode here */
752 if (!f2fs_may_inline_data(inode)) {
753 err = f2fs_convert_inline_inode(inode);
754 if (err)
755 return err;
757 inode->i_mtime = inode->i_ctime = current_time(inode);
760 size_changed = true;
763 __setattr_copy(inode, attr);
765 if (attr->ia_valid & ATTR_MODE) {
766 err = posix_acl_chmod(inode, get_inode_mode(inode));
767 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
768 inode->i_mode = F2FS_I(inode)->i_acl_mode;
769 clear_inode_flag(inode, FI_ACL_MODE);
773 /* file size may changed here */
774 f2fs_mark_inode_dirty_sync(inode, size_changed);
776 /* inode change will produce dirty node pages flushed by checkpoint */
777 f2fs_balance_fs(F2FS_I_SB(inode), true);
779 return err;
782 const struct inode_operations f2fs_file_inode_operations = {
783 .getattr = f2fs_getattr,
784 .setattr = f2fs_setattr,
785 .get_acl = f2fs_get_acl,
786 .set_acl = f2fs_set_acl,
787 #ifdef CONFIG_F2FS_FS_XATTR
788 .listxattr = f2fs_listxattr,
789 #endif
790 .fiemap = f2fs_fiemap,
793 static int fill_zero(struct inode *inode, pgoff_t index,
794 loff_t start, loff_t len)
796 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
797 struct page *page;
799 if (!len)
800 return 0;
802 f2fs_balance_fs(sbi, true);
804 f2fs_lock_op(sbi);
805 page = get_new_data_page(inode, NULL, index, false);
806 f2fs_unlock_op(sbi);
808 if (IS_ERR(page))
809 return PTR_ERR(page);
811 f2fs_wait_on_page_writeback(page, DATA, true);
812 zero_user(page, start, len);
813 set_page_dirty(page);
814 f2fs_put_page(page, 1);
815 return 0;
818 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
820 int err;
822 while (pg_start < pg_end) {
823 struct dnode_of_data dn;
824 pgoff_t end_offset, count;
826 set_new_dnode(&dn, inode, NULL, NULL, 0);
827 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
828 if (err) {
829 if (err == -ENOENT) {
830 pg_start++;
831 continue;
833 return err;
836 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
837 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
839 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
841 truncate_data_blocks_range(&dn, count);
842 f2fs_put_dnode(&dn);
844 pg_start += count;
846 return 0;
849 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
851 pgoff_t pg_start, pg_end;
852 loff_t off_start, off_end;
853 int ret;
855 ret = f2fs_convert_inline_inode(inode);
856 if (ret)
857 return ret;
859 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
860 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
862 off_start = offset & (PAGE_SIZE - 1);
863 off_end = (offset + len) & (PAGE_SIZE - 1);
865 if (pg_start == pg_end) {
866 ret = fill_zero(inode, pg_start, off_start,
867 off_end - off_start);
868 if (ret)
869 return ret;
870 } else {
871 if (off_start) {
872 ret = fill_zero(inode, pg_start++, off_start,
873 PAGE_SIZE - off_start);
874 if (ret)
875 return ret;
877 if (off_end) {
878 ret = fill_zero(inode, pg_end, 0, off_end);
879 if (ret)
880 return ret;
883 if (pg_start < pg_end) {
884 struct address_space *mapping = inode->i_mapping;
885 loff_t blk_start, blk_end;
886 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
888 f2fs_balance_fs(sbi, true);
890 blk_start = (loff_t)pg_start << PAGE_SHIFT;
891 blk_end = (loff_t)pg_end << PAGE_SHIFT;
892 down_write(&F2FS_I(inode)->i_mmap_sem);
893 truncate_inode_pages_range(mapping, blk_start,
894 blk_end - 1);
896 f2fs_lock_op(sbi);
897 ret = truncate_hole(inode, pg_start, pg_end);
898 f2fs_unlock_op(sbi);
899 up_write(&F2FS_I(inode)->i_mmap_sem);
903 return ret;
906 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
907 int *do_replace, pgoff_t off, pgoff_t len)
909 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
910 struct dnode_of_data dn;
911 int ret, done, i;
913 next_dnode:
914 set_new_dnode(&dn, inode, NULL, NULL, 0);
915 ret = get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
916 if (ret && ret != -ENOENT) {
917 return ret;
918 } else if (ret == -ENOENT) {
919 if (dn.max_level == 0)
920 return -ENOENT;
921 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
922 blkaddr += done;
923 do_replace += done;
924 goto next;
927 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
928 dn.ofs_in_node, len);
929 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
930 *blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
931 if (!is_checkpointed_data(sbi, *blkaddr)) {
933 if (test_opt(sbi, LFS)) {
934 f2fs_put_dnode(&dn);
935 return -ENOTSUPP;
938 /* do not invalidate this block address */
939 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
940 *do_replace = 1;
943 f2fs_put_dnode(&dn);
944 next:
945 len -= done;
946 off += done;
947 if (len)
948 goto next_dnode;
949 return 0;
952 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
953 int *do_replace, pgoff_t off, int len)
955 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
956 struct dnode_of_data dn;
957 int ret, i;
959 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
960 if (*do_replace == 0)
961 continue;
963 set_new_dnode(&dn, inode, NULL, NULL, 0);
964 ret = get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
965 if (ret) {
966 dec_valid_block_count(sbi, inode, 1);
967 invalidate_blocks(sbi, *blkaddr);
968 } else {
969 f2fs_update_data_blkaddr(&dn, *blkaddr);
971 f2fs_put_dnode(&dn);
973 return 0;
976 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
977 block_t *blkaddr, int *do_replace,
978 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
980 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
981 pgoff_t i = 0;
982 int ret;
984 while (i < len) {
985 if (blkaddr[i] == NULL_ADDR && !full) {
986 i++;
987 continue;
990 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
991 struct dnode_of_data dn;
992 struct node_info ni;
993 size_t new_size;
994 pgoff_t ilen;
996 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
997 ret = get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
998 if (ret)
999 return ret;
1001 get_node_info(sbi, dn.nid, &ni);
1002 ilen = min((pgoff_t)
1003 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1004 dn.ofs_in_node, len - i);
1005 do {
1006 dn.data_blkaddr = datablock_addr(dn.node_page,
1007 dn.ofs_in_node);
1008 truncate_data_blocks_range(&dn, 1);
1010 if (do_replace[i]) {
1011 f2fs_i_blocks_write(src_inode,
1012 1, false, false);
1013 f2fs_i_blocks_write(dst_inode,
1014 1, true, false);
1015 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1016 blkaddr[i], ni.version, true, false);
1018 do_replace[i] = 0;
1020 dn.ofs_in_node++;
1021 i++;
1022 new_size = (dst + i) << PAGE_SHIFT;
1023 if (dst_inode->i_size < new_size)
1024 f2fs_i_size_write(dst_inode, new_size);
1025 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1027 f2fs_put_dnode(&dn);
1028 } else {
1029 struct page *psrc, *pdst;
1031 psrc = get_lock_data_page(src_inode, src + i, true);
1032 if (IS_ERR(psrc))
1033 return PTR_ERR(psrc);
1034 pdst = get_new_data_page(dst_inode, NULL, dst + i,
1035 true);
1036 if (IS_ERR(pdst)) {
1037 f2fs_put_page(psrc, 1);
1038 return PTR_ERR(pdst);
1040 f2fs_copy_page(psrc, pdst);
1041 set_page_dirty(pdst);
1042 f2fs_put_page(pdst, 1);
1043 f2fs_put_page(psrc, 1);
1045 ret = truncate_hole(src_inode, src + i, src + i + 1);
1046 if (ret)
1047 return ret;
1048 i++;
1051 return 0;
1054 static int __exchange_data_block(struct inode *src_inode,
1055 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1056 pgoff_t len, bool full)
1058 block_t *src_blkaddr;
1059 int *do_replace;
1060 pgoff_t olen;
1061 int ret;
1063 while (len) {
1064 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
1066 src_blkaddr = kvzalloc(sizeof(block_t) * olen, GFP_KERNEL);
1067 if (!src_blkaddr)
1068 return -ENOMEM;
1070 do_replace = kvzalloc(sizeof(int) * olen, GFP_KERNEL);
1071 if (!do_replace) {
1072 kvfree(src_blkaddr);
1073 return -ENOMEM;
1076 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1077 do_replace, src, olen);
1078 if (ret)
1079 goto roll_back;
1081 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1082 do_replace, src, dst, olen, full);
1083 if (ret)
1084 goto roll_back;
1086 src += olen;
1087 dst += olen;
1088 len -= olen;
1090 kvfree(src_blkaddr);
1091 kvfree(do_replace);
1093 return 0;
1095 roll_back:
1096 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, len);
1097 kvfree(src_blkaddr);
1098 kvfree(do_replace);
1099 return ret;
1102 static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
1104 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1105 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1106 int ret;
1108 f2fs_balance_fs(sbi, true);
1109 f2fs_lock_op(sbi);
1111 f2fs_drop_extent_tree(inode);
1113 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1114 f2fs_unlock_op(sbi);
1115 return ret;
1118 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1120 pgoff_t pg_start, pg_end;
1121 loff_t new_size;
1122 int ret;
1124 if (offset + len >= i_size_read(inode))
1125 return -EINVAL;
1127 /* collapse range should be aligned to block size of f2fs. */
1128 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1129 return -EINVAL;
1131 ret = f2fs_convert_inline_inode(inode);
1132 if (ret)
1133 return ret;
1135 pg_start = offset >> PAGE_SHIFT;
1136 pg_end = (offset + len) >> PAGE_SHIFT;
1138 down_write(&F2FS_I(inode)->i_mmap_sem);
1139 /* write out all dirty pages from offset */
1140 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1141 if (ret)
1142 goto out;
1144 truncate_pagecache(inode, offset);
1146 ret = f2fs_do_collapse(inode, pg_start, pg_end);
1147 if (ret)
1148 goto out;
1150 /* write out all moved pages, if possible */
1151 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1152 truncate_pagecache(inode, offset);
1154 new_size = i_size_read(inode) - len;
1155 truncate_pagecache(inode, new_size);
1157 ret = truncate_blocks(inode, new_size, true);
1158 if (!ret)
1159 f2fs_i_size_write(inode, new_size);
1161 out:
1162 up_write(&F2FS_I(inode)->i_mmap_sem);
1163 return ret;
1166 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1167 pgoff_t end)
1169 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1170 pgoff_t index = start;
1171 unsigned int ofs_in_node = dn->ofs_in_node;
1172 blkcnt_t count = 0;
1173 int ret;
1175 for (; index < end; index++, dn->ofs_in_node++) {
1176 if (datablock_addr(dn->node_page, dn->ofs_in_node) == NULL_ADDR)
1177 count++;
1180 dn->ofs_in_node = ofs_in_node;
1181 ret = reserve_new_blocks(dn, count);
1182 if (ret)
1183 return ret;
1185 dn->ofs_in_node = ofs_in_node;
1186 for (index = start; index < end; index++, dn->ofs_in_node++) {
1187 dn->data_blkaddr =
1188 datablock_addr(dn->node_page, dn->ofs_in_node);
1190 * reserve_new_blocks will not guarantee entire block
1191 * allocation.
1193 if (dn->data_blkaddr == NULL_ADDR) {
1194 ret = -ENOSPC;
1195 break;
1197 if (dn->data_blkaddr != NEW_ADDR) {
1198 invalidate_blocks(sbi, dn->data_blkaddr);
1199 dn->data_blkaddr = NEW_ADDR;
1200 set_data_blkaddr(dn);
1204 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1206 return ret;
1209 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1210 int mode)
1212 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1213 struct address_space *mapping = inode->i_mapping;
1214 pgoff_t index, pg_start, pg_end;
1215 loff_t new_size = i_size_read(inode);
1216 loff_t off_start, off_end;
1217 int ret = 0;
1219 ret = inode_newsize_ok(inode, (len + offset));
1220 if (ret)
1221 return ret;
1223 ret = f2fs_convert_inline_inode(inode);
1224 if (ret)
1225 return ret;
1227 down_write(&F2FS_I(inode)->i_mmap_sem);
1228 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1229 if (ret)
1230 goto out_sem;
1232 truncate_pagecache_range(inode, offset, offset + len - 1);
1234 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1235 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1237 off_start = offset & (PAGE_SIZE - 1);
1238 off_end = (offset + len) & (PAGE_SIZE - 1);
1240 if (pg_start == pg_end) {
1241 ret = fill_zero(inode, pg_start, off_start,
1242 off_end - off_start);
1243 if (ret)
1244 goto out_sem;
1246 new_size = max_t(loff_t, new_size, offset + len);
1247 } else {
1248 if (off_start) {
1249 ret = fill_zero(inode, pg_start++, off_start,
1250 PAGE_SIZE - off_start);
1251 if (ret)
1252 goto out_sem;
1254 new_size = max_t(loff_t, new_size,
1255 (loff_t)pg_start << PAGE_SHIFT);
1258 for (index = pg_start; index < pg_end;) {
1259 struct dnode_of_data dn;
1260 unsigned int end_offset;
1261 pgoff_t end;
1263 f2fs_lock_op(sbi);
1265 set_new_dnode(&dn, inode, NULL, NULL, 0);
1266 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
1267 if (ret) {
1268 f2fs_unlock_op(sbi);
1269 goto out;
1272 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1273 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1275 ret = f2fs_do_zero_range(&dn, index, end);
1276 f2fs_put_dnode(&dn);
1277 f2fs_unlock_op(sbi);
1279 f2fs_balance_fs(sbi, dn.node_changed);
1281 if (ret)
1282 goto out;
1284 index = end;
1285 new_size = max_t(loff_t, new_size,
1286 (loff_t)index << PAGE_SHIFT);
1289 if (off_end) {
1290 ret = fill_zero(inode, pg_end, 0, off_end);
1291 if (ret)
1292 goto out;
1294 new_size = max_t(loff_t, new_size, offset + len);
1298 out:
1299 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
1300 f2fs_i_size_write(inode, new_size);
1301 out_sem:
1302 up_write(&F2FS_I(inode)->i_mmap_sem);
1304 return ret;
1307 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1309 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1310 pgoff_t nr, pg_start, pg_end, delta, idx;
1311 loff_t new_size;
1312 int ret = 0;
1314 new_size = i_size_read(inode) + len;
1315 ret = inode_newsize_ok(inode, new_size);
1316 if (ret)
1317 return ret;
1319 if (offset >= i_size_read(inode))
1320 return -EINVAL;
1322 /* insert range should be aligned to block size of f2fs. */
1323 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1324 return -EINVAL;
1326 ret = f2fs_convert_inline_inode(inode);
1327 if (ret)
1328 return ret;
1330 f2fs_balance_fs(sbi, true);
1332 down_write(&F2FS_I(inode)->i_mmap_sem);
1333 ret = truncate_blocks(inode, i_size_read(inode), true);
1334 if (ret)
1335 goto out;
1337 /* write out all dirty pages from offset */
1338 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1339 if (ret)
1340 goto out;
1342 truncate_pagecache(inode, offset);
1344 pg_start = offset >> PAGE_SHIFT;
1345 pg_end = (offset + len) >> PAGE_SHIFT;
1346 delta = pg_end - pg_start;
1347 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1349 while (!ret && idx > pg_start) {
1350 nr = idx - pg_start;
1351 if (nr > delta)
1352 nr = delta;
1353 idx -= nr;
1355 f2fs_lock_op(sbi);
1356 f2fs_drop_extent_tree(inode);
1358 ret = __exchange_data_block(inode, inode, idx,
1359 idx + delta, nr, false);
1360 f2fs_unlock_op(sbi);
1363 /* write out all moved pages, if possible */
1364 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1365 truncate_pagecache(inode, offset);
1367 if (!ret)
1368 f2fs_i_size_write(inode, new_size);
1369 out:
1370 up_write(&F2FS_I(inode)->i_mmap_sem);
1371 return ret;
1374 static int expand_inode_data(struct inode *inode, loff_t offset,
1375 loff_t len, int mode)
1377 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1378 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
1379 pgoff_t pg_end;
1380 loff_t new_size = i_size_read(inode);
1381 loff_t off_end;
1382 int err;
1384 err = inode_newsize_ok(inode, (len + offset));
1385 if (err)
1386 return err;
1388 err = f2fs_convert_inline_inode(inode);
1389 if (err)
1390 return err;
1392 f2fs_balance_fs(sbi, true);
1394 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1395 off_end = (offset + len) & (PAGE_SIZE - 1);
1397 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1398 map.m_len = pg_end - map.m_lblk;
1399 if (off_end)
1400 map.m_len++;
1402 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1403 if (err) {
1404 pgoff_t last_off;
1406 if (!map.m_len)
1407 return err;
1409 last_off = map.m_lblk + map.m_len - 1;
1411 /* update new size to the failed position */
1412 new_size = (last_off == pg_end) ? offset + len:
1413 (loff_t)(last_off + 1) << PAGE_SHIFT;
1414 } else {
1415 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1418 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
1419 f2fs_i_size_write(inode, new_size);
1421 return err;
1424 static long f2fs_fallocate(struct file *file, int mode,
1425 loff_t offset, loff_t len)
1427 struct inode *inode = file_inode(file);
1428 long ret = 0;
1430 /* f2fs only support ->fallocate for regular file */
1431 if (!S_ISREG(inode->i_mode))
1432 return -EINVAL;
1434 if (f2fs_encrypted_inode(inode) &&
1435 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1436 return -EOPNOTSUPP;
1438 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1439 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1440 FALLOC_FL_INSERT_RANGE))
1441 return -EOPNOTSUPP;
1443 inode_lock(inode);
1445 if (mode & FALLOC_FL_PUNCH_HOLE) {
1446 if (offset >= inode->i_size)
1447 goto out;
1449 ret = punch_hole(inode, offset, len);
1450 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1451 ret = f2fs_collapse_range(inode, offset, len);
1452 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1453 ret = f2fs_zero_range(inode, offset, len, mode);
1454 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1455 ret = f2fs_insert_range(inode, offset, len);
1456 } else {
1457 ret = expand_inode_data(inode, offset, len, mode);
1460 if (!ret) {
1461 inode->i_mtime = inode->i_ctime = current_time(inode);
1462 f2fs_mark_inode_dirty_sync(inode, false);
1463 if (mode & FALLOC_FL_KEEP_SIZE)
1464 file_set_keep_isize(inode);
1465 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1468 out:
1469 inode_unlock(inode);
1471 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1472 return ret;
1475 static int f2fs_release_file(struct inode *inode, struct file *filp)
1478 * f2fs_relase_file is called at every close calls. So we should
1479 * not drop any inmemory pages by close called by other process.
1481 if (!(filp->f_mode & FMODE_WRITE) ||
1482 atomic_read(&inode->i_writecount) != 1)
1483 return 0;
1485 /* some remained atomic pages should discarded */
1486 if (f2fs_is_atomic_file(inode))
1487 drop_inmem_pages(inode);
1488 if (f2fs_is_volatile_file(inode)) {
1489 clear_inode_flag(inode, FI_VOLATILE_FILE);
1490 stat_dec_volatile_write(inode);
1491 set_inode_flag(inode, FI_DROP_CACHE);
1492 filemap_fdatawrite(inode->i_mapping);
1493 clear_inode_flag(inode, FI_DROP_CACHE);
1495 return 0;
1498 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1499 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1501 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
1503 if (S_ISDIR(mode))
1504 return flags;
1505 else if (S_ISREG(mode))
1506 return flags & F2FS_REG_FLMASK;
1507 else
1508 return flags & F2FS_OTHER_FLMASK;
1511 static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1513 struct inode *inode = file_inode(filp);
1514 struct f2fs_inode_info *fi = F2FS_I(inode);
1515 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1516 return put_user(flags, (int __user *)arg);
1519 static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1521 struct inode *inode = file_inode(filp);
1522 struct f2fs_inode_info *fi = F2FS_I(inode);
1523 unsigned int flags;
1524 unsigned int oldflags;
1525 int ret;
1527 if (!inode_owner_or_capable(inode))
1528 return -EACCES;
1530 if (get_user(flags, (int __user *)arg))
1531 return -EFAULT;
1533 ret = mnt_want_write_file(filp);
1534 if (ret)
1535 return ret;
1537 inode_lock(inode);
1539 /* Is it quota file? Do not allow user to mess with it */
1540 if (IS_NOQUOTA(inode)) {
1541 ret = -EPERM;
1542 goto unlock_out;
1545 flags = f2fs_mask_flags(inode->i_mode, flags);
1547 oldflags = fi->i_flags;
1549 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
1550 if (!capable(CAP_LINUX_IMMUTABLE)) {
1551 ret = -EPERM;
1552 goto unlock_out;
1556 flags = flags & FS_FL_USER_MODIFIABLE;
1557 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
1558 fi->i_flags = flags;
1560 inode->i_ctime = current_time(inode);
1561 f2fs_set_inode_flags(inode);
1562 f2fs_mark_inode_dirty_sync(inode, false);
1563 unlock_out:
1564 inode_unlock(inode);
1565 mnt_drop_write_file(filp);
1566 return ret;
1569 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1571 struct inode *inode = file_inode(filp);
1573 return put_user(inode->i_generation, (int __user *)arg);
1576 static int f2fs_ioc_start_atomic_write(struct file *filp)
1578 struct inode *inode = file_inode(filp);
1579 int ret;
1581 if (!inode_owner_or_capable(inode))
1582 return -EACCES;
1584 if (!S_ISREG(inode->i_mode))
1585 return -EINVAL;
1587 ret = mnt_want_write_file(filp);
1588 if (ret)
1589 return ret;
1591 inode_lock(inode);
1593 if (f2fs_is_atomic_file(inode))
1594 goto out;
1596 ret = f2fs_convert_inline_inode(inode);
1597 if (ret)
1598 goto out;
1600 set_inode_flag(inode, FI_ATOMIC_FILE);
1601 set_inode_flag(inode, FI_HOT_DATA);
1602 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1604 if (!get_dirty_pages(inode))
1605 goto inc_stat;
1607 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
1608 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
1609 inode->i_ino, get_dirty_pages(inode));
1610 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
1611 if (ret) {
1612 clear_inode_flag(inode, FI_ATOMIC_FILE);
1613 goto out;
1616 inc_stat:
1617 stat_inc_atomic_write(inode);
1618 stat_update_max_atomic_write(inode);
1619 out:
1620 inode_unlock(inode);
1621 mnt_drop_write_file(filp);
1622 return ret;
1625 static int f2fs_ioc_commit_atomic_write(struct file *filp)
1627 struct inode *inode = file_inode(filp);
1628 int ret;
1630 if (!inode_owner_or_capable(inode))
1631 return -EACCES;
1633 ret = mnt_want_write_file(filp);
1634 if (ret)
1635 return ret;
1637 inode_lock(inode);
1639 if (f2fs_is_volatile_file(inode))
1640 goto err_out;
1642 if (f2fs_is_atomic_file(inode)) {
1643 ret = commit_inmem_pages(inode);
1644 if (ret)
1645 goto err_out;
1647 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1648 if (!ret) {
1649 clear_inode_flag(inode, FI_ATOMIC_FILE);
1650 stat_dec_atomic_write(inode);
1652 } else {
1653 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1655 err_out:
1656 inode_unlock(inode);
1657 mnt_drop_write_file(filp);
1658 return ret;
1661 static int f2fs_ioc_start_volatile_write(struct file *filp)
1663 struct inode *inode = file_inode(filp);
1664 int ret;
1666 if (!inode_owner_or_capable(inode))
1667 return -EACCES;
1669 if (!S_ISREG(inode->i_mode))
1670 return -EINVAL;
1672 ret = mnt_want_write_file(filp);
1673 if (ret)
1674 return ret;
1676 inode_lock(inode);
1678 if (f2fs_is_volatile_file(inode))
1679 goto out;
1681 ret = f2fs_convert_inline_inode(inode);
1682 if (ret)
1683 goto out;
1685 stat_inc_volatile_write(inode);
1686 stat_update_max_volatile_write(inode);
1688 set_inode_flag(inode, FI_VOLATILE_FILE);
1689 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1690 out:
1691 inode_unlock(inode);
1692 mnt_drop_write_file(filp);
1693 return ret;
1696 static int f2fs_ioc_release_volatile_write(struct file *filp)
1698 struct inode *inode = file_inode(filp);
1699 int ret;
1701 if (!inode_owner_or_capable(inode))
1702 return -EACCES;
1704 ret = mnt_want_write_file(filp);
1705 if (ret)
1706 return ret;
1708 inode_lock(inode);
1710 if (!f2fs_is_volatile_file(inode))
1711 goto out;
1713 if (!f2fs_is_first_block_written(inode)) {
1714 ret = truncate_partial_data_page(inode, 0, true);
1715 goto out;
1718 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1719 out:
1720 inode_unlock(inode);
1721 mnt_drop_write_file(filp);
1722 return ret;
1725 static int f2fs_ioc_abort_volatile_write(struct file *filp)
1727 struct inode *inode = file_inode(filp);
1728 int ret;
1730 if (!inode_owner_or_capable(inode))
1731 return -EACCES;
1733 ret = mnt_want_write_file(filp);
1734 if (ret)
1735 return ret;
1737 inode_lock(inode);
1739 if (f2fs_is_atomic_file(inode))
1740 drop_inmem_pages(inode);
1741 if (f2fs_is_volatile_file(inode)) {
1742 clear_inode_flag(inode, FI_VOLATILE_FILE);
1743 stat_dec_volatile_write(inode);
1744 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1747 inode_unlock(inode);
1749 mnt_drop_write_file(filp);
1750 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1751 return ret;
1754 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1756 struct inode *inode = file_inode(filp);
1757 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1758 struct super_block *sb = sbi->sb;
1759 __u32 in;
1760 int ret;
1762 if (!capable(CAP_SYS_ADMIN))
1763 return -EPERM;
1765 if (get_user(in, (__u32 __user *)arg))
1766 return -EFAULT;
1768 ret = mnt_want_write_file(filp);
1769 if (ret)
1770 return ret;
1772 switch (in) {
1773 case F2FS_GOING_DOWN_FULLSYNC:
1774 sb = freeze_bdev(sb->s_bdev);
1775 if (sb && !IS_ERR(sb)) {
1776 f2fs_stop_checkpoint(sbi, false);
1777 thaw_bdev(sb->s_bdev, sb);
1779 break;
1780 case F2FS_GOING_DOWN_METASYNC:
1781 /* do checkpoint only */
1782 f2fs_sync_fs(sb, 1);
1783 f2fs_stop_checkpoint(sbi, false);
1784 break;
1785 case F2FS_GOING_DOWN_NOSYNC:
1786 f2fs_stop_checkpoint(sbi, false);
1787 break;
1788 case F2FS_GOING_DOWN_METAFLUSH:
1789 sync_meta_pages(sbi, META, LONG_MAX);
1790 f2fs_stop_checkpoint(sbi, false);
1791 break;
1792 default:
1793 ret = -EINVAL;
1794 goto out;
1796 f2fs_update_time(sbi, REQ_TIME);
1797 out:
1798 mnt_drop_write_file(filp);
1799 return ret;
1802 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1804 struct inode *inode = file_inode(filp);
1805 struct super_block *sb = inode->i_sb;
1806 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1807 struct fstrim_range range;
1808 int ret;
1810 if (!capable(CAP_SYS_ADMIN))
1811 return -EPERM;
1813 if (!blk_queue_discard(q))
1814 return -EOPNOTSUPP;
1816 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1817 sizeof(range)))
1818 return -EFAULT;
1820 ret = mnt_want_write_file(filp);
1821 if (ret)
1822 return ret;
1824 range.minlen = max((unsigned int)range.minlen,
1825 q->limits.discard_granularity);
1826 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
1827 mnt_drop_write_file(filp);
1828 if (ret < 0)
1829 return ret;
1831 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1832 sizeof(range)))
1833 return -EFAULT;
1834 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1835 return 0;
1838 static bool uuid_is_nonzero(__u8 u[16])
1840 int i;
1842 for (i = 0; i < 16; i++)
1843 if (u[i])
1844 return true;
1845 return false;
1848 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1850 struct inode *inode = file_inode(filp);
1852 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1854 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
1857 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1859 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1862 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1864 struct inode *inode = file_inode(filp);
1865 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1866 int err;
1868 if (!f2fs_sb_has_crypto(inode->i_sb))
1869 return -EOPNOTSUPP;
1871 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1872 goto got_it;
1874 err = mnt_want_write_file(filp);
1875 if (err)
1876 return err;
1878 /* update superblock with uuid */
1879 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1881 err = f2fs_commit_super(sbi, false);
1882 if (err) {
1883 /* undo new data */
1884 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
1885 mnt_drop_write_file(filp);
1886 return err;
1888 mnt_drop_write_file(filp);
1889 got_it:
1890 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1891 16))
1892 return -EFAULT;
1893 return 0;
1896 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
1898 struct inode *inode = file_inode(filp);
1899 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1900 __u32 sync;
1901 int ret;
1903 if (!capable(CAP_SYS_ADMIN))
1904 return -EPERM;
1906 if (get_user(sync, (__u32 __user *)arg))
1907 return -EFAULT;
1909 if (f2fs_readonly(sbi->sb))
1910 return -EROFS;
1912 ret = mnt_want_write_file(filp);
1913 if (ret)
1914 return ret;
1916 if (!sync) {
1917 if (!mutex_trylock(&sbi->gc_mutex)) {
1918 ret = -EBUSY;
1919 goto out;
1921 } else {
1922 mutex_lock(&sbi->gc_mutex);
1925 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
1926 out:
1927 mnt_drop_write_file(filp);
1928 return ret;
1931 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
1933 struct inode *inode = file_inode(filp);
1934 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1935 struct f2fs_gc_range range;
1936 u64 end;
1937 int ret;
1939 if (!capable(CAP_SYS_ADMIN))
1940 return -EPERM;
1942 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
1943 sizeof(range)))
1944 return -EFAULT;
1946 if (f2fs_readonly(sbi->sb))
1947 return -EROFS;
1949 ret = mnt_want_write_file(filp);
1950 if (ret)
1951 return ret;
1953 end = range.start + range.len;
1954 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi))
1955 return -EINVAL;
1956 do_more:
1957 if (!range.sync) {
1958 if (!mutex_trylock(&sbi->gc_mutex)) {
1959 ret = -EBUSY;
1960 goto out;
1962 } else {
1963 mutex_lock(&sbi->gc_mutex);
1966 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
1967 range.start += sbi->blocks_per_seg;
1968 if (range.start <= end)
1969 goto do_more;
1970 out:
1971 mnt_drop_write_file(filp);
1972 return ret;
1975 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
1977 struct inode *inode = file_inode(filp);
1978 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1979 int ret;
1981 if (!capable(CAP_SYS_ADMIN))
1982 return -EPERM;
1984 if (f2fs_readonly(sbi->sb))
1985 return -EROFS;
1987 ret = mnt_want_write_file(filp);
1988 if (ret)
1989 return ret;
1991 ret = f2fs_sync_fs(sbi->sb, 1);
1993 mnt_drop_write_file(filp);
1994 return ret;
1997 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
1998 struct file *filp,
1999 struct f2fs_defragment *range)
2001 struct inode *inode = file_inode(filp);
2002 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
2003 struct extent_info ei = {0,0,0};
2004 pgoff_t pg_start, pg_end;
2005 unsigned int blk_per_seg = sbi->blocks_per_seg;
2006 unsigned int total = 0, sec_num;
2007 block_t blk_end = 0;
2008 bool fragmented = false;
2009 int err;
2011 /* if in-place-update policy is enabled, don't waste time here */
2012 if (need_inplace_update_policy(inode, NULL))
2013 return -EINVAL;
2015 pg_start = range->start >> PAGE_SHIFT;
2016 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2018 f2fs_balance_fs(sbi, true);
2020 inode_lock(inode);
2022 /* writeback all dirty pages in the range */
2023 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2024 range->start + range->len - 1);
2025 if (err)
2026 goto out;
2029 * lookup mapping info in extent cache, skip defragmenting if physical
2030 * block addresses are continuous.
2032 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2033 if (ei.fofs + ei.len >= pg_end)
2034 goto out;
2037 map.m_lblk = pg_start;
2040 * lookup mapping info in dnode page cache, skip defragmenting if all
2041 * physical block addresses are continuous even if there are hole(s)
2042 * in logical blocks.
2044 while (map.m_lblk < pg_end) {
2045 map.m_len = pg_end - map.m_lblk;
2046 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
2047 if (err)
2048 goto out;
2050 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2051 map.m_lblk++;
2052 continue;
2055 if (blk_end && blk_end != map.m_pblk) {
2056 fragmented = true;
2057 break;
2059 blk_end = map.m_pblk + map.m_len;
2061 map.m_lblk += map.m_len;
2064 if (!fragmented)
2065 goto out;
2067 map.m_lblk = pg_start;
2068 map.m_len = pg_end - pg_start;
2070 sec_num = (map.m_len + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
2073 * make sure there are enough free section for LFS allocation, this can
2074 * avoid defragment running in SSR mode when free section are allocated
2075 * intensively
2077 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2078 err = -EAGAIN;
2079 goto out;
2082 while (map.m_lblk < pg_end) {
2083 pgoff_t idx;
2084 int cnt = 0;
2086 do_map:
2087 map.m_len = pg_end - map.m_lblk;
2088 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
2089 if (err)
2090 goto clear_out;
2092 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2093 map.m_lblk++;
2094 continue;
2097 set_inode_flag(inode, FI_DO_DEFRAG);
2099 idx = map.m_lblk;
2100 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2101 struct page *page;
2103 page = get_lock_data_page(inode, idx, true);
2104 if (IS_ERR(page)) {
2105 err = PTR_ERR(page);
2106 goto clear_out;
2109 set_page_dirty(page);
2110 f2fs_put_page(page, 1);
2112 idx++;
2113 cnt++;
2114 total++;
2117 map.m_lblk = idx;
2119 if (idx < pg_end && cnt < blk_per_seg)
2120 goto do_map;
2122 clear_inode_flag(inode, FI_DO_DEFRAG);
2124 err = filemap_fdatawrite(inode->i_mapping);
2125 if (err)
2126 goto out;
2128 clear_out:
2129 clear_inode_flag(inode, FI_DO_DEFRAG);
2130 out:
2131 inode_unlock(inode);
2132 if (!err)
2133 range->len = (u64)total << PAGE_SHIFT;
2134 return err;
2137 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2139 struct inode *inode = file_inode(filp);
2140 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2141 struct f2fs_defragment range;
2142 int err;
2144 if (!capable(CAP_SYS_ADMIN))
2145 return -EPERM;
2147 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2148 return -EINVAL;
2150 if (f2fs_readonly(sbi->sb))
2151 return -EROFS;
2153 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2154 sizeof(range)))
2155 return -EFAULT;
2157 /* verify alignment of offset & size */
2158 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2159 return -EINVAL;
2161 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2162 sbi->max_file_blocks))
2163 return -EINVAL;
2165 err = mnt_want_write_file(filp);
2166 if (err)
2167 return err;
2169 err = f2fs_defragment_range(sbi, filp, &range);
2170 mnt_drop_write_file(filp);
2172 f2fs_update_time(sbi, REQ_TIME);
2173 if (err < 0)
2174 return err;
2176 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2177 sizeof(range)))
2178 return -EFAULT;
2180 return 0;
2183 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2184 struct file *file_out, loff_t pos_out, size_t len)
2186 struct inode *src = file_inode(file_in);
2187 struct inode *dst = file_inode(file_out);
2188 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2189 size_t olen = len, dst_max_i_size = 0;
2190 size_t dst_osize;
2191 int ret;
2193 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2194 src->i_sb != dst->i_sb)
2195 return -EXDEV;
2197 if (unlikely(f2fs_readonly(src->i_sb)))
2198 return -EROFS;
2200 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2201 return -EINVAL;
2203 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2204 return -EOPNOTSUPP;
2206 if (src == dst) {
2207 if (pos_in == pos_out)
2208 return 0;
2209 if (pos_out > pos_in && pos_out < pos_in + len)
2210 return -EINVAL;
2213 inode_lock(src);
2214 if (src != dst) {
2215 if (!inode_trylock(dst)) {
2216 ret = -EBUSY;
2217 goto out;
2221 ret = -EINVAL;
2222 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2223 goto out_unlock;
2224 if (len == 0)
2225 olen = len = src->i_size - pos_in;
2226 if (pos_in + len == src->i_size)
2227 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2228 if (len == 0) {
2229 ret = 0;
2230 goto out_unlock;
2233 dst_osize = dst->i_size;
2234 if (pos_out + olen > dst->i_size)
2235 dst_max_i_size = pos_out + olen;
2237 /* verify the end result is block aligned */
2238 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2239 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2240 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2241 goto out_unlock;
2243 ret = f2fs_convert_inline_inode(src);
2244 if (ret)
2245 goto out_unlock;
2247 ret = f2fs_convert_inline_inode(dst);
2248 if (ret)
2249 goto out_unlock;
2251 /* write out all dirty pages from offset */
2252 ret = filemap_write_and_wait_range(src->i_mapping,
2253 pos_in, pos_in + len);
2254 if (ret)
2255 goto out_unlock;
2257 ret = filemap_write_and_wait_range(dst->i_mapping,
2258 pos_out, pos_out + len);
2259 if (ret)
2260 goto out_unlock;
2262 f2fs_balance_fs(sbi, true);
2263 f2fs_lock_op(sbi);
2264 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2265 pos_out >> F2FS_BLKSIZE_BITS,
2266 len >> F2FS_BLKSIZE_BITS, false);
2268 if (!ret) {
2269 if (dst_max_i_size)
2270 f2fs_i_size_write(dst, dst_max_i_size);
2271 else if (dst_osize != dst->i_size)
2272 f2fs_i_size_write(dst, dst_osize);
2274 f2fs_unlock_op(sbi);
2275 out_unlock:
2276 if (src != dst)
2277 inode_unlock(dst);
2278 out:
2279 inode_unlock(src);
2280 return ret;
2283 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2285 struct f2fs_move_range range;
2286 struct fd dst;
2287 int err;
2289 if (!(filp->f_mode & FMODE_READ) ||
2290 !(filp->f_mode & FMODE_WRITE))
2291 return -EBADF;
2293 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2294 sizeof(range)))
2295 return -EFAULT;
2297 dst = fdget(range.dst_fd);
2298 if (!dst.file)
2299 return -EBADF;
2301 if (!(dst.file->f_mode & FMODE_WRITE)) {
2302 err = -EBADF;
2303 goto err_out;
2306 err = mnt_want_write_file(filp);
2307 if (err)
2308 goto err_out;
2310 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2311 range.pos_out, range.len);
2313 mnt_drop_write_file(filp);
2314 if (err)
2315 goto err_out;
2317 if (copy_to_user((struct f2fs_move_range __user *)arg,
2318 &range, sizeof(range)))
2319 err = -EFAULT;
2320 err_out:
2321 fdput(dst);
2322 return err;
2325 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2327 struct inode *inode = file_inode(filp);
2328 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2329 struct sit_info *sm = SIT_I(sbi);
2330 unsigned int start_segno = 0, end_segno = 0;
2331 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2332 struct f2fs_flush_device range;
2333 int ret;
2335 if (!capable(CAP_SYS_ADMIN))
2336 return -EPERM;
2338 if (f2fs_readonly(sbi->sb))
2339 return -EROFS;
2341 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2342 sizeof(range)))
2343 return -EFAULT;
2345 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num ||
2346 sbi->segs_per_sec != 1) {
2347 f2fs_msg(sbi->sb, KERN_WARNING,
2348 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2349 range.dev_num, sbi->s_ndevs,
2350 sbi->segs_per_sec);
2351 return -EINVAL;
2354 ret = mnt_want_write_file(filp);
2355 if (ret)
2356 return ret;
2358 if (range.dev_num != 0)
2359 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2360 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2362 start_segno = sm->last_victim[FLUSH_DEVICE];
2363 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2364 start_segno = dev_start_segno;
2365 end_segno = min(start_segno + range.segments, dev_end_segno);
2367 while (start_segno < end_segno) {
2368 if (!mutex_trylock(&sbi->gc_mutex)) {
2369 ret = -EBUSY;
2370 goto out;
2372 sm->last_victim[GC_CB] = end_segno + 1;
2373 sm->last_victim[GC_GREEDY] = end_segno + 1;
2374 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2375 ret = f2fs_gc(sbi, true, true, start_segno);
2376 if (ret == -EAGAIN)
2377 ret = 0;
2378 else if (ret < 0)
2379 break;
2380 start_segno++;
2382 out:
2383 mnt_drop_write_file(filp);
2384 return ret;
2388 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2390 switch (cmd) {
2391 case F2FS_IOC_GETFLAGS:
2392 return f2fs_ioc_getflags(filp, arg);
2393 case F2FS_IOC_SETFLAGS:
2394 return f2fs_ioc_setflags(filp, arg);
2395 case F2FS_IOC_GETVERSION:
2396 return f2fs_ioc_getversion(filp, arg);
2397 case F2FS_IOC_START_ATOMIC_WRITE:
2398 return f2fs_ioc_start_atomic_write(filp);
2399 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2400 return f2fs_ioc_commit_atomic_write(filp);
2401 case F2FS_IOC_START_VOLATILE_WRITE:
2402 return f2fs_ioc_start_volatile_write(filp);
2403 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2404 return f2fs_ioc_release_volatile_write(filp);
2405 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2406 return f2fs_ioc_abort_volatile_write(filp);
2407 case F2FS_IOC_SHUTDOWN:
2408 return f2fs_ioc_shutdown(filp, arg);
2409 case FITRIM:
2410 return f2fs_ioc_fitrim(filp, arg);
2411 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2412 return f2fs_ioc_set_encryption_policy(filp, arg);
2413 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2414 return f2fs_ioc_get_encryption_policy(filp, arg);
2415 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2416 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
2417 case F2FS_IOC_GARBAGE_COLLECT:
2418 return f2fs_ioc_gc(filp, arg);
2419 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2420 return f2fs_ioc_gc_range(filp, arg);
2421 case F2FS_IOC_WRITE_CHECKPOINT:
2422 return f2fs_ioc_write_checkpoint(filp, arg);
2423 case F2FS_IOC_DEFRAGMENT:
2424 return f2fs_ioc_defragment(filp, arg);
2425 case F2FS_IOC_MOVE_RANGE:
2426 return f2fs_ioc_move_range(filp, arg);
2427 case F2FS_IOC_FLUSH_DEVICE:
2428 return f2fs_ioc_flush_device(filp, arg);
2429 default:
2430 return -ENOTTY;
2434 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2436 struct file *file = iocb->ki_filp;
2437 struct inode *inode = file_inode(file);
2438 struct blk_plug plug;
2439 ssize_t ret;
2441 inode_lock(inode);
2442 ret = generic_write_checks(iocb, from);
2443 if (ret > 0) {
2444 int err;
2446 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
2447 set_inode_flag(inode, FI_NO_PREALLOC);
2449 err = f2fs_preallocate_blocks(iocb, from);
2450 if (err) {
2451 inode_unlock(inode);
2452 return err;
2454 blk_start_plug(&plug);
2455 ret = __generic_file_write_iter(iocb, from);
2456 blk_finish_plug(&plug);
2457 clear_inode_flag(inode, FI_NO_PREALLOC);
2459 inode_unlock(inode);
2461 if (ret > 0)
2462 ret = generic_write_sync(iocb, ret);
2463 return ret;
2466 #ifdef CONFIG_COMPAT
2467 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2469 switch (cmd) {
2470 case F2FS_IOC32_GETFLAGS:
2471 cmd = F2FS_IOC_GETFLAGS;
2472 break;
2473 case F2FS_IOC32_SETFLAGS:
2474 cmd = F2FS_IOC_SETFLAGS;
2475 break;
2476 case F2FS_IOC32_GETVERSION:
2477 cmd = F2FS_IOC_GETVERSION;
2478 break;
2479 case F2FS_IOC_START_ATOMIC_WRITE:
2480 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2481 case F2FS_IOC_START_VOLATILE_WRITE:
2482 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2483 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2484 case F2FS_IOC_SHUTDOWN:
2485 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2486 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2487 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2488 case F2FS_IOC_GARBAGE_COLLECT:
2489 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2490 case F2FS_IOC_WRITE_CHECKPOINT:
2491 case F2FS_IOC_DEFRAGMENT:
2492 case F2FS_IOC_MOVE_RANGE:
2493 case F2FS_IOC_FLUSH_DEVICE:
2494 break;
2495 default:
2496 return -ENOIOCTLCMD;
2498 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
2500 #endif
2502 const struct file_operations f2fs_file_operations = {
2503 .llseek = f2fs_llseek,
2504 .read_iter = generic_file_read_iter,
2505 .write_iter = f2fs_file_write_iter,
2506 .open = f2fs_file_open,
2507 .release = f2fs_release_file,
2508 .mmap = f2fs_file_mmap,
2509 .fsync = f2fs_sync_file,
2510 .fallocate = f2fs_fallocate,
2511 .unlocked_ioctl = f2fs_ioctl,
2512 #ifdef CONFIG_COMPAT
2513 .compat_ioctl = f2fs_compat_ioctl,
2514 #endif
2515 .splice_read = generic_file_splice_read,
2516 .splice_write = iter_file_splice_write,