xen/x86: don't lose event interrupts
[linux/fpc-iii.git] / fs / f2fs / file.c
blob90d1157a09f9b9b3dcc778d189a99d68c5512154
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/random.h>
25 #include "f2fs.h"
26 #include "node.h"
27 #include "segment.h"
28 #include "xattr.h"
29 #include "acl.h"
30 #include "gc.h"
31 #include "trace.h"
32 #include <trace/events/f2fs.h>
34 static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
35 struct vm_fault *vmf)
37 struct page *page = vmf->page;
38 struct inode *inode = file_inode(vma->vm_file);
39 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
40 struct dnode_of_data dn;
41 int err;
43 sb_start_pagefault(inode->i_sb);
45 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
47 /* block allocation */
48 f2fs_lock_op(sbi);
49 set_new_dnode(&dn, inode, NULL, NULL, 0);
50 err = f2fs_reserve_block(&dn, page->index);
51 if (err) {
52 f2fs_unlock_op(sbi);
53 goto out;
55 f2fs_put_dnode(&dn);
56 f2fs_unlock_op(sbi);
58 f2fs_balance_fs(sbi, dn.node_changed);
60 file_update_time(vma->vm_file);
61 lock_page(page);
62 if (unlikely(page->mapping != inode->i_mapping ||
63 page_offset(page) > i_size_read(inode) ||
64 !PageUptodate(page))) {
65 unlock_page(page);
66 err = -EFAULT;
67 goto out;
71 * check to see if the page is mapped already (no holes)
73 if (PageMappedToDisk(page))
74 goto mapped;
76 /* page is wholly or partially inside EOF */
77 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
78 i_size_read(inode)) {
79 unsigned offset;
80 offset = i_size_read(inode) & ~PAGE_MASK;
81 zero_user_segment(page, offset, PAGE_SIZE);
83 set_page_dirty(page);
84 SetPageUptodate(page);
86 trace_f2fs_vm_page_mkwrite(page, DATA);
87 mapped:
88 /* fill the page */
89 f2fs_wait_on_page_writeback(page, DATA, false);
91 /* wait for GCed encrypted page writeback */
92 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
93 f2fs_wait_on_encrypted_page_writeback(sbi, dn.data_blkaddr);
95 /* if gced page is attached, don't write to cold segment */
96 clear_cold_data(page);
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 (file_enc_name(inode) && need_dentry_mark(sbi, inode->i_ino))
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 fi->i_pino = pino;
175 file_got_pino(inode);
176 up_write(&fi->i_sem);
178 mark_inode_dirty_sync(inode);
179 f2fs_write_inode(inode, NULL);
180 } else {
181 up_write(&fi->i_sem);
185 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
187 struct inode *inode = file->f_mapping->host;
188 struct f2fs_inode_info *fi = F2FS_I(inode);
189 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
190 nid_t ino = inode->i_ino;
191 int ret = 0;
192 bool need_cp = false;
193 struct writeback_control wbc = {
194 .sync_mode = WB_SYNC_ALL,
195 .nr_to_write = LONG_MAX,
196 .for_reclaim = 0,
199 if (unlikely(f2fs_readonly(inode->i_sb)))
200 return 0;
202 trace_f2fs_sync_file_enter(inode);
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(fi, FI_NEED_IPU);
207 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
208 clear_inode_flag(fi, 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) {
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(fi, 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(fi, 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(&fi->i_sem);
242 need_cp = need_do_checkpoint(inode);
243 up_read(&fi->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(fi, FI_APPEND_WRITE);
255 clear_inode_flag(fi, FI_UPDATE_WRITE);
256 goto out;
258 sync_nodes:
259 sync_node_pages(sbi, ino, &wbc);
261 /* if cp_error was enabled, we should avoid infinite loop */
262 if (unlikely(f2fs_cp_error(sbi))) {
263 ret = -EIO;
264 goto out;
267 if (need_inode_block_update(sbi, ino)) {
268 mark_inode_dirty_sync(inode);
269 f2fs_write_inode(inode, NULL);
270 goto sync_nodes;
273 ret = wait_on_node_pages_writeback(sbi, ino);
274 if (ret)
275 goto out;
277 /* once recovery info is written, don't need to tack this */
278 remove_ino_entry(sbi, ino, APPEND_INO);
279 clear_inode_flag(fi, FI_APPEND_WRITE);
280 flush_out:
281 remove_ino_entry(sbi, ino, UPDATE_INO);
282 clear_inode_flag(fi, FI_UPDATE_WRITE);
283 ret = f2fs_issue_flush(sbi);
284 f2fs_update_time(sbi, REQ_TIME);
285 out:
286 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
287 f2fs_trace_ios(NULL, 1);
288 return ret;
291 static pgoff_t __get_first_dirty_index(struct address_space *mapping,
292 pgoff_t pgofs, int whence)
294 struct pagevec pvec;
295 int nr_pages;
297 if (whence != SEEK_DATA)
298 return 0;
300 /* find first dirty page index */
301 pagevec_init(&pvec, 0);
302 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
303 PAGECACHE_TAG_DIRTY, 1);
304 pgofs = nr_pages ? pvec.pages[0]->index : ULONG_MAX;
305 pagevec_release(&pvec);
306 return pgofs;
309 static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
310 int whence)
312 switch (whence) {
313 case SEEK_DATA:
314 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
315 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
316 return true;
317 break;
318 case SEEK_HOLE:
319 if (blkaddr == NULL_ADDR)
320 return true;
321 break;
323 return false;
326 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
328 struct inode *inode = file->f_mapping->host;
329 loff_t maxbytes = inode->i_sb->s_maxbytes;
330 struct dnode_of_data dn;
331 pgoff_t pgofs, end_offset, dirty;
332 loff_t data_ofs = offset;
333 loff_t isize;
334 int err = 0;
336 inode_lock(inode);
338 isize = i_size_read(inode);
339 if (offset >= isize)
340 goto fail;
342 /* handle inline data case */
343 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
344 if (whence == SEEK_HOLE)
345 data_ofs = isize;
346 goto found;
349 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
351 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
353 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
354 set_new_dnode(&dn, inode, NULL, NULL, 0);
355 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA);
356 if (err && err != -ENOENT) {
357 goto fail;
358 } else if (err == -ENOENT) {
359 /* direct node does not exists */
360 if (whence == SEEK_DATA) {
361 pgofs = get_next_page_offset(&dn, pgofs);
362 continue;
363 } else {
364 goto found;
368 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
370 /* find data/hole in dnode block */
371 for (; dn.ofs_in_node < end_offset;
372 dn.ofs_in_node++, pgofs++,
373 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
374 block_t blkaddr;
375 blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node);
377 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
378 f2fs_put_dnode(&dn);
379 goto found;
382 f2fs_put_dnode(&dn);
385 if (whence == SEEK_DATA)
386 goto fail;
387 found:
388 if (whence == SEEK_HOLE && data_ofs > isize)
389 data_ofs = isize;
390 inode_unlock(inode);
391 return vfs_setpos(file, data_ofs, maxbytes);
392 fail:
393 inode_unlock(inode);
394 return -ENXIO;
397 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
399 struct inode *inode = file->f_mapping->host;
400 loff_t maxbytes = inode->i_sb->s_maxbytes;
402 switch (whence) {
403 case SEEK_SET:
404 case SEEK_CUR:
405 case SEEK_END:
406 return generic_file_llseek_size(file, offset, whence,
407 maxbytes, i_size_read(inode));
408 case SEEK_DATA:
409 case SEEK_HOLE:
410 if (offset < 0)
411 return -ENXIO;
412 return f2fs_seek_block(file, offset, whence);
415 return -EINVAL;
418 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
420 struct inode *inode = file_inode(file);
421 int err;
423 if (f2fs_encrypted_inode(inode)) {
424 err = fscrypt_get_encryption_info(inode);
425 if (err)
426 return 0;
427 if (!f2fs_encrypted_inode(inode))
428 return -ENOKEY;
431 /* we don't need to use inline_data strictly */
432 err = f2fs_convert_inline_inode(inode);
433 if (err)
434 return err;
436 file_accessed(file);
437 vma->vm_ops = &f2fs_file_vm_ops;
438 return 0;
441 static int f2fs_file_open(struct inode *inode, struct file *filp)
443 int ret = generic_file_open(inode, filp);
444 struct dentry *dir;
446 if (!ret && f2fs_encrypted_inode(inode)) {
447 ret = fscrypt_get_encryption_info(inode);
448 if (ret)
449 return -EACCES;
450 if (!fscrypt_has_encryption_key(inode))
451 return -ENOKEY;
453 dir = dget_parent(file_dentry(filp));
454 if (f2fs_encrypted_inode(d_inode(dir)) &&
455 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
456 dput(dir);
457 return -EPERM;
459 dput(dir);
460 return ret;
463 int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
465 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
466 struct f2fs_node *raw_node;
467 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
468 __le32 *addr;
470 raw_node = F2FS_NODE(dn->node_page);
471 addr = blkaddr_in_node(raw_node) + ofs;
473 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
474 block_t blkaddr = le32_to_cpu(*addr);
475 if (blkaddr == NULL_ADDR)
476 continue;
478 dn->data_blkaddr = NULL_ADDR;
479 set_data_blkaddr(dn);
480 invalidate_blocks(sbi, blkaddr);
481 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
482 clear_inode_flag(F2FS_I(dn->inode),
483 FI_FIRST_BLOCK_WRITTEN);
484 nr_free++;
487 if (nr_free) {
488 pgoff_t fofs;
490 * once we invalidate valid blkaddr in range [ofs, ofs + count],
491 * we will invalidate all blkaddr in the whole range.
493 fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
494 dn->inode) + ofs;
495 f2fs_update_extent_cache_range(dn, fofs, 0, len);
496 dec_valid_block_count(sbi, dn->inode, nr_free);
497 sync_inode_page(dn);
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 = f2fs_grab_cache_page(mapping, index, false);
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 (lock)
559 f2fs_lock_op(sbi);
561 ipage = get_node_page(sbi, inode->i_ino);
562 if (IS_ERR(ipage)) {
563 err = PTR_ERR(ipage);
564 goto out;
567 if (f2fs_has_inline_data(inode)) {
568 if (truncate_inline_inode(ipage, from))
569 set_page_dirty(ipage);
570 f2fs_put_page(ipage, 1);
571 truncate_page = true;
572 goto out;
575 set_new_dnode(&dn, inode, ipage, NULL, 0);
576 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE);
577 if (err) {
578 if (err == -ENOENT)
579 goto free_next;
580 goto out;
583 count = ADDRS_PER_PAGE(dn.node_page, inode);
585 count -= dn.ofs_in_node;
586 f2fs_bug_on(sbi, count < 0);
588 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
589 truncate_data_blocks_range(&dn, count);
590 free_from += count;
593 f2fs_put_dnode(&dn);
594 free_next:
595 err = truncate_inode_blocks(inode, free_from);
596 out:
597 if (lock)
598 f2fs_unlock_op(sbi);
600 /* lastly zero out the first data page */
601 if (!err)
602 err = truncate_partial_data_page(inode, from, truncate_page);
604 trace_f2fs_truncate_blocks_exit(inode, err);
605 return err;
608 int f2fs_truncate(struct inode *inode, bool lock)
610 int err;
612 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
613 S_ISLNK(inode->i_mode)))
614 return 0;
616 trace_f2fs_truncate(inode);
618 /* we should check inline_data size */
619 if (!f2fs_may_inline_data(inode)) {
620 err = f2fs_convert_inline_inode(inode);
621 if (err)
622 return err;
625 err = truncate_blocks(inode, i_size_read(inode), lock);
626 if (err)
627 return err;
629 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
630 mark_inode_dirty(inode);
631 return 0;
634 int f2fs_getattr(struct vfsmount *mnt,
635 struct dentry *dentry, struct kstat *stat)
637 struct inode *inode = d_inode(dentry);
638 generic_fillattr(inode, stat);
639 stat->blocks <<= 3;
640 return 0;
643 #ifdef CONFIG_F2FS_FS_POSIX_ACL
644 static void __setattr_copy(struct inode *inode, const struct iattr *attr)
646 struct f2fs_inode_info *fi = F2FS_I(inode);
647 unsigned int ia_valid = attr->ia_valid;
649 if (ia_valid & ATTR_UID)
650 inode->i_uid = attr->ia_uid;
651 if (ia_valid & ATTR_GID)
652 inode->i_gid = attr->ia_gid;
653 if (ia_valid & ATTR_ATIME)
654 inode->i_atime = timespec_trunc(attr->ia_atime,
655 inode->i_sb->s_time_gran);
656 if (ia_valid & ATTR_MTIME)
657 inode->i_mtime = timespec_trunc(attr->ia_mtime,
658 inode->i_sb->s_time_gran);
659 if (ia_valid & ATTR_CTIME)
660 inode->i_ctime = timespec_trunc(attr->ia_ctime,
661 inode->i_sb->s_time_gran);
662 if (ia_valid & ATTR_MODE) {
663 umode_t mode = attr->ia_mode;
665 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
666 mode &= ~S_ISGID;
667 set_acl_inode(fi, mode);
670 #else
671 #define __setattr_copy setattr_copy
672 #endif
674 int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
676 struct inode *inode = d_inode(dentry);
677 struct f2fs_inode_info *fi = F2FS_I(inode);
678 int err;
680 err = inode_change_ok(inode, attr);
681 if (err)
682 return err;
684 if (attr->ia_valid & ATTR_SIZE) {
685 if (f2fs_encrypted_inode(inode) &&
686 fscrypt_get_encryption_info(inode))
687 return -EACCES;
689 if (attr->ia_size <= i_size_read(inode)) {
690 truncate_setsize(inode, attr->ia_size);
691 err = f2fs_truncate(inode, true);
692 if (err)
693 return err;
694 f2fs_balance_fs(F2FS_I_SB(inode), true);
695 } else {
697 * do not trim all blocks after i_size if target size is
698 * larger than i_size.
700 truncate_setsize(inode, attr->ia_size);
702 /* should convert inline inode here */
703 if (!f2fs_may_inline_data(inode)) {
704 err = f2fs_convert_inline_inode(inode);
705 if (err)
706 return err;
708 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
712 __setattr_copy(inode, attr);
714 if (attr->ia_valid & ATTR_MODE) {
715 err = posix_acl_chmod(inode, get_inode_mode(inode));
716 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
717 inode->i_mode = fi->i_acl_mode;
718 clear_inode_flag(fi, FI_ACL_MODE);
722 mark_inode_dirty(inode);
723 return err;
726 const struct inode_operations f2fs_file_inode_operations = {
727 .getattr = f2fs_getattr,
728 .setattr = f2fs_setattr,
729 .get_acl = f2fs_get_acl,
730 .set_acl = f2fs_set_acl,
731 #ifdef CONFIG_F2FS_FS_XATTR
732 .setxattr = generic_setxattr,
733 .getxattr = generic_getxattr,
734 .listxattr = f2fs_listxattr,
735 .removexattr = generic_removexattr,
736 #endif
737 .fiemap = f2fs_fiemap,
740 static int fill_zero(struct inode *inode, pgoff_t index,
741 loff_t start, loff_t len)
743 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
744 struct page *page;
746 if (!len)
747 return 0;
749 f2fs_balance_fs(sbi, true);
751 f2fs_lock_op(sbi);
752 page = get_new_data_page(inode, NULL, index, false);
753 f2fs_unlock_op(sbi);
755 if (IS_ERR(page))
756 return PTR_ERR(page);
758 f2fs_wait_on_page_writeback(page, DATA, true);
759 zero_user(page, start, len);
760 set_page_dirty(page);
761 f2fs_put_page(page, 1);
762 return 0;
765 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
767 int err;
769 while (pg_start < pg_end) {
770 struct dnode_of_data dn;
771 pgoff_t end_offset, count;
773 set_new_dnode(&dn, inode, NULL, NULL, 0);
774 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
775 if (err) {
776 if (err == -ENOENT) {
777 pg_start++;
778 continue;
780 return err;
783 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
784 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
786 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
788 truncate_data_blocks_range(&dn, count);
789 f2fs_put_dnode(&dn);
791 pg_start += count;
793 return 0;
796 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
798 pgoff_t pg_start, pg_end;
799 loff_t off_start, off_end;
800 int ret;
802 ret = f2fs_convert_inline_inode(inode);
803 if (ret)
804 return ret;
806 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
807 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
809 off_start = offset & (PAGE_SIZE - 1);
810 off_end = (offset + len) & (PAGE_SIZE - 1);
812 if (pg_start == pg_end) {
813 ret = fill_zero(inode, pg_start, off_start,
814 off_end - off_start);
815 if (ret)
816 return ret;
817 } else {
818 if (off_start) {
819 ret = fill_zero(inode, pg_start++, off_start,
820 PAGE_SIZE - off_start);
821 if (ret)
822 return ret;
824 if (off_end) {
825 ret = fill_zero(inode, pg_end, 0, off_end);
826 if (ret)
827 return ret;
830 if (pg_start < pg_end) {
831 struct address_space *mapping = inode->i_mapping;
832 loff_t blk_start, blk_end;
833 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
835 f2fs_balance_fs(sbi, true);
837 blk_start = (loff_t)pg_start << PAGE_SHIFT;
838 blk_end = (loff_t)pg_end << PAGE_SHIFT;
839 truncate_inode_pages_range(mapping, blk_start,
840 blk_end - 1);
842 f2fs_lock_op(sbi);
843 ret = truncate_hole(inode, pg_start, pg_end);
844 f2fs_unlock_op(sbi);
848 return ret;
851 static int __exchange_data_block(struct inode *inode, pgoff_t src,
852 pgoff_t dst, bool full)
854 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
855 struct dnode_of_data dn;
856 block_t new_addr;
857 bool do_replace = false;
858 int ret;
860 set_new_dnode(&dn, inode, NULL, NULL, 0);
861 ret = get_dnode_of_data(&dn, src, LOOKUP_NODE_RA);
862 if (ret && ret != -ENOENT) {
863 return ret;
864 } else if (ret == -ENOENT) {
865 new_addr = NULL_ADDR;
866 } else {
867 new_addr = dn.data_blkaddr;
868 if (!is_checkpointed_data(sbi, new_addr)) {
869 /* do not invalidate this block address */
870 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
871 do_replace = true;
873 f2fs_put_dnode(&dn);
876 if (new_addr == NULL_ADDR)
877 return full ? truncate_hole(inode, dst, dst + 1) : 0;
879 if (do_replace) {
880 struct page *ipage = get_node_page(sbi, inode->i_ino);
881 struct node_info ni;
883 if (IS_ERR(ipage)) {
884 ret = PTR_ERR(ipage);
885 goto err_out;
888 set_new_dnode(&dn, inode, ipage, NULL, 0);
889 ret = f2fs_reserve_block(&dn, dst);
890 if (ret)
891 goto err_out;
893 truncate_data_blocks_range(&dn, 1);
895 get_node_info(sbi, dn.nid, &ni);
896 f2fs_replace_block(sbi, &dn, dn.data_blkaddr, new_addr,
897 ni.version, true, false);
898 f2fs_put_dnode(&dn);
899 } else {
900 struct page *psrc, *pdst;
902 psrc = get_lock_data_page(inode, src, true);
903 if (IS_ERR(psrc))
904 return PTR_ERR(psrc);
905 pdst = get_new_data_page(inode, NULL, dst, true);
906 if (IS_ERR(pdst)) {
907 f2fs_put_page(psrc, 1);
908 return PTR_ERR(pdst);
910 f2fs_copy_page(psrc, pdst);
911 set_page_dirty(pdst);
912 f2fs_put_page(pdst, 1);
913 f2fs_put_page(psrc, 1);
915 return truncate_hole(inode, src, src + 1);
917 return 0;
919 err_out:
920 if (!get_dnode_of_data(&dn, src, LOOKUP_NODE)) {
921 f2fs_update_data_blkaddr(&dn, new_addr);
922 f2fs_put_dnode(&dn);
924 return ret;
927 static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
929 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
930 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
931 int ret = 0;
933 for (; end < nrpages; start++, end++) {
934 f2fs_balance_fs(sbi, true);
935 f2fs_lock_op(sbi);
936 ret = __exchange_data_block(inode, end, start, true);
937 f2fs_unlock_op(sbi);
938 if (ret)
939 break;
941 return ret;
944 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
946 pgoff_t pg_start, pg_end;
947 loff_t new_size;
948 int ret;
950 if (offset + len >= i_size_read(inode))
951 return -EINVAL;
953 /* collapse range should be aligned to block size of f2fs. */
954 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
955 return -EINVAL;
957 ret = f2fs_convert_inline_inode(inode);
958 if (ret)
959 return ret;
961 pg_start = offset >> PAGE_SHIFT;
962 pg_end = (offset + len) >> PAGE_SHIFT;
964 /* write out all dirty pages from offset */
965 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
966 if (ret)
967 return ret;
969 truncate_pagecache(inode, offset);
971 ret = f2fs_do_collapse(inode, pg_start, pg_end);
972 if (ret)
973 return ret;
975 /* write out all moved pages, if possible */
976 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
977 truncate_pagecache(inode, offset);
979 new_size = i_size_read(inode) - len;
980 truncate_pagecache(inode, new_size);
982 ret = truncate_blocks(inode, new_size, true);
983 if (!ret)
984 i_size_write(inode, new_size);
986 return ret;
989 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
990 int mode)
992 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
993 struct address_space *mapping = inode->i_mapping;
994 pgoff_t index, pg_start, pg_end;
995 loff_t new_size = i_size_read(inode);
996 loff_t off_start, off_end;
997 int ret = 0;
999 ret = inode_newsize_ok(inode, (len + offset));
1000 if (ret)
1001 return ret;
1003 ret = f2fs_convert_inline_inode(inode);
1004 if (ret)
1005 return ret;
1007 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1008 if (ret)
1009 return ret;
1011 truncate_pagecache_range(inode, offset, offset + len - 1);
1013 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1014 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1016 off_start = offset & (PAGE_SIZE - 1);
1017 off_end = (offset + len) & (PAGE_SIZE - 1);
1019 if (pg_start == pg_end) {
1020 ret = fill_zero(inode, pg_start, off_start,
1021 off_end - off_start);
1022 if (ret)
1023 return ret;
1025 if (offset + len > new_size)
1026 new_size = offset + len;
1027 new_size = max_t(loff_t, new_size, offset + len);
1028 } else {
1029 if (off_start) {
1030 ret = fill_zero(inode, pg_start++, off_start,
1031 PAGE_SIZE - off_start);
1032 if (ret)
1033 return ret;
1035 new_size = max_t(loff_t, new_size,
1036 (loff_t)pg_start << PAGE_SHIFT);
1039 for (index = pg_start; index < pg_end; index++) {
1040 struct dnode_of_data dn;
1041 struct page *ipage;
1043 f2fs_lock_op(sbi);
1045 ipage = get_node_page(sbi, inode->i_ino);
1046 if (IS_ERR(ipage)) {
1047 ret = PTR_ERR(ipage);
1048 f2fs_unlock_op(sbi);
1049 goto out;
1052 set_new_dnode(&dn, inode, ipage, NULL, 0);
1053 ret = f2fs_reserve_block(&dn, index);
1054 if (ret) {
1055 f2fs_unlock_op(sbi);
1056 goto out;
1059 if (dn.data_blkaddr != NEW_ADDR) {
1060 invalidate_blocks(sbi, dn.data_blkaddr);
1061 f2fs_update_data_blkaddr(&dn, NEW_ADDR);
1063 f2fs_put_dnode(&dn);
1064 f2fs_unlock_op(sbi);
1066 new_size = max_t(loff_t, new_size,
1067 (loff_t)(index + 1) << PAGE_SHIFT);
1070 if (off_end) {
1071 ret = fill_zero(inode, pg_end, 0, off_end);
1072 if (ret)
1073 goto out;
1075 new_size = max_t(loff_t, new_size, offset + len);
1079 out:
1080 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size) {
1081 i_size_write(inode, new_size);
1082 mark_inode_dirty(inode);
1083 update_inode_page(inode);
1086 return ret;
1089 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1091 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1092 pgoff_t pg_start, pg_end, delta, nrpages, idx;
1093 loff_t new_size;
1094 int ret = 0;
1096 new_size = i_size_read(inode) + len;
1097 if (new_size > inode->i_sb->s_maxbytes)
1098 return -EFBIG;
1100 if (offset >= i_size_read(inode))
1101 return -EINVAL;
1103 /* insert range should be aligned to block size of f2fs. */
1104 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1105 return -EINVAL;
1107 ret = f2fs_convert_inline_inode(inode);
1108 if (ret)
1109 return ret;
1111 f2fs_balance_fs(sbi, true);
1113 ret = truncate_blocks(inode, i_size_read(inode), true);
1114 if (ret)
1115 return ret;
1117 /* write out all dirty pages from offset */
1118 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1119 if (ret)
1120 return ret;
1122 truncate_pagecache(inode, offset);
1124 pg_start = offset >> PAGE_SHIFT;
1125 pg_end = (offset + len) >> PAGE_SHIFT;
1126 delta = pg_end - pg_start;
1127 nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1129 for (idx = nrpages - 1; idx >= pg_start && idx != -1; idx--) {
1130 f2fs_lock_op(sbi);
1131 ret = __exchange_data_block(inode, idx, idx + delta, false);
1132 f2fs_unlock_op(sbi);
1133 if (ret)
1134 break;
1137 /* write out all moved pages, if possible */
1138 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1139 truncate_pagecache(inode, offset);
1141 if (!ret)
1142 i_size_write(inode, new_size);
1143 return ret;
1146 static int expand_inode_data(struct inode *inode, loff_t offset,
1147 loff_t len, int mode)
1149 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1150 pgoff_t index, pg_start, pg_end;
1151 loff_t new_size = i_size_read(inode);
1152 loff_t off_start, off_end;
1153 int ret = 0;
1155 ret = inode_newsize_ok(inode, (len + offset));
1156 if (ret)
1157 return ret;
1159 ret = f2fs_convert_inline_inode(inode);
1160 if (ret)
1161 return ret;
1163 f2fs_balance_fs(sbi, true);
1165 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1166 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1168 off_start = offset & (PAGE_SIZE - 1);
1169 off_end = (offset + len) & (PAGE_SIZE - 1);
1171 f2fs_lock_op(sbi);
1173 for (index = pg_start; index <= pg_end; index++) {
1174 struct dnode_of_data dn;
1176 if (index == pg_end && !off_end)
1177 goto noalloc;
1179 set_new_dnode(&dn, inode, NULL, NULL, 0);
1180 ret = f2fs_reserve_block(&dn, index);
1181 if (ret)
1182 break;
1183 noalloc:
1184 if (pg_start == pg_end)
1185 new_size = offset + len;
1186 else if (index == pg_start && off_start)
1187 new_size = (loff_t)(index + 1) << PAGE_SHIFT;
1188 else if (index == pg_end)
1189 new_size = ((loff_t)index << PAGE_SHIFT) +
1190 off_end;
1191 else
1192 new_size += PAGE_SIZE;
1195 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
1196 i_size_read(inode) < new_size) {
1197 i_size_write(inode, new_size);
1198 mark_inode_dirty(inode);
1199 update_inode_page(inode);
1201 f2fs_unlock_op(sbi);
1203 return ret;
1206 static long f2fs_fallocate(struct file *file, int mode,
1207 loff_t offset, loff_t len)
1209 struct inode *inode = file_inode(file);
1210 long ret = 0;
1212 /* f2fs only support ->fallocate for regular file */
1213 if (!S_ISREG(inode->i_mode))
1214 return -EINVAL;
1216 if (f2fs_encrypted_inode(inode) &&
1217 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1218 return -EOPNOTSUPP;
1220 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1221 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1222 FALLOC_FL_INSERT_RANGE))
1223 return -EOPNOTSUPP;
1225 inode_lock(inode);
1227 if (mode & FALLOC_FL_PUNCH_HOLE) {
1228 if (offset >= inode->i_size)
1229 goto out;
1231 ret = punch_hole(inode, offset, len);
1232 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1233 ret = f2fs_collapse_range(inode, offset, len);
1234 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1235 ret = f2fs_zero_range(inode, offset, len, mode);
1236 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1237 ret = f2fs_insert_range(inode, offset, len);
1238 } else {
1239 ret = expand_inode_data(inode, offset, len, mode);
1242 if (!ret) {
1243 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1244 mark_inode_dirty(inode);
1245 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1248 out:
1249 inode_unlock(inode);
1251 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1252 return ret;
1255 static int f2fs_release_file(struct inode *inode, struct file *filp)
1257 /* some remained atomic pages should discarded */
1258 if (f2fs_is_atomic_file(inode))
1259 drop_inmem_pages(inode);
1260 if (f2fs_is_volatile_file(inode)) {
1261 set_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
1262 filemap_fdatawrite(inode->i_mapping);
1263 clear_inode_flag(F2FS_I(inode), FI_DROP_CACHE);
1265 return 0;
1268 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
1269 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
1271 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
1273 if (S_ISDIR(mode))
1274 return flags;
1275 else if (S_ISREG(mode))
1276 return flags & F2FS_REG_FLMASK;
1277 else
1278 return flags & F2FS_OTHER_FLMASK;
1281 static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1283 struct inode *inode = file_inode(filp);
1284 struct f2fs_inode_info *fi = F2FS_I(inode);
1285 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1286 return put_user(flags, (int __user *)arg);
1289 static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1291 struct inode *inode = file_inode(filp);
1292 struct f2fs_inode_info *fi = F2FS_I(inode);
1293 unsigned int flags = fi->i_flags & FS_FL_USER_VISIBLE;
1294 unsigned int oldflags;
1295 int ret;
1297 ret = mnt_want_write_file(filp);
1298 if (ret)
1299 return ret;
1301 if (!inode_owner_or_capable(inode)) {
1302 ret = -EACCES;
1303 goto out;
1306 if (get_user(flags, (int __user *)arg)) {
1307 ret = -EFAULT;
1308 goto out;
1311 flags = f2fs_mask_flags(inode->i_mode, flags);
1313 inode_lock(inode);
1315 oldflags = fi->i_flags;
1317 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
1318 if (!capable(CAP_LINUX_IMMUTABLE)) {
1319 inode_unlock(inode);
1320 ret = -EPERM;
1321 goto out;
1325 flags = flags & FS_FL_USER_MODIFIABLE;
1326 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
1327 fi->i_flags = flags;
1328 inode_unlock(inode);
1330 f2fs_set_inode_flags(inode);
1331 inode->i_ctime = CURRENT_TIME;
1332 mark_inode_dirty(inode);
1333 out:
1334 mnt_drop_write_file(filp);
1335 return ret;
1338 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1340 struct inode *inode = file_inode(filp);
1342 return put_user(inode->i_generation, (int __user *)arg);
1345 static int f2fs_ioc_start_atomic_write(struct file *filp)
1347 struct inode *inode = file_inode(filp);
1348 int ret;
1350 if (!inode_owner_or_capable(inode))
1351 return -EACCES;
1353 if (f2fs_is_atomic_file(inode))
1354 return 0;
1356 ret = f2fs_convert_inline_inode(inode);
1357 if (ret)
1358 return ret;
1360 set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
1361 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1363 return 0;
1366 static int f2fs_ioc_commit_atomic_write(struct file *filp)
1368 struct inode *inode = file_inode(filp);
1369 int ret;
1371 if (!inode_owner_or_capable(inode))
1372 return -EACCES;
1374 if (f2fs_is_volatile_file(inode))
1375 return 0;
1377 ret = mnt_want_write_file(filp);
1378 if (ret)
1379 return ret;
1381 if (f2fs_is_atomic_file(inode)) {
1382 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
1383 ret = commit_inmem_pages(inode);
1384 if (ret) {
1385 set_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
1386 goto err_out;
1390 ret = f2fs_sync_file(filp, 0, LLONG_MAX, 0);
1391 err_out:
1392 mnt_drop_write_file(filp);
1393 return ret;
1396 static int f2fs_ioc_start_volatile_write(struct file *filp)
1398 struct inode *inode = file_inode(filp);
1399 int ret;
1401 if (!inode_owner_or_capable(inode))
1402 return -EACCES;
1404 if (f2fs_is_volatile_file(inode))
1405 return 0;
1407 ret = f2fs_convert_inline_inode(inode);
1408 if (ret)
1409 return ret;
1411 set_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
1412 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1413 return 0;
1416 static int f2fs_ioc_release_volatile_write(struct file *filp)
1418 struct inode *inode = file_inode(filp);
1420 if (!inode_owner_or_capable(inode))
1421 return -EACCES;
1423 if (!f2fs_is_volatile_file(inode))
1424 return 0;
1426 if (!f2fs_is_first_block_written(inode))
1427 return truncate_partial_data_page(inode, 0, true);
1429 return punch_hole(inode, 0, F2FS_BLKSIZE);
1432 static int f2fs_ioc_abort_volatile_write(struct file *filp)
1434 struct inode *inode = file_inode(filp);
1435 int ret;
1437 if (!inode_owner_or_capable(inode))
1438 return -EACCES;
1440 ret = mnt_want_write_file(filp);
1441 if (ret)
1442 return ret;
1444 if (f2fs_is_atomic_file(inode)) {
1445 clear_inode_flag(F2FS_I(inode), FI_ATOMIC_FILE);
1446 drop_inmem_pages(inode);
1448 if (f2fs_is_volatile_file(inode)) {
1449 clear_inode_flag(F2FS_I(inode), FI_VOLATILE_FILE);
1450 ret = f2fs_sync_file(filp, 0, LLONG_MAX, 0);
1453 mnt_drop_write_file(filp);
1454 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1455 return ret;
1458 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1460 struct inode *inode = file_inode(filp);
1461 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1462 struct super_block *sb = sbi->sb;
1463 __u32 in;
1465 if (!capable(CAP_SYS_ADMIN))
1466 return -EPERM;
1468 if (get_user(in, (__u32 __user *)arg))
1469 return -EFAULT;
1471 switch (in) {
1472 case F2FS_GOING_DOWN_FULLSYNC:
1473 sb = freeze_bdev(sb->s_bdev);
1474 if (sb && !IS_ERR(sb)) {
1475 f2fs_stop_checkpoint(sbi);
1476 thaw_bdev(sb->s_bdev, sb);
1478 break;
1479 case F2FS_GOING_DOWN_METASYNC:
1480 /* do checkpoint only */
1481 f2fs_sync_fs(sb, 1);
1482 f2fs_stop_checkpoint(sbi);
1483 break;
1484 case F2FS_GOING_DOWN_NOSYNC:
1485 f2fs_stop_checkpoint(sbi);
1486 break;
1487 case F2FS_GOING_DOWN_METAFLUSH:
1488 sync_meta_pages(sbi, META, LONG_MAX);
1489 f2fs_stop_checkpoint(sbi);
1490 break;
1491 default:
1492 return -EINVAL;
1494 f2fs_update_time(sbi, REQ_TIME);
1495 return 0;
1498 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1500 struct inode *inode = file_inode(filp);
1501 struct super_block *sb = inode->i_sb;
1502 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1503 struct fstrim_range range;
1504 int ret;
1506 if (!capable(CAP_SYS_ADMIN))
1507 return -EPERM;
1509 if (!blk_queue_discard(q))
1510 return -EOPNOTSUPP;
1512 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1513 sizeof(range)))
1514 return -EFAULT;
1516 range.minlen = max((unsigned int)range.minlen,
1517 q->limits.discard_granularity);
1518 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
1519 if (ret < 0)
1520 return ret;
1522 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1523 sizeof(range)))
1524 return -EFAULT;
1525 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1526 return 0;
1529 static bool uuid_is_nonzero(__u8 u[16])
1531 int i;
1533 for (i = 0; i < 16; i++)
1534 if (u[i])
1535 return true;
1536 return false;
1539 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1541 struct fscrypt_policy policy;
1542 struct inode *inode = file_inode(filp);
1544 if (copy_from_user(&policy, (struct fscrypt_policy __user *)arg,
1545 sizeof(policy)))
1546 return -EFAULT;
1548 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1549 return fscrypt_process_policy(inode, &policy);
1552 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1554 struct fscrypt_policy policy;
1555 struct inode *inode = file_inode(filp);
1556 int err;
1558 err = fscrypt_get_policy(inode, &policy);
1559 if (err)
1560 return err;
1562 if (copy_to_user((struct fscrypt_policy __user *)arg, &policy, sizeof(policy)))
1563 return -EFAULT;
1564 return 0;
1567 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1569 struct inode *inode = file_inode(filp);
1570 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1571 int err;
1573 if (!f2fs_sb_has_crypto(inode->i_sb))
1574 return -EOPNOTSUPP;
1576 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1577 goto got_it;
1579 err = mnt_want_write_file(filp);
1580 if (err)
1581 return err;
1583 /* update superblock with uuid */
1584 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1586 err = f2fs_commit_super(sbi, false);
1587 if (err) {
1588 /* undo new data */
1589 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
1590 mnt_drop_write_file(filp);
1591 return err;
1593 mnt_drop_write_file(filp);
1594 got_it:
1595 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1596 16))
1597 return -EFAULT;
1598 return 0;
1601 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
1603 struct inode *inode = file_inode(filp);
1604 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1605 __u32 sync;
1607 if (!capable(CAP_SYS_ADMIN))
1608 return -EPERM;
1610 if (get_user(sync, (__u32 __user *)arg))
1611 return -EFAULT;
1613 if (f2fs_readonly(sbi->sb))
1614 return -EROFS;
1616 if (!sync) {
1617 if (!mutex_trylock(&sbi->gc_mutex))
1618 return -EBUSY;
1619 } else {
1620 mutex_lock(&sbi->gc_mutex);
1623 return f2fs_gc(sbi, sync);
1626 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
1628 struct inode *inode = file_inode(filp);
1629 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1631 if (!capable(CAP_SYS_ADMIN))
1632 return -EPERM;
1634 if (f2fs_readonly(sbi->sb))
1635 return -EROFS;
1637 return f2fs_sync_fs(sbi->sb, 1);
1640 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
1641 struct file *filp,
1642 struct f2fs_defragment *range)
1644 struct inode *inode = file_inode(filp);
1645 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
1646 struct extent_info ei;
1647 pgoff_t pg_start, pg_end;
1648 unsigned int blk_per_seg = sbi->blocks_per_seg;
1649 unsigned int total = 0, sec_num;
1650 unsigned int pages_per_sec = sbi->segs_per_sec * blk_per_seg;
1651 block_t blk_end = 0;
1652 bool fragmented = false;
1653 int err;
1655 /* if in-place-update policy is enabled, don't waste time here */
1656 if (need_inplace_update(inode))
1657 return -EINVAL;
1659 pg_start = range->start >> PAGE_SHIFT;
1660 pg_end = (range->start + range->len) >> PAGE_SHIFT;
1662 f2fs_balance_fs(sbi, true);
1664 inode_lock(inode);
1666 /* writeback all dirty pages in the range */
1667 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
1668 range->start + range->len - 1);
1669 if (err)
1670 goto out;
1673 * lookup mapping info in extent cache, skip defragmenting if physical
1674 * block addresses are continuous.
1676 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
1677 if (ei.fofs + ei.len >= pg_end)
1678 goto out;
1681 map.m_lblk = pg_start;
1684 * lookup mapping info in dnode page cache, skip defragmenting if all
1685 * physical block addresses are continuous even if there are hole(s)
1686 * in logical blocks.
1688 while (map.m_lblk < pg_end) {
1689 map.m_len = pg_end - map.m_lblk;
1690 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
1691 if (err)
1692 goto out;
1694 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1695 map.m_lblk++;
1696 continue;
1699 if (blk_end && blk_end != map.m_pblk) {
1700 fragmented = true;
1701 break;
1703 blk_end = map.m_pblk + map.m_len;
1705 map.m_lblk += map.m_len;
1708 if (!fragmented)
1709 goto out;
1711 map.m_lblk = pg_start;
1712 map.m_len = pg_end - pg_start;
1714 sec_num = (map.m_len + pages_per_sec - 1) / pages_per_sec;
1717 * make sure there are enough free section for LFS allocation, this can
1718 * avoid defragment running in SSR mode when free section are allocated
1719 * intensively
1721 if (has_not_enough_free_secs(sbi, sec_num)) {
1722 err = -EAGAIN;
1723 goto out;
1726 while (map.m_lblk < pg_end) {
1727 pgoff_t idx;
1728 int cnt = 0;
1730 do_map:
1731 map.m_len = pg_end - map.m_lblk;
1732 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_READ);
1733 if (err)
1734 goto clear_out;
1736 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
1737 map.m_lblk++;
1738 continue;
1741 set_inode_flag(F2FS_I(inode), FI_DO_DEFRAG);
1743 idx = map.m_lblk;
1744 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
1745 struct page *page;
1747 page = get_lock_data_page(inode, idx, true);
1748 if (IS_ERR(page)) {
1749 err = PTR_ERR(page);
1750 goto clear_out;
1753 set_page_dirty(page);
1754 f2fs_put_page(page, 1);
1756 idx++;
1757 cnt++;
1758 total++;
1761 map.m_lblk = idx;
1763 if (idx < pg_end && cnt < blk_per_seg)
1764 goto do_map;
1766 clear_inode_flag(F2FS_I(inode), FI_DO_DEFRAG);
1768 err = filemap_fdatawrite(inode->i_mapping);
1769 if (err)
1770 goto out;
1772 clear_out:
1773 clear_inode_flag(F2FS_I(inode), FI_DO_DEFRAG);
1774 out:
1775 inode_unlock(inode);
1776 if (!err)
1777 range->len = (u64)total << PAGE_SHIFT;
1778 return err;
1781 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
1783 struct inode *inode = file_inode(filp);
1784 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1785 struct f2fs_defragment range;
1786 int err;
1788 if (!capable(CAP_SYS_ADMIN))
1789 return -EPERM;
1791 if (!S_ISREG(inode->i_mode))
1792 return -EINVAL;
1794 err = mnt_want_write_file(filp);
1795 if (err)
1796 return err;
1798 if (f2fs_readonly(sbi->sb)) {
1799 err = -EROFS;
1800 goto out;
1803 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
1804 sizeof(range))) {
1805 err = -EFAULT;
1806 goto out;
1809 /* verify alignment of offset & size */
1810 if (range.start & (F2FS_BLKSIZE - 1) ||
1811 range.len & (F2FS_BLKSIZE - 1)) {
1812 err = -EINVAL;
1813 goto out;
1816 err = f2fs_defragment_range(sbi, filp, &range);
1817 f2fs_update_time(sbi, REQ_TIME);
1818 if (err < 0)
1819 goto out;
1821 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
1822 sizeof(range)))
1823 err = -EFAULT;
1824 out:
1825 mnt_drop_write_file(filp);
1826 return err;
1829 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1831 switch (cmd) {
1832 case F2FS_IOC_GETFLAGS:
1833 return f2fs_ioc_getflags(filp, arg);
1834 case F2FS_IOC_SETFLAGS:
1835 return f2fs_ioc_setflags(filp, arg);
1836 case F2FS_IOC_GETVERSION:
1837 return f2fs_ioc_getversion(filp, arg);
1838 case F2FS_IOC_START_ATOMIC_WRITE:
1839 return f2fs_ioc_start_atomic_write(filp);
1840 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
1841 return f2fs_ioc_commit_atomic_write(filp);
1842 case F2FS_IOC_START_VOLATILE_WRITE:
1843 return f2fs_ioc_start_volatile_write(filp);
1844 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
1845 return f2fs_ioc_release_volatile_write(filp);
1846 case F2FS_IOC_ABORT_VOLATILE_WRITE:
1847 return f2fs_ioc_abort_volatile_write(filp);
1848 case F2FS_IOC_SHUTDOWN:
1849 return f2fs_ioc_shutdown(filp, arg);
1850 case FITRIM:
1851 return f2fs_ioc_fitrim(filp, arg);
1852 case F2FS_IOC_SET_ENCRYPTION_POLICY:
1853 return f2fs_ioc_set_encryption_policy(filp, arg);
1854 case F2FS_IOC_GET_ENCRYPTION_POLICY:
1855 return f2fs_ioc_get_encryption_policy(filp, arg);
1856 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
1857 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
1858 case F2FS_IOC_GARBAGE_COLLECT:
1859 return f2fs_ioc_gc(filp, arg);
1860 case F2FS_IOC_WRITE_CHECKPOINT:
1861 return f2fs_ioc_write_checkpoint(filp, arg);
1862 case F2FS_IOC_DEFRAGMENT:
1863 return f2fs_ioc_defragment(filp, arg);
1864 default:
1865 return -ENOTTY;
1869 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1871 struct file *file = iocb->ki_filp;
1872 struct inode *inode = file_inode(file);
1873 ssize_t ret;
1875 if (f2fs_encrypted_inode(inode) &&
1876 !fscrypt_has_encryption_key(inode) &&
1877 fscrypt_get_encryption_info(inode))
1878 return -EACCES;
1880 inode_lock(inode);
1881 ret = generic_write_checks(iocb, from);
1882 if (ret > 0) {
1883 ret = f2fs_preallocate_blocks(iocb, from);
1884 if (!ret)
1885 ret = __generic_file_write_iter(iocb, from);
1887 inode_unlock(inode);
1889 if (ret > 0) {
1890 ssize_t err;
1892 err = generic_write_sync(file, iocb->ki_pos - ret, ret);
1893 if (err < 0)
1894 ret = err;
1896 return ret;
1899 #ifdef CONFIG_COMPAT
1900 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1902 switch (cmd) {
1903 case F2FS_IOC32_GETFLAGS:
1904 cmd = F2FS_IOC_GETFLAGS;
1905 break;
1906 case F2FS_IOC32_SETFLAGS:
1907 cmd = F2FS_IOC_SETFLAGS;
1908 break;
1909 case F2FS_IOC32_GETVERSION:
1910 cmd = F2FS_IOC_GETVERSION;
1911 break;
1912 case F2FS_IOC_START_ATOMIC_WRITE:
1913 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
1914 case F2FS_IOC_START_VOLATILE_WRITE:
1915 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
1916 case F2FS_IOC_ABORT_VOLATILE_WRITE:
1917 case F2FS_IOC_SHUTDOWN:
1918 case F2FS_IOC_SET_ENCRYPTION_POLICY:
1919 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
1920 case F2FS_IOC_GET_ENCRYPTION_POLICY:
1921 case F2FS_IOC_GARBAGE_COLLECT:
1922 case F2FS_IOC_WRITE_CHECKPOINT:
1923 case F2FS_IOC_DEFRAGMENT:
1924 break;
1925 default:
1926 return -ENOIOCTLCMD;
1928 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
1930 #endif
1932 const struct file_operations f2fs_file_operations = {
1933 .llseek = f2fs_llseek,
1934 .read_iter = generic_file_read_iter,
1935 .write_iter = f2fs_file_write_iter,
1936 .open = f2fs_file_open,
1937 .release = f2fs_release_file,
1938 .mmap = f2fs_file_mmap,
1939 .fsync = f2fs_sync_file,
1940 .fallocate = f2fs_fallocate,
1941 .unlocked_ioctl = f2fs_ioctl,
1942 #ifdef CONFIG_COMPAT
1943 .compat_ioctl = f2fs_compat_ioctl,
1944 #endif
1945 .splice_read = generic_file_splice_read,
1946 .splice_write = iter_file_splice_write,