Commit | Line | Data |
---|---|---|
ce4ef7c0 BS |
1 | /* |
2 | * linux/fs/nfs/file.c | |
3 | * | |
4 | * Copyright (C) 1992 Rick Sladkey | |
5 | */ | |
f4ac1674 AS |
6 | #include <linux/fs.h> |
7 | #include <linux/falloc.h> | |
ce4ef7c0 BS |
8 | #include <linux/nfs_fs.h> |
9 | #include "internal.h" | |
a4ff1468 | 10 | #include "fscache.h" |
ce4ef7c0 BS |
11 | #include "pnfs.h" |
12 | ||
81b79afb TM |
13 | #include "nfstrace.h" |
14 | ||
1c6dcbe5 AS |
15 | #ifdef CONFIG_NFS_V4_2 |
16 | #include "nfs42.h" | |
17 | #endif | |
18 | ||
ce4ef7c0 BS |
19 | #define NFSDBG_FACILITY NFSDBG_FILE |
20 | ||
21 | static int | |
22 | nfs4_file_open(struct inode *inode, struct file *filp) | |
23 | { | |
24 | struct nfs_open_context *ctx; | |
25 | struct dentry *dentry = filp->f_path.dentry; | |
26 | struct dentry *parent = NULL; | |
27 | struct inode *dir; | |
28 | unsigned openflags = filp->f_flags; | |
29 | struct iattr attr; | |
30 | int err; | |
31 | ||
ce4ef7c0 BS |
32 | /* |
33 | * If no cached dentry exists or if it's negative, NFSv4 handled the | |
34 | * opens in ->lookup() or ->create(). | |
35 | * | |
36 | * We only get this far for a cached positive dentry. We skipped | |
37 | * revalidation, so handle it here by dropping the dentry and returning | |
38 | * -EOPENSTALE. The VFS will retry the lookup/create/open. | |
39 | */ | |
40 | ||
6de1472f | 41 | dprintk("NFS: open file(%pd2)\n", dentry); |
ce4ef7c0 | 42 | |
18a60089 BC |
43 | err = nfs_check_flags(openflags); |
44 | if (err) | |
45 | return err; | |
46 | ||
ce4ef7c0 BS |
47 | if ((openflags & O_ACCMODE) == 3) |
48 | openflags--; | |
49 | ||
50 | /* We can't create new files here */ | |
51 | openflags &= ~(O_CREAT|O_EXCL); | |
52 | ||
53 | parent = dget_parent(dentry); | |
2b0143b5 | 54 | dir = d_inode(parent); |
ce4ef7c0 BS |
55 | |
56 | ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode); | |
57 | err = PTR_ERR(ctx); | |
58 | if (IS_ERR(ctx)) | |
59 | goto out; | |
60 | ||
61 | attr.ia_valid = ATTR_OPEN; | |
62 | if (openflags & O_TRUNC) { | |
63 | attr.ia_valid |= ATTR_SIZE; | |
64 | attr.ia_size = 0; | |
9e1681c2 | 65 | nfs_sync_inode(inode); |
ce4ef7c0 BS |
66 | } |
67 | ||
c5c3fb5f | 68 | inode = NFS_PROTO(dir)->open_context(dir, ctx, openflags, &attr, NULL); |
ce4ef7c0 BS |
69 | if (IS_ERR(inode)) { |
70 | err = PTR_ERR(inode); | |
71 | switch (err) { | |
72 | case -EPERM: | |
73 | case -EACCES: | |
74 | case -EDQUOT: | |
75 | case -ENOSPC: | |
76 | case -EROFS: | |
77 | goto out_put_ctx; | |
78 | default: | |
79 | goto out_drop; | |
80 | } | |
81 | } | |
2b0143b5 | 82 | if (inode != d_inode(dentry)) |
ce4ef7c0 BS |
83 | goto out_drop; |
84 | ||
85 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); | |
86 | nfs_file_set_open_context(filp, ctx); | |
f1fe29b4 | 87 | nfs_fscache_open_file(inode, filp); |
ce4ef7c0 BS |
88 | err = 0; |
89 | ||
90 | out_put_ctx: | |
91 | put_nfs_open_context(ctx); | |
92 | out: | |
93 | dput(parent); | |
94 | return err; | |
95 | ||
96 | out_drop: | |
97 | d_drop(dentry); | |
98 | err = -EOPENSTALE; | |
99 | goto out_put_ctx; | |
100 | } | |
101 | ||
102 | static int | |
103 | nfs4_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) | |
104 | { | |
105 | int ret; | |
496ad9aa | 106 | struct inode *inode = file_inode(file); |
ce4ef7c0 | 107 | |
81b79afb TM |
108 | trace_nfs_fsync_enter(inode); |
109 | ||
a0815d55 | 110 | nfs_inode_dio_wait(inode); |
05990d1b TM |
111 | do { |
112 | ret = filemap_write_and_wait_range(inode->i_mapping, start, end); | |
113 | if (ret != 0) | |
114 | break; | |
115 | mutex_lock(&inode->i_mutex); | |
116 | ret = nfs_file_fsync_commit(file, start, end, datasync); | |
1b33809e | 117 | if (!ret) |
5bb89b47 | 118 | ret = pnfs_sync_inode(inode, !!datasync); |
05990d1b | 119 | mutex_unlock(&inode->i_mutex); |
dcfc4f25 TM |
120 | /* |
121 | * If nfs_file_fsync_commit detected a server reboot, then | |
122 | * resend all dirty pages that might have been covered by | |
123 | * the NFS_CONTEXT_RESEND_WRITES flag | |
124 | */ | |
125 | start = 0; | |
126 | end = LLONG_MAX; | |
05990d1b TM |
127 | } while (ret == -EAGAIN); |
128 | ||
81b79afb | 129 | trace_nfs_fsync_exit(inode, ret); |
ce4ef7c0 BS |
130 | return ret; |
131 | } | |
132 | ||
1c6dcbe5 AS |
133 | #ifdef CONFIG_NFS_V4_2 |
134 | static loff_t nfs4_file_llseek(struct file *filep, loff_t offset, int whence) | |
135 | { | |
136 | loff_t ret; | |
137 | ||
138 | switch (whence) { | |
139 | case SEEK_HOLE: | |
140 | case SEEK_DATA: | |
141 | ret = nfs42_proc_llseek(filep, offset, whence); | |
142 | if (ret != -ENOTSUPP) | |
143 | return ret; | |
144 | default: | |
145 | return nfs_file_llseek(filep, offset, whence); | |
146 | } | |
147 | } | |
f4ac1674 AS |
148 | |
149 | static long nfs42_fallocate(struct file *filep, int mode, loff_t offset, loff_t len) | |
150 | { | |
151 | struct inode *inode = file_inode(filep); | |
152 | long ret; | |
153 | ||
154 | if (!S_ISREG(inode->i_mode)) | |
155 | return -EOPNOTSUPP; | |
156 | ||
624bd5b7 | 157 | if ((mode != 0) && (mode != (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE))) |
f4ac1674 AS |
158 | return -EOPNOTSUPP; |
159 | ||
160 | ret = inode_newsize_ok(inode, offset + len); | |
161 | if (ret < 0) | |
162 | return ret; | |
163 | ||
624bd5b7 | 164 | if (mode & FALLOC_FL_PUNCH_HOLE) |
f830f7dd AS |
165 | return nfs42_proc_deallocate(filep, offset, len); |
166 | return nfs42_proc_allocate(filep, offset, len); | |
f4ac1674 | 167 | } |
1c6dcbe5 AS |
168 | #endif /* CONFIG_NFS_V4_2 */ |
169 | ||
ce4ef7c0 | 170 | const struct file_operations nfs4_file_operations = { |
1c6dcbe5 AS |
171 | #ifdef CONFIG_NFS_V4_2 |
172 | .llseek = nfs4_file_llseek, | |
173 | #else | |
ce4ef7c0 | 174 | .llseek = nfs_file_llseek, |
1c6dcbe5 | 175 | #endif |
3aa2d199 | 176 | .read_iter = nfs_file_read, |
edaf4369 | 177 | .write_iter = nfs_file_write, |
ce4ef7c0 BS |
178 | .mmap = nfs_file_mmap, |
179 | .open = nfs4_file_open, | |
180 | .flush = nfs_file_flush, | |
181 | .release = nfs_file_release, | |
182 | .fsync = nfs4_file_fsync, | |
183 | .lock = nfs_lock, | |
184 | .flock = nfs_flock, | |
185 | .splice_read = nfs_file_splice_read, | |
4da54c21 | 186 | .splice_write = iter_file_splice_write, |
f4ac1674 AS |
187 | #ifdef CONFIG_NFS_V4_2 |
188 | .fallocate = nfs42_fallocate, | |
189 | #endif /* CONFIG_NFS_V4_2 */ | |
ce4ef7c0 | 190 | .check_flags = nfs_check_flags, |
1c994a09 | 191 | .setlease = simple_nosetlease, |
ce4ef7c0 | 192 | }; |