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