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> |
| 22 | |
| 23 | #include "f2fs.h" |
| 24 | #include "node.h" |
| 25 | #include "segment.h" |
| 26 | #include "xattr.h" |
| 27 | #include "acl.h" |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 28 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 29 | |
| 30 | static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma, |
| 31 | struct vm_fault *vmf) |
| 32 | { |
| 33 | struct page *page = vmf->page; |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 34 | struct inode *inode = file_inode(vma->vm_file); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 35 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 36 | block_t old_blk_addr; |
| 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); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 47 | err = get_dnode_of_data(&dn, page->index, ALLOC_NODE); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 48 | if (err) { |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 49 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 50 | goto out; |
| 51 | } |
| 52 | |
| 53 | old_blk_addr = dn.data_blkaddr; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 54 | |
| 55 | if (old_blk_addr == NULL_ADDR) { |
| 56 | err = reserve_new_block(&dn); |
| 57 | if (err) { |
| 58 | f2fs_put_dnode(&dn); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 59 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 60 | goto out; |
| 61 | } |
| 62 | } |
| 63 | f2fs_put_dnode(&dn); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 64 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 65 | |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 66 | file_update_time(vma->vm_file); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 67 | lock_page(page); |
| 68 | if (page->mapping != inode->i_mapping || |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 69 | page_offset(page) > i_size_read(inode) || |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 70 | !PageUptodate(page)) { |
| 71 | unlock_page(page); |
| 72 | err = -EFAULT; |
| 73 | goto out; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * check to see if the page is mapped already (no holes) |
| 78 | */ |
| 79 | if (PageMappedToDisk(page)) |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 80 | goto mapped; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 81 | |
| 82 | /* page is wholly or partially inside EOF */ |
| 83 | if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) { |
| 84 | unsigned offset; |
| 85 | offset = i_size_read(inode) & ~PAGE_CACHE_MASK; |
| 86 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
| 87 | } |
| 88 | set_page_dirty(page); |
| 89 | SetPageUptodate(page); |
| 90 | |
Jaegeuk Kim | e943a10 | 2013-10-25 14:26:31 +0900 | [diff] [blame] | 91 | trace_f2fs_vm_page_mkwrite(page, DATA); |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 92 | mapped: |
| 93 | /* fill the page */ |
| 94 | wait_on_page_writeback(page); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 95 | out: |
| 96 | sb_end_pagefault(inode->i_sb); |
| 97 | return block_page_mkwrite_return(err); |
| 98 | } |
| 99 | |
| 100 | static const struct vm_operations_struct f2fs_file_vm_ops = { |
Jaegeuk Kim | 692bb55 | 2013-01-17 18:37:41 +0900 | [diff] [blame] | 101 | .fault = filemap_fault, |
| 102 | .page_mkwrite = f2fs_vm_page_mkwrite, |
| 103 | .remap_pages = generic_file_remap_pages, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 104 | }; |
| 105 | |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 106 | static int get_parent_ino(struct inode *inode, nid_t *pino) |
| 107 | { |
| 108 | struct dentry *dentry; |
| 109 | |
| 110 | inode = igrab(inode); |
| 111 | dentry = d_find_any_alias(inode); |
| 112 | iput(inode); |
| 113 | if (!dentry) |
| 114 | return 0; |
| 115 | |
Jaegeuk Kim | f0947e5 | 2013-07-22 22:12:56 +0900 | [diff] [blame] | 116 | if (update_dent_inode(inode, &dentry->d_name)) { |
| 117 | dput(dentry); |
| 118 | return 0; |
| 119 | } |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 120 | |
Jaegeuk Kim | f0947e5 | 2013-07-22 22:12:56 +0900 | [diff] [blame] | 121 | *pino = parent_ino(dentry); |
| 122 | dput(dentry); |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 123 | return 1; |
| 124 | } |
| 125 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 126 | int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) |
| 127 | { |
| 128 | struct inode *inode = file->f_mapping->host; |
| 129 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 130 | int ret = 0; |
| 131 | bool need_cp = false; |
| 132 | struct writeback_control wbc = { |
| 133 | .sync_mode = WB_SYNC_ALL, |
| 134 | .nr_to_write = LONG_MAX, |
| 135 | .for_reclaim = 0, |
| 136 | }; |
| 137 | |
Jaegeuk Kim | 77888c1 | 2013-05-20 20:28:47 +0900 | [diff] [blame] | 138 | if (f2fs_readonly(inode->i_sb)) |
Namjae Jeon | 1fa95b0 | 2012-12-01 10:56:01 +0900 | [diff] [blame] | 139 | return 0; |
| 140 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 141 | trace_f2fs_sync_file_enter(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 142 | ret = filemap_write_and_wait_range(inode->i_mapping, start, end); |
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 | 7d82db8 | 2013-01-11 13:10:49 +0900 | [diff] [blame] | 148 | /* guarantee free sections for fsync */ |
| 149 | f2fs_balance_fs(sbi); |
| 150 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 151 | mutex_lock(&inode->i_mutex); |
| 152 | |
Jaegeuk Kim | e5d2385 | 2013-07-03 10:55:52 +0900 | [diff] [blame] | 153 | /* |
| 154 | * Both of fdatasync() and fsync() are able to be recovered from |
| 155 | * sudden-power-off. |
| 156 | */ |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 157 | if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1) |
| 158 | need_cp = true; |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 159 | else if (file_wrong_pino(inode)) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 160 | need_cp = true; |
Changman Lee | facb020 | 2013-01-31 17:16:56 +0900 | [diff] [blame] | 161 | else if (!space_for_roll_forward(sbi)) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 162 | need_cp = true; |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 163 | else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino)) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 164 | need_cp = true; |
Jaegeuk Kim | d71b556 | 2013-08-09 15:03:21 +0900 | [diff] [blame] | 165 | 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] | 166 | need_cp = true; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 167 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 168 | if (need_cp) { |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 169 | nid_t pino; |
| 170 | |
Jaegeuk Kim | e518ff8 | 2013-08-09 14:46:15 +0900 | [diff] [blame] | 171 | F2FS_I(inode)->xattr_ver = 0; |
| 172 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 173 | /* all the dirty node pages should be flushed for POR */ |
| 174 | ret = f2fs_sync_fs(inode->i_sb, 1); |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 175 | if (file_wrong_pino(inode) && inode->i_nlink == 1 && |
| 176 | get_parent_ino(inode, &pino)) { |
| 177 | F2FS_I(inode)->i_pino = pino; |
| 178 | file_got_pino(inode); |
| 179 | mark_inode_dirty_sync(inode); |
| 180 | ret = f2fs_write_inode(inode, NULL); |
| 181 | if (ret) |
| 182 | goto out; |
| 183 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 184 | } else { |
Jaegeuk Kim | 398b1ac | 2012-12-19 15:28:39 +0900 | [diff] [blame] | 185 | /* if there is no written node page, write its inode page */ |
| 186 | while (!sync_node_pages(sbi, inode->i_ino, &wbc)) { |
Jaegeuk Kim | b378387 | 2013-06-10 09:17:01 +0900 | [diff] [blame] | 187 | mark_inode_dirty_sync(inode); |
Jaegeuk Kim | 398b1ac | 2012-12-19 15:28:39 +0900 | [diff] [blame] | 188 | ret = f2fs_write_inode(inode, NULL); |
| 189 | if (ret) |
| 190 | goto out; |
| 191 | } |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame^] | 192 | ret = wait_on_node_pages_writeback(sbi, inode->i_ino); |
| 193 | if (ret) |
| 194 | goto out; |
Jaegeuk Kim | ae51fb3 | 2013-03-16 11:13:04 +0900 | [diff] [blame] | 195 | ret = blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 196 | } |
| 197 | out: |
| 198 | mutex_unlock(&inode->i_mutex); |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 199 | trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 204 | { |
| 205 | file_accessed(file); |
| 206 | vma->vm_ops = &f2fs_file_vm_ops; |
| 207 | return 0; |
| 208 | } |
| 209 | |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 210 | int truncate_data_blocks_range(struct dnode_of_data *dn, int count) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 211 | { |
| 212 | int nr_free = 0, ofs = dn->ofs_in_node; |
| 213 | struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); |
| 214 | struct f2fs_node *raw_node; |
| 215 | __le32 *addr; |
| 216 | |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 217 | raw_node = F2FS_NODE(dn->node_page); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 218 | addr = blkaddr_in_node(raw_node) + ofs; |
| 219 | |
| 220 | for ( ; count > 0; count--, addr++, dn->ofs_in_node++) { |
| 221 | block_t blkaddr = le32_to_cpu(*addr); |
| 222 | if (blkaddr == NULL_ADDR) |
| 223 | continue; |
| 224 | |
| 225 | update_extent_cache(NULL_ADDR, dn); |
| 226 | invalidate_blocks(sbi, blkaddr); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 227 | nr_free++; |
| 228 | } |
| 229 | if (nr_free) { |
Namjae Jeon | d7cc950 | 2013-06-08 21:25:40 +0900 | [diff] [blame] | 230 | dec_valid_block_count(sbi, dn->inode, nr_free); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 231 | set_page_dirty(dn->node_page); |
| 232 | sync_inode_page(dn); |
| 233 | } |
| 234 | dn->ofs_in_node = ofs; |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 235 | |
| 236 | trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid, |
| 237 | dn->ofs_in_node, nr_free); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 238 | return nr_free; |
| 239 | } |
| 240 | |
| 241 | void truncate_data_blocks(struct dnode_of_data *dn) |
| 242 | { |
| 243 | truncate_data_blocks_range(dn, ADDRS_PER_BLOCK); |
| 244 | } |
| 245 | |
| 246 | static void truncate_partial_data_page(struct inode *inode, u64 from) |
| 247 | { |
| 248 | unsigned offset = from & (PAGE_CACHE_SIZE - 1); |
| 249 | struct page *page; |
| 250 | |
| 251 | if (!offset) |
| 252 | return; |
| 253 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 254 | page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 255 | if (IS_ERR(page)) |
| 256 | return; |
| 257 | |
| 258 | lock_page(page); |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 259 | if (page->mapping != inode->i_mapping) { |
| 260 | f2fs_put_page(page, 1); |
| 261 | return; |
| 262 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 263 | wait_on_page_writeback(page); |
| 264 | zero_user(page, offset, PAGE_CACHE_SIZE - offset); |
| 265 | set_page_dirty(page); |
| 266 | f2fs_put_page(page, 1); |
| 267 | } |
| 268 | |
| 269 | static int truncate_blocks(struct inode *inode, u64 from) |
| 270 | { |
| 271 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 272 | unsigned int blocksize = inode->i_sb->s_blocksize; |
| 273 | struct dnode_of_data dn; |
| 274 | pgoff_t free_from; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 275 | int count = 0; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 276 | int err; |
| 277 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 278 | trace_f2fs_truncate_blocks_enter(inode, from); |
| 279 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 280 | free_from = (pgoff_t) |
| 281 | ((from + blocksize - 1) >> (sbi->log_blocksize)); |
| 282 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 283 | f2fs_lock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 284 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 285 | err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 286 | if (err) { |
| 287 | if (err == -ENOENT) |
| 288 | goto free_next; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 289 | f2fs_unlock_op(sbi); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 290 | trace_f2fs_truncate_blocks_exit(inode, err); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 291 | return err; |
| 292 | } |
| 293 | |
| 294 | if (IS_INODE(dn.node_page)) |
Jaegeuk Kim | de93653 | 2013-08-12 21:08:03 +0900 | [diff] [blame] | 295 | count = ADDRS_PER_INODE(F2FS_I(inode)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 296 | else |
| 297 | count = ADDRS_PER_BLOCK; |
| 298 | |
| 299 | count -= dn.ofs_in_node; |
Jaegeuk Kim | 5d56b67 | 2013-10-29 15:14:54 +0900 | [diff] [blame] | 300 | f2fs_bug_on(count < 0); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 301 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 302 | if (dn.ofs_in_node || IS_INODE(dn.node_page)) { |
| 303 | truncate_data_blocks_range(&dn, count); |
| 304 | free_from += count; |
| 305 | } |
| 306 | |
| 307 | f2fs_put_dnode(&dn); |
| 308 | free_next: |
| 309 | err = truncate_inode_blocks(inode, free_from); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 310 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 311 | |
| 312 | /* lastly zero out the first data page */ |
| 313 | truncate_partial_data_page(inode, from); |
| 314 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 315 | trace_f2fs_truncate_blocks_exit(inode, err); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 316 | return err; |
| 317 | } |
| 318 | |
| 319 | void f2fs_truncate(struct inode *inode) |
| 320 | { |
| 321 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 322 | S_ISLNK(inode->i_mode))) |
| 323 | return; |
| 324 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 325 | trace_f2fs_truncate(inode); |
| 326 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 327 | if (!truncate_blocks(inode, i_size_read(inode))) { |
| 328 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 329 | mark_inode_dirty(inode); |
| 330 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 331 | } |
| 332 | |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 333 | int f2fs_getattr(struct vfsmount *mnt, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 334 | struct dentry *dentry, struct kstat *stat) |
| 335 | { |
| 336 | struct inode *inode = dentry->d_inode; |
| 337 | generic_fillattr(inode, stat); |
| 338 | stat->blocks <<= 3; |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
| 343 | static void __setattr_copy(struct inode *inode, const struct iattr *attr) |
| 344 | { |
| 345 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 346 | unsigned int ia_valid = attr->ia_valid; |
| 347 | |
| 348 | if (ia_valid & ATTR_UID) |
| 349 | inode->i_uid = attr->ia_uid; |
| 350 | if (ia_valid & ATTR_GID) |
| 351 | inode->i_gid = attr->ia_gid; |
| 352 | if (ia_valid & ATTR_ATIME) |
| 353 | inode->i_atime = timespec_trunc(attr->ia_atime, |
| 354 | inode->i_sb->s_time_gran); |
| 355 | if (ia_valid & ATTR_MTIME) |
| 356 | inode->i_mtime = timespec_trunc(attr->ia_mtime, |
| 357 | inode->i_sb->s_time_gran); |
| 358 | if (ia_valid & ATTR_CTIME) |
| 359 | inode->i_ctime = timespec_trunc(attr->ia_ctime, |
| 360 | inode->i_sb->s_time_gran); |
| 361 | if (ia_valid & ATTR_MODE) { |
| 362 | umode_t mode = attr->ia_mode; |
| 363 | |
| 364 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) |
| 365 | mode &= ~S_ISGID; |
| 366 | set_acl_inode(fi, mode); |
| 367 | } |
| 368 | } |
| 369 | #else |
| 370 | #define __setattr_copy setattr_copy |
| 371 | #endif |
| 372 | |
| 373 | int f2fs_setattr(struct dentry *dentry, struct iattr *attr) |
| 374 | { |
| 375 | struct inode *inode = dentry->d_inode; |
| 376 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 377 | int err; |
| 378 | |
| 379 | err = inode_change_ok(inode, attr); |
| 380 | if (err) |
| 381 | return err; |
| 382 | |
| 383 | if ((attr->ia_valid & ATTR_SIZE) && |
| 384 | attr->ia_size != i_size_read(inode)) { |
| 385 | truncate_setsize(inode, attr->ia_size); |
| 386 | f2fs_truncate(inode); |
Jaegeuk Kim | d4686d56 | 2013-01-31 15:36:04 +0900 | [diff] [blame] | 387 | f2fs_balance_fs(F2FS_SB(inode->i_sb)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | __setattr_copy(inode, attr); |
| 391 | |
| 392 | if (attr->ia_valid & ATTR_MODE) { |
| 393 | err = f2fs_acl_chmod(inode); |
| 394 | if (err || is_inode_flag_set(fi, FI_ACL_MODE)) { |
| 395 | inode->i_mode = fi->i_acl_mode; |
| 396 | clear_inode_flag(fi, FI_ACL_MODE); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | mark_inode_dirty(inode); |
| 401 | return err; |
| 402 | } |
| 403 | |
| 404 | const struct inode_operations f2fs_file_inode_operations = { |
| 405 | .getattr = f2fs_getattr, |
| 406 | .setattr = f2fs_setattr, |
| 407 | .get_acl = f2fs_get_acl, |
| 408 | #ifdef CONFIG_F2FS_FS_XATTR |
| 409 | .setxattr = generic_setxattr, |
| 410 | .getxattr = generic_getxattr, |
| 411 | .listxattr = f2fs_listxattr, |
| 412 | .removexattr = generic_removexattr, |
| 413 | #endif |
| 414 | }; |
| 415 | |
| 416 | static void fill_zero(struct inode *inode, pgoff_t index, |
| 417 | loff_t start, loff_t len) |
| 418 | { |
Jaegeuk Kim | bd43df0 | 2013-01-25 18:33:41 +0900 | [diff] [blame] | 419 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 420 | struct page *page; |
| 421 | |
| 422 | if (!len) |
| 423 | return; |
| 424 | |
Jaegeuk Kim | bd43df0 | 2013-01-25 18:33:41 +0900 | [diff] [blame] | 425 | f2fs_balance_fs(sbi); |
| 426 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 427 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 428 | page = get_new_data_page(inode, NULL, index, false); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 429 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 430 | |
| 431 | if (!IS_ERR(page)) { |
| 432 | wait_on_page_writeback(page); |
| 433 | zero_user(page, start, len); |
| 434 | set_page_dirty(page); |
| 435 | f2fs_put_page(page, 1); |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end) |
| 440 | { |
| 441 | pgoff_t index; |
| 442 | int err; |
| 443 | |
| 444 | for (index = pg_start; index < pg_end; index++) { |
| 445 | struct dnode_of_data dn; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 446 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 447 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 448 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 449 | if (err) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 450 | if (err == -ENOENT) |
| 451 | continue; |
| 452 | return err; |
| 453 | } |
| 454 | |
| 455 | if (dn.data_blkaddr != NULL_ADDR) |
| 456 | truncate_data_blocks_range(&dn, 1); |
| 457 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 458 | } |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static int punch_hole(struct inode *inode, loff_t offset, loff_t len, int mode) |
| 463 | { |
| 464 | pgoff_t pg_start, pg_end; |
| 465 | loff_t off_start, off_end; |
| 466 | int ret = 0; |
| 467 | |
| 468 | pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT; |
| 469 | pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT; |
| 470 | |
| 471 | off_start = offset & (PAGE_CACHE_SIZE - 1); |
| 472 | off_end = (offset + len) & (PAGE_CACHE_SIZE - 1); |
| 473 | |
| 474 | if (pg_start == pg_end) { |
| 475 | fill_zero(inode, pg_start, off_start, |
| 476 | off_end - off_start); |
| 477 | } else { |
| 478 | if (off_start) |
| 479 | fill_zero(inode, pg_start++, off_start, |
| 480 | PAGE_CACHE_SIZE - off_start); |
| 481 | if (off_end) |
| 482 | fill_zero(inode, pg_end, 0, off_end); |
| 483 | |
| 484 | if (pg_start < pg_end) { |
| 485 | struct address_space *mapping = inode->i_mapping; |
| 486 | loff_t blk_start, blk_end; |
Jason Hrycay | 1127a3d | 2013-04-08 20:16:44 -0500 | [diff] [blame] | 487 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 488 | |
| 489 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 490 | |
| 491 | blk_start = pg_start << PAGE_CACHE_SHIFT; |
| 492 | blk_end = pg_end << PAGE_CACHE_SHIFT; |
| 493 | truncate_inode_pages_range(mapping, blk_start, |
| 494 | blk_end - 1); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 495 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 496 | f2fs_lock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 497 | ret = truncate_hole(inode, pg_start, pg_end); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 498 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
| 502 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
| 503 | i_size_read(inode) <= (offset + len)) { |
| 504 | i_size_write(inode, offset); |
| 505 | mark_inode_dirty(inode); |
| 506 | } |
| 507 | |
| 508 | return ret; |
| 509 | } |
| 510 | |
| 511 | static int expand_inode_data(struct inode *inode, loff_t offset, |
| 512 | loff_t len, int mode) |
| 513 | { |
| 514 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 515 | pgoff_t index, pg_start, pg_end; |
| 516 | loff_t new_size = i_size_read(inode); |
| 517 | loff_t off_start, off_end; |
| 518 | int ret = 0; |
| 519 | |
| 520 | ret = inode_newsize_ok(inode, (len + offset)); |
| 521 | if (ret) |
| 522 | return ret; |
| 523 | |
| 524 | pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT; |
| 525 | pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT; |
| 526 | |
| 527 | off_start = offset & (PAGE_CACHE_SIZE - 1); |
| 528 | off_end = (offset + len) & (PAGE_CACHE_SIZE - 1); |
| 529 | |
| 530 | for (index = pg_start; index <= pg_end; index++) { |
| 531 | struct dnode_of_data dn; |
| 532 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 533 | f2fs_lock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 534 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 535 | ret = get_dnode_of_data(&dn, index, ALLOC_NODE); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 536 | if (ret) { |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 537 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 538 | break; |
| 539 | } |
| 540 | |
| 541 | if (dn.data_blkaddr == NULL_ADDR) { |
| 542 | ret = reserve_new_block(&dn); |
| 543 | if (ret) { |
| 544 | f2fs_put_dnode(&dn); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 545 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 546 | break; |
| 547 | } |
| 548 | } |
| 549 | f2fs_put_dnode(&dn); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 550 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 551 | |
| 552 | if (pg_start == pg_end) |
| 553 | new_size = offset + len; |
| 554 | else if (index == pg_start && off_start) |
| 555 | new_size = (index + 1) << PAGE_CACHE_SHIFT; |
| 556 | else if (index == pg_end) |
| 557 | new_size = (index << PAGE_CACHE_SHIFT) + off_end; |
| 558 | else |
| 559 | new_size += PAGE_CACHE_SIZE; |
| 560 | } |
| 561 | |
| 562 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
| 563 | i_size_read(inode) < new_size) { |
| 564 | i_size_write(inode, new_size); |
| 565 | mark_inode_dirty(inode); |
| 566 | } |
| 567 | |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | static long f2fs_fallocate(struct file *file, int mode, |
| 572 | loff_t offset, loff_t len) |
| 573 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 574 | struct inode *inode = file_inode(file); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 575 | long ret; |
| 576 | |
| 577 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
| 578 | return -EOPNOTSUPP; |
| 579 | |
| 580 | if (mode & FALLOC_FL_PUNCH_HOLE) |
| 581 | ret = punch_hole(inode, offset, len, mode); |
| 582 | else |
| 583 | ret = expand_inode_data(inode, offset, len, mode); |
| 584 | |
Namjae Jeon | 3af60a4 | 2012-12-30 14:52:37 +0900 | [diff] [blame] | 585 | if (!ret) { |
| 586 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 587 | mark_inode_dirty(inode); |
| 588 | } |
Namjae Jeon | c01e285 | 2013-04-23 17:00:52 +0900 | [diff] [blame] | 589 | trace_f2fs_fallocate(inode, mode, offset, len, ret); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL)) |
| 594 | #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL) |
| 595 | |
| 596 | static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags) |
| 597 | { |
| 598 | if (S_ISDIR(mode)) |
| 599 | return flags; |
| 600 | else if (S_ISREG(mode)) |
| 601 | return flags & F2FS_REG_FLMASK; |
| 602 | else |
| 603 | return flags & F2FS_OTHER_FLMASK; |
| 604 | } |
| 605 | |
| 606 | long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 607 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 608 | struct inode *inode = file_inode(filp); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 609 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 610 | unsigned int flags; |
| 611 | int ret; |
| 612 | |
| 613 | switch (cmd) { |
Namjae Jeon | 6a3e8ef | 2013-06-08 21:25:28 +0900 | [diff] [blame] | 614 | case F2FS_IOC_GETFLAGS: |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 615 | flags = fi->i_flags & FS_FL_USER_VISIBLE; |
| 616 | return put_user(flags, (int __user *) arg); |
Namjae Jeon | 6a3e8ef | 2013-06-08 21:25:28 +0900 | [diff] [blame] | 617 | case F2FS_IOC_SETFLAGS: |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 618 | { |
| 619 | unsigned int oldflags; |
| 620 | |
Al Viro | bdaec33 | 2013-03-20 09:33:23 -0400 | [diff] [blame] | 621 | ret = mnt_want_write_file(filp); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 622 | if (ret) |
| 623 | return ret; |
| 624 | |
| 625 | if (!inode_owner_or_capable(inode)) { |
| 626 | ret = -EACCES; |
| 627 | goto out; |
| 628 | } |
| 629 | |
| 630 | if (get_user(flags, (int __user *) arg)) { |
| 631 | ret = -EFAULT; |
| 632 | goto out; |
| 633 | } |
| 634 | |
| 635 | flags = f2fs_mask_flags(inode->i_mode, flags); |
| 636 | |
| 637 | mutex_lock(&inode->i_mutex); |
| 638 | |
| 639 | oldflags = fi->i_flags; |
| 640 | |
| 641 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { |
| 642 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 643 | mutex_unlock(&inode->i_mutex); |
| 644 | ret = -EPERM; |
| 645 | goto out; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | flags = flags & FS_FL_USER_MODIFIABLE; |
| 650 | flags |= oldflags & ~FS_FL_USER_MODIFIABLE; |
| 651 | fi->i_flags = flags; |
| 652 | mutex_unlock(&inode->i_mutex); |
| 653 | |
| 654 | f2fs_set_inode_flags(inode); |
| 655 | inode->i_ctime = CURRENT_TIME; |
| 656 | mark_inode_dirty(inode); |
| 657 | out: |
Al Viro | bdaec33 | 2013-03-20 09:33:23 -0400 | [diff] [blame] | 658 | mnt_drop_write_file(filp); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 659 | return ret; |
| 660 | } |
| 661 | default: |
| 662 | return -ENOTTY; |
| 663 | } |
| 664 | } |
| 665 | |
Namjae Jeon | e975082 | 2013-02-04 23:41:41 +0900 | [diff] [blame] | 666 | #ifdef CONFIG_COMPAT |
| 667 | long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 668 | { |
| 669 | switch (cmd) { |
| 670 | case F2FS_IOC32_GETFLAGS: |
| 671 | cmd = F2FS_IOC_GETFLAGS; |
| 672 | break; |
| 673 | case F2FS_IOC32_SETFLAGS: |
| 674 | cmd = F2FS_IOC_SETFLAGS; |
| 675 | break; |
| 676 | default: |
| 677 | return -ENOIOCTLCMD; |
| 678 | } |
| 679 | return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
| 680 | } |
| 681 | #endif |
| 682 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 683 | const struct file_operations f2fs_file_operations = { |
| 684 | .llseek = generic_file_llseek, |
| 685 | .read = do_sync_read, |
| 686 | .write = do_sync_write, |
| 687 | .aio_read = generic_file_aio_read, |
| 688 | .aio_write = generic_file_aio_write, |
| 689 | .open = generic_file_open, |
| 690 | .mmap = f2fs_file_mmap, |
| 691 | .fsync = f2fs_sync_file, |
| 692 | .fallocate = f2fs_fallocate, |
| 693 | .unlocked_ioctl = f2fs_ioctl, |
Namjae Jeon | e975082 | 2013-02-04 23:41:41 +0900 | [diff] [blame] | 694 | #ifdef CONFIG_COMPAT |
| 695 | .compat_ioctl = f2fs_compat_ioctl, |
| 696 | #endif |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 697 | .splice_read = generic_file_splice_read, |
| 698 | .splice_write = generic_file_splice_write, |
| 699 | }; |