smp/hotplug: Undo tglxs brainfart
[linux/fpc-iii.git] / fs / f2fs / file.c
blob49f10dce817dc9e4806b6a417b96391a1c794fd1
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 out:
98 sb_end_pagefault(inode->i_sb);
99 f2fs_update_time(sbi, REQ_TIME);
100 return block_page_mkwrite_return(err);
103 static const struct vm_operations_struct f2fs_file_vm_ops = {
104 .fault = filemap_fault,
105 .map_pages = filemap_map_pages,
106 .page_mkwrite = f2fs_vm_page_mkwrite,
109 static int get_parent_ino(struct inode *inode, nid_t *pino)
111 struct dentry *dentry;
113 inode = igrab(inode);
114 dentry = d_find_any_alias(inode);
115 iput(inode);
116 if (!dentry)
117 return 0;
119 if (update_dent_inode(inode, inode, &dentry->d_name)) {
120 dput(dentry);
121 return 0;
124 *pino = parent_ino(dentry);
125 dput(dentry);
126 return 1;
129 static inline bool need_do_checkpoint(struct inode *inode)
131 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
132 bool need_cp = false;
134 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
135 need_cp = true;
136 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
137 need_cp = true;
138 else if (file_wrong_pino(inode))
139 need_cp = true;
140 else if (!space_for_roll_forward(sbi))
141 need_cp = true;
142 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
143 need_cp = true;
144 else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi)))
145 need_cp = true;
146 else if (test_opt(sbi, FASTBOOT))
147 need_cp = true;
148 else if (sbi->active_logs == 2)
149 need_cp = true;
151 return need_cp;
154 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
156 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
157 bool ret = false;
158 /* But we need to avoid that there are some inode updates */
159 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
160 ret = true;
161 f2fs_put_page(i, 0);
162 return ret;
165 static void try_to_fix_pino(struct inode *inode)
167 struct f2fs_inode_info *fi = F2FS_I(inode);
168 nid_t pino;
170 down_write(&fi->i_sem);
171 fi->xattr_ver = 0;
172 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
173 get_parent_ino(inode, &pino)) {
174 f2fs_i_pino_write(inode, pino);
175 file_got_pino(inode);
177 up_write(&fi->i_sem);
180 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
181 int datasync, bool atomic)
183 struct inode *inode = file->f_mapping->host;
184 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
185 nid_t ino = inode->i_ino;
186 int ret = 0;
187 bool need_cp = false;
188 struct writeback_control wbc = {
189 .sync_mode = WB_SYNC_ALL,
190 .nr_to_write = LONG_MAX,
191 .for_reclaim = 0,
194 if (unlikely(f2fs_readonly(inode->i_sb)))
195 return 0;
197 trace_f2fs_sync_file_enter(inode);
199 /* if fdatasync is triggered, let's do in-place-update */
200 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
201 set_inode_flag(inode, FI_NEED_IPU);
202 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
203 clear_inode_flag(inode, FI_NEED_IPU);
205 if (ret) {
206 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
207 return ret;
210 /* if the inode is dirty, let's recover all the time */
211 if (!f2fs_skip_inode_update(inode, datasync)) {
212 f2fs_write_inode(inode, NULL);
213 goto go_write;
217 * if there is no written data, don't waste time to write recovery info.
219 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
220 !exist_written_data(sbi, ino, APPEND_INO)) {
222 /* it may call write_inode just prior to fsync */
223 if (need_inode_page_update(sbi, ino))
224 goto go_write;
226 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
227 exist_written_data(sbi, ino, UPDATE_INO))
228 goto flush_out;
229 goto out;
231 go_write:
233 * Both of fdatasync() and fsync() are able to be recovered from
234 * sudden-power-off.
236 down_read(&F2FS_I(inode)->i_sem);
237 need_cp = need_do_checkpoint(inode);
238 up_read(&F2FS_I(inode)->i_sem);
240 if (need_cp) {
241 /* all the dirty node pages should be flushed for POR */
242 ret = f2fs_sync_fs(inode->i_sb, 1);
245 * We've secured consistency through sync_fs. Following pino
246 * will be used only for fsynced inodes after checkpoint.
248 try_to_fix_pino(inode);
249 clear_inode_flag(inode, FI_APPEND_WRITE);
250 clear_inode_flag(inode, FI_UPDATE_WRITE);
251 goto out;
253 sync_nodes:
254 ret = fsync_node_pages(sbi, inode, &wbc, atomic);
255 if (ret)
256 goto out;
258 /* if cp_error was enabled, we should avoid infinite loop */
259 if (unlikely(f2fs_cp_error(sbi))) {
260 ret = -EIO;
261 goto out;
264 if (need_inode_block_update(sbi, ino)) {
265 f2fs_mark_inode_dirty_sync(inode, true);
266 f2fs_write_inode(inode, NULL);
267 goto sync_nodes;
270 ret = wait_on_node_pages_writeback(sbi, ino);
271 if (ret)
272 goto out;
274 /* once recovery info is written, don't need to tack this */
275 remove_ino_entry(sbi, ino, APPEND_INO);
276 clear_inode_flag(inode, FI_APPEND_WRITE);
277 flush_out:
278 remove_ino_entry(sbi, ino, UPDATE_INO);
279 clear_inode_flag(inode, FI_UPDATE_WRITE);
280 ret = f2fs_issue_flush(sbi);
281 f2fs_update_time(sbi, REQ_TIME);
282 out:
283 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
284 f2fs_trace_ios(NULL, 1);
285 return ret;
288 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
290 return f2fs_do_sync_file(file, start, end, datasync, false);
293 static pgoff_t __get_first_dirty_index(struct address_space *mapping,
294 pgoff_t pgofs, int whence)
296 struct pagevec pvec;
297 int nr_pages;
299 if (whence != SEEK_DATA)
300 return 0;
302 /* find first dirty page index */
303 pagevec_init(&pvec, 0);
304 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
305 PAGECACHE_TAG_DIRTY, 1);
306 pgofs = nr_pages ? pvec.pages[0]->index : ULONG_MAX;
307 pagevec_release(&pvec);
308 return pgofs;
311 static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
312 int whence)
314 switch (whence) {
315 case SEEK_DATA:
316 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
317 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
318 return true;
319 break;
320 case SEEK_HOLE:
321 if (blkaddr == NULL_ADDR)
322 return true;
323 break;
325 return false;
328 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
330 struct inode *inode = file->f_mapping->host;
331 loff_t maxbytes = inode->i_sb->s_maxbytes;
332 struct dnode_of_data dn;
333 pgoff_t pgofs, end_offset, dirty;
334 loff_t data_ofs = offset;
335 loff_t isize;
336 int err = 0;
338 inode_lock(inode);
340 isize = i_size_read(inode);
341 if (offset >= isize)
342 goto fail;
344 /* handle inline data case */
345 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
346 if (whence == SEEK_HOLE)
347 data_ofs = isize;
348 goto found;
351 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
353 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
355 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
356 set_new_dnode(&dn, inode, NULL, NULL, 0);
357 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
358 if (err && err != -ENOENT) {
359 goto fail;
360 } else if (err == -ENOENT) {
361 /* direct node does not exists */
362 if (whence == SEEK_DATA) {
363 pgofs = get_next_page_offset(&dn, pgofs);
364 continue;
365 } else {
366 goto found;
370 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
372 /* find data/hole in dnode block */
373 for (; dn.ofs_in_node < end_offset;
374 dn.ofs_in_node++, pgofs++,
375 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
376 block_t blkaddr;
377 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
379 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
380 f2fs_put_dnode(&dn);
381 goto found;
384 f2fs_put_dnode(&dn);
387 if (whence == SEEK_DATA)
388 goto fail;
389 found:
390 if (whence == SEEK_HOLE && data_ofs > isize)
391 data_ofs = isize;
392 inode_unlock(inode);
393 return vfs_setpos(file, data_ofs, maxbytes);
394 fail:
395 inode_unlock(inode);
396 return -ENXIO;
399 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
401 struct inode *inode = file->f_mapping->host;
402 loff_t maxbytes = inode->i_sb->s_maxbytes;
404 switch (whence) {
405 case SEEK_SET:
406 case SEEK_CUR:
407 case SEEK_END:
408 return generic_file_llseek_size(file, offset, whence,
409 maxbytes, i_size_read(inode));
410 case SEEK_DATA:
411 case SEEK_HOLE:
412 if (offset < 0)
413 return -ENXIO;
414 return f2fs_seek_block(file, offset, whence);
417 return -EINVAL;
420 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
422 struct inode *inode = file_inode(file);
423 int err;
425 if (f2fs_encrypted_inode(inode)) {
426 err = fscrypt_get_encryption_info(inode);
427 if (err)
428 return 0;
429 if (!f2fs_encrypted_inode(inode))
430 return -ENOKEY;
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 int ret = generic_file_open(inode, filp);
446 struct dentry *dir;
448 if (!ret && f2fs_encrypted_inode(inode)) {
449 ret = fscrypt_get_encryption_info(inode);
450 if (ret)
451 return -EACCES;
452 if (!fscrypt_has_encryption_key(inode))
453 return -ENOKEY;
455 dir = dget_parent(file_dentry(filp));
456 if (f2fs_encrypted_inode(d_inode(dir)) &&
457 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
458 dput(dir);
459 return -EPERM;
461 dput(dir);
462 return ret;
465 int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
467 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
468 struct f2fs_node *raw_node;
469 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
470 __le32 *addr;
472 raw_node = F2FS_NODE(dn->node_page);
473 addr = blkaddr_in_node(raw_node) + ofs;
475 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
476 block_t blkaddr = le32_to_cpu(*addr);
477 if (blkaddr == NULL_ADDR)
478 continue;
480 dn->data_blkaddr = NULL_ADDR;
481 set_data_blkaddr(dn);
482 invalidate_blocks(sbi, blkaddr);
483 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
484 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
485 nr_free++;
488 if (nr_free) {
489 pgoff_t fofs;
491 * once we invalidate valid blkaddr in range [ofs, ofs + count],
492 * we will invalidate all blkaddr in the whole range.
494 fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
495 dn->inode) + ofs;
496 f2fs_update_extent_cache_range(dn, fofs, 0, len);
497 dec_valid_block_count(sbi, dn->inode, nr_free);
499 dn->ofs_in_node = ofs;
501 f2fs_update_time(sbi, REQ_TIME);
502 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
503 dn->ofs_in_node, nr_free);
504 return nr_free;
507 void truncate_data_blocks(struct dnode_of_data *dn)
509 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
512 static int truncate_partial_data_page(struct inode *inode, u64 from,
513 bool cache_only)
515 unsigned offset = from & (PAGE_SIZE - 1);
516 pgoff_t index = from >> PAGE_SHIFT;
517 struct address_space *mapping = inode->i_mapping;
518 struct page *page;
520 if (!offset && !cache_only)
521 return 0;
523 if (cache_only) {
524 page = find_lock_page(mapping, index);
525 if (page && PageUptodate(page))
526 goto truncate_out;
527 f2fs_put_page(page, 1);
528 return 0;
531 page = get_lock_data_page(inode, index, true);
532 if (IS_ERR(page))
533 return 0;
534 truncate_out:
535 f2fs_wait_on_page_writeback(page, DATA, true);
536 zero_user(page, offset, PAGE_SIZE - offset);
537 if (!cache_only || !f2fs_encrypted_inode(inode) ||
538 !S_ISREG(inode->i_mode))
539 set_page_dirty(page);
540 f2fs_put_page(page, 1);
541 return 0;
544 int truncate_blocks(struct inode *inode, u64 from, bool lock)
546 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
547 unsigned int blocksize = inode->i_sb->s_blocksize;
548 struct dnode_of_data dn;
549 pgoff_t free_from;
550 int count = 0, err = 0;
551 struct page *ipage;
552 bool truncate_page = false;
554 trace_f2fs_truncate_blocks_enter(inode, from);
556 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
558 if (free_from >= sbi->max_file_blocks)
559 goto free_partial;
561 if (lock)
562 f2fs_lock_op(sbi);
564 ipage = get_node_page(sbi, inode->i_ino);
565 if (IS_ERR(ipage)) {
566 err = PTR_ERR(ipage);
567 goto out;
570 if (f2fs_has_inline_data(inode)) {
571 if (truncate_inline_inode(ipage, from))
572 set_page_dirty(ipage);
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 /* we should check inline_data size */
622 if (!f2fs_may_inline_data(inode)) {
623 err = f2fs_convert_inline_inode(inode);
624 if (err)
625 return err;
628 err = truncate_blocks(inode, i_size_read(inode), true);
629 if (err)
630 return err;
632 inode->i_mtime = inode->i_ctime = current_time(inode);
633 f2fs_mark_inode_dirty_sync(inode, false);
634 return 0;
637 int f2fs_getattr(struct vfsmount *mnt,
638 struct dentry *dentry, struct kstat *stat)
640 struct inode *inode = d_inode(dentry);
641 generic_fillattr(inode, stat);
642 stat->blocks <<= 3;
643 return 0;
646 #ifdef CONFIG_F2FS_FS_POSIX_ACL
647 static void __setattr_copy(struct inode *inode, const struct iattr *attr)
649 unsigned int ia_valid = attr->ia_valid;
651 if (ia_valid & ATTR_UID)
652 inode->i_uid = attr->ia_uid;
653 if (ia_valid & ATTR_GID)
654 inode->i_gid = attr->ia_gid;
655 if (ia_valid & ATTR_ATIME)
656 inode->i_atime = timespec_trunc(attr->ia_atime,
657 inode->i_sb->s_time_gran);
658 if (ia_valid & ATTR_MTIME)
659 inode->i_mtime = timespec_trunc(attr->ia_mtime,
660 inode->i_sb->s_time_gran);
661 if (ia_valid & ATTR_CTIME)
662 inode->i_ctime = timespec_trunc(attr->ia_ctime,
663 inode->i_sb->s_time_gran);
664 if (ia_valid & ATTR_MODE) {
665 umode_t mode = attr->ia_mode;
667 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
668 mode &= ~S_ISGID;
669 set_acl_inode(inode, mode);
672 #else
673 #define __setattr_copy setattr_copy
674 #endif
676 int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
678 struct inode *inode = d_inode(dentry);
679 int err;
680 bool size_changed = false;
682 err = setattr_prepare(dentry, attr);
683 if (err)
684 return err;
686 if (attr->ia_valid & ATTR_SIZE) {
687 if (f2fs_encrypted_inode(inode) &&
688 fscrypt_get_encryption_info(inode))
689 return -EACCES;
691 if (attr->ia_size <= i_size_read(inode)) {
692 truncate_setsize(inode, attr->ia_size);
693 err = f2fs_truncate(inode);
694 if (err)
695 return err;
696 } else {
698 * do not trim all blocks after i_size if target size is
699 * larger than i_size.
701 truncate_setsize(inode, attr->ia_size);
703 /* should convert inline inode here */
704 if (!f2fs_may_inline_data(inode)) {
705 err = f2fs_convert_inline_inode(inode);
706 if (err)
707 return err;
709 inode->i_mtime = inode->i_ctime = current_time(inode);
712 size_changed = true;
715 __setattr_copy(inode, attr);
717 if (attr->ia_valid & ATTR_MODE) {
718 err = posix_acl_chmod(inode, get_inode_mode(inode));
719 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
720 inode->i_mode = F2FS_I(inode)->i_acl_mode;
721 clear_inode_flag(inode, FI_ACL_MODE);
725 /* file size may changed here */
726 f2fs_mark_inode_dirty_sync(inode, size_changed);
728 /* inode change will produce dirty node pages flushed by checkpoint */
729 f2fs_balance_fs(F2FS_I_SB(inode), true);
731 return err;
734 const struct inode_operations f2fs_file_inode_operations = {
735 .getattr = f2fs_getattr,
736 .setattr = f2fs_setattr,
737 .get_acl = f2fs_get_acl,
738 .set_acl = f2fs_set_acl,
739 #ifdef CONFIG_F2FS_FS_XATTR
740 .listxattr = f2fs_listxattr,
741 #endif
742 .fiemap = f2fs_fiemap,
745 static int fill_zero(struct inode *inode, pgoff_t index,
746 loff_t start, loff_t len)
748 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
749 struct page *page;
751 if (!len)
752 return 0;
754 f2fs_balance_fs(sbi, true);
756 f2fs_lock_op(sbi);
757 page = get_new_data_page(inode, NULL, index, false);
758 f2fs_unlock_op(sbi);
760 if (IS_ERR(page))
761 return PTR_ERR(page);
763 f2fs_wait_on_page_writeback(page, DATA, true);
764 zero_user(page, start, len);
765 set_page_dirty(page);
766 f2fs_put_page(page, 1);
767 return 0;
770 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
772 int err;
774 while (pg_start < pg_end) {
775 struct dnode_of_data dn;
776 pgoff_t end_offset, count;
778 set_new_dnode(&dn, inode, NULL, NULL, 0);
779 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
780 if (err) {
781 if (err == -ENOENT) {
782 pg_start++;
783 continue;
785 return err;
788 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
789 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
791 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
793 truncate_data_blocks_range(&dn, count);
794 f2fs_put_dnode(&dn);
796 pg_start += count;
798 return 0;
801 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
803 pgoff_t pg_start, pg_end;
804 loff_t off_start, off_end;
805 int ret;
807 ret = f2fs_convert_inline_inode(inode);
808 if (ret)
809 return ret;
811 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
812 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
814 off_start = offset & (PAGE_SIZE - 1);
815 off_end = (offset + len) & (PAGE_SIZE - 1);
817 if (pg_start == pg_end) {
818 ret = fill_zero(inode, pg_start, off_start,
819 off_end - off_start);
820 if (ret)
821 return ret;
822 } else {
823 if (off_start) {
824 ret = fill_zero(inode, pg_start++, off_start,
825 PAGE_SIZE - off_start);
826 if (ret)
827 return ret;
829 if (off_end) {
830 ret = fill_zero(inode, pg_end, 0, off_end);
831 if (ret)
832 return ret;
835 if (pg_start < pg_end) {
836 struct address_space *mapping = inode->i_mapping;
837 loff_t blk_start, blk_end;
838 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
840 f2fs_balance_fs(sbi, true);
842 blk_start = (loff_t)pg_start << PAGE_SHIFT;
843 blk_end = (loff_t)pg_end << PAGE_SHIFT;
844 truncate_inode_pages_range(mapping, blk_start,
845 blk_end - 1);
847 f2fs_lock_op(sbi);
848 ret = truncate_hole(inode, pg_start, pg_end);
849 f2fs_unlock_op(sbi);
853 return ret;
856 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
857 int *do_replace, pgoff_t off, pgoff_t len)
859 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
860 struct dnode_of_data dn;
861 int ret, done, i;
863 next_dnode:
864 set_new_dnode(&dn, inode, NULL, NULL, 0);
865 ret = get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
866 if (ret && ret != -ENOENT) {
867 return ret;
868 } else if (ret == -ENOENT) {
869 if (dn.max_level == 0)
870 return -ENOENT;
871 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
872 blkaddr += done;
873 do_replace += done;
874 goto next;
877 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
878 dn.ofs_in_node, len);
879 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
880 *blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
881 if (!is_checkpointed_data(sbi, *blkaddr)) {
883 if (test_opt(sbi, LFS)) {
884 f2fs_put_dnode(&dn);
885 return -ENOTSUPP;
888 /* do not invalidate this block address */
889 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
890 *do_replace = 1;
893 f2fs_put_dnode(&dn);
894 next:
895 len -= done;
896 off += done;
897 if (len)
898 goto next_dnode;
899 return 0;
902 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
903 int *do_replace, pgoff_t off, int len)
905 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
906 struct dnode_of_data dn;
907 int ret, i;
909 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
910 if (*do_replace == 0)
911 continue;
913 set_new_dnode(&dn, inode, NULL, NULL, 0);
914 ret = get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
915 if (ret) {
916 dec_valid_block_count(sbi, inode, 1);
917 invalidate_blocks(sbi, *blkaddr);
918 } else {
919 f2fs_update_data_blkaddr(&dn, *blkaddr);
921 f2fs_put_dnode(&dn);
923 return 0;
926 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
927 block_t *blkaddr, int *do_replace,
928 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
930 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
931 pgoff_t i = 0;
932 int ret;
934 while (i < len) {
935 if (blkaddr[i] == NULL_ADDR && !full) {
936 i++;
937 continue;
940 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
941 struct dnode_of_data dn;
942 struct node_info ni;
943 size_t new_size;
944 pgoff_t ilen;
946 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
947 ret = get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
948 if (ret)
949 return ret;
951 get_node_info(sbi, dn.nid, &ni);
952 ilen = min((pgoff_t)
953 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
954 dn.ofs_in_node, len - i);
955 do {
956 dn.data_blkaddr = datablock_addr(dn.node_page,
957 dn.ofs_in_node);
958 truncate_data_blocks_range(&dn, 1);
960 if (do_replace[i]) {
961 f2fs_i_blocks_write(src_inode,
962 1, false);
963 f2fs_i_blocks_write(dst_inode,
964 1, true);
965 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
966 blkaddr[i], ni.version, true, false);
968 do_replace[i] = 0;
970 dn.ofs_in_node++;
971 i++;
972 new_size = (dst + i) << PAGE_SHIFT;
973 if (dst_inode->i_size < new_size)
974 f2fs_i_size_write(dst_inode, new_size);
975 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
977 f2fs_put_dnode(&dn);
978 } else {
979 struct page *psrc, *pdst;
981 psrc = get_lock_data_page(src_inode, src + i, true);
982 if (IS_ERR(psrc))
983 return PTR_ERR(psrc);
984 pdst = get_new_data_page(dst_inode, NULL, dst + i,
985 true);
986 if (IS_ERR(pdst)) {
987 f2fs_put_page(psrc, 1);
988 return PTR_ERR(pdst);
990 f2fs_copy_page(psrc, pdst);
991 set_page_dirty(pdst);
992 f2fs_put_page(pdst, 1);
993 f2fs_put_page(psrc, 1);
995 ret = truncate_hole(src_inode, src + i, src + i + 1);
996 if (ret)
997 return ret;
998 i++;
1001 return 0;
1004 static int __exchange_data_block(struct inode *src_inode,
1005 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1006 pgoff_t len, bool full)
1008 block_t *src_blkaddr;
1009 int *do_replace;
1010 pgoff_t olen;
1011 int ret;
1013 while (len) {
1014 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
1016 src_blkaddr = f2fs_kvzalloc(sizeof(block_t) * olen, GFP_KERNEL);
1017 if (!src_blkaddr)
1018 return -ENOMEM;
1020 do_replace = f2fs_kvzalloc(sizeof(int) * olen, GFP_KERNEL);
1021 if (!do_replace) {
1022 kvfree(src_blkaddr);
1023 return -ENOMEM;
1026 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1027 do_replace, src, olen);
1028 if (ret)
1029 goto roll_back;
1031 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1032 do_replace, src, dst, olen, full);
1033 if (ret)
1034 goto roll_back;
1036 src += olen;
1037 dst += olen;
1038 len -= olen;
1040 kvfree(src_blkaddr);
1041 kvfree(do_replace);
1043 return 0;
1045 roll_back:
1046 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, len);
1047 kvfree(src_blkaddr);
1048 kvfree(do_replace);
1049 return ret;
1052 static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
1054 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1055 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1056 int ret;
1058 f2fs_balance_fs(sbi, true);
1059 f2fs_lock_op(sbi);
1061 f2fs_drop_extent_tree(inode);
1063 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1064 f2fs_unlock_op(sbi);
1065 return ret;
1068 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1070 pgoff_t pg_start, pg_end;
1071 loff_t new_size;
1072 int ret;
1074 if (offset + len >= i_size_read(inode))
1075 return -EINVAL;
1077 /* collapse range should be aligned to block size of f2fs. */
1078 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1079 return -EINVAL;
1081 ret = f2fs_convert_inline_inode(inode);
1082 if (ret)
1083 return ret;
1085 pg_start = offset >> PAGE_SHIFT;
1086 pg_end = (offset + len) >> PAGE_SHIFT;
1088 /* write out all dirty pages from offset */
1089 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1090 if (ret)
1091 return ret;
1093 truncate_pagecache(inode, offset);
1095 ret = f2fs_do_collapse(inode, pg_start, pg_end);
1096 if (ret)
1097 return ret;
1099 /* write out all moved pages, if possible */
1100 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1101 truncate_pagecache(inode, offset);
1103 new_size = i_size_read(inode) - len;
1104 truncate_pagecache(inode, new_size);
1106 ret = truncate_blocks(inode, new_size, true);
1107 if (!ret)
1108 f2fs_i_size_write(inode, new_size);
1110 return ret;
1113 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1114 pgoff_t end)
1116 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1117 pgoff_t index = start;
1118 unsigned int ofs_in_node = dn->ofs_in_node;
1119 blkcnt_t count = 0;
1120 int ret;
1122 for (; index < end; index++, dn->ofs_in_node++) {
1123 if (datablock_addr(dn->node_page, dn->ofs_in_node) == NULL_ADDR)
1124 count++;
1127 dn->ofs_in_node = ofs_in_node;
1128 ret = reserve_new_blocks(dn, count);
1129 if (ret)
1130 return ret;
1132 dn->ofs_in_node = ofs_in_node;
1133 for (index = start; index < end; index++, dn->ofs_in_node++) {
1134 dn->data_blkaddr =
1135 datablock_addr(dn->node_page, dn->ofs_in_node);
1137 * reserve_new_blocks will not guarantee entire block
1138 * allocation.
1140 if (dn->data_blkaddr == NULL_ADDR) {
1141 ret = -ENOSPC;
1142 break;
1144 if (dn->data_blkaddr != NEW_ADDR) {
1145 invalidate_blocks(sbi, dn->data_blkaddr);
1146 dn->data_blkaddr = NEW_ADDR;
1147 set_data_blkaddr(dn);
1151 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1153 return ret;
1156 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1157 int mode)
1159 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1160 struct address_space *mapping = inode->i_mapping;
1161 pgoff_t index, pg_start, pg_end;
1162 loff_t new_size = i_size_read(inode);
1163 loff_t off_start, off_end;
1164 int ret = 0;
1166 ret = inode_newsize_ok(inode, (len + offset));
1167 if (ret)
1168 return ret;
1170 ret = f2fs_convert_inline_inode(inode);
1171 if (ret)
1172 return ret;
1174 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1175 if (ret)
1176 return ret;
1178 truncate_pagecache_range(inode, offset, offset + len - 1);
1180 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1181 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1183 off_start = offset & (PAGE_SIZE - 1);
1184 off_end = (offset + len) & (PAGE_SIZE - 1);
1186 if (pg_start == pg_end) {
1187 ret = fill_zero(inode, pg_start, off_start,
1188 off_end - off_start);
1189 if (ret)
1190 return ret;
1192 if (offset + len > new_size)
1193 new_size = offset + len;
1194 new_size = max_t(loff_t, new_size, offset + len);
1195 } else {
1196 if (off_start) {
1197 ret = fill_zero(inode, pg_start++, off_start,
1198 PAGE_SIZE - off_start);
1199 if (ret)
1200 return ret;
1202 new_size = max_t(loff_t, new_size,
1203 (loff_t)pg_start << PAGE_SHIFT);
1206 for (index = pg_start; index < pg_end;) {
1207 struct dnode_of_data dn;
1208 unsigned int end_offset;
1209 pgoff_t end;
1211 f2fs_lock_op(sbi);
1213 set_new_dnode(&dn, inode, NULL, NULL, 0);
1214 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
1215 if (ret) {
1216 f2fs_unlock_op(sbi);
1217 goto out;
1220 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1221 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1223 ret = f2fs_do_zero_range(&dn, index, end);
1224 f2fs_put_dnode(&dn);
1225 f2fs_unlock_op(sbi);
1227 f2fs_balance_fs(sbi, dn.node_changed);
1229 if (ret)
1230 goto out;
1232 index = end;
1233 new_size = max_t(loff_t, new_size,
1234 (loff_t)index << PAGE_SHIFT);
1237 if (off_end) {
1238 ret = fill_zero(inode, pg_end, 0, off_end);
1239 if (ret)
1240 goto out;
1242 new_size = max_t(loff_t, new_size, offset + len);
1246 out:
1247 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
1248 f2fs_i_size_write(inode, new_size);
1250 return ret;
1253 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1255 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1256 pgoff_t nr, pg_start, pg_end, delta, idx;
1257 loff_t new_size;
1258 int ret = 0;
1260 new_size = i_size_read(inode) + len;
1261 if (new_size > inode->i_sb->s_maxbytes)
1262 return -EFBIG;
1264 if (offset >= i_size_read(inode))
1265 return -EINVAL;
1267 /* insert range should be aligned to block size of f2fs. */
1268 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1269 return -EINVAL;
1271 ret = f2fs_convert_inline_inode(inode);
1272 if (ret)
1273 return ret;
1275 f2fs_balance_fs(sbi, true);
1277 ret = truncate_blocks(inode, i_size_read(inode), true);
1278 if (ret)
1279 return ret;
1281 /* write out all dirty pages from offset */
1282 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1283 if (ret)
1284 return ret;
1286 truncate_pagecache(inode, offset);
1288 pg_start = offset >> PAGE_SHIFT;
1289 pg_end = (offset + len) >> PAGE_SHIFT;
1290 delta = pg_end - pg_start;
1291 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1293 while (!ret && idx > pg_start) {
1294 nr = idx - pg_start;
1295 if (nr > delta)
1296 nr = delta;
1297 idx -= nr;
1299 f2fs_lock_op(sbi);
1300 f2fs_drop_extent_tree(inode);
1302 ret = __exchange_data_block(inode, inode, idx,
1303 idx + delta, nr, false);
1304 f2fs_unlock_op(sbi);
1307 /* write out all moved pages, if possible */
1308 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1309 truncate_pagecache(inode, offset);
1311 if (!ret)
1312 f2fs_i_size_write(inode, new_size);
1313 return ret;
1316 static int expand_inode_data(struct inode *inode, loff_t offset,
1317 loff_t len, int mode)
1319 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1320 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
1321 pgoff_t pg_end;
1322 loff_t new_size = i_size_read(inode);
1323 loff_t off_end;
1324 int err;
1326 err = inode_newsize_ok(inode, (len + offset));
1327 if (err)
1328 return err;
1330 err = f2fs_convert_inline_inode(inode);
1331 if (err)
1332 return err;
1334 f2fs_balance_fs(sbi, true);
1336 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1337 off_end = (offset + len) & (PAGE_SIZE - 1);
1339 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1340 map.m_len = pg_end - map.m_lblk;
1341 if (off_end)
1342 map.m_len++;
1344 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1345 if (err) {
1346 pgoff_t last_off;
1348 if (!map.m_len)
1349 return err;
1351 last_off = map.m_lblk + map.m_len - 1;
1353 /* update new size to the failed position */
1354 new_size = (last_off == pg_end) ? offset + len:
1355 (loff_t)(last_off + 1) << PAGE_SHIFT;
1356 } else {
1357 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1360 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
1361 f2fs_i_size_write(inode, new_size);
1363 return err;
1366 static long f2fs_fallocate(struct file *file, int mode,
1367 loff_t offset, loff_t len)
1369 struct inode *inode = file_inode(file);
1370 long ret = 0;
1372 /* f2fs only support ->fallocate for regular file */
1373 if (!S_ISREG(inode->i_mode))
1374 return -EINVAL;
1376 if (f2fs_encrypted_inode(inode) &&
1377 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1378 return -EOPNOTSUPP;
1380 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1381 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1382 FALLOC_FL_INSERT_RANGE))
1383 return -EOPNOTSUPP;
1385 inode_lock(inode);
1387 if (mode & FALLOC_FL_PUNCH_HOLE) {
1388 if (offset >= inode->i_size)
1389 goto out;
1391 ret = punch_hole(inode, offset, len);
1392 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1393 ret = f2fs_collapse_range(inode, offset, len);
1394 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1395 ret = f2fs_zero_range(inode, offset, len, mode);
1396 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1397 ret = f2fs_insert_range(inode, offset, len);
1398 } else {
1399 ret = expand_inode_data(inode, offset, len, mode);
1402 if (!ret) {
1403 inode->i_mtime = inode->i_ctime = current_time(inode);
1404 f2fs_mark_inode_dirty_sync(inode, false);
1405 if (mode & FALLOC_FL_KEEP_SIZE)
1406 file_set_keep_isize(inode);
1407 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1410 out:
1411 inode_unlock(inode);
1413 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1414 return ret;
1417 static int f2fs_release_file(struct inode *inode, struct file *filp)
1420 * f2fs_relase_file is called at every close calls. So we should
1421 * not drop any inmemory pages by close called by other process.
1423 if (!(filp->f_mode & FMODE_WRITE) ||
1424 atomic_read(&inode->i_writecount) != 1)
1425 return 0;
1427 /* some remained atomic pages should discarded */
1428 if (f2fs_is_atomic_file(inode))
1429 drop_inmem_pages(inode);
1430 if (f2fs_is_volatile_file(inode)) {
1431 clear_inode_flag(inode, FI_VOLATILE_FILE);
1432 set_inode_flag(inode, FI_DROP_CACHE);
1433 filemap_fdatawrite(inode->i_mapping);
1434 clear_inode_flag(inode, FI_DROP_CACHE);
1436 return 0;
1439 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1440 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1442 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
1444 if (S_ISDIR(mode))
1445 return flags;
1446 else if (S_ISREG(mode))
1447 return flags & F2FS_REG_FLMASK;
1448 else
1449 return flags & F2FS_OTHER_FLMASK;
1452 static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1454 struct inode *inode = file_inode(filp);
1455 struct f2fs_inode_info *fi = F2FS_I(inode);
1456 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1457 return put_user(flags, (int __user *)arg);
1460 static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1462 struct inode *inode = file_inode(filp);
1463 struct f2fs_inode_info *fi = F2FS_I(inode);
1464 unsigned int flags;
1465 unsigned int oldflags;
1466 int ret;
1468 if (!inode_owner_or_capable(inode))
1469 return -EACCES;
1471 if (get_user(flags, (int __user *)arg))
1472 return -EFAULT;
1474 ret = mnt_want_write_file(filp);
1475 if (ret)
1476 return ret;
1478 flags = f2fs_mask_flags(inode->i_mode, flags);
1480 inode_lock(inode);
1482 oldflags = fi->i_flags;
1484 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
1485 if (!capable(CAP_LINUX_IMMUTABLE)) {
1486 inode_unlock(inode);
1487 ret = -EPERM;
1488 goto out;
1492 flags = flags & FS_FL_USER_MODIFIABLE;
1493 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
1494 fi->i_flags = flags;
1495 inode_unlock(inode);
1497 inode->i_ctime = current_time(inode);
1498 f2fs_set_inode_flags(inode);
1499 out:
1500 mnt_drop_write_file(filp);
1501 return ret;
1504 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1506 struct inode *inode = file_inode(filp);
1508 return put_user(inode->i_generation, (int __user *)arg);
1511 static int f2fs_ioc_start_atomic_write(struct file *filp)
1513 struct inode *inode = file_inode(filp);
1514 int ret;
1516 if (!inode_owner_or_capable(inode))
1517 return -EACCES;
1519 ret = mnt_want_write_file(filp);
1520 if (ret)
1521 return ret;
1523 inode_lock(inode);
1525 if (f2fs_is_atomic_file(inode))
1526 goto out;
1528 ret = f2fs_convert_inline_inode(inode);
1529 if (ret)
1530 goto out;
1532 set_inode_flag(inode, FI_ATOMIC_FILE);
1533 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1535 if (!get_dirty_pages(inode))
1536 goto out;
1538 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
1539 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
1540 inode->i_ino, get_dirty_pages(inode));
1541 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
1542 if (ret)
1543 clear_inode_flag(inode, FI_ATOMIC_FILE);
1544 out:
1545 inode_unlock(inode);
1546 mnt_drop_write_file(filp);
1547 return ret;
1550 static int f2fs_ioc_commit_atomic_write(struct file *filp)
1552 struct inode *inode = file_inode(filp);
1553 int ret;
1555 if (!inode_owner_or_capable(inode))
1556 return -EACCES;
1558 ret = mnt_want_write_file(filp);
1559 if (ret)
1560 return ret;
1562 inode_lock(inode);
1564 if (f2fs_is_volatile_file(inode))
1565 goto err_out;
1567 if (f2fs_is_atomic_file(inode)) {
1568 clear_inode_flag(inode, FI_ATOMIC_FILE);
1569 ret = commit_inmem_pages(inode);
1570 if (ret) {
1571 set_inode_flag(inode, FI_ATOMIC_FILE);
1572 goto err_out;
1576 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1577 err_out:
1578 inode_unlock(inode);
1579 mnt_drop_write_file(filp);
1580 return ret;
1583 static int f2fs_ioc_start_volatile_write(struct file *filp)
1585 struct inode *inode = file_inode(filp);
1586 int ret;
1588 if (!inode_owner_or_capable(inode))
1589 return -EACCES;
1591 ret = mnt_want_write_file(filp);
1592 if (ret)
1593 return ret;
1595 inode_lock(inode);
1597 if (f2fs_is_volatile_file(inode))
1598 goto out;
1600 ret = f2fs_convert_inline_inode(inode);
1601 if (ret)
1602 goto out;
1604 set_inode_flag(inode, FI_VOLATILE_FILE);
1605 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1606 out:
1607 inode_unlock(inode);
1608 mnt_drop_write_file(filp);
1609 return ret;
1612 static int f2fs_ioc_release_volatile_write(struct file *filp)
1614 struct inode *inode = file_inode(filp);
1615 int ret;
1617 if (!inode_owner_or_capable(inode))
1618 return -EACCES;
1620 ret = mnt_want_write_file(filp);
1621 if (ret)
1622 return ret;
1624 inode_lock(inode);
1626 if (!f2fs_is_volatile_file(inode))
1627 goto out;
1629 if (!f2fs_is_first_block_written(inode)) {
1630 ret = truncate_partial_data_page(inode, 0, true);
1631 goto out;
1634 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1635 out:
1636 inode_unlock(inode);
1637 mnt_drop_write_file(filp);
1638 return ret;
1641 static int f2fs_ioc_abort_volatile_write(struct file *filp)
1643 struct inode *inode = file_inode(filp);
1644 int ret;
1646 if (!inode_owner_or_capable(inode))
1647 return -EACCES;
1649 ret = mnt_want_write_file(filp);
1650 if (ret)
1651 return ret;
1653 inode_lock(inode);
1655 if (f2fs_is_atomic_file(inode))
1656 drop_inmem_pages(inode);
1657 if (f2fs_is_volatile_file(inode)) {
1658 clear_inode_flag(inode, FI_VOLATILE_FILE);
1659 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1662 inode_unlock(inode);
1664 mnt_drop_write_file(filp);
1665 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1666 return ret;
1669 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1671 struct inode *inode = file_inode(filp);
1672 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1673 struct super_block *sb = sbi->sb;
1674 __u32 in;
1675 int ret;
1677 if (!capable(CAP_SYS_ADMIN))
1678 return -EPERM;
1680 if (get_user(in, (__u32 __user *)arg))
1681 return -EFAULT;
1683 ret = mnt_want_write_file(filp);
1684 if (ret)
1685 return ret;
1687 switch (in) {
1688 case F2FS_GOING_DOWN_FULLSYNC:
1689 sb = freeze_bdev(sb->s_bdev);
1690 if (sb && !IS_ERR(sb)) {
1691 f2fs_stop_checkpoint(sbi, false);
1692 thaw_bdev(sb->s_bdev, sb);
1694 break;
1695 case F2FS_GOING_DOWN_METASYNC:
1696 /* do checkpoint only */
1697 f2fs_sync_fs(sb, 1);
1698 f2fs_stop_checkpoint(sbi, false);
1699 break;
1700 case F2FS_GOING_DOWN_NOSYNC:
1701 f2fs_stop_checkpoint(sbi, false);
1702 break;
1703 case F2FS_GOING_DOWN_METAFLUSH:
1704 sync_meta_pages(sbi, META, LONG_MAX);
1705 f2fs_stop_checkpoint(sbi, false);
1706 break;
1707 default:
1708 ret = -EINVAL;
1709 goto out;
1711 f2fs_update_time(sbi, REQ_TIME);
1712 out:
1713 mnt_drop_write_file(filp);
1714 return ret;
1717 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1719 struct inode *inode = file_inode(filp);
1720 struct super_block *sb = inode->i_sb;
1721 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1722 struct fstrim_range range;
1723 int ret;
1725 if (!capable(CAP_SYS_ADMIN))
1726 return -EPERM;
1728 if (!blk_queue_discard(q))
1729 return -EOPNOTSUPP;
1731 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1732 sizeof(range)))
1733 return -EFAULT;
1735 ret = mnt_want_write_file(filp);
1736 if (ret)
1737 return ret;
1739 range.minlen = max((unsigned int)range.minlen,
1740 q->limits.discard_granularity);
1741 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
1742 mnt_drop_write_file(filp);
1743 if (ret < 0)
1744 return ret;
1746 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1747 sizeof(range)))
1748 return -EFAULT;
1749 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1750 return 0;
1753 static bool uuid_is_nonzero(__u8 u[16])
1755 int i;
1757 for (i = 0; i < 16; i++)
1758 if (u[i])
1759 return true;
1760 return false;
1763 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1765 struct inode *inode = file_inode(filp);
1767 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1769 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
1772 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1774 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1777 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1779 struct inode *inode = file_inode(filp);
1780 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1781 int err;
1783 if (!f2fs_sb_has_crypto(inode->i_sb))
1784 return -EOPNOTSUPP;
1786 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1787 goto got_it;
1789 err = mnt_want_write_file(filp);
1790 if (err)
1791 return err;
1793 /* update superblock with uuid */
1794 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1796 err = f2fs_commit_super(sbi, false);
1797 if (err) {
1798 /* undo new data */
1799 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
1800 mnt_drop_write_file(filp);
1801 return err;
1803 mnt_drop_write_file(filp);
1804 got_it:
1805 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1806 16))
1807 return -EFAULT;
1808 return 0;
1811 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
1813 struct inode *inode = file_inode(filp);
1814 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1815 __u32 sync;
1816 int ret;
1818 if (!capable(CAP_SYS_ADMIN))
1819 return -EPERM;
1821 if (get_user(sync, (__u32 __user *)arg))
1822 return -EFAULT;
1824 if (f2fs_readonly(sbi->sb))
1825 return -EROFS;
1827 ret = mnt_want_write_file(filp);
1828 if (ret)
1829 return ret;
1831 if (!sync) {
1832 if (!mutex_trylock(&sbi->gc_mutex)) {
1833 ret = -EBUSY;
1834 goto out;
1836 } else {
1837 mutex_lock(&sbi->gc_mutex);
1840 ret = f2fs_gc(sbi, sync, true);
1841 out:
1842 mnt_drop_write_file(filp);
1843 return ret;
1846 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
1848 struct inode *inode = file_inode(filp);
1849 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1850 int ret;
1852 if (!capable(CAP_SYS_ADMIN))
1853 return -EPERM;
1855 if (f2fs_readonly(sbi->sb))
1856 return -EROFS;
1858 ret = mnt_want_write_file(filp);
1859 if (ret)
1860 return ret;
1862 ret = f2fs_sync_fs(sbi->sb, 1);
1864 mnt_drop_write_file(filp);
1865 return ret;
1868 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
1869 struct file *filp,
1870 struct f2fs_defragment *range)
1872 struct inode *inode = file_inode(filp);
1873 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
1874 struct extent_info ei;
1875 pgoff_t pg_start, pg_end;
1876 unsigned int blk_per_seg = sbi->blocks_per_seg;
1877 unsigned int total = 0, sec_num;
1878 unsigned int pages_per_sec = sbi->segs_per_sec * blk_per_seg;
1879 block_t blk_end = 0;
1880 bool fragmented = false;
1881 int err;
1883 /* if in-place-update policy is enabled, don't waste time here */
1884 if (need_inplace_update(inode))
1885 return -EINVAL;
1887 pg_start = range->start >> PAGE_SHIFT;
1888 pg_end = (range->start + range->len) >> PAGE_SHIFT;
1890 f2fs_balance_fs(sbi, true);
1892 inode_lock(inode);
1894 /* writeback all dirty pages in the range */
1895 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
1896 range->start + range->len - 1);
1897 if (err)
1898 goto out;
1901 * lookup mapping info in extent cache, skip defragmenting if physical
1902 * block addresses are continuous.
1904 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
1905 if (ei.fofs + ei.len >= pg_end)
1906 goto out;
1909 map.m_lblk = pg_start;
1912 * lookup mapping info in dnode page cache, skip defragmenting if all
1913 * physical block addresses are continuous even if there are hole(s)
1914 * in logical blocks.
1916 while (map.m_lblk < pg_end) {
1917 map.m_len = pg_end - map.m_lblk;
1918 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
1919 if (err)
1920 goto out;
1922 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1923 map.m_lblk++;
1924 continue;
1927 if (blk_end && blk_end != map.m_pblk) {
1928 fragmented = true;
1929 break;
1931 blk_end = map.m_pblk + map.m_len;
1933 map.m_lblk += map.m_len;
1936 if (!fragmented)
1937 goto out;
1939 map.m_lblk = pg_start;
1940 map.m_len = pg_end - pg_start;
1942 sec_num = (map.m_len + pages_per_sec - 1) / pages_per_sec;
1945 * make sure there are enough free section for LFS allocation, this can
1946 * avoid defragment running in SSR mode when free section are allocated
1947 * intensively
1949 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
1950 err = -EAGAIN;
1951 goto out;
1954 while (map.m_lblk < pg_end) {
1955 pgoff_t idx;
1956 int cnt = 0;
1958 do_map:
1959 map.m_len = pg_end - map.m_lblk;
1960 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
1961 if (err)
1962 goto clear_out;
1964 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1965 map.m_lblk++;
1966 continue;
1969 set_inode_flag(inode, FI_DO_DEFRAG);
1971 idx = map.m_lblk;
1972 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
1973 struct page *page;
1975 page = get_lock_data_page(inode, idx, true);
1976 if (IS_ERR(page)) {
1977 err = PTR_ERR(page);
1978 goto clear_out;
1981 set_page_dirty(page);
1982 f2fs_put_page(page, 1);
1984 idx++;
1985 cnt++;
1986 total++;
1989 map.m_lblk = idx;
1991 if (idx < pg_end && cnt < blk_per_seg)
1992 goto do_map;
1994 clear_inode_flag(inode, FI_DO_DEFRAG);
1996 err = filemap_fdatawrite(inode->i_mapping);
1997 if (err)
1998 goto out;
2000 clear_out:
2001 clear_inode_flag(inode, FI_DO_DEFRAG);
2002 out:
2003 inode_unlock(inode);
2004 if (!err)
2005 range->len = (u64)total << PAGE_SHIFT;
2006 return err;
2009 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2011 struct inode *inode = file_inode(filp);
2012 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2013 struct f2fs_defragment range;
2014 int err;
2016 if (!capable(CAP_SYS_ADMIN))
2017 return -EPERM;
2019 if (!S_ISREG(inode->i_mode))
2020 return -EINVAL;
2022 err = mnt_want_write_file(filp);
2023 if (err)
2024 return err;
2026 if (f2fs_readonly(sbi->sb)) {
2027 err = -EROFS;
2028 goto out;
2031 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2032 sizeof(range))) {
2033 err = -EFAULT;
2034 goto out;
2037 /* verify alignment of offset & size */
2038 if (range.start & (F2FS_BLKSIZE - 1) ||
2039 range.len & (F2FS_BLKSIZE - 1)) {
2040 err = -EINVAL;
2041 goto out;
2044 err = f2fs_defragment_range(sbi, filp, &range);
2045 f2fs_update_time(sbi, REQ_TIME);
2046 if (err < 0)
2047 goto out;
2049 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2050 sizeof(range)))
2051 err = -EFAULT;
2052 out:
2053 mnt_drop_write_file(filp);
2054 return err;
2057 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2058 struct file *file_out, loff_t pos_out, size_t len)
2060 struct inode *src = file_inode(file_in);
2061 struct inode *dst = file_inode(file_out);
2062 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2063 size_t olen = len, dst_max_i_size = 0;
2064 size_t dst_osize;
2065 int ret;
2067 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2068 src->i_sb != dst->i_sb)
2069 return -EXDEV;
2071 if (unlikely(f2fs_readonly(src->i_sb)))
2072 return -EROFS;
2074 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2075 return -EINVAL;
2077 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2078 return -EOPNOTSUPP;
2080 if (src == dst) {
2081 if (pos_in == pos_out)
2082 return 0;
2083 if (pos_out > pos_in && pos_out < pos_in + len)
2084 return -EINVAL;
2087 inode_lock(src);
2088 if (src != dst) {
2089 if (!inode_trylock(dst)) {
2090 ret = -EBUSY;
2091 goto out;
2095 ret = -EINVAL;
2096 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2097 goto out_unlock;
2098 if (len == 0)
2099 olen = len = src->i_size - pos_in;
2100 if (pos_in + len == src->i_size)
2101 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2102 if (len == 0) {
2103 ret = 0;
2104 goto out_unlock;
2107 dst_osize = dst->i_size;
2108 if (pos_out + olen > dst->i_size)
2109 dst_max_i_size = pos_out + olen;
2111 /* verify the end result is block aligned */
2112 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2113 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2114 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2115 goto out_unlock;
2117 ret = f2fs_convert_inline_inode(src);
2118 if (ret)
2119 goto out_unlock;
2121 ret = f2fs_convert_inline_inode(dst);
2122 if (ret)
2123 goto out_unlock;
2125 /* write out all dirty pages from offset */
2126 ret = filemap_write_and_wait_range(src->i_mapping,
2127 pos_in, pos_in + len);
2128 if (ret)
2129 goto out_unlock;
2131 ret = filemap_write_and_wait_range(dst->i_mapping,
2132 pos_out, pos_out + len);
2133 if (ret)
2134 goto out_unlock;
2136 f2fs_balance_fs(sbi, true);
2137 f2fs_lock_op(sbi);
2138 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2139 pos_out >> F2FS_BLKSIZE_BITS,
2140 len >> F2FS_BLKSIZE_BITS, false);
2142 if (!ret) {
2143 if (dst_max_i_size)
2144 f2fs_i_size_write(dst, dst_max_i_size);
2145 else if (dst_osize != dst->i_size)
2146 f2fs_i_size_write(dst, dst_osize);
2148 f2fs_unlock_op(sbi);
2149 out_unlock:
2150 if (src != dst)
2151 inode_unlock(dst);
2152 out:
2153 inode_unlock(src);
2154 return ret;
2157 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2159 struct f2fs_move_range range;
2160 struct fd dst;
2161 int err;
2163 if (!(filp->f_mode & FMODE_READ) ||
2164 !(filp->f_mode & FMODE_WRITE))
2165 return -EBADF;
2167 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2168 sizeof(range)))
2169 return -EFAULT;
2171 dst = fdget(range.dst_fd);
2172 if (!dst.file)
2173 return -EBADF;
2175 if (!(dst.file->f_mode & FMODE_WRITE)) {
2176 err = -EBADF;
2177 goto err_out;
2180 err = mnt_want_write_file(filp);
2181 if (err)
2182 goto err_out;
2184 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2185 range.pos_out, range.len);
2187 mnt_drop_write_file(filp);
2189 if (copy_to_user((struct f2fs_move_range __user *)arg,
2190 &range, sizeof(range)))
2191 err = -EFAULT;
2192 err_out:
2193 fdput(dst);
2194 return err;
2197 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2199 switch (cmd) {
2200 case F2FS_IOC_GETFLAGS:
2201 return f2fs_ioc_getflags(filp, arg);
2202 case F2FS_IOC_SETFLAGS:
2203 return f2fs_ioc_setflags(filp, arg);
2204 case F2FS_IOC_GETVERSION:
2205 return f2fs_ioc_getversion(filp, arg);
2206 case F2FS_IOC_START_ATOMIC_WRITE:
2207 return f2fs_ioc_start_atomic_write(filp);
2208 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2209 return f2fs_ioc_commit_atomic_write(filp);
2210 case F2FS_IOC_START_VOLATILE_WRITE:
2211 return f2fs_ioc_start_volatile_write(filp);
2212 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2213 return f2fs_ioc_release_volatile_write(filp);
2214 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2215 return f2fs_ioc_abort_volatile_write(filp);
2216 case F2FS_IOC_SHUTDOWN:
2217 return f2fs_ioc_shutdown(filp, arg);
2218 case FITRIM:
2219 return f2fs_ioc_fitrim(filp, arg);
2220 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2221 return f2fs_ioc_set_encryption_policy(filp, arg);
2222 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2223 return f2fs_ioc_get_encryption_policy(filp, arg);
2224 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2225 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
2226 case F2FS_IOC_GARBAGE_COLLECT:
2227 return f2fs_ioc_gc(filp, arg);
2228 case F2FS_IOC_WRITE_CHECKPOINT:
2229 return f2fs_ioc_write_checkpoint(filp, arg);
2230 case F2FS_IOC_DEFRAGMENT:
2231 return f2fs_ioc_defragment(filp, arg);
2232 case F2FS_IOC_MOVE_RANGE:
2233 return f2fs_ioc_move_range(filp, arg);
2234 default:
2235 return -ENOTTY;
2239 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2241 struct file *file = iocb->ki_filp;
2242 struct inode *inode = file_inode(file);
2243 struct blk_plug plug;
2244 ssize_t ret;
2246 if (f2fs_encrypted_inode(inode) &&
2247 !fscrypt_has_encryption_key(inode) &&
2248 fscrypt_get_encryption_info(inode))
2249 return -EACCES;
2251 inode_lock(inode);
2252 ret = generic_write_checks(iocb, from);
2253 if (ret > 0) {
2254 int err = f2fs_preallocate_blocks(iocb, from);
2256 if (err) {
2257 inode_unlock(inode);
2258 return err;
2260 blk_start_plug(&plug);
2261 ret = __generic_file_write_iter(iocb, from);
2262 blk_finish_plug(&plug);
2264 inode_unlock(inode);
2266 if (ret > 0)
2267 ret = generic_write_sync(iocb, ret);
2268 return ret;
2271 #ifdef CONFIG_COMPAT
2272 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2274 switch (cmd) {
2275 case F2FS_IOC32_GETFLAGS:
2276 cmd = F2FS_IOC_GETFLAGS;
2277 break;
2278 case F2FS_IOC32_SETFLAGS:
2279 cmd = F2FS_IOC_SETFLAGS;
2280 break;
2281 case F2FS_IOC32_GETVERSION:
2282 cmd = F2FS_IOC_GETVERSION;
2283 break;
2284 case F2FS_IOC_START_ATOMIC_WRITE:
2285 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2286 case F2FS_IOC_START_VOLATILE_WRITE:
2287 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2288 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2289 case F2FS_IOC_SHUTDOWN:
2290 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2291 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2292 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2293 case F2FS_IOC_GARBAGE_COLLECT:
2294 case F2FS_IOC_WRITE_CHECKPOINT:
2295 case F2FS_IOC_DEFRAGMENT:
2296 break;
2297 case F2FS_IOC_MOVE_RANGE:
2298 break;
2299 default:
2300 return -ENOIOCTLCMD;
2302 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
2304 #endif
2306 const struct file_operations f2fs_file_operations = {
2307 .llseek = f2fs_llseek,
2308 .read_iter = generic_file_read_iter,
2309 .write_iter = f2fs_file_write_iter,
2310 .open = f2fs_file_open,
2311 .release = f2fs_release_file,
2312 .mmap = f2fs_file_mmap,
2313 .fsync = f2fs_sync_file,
2314 .fallocate = f2fs_fallocate,
2315 .unlocked_ioctl = f2fs_ioctl,
2316 #ifdef CONFIG_COMPAT
2317 .compat_ioctl = f2fs_compat_ioctl,
2318 #endif
2319 .splice_read = generic_file_splice_read,
2320 .splice_write = iter_file_splice_write,