Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 2 | * fs/f2fs/file.c |
| 3 | * |
| 4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com/ |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | */ |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/f2fs_fs.h> |
| 13 | #include <linux/stat.h> |
| 14 | #include <linux/buffer_head.h> |
| 15 | #include <linux/writeback.h> |
Jaegeuk Kim | ae51fb3 | 2013-03-16 11:13:04 +0900 | [diff] [blame] | 16 | #include <linux/blkdev.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 17 | #include <linux/falloc.h> |
| 18 | #include <linux/types.h> |
Namjae Jeon | e975082 | 2013-02-04 23:41:41 +0900 | [diff] [blame] | 19 | #include <linux/compat.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 20 | #include <linux/uaccess.h> |
| 21 | #include <linux/mount.h> |
| 22 | |
| 23 | #include "f2fs.h" |
| 24 | #include "node.h" |
| 25 | #include "segment.h" |
| 26 | #include "xattr.h" |
| 27 | #include "acl.h" |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 28 | #include <trace/events/f2fs.h> |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 29 | |
| 30 | static int f2fs_vm_page_mkwrite(struct vm_area_struct *vma, |
| 31 | struct vm_fault *vmf) |
| 32 | { |
| 33 | struct page *page = vmf->page; |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 34 | struct inode *inode = file_inode(vma->vm_file); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 35 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 36 | struct dnode_of_data dn; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 37 | int err; |
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 */ |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 44 | f2fs_lock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 45 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 46 | err = f2fs_reserve_block(&dn, page->index); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 47 | f2fs_unlock_op(sbi); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 48 | if (err) |
| 49 | goto out; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 50 | |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 51 | file_update_time(vma->vm_file); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 52 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 53 | if (unlikely(page->mapping != inode->i_mapping || |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 54 | page_offset(page) > i_size_read(inode) || |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 55 | !PageUptodate(page))) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 56 | unlock_page(page); |
| 57 | err = -EFAULT; |
| 58 | goto out; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * check to see if the page is mapped already (no holes) |
| 63 | */ |
| 64 | if (PageMappedToDisk(page)) |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 65 | goto mapped; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 66 | |
| 67 | /* page is wholly or partially inside EOF */ |
| 68 | if (((page->index + 1) << PAGE_CACHE_SHIFT) > i_size_read(inode)) { |
| 69 | unsigned offset; |
| 70 | offset = i_size_read(inode) & ~PAGE_CACHE_MASK; |
| 71 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
| 72 | } |
| 73 | set_page_dirty(page); |
| 74 | SetPageUptodate(page); |
| 75 | |
Jaegeuk Kim | e943a10 | 2013-10-25 14:26:31 +0900 | [diff] [blame] | 76 | trace_f2fs_vm_page_mkwrite(page, DATA); |
Namjae Jeon | 9851e6e | 2013-04-28 09:04:18 +0900 | [diff] [blame] | 77 | mapped: |
| 78 | /* fill the page */ |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 79 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 80 | out: |
| 81 | sb_end_pagefault(inode->i_sb); |
| 82 | return block_page_mkwrite_return(err); |
| 83 | } |
| 84 | |
| 85 | static const struct vm_operations_struct f2fs_file_vm_ops = { |
Jaegeuk Kim | 692bb55 | 2013-01-17 18:37:41 +0900 | [diff] [blame] | 86 | .fault = filemap_fault, |
Kirill A. Shutemov | f182036 | 2014-04-07 15:37:19 -0700 | [diff] [blame] | 87 | .map_pages = filemap_map_pages, |
Jaegeuk Kim | 692bb55 | 2013-01-17 18:37:41 +0900 | [diff] [blame] | 88 | .page_mkwrite = f2fs_vm_page_mkwrite, |
| 89 | .remap_pages = generic_file_remap_pages, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 90 | }; |
| 91 | |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 92 | static int get_parent_ino(struct inode *inode, nid_t *pino) |
| 93 | { |
| 94 | struct dentry *dentry; |
| 95 | |
| 96 | inode = igrab(inode); |
| 97 | dentry = d_find_any_alias(inode); |
| 98 | iput(inode); |
| 99 | if (!dentry) |
| 100 | return 0; |
| 101 | |
Jaegeuk Kim | f0947e5 | 2013-07-22 22:12:56 +0900 | [diff] [blame] | 102 | if (update_dent_inode(inode, &dentry->d_name)) { |
| 103 | dput(dentry); |
| 104 | return 0; |
| 105 | } |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 106 | |
Jaegeuk Kim | f0947e5 | 2013-07-22 22:12:56 +0900 | [diff] [blame] | 107 | *pino = parent_ino(dentry); |
| 108 | dput(dentry); |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 109 | return 1; |
| 110 | } |
| 111 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 112 | int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync) |
| 113 | { |
| 114 | struct inode *inode = file->f_mapping->host; |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 115 | struct f2fs_inode_info *fi = F2FS_I(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 116 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 117 | int ret = 0; |
| 118 | bool need_cp = false; |
| 119 | struct writeback_control wbc = { |
Jaegeuk Kim | c81bf1c | 2014-03-03 11:28:40 +0900 | [diff] [blame] | 120 | .sync_mode = WB_SYNC_ALL, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 121 | .nr_to_write = LONG_MAX, |
| 122 | .for_reclaim = 0, |
| 123 | }; |
| 124 | |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 125 | if (unlikely(f2fs_readonly(inode->i_sb))) |
Namjae Jeon | 1fa95b0 | 2012-12-01 10:56:01 +0900 | [diff] [blame] | 126 | return 0; |
| 127 | |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 128 | trace_f2fs_sync_file_enter(inode); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 129 | ret = filemap_write_and_wait_range(inode->i_mapping, start, end); |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 130 | if (ret) { |
| 131 | trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 132 | return ret; |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 133 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 134 | |
Jaegeuk Kim | 7d82db8 | 2013-01-11 13:10:49 +0900 | [diff] [blame] | 135 | /* guarantee free sections for fsync */ |
| 136 | f2fs_balance_fs(sbi); |
| 137 | |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 138 | down_read(&fi->i_sem); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 139 | |
Jaegeuk Kim | e5d2385 | 2013-07-03 10:55:52 +0900 | [diff] [blame] | 140 | /* |
| 141 | * Both of fdatasync() and fsync() are able to be recovered from |
| 142 | * sudden-power-off. |
| 143 | */ |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 144 | if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1) |
| 145 | need_cp = true; |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 146 | else if (file_wrong_pino(inode)) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 147 | need_cp = true; |
Changman Lee | facb020 | 2013-01-31 17:16:56 +0900 | [diff] [blame] | 148 | else if (!space_for_roll_forward(sbi)) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 149 | need_cp = true; |
Jaegeuk Kim | 953a3e2 | 2013-03-21 15:21:57 +0900 | [diff] [blame] | 150 | else if (!is_checkpointed_node(sbi, F2FS_I(inode)->i_pino)) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 151 | need_cp = true; |
Jaegeuk Kim | d71b556 | 2013-08-09 15:03:21 +0900 | [diff] [blame] | 152 | else if (F2FS_I(inode)->xattr_ver == cur_cp_version(F2FS_CKPT(sbi))) |
Jaegeuk Kim | e518ff8 | 2013-08-09 14:46:15 +0900 | [diff] [blame] | 153 | need_cp = true; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 154 | |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 155 | up_read(&fi->i_sem); |
| 156 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 157 | if (need_cp) { |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 158 | nid_t pino; |
| 159 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 160 | /* all the dirty node pages should be flushed for POR */ |
| 161 | ret = f2fs_sync_fs(inode->i_sb, 1); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 162 | |
| 163 | down_write(&fi->i_sem); |
| 164 | F2FS_I(inode)->xattr_ver = 0; |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 165 | if (file_wrong_pino(inode) && inode->i_nlink == 1 && |
| 166 | get_parent_ino(inode, &pino)) { |
| 167 | F2FS_I(inode)->i_pino = pino; |
| 168 | file_got_pino(inode); |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 169 | up_write(&fi->i_sem); |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 170 | mark_inode_dirty_sync(inode); |
| 171 | ret = f2fs_write_inode(inode, NULL); |
| 172 | if (ret) |
| 173 | goto out; |
Jaegeuk Kim | d928bfb | 2014-03-20 19:10:08 +0900 | [diff] [blame] | 174 | } else { |
| 175 | up_write(&fi->i_sem); |
Jaegeuk Kim | 354a339 | 2013-06-14 08:52:35 +0900 | [diff] [blame] | 176 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 177 | } else { |
Jaegeuk Kim | 398b1ac | 2012-12-19 15:28:39 +0900 | [diff] [blame] | 178 | /* if there is no written node page, write its inode page */ |
| 179 | while (!sync_node_pages(sbi, inode->i_ino, &wbc)) { |
Jaegeuk Kim | 479f40c | 2014-03-20 21:52:53 +0900 | [diff] [blame] | 180 | if (fsync_mark_done(sbi, inode->i_ino)) |
| 181 | goto out; |
Jaegeuk Kim | b378387 | 2013-06-10 09:17:01 +0900 | [diff] [blame] | 182 | mark_inode_dirty_sync(inode); |
Jaegeuk Kim | 398b1ac | 2012-12-19 15:28:39 +0900 | [diff] [blame] | 183 | ret = f2fs_write_inode(inode, NULL); |
| 184 | if (ret) |
| 185 | goto out; |
| 186 | } |
Jaegeuk Kim | cfe58f9 | 2013-10-31 14:57:01 +0900 | [diff] [blame] | 187 | ret = wait_on_node_pages_writeback(sbi, inode->i_ino); |
| 188 | if (ret) |
| 189 | goto out; |
Jaegeuk Kim | 6b4afdd | 2014-04-02 15:34:36 +0900 | [diff] [blame] | 190 | ret = f2fs_issue_flush(F2FS_SB(inode->i_sb)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 191 | } |
| 192 | out: |
Namjae Jeon | a2a4a7e | 2013-04-20 01:28:40 +0900 | [diff] [blame] | 193 | trace_f2fs_sync_file_exit(inode, need_cp, datasync, ret); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 194 | return ret; |
| 195 | } |
| 196 | |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame^] | 197 | static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence) |
| 198 | { |
| 199 | struct inode *inode = file->f_mapping->host; |
| 200 | loff_t maxbytes = inode->i_sb->s_maxbytes; |
| 201 | struct dnode_of_data dn; |
| 202 | pgoff_t pgofs, end_offset; |
| 203 | loff_t data_ofs = offset, isize; |
| 204 | int err = 0; |
| 205 | |
| 206 | mutex_lock(&inode->i_mutex); |
| 207 | |
| 208 | isize = i_size_read(inode); |
| 209 | if (offset >= isize) |
| 210 | goto fail; |
| 211 | |
| 212 | /* handle inline data case */ |
| 213 | if (f2fs_has_inline_data(inode)) { |
| 214 | if (whence == SEEK_HOLE) |
| 215 | data_ofs = isize; |
| 216 | goto found; |
| 217 | } |
| 218 | |
| 219 | pgofs = (pgoff_t)(offset >> PAGE_CACHE_SHIFT); |
| 220 | |
| 221 | for (; data_ofs < isize; data_ofs = pgofs << PAGE_CACHE_SHIFT) { |
| 222 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
| 223 | err = get_dnode_of_data(&dn, pgofs, LOOKUP_NODE_RA); |
| 224 | if (err && err != -ENOENT) { |
| 225 | goto fail; |
| 226 | } else if (err == -ENOENT) { |
| 227 | /* direct node is not exist */ |
| 228 | if (whence == SEEK_DATA) { |
| 229 | pgofs = PGOFS_OF_NEXT_DNODE(pgofs, |
| 230 | F2FS_I(inode)); |
| 231 | continue; |
| 232 | } else { |
| 233 | goto found; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | end_offset = IS_INODE(dn.node_page) ? |
| 238 | ADDRS_PER_INODE(F2FS_I(inode)) : ADDRS_PER_BLOCK; |
| 239 | |
| 240 | /* find data/hole in dnode block */ |
| 241 | for (; dn.ofs_in_node < end_offset; |
| 242 | dn.ofs_in_node++, pgofs++, |
| 243 | data_ofs = pgofs << PAGE_CACHE_SHIFT) { |
| 244 | block_t blkaddr; |
| 245 | blkaddr = datablock_addr(dn.node_page, dn.ofs_in_node); |
| 246 | |
| 247 | if ((whence == SEEK_DATA && blkaddr != NULL_ADDR) || |
| 248 | (whence == SEEK_HOLE && blkaddr == NULL_ADDR)) { |
| 249 | f2fs_put_dnode(&dn); |
| 250 | goto found; |
| 251 | } |
| 252 | } |
| 253 | f2fs_put_dnode(&dn); |
| 254 | } |
| 255 | |
| 256 | if (whence == SEEK_DATA) |
| 257 | goto fail; |
| 258 | else |
| 259 | data_ofs = isize; |
| 260 | found: |
| 261 | mutex_unlock(&inode->i_mutex); |
| 262 | return vfs_setpos(file, data_ofs, maxbytes); |
| 263 | fail: |
| 264 | mutex_unlock(&inode->i_mutex); |
| 265 | return -ENXIO; |
| 266 | } |
| 267 | |
| 268 | static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence) |
| 269 | { |
| 270 | struct inode *inode = file->f_mapping->host; |
| 271 | loff_t maxbytes = inode->i_sb->s_maxbytes; |
| 272 | |
| 273 | switch (whence) { |
| 274 | case SEEK_SET: |
| 275 | case SEEK_CUR: |
| 276 | case SEEK_END: |
| 277 | return generic_file_llseek_size(file, offset, whence, |
| 278 | maxbytes, i_size_read(inode)); |
| 279 | case SEEK_DATA: |
| 280 | case SEEK_HOLE: |
| 281 | return f2fs_seek_block(file, offset, whence); |
| 282 | } |
| 283 | |
| 284 | return -EINVAL; |
| 285 | } |
| 286 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 287 | static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma) |
| 288 | { |
| 289 | file_accessed(file); |
| 290 | vma->vm_ops = &f2fs_file_vm_ops; |
| 291 | return 0; |
| 292 | } |
| 293 | |
Jaegeuk Kim | b292dcab | 2013-05-22 08:02:02 +0900 | [diff] [blame] | 294 | int truncate_data_blocks_range(struct dnode_of_data *dn, int count) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 295 | { |
| 296 | int nr_free = 0, ofs = dn->ofs_in_node; |
| 297 | struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb); |
| 298 | struct f2fs_node *raw_node; |
| 299 | __le32 *addr; |
| 300 | |
Gu Zheng | 4559071 | 2013-07-15 17:57:38 +0800 | [diff] [blame] | 301 | raw_node = F2FS_NODE(dn->node_page); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 302 | addr = blkaddr_in_node(raw_node) + ofs; |
| 303 | |
Chris Fries | 6c311ec | 2014-01-17 14:44:39 -0600 | [diff] [blame] | 304 | for (; count > 0; count--, addr++, dn->ofs_in_node++) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 305 | block_t blkaddr = le32_to_cpu(*addr); |
| 306 | if (blkaddr == NULL_ADDR) |
| 307 | continue; |
| 308 | |
| 309 | update_extent_cache(NULL_ADDR, dn); |
| 310 | invalidate_blocks(sbi, blkaddr); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 311 | nr_free++; |
| 312 | } |
| 313 | if (nr_free) { |
Namjae Jeon | d7cc950 | 2013-06-08 21:25:40 +0900 | [diff] [blame] | 314 | dec_valid_block_count(sbi, dn->inode, nr_free); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 315 | set_page_dirty(dn->node_page); |
| 316 | sync_inode_page(dn); |
| 317 | } |
| 318 | dn->ofs_in_node = ofs; |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 319 | |
| 320 | trace_f2fs_truncate_data_blocks_range(dn->inode, dn->nid, |
| 321 | dn->ofs_in_node, nr_free); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 322 | return nr_free; |
| 323 | } |
| 324 | |
| 325 | void truncate_data_blocks(struct dnode_of_data *dn) |
| 326 | { |
| 327 | truncate_data_blocks_range(dn, ADDRS_PER_BLOCK); |
| 328 | } |
| 329 | |
| 330 | static void truncate_partial_data_page(struct inode *inode, u64 from) |
| 331 | { |
| 332 | unsigned offset = from & (PAGE_CACHE_SIZE - 1); |
| 333 | struct page *page; |
| 334 | |
| 335 | if (!offset) |
| 336 | return; |
| 337 | |
Jaegeuk Kim | c718379b | 2013-04-24 13:19:56 +0900 | [diff] [blame] | 338 | page = find_data_page(inode, from >> PAGE_CACHE_SHIFT, false); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 339 | if (IS_ERR(page)) |
| 340 | return; |
| 341 | |
| 342 | lock_page(page); |
Jaegeuk Kim | 6bacf52 | 2013-12-06 15:00:58 +0900 | [diff] [blame] | 343 | if (unlikely(page->mapping != inode->i_mapping)) { |
Jaegeuk Kim | afcb7ca0 | 2013-04-26 11:55:17 +0900 | [diff] [blame] | 344 | f2fs_put_page(page, 1); |
| 345 | return; |
| 346 | } |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 347 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 348 | zero_user(page, offset, PAGE_CACHE_SIZE - offset); |
| 349 | set_page_dirty(page); |
| 350 | f2fs_put_page(page, 1); |
| 351 | } |
| 352 | |
Jaegeuk Kim | 1e1bb4b | 2013-12-26 12:49:48 +0900 | [diff] [blame] | 353 | int truncate_blocks(struct inode *inode, u64 from) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 354 | { |
| 355 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 356 | unsigned int blocksize = inode->i_sb->s_blocksize; |
| 357 | struct dnode_of_data dn; |
| 358 | pgoff_t free_from; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 359 | int count = 0, err = 0; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 360 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 361 | trace_f2fs_truncate_blocks_enter(inode, from); |
| 362 | |
Jaegeuk Kim | f185ff9 | 2013-12-27 11:01:54 +0900 | [diff] [blame] | 363 | if (f2fs_has_inline_data(inode)) |
| 364 | goto done; |
| 365 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 366 | free_from = (pgoff_t) |
| 367 | ((from + blocksize - 1) >> (sbi->log_blocksize)); |
| 368 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 369 | f2fs_lock_op(sbi); |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 370 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 371 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 372 | err = get_dnode_of_data(&dn, free_from, LOOKUP_NODE); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 373 | if (err) { |
| 374 | if (err == -ENOENT) |
| 375 | goto free_next; |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 376 | f2fs_unlock_op(sbi); |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 377 | trace_f2fs_truncate_blocks_exit(inode, err); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 378 | return err; |
| 379 | } |
| 380 | |
Chao Yu | 6403eb1 | 2014-04-26 19:59:52 +0800 | [diff] [blame] | 381 | count = ADDRS_PER_PAGE(dn.node_page, F2FS_I(inode)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 382 | |
| 383 | count -= dn.ofs_in_node; |
Jaegeuk Kim | 5d56b67 | 2013-10-29 15:14:54 +0900 | [diff] [blame] | 384 | f2fs_bug_on(count < 0); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 385 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 386 | if (dn.ofs_in_node || IS_INODE(dn.node_page)) { |
| 387 | truncate_data_blocks_range(&dn, count); |
| 388 | free_from += count; |
| 389 | } |
| 390 | |
| 391 | f2fs_put_dnode(&dn); |
| 392 | free_next: |
| 393 | err = truncate_inode_blocks(inode, free_from); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 394 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | f185ff9 | 2013-12-27 11:01:54 +0900 | [diff] [blame] | 395 | done: |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 396 | /* lastly zero out the first data page */ |
| 397 | truncate_partial_data_page(inode, from); |
| 398 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 399 | trace_f2fs_truncate_blocks_exit(inode, err); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 400 | return err; |
| 401 | } |
| 402 | |
| 403 | void f2fs_truncate(struct inode *inode) |
| 404 | { |
| 405 | if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 406 | S_ISLNK(inode->i_mode))) |
| 407 | return; |
| 408 | |
Namjae Jeon | 51dd624 | 2013-04-20 01:28:52 +0900 | [diff] [blame] | 409 | trace_f2fs_truncate(inode); |
| 410 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 411 | if (!truncate_blocks(inode, i_size_read(inode))) { |
| 412 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 413 | mark_inode_dirty(inode); |
| 414 | } |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 415 | } |
| 416 | |
Jaegeuk Kim | 2d4d9fb5 | 2013-06-07 16:33:07 +0900 | [diff] [blame] | 417 | int f2fs_getattr(struct vfsmount *mnt, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 418 | struct dentry *dentry, struct kstat *stat) |
| 419 | { |
| 420 | struct inode *inode = dentry->d_inode; |
| 421 | generic_fillattr(inode, stat); |
| 422 | stat->blocks <<= 3; |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | #ifdef CONFIG_F2FS_FS_POSIX_ACL |
| 427 | static void __setattr_copy(struct inode *inode, const struct iattr *attr) |
| 428 | { |
| 429 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 430 | unsigned int ia_valid = attr->ia_valid; |
| 431 | |
| 432 | if (ia_valid & ATTR_UID) |
| 433 | inode->i_uid = attr->ia_uid; |
| 434 | if (ia_valid & ATTR_GID) |
| 435 | inode->i_gid = attr->ia_gid; |
| 436 | if (ia_valid & ATTR_ATIME) |
| 437 | inode->i_atime = timespec_trunc(attr->ia_atime, |
| 438 | inode->i_sb->s_time_gran); |
| 439 | if (ia_valid & ATTR_MTIME) |
| 440 | inode->i_mtime = timespec_trunc(attr->ia_mtime, |
| 441 | inode->i_sb->s_time_gran); |
| 442 | if (ia_valid & ATTR_CTIME) |
| 443 | inode->i_ctime = timespec_trunc(attr->ia_ctime, |
| 444 | inode->i_sb->s_time_gran); |
| 445 | if (ia_valid & ATTR_MODE) { |
| 446 | umode_t mode = attr->ia_mode; |
| 447 | |
| 448 | if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID)) |
| 449 | mode &= ~S_ISGID; |
| 450 | set_acl_inode(fi, mode); |
| 451 | } |
| 452 | } |
| 453 | #else |
| 454 | #define __setattr_copy setattr_copy |
| 455 | #endif |
| 456 | |
| 457 | int f2fs_setattr(struct dentry *dentry, struct iattr *attr) |
| 458 | { |
| 459 | struct inode *inode = dentry->d_inode; |
| 460 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 461 | int err; |
| 462 | |
| 463 | err = inode_change_ok(inode, attr); |
| 464 | if (err) |
| 465 | return err; |
| 466 | |
| 467 | if ((attr->ia_valid & ATTR_SIZE) && |
| 468 | attr->ia_size != i_size_read(inode)) { |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 469 | err = f2fs_convert_inline_data(inode, attr->ia_size); |
| 470 | if (err) |
| 471 | return err; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 472 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 473 | truncate_setsize(inode, attr->ia_size); |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 474 | f2fs_truncate(inode); |
Jaegeuk Kim | d4686d56 | 2013-01-31 15:36:04 +0900 | [diff] [blame] | 475 | f2fs_balance_fs(F2FS_SB(inode->i_sb)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | __setattr_copy(inode, attr); |
| 479 | |
| 480 | if (attr->ia_valid & ATTR_MODE) { |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 481 | err = posix_acl_chmod(inode, get_inode_mode(inode)); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 482 | if (err || is_inode_flag_set(fi, FI_ACL_MODE)) { |
| 483 | inode->i_mode = fi->i_acl_mode; |
| 484 | clear_inode_flag(fi, FI_ACL_MODE); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | mark_inode_dirty(inode); |
| 489 | return err; |
| 490 | } |
| 491 | |
| 492 | const struct inode_operations f2fs_file_inode_operations = { |
| 493 | .getattr = f2fs_getattr, |
| 494 | .setattr = f2fs_setattr, |
| 495 | .get_acl = f2fs_get_acl, |
Christoph Hellwig | a6dda0e | 2013-12-20 05:16:45 -0800 | [diff] [blame] | 496 | .set_acl = f2fs_set_acl, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 497 | #ifdef CONFIG_F2FS_FS_XATTR |
| 498 | .setxattr = generic_setxattr, |
| 499 | .getxattr = generic_getxattr, |
| 500 | .listxattr = f2fs_listxattr, |
| 501 | .removexattr = generic_removexattr, |
| 502 | #endif |
| 503 | }; |
| 504 | |
| 505 | static void fill_zero(struct inode *inode, pgoff_t index, |
| 506 | loff_t start, loff_t len) |
| 507 | { |
Jaegeuk Kim | bd43df0 | 2013-01-25 18:33:41 +0900 | [diff] [blame] | 508 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 509 | struct page *page; |
| 510 | |
| 511 | if (!len) |
| 512 | return; |
| 513 | |
Jaegeuk Kim | bd43df0 | 2013-01-25 18:33:41 +0900 | [diff] [blame] | 514 | f2fs_balance_fs(sbi); |
| 515 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 516 | f2fs_lock_op(sbi); |
Jaegeuk Kim | 64aa7ed | 2013-05-20 09:55:50 +0900 | [diff] [blame] | 517 | page = get_new_data_page(inode, NULL, index, false); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 518 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 519 | |
| 520 | if (!IS_ERR(page)) { |
Jaegeuk Kim | 3cb5ad1 | 2014-03-18 13:29:07 +0900 | [diff] [blame] | 521 | f2fs_wait_on_page_writeback(page, DATA); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 522 | zero_user(page, start, len); |
| 523 | set_page_dirty(page); |
| 524 | f2fs_put_page(page, 1); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end) |
| 529 | { |
| 530 | pgoff_t index; |
| 531 | int err; |
| 532 | |
| 533 | for (index = pg_start; index < pg_end; index++) { |
| 534 | struct dnode_of_data dn; |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 535 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 536 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Jaegeuk Kim | 266e97a | 2013-02-26 13:10:46 +0900 | [diff] [blame] | 537 | err = get_dnode_of_data(&dn, index, LOOKUP_NODE); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 538 | if (err) { |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 539 | if (err == -ENOENT) |
| 540 | continue; |
| 541 | return err; |
| 542 | } |
| 543 | |
| 544 | if (dn.data_blkaddr != NULL_ADDR) |
| 545 | truncate_data_blocks_range(&dn, 1); |
| 546 | f2fs_put_dnode(&dn); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 547 | } |
| 548 | return 0; |
| 549 | } |
| 550 | |
Chao Yu | a66c7b2 | 2013-11-22 16:52:50 +0800 | [diff] [blame] | 551 | static int punch_hole(struct inode *inode, loff_t offset, loff_t len) |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 552 | { |
| 553 | pgoff_t pg_start, pg_end; |
| 554 | loff_t off_start, off_end; |
| 555 | int ret = 0; |
| 556 | |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 557 | ret = f2fs_convert_inline_data(inode, MAX_INLINE_DATA + 1); |
Jaegeuk Kim | 8230a0a | 2013-12-27 11:13:21 +0900 | [diff] [blame] | 558 | if (ret) |
| 559 | return ret; |
Huajun Li | 9ffe0fb | 2013-11-10 23:13:20 +0800 | [diff] [blame] | 560 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 561 | pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT; |
| 562 | pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT; |
| 563 | |
| 564 | off_start = offset & (PAGE_CACHE_SIZE - 1); |
| 565 | off_end = (offset + len) & (PAGE_CACHE_SIZE - 1); |
| 566 | |
| 567 | if (pg_start == pg_end) { |
| 568 | fill_zero(inode, pg_start, off_start, |
| 569 | off_end - off_start); |
| 570 | } else { |
| 571 | if (off_start) |
| 572 | fill_zero(inode, pg_start++, off_start, |
| 573 | PAGE_CACHE_SIZE - off_start); |
| 574 | if (off_end) |
| 575 | fill_zero(inode, pg_end, 0, off_end); |
| 576 | |
| 577 | if (pg_start < pg_end) { |
| 578 | struct address_space *mapping = inode->i_mapping; |
| 579 | loff_t blk_start, blk_end; |
Jason Hrycay | 1127a3d | 2013-04-08 20:16:44 -0500 | [diff] [blame] | 580 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 581 | |
| 582 | f2fs_balance_fs(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 583 | |
| 584 | blk_start = pg_start << PAGE_CACHE_SHIFT; |
| 585 | blk_end = pg_end << PAGE_CACHE_SHIFT; |
| 586 | truncate_inode_pages_range(mapping, blk_start, |
| 587 | blk_end - 1); |
Jaegeuk Kim | 3993683 | 2012-11-22 16:21:29 +0900 | [diff] [blame] | 588 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 589 | f2fs_lock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 590 | ret = truncate_hole(inode, pg_start, pg_end); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 591 | f2fs_unlock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 595 | return ret; |
| 596 | } |
| 597 | |
| 598 | static int expand_inode_data(struct inode *inode, loff_t offset, |
| 599 | loff_t len, int mode) |
| 600 | { |
| 601 | struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb); |
| 602 | pgoff_t index, pg_start, pg_end; |
| 603 | loff_t new_size = i_size_read(inode); |
| 604 | loff_t off_start, off_end; |
| 605 | int ret = 0; |
| 606 | |
| 607 | ret = inode_newsize_ok(inode, (len + offset)); |
| 608 | if (ret) |
| 609 | return ret; |
| 610 | |
Jaegeuk Kim | 9e09fc8 | 2013-12-27 12:28:59 +0900 | [diff] [blame] | 611 | ret = f2fs_convert_inline_data(inode, offset + len); |
| 612 | if (ret) |
| 613 | return ret; |
| 614 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 615 | pg_start = ((unsigned long long) offset) >> PAGE_CACHE_SHIFT; |
| 616 | pg_end = ((unsigned long long) offset + len) >> PAGE_CACHE_SHIFT; |
| 617 | |
| 618 | off_start = offset & (PAGE_CACHE_SIZE - 1); |
| 619 | off_end = (offset + len) & (PAGE_CACHE_SIZE - 1); |
| 620 | |
| 621 | for (index = pg_start; index <= pg_end; index++) { |
| 622 | struct dnode_of_data dn; |
| 623 | |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 624 | f2fs_lock_op(sbi); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 625 | set_new_dnode(&dn, inode, NULL, NULL, 0); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 626 | ret = f2fs_reserve_block(&dn, index); |
Gu Zheng | e479556 | 2013-09-27 18:08:30 +0800 | [diff] [blame] | 627 | f2fs_unlock_op(sbi); |
Huajun Li | b600965 | 2013-11-10 23:13:18 +0800 | [diff] [blame] | 628 | if (ret) |
| 629 | break; |
| 630 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 631 | if (pg_start == pg_end) |
| 632 | new_size = offset + len; |
| 633 | else if (index == pg_start && off_start) |
| 634 | new_size = (index + 1) << PAGE_CACHE_SHIFT; |
| 635 | else if (index == pg_end) |
| 636 | new_size = (index << PAGE_CACHE_SHIFT) + off_end; |
| 637 | else |
| 638 | new_size += PAGE_CACHE_SIZE; |
| 639 | } |
| 640 | |
| 641 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
| 642 | i_size_read(inode) < new_size) { |
| 643 | i_size_write(inode, new_size); |
| 644 | mark_inode_dirty(inode); |
| 645 | } |
| 646 | |
| 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | static long f2fs_fallocate(struct file *file, int mode, |
| 651 | loff_t offset, loff_t len) |
| 652 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 653 | struct inode *inode = file_inode(file); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 654 | long ret; |
| 655 | |
| 656 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
| 657 | return -EOPNOTSUPP; |
| 658 | |
Chao Yu | 3375f69 | 2014-01-28 10:29:26 +0800 | [diff] [blame] | 659 | mutex_lock(&inode->i_mutex); |
| 660 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 661 | if (mode & FALLOC_FL_PUNCH_HOLE) |
Chao Yu | a66c7b2 | 2013-11-22 16:52:50 +0800 | [diff] [blame] | 662 | ret = punch_hole(inode, offset, len); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 663 | else |
| 664 | ret = expand_inode_data(inode, offset, len, mode); |
| 665 | |
Namjae Jeon | 3af60a4 | 2012-12-30 14:52:37 +0900 | [diff] [blame] | 666 | if (!ret) { |
| 667 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
| 668 | mark_inode_dirty(inode); |
| 669 | } |
Chao Yu | 3375f69 | 2014-01-28 10:29:26 +0800 | [diff] [blame] | 670 | |
| 671 | mutex_unlock(&inode->i_mutex); |
| 672 | |
Namjae Jeon | c01e285 | 2013-04-23 17:00:52 +0900 | [diff] [blame] | 673 | trace_f2fs_fallocate(inode, mode, offset, len, ret); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 674 | return ret; |
| 675 | } |
| 676 | |
| 677 | #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL)) |
| 678 | #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL) |
| 679 | |
| 680 | static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags) |
| 681 | { |
| 682 | if (S_ISDIR(mode)) |
| 683 | return flags; |
| 684 | else if (S_ISREG(mode)) |
| 685 | return flags & F2FS_REG_FLMASK; |
| 686 | else |
| 687 | return flags & F2FS_OTHER_FLMASK; |
| 688 | } |
| 689 | |
| 690 | long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
| 691 | { |
Al Viro | 6131ffa | 2013-02-27 16:59:05 -0500 | [diff] [blame] | 692 | struct inode *inode = file_inode(filp); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 693 | struct f2fs_inode_info *fi = F2FS_I(inode); |
| 694 | unsigned int flags; |
| 695 | int ret; |
| 696 | |
| 697 | switch (cmd) { |
Namjae Jeon | 6a3e8ef | 2013-06-08 21:25:28 +0900 | [diff] [blame] | 698 | case F2FS_IOC_GETFLAGS: |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 699 | flags = fi->i_flags & FS_FL_USER_VISIBLE; |
| 700 | return put_user(flags, (int __user *) arg); |
Namjae Jeon | 6a3e8ef | 2013-06-08 21:25:28 +0900 | [diff] [blame] | 701 | case F2FS_IOC_SETFLAGS: |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 702 | { |
| 703 | unsigned int oldflags; |
| 704 | |
Al Viro | bdaec33 | 2013-03-20 09:33:23 -0400 | [diff] [blame] | 705 | ret = mnt_want_write_file(filp); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 706 | if (ret) |
| 707 | return ret; |
| 708 | |
| 709 | if (!inode_owner_or_capable(inode)) { |
| 710 | ret = -EACCES; |
| 711 | goto out; |
| 712 | } |
| 713 | |
| 714 | if (get_user(flags, (int __user *) arg)) { |
| 715 | ret = -EFAULT; |
| 716 | goto out; |
| 717 | } |
| 718 | |
| 719 | flags = f2fs_mask_flags(inode->i_mode, flags); |
| 720 | |
| 721 | mutex_lock(&inode->i_mutex); |
| 722 | |
| 723 | oldflags = fi->i_flags; |
| 724 | |
| 725 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { |
| 726 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
| 727 | mutex_unlock(&inode->i_mutex); |
| 728 | ret = -EPERM; |
| 729 | goto out; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | flags = flags & FS_FL_USER_MODIFIABLE; |
| 734 | flags |= oldflags & ~FS_FL_USER_MODIFIABLE; |
| 735 | fi->i_flags = flags; |
| 736 | mutex_unlock(&inode->i_mutex); |
| 737 | |
| 738 | f2fs_set_inode_flags(inode); |
| 739 | inode->i_ctime = CURRENT_TIME; |
| 740 | mark_inode_dirty(inode); |
| 741 | out: |
Al Viro | bdaec33 | 2013-03-20 09:33:23 -0400 | [diff] [blame] | 742 | mnt_drop_write_file(filp); |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 743 | return ret; |
| 744 | } |
| 745 | default: |
| 746 | return -ENOTTY; |
| 747 | } |
| 748 | } |
| 749 | |
Namjae Jeon | e975082 | 2013-02-04 23:41:41 +0900 | [diff] [blame] | 750 | #ifdef CONFIG_COMPAT |
| 751 | long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 752 | { |
| 753 | switch (cmd) { |
| 754 | case F2FS_IOC32_GETFLAGS: |
| 755 | cmd = F2FS_IOC_GETFLAGS; |
| 756 | break; |
| 757 | case F2FS_IOC32_SETFLAGS: |
| 758 | cmd = F2FS_IOC_SETFLAGS; |
| 759 | break; |
| 760 | default: |
| 761 | return -ENOIOCTLCMD; |
| 762 | } |
| 763 | return f2fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); |
| 764 | } |
| 765 | #endif |
| 766 | |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 767 | const struct file_operations f2fs_file_operations = { |
Chao Yu | 267378d | 2014-04-23 14:10:24 +0800 | [diff] [blame^] | 768 | .llseek = f2fs_llseek, |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 769 | .read = do_sync_read, |
| 770 | .write = do_sync_write, |
| 771 | .aio_read = generic_file_aio_read, |
| 772 | .aio_write = generic_file_aio_write, |
| 773 | .open = generic_file_open, |
| 774 | .mmap = f2fs_file_mmap, |
| 775 | .fsync = f2fs_sync_file, |
| 776 | .fallocate = f2fs_fallocate, |
| 777 | .unlocked_ioctl = f2fs_ioctl, |
Namjae Jeon | e975082 | 2013-02-04 23:41:41 +0900 | [diff] [blame] | 778 | #ifdef CONFIG_COMPAT |
| 779 | .compat_ioctl = f2fs_compat_ioctl, |
| 780 | #endif |
Jaegeuk Kim | fbfa2cc | 2012-11-02 17:09:44 +0900 | [diff] [blame] | 781 | .splice_read = generic_file_splice_read, |
| 782 | .splice_write = generic_file_splice_write, |
| 783 | }; |