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/uaccess.h>
19 #include <linux/mount.h>
27 static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma,
30 struct page *page = vmf->page;
31 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
32 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
34 struct dnode_of_data dn;
39 sb_start_pagefault(inode->i_sb);
41 mutex_lock_op(sbi, DATA_NEW);
43 /* block allocation */
44 set_new_dnode(&dn, inode, NULL, NULL, 0);
45 err = get_dnode_of_data(&dn, page->index, 0);
47 mutex_unlock_op(sbi, DATA_NEW);
51 old_blk_addr = dn.data_blkaddr;
53 if (old_blk_addr == NULL_ADDR) {
54 err = reserve_new_block(&dn);
57 mutex_unlock_op(sbi, DATA_NEW);
63 mutex_unlock_op(sbi, DATA_NEW);
66 if (page->mapping != inode->i_mapping ||
67 page_offset(page) >= i_size_read(inode) ||
68 !PageUptodate(page)) {
75 * check to see if the page is mapped already (no holes)
77 if (PageMappedToDisk(page))
81 wait_on_page_writeback(page);
83 /* page is wholly or partially inside EOF */
84 if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) {
86 offset = i_size_read(inode) & ~PAGE_CACHE_MASK;
87 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
90 SetPageUptodate(page);
92 file_update_time(vma->vm_file);
94 sb_end_pagefault(inode->i_sb);
95 return block_page_mkwrite_return(err);
98 static const struct vm_operations_struct f2fs_file_vm_ops = {
99 .fault = filemap_fault,
100 .page_mkwrite = f2fs_vm_page_mkwrite,
101 .remap_pages = generic_file_remap_pages,
104 static int need_to_sync_dir(struct f2fs_sb_info *sbi, struct inode *inode)
106 struct dentry *dentry;
109 inode = igrab(inode);
110 dentry = d_find_any_alias(inode);
115 pino = dentry->d_parent->d_inode->i_ino;
118 return !is_checkpointed_node(sbi, pino);
121 int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
123 struct inode *inode = file->f_mapping->host;
124 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
125 unsigned long long cur_version;
127 bool need_cp = false;
128 struct writeback_control wbc = {
129 .sync_mode = WB_SYNC_ALL,
130 .nr_to_write = LONG_MAX,
134 if (inode->i_sb->s_flags & MS_RDONLY)
137 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
141 /* guarantee free sections for fsync */
142 f2fs_balance_fs(sbi);
144 mutex_lock(&inode->i_mutex);
146 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
149 mutex_lock(&sbi->cp_mutex);
150 cur_version = le64_to_cpu(F2FS_CKPT(sbi)->checkpoint_ver);
151 mutex_unlock(&sbi->cp_mutex);
153 if (F2FS_I(inode)->data_version != cur_version &&
154 !(inode->i_state & I_DIRTY))
156 F2FS_I(inode)->data_version--;
158 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
160 if (is_inode_flag_set(F2FS_I(inode), FI_NEED_CP))
162 if (!space_for_roll_forward(sbi))
164 if (need_to_sync_dir(sbi, inode))
168 /* all the dirty node pages should be flushed for POR */
169 ret = f2fs_sync_fs(inode->i_sb, 1);
170 clear_inode_flag(F2FS_I(inode), FI_NEED_CP);
172 /* if there is no written node page, write its inode page */
173 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
174 ret = f2fs_write_inode(inode, NULL);
178 filemap_fdatawait_range(sbi->node_inode->i_mapping,
182 mutex_unlock(&inode->i_mutex);
186 static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
189 vma->vm_ops = &f2fs_file_vm_ops;
193 static int truncate_data_blocks_range(struct dnode_of_data *dn, int count)
195 int nr_free = 0, ofs = dn->ofs_in_node;
196 struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
197 struct f2fs_node *raw_node;
200 raw_node = page_address(dn->node_page);
201 addr = blkaddr_in_node(raw_node) + ofs;
203 for ( ; count > 0; count--, addr++, dn->ofs_in_node++) {
204 block_t blkaddr = le32_to_cpu(*addr);
205 if (blkaddr == NULL_ADDR)
208 update_extent_cache(NULL_ADDR, dn);
209 invalidate_blocks(sbi, blkaddr);
210 dec_valid_block_count(sbi, dn->inode, 1);
214 set_page_dirty(dn->node_page);
217 dn->ofs_in_node = ofs;
221 void truncate_data_blocks(struct dnode_of_data *dn)
223 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
226 static void truncate_partial_data_page(struct inode *inode, u64 from)
228 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
234 page = find_data_page(inode, from >> PAGE_CACHE_SHIFT);
239 wait_on_page_writeback(page);
240 zero_user(page, offset, PAGE_CACHE_SIZE - offset);
241 set_page_dirty(page);
242 f2fs_put_page(page, 1);
245 static int truncate_blocks(struct inode *inode, u64 from)
247 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
248 unsigned int blocksize = inode->i_sb->s_blocksize;
249 struct dnode_of_data dn;
254 free_from = (pgoff_t)
255 ((from + blocksize - 1) >> (sbi->log_blocksize));
257 mutex_lock_op(sbi, DATA_TRUNC);
259 set_new_dnode(&dn, inode, NULL, NULL, 0);
260 err = get_dnode_of_data(&dn, free_from, RDONLY_NODE);
264 mutex_unlock_op(sbi, DATA_TRUNC);
268 if (IS_INODE(dn.node_page))
269 count = ADDRS_PER_INODE;
271 count = ADDRS_PER_BLOCK;
273 count -= dn.ofs_in_node;
275 if (dn.ofs_in_node || IS_INODE(dn.node_page)) {
276 truncate_data_blocks_range(&dn, count);
282 err = truncate_inode_blocks(inode, free_from);
283 mutex_unlock_op(sbi, DATA_TRUNC);
285 /* lastly zero out the first data page */
286 truncate_partial_data_page(inode, from);
291 void f2fs_truncate(struct inode *inode)
293 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
294 S_ISLNK(inode->i_mode)))
297 if (!truncate_blocks(inode, i_size_read(inode))) {
298 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
299 mark_inode_dirty(inode);
302 f2fs_balance_fs(F2FS_SB(inode->i_sb));
305 static int f2fs_getattr(struct vfsmount *mnt,
306 struct dentry *dentry, struct kstat *stat)
308 struct inode *inode = dentry->d_inode;
309 generic_fillattr(inode, stat);
314 #ifdef CONFIG_F2FS_FS_POSIX_ACL
315 static void __setattr_copy(struct inode *inode, const struct iattr *attr)
317 struct f2fs_inode_info *fi = F2FS_I(inode);
318 unsigned int ia_valid = attr->ia_valid;
320 if (ia_valid & ATTR_UID)
321 inode->i_uid = attr->ia_uid;
322 if (ia_valid & ATTR_GID)
323 inode->i_gid = attr->ia_gid;
324 if (ia_valid & ATTR_ATIME)
325 inode->i_atime = timespec_trunc(attr->ia_atime,
326 inode->i_sb->s_time_gran);
327 if (ia_valid & ATTR_MTIME)
328 inode->i_mtime = timespec_trunc(attr->ia_mtime,
329 inode->i_sb->s_time_gran);
330 if (ia_valid & ATTR_CTIME)
331 inode->i_ctime = timespec_trunc(attr->ia_ctime,
332 inode->i_sb->s_time_gran);
333 if (ia_valid & ATTR_MODE) {
334 umode_t mode = attr->ia_mode;
336 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
338 set_acl_inode(fi, mode);
342 #define __setattr_copy setattr_copy
345 int f2fs_setattr(struct dentry *dentry, struct iattr *attr)
347 struct inode *inode = dentry->d_inode;
348 struct f2fs_inode_info *fi = F2FS_I(inode);
351 err = inode_change_ok(inode, attr);
355 if ((attr->ia_valid & ATTR_SIZE) &&
356 attr->ia_size != i_size_read(inode)) {
357 truncate_setsize(inode, attr->ia_size);
358 f2fs_truncate(inode);
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)
395 page = get_new_data_page(inode, index, false);
398 wait_on_page_writeback(page);
399 zero_user(page, start, len);
400 set_page_dirty(page);
401 f2fs_put_page(page, 1);
405 int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end)
410 for (index = pg_start; index < pg_end; index++) {
411 struct dnode_of_data dn;
412 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
414 f2fs_balance_fs(sbi);
416 mutex_lock_op(sbi, DATA_TRUNC);
417 set_new_dnode(&dn, inode, NULL, NULL, 0);
418 err = get_dnode_of_data(&dn, index, RDONLY_NODE);
420 mutex_unlock_op(sbi, DATA_TRUNC);
426 if (dn.data_blkaddr != NULL_ADDR)
427 truncate_data_blocks_range(&dn, 1);
429 mutex_unlock_op(sbi, DATA_TRUNC);
434 static int punch_hole(struct inode *inode, loff_t offset, loff_t len, int mode)
436 pgoff_t pg_start, pg_end;
437 loff_t off_start, off_end;
440 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
441 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
443 off_start = offset & (PAGE_CACHE_SIZE - 1);
444 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
446 if (pg_start == pg_end) {
447 fill_zero(inode, pg_start, off_start,
448 off_end - off_start);
451 fill_zero(inode, pg_start++, off_start,
452 PAGE_CACHE_SIZE - off_start);
454 fill_zero(inode, pg_end, 0, off_end);
456 if (pg_start < pg_end) {
457 struct address_space *mapping = inode->i_mapping;
458 loff_t blk_start, blk_end;
460 blk_start = pg_start << PAGE_CACHE_SHIFT;
461 blk_end = pg_end << PAGE_CACHE_SHIFT;
462 truncate_inode_pages_range(mapping, blk_start,
464 ret = truncate_hole(inode, pg_start, pg_end);
468 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
469 i_size_read(inode) <= (offset + len)) {
470 i_size_write(inode, offset);
471 mark_inode_dirty(inode);
477 static int expand_inode_data(struct inode *inode, loff_t offset,
478 loff_t len, int mode)
480 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
481 pgoff_t index, pg_start, pg_end;
482 loff_t new_size = i_size_read(inode);
483 loff_t off_start, off_end;
486 ret = inode_newsize_ok(inode, (len + offset));
490 pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT;
491 pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT;
493 off_start = offset & (PAGE_CACHE_SIZE - 1);
494 off_end = (offset + len) & (PAGE_CACHE_SIZE - 1);
496 for (index = pg_start; index <= pg_end; index++) {
497 struct dnode_of_data dn;
499 mutex_lock_op(sbi, DATA_NEW);
501 set_new_dnode(&dn, inode, NULL, NULL, 0);
502 ret = get_dnode_of_data(&dn, index, 0);
504 mutex_unlock_op(sbi, DATA_NEW);
508 if (dn.data_blkaddr == NULL_ADDR) {
509 ret = reserve_new_block(&dn);
512 mutex_unlock_op(sbi, DATA_NEW);
518 mutex_unlock_op(sbi, DATA_NEW);
520 if (pg_start == pg_end)
521 new_size = offset + len;
522 else if (index == pg_start && off_start)
523 new_size = (index + 1) << PAGE_CACHE_SHIFT;
524 else if (index == pg_end)
525 new_size = (index << PAGE_CACHE_SHIFT) + off_end;
527 new_size += PAGE_CACHE_SIZE;
530 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
531 i_size_read(inode) < new_size) {
532 i_size_write(inode, new_size);
533 mark_inode_dirty(inode);
539 static long f2fs_fallocate(struct file *file, int mode,
540 loff_t offset, loff_t len)
542 struct inode *inode = file->f_path.dentry->d_inode;
545 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
548 if (mode & FALLOC_FL_PUNCH_HOLE)
549 ret = punch_hole(inode, offset, len, mode);
551 ret = expand_inode_data(inode, offset, len, mode);
554 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
555 mark_inode_dirty(inode);
560 #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
561 #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
563 static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
567 else if (S_ISREG(mode))
568 return flags & F2FS_REG_FLMASK;
570 return flags & F2FS_OTHER_FLMASK;
573 long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
575 struct inode *inode = filp->f_dentry->d_inode;
576 struct f2fs_inode_info *fi = F2FS_I(inode);
581 case FS_IOC_GETFLAGS:
582 flags = fi->i_flags & FS_FL_USER_VISIBLE;
583 return put_user(flags, (int __user *) arg);
584 case FS_IOC_SETFLAGS:
586 unsigned int oldflags;
588 ret = mnt_want_write(filp->f_path.mnt);
592 if (!inode_owner_or_capable(inode)) {
597 if (get_user(flags, (int __user *) arg)) {
602 flags = f2fs_mask_flags(inode->i_mode, flags);
604 mutex_lock(&inode->i_mutex);
606 oldflags = fi->i_flags;
608 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
609 if (!capable(CAP_LINUX_IMMUTABLE)) {
610 mutex_unlock(&inode->i_mutex);
616 flags = flags & FS_FL_USER_MODIFIABLE;
617 flags |= oldflags & ~FS_FL_USER_MODIFIABLE;
619 mutex_unlock(&inode->i_mutex);
621 f2fs_set_inode_flags(inode);
622 inode->i_ctime = CURRENT_TIME;
623 mark_inode_dirty(inode);
625 mnt_drop_write(filp->f_path.mnt);
633 const struct file_operations f2fs_file_operations = {
634 .llseek = generic_file_llseek,
635 .read = do_sync_read,
636 .write = do_sync_write,
637 .aio_read = generic_file_aio_read,
638 .aio_write = generic_file_aio_write,
639 .open = generic_file_open,
640 .mmap = f2fs_file_mmap,
641 .fsync = f2fs_sync_file,
642 .fallocate = f2fs_fallocate,
643 .unlocked_ioctl = f2fs_ioctl,
644 .splice_read = generic_file_splice_read,
645 .splice_write = generic_file_splice_write,