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