Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 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> |
Jaegeuk Kim | ae51fb3 | 2013-03-16 11:13:04 +0900 | [diff] [blame] | 16 | #include <linux/blkdev.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 17 | #include <linux/falloc.h> |
| 18 | #include <linux/types.h> |
Namjae Jeon | e975082 | 2013-02-04 23:41:41 +0900 | [diff] [blame] | 19 | #include <linux/compat.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 20 | #include <linux/uaccess.h> |
| 21 | #include <linux/mount.h> |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 22 | #include <linux/pagevec.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 23 | |
| 24 | #include "f2fs.h" |
| 25 | #include "node.h" |
| 26 | #include "segment.h" |
| 27 | #include "xattr.h" |
| 28 | #include "acl.h" |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 29 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 30 | |
| 31 | static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma, |
| 32 | struct vm_fault *vmf) |
| 33 | { |
| 34 | struct page *page = vmf->page; |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 35 | struct inode *inode = file_inode(vma->vm_file); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 36 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 37 | struct dnode_of_data dn; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 38 | int err; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 39 | |
| 40 | f2fs_balance_fs(sbi); |
| 41 | |
| 42 | sb_start_pagefault(inode->i_sb); |
| 43 | |
Jaegeuk Kim | b067ba1f | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 44 | /* force to convert with normal data indices */ |
| 45 | err = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1, page); |
| 46 | if (err) |
| 47 | goto out; |
| 48 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 49 | /* block allocation */ |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 50 | f2fs_lock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 51 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 52 | err = f2fs_reserve_block(&dn, page->index); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 53 | f2fs_unlock_op(sbi); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 54 | if (err) |
| 55 | goto out; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 56 | |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 57 | file_update_time(vma->vm_file); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 58 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 59 | if (unlikely(page->mapping != inode->i_mapping || |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 60 | page_offset(page) > i_size_read(inode) || |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 61 | !PageUptodate(page))) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 62 | unlock_page(page); |
| 63 | err = -EFAULT; |
| 64 | goto out; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * check to see if the page is mapped already (no holes) |
| 69 | */ |
| 70 | if (PageMappedToDisk(page)) |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 71 | goto mapped; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 72 | |
| 73 | /* page is wholly or partially inside EOF */ |
| 74 | if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) { |
| 75 | unsigned offset; |
| 76 | offset = i_size_read(inode) & ~PAGE_CACHE_MASK; |
| 77 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
| 78 | } |
| 79 | set_page_dirty(page); |
| 80 | SetPageUptodate(page); |
| 81 | |
Jaegeuk Kim | e943a10 | 2013-10-25 14:26:31 +0900 | [diff] [blame] | 82 | trace_f2fs_vm_page_mkwrite(page, DATA); |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 83 | mapped: |
| 84 | /* fill the page */ |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 85 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 86 | out: |
| 87 | sb_end_pagefault(inode->i_sb); |
| 88 | return block_page_mkwrite_return(err); |
| 89 | } |
| 90 | |
| 91 | static const struct vm_operations_struct f2fs_file_vm_ops = { |
Jaegeuk Kim | 692bb55 | 2013-01-17 18:37:41 +0900 | [diff] [blame] | 92 | .fault = filemap_fault, |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 93 | .map_pages = filemap_map_pages, |
Jaegeuk Kim | 692bb55 | 2013-01-17 18:37:41 +0900 | [diff] [blame] | 94 | .page_mkwrite = f2fs_vm_page_mkwrite, |
| 95 | .remap_pages = generic_file_remap_pages, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 96 | }; |
| 97 | |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 98 | static int get_parent_ino(struct inode *inode, nid_t *pino) |
| 99 | { |
| 100 | struct dentry *dentry; |
| 101 | |
| 102 | inode = igrab(inode); |
| 103 | dentry = d_find_any_alias(inode); |
| 104 | iput(inode); |
| 105 | if (!dentry) |
| 106 | return 0; |
| 107 | |
Jaegeuk Kim | f0947e5 | 2013-07-22 22:12:56 +0900 | [diff] [blame] | 108 | if (update_dent_inode(inode, &dentry->d_name)) { |
| 109 | dput(dentry); |
| 110 | return 0; |
| 111 | } |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 112 | |
Jaegeuk Kim | f0947e5 | 2013-07-22 22:12:56 +0900 | [diff] [blame] | 113 | *pino = parent_ino(dentry); |
| 114 | dput(dentry); |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 115 | return 1; |
| 116 | } |
| 117 | |
Chao Yu | 9d1589e | 2014-08-20 18:37:35 +0800 | [diff] [blame] | 118 | static inline bool need_do_checkpoint(struct inode *inode) |
| 119 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 120 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Chao Yu | 9d1589e | 2014-08-20 18:37:35 +0800 | [diff] [blame] | 121 | bool need_cp = false; |
| 122 | |
| 123 | if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1) |
| 124 | need_cp = true; |
| 125 | else if (file_wrong_pino(inode)) |
| 126 | need_cp = true; |
| 127 | else if (!space_for_roll_forward(sbi)) |
| 128 | need_cp = true; |
| 129 | else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino)) |
| 130 | need_cp = true; |
| 131 | else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi))) |
| 132 | need_cp = true; |
| 133 | |
| 134 | return need_cp; |
| 135 | } |
| 136 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 137 | int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) |
| 138 | { |
| 139 | struct inode *inode = file->f_mapping->host; |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 140 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 141 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 142 | nid_t ino = inode->i_ino; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 143 | int ret = 0; |
| 144 | bool need_cp = false; |
| 145 | struct writeback_control wbc = { |
Jaegeuk Kim | c81bf1c | 2014-03-03 11:28:40 +0900 | [diff] [blame] | 146 | .sync_mode = WB_SYNC_ALL, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 147 | .nr_to_write = LONG_MAX, |
| 148 | .for_reclaim = 0, |
| 149 | }; |
| 150 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 151 | if (unlikely(f2fs_readonly(inode->i_sb))) |
Namjae Jeon | 1fa95b0 | 2012-12-01 10:56:01 +0900 | [diff] [blame] | 152 | return 0; |
| 153 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 154 | trace_f2fs_sync_file_enter(inode); |
Jaegeuk Kim | ea1aa12 | 2014-07-24 19:11:43 -0700 | [diff] [blame] | 155 | |
| 156 | /* if fdatasync is triggered, let's do in-place-update */ |
Jaegeuk Kim | c1ce1b0 | 2014-09-10 16:53:02 -0700 | [diff] [blame] | 157 | if (get_dirty_pages(inode) <= SM_I(sbi)->min_fsync_blocks) |
Jaegeuk Kim | ea1aa12 | 2014-07-24 19:11:43 -0700 | [diff] [blame] | 158 | set_inode_flag(fi, FI_NEED_IPU); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 159 | ret = filemap_write_and_wait_range(inode->i_mapping, start, end); |
Jaegeuk Kim | c1ce1b0 | 2014-09-10 16:53:02 -0700 | [diff] [blame] | 160 | clear_inode_flag(fi, FI_NEED_IPU); |
| 161 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 162 | if (ret) { |
| 163 | trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 164 | return ret; |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 165 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 166 | |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 167 | /* |
| 168 | * if there is no written data, don't waste time to write recovery info. |
| 169 | */ |
| 170 | if (!is_inode_flag_set(fi, FI_APPEND_WRITE) && |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 171 | !exist_written_data(sbi, ino, APPEND_INO)) { |
Jaegeuk Kim | 19c9c46 | 2014-09-10 15:04:03 -0700 | [diff] [blame] | 172 | struct page *i = find_get_page(NODE_MAPPING(sbi), ino); |
| 173 | |
| 174 | /* But we need to avoid that there are some inode updates */ |
| 175 | if ((i && PageDirty(i)) || need_inode_block_update(sbi, ino)) { |
| 176 | f2fs_put_page(i, 0); |
| 177 | goto go_write; |
| 178 | } |
| 179 | f2fs_put_page(i, 0); |
| 180 | |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 181 | if (is_inode_flag_set(fi, FI_UPDATE_WRITE) || |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 182 | exist_written_data(sbi, ino, UPDATE_INO)) |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 183 | goto flush_out; |
| 184 | goto out; |
| 185 | } |
Jaegeuk Kim | 19c9c46 | 2014-09-10 15:04:03 -0700 | [diff] [blame] | 186 | go_write: |
Jaegeuk Kim | 7d82db8 | 2013-01-11 13:10:49 +0900 | [diff] [blame] | 187 | /* guarantee free sections for fsync */ |
| 188 | f2fs_balance_fs(sbi); |
| 189 | |
Jaegeuk Kim | e5d2385 | 2013-07-03 10:55:52 +0900 | [diff] [blame] | 190 | /* |
| 191 | * Both of fdatasync() and fsync() are able to be recovered from |
| 192 | * sudden-power-off. |
| 193 | */ |
Chao Yu | 9d1589e | 2014-08-20 18:37:35 +0800 | [diff] [blame] | 194 | down_read(&fi->i_sem); |
| 195 | need_cp = need_do_checkpoint(inode); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 196 | up_read(&fi->i_sem); |
| 197 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 198 | if (need_cp) { |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 199 | nid_t pino; |
| 200 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 201 | /* all the dirty node pages should be flushed for POR */ |
| 202 | ret = f2fs_sync_fs(inode->i_sb, 1); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 203 | |
| 204 | down_write(&fi->i_sem); |
| 205 | F2FS_I(inode)->xattr_ver = 0; |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 206 | if (file_wrong_pino(inode) && inode->i_nlink == 1 && |
| 207 | get_parent_ino(inode, &pino)) { |
| 208 | F2FS_I(inode)->i_pino = pino; |
| 209 | file_got_pino(inode); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 210 | up_write(&fi->i_sem); |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 211 | mark_inode_dirty_sync(inode); |
| 212 | ret = f2fs_write_inode(inode, NULL); |
| 213 | if (ret) |
| 214 | goto out; |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 215 | } else { |
| 216 | up_write(&fi->i_sem); |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 217 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 218 | } else { |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 219 | sync_nodes: |
| 220 | sync_node_pages(sbi, ino, &wbc); |
| 221 | |
| 222 | if (need_inode_block_update(sbi, ino)) { |
Jaegeuk Kim | b378387 | 2013-06-10 09:17:01 +0900 | [diff] [blame] | 223 | mark_inode_dirty_sync(inode); |
Jaegeuk Kim | 398b1ac | 2012-12-19 15:28:39 +0900 | [diff] [blame] | 224 | ret = f2fs_write_inode(inode, NULL); |
| 225 | if (ret) |
| 226 | goto out; |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 227 | goto sync_nodes; |
Jaegeuk Kim | 398b1ac | 2012-12-19 15:28:39 +0900 | [diff] [blame] | 228 | } |
Jaegeuk Kim | 88bd02c | 2014-09-15 14:50:48 -0700 | [diff] [blame] | 229 | |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 230 | ret = wait_on_node_pages_writeback(sbi, ino); |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 231 | if (ret) |
| 232 | goto out; |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 233 | |
| 234 | /* once recovery info is written, don't need to tack this */ |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 235 | remove_dirty_inode(sbi, ino, APPEND_INO); |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 236 | clear_inode_flag(fi, FI_APPEND_WRITE); |
| 237 | flush_out: |
Jaegeuk Kim | 2403c15 | 2014-09-10 14:58:18 -0700 | [diff] [blame] | 238 | remove_dirty_inode(sbi, ino, UPDATE_INO); |
Jaegeuk Kim | 6d99ba41 | 2014-07-24 19:08:02 -0700 | [diff] [blame] | 239 | clear_inode_flag(fi, FI_UPDATE_WRITE); |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 240 | ret = f2fs_issue_flush(F2FS_I_SB(inode)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 241 | } |
| 242 | out: |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 243 | trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 244 | return ret; |
| 245 | } |
| 246 | |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 247 | static pgoff_t __get_first_dirty_index(struct address_space *mapping, |
| 248 | pgoff_t pgofs, int whence) |
| 249 | { |
| 250 | struct pagevec pvec; |
| 251 | int nr_pages; |
| 252 | |
| 253 | if (whence != SEEK_DATA) |
| 254 | return 0; |
| 255 | |
| 256 | /* find first dirty page index */ |
| 257 | pagevec_init(&pvec, 0); |
Jaegeuk Kim | 65b85cc | 2014-07-30 17:25:54 -0700 | [diff] [blame] | 258 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &pgofs, |
| 259 | PAGECACHE_TAG_DIRTY, 1); |
| 260 | pgofs = nr_pages ? pvec.pages[0]->index : LONG_MAX; |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 261 | pagevec_release(&pvec); |
| 262 | return pgofs; |
| 263 | } |
| 264 | |
| 265 | static bool __found_offset(block_t blkaddr, pgoff_t dirty, pgoff_t pgofs, |
| 266 | int whence) |
| 267 | { |
| 268 | switch (whence) { |
| 269 | case SEEK_DATA: |
| 270 | if ((blkaddr == NEW_ADDR && dirty == pgofs) || |
| 271 | (blkaddr != NEW_ADDR && blkaddr != NULL_ADDR)) |
| 272 | return true; |
| 273 | break; |
| 274 | case SEEK_HOLE: |
| 275 | if (blkaddr == NULL_ADDR) |
| 276 | return true; |
| 277 | break; |
| 278 | } |
| 279 | return false; |
| 280 | } |
| 281 | |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 282 | static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence) |
| 283 | { |
| 284 | struct inode *inode = file->f_mapping->host; |
| 285 | loff_t maxbytes = inode->i_sb->s_maxbytes; |
| 286 | struct dnode_of_data dn; |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 287 | pgoff_t pgofs, end_offset, dirty; |
| 288 | loff_t data_ofs = offset; |
| 289 | loff_t isize; |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 290 | int err = 0; |
| 291 | |
| 292 | mutex_lock(&inode->i_mutex); |
| 293 | |
| 294 | isize = i_size_read(inode); |
| 295 | if (offset >= isize) |
| 296 | goto fail; |
| 297 | |
| 298 | /* handle inline data case */ |
| 299 | if (f2fs_has_inline_data(inode)) { |
| 300 | if (whence == SEEK_HOLE) |
| 301 | data_ofs = isize; |
| 302 | goto found; |
| 303 | } |
| 304 | |
| 305 | pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT); |
| 306 | |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 307 | dirty = __get_first_dirty_index(inode->i_mapping, pgofs, whence); |
| 308 | |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 309 | for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) { |
| 310 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 311 | err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA); |
| 312 | if (err && err != -ENOENT) { |
| 313 | goto fail; |
| 314 | } else if (err == -ENOENT) { |
arter97 | e1c4204 | 2014-08-06 23:22:50 +0900 | [diff] [blame] | 315 | /* direct node does not exists */ |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 316 | if (whence == SEEK_DATA) { |
| 317 | pgofs = PGOFS_OF_NEXT_DNODE(pgofs, |
| 318 | F2FS_I(inode)); |
| 319 | continue; |
| 320 | } else { |
| 321 | goto found; |
| 322 | } |
| 323 | } |
| 324 | |
Chao Yu | 6b2920a | 2014-07-07 11:21:59 +0800 | [diff] [blame] | 325 | end_offset = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 326 | |
| 327 | /* find data/hole in dnode block */ |
| 328 | for (; dn.ofs_in_node < end_offset; |
| 329 | dn.ofs_in_node++, pgofs++, |
| 330 | data_ofs = pgofs << PAGE_CACHE_SHIFT) { |
| 331 | block_t blkaddr; |
| 332 | blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); |
| 333 | |
Jaegeuk Kim | 7f7670f | 2014-04-28 18:12:36 +0900 | [diff] [blame] | 334 | if (__found_offset(blkaddr, dirty, pgofs, whence)) { |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 335 | f2fs_put_dnode(&dn); |
| 336 | goto found; |
| 337 | } |
| 338 | } |
| 339 | f2fs_put_dnode(&dn); |
| 340 | } |
| 341 | |
| 342 | if (whence == SEEK_DATA) |
| 343 | goto fail; |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 344 | found: |
Jaegeuk Kim | fe369bc | 2014-04-28 17:02:48 +0900 | [diff] [blame] | 345 | if (whence == SEEK_HOLE && data_ofs > isize) |
| 346 | data_ofs = isize; |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 347 | mutex_unlock(&inode->i_mutex); |
| 348 | return vfs_setpos(file, data_ofs, maxbytes); |
| 349 | fail: |
| 350 | mutex_unlock(&inode->i_mutex); |
| 351 | return -ENXIO; |
| 352 | } |
| 353 | |
| 354 | static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence) |
| 355 | { |
| 356 | struct inode *inode = file->f_mapping->host; |
| 357 | loff_t maxbytes = inode->i_sb->s_maxbytes; |
| 358 | |
| 359 | switch (whence) { |
| 360 | case SEEK_SET: |
| 361 | case SEEK_CUR: |
| 362 | case SEEK_END: |
| 363 | return generic_file_llseek_size(file, offset, whence, |
| 364 | maxbytes, i_size_read(inode)); |
| 365 | case SEEK_DATA: |
| 366 | case SEEK_HOLE: |
Jaegeuk Kim | 0b4c5afde | 2014-09-08 10:59:43 -0700 | [diff] [blame] | 367 | if (offset < 0) |
| 368 | return -ENXIO; |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 369 | return f2fs_seek_block(file, offset, whence); |
| 370 | } |
| 371 | |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 375 | static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 376 | { |
| 377 | file_accessed(file); |
| 378 | vma->vm_ops = &f2fs_file_vm_ops; |
| 379 | return 0; |
| 380 | } |
| 381 | |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 382 | int truncate_data_blocks_range(struct dnode_of_data *dn, int count) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 383 | { |
| 384 | int nr_free = 0, ofs = dn->ofs_in_node; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 385 | struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 386 | struct f2fs_node *raw_node; |
| 387 | __le32 *addr; |
| 388 | |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 389 | raw_node = F2FS_NODE(dn->node_page); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 390 | addr = blkaddr_in_node(raw_node) + ofs; |
| 391 | |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 392 | for (; count > 0; count--, addr++, dn->ofs_in_node++) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 393 | block_t blkaddr = le32_to_cpu(*addr); |
| 394 | if (blkaddr == NULL_ADDR) |
| 395 | continue; |
| 396 | |
| 397 | update_extent_cache(NULL_ADDR, dn); |
| 398 | invalidate_blocks(sbi, blkaddr); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 399 | nr_free++; |
| 400 | } |
| 401 | if (nr_free) { |
Namjae Jeon | d7cc950 | 2013-06-08 21:25:40 +0900 | [diff] [blame] | 402 | dec_valid_block_count(sbi, dn->inode, nr_free); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 403 | set_page_dirty(dn->node_page); |
| 404 | sync_inode_page(dn); |
| 405 | } |
| 406 | dn->ofs_in_node = ofs; |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 407 | |
| 408 | trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid, |
| 409 | dn->ofs_in_node, nr_free); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 410 | return nr_free; |
| 411 | } |
| 412 | |
| 413 | void truncate_data_blocks(struct dnode_of_data *dn) |
| 414 | { |
| 415 | truncate_data_blocks_range(dn, ADDRS_PER_BLOCK); |
| 416 | } |
| 417 | |
| 418 | static void truncate_partial_data_page(struct inode *inode, u64 from) |
| 419 | { |
| 420 | unsigned offset = from & (PAGE_CACHE_SIZE - 1); |
| 421 | struct page *page; |
| 422 | |
Chao Yu | 8aa6f1c | 2014-04-29 09:03:03 +0800 | [diff] [blame] | 423 | if (f2fs_has_inline_data(inode)) |
| 424 | return truncate_inline_data(inode, from); |
| 425 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 426 | if (!offset) |
| 427 | return; |
| 428 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 429 | page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 430 | if (IS_ERR(page)) |
| 431 | return; |
| 432 | |
| 433 | lock_page(page); |
Chao Yu | ca0a81b | 2014-06-12 13:31:50 +0800 | [diff] [blame] | 434 | if (unlikely(!PageUptodate(page) || |
| 435 | page->mapping != inode->i_mapping)) |
| 436 | goto out; |
| 437 | |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 438 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 439 | zero_user(page, offset, PAGE_CACHE_SIZE - offset); |
| 440 | set_page_dirty(page); |
Chao Yu | ca0a81b | 2014-06-12 13:31:50 +0800 | [diff] [blame] | 441 | |
| 442 | out: |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 443 | f2fs_put_page(page, 1); |
| 444 | } |
| 445 | |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 446 | int truncate_blocks(struct inode *inode, u64 from, bool lock) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 447 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 448 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 449 | unsigned int blocksize = inode->i_sb->s_blocksize; |
| 450 | struct dnode_of_data dn; |
| 451 | pgoff_t free_from; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 452 | int count = 0, err = 0; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 453 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 454 | trace_f2fs_truncate_blocks_enter(inode, from); |
| 455 | |
Jaegeuk Kim | f185ff9 | 2013-12-27 11:01:54 +0900 | [diff] [blame] | 456 | if (f2fs_has_inline_data(inode)) |
| 457 | goto done; |
| 458 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 459 | free_from = (pgoff_t) |
| 460 | ((from + blocksize - 1) >> (sbi->log_blocksize)); |
| 461 | |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 462 | if (lock) |
| 463 | f2fs_lock_op(sbi); |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 464 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 465 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 466 | err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 467 | if (err) { |
| 468 | if (err == -ENOENT) |
| 469 | goto free_next; |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 470 | if (lock) |
| 471 | f2fs_unlock_op(sbi); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 472 | trace_f2fs_truncate_blocks_exit(inode, err); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 473 | return err; |
| 474 | } |
| 475 | |
Chao Yu | 6403eb1 | 2014-04-26 19:59:52 +0800 | [diff] [blame] | 476 | count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 477 | |
| 478 | count -= dn.ofs_in_node; |
Jaegeuk Kim | 9850cf4 | 2014-09-02 15:52:58 -0700 | [diff] [blame] | 479 | f2fs_bug_on(sbi, count < 0); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 480 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 481 | if (dn.ofs_in_node || IS_INODE(dn.node_page)) { |
| 482 | truncate_data_blocks_range(&dn, count); |
| 483 | free_from += count; |
| 484 | } |
| 485 | |
| 486 | f2fs_put_dnode(&dn); |
| 487 | free_next: |
| 488 | err = truncate_inode_blocks(inode, free_from); |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 489 | if (lock) |
| 490 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | f185ff9 | 2013-12-27 11:01:54 +0900 | [diff] [blame] | 491 | done: |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 492 | /* lastly zero out the first data page */ |
| 493 | truncate_partial_data_page(inode, from); |
| 494 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 495 | trace_f2fs_truncate_blocks_exit(inode, err); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 496 | return err; |
| 497 | } |
| 498 | |
| 499 | void f2fs_truncate(struct inode *inode) |
| 500 | { |
| 501 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 502 | S_ISLNK(inode->i_mode))) |
| 503 | return; |
| 504 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 505 | trace_f2fs_truncate(inode); |
| 506 | |
Jaegeuk Kim | 764aa3e | 2014-08-14 16:32:54 -0700 | [diff] [blame] | 507 | if (!truncate_blocks(inode, i_size_read(inode), true)) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 508 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 509 | mark_inode_dirty(inode); |
| 510 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 511 | } |
| 512 | |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 513 | int f2fs_getattr(struct vfsmount *mnt, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 514 | struct dentry *dentry, struct kstat *stat) |
| 515 | { |
| 516 | struct inode *inode = dentry->d_inode; |
| 517 | generic_fillattr(inode, stat); |
| 518 | stat->blocks <<= 3; |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
| 523 | static void __setattr_copy(struct inode *inode, const struct iattr *attr) |
| 524 | { |
| 525 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 526 | unsigned int ia_valid = attr->ia_valid; |
| 527 | |
| 528 | if (ia_valid & ATTR_UID) |
| 529 | inode->i_uid = attr->ia_uid; |
| 530 | if (ia_valid & ATTR_GID) |
| 531 | inode->i_gid = attr->ia_gid; |
| 532 | if (ia_valid & ATTR_ATIME) |
| 533 | inode->i_atime = timespec_trunc(attr->ia_atime, |
| 534 | inode->i_sb->s_time_gran); |
| 535 | if (ia_valid & ATTR_MTIME) |
| 536 | inode->i_mtime = timespec_trunc(attr->ia_mtime, |
| 537 | inode->i_sb->s_time_gran); |
| 538 | if (ia_valid & ATTR_CTIME) |
| 539 | inode->i_ctime = timespec_trunc(attr->ia_ctime, |
| 540 | inode->i_sb->s_time_gran); |
| 541 | if (ia_valid & ATTR_MODE) { |
| 542 | umode_t mode = attr->ia_mode; |
| 543 | |
| 544 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) |
| 545 | mode &= ~S_ISGID; |
| 546 | set_acl_inode(fi, mode); |
| 547 | } |
| 548 | } |
| 549 | #else |
| 550 | #define __setattr_copy setattr_copy |
| 551 | #endif |
| 552 | |
| 553 | int f2fs_setattr(struct dentry *dentry, struct iattr *attr) |
| 554 | { |
| 555 | struct inode *inode = dentry->d_inode; |
| 556 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 557 | int err; |
| 558 | |
| 559 | err = inode_change_ok(inode, attr); |
| 560 | if (err) |
| 561 | return err; |
| 562 | |
Chao Yu | 09db6a2 | 2014-09-15 18:02:09 +0800 | [diff] [blame^] | 563 | if (attr->ia_valid & ATTR_SIZE) { |
Jaegeuk Kim | b067ba1f | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 564 | err = f2fs_convert_inline_data(inode, attr->ia_size, NULL); |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 565 | if (err) |
| 566 | return err; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 567 | |
Chao Yu | 09db6a2 | 2014-09-15 18:02:09 +0800 | [diff] [blame^] | 568 | if (attr->ia_size != i_size_read(inode)) { |
| 569 | truncate_setsize(inode, attr->ia_size); |
| 570 | f2fs_truncate(inode); |
| 571 | f2fs_balance_fs(F2FS_I_SB(inode)); |
| 572 | } else { |
| 573 | /* |
| 574 | * giving a chance to truncate blocks past EOF which |
| 575 | * are fallocated with FALLOC_FL_KEEP_SIZE. |
| 576 | */ |
| 577 | f2fs_truncate(inode); |
| 578 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | __setattr_copy(inode, attr); |
| 582 | |
| 583 | if (attr->ia_valid & ATTR_MODE) { |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 584 | err = posix_acl_chmod(inode, get_inode_mode(inode)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 585 | if (err || is_inode_flag_set(fi, FI_ACL_MODE)) { |
| 586 | inode->i_mode = fi->i_acl_mode; |
| 587 | clear_inode_flag(fi, FI_ACL_MODE); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | mark_inode_dirty(inode); |
| 592 | return err; |
| 593 | } |
| 594 | |
| 595 | const struct inode_operations f2fs_file_inode_operations = { |
| 596 | .getattr = f2fs_getattr, |
| 597 | .setattr = f2fs_setattr, |
| 598 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 599 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 600 | #ifdef CONFIG_F2FS_FS_XATTR |
| 601 | .setxattr = generic_setxattr, |
| 602 | .getxattr = generic_getxattr, |
| 603 | .listxattr = f2fs_listxattr, |
| 604 | .removexattr = generic_removexattr, |
| 605 | #endif |
Jaegeuk Kim | 9ab7013 | 2014-06-08 04:30:14 +0900 | [diff] [blame] | 606 | .fiemap = f2fs_fiemap, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 607 | }; |
| 608 | |
| 609 | static void fill_zero(struct inode *inode, pgoff_t index, |
| 610 | loff_t start, loff_t len) |
| 611 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 612 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 613 | struct page *page; |
| 614 | |
| 615 | if (!len) |
| 616 | return; |
| 617 | |
Jaegeuk Kim | bd43df0 | 2013-01-25 18:33:41 +0900 | [diff] [blame] | 618 | f2fs_balance_fs(sbi); |
| 619 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 620 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 621 | page = get_new_data_page(inode, NULL, index, false); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 622 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 623 | |
| 624 | if (!IS_ERR(page)) { |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 625 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 626 | zero_user(page, start, len); |
| 627 | set_page_dirty(page); |
| 628 | f2fs_put_page(page, 1); |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end) |
| 633 | { |
| 634 | pgoff_t index; |
| 635 | int err; |
| 636 | |
| 637 | for (index = pg_start; index < pg_end; index++) { |
| 638 | struct dnode_of_data dn; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 639 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 640 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 641 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 642 | if (err) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 643 | if (err == -ENOENT) |
| 644 | continue; |
| 645 | return err; |
| 646 | } |
| 647 | |
| 648 | if (dn.data_blkaddr != NULL_ADDR) |
| 649 | truncate_data_blocks_range(&dn, 1); |
| 650 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 651 | } |
| 652 | return 0; |
| 653 | } |
| 654 | |
Chao Yu | a66c7b2 | 2013-11-22 16:52:50 +0800 | [diff] [blame] | 655 | static int punch_hole(struct inode *inode, loff_t offset, loff_t len) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 656 | { |
| 657 | pgoff_t pg_start, pg_end; |
| 658 | loff_t off_start, off_end; |
| 659 | int ret = 0; |
| 660 | |
Jaegeuk Kim | b067ba1f | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 661 | ret = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1, NULL); |
Jaegeuk Kim | 8230a0a | 2013-12-27 11:13:21 +0900 | [diff] [blame] | 662 | if (ret) |
| 663 | return ret; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 664 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 665 | pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT; |
| 666 | pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT; |
| 667 | |
| 668 | off_start = offset & (PAGE_CACHE_SIZE - 1); |
| 669 | off_end = (offset + len) & (PAGE_CACHE_SIZE - 1); |
| 670 | |
| 671 | if (pg_start == pg_end) { |
| 672 | fill_zero(inode, pg_start, off_start, |
| 673 | off_end - off_start); |
| 674 | } else { |
| 675 | if (off_start) |
| 676 | fill_zero(inode, pg_start++, off_start, |
| 677 | PAGE_CACHE_SIZE - off_start); |
| 678 | if (off_end) |
| 679 | fill_zero(inode, pg_end, 0, off_end); |
| 680 | |
| 681 | if (pg_start < pg_end) { |
| 682 | struct address_space *mapping = inode->i_mapping; |
| 683 | loff_t blk_start, blk_end; |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 684 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jason Hrycay | 1127a3d | 2013-04-08 20:16:44 -0500 | [diff] [blame] | 685 | |
| 686 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 687 | |
| 688 | blk_start = pg_start << PAGE_CACHE_SHIFT; |
| 689 | blk_end = pg_end << PAGE_CACHE_SHIFT; |
| 690 | truncate_inode_pages_range(mapping, blk_start, |
| 691 | blk_end - 1); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 692 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 693 | f2fs_lock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 694 | ret = truncate_hole(inode, pg_start, pg_end); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 695 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 699 | return ret; |
| 700 | } |
| 701 | |
| 702 | static int expand_inode_data(struct inode *inode, loff_t offset, |
| 703 | loff_t len, int mode) |
| 704 | { |
Jaegeuk Kim | 4081363 | 2014-09-02 15:31:18 -0700 | [diff] [blame] | 705 | struct f2fs_sb_info *sbi = F2FS_I_SB(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 706 | pgoff_t index, pg_start, pg_end; |
| 707 | loff_t new_size = i_size_read(inode); |
| 708 | loff_t off_start, off_end; |
| 709 | int ret = 0; |
| 710 | |
Chao Yu | 497a093 | 2014-08-04 10:11:17 +0800 | [diff] [blame] | 711 | f2fs_balance_fs(sbi); |
| 712 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 713 | ret = inode_newsize_ok(inode, (len + offset)); |
| 714 | if (ret) |
| 715 | return ret; |
| 716 | |
Jaegeuk Kim | b067ba1f | 2014-08-07 16:32:25 -0700 | [diff] [blame] | 717 | ret = f2fs_convert_inline_data(inode, offset + len, NULL); |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 718 | if (ret) |
| 719 | return ret; |
| 720 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 721 | pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT; |
| 722 | pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT; |
| 723 | |
| 724 | off_start = offset & (PAGE_CACHE_SIZE - 1); |
| 725 | off_end = (offset + len) & (PAGE_CACHE_SIZE - 1); |
| 726 | |
Jaegeuk Kim | ead4327 | 2014-06-13 13:05:55 +0900 | [diff] [blame] | 727 | f2fs_lock_op(sbi); |
| 728 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 729 | for (index = pg_start; index <= pg_end; index++) { |
| 730 | struct dnode_of_data dn; |
| 731 | |
Jaegeuk Kim | 98397ff | 2014-06-13 13:07:31 +0900 | [diff] [blame] | 732 | if (index == pg_end && !off_end) |
| 733 | goto noalloc; |
| 734 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 735 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 736 | ret = f2fs_reserve_block(&dn, index); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 737 | if (ret) |
| 738 | break; |
Jaegeuk Kim | 98397ff | 2014-06-13 13:07:31 +0900 | [diff] [blame] | 739 | noalloc: |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 740 | if (pg_start == pg_end) |
| 741 | new_size = offset + len; |
| 742 | else if (index == pg_start && off_start) |
| 743 | new_size = (index + 1) << PAGE_CACHE_SHIFT; |
| 744 | else if (index == pg_end) |
| 745 | new_size = (index << PAGE_CACHE_SHIFT) + off_end; |
| 746 | else |
| 747 | new_size += PAGE_CACHE_SIZE; |
| 748 | } |
| 749 | |
| 750 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
| 751 | i_size_read(inode) < new_size) { |
| 752 | i_size_write(inode, new_size); |
| 753 | mark_inode_dirty(inode); |
Jaegeuk Kim | ead4327 | 2014-06-13 13:05:55 +0900 | [diff] [blame] | 754 | update_inode_page(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 755 | } |
Jaegeuk Kim | ead4327 | 2014-06-13 13:05:55 +0900 | [diff] [blame] | 756 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 757 | |
| 758 | return ret; |
| 759 | } |
| 760 | |
| 761 | static long f2fs_fallocate(struct file *file, int mode, |
| 762 | loff_t offset, loff_t len) |
| 763 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 764 | struct inode *inode = file_inode(file); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 765 | long ret; |
| 766 | |
| 767 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
| 768 | return -EOPNOTSUPP; |
| 769 | |
Chao Yu | 3375f69 | 2014-01-28 10:29:26 +0800 | [diff] [blame] | 770 | mutex_lock(&inode->i_mutex); |
| 771 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 772 | if (mode & FALLOC_FL_PUNCH_HOLE) |
Chao Yu | a66c7b2 | 2013-11-22 16:52:50 +0800 | [diff] [blame] | 773 | ret = punch_hole(inode, offset, len); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 774 | else |
| 775 | ret = expand_inode_data(inode, offset, len, mode); |
| 776 | |
Namjae Jeon | 3af60a4 | 2012-12-30 14:52:37 +0900 | [diff] [blame] | 777 | if (!ret) { |
| 778 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 779 | mark_inode_dirty(inode); |
| 780 | } |
Chao Yu | 3375f69 | 2014-01-28 10:29:26 +0800 | [diff] [blame] | 781 | |
| 782 | mutex_unlock(&inode->i_mutex); |
| 783 | |
Namjae Jeon | c01e285 | 2013-04-23 17:00:52 +0900 | [diff] [blame] | 784 | trace_f2fs_fallocate(inode, mode, offset, len, ret); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 785 | return ret; |
| 786 | } |
| 787 | |
| 788 | #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL)) |
| 789 | #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL) |
| 790 | |
| 791 | static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags) |
| 792 | { |
| 793 | if (S_ISDIR(mode)) |
| 794 | return flags; |
| 795 | else if (S_ISREG(mode)) |
| 796 | return flags & F2FS_REG_FLMASK; |
| 797 | else |
| 798 | return flags & F2FS_OTHER_FLMASK; |
| 799 | } |
| 800 | |
| 801 | long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 802 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 803 | struct inode *inode = file_inode(filp); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 804 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 805 | unsigned int flags; |
| 806 | int ret; |
| 807 | |
| 808 | switch (cmd) { |
Namjae Jeon | 6a3e8ef | 2013-06-08 21:25:28 +0900 | [diff] [blame] | 809 | case F2FS_IOC_GETFLAGS: |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 810 | flags = fi->i_flags & FS_FL_USER_VISIBLE; |
| 811 | return put_user(flags, (int __user *) arg); |
Namjae Jeon | 6a3e8ef | 2013-06-08 21:25:28 +0900 | [diff] [blame] | 812 | case F2FS_IOC_SETFLAGS: |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 813 | { |
| 814 | unsigned int oldflags; |
| 815 | |
Al Viro | bdaec33 | 2013-03-20 09:33:23 -0400 | [diff] [blame] | 816 | ret = mnt_want_write_file(filp); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 817 | if (ret) |
| 818 | return ret; |
| 819 | |
| 820 | if (!inode_owner_or_capable(inode)) { |
| 821 | ret = -EACCES; |
| 822 | goto out; |
| 823 | } |
| 824 | |
| 825 | if (get_user(flags, (int __user *) arg)) { |
| 826 | ret = -EFAULT; |
| 827 | goto out; |
| 828 | } |
| 829 | |
| 830 | flags = f2fs_mask_flags(inode->i_mode, flags); |
| 831 | |
| 832 | mutex_lock(&inode->i_mutex); |
| 833 | |
| 834 | oldflags = fi->i_flags; |
| 835 | |
| 836 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { |
| 837 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 838 | mutex_unlock(&inode->i_mutex); |
| 839 | ret = -EPERM; |
| 840 | goto out; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | flags = flags & FS_FL_USER_MODIFIABLE; |
| 845 | flags |= oldflags & ~FS_FL_USER_MODIFIABLE; |
| 846 | fi->i_flags = flags; |
| 847 | mutex_unlock(&inode->i_mutex); |
| 848 | |
| 849 | f2fs_set_inode_flags(inode); |
| 850 | inode->i_ctime = CURRENT_TIME; |
| 851 | mark_inode_dirty(inode); |
| 852 | out: |
Al Viro | bdaec33 | 2013-03-20 09:33:23 -0400 | [diff] [blame] | 853 | mnt_drop_write_file(filp); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 854 | return ret; |
| 855 | } |
| 856 | default: |
| 857 | return -ENOTTY; |
| 858 | } |
| 859 | } |
| 860 | |
Namjae Jeon | e975082 | 2013-02-04 23:41:41 +0900 | [diff] [blame] | 861 | #ifdef CONFIG_COMPAT |
| 862 | long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 863 | { |
| 864 | switch (cmd) { |
| 865 | case F2FS_IOC32_GETFLAGS: |
| 866 | cmd = F2FS_IOC_GETFLAGS; |
| 867 | break; |
| 868 | case F2FS_IOC32_SETFLAGS: |
| 869 | cmd = F2FS_IOC_SETFLAGS; |
| 870 | break; |
| 871 | default: |
| 872 | return -ENOIOCTLCMD; |
| 873 | } |
| 874 | return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
| 875 | } |
| 876 | #endif |
| 877 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 878 | const struct file_operations f2fs_file_operations = { |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame] | 879 | .llseek = f2fs_llseek, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 880 | .read = new_sync_read, |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 881 | .write = new_sync_write, |
Al Viro | aad4f8b | 2014-04-02 14:33:16 -0400 | [diff] [blame] | 882 | .read_iter = generic_file_read_iter, |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 883 | .write_iter = generic_file_write_iter, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 884 | .open = generic_file_open, |
| 885 | .mmap = f2fs_file_mmap, |
| 886 | .fsync = f2fs_sync_file, |
| 887 | .fallocate = f2fs_fallocate, |
| 888 | .unlocked_ioctl = f2fs_ioctl, |
Namjae Jeon | e975082 | 2013-02-04 23:41:41 +0900 | [diff] [blame] | 889 | #ifdef CONFIG_COMPAT |
| 890 | .compat_ioctl = f2fs_compat_ioctl, |
| 891 | #endif |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 892 | .splice_read = generic_file_splice_read, |
Al Viro | 8d02076 | 2014-04-05 04:27:08 -0400 | [diff] [blame] | 893 | .splice_write = iter_file_splice_write, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 894 | }; |