]>
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" |
40c64c26 | 66 | #include "nfs4idmap.h" |
73e39aaa | 67 | #include "nfs4session.h" |
de242c0b | 68 | #include "fscache.h" |
1da177e4 | 69 | |
c6d01c6f TM |
70 | #include "nfs4trace.h" |
71 | ||
1da177e4 LT |
72 | #define NFSDBG_FACILITY NFSDBG_PROC |
73 | ||
30846df0 TM |
74 | #define NFS4_BITMASK_SZ 3 |
75 | ||
2066fe89 | 76 | #define NFS4_POLL_RETRY_MIN (HZ/10) |
1da177e4 LT |
77 | #define NFS4_POLL_RETRY_MAX (15*HZ) |
78 | ||
a1d1c4f1 TM |
79 | /* file attributes which can be mapped to nfs attributes */ |
80 | #define NFS4_VALID_ATTRS (ATTR_MODE \ | |
81 | | ATTR_UID \ | |
82 | | ATTR_GID \ | |
83 | | ATTR_SIZE \ | |
84 | | ATTR_ATIME \ | |
85 | | ATTR_MTIME \ | |
86 | | ATTR_CTIME \ | |
87 | | ATTR_ATIME_SET \ | |
88 | | ATTR_MTIME_SET) | |
89 | ||
cdd4e68b | 90 | struct nfs4_opendata; |
b257957e | 91 | static int _nfs4_recover_proc_open(struct nfs4_opendata *data); |
1da177e4 | 92 | static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *); |
81934ddb | 93 | static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr); |
a841b54d | 94 | static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode); |
a52458b4 | 95 | static int nfs4_do_setattr(struct inode *inode, const struct cred *cred, |
0ab64e0e | 96 | struct nfs_fattr *fattr, struct iattr *sattr, |
29b59f94 | 97 | struct nfs_open_context *ctx, struct nfs4_label *ilabel, |
1775fd3e | 98 | struct nfs4_label *olabel); |
f062eb6c | 99 | #ifdef CONFIG_NFS_V4_1 |
3be0f80b | 100 | static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, |
a52458b4 | 101 | const struct cred *cred, |
3be0f80b TM |
102 | struct nfs4_slot *slot, |
103 | bool is_privileged); | |
ab7cb0df | 104 | static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *, |
a52458b4 | 105 | const struct cred *); |
f0b0bf88 | 106 | static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *, |
a52458b4 | 107 | const struct cred *, bool); |
f062eb6c | 108 | #endif |
aa9c2669 DQ |
109 | |
110 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL | |
111 | static inline struct nfs4_label * | |
112 | nfs4_label_init_security(struct inode *dir, struct dentry *dentry, | |
113 | struct iattr *sattr, struct nfs4_label *label) | |
114 | { | |
115 | int err; | |
116 | ||
117 | if (label == NULL) | |
118 | return NULL; | |
119 | ||
120 | if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0) | |
121 | return NULL; | |
122 | ||
aa9c2669 DQ |
123 | err = security_dentry_init_security(dentry, sattr->ia_mode, |
124 | &dentry->d_name, (void **)&label->label, &label->len); | |
125 | if (err == 0) | |
126 | return label; | |
127 | ||
128 | return NULL; | |
129 | } | |
130 | static inline void | |
131 | nfs4_label_release_security(struct nfs4_label *label) | |
132 | { | |
133 | if (label) | |
134 | security_release_secctx(label->label, label->len); | |
135 | } | |
136 | static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label) | |
137 | { | |
138 | if (label) | |
139 | return server->attr_bitmask; | |
140 | ||
141 | return server->attr_bitmask_nl; | |
142 | } | |
143 | #else | |
144 | static inline struct nfs4_label * | |
145 | nfs4_label_init_security(struct inode *dir, struct dentry *dentry, | |
146 | struct iattr *sattr, struct nfs4_label *l) | |
147 | { return NULL; } | |
148 | static inline void | |
149 | nfs4_label_release_security(struct nfs4_label *label) | |
150 | { return; } | |
151 | static inline u32 * | |
152 | nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label) | |
153 | { return server->attr_bitmask; } | |
154 | #endif | |
155 | ||
1da177e4 | 156 | /* Prevent leaks of NFSv4 errors into userland */ |
46f72f57 | 157 | static int nfs4_map_errors(int err) |
1da177e4 | 158 | { |
52567b03 TM |
159 | if (err >= -1000) |
160 | return err; | |
161 | switch (err) { | |
162 | case -NFS4ERR_RESOURCE: | |
30005121 WAA |
163 | case -NFS4ERR_LAYOUTTRYLATER: |
164 | case -NFS4ERR_RECALLCONFLICT: | |
52567b03 | 165 | return -EREMOTEIO; |
7ebb9315 | 166 | case -NFS4ERR_WRONGSEC: |
8897538e | 167 | case -NFS4ERR_WRONG_CRED: |
7ebb9315 | 168 | return -EPERM; |
3ddeb7c5 TM |
169 | case -NFS4ERR_BADOWNER: |
170 | case -NFS4ERR_BADNAME: | |
171 | return -EINVAL; | |
fb13bfa7 TM |
172 | case -NFS4ERR_SHARE_DENIED: |
173 | return -EACCES; | |
f25efd85 SD |
174 | case -NFS4ERR_MINOR_VERS_MISMATCH: |
175 | return -EPROTONOSUPPORT; | |
6e3cf241 TM |
176 | case -NFS4ERR_FILE_OPEN: |
177 | return -EBUSY; | |
52567b03 | 178 | default: |
1da177e4 | 179 | dprintk("%s could not handle NFSv4 error %d\n", |
3110ff80 | 180 | __func__, -err); |
52567b03 | 181 | break; |
1da177e4 | 182 | } |
52567b03 | 183 | return -EIO; |
1da177e4 LT |
184 | } |
185 | ||
186 | /* | |
187 | * This is our standard bitmap for GETATTR requests. | |
188 | */ | |
1549210f | 189 | const u32 nfs4_fattr_bitmap[3] = { |
1da177e4 LT |
190 | FATTR4_WORD0_TYPE |
191 | | FATTR4_WORD0_CHANGE | |
192 | | FATTR4_WORD0_SIZE | |
193 | | FATTR4_WORD0_FSID | |
194 | | FATTR4_WORD0_FILEID, | |
195 | FATTR4_WORD1_MODE | |
196 | | FATTR4_WORD1_NUMLINKS | |
197 | | FATTR4_WORD1_OWNER | |
198 | | FATTR4_WORD1_OWNER_GROUP | |
199 | | FATTR4_WORD1_RAWDEV | |
200 | | FATTR4_WORD1_SPACE_USED | |
201 | | FATTR4_WORD1_TIME_ACCESS | |
202 | | FATTR4_WORD1_TIME_METADATA | |
ea96d1ec AS |
203 | | FATTR4_WORD1_TIME_MODIFY |
204 | | FATTR4_WORD1_MOUNTED_ON_FILEID, | |
aa9c2669 DQ |
205 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
206 | FATTR4_WORD2_SECURITY_LABEL | |
207 | #endif | |
1da177e4 LT |
208 | }; |
209 | ||
1549210f TM |
210 | static const u32 nfs4_pnfs_open_bitmap[3] = { |
211 | FATTR4_WORD0_TYPE | |
212 | | FATTR4_WORD0_CHANGE | |
213 | | FATTR4_WORD0_SIZE | |
214 | | FATTR4_WORD0_FSID | |
215 | | FATTR4_WORD0_FILEID, | |
216 | FATTR4_WORD1_MODE | |
217 | | FATTR4_WORD1_NUMLINKS | |
218 | | FATTR4_WORD1_OWNER | |
219 | | FATTR4_WORD1_OWNER_GROUP | |
220 | | FATTR4_WORD1_RAWDEV | |
221 | | FATTR4_WORD1_SPACE_USED | |
222 | | FATTR4_WORD1_TIME_ACCESS | |
223 | | FATTR4_WORD1_TIME_METADATA | |
224 | | FATTR4_WORD1_TIME_MODIFY, | |
225 | FATTR4_WORD2_MDSTHRESHOLD | |
95864c91 TM |
226 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
227 | | FATTR4_WORD2_SECURITY_LABEL | |
228 | #endif | |
1549210f TM |
229 | }; |
230 | ||
e23008ec AA |
231 | static const u32 nfs4_open_noattr_bitmap[3] = { |
232 | FATTR4_WORD0_TYPE | |
e23008ec AA |
233 | | FATTR4_WORD0_FILEID, |
234 | }; | |
235 | ||
a09df2ca | 236 | const u32 nfs4_statfs_bitmap[3] = { |
1da177e4 LT |
237 | FATTR4_WORD0_FILES_AVAIL |
238 | | FATTR4_WORD0_FILES_FREE | |
239 | | FATTR4_WORD0_FILES_TOTAL, | |
240 | FATTR4_WORD1_SPACE_AVAIL | |
241 | | FATTR4_WORD1_SPACE_FREE | |
242 | | FATTR4_WORD1_SPACE_TOTAL | |
243 | }; | |
244 | ||
a09df2ca | 245 | const u32 nfs4_pathconf_bitmap[3] = { |
1da177e4 LT |
246 | FATTR4_WORD0_MAXLINK |
247 | | FATTR4_WORD0_MAXNAME, | |
248 | 0 | |
249 | }; | |
250 | ||
dae100c2 | 251 | const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE |
1da177e4 LT |
252 | | FATTR4_WORD0_MAXREAD |
253 | | FATTR4_WORD0_MAXWRITE | |
254 | | FATTR4_WORD0_LEASE_TIME, | |
55b6e774 | 255 | FATTR4_WORD1_TIME_DELTA |
dae100c2 FI |
256 | | FATTR4_WORD1_FS_LAYOUT_TYPES, |
257 | FATTR4_WORD2_LAYOUT_BLKSIZE | |
2a92ee92 | 258 | | FATTR4_WORD2_CLONE_BLKSIZE |
1da177e4 LT |
259 | }; |
260 | ||
a09df2ca | 261 | const u32 nfs4_fs_locations_bitmap[3] = { |
c05cefcc | 262 | FATTR4_WORD0_CHANGE |
830b8e33 MN |
263 | | FATTR4_WORD0_SIZE |
264 | | FATTR4_WORD0_FSID | |
265 | | FATTR4_WORD0_FILEID | |
266 | | FATTR4_WORD0_FS_LOCATIONS, | |
c05cefcc | 267 | FATTR4_WORD1_OWNER |
830b8e33 MN |
268 | | FATTR4_WORD1_OWNER_GROUP |
269 | | FATTR4_WORD1_RAWDEV | |
270 | | FATTR4_WORD1_SPACE_USED | |
271 | | FATTR4_WORD1_TIME_ACCESS | |
272 | | FATTR4_WORD1_TIME_METADATA | |
273 | | FATTR4_WORD1_TIME_MODIFY | |
a09df2ca | 274 | | FATTR4_WORD1_MOUNTED_ON_FILEID, |
830b8e33 MN |
275 | }; |
276 | ||
30846df0 TM |
277 | static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src, |
278 | struct inode *inode) | |
279 | { | |
280 | unsigned long cache_validity; | |
281 | ||
282 | memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst)); | |
283 | if (!inode || !nfs4_have_delegation(inode, FMODE_READ)) | |
284 | return; | |
285 | ||
286 | cache_validity = READ_ONCE(NFS_I(inode)->cache_validity); | |
287 | if (!(cache_validity & NFS_INO_REVAL_FORCED)) | |
288 | cache_validity &= ~(NFS_INO_INVALID_CHANGE | |
289 | | NFS_INO_INVALID_SIZE); | |
290 | ||
291 | if (!(cache_validity & NFS_INO_INVALID_SIZE)) | |
292 | dst[0] &= ~FATTR4_WORD0_SIZE; | |
293 | ||
294 | if (!(cache_validity & NFS_INO_INVALID_CHANGE)) | |
295 | dst[0] &= ~FATTR4_WORD0_CHANGE; | |
296 | } | |
297 | ||
298 | static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst, | |
299 | const __u32 *src, struct inode *inode) | |
300 | { | |
301 | nfs4_bitmap_copy_adjust(dst, src, inode); | |
302 | } | |
303 | ||
bc4785cd | 304 | static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry, |
1da177e4 LT |
305 | struct nfs4_readdir_arg *readdir) |
306 | { | |
18fe6a23 | 307 | unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE; |
0dbb4c67 | 308 | __be32 *start, *p; |
1da177e4 | 309 | |
1da177e4 | 310 | if (cookie > 2) { |
b7ef1956 | 311 | readdir->cookie = cookie; |
1da177e4 LT |
312 | memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier)); |
313 | return; | |
314 | } | |
315 | ||
316 | readdir->cookie = 0; | |
317 | memset(&readdir->verifier, 0, sizeof(readdir->verifier)); | |
318 | if (cookie == 2) | |
319 | return; | |
320 | ||
321 | /* | |
322 | * NFSv4 servers do not return entries for '.' and '..' | |
323 | * Therefore, we fake these entries here. We let '.' | |
324 | * have cookie 0 and '..' have cookie 1. Note that | |
325 | * when talking to the server, we always send cookie 0 | |
326 | * instead of 1 or 2. | |
327 | */ | |
2b86ce2d | 328 | start = p = kmap_atomic(*readdir->pages); |
1da177e4 LT |
329 | |
330 | if (cookie == 0) { | |
331 | *p++ = xdr_one; /* next */ | |
332 | *p++ = xdr_zero; /* cookie, first word */ | |
333 | *p++ = xdr_one; /* cookie, second word */ | |
334 | *p++ = xdr_one; /* entry len */ | |
335 | memcpy(p, ".\0\0\0", 4); /* entry */ | |
336 | p++; | |
337 | *p++ = xdr_one; /* bitmap length */ | |
18fe6a23 AS |
338 | *p++ = htonl(attrs); /* bitmap */ |
339 | *p++ = htonl(12); /* attribute buffer length */ | |
340 | *p++ = htonl(NF4DIR); | |
2b0143b5 | 341 | p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry))); |
1da177e4 LT |
342 | } |
343 | ||
344 | *p++ = xdr_one; /* next */ | |
345 | *p++ = xdr_zero; /* cookie, first word */ | |
346 | *p++ = xdr_two; /* cookie, second word */ | |
347 | *p++ = xdr_two; /* entry len */ | |
348 | memcpy(p, "..\0\0", 4); /* entry */ | |
349 | p++; | |
350 | *p++ = xdr_one; /* bitmap length */ | |
18fe6a23 AS |
351 | *p++ = htonl(attrs); /* bitmap */ |
352 | *p++ = htonl(12); /* attribute buffer length */ | |
353 | *p++ = htonl(NF4DIR); | |
2b0143b5 | 354 | p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent))); |
1da177e4 LT |
355 | |
356 | readdir->pgbase = (char *)p - (char *)start; | |
357 | readdir->count -= readdir->pgbase; | |
2b86ce2d | 358 | kunmap_atomic(start); |
1da177e4 LT |
359 | } |
360 | ||
26d36301 TM |
361 | static void nfs4_test_and_free_stateid(struct nfs_server *server, |
362 | nfs4_stateid *stateid, | |
a52458b4 | 363 | const struct cred *cred) |
26d36301 TM |
364 | { |
365 | const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops; | |
366 | ||
367 | ops->test_and_free_expired(server, stateid, cred); | |
368 | } | |
369 | ||
370 | static void __nfs4_free_revoked_stateid(struct nfs_server *server, | |
371 | nfs4_stateid *stateid, | |
a52458b4 | 372 | const struct cred *cred) |
26d36301 TM |
373 | { |
374 | stateid->type = NFS4_REVOKED_STATEID_TYPE; | |
375 | nfs4_test_and_free_stateid(server, stateid, cred); | |
376 | } | |
377 | ||
378 | static void nfs4_free_revoked_stateid(struct nfs_server *server, | |
379 | const nfs4_stateid *stateid, | |
a52458b4 | 380 | const struct cred *cred) |
26d36301 TM |
381 | { |
382 | nfs4_stateid tmp; | |
383 | ||
384 | nfs4_stateid_copy(&tmp, stateid); | |
385 | __nfs4_free_revoked_stateid(server, &tmp, cred); | |
386 | } | |
387 | ||
8478eaa1 N |
388 | static long nfs4_update_delay(long *timeout) |
389 | { | |
390 | long ret; | |
391 | if (!timeout) | |
392 | return NFS4_POLL_RETRY_MAX; | |
393 | if (*timeout <= 0) | |
394 | *timeout = NFS4_POLL_RETRY_MIN; | |
395 | if (*timeout > NFS4_POLL_RETRY_MAX) | |
396 | *timeout = NFS4_POLL_RETRY_MAX; | |
397 | ret = *timeout; | |
398 | *timeout <<= 1; | |
399 | return ret; | |
400 | } | |
401 | ||
0688e64b | 402 | static int nfs4_delay_killable(long *timeout) |
65de872e | 403 | { |
65de872e TM |
404 | might_sleep(); |
405 | ||
8478eaa1 N |
406 | freezable_schedule_timeout_killable_unsafe( |
407 | nfs4_update_delay(timeout)); | |
0688e64b TM |
408 | if (!__fatal_signal_pending(current)) |
409 | return 0; | |
410 | return -EINTR; | |
411 | } | |
412 | ||
413 | static int nfs4_delay_interruptible(long *timeout) | |
414 | { | |
415 | might_sleep(); | |
416 | ||
417 | freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout)); | |
418 | if (!signal_pending(current)) | |
419 | return 0; | |
420 | return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS; | |
421 | } | |
422 | ||
423 | static int nfs4_delay(long *timeout, bool interruptible) | |
424 | { | |
425 | if (interruptible) | |
426 | return nfs4_delay_interruptible(timeout); | |
427 | return nfs4_delay_killable(timeout); | |
65de872e TM |
428 | } |
429 | ||
50c80007 TM |
430 | static const nfs4_stateid * |
431 | nfs4_recoverable_stateid(const nfs4_stateid *stateid) | |
432 | { | |
433 | if (!stateid) | |
434 | return NULL; | |
435 | switch (stateid->type) { | |
436 | case NFS4_OPEN_STATEID_TYPE: | |
437 | case NFS4_LOCK_STATEID_TYPE: | |
438 | case NFS4_DELEGATION_STATEID_TYPE: | |
439 | return stateid; | |
440 | default: | |
441 | break; | |
442 | } | |
443 | return NULL; | |
444 | } | |
445 | ||
65de872e TM |
446 | /* This is the error handling routine for processes that are allowed |
447 | * to sleep. | |
448 | */ | |
b3c2aa07 TM |
449 | static int nfs4_do_handle_exception(struct nfs_server *server, |
450 | int errorcode, struct nfs4_exception *exception) | |
65de872e TM |
451 | { |
452 | struct nfs_client *clp = server->nfs_client; | |
9e33bed5 | 453 | struct nfs4_state *state = exception->state; |
50c80007 | 454 | const nfs4_stateid *stateid; |
3114ea7a | 455 | struct inode *inode = exception->inode; |
65de872e TM |
456 | int ret = errorcode; |
457 | ||
b3c2aa07 TM |
458 | exception->delay = 0; |
459 | exception->recovering = 0; | |
65de872e | 460 | exception->retry = 0; |
272289a3 | 461 | |
50c80007 | 462 | stateid = nfs4_recoverable_stateid(exception->stateid); |
272289a3 | 463 | if (stateid == NULL && state != NULL) |
50c80007 | 464 | stateid = nfs4_recoverable_stateid(&state->stateid); |
272289a3 | 465 | |
65de872e TM |
466 | switch(errorcode) { |
467 | case 0: | |
468 | return 0; | |
cf61eb26 TM |
469 | case -NFS4ERR_BADHANDLE: |
470 | case -ESTALE: | |
471 | if (inode != NULL && S_ISREG(inode->i_mode)) | |
472 | pnfs_destroy_layout(NFS_I(inode)); | |
473 | break; | |
5ba12443 TM |
474 | case -NFS4ERR_DELEG_REVOKED: |
475 | case -NFS4ERR_ADMIN_REVOKED: | |
272289a3 | 476 | case -NFS4ERR_EXPIRED: |
5ba12443 | 477 | case -NFS4ERR_BAD_STATEID: |
fefa1a81 | 478 | case -NFS4ERR_PARTNER_NO_AUTH: |
272289a3 TM |
479 | if (inode != NULL && stateid != NULL) { |
480 | nfs_inode_find_state_and_recover(inode, | |
481 | stateid); | |
482 | goto wait_on_recovery; | |
483 | } | |
01e03bdc | 484 | /* Fall through */ |
272289a3 | 485 | case -NFS4ERR_OPENMODE: |
8487c479 TM |
486 | if (inode) { |
487 | int err; | |
488 | ||
489 | err = nfs_async_inode_return_delegation(inode, | |
490 | stateid); | |
491 | if (err == 0) | |
492 | goto wait_on_recovery; | |
493 | if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) { | |
494 | exception->retry = 1; | |
495 | break; | |
496 | } | |
497 | } | |
3114ea7a TM |
498 | if (state == NULL) |
499 | break; | |
5d422301 TM |
500 | ret = nfs4_schedule_stateid_recovery(server, state); |
501 | if (ret < 0) | |
502 | break; | |
3114ea7a | 503 | goto wait_on_recovery; |
65de872e | 504 | case -NFS4ERR_STALE_STATEID: |
a2c0b9e2 | 505 | case -NFS4ERR_STALE_CLIENTID: |
0400a6b0 TM |
506 | nfs4_schedule_lease_recovery(clp); |
507 | goto wait_on_recovery; | |
519ae255 CL |
508 | case -NFS4ERR_MOVED: |
509 | ret = nfs4_schedule_migration_recovery(server); | |
510 | if (ret < 0) | |
511 | break; | |
512 | goto wait_on_recovery; | |
8ef2f8d4 CL |
513 | case -NFS4ERR_LEASE_MOVED: |
514 | nfs4_schedule_lease_moved_recovery(clp); | |
515 | goto wait_on_recovery; | |
03391693 | 516 | #if defined(CONFIG_NFS_V4_1) |
4745e315 AA |
517 | case -NFS4ERR_BADSESSION: |
518 | case -NFS4ERR_BADSLOT: | |
519 | case -NFS4ERR_BAD_HIGH_SLOT: | |
520 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
521 | case -NFS4ERR_DEADSESSION: | |
522 | case -NFS4ERR_SEQ_FALSE_RETRY: | |
523 | case -NFS4ERR_SEQ_MISORDERED: | |
5c441544 | 524 | /* Handled in nfs41_sequence_process() */ |
399f11c3 | 525 | goto wait_on_recovery; |
03391693 | 526 | #endif /* defined(CONFIG_NFS_V4_1) */ |
65de872e | 527 | case -NFS4ERR_FILE_OPEN: |
44ed3556 N |
528 | if (exception->timeout > HZ) { |
529 | /* We have retried a decent amount, time to | |
530 | * fail | |
531 | */ | |
532 | ret = -EBUSY; | |
533 | break; | |
534 | } | |
01e03bdc | 535 | /* Fall through */ |
65de872e | 536 | case -NFS4ERR_DELAY: |
2598ed34 | 537 | nfs_inc_server_stats(server, NFSIOS_DELAY); |
01e03bdc | 538 | /* Fall through */ |
2598ed34 | 539 | case -NFS4ERR_GRACE: |
e85d7ee4 | 540 | case -NFS4ERR_LAYOUTTRYLATER: |
183d9e7b | 541 | case -NFS4ERR_RECALLCONFLICT: |
b3c2aa07 TM |
542 | exception->delay = 1; |
543 | return 0; | |
544 | ||
a8a4ae3a | 545 | case -NFS4ERR_RETRY_UNCACHED_REP: |
65de872e TM |
546 | case -NFS4ERR_OLD_STATEID: |
547 | exception->retry = 1; | |
b064eca2 TM |
548 | break; |
549 | case -NFS4ERR_BADOWNER: | |
550 | /* The following works around a Linux server bug! */ | |
551 | case -NFS4ERR_BADNAME: | |
552 | if (server->caps & NFS_CAP_UIDGID_NOMAP) { | |
553 | server->caps &= ~NFS_CAP_UIDGID_NOMAP; | |
554 | exception->retry = 1; | |
555 | printk(KERN_WARNING "NFS: v4 server %s " | |
556 | "does not accept raw " | |
557 | "uid/gids. " | |
558 | "Reenabling the idmapper.\n", | |
559 | server->nfs_client->cl_hostname); | |
560 | } | |
65de872e TM |
561 | } |
562 | /* We failed to handle the error */ | |
563 | return nfs4_map_errors(ret); | |
0400a6b0 | 564 | wait_on_recovery: |
b3c2aa07 TM |
565 | exception->recovering = 1; |
566 | return 0; | |
567 | } | |
568 | ||
569 | /* This is the error handling routine for processes that are allowed | |
570 | * to sleep. | |
571 | */ | |
572 | int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception) | |
573 | { | |
574 | struct nfs_client *clp = server->nfs_client; | |
575 | int ret; | |
576 | ||
577 | ret = nfs4_do_handle_exception(server, errorcode, exception); | |
578 | if (exception->delay) { | |
0688e64b TM |
579 | ret = nfs4_delay(&exception->timeout, |
580 | exception->interruptible); | |
b3c2aa07 TM |
581 | goto out_retry; |
582 | } | |
583 | if (exception->recovering) { | |
584 | ret = nfs4_wait_clnt_recover(clp); | |
585 | if (test_bit(NFS_MIG_FAILED, &server->mig_status)) | |
586 | return -EIO; | |
587 | goto out_retry; | |
588 | } | |
589 | return ret; | |
590 | out_retry: | |
a2c0b9e2 TM |
591 | if (ret == 0) |
592 | exception->retry = 1; | |
593 | return ret; | |
65de872e TM |
594 | } |
595 | ||
037fc980 TM |
596 | static int |
597 | nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server, | |
598 | int errorcode, struct nfs4_exception *exception) | |
599 | { | |
600 | struct nfs_client *clp = server->nfs_client; | |
601 | int ret; | |
602 | ||
603 | ret = nfs4_do_handle_exception(server, errorcode, exception); | |
604 | if (exception->delay) { | |
605 | rpc_delay(task, nfs4_update_delay(&exception->timeout)); | |
606 | goto out_retry; | |
607 | } | |
608 | if (exception->recovering) { | |
609 | rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL); | |
610 | if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0) | |
611 | rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task); | |
612 | goto out_retry; | |
613 | } | |
519ae255 | 614 | if (test_bit(NFS_MIG_FAILED, &server->mig_status)) |
037fc980 TM |
615 | ret = -EIO; |
616 | return ret; | |
617 | out_retry: | |
0f90be13 | 618 | if (ret == 0) { |
a2c0b9e2 | 619 | exception->retry = 1; |
0f90be13 BB |
620 | /* |
621 | * For NFS4ERR_MOVED, the client transport will need to | |
622 | * be recomputed after migration recovery has completed. | |
623 | */ | |
624 | if (errorcode == -NFS4ERR_MOVED) | |
625 | rpc_task_release_transport(task); | |
626 | } | |
a2c0b9e2 | 627 | return ret; |
65de872e TM |
628 | } |
629 | ||
0f913a57 | 630 | int |
037fc980 TM |
631 | nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server, |
632 | struct nfs4_state *state, long *timeout) | |
633 | { | |
634 | struct nfs4_exception exception = { | |
635 | .state = state, | |
636 | }; | |
637 | ||
638 | if (task->tk_status >= 0) | |
639 | return 0; | |
640 | if (timeout) | |
641 | exception.timeout = *timeout; | |
642 | task->tk_status = nfs4_async_handle_exception(task, server, | |
643 | task->tk_status, | |
644 | &exception); | |
645 | if (exception.delay && timeout) | |
646 | *timeout = exception.timeout; | |
647 | if (exception.retry) | |
648 | return -EAGAIN; | |
649 | return 0; | |
650 | } | |
651 | ||
a5250def WAA |
652 | /* |
653 | * Return 'true' if 'clp' is using an rpc_client that is integrity protected | |
654 | * or 'false' otherwise. | |
655 | */ | |
656 | static bool _nfs4_is_integrity_protected(struct nfs_client *clp) | |
657 | { | |
658 | rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor; | |
eeea5361 | 659 | return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P); |
a5250def | 660 | } |
65de872e | 661 | |
452e9352 | 662 | static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp) |
1da177e4 | 663 | { |
1da177e4 LT |
664 | spin_lock(&clp->cl_lock); |
665 | if (time_before(clp->cl_last_renewal,timestamp)) | |
666 | clp->cl_last_renewal = timestamp; | |
667 | spin_unlock(&clp->cl_lock); | |
668 | } | |
669 | ||
452e9352 TM |
670 | static void renew_lease(const struct nfs_server *server, unsigned long timestamp) |
671 | { | |
be824167 TM |
672 | struct nfs_client *clp = server->nfs_client; |
673 | ||
674 | if (!nfs4_has_session(clp)) | |
675 | do_renew_lease(clp, timestamp); | |
452e9352 TM |
676 | } |
677 | ||
2a3eb2b9 CL |
678 | struct nfs4_call_sync_data { |
679 | const struct nfs_server *seq_server; | |
680 | struct nfs4_sequence_args *seq_args; | |
681 | struct nfs4_sequence_res *seq_res; | |
682 | }; | |
683 | ||
be3a5d23 | 684 | void nfs4_init_sequence(struct nfs4_sequence_args *args, |
fba83f34 AS |
685 | struct nfs4_sequence_res *res, int cache_reply, |
686 | int privileged) | |
a9c92d6b CL |
687 | { |
688 | args->sa_slot = NULL; | |
689 | args->sa_cache_this = cache_reply; | |
fba83f34 | 690 | args->sa_privileged = privileged; |
a9c92d6b CL |
691 | |
692 | res->sr_slot = NULL; | |
693 | } | |
694 | ||
2e80dbe7 | 695 | static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res) |
3bd2384a CL |
696 | { |
697 | struct nfs4_slot *slot = res->sr_slot; | |
698 | struct nfs4_slot_table *tbl; | |
699 | ||
3bd2384a CL |
700 | tbl = slot->table; |
701 | spin_lock(&tbl->slot_tbl_lock); | |
702 | if (!nfs41_wake_and_assign_slot(tbl, slot)) | |
703 | nfs4_free_slot(tbl, slot); | |
704 | spin_unlock(&tbl->slot_tbl_lock); | |
705 | ||
706 | res->sr_slot = NULL; | |
2e80dbe7 TM |
707 | } |
708 | ||
709 | static int nfs40_sequence_done(struct rpc_task *task, | |
710 | struct nfs4_sequence_res *res) | |
711 | { | |
712 | if (res->sr_slot != NULL) | |
713 | nfs40_sequence_free_slot(res); | |
3bd2384a CL |
714 | return 1; |
715 | } | |
716 | ||
cccef3b9 AA |
717 | #if defined(CONFIG_NFS_V4_1) |
718 | ||
3be0f80b | 719 | static void nfs41_release_slot(struct nfs4_slot *slot) |
13615871 | 720 | { |
e3725ec0 | 721 | struct nfs4_session *session; |
13615871 | 722 | struct nfs4_slot_table *tbl; |
c10e4498 | 723 | bool send_new_highest_used_slotid = false; |
13615871 | 724 | |
3be0f80b TM |
725 | if (!slot) |
726 | return; | |
a13ce7c6 | 727 | tbl = slot->table; |
e3725ec0 | 728 | session = tbl->session; |
ea028ac9 | 729 | |
07e8dcbd TM |
730 | /* Bump the slot sequence number */ |
731 | if (slot->seq_done) | |
732 | slot->seq_nr++; | |
733 | slot->seq_done = 0; | |
734 | ||
35dc1d74 | 735 | spin_lock(&tbl->slot_tbl_lock); |
c10e4498 TM |
736 | /* Be nice to the server: try to ensure that the last transmitted |
737 | * value for highest_user_slotid <= target_highest_slotid | |
738 | */ | |
739 | if (tbl->highest_used_slotid > tbl->target_highest_slotid) | |
740 | send_new_highest_used_slotid = true; | |
741 | ||
a13ce7c6 | 742 | if (nfs41_wake_and_assign_slot(tbl, slot)) { |
b75ad4cd TM |
743 | send_new_highest_used_slotid = false; |
744 | goto out_unlock; | |
745 | } | |
a13ce7c6 | 746 | nfs4_free_slot(tbl, slot); |
c10e4498 TM |
747 | |
748 | if (tbl->highest_used_slotid != NFS4_NO_SLOT) | |
749 | send_new_highest_used_slotid = false; | |
b75ad4cd | 750 | out_unlock: |
35dc1d74 | 751 | spin_unlock(&tbl->slot_tbl_lock); |
c10e4498 | 752 | if (send_new_highest_used_slotid) |
3f10a6af | 753 | nfs41_notify_server(session->clp); |
045d2a6d TM |
754 | if (waitqueue_active(&tbl->slot_waitq)) |
755 | wake_up_all(&tbl->slot_waitq); | |
13615871 AA |
756 | } |
757 | ||
3be0f80b TM |
758 | static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res) |
759 | { | |
760 | nfs41_release_slot(res->sr_slot); | |
761 | res->sr_slot = NULL; | |
762 | } | |
763 | ||
3453d570 TM |
764 | static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot, |
765 | u32 seqnr) | |
766 | { | |
767 | if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0) | |
768 | slot->seq_nr_highest_sent = seqnr; | |
769 | } | |
770 | static void nfs4_slot_sequence_acked(struct nfs4_slot *slot, | |
771 | u32 seqnr) | |
772 | { | |
773 | slot->seq_nr_highest_sent = seqnr; | |
774 | slot->seq_nr_last_acked = seqnr; | |
775 | } | |
776 | ||
2e80dbe7 TM |
777 | static int nfs41_sequence_process(struct rpc_task *task, |
778 | struct nfs4_sequence_res *res) | |
b0df806c | 779 | { |
e3725ec0 | 780 | struct nfs4_session *session; |
a13ce7c6 | 781 | struct nfs4_slot *slot = res->sr_slot; |
14516c3a | 782 | struct nfs_client *clp; |
5c441544 | 783 | int status; |
85563073 | 784 | int ret = 1; |
b0df806c | 785 | |
a13ce7c6 TM |
786 | if (slot == NULL) |
787 | goto out_noaction; | |
468f8613 | 788 | /* don't increment the sequence number if the task wasn't sent */ |
c71c46f0 | 789 | if (!RPC_WAS_SENT(task) || slot->seq_done) |
b0df806c AA |
790 | goto out; |
791 | ||
e3725ec0 | 792 | session = slot->table->session; |
933602e3 | 793 | |
2f92ae34 | 794 | trace_nfs4_sequence_done(session, res); |
5c441544 TM |
795 | |
796 | status = res->sr_status; | |
797 | if (task->tk_status == -NFS4ERR_DEADSESSION) | |
798 | status = -NFS4ERR_DEADSESSION; | |
799 | ||
691daf3b | 800 | /* Check the SEQUENCE operation status */ |
5c441544 | 801 | switch (status) { |
14516c3a | 802 | case 0: |
3453d570 TM |
803 | /* Mark this sequence number as having been acked */ |
804 | nfs4_slot_sequence_acked(slot, slot->seq_nr); | |
b0df806c | 805 | /* Update the slot's sequence and clientid lease timer */ |
07e8dcbd | 806 | slot->seq_done = 1; |
e3725ec0 | 807 | clp = session->clp; |
8e63b6a8 | 808 | do_renew_lease(clp, res->sr_timestamp); |
0629e370 | 809 | /* Check sequence flags */ |
0a014a44 TM |
810 | nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags, |
811 | !!slot->privileged); | |
464ee9f9 | 812 | nfs41_update_target_slotid(slot->table, slot, res); |
14516c3a | 813 | break; |
ac20d163 TM |
814 | case 1: |
815 | /* | |
816 | * sr_status remains 1 if an RPC level error occurred. | |
817 | * The server may or may not have processed the sequence | |
818 | * operation.. | |
ac20d163 | 819 | */ |
3453d570 TM |
820 | nfs4_slot_sequence_record_sent(slot, slot->seq_nr); |
821 | slot->seq_done = 1; | |
ac20d163 | 822 | goto out; |
14516c3a TM |
823 | case -NFS4ERR_DELAY: |
824 | /* The server detected a resend of the RPC call and | |
825 | * returned NFS4ERR_DELAY as per Section 2.10.6.2 | |
826 | * of RFC5661. | |
827 | */ | |
df2fabff | 828 | dprintk("%s: slot=%u seq=%u: Operation in progress\n", |
dfb4f309 | 829 | __func__, |
df2fabff | 830 | slot->slot_nr, |
933602e3 | 831 | slot->seq_nr); |
3453d570 | 832 | nfs4_slot_sequence_acked(slot, slot->seq_nr); |
14516c3a | 833 | goto out_retry; |
f9312a54 TM |
834 | case -NFS4ERR_RETRY_UNCACHED_REP: |
835 | case -NFS4ERR_SEQ_FALSE_RETRY: | |
836 | /* | |
837 | * The server thinks we tried to replay a request. | |
838 | * Retry the call after bumping the sequence ID. | |
839 | */ | |
3453d570 | 840 | nfs4_slot_sequence_acked(slot, slot->seq_nr); |
f9312a54 | 841 | goto retry_new_seq; |
85563073 TM |
842 | case -NFS4ERR_BADSLOT: |
843 | /* | |
844 | * The slot id we used was probably retired. Try again | |
845 | * using a different slot id. | |
846 | */ | |
99589100 | 847 | if (slot->slot_nr < slot->table->target_highest_slotid) |
3be0f80b | 848 | goto session_recover; |
e8794440 TM |
849 | goto retry_nowait; |
850 | case -NFS4ERR_SEQ_MISORDERED: | |
3453d570 | 851 | nfs4_slot_sequence_record_sent(slot, slot->seq_nr); |
ac20d163 | 852 | /* |
3453d570 TM |
853 | * Were one or more calls using this slot interrupted? |
854 | * If the server never received the request, then our | |
855 | * transmitted slot sequence number may be too high. | |
e8794440 | 856 | */ |
3453d570 TM |
857 | if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) { |
858 | slot->seq_nr--; | |
8e63b6a8 TM |
859 | goto retry_nowait; |
860 | } | |
3453d570 TM |
861 | /* |
862 | * RFC5661: | |
863 | * A retry might be sent while the original request is | |
864 | * still in progress on the replier. The replier SHOULD | |
865 | * deal with the issue by returning NFS4ERR_DELAY as the | |
866 | * reply to SEQUENCE or CB_SEQUENCE operation, but | |
867 | * implementations MAY return NFS4ERR_SEQ_MISORDERED. | |
868 | * | |
869 | * Restart the search after a delay. | |
870 | */ | |
871 | slot->seq_nr = slot->seq_nr_highest_sent; | |
872 | goto out_retry; | |
5c441544 TM |
873 | case -NFS4ERR_BADSESSION: |
874 | case -NFS4ERR_DEADSESSION: | |
875 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
876 | goto session_recover; | |
14516c3a TM |
877 | default: |
878 | /* Just update the slot sequence no. */ | |
07e8dcbd | 879 | slot->seq_done = 1; |
b0df806c AA |
880 | } |
881 | out: | |
882 | /* The session may be reset by one of the error handlers. */ | |
883 | dprintk("%s: Error %d free the slot \n", __func__, res->sr_status); | |
a13ce7c6 | 884 | out_noaction: |
85563073 | 885 | return ret; |
3be0f80b | 886 | session_recover: |
5c441544 TM |
887 | nfs4_schedule_session_recovery(session, status); |
888 | dprintk("%s ERROR: %d Reset session\n", __func__, status); | |
889 | nfs41_sequence_free_slot(res); | |
890 | goto out; | |
3be0f80b TM |
891 | retry_new_seq: |
892 | ++slot->seq_nr; | |
e8794440 TM |
893 | retry_nowait: |
894 | if (rpc_restart_call_prepare(task)) { | |
2e80dbe7 | 895 | nfs41_sequence_free_slot(res); |
e8794440 TM |
896 | task->tk_status = 0; |
897 | ret = 0; | |
898 | } | |
899 | goto out; | |
14516c3a | 900 | out_retry: |
d05dd4e9 | 901 | if (!rpc_restart_call(task)) |
14516c3a TM |
902 | goto out; |
903 | rpc_delay(task, NFS4_POLL_RETRY_MAX); | |
904 | return 0; | |
b0df806c | 905 | } |
2e80dbe7 TM |
906 | |
907 | int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res) | |
908 | { | |
909 | if (!nfs41_sequence_process(task, res)) | |
910 | return 0; | |
911 | if (res->sr_slot != NULL) | |
912 | nfs41_sequence_free_slot(res); | |
913 | return 1; | |
914 | ||
915 | } | |
f9c96fcc | 916 | EXPORT_SYMBOL_GPL(nfs41_sequence_done); |
b0df806c | 917 | |
2e80dbe7 TM |
918 | static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res) |
919 | { | |
920 | if (res->sr_slot == NULL) | |
921 | return 1; | |
922 | if (res->sr_slot->table->session != NULL) | |
923 | return nfs41_sequence_process(task, res); | |
924 | return nfs40_sequence_done(task, res); | |
925 | } | |
926 | ||
927 | static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res) | |
928 | { | |
929 | if (res->sr_slot != NULL) { | |
930 | if (res->sr_slot->table->session != NULL) | |
931 | nfs41_sequence_free_slot(res); | |
932 | else | |
933 | nfs40_sequence_free_slot(res); | |
934 | } | |
935 | } | |
936 | ||
2c4b131d | 937 | int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res) |
df896455 | 938 | { |
e3725ec0 | 939 | if (res->sr_slot == NULL) |
14516c3a | 940 | return 1; |
3bd2384a CL |
941 | if (!res->sr_slot->table->session) |
942 | return nfs40_sequence_done(task, res); | |
14516c3a | 943 | return nfs41_sequence_done(task, res); |
df896455 | 944 | } |
2c4b131d | 945 | EXPORT_SYMBOL_GPL(nfs4_sequence_done); |
df896455 | 946 | |
ce5039c1 AA |
947 | static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata) |
948 | { | |
2a3eb2b9 | 949 | struct nfs4_call_sync_data *data = calldata; |
ce5039c1 | 950 | |
035168ab TM |
951 | dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server); |
952 | ||
7981c8a6 AS |
953 | nfs4_setup_sequence(data->seq_server->nfs_client, |
954 | data->seq_args, data->seq_res, task); | |
ce5039c1 AA |
955 | } |
956 | ||
69ab40c4 AA |
957 | static void nfs41_call_sync_done(struct rpc_task *task, void *calldata) |
958 | { | |
2a3eb2b9 | 959 | struct nfs4_call_sync_data *data = calldata; |
69ab40c4 | 960 | |
14516c3a | 961 | nfs41_sequence_done(task, data->seq_res); |
69ab40c4 AA |
962 | } |
963 | ||
17280175 | 964 | static const struct rpc_call_ops nfs41_call_sync_ops = { |
ce5039c1 | 965 | .rpc_call_prepare = nfs41_call_sync_prepare, |
69ab40c4 | 966 | .rpc_call_done = nfs41_call_sync_done, |
ce5039c1 AA |
967 | }; |
968 | ||
3bd2384a CL |
969 | #else /* !CONFIG_NFS_V4_1 */ |
970 | ||
2e80dbe7 TM |
971 | static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res) |
972 | { | |
973 | return nfs40_sequence_done(task, res); | |
974 | } | |
975 | ||
976 | static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res) | |
977 | { | |
978 | if (res->sr_slot != NULL) | |
979 | nfs40_sequence_free_slot(res); | |
980 | } | |
981 | ||
2c4b131d PT |
982 | int nfs4_sequence_done(struct rpc_task *task, |
983 | struct nfs4_sequence_res *res) | |
9915ea7e | 984 | { |
3bd2384a | 985 | return nfs40_sequence_done(task, res); |
9915ea7e | 986 | } |
2c4b131d | 987 | EXPORT_SYMBOL_GPL(nfs4_sequence_done); |
3bd2384a CL |
988 | |
989 | #endif /* !CONFIG_NFS_V4_1 */ | |
9915ea7e | 990 | |
c1dffe0b TM |
991 | static void nfs41_sequence_res_init(struct nfs4_sequence_res *res) |
992 | { | |
993 | res->sr_timestamp = jiffies; | |
994 | res->sr_status_flags = 0; | |
995 | res->sr_status = 1; | |
996 | } | |
997 | ||
3be0f80b TM |
998 | static |
999 | void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args, | |
1000 | struct nfs4_sequence_res *res, | |
1001 | struct nfs4_slot *slot) | |
1002 | { | |
1003 | if (!slot) | |
1004 | return; | |
1005 | slot->privileged = args->sa_privileged ? 1 : 0; | |
1006 | args->sa_slot = slot; | |
1007 | ||
1008 | res->sr_slot = slot; | |
3be0f80b TM |
1009 | } |
1010 | ||
1011 | int nfs4_setup_sequence(struct nfs_client *client, | |
7981c8a6 AS |
1012 | struct nfs4_sequence_args *args, |
1013 | struct nfs4_sequence_res *res, | |
1014 | struct rpc_task *task) | |
1015 | { | |
7981c8a6 | 1016 | struct nfs4_session *session = nfs4_get_session(client); |
76ee0354 | 1017 | struct nfs4_slot_table *tbl = client->cl_slot_tbl; |
3d35808b | 1018 | struct nfs4_slot *slot; |
7981c8a6 | 1019 | |
9dd9107f AS |
1020 | /* slot already allocated? */ |
1021 | if (res->sr_slot != NULL) | |
1022 | goto out_start; | |
1023 | ||
6b2e6856 | 1024 | if (session) |
76ee0354 | 1025 | tbl = &session->fc_slot_table; |
76ee0354 | 1026 | |
3453d570 TM |
1027 | spin_lock(&tbl->slot_tbl_lock); |
1028 | /* The state manager will wait until the slot table is empty */ | |
1029 | if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged) | |
1030 | goto out_sleep; | |
1031 | ||
1032 | slot = nfs4_alloc_slot(tbl); | |
1033 | if (IS_ERR(slot)) { | |
3453d570 | 1034 | if (slot == ERR_PTR(-ENOMEM)) |
6b2e6856 | 1035 | goto out_sleep_timeout; |
3453d570 | 1036 | goto out_sleep; |
3d35808b | 1037 | } |
3453d570 | 1038 | spin_unlock(&tbl->slot_tbl_lock); |
3d35808b | 1039 | |
3be0f80b | 1040 | nfs4_sequence_attach_slot(args, res, slot); |
3d35808b | 1041 | |
ad05cc0f | 1042 | trace_nfs4_setup_sequence(session, args); |
9dd9107f | 1043 | out_start: |
c1dffe0b | 1044 | nfs41_sequence_res_init(res); |
9dd9107f AS |
1045 | rpc_call_start(task); |
1046 | return 0; | |
6b2e6856 TM |
1047 | out_sleep_timeout: |
1048 | /* Try again in 1/4 second */ | |
1049 | if (args->sa_privileged) | |
1050 | rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task, | |
1051 | jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED); | |
1052 | else | |
1053 | rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task, | |
1054 | NULL, jiffies + (HZ >> 2)); | |
1055 | spin_unlock(&tbl->slot_tbl_lock); | |
1056 | return -EAGAIN; | |
0dcee8bb AS |
1057 | out_sleep: |
1058 | if (args->sa_privileged) | |
1059 | rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task, | |
8357a9b6 | 1060 | RPC_PRIORITY_PRIVILEGED); |
0dcee8bb AS |
1061 | else |
1062 | rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL); | |
1063 | spin_unlock(&tbl->slot_tbl_lock); | |
1064 | return -EAGAIN; | |
7981c8a6 AS |
1065 | } |
1066 | EXPORT_SYMBOL_GPL(nfs4_setup_sequence); | |
1067 | ||
9915ea7e CL |
1068 | static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata) |
1069 | { | |
1070 | struct nfs4_call_sync_data *data = calldata; | |
42e1cca7 | 1071 | nfs4_setup_sequence(data->seq_server->nfs_client, |
9915ea7e CL |
1072 | data->seq_args, data->seq_res, task); |
1073 | } | |
1074 | ||
1075 | static void nfs40_call_sync_done(struct rpc_task *task, void *calldata) | |
1076 | { | |
1077 | struct nfs4_call_sync_data *data = calldata; | |
1078 | nfs4_sequence_done(task, data->seq_res); | |
1079 | } | |
1080 | ||
1081 | static const struct rpc_call_ops nfs40_call_sync_ops = { | |
1082 | .rpc_call_prepare = nfs40_call_sync_prepare, | |
1083 | .rpc_call_done = nfs40_call_sync_done, | |
1084 | }; | |
1085 | ||
48c05854 AS |
1086 | static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup) |
1087 | { | |
1088 | int ret; | |
1089 | struct rpc_task *task; | |
1090 | ||
1091 | task = rpc_run_task(task_setup); | |
1092 | if (IS_ERR(task)) | |
1093 | return PTR_ERR(task); | |
1094 | ||
1095 | ret = task->tk_status; | |
1096 | rpc_put_task(task); | |
1097 | return ret; | |
1098 | } | |
1099 | ||
c74dfe97 TM |
1100 | static int nfs4_do_call_sync(struct rpc_clnt *clnt, |
1101 | struct nfs_server *server, | |
1102 | struct rpc_message *msg, | |
1103 | struct nfs4_sequence_args *args, | |
1104 | struct nfs4_sequence_res *res, | |
1105 | unsigned short task_flags) | |
ce5039c1 | 1106 | { |
9915ea7e | 1107 | struct nfs_client *clp = server->nfs_client; |
2a3eb2b9 | 1108 | struct nfs4_call_sync_data data = { |
035168ab | 1109 | .seq_server = server, |
ce5039c1 AA |
1110 | .seq_args = args, |
1111 | .seq_res = res, | |
ce5039c1 AA |
1112 | }; |
1113 | struct rpc_task_setup task_setup = { | |
7c513058 | 1114 | .rpc_client = clnt, |
ce5039c1 | 1115 | .rpc_message = msg, |
9915ea7e | 1116 | .callback_ops = clp->cl_mvops->call_sync_ops, |
c74dfe97 TM |
1117 | .callback_data = &data, |
1118 | .flags = task_flags, | |
ce5039c1 AA |
1119 | }; |
1120 | ||
48c05854 | 1121 | return nfs4_call_sync_custom(&task_setup); |
ce5039c1 AA |
1122 | } |
1123 | ||
c74dfe97 TM |
1124 | static int nfs4_call_sync_sequence(struct rpc_clnt *clnt, |
1125 | struct nfs_server *server, | |
1126 | struct rpc_message *msg, | |
1127 | struct nfs4_sequence_args *args, | |
1128 | struct nfs4_sequence_res *res) | |
1129 | { | |
1130 | return nfs4_do_call_sync(clnt, server, msg, args, res, 0); | |
1131 | } | |
1132 | ||
1133 | ||
7c513058 BS |
1134 | int nfs4_call_sync(struct rpc_clnt *clnt, |
1135 | struct nfs_server *server, | |
e73b83f2 BS |
1136 | struct rpc_message *msg, |
1137 | struct nfs4_sequence_args *args, | |
1138 | struct nfs4_sequence_res *res, | |
1139 | int cache_reply) | |
1140 | { | |
fba83f34 | 1141 | nfs4_init_sequence(args, res, cache_reply, 0); |
9915ea7e | 1142 | return nfs4_call_sync_sequence(clnt, server, msg, args, res); |
e73b83f2 | 1143 | } |
cccef3b9 | 1144 | |
3c591175 TM |
1145 | static void |
1146 | nfs4_inc_nlink_locked(struct inode *inode) | |
1147 | { | |
1148 | NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER; | |
1149 | inc_nlink(inode); | |
1150 | } | |
1151 | ||
1152 | static void | |
1153 | nfs4_dec_nlink_locked(struct inode *inode) | |
1154 | { | |
1155 | NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER; | |
1156 | drop_nlink(inode); | |
1157 | } | |
1158 | ||
1159 | static void | |
1160 | update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo, | |
5636ec4e | 1161 | unsigned long timestamp, unsigned long cache_validity) |
1da177e4 | 1162 | { |
38478b24 | 1163 | struct nfs_inode *nfsi = NFS_I(dir); |
1da177e4 | 1164 | |
16e14375 TM |
1165 | nfsi->cache_validity |= NFS_INO_INVALID_CTIME |
1166 | | NFS_INO_INVALID_MTIME | |
5636ec4e TM |
1167 | | NFS_INO_INVALID_DATA |
1168 | | cache_validity; | |
1eb5d98f | 1169 | if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) { |
e603a4c1 TM |
1170 | nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE; |
1171 | nfsi->attrtimeo_timestamp = jiffies; | |
1172 | } else { | |
bfc69a45 | 1173 | nfs_force_lookup_revalidate(dir); |
1eb5d98f | 1174 | if (cinfo->before != inode_peek_iversion_raw(dir)) |
e603a4c1 TM |
1175 | nfsi->cache_validity |= NFS_INO_INVALID_ACCESS | |
1176 | NFS_INO_INVALID_ACL; | |
1177 | } | |
1eb5d98f | 1178 | inode_set_iversion_raw(dir, cinfo->after); |
d3129ef6 | 1179 | nfsi->read_cache_jiffies = timestamp; |
3235b403 | 1180 | nfsi->attr_gencount = nfs_inc_attr_generation_counter(); |
c8d07159 | 1181 | nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE; |
de242c0b | 1182 | nfs_fscache_invalidate(dir); |
3c591175 TM |
1183 | } |
1184 | ||
1185 | static void | |
1186 | update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo, | |
5636ec4e | 1187 | unsigned long timestamp, unsigned long cache_validity) |
3c591175 TM |
1188 | { |
1189 | spin_lock(&dir->i_lock); | |
5636ec4e | 1190 | update_changeattr_locked(dir, cinfo, timestamp, cache_validity); |
38478b24 | 1191 | spin_unlock(&dir->i_lock); |
1da177e4 LT |
1192 | } |
1193 | ||
8fd1ab74 TM |
1194 | struct nfs4_open_createattrs { |
1195 | struct nfs4_label *label; | |
1196 | struct iattr *sattr; | |
1197 | const __u32 verf[2]; | |
1198 | }; | |
1199 | ||
49f9a0fa TM |
1200 | static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server, |
1201 | int err, struct nfs4_exception *exception) | |
1202 | { | |
1203 | if (err != -EINVAL) | |
1204 | return false; | |
1205 | if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1)) | |
1206 | return false; | |
1207 | server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1; | |
1208 | exception->retry = 1; | |
1209 | return true; | |
1210 | } | |
1211 | ||
1bf85d8c TM |
1212 | static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx) |
1213 | { | |
1214 | return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC); | |
1215 | } | |
1216 | ||
1217 | static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx) | |
1218 | { | |
1219 | fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE); | |
1220 | ||
1221 | return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret; | |
1222 | } | |
1223 | ||
6ae37339 TM |
1224 | static u32 |
1225 | nfs4_map_atomic_open_share(struct nfs_server *server, | |
1226 | fmode_t fmode, int openflags) | |
1227 | { | |
1228 | u32 res = 0; | |
1229 | ||
1230 | switch (fmode & (FMODE_READ | FMODE_WRITE)) { | |
1231 | case FMODE_READ: | |
1232 | res = NFS4_SHARE_ACCESS_READ; | |
1233 | break; | |
1234 | case FMODE_WRITE: | |
1235 | res = NFS4_SHARE_ACCESS_WRITE; | |
1236 | break; | |
1237 | case FMODE_READ|FMODE_WRITE: | |
1238 | res = NFS4_SHARE_ACCESS_BOTH; | |
1239 | } | |
1240 | if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1)) | |
1241 | goto out; | |
1242 | /* Want no delegation if we're using O_DIRECT */ | |
1243 | if (openflags & O_DIRECT) | |
1244 | res |= NFS4_SHARE_WANT_NO_DELEG; | |
1245 | out: | |
1246 | return res; | |
1247 | } | |
1248 | ||
49f9a0fa TM |
1249 | static enum open_claim_type4 |
1250 | nfs4_map_atomic_open_claim(struct nfs_server *server, | |
1251 | enum open_claim_type4 claim) | |
1252 | { | |
1253 | if (server->caps & NFS_CAP_ATOMIC_OPEN_V1) | |
1254 | return claim; | |
1255 | switch (claim) { | |
1256 | default: | |
1257 | return claim; | |
1258 | case NFS4_OPEN_CLAIM_FH: | |
1259 | return NFS4_OPEN_CLAIM_NULL; | |
1260 | case NFS4_OPEN_CLAIM_DELEG_CUR_FH: | |
1261 | return NFS4_OPEN_CLAIM_DELEGATE_CUR; | |
1262 | case NFS4_OPEN_CLAIM_DELEG_PREV_FH: | |
1263 | return NFS4_OPEN_CLAIM_DELEGATE_PREV; | |
1264 | } | |
1265 | } | |
2ced46c2 TM |
1266 | |
1267 | static void nfs4_init_opendata_res(struct nfs4_opendata *p) | |
1268 | { | |
1269 | p->o_res.f_attr = &p->f_attr; | |
1775fd3e | 1270 | p->o_res.f_label = p->f_label; |
c1d51931 TM |
1271 | p->o_res.seqid = p->o_arg.seqid; |
1272 | p->c_res.seqid = p->c_arg.seqid; | |
2ced46c2 | 1273 | p->o_res.server = p->o_arg.server; |
5f657530 | 1274 | p->o_res.access_request = p->o_arg.access; |
2ced46c2 | 1275 | nfs_fattr_init(&p->f_attr); |
6926afd1 | 1276 | nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name); |
2ced46c2 TM |
1277 | } |
1278 | ||
82a2c1b7 | 1279 | static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry, |
dc0b027d | 1280 | struct nfs4_state_owner *sp, fmode_t fmode, int flags, |
8fd1ab74 | 1281 | const struct nfs4_open_createattrs *c, |
4a1c0893 | 1282 | enum open_claim_type4 claim, |
8535b2be | 1283 | gfp_t gfp_mask) |
e56e0b78 | 1284 | { |
82a2c1b7 | 1285 | struct dentry *parent = dget_parent(dentry); |
2b0143b5 | 1286 | struct inode *dir = d_inode(parent); |
e56e0b78 | 1287 | struct nfs_server *server = NFS_SERVER(dir); |
63f5f796 | 1288 | struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); |
8fd1ab74 | 1289 | struct nfs4_label *label = (c != NULL) ? c->label : NULL; |
e56e0b78 TM |
1290 | struct nfs4_opendata *p; |
1291 | ||
8535b2be | 1292 | p = kzalloc(sizeof(*p), gfp_mask); |
e56e0b78 TM |
1293 | if (p == NULL) |
1294 | goto err; | |
14c43f76 DQ |
1295 | |
1296 | p->f_label = nfs4_label_alloc(server, gfp_mask); | |
1297 | if (IS_ERR(p->f_label)) | |
1298 | goto err_free_p; | |
1299 | ||
a49c2691 KM |
1300 | p->a_label = nfs4_label_alloc(server, gfp_mask); |
1301 | if (IS_ERR(p->a_label)) | |
1302 | goto err_free_f; | |
1303 | ||
63f5f796 TM |
1304 | alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; |
1305 | p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask); | |
badc76dd | 1306 | if (IS_ERR(p->o_arg.seqid)) |
14c43f76 | 1307 | goto err_free_label; |
82a2c1b7 AV |
1308 | nfs_sb_active(dentry->d_sb); |
1309 | p->dentry = dget(dentry); | |
e56e0b78 TM |
1310 | p->dir = parent; |
1311 | p->owner = sp; | |
1312 | atomic_inc(&sp->so_count); | |
dc0b027d TM |
1313 | p->o_arg.open_flags = flags; |
1314 | p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE); | |
536585cc | 1315 | p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim); |
6ae37339 TM |
1316 | p->o_arg.share_access = nfs4_map_atomic_open_share(server, |
1317 | fmode, flags); | |
90910519 BC |
1318 | if (flags & O_CREAT) { |
1319 | p->o_arg.umask = current_umask(); | |
1320 | p->o_arg.label = nfs4_label_copy(p->a_label, label); | |
1321 | if (c->sattr != NULL && c->sattr->ia_valid != 0) { | |
1322 | p->o_arg.u.attrs = &p->attrs; | |
1323 | memcpy(&p->attrs, c->sattr, sizeof(p->attrs)); | |
1324 | ||
1325 | memcpy(p->o_arg.u.verifier.data, c->verf, | |
1326 | sizeof(p->o_arg.u.verifier.data)); | |
1327 | } | |
1328 | } | |
ae2bb032 WAA |
1329 | /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS |
1330 | * will return permission denied for all bits until close */ | |
1331 | if (!(flags & O_EXCL)) { | |
1332 | /* ask server to check for all possible rights as results | |
1333 | * are cached */ | |
536585cc TM |
1334 | switch (p->o_arg.claim) { |
1335 | default: | |
1336 | break; | |
1337 | case NFS4_OPEN_CLAIM_NULL: | |
1338 | case NFS4_OPEN_CLAIM_FH: | |
1339 | p->o_arg.access = NFS4_ACCESS_READ | | |
1340 | NFS4_ACCESS_MODIFY | | |
1341 | NFS4_ACCESS_EXTEND | | |
1342 | NFS4_ACCESS_EXECUTE; | |
1343 | } | |
ae2bb032 | 1344 | } |
7539bbab | 1345 | p->o_arg.clientid = server->nfs_client->cl_clientid; |
95b72eb0 TM |
1346 | p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time); |
1347 | p->o_arg.id.uniquifier = sp->so_seqid.owner_id; | |
82a2c1b7 | 1348 | p->o_arg.name = &dentry->d_name; |
e56e0b78 | 1349 | p->o_arg.server = server; |
aa9c2669 | 1350 | p->o_arg.bitmask = nfs4_bitmask(server, label); |
1549210f | 1351 | p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0]; |
49f9a0fa | 1352 | switch (p->o_arg.claim) { |
4a1c0893 TM |
1353 | case NFS4_OPEN_CLAIM_NULL: |
1354 | case NFS4_OPEN_CLAIM_DELEGATE_CUR: | |
1355 | case NFS4_OPEN_CLAIM_DELEGATE_PREV: | |
1356 | p->o_arg.fh = NFS_FH(dir); | |
1357 | break; | |
1358 | case NFS4_OPEN_CLAIM_PREVIOUS: | |
1359 | case NFS4_OPEN_CLAIM_FH: | |
1360 | case NFS4_OPEN_CLAIM_DELEG_CUR_FH: | |
1361 | case NFS4_OPEN_CLAIM_DELEG_PREV_FH: | |
2b0143b5 | 1362 | p->o_arg.fh = NFS_FH(d_inode(dentry)); |
4a1c0893 | 1363 | } |
cdd4e68b TM |
1364 | p->c_arg.fh = &p->o_res.fh; |
1365 | p->c_arg.stateid = &p->o_res.stateid; | |
1366 | p->c_arg.seqid = p->o_arg.seqid; | |
2ced46c2 | 1367 | nfs4_init_opendata_res(p); |
c6d00e63 | 1368 | kref_init(&p->kref); |
e56e0b78 | 1369 | return p; |
14c43f76 DQ |
1370 | |
1371 | err_free_label: | |
a49c2691 KM |
1372 | nfs4_label_free(p->a_label); |
1373 | err_free_f: | |
14c43f76 DQ |
1374 | nfs4_label_free(p->f_label); |
1375 | err_free_p: | |
e56e0b78 TM |
1376 | kfree(p); |
1377 | err: | |
1378 | dput(parent); | |
1379 | return NULL; | |
1380 | } | |
1381 | ||
c6d00e63 | 1382 | static void nfs4_opendata_free(struct kref *kref) |
e56e0b78 | 1383 | { |
c6d00e63 TM |
1384 | struct nfs4_opendata *p = container_of(kref, |
1385 | struct nfs4_opendata, kref); | |
82a2c1b7 | 1386 | struct super_block *sb = p->dentry->d_sb; |
c6d00e63 | 1387 | |
30ae2412 | 1388 | nfs4_lgopen_release(p->lgp); |
c6d00e63 | 1389 | nfs_free_seqid(p->o_arg.seqid); |
2e80dbe7 | 1390 | nfs4_sequence_free_slot(&p->o_res.seq_res); |
aac00a8d TM |
1391 | if (p->state != NULL) |
1392 | nfs4_put_open_state(p->state); | |
c6d00e63 | 1393 | nfs4_put_state_owner(p->owner); |
14c43f76 | 1394 | |
a49c2691 | 1395 | nfs4_label_free(p->a_label); |
14c43f76 DQ |
1396 | nfs4_label_free(p->f_label); |
1397 | ||
c6d00e63 | 1398 | dput(p->dir); |
82a2c1b7 AV |
1399 | dput(p->dentry); |
1400 | nfs_sb_deactive(sb); | |
6926afd1 | 1401 | nfs_fattr_free_names(&p->f_attr); |
e911b815 | 1402 | kfree(p->f_attr.mdsthreshold); |
c6d00e63 TM |
1403 | kfree(p); |
1404 | } | |
1405 | ||
1406 | static void nfs4_opendata_put(struct nfs4_opendata *p) | |
1407 | { | |
1408 | if (p != NULL) | |
1409 | kref_put(&p->kref, nfs4_opendata_free); | |
e56e0b78 TM |
1410 | } |
1411 | ||
24311f88 TM |
1412 | static bool nfs4_mode_match_open_stateid(struct nfs4_state *state, |
1413 | fmode_t fmode) | |
1414 | { | |
1415 | switch(fmode & (FMODE_READ|FMODE_WRITE)) { | |
1416 | case FMODE_READ|FMODE_WRITE: | |
1417 | return state->n_rdwr != 0; | |
1418 | case FMODE_WRITE: | |
1419 | return state->n_wronly != 0; | |
1420 | case FMODE_READ: | |
1421 | return state->n_rdonly != 0; | |
1422 | } | |
1423 | WARN_ON_ONCE(1); | |
1424 | return false; | |
1425 | } | |
1426 | ||
be189f7e TM |
1427 | static int can_open_cached(struct nfs4_state *state, fmode_t mode, |
1428 | int open_mode, enum open_claim_type4 claim) | |
6ee41268 TM |
1429 | { |
1430 | int ret = 0; | |
dc0b027d | 1431 | |
536e43d1 | 1432 | if (open_mode & (O_EXCL|O_TRUNC)) |
dc0b027d | 1433 | goto out; |
be189f7e TM |
1434 | switch (claim) { |
1435 | case NFS4_OPEN_CLAIM_NULL: | |
1436 | case NFS4_OPEN_CLAIM_FH: | |
1437 | goto out; | |
1438 | default: | |
1439 | break; | |
1440 | } | |
dc0b027d | 1441 | switch (mode & (FMODE_READ|FMODE_WRITE)) { |
6ee41268 | 1442 | case FMODE_READ: |
88069f77 TM |
1443 | ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 |
1444 | && state->n_rdonly != 0; | |
6ee41268 TM |
1445 | break; |
1446 | case FMODE_WRITE: | |
88069f77 TM |
1447 | ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 |
1448 | && state->n_wronly != 0; | |
6ee41268 TM |
1449 | break; |
1450 | case FMODE_READ|FMODE_WRITE: | |
88069f77 TM |
1451 | ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 |
1452 | && state->n_rdwr != 0; | |
6ee41268 | 1453 | } |
dc0b027d | 1454 | out: |
6ee41268 TM |
1455 | return ret; |
1456 | } | |
1457 | ||
2a606188 TM |
1458 | static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode, |
1459 | enum open_claim_type4 claim) | |
aac00a8d | 1460 | { |
652f89f6 TM |
1461 | if (delegation == NULL) |
1462 | return 0; | |
dc0b027d | 1463 | if ((delegation->type & fmode) != fmode) |
aac00a8d | 1464 | return 0; |
2a606188 TM |
1465 | switch (claim) { |
1466 | case NFS4_OPEN_CLAIM_NULL: | |
1467 | case NFS4_OPEN_CLAIM_FH: | |
1468 | break; | |
1469 | case NFS4_OPEN_CLAIM_PREVIOUS: | |
1470 | if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags)) | |
1471 | break; | |
01e03bdc | 1472 | /* Fall through */ |
2a606188 TM |
1473 | default: |
1474 | return 0; | |
1475 | } | |
b7391f44 | 1476 | nfs_mark_delegation_referenced(delegation); |
aac00a8d TM |
1477 | return 1; |
1478 | } | |
1479 | ||
dc0b027d | 1480 | static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode) |
e7616923 | 1481 | { |
dc0b027d | 1482 | switch (fmode) { |
e7616923 TM |
1483 | case FMODE_WRITE: |
1484 | state->n_wronly++; | |
1485 | break; | |
1486 | case FMODE_READ: | |
1487 | state->n_rdonly++; | |
1488 | break; | |
1489 | case FMODE_READ|FMODE_WRITE: | |
1490 | state->n_rdwr++; | |
1491 | } | |
dc0b027d | 1492 | nfs4_state_set_mode_locked(state, state->state | fmode); |
003707c7 TM |
1493 | } |
1494 | ||
8a64c4ef TM |
1495 | #ifdef CONFIG_NFS_V4_1 |
1496 | static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state) | |
1497 | { | |
1498 | if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags)) | |
1499 | return true; | |
1500 | if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags)) | |
1501 | return true; | |
1502 | if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags)) | |
1503 | return true; | |
1504 | return false; | |
1505 | } | |
1506 | #endif /* CONFIG_NFS_V4_1 */ | |
1507 | ||
c9399f21 TM |
1508 | static void nfs_state_log_update_open_stateid(struct nfs4_state *state) |
1509 | { | |
1510 | if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags)) | |
1511 | wake_up_all(&state->waitq); | |
1512 | } | |
1513 | ||
1514 | static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state, | |
1515 | const nfs4_stateid *stateid) | |
1516 | { | |
1517 | u32 state_seqid = be32_to_cpu(state->open_stateid.seqid); | |
1518 | u32 stateid_seqid = be32_to_cpu(stateid->seqid); | |
1519 | ||
1520 | if (stateid_seqid == state_seqid + 1U || | |
1521 | (stateid_seqid == 1U && state_seqid == 0xffffffffU)) | |
1522 | nfs_state_log_update_open_stateid(state); | |
1523 | else | |
1524 | set_bit(NFS_STATE_CHANGE_WAIT, &state->flags); | |
1525 | } | |
1526 | ||
4f14c194 TM |
1527 | static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state) |
1528 | { | |
1529 | struct nfs_client *clp = state->owner->so_server->nfs_client; | |
1530 | bool need_recover = false; | |
1531 | ||
1532 | if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly) | |
1533 | need_recover = true; | |
1534 | if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly) | |
1535 | need_recover = true; | |
1536 | if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr) | |
1537 | need_recover = true; | |
1538 | if (need_recover) | |
1539 | nfs4_state_mark_reclaim_nograce(clp, state); | |
1540 | } | |
1541 | ||
c9399f21 TM |
1542 | /* |
1543 | * Check for whether or not the caller may update the open stateid | |
1544 | * to the value passed in by stateid. | |
1545 | * | |
1546 | * Note: This function relies heavily on the server implementing | |
1547 | * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2 | |
1548 | * correctly. | |
1549 | * i.e. The stateid seqids have to be initialised to 1, and | |
1550 | * are then incremented on every state transition. | |
1551 | */ | |
e999e80e | 1552 | static bool nfs_need_update_open_stateid(struct nfs4_state *state, |
c9399f21 | 1553 | const nfs4_stateid *stateid) |
e999e80e | 1554 | { |
c9399f21 TM |
1555 | if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 || |
1556 | !nfs4_stateid_match_other(stateid, &state->open_stateid)) { | |
1557 | if (stateid->seqid == cpu_to_be32(1)) | |
1558 | nfs_state_log_update_open_stateid(state); | |
1559 | else | |
1560 | set_bit(NFS_STATE_CHANGE_WAIT, &state->flags); | |
e999e80e | 1561 | return true; |
4f14c194 | 1562 | } |
c9399f21 TM |
1563 | |
1564 | if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) { | |
1565 | nfs_state_log_out_of_order_open_stateid(state, stateid); | |
e999e80e | 1566 | return true; |
c9399f21 | 1567 | } |
e999e80e TM |
1568 | return false; |
1569 | } | |
1570 | ||
f95549cf TM |
1571 | static void nfs_resync_open_stateid_locked(struct nfs4_state *state) |
1572 | { | |
3c38cbe2 TM |
1573 | if (!(state->n_wronly || state->n_rdonly || state->n_rdwr)) |
1574 | return; | |
f95549cf TM |
1575 | if (state->n_wronly) |
1576 | set_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1577 | if (state->n_rdonly) | |
1578 | set_bit(NFS_O_RDONLY_STATE, &state->flags); | |
1579 | if (state->n_rdwr) | |
1580 | set_bit(NFS_O_RDWR_STATE, &state->flags); | |
3c38cbe2 | 1581 | set_bit(NFS_OPEN_STATE, &state->flags); |
f95549cf TM |
1582 | } |
1583 | ||
226056c5 TM |
1584 | static void nfs_clear_open_stateid_locked(struct nfs4_state *state, |
1585 | nfs4_stateid *stateid, fmode_t fmode) | |
003707c7 | 1586 | { |
226056c5 TM |
1587 | clear_bit(NFS_O_RDWR_STATE, &state->flags); |
1588 | switch (fmode & (FMODE_READ|FMODE_WRITE)) { | |
1589 | case FMODE_WRITE: | |
1590 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); | |
1591 | break; | |
1592 | case FMODE_READ: | |
1593 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1594 | break; | |
1595 | case 0: | |
1596 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); | |
1597 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1598 | clear_bit(NFS_OPEN_STATE, &state->flags); | |
1599 | } | |
1600 | if (stateid == NULL) | |
1601 | return; | |
3e7dfb16 TM |
1602 | /* Handle OPEN+OPEN_DOWNGRADE races */ |
1603 | if (nfs4_stateid_match_other(stateid, &state->open_stateid) && | |
1604 | !nfs4_stateid_is_newer(stateid, &state->open_stateid)) { | |
f95549cf | 1605 | nfs_resync_open_stateid_locked(state); |
c9399f21 | 1606 | goto out; |
f95549cf | 1607 | } |
003707c7 | 1608 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) |
f597c537 TM |
1609 | nfs4_stateid_copy(&state->stateid, stateid); |
1610 | nfs4_stateid_copy(&state->open_stateid, stateid); | |
ad9e02dc | 1611 | trace_nfs4_open_stateid_update(state->inode, stateid, 0); |
c9399f21 TM |
1612 | out: |
1613 | nfs_state_log_update_open_stateid(state); | |
226056c5 TM |
1614 | } |
1615 | ||
4a1e2feb TM |
1616 | static void nfs_clear_open_stateid(struct nfs4_state *state, |
1617 | nfs4_stateid *arg_stateid, | |
1618 | nfs4_stateid *stateid, fmode_t fmode) | |
226056c5 TM |
1619 | { |
1620 | write_seqlock(&state->seqlock); | |
3e7dfb16 TM |
1621 | /* Ignore, if the CLOSE argment doesn't match the current stateid */ |
1622 | if (nfs4_state_match_open_stateid_other(state, arg_stateid)) | |
1623 | nfs_clear_open_stateid_locked(state, stateid, fmode); | |
226056c5 | 1624 | write_sequnlock(&state->seqlock); |
4f14c194 TM |
1625 | if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) |
1626 | nfs4_schedule_state_manager(state->owner->so_server->nfs_client); | |
226056c5 TM |
1627 | } |
1628 | ||
1393d961 | 1629 | static void nfs_set_open_stateid_locked(struct nfs4_state *state, |
c9399f21 | 1630 | const nfs4_stateid *stateid, nfs4_stateid *freeme) |
e9acf210 TM |
1631 | __must_hold(&state->owner->so_lock) |
1632 | __must_hold(&state->seqlock) | |
1633 | __must_hold(RCU) | |
1634 | ||
003707c7 | 1635 | { |
c9399f21 TM |
1636 | DEFINE_WAIT(wait); |
1637 | int status = 0; | |
1638 | for (;;) { | |
1639 | ||
1640 | if (!nfs_need_update_open_stateid(state, stateid)) | |
1641 | return; | |
1642 | if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags)) | |
003707c7 | 1643 | break; |
c9399f21 | 1644 | if (status) |
003707c7 | 1645 | break; |
c9399f21 TM |
1646 | /* Rely on seqids for serialisation with NFSv4.0 */ |
1647 | if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client)) | |
1648 | break; | |
1649 | ||
1650 | prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE); | |
1651 | /* | |
1652 | * Ensure we process the state changes in the same order | |
1653 | * in which the server processed them by delaying the | |
1654 | * update of the stateid until we are in sequence. | |
1655 | */ | |
1656 | write_sequnlock(&state->seqlock); | |
1657 | spin_unlock(&state->owner->so_lock); | |
1658 | rcu_read_unlock(); | |
ad9e02dc | 1659 | trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0); |
c9399f21 TM |
1660 | if (!signal_pending(current)) { |
1661 | if (schedule_timeout(5*HZ) == 0) | |
1662 | status = -EAGAIN; | |
1663 | else | |
1664 | status = 0; | |
1665 | } else | |
1666 | status = -EINTR; | |
1667 | finish_wait(&state->waitq, &wait); | |
1668 | rcu_read_lock(); | |
1669 | spin_lock(&state->owner->so_lock); | |
1670 | write_seqlock(&state->seqlock); | |
003707c7 | 1671 | } |
c9399f21 | 1672 | |
e1fff5df TM |
1673 | if (test_bit(NFS_OPEN_STATE, &state->flags) && |
1674 | !nfs4_stateid_match_other(stateid, &state->open_stateid)) { | |
c9399f21 TM |
1675 | nfs4_stateid_copy(freeme, &state->open_stateid); |
1676 | nfs_test_and_clear_all_open_stateid(state); | |
1677 | } | |
1678 | ||
e999e80e TM |
1679 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) |
1680 | nfs4_stateid_copy(&state->stateid, stateid); | |
1681 | nfs4_stateid_copy(&state->open_stateid, stateid); | |
ad9e02dc | 1682 | trace_nfs4_open_stateid_update(state->inode, stateid, status); |
c9399f21 | 1683 | nfs_state_log_update_open_stateid(state); |
003707c7 TM |
1684 | } |
1685 | ||
c9399f21 | 1686 | static void nfs_state_set_open_stateid(struct nfs4_state *state, |
1393d961 | 1687 | const nfs4_stateid *open_stateid, |
1393d961 TM |
1688 | fmode_t fmode, |
1689 | nfs4_stateid *freeme) | |
1da177e4 | 1690 | { |
8bda4e4c TM |
1691 | /* |
1692 | * Protect the call to nfs4_state_set_mode_locked and | |
1693 | * serialise the stateid update | |
1694 | */ | |
1695 | write_seqlock(&state->seqlock); | |
c9399f21 TM |
1696 | nfs_set_open_stateid_locked(state, open_stateid, freeme); |
1697 | switch (fmode) { | |
1698 | case FMODE_READ: | |
1699 | set_bit(NFS_O_RDONLY_STATE, &state->flags); | |
1700 | break; | |
1701 | case FMODE_WRITE: | |
1702 | set_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1703 | break; | |
1704 | case FMODE_READ|FMODE_WRITE: | |
1705 | set_bit(NFS_O_RDWR_STATE, &state->flags); | |
003707c7 | 1706 | } |
c9399f21 TM |
1707 | set_bit(NFS_OPEN_STATE, &state->flags); |
1708 | write_sequnlock(&state->seqlock); | |
1709 | } | |
1710 | ||
27a30cf6 TM |
1711 | static void nfs_state_clear_open_state_flags(struct nfs4_state *state) |
1712 | { | |
1713 | clear_bit(NFS_O_RDWR_STATE, &state->flags); | |
1714 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1715 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); | |
1716 | clear_bit(NFS_OPEN_STATE, &state->flags); | |
1717 | } | |
1718 | ||
c9399f21 TM |
1719 | static void nfs_state_set_delegation(struct nfs4_state *state, |
1720 | const nfs4_stateid *deleg_stateid, | |
1721 | fmode_t fmode) | |
1722 | { | |
1723 | /* | |
1724 | * Protect the call to nfs4_state_set_mode_locked and | |
1725 | * serialise the stateid update | |
1726 | */ | |
1727 | write_seqlock(&state->seqlock); | |
1728 | nfs4_stateid_copy(&state->stateid, deleg_stateid); | |
1729 | set_bit(NFS_DELEGATED_STATE, &state->flags); | |
8bda4e4c | 1730 | write_sequnlock(&state->seqlock); |
1da177e4 LT |
1731 | } |
1732 | ||
9f0c5124 TM |
1733 | static void nfs_state_clear_delegation(struct nfs4_state *state) |
1734 | { | |
1735 | write_seqlock(&state->seqlock); | |
1736 | nfs4_stateid_copy(&state->stateid, &state->open_stateid); | |
1737 | clear_bit(NFS_DELEGATED_STATE, &state->flags); | |
1738 | write_sequnlock(&state->seqlock); | |
1739 | } | |
1740 | ||
ec4b0925 | 1741 | int update_open_stateid(struct nfs4_state *state, |
1393d961 TM |
1742 | const nfs4_stateid *open_stateid, |
1743 | const nfs4_stateid *delegation, | |
1744 | fmode_t fmode) | |
34310430 | 1745 | { |
1393d961 TM |
1746 | struct nfs_server *server = NFS_SERVER(state->inode); |
1747 | struct nfs_client *clp = server->nfs_client; | |
34310430 TM |
1748 | struct nfs_inode *nfsi = NFS_I(state->inode); |
1749 | struct nfs_delegation *deleg_cur; | |
83aa3e0f | 1750 | nfs4_stateid freeme = { }; |
34310430 TM |
1751 | int ret = 0; |
1752 | ||
dc0b027d | 1753 | fmode &= (FMODE_READ|FMODE_WRITE); |
34310430 TM |
1754 | |
1755 | rcu_read_lock(); | |
c9399f21 TM |
1756 | spin_lock(&state->owner->so_lock); |
1757 | if (open_stateid != NULL) { | |
1758 | nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme); | |
1759 | ret = 1; | |
1760 | } | |
1761 | ||
333ac786 | 1762 | deleg_cur = nfs4_get_valid_delegation(state->inode); |
34310430 TM |
1763 | if (deleg_cur == NULL) |
1764 | goto no_delegation; | |
1765 | ||
1766 | spin_lock(&deleg_cur->lock); | |
17f26b12 | 1767 | if (rcu_dereference(nfsi->delegation) != deleg_cur || |
d25be546 | 1768 | test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) || |
dc0b027d | 1769 | (deleg_cur->type & fmode) != fmode) |
34310430 TM |
1770 | goto no_delegation_unlock; |
1771 | ||
1772 | if (delegation == NULL) | |
1773 | delegation = &deleg_cur->stateid; | |
333ac786 | 1774 | else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation)) |
34310430 TM |
1775 | goto no_delegation_unlock; |
1776 | ||
b7391f44 | 1777 | nfs_mark_delegation_referenced(deleg_cur); |
c9399f21 | 1778 | nfs_state_set_delegation(state, &deleg_cur->stateid, fmode); |
34310430 TM |
1779 | ret = 1; |
1780 | no_delegation_unlock: | |
1781 | spin_unlock(&deleg_cur->lock); | |
1782 | no_delegation: | |
c9399f21 TM |
1783 | if (ret) |
1784 | update_open_stateflags(state, fmode); | |
1785 | spin_unlock(&state->owner->so_lock); | |
34310430 TM |
1786 | rcu_read_unlock(); |
1787 | ||
4f14c194 | 1788 | if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) |
1393d961 TM |
1789 | nfs4_schedule_state_manager(clp); |
1790 | if (freeme.type != 0) | |
1791 | nfs4_test_and_free_stateid(server, &freeme, | |
1792 | state->owner->so_cred); | |
34310430 TM |
1793 | |
1794 | return ret; | |
1795 | } | |
1796 | ||
39071e6f TM |
1797 | static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp, |
1798 | const nfs4_stateid *stateid) | |
1799 | { | |
1800 | struct nfs4_state *state = lsp->ls_state; | |
1801 | bool ret = false; | |
1802 | ||
1803 | spin_lock(&state->state_lock); | |
1804 | if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid)) | |
1805 | goto out_noupdate; | |
1806 | if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid)) | |
1807 | goto out_noupdate; | |
1808 | nfs4_stateid_copy(&lsp->ls_stateid, stateid); | |
1809 | ret = true; | |
1810 | out_noupdate: | |
1811 | spin_unlock(&state->state_lock); | |
1812 | return ret; | |
1813 | } | |
34310430 | 1814 | |
dc0b027d | 1815 | static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode) |
aac00a8d TM |
1816 | { |
1817 | struct nfs_delegation *delegation; | |
1818 | ||
f5086242 | 1819 | fmode &= FMODE_READ|FMODE_WRITE; |
aac00a8d | 1820 | rcu_read_lock(); |
40e6aa10 | 1821 | delegation = nfs4_get_valid_delegation(inode); |
dc0b027d | 1822 | if (delegation == NULL || (delegation->type & fmode) == fmode) { |
aac00a8d TM |
1823 | rcu_read_unlock(); |
1824 | return; | |
1825 | } | |
1826 | rcu_read_unlock(); | |
57ec14c5 | 1827 | nfs4_inode_return_delegation(inode); |
aac00a8d TM |
1828 | } |
1829 | ||
6ee41268 | 1830 | static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata) |
aac00a8d TM |
1831 | { |
1832 | struct nfs4_state *state = opendata->state; | |
aac00a8d | 1833 | struct nfs_delegation *delegation; |
f448badd | 1834 | int open_mode = opendata->o_arg.open_flags; |
dc0b027d | 1835 | fmode_t fmode = opendata->o_arg.fmode; |
2a606188 | 1836 | enum open_claim_type4 claim = opendata->o_arg.claim; |
aac00a8d TM |
1837 | nfs4_stateid stateid; |
1838 | int ret = -EAGAIN; | |
1839 | ||
aac00a8d | 1840 | for (;;) { |
61beef75 | 1841 | spin_lock(&state->owner->so_lock); |
be189f7e | 1842 | if (can_open_cached(state, fmode, open_mode, claim)) { |
61beef75 | 1843 | update_open_stateflags(state, fmode); |
6ee41268 | 1844 | spin_unlock(&state->owner->so_lock); |
61beef75 | 1845 | goto out_return_state; |
6ee41268 | 1846 | } |
61beef75 | 1847 | spin_unlock(&state->owner->so_lock); |
34310430 | 1848 | rcu_read_lock(); |
be3df3dd | 1849 | delegation = nfs4_get_valid_delegation(state->inode); |
2a606188 | 1850 | if (!can_open_delegated(delegation, fmode, claim)) { |
34310430 | 1851 | rcu_read_unlock(); |
aac00a8d | 1852 | break; |
34310430 | 1853 | } |
aac00a8d | 1854 | /* Save the delegation */ |
f597c537 | 1855 | nfs4_stateid_copy(&stateid, &delegation->stateid); |
aac00a8d | 1856 | rcu_read_unlock(); |
fa332941 | 1857 | nfs_release_seqid(opendata->o_arg.seqid); |
bdeca1b7 TM |
1858 | if (!opendata->is_recover) { |
1859 | ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode); | |
1860 | if (ret != 0) | |
1861 | goto out; | |
1862 | } | |
aac00a8d | 1863 | ret = -EAGAIN; |
34310430 TM |
1864 | |
1865 | /* Try to update the stateid using the delegation */ | |
dc0b027d | 1866 | if (update_open_stateid(state, NULL, &stateid, fmode)) |
34310430 | 1867 | goto out_return_state; |
aac00a8d | 1868 | } |
aac00a8d TM |
1869 | out: |
1870 | return ERR_PTR(ret); | |
1871 | out_return_state: | |
ace9fad4 | 1872 | refcount_inc(&state->count); |
aac00a8d TM |
1873 | return state; |
1874 | } | |
1875 | ||
e23008ec AA |
1876 | static void |
1877 | nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state) | |
1878 | { | |
1879 | struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client; | |
1880 | struct nfs_delegation *delegation; | |
1881 | int delegation_flags = 0; | |
1882 | ||
1883 | rcu_read_lock(); | |
1884 | delegation = rcu_dereference(NFS_I(state->inode)->delegation); | |
1885 | if (delegation) | |
1886 | delegation_flags = delegation->flags; | |
1887 | rcu_read_unlock(); | |
72d79ff8 TM |
1888 | switch (data->o_arg.claim) { |
1889 | default: | |
1890 | break; | |
1891 | case NFS4_OPEN_CLAIM_DELEGATE_CUR: | |
1892 | case NFS4_OPEN_CLAIM_DELEG_CUR_FH: | |
e23008ec AA |
1893 | pr_err_ratelimited("NFS: Broken NFSv4 server %s is " |
1894 | "returning a delegation for " | |
1895 | "OPEN(CLAIM_DELEGATE_CUR)\n", | |
1896 | clp->cl_hostname); | |
72d79ff8 TM |
1897 | return; |
1898 | } | |
1899 | if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0) | |
e23008ec | 1900 | nfs_inode_set_delegation(state->inode, |
35156bff TM |
1901 | data->owner->so_cred, |
1902 | data->o_res.delegation_type, | |
1903 | &data->o_res.delegation, | |
1904 | data->o_res.pagemod_limit); | |
e23008ec AA |
1905 | else |
1906 | nfs_inode_reclaim_delegation(state->inode, | |
35156bff TM |
1907 | data->owner->so_cred, |
1908 | data->o_res.delegation_type, | |
1909 | &data->o_res.delegation, | |
1910 | data->o_res.pagemod_limit); | |
8b199e58 JL |
1911 | |
1912 | if (data->o_res.do_recall) | |
1913 | nfs_async_inode_return_delegation(state->inode, | |
1914 | &data->o_res.delegation); | |
e23008ec AA |
1915 | } |
1916 | ||
1917 | /* | |
1918 | * Check the inode attributes against the CLAIM_PREVIOUS returned attributes | |
1919 | * and update the nfs4_state. | |
1920 | */ | |
1921 | static struct nfs4_state * | |
1922 | _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data) | |
1923 | { | |
1924 | struct inode *inode = data->state->inode; | |
1925 | struct nfs4_state *state = data->state; | |
1926 | int ret; | |
1927 | ||
d2bfda2e | 1928 | if (!data->rpc_done) { |
37a8484a AS |
1929 | if (data->rpc_status) |
1930 | return ERR_PTR(data->rpc_status); | |
d2bfda2e WAA |
1931 | /* cached opens have already been processed */ |
1932 | goto update; | |
e23008ec AA |
1933 | } |
1934 | ||
e23008ec AA |
1935 | ret = nfs_refresh_inode(inode, &data->f_attr); |
1936 | if (ret) | |
37a8484a | 1937 | return ERR_PTR(ret); |
e23008ec AA |
1938 | |
1939 | if (data->o_res.delegation_type != 0) | |
1940 | nfs4_opendata_check_deleg(data, state); | |
d2bfda2e | 1941 | update: |
e3c8dc76 TM |
1942 | if (!update_open_stateid(state, &data->o_res.stateid, |
1943 | NULL, data->o_arg.fmode)) | |
1944 | return ERR_PTR(-EAGAIN); | |
ace9fad4 | 1945 | refcount_inc(&state->count); |
e23008ec AA |
1946 | |
1947 | return state; | |
e23008ec AA |
1948 | } |
1949 | ||
4e2fcac7 TM |
1950 | static struct inode * |
1951 | nfs4_opendata_get_inode(struct nfs4_opendata *data) | |
1952 | { | |
1953 | struct inode *inode; | |
1954 | ||
1955 | switch (data->o_arg.claim) { | |
1956 | case NFS4_OPEN_CLAIM_NULL: | |
1957 | case NFS4_OPEN_CLAIM_DELEGATE_CUR: | |
1958 | case NFS4_OPEN_CLAIM_DELEGATE_PREV: | |
1959 | if (!(data->f_attr.valid & NFS_ATTR_FATTR)) | |
1960 | return ERR_PTR(-EAGAIN); | |
1961 | inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, | |
1962 | &data->f_attr, data->f_label); | |
1963 | break; | |
1964 | default: | |
1965 | inode = d_inode(data->dentry); | |
1966 | ihold(inode); | |
1967 | nfs_refresh_inode(inode, &data->f_attr); | |
1968 | } | |
1969 | return inode; | |
1970 | } | |
1971 | ||
e23008ec | 1972 | static struct nfs4_state * |
75e8c48b | 1973 | nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data) |
24ac23ab | 1974 | { |
75e8c48b | 1975 | struct nfs4_state *state; |
24ac23ab | 1976 | struct inode *inode; |
75e8c48b TM |
1977 | |
1978 | inode = nfs4_opendata_get_inode(data); | |
1979 | if (IS_ERR(inode)) | |
1980 | return ERR_CAST(inode); | |
1981 | if (data->state != NULL && data->state->inode == inode) { | |
1982 | state = data->state; | |
ace9fad4 | 1983 | refcount_inc(&state->count); |
75e8c48b TM |
1984 | } else |
1985 | state = nfs4_get_open_state(inode, data->owner); | |
1986 | iput(inode); | |
1987 | if (state == NULL) | |
1988 | state = ERR_PTR(-ENOMEM); | |
1989 | return state; | |
1990 | } | |
1991 | ||
1992 | static struct nfs4_state * | |
1993 | _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) | |
1994 | { | |
1995 | struct nfs4_state *state; | |
24ac23ab | 1996 | |
aac00a8d | 1997 | if (!data->rpc_done) { |
6ee41268 | 1998 | state = nfs4_try_open_cached(data); |
9759b0fb | 1999 | trace_nfs4_cached_open(data->state); |
aac00a8d TM |
2000 | goto out; |
2001 | } | |
2002 | ||
75e8c48b TM |
2003 | state = nfs4_opendata_find_nfs4_state(data); |
2004 | if (IS_ERR(state)) | |
2005 | goto out; | |
2006 | ||
e23008ec AA |
2007 | if (data->o_res.delegation_type != 0) |
2008 | nfs4_opendata_check_deleg(data, state); | |
e3c8dc76 TM |
2009 | if (!update_open_stateid(state, &data->o_res.stateid, |
2010 | NULL, data->o_arg.fmode)) { | |
2011 | nfs4_put_open_state(state); | |
2012 | state = ERR_PTR(-EAGAIN); | |
2013 | } | |
aac00a8d | 2014 | out: |
7aa262b5 | 2015 | nfs_release_seqid(data->o_arg.seqid); |
24ac23ab TM |
2016 | return state; |
2017 | } | |
2018 | ||
e23008ec AA |
2019 | static struct nfs4_state * |
2020 | nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) | |
2021 | { | |
2e80dbe7 TM |
2022 | struct nfs4_state *ret; |
2023 | ||
e23008ec | 2024 | if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) |
2e80dbe7 TM |
2025 | ret =_nfs4_opendata_reclaim_to_nfs4_state(data); |
2026 | else | |
2027 | ret = _nfs4_opendata_to_nfs4_state(data); | |
2028 | nfs4_sequence_free_slot(&data->o_res.seq_res); | |
2029 | return ret; | |
e23008ec AA |
2030 | } |
2031 | ||
0de43976 TM |
2032 | static struct nfs_open_context * |
2033 | nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode) | |
864472e9 TM |
2034 | { |
2035 | struct nfs_inode *nfsi = NFS_I(state->inode); | |
2036 | struct nfs_open_context *ctx; | |
2037 | ||
0de43976 TM |
2038 | rcu_read_lock(); |
2039 | list_for_each_entry_rcu(ctx, &nfsi->open_files, list) { | |
864472e9 TM |
2040 | if (ctx->state != state) |
2041 | continue; | |
0de43976 TM |
2042 | if ((ctx->mode & mode) != mode) |
2043 | continue; | |
2044 | if (!get_nfs_open_context(ctx)) | |
2045 | continue; | |
2046 | rcu_read_unlock(); | |
864472e9 TM |
2047 | return ctx; |
2048 | } | |
0de43976 | 2049 | rcu_read_unlock(); |
864472e9 TM |
2050 | return ERR_PTR(-ENOENT); |
2051 | } | |
2052 | ||
0de43976 TM |
2053 | static struct nfs_open_context * |
2054 | nfs4_state_find_open_context(struct nfs4_state *state) | |
2055 | { | |
2056 | struct nfs_open_context *ctx; | |
2057 | ||
2058 | ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE); | |
2059 | if (!IS_ERR(ctx)) | |
2060 | return ctx; | |
2061 | ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE); | |
2062 | if (!IS_ERR(ctx)) | |
2063 | return ctx; | |
2064 | return nfs4_state_find_open_context_mode(state, FMODE_READ); | |
2065 | } | |
2066 | ||
4a1c0893 TM |
2067 | static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, |
2068 | struct nfs4_state *state, enum open_claim_type4 claim) | |
6f220ed5 TM |
2069 | { |
2070 | struct nfs4_opendata *opendata; | |
2071 | ||
4a1c0893 | 2072 | opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, |
8fd1ab74 | 2073 | NULL, claim, GFP_NOFS); |
6f220ed5 TM |
2074 | if (opendata == NULL) |
2075 | return ERR_PTR(-ENOMEM); | |
2076 | opendata->state = state; | |
ace9fad4 | 2077 | refcount_inc(&state->count); |
6f220ed5 TM |
2078 | return opendata; |
2079 | } | |
2080 | ||
24311f88 TM |
2081 | static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, |
2082 | fmode_t fmode) | |
864472e9 | 2083 | { |
2ced46c2 | 2084 | struct nfs4_state *newstate; |
864472e9 TM |
2085 | int ret; |
2086 | ||
24311f88 | 2087 | if (!nfs4_mode_match_open_stateid(opendata->state, fmode)) |
39f897fd | 2088 | return 0; |
dc0b027d TM |
2089 | opendata->o_arg.open_flags = 0; |
2090 | opendata->o_arg.fmode = fmode; | |
be36e185 TM |
2091 | opendata->o_arg.share_access = nfs4_map_atomic_open_share( |
2092 | NFS_SB(opendata->dentry->d_sb), | |
2093 | fmode, 0); | |
2ced46c2 TM |
2094 | memset(&opendata->o_res, 0, sizeof(opendata->o_res)); |
2095 | memset(&opendata->c_res, 0, sizeof(opendata->c_res)); | |
2096 | nfs4_init_opendata_res(opendata); | |
b257957e | 2097 | ret = _nfs4_recover_proc_open(opendata); |
864472e9 TM |
2098 | if (ret != 0) |
2099 | return ret; | |
2ced46c2 | 2100 | newstate = nfs4_opendata_to_nfs4_state(opendata); |
1b370bc2 TM |
2101 | if (IS_ERR(newstate)) |
2102 | return PTR_ERR(newstate); | |
24311f88 TM |
2103 | if (newstate != opendata->state) |
2104 | ret = -ESTALE; | |
643168c2 | 2105 | nfs4_close_state(newstate, fmode); |
24311f88 | 2106 | return ret; |
864472e9 TM |
2107 | } |
2108 | ||
2109 | static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state) | |
2110 | { | |
864472e9 TM |
2111 | int ret; |
2112 | ||
2113 | /* memory barrier prior to reading state->n_* */ | |
2114 | smp_rmb(); | |
24311f88 TM |
2115 | ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE); |
2116 | if (ret != 0) | |
2117 | return ret; | |
2118 | ret = nfs4_open_recover_helper(opendata, FMODE_WRITE); | |
2119 | if (ret != 0) | |
2120 | return ret; | |
2121 | ret = nfs4_open_recover_helper(opendata, FMODE_READ); | |
2122 | if (ret != 0) | |
2123 | return ret; | |
1ac7e2fd TM |
2124 | /* |
2125 | * We may have performed cached opens for all three recoveries. | |
2126 | * Check if we need to update the current stateid. | |
2127 | */ | |
2128 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 && | |
f597c537 | 2129 | !nfs4_stateid_match(&state->stateid, &state->open_stateid)) { |
8bda4e4c | 2130 | write_seqlock(&state->seqlock); |
1ac7e2fd | 2131 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) |
f597c537 | 2132 | nfs4_stateid_copy(&state->stateid, &state->open_stateid); |
8bda4e4c | 2133 | write_sequnlock(&state->seqlock); |
1ac7e2fd | 2134 | } |
864472e9 TM |
2135 | return 0; |
2136 | } | |
2137 | ||
1da177e4 LT |
2138 | /* |
2139 | * OPEN_RECLAIM: | |
2140 | * reclaim state on the server after a reboot. | |
1da177e4 | 2141 | */ |
539cd03a | 2142 | static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) |
1da177e4 | 2143 | { |
1ac7e2fd | 2144 | struct nfs_delegation *delegation; |
864472e9 | 2145 | struct nfs4_opendata *opendata; |
dc0b027d | 2146 | fmode_t delegation_type = 0; |
1da177e4 LT |
2147 | int status; |
2148 | ||
4a1c0893 TM |
2149 | opendata = nfs4_open_recoverdata_alloc(ctx, state, |
2150 | NFS4_OPEN_CLAIM_PREVIOUS); | |
6f220ed5 TM |
2151 | if (IS_ERR(opendata)) |
2152 | return PTR_ERR(opendata); | |
1ac7e2fd TM |
2153 | rcu_read_lock(); |
2154 | delegation = rcu_dereference(NFS_I(state->inode)->delegation); | |
15c831bf | 2155 | if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0) |
65bbf6bd | 2156 | delegation_type = delegation->type; |
1ac7e2fd | 2157 | rcu_read_unlock(); |
864472e9 TM |
2158 | opendata->o_arg.u.delegation_type = delegation_type; |
2159 | status = nfs4_open_recover(opendata, state); | |
c6d00e63 | 2160 | nfs4_opendata_put(opendata); |
1da177e4 LT |
2161 | return status; |
2162 | } | |
2163 | ||
539cd03a | 2164 | static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) |
1da177e4 LT |
2165 | { |
2166 | struct nfs_server *server = NFS_SERVER(state->inode); | |
2167 | struct nfs4_exception exception = { }; | |
2168 | int err; | |
2169 | do { | |
539cd03a | 2170 | err = _nfs4_do_open_reclaim(ctx, state); |
42113a75 | 2171 | trace_nfs4_open_reclaim(ctx, 0, err); |
49f9a0fa TM |
2172 | if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) |
2173 | continue; | |
168667c4 | 2174 | if (err != -NFS4ERR_DELAY) |
202b50dc TM |
2175 | break; |
2176 | nfs4_handle_exception(server, err, &exception); | |
1da177e4 LT |
2177 | } while (exception.retry); |
2178 | return err; | |
2179 | } | |
2180 | ||
864472e9 TM |
2181 | static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state) |
2182 | { | |
2183 | struct nfs_open_context *ctx; | |
2184 | int ret; | |
2185 | ||
2186 | ctx = nfs4_state_find_open_context(state); | |
2187 | if (IS_ERR(ctx)) | |
91876b13 | 2188 | return -EAGAIN; |
27a30cf6 TM |
2189 | clear_bit(NFS_DELEGATED_STATE, &state->flags); |
2190 | nfs_state_clear_open_state_flags(state); | |
539cd03a | 2191 | ret = nfs4_do_open_reclaim(ctx, state); |
864472e9 TM |
2192 | put_nfs_open_context(ctx); |
2193 | return ret; | |
2194 | } | |
2195 | ||
dce2630c | 2196 | 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 | 2197 | { |
be76b5b6 TM |
2198 | switch (err) { |
2199 | default: | |
2200 | printk(KERN_ERR "NFS: %s: unhandled error " | |
2201 | "%d.\n", __func__, err); | |
2202 | case 0: | |
2203 | case -ENOENT: | |
8eee52af | 2204 | case -EAGAIN: |
be76b5b6 | 2205 | case -ESTALE: |
67e7b52d | 2206 | case -ETIMEDOUT: |
be76b5b6 TM |
2207 | break; |
2208 | case -NFS4ERR_BADSESSION: | |
2209 | case -NFS4ERR_BADSLOT: | |
2210 | case -NFS4ERR_BAD_HIGH_SLOT: | |
2211 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
2212 | case -NFS4ERR_DEADSESSION: | |
be76b5b6 TM |
2213 | return -EAGAIN; |
2214 | case -NFS4ERR_STALE_CLIENTID: | |
2215 | case -NFS4ERR_STALE_STATEID: | |
be76b5b6 TM |
2216 | /* Don't recall a delegation if it was lost */ |
2217 | nfs4_schedule_lease_recovery(server->nfs_client); | |
2218 | return -EAGAIN; | |
352297b9 CL |
2219 | case -NFS4ERR_MOVED: |
2220 | nfs4_schedule_migration_recovery(server); | |
2221 | return -EAGAIN; | |
8ef2f8d4 CL |
2222 | case -NFS4ERR_LEASE_MOVED: |
2223 | nfs4_schedule_lease_moved_recovery(server->nfs_client); | |
2224 | return -EAGAIN; | |
be76b5b6 TM |
2225 | case -NFS4ERR_DELEG_REVOKED: |
2226 | case -NFS4ERR_ADMIN_REVOKED: | |
404ea356 | 2227 | case -NFS4ERR_EXPIRED: |
be76b5b6 | 2228 | case -NFS4ERR_BAD_STATEID: |
db4f2e63 | 2229 | case -NFS4ERR_OPENMODE: |
be76b5b6 TM |
2230 | nfs_inode_find_state_and_recover(state->inode, |
2231 | stateid); | |
2232 | nfs4_schedule_stateid_recovery(server, state); | |
869f9dfa | 2233 | return -EAGAIN; |
be76b5b6 TM |
2234 | case -NFS4ERR_DELAY: |
2235 | case -NFS4ERR_GRACE: | |
be76b5b6 TM |
2236 | ssleep(1); |
2237 | return -EAGAIN; | |
db4f2e63 TM |
2238 | case -ENOMEM: |
2239 | case -NFS4ERR_DENIED: | |
dce2630c N |
2240 | if (fl) { |
2241 | struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner; | |
2242 | if (lsp) | |
2243 | set_bit(NFS_LOCK_LOST, &lsp->ls_flags); | |
2244 | } | |
db4f2e63 | 2245 | return 0; |
be76b5b6 | 2246 | } |
1da177e4 LT |
2247 | return err; |
2248 | } | |
2249 | ||
24311f88 | 2250 | int nfs4_open_delegation_recall(struct nfs_open_context *ctx, |
5eb8d18c | 2251 | struct nfs4_state *state, const nfs4_stateid *stateid) |
db4f2e63 TM |
2252 | { |
2253 | struct nfs_server *server = NFS_SERVER(state->inode); | |
2254 | struct nfs4_opendata *opendata; | |
24311f88 | 2255 | int err = 0; |
db4f2e63 TM |
2256 | |
2257 | opendata = nfs4_open_recoverdata_alloc(ctx, state, | |
2258 | NFS4_OPEN_CLAIM_DELEG_CUR_FH); | |
2259 | if (IS_ERR(opendata)) | |
2260 | return PTR_ERR(opendata); | |
2261 | nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid); | |
5eb8d18c | 2262 | if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) { |
24311f88 TM |
2263 | err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE); |
2264 | if (err) | |
5eb8d18c TM |
2265 | goto out; |
2266 | } | |
2267 | if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) { | |
24311f88 TM |
2268 | err = nfs4_open_recover_helper(opendata, FMODE_WRITE); |
2269 | if (err) | |
5eb8d18c TM |
2270 | goto out; |
2271 | } | |
2272 | if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) { | |
24311f88 | 2273 | err = nfs4_open_recover_helper(opendata, FMODE_READ); |
5eb8d18c TM |
2274 | if (err) |
2275 | goto out; | |
24311f88 | 2276 | } |
5eb8d18c TM |
2277 | nfs_state_clear_delegation(state); |
2278 | out: | |
db4f2e63 | 2279 | nfs4_opendata_put(opendata); |
dce2630c | 2280 | return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err); |
db4f2e63 TM |
2281 | } |
2282 | ||
be05c860 CL |
2283 | static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata) |
2284 | { | |
2285 | struct nfs4_opendata *data = calldata; | |
2286 | ||
7981c8a6 AS |
2287 | nfs4_setup_sequence(data->o_arg.server->nfs_client, |
2288 | &data->c_arg.seq_args, &data->c_res.seq_res, task); | |
be05c860 CL |
2289 | } |
2290 | ||
cdd4e68b TM |
2291 | static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata) |
2292 | { | |
2293 | struct nfs4_opendata *data = calldata; | |
2294 | ||
17ead6c8 | 2295 | nfs40_sequence_done(task, &data->c_res.seq_res); |
be05c860 | 2296 | |
cdd4e68b | 2297 | data->rpc_status = task->tk_status; |
26e976a8 | 2298 | if (data->rpc_status == 0) { |
f597c537 | 2299 | nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid); |
bb22629e | 2300 | nfs_confirm_seqid(&data->owner->so_seqid, 0); |
26e976a8 | 2301 | renew_lease(data->o_res.server, data->timestamp); |
a7a3b1e9 | 2302 | data->rpc_done = true; |
26e976a8 | 2303 | } |
cdd4e68b TM |
2304 | } |
2305 | ||
2306 | static void nfs4_open_confirm_release(void *calldata) | |
2307 | { | |
2308 | struct nfs4_opendata *data = calldata; | |
2309 | struct nfs4_state *state = NULL; | |
2310 | ||
2311 | /* If this request hasn't been cancelled, do nothing */ | |
a7a3b1e9 | 2312 | if (!data->cancelled) |
cdd4e68b TM |
2313 | goto out_free; |
2314 | /* In case of error, no cleanup! */ | |
3e309914 | 2315 | if (!data->rpc_done) |
cdd4e68b | 2316 | goto out_free; |
cdd4e68b | 2317 | state = nfs4_opendata_to_nfs4_state(data); |
1b370bc2 | 2318 | if (!IS_ERR(state)) |
643168c2 | 2319 | nfs4_close_state(state, data->o_arg.fmode); |
cdd4e68b | 2320 | out_free: |
c6d00e63 | 2321 | nfs4_opendata_put(data); |
cdd4e68b TM |
2322 | } |
2323 | ||
2324 | static const struct rpc_call_ops nfs4_open_confirm_ops = { | |
be05c860 | 2325 | .rpc_call_prepare = nfs4_open_confirm_prepare, |
cdd4e68b TM |
2326 | .rpc_call_done = nfs4_open_confirm_done, |
2327 | .rpc_release = nfs4_open_confirm_release, | |
2328 | }; | |
2329 | ||
2330 | /* | |
2331 | * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata | |
2332 | */ | |
2333 | static int _nfs4_proc_open_confirm(struct nfs4_opendata *data) | |
2334 | { | |
2b0143b5 | 2335 | struct nfs_server *server = NFS_SERVER(d_inode(data->dir)); |
cdd4e68b | 2336 | struct rpc_task *task; |
5138fde0 TM |
2337 | struct rpc_message msg = { |
2338 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM], | |
2339 | .rpc_argp = &data->c_arg, | |
2340 | .rpc_resp = &data->c_res, | |
2341 | .rpc_cred = data->owner->so_cred, | |
2342 | }; | |
c970aa85 TM |
2343 | struct rpc_task_setup task_setup_data = { |
2344 | .rpc_client = server->client, | |
5138fde0 | 2345 | .rpc_message = &msg, |
c970aa85 TM |
2346 | .callback_ops = &nfs4_open_confirm_ops, |
2347 | .callback_data = data, | |
101070ca | 2348 | .workqueue = nfsiod_workqueue, |
61296507 | 2349 | .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, |
c970aa85 | 2350 | }; |
1da177e4 LT |
2351 | int status; |
2352 | ||
fba83f34 AS |
2353 | nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1, |
2354 | data->is_recover); | |
c6d00e63 | 2355 | kref_get(&data->kref); |
a7a3b1e9 | 2356 | data->rpc_done = false; |
3e309914 | 2357 | data->rpc_status = 0; |
5138fde0 | 2358 | data->timestamp = jiffies; |
c970aa85 | 2359 | task = rpc_run_task(&task_setup_data); |
7a1218a2 | 2360 | if (IS_ERR(task)) |
cdd4e68b | 2361 | return PTR_ERR(task); |
820bf85c | 2362 | status = rpc_wait_for_completion_task(task); |
cdd4e68b | 2363 | if (status != 0) { |
a7a3b1e9 | 2364 | data->cancelled = true; |
cdd4e68b TM |
2365 | smp_wmb(); |
2366 | } else | |
2367 | status = data->rpc_status; | |
e6b3c4db | 2368 | rpc_put_task(task); |
1da177e4 LT |
2369 | return status; |
2370 | } | |
2371 | ||
24ac23ab | 2372 | static void nfs4_open_prepare(struct rpc_task *task, void *calldata) |
1da177e4 | 2373 | { |
24ac23ab TM |
2374 | struct nfs4_opendata *data = calldata; |
2375 | struct nfs4_state_owner *sp = data->owner; | |
549b19cc | 2376 | struct nfs_client *clp = sp->so_server->nfs_client; |
2a606188 | 2377 | enum open_claim_type4 claim = data->o_arg.claim; |
5138fde0 | 2378 | |
24ac23ab | 2379 | if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0) |
c8da19b9 | 2380 | goto out_wait; |
aac00a8d TM |
2381 | /* |
2382 | * Check if we still need to send an OPEN call, or if we can use | |
2383 | * a delegation instead. | |
2384 | */ | |
2385 | if (data->state != NULL) { | |
2386 | struct nfs_delegation *delegation; | |
2387 | ||
be189f7e TM |
2388 | if (can_open_cached(data->state, data->o_arg.fmode, |
2389 | data->o_arg.open_flags, claim)) | |
6ee41268 | 2390 | goto out_no_action; |
aac00a8d | 2391 | rcu_read_lock(); |
be3df3dd | 2392 | delegation = nfs4_get_valid_delegation(data->state->inode); |
2a606188 | 2393 | if (can_open_delegated(delegation, data->o_arg.fmode, claim)) |
652f89f6 | 2394 | goto unlock_no_action; |
aac00a8d TM |
2395 | rcu_read_unlock(); |
2396 | } | |
95b72eb0 | 2397 | /* Update client id. */ |
549b19cc | 2398 | data->o_arg.clientid = clp->cl_clientid; |
2a606188 TM |
2399 | switch (claim) { |
2400 | default: | |
2401 | break; | |
8188df17 TM |
2402 | case NFS4_OPEN_CLAIM_PREVIOUS: |
2403 | case NFS4_OPEN_CLAIM_DELEG_CUR_FH: | |
2404 | case NFS4_OPEN_CLAIM_DELEG_PREV_FH: | |
e23008ec | 2405 | data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0]; |
01e03bdc | 2406 | /* Fall through */ |
8188df17 TM |
2407 | case NFS4_OPEN_CLAIM_FH: |
2408 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR]; | |
6f220ed5 | 2409 | } |
26e976a8 | 2410 | data->timestamp = jiffies; |
42e1cca7 | 2411 | if (nfs4_setup_sequence(data->o_arg.server->nfs_client, |
d898528c | 2412 | &data->o_arg.seq_args, |
2240a9e2 TM |
2413 | &data->o_res.seq_res, |
2414 | task) != 0) | |
2415 | nfs_release_seqid(data->o_arg.seqid); | |
549b19cc TM |
2416 | |
2417 | /* Set the create mode (note dependency on the session type) */ | |
2418 | data->o_arg.createmode = NFS4_CREATE_UNCHECKED; | |
2419 | if (data->o_arg.open_flags & O_EXCL) { | |
2420 | data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE; | |
2421 | if (nfs4_has_persistent_session(clp)) | |
2422 | data->o_arg.createmode = NFS4_CREATE_GUARDED; | |
2423 | else if (clp->cl_mvops->minor_version > 0) | |
2424 | data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1; | |
2425 | } | |
6ee41268 | 2426 | return; |
652f89f6 | 2427 | unlock_no_action: |
9759b0fb | 2428 | trace_nfs4_cached_open(data->state); |
652f89f6 | 2429 | rcu_read_unlock(); |
6ee41268 TM |
2430 | out_no_action: |
2431 | task->tk_action = NULL; | |
c8da19b9 | 2432 | out_wait: |
b75ad4cd | 2433 | nfs4_sequence_done(task, &data->o_res.seq_res); |
b257957e AB |
2434 | } |
2435 | ||
24ac23ab TM |
2436 | static void nfs4_open_done(struct rpc_task *task, void *calldata) |
2437 | { | |
2438 | struct nfs4_opendata *data = calldata; | |
1da177e4 | 2439 | |
24ac23ab | 2440 | data->rpc_status = task->tk_status; |
d898528c | 2441 | |
2e80dbe7 | 2442 | if (!nfs4_sequence_process(task, &data->o_res.seq_res)) |
14516c3a | 2443 | return; |
d898528c | 2444 | |
24ac23ab | 2445 | if (task->tk_status == 0) { |
807d66d8 TM |
2446 | if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) { |
2447 | switch (data->o_res.f_attr->mode & S_IFMT) { | |
6f926b5b TM |
2448 | case S_IFREG: |
2449 | break; | |
2450 | case S_IFLNK: | |
24ac23ab | 2451 | data->rpc_status = -ELOOP; |
6f926b5b TM |
2452 | break; |
2453 | case S_IFDIR: | |
24ac23ab | 2454 | data->rpc_status = -EISDIR; |
6f926b5b TM |
2455 | break; |
2456 | default: | |
24ac23ab | 2457 | data->rpc_status = -ENOTDIR; |
807d66d8 | 2458 | } |
6f926b5b | 2459 | } |
26e976a8 | 2460 | renew_lease(data->o_res.server, data->timestamp); |
0f9f95e0 TM |
2461 | if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)) |
2462 | nfs_confirm_seqid(&data->owner->so_seqid, 0); | |
6f926b5b | 2463 | } |
a7a3b1e9 | 2464 | data->rpc_done = true; |
24ac23ab | 2465 | } |
6f926b5b | 2466 | |
24ac23ab TM |
2467 | static void nfs4_open_release(void *calldata) |
2468 | { | |
2469 | struct nfs4_opendata *data = calldata; | |
2470 | struct nfs4_state *state = NULL; | |
2471 | ||
2472 | /* If this request hasn't been cancelled, do nothing */ | |
a7a3b1e9 | 2473 | if (!data->cancelled) |
24ac23ab TM |
2474 | goto out_free; |
2475 | /* In case of error, no cleanup! */ | |
3e309914 | 2476 | if (data->rpc_status != 0 || !data->rpc_done) |
24ac23ab TM |
2477 | goto out_free; |
2478 | /* In case we need an open_confirm, no cleanup! */ | |
2479 | if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) | |
2480 | goto out_free; | |
24ac23ab | 2481 | state = nfs4_opendata_to_nfs4_state(data); |
1b370bc2 | 2482 | if (!IS_ERR(state)) |
643168c2 | 2483 | nfs4_close_state(state, data->o_arg.fmode); |
24ac23ab | 2484 | out_free: |
c6d00e63 | 2485 | nfs4_opendata_put(data); |
24ac23ab TM |
2486 | } |
2487 | ||
2488 | static const struct rpc_call_ops nfs4_open_ops = { | |
2489 | .rpc_call_prepare = nfs4_open_prepare, | |
2490 | .rpc_call_done = nfs4_open_done, | |
2491 | .rpc_release = nfs4_open_release, | |
2492 | }; | |
2493 | ||
3b65a30d FI |
2494 | static int nfs4_run_open_task(struct nfs4_opendata *data, |
2495 | struct nfs_open_context *ctx) | |
24ac23ab | 2496 | { |
2b0143b5 | 2497 | struct inode *dir = d_inode(data->dir); |
24ac23ab TM |
2498 | struct nfs_server *server = NFS_SERVER(dir); |
2499 | struct nfs_openargs *o_arg = &data->o_arg; | |
2500 | struct nfs_openres *o_res = &data->o_res; | |
2501 | struct rpc_task *task; | |
5138fde0 TM |
2502 | struct rpc_message msg = { |
2503 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN], | |
2504 | .rpc_argp = o_arg, | |
2505 | .rpc_resp = o_res, | |
2506 | .rpc_cred = data->owner->so_cred, | |
2507 | }; | |
c970aa85 TM |
2508 | struct rpc_task_setup task_setup_data = { |
2509 | .rpc_client = server->client, | |
5138fde0 | 2510 | .rpc_message = &msg, |
c970aa85 TM |
2511 | .callback_ops = &nfs4_open_ops, |
2512 | .callback_data = data, | |
101070ca | 2513 | .workqueue = nfsiod_workqueue, |
61296507 | 2514 | .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, |
c970aa85 | 2515 | }; |
24ac23ab TM |
2516 | int status; |
2517 | ||
c6d00e63 | 2518 | kref_get(&data->kref); |
a7a3b1e9 | 2519 | data->rpc_done = false; |
3e309914 | 2520 | data->rpc_status = 0; |
a7a3b1e9 BC |
2521 | data->cancelled = false; |
2522 | data->is_recover = false; | |
3b65a30d FI |
2523 | if (!ctx) { |
2524 | nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1); | |
a7a3b1e9 | 2525 | data->is_recover = true; |
67e7b52d | 2526 | task_setup_data.flags |= RPC_TASK_TIMEOUT; |
2409a976 | 2527 | } else { |
3b65a30d | 2528 | nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0); |
2409a976 FI |
2529 | pnfs_lgopen_prepare(data, ctx); |
2530 | } | |
c970aa85 | 2531 | task = rpc_run_task(&task_setup_data); |
d9b67e1e AS |
2532 | if (IS_ERR(task)) |
2533 | return PTR_ERR(task); | |
820bf85c | 2534 | status = rpc_wait_for_completion_task(task); |
d9b67e1e | 2535 | if (status != 0) { |
a7a3b1e9 | 2536 | data->cancelled = true; |
d9b67e1e AS |
2537 | smp_wmb(); |
2538 | } else | |
2539 | status = data->rpc_status; | |
2540 | rpc_put_task(task); | |
b257957e AB |
2541 | |
2542 | return status; | |
2543 | } | |
2544 | ||
2545 | static int _nfs4_recover_proc_open(struct nfs4_opendata *data) | |
2546 | { | |
2b0143b5 | 2547 | struct inode *dir = d_inode(data->dir); |
b257957e | 2548 | struct nfs_openres *o_res = &data->o_res; |
d9b67e1e | 2549 | int status; |
b257957e | 2550 | |
3b65a30d | 2551 | status = nfs4_run_open_task(data, NULL); |
b257957e AB |
2552 | if (status != 0 || !data->rpc_done) |
2553 | return status; | |
2554 | ||
6926afd1 TM |
2555 | nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr); |
2556 | ||
d7e98258 | 2557 | if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) |
b257957e | 2558 | status = _nfs4_proc_open_confirm(data); |
b257957e AB |
2559 | |
2560 | return status; | |
2561 | } | |
2562 | ||
f3792d63 TM |
2563 | /* |
2564 | * Additional permission checks in order to distinguish between an | |
2565 | * open for read, and an open for execute. This works around the | |
2566 | * fact that NFSv4 OPEN treats read and execute permissions as being | |
2567 | * the same. | |
2568 | * Note that in the non-execute case, we want to turn off permission | |
2569 | * checking if we just created a new file (POSIX open() semantics). | |
2570 | */ | |
a52458b4 | 2571 | static int nfs4_opendata_access(const struct cred *cred, |
6168f62c | 2572 | struct nfs4_opendata *opendata, |
f8d9a897 WAA |
2573 | struct nfs4_state *state, fmode_t fmode, |
2574 | int openflags) | |
6168f62c WAA |
2575 | { |
2576 | struct nfs_access_entry cache; | |
1e6f2095 | 2577 | u32 mask, flags; |
6168f62c WAA |
2578 | |
2579 | /* access call failed or for some reason the server doesn't | |
2580 | * support any access modes -- defer access call until later */ | |
2581 | if (opendata->o_res.access_supported == 0) | |
2582 | return 0; | |
2583 | ||
2584 | mask = 0; | |
f3792d63 TM |
2585 | /* |
2586 | * Use openflags to check for exec, because fmode won't | |
2587 | * always have FMODE_EXEC set when file open for exec. | |
2588 | */ | |
f8d9a897 WAA |
2589 | if (openflags & __FMODE_EXEC) { |
2590 | /* ONLY check for exec rights */ | |
1e6f2095 AS |
2591 | if (S_ISDIR(state->inode->i_mode)) |
2592 | mask = NFS4_ACCESS_LOOKUP; | |
2593 | else | |
2594 | mask = NFS4_ACCESS_EXECUTE; | |
f3792d63 | 2595 | } else if ((fmode & FMODE_READ) && !opendata->file_created) |
1e6f2095 | 2596 | mask = NFS4_ACCESS_READ; |
6168f62c WAA |
2597 | |
2598 | cache.cred = cred; | |
6168f62c WAA |
2599 | nfs_access_set_mask(&cache, opendata->o_res.access_result); |
2600 | nfs_access_add_cache(state->inode, &cache); | |
2601 | ||
1e6f2095 AS |
2602 | flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP; |
2603 | if ((mask & ~cache.mask & flags) == 0) | |
6168f62c WAA |
2604 | return 0; |
2605 | ||
998f40b5 | 2606 | return -EACCES; |
6168f62c WAA |
2607 | } |
2608 | ||
b257957e AB |
2609 | /* |
2610 | * Note: On error, nfs4_proc_open will free the struct nfs4_opendata | |
2611 | */ | |
3b65a30d FI |
2612 | static int _nfs4_proc_open(struct nfs4_opendata *data, |
2613 | struct nfs_open_context *ctx) | |
b257957e | 2614 | { |
2b0143b5 | 2615 | struct inode *dir = d_inode(data->dir); |
b257957e AB |
2616 | struct nfs_server *server = NFS_SERVER(dir); |
2617 | struct nfs_openargs *o_arg = &data->o_arg; | |
2618 | struct nfs_openres *o_res = &data->o_res; | |
2619 | int status; | |
2620 | ||
3b65a30d | 2621 | status = nfs4_run_open_task(data, ctx); |
08ef7bd3 TM |
2622 | if (!data->rpc_done) |
2623 | return status; | |
2624 | if (status != 0) { | |
2625 | if (status == -NFS4ERR_BADNAME && | |
2626 | !(o_arg->open_flags & O_CREAT)) | |
2627 | return -ENOENT; | |
24ac23ab | 2628 | return status; |
08ef7bd3 | 2629 | } |
24ac23ab | 2630 | |
6926afd1 TM |
2631 | nfs_fattr_map_and_free_names(server, &data->f_attr); |
2632 | ||
5bc2afc2 | 2633 | if (o_arg->open_flags & O_CREAT) { |
5bc2afc2 | 2634 | if (o_arg->open_flags & O_EXCL) |
a7a3b1e9 | 2635 | data->file_created = true; |
5bc2afc2 | 2636 | else if (o_res->cinfo.before != o_res->cinfo.after) |
a7a3b1e9 | 2637 | data->file_created = true; |
1eb5d98f JL |
2638 | if (data->file_created || |
2639 | inode_peek_iversion_raw(dir) != o_res->cinfo.after) | |
d3129ef6 | 2640 | update_changeattr(dir, &o_res->cinfo, |
5636ec4e | 2641 | o_res->f_attr->time_start, 0); |
5bc2afc2 | 2642 | } |
0df5dd4a TM |
2643 | if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0) |
2644 | server->caps &= ~NFS_CAP_POSIX_LOCK; | |
1da177e4 | 2645 | if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { |
cdd4e68b | 2646 | status = _nfs4_proc_open_confirm(data); |
1da177e4 | 2647 | if (status != 0) |
24ac23ab | 2648 | return status; |
1da177e4 | 2649 | } |
56e0d71e TM |
2650 | if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) { |
2651 | nfs4_sequence_free_slot(&o_res->seq_res); | |
a841b54d TM |
2652 | nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, |
2653 | o_res->f_label, NULL); | |
56e0d71e | 2654 | } |
24ac23ab | 2655 | return 0; |
1da177e4 LT |
2656 | } |
2657 | ||
1da177e4 LT |
2658 | /* |
2659 | * OPEN_EXPIRED: | |
2660 | * reclaim state on the server after a network partition. | |
2661 | * Assumes caller holds the appropriate lock | |
2662 | */ | |
539cd03a | 2663 | static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) |
1da177e4 | 2664 | { |
e56e0b78 | 2665 | struct nfs4_opendata *opendata; |
864472e9 | 2666 | int ret; |
1da177e4 | 2667 | |
4a1c0893 | 2668 | opendata = nfs4_open_recoverdata_alloc(ctx, state, |
49f9a0fa | 2669 | NFS4_OPEN_CLAIM_FH); |
6f220ed5 TM |
2670 | if (IS_ERR(opendata)) |
2671 | return PTR_ERR(opendata); | |
864472e9 | 2672 | ret = nfs4_open_recover(opendata, state); |
35d05778 | 2673 | if (ret == -ESTALE) |
3d4ff43d | 2674 | d_drop(ctx->dentry); |
c6d00e63 | 2675 | nfs4_opendata_put(opendata); |
864472e9 | 2676 | return ret; |
1da177e4 LT |
2677 | } |
2678 | ||
a9ed2e25 | 2679 | static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) |
202b50dc | 2680 | { |
539cd03a | 2681 | struct nfs_server *server = NFS_SERVER(state->inode); |
202b50dc TM |
2682 | struct nfs4_exception exception = { }; |
2683 | int err; | |
2684 | ||
2685 | do { | |
539cd03a | 2686 | err = _nfs4_open_expired(ctx, state); |
42113a75 | 2687 | trace_nfs4_open_expired(ctx, 0, err); |
49f9a0fa TM |
2688 | if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) |
2689 | continue; | |
a9ed2e25 TM |
2690 | switch (err) { |
2691 | default: | |
2692 | goto out; | |
2693 | case -NFS4ERR_GRACE: | |
2694 | case -NFS4ERR_DELAY: | |
2695 | nfs4_handle_exception(server, err, &exception); | |
2696 | err = 0; | |
2697 | } | |
202b50dc | 2698 | } while (exception.retry); |
a9ed2e25 | 2699 | out: |
202b50dc TM |
2700 | return err; |
2701 | } | |
2702 | ||
1da177e4 LT |
2703 | static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) |
2704 | { | |
1da177e4 | 2705 | struct nfs_open_context *ctx; |
864472e9 | 2706 | int ret; |
1da177e4 | 2707 | |
864472e9 TM |
2708 | ctx = nfs4_state_find_open_context(state); |
2709 | if (IS_ERR(ctx)) | |
91876b13 | 2710 | return -EAGAIN; |
539cd03a | 2711 | ret = nfs4_do_open_expired(ctx, state); |
864472e9 TM |
2712 | put_nfs_open_context(ctx); |
2713 | return ret; | |
1da177e4 LT |
2714 | } |
2715 | ||
41020b67 TM |
2716 | static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state, |
2717 | const nfs4_stateid *stateid) | |
4dfd4f7a | 2718 | { |
41020b67 | 2719 | nfs_remove_bad_delegation(state->inode, stateid); |
9f0c5124 | 2720 | nfs_state_clear_delegation(state); |
4dfd4f7a TM |
2721 | } |
2722 | ||
2723 | static void nfs40_clear_delegation_stateid(struct nfs4_state *state) | |
2724 | { | |
2725 | if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL) | |
41020b67 | 2726 | nfs_finish_clear_delegation_stateid(state, NULL); |
4dfd4f7a TM |
2727 | } |
2728 | ||
2729 | static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) | |
2730 | { | |
2731 | /* NFSv4.0 doesn't allow for delegation recovery on open expire */ | |
2732 | nfs40_clear_delegation_stateid(state); | |
27a30cf6 | 2733 | nfs_state_clear_open_state_flags(state); |
4dfd4f7a TM |
2734 | return nfs4_open_expired(sp, state); |
2735 | } | |
2736 | ||
45870d69 TM |
2737 | static int nfs40_test_and_free_expired_stateid(struct nfs_server *server, |
2738 | nfs4_stateid *stateid, | |
a52458b4 | 2739 | const struct cred *cred) |
45870d69 TM |
2740 | { |
2741 | return -NFS4ERR_BAD_STATEID; | |
2742 | } | |
2743 | ||
f062eb6c | 2744 | #if defined(CONFIG_NFS_V4_1) |
4586f6e2 TM |
2745 | static int nfs41_test_and_free_expired_stateid(struct nfs_server *server, |
2746 | nfs4_stateid *stateid, | |
a52458b4 | 2747 | const struct cred *cred) |
4586f6e2 TM |
2748 | { |
2749 | int status; | |
2750 | ||
f7a62ada TM |
2751 | switch (stateid->type) { |
2752 | default: | |
2753 | break; | |
2754 | case NFS4_INVALID_STATEID_TYPE: | |
2755 | case NFS4_SPECIAL_STATEID_TYPE: | |
2756 | return -NFS4ERR_BAD_STATEID; | |
2757 | case NFS4_REVOKED_STATEID_TYPE: | |
2758 | goto out_free; | |
2759 | } | |
4586f6e2 | 2760 | |
f7a62ada | 2761 | status = nfs41_test_stateid(server, stateid, cred); |
4586f6e2 TM |
2762 | switch (status) { |
2763 | case -NFS4ERR_EXPIRED: | |
2764 | case -NFS4ERR_ADMIN_REVOKED: | |
2765 | case -NFS4ERR_DELEG_REVOKED: | |
f7a62ada TM |
2766 | break; |
2767 | default: | |
4586f6e2 TM |
2768 | return status; |
2769 | } | |
f7a62ada TM |
2770 | out_free: |
2771 | /* Ack the revoked state to the server */ | |
f0b0bf88 | 2772 | nfs41_free_stateid(server, stateid, cred, true); |
f7a62ada | 2773 | return -NFS4ERR_EXPIRED; |
4586f6e2 TM |
2774 | } |
2775 | ||
27a30cf6 | 2776 | static int nfs41_check_delegation_stateid(struct nfs4_state *state) |
f062eb6c | 2777 | { |
f062eb6c | 2778 | struct nfs_server *server = NFS_SERVER(state->inode); |
0c116cad | 2779 | nfs4_stateid stateid; |
ab7cb0df | 2780 | struct nfs_delegation *delegation; |
a52458b4 | 2781 | const struct cred *cred = NULL; |
27a30cf6 | 2782 | int status, ret = NFS_OK; |
3e60ffdd | 2783 | |
ab7cb0df TM |
2784 | /* Get the delegation credential for use by test/free_stateid */ |
2785 | rcu_read_lock(); | |
2786 | delegation = rcu_dereference(NFS_I(state->inode)->delegation); | |
0c116cad | 2787 | if (delegation == NULL) { |
ab7cb0df | 2788 | rcu_read_unlock(); |
9f0c5124 | 2789 | nfs_state_clear_delegation(state); |
27a30cf6 | 2790 | return NFS_OK; |
0c116cad TM |
2791 | } |
2792 | ||
fc51b1cf | 2793 | spin_lock(&delegation->lock); |
0c116cad | 2794 | nfs4_stateid_copy(&stateid, &delegation->stateid); |
0c116cad | 2795 | |
994b15b9 TM |
2796 | if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, |
2797 | &delegation->flags)) { | |
fc51b1cf | 2798 | spin_unlock(&delegation->lock); |
4c8e5447 | 2799 | rcu_read_unlock(); |
27a30cf6 | 2800 | return NFS_OK; |
4c8e5447 | 2801 | } |
0c116cad | 2802 | |
a52458b4 N |
2803 | if (delegation->cred) |
2804 | cred = get_cred(delegation->cred); | |
fc51b1cf | 2805 | spin_unlock(&delegation->lock); |
0c116cad | 2806 | rcu_read_unlock(); |
4586f6e2 | 2807 | status = nfs41_test_and_free_expired_stateid(server, &stateid, cred); |
0c116cad | 2808 | trace_nfs4_test_delegation_stateid(state, NULL, status); |
f7a62ada | 2809 | if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) |
41020b67 | 2810 | nfs_finish_clear_delegation_stateid(state, &stateid); |
27a30cf6 TM |
2811 | else |
2812 | ret = status; | |
ab7cb0df | 2813 | |
8c39a39e | 2814 | put_cred(cred); |
27a30cf6 TM |
2815 | return ret; |
2816 | } | |
2817 | ||
2818 | static void nfs41_delegation_recover_stateid(struct nfs4_state *state) | |
2819 | { | |
2820 | nfs4_stateid tmp; | |
2821 | ||
2822 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) && | |
2823 | nfs4_copy_delegation_stateid(state->inode, state->state, | |
2824 | &tmp, NULL) && | |
2825 | nfs4_stateid_match_other(&state->stateid, &tmp)) | |
2826 | nfs_state_set_delegation(state, &tmp, state->state); | |
2827 | else | |
2828 | nfs_state_clear_delegation(state); | |
3e60ffdd CL |
2829 | } |
2830 | ||
c5896fc8 TM |
2831 | /** |
2832 | * nfs41_check_expired_locks - possibly free a lock stateid | |
2833 | * | |
2834 | * @state: NFSv4 state for an inode | |
2835 | * | |
2836 | * Returns NFS_OK if recovery for this stateid is now finished. | |
2837 | * Otherwise a negative NFS4ERR value is returned. | |
2838 | */ | |
2839 | static int nfs41_check_expired_locks(struct nfs4_state *state) | |
2840 | { | |
2841 | int status, ret = NFS_OK; | |
d75a6a0e | 2842 | struct nfs4_lock_state *lsp, *prev = NULL; |
c5896fc8 TM |
2843 | struct nfs_server *server = NFS_SERVER(state->inode); |
2844 | ||
2845 | if (!test_bit(LK_STATE_IN_USE, &state->flags)) | |
2846 | goto out; | |
d75a6a0e BC |
2847 | |
2848 | spin_lock(&state->state_lock); | |
c5896fc8 TM |
2849 | list_for_each_entry(lsp, &state->lock_states, ls_locks) { |
2850 | if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) { | |
a52458b4 | 2851 | const struct cred *cred = lsp->ls_state->owner->so_cred; |
c5896fc8 | 2852 | |
194bc1f4 | 2853 | refcount_inc(&lsp->ls_count); |
d75a6a0e BC |
2854 | spin_unlock(&state->state_lock); |
2855 | ||
2856 | nfs4_put_lock_state(prev); | |
2857 | prev = lsp; | |
2858 | ||
c5896fc8 TM |
2859 | status = nfs41_test_and_free_expired_stateid(server, |
2860 | &lsp->ls_stateid, | |
2861 | cred); | |
2862 | trace_nfs4_test_lock_stateid(state, lsp, status); | |
2863 | if (status == -NFS4ERR_EXPIRED || | |
2864 | status == -NFS4ERR_BAD_STATEID) { | |
2865 | clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags); | |
67dd4830 | 2866 | lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE; |
c5896fc8 TM |
2867 | if (!recover_lost_locks) |
2868 | set_bit(NFS_LOCK_LOST, &lsp->ls_flags); | |
2869 | } else if (status != NFS_OK) { | |
2870 | ret = status; | |
d75a6a0e BC |
2871 | nfs4_put_lock_state(prev); |
2872 | goto out; | |
c5896fc8 | 2873 | } |
d75a6a0e | 2874 | spin_lock(&state->state_lock); |
c5896fc8 | 2875 | } |
d75a6a0e BC |
2876 | } |
2877 | spin_unlock(&state->state_lock); | |
2878 | nfs4_put_lock_state(prev); | |
c5896fc8 TM |
2879 | out: |
2880 | return ret; | |
2881 | } | |
2882 | ||
3e60ffdd CL |
2883 | /** |
2884 | * nfs41_check_open_stateid - possibly free an open stateid | |
2885 | * | |
2886 | * @state: NFSv4 state for an inode | |
2887 | * | |
2888 | * Returns NFS_OK if recovery for this stateid is now finished. | |
2889 | * Otherwise a negative NFS4ERR value is returned. | |
2890 | */ | |
2891 | static int nfs41_check_open_stateid(struct nfs4_state *state) | |
2892 | { | |
2893 | struct nfs_server *server = NFS_SERVER(state->inode); | |
fcb6d9c6 | 2894 | nfs4_stateid *stateid = &state->open_stateid; |
a52458b4 | 2895 | const struct cred *cred = state->owner->so_cred; |
3e60ffdd CL |
2896 | int status; |
2897 | ||
27a30cf6 | 2898 | if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) |
3e60ffdd | 2899 | return -NFS4ERR_BAD_STATEID; |
4586f6e2 | 2900 | status = nfs41_test_and_free_expired_stateid(server, stateid, cred); |
08cb47fa | 2901 | trace_nfs4_test_open_stateid(state, NULL, status); |
f7a62ada | 2902 | if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) { |
27a30cf6 | 2903 | nfs_state_clear_open_state_flags(state); |
67dd4830 | 2904 | stateid->type = NFS4_INVALID_STATEID_TYPE; |
8a64c4ef | 2905 | return status; |
c0ca0e59 | 2906 | } |
8a64c4ef TM |
2907 | if (nfs_open_stateid_recover_openmode(state)) |
2908 | return -NFS4ERR_OPENMODE; | |
2909 | return NFS_OK; | |
b01dd1d8 BS |
2910 | } |
2911 | ||
2912 | static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) | |
2913 | { | |
eb64cf96 | 2914 | int status; |
b01dd1d8 | 2915 | |
27a30cf6 TM |
2916 | status = nfs41_check_delegation_stateid(state); |
2917 | if (status != NFS_OK) | |
2918 | return status; | |
2919 | nfs41_delegation_recover_stateid(state); | |
2920 | ||
c5896fc8 TM |
2921 | status = nfs41_check_expired_locks(state); |
2922 | if (status != NFS_OK) | |
2923 | return status; | |
3e60ffdd | 2924 | status = nfs41_check_open_stateid(state); |
eb64cf96 CL |
2925 | if (status != NFS_OK) |
2926 | status = nfs4_open_expired(sp, state); | |
2927 | return status; | |
f062eb6c BS |
2928 | } |
2929 | #endif | |
2930 | ||
aa53ed54 JL |
2931 | /* |
2932 | * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-* | |
2933 | * fields corresponding to attributes that were used to store the verifier. | |
2934 | * Make sure we clobber those fields in the later setattr call | |
2935 | */ | |
609339c1 | 2936 | static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata, |
5334c5bd | 2937 | struct iattr *sattr, struct nfs4_label **label) |
aa53ed54 | 2938 | { |
609339c1 TM |
2939 | const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask; |
2940 | __u32 attrset[3]; | |
2941 | unsigned ret; | |
2942 | unsigned i; | |
5334c5bd | 2943 | |
609339c1 TM |
2944 | for (i = 0; i < ARRAY_SIZE(attrset); i++) { |
2945 | attrset[i] = opendata->o_res.attrset[i]; | |
2946 | if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1) | |
2947 | attrset[i] &= ~bitmask[i]; | |
2948 | } | |
2949 | ||
2950 | ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ? | |
2951 | sattr->ia_valid : 0; | |
aa53ed54 | 2952 | |
609339c1 TM |
2953 | if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) { |
2954 | if (sattr->ia_valid & ATTR_ATIME_SET) | |
2955 | ret |= ATTR_ATIME_SET; | |
2956 | else | |
2957 | ret |= ATTR_ATIME; | |
2958 | } | |
5334c5bd | 2959 | |
609339c1 TM |
2960 | if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) { |
2961 | if (sattr->ia_valid & ATTR_MTIME_SET) | |
2962 | ret |= ATTR_MTIME_SET; | |
2963 | else | |
2964 | ret |= ATTR_MTIME; | |
2965 | } | |
5334c5bd | 2966 | |
609339c1 | 2967 | if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL)) |
5334c5bd | 2968 | *label = NULL; |
609339c1 | 2969 | return ret; |
aa53ed54 JL |
2970 | } |
2971 | ||
c21443c2 | 2972 | static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata, |
1bf85d8c | 2973 | int flags, struct nfs_open_context *ctx) |
c21443c2 TM |
2974 | { |
2975 | struct nfs4_state_owner *sp = opendata->owner; | |
2976 | struct nfs_server *server = sp->so_server; | |
275bb307 | 2977 | struct dentry *dentry; |
c21443c2 | 2978 | struct nfs4_state *state; |
1bf85d8c | 2979 | fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx); |
cf5b4059 TM |
2980 | struct inode *dir = d_inode(opendata->dir); |
2981 | unsigned long dir_verifier; | |
c21443c2 TM |
2982 | unsigned int seq; |
2983 | int ret; | |
2984 | ||
2985 | seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); | |
cf5b4059 | 2986 | dir_verifier = nfs_save_change_attribute(dir); |
c21443c2 | 2987 | |
3b65a30d | 2988 | ret = _nfs4_proc_open(opendata, ctx); |
dca78001 | 2989 | if (ret != 0) |
c21443c2 TM |
2990 | goto out; |
2991 | ||
ae55e59d | 2992 | state = _nfs4_opendata_to_nfs4_state(opendata); |
c21443c2 TM |
2993 | ret = PTR_ERR(state); |
2994 | if (IS_ERR(state)) | |
2995 | goto out; | |
a974deee | 2996 | ctx->state = state; |
c21443c2 TM |
2997 | if (server->caps & NFS_CAP_POSIX_LOCK) |
2998 | set_bit(NFS_STATE_POSIX_LOCKS, &state->flags); | |
a8ce377a JL |
2999 | if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK) |
3000 | set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags); | |
c21443c2 | 3001 | |
275bb307 | 3002 | dentry = opendata->dentry; |
2b0143b5 | 3003 | if (d_really_is_negative(dentry)) { |
668d0cd5 | 3004 | struct dentry *alias; |
275bb307 | 3005 | d_drop(dentry); |
668d0cd5 AV |
3006 | alias = d_exact_alias(dentry, state->inode); |
3007 | if (!alias) | |
3008 | alias = d_splice_alias(igrab(state->inode), dentry); | |
3009 | /* d_splice_alias() can't fail here - it's a non-directory */ | |
3010 | if (alias) { | |
275bb307 | 3011 | dput(ctx->dentry); |
668d0cd5 | 3012 | ctx->dentry = dentry = alias; |
275bb307 | 3013 | } |
cf5b4059 TM |
3014 | } |
3015 | ||
3016 | switch(opendata->o_arg.claim) { | |
3017 | default: | |
3018 | break; | |
3019 | case NFS4_OPEN_CLAIM_NULL: | |
3020 | case NFS4_OPEN_CLAIM_DELEGATE_CUR: | |
3021 | case NFS4_OPEN_CLAIM_DELEGATE_PREV: | |
3022 | if (!opendata->rpc_done) | |
3023 | break; | |
3024 | if (opendata->o_res.delegation_type != 0) | |
3025 | dir_verifier = nfs_save_change_attribute(dir); | |
3026 | nfs_set_verifier(dentry, dir_verifier); | |
275bb307 TM |
3027 | } |
3028 | ||
af9b6d75 TM |
3029 | /* Parse layoutget results before we check for access */ |
3030 | pnfs_parse_lgopen(state->inode, opendata->lgp, ctx); | |
3031 | ||
1bf85d8c TM |
3032 | ret = nfs4_opendata_access(sp->so_cred, opendata, state, |
3033 | acc_mode, flags); | |
c21443c2 TM |
3034 | if (ret != 0) |
3035 | goto out; | |
3036 | ||
2b0143b5 | 3037 | if (d_inode(dentry) == state->inode) { |
c45ffdd2 TM |
3038 | nfs_inode_attach_open_context(ctx); |
3039 | if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) | |
3040 | nfs4_schedule_stateid_recovery(server, state); | |
3041 | } | |
2409a976 | 3042 | |
c21443c2 | 3043 | out: |
0cb98abb OK |
3044 | if (!opendata->cancelled) |
3045 | nfs4_sequence_free_slot(&opendata->o_res.seq_res); | |
c21443c2 TM |
3046 | return ret; |
3047 | } | |
3048 | ||
1da177e4 | 3049 | /* |
24ac23ab | 3050 | * Returns a referenced nfs4_state |
1da177e4 | 3051 | */ |
82be417a | 3052 | static int _nfs4_do_open(struct inode *dir, |
4197a055 | 3053 | struct nfs_open_context *ctx, |
82be417a | 3054 | int flags, |
8fd1ab74 | 3055 | const struct nfs4_open_createattrs *c, |
5bc2afc2 | 3056 | int *opened) |
1da177e4 LT |
3057 | { |
3058 | struct nfs4_state_owner *sp; | |
3059 | struct nfs4_state *state = NULL; | |
3060 | struct nfs_server *server = NFS_SERVER(dir); | |
e56e0b78 | 3061 | struct nfs4_opendata *opendata; |
4197a055 | 3062 | struct dentry *dentry = ctx->dentry; |
a52458b4 | 3063 | const struct cred *cred = ctx->cred; |
4197a055 | 3064 | struct nfs4_threshold **ctx_th = &ctx->mdsthreshold; |
1bf85d8c | 3065 | fmode_t fmode = _nfs4_ctx_to_openmode(ctx); |
49f9a0fa | 3066 | enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL; |
8fd1ab74 TM |
3067 | struct iattr *sattr = c->sattr; |
3068 | struct nfs4_label *label = c->label; | |
1775fd3e | 3069 | struct nfs4_label *olabel = NULL; |
aac00a8d | 3070 | int status; |
1da177e4 LT |
3071 | |
3072 | /* Protect against reboot recovery conflicts */ | |
1da177e4 | 3073 | status = -ENOMEM; |
d1e284d5 TM |
3074 | sp = nfs4_get_state_owner(server, cred, GFP_KERNEL); |
3075 | if (sp == NULL) { | |
1da177e4 LT |
3076 | dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n"); |
3077 | goto out_err; | |
3078 | } | |
334f87dd | 3079 | status = nfs4_client_recover_expired_lease(server->nfs_client); |
58d9714a | 3080 | if (status != 0) |
b4454fe1 | 3081 | goto err_put_state_owner; |
2b0143b5 DH |
3082 | if (d_really_is_positive(dentry)) |
3083 | nfs4_return_incompatible_delegation(d_inode(dentry), fmode); | |
58d9714a | 3084 | status = -ENOMEM; |
2b0143b5 | 3085 | if (d_really_is_positive(dentry)) |
49f9a0fa | 3086 | claim = NFS4_OPEN_CLAIM_FH; |
8fd1ab74 TM |
3087 | opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, |
3088 | c, claim, GFP_KERNEL); | |
e56e0b78 | 3089 | if (opendata == NULL) |
95d35cb4 | 3090 | goto err_put_state_owner; |
1da177e4 | 3091 | |
14c43f76 DQ |
3092 | if (label) { |
3093 | olabel = nfs4_label_alloc(server, GFP_KERNEL); | |
3094 | if (IS_ERR(olabel)) { | |
3095 | status = PTR_ERR(olabel); | |
3096 | goto err_opendata_put; | |
3097 | } | |
3098 | } | |
3099 | ||
e911b815 TM |
3100 | if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) { |
3101 | if (!opendata->f_attr.mdsthreshold) { | |
3102 | opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc(); | |
3103 | if (!opendata->f_attr.mdsthreshold) | |
3104 | goto err_free_label; | |
3105 | } | |
1549210f | 3106 | opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0]; |
82be417a | 3107 | } |
2b0143b5 DH |
3108 | if (d_really_is_positive(dentry)) |
3109 | opendata->state = nfs4_get_open_state(d_inode(dentry), sp); | |
aac00a8d | 3110 | |
1bf85d8c | 3111 | status = _nfs4_open_and_get_state(opendata, flags, ctx); |
6168f62c | 3112 | if (status != 0) |
14c43f76 | 3113 | goto err_free_label; |
3efb9722 | 3114 | state = ctx->state; |
6168f62c | 3115 | |
efcbc04e | 3116 | if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) && |
549b19cc | 3117 | (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) { |
609339c1 | 3118 | unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label); |
a1d1c4f1 TM |
3119 | /* |
3120 | * send create attributes which was not set by open | |
3121 | * with an extra setattr. | |
3122 | */ | |
609339c1 TM |
3123 | if (attrs || label) { |
3124 | unsigned ia_old = sattr->ia_valid; | |
3125 | ||
3126 | sattr->ia_valid = attrs; | |
a1d1c4f1 TM |
3127 | nfs_fattr_init(opendata->o_res.f_attr); |
3128 | status = nfs4_do_setattr(state->inode, cred, | |
3129 | opendata->o_res.f_attr, sattr, | |
29b59f94 | 3130 | ctx, label, olabel); |
a1d1c4f1 TM |
3131 | if (status == 0) { |
3132 | nfs_setattr_update_inode(state->inode, sattr, | |
3133 | opendata->o_res.f_attr); | |
3134 | nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel); | |
3135 | } | |
609339c1 | 3136 | sattr->ia_valid = ia_old; |
1775fd3e | 3137 | } |
0ab64e0e | 3138 | } |
c5c3fb5f | 3139 | if (opened && opendata->file_created) |
73a09dd9 | 3140 | *opened = 1; |
82be417a | 3141 | |
e911b815 | 3142 | if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) { |
82be417a | 3143 | *ctx_th = opendata->f_attr.mdsthreshold; |
e911b815 TM |
3144 | opendata->f_attr.mdsthreshold = NULL; |
3145 | } | |
82be417a | 3146 | |
14c43f76 DQ |
3147 | nfs4_label_free(olabel); |
3148 | ||
c6d00e63 | 3149 | nfs4_opendata_put(opendata); |
1da177e4 | 3150 | nfs4_put_state_owner(sp); |
1da177e4 | 3151 | return 0; |
14c43f76 DQ |
3152 | err_free_label: |
3153 | nfs4_label_free(olabel); | |
c6d00e63 TM |
3154 | err_opendata_put: |
3155 | nfs4_opendata_put(opendata); | |
e56e0b78 TM |
3156 | err_put_state_owner: |
3157 | nfs4_put_state_owner(sp); | |
1da177e4 | 3158 | out_err: |
1da177e4 LT |
3159 | return status; |
3160 | } | |
3161 | ||
3162 | ||
82be417a | 3163 | static struct nfs4_state *nfs4_do_open(struct inode *dir, |
4197a055 | 3164 | struct nfs_open_context *ctx, |
82be417a AA |
3165 | int flags, |
3166 | struct iattr *sattr, | |
5bc2afc2 TM |
3167 | struct nfs4_label *label, |
3168 | int *opened) | |
1da177e4 | 3169 | { |
49f9a0fa | 3170 | struct nfs_server *server = NFS_SERVER(dir); |
0688e64b TM |
3171 | struct nfs4_exception exception = { |
3172 | .interruptible = true, | |
3173 | }; | |
1da177e4 | 3174 | struct nfs4_state *res; |
8fd1ab74 TM |
3175 | struct nfs4_open_createattrs c = { |
3176 | .label = label, | |
3177 | .sattr = sattr, | |
3178 | .verf = { | |
3179 | [0] = (__u32)jiffies, | |
3180 | [1] = (__u32)current->pid, | |
3181 | }, | |
3182 | }; | |
1da177e4 LT |
3183 | int status; |
3184 | ||
3185 | do { | |
8fd1ab74 | 3186 | status = _nfs4_do_open(dir, ctx, flags, &c, opened); |
3efb9722 | 3187 | res = ctx->state; |
42113a75 | 3188 | trace_nfs4_open_file(ctx, flags, status); |
1da177e4 LT |
3189 | if (status == 0) |
3190 | break; | |
3191 | /* NOTE: BAD_SEQID means the server and client disagree about the | |
3192 | * book-keeping w.r.t. state-changing operations | |
3193 | * (OPEN/CLOSE/LOCK/LOCKU...) | |
3194 | * It is actually a sign of a bug on the client or on the server. | |
3195 | * | |
3196 | * If we receive a BAD_SEQID error in the particular case of | |
cee54fc9 | 3197 | * doing an OPEN, we assume that nfs_increment_open_seqid() will |
1da177e4 LT |
3198 | * have unhashed the old state_owner for us, and that we can |
3199 | * therefore safely retry using a new one. We should still warn | |
3200 | * the user though... | |
3201 | */ | |
3202 | if (status == -NFS4ERR_BAD_SEQID) { | |
9a3ba432 | 3203 | pr_warn_ratelimited("NFS: v4 server %s " |
6f43ddcc TM |
3204 | " returned a bad sequence-id error!\n", |
3205 | NFS_SERVER(dir)->nfs_client->cl_hostname); | |
1da177e4 LT |
3206 | exception.retry = 1; |
3207 | continue; | |
3208 | } | |
550f5747 TM |
3209 | /* |
3210 | * BAD_STATEID on OPEN means that the server cancelled our | |
3211 | * state before it received the OPEN_CONFIRM. | |
3212 | * Recover by retrying the request as per the discussion | |
3213 | * on Page 181 of RFC3530. | |
3214 | */ | |
3215 | if (status == -NFS4ERR_BAD_STATEID) { | |
3216 | exception.retry = 1; | |
3217 | continue; | |
3218 | } | |
924491f2 RM |
3219 | if (status == -NFS4ERR_EXPIRED) { |
3220 | nfs4_schedule_lease_recovery(server->nfs_client); | |
3221 | exception.retry = 1; | |
3222 | continue; | |
3223 | } | |
aac00a8d TM |
3224 | if (status == -EAGAIN) { |
3225 | /* We must have found a delegation */ | |
3226 | exception.retry = 1; | |
3227 | continue; | |
3228 | } | |
49f9a0fa TM |
3229 | if (nfs4_clear_cap_atomic_open_v1(server, status, &exception)) |
3230 | continue; | |
3231 | res = ERR_PTR(nfs4_handle_exception(server, | |
1da177e4 LT |
3232 | status, &exception)); |
3233 | } while (exception.retry); | |
3234 | return res; | |
3235 | } | |
3236 | ||
8487c479 TM |
3237 | static int _nfs4_do_setattr(struct inode *inode, |
3238 | struct nfs_setattrargs *arg, | |
3239 | struct nfs_setattrres *res, | |
a52458b4 | 3240 | const struct cred *cred, |
29b59f94 | 3241 | struct nfs_open_context *ctx) |
1da177e4 | 3242 | { |
3e4f6290 | 3243 | struct nfs_server *server = NFS_SERVER(inode); |
d9b67e1e | 3244 | struct rpc_message msg = { |
659bfcd6 | 3245 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], |
8487c479 TM |
3246 | .rpc_argp = arg, |
3247 | .rpc_resp = res, | |
659bfcd6 | 3248 | .rpc_cred = cred, |
d9b67e1e | 3249 | }; |
a52458b4 | 3250 | const struct cred *delegation_cred = NULL; |
26e976a8 | 3251 | unsigned long timestamp = jiffies; |
ee3ae84e | 3252 | bool truncate; |
65e4308d | 3253 | int status; |
1da177e4 | 3254 | |
8487c479 | 3255 | nfs_fattr_init(res->fattr); |
1da177e4 | 3256 | |
ee3ae84e | 3257 | /* Servers should only apply open mode checks for file size changes */ |
8487c479 | 3258 | truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false; |
991eedb1 TM |
3259 | if (!truncate) |
3260 | goto zero_stateid; | |
ee3ae84e | 3261 | |
991eedb1 | 3262 | if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) { |
ee3ae84e | 3263 | /* Use that stateid */ |
09a54f0e | 3264 | } else if (ctx != NULL && ctx->state) { |
17393475 | 3265 | struct nfs_lock_context *l_ctx; |
29b59f94 | 3266 | if (!nfs4_valid_open_stateid(ctx->state)) |
0418dae1 | 3267 | return -EBADF; |
17393475 N |
3268 | l_ctx = nfs_get_lock_context(ctx); |
3269 | if (IS_ERR(l_ctx)) | |
3270 | return PTR_ERR(l_ctx); | |
7a0566b3 N |
3271 | status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx, |
3272 | &arg->stateid, &delegation_cred); | |
3273 | nfs_put_lock_context(l_ctx); | |
3274 | if (status == -EIO) | |
0418dae1 | 3275 | return -EBADF; |
d826e5b8 OK |
3276 | else if (status == -EAGAIN) |
3277 | goto zero_stateid; | |
991eedb1 TM |
3278 | } else { |
3279 | zero_stateid: | |
8487c479 | 3280 | nfs4_stateid_copy(&arg->stateid, &zero_stateid); |
991eedb1 | 3281 | } |
abf4e13c TM |
3282 | if (delegation_cred) |
3283 | msg.rpc_cred = delegation_cred; | |
1da177e4 | 3284 | |
8487c479 | 3285 | status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1); |
abf4e13c | 3286 | |
a52458b4 | 3287 | put_cred(delegation_cred); |
29b59f94 | 3288 | if (status == 0 && ctx != NULL) |
26e976a8 | 3289 | renew_lease(server, timestamp); |
8487c479 | 3290 | trace_nfs4_setattr(inode, &arg->stateid, status); |
65e4308d | 3291 | return status; |
1da177e4 LT |
3292 | } |
3293 | ||
a52458b4 | 3294 | static int nfs4_do_setattr(struct inode *inode, const struct cred *cred, |
659bfcd6 | 3295 | struct nfs_fattr *fattr, struct iattr *sattr, |
29b59f94 | 3296 | struct nfs_open_context *ctx, struct nfs4_label *ilabel, |
1775fd3e | 3297 | struct nfs4_label *olabel) |
1da177e4 | 3298 | { |
3e4f6290 | 3299 | struct nfs_server *server = NFS_SERVER(inode); |
30846df0 | 3300 | __u32 bitmask[NFS4_BITMASK_SZ]; |
29b59f94 | 3301 | struct nfs4_state *state = ctx ? ctx->state : NULL; |
d9b67e1e AS |
3302 | struct nfs_setattrargs arg = { |
3303 | .fh = NFS_FH(inode), | |
3304 | .iap = sattr, | |
8487c479 | 3305 | .server = server, |
30846df0 | 3306 | .bitmask = bitmask, |
8487c479 | 3307 | .label = ilabel, |
d9b67e1e AS |
3308 | }; |
3309 | struct nfs_setattrres res = { | |
8487c479 TM |
3310 | .fattr = fattr, |
3311 | .label = olabel, | |
3312 | .server = server, | |
d9b67e1e | 3313 | }; |
a1d0b5ee TM |
3314 | struct nfs4_exception exception = { |
3315 | .state = state, | |
3114ea7a | 3316 | .inode = inode, |
8487c479 | 3317 | .stateid = &arg.stateid, |
a1d0b5ee | 3318 | }; |
1da177e4 | 3319 | int err; |
8487c479 | 3320 | |
1da177e4 | 3321 | do { |
30846df0 TM |
3322 | nfs4_bitmap_copy_adjust_setattr(bitmask, |
3323 | nfs4_bitmask(server, olabel), | |
3324 | inode); | |
3325 | ||
29b59f94 | 3326 | err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx); |
451146be TM |
3327 | switch (err) { |
3328 | case -NFS4ERR_OPENMODE: | |
721ccfb7 TM |
3329 | if (!(sattr->ia_valid & ATTR_SIZE)) { |
3330 | pr_warn_once("NFSv4: server %s is incorrectly " | |
3331 | "applying open mode checks to " | |
3332 | "a SETATTR that is not " | |
3333 | "changing file size.\n", | |
3334 | server->nfs_client->cl_hostname); | |
3335 | } | |
451146be TM |
3336 | if (state && !(state->state & FMODE_WRITE)) { |
3337 | err = -EBADF; | |
3338 | if (sattr->ia_valid & ATTR_OPEN) | |
3339 | err = -EACCES; | |
3340 | goto out; | |
3341 | } | |
3342 | } | |
3343 | err = nfs4_handle_exception(server, err, &exception); | |
1da177e4 | 3344 | } while (exception.retry); |
451146be | 3345 | out: |
1da177e4 LT |
3346 | return err; |
3347 | } | |
3348 | ||
500d701f PT |
3349 | static bool |
3350 | nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task) | |
3351 | { | |
3352 | if (inode == NULL || !nfs_have_layout(inode)) | |
3353 | return false; | |
3354 | ||
3355 | return pnfs_wait_on_layoutreturn(inode, task); | |
3356 | } | |
3357 | ||
0e0cb35b TM |
3358 | /* |
3359 | * Update the seqid of an open stateid | |
3360 | */ | |
3361 | static void nfs4_sync_open_stateid(nfs4_stateid *dst, | |
3362 | struct nfs4_state *state) | |
3363 | { | |
3364 | __be32 seqid_open; | |
3365 | u32 dst_seqid; | |
3366 | int seq; | |
3367 | ||
3368 | for (;;) { | |
3369 | if (!nfs4_valid_open_stateid(state)) | |
3370 | break; | |
3371 | seq = read_seqbegin(&state->seqlock); | |
3372 | if (!nfs4_state_match_open_stateid_other(state, dst)) { | |
3373 | nfs4_stateid_copy(dst, &state->open_stateid); | |
3374 | if (read_seqretry(&state->seqlock, seq)) | |
3375 | continue; | |
3376 | break; | |
3377 | } | |
3378 | seqid_open = state->open_stateid.seqid; | |
3379 | if (read_seqretry(&state->seqlock, seq)) | |
3380 | continue; | |
3381 | ||
3382 | dst_seqid = be32_to_cpu(dst->seqid); | |
3383 | if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0) | |
3384 | dst->seqid = seqid_open; | |
3385 | break; | |
3386 | } | |
3387 | } | |
3388 | ||
3389 | /* | |
3390 | * Update the seqid of an open stateid after receiving | |
3391 | * NFS4ERR_OLD_STATEID | |
3392 | */ | |
3393 | static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst, | |
3394 | struct nfs4_state *state) | |
3395 | { | |
3396 | __be32 seqid_open; | |
3397 | u32 dst_seqid; | |
3398 | bool ret; | |
3399 | int seq; | |
3400 | ||
3401 | for (;;) { | |
3402 | ret = false; | |
3403 | if (!nfs4_valid_open_stateid(state)) | |
3404 | break; | |
3405 | seq = read_seqbegin(&state->seqlock); | |
3406 | if (!nfs4_state_match_open_stateid_other(state, dst)) { | |
3407 | if (read_seqretry(&state->seqlock, seq)) | |
3408 | continue; | |
3409 | break; | |
3410 | } | |
3411 | seqid_open = state->open_stateid.seqid; | |
3412 | if (read_seqretry(&state->seqlock, seq)) | |
3413 | continue; | |
3414 | ||
3415 | dst_seqid = be32_to_cpu(dst->seqid); | |
3416 | if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0) | |
3417 | dst->seqid = cpu_to_be32(dst_seqid + 1); | |
3418 | else | |
3419 | dst->seqid = seqid_open; | |
3420 | ret = true; | |
3421 | break; | |
3422 | } | |
3423 | ||
3424 | return ret; | |
3425 | } | |
3426 | ||
1da177e4 LT |
3427 | struct nfs4_closedata { |
3428 | struct inode *inode; | |
3429 | struct nfs4_state *state; | |
3430 | struct nfs_closeargs arg; | |
3431 | struct nfs_closeres res; | |
cf805165 TM |
3432 | struct { |
3433 | struct nfs4_layoutreturn_args arg; | |
3434 | struct nfs4_layoutreturn_res res; | |
4d796d75 | 3435 | struct nfs4_xdr_opaque_data ld_private; |
cf805165 TM |
3436 | u32 roc_barrier; |
3437 | bool roc; | |
3438 | } lr; | |
516a6af6 | 3439 | struct nfs_fattr fattr; |
26e976a8 | 3440 | unsigned long timestamp; |
1da177e4 LT |
3441 | }; |
3442 | ||
963d8fe5 | 3443 | static void nfs4_free_closedata(void *data) |
9512135d | 3444 | { |
963d8fe5 TM |
3445 | struct nfs4_closedata *calldata = data; |
3446 | struct nfs4_state_owner *sp = calldata->state->owner; | |
643168c2 | 3447 | struct super_block *sb = calldata->state->inode->i_sb; |
9512135d | 3448 | |
cf805165 | 3449 | if (calldata->lr.roc) |
1c5bd76d TM |
3450 | pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res, |
3451 | calldata->res.lr_ret); | |
9512135d TM |
3452 | nfs4_put_open_state(calldata->state); |
3453 | nfs_free_seqid(calldata->arg.seqid); | |
9512135d | 3454 | nfs4_put_state_owner(sp); |
322b2b90 | 3455 | nfs_sb_deactive(sb); |
9512135d TM |
3456 | kfree(calldata); |
3457 | } | |
3458 | ||
963d8fe5 | 3459 | static void nfs4_close_done(struct rpc_task *task, void *data) |
1da177e4 | 3460 | { |
963d8fe5 | 3461 | struct nfs4_closedata *calldata = data; |
1da177e4 | 3462 | struct nfs4_state *state = calldata->state; |
1da177e4 | 3463 | struct nfs_server *server = NFS_SERVER(calldata->inode); |
412f6c4c | 3464 | nfs4_stateid *res_stateid = NULL; |
b8b8d221 TM |
3465 | struct nfs4_exception exception = { |
3466 | .state = state, | |
3467 | .inode = calldata->inode, | |
3468 | .stateid = &calldata->arg.stateid, | |
3469 | }; | |
1da177e4 | 3470 | |
a3ca5651 | 3471 | dprintk("%s: begin!\n", __func__); |
14516c3a TM |
3472 | if (!nfs4_sequence_done(task, &calldata->res.seq_res)) |
3473 | return; | |
42113a75 | 3474 | trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status); |
cf805165 TM |
3475 | |
3476 | /* Handle Layoutreturn errors */ | |
287a9c55 TM |
3477 | if (pnfs_roc_done(task, calldata->inode, |
3478 | &calldata->arg.lr_args, | |
3479 | &calldata->res.lr_res, | |
3480 | &calldata->res.lr_ret) == -EAGAIN) | |
3481 | goto out_restart; | |
cf805165 | 3482 | |
d9b67e1e | 3483 | /* hmm. we are done with the inode, and in the process of freeing |
1da177e4 LT |
3484 | * the state_owner. we keep this around to process errors |
3485 | */ | |
1da177e4 LT |
3486 | switch (task->tk_status) { |
3487 | case 0: | |
412f6c4c | 3488 | res_stateid = &calldata->res.stateid; |
26e976a8 | 3489 | renew_lease(server, calldata->timestamp); |
412f6c4c | 3490 | break; |
f07d4a31 TM |
3491 | case -NFS4ERR_ACCESS: |
3492 | if (calldata->arg.bitmask != NULL) { | |
3493 | calldata->arg.bitmask = NULL; | |
3494 | calldata->res.fattr = NULL; | |
91b30d2e | 3495 | goto out_restart; |
f07d4a31 TM |
3496 | |
3497 | } | |
3498 | break; | |
12f275cd TM |
3499 | case -NFS4ERR_OLD_STATEID: |
3500 | /* Did we race with OPEN? */ | |
0e0cb35b | 3501 | if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid, |
91b30d2e TM |
3502 | state)) |
3503 | goto out_restart; | |
12f275cd | 3504 | goto out_release; |
69794ad7 | 3505 | case -NFS4ERR_ADMIN_REVOKED: |
1da177e4 | 3506 | case -NFS4ERR_STALE_STATEID: |
26d36301 TM |
3507 | case -NFS4ERR_EXPIRED: |
3508 | nfs4_free_revoked_stateid(server, | |
3509 | &calldata->arg.stateid, | |
3510 | task->tk_msg.rpc_cred); | |
12f275cd | 3511 | /* Fallthrough */ |
9e33bed5 | 3512 | case -NFS4ERR_BAD_STATEID: |
e217e825 TM |
3513 | if (calldata->arg.fmode == 0) |
3514 | break; | |
3515 | /* Fallthrough */ | |
1da177e4 | 3516 | default: |
b8b8d221 TM |
3517 | task->tk_status = nfs4_async_handle_exception(task, |
3518 | server, task->tk_status, &exception); | |
3519 | if (exception.retry) | |
91b30d2e | 3520 | goto out_restart; |
1da177e4 | 3521 | } |
4a1e2feb TM |
3522 | nfs_clear_open_stateid(state, &calldata->arg.stateid, |
3523 | res_stateid, calldata->arg.fmode); | |
69794ad7 | 3524 | out_release: |
91b30d2e | 3525 | task->tk_status = 0; |
72211dbe | 3526 | nfs_release_seqid(calldata->arg.seqid); |
d8d84983 | 3527 | nfs_refresh_inode(calldata->inode, &calldata->fattr); |
a3ca5651 | 3528 | dprintk("%s: done, ret = %d!\n", __func__, task->tk_status); |
91b30d2e | 3529 | return; |
91b30d2e TM |
3530 | out_restart: |
3531 | task->tk_status = 0; | |
3532 | rpc_restart_call_prepare(task); | |
3533 | goto out_release; | |
1da177e4 LT |
3534 | } |
3535 | ||
4ce70ada | 3536 | static void nfs4_close_prepare(struct rpc_task *task, void *data) |
1da177e4 | 3537 | { |
4ce70ada | 3538 | struct nfs4_closedata *calldata = data; |
9512135d | 3539 | struct nfs4_state *state = calldata->state; |
7fdab069 | 3540 | struct inode *inode = calldata->inode; |
c8bf7073 | 3541 | struct pnfs_layout_hdr *lo; |
aee7af35 | 3542 | bool is_rdonly, is_wronly, is_rdwr; |
88069f77 | 3543 | int call_close = 0; |
9512135d | 3544 | |
a3ca5651 | 3545 | dprintk("%s: begin!\n", __func__); |
963d8fe5 | 3546 | if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) |
c8da19b9 | 3547 | goto out_wait; |
003707c7 | 3548 | |
88069f77 | 3549 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE]; |
4cecb76f | 3550 | spin_lock(&state->owner->so_lock); |
aee7af35 TM |
3551 | is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags); |
3552 | is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags); | |
3553 | is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags); | |
003707c7 | 3554 | /* Calculate the change in open mode */ |
cd9288ff | 3555 | calldata->arg.fmode = 0; |
e7616923 | 3556 | if (state->n_rdwr == 0) { |
cd9288ff TM |
3557 | if (state->n_rdonly == 0) |
3558 | call_close |= is_rdonly; | |
3559 | else if (is_rdonly) | |
3560 | calldata->arg.fmode |= FMODE_READ; | |
3561 | if (state->n_wronly == 0) | |
3562 | call_close |= is_wronly; | |
3563 | else if (is_wronly) | |
3564 | calldata->arg.fmode |= FMODE_WRITE; | |
e547f262 TM |
3565 | if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE)) |
3566 | call_close |= is_rdwr; | |
cd9288ff TM |
3567 | } else if (is_rdwr) |
3568 | calldata->arg.fmode |= FMODE_READ|FMODE_WRITE; | |
3569 | ||
0e0cb35b TM |
3570 | nfs4_sync_open_stateid(&calldata->arg.stateid, state); |
3571 | if (!nfs4_valid_open_stateid(state)) | |
5d422301 | 3572 | call_close = 0; |
4cecb76f | 3573 | spin_unlock(&state->owner->so_lock); |
88069f77 TM |
3574 | |
3575 | if (!call_close) { | |
963d8fe5 | 3576 | /* Note: exit _without_ calling nfs4_close_done */ |
c8da19b9 | 3577 | goto out_no_action; |
9512135d | 3578 | } |
88069f77 | 3579 | |
1c5bd76d | 3580 | if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) { |
500d701f PT |
3581 | nfs_release_seqid(calldata->arg.seqid); |
3582 | goto out_wait; | |
3583 | } | |
3584 | ||
c8bf7073 TM |
3585 | lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL; |
3586 | if (lo && !pnfs_layout_is_valid(lo)) { | |
3587 | calldata->arg.lr_args = NULL; | |
3588 | calldata->res.lr_res = NULL; | |
3589 | } | |
3590 | ||
9413a1a1 | 3591 | if (calldata->arg.fmode == 0) |
88069f77 | 3592 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE]; |
3ecefc92 | 3593 | |
9413a1a1 | 3594 | if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) { |
3ecefc92 TM |
3595 | /* Close-to-open cache consistency revalidation */ |
3596 | if (!nfs4_have_delegation(inode, FMODE_READ)) | |
3597 | calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask; | |
3598 | else | |
3599 | calldata->arg.bitmask = NULL; | |
3600 | } | |
3c13cb5b | 3601 | |
6ae37339 TM |
3602 | calldata->arg.share_access = |
3603 | nfs4_map_atomic_open_share(NFS_SERVER(inode), | |
3604 | calldata->arg.fmode, 0); | |
88069f77 | 3605 | |
d8d84983 TM |
3606 | if (calldata->res.fattr == NULL) |
3607 | calldata->arg.bitmask = NULL; | |
3608 | else if (calldata->arg.bitmask == NULL) | |
3609 | calldata->res.fattr = NULL; | |
26e976a8 | 3610 | calldata->timestamp = jiffies; |
42e1cca7 | 3611 | if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client, |
9d12b216 TM |
3612 | &calldata->arg.seq_args, |
3613 | &calldata->res.seq_res, | |
2240a9e2 TM |
3614 | task) != 0) |
3615 | nfs_release_seqid(calldata->arg.seqid); | |
a3ca5651 | 3616 | dprintk("%s: done!\n", __func__); |
c8da19b9 TM |
3617 | return; |
3618 | out_no_action: | |
3619 | task->tk_action = NULL; | |
3620 | out_wait: | |
3621 | nfs4_sequence_done(task, &calldata->res.seq_res); | |
1da177e4 LT |
3622 | } |
3623 | ||
963d8fe5 | 3624 | static const struct rpc_call_ops nfs4_close_ops = { |
4ce70ada | 3625 | .rpc_call_prepare = nfs4_close_prepare, |
963d8fe5 TM |
3626 | .rpc_call_done = nfs4_close_done, |
3627 | .rpc_release = nfs4_free_closedata, | |
3628 | }; | |
3629 | ||
1da177e4 LT |
3630 | /* |
3631 | * It is possible for data to be read/written from a mem-mapped file | |
3632 | * after the sys_close call (which hits the vfs layer as a flush). | |
3633 | * This means that we can't safely call nfsv4 close on a file until | |
3634 | * the inode is cleared. This in turn means that we are not good | |
3635 | * NFSv4 citizens - we do not indicate to the server to update the file's | |
3636 | * share state even when we are done with one of the three share | |
3637 | * stateid's in the inode. | |
3638 | * | |
3639 | * NOTE: Caller must be holding the sp->so_owner semaphore! | |
3640 | */ | |
1f7977c1 | 3641 | int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait) |
1da177e4 | 3642 | { |
4a35bd41 | 3643 | struct nfs_server *server = NFS_SERVER(state->inode); |
63f5f796 | 3644 | struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); |
1da177e4 | 3645 | struct nfs4_closedata *calldata; |
b39e625b TM |
3646 | struct nfs4_state_owner *sp = state->owner; |
3647 | struct rpc_task *task; | |
5138fde0 TM |
3648 | struct rpc_message msg = { |
3649 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE], | |
3650 | .rpc_cred = state->owner->so_cred, | |
3651 | }; | |
c970aa85 TM |
3652 | struct rpc_task_setup task_setup_data = { |
3653 | .rpc_client = server->client, | |
5138fde0 | 3654 | .rpc_message = &msg, |
c970aa85 | 3655 | .callback_ops = &nfs4_close_ops, |
101070ca | 3656 | .workqueue = nfsiod_workqueue, |
61296507 | 3657 | .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, |
c970aa85 | 3658 | }; |
9512135d | 3659 | int status = -ENOMEM; |
1da177e4 | 3660 | |
fa940720 WAA |
3661 | nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP, |
3662 | &task_setup_data.rpc_client, &msg); | |
3663 | ||
8535b2be | 3664 | calldata = kzalloc(sizeof(*calldata), gfp_mask); |
1da177e4 | 3665 | if (calldata == NULL) |
9512135d | 3666 | goto out; |
fba83f34 | 3667 | nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0); |
4a35bd41 | 3668 | calldata->inode = state->inode; |
1da177e4 | 3669 | calldata->state = state; |
4a35bd41 | 3670 | calldata->arg.fh = NFS_FH(state->inode); |
c82bac6f TM |
3671 | if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state)) |
3672 | goto out_free_calldata; | |
1da177e4 | 3673 | /* Serialization for the sequence id */ |
63f5f796 TM |
3674 | alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; |
3675 | calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask); | |
badc76dd | 3676 | if (IS_ERR(calldata->arg.seqid)) |
9512135d | 3677 | goto out_free_calldata; |
d8d84983 | 3678 | nfs_fattr_init(&calldata->fattr); |
dc0b027d | 3679 | calldata->arg.fmode = 0; |
4d796d75 | 3680 | calldata->lr.arg.ld_private = &calldata->lr.ld_private; |
516a6af6 | 3681 | calldata->res.fattr = &calldata->fattr; |
c1d51931 | 3682 | calldata->res.seqid = calldata->arg.seqid; |
516a6af6 | 3683 | calldata->res.server = server; |
cf805165 | 3684 | calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT; |
1c5bd76d TM |
3685 | calldata->lr.roc = pnfs_roc(state->inode, |
3686 | &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred); | |
3687 | if (calldata->lr.roc) { | |
3688 | calldata->arg.lr_args = &calldata->lr.arg; | |
3689 | calldata->res.lr_res = &calldata->lr.res; | |
3690 | } | |
643168c2 | 3691 | nfs_sb_active(calldata->inode->i_sb); |
9512135d | 3692 | |
1174dd1f TM |
3693 | msg.rpc_argp = &calldata->arg; |
3694 | msg.rpc_resp = &calldata->res; | |
c970aa85 TM |
3695 | task_setup_data.callback_data = calldata; |
3696 | task = rpc_run_task(&task_setup_data); | |
b39e625b TM |
3697 | if (IS_ERR(task)) |
3698 | return PTR_ERR(task); | |
a49c3c77 TM |
3699 | status = 0; |
3700 | if (wait) | |
3701 | status = rpc_wait_for_completion_task(task); | |
b39e625b | 3702 | rpc_put_task(task); |
a49c3c77 | 3703 | return status; |
9512135d TM |
3704 | out_free_calldata: |
3705 | kfree(calldata); | |
3706 | out: | |
b39e625b TM |
3707 | nfs4_put_open_state(state); |
3708 | nfs4_put_state_owner(sp); | |
9512135d | 3709 | return status; |
1da177e4 LT |
3710 | } |
3711 | ||
2b484297 | 3712 | static struct inode * |
5bc2afc2 TM |
3713 | nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, |
3714 | int open_flags, struct iattr *attr, int *opened) | |
1da177e4 | 3715 | { |
1da177e4 | 3716 | struct nfs4_state *state; |
aa9c2669 DQ |
3717 | struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL; |
3718 | ||
3719 | label = nfs4_label_init_security(dir, ctx->dentry, attr, &l); | |
1da177e4 | 3720 | |
565277f6 | 3721 | /* Protect against concurrent sillydeletes */ |
5bc2afc2 | 3722 | state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened); |
aa9c2669 DQ |
3723 | |
3724 | nfs4_label_release_security(label); | |
3725 | ||
f46e0bd3 TM |
3726 | if (IS_ERR(state)) |
3727 | return ERR_CAST(state); | |
275bb307 | 3728 | return state->inode; |
1da177e4 LT |
3729 | } |
3730 | ||
1185a552 | 3731 | static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync) |
7fe5c398 TM |
3732 | { |
3733 | if (ctx->state == NULL) | |
3734 | return; | |
3735 | if (is_sync) | |
1bf85d8c | 3736 | nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx)); |
7fe5c398 | 3737 | else |
1bf85d8c | 3738 | nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx)); |
7fe5c398 | 3739 | } |
1da177e4 | 3740 | |
b944dba3 TM |
3741 | #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL) |
3742 | #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL) | |
dff25ddb | 3743 | #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL) |
b944dba3 | 3744 | |
1da177e4 LT |
3745 | static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) |
3746 | { | |
8c61282f | 3747 | u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion; |
43652ad5 BH |
3748 | struct nfs4_server_caps_arg args = { |
3749 | .fhandle = fhandle, | |
8c61282f | 3750 | .bitmask = bitmask, |
43652ad5 | 3751 | }; |
1da177e4 LT |
3752 | struct nfs4_server_caps_res res = {}; |
3753 | struct rpc_message msg = { | |
3754 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS], | |
43652ad5 | 3755 | .rpc_argp = &args, |
1da177e4 LT |
3756 | .rpc_resp = &res, |
3757 | }; | |
3758 | int status; | |
f4b23de3 | 3759 | int i; |
1da177e4 | 3760 | |
8c61282f KM |
3761 | bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS | |
3762 | FATTR4_WORD0_FH_EXPIRE_TYPE | | |
3763 | FATTR4_WORD0_LINK_SUPPORT | | |
3764 | FATTR4_WORD0_SYMLINK_SUPPORT | | |
3765 | FATTR4_WORD0_ACLSUPPORT; | |
3766 | if (minorversion) | |
3767 | bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT; | |
3768 | ||
7c513058 | 3769 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 | 3770 | if (status == 0) { |
b944dba3 | 3771 | /* Sanity check the server answers */ |
8c61282f | 3772 | switch (minorversion) { |
b944dba3 TM |
3773 | case 0: |
3774 | res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK; | |
3775 | res.attr_bitmask[2] = 0; | |
3776 | break; | |
3777 | case 1: | |
3778 | res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK; | |
3779 | break; | |
3780 | case 2: | |
3781 | res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK; | |
3782 | } | |
1da177e4 | 3783 | memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask)); |
62ab460c TM |
3784 | server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS| |
3785 | NFS_CAP_SYMLINKS|NFS_CAP_FILEID| | |
3786 | NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER| | |
3787 | NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME| | |
b944dba3 TM |
3788 | NFS_CAP_CTIME|NFS_CAP_MTIME| |
3789 | NFS_CAP_SECURITY_LABEL); | |
7dd7d959 MN |
3790 | if (res.attr_bitmask[0] & FATTR4_WORD0_ACL && |
3791 | res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL) | |
1da177e4 LT |
3792 | server->caps |= NFS_CAP_ACLS; |
3793 | if (res.has_links != 0) | |
3794 | server->caps |= NFS_CAP_HARDLINKS; | |
3795 | if (res.has_symlinks != 0) | |
3796 | server->caps |= NFS_CAP_SYMLINKS; | |
62ab460c TM |
3797 | if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID) |
3798 | server->caps |= NFS_CAP_FILEID; | |
3799 | if (res.attr_bitmask[1] & FATTR4_WORD1_MODE) | |
3800 | server->caps |= NFS_CAP_MODE; | |
3801 | if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS) | |
3802 | server->caps |= NFS_CAP_NLINK; | |
3803 | if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER) | |
3804 | server->caps |= NFS_CAP_OWNER; | |
3805 | if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP) | |
3806 | server->caps |= NFS_CAP_OWNER_GROUP; | |
3807 | if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS) | |
3808 | server->caps |= NFS_CAP_ATIME; | |
3809 | if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA) | |
3810 | server->caps |= NFS_CAP_CTIME; | |
3811 | if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY) | |
3812 | server->caps |= NFS_CAP_MTIME; | |
aa9c2669 DQ |
3813 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
3814 | if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL) | |
3815 | server->caps |= NFS_CAP_SECURITY_LABEL; | |
3816 | #endif | |
3817 | memcpy(server->attr_bitmask_nl, res.attr_bitmask, | |
3818 | sizeof(server->attr_bitmask)); | |
b944dba3 | 3819 | server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL; |
62ab460c | 3820 | |
a65318bf TM |
3821 | memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask)); |
3822 | server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE; | |
3823 | server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY; | |
b944dba3 | 3824 | server->cache_consistency_bitmask[2] = 0; |
f4b23de3 TM |
3825 | |
3826 | /* Avoid a regression due to buggy server */ | |
3827 | for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++) | |
3828 | res.exclcreat_bitmask[i] &= res.attr_bitmask[i]; | |
8c61282f KM |
3829 | memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask, |
3830 | sizeof(server->exclcreat_bitmask)); | |
f4b23de3 | 3831 | |
1da177e4 | 3832 | server->acl_bitmask = res.acl_bitmask; |
264e6351 | 3833 | server->fh_expire_type = res.fh_expire_type; |
1da177e4 | 3834 | } |
cccef3b9 | 3835 | |
1da177e4 LT |
3836 | return status; |
3837 | } | |
3838 | ||
55a97593 | 3839 | int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) |
1da177e4 | 3840 | { |
0688e64b TM |
3841 | struct nfs4_exception exception = { |
3842 | .interruptible = true, | |
3843 | }; | |
1da177e4 LT |
3844 | int err; |
3845 | do { | |
3846 | err = nfs4_handle_exception(server, | |
3847 | _nfs4_server_capabilities(server, fhandle), | |
3848 | &exception); | |
3849 | } while (exception.retry); | |
3850 | return err; | |
3851 | } | |
3852 | ||
3853 | static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, | |
3854 | struct nfs_fsinfo *info) | |
3855 | { | |
aa9c2669 | 3856 | u32 bitmask[3]; |
1da177e4 | 3857 | struct nfs4_lookup_root_arg args = { |
aa9c2669 | 3858 | .bitmask = bitmask, |
1da177e4 LT |
3859 | }; |
3860 | struct nfs4_lookup_res res = { | |
3861 | .server = server, | |
0e574af1 | 3862 | .fattr = info->fattr, |
1da177e4 LT |
3863 | .fh = fhandle, |
3864 | }; | |
3865 | struct rpc_message msg = { | |
3866 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT], | |
3867 | .rpc_argp = &args, | |
3868 | .rpc_resp = &res, | |
3869 | }; | |
008f55d0 | 3870 | |
aa9c2669 DQ |
3871 | bitmask[0] = nfs4_fattr_bitmap[0]; |
3872 | bitmask[1] = nfs4_fattr_bitmap[1]; | |
3873 | /* | |
3874 | * Process the label in the upcoming getfattr | |
3875 | */ | |
3876 | bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL; | |
3877 | ||
0e574af1 | 3878 | nfs_fattr_init(info->fattr); |
7c513058 | 3879 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
3880 | } |
3881 | ||
3882 | static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, | |
3883 | struct nfs_fsinfo *info) | |
3884 | { | |
0688e64b TM |
3885 | struct nfs4_exception exception = { |
3886 | .interruptible = true, | |
3887 | }; | |
1da177e4 LT |
3888 | int err; |
3889 | do { | |
fb8a5ba8 | 3890 | err = _nfs4_lookup_root(server, fhandle, info); |
b5f875a9 | 3891 | trace_nfs4_lookup_root(server, fhandle, info->fattr, err); |
fb8a5ba8 BS |
3892 | switch (err) { |
3893 | case 0: | |
3894 | case -NFS4ERR_WRONGSEC: | |
05e9cfb4 | 3895 | goto out; |
fb8a5ba8 BS |
3896 | default: |
3897 | err = nfs4_handle_exception(server, err, &exception); | |
3898 | } | |
1da177e4 | 3899 | } while (exception.retry); |
05e9cfb4 | 3900 | out: |
1da177e4 LT |
3901 | return err; |
3902 | } | |
3903 | ||
8f70e95f BS |
3904 | static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, |
3905 | struct nfs_fsinfo *info, rpc_authflavor_t flavor) | |
3906 | { | |
c2190661 TM |
3907 | struct rpc_auth_create_args auth_args = { |
3908 | .pseudoflavor = flavor, | |
3909 | }; | |
8f70e95f | 3910 | struct rpc_auth *auth; |
8f70e95f | 3911 | |
c2190661 | 3912 | auth = rpcauth_create(&auth_args, server->client); |
9df1336c AS |
3913 | if (IS_ERR(auth)) |
3914 | return -EACCES; | |
3915 | return nfs4_lookup_root(server, fhandle, info); | |
8f70e95f BS |
3916 | } |
3917 | ||
9a744ba3 CL |
3918 | /* |
3919 | * Retry pseudoroot lookup with various security flavors. We do this when: | |
3920 | * | |
3921 | * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC | |
3922 | * NFSv4.1: the server does not support the SECINFO_NO_NAME operation | |
3923 | * | |
3924 | * Returns zero on success, or a negative NFS4ERR value, or a | |
3925 | * negative errno value. | |
3926 | */ | |
801a16dc | 3927 | static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, |
54ceac45 | 3928 | struct nfs_fsinfo *info) |
1da177e4 | 3929 | { |
9a744ba3 CL |
3930 | /* Per 3530bis 15.33.5 */ |
3931 | static const rpc_authflavor_t flav_array[] = { | |
3932 | RPC_AUTH_GSS_KRB5P, | |
3933 | RPC_AUTH_GSS_KRB5I, | |
3934 | RPC_AUTH_GSS_KRB5, | |
c4eafe11 | 3935 | RPC_AUTH_UNIX, /* courtesy */ |
9a744ba3 CL |
3936 | RPC_AUTH_NULL, |
3937 | }; | |
3938 | int status = -EPERM; | |
3939 | size_t i; | |
6a1a1e34 | 3940 | |
4d4b69dd WAA |
3941 | if (server->auth_info.flavor_len > 0) { |
3942 | /* try each flavor specified by user */ | |
3943 | for (i = 0; i < server->auth_info.flavor_len; i++) { | |
3944 | status = nfs4_lookup_root_sec(server, fhandle, info, | |
3945 | server->auth_info.flavors[i]); | |
3946 | if (status == -NFS4ERR_WRONGSEC || status == -EACCES) | |
3947 | continue; | |
3948 | break; | |
3949 | } | |
3950 | } else { | |
3951 | /* no flavors specified by user, try default list */ | |
3952 | for (i = 0; i < ARRAY_SIZE(flav_array); i++) { | |
3953 | status = nfs4_lookup_root_sec(server, fhandle, info, | |
3954 | flav_array[i]); | |
3955 | if (status == -NFS4ERR_WRONGSEC || status == -EACCES) | |
3956 | continue; | |
3957 | break; | |
3958 | } | |
8f70e95f | 3959 | } |
9a744ba3 | 3960 | |
fb8a5ba8 | 3961 | /* |
d3787af2 | 3962 | * -EACCES could mean that the user doesn't have correct permissions |
fb8a5ba8 BS |
3963 | * to access the mount. It could also mean that we tried to mount |
3964 | * with a gss auth flavor, but rpc.gssd isn't running. Either way, | |
3965 | * existing mount programs don't handle -EACCES very well so it should | |
3966 | * be mapped to -EPERM instead. | |
3967 | */ | |
3968 | if (status == -EACCES) | |
3969 | status = -EPERM; | |
801a16dc BS |
3970 | return status; |
3971 | } | |
3972 | ||
2ed4b95b CL |
3973 | /** |
3974 | * nfs4_proc_get_rootfh - get file handle for server's pseudoroot | |
3975 | * @server: initialized nfs_server handle | |
3976 | * @fhandle: we fill in the pseudo-fs root file handle | |
3977 | * @info: we fill in an FSINFO struct | |
5e6b1990 | 3978 | * @auth_probe: probe the auth flavours |
2ed4b95b CL |
3979 | * |
3980 | * Returns zero on success, or a negative errno. | |
801a16dc | 3981 | */ |
3028eb2b | 3982 | int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle, |
5e6b1990 TM |
3983 | struct nfs_fsinfo *info, |
3984 | bool auth_probe) | |
801a16dc | 3985 | { |
c7757074 | 3986 | int status = 0; |
2ed4b95b | 3987 | |
c7757074 | 3988 | if (!auth_probe) |
5e6b1990 | 3989 | status = nfs4_lookup_root(server, fhandle, info); |
c7757074 AP |
3990 | |
3991 | if (auth_probe || status == NFS4ERR_WRONGSEC) | |
698c937b TM |
3992 | status = server->nfs_client->cl_mvops->find_root_sec(server, |
3993 | fhandle, info); | |
2ed4b95b | 3994 | |
1da177e4 LT |
3995 | if (status == 0) |
3996 | status = nfs4_server_capabilities(server, fhandle); | |
3997 | if (status == 0) | |
3998 | status = nfs4_do_fsinfo(server, fhandle, info); | |
2ed4b95b | 3999 | |
c12e87f4 | 4000 | return nfs4_map_errors(status); |
1da177e4 LT |
4001 | } |
4002 | ||
bae36241 BS |
4003 | static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh, |
4004 | struct nfs_fsinfo *info) | |
4005 | { | |
4006 | int error; | |
4007 | struct nfs_fattr *fattr = info->fattr; | |
779df6a5 | 4008 | struct nfs4_label *label = fattr->label; |
bae36241 BS |
4009 | |
4010 | error = nfs4_server_capabilities(server, mntfh); | |
4011 | if (error < 0) { | |
4012 | dprintk("nfs4_get_root: getcaps error = %d\n", -error); | |
4013 | return error; | |
4014 | } | |
4015 | ||
a841b54d | 4016 | error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL); |
bae36241 BS |
4017 | if (error < 0) { |
4018 | dprintk("nfs4_get_root: getattr error = %d\n", -error); | |
779df6a5 | 4019 | goto out; |
bae36241 BS |
4020 | } |
4021 | ||
4022 | if (fattr->valid & NFS_ATTR_FATTR_FSID && | |
4023 | !nfs_fsid_equal(&server->fsid, &fattr->fsid)) | |
4024 | memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid)); | |
4025 | ||
779df6a5 | 4026 | out: |
bae36241 BS |
4027 | return error; |
4028 | } | |
4029 | ||
6b97fd3d MN |
4030 | /* |
4031 | * Get locations and (maybe) other attributes of a referral. | |
4032 | * Note that we'll actually follow the referral later when | |
4033 | * we detect fsid mismatch in inode revalidation | |
4034 | */ | |
f05d147f BS |
4035 | static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir, |
4036 | const struct qstr *name, struct nfs_fattr *fattr, | |
4037 | struct nfs_fh *fhandle) | |
6b97fd3d MN |
4038 | { |
4039 | int status = -ENOMEM; | |
4040 | struct page *page = NULL; | |
4041 | struct nfs4_fs_locations *locations = NULL; | |
6b97fd3d MN |
4042 | |
4043 | page = alloc_page(GFP_KERNEL); | |
4044 | if (page == NULL) | |
4045 | goto out; | |
4046 | locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); | |
4047 | if (locations == NULL) | |
4048 | goto out; | |
4049 | ||
f05d147f | 4050 | status = nfs4_proc_fs_locations(client, dir, name, locations, page); |
6b97fd3d MN |
4051 | if (status != 0) |
4052 | goto out; | |
519ae255 CL |
4053 | |
4054 | /* | |
4055 | * If the fsid didn't change, this is a migration event, not a | |
4056 | * referral. Cause us to drop into the exception handler, which | |
4057 | * will kick off migration recovery. | |
4058 | */ | |
6b97fd3d | 4059 | if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) { |
533eb461 AA |
4060 | dprintk("%s: server did not return a different fsid for" |
4061 | " a referral at %s\n", __func__, name->name); | |
519ae255 | 4062 | status = -NFS4ERR_MOVED; |
6b97fd3d MN |
4063 | goto out; |
4064 | } | |
533eb461 AA |
4065 | /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */ |
4066 | nfs_fixup_referral_attributes(&locations->fattr); | |
6b97fd3d | 4067 | |
533eb461 | 4068 | /* replace the lookup nfs_fattr with the locations nfs_fattr */ |
6b97fd3d | 4069 | memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr)); |
6b97fd3d MN |
4070 | memset(fhandle, 0, sizeof(struct nfs_fh)); |
4071 | out: | |
4072 | if (page) | |
4073 | __free_page(page); | |
5d7ca35a | 4074 | kfree(locations); |
6b97fd3d MN |
4075 | return status; |
4076 | } | |
4077 | ||
1775fd3e | 4078 | static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, |
a841b54d TM |
4079 | struct nfs_fattr *fattr, struct nfs4_label *label, |
4080 | struct inode *inode) | |
1da177e4 | 4081 | { |
771734f2 | 4082 | __u32 bitmask[NFS4_BITMASK_SZ]; |
1da177e4 LT |
4083 | struct nfs4_getattr_arg args = { |
4084 | .fh = fhandle, | |
771734f2 | 4085 | .bitmask = bitmask, |
1da177e4 LT |
4086 | }; |
4087 | struct nfs4_getattr_res res = { | |
4088 | .fattr = fattr, | |
1775fd3e | 4089 | .label = label, |
1da177e4 LT |
4090 | .server = server, |
4091 | }; | |
4092 | struct rpc_message msg = { | |
4093 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], | |
4094 | .rpc_argp = &args, | |
4095 | .rpc_resp = &res, | |
4096 | }; | |
c74dfe97 TM |
4097 | unsigned short task_flags = 0; |
4098 | ||
4099 | /* Is this is an attribute revalidation, subject to softreval? */ | |
4100 | if (inode && (server->flags & NFS_MOUNT_SOFTREVAL)) | |
4101 | task_flags |= RPC_TASK_TIMEOUT; | |
aa9c2669 | 4102 | |
771734f2 | 4103 | nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode); |
aa9c2669 | 4104 | |
0e574af1 | 4105 | nfs_fattr_init(fattr); |
c74dfe97 TM |
4106 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0); |
4107 | return nfs4_do_call_sync(server->client, server, &msg, | |
4108 | &args.seq_args, &res.seq_res, task_flags); | |
1da177e4 LT |
4109 | } |
4110 | ||
ec4b0925 | 4111 | int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, |
a841b54d TM |
4112 | struct nfs_fattr *fattr, struct nfs4_label *label, |
4113 | struct inode *inode) | |
1da177e4 | 4114 | { |
0688e64b TM |
4115 | struct nfs4_exception exception = { |
4116 | .interruptible = true, | |
4117 | }; | |
1da177e4 LT |
4118 | int err; |
4119 | do { | |
a841b54d | 4120 | err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode); |
b5f875a9 TM |
4121 | trace_nfs4_getattr(server, fhandle, fattr, err); |
4122 | err = nfs4_handle_exception(server, err, | |
1da177e4 LT |
4123 | &exception); |
4124 | } while (exception.retry); | |
4125 | return err; | |
4126 | } | |
4127 | ||
4128 | /* | |
4129 | * The file is not closed if it is opened due to the a request to change | |
4130 | * the size of the file. The open call will not be needed once the | |
4131 | * VFS layer lookup-intents are implemented. | |
4132 | * | |
4133 | * Close is called when the inode is destroyed. | |
4134 | * If we haven't opened the file for O_WRONLY, we | |
4135 | * need to in the size_change case to obtain a stateid. | |
4136 | * | |
4137 | * Got race? | |
4138 | * Because OPEN is always done by name in nfsv4, it is | |
4139 | * possible that we opened a different file by the same | |
4140 | * name. We can recognize this race condition, but we | |
4141 | * can't do anything about it besides returning an error. | |
4142 | * | |
4143 | * This will be fixed with VFS changes (lookup-intent). | |
4144 | */ | |
4145 | static int | |
4146 | nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr, | |
4147 | struct iattr *sattr) | |
4148 | { | |
2b0143b5 | 4149 | struct inode *inode = d_inode(dentry); |
a52458b4 | 4150 | const struct cred *cred = NULL; |
29b59f94 | 4151 | struct nfs_open_context *ctx = NULL; |
14c43f76 | 4152 | struct nfs4_label *label = NULL; |
1da177e4 LT |
4153 | int status; |
4154 | ||
88ac815c PT |
4155 | if (pnfs_ld_layoutret_on_setattr(inode) && |
4156 | sattr->ia_valid & ATTR_SIZE && | |
4157 | sattr->ia_size < i_size_read(inode)) | |
24028672 | 4158 | pnfs_commit_and_return_layout(inode); |
8a1636c4 | 4159 | |
0e574af1 | 4160 | nfs_fattr_init(fattr); |
1da177e4 | 4161 | |
2669940d AA |
4162 | /* Deal with open(O_TRUNC) */ |
4163 | if (sattr->ia_valid & ATTR_OPEN) | |
cc7936f9 | 4164 | sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME); |
2669940d AA |
4165 | |
4166 | /* Optimization: if the end result is no change, don't RPC */ | |
cc7936f9 | 4167 | if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0) |
2669940d AA |
4168 | return 0; |
4169 | ||
d530838b | 4170 | /* Search for an existing open(O_WRITE) file */ |
659bfcd6 | 4171 | if (sattr->ia_valid & ATTR_FILE) { |
659bfcd6 TM |
4172 | |
4173 | ctx = nfs_file_open_context(sattr->ia_file); | |
29b59f94 | 4174 | if (ctx) |
504e5189 | 4175 | cred = ctx->cred; |
659bfcd6 | 4176 | } |
08e9eac4 | 4177 | |
14c43f76 DQ |
4178 | label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); |
4179 | if (IS_ERR(label)) | |
4180 | return PTR_ERR(label); | |
4181 | ||
199366f0 TM |
4182 | /* Return any delegations if we're going to change ACLs */ |
4183 | if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) | |
c01d3645 | 4184 | nfs4_inode_make_writeable(inode); |
199366f0 | 4185 | |
29b59f94 | 4186 | status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label); |
aa9c2669 | 4187 | if (status == 0) { |
f044636d | 4188 | nfs_setattr_update_inode(inode, sattr, fattr); |
aa9c2669 DQ |
4189 | nfs_setsecurity(inode, fattr, label); |
4190 | } | |
14c43f76 | 4191 | nfs4_label_free(label); |
1da177e4 LT |
4192 | return status; |
4193 | } | |
4194 | ||
0c2e53f1 | 4195 | static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, |
f7b37b8b | 4196 | struct dentry *dentry, struct nfs_fh *fhandle, |
1775fd3e | 4197 | struct nfs_fattr *fattr, struct nfs4_label *label) |
2b3de441 | 4198 | { |
0c2e53f1 | 4199 | struct nfs_server *server = NFS_SERVER(dir); |
2b3de441 DH |
4200 | int status; |
4201 | struct nfs4_lookup_arg args = { | |
4202 | .bitmask = server->attr_bitmask, | |
0c2e53f1 | 4203 | .dir_fh = NFS_FH(dir), |
f7b37b8b | 4204 | .name = &dentry->d_name, |
2b3de441 DH |
4205 | }; |
4206 | struct nfs4_lookup_res res = { | |
4207 | .server = server, | |
4208 | .fattr = fattr, | |
aa9c2669 | 4209 | .label = label, |
2b3de441 DH |
4210 | .fh = fhandle, |
4211 | }; | |
4212 | struct rpc_message msg = { | |
4213 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP], | |
4214 | .rpc_argp = &args, | |
4215 | .rpc_resp = &res, | |
4216 | }; | |
f7b37b8b TM |
4217 | unsigned short task_flags = 0; |
4218 | ||
4219 | /* Is this is an attribute revalidation, subject to softreval? */ | |
4220 | if (nfs_lookup_is_soft_revalidate(dentry)) | |
4221 | task_flags |= RPC_TASK_TIMEOUT; | |
2b3de441 | 4222 | |
aa9c2669 DQ |
4223 | args.bitmask = nfs4_bitmask(server, label); |
4224 | ||
2b3de441 DH |
4225 | nfs_fattr_init(fattr); |
4226 | ||
f7b37b8b TM |
4227 | dprintk("NFS call lookup %pd2\n", dentry); |
4228 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0); | |
4229 | status = nfs4_do_call_sync(clnt, server, &msg, | |
4230 | &args.seq_args, &res.seq_res, task_flags); | |
1da177e4 LT |
4231 | dprintk("NFS reply lookup: %d\n", status); |
4232 | return status; | |
4233 | } | |
4234 | ||
72de53ec | 4235 | static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr) |
7ebb9315 | 4236 | { |
7ebb9315 | 4237 | fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | |
72de53ec | 4238 | NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT; |
7ebb9315 BS |
4239 | fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; |
4240 | fattr->nlink = 2; | |
4241 | } | |
4242 | ||
72de53ec | 4243 | static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir, |
f7b37b8b | 4244 | struct dentry *dentry, struct nfs_fh *fhandle, |
1775fd3e | 4245 | struct nfs_fattr *fattr, struct nfs4_label *label) |
1da177e4 | 4246 | { |
0688e64b TM |
4247 | struct nfs4_exception exception = { |
4248 | .interruptible = true, | |
4249 | }; | |
72de53ec | 4250 | struct rpc_clnt *client = *clnt; |
f7b37b8b | 4251 | const struct qstr *name = &dentry->d_name; |
1da177e4 LT |
4252 | int err; |
4253 | do { | |
f7b37b8b | 4254 | err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label); |
078ea3df | 4255 | trace_nfs4_lookup(dir, name, err); |
72de53ec | 4256 | switch (err) { |
08ef7bd3 | 4257 | case -NFS4ERR_BADNAME: |
72de53ec BS |
4258 | err = -ENOENT; |
4259 | goto out; | |
0c2e53f1 | 4260 | case -NFS4ERR_MOVED: |
f05d147f | 4261 | err = nfs4_get_referral(client, dir, name, fattr, fhandle); |
c86c90c6 DM |
4262 | if (err == -NFS4ERR_MOVED) |
4263 | err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception); | |
72de53ec | 4264 | goto out; |
0c2e53f1 | 4265 | case -NFS4ERR_WRONGSEC: |
72de53ec BS |
4266 | err = -EPERM; |
4267 | if (client != *clnt) | |
4268 | goto out; | |
66b06860 | 4269 | client = nfs4_negotiate_security(client, dir, name); |
72de53ec BS |
4270 | if (IS_ERR(client)) |
4271 | return PTR_ERR(client); | |
4272 | ||
4273 | exception.retry = 1; | |
4274 | break; | |
4275 | default: | |
4276 | err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception); | |
0c2e53f1 | 4277 | } |
1da177e4 | 4278 | } while (exception.retry); |
72de53ec BS |
4279 | |
4280 | out: | |
4281 | if (err == 0) | |
4282 | *clnt = client; | |
4283 | else if (client != *clnt) | |
4284 | rpc_shutdown_client(client); | |
4285 | ||
1da177e4 LT |
4286 | return err; |
4287 | } | |
4288 | ||
f7b37b8b | 4289 | static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry, |
1775fd3e DQ |
4290 | struct nfs_fh *fhandle, struct nfs_fattr *fattr, |
4291 | struct nfs4_label *label) | |
72de53ec BS |
4292 | { |
4293 | int status; | |
4294 | struct rpc_clnt *client = NFS_CLIENT(dir); | |
4295 | ||
f7b37b8b | 4296 | status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label); |
72de53ec BS |
4297 | if (client != NFS_CLIENT(dir)) { |
4298 | rpc_shutdown_client(client); | |
4299 | nfs_fixup_secinfo_attributes(fattr); | |
4300 | } | |
4301 | return status; | |
4302 | } | |
4303 | ||
f05d147f | 4304 | struct rpc_clnt * |
f7b37b8b | 4305 | nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry, |
f05d147f BS |
4306 | struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
4307 | { | |
b72888cb | 4308 | struct rpc_clnt *client = NFS_CLIENT(dir); |
f05d147f | 4309 | int status; |
f05d147f | 4310 | |
f7b37b8b | 4311 | status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL); |
b72888cb | 4312 | if (status < 0) |
f05d147f | 4313 | return ERR_PTR(status); |
b72888cb | 4314 | return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client; |
f05d147f BS |
4315 | } |
4316 | ||
5b5faaf6 JL |
4317 | static int _nfs4_proc_lookupp(struct inode *inode, |
4318 | struct nfs_fh *fhandle, struct nfs_fattr *fattr, | |
4319 | struct nfs4_label *label) | |
4320 | { | |
4321 | struct rpc_clnt *clnt = NFS_CLIENT(inode); | |
4322 | struct nfs_server *server = NFS_SERVER(inode); | |
4323 | int status; | |
4324 | struct nfs4_lookupp_arg args = { | |
4325 | .bitmask = server->attr_bitmask, | |
4326 | .fh = NFS_FH(inode), | |
4327 | }; | |
4328 | struct nfs4_lookupp_res res = { | |
4329 | .server = server, | |
4330 | .fattr = fattr, | |
4331 | .label = label, | |
4332 | .fh = fhandle, | |
4333 | }; | |
4334 | struct rpc_message msg = { | |
4335 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP], | |
4336 | .rpc_argp = &args, | |
4337 | .rpc_resp = &res, | |
4338 | }; | |
4339 | ||
4340 | args.bitmask = nfs4_bitmask(server, label); | |
4341 | ||
4342 | nfs_fattr_init(fattr); | |
4343 | ||
4344 | dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino); | |
4345 | status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, | |
4346 | &res.seq_res, 0); | |
4347 | dprintk("NFS reply lookupp: %d\n", status); | |
4348 | return status; | |
4349 | } | |
4350 | ||
4351 | static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle, | |
4352 | struct nfs_fattr *fattr, struct nfs4_label *label) | |
4353 | { | |
0688e64b TM |
4354 | struct nfs4_exception exception = { |
4355 | .interruptible = true, | |
4356 | }; | |
5b5faaf6 JL |
4357 | int err; |
4358 | do { | |
4359 | err = _nfs4_proc_lookupp(inode, fhandle, fattr, label); | |
4360 | trace_nfs4_lookupp(inode, err); | |
4361 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
4362 | &exception); | |
4363 | } while (exception.retry); | |
4364 | return err; | |
4365 | } | |
4366 | ||
1da177e4 LT |
4367 | static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) |
4368 | { | |
76b32999 | 4369 | struct nfs_server *server = NFS_SERVER(inode); |
1da177e4 LT |
4370 | struct nfs4_accessargs args = { |
4371 | .fh = NFS_FH(inode), | |
1750d929 | 4372 | .access = entry->mask, |
76b32999 TM |
4373 | }; |
4374 | struct nfs4_accessres res = { | |
4375 | .server = server, | |
1da177e4 | 4376 | }; |
1da177e4 LT |
4377 | struct rpc_message msg = { |
4378 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS], | |
4379 | .rpc_argp = &args, | |
4380 | .rpc_resp = &res, | |
4381 | .rpc_cred = entry->cred, | |
4382 | }; | |
aa9c2669 | 4383 | int status = 0; |
1da177e4 | 4384 | |
7c672654 | 4385 | if (!nfs4_have_delegation(inode, FMODE_READ)) { |
8bcbe7d9 TM |
4386 | res.fattr = nfs_alloc_fattr(); |
4387 | if (res.fattr == NULL) | |
4388 | return -ENOMEM; | |
4389 | args.bitmask = server->cache_consistency_bitmask; | |
4390 | } | |
7c513058 | 4391 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 | 4392 | if (!status) { |
6168f62c | 4393 | nfs_access_set_mask(entry, res.access); |
8bcbe7d9 TM |
4394 | if (res.fattr) |
4395 | nfs_refresh_inode(inode, res.fattr); | |
1da177e4 | 4396 | } |
c407d41a | 4397 | nfs_free_fattr(res.fattr); |
1da177e4 LT |
4398 | return status; |
4399 | } | |
4400 | ||
4401 | static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) | |
4402 | { | |
0688e64b TM |
4403 | struct nfs4_exception exception = { |
4404 | .interruptible = true, | |
4405 | }; | |
1da177e4 LT |
4406 | int err; |
4407 | do { | |
c1578b76 TM |
4408 | err = _nfs4_proc_access(inode, entry); |
4409 | trace_nfs4_access(inode, err); | |
4410 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
1da177e4 LT |
4411 | &exception); |
4412 | } while (exception.retry); | |
4413 | return err; | |
4414 | } | |
4415 | ||
4416 | /* | |
4417 | * TODO: For the time being, we don't try to get any attributes | |
4418 | * along with any of the zero-copy operations READ, READDIR, | |
4419 | * READLINK, WRITE. | |
4420 | * | |
4421 | * In the case of the first three, we want to put the GETATTR | |
4422 | * after the read-type operation -- this is because it is hard | |
4423 | * to predict the length of a GETATTR response in v4, and thus | |
4424 | * align the READ data correctly. This means that the GETATTR | |
4425 | * may end up partially falling into the page cache, and we should | |
4426 | * shift it into the 'tail' of the xdr_buf before processing. | |
4427 | * To do this efficiently, we need to know the total length | |
4428 | * of data received, which doesn't seem to be available outside | |
4429 | * of the RPC layer. | |
4430 | * | |
4431 | * In the case of WRITE, we also want to put the GETATTR after | |
4432 | * the operation -- in this case because we want to make sure | |
140150db | 4433 | * we get the post-operation mtime and size. |
1da177e4 LT |
4434 | * |
4435 | * Both of these changes to the XDR layer would in fact be quite | |
4436 | * minor, but I decided to leave them for a subsequent patch. | |
4437 | */ | |
4438 | static int _nfs4_proc_readlink(struct inode *inode, struct page *page, | |
4439 | unsigned int pgbase, unsigned int pglen) | |
4440 | { | |
4441 | struct nfs4_readlink args = { | |
4442 | .fh = NFS_FH(inode), | |
4443 | .pgbase = pgbase, | |
4444 | .pglen = pglen, | |
4445 | .pages = &page, | |
4446 | }; | |
f50c7000 | 4447 | struct nfs4_readlink_res res; |
1da177e4 LT |
4448 | struct rpc_message msg = { |
4449 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK], | |
4450 | .rpc_argp = &args, | |
f50c7000 | 4451 | .rpc_resp = &res, |
1da177e4 LT |
4452 | }; |
4453 | ||
7c513058 | 4454 | return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
4455 | } |
4456 | ||
4457 | static int nfs4_proc_readlink(struct inode *inode, struct page *page, | |
4458 | unsigned int pgbase, unsigned int pglen) | |
4459 | { | |
0688e64b TM |
4460 | struct nfs4_exception exception = { |
4461 | .interruptible = true, | |
4462 | }; | |
1da177e4 LT |
4463 | int err; |
4464 | do { | |
c1578b76 TM |
4465 | err = _nfs4_proc_readlink(inode, page, pgbase, pglen); |
4466 | trace_nfs4_readlink(inode, err); | |
4467 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
1da177e4 LT |
4468 | &exception); |
4469 | } while (exception.retry); | |
4470 | return err; | |
4471 | } | |
4472 | ||
1da177e4 | 4473 | /* |
8867fe58 | 4474 | * This is just for mknod. open(O_CREAT) will always do ->open_context(). |
1da177e4 | 4475 | */ |
1da177e4 LT |
4476 | static int |
4477 | nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, | |
8867fe58 | 4478 | int flags) |
1da177e4 | 4479 | { |
dff25ddb | 4480 | struct nfs_server *server = NFS_SERVER(dir); |
aa9c2669 | 4481 | struct nfs4_label l, *ilabel = NULL; |
8867fe58 | 4482 | struct nfs_open_context *ctx; |
1da177e4 | 4483 | struct nfs4_state *state; |
1da177e4 LT |
4484 | int status = 0; |
4485 | ||
532d4def | 4486 | ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL); |
8867fe58 MS |
4487 | if (IS_ERR(ctx)) |
4488 | return PTR_ERR(ctx); | |
4489 | ||
aa9c2669 DQ |
4490 | ilabel = nfs4_label_init_security(dir, dentry, sattr, &l); |
4491 | ||
dff25ddb AG |
4492 | if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) |
4493 | sattr->ia_mode &= ~current_umask(); | |
c5c3fb5f | 4494 | state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL); |
1da177e4 LT |
4495 | if (IS_ERR(state)) { |
4496 | status = PTR_ERR(state); | |
c0204fd2 | 4497 | goto out; |
1da177e4 | 4498 | } |
1da177e4 | 4499 | out: |
aa9c2669 | 4500 | nfs4_label_release_security(ilabel); |
8867fe58 | 4501 | put_nfs_open_context(ctx); |
1da177e4 LT |
4502 | return status; |
4503 | } | |
4504 | ||
3c591175 TM |
4505 | static int |
4506 | _nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype) | |
1da177e4 | 4507 | { |
16e42959 | 4508 | struct nfs_server *server = NFS_SERVER(dir); |
4fdc17b2 | 4509 | struct nfs_removeargs args = { |
1da177e4 | 4510 | .fh = NFS_FH(dir), |
26fe5750 | 4511 | .name = *name, |
16e42959 | 4512 | }; |
4fdc17b2 | 4513 | struct nfs_removeres res = { |
16e42959 | 4514 | .server = server, |
1da177e4 | 4515 | }; |
1da177e4 | 4516 | struct rpc_message msg = { |
4fdc17b2 TM |
4517 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE], |
4518 | .rpc_argp = &args, | |
4519 | .rpc_resp = &res, | |
1da177e4 | 4520 | }; |
d3129ef6 | 4521 | unsigned long timestamp = jiffies; |
778d2817 | 4522 | int status; |
1da177e4 | 4523 | |
7c513058 | 4524 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1); |
3c591175 TM |
4525 | if (status == 0) { |
4526 | spin_lock(&dir->i_lock); | |
5636ec4e | 4527 | update_changeattr_locked(dir, &res.cinfo, timestamp, 0); |
3c591175 TM |
4528 | /* Removing a directory decrements nlink in the parent */ |
4529 | if (ftype == NF4DIR && dir->i_nlink > 2) | |
4530 | nfs4_dec_nlink_locked(dir); | |
4531 | spin_unlock(&dir->i_lock); | |
4532 | } | |
1da177e4 LT |
4533 | return status; |
4534 | } | |
4535 | ||
912678db | 4536 | static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry) |
1da177e4 | 4537 | { |
0688e64b TM |
4538 | struct nfs4_exception exception = { |
4539 | .interruptible = true, | |
4540 | }; | |
912678db | 4541 | struct inode *inode = d_inode(dentry); |
1da177e4 | 4542 | int err; |
912678db | 4543 | |
c01d3645 TM |
4544 | if (inode) { |
4545 | if (inode->i_nlink == 1) | |
4546 | nfs4_inode_return_delegation(inode); | |
4547 | else | |
4548 | nfs4_inode_make_writeable(inode); | |
4549 | } | |
912678db | 4550 | do { |
3c591175 | 4551 | err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG); |
912678db TM |
4552 | trace_nfs4_remove(dir, &dentry->d_name, err); |
4553 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
4554 | &exception); | |
4555 | } while (exception.retry); | |
4556 | return err; | |
4557 | } | |
4558 | ||
4559 | static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name) | |
4560 | { | |
0688e64b TM |
4561 | struct nfs4_exception exception = { |
4562 | .interruptible = true, | |
4563 | }; | |
912678db TM |
4564 | int err; |
4565 | ||
1da177e4 | 4566 | do { |
3c591175 | 4567 | err = _nfs4_proc_remove(dir, name, NF4DIR); |
078ea3df TM |
4568 | trace_nfs4_remove(dir, name, err); |
4569 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
1da177e4 LT |
4570 | &exception); |
4571 | } while (exception.retry); | |
4572 | return err; | |
4573 | } | |
4574 | ||
ed7e9ad0 TM |
4575 | static void nfs4_proc_unlink_setup(struct rpc_message *msg, |
4576 | struct dentry *dentry, | |
4577 | struct inode *inode) | |
1da177e4 | 4578 | { |
e4eff1a6 TM |
4579 | struct nfs_removeargs *args = msg->rpc_argp; |
4580 | struct nfs_removeres *res = msg->rpc_resp; | |
1da177e4 | 4581 | |
977fcc2b | 4582 | res->server = NFS_SB(dentry->d_sb); |
1da177e4 | 4583 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE]; |
fba83f34 | 4584 | nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0); |
aa9c2669 DQ |
4585 | |
4586 | nfs_fattr_init(res->dir_attr); | |
977fcc2b TM |
4587 | |
4588 | if (inode) | |
4589 | nfs4_inode_return_delegation(inode); | |
1da177e4 LT |
4590 | } |
4591 | ||
34e137cc BS |
4592 | static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data) |
4593 | { | |
42e1cca7 | 4594 | nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client, |
d9afbd1b TM |
4595 | &data->args.seq_args, |
4596 | &data->res.seq_res, | |
4597 | task); | |
1da177e4 LT |
4598 | } |
4599 | ||
e4eff1a6 | 4600 | static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir) |
1da177e4 | 4601 | { |
078ea3df TM |
4602 | struct nfs_unlinkdata *data = task->tk_calldata; |
4603 | struct nfs_removeres *res = &data->res; | |
e4eff1a6 | 4604 | |
14516c3a TM |
4605 | if (!nfs4_sequence_done(task, &res->seq_res)) |
4606 | return 0; | |
8478eaa1 N |
4607 | if (nfs4_async_handle_error(task, res->server, NULL, |
4608 | &data->timeout) == -EAGAIN) | |
e4eff1a6 | 4609 | return 0; |
c40d52fe | 4610 | if (task->tk_status == 0) |
5636ec4e TM |
4611 | update_changeattr(dir, &res->cinfo, |
4612 | res->dir_attr->time_start, 0); | |
e4eff1a6 | 4613 | return 1; |
1da177e4 LT |
4614 | } |
4615 | ||
f2c2c552 TM |
4616 | static void nfs4_proc_rename_setup(struct rpc_message *msg, |
4617 | struct dentry *old_dentry, | |
4618 | struct dentry *new_dentry) | |
d3d4152a | 4619 | { |
d3d4152a JL |
4620 | struct nfs_renameargs *arg = msg->rpc_argp; |
4621 | struct nfs_renameres *res = msg->rpc_resp; | |
f2c2c552 TM |
4622 | struct inode *old_inode = d_inode(old_dentry); |
4623 | struct inode *new_inode = d_inode(new_dentry); | |
d3d4152a | 4624 | |
f2c2c552 | 4625 | if (old_inode) |
c01d3645 | 4626 | nfs4_inode_make_writeable(old_inode); |
f2c2c552 TM |
4627 | if (new_inode) |
4628 | nfs4_inode_return_delegation(new_inode); | |
d3d4152a | 4629 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME]; |
f2c2c552 | 4630 | res->server = NFS_SB(old_dentry->d_sb); |
fba83f34 | 4631 | nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0); |
d3d4152a JL |
4632 | } |
4633 | ||
c6bfa1a1 BS |
4634 | static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data) |
4635 | { | |
42e1cca7 | 4636 | nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client, |
d9afbd1b TM |
4637 | &data->args.seq_args, |
4638 | &data->res.seq_res, | |
4639 | task); | |
d3d4152a JL |
4640 | } |
4641 | ||
4642 | static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir, | |
4643 | struct inode *new_dir) | |
4644 | { | |
fbc6f7c2 TM |
4645 | struct nfs_renamedata *data = task->tk_calldata; |
4646 | struct nfs_renameres *res = &data->res; | |
d3d4152a JL |
4647 | |
4648 | if (!nfs4_sequence_done(task, &res->seq_res)) | |
4649 | return 0; | |
8478eaa1 | 4650 | if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN) |
d3d4152a JL |
4651 | return 0; |
4652 | ||
c733c49c | 4653 | if (task->tk_status == 0) { |
5636ec4e TM |
4654 | if (new_dir != old_dir) { |
4655 | /* Note: If we moved a directory, nlink will change */ | |
4656 | update_changeattr(old_dir, &res->old_cinfo, | |
4657 | res->old_fattr->time_start, | |
4658 | NFS_INO_INVALID_OTHER); | |
4659 | update_changeattr(new_dir, &res->new_cinfo, | |
4660 | res->new_fattr->time_start, | |
4661 | NFS_INO_INVALID_OTHER); | |
4662 | } else | |
4663 | update_changeattr(old_dir, &res->old_cinfo, | |
4664 | res->old_fattr->time_start, | |
4665 | 0); | |
c733c49c | 4666 | } |
d3d4152a JL |
4667 | return 1; |
4668 | } | |
4669 | ||
beffb8fe | 4670 | static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name) |
1da177e4 | 4671 | { |
91ba2eee | 4672 | struct nfs_server *server = NFS_SERVER(inode); |
2f28dc38 | 4673 | __u32 bitmask[NFS4_BITMASK_SZ]; |
1da177e4 LT |
4674 | struct nfs4_link_arg arg = { |
4675 | .fh = NFS_FH(inode), | |
4676 | .dir_fh = NFS_FH(dir), | |
4677 | .name = name, | |
2f28dc38 | 4678 | .bitmask = bitmask, |
91ba2eee | 4679 | }; |
91ba2eee TM |
4680 | struct nfs4_link_res res = { |
4681 | .server = server, | |
1775fd3e | 4682 | .label = NULL, |
1da177e4 | 4683 | }; |
1da177e4 LT |
4684 | struct rpc_message msg = { |
4685 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK], | |
4686 | .rpc_argp = &arg, | |
91ba2eee | 4687 | .rpc_resp = &res, |
1da177e4 | 4688 | }; |
136f2627 TM |
4689 | int status = -ENOMEM; |
4690 | ||
4691 | res.fattr = nfs_alloc_fattr(); | |
778d2817 | 4692 | if (res.fattr == NULL) |
136f2627 | 4693 | goto out; |
1da177e4 | 4694 | |
14c43f76 DQ |
4695 | res.label = nfs4_label_alloc(server, GFP_KERNEL); |
4696 | if (IS_ERR(res.label)) { | |
4697 | status = PTR_ERR(res.label); | |
4698 | goto out; | |
4699 | } | |
4700 | ||
c01d3645 | 4701 | nfs4_inode_make_writeable(inode); |
2f28dc38 | 4702 | nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode); |
9f768272 | 4703 | |
7c513058 | 4704 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
91ba2eee | 4705 | if (!status) { |
5636ec4e | 4706 | update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0); |
aa9c2669 DQ |
4707 | status = nfs_post_op_update_inode(inode, res.fattr); |
4708 | if (!status) | |
4709 | nfs_setsecurity(inode, res.fattr, res.label); | |
91ba2eee | 4710 | } |
14c43f76 DQ |
4711 | |
4712 | ||
4713 | nfs4_label_free(res.label); | |
4714 | ||
136f2627 | 4715 | out: |
136f2627 | 4716 | nfs_free_fattr(res.fattr); |
1da177e4 LT |
4717 | return status; |
4718 | } | |
4719 | ||
beffb8fe | 4720 | static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name) |
1da177e4 | 4721 | { |
0688e64b TM |
4722 | struct nfs4_exception exception = { |
4723 | .interruptible = true, | |
4724 | }; | |
1da177e4 LT |
4725 | int err; |
4726 | do { | |
4727 | err = nfs4_handle_exception(NFS_SERVER(inode), | |
4728 | _nfs4_proc_link(inode, dir, name), | |
4729 | &exception); | |
4730 | } while (exception.retry); | |
4731 | return err; | |
4732 | } | |
4733 | ||
57dc9a57 TM |
4734 | struct nfs4_createdata { |
4735 | struct rpc_message msg; | |
4736 | struct nfs4_create_arg arg; | |
4737 | struct nfs4_create_res res; | |
4738 | struct nfs_fh fh; | |
4739 | struct nfs_fattr fattr; | |
1775fd3e | 4740 | struct nfs4_label *label; |
57dc9a57 TM |
4741 | }; |
4742 | ||
4743 | static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir, | |
beffb8fe | 4744 | const struct qstr *name, struct iattr *sattr, u32 ftype) |
57dc9a57 TM |
4745 | { |
4746 | struct nfs4_createdata *data; | |
4747 | ||
4748 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
4749 | if (data != NULL) { | |
4750 | struct nfs_server *server = NFS_SERVER(dir); | |
4751 | ||
14c43f76 DQ |
4752 | data->label = nfs4_label_alloc(server, GFP_KERNEL); |
4753 | if (IS_ERR(data->label)) | |
4754 | goto out_free; | |
4755 | ||
57dc9a57 TM |
4756 | data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE]; |
4757 | data->msg.rpc_argp = &data->arg; | |
4758 | data->msg.rpc_resp = &data->res; | |
4759 | data->arg.dir_fh = NFS_FH(dir); | |
4760 | data->arg.server = server; | |
4761 | data->arg.name = name; | |
4762 | data->arg.attrs = sattr; | |
4763 | data->arg.ftype = ftype; | |
aa9c2669 | 4764 | data->arg.bitmask = nfs4_bitmask(server, data->label); |
dff25ddb | 4765 | data->arg.umask = current_umask(); |
57dc9a57 TM |
4766 | data->res.server = server; |
4767 | data->res.fh = &data->fh; | |
4768 | data->res.fattr = &data->fattr; | |
1775fd3e | 4769 | data->res.label = data->label; |
57dc9a57 | 4770 | nfs_fattr_init(data->res.fattr); |
57dc9a57 TM |
4771 | } |
4772 | return data; | |
14c43f76 DQ |
4773 | out_free: |
4774 | kfree(data); | |
4775 | return NULL; | |
57dc9a57 TM |
4776 | } |
4777 | ||
4778 | static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data) | |
4779 | { | |
7c513058 | 4780 | int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg, |
e73b83f2 | 4781 | &data->arg.seq_args, &data->res.seq_res, 1); |
57dc9a57 | 4782 | if (status == 0) { |
3c591175 TM |
4783 | spin_lock(&dir->i_lock); |
4784 | update_changeattr_locked(dir, &data->res.dir_cinfo, | |
5636ec4e | 4785 | data->res.fattr->time_start, 0); |
3c591175 TM |
4786 | /* Creating a directory bumps nlink in the parent */ |
4787 | if (data->arg.ftype == NF4DIR) | |
4788 | nfs4_inc_nlink_locked(dir); | |
4789 | spin_unlock(&dir->i_lock); | |
1775fd3e | 4790 | status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label); |
57dc9a57 TM |
4791 | } |
4792 | return status; | |
4793 | } | |
4794 | ||
4795 | static void nfs4_free_createdata(struct nfs4_createdata *data) | |
4796 | { | |
14c43f76 | 4797 | nfs4_label_free(data->label); |
57dc9a57 TM |
4798 | kfree(data); |
4799 | } | |
4800 | ||
4f390c15 | 4801 | static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, |
1775fd3e DQ |
4802 | struct page *page, unsigned int len, struct iattr *sattr, |
4803 | struct nfs4_label *label) | |
1da177e4 | 4804 | { |
57dc9a57 TM |
4805 | struct nfs4_createdata *data; |
4806 | int status = -ENAMETOOLONG; | |
1da177e4 | 4807 | |
94a6d753 | 4808 | if (len > NFS4_MAXPATHLEN) |
57dc9a57 | 4809 | goto out; |
4f390c15 | 4810 | |
57dc9a57 TM |
4811 | status = -ENOMEM; |
4812 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK); | |
4813 | if (data == NULL) | |
4814 | goto out; | |
4815 | ||
4816 | data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK]; | |
4817 | data->arg.u.symlink.pages = &page; | |
4818 | data->arg.u.symlink.len = len; | |
1775fd3e | 4819 | data->arg.label = label; |
1da177e4 | 4820 | |
57dc9a57 TM |
4821 | status = nfs4_do_create(dir, dentry, data); |
4822 | ||
4823 | nfs4_free_createdata(data); | |
4824 | out: | |
1da177e4 LT |
4825 | return status; |
4826 | } | |
4827 | ||
4f390c15 | 4828 | static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, |
94a6d753 | 4829 | struct page *page, unsigned int len, struct iattr *sattr) |
1da177e4 | 4830 | { |
0688e64b TM |
4831 | struct nfs4_exception exception = { |
4832 | .interruptible = true, | |
4833 | }; | |
aa9c2669 | 4834 | struct nfs4_label l, *label = NULL; |
1da177e4 | 4835 | int err; |
aa9c2669 DQ |
4836 | |
4837 | label = nfs4_label_init_security(dir, dentry, sattr, &l); | |
4838 | ||
1da177e4 | 4839 | do { |
078ea3df TM |
4840 | err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label); |
4841 | trace_nfs4_symlink(dir, &dentry->d_name, err); | |
4842 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
1da177e4 LT |
4843 | &exception); |
4844 | } while (exception.retry); | |
aa9c2669 DQ |
4845 | |
4846 | nfs4_label_release_security(label); | |
1da177e4 LT |
4847 | return err; |
4848 | } | |
4849 | ||
4850 | static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, | |
1775fd3e | 4851 | struct iattr *sattr, struct nfs4_label *label) |
1da177e4 | 4852 | { |
57dc9a57 TM |
4853 | struct nfs4_createdata *data; |
4854 | int status = -ENOMEM; | |
1da177e4 | 4855 | |
57dc9a57 TM |
4856 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR); |
4857 | if (data == NULL) | |
4858 | goto out; | |
4859 | ||
1775fd3e | 4860 | data->arg.label = label; |
57dc9a57 TM |
4861 | status = nfs4_do_create(dir, dentry, data); |
4862 | ||
4863 | nfs4_free_createdata(data); | |
4864 | out: | |
1da177e4 LT |
4865 | return status; |
4866 | } | |
4867 | ||
4868 | static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, | |
4869 | struct iattr *sattr) | |
4870 | { | |
dff25ddb | 4871 | struct nfs_server *server = NFS_SERVER(dir); |
0688e64b TM |
4872 | struct nfs4_exception exception = { |
4873 | .interruptible = true, | |
4874 | }; | |
aa9c2669 | 4875 | struct nfs4_label l, *label = NULL; |
1da177e4 | 4876 | int err; |
a8a5da99 | 4877 | |
aa9c2669 DQ |
4878 | label = nfs4_label_init_security(dir, dentry, sattr, &l); |
4879 | ||
dff25ddb AG |
4880 | if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) |
4881 | sattr->ia_mode &= ~current_umask(); | |
1da177e4 | 4882 | do { |
078ea3df TM |
4883 | err = _nfs4_proc_mkdir(dir, dentry, sattr, label); |
4884 | trace_nfs4_mkdir(dir, &dentry->d_name, err); | |
4885 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
1da177e4 LT |
4886 | &exception); |
4887 | } while (exception.retry); | |
aa9c2669 DQ |
4888 | nfs4_label_release_security(label); |
4889 | ||
1da177e4 LT |
4890 | return err; |
4891 | } | |
4892 | ||
684f39b4 | 4893 | static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred, |
a7a3b1e9 | 4894 | u64 cookie, struct page **pages, unsigned int count, bool plus) |
1da177e4 | 4895 | { |
2b0143b5 | 4896 | struct inode *dir = d_inode(dentry); |
1da177e4 LT |
4897 | struct nfs4_readdir_arg args = { |
4898 | .fh = NFS_FH(dir), | |
56e4ebf8 | 4899 | .pages = pages, |
1da177e4 LT |
4900 | .pgbase = 0, |
4901 | .count = count, | |
2b0143b5 | 4902 | .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask, |
82f2e547 | 4903 | .plus = plus, |
1da177e4 LT |
4904 | }; |
4905 | struct nfs4_readdir_res res; | |
4906 | struct rpc_message msg = { | |
4907 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR], | |
4908 | .rpc_argp = &args, | |
4909 | .rpc_resp = &res, | |
4910 | .rpc_cred = cred, | |
4911 | }; | |
4912 | int status; | |
4913 | ||
6de1472f AV |
4914 | dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__, |
4915 | dentry, | |
eadf4598 | 4916 | (unsigned long long)cookie); |
c3f52af3 | 4917 | nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args); |
1da177e4 | 4918 | res.pgbase = args.pgbase; |
7c513058 | 4919 | status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0); |
ac396128 | 4920 | if (status >= 0) { |
c3f52af3 | 4921 | memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE); |
ac396128 TM |
4922 | status += args.pgbase; |
4923 | } | |
c4812998 TM |
4924 | |
4925 | nfs_invalidate_atime(dir); | |
4926 | ||
3110ff80 | 4927 | dprintk("%s: returns %d\n", __func__, status); |
1da177e4 LT |
4928 | return status; |
4929 | } | |
4930 | ||
684f39b4 | 4931 | static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred, |
a7a3b1e9 | 4932 | u64 cookie, struct page **pages, unsigned int count, bool plus) |
1da177e4 | 4933 | { |
0688e64b TM |
4934 | struct nfs4_exception exception = { |
4935 | .interruptible = true, | |
4936 | }; | |
1da177e4 LT |
4937 | int err; |
4938 | do { | |
c1578b76 TM |
4939 | err = _nfs4_proc_readdir(dentry, cred, cookie, |
4940 | pages, count, plus); | |
2b0143b5 DH |
4941 | trace_nfs4_readdir(d_inode(dentry), err); |
4942 | err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err, | |
1da177e4 LT |
4943 | &exception); |
4944 | } while (exception.retry); | |
4945 | return err; | |
4946 | } | |
4947 | ||
4948 | static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, | |
aa9c2669 | 4949 | struct iattr *sattr, struct nfs4_label *label, dev_t rdev) |
1da177e4 | 4950 | { |
57dc9a57 TM |
4951 | struct nfs4_createdata *data; |
4952 | int mode = sattr->ia_mode; | |
4953 | int status = -ENOMEM; | |
1da177e4 | 4954 | |
57dc9a57 TM |
4955 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK); |
4956 | if (data == NULL) | |
4957 | goto out; | |
4958 | ||
1da177e4 | 4959 | if (S_ISFIFO(mode)) |
57dc9a57 | 4960 | data->arg.ftype = NF4FIFO; |
1da177e4 | 4961 | else if (S_ISBLK(mode)) { |
57dc9a57 TM |
4962 | data->arg.ftype = NF4BLK; |
4963 | data->arg.u.device.specdata1 = MAJOR(rdev); | |
4964 | data->arg.u.device.specdata2 = MINOR(rdev); | |
1da177e4 LT |
4965 | } |
4966 | else if (S_ISCHR(mode)) { | |
57dc9a57 TM |
4967 | data->arg.ftype = NF4CHR; |
4968 | data->arg.u.device.specdata1 = MAJOR(rdev); | |
4969 | data->arg.u.device.specdata2 = MINOR(rdev); | |
4ea8fed5 TM |
4970 | } else if (!S_ISSOCK(mode)) { |
4971 | status = -EINVAL; | |
4972 | goto out_free; | |
1da177e4 | 4973 | } |
1775fd3e | 4974 | |
aa9c2669 | 4975 | data->arg.label = label; |
57dc9a57 | 4976 | status = nfs4_do_create(dir, dentry, data); |
4ea8fed5 | 4977 | out_free: |
57dc9a57 TM |
4978 | nfs4_free_createdata(data); |
4979 | out: | |
1da177e4 LT |
4980 | return status; |
4981 | } | |
4982 | ||
4983 | static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, | |
4984 | struct iattr *sattr, dev_t rdev) | |
4985 | { | |
dff25ddb | 4986 | struct nfs_server *server = NFS_SERVER(dir); |
0688e64b TM |
4987 | struct nfs4_exception exception = { |
4988 | .interruptible = true, | |
4989 | }; | |
aa9c2669 | 4990 | struct nfs4_label l, *label = NULL; |
1da177e4 | 4991 | int err; |
a8a5da99 | 4992 | |
aa9c2669 DQ |
4993 | label = nfs4_label_init_security(dir, dentry, sattr, &l); |
4994 | ||
dff25ddb AG |
4995 | if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) |
4996 | sattr->ia_mode &= ~current_umask(); | |
1da177e4 | 4997 | do { |
078ea3df TM |
4998 | err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev); |
4999 | trace_nfs4_mknod(dir, &dentry->d_name, err); | |
5000 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
1da177e4 LT |
5001 | &exception); |
5002 | } while (exception.retry); | |
aa9c2669 DQ |
5003 | |
5004 | nfs4_label_release_security(label); | |
5005 | ||
1da177e4 LT |
5006 | return err; |
5007 | } | |
5008 | ||
5009 | static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, | |
5010 | struct nfs_fsstat *fsstat) | |
5011 | { | |
5012 | struct nfs4_statfs_arg args = { | |
5013 | .fh = fhandle, | |
5014 | .bitmask = server->attr_bitmask, | |
5015 | }; | |
24ad148a BH |
5016 | struct nfs4_statfs_res res = { |
5017 | .fsstat = fsstat, | |
5018 | }; | |
1da177e4 LT |
5019 | struct rpc_message msg = { |
5020 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS], | |
5021 | .rpc_argp = &args, | |
24ad148a | 5022 | .rpc_resp = &res, |
1da177e4 LT |
5023 | }; |
5024 | ||
0e574af1 | 5025 | nfs_fattr_init(fsstat->fattr); |
7c513058 | 5026 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
5027 | } |
5028 | ||
5029 | static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat) | |
5030 | { | |
0688e64b TM |
5031 | struct nfs4_exception exception = { |
5032 | .interruptible = true, | |
5033 | }; | |
1da177e4 LT |
5034 | int err; |
5035 | do { | |
5036 | err = nfs4_handle_exception(server, | |
5037 | _nfs4_proc_statfs(server, fhandle, fsstat), | |
5038 | &exception); | |
5039 | } while (exception.retry); | |
5040 | return err; | |
5041 | } | |
5042 | ||
5043 | static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, | |
5044 | struct nfs_fsinfo *fsinfo) | |
5045 | { | |
5046 | struct nfs4_fsinfo_arg args = { | |
5047 | .fh = fhandle, | |
5048 | .bitmask = server->attr_bitmask, | |
5049 | }; | |
3dda5e43 BH |
5050 | struct nfs4_fsinfo_res res = { |
5051 | .fsinfo = fsinfo, | |
5052 | }; | |
1da177e4 LT |
5053 | struct rpc_message msg = { |
5054 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO], | |
5055 | .rpc_argp = &args, | |
3dda5e43 | 5056 | .rpc_resp = &res, |
1da177e4 LT |
5057 | }; |
5058 | ||
7c513058 | 5059 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
5060 | } |
5061 | ||
5062 | static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) | |
5063 | { | |
0688e64b TM |
5064 | struct nfs4_exception exception = { |
5065 | .interruptible = true, | |
5066 | }; | |
1da177e4 LT |
5067 | int err; |
5068 | ||
5069 | do { | |
83ca7f5a | 5070 | err = _nfs4_do_fsinfo(server, fhandle, fsinfo); |
b5f875a9 | 5071 | trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err); |
83ca7f5a | 5072 | if (err == 0) { |
7dc2993a | 5073 | nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ); |
83ca7f5a CL |
5074 | break; |
5075 | } | |
5076 | err = nfs4_handle_exception(server, err, &exception); | |
1da177e4 LT |
5077 | } while (exception.retry); |
5078 | return err; | |
5079 | } | |
5080 | ||
5081 | static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) | |
5082 | { | |
e38eb650 BS |
5083 | int error; |
5084 | ||
0e574af1 | 5085 | nfs_fattr_init(fsinfo->fattr); |
e38eb650 | 5086 | error = nfs4_do_fsinfo(server, fhandle, fsinfo); |
dc182549 PT |
5087 | if (error == 0) { |
5088 | /* block layout checks this! */ | |
5089 | server->pnfs_blksize = fsinfo->blksize; | |
ca440c38 | 5090 | set_pnfs_layoutdriver(server, fhandle, fsinfo); |
dc182549 | 5091 | } |
e38eb650 BS |
5092 | |
5093 | return error; | |
1da177e4 LT |
5094 | } |
5095 | ||
5096 | static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, | |
5097 | struct nfs_pathconf *pathconf) | |
5098 | { | |
5099 | struct nfs4_pathconf_arg args = { | |
5100 | .fh = fhandle, | |
5101 | .bitmask = server->attr_bitmask, | |
5102 | }; | |
d45b2989 BH |
5103 | struct nfs4_pathconf_res res = { |
5104 | .pathconf = pathconf, | |
5105 | }; | |
1da177e4 LT |
5106 | struct rpc_message msg = { |
5107 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF], | |
5108 | .rpc_argp = &args, | |
d45b2989 | 5109 | .rpc_resp = &res, |
1da177e4 LT |
5110 | }; |
5111 | ||
5112 | /* None of the pathconf attributes are mandatory to implement */ | |
5113 | if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) { | |
5114 | memset(pathconf, 0, sizeof(*pathconf)); | |
5115 | return 0; | |
5116 | } | |
5117 | ||
0e574af1 | 5118 | nfs_fattr_init(pathconf->fattr); |
7c513058 | 5119 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
5120 | } |
5121 | ||
5122 | static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, | |
5123 | struct nfs_pathconf *pathconf) | |
5124 | { | |
0688e64b TM |
5125 | struct nfs4_exception exception = { |
5126 | .interruptible = true, | |
5127 | }; | |
1da177e4 LT |
5128 | int err; |
5129 | ||
5130 | do { | |
5131 | err = nfs4_handle_exception(server, | |
5132 | _nfs4_proc_pathconf(server, fhandle, pathconf), | |
5133 | &exception); | |
5134 | } while (exception.retry); | |
5135 | return err; | |
5136 | } | |
5137 | ||
5521abfd | 5138 | int nfs4_set_rw_stateid(nfs4_stateid *stateid, |
9b206149 TM |
5139 | const struct nfs_open_context *ctx, |
5140 | const struct nfs_lock_context *l_ctx, | |
5141 | fmode_t fmode) | |
5142 | { | |
17393475 | 5143 | return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL); |
9b206149 TM |
5144 | } |
5145 | EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid); | |
5146 | ||
5521abfd TM |
5147 | static bool nfs4_stateid_is_current(nfs4_stateid *stateid, |
5148 | const struct nfs_open_context *ctx, | |
5149 | const struct nfs_lock_context *l_ctx, | |
5150 | fmode_t fmode) | |
5151 | { | |
d49dd117 | 5152 | nfs4_stateid _current_stateid; |
5521abfd | 5153 | |
e1253be0 | 5154 | /* If the current stateid represents a lost lock, then exit */ |
d49dd117 | 5155 | if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO) |
e1253be0 | 5156 | return true; |
d49dd117 | 5157 | return nfs4_stateid_match(stateid, &_current_stateid); |
5521abfd TM |
5158 | } |
5159 | ||
5160 | static bool nfs4_error_stateid_expired(int err) | |
5161 | { | |
5162 | switch (err) { | |
5163 | case -NFS4ERR_DELEG_REVOKED: | |
5164 | case -NFS4ERR_ADMIN_REVOKED: | |
5165 | case -NFS4ERR_BAD_STATEID: | |
5166 | case -NFS4ERR_STALE_STATEID: | |
5167 | case -NFS4ERR_OLD_STATEID: | |
5168 | case -NFS4ERR_OPENMODE: | |
5169 | case -NFS4ERR_EXPIRED: | |
5170 | return true; | |
5171 | } | |
5172 | return false; | |
5173 | } | |
5174 | ||
d45f60c6 | 5175 | static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr) |
1da177e4 | 5176 | { |
d45f60c6 | 5177 | struct nfs_server *server = NFS_SERVER(hdr->inode); |
1da177e4 | 5178 | |
d45f60c6 | 5179 | trace_nfs4_read(hdr, task->tk_status); |
9c27869d TM |
5180 | if (task->tk_status < 0) { |
5181 | struct nfs4_exception exception = { | |
5182 | .inode = hdr->inode, | |
5183 | .state = hdr->args.context->state, | |
5184 | .stateid = &hdr->args.stateid, | |
5185 | }; | |
5186 | task->tk_status = nfs4_async_handle_exception(task, | |
5187 | server, task->tk_status, &exception); | |
5188 | if (exception.retry) { | |
5189 | rpc_restart_call_prepare(task); | |
5190 | return -EAGAIN; | |
5191 | } | |
1da177e4 | 5192 | } |
8850df99 | 5193 | |
1da177e4 | 5194 | if (task->tk_status > 0) |
d45f60c6 | 5195 | renew_lease(server, hdr->timestamp); |
ec06c096 | 5196 | return 0; |
1da177e4 LT |
5197 | } |
5198 | ||
5521abfd | 5199 | static bool nfs4_read_stateid_changed(struct rpc_task *task, |
3c6b899c | 5200 | struct nfs_pgio_args *args) |
5521abfd TM |
5201 | { |
5202 | ||
5203 | if (!nfs4_error_stateid_expired(task->tk_status) || | |
5204 | nfs4_stateid_is_current(&args->stateid, | |
5205 | args->context, | |
5206 | args->lock_context, | |
5207 | FMODE_READ)) | |
5208 | return false; | |
5209 | rpc_restart_call_prepare(task); | |
5210 | return true; | |
5211 | } | |
5212 | ||
d45f60c6 | 5213 | static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr) |
cbdabc7f AA |
5214 | { |
5215 | ||
5216 | dprintk("--> %s\n", __func__); | |
5217 | ||
d45f60c6 | 5218 | if (!nfs4_sequence_done(task, &hdr->res.seq_res)) |
cbdabc7f | 5219 | return -EAGAIN; |
d45f60c6 | 5220 | if (nfs4_read_stateid_changed(task, &hdr->args)) |
5521abfd | 5221 | return -EAGAIN; |
bfc505de TM |
5222 | if (task->tk_status > 0) |
5223 | nfs_invalidate_atime(hdr->inode); | |
d45f60c6 WAA |
5224 | return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : |
5225 | nfs4_read_done_cb(task, hdr); | |
cbdabc7f AA |
5226 | } |
5227 | ||
d45f60c6 WAA |
5228 | static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr, |
5229 | struct rpc_message *msg) | |
1da177e4 | 5230 | { |
d45f60c6 | 5231 | hdr->timestamp = jiffies; |
ca857cc1 TM |
5232 | if (!hdr->pgio_done_cb) |
5233 | hdr->pgio_done_cb = nfs4_read_done_cb; | |
bdc7f021 | 5234 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ]; |
fba83f34 | 5235 | nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0); |
1da177e4 LT |
5236 | } |
5237 | ||
d45f60c6 WAA |
5238 | static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task, |
5239 | struct nfs_pgio_header *hdr) | |
ea7c3303 | 5240 | { |
42e1cca7 | 5241 | if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client, |
d45f60c6 WAA |
5242 | &hdr->args.seq_args, |
5243 | &hdr->res.seq_res, | |
9b206149 | 5244 | task)) |
ef1820f9 | 5245 | return 0; |
d45f60c6 WAA |
5246 | if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, |
5247 | hdr->args.lock_context, | |
fbe77c30 | 5248 | hdr->rw_mode) == -EIO) |
ef1820f9 | 5249 | return -EIO; |
d45f60c6 | 5250 | if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) |
ef1820f9 N |
5251 | return -EIO; |
5252 | return 0; | |
1da177e4 LT |
5253 | } |
5254 | ||
d45f60c6 WAA |
5255 | static int nfs4_write_done_cb(struct rpc_task *task, |
5256 | struct nfs_pgio_header *hdr) | |
1da177e4 | 5257 | { |
d45f60c6 | 5258 | struct inode *inode = hdr->inode; |
8478eaa1 | 5259 | |
d45f60c6 | 5260 | trace_nfs4_write(hdr, task->tk_status); |
9c27869d TM |
5261 | if (task->tk_status < 0) { |
5262 | struct nfs4_exception exception = { | |
5263 | .inode = hdr->inode, | |
5264 | .state = hdr->args.context->state, | |
5265 | .stateid = &hdr->args.stateid, | |
5266 | }; | |
5267 | task->tk_status = nfs4_async_handle_exception(task, | |
5268 | NFS_SERVER(inode), task->tk_status, | |
5269 | &exception); | |
5270 | if (exception.retry) { | |
5271 | rpc_restart_call_prepare(task); | |
5272 | return -EAGAIN; | |
5273 | } | |
1da177e4 | 5274 | } |
4f9838c7 | 5275 | if (task->tk_status >= 0) { |
d45f60c6 | 5276 | renew_lease(NFS_SERVER(inode), hdr->timestamp); |
a08a8cd3 | 5277 | nfs_writeback_update_inode(hdr); |
4f9838c7 | 5278 | } |
788e7a89 | 5279 | return 0; |
1da177e4 LT |
5280 | } |
5281 | ||
5521abfd | 5282 | static bool nfs4_write_stateid_changed(struct rpc_task *task, |
3c6b899c | 5283 | struct nfs_pgio_args *args) |
5521abfd TM |
5284 | { |
5285 | ||
5286 | if (!nfs4_error_stateid_expired(task->tk_status) || | |
5287 | nfs4_stateid_is_current(&args->stateid, | |
5288 | args->context, | |
5289 | args->lock_context, | |
5290 | FMODE_WRITE)) | |
5291 | return false; | |
5292 | rpc_restart_call_prepare(task); | |
5293 | return true; | |
5294 | } | |
5295 | ||
d45f60c6 | 5296 | static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr) |
b029bc9b | 5297 | { |
d45f60c6 | 5298 | if (!nfs4_sequence_done(task, &hdr->res.seq_res)) |
b029bc9b | 5299 | return -EAGAIN; |
d45f60c6 | 5300 | if (nfs4_write_stateid_changed(task, &hdr->args)) |
5521abfd | 5301 | return -EAGAIN; |
d45f60c6 WAA |
5302 | return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : |
5303 | nfs4_write_done_cb(task, hdr); | |
b029bc9b FI |
5304 | } |
5305 | ||
5a37f851 | 5306 | static |
d45f60c6 | 5307 | bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr) |
a69aef14 | 5308 | { |
5a37f851 | 5309 | /* Don't request attributes for pNFS or O_DIRECT writes */ |
d45f60c6 | 5310 | if (hdr->ds_clp != NULL || hdr->dreq != NULL) |
5a37f851 TM |
5311 | return false; |
5312 | /* Otherwise, request attributes if and only if we don't hold | |
5313 | * a delegation | |
5314 | */ | |
011e2a7f | 5315 | return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0; |
a69aef14 | 5316 | } |
a69aef14 | 5317 | |
d45f60c6 | 5318 | static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr, |
fb91fb0e AS |
5319 | struct rpc_message *msg, |
5320 | struct rpc_clnt **clnt) | |
1da177e4 | 5321 | { |
d45f60c6 | 5322 | struct nfs_server *server = NFS_SERVER(hdr->inode); |
bdc7f021 | 5323 | |
d45f60c6 WAA |
5324 | if (!nfs4_write_need_cache_consistency_data(hdr)) { |
5325 | hdr->args.bitmask = NULL; | |
5326 | hdr->res.fattr = NULL; | |
7ffd1064 | 5327 | } else |
d45f60c6 | 5328 | hdr->args.bitmask = server->cache_consistency_bitmask; |
5a37f851 | 5329 | |
d45f60c6 WAA |
5330 | if (!hdr->pgio_done_cb) |
5331 | hdr->pgio_done_cb = nfs4_write_done_cb; | |
5332 | hdr->res.server = server; | |
5333 | hdr->timestamp = jiffies; | |
1da177e4 | 5334 | |
bdc7f021 | 5335 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE]; |
cd1b659d | 5336 | nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0); |
fb91fb0e | 5337 | nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr); |
1da177e4 LT |
5338 | } |
5339 | ||
0b7c0153 | 5340 | static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data) |
c6cb80d0 | 5341 | { |
42e1cca7 | 5342 | nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client, |
d9afbd1b TM |
5343 | &data->args.seq_args, |
5344 | &data->res.seq_res, | |
5345 | task); | |
1da177e4 LT |
5346 | } |
5347 | ||
0b7c0153 | 5348 | static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data) |
1da177e4 | 5349 | { |
1da177e4 | 5350 | struct inode *inode = data->inode; |
14516c3a | 5351 | |
cc668ab3 | 5352 | trace_nfs4_commit(data, task->tk_status); |
8478eaa1 N |
5353 | if (nfs4_async_handle_error(task, NFS_SERVER(inode), |
5354 | NULL, NULL) == -EAGAIN) { | |
d00c5d43 | 5355 | rpc_restart_call_prepare(task); |
788e7a89 | 5356 | return -EAGAIN; |
1da177e4 | 5357 | } |
788e7a89 | 5358 | return 0; |
1da177e4 LT |
5359 | } |
5360 | ||
0b7c0153 | 5361 | static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data) |
5f452431 FI |
5362 | { |
5363 | if (!nfs4_sequence_done(task, &data->res.seq_res)) | |
5364 | return -EAGAIN; | |
0b7c0153 | 5365 | return data->commit_done_cb(task, data); |
5f452431 FI |
5366 | } |
5367 | ||
e9ae1ee2 AS |
5368 | static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg, |
5369 | struct rpc_clnt **clnt) | |
1da177e4 | 5370 | { |
788e7a89 | 5371 | struct nfs_server *server = NFS_SERVER(data->inode); |
988b6dce | 5372 | |
0b7c0153 FI |
5373 | if (data->commit_done_cb == NULL) |
5374 | data->commit_done_cb = nfs4_commit_done_cb; | |
4f9838c7 | 5375 | data->res.server = server; |
bdc7f021 | 5376 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT]; |
fba83f34 | 5377 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0); |
e9ae1ee2 | 5378 | nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg); |
1da177e4 LT |
5379 | } |
5380 | ||
6b8d84e2 OK |
5381 | static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args, |
5382 | struct nfs_commitres *res) | |
5383 | { | |
5384 | struct inode *dst_inode = file_inode(dst); | |
5385 | struct nfs_server *server = NFS_SERVER(dst_inode); | |
5386 | struct rpc_message msg = { | |
5387 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT], | |
5388 | .rpc_argp = args, | |
5389 | .rpc_resp = res, | |
5390 | }; | |
5391 | ||
5392 | args->fh = NFS_FH(dst_inode); | |
5393 | return nfs4_call_sync(server->client, server, &msg, | |
5394 | &args->seq_args, &res->seq_res, 1); | |
5395 | } | |
5396 | ||
5397 | int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res) | |
5398 | { | |
5399 | struct nfs_commitargs args = { | |
5400 | .offset = offset, | |
5401 | .count = count, | |
5402 | }; | |
5403 | struct nfs_server *dst_server = NFS_SERVER(file_inode(dst)); | |
5404 | struct nfs4_exception exception = { }; | |
5405 | int status; | |
5406 | ||
5407 | do { | |
5408 | status = _nfs4_proc_commit(dst, &args, res); | |
5409 | status = nfs4_handle_exception(dst_server, status, &exception); | |
5410 | } while (exception.retry); | |
5411 | ||
5412 | return status; | |
5413 | } | |
5414 | ||
9bc4e3ca CL |
5415 | struct nfs4_renewdata { |
5416 | struct nfs_client *client; | |
5417 | unsigned long timestamp; | |
5418 | }; | |
5419 | ||
1da177e4 LT |
5420 | /* |
5421 | * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special | |
5422 | * standalone procedure for queueing an asynchronous RENEW. | |
5423 | */ | |
9bc4e3ca | 5424 | static void nfs4_renew_release(void *calldata) |
dc96aef9 | 5425 | { |
9bc4e3ca CL |
5426 | struct nfs4_renewdata *data = calldata; |
5427 | struct nfs_client *clp = data->client; | |
dc96aef9 | 5428 | |
212bf41d | 5429 | if (refcount_read(&clp->cl_count) > 1) |
0851de06 AB |
5430 | nfs4_schedule_state_renewal(clp); |
5431 | nfs_put_client(clp); | |
9bc4e3ca | 5432 | kfree(data); |
dc96aef9 AB |
5433 | } |
5434 | ||
9bc4e3ca | 5435 | static void nfs4_renew_done(struct rpc_task *task, void *calldata) |
1da177e4 | 5436 | { |
9bc4e3ca CL |
5437 | struct nfs4_renewdata *data = calldata; |
5438 | struct nfs_client *clp = data->client; | |
5439 | unsigned long timestamp = data->timestamp; | |
1da177e4 | 5440 | |
c6d01c6f | 5441 | trace_nfs4_renew_async(clp, task->tk_status); |
f8aba1e8 CL |
5442 | switch (task->tk_status) { |
5443 | case 0: | |
5444 | break; | |
5445 | case -NFS4ERR_LEASE_MOVED: | |
5446 | nfs4_schedule_lease_moved_recovery(clp); | |
5447 | break; | |
5448 | default: | |
95baa25c | 5449 | /* Unless we're shutting down, schedule state recovery! */ |
042b60be TM |
5450 | if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0) |
5451 | return; | |
5452 | if (task->tk_status != NFS4ERR_CB_PATH_DOWN) { | |
0400a6b0 | 5453 | nfs4_schedule_lease_recovery(clp); |
042b60be TM |
5454 | return; |
5455 | } | |
5456 | nfs4_schedule_path_down_recovery(clp); | |
1da177e4 | 5457 | } |
452e9352 | 5458 | do_renew_lease(clp, timestamp); |
1da177e4 LT |
5459 | } |
5460 | ||
963d8fe5 TM |
5461 | static const struct rpc_call_ops nfs4_renew_ops = { |
5462 | .rpc_call_done = nfs4_renew_done, | |
dc96aef9 | 5463 | .rpc_release = nfs4_renew_release, |
963d8fe5 TM |
5464 | }; |
5465 | ||
a52458b4 | 5466 | static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags) |
1da177e4 LT |
5467 | { |
5468 | struct rpc_message msg = { | |
5469 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], | |
5470 | .rpc_argp = clp, | |
b4454fe1 | 5471 | .rpc_cred = cred, |
1da177e4 | 5472 | }; |
9bc4e3ca | 5473 | struct nfs4_renewdata *data; |
1da177e4 | 5474 | |
2f60ea6b TM |
5475 | if (renew_flags == 0) |
5476 | return 0; | |
212bf41d | 5477 | if (!refcount_inc_not_zero(&clp->cl_count)) |
0851de06 | 5478 | return -EIO; |
b569ad34 | 5479 | data = kmalloc(sizeof(*data), GFP_NOFS); |
5c737cb2 DW |
5480 | if (data == NULL) { |
5481 | nfs_put_client(clp); | |
9bc4e3ca | 5482 | return -ENOMEM; |
5c737cb2 | 5483 | } |
9bc4e3ca CL |
5484 | data->client = clp; |
5485 | data->timestamp = jiffies; | |
bc7a05ca | 5486 | return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT, |
9bc4e3ca | 5487 | &nfs4_renew_ops, data); |
1da177e4 LT |
5488 | } |
5489 | ||
a52458b4 | 5490 | static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred) |
1da177e4 LT |
5491 | { |
5492 | struct rpc_message msg = { | |
5493 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], | |
5494 | .rpc_argp = clp, | |
b4454fe1 | 5495 | .rpc_cred = cred, |
1da177e4 LT |
5496 | }; |
5497 | unsigned long now = jiffies; | |
5498 | int status; | |
5499 | ||
bc7a05ca | 5500 | status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
1da177e4 LT |
5501 | if (status < 0) |
5502 | return status; | |
452e9352 | 5503 | do_renew_lease(clp, now); |
1da177e4 LT |
5504 | return 0; |
5505 | } | |
5506 | ||
aa1870af BF |
5507 | static inline int nfs4_server_supports_acls(struct nfs_server *server) |
5508 | { | |
7dd7d959 | 5509 | return server->caps & NFS_CAP_ACLS; |
aa1870af BF |
5510 | } |
5511 | ||
21f498c2 TM |
5512 | /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that |
5513 | * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on | |
aa1870af BF |
5514 | * the stack. |
5515 | */ | |
21f498c2 | 5516 | #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE) |
aa1870af | 5517 | |
e9e3d724 | 5518 | static int buf_to_pages_noslab(const void *buf, size_t buflen, |
8fbcf237 | 5519 | struct page **pages) |
e9e3d724 NH |
5520 | { |
5521 | struct page *newpage, **spages; | |
5522 | int rc = 0; | |
5523 | size_t len; | |
5524 | spages = pages; | |
5525 | ||
5526 | do { | |
21f498c2 | 5527 | len = min_t(size_t, PAGE_SIZE, buflen); |
e9e3d724 NH |
5528 | newpage = alloc_page(GFP_KERNEL); |
5529 | ||
5530 | if (newpage == NULL) | |
5531 | goto unwind; | |
5532 | memcpy(page_address(newpage), buf, len); | |
d9b67e1e AS |
5533 | buf += len; |
5534 | buflen -= len; | |
e9e3d724 NH |
5535 | *pages++ = newpage; |
5536 | rc++; | |
5537 | } while (buflen != 0); | |
5538 | ||
5539 | return rc; | |
5540 | ||
5541 | unwind: | |
5542 | for(; rc > 0; rc--) | |
5543 | __free_page(spages[rc-1]); | |
5544 | return -ENOMEM; | |
5545 | } | |
5546 | ||
e50a1c2e BF |
5547 | struct nfs4_cached_acl { |
5548 | int cached; | |
5549 | size_t len; | |
5601cda8 | 5550 | char data[]; |
e50a1c2e BF |
5551 | }; |
5552 | ||
5553 | static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl) | |
5554 | { | |
5555 | struct nfs_inode *nfsi = NFS_I(inode); | |
5556 | ||
5557 | spin_lock(&inode->i_lock); | |
5558 | kfree(nfsi->nfs4_acl); | |
5559 | nfsi->nfs4_acl = acl; | |
5560 | spin_unlock(&inode->i_lock); | |
5561 | } | |
5562 | ||
5563 | static void nfs4_zap_acl_attr(struct inode *inode) | |
5564 | { | |
5565 | nfs4_set_cached_acl(inode, NULL); | |
5566 | } | |
5567 | ||
5568 | static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen) | |
5569 | { | |
5570 | struct nfs_inode *nfsi = NFS_I(inode); | |
5571 | struct nfs4_cached_acl *acl; | |
5572 | int ret = -ENOENT; | |
5573 | ||
5574 | spin_lock(&inode->i_lock); | |
5575 | acl = nfsi->nfs4_acl; | |
5576 | if (acl == NULL) | |
5577 | goto out; | |
5578 | if (buf == NULL) /* user is just asking for length */ | |
5579 | goto out_len; | |
5580 | if (acl->cached == 0) | |
5581 | goto out; | |
5582 | ret = -ERANGE; /* see getxattr(2) man page */ | |
5583 | if (acl->len > buflen) | |
5584 | goto out; | |
5585 | memcpy(buf, acl->data, acl->len); | |
5586 | out_len: | |
5587 | ret = acl->len; | |
5588 | out: | |
5589 | spin_unlock(&inode->i_lock); | |
5590 | return ret; | |
5591 | } | |
5592 | ||
5794d21e | 5593 | static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len) |
e50a1c2e BF |
5594 | { |
5595 | struct nfs4_cached_acl *acl; | |
b291f1b1 | 5596 | size_t buflen = sizeof(*acl) + acl_len; |
e50a1c2e | 5597 | |
1f1ea6c2 | 5598 | if (buflen <= PAGE_SIZE) { |
b291f1b1 | 5599 | acl = kmalloc(buflen, GFP_KERNEL); |
e50a1c2e BF |
5600 | if (acl == NULL) |
5601 | goto out; | |
5602 | acl->cached = 1; | |
5794d21e | 5603 | _copy_from_pages(acl->data, pages, pgbase, acl_len); |
e50a1c2e BF |
5604 | } else { |
5605 | acl = kmalloc(sizeof(*acl), GFP_KERNEL); | |
5606 | if (acl == NULL) | |
5607 | goto out; | |
5608 | acl->cached = 0; | |
5609 | } | |
5610 | acl->len = acl_len; | |
5611 | out: | |
5612 | nfs4_set_cached_acl(inode, acl); | |
5613 | } | |
5614 | ||
bf118a34 AA |
5615 | /* |
5616 | * The getxattr API returns the required buffer length when called with a | |
5617 | * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating | |
5618 | * the required buf. On a NULL buf, we send a page of data to the server | |
5619 | * guessing that the ACL request can be serviced by a page. If so, we cache | |
5620 | * up to the page of ACL data, and the 2nd call to getxattr is serviced by | |
5621 | * the cache. If not so, we throw away the page, and cache the required | |
5622 | * length. The next getxattr call will then produce another round trip to | |
5623 | * the server, this time with the input buf of the required size. | |
5624 | */ | |
16b4289c | 5625 | static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) |
aa1870af | 5626 | { |
62a1573f | 5627 | struct page **pages; |
aa1870af BF |
5628 | struct nfs_getaclargs args = { |
5629 | .fh = NFS_FH(inode), | |
aa1870af BF |
5630 | .acl_len = buflen, |
5631 | }; | |
663c79b3 BH |
5632 | struct nfs_getaclres res = { |
5633 | .acl_len = buflen, | |
5634 | }; | |
aa1870af BF |
5635 | struct rpc_message msg = { |
5636 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL], | |
5637 | .rpc_argp = &args, | |
663c79b3 | 5638 | .rpc_resp = &res, |
aa1870af | 5639 | }; |
62a1573f | 5640 | unsigned int npages; |
21f498c2 | 5641 | int ret = -ENOMEM, i; |
62a1573f OK |
5642 | struct nfs_server *server = NFS_SERVER(inode); |
5643 | ||
5644 | if (buflen == 0) | |
5645 | buflen = server->rsize; | |
5646 | ||
5647 | npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1; | |
5648 | pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS); | |
5649 | if (!pages) | |
5650 | return -ENOMEM; | |
aa1870af | 5651 | |
62a1573f | 5652 | args.acl_pages = pages; |
5a006899 | 5653 | |
bf118a34 AA |
5654 | for (i = 0; i < npages; i++) { |
5655 | pages[i] = alloc_page(GFP_KERNEL); | |
5656 | if (!pages[i]) | |
5657 | goto out_free; | |
e50a1c2e | 5658 | } |
5a006899 SP |
5659 | |
5660 | /* for decoding across pages */ | |
5661 | res.acl_scratch = alloc_page(GFP_KERNEL); | |
5662 | if (!res.acl_scratch) | |
5663 | goto out_free; | |
5664 | ||
bf118a34 | 5665 | args.acl_len = npages * PAGE_SIZE; |
5a006899 | 5666 | |
de040bec | 5667 | dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n", |
bf118a34 AA |
5668 | __func__, buf, buflen, npages, args.acl_len); |
5669 | ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), | |
5670 | &msg, &args.seq_args, &res.seq_res, 0); | |
e50a1c2e BF |
5671 | if (ret) |
5672 | goto out_free; | |
bf118a34 | 5673 | |
1f1ea6c2 TM |
5674 | /* Handle the case where the passed-in buffer is too short */ |
5675 | if (res.acl_flags & NFS4_ACL_TRUNC) { | |
5676 | /* Did the user only issue a request for the acl length? */ | |
5677 | if (buf == NULL) | |
5678 | goto out_ok; | |
e50a1c2e | 5679 | ret = -ERANGE; |
1f1ea6c2 | 5680 | goto out_free; |
e50a1c2e | 5681 | } |
1f1ea6c2 | 5682 | nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len); |
7d3e91a8 SW |
5683 | if (buf) { |
5684 | if (res.acl_len > buflen) { | |
5685 | ret = -ERANGE; | |
5686 | goto out_free; | |
5687 | } | |
1f1ea6c2 | 5688 | _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len); |
7d3e91a8 | 5689 | } |
1f1ea6c2 TM |
5690 | out_ok: |
5691 | ret = res.acl_len; | |
e50a1c2e | 5692 | out_free: |
bf118a34 AA |
5693 | for (i = 0; i < npages; i++) |
5694 | if (pages[i]) | |
5695 | __free_page(pages[i]); | |
331818f1 TM |
5696 | if (res.acl_scratch) |
5697 | __free_page(res.acl_scratch); | |
62a1573f | 5698 | kfree(pages); |
aa1870af BF |
5699 | return ret; |
5700 | } | |
5701 | ||
16b4289c TM |
5702 | static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) |
5703 | { | |
0688e64b TM |
5704 | struct nfs4_exception exception = { |
5705 | .interruptible = true, | |
5706 | }; | |
16b4289c TM |
5707 | ssize_t ret; |
5708 | do { | |
5709 | ret = __nfs4_get_acl_uncached(inode, buf, buflen); | |
c1578b76 | 5710 | trace_nfs4_get_acl(inode, ret); |
16b4289c TM |
5711 | if (ret >= 0) |
5712 | break; | |
5713 | ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception); | |
5714 | } while (exception.retry); | |
5715 | return ret; | |
5716 | } | |
5717 | ||
e50a1c2e BF |
5718 | static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen) |
5719 | { | |
5720 | struct nfs_server *server = NFS_SERVER(inode); | |
5721 | int ret; | |
5722 | ||
5723 | if (!nfs4_server_supports_acls(server)) | |
5724 | return -EOPNOTSUPP; | |
5725 | ret = nfs_revalidate_inode(server, inode); | |
5726 | if (ret < 0) | |
5727 | return ret; | |
08a22b39 AK |
5728 | if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL) |
5729 | nfs_zap_acl_cache(inode); | |
e50a1c2e BF |
5730 | ret = nfs4_read_cached_acl(inode, buf, buflen); |
5731 | if (ret != -ENOENT) | |
bf118a34 AA |
5732 | /* -ENOENT is returned if there is no ACL or if there is an ACL |
5733 | * but no cached acl data, just the acl length */ | |
e50a1c2e BF |
5734 | return ret; |
5735 | return nfs4_get_acl_uncached(inode, buf, buflen); | |
5736 | } | |
5737 | ||
16b4289c | 5738 | static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) |
4b580ee3 BF |
5739 | { |
5740 | struct nfs_server *server = NFS_SERVER(inode); | |
5741 | struct page *pages[NFS4ACL_MAXPAGES]; | |
5742 | struct nfs_setaclargs arg = { | |
5743 | .fh = NFS_FH(inode), | |
5744 | .acl_pages = pages, | |
5745 | .acl_len = buflen, | |
5746 | }; | |
73c403a9 | 5747 | struct nfs_setaclres res; |
4b580ee3 BF |
5748 | struct rpc_message msg = { |
5749 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL], | |
5750 | .rpc_argp = &arg, | |
73c403a9 | 5751 | .rpc_resp = &res, |
4b580ee3 | 5752 | }; |
21f498c2 | 5753 | unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE); |
e9e3d724 | 5754 | int ret, i; |
4b580ee3 BF |
5755 | |
5756 | if (!nfs4_server_supports_acls(server)) | |
5757 | return -EOPNOTSUPP; | |
21f498c2 TM |
5758 | if (npages > ARRAY_SIZE(pages)) |
5759 | return -ERANGE; | |
8fbcf237 | 5760 | i = buf_to_pages_noslab(buf, buflen, arg.acl_pages); |
e9e3d724 NH |
5761 | if (i < 0) |
5762 | return i; | |
c01d3645 | 5763 | nfs4_inode_make_writeable(inode); |
7c513058 | 5764 | ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
e9e3d724 NH |
5765 | |
5766 | /* | |
5767 | * Free each page after tx, so the only ref left is | |
5768 | * held by the network stack | |
5769 | */ | |
5770 | for (; i > 0; i--) | |
5771 | put_page(pages[i-1]); | |
5772 | ||
08a22b39 AK |
5773 | /* |
5774 | * Acl update can result in inode attribute update. | |
5775 | * so mark the attribute cache invalid. | |
5776 | */ | |
5777 | spin_lock(&inode->i_lock); | |
16e14375 | 5778 | NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE |
472f761e TM |
5779 | | NFS_INO_INVALID_CTIME |
5780 | | NFS_INO_REVAL_FORCED; | |
08a22b39 | 5781 | spin_unlock(&inode->i_lock); |
f41f7418 TM |
5782 | nfs_access_zap_cache(inode); |
5783 | nfs_zap_acl_cache(inode); | |
4b580ee3 BF |
5784 | return ret; |
5785 | } | |
5786 | ||
16b4289c TM |
5787 | static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) |
5788 | { | |
5789 | struct nfs4_exception exception = { }; | |
5790 | int err; | |
5791 | do { | |
c1578b76 TM |
5792 | err = __nfs4_proc_set_acl(inode, buf, buflen); |
5793 | trace_nfs4_set_acl(inode, err); | |
5794 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
16b4289c TM |
5795 | &exception); |
5796 | } while (exception.retry); | |
5797 | return err; | |
5798 | } | |
5799 | ||
aa9c2669 DQ |
5800 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
5801 | static int _nfs4_get_security_label(struct inode *inode, void *buf, | |
5802 | size_t buflen) | |
5803 | { | |
5804 | struct nfs_server *server = NFS_SERVER(inode); | |
5805 | struct nfs_fattr fattr; | |
5806 | struct nfs4_label label = {0, 0, buflen, buf}; | |
5807 | ||
5808 | u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; | |
fcb63a9b | 5809 | struct nfs4_getattr_arg arg = { |
aa9c2669 DQ |
5810 | .fh = NFS_FH(inode), |
5811 | .bitmask = bitmask, | |
5812 | }; | |
5813 | struct nfs4_getattr_res res = { | |
5814 | .fattr = &fattr, | |
5815 | .label = &label, | |
5816 | .server = server, | |
5817 | }; | |
5818 | struct rpc_message msg = { | |
5819 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], | |
fcb63a9b | 5820 | .rpc_argp = &arg, |
aa9c2669 DQ |
5821 | .rpc_resp = &res, |
5822 | }; | |
5823 | int ret; | |
5824 | ||
5825 | nfs_fattr_init(&fattr); | |
5826 | ||
fcb63a9b | 5827 | ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0); |
aa9c2669 DQ |
5828 | if (ret) |
5829 | return ret; | |
5830 | if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL)) | |
5831 | return -ENOENT; | |
5832 | if (buflen < label.len) | |
5833 | return -ERANGE; | |
5834 | return 0; | |
5835 | } | |
5836 | ||
5837 | static int nfs4_get_security_label(struct inode *inode, void *buf, | |
5838 | size_t buflen) | |
5839 | { | |
0688e64b TM |
5840 | struct nfs4_exception exception = { |
5841 | .interruptible = true, | |
5842 | }; | |
aa9c2669 DQ |
5843 | int err; |
5844 | ||
5845 | if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) | |
5846 | return -EOPNOTSUPP; | |
5847 | ||
5848 | do { | |
c1578b76 TM |
5849 | err = _nfs4_get_security_label(inode, buf, buflen); |
5850 | trace_nfs4_get_security_label(inode, err); | |
5851 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
aa9c2669 DQ |
5852 | &exception); |
5853 | } while (exception.retry); | |
5854 | return err; | |
5855 | } | |
5856 | ||
5857 | static int _nfs4_do_set_security_label(struct inode *inode, | |
5858 | struct nfs4_label *ilabel, | |
5859 | struct nfs_fattr *fattr, | |
5860 | struct nfs4_label *olabel) | |
5861 | { | |
5862 | ||
5863 | struct iattr sattr = {0}; | |
5864 | struct nfs_server *server = NFS_SERVER(inode); | |
5865 | const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; | |
12207f69 | 5866 | struct nfs_setattrargs arg = { |
d9b67e1e AS |
5867 | .fh = NFS_FH(inode), |
5868 | .iap = &sattr, | |
aa9c2669 DQ |
5869 | .server = server, |
5870 | .bitmask = bitmask, | |
5871 | .label = ilabel, | |
5872 | }; | |
5873 | struct nfs_setattrres res = { | |
5874 | .fattr = fattr, | |
5875 | .label = olabel, | |
5876 | .server = server, | |
5877 | }; | |
5878 | struct rpc_message msg = { | |
d9b67e1e AS |
5879 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], |
5880 | .rpc_argp = &arg, | |
5881 | .rpc_resp = &res, | |
aa9c2669 DQ |
5882 | }; |
5883 | int status; | |
5884 | ||
12207f69 | 5885 | nfs4_stateid_copy(&arg.stateid, &zero_stateid); |
aa9c2669 | 5886 | |
12207f69 | 5887 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
aa9c2669 DQ |
5888 | if (status) |
5889 | dprintk("%s failed: %d\n", __func__, status); | |
5890 | ||
5891 | return status; | |
5892 | } | |
5893 | ||
5894 | static int nfs4_do_set_security_label(struct inode *inode, | |
5895 | struct nfs4_label *ilabel, | |
5896 | struct nfs_fattr *fattr, | |
5897 | struct nfs4_label *olabel) | |
5898 | { | |
5899 | struct nfs4_exception exception = { }; | |
5900 | int err; | |
5901 | ||
5902 | do { | |
c1578b76 TM |
5903 | err = _nfs4_do_set_security_label(inode, ilabel, |
5904 | fattr, olabel); | |
5905 | trace_nfs4_set_security_label(inode, err); | |
5906 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
aa9c2669 DQ |
5907 | &exception); |
5908 | } while (exception.retry); | |
5909 | return err; | |
5910 | } | |
5911 | ||
5912 | static int | |
59301226 | 5913 | nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen) |
aa9c2669 DQ |
5914 | { |
5915 | struct nfs4_label ilabel, *olabel = NULL; | |
5916 | struct nfs_fattr fattr; | |
aa9c2669 DQ |
5917 | int status; |
5918 | ||
5919 | if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) | |
5920 | return -EOPNOTSUPP; | |
5921 | ||
5922 | nfs_fattr_init(&fattr); | |
5923 | ||
5924 | ilabel.pi = 0; | |
5925 | ilabel.lfs = 0; | |
5926 | ilabel.label = (char *)buf; | |
5927 | ilabel.len = buflen; | |
5928 | ||
aa9c2669 DQ |
5929 | olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); |
5930 | if (IS_ERR(olabel)) { | |
5931 | status = -PTR_ERR(olabel); | |
5932 | goto out; | |
5933 | } | |
5934 | ||
5935 | status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel); | |
5936 | if (status == 0) | |
5937 | nfs_setsecurity(inode, &fattr, olabel); | |
5938 | ||
5939 | nfs4_label_free(olabel); | |
5940 | out: | |
aa9c2669 DQ |
5941 | return status; |
5942 | } | |
5943 | #endif /* CONFIG_NFS_V4_SECURITY_LABEL */ | |
5944 | ||
5945 | ||
f092075d CL |
5946 | static void nfs4_init_boot_verifier(const struct nfs_client *clp, |
5947 | nfs4_verifier *bootverf) | |
cd93710e CL |
5948 | { |
5949 | __be32 verf[2]; | |
5950 | ||
2c820d9a CL |
5951 | if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) { |
5952 | /* An impossible timestamp guarantees this value | |
5953 | * will never match a generated boot time. */ | |
2f86e091 DD |
5954 | verf[0] = cpu_to_be32(U32_MAX); |
5955 | verf[1] = cpu_to_be32(U32_MAX); | |
2c820d9a | 5956 | } else { |
f092075d | 5957 | struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); |
2f86e091 DD |
5958 | u64 ns = ktime_to_ns(nn->boot_time); |
5959 | ||
5960 | verf[0] = cpu_to_be32(ns >> 32); | |
5961 | verf[1] = cpu_to_be32(ns); | |
2c820d9a | 5962 | } |
cd93710e CL |
5963 | memcpy(bootverf->data, verf, sizeof(bootverf->data)); |
5964 | } | |
5965 | ||
a3192688 JL |
5966 | static int |
5967 | nfs4_init_nonuniform_client_string(struct nfs_client *clp) | |
e984a55a | 5968 | { |
a3192688 JL |
5969 | size_t len; |
5970 | char *str; | |
e984a55a | 5971 | |
ceb3a16c | 5972 | if (clp->cl_owner_id != NULL) |
a3192688 | 5973 | return 0; |
4a3e5779 | 5974 | |
a3192688 | 5975 | rcu_read_lock(); |
848a4eb2 CL |
5976 | len = 14 + |
5977 | strlen(clp->cl_rpcclient->cl_nodename) + | |
5978 | 1 + | |
a3192688 | 5979 | strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) + |
a3192688 JL |
5980 | 1; |
5981 | rcu_read_unlock(); | |
848a4eb2 CL |
5982 | if (nfs4_client_id_uniquifier[0] != '\0') |
5983 | len += strlen(nfs4_client_id_uniquifier) + 1; | |
a3192688 JL |
5984 | if (len > NFS4_OPAQUE_LIMIT + 1) |
5985 | return -EINVAL; | |
5986 | ||
5987 | /* | |
5988 | * Since this string is allocated at mount time, and held until the | |
5989 | * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying | |
5990 | * about a memory-reclaim deadlock. | |
5991 | */ | |
5992 | str = kmalloc(len, GFP_KERNEL); | |
5993 | if (!str) | |
5994 | return -ENOMEM; | |
ceb3a16c | 5995 | |
e984a55a | 5996 | rcu_read_lock(); |
848a4eb2 | 5997 | if (nfs4_client_id_uniquifier[0] != '\0') |
025bb9f8 | 5998 | scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s", |
848a4eb2 CL |
5999 | clp->cl_rpcclient->cl_nodename, |
6000 | nfs4_client_id_uniquifier, | |
6001 | rpc_peeraddr2str(clp->cl_rpcclient, | |
025bb9f8 | 6002 | RPC_DISPLAY_ADDR)); |
848a4eb2 | 6003 | else |
025bb9f8 | 6004 | scnprintf(str, len, "Linux NFSv4.0 %s/%s", |
848a4eb2 CL |
6005 | clp->cl_rpcclient->cl_nodename, |
6006 | rpc_peeraddr2str(clp->cl_rpcclient, | |
025bb9f8 | 6007 | RPC_DISPLAY_ADDR)); |
e984a55a | 6008 | rcu_read_unlock(); |
a3192688 | 6009 | |
a3192688 JL |
6010 | clp->cl_owner_id = str; |
6011 | return 0; | |
e984a55a CL |
6012 | } |
6013 | ||
873e3851 JL |
6014 | static int |
6015 | nfs4_init_uniquifier_client_string(struct nfs_client *clp) | |
6016 | { | |
873e3851 JL |
6017 | size_t len; |
6018 | char *str; | |
6019 | ||
6020 | len = 10 + 10 + 1 + 10 + 1 + | |
6021 | strlen(nfs4_client_id_uniquifier) + 1 + | |
6022 | strlen(clp->cl_rpcclient->cl_nodename) + 1; | |
6023 | ||
6024 | if (len > NFS4_OPAQUE_LIMIT + 1) | |
6025 | return -EINVAL; | |
6026 | ||
6027 | /* | |
6028 | * Since this string is allocated at mount time, and held until the | |
6029 | * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying | |
6030 | * about a memory-reclaim deadlock. | |
6031 | */ | |
6032 | str = kmalloc(len, GFP_KERNEL); | |
6033 | if (!str) | |
6034 | return -ENOMEM; | |
6035 | ||
f2dd436e | 6036 | scnprintf(str, len, "Linux NFSv%u.%u %s/%s", |
873e3851 JL |
6037 | clp->rpc_ops->version, clp->cl_minorversion, |
6038 | nfs4_client_id_uniquifier, | |
6039 | clp->cl_rpcclient->cl_nodename); | |
873e3851 JL |
6040 | clp->cl_owner_id = str; |
6041 | return 0; | |
6042 | } | |
6043 | ||
6044 | static int | |
6045 | nfs4_init_uniform_client_string(struct nfs_client *clp) | |
e984a55a | 6046 | { |
873e3851 JL |
6047 | size_t len; |
6048 | char *str; | |
ceb3a16c TM |
6049 | |
6050 | if (clp->cl_owner_id != NULL) | |
873e3851 | 6051 | return 0; |
6f2ea7f2 CL |
6052 | |
6053 | if (nfs4_client_id_uniquifier[0] != '\0') | |
873e3851 JL |
6054 | return nfs4_init_uniquifier_client_string(clp); |
6055 | ||
6056 | len = 10 + 10 + 1 + 10 + 1 + | |
6057 | strlen(clp->cl_rpcclient->cl_nodename) + 1; | |
6058 | ||
6059 | if (len > NFS4_OPAQUE_LIMIT + 1) | |
6060 | return -EINVAL; | |
6061 | ||
6062 | /* | |
6063 | * Since this string is allocated at mount time, and held until the | |
6064 | * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying | |
6065 | * about a memory-reclaim deadlock. | |
6066 | */ | |
6067 | str = kmalloc(len, GFP_KERNEL); | |
6068 | if (!str) | |
6069 | return -ENOMEM; | |
6070 | ||
f2dd436e | 6071 | scnprintf(str, len, "Linux NFSv%u.%u %s", |
873e3851 JL |
6072 | clp->rpc_ops->version, clp->cl_minorversion, |
6073 | clp->cl_rpcclient->cl_nodename); | |
873e3851 JL |
6074 | clp->cl_owner_id = str; |
6075 | return 0; | |
e984a55a CL |
6076 | } |
6077 | ||
706cb8db CL |
6078 | /* |
6079 | * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback | |
6080 | * services. Advertise one based on the address family of the | |
6081 | * clientaddr. | |
6082 | */ | |
6083 | static unsigned int | |
6084 | nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len) | |
6085 | { | |
6086 | if (strchr(clp->cl_ipaddr, ':') != NULL) | |
6087 | return scnprintf(buf, len, "tcp6"); | |
6088 | else | |
6089 | return scnprintf(buf, len, "tcp"); | |
6090 | } | |
6091 | ||
f11b2a1c JL |
6092 | static void nfs4_setclientid_done(struct rpc_task *task, void *calldata) |
6093 | { | |
6094 | struct nfs4_setclientid *sc = calldata; | |
6095 | ||
6096 | if (task->tk_status == 0) | |
6097 | sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred); | |
6098 | } | |
6099 | ||
6100 | static const struct rpc_call_ops nfs4_setclientid_ops = { | |
6101 | .rpc_call_done = nfs4_setclientid_done, | |
6102 | }; | |
6103 | ||
6bbb4ae8 CL |
6104 | /** |
6105 | * nfs4_proc_setclientid - Negotiate client ID | |
6106 | * @clp: state data structure | |
6107 | * @program: RPC program for NFSv4 callback service | |
6108 | * @port: IP port number for NFS4 callback service | |
a52458b4 | 6109 | * @cred: credential to use for this call |
6bbb4ae8 CL |
6110 | * @res: where to place the result |
6111 | * | |
6112 | * Returns zero, a negative errno, or a negative NFS4ERR status code. | |
6113 | */ | |
bb8b27e5 | 6114 | int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, |
a52458b4 | 6115 | unsigned short port, const struct cred *cred, |
bb8b27e5 | 6116 | struct nfs4_setclientid_res *res) |
1da177e4 LT |
6117 | { |
6118 | nfs4_verifier sc_verifier; | |
6119 | struct nfs4_setclientid setclientid = { | |
6120 | .sc_verifier = &sc_verifier, | |
6121 | .sc_prog = program, | |
3a6bb738 | 6122 | .sc_clnt = clp, |
1da177e4 LT |
6123 | }; |
6124 | struct rpc_message msg = { | |
6125 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID], | |
6126 | .rpc_argp = &setclientid, | |
bb8b27e5 | 6127 | .rpc_resp = res, |
286d7d6a | 6128 | .rpc_cred = cred, |
1da177e4 | 6129 | }; |
f11b2a1c JL |
6130 | struct rpc_task_setup task_setup_data = { |
6131 | .rpc_client = clp->cl_rpcclient, | |
6132 | .rpc_message = &msg, | |
6133 | .callback_ops = &nfs4_setclientid_ops, | |
6134 | .callback_data = &setclientid, | |
5a0c257f | 6135 | .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN, |
f11b2a1c | 6136 | }; |
7dc2993a | 6137 | unsigned long now = jiffies; |
6bbb4ae8 | 6138 | int status; |
1da177e4 | 6139 | |
de734831 | 6140 | /* nfs_client_id4 */ |
f092075d | 6141 | nfs4_init_boot_verifier(clp, &sc_verifier); |
873e3851 JL |
6142 | |
6143 | if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags)) | |
6144 | status = nfs4_init_uniform_client_string(clp); | |
6145 | else | |
a3192688 | 6146 | status = nfs4_init_nonuniform_client_string(clp); |
873e3851 JL |
6147 | |
6148 | if (status) | |
6149 | goto out; | |
3a6bb738 | 6150 | |
de734831 | 6151 | /* cb_client4 */ |
706cb8db CL |
6152 | setclientid.sc_netid_len = |
6153 | nfs4_init_callback_netid(clp, | |
6154 | setclientid.sc_netid, | |
6155 | sizeof(setclientid.sc_netid)); | |
de734831 | 6156 | setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr, |
d4d3c507 | 6157 | sizeof(setclientid.sc_uaddr), "%s.%u.%u", |
1da177e4 LT |
6158 | clp->cl_ipaddr, port >> 8, port & 255); |
6159 | ||
3a6bb738 | 6160 | dprintk("NFS call setclientid auth=%s, '%s'\n", |
6bbb4ae8 | 6161 | clp->cl_rpcclient->cl_auth->au_ops->au_name, |
3a6bb738 | 6162 | clp->cl_owner_id); |
dae40965 AS |
6163 | |
6164 | status = nfs4_call_sync_custom(&task_setup_data); | |
f11b2a1c | 6165 | if (setclientid.sc_cred) { |
1047ec86 | 6166 | kfree(clp->cl_acceptor); |
f11b2a1c JL |
6167 | clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred); |
6168 | put_rpccred(setclientid.sc_cred); | |
6169 | } | |
7dc2993a RM |
6170 | |
6171 | if (status == 0) | |
6172 | do_renew_lease(clp, now); | |
f11b2a1c | 6173 | out: |
c6d01c6f | 6174 | trace_nfs4_setclientid(clp, status); |
6bbb4ae8 CL |
6175 | dprintk("NFS reply setclientid: %d\n", status); |
6176 | return status; | |
1da177e4 LT |
6177 | } |
6178 | ||
6bbb4ae8 CL |
6179 | /** |
6180 | * nfs4_proc_setclientid_confirm - Confirm client ID | |
6181 | * @clp: state data structure | |
302fad7b | 6182 | * @arg: result of a previous SETCLIENTID |
a52458b4 | 6183 | * @cred: credential to use for this call |
6bbb4ae8 CL |
6184 | * |
6185 | * Returns zero, a negative errno, or a negative NFS4ERR status code. | |
6186 | */ | |
fd954ae1 | 6187 | int nfs4_proc_setclientid_confirm(struct nfs_client *clp, |
bb8b27e5 | 6188 | struct nfs4_setclientid_res *arg, |
a52458b4 | 6189 | const struct cred *cred) |
1da177e4 | 6190 | { |
1da177e4 LT |
6191 | struct rpc_message msg = { |
6192 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM], | |
bb8b27e5 | 6193 | .rpc_argp = arg, |
286d7d6a | 6194 | .rpc_cred = cred, |
1da177e4 | 6195 | }; |
1da177e4 LT |
6196 | int status; |
6197 | ||
6bbb4ae8 CL |
6198 | dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n", |
6199 | clp->cl_rpcclient->cl_auth->au_ops->au_name, | |
6200 | clp->cl_clientid); | |
5a0c257f N |
6201 | status = rpc_call_sync(clp->cl_rpcclient, &msg, |
6202 | RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); | |
c6d01c6f | 6203 | trace_nfs4_setclientid_confirm(clp, status); |
6bbb4ae8 | 6204 | dprintk("NFS reply setclientid_confirm: %d\n", status); |
1da177e4 LT |
6205 | return status; |
6206 | } | |
6207 | ||
fe650407 TM |
6208 | struct nfs4_delegreturndata { |
6209 | struct nfs4_delegreturnargs args; | |
fa178f29 | 6210 | struct nfs4_delegreturnres res; |
fe650407 TM |
6211 | struct nfs_fh fh; |
6212 | nfs4_stateid stateid; | |
26e976a8 | 6213 | unsigned long timestamp; |
586f1c39 TM |
6214 | struct { |
6215 | struct nfs4_layoutreturn_args arg; | |
6216 | struct nfs4_layoutreturn_res res; | |
4d796d75 | 6217 | struct nfs4_xdr_opaque_data ld_private; |
586f1c39 TM |
6218 | u32 roc_barrier; |
6219 | bool roc; | |
6220 | } lr; | |
fa178f29 | 6221 | struct nfs_fattr fattr; |
fe650407 | 6222 | int rpc_status; |
039b756a | 6223 | struct inode *inode; |
fe650407 TM |
6224 | }; |
6225 | ||
fe650407 TM |
6226 | static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata) |
6227 | { | |
6228 | struct nfs4_delegreturndata *data = calldata; | |
e0dba012 TM |
6229 | struct nfs4_exception exception = { |
6230 | .inode = data->inode, | |
6231 | .stateid = &data->stateid, | |
6232 | }; | |
938e1010 | 6233 | |
14516c3a TM |
6234 | if (!nfs4_sequence_done(task, &data->res.seq_res)) |
6235 | return; | |
938e1010 | 6236 | |
ca8acf8d | 6237 | trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status); |
586f1c39 TM |
6238 | |
6239 | /* Handle Layoutreturn errors */ | |
287a9c55 TM |
6240 | if (pnfs_roc_done(task, data->inode, |
6241 | &data->args.lr_args, | |
6242 | &data->res.lr_res, | |
6243 | &data->res.lr_ret) == -EAGAIN) | |
6244 | goto out_restart; | |
586f1c39 | 6245 | |
79708861 | 6246 | switch (task->tk_status) { |
79708861 | 6247 | case 0: |
fa178f29 | 6248 | renew_lease(data->res.server, data->timestamp); |
23ea44c2 | 6249 | break; |
c97cf606 TM |
6250 | case -NFS4ERR_ADMIN_REVOKED: |
6251 | case -NFS4ERR_DELEG_REVOKED: | |
26d36301 TM |
6252 | case -NFS4ERR_EXPIRED: |
6253 | nfs4_free_revoked_stateid(data->res.server, | |
6254 | data->args.stateid, | |
6255 | task->tk_msg.rpc_cred); | |
12f275cd | 6256 | /* Fallthrough */ |
c97cf606 | 6257 | case -NFS4ERR_BAD_STATEID: |
c97cf606 | 6258 | case -NFS4ERR_STALE_STATEID: |
244fcd2f | 6259 | case -ETIMEDOUT: |
c97cf606 TM |
6260 | task->tk_status = 0; |
6261 | break; | |
12f275cd | 6262 | case -NFS4ERR_OLD_STATEID: |
246afc0a TM |
6263 | if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode)) |
6264 | nfs4_stateid_seqid_inc(&data->stateid); | |
70d136b2 TM |
6265 | if (data->args.bitmask) { |
6266 | data->args.bitmask = NULL; | |
6267 | data->res.fattr = NULL; | |
6268 | } | |
246afc0a | 6269 | goto out_restart; |
8ac2b422 TM |
6270 | case -NFS4ERR_ACCESS: |
6271 | if (data->args.bitmask) { | |
6272 | data->args.bitmask = NULL; | |
6273 | data->res.fattr = NULL; | |
140087fd | 6274 | goto out_restart; |
8ac2b422 | 6275 | } |
140087fd | 6276 | /* Fallthrough */ |
79708861 | 6277 | default: |
e0dba012 TM |
6278 | task->tk_status = nfs4_async_handle_exception(task, |
6279 | data->res.server, task->tk_status, | |
6280 | &exception); | |
6281 | if (exception.retry) | |
140087fd | 6282 | goto out_restart; |
79708861 | 6283 | } |
d51f91d2 | 6284 | nfs_delegation_mark_returned(data->inode, data->args.stateid); |
79708861 | 6285 | data->rpc_status = task->tk_status; |
140087fd | 6286 | return; |
140087fd TM |
6287 | out_restart: |
6288 | task->tk_status = 0; | |
6289 | rpc_restart_call_prepare(task); | |
fe650407 TM |
6290 | } |
6291 | ||
6292 | static void nfs4_delegreturn_release(void *calldata) | |
6293 | { | |
039b756a | 6294 | struct nfs4_delegreturndata *data = calldata; |
ea7c38fe | 6295 | struct inode *inode = data->inode; |
039b756a | 6296 | |
ea7c38fe | 6297 | if (inode) { |
586f1c39 | 6298 | if (data->lr.roc) |
1c5bd76d TM |
6299 | pnfs_roc_release(&data->lr.arg, &data->lr.res, |
6300 | data->res.lr_ret); | |
0bc2c9b4 | 6301 | nfs_post_op_update_inode_force_wcc(inode, &data->fattr); |
ea7c38fe TM |
6302 | nfs_iput_and_deactive(inode); |
6303 | } | |
fe650407 TM |
6304 | kfree(calldata); |
6305 | } | |
6306 | ||
938e1010 AA |
6307 | static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data) |
6308 | { | |
6309 | struct nfs4_delegreturndata *d_data; | |
c8bf7073 | 6310 | struct pnfs_layout_hdr *lo; |
938e1010 AA |
6311 | |
6312 | d_data = (struct nfs4_delegreturndata *)data; | |
6313 | ||
5326de9e TM |
6314 | if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) { |
6315 | nfs4_sequence_done(task, &d_data->res.seq_res); | |
500d701f | 6316 | return; |
5326de9e | 6317 | } |
500d701f | 6318 | |
c8bf7073 TM |
6319 | lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL; |
6320 | if (lo && !pnfs_layout_is_valid(lo)) { | |
6321 | d_data->args.lr_args = NULL; | |
6322 | d_data->res.lr_res = NULL; | |
6323 | } | |
6324 | ||
42e1cca7 | 6325 | nfs4_setup_sequence(d_data->res.server->nfs_client, |
d9afbd1b TM |
6326 | &d_data->args.seq_args, |
6327 | &d_data->res.seq_res, | |
6328 | task); | |
938e1010 | 6329 | } |
938e1010 | 6330 | |
c8d149f3 | 6331 | static const struct rpc_call_ops nfs4_delegreturn_ops = { |
938e1010 | 6332 | .rpc_call_prepare = nfs4_delegreturn_prepare, |
fe650407 TM |
6333 | .rpc_call_done = nfs4_delegreturn_done, |
6334 | .rpc_release = nfs4_delegreturn_release, | |
6335 | }; | |
6336 | ||
a52458b4 | 6337 | static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync) |
fe650407 TM |
6338 | { |
6339 | struct nfs4_delegreturndata *data; | |
fa178f29 | 6340 | struct nfs_server *server = NFS_SERVER(inode); |
fe650407 | 6341 | struct rpc_task *task; |
5138fde0 TM |
6342 | struct rpc_message msg = { |
6343 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN], | |
6344 | .rpc_cred = cred, | |
6345 | }; | |
c970aa85 TM |
6346 | struct rpc_task_setup task_setup_data = { |
6347 | .rpc_client = server->client, | |
5138fde0 | 6348 | .rpc_message = &msg, |
c970aa85 | 6349 | .callback_ops = &nfs4_delegreturn_ops, |
f304a809 | 6350 | .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT, |
c970aa85 | 6351 | }; |
e6f81075 | 6352 | int status = 0; |
fe650407 | 6353 | |
8535b2be | 6354 | data = kzalloc(sizeof(*data), GFP_NOFS); |
fe650407 TM |
6355 | if (data == NULL) |
6356 | return -ENOMEM; | |
fba83f34 | 6357 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0); |
99ade3c7 AE |
6358 | |
6359 | nfs4_state_protect(server->nfs_client, | |
6360 | NFS_SP4_MACH_CRED_CLEANUP, | |
6361 | &task_setup_data.rpc_client, &msg); | |
6362 | ||
fe650407 TM |
6363 | data->args.fhandle = &data->fh; |
6364 | data->args.stateid = &data->stateid; | |
9e907fec | 6365 | data->args.bitmask = server->cache_consistency_bitmask; |
fe650407 | 6366 | nfs_copy_fh(&data->fh, NFS_FH(inode)); |
f597c537 | 6367 | nfs4_stateid_copy(&data->stateid, stateid); |
fa178f29 TM |
6368 | data->res.fattr = &data->fattr; |
6369 | data->res.server = server; | |
586f1c39 | 6370 | data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT; |
4d796d75 | 6371 | data->lr.arg.ld_private = &data->lr.ld_private; |
5138fde0 | 6372 | nfs_fattr_init(data->res.fattr); |
26e976a8 | 6373 | data->timestamp = jiffies; |
fe650407 | 6374 | data->rpc_status = 0; |
53e6fc86 | 6375 | data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred); |
ea7c38fe | 6376 | data->inode = nfs_igrab_and_active(inode); |
1c5bd76d | 6377 | if (data->inode) { |
1c5bd76d TM |
6378 | if (data->lr.roc) { |
6379 | data->args.lr_args = &data->lr.arg; | |
6380 | data->res.lr_res = &data->lr.res; | |
6381 | } | |
53e6fc86 TM |
6382 | } else if (data->lr.roc) { |
6383 | pnfs_roc_release(&data->lr.arg, &data->lr.res, 0); | |
6384 | data->lr.roc = false; | |
1c5bd76d | 6385 | } |
fe650407 | 6386 | |
c970aa85 | 6387 | task_setup_data.callback_data = data; |
1174dd1f TM |
6388 | msg.rpc_argp = &data->args; |
6389 | msg.rpc_resp = &data->res; | |
c970aa85 | 6390 | task = rpc_run_task(&task_setup_data); |
7a1218a2 | 6391 | if (IS_ERR(task)) |
fe650407 | 6392 | return PTR_ERR(task); |
e6f81075 TM |
6393 | if (!issync) |
6394 | goto out; | |
820bf85c | 6395 | status = rpc_wait_for_completion_task(task); |
e6f81075 TM |
6396 | if (status != 0) |
6397 | goto out; | |
6398 | status = data->rpc_status; | |
e6f81075 | 6399 | out: |
e6b3c4db | 6400 | rpc_put_task(task); |
fe650407 | 6401 | return status; |
1da177e4 LT |
6402 | } |
6403 | ||
a52458b4 | 6404 | int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync) |
1da177e4 LT |
6405 | { |
6406 | struct nfs_server *server = NFS_SERVER(inode); | |
6407 | struct nfs4_exception exception = { }; | |
6408 | int err; | |
6409 | do { | |
e6f81075 | 6410 | err = _nfs4_proc_delegreturn(inode, cred, stateid, issync); |
48c9579a | 6411 | trace_nfs4_delegreturn(inode, stateid, err); |
1da177e4 LT |
6412 | switch (err) { |
6413 | case -NFS4ERR_STALE_STATEID: | |
6414 | case -NFS4ERR_EXPIRED: | |
1da177e4 LT |
6415 | case 0: |
6416 | return 0; | |
6417 | } | |
6418 | err = nfs4_handle_exception(server, err, &exception); | |
6419 | } while (exception.retry); | |
6420 | return err; | |
6421 | } | |
6422 | ||
1da177e4 LT |
6423 | static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
6424 | { | |
6425 | struct inode *inode = state->inode; | |
6426 | struct nfs_server *server = NFS_SERVER(inode); | |
7539bbab | 6427 | struct nfs_client *clp = server->nfs_client; |
911d1aaf | 6428 | struct nfs_lockt_args arg = { |
1da177e4 | 6429 | .fh = NFS_FH(inode), |
911d1aaf | 6430 | .fl = request, |
1da177e4 | 6431 | }; |
911d1aaf TM |
6432 | struct nfs_lockt_res res = { |
6433 | .denied = request, | |
1da177e4 LT |
6434 | }; |
6435 | struct rpc_message msg = { | |
6436 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT], | |
d9b67e1e AS |
6437 | .rpc_argp = &arg, |
6438 | .rpc_resp = &res, | |
1da177e4 LT |
6439 | .rpc_cred = state->owner->so_cred, |
6440 | }; | |
1da177e4 LT |
6441 | struct nfs4_lock_state *lsp; |
6442 | int status; | |
6443 | ||
911d1aaf | 6444 | arg.lock_owner.clientid = clp->cl_clientid; |
8d0a8a9d TM |
6445 | status = nfs4_set_lock_state(state, request); |
6446 | if (status != 0) | |
6447 | goto out; | |
6448 | lsp = request->fl_u.nfs4_fl.owner; | |
48c22eb2 | 6449 | arg.lock_owner.id = lsp->ls_seqid.owner_id; |
d035c36c | 6450 | arg.lock_owner.s_dev = server->s_dev; |
7c513058 | 6451 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
911d1aaf TM |
6452 | switch (status) { |
6453 | case 0: | |
6454 | request->fl_type = F_UNLCK; | |
6455 | break; | |
6456 | case -NFS4ERR_DENIED: | |
6457 | status = 0; | |
1da177e4 | 6458 | } |
70cc6487 | 6459 | request->fl_ops->fl_release_private(request); |
a6f951dd | 6460 | request->fl_ops = NULL; |
8d0a8a9d | 6461 | out: |
1da177e4 LT |
6462 | return status; |
6463 | } | |
6464 | ||
6465 | static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) | |
6466 | { | |
0688e64b TM |
6467 | struct nfs4_exception exception = { |
6468 | .interruptible = true, | |
6469 | }; | |
1da177e4 LT |
6470 | int err; |
6471 | ||
6472 | do { | |
d1b748a5 TM |
6473 | err = _nfs4_proc_getlk(state, cmd, request); |
6474 | trace_nfs4_get_lock(request, state, cmd, err); | |
6475 | err = nfs4_handle_exception(NFS_SERVER(state->inode), err, | |
1da177e4 LT |
6476 | &exception); |
6477 | } while (exception.retry); | |
6478 | return err; | |
6479 | } | |
6480 | ||
32c6e7ee TM |
6481 | /* |
6482 | * Update the seqid of a lock stateid after receiving | |
6483 | * NFS4ERR_OLD_STATEID | |
6484 | */ | |
6485 | static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst, | |
6486 | struct nfs4_lock_state *lsp) | |
6487 | { | |
6488 | struct nfs4_state *state = lsp->ls_state; | |
6489 | bool ret = false; | |
6490 | ||
6491 | spin_lock(&state->state_lock); | |
6492 | if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid)) | |
6493 | goto out; | |
6494 | if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst)) | |
6495 | nfs4_stateid_seqid_inc(dst); | |
6496 | else | |
6497 | dst->seqid = lsp->ls_stateid.seqid; | |
6498 | ret = true; | |
6499 | out: | |
6500 | spin_unlock(&state->state_lock); | |
6501 | return ret; | |
6502 | } | |
6503 | ||
6504 | static bool nfs4_sync_lock_stateid(nfs4_stateid *dst, | |
6505 | struct nfs4_lock_state *lsp) | |
6506 | { | |
6507 | struct nfs4_state *state = lsp->ls_state; | |
6508 | bool ret; | |
6509 | ||
6510 | spin_lock(&state->state_lock); | |
6511 | ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid); | |
6512 | nfs4_stateid_copy(dst, &lsp->ls_stateid); | |
6513 | spin_unlock(&state->state_lock); | |
6514 | return ret; | |
6515 | } | |
6516 | ||
faf5f49c | 6517 | struct nfs4_unlockdata { |
911d1aaf TM |
6518 | struct nfs_locku_args arg; |
6519 | struct nfs_locku_res res; | |
faf5f49c TM |
6520 | struct nfs4_lock_state *lsp; |
6521 | struct nfs_open_context *ctx; | |
f30cb757 | 6522 | struct nfs_lock_context *l_ctx; |
911d1aaf | 6523 | struct file_lock fl; |
516285eb | 6524 | struct nfs_server *server; |
26e976a8 | 6525 | unsigned long timestamp; |
faf5f49c TM |
6526 | }; |
6527 | ||
911d1aaf TM |
6528 | static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl, |
6529 | struct nfs_open_context *ctx, | |
6530 | struct nfs4_lock_state *lsp, | |
6531 | struct nfs_seqid *seqid) | |
6532 | { | |
6533 | struct nfs4_unlockdata *p; | |
32c6e7ee TM |
6534 | struct nfs4_state *state = lsp->ls_state; |
6535 | struct inode *inode = state->inode; | |
911d1aaf | 6536 | |
8535b2be | 6537 | p = kzalloc(sizeof(*p), GFP_NOFS); |
911d1aaf TM |
6538 | if (p == NULL) |
6539 | return NULL; | |
6540 | p->arg.fh = NFS_FH(inode); | |
6541 | p->arg.fl = &p->fl; | |
6542 | p->arg.seqid = seqid; | |
c1d51931 | 6543 | p->res.seqid = seqid; |
911d1aaf | 6544 | p->lsp = lsp; |
911d1aaf TM |
6545 | /* Ensure we don't close file until we're done freeing locks! */ |
6546 | p->ctx = get_nfs_open_context(ctx); | |
f30cb757 | 6547 | p->l_ctx = nfs_get_lock_context(ctx); |
7b587e1a N |
6548 | locks_init_lock(&p->fl); |
6549 | locks_copy_lock(&p->fl, fl); | |
911d1aaf | 6550 | p->server = NFS_SERVER(inode); |
32c6e7ee TM |
6551 | spin_lock(&state->state_lock); |
6552 | nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid); | |
6553 | spin_unlock(&state->state_lock); | |
911d1aaf TM |
6554 | return p; |
6555 | } | |
6556 | ||
06f814a3 | 6557 | static void nfs4_locku_release_calldata(void *data) |
faf5f49c | 6558 | { |
963d8fe5 | 6559 | struct nfs4_unlockdata *calldata = data; |
911d1aaf | 6560 | nfs_free_seqid(calldata->arg.seqid); |
06f814a3 | 6561 | nfs4_put_lock_state(calldata->lsp); |
f30cb757 | 6562 | nfs_put_lock_context(calldata->l_ctx); |
06f814a3 TM |
6563 | put_nfs_open_context(calldata->ctx); |
6564 | kfree(calldata); | |
faf5f49c TM |
6565 | } |
6566 | ||
963d8fe5 | 6567 | static void nfs4_locku_done(struct rpc_task *task, void *data) |
faf5f49c | 6568 | { |
963d8fe5 | 6569 | struct nfs4_unlockdata *calldata = data; |
82571552 TM |
6570 | struct nfs4_exception exception = { |
6571 | .inode = calldata->lsp->ls_state->inode, | |
6572 | .stateid = &calldata->arg.stateid, | |
6573 | }; | |
faf5f49c | 6574 | |
14516c3a TM |
6575 | if (!nfs4_sequence_done(task, &calldata->res.seq_res)) |
6576 | return; | |
faf5f49c TM |
6577 | switch (task->tk_status) { |
6578 | case 0: | |
26e976a8 | 6579 | renew_lease(calldata->server, calldata->timestamp); |
75575ddf | 6580 | locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl); |
c69899a1 TM |
6581 | if (nfs4_update_lock_stateid(calldata->lsp, |
6582 | &calldata->res.stateid)) | |
6583 | break; | |
01e03bdc | 6584 | /* Fall through */ |
26d36301 TM |
6585 | case -NFS4ERR_ADMIN_REVOKED: |
6586 | case -NFS4ERR_EXPIRED: | |
6587 | nfs4_free_revoked_stateid(calldata->server, | |
6588 | &calldata->arg.stateid, | |
6589 | task->tk_msg.rpc_cred); | |
01e03bdc | 6590 | /* Fall through */ |
9e33bed5 | 6591 | case -NFS4ERR_BAD_STATEID: |
faf5f49c | 6592 | case -NFS4ERR_STALE_STATEID: |
32c6e7ee TM |
6593 | if (nfs4_sync_lock_stateid(&calldata->arg.stateid, |
6594 | calldata->lsp)) | |
6595 | rpc_restart_call_prepare(task); | |
6596 | break; | |
6597 | case -NFS4ERR_OLD_STATEID: | |
6598 | if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid, | |
6599 | calldata->lsp)) | |
425c1d4e | 6600 | rpc_restart_call_prepare(task); |
faf5f49c TM |
6601 | break; |
6602 | default: | |
82571552 TM |
6603 | task->tk_status = nfs4_async_handle_exception(task, |
6604 | calldata->server, task->tk_status, | |
6605 | &exception); | |
6606 | if (exception.retry) | |
d00c5d43 | 6607 | rpc_restart_call_prepare(task); |
faf5f49c | 6608 | } |
2b1bc308 | 6609 | nfs_release_seqid(calldata->arg.seqid); |
faf5f49c TM |
6610 | } |
6611 | ||
4ce70ada | 6612 | static void nfs4_locku_prepare(struct rpc_task *task, void *data) |
faf5f49c | 6613 | { |
4ce70ada | 6614 | struct nfs4_unlockdata *calldata = data; |
faf5f49c | 6615 | |
f30cb757 BC |
6616 | if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) && |
6617 | nfs_async_iocounter_wait(task, calldata->l_ctx)) | |
6618 | return; | |
6619 | ||
911d1aaf | 6620 | if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) |
c8da19b9 | 6621 | goto out_wait; |
795a88c9 | 6622 | if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) { |
963d8fe5 | 6623 | /* Note: exit _without_ running nfs4_locku_done */ |
c8da19b9 | 6624 | goto out_no_action; |
faf5f49c | 6625 | } |
26e976a8 | 6626 | calldata->timestamp = jiffies; |
42e1cca7 | 6627 | if (nfs4_setup_sequence(calldata->server->nfs_client, |
a893693c | 6628 | &calldata->arg.seq_args, |
2240a9e2 TM |
6629 | &calldata->res.seq_res, |
6630 | task) != 0) | |
6631 | nfs_release_seqid(calldata->arg.seqid); | |
c8da19b9 TM |
6632 | return; |
6633 | out_no_action: | |
6634 | task->tk_action = NULL; | |
6635 | out_wait: | |
6636 | nfs4_sequence_done(task, &calldata->res.seq_res); | |
faf5f49c TM |
6637 | } |
6638 | ||
963d8fe5 | 6639 | static const struct rpc_call_ops nfs4_locku_ops = { |
4ce70ada | 6640 | .rpc_call_prepare = nfs4_locku_prepare, |
963d8fe5 | 6641 | .rpc_call_done = nfs4_locku_done, |
06f814a3 | 6642 | .rpc_release = nfs4_locku_release_calldata, |
963d8fe5 TM |
6643 | }; |
6644 | ||
a5d16a4d TM |
6645 | static struct rpc_task *nfs4_do_unlck(struct file_lock *fl, |
6646 | struct nfs_open_context *ctx, | |
6647 | struct nfs4_lock_state *lsp, | |
6648 | struct nfs_seqid *seqid) | |
6649 | { | |
6650 | struct nfs4_unlockdata *data; | |
5138fde0 TM |
6651 | struct rpc_message msg = { |
6652 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU], | |
6653 | .rpc_cred = ctx->cred, | |
6654 | }; | |
c970aa85 TM |
6655 | struct rpc_task_setup task_setup_data = { |
6656 | .rpc_client = NFS_CLIENT(lsp->ls_state->inode), | |
5138fde0 | 6657 | .rpc_message = &msg, |
c970aa85 | 6658 | .callback_ops = &nfs4_locku_ops, |
101070ca | 6659 | .workqueue = nfsiod_workqueue, |
c970aa85 TM |
6660 | .flags = RPC_TASK_ASYNC, |
6661 | }; | |
a5d16a4d | 6662 | |
fa940720 WAA |
6663 | nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client, |
6664 | NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg); | |
6665 | ||
137d6aca FF |
6666 | /* Ensure this is an unlock - when canceling a lock, the |
6667 | * canceled lock is passed in, and it won't be an unlock. | |
6668 | */ | |
6669 | fl->fl_type = F_UNLCK; | |
f30cb757 BC |
6670 | if (fl->fl_flags & FL_CLOSE) |
6671 | set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags); | |
137d6aca | 6672 | |
a5d16a4d TM |
6673 | data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid); |
6674 | if (data == NULL) { | |
6675 | nfs_free_seqid(seqid); | |
6676 | return ERR_PTR(-ENOMEM); | |
6677 | } | |
6678 | ||
fba83f34 | 6679 | nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0); |
1174dd1f TM |
6680 | msg.rpc_argp = &data->arg; |
6681 | msg.rpc_resp = &data->res; | |
c970aa85 TM |
6682 | task_setup_data.callback_data = data; |
6683 | return rpc_run_task(&task_setup_data); | |
a5d16a4d TM |
6684 | } |
6685 | ||
faf5f49c TM |
6686 | static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request) |
6687 | { | |
65b62a29 TM |
6688 | struct inode *inode = state->inode; |
6689 | struct nfs4_state_owner *sp = state->owner; | |
6690 | struct nfs_inode *nfsi = NFS_I(inode); | |
911d1aaf | 6691 | struct nfs_seqid *seqid; |
1da177e4 | 6692 | struct nfs4_lock_state *lsp; |
06f814a3 | 6693 | struct rpc_task *task; |
b4019c0e | 6694 | struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); |
06f814a3 | 6695 | int status = 0; |
536ff0f8 | 6696 | unsigned char fl_flags = request->fl_flags; |
faf5f49c | 6697 | |
8d0a8a9d | 6698 | status = nfs4_set_lock_state(state, request); |
9b073574 TM |
6699 | /* Unlock _before_ we do the RPC call */ |
6700 | request->fl_flags |= FL_EXISTS; | |
65b62a29 TM |
6701 | /* Exclude nfs_delegation_claim_locks() */ |
6702 | mutex_lock(&sp->so_delegreturn_mutex); | |
6703 | /* Exclude nfs4_reclaim_open_stateid() - note nesting! */ | |
19e03c57 | 6704 | down_read(&nfsi->rwsem); |
75575ddf | 6705 | if (locks_lock_inode_wait(inode, request) == -ENOENT) { |
19e03c57 | 6706 | up_read(&nfsi->rwsem); |
65b62a29 | 6707 | mutex_unlock(&sp->so_delegreturn_mutex); |
9b073574 | 6708 | goto out; |
19e03c57 TM |
6709 | } |
6710 | up_read(&nfsi->rwsem); | |
65b62a29 | 6711 | mutex_unlock(&sp->so_delegreturn_mutex); |
8d0a8a9d | 6712 | if (status != 0) |
9b073574 TM |
6713 | goto out; |
6714 | /* Is this a delegated lock? */ | |
8d0a8a9d | 6715 | lsp = request->fl_u.nfs4_fl.owner; |
c5a2a15f TM |
6716 | if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0) |
6717 | goto out; | |
b4019c0e TM |
6718 | alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid; |
6719 | seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL); | |
9b073574 | 6720 | status = -ENOMEM; |
badc76dd | 6721 | if (IS_ERR(seqid)) |
9b073574 | 6722 | goto out; |
cd3758e3 | 6723 | task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid); |
a5d16a4d TM |
6724 | status = PTR_ERR(task); |
6725 | if (IS_ERR(task)) | |
9b073574 | 6726 | goto out; |
820bf85c | 6727 | status = rpc_wait_for_completion_task(task); |
e6b3c4db | 6728 | rpc_put_task(task); |
9b073574 | 6729 | out: |
536ff0f8 | 6730 | request->fl_flags = fl_flags; |
d1b748a5 | 6731 | trace_nfs4_unlock(request, state, F_SETLK, status); |
1da177e4 LT |
6732 | return status; |
6733 | } | |
6734 | ||
a5d16a4d TM |
6735 | struct nfs4_lockdata { |
6736 | struct nfs_lock_args arg; | |
6737 | struct nfs_lock_res res; | |
6738 | struct nfs4_lock_state *lsp; | |
6739 | struct nfs_open_context *ctx; | |
6740 | struct file_lock fl; | |
26e976a8 | 6741 | unsigned long timestamp; |
a5d16a4d TM |
6742 | int rpc_status; |
6743 | int cancelled; | |
66179efe | 6744 | struct nfs_server *server; |
a5d16a4d TM |
6745 | }; |
6746 | ||
6747 | static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl, | |
8535b2be TM |
6748 | struct nfs_open_context *ctx, struct nfs4_lock_state *lsp, |
6749 | gfp_t gfp_mask) | |
1da177e4 | 6750 | { |
a5d16a4d TM |
6751 | struct nfs4_lockdata *p; |
6752 | struct inode *inode = lsp->ls_state->inode; | |
1da177e4 | 6753 | struct nfs_server *server = NFS_SERVER(inode); |
b4019c0e | 6754 | struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t); |
a5d16a4d | 6755 | |
8535b2be | 6756 | p = kzalloc(sizeof(*p), gfp_mask); |
a5d16a4d TM |
6757 | if (p == NULL) |
6758 | return NULL; | |
6759 | ||
6760 | p->arg.fh = NFS_FH(inode); | |
6761 | p->arg.fl = &p->fl; | |
8535b2be | 6762 | p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask); |
badc76dd | 6763 | if (IS_ERR(p->arg.open_seqid)) |
2f74c0a0 | 6764 | goto out_free; |
b4019c0e TM |
6765 | alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid; |
6766 | p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask); | |
badc76dd | 6767 | if (IS_ERR(p->arg.lock_seqid)) |
2f74c0a0 | 6768 | goto out_free_seqid; |
7539bbab | 6769 | p->arg.lock_owner.clientid = server->nfs_client->cl_clientid; |
48c22eb2 | 6770 | p->arg.lock_owner.id = lsp->ls_seqid.owner_id; |
d035c36c | 6771 | p->arg.lock_owner.s_dev = server->s_dev; |
c1d51931 | 6772 | p->res.lock_seqid = p->arg.lock_seqid; |
a5d16a4d | 6773 | p->lsp = lsp; |
66179efe | 6774 | p->server = server; |
a5d16a4d | 6775 | p->ctx = get_nfs_open_context(ctx); |
7b587e1a N |
6776 | locks_init_lock(&p->fl); |
6777 | locks_copy_lock(&p->fl, fl); | |
a5d16a4d | 6778 | return p; |
2f74c0a0 TM |
6779 | out_free_seqid: |
6780 | nfs_free_seqid(p->arg.open_seqid); | |
a5d16a4d TM |
6781 | out_free: |
6782 | kfree(p); | |
6783 | return NULL; | |
6784 | } | |
6785 | ||
6786 | static void nfs4_lock_prepare(struct rpc_task *task, void *calldata) | |
6787 | { | |
6788 | struct nfs4_lockdata *data = calldata; | |
6789 | struct nfs4_state *state = data->lsp->ls_state; | |
06735b34 | 6790 | |
3110ff80 | 6791 | dprintk("%s: begin!\n", __func__); |
2f74c0a0 | 6792 | if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0) |
c8da19b9 | 6793 | goto out_wait; |
a5d16a4d | 6794 | /* Do we need to do an open_to_lock_owner? */ |
6b447539 | 6795 | if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) { |
8fe72bac | 6796 | if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) { |
2240a9e2 | 6797 | goto out_release_lock_seqid; |
8fe72bac | 6798 | } |
425c1d4e TM |
6799 | nfs4_stateid_copy(&data->arg.open_stateid, |
6800 | &state->open_stateid); | |
a5d16a4d | 6801 | data->arg.new_lock_owner = 1; |
c1d51931 | 6802 | data->res.open_seqid = data->arg.open_seqid; |
425c1d4e | 6803 | } else { |
2f74c0a0 | 6804 | data->arg.new_lock_owner = 0; |
425c1d4e TM |
6805 | nfs4_stateid_copy(&data->arg.lock_stateid, |
6806 | &data->lsp->ls_stateid); | |
6807 | } | |
5d422301 TM |
6808 | if (!nfs4_valid_open_stateid(state)) { |
6809 | data->rpc_status = -EBADF; | |
6810 | task->tk_action = NULL; | |
6811 | goto out_release_open_seqid; | |
6812 | } | |
26e976a8 | 6813 | data->timestamp = jiffies; |
42e1cca7 | 6814 | if (nfs4_setup_sequence(data->server->nfs_client, |
035168ab | 6815 | &data->arg.seq_args, |
2240a9e2 | 6816 | &data->res.seq_res, |
d9afbd1b | 6817 | task) == 0) |
66179efe | 6818 | return; |
5d422301 | 6819 | out_release_open_seqid: |
2240a9e2 TM |
6820 | nfs_release_seqid(data->arg.open_seqid); |
6821 | out_release_lock_seqid: | |
6822 | nfs_release_seqid(data->arg.lock_seqid); | |
c8da19b9 TM |
6823 | out_wait: |
6824 | nfs4_sequence_done(task, &data->res.seq_res); | |
8fe72bac | 6825 | dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status); |
b257957e AB |
6826 | } |
6827 | ||
a5d16a4d TM |
6828 | static void nfs4_lock_done(struct rpc_task *task, void *calldata) |
6829 | { | |
6830 | struct nfs4_lockdata *data = calldata; | |
39071e6f | 6831 | struct nfs4_lock_state *lsp = data->lsp; |
a5d16a4d | 6832 | |
3110ff80 | 6833 | dprintk("%s: begin!\n", __func__); |
a5d16a4d | 6834 | |
14516c3a TM |
6835 | if (!nfs4_sequence_done(task, &data->res.seq_res)) |
6836 | return; | |
66179efe | 6837 | |
a5d16a4d | 6838 | data->rpc_status = task->tk_status; |
425c1d4e TM |
6839 | switch (task->tk_status) { |
6840 | case 0: | |
2b0143b5 | 6841 | renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)), |
39071e6f | 6842 | data->timestamp); |
a3cf9bca | 6843 | if (data->arg.new_lock && !data->cancelled) { |
c69899a1 | 6844 | data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS); |
a3cf9bca | 6845 | if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) |
6ea76bf5 | 6846 | goto out_restart; |
c69899a1 | 6847 | } |
39071e6f TM |
6848 | if (data->arg.new_lock_owner != 0) { |
6849 | nfs_confirm_seqid(&lsp->ls_seqid, 0); | |
6850 | nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid); | |
6851 | set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags); | |
6ea76bf5 TM |
6852 | } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid)) |
6853 | goto out_restart; | |
425c1d4e TM |
6854 | break; |
6855 | case -NFS4ERR_BAD_STATEID: | |
6856 | case -NFS4ERR_OLD_STATEID: | |
6857 | case -NFS4ERR_STALE_STATEID: | |
6858 | case -NFS4ERR_EXPIRED: | |
6859 | if (data->arg.new_lock_owner != 0) { | |
6ea76bf5 | 6860 | if (!nfs4_stateid_match(&data->arg.open_stateid, |
425c1d4e | 6861 | &lsp->ls_state->open_stateid)) |
6ea76bf5 TM |
6862 | goto out_restart; |
6863 | } else if (!nfs4_stateid_match(&data->arg.lock_stateid, | |
425c1d4e | 6864 | &lsp->ls_stateid)) |
6ea76bf5 | 6865 | goto out_restart; |
a5d16a4d | 6866 | } |
a3cf9bca | 6867 | out_done: |
3110ff80 | 6868 | dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status); |
6ea76bf5 TM |
6869 | return; |
6870 | out_restart: | |
6871 | if (!data->cancelled) | |
6872 | rpc_restart_call_prepare(task); | |
6873 | goto out_done; | |
a5d16a4d TM |
6874 | } |
6875 | ||
6876 | static void nfs4_lock_release(void *calldata) | |
6877 | { | |
6878 | struct nfs4_lockdata *data = calldata; | |
6879 | ||
3110ff80 | 6880 | dprintk("%s: begin!\n", __func__); |
2f74c0a0 | 6881 | nfs_free_seqid(data->arg.open_seqid); |
6ea76bf5 | 6882 | if (data->cancelled && data->rpc_status == 0) { |
a5d16a4d TM |
6883 | struct rpc_task *task; |
6884 | task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp, | |
6885 | data->arg.lock_seqid); | |
6886 | if (!IS_ERR(task)) | |
bf294b41 | 6887 | rpc_put_task_async(task); |
3110ff80 | 6888 | dprintk("%s: cancelling lock!\n", __func__); |
a5d16a4d TM |
6889 | } else |
6890 | nfs_free_seqid(data->arg.lock_seqid); | |
6891 | nfs4_put_lock_state(data->lsp); | |
6892 | put_nfs_open_context(data->ctx); | |
6893 | kfree(data); | |
3110ff80 | 6894 | dprintk("%s: done!\n", __func__); |
a5d16a4d TM |
6895 | } |
6896 | ||
6897 | static const struct rpc_call_ops nfs4_lock_ops = { | |
6898 | .rpc_call_prepare = nfs4_lock_prepare, | |
6899 | .rpc_call_done = nfs4_lock_done, | |
6900 | .rpc_release = nfs4_lock_release, | |
6901 | }; | |
6902 | ||
2bee72a6 TM |
6903 | static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error) |
6904 | { | |
2bee72a6 TM |
6905 | switch (error) { |
6906 | case -NFS4ERR_ADMIN_REVOKED: | |
d7f3e4bf | 6907 | case -NFS4ERR_EXPIRED: |
2bee72a6 | 6908 | case -NFS4ERR_BAD_STATEID: |
ecac799a | 6909 | lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; |
2bee72a6 | 6910 | if (new_lock_owner != 0 || |
795a88c9 | 6911 | test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) |
ecac799a | 6912 | nfs4_schedule_stateid_recovery(server, lsp->ls_state); |
a2c0b9e2 TM |
6913 | break; |
6914 | case -NFS4ERR_STALE_STATEID: | |
a2c0b9e2 | 6915 | lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; |
ecac799a | 6916 | nfs4_schedule_lease_recovery(server->nfs_client); |
8b98a532 | 6917 | } |
2bee72a6 TM |
6918 | } |
6919 | ||
afe6c27c | 6920 | static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type) |
a5d16a4d TM |
6921 | { |
6922 | struct nfs4_lockdata *data; | |
6923 | struct rpc_task *task; | |
5138fde0 TM |
6924 | struct rpc_message msg = { |
6925 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK], | |
6926 | .rpc_cred = state->owner->so_cred, | |
6927 | }; | |
c970aa85 TM |
6928 | struct rpc_task_setup task_setup_data = { |
6929 | .rpc_client = NFS_CLIENT(state->inode), | |
5138fde0 | 6930 | .rpc_message = &msg, |
c970aa85 | 6931 | .callback_ops = &nfs4_lock_ops, |
101070ca | 6932 | .workqueue = nfsiod_workqueue, |
61296507 | 6933 | .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, |
c970aa85 | 6934 | }; |
a5d16a4d TM |
6935 | int ret; |
6936 | ||
3110ff80 | 6937 | dprintk("%s: begin!\n", __func__); |
cd3758e3 | 6938 | data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file), |
8535b2be TM |
6939 | fl->fl_u.nfs4_fl.owner, |
6940 | recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS); | |
a5d16a4d TM |
6941 | if (data == NULL) |
6942 | return -ENOMEM; | |
6943 | if (IS_SETLKW(cmd)) | |
6944 | data->arg.block = 1; | |
fba83f34 AS |
6945 | nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, |
6946 | recovery_type > NFS_LOCK_NEW); | |
1174dd1f TM |
6947 | msg.rpc_argp = &data->arg; |
6948 | msg.rpc_resp = &data->res; | |
c970aa85 | 6949 | task_setup_data.callback_data = data; |
8fe72bac TM |
6950 | if (recovery_type > NFS_LOCK_NEW) { |
6951 | if (recovery_type == NFS_LOCK_RECLAIM) | |
6952 | data->arg.reclaim = NFS_LOCK_RECLAIM; | |
c69899a1 TM |
6953 | } else |
6954 | data->arg.new_lock = 1; | |
c970aa85 | 6955 | task = rpc_run_task(&task_setup_data); |
7a1218a2 | 6956 | if (IS_ERR(task)) |
a5d16a4d | 6957 | return PTR_ERR(task); |
820bf85c | 6958 | ret = rpc_wait_for_completion_task(task); |
a5d16a4d TM |
6959 | if (ret == 0) { |
6960 | ret = data->rpc_status; | |
2bee72a6 TM |
6961 | if (ret) |
6962 | nfs4_handle_setlk_error(data->server, data->lsp, | |
6963 | data->arg.new_lock_owner, ret); | |
a5d16a4d | 6964 | } else |
a7a3b1e9 | 6965 | data->cancelled = true; |
e6b3c4db | 6966 | rpc_put_task(task); |
3110ff80 | 6967 | dprintk("%s: done, ret = %d!\n", __func__, ret); |
48c9579a | 6968 | trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret); |
a5d16a4d | 6969 | return ret; |
1da177e4 LT |
6970 | } |
6971 | ||
6972 | static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request) | |
6973 | { | |
202b50dc | 6974 | struct nfs_server *server = NFS_SERVER(state->inode); |
05ffe24f TM |
6975 | struct nfs4_exception exception = { |
6976 | .inode = state->inode, | |
6977 | }; | |
202b50dc TM |
6978 | int err; |
6979 | ||
6980 | do { | |
42a2d13e TM |
6981 | /* Cache the lock if possible... */ |
6982 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) | |
6983 | return 0; | |
afe6c27c | 6984 | err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM); |
168667c4 | 6985 | if (err != -NFS4ERR_DELAY) |
202b50dc TM |
6986 | break; |
6987 | nfs4_handle_exception(server, err, &exception); | |
6988 | } while (exception.retry); | |
6989 | return err; | |
1da177e4 LT |
6990 | } |
6991 | ||
6992 | static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request) | |
6993 | { | |
202b50dc | 6994 | struct nfs_server *server = NFS_SERVER(state->inode); |
05ffe24f TM |
6995 | struct nfs4_exception exception = { |
6996 | .inode = state->inode, | |
6997 | }; | |
202b50dc TM |
6998 | int err; |
6999 | ||
6bfc93ef TM |
7000 | err = nfs4_set_lock_state(state, request); |
7001 | if (err != 0) | |
7002 | return err; | |
f6de7a39 | 7003 | if (!recover_lost_locks) { |
ef1820f9 N |
7004 | set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags); |
7005 | return 0; | |
7006 | } | |
202b50dc | 7007 | do { |
42a2d13e TM |
7008 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) |
7009 | return 0; | |
afe6c27c | 7010 | err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED); |
a9ed2e25 TM |
7011 | switch (err) { |
7012 | default: | |
7013 | goto out; | |
7014 | case -NFS4ERR_GRACE: | |
7015 | case -NFS4ERR_DELAY: | |
7016 | nfs4_handle_exception(server, err, &exception); | |
7017 | err = 0; | |
7018 | } | |
202b50dc | 7019 | } while (exception.retry); |
a9ed2e25 | 7020 | out: |
202b50dc | 7021 | return err; |
1da177e4 LT |
7022 | } |
7023 | ||
f062eb6c | 7024 | #if defined(CONFIG_NFS_V4_1) |
b01dd1d8 BS |
7025 | static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request) |
7026 | { | |
c5896fc8 TM |
7027 | struct nfs4_lock_state *lsp; |
7028 | int status; | |
b01dd1d8 | 7029 | |
c5896fc8 TM |
7030 | status = nfs4_set_lock_state(state, request); |
7031 | if (status != 0) | |
7032 | return status; | |
7033 | lsp = request->fl_u.nfs4_fl.owner; | |
7034 | if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) || | |
7035 | test_bit(NFS_LOCK_LOST, &lsp->ls_flags)) | |
7036 | return 0; | |
81b68de4 | 7037 | return nfs4_lock_expired(state, request); |
f062eb6c BS |
7038 | } |
7039 | #endif | |
7040 | ||
1da177e4 LT |
7041 | static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
7042 | { | |
19e03c57 | 7043 | struct nfs_inode *nfsi = NFS_I(state->inode); |
11476e9d | 7044 | struct nfs4_state_owner *sp = state->owner; |
01c3b861 | 7045 | unsigned char fl_flags = request->fl_flags; |
1ea67dbd | 7046 | int status; |
1da177e4 | 7047 | |
01c3b861 | 7048 | request->fl_flags |= FL_ACCESS; |
75575ddf | 7049 | status = locks_lock_inode_wait(state->inode, request); |
01c3b861 TM |
7050 | if (status < 0) |
7051 | goto out; | |
11476e9d | 7052 | mutex_lock(&sp->so_delegreturn_mutex); |
19e03c57 | 7053 | down_read(&nfsi->rwsem); |
01c3b861 | 7054 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { |
01c3b861 | 7055 | /* Yes: cache locks! */ |
01c3b861 | 7056 | /* ...but avoid races with delegation recall... */ |
19e03c57 | 7057 | request->fl_flags = fl_flags & ~FL_SLEEP; |
75575ddf | 7058 | status = locks_lock_inode_wait(state->inode, request); |
c69899a1 | 7059 | up_read(&nfsi->rwsem); |
11476e9d | 7060 | mutex_unlock(&sp->so_delegreturn_mutex); |
9a99af49 | 7061 | goto out; |
9a99af49 | 7062 | } |
19e03c57 | 7063 | up_read(&nfsi->rwsem); |
11476e9d | 7064 | mutex_unlock(&sp->so_delegreturn_mutex); |
c69899a1 | 7065 | status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW); |
01c3b861 TM |
7066 | out: |
7067 | request->fl_flags = fl_flags; | |
1da177e4 LT |
7068 | return status; |
7069 | } | |
7070 | ||
7071 | static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) | |
7072 | { | |
a1d0b5ee TM |
7073 | struct nfs4_exception exception = { |
7074 | .state = state, | |
05ffe24f | 7075 | .inode = state->inode, |
0688e64b | 7076 | .interruptible = true, |
a1d0b5ee | 7077 | }; |
1da177e4 LT |
7078 | int err; |
7079 | ||
7080 | do { | |
965b5d67 TM |
7081 | err = _nfs4_proc_setlk(state, cmd, request); |
7082 | if (err == -NFS4ERR_DENIED) | |
7083 | err = -EAGAIN; | |
1da177e4 | 7084 | err = nfs4_handle_exception(NFS_SERVER(state->inode), |
965b5d67 | 7085 | err, &exception); |
1da177e4 LT |
7086 | } while (exception.retry); |
7087 | return err; | |
7088 | } | |
7089 | ||
d2f3a7f9 JL |
7090 | #define NFS4_LOCK_MINTIMEOUT (1 * HZ) |
7091 | #define NFS4_LOCK_MAXTIMEOUT (30 * HZ) | |
7092 | ||
7093 | static int | |
a1d617d8 JL |
7094 | nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd, |
7095 | struct file_lock *request) | |
d2f3a7f9 JL |
7096 | { |
7097 | int status = -ERESTARTSYS; | |
7098 | unsigned long timeout = NFS4_LOCK_MINTIMEOUT; | |
7099 | ||
7100 | while(!signalled()) { | |
7101 | status = nfs4_proc_setlk(state, cmd, request); | |
7102 | if ((status != -EAGAIN) || IS_SETLK(cmd)) | |
7103 | break; | |
7104 | freezable_schedule_timeout_interruptible(timeout); | |
7105 | timeout *= 2; | |
7106 | timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout); | |
7107 | status = -ERESTARTSYS; | |
7108 | } | |
7109 | return status; | |
7110 | } | |
7111 | ||
a1d617d8 JL |
7112 | #ifdef CONFIG_NFS_V4_1 |
7113 | struct nfs4_lock_waiter { | |
7114 | struct task_struct *task; | |
7115 | struct inode *inode; | |
7116 | struct nfs_lowner *owner; | |
a1d617d8 JL |
7117 | }; |
7118 | ||
7119 | static int | |
ac6424b9 | 7120 | nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key) |
a1d617d8 JL |
7121 | { |
7122 | int ret; | |
a1d617d8 | 7123 | struct nfs4_lock_waiter *waiter = wait->private; |
a1d617d8 | 7124 | |
57174593 JL |
7125 | /* NULL key means to wake up everyone */ |
7126 | if (key) { | |
7127 | struct cb_notify_lock_args *cbnl = key; | |
7128 | struct nfs_lowner *lowner = &cbnl->cbnl_owner, | |
7129 | *wowner = waiter->owner; | |
a1d617d8 | 7130 | |
57174593 JL |
7131 | /* Only wake if the callback was for the same owner. */ |
7132 | if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev) | |
7133 | return 0; | |
a1d617d8 | 7134 | |
57174593 JL |
7135 | /* Make sure it's for the right inode */ |
7136 | if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh)) | |
7137 | return 0; | |
57174593 | 7138 | } |
a1d617d8 JL |
7139 | |
7140 | /* override "private" so we can use default_wake_function */ | |
7141 | wait->private = waiter->task; | |
52b042ab YW |
7142 | ret = woken_wake_function(wait, mode, flags, key); |
7143 | if (ret) | |
7144 | list_del_init(&wait->entry); | |
a1d617d8 JL |
7145 | wait->private = waiter; |
7146 | return ret; | |
7147 | } | |
7148 | ||
7149 | static int | |
7150 | nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) | |
7151 | { | |
7152 | int status = -ERESTARTSYS; | |
a1d617d8 JL |
7153 | struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner; |
7154 | struct nfs_server *server = NFS_SERVER(state->inode); | |
7155 | struct nfs_client *clp = server->nfs_client; | |
7156 | wait_queue_head_t *q = &clp->cl_lock_waitq; | |
7157 | struct nfs_lowner owner = { .clientid = clp->cl_clientid, | |
7158 | .id = lsp->ls_seqid.owner_id, | |
7159 | .s_dev = server->s_dev }; | |
7160 | struct nfs4_lock_waiter waiter = { .task = current, | |
7161 | .inode = state->inode, | |
52b042ab | 7162 | .owner = &owner}; |
ac6424b9 | 7163 | wait_queue_entry_t wait; |
a1d617d8 JL |
7164 | |
7165 | /* Don't bother with waitqueue if we don't expect a callback */ | |
7166 | if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags)) | |
7167 | return nfs4_retry_setlk_simple(state, cmd, request); | |
7168 | ||
7169 | init_wait(&wait); | |
7170 | wait.private = &waiter; | |
7171 | wait.func = nfs4_wake_lock_waiter; | |
a1d617d8 JL |
7172 | |
7173 | while(!signalled()) { | |
ba851a39 | 7174 | add_wait_queue(q, &wait); |
a1d617d8 | 7175 | status = nfs4_proc_setlk(state, cmd, request); |
ba851a39 YW |
7176 | if ((status != -EAGAIN) || IS_SETLK(cmd)) { |
7177 | finish_wait(q, &wait); | |
a1d617d8 | 7178 | break; |
ba851a39 | 7179 | } |
a1d617d8 JL |
7180 | |
7181 | status = -ERESTARTSYS; | |
52b042ab YW |
7182 | freezer_do_not_count(); |
7183 | wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT); | |
7184 | freezer_count(); | |
ba851a39 | 7185 | finish_wait(q, &wait); |
a1d617d8 JL |
7186 | } |
7187 | ||
a1d617d8 JL |
7188 | return status; |
7189 | } | |
7190 | #else /* !CONFIG_NFS_V4_1 */ | |
7191 | static inline int | |
7192 | nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) | |
7193 | { | |
7194 | return nfs4_retry_setlk_simple(state, cmd, request); | |
7195 | } | |
7196 | #endif | |
7197 | ||
1da177e4 LT |
7198 | static int |
7199 | nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request) | |
7200 | { | |
7201 | struct nfs_open_context *ctx; | |
7202 | struct nfs4_state *state; | |
1da177e4 LT |
7203 | int status; |
7204 | ||
7205 | /* verify open state */ | |
cd3758e3 | 7206 | ctx = nfs_file_open_context(filp); |
1da177e4 LT |
7207 | state = ctx->state; |
7208 | ||
d953126a TM |
7209 | if (IS_GETLK(cmd)) { |
7210 | if (state != NULL) | |
7211 | return nfs4_proc_getlk(state, F_GETLK, request); | |
7212 | return 0; | |
7213 | } | |
1da177e4 LT |
7214 | |
7215 | if (!(IS_SETLK(cmd) || IS_SETLKW(cmd))) | |
7216 | return -EINVAL; | |
7217 | ||
d953126a TM |
7218 | if (request->fl_type == F_UNLCK) { |
7219 | if (state != NULL) | |
7220 | return nfs4_proc_unlck(state, cmd, request); | |
7221 | return 0; | |
7222 | } | |
1da177e4 | 7223 | |
d953126a TM |
7224 | if (state == NULL) |
7225 | return -ENOLCK; | |
1ea67dbd JL |
7226 | |
7227 | if ((request->fl_flags & FL_POSIX) && | |
7228 | !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags)) | |
7229 | return -ENOLCK; | |
7230 | ||
fcfa4470 BC |
7231 | /* |
7232 | * Don't rely on the VFS having checked the file open mode, | |
7233 | * since it won't do this for flock() locks. | |
7234 | */ | |
7235 | switch (request->fl_type) { | |
7236 | case F_RDLCK: | |
7237 | if (!(filp->f_mode & FMODE_READ)) | |
7238 | return -EBADF; | |
7239 | break; | |
7240 | case F_WRLCK: | |
7241 | if (!(filp->f_mode & FMODE_WRITE)) | |
7242 | return -EBADF; | |
7243 | } | |
7244 | ||
1ea67dbd JL |
7245 | status = nfs4_set_lock_state(state, request); |
7246 | if (status != 0) | |
7247 | return status; | |
7248 | ||
d2f3a7f9 | 7249 | return nfs4_retry_setlk(state, cmd, request); |
1da177e4 LT |
7250 | } |
7251 | ||
db4f2e63 | 7252 | int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid) |
888e694c TM |
7253 | { |
7254 | struct nfs_server *server = NFS_SERVER(state->inode); | |
888e694c TM |
7255 | int err; |
7256 | ||
7257 | err = nfs4_set_lock_state(state, fl); | |
7258 | if (err != 0) | |
db4f2e63 | 7259 | return err; |
4a706fa0 | 7260 | err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW); |
dce2630c | 7261 | return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err); |
888e694c | 7262 | } |
6b3b5496 | 7263 | |
cf470c3e TM |
7264 | struct nfs_release_lockowner_data { |
7265 | struct nfs4_lock_state *lsp; | |
5ae67c4f | 7266 | struct nfs_server *server; |
cf470c3e | 7267 | struct nfs_release_lockowner_args args; |
b7e63a10 | 7268 | struct nfs_release_lockowner_res res; |
60ea6812 | 7269 | unsigned long timestamp; |
cf470c3e TM |
7270 | }; |
7271 | ||
fbd4bfd1 CL |
7272 | static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata) |
7273 | { | |
7274 | struct nfs_release_lockowner_data *data = calldata; | |
5b53dc88 | 7275 | struct nfs_server *server = data->server; |
7981c8a6 AS |
7276 | nfs4_setup_sequence(server->nfs_client, &data->args.seq_args, |
7277 | &data->res.seq_res, task); | |
5b53dc88 | 7278 | data->args.lock_owner.clientid = server->nfs_client->cl_clientid; |
60ea6812 | 7279 | data->timestamp = jiffies; |
fbd4bfd1 CL |
7280 | } |
7281 | ||
7282 | static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata) | |
7283 | { | |
7284 | struct nfs_release_lockowner_data *data = calldata; | |
60ea6812 CL |
7285 | struct nfs_server *server = data->server; |
7286 | ||
b7e63a10 | 7287 | nfs40_sequence_done(task, &data->res.seq_res); |
60ea6812 CL |
7288 | |
7289 | switch (task->tk_status) { | |
7290 | case 0: | |
7291 | renew_lease(server, data->timestamp); | |
7292 | break; | |
7293 | case -NFS4ERR_STALE_CLIENTID: | |
7294 | case -NFS4ERR_EXPIRED: | |
5b53dc88 KM |
7295 | nfs4_schedule_lease_recovery(server->nfs_client); |
7296 | break; | |
60ea6812 CL |
7297 | case -NFS4ERR_LEASE_MOVED: |
7298 | case -NFS4ERR_DELAY: | |
8478eaa1 N |
7299 | if (nfs4_async_handle_error(task, server, |
7300 | NULL, NULL) == -EAGAIN) | |
60ea6812 CL |
7301 | rpc_restart_call_prepare(task); |
7302 | } | |
fbd4bfd1 CL |
7303 | } |
7304 | ||
d3c7b7cc TM |
7305 | static void nfs4_release_lockowner_release(void *calldata) |
7306 | { | |
cf470c3e | 7307 | struct nfs_release_lockowner_data *data = calldata; |
5ae67c4f | 7308 | nfs4_free_lock_state(data->server, data->lsp); |
d3c7b7cc TM |
7309 | kfree(calldata); |
7310 | } | |
7311 | ||
17280175 | 7312 | static const struct rpc_call_ops nfs4_release_lockowner_ops = { |
fbd4bfd1 CL |
7313 | .rpc_call_prepare = nfs4_release_lockowner_prepare, |
7314 | .rpc_call_done = nfs4_release_lockowner_done, | |
d3c7b7cc TM |
7315 | .rpc_release = nfs4_release_lockowner_release, |
7316 | }; | |
7317 | ||
f1cdae87 JL |
7318 | static void |
7319 | nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp) | |
d3c7b7cc | 7320 | { |
cf470c3e | 7321 | struct nfs_release_lockowner_data *data; |
d3c7b7cc TM |
7322 | struct rpc_message msg = { |
7323 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER], | |
7324 | }; | |
7325 | ||
7326 | if (server->nfs_client->cl_mvops->minor_version != 0) | |
f1cdae87 | 7327 | return; |
fbd4bfd1 | 7328 | |
cf470c3e TM |
7329 | data = kmalloc(sizeof(*data), GFP_NOFS); |
7330 | if (!data) | |
f1cdae87 | 7331 | return; |
cf470c3e | 7332 | data->lsp = lsp; |
5ae67c4f | 7333 | data->server = server; |
cf470c3e TM |
7334 | data->args.lock_owner.clientid = server->nfs_client->cl_clientid; |
7335 | data->args.lock_owner.id = lsp->ls_seqid.owner_id; | |
7336 | data->args.lock_owner.s_dev = server->s_dev; | |
fbd4bfd1 | 7337 | |
cf470c3e | 7338 | msg.rpc_argp = &data->args; |
b7e63a10 | 7339 | msg.rpc_resp = &data->res; |
fba83f34 | 7340 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0); |
cf470c3e | 7341 | rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data); |
d3c7b7cc TM |
7342 | } |
7343 | ||
aa1870af BF |
7344 | #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl" |
7345 | ||
d9a82a04 | 7346 | static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler, |
59301226 AV |
7347 | struct dentry *unused, struct inode *inode, |
7348 | const char *key, const void *buf, | |
7349 | size_t buflen, int flags) | |
6b3b5496 | 7350 | { |
59301226 | 7351 | return nfs4_proc_set_acl(inode, buf, buflen); |
6b3b5496 BF |
7352 | } |
7353 | ||
d9a82a04 | 7354 | static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler, |
b296821a AV |
7355 | struct dentry *unused, struct inode *inode, |
7356 | const char *key, void *buf, size_t buflen) | |
6b3b5496 | 7357 | { |
b296821a | 7358 | return nfs4_proc_get_acl(inode, buf, buflen); |
6b3b5496 BF |
7359 | } |
7360 | ||
764a5c6b | 7361 | static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry) |
6b3b5496 | 7362 | { |
764a5c6b | 7363 | return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry))); |
6b3b5496 BF |
7364 | } |
7365 | ||
c9bccef6 | 7366 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
c9bccef6 | 7367 | |
d9a82a04 | 7368 | static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler, |
59301226 AV |
7369 | struct dentry *unused, struct inode *inode, |
7370 | const char *key, const void *buf, | |
7371 | size_t buflen, int flags) | |
c9bccef6 DQ |
7372 | { |
7373 | if (security_ismaclabel(key)) | |
59301226 | 7374 | return nfs4_set_security_label(inode, buf, buflen); |
c9bccef6 DQ |
7375 | |
7376 | return -EOPNOTSUPP; | |
7377 | } | |
7378 | ||
d9a82a04 | 7379 | static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler, |
b296821a AV |
7380 | struct dentry *unused, struct inode *inode, |
7381 | const char *key, void *buf, size_t buflen) | |
c9bccef6 DQ |
7382 | { |
7383 | if (security_ismaclabel(key)) | |
b296821a | 7384 | return nfs4_get_security_label(inode, buf, buflen); |
c9bccef6 DQ |
7385 | return -EOPNOTSUPP; |
7386 | } | |
7387 | ||
c4803c49 AG |
7388 | static ssize_t |
7389 | nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len) | |
c9bccef6 | 7390 | { |
c4803c49 | 7391 | int len = 0; |
c9bccef6 | 7392 | |
c4803c49 AG |
7393 | if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) { |
7394 | len = security_inode_listsecurity(inode, list, list_len); | |
7395 | if (list_len && len > list_len) | |
7396 | return -ERANGE; | |
c9bccef6 DQ |
7397 | } |
7398 | return len; | |
7399 | } | |
7400 | ||
7401 | static const struct xattr_handler nfs4_xattr_nfs4_label_handler = { | |
7402 | .prefix = XATTR_SECURITY_PREFIX, | |
c9bccef6 DQ |
7403 | .get = nfs4_xattr_get_nfs4_label, |
7404 | .set = nfs4_xattr_set_nfs4_label, | |
7405 | }; | |
c9bccef6 | 7406 | |
c4803c49 AG |
7407 | #else |
7408 | ||
7409 | static ssize_t | |
7410 | nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len) | |
7411 | { | |
7412 | return 0; | |
7413 | } | |
7414 | ||
7415 | #endif | |
c9bccef6 | 7416 | |
533eb461 AA |
7417 | /* |
7418 | * nfs_fhget will use either the mounted_on_fileid or the fileid | |
7419 | */ | |
69aaaae1 TM |
7420 | static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr) |
7421 | { | |
533eb461 AA |
7422 | if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) || |
7423 | (fattr->valid & NFS_ATTR_FATTR_FILEID)) && | |
7424 | (fattr->valid & NFS_ATTR_FATTR_FSID) && | |
81934ddb | 7425 | (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS))) |
69aaaae1 TM |
7426 | return; |
7427 | ||
7428 | fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | | |
81934ddb | 7429 | NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL; |
69aaaae1 TM |
7430 | fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; |
7431 | fattr->nlink = 2; | |
7432 | } | |
7433 | ||
f05d147f BS |
7434 | static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, |
7435 | const struct qstr *name, | |
7436 | struct nfs4_fs_locations *fs_locations, | |
7437 | struct page *page) | |
683b57b4 TM |
7438 | { |
7439 | struct nfs_server *server = NFS_SERVER(dir); | |
c05cefcc | 7440 | u32 bitmask[3]; |
683b57b4 TM |
7441 | struct nfs4_fs_locations_arg args = { |
7442 | .dir_fh = NFS_FH(dir), | |
c228fd3a | 7443 | .name = name, |
683b57b4 TM |
7444 | .page = page, |
7445 | .bitmask = bitmask, | |
7446 | }; | |
22958463 BH |
7447 | struct nfs4_fs_locations_res res = { |
7448 | .fs_locations = fs_locations, | |
7449 | }; | |
683b57b4 TM |
7450 | struct rpc_message msg = { |
7451 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], | |
7452 | .rpc_argp = &args, | |
22958463 | 7453 | .rpc_resp = &res, |
683b57b4 TM |
7454 | }; |
7455 | int status; | |
7456 | ||
3110ff80 | 7457 | dprintk("%s: start\n", __func__); |
533eb461 | 7458 | |
c05cefcc CL |
7459 | bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS; |
7460 | bitmask[1] = nfs4_fattr_bitmap[1]; | |
7461 | ||
533eb461 AA |
7462 | /* Ask for the fileid of the absent filesystem if mounted_on_fileid |
7463 | * is not supported */ | |
7464 | if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID) | |
c05cefcc | 7465 | bitmask[0] &= ~FATTR4_WORD0_FILEID; |
533eb461 | 7466 | else |
c05cefcc | 7467 | bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID; |
533eb461 | 7468 | |
c228fd3a | 7469 | nfs_fattr_init(&fs_locations->fattr); |
683b57b4 | 7470 | fs_locations->server = server; |
830b8e33 | 7471 | fs_locations->nlocations = 0; |
f05d147f | 7472 | status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0); |
3110ff80 | 7473 | dprintk("%s: returned status = %d\n", __func__, status); |
683b57b4 TM |
7474 | return status; |
7475 | } | |
7476 | ||
f05d147f BS |
7477 | int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, |
7478 | const struct qstr *name, | |
7479 | struct nfs4_fs_locations *fs_locations, | |
7480 | struct page *page) | |
db0a9593 | 7481 | { |
0688e64b TM |
7482 | struct nfs4_exception exception = { |
7483 | .interruptible = true, | |
7484 | }; | |
db0a9593 BS |
7485 | int err; |
7486 | do { | |
078ea3df TM |
7487 | err = _nfs4_proc_fs_locations(client, dir, name, |
7488 | fs_locations, page); | |
7489 | trace_nfs4_get_fs_locations(dir, name, err); | |
7490 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
db0a9593 BS |
7491 | &exception); |
7492 | } while (exception.retry); | |
7493 | return err; | |
7494 | } | |
7495 | ||
b03d735b CL |
7496 | /* |
7497 | * This operation also signals the server that this client is | |
7498 | * performing migration recovery. The server can stop returning | |
7499 | * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is | |
7500 | * appended to this compound to identify the client ID which is | |
7501 | * performing recovery. | |
7502 | */ | |
7503 | static int _nfs40_proc_get_locations(struct inode *inode, | |
7504 | struct nfs4_fs_locations *locations, | |
a52458b4 | 7505 | struct page *page, const struct cred *cred) |
b03d735b CL |
7506 | { |
7507 | struct nfs_server *server = NFS_SERVER(inode); | |
7508 | struct rpc_clnt *clnt = server->client; | |
7509 | u32 bitmask[2] = { | |
7510 | [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, | |
7511 | }; | |
7512 | struct nfs4_fs_locations_arg args = { | |
7513 | .clientid = server->nfs_client->cl_clientid, | |
7514 | .fh = NFS_FH(inode), | |
7515 | .page = page, | |
7516 | .bitmask = bitmask, | |
7517 | .migration = 1, /* skip LOOKUP */ | |
7518 | .renew = 1, /* append RENEW */ | |
7519 | }; | |
7520 | struct nfs4_fs_locations_res res = { | |
7521 | .fs_locations = locations, | |
7522 | .migration = 1, | |
7523 | .renew = 1, | |
7524 | }; | |
7525 | struct rpc_message msg = { | |
7526 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], | |
7527 | .rpc_argp = &args, | |
7528 | .rpc_resp = &res, | |
7529 | .rpc_cred = cred, | |
7530 | }; | |
7531 | unsigned long now = jiffies; | |
7532 | int status; | |
7533 | ||
7534 | nfs_fattr_init(&locations->fattr); | |
7535 | locations->server = server; | |
7536 | locations->nlocations = 0; | |
7537 | ||
fba83f34 | 7538 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); |
b03d735b CL |
7539 | status = nfs4_call_sync_sequence(clnt, server, &msg, |
7540 | &args.seq_args, &res.seq_res); | |
7541 | if (status) | |
7542 | return status; | |
7543 | ||
7544 | renew_lease(server, now); | |
7545 | return 0; | |
7546 | } | |
7547 | ||
7548 | #ifdef CONFIG_NFS_V4_1 | |
7549 | ||
7550 | /* | |
7551 | * This operation also signals the server that this client is | |
7552 | * performing migration recovery. The server can stop asserting | |
7553 | * SEQ4_STATUS_LEASE_MOVED for this client. The client ID | |
7554 | * performing this operation is identified in the SEQUENCE | |
7555 | * operation in this compound. | |
7556 | * | |
7557 | * When the client supports GETATTR(fs_locations_info), it can | |
7558 | * be plumbed in here. | |
7559 | */ | |
7560 | static int _nfs41_proc_get_locations(struct inode *inode, | |
7561 | struct nfs4_fs_locations *locations, | |
a52458b4 | 7562 | struct page *page, const struct cred *cred) |
b03d735b CL |
7563 | { |
7564 | struct nfs_server *server = NFS_SERVER(inode); | |
7565 | struct rpc_clnt *clnt = server->client; | |
7566 | u32 bitmask[2] = { | |
7567 | [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, | |
7568 | }; | |
7569 | struct nfs4_fs_locations_arg args = { | |
7570 | .fh = NFS_FH(inode), | |
7571 | .page = page, | |
7572 | .bitmask = bitmask, | |
7573 | .migration = 1, /* skip LOOKUP */ | |
7574 | }; | |
7575 | struct nfs4_fs_locations_res res = { | |
7576 | .fs_locations = locations, | |
7577 | .migration = 1, | |
7578 | }; | |
7579 | struct rpc_message msg = { | |
7580 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], | |
7581 | .rpc_argp = &args, | |
7582 | .rpc_resp = &res, | |
7583 | .rpc_cred = cred, | |
7584 | }; | |
7585 | int status; | |
7586 | ||
7587 | nfs_fattr_init(&locations->fattr); | |
7588 | locations->server = server; | |
7589 | locations->nlocations = 0; | |
7590 | ||
fba83f34 | 7591 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); |
b03d735b CL |
7592 | status = nfs4_call_sync_sequence(clnt, server, &msg, |
7593 | &args.seq_args, &res.seq_res); | |
7594 | if (status == NFS4_OK && | |
7595 | res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED) | |
7596 | status = -NFS4ERR_LEASE_MOVED; | |
7597 | return status; | |
7598 | } | |
7599 | ||
7600 | #endif /* CONFIG_NFS_V4_1 */ | |
7601 | ||
7602 | /** | |
7603 | * nfs4_proc_get_locations - discover locations for a migrated FSID | |
7604 | * @inode: inode on FSID that is migrating | |
7605 | * @locations: result of query | |
7606 | * @page: buffer | |
7607 | * @cred: credential to use for this operation | |
7608 | * | |
7609 | * Returns NFS4_OK on success, a negative NFS4ERR status code if the | |
7610 | * operation failed, or a negative errno if a local error occurred. | |
7611 | * | |
7612 | * On success, "locations" is filled in, but if the server has | |
7613 | * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not | |
7614 | * asserted. | |
7615 | * | |
7616 | * -NFS4ERR_LEASE_MOVED is returned if the server still has leases | |
7617 | * from this client that require migration recovery. | |
7618 | */ | |
7619 | int nfs4_proc_get_locations(struct inode *inode, | |
7620 | struct nfs4_fs_locations *locations, | |
a52458b4 | 7621 | struct page *page, const struct cred *cred) |
b03d735b CL |
7622 | { |
7623 | struct nfs_server *server = NFS_SERVER(inode); | |
7624 | struct nfs_client *clp = server->nfs_client; | |
7625 | const struct nfs4_mig_recovery_ops *ops = | |
7626 | clp->cl_mvops->mig_recovery_ops; | |
0688e64b TM |
7627 | struct nfs4_exception exception = { |
7628 | .interruptible = true, | |
7629 | }; | |
b03d735b CL |
7630 | int status; |
7631 | ||
7632 | dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__, | |
7633 | (unsigned long long)server->fsid.major, | |
7634 | (unsigned long long)server->fsid.minor, | |
7635 | clp->cl_hostname); | |
7636 | nfs_display_fhandle(NFS_FH(inode), __func__); | |
7637 | ||
7638 | do { | |
7639 | status = ops->get_locations(inode, locations, page, cred); | |
7640 | if (status != -NFS4ERR_DELAY) | |
7641 | break; | |
7642 | nfs4_handle_exception(server, status, &exception); | |
7643 | } while (exception.retry); | |
7644 | return status; | |
7645 | } | |
7646 | ||
44c99933 CL |
7647 | /* |
7648 | * This operation also signals the server that this client is | |
7649 | * performing "lease moved" recovery. The server can stop | |
7650 | * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation | |
7651 | * is appended to this compound to identify the client ID which is | |
7652 | * performing recovery. | |
7653 | */ | |
a52458b4 | 7654 | static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred) |
44c99933 CL |
7655 | { |
7656 | struct nfs_server *server = NFS_SERVER(inode); | |
7657 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; | |
7658 | struct rpc_clnt *clnt = server->client; | |
7659 | struct nfs4_fsid_present_arg args = { | |
7660 | .fh = NFS_FH(inode), | |
7661 | .clientid = clp->cl_clientid, | |
7662 | .renew = 1, /* append RENEW */ | |
7663 | }; | |
7664 | struct nfs4_fsid_present_res res = { | |
7665 | .renew = 1, | |
7666 | }; | |
7667 | struct rpc_message msg = { | |
7668 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT], | |
7669 | .rpc_argp = &args, | |
7670 | .rpc_resp = &res, | |
7671 | .rpc_cred = cred, | |
7672 | }; | |
7673 | unsigned long now = jiffies; | |
7674 | int status; | |
7675 | ||
7676 | res.fh = nfs_alloc_fhandle(); | |
7677 | if (res.fh == NULL) | |
7678 | return -ENOMEM; | |
7679 | ||
fba83f34 | 7680 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); |
44c99933 CL |
7681 | status = nfs4_call_sync_sequence(clnt, server, &msg, |
7682 | &args.seq_args, &res.seq_res); | |
7683 | nfs_free_fhandle(res.fh); | |
7684 | if (status) | |
7685 | return status; | |
7686 | ||
7687 | do_renew_lease(clp, now); | |
7688 | return 0; | |
7689 | } | |
7690 | ||
7691 | #ifdef CONFIG_NFS_V4_1 | |
7692 | ||
7693 | /* | |
7694 | * This operation also signals the server that this client is | |
7695 | * performing "lease moved" recovery. The server can stop asserting | |
7696 | * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing | |
7697 | * this operation is identified in the SEQUENCE operation in this | |
7698 | * compound. | |
7699 | */ | |
a52458b4 | 7700 | static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred) |
44c99933 CL |
7701 | { |
7702 | struct nfs_server *server = NFS_SERVER(inode); | |
7703 | struct rpc_clnt *clnt = server->client; | |
7704 | struct nfs4_fsid_present_arg args = { | |
7705 | .fh = NFS_FH(inode), | |
7706 | }; | |
7707 | struct nfs4_fsid_present_res res = { | |
7708 | }; | |
7709 | struct rpc_message msg = { | |
7710 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT], | |
7711 | .rpc_argp = &args, | |
7712 | .rpc_resp = &res, | |
7713 | .rpc_cred = cred, | |
7714 | }; | |
7715 | int status; | |
7716 | ||
7717 | res.fh = nfs_alloc_fhandle(); | |
7718 | if (res.fh == NULL) | |
7719 | return -ENOMEM; | |
7720 | ||
fba83f34 | 7721 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); |
44c99933 CL |
7722 | status = nfs4_call_sync_sequence(clnt, server, &msg, |
7723 | &args.seq_args, &res.seq_res); | |
7724 | nfs_free_fhandle(res.fh); | |
7725 | if (status == NFS4_OK && | |
7726 | res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED) | |
7727 | status = -NFS4ERR_LEASE_MOVED; | |
7728 | return status; | |
7729 | } | |
7730 | ||
7731 | #endif /* CONFIG_NFS_V4_1 */ | |
7732 | ||
7733 | /** | |
7734 | * nfs4_proc_fsid_present - Is this FSID present or absent on server? | |
7735 | * @inode: inode on FSID to check | |
7736 | * @cred: credential to use for this operation | |
7737 | * | |
7738 | * Server indicates whether the FSID is present, moved, or not | |
7739 | * recognized. This operation is necessary to clear a LEASE_MOVED | |
7740 | * condition for this client ID. | |
7741 | * | |
7742 | * Returns NFS4_OK if the FSID is present on this server, | |
7743 | * -NFS4ERR_MOVED if the FSID is no longer present, a negative | |
7744 | * NFS4ERR code if some error occurred on the server, or a | |
7745 | * negative errno if a local failure occurred. | |
7746 | */ | |
a52458b4 | 7747 | int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred) |
44c99933 CL |
7748 | { |
7749 | struct nfs_server *server = NFS_SERVER(inode); | |
7750 | struct nfs_client *clp = server->nfs_client; | |
7751 | const struct nfs4_mig_recovery_ops *ops = | |
7752 | clp->cl_mvops->mig_recovery_ops; | |
0688e64b TM |
7753 | struct nfs4_exception exception = { |
7754 | .interruptible = true, | |
7755 | }; | |
44c99933 CL |
7756 | int status; |
7757 | ||
7758 | dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__, | |
7759 | (unsigned long long)server->fsid.major, | |
7760 | (unsigned long long)server->fsid.minor, | |
7761 | clp->cl_hostname); | |
7762 | nfs_display_fhandle(NFS_FH(inode), __func__); | |
7763 | ||
7764 | do { | |
7765 | status = ops->fsid_present(inode, cred); | |
7766 | if (status != -NFS4ERR_DELAY) | |
7767 | break; | |
7768 | nfs4_handle_exception(server, status, &exception); | |
7769 | } while (exception.retry); | |
7770 | return status; | |
7771 | } | |
7772 | ||
302fad7b | 7773 | /* |
a5250def WAA |
7774 | * If 'use_integrity' is true and the state managment nfs_client |
7775 | * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient | |
7776 | * and the machine credential as per RFC3530bis and RFC5661 Security | |
7777 | * Considerations sections. Otherwise, just use the user cred with the | |
7778 | * filesystem's rpc_client. | |
5ec16a85 | 7779 | */ |
a5250def | 7780 | static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity) |
5a5ea0d4 BS |
7781 | { |
7782 | int status; | |
50493364 AS |
7783 | struct rpc_clnt *clnt = NFS_SERVER(dir)->client; |
7784 | struct nfs_client *clp = NFS_SERVER(dir)->nfs_client; | |
5a5ea0d4 BS |
7785 | struct nfs4_secinfo_arg args = { |
7786 | .dir_fh = NFS_FH(dir), | |
7787 | .name = name, | |
7788 | }; | |
7789 | struct nfs4_secinfo_res res = { | |
7790 | .flavors = flavors, | |
7791 | }; | |
7792 | struct rpc_message msg = { | |
7793 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO], | |
7794 | .rpc_argp = &args, | |
7795 | .rpc_resp = &res, | |
7796 | }; | |
50493364 AS |
7797 | struct nfs4_call_sync_data data = { |
7798 | .seq_server = NFS_SERVER(dir), | |
7799 | .seq_args = &args.seq_args, | |
7800 | .seq_res = &res.seq_res, | |
7801 | }; | |
7802 | struct rpc_task_setup task_setup = { | |
7803 | .rpc_client = clnt, | |
7804 | .rpc_message = &msg, | |
7805 | .callback_ops = clp->cl_mvops->call_sync_ops, | |
7806 | .callback_data = &data, | |
7807 | .flags = RPC_TASK_NO_ROUND_ROBIN, | |
7808 | }; | |
a52458b4 | 7809 | const struct cred *cred = NULL; |
a5250def WAA |
7810 | |
7811 | if (use_integrity) { | |
50493364 AS |
7812 | clnt = clp->cl_rpcclient; |
7813 | task_setup.rpc_client = clnt; | |
7814 | ||
7815 | cred = nfs4_get_clid_cred(clp); | |
7cb852df | 7816 | msg.rpc_cred = cred; |
a5250def | 7817 | } |
5a5ea0d4 BS |
7818 | |
7819 | dprintk("NFS call secinfo %s\n", name->name); | |
8b5bee2e | 7820 | |
50493364 AS |
7821 | nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg); |
7822 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0); | |
7823 | status = nfs4_call_sync_custom(&task_setup); | |
8b5bee2e | 7824 | |
5a5ea0d4 | 7825 | dprintk("NFS reply secinfo: %d\n", status); |
a5250def | 7826 | |
a52458b4 | 7827 | put_cred(cred); |
5a5ea0d4 BS |
7828 | return status; |
7829 | } | |
7830 | ||
72de53ec BS |
7831 | int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, |
7832 | struct nfs4_secinfo_flavors *flavors) | |
5a5ea0d4 | 7833 | { |
0688e64b TM |
7834 | struct nfs4_exception exception = { |
7835 | .interruptible = true, | |
7836 | }; | |
5a5ea0d4 BS |
7837 | int err; |
7838 | do { | |
a5250def WAA |
7839 | err = -NFS4ERR_WRONGSEC; |
7840 | ||
7841 | /* try to use integrity protection with machine cred */ | |
7842 | if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client)) | |
7843 | err = _nfs4_proc_secinfo(dir, name, flavors, true); | |
7844 | ||
7845 | /* | |
7846 | * if unable to use integrity protection, or SECINFO with | |
7847 | * integrity protection returns NFS4ERR_WRONGSEC (which is | |
7848 | * disallowed by spec, but exists in deployed servers) use | |
7849 | * the current filesystem's rpc_client and the user cred. | |
7850 | */ | |
7851 | if (err == -NFS4ERR_WRONGSEC) | |
7852 | err = _nfs4_proc_secinfo(dir, name, flavors, false); | |
7853 | ||
078ea3df TM |
7854 | trace_nfs4_secinfo(dir, name, err); |
7855 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
5a5ea0d4 BS |
7856 | &exception); |
7857 | } while (exception.retry); | |
7858 | return err; | |
7859 | } | |
7860 | ||
557134a3 | 7861 | #ifdef CONFIG_NFS_V4_1 |
357f54d6 AA |
7862 | /* |
7863 | * Check the exchange flags returned by the server for invalid flags, having | |
7864 | * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or | |
7865 | * DS flags set. | |
7866 | */ | |
7867 | static int nfs4_check_cl_exchange_flags(u32 flags) | |
7868 | { | |
7869 | if (flags & ~EXCHGID4_FLAG_MASK_R) | |
7870 | goto out_inval; | |
7871 | if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) && | |
7872 | (flags & EXCHGID4_FLAG_USE_NON_PNFS)) | |
7873 | goto out_inval; | |
7874 | if (!(flags & (EXCHGID4_FLAG_MASK_PNFS))) | |
7875 | goto out_inval; | |
7876 | return NFS_OK; | |
7877 | out_inval: | |
7878 | return -NFS4ERR_INVAL; | |
7879 | } | |
7880 | ||
78fe0f41 | 7881 | static bool |
79d4e1f0 CL |
7882 | nfs41_same_server_scope(struct nfs41_server_scope *a, |
7883 | struct nfs41_server_scope *b) | |
78fe0f41 | 7884 | { |
49ad0145 AS |
7885 | if (a->server_scope_sz != b->server_scope_sz) |
7886 | return false; | |
7887 | return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0; | |
78fe0f41 WAA |
7888 | } |
7889 | ||
02a95dee AA |
7890 | static void |
7891 | nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata) | |
7892 | { | |
5c441544 | 7893 | struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp; |
dff58530 | 7894 | struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp; |
5c441544 TM |
7895 | struct nfs_client *clp = args->client; |
7896 | ||
7897 | switch (task->tk_status) { | |
7898 | case -NFS4ERR_BADSESSION: | |
7899 | case -NFS4ERR_DEADSESSION: | |
7900 | nfs4_schedule_session_recovery(clp->cl_session, | |
7901 | task->tk_status); | |
7902 | } | |
dff58530 OK |
7903 | if (args->dir == NFS4_CDFC4_FORE_OR_BOTH && |
7904 | res->dir != NFS4_CDFS4_BOTH) { | |
7905 | rpc_task_close_connection(task); | |
7906 | if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES) | |
7907 | rpc_restart_call(task); | |
7908 | } | |
02a95dee AA |
7909 | } |
7910 | ||
7911 | static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = { | |
7912 | .rpc_call_done = &nfs4_bind_one_conn_to_session_done, | |
7913 | }; | |
7914 | ||
7c44f1ae | 7915 | /* |
d9ddbf5d | 7916 | * nfs4_proc_bind_one_conn_to_session() |
7c44f1ae WAA |
7917 | * |
7918 | * The 4.1 client currently uses the same TCP connection for the | |
7919 | * fore and backchannel. | |
7920 | */ | |
d9ddbf5d TM |
7921 | static |
7922 | int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt, | |
7923 | struct rpc_xprt *xprt, | |
7924 | struct nfs_client *clp, | |
a52458b4 | 7925 | const struct cred *cred) |
7c44f1ae WAA |
7926 | { |
7927 | int status; | |
71a097c6 TM |
7928 | struct nfs41_bind_conn_to_session_args args = { |
7929 | .client = clp, | |
7930 | .dir = NFS4_CDFC4_FORE_OR_BOTH, | |
dff58530 | 7931 | .retries = 0, |
71a097c6 | 7932 | }; |
7c44f1ae WAA |
7933 | struct nfs41_bind_conn_to_session_res res; |
7934 | struct rpc_message msg = { | |
7935 | .rpc_proc = | |
7936 | &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION], | |
71a097c6 | 7937 | .rpc_argp = &args, |
7c44f1ae | 7938 | .rpc_resp = &res, |
2cf047c9 | 7939 | .rpc_cred = cred, |
7c44f1ae | 7940 | }; |
d9ddbf5d TM |
7941 | struct rpc_task_setup task_setup_data = { |
7942 | .rpc_client = clnt, | |
7943 | .rpc_xprt = xprt, | |
02a95dee | 7944 | .callback_ops = &nfs4_bind_one_conn_to_session_ops, |
d9ddbf5d TM |
7945 | .rpc_message = &msg, |
7946 | .flags = RPC_TASK_TIMEOUT, | |
7947 | }; | |
7948 | struct rpc_task *task; | |
7c44f1ae | 7949 | |
71a097c6 TM |
7950 | nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id); |
7951 | if (!(clp->cl_session->flags & SESSION4_BACK_CHAN)) | |
7952 | args.dir = NFS4_CDFC4_FORE; | |
7c44f1ae | 7953 | |
d9ddbf5d TM |
7954 | /* Do not set the backchannel flag unless this is clnt->cl_xprt */ |
7955 | if (xprt != rcu_access_pointer(clnt->cl_xprt)) | |
7956 | args.dir = NFS4_CDFC4_FORE; | |
7957 | ||
7958 | task = rpc_run_task(&task_setup_data); | |
7959 | if (!IS_ERR(task)) { | |
7960 | status = task->tk_status; | |
7961 | rpc_put_task(task); | |
7962 | } else | |
7963 | status = PTR_ERR(task); | |
c6d01c6f | 7964 | trace_nfs4_bind_conn_to_session(clp, status); |
7c44f1ae | 7965 | if (status == 0) { |
71a097c6 | 7966 | if (memcmp(res.sessionid.data, |
7c44f1ae WAA |
7967 | clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) { |
7968 | dprintk("NFS: %s: Session ID mismatch\n", __func__); | |
c7ae7639 | 7969 | return -EIO; |
7c44f1ae | 7970 | } |
71a097c6 | 7971 | if ((res.dir & args.dir) != res.dir || res.dir == 0) { |
7c44f1ae WAA |
7972 | dprintk("NFS: %s: Unexpected direction from server\n", |
7973 | __func__); | |
c7ae7639 | 7974 | return -EIO; |
7c44f1ae | 7975 | } |
71a097c6 | 7976 | if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) { |
7c44f1ae WAA |
7977 | dprintk("NFS: %s: Server returned RDMA mode = true\n", |
7978 | __func__); | |
c7ae7639 | 7979 | return -EIO; |
7c44f1ae WAA |
7980 | } |
7981 | } | |
c7ae7639 | 7982 | |
7c44f1ae WAA |
7983 | return status; |
7984 | } | |
7985 | ||
d9ddbf5d TM |
7986 | struct rpc_bind_conn_calldata { |
7987 | struct nfs_client *clp; | |
a52458b4 | 7988 | const struct cred *cred; |
d9ddbf5d TM |
7989 | }; |
7990 | ||
7991 | static int | |
7992 | nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt, | |
7993 | struct rpc_xprt *xprt, | |
7994 | void *calldata) | |
7995 | { | |
7996 | struct rpc_bind_conn_calldata *p = calldata; | |
7997 | ||
7998 | return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred); | |
7999 | } | |
8000 | ||
a52458b4 | 8001 | int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred) |
d9ddbf5d TM |
8002 | { |
8003 | struct rpc_bind_conn_calldata data = { | |
8004 | .clp = clp, | |
8005 | .cred = cred, | |
8006 | }; | |
8007 | return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient, | |
8008 | nfs4_proc_bind_conn_to_session_callback, &data); | |
8009 | } | |
8010 | ||
99fe60d0 | 8011 | /* |
fa940720 WAA |
8012 | * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map |
8013 | * and operations we'd like to see to enable certain features in the allow map | |
2031cd1a WAA |
8014 | */ |
8015 | static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = { | |
8016 | .how = SP4_MACH_CRED, | |
8017 | .enforce.u.words = { | |
8018 | [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) | | |
8019 | 1 << (OP_EXCHANGE_ID - 32) | | |
8020 | 1 << (OP_CREATE_SESSION - 32) | | |
8021 | 1 << (OP_DESTROY_SESSION - 32) | | |
8022 | 1 << (OP_DESTROY_CLIENTID - 32) | |
fa940720 WAA |
8023 | }, |
8024 | .allow.u.words = { | |
8025 | [0] = 1 << (OP_CLOSE) | | |
99ade3c7 | 8026 | 1 << (OP_OPEN_DOWNGRADE) | |
a0279625 | 8027 | 1 << (OP_LOCKU) | |
99ade3c7 | 8028 | 1 << (OP_DELEGRETURN) | |
a0279625 | 8029 | 1 << (OP_COMMIT), |
8b5bee2e | 8030 | [1] = 1 << (OP_SECINFO - 32) | |
3787d506 | 8031 | 1 << (OP_SECINFO_NO_NAME - 32) | |
99ade3c7 | 8032 | 1 << (OP_LAYOUTRETURN - 32) | |
3787d506 | 8033 | 1 << (OP_TEST_STATEID - 32) | |
a0279625 WAA |
8034 | 1 << (OP_FREE_STATEID - 32) | |
8035 | 1 << (OP_WRITE - 32) | |
2031cd1a WAA |
8036 | } |
8037 | }; | |
8038 | ||
8039 | /* | |
8040 | * Select the state protection mode for client `clp' given the server results | |
8041 | * from exchange_id in `sp'. | |
99fe60d0 | 8042 | * |
2031cd1a WAA |
8043 | * Returns 0 on success, negative errno otherwise. |
8044 | */ | |
8045 | static int nfs4_sp4_select_mode(struct nfs_client *clp, | |
8046 | struct nfs41_state_protection *sp) | |
8047 | { | |
8048 | static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = { | |
8049 | [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) | | |
8050 | 1 << (OP_EXCHANGE_ID - 32) | | |
8051 | 1 << (OP_CREATE_SESSION - 32) | | |
8052 | 1 << (OP_DESTROY_SESSION - 32) | | |
8053 | 1 << (OP_DESTROY_CLIENTID - 32) | |
8054 | }; | |
937e3133 | 8055 | unsigned long flags = 0; |
2031cd1a | 8056 | unsigned int i; |
937e3133 | 8057 | int ret = 0; |
2031cd1a WAA |
8058 | |
8059 | if (sp->how == SP4_MACH_CRED) { | |
8060 | /* Print state protect result */ | |
8061 | dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n"); | |
8062 | for (i = 0; i <= LAST_NFS4_OP; i++) { | |
8063 | if (test_bit(i, sp->enforce.u.longs)) | |
8064 | dfprintk(MOUNT, " enforce op %d\n", i); | |
8065 | if (test_bit(i, sp->allow.u.longs)) | |
8066 | dfprintk(MOUNT, " allow op %d\n", i); | |
8067 | } | |
8068 | ||
8069 | /* make sure nothing is on enforce list that isn't supported */ | |
8070 | for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) { | |
8071 | if (sp->enforce.u.words[i] & ~supported_enforce[i]) { | |
8072 | dfprintk(MOUNT, "sp4_mach_cred: disabled\n"); | |
937e3133 TM |
8073 | ret = -EINVAL; |
8074 | goto out; | |
2031cd1a WAA |
8075 | } |
8076 | } | |
8077 | ||
8078 | /* | |
8079 | * Minimal mode - state operations are allowed to use machine | |
8080 | * credential. Note this already happens by default, so the | |
8081 | * client doesn't have to do anything more than the negotiation. | |
8082 | * | |
8083 | * NOTE: we don't care if EXCHANGE_ID is in the list - | |
8084 | * we're already using the machine cred for exchange_id | |
8085 | * and will never use a different cred. | |
8086 | */ | |
8087 | if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) && | |
8088 | test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) && | |
8089 | test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) && | |
8090 | test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) { | |
8091 | dfprintk(MOUNT, "sp4_mach_cred:\n"); | |
8092 | dfprintk(MOUNT, " minimal mode enabled\n"); | |
937e3133 | 8093 | __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags); |
2031cd1a WAA |
8094 | } else { |
8095 | dfprintk(MOUNT, "sp4_mach_cred: disabled\n"); | |
937e3133 TM |
8096 | ret = -EINVAL; |
8097 | goto out; | |
2031cd1a | 8098 | } |
fa940720 WAA |
8099 | |
8100 | if (test_bit(OP_CLOSE, sp->allow.u.longs) && | |
99ade3c7 AE |
8101 | test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) && |
8102 | test_bit(OP_DELEGRETURN, sp->allow.u.longs) && | |
fa940720 WAA |
8103 | test_bit(OP_LOCKU, sp->allow.u.longs)) { |
8104 | dfprintk(MOUNT, " cleanup mode enabled\n"); | |
937e3133 | 8105 | __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags); |
fa940720 | 8106 | } |
8b5bee2e | 8107 | |
99ade3c7 AE |
8108 | if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) { |
8109 | dfprintk(MOUNT, " pnfs cleanup mode enabled\n"); | |
937e3133 | 8110 | __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags); |
99ade3c7 AE |
8111 | } |
8112 | ||
8b5bee2e WAA |
8113 | if (test_bit(OP_SECINFO, sp->allow.u.longs) && |
8114 | test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) { | |
8115 | dfprintk(MOUNT, " secinfo mode enabled\n"); | |
937e3133 | 8116 | __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags); |
8b5bee2e | 8117 | } |
3787d506 WAA |
8118 | |
8119 | if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) && | |
8120 | test_bit(OP_FREE_STATEID, sp->allow.u.longs)) { | |
8121 | dfprintk(MOUNT, " stateid mode enabled\n"); | |
937e3133 | 8122 | __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags); |
3787d506 | 8123 | } |
8c21c62c WAA |
8124 | |
8125 | if (test_bit(OP_WRITE, sp->allow.u.longs)) { | |
8126 | dfprintk(MOUNT, " write mode enabled\n"); | |
937e3133 | 8127 | __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags); |
8c21c62c WAA |
8128 | } |
8129 | ||
8130 | if (test_bit(OP_COMMIT, sp->allow.u.longs)) { | |
8131 | dfprintk(MOUNT, " commit mode enabled\n"); | |
937e3133 | 8132 | __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags); |
8c21c62c | 8133 | } |
2031cd1a | 8134 | } |
937e3133 TM |
8135 | out: |
8136 | clp->cl_sp4_flags = flags; | |
72bf75cf | 8137 | return ret; |
2031cd1a WAA |
8138 | } |
8139 | ||
8d89bd70 AA |
8140 | struct nfs41_exchange_id_data { |
8141 | struct nfs41_exchange_id_res res; | |
8142 | struct nfs41_exchange_id_args args; | |
8d89bd70 AA |
8143 | }; |
8144 | ||
8d89bd70 AA |
8145 | static void nfs4_exchange_id_release(void *data) |
8146 | { | |
8147 | struct nfs41_exchange_id_data *cdata = | |
8148 | (struct nfs41_exchange_id_data *)data; | |
8149 | ||
63513232 | 8150 | nfs_put_client(cdata->args.client); |
8d89bd70 AA |
8151 | kfree(cdata->res.impl_id); |
8152 | kfree(cdata->res.server_scope); | |
8153 | kfree(cdata->res.server_owner); | |
8154 | kfree(cdata); | |
8155 | } | |
8156 | ||
8157 | static const struct rpc_call_ops nfs4_exchange_id_call_ops = { | |
8d89bd70 AA |
8158 | .rpc_release = nfs4_exchange_id_release, |
8159 | }; | |
8160 | ||
2031cd1a WAA |
8161 | /* |
8162 | * _nfs4_proc_exchange_id() | |
6bbb4ae8 | 8163 | * |
2031cd1a | 8164 | * Wrapper for EXCHANGE_ID operation. |
99fe60d0 | 8165 | */ |
9c760d1f | 8166 | static struct rpc_task * |
a52458b4 | 8167 | nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred, |
ad0849a7 | 8168 | u32 sp4_how, struct rpc_xprt *xprt) |
99fe60d0 | 8169 | { |
99fe60d0 BH |
8170 | struct rpc_message msg = { |
8171 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID], | |
99fe60d0 BH |
8172 | .rpc_cred = cred, |
8173 | }; | |
8d89bd70 AA |
8174 | struct rpc_task_setup task_setup_data = { |
8175 | .rpc_client = clp->cl_rpcclient, | |
8176 | .callback_ops = &nfs4_exchange_id_call_ops, | |
8177 | .rpc_message = &msg, | |
5a0c257f | 8178 | .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN, |
8d89bd70 AA |
8179 | }; |
8180 | struct nfs41_exchange_id_data *calldata; | |
e917f0d1 | 8181 | int status; |
8d89bd70 | 8182 | |
212bf41d | 8183 | if (!refcount_inc_not_zero(&clp->cl_count)) |
9c760d1f | 8184 | return ERR_PTR(-EIO); |
8d89bd70 | 8185 | |
9c760d1f | 8186 | status = -ENOMEM; |
8d89bd70 | 8187 | calldata = kzalloc(sizeof(*calldata), GFP_NOFS); |
9c760d1f TM |
8188 | if (!calldata) |
8189 | goto out; | |
99fe60d0 | 8190 | |
fd40559c | 8191 | nfs4_init_boot_verifier(clp, &calldata->args.verifier); |
873e3851 JL |
8192 | |
8193 | status = nfs4_init_uniform_client_string(clp); | |
8194 | if (status) | |
8d89bd70 | 8195 | goto out_calldata; |
3a6bb738 | 8196 | |
8d89bd70 AA |
8197 | calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner), |
8198 | GFP_NOFS); | |
8199 | status = -ENOMEM; | |
8200 | if (unlikely(calldata->res.server_owner == NULL)) | |
8201 | goto out_calldata; | |
78fe0f41 | 8202 | |
8d89bd70 | 8203 | calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope), |
bbafffd2 | 8204 | GFP_NOFS); |
8d89bd70 | 8205 | if (unlikely(calldata->res.server_scope == NULL)) |
acdeb69d | 8206 | goto out_server_owner; |
78fe0f41 | 8207 | |
8d89bd70 AA |
8208 | calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS); |
8209 | if (unlikely(calldata->res.impl_id == NULL)) | |
7d2ed9ac | 8210 | goto out_server_scope; |
7d2ed9ac | 8211 | |
2031cd1a WAA |
8212 | switch (sp4_how) { |
8213 | case SP4_NONE: | |
8d89bd70 | 8214 | calldata->args.state_protect.how = SP4_NONE; |
2031cd1a WAA |
8215 | break; |
8216 | ||
8217 | case SP4_MACH_CRED: | |
8d89bd70 | 8218 | calldata->args.state_protect = nfs4_sp4_mach_cred_request; |
2031cd1a WAA |
8219 | break; |
8220 | ||
8221 | default: | |
8222 | /* unsupported! */ | |
8223 | WARN_ON_ONCE(1); | |
8224 | status = -EINVAL; | |
6b55970b | 8225 | goto out_impl_id; |
2031cd1a | 8226 | } |
ad0849a7 | 8227 | if (xprt) { |
ad0849a7 | 8228 | task_setup_data.rpc_xprt = xprt; |
d9cb7330 | 8229 | task_setup_data.flags |= RPC_TASK_SOFTCONN; |
fd40559c TM |
8230 | memcpy(calldata->args.verifier.data, clp->cl_confirm.data, |
8231 | sizeof(calldata->args.verifier.data)); | |
ad0849a7 | 8232 | } |
8d89bd70 | 8233 | calldata->args.client = clp; |
8d89bd70 | 8234 | calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER | |
bfab2817 TM |
8235 | EXCHGID4_FLAG_BIND_PRINC_STATEID; |
8236 | #ifdef CONFIG_NFS_V4_1_MIGRATION | |
8237 | calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR; | |
8d89bd70 AA |
8238 | #endif |
8239 | msg.rpc_argp = &calldata->args; | |
8240 | msg.rpc_resp = &calldata->res; | |
8241 | task_setup_data.callback_data = calldata; | |
2031cd1a | 8242 | |
9c760d1f | 8243 | return rpc_run_task(&task_setup_data); |
8d89bd70 AA |
8244 | |
8245 | out_impl_id: | |
8246 | kfree(calldata->res.impl_id); | |
8247 | out_server_scope: | |
8248 | kfree(calldata->res.server_scope); | |
8249 | out_server_owner: | |
8250 | kfree(calldata->res.server_owner); | |
8251 | out_calldata: | |
8252 | kfree(calldata); | |
9c760d1f | 8253 | out: |
63513232 | 8254 | nfs_put_client(clp); |
9c760d1f TM |
8255 | return ERR_PTR(status); |
8256 | } | |
8257 | ||
8258 | /* | |
8259 | * _nfs4_proc_exchange_id() | |
8260 | * | |
8261 | * Wrapper for EXCHANGE_ID operation. | |
8262 | */ | |
a52458b4 | 8263 | static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred, |
9c760d1f TM |
8264 | u32 sp4_how) |
8265 | { | |
8266 | struct rpc_task *task; | |
8267 | struct nfs41_exchange_id_args *argp; | |
8268 | struct nfs41_exchange_id_res *resp; | |
7dc2993a | 8269 | unsigned long now = jiffies; |
9c760d1f TM |
8270 | int status; |
8271 | ||
8272 | task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL); | |
8273 | if (IS_ERR(task)) | |
8274 | return PTR_ERR(task); | |
8275 | ||
8276 | argp = task->tk_msg.rpc_argp; | |
8277 | resp = task->tk_msg.rpc_resp; | |
8278 | status = task->tk_status; | |
8279 | if (status != 0) | |
8280 | goto out; | |
8281 | ||
8282 | status = nfs4_check_cl_exchange_flags(resp->flags); | |
8283 | if (status != 0) | |
8284 | goto out; | |
8285 | ||
8286 | status = nfs4_sp4_select_mode(clp, &resp->state_protect); | |
8287 | if (status != 0) | |
8288 | goto out; | |
8289 | ||
7dc2993a RM |
8290 | do_renew_lease(clp, now); |
8291 | ||
9c760d1f TM |
8292 | clp->cl_clientid = resp->clientid; |
8293 | clp->cl_exchange_flags = resp->flags; | |
8294 | clp->cl_seqid = resp->seqid; | |
8295 | /* Client ID is not confirmed */ | |
8296 | if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R)) | |
8297 | clear_bit(NFS4_SESSION_ESTABLISHED, | |
8298 | &clp->cl_session->session_state); | |
8299 | ||
8300 | if (clp->cl_serverscope != NULL && | |
8301 | !nfs41_same_server_scope(clp->cl_serverscope, | |
8302 | resp->server_scope)) { | |
8303 | dprintk("%s: server_scope mismatch detected\n", | |
8304 | __func__); | |
8305 | set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state); | |
8306 | } | |
8307 | ||
8308 | swap(clp->cl_serverowner, resp->server_owner); | |
8309 | swap(clp->cl_serverscope, resp->server_scope); | |
8310 | swap(clp->cl_implid, resp->impl_id); | |
8311 | ||
8312 | /* Save the EXCHANGE_ID verifier session trunk tests */ | |
8313 | memcpy(clp->cl_confirm.data, argp->verifier.data, | |
8314 | sizeof(clp->cl_confirm.data)); | |
8315 | out: | |
8316 | trace_nfs4_exchange_id(clp, status); | |
8317 | rpc_put_task(task); | |
8318 | return status; | |
99fe60d0 BH |
8319 | } |
8320 | ||
2031cd1a WAA |
8321 | /* |
8322 | * nfs4_proc_exchange_id() | |
8323 | * | |
8324 | * Returns zero, a negative errno, or a negative NFS4ERR status code. | |
8325 | * | |
8326 | * Since the clientid has expired, all compounds using sessions | |
8327 | * associated with the stale clientid will be returning | |
8328 | * NFS4ERR_BADSESSION in the sequence operation, and will therefore | |
8329 | * be in some phase of session reset. | |
8330 | * | |
8331 | * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used. | |
8332 | */ | |
a52458b4 | 8333 | int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred) |
2031cd1a WAA |
8334 | { |
8335 | rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor; | |
8336 | int status; | |
8337 | ||
8338 | /* try SP4_MACH_CRED if krb5i/p */ | |
8339 | if (authflavor == RPC_AUTH_GSS_KRB5I || | |
8340 | authflavor == RPC_AUTH_GSS_KRB5P) { | |
9c760d1f | 8341 | status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED); |
2031cd1a WAA |
8342 | if (!status) |
8343 | return 0; | |
8344 | } | |
8345 | ||
8346 | /* try SP4_NONE */ | |
9c760d1f | 8347 | return _nfs4_proc_exchange_id(clp, cred, SP4_NONE); |
2031cd1a WAA |
8348 | } |
8349 | ||
04fa2c6b AA |
8350 | /** |
8351 | * nfs4_test_session_trunk | |
8352 | * | |
8353 | * This is an add_xprt_test() test function called from | |
8354 | * rpc_clnt_setup_test_and_add_xprt. | |
8355 | * | |
8356 | * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt | |
8357 | * and is dereferrenced in nfs4_exchange_id_release | |
8358 | * | |
8359 | * Upon success, add the new transport to the rpc_clnt | |
8360 | * | |
8361 | * @clnt: struct rpc_clnt to get new transport | |
8362 | * @xprt: the rpc_xprt to test | |
8363 | * @data: call data for _nfs4_proc_exchange_id. | |
8364 | */ | |
10e037d1 | 8365 | void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt, |
04fa2c6b AA |
8366 | void *data) |
8367 | { | |
8368 | struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data; | |
9c760d1f TM |
8369 | struct rpc_task *task; |
8370 | int status; | |
8371 | ||
04fa2c6b AA |
8372 | u32 sp4_how; |
8373 | ||
8374 | dprintk("--> %s try %s\n", __func__, | |
8375 | xprt->address_strings[RPC_DISPLAY_ADDR]); | |
8376 | ||
8377 | sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED); | |
8378 | ||
8379 | /* Test connection for session trunking. Async exchange_id call */ | |
9c760d1f TM |
8380 | task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt); |
8381 | if (IS_ERR(task)) | |
10e037d1 | 8382 | return; |
9c760d1f TM |
8383 | |
8384 | status = task->tk_status; | |
8385 | if (status == 0) | |
8386 | status = nfs4_detect_session_trunking(adata->clp, | |
8387 | task->tk_msg.rpc_resp, xprt); | |
8388 | ||
10e037d1 S |
8389 | if (status == 0) |
8390 | rpc_clnt_xprt_switch_add_xprt(clnt, xprt); | |
8391 | ||
9c760d1f | 8392 | rpc_put_task(task); |
2031cd1a | 8393 | } |
04fa2c6b | 8394 | EXPORT_SYMBOL_GPL(nfs4_test_session_trunk); |
2031cd1a | 8395 | |
66245539 | 8396 | static int _nfs4_proc_destroy_clientid(struct nfs_client *clp, |
a52458b4 | 8397 | const struct cred *cred) |
66245539 TM |
8398 | { |
8399 | struct rpc_message msg = { | |
8400 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID], | |
8401 | .rpc_argp = clp, | |
8402 | .rpc_cred = cred, | |
8403 | }; | |
8404 | int status; | |
8405 | ||
5a0c257f N |
8406 | status = rpc_call_sync(clp->cl_rpcclient, &msg, |
8407 | RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); | |
c6d01c6f | 8408 | trace_nfs4_destroy_clientid(clp, status); |
66245539 | 8409 | if (status) |
02c67525 | 8410 | dprintk("NFS: Got error %d from the server %s on " |
66245539 TM |
8411 | "DESTROY_CLIENTID.", status, clp->cl_hostname); |
8412 | return status; | |
8413 | } | |
8414 | ||
8415 | static int nfs4_proc_destroy_clientid(struct nfs_client *clp, | |
a52458b4 | 8416 | const struct cred *cred) |
66245539 TM |
8417 | { |
8418 | unsigned int loop; | |
8419 | int ret; | |
8420 | ||
8421 | for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) { | |
8422 | ret = _nfs4_proc_destroy_clientid(clp, cred); | |
8423 | switch (ret) { | |
8424 | case -NFS4ERR_DELAY: | |
8425 | case -NFS4ERR_CLIENTID_BUSY: | |
8426 | ssleep(1); | |
8427 | break; | |
8428 | default: | |
8429 | return ret; | |
8430 | } | |
8431 | } | |
8432 | return 0; | |
8433 | } | |
8434 | ||
8435 | int nfs4_destroy_clientid(struct nfs_client *clp) | |
8436 | { | |
a52458b4 | 8437 | const struct cred *cred; |
66245539 TM |
8438 | int ret = 0; |
8439 | ||
8440 | if (clp->cl_mvops->minor_version < 1) | |
8441 | goto out; | |
8442 | if (clp->cl_exchange_flags == 0) | |
8443 | goto out; | |
05f4c350 CL |
8444 | if (clp->cl_preserve_clid) |
8445 | goto out; | |
73d8bde5 | 8446 | cred = nfs4_get_clid_cred(clp); |
66245539 | 8447 | ret = nfs4_proc_destroy_clientid(clp, cred); |
a52458b4 | 8448 | put_cred(cred); |
66245539 TM |
8449 | switch (ret) { |
8450 | case 0: | |
8451 | case -NFS4ERR_STALE_CLIENTID: | |
8452 | clp->cl_exchange_flags = 0; | |
8453 | } | |
8454 | out: | |
8455 | return ret; | |
8456 | } | |
8457 | ||
0efb01b2 DB |
8458 | #endif /* CONFIG_NFS_V4_1 */ |
8459 | ||
2050f0cc AA |
8460 | struct nfs4_get_lease_time_data { |
8461 | struct nfs4_get_lease_time_args *args; | |
8462 | struct nfs4_get_lease_time_res *res; | |
8463 | struct nfs_client *clp; | |
8464 | }; | |
8465 | ||
8466 | static void nfs4_get_lease_time_prepare(struct rpc_task *task, | |
8467 | void *calldata) | |
8468 | { | |
2050f0cc AA |
8469 | struct nfs4_get_lease_time_data *data = |
8470 | (struct nfs4_get_lease_time_data *)calldata; | |
8471 | ||
8472 | dprintk("--> %s\n", __func__); | |
8473 | /* just setup sequence, do not trigger session recovery | |
8474 | since we're invoked within one */ | |
7981c8a6 | 8475 | nfs4_setup_sequence(data->clp, |
d9afbd1b TM |
8476 | &data->args->la_seq_args, |
8477 | &data->res->lr_seq_res, | |
8478 | task); | |
2050f0cc AA |
8479 | dprintk("<-- %s\n", __func__); |
8480 | } | |
8481 | ||
8482 | /* | |
8483 | * Called from nfs4_state_manager thread for session setup, so don't recover | |
8484 | * from sequence operation or clientid errors. | |
8485 | */ | |
8486 | static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata) | |
8487 | { | |
8488 | struct nfs4_get_lease_time_data *data = | |
8489 | (struct nfs4_get_lease_time_data *)calldata; | |
8490 | ||
8491 | dprintk("--> %s\n", __func__); | |
0efb01b2 | 8492 | if (!nfs4_sequence_done(task, &data->res->lr_seq_res)) |
14516c3a | 8493 | return; |
2050f0cc AA |
8494 | switch (task->tk_status) { |
8495 | case -NFS4ERR_DELAY: | |
8496 | case -NFS4ERR_GRACE: | |
8497 | dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status); | |
8498 | rpc_delay(task, NFS4_POLL_RETRY_MIN); | |
8499 | task->tk_status = 0; | |
a8a4ae3a AA |
8500 | /* fall through */ |
8501 | case -NFS4ERR_RETRY_UNCACHED_REP: | |
d00c5d43 | 8502 | rpc_restart_call_prepare(task); |
2050f0cc AA |
8503 | return; |
8504 | } | |
2050f0cc AA |
8505 | dprintk("<-- %s\n", __func__); |
8506 | } | |
8507 | ||
17280175 | 8508 | static const struct rpc_call_ops nfs4_get_lease_time_ops = { |
2050f0cc AA |
8509 | .rpc_call_prepare = nfs4_get_lease_time_prepare, |
8510 | .rpc_call_done = nfs4_get_lease_time_done, | |
8511 | }; | |
8512 | ||
8513 | int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo) | |
8514 | { | |
2050f0cc AA |
8515 | struct nfs4_get_lease_time_args args; |
8516 | struct nfs4_get_lease_time_res res = { | |
8517 | .lr_fsinfo = fsinfo, | |
8518 | }; | |
8519 | struct nfs4_get_lease_time_data data = { | |
8520 | .args = &args, | |
8521 | .res = &res, | |
8522 | .clp = clp, | |
8523 | }; | |
8524 | struct rpc_message msg = { | |
8525 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME], | |
8526 | .rpc_argp = &args, | |
8527 | .rpc_resp = &res, | |
8528 | }; | |
8529 | struct rpc_task_setup task_setup = { | |
8530 | .rpc_client = clp->cl_rpcclient, | |
8531 | .rpc_message = &msg, | |
8532 | .callback_ops = &nfs4_get_lease_time_ops, | |
1bd714f2 TM |
8533 | .callback_data = &data, |
8534 | .flags = RPC_TASK_TIMEOUT, | |
2050f0cc | 8535 | }; |
2050f0cc | 8536 | |
fba83f34 | 8537 | nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1); |
f836b27e | 8538 | return nfs4_call_sync_custom(&task_setup); |
2050f0cc AA |
8539 | } |
8540 | ||
0efb01b2 DB |
8541 | #ifdef CONFIG_NFS_V4_1 |
8542 | ||
fc931582 AA |
8543 | /* |
8544 | * Initialize the values to be used by the client in CREATE_SESSION | |
8545 | * If nfs4_init_session set the fore channel request and response sizes, | |
8546 | * use them. | |
8547 | * | |
8548 | * Set the back channel max_resp_sz_cached to zero to force the client to | |
8549 | * always set csa_cachethis to FALSE because the current implementation | |
8550 | * of the back channel DRC only supports caching the CB_SEQUENCE operation. | |
8551 | */ | |
6b26cc8c CL |
8552 | static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args, |
8553 | struct rpc_clnt *clnt) | |
fc931582 | 8554 | { |
18aad3d5 | 8555 | unsigned int max_rqst_sz, max_resp_sz; |
6b26cc8c | 8556 | unsigned int max_bc_payload = rpc_max_bc_payload(clnt); |
7402a4fe | 8557 | unsigned int max_bc_slots = rpc_num_bc_slots(clnt); |
18aad3d5 AA |
8558 | |
8559 | max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead; | |
8560 | max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead; | |
fc931582 | 8561 | |
fc931582 | 8562 | /* Fore channel attributes */ |
18aad3d5 AA |
8563 | args->fc_attrs.max_rqst_sz = max_rqst_sz; |
8564 | args->fc_attrs.max_resp_sz = max_resp_sz; | |
fc931582 | 8565 | args->fc_attrs.max_ops = NFS4_MAX_OPS; |
ef159e91 | 8566 | args->fc_attrs.max_reqs = max_session_slots; |
fc931582 AA |
8567 | |
8568 | dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u " | |
8e0d46e1 | 8569 | "max_ops=%u max_reqs=%u\n", |
fc931582 AA |
8570 | __func__, |
8571 | args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz, | |
8e0d46e1 | 8572 | args->fc_attrs.max_ops, args->fc_attrs.max_reqs); |
fc931582 AA |
8573 | |
8574 | /* Back channel attributes */ | |
6b26cc8c CL |
8575 | args->bc_attrs.max_rqst_sz = max_bc_payload; |
8576 | args->bc_attrs.max_resp_sz = max_bc_payload; | |
fc931582 AA |
8577 | args->bc_attrs.max_resp_sz_cached = 0; |
8578 | args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS; | |
62421cd9 | 8579 | args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1); |
7402a4fe TM |
8580 | if (args->bc_attrs.max_reqs > max_bc_slots) |
8581 | args->bc_attrs.max_reqs = max_bc_slots; | |
fc931582 AA |
8582 | |
8583 | dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u " | |
8584 | "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n", | |
8585 | __func__, | |
8586 | args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz, | |
8587 | args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops, | |
8588 | args->bc_attrs.max_reqs); | |
8589 | } | |
8590 | ||
79969dd1 TM |
8591 | static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, |
8592 | struct nfs41_create_session_res *res) | |
8d35301d | 8593 | { |
43c2e885 | 8594 | struct nfs4_channel_attrs *sent = &args->fc_attrs; |
79969dd1 | 8595 | struct nfs4_channel_attrs *rcvd = &res->fc_attrs; |
43c2e885 | 8596 | |
43c2e885 BF |
8597 | if (rcvd->max_resp_sz > sent->max_resp_sz) |
8598 | return -EINVAL; | |
8599 | /* | |
8600 | * Our requested max_ops is the minimum we need; we're not | |
8601 | * prepared to break up compounds into smaller pieces than that. | |
8602 | * So, no point even trying to continue if the server won't | |
8603 | * cooperate: | |
8604 | */ | |
8605 | if (rcvd->max_ops < sent->max_ops) | |
8606 | return -EINVAL; | |
8607 | if (rcvd->max_reqs == 0) | |
8608 | return -EINVAL; | |
b4b9a0c1 VG |
8609 | if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE) |
8610 | rcvd->max_reqs = NFS4_MAX_SLOT_TABLE; | |
43c2e885 | 8611 | return 0; |
8d35301d AA |
8612 | } |
8613 | ||
79969dd1 TM |
8614 | static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, |
8615 | struct nfs41_create_session_res *res) | |
43c2e885 BF |
8616 | { |
8617 | struct nfs4_channel_attrs *sent = &args->bc_attrs; | |
79969dd1 | 8618 | struct nfs4_channel_attrs *rcvd = &res->bc_attrs; |
8d35301d | 8619 | |
b1c0df5f TM |
8620 | if (!(res->flags & SESSION4_BACK_CHAN)) |
8621 | goto out; | |
43c2e885 BF |
8622 | if (rcvd->max_rqst_sz > sent->max_rqst_sz) |
8623 | return -EINVAL; | |
8624 | if (rcvd->max_resp_sz < sent->max_resp_sz) | |
8625 | return -EINVAL; | |
8626 | if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached) | |
8627 | return -EINVAL; | |
5405fc44 | 8628 | if (rcvd->max_ops > sent->max_ops) |
43c2e885 | 8629 | return -EINVAL; |
5405fc44 | 8630 | if (rcvd->max_reqs > sent->max_reqs) |
43c2e885 | 8631 | return -EINVAL; |
b1c0df5f | 8632 | out: |
43c2e885 BF |
8633 | return 0; |
8634 | } | |
8d35301d | 8635 | |
8d35301d | 8636 | static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args, |
79969dd1 | 8637 | struct nfs41_create_session_res *res) |
8d35301d | 8638 | { |
43c2e885 | 8639 | int ret; |
8d35301d | 8640 | |
79969dd1 | 8641 | ret = nfs4_verify_fore_channel_attrs(args, res); |
43c2e885 BF |
8642 | if (ret) |
8643 | return ret; | |
79969dd1 TM |
8644 | return nfs4_verify_back_channel_attrs(args, res); |
8645 | } | |
8646 | ||
8647 | static void nfs4_update_session(struct nfs4_session *session, | |
8648 | struct nfs41_create_session_res *res) | |
8649 | { | |
8650 | nfs4_copy_sessionid(&session->sess_id, &res->sessionid); | |
e11259f9 TM |
8651 | /* Mark client id and session as being confirmed */ |
8652 | session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; | |
8653 | set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state); | |
79969dd1 TM |
8654 | session->flags = res->flags; |
8655 | memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs)); | |
b1c0df5f TM |
8656 | if (res->flags & SESSION4_BACK_CHAN) |
8657 | memcpy(&session->bc_attrs, &res->bc_attrs, | |
8658 | sizeof(session->bc_attrs)); | |
8d35301d AA |
8659 | } |
8660 | ||
848f5bda | 8661 | static int _nfs4_proc_create_session(struct nfs_client *clp, |
a52458b4 | 8662 | const struct cred *cred) |
fc931582 AA |
8663 | { |
8664 | struct nfs4_session *session = clp->cl_session; | |
8665 | struct nfs41_create_session_args args = { | |
8666 | .client = clp, | |
79969dd1 TM |
8667 | .clientid = clp->cl_clientid, |
8668 | .seqid = clp->cl_seqid, | |
fc931582 AA |
8669 | .cb_program = NFS4_CALLBACK, |
8670 | }; | |
79969dd1 TM |
8671 | struct nfs41_create_session_res res; |
8672 | ||
fc931582 AA |
8673 | struct rpc_message msg = { |
8674 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION], | |
8675 | .rpc_argp = &args, | |
8676 | .rpc_resp = &res, | |
848f5bda | 8677 | .rpc_cred = cred, |
fc931582 AA |
8678 | }; |
8679 | int status; | |
8680 | ||
6b26cc8c | 8681 | nfs4_init_channel_attrs(&args, clp->cl_rpcclient); |
0f91421e | 8682 | args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN); |
fc931582 | 8683 | |
5a0c257f N |
8684 | status = rpc_call_sync(session->clp->cl_rpcclient, &msg, |
8685 | RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); | |
c6d01c6f | 8686 | trace_nfs4_create_session(clp, status); |
fc931582 | 8687 | |
b519d408 TM |
8688 | switch (status) { |
8689 | case -NFS4ERR_STALE_CLIENTID: | |
8690 | case -NFS4ERR_DELAY: | |
8691 | case -ETIMEDOUT: | |
8692 | case -EACCES: | |
8693 | case -EAGAIN: | |
8694 | goto out; | |
8b98a532 | 8695 | } |
b519d408 TM |
8696 | |
8697 | clp->cl_seqid++; | |
43095d39 | 8698 | if (!status) { |
8d35301d | 8699 | /* Verify the session's negotiated channel_attrs values */ |
79969dd1 | 8700 | status = nfs4_verify_channel_attrs(&args, &res); |
fc931582 | 8701 | /* Increment the clientid slot sequence id */ |
79969dd1 TM |
8702 | if (status) |
8703 | goto out; | |
8704 | nfs4_update_session(session, &res); | |
fc931582 | 8705 | } |
79969dd1 | 8706 | out: |
fc931582 AA |
8707 | return status; |
8708 | } | |
8709 | ||
8710 | /* | |
8711 | * Issues a CREATE_SESSION operation to the server. | |
8712 | * It is the responsibility of the caller to verify the session is | |
8713 | * expired before calling this routine. | |
8714 | */ | |
a52458b4 | 8715 | int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred) |
fc931582 AA |
8716 | { |
8717 | int status; | |
8718 | unsigned *ptr; | |
fc931582 AA |
8719 | struct nfs4_session *session = clp->cl_session; |
8720 | ||
8721 | dprintk("--> %s clp=%p session=%p\n", __func__, clp, session); | |
8722 | ||
848f5bda | 8723 | status = _nfs4_proc_create_session(clp, cred); |
fc931582 AA |
8724 | if (status) |
8725 | goto out; | |
8726 | ||
aacd5537 AA |
8727 | /* Init or reset the session slot tables */ |
8728 | status = nfs4_setup_session_slot_tables(session); | |
8729 | dprintk("slot table setup returned %d\n", status); | |
fc931582 AA |
8730 | if (status) |
8731 | goto out; | |
8732 | ||
8733 | ptr = (unsigned *)&session->sess_id.data[0]; | |
8734 | dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__, | |
8735 | clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]); | |
fc931582 AA |
8736 | out: |
8737 | dprintk("<-- %s\n", __func__); | |
8738 | return status; | |
8739 | } | |
8740 | ||
0f3e66c6 AA |
8741 | /* |
8742 | * Issue the over-the-wire RPC DESTROY_SESSION. | |
8743 | * The caller must serialize access to this routine. | |
8744 | */ | |
848f5bda | 8745 | int nfs4_proc_destroy_session(struct nfs4_session *session, |
a52458b4 | 8746 | const struct cred *cred) |
0f3e66c6 | 8747 | { |
848f5bda TM |
8748 | struct rpc_message msg = { |
8749 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION], | |
8750 | .rpc_argp = session, | |
8751 | .rpc_cred = cred, | |
8752 | }; | |
0f3e66c6 | 8753 | int status = 0; |
0f3e66c6 AA |
8754 | |
8755 | dprintk("--> nfs4_proc_destroy_session\n"); | |
8756 | ||
8757 | /* session is still being setup */ | |
e11259f9 TM |
8758 | if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state)) |
8759 | return 0; | |
0f3e66c6 | 8760 | |
5a0c257f N |
8761 | status = rpc_call_sync(session->clp->cl_rpcclient, &msg, |
8762 | RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN); | |
c6d01c6f | 8763 | trace_nfs4_destroy_session(session->clp, status); |
0f3e66c6 AA |
8764 | |
8765 | if (status) | |
08106ac7 | 8766 | dprintk("NFS: Got error %d from the server on DESTROY_SESSION. " |
0f3e66c6 AA |
8767 | "Session has been destroyed regardless...\n", status); |
8768 | ||
8769 | dprintk("<-- nfs4_proc_destroy_session\n"); | |
8770 | return status; | |
8771 | } | |
8772 | ||
fc01cea9 AA |
8773 | /* |
8774 | * Renew the cl_session lease. | |
8775 | */ | |
d5f8d3fe TM |
8776 | struct nfs4_sequence_data { |
8777 | struct nfs_client *clp; | |
8778 | struct nfs4_sequence_args args; | |
8779 | struct nfs4_sequence_res res; | |
8780 | }; | |
8781 | ||
dc96aef9 AB |
8782 | static void nfs41_sequence_release(void *data) |
8783 | { | |
d5f8d3fe TM |
8784 | struct nfs4_sequence_data *calldata = data; |
8785 | struct nfs_client *clp = calldata->clp; | |
dc96aef9 | 8786 | |
212bf41d | 8787 | if (refcount_read(&clp->cl_count) > 1) |
7135840f AB |
8788 | nfs4_schedule_state_renewal(clp); |
8789 | nfs_put_client(clp); | |
d5f8d3fe | 8790 | kfree(calldata); |
dc96aef9 AB |
8791 | } |
8792 | ||
aa5190d0 TM |
8793 | static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp) |
8794 | { | |
8795 | switch(task->tk_status) { | |
8796 | case -NFS4ERR_DELAY: | |
aa5190d0 TM |
8797 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
8798 | return -EAGAIN; | |
8799 | default: | |
0400a6b0 | 8800 | nfs4_schedule_lease_recovery(clp); |
aa5190d0 TM |
8801 | } |
8802 | return 0; | |
8803 | } | |
8804 | ||
dc96aef9 | 8805 | static void nfs41_sequence_call_done(struct rpc_task *task, void *data) |
fc01cea9 | 8806 | { |
d5f8d3fe TM |
8807 | struct nfs4_sequence_data *calldata = data; |
8808 | struct nfs_client *clp = calldata->clp; | |
fc01cea9 | 8809 | |
14516c3a TM |
8810 | if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp)) |
8811 | return; | |
fc01cea9 | 8812 | |
c6d01c6f | 8813 | trace_nfs4_sequence(clp, task->tk_status); |
fc01cea9 AA |
8814 | if (task->tk_status < 0) { |
8815 | dprintk("%s ERROR %d\n", __func__, task->tk_status); | |
212bf41d | 8816 | if (refcount_read(&clp->cl_count) == 1) |
7135840f | 8817 | goto out; |
fc01cea9 | 8818 | |
aa5190d0 TM |
8819 | if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) { |
8820 | rpc_restart_call_prepare(task); | |
fc01cea9 AA |
8821 | return; |
8822 | } | |
8823 | } | |
fc01cea9 | 8824 | dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred); |
7135840f | 8825 | out: |
fc01cea9 AA |
8826 | dprintk("<-- %s\n", __func__); |
8827 | } | |
8828 | ||
8829 | static void nfs41_sequence_prepare(struct rpc_task *task, void *data) | |
8830 | { | |
d5f8d3fe TM |
8831 | struct nfs4_sequence_data *calldata = data; |
8832 | struct nfs_client *clp = calldata->clp; | |
fc01cea9 AA |
8833 | struct nfs4_sequence_args *args; |
8834 | struct nfs4_sequence_res *res; | |
8835 | ||
fc01cea9 AA |
8836 | args = task->tk_msg.rpc_argp; |
8837 | res = task->tk_msg.rpc_resp; | |
8838 | ||
7981c8a6 | 8839 | nfs4_setup_sequence(clp, args, res, task); |
fc01cea9 AA |
8840 | } |
8841 | ||
8842 | static const struct rpc_call_ops nfs41_sequence_ops = { | |
8843 | .rpc_call_done = nfs41_sequence_call_done, | |
8844 | .rpc_call_prepare = nfs41_sequence_prepare, | |
dc96aef9 | 8845 | .rpc_release = nfs41_sequence_release, |
fc01cea9 AA |
8846 | }; |
8847 | ||
8fe72bac | 8848 | static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, |
a52458b4 | 8849 | const struct cred *cred, |
3be0f80b | 8850 | struct nfs4_slot *slot, |
8fe72bac | 8851 | bool is_privileged) |
fc01cea9 | 8852 | { |
d5f8d3fe | 8853 | struct nfs4_sequence_data *calldata; |
fc01cea9 AA |
8854 | struct rpc_message msg = { |
8855 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE], | |
8856 | .rpc_cred = cred, | |
8857 | }; | |
71ac6da9 TM |
8858 | struct rpc_task_setup task_setup_data = { |
8859 | .rpc_client = clp->cl_rpcclient, | |
8860 | .rpc_message = &msg, | |
8fe72bac | 8861 | .callback_ops = &nfs41_sequence_ops, |
bc7a05ca | 8862 | .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT, |
71ac6da9 | 8863 | }; |
3be0f80b | 8864 | struct rpc_task *ret; |
fc01cea9 | 8865 | |
3be0f80b | 8866 | ret = ERR_PTR(-EIO); |
212bf41d | 8867 | if (!refcount_inc_not_zero(&clp->cl_count)) |
3be0f80b TM |
8868 | goto out_err; |
8869 | ||
8870 | ret = ERR_PTR(-ENOMEM); | |
dfb4f309 | 8871 | calldata = kzalloc(sizeof(*calldata), GFP_NOFS); |
3be0f80b TM |
8872 | if (calldata == NULL) |
8873 | goto out_put_clp; | |
fba83f34 | 8874 | nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged); |
3be0f80b | 8875 | nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot); |
d5f8d3fe TM |
8876 | msg.rpc_argp = &calldata->args; |
8877 | msg.rpc_resp = &calldata->res; | |
8878 | calldata->clp = clp; | |
71ac6da9 | 8879 | task_setup_data.callback_data = calldata; |
fc01cea9 | 8880 | |
3be0f80b TM |
8881 | ret = rpc_run_task(&task_setup_data); |
8882 | if (IS_ERR(ret)) | |
8883 | goto out_err; | |
8884 | return ret; | |
8885 | out_put_clp: | |
8886 | nfs_put_client(clp); | |
8887 | out_err: | |
8888 | nfs41_release_slot(slot); | |
8889 | return ret; | |
71ac6da9 TM |
8890 | } |
8891 | ||
a52458b4 | 8892 | static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags) |
71ac6da9 TM |
8893 | { |
8894 | struct rpc_task *task; | |
8895 | int ret = 0; | |
8896 | ||
2f60ea6b | 8897 | if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0) |
d1f456b0 | 8898 | return -EAGAIN; |
3be0f80b | 8899 | task = _nfs41_proc_sequence(clp, cred, NULL, false); |
71ac6da9 TM |
8900 | if (IS_ERR(task)) |
8901 | ret = PTR_ERR(task); | |
8902 | else | |
bf294b41 | 8903 | rpc_put_task_async(task); |
71ac6da9 TM |
8904 | dprintk("<-- %s status=%d\n", __func__, ret); |
8905 | return ret; | |
8906 | } | |
8907 | ||
a52458b4 | 8908 | static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred) |
71ac6da9 TM |
8909 | { |
8910 | struct rpc_task *task; | |
8911 | int ret; | |
8912 | ||
3be0f80b | 8913 | task = _nfs41_proc_sequence(clp, cred, NULL, true); |
71ac6da9 TM |
8914 | if (IS_ERR(task)) { |
8915 | ret = PTR_ERR(task); | |
8916 | goto out; | |
8917 | } | |
8918 | ret = rpc_wait_for_completion_task(task); | |
be824167 | 8919 | if (!ret) |
71ac6da9 TM |
8920 | ret = task->tk_status; |
8921 | rpc_put_task(task); | |
8922 | out: | |
8923 | dprintk("<-- %s status=%d\n", __func__, ret); | |
8924 | return ret; | |
fc01cea9 AA |
8925 | } |
8926 | ||
fce5c838 RL |
8927 | struct nfs4_reclaim_complete_data { |
8928 | struct nfs_client *clp; | |
8929 | struct nfs41_reclaim_complete_args arg; | |
8930 | struct nfs41_reclaim_complete_res res; | |
8931 | }; | |
8932 | ||
8933 | static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data) | |
8934 | { | |
8935 | struct nfs4_reclaim_complete_data *calldata = data; | |
8936 | ||
7981c8a6 | 8937 | nfs4_setup_sequence(calldata->clp, |
d9afbd1b TM |
8938 | &calldata->arg.seq_args, |
8939 | &calldata->res.seq_res, | |
8940 | task); | |
fce5c838 RL |
8941 | } |
8942 | ||
aa5190d0 TM |
8943 | static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp) |
8944 | { | |
8945 | switch(task->tk_status) { | |
8946 | case 0: | |
57174593 JL |
8947 | wake_up_all(&clp->cl_lock_waitq); |
8948 | /* Fallthrough */ | |
aa5190d0 TM |
8949 | case -NFS4ERR_COMPLETE_ALREADY: |
8950 | case -NFS4ERR_WRONG_CRED: /* What to do here? */ | |
8951 | break; | |
8952 | case -NFS4ERR_DELAY: | |
aa5190d0 | 8953 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
a8a4ae3a AA |
8954 | /* fall through */ |
8955 | case -NFS4ERR_RETRY_UNCACHED_REP: | |
aa5190d0 | 8956 | return -EAGAIN; |
0048fdd0 TM |
8957 | case -NFS4ERR_BADSESSION: |
8958 | case -NFS4ERR_DEADSESSION: | |
8959 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
0048fdd0 | 8960 | break; |
aa5190d0 | 8961 | default: |
0400a6b0 | 8962 | nfs4_schedule_lease_recovery(clp); |
aa5190d0 TM |
8963 | } |
8964 | return 0; | |
8965 | } | |
8966 | ||
fce5c838 RL |
8967 | static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data) |
8968 | { | |
8969 | struct nfs4_reclaim_complete_data *calldata = data; | |
8970 | struct nfs_client *clp = calldata->clp; | |
8971 | struct nfs4_sequence_res *res = &calldata->res.seq_res; | |
8972 | ||
8973 | dprintk("--> %s\n", __func__); | |
14516c3a TM |
8974 | if (!nfs41_sequence_done(task, res)) |
8975 | return; | |
fce5c838 | 8976 | |
c6d01c6f | 8977 | trace_nfs4_reclaim_complete(clp, task->tk_status); |
aa5190d0 TM |
8978 | if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) { |
8979 | rpc_restart_call_prepare(task); | |
8980 | return; | |
8981 | } | |
fce5c838 RL |
8982 | dprintk("<-- %s\n", __func__); |
8983 | } | |
8984 | ||
8985 | static void nfs4_free_reclaim_complete_data(void *data) | |
8986 | { | |
8987 | struct nfs4_reclaim_complete_data *calldata = data; | |
8988 | ||
8989 | kfree(calldata); | |
8990 | } | |
8991 | ||
8992 | static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = { | |
8993 | .rpc_call_prepare = nfs4_reclaim_complete_prepare, | |
8994 | .rpc_call_done = nfs4_reclaim_complete_done, | |
8995 | .rpc_release = nfs4_free_reclaim_complete_data, | |
8996 | }; | |
8997 | ||
8998 | /* | |
8999 | * Issue a global reclaim complete. | |
9000 | */ | |
965e9c23 | 9001 | static int nfs41_proc_reclaim_complete(struct nfs_client *clp, |
a52458b4 | 9002 | const struct cred *cred) |
fce5c838 RL |
9003 | { |
9004 | struct nfs4_reclaim_complete_data *calldata; | |
fce5c838 RL |
9005 | struct rpc_message msg = { |
9006 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE], | |
965e9c23 | 9007 | .rpc_cred = cred, |
fce5c838 RL |
9008 | }; |
9009 | struct rpc_task_setup task_setup_data = { | |
9010 | .rpc_client = clp->cl_rpcclient, | |
9011 | .rpc_message = &msg, | |
9012 | .callback_ops = &nfs4_reclaim_complete_call_ops, | |
4c952e3d | 9013 | .flags = RPC_TASK_NO_ROUND_ROBIN, |
fce5c838 RL |
9014 | }; |
9015 | int status = -ENOMEM; | |
9016 | ||
9017 | dprintk("--> %s\n", __func__); | |
8535b2be | 9018 | calldata = kzalloc(sizeof(*calldata), GFP_NOFS); |
fce5c838 RL |
9019 | if (calldata == NULL) |
9020 | goto out; | |
9021 | calldata->clp = clp; | |
9022 | calldata->arg.one_fs = 0; | |
fce5c838 | 9023 | |
fba83f34 | 9024 | nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1); |
fce5c838 RL |
9025 | msg.rpc_argp = &calldata->arg; |
9026 | msg.rpc_resp = &calldata->res; | |
9027 | task_setup_data.callback_data = calldata; | |
4c952e3d | 9028 | status = nfs4_call_sync_custom(&task_setup_data); |
fce5c838 RL |
9029 | out: |
9030 | dprintk("<-- %s status=%d\n", __func__, status); | |
9031 | return status; | |
9032 | } | |
b1f69b75 AA |
9033 | |
9034 | static void | |
9035 | nfs4_layoutget_prepare(struct rpc_task *task, void *calldata) | |
9036 | { | |
9037 | struct nfs4_layoutget *lgp = calldata; | |
c31663d4 | 9038 | struct nfs_server *server = NFS_SERVER(lgp->args.inode); |
b1f69b75 AA |
9039 | |
9040 | dprintk("--> %s\n", __func__); | |
7981c8a6 | 9041 | nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args, |
183d9e7b JL |
9042 | &lgp->res.seq_res, task); |
9043 | dprintk("<-- %s\n", __func__); | |
b1f69b75 AA |
9044 | } |
9045 | ||
9046 | static void nfs4_layoutget_done(struct rpc_task *task, void *calldata) | |
9047 | { | |
9048 | struct nfs4_layoutget *lgp = calldata; | |
183d9e7b JL |
9049 | |
9050 | dprintk("--> %s\n", __func__); | |
2e80dbe7 | 9051 | nfs41_sequence_process(task, &lgp->res.seq_res); |
183d9e7b JL |
9052 | dprintk("<-- %s\n", __func__); |
9053 | } | |
9054 | ||
9055 | static int | |
9056 | nfs4_layoutget_handle_exception(struct rpc_task *task, | |
9057 | struct nfs4_layoutget *lgp, struct nfs4_exception *exception) | |
9058 | { | |
ee314c2a TM |
9059 | struct inode *inode = lgp->args.inode; |
9060 | struct nfs_server *server = NFS_SERVER(inode); | |
9061 | struct pnfs_layout_hdr *lo; | |
e85d7ee4 TM |
9062 | int nfs4err = task->tk_status; |
9063 | int err, status = 0; | |
f7db0b28 | 9064 | LIST_HEAD(head); |
b1f69b75 | 9065 | |
ed7e5423 | 9066 | dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status); |
b1f69b75 | 9067 | |
2dbf8dff TM |
9068 | nfs4_sequence_free_slot(&lgp->res.seq_res); |
9069 | ||
e85d7ee4 | 9070 | switch (nfs4err) { |
b1f69b75 | 9071 | case 0: |
ee314c2a | 9072 | goto out; |
7c1e6e58 PT |
9073 | |
9074 | /* | |
9075 | * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs | |
9076 | * on the file. set tk_status to -ENODATA to tell upper layer to | |
9077 | * retry go inband. | |
9078 | */ | |
9079 | case -NFS4ERR_LAYOUTUNAVAILABLE: | |
183d9e7b | 9080 | status = -ENODATA; |
7c1e6e58 | 9081 | goto out; |
21b874c8 TM |
9082 | /* |
9083 | * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of | |
9084 | * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3). | |
9085 | */ | |
9086 | case -NFS4ERR_BADLAYOUT: | |
183d9e7b JL |
9087 | status = -EOVERFLOW; |
9088 | goto out; | |
ed7e5423 BH |
9089 | /* |
9090 | * NFS4ERR_LAYOUTTRYLATER is a conflict with another client | |
21b874c8 TM |
9091 | * (or clients) writing to the same RAID stripe except when |
9092 | * the minlength argument is 0 (see RFC5661 section 18.43.3). | |
183d9e7b JL |
9093 | * |
9094 | * Treat it like we would RECALLCONFLICT -- we retry for a little | |
9095 | * while, and then eventually give up. | |
ed7e5423 | 9096 | */ |
b1f69b75 | 9097 | case -NFS4ERR_LAYOUTTRYLATER: |
183d9e7b JL |
9098 | if (lgp->args.minlength == 0) { |
9099 | status = -EOVERFLOW; | |
9100 | goto out; | |
ed7e5423 | 9101 | } |
e85d7ee4 TM |
9102 | status = -EBUSY; |
9103 | break; | |
183d9e7b | 9104 | case -NFS4ERR_RECALLCONFLICT: |
183d9e7b | 9105 | status = -ERECALLCONFLICT; |
e85d7ee4 | 9106 | break; |
26f47443 TM |
9107 | case -NFS4ERR_DELEG_REVOKED: |
9108 | case -NFS4ERR_ADMIN_REVOKED: | |
ee314c2a TM |
9109 | case -NFS4ERR_EXPIRED: |
9110 | case -NFS4ERR_BAD_STATEID: | |
183d9e7b | 9111 | exception->timeout = 0; |
ee314c2a | 9112 | spin_lock(&inode->i_lock); |
f7db0b28 TM |
9113 | lo = NFS_I(inode)->layout; |
9114 | /* If the open stateid was bad, then recover it. */ | |
9115 | if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) || | |
e8fa33a6 | 9116 | !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) { |
ee314c2a | 9117 | spin_unlock(&inode->i_lock); |
183d9e7b | 9118 | exception->state = lgp->args.ctx->state; |
26f47443 | 9119 | exception->stateid = &lgp->args.stateid; |
2259f960 TM |
9120 | break; |
9121 | } | |
f7db0b28 TM |
9122 | |
9123 | /* | |
9124 | * Mark the bad layout state as invalid, then retry | |
9125 | */ | |
668f455d | 9126 | pnfs_mark_layout_stateid_invalid(lo, &head); |
f7db0b28 | 9127 | spin_unlock(&inode->i_lock); |
1f18b82c | 9128 | nfs_commit_inode(inode, 0); |
f7db0b28 TM |
9129 | pnfs_free_lseg_list(&head); |
9130 | status = -EAGAIN; | |
9131 | goto out; | |
b1f69b75 | 9132 | } |
183d9e7b | 9133 | |
e85d7ee4 TM |
9134 | err = nfs4_handle_exception(server, nfs4err, exception); |
9135 | if (!status) { | |
9136 | if (exception->retry) | |
9137 | status = -EAGAIN; | |
9138 | else | |
9139 | status = err; | |
9140 | } | |
ee314c2a | 9141 | out: |
b1f69b75 | 9142 | dprintk("<-- %s\n", __func__); |
183d9e7b | 9143 | return status; |
b1f69b75 AA |
9144 | } |
9145 | ||
dacb452d | 9146 | size_t max_response_pages(struct nfs_server *server) |
8554116e IK |
9147 | { |
9148 | u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; | |
9149 | return nfs_page_array_len(0, max_resp_sz); | |
9150 | } | |
9151 | ||
b1f69b75 AA |
9152 | static void nfs4_layoutget_release(void *calldata) |
9153 | { | |
9154 | struct nfs4_layoutget *lgp = calldata; | |
9155 | ||
9156 | dprintk("--> %s\n", __func__); | |
bd171930 | 9157 | nfs4_sequence_free_slot(&lgp->res.seq_res); |
29a8bfe5 | 9158 | pnfs_layoutget_free(lgp); |
b1f69b75 AA |
9159 | dprintk("<-- %s\n", __func__); |
9160 | } | |
9161 | ||
9162 | static const struct rpc_call_ops nfs4_layoutget_call_ops = { | |
9163 | .rpc_call_prepare = nfs4_layoutget_prepare, | |
9164 | .rpc_call_done = nfs4_layoutget_done, | |
9165 | .rpc_release = nfs4_layoutget_release, | |
9166 | }; | |
9167 | ||
a0b0a6e3 | 9168 | struct pnfs_layout_segment * |
dacb452d | 9169 | nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout) |
b1f69b75 | 9170 | { |
a47970ff WAA |
9171 | struct inode *inode = lgp->args.inode; |
9172 | struct nfs_server *server = NFS_SERVER(inode); | |
b1f69b75 AA |
9173 | struct rpc_task *task; |
9174 | struct rpc_message msg = { | |
9175 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET], | |
9176 | .rpc_argp = &lgp->args, | |
9177 | .rpc_resp = &lgp->res, | |
6ab59344 | 9178 | .rpc_cred = lgp->cred, |
b1f69b75 AA |
9179 | }; |
9180 | struct rpc_task_setup task_setup_data = { | |
9181 | .rpc_client = server->client, | |
9182 | .rpc_message = &msg, | |
9183 | .callback_ops = &nfs4_layoutget_call_ops, | |
9184 | .callback_data = lgp, | |
63ec2b69 | 9185 | .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF, |
b1f69b75 | 9186 | }; |
a0b0a6e3 | 9187 | struct pnfs_layout_segment *lseg = NULL; |
bc23676c TM |
9188 | struct nfs4_exception exception = { |
9189 | .inode = inode, | |
9190 | .timeout = *timeout, | |
9191 | }; | |
b1f69b75 AA |
9192 | int status = 0; |
9193 | ||
9194 | dprintk("--> %s\n", __func__); | |
9195 | ||
4bd5a980 PT |
9196 | /* nfs4_layoutget_release calls pnfs_put_layout_hdr */ |
9197 | pnfs_get_layout_hdr(NFS_I(inode)->layout); | |
9198 | ||
fba83f34 | 9199 | nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0); |
a47970ff | 9200 | |
b1f69b75 | 9201 | task = rpc_run_task(&task_setup_data); |
6e47666e | 9202 | |
820bf85c | 9203 | status = rpc_wait_for_completion_task(task); |
2dbf8dff TM |
9204 | if (status != 0) |
9205 | goto out; | |
9206 | ||
18c0778a | 9207 | if (task->tk_status < 0) { |
183d9e7b JL |
9208 | status = nfs4_layoutget_handle_exception(task, lgp, &exception); |
9209 | *timeout = exception.timeout; | |
18c0778a TM |
9210 | } else if (lgp->res.layoutp->len == 0) { |
9211 | status = -EAGAIN; | |
9212 | *timeout = nfs4_update_delay(&exception.timeout); | |
2dbf8dff TM |
9213 | } else |
9214 | lseg = pnfs_layout_process(lgp); | |
9215 | out: | |
1037e6ea TM |
9216 | trace_nfs4_layoutget(lgp->args.ctx, |
9217 | &lgp->args.range, | |
9218 | &lgp->res.range, | |
48c9579a | 9219 | &lgp->res.stateid, |
1037e6ea | 9220 | status); |
183d9e7b | 9221 | |
b1f69b75 AA |
9222 | rpc_put_task(task); |
9223 | dprintk("<-- %s status=%d\n", __func__, status); | |
a0b0a6e3 TM |
9224 | if (status) |
9225 | return ERR_PTR(status); | |
9226 | return lseg; | |
b1f69b75 AA |
9227 | } |
9228 | ||
cbe82603 BH |
9229 | static void |
9230 | nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata) | |
9231 | { | |
9232 | struct nfs4_layoutreturn *lrp = calldata; | |
9233 | ||
9234 | dprintk("--> %s\n", __func__); | |
7981c8a6 | 9235 | nfs4_setup_sequence(lrp->clp, |
d9afbd1b TM |
9236 | &lrp->args.seq_args, |
9237 | &lrp->res.seq_res, | |
9238 | task); | |
c8bf7073 TM |
9239 | if (!pnfs_layout_is_valid(lrp->args.layout)) |
9240 | rpc_exit(task, 0); | |
cbe82603 BH |
9241 | } |
9242 | ||
9243 | static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata) | |
9244 | { | |
9245 | struct nfs4_layoutreturn *lrp = calldata; | |
9246 | struct nfs_server *server; | |
9247 | ||
9248 | dprintk("--> %s\n", __func__); | |
9249 | ||
2e80dbe7 | 9250 | if (!nfs41_sequence_process(task, &lrp->res.seq_res)) |
cbe82603 BH |
9251 | return; |
9252 | ||
6109bcf7 TM |
9253 | /* |
9254 | * Was there an RPC level error? Assume the call succeeded, | |
9255 | * and that we need to release the layout | |
9256 | */ | |
9257 | if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) { | |
9258 | lrp->res.lrs_present = 0; | |
9259 | return; | |
9260 | } | |
9261 | ||
cbe82603 | 9262 | server = NFS_SERVER(lrp->args.inode); |
f22e5edd | 9263 | switch (task->tk_status) { |
ff90514e | 9264 | case -NFS4ERR_OLD_STATEID: |
30cb3ee2 | 9265 | if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid, |
ecf84026 | 9266 | &lrp->args.range, |
ff90514e TM |
9267 | lrp->args.inode)) |
9268 | goto out_restart; | |
9269 | /* Fallthrough */ | |
f22e5edd TM |
9270 | default: |
9271 | task->tk_status = 0; | |
ff90514e | 9272 | /* Fallthrough */ |
f22e5edd TM |
9273 | case 0: |
9274 | break; | |
9275 | case -NFS4ERR_DELAY: | |
8478eaa1 | 9276 | if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN) |
f22e5edd | 9277 | break; |
ff90514e | 9278 | goto out_restart; |
cbe82603 | 9279 | } |
cbe82603 | 9280 | dprintk("<-- %s\n", __func__); |
ff90514e TM |
9281 | return; |
9282 | out_restart: | |
9283 | task->tk_status = 0; | |
9284 | nfs4_sequence_free_slot(&lrp->res.seq_res); | |
9285 | rpc_restart_call_prepare(task); | |
cbe82603 BH |
9286 | } |
9287 | ||
9288 | static void nfs4_layoutreturn_release(void *calldata) | |
9289 | { | |
9290 | struct nfs4_layoutreturn *lrp = calldata; | |
849b286f | 9291 | struct pnfs_layout_hdr *lo = lrp->args.layout; |
cbe82603 BH |
9292 | |
9293 | dprintk("--> %s\n", __func__); | |
2a974425 | 9294 | pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range, |
68f74479 | 9295 | lrp->res.lrs_present ? &lrp->res.stateid : NULL); |
2e80dbe7 | 9296 | nfs4_sequence_free_slot(&lrp->res.seq_res); |
4d796d75 TM |
9297 | if (lrp->ld_private.ops && lrp->ld_private.ops->free) |
9298 | lrp->ld_private.ops->free(&lrp->ld_private); | |
2f065ddb TM |
9299 | pnfs_put_layout_hdr(lrp->args.layout); |
9300 | nfs_iput_and_deactive(lrp->inode); | |
44ea8dfc | 9301 | put_cred(lrp->cred); |
cbe82603 BH |
9302 | kfree(calldata); |
9303 | dprintk("<-- %s\n", __func__); | |
9304 | } | |
9305 | ||
9306 | static const struct rpc_call_ops nfs4_layoutreturn_call_ops = { | |
9307 | .rpc_call_prepare = nfs4_layoutreturn_prepare, | |
9308 | .rpc_call_done = nfs4_layoutreturn_done, | |
9309 | .rpc_release = nfs4_layoutreturn_release, | |
9310 | }; | |
9311 | ||
6c16605d | 9312 | int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync) |
cbe82603 BH |
9313 | { |
9314 | struct rpc_task *task; | |
9315 | struct rpc_message msg = { | |
9316 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN], | |
9317 | .rpc_argp = &lrp->args, | |
9318 | .rpc_resp = &lrp->res, | |
9556000d | 9319 | .rpc_cred = lrp->cred, |
cbe82603 BH |
9320 | }; |
9321 | struct rpc_task_setup task_setup_data = { | |
1771c577 | 9322 | .rpc_client = NFS_SERVER(lrp->args.inode)->client, |
cbe82603 BH |
9323 | .rpc_message = &msg, |
9324 | .callback_ops = &nfs4_layoutreturn_call_ops, | |
9325 | .callback_data = lrp, | |
9326 | }; | |
6c16605d | 9327 | int status = 0; |
cbe82603 | 9328 | |
99ade3c7 AE |
9329 | nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client, |
9330 | NFS_SP4_MACH_CRED_PNFS_CLEANUP, | |
9331 | &task_setup_data.rpc_client, &msg); | |
9332 | ||
cbe82603 | 9333 | dprintk("--> %s\n", __func__); |
5a0ec8ac TM |
9334 | if (!sync) { |
9335 | lrp->inode = nfs_igrab_and_active(lrp->args.inode); | |
9336 | if (!lrp->inode) { | |
9337 | nfs4_layoutreturn_release(lrp); | |
9338 | return -EAGAIN; | |
9339 | } | |
9340 | task_setup_data.flags |= RPC_TASK_ASYNC; | |
9341 | } | |
fba83f34 | 9342 | nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0); |
cbe82603 BH |
9343 | task = rpc_run_task(&task_setup_data); |
9344 | if (IS_ERR(task)) | |
9345 | return PTR_ERR(task); | |
5a0ec8ac TM |
9346 | if (sync) |
9347 | status = task->tk_status; | |
48c9579a | 9348 | trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status); |
cbe82603 BH |
9349 | dprintk("<-- %s status=%d\n", __func__, status); |
9350 | rpc_put_task(task); | |
9351 | return status; | |
9352 | } | |
9353 | ||
b1f69b75 | 9354 | static int |
cd5875fe TM |
9355 | _nfs4_proc_getdeviceinfo(struct nfs_server *server, |
9356 | struct pnfs_device *pdev, | |
a52458b4 | 9357 | const struct cred *cred) |
b1f69b75 AA |
9358 | { |
9359 | struct nfs4_getdeviceinfo_args args = { | |
9360 | .pdev = pdev, | |
4e590803 TM |
9361 | .notify_types = NOTIFY_DEVICEID4_CHANGE | |
9362 | NOTIFY_DEVICEID4_DELETE, | |
b1f69b75 AA |
9363 | }; |
9364 | struct nfs4_getdeviceinfo_res res = { | |
9365 | .pdev = pdev, | |
9366 | }; | |
9367 | struct rpc_message msg = { | |
9368 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO], | |
9369 | .rpc_argp = &args, | |
9370 | .rpc_resp = &res, | |
cd5875fe | 9371 | .rpc_cred = cred, |
b1f69b75 AA |
9372 | }; |
9373 | int status; | |
9374 | ||
9375 | dprintk("--> %s\n", __func__); | |
7c513058 | 9376 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
4e590803 TM |
9377 | if (res.notification & ~args.notify_types) |
9378 | dprintk("%s: unsupported notification\n", __func__); | |
df52699e TM |
9379 | if (res.notification != args.notify_types) |
9380 | pdev->nocache = 1; | |
4e590803 | 9381 | |
b1f69b75 AA |
9382 | dprintk("<-- %s status=%d\n", __func__, status); |
9383 | ||
9384 | return status; | |
9385 | } | |
9386 | ||
cd5875fe TM |
9387 | int nfs4_proc_getdeviceinfo(struct nfs_server *server, |
9388 | struct pnfs_device *pdev, | |
a52458b4 | 9389 | const struct cred *cred) |
b1f69b75 AA |
9390 | { |
9391 | struct nfs4_exception exception = { }; | |
9392 | int err; | |
9393 | ||
9394 | do { | |
9395 | err = nfs4_handle_exception(server, | |
cd5875fe | 9396 | _nfs4_proc_getdeviceinfo(server, pdev, cred), |
b1f69b75 AA |
9397 | &exception); |
9398 | } while (exception.retry); | |
9399 | return err; | |
9400 | } | |
9401 | EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo); | |
9402 | ||
863a3c6c AA |
9403 | static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata) |
9404 | { | |
9405 | struct nfs4_layoutcommit_data *data = calldata; | |
9406 | struct nfs_server *server = NFS_SERVER(data->args.inode); | |
9407 | ||
7981c8a6 | 9408 | nfs4_setup_sequence(server->nfs_client, |
d9afbd1b TM |
9409 | &data->args.seq_args, |
9410 | &data->res.seq_res, | |
9411 | task); | |
863a3c6c AA |
9412 | } |
9413 | ||
9414 | static void | |
9415 | nfs4_layoutcommit_done(struct rpc_task *task, void *calldata) | |
9416 | { | |
9417 | struct nfs4_layoutcommit_data *data = calldata; | |
9418 | struct nfs_server *server = NFS_SERVER(data->args.inode); | |
9419 | ||
6ba7db34 | 9420 | if (!nfs41_sequence_done(task, &data->res.seq_res)) |
863a3c6c AA |
9421 | return; |
9422 | ||
9423 | switch (task->tk_status) { /* Just ignore these failures */ | |
e59d27e0 TM |
9424 | case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */ |
9425 | case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */ | |
9426 | case -NFS4ERR_BADLAYOUT: /* no layout */ | |
9427 | case -NFS4ERR_GRACE: /* loca_recalim always false */ | |
863a3c6c | 9428 | task->tk_status = 0; |
e59d27e0 | 9429 | case 0: |
e59d27e0 TM |
9430 | break; |
9431 | default: | |
8478eaa1 | 9432 | if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) { |
e59d27e0 TM |
9433 | rpc_restart_call_prepare(task); |
9434 | return; | |
9435 | } | |
9436 | } | |
863a3c6c AA |
9437 | } |
9438 | ||
9439 | static void nfs4_layoutcommit_release(void *calldata) | |
9440 | { | |
9441 | struct nfs4_layoutcommit_data *data = calldata; | |
9442 | ||
db29c089 | 9443 | pnfs_cleanup_layoutcommit(data); |
d8c951c3 TM |
9444 | nfs_post_op_update_inode_force_wcc(data->args.inode, |
9445 | data->res.fattr); | |
a52458b4 | 9446 | put_cred(data->cred); |
472e2594 | 9447 | nfs_iput_and_deactive(data->inode); |
863a3c6c AA |
9448 | kfree(data); |
9449 | } | |
9450 | ||
9451 | static const struct rpc_call_ops nfs4_layoutcommit_ops = { | |
9452 | .rpc_call_prepare = nfs4_layoutcommit_prepare, | |
9453 | .rpc_call_done = nfs4_layoutcommit_done, | |
9454 | .rpc_release = nfs4_layoutcommit_release, | |
9455 | }; | |
9456 | ||
9457 | int | |
ef311537 | 9458 | nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync) |
863a3c6c AA |
9459 | { |
9460 | struct rpc_message msg = { | |
9461 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT], | |
9462 | .rpc_argp = &data->args, | |
9463 | .rpc_resp = &data->res, | |
9464 | .rpc_cred = data->cred, | |
9465 | }; | |
9466 | struct rpc_task_setup task_setup_data = { | |
9467 | .task = &data->task, | |
9468 | .rpc_client = NFS_CLIENT(data->args.inode), | |
9469 | .rpc_message = &msg, | |
9470 | .callback_ops = &nfs4_layoutcommit_ops, | |
9471 | .callback_data = data, | |
863a3c6c AA |
9472 | }; |
9473 | struct rpc_task *task; | |
9474 | int status = 0; | |
9475 | ||
b4839ebe KM |
9476 | dprintk("NFS: initiating layoutcommit call. sync %d " |
9477 | "lbw: %llu inode %lu\n", sync, | |
863a3c6c AA |
9478 | data->args.lastbytewritten, |
9479 | data->args.inode->i_ino); | |
9480 | ||
472e2594 TM |
9481 | if (!sync) { |
9482 | data->inode = nfs_igrab_and_active(data->args.inode); | |
9483 | if (data->inode == NULL) { | |
9484 | nfs4_layoutcommit_release(data); | |
9485 | return -EAGAIN; | |
9486 | } | |
9487 | task_setup_data.flags = RPC_TASK_ASYNC; | |
9488 | } | |
fba83f34 | 9489 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0); |
863a3c6c AA |
9490 | task = rpc_run_task(&task_setup_data); |
9491 | if (IS_ERR(task)) | |
9492 | return PTR_ERR(task); | |
472e2594 TM |
9493 | if (sync) |
9494 | status = task->tk_status; | |
48c9579a | 9495 | trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status); |
863a3c6c AA |
9496 | dprintk("%s: status %d\n", __func__, status); |
9497 | rpc_put_task(task); | |
9498 | return status; | |
9499 | } | |
fca78d6d | 9500 | |
302fad7b | 9501 | /* |
97431204 AA |
9502 | * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if |
9503 | * possible) as per RFC3530bis and RFC5661 Security Considerations sections | |
9504 | */ | |
fca78d6d BS |
9505 | static int |
9506 | _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle, | |
b1b3e136 WAA |
9507 | struct nfs_fsinfo *info, |
9508 | struct nfs4_secinfo_flavors *flavors, bool use_integrity) | |
fca78d6d BS |
9509 | { |
9510 | struct nfs41_secinfo_no_name_args args = { | |
9511 | .style = SECINFO_STYLE_CURRENT_FH, | |
9512 | }; | |
9513 | struct nfs4_secinfo_res res = { | |
9514 | .flavors = flavors, | |
9515 | }; | |
9516 | struct rpc_message msg = { | |
9517 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME], | |
9518 | .rpc_argp = &args, | |
9519 | .rpc_resp = &res, | |
9520 | }; | |
b1b3e136 | 9521 | struct rpc_clnt *clnt = server->client; |
cc15e24a AS |
9522 | struct nfs4_call_sync_data data = { |
9523 | .seq_server = server, | |
9524 | .seq_args = &args.seq_args, | |
9525 | .seq_res = &res.seq_res, | |
9526 | }; | |
9527 | struct rpc_task_setup task_setup = { | |
9528 | .rpc_client = server->client, | |
9529 | .rpc_message = &msg, | |
9530 | .callback_ops = server->nfs_client->cl_mvops->call_sync_ops, | |
9531 | .callback_data = &data, | |
9532 | .flags = RPC_TASK_NO_ROUND_ROBIN, | |
9533 | }; | |
a52458b4 | 9534 | const struct cred *cred = NULL; |
b1b3e136 WAA |
9535 | int status; |
9536 | ||
9537 | if (use_integrity) { | |
9538 | clnt = server->nfs_client->cl_rpcclient; | |
cc15e24a AS |
9539 | task_setup.rpc_client = clnt; |
9540 | ||
7cb852df WAA |
9541 | cred = nfs4_get_clid_cred(server->nfs_client); |
9542 | msg.rpc_cred = cred; | |
b1b3e136 WAA |
9543 | } |
9544 | ||
9545 | dprintk("--> %s\n", __func__); | |
cc15e24a AS |
9546 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0); |
9547 | status = nfs4_call_sync_custom(&task_setup); | |
b1b3e136 WAA |
9548 | dprintk("<-- %s status=%d\n", __func__, status); |
9549 | ||
a52458b4 | 9550 | put_cred(cred); |
b1b3e136 WAA |
9551 | |
9552 | return status; | |
fca78d6d BS |
9553 | } |
9554 | ||
9555 | static int | |
9556 | nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle, | |
9557 | struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors) | |
9558 | { | |
0688e64b TM |
9559 | struct nfs4_exception exception = { |
9560 | .interruptible = true, | |
9561 | }; | |
fca78d6d BS |
9562 | int err; |
9563 | do { | |
b1b3e136 WAA |
9564 | /* first try using integrity protection */ |
9565 | err = -NFS4ERR_WRONGSEC; | |
9566 | ||
9567 | /* try to use integrity protection with machine cred */ | |
9568 | if (_nfs4_is_integrity_protected(server->nfs_client)) | |
9569 | err = _nfs41_proc_secinfo_no_name(server, fhandle, info, | |
9570 | flavors, true); | |
9571 | ||
9572 | /* | |
9573 | * if unable to use integrity protection, or SECINFO with | |
9574 | * integrity protection returns NFS4ERR_WRONGSEC (which is | |
9575 | * disallowed by spec, but exists in deployed servers) use | |
9576 | * the current filesystem's rpc_client and the user cred. | |
9577 | */ | |
9578 | if (err == -NFS4ERR_WRONGSEC) | |
9579 | err = _nfs41_proc_secinfo_no_name(server, fhandle, info, | |
9580 | flavors, false); | |
9581 | ||
fca78d6d BS |
9582 | switch (err) { |
9583 | case 0: | |
9584 | case -NFS4ERR_WRONGSEC: | |
78b19bae | 9585 | case -ENOTSUPP: |
05e9cfb4 | 9586 | goto out; |
fca78d6d BS |
9587 | default: |
9588 | err = nfs4_handle_exception(server, err, &exception); | |
9589 | } | |
9590 | } while (exception.retry); | |
05e9cfb4 | 9591 | out: |
fca78d6d BS |
9592 | return err; |
9593 | } | |
9594 | ||
9595 | static int | |
9596 | nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, | |
9597 | struct nfs_fsinfo *info) | |
9598 | { | |
9599 | int err; | |
9600 | struct page *page; | |
367156d9 | 9601 | rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR; |
fca78d6d | 9602 | struct nfs4_secinfo_flavors *flavors; |
58a8cf12 WAA |
9603 | struct nfs4_secinfo4 *secinfo; |
9604 | int i; | |
fca78d6d BS |
9605 | |
9606 | page = alloc_page(GFP_KERNEL); | |
9607 | if (!page) { | |
9608 | err = -ENOMEM; | |
9609 | goto out; | |
9610 | } | |
9611 | ||
9612 | flavors = page_address(page); | |
9613 | err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors); | |
9614 | ||
9615 | /* | |
9616 | * Fall back on "guess and check" method if | |
9617 | * the server doesn't support SECINFO_NO_NAME | |
9618 | */ | |
78b19bae | 9619 | if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) { |
fca78d6d BS |
9620 | err = nfs4_find_root_sec(server, fhandle, info); |
9621 | goto out_freepage; | |
9622 | } | |
9623 | if (err) | |
9624 | goto out_freepage; | |
9625 | ||
58a8cf12 WAA |
9626 | for (i = 0; i < flavors->num_flavors; i++) { |
9627 | secinfo = &flavors->flavors[i]; | |
9628 | ||
9629 | switch (secinfo->flavor) { | |
9630 | case RPC_AUTH_NULL: | |
9631 | case RPC_AUTH_UNIX: | |
9632 | case RPC_AUTH_GSS: | |
9633 | flavor = rpcauth_get_pseudoflavor(secinfo->flavor, | |
9634 | &secinfo->flavor_info); | |
9635 | break; | |
9636 | default: | |
9637 | flavor = RPC_AUTH_MAXFLAVOR; | |
9638 | break; | |
9639 | } | |
9640 | ||
4d4b69dd WAA |
9641 | if (!nfs_auth_info_match(&server->auth_info, flavor)) |
9642 | flavor = RPC_AUTH_MAXFLAVOR; | |
9643 | ||
58a8cf12 WAA |
9644 | if (flavor != RPC_AUTH_MAXFLAVOR) { |
9645 | err = nfs4_lookup_root_sec(server, fhandle, | |
9646 | info, flavor); | |
9647 | if (!err) | |
9648 | break; | |
9649 | } | |
9650 | } | |
9651 | ||
9652 | if (flavor == RPC_AUTH_MAXFLAVOR) | |
9653 | err = -EPERM; | |
fca78d6d BS |
9654 | |
9655 | out_freepage: | |
9656 | put_page(page); | |
9657 | if (err == -EACCES) | |
9658 | return -EPERM; | |
9659 | out: | |
9660 | return err; | |
9661 | } | |
1cab0652 | 9662 | |
ab7cb0df TM |
9663 | static int _nfs41_test_stateid(struct nfs_server *server, |
9664 | nfs4_stateid *stateid, | |
a52458b4 | 9665 | const struct cred *cred) |
7d974794 BS |
9666 | { |
9667 | int status; | |
9668 | struct nfs41_test_stateid_args args = { | |
1cab0652 | 9669 | .stateid = stateid, |
7d974794 BS |
9670 | }; |
9671 | struct nfs41_test_stateid_res res; | |
9672 | struct rpc_message msg = { | |
9673 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID], | |
9674 | .rpc_argp = &args, | |
9675 | .rpc_resp = &res, | |
ab7cb0df | 9676 | .rpc_cred = cred, |
7d974794 | 9677 | }; |
3787d506 WAA |
9678 | struct rpc_clnt *rpc_client = server->client; |
9679 | ||
9680 | nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID, | |
9681 | &rpc_client, &msg); | |
1cab0652 | 9682 | |
38527b15 | 9683 | dprintk("NFS call test_stateid %p\n", stateid); |
fba83f34 | 9684 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1); |
3787d506 | 9685 | status = nfs4_call_sync_sequence(rpc_client, server, &msg, |
8fe72bac | 9686 | &args.seq_args, &res.seq_res); |
38527b15 CL |
9687 | if (status != NFS_OK) { |
9688 | dprintk("NFS reply test_stateid: failed, %d\n", status); | |
377e507d | 9689 | return status; |
38527b15 CL |
9690 | } |
9691 | dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status); | |
377e507d | 9692 | return -res.status; |
7d974794 BS |
9693 | } |
9694 | ||
43912bbb TM |
9695 | static void nfs4_handle_delay_or_session_error(struct nfs_server *server, |
9696 | int err, struct nfs4_exception *exception) | |
9697 | { | |
9698 | exception->retry = 0; | |
9699 | switch(err) { | |
9700 | case -NFS4ERR_DELAY: | |
76e8a1bd | 9701 | case -NFS4ERR_RETRY_UNCACHED_REP: |
43912bbb TM |
9702 | nfs4_handle_exception(server, err, exception); |
9703 | break; | |
9704 | case -NFS4ERR_BADSESSION: | |
9705 | case -NFS4ERR_BADSLOT: | |
9706 | case -NFS4ERR_BAD_HIGH_SLOT: | |
9707 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
9708 | case -NFS4ERR_DEADSESSION: | |
9709 | nfs4_do_handle_exception(server, err, exception); | |
9710 | } | |
9711 | } | |
9712 | ||
38527b15 CL |
9713 | /** |
9714 | * nfs41_test_stateid - perform a TEST_STATEID operation | |
9715 | * | |
9716 | * @server: server / transport on which to perform the operation | |
9717 | * @stateid: state ID to test | |
ab7cb0df | 9718 | * @cred: credential |
38527b15 CL |
9719 | * |
9720 | * Returns NFS_OK if the server recognizes that "stateid" is valid. | |
9721 | * Otherwise a negative NFS4ERR value is returned if the operation | |
9722 | * failed or the state ID is not currently valid. | |
9723 | */ | |
ab7cb0df TM |
9724 | static int nfs41_test_stateid(struct nfs_server *server, |
9725 | nfs4_stateid *stateid, | |
a52458b4 | 9726 | const struct cred *cred) |
7d974794 | 9727 | { |
0688e64b TM |
9728 | struct nfs4_exception exception = { |
9729 | .interruptible = true, | |
9730 | }; | |
7d974794 BS |
9731 | int err; |
9732 | do { | |
ab7cb0df | 9733 | err = _nfs41_test_stateid(server, stateid, cred); |
43912bbb | 9734 | nfs4_handle_delay_or_session_error(server, err, &exception); |
7d974794 BS |
9735 | } while (exception.retry); |
9736 | return err; | |
9737 | } | |
9aeda35f | 9738 | |
7c1d5fae TM |
9739 | struct nfs_free_stateid_data { |
9740 | struct nfs_server *server; | |
9741 | struct nfs41_free_stateid_args args; | |
9aeda35f | 9742 | struct nfs41_free_stateid_res res; |
7c1d5fae TM |
9743 | }; |
9744 | ||
9745 | static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata) | |
9746 | { | |
9747 | struct nfs_free_stateid_data *data = calldata; | |
7981c8a6 | 9748 | nfs4_setup_sequence(data->server->nfs_client, |
7c1d5fae TM |
9749 | &data->args.seq_args, |
9750 | &data->res.seq_res, | |
9751 | task); | |
9752 | } | |
9753 | ||
9754 | static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata) | |
9755 | { | |
9756 | struct nfs_free_stateid_data *data = calldata; | |
9757 | ||
9758 | nfs41_sequence_done(task, &data->res.seq_res); | |
9759 | ||
9760 | switch (task->tk_status) { | |
9761 | case -NFS4ERR_DELAY: | |
8478eaa1 | 9762 | if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN) |
7c1d5fae TM |
9763 | rpc_restart_call_prepare(task); |
9764 | } | |
9765 | } | |
9766 | ||
9767 | static void nfs41_free_stateid_release(void *calldata) | |
9768 | { | |
9769 | kfree(calldata); | |
9770 | } | |
9771 | ||
17f26b12 | 9772 | static const struct rpc_call_ops nfs41_free_stateid_ops = { |
7c1d5fae TM |
9773 | .rpc_call_prepare = nfs41_free_stateid_prepare, |
9774 | .rpc_call_done = nfs41_free_stateid_done, | |
9775 | .rpc_release = nfs41_free_stateid_release, | |
9776 | }; | |
9777 | ||
2f261020 AS |
9778 | /** |
9779 | * nfs41_free_stateid - perform a FREE_STATEID operation | |
9780 | * | |
9781 | * @server: server / transport on which to perform the operation | |
9782 | * @stateid: state ID to release | |
9783 | * @cred: credential | |
302fad7b | 9784 | * @privileged: set to true if this call needs to be privileged |
2f261020 AS |
9785 | * |
9786 | * Note: this function is always asynchronous. | |
9787 | */ | |
9788 | static int nfs41_free_stateid(struct nfs_server *server, | |
f0b0bf88 | 9789 | const nfs4_stateid *stateid, |
a52458b4 | 9790 | const struct cred *cred, |
7c1d5fae TM |
9791 | bool privileged) |
9792 | { | |
9aeda35f BS |
9793 | struct rpc_message msg = { |
9794 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID], | |
ab7cb0df | 9795 | .rpc_cred = cred, |
9aeda35f | 9796 | }; |
7c1d5fae TM |
9797 | struct rpc_task_setup task_setup = { |
9798 | .rpc_client = server->client, | |
9799 | .rpc_message = &msg, | |
9800 | .callback_ops = &nfs41_free_stateid_ops, | |
9801 | .flags = RPC_TASK_ASYNC, | |
9802 | }; | |
9803 | struct nfs_free_stateid_data *data; | |
2f261020 | 9804 | struct rpc_task *task; |
9aeda35f | 9805 | |
3787d506 WAA |
9806 | nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID, |
9807 | &task_setup.rpc_client, &msg); | |
9808 | ||
38527b15 | 9809 | dprintk("NFS call free_stateid %p\n", stateid); |
7c1d5fae TM |
9810 | data = kmalloc(sizeof(*data), GFP_NOFS); |
9811 | if (!data) | |
2f261020 | 9812 | return -ENOMEM; |
7c1d5fae TM |
9813 | data->server = server; |
9814 | nfs4_stateid_copy(&data->args.stateid, stateid); | |
9815 | ||
9816 | task_setup.callback_data = data; | |
9817 | ||
9818 | msg.rpc_argp = &data->args; | |
9819 | msg.rpc_resp = &data->res; | |
fba83f34 | 9820 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged); |
2f261020 | 9821 | task = rpc_run_task(&task_setup); |
7c1d5fae TM |
9822 | if (IS_ERR(task)) |
9823 | return PTR_ERR(task); | |
7c1d5fae | 9824 | rpc_put_task(task); |
f0b0bf88 | 9825 | return 0; |
9aeda35f | 9826 | } |
36281caa | 9827 | |
f1cdae87 JL |
9828 | static void |
9829 | nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp) | |
c8b2d0bf | 9830 | { |
a52458b4 | 9831 | const struct cred *cred = lsp->ls_state->owner->so_cred; |
c8b2d0bf | 9832 | |
f0b0bf88 | 9833 | nfs41_free_stateid(server, &lsp->ls_stateid, cred, false); |
c8b2d0bf | 9834 | nfs4_free_lock_state(server, lsp); |
c8b2d0bf TM |
9835 | } |
9836 | ||
36281caa TM |
9837 | static bool nfs41_match_stateid(const nfs4_stateid *s1, |
9838 | const nfs4_stateid *s2) | |
9839 | { | |
93b717fd TM |
9840 | if (s1->type != s2->type) |
9841 | return false; | |
9842 | ||
2d2f24ad | 9843 | if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0) |
36281caa TM |
9844 | return false; |
9845 | ||
2d2f24ad | 9846 | if (s1->seqid == s2->seqid) |
36281caa | 9847 | return true; |
36281caa | 9848 | |
045c5519 | 9849 | return s1->seqid == 0 || s2->seqid == 0; |
36281caa TM |
9850 | } |
9851 | ||
557134a3 AA |
9852 | #endif /* CONFIG_NFS_V4_1 */ |
9853 | ||
36281caa TM |
9854 | static bool nfs4_match_stateid(const nfs4_stateid *s1, |
9855 | const nfs4_stateid *s2) | |
9856 | { | |
f597c537 | 9857 | return nfs4_stateid_match(s1, s2); |
36281caa TM |
9858 | } |
9859 | ||
9860 | ||
17280175 | 9861 | static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = { |
7eff03ae | 9862 | .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, |
b79a4a1b | 9863 | .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, |
1da177e4 LT |
9864 | .recover_open = nfs4_open_reclaim, |
9865 | .recover_lock = nfs4_lock_reclaim, | |
591d71cb | 9866 | .establish_clid = nfs4_init_clientid, |
05f4c350 | 9867 | .detect_trunking = nfs40_discover_server_trunking, |
1da177e4 LT |
9868 | }; |
9869 | ||
591d71cb | 9870 | #if defined(CONFIG_NFS_V4_1) |
17280175 | 9871 | static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = { |
591d71cb AA |
9872 | .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, |
9873 | .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, | |
9874 | .recover_open = nfs4_open_reclaim, | |
9875 | .recover_lock = nfs4_lock_reclaim, | |
4d643d1d | 9876 | .establish_clid = nfs41_init_clientid, |
fce5c838 | 9877 | .reclaim_complete = nfs41_proc_reclaim_complete, |
05f4c350 | 9878 | .detect_trunking = nfs41_discover_server_trunking, |
591d71cb AA |
9879 | }; |
9880 | #endif /* CONFIG_NFS_V4_1 */ | |
9881 | ||
17280175 | 9882 | static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = { |
591d71cb AA |
9883 | .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, |
9884 | .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, | |
4dfd4f7a | 9885 | .recover_open = nfs40_open_expired, |
591d71cb AA |
9886 | .recover_lock = nfs4_lock_expired, |
9887 | .establish_clid = nfs4_init_clientid, | |
9888 | }; | |
9889 | ||
9890 | #if defined(CONFIG_NFS_V4_1) | |
17280175 | 9891 | static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = { |
7eff03ae | 9892 | .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, |
b79a4a1b | 9893 | .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, |
f062eb6c BS |
9894 | .recover_open = nfs41_open_expired, |
9895 | .recover_lock = nfs41_lock_expired, | |
4d643d1d | 9896 | .establish_clid = nfs41_init_clientid, |
1da177e4 | 9897 | }; |
591d71cb | 9898 | #endif /* CONFIG_NFS_V4_1 */ |
1da177e4 | 9899 | |
17280175 | 9900 | static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = { |
29fba38b | 9901 | .sched_state_renewal = nfs4_proc_async_renew, |
f15e1e8b | 9902 | .get_state_renewal_cred = nfs4_get_renew_cred, |
8e69514f | 9903 | .renew_lease = nfs4_proc_renew, |
29fba38b BH |
9904 | }; |
9905 | ||
9906 | #if defined(CONFIG_NFS_V4_1) | |
17280175 | 9907 | static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = { |
29fba38b | 9908 | .sched_state_renewal = nfs41_proc_async_sequence, |
f15e1e8b | 9909 | .get_state_renewal_cred = nfs4_get_machine_cred, |
8e69514f | 9910 | .renew_lease = nfs4_proc_sequence, |
29fba38b BH |
9911 | }; |
9912 | #endif | |
9913 | ||
ec011fe8 | 9914 | static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = { |
b03d735b | 9915 | .get_locations = _nfs40_proc_get_locations, |
44c99933 | 9916 | .fsid_present = _nfs40_proc_fsid_present, |
ec011fe8 CL |
9917 | }; |
9918 | ||
9919 | #if defined(CONFIG_NFS_V4_1) | |
9920 | static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = { | |
b03d735b | 9921 | .get_locations = _nfs41_proc_get_locations, |
44c99933 | 9922 | .fsid_present = _nfs41_proc_fsid_present, |
ec011fe8 CL |
9923 | }; |
9924 | #endif /* CONFIG_NFS_V4_1 */ | |
9925 | ||
97dc1359 TM |
9926 | static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = { |
9927 | .minor_version = 0, | |
39c6daae TM |
9928 | .init_caps = NFS_CAP_READDIRPLUS |
9929 | | NFS_CAP_ATOMIC_OPEN | |
39c6daae | 9930 | | NFS_CAP_POSIX_LOCK, |
abf79bb3 CL |
9931 | .init_client = nfs40_init_client, |
9932 | .shutdown_client = nfs40_shutdown_client, | |
36281caa | 9933 | .match_stateid = nfs4_match_stateid, |
fca78d6d | 9934 | .find_root_sec = nfs4_find_root_sec, |
c8b2d0bf | 9935 | .free_lock_state = nfs4_release_lockowner, |
45870d69 | 9936 | .test_and_free_expired = nfs40_test_and_free_expired_stateid, |
63f5f796 | 9937 | .alloc_seqid = nfs_alloc_seqid, |
9915ea7e | 9938 | .call_sync_ops = &nfs40_call_sync_ops, |
c48f4f35 TM |
9939 | .reboot_recovery_ops = &nfs40_reboot_recovery_ops, |
9940 | .nograce_recovery_ops = &nfs40_nograce_recovery_ops, | |
9941 | .state_renewal_ops = &nfs40_state_renewal_ops, | |
ec011fe8 | 9942 | .mig_recovery_ops = &nfs40_mig_recovery_ops, |
97dc1359 TM |
9943 | }; |
9944 | ||
9945 | #if defined(CONFIG_NFS_V4_1) | |
63f5f796 TM |
9946 | static struct nfs_seqid * |
9947 | nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2) | |
9948 | { | |
9949 | return NULL; | |
9950 | } | |
9951 | ||
97dc1359 TM |
9952 | static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = { |
9953 | .minor_version = 1, | |
39c6daae TM |
9954 | .init_caps = NFS_CAP_READDIRPLUS |
9955 | | NFS_CAP_ATOMIC_OPEN | |
3b66486c | 9956 | | NFS_CAP_POSIX_LOCK |
49f9a0fa | 9957 | | NFS_CAP_STATEID_NFSV41 |
6e01260c FI |
9958 | | NFS_CAP_ATOMIC_OPEN_V1 |
9959 | | NFS_CAP_LGOPEN, | |
abf79bb3 CL |
9960 | .init_client = nfs41_init_client, |
9961 | .shutdown_client = nfs41_shutdown_client, | |
36281caa | 9962 | .match_stateid = nfs41_match_stateid, |
fca78d6d | 9963 | .find_root_sec = nfs41_find_root_sec, |
c8b2d0bf | 9964 | .free_lock_state = nfs41_free_lock_state, |
45870d69 | 9965 | .test_and_free_expired = nfs41_test_and_free_expired_stateid, |
63f5f796 | 9966 | .alloc_seqid = nfs_alloc_no_seqid, |
04fa2c6b | 9967 | .session_trunk = nfs4_test_session_trunk, |
9915ea7e | 9968 | .call_sync_ops = &nfs41_call_sync_ops, |
c48f4f35 TM |
9969 | .reboot_recovery_ops = &nfs41_reboot_recovery_ops, |
9970 | .nograce_recovery_ops = &nfs41_nograce_recovery_ops, | |
9971 | .state_renewal_ops = &nfs41_state_renewal_ops, | |
ec011fe8 | 9972 | .mig_recovery_ops = &nfs41_mig_recovery_ops, |
97dc1359 TM |
9973 | }; |
9974 | #endif | |
9975 | ||
42c2c424 SD |
9976 | #if defined(CONFIG_NFS_V4_2) |
9977 | static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = { | |
9978 | .minor_version = 2, | |
7017310a BS |
9979 | .init_caps = NFS_CAP_READDIRPLUS |
9980 | | NFS_CAP_ATOMIC_OPEN | |
7017310a BS |
9981 | | NFS_CAP_POSIX_LOCK |
9982 | | NFS_CAP_STATEID_NFSV41 | |
e983120e | 9983 | | NFS_CAP_ATOMIC_OPEN_V1 |
6e01260c | 9984 | | NFS_CAP_LGOPEN |
f4ac1674 | 9985 | | NFS_CAP_ALLOCATE |
2e72448b | 9986 | | NFS_CAP_COPY |
cb95deea | 9987 | | NFS_CAP_OFFLOAD_CANCEL |
0491567b | 9988 | | NFS_CAP_COPY_NOTIFY |
624bd5b7 | 9989 | | NFS_CAP_DEALLOCATE |
6c5a0d89 | 9990 | | NFS_CAP_SEEK |
e5341f3a | 9991 | | NFS_CAP_LAYOUTSTATS |
3eb86093 TM |
9992 | | NFS_CAP_CLONE |
9993 | | NFS_CAP_LAYOUTERROR, | |
abf79bb3 CL |
9994 | .init_client = nfs41_init_client, |
9995 | .shutdown_client = nfs41_shutdown_client, | |
42c2c424 SD |
9996 | .match_stateid = nfs41_match_stateid, |
9997 | .find_root_sec = nfs41_find_root_sec, | |
7017310a | 9998 | .free_lock_state = nfs41_free_lock_state, |
9915ea7e | 9999 | .call_sync_ops = &nfs41_call_sync_ops, |
45870d69 | 10000 | .test_and_free_expired = nfs41_test_and_free_expired_stateid, |
63f5f796 | 10001 | .alloc_seqid = nfs_alloc_no_seqid, |
04fa2c6b | 10002 | .session_trunk = nfs4_test_session_trunk, |
42c2c424 SD |
10003 | .reboot_recovery_ops = &nfs41_reboot_recovery_ops, |
10004 | .nograce_recovery_ops = &nfs41_nograce_recovery_ops, | |
10005 | .state_renewal_ops = &nfs41_state_renewal_ops, | |
18e3b739 | 10006 | .mig_recovery_ops = &nfs41_mig_recovery_ops, |
42c2c424 SD |
10007 | }; |
10008 | #endif | |
10009 | ||
97dc1359 TM |
10010 | const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = { |
10011 | [0] = &nfs_v4_0_minor_ops, | |
10012 | #if defined(CONFIG_NFS_V4_1) | |
10013 | [1] = &nfs_v4_1_minor_ops, | |
10014 | #endif | |
42c2c424 SD |
10015 | #if defined(CONFIG_NFS_V4_2) |
10016 | [2] = &nfs_v4_2_minor_ops, | |
10017 | #endif | |
97dc1359 TM |
10018 | }; |
10019 | ||
13997823 | 10020 | static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size) |
c4803c49 AG |
10021 | { |
10022 | ssize_t error, error2; | |
10023 | ||
10024 | error = generic_listxattr(dentry, list, size); | |
10025 | if (error < 0) | |
10026 | return error; | |
10027 | if (list) { | |
10028 | list += error; | |
10029 | size -= error; | |
10030 | } | |
10031 | ||
10032 | error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size); | |
10033 | if (error2 < 0) | |
10034 | return error2; | |
10035 | return error + error2; | |
10036 | } | |
10037 | ||
17f26b12 | 10038 | static const struct inode_operations nfs4_dir_inode_operations = { |
73a79706 BS |
10039 | .create = nfs_create, |
10040 | .lookup = nfs_lookup, | |
10041 | .atomic_open = nfs_atomic_open, | |
10042 | .link = nfs_link, | |
10043 | .unlink = nfs_unlink, | |
10044 | .symlink = nfs_symlink, | |
10045 | .mkdir = nfs_mkdir, | |
10046 | .rmdir = nfs_rmdir, | |
10047 | .mknod = nfs_mknod, | |
10048 | .rename = nfs_rename, | |
10049 | .permission = nfs_permission, | |
10050 | .getattr = nfs_getattr, | |
10051 | .setattr = nfs_setattr, | |
c4803c49 | 10052 | .listxattr = nfs4_listxattr, |
73a79706 BS |
10053 | }; |
10054 | ||
92e1d5be | 10055 | static const struct inode_operations nfs4_file_inode_operations = { |
6b3b5496 BF |
10056 | .permission = nfs_permission, |
10057 | .getattr = nfs_getattr, | |
10058 | .setattr = nfs_setattr, | |
c4803c49 | 10059 | .listxattr = nfs4_listxattr, |
6b3b5496 BF |
10060 | }; |
10061 | ||
509de811 | 10062 | const struct nfs_rpc_ops nfs_v4_clientops = { |
1da177e4 LT |
10063 | .version = 4, /* protocol version */ |
10064 | .dentry_ops = &nfs4_dentry_operations, | |
10065 | .dir_inode_ops = &nfs4_dir_inode_operations, | |
6b3b5496 | 10066 | .file_inode_ops = &nfs4_file_inode_operations, |
1788ea6e | 10067 | .file_ops = &nfs4_file_operations, |
1da177e4 | 10068 | .getroot = nfs4_proc_get_root, |
281cad46 | 10069 | .submount = nfs4_submount, |
f2aedb71 | 10070 | .try_get_tree = nfs4_try_get_tree, |
1da177e4 LT |
10071 | .getattr = nfs4_proc_getattr, |
10072 | .setattr = nfs4_proc_setattr, | |
10073 | .lookup = nfs4_proc_lookup, | |
5b5faaf6 | 10074 | .lookupp = nfs4_proc_lookupp, |
1da177e4 LT |
10075 | .access = nfs4_proc_access, |
10076 | .readlink = nfs4_proc_readlink, | |
1da177e4 LT |
10077 | .create = nfs4_proc_create, |
10078 | .remove = nfs4_proc_remove, | |
10079 | .unlink_setup = nfs4_proc_unlink_setup, | |
34e137cc | 10080 | .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare, |
1da177e4 | 10081 | .unlink_done = nfs4_proc_unlink_done, |
d3d4152a | 10082 | .rename_setup = nfs4_proc_rename_setup, |
c6bfa1a1 | 10083 | .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare, |
d3d4152a | 10084 | .rename_done = nfs4_proc_rename_done, |
1da177e4 LT |
10085 | .link = nfs4_proc_link, |
10086 | .symlink = nfs4_proc_symlink, | |
10087 | .mkdir = nfs4_proc_mkdir, | |
912678db | 10088 | .rmdir = nfs4_proc_rmdir, |
1da177e4 LT |
10089 | .readdir = nfs4_proc_readdir, |
10090 | .mknod = nfs4_proc_mknod, | |
10091 | .statfs = nfs4_proc_statfs, | |
10092 | .fsinfo = nfs4_proc_fsinfo, | |
10093 | .pathconf = nfs4_proc_pathconf, | |
e9326dca | 10094 | .set_capabilities = nfs4_server_capabilities, |
1da177e4 | 10095 | .decode_dirent = nfs4_decode_dirent, |
a4cdda59 | 10096 | .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare, |
1da177e4 | 10097 | .read_setup = nfs4_proc_read_setup, |
ec06c096 | 10098 | .read_done = nfs4_read_done, |
1da177e4 | 10099 | .write_setup = nfs4_proc_write_setup, |
788e7a89 | 10100 | .write_done = nfs4_write_done, |
1da177e4 | 10101 | .commit_setup = nfs4_proc_commit_setup, |
0b7c0153 | 10102 | .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare, |
788e7a89 | 10103 | .commit_done = nfs4_commit_done, |
1da177e4 | 10104 | .lock = nfs4_proc_lock, |
e50a1c2e | 10105 | .clear_acl_cache = nfs4_zap_acl_attr, |
7fe5c398 | 10106 | .close_context = nfs4_close_context, |
2b484297 | 10107 | .open_context = nfs4_atomic_open, |
011e2a7f | 10108 | .have_delegation = nfs4_have_delegation, |
6663ee7f | 10109 | .alloc_client = nfs4_alloc_client, |
45a52a02 | 10110 | .init_client = nfs4_init_client, |
cdb7eced | 10111 | .free_client = nfs4_free_client, |
1179acc6 BS |
10112 | .create_server = nfs4_create_server, |
10113 | .clone_server = nfs_clone_server, | |
1da177e4 LT |
10114 | }; |
10115 | ||
64c2ce8b | 10116 | static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = { |
98e9cb57 | 10117 | .name = XATTR_NAME_NFSV4_ACL, |
64c2ce8b AK |
10118 | .list = nfs4_xattr_list_nfs4_acl, |
10119 | .get = nfs4_xattr_get_nfs4_acl, | |
10120 | .set = nfs4_xattr_set_nfs4_acl, | |
10121 | }; | |
10122 | ||
10123 | const struct xattr_handler *nfs4_xattr_handlers[] = { | |
10124 | &nfs4_xattr_nfs4_acl_handler, | |
c9bccef6 DQ |
10125 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
10126 | &nfs4_xattr_nfs4_label_handler, | |
10127 | #endif | |
64c2ce8b AK |
10128 | NULL |
10129 | }; | |
10130 | ||
1da177e4 LT |
10131 | /* |
10132 | * Local variables: | |
10133 | * c-basic-offset: 8 | |
10134 | * End: | |
10135 | */ |