MIPS: jazz: fix 64bit build
[linux-stable.git] / fs / f2fs / file.c
blob5f549bc4e0979650c20ebaa14b852e57077309c3
1 /*
2 * fs/f2fs/file.c
4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
13 #include <linux/stat.h>
14 #include <linux/buffer_head.h>
15 #include <linux/writeback.h>
16 #include <linux/blkdev.h>
17 #include <linux/falloc.h>
18 #include <linux/types.h>
19 #include <linux/compat.h>
20 #include <linux/uaccess.h>
21 #include <linux/mount.h>
22 #include <linux/pagevec.h>
23 #include <linux/uio.h>
24 #include <linux/uuid.h>
25 #include <linux/file.h>
27 #include "f2fs.h"
28 #include "node.h"
29 #include "segment.h"
30 #include "xattr.h"
31 #include "acl.h"
32 #include "gc.h"
33 #include "trace.h"
34 #include <trace/events/f2fs.h>
36 static int f2fs_filemap_fault(struct vm_fault *vmf)
38 struct inode *inode = file_inode(vmf->vma->vm_file);
39 int err;
41 down_read(&F2FS_I(inode)->i_mmap_sem);
42 err = filemap_fault(vmf);
43 up_read(&F2FS_I(inode)->i_mmap_sem);
45 return err;
48 static int f2fs_vm_page_mkwrite(struct vm_fault *vmf)
50 struct page *page = vmf->page;
51 struct inode *inode = file_inode(vmf->vma->vm_file);
52 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
53 struct dnode_of_data dn;
54 int err;
56 sb_start_pagefault(inode->i_sb);
58 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
60 /* block allocation */
61 f2fs_lock_op(sbi);
62 set_new_dnode(&dn, inode, NULL, NULL, 0);
63 err = f2fs_reserve_block(&dn, page->index);
64 if (err) {
65 f2fs_unlock_op(sbi);
66 goto out;
68 f2fs_put_dnode(&dn);
69 f2fs_unlock_op(sbi);
71 f2fs_balance_fs(sbi, dn.node_changed);
73 file_update_time(vmf->vma->vm_file);
74 down_read(&F2FS_I(inode)->i_mmap_sem);
75 lock_page(page);
76 if (unlikely(page->mapping != inode->i_mapping ||
77 page_offset(page) > i_size_read(inode) ||
78 !PageUptodate(page))) {
79 unlock_page(page);
80 err = -EFAULT;
81 goto out_sem;
85 * check to see if the page is mapped already (no holes)
87 if (PageMappedToDisk(page))
88 goto mapped;
90 /* page is wholly or partially inside EOF */
91 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
92 i_size_read(inode)) {
93 unsigned offset;
94 offset = i_size_read(inode) & ~PAGE_MASK;
95 zero_user_segment(page, offset, PAGE_SIZE);
97 set_page_dirty(page);
98 if (!PageUptodate(page))
99 SetPageUptodate(page);
101 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
103 trace_f2fs_vm_page_mkwrite(page, DATA);
104 mapped:
105 /* fill the page */
106 f2fs_wait_on_page_writeback(page, DATA, false);
108 /* wait for GCed encrypted page writeback */
109 if (f2fs_encrypted_file(inode))
110 f2fs_wait_on_block_writeback(sbi, dn.data_blkaddr);
112 out_sem:
113 up_read(&F2FS_I(inode)->i_mmap_sem);
114 out:
115 sb_end_pagefault(inode->i_sb);
116 f2fs_update_time(sbi, REQ_TIME);
117 return block_page_mkwrite_return(err);
120 static const struct vm_operations_struct f2fs_file_vm_ops = {
121 .fault = f2fs_filemap_fault,
122 .map_pages = filemap_map_pages,
123 .page_mkwrite = f2fs_vm_page_mkwrite,
126 static int get_parent_ino(struct inode *inode, nid_t *pino)
128 struct dentry *dentry;
130 inode = igrab(inode);
131 dentry = d_find_any_alias(inode);
132 iput(inode);
133 if (!dentry)
134 return 0;
136 *pino = parent_ino(dentry);
137 dput(dentry);
138 return 1;
141 static inline bool need_do_checkpoint(struct inode *inode)
143 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
144 bool need_cp = false;
146 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
147 need_cp = true;
148 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
149 need_cp = true;
150 else if (file_wrong_pino(inode))
151 need_cp = true;
152 else if (!space_for_roll_forward(sbi))
153 need_cp = true;
154 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
155 need_cp = true;
156 else if (test_opt(sbi, FASTBOOT))
157 need_cp = true;
158 else if (sbi->active_logs == 2)
159 need_cp = true;
161 return need_cp;
164 static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
166 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
167 bool ret = false;
168 /* But we need to avoid that there are some inode updates */
169 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
170 ret = true;
171 f2fs_put_page(i, 0);
172 return ret;
175 static void try_to_fix_pino(struct inode *inode)
177 struct f2fs_inode_info *fi = F2FS_I(inode);
178 nid_t pino;
180 down_write(&fi->i_sem);
181 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
182 get_parent_ino(inode, &pino)) {
183 f2fs_i_pino_write(inode, pino);
184 file_got_pino(inode);
186 up_write(&fi->i_sem);
189 static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
190 int datasync, bool atomic)
192 struct inode *inode = file->f_mapping->host;
193 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
194 nid_t ino = inode->i_ino;
195 int ret = 0;
196 bool need_cp = false;
197 struct writeback_control wbc = {
198 .sync_mode = WB_SYNC_ALL,
199 .nr_to_write = LONG_MAX,
200 .for_reclaim = 0,
203 if (unlikely(f2fs_readonly(inode->i_sb)))
204 return 0;
206 trace_f2fs_sync_file_enter(inode);
208 if (S_ISDIR(inode->i_mode))
209 goto go_write;
211 /* if fdatasync is triggered, let's do in-place-update */
212 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
213 set_inode_flag(inode, FI_NEED_IPU);
214 ret = file_write_and_wait_range(file, start, end);
215 clear_inode_flag(inode, FI_NEED_IPU);
217 if (ret) {
218 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
219 return ret;
222 /* if the inode is dirty, let's recover all the time */
223 if (!f2fs_skip_inode_update(inode, datasync)) {
224 f2fs_write_inode(inode, NULL);
225 goto go_write;
229 * if there is no written data, don't waste time to write recovery info.
231 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
232 !exist_written_data(sbi, ino, APPEND_INO)) {
234 /* it may call write_inode just prior to fsync */
235 if (need_inode_page_update(sbi, ino))
236 goto go_write;
238 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
239 exist_written_data(sbi, ino, UPDATE_INO))
240 goto flush_out;
241 goto out;
243 go_write:
245 * Both of fdatasync() and fsync() are able to be recovered from
246 * sudden-power-off.
248 down_read(&F2FS_I(inode)->i_sem);
249 need_cp = need_do_checkpoint(inode);
250 up_read(&F2FS_I(inode)->i_sem);
252 if (need_cp) {
253 /* all the dirty node pages should be flushed for POR */
254 ret = f2fs_sync_fs(inode->i_sb, 1);
257 * We've secured consistency through sync_fs. Following pino
258 * will be used only for fsynced inodes after checkpoint.
260 try_to_fix_pino(inode);
261 clear_inode_flag(inode, FI_APPEND_WRITE);
262 clear_inode_flag(inode, FI_UPDATE_WRITE);
263 goto out;
265 sync_nodes:
266 ret = fsync_node_pages(sbi, inode, &wbc, atomic);
267 if (ret)
268 goto out;
270 /* if cp_error was enabled, we should avoid infinite loop */
271 if (unlikely(f2fs_cp_error(sbi))) {
272 ret = -EIO;
273 goto out;
276 if (need_inode_block_update(sbi, ino)) {
277 f2fs_mark_inode_dirty_sync(inode, true);
278 f2fs_write_inode(inode, NULL);
279 goto sync_nodes;
283 * If it's atomic_write, it's just fine to keep write ordering. So
284 * here we don't need to wait for node write completion, since we use
285 * node chain which serializes node blocks. If one of node writes are
286 * reordered, we can see simply broken chain, resulting in stopping
287 * roll-forward recovery. It means we'll recover all or none node blocks
288 * given fsync mark.
290 if (!atomic) {
291 ret = wait_on_node_pages_writeback(sbi, ino);
292 if (ret)
293 goto out;
296 /* once recovery info is written, don't need to tack this */
297 remove_ino_entry(sbi, ino, APPEND_INO);
298 clear_inode_flag(inode, FI_APPEND_WRITE);
299 flush_out:
300 remove_ino_entry(sbi, ino, UPDATE_INO);
301 clear_inode_flag(inode, FI_UPDATE_WRITE);
302 if (!atomic)
303 ret = f2fs_issue_flush(sbi);
304 f2fs_update_time(sbi, REQ_TIME);
305 out:
306 trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret);
307 f2fs_trace_ios(NULL, 1);
308 return ret;
311 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
313 return f2fs_do_sync_file(file, start, end, datasync, false);
316 static pgoff_t __get_first_dirty_index(struct address_space *mapping,
317 pgoff_t pgofs, int whence)
319 struct pagevec pvec;
320 int nr_pages;
322 if (whence != SEEK_DATA)
323 return 0;
325 /* find first dirty page index */
326 pagevec_init(&pvec, 0);
327 nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs,
328 PAGECACHE_TAG_DIRTY, 1);
329 pgofs = nr_pages ? pvec.pages[0]->index : ULONG_MAX;
330 pagevec_release(&pvec);
331 return pgofs;
334 static bool __found_offset(struct f2fs_sb_info *sbi, block_t blkaddr,
335 pgoff_t dirty, pgoff_t pgofs, int whence)
337 switch (whence) {
338 case SEEK_DATA:
339 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
340 is_valid_data_blkaddr(sbi, blkaddr))
341 return true;
342 break;
343 case SEEK_HOLE:
344 if (blkaddr == NULL_ADDR)
345 return true;
346 break;
348 return false;
351 static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
353 struct inode *inode = file->f_mapping->host;
354 loff_t maxbytes = inode->i_sb->s_maxbytes;
355 struct dnode_of_data dn;
356 pgoff_t pgofs, end_offset, dirty;
357 loff_t data_ofs = offset;
358 loff_t isize;
359 int err = 0;
361 inode_lock(inode);
363 isize = i_size_read(inode);
364 if (offset >= isize)
365 goto fail;
367 /* handle inline data case */
368 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
369 if (whence == SEEK_HOLE)
370 data_ofs = isize;
371 goto found;
374 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
376 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
378 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
379 set_new_dnode(&dn, inode, NULL, NULL, 0);
380 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
381 if (err && err != -ENOENT) {
382 goto fail;
383 } else if (err == -ENOENT) {
384 /* direct node does not exists */
385 if (whence == SEEK_DATA) {
386 pgofs = get_next_page_offset(&dn, pgofs);
387 continue;
388 } else {
389 goto found;
393 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
395 /* find data/hole in dnode block */
396 for (; dn.ofs_in_node < end_offset;
397 dn.ofs_in_node++, pgofs++,
398 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
399 block_t blkaddr;
400 blkaddr = datablock_addr(dn.inode,
401 dn.node_page, dn.ofs_in_node);
403 if (__is_valid_data_blkaddr(blkaddr) &&
404 !f2fs_is_valid_blkaddr(F2FS_I_SB(inode),
405 blkaddr, DATA_GENERIC)) {
406 f2fs_put_dnode(&dn);
407 goto fail;
410 if (__found_offset(F2FS_I_SB(inode), blkaddr, dirty,
411 pgofs, whence)) {
412 f2fs_put_dnode(&dn);
413 goto found;
416 f2fs_put_dnode(&dn);
419 if (whence == SEEK_DATA)
420 goto fail;
421 found:
422 if (whence == SEEK_HOLE && data_ofs > isize)
423 data_ofs = isize;
424 inode_unlock(inode);
425 return vfs_setpos(file, data_ofs, maxbytes);
426 fail:
427 inode_unlock(inode);
428 return -ENXIO;
431 static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
433 struct inode *inode = file->f_mapping->host;
434 loff_t maxbytes = inode->i_sb->s_maxbytes;
436 switch (whence) {
437 case SEEK_SET:
438 case SEEK_CUR:
439 case SEEK_END:
440 return generic_file_llseek_size(file, offset, whence,
441 maxbytes, i_size_read(inode));
442 case SEEK_DATA:
443 case SEEK_HOLE:
444 if (offset < 0)
445 return -ENXIO;
446 return f2fs_seek_block(file, offset, whence);
449 return -EINVAL;
452 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
454 struct inode *inode = file_inode(file);
455 int err;
457 /* we don't need to use inline_data strictly */
458 err = f2fs_convert_inline_inode(inode);
459 if (err)
460 return err;
462 file_accessed(file);
463 vma->vm_ops = &f2fs_file_vm_ops;
464 return 0;
467 static int f2fs_file_open(struct inode *inode, struct file *filp)
469 struct dentry *dir;
471 if (f2fs_encrypted_inode(inode)) {
472 int ret = fscrypt_get_encryption_info(inode);
473 if (ret)
474 return -EACCES;
475 if (!fscrypt_has_encryption_key(inode))
476 return -ENOKEY;
478 dir = dget_parent(file_dentry(filp));
479 if (f2fs_encrypted_inode(d_inode(dir)) &&
480 !fscrypt_has_permitted_context(d_inode(dir), inode)) {
481 dput(dir);
482 return -EPERM;
484 dput(dir);
485 return dquot_file_open(inode, filp);
488 int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
490 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
491 struct f2fs_node *raw_node;
492 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
493 __le32 *addr;
494 int base = 0;
496 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
497 base = get_extra_isize(dn->inode);
499 raw_node = F2FS_NODE(dn->node_page);
500 addr = blkaddr_in_node(raw_node) + base + ofs;
502 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
503 block_t blkaddr = le32_to_cpu(*addr);
504 if (blkaddr == NULL_ADDR)
505 continue;
507 dn->data_blkaddr = NULL_ADDR;
508 set_data_blkaddr(dn);
510 if (__is_valid_data_blkaddr(blkaddr) &&
511 !f2fs_is_valid_blkaddr(sbi, blkaddr, DATA_GENERIC))
512 continue;
514 invalidate_blocks(sbi, blkaddr);
515 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
516 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
517 nr_free++;
520 if (nr_free) {
521 pgoff_t fofs;
523 * once we invalidate valid blkaddr in range [ofs, ofs + count],
524 * we will invalidate all blkaddr in the whole range.
526 fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
527 dn->inode) + ofs;
528 f2fs_update_extent_cache_range(dn, fofs, 0, len);
529 dec_valid_block_count(sbi, dn->inode, nr_free);
531 dn->ofs_in_node = ofs;
533 f2fs_update_time(sbi, REQ_TIME);
534 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
535 dn->ofs_in_node, nr_free);
536 return nr_free;
539 void truncate_data_blocks(struct dnode_of_data *dn)
541 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
544 static int truncate_partial_data_page(struct inode *inode, u64 from,
545 bool cache_only)
547 unsigned offset = from & (PAGE_SIZE - 1);
548 pgoff_t index = from >> PAGE_SHIFT;
549 struct address_space *mapping = inode->i_mapping;
550 struct page *page;
552 if (!offset && !cache_only)
553 return 0;
555 if (cache_only) {
556 page = find_lock_page(mapping, index);
557 if (page && PageUptodate(page))
558 goto truncate_out;
559 f2fs_put_page(page, 1);
560 return 0;
563 page = get_lock_data_page(inode, index, true);
564 if (IS_ERR(page))
565 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
566 truncate_out:
567 f2fs_wait_on_page_writeback(page, DATA, true);
568 zero_user(page, offset, PAGE_SIZE - offset);
570 /* An encrypted inode should have a key and truncate the last page. */
571 f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode));
572 if (!cache_only)
573 set_page_dirty(page);
574 f2fs_put_page(page, 1);
575 return 0;
578 int truncate_blocks(struct inode *inode, u64 from, bool lock)
580 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
581 unsigned int blocksize = inode->i_sb->s_blocksize;
582 struct dnode_of_data dn;
583 pgoff_t free_from;
584 int count = 0, err = 0;
585 struct page *ipage;
586 bool truncate_page = false;
588 trace_f2fs_truncate_blocks_enter(inode, from);
590 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
592 if (free_from >= sbi->max_file_blocks)
593 goto free_partial;
595 if (lock)
596 f2fs_lock_op(sbi);
598 ipage = get_node_page(sbi, inode->i_ino);
599 if (IS_ERR(ipage)) {
600 err = PTR_ERR(ipage);
601 goto out;
604 if (f2fs_has_inline_data(inode)) {
605 truncate_inline_inode(inode, ipage, from);
606 f2fs_put_page(ipage, 1);
607 truncate_page = true;
608 goto out;
611 set_new_dnode(&dn, inode, ipage, NULL, 0);
612 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
613 if (err) {
614 if (err == -ENOENT)
615 goto free_next;
616 goto out;
619 count = ADDRS_PER_PAGE(dn.node_page, inode);
621 count -= dn.ofs_in_node;
622 f2fs_bug_on(sbi, count < 0);
624 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
625 truncate_data_blocks_range(&dn, count);
626 free_from += count;
629 f2fs_put_dnode(&dn);
630 free_next:
631 err = truncate_inode_blocks(inode, free_from);
632 out:
633 if (lock)
634 f2fs_unlock_op(sbi);
635 free_partial:
636 /* lastly zero out the first data page */
637 if (!err)
638 err = truncate_partial_data_page(inode, from, truncate_page);
640 trace_f2fs_truncate_blocks_exit(inode, err);
641 return err;
644 int f2fs_truncate(struct inode *inode)
646 int err;
648 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
649 S_ISLNK(inode->i_mode)))
650 return 0;
652 trace_f2fs_truncate(inode);
654 #ifdef CONFIG_F2FS_FAULT_INJECTION
655 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
656 f2fs_show_injection_info(FAULT_TRUNCATE);
657 return -EIO;
659 #endif
660 /* we should check inline_data size */
661 if (!f2fs_may_inline_data(inode)) {
662 err = f2fs_convert_inline_inode(inode);
663 if (err)
664 return err;
667 err = truncate_blocks(inode, i_size_read(inode), true);
668 if (err)
669 return err;
671 inode->i_mtime = inode->i_ctime = current_time(inode);
672 f2fs_mark_inode_dirty_sync(inode, false);
673 return 0;
676 int f2fs_getattr(const struct path *path, struct kstat *stat,
677 u32 request_mask, unsigned int query_flags)
679 struct inode *inode = d_inode(path->dentry);
680 struct f2fs_inode_info *fi = F2FS_I(inode);
681 unsigned int flags;
683 flags = fi->i_flags & (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL);
684 if (flags & FS_APPEND_FL)
685 stat->attributes |= STATX_ATTR_APPEND;
686 if (flags & FS_COMPR_FL)
687 stat->attributes |= STATX_ATTR_COMPRESSED;
688 if (f2fs_encrypted_inode(inode))
689 stat->attributes |= STATX_ATTR_ENCRYPTED;
690 if (flags & FS_IMMUTABLE_FL)
691 stat->attributes |= STATX_ATTR_IMMUTABLE;
692 if (flags & FS_NODUMP_FL)
693 stat->attributes |= STATX_ATTR_NODUMP;
695 stat->attributes_mask |= (STATX_ATTR_APPEND |
696 STATX_ATTR_COMPRESSED |
697 STATX_ATTR_ENCRYPTED |
698 STATX_ATTR_IMMUTABLE |
699 STATX_ATTR_NODUMP);
701 generic_fillattr(inode, stat);
703 /* we need to show initial sectors used for inline_data/dentries */
704 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
705 f2fs_has_inline_dentry(inode))
706 stat->blocks += (stat->size + 511) >> 9;
708 return 0;
711 #ifdef CONFIG_F2FS_FS_POSIX_ACL
712 static void __setattr_copy(struct inode *inode, const struct iattr *attr)
714 unsigned int ia_valid = attr->ia_valid;
716 if (ia_valid & ATTR_UID)
717 inode->i_uid = attr->ia_uid;
718 if (ia_valid & ATTR_GID)
719 inode->i_gid = attr->ia_gid;
720 if (ia_valid & ATTR_ATIME)
721 inode->i_atime = timespec_trunc(attr->ia_atime,
722 inode->i_sb->s_time_gran);
723 if (ia_valid & ATTR_MTIME)
724 inode->i_mtime = timespec_trunc(attr->ia_mtime,
725 inode->i_sb->s_time_gran);
726 if (ia_valid & ATTR_CTIME)
727 inode->i_ctime = timespec_trunc(attr->ia_ctime,
728 inode->i_sb->s_time_gran);
729 if (ia_valid & ATTR_MODE) {
730 umode_t mode = attr->ia_mode;
732 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
733 mode &= ~S_ISGID;
734 set_acl_inode(inode, mode);
737 #else
738 #define __setattr_copy setattr_copy
739 #endif
741 int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
743 struct inode *inode = d_inode(dentry);
744 int err;
745 bool size_changed = false;
747 err = setattr_prepare(dentry, attr);
748 if (err)
749 return err;
751 if (is_quota_modification(inode, attr)) {
752 err = dquot_initialize(inode);
753 if (err)
754 return err;
756 if ((attr->ia_valid & ATTR_UID &&
757 !uid_eq(attr->ia_uid, inode->i_uid)) ||
758 (attr->ia_valid & ATTR_GID &&
759 !gid_eq(attr->ia_gid, inode->i_gid))) {
760 err = dquot_transfer(inode, attr);
761 if (err)
762 return err;
765 if (attr->ia_valid & ATTR_SIZE) {
766 if (f2fs_encrypted_inode(inode)) {
767 err = fscrypt_get_encryption_info(inode);
768 if (err)
769 return err;
770 if (!fscrypt_has_encryption_key(inode))
771 return -ENOKEY;
774 if (attr->ia_size <= i_size_read(inode)) {
775 down_write(&F2FS_I(inode)->i_mmap_sem);
776 truncate_setsize(inode, attr->ia_size);
777 err = f2fs_truncate(inode);
778 up_write(&F2FS_I(inode)->i_mmap_sem);
779 if (err)
780 return err;
781 } else {
783 * do not trim all blocks after i_size if target size is
784 * larger than i_size.
786 down_write(&F2FS_I(inode)->i_mmap_sem);
787 truncate_setsize(inode, attr->ia_size);
788 up_write(&F2FS_I(inode)->i_mmap_sem);
790 /* should convert inline inode here */
791 if (!f2fs_may_inline_data(inode)) {
792 err = f2fs_convert_inline_inode(inode);
793 if (err)
794 return err;
796 inode->i_mtime = inode->i_ctime = current_time(inode);
799 size_changed = true;
802 __setattr_copy(inode, attr);
804 if (attr->ia_valid & ATTR_MODE) {
805 err = posix_acl_chmod(inode, get_inode_mode(inode));
806 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
807 inode->i_mode = F2FS_I(inode)->i_acl_mode;
808 clear_inode_flag(inode, FI_ACL_MODE);
812 /* file size may changed here */
813 f2fs_mark_inode_dirty_sync(inode, size_changed);
815 /* inode change will produce dirty node pages flushed by checkpoint */
816 f2fs_balance_fs(F2FS_I_SB(inode), true);
818 return err;
821 const struct inode_operations f2fs_file_inode_operations = {
822 .getattr = f2fs_getattr,
823 .setattr = f2fs_setattr,
824 .get_acl = f2fs_get_acl,
825 .set_acl = f2fs_set_acl,
826 #ifdef CONFIG_F2FS_FS_XATTR
827 .listxattr = f2fs_listxattr,
828 #endif
829 .fiemap = f2fs_fiemap,
832 static int fill_zero(struct inode *inode, pgoff_t index,
833 loff_t start, loff_t len)
835 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
836 struct page *page;
838 if (!len)
839 return 0;
841 f2fs_balance_fs(sbi, true);
843 f2fs_lock_op(sbi);
844 page = get_new_data_page(inode, NULL, index, false);
845 f2fs_unlock_op(sbi);
847 if (IS_ERR(page))
848 return PTR_ERR(page);
850 f2fs_wait_on_page_writeback(page, DATA, true);
851 zero_user(page, start, len);
852 set_page_dirty(page);
853 f2fs_put_page(page, 1);
854 return 0;
857 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
859 int err;
861 while (pg_start < pg_end) {
862 struct dnode_of_data dn;
863 pgoff_t end_offset, count;
865 set_new_dnode(&dn, inode, NULL, NULL, 0);
866 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
867 if (err) {
868 if (err == -ENOENT) {
869 pg_start++;
870 continue;
872 return err;
875 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
876 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
878 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
880 truncate_data_blocks_range(&dn, count);
881 f2fs_put_dnode(&dn);
883 pg_start += count;
885 return 0;
888 static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
890 pgoff_t pg_start, pg_end;
891 loff_t off_start, off_end;
892 int ret;
894 ret = f2fs_convert_inline_inode(inode);
895 if (ret)
896 return ret;
898 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
899 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
901 off_start = offset & (PAGE_SIZE - 1);
902 off_end = (offset + len) & (PAGE_SIZE - 1);
904 if (pg_start == pg_end) {
905 ret = fill_zero(inode, pg_start, off_start,
906 off_end - off_start);
907 if (ret)
908 return ret;
909 } else {
910 if (off_start) {
911 ret = fill_zero(inode, pg_start++, off_start,
912 PAGE_SIZE - off_start);
913 if (ret)
914 return ret;
916 if (off_end) {
917 ret = fill_zero(inode, pg_end, 0, off_end);
918 if (ret)
919 return ret;
922 if (pg_start < pg_end) {
923 struct address_space *mapping = inode->i_mapping;
924 loff_t blk_start, blk_end;
925 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
927 f2fs_balance_fs(sbi, true);
929 blk_start = (loff_t)pg_start << PAGE_SHIFT;
930 blk_end = (loff_t)pg_end << PAGE_SHIFT;
931 down_write(&F2FS_I(inode)->i_mmap_sem);
932 truncate_inode_pages_range(mapping, blk_start,
933 blk_end - 1);
935 f2fs_lock_op(sbi);
936 ret = truncate_hole(inode, pg_start, pg_end);
937 f2fs_unlock_op(sbi);
938 up_write(&F2FS_I(inode)->i_mmap_sem);
942 return ret;
945 static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
946 int *do_replace, pgoff_t off, pgoff_t len)
948 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
949 struct dnode_of_data dn;
950 int ret, done, i;
952 next_dnode:
953 set_new_dnode(&dn, inode, NULL, NULL, 0);
954 ret = get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
955 if (ret && ret != -ENOENT) {
956 return ret;
957 } else if (ret == -ENOENT) {
958 if (dn.max_level == 0)
959 return -ENOENT;
960 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
961 blkaddr += done;
962 do_replace += done;
963 goto next;
966 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
967 dn.ofs_in_node, len);
968 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
969 *blkaddr = datablock_addr(dn.inode,
970 dn.node_page, dn.ofs_in_node);
971 if (!is_checkpointed_data(sbi, *blkaddr)) {
973 if (test_opt(sbi, LFS)) {
974 f2fs_put_dnode(&dn);
975 return -ENOTSUPP;
978 /* do not invalidate this block address */
979 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
980 *do_replace = 1;
983 f2fs_put_dnode(&dn);
984 next:
985 len -= done;
986 off += done;
987 if (len)
988 goto next_dnode;
989 return 0;
992 static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
993 int *do_replace, pgoff_t off, int len)
995 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
996 struct dnode_of_data dn;
997 int ret, i;
999 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1000 if (*do_replace == 0)
1001 continue;
1003 set_new_dnode(&dn, inode, NULL, NULL, 0);
1004 ret = get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1005 if (ret) {
1006 dec_valid_block_count(sbi, inode, 1);
1007 invalidate_blocks(sbi, *blkaddr);
1008 } else {
1009 f2fs_update_data_blkaddr(&dn, *blkaddr);
1011 f2fs_put_dnode(&dn);
1013 return 0;
1016 static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1017 block_t *blkaddr, int *do_replace,
1018 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1020 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1021 pgoff_t i = 0;
1022 int ret;
1024 while (i < len) {
1025 if (blkaddr[i] == NULL_ADDR && !full) {
1026 i++;
1027 continue;
1030 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1031 struct dnode_of_data dn;
1032 struct node_info ni;
1033 size_t new_size;
1034 pgoff_t ilen;
1036 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1037 ret = get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1038 if (ret)
1039 return ret;
1041 get_node_info(sbi, dn.nid, &ni);
1042 ilen = min((pgoff_t)
1043 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1044 dn.ofs_in_node, len - i);
1045 do {
1046 dn.data_blkaddr = datablock_addr(dn.inode,
1047 dn.node_page, dn.ofs_in_node);
1048 truncate_data_blocks_range(&dn, 1);
1050 if (do_replace[i]) {
1051 f2fs_i_blocks_write(src_inode,
1052 1, false, false);
1053 f2fs_i_blocks_write(dst_inode,
1054 1, true, false);
1055 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1056 blkaddr[i], ni.version, true, false);
1058 do_replace[i] = 0;
1060 dn.ofs_in_node++;
1061 i++;
1062 new_size = (dst + i) << PAGE_SHIFT;
1063 if (dst_inode->i_size < new_size)
1064 f2fs_i_size_write(dst_inode, new_size);
1065 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1067 f2fs_put_dnode(&dn);
1068 } else {
1069 struct page *psrc, *pdst;
1071 psrc = get_lock_data_page(src_inode, src + i, true);
1072 if (IS_ERR(psrc))
1073 return PTR_ERR(psrc);
1074 pdst = get_new_data_page(dst_inode, NULL, dst + i,
1075 true);
1076 if (IS_ERR(pdst)) {
1077 f2fs_put_page(psrc, 1);
1078 return PTR_ERR(pdst);
1080 f2fs_copy_page(psrc, pdst);
1081 set_page_dirty(pdst);
1082 f2fs_put_page(pdst, 1);
1083 f2fs_put_page(psrc, 1);
1085 ret = truncate_hole(src_inode, src + i, src + i + 1);
1086 if (ret)
1087 return ret;
1088 i++;
1091 return 0;
1094 static int __exchange_data_block(struct inode *src_inode,
1095 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1096 pgoff_t len, bool full)
1098 block_t *src_blkaddr;
1099 int *do_replace;
1100 pgoff_t olen;
1101 int ret;
1103 while (len) {
1104 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
1106 src_blkaddr = kvzalloc(sizeof(block_t) * olen, GFP_KERNEL);
1107 if (!src_blkaddr)
1108 return -ENOMEM;
1110 do_replace = kvzalloc(sizeof(int) * olen, GFP_KERNEL);
1111 if (!do_replace) {
1112 kvfree(src_blkaddr);
1113 return -ENOMEM;
1116 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1117 do_replace, src, olen);
1118 if (ret)
1119 goto roll_back;
1121 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1122 do_replace, src, dst, olen, full);
1123 if (ret)
1124 goto roll_back;
1126 src += olen;
1127 dst += olen;
1128 len -= olen;
1130 kvfree(src_blkaddr);
1131 kvfree(do_replace);
1133 return 0;
1135 roll_back:
1136 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, len);
1137 kvfree(src_blkaddr);
1138 kvfree(do_replace);
1139 return ret;
1142 static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
1144 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1145 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1146 int ret;
1148 f2fs_balance_fs(sbi, true);
1149 f2fs_lock_op(sbi);
1151 f2fs_drop_extent_tree(inode);
1153 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1154 f2fs_unlock_op(sbi);
1155 return ret;
1158 static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1160 pgoff_t pg_start, pg_end;
1161 loff_t new_size;
1162 int ret;
1164 if (offset + len >= i_size_read(inode))
1165 return -EINVAL;
1167 /* collapse range should be aligned to block size of f2fs. */
1168 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1169 return -EINVAL;
1171 ret = f2fs_convert_inline_inode(inode);
1172 if (ret)
1173 return ret;
1175 pg_start = offset >> PAGE_SHIFT;
1176 pg_end = (offset + len) >> PAGE_SHIFT;
1178 down_write(&F2FS_I(inode)->i_mmap_sem);
1179 /* write out all dirty pages from offset */
1180 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1181 if (ret)
1182 goto out;
1184 truncate_pagecache(inode, offset);
1186 ret = f2fs_do_collapse(inode, pg_start, pg_end);
1187 if (ret)
1188 goto out;
1190 /* write out all moved pages, if possible */
1191 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1192 truncate_pagecache(inode, offset);
1194 new_size = i_size_read(inode) - len;
1195 truncate_pagecache(inode, new_size);
1197 ret = truncate_blocks(inode, new_size, true);
1198 if (!ret)
1199 f2fs_i_size_write(inode, new_size);
1201 out:
1202 up_write(&F2FS_I(inode)->i_mmap_sem);
1203 return ret;
1206 static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1207 pgoff_t end)
1209 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1210 pgoff_t index = start;
1211 unsigned int ofs_in_node = dn->ofs_in_node;
1212 blkcnt_t count = 0;
1213 int ret;
1215 for (; index < end; index++, dn->ofs_in_node++) {
1216 if (datablock_addr(dn->inode, dn->node_page,
1217 dn->ofs_in_node) == NULL_ADDR)
1218 count++;
1221 dn->ofs_in_node = ofs_in_node;
1222 ret = reserve_new_blocks(dn, count);
1223 if (ret)
1224 return ret;
1226 dn->ofs_in_node = ofs_in_node;
1227 for (index = start; index < end; index++, dn->ofs_in_node++) {
1228 dn->data_blkaddr = datablock_addr(dn->inode,
1229 dn->node_page, dn->ofs_in_node);
1231 * reserve_new_blocks will not guarantee entire block
1232 * allocation.
1234 if (dn->data_blkaddr == NULL_ADDR) {
1235 ret = -ENOSPC;
1236 break;
1238 if (dn->data_blkaddr != NEW_ADDR) {
1239 invalidate_blocks(sbi, dn->data_blkaddr);
1240 dn->data_blkaddr = NEW_ADDR;
1241 set_data_blkaddr(dn);
1245 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1247 return ret;
1250 static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1251 int mode)
1253 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1254 struct address_space *mapping = inode->i_mapping;
1255 pgoff_t index, pg_start, pg_end;
1256 loff_t new_size = i_size_read(inode);
1257 loff_t off_start, off_end;
1258 int ret = 0;
1260 ret = inode_newsize_ok(inode, (len + offset));
1261 if (ret)
1262 return ret;
1264 ret = f2fs_convert_inline_inode(inode);
1265 if (ret)
1266 return ret;
1268 down_write(&F2FS_I(inode)->i_mmap_sem);
1269 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1270 if (ret)
1271 goto out_sem;
1273 truncate_pagecache_range(inode, offset, offset + len - 1);
1275 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1276 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1278 off_start = offset & (PAGE_SIZE - 1);
1279 off_end = (offset + len) & (PAGE_SIZE - 1);
1281 if (pg_start == pg_end) {
1282 ret = fill_zero(inode, pg_start, off_start,
1283 off_end - off_start);
1284 if (ret)
1285 goto out_sem;
1287 new_size = max_t(loff_t, new_size, offset + len);
1288 } else {
1289 if (off_start) {
1290 ret = fill_zero(inode, pg_start++, off_start,
1291 PAGE_SIZE - off_start);
1292 if (ret)
1293 goto out_sem;
1295 new_size = max_t(loff_t, new_size,
1296 (loff_t)pg_start << PAGE_SHIFT);
1299 for (index = pg_start; index < pg_end;) {
1300 struct dnode_of_data dn;
1301 unsigned int end_offset;
1302 pgoff_t end;
1304 f2fs_lock_op(sbi);
1306 set_new_dnode(&dn, inode, NULL, NULL, 0);
1307 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
1308 if (ret) {
1309 f2fs_unlock_op(sbi);
1310 goto out;
1313 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1314 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1316 ret = f2fs_do_zero_range(&dn, index, end);
1317 f2fs_put_dnode(&dn);
1318 f2fs_unlock_op(sbi);
1320 f2fs_balance_fs(sbi, dn.node_changed);
1322 if (ret)
1323 goto out;
1325 index = end;
1326 new_size = max_t(loff_t, new_size,
1327 (loff_t)index << PAGE_SHIFT);
1330 if (off_end) {
1331 ret = fill_zero(inode, pg_end, 0, off_end);
1332 if (ret)
1333 goto out;
1335 new_size = max_t(loff_t, new_size, offset + len);
1339 out:
1340 if (new_size > i_size_read(inode)) {
1341 if (mode & FALLOC_FL_KEEP_SIZE)
1342 file_set_keep_isize(inode);
1343 else
1344 f2fs_i_size_write(inode, new_size);
1346 out_sem:
1347 up_write(&F2FS_I(inode)->i_mmap_sem);
1349 return ret;
1352 static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1354 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1355 pgoff_t nr, pg_start, pg_end, delta, idx;
1356 loff_t new_size;
1357 int ret = 0;
1359 new_size = i_size_read(inode) + len;
1360 ret = inode_newsize_ok(inode, new_size);
1361 if (ret)
1362 return ret;
1364 if (offset >= i_size_read(inode))
1365 return -EINVAL;
1367 /* insert range should be aligned to block size of f2fs. */
1368 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1369 return -EINVAL;
1371 ret = f2fs_convert_inline_inode(inode);
1372 if (ret)
1373 return ret;
1375 f2fs_balance_fs(sbi, true);
1377 down_write(&F2FS_I(inode)->i_mmap_sem);
1378 ret = truncate_blocks(inode, i_size_read(inode), true);
1379 if (ret)
1380 goto out;
1382 /* write out all dirty pages from offset */
1383 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1384 if (ret)
1385 goto out;
1387 truncate_pagecache(inode, offset);
1389 pg_start = offset >> PAGE_SHIFT;
1390 pg_end = (offset + len) >> PAGE_SHIFT;
1391 delta = pg_end - pg_start;
1392 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1394 while (!ret && idx > pg_start) {
1395 nr = idx - pg_start;
1396 if (nr > delta)
1397 nr = delta;
1398 idx -= nr;
1400 f2fs_lock_op(sbi);
1401 f2fs_drop_extent_tree(inode);
1403 ret = __exchange_data_block(inode, inode, idx,
1404 idx + delta, nr, false);
1405 f2fs_unlock_op(sbi);
1408 /* write out all moved pages, if possible */
1409 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1410 truncate_pagecache(inode, offset);
1412 if (!ret)
1413 f2fs_i_size_write(inode, new_size);
1414 out:
1415 up_write(&F2FS_I(inode)->i_mmap_sem);
1416 return ret;
1419 static int expand_inode_data(struct inode *inode, loff_t offset,
1420 loff_t len, int mode)
1422 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1423 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
1424 pgoff_t pg_end;
1425 loff_t new_size = i_size_read(inode);
1426 loff_t off_end;
1427 int err;
1429 err = inode_newsize_ok(inode, (len + offset));
1430 if (err)
1431 return err;
1433 err = f2fs_convert_inline_inode(inode);
1434 if (err)
1435 return err;
1437 f2fs_balance_fs(sbi, true);
1439 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1440 off_end = (offset + len) & (PAGE_SIZE - 1);
1442 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1443 map.m_len = pg_end - map.m_lblk;
1444 if (off_end)
1445 map.m_len++;
1447 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1448 if (err) {
1449 pgoff_t last_off;
1451 if (!map.m_len)
1452 return err;
1454 last_off = map.m_lblk + map.m_len - 1;
1456 /* update new size to the failed position */
1457 new_size = (last_off == pg_end) ? offset + len:
1458 (loff_t)(last_off + 1) << PAGE_SHIFT;
1459 } else {
1460 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1463 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
1464 f2fs_i_size_write(inode, new_size);
1466 return err;
1469 static long f2fs_fallocate(struct file *file, int mode,
1470 loff_t offset, loff_t len)
1472 struct inode *inode = file_inode(file);
1473 long ret = 0;
1475 /* f2fs only support ->fallocate for regular file */
1476 if (!S_ISREG(inode->i_mode))
1477 return -EINVAL;
1479 if (f2fs_encrypted_inode(inode) &&
1480 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1481 return -EOPNOTSUPP;
1483 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1484 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1485 FALLOC_FL_INSERT_RANGE))
1486 return -EOPNOTSUPP;
1488 inode_lock(inode);
1490 if (mode & FALLOC_FL_PUNCH_HOLE) {
1491 if (offset >= inode->i_size)
1492 goto out;
1494 ret = punch_hole(inode, offset, len);
1495 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1496 ret = f2fs_collapse_range(inode, offset, len);
1497 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1498 ret = f2fs_zero_range(inode, offset, len, mode);
1499 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1500 ret = f2fs_insert_range(inode, offset, len);
1501 } else {
1502 ret = expand_inode_data(inode, offset, len, mode);
1505 if (!ret) {
1506 inode->i_mtime = inode->i_ctime = current_time(inode);
1507 f2fs_mark_inode_dirty_sync(inode, false);
1508 if (mode & FALLOC_FL_KEEP_SIZE)
1509 file_set_keep_isize(inode);
1510 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1513 out:
1514 inode_unlock(inode);
1516 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1517 return ret;
1520 static int f2fs_release_file(struct inode *inode, struct file *filp)
1523 * f2fs_relase_file is called at every close calls. So we should
1524 * not drop any inmemory pages by close called by other process.
1526 if (!(filp->f_mode & FMODE_WRITE) ||
1527 atomic_read(&inode->i_writecount) != 1)
1528 return 0;
1530 /* some remained atomic pages should discarded */
1531 if (f2fs_is_atomic_file(inode))
1532 drop_inmem_pages(inode);
1533 if (f2fs_is_volatile_file(inode)) {
1534 clear_inode_flag(inode, FI_VOLATILE_FILE);
1535 stat_dec_volatile_write(inode);
1536 set_inode_flag(inode, FI_DROP_CACHE);
1537 filemap_fdatawrite(inode->i_mapping);
1538 clear_inode_flag(inode, FI_DROP_CACHE);
1540 return 0;
1543 static int f2fs_file_flush(struct file *file, fl_owner_t id)
1545 struct inode *inode = file_inode(file);
1548 * If the process doing a transaction is crashed, we should do
1549 * roll-back. Otherwise, other reader/write can see corrupted database
1550 * until all the writers close its file. Since this should be done
1551 * before dropping file lock, it needs to do in ->flush.
1553 if (f2fs_is_atomic_file(inode) &&
1554 F2FS_I(inode)->inmem_task == current)
1555 drop_inmem_pages(inode);
1556 return 0;
1559 static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1561 struct inode *inode = file_inode(filp);
1562 struct f2fs_inode_info *fi = F2FS_I(inode);
1563 unsigned int flags = fi->i_flags &
1564 (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL);
1565 return put_user(flags, (int __user *)arg);
1568 static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags)
1570 struct f2fs_inode_info *fi = F2FS_I(inode);
1571 unsigned int oldflags;
1573 /* Is it quota file? Do not allow user to mess with it */
1574 if (IS_NOQUOTA(inode))
1575 return -EPERM;
1577 flags = f2fs_mask_flags(inode->i_mode, flags);
1579 oldflags = fi->i_flags;
1581 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL))
1582 if (!capable(CAP_LINUX_IMMUTABLE))
1583 return -EPERM;
1585 flags = flags & (FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL);
1586 flags |= oldflags & ~(FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL);
1587 fi->i_flags = flags;
1589 if (fi->i_flags & FS_PROJINHERIT_FL)
1590 set_inode_flag(inode, FI_PROJ_INHERIT);
1591 else
1592 clear_inode_flag(inode, FI_PROJ_INHERIT);
1594 inode->i_ctime = current_time(inode);
1595 f2fs_set_inode_flags(inode);
1596 f2fs_mark_inode_dirty_sync(inode, false);
1597 return 0;
1600 static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1602 struct inode *inode = file_inode(filp);
1603 unsigned int flags;
1604 int ret;
1606 if (!inode_owner_or_capable(inode))
1607 return -EACCES;
1609 if (get_user(flags, (int __user *)arg))
1610 return -EFAULT;
1612 ret = mnt_want_write_file(filp);
1613 if (ret)
1614 return ret;
1616 inode_lock(inode);
1618 ret = __f2fs_ioc_setflags(inode, flags);
1620 inode_unlock(inode);
1621 mnt_drop_write_file(filp);
1622 return ret;
1625 static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1627 struct inode *inode = file_inode(filp);
1629 return put_user(inode->i_generation, (int __user *)arg);
1632 static int f2fs_ioc_start_atomic_write(struct file *filp)
1634 struct inode *inode = file_inode(filp);
1635 int ret;
1637 if (!inode_owner_or_capable(inode))
1638 return -EACCES;
1640 if (!S_ISREG(inode->i_mode))
1641 return -EINVAL;
1643 ret = mnt_want_write_file(filp);
1644 if (ret)
1645 return ret;
1647 inode_lock(inode);
1649 down_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1651 if (f2fs_is_atomic_file(inode))
1652 goto out;
1654 ret = f2fs_convert_inline_inode(inode);
1655 if (ret)
1656 goto out;
1658 set_inode_flag(inode, FI_ATOMIC_FILE);
1659 set_inode_flag(inode, FI_HOT_DATA);
1660 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1662 if (!get_dirty_pages(inode))
1663 goto inc_stat;
1665 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
1666 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
1667 inode->i_ino, get_dirty_pages(inode));
1668 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
1669 if (ret) {
1670 clear_inode_flag(inode, FI_ATOMIC_FILE);
1671 clear_inode_flag(inode, FI_HOT_DATA);
1672 goto out;
1675 inc_stat:
1676 F2FS_I(inode)->inmem_task = current;
1677 stat_inc_atomic_write(inode);
1678 stat_update_max_atomic_write(inode);
1679 out:
1680 up_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1681 inode_unlock(inode);
1682 mnt_drop_write_file(filp);
1683 return ret;
1686 static int f2fs_ioc_commit_atomic_write(struct file *filp)
1688 struct inode *inode = file_inode(filp);
1689 int ret;
1691 if (!inode_owner_or_capable(inode))
1692 return -EACCES;
1694 ret = mnt_want_write_file(filp);
1695 if (ret)
1696 return ret;
1698 inode_lock(inode);
1700 if (f2fs_is_volatile_file(inode))
1701 goto err_out;
1703 if (f2fs_is_atomic_file(inode)) {
1704 ret = commit_inmem_pages(inode);
1705 if (ret)
1706 goto err_out;
1708 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1709 if (!ret) {
1710 clear_inode_flag(inode, FI_ATOMIC_FILE);
1711 clear_inode_flag(inode, FI_HOT_DATA);
1712 stat_dec_atomic_write(inode);
1714 } else {
1715 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
1717 err_out:
1718 inode_unlock(inode);
1719 mnt_drop_write_file(filp);
1720 return ret;
1723 static int f2fs_ioc_start_volatile_write(struct file *filp)
1725 struct inode *inode = file_inode(filp);
1726 int ret;
1728 if (!inode_owner_or_capable(inode))
1729 return -EACCES;
1731 if (!S_ISREG(inode->i_mode))
1732 return -EINVAL;
1734 ret = mnt_want_write_file(filp);
1735 if (ret)
1736 return ret;
1738 inode_lock(inode);
1740 if (f2fs_is_volatile_file(inode))
1741 goto out;
1743 ret = f2fs_convert_inline_inode(inode);
1744 if (ret)
1745 goto out;
1747 stat_inc_volatile_write(inode);
1748 stat_update_max_volatile_write(inode);
1750 set_inode_flag(inode, FI_VOLATILE_FILE);
1751 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1752 out:
1753 inode_unlock(inode);
1754 mnt_drop_write_file(filp);
1755 return ret;
1758 static int f2fs_ioc_release_volatile_write(struct file *filp)
1760 struct inode *inode = file_inode(filp);
1761 int ret;
1763 if (!inode_owner_or_capable(inode))
1764 return -EACCES;
1766 ret = mnt_want_write_file(filp);
1767 if (ret)
1768 return ret;
1770 inode_lock(inode);
1772 if (!f2fs_is_volatile_file(inode))
1773 goto out;
1775 if (!f2fs_is_first_block_written(inode)) {
1776 ret = truncate_partial_data_page(inode, 0, true);
1777 goto out;
1780 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1781 out:
1782 inode_unlock(inode);
1783 mnt_drop_write_file(filp);
1784 return ret;
1787 static int f2fs_ioc_abort_volatile_write(struct file *filp)
1789 struct inode *inode = file_inode(filp);
1790 int ret;
1792 if (!inode_owner_or_capable(inode))
1793 return -EACCES;
1795 ret = mnt_want_write_file(filp);
1796 if (ret)
1797 return ret;
1799 inode_lock(inode);
1801 if (f2fs_is_atomic_file(inode))
1802 drop_inmem_pages(inode);
1803 if (f2fs_is_volatile_file(inode)) {
1804 clear_inode_flag(inode, FI_VOLATILE_FILE);
1805 stat_dec_volatile_write(inode);
1806 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1809 inode_unlock(inode);
1811 mnt_drop_write_file(filp);
1812 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1813 return ret;
1816 static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1818 struct inode *inode = file_inode(filp);
1819 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1820 struct super_block *sb = sbi->sb;
1821 __u32 in;
1822 int ret = 0;
1824 if (!capable(CAP_SYS_ADMIN))
1825 return -EPERM;
1827 if (get_user(in, (__u32 __user *)arg))
1828 return -EFAULT;
1830 if (in != F2FS_GOING_DOWN_FULLSYNC) {
1831 ret = mnt_want_write_file(filp);
1832 if (ret)
1833 return ret;
1836 switch (in) {
1837 case F2FS_GOING_DOWN_FULLSYNC:
1838 sb = freeze_bdev(sb->s_bdev);
1839 if (sb && !IS_ERR(sb)) {
1840 f2fs_stop_checkpoint(sbi, false);
1841 thaw_bdev(sb->s_bdev, sb);
1843 break;
1844 case F2FS_GOING_DOWN_METASYNC:
1845 /* do checkpoint only */
1846 f2fs_sync_fs(sb, 1);
1847 f2fs_stop_checkpoint(sbi, false);
1848 break;
1849 case F2FS_GOING_DOWN_NOSYNC:
1850 f2fs_stop_checkpoint(sbi, false);
1851 break;
1852 case F2FS_GOING_DOWN_METAFLUSH:
1853 sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
1854 f2fs_stop_checkpoint(sbi, false);
1855 break;
1856 default:
1857 ret = -EINVAL;
1858 goto out;
1860 f2fs_update_time(sbi, REQ_TIME);
1861 out:
1862 if (in != F2FS_GOING_DOWN_FULLSYNC)
1863 mnt_drop_write_file(filp);
1864 return ret;
1867 static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1869 struct inode *inode = file_inode(filp);
1870 struct super_block *sb = inode->i_sb;
1871 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1872 struct fstrim_range range;
1873 int ret;
1875 if (!capable(CAP_SYS_ADMIN))
1876 return -EPERM;
1878 if (!blk_queue_discard(q))
1879 return -EOPNOTSUPP;
1881 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1882 sizeof(range)))
1883 return -EFAULT;
1885 ret = mnt_want_write_file(filp);
1886 if (ret)
1887 return ret;
1889 range.minlen = max((unsigned int)range.minlen,
1890 q->limits.discard_granularity);
1891 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
1892 mnt_drop_write_file(filp);
1893 if (ret < 0)
1894 return ret;
1896 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1897 sizeof(range)))
1898 return -EFAULT;
1899 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1900 return 0;
1903 static bool uuid_is_nonzero(__u8 u[16])
1905 int i;
1907 for (i = 0; i < 16; i++)
1908 if (u[i])
1909 return true;
1910 return false;
1913 static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1915 struct inode *inode = file_inode(filp);
1917 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1919 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
1922 static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1924 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1927 static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1929 struct inode *inode = file_inode(filp);
1930 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1931 int err;
1933 if (!f2fs_sb_has_crypto(inode->i_sb))
1934 return -EOPNOTSUPP;
1936 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1937 goto got_it;
1939 err = mnt_want_write_file(filp);
1940 if (err)
1941 return err;
1943 /* update superblock with uuid */
1944 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1946 err = f2fs_commit_super(sbi, false);
1947 if (err) {
1948 /* undo new data */
1949 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
1950 mnt_drop_write_file(filp);
1951 return err;
1953 mnt_drop_write_file(filp);
1954 got_it:
1955 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1956 16))
1957 return -EFAULT;
1958 return 0;
1961 static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
1963 struct inode *inode = file_inode(filp);
1964 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1965 __u32 sync;
1966 int ret;
1968 if (!capable(CAP_SYS_ADMIN))
1969 return -EPERM;
1971 if (get_user(sync, (__u32 __user *)arg))
1972 return -EFAULT;
1974 if (f2fs_readonly(sbi->sb))
1975 return -EROFS;
1977 ret = mnt_want_write_file(filp);
1978 if (ret)
1979 return ret;
1981 if (!sync) {
1982 if (!mutex_trylock(&sbi->gc_mutex)) {
1983 ret = -EBUSY;
1984 goto out;
1986 } else {
1987 mutex_lock(&sbi->gc_mutex);
1990 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
1991 out:
1992 mnt_drop_write_file(filp);
1993 return ret;
1996 static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
1998 struct inode *inode = file_inode(filp);
1999 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2000 struct f2fs_gc_range range;
2001 u64 end;
2002 int ret;
2004 if (!capable(CAP_SYS_ADMIN))
2005 return -EPERM;
2007 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2008 sizeof(range)))
2009 return -EFAULT;
2011 if (f2fs_readonly(sbi->sb))
2012 return -EROFS;
2014 ret = mnt_want_write_file(filp);
2015 if (ret)
2016 return ret;
2018 end = range.start + range.len;
2019 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi))
2020 return -EINVAL;
2021 do_more:
2022 if (!range.sync) {
2023 if (!mutex_trylock(&sbi->gc_mutex)) {
2024 ret = -EBUSY;
2025 goto out;
2027 } else {
2028 mutex_lock(&sbi->gc_mutex);
2031 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
2032 range.start += sbi->blocks_per_seg;
2033 if (range.start <= end)
2034 goto do_more;
2035 out:
2036 mnt_drop_write_file(filp);
2037 return ret;
2040 static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2042 struct inode *inode = file_inode(filp);
2043 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2044 int ret;
2046 if (!capable(CAP_SYS_ADMIN))
2047 return -EPERM;
2049 if (f2fs_readonly(sbi->sb))
2050 return -EROFS;
2052 ret = mnt_want_write_file(filp);
2053 if (ret)
2054 return ret;
2056 ret = f2fs_sync_fs(sbi->sb, 1);
2058 mnt_drop_write_file(filp);
2059 return ret;
2062 static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2063 struct file *filp,
2064 struct f2fs_defragment *range)
2066 struct inode *inode = file_inode(filp);
2067 struct f2fs_map_blocks map = { .m_next_pgofs = NULL };
2068 struct extent_info ei = {0,0,0};
2069 pgoff_t pg_start, pg_end;
2070 unsigned int blk_per_seg = sbi->blocks_per_seg;
2071 unsigned int total = 0, sec_num;
2072 block_t blk_end = 0;
2073 bool fragmented = false;
2074 int err;
2076 /* if in-place-update policy is enabled, don't waste time here */
2077 if (need_inplace_update_policy(inode, NULL))
2078 return -EINVAL;
2080 pg_start = range->start >> PAGE_SHIFT;
2081 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2083 f2fs_balance_fs(sbi, true);
2085 inode_lock(inode);
2087 /* writeback all dirty pages in the range */
2088 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2089 range->start + range->len - 1);
2090 if (err)
2091 goto out;
2094 * lookup mapping info in extent cache, skip defragmenting if physical
2095 * block addresses are continuous.
2097 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2098 if (ei.fofs + ei.len >= pg_end)
2099 goto out;
2102 map.m_lblk = pg_start;
2105 * lookup mapping info in dnode page cache, skip defragmenting if all
2106 * physical block addresses are continuous even if there are hole(s)
2107 * in logical blocks.
2109 while (map.m_lblk < pg_end) {
2110 map.m_len = pg_end - map.m_lblk;
2111 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2112 if (err)
2113 goto out;
2115 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2116 map.m_lblk++;
2117 continue;
2120 if (blk_end && blk_end != map.m_pblk) {
2121 fragmented = true;
2122 break;
2124 blk_end = map.m_pblk + map.m_len;
2126 map.m_lblk += map.m_len;
2129 if (!fragmented)
2130 goto out;
2132 map.m_lblk = pg_start;
2133 map.m_len = pg_end - pg_start;
2135 sec_num = (map.m_len + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
2138 * make sure there are enough free section for LFS allocation, this can
2139 * avoid defragment running in SSR mode when free section are allocated
2140 * intensively
2142 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2143 err = -EAGAIN;
2144 goto out;
2147 while (map.m_lblk < pg_end) {
2148 pgoff_t idx;
2149 int cnt = 0;
2151 do_map:
2152 map.m_len = pg_end - map.m_lblk;
2153 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2154 if (err)
2155 goto clear_out;
2157 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2158 map.m_lblk++;
2159 continue;
2162 set_inode_flag(inode, FI_DO_DEFRAG);
2164 idx = map.m_lblk;
2165 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2166 struct page *page;
2168 page = get_lock_data_page(inode, idx, true);
2169 if (IS_ERR(page)) {
2170 err = PTR_ERR(page);
2171 goto clear_out;
2174 set_page_dirty(page);
2175 f2fs_put_page(page, 1);
2177 idx++;
2178 cnt++;
2179 total++;
2182 map.m_lblk = idx;
2184 if (idx < pg_end && cnt < blk_per_seg)
2185 goto do_map;
2187 clear_inode_flag(inode, FI_DO_DEFRAG);
2189 err = filemap_fdatawrite(inode->i_mapping);
2190 if (err)
2191 goto out;
2193 clear_out:
2194 clear_inode_flag(inode, FI_DO_DEFRAG);
2195 out:
2196 inode_unlock(inode);
2197 if (!err)
2198 range->len = (u64)total << PAGE_SHIFT;
2199 return err;
2202 static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2204 struct inode *inode = file_inode(filp);
2205 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2206 struct f2fs_defragment range;
2207 int err;
2209 if (!capable(CAP_SYS_ADMIN))
2210 return -EPERM;
2212 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2213 return -EINVAL;
2215 if (f2fs_readonly(sbi->sb))
2216 return -EROFS;
2218 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2219 sizeof(range)))
2220 return -EFAULT;
2222 /* verify alignment of offset & size */
2223 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2224 return -EINVAL;
2226 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2227 sbi->max_file_blocks))
2228 return -EINVAL;
2230 err = mnt_want_write_file(filp);
2231 if (err)
2232 return err;
2234 err = f2fs_defragment_range(sbi, filp, &range);
2235 mnt_drop_write_file(filp);
2237 f2fs_update_time(sbi, REQ_TIME);
2238 if (err < 0)
2239 return err;
2241 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2242 sizeof(range)))
2243 return -EFAULT;
2245 return 0;
2248 static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2249 struct file *file_out, loff_t pos_out, size_t len)
2251 struct inode *src = file_inode(file_in);
2252 struct inode *dst = file_inode(file_out);
2253 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2254 size_t olen = len, dst_max_i_size = 0;
2255 size_t dst_osize;
2256 int ret;
2258 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2259 src->i_sb != dst->i_sb)
2260 return -EXDEV;
2262 if (unlikely(f2fs_readonly(src->i_sb)))
2263 return -EROFS;
2265 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2266 return -EINVAL;
2268 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2269 return -EOPNOTSUPP;
2271 if (src == dst) {
2272 if (pos_in == pos_out)
2273 return 0;
2274 if (pos_out > pos_in && pos_out < pos_in + len)
2275 return -EINVAL;
2278 inode_lock(src);
2279 if (src != dst) {
2280 if (!inode_trylock(dst)) {
2281 ret = -EBUSY;
2282 goto out;
2286 ret = -EINVAL;
2287 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2288 goto out_unlock;
2289 if (len == 0)
2290 olen = len = src->i_size - pos_in;
2291 if (pos_in + len == src->i_size)
2292 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2293 if (len == 0) {
2294 ret = 0;
2295 goto out_unlock;
2298 dst_osize = dst->i_size;
2299 if (pos_out + olen > dst->i_size)
2300 dst_max_i_size = pos_out + olen;
2302 /* verify the end result is block aligned */
2303 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2304 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2305 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2306 goto out_unlock;
2308 ret = f2fs_convert_inline_inode(src);
2309 if (ret)
2310 goto out_unlock;
2312 ret = f2fs_convert_inline_inode(dst);
2313 if (ret)
2314 goto out_unlock;
2316 /* write out all dirty pages from offset */
2317 ret = filemap_write_and_wait_range(src->i_mapping,
2318 pos_in, pos_in + len);
2319 if (ret)
2320 goto out_unlock;
2322 ret = filemap_write_and_wait_range(dst->i_mapping,
2323 pos_out, pos_out + len);
2324 if (ret)
2325 goto out_unlock;
2327 f2fs_balance_fs(sbi, true);
2328 f2fs_lock_op(sbi);
2329 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2330 pos_out >> F2FS_BLKSIZE_BITS,
2331 len >> F2FS_BLKSIZE_BITS, false);
2333 if (!ret) {
2334 if (dst_max_i_size)
2335 f2fs_i_size_write(dst, dst_max_i_size);
2336 else if (dst_osize != dst->i_size)
2337 f2fs_i_size_write(dst, dst_osize);
2339 f2fs_unlock_op(sbi);
2340 out_unlock:
2341 if (src != dst)
2342 inode_unlock(dst);
2343 out:
2344 inode_unlock(src);
2345 return ret;
2348 static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2350 struct f2fs_move_range range;
2351 struct fd dst;
2352 int err;
2354 if (!(filp->f_mode & FMODE_READ) ||
2355 !(filp->f_mode & FMODE_WRITE))
2356 return -EBADF;
2358 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2359 sizeof(range)))
2360 return -EFAULT;
2362 dst = fdget(range.dst_fd);
2363 if (!dst.file)
2364 return -EBADF;
2366 if (!(dst.file->f_mode & FMODE_WRITE)) {
2367 err = -EBADF;
2368 goto err_out;
2371 err = mnt_want_write_file(filp);
2372 if (err)
2373 goto err_out;
2375 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2376 range.pos_out, range.len);
2378 mnt_drop_write_file(filp);
2379 if (err)
2380 goto err_out;
2382 if (copy_to_user((struct f2fs_move_range __user *)arg,
2383 &range, sizeof(range)))
2384 err = -EFAULT;
2385 err_out:
2386 fdput(dst);
2387 return err;
2390 static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2392 struct inode *inode = file_inode(filp);
2393 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2394 struct sit_info *sm = SIT_I(sbi);
2395 unsigned int start_segno = 0, end_segno = 0;
2396 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2397 struct f2fs_flush_device range;
2398 int ret;
2400 if (!capable(CAP_SYS_ADMIN))
2401 return -EPERM;
2403 if (f2fs_readonly(sbi->sb))
2404 return -EROFS;
2406 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2407 sizeof(range)))
2408 return -EFAULT;
2410 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num ||
2411 sbi->segs_per_sec != 1) {
2412 f2fs_msg(sbi->sb, KERN_WARNING,
2413 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2414 range.dev_num, sbi->s_ndevs,
2415 sbi->segs_per_sec);
2416 return -EINVAL;
2419 ret = mnt_want_write_file(filp);
2420 if (ret)
2421 return ret;
2423 if (range.dev_num != 0)
2424 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2425 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2427 start_segno = sm->last_victim[FLUSH_DEVICE];
2428 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2429 start_segno = dev_start_segno;
2430 end_segno = min(start_segno + range.segments, dev_end_segno);
2432 while (start_segno < end_segno) {
2433 if (!mutex_trylock(&sbi->gc_mutex)) {
2434 ret = -EBUSY;
2435 goto out;
2437 sm->last_victim[GC_CB] = end_segno + 1;
2438 sm->last_victim[GC_GREEDY] = end_segno + 1;
2439 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2440 ret = f2fs_gc(sbi, true, true, start_segno);
2441 if (ret == -EAGAIN)
2442 ret = 0;
2443 else if (ret < 0)
2444 break;
2445 start_segno++;
2447 out:
2448 mnt_drop_write_file(filp);
2449 return ret;
2452 static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2454 struct inode *inode = file_inode(filp);
2455 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2457 /* Must validate to set it with SQLite behavior in Android. */
2458 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2460 return put_user(sb_feature, (u32 __user *)arg);
2463 #ifdef CONFIG_QUOTA
2464 static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2466 struct inode *inode = file_inode(filp);
2467 struct f2fs_inode_info *fi = F2FS_I(inode);
2468 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2469 struct super_block *sb = sbi->sb;
2470 struct dquot *transfer_to[MAXQUOTAS] = {};
2471 struct page *ipage;
2472 kprojid_t kprojid;
2473 int err;
2475 if (!f2fs_sb_has_project_quota(sb)) {
2476 if (projid != F2FS_DEF_PROJID)
2477 return -EOPNOTSUPP;
2478 else
2479 return 0;
2482 if (!f2fs_has_extra_attr(inode))
2483 return -EOPNOTSUPP;
2485 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2487 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2488 return 0;
2490 err = mnt_want_write_file(filp);
2491 if (err)
2492 return err;
2494 err = -EPERM;
2495 inode_lock(inode);
2497 /* Is it quota file? Do not allow user to mess with it */
2498 if (IS_NOQUOTA(inode))
2499 goto out_unlock;
2501 ipage = get_node_page(sbi, inode->i_ino);
2502 if (IS_ERR(ipage)) {
2503 err = PTR_ERR(ipage);
2504 goto out_unlock;
2507 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2508 i_projid)) {
2509 err = -EOVERFLOW;
2510 f2fs_put_page(ipage, 1);
2511 goto out_unlock;
2513 f2fs_put_page(ipage, 1);
2515 err = dquot_initialize(inode);
2516 if (err)
2517 goto out_unlock;
2519 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2520 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2521 err = __dquot_transfer(inode, transfer_to);
2522 dqput(transfer_to[PRJQUOTA]);
2523 if (err)
2524 goto out_dirty;
2527 F2FS_I(inode)->i_projid = kprojid;
2528 inode->i_ctime = current_time(inode);
2529 out_dirty:
2530 f2fs_mark_inode_dirty_sync(inode, true);
2531 out_unlock:
2532 inode_unlock(inode);
2533 mnt_drop_write_file(filp);
2534 return err;
2536 #else
2537 static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2539 if (projid != F2FS_DEF_PROJID)
2540 return -EOPNOTSUPP;
2541 return 0;
2543 #endif
2545 /* Transfer internal flags to xflags */
2546 static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags)
2548 __u32 xflags = 0;
2550 if (iflags & FS_SYNC_FL)
2551 xflags |= FS_XFLAG_SYNC;
2552 if (iflags & FS_IMMUTABLE_FL)
2553 xflags |= FS_XFLAG_IMMUTABLE;
2554 if (iflags & FS_APPEND_FL)
2555 xflags |= FS_XFLAG_APPEND;
2556 if (iflags & FS_NODUMP_FL)
2557 xflags |= FS_XFLAG_NODUMP;
2558 if (iflags & FS_NOATIME_FL)
2559 xflags |= FS_XFLAG_NOATIME;
2560 if (iflags & FS_PROJINHERIT_FL)
2561 xflags |= FS_XFLAG_PROJINHERIT;
2562 return xflags;
2565 #define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
2566 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
2567 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
2569 /* Flags we can manipulate with through EXT4_IOC_FSSETXATTR */
2570 #define F2FS_FL_XFLAG_VISIBLE (FS_SYNC_FL | \
2571 FS_IMMUTABLE_FL | \
2572 FS_APPEND_FL | \
2573 FS_NODUMP_FL | \
2574 FS_NOATIME_FL | \
2575 FS_PROJINHERIT_FL)
2577 /* Transfer xflags flags to internal */
2578 static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags)
2580 unsigned long iflags = 0;
2582 if (xflags & FS_XFLAG_SYNC)
2583 iflags |= FS_SYNC_FL;
2584 if (xflags & FS_XFLAG_IMMUTABLE)
2585 iflags |= FS_IMMUTABLE_FL;
2586 if (xflags & FS_XFLAG_APPEND)
2587 iflags |= FS_APPEND_FL;
2588 if (xflags & FS_XFLAG_NODUMP)
2589 iflags |= FS_NODUMP_FL;
2590 if (xflags & FS_XFLAG_NOATIME)
2591 iflags |= FS_NOATIME_FL;
2592 if (xflags & FS_XFLAG_PROJINHERIT)
2593 iflags |= FS_PROJINHERIT_FL;
2595 return iflags;
2598 static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2600 struct inode *inode = file_inode(filp);
2601 struct f2fs_inode_info *fi = F2FS_I(inode);
2602 struct fsxattr fa;
2604 memset(&fa, 0, sizeof(struct fsxattr));
2605 fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags &
2606 (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL));
2608 if (f2fs_sb_has_project_quota(inode->i_sb))
2609 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
2610 fi->i_projid);
2612 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2613 return -EFAULT;
2614 return 0;
2617 static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2619 struct inode *inode = file_inode(filp);
2620 struct f2fs_inode_info *fi = F2FS_I(inode);
2621 struct fsxattr fa;
2622 unsigned int flags;
2623 int err;
2625 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2626 return -EFAULT;
2628 /* Make sure caller has proper permission */
2629 if (!inode_owner_or_capable(inode))
2630 return -EACCES;
2632 if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS)
2633 return -EOPNOTSUPP;
2635 flags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2636 if (f2fs_mask_flags(inode->i_mode, flags) != flags)
2637 return -EOPNOTSUPP;
2639 err = mnt_want_write_file(filp);
2640 if (err)
2641 return err;
2643 inode_lock(inode);
2644 flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
2645 (flags & F2FS_FL_XFLAG_VISIBLE);
2646 err = __f2fs_ioc_setflags(inode, flags);
2647 inode_unlock(inode);
2648 mnt_drop_write_file(filp);
2649 if (err)
2650 return err;
2652 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
2653 if (err)
2654 return err;
2656 return 0;
2659 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2661 switch (cmd) {
2662 case F2FS_IOC_GETFLAGS:
2663 return f2fs_ioc_getflags(filp, arg);
2664 case F2FS_IOC_SETFLAGS:
2665 return f2fs_ioc_setflags(filp, arg);
2666 case F2FS_IOC_GETVERSION:
2667 return f2fs_ioc_getversion(filp, arg);
2668 case F2FS_IOC_START_ATOMIC_WRITE:
2669 return f2fs_ioc_start_atomic_write(filp);
2670 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2671 return f2fs_ioc_commit_atomic_write(filp);
2672 case F2FS_IOC_START_VOLATILE_WRITE:
2673 return f2fs_ioc_start_volatile_write(filp);
2674 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2675 return f2fs_ioc_release_volatile_write(filp);
2676 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2677 return f2fs_ioc_abort_volatile_write(filp);
2678 case F2FS_IOC_SHUTDOWN:
2679 return f2fs_ioc_shutdown(filp, arg);
2680 case FITRIM:
2681 return f2fs_ioc_fitrim(filp, arg);
2682 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2683 return f2fs_ioc_set_encryption_policy(filp, arg);
2684 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2685 return f2fs_ioc_get_encryption_policy(filp, arg);
2686 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2687 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
2688 case F2FS_IOC_GARBAGE_COLLECT:
2689 return f2fs_ioc_gc(filp, arg);
2690 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2691 return f2fs_ioc_gc_range(filp, arg);
2692 case F2FS_IOC_WRITE_CHECKPOINT:
2693 return f2fs_ioc_write_checkpoint(filp, arg);
2694 case F2FS_IOC_DEFRAGMENT:
2695 return f2fs_ioc_defragment(filp, arg);
2696 case F2FS_IOC_MOVE_RANGE:
2697 return f2fs_ioc_move_range(filp, arg);
2698 case F2FS_IOC_FLUSH_DEVICE:
2699 return f2fs_ioc_flush_device(filp, arg);
2700 case F2FS_IOC_GET_FEATURES:
2701 return f2fs_ioc_get_features(filp, arg);
2702 case F2FS_IOC_FSGETXATTR:
2703 return f2fs_ioc_fsgetxattr(filp, arg);
2704 case F2FS_IOC_FSSETXATTR:
2705 return f2fs_ioc_fssetxattr(filp, arg);
2706 default:
2707 return -ENOTTY;
2711 static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2713 struct file *file = iocb->ki_filp;
2714 struct inode *inode = file_inode(file);
2715 struct blk_plug plug;
2716 ssize_t ret;
2718 inode_lock(inode);
2719 ret = generic_write_checks(iocb, from);
2720 if (ret > 0) {
2721 bool preallocated = false;
2722 size_t target_size = 0;
2723 int err;
2725 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
2726 set_inode_flag(inode, FI_NO_PREALLOC);
2728 preallocated = true;
2729 target_size = iocb->ki_pos + iov_iter_count(from);
2731 err = f2fs_preallocate_blocks(iocb, from);
2732 if (err) {
2733 clear_inode_flag(inode, FI_NO_PREALLOC);
2734 inode_unlock(inode);
2735 return err;
2737 blk_start_plug(&plug);
2738 ret = __generic_file_write_iter(iocb, from);
2739 blk_finish_plug(&plug);
2740 clear_inode_flag(inode, FI_NO_PREALLOC);
2742 /* if we couldn't write data, we should deallocate blocks. */
2743 if (preallocated && i_size_read(inode) < target_size)
2744 f2fs_truncate(inode);
2746 if (ret > 0)
2747 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
2749 inode_unlock(inode);
2751 if (ret > 0)
2752 ret = generic_write_sync(iocb, ret);
2753 return ret;
2756 #ifdef CONFIG_COMPAT
2757 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2759 switch (cmd) {
2760 case F2FS_IOC32_GETFLAGS:
2761 cmd = F2FS_IOC_GETFLAGS;
2762 break;
2763 case F2FS_IOC32_SETFLAGS:
2764 cmd = F2FS_IOC_SETFLAGS;
2765 break;
2766 case F2FS_IOC32_GETVERSION:
2767 cmd = F2FS_IOC_GETVERSION;
2768 break;
2769 case F2FS_IOC_START_ATOMIC_WRITE:
2770 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2771 case F2FS_IOC_START_VOLATILE_WRITE:
2772 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2773 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2774 case F2FS_IOC_SHUTDOWN:
2775 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2776 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2777 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2778 case F2FS_IOC_GARBAGE_COLLECT:
2779 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2780 case F2FS_IOC_WRITE_CHECKPOINT:
2781 case F2FS_IOC_DEFRAGMENT:
2782 case F2FS_IOC_MOVE_RANGE:
2783 case F2FS_IOC_FLUSH_DEVICE:
2784 case F2FS_IOC_GET_FEATURES:
2785 case F2FS_IOC_FSGETXATTR:
2786 case F2FS_IOC_FSSETXATTR:
2787 break;
2788 default:
2789 return -ENOIOCTLCMD;
2791 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
2793 #endif
2795 const struct file_operations f2fs_file_operations = {
2796 .llseek = f2fs_llseek,
2797 .read_iter = generic_file_read_iter,
2798 .write_iter = f2fs_file_write_iter,
2799 .open = f2fs_file_open,
2800 .release = f2fs_release_file,
2801 .mmap = f2fs_file_mmap,
2802 .flush = f2fs_file_flush,
2803 .fsync = f2fs_sync_file,
2804 .fallocate = f2fs_fallocate,
2805 .unlocked_ioctl = f2fs_ioctl,
2806 #ifdef CONFIG_COMPAT
2807 .compat_ioctl = f2fs_compat_ioctl,
2808 #endif
2809 .splice_read = generic_file_splice_read,
2810 .splice_write = iter_file_splice_write,