arm64: kgdb: Fix single-step exception handling oops
[linux/fpc-iii.git] / fs / f2fs / file.c
blobe3c438c8b8ce086f754d1079aa3f08ca06c03cb1
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/uuid.h>
24 #include <linux/file.h>
26 #include "f2fs.h"
27 #include "node.h"
28 #include "segment.h"
29 #include "xattr.h"
30 #include "acl.h"
31 #include "gc.h"
32 #include "trace.h"
33 #include <trace/events/f2fs.h>
35 static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
36 struct vm_fault *vmf)
38 struct page *page = vmf->page;
39 struct inode *inode = file_inode(vma->vm_file);
40 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
41 struct dnode_of_data dn;
42 int err;
44 sb_start_pagefault(inode->i_sb);
46 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
48 /* block allocation */
49 f2fs_lock_op(sbi);
50 set_new_dnode(&dn, inode, NULL, NULL, 0);
51 err = f2fs_reserve_block(&dn, page->index);
52 if (err) {
53 f2fs_unlock_op(sbi);
54 goto out;
56 f2fs_put_dnode(&dn);
57 f2fs_unlock_op(sbi);
59 f2fs_balance_fs(sbi, dn.node_changed);
61 file_update_time(vma->vm_file);
62 lock_page(page);
63 if (unlikely(page->mapping != inode->i_mapping ||
64 page_offset(page) > i_size_read(inode) ||
65 !PageUptodate(page))) {
66 unlock_page(page);
67 err = -EFAULT;
68 goto out;
72 * check to see if the page is mapped already (no holes)
74 if (PageMappedToDisk(page))
75 goto mapped;
77 /* page is wholly or partially inside EOF */
78 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
79 i_size_read(inode)) {
80 unsigned offset;
81 offset = i_size_read(inode) & ~PAGE_MASK;
82 zero_user_segment(page, offset, PAGE_SIZE);
84 set_page_dirty(page);
85 if (!PageUptodate(page))
86 SetPageUptodate(page);
88 trace_f2fs_vm_page_mkwrite(page, DATA);
89 mapped:
90 /* fill the page */
91 f2fs_wait_on_page_writeback(page, DATA, false);
93 /* wait for GCed encrypted page writeback */
94 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
95 f2fs_wait_on_encrypted_page_writeback(sbi, dn.data_blkaddr);
97 /* if gced page is attached, don't write to cold segment */
98 clear_cold_data(page);
99 out:
100 sb_end_pagefault(inode->i_sb);
101 f2fs_update_time(sbi, REQ_TIME);
102 return block_page_mkwrite_return(err);
105 static const struct vm_operations_struct f2fs_file_vm_ops = {
106 .fault = filemap_fault,
107 .map_pages = filemap_map_pages,
108 .page_mkwrite = f2fs_vm_page_mkwrite,
111 static int get_parent_ino(struct inode *inode, nid_t *pino)
113 struct dentry *dentry;
115 inode = igrab(inode);
116 dentry = d_find_any_alias(inode);
117 iput(inode);
118 if (!dentry)
119 return 0;
121 if (update_dent_inode(inode, inode, &dentry->d_name)) {
122 dput(dentry);
123 return 0;
126 *pino = parent_ino(dentry);
127 dput(dentry);
128 return 1;
131 static inline bool need_do_checkpoint(struct inode *inode)
133 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
134 bool need_cp = false;
136 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
137 need_cp = true;
138 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
139 need_cp = true;
140 else if (file_wrong_pino(inode))
141 need_cp = true;
142 else if (!space_for_roll_forward(sbi))
143 need_cp = true;
144 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
145 need_cp = true;
146 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
147 need_cp = true;
148 else if (test_opt(sbi, FASTBOOT))
149 need_cp = true;
150 else if (sbi->active_logs == 2)
151 need_cp = true;
153 return need_cp;
156 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
158 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
159 bool ret = false;
160 /* But we need to avoid that there are some inode updates */
161 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
162 ret = true;
163 f2fs_put_page(i, 0);
164 return ret;
167 static void try_to_fix_pino(struct inode *inode)
169 struct f2fs_inode_info *fi = F2FS_I(inode);
170 nid_t pino;
172 down_write(&fi->i_sem);
173 fi->xattr_ver = 0;
174 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
175 get_parent_ino(inode, &pino)) {
176 f2fs_i_pino_write(inode, pino);
177 file_got_pino(inode);
179 up_write(&fi->i_sem);
182 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
183 int datasync, bool atomic)
185 struct inode *inode = file->f_mapping->host;
186 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
187 nid_t ino = inode->i_ino;
188 int ret = 0;
189 bool need_cp = false;
190 struct writeback_control wbc = {
191 .sync_mode = WB_SYNC_ALL,
192 .nr_to_write = LONG_MAX,
193 .for_reclaim = 0,
196 if (unlikely(f2fs_readonly(inode->i_sb)))
197 return 0;
199 trace_f2fs_sync_file_enter(inode);
201 if (S_ISDIR(inode->i_mode))
202 goto go_write;
204 /* if fdatasync is triggered, let's do in-place-update */
205 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
206 set_inode_flag(inode, FI_NEED_IPU);
207 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
208 clear_inode_flag(inode, FI_NEED_IPU);
210 if (ret) {
211 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
212 return ret;
215 /* if the inode is dirty, let's recover all the time */
216 if (!datasync && !f2fs_skip_inode_update(inode)) {
217 f2fs_write_inode(inode, NULL);
218 goto go_write;
222 * if there is no written data, don't waste time to write recovery info.
224 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
225 !exist_written_data(sbi, ino, APPEND_INO)) {
227 /* it may call write_inode just prior to fsync */
228 if (need_inode_page_update(sbi, ino))
229 goto go_write;
231 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
232 exist_written_data(sbi, ino, UPDATE_INO))
233 goto flush_out;
234 goto out;
236 go_write:
238 * Both of fdatasync() and fsync() are able to be recovered from
239 * sudden-power-off.
241 down_read(&F2FS_I(inode)->i_sem);
242 need_cp = need_do_checkpoint(inode);
243 up_read(&F2FS_I(inode)->i_sem);
245 if (need_cp) {
246 /* all the dirty node pages should be flushed for POR */
247 ret = f2fs_sync_fs(inode->i_sb, 1);
250 * We've secured consistency through sync_fs. Following pino
251 * will be used only for fsynced inodes after checkpoint.
253 try_to_fix_pino(inode);
254 clear_inode_flag(inode, FI_APPEND_WRITE);
255 clear_inode_flag(inode, FI_UPDATE_WRITE);
256 goto out;
258 sync_nodes:
259 ret = fsync_node_pages(sbi, inode, &wbc, atomic);
260 if (ret)
261 goto out;
263 /* if cp_error was enabled, we should avoid infinite loop */
264 if (unlikely(f2fs_cp_error(sbi))) {
265 ret = -EIO;
266 goto out;
269 if (need_inode_block_update(sbi, ino)) {
270 f2fs_mark_inode_dirty_sync(inode);
271 f2fs_write_inode(inode, NULL);
272 goto sync_nodes;
275 ret = wait_on_node_pages_writeback(sbi, ino);
276 if (ret)
277 goto out;
279 /* once recovery info is written, don't need to tack this */
280 remove_ino_entry(sbi, ino, APPEND_INO);
281 clear_inode_flag(inode, FI_APPEND_WRITE);
282 flush_out:
283 remove_ino_entry(sbi, ino, UPDATE_INO);
284 clear_inode_flag(inode, FI_UPDATE_WRITE);
285 ret = f2fs_issue_flush(sbi);
286 f2fs_update_time(sbi, REQ_TIME);
287 out:
288 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
289 f2fs_trace_ios(NULL, 1);
290 return ret;
293 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
295 return f2fs_do_sync_file(file, start, end, datasync, false);
298 static pgoff_t __get_first_dirty_index(struct address_space *mapping,
299 pgoff_t pgofs, int whence)
301 struct pagevec pvec;
302 int nr_pages;
304 if (whence != SEEK_DATA)
305 return 0;
307 /* find first dirty page index */
308 pagevec_init(&pvec, 0);
309 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
310 PAGECACHE_TAG_DIRTY, 1);
311 pgofs = nr_pages ? pvec.pages[0]->index : ULONG_MAX;
312 pagevec_release(&pvec);
313 return pgofs;
316 static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
317 pgoff_t dirty, pgoff_t pgofs, int whence)
319 switch (whence) {
320 case SEEK_DATA:
321 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
322 is_valid_data_blkaddr(sbi, blkaddr))
323 return true;
324 break;
325 case SEEK_HOLE:
326 if (blkaddr == NULL_ADDR)
327 return true;
328 break;
330 return false;
333 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
335 struct inode *inode = file->f_mapping->host;
336 loff_t maxbytes = inode->i_sb->s_maxbytes;
337 struct dnode_of_data dn;
338 pgoff_t pgofs, end_offset, dirty;
339 loff_t data_ofs = offset;
340 loff_t isize;
341 int err = 0;
343 inode_lock(inode);
345 isize = i_size_read(inode);
346 if (offset >= isize)
347 goto fail;
349 /* handle inline data case */
350 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
351 if (whence == SEEK_HOLE)
352 data_ofs = isize;
353 goto found;
356 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
358 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
360 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
361 set_new_dnode(&dn, inode, NULL, NULL, 0);
362 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
363 if (err && err != -ENOENT) {
364 goto fail;
365 } else if (err == -ENOENT) {
366 /* direct node does not exists */
367 if (whence == SEEK_DATA) {
368 pgofs = get_next_page_offset(&dn, pgofs);
369 continue;
370 } else {
371 goto found;
375 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
377 /* find data/hole in dnode block */
378 for (; dn.ofs_in_node < end_offset;
379 dn.ofs_in_node++, pgofs++,
380 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
381 block_t blkaddr;
382 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
384 if (__is_valid_data_blkaddr(blkaddr) &&
385 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
386 blkaddr, DATA_GENERIC)) {
387 f2fs_put_dnode(&dn);
388 goto fail;
391 if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
392 pgofs, whence)) {
393 f2fs_put_dnode(&dn);
394 goto found;
397 f2fs_put_dnode(&dn);
400 if (whence == SEEK_DATA)
401 goto fail;
402 found:
403 if (whence == SEEK_HOLE && data_ofs > isize)
404 data_ofs = isize;
405 inode_unlock(inode);
406 return vfs_setpos(file, data_ofs, maxbytes);
407 fail:
408 inode_unlock(inode);
409 return -ENXIO;
412 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
414 struct inode *inode = file->f_mapping->host;
415 loff_t maxbytes = inode->i_sb->s_maxbytes;
417 switch (whence) {
418 case SEEK_SET:
419 case SEEK_CUR:
420 case SEEK_END:
421 return generic_file_llseek_size(file, offset, whence,
422 maxbytes, i_size_read(inode));
423 case SEEK_DATA:
424 case SEEK_HOLE:
425 if (offset < 0)
426 return -ENXIO;
427 return f2fs_seek_block(file, offset, whence);
430 return -EINVAL;
433 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
435 struct inode *inode = file_inode(file);
436 int err;
438 if (f2fs_encrypted_inode(inode)) {
439 err = fscrypt_get_encryption_info(inode);
440 if (err)
441 return 0;
442 if (!f2fs_encrypted_inode(inode))
443 return -ENOKEY;
446 /* we don't need to use inline_data strictly */
447 err = f2fs_convert_inline_inode(inode);
448 if (err)
449 return err;
451 file_accessed(file);
452 vma->vm_ops = &f2fs_file_vm_ops;
453 return 0;
456 static int f2fs_file_open(struct inode *inode, struct file *filp)
458 int ret = generic_file_open(inode, filp);
459 struct dentry *dir;
461 if (!ret && f2fs_encrypted_inode(inode)) {
462 ret = fscrypt_get_encryption_info(inode);
463 if (ret)
464 return -EACCES;
465 if (!fscrypt_has_encryption_key(inode))
466 return -ENOKEY;
468 dir = dget_parent(file_dentry(filp));
469 if (f2fs_encrypted_inode(d_inode(dir)) &&
470 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
471 dput(dir);
472 return -EPERM;
474 dput(dir);
475 return ret;
478 int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
480 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
481 struct f2fs_node *raw_node;
482 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
483 __le32 *addr;
485 raw_node = F2FS_NODE(dn->node_page);
486 addr = blkaddr_in_node(raw_node) + ofs;
488 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
489 block_t blkaddr = le32_to_cpu(*addr);
490 if (blkaddr == NULL_ADDR)
491 continue;
493 dn->data_blkaddr = NULL_ADDR;
494 set_data_blkaddr(dn);
496 if (__is_valid_data_blkaddr(blkaddr) &&
497 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
498 continue;
500 invalidate_blocks(sbi, blkaddr);
501 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
502 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
503 nr_free++;
506 if (nr_free) {
507 pgoff_t fofs;
509 * once we invalidate valid blkaddr in range [ofs, ofs + count],
510 * we will invalidate all blkaddr in the whole range.
512 fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
513 dn->inode) + ofs;
514 f2fs_update_extent_cache_range(dn, fofs, 0, len);
515 dec_valid_block_count(sbi, dn->inode, nr_free);
517 dn->ofs_in_node = ofs;
519 f2fs_update_time(sbi, REQ_TIME);
520 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
521 dn->ofs_in_node, nr_free);
522 return nr_free;
525 void truncate_data_blocks(struct dnode_of_data *dn)
527 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
530 static int truncate_partial_data_page(struct inode *inode, u64 from,
531 bool cache_only)
533 unsigned offset = from & (PAGE_SIZE - 1);
534 pgoff_t index = from >> PAGE_SHIFT;
535 struct address_space *mapping = inode->i_mapping;
536 struct page *page;
538 if (!offset && !cache_only)
539 return 0;
541 if (cache_only) {
542 page = find_lock_page(mapping, index);
543 if (page && PageUptodate(page))
544 goto truncate_out;
545 f2fs_put_page(page, 1);
546 return 0;
549 page = get_lock_data_page(inode, index, true);
550 if (IS_ERR(page))
551 return 0;
552 truncate_out:
553 f2fs_wait_on_page_writeback(page, DATA, true);
554 zero_user(page, offset, PAGE_SIZE - offset);
555 if (!cache_only || !f2fs_encrypted_inode(inode) ||
556 !S_ISREG(inode->i_mode))
557 set_page_dirty(page);
558 f2fs_put_page(page, 1);
559 return 0;
562 int truncate_blocks(struct inode *inode, u64 from, bool lock)
564 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
565 unsigned int blocksize = inode->i_sb->s_blocksize;
566 struct dnode_of_data dn;
567 pgoff_t free_from;
568 int count = 0, err = 0;
569 struct page *ipage;
570 bool truncate_page = false;
572 trace_f2fs_truncate_blocks_enter(inode, from);
574 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
576 if (free_from >= sbi->max_file_blocks)
577 goto free_partial;
579 if (lock)
580 f2fs_lock_op(sbi);
582 ipage = get_node_page(sbi, inode->i_ino);
583 if (IS_ERR(ipage)) {
584 err = PTR_ERR(ipage);
585 goto out;
588 if (f2fs_has_inline_data(inode)) {
589 if (truncate_inline_inode(ipage, from))
590 set_page_dirty(ipage);
591 f2fs_put_page(ipage, 1);
592 truncate_page = true;
593 goto out;
596 set_new_dnode(&dn, inode, ipage, NULL, 0);
597 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
598 if (err) {
599 if (err == -ENOENT)
600 goto free_next;
601 goto out;
604 count = ADDRS_PER_PAGE(dn.node_page, inode);
606 count -= dn.ofs_in_node;
607 f2fs_bug_on(sbi, count < 0);
609 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
610 truncate_data_blocks_range(&dn, count);
611 free_from += count;
614 f2fs_put_dnode(&dn);
615 free_next:
616 err = truncate_inode_blocks(inode, free_from);
617 out:
618 if (lock)
619 f2fs_unlock_op(sbi);
620 free_partial:
621 /* lastly zero out the first data page */
622 if (!err)
623 err = truncate_partial_data_page(inode, from, truncate_page);
625 trace_f2fs_truncate_blocks_exit(inode, err);
626 return err;
629 int f2fs_truncate(struct inode *inode)
631 int err;
633 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
634 S_ISLNK(inode->i_mode)))
635 return 0;
637 trace_f2fs_truncate(inode);
639 /* we should check inline_data size */
640 if (!f2fs_may_inline_data(inode)) {
641 err = f2fs_convert_inline_inode(inode);
642 if (err)
643 return err;
646 err = truncate_blocks(inode, i_size_read(inode), true);
647 if (err)
648 return err;
650 inode->i_mtime = inode->i_ctime = current_time(inode);
651 f2fs_mark_inode_dirty_sync(inode);
652 return 0;
655 int f2fs_getattr(struct vfsmount *mnt,
656 struct dentry *dentry, struct kstat *stat)
658 struct inode *inode = d_inode(dentry);
659 generic_fillattr(inode, stat);
660 stat->blocks <<= 3;
661 return 0;
664 #ifdef CONFIG_F2FS_FS_POSIX_ACL
665 static void __setattr_copy(struct inode *inode, const struct iattr *attr)
667 unsigned int ia_valid = attr->ia_valid;
669 if (ia_valid & ATTR_UID)
670 inode->i_uid = attr->ia_uid;
671 if (ia_valid & ATTR_GID)
672 inode->i_gid = attr->ia_gid;
673 if (ia_valid & ATTR_ATIME)
674 inode->i_atime = timespec_trunc(attr->ia_atime,
675 inode->i_sb->s_time_gran);
676 if (ia_valid & ATTR_MTIME)
677 inode->i_mtime = timespec_trunc(attr->ia_mtime,
678 inode->i_sb->s_time_gran);
679 if (ia_valid & ATTR_CTIME)
680 inode->i_ctime = timespec_trunc(attr->ia_ctime,
681 inode->i_sb->s_time_gran);
682 if (ia_valid & ATTR_MODE) {
683 umode_t mode = attr->ia_mode;
685 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
686 mode &= ~S_ISGID;
687 set_acl_inode(inode, mode);
690 #else
691 #define __setattr_copy setattr_copy
692 #endif
694 int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
696 struct inode *inode = d_inode(dentry);
697 int err;
699 err = setattr_prepare(dentry, attr);
700 if (err)
701 return err;
703 if (attr->ia_valid & ATTR_SIZE) {
704 if (f2fs_encrypted_inode(inode) &&
705 fscrypt_get_encryption_info(inode))
706 return -EACCES;
708 if (attr->ia_size <= i_size_read(inode)) {
709 truncate_setsize(inode, attr->ia_size);
710 err = f2fs_truncate(inode);
711 if (err)
712 return err;
713 f2fs_balance_fs(F2FS_I_SB(inode), true);
714 } else {
716 * do not trim all blocks after i_size if target size is
717 * larger than i_size.
719 truncate_setsize(inode, attr->ia_size);
721 /* should convert inline inode here */
722 if (!f2fs_may_inline_data(inode)) {
723 err = f2fs_convert_inline_inode(inode);
724 if (err)
725 return err;
727 inode->i_mtime = inode->i_ctime = current_time(inode);
731 __setattr_copy(inode, attr);
733 if (attr->ia_valid & ATTR_MODE) {
734 err = posix_acl_chmod(inode, get_inode_mode(inode));
735 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
736 inode->i_mode = F2FS_I(inode)->i_acl_mode;
737 clear_inode_flag(inode, FI_ACL_MODE);
741 f2fs_mark_inode_dirty_sync(inode);
742 return err;
745 const struct inode_operations f2fs_file_inode_operations = {
746 .getattr = f2fs_getattr,
747 .setattr = f2fs_setattr,
748 .get_acl = f2fs_get_acl,
749 .set_acl = f2fs_set_acl,
750 #ifdef CONFIG_F2FS_FS_XATTR
751 .listxattr = f2fs_listxattr,
752 #endif
753 .fiemap = f2fs_fiemap,
756 static int fill_zero(struct inode *inode, pgoff_t index,
757 loff_t start, loff_t len)
759 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
760 struct page *page;
762 if (!len)
763 return 0;
765 f2fs_balance_fs(sbi, true);
767 f2fs_lock_op(sbi);
768 page = get_new_data_page(inode, NULL, index, false);
769 f2fs_unlock_op(sbi);
771 if (IS_ERR(page))
772 return PTR_ERR(page);
774 f2fs_wait_on_page_writeback(page, DATA, true);
775 zero_user(page, start, len);
776 set_page_dirty(page);
777 f2fs_put_page(page, 1);
778 return 0;
781 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
783 int err;
785 while (pg_start < pg_end) {
786 struct dnode_of_data dn;
787 pgoff_t end_offset, count;
789 set_new_dnode(&dn, inode, NULL, NULL, 0);
790 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
791 if (err) {
792 if (err == -ENOENT) {
793 pg_start++;
794 continue;
796 return err;
799 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
800 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
802 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
804 truncate_data_blocks_range(&dn, count);
805 f2fs_put_dnode(&dn);
807 pg_start += count;
809 return 0;
812 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
814 pgoff_t pg_start, pg_end;
815 loff_t off_start, off_end;
816 int ret;
818 ret = f2fs_convert_inline_inode(inode);
819 if (ret)
820 return ret;
822 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
823 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
825 off_start = offset & (PAGE_SIZE - 1);
826 off_end = (offset + len) & (PAGE_SIZE - 1);
828 if (pg_start == pg_end) {
829 ret = fill_zero(inode, pg_start, off_start,
830 off_end - off_start);
831 if (ret)
832 return ret;
833 } else {
834 if (off_start) {
835 ret = fill_zero(inode, pg_start++, off_start,
836 PAGE_SIZE - off_start);
837 if (ret)
838 return ret;
840 if (off_end) {
841 ret = fill_zero(inode, pg_end, 0, off_end);
842 if (ret)
843 return ret;
846 if (pg_start < pg_end) {
847 struct address_space *mapping = inode->i_mapping;
848 loff_t blk_start, blk_end;
849 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
851 f2fs_balance_fs(sbi, true);
853 blk_start = (loff_t)pg_start << PAGE_SHIFT;
854 blk_end = (loff_t)pg_end << PAGE_SHIFT;
855 truncate_inode_pages_range(mapping, blk_start,
856 blk_end - 1);
858 f2fs_lock_op(sbi);
859 ret = truncate_hole(inode, pg_start, pg_end);
860 f2fs_unlock_op(sbi);
864 return ret;
867 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
868 int *do_replace, pgoff_t off, pgoff_t len)
870 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
871 struct dnode_of_data dn;
872 int ret, done, i;
874 next_dnode:
875 set_new_dnode(&dn, inode, NULL, NULL, 0);
876 ret = get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
877 if (ret && ret != -ENOENT) {
878 return ret;
879 } else if (ret == -ENOENT) {
880 if (dn.max_level == 0)
881 return -ENOENT;
882 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
883 blkaddr += done;
884 do_replace += done;
885 goto next;
888 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
889 dn.ofs_in_node, len);
890 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
891 *blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
892 if (!is_checkpointed_data(sbi, *blkaddr)) {
894 if (test_opt(sbi, LFS)) {
895 f2fs_put_dnode(&dn);
896 return -ENOTSUPP;
899 /* do not invalidate this block address */
900 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
901 *do_replace = 1;
904 f2fs_put_dnode(&dn);
905 next:
906 len -= done;
907 off += done;
908 if (len)
909 goto next_dnode;
910 return 0;
913 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
914 int *do_replace, pgoff_t off, int len)
916 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
917 struct dnode_of_data dn;
918 int ret, i;
920 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
921 if (*do_replace == 0)
922 continue;
924 set_new_dnode(&dn, inode, NULL, NULL, 0);
925 ret = get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
926 if (ret) {
927 dec_valid_block_count(sbi, inode, 1);
928 invalidate_blocks(sbi, *blkaddr);
929 } else {
930 f2fs_update_data_blkaddr(&dn, *blkaddr);
932 f2fs_put_dnode(&dn);
934 return 0;
937 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
938 block_t *blkaddr, int *do_replace,
939 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
941 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
942 pgoff_t i = 0;
943 int ret;
945 while (i < len) {
946 if (blkaddr[i] == NULL_ADDR && !full) {
947 i++;
948 continue;
951 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
952 struct dnode_of_data dn;
953 struct node_info ni;
954 size_t new_size;
955 pgoff_t ilen;
957 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
958 ret = get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
959 if (ret)
960 return ret;
962 get_node_info(sbi, dn.nid, &ni);
963 ilen = min((pgoff_t)
964 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
965 dn.ofs_in_node, len - i);
966 do {
967 dn.data_blkaddr = datablock_addr(dn.node_page,
968 dn.ofs_in_node);
969 truncate_data_blocks_range(&dn, 1);
971 if (do_replace[i]) {
972 f2fs_i_blocks_write(src_inode,
973 1, false);
974 f2fs_i_blocks_write(dst_inode,
975 1, true);
976 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
977 blkaddr[i], ni.version, true, false);
979 do_replace[i] = 0;
981 dn.ofs_in_node++;
982 i++;
983 new_size = (loff_t)(dst + i) << PAGE_SHIFT;
984 if (dst_inode->i_size < new_size)
985 f2fs_i_size_write(dst_inode, new_size);
986 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
988 f2fs_put_dnode(&dn);
989 } else {
990 struct page *psrc, *pdst;
992 psrc = get_lock_data_page(src_inode, src + i, true);
993 if (IS_ERR(psrc))
994 return PTR_ERR(psrc);
995 pdst = get_new_data_page(dst_inode, NULL, dst + i,
996 true);
997 if (IS_ERR(pdst)) {
998 f2fs_put_page(psrc, 1);
999 return PTR_ERR(pdst);
1001 f2fs_copy_page(psrc, pdst);
1002 set_page_dirty(pdst);
1003 f2fs_put_page(pdst, 1);
1004 f2fs_put_page(psrc, 1);
1006 ret = truncate_hole(src_inode, src + i, src + i + 1);
1007 if (ret)
1008 return ret;
1009 i++;
1012 return 0;
1015 static int __exchange_data_block(struct inode *src_inode,
1016 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1017 pgoff_t len, bool full)
1019 block_t *src_blkaddr;
1020 int *do_replace;
1021 pgoff_t olen;
1022 int ret;
1024 while (len) {
1025 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
1027 src_blkaddr = f2fs_kvzalloc(sizeof(block_t) * olen, GFP_KERNEL);
1028 if (!src_blkaddr)
1029 return -ENOMEM;
1031 do_replace = f2fs_kvzalloc(sizeof(int) * olen, GFP_KERNEL);
1032 if (!do_replace) {
1033 kvfree(src_blkaddr);
1034 return -ENOMEM;
1037 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1038 do_replace, src, olen);
1039 if (ret)
1040 goto roll_back;
1042 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1043 do_replace, src, dst, olen, full);
1044 if (ret)
1045 goto roll_back;
1047 src += olen;
1048 dst += olen;
1049 len -= olen;
1051 kvfree(src_blkaddr);
1052 kvfree(do_replace);
1054 return 0;
1056 roll_back:
1057 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, len);
1058 kvfree(src_blkaddr);
1059 kvfree(do_replace);
1060 return ret;
1063 static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
1065 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1066 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1067 int ret;
1069 f2fs_balance_fs(sbi, true);
1070 f2fs_lock_op(sbi);
1072 f2fs_drop_extent_tree(inode);
1074 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1075 f2fs_unlock_op(sbi);
1076 return ret;
1079 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1081 pgoff_t pg_start, pg_end;
1082 loff_t new_size;
1083 int ret;
1085 if (offset + len >= i_size_read(inode))
1086 return -EINVAL;
1088 /* collapse range should be aligned to block size of f2fs. */
1089 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1090 return -EINVAL;
1092 ret = f2fs_convert_inline_inode(inode);
1093 if (ret)
1094 return ret;
1096 pg_start = offset >> PAGE_SHIFT;
1097 pg_end = (offset + len) >> PAGE_SHIFT;
1099 /* write out all dirty pages from offset */
1100 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1101 if (ret)
1102 return ret;
1104 truncate_pagecache(inode, offset);
1106 ret = f2fs_do_collapse(inode, pg_start, pg_end);
1107 if (ret)
1108 return ret;
1110 /* write out all moved pages, if possible */
1111 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1112 truncate_pagecache(inode, offset);
1114 new_size = i_size_read(inode) - len;
1115 truncate_pagecache(inode, new_size);
1117 ret = truncate_blocks(inode, new_size, true);
1118 if (!ret)
1119 f2fs_i_size_write(inode, new_size);
1121 return ret;
1124 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1125 pgoff_t end)
1127 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1128 pgoff_t index = start;
1129 unsigned int ofs_in_node = dn->ofs_in_node;
1130 blkcnt_t count = 0;
1131 int ret;
1133 for (; index < end; index++, dn->ofs_in_node++) {
1134 if (datablock_addr(dn->node_page, dn->ofs_in_node) == NULL_ADDR)
1135 count++;
1138 dn->ofs_in_node = ofs_in_node;
1139 ret = reserve_new_blocks(dn, count);
1140 if (ret)
1141 return ret;
1143 dn->ofs_in_node = ofs_in_node;
1144 for (index = start; index < end; index++, dn->ofs_in_node++) {
1145 dn->data_blkaddr =
1146 datablock_addr(dn->node_page, dn->ofs_in_node);
1148 * reserve_new_blocks will not guarantee entire block
1149 * allocation.
1151 if (dn->data_blkaddr == NULL_ADDR) {
1152 ret = -ENOSPC;
1153 break;
1155 if (dn->data_blkaddr != NEW_ADDR) {
1156 invalidate_blocks(sbi, dn->data_blkaddr);
1157 dn->data_blkaddr = NEW_ADDR;
1158 set_data_blkaddr(dn);
1162 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1164 return ret;
1167 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1168 int mode)
1170 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1171 struct address_space *mapping = inode->i_mapping;
1172 pgoff_t index, pg_start, pg_end;
1173 loff_t new_size = i_size_read(inode);
1174 loff_t off_start, off_end;
1175 int ret = 0;
1177 ret = inode_newsize_ok(inode, (len + offset));
1178 if (ret)
1179 return ret;
1181 ret = f2fs_convert_inline_inode(inode);
1182 if (ret)
1183 return ret;
1185 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1186 if (ret)
1187 return ret;
1189 truncate_pagecache_range(inode, offset, offset + len - 1);
1191 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1192 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1194 off_start = offset & (PAGE_SIZE - 1);
1195 off_end = (offset + len) & (PAGE_SIZE - 1);
1197 if (pg_start == pg_end) {
1198 ret = fill_zero(inode, pg_start, off_start,
1199 off_end - off_start);
1200 if (ret)
1201 return ret;
1203 if (offset + len > new_size)
1204 new_size = offset + len;
1205 new_size = max_t(loff_t, new_size, offset + len);
1206 } else {
1207 if (off_start) {
1208 ret = fill_zero(inode, pg_start++, off_start,
1209 PAGE_SIZE - off_start);
1210 if (ret)
1211 return ret;
1213 new_size = max_t(loff_t, new_size,
1214 (loff_t)pg_start << PAGE_SHIFT);
1217 for (index = pg_start; index < pg_end;) {
1218 struct dnode_of_data dn;
1219 unsigned int end_offset;
1220 pgoff_t end;
1222 f2fs_lock_op(sbi);
1224 set_new_dnode(&dn, inode, NULL, NULL, 0);
1225 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
1226 if (ret) {
1227 f2fs_unlock_op(sbi);
1228 goto out;
1231 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1232 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1234 ret = f2fs_do_zero_range(&dn, index, end);
1235 f2fs_put_dnode(&dn);
1236 f2fs_unlock_op(sbi);
1237 if (ret)
1238 goto out;
1240 index = end;
1241 new_size = max_t(loff_t, new_size,
1242 (loff_t)index << PAGE_SHIFT);
1245 if (off_end) {
1246 ret = fill_zero(inode, pg_end, 0, off_end);
1247 if (ret)
1248 goto out;
1250 new_size = max_t(loff_t, new_size, offset + len);
1254 out:
1255 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
1256 f2fs_i_size_write(inode, new_size);
1258 return ret;
1261 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1263 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1264 pgoff_t nr, pg_start, pg_end, delta, idx;
1265 loff_t new_size;
1266 int ret = 0;
1268 new_size = i_size_read(inode) + len;
1269 if (new_size > inode->i_sb->s_maxbytes)
1270 return -EFBIG;
1272 if (offset >= i_size_read(inode))
1273 return -EINVAL;
1275 /* insert range should be aligned to block size of f2fs. */
1276 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1277 return -EINVAL;
1279 ret = f2fs_convert_inline_inode(inode);
1280 if (ret)
1281 return ret;
1283 f2fs_balance_fs(sbi, true);
1285 ret = truncate_blocks(inode, i_size_read(inode), true);
1286 if (ret)
1287 return ret;
1289 /* write out all dirty pages from offset */
1290 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1291 if (ret)
1292 return ret;
1294 truncate_pagecache(inode, offset);
1296 pg_start = offset >> PAGE_SHIFT;
1297 pg_end = (offset + len) >> PAGE_SHIFT;
1298 delta = pg_end - pg_start;
1299 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1301 while (!ret && idx > pg_start) {
1302 nr = idx - pg_start;
1303 if (nr > delta)
1304 nr = delta;
1305 idx -= nr;
1307 f2fs_lock_op(sbi);
1308 f2fs_drop_extent_tree(inode);
1310 ret = __exchange_data_block(inode, inode, idx,
1311 idx + delta, nr, false);
1312 f2fs_unlock_op(sbi);
1315 /* write out all moved pages, if possible */
1316 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1317 truncate_pagecache(inode, offset);
1319 if (!ret)
1320 f2fs_i_size_write(inode, new_size);
1321 return ret;
1324 static int expand_inode_data(struct inode *inode, loff_t offset,
1325 loff_t len, int mode)
1327 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1328 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
1329 pgoff_t pg_end;
1330 loff_t new_size = i_size_read(inode);
1331 loff_t off_end;
1332 int ret;
1334 ret = inode_newsize_ok(inode, (len + offset));
1335 if (ret)
1336 return ret;
1338 ret = f2fs_convert_inline_inode(inode);
1339 if (ret)
1340 return ret;
1342 f2fs_balance_fs(sbi, true);
1344 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1345 off_end = (offset + len) & (PAGE_SIZE - 1);
1347 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1348 map.m_len = pg_end - map.m_lblk;
1349 if (off_end)
1350 map.m_len++;
1352 ret = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1353 if (ret) {
1354 pgoff_t last_off;
1356 if (!map.m_len)
1357 return ret;
1359 last_off = map.m_lblk + map.m_len - 1;
1361 /* update new size to the failed position */
1362 new_size = (last_off == pg_end) ? offset + len:
1363 (loff_t)(last_off + 1) << PAGE_SHIFT;
1364 } else {
1365 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1368 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
1369 f2fs_i_size_write(inode, new_size);
1371 return ret;
1374 static long f2fs_fallocate(struct file *file, int mode,
1375 loff_t offset, loff_t len)
1377 struct inode *inode = file_inode(file);
1378 long ret = 0;
1380 /* f2fs only support ->fallocate for regular file */
1381 if (!S_ISREG(inode->i_mode))
1382 return -EINVAL;
1384 if (f2fs_encrypted_inode(inode) &&
1385 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1386 return -EOPNOTSUPP;
1388 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1389 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1390 FALLOC_FL_INSERT_RANGE))
1391 return -EOPNOTSUPP;
1393 inode_lock(inode);
1395 if (mode & FALLOC_FL_PUNCH_HOLE) {
1396 if (offset >= inode->i_size)
1397 goto out;
1399 ret = punch_hole(inode, offset, len);
1400 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1401 ret = f2fs_collapse_range(inode, offset, len);
1402 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1403 ret = f2fs_zero_range(inode, offset, len, mode);
1404 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1405 ret = f2fs_insert_range(inode, offset, len);
1406 } else {
1407 ret = expand_inode_data(inode, offset, len, mode);
1410 if (!ret) {
1411 inode->i_mtime = inode->i_ctime = current_time(inode);
1412 f2fs_mark_inode_dirty_sync(inode);
1413 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1416 out:
1417 inode_unlock(inode);
1419 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1420 return ret;
1423 static int f2fs_release_file(struct inode *inode, struct file *filp)
1426 * f2fs_relase_file is called at every close calls. So we should
1427 * not drop any inmemory pages by close called by other process.
1429 if (!(filp->f_mode & FMODE_WRITE) ||
1430 atomic_read(&inode->i_writecount) != 1)
1431 return 0;
1433 /* some remained atomic pages should discarded */
1434 if (f2fs_is_atomic_file(inode))
1435 drop_inmem_pages(inode);
1436 if (f2fs_is_volatile_file(inode)) {
1437 clear_inode_flag(inode, FI_VOLATILE_FILE);
1438 set_inode_flag(inode, FI_DROP_CACHE);
1439 filemap_fdatawrite(inode->i_mapping);
1440 clear_inode_flag(inode, FI_DROP_CACHE);
1442 return 0;
1445 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1446 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1448 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
1450 if (S_ISDIR(mode))
1451 return flags;
1452 else if (S_ISREG(mode))
1453 return flags & F2FS_REG_FLMASK;
1454 else
1455 return flags & F2FS_OTHER_FLMASK;
1458 static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1460 struct inode *inode = file_inode(filp);
1461 struct f2fs_inode_info *fi = F2FS_I(inode);
1462 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1463 return put_user(flags, (int __user *)arg);
1466 static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1468 struct inode *inode = file_inode(filp);
1469 struct f2fs_inode_info *fi = F2FS_I(inode);
1470 unsigned int flags;
1471 unsigned int oldflags;
1472 int ret;
1474 if (!inode_owner_or_capable(inode))
1475 return -EACCES;
1477 if (get_user(flags, (int __user *)arg))
1478 return -EFAULT;
1480 ret = mnt_want_write_file(filp);
1481 if (ret)
1482 return ret;
1484 flags = f2fs_mask_flags(inode->i_mode, flags);
1486 inode_lock(inode);
1488 oldflags = fi->i_flags;
1490 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
1491 if (!capable(CAP_LINUX_IMMUTABLE)) {
1492 inode_unlock(inode);
1493 ret = -EPERM;
1494 goto out;
1498 flags = flags & FS_FL_USER_MODIFIABLE;
1499 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
1500 fi->i_flags = flags;
1501 inode_unlock(inode);
1503 inode->i_ctime = current_time(inode);
1504 f2fs_set_inode_flags(inode);
1505 out:
1506 mnt_drop_write_file(filp);
1507 return ret;
1510 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1512 struct inode *inode = file_inode(filp);
1514 return put_user(inode->i_generation, (int __user *)arg);
1517 static int f2fs_ioc_start_atomic_write(struct file *filp)
1519 struct inode *inode = file_inode(filp);
1520 int ret;
1522 if (!inode_owner_or_capable(inode))
1523 return -EACCES;
1525 ret = mnt_want_write_file(filp);
1526 if (ret)
1527 return ret;
1529 inode_lock(inode);
1531 down_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1533 if (f2fs_is_atomic_file(inode))
1534 goto out;
1536 ret = f2fs_convert_inline_inode(inode);
1537 if (ret)
1538 goto out;
1540 set_inode_flag(inode, FI_ATOMIC_FILE);
1541 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1543 if (!get_dirty_pages(inode))
1544 goto out;
1546 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
1547 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
1548 inode->i_ino, get_dirty_pages(inode));
1549 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
1550 if (ret)
1551 clear_inode_flag(inode, FI_ATOMIC_FILE);
1552 out:
1553 up_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1554 inode_unlock(inode);
1555 mnt_drop_write_file(filp);
1556 return ret;
1559 static int f2fs_ioc_commit_atomic_write(struct file *filp)
1561 struct inode *inode = file_inode(filp);
1562 int ret;
1564 if (!inode_owner_or_capable(inode))
1565 return -EACCES;
1567 ret = mnt_want_write_file(filp);
1568 if (ret)
1569 return ret;
1571 inode_lock(inode);
1573 if (f2fs_is_volatile_file(inode))
1574 goto err_out;
1576 if (f2fs_is_atomic_file(inode)) {
1577 clear_inode_flag(inode, FI_ATOMIC_FILE);
1578 ret = commit_inmem_pages(inode);
1579 if (ret) {
1580 set_inode_flag(inode, FI_ATOMIC_FILE);
1581 goto err_out;
1585 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1586 err_out:
1587 inode_unlock(inode);
1588 mnt_drop_write_file(filp);
1589 return ret;
1592 static int f2fs_ioc_start_volatile_write(struct file *filp)
1594 struct inode *inode = file_inode(filp);
1595 int ret;
1597 if (!inode_owner_or_capable(inode))
1598 return -EACCES;
1600 ret = mnt_want_write_file(filp);
1601 if (ret)
1602 return ret;
1604 inode_lock(inode);
1606 if (f2fs_is_volatile_file(inode))
1607 goto out;
1609 ret = f2fs_convert_inline_inode(inode);
1610 if (ret)
1611 goto out;
1613 set_inode_flag(inode, FI_VOLATILE_FILE);
1614 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1615 out:
1616 inode_unlock(inode);
1617 mnt_drop_write_file(filp);
1618 return ret;
1621 static int f2fs_ioc_release_volatile_write(struct file *filp)
1623 struct inode *inode = file_inode(filp);
1624 int ret;
1626 if (!inode_owner_or_capable(inode))
1627 return -EACCES;
1629 ret = mnt_want_write_file(filp);
1630 if (ret)
1631 return ret;
1633 inode_lock(inode);
1635 if (!f2fs_is_volatile_file(inode))
1636 goto out;
1638 if (!f2fs_is_first_block_written(inode)) {
1639 ret = truncate_partial_data_page(inode, 0, true);
1640 goto out;
1643 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1644 out:
1645 inode_unlock(inode);
1646 mnt_drop_write_file(filp);
1647 return ret;
1650 static int f2fs_ioc_abort_volatile_write(struct file *filp)
1652 struct inode *inode = file_inode(filp);
1653 int ret;
1655 if (!inode_owner_or_capable(inode))
1656 return -EACCES;
1658 ret = mnt_want_write_file(filp);
1659 if (ret)
1660 return ret;
1662 inode_lock(inode);
1664 if (f2fs_is_atomic_file(inode))
1665 drop_inmem_pages(inode);
1666 if (f2fs_is_volatile_file(inode)) {
1667 clear_inode_flag(inode, FI_VOLATILE_FILE);
1668 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1671 inode_unlock(inode);
1673 mnt_drop_write_file(filp);
1674 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1675 return ret;
1678 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1680 struct inode *inode = file_inode(filp);
1681 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1682 struct super_block *sb = sbi->sb;
1683 __u32 in;
1684 int ret = 0;
1686 if (!capable(CAP_SYS_ADMIN))
1687 return -EPERM;
1689 if (get_user(in, (__u32 __user *)arg))
1690 return -EFAULT;
1692 if (in != F2FS_GOING_DOWN_FULLSYNC) {
1693 ret = mnt_want_write_file(filp);
1694 if (ret)
1695 return ret;
1698 switch (in) {
1699 case F2FS_GOING_DOWN_FULLSYNC:
1700 sb = freeze_bdev(sb->s_bdev);
1701 if (sb && !IS_ERR(sb)) {
1702 f2fs_stop_checkpoint(sbi, false);
1703 thaw_bdev(sb->s_bdev, sb);
1705 break;
1706 case F2FS_GOING_DOWN_METASYNC:
1707 /* do checkpoint only */
1708 f2fs_sync_fs(sb, 1);
1709 f2fs_stop_checkpoint(sbi, false);
1710 break;
1711 case F2FS_GOING_DOWN_NOSYNC:
1712 f2fs_stop_checkpoint(sbi, false);
1713 break;
1714 case F2FS_GOING_DOWN_METAFLUSH:
1715 sync_meta_pages(sbi, META, LONG_MAX);
1716 f2fs_stop_checkpoint(sbi, false);
1717 break;
1718 default:
1719 ret = -EINVAL;
1720 goto out;
1722 f2fs_update_time(sbi, REQ_TIME);
1723 out:
1724 if (in != F2FS_GOING_DOWN_FULLSYNC)
1725 mnt_drop_write_file(filp);
1726 return ret;
1729 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1731 struct inode *inode = file_inode(filp);
1732 struct super_block *sb = inode->i_sb;
1733 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1734 struct fstrim_range range;
1735 int ret;
1737 if (!capable(CAP_SYS_ADMIN))
1738 return -EPERM;
1740 if (!blk_queue_discard(q))
1741 return -EOPNOTSUPP;
1743 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1744 sizeof(range)))
1745 return -EFAULT;
1747 ret = mnt_want_write_file(filp);
1748 if (ret)
1749 return ret;
1751 range.minlen = max((unsigned int)range.minlen,
1752 q->limits.discard_granularity);
1753 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
1754 mnt_drop_write_file(filp);
1755 if (ret < 0)
1756 return ret;
1758 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1759 sizeof(range)))
1760 return -EFAULT;
1761 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1762 return 0;
1765 static bool uuid_is_nonzero(__u8 u[16])
1767 int i;
1769 for (i = 0; i < 16; i++)
1770 if (u[i])
1771 return true;
1772 return false;
1775 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1777 struct fscrypt_policy policy;
1778 struct inode *inode = file_inode(filp);
1780 if (copy_from_user(&policy, (struct fscrypt_policy __user *)arg,
1781 sizeof(policy)))
1782 return -EFAULT;
1784 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1786 return fscrypt_process_policy(filp, &policy);
1789 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1791 struct fscrypt_policy policy;
1792 struct inode *inode = file_inode(filp);
1793 int err;
1795 err = fscrypt_get_policy(inode, &policy);
1796 if (err)
1797 return err;
1799 if (copy_to_user((struct fscrypt_policy __user *)arg, &policy, sizeof(policy)))
1800 return -EFAULT;
1801 return 0;
1804 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1806 struct inode *inode = file_inode(filp);
1807 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1808 int err;
1810 if (!f2fs_sb_has_crypto(inode->i_sb))
1811 return -EOPNOTSUPP;
1813 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1814 goto got_it;
1816 err = mnt_want_write_file(filp);
1817 if (err)
1818 return err;
1820 /* update superblock with uuid */
1821 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1823 err = f2fs_commit_super(sbi, false);
1824 if (err) {
1825 /* undo new data */
1826 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
1827 mnt_drop_write_file(filp);
1828 return err;
1830 mnt_drop_write_file(filp);
1831 got_it:
1832 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1833 16))
1834 return -EFAULT;
1835 return 0;
1838 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
1840 struct inode *inode = file_inode(filp);
1841 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1842 __u32 sync;
1843 int ret;
1845 if (!capable(CAP_SYS_ADMIN))
1846 return -EPERM;
1848 if (get_user(sync, (__u32 __user *)arg))
1849 return -EFAULT;
1851 if (f2fs_readonly(sbi->sb))
1852 return -EROFS;
1854 ret = mnt_want_write_file(filp);
1855 if (ret)
1856 return ret;
1858 if (!sync) {
1859 if (!mutex_trylock(&sbi->gc_mutex)) {
1860 ret = -EBUSY;
1861 goto out;
1863 } else {
1864 mutex_lock(&sbi->gc_mutex);
1867 ret = f2fs_gc(sbi, sync);
1868 out:
1869 mnt_drop_write_file(filp);
1870 return ret;
1873 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
1875 struct inode *inode = file_inode(filp);
1876 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1877 int ret;
1879 if (!capable(CAP_SYS_ADMIN))
1880 return -EPERM;
1882 if (f2fs_readonly(sbi->sb))
1883 return -EROFS;
1885 ret = mnt_want_write_file(filp);
1886 if (ret)
1887 return ret;
1889 ret = f2fs_sync_fs(sbi->sb, 1);
1891 mnt_drop_write_file(filp);
1892 return ret;
1895 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
1896 struct file *filp,
1897 struct f2fs_defragment *range)
1899 struct inode *inode = file_inode(filp);
1900 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
1901 struct extent_info ei;
1902 pgoff_t pg_start, pg_end;
1903 unsigned int blk_per_seg = sbi->blocks_per_seg;
1904 unsigned int total = 0, sec_num;
1905 unsigned int pages_per_sec = sbi->segs_per_sec * blk_per_seg;
1906 block_t blk_end = 0;
1907 bool fragmented = false;
1908 int err;
1910 /* if in-place-update policy is enabled, don't waste time here */
1911 if (need_inplace_update(inode))
1912 return -EINVAL;
1914 pg_start = range->start >> PAGE_SHIFT;
1915 pg_end = (range->start + range->len) >> PAGE_SHIFT;
1917 f2fs_balance_fs(sbi, true);
1919 inode_lock(inode);
1921 /* writeback all dirty pages in the range */
1922 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
1923 range->start + range->len - 1);
1924 if (err)
1925 goto out;
1928 * lookup mapping info in extent cache, skip defragmenting if physical
1929 * block addresses are continuous.
1931 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
1932 if (ei.fofs + ei.len >= pg_end)
1933 goto out;
1936 map.m_lblk = pg_start;
1939 * lookup mapping info in dnode page cache, skip defragmenting if all
1940 * physical block addresses are continuous even if there are hole(s)
1941 * in logical blocks.
1943 while (map.m_lblk < pg_end) {
1944 map.m_len = pg_end - map.m_lblk;
1945 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
1946 if (err)
1947 goto out;
1949 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1950 map.m_lblk++;
1951 continue;
1954 if (blk_end && blk_end != map.m_pblk) {
1955 fragmented = true;
1956 break;
1958 blk_end = map.m_pblk + map.m_len;
1960 map.m_lblk += map.m_len;
1963 if (!fragmented)
1964 goto out;
1966 map.m_lblk = pg_start;
1967 map.m_len = pg_end - pg_start;
1969 sec_num = (map.m_len + pages_per_sec - 1) / pages_per_sec;
1972 * make sure there are enough free section for LFS allocation, this can
1973 * avoid defragment running in SSR mode when free section are allocated
1974 * intensively
1976 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
1977 err = -EAGAIN;
1978 goto out;
1981 while (map.m_lblk < pg_end) {
1982 pgoff_t idx;
1983 int cnt = 0;
1985 do_map:
1986 map.m_len = pg_end - map.m_lblk;
1987 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
1988 if (err)
1989 goto clear_out;
1991 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1992 map.m_lblk++;
1993 continue;
1996 set_inode_flag(inode, FI_DO_DEFRAG);
1998 idx = map.m_lblk;
1999 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2000 struct page *page;
2002 page = get_lock_data_page(inode, idx, true);
2003 if (IS_ERR(page)) {
2004 err = PTR_ERR(page);
2005 goto clear_out;
2008 set_page_dirty(page);
2009 f2fs_put_page(page, 1);
2011 idx++;
2012 cnt++;
2013 total++;
2016 map.m_lblk = idx;
2018 if (idx < pg_end && cnt < blk_per_seg)
2019 goto do_map;
2021 clear_inode_flag(inode, FI_DO_DEFRAG);
2023 err = filemap_fdatawrite(inode->i_mapping);
2024 if (err)
2025 goto out;
2027 clear_out:
2028 clear_inode_flag(inode, FI_DO_DEFRAG);
2029 out:
2030 inode_unlock(inode);
2031 if (!err)
2032 range->len = (u64)total << PAGE_SHIFT;
2033 return err;
2036 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2038 struct inode *inode = file_inode(filp);
2039 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2040 struct f2fs_defragment range;
2041 int err;
2043 if (!capable(CAP_SYS_ADMIN))
2044 return -EPERM;
2046 if (!S_ISREG(inode->i_mode))
2047 return -EINVAL;
2049 err = mnt_want_write_file(filp);
2050 if (err)
2051 return err;
2053 if (f2fs_readonly(sbi->sb)) {
2054 err = -EROFS;
2055 goto out;
2058 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2059 sizeof(range))) {
2060 err = -EFAULT;
2061 goto out;
2064 /* verify alignment of offset & size */
2065 if (range.start & (F2FS_BLKSIZE - 1) ||
2066 range.len & (F2FS_BLKSIZE - 1)) {
2067 err = -EINVAL;
2068 goto out;
2071 err = f2fs_defragment_range(sbi, filp, &range);
2072 f2fs_update_time(sbi, REQ_TIME);
2073 if (err < 0)
2074 goto out;
2076 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2077 sizeof(range)))
2078 err = -EFAULT;
2079 out:
2080 mnt_drop_write_file(filp);
2081 return err;
2084 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2085 struct file *file_out, loff_t pos_out, size_t len)
2087 struct inode *src = file_inode(file_in);
2088 struct inode *dst = file_inode(file_out);
2089 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2090 size_t olen = len, dst_max_i_size = 0;
2091 size_t dst_osize;
2092 int ret;
2094 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2095 src->i_sb != dst->i_sb)
2096 return -EXDEV;
2098 if (unlikely(f2fs_readonly(src->i_sb)))
2099 return -EROFS;
2101 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2102 return -EINVAL;
2104 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2105 return -EOPNOTSUPP;
2107 if (src == dst) {
2108 if (pos_in == pos_out)
2109 return 0;
2110 if (pos_out > pos_in && pos_out < pos_in + len)
2111 return -EINVAL;
2114 inode_lock(src);
2115 if (src != dst) {
2116 if (!inode_trylock(dst)) {
2117 ret = -EBUSY;
2118 goto out;
2122 ret = -EINVAL;
2123 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2124 goto out_unlock;
2125 if (len == 0)
2126 olen = len = src->i_size - pos_in;
2127 if (pos_in + len == src->i_size)
2128 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2129 if (len == 0) {
2130 ret = 0;
2131 goto out_unlock;
2134 dst_osize = dst->i_size;
2135 if (pos_out + olen > dst->i_size)
2136 dst_max_i_size = pos_out + olen;
2138 /* verify the end result is block aligned */
2139 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2140 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2141 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2142 goto out_unlock;
2144 ret = f2fs_convert_inline_inode(src);
2145 if (ret)
2146 goto out_unlock;
2148 ret = f2fs_convert_inline_inode(dst);
2149 if (ret)
2150 goto out_unlock;
2152 /* write out all dirty pages from offset */
2153 ret = filemap_write_and_wait_range(src->i_mapping,
2154 pos_in, pos_in + len);
2155 if (ret)
2156 goto out_unlock;
2158 ret = filemap_write_and_wait_range(dst->i_mapping,
2159 pos_out, pos_out + len);
2160 if (ret)
2161 goto out_unlock;
2163 f2fs_balance_fs(sbi, true);
2164 f2fs_lock_op(sbi);
2165 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2166 pos_out >> F2FS_BLKSIZE_BITS,
2167 len >> F2FS_BLKSIZE_BITS, false);
2169 if (!ret) {
2170 if (dst_max_i_size)
2171 f2fs_i_size_write(dst, dst_max_i_size);
2172 else if (dst_osize != dst->i_size)
2173 f2fs_i_size_write(dst, dst_osize);
2175 f2fs_unlock_op(sbi);
2176 out_unlock:
2177 if (src != dst)
2178 inode_unlock(dst);
2179 out:
2180 inode_unlock(src);
2181 return ret;
2184 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2186 struct f2fs_move_range range;
2187 struct fd dst;
2188 int err;
2190 if (!(filp->f_mode & FMODE_READ) ||
2191 !(filp->f_mode & FMODE_WRITE))
2192 return -EBADF;
2194 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2195 sizeof(range)))
2196 return -EFAULT;
2198 dst = fdget(range.dst_fd);
2199 if (!dst.file)
2200 return -EBADF;
2202 if (!(dst.file->f_mode & FMODE_WRITE)) {
2203 err = -EBADF;
2204 goto err_out;
2207 err = mnt_want_write_file(filp);
2208 if (err)
2209 goto err_out;
2211 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2212 range.pos_out, range.len);
2214 mnt_drop_write_file(filp);
2216 if (copy_to_user((struct f2fs_move_range __user *)arg,
2217 &range, sizeof(range)))
2218 err = -EFAULT;
2219 err_out:
2220 fdput(dst);
2221 return err;
2224 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2226 switch (cmd) {
2227 case F2FS_IOC_GETFLAGS:
2228 return f2fs_ioc_getflags(filp, arg);
2229 case F2FS_IOC_SETFLAGS:
2230 return f2fs_ioc_setflags(filp, arg);
2231 case F2FS_IOC_GETVERSION:
2232 return f2fs_ioc_getversion(filp, arg);
2233 case F2FS_IOC_START_ATOMIC_WRITE:
2234 return f2fs_ioc_start_atomic_write(filp);
2235 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2236 return f2fs_ioc_commit_atomic_write(filp);
2237 case F2FS_IOC_START_VOLATILE_WRITE:
2238 return f2fs_ioc_start_volatile_write(filp);
2239 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2240 return f2fs_ioc_release_volatile_write(filp);
2241 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2242 return f2fs_ioc_abort_volatile_write(filp);
2243 case F2FS_IOC_SHUTDOWN:
2244 return f2fs_ioc_shutdown(filp, arg);
2245 case FITRIM:
2246 return f2fs_ioc_fitrim(filp, arg);
2247 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2248 return f2fs_ioc_set_encryption_policy(filp, arg);
2249 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2250 return f2fs_ioc_get_encryption_policy(filp, arg);
2251 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2252 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
2253 case F2FS_IOC_GARBAGE_COLLECT:
2254 return f2fs_ioc_gc(filp, arg);
2255 case F2FS_IOC_WRITE_CHECKPOINT:
2256 return f2fs_ioc_write_checkpoint(filp, arg);
2257 case F2FS_IOC_DEFRAGMENT:
2258 return f2fs_ioc_defragment(filp, arg);
2259 case F2FS_IOC_MOVE_RANGE:
2260 return f2fs_ioc_move_range(filp, arg);
2261 default:
2262 return -ENOTTY;
2266 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2268 struct file *file = iocb->ki_filp;
2269 struct inode *inode = file_inode(file);
2270 struct blk_plug plug;
2271 ssize_t ret;
2273 if (f2fs_encrypted_inode(inode) &&
2274 !fscrypt_has_encryption_key(inode) &&
2275 fscrypt_get_encryption_info(inode))
2276 return -EACCES;
2278 inode_lock(inode);
2279 ret = generic_write_checks(iocb, from);
2280 if (ret > 0) {
2281 ret = f2fs_preallocate_blocks(iocb, from);
2282 if (!ret) {
2283 blk_start_plug(&plug);
2284 ret = __generic_file_write_iter(iocb, from);
2285 blk_finish_plug(&plug);
2288 inode_unlock(inode);
2290 if (ret > 0)
2291 ret = generic_write_sync(iocb, ret);
2292 return ret;
2295 #ifdef CONFIG_COMPAT
2296 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2298 switch (cmd) {
2299 case F2FS_IOC32_GETFLAGS:
2300 cmd = F2FS_IOC_GETFLAGS;
2301 break;
2302 case F2FS_IOC32_SETFLAGS:
2303 cmd = F2FS_IOC_SETFLAGS;
2304 break;
2305 case F2FS_IOC32_GETVERSION:
2306 cmd = F2FS_IOC_GETVERSION;
2307 break;
2308 case F2FS_IOC_START_ATOMIC_WRITE:
2309 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2310 case F2FS_IOC_START_VOLATILE_WRITE:
2311 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2312 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2313 case F2FS_IOC_SHUTDOWN:
2314 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2315 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2316 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2317 case F2FS_IOC_GARBAGE_COLLECT:
2318 case F2FS_IOC_WRITE_CHECKPOINT:
2319 case F2FS_IOC_DEFRAGMENT:
2320 break;
2321 case F2FS_IOC_MOVE_RANGE:
2322 break;
2323 default:
2324 return -ENOIOCTLCMD;
2326 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
2328 #endif
2330 const struct file_operations f2fs_file_operations = {
2331 .llseek = f2fs_llseek,
2332 .read_iter = generic_file_read_iter,
2333 .write_iter = f2fs_file_write_iter,
2334 .open = f2fs_file_open,
2335 .release = f2fs_release_file,
2336 .mmap = f2fs_file_mmap,
2337 .fsync = f2fs_sync_file,
2338 .fallocate = f2fs_fallocate,
2339 .unlocked_ioctl = f2fs_ioctl,
2340 #ifdef CONFIG_COMPAT
2341 .compat_ioctl = f2fs_compat_ioctl,
2342 #endif
2343 .splice_read = generic_file_splice_read,
2344 .splice_write = iter_file_splice_write,