blob: 7f9ea9271ebeaa4e1da9440758c5fe6c2cee62f3 [file] [log] [blame]
Jaegeuk Kim0a8165d2012-11-29 13:28:09 +09001/*
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +09002 * 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
27static 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 Kimfbfa2cc2012-11-02 17:09:44 +090033 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 Kimfbfa2cc2012-11-02 17:09:44 +090052
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);
93out:
94 sb_end_pagefault(inode->i_sb);
95 return block_page_mkwrite_return(err);
96}
97
98static const struct vm_operations_struct f2fs_file_vm_ops = {
99 .fault = filemap_fault,
100 .page_mkwrite = f2fs_vm_page_mkwrite,
101};
102
103static int need_to_sync_dir(struct f2fs_sb_info *sbi, struct inode *inode)
104{
105 struct dentry *dentry;
106 nid_t pino;
107
108 inode = igrab(inode);
109 dentry = d_find_any_alias(inode);
110 if (!dentry) {
111 iput(inode);
112 return 0;
113 }
114 pino = dentry->d_parent->d_inode->i_ino;
115 dput(dentry);
116 iput(inode);
117 return !is_checkpointed_node(sbi, pino);
118}
119
120int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
121{
122 struct inode *inode = file->f_mapping->host;
123 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
124 unsigned long long cur_version;
125 int ret = 0;
126 bool need_cp = false;
127 struct writeback_control wbc = {
128 .sync_mode = WB_SYNC_ALL,
129 .nr_to_write = LONG_MAX,
130 .for_reclaim = 0,
131 };
132
Namjae Jeon1fa95b02012-12-01 10:56:01 +0900133 if (inode->i_sb->s_flags & MS_RDONLY)
134 return 0;
135
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900136 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
137 if (ret)
138 return ret;
139
140 mutex_lock(&inode->i_mutex);
141
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900142 if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
143 goto out;
144
145 mutex_lock(&sbi->cp_mutex);
146 cur_version = le64_to_cpu(F2FS_CKPT(sbi)->checkpoint_ver);
147 mutex_unlock(&sbi->cp_mutex);
148
149 if (F2FS_I(inode)->data_version != cur_version &&
150 !(inode->i_state & I_DIRTY))
151 goto out;
152 F2FS_I(inode)->data_version--;
153
154 if (!S_ISREG(inode->i_mode) || inode->i_nlink != 1)
155 need_cp = true;
156 if (is_inode_flag_set(F2FS_I(inode), FI_NEED_CP))
157 need_cp = true;
158 if (!space_for_roll_forward(sbi))
159 need_cp = true;
160 if (need_to_sync_dir(sbi, inode))
161 need_cp = true;
162
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900163 if (need_cp) {
164 /* all the dirty node pages should be flushed for POR */
165 ret = f2fs_sync_fs(inode->i_sb, 1);
166 clear_inode_flag(F2FS_I(inode), FI_NEED_CP);
167 } else {
Jaegeuk Kim398b1ac2012-12-19 15:28:39 +0900168 /* if there is no written node page, write its inode page */
169 while (!sync_node_pages(sbi, inode->i_ino, &wbc)) {
170 ret = f2fs_write_inode(inode, NULL);
171 if (ret)
172 goto out;
173 }
Jaegeuk Kimfbfa2cc2012-11-02 17:09:44 +0900174 filemap_fdatawait_range(sbi->node_inode->i_mapping,
175 0, LONG_MAX);
176 }
177out:
178 mutex_unlock(&inode->i_mutex);
179 return ret;
180}
181
182static 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
189static 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
217void truncate_data_blocks(struct dnode_of_data *dn)
218{
219 truncate_data_blocks_range(dn, ADDRS_PER_BLOCK);
220}
221
222static 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
241static 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);
277free_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
287void 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
301static 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
311static 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
341int 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
371const 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
383static 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
401int 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
428static 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
471static 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
533static 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
555static 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
565long 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);
616out:
617 mnt_drop_write(filp->f_path.mnt);
618 return ret;
619 }
620 default:
621 return -ENOTTY;
622 }
623}
624
625const 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};