4 * Copyright (C) 1992 Rick Sladkey
7 #include <linux/falloc.h>
8 #include <linux/nfs_fs.h>
13 #ifdef CONFIG_NFS_V4_2
17 #define NFSDBG_FACILITY NFSDBG_FILE
20 nfs4_file_open(struct inode
*inode
, struct file
*filp
)
22 struct nfs_open_context
*ctx
;
23 struct dentry
*dentry
= filp
->f_path
.dentry
;
24 struct dentry
*parent
= NULL
;
26 unsigned openflags
= filp
->f_flags
;
32 * If no cached dentry exists or if it's negative, NFSv4 handled the
33 * opens in ->lookup() or ->create().
35 * We only get this far for a cached positive dentry. We skipped
36 * revalidation, so handle it here by dropping the dentry and returning
37 * -EOPENSTALE. The VFS will retry the lookup/create/open.
40 dprintk("NFS: open file(%pd2)\n", dentry
);
42 if ((openflags
& O_ACCMODE
) == 3)
45 /* We can't create new files here */
46 openflags
&= ~(O_CREAT
|O_EXCL
);
48 parent
= dget_parent(dentry
);
49 dir
= parent
->d_inode
;
51 ctx
= alloc_nfs_open_context(filp
->f_path
.dentry
, filp
->f_mode
);
56 attr
.ia_valid
= ATTR_OPEN
;
57 if (openflags
& O_TRUNC
) {
58 attr
.ia_valid
|= ATTR_SIZE
;
63 inode
= NFS_PROTO(dir
)->open_context(dir
, ctx
, openflags
, &attr
, &opened
);
77 if (inode
!= dentry
->d_inode
)
80 nfs_set_verifier(dentry
, nfs_save_change_attribute(dir
));
81 nfs_file_set_open_context(filp
, ctx
);
82 nfs_fscache_open_file(inode
, filp
);
86 put_nfs_open_context(ctx
);
98 nfs4_file_fsync(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
101 struct inode
*inode
= file_inode(file
);
104 ret
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
107 mutex_lock(&inode
->i_mutex
);
108 ret
= nfs_file_fsync_commit(file
, start
, end
, datasync
);
110 ret
= pnfs_layoutcommit_inode(inode
, true);
111 mutex_unlock(&inode
->i_mutex
);
113 * If nfs_file_fsync_commit detected a server reboot, then
114 * resend all dirty pages that might have been covered by
115 * the NFS_CONTEXT_RESEND_WRITES flag
119 } while (ret
== -EAGAIN
);
124 #ifdef CONFIG_NFS_V4_2
125 static loff_t
nfs4_file_llseek(struct file
*filep
, loff_t offset
, int whence
)
132 ret
= nfs42_proc_llseek(filep
, offset
, whence
);
133 if (ret
!= -ENOTSUPP
)
136 return nfs_file_llseek(filep
, offset
, whence
);
140 static long nfs42_fallocate(struct file
*filep
, int mode
, loff_t offset
, loff_t len
)
142 struct inode
*inode
= file_inode(filep
);
145 if (!S_ISREG(inode
->i_mode
))
148 if ((mode
!= 0) && (mode
!= (FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
)))
151 ret
= inode_newsize_ok(inode
, offset
+ len
);
155 mutex_lock(&inode
->i_mutex
);
156 if (mode
& FALLOC_FL_PUNCH_HOLE
)
157 ret
= nfs42_proc_deallocate(filep
, offset
, len
);
159 ret
= nfs42_proc_allocate(filep
, offset
, len
);
160 mutex_unlock(&inode
->i_mutex
);
162 nfs_zap_caches(inode
);
165 #endif /* CONFIG_NFS_V4_2 */
167 const struct file_operations nfs4_file_operations
= {
168 #ifdef CONFIG_NFS_V4_2
169 .llseek
= nfs4_file_llseek
,
171 .llseek
= nfs_file_llseek
,
173 .read
= new_sync_read
,
174 .write
= new_sync_write
,
175 .read_iter
= nfs_file_read
,
176 .write_iter
= nfs_file_write
,
177 .mmap
= nfs_file_mmap
,
178 .open
= nfs4_file_open
,
179 .flush
= nfs_file_flush
,
180 .release
= nfs_file_release
,
181 .fsync
= nfs4_file_fsync
,
184 .splice_read
= nfs_file_splice_read
,
185 .splice_write
= iter_file_splice_write
,
186 #ifdef CONFIG_NFS_V4_2
187 .fallocate
= nfs42_fallocate
,
188 #endif /* CONFIG_NFS_V4_2 */
189 .check_flags
= nfs_check_flags
,
190 .setlease
= simple_nosetlease
,