4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
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/falloc.h>
17 #include <linux/types.h>
18 #include <linux/compat.h>
19 #include <linux/uaccess.h>
20 #include <linux/mount.h>
28 static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
31 struct page *page = vmf->page;
32 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
33 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
35 struct dnode_of_data dn;
40 sb_start_pagefault(inode->i_sb);
42 mutex_lock_op(sbi, DATA_NEW);
44 /* block allocation */
45 set_new_dnode(&dn, inode, NULL, NULL, 0);
46 err = get_dnode_of_data(&dn, page->index, 0);
48 mutex_unlock_op(sbi, DATA_NEW);
52 old_blk_addr = dn.data_blkaddr;
54 if (old_blk_addr == NULL_ADDR) {
55 err = reserve_new_block(&dn);
58 mutex_unlock_op(sbi, DATA_NEW);
64 mutex_unlock_op(sbi, DATA_NEW);
67 if (page->mapping != inode->i_mapping ||
68 page_offset(page) >= i_size_read(inode) ||
69 !PageUptodate(page)) {
76 * check to see if the page is mapped already (no holes)
78 if (PageMappedToDisk(page))
82 wait_on_page_writeback(page);
84 /* page is wholly or partially inside EOF */
85 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
87 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
88 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
91 SetPageUptodate(page);
93 file_update_time(vma->vm_file);
95 sb_end_pagefault(inode->i_sb);
96 return block_page_mkwrite_return(err);
99 static const struct vm_operations_struct f2fs_file_vm_ops = {
100 .fault = filemap_fault,
101 .page_mkwrite = f2fs_vm_page_mkwrite,
102 .remap_pages = generic_file_remap_pages,
105 static int need_to_sync_dir(struct f2fs_sb_info *sbi, struct inode *inode)
107 struct dentry *dentry;
110 inode = igrab(inode);
111 dentry = d_find_any_alias(inode);
116 pino = dentry->d_parent->d_inode->i_ino;
119 return !is_checkpointed_node(sbi, pino);
122 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
124 struct inode *inode = file->f_mapping->host;
125 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
126 unsigned long long cur_version;
128 bool need_cp = false;
129 struct writeback_control wbc = {
130 .sync_mode = WB_SYNC_ALL,
131 .nr_to_write = LONG_MAX,
135 if (inode->i_sb->s_flags & MS_RDONLY)
138 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
142 /* guarantee free sections for fsync */
143 f2fs_balance_fs(sbi);
145 mutex_lock(&inode->i_mutex);
147 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
150 mutex_lock(&sbi->cp_mutex);
151 cur_version = le64_to_cpu(F2FS_CKPT(sbi)->checkpoint_ver);
152 mutex_unlock(&sbi->cp_mutex);
154 if (F2FS_I(inode)->data_version != cur_version &&
155 !(inode->i_state & I_DIRTY))
157 F2FS_I(inode)->data_version--;
159 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
161 else if (is_inode_flag_set(F2FS_I(inode), FI_NEED_CP))
163 else if (!space_for_roll_forward(sbi))
165 else if (need_to_sync_dir(sbi, inode))
169 /* all the dirty node pages should be flushed for POR */
170 ret = f2fs_sync_fs(inode->i_sb, 1);
171 clear_inode_flag(F2FS_I(inode), FI_NEED_CP);
173 /* if there is no written node page, write its inode page */
174 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
175 ret = f2fs_write_inode(inode, NULL);
179 filemap_fdatawait_range(sbi->node_inode->i_mapping,
183 mutex_unlock(&inode->i_mutex);
187 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
190 vma->vm_ops = &f2fs_file_vm_ops;
194 static int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
196 int nr_free = 0, ofs = dn->ofs_in_node;
197 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
198 struct f2fs_node *raw_node;
201 raw_node = page_address(dn->node_page);
202 addr = blkaddr_in_node(raw_node) + ofs;
204 for ( ; count > 0; count--, addr++, dn->ofs_in_node++) {
205 block_t blkaddr = le32_to_cpu(*addr);
206 if (blkaddr == NULL_ADDR)
209 update_extent_cache(NULL_ADDR, dn);
210 invalidate_blocks(sbi, blkaddr);
211 dec_valid_block_count(sbi, dn->inode, 1);
215 set_page_dirty(dn->node_page);
218 dn->ofs_in_node = ofs;
222 void truncate_data_blocks(struct dnode_of_data *dn)
224 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
227 static void truncate_partial_data_page(struct inode *inode, u64 from)
229 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
235 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT);
240 wait_on_page_writeback(page);
241 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
242 set_page_dirty(page);
243 f2fs_put_page(page, 1);
246 static int truncate_blocks(struct inode *inode, u64 from)
248 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
249 unsigned int blocksize = inode->i_sb->s_blocksize;
250 struct dnode_of_data dn;
255 free_from = (pgoff_t)
256 ((from + blocksize - 1) >> (sbi->log_blocksize));
258 mutex_lock_op(sbi, DATA_TRUNC);
260 set_new_dnode(&dn, inode, NULL, NULL, 0);
261 err = get_dnode_of_data(&dn, free_from, RDONLY_NODE);
265 mutex_unlock_op(sbi, DATA_TRUNC);
269 if (IS_INODE(dn.node_page))
270 count = ADDRS_PER_INODE;
272 count = ADDRS_PER_BLOCK;
274 count -= dn.ofs_in_node;
276 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
277 truncate_data_blocks_range(&dn, count);
283 err = truncate_inode_blocks(inode, free_from);
284 mutex_unlock_op(sbi, DATA_TRUNC);
286 /* lastly zero out the first data page */
287 truncate_partial_data_page(inode, from);
292 void f2fs_truncate(struct inode *inode)
294 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
295 S_ISLNK(inode->i_mode)))
298 if (!truncate_blocks(inode, i_size_read(inode))) {
299 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
300 mark_inode_dirty(inode);
304 static int f2fs_getattr(struct vfsmount *mnt,
305 struct dentry *dentry, struct kstat *stat)
307 struct inode *inode = dentry->d_inode;
308 generic_fillattr(inode, stat);
313 #ifdef CONFIG_F2FS_FS_POSIX_ACL
314 static void __setattr_copy(struct inode *inode, const struct iattr *attr)
316 struct f2fs_inode_info *fi = F2FS_I(inode);
317 unsigned int ia_valid = attr->ia_valid;
319 if (ia_valid & ATTR_UID)
320 inode->i_uid = attr->ia_uid;
321 if (ia_valid & ATTR_GID)
322 inode->i_gid = attr->ia_gid;
323 if (ia_valid & ATTR_ATIME)
324 inode->i_atime = timespec_trunc(attr->ia_atime,
325 inode->i_sb->s_time_gran);
326 if (ia_valid & ATTR_MTIME)
327 inode->i_mtime = timespec_trunc(attr->ia_mtime,
328 inode->i_sb->s_time_gran);
329 if (ia_valid & ATTR_CTIME)
330 inode->i_ctime = timespec_trunc(attr->ia_ctime,
331 inode->i_sb->s_time_gran);
332 if (ia_valid & ATTR_MODE) {
333 umode_t mode = attr->ia_mode;
335 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
337 set_acl_inode(fi, mode);
341 #define __setattr_copy setattr_copy
344 int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
346 struct inode *inode = dentry->d_inode;
347 struct f2fs_inode_info *fi = F2FS_I(inode);
350 err = inode_change_ok(inode, attr);
354 if ((attr->ia_valid & ATTR_SIZE) &&
355 attr->ia_size != i_size_read(inode)) {
356 truncate_setsize(inode, attr->ia_size);
357 f2fs_truncate(inode);
358 f2fs_balance_fs(F2FS_SB(inode->i_sb));
361 __setattr_copy(inode, attr);
363 if (attr->ia_valid & ATTR_MODE) {
364 err = f2fs_acl_chmod(inode);
365 if (err || is_inode_flag_set(fi, FI_ACL_MODE)) {
366 inode->i_mode = fi->i_acl_mode;
367 clear_inode_flag(fi, FI_ACL_MODE);
371 mark_inode_dirty(inode);
375 const struct inode_operations f2fs_file_inode_operations = {
376 .getattr = f2fs_getattr,
377 .setattr = f2fs_setattr,
378 .get_acl = f2fs_get_acl,
379 #ifdef CONFIG_F2FS_FS_XATTR
380 .setxattr = generic_setxattr,
381 .getxattr = generic_getxattr,
382 .listxattr = f2fs_listxattr,
383 .removexattr = generic_removexattr,
387 static void fill_zero(struct inode *inode, pgoff_t index,
388 loff_t start, loff_t len)
390 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
396 f2fs_balance_fs(sbi);
398 mutex_lock_op(sbi, DATA_NEW);
399 page = get_new_data_page(inode, index, false);
400 mutex_unlock_op(sbi, DATA_NEW);
403 wait_on_page_writeback(page);
404 zero_user(page, start, len);
405 set_page_dirty(page);
406 f2fs_put_page(page, 1);
410 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
415 for (index = pg_start; index < pg_end; index++) {
416 struct dnode_of_data dn;
417 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
419 f2fs_balance_fs(sbi);
421 mutex_lock_op(sbi, DATA_TRUNC);
422 set_new_dnode(&dn, inode, NULL, NULL, 0);
423 err = get_dnode_of_data(&dn, index, RDONLY_NODE);
425 mutex_unlock_op(sbi, DATA_TRUNC);
431 if (dn.data_blkaddr != NULL_ADDR)
432 truncate_data_blocks_range(&dn, 1);
434 mutex_unlock_op(sbi, DATA_TRUNC);
439 static int punch_hole(struct inode *inode, loff_t offset, loff_t len, int mode)
441 pgoff_t pg_start, pg_end;
442 loff_t off_start, off_end;
445 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
446 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
448 off_start = offset & (PAGE_CACHE_SIZE - 1);
449 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
451 if (pg_start == pg_end) {
452 fill_zero(inode, pg_start, off_start,
453 off_end - off_start);
456 fill_zero(inode, pg_start++, off_start,
457 PAGE_CACHE_SIZE - off_start);
459 fill_zero(inode, pg_end, 0, off_end);
461 if (pg_start < pg_end) {
462 struct address_space *mapping = inode->i_mapping;
463 loff_t blk_start, blk_end;
465 blk_start = pg_start << PAGE_CACHE_SHIFT;
466 blk_end = pg_end << PAGE_CACHE_SHIFT;
467 truncate_inode_pages_range(mapping, blk_start,
469 ret = truncate_hole(inode, pg_start, pg_end);
473 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
474 i_size_read(inode) <= (offset + len)) {
475 i_size_write(inode, offset);
476 mark_inode_dirty(inode);
482 static int expand_inode_data(struct inode *inode, loff_t offset,
483 loff_t len, int mode)
485 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
486 pgoff_t index, pg_start, pg_end;
487 loff_t new_size = i_size_read(inode);
488 loff_t off_start, off_end;
491 ret = inode_newsize_ok(inode, (len + offset));
495 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
496 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
498 off_start = offset & (PAGE_CACHE_SIZE - 1);
499 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
501 for (index = pg_start; index <= pg_end; index++) {
502 struct dnode_of_data dn;
504 mutex_lock_op(sbi, DATA_NEW);
506 set_new_dnode(&dn, inode, NULL, NULL, 0);
507 ret = get_dnode_of_data(&dn, index, 0);
509 mutex_unlock_op(sbi, DATA_NEW);
513 if (dn.data_blkaddr == NULL_ADDR) {
514 ret = reserve_new_block(&dn);
517 mutex_unlock_op(sbi, DATA_NEW);
523 mutex_unlock_op(sbi, DATA_NEW);
525 if (pg_start == pg_end)
526 new_size = offset + len;
527 else if (index == pg_start && off_start)
528 new_size = (index + 1) << PAGE_CACHE_SHIFT;
529 else if (index == pg_end)
530 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
532 new_size += PAGE_CACHE_SIZE;
535 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
536 i_size_read(inode) < new_size) {
537 i_size_write(inode, new_size);
538 mark_inode_dirty(inode);
544 static long f2fs_fallocate(struct file *file, int mode,
545 loff_t offset, loff_t len)
547 struct inode *inode = file->f_path.dentry->d_inode;
550 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
553 if (mode & FALLOC_FL_PUNCH_HOLE)
554 ret = punch_hole(inode, offset, len, mode);
556 ret = expand_inode_data(inode, offset, len, mode);
559 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
560 mark_inode_dirty(inode);
565 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
566 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
568 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
572 else if (S_ISREG(mode))
573 return flags & F2FS_REG_FLMASK;
575 return flags & F2FS_OTHER_FLMASK;
578 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
580 struct inode *inode = filp->f_dentry->d_inode;
581 struct f2fs_inode_info *fi = F2FS_I(inode);
586 case FS_IOC_GETFLAGS:
587 flags = fi->i_flags & FS_FL_USER_VISIBLE;
588 return put_user(flags, (int __user *) arg);
589 case FS_IOC_SETFLAGS:
591 unsigned int oldflags;
593 ret = mnt_want_write(filp->f_path.mnt);
597 if (!inode_owner_or_capable(inode)) {
602 if (get_user(flags, (int __user *) arg)) {
607 flags = f2fs_mask_flags(inode->i_mode, flags);
609 mutex_lock(&inode->i_mutex);
611 oldflags = fi->i_flags;
613 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
614 if (!capable(CAP_LINUX_IMMUTABLE)) {
615 mutex_unlock(&inode->i_mutex);
621 flags = flags & FS_FL_USER_MODIFIABLE;
622 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
624 mutex_unlock(&inode->i_mutex);
626 f2fs_set_inode_flags(inode);
627 inode->i_ctime = CURRENT_TIME;
628 mark_inode_dirty(inode);
630 mnt_drop_write(filp->f_path.mnt);
639 long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
642 case F2FS_IOC32_GETFLAGS:
643 cmd = F2FS_IOC_GETFLAGS;
645 case F2FS_IOC32_SETFLAGS:
646 cmd = F2FS_IOC_SETFLAGS;
651 return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
655 const struct file_operations f2fs_file_operations = {
656 .llseek = generic_file_llseek,
657 .read = do_sync_read,
658 .write = do_sync_write,
659 .aio_read = generic_file_aio_read,
660 .aio_write = generic_file_aio_write,
661 .open = generic_file_open,
662 .mmap = f2fs_file_mmap,
663 .fsync = f2fs_sync_file,
664 .fallocate = f2fs_fallocate,
665 .unlocked_ioctl = f2fs_ioctl,
667 .compat_ioctl = f2fs_compat_ioctl,
669 .splice_read = generic_file_splice_read,
670 .splice_write = generic_file_splice_write,