| Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 1 | /* | 
|  | 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 Howells | a4ff146 | 2012-12-05 16:31:49 +0000 | [diff] [blame] | 8 | #include "fscache.h" | 
| Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 9 | #include "pnfs.h" | 
|  | 10 |  | 
|  | 11 | #define NFSDBG_FACILITY		NFSDBG_FILE | 
|  | 12 |  | 
|  | 13 | static int | 
|  | 14 | nfs4_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; | 
|  | 22 | int err; | 
|  | 23 |  | 
| Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 24 | /* | 
|  | 25 | * If no cached dentry exists or if it's negative, NFSv4 handled the | 
|  | 26 | * opens in ->lookup() or ->create(). | 
|  | 27 | * | 
|  | 28 | * We only get this far for a cached positive dentry.  We skipped | 
|  | 29 | * revalidation, so handle it here by dropping the dentry and returning | 
|  | 30 | * -EOPENSTALE.  The VFS will retry the lookup/create/open. | 
|  | 31 | */ | 
|  | 32 |  | 
|  | 33 | dprintk("NFS: open file(%s/%s)\n", | 
|  | 34 | dentry->d_parent->d_name.name, | 
|  | 35 | dentry->d_name.name); | 
|  | 36 |  | 
|  | 37 | if ((openflags & O_ACCMODE) == 3) | 
|  | 38 | openflags--; | 
|  | 39 |  | 
|  | 40 | /* We can't create new files here */ | 
|  | 41 | openflags &= ~(O_CREAT|O_EXCL); | 
|  | 42 |  | 
|  | 43 | parent = dget_parent(dentry); | 
|  | 44 | dir = parent->d_inode; | 
|  | 45 |  | 
|  | 46 | ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode); | 
|  | 47 | err = PTR_ERR(ctx); | 
|  | 48 | if (IS_ERR(ctx)) | 
|  | 49 | goto out; | 
|  | 50 |  | 
|  | 51 | attr.ia_valid = ATTR_OPEN; | 
|  | 52 | if (openflags & O_TRUNC) { | 
|  | 53 | attr.ia_valid |= ATTR_SIZE; | 
|  | 54 | attr.ia_size = 0; | 
|  | 55 | nfs_wb_all(inode); | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr); | 
|  | 59 | if (IS_ERR(inode)) { | 
|  | 60 | err = PTR_ERR(inode); | 
|  | 61 | switch (err) { | 
|  | 62 | case -EPERM: | 
|  | 63 | case -EACCES: | 
|  | 64 | case -EDQUOT: | 
|  | 65 | case -ENOSPC: | 
|  | 66 | case -EROFS: | 
|  | 67 | goto out_put_ctx; | 
|  | 68 | default: | 
|  | 69 | goto out_drop; | 
|  | 70 | } | 
|  | 71 | } | 
|  | 72 | iput(inode); | 
|  | 73 | if (inode != dentry->d_inode) | 
|  | 74 | goto out_drop; | 
|  | 75 |  | 
|  | 76 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); | 
|  | 77 | nfs_file_set_open_context(filp, ctx); | 
| David Howells | a4ff146 | 2012-12-05 16:31:49 +0000 | [diff] [blame] | 78 | nfs_fscache_set_inode_cookie(inode, filp); | 
| Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 79 | err = 0; | 
|  | 80 |  | 
|  | 81 | out_put_ctx: | 
|  | 82 | put_nfs_open_context(ctx); | 
|  | 83 | out: | 
|  | 84 | dput(parent); | 
|  | 85 | return err; | 
|  | 86 |  | 
|  | 87 | out_drop: | 
|  | 88 | d_drop(dentry); | 
|  | 89 | err = -EOPENSTALE; | 
|  | 90 | goto out_put_ctx; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | static int | 
|  | 94 | nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) | 
|  | 95 | { | 
|  | 96 | int ret; | 
| Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 97 | struct inode *inode = file_inode(file); | 
| Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 98 |  | 
| Trond Myklebust | 05990d1 | 2012-09-11 16:01:22 -0400 | [diff] [blame] | 99 | do { | 
|  | 100 | ret = filemap_write_and_wait_range(inode->i_mapping, start, end); | 
|  | 101 | if (ret != 0) | 
|  | 102 | break; | 
|  | 103 | mutex_lock(&inode->i_mutex); | 
|  | 104 | ret = nfs_file_fsync_commit(file, start, end, datasync); | 
|  | 105 | if (!ret && !datasync) | 
|  | 106 | /* application has asked for meta-data sync */ | 
|  | 107 | ret = pnfs_layoutcommit_inode(inode, true); | 
|  | 108 | mutex_unlock(&inode->i_mutex); | 
| Trond Myklebust | dcfc4f2 | 2012-09-11 16:19:38 -0400 | [diff] [blame] | 109 | /* | 
|  | 110 | * If nfs_file_fsync_commit detected a server reboot, then | 
|  | 111 | * resend all dirty pages that might have been covered by | 
|  | 112 | * the NFS_CONTEXT_RESEND_WRITES flag | 
|  | 113 | */ | 
|  | 114 | start = 0; | 
|  | 115 | end = LLONG_MAX; | 
| Trond Myklebust | 05990d1 | 2012-09-11 16:01:22 -0400 | [diff] [blame] | 116 | } while (ret == -EAGAIN); | 
|  | 117 |  | 
| Bryan Schumaker | ce4ef7c | 2012-07-16 16:39:15 -0400 | [diff] [blame] | 118 | return ret; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | const struct file_operations nfs4_file_operations = { | 
|  | 122 | .llseek		= nfs_file_llseek, | 
|  | 123 | .read		= do_sync_read, | 
|  | 124 | .write		= do_sync_write, | 
|  | 125 | .aio_read	= nfs_file_read, | 
|  | 126 | .aio_write	= nfs_file_write, | 
|  | 127 | .mmap		= nfs_file_mmap, | 
|  | 128 | .open		= nfs4_file_open, | 
|  | 129 | .flush		= nfs_file_flush, | 
|  | 130 | .release	= nfs_file_release, | 
|  | 131 | .fsync		= nfs4_file_fsync, | 
|  | 132 | .lock		= nfs_lock, | 
|  | 133 | .flock		= nfs_flock, | 
|  | 134 | .splice_read	= nfs_file_splice_read, | 
|  | 135 | .splice_write	= nfs_file_splice_write, | 
|  | 136 | .check_flags	= nfs_check_flags, | 
|  | 137 | .setlease	= nfs_setlease, | 
|  | 138 | }; |