Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/nfs/nfs4proc.c | |
3 | * | |
4 | * Client-side procedure declarations for NFSv4. | |
5 | * | |
6 | * Copyright (c) 2002 The Regents of the University of Michigan. | |
7 | * All rights reserved. | |
8 | * | |
9 | * Kendrick Smith <kmsmith@umich.edu> | |
10 | * Andy Adamson <andros@umich.edu> | |
11 | * | |
12 | * Redistribution and use in source and binary forms, with or without | |
13 | * modification, are permitted provided that the following conditions | |
14 | * are met: | |
15 | * | |
16 | * 1. Redistributions of source code must retain the above copyright | |
17 | * notice, this list of conditions and the following disclaimer. | |
18 | * 2. Redistributions in binary form must reproduce the above copyright | |
19 | * notice, this list of conditions and the following disclaimer in the | |
20 | * documentation and/or other materials provided with the distribution. | |
21 | * 3. Neither the name of the University nor the names of its | |
22 | * contributors may be used to endorse or promote products derived | |
23 | * from this software without specific prior written permission. | |
24 | * | |
25 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
26 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
27 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
28 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR | |
32 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
34 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
35 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
36 | */ | |
37 | ||
38 | #include <linux/mm.h> | |
1da177e4 LT |
39 | #include <linux/delay.h> |
40 | #include <linux/errno.h> | |
41 | #include <linux/string.h> | |
652f89f6 TM |
42 | #include <linux/ratelimit.h> |
43 | #include <linux/printk.h> | |
5a0e3ad6 | 44 | #include <linux/slab.h> |
1da177e4 LT |
45 | #include <linux/sunrpc/clnt.h> |
46 | #include <linux/nfs.h> | |
47 | #include <linux/nfs4.h> | |
48 | #include <linux/nfs_fs.h> | |
49 | #include <linux/nfs_page.h> | |
9b7160c5 | 50 | #include <linux/nfs_mount.h> |
1da177e4 | 51 | #include <linux/namei.h> |
02a913a7 | 52 | #include <linux/mount.h> |
99fe60d0 | 53 | #include <linux/module.h> |
64c2ce8b | 54 | #include <linux/xattr.h> |
c7a360b0 | 55 | #include <linux/utsname.h> |
d310310c | 56 | #include <linux/freezer.h> |
1eb5d98f | 57 | #include <linux/iversion.h> |
1da177e4 | 58 | |
4ce79717 | 59 | #include "nfs4_fs.h" |
1da177e4 | 60 | #include "delegation.h" |
101070ca | 61 | #include "internal.h" |
006ea73e | 62 | #include "iostat.h" |
fc931582 | 63 | #include "callback.h" |
b1f69b75 | 64 | #include "pnfs.h" |
f092075d | 65 | #include "netns.h" |
39d43d16 | 66 | #include "sysfs.h" |
40c64c26 | 67 | #include "nfs4idmap.h" |
73e39aaa | 68 | #include "nfs4session.h" |
de242c0b | 69 | #include "fscache.h" |
012a211a | 70 | #include "nfs42.h" |
1da177e4 | 71 | |
c6d01c6f TM |
72 | #include "nfs4trace.h" |
73 | ||
1da177e4 LT |
74 | #define NFSDBG_FACILITY NFSDBG_PROC |
75 | ||
30846df0 TM |
76 | #define NFS4_BITMASK_SZ 3 |
77 | ||
2066fe89 | 78 | #define NFS4_POLL_RETRY_MIN (HZ/10) |
1da177e4 LT |
79 | #define NFS4_POLL_RETRY_MAX (15*HZ) |
80 | ||
a1d1c4f1 TM |
81 | /* file attributes which can be mapped to nfs attributes */ |
82 | #define NFS4_VALID_ATTRS (ATTR_MODE \ | |
83 | | ATTR_UID \ | |
84 | | ATTR_GID \ | |
85 | | ATTR_SIZE \ | |
86 | | ATTR_ATIME \ | |
87 | | ATTR_MTIME \ | |
88 | | ATTR_CTIME \ | |
89 | | ATTR_ATIME_SET \ | |
90 | | ATTR_MTIME_SET) | |
91 | ||
cdd4e68b | 92 | struct nfs4_opendata; |
b257957e | 93 | static int _nfs4_recover_proc_open(struct nfs4_opendata *data); |
1da177e4 | 94 | static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *); |
81934ddb | 95 | static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr); |
2ef61e0e AS |
96 | static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, |
97 | struct nfs_fattr *fattr, struct inode *inode); | |
a52458b4 | 98 | static int nfs4_do_setattr(struct inode *inode, const struct cred *cred, |
0ab64e0e | 99 | struct nfs_fattr *fattr, struct iattr *sattr, |
1b00ad65 | 100 | struct nfs_open_context *ctx, struct nfs4_label *ilabel); |
f062eb6c | 101 | #ifdef CONFIG_NFS_V4_1 |
3be0f80b | 102 | static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, |
a52458b4 | 103 | const struct cred *cred, |
3be0f80b TM |
104 | struct nfs4_slot *slot, |
105 | bool is_privileged); | |
ab7cb0df | 106 | static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *, |
a52458b4 | 107 | const struct cred *); |
f0b0bf88 | 108 | static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *, |
a52458b4 | 109 | const struct cred *, bool); |
f062eb6c | 110 | #endif |
aa9c2669 DQ |
111 | |
112 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL | |
113 | static inline struct nfs4_label * | |
114 | nfs4_label_init_security(struct inode *dir, struct dentry *dentry, | |
115 | struct iattr *sattr, struct nfs4_label *label) | |
116 | { | |
117 | int err; | |
118 | ||
119 | if (label == NULL) | |
120 | return NULL; | |
121 | ||
122 | if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0) | |
123 | return NULL; | |
124 | ||
c528f70f TM |
125 | label->lfs = 0; |
126 | label->pi = 0; | |
127 | label->len = 0; | |
128 | label->label = NULL; | |
129 | ||
aa9c2669 | 130 | err = security_dentry_init_security(dentry, sattr->ia_mode, |
15bf3239 VG |
131 | &dentry->d_name, NULL, |
132 | (void **)&label->label, &label->len); | |
aa9c2669 DQ |
133 | if (err == 0) |
134 | return label; | |
135 | ||
136 | return NULL; | |
137 | } | |
138 | static inline void | |
139 | nfs4_label_release_security(struct nfs4_label *label) | |
140 | { | |
141 | if (label) | |
142 | security_release_secctx(label->label, label->len); | |
143 | } | |
144 | static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label) | |
145 | { | |
146 | if (label) | |
147 | return server->attr_bitmask; | |
148 | ||
149 | return server->attr_bitmask_nl; | |
150 | } | |
151 | #else | |
152 | static inline struct nfs4_label * | |
153 | nfs4_label_init_security(struct inode *dir, struct dentry *dentry, | |
154 | struct iattr *sattr, struct nfs4_label *l) | |
155 | { return NULL; } | |
156 | static inline void | |
157 | nfs4_label_release_security(struct nfs4_label *label) | |
158 | { return; } | |
159 | static inline u32 * | |
160 | nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label) | |
161 | { return server->attr_bitmask; } | |
162 | #endif | |
163 | ||
1da177e4 | 164 | /* Prevent leaks of NFSv4 errors into userland */ |
46f72f57 | 165 | static int nfs4_map_errors(int err) |
1da177e4 | 166 | { |
52567b03 TM |
167 | if (err >= -1000) |
168 | return err; | |
169 | switch (err) { | |
170 | case -NFS4ERR_RESOURCE: | |
30005121 WAA |
171 | case -NFS4ERR_LAYOUTTRYLATER: |
172 | case -NFS4ERR_RECALLCONFLICT: | |
037e56a2 | 173 | case -NFS4ERR_RETURNCONFLICT: |
52567b03 | 174 | return -EREMOTEIO; |
7ebb9315 | 175 | case -NFS4ERR_WRONGSEC: |
8897538e | 176 | case -NFS4ERR_WRONG_CRED: |
7ebb9315 | 177 | return -EPERM; |
3ddeb7c5 TM |
178 | case -NFS4ERR_BADOWNER: |
179 | case -NFS4ERR_BADNAME: | |
180 | return -EINVAL; | |
fb13bfa7 TM |
181 | case -NFS4ERR_SHARE_DENIED: |
182 | return -EACCES; | |
f25efd85 SD |
183 | case -NFS4ERR_MINOR_VERS_MISMATCH: |
184 | return -EPROTONOSUPPORT; | |
6e3cf241 TM |
185 | case -NFS4ERR_FILE_OPEN: |
186 | return -EBUSY; | |
9fff59ed TM |
187 | case -NFS4ERR_NOT_SAME: |
188 | return -ENOTSYNC; | |
52567b03 | 189 | default: |
1da177e4 | 190 | dprintk("%s could not handle NFSv4 error %d\n", |
3110ff80 | 191 | __func__, -err); |
52567b03 | 192 | break; |
1da177e4 | 193 | } |
52567b03 | 194 | return -EIO; |
1da177e4 LT |
195 | } |
196 | ||
197 | /* | |
198 | * This is our standard bitmap for GETATTR requests. | |
199 | */ | |
1549210f | 200 | const u32 nfs4_fattr_bitmap[3] = { |
1da177e4 LT |
201 | FATTR4_WORD0_TYPE |
202 | | FATTR4_WORD0_CHANGE | |
203 | | FATTR4_WORD0_SIZE | |
204 | | FATTR4_WORD0_FSID | |
205 | | FATTR4_WORD0_FILEID, | |
206 | FATTR4_WORD1_MODE | |
207 | | FATTR4_WORD1_NUMLINKS | |
208 | | FATTR4_WORD1_OWNER | |
209 | | FATTR4_WORD1_OWNER_GROUP | |
210 | | FATTR4_WORD1_RAWDEV | |
211 | | FATTR4_WORD1_SPACE_USED | |
212 | | FATTR4_WORD1_TIME_ACCESS | |
213 | | FATTR4_WORD1_TIME_METADATA | |
ea96d1ec AS |
214 | | FATTR4_WORD1_TIME_MODIFY |
215 | | FATTR4_WORD1_MOUNTED_ON_FILEID, | |
aa9c2669 DQ |
216 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
217 | FATTR4_WORD2_SECURITY_LABEL | |
218 | #endif | |
1da177e4 LT |
219 | }; |
220 | ||
1549210f TM |
221 | static const u32 nfs4_pnfs_open_bitmap[3] = { |
222 | FATTR4_WORD0_TYPE | |
223 | | FATTR4_WORD0_CHANGE | |
224 | | FATTR4_WORD0_SIZE | |
225 | | FATTR4_WORD0_FSID | |
226 | | FATTR4_WORD0_FILEID, | |
227 | FATTR4_WORD1_MODE | |
228 | | FATTR4_WORD1_NUMLINKS | |
229 | | FATTR4_WORD1_OWNER | |
230 | | FATTR4_WORD1_OWNER_GROUP | |
231 | | FATTR4_WORD1_RAWDEV | |
232 | | FATTR4_WORD1_SPACE_USED | |
233 | | FATTR4_WORD1_TIME_ACCESS | |
234 | | FATTR4_WORD1_TIME_METADATA | |
235 | | FATTR4_WORD1_TIME_MODIFY, | |
236 | FATTR4_WORD2_MDSTHRESHOLD | |
95864c91 TM |
237 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
238 | | FATTR4_WORD2_SECURITY_LABEL | |
239 | #endif | |
1549210f TM |
240 | }; |
241 | ||
e23008ec AA |
242 | static const u32 nfs4_open_noattr_bitmap[3] = { |
243 | FATTR4_WORD0_TYPE | |
e23008ec AA |
244 | | FATTR4_WORD0_FILEID, |
245 | }; | |
246 | ||
a09df2ca | 247 | const u32 nfs4_statfs_bitmap[3] = { |
1da177e4 LT |
248 | FATTR4_WORD0_FILES_AVAIL |
249 | | FATTR4_WORD0_FILES_FREE | |
250 | | FATTR4_WORD0_FILES_TOTAL, | |
251 | FATTR4_WORD1_SPACE_AVAIL | |
252 | | FATTR4_WORD1_SPACE_FREE | |
253 | | FATTR4_WORD1_SPACE_TOTAL | |
254 | }; | |
255 | ||
a09df2ca | 256 | const u32 nfs4_pathconf_bitmap[3] = { |
1da177e4 LT |
257 | FATTR4_WORD0_MAXLINK |
258 | | FATTR4_WORD0_MAXNAME, | |
259 | 0 | |
260 | }; | |
261 | ||
dae100c2 | 262 | const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE |
1da177e4 LT |
263 | | FATTR4_WORD0_MAXREAD |
264 | | FATTR4_WORD0_MAXWRITE | |
265 | | FATTR4_WORD0_LEASE_TIME, | |
55b6e774 | 266 | FATTR4_WORD1_TIME_DELTA |
dae100c2 FI |
267 | | FATTR4_WORD1_FS_LAYOUT_TYPES, |
268 | FATTR4_WORD2_LAYOUT_BLKSIZE | |
2a92ee92 | 269 | | FATTR4_WORD2_CLONE_BLKSIZE |
7f08a335 | 270 | | FATTR4_WORD2_CHANGE_ATTR_TYPE |
b78ef845 | 271 | | FATTR4_WORD2_XATTR_SUPPORT |
1da177e4 LT |
272 | }; |
273 | ||
a09df2ca | 274 | const u32 nfs4_fs_locations_bitmap[3] = { |
c05cefcc | 275 | FATTR4_WORD0_CHANGE |
830b8e33 MN |
276 | | FATTR4_WORD0_SIZE |
277 | | FATTR4_WORD0_FSID | |
278 | | FATTR4_WORD0_FILEID | |
279 | | FATTR4_WORD0_FS_LOCATIONS, | |
c05cefcc | 280 | FATTR4_WORD1_OWNER |
830b8e33 MN |
281 | | FATTR4_WORD1_OWNER_GROUP |
282 | | FATTR4_WORD1_RAWDEV | |
283 | | FATTR4_WORD1_SPACE_USED | |
284 | | FATTR4_WORD1_TIME_ACCESS | |
285 | | FATTR4_WORD1_TIME_METADATA | |
286 | | FATTR4_WORD1_TIME_MODIFY | |
a09df2ca | 287 | | FATTR4_WORD1_MOUNTED_ON_FILEID, |
830b8e33 MN |
288 | }; |
289 | ||
30846df0 | 290 | static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src, |
a71029b8 | 291 | struct inode *inode, unsigned long flags) |
30846df0 TM |
292 | { |
293 | unsigned long cache_validity; | |
294 | ||
295 | memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst)); | |
296 | if (!inode || !nfs4_have_delegation(inode, FMODE_READ)) | |
297 | return; | |
298 | ||
a71029b8 | 299 | cache_validity = READ_ONCE(NFS_I(inode)->cache_validity) | flags; |
30846df0 | 300 | |
a71029b8 TM |
301 | /* Remove the attributes over which we have full control */ |
302 | dst[1] &= ~FATTR4_WORD1_RAWDEV; | |
30846df0 TM |
303 | if (!(cache_validity & NFS_INO_INVALID_SIZE)) |
304 | dst[0] &= ~FATTR4_WORD0_SIZE; | |
305 | ||
306 | if (!(cache_validity & NFS_INO_INVALID_CHANGE)) | |
307 | dst[0] &= ~FATTR4_WORD0_CHANGE; | |
30846df0 | 308 | |
720869eb TM |
309 | if (!(cache_validity & NFS_INO_INVALID_MODE)) |
310 | dst[1] &= ~FATTR4_WORD1_MODE; | |
a71029b8 | 311 | if (!(cache_validity & NFS_INO_INVALID_OTHER)) |
720869eb | 312 | dst[1] &= ~(FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP); |
30846df0 TM |
313 | } |
314 | ||
bc4785cd | 315 | static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry, |
1da177e4 LT |
316 | struct nfs4_readdir_arg *readdir) |
317 | { | |
18fe6a23 | 318 | unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE; |
0dbb4c67 | 319 | __be32 *start, *p; |
1da177e4 | 320 | |
1da177e4 | 321 | if (cookie > 2) { |
b7ef1956 | 322 | readdir->cookie = cookie; |
1da177e4 LT |
323 | memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier)); |
324 | return; | |
325 | } | |
326 | ||
327 | readdir->cookie = 0; | |
328 | memset(&readdir->verifier, 0, sizeof(readdir->verifier)); | |
329 | if (cookie == 2) | |
330 | return; | |
331 | ||
332 | /* | |
333 | * NFSv4 servers do not return entries for '.' and '..' | |
334 | * Therefore, we fake these entries here. We let '.' | |
335 | * have cookie 0 and '..' have cookie 1. Note that | |
336 | * when talking to the server, we always send cookie 0 | |
337 | * instead of 1 or 2. | |
338 | */ | |
2b86ce2d | 339 | start = p = kmap_atomic(*readdir->pages); |
1da177e4 LT |
340 | |
341 | if (cookie == 0) { | |
342 | *p++ = xdr_one; /* next */ | |
343 | *p++ = xdr_zero; /* cookie, first word */ | |
344 | *p++ = xdr_one; /* cookie, second word */ | |
345 | *p++ = xdr_one; /* entry len */ | |
346 | memcpy(p, ".\0\0\0", 4); /* entry */ | |
347 | p++; | |
348 | *p++ = xdr_one; /* bitmap length */ | |
18fe6a23 AS |
349 | *p++ = htonl(attrs); /* bitmap */ |
350 | *p++ = htonl(12); /* attribute buffer length */ | |
351 | *p++ = htonl(NF4DIR); | |
2b0143b5 | 352 | p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry))); |
1da177e4 LT |
353 | } |
354 | ||
355 | *p++ = xdr_one; /* next */ | |
356 | *p++ = xdr_zero; /* cookie, first word */ | |
357 | *p++ = xdr_two; /* cookie, second word */ | |
358 | *p++ = xdr_two; /* entry len */ | |
359 | memcpy(p, "..\0\0", 4); /* entry */ | |
360 | p++; | |
361 | *p++ = xdr_one; /* bitmap length */ | |
18fe6a23 AS |
362 | *p++ = htonl(attrs); /* bitmap */ |
363 | *p++ = htonl(12); /* attribute buffer length */ | |
364 | *p++ = htonl(NF4DIR); | |
2b0143b5 | 365 | p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent))); |
1da177e4 LT |
366 | |
367 | readdir->pgbase = (char *)p - (char *)start; | |
368 | readdir->count -= readdir->pgbase; | |
2b86ce2d | 369 | kunmap_atomic(start); |
1da177e4 LT |
370 | } |
371 | ||
00c94ebe TM |
372 | static void nfs4_fattr_set_prechange(struct nfs_fattr *fattr, u64 version) |
373 | { | |
374 | if (!(fattr->valid & NFS_ATTR_FATTR_PRECHANGE)) { | |
375 | fattr->pre_change_attr = version; | |
376 | fattr->valid |= NFS_ATTR_FATTR_PRECHANGE; | |
377 | } | |
378 | } | |
379 | ||
26d36301 TM |
380 | static void nfs4_test_and_free_stateid(struct nfs_server *server, |
381 | nfs4_stateid *stateid, | |
a52458b4 | 382 | const struct cred *cred) |
26d36301 TM |
383 | { |
384 | const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops; | |
385 | ||
386 | ops->test_and_free_expired(server, stateid, cred); | |
387 | } | |
388 | ||
389 | static void __nfs4_free_revoked_stateid(struct nfs_server *server, | |
390 | nfs4_stateid *stateid, | |
a52458b4 | 391 | const struct cred *cred) |
26d36301 TM |
392 | { |
393 | stateid->type = NFS4_REVOKED_STATEID_TYPE; | |
394 | nfs4_test_and_free_stateid(server, stateid, cred); | |
395 | } | |
396 | ||
397 | static void nfs4_free_revoked_stateid(struct nfs_server *server, | |
398 | const nfs4_stateid *stateid, | |
a52458b4 | 399 | const struct cred *cred) |
26d36301 TM |
400 | { |
401 | nfs4_stateid tmp; | |
402 | ||
403 | nfs4_stateid_copy(&tmp, stateid); | |
404 | __nfs4_free_revoked_stateid(server, &tmp, cred); | |
405 | } | |
406 | ||
8478eaa1 N |
407 | static long nfs4_update_delay(long *timeout) |
408 | { | |
409 | long ret; | |
410 | if (!timeout) | |
411 | return NFS4_POLL_RETRY_MAX; | |
412 | if (*timeout <= 0) | |
413 | *timeout = NFS4_POLL_RETRY_MIN; | |
414 | if (*timeout > NFS4_POLL_RETRY_MAX) | |
415 | *timeout = NFS4_POLL_RETRY_MAX; | |
416 | ret = *timeout; | |
417 | *timeout <<= 1; | |
418 | return ret; | |
419 | } | |
420 | ||
0688e64b | 421 | static int nfs4_delay_killable(long *timeout) |
65de872e | 422 | { |
65de872e TM |
423 | might_sleep(); |
424 | ||
f5d39b02 PZ |
425 | __set_current_state(TASK_KILLABLE|TASK_FREEZABLE_UNSAFE); |
426 | schedule_timeout(nfs4_update_delay(timeout)); | |
0688e64b TM |
427 | if (!__fatal_signal_pending(current)) |
428 | return 0; | |
429 | return -EINTR; | |
430 | } | |
431 | ||
432 | static int nfs4_delay_interruptible(long *timeout) | |
433 | { | |
434 | might_sleep(); | |
435 | ||
f5d39b02 PZ |
436 | __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE_UNSAFE); |
437 | schedule_timeout(nfs4_update_delay(timeout)); | |
0688e64b TM |
438 | if (!signal_pending(current)) |
439 | return 0; | |
440 | return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS; | |
441 | } | |
442 | ||
443 | static int nfs4_delay(long *timeout, bool interruptible) | |
444 | { | |
445 | if (interruptible) | |
446 | return nfs4_delay_interruptible(timeout); | |
447 | return nfs4_delay_killable(timeout); | |
65de872e TM |
448 | } |
449 | ||
50c80007 TM |
450 | static const nfs4_stateid * |
451 | nfs4_recoverable_stateid(const nfs4_stateid *stateid) | |
452 | { | |
453 | if (!stateid) | |
454 | return NULL; | |
455 | switch (stateid->type) { | |
456 | case NFS4_OPEN_STATEID_TYPE: | |
457 | case NFS4_LOCK_STATEID_TYPE: | |
458 | case NFS4_DELEGATION_STATEID_TYPE: | |
459 | return stateid; | |
460 | default: | |
461 | break; | |
462 | } | |
463 | return NULL; | |
464 | } | |
465 | ||
65de872e TM |
466 | /* This is the error handling routine for processes that are allowed |
467 | * to sleep. | |
468 | */ | |
b3c2aa07 TM |
469 | static int nfs4_do_handle_exception(struct nfs_server *server, |
470 | int errorcode, struct nfs4_exception *exception) | |
65de872e TM |
471 | { |
472 | struct nfs_client *clp = server->nfs_client; | |
9e33bed5 | 473 | struct nfs4_state *state = exception->state; |
50c80007 | 474 | const nfs4_stateid *stateid; |
3114ea7a | 475 | struct inode *inode = exception->inode; |
65de872e TM |
476 | int ret = errorcode; |
477 | ||
b3c2aa07 TM |
478 | exception->delay = 0; |
479 | exception->recovering = 0; | |
65de872e | 480 | exception->retry = 0; |
272289a3 | 481 | |
50c80007 | 482 | stateid = nfs4_recoverable_stateid(exception->stateid); |
272289a3 | 483 | if (stateid == NULL && state != NULL) |
50c80007 | 484 | stateid = nfs4_recoverable_stateid(&state->stateid); |
272289a3 | 485 | |
65de872e TM |
486 | switch(errorcode) { |
487 | case 0: | |
488 | return 0; | |
cf61eb26 TM |
489 | case -NFS4ERR_BADHANDLE: |
490 | case -ESTALE: | |
491 | if (inode != NULL && S_ISREG(inode->i_mode)) | |
492 | pnfs_destroy_layout(NFS_I(inode)); | |
493 | break; | |
5ba12443 TM |
494 | case -NFS4ERR_DELEG_REVOKED: |
495 | case -NFS4ERR_ADMIN_REVOKED: | |
272289a3 | 496 | case -NFS4ERR_EXPIRED: |
5ba12443 | 497 | case -NFS4ERR_BAD_STATEID: |
fefa1a81 | 498 | case -NFS4ERR_PARTNER_NO_AUTH: |
272289a3 TM |
499 | if (inode != NULL && stateid != NULL) { |
500 | nfs_inode_find_state_and_recover(inode, | |
501 | stateid); | |
502 | goto wait_on_recovery; | |
503 | } | |
df561f66 | 504 | fallthrough; |
272289a3 | 505 | case -NFS4ERR_OPENMODE: |
8487c479 TM |
506 | if (inode) { |
507 | int err; | |
508 | ||
509 | err = nfs_async_inode_return_delegation(inode, | |
510 | stateid); | |
511 | if (err == 0) | |
512 | goto wait_on_recovery; | |
513 | if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) { | |
514 | exception->retry = 1; | |
515 | break; | |
516 | } | |
517 | } | |
3114ea7a TM |
518 | if (state == NULL) |
519 | break; | |
5d422301 TM |
520 | ret = nfs4_schedule_stateid_recovery(server, state); |
521 | if (ret < 0) | |
522 | break; | |
3114ea7a | 523 | goto wait_on_recovery; |
65de872e | 524 | case -NFS4ERR_STALE_STATEID: |
a2c0b9e2 | 525 | case -NFS4ERR_STALE_CLIENTID: |
0400a6b0 TM |
526 | nfs4_schedule_lease_recovery(clp); |
527 | goto wait_on_recovery; | |
519ae255 CL |
528 | case -NFS4ERR_MOVED: |
529 | ret = nfs4_schedule_migration_recovery(server); | |
530 | if (ret < 0) | |
531 | break; | |
532 | goto wait_on_recovery; | |
8ef2f8d4 CL |
533 | case -NFS4ERR_LEASE_MOVED: |
534 | nfs4_schedule_lease_moved_recovery(clp); | |
535 | goto wait_on_recovery; | |
03391693 | 536 | #if defined(CONFIG_NFS_V4_1) |
4745e315 AA |
537 | case -NFS4ERR_BADSESSION: |
538 | case -NFS4ERR_BADSLOT: | |
539 | case -NFS4ERR_BAD_HIGH_SLOT: | |
540 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
541 | case -NFS4ERR_DEADSESSION: | |
542 | case -NFS4ERR_SEQ_FALSE_RETRY: | |
543 | case -NFS4ERR_SEQ_MISORDERED: | |
5c441544 | 544 | /* Handled in nfs41_sequence_process() */ |
399f11c3 | 545 | goto wait_on_recovery; |
03391693 | 546 | #endif /* defined(CONFIG_NFS_V4_1) */ |
65de872e | 547 | case -NFS4ERR_FILE_OPEN: |
44ed3556 N |
548 | if (exception->timeout > HZ) { |
549 | /* We have retried a decent amount, time to | |
550 | * fail | |
551 | */ | |
552 | ret = -EBUSY; | |
553 | break; | |
554 | } | |
df561f66 | 555 | fallthrough; |
65de872e | 556 | case -NFS4ERR_DELAY: |
2598ed34 | 557 | nfs_inc_server_stats(server, NFSIOS_DELAY); |
df561f66 | 558 | fallthrough; |
2598ed34 | 559 | case -NFS4ERR_GRACE: |
e85d7ee4 | 560 | case -NFS4ERR_LAYOUTTRYLATER: |
183d9e7b | 561 | case -NFS4ERR_RECALLCONFLICT: |
037e56a2 | 562 | case -NFS4ERR_RETURNCONFLICT: |
b3c2aa07 TM |
563 | exception->delay = 1; |
564 | return 0; | |
565 | ||
a8a4ae3a | 566 | case -NFS4ERR_RETRY_UNCACHED_REP: |
65de872e TM |
567 | case -NFS4ERR_OLD_STATEID: |
568 | exception->retry = 1; | |
b064eca2 TM |
569 | break; |
570 | case -NFS4ERR_BADOWNER: | |
571 | /* The following works around a Linux server bug! */ | |
572 | case -NFS4ERR_BADNAME: | |
573 | if (server->caps & NFS_CAP_UIDGID_NOMAP) { | |
574 | server->caps &= ~NFS_CAP_UIDGID_NOMAP; | |
575 | exception->retry = 1; | |
576 | printk(KERN_WARNING "NFS: v4 server %s " | |
577 | "does not accept raw " | |
578 | "uid/gids. " | |
579 | "Reenabling the idmapper.\n", | |
580 | server->nfs_client->cl_hostname); | |
581 | } | |
65de872e TM |
582 | } |
583 | /* We failed to handle the error */ | |
584 | return nfs4_map_errors(ret); | |
0400a6b0 | 585 | wait_on_recovery: |
b3c2aa07 TM |
586 | exception->recovering = 1; |
587 | return 0; | |
588 | } | |
589 | ||
5b9d31ae TM |
590 | /* |
591 | * Track the number of NFS4ERR_DELAY related retransmissions and return | |
592 | * EAGAIN if the 'softerr' mount option is set, and we've exceeded the limit | |
593 | * set by 'nfs_delay_retrans'. | |
594 | */ | |
595 | static int nfs4_exception_should_retrans(const struct nfs_server *server, | |
596 | struct nfs4_exception *exception) | |
597 | { | |
598 | if (server->flags & NFS_MOUNT_SOFTERR && nfs_delay_retrans >= 0) { | |
599 | if (exception->retrans++ >= (unsigned short)nfs_delay_retrans) | |
600 | return -EAGAIN; | |
601 | } | |
602 | return 0; | |
603 | } | |
604 | ||
b3c2aa07 TM |
605 | /* This is the error handling routine for processes that are allowed |
606 | * to sleep. | |
607 | */ | |
608 | int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception) | |
609 | { | |
610 | struct nfs_client *clp = server->nfs_client; | |
611 | int ret; | |
612 | ||
613 | ret = nfs4_do_handle_exception(server, errorcode, exception); | |
614 | if (exception->delay) { | |
5b9d31ae TM |
615 | int ret2 = nfs4_exception_should_retrans(server, exception); |
616 | if (ret2 < 0) { | |
617 | exception->retry = 0; | |
618 | return ret2; | |
619 | } | |
0688e64b TM |
620 | ret = nfs4_delay(&exception->timeout, |
621 | exception->interruptible); | |
b3c2aa07 TM |
622 | goto out_retry; |
623 | } | |
624 | if (exception->recovering) { | |
dfe1fe75 TM |
625 | if (exception->task_is_privileged) |
626 | return -EDEADLOCK; | |
b3c2aa07 TM |
627 | ret = nfs4_wait_clnt_recover(clp); |
628 | if (test_bit(NFS_MIG_FAILED, &server->mig_status)) | |
629 | return -EIO; | |
630 | goto out_retry; | |
631 | } | |
632 | return ret; | |
633 | out_retry: | |
a2c0b9e2 TM |
634 | if (ret == 0) |
635 | exception->retry = 1; | |
636 | return ret; | |
65de872e TM |
637 | } |
638 | ||
037fc980 TM |
639 | static int |
640 | nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server, | |
641 | int errorcode, struct nfs4_exception *exception) | |
642 | { | |
643 | struct nfs_client *clp = server->nfs_client; | |
644 | int ret; | |
645 | ||
646 | ret = nfs4_do_handle_exception(server, errorcode, exception); | |
647 | if (exception->delay) { | |
5b9d31ae TM |
648 | int ret2 = nfs4_exception_should_retrans(server, exception); |
649 | if (ret2 < 0) { | |
650 | exception->retry = 0; | |
651 | return ret2; | |
652 | } | |
037fc980 TM |
653 | rpc_delay(task, nfs4_update_delay(&exception->timeout)); |
654 | goto out_retry; | |
655 | } | |
656 | if (exception->recovering) { | |
dfe1fe75 TM |
657 | if (exception->task_is_privileged) |
658 | return -EDEADLOCK; | |
037fc980 TM |
659 | rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL); |
660 | if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0) | |
661 | rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task); | |
662 | goto out_retry; | |
663 | } | |
519ae255 | 664 | if (test_bit(NFS_MIG_FAILED, &server->mig_status)) |
037fc980 TM |
665 | ret = -EIO; |
666 | return ret; | |
667 | out_retry: | |
0f90be13 | 668 | if (ret == 0) { |
a2c0b9e2 | 669 | exception->retry = 1; |
0f90be13 BB |
670 | /* |
671 | * For NFS4ERR_MOVED, the client transport will need to | |
672 | * be recomputed after migration recovery has completed. | |
673 | */ | |
674 | if (errorcode == -NFS4ERR_MOVED) | |
675 | rpc_task_release_transport(task); | |
676 | } | |
a2c0b9e2 | 677 | return ret; |
65de872e TM |
678 | } |
679 | ||
0f913a57 | 680 | int |
037fc980 TM |
681 | nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server, |
682 | struct nfs4_state *state, long *timeout) | |
683 | { | |
684 | struct nfs4_exception exception = { | |
685 | .state = state, | |
686 | }; | |
687 | ||
688 | if (task->tk_status >= 0) | |
689 | return 0; | |
690 | if (timeout) | |
691 | exception.timeout = *timeout; | |
692 | task->tk_status = nfs4_async_handle_exception(task, server, | |
693 | task->tk_status, | |
694 | &exception); | |
695 | if (exception.delay && timeout) | |
696 | *timeout = exception.timeout; | |
697 | if (exception.retry) | |
698 | return -EAGAIN; | |
699 | return 0; | |
700 | } | |
701 | ||
a5250def WAA |
702 | /* |
703 | * Return 'true' if 'clp' is using an rpc_client that is integrity protected | |
704 | * or 'false' otherwise. | |
705 | */ | |
706 | static bool _nfs4_is_integrity_protected(struct nfs_client *clp) | |
707 | { | |
708 | rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor; | |
eeea5361 | 709 | return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P); |
a5250def | 710 | } |
65de872e | 711 | |
452e9352 | 712 | static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp) |
1da177e4 | 713 | { |
1da177e4 LT |
714 | spin_lock(&clp->cl_lock); |
715 | if (time_before(clp->cl_last_renewal,timestamp)) | |
716 | clp->cl_last_renewal = timestamp; | |
717 | spin_unlock(&clp->cl_lock); | |
718 | } | |
719 | ||
452e9352 TM |
720 | static void renew_lease(const struct nfs_server *server, unsigned long timestamp) |
721 | { | |
be824167 TM |
722 | struct nfs_client *clp = server->nfs_client; |
723 | ||
724 | if (!nfs4_has_session(clp)) | |
725 | do_renew_lease(clp, timestamp); | |
452e9352 TM |
726 | } |
727 | ||
2a3eb2b9 CL |
728 | struct nfs4_call_sync_data { |
729 | const struct nfs_server *seq_server; | |
730 | struct nfs4_sequence_args *seq_args; | |
731 | struct nfs4_sequence_res *seq_res; | |
732 | }; | |
733 | ||
be3a5d23 | 734 | void nfs4_init_sequence(struct nfs4_sequence_args *args, |
fba83f34 AS |
735 | struct nfs4_sequence_res *res, int cache_reply, |
736 | int privileged) | |
a9c92d6b CL |
737 | { |
738 | args->sa_slot = NULL; | |
739 | args->sa_cache_this = cache_reply; | |
fba83f34 | 740 | args->sa_privileged = privileged; |
a9c92d6b CL |
741 | |
742 | res->sr_slot = NULL; | |
743 | } | |
744 | ||
2e80dbe7 | 745 | static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res) |
3bd2384a CL |
746 | { |
747 | struct nfs4_slot *slot = res->sr_slot; | |
748 | struct nfs4_slot_table *tbl; | |
749 | ||
3bd2384a CL |
750 | tbl = slot->table; |
751 | spin_lock(&tbl->slot_tbl_lock); | |
752 | if (!nfs41_wake_and_assign_slot(tbl, slot)) | |
753 | nfs4_free_slot(tbl, slot); | |
754 | spin_unlock(&tbl->slot_tbl_lock); | |
755 | ||
756 | res->sr_slot = NULL; | |
2e80dbe7 TM |
757 | } |
758 | ||
759 | static int nfs40_sequence_done(struct rpc_task *task, | |
760 | struct nfs4_sequence_res *res) | |
761 | { | |
762 | if (res->sr_slot != NULL) | |
763 | nfs40_sequence_free_slot(res); | |
3bd2384a CL |
764 | return 1; |
765 | } | |
766 | ||
cccef3b9 AA |
767 | #if defined(CONFIG_NFS_V4_1) |
768 | ||
3be0f80b | 769 | static void nfs41_release_slot(struct nfs4_slot *slot) |
13615871 | 770 | { |
e3725ec0 | 771 | struct nfs4_session *session; |
13615871 | 772 | struct nfs4_slot_table *tbl; |
c10e4498 | 773 | bool send_new_highest_used_slotid = false; |
13615871 | 774 | |
3be0f80b TM |
775 | if (!slot) |
776 | return; | |
a13ce7c6 | 777 | tbl = slot->table; |
e3725ec0 | 778 | session = tbl->session; |
ea028ac9 | 779 | |
07e8dcbd TM |
780 | /* Bump the slot sequence number */ |
781 | if (slot->seq_done) | |
782 | slot->seq_nr++; | |
783 | slot->seq_done = 0; | |
784 | ||
35dc1d74 | 785 | spin_lock(&tbl->slot_tbl_lock); |
c10e4498 TM |
786 | /* Be nice to the server: try to ensure that the last transmitted |
787 | * value for highest_user_slotid <= target_highest_slotid | |
788 | */ | |
789 | if (tbl->highest_used_slotid > tbl->target_highest_slotid) | |
790 | send_new_highest_used_slotid = true; | |
791 | ||
a13ce7c6 | 792 | if (nfs41_wake_and_assign_slot(tbl, slot)) { |
b75ad4cd TM |
793 | send_new_highest_used_slotid = false; |
794 | goto out_unlock; | |
795 | } | |
a13ce7c6 | 796 | nfs4_free_slot(tbl, slot); |
c10e4498 TM |
797 | |
798 | if (tbl->highest_used_slotid != NFS4_NO_SLOT) | |
799 | send_new_highest_used_slotid = false; | |
b75ad4cd | 800 | out_unlock: |
35dc1d74 | 801 | spin_unlock(&tbl->slot_tbl_lock); |
c10e4498 | 802 | if (send_new_highest_used_slotid) |
3f10a6af | 803 | nfs41_notify_server(session->clp); |
045d2a6d TM |
804 | if (waitqueue_active(&tbl->slot_waitq)) |
805 | wake_up_all(&tbl->slot_waitq); | |
13615871 AA |
806 | } |
807 | ||
3be0f80b TM |
808 | static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res) |
809 | { | |
810 | nfs41_release_slot(res->sr_slot); | |
811 | res->sr_slot = NULL; | |
812 | } | |
813 | ||
3453d570 TM |
814 | static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot, |
815 | u32 seqnr) | |
816 | { | |
817 | if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0) | |
818 | slot->seq_nr_highest_sent = seqnr; | |
819 | } | |
f07a5d24 | 820 | static void nfs4_slot_sequence_acked(struct nfs4_slot *slot, u32 seqnr) |
3453d570 | 821 | { |
f07a5d24 | 822 | nfs4_slot_sequence_record_sent(slot, seqnr); |
3453d570 TM |
823 | slot->seq_nr_last_acked = seqnr; |
824 | } | |
825 | ||
913fadc5 AS |
826 | static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred, |
827 | struct nfs4_slot *slot) | |
828 | { | |
829 | struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true); | |
830 | if (!IS_ERR(task)) | |
831 | rpc_put_task_async(task); | |
832 | } | |
833 | ||
2e80dbe7 TM |
834 | static int nfs41_sequence_process(struct rpc_task *task, |
835 | struct nfs4_sequence_res *res) | |
b0df806c | 836 | { |
e3725ec0 | 837 | struct nfs4_session *session; |
a13ce7c6 | 838 | struct nfs4_slot *slot = res->sr_slot; |
14516c3a | 839 | struct nfs_client *clp; |
5c441544 | 840 | int status; |
85563073 | 841 | int ret = 1; |
b0df806c | 842 | |
a13ce7c6 TM |
843 | if (slot == NULL) |
844 | goto out_noaction; | |
468f8613 | 845 | /* don't increment the sequence number if the task wasn't sent */ |
c71c46f0 | 846 | if (!RPC_WAS_SENT(task) || slot->seq_done) |
b0df806c AA |
847 | goto out; |
848 | ||
e3725ec0 | 849 | session = slot->table->session; |
913fadc5 | 850 | clp = session->clp; |
933602e3 | 851 | |
2f92ae34 | 852 | trace_nfs4_sequence_done(session, res); |
5c441544 TM |
853 | |
854 | status = res->sr_status; | |
855 | if (task->tk_status == -NFS4ERR_DEADSESSION) | |
856 | status = -NFS4ERR_DEADSESSION; | |
857 | ||
691daf3b | 858 | /* Check the SEQUENCE operation status */ |
5c441544 | 859 | switch (status) { |
14516c3a | 860 | case 0: |
3453d570 TM |
861 | /* Mark this sequence number as having been acked */ |
862 | nfs4_slot_sequence_acked(slot, slot->seq_nr); | |
b0df806c | 863 | /* Update the slot's sequence and clientid lease timer */ |
07e8dcbd | 864 | slot->seq_done = 1; |
8e63b6a8 | 865 | do_renew_lease(clp, res->sr_timestamp); |
0629e370 | 866 | /* Check sequence flags */ |
0a014a44 TM |
867 | nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags, |
868 | !!slot->privileged); | |
464ee9f9 | 869 | nfs41_update_target_slotid(slot->table, slot, res); |
14516c3a | 870 | break; |
ac20d163 TM |
871 | case 1: |
872 | /* | |
873 | * sr_status remains 1 if an RPC level error occurred. | |
874 | * The server may or may not have processed the sequence | |
875 | * operation.. | |
ac20d163 | 876 | */ |
3453d570 TM |
877 | nfs4_slot_sequence_record_sent(slot, slot->seq_nr); |
878 | slot->seq_done = 1; | |
ac20d163 | 879 | goto out; |
14516c3a TM |
880 | case -NFS4ERR_DELAY: |
881 | /* The server detected a resend of the RPC call and | |
882 | * returned NFS4ERR_DELAY as per Section 2.10.6.2 | |
883 | * of RFC5661. | |
884 | */ | |
df2fabff | 885 | dprintk("%s: slot=%u seq=%u: Operation in progress\n", |
dfb4f309 | 886 | __func__, |
df2fabff | 887 | slot->slot_nr, |
933602e3 | 888 | slot->seq_nr); |
14516c3a | 889 | goto out_retry; |
f9312a54 TM |
890 | case -NFS4ERR_RETRY_UNCACHED_REP: |
891 | case -NFS4ERR_SEQ_FALSE_RETRY: | |
892 | /* | |
893 | * The server thinks we tried to replay a request. | |
894 | * Retry the call after bumping the sequence ID. | |
895 | */ | |
3453d570 | 896 | nfs4_slot_sequence_acked(slot, slot->seq_nr); |
f9312a54 | 897 | goto retry_new_seq; |
85563073 TM |
898 | case -NFS4ERR_BADSLOT: |
899 | /* | |
900 | * The slot id we used was probably retired. Try again | |
901 | * using a different slot id. | |
902 | */ | |
99589100 | 903 | if (slot->slot_nr < slot->table->target_highest_slotid) |
3be0f80b | 904 | goto session_recover; |
e8794440 TM |
905 | goto retry_nowait; |
906 | case -NFS4ERR_SEQ_MISORDERED: | |
3453d570 | 907 | nfs4_slot_sequence_record_sent(slot, slot->seq_nr); |
ac20d163 | 908 | /* |
3453d570 TM |
909 | * Were one or more calls using this slot interrupted? |
910 | * If the server never received the request, then our | |
913fadc5 AS |
911 | * transmitted slot sequence number may be too high. However, |
912 | * if the server did receive the request then it might | |
913 | * accidentally give us a reply with a mismatched operation. | |
914 | * We can sort this out by sending a lone sequence operation | |
915 | * to the server on the same slot. | |
e8794440 | 916 | */ |
3453d570 TM |
917 | if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) { |
918 | slot->seq_nr--; | |
913fadc5 AS |
919 | if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) { |
920 | nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot); | |
921 | res->sr_slot = NULL; | |
922 | } | |
8e63b6a8 TM |
923 | goto retry_nowait; |
924 | } | |
3453d570 TM |
925 | /* |
926 | * RFC5661: | |
927 | * A retry might be sent while the original request is | |
928 | * still in progress on the replier. The replier SHOULD | |
929 | * deal with the issue by returning NFS4ERR_DELAY as the | |
930 | * reply to SEQUENCE or CB_SEQUENCE operation, but | |
931 | * implementations MAY return NFS4ERR_SEQ_MISORDERED. | |
932 | * | |
933 | * Restart the search after a delay. | |
934 | */ | |
935 | slot->seq_nr = slot->seq_nr_highest_sent; | |
936 | goto out_retry; | |
5c441544 TM |
937 | case -NFS4ERR_BADSESSION: |
938 | case -NFS4ERR_DEADSESSION: | |
939 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
940 | goto session_recover; | |
14516c3a TM |
941 | default: |
942 | /* Just update the slot sequence no. */ | |
07e8dcbd | 943 | slot->seq_done = 1; |
b0df806c AA |
944 | } |
945 | out: | |
946 | /* The session may be reset by one of the error handlers. */ | |
947 | dprintk("%s: Error %d free the slot \n", __func__, res->sr_status); | |
a13ce7c6 | 948 | out_noaction: |
85563073 | 949 | return ret; |
3be0f80b | 950 | session_recover: |
c907e72f | 951 | set_bit(NFS4_SLOT_TBL_DRAINING, &session->fc_slot_table.slot_tbl_state); |
5c441544 TM |
952 | nfs4_schedule_session_recovery(session, status); |
953 | dprintk("%s ERROR: %d Reset session\n", __func__, status); | |
954 | nfs41_sequence_free_slot(res); | |
955 | goto out; | |
3be0f80b TM |
956 | retry_new_seq: |
957 | ++slot->seq_nr; | |
e8794440 TM |
958 | retry_nowait: |
959 | if (rpc_restart_call_prepare(task)) { | |
2e80dbe7 | 960 | nfs41_sequence_free_slot(res); |
e8794440 TM |
961 | task->tk_status = 0; |
962 | ret = 0; | |
963 | } | |
964 | goto out; | |
14516c3a | 965 | out_retry: |
d05dd4e9 | 966 | if (!rpc_restart_call(task)) |
14516c3a TM |
967 | goto out; |
968 | rpc_delay(task, NFS4_POLL_RETRY_MAX); | |
969 | return 0; | |
b0df806c | 970 | } |
2e80dbe7 TM |
971 | |
972 | int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res) | |
973 | { | |
974 | if (!nfs41_sequence_process(task, res)) | |
975 | return 0; | |
976 | if (res->sr_slot != NULL) | |
977 | nfs41_sequence_free_slot(res); | |
978 | return 1; | |
979 | ||
980 | } | |
f9c96fcc | 981 | EXPORT_SYMBOL_GPL(nfs41_sequence_done); |
b0df806c | 982 | |
2e80dbe7 TM |
983 | static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res) |
984 | { | |
985 | if (res->sr_slot == NULL) | |
986 | return 1; | |
987 | if (res->sr_slot->table->session != NULL) | |
988 | return nfs41_sequence_process(task, res); | |
989 | return nfs40_sequence_done(task, res); | |
990 | } | |
991 | ||
992 | static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res) | |
993 | { | |
994 | if (res->sr_slot != NULL) { | |
995 | if (res->sr_slot->table->session != NULL) | |
996 | nfs41_sequence_free_slot(res); | |
997 | else | |
998 | nfs40_sequence_free_slot(res); | |
999 | } | |
1000 | } | |
1001 | ||
2c4b131d | 1002 | int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res) |
df896455 | 1003 | { |
e3725ec0 | 1004 | if (res->sr_slot == NULL) |
14516c3a | 1005 | return 1; |
3bd2384a CL |
1006 | if (!res->sr_slot->table->session) |
1007 | return nfs40_sequence_done(task, res); | |
14516c3a | 1008 | return nfs41_sequence_done(task, res); |
df896455 | 1009 | } |
2c4b131d | 1010 | EXPORT_SYMBOL_GPL(nfs4_sequence_done); |
df896455 | 1011 | |
ce5039c1 AA |
1012 | static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata) |
1013 | { | |
2a3eb2b9 | 1014 | struct nfs4_call_sync_data *data = calldata; |
ce5039c1 | 1015 | |
035168ab TM |
1016 | dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server); |
1017 | ||
7981c8a6 AS |
1018 | nfs4_setup_sequence(data->seq_server->nfs_client, |
1019 | data->seq_args, data->seq_res, task); | |
ce5039c1 AA |
1020 | } |
1021 | ||
69ab40c4 AA |
1022 | static void nfs41_call_sync_done(struct rpc_task *task, void *calldata) |
1023 | { | |
2a3eb2b9 | 1024 | struct nfs4_call_sync_data *data = calldata; |
69ab40c4 | 1025 | |
14516c3a | 1026 | nfs41_sequence_done(task, data->seq_res); |
69ab40c4 AA |
1027 | } |
1028 | ||
17280175 | 1029 | static const struct rpc_call_ops nfs41_call_sync_ops = { |
ce5039c1 | 1030 | .rpc_call_prepare = nfs41_call_sync_prepare, |
69ab40c4 | 1031 | .rpc_call_done = nfs41_call_sync_done, |
ce5039c1 AA |
1032 | }; |
1033 | ||
3bd2384a CL |
1034 | #else /* !CONFIG_NFS_V4_1 */ |
1035 | ||
2e80dbe7 TM |
1036 | static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res) |
1037 | { | |
1038 | return nfs40_sequence_done(task, res); | |
1039 | } | |
1040 | ||
1041 | static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res) | |
1042 | { | |
1043 | if (res->sr_slot != NULL) | |
1044 | nfs40_sequence_free_slot(res); | |
1045 | } | |
1046 | ||
2c4b131d PT |
1047 | int nfs4_sequence_done(struct rpc_task *task, |
1048 | struct nfs4_sequence_res *res) | |
9915ea7e | 1049 | { |
3bd2384a | 1050 | return nfs40_sequence_done(task, res); |
9915ea7e | 1051 | } |
2c4b131d | 1052 | EXPORT_SYMBOL_GPL(nfs4_sequence_done); |
3bd2384a CL |
1053 | |
1054 | #endif /* !CONFIG_NFS_V4_1 */ | |
9915ea7e | 1055 | |
c1dffe0b TM |
1056 | static void nfs41_sequence_res_init(struct nfs4_sequence_res *res) |
1057 | { | |
1058 | res->sr_timestamp = jiffies; | |
1059 | res->sr_status_flags = 0; | |
1060 | res->sr_status = 1; | |
1061 | } | |
1062 | ||
3be0f80b TM |
1063 | static |
1064 | void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args, | |
1065 | struct nfs4_sequence_res *res, | |
1066 | struct nfs4_slot *slot) | |
1067 | { | |
1068 | if (!slot) | |
1069 | return; | |
1070 | slot->privileged = args->sa_privileged ? 1 : 0; | |
1071 | args->sa_slot = slot; | |
1072 | ||
1073 | res->sr_slot = slot; | |
3be0f80b TM |
1074 | } |
1075 | ||
1076 | int nfs4_setup_sequence(struct nfs_client *client, | |
7981c8a6 AS |
1077 | struct nfs4_sequence_args *args, |
1078 | struct nfs4_sequence_res *res, | |
1079 | struct rpc_task *task) | |
1080 | { | |
7981c8a6 | 1081 | struct nfs4_session *session = nfs4_get_session(client); |
76ee0354 | 1082 | struct nfs4_slot_table *tbl = client->cl_slot_tbl; |
3d35808b | 1083 | struct nfs4_slot *slot; |
7981c8a6 | 1084 | |
9dd9107f AS |
1085 | /* slot already allocated? */ |
1086 | if (res->sr_slot != NULL) | |
1087 | goto out_start; | |
1088 | ||
6b2e6856 | 1089 | if (session) |
76ee0354 | 1090 | tbl = &session->fc_slot_table; |
76ee0354 | 1091 | |
3453d570 TM |
1092 | spin_lock(&tbl->slot_tbl_lock); |
1093 | /* The state manager will wait until the slot table is empty */ | |
1094 | if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged) | |
1095 | goto out_sleep; | |
1096 | ||
1097 | slot = nfs4_alloc_slot(tbl); | |
1098 | if (IS_ERR(slot)) { | |
3453d570 | 1099 | if (slot == ERR_PTR(-ENOMEM)) |
6b2e6856 | 1100 | goto out_sleep_timeout; |
3453d570 | 1101 | goto out_sleep; |
3d35808b | 1102 | } |
3453d570 | 1103 | spin_unlock(&tbl->slot_tbl_lock); |
3d35808b | 1104 | |
3be0f80b | 1105 | nfs4_sequence_attach_slot(args, res, slot); |
3d35808b | 1106 | |
ad05cc0f | 1107 | trace_nfs4_setup_sequence(session, args); |
9dd9107f | 1108 | out_start: |
c1dffe0b | 1109 | nfs41_sequence_res_init(res); |
9dd9107f AS |
1110 | rpc_call_start(task); |
1111 | return 0; | |
6b2e6856 TM |
1112 | out_sleep_timeout: |
1113 | /* Try again in 1/4 second */ | |
1114 | if (args->sa_privileged) | |
1115 | rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task, | |
1116 | jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED); | |
1117 | else | |
1118 | rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task, | |
1119 | NULL, jiffies + (HZ >> 2)); | |
1120 | spin_unlock(&tbl->slot_tbl_lock); | |
1121 | return -EAGAIN; | |
0dcee8bb AS |
1122 | out_sleep: |
1123 | if (args->sa_privileged) | |
1124 | rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task, | |
8357a9b6 | 1125 | RPC_PRIORITY_PRIVILEGED); |
0dcee8bb AS |
1126 | else |
1127 | rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL); | |
1128 | spin_unlock(&tbl->slot_tbl_lock); | |
1129 | return -EAGAIN; | |
7981c8a6 AS |
1130 | } |
1131 | EXPORT_SYMBOL_GPL(nfs4_setup_sequence); | |
1132 | ||
9915ea7e CL |
1133 | static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata) |
1134 | { | |
1135 | struct nfs4_call_sync_data *data = calldata; | |
42e1cca7 | 1136 | nfs4_setup_sequence(data->seq_server->nfs_client, |
9915ea7e CL |
1137 | data->seq_args, data->seq_res, task); |
1138 | } | |
1139 | ||
1140 | static void nfs40_call_sync_done(struct rpc_task *task, void *calldata) | |
1141 | { | |
1142 | struct nfs4_call_sync_data *data = calldata; | |
1143 | nfs4_sequence_done(task, data->seq_res); | |
1144 | } | |
1145 | ||
1146 | static const struct rpc_call_ops nfs40_call_sync_ops = { | |
1147 | .rpc_call_prepare = nfs40_call_sync_prepare, | |
1148 | .rpc_call_done = nfs40_call_sync_done, | |
1149 | }; | |
1150 | ||
48c05854 AS |
1151 | static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup) |
1152 | { | |
1153 | int ret; | |
1154 | struct rpc_task *task; | |
1155 | ||
1156 | task = rpc_run_task(task_setup); | |
1157 | if (IS_ERR(task)) | |
1158 | return PTR_ERR(task); | |
1159 | ||
1160 | ret = task->tk_status; | |
1161 | rpc_put_task(task); | |
1162 | return ret; | |
1163 | } | |
1164 | ||
c74dfe97 TM |
1165 | static int nfs4_do_call_sync(struct rpc_clnt *clnt, |
1166 | struct nfs_server *server, | |
1167 | struct rpc_message *msg, | |
1168 | struct nfs4_sequence_args *args, | |
1169 | struct nfs4_sequence_res *res, | |
1170 | unsigned short task_flags) | |
ce5039c1 | 1171 | { |
9915ea7e | 1172 | struct nfs_client *clp = server->nfs_client; |
2a3eb2b9 | 1173 | struct nfs4_call_sync_data data = { |
035168ab | 1174 | .seq_server = server, |
ce5039c1 AA |
1175 | .seq_args = args, |
1176 | .seq_res = res, | |
ce5039c1 AA |
1177 | }; |
1178 | struct rpc_task_setup task_setup = { | |
7c513058 | 1179 | .rpc_client = clnt, |
ce5039c1 | 1180 | .rpc_message = msg, |
9915ea7e | 1181 | .callback_ops = clp->cl_mvops->call_sync_ops, |
c74dfe97 TM |
1182 | .callback_data = &data, |
1183 | .flags = task_flags, | |
ce5039c1 AA |
1184 | }; |
1185 | ||
48c05854 | 1186 | return nfs4_call_sync_custom(&task_setup); |
ce5039c1 AA |
1187 | } |
1188 | ||
c74dfe97 TM |
1189 | static int nfs4_call_sync_sequence(struct rpc_clnt *clnt, |
1190 | struct nfs_server *server, | |
1191 | struct rpc_message *msg, | |
1192 | struct nfs4_sequence_args *args, | |
1193 | struct nfs4_sequence_res *res) | |
1194 | { | |
85e39fee OK |
1195 | unsigned short task_flags = 0; |
1196 | ||
118f09ed | 1197 | if (server->caps & NFS_CAP_MOVEABLE) |
85e39fee OK |
1198 | task_flags = RPC_TASK_MOVEABLE; |
1199 | return nfs4_do_call_sync(clnt, server, msg, args, res, task_flags); | |
c74dfe97 TM |
1200 | } |
1201 | ||
1202 | ||
7c513058 BS |
1203 | int nfs4_call_sync(struct rpc_clnt *clnt, |
1204 | struct nfs_server *server, | |
e73b83f2 BS |
1205 | struct rpc_message *msg, |
1206 | struct nfs4_sequence_args *args, | |
1207 | struct nfs4_sequence_res *res, | |
1208 | int cache_reply) | |
1209 | { | |
fba83f34 | 1210 | nfs4_init_sequence(args, res, cache_reply, 0); |
9915ea7e | 1211 | return nfs4_call_sync_sequence(clnt, server, msg, args, res); |
e73b83f2 | 1212 | } |
cccef3b9 | 1213 | |
3c591175 TM |
1214 | static void |
1215 | nfs4_inc_nlink_locked(struct inode *inode) | |
1216 | { | |
82eae5a4 TM |
1217 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE | |
1218 | NFS_INO_INVALID_CTIME | | |
1219 | NFS_INO_INVALID_NLINK); | |
3c591175 TM |
1220 | inc_nlink(inode); |
1221 | } | |
1222 | ||
1301e421 TM |
1223 | static void |
1224 | nfs4_inc_nlink(struct inode *inode) | |
1225 | { | |
1226 | spin_lock(&inode->i_lock); | |
1227 | nfs4_inc_nlink_locked(inode); | |
1228 | spin_unlock(&inode->i_lock); | |
1229 | } | |
1230 | ||
3c591175 TM |
1231 | static void |
1232 | nfs4_dec_nlink_locked(struct inode *inode) | |
1233 | { | |
82eae5a4 TM |
1234 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE | |
1235 | NFS_INO_INVALID_CTIME | | |
1236 | NFS_INO_INVALID_NLINK); | |
3c591175 TM |
1237 | drop_nlink(inode); |
1238 | } | |
1239 | ||
1240 | static void | |
1b523ca9 FL |
1241 | nfs4_update_changeattr_locked(struct inode *inode, |
1242 | struct nfs4_change_info *cinfo, | |
5636ec4e | 1243 | unsigned long timestamp, unsigned long cache_validity) |
1da177e4 | 1244 | { |
1b523ca9 | 1245 | struct nfs_inode *nfsi = NFS_I(inode); |
6f9be83d | 1246 | u64 change_attr = inode_peek_iversion_raw(inode); |
1da177e4 | 1247 | |
b6f80a2e | 1248 | cache_validity |= NFS_INO_INVALID_CTIME | NFS_INO_INVALID_MTIME; |
20cf7d4e TM |
1249 | if (S_ISDIR(inode->i_mode)) |
1250 | cache_validity |= NFS_INO_INVALID_DATA; | |
1b523ca9 | 1251 | |
6f9be83d TM |
1252 | switch (NFS_SERVER(inode)->change_attr_type) { |
1253 | case NFS4_CHANGE_TYPE_IS_UNDEFINED: | |
20cf7d4e | 1254 | if (cinfo->after == change_attr) |
6f9be83d TM |
1255 | goto out; |
1256 | break; | |
1257 | default: | |
1258 | if ((s64)(change_attr - cinfo->after) >= 0) | |
1259 | goto out; | |
1260 | } | |
1261 | ||
20cf7d4e TM |
1262 | inode_set_iversion_raw(inode, cinfo->after); |
1263 | if (!cinfo->atomic || cinfo->before != change_attr) { | |
1264 | if (S_ISDIR(inode->i_mode)) | |
1b523ca9 | 1265 | nfs_force_lookup_revalidate(inode); |
1b523ca9 | 1266 | |
20cf7d4e TM |
1267 | if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) |
1268 | cache_validity |= | |
1269 | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL | | |
1270 | NFS_INO_INVALID_SIZE | NFS_INO_INVALID_OTHER | | |
1271 | NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK | | |
9d047bf6 | 1272 | NFS_INO_INVALID_MODE | NFS_INO_INVALID_XATTR; |
20cf7d4e | 1273 | nfsi->attrtimeo = NFS_MINATTRTIMEO(inode); |
e603a4c1 | 1274 | } |
20cf7d4e | 1275 | nfsi->attrtimeo_timestamp = jiffies; |
d3129ef6 | 1276 | nfsi->read_cache_jiffies = timestamp; |
3235b403 | 1277 | nfsi->attr_gencount = nfs_inc_attr_generation_counter(); |
c8d07159 | 1278 | nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE; |
6f9be83d TM |
1279 | out: |
1280 | nfs_set_cache_invalid(inode, cache_validity); | |
3c591175 TM |
1281 | } |
1282 | ||
1b523ca9 FL |
1283 | void |
1284 | nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo, | |
5636ec4e | 1285 | unsigned long timestamp, unsigned long cache_validity) |
3c591175 TM |
1286 | { |
1287 | spin_lock(&dir->i_lock); | |
1b523ca9 | 1288 | nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity); |
38478b24 | 1289 | spin_unlock(&dir->i_lock); |
1da177e4 LT |
1290 | } |
1291 | ||
8fd1ab74 TM |
1292 | struct nfs4_open_createattrs { |
1293 | struct nfs4_label *label; | |
1294 | struct iattr *sattr; | |
1295 | const __u32 verf[2]; | |
1296 | }; | |
1297 | ||
49f9a0fa TM |
1298 | static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server, |
1299 | int err, struct nfs4_exception *exception) | |
1300 | { | |
1301 | if (err != -EINVAL) | |
1302 | return false; | |
1303 | if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1)) | |
1304 | return false; | |
1305 | server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1; | |
1306 | exception->retry = 1; | |
1307 | return true; | |
1308 | } | |
1309 | ||
1bf85d8c TM |
1310 | static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx) |
1311 | { | |
1312 | return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC); | |
1313 | } | |
1314 | ||
1315 | static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx) | |
1316 | { | |
1317 | fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE); | |
1318 | ||
1319 | return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret; | |
1320 | } | |
1321 | ||
6ae37339 TM |
1322 | static u32 |
1323 | nfs4_map_atomic_open_share(struct nfs_server *server, | |
1324 | fmode_t fmode, int openflags) | |
1325 | { | |
1326 | u32 res = 0; | |
1327 | ||
1328 | switch (fmode & (FMODE_READ | FMODE_WRITE)) { | |
1329 | case FMODE_READ: | |
1330 | res = NFS4_SHARE_ACCESS_READ; | |
1331 | break; | |
1332 | case FMODE_WRITE: | |
1333 | res = NFS4_SHARE_ACCESS_WRITE; | |
1334 | break; | |
1335 | case FMODE_READ|FMODE_WRITE: | |
1336 | res = NFS4_SHARE_ACCESS_BOTH; | |
1337 | } | |
1338 | if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1)) | |
1339 | goto out; | |
1340 | /* Want no delegation if we're using O_DIRECT */ | |
1341 | if (openflags & O_DIRECT) | |
1342 | res |= NFS4_SHARE_WANT_NO_DELEG; | |
1343 | out: | |
1344 | return res; | |
1345 | } | |
1346 | ||
49f9a0fa TM |
1347 | static enum open_claim_type4 |
1348 | nfs4_map_atomic_open_claim(struct nfs_server *server, | |
1349 | enum open_claim_type4 claim) | |
1350 | { | |
1351 | if (server->caps & NFS_CAP_ATOMIC_OPEN_V1) | |
1352 | return claim; | |
1353 | switch (claim) { | |
1354 | default: | |
1355 | return claim; | |
1356 | case NFS4_OPEN_CLAIM_FH: | |
1357 | return NFS4_OPEN_CLAIM_NULL; | |
1358 | case NFS4_OPEN_CLAIM_DELEG_CUR_FH: | |
1359 | return NFS4_OPEN_CLAIM_DELEGATE_CUR; | |
1360 | case NFS4_OPEN_CLAIM_DELEG_PREV_FH: | |
1361 | return NFS4_OPEN_CLAIM_DELEGATE_PREV; | |
1362 | } | |
1363 | } | |
2ced46c2 TM |
1364 | |
1365 | static void nfs4_init_opendata_res(struct nfs4_opendata *p) | |
1366 | { | |
1367 | p->o_res.f_attr = &p->f_attr; | |
c1d51931 TM |
1368 | p->o_res.seqid = p->o_arg.seqid; |
1369 | p->c_res.seqid = p->c_arg.seqid; | |
2ced46c2 | 1370 | p->o_res.server = p->o_arg.server; |
5f657530 | 1371 | p->o_res.access_request = p->o_arg.access; |
2ced46c2 | 1372 | nfs_fattr_init(&p->f_attr); |
6926afd1 | 1373 | nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name); |
2ced46c2 TM |
1374 | } |
1375 | ||
82a2c1b7 | 1376 | static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry, |
dc0b027d | 1377 | struct nfs4_state_owner *sp, fmode_t fmode, int flags, |
8fd1ab74 | 1378 | const struct nfs4_open_createattrs *c, |
4a1c0893 | 1379 | enum open_claim_type4 claim, |
8535b2be | 1380 | gfp_t gfp_mask) |
e56e0b78 | 1381 | { |
82a2c1b7 | 1382 | struct dentry *parent = dget_parent(dentry); |
2b0143b5 | 1383 | struct inode *dir = d_inode(parent); |
e56e0b78 | 1384 | struct nfs_server *server = NFS_SERVER(dir); |
63f5f796 | 1385 | struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); |
8fd1ab74 | 1386 | struct nfs4_label *label = (c != NULL) ? c->label : NULL; |
e56e0b78 TM |
1387 | struct nfs4_opendata *p; |
1388 | ||
8535b2be | 1389 | p = kzalloc(sizeof(*p), gfp_mask); |
e56e0b78 TM |
1390 | if (p == NULL) |
1391 | goto err; | |
14c43f76 | 1392 | |
76baa2b2 AS |
1393 | p->f_attr.label = nfs4_label_alloc(server, gfp_mask); |
1394 | if (IS_ERR(p->f_attr.label)) | |
14c43f76 DQ |
1395 | goto err_free_p; |
1396 | ||
a49c2691 KM |
1397 | p->a_label = nfs4_label_alloc(server, gfp_mask); |
1398 | if (IS_ERR(p->a_label)) | |
1399 | goto err_free_f; | |
1400 | ||
63f5f796 TM |
1401 | alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; |
1402 | p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask); | |
badc76dd | 1403 | if (IS_ERR(p->o_arg.seqid)) |
14c43f76 | 1404 | goto err_free_label; |
82a2c1b7 AV |
1405 | nfs_sb_active(dentry->d_sb); |
1406 | p->dentry = dget(dentry); | |
e56e0b78 TM |
1407 | p->dir = parent; |
1408 | p->owner = sp; | |
1409 | atomic_inc(&sp->so_count); | |
dc0b027d TM |
1410 | p->o_arg.open_flags = flags; |
1411 | p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE); | |
536585cc | 1412 | p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim); |
6ae37339 TM |
1413 | p->o_arg.share_access = nfs4_map_atomic_open_share(server, |
1414 | fmode, flags); | |
90910519 BC |
1415 | if (flags & O_CREAT) { |
1416 | p->o_arg.umask = current_umask(); | |
1417 | p->o_arg.label = nfs4_label_copy(p->a_label, label); | |
1418 | if (c->sattr != NULL && c->sattr->ia_valid != 0) { | |
1419 | p->o_arg.u.attrs = &p->attrs; | |
1420 | memcpy(&p->attrs, c->sattr, sizeof(p->attrs)); | |
1421 | ||
1422 | memcpy(p->o_arg.u.verifier.data, c->verf, | |
1423 | sizeof(p->o_arg.u.verifier.data)); | |
1424 | } | |
1425 | } | |
a1e7f30a TM |
1426 | /* ask server to check for all possible rights as results |
1427 | * are cached */ | |
1428 | switch (p->o_arg.claim) { | |
1429 | default: | |
1430 | break; | |
1431 | case NFS4_OPEN_CLAIM_NULL: | |
1432 | case NFS4_OPEN_CLAIM_FH: | |
1433 | p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY | | |
1434 | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE | | |
84631f84 TM |
1435 | NFS4_ACCESS_EXECUTE | |
1436 | nfs_access_xattr_mask(server); | |
ae2bb032 | 1437 | } |
7539bbab | 1438 | p->o_arg.clientid = server->nfs_client->cl_clientid; |
95b72eb0 TM |
1439 | p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time); |
1440 | p->o_arg.id.uniquifier = sp->so_seqid.owner_id; | |
82a2c1b7 | 1441 | p->o_arg.name = &dentry->d_name; |
e56e0b78 | 1442 | p->o_arg.server = server; |
aa9c2669 | 1443 | p->o_arg.bitmask = nfs4_bitmask(server, label); |
1549210f | 1444 | p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0]; |
49f9a0fa | 1445 | switch (p->o_arg.claim) { |
4a1c0893 TM |
1446 | case NFS4_OPEN_CLAIM_NULL: |
1447 | case NFS4_OPEN_CLAIM_DELEGATE_CUR: | |
1448 | case NFS4_OPEN_CLAIM_DELEGATE_PREV: | |
1449 | p->o_arg.fh = NFS_FH(dir); | |
1450 | break; | |
1451 | case NFS4_OPEN_CLAIM_PREVIOUS: | |
1452 | case NFS4_OPEN_CLAIM_FH: | |
1453 | case NFS4_OPEN_CLAIM_DELEG_CUR_FH: | |
1454 | case NFS4_OPEN_CLAIM_DELEG_PREV_FH: | |
2b0143b5 | 1455 | p->o_arg.fh = NFS_FH(d_inode(dentry)); |
4a1c0893 | 1456 | } |
cdd4e68b TM |
1457 | p->c_arg.fh = &p->o_res.fh; |
1458 | p->c_arg.stateid = &p->o_res.stateid; | |
1459 | p->c_arg.seqid = p->o_arg.seqid; | |
2ced46c2 | 1460 | nfs4_init_opendata_res(p); |
c6d00e63 | 1461 | kref_init(&p->kref); |
e56e0b78 | 1462 | return p; |
14c43f76 DQ |
1463 | |
1464 | err_free_label: | |
a49c2691 KM |
1465 | nfs4_label_free(p->a_label); |
1466 | err_free_f: | |
76baa2b2 | 1467 | nfs4_label_free(p->f_attr.label); |
14c43f76 | 1468 | err_free_p: |
e56e0b78 TM |
1469 | kfree(p); |
1470 | err: | |
1471 | dput(parent); | |
1472 | return NULL; | |
1473 | } | |
1474 | ||
c6d00e63 | 1475 | static void nfs4_opendata_free(struct kref *kref) |
e56e0b78 | 1476 | { |
c6d00e63 TM |
1477 | struct nfs4_opendata *p = container_of(kref, |
1478 | struct nfs4_opendata, kref); | |
82a2c1b7 | 1479 | struct super_block *sb = p->dentry->d_sb; |
c6d00e63 | 1480 | |
30ae2412 | 1481 | nfs4_lgopen_release(p->lgp); |
c6d00e63 | 1482 | nfs_free_seqid(p->o_arg.seqid); |
2e80dbe7 | 1483 | nfs4_sequence_free_slot(&p->o_res.seq_res); |
aac00a8d TM |
1484 | if (p->state != NULL) |
1485 | nfs4_put_open_state(p->state); | |
c6d00e63 | 1486 | nfs4_put_state_owner(p->owner); |
14c43f76 | 1487 | |
a49c2691 | 1488 | nfs4_label_free(p->a_label); |
76baa2b2 | 1489 | nfs4_label_free(p->f_attr.label); |
14c43f76 | 1490 | |
c6d00e63 | 1491 | dput(p->dir); |
82a2c1b7 AV |
1492 | dput(p->dentry); |
1493 | nfs_sb_deactive(sb); | |
6926afd1 | 1494 | nfs_fattr_free_names(&p->f_attr); |
e911b815 | 1495 | kfree(p->f_attr.mdsthreshold); |
c6d00e63 TM |
1496 | kfree(p); |
1497 | } | |
1498 | ||
1499 | static void nfs4_opendata_put(struct nfs4_opendata *p) | |
1500 | { | |
1501 | if (p != NULL) | |
1502 | kref_put(&p->kref, nfs4_opendata_free); | |
e56e0b78 TM |
1503 | } |
1504 | ||
24311f88 TM |
1505 | static bool nfs4_mode_match_open_stateid(struct nfs4_state *state, |
1506 | fmode_t fmode) | |
1507 | { | |
1508 | switch(fmode & (FMODE_READ|FMODE_WRITE)) { | |
1509 | case FMODE_READ|FMODE_WRITE: | |
1510 | return state->n_rdwr != 0; | |
1511 | case FMODE_WRITE: | |
1512 | return state->n_wronly != 0; | |
1513 | case FMODE_READ: | |
1514 | return state->n_rdonly != 0; | |
1515 | } | |
1516 | WARN_ON_ONCE(1); | |
1517 | return false; | |
1518 | } | |
1519 | ||
be189f7e TM |
1520 | static int can_open_cached(struct nfs4_state *state, fmode_t mode, |
1521 | int open_mode, enum open_claim_type4 claim) | |
6ee41268 TM |
1522 | { |
1523 | int ret = 0; | |
dc0b027d | 1524 | |
536e43d1 | 1525 | if (open_mode & (O_EXCL|O_TRUNC)) |
dc0b027d | 1526 | goto out; |
be189f7e TM |
1527 | switch (claim) { |
1528 | case NFS4_OPEN_CLAIM_NULL: | |
1529 | case NFS4_OPEN_CLAIM_FH: | |
1530 | goto out; | |
1531 | default: | |
1532 | break; | |
1533 | } | |
dc0b027d | 1534 | switch (mode & (FMODE_READ|FMODE_WRITE)) { |
6ee41268 | 1535 | case FMODE_READ: |
88069f77 TM |
1536 | ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 |
1537 | && state->n_rdonly != 0; | |
6ee41268 TM |
1538 | break; |
1539 | case FMODE_WRITE: | |
88069f77 TM |
1540 | ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 |
1541 | && state->n_wronly != 0; | |
6ee41268 TM |
1542 | break; |
1543 | case FMODE_READ|FMODE_WRITE: | |
88069f77 TM |
1544 | ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 |
1545 | && state->n_rdwr != 0; | |
6ee41268 | 1546 | } |
dc0b027d | 1547 | out: |
6ee41268 TM |
1548 | return ret; |
1549 | } | |
1550 | ||
2a606188 TM |
1551 | static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode, |
1552 | enum open_claim_type4 claim) | |
aac00a8d | 1553 | { |
652f89f6 TM |
1554 | if (delegation == NULL) |
1555 | return 0; | |
dc0b027d | 1556 | if ((delegation->type & fmode) != fmode) |
aac00a8d | 1557 | return 0; |
2a606188 TM |
1558 | switch (claim) { |
1559 | case NFS4_OPEN_CLAIM_NULL: | |
1560 | case NFS4_OPEN_CLAIM_FH: | |
1561 | break; | |
1562 | case NFS4_OPEN_CLAIM_PREVIOUS: | |
1563 | if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags)) | |
1564 | break; | |
df561f66 | 1565 | fallthrough; |
2a606188 TM |
1566 | default: |
1567 | return 0; | |
1568 | } | |
b7391f44 | 1569 | nfs_mark_delegation_referenced(delegation); |
aac00a8d TM |
1570 | return 1; |
1571 | } | |
1572 | ||
dc0b027d | 1573 | static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode) |
e7616923 | 1574 | { |
dc0b027d | 1575 | switch (fmode) { |
e7616923 TM |
1576 | case FMODE_WRITE: |
1577 | state->n_wronly++; | |
1578 | break; | |
1579 | case FMODE_READ: | |
1580 | state->n_rdonly++; | |
1581 | break; | |
1582 | case FMODE_READ|FMODE_WRITE: | |
1583 | state->n_rdwr++; | |
1584 | } | |
dc0b027d | 1585 | nfs4_state_set_mode_locked(state, state->state | fmode); |
003707c7 TM |
1586 | } |
1587 | ||
8a64c4ef TM |
1588 | #ifdef CONFIG_NFS_V4_1 |
1589 | static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state) | |
1590 | { | |
1591 | if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags)) | |
1592 | return true; | |
1593 | if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags)) | |
1594 | return true; | |
1595 | if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags)) | |
1596 | return true; | |
1597 | return false; | |
1598 | } | |
1599 | #endif /* CONFIG_NFS_V4_1 */ | |
1600 | ||
c9399f21 TM |
1601 | static void nfs_state_log_update_open_stateid(struct nfs4_state *state) |
1602 | { | |
1603 | if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags)) | |
1604 | wake_up_all(&state->waitq); | |
1605 | } | |
1606 | ||
4f14c194 TM |
1607 | static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state) |
1608 | { | |
1609 | struct nfs_client *clp = state->owner->so_server->nfs_client; | |
1610 | bool need_recover = false; | |
1611 | ||
1612 | if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly) | |
1613 | need_recover = true; | |
1614 | if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly) | |
1615 | need_recover = true; | |
1616 | if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr) | |
1617 | need_recover = true; | |
1618 | if (need_recover) | |
1619 | nfs4_state_mark_reclaim_nograce(clp, state); | |
1620 | } | |
1621 | ||
c9399f21 TM |
1622 | /* |
1623 | * Check for whether or not the caller may update the open stateid | |
1624 | * to the value passed in by stateid. | |
1625 | * | |
1626 | * Note: This function relies heavily on the server implementing | |
1627 | * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2 | |
1628 | * correctly. | |
1629 | * i.e. The stateid seqids have to be initialised to 1, and | |
1630 | * are then incremented on every state transition. | |
1631 | */ | |
b4868b44 | 1632 | static bool nfs_stateid_is_sequential(struct nfs4_state *state, |
c9399f21 | 1633 | const nfs4_stateid *stateid) |
e999e80e | 1634 | { |
b4868b44 BC |
1635 | if (test_bit(NFS_OPEN_STATE, &state->flags)) { |
1636 | /* The common case - we're updating to a new sequence number */ | |
01d29f87 TM |
1637 | if (nfs4_stateid_match_other(stateid, &state->open_stateid)) { |
1638 | if (nfs4_stateid_is_next(&state->open_stateid, stateid)) | |
1639 | return true; | |
1640 | return false; | |
b4868b44 | 1641 | } |
01d29f87 | 1642 | /* The server returned a new stateid */ |
c9399f21 | 1643 | } |
01d29f87 TM |
1644 | /* This is the first OPEN in this generation */ |
1645 | if (stateid->seqid == cpu_to_be32(1)) | |
1646 | return true; | |
e999e80e TM |
1647 | return false; |
1648 | } | |
1649 | ||
f95549cf TM |
1650 | static void nfs_resync_open_stateid_locked(struct nfs4_state *state) |
1651 | { | |
3c38cbe2 TM |
1652 | if (!(state->n_wronly || state->n_rdonly || state->n_rdwr)) |
1653 | return; | |
f95549cf TM |
1654 | if (state->n_wronly) |
1655 | set_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1656 | if (state->n_rdonly) | |
1657 | set_bit(NFS_O_RDONLY_STATE, &state->flags); | |
1658 | if (state->n_rdwr) | |
1659 | set_bit(NFS_O_RDWR_STATE, &state->flags); | |
3c38cbe2 | 1660 | set_bit(NFS_OPEN_STATE, &state->flags); |
f95549cf TM |
1661 | } |
1662 | ||
226056c5 TM |
1663 | static void nfs_clear_open_stateid_locked(struct nfs4_state *state, |
1664 | nfs4_stateid *stateid, fmode_t fmode) | |
003707c7 | 1665 | { |
226056c5 TM |
1666 | clear_bit(NFS_O_RDWR_STATE, &state->flags); |
1667 | switch (fmode & (FMODE_READ|FMODE_WRITE)) { | |
1668 | case FMODE_WRITE: | |
1669 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); | |
1670 | break; | |
1671 | case FMODE_READ: | |
1672 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1673 | break; | |
1674 | case 0: | |
1675 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); | |
1676 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1677 | clear_bit(NFS_OPEN_STATE, &state->flags); | |
1678 | } | |
1679 | if (stateid == NULL) | |
1680 | return; | |
3e7dfb16 TM |
1681 | /* Handle OPEN+OPEN_DOWNGRADE races */ |
1682 | if (nfs4_stateid_match_other(stateid, &state->open_stateid) && | |
1683 | !nfs4_stateid_is_newer(stateid, &state->open_stateid)) { | |
f95549cf | 1684 | nfs_resync_open_stateid_locked(state); |
c9399f21 | 1685 | goto out; |
f95549cf | 1686 | } |
003707c7 | 1687 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) |
f597c537 TM |
1688 | nfs4_stateid_copy(&state->stateid, stateid); |
1689 | nfs4_stateid_copy(&state->open_stateid, stateid); | |
ad9e02dc | 1690 | trace_nfs4_open_stateid_update(state->inode, stateid, 0); |
c9399f21 TM |
1691 | out: |
1692 | nfs_state_log_update_open_stateid(state); | |
226056c5 TM |
1693 | } |
1694 | ||
4a1e2feb TM |
1695 | static void nfs_clear_open_stateid(struct nfs4_state *state, |
1696 | nfs4_stateid *arg_stateid, | |
1697 | nfs4_stateid *stateid, fmode_t fmode) | |
226056c5 TM |
1698 | { |
1699 | write_seqlock(&state->seqlock); | |
3e7dfb16 TM |
1700 | /* Ignore, if the CLOSE argment doesn't match the current stateid */ |
1701 | if (nfs4_state_match_open_stateid_other(state, arg_stateid)) | |
1702 | nfs_clear_open_stateid_locked(state, stateid, fmode); | |
226056c5 | 1703 | write_sequnlock(&state->seqlock); |
4f14c194 TM |
1704 | if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) |
1705 | nfs4_schedule_state_manager(state->owner->so_server->nfs_client); | |
226056c5 TM |
1706 | } |
1707 | ||
1393d961 | 1708 | static void nfs_set_open_stateid_locked(struct nfs4_state *state, |
c9399f21 | 1709 | const nfs4_stateid *stateid, nfs4_stateid *freeme) |
e9acf210 TM |
1710 | __must_hold(&state->owner->so_lock) |
1711 | __must_hold(&state->seqlock) | |
1712 | __must_hold(RCU) | |
1713 | ||
003707c7 | 1714 | { |
c9399f21 TM |
1715 | DEFINE_WAIT(wait); |
1716 | int status = 0; | |
1717 | for (;;) { | |
1718 | ||
b4868b44 | 1719 | if (nfs_stateid_is_sequential(state, stateid)) |
003707c7 | 1720 | break; |
b4868b44 | 1721 | |
c9399f21 | 1722 | if (status) |
003707c7 | 1723 | break; |
c9399f21 TM |
1724 | /* Rely on seqids for serialisation with NFSv4.0 */ |
1725 | if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client)) | |
1726 | break; | |
1727 | ||
b4868b44 | 1728 | set_bit(NFS_STATE_CHANGE_WAIT, &state->flags); |
c9399f21 TM |
1729 | prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE); |
1730 | /* | |
1731 | * Ensure we process the state changes in the same order | |
1732 | * in which the server processed them by delaying the | |
1733 | * update of the stateid until we are in sequence. | |
1734 | */ | |
1735 | write_sequnlock(&state->seqlock); | |
1736 | spin_unlock(&state->owner->so_lock); | |
1737 | rcu_read_unlock(); | |
ad9e02dc | 1738 | trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0); |
b4868b44 | 1739 | |
bb002388 | 1740 | if (!fatal_signal_pending(current)) { |
c9399f21 TM |
1741 | if (schedule_timeout(5*HZ) == 0) |
1742 | status = -EAGAIN; | |
1743 | else | |
1744 | status = 0; | |
1745 | } else | |
1746 | status = -EINTR; | |
1747 | finish_wait(&state->waitq, &wait); | |
1748 | rcu_read_lock(); | |
1749 | spin_lock(&state->owner->so_lock); | |
1750 | write_seqlock(&state->seqlock); | |
003707c7 | 1751 | } |
c9399f21 | 1752 | |
e1fff5df TM |
1753 | if (test_bit(NFS_OPEN_STATE, &state->flags) && |
1754 | !nfs4_stateid_match_other(stateid, &state->open_stateid)) { | |
c9399f21 TM |
1755 | nfs4_stateid_copy(freeme, &state->open_stateid); |
1756 | nfs_test_and_clear_all_open_stateid(state); | |
1757 | } | |
1758 | ||
e999e80e TM |
1759 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) |
1760 | nfs4_stateid_copy(&state->stateid, stateid); | |
1761 | nfs4_stateid_copy(&state->open_stateid, stateid); | |
ad9e02dc | 1762 | trace_nfs4_open_stateid_update(state->inode, stateid, status); |
c9399f21 | 1763 | nfs_state_log_update_open_stateid(state); |
003707c7 TM |
1764 | } |
1765 | ||
c9399f21 | 1766 | static void nfs_state_set_open_stateid(struct nfs4_state *state, |
1393d961 | 1767 | const nfs4_stateid *open_stateid, |
1393d961 TM |
1768 | fmode_t fmode, |
1769 | nfs4_stateid *freeme) | |
1da177e4 | 1770 | { |
8bda4e4c TM |
1771 | /* |
1772 | * Protect the call to nfs4_state_set_mode_locked and | |
1773 | * serialise the stateid update | |
1774 | */ | |
1775 | write_seqlock(&state->seqlock); | |
c9399f21 TM |
1776 | nfs_set_open_stateid_locked(state, open_stateid, freeme); |
1777 | switch (fmode) { | |
1778 | case FMODE_READ: | |
1779 | set_bit(NFS_O_RDONLY_STATE, &state->flags); | |
1780 | break; | |
1781 | case FMODE_WRITE: | |
1782 | set_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1783 | break; | |
1784 | case FMODE_READ|FMODE_WRITE: | |
1785 | set_bit(NFS_O_RDWR_STATE, &state->flags); | |
003707c7 | 1786 | } |
c9399f21 TM |
1787 | set_bit(NFS_OPEN_STATE, &state->flags); |
1788 | write_sequnlock(&state->seqlock); | |
1789 | } | |
1790 | ||
27a30cf6 TM |
1791 | static void nfs_state_clear_open_state_flags(struct nfs4_state *state) |
1792 | { | |
1793 | clear_bit(NFS_O_RDWR_STATE, &state->flags); | |
1794 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1795 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); | |
1796 | clear_bit(NFS_OPEN_STATE, &state->flags); | |
1797 | } | |
1798 | ||
c9399f21 TM |
1799 | static void nfs_state_set_delegation(struct nfs4_state *state, |
1800 | const nfs4_stateid *deleg_stateid, | |
1801 | fmode_t fmode) | |
1802 | { | |
1803 | /* | |
1804 | * Protect the call to nfs4_state_set_mode_locked and | |
1805 | * serialise the stateid update | |
1806 | */ | |
1807 | write_seqlock(&state->seqlock); | |
1808 | nfs4_stateid_copy(&state->stateid, deleg_stateid); | |
1809 | set_bit(NFS_DELEGATED_STATE, &state->flags); | |
8bda4e4c | 1810 | write_sequnlock(&state->seqlock); |
1da177e4 LT |
1811 | } |
1812 | ||
9f0c5124 TM |
1813 | static void nfs_state_clear_delegation(struct nfs4_state *state) |
1814 | { | |
1815 | write_seqlock(&state->seqlock); | |
1816 | nfs4_stateid_copy(&state->stateid, &state->open_stateid); | |
1817 | clear_bit(NFS_DELEGATED_STATE, &state->flags); | |
1818 | write_sequnlock(&state->seqlock); | |
1819 | } | |
1820 | ||
ec4b0925 | 1821 | int update_open_stateid(struct nfs4_state *state, |
1393d961 TM |
1822 | const nfs4_stateid *open_stateid, |
1823 | const nfs4_stateid *delegation, | |
1824 | fmode_t fmode) | |
34310430 | 1825 | { |
1393d961 TM |
1826 | struct nfs_server *server = NFS_SERVER(state->inode); |
1827 | struct nfs_client *clp = server->nfs_client; | |
34310430 TM |
1828 | struct nfs_inode *nfsi = NFS_I(state->inode); |
1829 | struct nfs_delegation *deleg_cur; | |
83aa3e0f | 1830 | nfs4_stateid freeme = { }; |
34310430 TM |
1831 | int ret = 0; |
1832 | ||
dc0b027d | 1833 | fmode &= (FMODE_READ|FMODE_WRITE); |
34310430 TM |
1834 | |
1835 | rcu_read_lock(); | |
c9399f21 TM |
1836 | spin_lock(&state->owner->so_lock); |
1837 | if (open_stateid != NULL) { | |
1838 | nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme); | |
1839 | ret = 1; | |
1840 | } | |
1841 | ||
333ac786 | 1842 | deleg_cur = nfs4_get_valid_delegation(state->inode); |
34310430 TM |
1843 | if (deleg_cur == NULL) |
1844 | goto no_delegation; | |
1845 | ||
1846 | spin_lock(&deleg_cur->lock); | |
17f26b12 | 1847 | if (rcu_dereference(nfsi->delegation) != deleg_cur || |
d25be546 | 1848 | test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) || |
dc0b027d | 1849 | (deleg_cur->type & fmode) != fmode) |
34310430 TM |
1850 | goto no_delegation_unlock; |
1851 | ||
1852 | if (delegation == NULL) | |
1853 | delegation = &deleg_cur->stateid; | |
333ac786 | 1854 | else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation)) |
34310430 TM |
1855 | goto no_delegation_unlock; |
1856 | ||
b7391f44 | 1857 | nfs_mark_delegation_referenced(deleg_cur); |
c9399f21 | 1858 | nfs_state_set_delegation(state, &deleg_cur->stateid, fmode); |
34310430 TM |
1859 | ret = 1; |
1860 | no_delegation_unlock: | |
1861 | spin_unlock(&deleg_cur->lock); | |
1862 | no_delegation: | |
c9399f21 TM |
1863 | if (ret) |
1864 | update_open_stateflags(state, fmode); | |
1865 | spin_unlock(&state->owner->so_lock); | |
34310430 TM |
1866 | rcu_read_unlock(); |
1867 | ||
4f14c194 | 1868 | if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) |
1393d961 TM |
1869 | nfs4_schedule_state_manager(clp); |
1870 | if (freeme.type != 0) | |
1871 | nfs4_test_and_free_stateid(server, &freeme, | |
1872 | state->owner->so_cred); | |
34310430 TM |
1873 | |
1874 | return ret; | |
1875 | } | |
1876 | ||
39071e6f TM |
1877 | static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp, |
1878 | const nfs4_stateid *stateid) | |
1879 | { | |
1880 | struct nfs4_state *state = lsp->ls_state; | |
1881 | bool ret = false; | |
1882 | ||
1883 | spin_lock(&state->state_lock); | |
1884 | if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid)) | |
1885 | goto out_noupdate; | |
1886 | if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid)) | |
1887 | goto out_noupdate; | |
1888 | nfs4_stateid_copy(&lsp->ls_stateid, stateid); | |
1889 | ret = true; | |
1890 | out_noupdate: | |
1891 | spin_unlock(&state->state_lock); | |
1892 | return ret; | |
1893 | } | |
34310430 | 1894 | |
dc0b027d | 1895 | static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode) |
aac00a8d TM |
1896 | { |
1897 | struct nfs_delegation *delegation; | |
1898 | ||
f5086242 | 1899 | fmode &= FMODE_READ|FMODE_WRITE; |
aac00a8d | 1900 | rcu_read_lock(); |
40e6aa10 | 1901 | delegation = nfs4_get_valid_delegation(inode); |
dc0b027d | 1902 | if (delegation == NULL || (delegation->type & fmode) == fmode) { |
aac00a8d TM |
1903 | rcu_read_unlock(); |
1904 | return; | |
1905 | } | |
1906 | rcu_read_unlock(); | |
57ec14c5 | 1907 | nfs4_inode_return_delegation(inode); |
aac00a8d TM |
1908 | } |
1909 | ||
6ee41268 | 1910 | static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata) |
aac00a8d TM |
1911 | { |
1912 | struct nfs4_state *state = opendata->state; | |
aac00a8d | 1913 | struct nfs_delegation *delegation; |
f448badd | 1914 | int open_mode = opendata->o_arg.open_flags; |
dc0b027d | 1915 | fmode_t fmode = opendata->o_arg.fmode; |
2a606188 | 1916 | enum open_claim_type4 claim = opendata->o_arg.claim; |
aac00a8d TM |
1917 | nfs4_stateid stateid; |
1918 | int ret = -EAGAIN; | |
1919 | ||
aac00a8d | 1920 | for (;;) { |
61beef75 | 1921 | spin_lock(&state->owner->so_lock); |
be189f7e | 1922 | if (can_open_cached(state, fmode, open_mode, claim)) { |
61beef75 | 1923 | update_open_stateflags(state, fmode); |
6ee41268 | 1924 | spin_unlock(&state->owner->so_lock); |
61beef75 | 1925 | goto out_return_state; |
6ee41268 | 1926 | } |
61beef75 | 1927 | spin_unlock(&state->owner->so_lock); |
34310430 | 1928 | rcu_read_lock(); |
be3df3dd | 1929 | delegation = nfs4_get_valid_delegation(state->inode); |
2a606188 | 1930 | if (!can_open_delegated(delegation, fmode, claim)) { |
34310430 | 1931 | rcu_read_unlock(); |
aac00a8d | 1932 | break; |
34310430 | 1933 | } |
aac00a8d | 1934 | /* Save the delegation */ |
f597c537 | 1935 | nfs4_stateid_copy(&stateid, &delegation->stateid); |
aac00a8d | 1936 | rcu_read_unlock(); |
fa332941 | 1937 | nfs_release_seqid(opendata->o_arg.seqid); |
bdeca1b7 TM |
1938 | if (!opendata->is_recover) { |
1939 | ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode); | |
1940 | if (ret != 0) | |
1941 | goto out; | |
1942 | } | |
aac00a8d | 1943 | ret = -EAGAIN; |
34310430 TM |
1944 | |
1945 | /* Try to update the stateid using the delegation */ | |
dc0b027d | 1946 | if (update_open_stateid(state, NULL, &stateid, fmode)) |
34310430 | 1947 | goto out_return_state; |
aac00a8d | 1948 | } |
aac00a8d TM |
1949 | out: |
1950 | return ERR_PTR(ret); | |
1951 | out_return_state: | |
ace9fad4 | 1952 | refcount_inc(&state->count); |
aac00a8d TM |
1953 | return state; |
1954 | } | |
1955 | ||
e23008ec AA |
1956 | static void |
1957 | nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state) | |
1958 | { | |
1959 | struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client; | |
1960 | struct nfs_delegation *delegation; | |
1961 | int delegation_flags = 0; | |
1962 | ||
1963 | rcu_read_lock(); | |
1964 | delegation = rcu_dereference(NFS_I(state->inode)->delegation); | |
1965 | if (delegation) | |
1966 | delegation_flags = delegation->flags; | |
1967 | rcu_read_unlock(); | |
72d79ff8 TM |
1968 | switch (data->o_arg.claim) { |
1969 | default: | |
1970 | break; | |
1971 | case NFS4_OPEN_CLAIM_DELEGATE_CUR: | |
1972 | case NFS4_OPEN_CLAIM_DELEG_CUR_FH: | |
e23008ec AA |
1973 | pr_err_ratelimited("NFS: Broken NFSv4 server %s is " |
1974 | "returning a delegation for " | |
1975 | "OPEN(CLAIM_DELEGATE_CUR)\n", | |
1976 | clp->cl_hostname); | |
72d79ff8 TM |
1977 | return; |
1978 | } | |
1979 | if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0) | |
e23008ec | 1980 | nfs_inode_set_delegation(state->inode, |
35156bff TM |
1981 | data->owner->so_cred, |
1982 | data->o_res.delegation_type, | |
1983 | &data->o_res.delegation, | |
1984 | data->o_res.pagemod_limit); | |
e23008ec AA |
1985 | else |
1986 | nfs_inode_reclaim_delegation(state->inode, | |
35156bff TM |
1987 | data->owner->so_cred, |
1988 | data->o_res.delegation_type, | |
1989 | &data->o_res.delegation, | |
1990 | data->o_res.pagemod_limit); | |
8b199e58 JL |
1991 | |
1992 | if (data->o_res.do_recall) | |
1993 | nfs_async_inode_return_delegation(state->inode, | |
1994 | &data->o_res.delegation); | |
e23008ec AA |
1995 | } |
1996 | ||
1997 | /* | |
1998 | * Check the inode attributes against the CLAIM_PREVIOUS returned attributes | |
1999 | * and update the nfs4_state. | |
2000 | */ | |
2001 | static struct nfs4_state * | |
2002 | _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data) | |
2003 | { | |
2004 | struct inode *inode = data->state->inode; | |
2005 | struct nfs4_state *state = data->state; | |
2006 | int ret; | |
2007 | ||
d2bfda2e | 2008 | if (!data->rpc_done) { |
37a8484a AS |
2009 | if (data->rpc_status) |
2010 | return ERR_PTR(data->rpc_status); | |
6165a16a | 2011 | return nfs4_try_open_cached(data); |
e23008ec AA |
2012 | } |
2013 | ||
e23008ec AA |
2014 | ret = nfs_refresh_inode(inode, &data->f_attr); |
2015 | if (ret) | |
37a8484a | 2016 | return ERR_PTR(ret); |
e23008ec AA |
2017 | |
2018 | if (data->o_res.delegation_type != 0) | |
2019 | nfs4_opendata_check_deleg(data, state); | |
6165a16a | 2020 | |
e3c8dc76 TM |
2021 | if (!update_open_stateid(state, &data->o_res.stateid, |
2022 | NULL, data->o_arg.fmode)) | |
2023 | return ERR_PTR(-EAGAIN); | |
ace9fad4 | 2024 | refcount_inc(&state->count); |
e23008ec AA |
2025 | |
2026 | return state; | |
e23008ec AA |
2027 | } |
2028 | ||
4e2fcac7 TM |
2029 | static struct inode * |
2030 | nfs4_opendata_get_inode(struct nfs4_opendata *data) | |
2031 | { | |
2032 | struct inode *inode; | |
2033 | ||
2034 | switch (data->o_arg.claim) { | |
2035 | case NFS4_OPEN_CLAIM_NULL: | |
2036 | case NFS4_OPEN_CLAIM_DELEGATE_CUR: | |
2037 | case NFS4_OPEN_CLAIM_DELEGATE_PREV: | |
2038 | if (!(data->f_attr.valid & NFS_ATTR_FATTR)) | |
2039 | return ERR_PTR(-EAGAIN); | |
2040 | inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, | |
cf7ab00a | 2041 | &data->f_attr); |
4e2fcac7 TM |
2042 | break; |
2043 | default: | |
2044 | inode = d_inode(data->dentry); | |
2045 | ihold(inode); | |
2046 | nfs_refresh_inode(inode, &data->f_attr); | |
2047 | } | |
2048 | return inode; | |
2049 | } | |
2050 | ||
e23008ec | 2051 | static struct nfs4_state * |
75e8c48b | 2052 | nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data) |
24ac23ab | 2053 | { |
75e8c48b | 2054 | struct nfs4_state *state; |
24ac23ab | 2055 | struct inode *inode; |
75e8c48b TM |
2056 | |
2057 | inode = nfs4_opendata_get_inode(data); | |
2058 | if (IS_ERR(inode)) | |
2059 | return ERR_CAST(inode); | |
2060 | if (data->state != NULL && data->state->inode == inode) { | |
2061 | state = data->state; | |
ace9fad4 | 2062 | refcount_inc(&state->count); |
75e8c48b TM |
2063 | } else |
2064 | state = nfs4_get_open_state(inode, data->owner); | |
2065 | iput(inode); | |
2066 | if (state == NULL) | |
2067 | state = ERR_PTR(-ENOMEM); | |
2068 | return state; | |
2069 | } | |
2070 | ||
2071 | static struct nfs4_state * | |
2072 | _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) | |
2073 | { | |
2074 | struct nfs4_state *state; | |
24ac23ab | 2075 | |
aac00a8d | 2076 | if (!data->rpc_done) { |
6ee41268 | 2077 | state = nfs4_try_open_cached(data); |
9759b0fb | 2078 | trace_nfs4_cached_open(data->state); |
aac00a8d TM |
2079 | goto out; |
2080 | } | |
2081 | ||
75e8c48b TM |
2082 | state = nfs4_opendata_find_nfs4_state(data); |
2083 | if (IS_ERR(state)) | |
2084 | goto out; | |
2085 | ||
e23008ec AA |
2086 | if (data->o_res.delegation_type != 0) |
2087 | nfs4_opendata_check_deleg(data, state); | |
e3c8dc76 TM |
2088 | if (!update_open_stateid(state, &data->o_res.stateid, |
2089 | NULL, data->o_arg.fmode)) { | |
2090 | nfs4_put_open_state(state); | |
2091 | state = ERR_PTR(-EAGAIN); | |
2092 | } | |
aac00a8d | 2093 | out: |
7aa262b5 | 2094 | nfs_release_seqid(data->o_arg.seqid); |
24ac23ab TM |
2095 | return state; |
2096 | } | |
2097 | ||
e23008ec AA |
2098 | static struct nfs4_state * |
2099 | nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) | |
2100 | { | |
2e80dbe7 TM |
2101 | struct nfs4_state *ret; |
2102 | ||
e23008ec | 2103 | if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) |
2e80dbe7 TM |
2104 | ret =_nfs4_opendata_reclaim_to_nfs4_state(data); |
2105 | else | |
2106 | ret = _nfs4_opendata_to_nfs4_state(data); | |
2107 | nfs4_sequence_free_slot(&data->o_res.seq_res); | |
2108 | return ret; | |
e23008ec AA |
2109 | } |
2110 | ||
0de43976 TM |
2111 | static struct nfs_open_context * |
2112 | nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode) | |
864472e9 TM |
2113 | { |
2114 | struct nfs_inode *nfsi = NFS_I(state->inode); | |
2115 | struct nfs_open_context *ctx; | |
2116 | ||
0de43976 TM |
2117 | rcu_read_lock(); |
2118 | list_for_each_entry_rcu(ctx, &nfsi->open_files, list) { | |
864472e9 TM |
2119 | if (ctx->state != state) |
2120 | continue; | |
0de43976 TM |
2121 | if ((ctx->mode & mode) != mode) |
2122 | continue; | |
2123 | if (!get_nfs_open_context(ctx)) | |
2124 | continue; | |
2125 | rcu_read_unlock(); | |
864472e9 TM |
2126 | return ctx; |
2127 | } | |
0de43976 | 2128 | rcu_read_unlock(); |
864472e9 TM |
2129 | return ERR_PTR(-ENOENT); |
2130 | } | |
2131 | ||
0de43976 TM |
2132 | static struct nfs_open_context * |
2133 | nfs4_state_find_open_context(struct nfs4_state *state) | |
2134 | { | |
2135 | struct nfs_open_context *ctx; | |
2136 | ||
2137 | ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE); | |
2138 | if (!IS_ERR(ctx)) | |
2139 | return ctx; | |
2140 | ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE); | |
2141 | if (!IS_ERR(ctx)) | |
2142 | return ctx; | |
2143 | return nfs4_state_find_open_context_mode(state, FMODE_READ); | |
2144 | } | |
2145 | ||
4a1c0893 TM |
2146 | static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, |
2147 | struct nfs4_state *state, enum open_claim_type4 claim) | |
6f220ed5 TM |
2148 | { |
2149 | struct nfs4_opendata *opendata; | |
2150 | ||
4a1c0893 | 2151 | opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, |
8fd1ab74 | 2152 | NULL, claim, GFP_NOFS); |
6f220ed5 TM |
2153 | if (opendata == NULL) |
2154 | return ERR_PTR(-ENOMEM); | |
2155 | opendata->state = state; | |
ace9fad4 | 2156 | refcount_inc(&state->count); |
6f220ed5 TM |
2157 | return opendata; |
2158 | } | |
2159 | ||
24311f88 | 2160 | static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, |
51069e4a | 2161 | fmode_t fmode) |
864472e9 | 2162 | { |
2ced46c2 | 2163 | struct nfs4_state *newstate; |
51069e4a TM |
2164 | struct nfs_server *server = NFS_SB(opendata->dentry->d_sb); |
2165 | int openflags = opendata->o_arg.open_flags; | |
864472e9 TM |
2166 | int ret; |
2167 | ||
24311f88 | 2168 | if (!nfs4_mode_match_open_stateid(opendata->state, fmode)) |
39f897fd | 2169 | return 0; |
dc0b027d | 2170 | opendata->o_arg.fmode = fmode; |
51069e4a TM |
2171 | opendata->o_arg.share_access = |
2172 | nfs4_map_atomic_open_share(server, fmode, openflags); | |
2ced46c2 TM |
2173 | memset(&opendata->o_res, 0, sizeof(opendata->o_res)); |
2174 | memset(&opendata->c_res, 0, sizeof(opendata->c_res)); | |
2175 | nfs4_init_opendata_res(opendata); | |
b257957e | 2176 | ret = _nfs4_recover_proc_open(opendata); |
864472e9 TM |
2177 | if (ret != 0) |
2178 | return ret; | |
2ced46c2 | 2179 | newstate = nfs4_opendata_to_nfs4_state(opendata); |
1b370bc2 TM |
2180 | if (IS_ERR(newstate)) |
2181 | return PTR_ERR(newstate); | |
24311f88 TM |
2182 | if (newstate != opendata->state) |
2183 | ret = -ESTALE; | |
643168c2 | 2184 | nfs4_close_state(newstate, fmode); |
24311f88 | 2185 | return ret; |
864472e9 TM |
2186 | } |
2187 | ||
2188 | static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state) | |
2189 | { | |
864472e9 TM |
2190 | int ret; |
2191 | ||
2192 | /* memory barrier prior to reading state->n_* */ | |
2193 | smp_rmb(); | |
24311f88 TM |
2194 | ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE); |
2195 | if (ret != 0) | |
2196 | return ret; | |
2197 | ret = nfs4_open_recover_helper(opendata, FMODE_WRITE); | |
2198 | if (ret != 0) | |
2199 | return ret; | |
2200 | ret = nfs4_open_recover_helper(opendata, FMODE_READ); | |
2201 | if (ret != 0) | |
2202 | return ret; | |
1ac7e2fd TM |
2203 | /* |
2204 | * We may have performed cached opens for all three recoveries. | |
2205 | * Check if we need to update the current stateid. | |
2206 | */ | |
2207 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 && | |
f597c537 | 2208 | !nfs4_stateid_match(&state->stateid, &state->open_stateid)) { |
8bda4e4c | 2209 | write_seqlock(&state->seqlock); |
1ac7e2fd | 2210 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) |
f597c537 | 2211 | nfs4_stateid_copy(&state->stateid, &state->open_stateid); |
8bda4e4c | 2212 | write_sequnlock(&state->seqlock); |
1ac7e2fd | 2213 | } |
864472e9 TM |
2214 | return 0; |
2215 | } | |
2216 | ||
1da177e4 LT |
2217 | /* |
2218 | * OPEN_RECLAIM: | |
2219 | * reclaim state on the server after a reboot. | |
1da177e4 | 2220 | */ |
539cd03a | 2221 | static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) |
1da177e4 | 2222 | { |
1ac7e2fd | 2223 | struct nfs_delegation *delegation; |
864472e9 | 2224 | struct nfs4_opendata *opendata; |
dc0b027d | 2225 | fmode_t delegation_type = 0; |
1da177e4 LT |
2226 | int status; |
2227 | ||
4a1c0893 TM |
2228 | opendata = nfs4_open_recoverdata_alloc(ctx, state, |
2229 | NFS4_OPEN_CLAIM_PREVIOUS); | |
6f220ed5 TM |
2230 | if (IS_ERR(opendata)) |
2231 | return PTR_ERR(opendata); | |
1ac7e2fd TM |
2232 | rcu_read_lock(); |
2233 | delegation = rcu_dereference(NFS_I(state->inode)->delegation); | |
15c831bf | 2234 | if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0) |
65bbf6bd | 2235 | delegation_type = delegation->type; |
1ac7e2fd | 2236 | rcu_read_unlock(); |
864472e9 TM |
2237 | opendata->o_arg.u.delegation_type = delegation_type; |
2238 | status = nfs4_open_recover(opendata, state); | |
c6d00e63 | 2239 | nfs4_opendata_put(opendata); |
1da177e4 LT |
2240 | return status; |
2241 | } | |
2242 | ||
539cd03a | 2243 | static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) |
1da177e4 LT |
2244 | { |
2245 | struct nfs_server *server = NFS_SERVER(state->inode); | |
2246 | struct nfs4_exception exception = { }; | |
2247 | int err; | |
2248 | do { | |
539cd03a | 2249 | err = _nfs4_do_open_reclaim(ctx, state); |
42113a75 | 2250 | trace_nfs4_open_reclaim(ctx, 0, err); |
49f9a0fa TM |
2251 | if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) |
2252 | continue; | |
168667c4 | 2253 | if (err != -NFS4ERR_DELAY) |
202b50dc TM |
2254 | break; |
2255 | nfs4_handle_exception(server, err, &exception); | |
1da177e4 LT |
2256 | } while (exception.retry); |
2257 | return err; | |
2258 | } | |
2259 | ||
864472e9 TM |
2260 | static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state) |
2261 | { | |
2262 | struct nfs_open_context *ctx; | |
2263 | int ret; | |
2264 | ||
2265 | ctx = nfs4_state_find_open_context(state); | |
2266 | if (IS_ERR(ctx)) | |
91876b13 | 2267 | return -EAGAIN; |
27a30cf6 TM |
2268 | clear_bit(NFS_DELEGATED_STATE, &state->flags); |
2269 | nfs_state_clear_open_state_flags(state); | |
539cd03a | 2270 | ret = nfs4_do_open_reclaim(ctx, state); |
864472e9 TM |
2271 | put_nfs_open_context(ctx); |
2272 | return ret; | |
2273 | } | |
2274 | ||
dce2630c | 2275 | static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err) |
1da177e4 | 2276 | { |
be76b5b6 TM |
2277 | switch (err) { |
2278 | default: | |
2279 | printk(KERN_ERR "NFS: %s: unhandled error " | |
2280 | "%d.\n", __func__, err); | |
ffb81717 | 2281 | fallthrough; |
be76b5b6 TM |
2282 | case 0: |
2283 | case -ENOENT: | |
8eee52af | 2284 | case -EAGAIN: |
be76b5b6 | 2285 | case -ESTALE: |
67e7b52d | 2286 | case -ETIMEDOUT: |
be76b5b6 TM |
2287 | break; |
2288 | case -NFS4ERR_BADSESSION: | |
2289 | case -NFS4ERR_BADSLOT: | |
2290 | case -NFS4ERR_BAD_HIGH_SLOT: | |
2291 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
2292 | case -NFS4ERR_DEADSESSION: | |
be76b5b6 TM |
2293 | return -EAGAIN; |
2294 | case -NFS4ERR_STALE_CLIENTID: | |
2295 | case -NFS4ERR_STALE_STATEID: | |
be76b5b6 TM |
2296 | /* Don't recall a delegation if it was lost */ |
2297 | nfs4_schedule_lease_recovery(server->nfs_client); | |
2298 | return -EAGAIN; | |
352297b9 CL |
2299 | case -NFS4ERR_MOVED: |
2300 | nfs4_schedule_migration_recovery(server); | |
2301 | return -EAGAIN; | |
8ef2f8d4 CL |
2302 | case -NFS4ERR_LEASE_MOVED: |
2303 | nfs4_schedule_lease_moved_recovery(server->nfs_client); | |
2304 | return -EAGAIN; | |
be76b5b6 TM |
2305 | case -NFS4ERR_DELEG_REVOKED: |
2306 | case -NFS4ERR_ADMIN_REVOKED: | |
404ea356 | 2307 | case -NFS4ERR_EXPIRED: |
be76b5b6 | 2308 | case -NFS4ERR_BAD_STATEID: |
db4f2e63 | 2309 | case -NFS4ERR_OPENMODE: |
be76b5b6 TM |
2310 | nfs_inode_find_state_and_recover(state->inode, |
2311 | stateid); | |
2312 | nfs4_schedule_stateid_recovery(server, state); | |
869f9dfa | 2313 | return -EAGAIN; |
be76b5b6 TM |
2314 | case -NFS4ERR_DELAY: |
2315 | case -NFS4ERR_GRACE: | |
be76b5b6 TM |
2316 | ssleep(1); |
2317 | return -EAGAIN; | |
db4f2e63 TM |
2318 | case -ENOMEM: |
2319 | case -NFS4ERR_DENIED: | |
dce2630c N |
2320 | if (fl) { |
2321 | struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner; | |
2322 | if (lsp) | |
2323 | set_bit(NFS_LOCK_LOST, &lsp->ls_flags); | |
2324 | } | |
db4f2e63 | 2325 | return 0; |
be76b5b6 | 2326 | } |
1da177e4 LT |
2327 | return err; |
2328 | } | |
2329 | ||
24311f88 | 2330 | int nfs4_open_delegation_recall(struct nfs_open_context *ctx, |
5eb8d18c | 2331 | struct nfs4_state *state, const nfs4_stateid *stateid) |
db4f2e63 TM |
2332 | { |
2333 | struct nfs_server *server = NFS_SERVER(state->inode); | |
2334 | struct nfs4_opendata *opendata; | |
24311f88 | 2335 | int err = 0; |
db4f2e63 TM |
2336 | |
2337 | opendata = nfs4_open_recoverdata_alloc(ctx, state, | |
2338 | NFS4_OPEN_CLAIM_DELEG_CUR_FH); | |
2339 | if (IS_ERR(opendata)) | |
2340 | return PTR_ERR(opendata); | |
2341 | nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid); | |
5eb8d18c | 2342 | if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) { |
24311f88 TM |
2343 | err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE); |
2344 | if (err) | |
5eb8d18c TM |
2345 | goto out; |
2346 | } | |
2347 | if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) { | |
24311f88 TM |
2348 | err = nfs4_open_recover_helper(opendata, FMODE_WRITE); |
2349 | if (err) | |
5eb8d18c TM |
2350 | goto out; |
2351 | } | |
2352 | if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) { | |
24311f88 | 2353 | err = nfs4_open_recover_helper(opendata, FMODE_READ); |
5eb8d18c TM |
2354 | if (err) |
2355 | goto out; | |
24311f88 | 2356 | } |
5eb8d18c TM |
2357 | nfs_state_clear_delegation(state); |
2358 | out: | |
db4f2e63 | 2359 | nfs4_opendata_put(opendata); |
dce2630c | 2360 | return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err); |
db4f2e63 TM |
2361 | } |
2362 | ||
be05c860 CL |
2363 | static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata) |
2364 | { | |
2365 | struct nfs4_opendata *data = calldata; | |
2366 | ||
7981c8a6 AS |
2367 | nfs4_setup_sequence(data->o_arg.server->nfs_client, |
2368 | &data->c_arg.seq_args, &data->c_res.seq_res, task); | |
be05c860 CL |
2369 | } |
2370 | ||
cdd4e68b TM |
2371 | static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata) |
2372 | { | |
2373 | struct nfs4_opendata *data = calldata; | |
2374 | ||
17ead6c8 | 2375 | nfs40_sequence_done(task, &data->c_res.seq_res); |
be05c860 | 2376 | |
cdd4e68b | 2377 | data->rpc_status = task->tk_status; |
26e976a8 | 2378 | if (data->rpc_status == 0) { |
f597c537 | 2379 | nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid); |
bb22629e | 2380 | nfs_confirm_seqid(&data->owner->so_seqid, 0); |
26e976a8 | 2381 | renew_lease(data->o_res.server, data->timestamp); |
a7a3b1e9 | 2382 | data->rpc_done = true; |
26e976a8 | 2383 | } |
cdd4e68b TM |
2384 | } |
2385 | ||
2386 | static void nfs4_open_confirm_release(void *calldata) | |
2387 | { | |
2388 | struct nfs4_opendata *data = calldata; | |
2389 | struct nfs4_state *state = NULL; | |
2390 | ||
2391 | /* If this request hasn't been cancelled, do nothing */ | |
a7a3b1e9 | 2392 | if (!data->cancelled) |
cdd4e68b TM |
2393 | goto out_free; |
2394 | /* In case of error, no cleanup! */ | |
3e309914 | 2395 | if (!data->rpc_done) |
cdd4e68b | 2396 | goto out_free; |
cdd4e68b | 2397 | state = nfs4_opendata_to_nfs4_state(data); |
1b370bc2 | 2398 | if (!IS_ERR(state)) |
643168c2 | 2399 | nfs4_close_state(state, data->o_arg.fmode); |
cdd4e68b | 2400 | out_free: |
c6d00e63 | 2401 | nfs4_opendata_put(data); |
cdd4e68b TM |
2402 | } |
2403 | ||
2404 | static const struct rpc_call_ops nfs4_open_confirm_ops = { | |
be05c860 | 2405 | .rpc_call_prepare = nfs4_open_confirm_prepare, |
cdd4e68b TM |
2406 | .rpc_call_done = nfs4_open_confirm_done, |
2407 | .rpc_release = nfs4_open_confirm_release, | |
2408 | }; | |
2409 | ||
2410 | /* | |
2411 | * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata | |
2412 | */ | |
2413 | static int _nfs4_proc_open_confirm(struct nfs4_opendata *data) | |
2414 | { | |
2b0143b5 | 2415 | struct nfs_server *server = NFS_SERVER(d_inode(data->dir)); |
cdd4e68b | 2416 | struct rpc_task *task; |
5138fde0 TM |
2417 | struct rpc_message msg = { |
2418 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM], | |
2419 | .rpc_argp = &data->c_arg, | |
2420 | .rpc_resp = &data->c_res, | |
2421 | .rpc_cred = data->owner->so_cred, | |
2422 | }; | |
c970aa85 TM |
2423 | struct rpc_task_setup task_setup_data = { |
2424 | .rpc_client = server->client, | |
5138fde0 | 2425 | .rpc_message = &msg, |
c970aa85 TM |
2426 | .callback_ops = &nfs4_open_confirm_ops, |
2427 | .callback_data = data, | |
101070ca | 2428 | .workqueue = nfsiod_workqueue, |
61296507 | 2429 | .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, |
c970aa85 | 2430 | }; |
1da177e4 LT |
2431 | int status; |
2432 | ||
fba83f34 AS |
2433 | nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1, |
2434 | data->is_recover); | |
c6d00e63 | 2435 | kref_get(&data->kref); |
a7a3b1e9 | 2436 | data->rpc_done = false; |
3e309914 | 2437 | data->rpc_status = 0; |
5138fde0 | 2438 | data->timestamp = jiffies; |
c970aa85 | 2439 | task = rpc_run_task(&task_setup_data); |
7a1218a2 | 2440 | if (IS_ERR(task)) |
cdd4e68b | 2441 | return PTR_ERR(task); |
820bf85c | 2442 | status = rpc_wait_for_completion_task(task); |
cdd4e68b | 2443 | if (status != 0) { |
a7a3b1e9 | 2444 | data->cancelled = true; |
cdd4e68b TM |
2445 | smp_wmb(); |
2446 | } else | |
2447 | status = data->rpc_status; | |
e6b3c4db | 2448 | rpc_put_task(task); |
1da177e4 LT |
2449 | return status; |
2450 | } | |
2451 | ||
24ac23ab | 2452 | static void nfs4_open_prepare(struct rpc_task *task, void *calldata) |
1da177e4 | 2453 | { |
24ac23ab TM |
2454 | struct nfs4_opendata *data = calldata; |
2455 | struct nfs4_state_owner *sp = data->owner; | |
549b19cc | 2456 | struct nfs_client *clp = sp->so_server->nfs_client; |
2a606188 | 2457 | enum open_claim_type4 claim = data->o_arg.claim; |
5138fde0 | 2458 | |
24ac23ab | 2459 | if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0) |
c8da19b9 | 2460 | goto out_wait; |
aac00a8d TM |
2461 | /* |
2462 | * Check if we still need to send an OPEN call, or if we can use | |
2463 | * a delegation instead. | |
2464 | */ | |
2465 | if (data->state != NULL) { | |
2466 | struct nfs_delegation *delegation; | |
2467 | ||
be189f7e TM |
2468 | if (can_open_cached(data->state, data->o_arg.fmode, |
2469 | data->o_arg.open_flags, claim)) | |
6ee41268 | 2470 | goto out_no_action; |
aac00a8d | 2471 | rcu_read_lock(); |
be3df3dd | 2472 | delegation = nfs4_get_valid_delegation(data->state->inode); |
2a606188 | 2473 | if (can_open_delegated(delegation, data->o_arg.fmode, claim)) |
652f89f6 | 2474 | goto unlock_no_action; |
aac00a8d TM |
2475 | rcu_read_unlock(); |
2476 | } | |
95b72eb0 | 2477 | /* Update client id. */ |
549b19cc | 2478 | data->o_arg.clientid = clp->cl_clientid; |
2a606188 TM |
2479 | switch (claim) { |
2480 | default: | |
2481 | break; | |
8188df17 TM |
2482 | case NFS4_OPEN_CLAIM_PREVIOUS: |
2483 | case NFS4_OPEN_CLAIM_DELEG_CUR_FH: | |
2484 | case NFS4_OPEN_CLAIM_DELEG_PREV_FH: | |
e23008ec | 2485 | data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0]; |
df561f66 | 2486 | fallthrough; |
8188df17 TM |
2487 | case NFS4_OPEN_CLAIM_FH: |
2488 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR]; | |
6f220ed5 | 2489 | } |
26e976a8 | 2490 | data->timestamp = jiffies; |
42e1cca7 | 2491 | if (nfs4_setup_sequence(data->o_arg.server->nfs_client, |
d898528c | 2492 | &data->o_arg.seq_args, |
2240a9e2 TM |
2493 | &data->o_res.seq_res, |
2494 | task) != 0) | |
2495 | nfs_release_seqid(data->o_arg.seqid); | |
549b19cc TM |
2496 | |
2497 | /* Set the create mode (note dependency on the session type) */ | |
2498 | data->o_arg.createmode = NFS4_CREATE_UNCHECKED; | |
2499 | if (data->o_arg.open_flags & O_EXCL) { | |
a1e7f30a TM |
2500 | data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1; |
2501 | if (clp->cl_mvops->minor_version == 0) { | |
2502 | data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE; | |
2503 | /* don't put an ACCESS op in OPEN compound if O_EXCL, | |
2504 | * because ACCESS will return permission denied for | |
2505 | * all bits until close */ | |
2506 | data->o_res.access_request = data->o_arg.access = 0; | |
2507 | } else if (nfs4_has_persistent_session(clp)) | |
549b19cc | 2508 | data->o_arg.createmode = NFS4_CREATE_GUARDED; |
549b19cc | 2509 | } |
6ee41268 | 2510 | return; |
652f89f6 | 2511 | unlock_no_action: |
9759b0fb | 2512 | trace_nfs4_cached_open(data->state); |
652f89f6 | 2513 | rcu_read_unlock(); |
6ee41268 TM |
2514 | out_no_action: |
2515 | task->tk_action = NULL; | |
c8da19b9 | 2516 | out_wait: |
b75ad4cd | 2517 | nfs4_sequence_done(task, &data->o_res.seq_res); |
b257957e AB |
2518 | } |
2519 | ||
24ac23ab TM |
2520 | static void nfs4_open_done(struct rpc_task *task, void *calldata) |
2521 | { | |
2522 | struct nfs4_opendata *data = calldata; | |
1da177e4 | 2523 | |
24ac23ab | 2524 | data->rpc_status = task->tk_status; |
d898528c | 2525 | |
2e80dbe7 | 2526 | if (!nfs4_sequence_process(task, &data->o_res.seq_res)) |
14516c3a | 2527 | return; |
d898528c | 2528 | |
24ac23ab | 2529 | if (task->tk_status == 0) { |
807d66d8 TM |
2530 | if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) { |
2531 | switch (data->o_res.f_attr->mode & S_IFMT) { | |
6f926b5b TM |
2532 | case S_IFREG: |
2533 | break; | |
2534 | case S_IFLNK: | |
24ac23ab | 2535 | data->rpc_status = -ELOOP; |
6f926b5b TM |
2536 | break; |
2537 | case S_IFDIR: | |
24ac23ab | 2538 | data->rpc_status = -EISDIR; |
6f926b5b TM |
2539 | break; |
2540 | default: | |
24ac23ab | 2541 | data->rpc_status = -ENOTDIR; |
807d66d8 | 2542 | } |
6f926b5b | 2543 | } |
26e976a8 | 2544 | renew_lease(data->o_res.server, data->timestamp); |
0f9f95e0 TM |
2545 | if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)) |
2546 | nfs_confirm_seqid(&data->owner->so_seqid, 0); | |
6f926b5b | 2547 | } |
a7a3b1e9 | 2548 | data->rpc_done = true; |
24ac23ab | 2549 | } |
6f926b5b | 2550 | |
24ac23ab TM |
2551 | static void nfs4_open_release(void *calldata) |
2552 | { | |
2553 | struct nfs4_opendata *data = calldata; | |
2554 | struct nfs4_state *state = NULL; | |
2555 | ||
2556 | /* If this request hasn't been cancelled, do nothing */ | |
a7a3b1e9 | 2557 | if (!data->cancelled) |
24ac23ab TM |
2558 | goto out_free; |
2559 | /* In case of error, no cleanup! */ | |
3e309914 | 2560 | if (data->rpc_status != 0 || !data->rpc_done) |
24ac23ab TM |
2561 | goto out_free; |
2562 | /* In case we need an open_confirm, no cleanup! */ | |
2563 | if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) | |
2564 | goto out_free; | |
24ac23ab | 2565 | state = nfs4_opendata_to_nfs4_state(data); |
1b370bc2 | 2566 | if (!IS_ERR(state)) |
643168c2 | 2567 | nfs4_close_state(state, data->o_arg.fmode); |
24ac23ab | 2568 | out_free: |
c6d00e63 | 2569 | nfs4_opendata_put(data); |
24ac23ab TM |
2570 | } |
2571 | ||
2572 | static const struct rpc_call_ops nfs4_open_ops = { | |
2573 | .rpc_call_prepare = nfs4_open_prepare, | |
2574 | .rpc_call_done = nfs4_open_done, | |
2575 | .rpc_release = nfs4_open_release, | |
2576 | }; | |
2577 | ||
3b65a30d FI |
2578 | static int nfs4_run_open_task(struct nfs4_opendata *data, |
2579 | struct nfs_open_context *ctx) | |
24ac23ab | 2580 | { |
2b0143b5 | 2581 | struct inode *dir = d_inode(data->dir); |
24ac23ab TM |
2582 | struct nfs_server *server = NFS_SERVER(dir); |
2583 | struct nfs_openargs *o_arg = &data->o_arg; | |
2584 | struct nfs_openres *o_res = &data->o_res; | |
2585 | struct rpc_task *task; | |
5138fde0 TM |
2586 | struct rpc_message msg = { |
2587 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN], | |
2588 | .rpc_argp = o_arg, | |
2589 | .rpc_resp = o_res, | |
2590 | .rpc_cred = data->owner->so_cred, | |
2591 | }; | |
c970aa85 TM |
2592 | struct rpc_task_setup task_setup_data = { |
2593 | .rpc_client = server->client, | |
5138fde0 | 2594 | .rpc_message = &msg, |
c970aa85 TM |
2595 | .callback_ops = &nfs4_open_ops, |
2596 | .callback_data = data, | |
101070ca | 2597 | .workqueue = nfsiod_workqueue, |
61296507 | 2598 | .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, |
c970aa85 | 2599 | }; |
24ac23ab TM |
2600 | int status; |
2601 | ||
118f09ed | 2602 | if (nfs_server_capable(dir, NFS_CAP_MOVEABLE)) |
85e39fee OK |
2603 | task_setup_data.flags |= RPC_TASK_MOVEABLE; |
2604 | ||
c6d00e63 | 2605 | kref_get(&data->kref); |
a7a3b1e9 | 2606 | data->rpc_done = false; |
3e309914 | 2607 | data->rpc_status = 0; |
a7a3b1e9 BC |
2608 | data->cancelled = false; |
2609 | data->is_recover = false; | |
3b65a30d FI |
2610 | if (!ctx) { |
2611 | nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1); | |
a7a3b1e9 | 2612 | data->is_recover = true; |
67e7b52d | 2613 | task_setup_data.flags |= RPC_TASK_TIMEOUT; |
2409a976 | 2614 | } else { |
3b65a30d | 2615 | nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0); |
2409a976 FI |
2616 | pnfs_lgopen_prepare(data, ctx); |
2617 | } | |
c970aa85 | 2618 | task = rpc_run_task(&task_setup_data); |
d9b67e1e AS |
2619 | if (IS_ERR(task)) |
2620 | return PTR_ERR(task); | |
820bf85c | 2621 | status = rpc_wait_for_completion_task(task); |
d9b67e1e | 2622 | if (status != 0) { |
a7a3b1e9 | 2623 | data->cancelled = true; |
d9b67e1e AS |
2624 | smp_wmb(); |
2625 | } else | |
2626 | status = data->rpc_status; | |
2627 | rpc_put_task(task); | |
b257957e AB |
2628 | |
2629 | return status; | |
2630 | } | |
2631 | ||
2632 | static int _nfs4_recover_proc_open(struct nfs4_opendata *data) | |
2633 | { | |
2b0143b5 | 2634 | struct inode *dir = d_inode(data->dir); |
b257957e | 2635 | struct nfs_openres *o_res = &data->o_res; |
d9b67e1e | 2636 | int status; |
b257957e | 2637 | |
3b65a30d | 2638 | status = nfs4_run_open_task(data, NULL); |
b257957e AB |
2639 | if (status != 0 || !data->rpc_done) |
2640 | return status; | |
2641 | ||
6926afd1 TM |
2642 | nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr); |
2643 | ||
d7e98258 | 2644 | if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) |
b257957e | 2645 | status = _nfs4_proc_open_confirm(data); |
b257957e AB |
2646 | |
2647 | return status; | |
2648 | } | |
2649 | ||
f3792d63 TM |
2650 | /* |
2651 | * Additional permission checks in order to distinguish between an | |
2652 | * open for read, and an open for execute. This works around the | |
2653 | * fact that NFSv4 OPEN treats read and execute permissions as being | |
2654 | * the same. | |
2655 | * Note that in the non-execute case, we want to turn off permission | |
2656 | * checking if we just created a new file (POSIX open() semantics). | |
2657 | */ | |
a52458b4 | 2658 | static int nfs4_opendata_access(const struct cred *cred, |
6168f62c | 2659 | struct nfs4_opendata *opendata, |
d564d2c4 | 2660 | struct nfs4_state *state, fmode_t fmode) |
6168f62c WAA |
2661 | { |
2662 | struct nfs_access_entry cache; | |
1e6f2095 | 2663 | u32 mask, flags; |
6168f62c WAA |
2664 | |
2665 | /* access call failed or for some reason the server doesn't | |
2666 | * support any access modes -- defer access call until later */ | |
2667 | if (opendata->o_res.access_supported == 0) | |
2668 | return 0; | |
2669 | ||
2670 | mask = 0; | |
d564d2c4 | 2671 | if (fmode & FMODE_EXEC) { |
f8d9a897 | 2672 | /* ONLY check for exec rights */ |
1e6f2095 AS |
2673 | if (S_ISDIR(state->inode->i_mode)) |
2674 | mask = NFS4_ACCESS_LOOKUP; | |
2675 | else | |
2676 | mask = NFS4_ACCESS_EXECUTE; | |
f3792d63 | 2677 | } else if ((fmode & FMODE_READ) && !opendata->file_created) |
1e6f2095 | 2678 | mask = NFS4_ACCESS_READ; |
6168f62c | 2679 | |
6168f62c | 2680 | nfs_access_set_mask(&cache, opendata->o_res.access_result); |
73fbb3fa | 2681 | nfs_access_add_cache(state->inode, &cache, cred); |
6168f62c | 2682 | |
1e6f2095 AS |
2683 | flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP; |
2684 | if ((mask & ~cache.mask & flags) == 0) | |
6168f62c WAA |
2685 | return 0; |
2686 | ||
998f40b5 | 2687 | return -EACCES; |
6168f62c WAA |
2688 | } |
2689 | ||
b257957e AB |
2690 | /* |
2691 | * Note: On error, nfs4_proc_open will free the struct nfs4_opendata | |
2692 | */ | |
3b65a30d FI |
2693 | static int _nfs4_proc_open(struct nfs4_opendata *data, |
2694 | struct nfs_open_context *ctx) | |
b257957e | 2695 | { |
2b0143b5 | 2696 | struct inode *dir = d_inode(data->dir); |
b257957e AB |
2697 | struct nfs_server *server = NFS_SERVER(dir); |
2698 | struct nfs_openargs *o_arg = &data->o_arg; | |
2699 | struct nfs_openres *o_res = &data->o_res; | |
2700 | int status; | |
2701 | ||
3b65a30d | 2702 | status = nfs4_run_open_task(data, ctx); |
08ef7bd3 TM |
2703 | if (!data->rpc_done) |
2704 | return status; | |
2705 | if (status != 0) { | |
2706 | if (status == -NFS4ERR_BADNAME && | |
2707 | !(o_arg->open_flags & O_CREAT)) | |
2708 | return -ENOENT; | |
24ac23ab | 2709 | return status; |
08ef7bd3 | 2710 | } |
24ac23ab | 2711 | |
6926afd1 TM |
2712 | nfs_fattr_map_and_free_names(server, &data->f_attr); |
2713 | ||
5bc2afc2 | 2714 | if (o_arg->open_flags & O_CREAT) { |
5bc2afc2 | 2715 | if (o_arg->open_flags & O_EXCL) |
a7a3b1e9 | 2716 | data->file_created = true; |
5bc2afc2 | 2717 | else if (o_res->cinfo.before != o_res->cinfo.after) |
a7a3b1e9 | 2718 | data->file_created = true; |
1eb5d98f JL |
2719 | if (data->file_created || |
2720 | inode_peek_iversion_raw(dir) != o_res->cinfo.after) | |
1b523ca9 FL |
2721 | nfs4_update_changeattr(dir, &o_res->cinfo, |
2722 | o_res->f_attr->time_start, | |
2723 | NFS_INO_INVALID_DATA); | |
5bc2afc2 | 2724 | } |
0df5dd4a TM |
2725 | if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0) |
2726 | server->caps &= ~NFS_CAP_POSIX_LOCK; | |
1da177e4 | 2727 | if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { |
cdd4e68b | 2728 | status = _nfs4_proc_open_confirm(data); |
1da177e4 | 2729 | if (status != 0) |
24ac23ab | 2730 | return status; |
1da177e4 | 2731 | } |
56e0d71e | 2732 | if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) { |
4506f23e OK |
2733 | struct nfs_fh *fh = &o_res->fh; |
2734 | ||
56e0d71e | 2735 | nfs4_sequence_free_slot(&o_res->seq_res); |
4506f23e OK |
2736 | if (o_arg->claim == NFS4_OPEN_CLAIM_FH) |
2737 | fh = NFS_FH(d_inode(data->dentry)); | |
2738 | nfs4_proc_getattr(server, fh, o_res->f_attr, NULL); | |
56e0d71e | 2739 | } |
24ac23ab | 2740 | return 0; |
1da177e4 LT |
2741 | } |
2742 | ||
1da177e4 LT |
2743 | /* |
2744 | * OPEN_EXPIRED: | |
2745 | * reclaim state on the server after a network partition. | |
2746 | * Assumes caller holds the appropriate lock | |
2747 | */ | |
539cd03a | 2748 | static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) |
1da177e4 | 2749 | { |
e56e0b78 | 2750 | struct nfs4_opendata *opendata; |
864472e9 | 2751 | int ret; |
1da177e4 | 2752 | |
51069e4a | 2753 | opendata = nfs4_open_recoverdata_alloc(ctx, state, NFS4_OPEN_CLAIM_FH); |
6f220ed5 TM |
2754 | if (IS_ERR(opendata)) |
2755 | return PTR_ERR(opendata); | |
51069e4a TM |
2756 | /* |
2757 | * We're not recovering a delegation, so ask for no delegation. | |
2758 | * Otherwise the recovery thread could deadlock with an outstanding | |
2759 | * delegation return. | |
2760 | */ | |
2761 | opendata->o_arg.open_flags = O_DIRECT; | |
864472e9 | 2762 | ret = nfs4_open_recover(opendata, state); |
35d05778 | 2763 | if (ret == -ESTALE) |
3d4ff43d | 2764 | d_drop(ctx->dentry); |
c6d00e63 | 2765 | nfs4_opendata_put(opendata); |
864472e9 | 2766 | return ret; |
1da177e4 LT |
2767 | } |
2768 | ||
a9ed2e25 | 2769 | static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) |
202b50dc | 2770 | { |
539cd03a | 2771 | struct nfs_server *server = NFS_SERVER(state->inode); |
202b50dc TM |
2772 | struct nfs4_exception exception = { }; |
2773 | int err; | |
2774 | ||
2775 | do { | |
539cd03a | 2776 | err = _nfs4_open_expired(ctx, state); |
42113a75 | 2777 | trace_nfs4_open_expired(ctx, 0, err); |
49f9a0fa TM |
2778 | if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) |
2779 | continue; | |
a9ed2e25 TM |
2780 | switch (err) { |
2781 | default: | |
2782 | goto out; | |
2783 | case -NFS4ERR_GRACE: | |
2784 | case -NFS4ERR_DELAY: | |
2785 | nfs4_handle_exception(server, err, &exception); | |
2786 | err = 0; | |
2787 | } | |
202b50dc | 2788 | } while (exception.retry); |
a9ed2e25 | 2789 | out: |
202b50dc TM |
2790 | return err; |
2791 | } | |
2792 | ||
1da177e4 LT |
2793 | static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) |
2794 | { | |
1da177e4 | 2795 | struct nfs_open_context *ctx; |
864472e9 | 2796 | int ret; |
1da177e4 | 2797 | |
864472e9 TM |
2798 | ctx = nfs4_state_find_open_context(state); |
2799 | if (IS_ERR(ctx)) | |
91876b13 | 2800 | return -EAGAIN; |
539cd03a | 2801 | ret = nfs4_do_open_expired(ctx, state); |
864472e9 TM |
2802 | put_nfs_open_context(ctx); |
2803 | return ret; | |
1da177e4 LT |
2804 | } |
2805 | ||
41020b67 TM |
2806 | static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state, |
2807 | const nfs4_stateid *stateid) | |
4dfd4f7a | 2808 | { |
41020b67 | 2809 | nfs_remove_bad_delegation(state->inode, stateid); |
9f0c5124 | 2810 | nfs_state_clear_delegation(state); |
4dfd4f7a TM |
2811 | } |
2812 | ||
2813 | static void nfs40_clear_delegation_stateid(struct nfs4_state *state) | |
2814 | { | |
2815 | if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL) | |
41020b67 | 2816 | nfs_finish_clear_delegation_stateid(state, NULL); |
4dfd4f7a TM |
2817 | } |
2818 | ||
2819 | static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) | |
2820 | { | |
2821 | /* NFSv4.0 doesn't allow for delegation recovery on open expire */ | |
2822 | nfs40_clear_delegation_stateid(state); | |
27a30cf6 | 2823 | nfs_state_clear_open_state_flags(state); |
4dfd4f7a TM |
2824 | return nfs4_open_expired(sp, state); |
2825 | } | |
2826 | ||
45870d69 TM |
2827 | static int nfs40_test_and_free_expired_stateid(struct nfs_server *server, |
2828 | nfs4_stateid *stateid, | |
a52458b4 | 2829 | const struct cred *cred) |
45870d69 TM |
2830 | { |
2831 | return -NFS4ERR_BAD_STATEID; | |
2832 | } | |
2833 | ||
f062eb6c | 2834 | #if defined(CONFIG_NFS_V4_1) |
4586f6e2 TM |
2835 | static int nfs41_test_and_free_expired_stateid(struct nfs_server *server, |
2836 | nfs4_stateid *stateid, | |
a52458b4 | 2837 | const struct cred *cred) |
4586f6e2 TM |
2838 | { |
2839 | int status; | |
2840 | ||
f7a62ada TM |
2841 | switch (stateid->type) { |
2842 | default: | |
2843 | break; | |
2844 | case NFS4_INVALID_STATEID_TYPE: | |
2845 | case NFS4_SPECIAL_STATEID_TYPE: | |
2846 | return -NFS4ERR_BAD_STATEID; | |
2847 | case NFS4_REVOKED_STATEID_TYPE: | |
2848 | goto out_free; | |
2849 | } | |
4586f6e2 | 2850 | |
f7a62ada | 2851 | status = nfs41_test_stateid(server, stateid, cred); |
4586f6e2 TM |
2852 | switch (status) { |
2853 | case -NFS4ERR_EXPIRED: | |
2854 | case -NFS4ERR_ADMIN_REVOKED: | |
2855 | case -NFS4ERR_DELEG_REVOKED: | |
f7a62ada TM |
2856 | break; |
2857 | default: | |
4586f6e2 TM |
2858 | return status; |
2859 | } | |
f7a62ada TM |
2860 | out_free: |
2861 | /* Ack the revoked state to the server */ | |
f0b0bf88 | 2862 | nfs41_free_stateid(server, stateid, cred, true); |
f7a62ada | 2863 | return -NFS4ERR_EXPIRED; |
4586f6e2 TM |
2864 | } |
2865 | ||
27a30cf6 | 2866 | static int nfs41_check_delegation_stateid(struct nfs4_state *state) |
f062eb6c | 2867 | { |
f062eb6c | 2868 | struct nfs_server *server = NFS_SERVER(state->inode); |
0c116cad | 2869 | nfs4_stateid stateid; |
ab7cb0df | 2870 | struct nfs_delegation *delegation; |
a52458b4 | 2871 | const struct cred *cred = NULL; |
27a30cf6 | 2872 | int status, ret = NFS_OK; |
3e60ffdd | 2873 | |
ab7cb0df TM |
2874 | /* Get the delegation credential for use by test/free_stateid */ |
2875 | rcu_read_lock(); | |
2876 | delegation = rcu_dereference(NFS_I(state->inode)->delegation); | |
0c116cad | 2877 | if (delegation == NULL) { |
ab7cb0df | 2878 | rcu_read_unlock(); |
9f0c5124 | 2879 | nfs_state_clear_delegation(state); |
27a30cf6 | 2880 | return NFS_OK; |
0c116cad TM |
2881 | } |
2882 | ||
fc51b1cf | 2883 | spin_lock(&delegation->lock); |
0c116cad | 2884 | nfs4_stateid_copy(&stateid, &delegation->stateid); |
0c116cad | 2885 | |
994b15b9 TM |
2886 | if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, |
2887 | &delegation->flags)) { | |
fc51b1cf | 2888 | spin_unlock(&delegation->lock); |
4c8e5447 | 2889 | rcu_read_unlock(); |
27a30cf6 | 2890 | return NFS_OK; |
4c8e5447 | 2891 | } |
0c116cad | 2892 | |
a52458b4 N |
2893 | if (delegation->cred) |
2894 | cred = get_cred(delegation->cred); | |
fc51b1cf | 2895 | spin_unlock(&delegation->lock); |
0c116cad | 2896 | rcu_read_unlock(); |
4586f6e2 | 2897 | status = nfs41_test_and_free_expired_stateid(server, &stateid, cred); |
0c116cad | 2898 | trace_nfs4_test_delegation_stateid(state, NULL, status); |
f7a62ada | 2899 | if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) |
41020b67 | 2900 | nfs_finish_clear_delegation_stateid(state, &stateid); |
27a30cf6 TM |
2901 | else |
2902 | ret = status; | |
ab7cb0df | 2903 | |
8c39a39e | 2904 | put_cred(cred); |
27a30cf6 TM |
2905 | return ret; |
2906 | } | |
2907 | ||
2908 | static void nfs41_delegation_recover_stateid(struct nfs4_state *state) | |
2909 | { | |
2910 | nfs4_stateid tmp; | |
2911 | ||
2912 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) && | |
2913 | nfs4_copy_delegation_stateid(state->inode, state->state, | |
2914 | &tmp, NULL) && | |
2915 | nfs4_stateid_match_other(&state->stateid, &tmp)) | |
2916 | nfs_state_set_delegation(state, &tmp, state->state); | |
2917 | else | |
2918 | nfs_state_clear_delegation(state); | |
3e60ffdd CL |
2919 | } |
2920 | ||
c5896fc8 TM |
2921 | /** |
2922 | * nfs41_check_expired_locks - possibly free a lock stateid | |
2923 | * | |
2924 | * @state: NFSv4 state for an inode | |
2925 | * | |
2926 | * Returns NFS_OK if recovery for this stateid is now finished. | |
2927 | * Otherwise a negative NFS4ERR value is returned. | |
2928 | */ | |
2929 | static int nfs41_check_expired_locks(struct nfs4_state *state) | |
2930 | { | |
2931 | int status, ret = NFS_OK; | |
d75a6a0e | 2932 | struct nfs4_lock_state *lsp, *prev = NULL; |
c5896fc8 TM |
2933 | struct nfs_server *server = NFS_SERVER(state->inode); |
2934 | ||
2935 | if (!test_bit(LK_STATE_IN_USE, &state->flags)) | |
2936 | goto out; | |
d75a6a0e BC |
2937 | |
2938 | spin_lock(&state->state_lock); | |
c5896fc8 TM |
2939 | list_for_each_entry(lsp, &state->lock_states, ls_locks) { |
2940 | if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) { | |
a52458b4 | 2941 | const struct cred *cred = lsp->ls_state->owner->so_cred; |
c5896fc8 | 2942 | |
194bc1f4 | 2943 | refcount_inc(&lsp->ls_count); |
d75a6a0e BC |
2944 | spin_unlock(&state->state_lock); |
2945 | ||
2946 | nfs4_put_lock_state(prev); | |
2947 | prev = lsp; | |
2948 | ||
c5896fc8 TM |
2949 | status = nfs41_test_and_free_expired_stateid(server, |
2950 | &lsp->ls_stateid, | |
2951 | cred); | |
2952 | trace_nfs4_test_lock_stateid(state, lsp, status); | |
2953 | if (status == -NFS4ERR_EXPIRED || | |
2954 | status == -NFS4ERR_BAD_STATEID) { | |
2955 | clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags); | |
67dd4830 | 2956 | lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE; |
c5896fc8 TM |
2957 | if (!recover_lost_locks) |
2958 | set_bit(NFS_LOCK_LOST, &lsp->ls_flags); | |
2959 | } else if (status != NFS_OK) { | |
2960 | ret = status; | |
d75a6a0e BC |
2961 | nfs4_put_lock_state(prev); |
2962 | goto out; | |
c5896fc8 | 2963 | } |
d75a6a0e | 2964 | spin_lock(&state->state_lock); |
c5896fc8 | 2965 | } |
d75a6a0e BC |
2966 | } |
2967 | spin_unlock(&state->state_lock); | |
2968 | nfs4_put_lock_state(prev); | |
c5896fc8 TM |
2969 | out: |
2970 | return ret; | |
2971 | } | |
2972 | ||
3e60ffdd CL |
2973 | /** |
2974 | * nfs41_check_open_stateid - possibly free an open stateid | |
2975 | * | |
2976 | * @state: NFSv4 state for an inode | |
2977 | * | |
2978 | * Returns NFS_OK if recovery for this stateid is now finished. | |
2979 | * Otherwise a negative NFS4ERR value is returned. | |
2980 | */ | |
2981 | static int nfs41_check_open_stateid(struct nfs4_state *state) | |
2982 | { | |
2983 | struct nfs_server *server = NFS_SERVER(state->inode); | |
fcb6d9c6 | 2984 | nfs4_stateid *stateid = &state->open_stateid; |
a52458b4 | 2985 | const struct cred *cred = state->owner->so_cred; |
3e60ffdd CL |
2986 | int status; |
2987 | ||
27a30cf6 | 2988 | if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) |
3e60ffdd | 2989 | return -NFS4ERR_BAD_STATEID; |
4586f6e2 | 2990 | status = nfs41_test_and_free_expired_stateid(server, stateid, cred); |
08cb47fa | 2991 | trace_nfs4_test_open_stateid(state, NULL, status); |
f7a62ada | 2992 | if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) { |
27a30cf6 | 2993 | nfs_state_clear_open_state_flags(state); |
67dd4830 | 2994 | stateid->type = NFS4_INVALID_STATEID_TYPE; |
8a64c4ef | 2995 | return status; |
c0ca0e59 | 2996 | } |
8a64c4ef TM |
2997 | if (nfs_open_stateid_recover_openmode(state)) |
2998 | return -NFS4ERR_OPENMODE; | |
2999 | return NFS_OK; | |
b01dd1d8 BS |
3000 | } |
3001 | ||
3002 | static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) | |
3003 | { | |
eb64cf96 | 3004 | int status; |
b01dd1d8 | 3005 | |
27a30cf6 TM |
3006 | status = nfs41_check_delegation_stateid(state); |
3007 | if (status != NFS_OK) | |
3008 | return status; | |
3009 | nfs41_delegation_recover_stateid(state); | |
3010 | ||
c5896fc8 TM |
3011 | status = nfs41_check_expired_locks(state); |
3012 | if (status != NFS_OK) | |
3013 | return status; | |
3e60ffdd | 3014 | status = nfs41_check_open_stateid(state); |
eb64cf96 CL |
3015 | if (status != NFS_OK) |
3016 | status = nfs4_open_expired(sp, state); | |
3017 | return status; | |
f062eb6c BS |
3018 | } |
3019 | #endif | |
3020 | ||
aa53ed54 JL |
3021 | /* |
3022 | * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-* | |
3023 | * fields corresponding to attributes that were used to store the verifier. | |
3024 | * Make sure we clobber those fields in the later setattr call | |
3025 | */ | |
609339c1 | 3026 | static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata, |
5334c5bd | 3027 | struct iattr *sattr, struct nfs4_label **label) |
aa53ed54 | 3028 | { |
609339c1 TM |
3029 | const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask; |
3030 | __u32 attrset[3]; | |
3031 | unsigned ret; | |
3032 | unsigned i; | |
5334c5bd | 3033 | |
609339c1 TM |
3034 | for (i = 0; i < ARRAY_SIZE(attrset); i++) { |
3035 | attrset[i] = opendata->o_res.attrset[i]; | |
3036 | if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1) | |
3037 | attrset[i] &= ~bitmask[i]; | |
3038 | } | |
3039 | ||
3040 | ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ? | |
3041 | sattr->ia_valid : 0; | |
aa53ed54 | 3042 | |
609339c1 TM |
3043 | if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) { |
3044 | if (sattr->ia_valid & ATTR_ATIME_SET) | |
3045 | ret |= ATTR_ATIME_SET; | |
3046 | else | |
3047 | ret |= ATTR_ATIME; | |
3048 | } | |
5334c5bd | 3049 | |
609339c1 TM |
3050 | if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) { |
3051 | if (sattr->ia_valid & ATTR_MTIME_SET) | |
3052 | ret |= ATTR_MTIME_SET; | |
3053 | else | |
3054 | ret |= ATTR_MTIME; | |
3055 | } | |
5334c5bd | 3056 | |
609339c1 | 3057 | if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL)) |
5334c5bd | 3058 | *label = NULL; |
609339c1 | 3059 | return ret; |
aa53ed54 JL |
3060 | } |
3061 | ||
c21443c2 | 3062 | static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata, |
d564d2c4 | 3063 | struct nfs_open_context *ctx) |
c21443c2 TM |
3064 | { |
3065 | struct nfs4_state_owner *sp = opendata->owner; | |
3066 | struct nfs_server *server = sp->so_server; | |
275bb307 | 3067 | struct dentry *dentry; |
c21443c2 | 3068 | struct nfs4_state *state; |
1bf85d8c | 3069 | fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx); |
cf5b4059 TM |
3070 | struct inode *dir = d_inode(opendata->dir); |
3071 | unsigned long dir_verifier; | |
c21443c2 TM |
3072 | int ret; |
3073 | ||
cf5b4059 | 3074 | dir_verifier = nfs_save_change_attribute(dir); |
c21443c2 | 3075 | |
3b65a30d | 3076 | ret = _nfs4_proc_open(opendata, ctx); |
dca78001 | 3077 | if (ret != 0) |
c21443c2 TM |
3078 | goto out; |
3079 | ||
ae55e59d | 3080 | state = _nfs4_opendata_to_nfs4_state(opendata); |
c21443c2 TM |
3081 | ret = PTR_ERR(state); |
3082 | if (IS_ERR(state)) | |
3083 | goto out; | |
a974deee | 3084 | ctx->state = state; |
c21443c2 TM |
3085 | if (server->caps & NFS_CAP_POSIX_LOCK) |
3086 | set_bit(NFS_STATE_POSIX_LOCKS, &state->flags); | |
a8ce377a JL |
3087 | if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK) |
3088 | set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags); | |
43245eca OK |
3089 | if (opendata->o_res.rflags & NFS4_OPEN_RESULT_PRESERVE_UNLINKED) |
3090 | set_bit(NFS_INO_PRESERVE_UNLINKED, &NFS_I(state->inode)->flags); | |
c21443c2 | 3091 | |
275bb307 | 3092 | dentry = opendata->dentry; |
2b0143b5 | 3093 | if (d_really_is_negative(dentry)) { |
668d0cd5 | 3094 | struct dentry *alias; |
275bb307 | 3095 | d_drop(dentry); |
668d0cd5 AV |
3096 | alias = d_exact_alias(dentry, state->inode); |
3097 | if (!alias) | |
3098 | alias = d_splice_alias(igrab(state->inode), dentry); | |
3099 | /* d_splice_alias() can't fail here - it's a non-directory */ | |
3100 | if (alias) { | |
275bb307 | 3101 | dput(ctx->dentry); |
668d0cd5 | 3102 | ctx->dentry = dentry = alias; |
275bb307 | 3103 | } |
cf5b4059 TM |
3104 | } |
3105 | ||
3106 | switch(opendata->o_arg.claim) { | |
3107 | default: | |
3108 | break; | |
3109 | case NFS4_OPEN_CLAIM_NULL: | |
3110 | case NFS4_OPEN_CLAIM_DELEGATE_CUR: | |
3111 | case NFS4_OPEN_CLAIM_DELEGATE_PREV: | |
3112 | if (!opendata->rpc_done) | |
3113 | break; | |
3114 | if (opendata->o_res.delegation_type != 0) | |
3115 | dir_verifier = nfs_save_change_attribute(dir); | |
3116 | nfs_set_verifier(dentry, dir_verifier); | |
275bb307 TM |
3117 | } |
3118 | ||
af9b6d75 TM |
3119 | /* Parse layoutget results before we check for access */ |
3120 | pnfs_parse_lgopen(state->inode, opendata->lgp, ctx); | |
3121 | ||
d564d2c4 | 3122 | ret = nfs4_opendata_access(sp->so_cred, opendata, state, acc_mode); |
c21443c2 TM |
3123 | if (ret != 0) |
3124 | goto out; | |
3125 | ||
04602539 | 3126 | if (d_inode(dentry) == state->inode) |
c45ffdd2 | 3127 | nfs_inode_attach_open_context(ctx); |
2409a976 | 3128 | |
c21443c2 | 3129 | out: |
2135e5d5 TM |
3130 | if (!opendata->cancelled) { |
3131 | if (opendata->lgp) { | |
3132 | nfs4_lgopen_release(opendata->lgp); | |
3133 | opendata->lgp = NULL; | |
3134 | } | |
0cb98abb | 3135 | nfs4_sequence_free_slot(&opendata->o_res.seq_res); |
2135e5d5 | 3136 | } |
c21443c2 TM |
3137 | return ret; |
3138 | } | |
3139 | ||
1da177e4 | 3140 | /* |
24ac23ab | 3141 | * Returns a referenced nfs4_state |
1da177e4 | 3142 | */ |
82be417a | 3143 | static int _nfs4_do_open(struct inode *dir, |
4197a055 | 3144 | struct nfs_open_context *ctx, |
82be417a | 3145 | int flags, |
8fd1ab74 | 3146 | const struct nfs4_open_createattrs *c, |
5bc2afc2 | 3147 | int *opened) |
1da177e4 LT |
3148 | { |
3149 | struct nfs4_state_owner *sp; | |
3150 | struct nfs4_state *state = NULL; | |
3151 | struct nfs_server *server = NFS_SERVER(dir); | |
e56e0b78 | 3152 | struct nfs4_opendata *opendata; |
4197a055 | 3153 | struct dentry *dentry = ctx->dentry; |
a52458b4 | 3154 | const struct cred *cred = ctx->cred; |
4197a055 | 3155 | struct nfs4_threshold **ctx_th = &ctx->mdsthreshold; |
1bf85d8c | 3156 | fmode_t fmode = _nfs4_ctx_to_openmode(ctx); |
49f9a0fa | 3157 | enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL; |
8fd1ab74 TM |
3158 | struct iattr *sattr = c->sattr; |
3159 | struct nfs4_label *label = c->label; | |
aac00a8d | 3160 | int status; |
1da177e4 LT |
3161 | |
3162 | /* Protect against reboot recovery conflicts */ | |
1da177e4 | 3163 | status = -ENOMEM; |
d1e284d5 TM |
3164 | sp = nfs4_get_state_owner(server, cred, GFP_KERNEL); |
3165 | if (sp == NULL) { | |
1da177e4 LT |
3166 | dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n"); |
3167 | goto out_err; | |
3168 | } | |
334f87dd | 3169 | status = nfs4_client_recover_expired_lease(server->nfs_client); |
58d9714a | 3170 | if (status != 0) |
b4454fe1 | 3171 | goto err_put_state_owner; |
2b0143b5 DH |
3172 | if (d_really_is_positive(dentry)) |
3173 | nfs4_return_incompatible_delegation(d_inode(dentry), fmode); | |
58d9714a | 3174 | status = -ENOMEM; |
2b0143b5 | 3175 | if (d_really_is_positive(dentry)) |
49f9a0fa | 3176 | claim = NFS4_OPEN_CLAIM_FH; |
8fd1ab74 TM |
3177 | opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, |
3178 | c, claim, GFP_KERNEL); | |
e56e0b78 | 3179 | if (opendata == NULL) |
95d35cb4 | 3180 | goto err_put_state_owner; |
1da177e4 | 3181 | |
e911b815 TM |
3182 | if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) { |
3183 | if (!opendata->f_attr.mdsthreshold) { | |
3184 | opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc(); | |
3185 | if (!opendata->f_attr.mdsthreshold) | |
76baa2b2 | 3186 | goto err_opendata_put; |
e911b815 | 3187 | } |
1549210f | 3188 | opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0]; |
82be417a | 3189 | } |
2b0143b5 DH |
3190 | if (d_really_is_positive(dentry)) |
3191 | opendata->state = nfs4_get_open_state(d_inode(dentry), sp); | |
aac00a8d | 3192 | |
d564d2c4 | 3193 | status = _nfs4_open_and_get_state(opendata, ctx); |
6168f62c | 3194 | if (status != 0) |
76baa2b2 | 3195 | goto err_opendata_put; |
3efb9722 | 3196 | state = ctx->state; |
6168f62c | 3197 | |
efcbc04e | 3198 | if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) && |
549b19cc | 3199 | (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) { |
609339c1 | 3200 | unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label); |
a1d1c4f1 TM |
3201 | /* |
3202 | * send create attributes which was not set by open | |
3203 | * with an extra setattr. | |
3204 | */ | |
609339c1 TM |
3205 | if (attrs || label) { |
3206 | unsigned ia_old = sattr->ia_valid; | |
3207 | ||
3208 | sattr->ia_valid = attrs; | |
a1d1c4f1 TM |
3209 | nfs_fattr_init(opendata->o_res.f_attr); |
3210 | status = nfs4_do_setattr(state->inode, cred, | |
3211 | opendata->o_res.f_attr, sattr, | |
1b00ad65 | 3212 | ctx, label); |
a1d1c4f1 TM |
3213 | if (status == 0) { |
3214 | nfs_setattr_update_inode(state->inode, sattr, | |
3215 | opendata->o_res.f_attr); | |
dd225cb3 | 3216 | nfs_setsecurity(state->inode, opendata->o_res.f_attr); |
a1d1c4f1 | 3217 | } |
609339c1 | 3218 | sattr->ia_valid = ia_old; |
1775fd3e | 3219 | } |
0ab64e0e | 3220 | } |
c5c3fb5f | 3221 | if (opened && opendata->file_created) |
73a09dd9 | 3222 | *opened = 1; |
82be417a | 3223 | |
e911b815 | 3224 | if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) { |
82be417a | 3225 | *ctx_th = opendata->f_attr.mdsthreshold; |
e911b815 TM |
3226 | opendata->f_attr.mdsthreshold = NULL; |
3227 | } | |
82be417a | 3228 | |
c6d00e63 | 3229 | nfs4_opendata_put(opendata); |
1da177e4 | 3230 | nfs4_put_state_owner(sp); |
1da177e4 | 3231 | return 0; |
c6d00e63 TM |
3232 | err_opendata_put: |
3233 | nfs4_opendata_put(opendata); | |
e56e0b78 TM |
3234 | err_put_state_owner: |
3235 | nfs4_put_state_owner(sp); | |
1da177e4 | 3236 | out_err: |
1da177e4 LT |
3237 | return status; |
3238 | } | |
3239 | ||
3240 | ||
82be417a | 3241 | static struct nfs4_state *nfs4_do_open(struct inode *dir, |
4197a055 | 3242 | struct nfs_open_context *ctx, |
82be417a AA |
3243 | int flags, |
3244 | struct iattr *sattr, | |
5bc2afc2 TM |
3245 | struct nfs4_label *label, |
3246 | int *opened) | |
1da177e4 | 3247 | { |
49f9a0fa | 3248 | struct nfs_server *server = NFS_SERVER(dir); |
0688e64b TM |
3249 | struct nfs4_exception exception = { |
3250 | .interruptible = true, | |
3251 | }; | |
1da177e4 | 3252 | struct nfs4_state *res; |
8fd1ab74 TM |
3253 | struct nfs4_open_createattrs c = { |
3254 | .label = label, | |
3255 | .sattr = sattr, | |
3256 | .verf = { | |
3257 | [0] = (__u32)jiffies, | |
3258 | [1] = (__u32)current->pid, | |
3259 | }, | |
3260 | }; | |
1da177e4 LT |
3261 | int status; |
3262 | ||
3263 | do { | |
8fd1ab74 | 3264 | status = _nfs4_do_open(dir, ctx, flags, &c, opened); |
3efb9722 | 3265 | res = ctx->state; |
42113a75 | 3266 | trace_nfs4_open_file(ctx, flags, status); |
1da177e4 LT |
3267 | if (status == 0) |
3268 | break; | |
3269 | /* NOTE: BAD_SEQID means the server and client disagree about the | |
3270 | * book-keeping w.r.t. state-changing operations | |
3271 | * (OPEN/CLOSE/LOCK/LOCKU...) | |
3272 | * It is actually a sign of a bug on the client or on the server. | |
3273 | * | |
3274 | * If we receive a BAD_SEQID error in the particular case of | |
cee54fc9 | 3275 | * doing an OPEN, we assume that nfs_increment_open_seqid() will |
1da177e4 LT |
3276 | * have unhashed the old state_owner for us, and that we can |
3277 | * therefore safely retry using a new one. We should still warn | |
3278 | * the user though... | |
3279 | */ | |
3280 | if (status == -NFS4ERR_BAD_SEQID) { | |
9a3ba432 | 3281 | pr_warn_ratelimited("NFS: v4 server %s " |
6f43ddcc TM |
3282 | " returned a bad sequence-id error!\n", |
3283 | NFS_SERVER(dir)->nfs_client->cl_hostname); | |
1da177e4 LT |
3284 | exception.retry = 1; |
3285 | continue; | |
3286 | } | |
550f5747 TM |
3287 | /* |
3288 | * BAD_STATEID on OPEN means that the server cancelled our | |
3289 | * state before it received the OPEN_CONFIRM. | |
3290 | * Recover by retrying the request as per the discussion | |
3291 | * on Page 181 of RFC3530. | |
3292 | */ | |
3293 | if (status == -NFS4ERR_BAD_STATEID) { | |
3294 | exception.retry = 1; | |
3295 | continue; | |
3296 | } | |
924491f2 RM |
3297 | if (status == -NFS4ERR_EXPIRED) { |
3298 | nfs4_schedule_lease_recovery(server->nfs_client); | |
3299 | exception.retry = 1; | |
3300 | continue; | |
3301 | } | |
aac00a8d TM |
3302 | if (status == -EAGAIN) { |
3303 | /* We must have found a delegation */ | |
3304 | exception.retry = 1; | |
3305 | continue; | |
3306 | } | |
49f9a0fa TM |
3307 | if (nfs4_clear_cap_atomic_open_v1(server, status, &exception)) |
3308 | continue; | |
3309 | res = ERR_PTR(nfs4_handle_exception(server, | |
1da177e4 LT |
3310 | status, &exception)); |
3311 | } while (exception.retry); | |
3312 | return res; | |
3313 | } | |
3314 | ||
8487c479 TM |
3315 | static int _nfs4_do_setattr(struct inode *inode, |
3316 | struct nfs_setattrargs *arg, | |
3317 | struct nfs_setattrres *res, | |
a52458b4 | 3318 | const struct cred *cred, |
29b59f94 | 3319 | struct nfs_open_context *ctx) |
1da177e4 | 3320 | { |
3e4f6290 | 3321 | struct nfs_server *server = NFS_SERVER(inode); |
d9b67e1e | 3322 | struct rpc_message msg = { |
659bfcd6 | 3323 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], |
8487c479 TM |
3324 | .rpc_argp = arg, |
3325 | .rpc_resp = res, | |
659bfcd6 | 3326 | .rpc_cred = cred, |
d9b67e1e | 3327 | }; |
a52458b4 | 3328 | const struct cred *delegation_cred = NULL; |
26e976a8 | 3329 | unsigned long timestamp = jiffies; |
ee3ae84e | 3330 | bool truncate; |
65e4308d | 3331 | int status; |
1da177e4 | 3332 | |
8487c479 | 3333 | nfs_fattr_init(res->fattr); |
1da177e4 | 3334 | |
ee3ae84e | 3335 | /* Servers should only apply open mode checks for file size changes */ |
8487c479 | 3336 | truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false; |
644c9f40 CL |
3337 | if (!truncate) { |
3338 | nfs4_inode_make_writeable(inode); | |
991eedb1 | 3339 | goto zero_stateid; |
644c9f40 | 3340 | } |
ee3ae84e | 3341 | |
991eedb1 | 3342 | if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) { |
ee3ae84e | 3343 | /* Use that stateid */ |
09a54f0e | 3344 | } else if (ctx != NULL && ctx->state) { |
17393475 | 3345 | struct nfs_lock_context *l_ctx; |
29b59f94 | 3346 | if (!nfs4_valid_open_stateid(ctx->state)) |
0418dae1 | 3347 | return -EBADF; |
17393475 N |
3348 | l_ctx = nfs_get_lock_context(ctx); |
3349 | if (IS_ERR(l_ctx)) | |
3350 | return PTR_ERR(l_ctx); | |
7a0566b3 N |
3351 | status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx, |
3352 | &arg->stateid, &delegation_cred); | |
3353 | nfs_put_lock_context(l_ctx); | |
3354 | if (status == -EIO) | |
0418dae1 | 3355 | return -EBADF; |
d826e5b8 OK |
3356 | else if (status == -EAGAIN) |
3357 | goto zero_stateid; | |
991eedb1 TM |
3358 | } else { |
3359 | zero_stateid: | |
8487c479 | 3360 | nfs4_stateid_copy(&arg->stateid, &zero_stateid); |
991eedb1 | 3361 | } |
abf4e13c TM |
3362 | if (delegation_cred) |
3363 | msg.rpc_cred = delegation_cred; | |
1da177e4 | 3364 | |
8487c479 | 3365 | status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1); |
abf4e13c | 3366 | |
a52458b4 | 3367 | put_cred(delegation_cred); |
29b59f94 | 3368 | if (status == 0 && ctx != NULL) |
26e976a8 | 3369 | renew_lease(server, timestamp); |
8487c479 | 3370 | trace_nfs4_setattr(inode, &arg->stateid, status); |
65e4308d | 3371 | return status; |
1da177e4 LT |
3372 | } |
3373 | ||
a52458b4 | 3374 | static int nfs4_do_setattr(struct inode *inode, const struct cred *cred, |
659bfcd6 | 3375 | struct nfs_fattr *fattr, struct iattr *sattr, |
1b00ad65 | 3376 | struct nfs_open_context *ctx, struct nfs4_label *ilabel) |
1da177e4 | 3377 | { |
3e4f6290 | 3378 | struct nfs_server *server = NFS_SERVER(inode); |
30846df0 | 3379 | __u32 bitmask[NFS4_BITMASK_SZ]; |
29b59f94 | 3380 | struct nfs4_state *state = ctx ? ctx->state : NULL; |
d9b67e1e AS |
3381 | struct nfs_setattrargs arg = { |
3382 | .fh = NFS_FH(inode), | |
3383 | .iap = sattr, | |
8487c479 | 3384 | .server = server, |
30846df0 | 3385 | .bitmask = bitmask, |
8487c479 | 3386 | .label = ilabel, |
d9b67e1e AS |
3387 | }; |
3388 | struct nfs_setattrres res = { | |
8487c479 | 3389 | .fattr = fattr, |
8487c479 | 3390 | .server = server, |
d9b67e1e | 3391 | }; |
a1d0b5ee TM |
3392 | struct nfs4_exception exception = { |
3393 | .state = state, | |
3114ea7a | 3394 | .inode = inode, |
8487c479 | 3395 | .stateid = &arg.stateid, |
a1d0b5ee | 3396 | }; |
a71029b8 | 3397 | unsigned long adjust_flags = NFS_INO_INVALID_CHANGE; |
1da177e4 | 3398 | int err; |
8487c479 | 3399 | |
720869eb TM |
3400 | if (sattr->ia_valid & (ATTR_MODE | ATTR_KILL_SUID | ATTR_KILL_SGID)) |
3401 | adjust_flags |= NFS_INO_INVALID_MODE; | |
3402 | if (sattr->ia_valid & (ATTR_UID | ATTR_GID)) | |
a71029b8 TM |
3403 | adjust_flags |= NFS_INO_INVALID_OTHER; |
3404 | ||
1da177e4 | 3405 | do { |
1b00ad65 | 3406 | nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), |
a71029b8 | 3407 | inode, adjust_flags); |
30846df0 | 3408 | |
29b59f94 | 3409 | err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx); |
451146be TM |
3410 | switch (err) { |
3411 | case -NFS4ERR_OPENMODE: | |
721ccfb7 TM |
3412 | if (!(sattr->ia_valid & ATTR_SIZE)) { |
3413 | pr_warn_once("NFSv4: server %s is incorrectly " | |
3414 | "applying open mode checks to " | |
3415 | "a SETATTR that is not " | |
3416 | "changing file size.\n", | |
3417 | server->nfs_client->cl_hostname); | |
3418 | } | |
451146be TM |
3419 | if (state && !(state->state & FMODE_WRITE)) { |
3420 | err = -EBADF; | |
3421 | if (sattr->ia_valid & ATTR_OPEN) | |
3422 | err = -EACCES; | |
3423 | goto out; | |
3424 | } | |
3425 | } | |
3426 | err = nfs4_handle_exception(server, err, &exception); | |
1da177e4 | 3427 | } while (exception.retry); |
451146be | 3428 | out: |
1da177e4 LT |
3429 | return err; |
3430 | } | |
3431 | ||
500d701f PT |
3432 | static bool |
3433 | nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task) | |
3434 | { | |
3435 | if (inode == NULL || !nfs_have_layout(inode)) | |
3436 | return false; | |
3437 | ||
3438 | return pnfs_wait_on_layoutreturn(inode, task); | |
3439 | } | |
3440 | ||
0e0cb35b TM |
3441 | /* |
3442 | * Update the seqid of an open stateid | |
3443 | */ | |
3444 | static void nfs4_sync_open_stateid(nfs4_stateid *dst, | |
3445 | struct nfs4_state *state) | |
3446 | { | |
3447 | __be32 seqid_open; | |
3448 | u32 dst_seqid; | |
3449 | int seq; | |
3450 | ||
3451 | for (;;) { | |
3452 | if (!nfs4_valid_open_stateid(state)) | |
3453 | break; | |
3454 | seq = read_seqbegin(&state->seqlock); | |
3455 | if (!nfs4_state_match_open_stateid_other(state, dst)) { | |
3456 | nfs4_stateid_copy(dst, &state->open_stateid); | |
3457 | if (read_seqretry(&state->seqlock, seq)) | |
3458 | continue; | |
3459 | break; | |
3460 | } | |
3461 | seqid_open = state->open_stateid.seqid; | |
3462 | if (read_seqretry(&state->seqlock, seq)) | |
3463 | continue; | |
3464 | ||
3465 | dst_seqid = be32_to_cpu(dst->seqid); | |
3466 | if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0) | |
3467 | dst->seqid = seqid_open; | |
3468 | break; | |
3469 | } | |
3470 | } | |
3471 | ||
3472 | /* | |
3473 | * Update the seqid of an open stateid after receiving | |
3474 | * NFS4ERR_OLD_STATEID | |
3475 | */ | |
3476 | static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst, | |
3477 | struct nfs4_state *state) | |
3478 | { | |
3479 | __be32 seqid_open; | |
3480 | u32 dst_seqid; | |
3481 | bool ret; | |
b4868b44 BC |
3482 | int seq, status = -EAGAIN; |
3483 | DEFINE_WAIT(wait); | |
0e0cb35b TM |
3484 | |
3485 | for (;;) { | |
3486 | ret = false; | |
3487 | if (!nfs4_valid_open_stateid(state)) | |
3488 | break; | |
3489 | seq = read_seqbegin(&state->seqlock); | |
3490 | if (!nfs4_state_match_open_stateid_other(state, dst)) { | |
3491 | if (read_seqretry(&state->seqlock, seq)) | |
3492 | continue; | |
3493 | break; | |
3494 | } | |
b4868b44 BC |
3495 | |
3496 | write_seqlock(&state->seqlock); | |
0e0cb35b | 3497 | seqid_open = state->open_stateid.seqid; |
0e0cb35b TM |
3498 | |
3499 | dst_seqid = be32_to_cpu(dst->seqid); | |
b4868b44 BC |
3500 | |
3501 | /* Did another OPEN bump the state's seqid? try again: */ | |
3502 | if ((s32)(be32_to_cpu(seqid_open) - dst_seqid) > 0) { | |
0e0cb35b | 3503 | dst->seqid = seqid_open; |
b4868b44 BC |
3504 | write_sequnlock(&state->seqlock); |
3505 | ret = true; | |
3506 | break; | |
3507 | } | |
3508 | ||
3509 | /* server says we're behind but we haven't seen the update yet */ | |
3510 | set_bit(NFS_STATE_CHANGE_WAIT, &state->flags); | |
3511 | prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE); | |
3512 | write_sequnlock(&state->seqlock); | |
3513 | trace_nfs4_close_stateid_update_wait(state->inode, dst, 0); | |
3514 | ||
bb002388 | 3515 | if (fatal_signal_pending(current)) |
b4868b44 BC |
3516 | status = -EINTR; |
3517 | else | |
3518 | if (schedule_timeout(5*HZ) != 0) | |
3519 | status = 0; | |
3520 | ||
3521 | finish_wait(&state->waitq, &wait); | |
3522 | ||
3523 | if (!status) | |
3524 | continue; | |
3525 | if (status == -EINTR) | |
3526 | break; | |
3527 | ||
3528 | /* we slept the whole 5 seconds, we must have lost a seqid */ | |
3529 | dst->seqid = cpu_to_be32(dst_seqid + 1); | |
0e0cb35b TM |
3530 | ret = true; |
3531 | break; | |
3532 | } | |
3533 | ||
3534 | return ret; | |
3535 | } | |
3536 | ||
1da177e4 LT |
3537 | struct nfs4_closedata { |
3538 | struct inode *inode; | |
3539 | struct nfs4_state *state; | |
3540 | struct nfs_closeargs arg; | |
3541 | struct nfs_closeres res; | |
cf805165 TM |
3542 | struct { |
3543 | struct nfs4_layoutreturn_args arg; | |
3544 | struct nfs4_layoutreturn_res res; | |
4d796d75 | 3545 | struct nfs4_xdr_opaque_data ld_private; |
cf805165 TM |
3546 | u32 roc_barrier; |
3547 | bool roc; | |
3548 | } lr; | |
516a6af6 | 3549 | struct nfs_fattr fattr; |
26e976a8 | 3550 | unsigned long timestamp; |
1da177e4 LT |
3551 | }; |
3552 | ||
963d8fe5 | 3553 | static void nfs4_free_closedata(void *data) |
9512135d | 3554 | { |
963d8fe5 TM |
3555 | struct nfs4_closedata *calldata = data; |
3556 | struct nfs4_state_owner *sp = calldata->state->owner; | |
643168c2 | 3557 | struct super_block *sb = calldata->state->inode->i_sb; |
9512135d | 3558 | |
cf805165 | 3559 | if (calldata->lr.roc) |
1c5bd76d TM |
3560 | pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res, |
3561 | calldata->res.lr_ret); | |
9512135d TM |
3562 | nfs4_put_open_state(calldata->state); |
3563 | nfs_free_seqid(calldata->arg.seqid); | |
9512135d | 3564 | nfs4_put_state_owner(sp); |
322b2b90 | 3565 | nfs_sb_deactive(sb); |
9512135d TM |
3566 | kfree(calldata); |
3567 | } | |
3568 | ||
963d8fe5 | 3569 | static void nfs4_close_done(struct rpc_task *task, void *data) |
1da177e4 | 3570 | { |
963d8fe5 | 3571 | struct nfs4_closedata *calldata = data; |
1da177e4 | 3572 | struct nfs4_state *state = calldata->state; |
1da177e4 | 3573 | struct nfs_server *server = NFS_SERVER(calldata->inode); |
412f6c4c | 3574 | nfs4_stateid *res_stateid = NULL; |
b8b8d221 TM |
3575 | struct nfs4_exception exception = { |
3576 | .state = state, | |
3577 | .inode = calldata->inode, | |
3578 | .stateid = &calldata->arg.stateid, | |
3579 | }; | |
1da177e4 | 3580 | |
14516c3a TM |
3581 | if (!nfs4_sequence_done(task, &calldata->res.seq_res)) |
3582 | return; | |
42113a75 | 3583 | trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status); |
cf805165 TM |
3584 | |
3585 | /* Handle Layoutreturn errors */ | |
078000d0 TM |
3586 | if (pnfs_roc_done(task, &calldata->arg.lr_args, &calldata->res.lr_res, |
3587 | &calldata->res.lr_ret) == -EAGAIN) | |
287a9c55 | 3588 | goto out_restart; |
cf805165 | 3589 | |
d9b67e1e | 3590 | /* hmm. we are done with the inode, and in the process of freeing |
1da177e4 LT |
3591 | * the state_owner. we keep this around to process errors |
3592 | */ | |
1da177e4 LT |
3593 | switch (task->tk_status) { |
3594 | case 0: | |
412f6c4c | 3595 | res_stateid = &calldata->res.stateid; |
26e976a8 | 3596 | renew_lease(server, calldata->timestamp); |
412f6c4c | 3597 | break; |
f07d4a31 TM |
3598 | case -NFS4ERR_ACCESS: |
3599 | if (calldata->arg.bitmask != NULL) { | |
3600 | calldata->arg.bitmask = NULL; | |
3601 | calldata->res.fattr = NULL; | |
91b30d2e | 3602 | goto out_restart; |
f07d4a31 TM |
3603 | |
3604 | } | |
3605 | break; | |
12f275cd TM |
3606 | case -NFS4ERR_OLD_STATEID: |
3607 | /* Did we race with OPEN? */ | |
0e0cb35b | 3608 | if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid, |
91b30d2e TM |
3609 | state)) |
3610 | goto out_restart; | |
12f275cd | 3611 | goto out_release; |
69794ad7 | 3612 | case -NFS4ERR_ADMIN_REVOKED: |
1da177e4 | 3613 | case -NFS4ERR_STALE_STATEID: |
26d36301 TM |
3614 | case -NFS4ERR_EXPIRED: |
3615 | nfs4_free_revoked_stateid(server, | |
3616 | &calldata->arg.stateid, | |
3617 | task->tk_msg.rpc_cred); | |
df561f66 | 3618 | fallthrough; |
9e33bed5 | 3619 | case -NFS4ERR_BAD_STATEID: |
e217e825 TM |
3620 | if (calldata->arg.fmode == 0) |
3621 | break; | |
df561f66 | 3622 | fallthrough; |
1da177e4 | 3623 | default: |
b8b8d221 TM |
3624 | task->tk_status = nfs4_async_handle_exception(task, |
3625 | server, task->tk_status, &exception); | |
3626 | if (exception.retry) | |
91b30d2e | 3627 | goto out_restart; |
1da177e4 | 3628 | } |
4a1e2feb TM |
3629 | nfs_clear_open_stateid(state, &calldata->arg.stateid, |
3630 | res_stateid, calldata->arg.fmode); | |
69794ad7 | 3631 | out_release: |
91b30d2e | 3632 | task->tk_status = 0; |
72211dbe | 3633 | nfs_release_seqid(calldata->arg.seqid); |
d8d84983 | 3634 | nfs_refresh_inode(calldata->inode, &calldata->fattr); |
86882c75 | 3635 | dprintk("%s: ret = %d\n", __func__, task->tk_status); |
91b30d2e | 3636 | return; |
91b30d2e TM |
3637 | out_restart: |
3638 | task->tk_status = 0; | |
3639 | rpc_restart_call_prepare(task); | |
3640 | goto out_release; | |
1da177e4 LT |
3641 | } |
3642 | ||
4ce70ada | 3643 | static void nfs4_close_prepare(struct rpc_task *task, void *data) |
1da177e4 | 3644 | { |
4ce70ada | 3645 | struct nfs4_closedata *calldata = data; |
9512135d | 3646 | struct nfs4_state *state = calldata->state; |
7fdab069 | 3647 | struct inode *inode = calldata->inode; |
332d1a03 | 3648 | struct nfs_server *server = NFS_SERVER(inode); |
c8bf7073 | 3649 | struct pnfs_layout_hdr *lo; |
aee7af35 | 3650 | bool is_rdonly, is_wronly, is_rdwr; |
88069f77 | 3651 | int call_close = 0; |
9512135d | 3652 | |
963d8fe5 | 3653 | if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) |
c8da19b9 | 3654 | goto out_wait; |
003707c7 | 3655 | |
88069f77 | 3656 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE]; |
4cecb76f | 3657 | spin_lock(&state->owner->so_lock); |
aee7af35 TM |
3658 | is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags); |
3659 | is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags); | |
3660 | is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags); | |
003707c7 | 3661 | /* Calculate the change in open mode */ |
cd9288ff | 3662 | calldata->arg.fmode = 0; |
e7616923 | 3663 | if (state->n_rdwr == 0) { |
cd9288ff TM |
3664 | if (state->n_rdonly == 0) |
3665 | call_close |= is_rdonly; | |
3666 | else if (is_rdonly) | |
3667 | calldata->arg.fmode |= FMODE_READ; | |
3668 | if (state->n_wronly == 0) | |
3669 | call_close |= is_wronly; | |
3670 | else if (is_wronly) | |
3671 | calldata->arg.fmode |= FMODE_WRITE; | |
e547f262 TM |
3672 | if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE)) |
3673 | call_close |= is_rdwr; | |
cd9288ff TM |
3674 | } else if (is_rdwr) |
3675 | calldata->arg.fmode |= FMODE_READ|FMODE_WRITE; | |
3676 | ||
0e0cb35b TM |
3677 | nfs4_sync_open_stateid(&calldata->arg.stateid, state); |
3678 | if (!nfs4_valid_open_stateid(state)) | |
5d422301 | 3679 | call_close = 0; |
4cecb76f | 3680 | spin_unlock(&state->owner->so_lock); |
88069f77 TM |
3681 | |
3682 | if (!call_close) { | |
963d8fe5 | 3683 | /* Note: exit _without_ calling nfs4_close_done */ |
c8da19b9 | 3684 | goto out_no_action; |
9512135d | 3685 | } |
88069f77 | 3686 | |
1c5bd76d | 3687 | if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) { |
500d701f PT |
3688 | nfs_release_seqid(calldata->arg.seqid); |
3689 | goto out_wait; | |
3690 | } | |
3691 | ||
c8bf7073 TM |
3692 | lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL; |
3693 | if (lo && !pnfs_layout_is_valid(lo)) { | |
3694 | calldata->arg.lr_args = NULL; | |
3695 | calldata->res.lr_res = NULL; | |
3696 | } | |
3697 | ||
9413a1a1 | 3698 | if (calldata->arg.fmode == 0) |
88069f77 | 3699 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE]; |
3ecefc92 | 3700 | |
9413a1a1 | 3701 | if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) { |
3ecefc92 | 3702 | /* Close-to-open cache consistency revalidation */ |
76bd5c01 | 3703 | if (!nfs4_have_delegation(inode, FMODE_READ)) { |
332d1a03 TM |
3704 | nfs4_bitmask_set(calldata->arg.bitmask_store, |
3705 | server->cache_consistency_bitmask, | |
85847280 | 3706 | inode, 0); |
332d1a03 | 3707 | calldata->arg.bitmask = calldata->arg.bitmask_store; |
76bd5c01 | 3708 | } else |
3ecefc92 TM |
3709 | calldata->arg.bitmask = NULL; |
3710 | } | |
3c13cb5b | 3711 | |
6ae37339 TM |
3712 | calldata->arg.share_access = |
3713 | nfs4_map_atomic_open_share(NFS_SERVER(inode), | |
3714 | calldata->arg.fmode, 0); | |
88069f77 | 3715 | |
d8d84983 TM |
3716 | if (calldata->res.fattr == NULL) |
3717 | calldata->arg.bitmask = NULL; | |
3718 | else if (calldata->arg.bitmask == NULL) | |
3719 | calldata->res.fattr = NULL; | |
26e976a8 | 3720 | calldata->timestamp = jiffies; |
42e1cca7 | 3721 | if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client, |
9d12b216 TM |
3722 | &calldata->arg.seq_args, |
3723 | &calldata->res.seq_res, | |
2240a9e2 TM |
3724 | task) != 0) |
3725 | nfs_release_seqid(calldata->arg.seqid); | |
c8da19b9 TM |
3726 | return; |
3727 | out_no_action: | |
3728 | task->tk_action = NULL; | |
3729 | out_wait: | |
3730 | nfs4_sequence_done(task, &calldata->res.seq_res); | |
1da177e4 LT |
3731 | } |
3732 | ||
963d8fe5 | 3733 | static const struct rpc_call_ops nfs4_close_ops = { |
4ce70ada | 3734 | .rpc_call_prepare = nfs4_close_prepare, |
963d8fe5 TM |
3735 | .rpc_call_done = nfs4_close_done, |
3736 | .rpc_release = nfs4_free_closedata, | |
3737 | }; | |
3738 | ||
1da177e4 LT |
3739 | /* |
3740 | * It is possible for data to be read/written from a mem-mapped file | |
3741 | * after the sys_close call (which hits the vfs layer as a flush). | |
3742 | * This means that we can't safely call nfsv4 close on a file until | |
3743 | * the inode is cleared. This in turn means that we are not good | |
3744 | * NFSv4 citizens - we do not indicate to the server to update the file's | |
3745 | * share state even when we are done with one of the three share | |
3746 | * stateid's in the inode. | |
3747 | * | |
3748 | * NOTE: Caller must be holding the sp->so_owner semaphore! | |
3749 | */ | |
1f7977c1 | 3750 | int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait) |
1da177e4 | 3751 | { |
4a35bd41 | 3752 | struct nfs_server *server = NFS_SERVER(state->inode); |
63f5f796 | 3753 | struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); |
1da177e4 | 3754 | struct nfs4_closedata *calldata; |
b39e625b TM |
3755 | struct nfs4_state_owner *sp = state->owner; |
3756 | struct rpc_task *task; | |
5138fde0 TM |
3757 | struct rpc_message msg = { |
3758 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE], | |
3759 | .rpc_cred = state->owner->so_cred, | |
3760 | }; | |
c970aa85 TM |
3761 | struct rpc_task_setup task_setup_data = { |
3762 | .rpc_client = server->client, | |
5138fde0 | 3763 | .rpc_message = &msg, |
c970aa85 | 3764 | .callback_ops = &nfs4_close_ops, |
101070ca | 3765 | .workqueue = nfsiod_workqueue, |
61296507 | 3766 | .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, |
c970aa85 | 3767 | }; |
9512135d | 3768 | int status = -ENOMEM; |
1da177e4 | 3769 | |
118f09ed | 3770 | if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE)) |
85e39fee OK |
3771 | task_setup_data.flags |= RPC_TASK_MOVEABLE; |
3772 | ||
fa940720 WAA |
3773 | nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP, |
3774 | &task_setup_data.rpc_client, &msg); | |
3775 | ||
8535b2be | 3776 | calldata = kzalloc(sizeof(*calldata), gfp_mask); |
1da177e4 | 3777 | if (calldata == NULL) |
9512135d | 3778 | goto out; |
fba83f34 | 3779 | nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0); |
4a35bd41 | 3780 | calldata->inode = state->inode; |
1da177e4 | 3781 | calldata->state = state; |
4a35bd41 | 3782 | calldata->arg.fh = NFS_FH(state->inode); |
c82bac6f TM |
3783 | if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state)) |
3784 | goto out_free_calldata; | |
1da177e4 | 3785 | /* Serialization for the sequence id */ |
63f5f796 TM |
3786 | alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; |
3787 | calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask); | |
badc76dd | 3788 | if (IS_ERR(calldata->arg.seqid)) |
9512135d | 3789 | goto out_free_calldata; |
d8d84983 | 3790 | nfs_fattr_init(&calldata->fattr); |
dc0b027d | 3791 | calldata->arg.fmode = 0; |
4d796d75 | 3792 | calldata->lr.arg.ld_private = &calldata->lr.ld_private; |
516a6af6 | 3793 | calldata->res.fattr = &calldata->fattr; |
c1d51931 | 3794 | calldata->res.seqid = calldata->arg.seqid; |
516a6af6 | 3795 | calldata->res.server = server; |
cf805165 | 3796 | calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT; |
1c5bd76d TM |
3797 | calldata->lr.roc = pnfs_roc(state->inode, |
3798 | &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred); | |
3799 | if (calldata->lr.roc) { | |
3800 | calldata->arg.lr_args = &calldata->lr.arg; | |
3801 | calldata->res.lr_res = &calldata->lr.res; | |
3802 | } | |
643168c2 | 3803 | nfs_sb_active(calldata->inode->i_sb); |
9512135d | 3804 | |
1174dd1f TM |
3805 | msg.rpc_argp = &calldata->arg; |
3806 | msg.rpc_resp = &calldata->res; | |
c970aa85 TM |
3807 | task_setup_data.callback_data = calldata; |
3808 | task = rpc_run_task(&task_setup_data); | |
b39e625b TM |
3809 | if (IS_ERR(task)) |
3810 | return PTR_ERR(task); | |
a49c3c77 TM |
3811 | status = 0; |
3812 | if (wait) | |
3813 | status = rpc_wait_for_completion_task(task); | |
b39e625b | 3814 | rpc_put_task(task); |
a49c3c77 | 3815 | return status; |
9512135d TM |
3816 | out_free_calldata: |
3817 | kfree(calldata); | |
3818 | out: | |
b39e625b TM |
3819 | nfs4_put_open_state(state); |
3820 | nfs4_put_state_owner(sp); | |
9512135d | 3821 | return status; |
1da177e4 LT |
3822 | } |
3823 | ||
2b484297 | 3824 | static struct inode * |
5bc2afc2 TM |
3825 | nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, |
3826 | int open_flags, struct iattr *attr, int *opened) | |
1da177e4 | 3827 | { |
1da177e4 | 3828 | struct nfs4_state *state; |
c528f70f | 3829 | struct nfs4_label l, *label; |
aa9c2669 DQ |
3830 | |
3831 | label = nfs4_label_init_security(dir, ctx->dentry, attr, &l); | |
1da177e4 | 3832 | |
565277f6 | 3833 | /* Protect against concurrent sillydeletes */ |
5bc2afc2 | 3834 | state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened); |
aa9c2669 DQ |
3835 | |
3836 | nfs4_label_release_security(label); | |
3837 | ||
f46e0bd3 TM |
3838 | if (IS_ERR(state)) |
3839 | return ERR_CAST(state); | |
275bb307 | 3840 | return state->inode; |
1da177e4 LT |
3841 | } |
3842 | ||
1185a552 | 3843 | static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync) |
7fe5c398 TM |
3844 | { |
3845 | if (ctx->state == NULL) | |
3846 | return; | |
3847 | if (is_sync) | |
1bf85d8c | 3848 | nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx)); |
7fe5c398 | 3849 | else |
1bf85d8c | 3850 | nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx)); |
7fe5c398 | 3851 | } |
1da177e4 | 3852 | |
b944dba3 TM |
3853 | #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL) |
3854 | #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL) | |
b78ef845 | 3855 | #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL) |
b944dba3 | 3856 | |
1da177e4 LT |
3857 | static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) |
3858 | { | |
8c61282f | 3859 | u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion; |
43652ad5 BH |
3860 | struct nfs4_server_caps_arg args = { |
3861 | .fhandle = fhandle, | |
8c61282f | 3862 | .bitmask = bitmask, |
43652ad5 | 3863 | }; |
1da177e4 LT |
3864 | struct nfs4_server_caps_res res = {}; |
3865 | struct rpc_message msg = { | |
3866 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS], | |
43652ad5 | 3867 | .rpc_argp = &args, |
1da177e4 LT |
3868 | .rpc_resp = &res, |
3869 | }; | |
3870 | int status; | |
f4b23de3 | 3871 | int i; |
1da177e4 | 3872 | |
8c61282f KM |
3873 | bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS | |
3874 | FATTR4_WORD0_FH_EXPIRE_TYPE | | |
3875 | FATTR4_WORD0_LINK_SUPPORT | | |
3876 | FATTR4_WORD0_SYMLINK_SUPPORT | | |
1ab5be4a TM |
3877 | FATTR4_WORD0_ACLSUPPORT | |
3878 | FATTR4_WORD0_CASE_INSENSITIVE | | |
3879 | FATTR4_WORD0_CASE_PRESERVING; | |
8c61282f KM |
3880 | if (minorversion) |
3881 | bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT; | |
3882 | ||
7c513058 | 3883 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 | 3884 | if (status == 0) { |
b944dba3 | 3885 | /* Sanity check the server answers */ |
8c61282f | 3886 | switch (minorversion) { |
b944dba3 TM |
3887 | case 0: |
3888 | res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK; | |
3889 | res.attr_bitmask[2] = 0; | |
3890 | break; | |
3891 | case 1: | |
3892 | res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK; | |
3893 | break; | |
3894 | case 2: | |
3895 | res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK; | |
3896 | } | |
1da177e4 | 3897 | memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask)); |
ce62b114 TM |
3898 | server->caps &= ~(NFS_CAP_ACLS | NFS_CAP_HARDLINKS | |
3899 | NFS_CAP_SYMLINKS| NFS_CAP_SECURITY_LABEL); | |
3900 | server->fattr_valid = NFS_ATTR_FATTR_V4; | |
7dd7d959 MN |
3901 | if (res.attr_bitmask[0] & FATTR4_WORD0_ACL && |
3902 | res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL) | |
1da177e4 LT |
3903 | server->caps |= NFS_CAP_ACLS; |
3904 | if (res.has_links != 0) | |
3905 | server->caps |= NFS_CAP_HARDLINKS; | |
3906 | if (res.has_symlinks != 0) | |
3907 | server->caps |= NFS_CAP_SYMLINKS; | |
1ab5be4a TM |
3908 | if (res.case_insensitive) |
3909 | server->caps |= NFS_CAP_CASE_INSENSITIVE; | |
3910 | if (res.case_preserving) | |
3911 | server->caps |= NFS_CAP_CASE_PRESERVING; | |
0b4f132b SM |
3912 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
3913 | if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL) | |
3914 | server->caps |= NFS_CAP_SECURITY_LABEL; | |
3915 | #endif | |
8a59bb93 OK |
3916 | if (res.attr_bitmask[0] & FATTR4_WORD0_FS_LOCATIONS) |
3917 | server->caps |= NFS_CAP_FS_LOCATIONS; | |
ce62b114 TM |
3918 | if (!(res.attr_bitmask[0] & FATTR4_WORD0_FILEID)) |
3919 | server->fattr_valid &= ~NFS_ATTR_FATTR_FILEID; | |
3920 | if (!(res.attr_bitmask[1] & FATTR4_WORD1_MODE)) | |
3921 | server->fattr_valid &= ~NFS_ATTR_FATTR_MODE; | |
3922 | if (!(res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)) | |
3923 | server->fattr_valid &= ~NFS_ATTR_FATTR_NLINK; | |
3924 | if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER)) | |
3925 | server->fattr_valid &= ~(NFS_ATTR_FATTR_OWNER | | |
3926 | NFS_ATTR_FATTR_OWNER_NAME); | |
3927 | if (!(res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)) | |
3928 | server->fattr_valid &= ~(NFS_ATTR_FATTR_GROUP | | |
3929 | NFS_ATTR_FATTR_GROUP_NAME); | |
3930 | if (!(res.attr_bitmask[1] & FATTR4_WORD1_SPACE_USED)) | |
3931 | server->fattr_valid &= ~NFS_ATTR_FATTR_SPACE_USED; | |
3932 | if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)) | |
3933 | server->fattr_valid &= ~NFS_ATTR_FATTR_ATIME; | |
3934 | if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)) | |
3935 | server->fattr_valid &= ~NFS_ATTR_FATTR_CTIME; | |
3936 | if (!(res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)) | |
3937 | server->fattr_valid &= ~NFS_ATTR_FATTR_MTIME; | |
aa9c2669 DQ |
3938 | memcpy(server->attr_bitmask_nl, res.attr_bitmask, |
3939 | sizeof(server->attr_bitmask)); | |
b944dba3 | 3940 | server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL; |
62ab460c | 3941 | |
a65318bf TM |
3942 | memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask)); |
3943 | server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE; | |
3944 | server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY; | |
b944dba3 | 3945 | server->cache_consistency_bitmask[2] = 0; |
f4b23de3 TM |
3946 | |
3947 | /* Avoid a regression due to buggy server */ | |
3948 | for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++) | |
3949 | res.exclcreat_bitmask[i] &= res.attr_bitmask[i]; | |
8c61282f KM |
3950 | memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask, |
3951 | sizeof(server->exclcreat_bitmask)); | |
f4b23de3 | 3952 | |
1da177e4 | 3953 | server->acl_bitmask = res.acl_bitmask; |
264e6351 | 3954 | server->fh_expire_type = res.fh_expire_type; |
1da177e4 | 3955 | } |
cccef3b9 | 3956 | |
1da177e4 LT |
3957 | return status; |
3958 | } | |
3959 | ||
55a97593 | 3960 | int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) |
1da177e4 | 3961 | { |
0688e64b TM |
3962 | struct nfs4_exception exception = { |
3963 | .interruptible = true, | |
3964 | }; | |
1da177e4 | 3965 | int err; |
01dde76e AS |
3966 | |
3967 | nfs4_server_set_init_caps(server); | |
1da177e4 LT |
3968 | do { |
3969 | err = nfs4_handle_exception(server, | |
3970 | _nfs4_server_capabilities(server, fhandle), | |
3971 | &exception); | |
3972 | } while (exception.retry); | |
3973 | return err; | |
3974 | } | |
3975 | ||
4ca9f31a OK |
3976 | static void test_fs_location_for_trunking(struct nfs4_fs_location *location, |
3977 | struct nfs_client *clp, | |
3978 | struct nfs_server *server) | |
3979 | { | |
3980 | int i; | |
3981 | ||
3982 | for (i = 0; i < location->nservers; i++) { | |
3983 | struct nfs4_string *srv_loc = &location->servers[i]; | |
cf0d7e7f | 3984 | struct sockaddr_storage addr; |
4ca9f31a OK |
3985 | size_t addrlen; |
3986 | struct xprt_create xprt_args = { | |
3987 | .ident = 0, | |
3988 | .net = clp->cl_net, | |
3989 | }; | |
3990 | struct nfs4_add_xprt_data xprtdata = { | |
3991 | .clp = clp, | |
3992 | }; | |
3993 | struct rpc_add_xprt_test rpcdata = { | |
3994 | .add_xprt_test = clp->cl_mvops->session_trunk, | |
3995 | .data = &xprtdata, | |
3996 | }; | |
3997 | char *servername = NULL; | |
3998 | ||
3999 | if (!srv_loc->len) | |
4000 | continue; | |
4001 | ||
4002 | addrlen = nfs_parse_server_name(srv_loc->data, srv_loc->len, | |
4003 | &addr, sizeof(addr), | |
4004 | clp->cl_net, server->port); | |
4005 | if (!addrlen) | |
4006 | return; | |
cf0d7e7f | 4007 | xprt_args.dstaddr = (struct sockaddr *)&addr; |
4ca9f31a OK |
4008 | xprt_args.addrlen = addrlen; |
4009 | servername = kmalloc(srv_loc->len + 1, GFP_KERNEL); | |
4010 | if (!servername) | |
4011 | return; | |
4012 | memcpy(servername, srv_loc->data, srv_loc->len); | |
4013 | servername[srv_loc->len] = '\0'; | |
4014 | xprt_args.servername = servername; | |
4015 | ||
4016 | xprtdata.cred = nfs4_get_clid_cred(clp); | |
4017 | rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args, | |
4018 | rpc_clnt_setup_test_and_add_xprt, | |
4019 | &rpcdata); | |
4020 | if (xprtdata.cred) | |
4021 | put_cred(xprtdata.cred); | |
4022 | kfree(servername); | |
4023 | } | |
4024 | } | |
4025 | ||
1976b2b3 OK |
4026 | static int _nfs4_discover_trunking(struct nfs_server *server, |
4027 | struct nfs_fh *fhandle) | |
4028 | { | |
4029 | struct nfs4_fs_locations *locations = NULL; | |
4030 | struct page *page; | |
4031 | const struct cred *cred; | |
4032 | struct nfs_client *clp = server->nfs_client; | |
4033 | const struct nfs4_state_maintenance_ops *ops = | |
4034 | clp->cl_mvops->state_renewal_ops; | |
4ca9f31a | 4035 | int status = -ENOMEM, i; |
1976b2b3 OK |
4036 | |
4037 | cred = ops->get_state_renewal_cred(clp); | |
4038 | if (cred == NULL) { | |
4039 | cred = nfs4_get_clid_cred(clp); | |
4040 | if (cred == NULL) | |
4041 | return -ENOKEY; | |
4042 | } | |
4043 | ||
4044 | page = alloc_page(GFP_KERNEL); | |
4f40a5b5 | 4045 | if (!page) |
e83458fc | 4046 | goto out_put_cred; |
1976b2b3 | 4047 | locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); |
4f40a5b5 SM |
4048 | if (!locations) |
4049 | goto out_free; | |
4050 | locations->fattr = nfs_alloc_fattr(); | |
4051 | if (!locations->fattr) | |
4052 | goto out_free_2; | |
1976b2b3 OK |
4053 | |
4054 | status = nfs4_proc_get_locations(server, fhandle, locations, page, | |
4055 | cred); | |
4056 | if (status) | |
4f40a5b5 | 4057 | goto out_free_3; |
4ca9f31a OK |
4058 | |
4059 | for (i = 0; i < locations->nlocations; i++) | |
4060 | test_fs_location_for_trunking(&locations->locations[i], clp, | |
4061 | server); | |
4f40a5b5 SM |
4062 | out_free_3: |
4063 | kfree(locations->fattr); | |
4064 | out_free_2: | |
1976b2b3 | 4065 | kfree(locations); |
4f40a5b5 SM |
4066 | out_free: |
4067 | __free_page(page); | |
e83458fc TM |
4068 | out_put_cred: |
4069 | put_cred(cred); | |
1976b2b3 OK |
4070 | return status; |
4071 | } | |
4072 | ||
4073 | static int nfs4_discover_trunking(struct nfs_server *server, | |
4074 | struct nfs_fh *fhandle) | |
4075 | { | |
4076 | struct nfs4_exception exception = { | |
4077 | .interruptible = true, | |
4078 | }; | |
4079 | struct nfs_client *clp = server->nfs_client; | |
4080 | int err = 0; | |
4081 | ||
4082 | if (!nfs4_has_session(clp)) | |
4083 | goto out; | |
4084 | do { | |
4085 | err = nfs4_handle_exception(server, | |
4086 | _nfs4_discover_trunking(server, fhandle), | |
4087 | &exception); | |
4088 | } while (exception.retry); | |
4089 | out: | |
4090 | return err; | |
4091 | } | |
4092 | ||
1da177e4 LT |
4093 | static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, |
4094 | struct nfs_fsinfo *info) | |
4095 | { | |
aa9c2669 | 4096 | u32 bitmask[3]; |
1da177e4 | 4097 | struct nfs4_lookup_root_arg args = { |
aa9c2669 | 4098 | .bitmask = bitmask, |
1da177e4 LT |
4099 | }; |
4100 | struct nfs4_lookup_res res = { | |
4101 | .server = server, | |
0e574af1 | 4102 | .fattr = info->fattr, |
1da177e4 LT |
4103 | .fh = fhandle, |
4104 | }; | |
4105 | struct rpc_message msg = { | |
4106 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT], | |
4107 | .rpc_argp = &args, | |
4108 | .rpc_resp = &res, | |
4109 | }; | |
008f55d0 | 4110 | |
aa9c2669 DQ |
4111 | bitmask[0] = nfs4_fattr_bitmap[0]; |
4112 | bitmask[1] = nfs4_fattr_bitmap[1]; | |
4113 | /* | |
4114 | * Process the label in the upcoming getfattr | |
4115 | */ | |
4116 | bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL; | |
4117 | ||
0e574af1 | 4118 | nfs_fattr_init(info->fattr); |
7c513058 | 4119 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
4120 | } |
4121 | ||
4122 | static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, | |
4123 | struct nfs_fsinfo *info) | |
4124 | { | |
0688e64b TM |
4125 | struct nfs4_exception exception = { |
4126 | .interruptible = true, | |
4127 | }; | |
1da177e4 LT |
4128 | int err; |
4129 | do { | |
fb8a5ba8 | 4130 | err = _nfs4_lookup_root(server, fhandle, info); |
b5f875a9 | 4131 | trace_nfs4_lookup_root(server, fhandle, info->fattr, err); |
fb8a5ba8 BS |
4132 | switch (err) { |
4133 | case 0: | |
4134 | case -NFS4ERR_WRONGSEC: | |
05e9cfb4 | 4135 | goto out; |
fb8a5ba8 BS |
4136 | default: |
4137 | err = nfs4_handle_exception(server, err, &exception); | |
4138 | } | |
1da177e4 | 4139 | } while (exception.retry); |
05e9cfb4 | 4140 | out: |
1da177e4 LT |
4141 | return err; |
4142 | } | |
4143 | ||
8f70e95f BS |
4144 | static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, |
4145 | struct nfs_fsinfo *info, rpc_authflavor_t flavor) | |
4146 | { | |
c2190661 TM |
4147 | struct rpc_auth_create_args auth_args = { |
4148 | .pseudoflavor = flavor, | |
4149 | }; | |
8f70e95f | 4150 | struct rpc_auth *auth; |
8f70e95f | 4151 | |
c2190661 | 4152 | auth = rpcauth_create(&auth_args, server->client); |
9df1336c AS |
4153 | if (IS_ERR(auth)) |
4154 | return -EACCES; | |
4155 | return nfs4_lookup_root(server, fhandle, info); | |
8f70e95f BS |
4156 | } |
4157 | ||
9a744ba3 CL |
4158 | /* |
4159 | * Retry pseudoroot lookup with various security flavors. We do this when: | |
4160 | * | |
4161 | * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC | |
4162 | * NFSv4.1: the server does not support the SECINFO_NO_NAME operation | |
4163 | * | |
4164 | * Returns zero on success, or a negative NFS4ERR value, or a | |
4165 | * negative errno value. | |
4166 | */ | |
801a16dc | 4167 | static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, |
54ceac45 | 4168 | struct nfs_fsinfo *info) |
1da177e4 | 4169 | { |
9a744ba3 CL |
4170 | /* Per 3530bis 15.33.5 */ |
4171 | static const rpc_authflavor_t flav_array[] = { | |
4172 | RPC_AUTH_GSS_KRB5P, | |
4173 | RPC_AUTH_GSS_KRB5I, | |
4174 | RPC_AUTH_GSS_KRB5, | |
c4eafe11 | 4175 | RPC_AUTH_UNIX, /* courtesy */ |
9a744ba3 CL |
4176 | RPC_AUTH_NULL, |
4177 | }; | |
4178 | int status = -EPERM; | |
4179 | size_t i; | |
6a1a1e34 | 4180 | |
4d4b69dd WAA |
4181 | if (server->auth_info.flavor_len > 0) { |
4182 | /* try each flavor specified by user */ | |
4183 | for (i = 0; i < server->auth_info.flavor_len; i++) { | |
4184 | status = nfs4_lookup_root_sec(server, fhandle, info, | |
4185 | server->auth_info.flavors[i]); | |
4186 | if (status == -NFS4ERR_WRONGSEC || status == -EACCES) | |
4187 | continue; | |
4188 | break; | |
4189 | } | |
4190 | } else { | |
4191 | /* no flavors specified by user, try default list */ | |
4192 | for (i = 0; i < ARRAY_SIZE(flav_array); i++) { | |
4193 | status = nfs4_lookup_root_sec(server, fhandle, info, | |
4194 | flav_array[i]); | |
4195 | if (status == -NFS4ERR_WRONGSEC || status == -EACCES) | |
4196 | continue; | |
4197 | break; | |
4198 | } | |
8f70e95f | 4199 | } |
9a744ba3 | 4200 | |
fb8a5ba8 | 4201 | /* |
d3787af2 | 4202 | * -EACCES could mean that the user doesn't have correct permissions |
fb8a5ba8 BS |
4203 | * to access the mount. It could also mean that we tried to mount |
4204 | * with a gss auth flavor, but rpc.gssd isn't running. Either way, | |
4205 | * existing mount programs don't handle -EACCES very well so it should | |
4206 | * be mapped to -EPERM instead. | |
4207 | */ | |
4208 | if (status == -EACCES) | |
4209 | status = -EPERM; | |
801a16dc BS |
4210 | return status; |
4211 | } | |
4212 | ||
2ed4b95b CL |
4213 | /** |
4214 | * nfs4_proc_get_rootfh - get file handle for server's pseudoroot | |
4215 | * @server: initialized nfs_server handle | |
4216 | * @fhandle: we fill in the pseudo-fs root file handle | |
4217 | * @info: we fill in an FSINFO struct | |
5e6b1990 | 4218 | * @auth_probe: probe the auth flavours |
2ed4b95b CL |
4219 | * |
4220 | * Returns zero on success, or a negative errno. | |
801a16dc | 4221 | */ |
3028eb2b | 4222 | int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle, |
5e6b1990 TM |
4223 | struct nfs_fsinfo *info, |
4224 | bool auth_probe) | |
801a16dc | 4225 | { |
c7757074 | 4226 | int status = 0; |
2ed4b95b | 4227 | |
c7757074 | 4228 | if (!auth_probe) |
5e6b1990 | 4229 | status = nfs4_lookup_root(server, fhandle, info); |
c7757074 AP |
4230 | |
4231 | if (auth_probe || status == NFS4ERR_WRONGSEC) | |
698c937b TM |
4232 | status = server->nfs_client->cl_mvops->find_root_sec(server, |
4233 | fhandle, info); | |
2ed4b95b | 4234 | |
1da177e4 LT |
4235 | if (status == 0) |
4236 | status = nfs4_server_capabilities(server, fhandle); | |
4237 | if (status == 0) | |
4238 | status = nfs4_do_fsinfo(server, fhandle, info); | |
2ed4b95b | 4239 | |
c12e87f4 | 4240 | return nfs4_map_errors(status); |
1da177e4 LT |
4241 | } |
4242 | ||
bae36241 BS |
4243 | static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh, |
4244 | struct nfs_fsinfo *info) | |
4245 | { | |
4246 | int error; | |
4247 | struct nfs_fattr *fattr = info->fattr; | |
4248 | ||
4249 | error = nfs4_server_capabilities(server, mntfh); | |
4250 | if (error < 0) { | |
4251 | dprintk("nfs4_get_root: getcaps error = %d\n", -error); | |
4252 | return error; | |
4253 | } | |
4254 | ||
2ef61e0e | 4255 | error = nfs4_proc_getattr(server, mntfh, fattr, NULL); |
bae36241 BS |
4256 | if (error < 0) { |
4257 | dprintk("nfs4_get_root: getattr error = %d\n", -error); | |
779df6a5 | 4258 | goto out; |
bae36241 BS |
4259 | } |
4260 | ||
4261 | if (fattr->valid & NFS_ATTR_FATTR_FSID && | |
4262 | !nfs_fsid_equal(&server->fsid, &fattr->fsid)) | |
4263 | memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid)); | |
4264 | ||
779df6a5 | 4265 | out: |
bae36241 BS |
4266 | return error; |
4267 | } | |
4268 | ||
6b97fd3d MN |
4269 | /* |
4270 | * Get locations and (maybe) other attributes of a referral. | |
4271 | * Note that we'll actually follow the referral later when | |
4272 | * we detect fsid mismatch in inode revalidation | |
4273 | */ | |
f05d147f BS |
4274 | static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir, |
4275 | const struct qstr *name, struct nfs_fattr *fattr, | |
4276 | struct nfs_fh *fhandle) | |
6b97fd3d MN |
4277 | { |
4278 | int status = -ENOMEM; | |
4279 | struct page *page = NULL; | |
4280 | struct nfs4_fs_locations *locations = NULL; | |
6b97fd3d MN |
4281 | |
4282 | page = alloc_page(GFP_KERNEL); | |
4283 | if (page == NULL) | |
4284 | goto out; | |
4285 | locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); | |
4286 | if (locations == NULL) | |
4287 | goto out; | |
4288 | ||
c3ed2227 BC |
4289 | locations->fattr = fattr; |
4290 | ||
f05d147f | 4291 | status = nfs4_proc_fs_locations(client, dir, name, locations, page); |
6b97fd3d MN |
4292 | if (status != 0) |
4293 | goto out; | |
519ae255 CL |
4294 | |
4295 | /* | |
4296 | * If the fsid didn't change, this is a migration event, not a | |
4297 | * referral. Cause us to drop into the exception handler, which | |
4298 | * will kick off migration recovery. | |
4299 | */ | |
c3ed2227 | 4300 | if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &fattr->fsid)) { |
533eb461 AA |
4301 | dprintk("%s: server did not return a different fsid for" |
4302 | " a referral at %s\n", __func__, name->name); | |
519ae255 | 4303 | status = -NFS4ERR_MOVED; |
6b97fd3d MN |
4304 | goto out; |
4305 | } | |
533eb461 | 4306 | /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */ |
c3ed2227 | 4307 | nfs_fixup_referral_attributes(fattr); |
6b97fd3d MN |
4308 | memset(fhandle, 0, sizeof(struct nfs_fh)); |
4309 | out: | |
4310 | if (page) | |
4311 | __free_page(page); | |
5d7ca35a | 4312 | kfree(locations); |
6b97fd3d MN |
4313 | return status; |
4314 | } | |
4315 | ||
1775fd3e | 4316 | static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, |
2ef61e0e | 4317 | struct nfs_fattr *fattr, struct inode *inode) |
1da177e4 | 4318 | { |
771734f2 | 4319 | __u32 bitmask[NFS4_BITMASK_SZ]; |
1da177e4 LT |
4320 | struct nfs4_getattr_arg args = { |
4321 | .fh = fhandle, | |
771734f2 | 4322 | .bitmask = bitmask, |
1da177e4 LT |
4323 | }; |
4324 | struct nfs4_getattr_res res = { | |
4325 | .fattr = fattr, | |
4326 | .server = server, | |
4327 | }; | |
4328 | struct rpc_message msg = { | |
4329 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], | |
4330 | .rpc_argp = &args, | |
4331 | .rpc_resp = &res, | |
4332 | }; | |
c74dfe97 TM |
4333 | unsigned short task_flags = 0; |
4334 | ||
85e39fee OK |
4335 | if (nfs4_has_session(server->nfs_client)) |
4336 | task_flags = RPC_TASK_MOVEABLE; | |
4337 | ||
c74dfe97 TM |
4338 | /* Is this is an attribute revalidation, subject to softreval? */ |
4339 | if (inode && (server->flags & NFS_MOUNT_SOFTREVAL)) | |
4340 | task_flags |= RPC_TASK_TIMEOUT; | |
aa9c2669 | 4341 | |
2ef61e0e | 4342 | nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, fattr->label), inode, 0); |
0e574af1 | 4343 | nfs_fattr_init(fattr); |
c74dfe97 TM |
4344 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0); |
4345 | return nfs4_do_call_sync(server->client, server, &msg, | |
4346 | &args.seq_args, &res.seq_res, task_flags); | |
1da177e4 LT |
4347 | } |
4348 | ||
ec4b0925 | 4349 | int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, |
2ef61e0e | 4350 | struct nfs_fattr *fattr, struct inode *inode) |
1da177e4 | 4351 | { |
0688e64b TM |
4352 | struct nfs4_exception exception = { |
4353 | .interruptible = true, | |
4354 | }; | |
1da177e4 LT |
4355 | int err; |
4356 | do { | |
2ef61e0e | 4357 | err = _nfs4_proc_getattr(server, fhandle, fattr, inode); |
b5f875a9 TM |
4358 | trace_nfs4_getattr(server, fhandle, fattr, err); |
4359 | err = nfs4_handle_exception(server, err, | |
1da177e4 LT |
4360 | &exception); |
4361 | } while (exception.retry); | |
4362 | return err; | |
4363 | } | |
4364 | ||
4365 | /* | |
4366 | * The file is not closed if it is opened due to the a request to change | |
4367 | * the size of the file. The open call will not be needed once the | |
4368 | * VFS layer lookup-intents are implemented. | |
4369 | * | |
4370 | * Close is called when the inode is destroyed. | |
4371 | * If we haven't opened the file for O_WRONLY, we | |
4372 | * need to in the size_change case to obtain a stateid. | |
4373 | * | |
4374 | * Got race? | |
4375 | * Because OPEN is always done by name in nfsv4, it is | |
4376 | * possible that we opened a different file by the same | |
4377 | * name. We can recognize this race condition, but we | |
4378 | * can't do anything about it besides returning an error. | |
4379 | * | |
4380 | * This will be fixed with VFS changes (lookup-intent). | |
4381 | */ | |
4382 | static int | |
4383 | nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr, | |
4384 | struct iattr *sattr) | |
4385 | { | |
2b0143b5 | 4386 | struct inode *inode = d_inode(dentry); |
a52458b4 | 4387 | const struct cred *cred = NULL; |
29b59f94 | 4388 | struct nfs_open_context *ctx = NULL; |
1da177e4 LT |
4389 | int status; |
4390 | ||
88ac815c PT |
4391 | if (pnfs_ld_layoutret_on_setattr(inode) && |
4392 | sattr->ia_valid & ATTR_SIZE && | |
4393 | sattr->ia_size < i_size_read(inode)) | |
24028672 | 4394 | pnfs_commit_and_return_layout(inode); |
8a1636c4 | 4395 | |
0e574af1 | 4396 | nfs_fattr_init(fattr); |
1da177e4 | 4397 | |
2669940d AA |
4398 | /* Deal with open(O_TRUNC) */ |
4399 | if (sattr->ia_valid & ATTR_OPEN) | |
cc7936f9 | 4400 | sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME); |
2669940d AA |
4401 | |
4402 | /* Optimization: if the end result is no change, don't RPC */ | |
cc7936f9 | 4403 | if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0) |
2669940d AA |
4404 | return 0; |
4405 | ||
d530838b | 4406 | /* Search for an existing open(O_WRITE) file */ |
659bfcd6 | 4407 | if (sattr->ia_valid & ATTR_FILE) { |
659bfcd6 TM |
4408 | |
4409 | ctx = nfs_file_open_context(sattr->ia_file); | |
29b59f94 | 4410 | if (ctx) |
504e5189 | 4411 | cred = ctx->cred; |
659bfcd6 | 4412 | } |
08e9eac4 | 4413 | |
199366f0 TM |
4414 | /* Return any delegations if we're going to change ACLs */ |
4415 | if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) | |
c01d3645 | 4416 | nfs4_inode_make_writeable(inode); |
199366f0 | 4417 | |
1b00ad65 | 4418 | status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL); |
aa9c2669 | 4419 | if (status == 0) { |
f044636d | 4420 | nfs_setattr_update_inode(inode, sattr, fattr); |
dd225cb3 | 4421 | nfs_setsecurity(inode, fattr); |
aa9c2669 | 4422 | } |
1da177e4 LT |
4423 | return status; |
4424 | } | |
4425 | ||
0c2e53f1 | 4426 | static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, |
f7b37b8b | 4427 | struct dentry *dentry, struct nfs_fh *fhandle, |
9558a007 | 4428 | struct nfs_fattr *fattr) |
2b3de441 | 4429 | { |
0c2e53f1 | 4430 | struct nfs_server *server = NFS_SERVER(dir); |
2b3de441 DH |
4431 | int status; |
4432 | struct nfs4_lookup_arg args = { | |
4433 | .bitmask = server->attr_bitmask, | |
0c2e53f1 | 4434 | .dir_fh = NFS_FH(dir), |
f7b37b8b | 4435 | .name = &dentry->d_name, |
2b3de441 DH |
4436 | }; |
4437 | struct nfs4_lookup_res res = { | |
4438 | .server = server, | |
4439 | .fattr = fattr, | |
4440 | .fh = fhandle, | |
4441 | }; | |
4442 | struct rpc_message msg = { | |
4443 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP], | |
4444 | .rpc_argp = &args, | |
4445 | .rpc_resp = &res, | |
4446 | }; | |
f7b37b8b TM |
4447 | unsigned short task_flags = 0; |
4448 | ||
118f09ed | 4449 | if (nfs_server_capable(dir, NFS_CAP_MOVEABLE)) |
85e39fee OK |
4450 | task_flags = RPC_TASK_MOVEABLE; |
4451 | ||
f7b37b8b TM |
4452 | /* Is this is an attribute revalidation, subject to softreval? */ |
4453 | if (nfs_lookup_is_soft_revalidate(dentry)) | |
4454 | task_flags |= RPC_TASK_TIMEOUT; | |
2b3de441 | 4455 | |
9558a007 | 4456 | args.bitmask = nfs4_bitmask(server, fattr->label); |
aa9c2669 | 4457 | |
2b3de441 DH |
4458 | nfs_fattr_init(fattr); |
4459 | ||
f7b37b8b TM |
4460 | dprintk("NFS call lookup %pd2\n", dentry); |
4461 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0); | |
4462 | status = nfs4_do_call_sync(clnt, server, &msg, | |
4463 | &args.seq_args, &res.seq_res, task_flags); | |
1da177e4 LT |
4464 | dprintk("NFS reply lookup: %d\n", status); |
4465 | return status; | |
4466 | } | |
4467 | ||
72de53ec | 4468 | static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr) |
7ebb9315 | 4469 | { |
7ebb9315 | 4470 | fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | |
72de53ec | 4471 | NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT; |
7ebb9315 BS |
4472 | fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; |
4473 | fattr->nlink = 2; | |
4474 | } | |
4475 | ||
72de53ec | 4476 | static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir, |
f7b37b8b | 4477 | struct dentry *dentry, struct nfs_fh *fhandle, |
9558a007 | 4478 | struct nfs_fattr *fattr) |
1da177e4 | 4479 | { |
0688e64b TM |
4480 | struct nfs4_exception exception = { |
4481 | .interruptible = true, | |
4482 | }; | |
72de53ec | 4483 | struct rpc_clnt *client = *clnt; |
f7b37b8b | 4484 | const struct qstr *name = &dentry->d_name; |
1da177e4 LT |
4485 | int err; |
4486 | do { | |
9558a007 | 4487 | err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr); |
078ea3df | 4488 | trace_nfs4_lookup(dir, name, err); |
72de53ec | 4489 | switch (err) { |
08ef7bd3 | 4490 | case -NFS4ERR_BADNAME: |
72de53ec BS |
4491 | err = -ENOENT; |
4492 | goto out; | |
0c2e53f1 | 4493 | case -NFS4ERR_MOVED: |
f05d147f | 4494 | err = nfs4_get_referral(client, dir, name, fattr, fhandle); |
c86c90c6 DM |
4495 | if (err == -NFS4ERR_MOVED) |
4496 | err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception); | |
72de53ec | 4497 | goto out; |
0c2e53f1 | 4498 | case -NFS4ERR_WRONGSEC: |
72de53ec BS |
4499 | err = -EPERM; |
4500 | if (client != *clnt) | |
4501 | goto out; | |
66b06860 | 4502 | client = nfs4_negotiate_security(client, dir, name); |
72de53ec BS |
4503 | if (IS_ERR(client)) |
4504 | return PTR_ERR(client); | |
4505 | ||
4506 | exception.retry = 1; | |
4507 | break; | |
4508 | default: | |
4509 | err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception); | |
0c2e53f1 | 4510 | } |
1da177e4 | 4511 | } while (exception.retry); |
72de53ec BS |
4512 | |
4513 | out: | |
4514 | if (err == 0) | |
4515 | *clnt = client; | |
4516 | else if (client != *clnt) | |
4517 | rpc_shutdown_client(client); | |
4518 | ||
1da177e4 LT |
4519 | return err; |
4520 | } | |
4521 | ||
f7b37b8b | 4522 | static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry, |
9558a007 | 4523 | struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
72de53ec BS |
4524 | { |
4525 | int status; | |
4526 | struct rpc_clnt *client = NFS_CLIENT(dir); | |
4527 | ||
9558a007 | 4528 | status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr); |
72de53ec BS |
4529 | if (client != NFS_CLIENT(dir)) { |
4530 | rpc_shutdown_client(client); | |
4531 | nfs_fixup_secinfo_attributes(fattr); | |
4532 | } | |
4533 | return status; | |
4534 | } | |
4535 | ||
f05d147f | 4536 | struct rpc_clnt * |
f7b37b8b | 4537 | nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry, |
f05d147f BS |
4538 | struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
4539 | { | |
b72888cb | 4540 | struct rpc_clnt *client = NFS_CLIENT(dir); |
f05d147f | 4541 | int status; |
f05d147f | 4542 | |
9558a007 | 4543 | status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr); |
b72888cb | 4544 | if (status < 0) |
f05d147f | 4545 | return ERR_PTR(status); |
b72888cb | 4546 | return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client; |
f05d147f BS |
4547 | } |
4548 | ||
5b5faaf6 | 4549 | static int _nfs4_proc_lookupp(struct inode *inode, |
ba4bc8dc | 4550 | struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
5b5faaf6 JL |
4551 | { |
4552 | struct rpc_clnt *clnt = NFS_CLIENT(inode); | |
4553 | struct nfs_server *server = NFS_SERVER(inode); | |
4554 | int status; | |
4555 | struct nfs4_lookupp_arg args = { | |
4556 | .bitmask = server->attr_bitmask, | |
4557 | .fh = NFS_FH(inode), | |
4558 | }; | |
4559 | struct nfs4_lookupp_res res = { | |
4560 | .server = server, | |
4561 | .fattr = fattr, | |
5b5faaf6 JL |
4562 | .fh = fhandle, |
4563 | }; | |
4564 | struct rpc_message msg = { | |
4565 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP], | |
4566 | .rpc_argp = &args, | |
4567 | .rpc_resp = &res, | |
4568 | }; | |
76998ebb TM |
4569 | unsigned short task_flags = 0; |
4570 | ||
4571 | if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) | |
4572 | task_flags |= RPC_TASK_TIMEOUT; | |
5b5faaf6 | 4573 | |
ba4bc8dc | 4574 | args.bitmask = nfs4_bitmask(server, fattr->label); |
5b5faaf6 JL |
4575 | |
4576 | nfs_fattr_init(fattr); | |
4577 | ||
4578 | dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino); | |
4579 | status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, | |
76998ebb | 4580 | &res.seq_res, task_flags); |
5b5faaf6 JL |
4581 | dprintk("NFS reply lookupp: %d\n", status); |
4582 | return status; | |
4583 | } | |
4584 | ||
4585 | static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle, | |
ba4bc8dc | 4586 | struct nfs_fattr *fattr) |
5b5faaf6 | 4587 | { |
0688e64b TM |
4588 | struct nfs4_exception exception = { |
4589 | .interruptible = true, | |
4590 | }; | |
5b5faaf6 JL |
4591 | int err; |
4592 | do { | |
ba4bc8dc | 4593 | err = _nfs4_proc_lookupp(inode, fhandle, fattr); |
5b5faaf6 JL |
4594 | trace_nfs4_lookupp(inode, err); |
4595 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
4596 | &exception); | |
4597 | } while (exception.retry); | |
4598 | return err; | |
4599 | } | |
4600 | ||
73fbb3fa N |
4601 | static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry, |
4602 | const struct cred *cred) | |
1da177e4 | 4603 | { |
76b32999 | 4604 | struct nfs_server *server = NFS_SERVER(inode); |
1da177e4 LT |
4605 | struct nfs4_accessargs args = { |
4606 | .fh = NFS_FH(inode), | |
1750d929 | 4607 | .access = entry->mask, |
76b32999 TM |
4608 | }; |
4609 | struct nfs4_accessres res = { | |
4610 | .server = server, | |
1da177e4 | 4611 | }; |
1da177e4 LT |
4612 | struct rpc_message msg = { |
4613 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS], | |
4614 | .rpc_argp = &args, | |
4615 | .rpc_resp = &res, | |
73fbb3fa | 4616 | .rpc_cred = cred, |
1da177e4 | 4617 | }; |
aa9c2669 | 4618 | int status = 0; |
1da177e4 | 4619 | |
7c672654 | 4620 | if (!nfs4_have_delegation(inode, FMODE_READ)) { |
8bcbe7d9 TM |
4621 | res.fattr = nfs_alloc_fattr(); |
4622 | if (res.fattr == NULL) | |
4623 | return -ENOMEM; | |
4624 | args.bitmask = server->cache_consistency_bitmask; | |
4625 | } | |
7c513058 | 4626 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 | 4627 | if (!status) { |
6168f62c | 4628 | nfs_access_set_mask(entry, res.access); |
8bcbe7d9 TM |
4629 | if (res.fattr) |
4630 | nfs_refresh_inode(inode, res.fattr); | |
1da177e4 | 4631 | } |
c407d41a | 4632 | nfs_free_fattr(res.fattr); |
1da177e4 LT |
4633 | return status; |
4634 | } | |
4635 | ||
73fbb3fa N |
4636 | static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry, |
4637 | const struct cred *cred) | |
1da177e4 | 4638 | { |
0688e64b TM |
4639 | struct nfs4_exception exception = { |
4640 | .interruptible = true, | |
4641 | }; | |
1da177e4 LT |
4642 | int err; |
4643 | do { | |
73fbb3fa | 4644 | err = _nfs4_proc_access(inode, entry, cred); |
c1578b76 TM |
4645 | trace_nfs4_access(inode, err); |
4646 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
1da177e4 LT |
4647 | &exception); |
4648 | } while (exception.retry); | |
4649 | return err; | |
4650 | } | |
4651 | ||
4652 | /* | |
4653 | * TODO: For the time being, we don't try to get any attributes | |
4654 | * along with any of the zero-copy operations READ, READDIR, | |
4655 | * READLINK, WRITE. | |
4656 | * | |
4657 | * In the case of the first three, we want to put the GETATTR | |
4658 | * after the read-type operation -- this is because it is hard | |
4659 | * to predict the length of a GETATTR response in v4, and thus | |
4660 | * align the READ data correctly. This means that the GETATTR | |
4661 | * may end up partially falling into the page cache, and we should | |
4662 | * shift it into the 'tail' of the xdr_buf before processing. | |
4663 | * To do this efficiently, we need to know the total length | |
4664 | * of data received, which doesn't seem to be available outside | |
4665 | * of the RPC layer. | |
4666 | * | |
4667 | * In the case of WRITE, we also want to put the GETATTR after | |
4668 | * the operation -- in this case because we want to make sure | |
140150db | 4669 | * we get the post-operation mtime and size. |
1da177e4 LT |
4670 | * |
4671 | * Both of these changes to the XDR layer would in fact be quite | |
4672 | * minor, but I decided to leave them for a subsequent patch. | |
4673 | */ | |
4674 | static int _nfs4_proc_readlink(struct inode *inode, struct page *page, | |
4675 | unsigned int pgbase, unsigned int pglen) | |
4676 | { | |
4677 | struct nfs4_readlink args = { | |
4678 | .fh = NFS_FH(inode), | |
4679 | .pgbase = pgbase, | |
4680 | .pglen = pglen, | |
4681 | .pages = &page, | |
4682 | }; | |
f50c7000 | 4683 | struct nfs4_readlink_res res; |
1da177e4 LT |
4684 | struct rpc_message msg = { |
4685 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK], | |
4686 | .rpc_argp = &args, | |
f50c7000 | 4687 | .rpc_resp = &res, |
1da177e4 LT |
4688 | }; |
4689 | ||
7c513058 | 4690 | return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
4691 | } |
4692 | ||
4693 | static int nfs4_proc_readlink(struct inode *inode, struct page *page, | |
4694 | unsigned int pgbase, unsigned int pglen) | |
4695 | { | |
0688e64b TM |
4696 | struct nfs4_exception exception = { |
4697 | .interruptible = true, | |
4698 | }; | |
1da177e4 LT |
4699 | int err; |
4700 | do { | |
c1578b76 TM |
4701 | err = _nfs4_proc_readlink(inode, page, pgbase, pglen); |
4702 | trace_nfs4_readlink(inode, err); | |
4703 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
1da177e4 LT |
4704 | &exception); |
4705 | } while (exception.retry); | |
4706 | return err; | |
4707 | } | |
4708 | ||
1da177e4 | 4709 | /* |
8867fe58 | 4710 | * This is just for mknod. open(O_CREAT) will always do ->open_context(). |
1da177e4 | 4711 | */ |
1da177e4 LT |
4712 | static int |
4713 | nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, | |
8867fe58 | 4714 | int flags) |
1da177e4 | 4715 | { |
dff25ddb | 4716 | struct nfs_server *server = NFS_SERVER(dir); |
c528f70f | 4717 | struct nfs4_label l, *ilabel; |
8867fe58 | 4718 | struct nfs_open_context *ctx; |
1da177e4 | 4719 | struct nfs4_state *state; |
1da177e4 LT |
4720 | int status = 0; |
4721 | ||
532d4def | 4722 | ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL); |
8867fe58 MS |
4723 | if (IS_ERR(ctx)) |
4724 | return PTR_ERR(ctx); | |
4725 | ||
aa9c2669 DQ |
4726 | ilabel = nfs4_label_init_security(dir, dentry, sattr, &l); |
4727 | ||
dff25ddb AG |
4728 | if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) |
4729 | sattr->ia_mode &= ~current_umask(); | |
c5c3fb5f | 4730 | state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL); |
1da177e4 LT |
4731 | if (IS_ERR(state)) { |
4732 | status = PTR_ERR(state); | |
c0204fd2 | 4733 | goto out; |
1da177e4 | 4734 | } |
1da177e4 | 4735 | out: |
aa9c2669 | 4736 | nfs4_label_release_security(ilabel); |
8867fe58 | 4737 | put_nfs_open_context(ctx); |
1da177e4 LT |
4738 | return status; |
4739 | } | |
4740 | ||
3c591175 TM |
4741 | static int |
4742 | _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype) | |
1da177e4 | 4743 | { |
16e42959 | 4744 | struct nfs_server *server = NFS_SERVER(dir); |
4fdc17b2 | 4745 | struct nfs_removeargs args = { |
1da177e4 | 4746 | .fh = NFS_FH(dir), |
26fe5750 | 4747 | .name = *name, |
16e42959 | 4748 | }; |
4fdc17b2 | 4749 | struct nfs_removeres res = { |
16e42959 | 4750 | .server = server, |
1da177e4 | 4751 | }; |
1da177e4 | 4752 | struct rpc_message msg = { |
4fdc17b2 TM |
4753 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE], |
4754 | .rpc_argp = &args, | |
4755 | .rpc_resp = &res, | |
1da177e4 | 4756 | }; |
d3129ef6 | 4757 | unsigned long timestamp = jiffies; |
778d2817 | 4758 | int status; |
1da177e4 | 4759 | |
7c513058 | 4760 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1); |
3c591175 TM |
4761 | if (status == 0) { |
4762 | spin_lock(&dir->i_lock); | |
3c591175 TM |
4763 | /* Removing a directory decrements nlink in the parent */ |
4764 | if (ftype == NF4DIR && dir->i_nlink > 2) | |
4765 | nfs4_dec_nlink_locked(dir); | |
82eae5a4 TM |
4766 | nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp, |
4767 | NFS_INO_INVALID_DATA); | |
3c591175 TM |
4768 | spin_unlock(&dir->i_lock); |
4769 | } | |
1da177e4 LT |
4770 | return status; |
4771 | } | |
4772 | ||
912678db | 4773 | static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry) |
1da177e4 | 4774 | { |
0688e64b TM |
4775 | struct nfs4_exception exception = { |
4776 | .interruptible = true, | |
4777 | }; | |
912678db | 4778 | struct inode *inode = d_inode(dentry); |
1da177e4 | 4779 | int err; |
912678db | 4780 | |
c01d3645 TM |
4781 | if (inode) { |
4782 | if (inode->i_nlink == 1) | |
4783 | nfs4_inode_return_delegation(inode); | |
4784 | else | |
4785 | nfs4_inode_make_writeable(inode); | |
4786 | } | |
912678db | 4787 | do { |
3c591175 | 4788 | err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG); |
912678db TM |
4789 | trace_nfs4_remove(dir, &dentry->d_name, err); |
4790 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
4791 | &exception); | |
4792 | } while (exception.retry); | |
4793 | return err; | |
4794 | } | |
4795 | ||
4796 | static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name) | |
4797 | { | |
0688e64b TM |
4798 | struct nfs4_exception exception = { |
4799 | .interruptible = true, | |
4800 | }; | |
912678db TM |
4801 | int err; |
4802 | ||
1da177e4 | 4803 | do { |
3c591175 | 4804 | err = _nfs4_proc_remove(dir, name, NF4DIR); |
078ea3df TM |
4805 | trace_nfs4_remove(dir, name, err); |
4806 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
1da177e4 LT |
4807 | &exception); |
4808 | } while (exception.retry); | |
4809 | return err; | |
4810 | } | |
4811 | ||
ed7e9ad0 TM |
4812 | static void nfs4_proc_unlink_setup(struct rpc_message *msg, |
4813 | struct dentry *dentry, | |
4814 | struct inode *inode) | |
1da177e4 | 4815 | { |
e4eff1a6 TM |
4816 | struct nfs_removeargs *args = msg->rpc_argp; |
4817 | struct nfs_removeres *res = msg->rpc_resp; | |
1da177e4 | 4818 | |
977fcc2b | 4819 | res->server = NFS_SB(dentry->d_sb); |
1da177e4 | 4820 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE]; |
fba83f34 | 4821 | nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0); |
aa9c2669 DQ |
4822 | |
4823 | nfs_fattr_init(res->dir_attr); | |
977fcc2b | 4824 | |
00bdadc7 | 4825 | if (inode) { |
977fcc2b | 4826 | nfs4_inode_return_delegation(inode); |
00bdadc7 TM |
4827 | nfs_d_prune_case_insensitive_aliases(inode); |
4828 | } | |
1da177e4 LT |
4829 | } |
4830 | ||
34e137cc BS |
4831 | static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data) |
4832 | { | |
42e1cca7 | 4833 | nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client, |
d9afbd1b TM |
4834 | &data->args.seq_args, |
4835 | &data->res.seq_res, | |
4836 | task); | |
1da177e4 LT |
4837 | } |
4838 | ||
e4eff1a6 | 4839 | static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir) |
1da177e4 | 4840 | { |
078ea3df TM |
4841 | struct nfs_unlinkdata *data = task->tk_calldata; |
4842 | struct nfs_removeres *res = &data->res; | |
e4eff1a6 | 4843 | |
14516c3a TM |
4844 | if (!nfs4_sequence_done(task, &res->seq_res)) |
4845 | return 0; | |
8478eaa1 N |
4846 | if (nfs4_async_handle_error(task, res->server, NULL, |
4847 | &data->timeout) == -EAGAIN) | |
e4eff1a6 | 4848 | return 0; |
c40d52fe | 4849 | if (task->tk_status == 0) |
1b523ca9 FL |
4850 | nfs4_update_changeattr(dir, &res->cinfo, |
4851 | res->dir_attr->time_start, | |
4852 | NFS_INO_INVALID_DATA); | |
e4eff1a6 | 4853 | return 1; |
1da177e4 LT |
4854 | } |
4855 | ||
f2c2c552 TM |
4856 | static void nfs4_proc_rename_setup(struct rpc_message *msg, |
4857 | struct dentry *old_dentry, | |
4858 | struct dentry *new_dentry) | |
d3d4152a | 4859 | { |
d3d4152a JL |
4860 | struct nfs_renameargs *arg = msg->rpc_argp; |
4861 | struct nfs_renameres *res = msg->rpc_resp; | |
f2c2c552 TM |
4862 | struct inode *old_inode = d_inode(old_dentry); |
4863 | struct inode *new_inode = d_inode(new_dentry); | |
d3d4152a | 4864 | |
f2c2c552 | 4865 | if (old_inode) |
c01d3645 | 4866 | nfs4_inode_make_writeable(old_inode); |
f2c2c552 TM |
4867 | if (new_inode) |
4868 | nfs4_inode_return_delegation(new_inode); | |
d3d4152a | 4869 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME]; |
f2c2c552 | 4870 | res->server = NFS_SB(old_dentry->d_sb); |
fba83f34 | 4871 | nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0); |
d3d4152a JL |
4872 | } |
4873 | ||
c6bfa1a1 BS |
4874 | static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data) |
4875 | { | |
42e1cca7 | 4876 | nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client, |
d9afbd1b TM |
4877 | &data->args.seq_args, |
4878 | &data->res.seq_res, | |
4879 | task); | |
d3d4152a JL |
4880 | } |
4881 | ||
4882 | static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir, | |
4883 | struct inode *new_dir) | |
4884 | { | |
fbc6f7c2 TM |
4885 | struct nfs_renamedata *data = task->tk_calldata; |
4886 | struct nfs_renameres *res = &data->res; | |
d3d4152a JL |
4887 | |
4888 | if (!nfs4_sequence_done(task, &res->seq_res)) | |
4889 | return 0; | |
8478eaa1 | 4890 | if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN) |
d3d4152a JL |
4891 | return 0; |
4892 | ||
c733c49c | 4893 | if (task->tk_status == 0) { |
00bdadc7 | 4894 | nfs_d_prune_case_insensitive_aliases(d_inode(data->old_dentry)); |
5636ec4e TM |
4895 | if (new_dir != old_dir) { |
4896 | /* Note: If we moved a directory, nlink will change */ | |
1b523ca9 | 4897 | nfs4_update_changeattr(old_dir, &res->old_cinfo, |
5636ec4e | 4898 | res->old_fattr->time_start, |
fabf2b34 | 4899 | NFS_INO_INVALID_NLINK | |
1b523ca9 FL |
4900 | NFS_INO_INVALID_DATA); |
4901 | nfs4_update_changeattr(new_dir, &res->new_cinfo, | |
5636ec4e | 4902 | res->new_fattr->time_start, |
fabf2b34 | 4903 | NFS_INO_INVALID_NLINK | |
1b523ca9 | 4904 | NFS_INO_INVALID_DATA); |
5636ec4e | 4905 | } else |
1b523ca9 | 4906 | nfs4_update_changeattr(old_dir, &res->old_cinfo, |
5636ec4e | 4907 | res->old_fattr->time_start, |
1b523ca9 | 4908 | NFS_INO_INVALID_DATA); |
c733c49c | 4909 | } |
d3d4152a JL |
4910 | return 1; |
4911 | } | |
4912 | ||
beffb8fe | 4913 | static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name) |
1da177e4 | 4914 | { |
91ba2eee | 4915 | struct nfs_server *server = NFS_SERVER(inode); |
2f28dc38 | 4916 | __u32 bitmask[NFS4_BITMASK_SZ]; |
1da177e4 LT |
4917 | struct nfs4_link_arg arg = { |
4918 | .fh = NFS_FH(inode), | |
4919 | .dir_fh = NFS_FH(dir), | |
4920 | .name = name, | |
2f28dc38 | 4921 | .bitmask = bitmask, |
91ba2eee | 4922 | }; |
91ba2eee TM |
4923 | struct nfs4_link_res res = { |
4924 | .server = server, | |
1da177e4 | 4925 | }; |
1da177e4 LT |
4926 | struct rpc_message msg = { |
4927 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK], | |
4928 | .rpc_argp = &arg, | |
91ba2eee | 4929 | .rpc_resp = &res, |
1da177e4 | 4930 | }; |
136f2627 TM |
4931 | int status = -ENOMEM; |
4932 | ||
aa7ca3b2 | 4933 | res.fattr = nfs_alloc_fattr_with_label(server); |
778d2817 | 4934 | if (res.fattr == NULL) |
136f2627 | 4935 | goto out; |
1da177e4 | 4936 | |
c01d3645 | 4937 | nfs4_inode_make_writeable(inode); |
aa7ca3b2 | 4938 | nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, res.fattr->label), inode, |
a71029b8 | 4939 | NFS_INO_INVALID_CHANGE); |
7c513058 | 4940 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
91ba2eee | 4941 | if (!status) { |
1b523ca9 FL |
4942 | nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start, |
4943 | NFS_INO_INVALID_DATA); | |
1301e421 | 4944 | nfs4_inc_nlink(inode); |
aa9c2669 DQ |
4945 | status = nfs_post_op_update_inode(inode, res.fattr); |
4946 | if (!status) | |
dd225cb3 | 4947 | nfs_setsecurity(inode, res.fattr); |
91ba2eee | 4948 | } |
14c43f76 | 4949 | |
136f2627 | 4950 | out: |
136f2627 | 4951 | nfs_free_fattr(res.fattr); |
1da177e4 LT |
4952 | return status; |
4953 | } | |
4954 | ||
beffb8fe | 4955 | static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name) |
1da177e4 | 4956 | { |
0688e64b TM |
4957 | struct nfs4_exception exception = { |
4958 | .interruptible = true, | |
4959 | }; | |
1da177e4 LT |
4960 | int err; |
4961 | do { | |
4962 | err = nfs4_handle_exception(NFS_SERVER(inode), | |
4963 | _nfs4_proc_link(inode, dir, name), | |
4964 | &exception); | |
4965 | } while (exception.retry); | |
4966 | return err; | |
4967 | } | |
4968 | ||
57dc9a57 TM |
4969 | struct nfs4_createdata { |
4970 | struct rpc_message msg; | |
4971 | struct nfs4_create_arg arg; | |
4972 | struct nfs4_create_res res; | |
4973 | struct nfs_fh fh; | |
4974 | struct nfs_fattr fattr; | |
57dc9a57 TM |
4975 | }; |
4976 | ||
4977 | static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir, | |
beffb8fe | 4978 | const struct qstr *name, struct iattr *sattr, u32 ftype) |
57dc9a57 TM |
4979 | { |
4980 | struct nfs4_createdata *data; | |
4981 | ||
4982 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
4983 | if (data != NULL) { | |
4984 | struct nfs_server *server = NFS_SERVER(dir); | |
4985 | ||
68be1742 AS |
4986 | data->fattr.label = nfs4_label_alloc(server, GFP_KERNEL); |
4987 | if (IS_ERR(data->fattr.label)) | |
14c43f76 DQ |
4988 | goto out_free; |
4989 | ||
57dc9a57 TM |
4990 | data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE]; |
4991 | data->msg.rpc_argp = &data->arg; | |
4992 | data->msg.rpc_resp = &data->res; | |
4993 | data->arg.dir_fh = NFS_FH(dir); | |
4994 | data->arg.server = server; | |
4995 | data->arg.name = name; | |
4996 | data->arg.attrs = sattr; | |
4997 | data->arg.ftype = ftype; | |
68be1742 | 4998 | data->arg.bitmask = nfs4_bitmask(server, data->fattr.label); |
dff25ddb | 4999 | data->arg.umask = current_umask(); |
57dc9a57 TM |
5000 | data->res.server = server; |
5001 | data->res.fh = &data->fh; | |
5002 | data->res.fattr = &data->fattr; | |
57dc9a57 | 5003 | nfs_fattr_init(data->res.fattr); |
57dc9a57 TM |
5004 | } |
5005 | return data; | |
14c43f76 DQ |
5006 | out_free: |
5007 | kfree(data); | |
5008 | return NULL; | |
57dc9a57 TM |
5009 | } |
5010 | ||
5011 | static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data) | |
5012 | { | |
7c513058 | 5013 | int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg, |
e73b83f2 | 5014 | &data->arg.seq_args, &data->res.seq_res, 1); |
57dc9a57 | 5015 | if (status == 0) { |
3c591175 | 5016 | spin_lock(&dir->i_lock); |
3c591175 TM |
5017 | /* Creating a directory bumps nlink in the parent */ |
5018 | if (data->arg.ftype == NF4DIR) | |
5019 | nfs4_inc_nlink_locked(dir); | |
82eae5a4 TM |
5020 | nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo, |
5021 | data->res.fattr->time_start, | |
5022 | NFS_INO_INVALID_DATA); | |
3c591175 | 5023 | spin_unlock(&dir->i_lock); |
d91bfc46 | 5024 | status = nfs_instantiate(dentry, data->res.fh, data->res.fattr); |
57dc9a57 TM |
5025 | } |
5026 | return status; | |
5027 | } | |
5028 | ||
5029 | static void nfs4_free_createdata(struct nfs4_createdata *data) | |
5030 | { | |
68be1742 | 5031 | nfs4_label_free(data->fattr.label); |
57dc9a57 TM |
5032 | kfree(data); |
5033 | } | |
5034 | ||
4f390c15 | 5035 | static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, |
f003a717 | 5036 | struct folio *folio, unsigned int len, struct iattr *sattr, |
1775fd3e | 5037 | struct nfs4_label *label) |
1da177e4 | 5038 | { |
f003a717 | 5039 | struct page *page = &folio->page; |
57dc9a57 TM |
5040 | struct nfs4_createdata *data; |
5041 | int status = -ENAMETOOLONG; | |
1da177e4 | 5042 | |
94a6d753 | 5043 | if (len > NFS4_MAXPATHLEN) |
57dc9a57 | 5044 | goto out; |
4f390c15 | 5045 | |
57dc9a57 TM |
5046 | status = -ENOMEM; |
5047 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK); | |
5048 | if (data == NULL) | |
5049 | goto out; | |
5050 | ||
5051 | data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK]; | |
5052 | data->arg.u.symlink.pages = &page; | |
5053 | data->arg.u.symlink.len = len; | |
1775fd3e | 5054 | data->arg.label = label; |
1da177e4 | 5055 | |
57dc9a57 TM |
5056 | status = nfs4_do_create(dir, dentry, data); |
5057 | ||
5058 | nfs4_free_createdata(data); | |
5059 | out: | |
1da177e4 LT |
5060 | return status; |
5061 | } | |
5062 | ||
4f390c15 | 5063 | static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, |
f003a717 | 5064 | struct folio *folio, unsigned int len, struct iattr *sattr) |
1da177e4 | 5065 | { |
0688e64b TM |
5066 | struct nfs4_exception exception = { |
5067 | .interruptible = true, | |
5068 | }; | |
c528f70f | 5069 | struct nfs4_label l, *label; |
1da177e4 | 5070 | int err; |
aa9c2669 DQ |
5071 | |
5072 | label = nfs4_label_init_security(dir, dentry, sattr, &l); | |
5073 | ||
1da177e4 | 5074 | do { |
f003a717 | 5075 | err = _nfs4_proc_symlink(dir, dentry, folio, len, sattr, label); |
078ea3df TM |
5076 | trace_nfs4_symlink(dir, &dentry->d_name, err); |
5077 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
1da177e4 LT |
5078 | &exception); |
5079 | } while (exception.retry); | |
aa9c2669 DQ |
5080 | |
5081 | nfs4_label_release_security(label); | |
1da177e4 LT |
5082 | return err; |
5083 | } | |
5084 | ||
5085 | static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, | |
1775fd3e | 5086 | struct iattr *sattr, struct nfs4_label *label) |
1da177e4 | 5087 | { |
57dc9a57 TM |
5088 | struct nfs4_createdata *data; |
5089 | int status = -ENOMEM; | |
1da177e4 | 5090 | |
57dc9a57 TM |
5091 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR); |
5092 | if (data == NULL) | |
5093 | goto out; | |
5094 | ||
1775fd3e | 5095 | data->arg.label = label; |
57dc9a57 TM |
5096 | status = nfs4_do_create(dir, dentry, data); |
5097 | ||
5098 | nfs4_free_createdata(data); | |
5099 | out: | |
1da177e4 LT |
5100 | return status; |
5101 | } | |
5102 | ||
5103 | static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, | |
5104 | struct iattr *sattr) | |
5105 | { | |
dff25ddb | 5106 | struct nfs_server *server = NFS_SERVER(dir); |
0688e64b TM |
5107 | struct nfs4_exception exception = { |
5108 | .interruptible = true, | |
5109 | }; | |
c528f70f | 5110 | struct nfs4_label l, *label; |
1da177e4 | 5111 | int err; |
a8a5da99 | 5112 | |
aa9c2669 DQ |
5113 | label = nfs4_label_init_security(dir, dentry, sattr, &l); |
5114 | ||
dff25ddb AG |
5115 | if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) |
5116 | sattr->ia_mode &= ~current_umask(); | |
1da177e4 | 5117 | do { |
078ea3df TM |
5118 | err = _nfs4_proc_mkdir(dir, dentry, sattr, label); |
5119 | trace_nfs4_mkdir(dir, &dentry->d_name, err); | |
5120 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
1da177e4 LT |
5121 | &exception); |
5122 | } while (exception.retry); | |
aa9c2669 DQ |
5123 | nfs4_label_release_security(label); |
5124 | ||
1da177e4 LT |
5125 | return err; |
5126 | } | |
5127 | ||
82e22a5e TM |
5128 | static int _nfs4_proc_readdir(struct nfs_readdir_arg *nr_arg, |
5129 | struct nfs_readdir_res *nr_res) | |
1da177e4 | 5130 | { |
82e22a5e | 5131 | struct inode *dir = d_inode(nr_arg->dentry); |
05ad9175 | 5132 | struct nfs_server *server = NFS_SERVER(dir); |
1da177e4 LT |
5133 | struct nfs4_readdir_arg args = { |
5134 | .fh = NFS_FH(dir), | |
82e22a5e | 5135 | .pages = nr_arg->pages, |
1da177e4 | 5136 | .pgbase = 0, |
82e22a5e TM |
5137 | .count = nr_arg->page_len, |
5138 | .plus = nr_arg->plus, | |
1da177e4 LT |
5139 | }; |
5140 | struct nfs4_readdir_res res; | |
5141 | struct rpc_message msg = { | |
5142 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR], | |
5143 | .rpc_argp = &args, | |
5144 | .rpc_resp = &res, | |
82e22a5e | 5145 | .rpc_cred = nr_arg->cred, |
1da177e4 LT |
5146 | }; |
5147 | int status; | |
5148 | ||
82e22a5e TM |
5149 | dprintk("%s: dentry = %pd2, cookie = %llu\n", __func__, |
5150 | nr_arg->dentry, (unsigned long long)nr_arg->cookie); | |
05ad9175 OK |
5151 | if (!(server->caps & NFS_CAP_SECURITY_LABEL)) |
5152 | args.bitmask = server->attr_bitmask_nl; | |
5153 | else | |
5154 | args.bitmask = server->attr_bitmask; | |
5155 | ||
82e22a5e | 5156 | nfs4_setup_readdir(nr_arg->cookie, nr_arg->verf, nr_arg->dentry, &args); |
1da177e4 | 5157 | res.pgbase = args.pgbase; |
05ad9175 OK |
5158 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, |
5159 | &res.seq_res, 0); | |
ac396128 | 5160 | if (status >= 0) { |
82e22a5e | 5161 | memcpy(nr_res->verf, res.verifier.data, NFS4_VERIFIER_SIZE); |
ac396128 TM |
5162 | status += args.pgbase; |
5163 | } | |
c4812998 TM |
5164 | |
5165 | nfs_invalidate_atime(dir); | |
5166 | ||
3110ff80 | 5167 | dprintk("%s: returns %d\n", __func__, status); |
1da177e4 LT |
5168 | return status; |
5169 | } | |
5170 | ||
82e22a5e TM |
5171 | static int nfs4_proc_readdir(struct nfs_readdir_arg *arg, |
5172 | struct nfs_readdir_res *res) | |
1da177e4 | 5173 | { |
0688e64b TM |
5174 | struct nfs4_exception exception = { |
5175 | .interruptible = true, | |
5176 | }; | |
1da177e4 LT |
5177 | int err; |
5178 | do { | |
82e22a5e TM |
5179 | err = _nfs4_proc_readdir(arg, res); |
5180 | trace_nfs4_readdir(d_inode(arg->dentry), err); | |
5181 | err = nfs4_handle_exception(NFS_SERVER(d_inode(arg->dentry)), | |
5182 | err, &exception); | |
1da177e4 LT |
5183 | } while (exception.retry); |
5184 | return err; | |
5185 | } | |
5186 | ||
5187 | static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, | |
aa9c2669 | 5188 | struct iattr *sattr, struct nfs4_label *label, dev_t rdev) |
1da177e4 | 5189 | { |
57dc9a57 TM |
5190 | struct nfs4_createdata *data; |
5191 | int mode = sattr->ia_mode; | |
5192 | int status = -ENOMEM; | |
1da177e4 | 5193 | |
57dc9a57 TM |
5194 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK); |
5195 | if (data == NULL) | |
5196 | goto out; | |
5197 | ||
1da177e4 | 5198 | if (S_ISFIFO(mode)) |
57dc9a57 | 5199 | data->arg.ftype = NF4FIFO; |
1da177e4 | 5200 | else if (S_ISBLK(mode)) { |
57dc9a57 TM |
5201 | data->arg.ftype = NF4BLK; |
5202 | data->arg.u.device.specdata1 = MAJOR(rdev); | |
5203 | data->arg.u.device.specdata2 = MINOR(rdev); | |
1da177e4 LT |
5204 | } |
5205 | else if (S_ISCHR(mode)) { | |
57dc9a57 TM |
5206 | data->arg.ftype = NF4CHR; |
5207 | data->arg.u.device.specdata1 = MAJOR(rdev); | |
5208 | data->arg.u.device.specdata2 = MINOR(rdev); | |
4ea8fed5 TM |
5209 | } else if (!S_ISSOCK(mode)) { |
5210 | status = -EINVAL; | |
5211 | goto out_free; | |
1da177e4 | 5212 | } |
1775fd3e | 5213 | |
aa9c2669 | 5214 | data->arg.label = label; |
57dc9a57 | 5215 | status = nfs4_do_create(dir, dentry, data); |
4ea8fed5 | 5216 | out_free: |
57dc9a57 TM |
5217 | nfs4_free_createdata(data); |
5218 | out: | |
1da177e4 LT |
5219 | return status; |
5220 | } | |
5221 | ||
5222 | static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, | |
5223 | struct iattr *sattr, dev_t rdev) | |
5224 | { | |
dff25ddb | 5225 | struct nfs_server *server = NFS_SERVER(dir); |
0688e64b TM |
5226 | struct nfs4_exception exception = { |
5227 | .interruptible = true, | |
5228 | }; | |
c528f70f | 5229 | struct nfs4_label l, *label; |
1da177e4 | 5230 | int err; |
a8a5da99 | 5231 | |
aa9c2669 DQ |
5232 | label = nfs4_label_init_security(dir, dentry, sattr, &l); |
5233 | ||
dff25ddb AG |
5234 | if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) |
5235 | sattr->ia_mode &= ~current_umask(); | |
1da177e4 | 5236 | do { |
078ea3df TM |
5237 | err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev); |
5238 | trace_nfs4_mknod(dir, &dentry->d_name, err); | |
5239 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
1da177e4 LT |
5240 | &exception); |
5241 | } while (exception.retry); | |
aa9c2669 DQ |
5242 | |
5243 | nfs4_label_release_security(label); | |
5244 | ||
1da177e4 LT |
5245 | return err; |
5246 | } | |
5247 | ||
5248 | static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, | |
5249 | struct nfs_fsstat *fsstat) | |
5250 | { | |
5251 | struct nfs4_statfs_arg args = { | |
5252 | .fh = fhandle, | |
5253 | .bitmask = server->attr_bitmask, | |
5254 | }; | |
24ad148a BH |
5255 | struct nfs4_statfs_res res = { |
5256 | .fsstat = fsstat, | |
5257 | }; | |
1da177e4 LT |
5258 | struct rpc_message msg = { |
5259 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS], | |
5260 | .rpc_argp = &args, | |
24ad148a | 5261 | .rpc_resp = &res, |
1da177e4 LT |
5262 | }; |
5263 | ||
0e574af1 | 5264 | nfs_fattr_init(fsstat->fattr); |
7c513058 | 5265 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
5266 | } |
5267 | ||
5268 | static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat) | |
5269 | { | |
0688e64b TM |
5270 | struct nfs4_exception exception = { |
5271 | .interruptible = true, | |
5272 | }; | |
1da177e4 LT |
5273 | int err; |
5274 | do { | |
5275 | err = nfs4_handle_exception(server, | |
5276 | _nfs4_proc_statfs(server, fhandle, fsstat), | |
5277 | &exception); | |
5278 | } while (exception.retry); | |
5279 | return err; | |
5280 | } | |
5281 | ||
5282 | static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, | |
5283 | struct nfs_fsinfo *fsinfo) | |
5284 | { | |
5285 | struct nfs4_fsinfo_arg args = { | |
5286 | .fh = fhandle, | |
5287 | .bitmask = server->attr_bitmask, | |
5288 | }; | |
3dda5e43 BH |
5289 | struct nfs4_fsinfo_res res = { |
5290 | .fsinfo = fsinfo, | |
5291 | }; | |
1da177e4 LT |
5292 | struct rpc_message msg = { |
5293 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO], | |
5294 | .rpc_argp = &args, | |
3dda5e43 | 5295 | .rpc_resp = &res, |
1da177e4 LT |
5296 | }; |
5297 | ||
7c513058 | 5298 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
5299 | } |
5300 | ||
5301 | static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) | |
5302 | { | |
0688e64b TM |
5303 | struct nfs4_exception exception = { |
5304 | .interruptible = true, | |
5305 | }; | |
1da177e4 LT |
5306 | int err; |
5307 | ||
5308 | do { | |
83ca7f5a | 5309 | err = _nfs4_do_fsinfo(server, fhandle, fsinfo); |
b5f875a9 | 5310 | trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err); |
83ca7f5a | 5311 | if (err == 0) { |
7dc2993a | 5312 | nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ); |
83ca7f5a CL |
5313 | break; |
5314 | } | |
5315 | err = nfs4_handle_exception(server, err, &exception); | |
1da177e4 LT |
5316 | } while (exception.retry); |
5317 | return err; | |
5318 | } | |
5319 | ||
5320 | static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) | |
5321 | { | |
e38eb650 BS |
5322 | int error; |
5323 | ||
0e574af1 | 5324 | nfs_fattr_init(fsinfo->fattr); |
e38eb650 | 5325 | error = nfs4_do_fsinfo(server, fhandle, fsinfo); |
dc182549 PT |
5326 | if (error == 0) { |
5327 | /* block layout checks this! */ | |
5328 | server->pnfs_blksize = fsinfo->blksize; | |
ca440c38 | 5329 | set_pnfs_layoutdriver(server, fhandle, fsinfo); |
dc182549 | 5330 | } |
e38eb650 BS |
5331 | |
5332 | return error; | |
1da177e4 LT |
5333 | } |
5334 | ||
5335 | static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, | |
5336 | struct nfs_pathconf *pathconf) | |
5337 | { | |
5338 | struct nfs4_pathconf_arg args = { | |
5339 | .fh = fhandle, | |
5340 | .bitmask = server->attr_bitmask, | |
5341 | }; | |
d45b2989 BH |
5342 | struct nfs4_pathconf_res res = { |
5343 | .pathconf = pathconf, | |
5344 | }; | |
1da177e4 LT |
5345 | struct rpc_message msg = { |
5346 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF], | |
5347 | .rpc_argp = &args, | |
d45b2989 | 5348 | .rpc_resp = &res, |
1da177e4 LT |
5349 | }; |
5350 | ||
5351 | /* None of the pathconf attributes are mandatory to implement */ | |
5352 | if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) { | |
5353 | memset(pathconf, 0, sizeof(*pathconf)); | |
5354 | return 0; | |
5355 | } | |
5356 | ||
0e574af1 | 5357 | nfs_fattr_init(pathconf->fattr); |
7c513058 | 5358 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
5359 | } |
5360 | ||
5361 | static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, | |
5362 | struct nfs_pathconf *pathconf) | |
5363 | { | |
0688e64b TM |
5364 | struct nfs4_exception exception = { |
5365 | .interruptible = true, | |
5366 | }; | |
1da177e4 LT |
5367 | int err; |
5368 | ||
5369 | do { | |
5370 | err = nfs4_handle_exception(server, | |
5371 | _nfs4_proc_pathconf(server, fhandle, pathconf), | |
5372 | &exception); | |
5373 | } while (exception.retry); | |
5374 | return err; | |
5375 | } | |
5376 | ||
5521abfd | 5377 | int nfs4_set_rw_stateid(nfs4_stateid *stateid, |
9b206149 TM |
5378 | const struct nfs_open_context *ctx, |
5379 | const struct nfs_lock_context *l_ctx, | |
5380 | fmode_t fmode) | |
5381 | { | |
17393475 | 5382 | return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL); |
9b206149 TM |
5383 | } |
5384 | EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid); | |
5385 | ||
5521abfd TM |
5386 | static bool nfs4_stateid_is_current(nfs4_stateid *stateid, |
5387 | const struct nfs_open_context *ctx, | |
5388 | const struct nfs_lock_context *l_ctx, | |
5389 | fmode_t fmode) | |
5390 | { | |
d49dd117 | 5391 | nfs4_stateid _current_stateid; |
5521abfd | 5392 | |
e1253be0 | 5393 | /* If the current stateid represents a lost lock, then exit */ |
d49dd117 | 5394 | if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO) |
e1253be0 | 5395 | return true; |
d49dd117 | 5396 | return nfs4_stateid_match(stateid, &_current_stateid); |
5521abfd TM |
5397 | } |
5398 | ||
5399 | static bool nfs4_error_stateid_expired(int err) | |
5400 | { | |
5401 | switch (err) { | |
5402 | case -NFS4ERR_DELEG_REVOKED: | |
5403 | case -NFS4ERR_ADMIN_REVOKED: | |
5404 | case -NFS4ERR_BAD_STATEID: | |
5405 | case -NFS4ERR_STALE_STATEID: | |
5406 | case -NFS4ERR_OLD_STATEID: | |
5407 | case -NFS4ERR_OPENMODE: | |
5408 | case -NFS4ERR_EXPIRED: | |
5409 | return true; | |
5410 | } | |
5411 | return false; | |
5412 | } | |
5413 | ||
d45f60c6 | 5414 | static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr) |
1da177e4 | 5415 | { |
d45f60c6 | 5416 | struct nfs_server *server = NFS_SERVER(hdr->inode); |
1da177e4 | 5417 | |
d45f60c6 | 5418 | trace_nfs4_read(hdr, task->tk_status); |
9c27869d TM |
5419 | if (task->tk_status < 0) { |
5420 | struct nfs4_exception exception = { | |
5421 | .inode = hdr->inode, | |
5422 | .state = hdr->args.context->state, | |
5423 | .stateid = &hdr->args.stateid, | |
5424 | }; | |
5425 | task->tk_status = nfs4_async_handle_exception(task, | |
5426 | server, task->tk_status, &exception); | |
5427 | if (exception.retry) { | |
5428 | rpc_restart_call_prepare(task); | |
5429 | return -EAGAIN; | |
5430 | } | |
1da177e4 | 5431 | } |
8850df99 | 5432 | |
1da177e4 | 5433 | if (task->tk_status > 0) |
d45f60c6 | 5434 | renew_lease(server, hdr->timestamp); |
ec06c096 | 5435 | return 0; |
1da177e4 LT |
5436 | } |
5437 | ||
5521abfd | 5438 | static bool nfs4_read_stateid_changed(struct rpc_task *task, |
3c6b899c | 5439 | struct nfs_pgio_args *args) |
5521abfd TM |
5440 | { |
5441 | ||
5442 | if (!nfs4_error_stateid_expired(task->tk_status) || | |
5443 | nfs4_stateid_is_current(&args->stateid, | |
5444 | args->context, | |
5445 | args->lock_context, | |
5446 | FMODE_READ)) | |
5447 | return false; | |
5448 | rpc_restart_call_prepare(task); | |
5449 | return true; | |
5450 | } | |
5451 | ||
c5675526 AS |
5452 | static bool nfs4_read_plus_not_supported(struct rpc_task *task, |
5453 | struct nfs_pgio_header *hdr) | |
cbdabc7f | 5454 | { |
c5675526 AS |
5455 | struct nfs_server *server = NFS_SERVER(hdr->inode); |
5456 | struct rpc_message *msg = &task->tk_msg; | |
cbdabc7f | 5457 | |
c5675526 | 5458 | if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS] && |
f06d1b10 | 5459 | task->tk_status == -ENOTSUPP) { |
c5675526 AS |
5460 | server->caps &= ~NFS_CAP_READ_PLUS; |
5461 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ]; | |
5462 | rpc_restart_call_prepare(task); | |
5463 | return true; | |
5464 | } | |
5465 | return false; | |
5466 | } | |
5467 | ||
43439d85 AS |
5468 | static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr) |
5469 | { | |
d45f60c6 | 5470 | if (!nfs4_sequence_done(task, &hdr->res.seq_res)) |
cbdabc7f | 5471 | return -EAGAIN; |
d45f60c6 | 5472 | if (nfs4_read_stateid_changed(task, &hdr->args)) |
5521abfd | 5473 | return -EAGAIN; |
c5675526 AS |
5474 | if (nfs4_read_plus_not_supported(task, hdr)) |
5475 | return -EAGAIN; | |
bfc505de TM |
5476 | if (task->tk_status > 0) |
5477 | nfs_invalidate_atime(hdr->inode); | |
d45f60c6 WAA |
5478 | return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : |
5479 | nfs4_read_done_cb(task, hdr); | |
cbdabc7f AA |
5480 | } |
5481 | ||
21e31401 | 5482 | #if defined CONFIG_NFS_V4_2 && defined CONFIG_NFS_V4_2_READ_PLUS |
fbd2a05f | 5483 | static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr, |
5c3485bb | 5484 | struct rpc_message *msg) |
c5675526 | 5485 | { |
5c3485bb | 5486 | /* Note: We don't use READ_PLUS with pNFS yet */ |
fbd2a05f | 5487 | if (nfs_server_capable(hdr->inode, NFS_CAP_READ_PLUS) && !hdr->ds_clp) { |
c5675526 | 5488 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ_PLUS]; |
303a7805 | 5489 | return nfs_read_alloc_scratch(hdr, READ_PLUS_SCRATCH_SIZE); |
fbd2a05f AS |
5490 | } |
5491 | return false; | |
c5675526 AS |
5492 | } |
5493 | #else | |
fbd2a05f | 5494 | static bool nfs42_read_plus_support(struct nfs_pgio_header *hdr, |
5c3485bb | 5495 | struct rpc_message *msg) |
c5675526 | 5496 | { |
fbd2a05f | 5497 | return false; |
c5675526 AS |
5498 | } |
5499 | #endif /* CONFIG_NFS_V4_2 */ | |
5500 | ||
d45f60c6 WAA |
5501 | static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr, |
5502 | struct rpc_message *msg) | |
1da177e4 | 5503 | { |
d45f60c6 | 5504 | hdr->timestamp = jiffies; |
ca857cc1 TM |
5505 | if (!hdr->pgio_done_cb) |
5506 | hdr->pgio_done_cb = nfs4_read_done_cb; | |
fbd2a05f AS |
5507 | if (!nfs42_read_plus_support(hdr, msg)) |
5508 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ]; | |
fba83f34 | 5509 | nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0); |
1da177e4 LT |
5510 | } |
5511 | ||
d45f60c6 WAA |
5512 | static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task, |
5513 | struct nfs_pgio_header *hdr) | |
ea7c3303 | 5514 | { |
42e1cca7 | 5515 | if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client, |
d45f60c6 WAA |
5516 | &hdr->args.seq_args, |
5517 | &hdr->res.seq_res, | |
9b206149 | 5518 | task)) |
ef1820f9 | 5519 | return 0; |
d45f60c6 WAA |
5520 | if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, |
5521 | hdr->args.lock_context, | |
fbe77c30 | 5522 | hdr->rw_mode) == -EIO) |
ef1820f9 | 5523 | return -EIO; |
d45f60c6 | 5524 | if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) |
ef1820f9 N |
5525 | return -EIO; |
5526 | return 0; | |
1da177e4 LT |
5527 | } |
5528 | ||
d45f60c6 WAA |
5529 | static int nfs4_write_done_cb(struct rpc_task *task, |
5530 | struct nfs_pgio_header *hdr) | |
1da177e4 | 5531 | { |
d45f60c6 | 5532 | struct inode *inode = hdr->inode; |
8478eaa1 | 5533 | |
d45f60c6 | 5534 | trace_nfs4_write(hdr, task->tk_status); |
9c27869d TM |
5535 | if (task->tk_status < 0) { |
5536 | struct nfs4_exception exception = { | |
5537 | .inode = hdr->inode, | |
5538 | .state = hdr->args.context->state, | |
5539 | .stateid = &hdr->args.stateid, | |
5540 | }; | |
5541 | task->tk_status = nfs4_async_handle_exception(task, | |
5542 | NFS_SERVER(inode), task->tk_status, | |
5543 | &exception); | |
5544 | if (exception.retry) { | |
5545 | rpc_restart_call_prepare(task); | |
5546 | return -EAGAIN; | |
5547 | } | |
1da177e4 | 5548 | } |
4f9838c7 | 5549 | if (task->tk_status >= 0) { |
d45f60c6 | 5550 | renew_lease(NFS_SERVER(inode), hdr->timestamp); |
a08a8cd3 | 5551 | nfs_writeback_update_inode(hdr); |
4f9838c7 | 5552 | } |
788e7a89 | 5553 | return 0; |
1da177e4 LT |
5554 | } |
5555 | ||
5521abfd | 5556 | static bool nfs4_write_stateid_changed(struct rpc_task *task, |
3c6b899c | 5557 | struct nfs_pgio_args *args) |
5521abfd TM |
5558 | { |
5559 | ||
5560 | if (!nfs4_error_stateid_expired(task->tk_status) || | |
5561 | nfs4_stateid_is_current(&args->stateid, | |
5562 | args->context, | |
5563 | args->lock_context, | |
5564 | FMODE_WRITE)) | |
5565 | return false; | |
5566 | rpc_restart_call_prepare(task); | |
5567 | return true; | |
5568 | } | |
5569 | ||
d45f60c6 | 5570 | static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr) |
b029bc9b | 5571 | { |
d45f60c6 | 5572 | if (!nfs4_sequence_done(task, &hdr->res.seq_res)) |
b029bc9b | 5573 | return -EAGAIN; |
d45f60c6 | 5574 | if (nfs4_write_stateid_changed(task, &hdr->args)) |
5521abfd | 5575 | return -EAGAIN; |
d45f60c6 WAA |
5576 | return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : |
5577 | nfs4_write_done_cb(task, hdr); | |
b029bc9b FI |
5578 | } |
5579 | ||
5a37f851 | 5580 | static |
d45f60c6 | 5581 | bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr) |
a69aef14 | 5582 | { |
5a37f851 | 5583 | /* Don't request attributes for pNFS or O_DIRECT writes */ |
d45f60c6 | 5584 | if (hdr->ds_clp != NULL || hdr->dreq != NULL) |
5a37f851 TM |
5585 | return false; |
5586 | /* Otherwise, request attributes if and only if we don't hold | |
5587 | * a delegation | |
5588 | */ | |
011e2a7f | 5589 | return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0; |
a69aef14 | 5590 | } |
a69aef14 | 5591 | |
85847280 TM |
5592 | void nfs4_bitmask_set(__u32 bitmask[], const __u32 src[], |
5593 | struct inode *inode, unsigned long cache_validity) | |
76bd5c01 | 5594 | { |
85847280 | 5595 | struct nfs_server *server = NFS_SERVER(inode); |
332d1a03 | 5596 | unsigned int i; |
76bd5c01 | 5597 | |
332d1a03 | 5598 | memcpy(bitmask, src, sizeof(*bitmask) * NFS4_BITMASK_SZ); |
85847280 | 5599 | cache_validity |= READ_ONCE(NFS_I(inode)->cache_validity); |
76bd5c01 | 5600 | |
13c0b082 | 5601 | if (cache_validity & NFS_INO_INVALID_CHANGE) |
332d1a03 | 5602 | bitmask[0] |= FATTR4_WORD0_CHANGE; |
76bd5c01 OK |
5603 | if (cache_validity & NFS_INO_INVALID_ATIME) |
5604 | bitmask[1] |= FATTR4_WORD1_TIME_ACCESS; | |
720869eb TM |
5605 | if (cache_validity & NFS_INO_INVALID_MODE) |
5606 | bitmask[1] |= FATTR4_WORD1_MODE; | |
45901a23 | 5607 | if (cache_validity & NFS_INO_INVALID_OTHER) |
720869eb | 5608 | bitmask[1] |= FATTR4_WORD1_OWNER | FATTR4_WORD1_OWNER_GROUP; |
fabf2b34 TM |
5609 | if (cache_validity & NFS_INO_INVALID_NLINK) |
5610 | bitmask[1] |= FATTR4_WORD1_NUMLINKS; | |
45901a23 TM |
5611 | if (cache_validity & NFS_INO_INVALID_CTIME) |
5612 | bitmask[1] |= FATTR4_WORD1_TIME_METADATA; | |
76bd5c01 OK |
5613 | if (cache_validity & NFS_INO_INVALID_MTIME) |
5614 | bitmask[1] |= FATTR4_WORD1_TIME_MODIFY; | |
76bd5c01 OK |
5615 | if (cache_validity & NFS_INO_INVALID_BLOCKS) |
5616 | bitmask[1] |= FATTR4_WORD1_SPACE_USED; | |
332d1a03 | 5617 | |
cc7f2dae | 5618 | if (cache_validity & NFS_INO_INVALID_SIZE) |
332d1a03 TM |
5619 | bitmask[0] |= FATTR4_WORD0_SIZE; |
5620 | ||
5621 | for (i = 0; i < NFS4_BITMASK_SZ; i++) | |
5622 | bitmask[i] &= server->attr_bitmask[i]; | |
76bd5c01 OK |
5623 | } |
5624 | ||
d45f60c6 | 5625 | static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr, |
fb91fb0e AS |
5626 | struct rpc_message *msg, |
5627 | struct rpc_clnt **clnt) | |
1da177e4 | 5628 | { |
d45f60c6 | 5629 | struct nfs_server *server = NFS_SERVER(hdr->inode); |
bdc7f021 | 5630 | |
d45f60c6 WAA |
5631 | if (!nfs4_write_need_cache_consistency_data(hdr)) { |
5632 | hdr->args.bitmask = NULL; | |
5633 | hdr->res.fattr = NULL; | |
76bd5c01 | 5634 | } else { |
332d1a03 TM |
5635 | nfs4_bitmask_set(hdr->args.bitmask_store, |
5636 | server->cache_consistency_bitmask, | |
85847280 | 5637 | hdr->inode, NFS_INO_INVALID_BLOCKS); |
332d1a03 | 5638 | hdr->args.bitmask = hdr->args.bitmask_store; |
76bd5c01 | 5639 | } |
5a37f851 | 5640 | |
d45f60c6 WAA |
5641 | if (!hdr->pgio_done_cb) |
5642 | hdr->pgio_done_cb = nfs4_write_done_cb; | |
5643 | hdr->res.server = server; | |
5644 | hdr->timestamp = jiffies; | |
1da177e4 | 5645 | |
bdc7f021 | 5646 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE]; |
cd1b659d | 5647 | nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0); |
5cc7688b | 5648 | nfs4_state_protect_write(hdr->ds_clp ? hdr->ds_clp : server->nfs_client, clnt, msg, hdr); |
1da177e4 LT |
5649 | } |
5650 | ||
0b7c0153 | 5651 | static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data) |
c6cb80d0 | 5652 | { |
42e1cca7 | 5653 | nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client, |
d9afbd1b TM |
5654 | &data->args.seq_args, |
5655 | &data->res.seq_res, | |
5656 | task); | |
1da177e4 LT |
5657 | } |
5658 | ||
0b7c0153 | 5659 | static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data) |
1da177e4 | 5660 | { |
1da177e4 | 5661 | struct inode *inode = data->inode; |
14516c3a | 5662 | |
cc668ab3 | 5663 | trace_nfs4_commit(data, task->tk_status); |
8478eaa1 N |
5664 | if (nfs4_async_handle_error(task, NFS_SERVER(inode), |
5665 | NULL, NULL) == -EAGAIN) { | |
d00c5d43 | 5666 | rpc_restart_call_prepare(task); |
788e7a89 | 5667 | return -EAGAIN; |
1da177e4 | 5668 | } |
788e7a89 | 5669 | return 0; |
1da177e4 LT |
5670 | } |
5671 | ||
0b7c0153 | 5672 | static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data) |
5f452431 FI |
5673 | { |
5674 | if (!nfs4_sequence_done(task, &data->res.seq_res)) | |
5675 | return -EAGAIN; | |
0b7c0153 | 5676 | return data->commit_done_cb(task, data); |
5f452431 FI |
5677 | } |
5678 | ||
e9ae1ee2 AS |
5679 | static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg, |
5680 | struct rpc_clnt **clnt) | |
1da177e4 | 5681 | { |
788e7a89 | 5682 | struct nfs_server *server = NFS_SERVER(data->inode); |
988b6dce | 5683 | |
0b7c0153 FI |
5684 | if (data->commit_done_cb == NULL) |
5685 | data->commit_done_cb = nfs4_commit_done_cb; | |
4f9838c7 | 5686 | data->res.server = server; |
bdc7f021 | 5687 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT]; |
fba83f34 | 5688 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0); |
5cc7688b OK |
5689 | nfs4_state_protect(data->ds_clp ? data->ds_clp : server->nfs_client, |
5690 | NFS_SP4_MACH_CRED_COMMIT, clnt, msg); | |
1da177e4 LT |
5691 | } |
5692 | ||
6b8d84e2 OK |
5693 | static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args, |
5694 | struct nfs_commitres *res) | |
5695 | { | |
5696 | struct inode *dst_inode = file_inode(dst); | |
5697 | struct nfs_server *server = NFS_SERVER(dst_inode); | |
5698 | struct rpc_message msg = { | |
5699 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT], | |
5700 | .rpc_argp = args, | |
5701 | .rpc_resp = res, | |
5702 | }; | |
5703 | ||
5704 | args->fh = NFS_FH(dst_inode); | |
5705 | return nfs4_call_sync(server->client, server, &msg, | |
5706 | &args->seq_args, &res->seq_res, 1); | |
5707 | } | |
5708 | ||
5709 | int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res) | |
5710 | { | |
5711 | struct nfs_commitargs args = { | |
5712 | .offset = offset, | |
5713 | .count = count, | |
5714 | }; | |
5715 | struct nfs_server *dst_server = NFS_SERVER(file_inode(dst)); | |
5716 | struct nfs4_exception exception = { }; | |
5717 | int status; | |
5718 | ||
5719 | do { | |
5720 | status = _nfs4_proc_commit(dst, &args, res); | |
5721 | status = nfs4_handle_exception(dst_server, status, &exception); | |
5722 | } while (exception.retry); | |
5723 | ||
5724 | return status; | |
5725 | } | |
5726 | ||
9bc4e3ca CL |
5727 | struct nfs4_renewdata { |
5728 | struct nfs_client *client; | |
5729 | unsigned long timestamp; | |
5730 | }; | |
5731 | ||
1da177e4 LT |
5732 | /* |
5733 | * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special | |
5734 | * standalone procedure for queueing an asynchronous RENEW. | |
5735 | */ | |
9bc4e3ca | 5736 | static void nfs4_renew_release(void *calldata) |
dc96aef9 | 5737 | { |
9bc4e3ca CL |
5738 | struct nfs4_renewdata *data = calldata; |
5739 | struct nfs_client *clp = data->client; | |
dc96aef9 | 5740 | |
212bf41d | 5741 | if (refcount_read(&clp->cl_count) > 1) |
0851de06 AB |
5742 | nfs4_schedule_state_renewal(clp); |
5743 | nfs_put_client(clp); | |
9bc4e3ca | 5744 | kfree(data); |
dc96aef9 AB |
5745 | } |
5746 | ||
9bc4e3ca | 5747 | static void nfs4_renew_done(struct rpc_task *task, void *calldata) |
1da177e4 | 5748 | { |
9bc4e3ca CL |
5749 | struct nfs4_renewdata *data = calldata; |
5750 | struct nfs_client *clp = data->client; | |
5751 | unsigned long timestamp = data->timestamp; | |
1da177e4 | 5752 | |
c6d01c6f | 5753 | trace_nfs4_renew_async(clp, task->tk_status); |
f8aba1e8 CL |
5754 | switch (task->tk_status) { |
5755 | case 0: | |
5756 | break; | |
5757 | case -NFS4ERR_LEASE_MOVED: | |
5758 | nfs4_schedule_lease_moved_recovery(clp); | |
5759 | break; | |
5760 | default: | |
95baa25c | 5761 | /* Unless we're shutting down, schedule state recovery! */ |
042b60be TM |
5762 | if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0) |
5763 | return; | |
5764 | if (task->tk_status != NFS4ERR_CB_PATH_DOWN) { | |
0400a6b0 | 5765 | nfs4_schedule_lease_recovery(clp); |
042b60be TM |
5766 | return; |
5767 | } | |
5768 | nfs4_schedule_path_down_recovery(clp); | |
1da177e4 | 5769 | } |
452e9352 | 5770 | do_renew_lease(clp, timestamp); |
1da177e4 LT |
5771 | } |
5772 | ||
963d8fe5 TM |
5773 | static const struct rpc_call_ops nfs4_renew_ops = { |
5774 | .rpc_call_done = nfs4_renew_done, | |
dc96aef9 | 5775 | .rpc_release = nfs4_renew_release, |
963d8fe5 TM |
5776 | }; |
5777 | ||
a52458b4 | 5778 | static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags) |
1da177e4 LT |
5779 | { |
5780 | struct rpc_message msg = { | |
5781 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], | |
5782 | .rpc_argp = clp, | |
b4454fe1 | 5783 | .rpc_cred = cred, |
1da177e4 | 5784 | }; |
9bc4e3ca | 5785 | struct nfs4_renewdata *data; |
1da177e4 | 5786 | |
2f60ea6b TM |
5787 | if (renew_flags == 0) |
5788 | return 0; | |
212bf41d | 5789 | if (!refcount_inc_not_zero(&clp->cl_count)) |
0851de06 | 5790 | return -EIO; |
b569ad34 | 5791 | data = kmalloc(sizeof(*data), GFP_NOFS); |
5c737cb2 DW |
5792 | if (data == NULL) { |
5793 | nfs_put_client(clp); | |
9bc4e3ca | 5794 | return -ENOMEM; |
5c737cb2 | 5795 | } |
9bc4e3ca CL |
5796 | data->client = clp; |
5797 | data->timestamp = jiffies; | |
bc7a05ca | 5798 | return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT, |
9bc4e3ca | 5799 | &nfs4_renew_ops, data); |
1da177e4 LT |
5800 | } |
5801 | ||
a52458b4 | 5802 | static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred) |
1da177e4 LT |
5803 | { |
5804 | struct rpc_message msg = { | |
5805 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], | |
5806 | .rpc_argp = clp, | |
b4454fe1 | 5807 | .rpc_cred = cred, |
1da177e4 LT |
5808 | }; |
5809 | unsigned long now = jiffies; | |
5810 | int status; | |
5811 | ||
bc7a05ca | 5812 | status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
1da177e4 LT |
5813 | if (status < 0) |
5814 | return status; | |
452e9352 | 5815 | do_renew_lease(clp, now); |
1da177e4 LT |
5816 | return 0; |
5817 | } | |
5818 | ||
7b8b44eb TM |
5819 | static bool nfs4_server_supports_acls(const struct nfs_server *server, |
5820 | enum nfs4_acl_type type) | |
aa1870af | 5821 | { |
7b8b44eb TM |
5822 | switch (type) { |
5823 | default: | |
5824 | return server->attr_bitmask[0] & FATTR4_WORD0_ACL; | |
5825 | case NFS4ACL_DACL: | |
5826 | return server->attr_bitmask[1] & FATTR4_WORD1_DACL; | |
5827 | case NFS4ACL_SACL: | |
5828 | return server->attr_bitmask[1] & FATTR4_WORD1_SACL; | |
5829 | } | |
aa1870af BF |
5830 | } |
5831 | ||
21f498c2 TM |
5832 | /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that |
5833 | * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on | |
aa1870af BF |
5834 | * the stack. |
5835 | */ | |
21f498c2 | 5836 | #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE) |
aa1870af | 5837 | |
ccde1e9c | 5838 | int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen, |
8fbcf237 | 5839 | struct page **pages) |
e9e3d724 NH |
5840 | { |
5841 | struct page *newpage, **spages; | |
5842 | int rc = 0; | |
5843 | size_t len; | |
5844 | spages = pages; | |
5845 | ||
5846 | do { | |
21f498c2 | 5847 | len = min_t(size_t, PAGE_SIZE, buflen); |
e9e3d724 NH |
5848 | newpage = alloc_page(GFP_KERNEL); |
5849 | ||
5850 | if (newpage == NULL) | |
5851 | goto unwind; | |
5852 | memcpy(page_address(newpage), buf, len); | |
d9b67e1e AS |
5853 | buf += len; |
5854 | buflen -= len; | |
e9e3d724 NH |
5855 | *pages++ = newpage; |
5856 | rc++; | |
5857 | } while (buflen != 0); | |
5858 | ||
5859 | return rc; | |
5860 | ||
5861 | unwind: | |
5862 | for(; rc > 0; rc--) | |
5863 | __free_page(spages[rc-1]); | |
5864 | return -ENOMEM; | |
5865 | } | |
5866 | ||
e50a1c2e | 5867 | struct nfs4_cached_acl { |
7b8b44eb | 5868 | enum nfs4_acl_type type; |
e50a1c2e BF |
5869 | int cached; |
5870 | size_t len; | |
5601cda8 | 5871 | char data[]; |
e50a1c2e BF |
5872 | }; |
5873 | ||
5874 | static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl) | |
5875 | { | |
5876 | struct nfs_inode *nfsi = NFS_I(inode); | |
5877 | ||
5878 | spin_lock(&inode->i_lock); | |
5879 | kfree(nfsi->nfs4_acl); | |
5880 | nfsi->nfs4_acl = acl; | |
5881 | spin_unlock(&inode->i_lock); | |
5882 | } | |
5883 | ||
5884 | static void nfs4_zap_acl_attr(struct inode *inode) | |
5885 | { | |
5886 | nfs4_set_cached_acl(inode, NULL); | |
5887 | } | |
5888 | ||
7b8b44eb TM |
5889 | static ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, |
5890 | size_t buflen, enum nfs4_acl_type type) | |
e50a1c2e BF |
5891 | { |
5892 | struct nfs_inode *nfsi = NFS_I(inode); | |
5893 | struct nfs4_cached_acl *acl; | |
5894 | int ret = -ENOENT; | |
5895 | ||
5896 | spin_lock(&inode->i_lock); | |
5897 | acl = nfsi->nfs4_acl; | |
5898 | if (acl == NULL) | |
5899 | goto out; | |
7b8b44eb TM |
5900 | if (acl->type != type) |
5901 | goto out; | |
e50a1c2e BF |
5902 | if (buf == NULL) /* user is just asking for length */ |
5903 | goto out_len; | |
5904 | if (acl->cached == 0) | |
5905 | goto out; | |
5906 | ret = -ERANGE; /* see getxattr(2) man page */ | |
5907 | if (acl->len > buflen) | |
5908 | goto out; | |
5909 | memcpy(buf, acl->data, acl->len); | |
5910 | out_len: | |
5911 | ret = acl->len; | |
5912 | out: | |
5913 | spin_unlock(&inode->i_lock); | |
5914 | return ret; | |
5915 | } | |
5916 | ||
7b8b44eb TM |
5917 | static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, |
5918 | size_t pgbase, size_t acl_len, | |
5919 | enum nfs4_acl_type type) | |
e50a1c2e BF |
5920 | { |
5921 | struct nfs4_cached_acl *acl; | |
b291f1b1 | 5922 | size_t buflen = sizeof(*acl) + acl_len; |
e50a1c2e | 5923 | |
1f1ea6c2 | 5924 | if (buflen <= PAGE_SIZE) { |
b291f1b1 | 5925 | acl = kmalloc(buflen, GFP_KERNEL); |
e50a1c2e BF |
5926 | if (acl == NULL) |
5927 | goto out; | |
5928 | acl->cached = 1; | |
5794d21e | 5929 | _copy_from_pages(acl->data, pages, pgbase, acl_len); |
e50a1c2e BF |
5930 | } else { |
5931 | acl = kmalloc(sizeof(*acl), GFP_KERNEL); | |
5932 | if (acl == NULL) | |
5933 | goto out; | |
5934 | acl->cached = 0; | |
5935 | } | |
7b8b44eb | 5936 | acl->type = type; |
e50a1c2e BF |
5937 | acl->len = acl_len; |
5938 | out: | |
5939 | nfs4_set_cached_acl(inode, acl); | |
5940 | } | |
5941 | ||
bf118a34 AA |
5942 | /* |
5943 | * The getxattr API returns the required buffer length when called with a | |
5944 | * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating | |
5945 | * the required buf. On a NULL buf, we send a page of data to the server | |
5946 | * guessing that the ACL request can be serviced by a page. If so, we cache | |
5947 | * up to the page of ACL data, and the 2nd call to getxattr is serviced by | |
5948 | * the cache. If not so, we throw away the page, and cache the required | |
5949 | * length. The next getxattr call will then produce another round trip to | |
5950 | * the server, this time with the input buf of the required size. | |
5951 | */ | |
7b8b44eb TM |
5952 | static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, |
5953 | size_t buflen, enum nfs4_acl_type type) | |
aa1870af | 5954 | { |
62a1573f | 5955 | struct page **pages; |
aa1870af BF |
5956 | struct nfs_getaclargs args = { |
5957 | .fh = NFS_FH(inode), | |
db145db0 | 5958 | .acl_type = type, |
aa1870af BF |
5959 | .acl_len = buflen, |
5960 | }; | |
663c79b3 | 5961 | struct nfs_getaclres res = { |
db145db0 | 5962 | .acl_type = type, |
663c79b3 BH |
5963 | .acl_len = buflen, |
5964 | }; | |
aa1870af BF |
5965 | struct rpc_message msg = { |
5966 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL], | |
5967 | .rpc_argp = &args, | |
663c79b3 | 5968 | .rpc_resp = &res, |
aa1870af | 5969 | }; |
62a1573f | 5970 | unsigned int npages; |
21f498c2 | 5971 | int ret = -ENOMEM, i; |
62a1573f OK |
5972 | struct nfs_server *server = NFS_SERVER(inode); |
5973 | ||
5974 | if (buflen == 0) | |
5975 | buflen = server->rsize; | |
5976 | ||
5977 | npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1; | |
da48f267 | 5978 | pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); |
62a1573f OK |
5979 | if (!pages) |
5980 | return -ENOMEM; | |
aa1870af | 5981 | |
62a1573f | 5982 | args.acl_pages = pages; |
5a006899 | 5983 | |
bf118a34 AA |
5984 | for (i = 0; i < npages; i++) { |
5985 | pages[i] = alloc_page(GFP_KERNEL); | |
5986 | if (!pages[i]) | |
5987 | goto out_free; | |
e50a1c2e | 5988 | } |
5a006899 SP |
5989 | |
5990 | /* for decoding across pages */ | |
5991 | res.acl_scratch = alloc_page(GFP_KERNEL); | |
5992 | if (!res.acl_scratch) | |
5993 | goto out_free; | |
5994 | ||
bf118a34 | 5995 | args.acl_len = npages * PAGE_SIZE; |
5a006899 | 5996 | |
de040bec | 5997 | dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n", |
bf118a34 AA |
5998 | __func__, buf, buflen, npages, args.acl_len); |
5999 | ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), | |
6000 | &msg, &args.seq_args, &res.seq_res, 0); | |
e50a1c2e BF |
6001 | if (ret) |
6002 | goto out_free; | |
bf118a34 | 6003 | |
1f1ea6c2 TM |
6004 | /* Handle the case where the passed-in buffer is too short */ |
6005 | if (res.acl_flags & NFS4_ACL_TRUNC) { | |
6006 | /* Did the user only issue a request for the acl length? */ | |
6007 | if (buf == NULL) | |
6008 | goto out_ok; | |
e50a1c2e | 6009 | ret = -ERANGE; |
1f1ea6c2 | 6010 | goto out_free; |
e50a1c2e | 6011 | } |
7b8b44eb TM |
6012 | nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len, |
6013 | type); | |
7d3e91a8 SW |
6014 | if (buf) { |
6015 | if (res.acl_len > buflen) { | |
6016 | ret = -ERANGE; | |
6017 | goto out_free; | |
6018 | } | |
1f1ea6c2 | 6019 | _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len); |
7d3e91a8 | 6020 | } |
1f1ea6c2 TM |
6021 | out_ok: |
6022 | ret = res.acl_len; | |
e50a1c2e | 6023 | out_free: |
f4e89f1a FP |
6024 | while (--i >= 0) |
6025 | __free_page(pages[i]); | |
331818f1 TM |
6026 | if (res.acl_scratch) |
6027 | __free_page(res.acl_scratch); | |
62a1573f | 6028 | kfree(pages); |
aa1870af BF |
6029 | return ret; |
6030 | } | |
6031 | ||
7b8b44eb TM |
6032 | static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, |
6033 | size_t buflen, enum nfs4_acl_type type) | |
16b4289c | 6034 | { |
0688e64b TM |
6035 | struct nfs4_exception exception = { |
6036 | .interruptible = true, | |
6037 | }; | |
16b4289c TM |
6038 | ssize_t ret; |
6039 | do { | |
7b8b44eb | 6040 | ret = __nfs4_get_acl_uncached(inode, buf, buflen, type); |
c1578b76 | 6041 | trace_nfs4_get_acl(inode, ret); |
16b4289c TM |
6042 | if (ret >= 0) |
6043 | break; | |
6044 | ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception); | |
6045 | } while (exception.retry); | |
6046 | return ret; | |
6047 | } | |
6048 | ||
7b8b44eb TM |
6049 | static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen, |
6050 | enum nfs4_acl_type type) | |
e50a1c2e BF |
6051 | { |
6052 | struct nfs_server *server = NFS_SERVER(inode); | |
6053 | int ret; | |
6054 | ||
7b8b44eb | 6055 | if (!nfs4_server_supports_acls(server, type)) |
e50a1c2e | 6056 | return -EOPNOTSUPP; |
1f3208b2 | 6057 | ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE); |
e50a1c2e BF |
6058 | if (ret < 0) |
6059 | return ret; | |
08a22b39 AK |
6060 | if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL) |
6061 | nfs_zap_acl_cache(inode); | |
7b8b44eb | 6062 | ret = nfs4_read_cached_acl(inode, buf, buflen, type); |
e50a1c2e | 6063 | if (ret != -ENOENT) |
bf118a34 AA |
6064 | /* -ENOENT is returned if there is no ACL or if there is an ACL |
6065 | * but no cached acl data, just the acl length */ | |
e50a1c2e | 6066 | return ret; |
7b8b44eb | 6067 | return nfs4_get_acl_uncached(inode, buf, buflen, type); |
e50a1c2e BF |
6068 | } |
6069 | ||
7b8b44eb TM |
6070 | static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, |
6071 | size_t buflen, enum nfs4_acl_type type) | |
4b580ee3 BF |
6072 | { |
6073 | struct nfs_server *server = NFS_SERVER(inode); | |
6074 | struct page *pages[NFS4ACL_MAXPAGES]; | |
6075 | struct nfs_setaclargs arg = { | |
db145db0 TM |
6076 | .fh = NFS_FH(inode), |
6077 | .acl_type = type, | |
6078 | .acl_len = buflen, | |
6079 | .acl_pages = pages, | |
4b580ee3 | 6080 | }; |
73c403a9 | 6081 | struct nfs_setaclres res; |
4b580ee3 BF |
6082 | struct rpc_message msg = { |
6083 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL], | |
6084 | .rpc_argp = &arg, | |
73c403a9 | 6085 | .rpc_resp = &res, |
4b580ee3 | 6086 | }; |
21f498c2 | 6087 | unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE); |
e9e3d724 | 6088 | int ret, i; |
4b580ee3 | 6089 | |
4f8be1f5 BF |
6090 | /* You can't remove system.nfs4_acl: */ |
6091 | if (buflen == 0) | |
6092 | return -EINVAL; | |
7b8b44eb | 6093 | if (!nfs4_server_supports_acls(server, type)) |
4b580ee3 | 6094 | return -EOPNOTSUPP; |
21f498c2 TM |
6095 | if (npages > ARRAY_SIZE(pages)) |
6096 | return -ERANGE; | |
ccde1e9c | 6097 | i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages); |
e9e3d724 NH |
6098 | if (i < 0) |
6099 | return i; | |
c01d3645 | 6100 | nfs4_inode_make_writeable(inode); |
7c513058 | 6101 | ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
e9e3d724 NH |
6102 | |
6103 | /* | |
6104 | * Free each page after tx, so the only ref left is | |
6105 | * held by the network stack | |
6106 | */ | |
6107 | for (; i > 0; i--) | |
6108 | put_page(pages[i-1]); | |
6109 | ||
08a22b39 AK |
6110 | /* |
6111 | * Acl update can result in inode attribute update. | |
6112 | * so mark the attribute cache invalid. | |
6113 | */ | |
6114 | spin_lock(&inode->i_lock); | |
b6f80a2e TM |
6115 | nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE | |
6116 | NFS_INO_INVALID_CTIME | | |
6117 | NFS_INO_REVAL_FORCED); | |
08a22b39 | 6118 | spin_unlock(&inode->i_lock); |
f41f7418 TM |
6119 | nfs_access_zap_cache(inode); |
6120 | nfs_zap_acl_cache(inode); | |
4b580ee3 BF |
6121 | return ret; |
6122 | } | |
6123 | ||
7b8b44eb TM |
6124 | static int nfs4_proc_set_acl(struct inode *inode, const void *buf, |
6125 | size_t buflen, enum nfs4_acl_type type) | |
16b4289c TM |
6126 | { |
6127 | struct nfs4_exception exception = { }; | |
6128 | int err; | |
6129 | do { | |
7b8b44eb | 6130 | err = __nfs4_proc_set_acl(inode, buf, buflen, type); |
c1578b76 | 6131 | trace_nfs4_set_acl(inode, err); |
f8849e20 DN |
6132 | if (err == -NFS4ERR_BADOWNER || err == -NFS4ERR_BADNAME) { |
6133 | /* | |
6134 | * no need to retry since the kernel | |
6135 | * isn't involved in encoding the ACEs. | |
6136 | */ | |
6137 | err = -EINVAL; | |
6138 | break; | |
6139 | } | |
c1578b76 | 6140 | err = nfs4_handle_exception(NFS_SERVER(inode), err, |
16b4289c TM |
6141 | &exception); |
6142 | } while (exception.retry); | |
6143 | return err; | |
6144 | } | |
6145 | ||
aa9c2669 DQ |
6146 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
6147 | static int _nfs4_get_security_label(struct inode *inode, void *buf, | |
6148 | size_t buflen) | |
6149 | { | |
6150 | struct nfs_server *server = NFS_SERVER(inode); | |
aa9c2669 DQ |
6151 | struct nfs4_label label = {0, 0, buflen, buf}; |
6152 | ||
6153 | u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; | |
2ef61e0e AS |
6154 | struct nfs_fattr fattr = { |
6155 | .label = &label, | |
6156 | }; | |
fcb63a9b | 6157 | struct nfs4_getattr_arg arg = { |
aa9c2669 DQ |
6158 | .fh = NFS_FH(inode), |
6159 | .bitmask = bitmask, | |
6160 | }; | |
6161 | struct nfs4_getattr_res res = { | |
6162 | .fattr = &fattr, | |
aa9c2669 DQ |
6163 | .server = server, |
6164 | }; | |
6165 | struct rpc_message msg = { | |
6166 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], | |
fcb63a9b | 6167 | .rpc_argp = &arg, |
aa9c2669 DQ |
6168 | .rpc_resp = &res, |
6169 | }; | |
6170 | int ret; | |
6171 | ||
6172 | nfs_fattr_init(&fattr); | |
6173 | ||
fcb63a9b | 6174 | ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0); |
aa9c2669 DQ |
6175 | if (ret) |
6176 | return ret; | |
6177 | if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL)) | |
6178 | return -ENOENT; | |
53cb2454 | 6179 | return label.len; |
aa9c2669 DQ |
6180 | } |
6181 | ||
6182 | static int nfs4_get_security_label(struct inode *inode, void *buf, | |
6183 | size_t buflen) | |
6184 | { | |
0688e64b TM |
6185 | struct nfs4_exception exception = { |
6186 | .interruptible = true, | |
6187 | }; | |
aa9c2669 DQ |
6188 | int err; |
6189 | ||
6190 | if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) | |
6191 | return -EOPNOTSUPP; | |
6192 | ||
6193 | do { | |
c1578b76 TM |
6194 | err = _nfs4_get_security_label(inode, buf, buflen); |
6195 | trace_nfs4_get_security_label(inode, err); | |
6196 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
aa9c2669 DQ |
6197 | &exception); |
6198 | } while (exception.retry); | |
6199 | return err; | |
6200 | } | |
6201 | ||
6202 | static int _nfs4_do_set_security_label(struct inode *inode, | |
6203 | struct nfs4_label *ilabel, | |
1b00ad65 | 6204 | struct nfs_fattr *fattr) |
aa9c2669 DQ |
6205 | { |
6206 | ||
6207 | struct iattr sattr = {0}; | |
6208 | struct nfs_server *server = NFS_SERVER(inode); | |
6209 | const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; | |
12207f69 | 6210 | struct nfs_setattrargs arg = { |
d9b67e1e AS |
6211 | .fh = NFS_FH(inode), |
6212 | .iap = &sattr, | |
aa9c2669 DQ |
6213 | .server = server, |
6214 | .bitmask = bitmask, | |
6215 | .label = ilabel, | |
6216 | }; | |
6217 | struct nfs_setattrres res = { | |
6218 | .fattr = fattr, | |
aa9c2669 DQ |
6219 | .server = server, |
6220 | }; | |
6221 | struct rpc_message msg = { | |
d9b67e1e AS |
6222 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], |
6223 | .rpc_argp = &arg, | |
6224 | .rpc_resp = &res, | |
aa9c2669 DQ |
6225 | }; |
6226 | int status; | |
6227 | ||
12207f69 | 6228 | nfs4_stateid_copy(&arg.stateid, &zero_stateid); |
aa9c2669 | 6229 | |
12207f69 | 6230 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
aa9c2669 DQ |
6231 | if (status) |
6232 | dprintk("%s failed: %d\n", __func__, status); | |
6233 | ||
6234 | return status; | |
6235 | } | |
6236 | ||
6237 | static int nfs4_do_set_security_label(struct inode *inode, | |
6238 | struct nfs4_label *ilabel, | |
1b00ad65 | 6239 | struct nfs_fattr *fattr) |
aa9c2669 DQ |
6240 | { |
6241 | struct nfs4_exception exception = { }; | |
6242 | int err; | |
6243 | ||
6244 | do { | |
1b00ad65 | 6245 | err = _nfs4_do_set_security_label(inode, ilabel, fattr); |
c1578b76 TM |
6246 | trace_nfs4_set_security_label(inode, err); |
6247 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
aa9c2669 DQ |
6248 | &exception); |
6249 | } while (exception.retry); | |
6250 | return err; | |
6251 | } | |
6252 | ||
6253 | static int | |
59301226 | 6254 | nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen) |
aa9c2669 | 6255 | { |
1b00ad65 AS |
6256 | struct nfs4_label ilabel = {0, 0, buflen, (char *)buf }; |
6257 | struct nfs_fattr *fattr; | |
aa9c2669 DQ |
6258 | int status; |
6259 | ||
6260 | if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) | |
6261 | return -EOPNOTSUPP; | |
6262 | ||
1b00ad65 AS |
6263 | fattr = nfs_alloc_fattr_with_label(NFS_SERVER(inode)); |
6264 | if (fattr == NULL) | |
6265 | return -ENOMEM; | |
aa9c2669 | 6266 | |
1b00ad65 | 6267 | status = nfs4_do_set_security_label(inode, &ilabel, fattr); |
aa9c2669 | 6268 | if (status == 0) |
dd225cb3 | 6269 | nfs_setsecurity(inode, fattr); |
aa9c2669 | 6270 | |
aa9c2669 DQ |
6271 | return status; |
6272 | } | |
6273 | #endif /* CONFIG_NFS_V4_SECURITY_LABEL */ | |
6274 | ||
6275 | ||
f092075d CL |
6276 | static void nfs4_init_boot_verifier(const struct nfs_client *clp, |
6277 | nfs4_verifier *bootverf) | |
cd93710e CL |
6278 | { |
6279 | __be32 verf[2]; | |
6280 | ||
2c820d9a CL |
6281 | if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) { |
6282 | /* An impossible timestamp guarantees this value | |
6283 | * will never match a generated boot time. */ | |
2f86e091 DD |
6284 | verf[0] = cpu_to_be32(U32_MAX); |
6285 | verf[1] = cpu_to_be32(U32_MAX); | |
2c820d9a | 6286 | } else { |
f092075d | 6287 | struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); |
2f86e091 DD |
6288 | u64 ns = ktime_to_ns(nn->boot_time); |
6289 | ||
6290 | verf[0] = cpu_to_be32(ns >> 32); | |
6291 | verf[1] = cpu_to_be32(ns); | |
2c820d9a | 6292 | } |
cd93710e CL |
6293 | memcpy(bootverf->data, verf, sizeof(bootverf->data)); |
6294 | } | |
6295 | ||
1aee5513 | 6296 | static size_t |
39d43d16 | 6297 | nfs4_get_uniquifier(struct nfs_client *clp, char *buf, size_t buflen) |
1aee5513 | 6298 | { |
39d43d16 TM |
6299 | struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); |
6300 | struct nfs_netns_client *nn_clp = nn->nfs_client; | |
6301 | const char *id; | |
6302 | ||
1aee5513 TM |
6303 | buf[0] = '\0'; |
6304 | ||
39d43d16 TM |
6305 | if (nn_clp) { |
6306 | rcu_read_lock(); | |
6307 | id = rcu_dereference(nn_clp->identifier); | |
6308 | if (id) | |
6309 | strscpy(buf, id, buflen); | |
6310 | rcu_read_unlock(); | |
6311 | } | |
6312 | ||
6313 | if (nfs4_client_id_uniquifier[0] != '\0' && buf[0] == '\0') | |
1aee5513 TM |
6314 | strscpy(buf, nfs4_client_id_uniquifier, buflen); |
6315 | ||
6316 | return strlen(buf); | |
6317 | } | |
6318 | ||
a3192688 JL |
6319 | static int |
6320 | nfs4_init_nonuniform_client_string(struct nfs_client *clp) | |
e984a55a | 6321 | { |
1aee5513 TM |
6322 | char buf[NFS4_CLIENT_ID_UNIQ_LEN]; |
6323 | size_t buflen; | |
a3192688 JL |
6324 | size_t len; |
6325 | char *str; | |
e984a55a | 6326 | |
ceb3a16c | 6327 | if (clp->cl_owner_id != NULL) |
a3192688 | 6328 | return 0; |
4a3e5779 | 6329 | |
a3192688 | 6330 | rcu_read_lock(); |
848a4eb2 CL |
6331 | len = 14 + |
6332 | strlen(clp->cl_rpcclient->cl_nodename) + | |
6333 | 1 + | |
a3192688 | 6334 | strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) + |
a3192688 JL |
6335 | 1; |
6336 | rcu_read_unlock(); | |
1aee5513 | 6337 | |
39d43d16 | 6338 | buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf)); |
1aee5513 TM |
6339 | if (buflen) |
6340 | len += buflen + 1; | |
6341 | ||
a3192688 JL |
6342 | if (len > NFS4_OPAQUE_LIMIT + 1) |
6343 | return -EINVAL; | |
6344 | ||
6345 | /* | |
6346 | * Since this string is allocated at mount time, and held until the | |
6347 | * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying | |
6348 | * about a memory-reclaim deadlock. | |
6349 | */ | |
6350 | str = kmalloc(len, GFP_KERNEL); | |
6351 | if (!str) | |
6352 | return -ENOMEM; | |
ceb3a16c | 6353 | |
e984a55a | 6354 | rcu_read_lock(); |
1aee5513 | 6355 | if (buflen) |
025bb9f8 | 6356 | scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s", |
1aee5513 | 6357 | clp->cl_rpcclient->cl_nodename, buf, |
848a4eb2 | 6358 | rpc_peeraddr2str(clp->cl_rpcclient, |
025bb9f8 | 6359 | RPC_DISPLAY_ADDR)); |
848a4eb2 | 6360 | else |
025bb9f8 | 6361 | scnprintf(str, len, "Linux NFSv4.0 %s/%s", |
848a4eb2 CL |
6362 | clp->cl_rpcclient->cl_nodename, |
6363 | rpc_peeraddr2str(clp->cl_rpcclient, | |
025bb9f8 | 6364 | RPC_DISPLAY_ADDR)); |
e984a55a | 6365 | rcu_read_unlock(); |
a3192688 | 6366 | |
a3192688 JL |
6367 | clp->cl_owner_id = str; |
6368 | return 0; | |
e984a55a CL |
6369 | } |
6370 | ||
873e3851 JL |
6371 | static int |
6372 | nfs4_init_uniform_client_string(struct nfs_client *clp) | |
e984a55a | 6373 | { |
1aee5513 TM |
6374 | char buf[NFS4_CLIENT_ID_UNIQ_LEN]; |
6375 | size_t buflen; | |
873e3851 JL |
6376 | size_t len; |
6377 | char *str; | |
ceb3a16c TM |
6378 | |
6379 | if (clp->cl_owner_id != NULL) | |
873e3851 | 6380 | return 0; |
6f2ea7f2 | 6381 | |
873e3851 JL |
6382 | len = 10 + 10 + 1 + 10 + 1 + |
6383 | strlen(clp->cl_rpcclient->cl_nodename) + 1; | |
6384 | ||
39d43d16 | 6385 | buflen = nfs4_get_uniquifier(clp, buf, sizeof(buf)); |
1aee5513 TM |
6386 | if (buflen) |
6387 | len += buflen + 1; | |
6388 | ||
873e3851 JL |
6389 | if (len > NFS4_OPAQUE_LIMIT + 1) |
6390 | return -EINVAL; | |
6391 | ||
6392 | /* | |
6393 | * Since this string is allocated at mount time, and held until the | |
6394 | * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying | |
6395 | * about a memory-reclaim deadlock. | |
6396 | */ | |
6397 | str = kmalloc(len, GFP_KERNEL); | |
6398 | if (!str) | |
6399 | return -ENOMEM; | |
6400 | ||
1aee5513 TM |
6401 | if (buflen) |
6402 | scnprintf(str, len, "Linux NFSv%u.%u %s/%s", | |
6403 | clp->rpc_ops->version, clp->cl_minorversion, | |
6404 | buf, clp->cl_rpcclient->cl_nodename); | |
6405 | else | |
6406 | scnprintf(str, len, "Linux NFSv%u.%u %s", | |
6407 | clp->rpc_ops->version, clp->cl_minorversion, | |
6408 | clp->cl_rpcclient->cl_nodename); | |
873e3851 JL |
6409 | clp->cl_owner_id = str; |
6410 | return 0; | |
e984a55a CL |
6411 | } |
6412 | ||
706cb8db CL |
6413 | /* |
6414 | * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback | |
6415 | * services. Advertise one based on the address family of the | |
6416 | * clientaddr. | |
6417 | */ | |
6418 | static unsigned int | |
6419 | nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len) | |
6420 | { | |
6421 | if (strchr(clp->cl_ipaddr, ':') != NULL) | |
6422 | return scnprintf(buf, len, "tcp6"); | |
6423 | else | |
6424 | return scnprintf(buf, len, "tcp"); | |
6425 | } | |
6426 | ||
f11b2a1c JL |
6427 | static void nfs4_setclientid_done(struct rpc_task *task, void *calldata) |
6428 | { | |
6429 | struct nfs4_setclientid *sc = calldata; | |
6430 | ||
6431 | if (task->tk_status == 0) | |
6432 | sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred); | |
6433 | } | |
6434 | ||
6435 | static const struct rpc_call_ops nfs4_setclientid_ops = { | |
6436 | .rpc_call_done = nfs4_setclientid_done, | |
6437 | }; | |
6438 | ||
6bbb4ae8 CL |
6439 | /** |
6440 | * nfs4_proc_setclientid - Negotiate client ID | |
6441 | * @clp: state data structure | |
6442 | * @program: RPC program for NFSv4 callback service | |
6443 | * @port: IP port number for NFS4 callback service | |
a52458b4 | 6444 | * @cred: credential to use for this call |
6bbb4ae8 CL |
6445 | * @res: where to place the result |
6446 | * | |
6447 | * Returns zero, a negative errno, or a negative NFS4ERR status code. | |
6448 | */ | |
bb8b27e5 | 6449 | int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, |
a52458b4 | 6450 | unsigned short port, const struct cred *cred, |
bb8b27e5 | 6451 | struct nfs4_setclientid_res *res) |
1da177e4 LT |
6452 | { |
6453 | nfs4_verifier sc_verifier; | |
6454 | struct nfs4_setclientid setclientid = { | |
6455 | .sc_verifier = &sc_verifier, | |
6456 | .sc_prog = program, | |
3a6bb738 | 6457 | .sc_clnt = clp, |
1da177e4 LT |
6458 | }; |
6459 | struct rpc_message msg = { | |
6460 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID], | |
6461 | .rpc_argp = &setclientid, | |
bb8b27e5 | 6462 | .rpc_resp = res, |
286d7d6a | 6463 | .rpc_cred = cred, |
1da177e4 | 6464 | }; |
f11b2a1c JL |
6465 | struct rpc_task_setup task_setup_data = { |
6466 | .rpc_client = clp->cl_rpcclient, | |
6467 | .rpc_message = &msg, | |
6468 | .callback_ops = &nfs4_setclientid_ops, | |
6469 | .callback_data = &setclientid, | |
5a0c257f | 6470 | .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN, |
f11b2a1c | 6471 | }; |
7dc2993a | 6472 | unsigned long now = jiffies; |
6bbb4ae8 | 6473 | int status; |
1da177e4 | 6474 | |
de734831 | 6475 | /* nfs_client_id4 */ |
f092075d | 6476 | nfs4_init_boot_verifier(clp, &sc_verifier); |
873e3851 JL |
6477 | |
6478 | if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags)) | |
6479 | status = nfs4_init_uniform_client_string(clp); | |
6480 | else | |
a3192688 | 6481 | status = nfs4_init_nonuniform_client_string(clp); |
873e3851 JL |
6482 | |
6483 | if (status) | |
6484 | goto out; | |
3a6bb738 | 6485 | |
de734831 | 6486 | /* cb_client4 */ |
706cb8db CL |
6487 | setclientid.sc_netid_len = |
6488 | nfs4_init_callback_netid(clp, | |
6489 | setclientid.sc_netid, | |
6490 | sizeof(setclientid.sc_netid)); | |
de734831 | 6491 | setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr, |
d4d3c507 | 6492 | sizeof(setclientid.sc_uaddr), "%s.%u.%u", |
1da177e4 LT |
6493 | clp->cl_ipaddr, port >> 8, port & 255); |
6494 | ||
3a6bb738 | 6495 | dprintk("NFS call setclientid auth=%s, '%s'\n", |
6bbb4ae8 | 6496 | clp->cl_rpcclient->cl_auth->au_ops->au_name, |
3a6bb738 | 6497 | clp->cl_owner_id); |
dae40965 AS |
6498 | |
6499 | status = nfs4_call_sync_custom(&task_setup_data); | |
f11b2a1c | 6500 | if (setclientid.sc_cred) { |
1047ec86 | 6501 | kfree(clp->cl_acceptor); |
f11b2a1c JL |
6502 | clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred); |
6503 | put_rpccred(setclientid.sc_cred); | |
6504 | } | |
7dc2993a RM |
6505 | |
6506 | if (status == 0) | |
6507 | do_renew_lease(clp, now); | |
f11b2a1c | 6508 | out: |
c6d01c6f | 6509 | trace_nfs4_setclientid(clp, status); |
6bbb4ae8 CL |
6510 | dprintk("NFS reply setclientid: %d\n", status); |
6511 | return status; | |
1da177e4 LT |
6512 | } |
6513 | ||
6bbb4ae8 CL |
6514 | /** |
6515 | * nfs4_proc_setclientid_confirm - Confirm client ID | |
6516 | * @clp: state data structure | |
302fad7b | 6517 | * @arg: result of a previous SETCLIENTID |
a52458b4 | 6518 | * @cred: credential to use for this call |
6bbb4ae8 CL |
6519 | * |
6520 | * Returns zero, a negative errno, or a negative NFS4ERR status code. | |
6521 | */ | |
fd954ae1 | 6522 | int nfs4_proc_setclientid_confirm(struct nfs_client *clp, |
bb8b27e5 | 6523 | struct nfs4_setclientid_res *arg, |
a52458b4 | 6524 | const struct cred *cred) |
1da177e4 | 6525 | { |
1da177e4 LT |
6526 | struct rpc_message msg = { |
6527 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM], | |
bb8b27e5 | 6528 | .rpc_argp = arg, |
286d7d6a | 6529 | .rpc_cred = cred, |
1da177e4 | 6530 | }; |
1da177e4 LT |
6531 | int status; |
6532 | ||
6bbb4ae8 CL |
6533 | dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n", |
6534 | clp->cl_rpcclient->cl_auth->au_ops->au_name, | |
6535 | clp->cl_clientid); | |
5a0c257f N |
6536 | status = rpc_call_sync(clp->cl_rpcclient, &msg, |
6537 | RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); | |
c6d01c6f | 6538 | trace_nfs4_setclientid_confirm(clp, status); |
6bbb4ae8 | 6539 | dprintk("NFS reply setclientid_confirm: %d\n", status); |
1da177e4 LT |
6540 | return status; |
6541 | } | |
6542 | ||
fe650407 TM |
6543 | struct nfs4_delegreturndata { |
6544 | struct nfs4_delegreturnargs args; | |
fa178f29 | 6545 | struct nfs4_delegreturnres res; |
fe650407 TM |
6546 | struct nfs_fh fh; |
6547 | nfs4_stateid stateid; | |
26e976a8 | 6548 | unsigned long timestamp; |
586f1c39 TM |
6549 | struct { |
6550 | struct nfs4_layoutreturn_args arg; | |
6551 | struct nfs4_layoutreturn_res res; | |
4d796d75 | 6552 | struct nfs4_xdr_opaque_data ld_private; |
586f1c39 TM |
6553 | u32 roc_barrier; |
6554 | bool roc; | |
6555 | } lr; | |
fa178f29 | 6556 | struct nfs_fattr fattr; |
fe650407 | 6557 | int rpc_status; |
039b756a | 6558 | struct inode *inode; |
fe650407 TM |
6559 | }; |
6560 | ||
fe650407 TM |
6561 | static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata) |
6562 | { | |
6563 | struct nfs4_delegreturndata *data = calldata; | |
e0dba012 TM |
6564 | struct nfs4_exception exception = { |
6565 | .inode = data->inode, | |
6566 | .stateid = &data->stateid, | |
dfe1fe75 | 6567 | .task_is_privileged = data->args.seq_args.sa_privileged, |
e0dba012 | 6568 | }; |
938e1010 | 6569 | |
14516c3a TM |
6570 | if (!nfs4_sequence_done(task, &data->res.seq_res)) |
6571 | return; | |
938e1010 | 6572 | |
ca8acf8d | 6573 | trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status); |
586f1c39 TM |
6574 | |
6575 | /* Handle Layoutreturn errors */ | |
078000d0 TM |
6576 | if (pnfs_roc_done(task, &data->args.lr_args, &data->res.lr_res, |
6577 | &data->res.lr_ret) == -EAGAIN) | |
287a9c55 | 6578 | goto out_restart; |
586f1c39 | 6579 | |
79708861 | 6580 | switch (task->tk_status) { |
79708861 | 6581 | case 0: |
fa178f29 | 6582 | renew_lease(data->res.server, data->timestamp); |
23ea44c2 | 6583 | break; |
c97cf606 TM |
6584 | case -NFS4ERR_ADMIN_REVOKED: |
6585 | case -NFS4ERR_DELEG_REVOKED: | |
26d36301 TM |
6586 | case -NFS4ERR_EXPIRED: |
6587 | nfs4_free_revoked_stateid(data->res.server, | |
6588 | data->args.stateid, | |
6589 | task->tk_msg.rpc_cred); | |
df561f66 | 6590 | fallthrough; |
c97cf606 | 6591 | case -NFS4ERR_BAD_STATEID: |
c97cf606 | 6592 | case -NFS4ERR_STALE_STATEID: |
244fcd2f | 6593 | case -ETIMEDOUT: |
c97cf606 TM |
6594 | task->tk_status = 0; |
6595 | break; | |
12f275cd | 6596 | case -NFS4ERR_OLD_STATEID: |
246afc0a TM |
6597 | if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode)) |
6598 | nfs4_stateid_seqid_inc(&data->stateid); | |
70d136b2 TM |
6599 | if (data->args.bitmask) { |
6600 | data->args.bitmask = NULL; | |
6601 | data->res.fattr = NULL; | |
6602 | } | |
246afc0a | 6603 | goto out_restart; |
8ac2b422 TM |
6604 | case -NFS4ERR_ACCESS: |
6605 | if (data->args.bitmask) { | |
6606 | data->args.bitmask = NULL; | |
6607 | data->res.fattr = NULL; | |
140087fd | 6608 | goto out_restart; |
8ac2b422 | 6609 | } |
df561f66 | 6610 | fallthrough; |
79708861 | 6611 | default: |
e0dba012 TM |
6612 | task->tk_status = nfs4_async_handle_exception(task, |
6613 | data->res.server, task->tk_status, | |
6614 | &exception); | |
6615 | if (exception.retry) | |
140087fd | 6616 | goto out_restart; |
79708861 | 6617 | } |
d51f91d2 | 6618 | nfs_delegation_mark_returned(data->inode, data->args.stateid); |
79708861 | 6619 | data->rpc_status = task->tk_status; |
140087fd | 6620 | return; |
140087fd TM |
6621 | out_restart: |
6622 | task->tk_status = 0; | |
6623 | rpc_restart_call_prepare(task); | |
fe650407 TM |
6624 | } |
6625 | ||
6626 | static void nfs4_delegreturn_release(void *calldata) | |
6627 | { | |
039b756a | 6628 | struct nfs4_delegreturndata *data = calldata; |
ea7c38fe | 6629 | struct inode *inode = data->inode; |
039b756a | 6630 | |
078000d0 TM |
6631 | if (data->lr.roc) |
6632 | pnfs_roc_release(&data->lr.arg, &data->lr.res, | |
6633 | data->res.lr_ret); | |
ea7c38fe | 6634 | if (inode) { |
00c94ebe TM |
6635 | nfs4_fattr_set_prechange(&data->fattr, |
6636 | inode_peek_iversion_raw(inode)); | |
6637 | nfs_refresh_inode(inode, &data->fattr); | |
ea7c38fe TM |
6638 | nfs_iput_and_deactive(inode); |
6639 | } | |
fe650407 TM |
6640 | kfree(calldata); |
6641 | } | |
6642 | ||
938e1010 AA |
6643 | static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data) |
6644 | { | |
6645 | struct nfs4_delegreturndata *d_data; | |
c8bf7073 | 6646 | struct pnfs_layout_hdr *lo; |
938e1010 | 6647 | |
7e7ce2cc | 6648 | d_data = data; |
938e1010 | 6649 | |
5326de9e TM |
6650 | if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) { |
6651 | nfs4_sequence_done(task, &d_data->res.seq_res); | |
500d701f | 6652 | return; |
5326de9e | 6653 | } |
500d701f | 6654 | |
c8bf7073 TM |
6655 | lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL; |
6656 | if (lo && !pnfs_layout_is_valid(lo)) { | |
6657 | d_data->args.lr_args = NULL; | |
6658 | d_data->res.lr_res = NULL; | |
6659 | } | |
6660 | ||
42e1cca7 | 6661 | nfs4_setup_sequence(d_data->res.server->nfs_client, |
d9afbd1b TM |
6662 | &d_data->args.seq_args, |
6663 | &d_data->res.seq_res, | |
6664 | task); | |
938e1010 | 6665 | } |
938e1010 | 6666 | |
c8d149f3 | 6667 | static const struct rpc_call_ops nfs4_delegreturn_ops = { |
938e1010 | 6668 | .rpc_call_prepare = nfs4_delegreturn_prepare, |
fe650407 TM |
6669 | .rpc_call_done = nfs4_delegreturn_done, |
6670 | .rpc_release = nfs4_delegreturn_release, | |
6671 | }; | |
6672 | ||
a52458b4 | 6673 | static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync) |
fe650407 TM |
6674 | { |
6675 | struct nfs4_delegreturndata *data; | |
fa178f29 | 6676 | struct nfs_server *server = NFS_SERVER(inode); |
fe650407 | 6677 | struct rpc_task *task; |
5138fde0 TM |
6678 | struct rpc_message msg = { |
6679 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN], | |
6680 | .rpc_cred = cred, | |
6681 | }; | |
c970aa85 TM |
6682 | struct rpc_task_setup task_setup_data = { |
6683 | .rpc_client = server->client, | |
5138fde0 | 6684 | .rpc_message = &msg, |
c970aa85 | 6685 | .callback_ops = &nfs4_delegreturn_ops, |
118f09ed | 6686 | .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT, |
c970aa85 | 6687 | }; |
e6f81075 | 6688 | int status = 0; |
fe650407 | 6689 | |
118f09ed OK |
6690 | if (nfs_server_capable(inode, NFS_CAP_MOVEABLE)) |
6691 | task_setup_data.flags |= RPC_TASK_MOVEABLE; | |
6692 | ||
da48f267 | 6693 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
fe650407 TM |
6694 | if (data == NULL) |
6695 | return -ENOMEM; | |
99ade3c7 AE |
6696 | |
6697 | nfs4_state_protect(server->nfs_client, | |
6698 | NFS_SP4_MACH_CRED_CLEANUP, | |
6699 | &task_setup_data.rpc_client, &msg); | |
6700 | ||
fe650407 TM |
6701 | data->args.fhandle = &data->fh; |
6702 | data->args.stateid = &data->stateid; | |
332d1a03 | 6703 | nfs4_bitmask_set(data->args.bitmask_store, |
85847280 | 6704 | server->cache_consistency_bitmask, inode, 0); |
332d1a03 | 6705 | data->args.bitmask = data->args.bitmask_store; |
fe650407 | 6706 | nfs_copy_fh(&data->fh, NFS_FH(inode)); |
f597c537 | 6707 | nfs4_stateid_copy(&data->stateid, stateid); |
fa178f29 TM |
6708 | data->res.fattr = &data->fattr; |
6709 | data->res.server = server; | |
586f1c39 | 6710 | data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT; |
4d796d75 | 6711 | data->lr.arg.ld_private = &data->lr.ld_private; |
5138fde0 | 6712 | nfs_fattr_init(data->res.fattr); |
26e976a8 | 6713 | data->timestamp = jiffies; |
fe650407 | 6714 | data->rpc_status = 0; |
ea7c38fe | 6715 | data->inode = nfs_igrab_and_active(inode); |
078000d0 TM |
6716 | if (data->inode || issync) { |
6717 | data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, | |
6718 | cred); | |
1c5bd76d TM |
6719 | if (data->lr.roc) { |
6720 | data->args.lr_args = &data->lr.arg; | |
6721 | data->res.lr_res = &data->lr.res; | |
6722 | } | |
6723 | } | |
fe650407 | 6724 | |
dfe1fe75 TM |
6725 | if (!data->inode) |
6726 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, | |
6727 | 1); | |
6728 | else | |
6729 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, | |
6730 | 0); | |
c970aa85 | 6731 | task_setup_data.callback_data = data; |
1174dd1f TM |
6732 | msg.rpc_argp = &data->args; |
6733 | msg.rpc_resp = &data->res; | |
c970aa85 | 6734 | task = rpc_run_task(&task_setup_data); |
7a1218a2 | 6735 | if (IS_ERR(task)) |
fe650407 | 6736 | return PTR_ERR(task); |
e6f81075 TM |
6737 | if (!issync) |
6738 | goto out; | |
820bf85c | 6739 | status = rpc_wait_for_completion_task(task); |
e6f81075 TM |
6740 | if (status != 0) |
6741 | goto out; | |
6742 | status = data->rpc_status; | |
e6f81075 | 6743 | out: |
e6b3c4db | 6744 | rpc_put_task(task); |
fe650407 | 6745 | return status; |
1da177e4 LT |
6746 | } |
6747 | ||
a52458b4 | 6748 | int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync) |
1da177e4 LT |
6749 | { |
6750 | struct nfs_server *server = NFS_SERVER(inode); | |
6751 | struct nfs4_exception exception = { }; | |
6752 | int err; | |
6753 | do { | |
e6f81075 | 6754 | err = _nfs4_proc_delegreturn(inode, cred, stateid, issync); |
48c9579a | 6755 | trace_nfs4_delegreturn(inode, stateid, err); |
1da177e4 LT |
6756 | switch (err) { |
6757 | case -NFS4ERR_STALE_STATEID: | |
6758 | case -NFS4ERR_EXPIRED: | |
1da177e4 LT |
6759 | case 0: |
6760 | return 0; | |
6761 | } | |
6762 | err = nfs4_handle_exception(server, err, &exception); | |
6763 | } while (exception.retry); | |
6764 | return err; | |
6765 | } | |
6766 | ||
1da177e4 LT |
6767 | static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
6768 | { | |
6769 | struct inode *inode = state->inode; | |
6770 | struct nfs_server *server = NFS_SERVER(inode); | |
7539bbab | 6771 | struct nfs_client *clp = server->nfs_client; |
911d1aaf | 6772 | struct nfs_lockt_args arg = { |
1da177e4 | 6773 | .fh = NFS_FH(inode), |
911d1aaf | 6774 | .fl = request, |
1da177e4 | 6775 | }; |
911d1aaf TM |
6776 | struct nfs_lockt_res res = { |
6777 | .denied = request, | |
1da177e4 LT |
6778 | }; |
6779 | struct rpc_message msg = { | |
6780 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT], | |
d9b67e1e AS |
6781 | .rpc_argp = &arg, |
6782 | .rpc_resp = &res, | |
1da177e4 LT |
6783 | .rpc_cred = state->owner->so_cred, |
6784 | }; | |
1da177e4 LT |
6785 | struct nfs4_lock_state *lsp; |
6786 | int status; | |
6787 | ||
911d1aaf | 6788 | arg.lock_owner.clientid = clp->cl_clientid; |
8d0a8a9d TM |
6789 | status = nfs4_set_lock_state(state, request); |
6790 | if (status != 0) | |
6791 | goto out; | |
6792 | lsp = request->fl_u.nfs4_fl.owner; | |
48c22eb2 | 6793 | arg.lock_owner.id = lsp->ls_seqid.owner_id; |
d035c36c | 6794 | arg.lock_owner.s_dev = server->s_dev; |
7c513058 | 6795 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
911d1aaf TM |
6796 | switch (status) { |
6797 | case 0: | |
dd1fac6a | 6798 | request->c.flc_type = F_UNLCK; |
911d1aaf TM |
6799 | break; |
6800 | case -NFS4ERR_DENIED: | |
6801 | status = 0; | |
1da177e4 | 6802 | } |
70cc6487 | 6803 | request->fl_ops->fl_release_private(request); |
a6f951dd | 6804 | request->fl_ops = NULL; |
8d0a8a9d | 6805 | out: |
1da177e4 LT |
6806 | return status; |
6807 | } | |
6808 | ||
6809 | static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) | |
6810 | { | |
0688e64b TM |
6811 | struct nfs4_exception exception = { |
6812 | .interruptible = true, | |
6813 | }; | |
1da177e4 LT |
6814 | int err; |
6815 | ||
6816 | do { | |
d1b748a5 TM |
6817 | err = _nfs4_proc_getlk(state, cmd, request); |
6818 | trace_nfs4_get_lock(request, state, cmd, err); | |
6819 | err = nfs4_handle_exception(NFS_SERVER(state->inode), err, | |
1da177e4 LT |
6820 | &exception); |
6821 | } while (exception.retry); | |
6822 | return err; | |
6823 | } | |
6824 | ||
32c6e7ee TM |
6825 | /* |
6826 | * Update the seqid of a lock stateid after receiving | |
6827 | * NFS4ERR_OLD_STATEID | |
6828 | */ | |
6829 | static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst, | |
6830 | struct nfs4_lock_state *lsp) | |
6831 | { | |
6832 | struct nfs4_state *state = lsp->ls_state; | |
6833 | bool ret = false; | |
6834 | ||
6835 | spin_lock(&state->state_lock); | |
6836 | if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid)) | |
6837 | goto out; | |
6838 | if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst)) | |
6839 | nfs4_stateid_seqid_inc(dst); | |
6840 | else | |
6841 | dst->seqid = lsp->ls_stateid.seqid; | |
6842 | ret = true; | |
6843 | out: | |
6844 | spin_unlock(&state->state_lock); | |
6845 | return ret; | |
6846 | } | |
6847 | ||
6848 | static bool nfs4_sync_lock_stateid(nfs4_stateid *dst, | |
6849 | struct nfs4_lock_state *lsp) | |
6850 | { | |
6851 | struct nfs4_state *state = lsp->ls_state; | |
6852 | bool ret; | |
6853 | ||
6854 | spin_lock(&state->state_lock); | |
6855 | ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid); | |
6856 | nfs4_stateid_copy(dst, &lsp->ls_stateid); | |
6857 | spin_unlock(&state->state_lock); | |
6858 | return ret; | |
6859 | } | |
6860 | ||
faf5f49c | 6861 | struct nfs4_unlockdata { |
911d1aaf TM |
6862 | struct nfs_locku_args arg; |
6863 | struct nfs_locku_res res; | |
faf5f49c TM |
6864 | struct nfs4_lock_state *lsp; |
6865 | struct nfs_open_context *ctx; | |
f30cb757 | 6866 | struct nfs_lock_context *l_ctx; |
911d1aaf | 6867 | struct file_lock fl; |
516285eb | 6868 | struct nfs_server *server; |
26e976a8 | 6869 | unsigned long timestamp; |
faf5f49c TM |
6870 | }; |
6871 | ||
911d1aaf TM |
6872 | static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl, |
6873 | struct nfs_open_context *ctx, | |
6874 | struct nfs4_lock_state *lsp, | |
6875 | struct nfs_seqid *seqid) | |
6876 | { | |
6877 | struct nfs4_unlockdata *p; | |
32c6e7ee TM |
6878 | struct nfs4_state *state = lsp->ls_state; |
6879 | struct inode *inode = state->inode; | |
911d1aaf | 6880 | |
da48f267 | 6881 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
911d1aaf TM |
6882 | if (p == NULL) |
6883 | return NULL; | |
6884 | p->arg.fh = NFS_FH(inode); | |
6885 | p->arg.fl = &p->fl; | |
6886 | p->arg.seqid = seqid; | |
c1d51931 | 6887 | p->res.seqid = seqid; |
911d1aaf | 6888 | p->lsp = lsp; |
911d1aaf TM |
6889 | /* Ensure we don't close file until we're done freeing locks! */ |
6890 | p->ctx = get_nfs_open_context(ctx); | |
f30cb757 | 6891 | p->l_ctx = nfs_get_lock_context(ctx); |
7b587e1a N |
6892 | locks_init_lock(&p->fl); |
6893 | locks_copy_lock(&p->fl, fl); | |
911d1aaf | 6894 | p->server = NFS_SERVER(inode); |
32c6e7ee TM |
6895 | spin_lock(&state->state_lock); |
6896 | nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid); | |
6897 | spin_unlock(&state->state_lock); | |
911d1aaf TM |
6898 | return p; |
6899 | } | |
6900 | ||
06f814a3 | 6901 | static void nfs4_locku_release_calldata(void *data) |
faf5f49c | 6902 | { |
963d8fe5 | 6903 | struct nfs4_unlockdata *calldata = data; |
911d1aaf | 6904 | nfs_free_seqid(calldata->arg.seqid); |
06f814a3 | 6905 | nfs4_put_lock_state(calldata->lsp); |
f30cb757 | 6906 | nfs_put_lock_context(calldata->l_ctx); |
06f814a3 TM |
6907 | put_nfs_open_context(calldata->ctx); |
6908 | kfree(calldata); | |
faf5f49c TM |
6909 | } |
6910 | ||
963d8fe5 | 6911 | static void nfs4_locku_done(struct rpc_task *task, void *data) |
faf5f49c | 6912 | { |
963d8fe5 | 6913 | struct nfs4_unlockdata *calldata = data; |
82571552 TM |
6914 | struct nfs4_exception exception = { |
6915 | .inode = calldata->lsp->ls_state->inode, | |
6916 | .stateid = &calldata->arg.stateid, | |
6917 | }; | |
faf5f49c | 6918 | |
14516c3a TM |
6919 | if (!nfs4_sequence_done(task, &calldata->res.seq_res)) |
6920 | return; | |
faf5f49c TM |
6921 | switch (task->tk_status) { |
6922 | case 0: | |
26e976a8 | 6923 | renew_lease(calldata->server, calldata->timestamp); |
75575ddf | 6924 | locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl); |
c69899a1 TM |
6925 | if (nfs4_update_lock_stateid(calldata->lsp, |
6926 | &calldata->res.stateid)) | |
6927 | break; | |
df561f66 | 6928 | fallthrough; |
26d36301 TM |
6929 | case -NFS4ERR_ADMIN_REVOKED: |
6930 | case -NFS4ERR_EXPIRED: | |
6931 | nfs4_free_revoked_stateid(calldata->server, | |
6932 | &calldata->arg.stateid, | |
6933 | task->tk_msg.rpc_cred); | |
df561f66 | 6934 | fallthrough; |
9e33bed5 | 6935 | case -NFS4ERR_BAD_STATEID: |
faf5f49c | 6936 | case -NFS4ERR_STALE_STATEID: |
32c6e7ee TM |
6937 | if (nfs4_sync_lock_stateid(&calldata->arg.stateid, |
6938 | calldata->lsp)) | |
6939 | rpc_restart_call_prepare(task); | |
6940 | break; | |
6941 | case -NFS4ERR_OLD_STATEID: | |
6942 | if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid, | |
6943 | calldata->lsp)) | |
425c1d4e | 6944 | rpc_restart_call_prepare(task); |
faf5f49c TM |
6945 | break; |
6946 | default: | |
82571552 TM |
6947 | task->tk_status = nfs4_async_handle_exception(task, |
6948 | calldata->server, task->tk_status, | |
6949 | &exception); | |
6950 | if (exception.retry) | |
d00c5d43 | 6951 | rpc_restart_call_prepare(task); |
faf5f49c | 6952 | } |
2b1bc308 | 6953 | nfs_release_seqid(calldata->arg.seqid); |
faf5f49c TM |
6954 | } |
6955 | ||
4ce70ada | 6956 | static void nfs4_locku_prepare(struct rpc_task *task, void *data) |
faf5f49c | 6957 | { |
4ce70ada | 6958 | struct nfs4_unlockdata *calldata = data; |
faf5f49c | 6959 | |
f30cb757 BC |
6960 | if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) && |
6961 | nfs_async_iocounter_wait(task, calldata->l_ctx)) | |
6962 | return; | |
6963 | ||
911d1aaf | 6964 | if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) |
c8da19b9 | 6965 | goto out_wait; |
795a88c9 | 6966 | if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) { |
963d8fe5 | 6967 | /* Note: exit _without_ running nfs4_locku_done */ |
c8da19b9 | 6968 | goto out_no_action; |
faf5f49c | 6969 | } |
26e976a8 | 6970 | calldata->timestamp = jiffies; |
42e1cca7 | 6971 | if (nfs4_setup_sequence(calldata->server->nfs_client, |
a893693c | 6972 | &calldata->arg.seq_args, |
2240a9e2 TM |
6973 | &calldata->res.seq_res, |
6974 | task) != 0) | |
6975 | nfs_release_seqid(calldata->arg.seqid); | |
c8da19b9 TM |
6976 | return; |
6977 | out_no_action: | |
6978 | task->tk_action = NULL; | |
6979 | out_wait: | |
6980 | nfs4_sequence_done(task, &calldata->res.seq_res); | |
faf5f49c TM |
6981 | } |
6982 | ||
963d8fe5 | 6983 | static const struct rpc_call_ops nfs4_locku_ops = { |
4ce70ada | 6984 | .rpc_call_prepare = nfs4_locku_prepare, |
963d8fe5 | 6985 | .rpc_call_done = nfs4_locku_done, |
06f814a3 | 6986 | .rpc_release = nfs4_locku_release_calldata, |
963d8fe5 TM |
6987 | }; |
6988 | ||
a5d16a4d TM |
6989 | static struct rpc_task *nfs4_do_unlck(struct file_lock *fl, |
6990 | struct nfs_open_context *ctx, | |
6991 | struct nfs4_lock_state *lsp, | |
6992 | struct nfs_seqid *seqid) | |
6993 | { | |
6994 | struct nfs4_unlockdata *data; | |
5138fde0 TM |
6995 | struct rpc_message msg = { |
6996 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU], | |
6997 | .rpc_cred = ctx->cred, | |
6998 | }; | |
c970aa85 TM |
6999 | struct rpc_task_setup task_setup_data = { |
7000 | .rpc_client = NFS_CLIENT(lsp->ls_state->inode), | |
5138fde0 | 7001 | .rpc_message = &msg, |
c970aa85 | 7002 | .callback_ops = &nfs4_locku_ops, |
101070ca | 7003 | .workqueue = nfsiod_workqueue, |
c970aa85 TM |
7004 | .flags = RPC_TASK_ASYNC, |
7005 | }; | |
85e39fee | 7006 | |
118f09ed | 7007 | if (nfs_server_capable(lsp->ls_state->inode, NFS_CAP_MOVEABLE)) |
85e39fee | 7008 | task_setup_data.flags |= RPC_TASK_MOVEABLE; |
a5d16a4d | 7009 | |
fa940720 WAA |
7010 | nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client, |
7011 | NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg); | |
7012 | ||
137d6aca FF |
7013 | /* Ensure this is an unlock - when canceling a lock, the |
7014 | * canceled lock is passed in, and it won't be an unlock. | |
7015 | */ | |
dd1fac6a JL |
7016 | fl->c.flc_type = F_UNLCK; |
7017 | if (fl->c.flc_flags & FL_CLOSE) | |
f30cb757 | 7018 | set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags); |
137d6aca | 7019 | |
a5d16a4d TM |
7020 | data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid); |
7021 | if (data == NULL) { | |
7022 | nfs_free_seqid(seqid); | |
7023 | return ERR_PTR(-ENOMEM); | |
7024 | } | |
7025 | ||
fba83f34 | 7026 | nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0); |
1174dd1f TM |
7027 | msg.rpc_argp = &data->arg; |
7028 | msg.rpc_resp = &data->res; | |
c970aa85 TM |
7029 | task_setup_data.callback_data = data; |
7030 | return rpc_run_task(&task_setup_data); | |
a5d16a4d TM |
7031 | } |
7032 | ||
faf5f49c TM |
7033 | static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request) |
7034 | { | |
65b62a29 TM |
7035 | struct inode *inode = state->inode; |
7036 | struct nfs4_state_owner *sp = state->owner; | |
7037 | struct nfs_inode *nfsi = NFS_I(inode); | |
911d1aaf | 7038 | struct nfs_seqid *seqid; |
1da177e4 | 7039 | struct nfs4_lock_state *lsp; |
06f814a3 | 7040 | struct rpc_task *task; |
b4019c0e | 7041 | struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); |
06f814a3 | 7042 | int status = 0; |
dd1fac6a | 7043 | unsigned char saved_flags = request->c.flc_flags; |
faf5f49c | 7044 | |
8d0a8a9d | 7045 | status = nfs4_set_lock_state(state, request); |
9b073574 | 7046 | /* Unlock _before_ we do the RPC call */ |
dd1fac6a | 7047 | request->c.flc_flags |= FL_EXISTS; |
65b62a29 TM |
7048 | /* Exclude nfs_delegation_claim_locks() */ |
7049 | mutex_lock(&sp->so_delegreturn_mutex); | |
7050 | /* Exclude nfs4_reclaim_open_stateid() - note nesting! */ | |
19e03c57 | 7051 | down_read(&nfsi->rwsem); |
75575ddf | 7052 | if (locks_lock_inode_wait(inode, request) == -ENOENT) { |
19e03c57 | 7053 | up_read(&nfsi->rwsem); |
65b62a29 | 7054 | mutex_unlock(&sp->so_delegreturn_mutex); |
9b073574 | 7055 | goto out; |
19e03c57 | 7056 | } |
ef8d98f2 N |
7057 | lsp = request->fl_u.nfs4_fl.owner; |
7058 | set_bit(NFS_LOCK_UNLOCKING, &lsp->ls_flags); | |
19e03c57 | 7059 | up_read(&nfsi->rwsem); |
65b62a29 | 7060 | mutex_unlock(&sp->so_delegreturn_mutex); |
8d0a8a9d | 7061 | if (status != 0) |
9b073574 TM |
7062 | goto out; |
7063 | /* Is this a delegated lock? */ | |
c5a2a15f TM |
7064 | if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0) |
7065 | goto out; | |
b4019c0e TM |
7066 | alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid; |
7067 | seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL); | |
9b073574 | 7068 | status = -ENOMEM; |
badc76dd | 7069 | if (IS_ERR(seqid)) |
9b073574 | 7070 | goto out; |
dd1fac6a JL |
7071 | task = nfs4_do_unlck(request, |
7072 | nfs_file_open_context(request->c.flc_file), | |
7073 | lsp, seqid); | |
a5d16a4d TM |
7074 | status = PTR_ERR(task); |
7075 | if (IS_ERR(task)) | |
9b073574 | 7076 | goto out; |
820bf85c | 7077 | status = rpc_wait_for_completion_task(task); |
e6b3c4db | 7078 | rpc_put_task(task); |
9b073574 | 7079 | out: |
dd1fac6a | 7080 | request->c.flc_flags = saved_flags; |
d1b748a5 | 7081 | trace_nfs4_unlock(request, state, F_SETLK, status); |
1da177e4 LT |
7082 | return status; |
7083 | } | |
7084 | ||
a5d16a4d TM |
7085 | struct nfs4_lockdata { |
7086 | struct nfs_lock_args arg; | |
7087 | struct nfs_lock_res res; | |
7088 | struct nfs4_lock_state *lsp; | |
7089 | struct nfs_open_context *ctx; | |
7090 | struct file_lock fl; | |
26e976a8 | 7091 | unsigned long timestamp; |
a5d16a4d TM |
7092 | int rpc_status; |
7093 | int cancelled; | |
66179efe | 7094 | struct nfs_server *server; |
a5d16a4d TM |
7095 | }; |
7096 | ||
7097 | static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl, | |
8535b2be TM |
7098 | struct nfs_open_context *ctx, struct nfs4_lock_state *lsp, |
7099 | gfp_t gfp_mask) | |
1da177e4 | 7100 | { |
a5d16a4d TM |
7101 | struct nfs4_lockdata *p; |
7102 | struct inode *inode = lsp->ls_state->inode; | |
1da177e4 | 7103 | struct nfs_server *server = NFS_SERVER(inode); |
b4019c0e | 7104 | struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); |
a5d16a4d | 7105 | |
8535b2be | 7106 | p = kzalloc(sizeof(*p), gfp_mask); |
a5d16a4d TM |
7107 | if (p == NULL) |
7108 | return NULL; | |
7109 | ||
7110 | p->arg.fh = NFS_FH(inode); | |
7111 | p->arg.fl = &p->fl; | |
8535b2be | 7112 | p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask); |
badc76dd | 7113 | if (IS_ERR(p->arg.open_seqid)) |
2f74c0a0 | 7114 | goto out_free; |
b4019c0e TM |
7115 | alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; |
7116 | p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask); | |
badc76dd | 7117 | if (IS_ERR(p->arg.lock_seqid)) |
2f74c0a0 | 7118 | goto out_free_seqid; |
7539bbab | 7119 | p->arg.lock_owner.clientid = server->nfs_client->cl_clientid; |
48c22eb2 | 7120 | p->arg.lock_owner.id = lsp->ls_seqid.owner_id; |
d035c36c | 7121 | p->arg.lock_owner.s_dev = server->s_dev; |
c1d51931 | 7122 | p->res.lock_seqid = p->arg.lock_seqid; |
a5d16a4d | 7123 | p->lsp = lsp; |
66179efe | 7124 | p->server = server; |
a5d16a4d | 7125 | p->ctx = get_nfs_open_context(ctx); |
7b587e1a N |
7126 | locks_init_lock(&p->fl); |
7127 | locks_copy_lock(&p->fl, fl); | |
a5d16a4d | 7128 | return p; |
2f74c0a0 TM |
7129 | out_free_seqid: |
7130 | nfs_free_seqid(p->arg.open_seqid); | |
a5d16a4d TM |
7131 | out_free: |
7132 | kfree(p); | |
7133 | return NULL; | |
7134 | } | |
7135 | ||
7136 | static void nfs4_lock_prepare(struct rpc_task *task, void *calldata) | |
7137 | { | |
7138 | struct nfs4_lockdata *data = calldata; | |
7139 | struct nfs4_state *state = data->lsp->ls_state; | |
06735b34 | 7140 | |
2f74c0a0 | 7141 | if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0) |
c8da19b9 | 7142 | goto out_wait; |
a5d16a4d | 7143 | /* Do we need to do an open_to_lock_owner? */ |
6b447539 | 7144 | if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) { |
8fe72bac | 7145 | if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) { |
2240a9e2 | 7146 | goto out_release_lock_seqid; |
8fe72bac | 7147 | } |
425c1d4e TM |
7148 | nfs4_stateid_copy(&data->arg.open_stateid, |
7149 | &state->open_stateid); | |
a5d16a4d | 7150 | data->arg.new_lock_owner = 1; |
c1d51931 | 7151 | data->res.open_seqid = data->arg.open_seqid; |
425c1d4e | 7152 | } else { |
2f74c0a0 | 7153 | data->arg.new_lock_owner = 0; |
425c1d4e TM |
7154 | nfs4_stateid_copy(&data->arg.lock_stateid, |
7155 | &data->lsp->ls_stateid); | |
7156 | } | |
5d422301 TM |
7157 | if (!nfs4_valid_open_stateid(state)) { |
7158 | data->rpc_status = -EBADF; | |
7159 | task->tk_action = NULL; | |
7160 | goto out_release_open_seqid; | |
7161 | } | |
26e976a8 | 7162 | data->timestamp = jiffies; |
42e1cca7 | 7163 | if (nfs4_setup_sequence(data->server->nfs_client, |
035168ab | 7164 | &data->arg.seq_args, |
2240a9e2 | 7165 | &data->res.seq_res, |
d9afbd1b | 7166 | task) == 0) |
66179efe | 7167 | return; |
5d422301 | 7168 | out_release_open_seqid: |
2240a9e2 TM |
7169 | nfs_release_seqid(data->arg.open_seqid); |
7170 | out_release_lock_seqid: | |
7171 | nfs_release_seqid(data->arg.lock_seqid); | |
c8da19b9 TM |
7172 | out_wait: |
7173 | nfs4_sequence_done(task, &data->res.seq_res); | |
86882c75 | 7174 | dprintk("%s: ret = %d\n", __func__, data->rpc_status); |
b257957e AB |
7175 | } |
7176 | ||
a5d16a4d TM |
7177 | static void nfs4_lock_done(struct rpc_task *task, void *calldata) |
7178 | { | |
7179 | struct nfs4_lockdata *data = calldata; | |
39071e6f | 7180 | struct nfs4_lock_state *lsp = data->lsp; |
a5d16a4d | 7181 | |
14516c3a TM |
7182 | if (!nfs4_sequence_done(task, &data->res.seq_res)) |
7183 | return; | |
66179efe | 7184 | |
a5d16a4d | 7185 | data->rpc_status = task->tk_status; |
425c1d4e TM |
7186 | switch (task->tk_status) { |
7187 | case 0: | |
5b4a82a0 BC |
7188 | renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)), |
7189 | data->timestamp); | |
a3cf9bca | 7190 | if (data->arg.new_lock && !data->cancelled) { |
dd1fac6a | 7191 | data->fl.c.flc_flags &= ~(FL_SLEEP | FL_ACCESS); |
a3cf9bca | 7192 | if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) |
6ea76bf5 | 7193 | goto out_restart; |
c69899a1 | 7194 | } |
39071e6f TM |
7195 | if (data->arg.new_lock_owner != 0) { |
7196 | nfs_confirm_seqid(&lsp->ls_seqid, 0); | |
7197 | nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid); | |
7198 | set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags); | |
6ea76bf5 TM |
7199 | } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid)) |
7200 | goto out_restart; | |
425c1d4e | 7201 | break; |
425c1d4e | 7202 | case -NFS4ERR_OLD_STATEID: |
1cbc11aa BC |
7203 | if (data->arg.new_lock_owner != 0 && |
7204 | nfs4_refresh_open_old_stateid(&data->arg.open_stateid, | |
7205 | lsp->ls_state)) | |
7206 | goto out_restart; | |
7207 | if (nfs4_refresh_lock_old_stateid(&data->arg.lock_stateid, lsp)) | |
7208 | goto out_restart; | |
7209 | fallthrough; | |
7210 | case -NFS4ERR_BAD_STATEID: | |
425c1d4e TM |
7211 | case -NFS4ERR_STALE_STATEID: |
7212 | case -NFS4ERR_EXPIRED: | |
7213 | if (data->arg.new_lock_owner != 0) { | |
6ea76bf5 | 7214 | if (!nfs4_stateid_match(&data->arg.open_stateid, |
425c1d4e | 7215 | &lsp->ls_state->open_stateid)) |
6ea76bf5 TM |
7216 | goto out_restart; |
7217 | } else if (!nfs4_stateid_match(&data->arg.lock_stateid, | |
425c1d4e | 7218 | &lsp->ls_stateid)) |
6ea76bf5 | 7219 | goto out_restart; |
a5d16a4d | 7220 | } |
a3cf9bca | 7221 | out_done: |
86882c75 | 7222 | dprintk("%s: ret = %d!\n", __func__, data->rpc_status); |
6ea76bf5 TM |
7223 | return; |
7224 | out_restart: | |
7225 | if (!data->cancelled) | |
7226 | rpc_restart_call_prepare(task); | |
7227 | goto out_done; | |
a5d16a4d TM |
7228 | } |
7229 | ||
7230 | static void nfs4_lock_release(void *calldata) | |
7231 | { | |
7232 | struct nfs4_lockdata *data = calldata; | |
7233 | ||
2f74c0a0 | 7234 | nfs_free_seqid(data->arg.open_seqid); |
6ea76bf5 | 7235 | if (data->cancelled && data->rpc_status == 0) { |
a5d16a4d TM |
7236 | struct rpc_task *task; |
7237 | task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp, | |
7238 | data->arg.lock_seqid); | |
7239 | if (!IS_ERR(task)) | |
bf294b41 | 7240 | rpc_put_task_async(task); |
3110ff80 | 7241 | dprintk("%s: cancelling lock!\n", __func__); |
a5d16a4d TM |
7242 | } else |
7243 | nfs_free_seqid(data->arg.lock_seqid); | |
7244 | nfs4_put_lock_state(data->lsp); | |
7245 | put_nfs_open_context(data->ctx); | |
7246 | kfree(data); | |
a5d16a4d TM |
7247 | } |
7248 | ||
7249 | static const struct rpc_call_ops nfs4_lock_ops = { | |
7250 | .rpc_call_prepare = nfs4_lock_prepare, | |
7251 | .rpc_call_done = nfs4_lock_done, | |
7252 | .rpc_release = nfs4_lock_release, | |
7253 | }; | |
7254 | ||
2bee72a6 TM |
7255 | static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error) |
7256 | { | |
2bee72a6 TM |
7257 | switch (error) { |
7258 | case -NFS4ERR_ADMIN_REVOKED: | |
d7f3e4bf | 7259 | case -NFS4ERR_EXPIRED: |
2bee72a6 | 7260 | case -NFS4ERR_BAD_STATEID: |
ecac799a | 7261 | lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; |
2bee72a6 | 7262 | if (new_lock_owner != 0 || |
795a88c9 | 7263 | test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) |
ecac799a | 7264 | nfs4_schedule_stateid_recovery(server, lsp->ls_state); |
a2c0b9e2 TM |
7265 | break; |
7266 | case -NFS4ERR_STALE_STATEID: | |
a2c0b9e2 | 7267 | lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; |
ecac799a | 7268 | nfs4_schedule_lease_recovery(server->nfs_client); |
8b98a532 | 7269 | } |
2bee72a6 TM |
7270 | } |
7271 | ||
afe6c27c | 7272 | static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type) |
a5d16a4d TM |
7273 | { |
7274 | struct nfs4_lockdata *data; | |
7275 | struct rpc_task *task; | |
5138fde0 TM |
7276 | struct rpc_message msg = { |
7277 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK], | |
7278 | .rpc_cred = state->owner->so_cred, | |
7279 | }; | |
c970aa85 TM |
7280 | struct rpc_task_setup task_setup_data = { |
7281 | .rpc_client = NFS_CLIENT(state->inode), | |
5138fde0 | 7282 | .rpc_message = &msg, |
c970aa85 | 7283 | .callback_ops = &nfs4_lock_ops, |
101070ca | 7284 | .workqueue = nfsiod_workqueue, |
61296507 | 7285 | .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, |
c970aa85 | 7286 | }; |
a5d16a4d | 7287 | int ret; |
85e39fee | 7288 | |
118f09ed | 7289 | if (nfs_server_capable(state->inode, NFS_CAP_MOVEABLE)) |
85e39fee | 7290 | task_setup_data.flags |= RPC_TASK_MOVEABLE; |
a5d16a4d | 7291 | |
dd1fac6a JL |
7292 | data = nfs4_alloc_lockdata(fl, |
7293 | nfs_file_open_context(fl->c.flc_file), | |
da48f267 | 7294 | fl->fl_u.nfs4_fl.owner, GFP_KERNEL); |
a5d16a4d TM |
7295 | if (data == NULL) |
7296 | return -ENOMEM; | |
7297 | if (IS_SETLKW(cmd)) | |
7298 | data->arg.block = 1; | |
fba83f34 AS |
7299 | nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, |
7300 | recovery_type > NFS_LOCK_NEW); | |
1174dd1f TM |
7301 | msg.rpc_argp = &data->arg; |
7302 | msg.rpc_resp = &data->res; | |
c970aa85 | 7303 | task_setup_data.callback_data = data; |
8fe72bac TM |
7304 | if (recovery_type > NFS_LOCK_NEW) { |
7305 | if (recovery_type == NFS_LOCK_RECLAIM) | |
7306 | data->arg.reclaim = NFS_LOCK_RECLAIM; | |
c69899a1 TM |
7307 | } else |
7308 | data->arg.new_lock = 1; | |
c970aa85 | 7309 | task = rpc_run_task(&task_setup_data); |
7a1218a2 | 7310 | if (IS_ERR(task)) |
a5d16a4d | 7311 | return PTR_ERR(task); |
820bf85c | 7312 | ret = rpc_wait_for_completion_task(task); |
a5d16a4d TM |
7313 | if (ret == 0) { |
7314 | ret = data->rpc_status; | |
2bee72a6 TM |
7315 | if (ret) |
7316 | nfs4_handle_setlk_error(data->server, data->lsp, | |
7317 | data->arg.new_lock_owner, ret); | |
a5d16a4d | 7318 | } else |
a7a3b1e9 | 7319 | data->cancelled = true; |
3d1a90ab | 7320 | trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret); |
e6b3c4db | 7321 | rpc_put_task(task); |
86882c75 | 7322 | dprintk("%s: ret = %d\n", __func__, ret); |
a5d16a4d | 7323 | return ret; |
1da177e4 LT |
7324 | } |
7325 | ||
7326 | static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request) | |
7327 | { | |
202b50dc | 7328 | struct nfs_server *server = NFS_SERVER(state->inode); |
05ffe24f TM |
7329 | struct nfs4_exception exception = { |
7330 | .inode = state->inode, | |
7331 | }; | |
202b50dc TM |
7332 | int err; |
7333 | ||
7334 | do { | |
42a2d13e TM |
7335 | /* Cache the lock if possible... */ |
7336 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) | |
7337 | return 0; | |
afe6c27c | 7338 | err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM); |
168667c4 | 7339 | if (err != -NFS4ERR_DELAY) |
202b50dc TM |
7340 | break; |
7341 | nfs4_handle_exception(server, err, &exception); | |
7342 | } while (exception.retry); | |
7343 | return err; | |
1da177e4 LT |
7344 | } |
7345 | ||
7346 | static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request) | |
7347 | { | |
202b50dc | 7348 | struct nfs_server *server = NFS_SERVER(state->inode); |
05ffe24f TM |
7349 | struct nfs4_exception exception = { |
7350 | .inode = state->inode, | |
7351 | }; | |
202b50dc TM |
7352 | int err; |
7353 | ||
6bfc93ef TM |
7354 | err = nfs4_set_lock_state(state, request); |
7355 | if (err != 0) | |
7356 | return err; | |
f6de7a39 | 7357 | if (!recover_lost_locks) { |
ef1820f9 N |
7358 | set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags); |
7359 | return 0; | |
7360 | } | |
202b50dc | 7361 | do { |
42a2d13e TM |
7362 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) |
7363 | return 0; | |
afe6c27c | 7364 | err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED); |
a9ed2e25 TM |
7365 | switch (err) { |
7366 | default: | |
7367 | goto out; | |
7368 | case -NFS4ERR_GRACE: | |
7369 | case -NFS4ERR_DELAY: | |
7370 | nfs4_handle_exception(server, err, &exception); | |
7371 | err = 0; | |
7372 | } | |
202b50dc | 7373 | } while (exception.retry); |
a9ed2e25 | 7374 | out: |
202b50dc | 7375 | return err; |
1da177e4 LT |
7376 | } |
7377 | ||
f062eb6c | 7378 | #if defined(CONFIG_NFS_V4_1) |
b01dd1d8 BS |
7379 | static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request) |
7380 | { | |
c5896fc8 TM |
7381 | struct nfs4_lock_state *lsp; |
7382 | int status; | |
b01dd1d8 | 7383 | |
c5896fc8 TM |
7384 | status = nfs4_set_lock_state(state, request); |
7385 | if (status != 0) | |
7386 | return status; | |
7387 | lsp = request->fl_u.nfs4_fl.owner; | |
7388 | if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) || | |
7389 | test_bit(NFS_LOCK_LOST, &lsp->ls_flags)) | |
7390 | return 0; | |
81b68de4 | 7391 | return nfs4_lock_expired(state, request); |
f062eb6c BS |
7392 | } |
7393 | #endif | |
7394 | ||
1da177e4 LT |
7395 | static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
7396 | { | |
19e03c57 | 7397 | struct nfs_inode *nfsi = NFS_I(state->inode); |
11476e9d | 7398 | struct nfs4_state_owner *sp = state->owner; |
dd1fac6a | 7399 | unsigned char flags = request->c.flc_flags; |
1ea67dbd | 7400 | int status; |
1da177e4 | 7401 | |
dd1fac6a | 7402 | request->c.flc_flags |= FL_ACCESS; |
75575ddf | 7403 | status = locks_lock_inode_wait(state->inode, request); |
01c3b861 TM |
7404 | if (status < 0) |
7405 | goto out; | |
11476e9d | 7406 | mutex_lock(&sp->so_delegreturn_mutex); |
19e03c57 | 7407 | down_read(&nfsi->rwsem); |
01c3b861 | 7408 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { |
01c3b861 | 7409 | /* Yes: cache locks! */ |
01c3b861 | 7410 | /* ...but avoid races with delegation recall... */ |
dd1fac6a | 7411 | request->c.flc_flags = flags & ~FL_SLEEP; |
75575ddf | 7412 | status = locks_lock_inode_wait(state->inode, request); |
c69899a1 | 7413 | up_read(&nfsi->rwsem); |
11476e9d | 7414 | mutex_unlock(&sp->so_delegreturn_mutex); |
9a99af49 | 7415 | goto out; |
9a99af49 | 7416 | } |
19e03c57 | 7417 | up_read(&nfsi->rwsem); |
11476e9d | 7418 | mutex_unlock(&sp->so_delegreturn_mutex); |
c69899a1 | 7419 | status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW); |
01c3b861 | 7420 | out: |
dd1fac6a | 7421 | request->c.flc_flags = flags; |
1da177e4 LT |
7422 | return status; |
7423 | } | |
7424 | ||
7425 | static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) | |
7426 | { | |
a1d0b5ee TM |
7427 | struct nfs4_exception exception = { |
7428 | .state = state, | |
05ffe24f | 7429 | .inode = state->inode, |
0688e64b | 7430 | .interruptible = true, |
a1d0b5ee | 7431 | }; |
1da177e4 LT |
7432 | int err; |
7433 | ||
7434 | do { | |
965b5d67 TM |
7435 | err = _nfs4_proc_setlk(state, cmd, request); |
7436 | if (err == -NFS4ERR_DENIED) | |
7437 | err = -EAGAIN; | |
1da177e4 | 7438 | err = nfs4_handle_exception(NFS_SERVER(state->inode), |
965b5d67 | 7439 | err, &exception); |
1da177e4 LT |
7440 | } while (exception.retry); |
7441 | return err; | |
7442 | } | |
7443 | ||
d2f3a7f9 JL |
7444 | #define NFS4_LOCK_MINTIMEOUT (1 * HZ) |
7445 | #define NFS4_LOCK_MAXTIMEOUT (30 * HZ) | |
7446 | ||
7447 | static int | |
a1d617d8 JL |
7448 | nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd, |
7449 | struct file_lock *request) | |
d2f3a7f9 JL |
7450 | { |
7451 | int status = -ERESTARTSYS; | |
7452 | unsigned long timeout = NFS4_LOCK_MINTIMEOUT; | |
7453 | ||
7454 | while(!signalled()) { | |
7455 | status = nfs4_proc_setlk(state, cmd, request); | |
7456 | if ((status != -EAGAIN) || IS_SETLK(cmd)) | |
7457 | break; | |
f5d39b02 PZ |
7458 | __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE); |
7459 | schedule_timeout(timeout); | |
d2f3a7f9 JL |
7460 | timeout *= 2; |
7461 | timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout); | |
7462 | status = -ERESTARTSYS; | |
7463 | } | |
7464 | return status; | |
7465 | } | |
7466 | ||
a1d617d8 JL |
7467 | #ifdef CONFIG_NFS_V4_1 |
7468 | struct nfs4_lock_waiter { | |
a1d617d8 | 7469 | struct inode *inode; |
86438186 TM |
7470 | struct nfs_lowner owner; |
7471 | wait_queue_entry_t wait; | |
a1d617d8 JL |
7472 | }; |
7473 | ||
7474 | static int | |
ac6424b9 | 7475 | nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key) |
a1d617d8 | 7476 | { |
86438186 TM |
7477 | struct nfs4_lock_waiter *waiter = |
7478 | container_of(wait, struct nfs4_lock_waiter, wait); | |
a1d617d8 | 7479 | |
57174593 JL |
7480 | /* NULL key means to wake up everyone */ |
7481 | if (key) { | |
7482 | struct cb_notify_lock_args *cbnl = key; | |
7483 | struct nfs_lowner *lowner = &cbnl->cbnl_owner, | |
86438186 | 7484 | *wowner = &waiter->owner; |
a1d617d8 | 7485 | |
57174593 JL |
7486 | /* Only wake if the callback was for the same owner. */ |
7487 | if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev) | |
7488 | return 0; | |
a1d617d8 | 7489 | |
57174593 JL |
7490 | /* Make sure it's for the right inode */ |
7491 | if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh)) | |
7492 | return 0; | |
57174593 | 7493 | } |
a1d617d8 | 7494 | |
86438186 | 7495 | return woken_wake_function(wait, mode, flags, key); |
a1d617d8 JL |
7496 | } |
7497 | ||
7498 | static int | |
7499 | nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) | |
7500 | { | |
a1d617d8 JL |
7501 | struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner; |
7502 | struct nfs_server *server = NFS_SERVER(state->inode); | |
7503 | struct nfs_client *clp = server->nfs_client; | |
7504 | wait_queue_head_t *q = &clp->cl_lock_waitq; | |
86438186 TM |
7505 | struct nfs4_lock_waiter waiter = { |
7506 | .inode = state->inode, | |
7507 | .owner = { .clientid = clp->cl_clientid, | |
7508 | .id = lsp->ls_seqid.owner_id, | |
7509 | .s_dev = server->s_dev }, | |
7510 | }; | |
7511 | int status; | |
a1d617d8 JL |
7512 | |
7513 | /* Don't bother with waitqueue if we don't expect a callback */ | |
7514 | if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags)) | |
7515 | return nfs4_retry_setlk_simple(state, cmd, request); | |
7516 | ||
86438186 TM |
7517 | init_wait(&waiter.wait); |
7518 | waiter.wait.func = nfs4_wake_lock_waiter; | |
7519 | add_wait_queue(q, &waiter.wait); | |
a1d617d8 | 7520 | |
86438186 | 7521 | do { |
a1d617d8 | 7522 | status = nfs4_proc_setlk(state, cmd, request); |
86438186 | 7523 | if (status != -EAGAIN || IS_SETLK(cmd)) |
a1d617d8 JL |
7524 | break; |
7525 | ||
7526 | status = -ERESTARTSYS; | |
f5d39b02 | 7527 | wait_woken(&waiter.wait, TASK_INTERRUPTIBLE|TASK_FREEZABLE, |
86438186 | 7528 | NFS4_LOCK_MAXTIMEOUT); |
86438186 TM |
7529 | } while (!signalled()); |
7530 | ||
7531 | remove_wait_queue(q, &waiter.wait); | |
a1d617d8 | 7532 | |
a1d617d8 JL |
7533 | return status; |
7534 | } | |
7535 | #else /* !CONFIG_NFS_V4_1 */ | |
7536 | static inline int | |
7537 | nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) | |
7538 | { | |
7539 | return nfs4_retry_setlk_simple(state, cmd, request); | |
7540 | } | |
7541 | #endif | |
7542 | ||
1da177e4 LT |
7543 | static int |
7544 | nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request) | |
7545 | { | |
7546 | struct nfs_open_context *ctx; | |
7547 | struct nfs4_state *state; | |
1da177e4 LT |
7548 | int status; |
7549 | ||
7550 | /* verify open state */ | |
cd3758e3 | 7551 | ctx = nfs_file_open_context(filp); |
1da177e4 LT |
7552 | state = ctx->state; |
7553 | ||
d953126a TM |
7554 | if (IS_GETLK(cmd)) { |
7555 | if (state != NULL) | |
7556 | return nfs4_proc_getlk(state, F_GETLK, request); | |
7557 | return 0; | |
7558 | } | |
1da177e4 LT |
7559 | |
7560 | if (!(IS_SETLK(cmd) || IS_SETLKW(cmd))) | |
7561 | return -EINVAL; | |
7562 | ||
d7c9616b | 7563 | if (lock_is_unlock(request)) { |
d953126a TM |
7564 | if (state != NULL) |
7565 | return nfs4_proc_unlck(state, cmd, request); | |
7566 | return 0; | |
7567 | } | |
1da177e4 | 7568 | |
d953126a TM |
7569 | if (state == NULL) |
7570 | return -ENOLCK; | |
1ea67dbd | 7571 | |
dd1fac6a | 7572 | if ((request->c.flc_flags & FL_POSIX) && |
1ea67dbd JL |
7573 | !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags)) |
7574 | return -ENOLCK; | |
7575 | ||
fcfa4470 BC |
7576 | /* |
7577 | * Don't rely on the VFS having checked the file open mode, | |
7578 | * since it won't do this for flock() locks. | |
7579 | */ | |
dd1fac6a | 7580 | switch (request->c.flc_type) { |
fcfa4470 BC |
7581 | case F_RDLCK: |
7582 | if (!(filp->f_mode & FMODE_READ)) | |
7583 | return -EBADF; | |
7584 | break; | |
7585 | case F_WRLCK: | |
7586 | if (!(filp->f_mode & FMODE_WRITE)) | |
7587 | return -EBADF; | |
7588 | } | |
7589 | ||
1ea67dbd JL |
7590 | status = nfs4_set_lock_state(state, request); |
7591 | if (status != 0) | |
7592 | return status; | |
7593 | ||
d2f3a7f9 | 7594 | return nfs4_retry_setlk(state, cmd, request); |
1da177e4 LT |
7595 | } |
7596 | ||
e93a5e93 TM |
7597 | static int nfs4_delete_lease(struct file *file, void **priv) |
7598 | { | |
7599 | return generic_setlease(file, F_UNLCK, NULL, priv); | |
7600 | } | |
7601 | ||
c69ff407 | 7602 | static int nfs4_add_lease(struct file *file, int arg, struct file_lease **lease, |
e93a5e93 TM |
7603 | void **priv) |
7604 | { | |
7605 | struct inode *inode = file_inode(file); | |
7606 | fmode_t type = arg == F_RDLCK ? FMODE_READ : FMODE_WRITE; | |
7607 | int ret; | |
7608 | ||
7609 | /* No delegation, no lease */ | |
7610 | if (!nfs4_have_delegation(inode, type)) | |
df2c7b95 | 7611 | return -EAGAIN; |
e93a5e93 TM |
7612 | ret = generic_setlease(file, arg, lease, priv); |
7613 | if (ret || nfs4_have_delegation(inode, type)) | |
7614 | return ret; | |
7615 | /* We raced with a delegation return */ | |
7616 | nfs4_delete_lease(file, priv); | |
df2c7b95 | 7617 | return -EAGAIN; |
e93a5e93 TM |
7618 | } |
7619 | ||
c69ff407 | 7620 | int nfs4_proc_setlease(struct file *file, int arg, struct file_lease **lease, |
e93a5e93 TM |
7621 | void **priv) |
7622 | { | |
7623 | switch (arg) { | |
7624 | case F_RDLCK: | |
7625 | case F_WRLCK: | |
7626 | return nfs4_add_lease(file, arg, lease, priv); | |
7627 | case F_UNLCK: | |
7628 | return nfs4_delete_lease(file, priv); | |
7629 | default: | |
7630 | return -EINVAL; | |
7631 | } | |
7632 | } | |
7633 | ||
db4f2e63 | 7634 | int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid) |
888e694c TM |
7635 | { |
7636 | struct nfs_server *server = NFS_SERVER(state->inode); | |
888e694c TM |
7637 | int err; |
7638 | ||
7639 | err = nfs4_set_lock_state(state, fl); | |
7640 | if (err != 0) | |
db4f2e63 | 7641 | return err; |
3d7a9520 OK |
7642 | do { |
7643 | err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW); | |
7644 | if (err != -NFS4ERR_DELAY) | |
7645 | break; | |
7646 | ssleep(1); | |
7647 | } while (err == -NFS4ERR_DELAY); | |
dce2630c | 7648 | return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err); |
888e694c | 7649 | } |
6b3b5496 | 7650 | |
cf470c3e TM |
7651 | struct nfs_release_lockowner_data { |
7652 | struct nfs4_lock_state *lsp; | |
5ae67c4f | 7653 | struct nfs_server *server; |
cf470c3e | 7654 | struct nfs_release_lockowner_args args; |
b7e63a10 | 7655 | struct nfs_release_lockowner_res res; |
60ea6812 | 7656 | unsigned long timestamp; |
cf470c3e TM |
7657 | }; |
7658 | ||
fbd4bfd1 CL |
7659 | static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata) |
7660 | { | |
7661 | struct nfs_release_lockowner_data *data = calldata; | |
5b53dc88 | 7662 | struct nfs_server *server = data->server; |
7981c8a6 AS |
7663 | nfs4_setup_sequence(server->nfs_client, &data->args.seq_args, |
7664 | &data->res.seq_res, task); | |
5b53dc88 | 7665 | data->args.lock_owner.clientid = server->nfs_client->cl_clientid; |
60ea6812 | 7666 | data->timestamp = jiffies; |
fbd4bfd1 CL |
7667 | } |
7668 | ||
7669 | static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata) | |
7670 | { | |
7671 | struct nfs_release_lockowner_data *data = calldata; | |
60ea6812 CL |
7672 | struct nfs_server *server = data->server; |
7673 | ||
b7e63a10 | 7674 | nfs40_sequence_done(task, &data->res.seq_res); |
60ea6812 CL |
7675 | |
7676 | switch (task->tk_status) { | |
7677 | case 0: | |
7678 | renew_lease(server, data->timestamp); | |
7679 | break; | |
7680 | case -NFS4ERR_STALE_CLIENTID: | |
7681 | case -NFS4ERR_EXPIRED: | |
5b53dc88 KM |
7682 | nfs4_schedule_lease_recovery(server->nfs_client); |
7683 | break; | |
60ea6812 CL |
7684 | case -NFS4ERR_LEASE_MOVED: |
7685 | case -NFS4ERR_DELAY: | |
8478eaa1 N |
7686 | if (nfs4_async_handle_error(task, server, |
7687 | NULL, NULL) == -EAGAIN) | |
60ea6812 CL |
7688 | rpc_restart_call_prepare(task); |
7689 | } | |
fbd4bfd1 CL |
7690 | } |
7691 | ||
d3c7b7cc TM |
7692 | static void nfs4_release_lockowner_release(void *calldata) |
7693 | { | |
cf470c3e | 7694 | struct nfs_release_lockowner_data *data = calldata; |
5ae67c4f | 7695 | nfs4_free_lock_state(data->server, data->lsp); |
d3c7b7cc TM |
7696 | kfree(calldata); |
7697 | } | |
7698 | ||
17280175 | 7699 | static const struct rpc_call_ops nfs4_release_lockowner_ops = { |
fbd4bfd1 CL |
7700 | .rpc_call_prepare = nfs4_release_lockowner_prepare, |
7701 | .rpc_call_done = nfs4_release_lockowner_done, | |
d3c7b7cc TM |
7702 | .rpc_release = nfs4_release_lockowner_release, |
7703 | }; | |
7704 | ||
f1cdae87 JL |
7705 | static void |
7706 | nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp) | |
d3c7b7cc | 7707 | { |
cf470c3e | 7708 | struct nfs_release_lockowner_data *data; |
d3c7b7cc TM |
7709 | struct rpc_message msg = { |
7710 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER], | |
7711 | }; | |
7712 | ||
7713 | if (server->nfs_client->cl_mvops->minor_version != 0) | |
f1cdae87 | 7714 | return; |
fbd4bfd1 | 7715 | |
da48f267 | 7716 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
cf470c3e | 7717 | if (!data) |
f1cdae87 | 7718 | return; |
cf470c3e | 7719 | data->lsp = lsp; |
5ae67c4f | 7720 | data->server = server; |
cf470c3e TM |
7721 | data->args.lock_owner.clientid = server->nfs_client->cl_clientid; |
7722 | data->args.lock_owner.id = lsp->ls_seqid.owner_id; | |
7723 | data->args.lock_owner.s_dev = server->s_dev; | |
fbd4bfd1 | 7724 | |
cf470c3e | 7725 | msg.rpc_argp = &data->args; |
b7e63a10 | 7726 | msg.rpc_resp = &data->res; |
fba83f34 | 7727 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0); |
cf470c3e | 7728 | rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data); |
d3c7b7cc TM |
7729 | } |
7730 | ||
aa1870af BF |
7731 | #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl" |
7732 | ||
d9a82a04 | 7733 | static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler, |
39f60c1c | 7734 | struct mnt_idmap *idmap, |
59301226 AV |
7735 | struct dentry *unused, struct inode *inode, |
7736 | const char *key, const void *buf, | |
7737 | size_t buflen, int flags) | |
6b3b5496 | 7738 | { |
7b8b44eb | 7739 | return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_ACL); |
6b3b5496 BF |
7740 | } |
7741 | ||
d9a82a04 | 7742 | static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler, |
b296821a AV |
7743 | struct dentry *unused, struct inode *inode, |
7744 | const char *key, void *buf, size_t buflen) | |
6b3b5496 | 7745 | { |
7b8b44eb | 7746 | return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_ACL); |
6b3b5496 BF |
7747 | } |
7748 | ||
764a5c6b | 7749 | static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry) |
6b3b5496 | 7750 | { |
7b8b44eb | 7751 | return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_ACL); |
6b3b5496 BF |
7752 | } |
7753 | ||
71342db0 TM |
7754 | #if defined(CONFIG_NFS_V4_1) |
7755 | #define XATTR_NAME_NFSV4_DACL "system.nfs4_dacl" | |
7756 | ||
7757 | static int nfs4_xattr_set_nfs4_dacl(const struct xattr_handler *handler, | |
39f60c1c | 7758 | struct mnt_idmap *idmap, |
71342db0 TM |
7759 | struct dentry *unused, struct inode *inode, |
7760 | const char *key, const void *buf, | |
7761 | size_t buflen, int flags) | |
7762 | { | |
7763 | return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_DACL); | |
7764 | } | |
7765 | ||
7766 | static int nfs4_xattr_get_nfs4_dacl(const struct xattr_handler *handler, | |
7767 | struct dentry *unused, struct inode *inode, | |
7768 | const char *key, void *buf, size_t buflen) | |
7769 | { | |
7770 | return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_DACL); | |
7771 | } | |
7772 | ||
7773 | static bool nfs4_xattr_list_nfs4_dacl(struct dentry *dentry) | |
7774 | { | |
7775 | return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_DACL); | |
7776 | } | |
7777 | ||
7778 | #define XATTR_NAME_NFSV4_SACL "system.nfs4_sacl" | |
7779 | ||
7780 | static int nfs4_xattr_set_nfs4_sacl(const struct xattr_handler *handler, | |
39f60c1c | 7781 | struct mnt_idmap *idmap, |
71342db0 TM |
7782 | struct dentry *unused, struct inode *inode, |
7783 | const char *key, const void *buf, | |
7784 | size_t buflen, int flags) | |
7785 | { | |
7786 | return nfs4_proc_set_acl(inode, buf, buflen, NFS4ACL_SACL); | |
7787 | } | |
7788 | ||
7789 | static int nfs4_xattr_get_nfs4_sacl(const struct xattr_handler *handler, | |
7790 | struct dentry *unused, struct inode *inode, | |
7791 | const char *key, void *buf, size_t buflen) | |
7792 | { | |
7793 | return nfs4_proc_get_acl(inode, buf, buflen, NFS4ACL_SACL); | |
7794 | } | |
7795 | ||
7796 | static bool nfs4_xattr_list_nfs4_sacl(struct dentry *dentry) | |
7797 | { | |
7798 | return nfs4_server_supports_acls(NFS_SB(dentry->d_sb), NFS4ACL_SACL); | |
7799 | } | |
7800 | ||
7801 | #endif | |
7802 | ||
c9bccef6 | 7803 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
c9bccef6 | 7804 | |
d9a82a04 | 7805 | static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler, |
39f60c1c | 7806 | struct mnt_idmap *idmap, |
59301226 AV |
7807 | struct dentry *unused, struct inode *inode, |
7808 | const char *key, const void *buf, | |
7809 | size_t buflen, int flags) | |
c9bccef6 DQ |
7810 | { |
7811 | if (security_ismaclabel(key)) | |
59301226 | 7812 | return nfs4_set_security_label(inode, buf, buflen); |
c9bccef6 DQ |
7813 | |
7814 | return -EOPNOTSUPP; | |
7815 | } | |
7816 | ||
d9a82a04 | 7817 | static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler, |
b296821a AV |
7818 | struct dentry *unused, struct inode *inode, |
7819 | const char *key, void *buf, size_t buflen) | |
c9bccef6 DQ |
7820 | { |
7821 | if (security_ismaclabel(key)) | |
b296821a | 7822 | return nfs4_get_security_label(inode, buf, buflen); |
c9bccef6 DQ |
7823 | return -EOPNOTSUPP; |
7824 | } | |
7825 | ||
c4803c49 AG |
7826 | static ssize_t |
7827 | nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len) | |
c9bccef6 | 7828 | { |
c4803c49 | 7829 | int len = 0; |
c9bccef6 | 7830 | |
c4803c49 AG |
7831 | if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) { |
7832 | len = security_inode_listsecurity(inode, list, list_len); | |
82c596eb | 7833 | if (len >= 0 && list_len && len > list_len) |
c4803c49 | 7834 | return -ERANGE; |
c9bccef6 DQ |
7835 | } |
7836 | return len; | |
7837 | } | |
7838 | ||
7839 | static const struct xattr_handler nfs4_xattr_nfs4_label_handler = { | |
7840 | .prefix = XATTR_SECURITY_PREFIX, | |
c9bccef6 DQ |
7841 | .get = nfs4_xattr_get_nfs4_label, |
7842 | .set = nfs4_xattr_set_nfs4_label, | |
7843 | }; | |
c9bccef6 | 7844 | |
c4803c49 AG |
7845 | #else |
7846 | ||
7847 | static ssize_t | |
7848 | nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len) | |
7849 | { | |
7850 | return 0; | |
7851 | } | |
7852 | ||
7853 | #endif | |
c9bccef6 | 7854 | |
012a211a FL |
7855 | #ifdef CONFIG_NFS_V4_2 |
7856 | static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler, | |
39f60c1c | 7857 | struct mnt_idmap *idmap, |
012a211a FL |
7858 | struct dentry *unused, struct inode *inode, |
7859 | const char *key, const void *buf, | |
7860 | size_t buflen, int flags) | |
7861 | { | |
b5e7b59c | 7862 | u32 mask; |
95ad37f9 | 7863 | int ret; |
012a211a FL |
7864 | |
7865 | if (!nfs_server_capable(inode, NFS_CAP_XATTR)) | |
7866 | return -EOPNOTSUPP; | |
7867 | ||
7868 | /* | |
7869 | * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA* | |
7870 | * flags right now. Handling of xattr operations use the normal | |
7871 | * file read/write permissions. | |
7872 | * | |
7873 | * Just in case the server has other ideas (which RFC 8276 allows), | |
7874 | * do a cached access check for the XA* flags to possibly avoid | |
7875 | * doing an RPC and getting EACCES back. | |
7876 | */ | |
b5e7b59c N |
7877 | if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) { |
7878 | if (!(mask & NFS_ACCESS_XAWRITE)) | |
012a211a FL |
7879 | return -EACCES; |
7880 | } | |
7881 | ||
95ad37f9 FL |
7882 | if (buf == NULL) { |
7883 | ret = nfs42_proc_removexattr(inode, key); | |
7884 | if (!ret) | |
7885 | nfs4_xattr_cache_remove(inode, key); | |
7886 | } else { | |
7887 | ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags); | |
7888 | if (!ret) | |
7889 | nfs4_xattr_cache_add(inode, key, buf, NULL, buflen); | |
7890 | } | |
7891 | ||
7892 | return ret; | |
012a211a FL |
7893 | } |
7894 | ||
7895 | static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler, | |
7896 | struct dentry *unused, struct inode *inode, | |
7897 | const char *key, void *buf, size_t buflen) | |
7898 | { | |
b5e7b59c | 7899 | u32 mask; |
95ad37f9 | 7900 | ssize_t ret; |
012a211a FL |
7901 | |
7902 | if (!nfs_server_capable(inode, NFS_CAP_XATTR)) | |
7903 | return -EOPNOTSUPP; | |
7904 | ||
b5e7b59c N |
7905 | if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) { |
7906 | if (!(mask & NFS_ACCESS_XAREAD)) | |
012a211a FL |
7907 | return -EACCES; |
7908 | } | |
7909 | ||
1f3208b2 | 7910 | ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE); |
95ad37f9 FL |
7911 | if (ret) |
7912 | return ret; | |
7913 | ||
7914 | ret = nfs4_xattr_cache_get(inode, key, buf, buflen); | |
7915 | if (ret >= 0 || (ret < 0 && ret != -ENOENT)) | |
7916 | return ret; | |
7917 | ||
7918 | ret = nfs42_proc_getxattr(inode, key, buf, buflen); | |
7919 | ||
7920 | return ret; | |
012a211a FL |
7921 | } |
7922 | ||
7923 | static ssize_t | |
7924 | nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len) | |
7925 | { | |
7926 | u64 cookie; | |
7927 | bool eof; | |
95ad37f9 | 7928 | ssize_t ret, size; |
012a211a FL |
7929 | char *buf; |
7930 | size_t buflen; | |
b5e7b59c | 7931 | u32 mask; |
012a211a FL |
7932 | |
7933 | if (!nfs_server_capable(inode, NFS_CAP_XATTR)) | |
7934 | return 0; | |
7935 | ||
b5e7b59c N |
7936 | if (!nfs_access_get_cached(inode, current_cred(), &mask, true)) { |
7937 | if (!(mask & NFS_ACCESS_XALIST)) | |
012a211a FL |
7938 | return 0; |
7939 | } | |
7940 | ||
1f3208b2 | 7941 | ret = nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE); |
95ad37f9 FL |
7942 | if (ret) |
7943 | return ret; | |
7944 | ||
7945 | ret = nfs4_xattr_cache_list(inode, list, list_len); | |
7946 | if (ret >= 0 || (ret < 0 && ret != -ENOENT)) | |
7947 | return ret; | |
7948 | ||
012a211a FL |
7949 | cookie = 0; |
7950 | eof = false; | |
7951 | buflen = list_len ? list_len : XATTR_LIST_MAX; | |
7952 | buf = list_len ? list : NULL; | |
7953 | size = 0; | |
7954 | ||
7955 | while (!eof) { | |
7956 | ret = nfs42_proc_listxattrs(inode, buf, buflen, | |
7957 | &cookie, &eof); | |
7958 | if (ret < 0) | |
7959 | return ret; | |
7960 | ||
7961 | if (list_len) { | |
7962 | buf += ret; | |
7963 | buflen -= ret; | |
7964 | } | |
7965 | size += ret; | |
7966 | } | |
7967 | ||
95ad37f9 FL |
7968 | if (list_len) |
7969 | nfs4_xattr_cache_set_list(inode, list, size); | |
7970 | ||
012a211a FL |
7971 | return size; |
7972 | } | |
7973 | ||
7974 | #else | |
7975 | ||
7976 | static ssize_t | |
7977 | nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len) | |
7978 | { | |
7979 | return 0; | |
7980 | } | |
7981 | #endif /* CONFIG_NFS_V4_2 */ | |
7982 | ||
533eb461 AA |
7983 | /* |
7984 | * nfs_fhget will use either the mounted_on_fileid or the fileid | |
7985 | */ | |
69aaaae1 TM |
7986 | static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr) |
7987 | { | |
533eb461 AA |
7988 | if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) || |
7989 | (fattr->valid & NFS_ATTR_FATTR_FILEID)) && | |
7990 | (fattr->valid & NFS_ATTR_FATTR_FSID) && | |
81934ddb | 7991 | (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS))) |
69aaaae1 TM |
7992 | return; |
7993 | ||
7994 | fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | | |
81934ddb | 7995 | NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL; |
69aaaae1 TM |
7996 | fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; |
7997 | fattr->nlink = 2; | |
7998 | } | |
7999 | ||
f05d147f BS |
8000 | static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, |
8001 | const struct qstr *name, | |
8002 | struct nfs4_fs_locations *fs_locations, | |
8003 | struct page *page) | |
683b57b4 TM |
8004 | { |
8005 | struct nfs_server *server = NFS_SERVER(dir); | |
c05cefcc | 8006 | u32 bitmask[3]; |
683b57b4 TM |
8007 | struct nfs4_fs_locations_arg args = { |
8008 | .dir_fh = NFS_FH(dir), | |
c228fd3a | 8009 | .name = name, |
683b57b4 TM |
8010 | .page = page, |
8011 | .bitmask = bitmask, | |
8012 | }; | |
22958463 BH |
8013 | struct nfs4_fs_locations_res res = { |
8014 | .fs_locations = fs_locations, | |
8015 | }; | |
683b57b4 TM |
8016 | struct rpc_message msg = { |
8017 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], | |
8018 | .rpc_argp = &args, | |
22958463 | 8019 | .rpc_resp = &res, |
683b57b4 TM |
8020 | }; |
8021 | int status; | |
8022 | ||
3110ff80 | 8023 | dprintk("%s: start\n", __func__); |
533eb461 | 8024 | |
c05cefcc CL |
8025 | bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS; |
8026 | bitmask[1] = nfs4_fattr_bitmap[1]; | |
8027 | ||
533eb461 AA |
8028 | /* Ask for the fileid of the absent filesystem if mounted_on_fileid |
8029 | * is not supported */ | |
8030 | if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID) | |
c05cefcc | 8031 | bitmask[0] &= ~FATTR4_WORD0_FILEID; |
533eb461 | 8032 | else |
c05cefcc | 8033 | bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID; |
533eb461 | 8034 | |
c3ed2227 | 8035 | nfs_fattr_init(fs_locations->fattr); |
683b57b4 | 8036 | fs_locations->server = server; |
830b8e33 | 8037 | fs_locations->nlocations = 0; |
f05d147f | 8038 | status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0); |
3110ff80 | 8039 | dprintk("%s: returned status = %d\n", __func__, status); |
683b57b4 TM |
8040 | return status; |
8041 | } | |
8042 | ||
f05d147f BS |
8043 | int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, |
8044 | const struct qstr *name, | |
8045 | struct nfs4_fs_locations *fs_locations, | |
8046 | struct page *page) | |
db0a9593 | 8047 | { |
0688e64b TM |
8048 | struct nfs4_exception exception = { |
8049 | .interruptible = true, | |
8050 | }; | |
db0a9593 BS |
8051 | int err; |
8052 | do { | |
078ea3df TM |
8053 | err = _nfs4_proc_fs_locations(client, dir, name, |
8054 | fs_locations, page); | |
8055 | trace_nfs4_get_fs_locations(dir, name, err); | |
8056 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
db0a9593 BS |
8057 | &exception); |
8058 | } while (exception.retry); | |
8059 | return err; | |
8060 | } | |
8061 | ||
b03d735b CL |
8062 | /* |
8063 | * This operation also signals the server that this client is | |
8064 | * performing migration recovery. The server can stop returning | |
8065 | * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is | |
8066 | * appended to this compound to identify the client ID which is | |
8067 | * performing recovery. | |
8068 | */ | |
1976b2b3 OK |
8069 | static int _nfs40_proc_get_locations(struct nfs_server *server, |
8070 | struct nfs_fh *fhandle, | |
b03d735b | 8071 | struct nfs4_fs_locations *locations, |
a52458b4 | 8072 | struct page *page, const struct cred *cred) |
b03d735b | 8073 | { |
b03d735b CL |
8074 | struct rpc_clnt *clnt = server->client; |
8075 | u32 bitmask[2] = { | |
8076 | [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, | |
8077 | }; | |
8078 | struct nfs4_fs_locations_arg args = { | |
8079 | .clientid = server->nfs_client->cl_clientid, | |
1976b2b3 | 8080 | .fh = fhandle, |
b03d735b CL |
8081 | .page = page, |
8082 | .bitmask = bitmask, | |
8083 | .migration = 1, /* skip LOOKUP */ | |
8084 | .renew = 1, /* append RENEW */ | |
8085 | }; | |
8086 | struct nfs4_fs_locations_res res = { | |
8087 | .fs_locations = locations, | |
8088 | .migration = 1, | |
8089 | .renew = 1, | |
8090 | }; | |
8091 | struct rpc_message msg = { | |
8092 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], | |
8093 | .rpc_argp = &args, | |
8094 | .rpc_resp = &res, | |
8095 | .rpc_cred = cred, | |
8096 | }; | |
8097 | unsigned long now = jiffies; | |
8098 | int status; | |
8099 | ||
c3ed2227 | 8100 | nfs_fattr_init(locations->fattr); |
b03d735b CL |
8101 | locations->server = server; |
8102 | locations->nlocations = 0; | |
8103 | ||
fba83f34 | 8104 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); |
b03d735b CL |
8105 | status = nfs4_call_sync_sequence(clnt, server, &msg, |
8106 | &args.seq_args, &res.seq_res); | |
8107 | if (status) | |
8108 | return status; | |
8109 | ||
8110 | renew_lease(server, now); | |
8111 | return 0; | |
8112 | } | |
8113 | ||
8114 | #ifdef CONFIG_NFS_V4_1 | |
8115 | ||
8116 | /* | |
8117 | * This operation also signals the server that this client is | |
8118 | * performing migration recovery. The server can stop asserting | |
8119 | * SEQ4_STATUS_LEASE_MOVED for this client. The client ID | |
8120 | * performing this operation is identified in the SEQUENCE | |
8121 | * operation in this compound. | |
8122 | * | |
8123 | * When the client supports GETATTR(fs_locations_info), it can | |
8124 | * be plumbed in here. | |
8125 | */ | |
1976b2b3 OK |
8126 | static int _nfs41_proc_get_locations(struct nfs_server *server, |
8127 | struct nfs_fh *fhandle, | |
b03d735b | 8128 | struct nfs4_fs_locations *locations, |
a52458b4 | 8129 | struct page *page, const struct cred *cred) |
b03d735b | 8130 | { |
b03d735b CL |
8131 | struct rpc_clnt *clnt = server->client; |
8132 | u32 bitmask[2] = { | |
8133 | [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, | |
8134 | }; | |
8135 | struct nfs4_fs_locations_arg args = { | |
1976b2b3 | 8136 | .fh = fhandle, |
b03d735b CL |
8137 | .page = page, |
8138 | .bitmask = bitmask, | |
8139 | .migration = 1, /* skip LOOKUP */ | |
8140 | }; | |
8141 | struct nfs4_fs_locations_res res = { | |
8142 | .fs_locations = locations, | |
8143 | .migration = 1, | |
8144 | }; | |
8145 | struct rpc_message msg = { | |
8146 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], | |
8147 | .rpc_argp = &args, | |
8148 | .rpc_resp = &res, | |
8149 | .rpc_cred = cred, | |
8150 | }; | |
b4be2c59 OK |
8151 | struct nfs4_call_sync_data data = { |
8152 | .seq_server = server, | |
8153 | .seq_args = &args.seq_args, | |
8154 | .seq_res = &res.seq_res, | |
8155 | }; | |
8156 | struct rpc_task_setup task_setup_data = { | |
8157 | .rpc_client = clnt, | |
8158 | .rpc_message = &msg, | |
8159 | .callback_ops = server->nfs_client->cl_mvops->call_sync_ops, | |
8160 | .callback_data = &data, | |
8161 | .flags = RPC_TASK_NO_ROUND_ROBIN, | |
8162 | }; | |
b03d735b CL |
8163 | int status; |
8164 | ||
c3ed2227 | 8165 | nfs_fattr_init(locations->fattr); |
b03d735b CL |
8166 | locations->server = server; |
8167 | locations->nlocations = 0; | |
8168 | ||
fba83f34 | 8169 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); |
b4be2c59 | 8170 | status = nfs4_call_sync_custom(&task_setup_data); |
b03d735b CL |
8171 | if (status == NFS4_OK && |
8172 | res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED) | |
8173 | status = -NFS4ERR_LEASE_MOVED; | |
8174 | return status; | |
8175 | } | |
8176 | ||
8177 | #endif /* CONFIG_NFS_V4_1 */ | |
8178 | ||
8179 | /** | |
8180 | * nfs4_proc_get_locations - discover locations for a migrated FSID | |
3d4a3940 YL |
8181 | * @server: pointer to nfs_server to process |
8182 | * @fhandle: pointer to the kernel NFS client file handle | |
b03d735b CL |
8183 | * @locations: result of query |
8184 | * @page: buffer | |
8185 | * @cred: credential to use for this operation | |
8186 | * | |
8187 | * Returns NFS4_OK on success, a negative NFS4ERR status code if the | |
8188 | * operation failed, or a negative errno if a local error occurred. | |
8189 | * | |
8190 | * On success, "locations" is filled in, but if the server has | |
8191 | * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not | |
8192 | * asserted. | |
8193 | * | |
8194 | * -NFS4ERR_LEASE_MOVED is returned if the server still has leases | |
8195 | * from this client that require migration recovery. | |
8196 | */ | |
1976b2b3 OK |
8197 | int nfs4_proc_get_locations(struct nfs_server *server, |
8198 | struct nfs_fh *fhandle, | |
b03d735b | 8199 | struct nfs4_fs_locations *locations, |
a52458b4 | 8200 | struct page *page, const struct cred *cred) |
b03d735b | 8201 | { |
b03d735b CL |
8202 | struct nfs_client *clp = server->nfs_client; |
8203 | const struct nfs4_mig_recovery_ops *ops = | |
8204 | clp->cl_mvops->mig_recovery_ops; | |
0688e64b TM |
8205 | struct nfs4_exception exception = { |
8206 | .interruptible = true, | |
8207 | }; | |
b03d735b CL |
8208 | int status; |
8209 | ||
8210 | dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__, | |
8211 | (unsigned long long)server->fsid.major, | |
8212 | (unsigned long long)server->fsid.minor, | |
8213 | clp->cl_hostname); | |
1976b2b3 | 8214 | nfs_display_fhandle(fhandle, __func__); |
b03d735b CL |
8215 | |
8216 | do { | |
1976b2b3 OK |
8217 | status = ops->get_locations(server, fhandle, locations, page, |
8218 | cred); | |
b03d735b CL |
8219 | if (status != -NFS4ERR_DELAY) |
8220 | break; | |
8221 | nfs4_handle_exception(server, status, &exception); | |
8222 | } while (exception.retry); | |
8223 | return status; | |
8224 | } | |
8225 | ||
44c99933 CL |
8226 | /* |
8227 | * This operation also signals the server that this client is | |
8228 | * performing "lease moved" recovery. The server can stop | |
8229 | * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation | |
8230 | * is appended to this compound to identify the client ID which is | |
8231 | * performing recovery. | |
8232 | */ | |
a52458b4 | 8233 | static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred) |
44c99933 CL |
8234 | { |
8235 | struct nfs_server *server = NFS_SERVER(inode); | |
8236 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; | |
8237 | struct rpc_clnt *clnt = server->client; | |
8238 | struct nfs4_fsid_present_arg args = { | |
8239 | .fh = NFS_FH(inode), | |
8240 | .clientid = clp->cl_clientid, | |
8241 | .renew = 1, /* append RENEW */ | |
8242 | }; | |
8243 | struct nfs4_fsid_present_res res = { | |
8244 | .renew = 1, | |
8245 | }; | |
8246 | struct rpc_message msg = { | |
8247 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT], | |
8248 | .rpc_argp = &args, | |
8249 | .rpc_resp = &res, | |
8250 | .rpc_cred = cred, | |
8251 | }; | |
8252 | unsigned long now = jiffies; | |
8253 | int status; | |
8254 | ||
8255 | res.fh = nfs_alloc_fhandle(); | |
8256 | if (res.fh == NULL) | |
8257 | return -ENOMEM; | |
8258 | ||
fba83f34 | 8259 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); |
44c99933 CL |
8260 | status = nfs4_call_sync_sequence(clnt, server, &msg, |
8261 | &args.seq_args, &res.seq_res); | |
8262 | nfs_free_fhandle(res.fh); | |
8263 | if (status) | |
8264 | return status; | |
8265 | ||
8266 | do_renew_lease(clp, now); | |
8267 | return 0; | |
8268 | } | |
8269 | ||
8270 | #ifdef CONFIG_NFS_V4_1 | |
8271 | ||
8272 | /* | |
8273 | * This operation also signals the server that this client is | |
8274 | * performing "lease moved" recovery. The server can stop asserting | |
8275 | * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing | |
8276 | * this operation is identified in the SEQUENCE operation in this | |
8277 | * compound. | |
8278 | */ | |
a52458b4 | 8279 | static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred) |
44c99933 CL |
8280 | { |
8281 | struct nfs_server *server = NFS_SERVER(inode); | |
8282 | struct rpc_clnt *clnt = server->client; | |
8283 | struct nfs4_fsid_present_arg args = { | |
8284 | .fh = NFS_FH(inode), | |
8285 | }; | |
8286 | struct nfs4_fsid_present_res res = { | |
8287 | }; | |
8288 | struct rpc_message msg = { | |
8289 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT], | |
8290 | .rpc_argp = &args, | |
8291 | .rpc_resp = &res, | |
8292 | .rpc_cred = cred, | |
8293 | }; | |
8294 | int status; | |
8295 | ||
8296 | res.fh = nfs_alloc_fhandle(); | |
8297 | if (res.fh == NULL) | |
8298 | return -ENOMEM; | |
8299 | ||
fba83f34 | 8300 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); |
44c99933 CL |
8301 | status = nfs4_call_sync_sequence(clnt, server, &msg, |
8302 | &args.seq_args, &res.seq_res); | |
8303 | nfs_free_fhandle(res.fh); | |
8304 | if (status == NFS4_OK && | |
8305 | res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED) | |
8306 | status = -NFS4ERR_LEASE_MOVED; | |
8307 | return status; | |
8308 | } | |
8309 | ||
8310 | #endif /* CONFIG_NFS_V4_1 */ | |
8311 | ||
8312 | /** | |
8313 | * nfs4_proc_fsid_present - Is this FSID present or absent on server? | |
8314 | * @inode: inode on FSID to check | |
8315 | * @cred: credential to use for this operation | |
8316 | * | |
8317 | * Server indicates whether the FSID is present, moved, or not | |
8318 | * recognized. This operation is necessary to clear a LEASE_MOVED | |
8319 | * condition for this client ID. | |
8320 | * | |
8321 | * Returns NFS4_OK if the FSID is present on this server, | |
8322 | * -NFS4ERR_MOVED if the FSID is no longer present, a negative | |
8323 | * NFS4ERR code if some error occurred on the server, or a | |
8324 | * negative errno if a local failure occurred. | |
8325 | */ | |
a52458b4 | 8326 | int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred) |
44c99933 CL |
8327 | { |
8328 | struct nfs_server *server = NFS_SERVER(inode); | |
8329 | struct nfs_client *clp = server->nfs_client; | |
8330 | const struct nfs4_mig_recovery_ops *ops = | |
8331 | clp->cl_mvops->mig_recovery_ops; | |
0688e64b TM |
8332 | struct nfs4_exception exception = { |
8333 | .interruptible = true, | |
8334 | }; | |
44c99933 CL |
8335 | int status; |
8336 | ||
8337 | dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__, | |
8338 | (unsigned long long)server->fsid.major, | |
8339 | (unsigned long long)server->fsid.minor, | |
8340 | clp->cl_hostname); | |
8341 | nfs_display_fhandle(NFS_FH(inode), __func__); | |
8342 | ||
8343 | do { | |
8344 | status = ops->fsid_present(inode, cred); | |
8345 | if (status != -NFS4ERR_DELAY) | |
8346 | break; | |
8347 | nfs4_handle_exception(server, status, &exception); | |
8348 | } while (exception.retry); | |
8349 | return status; | |
8350 | } | |
8351 | ||
302fad7b | 8352 | /* |
a5250def WAA |
8353 | * If 'use_integrity' is true and the state managment nfs_client |
8354 | * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient | |
8355 | * and the machine credential as per RFC3530bis and RFC5661 Security | |
8356 | * Considerations sections. Otherwise, just use the user cred with the | |
8357 | * filesystem's rpc_client. | |
5ec16a85 | 8358 | */ |
a5250def | 8359 | static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity) |
5a5ea0d4 BS |
8360 | { |
8361 | int status; | |
50493364 AS |
8362 | struct rpc_clnt *clnt = NFS_SERVER(dir)->client; |
8363 | struct nfs_client *clp = NFS_SERVER(dir)->nfs_client; | |
5a5ea0d4 BS |
8364 | struct nfs4_secinfo_arg args = { |
8365 | .dir_fh = NFS_FH(dir), | |
8366 | .name = name, | |
8367 | }; | |
8368 | struct nfs4_secinfo_res res = { | |
8369 | .flavors = flavors, | |
8370 | }; | |
8371 | struct rpc_message msg = { | |
8372 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO], | |
8373 | .rpc_argp = &args, | |
8374 | .rpc_resp = &res, | |
8375 | }; | |
50493364 AS |
8376 | struct nfs4_call_sync_data data = { |
8377 | .seq_server = NFS_SERVER(dir), | |
8378 | .seq_args = &args.seq_args, | |
8379 | .seq_res = &res.seq_res, | |
8380 | }; | |
8381 | struct rpc_task_setup task_setup = { | |
8382 | .rpc_client = clnt, | |
8383 | .rpc_message = &msg, | |
8384 | .callback_ops = clp->cl_mvops->call_sync_ops, | |
8385 | .callback_data = &data, | |
8386 | .flags = RPC_TASK_NO_ROUND_ROBIN, | |
8387 | }; | |
a52458b4 | 8388 | const struct cred *cred = NULL; |
a5250def WAA |
8389 | |
8390 | if (use_integrity) { | |
50493364 AS |
8391 | clnt = clp->cl_rpcclient; |
8392 | task_setup.rpc_client = clnt; | |
8393 | ||
8394 | cred = nfs4_get_clid_cred(clp); | |
7cb852df | 8395 | msg.rpc_cred = cred; |
a5250def | 8396 | } |
5a5ea0d4 BS |
8397 | |
8398 | dprintk("NFS call secinfo %s\n", name->name); | |
8b5bee2e | 8399 | |
50493364 AS |
8400 | nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg); |
8401 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0); | |
8402 | status = nfs4_call_sync_custom(&task_setup); | |
8b5bee2e | 8403 | |
5a5ea0d4 | 8404 | dprintk("NFS reply secinfo: %d\n", status); |
a5250def | 8405 | |
a52458b4 | 8406 | put_cred(cred); |
5a5ea0d4 BS |
8407 | return status; |
8408 | } | |
8409 | ||
72de53ec BS |
8410 | int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, |
8411 | struct nfs4_secinfo_flavors *flavors) | |
5a5ea0d4 | 8412 | { |
0688e64b TM |
8413 | struct nfs4_exception exception = { |
8414 | .interruptible = true, | |
8415 | }; | |
5a5ea0d4 BS |
8416 | int err; |
8417 | do { | |
a5250def WAA |
8418 | err = -NFS4ERR_WRONGSEC; |
8419 | ||
8420 | /* try to use integrity protection with machine cred */ | |
8421 | if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client)) | |
8422 | err = _nfs4_proc_secinfo(dir, name, flavors, true); | |
8423 | ||
8424 | /* | |
8425 | * if unable to use integrity protection, or SECINFO with | |
8426 | * integrity protection returns NFS4ERR_WRONGSEC (which is | |
8427 | * disallowed by spec, but exists in deployed servers) use | |
8428 | * the current filesystem's rpc_client and the user cred. | |
8429 | */ | |
8430 | if (err == -NFS4ERR_WRONGSEC) | |
8431 | err = _nfs4_proc_secinfo(dir, name, flavors, false); | |
8432 | ||
078ea3df TM |
8433 | trace_nfs4_secinfo(dir, name, err); |
8434 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
5a5ea0d4 BS |
8435 | &exception); |
8436 | } while (exception.retry); | |
8437 | return err; | |
8438 | } | |
8439 | ||
557134a3 | 8440 | #ifdef CONFIG_NFS_V4_1 |
357f54d6 AA |
8441 | /* |
8442 | * Check the exchange flags returned by the server for invalid flags, having | |
8443 | * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or | |
8444 | * DS flags set. | |
8445 | */ | |
8c39076c | 8446 | static int nfs4_check_cl_exchange_flags(u32 flags, u32 version) |
357f54d6 | 8447 | { |
8c39076c OK |
8448 | if (version >= 2 && (flags & ~EXCHGID4_2_FLAG_MASK_R)) |
8449 | goto out_inval; | |
8450 | else if (version < 2 && (flags & ~EXCHGID4_FLAG_MASK_R)) | |
357f54d6 AA |
8451 | goto out_inval; |
8452 | if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) && | |
8453 | (flags & EXCHGID4_FLAG_USE_NON_PNFS)) | |
8454 | goto out_inval; | |
8455 | if (!(flags & (EXCHGID4_FLAG_MASK_PNFS))) | |
8456 | goto out_inval; | |
8457 | return NFS_OK; | |
8458 | out_inval: | |
8459 | return -NFS4ERR_INVAL; | |
8460 | } | |
8461 | ||
78fe0f41 | 8462 | static bool |
79d4e1f0 CL |
8463 | nfs41_same_server_scope(struct nfs41_server_scope *a, |
8464 | struct nfs41_server_scope *b) | |
78fe0f41 | 8465 | { |
49ad0145 AS |
8466 | if (a->server_scope_sz != b->server_scope_sz) |
8467 | return false; | |
8468 | return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0; | |
78fe0f41 WAA |
8469 | } |
8470 | ||
02a95dee AA |
8471 | static void |
8472 | nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata) | |
8473 | { | |
5c441544 | 8474 | struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp; |
dff58530 | 8475 | struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp; |
5c441544 TM |
8476 | struct nfs_client *clp = args->client; |
8477 | ||
8478 | switch (task->tk_status) { | |
8479 | case -NFS4ERR_BADSESSION: | |
8480 | case -NFS4ERR_DEADSESSION: | |
8481 | nfs4_schedule_session_recovery(clp->cl_session, | |
8482 | task->tk_status); | |
1d15d121 | 8483 | return; |
5c441544 | 8484 | } |
dff58530 OK |
8485 | if (args->dir == NFS4_CDFC4_FORE_OR_BOTH && |
8486 | res->dir != NFS4_CDFS4_BOTH) { | |
8487 | rpc_task_close_connection(task); | |
8488 | if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES) | |
8489 | rpc_restart_call(task); | |
8490 | } | |
02a95dee AA |
8491 | } |
8492 | ||
8493 | static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = { | |
1c709b76 | 8494 | .rpc_call_done = nfs4_bind_one_conn_to_session_done, |
02a95dee AA |
8495 | }; |
8496 | ||
7c44f1ae | 8497 | /* |
d9ddbf5d | 8498 | * nfs4_proc_bind_one_conn_to_session() |
7c44f1ae WAA |
8499 | * |
8500 | * The 4.1 client currently uses the same TCP connection for the | |
8501 | * fore and backchannel. | |
8502 | */ | |
d9ddbf5d TM |
8503 | static |
8504 | int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt, | |
8505 | struct rpc_xprt *xprt, | |
8506 | struct nfs_client *clp, | |
a52458b4 | 8507 | const struct cred *cred) |
7c44f1ae WAA |
8508 | { |
8509 | int status; | |
71a097c6 TM |
8510 | struct nfs41_bind_conn_to_session_args args = { |
8511 | .client = clp, | |
8512 | .dir = NFS4_CDFC4_FORE_OR_BOTH, | |
dff58530 | 8513 | .retries = 0, |
71a097c6 | 8514 | }; |
7c44f1ae WAA |
8515 | struct nfs41_bind_conn_to_session_res res; |
8516 | struct rpc_message msg = { | |
8517 | .rpc_proc = | |
8518 | &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION], | |
71a097c6 | 8519 | .rpc_argp = &args, |
7c44f1ae | 8520 | .rpc_resp = &res, |
2cf047c9 | 8521 | .rpc_cred = cred, |
7c44f1ae | 8522 | }; |
d9ddbf5d TM |
8523 | struct rpc_task_setup task_setup_data = { |
8524 | .rpc_client = clnt, | |
8525 | .rpc_xprt = xprt, | |
02a95dee | 8526 | .callback_ops = &nfs4_bind_one_conn_to_session_ops, |
d9ddbf5d TM |
8527 | .rpc_message = &msg, |
8528 | .flags = RPC_TASK_TIMEOUT, | |
8529 | }; | |
8530 | struct rpc_task *task; | |
7c44f1ae | 8531 | |
71a097c6 TM |
8532 | nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id); |
8533 | if (!(clp->cl_session->flags & SESSION4_BACK_CHAN)) | |
8534 | args.dir = NFS4_CDFC4_FORE; | |
7c44f1ae | 8535 | |
d9ddbf5d TM |
8536 | /* Do not set the backchannel flag unless this is clnt->cl_xprt */ |
8537 | if (xprt != rcu_access_pointer(clnt->cl_xprt)) | |
8538 | args.dir = NFS4_CDFC4_FORE; | |
8539 | ||
8540 | task = rpc_run_task(&task_setup_data); | |
8541 | if (!IS_ERR(task)) { | |
8542 | status = task->tk_status; | |
8543 | rpc_put_task(task); | |
8544 | } else | |
8545 | status = PTR_ERR(task); | |
c6d01c6f | 8546 | trace_nfs4_bind_conn_to_session(clp, status); |
7c44f1ae | 8547 | if (status == 0) { |
71a097c6 | 8548 | if (memcmp(res.sessionid.data, |
7c44f1ae WAA |
8549 | clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) { |
8550 | dprintk("NFS: %s: Session ID mismatch\n", __func__); | |
c7ae7639 | 8551 | return -EIO; |
7c44f1ae | 8552 | } |
71a097c6 | 8553 | if ((res.dir & args.dir) != res.dir || res.dir == 0) { |
7c44f1ae WAA |
8554 | dprintk("NFS: %s: Unexpected direction from server\n", |
8555 | __func__); | |
c7ae7639 | 8556 | return -EIO; |
7c44f1ae | 8557 | } |
71a097c6 | 8558 | if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) { |
7c44f1ae WAA |
8559 | dprintk("NFS: %s: Server returned RDMA mode = true\n", |
8560 | __func__); | |
c7ae7639 | 8561 | return -EIO; |
7c44f1ae WAA |
8562 | } |
8563 | } | |
c7ae7639 | 8564 | |
7c44f1ae WAA |
8565 | return status; |
8566 | } | |
8567 | ||
d9ddbf5d TM |
8568 | struct rpc_bind_conn_calldata { |
8569 | struct nfs_client *clp; | |
a52458b4 | 8570 | const struct cred *cred; |
d9ddbf5d TM |
8571 | }; |
8572 | ||
8573 | static int | |
8574 | nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt, | |
8575 | struct rpc_xprt *xprt, | |
8576 | void *calldata) | |
8577 | { | |
8578 | struct rpc_bind_conn_calldata *p = calldata; | |
8579 | ||
8580 | return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred); | |
8581 | } | |
8582 | ||
a52458b4 | 8583 | int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred) |
d9ddbf5d TM |
8584 | { |
8585 | struct rpc_bind_conn_calldata data = { | |
8586 | .clp = clp, | |
8587 | .cred = cred, | |
8588 | }; | |
8589 | return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient, | |
8590 | nfs4_proc_bind_conn_to_session_callback, &data); | |
8591 | } | |
8592 | ||
99fe60d0 | 8593 | /* |
fa940720 WAA |
8594 | * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map |
8595 | * and operations we'd like to see to enable certain features in the allow map | |
2031cd1a WAA |
8596 | */ |
8597 | static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = { | |
8598 | .how = SP4_MACH_CRED, | |
8599 | .enforce.u.words = { | |
8600 | [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) | | |
8601 | 1 << (OP_EXCHANGE_ID - 32) | | |
8602 | 1 << (OP_CREATE_SESSION - 32) | | |
8603 | 1 << (OP_DESTROY_SESSION - 32) | | |
8604 | 1 << (OP_DESTROY_CLIENTID - 32) | |
fa940720 WAA |
8605 | }, |
8606 | .allow.u.words = { | |
8607 | [0] = 1 << (OP_CLOSE) | | |
99ade3c7 | 8608 | 1 << (OP_OPEN_DOWNGRADE) | |
a0279625 | 8609 | 1 << (OP_LOCKU) | |
99ade3c7 | 8610 | 1 << (OP_DELEGRETURN) | |
a0279625 | 8611 | 1 << (OP_COMMIT), |
8b5bee2e | 8612 | [1] = 1 << (OP_SECINFO - 32) | |
3787d506 | 8613 | 1 << (OP_SECINFO_NO_NAME - 32) | |
99ade3c7 | 8614 | 1 << (OP_LAYOUTRETURN - 32) | |
3787d506 | 8615 | 1 << (OP_TEST_STATEID - 32) | |
a0279625 WAA |
8616 | 1 << (OP_FREE_STATEID - 32) | |
8617 | 1 << (OP_WRITE - 32) | |
2031cd1a WAA |
8618 | } |
8619 | }; | |
8620 | ||
8621 | /* | |
8622 | * Select the state protection mode for client `clp' given the server results | |
8623 | * from exchange_id in `sp'. | |
99fe60d0 | 8624 | * |
2031cd1a WAA |
8625 | * Returns 0 on success, negative errno otherwise. |
8626 | */ | |
8627 | static int nfs4_sp4_select_mode(struct nfs_client *clp, | |
8628 | struct nfs41_state_protection *sp) | |
8629 | { | |
8630 | static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = { | |
8631 | [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) | | |
8632 | 1 << (OP_EXCHANGE_ID - 32) | | |
8633 | 1 << (OP_CREATE_SESSION - 32) | | |
8634 | 1 << (OP_DESTROY_SESSION - 32) | | |
8635 | 1 << (OP_DESTROY_CLIENTID - 32) | |
8636 | }; | |
937e3133 | 8637 | unsigned long flags = 0; |
2031cd1a | 8638 | unsigned int i; |
937e3133 | 8639 | int ret = 0; |
2031cd1a WAA |
8640 | |
8641 | if (sp->how == SP4_MACH_CRED) { | |
8642 | /* Print state protect result */ | |
8643 | dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n"); | |
8644 | for (i = 0; i <= LAST_NFS4_OP; i++) { | |
8645 | if (test_bit(i, sp->enforce.u.longs)) | |
8646 | dfprintk(MOUNT, " enforce op %d\n", i); | |
8647 | if (test_bit(i, sp->allow.u.longs)) | |
8648 | dfprintk(MOUNT, " allow op %d\n", i); | |
8649 | } | |
8650 | ||
8651 | /* make sure nothing is on enforce list that isn't supported */ | |
8652 | for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) { | |
8653 | if (sp->enforce.u.words[i] & ~supported_enforce[i]) { | |
8654 | dfprintk(MOUNT, "sp4_mach_cred: disabled\n"); | |
937e3133 TM |
8655 | ret = -EINVAL; |
8656 | goto out; | |
2031cd1a WAA |
8657 | } |
8658 | } | |
8659 | ||
8660 | /* | |
8661 | * Minimal mode - state operations are allowed to use machine | |
8662 | * credential. Note this already happens by default, so the | |
8663 | * client doesn't have to do anything more than the negotiation. | |
8664 | * | |
8665 | * NOTE: we don't care if EXCHANGE_ID is in the list - | |
8666 | * we're already using the machine cred for exchange_id | |
8667 | * and will never use a different cred. | |
8668 | */ | |
8669 | if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) && | |
8670 | test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) && | |
8671 | test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) && | |
8672 | test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) { | |
8673 | dfprintk(MOUNT, "sp4_mach_cred:\n"); | |
8674 | dfprintk(MOUNT, " minimal mode enabled\n"); | |
937e3133 | 8675 | __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags); |
2031cd1a WAA |
8676 | } else { |
8677 | dfprintk(MOUNT, "sp4_mach_cred: disabled\n"); | |
937e3133 TM |
8678 | ret = -EINVAL; |
8679 | goto out; | |
2031cd1a | 8680 | } |
fa940720 WAA |
8681 | |
8682 | if (test_bit(OP_CLOSE, sp->allow.u.longs) && | |
99ade3c7 AE |
8683 | test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) && |
8684 | test_bit(OP_DELEGRETURN, sp->allow.u.longs) && | |
fa940720 WAA |
8685 | test_bit(OP_LOCKU, sp->allow.u.longs)) { |
8686 | dfprintk(MOUNT, " cleanup mode enabled\n"); | |
937e3133 | 8687 | __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags); |
fa940720 | 8688 | } |
8b5bee2e | 8689 | |
99ade3c7 AE |
8690 | if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) { |
8691 | dfprintk(MOUNT, " pnfs cleanup mode enabled\n"); | |
937e3133 | 8692 | __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags); |
99ade3c7 AE |
8693 | } |
8694 | ||
8b5bee2e WAA |
8695 | if (test_bit(OP_SECINFO, sp->allow.u.longs) && |
8696 | test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) { | |
8697 | dfprintk(MOUNT, " secinfo mode enabled\n"); | |
937e3133 | 8698 | __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags); |
8b5bee2e | 8699 | } |
3787d506 WAA |
8700 | |
8701 | if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) && | |
8702 | test_bit(OP_FREE_STATEID, sp->allow.u.longs)) { | |
8703 | dfprintk(MOUNT, " stateid mode enabled\n"); | |
937e3133 | 8704 | __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags); |
3787d506 | 8705 | } |
8c21c62c WAA |
8706 | |
8707 | if (test_bit(OP_WRITE, sp->allow.u.longs)) { | |
8708 | dfprintk(MOUNT, " write mode enabled\n"); | |
937e3133 | 8709 | __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags); |
8c21c62c WAA |
8710 | } |
8711 | ||
8712 | if (test_bit(OP_COMMIT, sp->allow.u.longs)) { | |
8713 | dfprintk(MOUNT, " commit mode enabled\n"); | |
937e3133 | 8714 | __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags); |
8c21c62c | 8715 | } |
2031cd1a | 8716 | } |
937e3133 TM |
8717 | out: |
8718 | clp->cl_sp4_flags = flags; | |
72bf75cf | 8719 | return ret; |
2031cd1a WAA |
8720 | } |
8721 | ||
8d89bd70 AA |
8722 | struct nfs41_exchange_id_data { |
8723 | struct nfs41_exchange_id_res res; | |
8724 | struct nfs41_exchange_id_args args; | |
8d89bd70 AA |
8725 | }; |
8726 | ||
8d89bd70 AA |
8727 | static void nfs4_exchange_id_release(void *data) |
8728 | { | |
8729 | struct nfs41_exchange_id_data *cdata = | |
8730 | (struct nfs41_exchange_id_data *)data; | |
8731 | ||
63513232 | 8732 | nfs_put_client(cdata->args.client); |
8d89bd70 AA |
8733 | kfree(cdata->res.impl_id); |
8734 | kfree(cdata->res.server_scope); | |
8735 | kfree(cdata->res.server_owner); | |
8736 | kfree(cdata); | |
8737 | } | |
8738 | ||
8739 | static const struct rpc_call_ops nfs4_exchange_id_call_ops = { | |
8d89bd70 AA |
8740 | .rpc_release = nfs4_exchange_id_release, |
8741 | }; | |
8742 | ||
2031cd1a WAA |
8743 | /* |
8744 | * _nfs4_proc_exchange_id() | |
6bbb4ae8 | 8745 | * |
2031cd1a | 8746 | * Wrapper for EXCHANGE_ID operation. |
99fe60d0 | 8747 | */ |
9c760d1f | 8748 | static struct rpc_task * |
a52458b4 | 8749 | nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred, |
ad0849a7 | 8750 | u32 sp4_how, struct rpc_xprt *xprt) |
99fe60d0 | 8751 | { |
99fe60d0 BH |
8752 | struct rpc_message msg = { |
8753 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID], | |
99fe60d0 BH |
8754 | .rpc_cred = cred, |
8755 | }; | |
8d89bd70 AA |
8756 | struct rpc_task_setup task_setup_data = { |
8757 | .rpc_client = clp->cl_rpcclient, | |
8758 | .callback_ops = &nfs4_exchange_id_call_ops, | |
8759 | .rpc_message = &msg, | |
5a0c257f | 8760 | .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN, |
8d89bd70 AA |
8761 | }; |
8762 | struct nfs41_exchange_id_data *calldata; | |
e917f0d1 | 8763 | int status; |
8d89bd70 | 8764 | |
212bf41d | 8765 | if (!refcount_inc_not_zero(&clp->cl_count)) |
9c760d1f | 8766 | return ERR_PTR(-EIO); |
8d89bd70 | 8767 | |
9c760d1f | 8768 | status = -ENOMEM; |
8d89bd70 | 8769 | calldata = kzalloc(sizeof(*calldata), GFP_NOFS); |
9c760d1f TM |
8770 | if (!calldata) |
8771 | goto out; | |
99fe60d0 | 8772 | |
fd40559c | 8773 | nfs4_init_boot_verifier(clp, &calldata->args.verifier); |
873e3851 JL |
8774 | |
8775 | status = nfs4_init_uniform_client_string(clp); | |
8776 | if (status) | |
8d89bd70 | 8777 | goto out_calldata; |
3a6bb738 | 8778 | |
8d89bd70 AA |
8779 | calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner), |
8780 | GFP_NOFS); | |
8781 | status = -ENOMEM; | |
8782 | if (unlikely(calldata->res.server_owner == NULL)) | |
8783 | goto out_calldata; | |
78fe0f41 | 8784 | |
8d89bd70 | 8785 | calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope), |
bbafffd2 | 8786 | GFP_NOFS); |
8d89bd70 | 8787 | if (unlikely(calldata->res.server_scope == NULL)) |
acdeb69d | 8788 | goto out_server_owner; |
78fe0f41 | 8789 | |
8d89bd70 AA |
8790 | calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS); |
8791 | if (unlikely(calldata->res.impl_id == NULL)) | |
7d2ed9ac | 8792 | goto out_server_scope; |
7d2ed9ac | 8793 | |
2031cd1a WAA |
8794 | switch (sp4_how) { |
8795 | case SP4_NONE: | |
8d89bd70 | 8796 | calldata->args.state_protect.how = SP4_NONE; |
2031cd1a WAA |
8797 | break; |
8798 | ||
8799 | case SP4_MACH_CRED: | |
8d89bd70 | 8800 | calldata->args.state_protect = nfs4_sp4_mach_cred_request; |
2031cd1a WAA |
8801 | break; |
8802 | ||
8803 | default: | |
8804 | /* unsupported! */ | |
8805 | WARN_ON_ONCE(1); | |
8806 | status = -EINVAL; | |
6b55970b | 8807 | goto out_impl_id; |
2031cd1a | 8808 | } |
ad0849a7 | 8809 | if (xprt) { |
ad0849a7 | 8810 | task_setup_data.rpc_xprt = xprt; |
d9cb7330 | 8811 | task_setup_data.flags |= RPC_TASK_SOFTCONN; |
fd40559c TM |
8812 | memcpy(calldata->args.verifier.data, clp->cl_confirm.data, |
8813 | sizeof(calldata->args.verifier.data)); | |
ad0849a7 | 8814 | } |
8d89bd70 | 8815 | calldata->args.client = clp; |
8d89bd70 | 8816 | calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER | |
bfab2817 TM |
8817 | EXCHGID4_FLAG_BIND_PRINC_STATEID; |
8818 | #ifdef CONFIG_NFS_V4_1_MIGRATION | |
8819 | calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR; | |
8d89bd70 | 8820 | #endif |
51d674a5 OK |
8821 | if (test_bit(NFS_CS_DS, &clp->cl_flags)) |
8822 | calldata->args.flags |= EXCHGID4_FLAG_USE_PNFS_DS; | |
8d89bd70 AA |
8823 | msg.rpc_argp = &calldata->args; |
8824 | msg.rpc_resp = &calldata->res; | |
8825 | task_setup_data.callback_data = calldata; | |
2031cd1a | 8826 | |
9c760d1f | 8827 | return rpc_run_task(&task_setup_data); |
8d89bd70 AA |
8828 | |
8829 | out_impl_id: | |
8830 | kfree(calldata->res.impl_id); | |
8831 | out_server_scope: | |
8832 | kfree(calldata->res.server_scope); | |
8833 | out_server_owner: | |
8834 | kfree(calldata->res.server_owner); | |
8835 | out_calldata: | |
8836 | kfree(calldata); | |
9c760d1f | 8837 | out: |
63513232 | 8838 | nfs_put_client(clp); |
9c760d1f TM |
8839 | return ERR_PTR(status); |
8840 | } | |
8841 | ||
8842 | /* | |
8843 | * _nfs4_proc_exchange_id() | |
8844 | * | |
8845 | * Wrapper for EXCHANGE_ID operation. | |
8846 | */ | |
a52458b4 | 8847 | static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred, |
9c760d1f TM |
8848 | u32 sp4_how) |
8849 | { | |
8850 | struct rpc_task *task; | |
8851 | struct nfs41_exchange_id_args *argp; | |
8852 | struct nfs41_exchange_id_res *resp; | |
7dc2993a | 8853 | unsigned long now = jiffies; |
9c760d1f TM |
8854 | int status; |
8855 | ||
8856 | task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL); | |
8857 | if (IS_ERR(task)) | |
8858 | return PTR_ERR(task); | |
8859 | ||
8860 | argp = task->tk_msg.rpc_argp; | |
8861 | resp = task->tk_msg.rpc_resp; | |
8862 | status = task->tk_status; | |
8863 | if (status != 0) | |
8864 | goto out; | |
8865 | ||
8c39076c OK |
8866 | status = nfs4_check_cl_exchange_flags(resp->flags, |
8867 | clp->cl_mvops->minor_version); | |
9c760d1f TM |
8868 | if (status != 0) |
8869 | goto out; | |
8870 | ||
8871 | status = nfs4_sp4_select_mode(clp, &resp->state_protect); | |
8872 | if (status != 0) | |
8873 | goto out; | |
8874 | ||
7dc2993a RM |
8875 | do_renew_lease(clp, now); |
8876 | ||
9c760d1f TM |
8877 | clp->cl_clientid = resp->clientid; |
8878 | clp->cl_exchange_flags = resp->flags; | |
8879 | clp->cl_seqid = resp->seqid; | |
8880 | /* Client ID is not confirmed */ | |
8881 | if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R)) | |
8882 | clear_bit(NFS4_SESSION_ESTABLISHED, | |
8883 | &clp->cl_session->session_state); | |
8884 | ||
8885 | if (clp->cl_serverscope != NULL && | |
8886 | !nfs41_same_server_scope(clp->cl_serverscope, | |
8887 | resp->server_scope)) { | |
8888 | dprintk("%s: server_scope mismatch detected\n", | |
8889 | __func__); | |
8890 | set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state); | |
8891 | } | |
8892 | ||
8893 | swap(clp->cl_serverowner, resp->server_owner); | |
8894 | swap(clp->cl_serverscope, resp->server_scope); | |
8895 | swap(clp->cl_implid, resp->impl_id); | |
8896 | ||
8897 | /* Save the EXCHANGE_ID verifier session trunk tests */ | |
8898 | memcpy(clp->cl_confirm.data, argp->verifier.data, | |
8899 | sizeof(clp->cl_confirm.data)); | |
8900 | out: | |
8901 | trace_nfs4_exchange_id(clp, status); | |
8902 | rpc_put_task(task); | |
8903 | return status; | |
99fe60d0 BH |
8904 | } |
8905 | ||
2031cd1a WAA |
8906 | /* |
8907 | * nfs4_proc_exchange_id() | |
8908 | * | |
8909 | * Returns zero, a negative errno, or a negative NFS4ERR status code. | |
8910 | * | |
8911 | * Since the clientid has expired, all compounds using sessions | |
8912 | * associated with the stale clientid will be returning | |
8913 | * NFS4ERR_BADSESSION in the sequence operation, and will therefore | |
8914 | * be in some phase of session reset. | |
8915 | * | |
8916 | * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used. | |
8917 | */ | |
a52458b4 | 8918 | int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred) |
2031cd1a WAA |
8919 | { |
8920 | rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor; | |
8921 | int status; | |
8922 | ||
8923 | /* try SP4_MACH_CRED if krb5i/p */ | |
8924 | if (authflavor == RPC_AUTH_GSS_KRB5I || | |
8925 | authflavor == RPC_AUTH_GSS_KRB5P) { | |
9c760d1f | 8926 | status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED); |
2031cd1a WAA |
8927 | if (!status) |
8928 | return 0; | |
8929 | } | |
8930 | ||
8931 | /* try SP4_NONE */ | |
9c760d1f | 8932 | return _nfs4_proc_exchange_id(clp, cred, SP4_NONE); |
2031cd1a WAA |
8933 | } |
8934 | ||
04fa2c6b AA |
8935 | /** |
8936 | * nfs4_test_session_trunk | |
8937 | * | |
8938 | * This is an add_xprt_test() test function called from | |
8939 | * rpc_clnt_setup_test_and_add_xprt. | |
8940 | * | |
8941 | * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt | |
8942 | * and is dereferrenced in nfs4_exchange_id_release | |
8943 | * | |
8944 | * Upon success, add the new transport to the rpc_clnt | |
8945 | * | |
8946 | * @clnt: struct rpc_clnt to get new transport | |
8947 | * @xprt: the rpc_xprt to test | |
8948 | * @data: call data for _nfs4_proc_exchange_id. | |
8949 | */ | |
10e037d1 | 8950 | void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt, |
04fa2c6b AA |
8951 | void *data) |
8952 | { | |
7e7ce2cc | 8953 | struct nfs4_add_xprt_data *adata = data; |
9c760d1f TM |
8954 | struct rpc_task *task; |
8955 | int status; | |
8956 | ||
04fa2c6b AA |
8957 | u32 sp4_how; |
8958 | ||
8959 | dprintk("--> %s try %s\n", __func__, | |
8960 | xprt->address_strings[RPC_DISPLAY_ADDR]); | |
8961 | ||
8962 | sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED); | |
8963 | ||
6bd1a77d | 8964 | try_again: |
04fa2c6b | 8965 | /* Test connection for session trunking. Async exchange_id call */ |
9c760d1f TM |
8966 | task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt); |
8967 | if (IS_ERR(task)) | |
10e037d1 | 8968 | return; |
9c760d1f TM |
8969 | |
8970 | status = task->tk_status; | |
7e5ae43b | 8971 | if (status == 0) { |
9c760d1f TM |
8972 | status = nfs4_detect_session_trunking(adata->clp, |
8973 | task->tk_msg.rpc_resp, xprt); | |
7e5ae43b OK |
8974 | trace_nfs4_trunked_exchange_id(adata->clp, |
8975 | xprt->address_strings[RPC_DISPLAY_ADDR], status); | |
8976 | } | |
10e037d1 S |
8977 | if (status == 0) |
8978 | rpc_clnt_xprt_switch_add_xprt(clnt, xprt); | |
6bd1a77d | 8979 | else if (status != -NFS4ERR_DELAY && rpc_clnt_xprt_switch_has_addr(clnt, |
e818bd08 OK |
8980 | (struct sockaddr *)&xprt->addr)) |
8981 | rpc_clnt_xprt_switch_remove_xprt(clnt, xprt); | |
10e037d1 | 8982 | |
9c760d1f | 8983 | rpc_put_task(task); |
6bd1a77d OK |
8984 | if (status == -NFS4ERR_DELAY) { |
8985 | ssleep(1); | |
8986 | goto try_again; | |
8987 | } | |
2031cd1a | 8988 | } |
04fa2c6b | 8989 | EXPORT_SYMBOL_GPL(nfs4_test_session_trunk); |
2031cd1a | 8990 | |
66245539 | 8991 | static int _nfs4_proc_destroy_clientid(struct nfs_client *clp, |
a52458b4 | 8992 | const struct cred *cred) |
66245539 TM |
8993 | { |
8994 | struct rpc_message msg = { | |
8995 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID], | |
8996 | .rpc_argp = clp, | |
8997 | .rpc_cred = cred, | |
8998 | }; | |
8999 | int status; | |
9000 | ||
5a0c257f N |
9001 | status = rpc_call_sync(clp->cl_rpcclient, &msg, |
9002 | RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); | |
c6d01c6f | 9003 | trace_nfs4_destroy_clientid(clp, status); |
66245539 | 9004 | if (status) |
02c67525 | 9005 | dprintk("NFS: Got error %d from the server %s on " |
66245539 TM |
9006 | "DESTROY_CLIENTID.", status, clp->cl_hostname); |
9007 | return status; | |
9008 | } | |
9009 | ||
9010 | static int nfs4_proc_destroy_clientid(struct nfs_client *clp, | |
a52458b4 | 9011 | const struct cred *cred) |
66245539 TM |
9012 | { |
9013 | unsigned int loop; | |
9014 | int ret; | |
9015 | ||
9016 | for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) { | |
9017 | ret = _nfs4_proc_destroy_clientid(clp, cred); | |
9018 | switch (ret) { | |
9019 | case -NFS4ERR_DELAY: | |
9020 | case -NFS4ERR_CLIENTID_BUSY: | |
9021 | ssleep(1); | |
9022 | break; | |
9023 | default: | |
9024 | return ret; | |
9025 | } | |
9026 | } | |
9027 | return 0; | |
9028 | } | |
9029 | ||
9030 | int nfs4_destroy_clientid(struct nfs_client *clp) | |
9031 | { | |
a52458b4 | 9032 | const struct cred *cred; |
66245539 TM |
9033 | int ret = 0; |
9034 | ||
9035 | if (clp->cl_mvops->minor_version < 1) | |
9036 | goto out; | |
9037 | if (clp->cl_exchange_flags == 0) | |
9038 | goto out; | |
05f4c350 CL |
9039 | if (clp->cl_preserve_clid) |
9040 | goto out; | |
73d8bde5 | 9041 | cred = nfs4_get_clid_cred(clp); |
66245539 | 9042 | ret = nfs4_proc_destroy_clientid(clp, cred); |
a52458b4 | 9043 | put_cred(cred); |
66245539 TM |
9044 | switch (ret) { |
9045 | case 0: | |
9046 | case -NFS4ERR_STALE_CLIENTID: | |
9047 | clp->cl_exchange_flags = 0; | |
9048 | } | |
9049 | out: | |
9050 | return ret; | |
9051 | } | |
9052 | ||
0efb01b2 DB |
9053 | #endif /* CONFIG_NFS_V4_1 */ |
9054 | ||
2050f0cc AA |
9055 | struct nfs4_get_lease_time_data { |
9056 | struct nfs4_get_lease_time_args *args; | |
9057 | struct nfs4_get_lease_time_res *res; | |
9058 | struct nfs_client *clp; | |
9059 | }; | |
9060 | ||
9061 | static void nfs4_get_lease_time_prepare(struct rpc_task *task, | |
9062 | void *calldata) | |
9063 | { | |
2050f0cc AA |
9064 | struct nfs4_get_lease_time_data *data = |
9065 | (struct nfs4_get_lease_time_data *)calldata; | |
9066 | ||
2050f0cc AA |
9067 | /* just setup sequence, do not trigger session recovery |
9068 | since we're invoked within one */ | |
7981c8a6 | 9069 | nfs4_setup_sequence(data->clp, |
d9afbd1b TM |
9070 | &data->args->la_seq_args, |
9071 | &data->res->lr_seq_res, | |
9072 | task); | |
2050f0cc AA |
9073 | } |
9074 | ||
9075 | /* | |
9076 | * Called from nfs4_state_manager thread for session setup, so don't recover | |
9077 | * from sequence operation or clientid errors. | |
9078 | */ | |
9079 | static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata) | |
9080 | { | |
9081 | struct nfs4_get_lease_time_data *data = | |
9082 | (struct nfs4_get_lease_time_data *)calldata; | |
9083 | ||
0efb01b2 | 9084 | if (!nfs4_sequence_done(task, &data->res->lr_seq_res)) |
14516c3a | 9085 | return; |
2050f0cc AA |
9086 | switch (task->tk_status) { |
9087 | case -NFS4ERR_DELAY: | |
9088 | case -NFS4ERR_GRACE: | |
2050f0cc AA |
9089 | rpc_delay(task, NFS4_POLL_RETRY_MIN); |
9090 | task->tk_status = 0; | |
df561f66 | 9091 | fallthrough; |
a8a4ae3a | 9092 | case -NFS4ERR_RETRY_UNCACHED_REP: |
d00c5d43 | 9093 | rpc_restart_call_prepare(task); |
2050f0cc AA |
9094 | return; |
9095 | } | |
2050f0cc AA |
9096 | } |
9097 | ||
17280175 | 9098 | static const struct rpc_call_ops nfs4_get_lease_time_ops = { |
2050f0cc AA |
9099 | .rpc_call_prepare = nfs4_get_lease_time_prepare, |
9100 | .rpc_call_done = nfs4_get_lease_time_done, | |
9101 | }; | |
9102 | ||
9103 | int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo) | |
9104 | { | |
2050f0cc AA |
9105 | struct nfs4_get_lease_time_args args; |
9106 | struct nfs4_get_lease_time_res res = { | |
9107 | .lr_fsinfo = fsinfo, | |
9108 | }; | |
9109 | struct nfs4_get_lease_time_data data = { | |
9110 | .args = &args, | |
9111 | .res = &res, | |
9112 | .clp = clp, | |
9113 | }; | |
9114 | struct rpc_message msg = { | |
9115 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME], | |
9116 | .rpc_argp = &args, | |
9117 | .rpc_resp = &res, | |
9118 | }; | |
9119 | struct rpc_task_setup task_setup = { | |
9120 | .rpc_client = clp->cl_rpcclient, | |
9121 | .rpc_message = &msg, | |
9122 | .callback_ops = &nfs4_get_lease_time_ops, | |
1bd714f2 TM |
9123 | .callback_data = &data, |
9124 | .flags = RPC_TASK_TIMEOUT, | |
2050f0cc | 9125 | }; |
2050f0cc | 9126 | |
fba83f34 | 9127 | nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1); |
f836b27e | 9128 | return nfs4_call_sync_custom(&task_setup); |
2050f0cc AA |
9129 | } |
9130 | ||
0efb01b2 DB |
9131 | #ifdef CONFIG_NFS_V4_1 |
9132 | ||
fc931582 AA |
9133 | /* |
9134 | * Initialize the values to be used by the client in CREATE_SESSION | |
9135 | * If nfs4_init_session set the fore channel request and response sizes, | |
9136 | * use them. | |
9137 | * | |
9138 | * Set the back channel max_resp_sz_cached to zero to force the client to | |
9139 | * always set csa_cachethis to FALSE because the current implementation | |
9140 | * of the back channel DRC only supports caching the CB_SEQUENCE operation. | |
9141 | */ | |
6b26cc8c CL |
9142 | static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args, |
9143 | struct rpc_clnt *clnt) | |
fc931582 | 9144 | { |
18aad3d5 | 9145 | unsigned int max_rqst_sz, max_resp_sz; |
6b26cc8c | 9146 | unsigned int max_bc_payload = rpc_max_bc_payload(clnt); |
7402a4fe | 9147 | unsigned int max_bc_slots = rpc_num_bc_slots(clnt); |
18aad3d5 AA |
9148 | |
9149 | max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead; | |
9150 | max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead; | |
fc931582 | 9151 | |
fc931582 | 9152 | /* Fore channel attributes */ |
18aad3d5 AA |
9153 | args->fc_attrs.max_rqst_sz = max_rqst_sz; |
9154 | args->fc_attrs.max_resp_sz = max_resp_sz; | |
fc931582 | 9155 | args->fc_attrs.max_ops = NFS4_MAX_OPS; |
ef159e91 | 9156 | args->fc_attrs.max_reqs = max_session_slots; |
fc931582 AA |
9157 | |
9158 | dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u " | |
8e0d46e1 | 9159 | "max_ops=%u max_reqs=%u\n", |
fc931582 AA |
9160 | __func__, |
9161 | args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz, | |
8e0d46e1 | 9162 | args->fc_attrs.max_ops, args->fc_attrs.max_reqs); |
fc931582 AA |
9163 | |
9164 | /* Back channel attributes */ | |
6b26cc8c CL |
9165 | args->bc_attrs.max_rqst_sz = max_bc_payload; |
9166 | args->bc_attrs.max_resp_sz = max_bc_payload; | |
fc931582 AA |
9167 | args->bc_attrs.max_resp_sz_cached = 0; |
9168 | args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS; | |
62421cd9 | 9169 | args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1); |
7402a4fe TM |
9170 | if (args->bc_attrs.max_reqs > max_bc_slots) |
9171 | args->bc_attrs.max_reqs = max_bc_slots; | |
fc931582 AA |
9172 | |
9173 | dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u " | |
9174 | "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n", | |
9175 | __func__, | |
9176 | args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz, | |
9177 | args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops, | |
9178 | args->bc_attrs.max_reqs); | |
9179 | } | |
9180 | ||
79969dd1 TM |
9181 | static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, |
9182 | struct nfs41_create_session_res *res) | |
8d35301d | 9183 | { |
43c2e885 | 9184 | struct nfs4_channel_attrs *sent = &args->fc_attrs; |
79969dd1 | 9185 | struct nfs4_channel_attrs *rcvd = &res->fc_attrs; |
43c2e885 | 9186 | |
43c2e885 BF |
9187 | if (rcvd->max_resp_sz > sent->max_resp_sz) |
9188 | return -EINVAL; | |
9189 | /* | |
9190 | * Our requested max_ops is the minimum we need; we're not | |
9191 | * prepared to break up compounds into smaller pieces than that. | |
9192 | * So, no point even trying to continue if the server won't | |
9193 | * cooperate: | |
9194 | */ | |
9195 | if (rcvd->max_ops < sent->max_ops) | |
9196 | return -EINVAL; | |
9197 | if (rcvd->max_reqs == 0) | |
9198 | return -EINVAL; | |
b4b9a0c1 VG |
9199 | if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE) |
9200 | rcvd->max_reqs = NFS4_MAX_SLOT_TABLE; | |
43c2e885 | 9201 | return 0; |
8d35301d AA |
9202 | } |
9203 | ||
79969dd1 TM |
9204 | static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, |
9205 | struct nfs41_create_session_res *res) | |
43c2e885 BF |
9206 | { |
9207 | struct nfs4_channel_attrs *sent = &args->bc_attrs; | |
79969dd1 | 9208 | struct nfs4_channel_attrs *rcvd = &res->bc_attrs; |
8d35301d | 9209 | |
b1c0df5f TM |
9210 | if (!(res->flags & SESSION4_BACK_CHAN)) |
9211 | goto out; | |
43c2e885 BF |
9212 | if (rcvd->max_rqst_sz > sent->max_rqst_sz) |
9213 | return -EINVAL; | |
9214 | if (rcvd->max_resp_sz < sent->max_resp_sz) | |
9215 | return -EINVAL; | |
9216 | if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached) | |
9217 | return -EINVAL; | |
5405fc44 | 9218 | if (rcvd->max_ops > sent->max_ops) |
43c2e885 | 9219 | return -EINVAL; |
5405fc44 | 9220 | if (rcvd->max_reqs > sent->max_reqs) |
43c2e885 | 9221 | return -EINVAL; |
b1c0df5f | 9222 | out: |
43c2e885 BF |
9223 | return 0; |
9224 | } | |
8d35301d | 9225 | |
8d35301d | 9226 | static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args, |
79969dd1 | 9227 | struct nfs41_create_session_res *res) |
8d35301d | 9228 | { |
43c2e885 | 9229 | int ret; |
8d35301d | 9230 | |
79969dd1 | 9231 | ret = nfs4_verify_fore_channel_attrs(args, res); |
43c2e885 BF |
9232 | if (ret) |
9233 | return ret; | |
79969dd1 TM |
9234 | return nfs4_verify_back_channel_attrs(args, res); |
9235 | } | |
9236 | ||
9237 | static void nfs4_update_session(struct nfs4_session *session, | |
9238 | struct nfs41_create_session_res *res) | |
9239 | { | |
9240 | nfs4_copy_sessionid(&session->sess_id, &res->sessionid); | |
e11259f9 TM |
9241 | /* Mark client id and session as being confirmed */ |
9242 | session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; | |
9243 | set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state); | |
79969dd1 TM |
9244 | session->flags = res->flags; |
9245 | memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs)); | |
b1c0df5f TM |
9246 | if (res->flags & SESSION4_BACK_CHAN) |
9247 | memcpy(&session->bc_attrs, &res->bc_attrs, | |
9248 | sizeof(session->bc_attrs)); | |
8d35301d AA |
9249 | } |
9250 | ||
848f5bda | 9251 | static int _nfs4_proc_create_session(struct nfs_client *clp, |
a52458b4 | 9252 | const struct cred *cred) |
fc931582 AA |
9253 | { |
9254 | struct nfs4_session *session = clp->cl_session; | |
9255 | struct nfs41_create_session_args args = { | |
9256 | .client = clp, | |
79969dd1 TM |
9257 | .clientid = clp->cl_clientid, |
9258 | .seqid = clp->cl_seqid, | |
fc931582 AA |
9259 | .cb_program = NFS4_CALLBACK, |
9260 | }; | |
79969dd1 TM |
9261 | struct nfs41_create_session_res res; |
9262 | ||
fc931582 AA |
9263 | struct rpc_message msg = { |
9264 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION], | |
9265 | .rpc_argp = &args, | |
9266 | .rpc_resp = &res, | |
848f5bda | 9267 | .rpc_cred = cred, |
fc931582 AA |
9268 | }; |
9269 | int status; | |
9270 | ||
6b26cc8c | 9271 | nfs4_init_channel_attrs(&args, clp->cl_rpcclient); |
0f91421e | 9272 | args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN); |
fc931582 | 9273 | |
5a0c257f N |
9274 | status = rpc_call_sync(session->clp->cl_rpcclient, &msg, |
9275 | RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); | |
c6d01c6f | 9276 | trace_nfs4_create_session(clp, status); |
fc931582 | 9277 | |
b519d408 TM |
9278 | switch (status) { |
9279 | case -NFS4ERR_STALE_CLIENTID: | |
9280 | case -NFS4ERR_DELAY: | |
9281 | case -ETIMEDOUT: | |
9282 | case -EACCES: | |
9283 | case -EAGAIN: | |
9284 | goto out; | |
8b98a532 | 9285 | } |
b519d408 TM |
9286 | |
9287 | clp->cl_seqid++; | |
43095d39 | 9288 | if (!status) { |
8d35301d | 9289 | /* Verify the session's negotiated channel_attrs values */ |
79969dd1 | 9290 | status = nfs4_verify_channel_attrs(&args, &res); |
fc931582 | 9291 | /* Increment the clientid slot sequence id */ |
79969dd1 TM |
9292 | if (status) |
9293 | goto out; | |
9294 | nfs4_update_session(session, &res); | |
fc931582 | 9295 | } |
79969dd1 | 9296 | out: |
fc931582 AA |
9297 | return status; |
9298 | } | |
9299 | ||
9300 | /* | |
9301 | * Issues a CREATE_SESSION operation to the server. | |
9302 | * It is the responsibility of the caller to verify the session is | |
9303 | * expired before calling this routine. | |
9304 | */ | |
a52458b4 | 9305 | int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred) |
fc931582 AA |
9306 | { |
9307 | int status; | |
9308 | unsigned *ptr; | |
fc931582 | 9309 | struct nfs4_session *session = clp->cl_session; |
f201bdfd OK |
9310 | struct nfs4_add_xprt_data xprtdata = { |
9311 | .clp = clp, | |
9312 | }; | |
9313 | struct rpc_add_xprt_test rpcdata = { | |
9314 | .add_xprt_test = clp->cl_mvops->session_trunk, | |
9315 | .data = &xprtdata, | |
9316 | }; | |
fc931582 AA |
9317 | |
9318 | dprintk("--> %s clp=%p session=%p\n", __func__, clp, session); | |
9319 | ||
848f5bda | 9320 | status = _nfs4_proc_create_session(clp, cred); |
fc931582 AA |
9321 | if (status) |
9322 | goto out; | |
9323 | ||
aacd5537 AA |
9324 | /* Init or reset the session slot tables */ |
9325 | status = nfs4_setup_session_slot_tables(session); | |
9326 | dprintk("slot table setup returned %d\n", status); | |
fc931582 AA |
9327 | if (status) |
9328 | goto out; | |
9329 | ||
9330 | ptr = (unsigned *)&session->sess_id.data[0]; | |
9331 | dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__, | |
9332 | clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]); | |
f201bdfd | 9333 | rpc_clnt_probe_trunked_xprts(clp->cl_rpcclient, &rpcdata); |
fc931582 | 9334 | out: |
fc931582 AA |
9335 | return status; |
9336 | } | |
9337 | ||
0f3e66c6 AA |
9338 | /* |
9339 | * Issue the over-the-wire RPC DESTROY_SESSION. | |
9340 | * The caller must serialize access to this routine. | |
9341 | */ | |
848f5bda | 9342 | int nfs4_proc_destroy_session(struct nfs4_session *session, |
a52458b4 | 9343 | const struct cred *cred) |
0f3e66c6 | 9344 | { |
848f5bda TM |
9345 | struct rpc_message msg = { |
9346 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION], | |
9347 | .rpc_argp = session, | |
9348 | .rpc_cred = cred, | |
9349 | }; | |
0f3e66c6 | 9350 | int status = 0; |
0f3e66c6 | 9351 | |
0f3e66c6 | 9352 | /* session is still being setup */ |
e11259f9 TM |
9353 | if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state)) |
9354 | return 0; | |
0f3e66c6 | 9355 | |
5a0c257f N |
9356 | status = rpc_call_sync(session->clp->cl_rpcclient, &msg, |
9357 | RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); | |
c6d01c6f | 9358 | trace_nfs4_destroy_session(session->clp, status); |
0f3e66c6 AA |
9359 | |
9360 | if (status) | |
08106ac7 | 9361 | dprintk("NFS: Got error %d from the server on DESTROY_SESSION. " |
0f3e66c6 | 9362 | "Session has been destroyed regardless...\n", status); |
88363d3e | 9363 | rpc_clnt_manage_trunked_xprts(session->clp->cl_rpcclient); |
0f3e66c6 AA |
9364 | return status; |
9365 | } | |
9366 | ||
fc01cea9 AA |
9367 | /* |
9368 | * Renew the cl_session lease. | |
9369 | */ | |
d5f8d3fe TM |
9370 | struct nfs4_sequence_data { |
9371 | struct nfs_client *clp; | |
9372 | struct nfs4_sequence_args args; | |
9373 | struct nfs4_sequence_res res; | |
9374 | }; | |
9375 | ||
dc96aef9 AB |
9376 | static void nfs41_sequence_release(void *data) |
9377 | { | |
d5f8d3fe TM |
9378 | struct nfs4_sequence_data *calldata = data; |
9379 | struct nfs_client *clp = calldata->clp; | |
dc96aef9 | 9380 | |
212bf41d | 9381 | if (refcount_read(&clp->cl_count) > 1) |
7135840f AB |
9382 | nfs4_schedule_state_renewal(clp); |
9383 | nfs_put_client(clp); | |
d5f8d3fe | 9384 | kfree(calldata); |
dc96aef9 AB |
9385 | } |
9386 | ||
aa5190d0 TM |
9387 | static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp) |
9388 | { | |
9389 | switch(task->tk_status) { | |
9390 | case -NFS4ERR_DELAY: | |
aa5190d0 TM |
9391 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
9392 | return -EAGAIN; | |
9393 | default: | |
0400a6b0 | 9394 | nfs4_schedule_lease_recovery(clp); |
aa5190d0 TM |
9395 | } |
9396 | return 0; | |
9397 | } | |
9398 | ||
dc96aef9 | 9399 | static void nfs41_sequence_call_done(struct rpc_task *task, void *data) |
fc01cea9 | 9400 | { |
d5f8d3fe TM |
9401 | struct nfs4_sequence_data *calldata = data; |
9402 | struct nfs_client *clp = calldata->clp; | |
fc01cea9 | 9403 | |
14516c3a TM |
9404 | if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp)) |
9405 | return; | |
fc01cea9 | 9406 | |
c6d01c6f | 9407 | trace_nfs4_sequence(clp, task->tk_status); |
6ad477a6 | 9408 | if (task->tk_status < 0 && !task->tk_client->cl_shutdown) { |
fc01cea9 | 9409 | dprintk("%s ERROR %d\n", __func__, task->tk_status); |
212bf41d | 9410 | if (refcount_read(&clp->cl_count) == 1) |
86882c75 | 9411 | return; |
fc01cea9 | 9412 | |
aa5190d0 TM |
9413 | if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) { |
9414 | rpc_restart_call_prepare(task); | |
fc01cea9 AA |
9415 | return; |
9416 | } | |
9417 | } | |
fc01cea9 | 9418 | dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred); |
fc01cea9 AA |
9419 | } |
9420 | ||
9421 | static void nfs41_sequence_prepare(struct rpc_task *task, void *data) | |
9422 | { | |
d5f8d3fe TM |
9423 | struct nfs4_sequence_data *calldata = data; |
9424 | struct nfs_client *clp = calldata->clp; | |
fc01cea9 AA |
9425 | struct nfs4_sequence_args *args; |
9426 | struct nfs4_sequence_res *res; | |
9427 | ||
fc01cea9 AA |
9428 | args = task->tk_msg.rpc_argp; |
9429 | res = task->tk_msg.rpc_resp; | |
9430 | ||
7981c8a6 | 9431 | nfs4_setup_sequence(clp, args, res, task); |
fc01cea9 AA |
9432 | } |
9433 | ||
9434 | static const struct rpc_call_ops nfs41_sequence_ops = { | |
9435 | .rpc_call_done = nfs41_sequence_call_done, | |
9436 | .rpc_call_prepare = nfs41_sequence_prepare, | |
dc96aef9 | 9437 | .rpc_release = nfs41_sequence_release, |
fc01cea9 AA |
9438 | }; |
9439 | ||
8fe72bac | 9440 | static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, |
a52458b4 | 9441 | const struct cred *cred, |
3be0f80b | 9442 | struct nfs4_slot *slot, |
8fe72bac | 9443 | bool is_privileged) |
fc01cea9 | 9444 | { |
d5f8d3fe | 9445 | struct nfs4_sequence_data *calldata; |
fc01cea9 AA |
9446 | struct rpc_message msg = { |
9447 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE], | |
9448 | .rpc_cred = cred, | |
9449 | }; | |
71ac6da9 TM |
9450 | struct rpc_task_setup task_setup_data = { |
9451 | .rpc_client = clp->cl_rpcclient, | |
9452 | .rpc_message = &msg, | |
8fe72bac | 9453 | .callback_ops = &nfs41_sequence_ops, |
85e39fee | 9454 | .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT | RPC_TASK_MOVEABLE, |
71ac6da9 | 9455 | }; |
3be0f80b | 9456 | struct rpc_task *ret; |
fc01cea9 | 9457 | |
3be0f80b | 9458 | ret = ERR_PTR(-EIO); |
212bf41d | 9459 | if (!refcount_inc_not_zero(&clp->cl_count)) |
3be0f80b TM |
9460 | goto out_err; |
9461 | ||
9462 | ret = ERR_PTR(-ENOMEM); | |
da48f267 | 9463 | calldata = kzalloc(sizeof(*calldata), GFP_KERNEL); |
3be0f80b TM |
9464 | if (calldata == NULL) |
9465 | goto out_put_clp; | |
fba83f34 | 9466 | nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged); |
3be0f80b | 9467 | nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot); |
d5f8d3fe TM |
9468 | msg.rpc_argp = &calldata->args; |
9469 | msg.rpc_resp = &calldata->res; | |
9470 | calldata->clp = clp; | |
71ac6da9 | 9471 | task_setup_data.callback_data = calldata; |
fc01cea9 | 9472 | |
3be0f80b TM |
9473 | ret = rpc_run_task(&task_setup_data); |
9474 | if (IS_ERR(ret)) | |
9475 | goto out_err; | |
9476 | return ret; | |
9477 | out_put_clp: | |
9478 | nfs_put_client(clp); | |
9479 | out_err: | |
9480 | nfs41_release_slot(slot); | |
9481 | return ret; | |
71ac6da9 TM |
9482 | } |
9483 | ||
a52458b4 | 9484 | static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags) |
71ac6da9 TM |
9485 | { |
9486 | struct rpc_task *task; | |
9487 | int ret = 0; | |
9488 | ||
2f60ea6b | 9489 | if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0) |
d1f456b0 | 9490 | return -EAGAIN; |
3be0f80b | 9491 | task = _nfs41_proc_sequence(clp, cred, NULL, false); |
71ac6da9 TM |
9492 | if (IS_ERR(task)) |
9493 | ret = PTR_ERR(task); | |
9494 | else | |
bf294b41 | 9495 | rpc_put_task_async(task); |
71ac6da9 TM |
9496 | dprintk("<-- %s status=%d\n", __func__, ret); |
9497 | return ret; | |
9498 | } | |
9499 | ||
a52458b4 | 9500 | static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred) |
71ac6da9 TM |
9501 | { |
9502 | struct rpc_task *task; | |
9503 | int ret; | |
9504 | ||
3be0f80b | 9505 | task = _nfs41_proc_sequence(clp, cred, NULL, true); |
71ac6da9 TM |
9506 | if (IS_ERR(task)) { |
9507 | ret = PTR_ERR(task); | |
9508 | goto out; | |
9509 | } | |
9510 | ret = rpc_wait_for_completion_task(task); | |
be824167 | 9511 | if (!ret) |
71ac6da9 TM |
9512 | ret = task->tk_status; |
9513 | rpc_put_task(task); | |
9514 | out: | |
9515 | dprintk("<-- %s status=%d\n", __func__, ret); | |
9516 | return ret; | |
fc01cea9 AA |
9517 | } |
9518 | ||
fce5c838 RL |
9519 | struct nfs4_reclaim_complete_data { |
9520 | struct nfs_client *clp; | |
9521 | struct nfs41_reclaim_complete_args arg; | |
9522 | struct nfs41_reclaim_complete_res res; | |
9523 | }; | |
9524 | ||
9525 | static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data) | |
9526 | { | |
9527 | struct nfs4_reclaim_complete_data *calldata = data; | |
9528 | ||
7981c8a6 | 9529 | nfs4_setup_sequence(calldata->clp, |
d9afbd1b TM |
9530 | &calldata->arg.seq_args, |
9531 | &calldata->res.seq_res, | |
9532 | task); | |
fce5c838 RL |
9533 | } |
9534 | ||
aa5190d0 TM |
9535 | static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp) |
9536 | { | |
9537 | switch(task->tk_status) { | |
9538 | case 0: | |
57174593 | 9539 | wake_up_all(&clp->cl_lock_waitq); |
df561f66 | 9540 | fallthrough; |
aa5190d0 TM |
9541 | case -NFS4ERR_COMPLETE_ALREADY: |
9542 | case -NFS4ERR_WRONG_CRED: /* What to do here? */ | |
9543 | break; | |
9544 | case -NFS4ERR_DELAY: | |
aa5190d0 | 9545 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
df561f66 | 9546 | fallthrough; |
a8a4ae3a | 9547 | case -NFS4ERR_RETRY_UNCACHED_REP: |
e35a5e78 ZX |
9548 | case -EACCES: |
9549 | dprintk("%s: failed to reclaim complete error %d for server %s, retrying\n", | |
9550 | __func__, task->tk_status, clp->cl_hostname); | |
aa5190d0 | 9551 | return -EAGAIN; |
0048fdd0 TM |
9552 | case -NFS4ERR_BADSESSION: |
9553 | case -NFS4ERR_DEADSESSION: | |
9554 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
0048fdd0 | 9555 | break; |
aa5190d0 | 9556 | default: |
0400a6b0 | 9557 | nfs4_schedule_lease_recovery(clp); |
aa5190d0 TM |
9558 | } |
9559 | return 0; | |
9560 | } | |
9561 | ||
fce5c838 RL |
9562 | static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data) |
9563 | { | |
9564 | struct nfs4_reclaim_complete_data *calldata = data; | |
9565 | struct nfs_client *clp = calldata->clp; | |
9566 | struct nfs4_sequence_res *res = &calldata->res.seq_res; | |
9567 | ||
14516c3a TM |
9568 | if (!nfs41_sequence_done(task, res)) |
9569 | return; | |
fce5c838 | 9570 | |
c6d01c6f | 9571 | trace_nfs4_reclaim_complete(clp, task->tk_status); |
aa5190d0 TM |
9572 | if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) { |
9573 | rpc_restart_call_prepare(task); | |
9574 | return; | |
9575 | } | |
fce5c838 RL |
9576 | } |
9577 | ||
9578 | static void nfs4_free_reclaim_complete_data(void *data) | |
9579 | { | |
9580 | struct nfs4_reclaim_complete_data *calldata = data; | |
9581 | ||
9582 | kfree(calldata); | |
9583 | } | |
9584 | ||
9585 | static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = { | |
9586 | .rpc_call_prepare = nfs4_reclaim_complete_prepare, | |
9587 | .rpc_call_done = nfs4_reclaim_complete_done, | |
9588 | .rpc_release = nfs4_free_reclaim_complete_data, | |
9589 | }; | |
9590 | ||
9591 | /* | |
9592 | * Issue a global reclaim complete. | |
9593 | */ | |
965e9c23 | 9594 | static int nfs41_proc_reclaim_complete(struct nfs_client *clp, |
a52458b4 | 9595 | const struct cred *cred) |
fce5c838 RL |
9596 | { |
9597 | struct nfs4_reclaim_complete_data *calldata; | |
fce5c838 RL |
9598 | struct rpc_message msg = { |
9599 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE], | |
965e9c23 | 9600 | .rpc_cred = cred, |
fce5c838 RL |
9601 | }; |
9602 | struct rpc_task_setup task_setup_data = { | |
9603 | .rpc_client = clp->cl_rpcclient, | |
9604 | .rpc_message = &msg, | |
9605 | .callback_ops = &nfs4_reclaim_complete_call_ops, | |
4c952e3d | 9606 | .flags = RPC_TASK_NO_ROUND_ROBIN, |
fce5c838 RL |
9607 | }; |
9608 | int status = -ENOMEM; | |
9609 | ||
8535b2be | 9610 | calldata = kzalloc(sizeof(*calldata), GFP_NOFS); |
fce5c838 RL |
9611 | if (calldata == NULL) |
9612 | goto out; | |
9613 | calldata->clp = clp; | |
9614 | calldata->arg.one_fs = 0; | |
fce5c838 | 9615 | |
fba83f34 | 9616 | nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1); |
fce5c838 RL |
9617 | msg.rpc_argp = &calldata->arg; |
9618 | msg.rpc_resp = &calldata->res; | |
9619 | task_setup_data.callback_data = calldata; | |
4c952e3d | 9620 | status = nfs4_call_sync_custom(&task_setup_data); |
fce5c838 RL |
9621 | out: |
9622 | dprintk("<-- %s status=%d\n", __func__, status); | |
9623 | return status; | |
9624 | } | |
b1f69b75 AA |
9625 | |
9626 | static void | |
9627 | nfs4_layoutget_prepare(struct rpc_task *task, void *calldata) | |
9628 | { | |
9629 | struct nfs4_layoutget *lgp = calldata; | |
c31663d4 | 9630 | struct nfs_server *server = NFS_SERVER(lgp->args.inode); |
b1f69b75 | 9631 | |
7981c8a6 | 9632 | nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args, |
183d9e7b | 9633 | &lgp->res.seq_res, task); |
b1f69b75 AA |
9634 | } |
9635 | ||
9636 | static void nfs4_layoutget_done(struct rpc_task *task, void *calldata) | |
9637 | { | |
9638 | struct nfs4_layoutget *lgp = calldata; | |
183d9e7b | 9639 | |
2e80dbe7 | 9640 | nfs41_sequence_process(task, &lgp->res.seq_res); |
183d9e7b JL |
9641 | } |
9642 | ||
9643 | static int | |
9644 | nfs4_layoutget_handle_exception(struct rpc_task *task, | |
9645 | struct nfs4_layoutget *lgp, struct nfs4_exception *exception) | |
9646 | { | |
ee314c2a TM |
9647 | struct inode *inode = lgp->args.inode; |
9648 | struct nfs_server *server = NFS_SERVER(inode); | |
b4e89bcb | 9649 | struct pnfs_layout_hdr *lo = lgp->lo; |
e85d7ee4 TM |
9650 | int nfs4err = task->tk_status; |
9651 | int err, status = 0; | |
f7db0b28 | 9652 | LIST_HEAD(head); |
b1f69b75 | 9653 | |
ed7e5423 | 9654 | dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status); |
b1f69b75 | 9655 | |
2dbf8dff TM |
9656 | nfs4_sequence_free_slot(&lgp->res.seq_res); |
9657 | ||
6e7434ab TM |
9658 | exception->state = NULL; |
9659 | exception->stateid = NULL; | |
9660 | ||
e85d7ee4 | 9661 | switch (nfs4err) { |
b1f69b75 | 9662 | case 0: |
ee314c2a | 9663 | goto out; |
7c1e6e58 PT |
9664 | |
9665 | /* | |
9666 | * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs | |
9667 | * on the file. set tk_status to -ENODATA to tell upper layer to | |
9668 | * retry go inband. | |
9669 | */ | |
9670 | case -NFS4ERR_LAYOUTUNAVAILABLE: | |
183d9e7b | 9671 | status = -ENODATA; |
7c1e6e58 | 9672 | goto out; |
21b874c8 TM |
9673 | /* |
9674 | * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of | |
9675 | * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3). | |
9676 | */ | |
9677 | case -NFS4ERR_BADLAYOUT: | |
183d9e7b JL |
9678 | status = -EOVERFLOW; |
9679 | goto out; | |
ed7e5423 BH |
9680 | /* |
9681 | * NFS4ERR_LAYOUTTRYLATER is a conflict with another client | |
21b874c8 TM |
9682 | * (or clients) writing to the same RAID stripe except when |
9683 | * the minlength argument is 0 (see RFC5661 section 18.43.3). | |
183d9e7b JL |
9684 | * |
9685 | * Treat it like we would RECALLCONFLICT -- we retry for a little | |
9686 | * while, and then eventually give up. | |
ed7e5423 | 9687 | */ |
b1f69b75 | 9688 | case -NFS4ERR_LAYOUTTRYLATER: |
183d9e7b JL |
9689 | if (lgp->args.minlength == 0) { |
9690 | status = -EOVERFLOW; | |
9691 | goto out; | |
ed7e5423 | 9692 | } |
e85d7ee4 TM |
9693 | status = -EBUSY; |
9694 | break; | |
183d9e7b | 9695 | case -NFS4ERR_RECALLCONFLICT: |
037e56a2 | 9696 | case -NFS4ERR_RETURNCONFLICT: |
183d9e7b | 9697 | status = -ERECALLCONFLICT; |
e85d7ee4 | 9698 | break; |
26f47443 TM |
9699 | case -NFS4ERR_DELEG_REVOKED: |
9700 | case -NFS4ERR_ADMIN_REVOKED: | |
ee314c2a TM |
9701 | case -NFS4ERR_EXPIRED: |
9702 | case -NFS4ERR_BAD_STATEID: | |
183d9e7b | 9703 | exception->timeout = 0; |
ee314c2a | 9704 | spin_lock(&inode->i_lock); |
f7db0b28 TM |
9705 | /* If the open stateid was bad, then recover it. */ |
9706 | if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) || | |
e8fa33a6 | 9707 | !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) { |
ee314c2a | 9708 | spin_unlock(&inode->i_lock); |
183d9e7b | 9709 | exception->state = lgp->args.ctx->state; |
26f47443 | 9710 | exception->stateid = &lgp->args.stateid; |
2259f960 TM |
9711 | break; |
9712 | } | |
f7db0b28 TM |
9713 | |
9714 | /* | |
9715 | * Mark the bad layout state as invalid, then retry | |
9716 | */ | |
668f455d | 9717 | pnfs_mark_layout_stateid_invalid(lo, &head); |
f7db0b28 | 9718 | spin_unlock(&inode->i_lock); |
1f18b82c | 9719 | nfs_commit_inode(inode, 0); |
f7db0b28 TM |
9720 | pnfs_free_lseg_list(&head); |
9721 | status = -EAGAIN; | |
9722 | goto out; | |
b1f69b75 | 9723 | } |
183d9e7b | 9724 | |
e85d7ee4 TM |
9725 | err = nfs4_handle_exception(server, nfs4err, exception); |
9726 | if (!status) { | |
9727 | if (exception->retry) | |
9728 | status = -EAGAIN; | |
9729 | else | |
9730 | status = err; | |
9731 | } | |
ee314c2a | 9732 | out: |
183d9e7b | 9733 | return status; |
b1f69b75 AA |
9734 | } |
9735 | ||
dacb452d | 9736 | size_t max_response_pages(struct nfs_server *server) |
8554116e IK |
9737 | { |
9738 | u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; | |
9739 | return nfs_page_array_len(0, max_resp_sz); | |
9740 | } | |
9741 | ||
b1f69b75 AA |
9742 | static void nfs4_layoutget_release(void *calldata) |
9743 | { | |
9744 | struct nfs4_layoutget *lgp = calldata; | |
9745 | ||
bd171930 | 9746 | nfs4_sequence_free_slot(&lgp->res.seq_res); |
29a8bfe5 | 9747 | pnfs_layoutget_free(lgp); |
b1f69b75 AA |
9748 | } |
9749 | ||
9750 | static const struct rpc_call_ops nfs4_layoutget_call_ops = { | |
9751 | .rpc_call_prepare = nfs4_layoutget_prepare, | |
9752 | .rpc_call_done = nfs4_layoutget_done, | |
9753 | .rpc_release = nfs4_layoutget_release, | |
9754 | }; | |
9755 | ||
a0b0a6e3 | 9756 | struct pnfs_layout_segment * |
6e7434ab TM |
9757 | nfs4_proc_layoutget(struct nfs4_layoutget *lgp, |
9758 | struct nfs4_exception *exception) | |
b1f69b75 | 9759 | { |
a47970ff WAA |
9760 | struct inode *inode = lgp->args.inode; |
9761 | struct nfs_server *server = NFS_SERVER(inode); | |
b1f69b75 AA |
9762 | struct rpc_task *task; |
9763 | struct rpc_message msg = { | |
9764 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET], | |
9765 | .rpc_argp = &lgp->args, | |
9766 | .rpc_resp = &lgp->res, | |
6ab59344 | 9767 | .rpc_cred = lgp->cred, |
b1f69b75 AA |
9768 | }; |
9769 | struct rpc_task_setup task_setup_data = { | |
9770 | .rpc_client = server->client, | |
9771 | .rpc_message = &msg, | |
9772 | .callback_ops = &nfs4_layoutget_call_ops, | |
9773 | .callback_data = lgp, | |
85e39fee OK |
9774 | .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF | |
9775 | RPC_TASK_MOVEABLE, | |
b1f69b75 | 9776 | }; |
a0b0a6e3 | 9777 | struct pnfs_layout_segment *lseg = NULL; |
b1f69b75 AA |
9778 | int status = 0; |
9779 | ||
fba83f34 | 9780 | nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0); |
6e7434ab | 9781 | exception->retry = 0; |
a47970ff | 9782 | |
b1f69b75 | 9783 | task = rpc_run_task(&task_setup_data); |
68b78dcd TM |
9784 | if (IS_ERR(task)) |
9785 | return ERR_CAST(task); | |
6e47666e | 9786 | |
820bf85c | 9787 | status = rpc_wait_for_completion_task(task); |
2dbf8dff TM |
9788 | if (status != 0) |
9789 | goto out; | |
9790 | ||
18c0778a | 9791 | if (task->tk_status < 0) { |
6e7434ab TM |
9792 | exception->retry = 1; |
9793 | status = nfs4_layoutget_handle_exception(task, lgp, exception); | |
18c0778a | 9794 | } else if (lgp->res.layoutp->len == 0) { |
6e7434ab | 9795 | exception->retry = 1; |
18c0778a | 9796 | status = -EAGAIN; |
6e7434ab | 9797 | nfs4_update_delay(&exception->timeout); |
2dbf8dff TM |
9798 | } else |
9799 | lseg = pnfs_layout_process(lgp); | |
9800 | out: | |
1037e6ea TM |
9801 | trace_nfs4_layoutget(lgp->args.ctx, |
9802 | &lgp->args.range, | |
9803 | &lgp->res.range, | |
48c9579a | 9804 | &lgp->res.stateid, |
1037e6ea | 9805 | status); |
183d9e7b | 9806 | |
b1f69b75 AA |
9807 | rpc_put_task(task); |
9808 | dprintk("<-- %s status=%d\n", __func__, status); | |
a0b0a6e3 TM |
9809 | if (status) |
9810 | return ERR_PTR(status); | |
9811 | return lseg; | |
b1f69b75 AA |
9812 | } |
9813 | ||
cbe82603 BH |
9814 | static void |
9815 | nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata) | |
9816 | { | |
9817 | struct nfs4_layoutreturn *lrp = calldata; | |
9818 | ||
7981c8a6 | 9819 | nfs4_setup_sequence(lrp->clp, |
d9afbd1b TM |
9820 | &lrp->args.seq_args, |
9821 | &lrp->res.seq_res, | |
9822 | task); | |
c8bf7073 TM |
9823 | if (!pnfs_layout_is_valid(lrp->args.layout)) |
9824 | rpc_exit(task, 0); | |
cbe82603 BH |
9825 | } |
9826 | ||
9827 | static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata) | |
9828 | { | |
9829 | struct nfs4_layoutreturn *lrp = calldata; | |
9830 | struct nfs_server *server; | |
9831 | ||
2e80dbe7 | 9832 | if (!nfs41_sequence_process(task, &lrp->res.seq_res)) |
cbe82603 BH |
9833 | return; |
9834 | ||
6109bcf7 TM |
9835 | /* |
9836 | * Was there an RPC level error? Assume the call succeeded, | |
9837 | * and that we need to release the layout | |
9838 | */ | |
9839 | if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) { | |
9840 | lrp->res.lrs_present = 0; | |
9841 | return; | |
9842 | } | |
9843 | ||
cbe82603 | 9844 | server = NFS_SERVER(lrp->args.inode); |
f22e5edd | 9845 | switch (task->tk_status) { |
ff90514e | 9846 | case -NFS4ERR_OLD_STATEID: |
30cb3ee2 | 9847 | if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid, |
ecf84026 | 9848 | &lrp->args.range, |
ff90514e TM |
9849 | lrp->args.inode)) |
9850 | goto out_restart; | |
df561f66 | 9851 | fallthrough; |
f22e5edd TM |
9852 | default: |
9853 | task->tk_status = 0; | |
df561f66 | 9854 | fallthrough; |
f22e5edd TM |
9855 | case 0: |
9856 | break; | |
9857 | case -NFS4ERR_DELAY: | |
8478eaa1 | 9858 | if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN) |
f22e5edd | 9859 | break; |
ff90514e | 9860 | goto out_restart; |
cbe82603 | 9861 | } |
ff90514e TM |
9862 | return; |
9863 | out_restart: | |
9864 | task->tk_status = 0; | |
9865 | nfs4_sequence_free_slot(&lrp->res.seq_res); | |
9866 | rpc_restart_call_prepare(task); | |
cbe82603 BH |
9867 | } |
9868 | ||
9869 | static void nfs4_layoutreturn_release(void *calldata) | |
9870 | { | |
9871 | struct nfs4_layoutreturn *lrp = calldata; | |
849b286f | 9872 | struct pnfs_layout_hdr *lo = lrp->args.layout; |
cbe82603 | 9873 | |
2a974425 | 9874 | pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range, |
68f74479 | 9875 | lrp->res.lrs_present ? &lrp->res.stateid : NULL); |
2e80dbe7 | 9876 | nfs4_sequence_free_slot(&lrp->res.seq_res); |
4d796d75 TM |
9877 | if (lrp->ld_private.ops && lrp->ld_private.ops->free) |
9878 | lrp->ld_private.ops->free(&lrp->ld_private); | |
2f065ddb TM |
9879 | pnfs_put_layout_hdr(lrp->args.layout); |
9880 | nfs_iput_and_deactive(lrp->inode); | |
44ea8dfc | 9881 | put_cred(lrp->cred); |
cbe82603 | 9882 | kfree(calldata); |
cbe82603 BH |
9883 | } |
9884 | ||
9885 | static const struct rpc_call_ops nfs4_layoutreturn_call_ops = { | |
9886 | .rpc_call_prepare = nfs4_layoutreturn_prepare, | |
9887 | .rpc_call_done = nfs4_layoutreturn_done, | |
9888 | .rpc_release = nfs4_layoutreturn_release, | |
9889 | }; | |
9890 | ||
6c16605d | 9891 | int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync) |
cbe82603 BH |
9892 | { |
9893 | struct rpc_task *task; | |
9894 | struct rpc_message msg = { | |
9895 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN], | |
9896 | .rpc_argp = &lrp->args, | |
9897 | .rpc_resp = &lrp->res, | |
9556000d | 9898 | .rpc_cred = lrp->cred, |
cbe82603 BH |
9899 | }; |
9900 | struct rpc_task_setup task_setup_data = { | |
1771c577 | 9901 | .rpc_client = NFS_SERVER(lrp->args.inode)->client, |
cbe82603 BH |
9902 | .rpc_message = &msg, |
9903 | .callback_ops = &nfs4_layoutreturn_call_ops, | |
9904 | .callback_data = lrp, | |
85e39fee | 9905 | .flags = RPC_TASK_MOVEABLE, |
cbe82603 | 9906 | }; |
6c16605d | 9907 | int status = 0; |
cbe82603 | 9908 | |
99ade3c7 AE |
9909 | nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client, |
9910 | NFS_SP4_MACH_CRED_PNFS_CLEANUP, | |
9911 | &task_setup_data.rpc_client, &msg); | |
9912 | ||
c3aba897 | 9913 | lrp->inode = nfs_igrab_and_active(lrp->args.inode); |
5a0ec8ac | 9914 | if (!sync) { |
5a0ec8ac TM |
9915 | if (!lrp->inode) { |
9916 | nfs4_layoutreturn_release(lrp); | |
9917 | return -EAGAIN; | |
9918 | } | |
9919 | task_setup_data.flags |= RPC_TASK_ASYNC; | |
9920 | } | |
c3aba897 TM |
9921 | if (!lrp->inode) |
9922 | nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, | |
9923 | 1); | |
9924 | else | |
9925 | nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, | |
9926 | 0); | |
cbe82603 BH |
9927 | task = rpc_run_task(&task_setup_data); |
9928 | if (IS_ERR(task)) | |
9929 | return PTR_ERR(task); | |
5a0ec8ac TM |
9930 | if (sync) |
9931 | status = task->tk_status; | |
48c9579a | 9932 | trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status); |
cbe82603 BH |
9933 | dprintk("<-- %s status=%d\n", __func__, status); |
9934 | rpc_put_task(task); | |
9935 | return status; | |
9936 | } | |
9937 | ||
b1f69b75 | 9938 | static int |
cd5875fe TM |
9939 | _nfs4_proc_getdeviceinfo(struct nfs_server *server, |
9940 | struct pnfs_device *pdev, | |
a52458b4 | 9941 | const struct cred *cred) |
b1f69b75 AA |
9942 | { |
9943 | struct nfs4_getdeviceinfo_args args = { | |
9944 | .pdev = pdev, | |
4e590803 TM |
9945 | .notify_types = NOTIFY_DEVICEID4_CHANGE | |
9946 | NOTIFY_DEVICEID4_DELETE, | |
b1f69b75 AA |
9947 | }; |
9948 | struct nfs4_getdeviceinfo_res res = { | |
9949 | .pdev = pdev, | |
9950 | }; | |
9951 | struct rpc_message msg = { | |
9952 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO], | |
9953 | .rpc_argp = &args, | |
9954 | .rpc_resp = &res, | |
cd5875fe | 9955 | .rpc_cred = cred, |
b1f69b75 AA |
9956 | }; |
9957 | int status; | |
9958 | ||
7c513058 | 9959 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
4e590803 TM |
9960 | if (res.notification & ~args.notify_types) |
9961 | dprintk("%s: unsupported notification\n", __func__); | |
df52699e TM |
9962 | if (res.notification != args.notify_types) |
9963 | pdev->nocache = 1; | |
4e590803 | 9964 | |
cac1d3a2 TM |
9965 | trace_nfs4_getdeviceinfo(server, &pdev->dev_id, status); |
9966 | ||
b1f69b75 AA |
9967 | dprintk("<-- %s status=%d\n", __func__, status); |
9968 | ||
9969 | return status; | |
9970 | } | |
9971 | ||
cd5875fe TM |
9972 | int nfs4_proc_getdeviceinfo(struct nfs_server *server, |
9973 | struct pnfs_device *pdev, | |
a52458b4 | 9974 | const struct cred *cred) |
b1f69b75 AA |
9975 | { |
9976 | struct nfs4_exception exception = { }; | |
9977 | int err; | |
9978 | ||
9979 | do { | |
9980 | err = nfs4_handle_exception(server, | |
cd5875fe | 9981 | _nfs4_proc_getdeviceinfo(server, pdev, cred), |
b1f69b75 AA |
9982 | &exception); |
9983 | } while (exception.retry); | |
9984 | return err; | |
9985 | } | |
9986 | EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo); | |
9987 | ||
863a3c6c AA |
9988 | static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata) |
9989 | { | |
9990 | struct nfs4_layoutcommit_data *data = calldata; | |
9991 | struct nfs_server *server = NFS_SERVER(data->args.inode); | |
9992 | ||
7981c8a6 | 9993 | nfs4_setup_sequence(server->nfs_client, |
d9afbd1b TM |
9994 | &data->args.seq_args, |
9995 | &data->res.seq_res, | |
9996 | task); | |
863a3c6c AA |
9997 | } |
9998 | ||
9999 | static void | |
10000 | nfs4_layoutcommit_done(struct rpc_task *task, void *calldata) | |
10001 | { | |
10002 | struct nfs4_layoutcommit_data *data = calldata; | |
10003 | struct nfs_server *server = NFS_SERVER(data->args.inode); | |
10004 | ||
6ba7db34 | 10005 | if (!nfs41_sequence_done(task, &data->res.seq_res)) |
863a3c6c AA |
10006 | return; |
10007 | ||
10008 | switch (task->tk_status) { /* Just ignore these failures */ | |
e59d27e0 TM |
10009 | case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */ |
10010 | case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */ | |
10011 | case -NFS4ERR_BADLAYOUT: /* no layout */ | |
10012 | case -NFS4ERR_GRACE: /* loca_recalim always false */ | |
863a3c6c | 10013 | task->tk_status = 0; |
ffb81717 | 10014 | break; |
e59d27e0 | 10015 | case 0: |
e59d27e0 TM |
10016 | break; |
10017 | default: | |
8478eaa1 | 10018 | if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) { |
e59d27e0 TM |
10019 | rpc_restart_call_prepare(task); |
10020 | return; | |
10021 | } | |
10022 | } | |
863a3c6c AA |
10023 | } |
10024 | ||
10025 | static void nfs4_layoutcommit_release(void *calldata) | |
10026 | { | |
10027 | struct nfs4_layoutcommit_data *data = calldata; | |
10028 | ||
db29c089 | 10029 | pnfs_cleanup_layoutcommit(data); |
d8c951c3 TM |
10030 | nfs_post_op_update_inode_force_wcc(data->args.inode, |
10031 | data->res.fattr); | |
a52458b4 | 10032 | put_cred(data->cred); |
472e2594 | 10033 | nfs_iput_and_deactive(data->inode); |
863a3c6c AA |
10034 | kfree(data); |
10035 | } | |
10036 | ||
10037 | static const struct rpc_call_ops nfs4_layoutcommit_ops = { | |
10038 | .rpc_call_prepare = nfs4_layoutcommit_prepare, | |
10039 | .rpc_call_done = nfs4_layoutcommit_done, | |
10040 | .rpc_release = nfs4_layoutcommit_release, | |
10041 | }; | |
10042 | ||
10043 | int | |
ef311537 | 10044 | nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync) |
863a3c6c AA |
10045 | { |
10046 | struct rpc_message msg = { | |
10047 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT], | |
10048 | .rpc_argp = &data->args, | |
10049 | .rpc_resp = &data->res, | |
10050 | .rpc_cred = data->cred, | |
10051 | }; | |
10052 | struct rpc_task_setup task_setup_data = { | |
10053 | .task = &data->task, | |
10054 | .rpc_client = NFS_CLIENT(data->args.inode), | |
10055 | .rpc_message = &msg, | |
10056 | .callback_ops = &nfs4_layoutcommit_ops, | |
10057 | .callback_data = data, | |
85e39fee | 10058 | .flags = RPC_TASK_MOVEABLE, |
863a3c6c AA |
10059 | }; |
10060 | struct rpc_task *task; | |
10061 | int status = 0; | |
10062 | ||
b4839ebe KM |
10063 | dprintk("NFS: initiating layoutcommit call. sync %d " |
10064 | "lbw: %llu inode %lu\n", sync, | |
863a3c6c AA |
10065 | data->args.lastbytewritten, |
10066 | data->args.inode->i_ino); | |
10067 | ||
472e2594 TM |
10068 | if (!sync) { |
10069 | data->inode = nfs_igrab_and_active(data->args.inode); | |
10070 | if (data->inode == NULL) { | |
10071 | nfs4_layoutcommit_release(data); | |
10072 | return -EAGAIN; | |
10073 | } | |
10074 | task_setup_data.flags = RPC_TASK_ASYNC; | |
10075 | } | |
fba83f34 | 10076 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0); |
863a3c6c AA |
10077 | task = rpc_run_task(&task_setup_data); |
10078 | if (IS_ERR(task)) | |
10079 | return PTR_ERR(task); | |
472e2594 TM |
10080 | if (sync) |
10081 | status = task->tk_status; | |
48c9579a | 10082 | trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status); |
863a3c6c AA |
10083 | dprintk("%s: status %d\n", __func__, status); |
10084 | rpc_put_task(task); | |
10085 | return status; | |
10086 | } | |
fca78d6d | 10087 | |
302fad7b | 10088 | /* |
97431204 AA |
10089 | * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if |
10090 | * possible) as per RFC3530bis and RFC5661 Security Considerations sections | |
10091 | */ | |
fca78d6d BS |
10092 | static int |
10093 | _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle, | |
b1b3e136 WAA |
10094 | struct nfs_fsinfo *info, |
10095 | struct nfs4_secinfo_flavors *flavors, bool use_integrity) | |
fca78d6d BS |
10096 | { |
10097 | struct nfs41_secinfo_no_name_args args = { | |
10098 | .style = SECINFO_STYLE_CURRENT_FH, | |
10099 | }; | |
10100 | struct nfs4_secinfo_res res = { | |
10101 | .flavors = flavors, | |
10102 | }; | |
10103 | struct rpc_message msg = { | |
10104 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME], | |
10105 | .rpc_argp = &args, | |
10106 | .rpc_resp = &res, | |
10107 | }; | |
cc15e24a AS |
10108 | struct nfs4_call_sync_data data = { |
10109 | .seq_server = server, | |
10110 | .seq_args = &args.seq_args, | |
10111 | .seq_res = &res.seq_res, | |
10112 | }; | |
10113 | struct rpc_task_setup task_setup = { | |
10114 | .rpc_client = server->client, | |
10115 | .rpc_message = &msg, | |
10116 | .callback_ops = server->nfs_client->cl_mvops->call_sync_ops, | |
10117 | .callback_data = &data, | |
10118 | .flags = RPC_TASK_NO_ROUND_ROBIN, | |
10119 | }; | |
a52458b4 | 10120 | const struct cred *cred = NULL; |
b1b3e136 WAA |
10121 | int status; |
10122 | ||
10123 | if (use_integrity) { | |
48bb6ec1 | 10124 | task_setup.rpc_client = server->nfs_client->cl_rpcclient; |
cc15e24a | 10125 | |
7cb852df WAA |
10126 | cred = nfs4_get_clid_cred(server->nfs_client); |
10127 | msg.rpc_cred = cred; | |
b1b3e136 WAA |
10128 | } |
10129 | ||
cc15e24a AS |
10130 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0); |
10131 | status = nfs4_call_sync_custom(&task_setup); | |
b1b3e136 WAA |
10132 | dprintk("<-- %s status=%d\n", __func__, status); |
10133 | ||
a52458b4 | 10134 | put_cred(cred); |
b1b3e136 WAA |
10135 | |
10136 | return status; | |
fca78d6d BS |
10137 | } |
10138 | ||
10139 | static int | |
10140 | nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle, | |
10141 | struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors) | |
10142 | { | |
0688e64b TM |
10143 | struct nfs4_exception exception = { |
10144 | .interruptible = true, | |
10145 | }; | |
fca78d6d BS |
10146 | int err; |
10147 | do { | |
b1b3e136 WAA |
10148 | /* first try using integrity protection */ |
10149 | err = -NFS4ERR_WRONGSEC; | |
10150 | ||
10151 | /* try to use integrity protection with machine cred */ | |
10152 | if (_nfs4_is_integrity_protected(server->nfs_client)) | |
10153 | err = _nfs41_proc_secinfo_no_name(server, fhandle, info, | |
10154 | flavors, true); | |
10155 | ||
10156 | /* | |
10157 | * if unable to use integrity protection, or SECINFO with | |
10158 | * integrity protection returns NFS4ERR_WRONGSEC (which is | |
10159 | * disallowed by spec, but exists in deployed servers) use | |
10160 | * the current filesystem's rpc_client and the user cred. | |
10161 | */ | |
10162 | if (err == -NFS4ERR_WRONGSEC) | |
10163 | err = _nfs41_proc_secinfo_no_name(server, fhandle, info, | |
10164 | flavors, false); | |
10165 | ||
fca78d6d BS |
10166 | switch (err) { |
10167 | case 0: | |
10168 | case -NFS4ERR_WRONGSEC: | |
78b19bae | 10169 | case -ENOTSUPP: |
05e9cfb4 | 10170 | goto out; |
fca78d6d BS |
10171 | default: |
10172 | err = nfs4_handle_exception(server, err, &exception); | |
10173 | } | |
10174 | } while (exception.retry); | |
05e9cfb4 | 10175 | out: |
fca78d6d BS |
10176 | return err; |
10177 | } | |
10178 | ||
10179 | static int | |
10180 | nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, | |
10181 | struct nfs_fsinfo *info) | |
10182 | { | |
10183 | int err; | |
10184 | struct page *page; | |
367156d9 | 10185 | rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR; |
fca78d6d | 10186 | struct nfs4_secinfo_flavors *flavors; |
58a8cf12 WAA |
10187 | struct nfs4_secinfo4 *secinfo; |
10188 | int i; | |
fca78d6d BS |
10189 | |
10190 | page = alloc_page(GFP_KERNEL); | |
10191 | if (!page) { | |
10192 | err = -ENOMEM; | |
10193 | goto out; | |
10194 | } | |
10195 | ||
10196 | flavors = page_address(page); | |
10197 | err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors); | |
10198 | ||
10199 | /* | |
10200 | * Fall back on "guess and check" method if | |
10201 | * the server doesn't support SECINFO_NO_NAME | |
10202 | */ | |
78b19bae | 10203 | if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) { |
fca78d6d BS |
10204 | err = nfs4_find_root_sec(server, fhandle, info); |
10205 | goto out_freepage; | |
10206 | } | |
10207 | if (err) | |
10208 | goto out_freepage; | |
10209 | ||
58a8cf12 WAA |
10210 | for (i = 0; i < flavors->num_flavors; i++) { |
10211 | secinfo = &flavors->flavors[i]; | |
10212 | ||
10213 | switch (secinfo->flavor) { | |
10214 | case RPC_AUTH_NULL: | |
10215 | case RPC_AUTH_UNIX: | |
10216 | case RPC_AUTH_GSS: | |
10217 | flavor = rpcauth_get_pseudoflavor(secinfo->flavor, | |
10218 | &secinfo->flavor_info); | |
10219 | break; | |
10220 | default: | |
10221 | flavor = RPC_AUTH_MAXFLAVOR; | |
10222 | break; | |
10223 | } | |
10224 | ||
4d4b69dd WAA |
10225 | if (!nfs_auth_info_match(&server->auth_info, flavor)) |
10226 | flavor = RPC_AUTH_MAXFLAVOR; | |
10227 | ||
58a8cf12 WAA |
10228 | if (flavor != RPC_AUTH_MAXFLAVOR) { |
10229 | err = nfs4_lookup_root_sec(server, fhandle, | |
10230 | info, flavor); | |
10231 | if (!err) | |
10232 | break; | |
10233 | } | |
10234 | } | |
10235 | ||
10236 | if (flavor == RPC_AUTH_MAXFLAVOR) | |
10237 | err = -EPERM; | |
fca78d6d BS |
10238 | |
10239 | out_freepage: | |
10240 | put_page(page); | |
10241 | if (err == -EACCES) | |
10242 | return -EPERM; | |
10243 | out: | |
10244 | return err; | |
10245 | } | |
1cab0652 | 10246 | |
ab7cb0df TM |
10247 | static int _nfs41_test_stateid(struct nfs_server *server, |
10248 | nfs4_stateid *stateid, | |
a52458b4 | 10249 | const struct cred *cred) |
7d974794 BS |
10250 | { |
10251 | int status; | |
10252 | struct nfs41_test_stateid_args args = { | |
1cab0652 | 10253 | .stateid = stateid, |
7d974794 BS |
10254 | }; |
10255 | struct nfs41_test_stateid_res res; | |
10256 | struct rpc_message msg = { | |
10257 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID], | |
10258 | .rpc_argp = &args, | |
10259 | .rpc_resp = &res, | |
ab7cb0df | 10260 | .rpc_cred = cred, |
7d974794 | 10261 | }; |
3787d506 WAA |
10262 | struct rpc_clnt *rpc_client = server->client; |
10263 | ||
10264 | nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID, | |
10265 | &rpc_client, &msg); | |
1cab0652 | 10266 | |
38527b15 | 10267 | dprintk("NFS call test_stateid %p\n", stateid); |
fba83f34 | 10268 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); |
3787d506 | 10269 | status = nfs4_call_sync_sequence(rpc_client, server, &msg, |
8fe72bac | 10270 | &args.seq_args, &res.seq_res); |
38527b15 CL |
10271 | if (status != NFS_OK) { |
10272 | dprintk("NFS reply test_stateid: failed, %d\n", status); | |
377e507d | 10273 | return status; |
38527b15 CL |
10274 | } |
10275 | dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status); | |
377e507d | 10276 | return -res.status; |
7d974794 BS |
10277 | } |
10278 | ||
43912bbb TM |
10279 | static void nfs4_handle_delay_or_session_error(struct nfs_server *server, |
10280 | int err, struct nfs4_exception *exception) | |
10281 | { | |
10282 | exception->retry = 0; | |
10283 | switch(err) { | |
10284 | case -NFS4ERR_DELAY: | |
76e8a1bd | 10285 | case -NFS4ERR_RETRY_UNCACHED_REP: |
43912bbb TM |
10286 | nfs4_handle_exception(server, err, exception); |
10287 | break; | |
10288 | case -NFS4ERR_BADSESSION: | |
10289 | case -NFS4ERR_BADSLOT: | |
10290 | case -NFS4ERR_BAD_HIGH_SLOT: | |
10291 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
10292 | case -NFS4ERR_DEADSESSION: | |
10293 | nfs4_do_handle_exception(server, err, exception); | |
10294 | } | |
10295 | } | |
10296 | ||
38527b15 CL |
10297 | /** |
10298 | * nfs41_test_stateid - perform a TEST_STATEID operation | |
10299 | * | |
10300 | * @server: server / transport on which to perform the operation | |
10301 | * @stateid: state ID to test | |
ab7cb0df | 10302 | * @cred: credential |
38527b15 CL |
10303 | * |
10304 | * Returns NFS_OK if the server recognizes that "stateid" is valid. | |
10305 | * Otherwise a negative NFS4ERR value is returned if the operation | |
10306 | * failed or the state ID is not currently valid. | |
10307 | */ | |
ab7cb0df TM |
10308 | static int nfs41_test_stateid(struct nfs_server *server, |
10309 | nfs4_stateid *stateid, | |
a52458b4 | 10310 | const struct cred *cred) |
7d974794 | 10311 | { |
0688e64b TM |
10312 | struct nfs4_exception exception = { |
10313 | .interruptible = true, | |
10314 | }; | |
7d974794 BS |
10315 | int err; |
10316 | do { | |
ab7cb0df | 10317 | err = _nfs41_test_stateid(server, stateid, cred); |
43912bbb | 10318 | nfs4_handle_delay_or_session_error(server, err, &exception); |
7d974794 BS |
10319 | } while (exception.retry); |
10320 | return err; | |
10321 | } | |
9aeda35f | 10322 | |
7c1d5fae TM |
10323 | struct nfs_free_stateid_data { |
10324 | struct nfs_server *server; | |
10325 | struct nfs41_free_stateid_args args; | |
9aeda35f | 10326 | struct nfs41_free_stateid_res res; |
7c1d5fae TM |
10327 | }; |
10328 | ||
10329 | static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata) | |
10330 | { | |
10331 | struct nfs_free_stateid_data *data = calldata; | |
7981c8a6 | 10332 | nfs4_setup_sequence(data->server->nfs_client, |
7c1d5fae TM |
10333 | &data->args.seq_args, |
10334 | &data->res.seq_res, | |
10335 | task); | |
10336 | } | |
10337 | ||
10338 | static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata) | |
10339 | { | |
10340 | struct nfs_free_stateid_data *data = calldata; | |
10341 | ||
10342 | nfs41_sequence_done(task, &data->res.seq_res); | |
10343 | ||
10344 | switch (task->tk_status) { | |
10345 | case -NFS4ERR_DELAY: | |
8478eaa1 | 10346 | if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN) |
7c1d5fae TM |
10347 | rpc_restart_call_prepare(task); |
10348 | } | |
10349 | } | |
10350 | ||
10351 | static void nfs41_free_stateid_release(void *calldata) | |
10352 | { | |
576acc25 SM |
10353 | struct nfs_free_stateid_data *data = calldata; |
10354 | struct nfs_client *clp = data->server->nfs_client; | |
10355 | ||
10356 | nfs_put_client(clp); | |
7c1d5fae TM |
10357 | kfree(calldata); |
10358 | } | |
10359 | ||
17f26b12 | 10360 | static const struct rpc_call_ops nfs41_free_stateid_ops = { |
7c1d5fae TM |
10361 | .rpc_call_prepare = nfs41_free_stateid_prepare, |
10362 | .rpc_call_done = nfs41_free_stateid_done, | |
10363 | .rpc_release = nfs41_free_stateid_release, | |
10364 | }; | |
10365 | ||
2f261020 AS |
10366 | /** |
10367 | * nfs41_free_stateid - perform a FREE_STATEID operation | |
10368 | * | |
10369 | * @server: server / transport on which to perform the operation | |
10370 | * @stateid: state ID to release | |
10371 | * @cred: credential | |
302fad7b | 10372 | * @privileged: set to true if this call needs to be privileged |
2f261020 AS |
10373 | * |
10374 | * Note: this function is always asynchronous. | |
10375 | */ | |
10376 | static int nfs41_free_stateid(struct nfs_server *server, | |
f0b0bf88 | 10377 | const nfs4_stateid *stateid, |
a52458b4 | 10378 | const struct cred *cred, |
7c1d5fae TM |
10379 | bool privileged) |
10380 | { | |
9aeda35f BS |
10381 | struct rpc_message msg = { |
10382 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID], | |
ab7cb0df | 10383 | .rpc_cred = cred, |
9aeda35f | 10384 | }; |
7c1d5fae TM |
10385 | struct rpc_task_setup task_setup = { |
10386 | .rpc_client = server->client, | |
10387 | .rpc_message = &msg, | |
10388 | .callback_ops = &nfs41_free_stateid_ops, | |
85e39fee | 10389 | .flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE, |
7c1d5fae TM |
10390 | }; |
10391 | struct nfs_free_stateid_data *data; | |
2f261020 | 10392 | struct rpc_task *task; |
576acc25 SM |
10393 | struct nfs_client *clp = server->nfs_client; |
10394 | ||
10395 | if (!refcount_inc_not_zero(&clp->cl_count)) | |
10396 | return -EIO; | |
9aeda35f | 10397 | |
3787d506 WAA |
10398 | nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID, |
10399 | &task_setup.rpc_client, &msg); | |
10400 | ||
38527b15 | 10401 | dprintk("NFS call free_stateid %p\n", stateid); |
da48f267 | 10402 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
7c1d5fae | 10403 | if (!data) |
2f261020 | 10404 | return -ENOMEM; |
7c1d5fae TM |
10405 | data->server = server; |
10406 | nfs4_stateid_copy(&data->args.stateid, stateid); | |
10407 | ||
10408 | task_setup.callback_data = data; | |
10409 | ||
10410 | msg.rpc_argp = &data->args; | |
10411 | msg.rpc_resp = &data->res; | |
fba83f34 | 10412 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged); |
2f261020 | 10413 | task = rpc_run_task(&task_setup); |
7c1d5fae TM |
10414 | if (IS_ERR(task)) |
10415 | return PTR_ERR(task); | |
7c1d5fae | 10416 | rpc_put_task(task); |
f0b0bf88 | 10417 | return 0; |
9aeda35f | 10418 | } |
36281caa | 10419 | |
f1cdae87 JL |
10420 | static void |
10421 | nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp) | |
c8b2d0bf | 10422 | { |
a52458b4 | 10423 | const struct cred *cred = lsp->ls_state->owner->so_cred; |
c8b2d0bf | 10424 | |
f0b0bf88 | 10425 | nfs41_free_stateid(server, &lsp->ls_stateid, cred, false); |
c8b2d0bf | 10426 | nfs4_free_lock_state(server, lsp); |
c8b2d0bf TM |
10427 | } |
10428 | ||
36281caa TM |
10429 | static bool nfs41_match_stateid(const nfs4_stateid *s1, |
10430 | const nfs4_stateid *s2) | |
10431 | { | |
93b717fd TM |
10432 | if (s1->type != s2->type) |
10433 | return false; | |
10434 | ||
2d2f24ad | 10435 | if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0) |
36281caa TM |
10436 | return false; |
10437 | ||
2d2f24ad | 10438 | if (s1->seqid == s2->seqid) |
36281caa | 10439 | return true; |
36281caa | 10440 | |
045c5519 | 10441 | return s1->seqid == 0 || s2->seqid == 0; |
36281caa TM |
10442 | } |
10443 | ||
557134a3 AA |
10444 | #endif /* CONFIG_NFS_V4_1 */ |
10445 | ||
36281caa TM |
10446 | static bool nfs4_match_stateid(const nfs4_stateid *s1, |
10447 | const nfs4_stateid *s2) | |
10448 | { | |
f597c537 | 10449 | return nfs4_stateid_match(s1, s2); |
36281caa TM |
10450 | } |
10451 | ||
10452 | ||
17280175 | 10453 | static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = { |
7eff03ae | 10454 | .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, |
b79a4a1b | 10455 | .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, |
1da177e4 LT |
10456 | .recover_open = nfs4_open_reclaim, |
10457 | .recover_lock = nfs4_lock_reclaim, | |
591d71cb | 10458 | .establish_clid = nfs4_init_clientid, |
05f4c350 | 10459 | .detect_trunking = nfs40_discover_server_trunking, |
1da177e4 LT |
10460 | }; |
10461 | ||
591d71cb | 10462 | #if defined(CONFIG_NFS_V4_1) |
17280175 | 10463 | static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = { |
591d71cb AA |
10464 | .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, |
10465 | .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, | |
10466 | .recover_open = nfs4_open_reclaim, | |
10467 | .recover_lock = nfs4_lock_reclaim, | |
4d643d1d | 10468 | .establish_clid = nfs41_init_clientid, |
fce5c838 | 10469 | .reclaim_complete = nfs41_proc_reclaim_complete, |
05f4c350 | 10470 | .detect_trunking = nfs41_discover_server_trunking, |
591d71cb AA |
10471 | }; |
10472 | #endif /* CONFIG_NFS_V4_1 */ | |
10473 | ||
17280175 | 10474 | static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = { |
591d71cb AA |
10475 | .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, |
10476 | .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, | |
4dfd4f7a | 10477 | .recover_open = nfs40_open_expired, |
591d71cb AA |
10478 | .recover_lock = nfs4_lock_expired, |
10479 | .establish_clid = nfs4_init_clientid, | |
10480 | }; | |
10481 | ||
10482 | #if defined(CONFIG_NFS_V4_1) | |
17280175 | 10483 | static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = { |
7eff03ae | 10484 | .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, |
b79a4a1b | 10485 | .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, |
f062eb6c BS |
10486 | .recover_open = nfs41_open_expired, |
10487 | .recover_lock = nfs41_lock_expired, | |
4d643d1d | 10488 | .establish_clid = nfs41_init_clientid, |
1da177e4 | 10489 | }; |
591d71cb | 10490 | #endif /* CONFIG_NFS_V4_1 */ |
1da177e4 | 10491 | |
17280175 | 10492 | static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = { |
29fba38b | 10493 | .sched_state_renewal = nfs4_proc_async_renew, |
f15e1e8b | 10494 | .get_state_renewal_cred = nfs4_get_renew_cred, |
8e69514f | 10495 | .renew_lease = nfs4_proc_renew, |
29fba38b BH |
10496 | }; |
10497 | ||
10498 | #if defined(CONFIG_NFS_V4_1) | |
17280175 | 10499 | static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = { |
29fba38b | 10500 | .sched_state_renewal = nfs41_proc_async_sequence, |
f15e1e8b | 10501 | .get_state_renewal_cred = nfs4_get_machine_cred, |
8e69514f | 10502 | .renew_lease = nfs4_proc_sequence, |
29fba38b BH |
10503 | }; |
10504 | #endif | |
10505 | ||
ec011fe8 | 10506 | static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = { |
b03d735b | 10507 | .get_locations = _nfs40_proc_get_locations, |
44c99933 | 10508 | .fsid_present = _nfs40_proc_fsid_present, |
ec011fe8 CL |
10509 | }; |
10510 | ||
10511 | #if defined(CONFIG_NFS_V4_1) | |
10512 | static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = { | |
b03d735b | 10513 | .get_locations = _nfs41_proc_get_locations, |
44c99933 | 10514 | .fsid_present = _nfs41_proc_fsid_present, |
ec011fe8 CL |
10515 | }; |
10516 | #endif /* CONFIG_NFS_V4_1 */ | |
10517 | ||
97dc1359 TM |
10518 | static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = { |
10519 | .minor_version = 0, | |
39c6daae TM |
10520 | .init_caps = NFS_CAP_READDIRPLUS |
10521 | | NFS_CAP_ATOMIC_OPEN | |
39c6daae | 10522 | | NFS_CAP_POSIX_LOCK, |
abf79bb3 CL |
10523 | .init_client = nfs40_init_client, |
10524 | .shutdown_client = nfs40_shutdown_client, | |
36281caa | 10525 | .match_stateid = nfs4_match_stateid, |
fca78d6d | 10526 | .find_root_sec = nfs4_find_root_sec, |
c8b2d0bf | 10527 | .free_lock_state = nfs4_release_lockowner, |
45870d69 | 10528 | .test_and_free_expired = nfs40_test_and_free_expired_stateid, |
63f5f796 | 10529 | .alloc_seqid = nfs_alloc_seqid, |
9915ea7e | 10530 | .call_sync_ops = &nfs40_call_sync_ops, |
c48f4f35 TM |
10531 | .reboot_recovery_ops = &nfs40_reboot_recovery_ops, |
10532 | .nograce_recovery_ops = &nfs40_nograce_recovery_ops, | |
10533 | .state_renewal_ops = &nfs40_state_renewal_ops, | |
ec011fe8 | 10534 | .mig_recovery_ops = &nfs40_mig_recovery_ops, |
97dc1359 TM |
10535 | }; |
10536 | ||
10537 | #if defined(CONFIG_NFS_V4_1) | |
63f5f796 TM |
10538 | static struct nfs_seqid * |
10539 | nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2) | |
10540 | { | |
10541 | return NULL; | |
10542 | } | |
10543 | ||
97dc1359 TM |
10544 | static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = { |
10545 | .minor_version = 1, | |
39c6daae TM |
10546 | .init_caps = NFS_CAP_READDIRPLUS |
10547 | | NFS_CAP_ATOMIC_OPEN | |
3b66486c | 10548 | | NFS_CAP_POSIX_LOCK |
49f9a0fa | 10549 | | NFS_CAP_STATEID_NFSV41 |
6e01260c | 10550 | | NFS_CAP_ATOMIC_OPEN_V1 |
118f09ed OK |
10551 | | NFS_CAP_LGOPEN |
10552 | | NFS_CAP_MOVEABLE, | |
abf79bb3 CL |
10553 | .init_client = nfs41_init_client, |
10554 | .shutdown_client = nfs41_shutdown_client, | |
36281caa | 10555 | .match_stateid = nfs41_match_stateid, |
fca78d6d | 10556 | .find_root_sec = nfs41_find_root_sec, |
c8b2d0bf | 10557 | .free_lock_state = nfs41_free_lock_state, |
45870d69 | 10558 | .test_and_free_expired = nfs41_test_and_free_expired_stateid, |
63f5f796 | 10559 | .alloc_seqid = nfs_alloc_no_seqid, |
04fa2c6b | 10560 | .session_trunk = nfs4_test_session_trunk, |
9915ea7e | 10561 | .call_sync_ops = &nfs41_call_sync_ops, |
c48f4f35 TM |
10562 | .reboot_recovery_ops = &nfs41_reboot_recovery_ops, |
10563 | .nograce_recovery_ops = &nfs41_nograce_recovery_ops, | |
10564 | .state_renewal_ops = &nfs41_state_renewal_ops, | |
ec011fe8 | 10565 | .mig_recovery_ops = &nfs41_mig_recovery_ops, |
97dc1359 TM |
10566 | }; |
10567 | #endif | |
10568 | ||
42c2c424 SD |
10569 | #if defined(CONFIG_NFS_V4_2) |
10570 | static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = { | |
10571 | .minor_version = 2, | |
7017310a BS |
10572 | .init_caps = NFS_CAP_READDIRPLUS |
10573 | | NFS_CAP_ATOMIC_OPEN | |
7017310a BS |
10574 | | NFS_CAP_POSIX_LOCK |
10575 | | NFS_CAP_STATEID_NFSV41 | |
e983120e | 10576 | | NFS_CAP_ATOMIC_OPEN_V1 |
6e01260c | 10577 | | NFS_CAP_LGOPEN |
f4ac1674 | 10578 | | NFS_CAP_ALLOCATE |
2e72448b | 10579 | | NFS_CAP_COPY |
cb95deea | 10580 | | NFS_CAP_OFFLOAD_CANCEL |
0491567b | 10581 | | NFS_CAP_COPY_NOTIFY |
624bd5b7 | 10582 | | NFS_CAP_DEALLOCATE |
6c5a0d89 | 10583 | | NFS_CAP_SEEK |
e5341f3a | 10584 | | NFS_CAP_LAYOUTSTATS |
3eb86093 | 10585 | | NFS_CAP_CLONE |
c5675526 | 10586 | | NFS_CAP_LAYOUTERROR |
118f09ed OK |
10587 | | NFS_CAP_READ_PLUS |
10588 | | NFS_CAP_MOVEABLE, | |
abf79bb3 CL |
10589 | .init_client = nfs41_init_client, |
10590 | .shutdown_client = nfs41_shutdown_client, | |
42c2c424 SD |
10591 | .match_stateid = nfs41_match_stateid, |
10592 | .find_root_sec = nfs41_find_root_sec, | |
7017310a | 10593 | .free_lock_state = nfs41_free_lock_state, |
9915ea7e | 10594 | .call_sync_ops = &nfs41_call_sync_ops, |
45870d69 | 10595 | .test_and_free_expired = nfs41_test_and_free_expired_stateid, |
63f5f796 | 10596 | .alloc_seqid = nfs_alloc_no_seqid, |
04fa2c6b | 10597 | .session_trunk = nfs4_test_session_trunk, |
42c2c424 SD |
10598 | .reboot_recovery_ops = &nfs41_reboot_recovery_ops, |
10599 | .nograce_recovery_ops = &nfs41_nograce_recovery_ops, | |
10600 | .state_renewal_ops = &nfs41_state_renewal_ops, | |
18e3b739 | 10601 | .mig_recovery_ops = &nfs41_mig_recovery_ops, |
42c2c424 SD |
10602 | }; |
10603 | #endif | |
10604 | ||
97dc1359 TM |
10605 | const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = { |
10606 | [0] = &nfs_v4_0_minor_ops, | |
10607 | #if defined(CONFIG_NFS_V4_1) | |
10608 | [1] = &nfs_v4_1_minor_ops, | |
10609 | #endif | |
42c2c424 SD |
10610 | #if defined(CONFIG_NFS_V4_2) |
10611 | [2] = &nfs_v4_2_minor_ops, | |
10612 | #endif | |
97dc1359 TM |
10613 | }; |
10614 | ||
13997823 | 10615 | static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size) |
c4803c49 | 10616 | { |
012a211a | 10617 | ssize_t error, error2, error3; |
251a658b | 10618 | size_t left = size; |
c4803c49 | 10619 | |
251a658b | 10620 | error = generic_listxattr(dentry, list, left); |
c4803c49 AG |
10621 | if (error < 0) |
10622 | return error; | |
10623 | if (list) { | |
10624 | list += error; | |
251a658b | 10625 | left -= error; |
c4803c49 AG |
10626 | } |
10627 | ||
251a658b | 10628 | error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, left); |
c4803c49 AG |
10629 | if (error2 < 0) |
10630 | return error2; | |
012a211a FL |
10631 | |
10632 | if (list) { | |
10633 | list += error2; | |
251a658b | 10634 | left -= error2; |
012a211a FL |
10635 | } |
10636 | ||
251a658b | 10637 | error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, left); |
012a211a FL |
10638 | if (error3 < 0) |
10639 | return error3; | |
10640 | ||
251a658b JM |
10641 | error += error2 + error3; |
10642 | if (size && error > size) | |
10643 | return -ERANGE; | |
10644 | return error; | |
c4803c49 AG |
10645 | } |
10646 | ||
4dc73c67 N |
10647 | static void nfs4_enable_swap(struct inode *inode) |
10648 | { | |
10649 | /* The state manager thread must always be running. | |
10650 | * It will notice the client is a swapper, and stay put. | |
10651 | */ | |
10652 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; | |
10653 | ||
10654 | nfs4_schedule_state_manager(clp); | |
10655 | } | |
10656 | ||
10657 | static void nfs4_disable_swap(struct inode *inode) | |
10658 | { | |
10659 | /* The state manager thread will now exit once it is | |
10660 | * woken. | |
10661 | */ | |
5bab56ff N |
10662 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
10663 | ||
956fd46f TM |
10664 | set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state); |
10665 | clear_bit(NFS4CLNT_MANAGER_AVAILABLE, &clp->cl_state); | |
10666 | wake_up_var(&clp->cl_state); | |
4dc73c67 N |
10667 | } |
10668 | ||
17f26b12 | 10669 | static const struct inode_operations nfs4_dir_inode_operations = { |
73a79706 BS |
10670 | .create = nfs_create, |
10671 | .lookup = nfs_lookup, | |
10672 | .atomic_open = nfs_atomic_open, | |
10673 | .link = nfs_link, | |
10674 | .unlink = nfs_unlink, | |
10675 | .symlink = nfs_symlink, | |
10676 | .mkdir = nfs_mkdir, | |
10677 | .rmdir = nfs_rmdir, | |
10678 | .mknod = nfs_mknod, | |
10679 | .rename = nfs_rename, | |
10680 | .permission = nfs_permission, | |
10681 | .getattr = nfs_getattr, | |
10682 | .setattr = nfs_setattr, | |
c4803c49 | 10683 | .listxattr = nfs4_listxattr, |
73a79706 BS |
10684 | }; |
10685 | ||
92e1d5be | 10686 | static const struct inode_operations nfs4_file_inode_operations = { |
6b3b5496 BF |
10687 | .permission = nfs_permission, |
10688 | .getattr = nfs_getattr, | |
10689 | .setattr = nfs_setattr, | |
c4803c49 | 10690 | .listxattr = nfs4_listxattr, |
6b3b5496 BF |
10691 | }; |
10692 | ||
509de811 | 10693 | const struct nfs_rpc_ops nfs_v4_clientops = { |
1da177e4 LT |
10694 | .version = 4, /* protocol version */ |
10695 | .dentry_ops = &nfs4_dentry_operations, | |
10696 | .dir_inode_ops = &nfs4_dir_inode_operations, | |
6b3b5496 | 10697 | .file_inode_ops = &nfs4_file_inode_operations, |
1788ea6e | 10698 | .file_ops = &nfs4_file_operations, |
1da177e4 | 10699 | .getroot = nfs4_proc_get_root, |
281cad46 | 10700 | .submount = nfs4_submount, |
f2aedb71 | 10701 | .try_get_tree = nfs4_try_get_tree, |
1da177e4 LT |
10702 | .getattr = nfs4_proc_getattr, |
10703 | .setattr = nfs4_proc_setattr, | |
10704 | .lookup = nfs4_proc_lookup, | |
5b5faaf6 | 10705 | .lookupp = nfs4_proc_lookupp, |
1da177e4 LT |
10706 | .access = nfs4_proc_access, |
10707 | .readlink = nfs4_proc_readlink, | |
1da177e4 LT |
10708 | .create = nfs4_proc_create, |
10709 | .remove = nfs4_proc_remove, | |
10710 | .unlink_setup = nfs4_proc_unlink_setup, | |
34e137cc | 10711 | .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare, |
1da177e4 | 10712 | .unlink_done = nfs4_proc_unlink_done, |
d3d4152a | 10713 | .rename_setup = nfs4_proc_rename_setup, |
c6bfa1a1 | 10714 | .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare, |
d3d4152a | 10715 | .rename_done = nfs4_proc_rename_done, |
1da177e4 LT |
10716 | .link = nfs4_proc_link, |
10717 | .symlink = nfs4_proc_symlink, | |
10718 | .mkdir = nfs4_proc_mkdir, | |
912678db | 10719 | .rmdir = nfs4_proc_rmdir, |
1da177e4 LT |
10720 | .readdir = nfs4_proc_readdir, |
10721 | .mknod = nfs4_proc_mknod, | |
10722 | .statfs = nfs4_proc_statfs, | |
10723 | .fsinfo = nfs4_proc_fsinfo, | |
10724 | .pathconf = nfs4_proc_pathconf, | |
e9326dca | 10725 | .set_capabilities = nfs4_server_capabilities, |
1da177e4 | 10726 | .decode_dirent = nfs4_decode_dirent, |
a4cdda59 | 10727 | .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare, |
1da177e4 | 10728 | .read_setup = nfs4_proc_read_setup, |
ec06c096 | 10729 | .read_done = nfs4_read_done, |
1da177e4 | 10730 | .write_setup = nfs4_proc_write_setup, |
788e7a89 | 10731 | .write_done = nfs4_write_done, |
1da177e4 | 10732 | .commit_setup = nfs4_proc_commit_setup, |
0b7c0153 | 10733 | .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare, |
788e7a89 | 10734 | .commit_done = nfs4_commit_done, |
1da177e4 | 10735 | .lock = nfs4_proc_lock, |
e50a1c2e | 10736 | .clear_acl_cache = nfs4_zap_acl_attr, |
7fe5c398 | 10737 | .close_context = nfs4_close_context, |
2b484297 | 10738 | .open_context = nfs4_atomic_open, |
011e2a7f | 10739 | .have_delegation = nfs4_have_delegation, |
6663ee7f | 10740 | .alloc_client = nfs4_alloc_client, |
45a52a02 | 10741 | .init_client = nfs4_init_client, |
cdb7eced | 10742 | .free_client = nfs4_free_client, |
1179acc6 BS |
10743 | .create_server = nfs4_create_server, |
10744 | .clone_server = nfs_clone_server, | |
1976b2b3 | 10745 | .discover_trunking = nfs4_discover_trunking, |
4dc73c67 N |
10746 | .enable_swap = nfs4_enable_swap, |
10747 | .disable_swap = nfs4_disable_swap, | |
1da177e4 LT |
10748 | }; |
10749 | ||
64c2ce8b | 10750 | static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = { |
98e9cb57 | 10751 | .name = XATTR_NAME_NFSV4_ACL, |
64c2ce8b AK |
10752 | .list = nfs4_xattr_list_nfs4_acl, |
10753 | .get = nfs4_xattr_get_nfs4_acl, | |
10754 | .set = nfs4_xattr_set_nfs4_acl, | |
10755 | }; | |
10756 | ||
71342db0 TM |
10757 | #if defined(CONFIG_NFS_V4_1) |
10758 | static const struct xattr_handler nfs4_xattr_nfs4_dacl_handler = { | |
10759 | .name = XATTR_NAME_NFSV4_DACL, | |
10760 | .list = nfs4_xattr_list_nfs4_dacl, | |
10761 | .get = nfs4_xattr_get_nfs4_dacl, | |
10762 | .set = nfs4_xattr_set_nfs4_dacl, | |
10763 | }; | |
10764 | ||
10765 | static const struct xattr_handler nfs4_xattr_nfs4_sacl_handler = { | |
10766 | .name = XATTR_NAME_NFSV4_SACL, | |
10767 | .list = nfs4_xattr_list_nfs4_sacl, | |
10768 | .get = nfs4_xattr_get_nfs4_sacl, | |
10769 | .set = nfs4_xattr_set_nfs4_sacl, | |
10770 | }; | |
10771 | #endif | |
10772 | ||
012a211a FL |
10773 | #ifdef CONFIG_NFS_V4_2 |
10774 | static const struct xattr_handler nfs4_xattr_nfs4_user_handler = { | |
10775 | .prefix = XATTR_USER_PREFIX, | |
10776 | .get = nfs4_xattr_get_nfs4_user, | |
10777 | .set = nfs4_xattr_set_nfs4_user, | |
10778 | }; | |
10779 | #endif | |
10780 | ||
f496647e | 10781 | const struct xattr_handler * const nfs4_xattr_handlers[] = { |
64c2ce8b | 10782 | &nfs4_xattr_nfs4_acl_handler, |
71342db0 TM |
10783 | #if defined(CONFIG_NFS_V4_1) |
10784 | &nfs4_xattr_nfs4_dacl_handler, | |
10785 | &nfs4_xattr_nfs4_sacl_handler, | |
10786 | #endif | |
c9bccef6 DQ |
10787 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
10788 | &nfs4_xattr_nfs4_label_handler, | |
012a211a FL |
10789 | #endif |
10790 | #ifdef CONFIG_NFS_V4_2 | |
10791 | &nfs4_xattr_nfs4_user_handler, | |
c9bccef6 | 10792 | #endif |
64c2ce8b AK |
10793 | NULL |
10794 | }; |