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