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