1
2
3
4
5
6
7
8
9
10
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>
26
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>
35
36static int f2fs_filemap_fault(struct vm_fault *vmf)
37{
38 struct inode *inode = file_inode(vmf->vma->vm_file);
39 int err;
40
41 down_read(&F2FS_I(inode)->i_mmap_sem);
42 err = filemap_fault(vmf);
43 up_read(&F2FS_I(inode)->i_mmap_sem);
44
45 return err;
46}
47
48static int f2fs_vm_page_mkwrite(struct vm_fault *vmf)
49{
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;
55
56 if (unlikely(f2fs_cp_error(sbi))) {
57 err = -EIO;
58 goto err;
59 }
60
61 sb_start_pagefault(inode->i_sb);
62
63 f2fs_bug_on(sbi, f2fs_has_inline_data(inode));
64
65
66 f2fs_lock_op(sbi);
67 set_new_dnode(&dn, inode, NULL, NULL, 0);
68 err = f2fs_reserve_block(&dn, page->index);
69 if (err) {
70 f2fs_unlock_op(sbi);
71 goto out;
72 }
73 f2fs_put_dnode(&dn);
74 f2fs_unlock_op(sbi);
75
76 f2fs_balance_fs(sbi, dn.node_changed);
77
78 file_update_time(vmf->vma->vm_file);
79 down_read(&F2FS_I(inode)->i_mmap_sem);
80 lock_page(page);
81 if (unlikely(page->mapping != inode->i_mapping ||
82 page_offset(page) > i_size_read(inode) ||
83 !PageUptodate(page))) {
84 unlock_page(page);
85 err = -EFAULT;
86 goto out_sem;
87 }
88
89
90
91
92 if (PageMappedToDisk(page))
93 goto mapped;
94
95
96 if (((loff_t)(page->index + 1) << PAGE_SHIFT) >
97 i_size_read(inode)) {
98 unsigned offset;
99 offset = i_size_read(inode) & ~PAGE_MASK;
100 zero_user_segment(page, offset, PAGE_SIZE);
101 }
102 set_page_dirty(page);
103 if (!PageUptodate(page))
104 SetPageUptodate(page);
105
106 f2fs_update_iostat(sbi, APP_MAPPED_IO, F2FS_BLKSIZE);
107
108 trace_f2fs_vm_page_mkwrite(page, DATA);
109mapped:
110
111 f2fs_wait_on_page_writeback(page, DATA, false);
112
113
114 if (f2fs_encrypted_file(inode))
115 f2fs_wait_on_block_writeback(sbi, dn.data_blkaddr);
116
117out_sem:
118 up_read(&F2FS_I(inode)->i_mmap_sem);
119out:
120 sb_end_pagefault(inode->i_sb);
121 f2fs_update_time(sbi, REQ_TIME);
122err:
123 return block_page_mkwrite_return(err);
124}
125
126static const struct vm_operations_struct f2fs_file_vm_ops = {
127 .fault = f2fs_filemap_fault,
128 .map_pages = filemap_map_pages,
129 .page_mkwrite = f2fs_vm_page_mkwrite,
130};
131
132static int get_parent_ino(struct inode *inode, nid_t *pino)
133{
134 struct dentry *dentry;
135
136 inode = igrab(inode);
137 dentry = d_find_any_alias(inode);
138 iput(inode);
139 if (!dentry)
140 return 0;
141
142 *pino = parent_ino(dentry);
143 dput(dentry);
144 return 1;
145}
146
147static inline enum cp_reason_type need_do_checkpoint(struct inode *inode)
148{
149 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
150 enum cp_reason_type cp_reason = CP_NO_NEEDED;
151
152 if (!S_ISREG(inode->i_mode))
153 cp_reason = CP_NON_REGULAR;
154 else if (inode->i_nlink != 1)
155 cp_reason = CP_HARDLINK;
156 else if (is_sbi_flag_set(sbi, SBI_NEED_CP))
157 cp_reason = CP_SB_NEED_CP;
158 else if (file_wrong_pino(inode))
159 cp_reason = CP_WRONG_PINO;
160 else if (!space_for_roll_forward(sbi))
161 cp_reason = CP_NO_SPC_ROLL;
162 else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino))
163 cp_reason = CP_NODE_NEED_CP;
164 else if (test_opt(sbi, FASTBOOT))
165 cp_reason = CP_FASTBOOT_MODE;
166 else if (sbi->active_logs == 2)
167 cp_reason = CP_SPEC_LOG_NUM;
168 else if (need_dentry_mark(sbi, inode->i_ino) &&
169 exist_written_data(sbi, F2FS_I(inode)->i_pino, TRANS_DIR_INO))
170 cp_reason = CP_RECOVER_DIR;
171
172 return cp_reason;
173}
174
175static bool need_inode_page_update(struct f2fs_sb_info *sbi, nid_t ino)
176{
177 struct page *i = find_get_page(NODE_MAPPING(sbi), ino);
178 bool ret = false;
179
180 if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino))
181 ret = true;
182 f2fs_put_page(i, 0);
183 return ret;
184}
185
186static void try_to_fix_pino(struct inode *inode)
187{
188 struct f2fs_inode_info *fi = F2FS_I(inode);
189 nid_t pino;
190
191 down_write(&fi->i_sem);
192 if (file_wrong_pino(inode) && inode->i_nlink == 1 &&
193 get_parent_ino(inode, &pino)) {
194 f2fs_i_pino_write(inode, pino);
195 file_got_pino(inode);
196 }
197 up_write(&fi->i_sem);
198}
199
200static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end,
201 int datasync, bool atomic)
202{
203 struct inode *inode = file->f_mapping->host;
204 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
205 nid_t ino = inode->i_ino;
206 int ret = 0;
207 enum cp_reason_type cp_reason = 0;
208 struct writeback_control wbc = {
209 .sync_mode = WB_SYNC_ALL,
210 .nr_to_write = LONG_MAX,
211 .for_reclaim = 0,
212 };
213
214 if (unlikely(f2fs_readonly(inode->i_sb)))
215 return 0;
216
217 trace_f2fs_sync_file_enter(inode);
218
219
220 if (datasync || get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks)
221 set_inode_flag(inode, FI_NEED_IPU);
222 ret = file_write_and_wait_range(file, start, end);
223 clear_inode_flag(inode, FI_NEED_IPU);
224
225 if (ret) {
226 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
227 return ret;
228 }
229
230
231 if (!f2fs_skip_inode_update(inode, datasync)) {
232 f2fs_write_inode(inode, NULL);
233 goto go_write;
234 }
235
236
237
238
239 if (!is_inode_flag_set(inode, FI_APPEND_WRITE) &&
240 !exist_written_data(sbi, ino, APPEND_INO)) {
241
242
243 if (need_inode_page_update(sbi, ino))
244 goto go_write;
245
246 if (is_inode_flag_set(inode, FI_UPDATE_WRITE) ||
247 exist_written_data(sbi, ino, UPDATE_INO))
248 goto flush_out;
249 goto out;
250 }
251go_write:
252
253
254
255
256 down_read(&F2FS_I(inode)->i_sem);
257 cp_reason = need_do_checkpoint(inode);
258 up_read(&F2FS_I(inode)->i_sem);
259
260 if (cp_reason) {
261
262 ret = f2fs_sync_fs(inode->i_sb, 1);
263
264
265
266
267
268 try_to_fix_pino(inode);
269 clear_inode_flag(inode, FI_APPEND_WRITE);
270 clear_inode_flag(inode, FI_UPDATE_WRITE);
271 goto out;
272 }
273sync_nodes:
274 ret = fsync_node_pages(sbi, inode, &wbc, atomic);
275 if (ret)
276 goto out;
277
278
279 if (unlikely(f2fs_cp_error(sbi))) {
280 ret = -EIO;
281 goto out;
282 }
283
284 if (need_inode_block_update(sbi, ino)) {
285 f2fs_mark_inode_dirty_sync(inode, true);
286 f2fs_write_inode(inode, NULL);
287 goto sync_nodes;
288 }
289
290
291
292
293
294
295
296
297
298 if (!atomic) {
299 ret = wait_on_node_pages_writeback(sbi, ino);
300 if (ret)
301 goto out;
302 }
303
304
305 remove_ino_entry(sbi, ino, APPEND_INO);
306 clear_inode_flag(inode, FI_APPEND_WRITE);
307flush_out:
308 if (!atomic)
309 ret = f2fs_issue_flush(sbi, inode->i_ino);
310 if (!ret) {
311 remove_ino_entry(sbi, ino, UPDATE_INO);
312 clear_inode_flag(inode, FI_UPDATE_WRITE);
313 remove_ino_entry(sbi, ino, FLUSH_INO);
314 }
315 f2fs_update_time(sbi, REQ_TIME);
316out:
317 trace_f2fs_sync_file_exit(inode, cp_reason, datasync, ret);
318 f2fs_trace_ios(NULL, 1);
319 return ret;
320}
321
322int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
323{
324 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(file)))))
325 return -EIO;
326 return f2fs_do_sync_file(file, start, end, datasync, false);
327}
328
329static pgoff_t __get_first_dirty_index(struct address_space *mapping,
330 pgoff_t pgofs, int whence)
331{
332 struct page *page;
333 int nr_pages;
334
335 if (whence != SEEK_DATA)
336 return 0;
337
338
339 nr_pages = find_get_pages_tag(mapping, &pgofs, PAGECACHE_TAG_DIRTY,
340 1, &page);
341 if (!nr_pages)
342 return ULONG_MAX;
343 pgofs = page->index;
344 put_page(page);
345 return pgofs;
346}
347
348static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs,
349 int whence)
350{
351 switch (whence) {
352 case SEEK_DATA:
353 if ((blkaddr == NEW_ADDR && dirty == pgofs) ||
354 (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR))
355 return true;
356 break;
357 case SEEK_HOLE:
358 if (blkaddr == NULL_ADDR)
359 return true;
360 break;
361 }
362 return false;
363}
364
365static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence)
366{
367 struct inode *inode = file->f_mapping->host;
368 loff_t maxbytes = inode->i_sb->s_maxbytes;
369 struct dnode_of_data dn;
370 pgoff_t pgofs, end_offset, dirty;
371 loff_t data_ofs = offset;
372 loff_t isize;
373 int err = 0;
374
375 inode_lock(inode);
376
377 isize = i_size_read(inode);
378 if (offset >= isize)
379 goto fail;
380
381
382 if (f2fs_has_inline_data(inode) || f2fs_has_inline_dentry(inode)) {
383 if (whence == SEEK_HOLE)
384 data_ofs = isize;
385 goto found;
386 }
387
388 pgofs = (pgoff_t)(offset >> PAGE_SHIFT);
389
390 dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence);
391
392 for (; data_ofs < isize; data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
393 set_new_dnode(&dn, inode, NULL, NULL, 0);
394 err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE);
395 if (err && err != -ENOENT) {
396 goto fail;
397 } else if (err == -ENOENT) {
398
399 if (whence == SEEK_DATA) {
400 pgofs = get_next_page_offset(&dn, pgofs);
401 continue;
402 } else {
403 goto found;
404 }
405 }
406
407 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
408
409
410 for (; dn.ofs_in_node < end_offset;
411 dn.ofs_in_node++, pgofs++,
412 data_ofs = (loff_t)pgofs << PAGE_SHIFT) {
413 block_t blkaddr;
414 blkaddr = datablock_addr(dn.inode,
415 dn.node_page, dn.ofs_in_node);
416
417 if (__found_offset(blkaddr, dirty, pgofs, whence)) {
418 f2fs_put_dnode(&dn);
419 goto found;
420 }
421 }
422 f2fs_put_dnode(&dn);
423 }
424
425 if (whence == SEEK_DATA)
426 goto fail;
427found:
428 if (whence == SEEK_HOLE && data_ofs > isize)
429 data_ofs = isize;
430 inode_unlock(inode);
431 return vfs_setpos(file, data_ofs, maxbytes);
432fail:
433 inode_unlock(inode);
434 return -ENXIO;
435}
436
437static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
438{
439 struct inode *inode = file->f_mapping->host;
440 loff_t maxbytes = inode->i_sb->s_maxbytes;
441
442 switch (whence) {
443 case SEEK_SET:
444 case SEEK_CUR:
445 case SEEK_END:
446 return generic_file_llseek_size(file, offset, whence,
447 maxbytes, i_size_read(inode));
448 case SEEK_DATA:
449 case SEEK_HOLE:
450 if (offset < 0)
451 return -ENXIO;
452 return f2fs_seek_block(file, offset, whence);
453 }
454
455 return -EINVAL;
456}
457
458static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
459{
460 struct inode *inode = file_inode(file);
461 int err;
462
463 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
464 return -EIO;
465
466
467 err = f2fs_convert_inline_inode(inode);
468 if (err)
469 return err;
470
471 file_accessed(file);
472 vma->vm_ops = &f2fs_file_vm_ops;
473 return 0;
474}
475
476static int f2fs_file_open(struct inode *inode, struct file *filp)
477{
478 int err = fscrypt_file_open(inode, filp);
479
480 if (err)
481 return err;
482 return dquot_file_open(inode, filp);
483}
484
485void truncate_data_blocks_range(struct dnode_of_data *dn, int count)
486{
487 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
488 struct f2fs_node *raw_node;
489 int nr_free = 0, ofs = dn->ofs_in_node, len = count;
490 __le32 *addr;
491 int base = 0;
492
493 if (IS_INODE(dn->node_page) && f2fs_has_extra_attr(dn->inode))
494 base = get_extra_isize(dn->inode);
495
496 raw_node = F2FS_NODE(dn->node_page);
497 addr = blkaddr_in_node(raw_node) + base + ofs;
498
499 for (; count > 0; count--, addr++, dn->ofs_in_node++) {
500 block_t blkaddr = le32_to_cpu(*addr);
501 if (blkaddr == NULL_ADDR)
502 continue;
503
504 dn->data_blkaddr = NULL_ADDR;
505 set_data_blkaddr(dn);
506 invalidate_blocks(sbi, blkaddr);
507 if (dn->ofs_in_node == 0 && IS_INODE(dn->node_page))
508 clear_inode_flag(dn->inode, FI_FIRST_BLOCK_WRITTEN);
509 nr_free++;
510 }
511
512 if (nr_free) {
513 pgoff_t fofs;
514
515
516
517
518 fofs = start_bidx_of_node(ofs_of_node(dn->node_page),
519 dn->inode) + ofs;
520 f2fs_update_extent_cache_range(dn, fofs, 0, len);
521 dec_valid_block_count(sbi, dn->inode, nr_free);
522 }
523 dn->ofs_in_node = ofs;
524
525 f2fs_update_time(sbi, REQ_TIME);
526 trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid,
527 dn->ofs_in_node, nr_free);
528}
529
530void truncate_data_blocks(struct dnode_of_data *dn)
531{
532 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
533}
534
535static int truncate_partial_data_page(struct inode *inode, u64 from,
536 bool cache_only)
537{
538 unsigned offset = from & (PAGE_SIZE - 1);
539 pgoff_t index = from >> PAGE_SHIFT;
540 struct address_space *mapping = inode->i_mapping;
541 struct page *page;
542
543 if (!offset && !cache_only)
544 return 0;
545
546 if (cache_only) {
547 page = find_lock_page(mapping, index);
548 if (page && PageUptodate(page))
549 goto truncate_out;
550 f2fs_put_page(page, 1);
551 return 0;
552 }
553
554 page = get_lock_data_page(inode, index, true);
555 if (IS_ERR(page))
556 return PTR_ERR(page) == -ENOENT ? 0 : PTR_ERR(page);
557truncate_out:
558 f2fs_wait_on_page_writeback(page, DATA, true);
559 zero_user(page, offset, PAGE_SIZE - offset);
560
561
562 f2fs_bug_on(F2FS_I_SB(inode), cache_only && f2fs_encrypted_inode(inode));
563 if (!cache_only)
564 set_page_dirty(page);
565 f2fs_put_page(page, 1);
566 return 0;
567}
568
569int truncate_blocks(struct inode *inode, u64 from, bool lock)
570{
571 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
572 unsigned int blocksize = inode->i_sb->s_blocksize;
573 struct dnode_of_data dn;
574 pgoff_t free_from;
575 int count = 0, err = 0;
576 struct page *ipage;
577 bool truncate_page = false;
578
579 trace_f2fs_truncate_blocks_enter(inode, from);
580
581 free_from = (pgoff_t)F2FS_BYTES_TO_BLK(from + blocksize - 1);
582
583 if (free_from >= sbi->max_file_blocks)
584 goto free_partial;
585
586 if (lock)
587 f2fs_lock_op(sbi);
588
589 ipage = get_node_page(sbi, inode->i_ino);
590 if (IS_ERR(ipage)) {
591 err = PTR_ERR(ipage);
592 goto out;
593 }
594
595 if (f2fs_has_inline_data(inode)) {
596 truncate_inline_inode(inode, ipage, from);
597 f2fs_put_page(ipage, 1);
598 truncate_page = true;
599 goto out;
600 }
601
602 set_new_dnode(&dn, inode, ipage, NULL, 0);
603 err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE_RA);
604 if (err) {
605 if (err == -ENOENT)
606 goto free_next;
607 goto out;
608 }
609
610 count = ADDRS_PER_PAGE(dn.node_page, inode);
611
612 count -= dn.ofs_in_node;
613 f2fs_bug_on(sbi, count < 0);
614
615 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
616 truncate_data_blocks_range(&dn, count);
617 free_from += count;
618 }
619
620 f2fs_put_dnode(&dn);
621free_next:
622 err = truncate_inode_blocks(inode, free_from);
623out:
624 if (lock)
625 f2fs_unlock_op(sbi);
626free_partial:
627
628 if (!err)
629 err = truncate_partial_data_page(inode, from, truncate_page);
630
631 trace_f2fs_truncate_blocks_exit(inode, err);
632 return err;
633}
634
635int f2fs_truncate(struct inode *inode)
636{
637 int err;
638
639 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
640 return -EIO;
641
642 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
643 S_ISLNK(inode->i_mode)))
644 return 0;
645
646 trace_f2fs_truncate(inode);
647
648#ifdef CONFIG_F2FS_FAULT_INJECTION
649 if (time_to_inject(F2FS_I_SB(inode), FAULT_TRUNCATE)) {
650 f2fs_show_injection_info(FAULT_TRUNCATE);
651 return -EIO;
652 }
653#endif
654
655 if (!f2fs_may_inline_data(inode)) {
656 err = f2fs_convert_inline_inode(inode);
657 if (err)
658 return err;
659 }
660
661 err = truncate_blocks(inode, i_size_read(inode), true);
662 if (err)
663 return err;
664
665 inode->i_mtime = inode->i_ctime = current_time(inode);
666 f2fs_mark_inode_dirty_sync(inode, false);
667 return 0;
668}
669
670int f2fs_getattr(const struct path *path, struct kstat *stat,
671 u32 request_mask, unsigned int query_flags)
672{
673 struct inode *inode = d_inode(path->dentry);
674 struct f2fs_inode_info *fi = F2FS_I(inode);
675 struct f2fs_inode *ri;
676 unsigned int flags;
677
678 if (f2fs_has_extra_attr(inode) &&
679 f2fs_sb_has_inode_crtime(inode->i_sb) &&
680 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
681 stat->result_mask |= STATX_BTIME;
682 stat->btime.tv_sec = fi->i_crtime.tv_sec;
683 stat->btime.tv_nsec = fi->i_crtime.tv_nsec;
684 }
685
686 flags = fi->i_flags & (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL);
687 if (flags & FS_APPEND_FL)
688 stat->attributes |= STATX_ATTR_APPEND;
689 if (flags & FS_COMPR_FL)
690 stat->attributes |= STATX_ATTR_COMPRESSED;
691 if (f2fs_encrypted_inode(inode))
692 stat->attributes |= STATX_ATTR_ENCRYPTED;
693 if (flags & FS_IMMUTABLE_FL)
694 stat->attributes |= STATX_ATTR_IMMUTABLE;
695 if (flags & FS_NODUMP_FL)
696 stat->attributes |= STATX_ATTR_NODUMP;
697
698 stat->attributes_mask |= (STATX_ATTR_APPEND |
699 STATX_ATTR_COMPRESSED |
700 STATX_ATTR_ENCRYPTED |
701 STATX_ATTR_IMMUTABLE |
702 STATX_ATTR_NODUMP);
703
704 generic_fillattr(inode, stat);
705
706
707 if ((S_ISREG(inode->i_mode) && f2fs_has_inline_data(inode)) ||
708 f2fs_has_inline_dentry(inode))
709 stat->blocks += (stat->size + 511) >> 9;
710
711 return 0;
712}
713
714#ifdef CONFIG_F2FS_FS_POSIX_ACL
715static void __setattr_copy(struct inode *inode, const struct iattr *attr)
716{
717 unsigned int ia_valid = attr->ia_valid;
718
719 if (ia_valid & ATTR_UID)
720 inode->i_uid = attr->ia_uid;
721 if (ia_valid & ATTR_GID)
722 inode->i_gid = attr->ia_gid;
723 if (ia_valid & ATTR_ATIME)
724 inode->i_atime = timespec_trunc(attr->ia_atime,
725 inode->i_sb->s_time_gran);
726 if (ia_valid & ATTR_MTIME)
727 inode->i_mtime = timespec_trunc(attr->ia_mtime,
728 inode->i_sb->s_time_gran);
729 if (ia_valid & ATTR_CTIME)
730 inode->i_ctime = timespec_trunc(attr->ia_ctime,
731 inode->i_sb->s_time_gran);
732 if (ia_valid & ATTR_MODE) {
733 umode_t mode = attr->ia_mode;
734
735 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
736 mode &= ~S_ISGID;
737 set_acl_inode(inode, mode);
738 }
739}
740#else
741#define __setattr_copy setattr_copy
742#endif
743
744int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
745{
746 struct inode *inode = d_inode(dentry);
747 int err;
748 bool size_changed = false;
749
750 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
751 return -EIO;
752
753 err = setattr_prepare(dentry, attr);
754 if (err)
755 return err;
756
757 err = fscrypt_prepare_setattr(dentry, attr);
758 if (err)
759 return err;
760
761 if (is_quota_modification(inode, attr)) {
762 err = dquot_initialize(inode);
763 if (err)
764 return err;
765 }
766 if ((attr->ia_valid & ATTR_UID &&
767 !uid_eq(attr->ia_uid, inode->i_uid)) ||
768 (attr->ia_valid & ATTR_GID &&
769 !gid_eq(attr->ia_gid, inode->i_gid))) {
770 err = dquot_transfer(inode, attr);
771 if (err)
772 return err;
773 }
774
775 if (attr->ia_valid & ATTR_SIZE) {
776 if (attr->ia_size <= i_size_read(inode)) {
777 down_write(&F2FS_I(inode)->i_mmap_sem);
778 truncate_setsize(inode, attr->ia_size);
779 err = f2fs_truncate(inode);
780 up_write(&F2FS_I(inode)->i_mmap_sem);
781 if (err)
782 return err;
783 } else {
784
785
786
787
788 down_write(&F2FS_I(inode)->i_mmap_sem);
789 truncate_setsize(inode, attr->ia_size);
790 up_write(&F2FS_I(inode)->i_mmap_sem);
791
792
793 if (!f2fs_may_inline_data(inode)) {
794 err = f2fs_convert_inline_inode(inode);
795 if (err)
796 return err;
797 }
798 inode->i_mtime = inode->i_ctime = current_time(inode);
799 }
800
801 down_write(&F2FS_I(inode)->i_sem);
802 F2FS_I(inode)->last_disk_size = i_size_read(inode);
803 up_write(&F2FS_I(inode)->i_sem);
804
805 size_changed = true;
806 }
807
808 __setattr_copy(inode, attr);
809
810 if (attr->ia_valid & ATTR_MODE) {
811 err = posix_acl_chmod(inode, get_inode_mode(inode));
812 if (err || is_inode_flag_set(inode, FI_ACL_MODE)) {
813 inode->i_mode = F2FS_I(inode)->i_acl_mode;
814 clear_inode_flag(inode, FI_ACL_MODE);
815 }
816 }
817
818
819 f2fs_mark_inode_dirty_sync(inode, size_changed);
820
821
822 f2fs_balance_fs(F2FS_I_SB(inode), true);
823
824 return err;
825}
826
827const struct inode_operations f2fs_file_inode_operations = {
828 .getattr = f2fs_getattr,
829 .setattr = f2fs_setattr,
830 .get_acl = f2fs_get_acl,
831 .set_acl = f2fs_set_acl,
832#ifdef CONFIG_F2FS_FS_XATTR
833 .listxattr = f2fs_listxattr,
834#endif
835 .fiemap = f2fs_fiemap,
836};
837
838static int fill_zero(struct inode *inode, pgoff_t index,
839 loff_t start, loff_t len)
840{
841 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
842 struct page *page;
843
844 if (!len)
845 return 0;
846
847 f2fs_balance_fs(sbi, true);
848
849 f2fs_lock_op(sbi);
850 page = get_new_data_page(inode, NULL, index, false);
851 f2fs_unlock_op(sbi);
852
853 if (IS_ERR(page))
854 return PTR_ERR(page);
855
856 f2fs_wait_on_page_writeback(page, DATA, true);
857 zero_user(page, start, len);
858 set_page_dirty(page);
859 f2fs_put_page(page, 1);
860 return 0;
861}
862
863int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
864{
865 int err;
866
867 while (pg_start < pg_end) {
868 struct dnode_of_data dn;
869 pgoff_t end_offset, count;
870
871 set_new_dnode(&dn, inode, NULL, NULL, 0);
872 err = get_dnode_of_data(&dn, pg_start, LOOKUP_NODE);
873 if (err) {
874 if (err == -ENOENT) {
875 pg_start = get_next_page_offset(&dn, pg_start);
876 continue;
877 }
878 return err;
879 }
880
881 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
882 count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
883
884 f2fs_bug_on(F2FS_I_SB(inode), count == 0 || count > end_offset);
885
886 truncate_data_blocks_range(&dn, count);
887 f2fs_put_dnode(&dn);
888
889 pg_start += count;
890 }
891 return 0;
892}
893
894static int punch_hole(struct inode *inode, loff_t offset, loff_t len)
895{
896 pgoff_t pg_start, pg_end;
897 loff_t off_start, off_end;
898 int ret;
899
900 ret = f2fs_convert_inline_inode(inode);
901 if (ret)
902 return ret;
903
904 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
905 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
906
907 off_start = offset & (PAGE_SIZE - 1);
908 off_end = (offset + len) & (PAGE_SIZE - 1);
909
910 if (pg_start == pg_end) {
911 ret = fill_zero(inode, pg_start, off_start,
912 off_end - off_start);
913 if (ret)
914 return ret;
915 } else {
916 if (off_start) {
917 ret = fill_zero(inode, pg_start++, off_start,
918 PAGE_SIZE - off_start);
919 if (ret)
920 return ret;
921 }
922 if (off_end) {
923 ret = fill_zero(inode, pg_end, 0, off_end);
924 if (ret)
925 return ret;
926 }
927
928 if (pg_start < pg_end) {
929 struct address_space *mapping = inode->i_mapping;
930 loff_t blk_start, blk_end;
931 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
932
933 f2fs_balance_fs(sbi, true);
934
935 blk_start = (loff_t)pg_start << PAGE_SHIFT;
936 blk_end = (loff_t)pg_end << PAGE_SHIFT;
937 down_write(&F2FS_I(inode)->i_mmap_sem);
938 truncate_inode_pages_range(mapping, blk_start,
939 blk_end - 1);
940
941 f2fs_lock_op(sbi);
942 ret = truncate_hole(inode, pg_start, pg_end);
943 f2fs_unlock_op(sbi);
944 up_write(&F2FS_I(inode)->i_mmap_sem);
945 }
946 }
947
948 return ret;
949}
950
951static int __read_out_blkaddrs(struct inode *inode, block_t *blkaddr,
952 int *do_replace, pgoff_t off, pgoff_t len)
953{
954 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
955 struct dnode_of_data dn;
956 int ret, done, i;
957
958next_dnode:
959 set_new_dnode(&dn, inode, NULL, NULL, 0);
960 ret = get_dnode_of_data(&dn, off, LOOKUP_NODE_RA);
961 if (ret && ret != -ENOENT) {
962 return ret;
963 } else if (ret == -ENOENT) {
964 if (dn.max_level == 0)
965 return -ENOENT;
966 done = min((pgoff_t)ADDRS_PER_BLOCK - dn.ofs_in_node, len);
967 blkaddr += done;
968 do_replace += done;
969 goto next;
970 }
971
972 done = min((pgoff_t)ADDRS_PER_PAGE(dn.node_page, inode) -
973 dn.ofs_in_node, len);
974 for (i = 0; i < done; i++, blkaddr++, do_replace++, dn.ofs_in_node++) {
975 *blkaddr = datablock_addr(dn.inode,
976 dn.node_page, dn.ofs_in_node);
977 if (!is_checkpointed_data(sbi, *blkaddr)) {
978
979 if (test_opt(sbi, LFS)) {
980 f2fs_put_dnode(&dn);
981 return -ENOTSUPP;
982 }
983
984
985 f2fs_update_data_blkaddr(&dn, NULL_ADDR);
986 *do_replace = 1;
987 }
988 }
989 f2fs_put_dnode(&dn);
990next:
991 len -= done;
992 off += done;
993 if (len)
994 goto next_dnode;
995 return 0;
996}
997
998static int __roll_back_blkaddrs(struct inode *inode, block_t *blkaddr,
999 int *do_replace, pgoff_t off, int len)
1000{
1001 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1002 struct dnode_of_data dn;
1003 int ret, i;
1004
1005 for (i = 0; i < len; i++, do_replace++, blkaddr++) {
1006 if (*do_replace == 0)
1007 continue;
1008
1009 set_new_dnode(&dn, inode, NULL, NULL, 0);
1010 ret = get_dnode_of_data(&dn, off + i, LOOKUP_NODE_RA);
1011 if (ret) {
1012 dec_valid_block_count(sbi, inode, 1);
1013 invalidate_blocks(sbi, *blkaddr);
1014 } else {
1015 f2fs_update_data_blkaddr(&dn, *blkaddr);
1016 }
1017 f2fs_put_dnode(&dn);
1018 }
1019 return 0;
1020}
1021
1022static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
1023 block_t *blkaddr, int *do_replace,
1024 pgoff_t src, pgoff_t dst, pgoff_t len, bool full)
1025{
1026 struct f2fs_sb_info *sbi = F2FS_I_SB(src_inode);
1027 pgoff_t i = 0;
1028 int ret;
1029
1030 while (i < len) {
1031 if (blkaddr[i] == NULL_ADDR && !full) {
1032 i++;
1033 continue;
1034 }
1035
1036 if (do_replace[i] || blkaddr[i] == NULL_ADDR) {
1037 struct dnode_of_data dn;
1038 struct node_info ni;
1039 size_t new_size;
1040 pgoff_t ilen;
1041
1042 set_new_dnode(&dn, dst_inode, NULL, NULL, 0);
1043 ret = get_dnode_of_data(&dn, dst + i, ALLOC_NODE);
1044 if (ret)
1045 return ret;
1046
1047 get_node_info(sbi, dn.nid, &ni);
1048 ilen = min((pgoff_t)
1049 ADDRS_PER_PAGE(dn.node_page, dst_inode) -
1050 dn.ofs_in_node, len - i);
1051 do {
1052 dn.data_blkaddr = datablock_addr(dn.inode,
1053 dn.node_page, dn.ofs_in_node);
1054 truncate_data_blocks_range(&dn, 1);
1055
1056 if (do_replace[i]) {
1057 f2fs_i_blocks_write(src_inode,
1058 1, false, false);
1059 f2fs_i_blocks_write(dst_inode,
1060 1, true, false);
1061 f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
1062 blkaddr[i], ni.version, true, false);
1063
1064 do_replace[i] = 0;
1065 }
1066 dn.ofs_in_node++;
1067 i++;
1068 new_size = (dst + i) << PAGE_SHIFT;
1069 if (dst_inode->i_size < new_size)
1070 f2fs_i_size_write(dst_inode, new_size);
1071 } while (--ilen && (do_replace[i] || blkaddr[i] == NULL_ADDR));
1072
1073 f2fs_put_dnode(&dn);
1074 } else {
1075 struct page *psrc, *pdst;
1076
1077 psrc = get_lock_data_page(src_inode, src + i, true);
1078 if (IS_ERR(psrc))
1079 return PTR_ERR(psrc);
1080 pdst = get_new_data_page(dst_inode, NULL, dst + i,
1081 true);
1082 if (IS_ERR(pdst)) {
1083 f2fs_put_page(psrc, 1);
1084 return PTR_ERR(pdst);
1085 }
1086 f2fs_copy_page(psrc, pdst);
1087 set_page_dirty(pdst);
1088 f2fs_put_page(pdst, 1);
1089 f2fs_put_page(psrc, 1);
1090
1091 ret = truncate_hole(src_inode, src + i, src + i + 1);
1092 if (ret)
1093 return ret;
1094 i++;
1095 }
1096 }
1097 return 0;
1098}
1099
1100static int __exchange_data_block(struct inode *src_inode,
1101 struct inode *dst_inode, pgoff_t src, pgoff_t dst,
1102 pgoff_t len, bool full)
1103{
1104 block_t *src_blkaddr;
1105 int *do_replace;
1106 pgoff_t olen;
1107 int ret;
1108
1109 while (len) {
1110 olen = min((pgoff_t)4 * ADDRS_PER_BLOCK, len);
1111
1112 src_blkaddr = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1113 sizeof(block_t) * olen, GFP_KERNEL);
1114 if (!src_blkaddr)
1115 return -ENOMEM;
1116
1117 do_replace = f2fs_kvzalloc(F2FS_I_SB(src_inode),
1118 sizeof(int) * olen, GFP_KERNEL);
1119 if (!do_replace) {
1120 kvfree(src_blkaddr);
1121 return -ENOMEM;
1122 }
1123
1124 ret = __read_out_blkaddrs(src_inode, src_blkaddr,
1125 do_replace, src, olen);
1126 if (ret)
1127 goto roll_back;
1128
1129 ret = __clone_blkaddrs(src_inode, dst_inode, src_blkaddr,
1130 do_replace, src, dst, olen, full);
1131 if (ret)
1132 goto roll_back;
1133
1134 src += olen;
1135 dst += olen;
1136 len -= olen;
1137
1138 kvfree(src_blkaddr);
1139 kvfree(do_replace);
1140 }
1141 return 0;
1142
1143roll_back:
1144 __roll_back_blkaddrs(src_inode, src_blkaddr, do_replace, src, len);
1145 kvfree(src_blkaddr);
1146 kvfree(do_replace);
1147 return ret;
1148}
1149
1150static int f2fs_do_collapse(struct inode *inode, pgoff_t start, pgoff_t end)
1151{
1152 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1153 pgoff_t nrpages = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1154 int ret;
1155
1156 f2fs_balance_fs(sbi, true);
1157 f2fs_lock_op(sbi);
1158
1159 f2fs_drop_extent_tree(inode);
1160
1161 ret = __exchange_data_block(inode, inode, end, start, nrpages - end, true);
1162 f2fs_unlock_op(sbi);
1163 return ret;
1164}
1165
1166static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)
1167{
1168 pgoff_t pg_start, pg_end;
1169 loff_t new_size;
1170 int ret;
1171
1172 if (offset + len >= i_size_read(inode))
1173 return -EINVAL;
1174
1175
1176 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1177 return -EINVAL;
1178
1179 ret = f2fs_convert_inline_inode(inode);
1180 if (ret)
1181 return ret;
1182
1183 pg_start = offset >> PAGE_SHIFT;
1184 pg_end = (offset + len) >> PAGE_SHIFT;
1185
1186
1187 down_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1188
1189 down_write(&F2FS_I(inode)->i_mmap_sem);
1190
1191 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1192 if (ret)
1193 goto out_unlock;
1194
1195 truncate_pagecache(inode, offset);
1196
1197 ret = f2fs_do_collapse(inode, pg_start, pg_end);
1198 if (ret)
1199 goto out_unlock;
1200
1201
1202 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1203 truncate_pagecache(inode, offset);
1204
1205 new_size = i_size_read(inode) - len;
1206 truncate_pagecache(inode, new_size);
1207
1208 ret = truncate_blocks(inode, new_size, true);
1209 if (!ret)
1210 f2fs_i_size_write(inode, new_size);
1211out_unlock:
1212 up_write(&F2FS_I(inode)->i_mmap_sem);
1213 up_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1214 return ret;
1215}
1216
1217static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
1218 pgoff_t end)
1219{
1220 struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
1221 pgoff_t index = start;
1222 unsigned int ofs_in_node = dn->ofs_in_node;
1223 blkcnt_t count = 0;
1224 int ret;
1225
1226 for (; index < end; index++, dn->ofs_in_node++) {
1227 if (datablock_addr(dn->inode, dn->node_page,
1228 dn->ofs_in_node) == NULL_ADDR)
1229 count++;
1230 }
1231
1232 dn->ofs_in_node = ofs_in_node;
1233 ret = reserve_new_blocks(dn, count);
1234 if (ret)
1235 return ret;
1236
1237 dn->ofs_in_node = ofs_in_node;
1238 for (index = start; index < end; index++, dn->ofs_in_node++) {
1239 dn->data_blkaddr = datablock_addr(dn->inode,
1240 dn->node_page, dn->ofs_in_node);
1241
1242
1243
1244
1245 if (dn->data_blkaddr == NULL_ADDR) {
1246 ret = -ENOSPC;
1247 break;
1248 }
1249 if (dn->data_blkaddr != NEW_ADDR) {
1250 invalidate_blocks(sbi, dn->data_blkaddr);
1251 dn->data_blkaddr = NEW_ADDR;
1252 set_data_blkaddr(dn);
1253 }
1254 }
1255
1256 f2fs_update_extent_cache_range(dn, start, 0, index - start);
1257
1258 return ret;
1259}
1260
1261static int f2fs_zero_range(struct inode *inode, loff_t offset, loff_t len,
1262 int mode)
1263{
1264 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1265 struct address_space *mapping = inode->i_mapping;
1266 pgoff_t index, pg_start, pg_end;
1267 loff_t new_size = i_size_read(inode);
1268 loff_t off_start, off_end;
1269 int ret = 0;
1270
1271 ret = inode_newsize_ok(inode, (len + offset));
1272 if (ret)
1273 return ret;
1274
1275 ret = f2fs_convert_inline_inode(inode);
1276 if (ret)
1277 return ret;
1278
1279 down_write(&F2FS_I(inode)->i_mmap_sem);
1280 ret = filemap_write_and_wait_range(mapping, offset, offset + len - 1);
1281 if (ret)
1282 goto out_sem;
1283
1284 truncate_pagecache_range(inode, offset, offset + len - 1);
1285
1286 pg_start = ((unsigned long long) offset) >> PAGE_SHIFT;
1287 pg_end = ((unsigned long long) offset + len) >> PAGE_SHIFT;
1288
1289 off_start = offset & (PAGE_SIZE - 1);
1290 off_end = (offset + len) & (PAGE_SIZE - 1);
1291
1292 if (pg_start == pg_end) {
1293 ret = fill_zero(inode, pg_start, off_start,
1294 off_end - off_start);
1295 if (ret)
1296 goto out_sem;
1297
1298 new_size = max_t(loff_t, new_size, offset + len);
1299 } else {
1300 if (off_start) {
1301 ret = fill_zero(inode, pg_start++, off_start,
1302 PAGE_SIZE - off_start);
1303 if (ret)
1304 goto out_sem;
1305
1306 new_size = max_t(loff_t, new_size,
1307 (loff_t)pg_start << PAGE_SHIFT);
1308 }
1309
1310 for (index = pg_start; index < pg_end;) {
1311 struct dnode_of_data dn;
1312 unsigned int end_offset;
1313 pgoff_t end;
1314
1315 f2fs_lock_op(sbi);
1316
1317 set_new_dnode(&dn, inode, NULL, NULL, 0);
1318 ret = get_dnode_of_data(&dn, index, ALLOC_NODE);
1319 if (ret) {
1320 f2fs_unlock_op(sbi);
1321 goto out;
1322 }
1323
1324 end_offset = ADDRS_PER_PAGE(dn.node_page, inode);
1325 end = min(pg_end, end_offset - dn.ofs_in_node + index);
1326
1327 ret = f2fs_do_zero_range(&dn, index, end);
1328 f2fs_put_dnode(&dn);
1329 f2fs_unlock_op(sbi);
1330
1331 f2fs_balance_fs(sbi, dn.node_changed);
1332
1333 if (ret)
1334 goto out;
1335
1336 index = end;
1337 new_size = max_t(loff_t, new_size,
1338 (loff_t)index << PAGE_SHIFT);
1339 }
1340
1341 if (off_end) {
1342 ret = fill_zero(inode, pg_end, 0, off_end);
1343 if (ret)
1344 goto out;
1345
1346 new_size = max_t(loff_t, new_size, offset + len);
1347 }
1348 }
1349
1350out:
1351 if (!(mode & FALLOC_FL_KEEP_SIZE) && i_size_read(inode) < new_size)
1352 f2fs_i_size_write(inode, new_size);
1353out_sem:
1354 up_write(&F2FS_I(inode)->i_mmap_sem);
1355
1356 return ret;
1357}
1358
1359static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)
1360{
1361 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1362 pgoff_t nr, pg_start, pg_end, delta, idx;
1363 loff_t new_size;
1364 int ret = 0;
1365
1366 new_size = i_size_read(inode) + len;
1367 ret = inode_newsize_ok(inode, new_size);
1368 if (ret)
1369 return ret;
1370
1371 if (offset >= i_size_read(inode))
1372 return -EINVAL;
1373
1374
1375 if (offset & (F2FS_BLKSIZE - 1) || len & (F2FS_BLKSIZE - 1))
1376 return -EINVAL;
1377
1378 ret = f2fs_convert_inline_inode(inode);
1379 if (ret)
1380 return ret;
1381
1382 f2fs_balance_fs(sbi, true);
1383
1384
1385 down_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1386
1387 down_write(&F2FS_I(inode)->i_mmap_sem);
1388 ret = truncate_blocks(inode, i_size_read(inode), true);
1389 if (ret)
1390 goto out;
1391
1392
1393 ret = filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1394 if (ret)
1395 goto out;
1396
1397 truncate_pagecache(inode, offset);
1398
1399 pg_start = offset >> PAGE_SHIFT;
1400 pg_end = (offset + len) >> PAGE_SHIFT;
1401 delta = pg_end - pg_start;
1402 idx = (i_size_read(inode) + PAGE_SIZE - 1) / PAGE_SIZE;
1403
1404 while (!ret && idx > pg_start) {
1405 nr = idx - pg_start;
1406 if (nr > delta)
1407 nr = delta;
1408 idx -= nr;
1409
1410 f2fs_lock_op(sbi);
1411 f2fs_drop_extent_tree(inode);
1412
1413 ret = __exchange_data_block(inode, inode, idx,
1414 idx + delta, nr, false);
1415 f2fs_unlock_op(sbi);
1416 }
1417
1418
1419 filemap_write_and_wait_range(inode->i_mapping, offset, LLONG_MAX);
1420 truncate_pagecache(inode, offset);
1421
1422 if (!ret)
1423 f2fs_i_size_write(inode, new_size);
1424out:
1425 up_write(&F2FS_I(inode)->i_mmap_sem);
1426 up_write(&F2FS_I(inode)->dio_rwsem[WRITE]);
1427 return ret;
1428}
1429
1430static int expand_inode_data(struct inode *inode, loff_t offset,
1431 loff_t len, int mode)
1432{
1433 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1434 struct f2fs_map_blocks map = { .m_next_pgofs = NULL,
1435 .m_next_extent = NULL, .m_seg_type = NO_CHECK_TYPE };
1436 pgoff_t pg_end;
1437 loff_t new_size = i_size_read(inode);
1438 loff_t off_end;
1439 int err;
1440
1441 err = inode_newsize_ok(inode, (len + offset));
1442 if (err)
1443 return err;
1444
1445 err = f2fs_convert_inline_inode(inode);
1446 if (err)
1447 return err;
1448
1449 f2fs_balance_fs(sbi, true);
1450
1451 pg_end = ((unsigned long long)offset + len) >> PAGE_SHIFT;
1452 off_end = (offset + len) & (PAGE_SIZE - 1);
1453
1454 map.m_lblk = ((unsigned long long)offset) >> PAGE_SHIFT;
1455 map.m_len = pg_end - map.m_lblk;
1456 if (off_end)
1457 map.m_len++;
1458
1459 err = f2fs_map_blocks(inode, &map, 1, F2FS_GET_BLOCK_PRE_AIO);
1460 if (err) {
1461 pgoff_t last_off;
1462
1463 if (!map.m_len)
1464 return err;
1465
1466 last_off = map.m_lblk + map.m_len - 1;
1467
1468
1469 new_size = (last_off == pg_end) ? offset + len:
1470 (loff_t)(last_off + 1) << PAGE_SHIFT;
1471 } else {
1472 new_size = ((loff_t)pg_end << PAGE_SHIFT) + off_end;
1473 }
1474
1475 if (new_size > i_size_read(inode)) {
1476 if (mode & FALLOC_FL_KEEP_SIZE)
1477 file_set_keep_isize(inode);
1478 else
1479 f2fs_i_size_write(inode, new_size);
1480 }
1481
1482 return err;
1483}
1484
1485static long f2fs_fallocate(struct file *file, int mode,
1486 loff_t offset, loff_t len)
1487{
1488 struct inode *inode = file_inode(file);
1489 long ret = 0;
1490
1491 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
1492 return -EIO;
1493
1494
1495 if (!S_ISREG(inode->i_mode))
1496 return -EINVAL;
1497
1498 if (f2fs_encrypted_inode(inode) &&
1499 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)))
1500 return -EOPNOTSUPP;
1501
1502 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
1503 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
1504 FALLOC_FL_INSERT_RANGE))
1505 return -EOPNOTSUPP;
1506
1507 inode_lock(inode);
1508
1509 if (mode & FALLOC_FL_PUNCH_HOLE) {
1510 if (offset >= inode->i_size)
1511 goto out;
1512
1513 ret = punch_hole(inode, offset, len);
1514 } else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
1515 ret = f2fs_collapse_range(inode, offset, len);
1516 } else if (mode & FALLOC_FL_ZERO_RANGE) {
1517 ret = f2fs_zero_range(inode, offset, len, mode);
1518 } else if (mode & FALLOC_FL_INSERT_RANGE) {
1519 ret = f2fs_insert_range(inode, offset, len);
1520 } else {
1521 ret = expand_inode_data(inode, offset, len, mode);
1522 }
1523
1524 if (!ret) {
1525 inode->i_mtime = inode->i_ctime = current_time(inode);
1526 f2fs_mark_inode_dirty_sync(inode, false);
1527 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1528 }
1529
1530out:
1531 inode_unlock(inode);
1532
1533 trace_f2fs_fallocate(inode, mode, offset, len, ret);
1534 return ret;
1535}
1536
1537static int f2fs_release_file(struct inode *inode, struct file *filp)
1538{
1539
1540
1541
1542
1543 if (!(filp->f_mode & FMODE_WRITE) ||
1544 atomic_read(&inode->i_writecount) != 1)
1545 return 0;
1546
1547
1548 if (f2fs_is_atomic_file(inode))
1549 drop_inmem_pages(inode);
1550 if (f2fs_is_volatile_file(inode)) {
1551 clear_inode_flag(inode, FI_VOLATILE_FILE);
1552 stat_dec_volatile_write(inode);
1553 set_inode_flag(inode, FI_DROP_CACHE);
1554 filemap_fdatawrite(inode->i_mapping);
1555 clear_inode_flag(inode, FI_DROP_CACHE);
1556 }
1557 return 0;
1558}
1559
1560static int f2fs_file_flush(struct file *file, fl_owner_t id)
1561{
1562 struct inode *inode = file_inode(file);
1563
1564
1565
1566
1567
1568
1569
1570 if (f2fs_is_atomic_file(inode) &&
1571 F2FS_I(inode)->inmem_task == current)
1572 drop_inmem_pages(inode);
1573 return 0;
1574}
1575
1576static int f2fs_ioc_getflags(struct file *filp, unsigned long arg)
1577{
1578 struct inode *inode = file_inode(filp);
1579 struct f2fs_inode_info *fi = F2FS_I(inode);
1580 unsigned int flags = fi->i_flags &
1581 (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL);
1582 return put_user(flags, (int __user *)arg);
1583}
1584
1585static int __f2fs_ioc_setflags(struct inode *inode, unsigned int flags)
1586{
1587 struct f2fs_inode_info *fi = F2FS_I(inode);
1588 unsigned int oldflags;
1589
1590
1591 if (IS_NOQUOTA(inode))
1592 return -EPERM;
1593
1594 flags = f2fs_mask_flags(inode->i_mode, flags);
1595
1596 oldflags = fi->i_flags;
1597
1598 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL))
1599 if (!capable(CAP_LINUX_IMMUTABLE))
1600 return -EPERM;
1601
1602 flags = flags & (FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL);
1603 flags |= oldflags & ~(FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL);
1604 fi->i_flags = flags;
1605
1606 if (fi->i_flags & FS_PROJINHERIT_FL)
1607 set_inode_flag(inode, FI_PROJ_INHERIT);
1608 else
1609 clear_inode_flag(inode, FI_PROJ_INHERIT);
1610
1611 inode->i_ctime = current_time(inode);
1612 f2fs_set_inode_flags(inode);
1613 f2fs_mark_inode_dirty_sync(inode, false);
1614 return 0;
1615}
1616
1617static int f2fs_ioc_setflags(struct file *filp, unsigned long arg)
1618{
1619 struct inode *inode = file_inode(filp);
1620 unsigned int flags;
1621 int ret;
1622
1623 if (!inode_owner_or_capable(inode))
1624 return -EACCES;
1625
1626 if (get_user(flags, (int __user *)arg))
1627 return -EFAULT;
1628
1629 ret = mnt_want_write_file(filp);
1630 if (ret)
1631 return ret;
1632
1633 inode_lock(inode);
1634
1635 ret = __f2fs_ioc_setflags(inode, flags);
1636
1637 inode_unlock(inode);
1638 mnt_drop_write_file(filp);
1639 return ret;
1640}
1641
1642static int f2fs_ioc_getversion(struct file *filp, unsigned long arg)
1643{
1644 struct inode *inode = file_inode(filp);
1645
1646 return put_user(inode->i_generation, (int __user *)arg);
1647}
1648
1649static int f2fs_ioc_start_atomic_write(struct file *filp)
1650{
1651 struct inode *inode = file_inode(filp);
1652 int ret;
1653
1654 if (!inode_owner_or_capable(inode))
1655 return -EACCES;
1656
1657 if (!S_ISREG(inode->i_mode))
1658 return -EINVAL;
1659
1660 ret = mnt_want_write_file(filp);
1661 if (ret)
1662 return ret;
1663
1664 inode_lock(inode);
1665
1666 if (f2fs_is_atomic_file(inode))
1667 goto out;
1668
1669 ret = f2fs_convert_inline_inode(inode);
1670 if (ret)
1671 goto out;
1672
1673 set_inode_flag(inode, FI_ATOMIC_FILE);
1674 set_inode_flag(inode, FI_HOT_DATA);
1675 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1676
1677 if (!get_dirty_pages(inode))
1678 goto inc_stat;
1679
1680 f2fs_msg(F2FS_I_SB(inode)->sb, KERN_WARNING,
1681 "Unexpected flush for atomic writes: ino=%lu, npages=%u",
1682 inode->i_ino, get_dirty_pages(inode));
1683 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
1684 if (ret) {
1685 clear_inode_flag(inode, FI_ATOMIC_FILE);
1686 clear_inode_flag(inode, FI_HOT_DATA);
1687 goto out;
1688 }
1689
1690inc_stat:
1691 F2FS_I(inode)->inmem_task = current;
1692 stat_inc_atomic_write(inode);
1693 stat_update_max_atomic_write(inode);
1694out:
1695 inode_unlock(inode);
1696 mnt_drop_write_file(filp);
1697 return ret;
1698}
1699
1700static int f2fs_ioc_commit_atomic_write(struct file *filp)
1701{
1702 struct inode *inode = file_inode(filp);
1703 int ret;
1704
1705 if (!inode_owner_or_capable(inode))
1706 return -EACCES;
1707
1708 ret = mnt_want_write_file(filp);
1709 if (ret)
1710 return ret;
1711
1712 inode_lock(inode);
1713
1714 if (f2fs_is_volatile_file(inode))
1715 goto err_out;
1716
1717 if (f2fs_is_atomic_file(inode)) {
1718 ret = commit_inmem_pages(inode);
1719 if (ret)
1720 goto err_out;
1721
1722 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1723 if (!ret) {
1724 clear_inode_flag(inode, FI_ATOMIC_FILE);
1725 clear_inode_flag(inode, FI_HOT_DATA);
1726 stat_dec_atomic_write(inode);
1727 }
1728 } else {
1729 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 1, false);
1730 }
1731err_out:
1732 inode_unlock(inode);
1733 mnt_drop_write_file(filp);
1734 return ret;
1735}
1736
1737static int f2fs_ioc_start_volatile_write(struct file *filp)
1738{
1739 struct inode *inode = file_inode(filp);
1740 int ret;
1741
1742 if (!inode_owner_or_capable(inode))
1743 return -EACCES;
1744
1745 if (!S_ISREG(inode->i_mode))
1746 return -EINVAL;
1747
1748 ret = mnt_want_write_file(filp);
1749 if (ret)
1750 return ret;
1751
1752 inode_lock(inode);
1753
1754 if (f2fs_is_volatile_file(inode))
1755 goto out;
1756
1757 ret = f2fs_convert_inline_inode(inode);
1758 if (ret)
1759 goto out;
1760
1761 stat_inc_volatile_write(inode);
1762 stat_update_max_volatile_write(inode);
1763
1764 set_inode_flag(inode, FI_VOLATILE_FILE);
1765 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1766out:
1767 inode_unlock(inode);
1768 mnt_drop_write_file(filp);
1769 return ret;
1770}
1771
1772static int f2fs_ioc_release_volatile_write(struct file *filp)
1773{
1774 struct inode *inode = file_inode(filp);
1775 int ret;
1776
1777 if (!inode_owner_or_capable(inode))
1778 return -EACCES;
1779
1780 ret = mnt_want_write_file(filp);
1781 if (ret)
1782 return ret;
1783
1784 inode_lock(inode);
1785
1786 if (!f2fs_is_volatile_file(inode))
1787 goto out;
1788
1789 if (!f2fs_is_first_block_written(inode)) {
1790 ret = truncate_partial_data_page(inode, 0, true);
1791 goto out;
1792 }
1793
1794 ret = punch_hole(inode, 0, F2FS_BLKSIZE);
1795out:
1796 inode_unlock(inode);
1797 mnt_drop_write_file(filp);
1798 return ret;
1799}
1800
1801static int f2fs_ioc_abort_volatile_write(struct file *filp)
1802{
1803 struct inode *inode = file_inode(filp);
1804 int ret;
1805
1806 if (!inode_owner_or_capable(inode))
1807 return -EACCES;
1808
1809 ret = mnt_want_write_file(filp);
1810 if (ret)
1811 return ret;
1812
1813 inode_lock(inode);
1814
1815 if (f2fs_is_atomic_file(inode))
1816 drop_inmem_pages(inode);
1817 if (f2fs_is_volatile_file(inode)) {
1818 clear_inode_flag(inode, FI_VOLATILE_FILE);
1819 stat_dec_volatile_write(inode);
1820 ret = f2fs_do_sync_file(filp, 0, LLONG_MAX, 0, true);
1821 }
1822
1823 inode_unlock(inode);
1824
1825 mnt_drop_write_file(filp);
1826 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1827 return ret;
1828}
1829
1830static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
1831{
1832 struct inode *inode = file_inode(filp);
1833 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1834 struct super_block *sb = sbi->sb;
1835 __u32 in;
1836 int ret;
1837
1838 if (!capable(CAP_SYS_ADMIN))
1839 return -EPERM;
1840
1841 if (get_user(in, (__u32 __user *)arg))
1842 return -EFAULT;
1843
1844 ret = mnt_want_write_file(filp);
1845 if (ret)
1846 return ret;
1847
1848 switch (in) {
1849 case F2FS_GOING_DOWN_FULLSYNC:
1850 sb = freeze_bdev(sb->s_bdev);
1851 if (IS_ERR(sb)) {
1852 ret = PTR_ERR(sb);
1853 goto out;
1854 }
1855 if (sb) {
1856 f2fs_stop_checkpoint(sbi, false);
1857 thaw_bdev(sb->s_bdev, sb);
1858 }
1859 break;
1860 case F2FS_GOING_DOWN_METASYNC:
1861
1862 ret = f2fs_sync_fs(sb, 1);
1863 if (ret)
1864 goto out;
1865 f2fs_stop_checkpoint(sbi, false);
1866 break;
1867 case F2FS_GOING_DOWN_NOSYNC:
1868 f2fs_stop_checkpoint(sbi, false);
1869 break;
1870 case F2FS_GOING_DOWN_METAFLUSH:
1871 sync_meta_pages(sbi, META, LONG_MAX, FS_META_IO);
1872 f2fs_stop_checkpoint(sbi, false);
1873 break;
1874 default:
1875 ret = -EINVAL;
1876 goto out;
1877 }
1878
1879 stop_gc_thread(sbi);
1880 stop_discard_thread(sbi);
1881
1882 drop_discard_cmd(sbi);
1883 clear_opt(sbi, DISCARD);
1884
1885 f2fs_update_time(sbi, REQ_TIME);
1886out:
1887 mnt_drop_write_file(filp);
1888 return ret;
1889}
1890
1891static int f2fs_ioc_fitrim(struct file *filp, unsigned long arg)
1892{
1893 struct inode *inode = file_inode(filp);
1894 struct super_block *sb = inode->i_sb;
1895 struct request_queue *q = bdev_get_queue(sb->s_bdev);
1896 struct fstrim_range range;
1897 int ret;
1898
1899 if (!capable(CAP_SYS_ADMIN))
1900 return -EPERM;
1901
1902 if (!blk_queue_discard(q))
1903 return -EOPNOTSUPP;
1904
1905 if (copy_from_user(&range, (struct fstrim_range __user *)arg,
1906 sizeof(range)))
1907 return -EFAULT;
1908
1909 ret = mnt_want_write_file(filp);
1910 if (ret)
1911 return ret;
1912
1913 range.minlen = max((unsigned int)range.minlen,
1914 q->limits.discard_granularity);
1915 ret = f2fs_trim_fs(F2FS_SB(sb), &range);
1916 mnt_drop_write_file(filp);
1917 if (ret < 0)
1918 return ret;
1919
1920 if (copy_to_user((struct fstrim_range __user *)arg, &range,
1921 sizeof(range)))
1922 return -EFAULT;
1923 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1924 return 0;
1925}
1926
1927static bool uuid_is_nonzero(__u8 u[16])
1928{
1929 int i;
1930
1931 for (i = 0; i < 16; i++)
1932 if (u[i])
1933 return true;
1934 return false;
1935}
1936
1937static int f2fs_ioc_set_encryption_policy(struct file *filp, unsigned long arg)
1938{
1939 struct inode *inode = file_inode(filp);
1940
1941 if (!f2fs_sb_has_crypto(inode->i_sb))
1942 return -EOPNOTSUPP;
1943
1944 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
1945
1946 return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
1947}
1948
1949static int f2fs_ioc_get_encryption_policy(struct file *filp, unsigned long arg)
1950{
1951 if (!f2fs_sb_has_crypto(file_inode(filp)->i_sb))
1952 return -EOPNOTSUPP;
1953 return fscrypt_ioctl_get_policy(filp, (void __user *)arg);
1954}
1955
1956static int f2fs_ioc_get_encryption_pwsalt(struct file *filp, unsigned long arg)
1957{
1958 struct inode *inode = file_inode(filp);
1959 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1960 int err;
1961
1962 if (!f2fs_sb_has_crypto(inode->i_sb))
1963 return -EOPNOTSUPP;
1964
1965 if (uuid_is_nonzero(sbi->raw_super->encrypt_pw_salt))
1966 goto got_it;
1967
1968 err = mnt_want_write_file(filp);
1969 if (err)
1970 return err;
1971
1972
1973 generate_random_uuid(sbi->raw_super->encrypt_pw_salt);
1974
1975 err = f2fs_commit_super(sbi, false);
1976 if (err) {
1977
1978 memset(sbi->raw_super->encrypt_pw_salt, 0, 16);
1979 mnt_drop_write_file(filp);
1980 return err;
1981 }
1982 mnt_drop_write_file(filp);
1983got_it:
1984 if (copy_to_user((__u8 __user *)arg, sbi->raw_super->encrypt_pw_salt,
1985 16))
1986 return -EFAULT;
1987 return 0;
1988}
1989
1990static int f2fs_ioc_gc(struct file *filp, unsigned long arg)
1991{
1992 struct inode *inode = file_inode(filp);
1993 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1994 __u32 sync;
1995 int ret;
1996
1997 if (!capable(CAP_SYS_ADMIN))
1998 return -EPERM;
1999
2000 if (get_user(sync, (__u32 __user *)arg))
2001 return -EFAULT;
2002
2003 if (f2fs_readonly(sbi->sb))
2004 return -EROFS;
2005
2006 ret = mnt_want_write_file(filp);
2007 if (ret)
2008 return ret;
2009
2010 if (!sync) {
2011 if (!mutex_trylock(&sbi->gc_mutex)) {
2012 ret = -EBUSY;
2013 goto out;
2014 }
2015 } else {
2016 mutex_lock(&sbi->gc_mutex);
2017 }
2018
2019 ret = f2fs_gc(sbi, sync, true, NULL_SEGNO);
2020out:
2021 mnt_drop_write_file(filp);
2022 return ret;
2023}
2024
2025static int f2fs_ioc_gc_range(struct file *filp, unsigned long arg)
2026{
2027 struct inode *inode = file_inode(filp);
2028 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2029 struct f2fs_gc_range range;
2030 u64 end;
2031 int ret;
2032
2033 if (!capable(CAP_SYS_ADMIN))
2034 return -EPERM;
2035
2036 if (copy_from_user(&range, (struct f2fs_gc_range __user *)arg,
2037 sizeof(range)))
2038 return -EFAULT;
2039
2040 if (f2fs_readonly(sbi->sb))
2041 return -EROFS;
2042
2043 ret = mnt_want_write_file(filp);
2044 if (ret)
2045 return ret;
2046
2047 end = range.start + range.len;
2048 if (range.start < MAIN_BLKADDR(sbi) || end >= MAX_BLKADDR(sbi))
2049 return -EINVAL;
2050do_more:
2051 if (!range.sync) {
2052 if (!mutex_trylock(&sbi->gc_mutex)) {
2053 ret = -EBUSY;
2054 goto out;
2055 }
2056 } else {
2057 mutex_lock(&sbi->gc_mutex);
2058 }
2059
2060 ret = f2fs_gc(sbi, range.sync, true, GET_SEGNO(sbi, range.start));
2061 range.start += sbi->blocks_per_seg;
2062 if (range.start <= end)
2063 goto do_more;
2064out:
2065 mnt_drop_write_file(filp);
2066 return ret;
2067}
2068
2069static int f2fs_ioc_write_checkpoint(struct file *filp, unsigned long arg)
2070{
2071 struct inode *inode = file_inode(filp);
2072 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2073 int ret;
2074
2075 if (!capable(CAP_SYS_ADMIN))
2076 return -EPERM;
2077
2078 if (f2fs_readonly(sbi->sb))
2079 return -EROFS;
2080
2081 ret = mnt_want_write_file(filp);
2082 if (ret)
2083 return ret;
2084
2085 ret = f2fs_sync_fs(sbi->sb, 1);
2086
2087 mnt_drop_write_file(filp);
2088 return ret;
2089}
2090
2091static int f2fs_defragment_range(struct f2fs_sb_info *sbi,
2092 struct file *filp,
2093 struct f2fs_defragment *range)
2094{
2095 struct inode *inode = file_inode(filp);
2096 struct f2fs_map_blocks map = { .m_next_extent = NULL,
2097 .m_seg_type = NO_CHECK_TYPE };
2098 struct extent_info ei = {0,0,0};
2099 pgoff_t pg_start, pg_end, next_pgofs;
2100 unsigned int blk_per_seg = sbi->blocks_per_seg;
2101 unsigned int total = 0, sec_num;
2102 block_t blk_end = 0;
2103 bool fragmented = false;
2104 int err;
2105
2106
2107 if (should_update_inplace(inode, NULL))
2108 return -EINVAL;
2109
2110 pg_start = range->start >> PAGE_SHIFT;
2111 pg_end = (range->start + range->len) >> PAGE_SHIFT;
2112
2113 f2fs_balance_fs(sbi, true);
2114
2115 inode_lock(inode);
2116
2117
2118 err = filemap_write_and_wait_range(inode->i_mapping, range->start,
2119 range->start + range->len - 1);
2120 if (err)
2121 goto out;
2122
2123
2124
2125
2126
2127 if (f2fs_lookup_extent_cache(inode, pg_start, &ei)) {
2128 if (ei.fofs + ei.len >= pg_end)
2129 goto out;
2130 }
2131
2132 map.m_lblk = pg_start;
2133 map.m_next_pgofs = &next_pgofs;
2134
2135
2136
2137
2138
2139
2140 while (map.m_lblk < pg_end) {
2141 map.m_len = pg_end - map.m_lblk;
2142 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2143 if (err)
2144 goto out;
2145
2146 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2147 map.m_lblk = next_pgofs;
2148 continue;
2149 }
2150
2151 if (blk_end && blk_end != map.m_pblk)
2152 fragmented = true;
2153
2154
2155 total += map.m_len;
2156
2157 blk_end = map.m_pblk + map.m_len;
2158
2159 map.m_lblk += map.m_len;
2160 }
2161
2162 if (!fragmented)
2163 goto out;
2164
2165 sec_num = (total + BLKS_PER_SEC(sbi) - 1) / BLKS_PER_SEC(sbi);
2166
2167
2168
2169
2170
2171
2172 if (has_not_enough_free_secs(sbi, 0, sec_num)) {
2173 err = -EAGAIN;
2174 goto out;
2175 }
2176
2177 map.m_lblk = pg_start;
2178 map.m_len = pg_end - pg_start;
2179 total = 0;
2180
2181 while (map.m_lblk < pg_end) {
2182 pgoff_t idx;
2183 int cnt = 0;
2184
2185do_map:
2186 map.m_len = pg_end - map.m_lblk;
2187 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_DEFAULT);
2188 if (err)
2189 goto clear_out;
2190
2191 if (!(map.m_flags & F2FS_MAP_FLAGS)) {
2192 map.m_lblk = next_pgofs;
2193 continue;
2194 }
2195
2196 set_inode_flag(inode, FI_DO_DEFRAG);
2197
2198 idx = map.m_lblk;
2199 while (idx < map.m_lblk + map.m_len && cnt < blk_per_seg) {
2200 struct page *page;
2201
2202 page = get_lock_data_page(inode, idx, true);
2203 if (IS_ERR(page)) {
2204 err = PTR_ERR(page);
2205 goto clear_out;
2206 }
2207
2208 set_page_dirty(page);
2209 f2fs_put_page(page, 1);
2210
2211 idx++;
2212 cnt++;
2213 total++;
2214 }
2215
2216 map.m_lblk = idx;
2217
2218 if (idx < pg_end && cnt < blk_per_seg)
2219 goto do_map;
2220
2221 clear_inode_flag(inode, FI_DO_DEFRAG);
2222
2223 err = filemap_fdatawrite(inode->i_mapping);
2224 if (err)
2225 goto out;
2226 }
2227clear_out:
2228 clear_inode_flag(inode, FI_DO_DEFRAG);
2229out:
2230 inode_unlock(inode);
2231 if (!err)
2232 range->len = (u64)total << PAGE_SHIFT;
2233 return err;
2234}
2235
2236static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)
2237{
2238 struct inode *inode = file_inode(filp);
2239 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2240 struct f2fs_defragment range;
2241 int err;
2242
2243 if (!capable(CAP_SYS_ADMIN))
2244 return -EPERM;
2245
2246 if (!S_ISREG(inode->i_mode) || f2fs_is_atomic_file(inode))
2247 return -EINVAL;
2248
2249 if (f2fs_readonly(sbi->sb))
2250 return -EROFS;
2251
2252 if (copy_from_user(&range, (struct f2fs_defragment __user *)arg,
2253 sizeof(range)))
2254 return -EFAULT;
2255
2256
2257 if (range.start & (F2FS_BLKSIZE - 1) || range.len & (F2FS_BLKSIZE - 1))
2258 return -EINVAL;
2259
2260 if (unlikely((range.start + range.len) >> PAGE_SHIFT >
2261 sbi->max_file_blocks))
2262 return -EINVAL;
2263
2264 err = mnt_want_write_file(filp);
2265 if (err)
2266 return err;
2267
2268 err = f2fs_defragment_range(sbi, filp, &range);
2269 mnt_drop_write_file(filp);
2270
2271 f2fs_update_time(sbi, REQ_TIME);
2272 if (err < 0)
2273 return err;
2274
2275 if (copy_to_user((struct f2fs_defragment __user *)arg, &range,
2276 sizeof(range)))
2277 return -EFAULT;
2278
2279 return 0;
2280}
2281
2282static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
2283 struct file *file_out, loff_t pos_out, size_t len)
2284{
2285 struct inode *src = file_inode(file_in);
2286 struct inode *dst = file_inode(file_out);
2287 struct f2fs_sb_info *sbi = F2FS_I_SB(src);
2288 size_t olen = len, dst_max_i_size = 0;
2289 size_t dst_osize;
2290 int ret;
2291
2292 if (file_in->f_path.mnt != file_out->f_path.mnt ||
2293 src->i_sb != dst->i_sb)
2294 return -EXDEV;
2295
2296 if (unlikely(f2fs_readonly(src->i_sb)))
2297 return -EROFS;
2298
2299 if (!S_ISREG(src->i_mode) || !S_ISREG(dst->i_mode))
2300 return -EINVAL;
2301
2302 if (f2fs_encrypted_inode(src) || f2fs_encrypted_inode(dst))
2303 return -EOPNOTSUPP;
2304
2305 if (src == dst) {
2306 if (pos_in == pos_out)
2307 return 0;
2308 if (pos_out > pos_in && pos_out < pos_in + len)
2309 return -EINVAL;
2310 }
2311
2312 inode_lock(src);
2313 down_write(&F2FS_I(src)->dio_rwsem[WRITE]);
2314 if (src != dst) {
2315 ret = -EBUSY;
2316 if (!inode_trylock(dst))
2317 goto out;
2318 if (!down_write_trylock(&F2FS_I(dst)->dio_rwsem[WRITE])) {
2319 inode_unlock(dst);
2320 goto out;
2321 }
2322 }
2323
2324 ret = -EINVAL;
2325 if (pos_in + len > src->i_size || pos_in + len < pos_in)
2326 goto out_unlock;
2327 if (len == 0)
2328 olen = len = src->i_size - pos_in;
2329 if (pos_in + len == src->i_size)
2330 len = ALIGN(src->i_size, F2FS_BLKSIZE) - pos_in;
2331 if (len == 0) {
2332 ret = 0;
2333 goto out_unlock;
2334 }
2335
2336 dst_osize = dst->i_size;
2337 if (pos_out + olen > dst->i_size)
2338 dst_max_i_size = pos_out + olen;
2339
2340
2341 if (!IS_ALIGNED(pos_in, F2FS_BLKSIZE) ||
2342 !IS_ALIGNED(pos_in + len, F2FS_BLKSIZE) ||
2343 !IS_ALIGNED(pos_out, F2FS_BLKSIZE))
2344 goto out_unlock;
2345
2346 ret = f2fs_convert_inline_inode(src);
2347 if (ret)
2348 goto out_unlock;
2349
2350 ret = f2fs_convert_inline_inode(dst);
2351 if (ret)
2352 goto out_unlock;
2353
2354
2355 ret = filemap_write_and_wait_range(src->i_mapping,
2356 pos_in, pos_in + len);
2357 if (ret)
2358 goto out_unlock;
2359
2360 ret = filemap_write_and_wait_range(dst->i_mapping,
2361 pos_out, pos_out + len);
2362 if (ret)
2363 goto out_unlock;
2364
2365 f2fs_balance_fs(sbi, true);
2366 f2fs_lock_op(sbi);
2367 ret = __exchange_data_block(src, dst, pos_in >> F2FS_BLKSIZE_BITS,
2368 pos_out >> F2FS_BLKSIZE_BITS,
2369 len >> F2FS_BLKSIZE_BITS, false);
2370
2371 if (!ret) {
2372 if (dst_max_i_size)
2373 f2fs_i_size_write(dst, dst_max_i_size);
2374 else if (dst_osize != dst->i_size)
2375 f2fs_i_size_write(dst, dst_osize);
2376 }
2377 f2fs_unlock_op(sbi);
2378out_unlock:
2379 if (src != dst) {
2380 up_write(&F2FS_I(dst)->dio_rwsem[WRITE]);
2381 inode_unlock(dst);
2382 }
2383out:
2384 up_write(&F2FS_I(src)->dio_rwsem[WRITE]);
2385 inode_unlock(src);
2386 return ret;
2387}
2388
2389static int f2fs_ioc_move_range(struct file *filp, unsigned long arg)
2390{
2391 struct f2fs_move_range range;
2392 struct fd dst;
2393 int err;
2394
2395 if (!(filp->f_mode & FMODE_READ) ||
2396 !(filp->f_mode & FMODE_WRITE))
2397 return -EBADF;
2398
2399 if (copy_from_user(&range, (struct f2fs_move_range __user *)arg,
2400 sizeof(range)))
2401 return -EFAULT;
2402
2403 dst = fdget(range.dst_fd);
2404 if (!dst.file)
2405 return -EBADF;
2406
2407 if (!(dst.file->f_mode & FMODE_WRITE)) {
2408 err = -EBADF;
2409 goto err_out;
2410 }
2411
2412 err = mnt_want_write_file(filp);
2413 if (err)
2414 goto err_out;
2415
2416 err = f2fs_move_file_range(filp, range.pos_in, dst.file,
2417 range.pos_out, range.len);
2418
2419 mnt_drop_write_file(filp);
2420 if (err)
2421 goto err_out;
2422
2423 if (copy_to_user((struct f2fs_move_range __user *)arg,
2424 &range, sizeof(range)))
2425 err = -EFAULT;
2426err_out:
2427 fdput(dst);
2428 return err;
2429}
2430
2431static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)
2432{
2433 struct inode *inode = file_inode(filp);
2434 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2435 struct sit_info *sm = SIT_I(sbi);
2436 unsigned int start_segno = 0, end_segno = 0;
2437 unsigned int dev_start_segno = 0, dev_end_segno = 0;
2438 struct f2fs_flush_device range;
2439 int ret;
2440
2441 if (!capable(CAP_SYS_ADMIN))
2442 return -EPERM;
2443
2444 if (f2fs_readonly(sbi->sb))
2445 return -EROFS;
2446
2447 if (copy_from_user(&range, (struct f2fs_flush_device __user *)arg,
2448 sizeof(range)))
2449 return -EFAULT;
2450
2451 if (sbi->s_ndevs <= 1 || sbi->s_ndevs - 1 <= range.dev_num ||
2452 sbi->segs_per_sec != 1) {
2453 f2fs_msg(sbi->sb, KERN_WARNING,
2454 "Can't flush %u in %d for segs_per_sec %u != 1\n",
2455 range.dev_num, sbi->s_ndevs,
2456 sbi->segs_per_sec);
2457 return -EINVAL;
2458 }
2459
2460 ret = mnt_want_write_file(filp);
2461 if (ret)
2462 return ret;
2463
2464 if (range.dev_num != 0)
2465 dev_start_segno = GET_SEGNO(sbi, FDEV(range.dev_num).start_blk);
2466 dev_end_segno = GET_SEGNO(sbi, FDEV(range.dev_num).end_blk);
2467
2468 start_segno = sm->last_victim[FLUSH_DEVICE];
2469 if (start_segno < dev_start_segno || start_segno >= dev_end_segno)
2470 start_segno = dev_start_segno;
2471 end_segno = min(start_segno + range.segments, dev_end_segno);
2472
2473 while (start_segno < end_segno) {
2474 if (!mutex_trylock(&sbi->gc_mutex)) {
2475 ret = -EBUSY;
2476 goto out;
2477 }
2478 sm->last_victim[GC_CB] = end_segno + 1;
2479 sm->last_victim[GC_GREEDY] = end_segno + 1;
2480 sm->last_victim[ALLOC_NEXT] = end_segno + 1;
2481 ret = f2fs_gc(sbi, true, true, start_segno);
2482 if (ret == -EAGAIN)
2483 ret = 0;
2484 else if (ret < 0)
2485 break;
2486 start_segno++;
2487 }
2488out:
2489 mnt_drop_write_file(filp);
2490 return ret;
2491}
2492
2493static int f2fs_ioc_get_features(struct file *filp, unsigned long arg)
2494{
2495 struct inode *inode = file_inode(filp);
2496 u32 sb_feature = le32_to_cpu(F2FS_I_SB(inode)->raw_super->feature);
2497
2498
2499 sb_feature |= F2FS_FEATURE_ATOMIC_WRITE;
2500
2501 return put_user(sb_feature, (u32 __user *)arg);
2502}
2503
2504#ifdef CONFIG_QUOTA
2505static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2506{
2507 struct inode *inode = file_inode(filp);
2508 struct f2fs_inode_info *fi = F2FS_I(inode);
2509 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2510 struct super_block *sb = sbi->sb;
2511 struct dquot *transfer_to[MAXQUOTAS] = {};
2512 struct page *ipage;
2513 kprojid_t kprojid;
2514 int err;
2515
2516 if (!f2fs_sb_has_project_quota(sb)) {
2517 if (projid != F2FS_DEF_PROJID)
2518 return -EOPNOTSUPP;
2519 else
2520 return 0;
2521 }
2522
2523 if (!f2fs_has_extra_attr(inode))
2524 return -EOPNOTSUPP;
2525
2526 kprojid = make_kprojid(&init_user_ns, (projid_t)projid);
2527
2528 if (projid_eq(kprojid, F2FS_I(inode)->i_projid))
2529 return 0;
2530
2531 err = mnt_want_write_file(filp);
2532 if (err)
2533 return err;
2534
2535 err = -EPERM;
2536 inode_lock(inode);
2537
2538
2539 if (IS_NOQUOTA(inode))
2540 goto out_unlock;
2541
2542 ipage = get_node_page(sbi, inode->i_ino);
2543 if (IS_ERR(ipage)) {
2544 err = PTR_ERR(ipage);
2545 goto out_unlock;
2546 }
2547
2548 if (!F2FS_FITS_IN_INODE(F2FS_INODE(ipage), fi->i_extra_isize,
2549 i_projid)) {
2550 err = -EOVERFLOW;
2551 f2fs_put_page(ipage, 1);
2552 goto out_unlock;
2553 }
2554 f2fs_put_page(ipage, 1);
2555
2556 dquot_initialize(inode);
2557
2558 transfer_to[PRJQUOTA] = dqget(sb, make_kqid_projid(kprojid));
2559 if (!IS_ERR(transfer_to[PRJQUOTA])) {
2560 err = __dquot_transfer(inode, transfer_to);
2561 dqput(transfer_to[PRJQUOTA]);
2562 if (err)
2563 goto out_dirty;
2564 }
2565
2566 F2FS_I(inode)->i_projid = kprojid;
2567 inode->i_ctime = current_time(inode);
2568out_dirty:
2569 f2fs_mark_inode_dirty_sync(inode, true);
2570out_unlock:
2571 inode_unlock(inode);
2572 mnt_drop_write_file(filp);
2573 return err;
2574}
2575#else
2576static int f2fs_ioc_setproject(struct file *filp, __u32 projid)
2577{
2578 if (projid != F2FS_DEF_PROJID)
2579 return -EOPNOTSUPP;
2580 return 0;
2581}
2582#endif
2583
2584
2585static inline __u32 f2fs_iflags_to_xflags(unsigned long iflags)
2586{
2587 __u32 xflags = 0;
2588
2589 if (iflags & FS_SYNC_FL)
2590 xflags |= FS_XFLAG_SYNC;
2591 if (iflags & FS_IMMUTABLE_FL)
2592 xflags |= FS_XFLAG_IMMUTABLE;
2593 if (iflags & FS_APPEND_FL)
2594 xflags |= FS_XFLAG_APPEND;
2595 if (iflags & FS_NODUMP_FL)
2596 xflags |= FS_XFLAG_NODUMP;
2597 if (iflags & FS_NOATIME_FL)
2598 xflags |= FS_XFLAG_NOATIME;
2599 if (iflags & FS_PROJINHERIT_FL)
2600 xflags |= FS_XFLAG_PROJINHERIT;
2601 return xflags;
2602}
2603
2604#define F2FS_SUPPORTED_FS_XFLAGS (FS_XFLAG_SYNC | FS_XFLAG_IMMUTABLE | \
2605 FS_XFLAG_APPEND | FS_XFLAG_NODUMP | \
2606 FS_XFLAG_NOATIME | FS_XFLAG_PROJINHERIT)
2607
2608
2609#define F2FS_FL_XFLAG_VISIBLE (FS_SYNC_FL | \
2610 FS_IMMUTABLE_FL | \
2611 FS_APPEND_FL | \
2612 FS_NODUMP_FL | \
2613 FS_NOATIME_FL | \
2614 FS_PROJINHERIT_FL)
2615
2616
2617static inline unsigned long f2fs_xflags_to_iflags(__u32 xflags)
2618{
2619 unsigned long iflags = 0;
2620
2621 if (xflags & FS_XFLAG_SYNC)
2622 iflags |= FS_SYNC_FL;
2623 if (xflags & FS_XFLAG_IMMUTABLE)
2624 iflags |= FS_IMMUTABLE_FL;
2625 if (xflags & FS_XFLAG_APPEND)
2626 iflags |= FS_APPEND_FL;
2627 if (xflags & FS_XFLAG_NODUMP)
2628 iflags |= FS_NODUMP_FL;
2629 if (xflags & FS_XFLAG_NOATIME)
2630 iflags |= FS_NOATIME_FL;
2631 if (xflags & FS_XFLAG_PROJINHERIT)
2632 iflags |= FS_PROJINHERIT_FL;
2633
2634 return iflags;
2635}
2636
2637static int f2fs_ioc_fsgetxattr(struct file *filp, unsigned long arg)
2638{
2639 struct inode *inode = file_inode(filp);
2640 struct f2fs_inode_info *fi = F2FS_I(inode);
2641 struct fsxattr fa;
2642
2643 memset(&fa, 0, sizeof(struct fsxattr));
2644 fa.fsx_xflags = f2fs_iflags_to_xflags(fi->i_flags &
2645 (FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL));
2646
2647 if (f2fs_sb_has_project_quota(inode->i_sb))
2648 fa.fsx_projid = (__u32)from_kprojid(&init_user_ns,
2649 fi->i_projid);
2650
2651 if (copy_to_user((struct fsxattr __user *)arg, &fa, sizeof(fa)))
2652 return -EFAULT;
2653 return 0;
2654}
2655
2656static int f2fs_ioc_fssetxattr(struct file *filp, unsigned long arg)
2657{
2658 struct inode *inode = file_inode(filp);
2659 struct f2fs_inode_info *fi = F2FS_I(inode);
2660 struct fsxattr fa;
2661 unsigned int flags;
2662 int err;
2663
2664 if (copy_from_user(&fa, (struct fsxattr __user *)arg, sizeof(fa)))
2665 return -EFAULT;
2666
2667
2668 if (!inode_owner_or_capable(inode))
2669 return -EACCES;
2670
2671 if (fa.fsx_xflags & ~F2FS_SUPPORTED_FS_XFLAGS)
2672 return -EOPNOTSUPP;
2673
2674 flags = f2fs_xflags_to_iflags(fa.fsx_xflags);
2675 if (f2fs_mask_flags(inode->i_mode, flags) != flags)
2676 return -EOPNOTSUPP;
2677
2678 err = mnt_want_write_file(filp);
2679 if (err)
2680 return err;
2681
2682 inode_lock(inode);
2683 flags = (fi->i_flags & ~F2FS_FL_XFLAG_VISIBLE) |
2684 (flags & F2FS_FL_XFLAG_VISIBLE);
2685 err = __f2fs_ioc_setflags(inode, flags);
2686 inode_unlock(inode);
2687 mnt_drop_write_file(filp);
2688 if (err)
2689 return err;
2690
2691 err = f2fs_ioc_setproject(filp, fa.fsx_projid);
2692 if (err)
2693 return err;
2694
2695 return 0;
2696}
2697
2698int f2fs_pin_file_control(struct inode *inode, bool inc)
2699{
2700 struct f2fs_inode_info *fi = F2FS_I(inode);
2701 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2702
2703
2704 if (inc)
2705 f2fs_i_gc_failures_write(inode, fi->i_gc_failures + 1);
2706
2707 if (fi->i_gc_failures > sbi->gc_pin_file_threshold) {
2708 f2fs_msg(sbi->sb, KERN_WARNING,
2709 "%s: Enable GC = ino %lx after %x GC trials\n",
2710 __func__, inode->i_ino, fi->i_gc_failures);
2711 clear_inode_flag(inode, FI_PIN_FILE);
2712 return -EAGAIN;
2713 }
2714 return 0;
2715}
2716
2717static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)
2718{
2719 struct inode *inode = file_inode(filp);
2720 __u32 pin;
2721 int ret = 0;
2722
2723 if (!inode_owner_or_capable(inode))
2724 return -EACCES;
2725
2726 if (get_user(pin, (__u32 __user *)arg))
2727 return -EFAULT;
2728
2729 if (!S_ISREG(inode->i_mode))
2730 return -EINVAL;
2731
2732 if (f2fs_readonly(F2FS_I_SB(inode)->sb))
2733 return -EROFS;
2734
2735 ret = mnt_want_write_file(filp);
2736 if (ret)
2737 return ret;
2738
2739 inode_lock(inode);
2740
2741 if (should_update_outplace(inode, NULL)) {
2742 ret = -EINVAL;
2743 goto out;
2744 }
2745
2746 if (!pin) {
2747 clear_inode_flag(inode, FI_PIN_FILE);
2748 F2FS_I(inode)->i_gc_failures = 1;
2749 goto done;
2750 }
2751
2752 if (f2fs_pin_file_control(inode, false)) {
2753 ret = -EAGAIN;
2754 goto out;
2755 }
2756 ret = f2fs_convert_inline_inode(inode);
2757 if (ret)
2758 goto out;
2759
2760 set_inode_flag(inode, FI_PIN_FILE);
2761 ret = F2FS_I(inode)->i_gc_failures;
2762done:
2763 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
2764out:
2765 inode_unlock(inode);
2766 mnt_drop_write_file(filp);
2767 return ret;
2768}
2769
2770static int f2fs_ioc_get_pin_file(struct file *filp, unsigned long arg)
2771{
2772 struct inode *inode = file_inode(filp);
2773 __u32 pin = 0;
2774
2775 if (is_inode_flag_set(inode, FI_PIN_FILE))
2776 pin = F2FS_I(inode)->i_gc_failures;
2777 return put_user(pin, (u32 __user *)arg);
2778}
2779
2780int f2fs_precache_extents(struct inode *inode)
2781{
2782 struct f2fs_inode_info *fi = F2FS_I(inode);
2783 struct f2fs_map_blocks map;
2784 pgoff_t m_next_extent;
2785 loff_t end;
2786 int err;
2787
2788 if (is_inode_flag_set(inode, FI_NO_EXTENT))
2789 return -EOPNOTSUPP;
2790
2791 map.m_lblk = 0;
2792 map.m_next_pgofs = NULL;
2793 map.m_next_extent = &m_next_extent;
2794 map.m_seg_type = NO_CHECK_TYPE;
2795 end = F2FS_I_SB(inode)->max_file_blocks;
2796
2797 while (map.m_lblk < end) {
2798 map.m_len = end - map.m_lblk;
2799
2800 down_write(&fi->dio_rwsem[WRITE]);
2801 err = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_PRECACHE);
2802 up_write(&fi->dio_rwsem[WRITE]);
2803 if (err)
2804 return err;
2805
2806 map.m_lblk = m_next_extent;
2807 }
2808
2809 return err;
2810}
2811
2812static int f2fs_ioc_precache_extents(struct file *filp, unsigned long arg)
2813{
2814 return f2fs_precache_extents(file_inode(filp));
2815}
2816
2817long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
2818{
2819 if (unlikely(f2fs_cp_error(F2FS_I_SB(file_inode(filp)))))
2820 return -EIO;
2821
2822 switch (cmd) {
2823 case F2FS_IOC_GETFLAGS:
2824 return f2fs_ioc_getflags(filp, arg);
2825 case F2FS_IOC_SETFLAGS:
2826 return f2fs_ioc_setflags(filp, arg);
2827 case F2FS_IOC_GETVERSION:
2828 return f2fs_ioc_getversion(filp, arg);
2829 case F2FS_IOC_START_ATOMIC_WRITE:
2830 return f2fs_ioc_start_atomic_write(filp);
2831 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2832 return f2fs_ioc_commit_atomic_write(filp);
2833 case F2FS_IOC_START_VOLATILE_WRITE:
2834 return f2fs_ioc_start_volatile_write(filp);
2835 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2836 return f2fs_ioc_release_volatile_write(filp);
2837 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2838 return f2fs_ioc_abort_volatile_write(filp);
2839 case F2FS_IOC_SHUTDOWN:
2840 return f2fs_ioc_shutdown(filp, arg);
2841 case FITRIM:
2842 return f2fs_ioc_fitrim(filp, arg);
2843 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2844 return f2fs_ioc_set_encryption_policy(filp, arg);
2845 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2846 return f2fs_ioc_get_encryption_policy(filp, arg);
2847 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2848 return f2fs_ioc_get_encryption_pwsalt(filp, arg);
2849 case F2FS_IOC_GARBAGE_COLLECT:
2850 return f2fs_ioc_gc(filp, arg);
2851 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2852 return f2fs_ioc_gc_range(filp, arg);
2853 case F2FS_IOC_WRITE_CHECKPOINT:
2854 return f2fs_ioc_write_checkpoint(filp, arg);
2855 case F2FS_IOC_DEFRAGMENT:
2856 return f2fs_ioc_defragment(filp, arg);
2857 case F2FS_IOC_MOVE_RANGE:
2858 return f2fs_ioc_move_range(filp, arg);
2859 case F2FS_IOC_FLUSH_DEVICE:
2860 return f2fs_ioc_flush_device(filp, arg);
2861 case F2FS_IOC_GET_FEATURES:
2862 return f2fs_ioc_get_features(filp, arg);
2863 case F2FS_IOC_FSGETXATTR:
2864 return f2fs_ioc_fsgetxattr(filp, arg);
2865 case F2FS_IOC_FSSETXATTR:
2866 return f2fs_ioc_fssetxattr(filp, arg);
2867 case F2FS_IOC_GET_PIN_FILE:
2868 return f2fs_ioc_get_pin_file(filp, arg);
2869 case F2FS_IOC_SET_PIN_FILE:
2870 return f2fs_ioc_set_pin_file(filp, arg);
2871 case F2FS_IOC_PRECACHE_EXTENTS:
2872 return f2fs_ioc_precache_extents(filp, arg);
2873 default:
2874 return -ENOTTY;
2875 }
2876}
2877
2878static ssize_t f2fs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
2879{
2880 struct file *file = iocb->ki_filp;
2881 struct inode *inode = file_inode(file);
2882 struct blk_plug plug;
2883 ssize_t ret;
2884
2885 if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
2886 return -EIO;
2887
2888 inode_lock(inode);
2889 ret = generic_write_checks(iocb, from);
2890 if (ret > 0) {
2891 int err;
2892
2893 if (iov_iter_fault_in_readable(from, iov_iter_count(from)))
2894 set_inode_flag(inode, FI_NO_PREALLOC);
2895
2896 err = f2fs_preallocate_blocks(iocb, from);
2897 if (err) {
2898 clear_inode_flag(inode, FI_NO_PREALLOC);
2899 inode_unlock(inode);
2900 return err;
2901 }
2902 blk_start_plug(&plug);
2903 ret = __generic_file_write_iter(iocb, from);
2904 blk_finish_plug(&plug);
2905 clear_inode_flag(inode, FI_NO_PREALLOC);
2906
2907 if (ret > 0)
2908 f2fs_update_iostat(F2FS_I_SB(inode), APP_WRITE_IO, ret);
2909 }
2910 inode_unlock(inode);
2911
2912 if (ret > 0)
2913 ret = generic_write_sync(iocb, ret);
2914 return ret;
2915}
2916
2917#ifdef CONFIG_COMPAT
2918long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2919{
2920 switch (cmd) {
2921 case F2FS_IOC32_GETFLAGS:
2922 cmd = F2FS_IOC_GETFLAGS;
2923 break;
2924 case F2FS_IOC32_SETFLAGS:
2925 cmd = F2FS_IOC_SETFLAGS;
2926 break;
2927 case F2FS_IOC32_GETVERSION:
2928 cmd = F2FS_IOC_GETVERSION;
2929 break;
2930 case F2FS_IOC_START_ATOMIC_WRITE:
2931 case F2FS_IOC_COMMIT_ATOMIC_WRITE:
2932 case F2FS_IOC_START_VOLATILE_WRITE:
2933 case F2FS_IOC_RELEASE_VOLATILE_WRITE:
2934 case F2FS_IOC_ABORT_VOLATILE_WRITE:
2935 case F2FS_IOC_SHUTDOWN:
2936 case F2FS_IOC_SET_ENCRYPTION_POLICY:
2937 case F2FS_IOC_GET_ENCRYPTION_PWSALT:
2938 case F2FS_IOC_GET_ENCRYPTION_POLICY:
2939 case F2FS_IOC_GARBAGE_COLLECT:
2940 case F2FS_IOC_GARBAGE_COLLECT_RANGE:
2941 case F2FS_IOC_WRITE_CHECKPOINT:
2942 case F2FS_IOC_DEFRAGMENT:
2943 case F2FS_IOC_MOVE_RANGE:
2944 case F2FS_IOC_FLUSH_DEVICE:
2945 case F2FS_IOC_GET_FEATURES:
2946 case F2FS_IOC_FSGETXATTR:
2947 case F2FS_IOC_FSSETXATTR:
2948 case F2FS_IOC_GET_PIN_FILE:
2949 case F2FS_IOC_SET_PIN_FILE:
2950 case F2FS_IOC_PRECACHE_EXTENTS:
2951 break;
2952 default:
2953 return -ENOIOCTLCMD;
2954 }
2955 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
2956}
2957#endif
2958
2959const struct file_operations f2fs_file_operations = {
2960 .llseek = f2fs_llseek,
2961 .read_iter = generic_file_read_iter,
2962 .write_iter = f2fs_file_write_iter,
2963 .open = f2fs_file_open,
2964 .release = f2fs_release_file,
2965 .mmap = f2fs_file_mmap,
2966 .flush = f2fs_file_flush,
2967 .fsync = f2fs_sync_file,
2968 .fallocate = f2fs_fallocate,
2969 .unlocked_ioctl = f2fs_ioctl,
2970#ifdef CONFIG_COMPAT
2971 .compat_ioctl = f2fs_compat_ioctl,
2972#endif
2973 .splice_read = generic_file_splice_read,
2974 .splice_write = iter_file_splice_write,
2975};
2976