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