~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/f2fs/file.c

Version: ~ [ linux-5.5-rc7 ] ~ [ linux-5.4.13 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.97 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.166 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.210 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.210 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp