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