blob: a816f0627a6ce03cda2502c42c780c5ab6a2742c [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
11#define NFSDBG_FACILITY NFSDBG_FILE
12
13static int
14nfs4_file_open(struct inode *inode, struct file *filp)
15{
16 struct nfs_open_context *ctx;
17 struct dentry *dentry = filp->f_path.dentry;
18 struct dentry *parent = NULL;
19 struct inode *dir;
20 unsigned openflags = filp->f_flags;
21 struct iattr attr;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -040022 int opened = 0;
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040023 int err;
24
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040025 /*
26 * If no cached dentry exists or if it's negative, NFSv4 handled the
27 * opens in ->lookup() or ->create().
28 *
29 * We only get this far for a cached positive dentry. We skipped
30 * revalidation, so handle it here by dropping the dentry and returning
31 * -EOPENSTALE. The VFS will retry the lookup/create/open.
32 */
33
Al Viro6de14722013-09-16 10:53:17 -040034 dprintk("NFS: open file(%pd2)\n", dentry);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040035
36 if ((openflags & O_ACCMODE) == 3)
37 openflags--;
38
39 /* We can't create new files here */
40 openflags &= ~(O_CREAT|O_EXCL);
41
42 parent = dget_parent(dentry);
43 dir = parent->d_inode;
44
45 ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
46 err = PTR_ERR(ctx);
47 if (IS_ERR(ctx))
48 goto out;
49
50 attr.ia_valid = ATTR_OPEN;
51 if (openflags & O_TRUNC) {
52 attr.ia_valid |= ATTR_SIZE;
53 attr.ia_size = 0;
54 nfs_wb_all(inode);
55 }
56
Trond Myklebust5bc2afc2013-09-23 18:01:28 -040057 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, &opened);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040058 if (IS_ERR(inode)) {
59 err = PTR_ERR(inode);
60 switch (err) {
61 case -EPERM:
62 case -EACCES:
63 case -EDQUOT:
64 case -ENOSPC:
65 case -EROFS:
66 goto out_put_ctx;
67 default:
68 goto out_drop;
69 }
70 }
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040071 if (inode != dentry->d_inode)
72 goto out_drop;
73
74 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
75 nfs_file_set_open_context(filp, ctx);
David Howellsf1fe29b2013-09-27 11:20:03 +010076 nfs_fscache_open_file(inode, filp);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040077 err = 0;
78
79out_put_ctx:
80 put_nfs_open_context(ctx);
81out:
82 dput(parent);
83 return err;
84
85out_drop:
86 d_drop(dentry);
87 err = -EOPENSTALE;
88 goto out_put_ctx;
89}
90
91static int
92nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
93{
94 int ret;
Al Viro496ad9a2013-01-23 17:07:38 -050095 struct inode *inode = file_inode(file);
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -040096
Trond Myklebust05990d12012-09-11 16:01:22 -040097 do {
98 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
99 if (ret != 0)
100 break;
101 mutex_lock(&inode->i_mutex);
102 ret = nfs_file_fsync_commit(file, start, end, datasync);
Christoph Hellwig1b338092014-04-21 10:29:17 -0700103 if (!ret)
Trond Myklebust05990d12012-09-11 16:01:22 -0400104 ret = pnfs_layoutcommit_inode(inode, true);
105 mutex_unlock(&inode->i_mutex);
Trond Myklebustdcfc4f22012-09-11 16:19:38 -0400106 /*
107 * If nfs_file_fsync_commit detected a server reboot, then
108 * resend all dirty pages that might have been covered by
109 * the NFS_CONTEXT_RESEND_WRITES flag
110 */
111 start = 0;
112 end = LLONG_MAX;
Trond Myklebust05990d12012-09-11 16:01:22 -0400113 } while (ret == -EAGAIN);
114
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400115 return ret;
116}
117
118const struct file_operations nfs4_file_operations = {
119 .llseek = nfs_file_llseek,
Al Viro3aa2d192014-04-02 20:14:12 -0400120 .read = new_sync_read,
Al Viroedaf4362014-04-03 14:07:25 -0400121 .write = new_sync_write,
Al Viro3aa2d192014-04-02 20:14:12 -0400122 .read_iter = nfs_file_read,
Al Viroedaf4362014-04-03 14:07:25 -0400123 .write_iter = nfs_file_write,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400124 .mmap = nfs_file_mmap,
125 .open = nfs4_file_open,
126 .flush = nfs_file_flush,
127 .release = nfs_file_release,
128 .fsync = nfs4_file_fsync,
129 .lock = nfs_lock,
130 .flock = nfs_flock,
131 .splice_read = nfs_file_splice_read,
Al Viro4da54c22014-04-05 04:37:17 -0400132 .splice_write = iter_file_splice_write,
Bryan Schumakerce4ef7c2012-07-16 16:39:15 -0400133 .check_flags = nfs_check_flags,
134 .setlease = nfs_setlease,
135};