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