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" 8 #include "fscache.h" 9 #include "pnfs.h" 10 11 #ifdef CONFIG_NFS_V4_2 12 #include "nfs42.h" 13 #endif 14 15 #define NFSDBG_FACILITY NFSDBG_FILE 16 17 static int 18 nfs4_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; 26 int opened = 0; 27 int err; 28 29 /* 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 38 dprintk("NFS: open file(%pd2)\n", dentry); 39 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 61 inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, &opened); 62 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 } 75 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); 80 nfs_fscache_open_file(inode, filp); 81 err = 0; 82 83 out_put_ctx: 84 put_nfs_open_context(ctx); 85 out: 86 dput(parent); 87 return err; 88 89 out_drop: 90 d_drop(dentry); 91 err = -EOPENSTALE; 92 goto out_put_ctx; 93 } 94 95 static int 96 nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) 97 { 98 int ret; 99 struct inode *inode = file_inode(file); 100 101 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); 107 if (!ret) 108 ret = pnfs_layoutcommit_inode(inode, true); 109 mutex_unlock(&inode->i_mutex); 110 /* 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; 117 } while (ret == -EAGAIN); 118 119 return ret; 120 } 121 122 #ifdef CONFIG_NFS_V4_2 123 static 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 139 const struct file_operations nfs4_file_operations = { 140 #ifdef CONFIG_NFS_V4_2 141 .llseek = nfs4_file_llseek, 142 #else 143 .llseek = nfs_file_llseek, 144 #endif 145 .read = new_sync_read, 146 .write = new_sync_write, 147 .read_iter = nfs_file_read, 148 .write_iter = nfs_file_write, 149 .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, 157 .splice_write = iter_file_splice_write, 158 .check_flags = nfs_check_flags, 159 .setlease = simple_nosetlease, 160 }; 161