blob: 4dffa3a6473109cf8b9afd5608ffb142d9e5974c [file] [log] [blame]
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -04001/*
2 * linux/fs/nfs/file.c
3 *
4 * Copyright (C) 1992 Rick Sladkey
5 */
6#include <linux/nfs_fs.h>
7#include "internal.h"
David Howellsa4ff1462012-12-05 16:31:49 +00008#include "fscache.h"
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -04009#include "pnfs.h"
10
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -040011#ifdef CONFIG_NFS_V4_2
12#include "nfs42.h"
13#endif
14
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040015#define NFSDBG_FACILITY NFSDBG_FILE
16
17static int
18nfs4_file_open(struct inode *inode, struct file *filp)
19{
20 struct nfs_open_context *ctx;
21 struct dentry *dentry = filp->f_path.dentry;
22 struct dentry *parent = NULL;
23 struct inode *dir;
24 unsigned openflags = filp->f_flags;
25 struct iattr attr;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -040026 int opened = 0;
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040027 int err;
28
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040029 /*
30 * If no cached dentry exists or if it's negative, NFSv4 handled the
31 * opens in ->lookup() or ->create().
32 *
33 * We only get this far for a cached positive dentry. We skipped
34 * revalidation, so handle it here by dropping the dentry and returning
35 * -EOPENSTALE. The VFS will retry the lookup/create/open.
36 */
37
Al Viro6de14722013-09-16 10:53:17 -040038 dprintk("NFS: open file(%pd2)\n", dentry);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040039
40 if ((openflags & O_ACCMODE) == 3)
41 openflags--;
42
43 /* We can't create new files here */
44 openflags &= ~(O_CREAT|O_EXCL);
45
46 parent = dget_parent(dentry);
47 dir = parent->d_inode;
48
49 ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
50 err = PTR_ERR(ctx);
51 if (IS_ERR(ctx))
52 goto out;
53
54 attr.ia_valid = ATTR_OPEN;
55 if (openflags & O_TRUNC) {
56 attr.ia_valid |= ATTR_SIZE;
57 attr.ia_size = 0;
58 nfs_wb_all(inode);
59 }
60
Trond Myklebust5bc2afc2013-09-23 18:01:28 -040061 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, &opened);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040062 if (IS_ERR(inode)) {
63 err = PTR_ERR(inode);
64 switch (err) {
65 case -EPERM:
66 case -EACCES:
67 case -EDQUOT:
68 case -ENOSPC:
69 case -EROFS:
70 goto out_put_ctx;
71 default:
72 goto out_drop;
73 }
74 }
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040075 if (inode != dentry->d_inode)
76 goto out_drop;
77
78 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
79 nfs_file_set_open_context(filp, ctx);
David Howellsf1fe29b2013-09-27 11:20:03 +010080 nfs_fscache_open_file(inode, filp);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040081 err = 0;
82
83out_put_ctx:
84 put_nfs_open_context(ctx);
85out:
86 dput(parent);
87 return err;
88
89out_drop:
90 d_drop(dentry);
91 err = -EOPENSTALE;
92 goto out_put_ctx;
93}
94
95static int
96nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
97{
98 int ret;
Al Viro496ad9a2013-01-23 17:07:38 -050099 struct inode *inode = file_inode(file);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400100
Trond Myklebust05990d12012-09-11 16:01:22 -0400101 do {
102 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
103 if (ret != 0)
104 break;
105 mutex_lock(&inode->i_mutex);
106 ret = nfs_file_fsync_commit(file, start, end, datasync);
Christoph Hellwig1b338092014-04-21 10:29:17 -0700107 if (!ret)
Trond Myklebust05990d12012-09-11 16:01:22 -0400108 ret = pnfs_layoutcommit_inode(inode, true);
109 mutex_unlock(&inode->i_mutex);
Trond Myklebustdcfc4f22012-09-11 16:19:38 -0400110 /*
111 * If nfs_file_fsync_commit detected a server reboot, then
112 * resend all dirty pages that might have been covered by
113 * the NFS_CONTEXT_RESEND_WRITES flag
114 */
115 start = 0;
116 end = LLONG_MAX;
Trond Myklebust05990d12012-09-11 16:01:22 -0400117 } while (ret == -EAGAIN);
118
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400119 return ret;
120}
121
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400122#ifdef CONFIG_NFS_V4_2
123static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence)
124{
125 loff_t ret;
126
127 switch (whence) {
128 case SEEK_HOLE:
129 case SEEK_DATA:
130 ret = nfs42_proc_llseek(filep, offset, whence);
131 if (ret != -ENOTSUPP)
132 return ret;
133 default:
134 return nfs_file_llseek(filep, offset, whence);
135 }
136}
137#endif /* CONFIG_NFS_V4_2 */
138
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400139const struct file_operations nfs4_file_operations = {
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400140#ifdef CONFIG_NFS_V4_2
141 .llseek = nfs4_file_llseek,
142#else
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400143 .llseek = nfs_file_llseek,
Anna Schumaker1c6dcbe2014-09-26 13:58:48 -0400144#endif
Al Viro3aa2d192014-04-02 20:14:12 -0400145 .read = new_sync_read,
Al Viroedaf4362014-04-03 14:07:25 -0400146 .write = new_sync_write,
Al Viro3aa2d192014-04-02 20:14:12 -0400147 .read_iter = nfs_file_read,
Al Viroedaf4362014-04-03 14:07:25 -0400148 .write_iter = nfs_file_write,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400149 .mmap = nfs_file_mmap,
150 .open = nfs4_file_open,
151 .flush = nfs_file_flush,
152 .release = nfs_file_release,
153 .fsync = nfs4_file_fsync,
154 .lock = nfs_lock,
155 .flock = nfs_flock,
156 .splice_read = nfs_file_splice_read,
Al Viro4da54c22014-04-05 04:37:17 -0400157 .splice_write = iter_file_splice_write,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400158 .check_flags = nfs_check_flags,
159 .setlease = nfs_setlease,
160};