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