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> |
6926afd1 | 54 | #include <linux/nfs_idmap.h> |
64c2ce8b | 55 | #include <linux/xattr.h> |
c7a360b0 | 56 | #include <linux/utsname.h> |
d310310c | 57 | #include <linux/freezer.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" |
73e39aaa | 66 | #include "nfs4session.h" |
de242c0b | 67 | #include "fscache.h" |
1da177e4 | 68 | |
c6d01c6f TM |
69 | #include "nfs4trace.h" |
70 | ||
1da177e4 LT |
71 | #define NFSDBG_FACILITY NFSDBG_PROC |
72 | ||
2066fe89 | 73 | #define NFS4_POLL_RETRY_MIN (HZ/10) |
1da177e4 LT |
74 | #define NFS4_POLL_RETRY_MAX (15*HZ) |
75 | ||
cdd4e68b | 76 | struct nfs4_opendata; |
864472e9 | 77 | static int _nfs4_proc_open(struct nfs4_opendata *data); |
b257957e | 78 | static int _nfs4_recover_proc_open(struct nfs4_opendata *data); |
1da177e4 | 79 | static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *); |
8478eaa1 | 80 | static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *, long *); |
81934ddb | 81 | static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr); |
1775fd3e DQ |
82 | static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label); |
83 | static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label); | |
0ab64e0e TM |
84 | static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred, |
85 | struct nfs_fattr *fattr, struct iattr *sattr, | |
1775fd3e DQ |
86 | struct nfs4_state *state, struct nfs4_label *ilabel, |
87 | struct nfs4_label *olabel); | |
f062eb6c | 88 | #ifdef CONFIG_NFS_V4_1 |
ab7cb0df TM |
89 | static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *, |
90 | struct rpc_cred *); | |
91 | static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *, | |
92 | struct rpc_cred *); | |
f062eb6c | 93 | #endif |
aa9c2669 DQ |
94 | |
95 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL | |
96 | static inline struct nfs4_label * | |
97 | nfs4_label_init_security(struct inode *dir, struct dentry *dentry, | |
98 | struct iattr *sattr, struct nfs4_label *label) | |
99 | { | |
100 | int err; | |
101 | ||
102 | if (label == NULL) | |
103 | return NULL; | |
104 | ||
105 | if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0) | |
106 | return NULL; | |
107 | ||
aa9c2669 DQ |
108 | err = security_dentry_init_security(dentry, sattr->ia_mode, |
109 | &dentry->d_name, (void **)&label->label, &label->len); | |
110 | if (err == 0) | |
111 | return label; | |
112 | ||
113 | return NULL; | |
114 | } | |
115 | static inline void | |
116 | nfs4_label_release_security(struct nfs4_label *label) | |
117 | { | |
118 | if (label) | |
119 | security_release_secctx(label->label, label->len); | |
120 | } | |
121 | static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label) | |
122 | { | |
123 | if (label) | |
124 | return server->attr_bitmask; | |
125 | ||
126 | return server->attr_bitmask_nl; | |
127 | } | |
128 | #else | |
129 | static inline struct nfs4_label * | |
130 | nfs4_label_init_security(struct inode *dir, struct dentry *dentry, | |
131 | struct iattr *sattr, struct nfs4_label *l) | |
132 | { return NULL; } | |
133 | static inline void | |
134 | nfs4_label_release_security(struct nfs4_label *label) | |
135 | { return; } | |
136 | static inline u32 * | |
137 | nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label) | |
138 | { return server->attr_bitmask; } | |
139 | #endif | |
140 | ||
1da177e4 | 141 | /* Prevent leaks of NFSv4 errors into userland */ |
46f72f57 | 142 | static int nfs4_map_errors(int err) |
1da177e4 | 143 | { |
52567b03 TM |
144 | if (err >= -1000) |
145 | return err; | |
146 | switch (err) { | |
147 | case -NFS4ERR_RESOURCE: | |
30005121 WAA |
148 | case -NFS4ERR_LAYOUTTRYLATER: |
149 | case -NFS4ERR_RECALLCONFLICT: | |
52567b03 | 150 | return -EREMOTEIO; |
7ebb9315 | 151 | case -NFS4ERR_WRONGSEC: |
8897538e | 152 | case -NFS4ERR_WRONG_CRED: |
7ebb9315 | 153 | return -EPERM; |
3ddeb7c5 TM |
154 | case -NFS4ERR_BADOWNER: |
155 | case -NFS4ERR_BADNAME: | |
156 | return -EINVAL; | |
fb13bfa7 TM |
157 | case -NFS4ERR_SHARE_DENIED: |
158 | return -EACCES; | |
f25efd85 SD |
159 | case -NFS4ERR_MINOR_VERS_MISMATCH: |
160 | return -EPROTONOSUPPORT; | |
6e3cf241 TM |
161 | case -NFS4ERR_FILE_OPEN: |
162 | return -EBUSY; | |
52567b03 | 163 | default: |
1da177e4 | 164 | dprintk("%s could not handle NFSv4 error %d\n", |
3110ff80 | 165 | __func__, -err); |
52567b03 | 166 | break; |
1da177e4 | 167 | } |
52567b03 | 168 | return -EIO; |
1da177e4 LT |
169 | } |
170 | ||
171 | /* | |
172 | * This is our standard bitmap for GETATTR requests. | |
173 | */ | |
1549210f | 174 | const u32 nfs4_fattr_bitmap[3] = { |
1da177e4 LT |
175 | FATTR4_WORD0_TYPE |
176 | | FATTR4_WORD0_CHANGE | |
177 | | FATTR4_WORD0_SIZE | |
178 | | FATTR4_WORD0_FSID | |
179 | | FATTR4_WORD0_FILEID, | |
180 | FATTR4_WORD1_MODE | |
181 | | FATTR4_WORD1_NUMLINKS | |
182 | | FATTR4_WORD1_OWNER | |
183 | | FATTR4_WORD1_OWNER_GROUP | |
184 | | FATTR4_WORD1_RAWDEV | |
185 | | FATTR4_WORD1_SPACE_USED | |
186 | | FATTR4_WORD1_TIME_ACCESS | |
187 | | FATTR4_WORD1_TIME_METADATA | |
aa9c2669 DQ |
188 | | FATTR4_WORD1_TIME_MODIFY, |
189 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL | |
190 | FATTR4_WORD2_SECURITY_LABEL | |
191 | #endif | |
1da177e4 LT |
192 | }; |
193 | ||
1549210f TM |
194 | static const u32 nfs4_pnfs_open_bitmap[3] = { |
195 | FATTR4_WORD0_TYPE | |
196 | | FATTR4_WORD0_CHANGE | |
197 | | FATTR4_WORD0_SIZE | |
198 | | FATTR4_WORD0_FSID | |
199 | | FATTR4_WORD0_FILEID, | |
200 | FATTR4_WORD1_MODE | |
201 | | FATTR4_WORD1_NUMLINKS | |
202 | | FATTR4_WORD1_OWNER | |
203 | | FATTR4_WORD1_OWNER_GROUP | |
204 | | FATTR4_WORD1_RAWDEV | |
205 | | FATTR4_WORD1_SPACE_USED | |
206 | | FATTR4_WORD1_TIME_ACCESS | |
207 | | FATTR4_WORD1_TIME_METADATA | |
208 | | FATTR4_WORD1_TIME_MODIFY, | |
209 | FATTR4_WORD2_MDSTHRESHOLD | |
210 | }; | |
211 | ||
e23008ec AA |
212 | static const u32 nfs4_open_noattr_bitmap[3] = { |
213 | FATTR4_WORD0_TYPE | |
214 | | FATTR4_WORD0_CHANGE | |
215 | | FATTR4_WORD0_FILEID, | |
216 | }; | |
217 | ||
a09df2ca | 218 | const u32 nfs4_statfs_bitmap[3] = { |
1da177e4 LT |
219 | FATTR4_WORD0_FILES_AVAIL |
220 | | FATTR4_WORD0_FILES_FREE | |
221 | | FATTR4_WORD0_FILES_TOTAL, | |
222 | FATTR4_WORD1_SPACE_AVAIL | |
223 | | FATTR4_WORD1_SPACE_FREE | |
224 | | FATTR4_WORD1_SPACE_TOTAL | |
225 | }; | |
226 | ||
a09df2ca | 227 | const u32 nfs4_pathconf_bitmap[3] = { |
1da177e4 LT |
228 | FATTR4_WORD0_MAXLINK |
229 | | FATTR4_WORD0_MAXNAME, | |
230 | 0 | |
231 | }; | |
232 | ||
dae100c2 | 233 | const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE |
1da177e4 LT |
234 | | FATTR4_WORD0_MAXREAD |
235 | | FATTR4_WORD0_MAXWRITE | |
236 | | FATTR4_WORD0_LEASE_TIME, | |
55b6e774 | 237 | FATTR4_WORD1_TIME_DELTA |
dae100c2 FI |
238 | | FATTR4_WORD1_FS_LAYOUT_TYPES, |
239 | FATTR4_WORD2_LAYOUT_BLKSIZE | |
1da177e4 LT |
240 | }; |
241 | ||
a09df2ca | 242 | const u32 nfs4_fs_locations_bitmap[3] = { |
830b8e33 MN |
243 | FATTR4_WORD0_TYPE |
244 | | FATTR4_WORD0_CHANGE | |
245 | | FATTR4_WORD0_SIZE | |
246 | | FATTR4_WORD0_FSID | |
247 | | FATTR4_WORD0_FILEID | |
248 | | FATTR4_WORD0_FS_LOCATIONS, | |
249 | FATTR4_WORD1_MODE | |
250 | | FATTR4_WORD1_NUMLINKS | |
251 | | FATTR4_WORD1_OWNER | |
252 | | FATTR4_WORD1_OWNER_GROUP | |
253 | | FATTR4_WORD1_RAWDEV | |
254 | | FATTR4_WORD1_SPACE_USED | |
255 | | FATTR4_WORD1_TIME_ACCESS | |
256 | | FATTR4_WORD1_TIME_METADATA | |
257 | | FATTR4_WORD1_TIME_MODIFY | |
a09df2ca | 258 | | FATTR4_WORD1_MOUNTED_ON_FILEID, |
830b8e33 MN |
259 | }; |
260 | ||
bc4785cd | 261 | static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry, |
1da177e4 LT |
262 | struct nfs4_readdir_arg *readdir) |
263 | { | |
0dbb4c67 | 264 | __be32 *start, *p; |
1da177e4 | 265 | |
1da177e4 | 266 | if (cookie > 2) { |
b7ef1956 | 267 | readdir->cookie = cookie; |
1da177e4 LT |
268 | memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier)); |
269 | return; | |
270 | } | |
271 | ||
272 | readdir->cookie = 0; | |
273 | memset(&readdir->verifier, 0, sizeof(readdir->verifier)); | |
274 | if (cookie == 2) | |
275 | return; | |
276 | ||
277 | /* | |
278 | * NFSv4 servers do not return entries for '.' and '..' | |
279 | * Therefore, we fake these entries here. We let '.' | |
280 | * have cookie 0 and '..' have cookie 1. Note that | |
281 | * when talking to the server, we always send cookie 0 | |
282 | * instead of 1 or 2. | |
283 | */ | |
2b86ce2d | 284 | start = p = kmap_atomic(*readdir->pages); |
1da177e4 LT |
285 | |
286 | if (cookie == 0) { | |
287 | *p++ = xdr_one; /* next */ | |
288 | *p++ = xdr_zero; /* cookie, first word */ | |
289 | *p++ = xdr_one; /* cookie, second word */ | |
290 | *p++ = xdr_one; /* entry len */ | |
291 | memcpy(p, ".\0\0\0", 4); /* entry */ | |
292 | p++; | |
293 | *p++ = xdr_one; /* bitmap length */ | |
294 | *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */ | |
295 | *p++ = htonl(8); /* attribute buffer length */ | |
4e769b93 | 296 | p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode)); |
1da177e4 LT |
297 | } |
298 | ||
299 | *p++ = xdr_one; /* next */ | |
300 | *p++ = xdr_zero; /* cookie, first word */ | |
301 | *p++ = xdr_two; /* cookie, second word */ | |
302 | *p++ = xdr_two; /* entry len */ | |
303 | memcpy(p, "..\0\0", 4); /* entry */ | |
304 | p++; | |
305 | *p++ = xdr_one; /* bitmap length */ | |
306 | *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */ | |
307 | *p++ = htonl(8); /* attribute buffer length */ | |
4e769b93 | 308 | p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode)); |
1da177e4 LT |
309 | |
310 | readdir->pgbase = (char *)p - (char *)start; | |
311 | readdir->count -= readdir->pgbase; | |
2b86ce2d | 312 | kunmap_atomic(start); |
1da177e4 LT |
313 | } |
314 | ||
8478eaa1 N |
315 | static long nfs4_update_delay(long *timeout) |
316 | { | |
317 | long ret; | |
318 | if (!timeout) | |
319 | return NFS4_POLL_RETRY_MAX; | |
320 | if (*timeout <= 0) | |
321 | *timeout = NFS4_POLL_RETRY_MIN; | |
322 | if (*timeout > NFS4_POLL_RETRY_MAX) | |
323 | *timeout = NFS4_POLL_RETRY_MAX; | |
324 | ret = *timeout; | |
325 | *timeout <<= 1; | |
326 | return ret; | |
327 | } | |
328 | ||
65de872e TM |
329 | static int nfs4_delay(struct rpc_clnt *clnt, long *timeout) |
330 | { | |
331 | int res = 0; | |
332 | ||
333 | might_sleep(); | |
334 | ||
8478eaa1 N |
335 | freezable_schedule_timeout_killable_unsafe( |
336 | nfs4_update_delay(timeout)); | |
65de872e TM |
337 | if (fatal_signal_pending(current)) |
338 | res = -ERESTARTSYS; | |
65de872e TM |
339 | return res; |
340 | } | |
341 | ||
342 | /* This is the error handling routine for processes that are allowed | |
343 | * to sleep. | |
344 | */ | |
f4ac1674 | 345 | int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception) |
65de872e TM |
346 | { |
347 | struct nfs_client *clp = server->nfs_client; | |
9e33bed5 | 348 | struct nfs4_state *state = exception->state; |
3114ea7a | 349 | struct inode *inode = exception->inode; |
65de872e TM |
350 | int ret = errorcode; |
351 | ||
352 | exception->retry = 0; | |
353 | switch(errorcode) { | |
354 | case 0: | |
355 | return 0; | |
3114ea7a | 356 | case -NFS4ERR_OPENMODE: |
011e2a7f | 357 | if (inode && nfs4_have_delegation(inode, FMODE_READ)) { |
57ec14c5 | 358 | nfs4_inode_return_delegation(inode); |
3114ea7a TM |
359 | exception->retry = 1; |
360 | return 0; | |
361 | } | |
362 | if (state == NULL) | |
363 | break; | |
5d422301 TM |
364 | ret = nfs4_schedule_stateid_recovery(server, state); |
365 | if (ret < 0) | |
366 | break; | |
3114ea7a | 367 | goto wait_on_recovery; |
a1d0b5ee | 368 | case -NFS4ERR_DELEG_REVOKED: |
9e33bed5 TM |
369 | case -NFS4ERR_ADMIN_REVOKED: |
370 | case -NFS4ERR_BAD_STATEID: | |
9e33bed5 TM |
371 | if (state == NULL) |
372 | break; | |
5d422301 TM |
373 | ret = nfs4_schedule_stateid_recovery(server, state); |
374 | if (ret < 0) | |
375 | break; | |
0400a6b0 | 376 | goto wait_on_recovery; |
0ced63d1 | 377 | case -NFS4ERR_EXPIRED: |
5d422301 TM |
378 | if (state != NULL) { |
379 | ret = nfs4_schedule_stateid_recovery(server, state); | |
380 | if (ret < 0) | |
381 | break; | |
382 | } | |
65de872e | 383 | case -NFS4ERR_STALE_STATEID: |
a2c0b9e2 | 384 | case -NFS4ERR_STALE_CLIENTID: |
0400a6b0 TM |
385 | nfs4_schedule_lease_recovery(clp); |
386 | goto wait_on_recovery; | |
519ae255 CL |
387 | case -NFS4ERR_MOVED: |
388 | ret = nfs4_schedule_migration_recovery(server); | |
389 | if (ret < 0) | |
390 | break; | |
391 | goto wait_on_recovery; | |
8ef2f8d4 CL |
392 | case -NFS4ERR_LEASE_MOVED: |
393 | nfs4_schedule_lease_moved_recovery(clp); | |
394 | goto wait_on_recovery; | |
03391693 | 395 | #if defined(CONFIG_NFS_V4_1) |
4745e315 AA |
396 | case -NFS4ERR_BADSESSION: |
397 | case -NFS4ERR_BADSLOT: | |
398 | case -NFS4ERR_BAD_HIGH_SLOT: | |
399 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
400 | case -NFS4ERR_DEADSESSION: | |
401 | case -NFS4ERR_SEQ_FALSE_RETRY: | |
402 | case -NFS4ERR_SEQ_MISORDERED: | |
403 | dprintk("%s ERROR: %d Reset session\n", __func__, | |
404 | errorcode); | |
9f594791 | 405 | nfs4_schedule_session_recovery(clp->cl_session, errorcode); |
399f11c3 | 406 | goto wait_on_recovery; |
03391693 | 407 | #endif /* defined(CONFIG_NFS_V4_1) */ |
65de872e | 408 | case -NFS4ERR_FILE_OPEN: |
44ed3556 N |
409 | if (exception->timeout > HZ) { |
410 | /* We have retried a decent amount, time to | |
411 | * fail | |
412 | */ | |
413 | ret = -EBUSY; | |
414 | break; | |
415 | } | |
65de872e TM |
416 | case -NFS4ERR_GRACE: |
417 | case -NFS4ERR_DELAY: | |
418 | ret = nfs4_delay(server->client, &exception->timeout); | |
419 | if (ret != 0) | |
420 | break; | |
a8a4ae3a | 421 | case -NFS4ERR_RETRY_UNCACHED_REP: |
65de872e TM |
422 | case -NFS4ERR_OLD_STATEID: |
423 | exception->retry = 1; | |
b064eca2 TM |
424 | break; |
425 | case -NFS4ERR_BADOWNER: | |
426 | /* The following works around a Linux server bug! */ | |
427 | case -NFS4ERR_BADNAME: | |
428 | if (server->caps & NFS_CAP_UIDGID_NOMAP) { | |
429 | server->caps &= ~NFS_CAP_UIDGID_NOMAP; | |
430 | exception->retry = 1; | |
431 | printk(KERN_WARNING "NFS: v4 server %s " | |
432 | "does not accept raw " | |
433 | "uid/gids. " | |
434 | "Reenabling the idmapper.\n", | |
435 | server->nfs_client->cl_hostname); | |
436 | } | |
65de872e TM |
437 | } |
438 | /* We failed to handle the error */ | |
439 | return nfs4_map_errors(ret); | |
0400a6b0 | 440 | wait_on_recovery: |
a2c0b9e2 | 441 | ret = nfs4_wait_clnt_recover(clp); |
519ae255 CL |
442 | if (test_bit(NFS_MIG_FAILED, &server->mig_status)) |
443 | return -EIO; | |
a2c0b9e2 TM |
444 | if (ret == 0) |
445 | exception->retry = 1; | |
446 | return ret; | |
65de872e TM |
447 | } |
448 | ||
a5250def WAA |
449 | /* |
450 | * Return 'true' if 'clp' is using an rpc_client that is integrity protected | |
451 | * or 'false' otherwise. | |
452 | */ | |
453 | static bool _nfs4_is_integrity_protected(struct nfs_client *clp) | |
454 | { | |
455 | rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor; | |
456 | ||
457 | if (flavor == RPC_AUTH_GSS_KRB5I || | |
458 | flavor == RPC_AUTH_GSS_KRB5P) | |
459 | return true; | |
460 | ||
461 | return false; | |
462 | } | |
65de872e | 463 | |
452e9352 | 464 | static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp) |
1da177e4 | 465 | { |
1da177e4 LT |
466 | spin_lock(&clp->cl_lock); |
467 | if (time_before(clp->cl_last_renewal,timestamp)) | |
468 | clp->cl_last_renewal = timestamp; | |
469 | spin_unlock(&clp->cl_lock); | |
470 | } | |
471 | ||
452e9352 TM |
472 | static void renew_lease(const struct nfs_server *server, unsigned long timestamp) |
473 | { | |
474 | do_renew_lease(server->nfs_client, timestamp); | |
475 | } | |
476 | ||
2a3eb2b9 CL |
477 | struct nfs4_call_sync_data { |
478 | const struct nfs_server *seq_server; | |
479 | struct nfs4_sequence_args *seq_args; | |
480 | struct nfs4_sequence_res *seq_res; | |
481 | }; | |
482 | ||
a9c92d6b CL |
483 | static void nfs4_init_sequence(struct nfs4_sequence_args *args, |
484 | struct nfs4_sequence_res *res, int cache_reply) | |
485 | { | |
486 | args->sa_slot = NULL; | |
487 | args->sa_cache_this = cache_reply; | |
488 | args->sa_privileged = 0; | |
489 | ||
490 | res->sr_slot = NULL; | |
491 | } | |
492 | ||
493 | static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args) | |
494 | { | |
495 | args->sa_privileged = 1; | |
496 | } | |
497 | ||
cb04ad2a PT |
498 | int nfs40_setup_sequence(struct nfs4_slot_table *tbl, |
499 | struct nfs4_sequence_args *args, | |
500 | struct nfs4_sequence_res *res, | |
501 | struct rpc_task *task) | |
3bd2384a | 502 | { |
3bd2384a CL |
503 | struct nfs4_slot *slot; |
504 | ||
505 | /* slot already allocated? */ | |
506 | if (res->sr_slot != NULL) | |
507 | goto out_start; | |
508 | ||
509 | spin_lock(&tbl->slot_tbl_lock); | |
510 | if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged) | |
511 | goto out_sleep; | |
512 | ||
513 | slot = nfs4_alloc_slot(tbl); | |
514 | if (IS_ERR(slot)) { | |
515 | if (slot == ERR_PTR(-ENOMEM)) | |
516 | task->tk_timeout = HZ >> 2; | |
517 | goto out_sleep; | |
518 | } | |
519 | spin_unlock(&tbl->slot_tbl_lock); | |
520 | ||
521 | args->sa_slot = slot; | |
522 | res->sr_slot = slot; | |
523 | ||
524 | out_start: | |
525 | rpc_call_start(task); | |
526 | return 0; | |
527 | ||
528 | out_sleep: | |
529 | if (args->sa_privileged) | |
530 | rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task, | |
531 | NULL, RPC_PRIORITY_PRIVILEGED); | |
532 | else | |
533 | rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL); | |
534 | spin_unlock(&tbl->slot_tbl_lock); | |
535 | return -EAGAIN; | |
536 | } | |
cb04ad2a | 537 | EXPORT_SYMBOL_GPL(nfs40_setup_sequence); |
3bd2384a CL |
538 | |
539 | static int nfs40_sequence_done(struct rpc_task *task, | |
540 | struct nfs4_sequence_res *res) | |
541 | { | |
542 | struct nfs4_slot *slot = res->sr_slot; | |
543 | struct nfs4_slot_table *tbl; | |
544 | ||
cab92c19 | 545 | if (slot == NULL) |
3bd2384a CL |
546 | goto out; |
547 | ||
548 | tbl = slot->table; | |
549 | spin_lock(&tbl->slot_tbl_lock); | |
550 | if (!nfs41_wake_and_assign_slot(tbl, slot)) | |
551 | nfs4_free_slot(tbl, slot); | |
552 | spin_unlock(&tbl->slot_tbl_lock); | |
553 | ||
554 | res->sr_slot = NULL; | |
555 | out: | |
556 | return 1; | |
557 | } | |
558 | ||
cccef3b9 AA |
559 | #if defined(CONFIG_NFS_V4_1) |
560 | ||
d185a334 | 561 | static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res) |
13615871 | 562 | { |
e3725ec0 | 563 | struct nfs4_session *session; |
13615871 | 564 | struct nfs4_slot_table *tbl; |
a13ce7c6 | 565 | struct nfs4_slot *slot = res->sr_slot; |
c10e4498 | 566 | bool send_new_highest_used_slotid = false; |
13615871 | 567 | |
a13ce7c6 | 568 | tbl = slot->table; |
e3725ec0 | 569 | session = tbl->session; |
ea028ac9 | 570 | |
35dc1d74 | 571 | spin_lock(&tbl->slot_tbl_lock); |
c10e4498 TM |
572 | /* Be nice to the server: try to ensure that the last transmitted |
573 | * value for highest_user_slotid <= target_highest_slotid | |
574 | */ | |
575 | if (tbl->highest_used_slotid > tbl->target_highest_slotid) | |
576 | send_new_highest_used_slotid = true; | |
577 | ||
a13ce7c6 | 578 | if (nfs41_wake_and_assign_slot(tbl, slot)) { |
b75ad4cd TM |
579 | send_new_highest_used_slotid = false; |
580 | goto out_unlock; | |
581 | } | |
a13ce7c6 | 582 | nfs4_free_slot(tbl, slot); |
c10e4498 TM |
583 | |
584 | if (tbl->highest_used_slotid != NFS4_NO_SLOT) | |
585 | send_new_highest_used_slotid = false; | |
b75ad4cd | 586 | out_unlock: |
35dc1d74 | 587 | spin_unlock(&tbl->slot_tbl_lock); |
dfb4f309 | 588 | res->sr_slot = NULL; |
c10e4498 TM |
589 | if (send_new_highest_used_slotid) |
590 | nfs41_server_notify_highest_slotid_update(session->clp); | |
13615871 AA |
591 | } |
592 | ||
f9c96fcc | 593 | int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res) |
b0df806c | 594 | { |
e3725ec0 | 595 | struct nfs4_session *session; |
a13ce7c6 | 596 | struct nfs4_slot *slot = res->sr_slot; |
14516c3a | 597 | struct nfs_client *clp; |
ac20d163 | 598 | bool interrupted = false; |
85563073 | 599 | int ret = 1; |
b0df806c | 600 | |
a13ce7c6 TM |
601 | if (slot == NULL) |
602 | goto out_noaction; | |
468f8613 BS |
603 | /* don't increment the sequence number if the task wasn't sent */ |
604 | if (!RPC_WAS_SENT(task)) | |
b0df806c AA |
605 | goto out; |
606 | ||
e3725ec0 | 607 | session = slot->table->session; |
933602e3 | 608 | |
ac20d163 TM |
609 | if (slot->interrupted) { |
610 | slot->interrupted = 0; | |
611 | interrupted = true; | |
612 | } | |
613 | ||
2f92ae34 | 614 | trace_nfs4_sequence_done(session, res); |
691daf3b | 615 | /* Check the SEQUENCE operation status */ |
14516c3a TM |
616 | switch (res->sr_status) { |
617 | case 0: | |
b0df806c | 618 | /* Update the slot's sequence and clientid lease timer */ |
933602e3 | 619 | ++slot->seq_nr; |
e3725ec0 | 620 | clp = session->clp; |
8e63b6a8 | 621 | do_renew_lease(clp, res->sr_timestamp); |
0629e370 | 622 | /* Check sequence flags */ |
b4410c2f TM |
623 | if (res->sr_status_flags != 0) |
624 | nfs4_schedule_lease_recovery(clp); | |
464ee9f9 | 625 | nfs41_update_target_slotid(slot->table, slot, res); |
14516c3a | 626 | break; |
ac20d163 TM |
627 | case 1: |
628 | /* | |
629 | * sr_status remains 1 if an RPC level error occurred. | |
630 | * The server may or may not have processed the sequence | |
631 | * operation.. | |
632 | * Mark the slot as having hosted an interrupted RPC call. | |
633 | */ | |
634 | slot->interrupted = 1; | |
635 | goto out; | |
14516c3a TM |
636 | case -NFS4ERR_DELAY: |
637 | /* The server detected a resend of the RPC call and | |
638 | * returned NFS4ERR_DELAY as per Section 2.10.6.2 | |
639 | * of RFC5661. | |
640 | */ | |
df2fabff | 641 | dprintk("%s: slot=%u seq=%u: Operation in progress\n", |
dfb4f309 | 642 | __func__, |
df2fabff | 643 | slot->slot_nr, |
933602e3 | 644 | slot->seq_nr); |
14516c3a | 645 | goto out_retry; |
85563073 TM |
646 | case -NFS4ERR_BADSLOT: |
647 | /* | |
648 | * The slot id we used was probably retired. Try again | |
649 | * using a different slot id. | |
650 | */ | |
e8794440 TM |
651 | goto retry_nowait; |
652 | case -NFS4ERR_SEQ_MISORDERED: | |
ac20d163 TM |
653 | /* |
654 | * Was the last operation on this sequence interrupted? | |
655 | * If so, retry after bumping the sequence number. | |
656 | */ | |
657 | if (interrupted) { | |
658 | ++slot->seq_nr; | |
659 | goto retry_nowait; | |
660 | } | |
e8794440 TM |
661 | /* |
662 | * Could this slot have been previously retired? | |
663 | * If so, then the server may be expecting seq_nr = 1! | |
664 | */ | |
8e63b6a8 TM |
665 | if (slot->seq_nr != 1) { |
666 | slot->seq_nr = 1; | |
667 | goto retry_nowait; | |
668 | } | |
669 | break; | |
e8794440 TM |
670 | case -NFS4ERR_SEQ_FALSE_RETRY: |
671 | ++slot->seq_nr; | |
672 | goto retry_nowait; | |
14516c3a TM |
673 | default: |
674 | /* Just update the slot sequence no. */ | |
933602e3 | 675 | ++slot->seq_nr; |
b0df806c AA |
676 | } |
677 | out: | |
678 | /* The session may be reset by one of the error handlers. */ | |
679 | dprintk("%s: Error %d free the slot \n", __func__, res->sr_status); | |
d185a334 | 680 | nfs41_sequence_free_slot(res); |
a13ce7c6 | 681 | out_noaction: |
85563073 | 682 | return ret; |
e8794440 TM |
683 | retry_nowait: |
684 | if (rpc_restart_call_prepare(task)) { | |
685 | task->tk_status = 0; | |
686 | ret = 0; | |
687 | } | |
688 | goto out; | |
14516c3a | 689 | out_retry: |
d05dd4e9 | 690 | if (!rpc_restart_call(task)) |
14516c3a TM |
691 | goto out; |
692 | rpc_delay(task, NFS4_POLL_RETRY_MAX); | |
693 | return 0; | |
b0df806c | 694 | } |
f9c96fcc | 695 | EXPORT_SYMBOL_GPL(nfs41_sequence_done); |
b0df806c | 696 | |
2c4b131d | 697 | int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res) |
df896455 | 698 | { |
e3725ec0 | 699 | if (res->sr_slot == NULL) |
14516c3a | 700 | return 1; |
3bd2384a CL |
701 | if (!res->sr_slot->table->session) |
702 | return nfs40_sequence_done(task, res); | |
14516c3a | 703 | return nfs41_sequence_done(task, res); |
df896455 | 704 | } |
2c4b131d | 705 | EXPORT_SYMBOL_GPL(nfs4_sequence_done); |
df896455 | 706 | |
dc70d7b3 | 707 | int nfs41_setup_sequence(struct nfs4_session *session, |
ce5039c1 AA |
708 | struct nfs4_sequence_args *args, |
709 | struct nfs4_sequence_res *res, | |
ce5039c1 AA |
710 | struct rpc_task *task) |
711 | { | |
fbcd4abc AA |
712 | struct nfs4_slot *slot; |
713 | struct nfs4_slot_table *tbl; | |
fbcd4abc AA |
714 | |
715 | dprintk("--> %s\n", __func__); | |
ce5039c1 | 716 | /* slot already allocated? */ |
dfb4f309 | 717 | if (res->sr_slot != NULL) |
d9afbd1b | 718 | goto out_success; |
ce5039c1 | 719 | |
fbcd4abc AA |
720 | tbl = &session->fc_slot_table; |
721 | ||
69d206b5 TM |
722 | task->tk_timeout = 0; |
723 | ||
fbcd4abc | 724 | spin_lock(&tbl->slot_tbl_lock); |
774d5f14 | 725 | if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) && |
8fe72bac | 726 | !args->sa_privileged) { |
0b1c8fc4 | 727 | /* The state manager will wait until the slot table is empty */ |
0b1c8fc4 | 728 | dprintk("%s session is draining\n", __func__); |
7b939a3f | 729 | goto out_sleep; |
689cf5c1 AB |
730 | } |
731 | ||
2dc03b7f | 732 | slot = nfs4_alloc_slot(tbl); |
69d206b5 TM |
733 | if (IS_ERR(slot)) { |
734 | /* If out of memory, try again in 1/4 second */ | |
735 | if (slot == ERR_PTR(-ENOMEM)) | |
736 | task->tk_timeout = HZ >> 2; | |
fbcd4abc | 737 | dprintk("<-- %s: no free slots\n", __func__); |
7b939a3f | 738 | goto out_sleep; |
fbcd4abc AA |
739 | } |
740 | spin_unlock(&tbl->slot_tbl_lock); | |
741 | ||
2b2fa717 | 742 | args->sa_slot = slot; |
fbcd4abc | 743 | |
e8d92382 | 744 | dprintk("<-- %s slotid=%u seqid=%u\n", __func__, |
2dc03b7f | 745 | slot->slot_nr, slot->seq_nr); |
fbcd4abc | 746 | |
dfb4f309 | 747 | res->sr_slot = slot; |
8e63b6a8 | 748 | res->sr_timestamp = jiffies; |
2a6e26cd | 749 | res->sr_status_flags = 0; |
fbcd4abc AA |
750 | /* |
751 | * sr_status is only set in decode_sequence, and so will remain | |
752 | * set to 1 if an rpc level failure occurs. | |
753 | */ | |
754 | res->sr_status = 1; | |
2f92ae34 | 755 | trace_nfs4_setup_sequence(session, args); |
d9afbd1b TM |
756 | out_success: |
757 | rpc_call_start(task); | |
ce5039c1 | 758 | return 0; |
7b939a3f | 759 | out_sleep: |
8fe72bac TM |
760 | /* Privileged tasks are queued with top priority */ |
761 | if (args->sa_privileged) | |
1e1093c7 TM |
762 | rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task, |
763 | NULL, RPC_PRIORITY_PRIVILEGED); | |
764 | else | |
765 | rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL); | |
7b939a3f TM |
766 | spin_unlock(&tbl->slot_tbl_lock); |
767 | return -EAGAIN; | |
ce5039c1 | 768 | } |
dc70d7b3 | 769 | EXPORT_SYMBOL_GPL(nfs41_setup_sequence); |
ce5039c1 | 770 | |
5a580e0a CL |
771 | static int nfs4_setup_sequence(const struct nfs_server *server, |
772 | struct nfs4_sequence_args *args, | |
773 | struct nfs4_sequence_res *res, | |
774 | struct rpc_task *task) | |
ce5039c1 | 775 | { |
035168ab | 776 | struct nfs4_session *session = nfs4_get_session(server); |
ce5039c1 AA |
777 | int ret = 0; |
778 | ||
3bd2384a | 779 | if (!session) |
cb04ad2a PT |
780 | return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl, |
781 | args, res, task); | |
035168ab | 782 | |
e8d92382 | 783 | dprintk("--> %s clp %p session %p sr_slot %u\n", |
dfb4f309 | 784 | __func__, session->clp, session, res->sr_slot ? |
e8d92382 | 785 | res->sr_slot->slot_nr : NFS4_NO_SLOT); |
ce5039c1 | 786 | |
9d12b216 | 787 | ret = nfs41_setup_sequence(session, args, res, task); |
3bd2384a | 788 | |
ce5039c1 AA |
789 | dprintk("<-- %s status=%d\n", __func__, ret); |
790 | return ret; | |
791 | } | |
792 | ||
ce5039c1 AA |
793 | static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata) |
794 | { | |
2a3eb2b9 | 795 | struct nfs4_call_sync_data *data = calldata; |
6ba7db34 | 796 | struct nfs4_session *session = nfs4_get_session(data->seq_server); |
ce5039c1 | 797 | |
035168ab TM |
798 | dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server); |
799 | ||
d9afbd1b | 800 | nfs41_setup_sequence(session, data->seq_args, data->seq_res, task); |
ce5039c1 AA |
801 | } |
802 | ||
69ab40c4 AA |
803 | static void nfs41_call_sync_done(struct rpc_task *task, void *calldata) |
804 | { | |
2a3eb2b9 | 805 | struct nfs4_call_sync_data *data = calldata; |
69ab40c4 | 806 | |
14516c3a | 807 | nfs41_sequence_done(task, data->seq_res); |
69ab40c4 AA |
808 | } |
809 | ||
17280175 | 810 | static const struct rpc_call_ops nfs41_call_sync_ops = { |
ce5039c1 | 811 | .rpc_call_prepare = nfs41_call_sync_prepare, |
69ab40c4 | 812 | .rpc_call_done = nfs41_call_sync_done, |
ce5039c1 AA |
813 | }; |
814 | ||
3bd2384a CL |
815 | #else /* !CONFIG_NFS_V4_1 */ |
816 | ||
9915ea7e CL |
817 | static int nfs4_setup_sequence(const struct nfs_server *server, |
818 | struct nfs4_sequence_args *args, | |
819 | struct nfs4_sequence_res *res, | |
820 | struct rpc_task *task) | |
821 | { | |
cb04ad2a PT |
822 | return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl, |
823 | args, res, task); | |
9915ea7e CL |
824 | } |
825 | ||
2c4b131d PT |
826 | int nfs4_sequence_done(struct rpc_task *task, |
827 | struct nfs4_sequence_res *res) | |
9915ea7e | 828 | { |
3bd2384a | 829 | return nfs40_sequence_done(task, res); |
9915ea7e | 830 | } |
2c4b131d | 831 | EXPORT_SYMBOL_GPL(nfs4_sequence_done); |
3bd2384a CL |
832 | |
833 | #endif /* !CONFIG_NFS_V4_1 */ | |
9915ea7e CL |
834 | |
835 | static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata) | |
836 | { | |
837 | struct nfs4_call_sync_data *data = calldata; | |
838 | nfs4_setup_sequence(data->seq_server, | |
839 | data->seq_args, data->seq_res, task); | |
840 | } | |
841 | ||
842 | static void nfs40_call_sync_done(struct rpc_task *task, void *calldata) | |
843 | { | |
844 | struct nfs4_call_sync_data *data = calldata; | |
845 | nfs4_sequence_done(task, data->seq_res); | |
846 | } | |
847 | ||
848 | static const struct rpc_call_ops nfs40_call_sync_ops = { | |
849 | .rpc_call_prepare = nfs40_call_sync_prepare, | |
850 | .rpc_call_done = nfs40_call_sync_done, | |
851 | }; | |
852 | ||
7c513058 BS |
853 | static int nfs4_call_sync_sequence(struct rpc_clnt *clnt, |
854 | struct nfs_server *server, | |
ce5039c1 AA |
855 | struct rpc_message *msg, |
856 | struct nfs4_sequence_args *args, | |
8fe72bac | 857 | struct nfs4_sequence_res *res) |
ce5039c1 AA |
858 | { |
859 | int ret; | |
860 | struct rpc_task *task; | |
9915ea7e | 861 | struct nfs_client *clp = server->nfs_client; |
2a3eb2b9 | 862 | struct nfs4_call_sync_data data = { |
035168ab | 863 | .seq_server = server, |
ce5039c1 AA |
864 | .seq_args = args, |
865 | .seq_res = res, | |
ce5039c1 AA |
866 | }; |
867 | struct rpc_task_setup task_setup = { | |
7c513058 | 868 | .rpc_client = clnt, |
ce5039c1 | 869 | .rpc_message = msg, |
9915ea7e | 870 | .callback_ops = clp->cl_mvops->call_sync_ops, |
ce5039c1 AA |
871 | .callback_data = &data |
872 | }; | |
873 | ||
ce5039c1 AA |
874 | task = rpc_run_task(&task_setup); |
875 | if (IS_ERR(task)) | |
876 | ret = PTR_ERR(task); | |
877 | else { | |
878 | ret = task->tk_status; | |
879 | rpc_put_task(task); | |
880 | } | |
881 | return ret; | |
882 | } | |
883 | ||
7c513058 BS |
884 | int nfs4_call_sync(struct rpc_clnt *clnt, |
885 | struct nfs_server *server, | |
e73b83f2 BS |
886 | struct rpc_message *msg, |
887 | struct nfs4_sequence_args *args, | |
888 | struct nfs4_sequence_res *res, | |
889 | int cache_reply) | |
890 | { | |
a9c92d6b | 891 | nfs4_init_sequence(args, res, cache_reply); |
9915ea7e | 892 | return nfs4_call_sync_sequence(clnt, server, msg, args, res); |
e73b83f2 | 893 | } |
cccef3b9 | 894 | |
38478b24 | 895 | static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo) |
1da177e4 | 896 | { |
38478b24 | 897 | struct nfs_inode *nfsi = NFS_I(dir); |
1da177e4 | 898 | |
38478b24 | 899 | spin_lock(&dir->i_lock); |
359d7d1c | 900 | nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA; |
a9a4a87a | 901 | if (!cinfo->atomic || cinfo->before != dir->i_version) |
bfc69a45 | 902 | nfs_force_lookup_revalidate(dir); |
a9a4a87a | 903 | dir->i_version = cinfo->after; |
de242c0b | 904 | nfs_fscache_invalidate(dir); |
38478b24 | 905 | spin_unlock(&dir->i_lock); |
1da177e4 LT |
906 | } |
907 | ||
e56e0b78 | 908 | struct nfs4_opendata { |
c6d00e63 | 909 | struct kref kref; |
e56e0b78 TM |
910 | struct nfs_openargs o_arg; |
911 | struct nfs_openres o_res; | |
cdd4e68b TM |
912 | struct nfs_open_confirmargs c_arg; |
913 | struct nfs_open_confirmres c_res; | |
6926afd1 TM |
914 | struct nfs4_string owner_name; |
915 | struct nfs4_string group_name; | |
e56e0b78 | 916 | struct nfs_fattr f_attr; |
1775fd3e | 917 | struct nfs4_label *f_label; |
e56e0b78 | 918 | struct dentry *dir; |
82a2c1b7 | 919 | struct dentry *dentry; |
e56e0b78 | 920 | struct nfs4_state_owner *owner; |
aac00a8d | 921 | struct nfs4_state *state; |
e56e0b78 | 922 | struct iattr attrs; |
26e976a8 | 923 | unsigned long timestamp; |
3e309914 | 924 | unsigned int rpc_done : 1; |
5bc2afc2 | 925 | unsigned int file_created : 1; |
bdeca1b7 | 926 | unsigned int is_recover : 1; |
24ac23ab TM |
927 | int rpc_status; |
928 | int cancelled; | |
e56e0b78 TM |
929 | }; |
930 | ||
49f9a0fa TM |
931 | static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server, |
932 | int err, struct nfs4_exception *exception) | |
933 | { | |
934 | if (err != -EINVAL) | |
935 | return false; | |
936 | if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1)) | |
937 | return false; | |
938 | server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1; | |
939 | exception->retry = 1; | |
940 | return true; | |
941 | } | |
942 | ||
943 | static enum open_claim_type4 | |
944 | nfs4_map_atomic_open_claim(struct nfs_server *server, | |
945 | enum open_claim_type4 claim) | |
946 | { | |
947 | if (server->caps & NFS_CAP_ATOMIC_OPEN_V1) | |
948 | return claim; | |
949 | switch (claim) { | |
950 | default: | |
951 | return claim; | |
952 | case NFS4_OPEN_CLAIM_FH: | |
953 | return NFS4_OPEN_CLAIM_NULL; | |
954 | case NFS4_OPEN_CLAIM_DELEG_CUR_FH: | |
955 | return NFS4_OPEN_CLAIM_DELEGATE_CUR; | |
956 | case NFS4_OPEN_CLAIM_DELEG_PREV_FH: | |
957 | return NFS4_OPEN_CLAIM_DELEGATE_PREV; | |
958 | } | |
959 | } | |
2ced46c2 TM |
960 | |
961 | static void nfs4_init_opendata_res(struct nfs4_opendata *p) | |
962 | { | |
963 | p->o_res.f_attr = &p->f_attr; | |
1775fd3e | 964 | p->o_res.f_label = p->f_label; |
c1d51931 TM |
965 | p->o_res.seqid = p->o_arg.seqid; |
966 | p->c_res.seqid = p->c_arg.seqid; | |
2ced46c2 | 967 | p->o_res.server = p->o_arg.server; |
5f657530 | 968 | p->o_res.access_request = p->o_arg.access; |
2ced46c2 | 969 | nfs_fattr_init(&p->f_attr); |
6926afd1 | 970 | nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name); |
2ced46c2 TM |
971 | } |
972 | ||
82a2c1b7 | 973 | static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry, |
dc0b027d | 974 | struct nfs4_state_owner *sp, fmode_t fmode, int flags, |
8535b2be | 975 | const struct iattr *attrs, |
1775fd3e | 976 | struct nfs4_label *label, |
4a1c0893 | 977 | enum open_claim_type4 claim, |
8535b2be | 978 | gfp_t gfp_mask) |
e56e0b78 | 979 | { |
82a2c1b7 | 980 | struct dentry *parent = dget_parent(dentry); |
e56e0b78 TM |
981 | struct inode *dir = parent->d_inode; |
982 | struct nfs_server *server = NFS_SERVER(dir); | |
983 | struct nfs4_opendata *p; | |
984 | ||
8535b2be | 985 | p = kzalloc(sizeof(*p), gfp_mask); |
e56e0b78 TM |
986 | if (p == NULL) |
987 | goto err; | |
14c43f76 DQ |
988 | |
989 | p->f_label = nfs4_label_alloc(server, gfp_mask); | |
990 | if (IS_ERR(p->f_label)) | |
991 | goto err_free_p; | |
992 | ||
8535b2be | 993 | p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask); |
e56e0b78 | 994 | if (p->o_arg.seqid == NULL) |
14c43f76 | 995 | goto err_free_label; |
82a2c1b7 AV |
996 | nfs_sb_active(dentry->d_sb); |
997 | p->dentry = dget(dentry); | |
e56e0b78 TM |
998 | p->dir = parent; |
999 | p->owner = sp; | |
1000 | atomic_inc(&sp->so_count); | |
dc0b027d TM |
1001 | p->o_arg.open_flags = flags; |
1002 | p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE); | |
ae2bb032 WAA |
1003 | /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS |
1004 | * will return permission denied for all bits until close */ | |
1005 | if (!(flags & O_EXCL)) { | |
1006 | /* ask server to check for all possible rights as results | |
1007 | * are cached */ | |
1008 | p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY | | |
1009 | NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE; | |
ae2bb032 | 1010 | } |
7539bbab | 1011 | p->o_arg.clientid = server->nfs_client->cl_clientid; |
95b72eb0 TM |
1012 | p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time); |
1013 | p->o_arg.id.uniquifier = sp->so_seqid.owner_id; | |
82a2c1b7 | 1014 | p->o_arg.name = &dentry->d_name; |
e56e0b78 | 1015 | p->o_arg.server = server; |
aa9c2669 | 1016 | p->o_arg.bitmask = nfs4_bitmask(server, label); |
1549210f | 1017 | p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0]; |
1775fd3e | 1018 | p->o_arg.label = label; |
49f9a0fa TM |
1019 | p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim); |
1020 | switch (p->o_arg.claim) { | |
4a1c0893 TM |
1021 | case NFS4_OPEN_CLAIM_NULL: |
1022 | case NFS4_OPEN_CLAIM_DELEGATE_CUR: | |
1023 | case NFS4_OPEN_CLAIM_DELEGATE_PREV: | |
1024 | p->o_arg.fh = NFS_FH(dir); | |
1025 | break; | |
1026 | case NFS4_OPEN_CLAIM_PREVIOUS: | |
1027 | case NFS4_OPEN_CLAIM_FH: | |
1028 | case NFS4_OPEN_CLAIM_DELEG_CUR_FH: | |
1029 | case NFS4_OPEN_CLAIM_DELEG_PREV_FH: | |
1030 | p->o_arg.fh = NFS_FH(dentry->d_inode); | |
1031 | } | |
536e43d1 | 1032 | if (attrs != NULL && attrs->ia_valid != 0) { |
c281fa9c | 1033 | __u32 verf[2]; |
d77d76ff | 1034 | |
e56e0b78 TM |
1035 | p->o_arg.u.attrs = &p->attrs; |
1036 | memcpy(&p->attrs, attrs, sizeof(p->attrs)); | |
cd93710e CL |
1037 | |
1038 | verf[0] = jiffies; | |
1039 | verf[1] = current->pid; | |
1040 | memcpy(p->o_arg.u.verifier.data, verf, | |
1041 | sizeof(p->o_arg.u.verifier.data)); | |
e56e0b78 | 1042 | } |
cdd4e68b TM |
1043 | p->c_arg.fh = &p->o_res.fh; |
1044 | p->c_arg.stateid = &p->o_res.stateid; | |
1045 | p->c_arg.seqid = p->o_arg.seqid; | |
2ced46c2 | 1046 | nfs4_init_opendata_res(p); |
c6d00e63 | 1047 | kref_init(&p->kref); |
e56e0b78 | 1048 | return p; |
14c43f76 DQ |
1049 | |
1050 | err_free_label: | |
1051 | nfs4_label_free(p->f_label); | |
1052 | err_free_p: | |
e56e0b78 TM |
1053 | kfree(p); |
1054 | err: | |
1055 | dput(parent); | |
1056 | return NULL; | |
1057 | } | |
1058 | ||
c6d00e63 | 1059 | static void nfs4_opendata_free(struct kref *kref) |
e56e0b78 | 1060 | { |
c6d00e63 TM |
1061 | struct nfs4_opendata *p = container_of(kref, |
1062 | struct nfs4_opendata, kref); | |
82a2c1b7 | 1063 | struct super_block *sb = p->dentry->d_sb; |
c6d00e63 TM |
1064 | |
1065 | nfs_free_seqid(p->o_arg.seqid); | |
aac00a8d TM |
1066 | if (p->state != NULL) |
1067 | nfs4_put_open_state(p->state); | |
c6d00e63 | 1068 | nfs4_put_state_owner(p->owner); |
14c43f76 DQ |
1069 | |
1070 | nfs4_label_free(p->f_label); | |
1071 | ||
c6d00e63 | 1072 | dput(p->dir); |
82a2c1b7 AV |
1073 | dput(p->dentry); |
1074 | nfs_sb_deactive(sb); | |
6926afd1 | 1075 | nfs_fattr_free_names(&p->f_attr); |
e911b815 | 1076 | kfree(p->f_attr.mdsthreshold); |
c6d00e63 TM |
1077 | kfree(p); |
1078 | } | |
1079 | ||
1080 | static void nfs4_opendata_put(struct nfs4_opendata *p) | |
1081 | { | |
1082 | if (p != NULL) | |
1083 | kref_put(&p->kref, nfs4_opendata_free); | |
e56e0b78 TM |
1084 | } |
1085 | ||
06f814a3 TM |
1086 | static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task) |
1087 | { | |
06f814a3 TM |
1088 | int ret; |
1089 | ||
06f814a3 | 1090 | ret = rpc_wait_for_completion_task(task); |
06f814a3 TM |
1091 | return ret; |
1092 | } | |
1093 | ||
dc0b027d | 1094 | static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode) |
6ee41268 TM |
1095 | { |
1096 | int ret = 0; | |
dc0b027d | 1097 | |
536e43d1 | 1098 | if (open_mode & (O_EXCL|O_TRUNC)) |
dc0b027d TM |
1099 | goto out; |
1100 | switch (mode & (FMODE_READ|FMODE_WRITE)) { | |
6ee41268 | 1101 | case FMODE_READ: |
88069f77 TM |
1102 | ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 |
1103 | && state->n_rdonly != 0; | |
6ee41268 TM |
1104 | break; |
1105 | case FMODE_WRITE: | |
88069f77 TM |
1106 | ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 |
1107 | && state->n_wronly != 0; | |
6ee41268 TM |
1108 | break; |
1109 | case FMODE_READ|FMODE_WRITE: | |
88069f77 TM |
1110 | ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 |
1111 | && state->n_rdwr != 0; | |
6ee41268 | 1112 | } |
dc0b027d | 1113 | out: |
6ee41268 TM |
1114 | return ret; |
1115 | } | |
1116 | ||
dc0b027d | 1117 | static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode) |
aac00a8d | 1118 | { |
652f89f6 TM |
1119 | if (delegation == NULL) |
1120 | return 0; | |
dc0b027d | 1121 | if ((delegation->type & fmode) != fmode) |
aac00a8d | 1122 | return 0; |
d25be546 TM |
1123 | if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) |
1124 | return 0; | |
b7391f44 | 1125 | nfs_mark_delegation_referenced(delegation); |
aac00a8d TM |
1126 | return 1; |
1127 | } | |
1128 | ||
dc0b027d | 1129 | static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode) |
e7616923 | 1130 | { |
dc0b027d | 1131 | switch (fmode) { |
e7616923 TM |
1132 | case FMODE_WRITE: |
1133 | state->n_wronly++; | |
1134 | break; | |
1135 | case FMODE_READ: | |
1136 | state->n_rdonly++; | |
1137 | break; | |
1138 | case FMODE_READ|FMODE_WRITE: | |
1139 | state->n_rdwr++; | |
1140 | } | |
dc0b027d | 1141 | nfs4_state_set_mode_locked(state, state->state | fmode); |
003707c7 TM |
1142 | } |
1143 | ||
4f14c194 TM |
1144 | static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state) |
1145 | { | |
1146 | struct nfs_client *clp = state->owner->so_server->nfs_client; | |
1147 | bool need_recover = false; | |
1148 | ||
1149 | if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly) | |
1150 | need_recover = true; | |
1151 | if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly) | |
1152 | need_recover = true; | |
1153 | if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr) | |
1154 | need_recover = true; | |
1155 | if (need_recover) | |
1156 | nfs4_state_mark_reclaim_nograce(clp, state); | |
1157 | } | |
1158 | ||
e999e80e TM |
1159 | static bool nfs_need_update_open_stateid(struct nfs4_state *state, |
1160 | nfs4_stateid *stateid) | |
1161 | { | |
1162 | if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0) | |
1163 | return true; | |
4f14c194 TM |
1164 | if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) { |
1165 | nfs_test_and_clear_all_open_stateid(state); | |
e999e80e | 1166 | return true; |
4f14c194 | 1167 | } |
e999e80e TM |
1168 | if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) |
1169 | return true; | |
1170 | return false; | |
1171 | } | |
1172 | ||
226056c5 TM |
1173 | static void nfs_clear_open_stateid_locked(struct nfs4_state *state, |
1174 | nfs4_stateid *stateid, fmode_t fmode) | |
003707c7 | 1175 | { |
226056c5 TM |
1176 | clear_bit(NFS_O_RDWR_STATE, &state->flags); |
1177 | switch (fmode & (FMODE_READ|FMODE_WRITE)) { | |
1178 | case FMODE_WRITE: | |
1179 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); | |
1180 | break; | |
1181 | case FMODE_READ: | |
1182 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1183 | break; | |
1184 | case 0: | |
1185 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); | |
1186 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1187 | clear_bit(NFS_OPEN_STATE, &state->flags); | |
1188 | } | |
1189 | if (stateid == NULL) | |
1190 | return; | |
1191 | if (!nfs_need_update_open_stateid(state, stateid)) | |
1192 | return; | |
003707c7 | 1193 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) |
f597c537 TM |
1194 | nfs4_stateid_copy(&state->stateid, stateid); |
1195 | nfs4_stateid_copy(&state->open_stateid, stateid); | |
226056c5 TM |
1196 | } |
1197 | ||
1198 | static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode) | |
1199 | { | |
1200 | write_seqlock(&state->seqlock); | |
1201 | nfs_clear_open_stateid_locked(state, stateid, fmode); | |
1202 | write_sequnlock(&state->seqlock); | |
4f14c194 TM |
1203 | if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) |
1204 | nfs4_schedule_state_manager(state->owner->so_server->nfs_client); | |
226056c5 TM |
1205 | } |
1206 | ||
dc0b027d | 1207 | static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode) |
003707c7 | 1208 | { |
dc0b027d | 1209 | switch (fmode) { |
003707c7 TM |
1210 | case FMODE_READ: |
1211 | set_bit(NFS_O_RDONLY_STATE, &state->flags); | |
1212 | break; | |
1213 | case FMODE_WRITE: | |
1214 | set_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1215 | break; | |
1216 | case FMODE_READ|FMODE_WRITE: | |
1217 | set_bit(NFS_O_RDWR_STATE, &state->flags); | |
1218 | } | |
e999e80e TM |
1219 | if (!nfs_need_update_open_stateid(state, stateid)) |
1220 | return; | |
1221 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) | |
1222 | nfs4_stateid_copy(&state->stateid, stateid); | |
1223 | nfs4_stateid_copy(&state->open_stateid, stateid); | |
003707c7 TM |
1224 | } |
1225 | ||
dc0b027d | 1226 | static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode) |
1da177e4 | 1227 | { |
8bda4e4c TM |
1228 | /* |
1229 | * Protect the call to nfs4_state_set_mode_locked and | |
1230 | * serialise the stateid update | |
1231 | */ | |
1232 | write_seqlock(&state->seqlock); | |
003707c7 | 1233 | if (deleg_stateid != NULL) { |
f597c537 | 1234 | nfs4_stateid_copy(&state->stateid, deleg_stateid); |
003707c7 TM |
1235 | set_bit(NFS_DELEGATED_STATE, &state->flags); |
1236 | } | |
1237 | if (open_stateid != NULL) | |
dc0b027d | 1238 | nfs_set_open_stateid_locked(state, open_stateid, fmode); |
8bda4e4c TM |
1239 | write_sequnlock(&state->seqlock); |
1240 | spin_lock(&state->owner->so_lock); | |
dc0b027d | 1241 | update_open_stateflags(state, fmode); |
ec073428 | 1242 | spin_unlock(&state->owner->so_lock); |
1da177e4 LT |
1243 | } |
1244 | ||
dc0b027d | 1245 | static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode) |
34310430 TM |
1246 | { |
1247 | struct nfs_inode *nfsi = NFS_I(state->inode); | |
1248 | struct nfs_delegation *deleg_cur; | |
1249 | int ret = 0; | |
1250 | ||
dc0b027d | 1251 | fmode &= (FMODE_READ|FMODE_WRITE); |
34310430 TM |
1252 | |
1253 | rcu_read_lock(); | |
1254 | deleg_cur = rcu_dereference(nfsi->delegation); | |
1255 | if (deleg_cur == NULL) | |
1256 | goto no_delegation; | |
1257 | ||
1258 | spin_lock(&deleg_cur->lock); | |
17f26b12 | 1259 | if (rcu_dereference(nfsi->delegation) != deleg_cur || |
d25be546 | 1260 | test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) || |
dc0b027d | 1261 | (deleg_cur->type & fmode) != fmode) |
34310430 TM |
1262 | goto no_delegation_unlock; |
1263 | ||
1264 | if (delegation == NULL) | |
1265 | delegation = &deleg_cur->stateid; | |
f597c537 | 1266 | else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation)) |
34310430 TM |
1267 | goto no_delegation_unlock; |
1268 | ||
b7391f44 | 1269 | nfs_mark_delegation_referenced(deleg_cur); |
dc0b027d | 1270 | __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode); |
34310430 TM |
1271 | ret = 1; |
1272 | no_delegation_unlock: | |
1273 | spin_unlock(&deleg_cur->lock); | |
1274 | no_delegation: | |
1275 | rcu_read_unlock(); | |
1276 | ||
1277 | if (!ret && open_stateid != NULL) { | |
dc0b027d | 1278 | __update_open_stateid(state, open_stateid, NULL, fmode); |
34310430 TM |
1279 | ret = 1; |
1280 | } | |
4f14c194 TM |
1281 | if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) |
1282 | nfs4_schedule_state_manager(state->owner->so_server->nfs_client); | |
34310430 TM |
1283 | |
1284 | return ret; | |
1285 | } | |
1286 | ||
1287 | ||
dc0b027d | 1288 | static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode) |
aac00a8d TM |
1289 | { |
1290 | struct nfs_delegation *delegation; | |
1291 | ||
1292 | rcu_read_lock(); | |
1293 | delegation = rcu_dereference(NFS_I(inode)->delegation); | |
dc0b027d | 1294 | if (delegation == NULL || (delegation->type & fmode) == fmode) { |
aac00a8d TM |
1295 | rcu_read_unlock(); |
1296 | return; | |
1297 | } | |
1298 | rcu_read_unlock(); | |
57ec14c5 | 1299 | nfs4_inode_return_delegation(inode); |
aac00a8d TM |
1300 | } |
1301 | ||
6ee41268 | 1302 | static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata) |
aac00a8d TM |
1303 | { |
1304 | struct nfs4_state *state = opendata->state; | |
1305 | struct nfs_inode *nfsi = NFS_I(state->inode); | |
1306 | struct nfs_delegation *delegation; | |
f448badd | 1307 | int open_mode = opendata->o_arg.open_flags; |
dc0b027d | 1308 | fmode_t fmode = opendata->o_arg.fmode; |
aac00a8d TM |
1309 | nfs4_stateid stateid; |
1310 | int ret = -EAGAIN; | |
1311 | ||
aac00a8d | 1312 | for (;;) { |
61beef75 | 1313 | spin_lock(&state->owner->so_lock); |
dc0b027d | 1314 | if (can_open_cached(state, fmode, open_mode)) { |
61beef75 | 1315 | update_open_stateflags(state, fmode); |
6ee41268 | 1316 | spin_unlock(&state->owner->so_lock); |
61beef75 | 1317 | goto out_return_state; |
6ee41268 | 1318 | } |
61beef75 | 1319 | spin_unlock(&state->owner->so_lock); |
34310430 TM |
1320 | rcu_read_lock(); |
1321 | delegation = rcu_dereference(nfsi->delegation); | |
652f89f6 | 1322 | if (!can_open_delegated(delegation, fmode)) { |
34310430 | 1323 | rcu_read_unlock(); |
aac00a8d | 1324 | break; |
34310430 | 1325 | } |
aac00a8d | 1326 | /* Save the delegation */ |
f597c537 | 1327 | nfs4_stateid_copy(&stateid, &delegation->stateid); |
aac00a8d | 1328 | rcu_read_unlock(); |
fa332941 | 1329 | nfs_release_seqid(opendata->o_arg.seqid); |
bdeca1b7 TM |
1330 | if (!opendata->is_recover) { |
1331 | ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode); | |
1332 | if (ret != 0) | |
1333 | goto out; | |
1334 | } | |
aac00a8d | 1335 | ret = -EAGAIN; |
34310430 TM |
1336 | |
1337 | /* Try to update the stateid using the delegation */ | |
dc0b027d | 1338 | if (update_open_stateid(state, NULL, &stateid, fmode)) |
34310430 | 1339 | goto out_return_state; |
aac00a8d | 1340 | } |
aac00a8d TM |
1341 | out: |
1342 | return ERR_PTR(ret); | |
1343 | out_return_state: | |
1344 | atomic_inc(&state->count); | |
1345 | return state; | |
1346 | } | |
1347 | ||
e23008ec AA |
1348 | static void |
1349 | nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state) | |
1350 | { | |
1351 | struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client; | |
1352 | struct nfs_delegation *delegation; | |
1353 | int delegation_flags = 0; | |
1354 | ||
1355 | rcu_read_lock(); | |
1356 | delegation = rcu_dereference(NFS_I(state->inode)->delegation); | |
1357 | if (delegation) | |
1358 | delegation_flags = delegation->flags; | |
1359 | rcu_read_unlock(); | |
1360 | if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) { | |
1361 | pr_err_ratelimited("NFS: Broken NFSv4 server %s is " | |
1362 | "returning a delegation for " | |
1363 | "OPEN(CLAIM_DELEGATE_CUR)\n", | |
1364 | clp->cl_hostname); | |
1365 | } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0) | |
1366 | nfs_inode_set_delegation(state->inode, | |
1367 | data->owner->so_cred, | |
1368 | &data->o_res); | |
1369 | else | |
1370 | nfs_inode_reclaim_delegation(state->inode, | |
1371 | data->owner->so_cred, | |
1372 | &data->o_res); | |
1373 | } | |
1374 | ||
1375 | /* | |
1376 | * Check the inode attributes against the CLAIM_PREVIOUS returned attributes | |
1377 | * and update the nfs4_state. | |
1378 | */ | |
1379 | static struct nfs4_state * | |
1380 | _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data) | |
1381 | { | |
1382 | struct inode *inode = data->state->inode; | |
1383 | struct nfs4_state *state = data->state; | |
1384 | int ret; | |
1385 | ||
d2bfda2e WAA |
1386 | if (!data->rpc_done) { |
1387 | if (data->rpc_status) { | |
1388 | ret = data->rpc_status; | |
1389 | goto err; | |
1390 | } | |
1391 | /* cached opens have already been processed */ | |
1392 | goto update; | |
e23008ec AA |
1393 | } |
1394 | ||
e23008ec AA |
1395 | ret = nfs_refresh_inode(inode, &data->f_attr); |
1396 | if (ret) | |
1397 | goto err; | |
1398 | ||
1399 | if (data->o_res.delegation_type != 0) | |
1400 | nfs4_opendata_check_deleg(data, state); | |
d2bfda2e | 1401 | update: |
e23008ec AA |
1402 | update_open_stateid(state, &data->o_res.stateid, NULL, |
1403 | data->o_arg.fmode); | |
d49f042a | 1404 | atomic_inc(&state->count); |
e23008ec AA |
1405 | |
1406 | return state; | |
1407 | err: | |
1408 | return ERR_PTR(ret); | |
1409 | ||
1410 | } | |
1411 | ||
1412 | static struct nfs4_state * | |
1413 | _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) | |
24ac23ab TM |
1414 | { |
1415 | struct inode *inode; | |
1416 | struct nfs4_state *state = NULL; | |
1b370bc2 | 1417 | int ret; |
24ac23ab | 1418 | |
aac00a8d | 1419 | if (!data->rpc_done) { |
6ee41268 | 1420 | state = nfs4_try_open_cached(data); |
aac00a8d TM |
1421 | goto out; |
1422 | } | |
1423 | ||
1b370bc2 | 1424 | ret = -EAGAIN; |
24ac23ab | 1425 | if (!(data->f_attr.valid & NFS_ATTR_FATTR)) |
1b370bc2 | 1426 | goto err; |
1775fd3e | 1427 | inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label); |
1b370bc2 | 1428 | ret = PTR_ERR(inode); |
03f28e3a | 1429 | if (IS_ERR(inode)) |
1b370bc2 TM |
1430 | goto err; |
1431 | ret = -ENOMEM; | |
24ac23ab TM |
1432 | state = nfs4_get_open_state(inode, data->owner); |
1433 | if (state == NULL) | |
1b370bc2 | 1434 | goto err_put_inode; |
e23008ec AA |
1435 | if (data->o_res.delegation_type != 0) |
1436 | nfs4_opendata_check_deleg(data, state); | |
34310430 | 1437 | update_open_stateid(state, &data->o_res.stateid, NULL, |
dc0b027d | 1438 | data->o_arg.fmode); |
24ac23ab | 1439 | iput(inode); |
aac00a8d | 1440 | out: |
7aa262b5 | 1441 | nfs_release_seqid(data->o_arg.seqid); |
24ac23ab | 1442 | return state; |
1b370bc2 TM |
1443 | err_put_inode: |
1444 | iput(inode); | |
1445 | err: | |
1446 | return ERR_PTR(ret); | |
24ac23ab TM |
1447 | } |
1448 | ||
e23008ec AA |
1449 | static struct nfs4_state * |
1450 | nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) | |
1451 | { | |
1452 | if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) | |
1453 | return _nfs4_opendata_reclaim_to_nfs4_state(data); | |
1454 | return _nfs4_opendata_to_nfs4_state(data); | |
1455 | } | |
1456 | ||
864472e9 TM |
1457 | static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state) |
1458 | { | |
1459 | struct nfs_inode *nfsi = NFS_I(state->inode); | |
1460 | struct nfs_open_context *ctx; | |
1461 | ||
1462 | spin_lock(&state->inode->i_lock); | |
1463 | list_for_each_entry(ctx, &nfsi->open_files, list) { | |
1464 | if (ctx->state != state) | |
1465 | continue; | |
1466 | get_nfs_open_context(ctx); | |
1467 | spin_unlock(&state->inode->i_lock); | |
1468 | return ctx; | |
1469 | } | |
1470 | spin_unlock(&state->inode->i_lock); | |
1471 | return ERR_PTR(-ENOENT); | |
1472 | } | |
1473 | ||
4a1c0893 TM |
1474 | static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, |
1475 | struct nfs4_state *state, enum open_claim_type4 claim) | |
6f220ed5 TM |
1476 | { |
1477 | struct nfs4_opendata *opendata; | |
1478 | ||
4a1c0893 | 1479 | opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, |
1775fd3e | 1480 | NULL, NULL, claim, GFP_NOFS); |
6f220ed5 TM |
1481 | if (opendata == NULL) |
1482 | return ERR_PTR(-ENOMEM); | |
1483 | opendata->state = state; | |
1484 | atomic_inc(&state->count); | |
1485 | return opendata; | |
1486 | } | |
1487 | ||
dc0b027d | 1488 | static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res) |
864472e9 | 1489 | { |
2ced46c2 | 1490 | struct nfs4_state *newstate; |
864472e9 TM |
1491 | int ret; |
1492 | ||
dc0b027d TM |
1493 | opendata->o_arg.open_flags = 0; |
1494 | opendata->o_arg.fmode = fmode; | |
2ced46c2 TM |
1495 | memset(&opendata->o_res, 0, sizeof(opendata->o_res)); |
1496 | memset(&opendata->c_res, 0, sizeof(opendata->c_res)); | |
1497 | nfs4_init_opendata_res(opendata); | |
b257957e | 1498 | ret = _nfs4_recover_proc_open(opendata); |
864472e9 TM |
1499 | if (ret != 0) |
1500 | return ret; | |
2ced46c2 | 1501 | newstate = nfs4_opendata_to_nfs4_state(opendata); |
1b370bc2 TM |
1502 | if (IS_ERR(newstate)) |
1503 | return PTR_ERR(newstate); | |
643168c2 | 1504 | nfs4_close_state(newstate, fmode); |
2ced46c2 | 1505 | *res = newstate; |
864472e9 TM |
1506 | return 0; |
1507 | } | |
1508 | ||
1509 | static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state) | |
1510 | { | |
864472e9 | 1511 | struct nfs4_state *newstate; |
864472e9 TM |
1512 | int ret; |
1513 | ||
4f14c194 TM |
1514 | /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */ |
1515 | clear_bit(NFS_O_RDWR_STATE, &state->flags); | |
1516 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); | |
1517 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); | |
864472e9 | 1518 | /* memory barrier prior to reading state->n_* */ |
2ced46c2 | 1519 | clear_bit(NFS_DELEGATED_STATE, &state->flags); |
fd068b20 | 1520 | clear_bit(NFS_OPEN_STATE, &state->flags); |
864472e9 TM |
1521 | smp_rmb(); |
1522 | if (state->n_rdwr != 0) { | |
2ced46c2 | 1523 | ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate); |
864472e9 TM |
1524 | if (ret != 0) |
1525 | return ret; | |
2ced46c2 TM |
1526 | if (newstate != state) |
1527 | return -ESTALE; | |
864472e9 TM |
1528 | } |
1529 | if (state->n_wronly != 0) { | |
2ced46c2 | 1530 | ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate); |
864472e9 TM |
1531 | if (ret != 0) |
1532 | return ret; | |
2ced46c2 TM |
1533 | if (newstate != state) |
1534 | return -ESTALE; | |
864472e9 TM |
1535 | } |
1536 | if (state->n_rdonly != 0) { | |
2ced46c2 | 1537 | ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate); |
864472e9 TM |
1538 | if (ret != 0) |
1539 | return ret; | |
2ced46c2 TM |
1540 | if (newstate != state) |
1541 | return -ESTALE; | |
864472e9 | 1542 | } |
1ac7e2fd TM |
1543 | /* |
1544 | * We may have performed cached opens for all three recoveries. | |
1545 | * Check if we need to update the current stateid. | |
1546 | */ | |
1547 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 && | |
f597c537 | 1548 | !nfs4_stateid_match(&state->stateid, &state->open_stateid)) { |
8bda4e4c | 1549 | write_seqlock(&state->seqlock); |
1ac7e2fd | 1550 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) |
f597c537 | 1551 | nfs4_stateid_copy(&state->stateid, &state->open_stateid); |
8bda4e4c | 1552 | write_sequnlock(&state->seqlock); |
1ac7e2fd | 1553 | } |
864472e9 TM |
1554 | return 0; |
1555 | } | |
1556 | ||
1da177e4 LT |
1557 | /* |
1558 | * OPEN_RECLAIM: | |
1559 | * reclaim state on the server after a reboot. | |
1da177e4 | 1560 | */ |
539cd03a | 1561 | static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) |
1da177e4 | 1562 | { |
1ac7e2fd | 1563 | struct nfs_delegation *delegation; |
864472e9 | 1564 | struct nfs4_opendata *opendata; |
dc0b027d | 1565 | fmode_t delegation_type = 0; |
1da177e4 LT |
1566 | int status; |
1567 | ||
4a1c0893 TM |
1568 | opendata = nfs4_open_recoverdata_alloc(ctx, state, |
1569 | NFS4_OPEN_CLAIM_PREVIOUS); | |
6f220ed5 TM |
1570 | if (IS_ERR(opendata)) |
1571 | return PTR_ERR(opendata); | |
1ac7e2fd TM |
1572 | rcu_read_lock(); |
1573 | delegation = rcu_dereference(NFS_I(state->inode)->delegation); | |
15c831bf | 1574 | if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0) |
65bbf6bd | 1575 | delegation_type = delegation->type; |
1ac7e2fd | 1576 | rcu_read_unlock(); |
864472e9 TM |
1577 | opendata->o_arg.u.delegation_type = delegation_type; |
1578 | status = nfs4_open_recover(opendata, state); | |
c6d00e63 | 1579 | nfs4_opendata_put(opendata); |
1da177e4 LT |
1580 | return status; |
1581 | } | |
1582 | ||
539cd03a | 1583 | static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) |
1da177e4 LT |
1584 | { |
1585 | struct nfs_server *server = NFS_SERVER(state->inode); | |
1586 | struct nfs4_exception exception = { }; | |
1587 | int err; | |
1588 | do { | |
539cd03a | 1589 | err = _nfs4_do_open_reclaim(ctx, state); |
42113a75 | 1590 | trace_nfs4_open_reclaim(ctx, 0, err); |
49f9a0fa TM |
1591 | if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) |
1592 | continue; | |
168667c4 | 1593 | if (err != -NFS4ERR_DELAY) |
202b50dc TM |
1594 | break; |
1595 | nfs4_handle_exception(server, err, &exception); | |
1da177e4 LT |
1596 | } while (exception.retry); |
1597 | return err; | |
1598 | } | |
1599 | ||
864472e9 TM |
1600 | static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state) |
1601 | { | |
1602 | struct nfs_open_context *ctx; | |
1603 | int ret; | |
1604 | ||
1605 | ctx = nfs4_state_find_open_context(state); | |
1606 | if (IS_ERR(ctx)) | |
91876b13 | 1607 | return -EAGAIN; |
539cd03a | 1608 | ret = nfs4_do_open_reclaim(ctx, state); |
864472e9 TM |
1609 | put_nfs_open_context(ctx); |
1610 | return ret; | |
1611 | } | |
1612 | ||
db4f2e63 | 1613 | static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err) |
1da177e4 | 1614 | { |
be76b5b6 TM |
1615 | switch (err) { |
1616 | default: | |
1617 | printk(KERN_ERR "NFS: %s: unhandled error " | |
1618 | "%d.\n", __func__, err); | |
1619 | case 0: | |
1620 | case -ENOENT: | |
1621 | case -ESTALE: | |
1622 | break; | |
1623 | case -NFS4ERR_BADSESSION: | |
1624 | case -NFS4ERR_BADSLOT: | |
1625 | case -NFS4ERR_BAD_HIGH_SLOT: | |
1626 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
1627 | case -NFS4ERR_DEADSESSION: | |
1628 | set_bit(NFS_DELEGATED_STATE, &state->flags); | |
1629 | nfs4_schedule_session_recovery(server->nfs_client->cl_session, err); | |
1630 | return -EAGAIN; | |
1631 | case -NFS4ERR_STALE_CLIENTID: | |
1632 | case -NFS4ERR_STALE_STATEID: | |
1633 | set_bit(NFS_DELEGATED_STATE, &state->flags); | |
1634 | case -NFS4ERR_EXPIRED: | |
1635 | /* Don't recall a delegation if it was lost */ | |
1636 | nfs4_schedule_lease_recovery(server->nfs_client); | |
1637 | return -EAGAIN; | |
352297b9 CL |
1638 | case -NFS4ERR_MOVED: |
1639 | nfs4_schedule_migration_recovery(server); | |
1640 | return -EAGAIN; | |
8ef2f8d4 CL |
1641 | case -NFS4ERR_LEASE_MOVED: |
1642 | nfs4_schedule_lease_moved_recovery(server->nfs_client); | |
1643 | return -EAGAIN; | |
be76b5b6 TM |
1644 | case -NFS4ERR_DELEG_REVOKED: |
1645 | case -NFS4ERR_ADMIN_REVOKED: | |
1646 | case -NFS4ERR_BAD_STATEID: | |
db4f2e63 | 1647 | case -NFS4ERR_OPENMODE: |
be76b5b6 TM |
1648 | nfs_inode_find_state_and_recover(state->inode, |
1649 | stateid); | |
1650 | nfs4_schedule_stateid_recovery(server, state); | |
869f9dfa | 1651 | return -EAGAIN; |
be76b5b6 TM |
1652 | case -NFS4ERR_DELAY: |
1653 | case -NFS4ERR_GRACE: | |
1654 | set_bit(NFS_DELEGATED_STATE, &state->flags); | |
1655 | ssleep(1); | |
1656 | return -EAGAIN; | |
db4f2e63 TM |
1657 | case -ENOMEM: |
1658 | case -NFS4ERR_DENIED: | |
1659 | /* kill_proc(fl->fl_pid, SIGLOST, 1); */ | |
1660 | return 0; | |
be76b5b6 | 1661 | } |
1da177e4 LT |
1662 | return err; |
1663 | } | |
1664 | ||
db4f2e63 TM |
1665 | int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid) |
1666 | { | |
1667 | struct nfs_server *server = NFS_SERVER(state->inode); | |
1668 | struct nfs4_opendata *opendata; | |
1669 | int err; | |
1670 | ||
1671 | opendata = nfs4_open_recoverdata_alloc(ctx, state, | |
1672 | NFS4_OPEN_CLAIM_DELEG_CUR_FH); | |
1673 | if (IS_ERR(opendata)) | |
1674 | return PTR_ERR(opendata); | |
1675 | nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid); | |
1676 | err = nfs4_open_recover(opendata, state); | |
1677 | nfs4_opendata_put(opendata); | |
1678 | return nfs4_handle_delegation_recall_error(server, state, stateid, err); | |
1679 | } | |
1680 | ||
be05c860 CL |
1681 | static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata) |
1682 | { | |
1683 | struct nfs4_opendata *data = calldata; | |
1684 | ||
cb04ad2a PT |
1685 | nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl, |
1686 | &data->c_arg.seq_args, &data->c_res.seq_res, task); | |
be05c860 CL |
1687 | } |
1688 | ||
cdd4e68b TM |
1689 | static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata) |
1690 | { | |
1691 | struct nfs4_opendata *data = calldata; | |
1692 | ||
17ead6c8 | 1693 | nfs40_sequence_done(task, &data->c_res.seq_res); |
be05c860 | 1694 | |
cdd4e68b | 1695 | data->rpc_status = task->tk_status; |
26e976a8 | 1696 | if (data->rpc_status == 0) { |
f597c537 | 1697 | nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid); |
bb22629e | 1698 | nfs_confirm_seqid(&data->owner->so_seqid, 0); |
26e976a8 | 1699 | renew_lease(data->o_res.server, data->timestamp); |
3e309914 | 1700 | data->rpc_done = 1; |
26e976a8 | 1701 | } |
cdd4e68b TM |
1702 | } |
1703 | ||
1704 | static void nfs4_open_confirm_release(void *calldata) | |
1705 | { | |
1706 | struct nfs4_opendata *data = calldata; | |
1707 | struct nfs4_state *state = NULL; | |
1708 | ||
1709 | /* If this request hasn't been cancelled, do nothing */ | |
1710 | if (data->cancelled == 0) | |
1711 | goto out_free; | |
1712 | /* In case of error, no cleanup! */ | |
3e309914 | 1713 | if (!data->rpc_done) |
cdd4e68b | 1714 | goto out_free; |
cdd4e68b | 1715 | state = nfs4_opendata_to_nfs4_state(data); |
1b370bc2 | 1716 | if (!IS_ERR(state)) |
643168c2 | 1717 | nfs4_close_state(state, data->o_arg.fmode); |
cdd4e68b | 1718 | out_free: |
c6d00e63 | 1719 | nfs4_opendata_put(data); |
cdd4e68b TM |
1720 | } |
1721 | ||
1722 | static const struct rpc_call_ops nfs4_open_confirm_ops = { | |
be05c860 | 1723 | .rpc_call_prepare = nfs4_open_confirm_prepare, |
cdd4e68b TM |
1724 | .rpc_call_done = nfs4_open_confirm_done, |
1725 | .rpc_release = nfs4_open_confirm_release, | |
1726 | }; | |
1727 | ||
1728 | /* | |
1729 | * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata | |
1730 | */ | |
1731 | static int _nfs4_proc_open_confirm(struct nfs4_opendata *data) | |
1732 | { | |
1733 | struct nfs_server *server = NFS_SERVER(data->dir->d_inode); | |
1734 | struct rpc_task *task; | |
5138fde0 TM |
1735 | struct rpc_message msg = { |
1736 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM], | |
1737 | .rpc_argp = &data->c_arg, | |
1738 | .rpc_resp = &data->c_res, | |
1739 | .rpc_cred = data->owner->so_cred, | |
1740 | }; | |
c970aa85 TM |
1741 | struct rpc_task_setup task_setup_data = { |
1742 | .rpc_client = server->client, | |
5138fde0 | 1743 | .rpc_message = &msg, |
c970aa85 TM |
1744 | .callback_ops = &nfs4_open_confirm_ops, |
1745 | .callback_data = data, | |
101070ca | 1746 | .workqueue = nfsiod_workqueue, |
c970aa85 TM |
1747 | .flags = RPC_TASK_ASYNC, |
1748 | }; | |
1da177e4 LT |
1749 | int status; |
1750 | ||
17ead6c8 | 1751 | nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1); |
c6d00e63 | 1752 | kref_get(&data->kref); |
3e309914 TM |
1753 | data->rpc_done = 0; |
1754 | data->rpc_status = 0; | |
5138fde0 | 1755 | data->timestamp = jiffies; |
c970aa85 | 1756 | task = rpc_run_task(&task_setup_data); |
7a1218a2 | 1757 | if (IS_ERR(task)) |
cdd4e68b | 1758 | return PTR_ERR(task); |
cdd4e68b TM |
1759 | status = nfs4_wait_for_completion_rpc_task(task); |
1760 | if (status != 0) { | |
1761 | data->cancelled = 1; | |
1762 | smp_wmb(); | |
1763 | } else | |
1764 | status = data->rpc_status; | |
e6b3c4db | 1765 | rpc_put_task(task); |
1da177e4 LT |
1766 | return status; |
1767 | } | |
1768 | ||
24ac23ab | 1769 | static void nfs4_open_prepare(struct rpc_task *task, void *calldata) |
1da177e4 | 1770 | { |
24ac23ab TM |
1771 | struct nfs4_opendata *data = calldata; |
1772 | struct nfs4_state_owner *sp = data->owner; | |
549b19cc | 1773 | struct nfs_client *clp = sp->so_server->nfs_client; |
5138fde0 | 1774 | |
24ac23ab | 1775 | if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0) |
c8da19b9 | 1776 | goto out_wait; |
aac00a8d TM |
1777 | /* |
1778 | * Check if we still need to send an OPEN call, or if we can use | |
1779 | * a delegation instead. | |
1780 | */ | |
1781 | if (data->state != NULL) { | |
1782 | struct nfs_delegation *delegation; | |
1783 | ||
dc0b027d | 1784 | if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags)) |
6ee41268 | 1785 | goto out_no_action; |
aac00a8d TM |
1786 | rcu_read_lock(); |
1787 | delegation = rcu_dereference(NFS_I(data->state->inode)->delegation); | |
652f89f6 | 1788 | if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR && |
cd4c9be2 | 1789 | data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH && |
652f89f6 TM |
1790 | can_open_delegated(delegation, data->o_arg.fmode)) |
1791 | goto unlock_no_action; | |
aac00a8d TM |
1792 | rcu_read_unlock(); |
1793 | } | |
95b72eb0 | 1794 | /* Update client id. */ |
549b19cc | 1795 | data->o_arg.clientid = clp->cl_clientid; |
8188df17 TM |
1796 | switch (data->o_arg.claim) { |
1797 | case NFS4_OPEN_CLAIM_PREVIOUS: | |
1798 | case NFS4_OPEN_CLAIM_DELEG_CUR_FH: | |
1799 | case NFS4_OPEN_CLAIM_DELEG_PREV_FH: | |
e23008ec | 1800 | data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0]; |
8188df17 TM |
1801 | case NFS4_OPEN_CLAIM_FH: |
1802 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR]; | |
6f220ed5 TM |
1803 | nfs_copy_fh(&data->o_res.fh, data->o_arg.fh); |
1804 | } | |
26e976a8 | 1805 | data->timestamp = jiffies; |
035168ab | 1806 | if (nfs4_setup_sequence(data->o_arg.server, |
d898528c | 1807 | &data->o_arg.seq_args, |
2240a9e2 TM |
1808 | &data->o_res.seq_res, |
1809 | task) != 0) | |
1810 | nfs_release_seqid(data->o_arg.seqid); | |
549b19cc TM |
1811 | |
1812 | /* Set the create mode (note dependency on the session type) */ | |
1813 | data->o_arg.createmode = NFS4_CREATE_UNCHECKED; | |
1814 | if (data->o_arg.open_flags & O_EXCL) { | |
1815 | data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE; | |
1816 | if (nfs4_has_persistent_session(clp)) | |
1817 | data->o_arg.createmode = NFS4_CREATE_GUARDED; | |
1818 | else if (clp->cl_mvops->minor_version > 0) | |
1819 | data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1; | |
1820 | } | |
6ee41268 | 1821 | return; |
652f89f6 TM |
1822 | unlock_no_action: |
1823 | rcu_read_unlock(); | |
6ee41268 TM |
1824 | out_no_action: |
1825 | task->tk_action = NULL; | |
c8da19b9 | 1826 | out_wait: |
b75ad4cd | 1827 | nfs4_sequence_done(task, &data->o_res.seq_res); |
b257957e AB |
1828 | } |
1829 | ||
24ac23ab TM |
1830 | static void nfs4_open_done(struct rpc_task *task, void *calldata) |
1831 | { | |
1832 | struct nfs4_opendata *data = calldata; | |
1da177e4 | 1833 | |
24ac23ab | 1834 | data->rpc_status = task->tk_status; |
d898528c | 1835 | |
14516c3a TM |
1836 | if (!nfs4_sequence_done(task, &data->o_res.seq_res)) |
1837 | return; | |
d898528c | 1838 | |
24ac23ab | 1839 | if (task->tk_status == 0) { |
807d66d8 TM |
1840 | if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) { |
1841 | switch (data->o_res.f_attr->mode & S_IFMT) { | |
6f926b5b TM |
1842 | case S_IFREG: |
1843 | break; | |
1844 | case S_IFLNK: | |
24ac23ab | 1845 | data->rpc_status = -ELOOP; |
6f926b5b TM |
1846 | break; |
1847 | case S_IFDIR: | |
24ac23ab | 1848 | data->rpc_status = -EISDIR; |
6f926b5b TM |
1849 | break; |
1850 | default: | |
24ac23ab | 1851 | data->rpc_status = -ENOTDIR; |
807d66d8 | 1852 | } |
6f926b5b | 1853 | } |
26e976a8 | 1854 | renew_lease(data->o_res.server, data->timestamp); |
0f9f95e0 TM |
1855 | if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)) |
1856 | nfs_confirm_seqid(&data->owner->so_seqid, 0); | |
6f926b5b | 1857 | } |
3e309914 | 1858 | data->rpc_done = 1; |
24ac23ab | 1859 | } |
6f926b5b | 1860 | |
24ac23ab TM |
1861 | static void nfs4_open_release(void *calldata) |
1862 | { | |
1863 | struct nfs4_opendata *data = calldata; | |
1864 | struct nfs4_state *state = NULL; | |
1865 | ||
1866 | /* If this request hasn't been cancelled, do nothing */ | |
1867 | if (data->cancelled == 0) | |
1868 | goto out_free; | |
1869 | /* In case of error, no cleanup! */ | |
3e309914 | 1870 | if (data->rpc_status != 0 || !data->rpc_done) |
24ac23ab TM |
1871 | goto out_free; |
1872 | /* In case we need an open_confirm, no cleanup! */ | |
1873 | if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) | |
1874 | goto out_free; | |
24ac23ab | 1875 | state = nfs4_opendata_to_nfs4_state(data); |
1b370bc2 | 1876 | if (!IS_ERR(state)) |
643168c2 | 1877 | nfs4_close_state(state, data->o_arg.fmode); |
24ac23ab | 1878 | out_free: |
c6d00e63 | 1879 | nfs4_opendata_put(data); |
24ac23ab TM |
1880 | } |
1881 | ||
1882 | static const struct rpc_call_ops nfs4_open_ops = { | |
1883 | .rpc_call_prepare = nfs4_open_prepare, | |
1884 | .rpc_call_done = nfs4_open_done, | |
1885 | .rpc_release = nfs4_open_release, | |
1886 | }; | |
1887 | ||
b257957e | 1888 | static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover) |
24ac23ab TM |
1889 | { |
1890 | struct inode *dir = data->dir->d_inode; | |
1891 | struct nfs_server *server = NFS_SERVER(dir); | |
1892 | struct nfs_openargs *o_arg = &data->o_arg; | |
1893 | struct nfs_openres *o_res = &data->o_res; | |
1894 | struct rpc_task *task; | |
5138fde0 TM |
1895 | struct rpc_message msg = { |
1896 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN], | |
1897 | .rpc_argp = o_arg, | |
1898 | .rpc_resp = o_res, | |
1899 | .rpc_cred = data->owner->so_cred, | |
1900 | }; | |
c970aa85 TM |
1901 | struct rpc_task_setup task_setup_data = { |
1902 | .rpc_client = server->client, | |
5138fde0 | 1903 | .rpc_message = &msg, |
c970aa85 TM |
1904 | .callback_ops = &nfs4_open_ops, |
1905 | .callback_data = data, | |
101070ca | 1906 | .workqueue = nfsiod_workqueue, |
c970aa85 TM |
1907 | .flags = RPC_TASK_ASYNC, |
1908 | }; | |
24ac23ab TM |
1909 | int status; |
1910 | ||
a9c92d6b | 1911 | nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1); |
c6d00e63 | 1912 | kref_get(&data->kref); |
3e309914 TM |
1913 | data->rpc_done = 0; |
1914 | data->rpc_status = 0; | |
2ced46c2 | 1915 | data->cancelled = 0; |
bdeca1b7 TM |
1916 | data->is_recover = 0; |
1917 | if (isrecover) { | |
8fe72bac | 1918 | nfs4_set_sequence_privileged(&o_arg->seq_args); |
bdeca1b7 TM |
1919 | data->is_recover = 1; |
1920 | } | |
c970aa85 | 1921 | task = rpc_run_task(&task_setup_data); |
b257957e AB |
1922 | if (IS_ERR(task)) |
1923 | return PTR_ERR(task); | |
1924 | status = nfs4_wait_for_completion_rpc_task(task); | |
1925 | if (status != 0) { | |
1926 | data->cancelled = 1; | |
1927 | smp_wmb(); | |
1928 | } else | |
1929 | status = data->rpc_status; | |
1930 | rpc_put_task(task); | |
1931 | ||
1932 | return status; | |
1933 | } | |
1934 | ||
1935 | static int _nfs4_recover_proc_open(struct nfs4_opendata *data) | |
1936 | { | |
1937 | struct inode *dir = data->dir->d_inode; | |
1938 | struct nfs_openres *o_res = &data->o_res; | |
1939 | int status; | |
1940 | ||
1941 | status = nfs4_run_open_task(data, 1); | |
1942 | if (status != 0 || !data->rpc_done) | |
1943 | return status; | |
1944 | ||
6926afd1 TM |
1945 | nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr); |
1946 | ||
b257957e AB |
1947 | if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { |
1948 | status = _nfs4_proc_open_confirm(data); | |
1949 | if (status != 0) | |
1950 | return status; | |
1951 | } | |
1952 | ||
1953 | return status; | |
1954 | } | |
1955 | ||
f3792d63 TM |
1956 | /* |
1957 | * Additional permission checks in order to distinguish between an | |
1958 | * open for read, and an open for execute. This works around the | |
1959 | * fact that NFSv4 OPEN treats read and execute permissions as being | |
1960 | * the same. | |
1961 | * Note that in the non-execute case, we want to turn off permission | |
1962 | * checking if we just created a new file (POSIX open() semantics). | |
1963 | */ | |
6168f62c WAA |
1964 | static int nfs4_opendata_access(struct rpc_cred *cred, |
1965 | struct nfs4_opendata *opendata, | |
f8d9a897 WAA |
1966 | struct nfs4_state *state, fmode_t fmode, |
1967 | int openflags) | |
6168f62c WAA |
1968 | { |
1969 | struct nfs_access_entry cache; | |
1970 | u32 mask; | |
1971 | ||
1972 | /* access call failed or for some reason the server doesn't | |
1973 | * support any access modes -- defer access call until later */ | |
1974 | if (opendata->o_res.access_supported == 0) | |
1975 | return 0; | |
1976 | ||
1977 | mask = 0; | |
f3792d63 TM |
1978 | /* |
1979 | * Use openflags to check for exec, because fmode won't | |
1980 | * always have FMODE_EXEC set when file open for exec. | |
1981 | */ | |
f8d9a897 WAA |
1982 | if (openflags & __FMODE_EXEC) { |
1983 | /* ONLY check for exec rights */ | |
1984 | mask = MAY_EXEC; | |
f3792d63 | 1985 | } else if ((fmode & FMODE_READ) && !opendata->file_created) |
f8d9a897 | 1986 | mask = MAY_READ; |
6168f62c WAA |
1987 | |
1988 | cache.cred = cred; | |
1989 | cache.jiffies = jiffies; | |
1990 | nfs_access_set_mask(&cache, opendata->o_res.access_result); | |
1991 | nfs_access_add_cache(state->inode, &cache); | |
1992 | ||
bbd3a8ee | 1993 | if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0) |
6168f62c WAA |
1994 | return 0; |
1995 | ||
1996 | /* even though OPEN succeeded, access is denied. Close the file */ | |
1997 | nfs4_close_state(state, fmode); | |
998f40b5 | 1998 | return -EACCES; |
6168f62c WAA |
1999 | } |
2000 | ||
b257957e AB |
2001 | /* |
2002 | * Note: On error, nfs4_proc_open will free the struct nfs4_opendata | |
2003 | */ | |
2004 | static int _nfs4_proc_open(struct nfs4_opendata *data) | |
2005 | { | |
2006 | struct inode *dir = data->dir->d_inode; | |
2007 | struct nfs_server *server = NFS_SERVER(dir); | |
2008 | struct nfs_openargs *o_arg = &data->o_arg; | |
2009 | struct nfs_openres *o_res = &data->o_res; | |
2010 | int status; | |
2011 | ||
2012 | status = nfs4_run_open_task(data, 0); | |
08ef7bd3 TM |
2013 | if (!data->rpc_done) |
2014 | return status; | |
2015 | if (status != 0) { | |
2016 | if (status == -NFS4ERR_BADNAME && | |
2017 | !(o_arg->open_flags & O_CREAT)) | |
2018 | return -ENOENT; | |
24ac23ab | 2019 | return status; |
08ef7bd3 | 2020 | } |
24ac23ab | 2021 | |
6926afd1 TM |
2022 | nfs_fattr_map_and_free_names(server, &data->f_attr); |
2023 | ||
5bc2afc2 | 2024 | if (o_arg->open_flags & O_CREAT) { |
56ae19f3 | 2025 | update_changeattr(dir, &o_res->cinfo); |
5bc2afc2 TM |
2026 | if (o_arg->open_flags & O_EXCL) |
2027 | data->file_created = 1; | |
2028 | else if (o_res->cinfo.before != o_res->cinfo.after) | |
2029 | data->file_created = 1; | |
2030 | } | |
0df5dd4a TM |
2031 | if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0) |
2032 | server->caps &= ~NFS_CAP_POSIX_LOCK; | |
1da177e4 | 2033 | if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { |
cdd4e68b | 2034 | status = _nfs4_proc_open_confirm(data); |
1da177e4 | 2035 | if (status != 0) |
24ac23ab | 2036 | return status; |
1da177e4 LT |
2037 | } |
2038 | if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) | |
8935ef66 | 2039 | nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label); |
24ac23ab | 2040 | return 0; |
1da177e4 LT |
2041 | } |
2042 | ||
d83217c1 AA |
2043 | static int nfs4_recover_expired_lease(struct nfs_server *server) |
2044 | { | |
2045 | return nfs4_client_recover_expired_lease(server->nfs_client); | |
2046 | } | |
2047 | ||
1da177e4 LT |
2048 | /* |
2049 | * OPEN_EXPIRED: | |
2050 | * reclaim state on the server after a network partition. | |
2051 | * Assumes caller holds the appropriate lock | |
2052 | */ | |
539cd03a | 2053 | static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) |
1da177e4 | 2054 | { |
e56e0b78 | 2055 | struct nfs4_opendata *opendata; |
864472e9 | 2056 | int ret; |
1da177e4 | 2057 | |
4a1c0893 | 2058 | opendata = nfs4_open_recoverdata_alloc(ctx, state, |
49f9a0fa | 2059 | NFS4_OPEN_CLAIM_FH); |
6f220ed5 TM |
2060 | if (IS_ERR(opendata)) |
2061 | return PTR_ERR(opendata); | |
864472e9 | 2062 | ret = nfs4_open_recover(opendata, state); |
35d05778 | 2063 | if (ret == -ESTALE) |
3d4ff43d | 2064 | d_drop(ctx->dentry); |
c6d00e63 | 2065 | nfs4_opendata_put(opendata); |
864472e9 | 2066 | return ret; |
1da177e4 LT |
2067 | } |
2068 | ||
a9ed2e25 | 2069 | static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) |
202b50dc | 2070 | { |
539cd03a | 2071 | struct nfs_server *server = NFS_SERVER(state->inode); |
202b50dc TM |
2072 | struct nfs4_exception exception = { }; |
2073 | int err; | |
2074 | ||
2075 | do { | |
539cd03a | 2076 | err = _nfs4_open_expired(ctx, state); |
42113a75 | 2077 | trace_nfs4_open_expired(ctx, 0, err); |
49f9a0fa TM |
2078 | if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) |
2079 | continue; | |
a9ed2e25 TM |
2080 | switch (err) { |
2081 | default: | |
2082 | goto out; | |
2083 | case -NFS4ERR_GRACE: | |
2084 | case -NFS4ERR_DELAY: | |
2085 | nfs4_handle_exception(server, err, &exception); | |
2086 | err = 0; | |
2087 | } | |
202b50dc | 2088 | } while (exception.retry); |
a9ed2e25 | 2089 | out: |
202b50dc TM |
2090 | return err; |
2091 | } | |
2092 | ||
1da177e4 LT |
2093 | static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) |
2094 | { | |
1da177e4 | 2095 | struct nfs_open_context *ctx; |
864472e9 | 2096 | int ret; |
1da177e4 | 2097 | |
864472e9 TM |
2098 | ctx = nfs4_state_find_open_context(state); |
2099 | if (IS_ERR(ctx)) | |
91876b13 | 2100 | return -EAGAIN; |
539cd03a | 2101 | ret = nfs4_do_open_expired(ctx, state); |
864472e9 TM |
2102 | put_nfs_open_context(ctx); |
2103 | return ret; | |
1da177e4 LT |
2104 | } |
2105 | ||
4dfd4f7a TM |
2106 | static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state) |
2107 | { | |
2108 | nfs_remove_bad_delegation(state->inode); | |
2109 | write_seqlock(&state->seqlock); | |
2110 | nfs4_stateid_copy(&state->stateid, &state->open_stateid); | |
2111 | write_sequnlock(&state->seqlock); | |
2112 | clear_bit(NFS_DELEGATED_STATE, &state->flags); | |
2113 | } | |
2114 | ||
2115 | static void nfs40_clear_delegation_stateid(struct nfs4_state *state) | |
2116 | { | |
2117 | if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL) | |
2118 | nfs_finish_clear_delegation_stateid(state); | |
2119 | } | |
2120 | ||
2121 | static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) | |
2122 | { | |
2123 | /* NFSv4.0 doesn't allow for delegation recovery on open expire */ | |
2124 | nfs40_clear_delegation_stateid(state); | |
2125 | return nfs4_open_expired(sp, state); | |
2126 | } | |
2127 | ||
f062eb6c | 2128 | #if defined(CONFIG_NFS_V4_1) |
0c116cad | 2129 | static void nfs41_check_delegation_stateid(struct nfs4_state *state) |
f062eb6c | 2130 | { |
f062eb6c | 2131 | struct nfs_server *server = NFS_SERVER(state->inode); |
0c116cad | 2132 | nfs4_stateid stateid; |
ab7cb0df | 2133 | struct nfs_delegation *delegation; |
0c116cad TM |
2134 | struct rpc_cred *cred; |
2135 | int status; | |
3e60ffdd | 2136 | |
ab7cb0df TM |
2137 | /* Get the delegation credential for use by test/free_stateid */ |
2138 | rcu_read_lock(); | |
2139 | delegation = rcu_dereference(NFS_I(state->inode)->delegation); | |
0c116cad | 2140 | if (delegation == NULL) { |
ab7cb0df | 2141 | rcu_read_unlock(); |
0c116cad TM |
2142 | return; |
2143 | } | |
2144 | ||
2145 | nfs4_stateid_copy(&stateid, &delegation->stateid); | |
2146 | cred = get_rpccred(delegation->cred); | |
2147 | rcu_read_unlock(); | |
2148 | status = nfs41_test_stateid(server, &stateid, cred); | |
2149 | trace_nfs4_test_delegation_stateid(state, NULL, status); | |
ab7cb0df | 2150 | |
3e60ffdd CL |
2151 | if (status != NFS_OK) { |
2152 | /* Free the stateid unless the server explicitly | |
2153 | * informs us the stateid is unrecognized. */ | |
2154 | if (status != -NFS4ERR_BAD_STATEID) | |
0c116cad TM |
2155 | nfs41_free_stateid(server, &stateid, cred); |
2156 | nfs_finish_clear_delegation_stateid(state); | |
3e60ffdd | 2157 | } |
ab7cb0df | 2158 | |
0c116cad | 2159 | put_rpccred(cred); |
3e60ffdd CL |
2160 | } |
2161 | ||
2162 | /** | |
2163 | * nfs41_check_open_stateid - possibly free an open stateid | |
2164 | * | |
2165 | * @state: NFSv4 state for an inode | |
2166 | * | |
2167 | * Returns NFS_OK if recovery for this stateid is now finished. | |
2168 | * Otherwise a negative NFS4ERR value is returned. | |
2169 | */ | |
2170 | static int nfs41_check_open_stateid(struct nfs4_state *state) | |
2171 | { | |
2172 | struct nfs_server *server = NFS_SERVER(state->inode); | |
fcb6d9c6 | 2173 | nfs4_stateid *stateid = &state->open_stateid; |
ab7cb0df | 2174 | struct rpc_cred *cred = state->owner->so_cred; |
3e60ffdd CL |
2175 | int status; |
2176 | ||
2177 | /* If a state reset has been done, test_stateid is unneeded */ | |
2178 | if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) && | |
2179 | (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) && | |
2180 | (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0)) | |
2181 | return -NFS4ERR_BAD_STATEID; | |
2182 | ||
ab7cb0df | 2183 | status = nfs41_test_stateid(server, stateid, cred); |
08cb47fa | 2184 | trace_nfs4_test_open_stateid(state, NULL, status); |
3e60ffdd CL |
2185 | if (status != NFS_OK) { |
2186 | /* Free the stateid unless the server explicitly | |
2187 | * informs us the stateid is unrecognized. */ | |
2188 | if (status != -NFS4ERR_BAD_STATEID) | |
ab7cb0df | 2189 | nfs41_free_stateid(server, stateid, cred); |
3e60ffdd CL |
2190 | |
2191 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); | |
2192 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); | |
2193 | clear_bit(NFS_O_RDWR_STATE, &state->flags); | |
fd068b20 | 2194 | clear_bit(NFS_OPEN_STATE, &state->flags); |
b01dd1d8 BS |
2195 | } |
2196 | return status; | |
2197 | } | |
2198 | ||
2199 | static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) | |
2200 | { | |
eb64cf96 | 2201 | int status; |
b01dd1d8 | 2202 | |
0c116cad | 2203 | nfs41_check_delegation_stateid(state); |
3e60ffdd | 2204 | status = nfs41_check_open_stateid(state); |
eb64cf96 CL |
2205 | if (status != NFS_OK) |
2206 | status = nfs4_open_expired(sp, state); | |
2207 | return status; | |
f062eb6c BS |
2208 | } |
2209 | #endif | |
2210 | ||
aa53ed54 JL |
2211 | /* |
2212 | * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-* | |
2213 | * fields corresponding to attributes that were used to store the verifier. | |
2214 | * Make sure we clobber those fields in the later setattr call | |
2215 | */ | |
2216 | static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr) | |
2217 | { | |
2218 | if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) && | |
2219 | !(sattr->ia_valid & ATTR_ATIME_SET)) | |
2220 | sattr->ia_valid |= ATTR_ATIME; | |
2221 | ||
2222 | if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) && | |
2223 | !(sattr->ia_valid & ATTR_MTIME_SET)) | |
2224 | sattr->ia_valid |= ATTR_MTIME; | |
2225 | } | |
2226 | ||
c21443c2 TM |
2227 | static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata, |
2228 | fmode_t fmode, | |
2229 | int flags, | |
3efb9722 | 2230 | struct nfs_open_context *ctx) |
c21443c2 TM |
2231 | { |
2232 | struct nfs4_state_owner *sp = opendata->owner; | |
2233 | struct nfs_server *server = sp->so_server; | |
275bb307 | 2234 | struct dentry *dentry; |
c21443c2 TM |
2235 | struct nfs4_state *state; |
2236 | unsigned int seq; | |
2237 | int ret; | |
2238 | ||
2239 | seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); | |
2240 | ||
2241 | ret = _nfs4_proc_open(opendata); | |
dca78001 | 2242 | if (ret != 0) |
c21443c2 TM |
2243 | goto out; |
2244 | ||
2245 | state = nfs4_opendata_to_nfs4_state(opendata); | |
2246 | ret = PTR_ERR(state); | |
2247 | if (IS_ERR(state)) | |
2248 | goto out; | |
2249 | if (server->caps & NFS_CAP_POSIX_LOCK) | |
2250 | set_bit(NFS_STATE_POSIX_LOCKS, &state->flags); | |
2251 | ||
275bb307 TM |
2252 | dentry = opendata->dentry; |
2253 | if (dentry->d_inode == NULL) { | |
2254 | /* FIXME: Is this d_drop() ever needed? */ | |
2255 | d_drop(dentry); | |
2256 | dentry = d_add_unique(dentry, igrab(state->inode)); | |
2257 | if (dentry == NULL) { | |
2258 | dentry = opendata->dentry; | |
2259 | } else if (dentry != ctx->dentry) { | |
2260 | dput(ctx->dentry); | |
2261 | ctx->dentry = dget(dentry); | |
2262 | } | |
2263 | nfs_set_verifier(dentry, | |
2264 | nfs_save_change_attribute(opendata->dir->d_inode)); | |
2265 | } | |
2266 | ||
c21443c2 TM |
2267 | ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags); |
2268 | if (ret != 0) | |
2269 | goto out; | |
2270 | ||
3efb9722 | 2271 | ctx->state = state; |
c45ffdd2 TM |
2272 | if (dentry->d_inode == state->inode) { |
2273 | nfs_inode_attach_open_context(ctx); | |
2274 | if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) | |
2275 | nfs4_schedule_stateid_recovery(server, state); | |
2276 | } | |
c21443c2 TM |
2277 | out: |
2278 | return ret; | |
2279 | } | |
2280 | ||
1da177e4 | 2281 | /* |
24ac23ab | 2282 | * Returns a referenced nfs4_state |
1da177e4 | 2283 | */ |
82be417a | 2284 | static int _nfs4_do_open(struct inode *dir, |
4197a055 | 2285 | struct nfs_open_context *ctx, |
82be417a AA |
2286 | int flags, |
2287 | struct iattr *sattr, | |
5bc2afc2 TM |
2288 | struct nfs4_label *label, |
2289 | int *opened) | |
1da177e4 LT |
2290 | { |
2291 | struct nfs4_state_owner *sp; | |
2292 | struct nfs4_state *state = NULL; | |
2293 | struct nfs_server *server = NFS_SERVER(dir); | |
e56e0b78 | 2294 | struct nfs4_opendata *opendata; |
4197a055 TM |
2295 | struct dentry *dentry = ctx->dentry; |
2296 | struct rpc_cred *cred = ctx->cred; | |
2297 | struct nfs4_threshold **ctx_th = &ctx->mdsthreshold; | |
2298 | fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC); | |
49f9a0fa | 2299 | enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL; |
1775fd3e | 2300 | struct nfs4_label *olabel = NULL; |
aac00a8d | 2301 | int status; |
1da177e4 LT |
2302 | |
2303 | /* Protect against reboot recovery conflicts */ | |
1da177e4 | 2304 | status = -ENOMEM; |
d1e284d5 TM |
2305 | sp = nfs4_get_state_owner(server, cred, GFP_KERNEL); |
2306 | if (sp == NULL) { | |
1da177e4 LT |
2307 | dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n"); |
2308 | goto out_err; | |
2309 | } | |
58d9714a TM |
2310 | status = nfs4_recover_expired_lease(server); |
2311 | if (status != 0) | |
b4454fe1 | 2312 | goto err_put_state_owner; |
82a2c1b7 AV |
2313 | if (dentry->d_inode != NULL) |
2314 | nfs4_return_incompatible_delegation(dentry->d_inode, fmode); | |
58d9714a | 2315 | status = -ENOMEM; |
49f9a0fa TM |
2316 | if (dentry->d_inode) |
2317 | claim = NFS4_OPEN_CLAIM_FH; | |
4a1c0893 | 2318 | opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, |
1775fd3e | 2319 | label, claim, GFP_KERNEL); |
e56e0b78 | 2320 | if (opendata == NULL) |
95d35cb4 | 2321 | goto err_put_state_owner; |
1da177e4 | 2322 | |
14c43f76 DQ |
2323 | if (label) { |
2324 | olabel = nfs4_label_alloc(server, GFP_KERNEL); | |
2325 | if (IS_ERR(olabel)) { | |
2326 | status = PTR_ERR(olabel); | |
2327 | goto err_opendata_put; | |
2328 | } | |
2329 | } | |
2330 | ||
e911b815 TM |
2331 | if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) { |
2332 | if (!opendata->f_attr.mdsthreshold) { | |
2333 | opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc(); | |
2334 | if (!opendata->f_attr.mdsthreshold) | |
2335 | goto err_free_label; | |
2336 | } | |
1549210f | 2337 | opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0]; |
82be417a | 2338 | } |
82a2c1b7 AV |
2339 | if (dentry->d_inode != NULL) |
2340 | opendata->state = nfs4_get_open_state(dentry->d_inode, sp); | |
aac00a8d | 2341 | |
3efb9722 | 2342 | status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx); |
6168f62c | 2343 | if (status != 0) |
14c43f76 | 2344 | goto err_free_label; |
3efb9722 | 2345 | state = ctx->state; |
6168f62c | 2346 | |
549b19cc TM |
2347 | if ((opendata->o_arg.open_flags & O_EXCL) && |
2348 | (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) { | |
0ab64e0e TM |
2349 | nfs4_exclusive_attrset(opendata, sattr); |
2350 | ||
2351 | nfs_fattr_init(opendata->o_res.f_attr); | |
2352 | status = nfs4_do_setattr(state->inode, cred, | |
2353 | opendata->o_res.f_attr, sattr, | |
1775fd3e DQ |
2354 | state, label, olabel); |
2355 | if (status == 0) { | |
0ab64e0e | 2356 | nfs_setattr_update_inode(state->inode, sattr); |
1775fd3e | 2357 | nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr); |
aa9c2669 | 2358 | nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel); |
1775fd3e | 2359 | } |
0ab64e0e | 2360 | } |
5bc2afc2 TM |
2361 | if (opendata->file_created) |
2362 | *opened |= FILE_CREATED; | |
82be417a | 2363 | |
e911b815 | 2364 | if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) { |
82be417a | 2365 | *ctx_th = opendata->f_attr.mdsthreshold; |
e911b815 TM |
2366 | opendata->f_attr.mdsthreshold = NULL; |
2367 | } | |
82be417a | 2368 | |
14c43f76 DQ |
2369 | nfs4_label_free(olabel); |
2370 | ||
c6d00e63 | 2371 | nfs4_opendata_put(opendata); |
1da177e4 | 2372 | nfs4_put_state_owner(sp); |
1da177e4 | 2373 | return 0; |
14c43f76 DQ |
2374 | err_free_label: |
2375 | nfs4_label_free(olabel); | |
c6d00e63 TM |
2376 | err_opendata_put: |
2377 | nfs4_opendata_put(opendata); | |
e56e0b78 TM |
2378 | err_put_state_owner: |
2379 | nfs4_put_state_owner(sp); | |
1da177e4 | 2380 | out_err: |
1da177e4 LT |
2381 | return status; |
2382 | } | |
2383 | ||
2384 | ||
82be417a | 2385 | static struct nfs4_state *nfs4_do_open(struct inode *dir, |
4197a055 | 2386 | struct nfs_open_context *ctx, |
82be417a AA |
2387 | int flags, |
2388 | struct iattr *sattr, | |
5bc2afc2 TM |
2389 | struct nfs4_label *label, |
2390 | int *opened) | |
1da177e4 | 2391 | { |
49f9a0fa | 2392 | struct nfs_server *server = NFS_SERVER(dir); |
1da177e4 LT |
2393 | struct nfs4_exception exception = { }; |
2394 | struct nfs4_state *res; | |
2395 | int status; | |
2396 | ||
2397 | do { | |
5bc2afc2 | 2398 | status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened); |
3efb9722 | 2399 | res = ctx->state; |
42113a75 | 2400 | trace_nfs4_open_file(ctx, flags, status); |
1da177e4 LT |
2401 | if (status == 0) |
2402 | break; | |
2403 | /* NOTE: BAD_SEQID means the server and client disagree about the | |
2404 | * book-keeping w.r.t. state-changing operations | |
2405 | * (OPEN/CLOSE/LOCK/LOCKU...) | |
2406 | * It is actually a sign of a bug on the client or on the server. | |
2407 | * | |
2408 | * If we receive a BAD_SEQID error in the particular case of | |
cee54fc9 | 2409 | * doing an OPEN, we assume that nfs_increment_open_seqid() will |
1da177e4 LT |
2410 | * have unhashed the old state_owner for us, and that we can |
2411 | * therefore safely retry using a new one. We should still warn | |
2412 | * the user though... | |
2413 | */ | |
2414 | if (status == -NFS4ERR_BAD_SEQID) { | |
9a3ba432 | 2415 | pr_warn_ratelimited("NFS: v4 server %s " |
6f43ddcc TM |
2416 | " returned a bad sequence-id error!\n", |
2417 | NFS_SERVER(dir)->nfs_client->cl_hostname); | |
1da177e4 LT |
2418 | exception.retry = 1; |
2419 | continue; | |
2420 | } | |
550f5747 TM |
2421 | /* |
2422 | * BAD_STATEID on OPEN means that the server cancelled our | |
2423 | * state before it received the OPEN_CONFIRM. | |
2424 | * Recover by retrying the request as per the discussion | |
2425 | * on Page 181 of RFC3530. | |
2426 | */ | |
2427 | if (status == -NFS4ERR_BAD_STATEID) { | |
2428 | exception.retry = 1; | |
2429 | continue; | |
2430 | } | |
aac00a8d TM |
2431 | if (status == -EAGAIN) { |
2432 | /* We must have found a delegation */ | |
2433 | exception.retry = 1; | |
2434 | continue; | |
2435 | } | |
49f9a0fa TM |
2436 | if (nfs4_clear_cap_atomic_open_v1(server, status, &exception)) |
2437 | continue; | |
2438 | res = ERR_PTR(nfs4_handle_exception(server, | |
1da177e4 LT |
2439 | status, &exception)); |
2440 | } while (exception.retry); | |
2441 | return res; | |
2442 | } | |
2443 | ||
659bfcd6 TM |
2444 | static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred, |
2445 | struct nfs_fattr *fattr, struct iattr *sattr, | |
1775fd3e DQ |
2446 | struct nfs4_state *state, struct nfs4_label *ilabel, |
2447 | struct nfs4_label *olabel) | |
1da177e4 | 2448 | { |
3e4f6290 | 2449 | struct nfs_server *server = NFS_SERVER(inode); |
1da177e4 | 2450 | struct nfs_setattrargs arg = { |
3e4f6290 | 2451 | .fh = NFS_FH(inode), |
1da177e4 LT |
2452 | .iap = sattr, |
2453 | .server = server, | |
2454 | .bitmask = server->attr_bitmask, | |
1775fd3e | 2455 | .label = ilabel, |
1da177e4 LT |
2456 | }; |
2457 | struct nfs_setattrres res = { | |
2458 | .fattr = fattr, | |
1775fd3e | 2459 | .label = olabel, |
1da177e4 LT |
2460 | .server = server, |
2461 | }; | |
2462 | struct rpc_message msg = { | |
659bfcd6 TM |
2463 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], |
2464 | .rpc_argp = &arg, | |
2465 | .rpc_resp = &res, | |
2466 | .rpc_cred = cred, | |
1da177e4 | 2467 | }; |
26e976a8 | 2468 | unsigned long timestamp = jiffies; |
ee3ae84e TM |
2469 | fmode_t fmode; |
2470 | bool truncate; | |
65e4308d | 2471 | int status; |
1da177e4 | 2472 | |
aa9c2669 DQ |
2473 | arg.bitmask = nfs4_bitmask(server, ilabel); |
2474 | if (ilabel) | |
2475 | arg.bitmask = nfs4_bitmask(server, olabel); | |
2476 | ||
0e574af1 | 2477 | nfs_fattr_init(fattr); |
1da177e4 | 2478 | |
ee3ae84e TM |
2479 | /* Servers should only apply open mode checks for file size changes */ |
2480 | truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false; | |
2481 | fmode = truncate ? FMODE_WRITE : FMODE_READ; | |
2482 | ||
2483 | if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) { | |
2484 | /* Use that stateid */ | |
0418dae1 | 2485 | } else if (truncate && state != NULL) { |
2a369153 TM |
2486 | struct nfs_lockowner lockowner = { |
2487 | .l_owner = current->files, | |
2488 | .l_pid = current->tgid, | |
2489 | }; | |
0418dae1 TM |
2490 | if (!nfs4_valid_open_stateid(state)) |
2491 | return -EBADF; | |
2492 | if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE, | |
2493 | &lockowner) == -EIO) | |
2494 | return -EBADF; | |
08e9eac4 | 2495 | } else |
f597c537 | 2496 | nfs4_stateid_copy(&arg.stateid, &zero_stateid); |
1da177e4 | 2497 | |
7c513058 | 2498 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
26e976a8 TM |
2499 | if (status == 0 && state != NULL) |
2500 | renew_lease(server, timestamp); | |
65e4308d | 2501 | return status; |
1da177e4 LT |
2502 | } |
2503 | ||
659bfcd6 TM |
2504 | static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred, |
2505 | struct nfs_fattr *fattr, struct iattr *sattr, | |
1775fd3e DQ |
2506 | struct nfs4_state *state, struct nfs4_label *ilabel, |
2507 | struct nfs4_label *olabel) | |
1da177e4 | 2508 | { |
3e4f6290 | 2509 | struct nfs_server *server = NFS_SERVER(inode); |
a1d0b5ee TM |
2510 | struct nfs4_exception exception = { |
2511 | .state = state, | |
3114ea7a | 2512 | .inode = inode, |
a1d0b5ee | 2513 | }; |
1da177e4 LT |
2514 | int err; |
2515 | do { | |
1775fd3e | 2516 | err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel); |
c1578b76 | 2517 | trace_nfs4_setattr(inode, err); |
451146be TM |
2518 | switch (err) { |
2519 | case -NFS4ERR_OPENMODE: | |
721ccfb7 TM |
2520 | if (!(sattr->ia_valid & ATTR_SIZE)) { |
2521 | pr_warn_once("NFSv4: server %s is incorrectly " | |
2522 | "applying open mode checks to " | |
2523 | "a SETATTR that is not " | |
2524 | "changing file size.\n", | |
2525 | server->nfs_client->cl_hostname); | |
2526 | } | |
451146be TM |
2527 | if (state && !(state->state & FMODE_WRITE)) { |
2528 | err = -EBADF; | |
2529 | if (sattr->ia_valid & ATTR_OPEN) | |
2530 | err = -EACCES; | |
2531 | goto out; | |
2532 | } | |
2533 | } | |
2534 | err = nfs4_handle_exception(server, err, &exception); | |
1da177e4 | 2535 | } while (exception.retry); |
451146be | 2536 | out: |
1da177e4 LT |
2537 | return err; |
2538 | } | |
2539 | ||
2540 | struct nfs4_closedata { | |
2541 | struct inode *inode; | |
2542 | struct nfs4_state *state; | |
2543 | struct nfs_closeargs arg; | |
2544 | struct nfs_closeres res; | |
516a6af6 | 2545 | struct nfs_fattr fattr; |
26e976a8 | 2546 | unsigned long timestamp; |
f7e8917a FI |
2547 | bool roc; |
2548 | u32 roc_barrier; | |
1da177e4 LT |
2549 | }; |
2550 | ||
963d8fe5 | 2551 | static void nfs4_free_closedata(void *data) |
9512135d | 2552 | { |
963d8fe5 TM |
2553 | struct nfs4_closedata *calldata = data; |
2554 | struct nfs4_state_owner *sp = calldata->state->owner; | |
643168c2 | 2555 | struct super_block *sb = calldata->state->inode->i_sb; |
9512135d | 2556 | |
f7e8917a FI |
2557 | if (calldata->roc) |
2558 | pnfs_roc_release(calldata->state->inode); | |
9512135d TM |
2559 | nfs4_put_open_state(calldata->state); |
2560 | nfs_free_seqid(calldata->arg.seqid); | |
9512135d | 2561 | nfs4_put_state_owner(sp); |
322b2b90 | 2562 | nfs_sb_deactive(sb); |
9512135d TM |
2563 | kfree(calldata); |
2564 | } | |
2565 | ||
963d8fe5 | 2566 | static void nfs4_close_done(struct rpc_task *task, void *data) |
1da177e4 | 2567 | { |
963d8fe5 | 2568 | struct nfs4_closedata *calldata = data; |
1da177e4 | 2569 | struct nfs4_state *state = calldata->state; |
1da177e4 | 2570 | struct nfs_server *server = NFS_SERVER(calldata->inode); |
412f6c4c | 2571 | nfs4_stateid *res_stateid = NULL; |
1da177e4 | 2572 | |
a3ca5651 | 2573 | dprintk("%s: begin!\n", __func__); |
14516c3a TM |
2574 | if (!nfs4_sequence_done(task, &calldata->res.seq_res)) |
2575 | return; | |
42113a75 | 2576 | trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status); |
1da177e4 LT |
2577 | /* hmm. we are done with the inode, and in the process of freeing |
2578 | * the state_owner. we keep this around to process errors | |
2579 | */ | |
1da177e4 LT |
2580 | switch (task->tk_status) { |
2581 | case 0: | |
412f6c4c TM |
2582 | res_stateid = &calldata->res.stateid; |
2583 | if (calldata->arg.fmode == 0 && calldata->roc) | |
f7e8917a FI |
2584 | pnfs_roc_set_barrier(state->inode, |
2585 | calldata->roc_barrier); | |
26e976a8 | 2586 | renew_lease(server, calldata->timestamp); |
412f6c4c | 2587 | break; |
69794ad7 | 2588 | case -NFS4ERR_ADMIN_REVOKED: |
1da177e4 | 2589 | case -NFS4ERR_STALE_STATEID: |
9e33bed5 TM |
2590 | case -NFS4ERR_OLD_STATEID: |
2591 | case -NFS4ERR_BAD_STATEID: | |
1da177e4 | 2592 | case -NFS4ERR_EXPIRED: |
dc0b027d | 2593 | if (calldata->arg.fmode == 0) |
9e33bed5 | 2594 | break; |
1da177e4 | 2595 | default: |
8478eaa1 | 2596 | if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) { |
72211dbe | 2597 | rpc_restart_call_prepare(task); |
69794ad7 TM |
2598 | goto out_release; |
2599 | } | |
1da177e4 | 2600 | } |
412f6c4c | 2601 | nfs_clear_open_stateid(state, res_stateid, calldata->arg.fmode); |
69794ad7 | 2602 | out_release: |
72211dbe | 2603 | nfs_release_seqid(calldata->arg.seqid); |
516a6af6 | 2604 | nfs_refresh_inode(calldata->inode, calldata->res.fattr); |
a3ca5651 | 2605 | dprintk("%s: done, ret = %d!\n", __func__, task->tk_status); |
1da177e4 LT |
2606 | } |
2607 | ||
4ce70ada | 2608 | static void nfs4_close_prepare(struct rpc_task *task, void *data) |
1da177e4 | 2609 | { |
4ce70ada | 2610 | struct nfs4_closedata *calldata = data; |
9512135d | 2611 | struct nfs4_state *state = calldata->state; |
7fdab069 | 2612 | struct inode *inode = calldata->inode; |
aee7af35 | 2613 | bool is_rdonly, is_wronly, is_rdwr; |
88069f77 | 2614 | int call_close = 0; |
9512135d | 2615 | |
a3ca5651 | 2616 | dprintk("%s: begin!\n", __func__); |
963d8fe5 | 2617 | if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) |
c8da19b9 | 2618 | goto out_wait; |
003707c7 | 2619 | |
88069f77 | 2620 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE]; |
4cecb76f | 2621 | spin_lock(&state->owner->so_lock); |
aee7af35 TM |
2622 | is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags); |
2623 | is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags); | |
2624 | is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags); | |
003707c7 | 2625 | /* Calculate the change in open mode */ |
cd9288ff | 2626 | calldata->arg.fmode = 0; |
e7616923 | 2627 | if (state->n_rdwr == 0) { |
cd9288ff TM |
2628 | if (state->n_rdonly == 0) |
2629 | call_close |= is_rdonly; | |
2630 | else if (is_rdonly) | |
2631 | calldata->arg.fmode |= FMODE_READ; | |
2632 | if (state->n_wronly == 0) | |
2633 | call_close |= is_wronly; | |
2634 | else if (is_wronly) | |
2635 | calldata->arg.fmode |= FMODE_WRITE; | |
2636 | } else if (is_rdwr) | |
2637 | calldata->arg.fmode |= FMODE_READ|FMODE_WRITE; | |
2638 | ||
2639 | if (calldata->arg.fmode == 0) | |
2640 | call_close |= is_rdwr; | |
2641 | ||
5d422301 TM |
2642 | if (!nfs4_valid_open_stateid(state)) |
2643 | call_close = 0; | |
4cecb76f | 2644 | spin_unlock(&state->owner->so_lock); |
88069f77 TM |
2645 | |
2646 | if (!call_close) { | |
963d8fe5 | 2647 | /* Note: exit _without_ calling nfs4_close_done */ |
c8da19b9 | 2648 | goto out_no_action; |
9512135d | 2649 | } |
88069f77 | 2650 | |
f7e8917a | 2651 | if (calldata->arg.fmode == 0) { |
88069f77 | 2652 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE]; |
f7e8917a | 2653 | if (calldata->roc && |
b9536ad5 AA |
2654 | pnfs_roc_drain(inode, &calldata->roc_barrier, task)) { |
2655 | nfs_release_seqid(calldata->arg.seqid); | |
c8da19b9 | 2656 | goto out_wait; |
b9536ad5 | 2657 | } |
f7e8917a | 2658 | } |
88069f77 | 2659 | |
516a6af6 | 2660 | nfs_fattr_init(calldata->res.fattr); |
26e976a8 | 2661 | calldata->timestamp = jiffies; |
7fdab069 | 2662 | if (nfs4_setup_sequence(NFS_SERVER(inode), |
9d12b216 TM |
2663 | &calldata->arg.seq_args, |
2664 | &calldata->res.seq_res, | |
2240a9e2 TM |
2665 | task) != 0) |
2666 | nfs_release_seqid(calldata->arg.seqid); | |
a3ca5651 | 2667 | dprintk("%s: done!\n", __func__); |
c8da19b9 TM |
2668 | return; |
2669 | out_no_action: | |
2670 | task->tk_action = NULL; | |
2671 | out_wait: | |
2672 | nfs4_sequence_done(task, &calldata->res.seq_res); | |
1da177e4 LT |
2673 | } |
2674 | ||
963d8fe5 | 2675 | static const struct rpc_call_ops nfs4_close_ops = { |
4ce70ada | 2676 | .rpc_call_prepare = nfs4_close_prepare, |
963d8fe5 TM |
2677 | .rpc_call_done = nfs4_close_done, |
2678 | .rpc_release = nfs4_free_closedata, | |
2679 | }; | |
2680 | ||
fe08c546 PT |
2681 | static bool nfs4_state_has_opener(struct nfs4_state *state) |
2682 | { | |
2683 | /* first check existing openers */ | |
2684 | if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 && | |
2685 | state->n_rdonly != 0) | |
2686 | return true; | |
2687 | ||
2688 | if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 && | |
2689 | state->n_wronly != 0) | |
2690 | return true; | |
2691 | ||
2692 | if (test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 && | |
2693 | state->n_rdwr != 0) | |
2694 | return true; | |
2695 | ||
2696 | return false; | |
2697 | } | |
2698 | ||
2699 | static bool nfs4_roc(struct inode *inode) | |
2700 | { | |
2701 | struct nfs_inode *nfsi = NFS_I(inode); | |
2702 | struct nfs_open_context *ctx; | |
2703 | struct nfs4_state *state; | |
2704 | ||
2705 | spin_lock(&inode->i_lock); | |
2706 | list_for_each_entry(ctx, &nfsi->open_files, list) { | |
2707 | state = ctx->state; | |
2708 | if (state == NULL) | |
2709 | continue; | |
2710 | if (nfs4_state_has_opener(state)) { | |
2711 | spin_unlock(&inode->i_lock); | |
2712 | return false; | |
2713 | } | |
2714 | } | |
2715 | spin_unlock(&inode->i_lock); | |
2716 | ||
2717 | if (nfs4_check_delegation(inode, FMODE_READ)) | |
2718 | return false; | |
2719 | ||
2720 | return pnfs_roc(inode); | |
2721 | } | |
2722 | ||
1da177e4 LT |
2723 | /* |
2724 | * It is possible for data to be read/written from a mem-mapped file | |
2725 | * after the sys_close call (which hits the vfs layer as a flush). | |
2726 | * This means that we can't safely call nfsv4 close on a file until | |
2727 | * the inode is cleared. This in turn means that we are not good | |
2728 | * NFSv4 citizens - we do not indicate to the server to update the file's | |
2729 | * share state even when we are done with one of the three share | |
2730 | * stateid's in the inode. | |
2731 | * | |
2732 | * NOTE: Caller must be holding the sp->so_owner semaphore! | |
2733 | */ | |
1f7977c1 | 2734 | int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait) |
1da177e4 | 2735 | { |
4a35bd41 | 2736 | struct nfs_server *server = NFS_SERVER(state->inode); |
1da177e4 | 2737 | struct nfs4_closedata *calldata; |
b39e625b TM |
2738 | struct nfs4_state_owner *sp = state->owner; |
2739 | struct rpc_task *task; | |
5138fde0 TM |
2740 | struct rpc_message msg = { |
2741 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE], | |
2742 | .rpc_cred = state->owner->so_cred, | |
2743 | }; | |
c970aa85 TM |
2744 | struct rpc_task_setup task_setup_data = { |
2745 | .rpc_client = server->client, | |
5138fde0 | 2746 | .rpc_message = &msg, |
c970aa85 | 2747 | .callback_ops = &nfs4_close_ops, |
101070ca | 2748 | .workqueue = nfsiod_workqueue, |
c970aa85 TM |
2749 | .flags = RPC_TASK_ASYNC, |
2750 | }; | |
9512135d | 2751 | int status = -ENOMEM; |
1da177e4 | 2752 | |
fa940720 WAA |
2753 | nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP, |
2754 | &task_setup_data.rpc_client, &msg); | |
2755 | ||
8535b2be | 2756 | calldata = kzalloc(sizeof(*calldata), gfp_mask); |
1da177e4 | 2757 | if (calldata == NULL) |
9512135d | 2758 | goto out; |
a9c92d6b | 2759 | nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1); |
4a35bd41 | 2760 | calldata->inode = state->inode; |
1da177e4 | 2761 | calldata->state = state; |
4a35bd41 | 2762 | calldata->arg.fh = NFS_FH(state->inode); |
003707c7 | 2763 | calldata->arg.stateid = &state->open_stateid; |
1da177e4 | 2764 | /* Serialization for the sequence id */ |
8535b2be | 2765 | calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask); |
9512135d TM |
2766 | if (calldata->arg.seqid == NULL) |
2767 | goto out_free_calldata; | |
dc0b027d | 2768 | calldata->arg.fmode = 0; |
a65318bf | 2769 | calldata->arg.bitmask = server->cache_consistency_bitmask; |
516a6af6 | 2770 | calldata->res.fattr = &calldata->fattr; |
c1d51931 | 2771 | calldata->res.seqid = calldata->arg.seqid; |
516a6af6 | 2772 | calldata->res.server = server; |
fe08c546 | 2773 | calldata->roc = nfs4_roc(state->inode); |
643168c2 | 2774 | nfs_sb_active(calldata->inode->i_sb); |
9512135d | 2775 | |
1174dd1f TM |
2776 | msg.rpc_argp = &calldata->arg; |
2777 | msg.rpc_resp = &calldata->res; | |
c970aa85 TM |
2778 | task_setup_data.callback_data = calldata; |
2779 | task = rpc_run_task(&task_setup_data); | |
b39e625b TM |
2780 | if (IS_ERR(task)) |
2781 | return PTR_ERR(task); | |
a49c3c77 TM |
2782 | status = 0; |
2783 | if (wait) | |
2784 | status = rpc_wait_for_completion_task(task); | |
b39e625b | 2785 | rpc_put_task(task); |
a49c3c77 | 2786 | return status; |
9512135d TM |
2787 | out_free_calldata: |
2788 | kfree(calldata); | |
2789 | out: | |
b39e625b TM |
2790 | nfs4_put_open_state(state); |
2791 | nfs4_put_state_owner(sp); | |
9512135d | 2792 | return status; |
1da177e4 LT |
2793 | } |
2794 | ||
2b484297 | 2795 | static struct inode * |
5bc2afc2 TM |
2796 | nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, |
2797 | int open_flags, struct iattr *attr, int *opened) | |
1da177e4 | 2798 | { |
1da177e4 | 2799 | struct nfs4_state *state; |
aa9c2669 DQ |
2800 | struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL; |
2801 | ||
2802 | label = nfs4_label_init_security(dir, ctx->dentry, attr, &l); | |
1da177e4 | 2803 | |
565277f6 | 2804 | /* Protect against concurrent sillydeletes */ |
5bc2afc2 | 2805 | state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened); |
aa9c2669 DQ |
2806 | |
2807 | nfs4_label_release_security(label); | |
2808 | ||
f46e0bd3 TM |
2809 | if (IS_ERR(state)) |
2810 | return ERR_CAST(state); | |
275bb307 | 2811 | return state->inode; |
1da177e4 LT |
2812 | } |
2813 | ||
1185a552 | 2814 | static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync) |
7fe5c398 TM |
2815 | { |
2816 | if (ctx->state == NULL) | |
2817 | return; | |
2818 | if (is_sync) | |
643168c2 | 2819 | nfs4_close_sync(ctx->state, ctx->mode); |
7fe5c398 | 2820 | else |
643168c2 | 2821 | nfs4_close_state(ctx->state, ctx->mode); |
7fe5c398 | 2822 | } |
1da177e4 | 2823 | |
b944dba3 TM |
2824 | #define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL) |
2825 | #define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL) | |
999e5683 | 2826 | #define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL) |
b944dba3 | 2827 | |
1da177e4 LT |
2828 | static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) |
2829 | { | |
43652ad5 BH |
2830 | struct nfs4_server_caps_arg args = { |
2831 | .fhandle = fhandle, | |
2832 | }; | |
1da177e4 LT |
2833 | struct nfs4_server_caps_res res = {}; |
2834 | struct rpc_message msg = { | |
2835 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS], | |
43652ad5 | 2836 | .rpc_argp = &args, |
1da177e4 LT |
2837 | .rpc_resp = &res, |
2838 | }; | |
2839 | int status; | |
2840 | ||
7c513058 | 2841 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 | 2842 | if (status == 0) { |
b944dba3 TM |
2843 | /* Sanity check the server answers */ |
2844 | switch (server->nfs_client->cl_minorversion) { | |
2845 | case 0: | |
2846 | res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK; | |
2847 | res.attr_bitmask[2] = 0; | |
2848 | break; | |
2849 | case 1: | |
2850 | res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK; | |
2851 | break; | |
2852 | case 2: | |
2853 | res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK; | |
2854 | } | |
1da177e4 | 2855 | memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask)); |
62ab460c TM |
2856 | server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS| |
2857 | NFS_CAP_SYMLINKS|NFS_CAP_FILEID| | |
2858 | NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER| | |
2859 | NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME| | |
b944dba3 TM |
2860 | NFS_CAP_CTIME|NFS_CAP_MTIME| |
2861 | NFS_CAP_SECURITY_LABEL); | |
7dd7d959 MN |
2862 | if (res.attr_bitmask[0] & FATTR4_WORD0_ACL && |
2863 | res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL) | |
1da177e4 LT |
2864 | server->caps |= NFS_CAP_ACLS; |
2865 | if (res.has_links != 0) | |
2866 | server->caps |= NFS_CAP_HARDLINKS; | |
2867 | if (res.has_symlinks != 0) | |
2868 | server->caps |= NFS_CAP_SYMLINKS; | |
62ab460c TM |
2869 | if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID) |
2870 | server->caps |= NFS_CAP_FILEID; | |
2871 | if (res.attr_bitmask[1] & FATTR4_WORD1_MODE) | |
2872 | server->caps |= NFS_CAP_MODE; | |
2873 | if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS) | |
2874 | server->caps |= NFS_CAP_NLINK; | |
2875 | if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER) | |
2876 | server->caps |= NFS_CAP_OWNER; | |
2877 | if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP) | |
2878 | server->caps |= NFS_CAP_OWNER_GROUP; | |
2879 | if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS) | |
2880 | server->caps |= NFS_CAP_ATIME; | |
2881 | if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA) | |
2882 | server->caps |= NFS_CAP_CTIME; | |
2883 | if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY) | |
2884 | server->caps |= NFS_CAP_MTIME; | |
aa9c2669 DQ |
2885 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
2886 | if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL) | |
2887 | server->caps |= NFS_CAP_SECURITY_LABEL; | |
2888 | #endif | |
2889 | memcpy(server->attr_bitmask_nl, res.attr_bitmask, | |
2890 | sizeof(server->attr_bitmask)); | |
b944dba3 | 2891 | server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL; |
62ab460c | 2892 | |
a65318bf TM |
2893 | memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask)); |
2894 | server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE; | |
2895 | server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY; | |
b944dba3 | 2896 | server->cache_consistency_bitmask[2] = 0; |
1da177e4 | 2897 | server->acl_bitmask = res.acl_bitmask; |
264e6351 | 2898 | server->fh_expire_type = res.fh_expire_type; |
1da177e4 | 2899 | } |
cccef3b9 | 2900 | |
1da177e4 LT |
2901 | return status; |
2902 | } | |
2903 | ||
55a97593 | 2904 | int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) |
1da177e4 LT |
2905 | { |
2906 | struct nfs4_exception exception = { }; | |
2907 | int err; | |
2908 | do { | |
2909 | err = nfs4_handle_exception(server, | |
2910 | _nfs4_server_capabilities(server, fhandle), | |
2911 | &exception); | |
2912 | } while (exception.retry); | |
2913 | return err; | |
2914 | } | |
2915 | ||
2916 | static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, | |
2917 | struct nfs_fsinfo *info) | |
2918 | { | |
aa9c2669 | 2919 | u32 bitmask[3]; |
1da177e4 | 2920 | struct nfs4_lookup_root_arg args = { |
aa9c2669 | 2921 | .bitmask = bitmask, |
1da177e4 LT |
2922 | }; |
2923 | struct nfs4_lookup_res res = { | |
2924 | .server = server, | |
0e574af1 | 2925 | .fattr = info->fattr, |
1da177e4 LT |
2926 | .fh = fhandle, |
2927 | }; | |
2928 | struct rpc_message msg = { | |
2929 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT], | |
2930 | .rpc_argp = &args, | |
2931 | .rpc_resp = &res, | |
2932 | }; | |
008f55d0 | 2933 | |
aa9c2669 DQ |
2934 | bitmask[0] = nfs4_fattr_bitmap[0]; |
2935 | bitmask[1] = nfs4_fattr_bitmap[1]; | |
2936 | /* | |
2937 | * Process the label in the upcoming getfattr | |
2938 | */ | |
2939 | bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL; | |
2940 | ||
0e574af1 | 2941 | nfs_fattr_init(info->fattr); |
7c513058 | 2942 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
2943 | } |
2944 | ||
2945 | static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, | |
2946 | struct nfs_fsinfo *info) | |
2947 | { | |
2948 | struct nfs4_exception exception = { }; | |
2949 | int err; | |
2950 | do { | |
fb8a5ba8 | 2951 | err = _nfs4_lookup_root(server, fhandle, info); |
b5f875a9 | 2952 | trace_nfs4_lookup_root(server, fhandle, info->fattr, err); |
fb8a5ba8 BS |
2953 | switch (err) { |
2954 | case 0: | |
2955 | case -NFS4ERR_WRONGSEC: | |
05e9cfb4 | 2956 | goto out; |
fb8a5ba8 BS |
2957 | default: |
2958 | err = nfs4_handle_exception(server, err, &exception); | |
2959 | } | |
1da177e4 | 2960 | } while (exception.retry); |
05e9cfb4 | 2961 | out: |
1da177e4 LT |
2962 | return err; |
2963 | } | |
2964 | ||
8f70e95f BS |
2965 | static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, |
2966 | struct nfs_fsinfo *info, rpc_authflavor_t flavor) | |
2967 | { | |
c2190661 TM |
2968 | struct rpc_auth_create_args auth_args = { |
2969 | .pseudoflavor = flavor, | |
2970 | }; | |
8f70e95f BS |
2971 | struct rpc_auth *auth; |
2972 | int ret; | |
2973 | ||
c2190661 | 2974 | auth = rpcauth_create(&auth_args, server->client); |
e8d920c5 | 2975 | if (IS_ERR(auth)) { |
75bc8821 | 2976 | ret = -EACCES; |
8f70e95f BS |
2977 | goto out; |
2978 | } | |
2979 | ret = nfs4_lookup_root(server, fhandle, info); | |
8f70e95f BS |
2980 | out: |
2981 | return ret; | |
2982 | } | |
2983 | ||
9a744ba3 CL |
2984 | /* |
2985 | * Retry pseudoroot lookup with various security flavors. We do this when: | |
2986 | * | |
2987 | * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC | |
2988 | * NFSv4.1: the server does not support the SECINFO_NO_NAME operation | |
2989 | * | |
2990 | * Returns zero on success, or a negative NFS4ERR value, or a | |
2991 | * negative errno value. | |
2992 | */ | |
801a16dc | 2993 | static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, |
54ceac45 | 2994 | struct nfs_fsinfo *info) |
1da177e4 | 2995 | { |
9a744ba3 CL |
2996 | /* Per 3530bis 15.33.5 */ |
2997 | static const rpc_authflavor_t flav_array[] = { | |
2998 | RPC_AUTH_GSS_KRB5P, | |
2999 | RPC_AUTH_GSS_KRB5I, | |
3000 | RPC_AUTH_GSS_KRB5, | |
c4eafe11 | 3001 | RPC_AUTH_UNIX, /* courtesy */ |
9a744ba3 CL |
3002 | RPC_AUTH_NULL, |
3003 | }; | |
3004 | int status = -EPERM; | |
3005 | size_t i; | |
6a1a1e34 | 3006 | |
4d4b69dd WAA |
3007 | if (server->auth_info.flavor_len > 0) { |
3008 | /* try each flavor specified by user */ | |
3009 | for (i = 0; i < server->auth_info.flavor_len; i++) { | |
3010 | status = nfs4_lookup_root_sec(server, fhandle, info, | |
3011 | server->auth_info.flavors[i]); | |
3012 | if (status == -NFS4ERR_WRONGSEC || status == -EACCES) | |
3013 | continue; | |
3014 | break; | |
3015 | } | |
3016 | } else { | |
3017 | /* no flavors specified by user, try default list */ | |
3018 | for (i = 0; i < ARRAY_SIZE(flav_array); i++) { | |
3019 | status = nfs4_lookup_root_sec(server, fhandle, info, | |
3020 | flav_array[i]); | |
3021 | if (status == -NFS4ERR_WRONGSEC || status == -EACCES) | |
3022 | continue; | |
3023 | break; | |
3024 | } | |
8f70e95f | 3025 | } |
9a744ba3 | 3026 | |
fb8a5ba8 BS |
3027 | /* |
3028 | * -EACCESS could mean that the user doesn't have correct permissions | |
3029 | * to access the mount. It could also mean that we tried to mount | |
3030 | * with a gss auth flavor, but rpc.gssd isn't running. Either way, | |
3031 | * existing mount programs don't handle -EACCES very well so it should | |
3032 | * be mapped to -EPERM instead. | |
3033 | */ | |
3034 | if (status == -EACCES) | |
3035 | status = -EPERM; | |
801a16dc BS |
3036 | return status; |
3037 | } | |
3038 | ||
2ed4b95b CL |
3039 | static int nfs4_do_find_root_sec(struct nfs_server *server, |
3040 | struct nfs_fh *fhandle, struct nfs_fsinfo *info) | |
3041 | { | |
3042 | int mv = server->nfs_client->cl_minorversion; | |
3043 | return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info); | |
3044 | } | |
3045 | ||
3046 | /** | |
3047 | * nfs4_proc_get_rootfh - get file handle for server's pseudoroot | |
3048 | * @server: initialized nfs_server handle | |
3049 | * @fhandle: we fill in the pseudo-fs root file handle | |
3050 | * @info: we fill in an FSINFO struct | |
5e6b1990 | 3051 | * @auth_probe: probe the auth flavours |
2ed4b95b CL |
3052 | * |
3053 | * Returns zero on success, or a negative errno. | |
801a16dc | 3054 | */ |
3028eb2b | 3055 | int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle, |
5e6b1990 TM |
3056 | struct nfs_fsinfo *info, |
3057 | bool auth_probe) | |
801a16dc | 3058 | { |
2ed4b95b CL |
3059 | int status; |
3060 | ||
5e6b1990 TM |
3061 | switch (auth_probe) { |
3062 | case false: | |
3063 | status = nfs4_lookup_root(server, fhandle, info); | |
3064 | if (status != -NFS4ERR_WRONGSEC) | |
3065 | break; | |
5e6b1990 | 3066 | default: |
2ed4b95b | 3067 | status = nfs4_do_find_root_sec(server, fhandle, info); |
5e6b1990 | 3068 | } |
2ed4b95b | 3069 | |
1da177e4 LT |
3070 | if (status == 0) |
3071 | status = nfs4_server_capabilities(server, fhandle); | |
3072 | if (status == 0) | |
3073 | status = nfs4_do_fsinfo(server, fhandle, info); | |
2ed4b95b | 3074 | |
c12e87f4 | 3075 | return nfs4_map_errors(status); |
1da177e4 LT |
3076 | } |
3077 | ||
bae36241 BS |
3078 | static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh, |
3079 | struct nfs_fsinfo *info) | |
3080 | { | |
3081 | int error; | |
3082 | struct nfs_fattr *fattr = info->fattr; | |
1775fd3e | 3083 | struct nfs4_label *label = NULL; |
bae36241 BS |
3084 | |
3085 | error = nfs4_server_capabilities(server, mntfh); | |
3086 | if (error < 0) { | |
3087 | dprintk("nfs4_get_root: getcaps error = %d\n", -error); | |
3088 | return error; | |
3089 | } | |
3090 | ||
14c43f76 DQ |
3091 | label = nfs4_label_alloc(server, GFP_KERNEL); |
3092 | if (IS_ERR(label)) | |
3093 | return PTR_ERR(label); | |
3094 | ||
1775fd3e | 3095 | error = nfs4_proc_getattr(server, mntfh, fattr, label); |
bae36241 BS |
3096 | if (error < 0) { |
3097 | dprintk("nfs4_get_root: getattr error = %d\n", -error); | |
14c43f76 | 3098 | goto err_free_label; |
bae36241 BS |
3099 | } |
3100 | ||
3101 | if (fattr->valid & NFS_ATTR_FATTR_FSID && | |
3102 | !nfs_fsid_equal(&server->fsid, &fattr->fsid)) | |
3103 | memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid)); | |
3104 | ||
14c43f76 DQ |
3105 | err_free_label: |
3106 | nfs4_label_free(label); | |
3107 | ||
bae36241 BS |
3108 | return error; |
3109 | } | |
3110 | ||
6b97fd3d MN |
3111 | /* |
3112 | * Get locations and (maybe) other attributes of a referral. | |
3113 | * Note that we'll actually follow the referral later when | |
3114 | * we detect fsid mismatch in inode revalidation | |
3115 | */ | |
f05d147f BS |
3116 | static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir, |
3117 | const struct qstr *name, struct nfs_fattr *fattr, | |
3118 | struct nfs_fh *fhandle) | |
6b97fd3d MN |
3119 | { |
3120 | int status = -ENOMEM; | |
3121 | struct page *page = NULL; | |
3122 | struct nfs4_fs_locations *locations = NULL; | |
6b97fd3d MN |
3123 | |
3124 | page = alloc_page(GFP_KERNEL); | |
3125 | if (page == NULL) | |
3126 | goto out; | |
3127 | locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); | |
3128 | if (locations == NULL) | |
3129 | goto out; | |
3130 | ||
f05d147f | 3131 | status = nfs4_proc_fs_locations(client, dir, name, locations, page); |
6b97fd3d MN |
3132 | if (status != 0) |
3133 | goto out; | |
519ae255 CL |
3134 | |
3135 | /* | |
3136 | * If the fsid didn't change, this is a migration event, not a | |
3137 | * referral. Cause us to drop into the exception handler, which | |
3138 | * will kick off migration recovery. | |
3139 | */ | |
6b97fd3d | 3140 | if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) { |
533eb461 AA |
3141 | dprintk("%s: server did not return a different fsid for" |
3142 | " a referral at %s\n", __func__, name->name); | |
519ae255 | 3143 | status = -NFS4ERR_MOVED; |
6b97fd3d MN |
3144 | goto out; |
3145 | } | |
533eb461 AA |
3146 | /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */ |
3147 | nfs_fixup_referral_attributes(&locations->fattr); | |
6b97fd3d | 3148 | |
533eb461 | 3149 | /* replace the lookup nfs_fattr with the locations nfs_fattr */ |
6b97fd3d | 3150 | memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr)); |
6b97fd3d MN |
3151 | memset(fhandle, 0, sizeof(struct nfs_fh)); |
3152 | out: | |
3153 | if (page) | |
3154 | __free_page(page); | |
5d7ca35a | 3155 | kfree(locations); |
6b97fd3d MN |
3156 | return status; |
3157 | } | |
3158 | ||
1775fd3e DQ |
3159 | static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, |
3160 | struct nfs_fattr *fattr, struct nfs4_label *label) | |
1da177e4 LT |
3161 | { |
3162 | struct nfs4_getattr_arg args = { | |
3163 | .fh = fhandle, | |
3164 | .bitmask = server->attr_bitmask, | |
3165 | }; | |
3166 | struct nfs4_getattr_res res = { | |
3167 | .fattr = fattr, | |
1775fd3e | 3168 | .label = label, |
1da177e4 LT |
3169 | .server = server, |
3170 | }; | |
3171 | struct rpc_message msg = { | |
3172 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], | |
3173 | .rpc_argp = &args, | |
3174 | .rpc_resp = &res, | |
3175 | }; | |
aa9c2669 DQ |
3176 | |
3177 | args.bitmask = nfs4_bitmask(server, label); | |
3178 | ||
0e574af1 | 3179 | nfs_fattr_init(fattr); |
7c513058 | 3180 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
3181 | } |
3182 | ||
1775fd3e DQ |
3183 | static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, |
3184 | struct nfs_fattr *fattr, struct nfs4_label *label) | |
1da177e4 LT |
3185 | { |
3186 | struct nfs4_exception exception = { }; | |
3187 | int err; | |
3188 | do { | |
b5f875a9 TM |
3189 | err = _nfs4_proc_getattr(server, fhandle, fattr, label); |
3190 | trace_nfs4_getattr(server, fhandle, fattr, err); | |
3191 | err = nfs4_handle_exception(server, err, | |
1da177e4 LT |
3192 | &exception); |
3193 | } while (exception.retry); | |
3194 | return err; | |
3195 | } | |
3196 | ||
3197 | /* | |
3198 | * The file is not closed if it is opened due to the a request to change | |
3199 | * the size of the file. The open call will not be needed once the | |
3200 | * VFS layer lookup-intents are implemented. | |
3201 | * | |
3202 | * Close is called when the inode is destroyed. | |
3203 | * If we haven't opened the file for O_WRONLY, we | |
3204 | * need to in the size_change case to obtain a stateid. | |
3205 | * | |
3206 | * Got race? | |
3207 | * Because OPEN is always done by name in nfsv4, it is | |
3208 | * possible that we opened a different file by the same | |
3209 | * name. We can recognize this race condition, but we | |
3210 | * can't do anything about it besides returning an error. | |
3211 | * | |
3212 | * This will be fixed with VFS changes (lookup-intent). | |
3213 | */ | |
3214 | static int | |
3215 | nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr, | |
3216 | struct iattr *sattr) | |
3217 | { | |
08e9eac4 | 3218 | struct inode *inode = dentry->d_inode; |
659bfcd6 | 3219 | struct rpc_cred *cred = NULL; |
d530838b | 3220 | struct nfs4_state *state = NULL; |
14c43f76 | 3221 | struct nfs4_label *label = NULL; |
1da177e4 LT |
3222 | int status; |
3223 | ||
88ac815c PT |
3224 | if (pnfs_ld_layoutret_on_setattr(inode) && |
3225 | sattr->ia_valid & ATTR_SIZE && | |
3226 | sattr->ia_size < i_size_read(inode)) | |
24028672 | 3227 | pnfs_commit_and_return_layout(inode); |
8a1636c4 | 3228 | |
0e574af1 | 3229 | nfs_fattr_init(fattr); |
1da177e4 | 3230 | |
2669940d AA |
3231 | /* Deal with open(O_TRUNC) */ |
3232 | if (sattr->ia_valid & ATTR_OPEN) | |
cc7936f9 | 3233 | sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME); |
2669940d AA |
3234 | |
3235 | /* Optimization: if the end result is no change, don't RPC */ | |
cc7936f9 | 3236 | if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0) |
2669940d AA |
3237 | return 0; |
3238 | ||
d530838b | 3239 | /* Search for an existing open(O_WRITE) file */ |
659bfcd6 TM |
3240 | if (sattr->ia_valid & ATTR_FILE) { |
3241 | struct nfs_open_context *ctx; | |
3242 | ||
3243 | ctx = nfs_file_open_context(sattr->ia_file); | |
504e5189 NB |
3244 | if (ctx) { |
3245 | cred = ctx->cred; | |
3246 | state = ctx->state; | |
3247 | } | |
659bfcd6 | 3248 | } |
08e9eac4 | 3249 | |
14c43f76 DQ |
3250 | label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); |
3251 | if (IS_ERR(label)) | |
3252 | return PTR_ERR(label); | |
3253 | ||
3254 | status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label); | |
aa9c2669 | 3255 | if (status == 0) { |
65e4308d | 3256 | nfs_setattr_update_inode(inode, sattr); |
aa9c2669 DQ |
3257 | nfs_setsecurity(inode, fattr, label); |
3258 | } | |
14c43f76 | 3259 | nfs4_label_free(label); |
1da177e4 LT |
3260 | return status; |
3261 | } | |
3262 | ||
0c2e53f1 TM |
3263 | static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, |
3264 | const struct qstr *name, struct nfs_fh *fhandle, | |
1775fd3e | 3265 | struct nfs_fattr *fattr, struct nfs4_label *label) |
2b3de441 | 3266 | { |
0c2e53f1 | 3267 | struct nfs_server *server = NFS_SERVER(dir); |
2b3de441 DH |
3268 | int status; |
3269 | struct nfs4_lookup_arg args = { | |
3270 | .bitmask = server->attr_bitmask, | |
0c2e53f1 | 3271 | .dir_fh = NFS_FH(dir), |
2b3de441 DH |
3272 | .name = name, |
3273 | }; | |
3274 | struct nfs4_lookup_res res = { | |
3275 | .server = server, | |
3276 | .fattr = fattr, | |
aa9c2669 | 3277 | .label = label, |
2b3de441 DH |
3278 | .fh = fhandle, |
3279 | }; | |
3280 | struct rpc_message msg = { | |
3281 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP], | |
3282 | .rpc_argp = &args, | |
3283 | .rpc_resp = &res, | |
3284 | }; | |
3285 | ||
aa9c2669 DQ |
3286 | args.bitmask = nfs4_bitmask(server, label); |
3287 | ||
2b3de441 DH |
3288 | nfs_fattr_init(fattr); |
3289 | ||
1da177e4 | 3290 | dprintk("NFS call lookup %s\n", name->name); |
0c2e53f1 | 3291 | status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
3292 | dprintk("NFS reply lookup: %d\n", status); |
3293 | return status; | |
3294 | } | |
3295 | ||
72de53ec | 3296 | static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr) |
7ebb9315 | 3297 | { |
7ebb9315 | 3298 | fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | |
72de53ec | 3299 | NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT; |
7ebb9315 BS |
3300 | fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; |
3301 | fattr->nlink = 2; | |
3302 | } | |
3303 | ||
72de53ec BS |
3304 | static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir, |
3305 | struct qstr *name, struct nfs_fh *fhandle, | |
1775fd3e | 3306 | struct nfs_fattr *fattr, struct nfs4_label *label) |
1da177e4 LT |
3307 | { |
3308 | struct nfs4_exception exception = { }; | |
72de53ec | 3309 | struct rpc_clnt *client = *clnt; |
1da177e4 LT |
3310 | int err; |
3311 | do { | |
1775fd3e | 3312 | err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label); |
078ea3df | 3313 | trace_nfs4_lookup(dir, name, err); |
72de53ec | 3314 | switch (err) { |
08ef7bd3 | 3315 | case -NFS4ERR_BADNAME: |
72de53ec BS |
3316 | err = -ENOENT; |
3317 | goto out; | |
0c2e53f1 | 3318 | case -NFS4ERR_MOVED: |
f05d147f | 3319 | err = nfs4_get_referral(client, dir, name, fattr, fhandle); |
72de53ec | 3320 | goto out; |
0c2e53f1 | 3321 | case -NFS4ERR_WRONGSEC: |
72de53ec BS |
3322 | err = -EPERM; |
3323 | if (client != *clnt) | |
3324 | goto out; | |
66b06860 | 3325 | client = nfs4_negotiate_security(client, dir, name); |
72de53ec BS |
3326 | if (IS_ERR(client)) |
3327 | return PTR_ERR(client); | |
3328 | ||
3329 | exception.retry = 1; | |
3330 | break; | |
3331 | default: | |
3332 | err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception); | |
0c2e53f1 | 3333 | } |
1da177e4 | 3334 | } while (exception.retry); |
72de53ec BS |
3335 | |
3336 | out: | |
3337 | if (err == 0) | |
3338 | *clnt = client; | |
3339 | else if (client != *clnt) | |
3340 | rpc_shutdown_client(client); | |
3341 | ||
1da177e4 LT |
3342 | return err; |
3343 | } | |
3344 | ||
80a16b21 | 3345 | static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, |
1775fd3e DQ |
3346 | struct nfs_fh *fhandle, struct nfs_fattr *fattr, |
3347 | struct nfs4_label *label) | |
72de53ec BS |
3348 | { |
3349 | int status; | |
3350 | struct rpc_clnt *client = NFS_CLIENT(dir); | |
3351 | ||
1775fd3e | 3352 | status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label); |
72de53ec BS |
3353 | if (client != NFS_CLIENT(dir)) { |
3354 | rpc_shutdown_client(client); | |
3355 | nfs_fixup_secinfo_attributes(fattr); | |
3356 | } | |
3357 | return status; | |
3358 | } | |
3359 | ||
f05d147f BS |
3360 | struct rpc_clnt * |
3361 | nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name, | |
3362 | struct nfs_fh *fhandle, struct nfs_fattr *fattr) | |
3363 | { | |
b72888cb | 3364 | struct rpc_clnt *client = NFS_CLIENT(dir); |
f05d147f | 3365 | int status; |
f05d147f | 3366 | |
1775fd3e | 3367 | status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL); |
b72888cb | 3368 | if (status < 0) |
f05d147f | 3369 | return ERR_PTR(status); |
b72888cb | 3370 | return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client; |
f05d147f BS |
3371 | } |
3372 | ||
1da177e4 LT |
3373 | static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) |
3374 | { | |
76b32999 | 3375 | struct nfs_server *server = NFS_SERVER(inode); |
1da177e4 LT |
3376 | struct nfs4_accessargs args = { |
3377 | .fh = NFS_FH(inode), | |
a4980e78 | 3378 | .bitmask = server->cache_consistency_bitmask, |
76b32999 TM |
3379 | }; |
3380 | struct nfs4_accessres res = { | |
3381 | .server = server, | |
1da177e4 | 3382 | }; |
1da177e4 LT |
3383 | struct rpc_message msg = { |
3384 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS], | |
3385 | .rpc_argp = &args, | |
3386 | .rpc_resp = &res, | |
3387 | .rpc_cred = entry->cred, | |
3388 | }; | |
3389 | int mode = entry->mask; | |
aa9c2669 | 3390 | int status = 0; |
1da177e4 LT |
3391 | |
3392 | /* | |
3393 | * Determine which access bits we want to ask for... | |
3394 | */ | |
3395 | if (mode & MAY_READ) | |
3396 | args.access |= NFS4_ACCESS_READ; | |
3397 | if (S_ISDIR(inode->i_mode)) { | |
3398 | if (mode & MAY_WRITE) | |
3399 | args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE; | |
3400 | if (mode & MAY_EXEC) | |
3401 | args.access |= NFS4_ACCESS_LOOKUP; | |
3402 | } else { | |
3403 | if (mode & MAY_WRITE) | |
3404 | args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND; | |
3405 | if (mode & MAY_EXEC) | |
3406 | args.access |= NFS4_ACCESS_EXECUTE; | |
3407 | } | |
c407d41a TM |
3408 | |
3409 | res.fattr = nfs_alloc_fattr(); | |
3410 | if (res.fattr == NULL) | |
3411 | return -ENOMEM; | |
3412 | ||
7c513058 | 3413 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 | 3414 | if (!status) { |
6168f62c | 3415 | nfs_access_set_mask(entry, res.access); |
c407d41a | 3416 | nfs_refresh_inode(inode, res.fattr); |
1da177e4 | 3417 | } |
c407d41a | 3418 | nfs_free_fattr(res.fattr); |
1da177e4 LT |
3419 | return status; |
3420 | } | |
3421 | ||
3422 | static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) | |
3423 | { | |
3424 | struct nfs4_exception exception = { }; | |
3425 | int err; | |
3426 | do { | |
c1578b76 TM |
3427 | err = _nfs4_proc_access(inode, entry); |
3428 | trace_nfs4_access(inode, err); | |
3429 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
1da177e4 LT |
3430 | &exception); |
3431 | } while (exception.retry); | |
3432 | return err; | |
3433 | } | |
3434 | ||
3435 | /* | |
3436 | * TODO: For the time being, we don't try to get any attributes | |
3437 | * along with any of the zero-copy operations READ, READDIR, | |
3438 | * READLINK, WRITE. | |
3439 | * | |
3440 | * In the case of the first three, we want to put the GETATTR | |
3441 | * after the read-type operation -- this is because it is hard | |
3442 | * to predict the length of a GETATTR response in v4, and thus | |
3443 | * align the READ data correctly. This means that the GETATTR | |
3444 | * may end up partially falling into the page cache, and we should | |
3445 | * shift it into the 'tail' of the xdr_buf before processing. | |
3446 | * To do this efficiently, we need to know the total length | |
3447 | * of data received, which doesn't seem to be available outside | |
3448 | * of the RPC layer. | |
3449 | * | |
3450 | * In the case of WRITE, we also want to put the GETATTR after | |
3451 | * the operation -- in this case because we want to make sure | |
140150db | 3452 | * we get the post-operation mtime and size. |
1da177e4 LT |
3453 | * |
3454 | * Both of these changes to the XDR layer would in fact be quite | |
3455 | * minor, but I decided to leave them for a subsequent patch. | |
3456 | */ | |
3457 | static int _nfs4_proc_readlink(struct inode *inode, struct page *page, | |
3458 | unsigned int pgbase, unsigned int pglen) | |
3459 | { | |
3460 | struct nfs4_readlink args = { | |
3461 | .fh = NFS_FH(inode), | |
3462 | .pgbase = pgbase, | |
3463 | .pglen = pglen, | |
3464 | .pages = &page, | |
3465 | }; | |
f50c7000 | 3466 | struct nfs4_readlink_res res; |
1da177e4 LT |
3467 | struct rpc_message msg = { |
3468 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK], | |
3469 | .rpc_argp = &args, | |
f50c7000 | 3470 | .rpc_resp = &res, |
1da177e4 LT |
3471 | }; |
3472 | ||
7c513058 | 3473 | return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
3474 | } |
3475 | ||
3476 | static int nfs4_proc_readlink(struct inode *inode, struct page *page, | |
3477 | unsigned int pgbase, unsigned int pglen) | |
3478 | { | |
3479 | struct nfs4_exception exception = { }; | |
3480 | int err; | |
3481 | do { | |
c1578b76 TM |
3482 | err = _nfs4_proc_readlink(inode, page, pgbase, pglen); |
3483 | trace_nfs4_readlink(inode, err); | |
3484 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
1da177e4 LT |
3485 | &exception); |
3486 | } while (exception.retry); | |
3487 | return err; | |
3488 | } | |
3489 | ||
1da177e4 | 3490 | /* |
8867fe58 | 3491 | * This is just for mknod. open(O_CREAT) will always do ->open_context(). |
1da177e4 | 3492 | */ |
1da177e4 LT |
3493 | static int |
3494 | nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, | |
8867fe58 | 3495 | int flags) |
1da177e4 | 3496 | { |
aa9c2669 | 3497 | struct nfs4_label l, *ilabel = NULL; |
8867fe58 | 3498 | struct nfs_open_context *ctx; |
1da177e4 | 3499 | struct nfs4_state *state; |
5bc2afc2 | 3500 | int opened = 0; |
1da177e4 LT |
3501 | int status = 0; |
3502 | ||
8867fe58 MS |
3503 | ctx = alloc_nfs_open_context(dentry, FMODE_READ); |
3504 | if (IS_ERR(ctx)) | |
3505 | return PTR_ERR(ctx); | |
3506 | ||
aa9c2669 DQ |
3507 | ilabel = nfs4_label_init_security(dir, dentry, sattr, &l); |
3508 | ||
a8a5da99 | 3509 | sattr->ia_mode &= ~current_umask(); |
5bc2afc2 | 3510 | state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened); |
1da177e4 LT |
3511 | if (IS_ERR(state)) { |
3512 | status = PTR_ERR(state); | |
c0204fd2 | 3513 | goto out; |
1da177e4 | 3514 | } |
1da177e4 | 3515 | out: |
aa9c2669 | 3516 | nfs4_label_release_security(ilabel); |
8867fe58 | 3517 | put_nfs_open_context(ctx); |
1da177e4 LT |
3518 | return status; |
3519 | } | |
3520 | ||
3521 | static int _nfs4_proc_remove(struct inode *dir, struct qstr *name) | |
3522 | { | |
16e42959 | 3523 | struct nfs_server *server = NFS_SERVER(dir); |
4fdc17b2 | 3524 | struct nfs_removeargs args = { |
1da177e4 | 3525 | .fh = NFS_FH(dir), |
26fe5750 | 3526 | .name = *name, |
16e42959 | 3527 | }; |
4fdc17b2 | 3528 | struct nfs_removeres res = { |
16e42959 | 3529 | .server = server, |
1da177e4 | 3530 | }; |
1da177e4 | 3531 | struct rpc_message msg = { |
4fdc17b2 TM |
3532 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE], |
3533 | .rpc_argp = &args, | |
3534 | .rpc_resp = &res, | |
1da177e4 | 3535 | }; |
778d2817 | 3536 | int status; |
1da177e4 | 3537 | |
7c513058 | 3538 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1); |
778d2817 | 3539 | if (status == 0) |
16e42959 | 3540 | update_changeattr(dir, &res.cinfo); |
1da177e4 LT |
3541 | return status; |
3542 | } | |
3543 | ||
3544 | static int nfs4_proc_remove(struct inode *dir, struct qstr *name) | |
3545 | { | |
3546 | struct nfs4_exception exception = { }; | |
3547 | int err; | |
3548 | do { | |
078ea3df TM |
3549 | err = _nfs4_proc_remove(dir, name); |
3550 | trace_nfs4_remove(dir, name, err); | |
3551 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
1da177e4 LT |
3552 | &exception); |
3553 | } while (exception.retry); | |
3554 | return err; | |
3555 | } | |
3556 | ||
e4eff1a6 | 3557 | static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir) |
1da177e4 | 3558 | { |
e4eff1a6 TM |
3559 | struct nfs_server *server = NFS_SERVER(dir); |
3560 | struct nfs_removeargs *args = msg->rpc_argp; | |
3561 | struct nfs_removeres *res = msg->rpc_resp; | |
1da177e4 | 3562 | |
e4eff1a6 | 3563 | res->server = server; |
1da177e4 | 3564 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE]; |
a9c92d6b | 3565 | nfs4_init_sequence(&args->seq_args, &res->seq_res, 1); |
aa9c2669 DQ |
3566 | |
3567 | nfs_fattr_init(res->dir_attr); | |
1da177e4 LT |
3568 | } |
3569 | ||
34e137cc BS |
3570 | static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data) |
3571 | { | |
d9afbd1b TM |
3572 | nfs4_setup_sequence(NFS_SERVER(data->dir), |
3573 | &data->args.seq_args, | |
3574 | &data->res.seq_res, | |
3575 | task); | |
1da177e4 LT |
3576 | } |
3577 | ||
e4eff1a6 | 3578 | static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir) |
1da177e4 | 3579 | { |
078ea3df TM |
3580 | struct nfs_unlinkdata *data = task->tk_calldata; |
3581 | struct nfs_removeres *res = &data->res; | |
e4eff1a6 | 3582 | |
14516c3a TM |
3583 | if (!nfs4_sequence_done(task, &res->seq_res)) |
3584 | return 0; | |
8478eaa1 N |
3585 | if (nfs4_async_handle_error(task, res->server, NULL, |
3586 | &data->timeout) == -EAGAIN) | |
e4eff1a6 TM |
3587 | return 0; |
3588 | update_changeattr(dir, &res->cinfo); | |
e4eff1a6 | 3589 | return 1; |
1da177e4 LT |
3590 | } |
3591 | ||
d3d4152a JL |
3592 | static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir) |
3593 | { | |
3594 | struct nfs_server *server = NFS_SERVER(dir); | |
3595 | struct nfs_renameargs *arg = msg->rpc_argp; | |
3596 | struct nfs_renameres *res = msg->rpc_resp; | |
3597 | ||
3598 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME]; | |
d3d4152a | 3599 | res->server = server; |
a9c92d6b | 3600 | nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1); |
d3d4152a JL |
3601 | } |
3602 | ||
c6bfa1a1 BS |
3603 | static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data) |
3604 | { | |
d9afbd1b TM |
3605 | nfs4_setup_sequence(NFS_SERVER(data->old_dir), |
3606 | &data->args.seq_args, | |
3607 | &data->res.seq_res, | |
3608 | task); | |
d3d4152a JL |
3609 | } |
3610 | ||
3611 | static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir, | |
3612 | struct inode *new_dir) | |
3613 | { | |
fbc6f7c2 TM |
3614 | struct nfs_renamedata *data = task->tk_calldata; |
3615 | struct nfs_renameres *res = &data->res; | |
d3d4152a JL |
3616 | |
3617 | if (!nfs4_sequence_done(task, &res->seq_res)) | |
3618 | return 0; | |
8478eaa1 | 3619 | if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN) |
d3d4152a JL |
3620 | return 0; |
3621 | ||
3622 | update_changeattr(old_dir, &res->old_cinfo); | |
d3d4152a | 3623 | update_changeattr(new_dir, &res->new_cinfo); |
d3d4152a JL |
3624 | return 1; |
3625 | } | |
3626 | ||
1da177e4 LT |
3627 | static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name) |
3628 | { | |
91ba2eee | 3629 | struct nfs_server *server = NFS_SERVER(inode); |
1da177e4 LT |
3630 | struct nfs4_link_arg arg = { |
3631 | .fh = NFS_FH(inode), | |
3632 | .dir_fh = NFS_FH(dir), | |
3633 | .name = name, | |
91ba2eee TM |
3634 | .bitmask = server->attr_bitmask, |
3635 | }; | |
91ba2eee TM |
3636 | struct nfs4_link_res res = { |
3637 | .server = server, | |
1775fd3e | 3638 | .label = NULL, |
1da177e4 | 3639 | }; |
1da177e4 LT |
3640 | struct rpc_message msg = { |
3641 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK], | |
3642 | .rpc_argp = &arg, | |
91ba2eee | 3643 | .rpc_resp = &res, |
1da177e4 | 3644 | }; |
136f2627 TM |
3645 | int status = -ENOMEM; |
3646 | ||
3647 | res.fattr = nfs_alloc_fattr(); | |
778d2817 | 3648 | if (res.fattr == NULL) |
136f2627 | 3649 | goto out; |
1da177e4 | 3650 | |
14c43f76 DQ |
3651 | res.label = nfs4_label_alloc(server, GFP_KERNEL); |
3652 | if (IS_ERR(res.label)) { | |
3653 | status = PTR_ERR(res.label); | |
3654 | goto out; | |
3655 | } | |
aa9c2669 | 3656 | arg.bitmask = nfs4_bitmask(server, res.label); |
14c43f76 | 3657 | |
7c513058 | 3658 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
91ba2eee TM |
3659 | if (!status) { |
3660 | update_changeattr(dir, &res.cinfo); | |
aa9c2669 DQ |
3661 | status = nfs_post_op_update_inode(inode, res.fattr); |
3662 | if (!status) | |
3663 | nfs_setsecurity(inode, res.fattr, res.label); | |
91ba2eee | 3664 | } |
14c43f76 DQ |
3665 | |
3666 | ||
3667 | nfs4_label_free(res.label); | |
3668 | ||
136f2627 | 3669 | out: |
136f2627 | 3670 | nfs_free_fattr(res.fattr); |
1da177e4 LT |
3671 | return status; |
3672 | } | |
3673 | ||
3674 | static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name) | |
3675 | { | |
3676 | struct nfs4_exception exception = { }; | |
3677 | int err; | |
3678 | do { | |
3679 | err = nfs4_handle_exception(NFS_SERVER(inode), | |
3680 | _nfs4_proc_link(inode, dir, name), | |
3681 | &exception); | |
3682 | } while (exception.retry); | |
3683 | return err; | |
3684 | } | |
3685 | ||
57dc9a57 TM |
3686 | struct nfs4_createdata { |
3687 | struct rpc_message msg; | |
3688 | struct nfs4_create_arg arg; | |
3689 | struct nfs4_create_res res; | |
3690 | struct nfs_fh fh; | |
3691 | struct nfs_fattr fattr; | |
1775fd3e | 3692 | struct nfs4_label *label; |
57dc9a57 TM |
3693 | }; |
3694 | ||
3695 | static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir, | |
3696 | struct qstr *name, struct iattr *sattr, u32 ftype) | |
3697 | { | |
3698 | struct nfs4_createdata *data; | |
3699 | ||
3700 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
3701 | if (data != NULL) { | |
3702 | struct nfs_server *server = NFS_SERVER(dir); | |
3703 | ||
14c43f76 DQ |
3704 | data->label = nfs4_label_alloc(server, GFP_KERNEL); |
3705 | if (IS_ERR(data->label)) | |
3706 | goto out_free; | |
3707 | ||
57dc9a57 TM |
3708 | data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE]; |
3709 | data->msg.rpc_argp = &data->arg; | |
3710 | data->msg.rpc_resp = &data->res; | |
3711 | data->arg.dir_fh = NFS_FH(dir); | |
3712 | data->arg.server = server; | |
3713 | data->arg.name = name; | |
3714 | data->arg.attrs = sattr; | |
3715 | data->arg.ftype = ftype; | |
aa9c2669 | 3716 | data->arg.bitmask = nfs4_bitmask(server, data->label); |
57dc9a57 TM |
3717 | data->res.server = server; |
3718 | data->res.fh = &data->fh; | |
3719 | data->res.fattr = &data->fattr; | |
1775fd3e | 3720 | data->res.label = data->label; |
57dc9a57 | 3721 | nfs_fattr_init(data->res.fattr); |
57dc9a57 TM |
3722 | } |
3723 | return data; | |
14c43f76 DQ |
3724 | out_free: |
3725 | kfree(data); | |
3726 | return NULL; | |
57dc9a57 TM |
3727 | } |
3728 | ||
3729 | static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data) | |
3730 | { | |
7c513058 | 3731 | int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg, |
e73b83f2 | 3732 | &data->arg.seq_args, &data->res.seq_res, 1); |
57dc9a57 TM |
3733 | if (status == 0) { |
3734 | update_changeattr(dir, &data->res.dir_cinfo); | |
1775fd3e | 3735 | status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label); |
57dc9a57 TM |
3736 | } |
3737 | return status; | |
3738 | } | |
3739 | ||
3740 | static void nfs4_free_createdata(struct nfs4_createdata *data) | |
3741 | { | |
14c43f76 | 3742 | nfs4_label_free(data->label); |
57dc9a57 TM |
3743 | kfree(data); |
3744 | } | |
3745 | ||
4f390c15 | 3746 | static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, |
1775fd3e DQ |
3747 | struct page *page, unsigned int len, struct iattr *sattr, |
3748 | struct nfs4_label *label) | |
1da177e4 | 3749 | { |
57dc9a57 TM |
3750 | struct nfs4_createdata *data; |
3751 | int status = -ENAMETOOLONG; | |
1da177e4 | 3752 | |
94a6d753 | 3753 | if (len > NFS4_MAXPATHLEN) |
57dc9a57 | 3754 | goto out; |
4f390c15 | 3755 | |
57dc9a57 TM |
3756 | status = -ENOMEM; |
3757 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK); | |
3758 | if (data == NULL) | |
3759 | goto out; | |
3760 | ||
3761 | data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK]; | |
3762 | data->arg.u.symlink.pages = &page; | |
3763 | data->arg.u.symlink.len = len; | |
1775fd3e | 3764 | data->arg.label = label; |
1da177e4 | 3765 | |
57dc9a57 TM |
3766 | status = nfs4_do_create(dir, dentry, data); |
3767 | ||
3768 | nfs4_free_createdata(data); | |
3769 | out: | |
1da177e4 LT |
3770 | return status; |
3771 | } | |
3772 | ||
4f390c15 | 3773 | static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, |
94a6d753 | 3774 | struct page *page, unsigned int len, struct iattr *sattr) |
1da177e4 LT |
3775 | { |
3776 | struct nfs4_exception exception = { }; | |
aa9c2669 | 3777 | struct nfs4_label l, *label = NULL; |
1da177e4 | 3778 | int err; |
aa9c2669 DQ |
3779 | |
3780 | label = nfs4_label_init_security(dir, dentry, sattr, &l); | |
3781 | ||
1da177e4 | 3782 | do { |
078ea3df TM |
3783 | err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label); |
3784 | trace_nfs4_symlink(dir, &dentry->d_name, err); | |
3785 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
1da177e4 LT |
3786 | &exception); |
3787 | } while (exception.retry); | |
aa9c2669 DQ |
3788 | |
3789 | nfs4_label_release_security(label); | |
1da177e4 LT |
3790 | return err; |
3791 | } | |
3792 | ||
3793 | static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, | |
1775fd3e | 3794 | struct iattr *sattr, struct nfs4_label *label) |
1da177e4 | 3795 | { |
57dc9a57 TM |
3796 | struct nfs4_createdata *data; |
3797 | int status = -ENOMEM; | |
1da177e4 | 3798 | |
57dc9a57 TM |
3799 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR); |
3800 | if (data == NULL) | |
3801 | goto out; | |
3802 | ||
1775fd3e | 3803 | data->arg.label = label; |
57dc9a57 TM |
3804 | status = nfs4_do_create(dir, dentry, data); |
3805 | ||
3806 | nfs4_free_createdata(data); | |
3807 | out: | |
1da177e4 LT |
3808 | return status; |
3809 | } | |
3810 | ||
3811 | static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, | |
3812 | struct iattr *sattr) | |
3813 | { | |
3814 | struct nfs4_exception exception = { }; | |
aa9c2669 | 3815 | struct nfs4_label l, *label = NULL; |
1da177e4 | 3816 | int err; |
a8a5da99 | 3817 | |
aa9c2669 DQ |
3818 | label = nfs4_label_init_security(dir, dentry, sattr, &l); |
3819 | ||
a8a5da99 | 3820 | sattr->ia_mode &= ~current_umask(); |
1da177e4 | 3821 | do { |
078ea3df TM |
3822 | err = _nfs4_proc_mkdir(dir, dentry, sattr, label); |
3823 | trace_nfs4_mkdir(dir, &dentry->d_name, err); | |
3824 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
1da177e4 LT |
3825 | &exception); |
3826 | } while (exception.retry); | |
aa9c2669 DQ |
3827 | nfs4_label_release_security(label); |
3828 | ||
1da177e4 LT |
3829 | return err; |
3830 | } | |
3831 | ||
3832 | static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, | |
56e4ebf8 | 3833 | u64 cookie, struct page **pages, unsigned int count, int plus) |
1da177e4 LT |
3834 | { |
3835 | struct inode *dir = dentry->d_inode; | |
3836 | struct nfs4_readdir_arg args = { | |
3837 | .fh = NFS_FH(dir), | |
56e4ebf8 | 3838 | .pages = pages, |
1da177e4 LT |
3839 | .pgbase = 0, |
3840 | .count = count, | |
96d25e53 | 3841 | .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask, |
82f2e547 | 3842 | .plus = plus, |
1da177e4 LT |
3843 | }; |
3844 | struct nfs4_readdir_res res; | |
3845 | struct rpc_message msg = { | |
3846 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR], | |
3847 | .rpc_argp = &args, | |
3848 | .rpc_resp = &res, | |
3849 | .rpc_cred = cred, | |
3850 | }; | |
3851 | int status; | |
3852 | ||
6de1472f AV |
3853 | dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__, |
3854 | dentry, | |
eadf4598 | 3855 | (unsigned long long)cookie); |
c3f52af3 | 3856 | nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args); |
1da177e4 | 3857 | res.pgbase = args.pgbase; |
7c513058 | 3858 | status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0); |
ac396128 | 3859 | if (status >= 0) { |
c3f52af3 | 3860 | memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE); |
ac396128 TM |
3861 | status += args.pgbase; |
3862 | } | |
c4812998 TM |
3863 | |
3864 | nfs_invalidate_atime(dir); | |
3865 | ||
3110ff80 | 3866 | dprintk("%s: returns %d\n", __func__, status); |
1da177e4 LT |
3867 | return status; |
3868 | } | |
3869 | ||
3870 | static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, | |
56e4ebf8 | 3871 | u64 cookie, struct page **pages, unsigned int count, int plus) |
1da177e4 LT |
3872 | { |
3873 | struct nfs4_exception exception = { }; | |
3874 | int err; | |
3875 | do { | |
c1578b76 TM |
3876 | err = _nfs4_proc_readdir(dentry, cred, cookie, |
3877 | pages, count, plus); | |
3878 | trace_nfs4_readdir(dentry->d_inode, err); | |
3879 | err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err, | |
1da177e4 LT |
3880 | &exception); |
3881 | } while (exception.retry); | |
3882 | return err; | |
3883 | } | |
3884 | ||
3885 | static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, | |
aa9c2669 | 3886 | struct iattr *sattr, struct nfs4_label *label, dev_t rdev) |
1da177e4 | 3887 | { |
57dc9a57 TM |
3888 | struct nfs4_createdata *data; |
3889 | int mode = sattr->ia_mode; | |
3890 | int status = -ENOMEM; | |
1da177e4 | 3891 | |
57dc9a57 TM |
3892 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK); |
3893 | if (data == NULL) | |
3894 | goto out; | |
3895 | ||
1da177e4 | 3896 | if (S_ISFIFO(mode)) |
57dc9a57 | 3897 | data->arg.ftype = NF4FIFO; |
1da177e4 | 3898 | else if (S_ISBLK(mode)) { |
57dc9a57 TM |
3899 | data->arg.ftype = NF4BLK; |
3900 | data->arg.u.device.specdata1 = MAJOR(rdev); | |
3901 | data->arg.u.device.specdata2 = MINOR(rdev); | |
1da177e4 LT |
3902 | } |
3903 | else if (S_ISCHR(mode)) { | |
57dc9a57 TM |
3904 | data->arg.ftype = NF4CHR; |
3905 | data->arg.u.device.specdata1 = MAJOR(rdev); | |
3906 | data->arg.u.device.specdata2 = MINOR(rdev); | |
4ea8fed5 TM |
3907 | } else if (!S_ISSOCK(mode)) { |
3908 | status = -EINVAL; | |
3909 | goto out_free; | |
1da177e4 | 3910 | } |
1775fd3e | 3911 | |
aa9c2669 | 3912 | data->arg.label = label; |
57dc9a57 | 3913 | status = nfs4_do_create(dir, dentry, data); |
4ea8fed5 | 3914 | out_free: |
57dc9a57 TM |
3915 | nfs4_free_createdata(data); |
3916 | out: | |
1da177e4 LT |
3917 | return status; |
3918 | } | |
3919 | ||
3920 | static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, | |
3921 | struct iattr *sattr, dev_t rdev) | |
3922 | { | |
3923 | struct nfs4_exception exception = { }; | |
aa9c2669 | 3924 | struct nfs4_label l, *label = NULL; |
1da177e4 | 3925 | int err; |
a8a5da99 | 3926 | |
aa9c2669 DQ |
3927 | label = nfs4_label_init_security(dir, dentry, sattr, &l); |
3928 | ||
a8a5da99 | 3929 | sattr->ia_mode &= ~current_umask(); |
1da177e4 | 3930 | do { |
078ea3df TM |
3931 | err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev); |
3932 | trace_nfs4_mknod(dir, &dentry->d_name, err); | |
3933 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
1da177e4 LT |
3934 | &exception); |
3935 | } while (exception.retry); | |
aa9c2669 DQ |
3936 | |
3937 | nfs4_label_release_security(label); | |
3938 | ||
1da177e4 LT |
3939 | return err; |
3940 | } | |
3941 | ||
3942 | static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, | |
3943 | struct nfs_fsstat *fsstat) | |
3944 | { | |
3945 | struct nfs4_statfs_arg args = { | |
3946 | .fh = fhandle, | |
3947 | .bitmask = server->attr_bitmask, | |
3948 | }; | |
24ad148a BH |
3949 | struct nfs4_statfs_res res = { |
3950 | .fsstat = fsstat, | |
3951 | }; | |
1da177e4 LT |
3952 | struct rpc_message msg = { |
3953 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS], | |
3954 | .rpc_argp = &args, | |
24ad148a | 3955 | .rpc_resp = &res, |
1da177e4 LT |
3956 | }; |
3957 | ||
0e574af1 | 3958 | nfs_fattr_init(fsstat->fattr); |
7c513058 | 3959 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
3960 | } |
3961 | ||
3962 | static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat) | |
3963 | { | |
3964 | struct nfs4_exception exception = { }; | |
3965 | int err; | |
3966 | do { | |
3967 | err = nfs4_handle_exception(server, | |
3968 | _nfs4_proc_statfs(server, fhandle, fsstat), | |
3969 | &exception); | |
3970 | } while (exception.retry); | |
3971 | return err; | |
3972 | } | |
3973 | ||
3974 | static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, | |
3975 | struct nfs_fsinfo *fsinfo) | |
3976 | { | |
3977 | struct nfs4_fsinfo_arg args = { | |
3978 | .fh = fhandle, | |
3979 | .bitmask = server->attr_bitmask, | |
3980 | }; | |
3dda5e43 BH |
3981 | struct nfs4_fsinfo_res res = { |
3982 | .fsinfo = fsinfo, | |
3983 | }; | |
1da177e4 LT |
3984 | struct rpc_message msg = { |
3985 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO], | |
3986 | .rpc_argp = &args, | |
3dda5e43 | 3987 | .rpc_resp = &res, |
1da177e4 LT |
3988 | }; |
3989 | ||
7c513058 | 3990 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
3991 | } |
3992 | ||
3993 | static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) | |
3994 | { | |
3995 | struct nfs4_exception exception = { }; | |
83ca7f5a | 3996 | unsigned long now = jiffies; |
1da177e4 LT |
3997 | int err; |
3998 | ||
3999 | do { | |
83ca7f5a | 4000 | err = _nfs4_do_fsinfo(server, fhandle, fsinfo); |
b5f875a9 | 4001 | trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err); |
83ca7f5a CL |
4002 | if (err == 0) { |
4003 | struct nfs_client *clp = server->nfs_client; | |
4004 | ||
4005 | spin_lock(&clp->cl_lock); | |
4006 | clp->cl_lease_time = fsinfo->lease_time * HZ; | |
4007 | clp->cl_last_renewal = now; | |
4008 | spin_unlock(&clp->cl_lock); | |
4009 | break; | |
4010 | } | |
4011 | err = nfs4_handle_exception(server, err, &exception); | |
1da177e4 LT |
4012 | } while (exception.retry); |
4013 | return err; | |
4014 | } | |
4015 | ||
4016 | static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) | |
4017 | { | |
e38eb650 BS |
4018 | int error; |
4019 | ||
0e574af1 | 4020 | nfs_fattr_init(fsinfo->fattr); |
e38eb650 | 4021 | error = nfs4_do_fsinfo(server, fhandle, fsinfo); |
dc182549 PT |
4022 | if (error == 0) { |
4023 | /* block layout checks this! */ | |
4024 | server->pnfs_blksize = fsinfo->blksize; | |
e38eb650 | 4025 | set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype); |
dc182549 | 4026 | } |
e38eb650 BS |
4027 | |
4028 | return error; | |
1da177e4 LT |
4029 | } |
4030 | ||
4031 | static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, | |
4032 | struct nfs_pathconf *pathconf) | |
4033 | { | |
4034 | struct nfs4_pathconf_arg args = { | |
4035 | .fh = fhandle, | |
4036 | .bitmask = server->attr_bitmask, | |
4037 | }; | |
d45b2989 BH |
4038 | struct nfs4_pathconf_res res = { |
4039 | .pathconf = pathconf, | |
4040 | }; | |
1da177e4 LT |
4041 | struct rpc_message msg = { |
4042 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF], | |
4043 | .rpc_argp = &args, | |
d45b2989 | 4044 | .rpc_resp = &res, |
1da177e4 LT |
4045 | }; |
4046 | ||
4047 | /* None of the pathconf attributes are mandatory to implement */ | |
4048 | if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) { | |
4049 | memset(pathconf, 0, sizeof(*pathconf)); | |
4050 | return 0; | |
4051 | } | |
4052 | ||
0e574af1 | 4053 | nfs_fattr_init(pathconf->fattr); |
7c513058 | 4054 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
1da177e4 LT |
4055 | } |
4056 | ||
4057 | static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, | |
4058 | struct nfs_pathconf *pathconf) | |
4059 | { | |
4060 | struct nfs4_exception exception = { }; | |
4061 | int err; | |
4062 | ||
4063 | do { | |
4064 | err = nfs4_handle_exception(server, | |
4065 | _nfs4_proc_pathconf(server, fhandle, pathconf), | |
4066 | &exception); | |
4067 | } while (exception.retry); | |
4068 | return err; | |
4069 | } | |
4070 | ||
5521abfd | 4071 | int nfs4_set_rw_stateid(nfs4_stateid *stateid, |
9b206149 TM |
4072 | const struct nfs_open_context *ctx, |
4073 | const struct nfs_lock_context *l_ctx, | |
4074 | fmode_t fmode) | |
4075 | { | |
4076 | const struct nfs_lockowner *lockowner = NULL; | |
4077 | ||
4078 | if (l_ctx != NULL) | |
4079 | lockowner = &l_ctx->lockowner; | |
5521abfd | 4080 | return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner); |
9b206149 TM |
4081 | } |
4082 | EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid); | |
4083 | ||
5521abfd TM |
4084 | static bool nfs4_stateid_is_current(nfs4_stateid *stateid, |
4085 | const struct nfs_open_context *ctx, | |
4086 | const struct nfs_lock_context *l_ctx, | |
4087 | fmode_t fmode) | |
4088 | { | |
4089 | nfs4_stateid current_stateid; | |
4090 | ||
e1253be0 TM |
4091 | /* If the current stateid represents a lost lock, then exit */ |
4092 | if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode) == -EIO) | |
4093 | return true; | |
5521abfd TM |
4094 | return nfs4_stateid_match(stateid, ¤t_stateid); |
4095 | } | |
4096 | ||
4097 | static bool nfs4_error_stateid_expired(int err) | |
4098 | { | |
4099 | switch (err) { | |
4100 | case -NFS4ERR_DELEG_REVOKED: | |
4101 | case -NFS4ERR_ADMIN_REVOKED: | |
4102 | case -NFS4ERR_BAD_STATEID: | |
4103 | case -NFS4ERR_STALE_STATEID: | |
4104 | case -NFS4ERR_OLD_STATEID: | |
4105 | case -NFS4ERR_OPENMODE: | |
4106 | case -NFS4ERR_EXPIRED: | |
4107 | return true; | |
4108 | } | |
4109 | return false; | |
4110 | } | |
4111 | ||
d45f60c6 | 4112 | void __nfs4_read_done_cb(struct nfs_pgio_header *hdr) |
d20581aa | 4113 | { |
d45f60c6 | 4114 | nfs_invalidate_atime(hdr->inode); |
d20581aa BH |
4115 | } |
4116 | ||
d45f60c6 | 4117 | static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr) |
1da177e4 | 4118 | { |
d45f60c6 | 4119 | struct nfs_server *server = NFS_SERVER(hdr->inode); |
1da177e4 | 4120 | |
d45f60c6 WAA |
4121 | trace_nfs4_read(hdr, task->tk_status); |
4122 | if (nfs4_async_handle_error(task, server, | |
8478eaa1 N |
4123 | hdr->args.context->state, |
4124 | NULL) == -EAGAIN) { | |
d00c5d43 | 4125 | rpc_restart_call_prepare(task); |
ec06c096 | 4126 | return -EAGAIN; |
1da177e4 | 4127 | } |
8850df99 | 4128 | |
d45f60c6 | 4129 | __nfs4_read_done_cb(hdr); |
1da177e4 | 4130 | if (task->tk_status > 0) |
d45f60c6 | 4131 | renew_lease(server, hdr->timestamp); |
ec06c096 | 4132 | return 0; |
1da177e4 LT |
4133 | } |
4134 | ||
5521abfd | 4135 | static bool nfs4_read_stateid_changed(struct rpc_task *task, |
3c6b899c | 4136 | struct nfs_pgio_args *args) |
5521abfd TM |
4137 | { |
4138 | ||
4139 | if (!nfs4_error_stateid_expired(task->tk_status) || | |
4140 | nfs4_stateid_is_current(&args->stateid, | |
4141 | args->context, | |
4142 | args->lock_context, | |
4143 | FMODE_READ)) | |
4144 | return false; | |
4145 | rpc_restart_call_prepare(task); | |
4146 | return true; | |
4147 | } | |
4148 | ||
d45f60c6 | 4149 | static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr) |
cbdabc7f AA |
4150 | { |
4151 | ||
4152 | dprintk("--> %s\n", __func__); | |
4153 | ||
d45f60c6 | 4154 | if (!nfs4_sequence_done(task, &hdr->res.seq_res)) |
cbdabc7f | 4155 | return -EAGAIN; |
d45f60c6 | 4156 | if (nfs4_read_stateid_changed(task, &hdr->args)) |
5521abfd | 4157 | return -EAGAIN; |
d45f60c6 WAA |
4158 | return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : |
4159 | nfs4_read_done_cb(task, hdr); | |
cbdabc7f AA |
4160 | } |
4161 | ||
d45f60c6 WAA |
4162 | static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr, |
4163 | struct rpc_message *msg) | |
1da177e4 | 4164 | { |
d45f60c6 WAA |
4165 | hdr->timestamp = jiffies; |
4166 | hdr->pgio_done_cb = nfs4_read_done_cb; | |
bdc7f021 | 4167 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ]; |
d45f60c6 | 4168 | nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0); |
1da177e4 LT |
4169 | } |
4170 | ||
d45f60c6 WAA |
4171 | static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task, |
4172 | struct nfs_pgio_header *hdr) | |
ea7c3303 | 4173 | { |
d45f60c6 WAA |
4174 | if (nfs4_setup_sequence(NFS_SERVER(hdr->inode), |
4175 | &hdr->args.seq_args, | |
4176 | &hdr->res.seq_res, | |
9b206149 | 4177 | task)) |
ef1820f9 | 4178 | return 0; |
d45f60c6 WAA |
4179 | if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, |
4180 | hdr->args.lock_context, | |
4181 | hdr->rw_ops->rw_mode) == -EIO) | |
ef1820f9 | 4182 | return -EIO; |
d45f60c6 | 4183 | if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) |
ef1820f9 N |
4184 | return -EIO; |
4185 | return 0; | |
1da177e4 LT |
4186 | } |
4187 | ||
d45f60c6 WAA |
4188 | static int nfs4_write_done_cb(struct rpc_task *task, |
4189 | struct nfs_pgio_header *hdr) | |
1da177e4 | 4190 | { |
d45f60c6 | 4191 | struct inode *inode = hdr->inode; |
8478eaa1 | 4192 | |
d45f60c6 WAA |
4193 | trace_nfs4_write(hdr, task->tk_status); |
4194 | if (nfs4_async_handle_error(task, NFS_SERVER(inode), | |
8478eaa1 N |
4195 | hdr->args.context->state, |
4196 | NULL) == -EAGAIN) { | |
d00c5d43 | 4197 | rpc_restart_call_prepare(task); |
788e7a89 | 4198 | return -EAGAIN; |
1da177e4 | 4199 | } |
4f9838c7 | 4200 | if (task->tk_status >= 0) { |
d45f60c6 WAA |
4201 | renew_lease(NFS_SERVER(inode), hdr->timestamp); |
4202 | nfs_post_op_update_inode_force_wcc(inode, &hdr->fattr); | |
4f9838c7 | 4203 | } |
788e7a89 | 4204 | return 0; |
1da177e4 LT |
4205 | } |
4206 | ||
5521abfd | 4207 | static bool nfs4_write_stateid_changed(struct rpc_task *task, |
3c6b899c | 4208 | struct nfs_pgio_args *args) |
5521abfd TM |
4209 | { |
4210 | ||
4211 | if (!nfs4_error_stateid_expired(task->tk_status) || | |
4212 | nfs4_stateid_is_current(&args->stateid, | |
4213 | args->context, | |
4214 | args->lock_context, | |
4215 | FMODE_WRITE)) | |
4216 | return false; | |
4217 | rpc_restart_call_prepare(task); | |
4218 | return true; | |
4219 | } | |
4220 | ||
d45f60c6 | 4221 | static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr) |
b029bc9b | 4222 | { |
d45f60c6 | 4223 | if (!nfs4_sequence_done(task, &hdr->res.seq_res)) |
b029bc9b | 4224 | return -EAGAIN; |
d45f60c6 | 4225 | if (nfs4_write_stateid_changed(task, &hdr->args)) |
5521abfd | 4226 | return -EAGAIN; |
d45f60c6 WAA |
4227 | return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) : |
4228 | nfs4_write_done_cb(task, hdr); | |
b029bc9b FI |
4229 | } |
4230 | ||
5a37f851 | 4231 | static |
d45f60c6 | 4232 | bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr) |
a69aef14 | 4233 | { |
5a37f851 | 4234 | /* Don't request attributes for pNFS or O_DIRECT writes */ |
d45f60c6 | 4235 | if (hdr->ds_clp != NULL || hdr->dreq != NULL) |
5a37f851 TM |
4236 | return false; |
4237 | /* Otherwise, request attributes if and only if we don't hold | |
4238 | * a delegation | |
4239 | */ | |
011e2a7f | 4240 | return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0; |
a69aef14 | 4241 | } |
a69aef14 | 4242 | |
d45f60c6 WAA |
4243 | static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr, |
4244 | struct rpc_message *msg) | |
1da177e4 | 4245 | { |
d45f60c6 | 4246 | struct nfs_server *server = NFS_SERVER(hdr->inode); |
bdc7f021 | 4247 | |
d45f60c6 WAA |
4248 | if (!nfs4_write_need_cache_consistency_data(hdr)) { |
4249 | hdr->args.bitmask = NULL; | |
4250 | hdr->res.fattr = NULL; | |
7ffd1064 | 4251 | } else |
d45f60c6 | 4252 | hdr->args.bitmask = server->cache_consistency_bitmask; |
5a37f851 | 4253 | |
d45f60c6 WAA |
4254 | if (!hdr->pgio_done_cb) |
4255 | hdr->pgio_done_cb = nfs4_write_done_cb; | |
4256 | hdr->res.server = server; | |
4257 | hdr->timestamp = jiffies; | |
1da177e4 | 4258 | |
bdc7f021 | 4259 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE]; |
d45f60c6 | 4260 | nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1); |
1da177e4 LT |
4261 | } |
4262 | ||
0b7c0153 | 4263 | static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data) |
c6cb80d0 | 4264 | { |
d9afbd1b TM |
4265 | nfs4_setup_sequence(NFS_SERVER(data->inode), |
4266 | &data->args.seq_args, | |
4267 | &data->res.seq_res, | |
4268 | task); | |
1da177e4 LT |
4269 | } |
4270 | ||
0b7c0153 | 4271 | static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data) |
1da177e4 | 4272 | { |
1da177e4 | 4273 | struct inode *inode = data->inode; |
14516c3a | 4274 | |
cc668ab3 | 4275 | trace_nfs4_commit(data, task->tk_status); |
8478eaa1 N |
4276 | if (nfs4_async_handle_error(task, NFS_SERVER(inode), |
4277 | NULL, NULL) == -EAGAIN) { | |
d00c5d43 | 4278 | rpc_restart_call_prepare(task); |
788e7a89 | 4279 | return -EAGAIN; |
1da177e4 | 4280 | } |
788e7a89 | 4281 | return 0; |
1da177e4 LT |
4282 | } |
4283 | ||
0b7c0153 | 4284 | static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data) |
5f452431 FI |
4285 | { |
4286 | if (!nfs4_sequence_done(task, &data->res.seq_res)) | |
4287 | return -EAGAIN; | |
0b7c0153 | 4288 | return data->commit_done_cb(task, data); |
5f452431 FI |
4289 | } |
4290 | ||
0b7c0153 | 4291 | static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg) |
1da177e4 | 4292 | { |
788e7a89 | 4293 | struct nfs_server *server = NFS_SERVER(data->inode); |
988b6dce | 4294 | |
0b7c0153 FI |
4295 | if (data->commit_done_cb == NULL) |
4296 | data->commit_done_cb = nfs4_commit_done_cb; | |
4f9838c7 | 4297 | data->res.server = server; |
bdc7f021 | 4298 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT]; |
a9c92d6b | 4299 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1); |
1da177e4 LT |
4300 | } |
4301 | ||
9bc4e3ca CL |
4302 | struct nfs4_renewdata { |
4303 | struct nfs_client *client; | |
4304 | unsigned long timestamp; | |
4305 | }; | |
4306 | ||
1da177e4 LT |
4307 | /* |
4308 | * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special | |
4309 | * standalone procedure for queueing an asynchronous RENEW. | |
4310 | */ | |
9bc4e3ca | 4311 | static void nfs4_renew_release(void *calldata) |
dc96aef9 | 4312 | { |
9bc4e3ca CL |
4313 | struct nfs4_renewdata *data = calldata; |
4314 | struct nfs_client *clp = data->client; | |
dc96aef9 | 4315 | |
0851de06 AB |
4316 | if (atomic_read(&clp->cl_count) > 1) |
4317 | nfs4_schedule_state_renewal(clp); | |
4318 | nfs_put_client(clp); | |
9bc4e3ca | 4319 | kfree(data); |
dc96aef9 AB |
4320 | } |
4321 | ||
9bc4e3ca | 4322 | static void nfs4_renew_done(struct rpc_task *task, void *calldata) |
1da177e4 | 4323 | { |
9bc4e3ca CL |
4324 | struct nfs4_renewdata *data = calldata; |
4325 | struct nfs_client *clp = data->client; | |
4326 | unsigned long timestamp = data->timestamp; | |
1da177e4 | 4327 | |
c6d01c6f | 4328 | trace_nfs4_renew_async(clp, task->tk_status); |
f8aba1e8 CL |
4329 | switch (task->tk_status) { |
4330 | case 0: | |
4331 | break; | |
4332 | case -NFS4ERR_LEASE_MOVED: | |
4333 | nfs4_schedule_lease_moved_recovery(clp); | |
4334 | break; | |
4335 | default: | |
95baa25c | 4336 | /* Unless we're shutting down, schedule state recovery! */ |
042b60be TM |
4337 | if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0) |
4338 | return; | |
4339 | if (task->tk_status != NFS4ERR_CB_PATH_DOWN) { | |
0400a6b0 | 4340 | nfs4_schedule_lease_recovery(clp); |
042b60be TM |
4341 | return; |
4342 | } | |
4343 | nfs4_schedule_path_down_recovery(clp); | |
1da177e4 | 4344 | } |
452e9352 | 4345 | do_renew_lease(clp, timestamp); |
1da177e4 LT |
4346 | } |
4347 | ||
963d8fe5 TM |
4348 | static const struct rpc_call_ops nfs4_renew_ops = { |
4349 | .rpc_call_done = nfs4_renew_done, | |
dc96aef9 | 4350 | .rpc_release = nfs4_renew_release, |
963d8fe5 TM |
4351 | }; |
4352 | ||
2f60ea6b | 4353 | static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags) |
1da177e4 LT |
4354 | { |
4355 | struct rpc_message msg = { | |
4356 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], | |
4357 | .rpc_argp = clp, | |
b4454fe1 | 4358 | .rpc_cred = cred, |
1da177e4 | 4359 | }; |
9bc4e3ca | 4360 | struct nfs4_renewdata *data; |
1da177e4 | 4361 | |
2f60ea6b TM |
4362 | if (renew_flags == 0) |
4363 | return 0; | |
0851de06 AB |
4364 | if (!atomic_inc_not_zero(&clp->cl_count)) |
4365 | return -EIO; | |
b569ad34 | 4366 | data = kmalloc(sizeof(*data), GFP_NOFS); |
9bc4e3ca CL |
4367 | if (data == NULL) |
4368 | return -ENOMEM; | |
4369 | data->client = clp; | |
4370 | data->timestamp = jiffies; | |
bc7a05ca | 4371 | return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT, |
9bc4e3ca | 4372 | &nfs4_renew_ops, data); |
1da177e4 LT |
4373 | } |
4374 | ||
8534d4ec | 4375 | static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred) |
1da177e4 LT |
4376 | { |
4377 | struct rpc_message msg = { | |
4378 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], | |
4379 | .rpc_argp = clp, | |
b4454fe1 | 4380 | .rpc_cred = cred, |
1da177e4 LT |
4381 | }; |
4382 | unsigned long now = jiffies; | |
4383 | int status; | |
4384 | ||
bc7a05ca | 4385 | status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
1da177e4 LT |
4386 | if (status < 0) |
4387 | return status; | |
452e9352 | 4388 | do_renew_lease(clp, now); |
1da177e4 LT |
4389 | return 0; |
4390 | } | |
4391 | ||
aa1870af BF |
4392 | static inline int nfs4_server_supports_acls(struct nfs_server *server) |
4393 | { | |
7dd7d959 | 4394 | return server->caps & NFS_CAP_ACLS; |
aa1870af BF |
4395 | } |
4396 | ||
21f498c2 TM |
4397 | /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that |
4398 | * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on | |
aa1870af BF |
4399 | * the stack. |
4400 | */ | |
21f498c2 | 4401 | #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE) |
aa1870af | 4402 | |
e9e3d724 NH |
4403 | static int buf_to_pages_noslab(const void *buf, size_t buflen, |
4404 | struct page **pages, unsigned int *pgbase) | |
4405 | { | |
4406 | struct page *newpage, **spages; | |
4407 | int rc = 0; | |
4408 | size_t len; | |
4409 | spages = pages; | |
4410 | ||
4411 | do { | |
21f498c2 | 4412 | len = min_t(size_t, PAGE_SIZE, buflen); |
e9e3d724 NH |
4413 | newpage = alloc_page(GFP_KERNEL); |
4414 | ||
4415 | if (newpage == NULL) | |
4416 | goto unwind; | |
4417 | memcpy(page_address(newpage), buf, len); | |
4418 | buf += len; | |
4419 | buflen -= len; | |
4420 | *pages++ = newpage; | |
4421 | rc++; | |
4422 | } while (buflen != 0); | |
4423 | ||
4424 | return rc; | |
4425 | ||
4426 | unwind: | |
4427 | for(; rc > 0; rc--) | |
4428 | __free_page(spages[rc-1]); | |
4429 | return -ENOMEM; | |
4430 | } | |
4431 | ||
e50a1c2e BF |
4432 | struct nfs4_cached_acl { |
4433 | int cached; | |
4434 | size_t len; | |
3e9d4154 | 4435 | char data[0]; |
e50a1c2e BF |
4436 | }; |
4437 | ||
4438 | static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl) | |
4439 | { | |
4440 | struct nfs_inode *nfsi = NFS_I(inode); | |
4441 | ||
4442 | spin_lock(&inode->i_lock); | |
4443 | kfree(nfsi->nfs4_acl); | |
4444 | nfsi->nfs4_acl = acl; | |
4445 | spin_unlock(&inode->i_lock); | |
4446 | } | |
4447 | ||
4448 | static void nfs4_zap_acl_attr(struct inode *inode) | |
4449 | { | |
4450 | nfs4_set_cached_acl(inode, NULL); | |
4451 | } | |
4452 | ||
4453 | static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen) | |
4454 | { | |
4455 | struct nfs_inode *nfsi = NFS_I(inode); | |
4456 | struct nfs4_cached_acl *acl; | |
4457 | int ret = -ENOENT; | |
4458 | ||
4459 | spin_lock(&inode->i_lock); | |
4460 | acl = nfsi->nfs4_acl; | |
4461 | if (acl == NULL) | |
4462 | goto out; | |
4463 | if (buf == NULL) /* user is just asking for length */ | |
4464 | goto out_len; | |
4465 | if (acl->cached == 0) | |
4466 | goto out; | |
4467 | ret = -ERANGE; /* see getxattr(2) man page */ | |
4468 | if (acl->len > buflen) | |
4469 | goto out; | |
4470 | memcpy(buf, acl->data, acl->len); | |
4471 | out_len: | |
4472 | ret = acl->len; | |
4473 | out: | |
4474 | spin_unlock(&inode->i_lock); | |
4475 | return ret; | |
4476 | } | |
4477 | ||
5794d21e | 4478 | static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len) |
e50a1c2e BF |
4479 | { |
4480 | struct nfs4_cached_acl *acl; | |
b291f1b1 | 4481 | size_t buflen = sizeof(*acl) + acl_len; |
e50a1c2e | 4482 | |
1f1ea6c2 | 4483 | if (buflen <= PAGE_SIZE) { |
b291f1b1 | 4484 | acl = kmalloc(buflen, GFP_KERNEL); |
e50a1c2e BF |
4485 | if (acl == NULL) |
4486 | goto out; | |
4487 | acl->cached = 1; | |
5794d21e | 4488 | _copy_from_pages(acl->data, pages, pgbase, acl_len); |
e50a1c2e BF |
4489 | } else { |
4490 | acl = kmalloc(sizeof(*acl), GFP_KERNEL); | |
4491 | if (acl == NULL) | |
4492 | goto out; | |
4493 | acl->cached = 0; | |
4494 | } | |
4495 | acl->len = acl_len; | |
4496 | out: | |
4497 | nfs4_set_cached_acl(inode, acl); | |
4498 | } | |
4499 | ||
bf118a34 AA |
4500 | /* |
4501 | * The getxattr API returns the required buffer length when called with a | |
4502 | * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating | |
4503 | * the required buf. On a NULL buf, we send a page of data to the server | |
4504 | * guessing that the ACL request can be serviced by a page. If so, we cache | |
4505 | * up to the page of ACL data, and the 2nd call to getxattr is serviced by | |
4506 | * the cache. If not so, we throw away the page, and cache the required | |
4507 | * length. The next getxattr call will then produce another round trip to | |
4508 | * the server, this time with the input buf of the required size. | |
4509 | */ | |
16b4289c | 4510 | static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) |
aa1870af | 4511 | { |
bf118a34 | 4512 | struct page *pages[NFS4ACL_MAXPAGES] = {NULL, }; |
aa1870af BF |
4513 | struct nfs_getaclargs args = { |
4514 | .fh = NFS_FH(inode), | |
4515 | .acl_pages = pages, | |
4516 | .acl_len = buflen, | |
4517 | }; | |
663c79b3 BH |
4518 | struct nfs_getaclres res = { |
4519 | .acl_len = buflen, | |
4520 | }; | |
aa1870af BF |
4521 | struct rpc_message msg = { |
4522 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL], | |
4523 | .rpc_argp = &args, | |
663c79b3 | 4524 | .rpc_resp = &res, |
aa1870af | 4525 | }; |
21f498c2 TM |
4526 | unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE); |
4527 | int ret = -ENOMEM, i; | |
aa1870af | 4528 | |
bf118a34 AA |
4529 | /* As long as we're doing a round trip to the server anyway, |
4530 | * let's be prepared for a page of acl data. */ | |
4531 | if (npages == 0) | |
4532 | npages = 1; | |
21f498c2 TM |
4533 | if (npages > ARRAY_SIZE(pages)) |
4534 | return -ERANGE; | |
5a006899 | 4535 | |
bf118a34 AA |
4536 | for (i = 0; i < npages; i++) { |
4537 | pages[i] = alloc_page(GFP_KERNEL); | |
4538 | if (!pages[i]) | |
4539 | goto out_free; | |
e50a1c2e | 4540 | } |
5a006899 SP |
4541 | |
4542 | /* for decoding across pages */ | |
4543 | res.acl_scratch = alloc_page(GFP_KERNEL); | |
4544 | if (!res.acl_scratch) | |
4545 | goto out_free; | |
4546 | ||
bf118a34 AA |
4547 | args.acl_len = npages * PAGE_SIZE; |
4548 | args.acl_pgbase = 0; | |
5a006899 | 4549 | |
de040bec | 4550 | dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n", |
bf118a34 AA |
4551 | __func__, buf, buflen, npages, args.acl_len); |
4552 | ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), | |
4553 | &msg, &args.seq_args, &res.seq_res, 0); | |
e50a1c2e BF |
4554 | if (ret) |
4555 | goto out_free; | |
bf118a34 | 4556 | |
1f1ea6c2 TM |
4557 | /* Handle the case where the passed-in buffer is too short */ |
4558 | if (res.acl_flags & NFS4_ACL_TRUNC) { | |
4559 | /* Did the user only issue a request for the acl length? */ | |
4560 | if (buf == NULL) | |
4561 | goto out_ok; | |
e50a1c2e | 4562 | ret = -ERANGE; |
1f1ea6c2 | 4563 | goto out_free; |
e50a1c2e | 4564 | } |
1f1ea6c2 | 4565 | nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len); |
7d3e91a8 SW |
4566 | if (buf) { |
4567 | if (res.acl_len > buflen) { | |
4568 | ret = -ERANGE; | |
4569 | goto out_free; | |
4570 | } | |
1f1ea6c2 | 4571 | _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len); |
7d3e91a8 | 4572 | } |
1f1ea6c2 TM |
4573 | out_ok: |
4574 | ret = res.acl_len; | |
e50a1c2e | 4575 | out_free: |
bf118a34 AA |
4576 | for (i = 0; i < npages; i++) |
4577 | if (pages[i]) | |
4578 | __free_page(pages[i]); | |
331818f1 TM |
4579 | if (res.acl_scratch) |
4580 | __free_page(res.acl_scratch); | |
aa1870af BF |
4581 | return ret; |
4582 | } | |
4583 | ||
16b4289c TM |
4584 | static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) |
4585 | { | |
4586 | struct nfs4_exception exception = { }; | |
4587 | ssize_t ret; | |
4588 | do { | |
4589 | ret = __nfs4_get_acl_uncached(inode, buf, buflen); | |
c1578b76 | 4590 | trace_nfs4_get_acl(inode, ret); |
16b4289c TM |
4591 | if (ret >= 0) |
4592 | break; | |
4593 | ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception); | |
4594 | } while (exception.retry); | |
4595 | return ret; | |
4596 | } | |
4597 | ||
e50a1c2e BF |
4598 | static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen) |
4599 | { | |
4600 | struct nfs_server *server = NFS_SERVER(inode); | |
4601 | int ret; | |
4602 | ||
4603 | if (!nfs4_server_supports_acls(server)) | |
4604 | return -EOPNOTSUPP; | |
4605 | ret = nfs_revalidate_inode(server, inode); | |
4606 | if (ret < 0) | |
4607 | return ret; | |
08a22b39 AK |
4608 | if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL) |
4609 | nfs_zap_acl_cache(inode); | |
e50a1c2e BF |
4610 | ret = nfs4_read_cached_acl(inode, buf, buflen); |
4611 | if (ret != -ENOENT) | |
bf118a34 AA |
4612 | /* -ENOENT is returned if there is no ACL or if there is an ACL |
4613 | * but no cached acl data, just the acl length */ | |
e50a1c2e BF |
4614 | return ret; |
4615 | return nfs4_get_acl_uncached(inode, buf, buflen); | |
4616 | } | |
4617 | ||
16b4289c | 4618 | static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) |
4b580ee3 BF |
4619 | { |
4620 | struct nfs_server *server = NFS_SERVER(inode); | |
4621 | struct page *pages[NFS4ACL_MAXPAGES]; | |
4622 | struct nfs_setaclargs arg = { | |
4623 | .fh = NFS_FH(inode), | |
4624 | .acl_pages = pages, | |
4625 | .acl_len = buflen, | |
4626 | }; | |
73c403a9 | 4627 | struct nfs_setaclres res; |
4b580ee3 BF |
4628 | struct rpc_message msg = { |
4629 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL], | |
4630 | .rpc_argp = &arg, | |
73c403a9 | 4631 | .rpc_resp = &res, |
4b580ee3 | 4632 | }; |
21f498c2 | 4633 | unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE); |
e9e3d724 | 4634 | int ret, i; |
4b580ee3 BF |
4635 | |
4636 | if (!nfs4_server_supports_acls(server)) | |
4637 | return -EOPNOTSUPP; | |
21f498c2 TM |
4638 | if (npages > ARRAY_SIZE(pages)) |
4639 | return -ERANGE; | |
e9e3d724 NH |
4640 | i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase); |
4641 | if (i < 0) | |
4642 | return i; | |
57ec14c5 | 4643 | nfs4_inode_return_delegation(inode); |
7c513058 | 4644 | ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
e9e3d724 NH |
4645 | |
4646 | /* | |
4647 | * Free each page after tx, so the only ref left is | |
4648 | * held by the network stack | |
4649 | */ | |
4650 | for (; i > 0; i--) | |
4651 | put_page(pages[i-1]); | |
4652 | ||
08a22b39 AK |
4653 | /* |
4654 | * Acl update can result in inode attribute update. | |
4655 | * so mark the attribute cache invalid. | |
4656 | */ | |
4657 | spin_lock(&inode->i_lock); | |
4658 | NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR; | |
4659 | spin_unlock(&inode->i_lock); | |
f41f7418 TM |
4660 | nfs_access_zap_cache(inode); |
4661 | nfs_zap_acl_cache(inode); | |
4b580ee3 BF |
4662 | return ret; |
4663 | } | |
4664 | ||
16b4289c TM |
4665 | static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) |
4666 | { | |
4667 | struct nfs4_exception exception = { }; | |
4668 | int err; | |
4669 | do { | |
c1578b76 TM |
4670 | err = __nfs4_proc_set_acl(inode, buf, buflen); |
4671 | trace_nfs4_set_acl(inode, err); | |
4672 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
16b4289c TM |
4673 | &exception); |
4674 | } while (exception.retry); | |
4675 | return err; | |
4676 | } | |
4677 | ||
aa9c2669 DQ |
4678 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
4679 | static int _nfs4_get_security_label(struct inode *inode, void *buf, | |
4680 | size_t buflen) | |
4681 | { | |
4682 | struct nfs_server *server = NFS_SERVER(inode); | |
4683 | struct nfs_fattr fattr; | |
4684 | struct nfs4_label label = {0, 0, buflen, buf}; | |
4685 | ||
4686 | u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; | |
fcb63a9b | 4687 | struct nfs4_getattr_arg arg = { |
aa9c2669 DQ |
4688 | .fh = NFS_FH(inode), |
4689 | .bitmask = bitmask, | |
4690 | }; | |
4691 | struct nfs4_getattr_res res = { | |
4692 | .fattr = &fattr, | |
4693 | .label = &label, | |
4694 | .server = server, | |
4695 | }; | |
4696 | struct rpc_message msg = { | |
4697 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], | |
fcb63a9b | 4698 | .rpc_argp = &arg, |
aa9c2669 DQ |
4699 | .rpc_resp = &res, |
4700 | }; | |
4701 | int ret; | |
4702 | ||
4703 | nfs_fattr_init(&fattr); | |
4704 | ||
fcb63a9b | 4705 | ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0); |
aa9c2669 DQ |
4706 | if (ret) |
4707 | return ret; | |
4708 | if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL)) | |
4709 | return -ENOENT; | |
4710 | if (buflen < label.len) | |
4711 | return -ERANGE; | |
4712 | return 0; | |
4713 | } | |
4714 | ||
4715 | static int nfs4_get_security_label(struct inode *inode, void *buf, | |
4716 | size_t buflen) | |
4717 | { | |
4718 | struct nfs4_exception exception = { }; | |
4719 | int err; | |
4720 | ||
4721 | if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) | |
4722 | return -EOPNOTSUPP; | |
4723 | ||
4724 | do { | |
c1578b76 TM |
4725 | err = _nfs4_get_security_label(inode, buf, buflen); |
4726 | trace_nfs4_get_security_label(inode, err); | |
4727 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
aa9c2669 DQ |
4728 | &exception); |
4729 | } while (exception.retry); | |
4730 | return err; | |
4731 | } | |
4732 | ||
4733 | static int _nfs4_do_set_security_label(struct inode *inode, | |
4734 | struct nfs4_label *ilabel, | |
4735 | struct nfs_fattr *fattr, | |
4736 | struct nfs4_label *olabel) | |
4737 | { | |
4738 | ||
4739 | struct iattr sattr = {0}; | |
4740 | struct nfs_server *server = NFS_SERVER(inode); | |
4741 | const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL }; | |
12207f69 | 4742 | struct nfs_setattrargs arg = { |
aa9c2669 DQ |
4743 | .fh = NFS_FH(inode), |
4744 | .iap = &sattr, | |
4745 | .server = server, | |
4746 | .bitmask = bitmask, | |
4747 | .label = ilabel, | |
4748 | }; | |
4749 | struct nfs_setattrres res = { | |
4750 | .fattr = fattr, | |
4751 | .label = olabel, | |
4752 | .server = server, | |
4753 | }; | |
4754 | struct rpc_message msg = { | |
4755 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], | |
12207f69 | 4756 | .rpc_argp = &arg, |
aa9c2669 DQ |
4757 | .rpc_resp = &res, |
4758 | }; | |
4759 | int status; | |
4760 | ||
12207f69 | 4761 | nfs4_stateid_copy(&arg.stateid, &zero_stateid); |
aa9c2669 | 4762 | |
12207f69 | 4763 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
aa9c2669 DQ |
4764 | if (status) |
4765 | dprintk("%s failed: %d\n", __func__, status); | |
4766 | ||
4767 | return status; | |
4768 | } | |
4769 | ||
4770 | static int nfs4_do_set_security_label(struct inode *inode, | |
4771 | struct nfs4_label *ilabel, | |
4772 | struct nfs_fattr *fattr, | |
4773 | struct nfs4_label *olabel) | |
4774 | { | |
4775 | struct nfs4_exception exception = { }; | |
4776 | int err; | |
4777 | ||
4778 | do { | |
c1578b76 TM |
4779 | err = _nfs4_do_set_security_label(inode, ilabel, |
4780 | fattr, olabel); | |
4781 | trace_nfs4_set_security_label(inode, err); | |
4782 | err = nfs4_handle_exception(NFS_SERVER(inode), err, | |
aa9c2669 DQ |
4783 | &exception); |
4784 | } while (exception.retry); | |
4785 | return err; | |
4786 | } | |
4787 | ||
4788 | static int | |
4789 | nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen) | |
4790 | { | |
4791 | struct nfs4_label ilabel, *olabel = NULL; | |
4792 | struct nfs_fattr fattr; | |
4793 | struct rpc_cred *cred; | |
4794 | struct inode *inode = dentry->d_inode; | |
4795 | int status; | |
4796 | ||
4797 | if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) | |
4798 | return -EOPNOTSUPP; | |
4799 | ||
4800 | nfs_fattr_init(&fattr); | |
4801 | ||
4802 | ilabel.pi = 0; | |
4803 | ilabel.lfs = 0; | |
4804 | ilabel.label = (char *)buf; | |
4805 | ilabel.len = buflen; | |
4806 | ||
4807 | cred = rpc_lookup_cred(); | |
4808 | if (IS_ERR(cred)) | |
4809 | return PTR_ERR(cred); | |
4810 | ||
4811 | olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); | |
4812 | if (IS_ERR(olabel)) { | |
4813 | status = -PTR_ERR(olabel); | |
4814 | goto out; | |
4815 | } | |
4816 | ||
4817 | status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel); | |
4818 | if (status == 0) | |
4819 | nfs_setsecurity(inode, &fattr, olabel); | |
4820 | ||
4821 | nfs4_label_free(olabel); | |
4822 | out: | |
4823 | put_rpccred(cred); | |
4824 | return status; | |
4825 | } | |
4826 | #endif /* CONFIG_NFS_V4_SECURITY_LABEL */ | |
4827 | ||
4828 | ||
1da177e4 | 4829 | static int |
8478eaa1 N |
4830 | nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, |
4831 | struct nfs4_state *state, long *timeout) | |
1da177e4 | 4832 | { |
aa5190d0 TM |
4833 | struct nfs_client *clp = server->nfs_client; |
4834 | ||
4835 | if (task->tk_status >= 0) | |
1da177e4 LT |
4836 | return 0; |
4837 | switch(task->tk_status) { | |
a1d0b5ee | 4838 | case -NFS4ERR_DELEG_REVOKED: |
9e33bed5 TM |
4839 | case -NFS4ERR_ADMIN_REVOKED: |
4840 | case -NFS4ERR_BAD_STATEID: | |
4841 | case -NFS4ERR_OPENMODE: | |
4842 | if (state == NULL) | |
4843 | break; | |
5d422301 | 4844 | if (nfs4_schedule_stateid_recovery(server, state) < 0) |
9f51a78e | 4845 | goto recovery_failed; |
0400a6b0 | 4846 | goto wait_on_recovery; |
0ced63d1 | 4847 | case -NFS4ERR_EXPIRED: |
5d422301 TM |
4848 | if (state != NULL) { |
4849 | if (nfs4_schedule_stateid_recovery(server, state) < 0) | |
9f51a78e | 4850 | goto recovery_failed; |
5d422301 | 4851 | } |
1da177e4 | 4852 | case -NFS4ERR_STALE_STATEID: |
a2c0b9e2 | 4853 | case -NFS4ERR_STALE_CLIENTID: |
0400a6b0 TM |
4854 | nfs4_schedule_lease_recovery(clp); |
4855 | goto wait_on_recovery; | |
519ae255 CL |
4856 | case -NFS4ERR_MOVED: |
4857 | if (nfs4_schedule_migration_recovery(server) < 0) | |
4858 | goto recovery_failed; | |
4859 | goto wait_on_recovery; | |
8ef2f8d4 CL |
4860 | case -NFS4ERR_LEASE_MOVED: |
4861 | nfs4_schedule_lease_moved_recovery(clp); | |
4862 | goto wait_on_recovery; | |
4745e315 AA |
4863 | #if defined(CONFIG_NFS_V4_1) |
4864 | case -NFS4ERR_BADSESSION: | |
4865 | case -NFS4ERR_BADSLOT: | |
4866 | case -NFS4ERR_BAD_HIGH_SLOT: | |
4867 | case -NFS4ERR_DEADSESSION: | |
4868 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: | |
4869 | case -NFS4ERR_SEQ_FALSE_RETRY: | |
4870 | case -NFS4ERR_SEQ_MISORDERED: | |
4871 | dprintk("%s ERROR %d, Reset session\n", __func__, | |
4872 | task->tk_status); | |
9f594791 | 4873 | nfs4_schedule_session_recovery(clp->cl_session, task->tk_status); |
4a82fd7c | 4874 | goto wait_on_recovery; |
4745e315 | 4875 | #endif /* CONFIG_NFS_V4_1 */ |
1da177e4 | 4876 | case -NFS4ERR_DELAY: |
aa5190d0 | 4877 | nfs_inc_server_stats(server, NFSIOS_DELAY); |
8478eaa1 N |
4878 | rpc_delay(task, nfs4_update_delay(timeout)); |
4879 | goto restart_call; | |
006ea73e | 4880 | case -NFS4ERR_GRACE: |
1da177e4 | 4881 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
a8a4ae3a | 4882 | case -NFS4ERR_RETRY_UNCACHED_REP: |
1da177e4 | 4883 | case -NFS4ERR_OLD_STATEID: |
f1478c13 | 4884 | goto restart_call; |
1da177e4 LT |
4885 | } |
4886 | task->tk_status = nfs4_map_errors(task->tk_status); | |
4887 | return 0; | |
9f51a78e | 4888 | recovery_failed: |
5d422301 TM |
4889 | task->tk_status = -EIO; |
4890 | return 0; | |
0400a6b0 | 4891 | wait_on_recovery: |
a2c0b9e2 | 4892 | rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL); |
a2c0b9e2 TM |
4893 | if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0) |
4894 | rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task); | |
519ae255 CL |
4895 | if (test_bit(NFS_MIG_FAILED, &server->mig_status)) |
4896 | goto recovery_failed; | |
f1478c13 | 4897 | restart_call: |
a2c0b9e2 TM |
4898 | task->tk_status = 0; |
4899 | return -EAGAIN; | |
1da177e4 LT |
4900 | } |
4901 | ||
f092075d CL |
4902 | static void nfs4_init_boot_verifier(const struct nfs_client *clp, |
4903 | nfs4_verifier *bootverf) | |
cd93710e CL |
4904 | { |
4905 | __be32 verf[2]; | |
4906 | ||
2c820d9a CL |
4907 | if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) { |
4908 | /* An impossible timestamp guarantees this value | |
4909 | * will never match a generated boot time. */ | |
4910 | verf[0] = 0; | |
17f26b12 | 4911 | verf[1] = cpu_to_be32(NSEC_PER_SEC + 1); |
2c820d9a | 4912 | } else { |
f092075d | 4913 | struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); |
17f26b12 TM |
4914 | verf[0] = cpu_to_be32(nn->boot_time.tv_sec); |
4915 | verf[1] = cpu_to_be32(nn->boot_time.tv_nsec); | |
2c820d9a | 4916 | } |
cd93710e CL |
4917 | memcpy(bootverf->data, verf, sizeof(bootverf->data)); |
4918 | } | |
4919 | ||
e984a55a | 4920 | static unsigned int |
ceb3a16c | 4921 | nfs4_init_nonuniform_client_string(struct nfs_client *clp, |
e984a55a CL |
4922 | char *buf, size_t len) |
4923 | { | |
4924 | unsigned int result; | |
4925 | ||
ceb3a16c TM |
4926 | if (clp->cl_owner_id != NULL) |
4927 | return strlcpy(buf, clp->cl_owner_id, len); | |
4928 | ||
e984a55a CL |
4929 | rcu_read_lock(); |
4930 | result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s", | |
4931 | clp->cl_ipaddr, | |
4932 | rpc_peeraddr2str(clp->cl_rpcclient, | |
4933 | RPC_DISPLAY_ADDR), | |
4934 | rpc_peeraddr2str(clp->cl_rpcclient, | |
4935 | RPC_DISPLAY_PROTO)); | |
4936 | rcu_read_unlock(); | |
ceb3a16c | 4937 | clp->cl_owner_id = kstrdup(buf, GFP_KERNEL); |
e984a55a CL |
4938 | return result; |
4939 | } | |
4940 | ||
4941 | static unsigned int | |
ceb3a16c | 4942 | nfs4_init_uniform_client_string(struct nfs_client *clp, |
e984a55a CL |
4943 | char *buf, size_t len) |
4944 | { | |
55b59293 | 4945 | const char *nodename = clp->cl_rpcclient->cl_nodename; |
ceb3a16c TM |
4946 | unsigned int result; |
4947 | ||
4948 | if (clp->cl_owner_id != NULL) | |
4949 | return strlcpy(buf, clp->cl_owner_id, len); | |
6f2ea7f2 CL |
4950 | |
4951 | if (nfs4_client_id_uniquifier[0] != '\0') | |
ceb3a16c | 4952 | result = scnprintf(buf, len, "Linux NFSv%u.%u %s/%s", |
55b59293 TM |
4953 | clp->rpc_ops->version, |
4954 | clp->cl_minorversion, | |
4955 | nfs4_client_id_uniquifier, | |
4956 | nodename); | |
ceb3a16c TM |
4957 | else |
4958 | result = scnprintf(buf, len, "Linux NFSv%u.%u %s", | |
e984a55a | 4959 | clp->rpc_ops->version, clp->cl_minorversion, |
6f2ea7f2 | 4960 | nodename); |
ceb3a16c TM |
4961 | clp->cl_owner_id = kstrdup(buf, GFP_KERNEL); |
4962 | return result; | |
e984a55a CL |
4963 | } |
4964 | ||
706cb8db CL |
4965 | /* |
4966 | * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback | |
4967 | * services. Advertise one based on the address family of the | |
4968 | * clientaddr. | |
4969 | */ | |
4970 | static unsigned int | |
4971 | nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len) | |
4972 | { | |
4973 | if (strchr(clp->cl_ipaddr, ':') != NULL) | |
4974 | return scnprintf(buf, len, "tcp6"); | |
4975 | else | |
4976 | return scnprintf(buf, len, "tcp"); | |
4977 | } | |
4978 | ||
f11b2a1c JL |
4979 | static void nfs4_setclientid_done(struct rpc_task *task, void *calldata) |
4980 | { | |
4981 | struct nfs4_setclientid *sc = calldata; | |
4982 | ||
4983 | if (task->tk_status == 0) | |
4984 | sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred); | |
4985 | } | |
4986 | ||
4987 | static const struct rpc_call_ops nfs4_setclientid_ops = { | |
4988 | .rpc_call_done = nfs4_setclientid_done, | |
4989 | }; | |
4990 | ||
6bbb4ae8 CL |
4991 | /** |
4992 | * nfs4_proc_setclientid - Negotiate client ID | |
4993 | * @clp: state data structure | |
4994 | * @program: RPC program for NFSv4 callback service | |
4995 | * @port: IP port number for NFS4 callback service | |
4996 | * @cred: RPC credential to use for this call | |
4997 | * @res: where to place the result | |
4998 | * | |
4999 | * Returns zero, a negative errno, or a negative NFS4ERR status code. | |
5000 | */ | |
bb8b27e5 TM |
5001 | int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, |
5002 | unsigned short port, struct rpc_cred *cred, | |
5003 | struct nfs4_setclientid_res *res) | |
1da177e4 LT |
5004 | { |
5005 | nfs4_verifier sc_verifier; | |
5006 | struct nfs4_setclientid setclientid = { | |
5007 | .sc_verifier = &sc_verifier, | |
5008 | .sc_prog = program, | |
f4eecd5d | 5009 | .sc_cb_ident = clp->cl_cb_ident, |
1da177e4 LT |
5010 | }; |
5011 | struct rpc_message msg = { | |
5012 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID], | |
5013 | .rpc_argp = &setclientid, | |
bb8b27e5 | 5014 | .rpc_resp = res, |
286d7d6a | 5015 | .rpc_cred = cred, |
1da177e4 | 5016 | }; |
f11b2a1c JL |
5017 | struct rpc_task *task; |
5018 | struct rpc_task_setup task_setup_data = { | |
5019 | .rpc_client = clp->cl_rpcclient, | |
5020 | .rpc_message = &msg, | |
5021 | .callback_ops = &nfs4_setclientid_ops, | |
5022 | .callback_data = &setclientid, | |
5023 | .flags = RPC_TASK_TIMEOUT, | |
5024 | }; | |
6bbb4ae8 | 5025 | int status; |
1da177e4 | 5026 | |
de734831 | 5027 | /* nfs_client_id4 */ |
f092075d | 5028 | nfs4_init_boot_verifier(clp, &sc_verifier); |
e984a55a CL |
5029 | if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags)) |
5030 | setclientid.sc_name_len = | |
5031 | nfs4_init_uniform_client_string(clp, | |
5032 | setclientid.sc_name, | |
5033 | sizeof(setclientid.sc_name)); | |
5034 | else | |
5035 | setclientid.sc_name_len = | |
5036 | nfs4_init_nonuniform_client_string(clp, | |
5037 | setclientid.sc_name, | |
5038 | sizeof(setclientid.sc_name)); | |
de734831 | 5039 | /* cb_client4 */ |
706cb8db CL |
5040 | setclientid.sc_netid_len = |
5041 | nfs4_init_callback_netid(clp, | |
5042 | setclientid.sc_netid, | |
5043 | sizeof(setclientid.sc_netid)); | |
de734831 | 5044 | setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr, |
d4d3c507 | 5045 | sizeof(setclientid.sc_uaddr), "%s.%u.%u", |
1da177e4 LT |
5046 | clp->cl_ipaddr, port >> 8, port & 255); |
5047 | ||
6bbb4ae8 CL |
5048 | dprintk("NFS call setclientid auth=%s, '%.*s'\n", |
5049 | clp->cl_rpcclient->cl_auth->au_ops->au_name, | |
5050 | setclientid.sc_name_len, setclientid.sc_name); | |
f11b2a1c JL |
5051 | task = rpc_run_task(&task_setup_data); |
5052 | if (IS_ERR(task)) { | |
5053 | status = PTR_ERR(task); | |
5054 | goto out; | |
5055 | } | |
5056 | status = task->tk_status; | |
5057 | if (setclientid.sc_cred) { | |
5058 | clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred); | |
5059 | put_rpccred(setclientid.sc_cred); | |
5060 | } | |
5061 | rpc_put_task(task); | |
5062 | out: | |
c6d01c6f | 5063 | trace_nfs4_setclientid(clp, status); |
6bbb4ae8 CL |
5064 | dprintk("NFS reply setclientid: %d\n", status); |
5065 | return status; | |
1da177e4 LT |
5066 | } |
5067 | ||
6bbb4ae8 CL |
5068 | /** |
5069 | * nfs4_proc_setclientid_confirm - Confirm client ID | |
5070 | * @clp: state data structure | |
5071 | * @res: result of a previous SETCLIENTID | |
5072 | * @cred: RPC credential to use for this call | |
5073 | * | |
5074 | * Returns zero, a negative errno, or a negative NFS4ERR status code. | |
5075 | */ | |
fd954ae1 | 5076 | int nfs4_proc_setclientid_confirm(struct nfs_client *clp, |
bb8b27e5 TM |
5077 | struct nfs4_setclientid_res *arg, |
5078 | struct rpc_cred *cred) | |
1da177e4 | 5079 | { |
1da177e4 LT |
5080 | struct rpc_message msg = { |
5081 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM], | |
bb8b27e5 | 5082 | .rpc_argp = arg, |
286d7d6a | 5083 | .rpc_cred = cred, |
1da177e4 | 5084 | }; |
1da177e4 LT |
5085 | int status; |
5086 | ||
6bbb4ae8 CL |
5087 | dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n", |
5088 | clp->cl_rpcclient->cl_auth->au_ops->au_name, | |
5089 | clp->cl_clientid); | |
1bd714f2 | 5090 | status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
c6d01c6f | 5091 | trace_nfs4_setclientid_confirm(clp, status); |
6bbb4ae8 | 5092 | dprintk("NFS reply setclientid_confirm: %d\n", status); |
1da177e4 LT |
5093 | return status; |
5094 | } | |
5095 | ||
fe650407 TM |
5096 | struct nfs4_delegreturndata { |
5097 | struct nfs4_delegreturnargs args; | |
fa178f29 | 5098 | struct nfs4_delegreturnres res; |
fe650407 TM |
5099 | struct nfs_fh fh; |
5100 | nfs4_stateid stateid; | |
26e976a8 | 5101 | unsigned long timestamp; |
fa178f29 | 5102 | struct nfs_fattr fattr; |
fe650407 | 5103 | int rpc_status; |
039b756a PT |
5104 | struct inode *inode; |
5105 | bool roc; | |
5106 | u32 roc_barrier; | |
fe650407 TM |
5107 | }; |
5108 | ||
fe650407 TM |
5109 | static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata) |
5110 | { | |
5111 | struct nfs4_delegreturndata *data = calldata; | |
938e1010 | 5112 | |
14516c3a TM |
5113 | if (!nfs4_sequence_done(task, &data->res.seq_res)) |
5114 | return; | |
938e1010 | 5115 | |
ca8acf8d | 5116 | trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status); |
79708861 | 5117 | switch (task->tk_status) { |
79708861 | 5118 | case 0: |
fa178f29 | 5119 | renew_lease(data->res.server, data->timestamp); |
c97cf606 TM |
5120 | case -NFS4ERR_ADMIN_REVOKED: |
5121 | case -NFS4ERR_DELEG_REVOKED: | |
5122 | case -NFS4ERR_BAD_STATEID: | |
5123 | case -NFS4ERR_OLD_STATEID: | |
5124 | case -NFS4ERR_STALE_STATEID: | |
5125 | case -NFS4ERR_EXPIRED: | |
5126 | task->tk_status = 0; | |
039b756a PT |
5127 | if (data->roc) |
5128 | pnfs_roc_set_barrier(data->inode, data->roc_barrier); | |
c97cf606 | 5129 | break; |
79708861 | 5130 | default: |
8478eaa1 N |
5131 | if (nfs4_async_handle_error(task, data->res.server, |
5132 | NULL, NULL) == -EAGAIN) { | |
d00c5d43 | 5133 | rpc_restart_call_prepare(task); |
79708861 RL |
5134 | return; |
5135 | } | |
5136 | } | |
5137 | data->rpc_status = task->tk_status; | |
fe650407 TM |
5138 | } |
5139 | ||
5140 | static void nfs4_delegreturn_release(void *calldata) | |
5141 | { | |
039b756a PT |
5142 | struct nfs4_delegreturndata *data = calldata; |
5143 | ||
5144 | if (data->roc) | |
5145 | pnfs_roc_release(data->inode); | |
fe650407 TM |
5146 | kfree(calldata); |
5147 | } | |
5148 | ||
938e1010 AA |
5149 | static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data) |
5150 | { | |
5151 | struct nfs4_delegreturndata *d_data; | |
5152 | ||
5153 | d_data = (struct nfs4_delegreturndata *)data; | |
5154 | ||
039b756a PT |
5155 | if (d_data->roc && |
5156 | pnfs_roc_drain(d_data->inode, &d_data->roc_barrier, task)) | |
5157 | return; | |
5158 | ||
d9afbd1b TM |
5159 | nfs4_setup_sequence(d_data->res.server, |
5160 | &d_data->args.seq_args, | |
5161 | &d_data->res.seq_res, | |
5162 | task); | |
938e1010 | 5163 | } |
938e1010 | 5164 | |
c8d149f3 | 5165 | static const struct rpc_call_ops nfs4_delegreturn_ops = { |
938e1010 | 5166 | .rpc_call_prepare = nfs4_delegreturn_prepare, |
fe650407 TM |
5167 | .rpc_call_done = nfs4_delegreturn_done, |
5168 | .rpc_release = nfs4_delegreturn_release, | |
5169 | }; | |
5170 | ||
e6f81075 | 5171 | static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync) |
fe650407 TM |
5172 | { |
5173 | struct nfs4_delegreturndata *data; | |
fa178f29 | 5174 | struct nfs_server *server = NFS_SERVER(inode); |
fe650407 | 5175 | struct rpc_task *task; |
5138fde0 TM |
5176 | struct rpc_message msg = { |
5177 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN], | |
5178 | .rpc_cred = cred, | |
5179 | }; | |
c970aa85 TM |
5180 | struct rpc_task_setup task_setup_data = { |
5181 | .rpc_client = server->client, | |
5138fde0 | 5182 | .rpc_message = &msg, |
c970aa85 TM |
5183 | .callback_ops = &nfs4_delegreturn_ops, |
5184 | .flags = RPC_TASK_ASYNC, | |
5185 | }; | |
e6f81075 | 5186 | int status = 0; |
fe650407 | 5187 | |
8535b2be | 5188 | data = kzalloc(sizeof(*data), GFP_NOFS); |
fe650407 TM |
5189 | if (data == NULL) |
5190 | return -ENOMEM; | |
a9c92d6b | 5191 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1); |
fe650407 TM |
5192 | data->args.fhandle = &data->fh; |
5193 | data->args.stateid = &data->stateid; | |
9e907fec | 5194 | data->args.bitmask = server->cache_consistency_bitmask; |
fe650407 | 5195 | nfs_copy_fh(&data->fh, NFS_FH(inode)); |
f597c537 | 5196 | nfs4_stateid_copy(&data->stateid, stateid); |
fa178f29 TM |
5197 | data->res.fattr = &data->fattr; |
5198 | data->res.server = server; | |
5138fde0 | 5199 | nfs_fattr_init(data->res.fattr); |
26e976a8 | 5200 | data->timestamp = jiffies; |
fe650407 | 5201 | data->rpc_status = 0; |
039b756a PT |
5202 | data->inode = inode; |
5203 | data->roc = list_empty(&NFS_I(inode)->open_files) ? | |
5204 | pnfs_roc(inode) : false; | |
fe650407 | 5205 | |
c970aa85 | 5206 | task_setup_data.callback_data = data; |
1174dd1f TM |
5207 | msg.rpc_argp = &data->args; |
5208 | msg.rpc_resp = &data->res; | |
c970aa85 | 5209 | task = rpc_run_task(&task_setup_data); |
7a1218a2 | 5210 | if (IS_ERR(task)) |
fe650407 | 5211 | return PTR_ERR(task); |
e6f81075 TM |
5212 | if (!issync) |
5213 | goto out; | |
fe650407 | 5214 | status = nfs4_wait_for_completion_rpc_task(task); |
e6f81075 TM |
5215 | if (status != 0) |
5216 | goto out; | |
5217 | status = data->rpc_status; | |
e144cbcc TM |
5218 | if (status == 0) |
5219 | nfs_post_op_update_inode_force_wcc(inode, &data->fattr); | |
5220 | else | |
5221 | nfs_refresh_inode(inode, &data->fattr); | |
e6f81075 | 5222 | out: |
e6b3c4db | 5223 | rpc_put_task(task); |
fe650407 | 5224 | return status; |
1da177e4 LT |
5225 | } |
5226 | ||
e6f81075 | 5227 | int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync) |
1da177e4 LT |
5228 | { |
5229 | struct nfs_server *server = NFS_SERVER(inode); | |
5230 | struct nfs4_exception exception = { }; | |
5231 | int err; | |
5232 | do { | |
e6f81075 | 5233 | err = _nfs4_proc_delegreturn(inode, cred, stateid, issync); |
ca8acf8d | 5234 | trace_nfs4_delegreturn(inode, err); |
1da177e4 LT |
5235 | switch (err) { |
5236 | case -NFS4ERR_STALE_STATEID: | |
5237 | case -NFS4ERR_EXPIRED: | |
1da177e4 LT |
5238 | case 0: |
5239 | return 0; | |
5240 | } | |
5241 | err = nfs4_handle_exception(server, err, &exception); | |
5242 | } while (exception.retry); | |
5243 | return err; | |
5244 | } | |
5245 | ||
5246 | #define NFS4_LOCK_MINTIMEOUT (1 * HZ) | |
5247 | #define NFS4_LOCK_MAXTIMEOUT (30 * HZ) | |
5248 | ||
5249 | /* | |
5250 | * sleep, with exponential backoff, and retry the LOCK operation. | |
5251 | */ | |
5252 | static unsigned long | |
5253 | nfs4_set_lock_task_retry(unsigned long timeout) | |
5254 | { | |
416ad3c9 | 5255 | freezable_schedule_timeout_killable_unsafe(timeout); |
1da177e4 LT |
5256 | timeout <<= 1; |
5257 | if (timeout > NFS4_LOCK_MAXTIMEOUT) | |
5258 | return NFS4_LOCK_MAXTIMEOUT; | |
5259 | return timeout; | |
5260 | } | |
5261 | ||
1da177e4 LT |
5262 | static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
5263 | { | |
5264 | struct inode *inode = state->inode; | |
5265 | struct nfs_server *server = NFS_SERVER(inode); | |
7539bbab | 5266 | struct nfs_client *clp = server->nfs_client; |
911d1aaf | 5267 | struct nfs_lockt_args arg = { |
1da177e4 | 5268 | .fh = NFS_FH(inode), |
911d1aaf | 5269 | .fl = request, |
1da177e4 | 5270 | }; |
911d1aaf TM |
5271 | struct nfs_lockt_res res = { |
5272 | .denied = request, | |
1da177e4 LT |
5273 | }; |
5274 | struct rpc_message msg = { | |
5275 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT], | |
5276 | .rpc_argp = &arg, | |
5277 | .rpc_resp = &res, | |
5278 | .rpc_cred = state->owner->so_cred, | |
5279 | }; | |
1da177e4 LT |
5280 | struct nfs4_lock_state *lsp; |
5281 | int status; | |
5282 | ||
911d1aaf | 5283 | arg.lock_owner.clientid = clp->cl_clientid; |
8d0a8a9d TM |
5284 | status = nfs4_set_lock_state(state, request); |
5285 | if (status != 0) | |
5286 | goto out; | |
5287 | lsp = request->fl_u.nfs4_fl.owner; | |
48c22eb2 | 5288 | arg.lock_owner.id = lsp->ls_seqid.owner_id; |
d035c36c | 5289 | arg.lock_owner.s_dev = server->s_dev; |
7c513058 | 5290 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
911d1aaf TM |
5291 | switch (status) { |
5292 | case 0: | |
5293 | request->fl_type = F_UNLCK; | |
5294 | break; | |
5295 | case -NFS4ERR_DENIED: | |
5296 | status = 0; | |
1da177e4 | 5297 | } |
70cc6487 | 5298 | request->fl_ops->fl_release_private(request); |
a6f951dd | 5299 | request->fl_ops = NULL; |
8d0a8a9d | 5300 | out: |
1da177e4 LT |
5301 | return status; |
5302 | } | |
5303 | ||
5304 | static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) | |
5305 | { | |
5306 | struct nfs4_exception exception = { }; | |
5307 | int err; | |
5308 | ||
5309 | do { | |
d1b748a5 TM |
5310 | err = _nfs4_proc_getlk(state, cmd, request); |
5311 | trace_nfs4_get_lock(request, state, cmd, err); | |
5312 | err = nfs4_handle_exception(NFS_SERVER(state->inode), err, | |
1da177e4 LT |
5313 | &exception); |
5314 | } while (exception.retry); | |
5315 | return err; | |
5316 | } | |
5317 | ||
5318 | static int do_vfs_lock(struct file *file, struct file_lock *fl) | |
5319 | { | |
5320 | int res = 0; | |
5321 | switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { | |
5322 | case FL_POSIX: | |
5323 | res = posix_lock_file_wait(file, fl); | |
5324 | break; | |
5325 | case FL_FLOCK: | |
5326 | res = flock_lock_file_wait(file, fl); | |
5327 | break; | |
5328 | default: | |
5329 | BUG(); | |
5330 | } | |
1da177e4 LT |
5331 | return res; |
5332 | } | |
5333 | ||
faf5f49c | 5334 | struct nfs4_unlockdata { |
911d1aaf TM |
5335 | struct nfs_locku_args arg; |
5336 | struct nfs_locku_res res; | |
faf5f49c TM |
5337 | struct nfs4_lock_state *lsp; |
5338 | struct nfs_open_context *ctx; | |
911d1aaf TM |
5339 | struct file_lock fl; |
5340 | const struct nfs_server *server; | |
26e976a8 | 5341 | unsigned long timestamp; |
faf5f49c TM |
5342 | }; |
5343 | ||
911d1aaf TM |
5344 | static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl, |
5345 | struct nfs_open_context *ctx, | |
5346 | struct nfs4_lock_state *lsp, | |
5347 | struct nfs_seqid *seqid) | |
5348 | { | |
5349 | struct nfs4_unlockdata *p; | |
5350 | struct inode *inode = lsp->ls_state->inode; | |
5351 | ||
8535b2be | 5352 | p = kzalloc(sizeof(*p), GFP_NOFS); |
911d1aaf TM |
5353 | if (p == NULL) |
5354 | return NULL; | |
5355 | p->arg.fh = NFS_FH(inode); | |
5356 | p->arg.fl = &p->fl; | |
5357 | p->arg.seqid = seqid; | |
c1d51931 | 5358 | p->res.seqid = seqid; |
911d1aaf TM |
5359 | p->arg.stateid = &lsp->ls_stateid; |
5360 | p->lsp = lsp; | |
5361 | atomic_inc(&lsp->ls_count); | |
5362 | /* Ensure we don't close file until we're done freeing locks! */ | |
5363 | p->ctx = get_nfs_open_context(ctx); | |
5364 | memcpy(&p->fl, fl, sizeof(p->fl)); | |
5365 | p->server = NFS_SERVER(inode); | |
5366 | return p; | |
5367 | } | |
5368 | ||
06f814a3 | 5369 | static void nfs4_locku_release_calldata(void *data) |
faf5f49c | 5370 | { |
963d8fe5 | 5371 | struct nfs4_unlockdata *calldata = data; |
911d1aaf | 5372 | nfs_free_seqid(calldata->arg.seqid); |
06f814a3 TM |
5373 | nfs4_put_lock_state(calldata->lsp); |
5374 | put_nfs_open_context(calldata->ctx); | |
5375 | kfree(calldata); | |
faf5f49c TM |
5376 | } |
5377 | ||
963d8fe5 | 5378 | static void nfs4_locku_done(struct rpc_task *task, void *data) |
faf5f49c | 5379 | { |
963d8fe5 | 5380 | struct nfs4_unlockdata *calldata = data; |
faf5f49c | 5381 | |
14516c3a TM |
5382 | if (!nfs4_sequence_done(task, &calldata->res.seq_res)) |
5383 | return; | |
faf5f49c TM |
5384 | switch (task->tk_status) { |
5385 | case 0: | |
f597c537 TM |
5386 | nfs4_stateid_copy(&calldata->lsp->ls_stateid, |
5387 | &calldata->res.stateid); | |
26e976a8 | 5388 | renew_lease(calldata->server, calldata->timestamp); |
faf5f49c | 5389 | break; |
9e33bed5 TM |
5390 | case -NFS4ERR_BAD_STATEID: |
5391 | case -NFS4ERR_OLD_STATEID: | |
faf5f49c TM |
5392 | case -NFS4ERR_STALE_STATEID: |
5393 | case -NFS4ERR_EXPIRED: | |
faf5f49c TM |
5394 | break; |
5395 | default: | |
8478eaa1 N |
5396 | if (nfs4_async_handle_error(task, calldata->server, |
5397 | NULL, NULL) == -EAGAIN) | |
d00c5d43 | 5398 | rpc_restart_call_prepare(task); |
faf5f49c | 5399 | } |
2b1bc308 | 5400 | nfs_release_seqid(calldata->arg.seqid); |
faf5f49c TM |
5401 | } |
5402 | ||
4ce70ada | 5403 | static void nfs4_locku_prepare(struct rpc_task *task, void *data) |
faf5f49c | 5404 | { |
4ce70ada | 5405 | struct nfs4_unlockdata *calldata = data; |
faf5f49c | 5406 | |
911d1aaf | 5407 | if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) |
c8da19b9 | 5408 | goto out_wait; |
795a88c9 | 5409 | if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) { |
963d8fe5 | 5410 | /* Note: exit _without_ running nfs4_locku_done */ |
c8da19b9 | 5411 | goto out_no_action; |
faf5f49c | 5412 | } |
26e976a8 | 5413 | calldata->timestamp = jiffies; |
035168ab | 5414 | if (nfs4_setup_sequence(calldata->server, |
a893693c | 5415 | &calldata->arg.seq_args, |
2240a9e2 TM |
5416 | &calldata->res.seq_res, |
5417 | task) != 0) | |
5418 | nfs_release_seqid(calldata->arg.seqid); | |
c8da19b9 TM |
5419 | return; |
5420 | out_no_action: | |
5421 | task->tk_action = NULL; | |
5422 | out_wait: | |
5423 | nfs4_sequence_done(task, &calldata->res.seq_res); | |
faf5f49c TM |
5424 | } |
5425 | ||
963d8fe5 | 5426 | static const struct rpc_call_ops nfs4_locku_ops = { |
4ce70ada | 5427 | .rpc_call_prepare = nfs4_locku_prepare, |
963d8fe5 | 5428 | .rpc_call_done = nfs4_locku_done, |
06f814a3 | 5429 | .rpc_release = nfs4_locku_release_calldata, |
963d8fe5 TM |
5430 | }; |
5431 | ||
a5d16a4d TM |
5432 | static struct rpc_task *nfs4_do_unlck(struct file_lock *fl, |
5433 | struct nfs_open_context *ctx, | |
5434 | struct nfs4_lock_state *lsp, | |
5435 | struct nfs_seqid *seqid) | |
5436 | { | |
5437 | struct nfs4_unlockdata *data; | |
5138fde0 TM |
5438 | struct rpc_message msg = { |
5439 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU], | |
5440 | .rpc_cred = ctx->cred, | |
5441 | }; | |
c970aa85 TM |
5442 | struct rpc_task_setup task_setup_data = { |
5443 | .rpc_client = NFS_CLIENT(lsp->ls_state->inode), | |
5138fde0 | 5444 | .rpc_message = &msg, |
c970aa85 | 5445 | .callback_ops = &nfs4_locku_ops, |
101070ca | 5446 | .workqueue = nfsiod_workqueue, |
c970aa85 TM |
5447 | .flags = RPC_TASK_ASYNC, |
5448 | }; | |
a5d16a4d | 5449 | |
fa940720 WAA |
5450 | nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client, |
5451 | NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg); | |
5452 | ||
137d6aca FF |
5453 | /* Ensure this is an unlock - when canceling a lock, the |
5454 | * canceled lock is passed in, and it won't be an unlock. | |
5455 | */ | |
5456 | fl->fl_type = F_UNLCK; | |
5457 | ||
a5d16a4d TM |
5458 | data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid); |
5459 | if (data == NULL) { | |
5460 | nfs_free_seqid(seqid); | |
5461 | return ERR_PTR(-ENOMEM); | |
5462 | } | |
5463 | ||
a9c92d6b | 5464 | nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1); |
1174dd1f TM |
5465 | msg.rpc_argp = &data->arg; |
5466 | msg.rpc_resp = &data->res; | |
c970aa85 TM |
5467 | task_setup_data.callback_data = data; |
5468 | return rpc_run_task(&task_setup_data); | |
a5d16a4d TM |
5469 | } |
5470 | ||
faf5f49c TM |
5471 | static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request) |
5472 | { | |
65b62a29 TM |
5473 | struct inode *inode = state->inode; |
5474 | struct nfs4_state_owner *sp = state->owner; | |
5475 | struct nfs_inode *nfsi = NFS_I(inode); | |
911d1aaf | 5476 | struct nfs_seqid *seqid; |
1da177e4 | 5477 | struct nfs4_lock_state *lsp; |
06f814a3 TM |
5478 | struct rpc_task *task; |
5479 | int status = 0; | |
536ff0f8 | 5480 | unsigned char fl_flags = request->fl_flags; |
faf5f49c | 5481 | |
8d0a8a9d | 5482 | status = nfs4_set_lock_state(state, request); |
9b073574 TM |
5483 | /* Unlock _before_ we do the RPC call */ |
5484 | request->fl_flags |= FL_EXISTS; | |
65b62a29 TM |
5485 | /* Exclude nfs_delegation_claim_locks() */ |
5486 | mutex_lock(&sp->so_delegreturn_mutex); | |
5487 | /* Exclude nfs4_reclaim_open_stateid() - note nesting! */ | |
19e03c57 TM |
5488 | down_read(&nfsi->rwsem); |
5489 | if (do_vfs_lock(request->fl_file, request) == -ENOENT) { | |
5490 | up_read(&nfsi->rwsem); | |
65b62a29 | 5491 | mutex_unlock(&sp->so_delegreturn_mutex); |
9b073574 | 5492 | goto out; |
19e03c57 TM |
5493 | } |
5494 | up_read(&nfsi->rwsem); | |
65b62a29 | 5495 | mutex_unlock(&sp->so_delegreturn_mutex); |
8d0a8a9d | 5496 | if (status != 0) |
9b073574 TM |
5497 | goto out; |
5498 | /* Is this a delegated lock? */ | |
8d0a8a9d | 5499 | lsp = request->fl_u.nfs4_fl.owner; |
c5a2a15f TM |
5500 | if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0) |
5501 | goto out; | |
8535b2be | 5502 | seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL); |
9b073574 | 5503 | status = -ENOMEM; |
911d1aaf | 5504 | if (seqid == NULL) |
9b073574 | 5505 | goto out; |
cd3758e3 | 5506 | task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid); |
a5d16a4d TM |
5507 | status = PTR_ERR(task); |
5508 | if (IS_ERR(task)) | |
9b073574 | 5509 | goto out; |
a5d16a4d | 5510 | status = nfs4_wait_for_completion_rpc_task(task); |
e6b3c4db | 5511 | rpc_put_task(task); |
9b073574 | 5512 | out: |
536ff0f8 | 5513 | request->fl_flags = fl_flags; |
d1b748a5 | 5514 | trace_nfs4_unlock(request, state, F_SETLK, status); |
1da177e4 LT |
5515 | return status; |
5516 | } | |
5517 | ||
a5d16a4d TM |
5518 | struct nfs4_lockdata { |
5519 | struct nfs_lock_args arg; | |
5520 | struct nfs_lock_res res; | |
5521 | struct nfs4_lock_state *lsp; | |
5522 | struct nfs_open_context *ctx; | |
5523 | struct file_lock fl; | |
26e976a8 | 5524 | unsigned long timestamp; |
a5d16a4d TM |
5525 | int rpc_status; |
5526 | int cancelled; | |
66179efe | 5527 | struct nfs_server *server; |
a5d16a4d TM |
5528 | }; |
5529 | ||
5530 | static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl, | |
8535b2be TM |
5531 | struct nfs_open_context *ctx, struct nfs4_lock_state *lsp, |
5532 | gfp_t gfp_mask) | |
1da177e4 | 5533 | { |
a5d16a4d TM |
5534 | struct nfs4_lockdata *p; |
5535 | struct inode *inode = lsp->ls_state->inode; | |
1da177e4 | 5536 | struct nfs_server *server = NFS_SERVER(inode); |
a5d16a4d | 5537 | |
8535b2be | 5538 | p = kzalloc(sizeof(*p), gfp_mask); |
a5d16a4d TM |
5539 | if (p == NULL) |
5540 | return NULL; | |
5541 | ||
5542 | p->arg.fh = NFS_FH(inode); | |
5543 | p->arg.fl = &p->fl; | |
8535b2be | 5544 | p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask); |
2f74c0a0 TM |
5545 | if (p->arg.open_seqid == NULL) |
5546 | goto out_free; | |
8535b2be | 5547 | p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask); |
a5d16a4d | 5548 | if (p->arg.lock_seqid == NULL) |
2f74c0a0 | 5549 | goto out_free_seqid; |
a5d16a4d | 5550 | p->arg.lock_stateid = &lsp->ls_stateid; |
7539bbab | 5551 | p->arg.lock_owner.clientid = server->nfs_client->cl_clientid; |
48c22eb2 | 5552 | p->arg.lock_owner.id = lsp->ls_seqid.owner_id; |
d035c36c | 5553 | p->arg.lock_owner.s_dev = server->s_dev; |
c1d51931 | 5554 | p->res.lock_seqid = p->arg.lock_seqid; |
a5d16a4d | 5555 | p->lsp = lsp; |
66179efe | 5556 | p->server = server; |
a5d16a4d TM |
5557 | atomic_inc(&lsp->ls_count); |
5558 | p->ctx = get_nfs_open_context(ctx); | |
5559 | memcpy(&p->fl, fl, sizeof(p->fl)); | |
5560 | return p; | |
2f74c0a0 TM |
5561 | out_free_seqid: |
5562 | nfs_free_seqid(p->arg.open_seqid); | |
a5d16a4d TM |
5563 | out_free: |
5564 | kfree(p); | |
5565 | return NULL; | |
5566 | } | |
5567 | ||
5568 | static void nfs4_lock_prepare(struct rpc_task *task, void *calldata) | |
5569 | { | |
5570 | struct nfs4_lockdata *data = calldata; | |
5571 | struct nfs4_state *state = data->lsp->ls_state; | |
06735b34 | 5572 | |
3110ff80 | 5573 | dprintk("%s: begin!\n", __func__); |
2f74c0a0 | 5574 | if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0) |
c8da19b9 | 5575 | goto out_wait; |
a5d16a4d TM |
5576 | /* Do we need to do an open_to_lock_owner? */ |
5577 | if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) { | |
8fe72bac | 5578 | if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) { |
2240a9e2 | 5579 | goto out_release_lock_seqid; |
8fe72bac | 5580 | } |
8e472f33 | 5581 | data->arg.open_stateid = &state->open_stateid; |
a5d16a4d | 5582 | data->arg.new_lock_owner = 1; |
c1d51931 | 5583 | data->res.open_seqid = data->arg.open_seqid; |
2f74c0a0 TM |
5584 | } else |
5585 | data->arg.new_lock_owner = 0; | |
5d422301 TM |
5586 | if (!nfs4_valid_open_stateid(state)) { |
5587 | data->rpc_status = -EBADF; | |
5588 | task->tk_action = NULL; | |
5589 | goto out_release_open_seqid; | |
5590 | } | |
26e976a8 | 5591 | data->timestamp = jiffies; |
035168ab TM |
5592 | if (nfs4_setup_sequence(data->server, |
5593 | &data->arg.seq_args, | |
2240a9e2 | 5594 | &data->res.seq_res, |
d9afbd1b | 5595 | task) == 0) |
66179efe | 5596 | return; |
5d422301 | 5597 | out_release_open_seqid: |
2240a9e2 TM |
5598 | nfs_release_seqid(data->arg.open_seqid); |
5599 | out_release_lock_seqid: | |
5600 | nfs_release_seqid(data->arg.lock_seqid); | |
c8da19b9 TM |
5601 | out_wait: |
5602 | nfs4_sequence_done(task, &data->res.seq_res); | |
8fe72bac | 5603 | dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status); |
b257957e AB |
5604 | } |
5605 | ||
a5d16a4d TM |
5606 | static void nfs4_lock_done(struct rpc_task *task, void *calldata) |
5607 | { | |
5608 | struct nfs4_lockdata *data = calldata; | |
5609 | ||
3110ff80 | 5610 | dprintk("%s: begin!\n", __func__); |
a5d16a4d | 5611 | |
14516c3a TM |
5612 | if (!nfs4_sequence_done(task, &data->res.seq_res)) |
5613 | return; | |
66179efe | 5614 | |
a5d16a4d | 5615 | data->rpc_status = task->tk_status; |
a5d16a4d | 5616 | if (data->arg.new_lock_owner != 0) { |
a5d16a4d TM |
5617 | if (data->rpc_status == 0) |
5618 | nfs_confirm_seqid(&data->lsp->ls_seqid, 0); | |
5619 | else | |
5620 | goto out; | |
5621 | } | |
5622 | if (data->rpc_status == 0) { | |
f597c537 | 5623 | nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid); |
795a88c9 | 5624 | set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags); |
3d4ff43d | 5625 | renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp); |
a5d16a4d | 5626 | } |
a5d16a4d | 5627 | out: |
3110ff80 | 5628 | dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status); |
a5d16a4d TM |
5629 | } |
5630 | ||
5631 | static void nfs4_lock_release(void *calldata) | |
5632 | { | |
5633 | struct nfs4_lockdata *data = calldata; | |
5634 | ||
3110ff80 | 5635 | dprintk("%s: begin!\n", __func__); |
2f74c0a0 | 5636 | nfs_free_seqid(data->arg.open_seqid); |
a5d16a4d TM |
5637 | if (data->cancelled != 0) { |
5638 | struct rpc_task *task; | |
5639 | task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp, | |
5640 | data->arg.lock_seqid); | |
5641 | if (!IS_ERR(task)) | |
bf294b41 | 5642 | rpc_put_task_async(task); |
3110ff80 | 5643 | dprintk("%s: cancelling lock!\n", __func__); |
a5d16a4d TM |
5644 | } else |
5645 | nfs_free_seqid(data->arg.lock_seqid); | |
5646 | nfs4_put_lock_state(data->lsp); | |
5647 | put_nfs_open_context(data->ctx); | |
5648 | kfree(data); | |
3110ff80 | 5649 | dprintk("%s: done!\n", __func__); |
a5d16a4d TM |
5650 | } |
5651 | ||
5652 | static const struct rpc_call_ops nfs4_lock_ops = { | |
5653 | .rpc_call_prepare = nfs4_lock_prepare, | |
5654 | .rpc_call_done = nfs4_lock_done, | |
5655 | .rpc_release = nfs4_lock_release, | |
5656 | }; | |
5657 | ||
2bee72a6 TM |
5658 | static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error) |
5659 | { | |
2bee72a6 TM |
5660 | switch (error) { |
5661 | case -NFS4ERR_ADMIN_REVOKED: | |
5662 | case -NFS4ERR_BAD_STATEID: | |
ecac799a | 5663 | lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; |
2bee72a6 | 5664 | if (new_lock_owner != 0 || |
795a88c9 | 5665 | test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) |
ecac799a | 5666 | nfs4_schedule_stateid_recovery(server, lsp->ls_state); |
a2c0b9e2 TM |
5667 | break; |
5668 | case -NFS4ERR_STALE_STATEID: | |
a2c0b9e2 | 5669 | lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; |
ecac799a TM |
5670 | case -NFS4ERR_EXPIRED: |
5671 | nfs4_schedule_lease_recovery(server->nfs_client); | |
2bee72a6 TM |
5672 | }; |
5673 | } | |
5674 | ||
afe6c27c | 5675 | static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type) |
a5d16a4d TM |
5676 | { |
5677 | struct nfs4_lockdata *data; | |
5678 | struct rpc_task *task; | |
5138fde0 TM |
5679 | struct rpc_message msg = { |
5680 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK], | |
5681 | .rpc_cred = state->owner->so_cred, | |
5682 | }; | |
c970aa85 TM |
5683 | struct rpc_task_setup task_setup_data = { |
5684 | .rpc_client = NFS_CLIENT(state->inode), | |
5138fde0 | 5685 | .rpc_message = &msg, |
c970aa85 | 5686 | .callback_ops = &nfs4_lock_ops, |
101070ca | 5687 | .workqueue = nfsiod_workqueue, |
c970aa85 TM |
5688 | .flags = RPC_TASK_ASYNC, |
5689 | }; | |
a5d16a4d TM |
5690 | int ret; |
5691 | ||
3110ff80 | 5692 | dprintk("%s: begin!\n", __func__); |
cd3758e3 | 5693 | data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file), |
8535b2be TM |
5694 | fl->fl_u.nfs4_fl.owner, |
5695 | recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS); | |
a5d16a4d TM |
5696 | if (data == NULL) |
5697 | return -ENOMEM; | |
5698 | if (IS_SETLKW(cmd)) | |
5699 | data->arg.block = 1; | |
a9c92d6b | 5700 | nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1); |
1174dd1f TM |
5701 | msg.rpc_argp = &data->arg; |
5702 | msg.rpc_resp = &data->res; | |
c970aa85 | 5703 | task_setup_data.callback_data = data; |
8fe72bac TM |
5704 | if (recovery_type > NFS_LOCK_NEW) { |
5705 | if (recovery_type == NFS_LOCK_RECLAIM) | |
5706 | data->arg.reclaim = NFS_LOCK_RECLAIM; | |
5707 | nfs4_set_sequence_privileged(&data->arg.seq_args); | |
5708 | } | |
c970aa85 | 5709 | task = rpc_run_task(&task_setup_data); |
7a1218a2 | 5710 | if (IS_ERR(task)) |
a5d16a4d | 5711 | return PTR_ERR(task); |
a5d16a4d TM |
5712 | ret = nfs4_wait_for_completion_rpc_task(task); |
5713 | if (ret == 0) { | |
5714 | ret = data->rpc_status; | |
2bee72a6 TM |
5715 | if (ret) |
5716 | nfs4_handle_setlk_error(data->server, data->lsp, | |
5717 | data->arg.new_lock_owner, ret); | |
a5d16a4d TM |
5718 | } else |
5719 | data->cancelled = 1; | |
e6b3c4db | 5720 | rpc_put_task(task); |
3110ff80 | 5721 | dprintk("%s: done, ret = %d!\n", __func__, ret); |
a5d16a4d | 5722 | return ret; |
1da177e4 LT |
5723 | } |
5724 | ||
5725 | static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request) | |
5726 | { | |
202b50dc | 5727 | struct nfs_server *server = NFS_SERVER(state->inode); |
05ffe24f TM |
5728 | struct nfs4_exception exception = { |
5729 | .inode = state->inode, | |
5730 | }; | |
202b50dc TM |
5731 | int err; |
5732 | ||
5733 | do { | |
42a2d13e TM |
5734 | /* Cache the lock if possible... */ |
5735 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) | |
5736 | return 0; | |
afe6c27c | 5737 | err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM); |
d1b748a5 | 5738 | trace_nfs4_lock_reclaim(request, state, F_SETLK, err); |
168667c4 | 5739 | if (err != -NFS4ERR_DELAY) |
202b50dc TM |
5740 | break; |
5741 | nfs4_handle_exception(server, err, &exception); | |
5742 | } while (exception.retry); | |
5743 | return err; | |
1da177e4 LT |
5744 | } |
5745 | ||
5746 | static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request) | |
5747 | { | |
202b50dc | 5748 | struct nfs_server *server = NFS_SERVER(state->inode); |
05ffe24f TM |
5749 | struct nfs4_exception exception = { |
5750 | .inode = state->inode, | |
5751 | }; | |
202b50dc TM |
5752 | int err; |
5753 | ||
6bfc93ef TM |
5754 | err = nfs4_set_lock_state(state, request); |
5755 | if (err != 0) | |
5756 | return err; | |
f6de7a39 | 5757 | if (!recover_lost_locks) { |
ef1820f9 N |
5758 | set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags); |
5759 | return 0; | |
5760 | } | |
202b50dc | 5761 | do { |
42a2d13e TM |
5762 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) |
5763 | return 0; | |
afe6c27c | 5764 | err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED); |
d1b748a5 | 5765 | trace_nfs4_lock_expired(request, state, F_SETLK, err); |
a9ed2e25 TM |
5766 | switch (err) { |
5767 | default: | |
5768 | goto out; | |
5769 | case -NFS4ERR_GRACE: | |
5770 | case -NFS4ERR_DELAY: | |
5771 | nfs4_handle_exception(server, err, &exception); | |
5772 | err = 0; | |
5773 | } | |
202b50dc | 5774 | } while (exception.retry); |
a9ed2e25 | 5775 | out: |
202b50dc | 5776 | return err; |
1da177e4 LT |
5777 | } |
5778 | ||
f062eb6c | 5779 | #if defined(CONFIG_NFS_V4_1) |
3e60ffdd CL |
5780 | /** |
5781 | * nfs41_check_expired_locks - possibly free a lock stateid | |
5782 | * | |
5783 | * @state: NFSv4 state for an inode | |
5784 | * | |
5785 | * Returns NFS_OK if recovery for this stateid is now finished. | |
5786 | * Otherwise a negative NFS4ERR value is returned. | |
5787 | */ | |
b01dd1d8 | 5788 | static int nfs41_check_expired_locks(struct nfs4_state *state) |
f062eb6c | 5789 | { |
eb64cf96 | 5790 | int status, ret = -NFS4ERR_BAD_STATEID; |
b01dd1d8 | 5791 | struct nfs4_lock_state *lsp; |
f062eb6c BS |
5792 | struct nfs_server *server = NFS_SERVER(state->inode); |
5793 | ||
b01dd1d8 | 5794 | list_for_each_entry(lsp, &state->lock_states, ls_locks) { |
795a88c9 | 5795 | if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) { |
ab7cb0df TM |
5796 | struct rpc_cred *cred = lsp->ls_state->owner->so_cred; |
5797 | ||
5798 | status = nfs41_test_stateid(server, | |
5799 | &lsp->ls_stateid, | |
5800 | cred); | |
08cb47fa | 5801 | trace_nfs4_test_lock_stateid(state, lsp, status); |
b01dd1d8 | 5802 | if (status != NFS_OK) { |
3e60ffdd CL |
5803 | /* Free the stateid unless the server |
5804 | * informs us the stateid is unrecognized. */ | |
89af2739 CL |
5805 | if (status != -NFS4ERR_BAD_STATEID) |
5806 | nfs41_free_stateid(server, | |
ab7cb0df TM |
5807 | &lsp->ls_stateid, |
5808 | cred); | |
795a88c9 | 5809 | clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags); |
b01dd1d8 BS |
5810 | ret = status; |
5811 | } | |
5812 | } | |
5813 | }; | |
5814 | ||
5815 | return ret; | |
5816 | } | |
5817 | ||
5818 | static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request) | |
5819 | { | |
5820 | int status = NFS_OK; | |
5821 | ||
5822 | if (test_bit(LK_STATE_IN_USE, &state->flags)) | |
5823 | status = nfs41_check_expired_locks(state); | |
eb64cf96 CL |
5824 | if (status != NFS_OK) |
5825 | status = nfs4_lock_expired(state, request); | |
5826 | return status; | |
f062eb6c BS |
5827 | } |
5828 | #endif | |
5829 | ||
1da177e4 LT |
5830 | static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
5831 | { | |
9a99af49 | 5832 | struct nfs4_state_owner *sp = state->owner; |
19e03c57 | 5833 | struct nfs_inode *nfsi = NFS_I(state->inode); |
01c3b861 | 5834 | unsigned char fl_flags = request->fl_flags; |
9a99af49 | 5835 | unsigned int seq; |
8e469ebd | 5836 | int status = -ENOLCK; |
1da177e4 | 5837 | |
8e469ebd TM |
5838 | if ((fl_flags & FL_POSIX) && |
5839 | !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags)) | |
5840 | goto out; | |
6bfc93ef | 5841 | /* Is this a delegated open? */ |
6bfc93ef TM |
5842 | status = nfs4_set_lock_state(state, request); |
5843 | if (status != 0) | |
5844 | goto out; | |
01c3b861 TM |
5845 | request->fl_flags |= FL_ACCESS; |
5846 | status = do_vfs_lock(request->fl_file, request); | |
5847 | if (status < 0) | |
5848 | goto out; | |
19e03c57 | 5849 | down_read(&nfsi->rwsem); |
01c3b861 | 5850 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { |
01c3b861 | 5851 | /* Yes: cache locks! */ |
01c3b861 | 5852 | /* ...but avoid races with delegation recall... */ |
19e03c57 TM |
5853 | request->fl_flags = fl_flags & ~FL_SLEEP; |
5854 | status = do_vfs_lock(request->fl_file, request); | |
5855 | goto out_unlock; | |
01c3b861 | 5856 | } |
9a99af49 TM |
5857 | seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); |
5858 | up_read(&nfsi->rwsem); | |
afe6c27c | 5859 | status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW); |
6bfc93ef | 5860 | if (status != 0) |
9a99af49 TM |
5861 | goto out; |
5862 | down_read(&nfsi->rwsem); | |
5863 | if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) { | |
5864 | status = -NFS4ERR_DELAY; | |
01c3b861 | 5865 | goto out_unlock; |
9a99af49 | 5866 | } |
6bfc93ef | 5867 | /* Note: we always want to sleep here! */ |
01c3b861 | 5868 | request->fl_flags = fl_flags | FL_SLEEP; |
6bfc93ef | 5869 | if (do_vfs_lock(request->fl_file, request) < 0) |
a030889a WAA |
5870 | printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock " |
5871 | "manager!\n", __func__); | |
01c3b861 | 5872 | out_unlock: |
19e03c57 | 5873 | up_read(&nfsi->rwsem); |
01c3b861 TM |
5874 | out: |
5875 | request->fl_flags = fl_flags; | |
1da177e4 LT |
5876 | return status; |
5877 | } | |
5878 | ||
5879 | static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) | |
5880 | { | |
a1d0b5ee TM |
5881 | struct nfs4_exception exception = { |
5882 | .state = state, | |
05ffe24f | 5883 | .inode = state->inode, |
a1d0b5ee | 5884 | }; |
1da177e4 LT |
5885 | int err; |
5886 | ||
5887 | do { | |
965b5d67 | 5888 | err = _nfs4_proc_setlk(state, cmd, request); |
d1b748a5 | 5889 | trace_nfs4_set_lock(request, state, cmd, err); |
965b5d67 TM |
5890 | if (err == -NFS4ERR_DENIED) |
5891 | err = -EAGAIN; | |
1da177e4 | 5892 | err = nfs4_handle_exception(NFS_SERVER(state->inode), |
965b5d67 | 5893 | err, &exception); |
1da177e4 LT |
5894 | } while (exception.retry); |
5895 | return err; | |
5896 | } | |
5897 | ||
5898 | static int | |
5899 | nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request) | |
5900 | { | |
5901 | struct nfs_open_context *ctx; | |
5902 | struct nfs4_state *state; | |
5903 | unsigned long timeout = NFS4_LOCK_MINTIMEOUT; | |
5904 | int status; | |
5905 | ||
5906 | /* verify open state */ | |
cd3758e3 | 5907 | ctx = nfs_file_open_context(filp); |
1da177e4 LT |
5908 | state = ctx->state; |
5909 | ||
5910 | if (request->fl_start < 0 || request->fl_end < 0) | |
5911 | return -EINVAL; | |
5912 | ||
d953126a TM |
5913 | if (IS_GETLK(cmd)) { |
5914 | if (state != NULL) | |
5915 | return nfs4_proc_getlk(state, F_GETLK, request); | |
5916 | return 0; | |
5917 | } | |
1da177e4 LT |
5918 | |
5919 | if (!(IS_SETLK(cmd) || IS_SETLKW(cmd))) | |
5920 | return -EINVAL; | |
5921 | ||
d953126a TM |
5922 | if (request->fl_type == F_UNLCK) { |
5923 | if (state != NULL) | |
5924 | return nfs4_proc_unlck(state, cmd, request); | |
5925 | return 0; | |
5926 | } | |
1da177e4 | 5927 | |
d953126a TM |
5928 | if (state == NULL) |
5929 | return -ENOLCK; | |
55725513 TM |
5930 | /* |
5931 | * Don't rely on the VFS having checked the file open mode, | |
5932 | * since it won't do this for flock() locks. | |
5933 | */ | |
f44106e2 | 5934 | switch (request->fl_type) { |
55725513 TM |
5935 | case F_RDLCK: |
5936 | if (!(filp->f_mode & FMODE_READ)) | |
5937 | return -EBADF; | |
5938 | break; | |
5939 | case F_WRLCK: | |
5940 | if (!(filp->f_mode & FMODE_WRITE)) | |
5941 | return -EBADF; | |
5942 | } | |
5943 | ||
1da177e4 LT |
5944 | do { |
5945 | status = nfs4_proc_setlk(state, cmd, request); | |
5946 | if ((status != -EAGAIN) || IS_SETLK(cmd)) | |
5947 | break; | |
5948 | timeout = nfs4_set_lock_task_retry(timeout); | |
5949 | status = -ERESTARTSYS; | |
5950 | if (signalled()) | |
5951 | break; | |
5952 | } while(status < 0); | |
1da177e4 LT |
5953 | return status; |
5954 | } | |
5955 | ||
db4f2e63 | 5956 | int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid) |
888e694c TM |
5957 | { |
5958 | struct nfs_server *server = NFS_SERVER(state->inode); | |
888e694c TM |
5959 | int err; |
5960 | ||
5961 | err = nfs4_set_lock_state(state, fl); | |
5962 | if (err != 0) | |
db4f2e63 | 5963 | return err; |
4a706fa0 | 5964 | err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW); |
db4f2e63 | 5965 | return nfs4_handle_delegation_recall_error(server, state, stateid, err); |
888e694c | 5966 | } |
6b3b5496 | 5967 | |
cf470c3e TM |
5968 | struct nfs_release_lockowner_data { |
5969 | struct nfs4_lock_state *lsp; | |
5ae67c4f | 5970 | struct nfs_server *server; |
cf470c3e | 5971 | struct nfs_release_lockowner_args args; |
b7e63a10 | 5972 | struct nfs_release_lockowner_res res; |
60ea6812 | 5973 | unsigned long timestamp; |
cf470c3e TM |
5974 | }; |
5975 | ||
fbd4bfd1 CL |
5976 | static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata) |
5977 | { | |
5978 | struct nfs_release_lockowner_data *data = calldata; | |
5b53dc88 | 5979 | struct nfs_server *server = data->server; |
cb04ad2a PT |
5980 | nfs40_setup_sequence(server->nfs_client->cl_slot_tbl, |
5981 | &data->args.seq_args, &data->res.seq_res, task); | |
5b53dc88 | 5982 | data->args.lock_owner.clientid = server->nfs_client->cl_clientid; |
60ea6812 | 5983 | data->timestamp = jiffies; |
fbd4bfd1 CL |
5984 | } |
5985 | ||
5986 | static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata) | |
5987 | { | |
5988 | struct nfs_release_lockowner_data *data = calldata; | |
60ea6812 CL |
5989 | struct nfs_server *server = data->server; |
5990 | ||
b7e63a10 | 5991 | nfs40_sequence_done(task, &data->res.seq_res); |
60ea6812 CL |
5992 | |
5993 | switch (task->tk_status) { | |
5994 | case 0: | |
5995 | renew_lease(server, data->timestamp); | |
5996 | break; | |
5997 | case -NFS4ERR_STALE_CLIENTID: | |
5998 | case -NFS4ERR_EXPIRED: | |
5b53dc88 KM |
5999 | nfs4_schedule_lease_recovery(server->nfs_client); |
6000 | break; | |
60ea6812 CL |
6001 | case -NFS4ERR_LEASE_MOVED: |
6002 | case -NFS4ERR_DELAY: | |
8478eaa1 N |
6003 | if (nfs4_async_handle_error(task, server, |
6004 | NULL, NULL) == -EAGAIN) | |
60ea6812 CL |
6005 | rpc_restart_call_prepare(task); |
6006 | } | |
fbd4bfd1 CL |
6007 | } |
6008 | ||
d3c7b7cc TM |
6009 | static void nfs4_release_lockowner_release(void *calldata) |
6010 | { | |
cf470c3e | 6011 | struct nfs_release_lockowner_data *data = calldata; |
5ae67c4f | 6012 | nfs4_free_lock_state(data->server, data->lsp); |
d3c7b7cc TM |
6013 | kfree(calldata); |
6014 | } | |
6015 | ||
17280175 | 6016 | static const struct rpc_call_ops nfs4_release_lockowner_ops = { |
fbd4bfd1 CL |
6017 | .rpc_call_prepare = nfs4_release_lockowner_prepare, |
6018 | .rpc_call_done = nfs4_release_lockowner_done, | |
d3c7b7cc TM |
6019 | .rpc_release = nfs4_release_lockowner_release, |
6020 | }; | |
6021 | ||
f1cdae87 JL |
6022 | static void |
6023 | nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp) | |
d3c7b7cc | 6024 | { |
cf470c3e | 6025 | struct nfs_release_lockowner_data *data; |
d3c7b7cc TM |
6026 | struct rpc_message msg = { |
6027 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER], | |
6028 | }; | |
6029 | ||
6030 | if (server->nfs_client->cl_mvops->minor_version != 0) | |
f1cdae87 | 6031 | return; |
fbd4bfd1 | 6032 | |
cf470c3e TM |
6033 | data = kmalloc(sizeof(*data), GFP_NOFS); |
6034 | if (!data) | |
f1cdae87 | 6035 | return; |
cf470c3e | 6036 | data->lsp = lsp; |
5ae67c4f | 6037 | data->server = server; |
cf470c3e TM |
6038 | data->args.lock_owner.clientid = server->nfs_client->cl_clientid; |
6039 | data->args.lock_owner.id = lsp->ls_seqid.owner_id; | |
6040 | data->args.lock_owner.s_dev = server->s_dev; | |
fbd4bfd1 | 6041 | |
cf470c3e | 6042 | msg.rpc_argp = &data->args; |
b7e63a10 TM |
6043 | msg.rpc_resp = &data->res; |
6044 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0); | |
cf470c3e | 6045 | rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data); |
d3c7b7cc TM |
6046 | } |
6047 | ||
aa1870af BF |
6048 | #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl" |
6049 | ||
64c2ce8b AK |
6050 | static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key, |
6051 | const void *buf, size_t buflen, | |
6052 | int flags, int type) | |
6b3b5496 | 6053 | { |
64c2ce8b AK |
6054 | if (strcmp(key, "") != 0) |
6055 | return -EINVAL; | |
4b580ee3 | 6056 | |
64c2ce8b | 6057 | return nfs4_proc_set_acl(dentry->d_inode, buf, buflen); |
6b3b5496 BF |
6058 | } |
6059 | ||
64c2ce8b AK |
6060 | static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key, |
6061 | void *buf, size_t buflen, int type) | |
6b3b5496 | 6062 | { |
64c2ce8b AK |
6063 | if (strcmp(key, "") != 0) |
6064 | return -EINVAL; | |
aa1870af | 6065 | |
64c2ce8b | 6066 | return nfs4_proc_get_acl(dentry->d_inode, buf, buflen); |
6b3b5496 BF |
6067 | } |
6068 | ||
64c2ce8b AK |
6069 | static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list, |
6070 | size_t list_len, const char *name, | |
6071 | size_t name_len, int type) | |
6b3b5496 | 6072 | { |
64c2ce8b | 6073 | size_t len = sizeof(XATTR_NAME_NFSV4_ACL); |
6b3b5496 | 6074 | |
096455a2 BF |
6075 | if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode))) |
6076 | return 0; | |
64c2ce8b AK |
6077 | |
6078 | if (list && len <= list_len) | |
6079 | memcpy(list, XATTR_NAME_NFSV4_ACL, len); | |
aa1870af | 6080 | return len; |
6b3b5496 BF |
6081 | } |
6082 | ||
c9bccef6 DQ |
6083 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
6084 | static inline int nfs4_server_supports_labels(struct nfs_server *server) | |
6085 | { | |
6086 | return server->caps & NFS_CAP_SECURITY_LABEL; | |
6087 | } | |
6088 | ||
6089 | static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key, | |
6090 | const void *buf, size_t buflen, | |
6091 | int flags, int type) | |
6092 | { | |
6093 | if (security_ismaclabel(key)) | |
6094 | return nfs4_set_security_label(dentry, buf, buflen); | |
6095 | ||
6096 | return -EOPNOTSUPP; | |
6097 | } | |
6098 | ||
6099 | static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key, | |
6100 | void *buf, size_t buflen, int type) | |
6101 | { | |
6102 | if (security_ismaclabel(key)) | |
6103 | return nfs4_get_security_label(dentry->d_inode, buf, buflen); | |
6104 | return -EOPNOTSUPP; | |
6105 | } | |
6106 | ||
6107 | static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list, | |
6108 | size_t list_len, const char *name, | |
6109 | size_t name_len, int type) | |
6110 | { | |
6111 | size_t len = 0; | |
6112 | ||
6113 | if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) { | |
6114 | len = security_inode_listsecurity(dentry->d_inode, NULL, 0); | |
6115 | if (list && len <= list_len) | |
6116 | security_inode_listsecurity(dentry->d_inode, list, len); | |
6117 | } | |
6118 | return len; | |
6119 | } | |
6120 | ||
6121 | static const struct xattr_handler nfs4_xattr_nfs4_label_handler = { | |
6122 | .prefix = XATTR_SECURITY_PREFIX, | |
6123 | .list = nfs4_xattr_list_nfs4_label, | |
6124 | .get = nfs4_xattr_get_nfs4_label, | |
6125 | .set = nfs4_xattr_set_nfs4_label, | |
6126 | }; | |
6127 | #endif | |
6128 | ||
6129 | ||
533eb461 AA |
6130 | /* |
6131 | * nfs_fhget will use either the mounted_on_fileid or the fileid | |
6132 | */ | |
69aaaae1 TM |
6133 | static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr) |
6134 | { | |
533eb461 AA |
6135 | if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) || |
6136 | (fattr->valid & NFS_ATTR_FATTR_FILEID)) && | |
6137 | (fattr->valid & NFS_ATTR_FATTR_FSID) && | |
81934ddb | 6138 | (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS))) |
69aaaae1 TM |
6139 | return; |
6140 | ||
6141 | fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | | |
81934ddb | 6142 | NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL; |
69aaaae1 TM |
6143 | fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; |
6144 | fattr->nlink = 2; | |
6145 | } | |
6146 | ||
f05d147f BS |
6147 | static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, |
6148 | const struct qstr *name, | |
6149 | struct nfs4_fs_locations *fs_locations, | |
6150 | struct page *page) | |
683b57b4 TM |
6151 | { |
6152 | struct nfs_server *server = NFS_SERVER(dir); | |
a09df2ca | 6153 | u32 bitmask[3] = { |
361e624f | 6154 | [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, |
683b57b4 TM |
6155 | }; |
6156 | struct nfs4_fs_locations_arg args = { | |
6157 | .dir_fh = NFS_FH(dir), | |
c228fd3a | 6158 | .name = name, |
683b57b4 TM |
6159 | .page = page, |
6160 | .bitmask = bitmask, | |
6161 | }; | |
22958463 BH |
6162 | struct nfs4_fs_locations_res res = { |
6163 | .fs_locations = fs_locations, | |
6164 | }; | |
683b57b4 TM |
6165 | struct rpc_message msg = { |
6166 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], | |
6167 | .rpc_argp = &args, | |
22958463 | 6168 | .rpc_resp = &res, |
683b57b4 TM |
6169 | }; |
6170 | int status; | |
6171 | ||
3110ff80 | 6172 | dprintk("%s: start\n", __func__); |
533eb461 AA |
6173 | |
6174 | /* Ask for the fileid of the absent filesystem if mounted_on_fileid | |
6175 | * is not supported */ | |
6176 | if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID) | |
6177 | bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID; | |
6178 | else | |
6179 | bitmask[0] |= FATTR4_WORD0_FILEID; | |
6180 | ||
c228fd3a | 6181 | nfs_fattr_init(&fs_locations->fattr); |
683b57b4 | 6182 | fs_locations->server = server; |
830b8e33 | 6183 | fs_locations->nlocations = 0; |
f05d147f | 6184 | status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0); |
3110ff80 | 6185 | dprintk("%s: returned status = %d\n", __func__, status); |
683b57b4 TM |
6186 | return status; |
6187 | } | |
6188 | ||
f05d147f BS |
6189 | int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, |
6190 | const struct qstr *name, | |
6191 | struct nfs4_fs_locations *fs_locations, | |
6192 | struct page *page) | |
db0a9593 BS |
6193 | { |
6194 | struct nfs4_exception exception = { }; | |
6195 | int err; | |
6196 | do { | |
078ea3df TM |
6197 | err = _nfs4_proc_fs_locations(client, dir, name, |
6198 | fs_locations, page); | |
6199 | trace_nfs4_get_fs_locations(dir, name, err); | |
6200 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
db0a9593 BS |
6201 | &exception); |
6202 | } while (exception.retry); | |
6203 | return err; | |
6204 | } | |
6205 | ||
b03d735b CL |
6206 | /* |
6207 | * This operation also signals the server that this client is | |
6208 | * performing migration recovery. The server can stop returning | |
6209 | * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is | |
6210 | * appended to this compound to identify the client ID which is | |
6211 | * performing recovery. | |
6212 | */ | |
6213 | static int _nfs40_proc_get_locations(struct inode *inode, | |
6214 | struct nfs4_fs_locations *locations, | |
6215 | struct page *page, struct rpc_cred *cred) | |
6216 | { | |
6217 | struct nfs_server *server = NFS_SERVER(inode); | |
6218 | struct rpc_clnt *clnt = server->client; | |
6219 | u32 bitmask[2] = { | |
6220 | [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, | |
6221 | }; | |
6222 | struct nfs4_fs_locations_arg args = { | |
6223 | .clientid = server->nfs_client->cl_clientid, | |
6224 | .fh = NFS_FH(inode), | |
6225 | .page = page, | |
6226 | .bitmask = bitmask, | |
6227 | .migration = 1, /* skip LOOKUP */ | |
6228 | .renew = 1, /* append RENEW */ | |
6229 | }; | |
6230 | struct nfs4_fs_locations_res res = { | |
6231 | .fs_locations = locations, | |
6232 | .migration = 1, | |
6233 | .renew = 1, | |
6234 | }; | |
6235 | struct rpc_message msg = { | |
6236 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], | |
6237 | .rpc_argp = &args, | |
6238 | .rpc_resp = &res, | |
6239 | .rpc_cred = cred, | |
6240 | }; | |
6241 | unsigned long now = jiffies; | |
6242 | int status; | |
6243 | ||
6244 | nfs_fattr_init(&locations->fattr); | |
6245 | locations->server = server; | |
6246 | locations->nlocations = 0; | |
6247 | ||
6248 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0); | |
6249 | nfs4_set_sequence_privileged(&args.seq_args); | |
6250 | status = nfs4_call_sync_sequence(clnt, server, &msg, | |
6251 | &args.seq_args, &res.seq_res); | |
6252 | if (status) | |
6253 | return status; | |
6254 | ||
6255 | renew_lease(server, now); | |
6256 | return 0; | |
6257 | } | |
6258 | ||
6259 | #ifdef CONFIG_NFS_V4_1 | |
6260 | ||
6261 | /* | |
6262 | * This operation also signals the server that this client is | |
6263 | * performing migration recovery. The server can stop asserting | |
6264 | * SEQ4_STATUS_LEASE_MOVED for this client. The client ID | |
6265 | * performing this operation is identified in the SEQUENCE | |
6266 | * operation in this compound. | |
6267 | * | |
6268 | * When the client supports GETATTR(fs_locations_info), it can | |
6269 | * be plumbed in here. | |
6270 | */ | |
6271 | static int _nfs41_proc_get_locations(struct inode *inode, | |
6272 | struct nfs4_fs_locations *locations, | |
6273 | struct page *page, struct rpc_cred *cred) | |
6274 | { | |
6275 | struct nfs_server *server = NFS_SERVER(inode); | |
6276 | struct rpc_clnt *clnt = server->client; | |
6277 | u32 bitmask[2] = { | |
6278 | [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, | |
6279 | }; | |
6280 | struct nfs4_fs_locations_arg args = { | |
6281 | .fh = NFS_FH(inode), | |
6282 | .page = page, | |
6283 | .bitmask = bitmask, | |
6284 | .migration = 1, /* skip LOOKUP */ | |
6285 | }; | |
6286 | struct nfs4_fs_locations_res res = { | |
6287 | .fs_locations = locations, | |
6288 | .migration = 1, | |
6289 | }; | |
6290 | struct rpc_message msg = { | |
6291 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], | |
6292 | .rpc_argp = &args, | |
6293 | .rpc_resp = &res, | |
6294 | .rpc_cred = cred, | |
6295 | }; | |
6296 | int status; | |
6297 | ||
6298 | nfs_fattr_init(&locations->fattr); | |
6299 | locations->server = server; | |
6300 | locations->nlocations = 0; | |
6301 | ||
6302 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0); | |
6303 | nfs4_set_sequence_privileged(&args.seq_args); | |
6304 | status = nfs4_call_sync_sequence(clnt, server, &msg, | |
6305 | &args.seq_args, &res.seq_res); | |
6306 | if (status == NFS4_OK && | |
6307 | res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED) | |
6308 | status = -NFS4ERR_LEASE_MOVED; | |
6309 | return status; | |
6310 | } | |
6311 | ||
6312 | #endif /* CONFIG_NFS_V4_1 */ | |
6313 | ||
6314 | /** | |
6315 | * nfs4_proc_get_locations - discover locations for a migrated FSID | |
6316 | * @inode: inode on FSID that is migrating | |
6317 | * @locations: result of query | |
6318 | * @page: buffer | |
6319 | * @cred: credential to use for this operation | |
6320 | * | |
6321 | * Returns NFS4_OK on success, a negative NFS4ERR status code if the | |
6322 | * operation failed, or a negative errno if a local error occurred. | |
6323 | * | |
6324 | * On success, "locations" is filled in, but if the server has | |
6325 | * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not | |
6326 | * asserted. | |
6327 | * | |
6328 | * -NFS4ERR_LEASE_MOVED is returned if the server still has leases | |
6329 | * from this client that require migration recovery. | |
6330 | */ | |
6331 | int nfs4_proc_get_locations(struct inode *inode, | |
6332 | struct nfs4_fs_locations *locations, | |
6333 | struct page *page, struct rpc_cred *cred) | |
6334 | { | |
6335 | struct nfs_server *server = NFS_SERVER(inode); | |
6336 | struct nfs_client *clp = server->nfs_client; | |
6337 | const struct nfs4_mig_recovery_ops *ops = | |
6338 | clp->cl_mvops->mig_recovery_ops; | |
6339 | struct nfs4_exception exception = { }; | |
6340 | int status; | |
6341 | ||
6342 | dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__, | |
6343 | (unsigned long long)server->fsid.major, | |
6344 | (unsigned long long)server->fsid.minor, | |
6345 | clp->cl_hostname); | |
6346 | nfs_display_fhandle(NFS_FH(inode), __func__); | |
6347 | ||
6348 | do { | |
6349 | status = ops->get_locations(inode, locations, page, cred); | |
6350 | if (status != -NFS4ERR_DELAY) | |
6351 | break; | |
6352 | nfs4_handle_exception(server, status, &exception); | |
6353 | } while (exception.retry); | |
6354 | return status; | |
6355 | } | |
6356 | ||
44c99933 CL |
6357 | /* |
6358 | * This operation also signals the server that this client is | |
6359 | * performing "lease moved" recovery. The server can stop | |
6360 | * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation | |
6361 | * is appended to this compound to identify the client ID which is | |
6362 | * performing recovery. | |
6363 | */ | |
6364 | static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred) | |
6365 | { | |
6366 | struct nfs_server *server = NFS_SERVER(inode); | |
6367 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; | |
6368 | struct rpc_clnt *clnt = server->client; | |
6369 | struct nfs4_fsid_present_arg args = { | |
6370 | .fh = NFS_FH(inode), | |
6371 | .clientid = clp->cl_clientid, | |
6372 | .renew = 1, /* append RENEW */ | |
6373 | }; | |
6374 | struct nfs4_fsid_present_res res = { | |
6375 | .renew = 1, | |
6376 | }; | |
6377 | struct rpc_message msg = { | |
6378 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT], | |
6379 | .rpc_argp = &args, | |
6380 | .rpc_resp = &res, | |
6381 | .rpc_cred = cred, | |
6382 | }; | |
6383 | unsigned long now = jiffies; | |
6384 | int status; | |
6385 | ||
6386 | res.fh = nfs_alloc_fhandle(); | |
6387 | if (res.fh == NULL) | |
6388 | return -ENOMEM; | |
6389 | ||
6390 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0); | |
6391 | nfs4_set_sequence_privileged(&args.seq_args); | |
6392 | status = nfs4_call_sync_sequence(clnt, server, &msg, | |
6393 | &args.seq_args, &res.seq_res); | |
6394 | nfs_free_fhandle(res.fh); | |
6395 | if (status) | |
6396 | return status; | |
6397 | ||
6398 | do_renew_lease(clp, now); | |
6399 | return 0; | |
6400 | } | |
6401 | ||
6402 | #ifdef CONFIG_NFS_V4_1 | |
6403 | ||
6404 | /* | |
6405 | * This operation also signals the server that this client is | |
6406 | * performing "lease moved" recovery. The server can stop asserting | |
6407 | * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing | |
6408 | * this operation is identified in the SEQUENCE operation in this | |
6409 | * compound. | |
6410 | */ | |
6411 | static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred) | |
6412 | { | |
6413 | struct nfs_server *server = NFS_SERVER(inode); | |
6414 | struct rpc_clnt *clnt = server->client; | |
6415 | struct nfs4_fsid_present_arg args = { | |
6416 | .fh = NFS_FH(inode), | |
6417 | }; | |
6418 | struct nfs4_fsid_present_res res = { | |
6419 | }; | |
6420 | struct rpc_message msg = { | |
6421 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT], | |
6422 | .rpc_argp = &args, | |
6423 | .rpc_resp = &res, | |
6424 | .rpc_cred = cred, | |
6425 | }; | |
6426 | int status; | |
6427 | ||
6428 | res.fh = nfs_alloc_fhandle(); | |
6429 | if (res.fh == NULL) | |
6430 | return -ENOMEM; | |
6431 | ||
6432 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0); | |
6433 | nfs4_set_sequence_privileged(&args.seq_args); | |
6434 | status = nfs4_call_sync_sequence(clnt, server, &msg, | |
6435 | &args.seq_args, &res.seq_res); | |
6436 | nfs_free_fhandle(res.fh); | |
6437 | if (status == NFS4_OK && | |
6438 | res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED) | |
6439 | status = -NFS4ERR_LEASE_MOVED; | |
6440 | return status; | |
6441 | } | |
6442 | ||
6443 | #endif /* CONFIG_NFS_V4_1 */ | |
6444 | ||
6445 | /** | |
6446 | * nfs4_proc_fsid_present - Is this FSID present or absent on server? | |
6447 | * @inode: inode on FSID to check | |
6448 | * @cred: credential to use for this operation | |
6449 | * | |
6450 | * Server indicates whether the FSID is present, moved, or not | |
6451 | * recognized. This operation is necessary to clear a LEASE_MOVED | |
6452 | * condition for this client ID. | |
6453 | * | |
6454 | * Returns NFS4_OK if the FSID is present on this server, | |
6455 | * -NFS4ERR_MOVED if the FSID is no longer present, a negative | |
6456 | * NFS4ERR code if some error occurred on the server, or a | |
6457 | * negative errno if a local failure occurred. | |
6458 | */ | |
6459 | int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred) | |
6460 | { | |
6461 | struct nfs_server *server = NFS_SERVER(inode); | |
6462 | struct nfs_client *clp = server->nfs_client; | |
6463 | const struct nfs4_mig_recovery_ops *ops = | |
6464 | clp->cl_mvops->mig_recovery_ops; | |
6465 | struct nfs4_exception exception = { }; | |
6466 | int status; | |
6467 | ||
6468 | dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__, | |
6469 | (unsigned long long)server->fsid.major, | |
6470 | (unsigned long long)server->fsid.minor, | |
6471 | clp->cl_hostname); | |
6472 | nfs_display_fhandle(NFS_FH(inode), __func__); | |
6473 | ||
6474 | do { | |
6475 | status = ops->fsid_present(inode, cred); | |
6476 | if (status != -NFS4ERR_DELAY) | |
6477 | break; | |
6478 | nfs4_handle_exception(server, status, &exception); | |
6479 | } while (exception.retry); | |
6480 | return status; | |
6481 | } | |
6482 | ||
5ec16a85 | 6483 | /** |
a5250def WAA |
6484 | * If 'use_integrity' is true and the state managment nfs_client |
6485 | * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient | |
6486 | * and the machine credential as per RFC3530bis and RFC5661 Security | |
6487 | * Considerations sections. Otherwise, just use the user cred with the | |
6488 | * filesystem's rpc_client. | |
5ec16a85 | 6489 | */ |
a5250def | 6490 | static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity) |
5a5ea0d4 BS |
6491 | { |
6492 | int status; | |
6493 | struct nfs4_secinfo_arg args = { | |
6494 | .dir_fh = NFS_FH(dir), | |
6495 | .name = name, | |
6496 | }; | |
6497 | struct nfs4_secinfo_res res = { | |
6498 | .flavors = flavors, | |
6499 | }; | |
6500 | struct rpc_message msg = { | |
6501 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO], | |
6502 | .rpc_argp = &args, | |
6503 | .rpc_resp = &res, | |
6504 | }; | |
a5250def | 6505 | struct rpc_clnt *clnt = NFS_SERVER(dir)->client; |
7cb852df | 6506 | struct rpc_cred *cred = NULL; |
a5250def WAA |
6507 | |
6508 | if (use_integrity) { | |
6509 | clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient; | |
7cb852df WAA |
6510 | cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client); |
6511 | msg.rpc_cred = cred; | |
a5250def | 6512 | } |
5a5ea0d4 BS |
6513 | |
6514 | dprintk("NFS call secinfo %s\n", name->name); | |
8b5bee2e WAA |
6515 | |
6516 | nfs4_state_protect(NFS_SERVER(dir)->nfs_client, | |
6517 | NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg); | |
6518 | ||
a5250def WAA |
6519 | status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args, |
6520 | &res.seq_res, 0); | |
5a5ea0d4 | 6521 | dprintk("NFS reply secinfo: %d\n", status); |
a5250def | 6522 | |
7cb852df WAA |
6523 | if (cred) |
6524 | put_rpccred(cred); | |
a5250def | 6525 | |
5a5ea0d4 BS |
6526 | return status; |
6527 | } | |
6528 | ||
72de53ec BS |
6529 | int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, |
6530 | struct nfs4_secinfo_flavors *flavors) | |
5a5ea0d4 BS |
6531 | { |
6532 | struct nfs4_exception exception = { }; | |
6533 | int err; | |
6534 | do { | |
a5250def WAA |
6535 | err = -NFS4ERR_WRONGSEC; |
6536 | ||
6537 | /* try to use integrity protection with machine cred */ | |
6538 | if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client)) | |
6539 | err = _nfs4_proc_secinfo(dir, name, flavors, true); | |
6540 | ||
6541 | /* | |
6542 | * if unable to use integrity protection, or SECINFO with | |
6543 | * integrity protection returns NFS4ERR_WRONGSEC (which is | |
6544 | * disallowed by spec, but exists in deployed servers) use | |
6545 | * the current filesystem's rpc_client and the user cred. | |
6546 | */ | |
6547 | if (err == -NFS4ERR_WRONGSEC) | |
6548 | err = _nfs4_proc_secinfo(dir, name, flavors, false); | |
6549 | ||
078ea3df TM |
6550 | trace_nfs4_secinfo(dir, name, err); |
6551 | err = nfs4_handle_exception(NFS_SERVER(dir), err, | |
5a5ea0d4 BS |
6552 | &exception); |
6553 | } while (exception.retry); | |
6554 | return err; | |
6555 | } | |
6556 | ||
557134a3 | 6557 | #ifdef CONFIG_NFS_V4_1 |
357f54d6 AA |
6558 | /* |
6559 | * Check the exchange flags returned by the server for invalid flags, having | |
6560 | * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or | |
6561 | * DS flags set. | |
6562 | */ | |
6563 | static int nfs4_check_cl_exchange_flags(u32 flags) | |
6564 | { | |
6565 | if (flags & ~EXCHGID4_FLAG_MASK_R) | |
6566 | goto out_inval; | |
6567 | if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) && | |
6568 | (flags & EXCHGID4_FLAG_USE_NON_PNFS)) | |
6569 | goto out_inval; | |
6570 | if (!(flags & (EXCHGID4_FLAG_MASK_PNFS))) | |
6571 | goto out_inval; | |
6572 | return NFS_OK; | |
6573 | out_inval: | |
6574 | return -NFS4ERR_INVAL; | |
6575 | } | |
6576 | ||
78fe0f41 | 6577 | static bool |
79d4e1f0 CL |
6578 | nfs41_same_server_scope(struct nfs41_server_scope *a, |
6579 | struct nfs41_server_scope *b) | |
78fe0f41 WAA |
6580 | { |
6581 | if (a->server_scope_sz == b->server_scope_sz && | |
6582 | memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0) | |
6583 | return true; | |
6584 | ||
6585 | return false; | |
6586 | } | |
6587 | ||
7c44f1ae WAA |
6588 | /* |
6589 | * nfs4_proc_bind_conn_to_session() | |
6590 | * | |
6591 | * The 4.1 client currently uses the same TCP connection for the | |
6592 | * fore and backchannel. | |
6593 | */ | |
2cf047c9 | 6594 | int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred) |
7c44f1ae WAA |
6595 | { |
6596 | int status; | |
6597 | struct nfs41_bind_conn_to_session_res res; | |
6598 | struct rpc_message msg = { | |
6599 | .rpc_proc = | |
6600 | &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION], | |
6601 | .rpc_argp = clp, | |
6602 | .rpc_resp = &res, | |
2cf047c9 | 6603 | .rpc_cred = cred, |
7c44f1ae WAA |
6604 | }; |
6605 | ||
6606 | dprintk("--> %s\n", __func__); | |
7c44f1ae WAA |
6607 | |
6608 | res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS); | |
6609 | if (unlikely(res.session == NULL)) { | |
6610 | status = -ENOMEM; | |
6611 | goto out; | |
6612 | } | |
6613 | ||
6614 | status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); | |
c6d01c6f | 6615 | trace_nfs4_bind_conn_to_session(clp, status); |
7c44f1ae WAA |
6616 | if (status == 0) { |
6617 | if (memcmp(res.session->sess_id.data, | |
6618 | clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) { | |
6619 | dprintk("NFS: %s: Session ID mismatch\n", __func__); | |
6620 | status = -EIO; | |
6621 | goto out_session; | |
6622 | } | |
6623 | if (res.dir != NFS4_CDFS4_BOTH) { | |
6624 | dprintk("NFS: %s: Unexpected direction from server\n", | |
6625 | __func__); | |
6626 | status = -EIO; | |
6627 | goto out_session; | |
6628 | } | |
6629 | if (res.use_conn_in_rdma_mode) { | |
6630 | dprintk("NFS: %s: Server returned RDMA mode = true\n", | |
6631 | __func__); | |
6632 | status = -EIO; | |
6633 | goto out_session; | |
6634 | } | |
6635 | } | |
6636 | out_session: | |
6637 | kfree(res.session); | |
6638 | out: | |
6639 | dprintk("<-- %s status= %d\n", __func__, status); | |
6640 | return status; | |
6641 | } | |
6642 | ||
99fe60d0 | 6643 | /* |
fa940720 WAA |
6644 | * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map |
6645 | * and operations we'd like to see to enable certain features in the allow map | |
2031cd1a WAA |
6646 | */ |
6647 | static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = { | |
6648 | .how = SP4_MACH_CRED, | |
6649 | .enforce.u.words = { | |
6650 | [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) | | |
6651 | 1 << (OP_EXCHANGE_ID - 32) | | |
6652 | 1 << (OP_CREATE_SESSION - 32) | | |
6653 | 1 << (OP_DESTROY_SESSION - 32) | | |
6654 | 1 << (OP_DESTROY_CLIENTID - 32) | |
fa940720 WAA |
6655 | }, |
6656 | .allow.u.words = { | |
6657 | [0] = 1 << (OP_CLOSE) | | |
a0279625 WAA |
6658 | 1 << (OP_LOCKU) | |
6659 | 1 << (OP_COMMIT), | |
8b5bee2e | 6660 | [1] = 1 << (OP_SECINFO - 32) | |
3787d506 WAA |
6661 | 1 << (OP_SECINFO_NO_NAME - 32) | |
6662 | 1 << (OP_TEST_STATEID - 32) | | |
a0279625 WAA |
6663 | 1 << (OP_FREE_STATEID - 32) | |
6664 | 1 << (OP_WRITE - 32) | |
2031cd1a WAA |
6665 | } |
6666 | }; | |
6667 | ||
6668 | /* | |
6669 | * Select the state protection mode for client `clp' given the server results | |
6670 | * from exchange_id in `sp'. | |
99fe60d0 | 6671 | * |
2031cd1a WAA |
6672 | * Returns 0 on success, negative errno otherwise. |
6673 | */ | |
6674 | static int nfs4_sp4_select_mode(struct nfs_client *clp, | |
6675 | struct nfs41_state_protection *sp) | |
6676 | { | |
6677 | static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = { | |
6678 | [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) | | |
6679 | 1 << (OP_EXCHANGE_ID - 32) | | |
6680 | 1 << (OP_CREATE_SESSION - 32) | | |
6681 | 1 << (OP_DESTROY_SESSION - 32) | | |
6682 | 1 << (OP_DESTROY_CLIENTID - 32) | |
6683 | }; | |
6684 | unsigned int i; | |
6685 | ||
6686 | if (sp->how == SP4_MACH_CRED) { | |
6687 | /* Print state protect result */ | |
6688 | dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n"); | |
6689 | for (i = 0; i <= LAST_NFS4_OP; i++) { | |
6690 | if (test_bit(i, sp->enforce.u.longs)) | |
6691 | dfprintk(MOUNT, " enforce op %d\n", i); | |
6692 | if (test_bit(i, sp->allow.u.longs)) | |
6693 | dfprintk(MOUNT, " allow op %d\n", i); | |
6694 | } | |
6695 | ||
6696 | /* make sure nothing is on enforce list that isn't supported */ | |
6697 | for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) { | |
6698 | if (sp->enforce.u.words[i] & ~supported_enforce[i]) { | |
6699 | dfprintk(MOUNT, "sp4_mach_cred: disabled\n"); | |
6700 | return -EINVAL; | |
6701 | } | |
6702 | } | |
6703 | ||
6704 | /* | |
6705 | * Minimal mode - state operations are allowed to use machine | |
6706 | * credential. Note this already happens by default, so the | |
6707 | * client doesn't have to do anything more than the negotiation. | |
6708 | * | |
6709 | * NOTE: we don't care if EXCHANGE_ID is in the list - | |
6710 | * we're already using the machine cred for exchange_id | |
6711 | * and will never use a different cred. | |
6712 | */ | |
6713 | if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) && | |
6714 | test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) && | |
6715 | test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) && | |
6716 | test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) { | |
6717 | dfprintk(MOUNT, "sp4_mach_cred:\n"); | |
6718 | dfprintk(MOUNT, " minimal mode enabled\n"); | |
6719 | set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags); | |
6720 | } else { | |
6721 | dfprintk(MOUNT, "sp4_mach_cred: disabled\n"); | |
6722 | return -EINVAL; | |
6723 | } | |
fa940720 WAA |
6724 | |
6725 | if (test_bit(OP_CLOSE, sp->allow.u.longs) && | |
6726 | test_bit(OP_LOCKU, sp->allow.u.longs)) { | |
6727 | dfprintk(MOUNT, " cleanup mode enabled\n"); | |
6728 | set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags); | |
6729 | } | |
8b5bee2e WAA |
6730 | |
6731 | if (test_bit(OP_SECINFO, sp->allow.u.longs) && | |
6732 | test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) { | |
6733 | dfprintk(MOUNT, " secinfo mode enabled\n"); | |
6734 | set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags); | |
6735 | } | |
3787d506 WAA |
6736 | |
6737 | if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) && | |
6738 | test_bit(OP_FREE_STATEID, sp->allow.u.longs)) { | |
6739 | dfprintk(MOUNT, " stateid mode enabled\n"); | |
6740 | set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags); | |
6741 | } | |
8c21c62c WAA |
6742 | |
6743 | if (test_bit(OP_WRITE, sp->allow.u.longs)) { | |
6744 | dfprintk(MOUNT, " write mode enabled\n"); | |
6745 | set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags); | |
6746 | } | |
6747 | ||
6748 | if (test_bit(OP_COMMIT, sp->allow.u.longs)) { | |
6749 | dfprintk(MOUNT, " commit mode enabled\n"); | |
6750 | set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags); | |
6751 | } | |
2031cd1a WAA |
6752 | } |
6753 | ||
6754 | return 0; | |
6755 | } | |
6756 | ||
6757 | /* | |
6758 | * _nfs4_proc_exchange_id() | |
6bbb4ae8 | 6759 | * |
2031cd1a | 6760 | * Wrapper for EXCHANGE_ID operation. |
99fe60d0 | 6761 | */ |
2031cd1a WAA |
6762 | static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred, |
6763 | u32 sp4_how) | |
99fe60d0 BH |
6764 | { |
6765 | nfs4_verifier verifier; | |
6766 | struct nfs41_exchange_id_args args = { | |
cd93710e | 6767 | .verifier = &verifier, |
99fe60d0 | 6768 | .client = clp, |
cd3fadec | 6769 | #ifdef CONFIG_NFS_V4_1_MIGRATION |
4f0b429d | 6770 | .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER | |
cd3fadec CL |
6771 | EXCHGID4_FLAG_BIND_PRINC_STATEID | |
6772 | EXCHGID4_FLAG_SUPP_MOVED_MIGR, | |
6773 | #else | |
6774 | .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER | | |
6775 | EXCHGID4_FLAG_BIND_PRINC_STATEID, | |
6776 | #endif | |
99fe60d0 BH |
6777 | }; |
6778 | struct nfs41_exchange_id_res res = { | |
32b01310 | 6779 | 0 |
99fe60d0 BH |
6780 | }; |
6781 | int status; | |
6782 | struct rpc_message msg = { | |
6783 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID], | |
6784 | .rpc_argp = &args, | |
6785 | .rpc_resp = &res, | |
6786 | .rpc_cred = cred, | |
6787 | }; | |
99fe60d0 | 6788 | |
f092075d | 6789 | nfs4_init_boot_verifier(clp, &verifier); |
e984a55a CL |
6790 | args.id_len = nfs4_init_uniform_client_string(clp, args.id, |
6791 | sizeof(args.id)); | |
6bbb4ae8 CL |
6792 | dprintk("NFS call exchange_id auth=%s, '%.*s'\n", |
6793 | clp->cl_rpcclient->cl_auth->au_ops->au_name, | |
6794 | args.id_len, args.id); | |
99fe60d0 | 6795 | |
acdeb69d | 6796 | res.server_owner = kzalloc(sizeof(struct nfs41_server_owner), |
bbafffd2 | 6797 | GFP_NOFS); |
acdeb69d | 6798 | if (unlikely(res.server_owner == NULL)) { |
abe9a6d5 WAA |
6799 | status = -ENOMEM; |
6800 | goto out; | |
6801 | } | |
78fe0f41 | 6802 | |
79d4e1f0 | 6803 | res.server_scope = kzalloc(sizeof(struct nfs41_server_scope), |
bbafffd2 | 6804 | GFP_NOFS); |
177313f1 | 6805 | if (unlikely(res.server_scope == NULL)) { |
abe9a6d5 | 6806 | status = -ENOMEM; |
acdeb69d | 6807 | goto out_server_owner; |
abe9a6d5 | 6808 | } |
78fe0f41 | 6809 | |
bbafffd2 | 6810 | res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS); |
177313f1 | 6811 | if (unlikely(res.impl_id == NULL)) { |
7d2ed9ac WAA |
6812 | status = -ENOMEM; |
6813 | goto out_server_scope; | |
6814 | } | |
6815 | ||
2031cd1a WAA |
6816 | switch (sp4_how) { |
6817 | case SP4_NONE: | |
6818 | args.state_protect.how = SP4_NONE; | |
6819 | break; | |
6820 | ||
6821 | case SP4_MACH_CRED: | |
6822 | args.state_protect = nfs4_sp4_mach_cred_request; | |
6823 | break; | |
6824 | ||
6825 | default: | |
6826 | /* unsupported! */ | |
6827 | WARN_ON_ONCE(1); | |
6828 | status = -EINVAL; | |
6829 | goto out_server_scope; | |
6830 | } | |
6831 | ||
1bd714f2 | 6832 | status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
c6d01c6f | 6833 | trace_nfs4_exchange_id(clp, status); |
177313f1 | 6834 | if (status == 0) |
32b01310 | 6835 | status = nfs4_check_cl_exchange_flags(res.flags); |
78fe0f41 | 6836 | |
2031cd1a WAA |
6837 | if (status == 0) |
6838 | status = nfs4_sp4_select_mode(clp, &res.state_protect); | |
6839 | ||
acdeb69d | 6840 | if (status == 0) { |
32b01310 TM |
6841 | clp->cl_clientid = res.clientid; |
6842 | clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R); | |
6843 | if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) | |
6844 | clp->cl_seqid = res.seqid; | |
6845 | ||
acdeb69d CL |
6846 | kfree(clp->cl_serverowner); |
6847 | clp->cl_serverowner = res.server_owner; | |
6848 | res.server_owner = NULL; | |
78fe0f41 | 6849 | |
7d2ed9ac | 6850 | /* use the most recent implementation id */ |
59155546 CL |
6851 | kfree(clp->cl_implid); |
6852 | clp->cl_implid = res.impl_id; | |
7d2ed9ac | 6853 | |
177313f1 | 6854 | if (clp->cl_serverscope != NULL && |
79d4e1f0 | 6855 | !nfs41_same_server_scope(clp->cl_serverscope, |
78fe0f41 WAA |
6856 | res.server_scope)) { |
6857 | dprintk("%s: server_scope mismatch detected\n", | |
6858 | __func__); | |
6859 | set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state); | |
79d4e1f0 CL |
6860 | kfree(clp->cl_serverscope); |
6861 | clp->cl_serverscope = NULL; | |
78fe0f41 WAA |
6862 | } |
6863 | ||
177313f1 | 6864 | if (clp->cl_serverscope == NULL) { |
79d4e1f0 | 6865 | clp->cl_serverscope = res.server_scope; |
abe9a6d5 WAA |
6866 | goto out; |
6867 | } | |
32b01310 TM |
6868 | } else |
6869 | kfree(res.impl_id); | |
7d2ed9ac | 6870 | |
acdeb69d CL |
6871 | out_server_owner: |
6872 | kfree(res.server_owner); | |
7d2ed9ac | 6873 | out_server_scope: |
abe9a6d5 WAA |
6874 | kfree(res.server_scope); |
6875 | out: | |
177313f1 | 6876 | if (clp->cl_implid != NULL) |
6bbb4ae8 | 6877 | dprintk("NFS reply exchange_id: Server Implementation ID: " |
7d2ed9ac | 6878 | "domain: %s, name: %s, date: %llu,%u\n", |
6bbb4ae8 | 6879 | clp->cl_implid->domain, clp->cl_implid->name, |
59155546 CL |
6880 | clp->cl_implid->date.seconds, |
6881 | clp->cl_implid->date.nseconds); | |
6bbb4ae8 | 6882 | dprintk("NFS reply exchange_id: %d\n", status); |
99fe60d0 BH |
6883 | return status; |
6884 | } | |
6885 | ||
2031cd1a WAA |
6886 | /* |
6887 | * nfs4_proc_exchange_id() | |
6888 | * | |
6889 | * Returns zero, a negative errno, or a negative NFS4ERR status code. | |
6890 | * | |
6891 | * Since the clientid has expired, all compounds using sessions | |
6892 | * associated with the stale clientid will be returning | |
6893 | * NFS4ERR_BADSESSION in the sequence operation, and will therefore | |
6894 | * be in some phase of session reset. | |
6895 | * | |
6896 | * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used. | |
6897 | */ | |
6898 | int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred) | |
6899 | { | |
6900 | rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor; | |
6901 | int status; | |
6902 | ||
6903 | /* try SP4_MACH_CRED if krb5i/p */ | |
6904 | if (authflavor == RPC_AUTH_GSS_KRB5I || | |
6905 | authflavor == RPC_AUTH_GSS_KRB5P) { | |
6906 | status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED); | |
6907 | if (!status) | |
6908 | return 0; | |
6909 | } | |
6910 | ||
6911 | /* try SP4_NONE */ | |
6912 | return _nfs4_proc_exchange_id(clp, cred, SP4_NONE); | |
6913 | } | |
6914 | ||
66245539 TM |
6915 | static int _nfs4_proc_destroy_clientid(struct nfs_client *clp, |
6916 | struct rpc_cred *cred) | |
6917 | { | |
6918 | struct rpc_message msg = { | |
6919 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID], | |
6920 | .rpc_argp = clp, | |
6921 | .rpc_cred = cred, | |
6922 | }; | |
6923 | int status; | |
6924 | ||
6925 | status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); | |
c6d01c6f | 6926 | trace_nfs4_destroy_clientid(clp, status); |
66245539 | 6927 | if (status) |
02c67525 | 6928 | dprintk("NFS: Got error %d from the server %s on " |
66245539 TM |
6929 | "DESTROY_CLIENTID.", status, clp->cl_hostname); |
6930 | return status; | |
6931 | } | |
6932 | ||
6933 | static int nfs4_proc_destroy_clientid(struct nfs_client *clp, | |
6934 | struct rpc_cred *cred) | |
6935 | { | |
6936 | unsigned int loop; | |
6937 | int ret; | |
6938 | ||
6939 | for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) { | |
6940 | ret = _nfs4_proc_destroy_clientid(clp, cred); | |
6941 | switch (ret) { | |
6942 | case -NFS4ERR_DELAY: | |
6943 | case -NFS4ERR_CLIENTID_BUSY: | |
6944 | ssleep(1); | |
6945 | break; | |
6946 | default: | |
6947 | return ret; | |
6948 | } | |
6949 | } | |
6950 | return 0; | |
6951 | } | |
6952 | ||
6953 | int nfs4_destroy_clientid(struct nfs_client *clp) | |
6954 | { | |
6955 | struct rpc_cred *cred; | |
6956 | int ret = 0; | |
6957 | ||
6958 | if (clp->cl_mvops->minor_version < 1) | |
6959 | goto out; | |
6960 | if (clp->cl_exchange_flags == 0) | |
6961 | goto out; | |
05f4c350 CL |
6962 | if (clp->cl_preserve_clid) |
6963 | goto out; | |
73d8bde5 | 6964 | cred = nfs4_get_clid_cred(clp); |
66245539 TM |
6965 | ret = nfs4_proc_destroy_clientid(clp, cred); |
6966 | if (cred) | |
6967 | put_rpccred(cred); | |
6968 | switch (ret) { | |
6969 | case 0: | |
6970 | case -NFS4ERR_STALE_CLIENTID: | |
6971 | clp->cl_exchange_flags = 0; | |
6972 | } | |
6973 | out: | |
6974 | return ret; | |
6975 | } | |
6976 | ||
2050f0cc AA |
6977 | struct nfs4_get_lease_time_data { |
6978 | struct nfs4_get_lease_time_args *args; | |
6979 | struct nfs4_get_lease_time_res *res; | |
6980 | struct nfs_client *clp; | |
6981 | }; | |
6982 | ||
6983 | static void nfs4_get_lease_time_prepare(struct rpc_task *task, | |
6984 | void *calldata) | |
6985 | { | |
2050f0cc AA |
6986 | struct nfs4_get_lease_time_data *data = |
6987 | (struct nfs4_get_lease_time_data *)calldata; | |
6988 | ||
6989 | dprintk("--> %s\n", __func__); | |
6990 | /* just setup sequence, do not trigger session recovery | |
6991 | since we're invoked within one */ | |
d9afbd1b TM |
6992 | nfs41_setup_sequence(data->clp->cl_session, |
6993 | &data->args->la_seq_args, | |
6994 | &data->res->lr_seq_res, | |
6995 | task); | |
2050f0cc AA |
6996 | dprintk("<-- %s\n", __func__); |
6997 | } | |
6998 | ||
6999 | /* | |
7000 | * Called from nfs4_state_manager thread for session setup, so don't recover | |
7001 | * from sequence operation or clientid errors. | |
7002 | */ | |
7003 | static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata) | |
7004 | { | |
7005 | struct nfs4_get_lease_time_data *data = | |
7006 | (struct nfs4_get_lease_time_data *)calldata; | |
7007 | ||
7008 | dprintk("--> %s\n", __func__); | |
14516c3a TM |
7009 | if (!nfs41_sequence_done(task, &data->res->lr_seq_res)) |
7010 | return; | |
2050f0cc AA |
7011 | switch (task->tk_status) { |
7012 | case -NFS4ERR_DELAY: | |
7013 | case -NFS4ERR_GRACE: | |
7014 | dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status); | |
7015 | rpc_delay(task, NFS4_POLL_RETRY_MIN); | |
7016 | task->tk_status = 0; | |
a8a4ae3a AA |
7017 | /* fall through */ |
7018 | case -NFS4ERR_RETRY_UNCACHED_REP: | |
d00c5d43 | 7019 | rpc_restart_call_prepare(task); |
2050f0cc AA |
7020 | return; |
7021 | } | |
2050f0cc AA |
7022 | dprintk("<-- %s\n", __func__); |
7023 | } | |
7024 | ||
17280175 | 7025 | static const struct rpc_call_ops nfs4_get_lease_time_ops = { |
2050f0cc AA |
7026 | .rpc_call_prepare = nfs4_get_lease_time_prepare, |
7027 | .rpc_call_done = nfs4_get_lease_time_done, | |
7028 | }; | |
7029 | ||
7030 | int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo) | |
7031 | { | |
7032 | struct rpc_task *task; | |
7033 | struct nfs4_get_lease_time_args args; | |
7034 | struct nfs4_get_lease_time_res res = { | |
7035 | .lr_fsinfo = fsinfo, | |
7036 | }; | |
7037 | struct nfs4_get_lease_time_data data = { | |
7038 | .args = &args, | |
7039 | .res = &res, | |
7040 | .clp = clp, | |
7041 | }; | |
7042 | struct rpc_message msg = { | |
7043 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME], | |
7044 | .rpc_argp = &args, | |
7045 | .rpc_resp = &res, | |
7046 | }; | |
7047 | struct rpc_task_setup task_setup = { | |
7048 | .rpc_client = clp->cl_rpcclient, | |
7049 | .rpc_message = &msg, | |
7050 | .callback_ops = &nfs4_get_lease_time_ops, | |
1bd714f2 TM |
7051 | .callback_data = &data, |
7052 | .flags = RPC_TASK_TIMEOUT, | |
2050f0cc AA |
7053 | }; |
7054 | int status; | |
7055 | ||
a9c92d6b | 7056 | nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0); |
8fe72bac | 7057 | nfs4_set_sequence_privileged(&args.la_seq_args); |
2050f0cc AA |
7058 | dprintk("--> %s\n", __func__); |
7059 | task = rpc_run_task(&task_setup); | |
7060 | ||
7061 | if (IS_ERR(task)) | |
7062 | status = PTR_ERR(task); | |
7063 | else { | |
7064 | status = task->tk_status; | |
7065 | rpc_put_task(task); | |
7066 | } | |
7067 | dprintk("<-- %s return %d\n", __func__, status); | |
7068 | ||
7069 | return status; | |
7070 | } | |
7071 | ||
fc931582 AA |
7072 | /* |
7073 | * Initialize the values to be used by the client in CREATE_SESSION | |
7074 | * If nfs4_init_session set the fore channel request and response sizes, | |
7075 | * use them. | |
7076 | * | |
7077 | * Set the back channel max_resp_sz_cached to zero to force the client to | |
7078 | * always set csa_cachethis to FALSE because the current implementation | |
7079 | * of the back channel DRC only supports caching the CB_SEQUENCE operation. | |
7080 | */ | |
7081 | static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args) | |
7082 | { | |
18aad3d5 AA |
7083 | unsigned int max_rqst_sz, max_resp_sz; |
7084 | ||
7085 | max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead; | |
7086 | max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead; | |
fc931582 | 7087 | |
fc931582 | 7088 | /* Fore channel attributes */ |
18aad3d5 AA |
7089 | args->fc_attrs.max_rqst_sz = max_rqst_sz; |
7090 | args->fc_attrs.max_resp_sz = max_resp_sz; | |
fc931582 | 7091 | args->fc_attrs.max_ops = NFS4_MAX_OPS; |
ef159e91 | 7092 | args->fc_attrs.max_reqs = max_session_slots; |
fc931582 AA |
7093 | |
7094 | dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u " | |
8e0d46e1 | 7095 | "max_ops=%u max_reqs=%u\n", |
fc931582 AA |
7096 | __func__, |
7097 | args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz, | |
8e0d46e1 | 7098 | args->fc_attrs.max_ops, args->fc_attrs.max_reqs); |
fc931582 AA |
7099 | |
7100 | /* Back channel attributes */ | |
fc931582 AA |
7101 | args->bc_attrs.max_rqst_sz = PAGE_SIZE; |
7102 | args->bc_attrs.max_resp_sz = PAGE_SIZE; | |
7103 | args->bc_attrs.max_resp_sz_cached = 0; | |
7104 | args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS; | |
7105 | args->bc_attrs.max_reqs = 1; | |
7106 | ||
7107 | dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u " | |
7108 | "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n", | |
7109 | __func__, | |
7110 | args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz, | |
7111 | args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops, | |
7112 | args->bc_attrs.max_reqs); | |
7113 | } | |
7114 | ||
43c2e885 | 7115 | static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session) |
8d35301d | 7116 | { |
43c2e885 BF |
7117 | struct nfs4_channel_attrs *sent = &args->fc_attrs; |
7118 | struct nfs4_channel_attrs *rcvd = &session->fc_attrs; | |
7119 | ||
43c2e885 BF |
7120 | if (rcvd->max_resp_sz > sent->max_resp_sz) |
7121 | return -EINVAL; | |
7122 | /* | |
7123 | * Our requested max_ops is the minimum we need; we're not | |
7124 | * prepared to break up compounds into smaller pieces than that. | |
7125 | * So, no point even trying to continue if the server won't | |
7126 | * cooperate: | |
7127 | */ | |
7128 | if (rcvd->max_ops < sent->max_ops) | |
7129 | return -EINVAL; | |
7130 | if (rcvd->max_reqs == 0) | |
7131 | return -EINVAL; | |
b4b9a0c1 VG |
7132 | if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE) |
7133 | rcvd->max_reqs = NFS4_MAX_SLOT_TABLE; | |
43c2e885 | 7134 | return 0; |
8d35301d AA |
7135 | } |
7136 | ||
43c2e885 BF |
7137 | static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session) |
7138 | { | |
7139 | struct nfs4_channel_attrs *sent = &args->bc_attrs; | |
7140 | struct nfs4_channel_attrs *rcvd = &session->bc_attrs; | |
8d35301d | 7141 | |
43c2e885 BF |
7142 | if (rcvd->max_rqst_sz > sent->max_rqst_sz) |
7143 | return -EINVAL; | |
7144 | if (rcvd->max_resp_sz < sent->max_resp_sz) | |
7145 | return -EINVAL; | |
7146 | if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached) | |
7147 | return -EINVAL; | |
7148 | /* These would render the backchannel useless: */ | |
b4b9a0c1 | 7149 | if (rcvd->max_ops != sent->max_ops) |
43c2e885 | 7150 | return -EINVAL; |
b4b9a0c1 | 7151 | if (rcvd->max_reqs != sent->max_reqs) |
43c2e885 BF |
7152 | return -EINVAL; |
7153 | return 0; | |
7154 | } | |
8d35301d | 7155 | |
8d35301d AA |
7156 | static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args, |
7157 | struct nfs4_session *session) | |
7158 | { | |
43c2e885 | 7159 | int ret; |
8d35301d | 7160 | |
43c2e885 BF |
7161 | ret = nfs4_verify_fore_channel_attrs(args, session); |
7162 | if (ret) | |
7163 | return ret; | |
7164 | return nfs4_verify_back_channel_attrs(args, session); | |
8d35301d AA |
7165 | } |
7166 | ||
848f5bda TM |
7167 | static int _nfs4_proc_create_session(struct nfs_client *clp, |
7168 | struct rpc_cred *cred) | |
fc931582 AA |
7169 | { |
7170 | struct nfs4_session *session = clp->cl_session; | |
7171 | struct nfs41_create_session_args args = { | |
7172 | .client = clp, | |
7173 | .cb_program = NFS4_CALLBACK, | |
7174 | }; | |
7175 | struct nfs41_create_session_res res = { | |
7176 | .client = clp, | |
7177 | }; | |
7178 | struct rpc_message msg = { | |
7179 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION], | |
7180 | .rpc_argp = &args, | |
7181 | .rpc_resp = &res, | |
848f5bda | 7182 | .rpc_cred = cred, |
fc931582 AA |
7183 | }; |
7184 | int status; | |
7185 | ||
7186 | nfs4_init_channel_attrs(&args); | |
0f91421e | 7187 | args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN); |
fc931582 | 7188 | |
1bd714f2 | 7189 | status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
c6d01c6f | 7190 | trace_nfs4_create_session(clp, status); |
fc931582 | 7191 | |
43095d39 | 7192 | if (!status) { |
8d35301d AA |
7193 | /* Verify the session's negotiated channel_attrs values */ |
7194 | status = nfs4_verify_channel_attrs(&args, session); | |
fc931582 AA |
7195 | /* Increment the clientid slot sequence id */ |
7196 | clp->cl_seqid++; | |
7197 | } | |
7198 | ||
7199 | return status; | |
7200 | } | |
7201 | ||
7202 | /* | |
7203 | * Issues a CREATE_SESSION operation to the server. | |
7204 | * It is the responsibility of the caller to verify the session is | |
7205 | * expired before calling this routine. | |
7206 | */ | |
848f5bda | 7207 | int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred) |
fc931582 AA |
7208 | { |
7209 | int status; | |
7210 | unsigned *ptr; | |
fc931582 AA |
7211 | struct nfs4_session *session = clp->cl_session; |
7212 | ||
7213 | dprintk("--> %s clp=%p session=%p\n", __func__, clp, session); | |
7214 | ||
848f5bda | 7215 | status = _nfs4_proc_create_session(clp, cred); |
fc931582 AA |
7216 | if (status) |
7217 | goto out; | |
7218 | ||
aacd5537 AA |
7219 | /* Init or reset the session slot tables */ |
7220 | status = nfs4_setup_session_slot_tables(session); | |
7221 | dprintk("slot table setup returned %d\n", status); | |
fc931582 AA |
7222 | if (status) |
7223 | goto out; | |
7224 | ||
7225 | ptr = (unsigned *)&session->sess_id.data[0]; | |
7226 | dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__, | |
7227 | clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]); | |
fc931582 AA |
7228 | out: |
7229 | dprintk("<-- %s\n", __func__); | |
7230 | return status; | |
7231 | } | |
7232 | ||
0f3e66c6 AA |
7233 | /* |
7234 | * Issue the over-the-wire RPC DESTROY_SESSION. | |
7235 | * The caller must serialize access to this routine. | |
7236 | */ | |
848f5bda TM |
7237 | int nfs4_proc_destroy_session(struct nfs4_session *session, |
7238 | struct rpc_cred *cred) | |
0f3e66c6 | 7239 | { |
848f5bda TM |
7240 | struct rpc_message msg = { |
7241 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION], | |
7242 | .rpc_argp = session, | |
7243 | .rpc_cred = cred, | |
7244 | }; | |
0f3e66c6 | 7245 | int status = 0; |
0f3e66c6 AA |
7246 | |
7247 | dprintk("--> nfs4_proc_destroy_session\n"); | |
7248 | ||
7249 | /* session is still being setup */ | |
7250 | if (session->clp->cl_cons_state != NFS_CS_READY) | |
7251 | return status; | |
7252 | ||
1bd714f2 | 7253 | status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
c6d01c6f | 7254 | trace_nfs4_destroy_session(session->clp, status); |
0f3e66c6 AA |
7255 | |
7256 | if (status) | |
08106ac7 | 7257 | dprintk("NFS: Got error %d from the server on DESTROY_SESSION. " |
0f3e66c6 AA |
7258 | "Session has been destroyed regardless...\n", status); |
7259 | ||
7260 | dprintk("<-- nfs4_proc_destroy_session\n"); | |
7261 | return status; | |
7262 | } | |
7263 | ||
fc01cea9 AA |
7264 | /* |
7265 | * Renew the cl_session lease. | |
7266 | */ | |
d5f8d3fe TM |
7267 | struct nfs4_sequence_data { |
7268 | struct nfs_client *clp; | |
7269 | struct nfs4_sequence_args args; | |
7270 | struct nfs4_sequence_res res; | |
7271 | }; | |
7272 | ||
dc96aef9 AB |
7273 | static void nfs41_sequence_release(void *data) |
7274 | { | |
d5f8d3fe TM |
7275 | struct nfs4_sequence_data *calldata = data; |
7276 | struct nfs_client *clp = calldata->clp; | |
dc96aef9 | 7277 | |
7135840f AB |
7278 | if (atomic_read(&clp->cl_count) > 1) |
7279 | nfs4_schedule_state_renewal(clp); | |
7280 | nfs_put_client(clp); | |
d5f8d3fe | 7281 | kfree(calldata); |
dc96aef9 AB |
7282 | } |
7283 | ||
aa5190d0 TM |
7284 | static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp) |
7285 | { | |
7286 | switch(task->tk_status) { | |
7287 | case -NFS4ERR_DELAY: | |
aa5190d0 TM |
7288 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
7289 | return -EAGAIN; | |
7290 | default: | |
0400a6b0 | 7291 | nfs4_schedule_lease_recovery(clp); |
aa5190d0 TM |
7292 | } |
7293 | return 0; | |
7294 | } | |
7295 | ||
dc96aef9 | 7296 | static void nfs41_sequence_call_done(struct rpc_task *task, void *data) |
fc01cea9 | 7297 | { |
d5f8d3fe TM |
7298 | struct nfs4_sequence_data *calldata = data; |
7299 | struct nfs_client *clp = calldata->clp; | |
fc01cea9 | 7300 | |
14516c3a TM |
7301 | if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp)) |
7302 | return; | |
fc01cea9 | 7303 | |
c6d01c6f | 7304 | trace_nfs4_sequence(clp, task->tk_status); |
fc01cea9 AA |
7305 | if (task->tk_status < 0) { |
7306 | dprintk("%s ERROR %d\n", __func__, task->tk_status); | |
7135840f AB |
7307 | if (atomic_read(&clp->cl_count) == 1) |
7308 | goto out; | |
fc01cea9 | 7309 | |
aa5190d0 TM |
7310 | if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) { |
7311 | rpc_restart_call_prepare(task); | |
fc01cea9 AA |
7312 | return; |
7313 | } | |
7314 | } | |
fc01cea9 | 7315 | dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred); |
7135840f | 7316 | out: |
fc01cea9 AA |
7317 | dprintk("<-- %s\n", __func__); |
7318 | } | |
7319 | ||
7320 | static void nfs41_sequence_prepare(struct rpc_task *task, void *data) | |
7321 | { | |
d5f8d3fe TM |
7322 | struct nfs4_sequence_data *calldata = data; |
7323 | struct nfs_client *clp = calldata->clp; | |
fc01cea9 AA |
7324 | struct nfs4_sequence_args *args; |
7325 | struct nfs4_sequence_res *res; | |
7326 | ||
fc01cea9 AA |
7327 | args = task->tk_msg.rpc_argp; |
7328 | res = task->tk_msg.rpc_resp; | |
7329 | ||
d9afbd1b | 7330 | nfs41_setup_sequence(clp->cl_session, args, res, task); |
fc01cea9 AA |
7331 | } |
7332 | ||
7333 | static const struct rpc_call_ops nfs41_sequence_ops = { | |
7334 | .rpc_call_done = nfs41_sequence_call_done, | |
7335 | .rpc_call_prepare = nfs41_sequence_prepare, | |
dc96aef9 | 7336 | .rpc_release = nfs41_sequence_release, |
fc01cea9 AA |
7337 | }; |
7338 | ||
8fe72bac TM |
7339 | static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, |
7340 | struct rpc_cred *cred, | |
7341 | bool is_privileged) | |
fc01cea9 | 7342 | { |
d5f8d3fe | 7343 | struct nfs4_sequence_data *calldata; |
fc01cea9 AA |
7344 | struct rpc_message msg = { |
7345 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE], | |
7346 | .rpc_cred = cred, | |
7347 | }; | |
71ac6da9 TM |
7348 | struct rpc_task_setup task_setup_data = { |
7349 | .rpc_client = clp->cl_rpcclient, | |
7350 | .rpc_message = &msg, | |
8fe72bac | 7351 | .callback_ops = &nfs41_sequence_ops, |
bc7a05ca | 7352 | .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT, |
71ac6da9 | 7353 | }; |
fc01cea9 | 7354 | |
7135840f | 7355 | if (!atomic_inc_not_zero(&clp->cl_count)) |
71ac6da9 | 7356 | return ERR_PTR(-EIO); |
dfb4f309 | 7357 | calldata = kzalloc(sizeof(*calldata), GFP_NOFS); |
d5f8d3fe | 7358 | if (calldata == NULL) { |
7135840f | 7359 | nfs_put_client(clp); |
71ac6da9 | 7360 | return ERR_PTR(-ENOMEM); |
fc01cea9 | 7361 | } |
a9c92d6b | 7362 | nfs4_init_sequence(&calldata->args, &calldata->res, 0); |
8fe72bac TM |
7363 | if (is_privileged) |
7364 | nfs4_set_sequence_privileged(&calldata->args); | |
d5f8d3fe TM |
7365 | msg.rpc_argp = &calldata->args; |
7366 | msg.rpc_resp = &calldata->res; | |
7367 | calldata->clp = clp; | |
71ac6da9 | 7368 | task_setup_data.callback_data = calldata; |
fc01cea9 | 7369 | |
71ac6da9 TM |
7370 | return rpc_run_task(&task_setup_data); |
7371 | } | |
7372 | ||
2f60ea6b | 7373 | static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags) |
71ac6da9 TM |
7374 | { |
7375 | struct rpc_task *task; | |
7376 | int ret = 0; | |
7377 | ||
2f60ea6b | 7378 | if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0) |
d1f456b0 | 7379 | return -EAGAIN; |
8fe72bac | 7380 | task = _nfs41_proc_sequence(clp, cred, false); |
71ac6da9 TM |
7381 | if (IS_ERR(task)) |
7382 | ret = PTR_ERR(task); | |
7383 | else | |
bf294b41 | 7384 | rpc_put_task_async(task); |
71ac6da9 TM |
7385 | dprintk("<-- %s status=%d\n", __func__, ret); |
7386 | return ret; | |
7387 | } | |
7388 | ||
7389 | static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred) | |
7390 | { | |
7391 | struct rpc_task *task; | |
7392 | int ret; | |
7393 | ||
8fe72bac | 7394 | task = _nfs41_proc_sequence(clp, cred, true); |
71ac6da9 TM |
7395 | if (IS_ERR(task)) { |
7396 | ret = PTR_ERR(task); | |
7397 | goto out; | |
7398 | } | |
7399 | ret = rpc_wait_for_completion_task(task); | |
b4410c2f TM |
7400 | if (!ret) { |
7401 | struct nfs4_sequence_res *res = task->tk_msg.rpc_resp; | |
7402 | ||
7403 | if (task->tk_status == 0) | |
7404 | nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags); | |
71ac6da9 | 7405 | ret = task->tk_status; |
b4410c2f | 7406 | } |
71ac6da9 TM |
7407 | rpc_put_task(task); |
7408 | out: | |
7409 | dprintk("<-- %s status=%d\n", __func__, ret); | |
7410 | return ret; | |
fc01cea9 AA |
7411 | } |
7412 | ||
fce5c838 RL |
7413 | struct nfs4_reclaim_complete_data { |
7414 | struct nfs_client *clp; | |
7415 | struct nfs41_reclaim_complete_args arg; | |
7416 | struct nfs41_reclaim_complete_res res; | |
7417 | }; | |
7418 | ||
7419 | static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data) | |
7420 | { | |
7421 | struct nfs4_reclaim_complete_data *calldata = data; | |
7422 | ||
d9afbd1b TM |
7423 | nfs41_setup_sequence(calldata->clp->cl_session, |
7424 | &calldata->arg.seq_args, | |
7425 | &calldata->res.seq_res, | |
7426 | task); | |
fce5c838 RL |
7427 | } |
7428 | ||
aa5190d0 TM |
7429 | static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp) |
7430 | { | |
7431 | switch(task->tk_status) { | |
7432 | case 0: | |
7433 | case -NFS4ERR_COMPLETE_ALREADY: | |
7434 | case -NFS4ERR_WRONG_CRED: /* What to do here? */ | |
7435 | break; | |
7436 | case -NFS4ERR_DELAY: | |
aa5190d0 | 7437 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
a8a4ae3a AA |
7438 | /* fall through */ |
7439 | case -NFS4ERR_RETRY_UNCACHED_REP: | |
aa5190d0 TM |
7440 | return -EAGAIN; |
7441 | default: | |
0400a6b0 | 7442 | nfs4_schedule_lease_recovery(clp); |
aa5190d0 TM |
7443 | } |
7444 | return 0; | |
7445 | } | |
7446 | ||
fce5c838 RL |
7447 | static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data) |
7448 | { | |
7449 | struct nfs4_reclaim_complete_data *calldata = data; | |
7450 | struct nfs_client *clp = calldata->clp; | |
7451 | struct nfs4_sequence_res *res = &calldata->res.seq_res; | |
7452 | ||
7453 | dprintk("--> %s\n", __func__); | |
14516c3a TM |
7454 | if (!nfs41_sequence_done(task, res)) |
7455 | return; | |
fce5c838 | 7456 | |
c6d01c6f | 7457 | trace_nfs4_reclaim_complete(clp, task->tk_status); |
aa5190d0 TM |
7458 | if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) { |
7459 | rpc_restart_call_prepare(task); | |
7460 | return; | |
7461 | } | |
fce5c838 RL |
7462 | dprintk("<-- %s\n", __func__); |
7463 | } | |
7464 | ||
7465 | static void nfs4_free_reclaim_complete_data(void *data) | |
7466 | { | |
7467 | struct nfs4_reclaim_complete_data *calldata = data; | |
7468 | ||
7469 | kfree(calldata); | |
7470 | } | |
7471 | ||
7472 | static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = { | |
7473 | .rpc_call_prepare = nfs4_reclaim_complete_prepare, | |
7474 | .rpc_call_done = nfs4_reclaim_complete_done, | |
7475 | .rpc_release = nfs4_free_reclaim_complete_data, | |
7476 | }; | |
7477 | ||
7478 | /* | |
7479 | * Issue a global reclaim complete. | |
7480 | */ | |
965e9c23 TM |
7481 | static int nfs41_proc_reclaim_complete(struct nfs_client *clp, |
7482 | struct rpc_cred *cred) | |
fce5c838 RL |
7483 | { |
7484 | struct nfs4_reclaim_complete_data *calldata; | |
7485 | struct rpc_task *task; | |
7486 | struct rpc_message msg = { | |
7487 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE], | |
965e9c23 | 7488 | .rpc_cred = cred, |
fce5c838 RL |
7489 | }; |
7490 | struct rpc_task_setup task_setup_data = { | |
7491 | .rpc_client = clp->cl_rpcclient, | |
7492 | .rpc_message = &msg, | |
7493 | .callback_ops = &nfs4_reclaim_complete_call_ops, | |
7494 | .flags = RPC_TASK_ASYNC, | |
7495 | }; | |
7496 | int status = -ENOMEM; | |
7497 | ||
7498 | dprintk("--> %s\n", __func__); | |
8535b2be | 7499 | calldata = kzalloc(sizeof(*calldata), GFP_NOFS); |
fce5c838 RL |
7500 | if (calldata == NULL) |
7501 | goto out; | |
7502 | calldata->clp = clp; | |
7503 | calldata->arg.one_fs = 0; | |
fce5c838 | 7504 | |
a9c92d6b | 7505 | nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0); |
8fe72bac | 7506 | nfs4_set_sequence_privileged(&calldata->arg.seq_args); |
fce5c838 RL |
7507 | msg.rpc_argp = &calldata->arg; |
7508 | msg.rpc_resp = &calldata->res; | |
7509 | task_setup_data.callback_data = calldata; | |
7510 | task = rpc_run_task(&task_setup_data); | |
acf82b85 | 7511 | if (IS_ERR(task)) { |
fce5c838 | 7512 | status = PTR_ERR(task); |
acf82b85 DC |
7513 | goto out; |
7514 | } | |
c34c32ea AA |
7515 | status = nfs4_wait_for_completion_rpc_task(task); |
7516 | if (status == 0) | |
7517 | status = task->tk_status; | |
fce5c838 | 7518 | rpc_put_task(task); |
acf82b85 | 7519 | return 0; |
fce5c838 RL |
7520 | out: |
7521 | dprintk("<-- %s status=%d\n", __func__, status); | |
7522 | return status; | |
7523 | } | |
b1f69b75 AA |
7524 | |
7525 | static void | |
7526 | nfs4_layoutget_prepare(struct rpc_task *task, void *calldata) | |
7527 | { | |
7528 | struct nfs4_layoutget *lgp = calldata; | |
c31663d4 | 7529 | struct nfs_server *server = NFS_SERVER(lgp->args.inode); |
6ba7db34 | 7530 | struct nfs4_session *session = nfs4_get_session(server); |
b1f69b75 AA |
7531 | |
7532 | dprintk("--> %s\n", __func__); | |
c31663d4 FI |
7533 | /* Note the is a race here, where a CB_LAYOUTRECALL can come in |
7534 | * right now covering the LAYOUTGET we are about to send. | |
7535 | * However, that is not so catastrophic, and there seems | |
7536 | * to be no way to prevent it completely. | |
7537 | */ | |
6ba7db34 | 7538 | if (nfs41_setup_sequence(session, &lgp->args.seq_args, |
9d12b216 | 7539 | &lgp->res.seq_res, task)) |
b1f69b75 | 7540 | return; |
cf7d63f1 FI |
7541 | if (pnfs_choose_layoutget_stateid(&lgp->args.stateid, |
7542 | NFS_I(lgp->args.inode)->layout, | |
7543 | lgp->args.ctx->state)) { | |
7544 | rpc_exit(task, NFS4_OK); | |
cf7d63f1 | 7545 | } |
b1f69b75 AA |
7546 | } |
7547 | ||
7548 | static void nfs4_layoutget_done(struct rpc_task *task, void *calldata) | |
7549 | { | |
7550 | struct nfs4_layoutget *lgp = calldata; | |
ee314c2a TM |
7551 | struct inode *inode = lgp->args.inode; |
7552 | struct nfs_server *server = NFS_SERVER(inode); | |
7553 | struct pnfs_layout_hdr *lo; | |
7554 | struct nfs4_state *state = NULL; | |
ed7e5423 | 7555 | unsigned long timeo, now, giveup; |
b1f69b75 | 7556 | |
ed7e5423 | 7557 | dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status); |
b1f69b75 | 7558 | |
6ba7db34 | 7559 | if (!nfs41_sequence_done(task, &lgp->res.seq_res)) |
ee314c2a | 7560 | goto out; |
b1f69b75 AA |
7561 | |
7562 | switch (task->tk_status) { | |
7563 | case 0: | |
ee314c2a | 7564 | goto out; |
ed7e5423 BH |
7565 | /* |
7566 | * NFS4ERR_LAYOUTTRYLATER is a conflict with another client | |
7567 | * (or clients) writing to the same RAID stripe | |
7568 | */ | |
b1f69b75 | 7569 | case -NFS4ERR_LAYOUTTRYLATER: |
ed7e5423 BH |
7570 | /* |
7571 | * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall | |
7572 | * existing layout before getting a new one). | |
7573 | */ | |
b1f69b75 | 7574 | case -NFS4ERR_RECALLCONFLICT: |
30005121 WAA |
7575 | timeo = rpc_get_timeout(task->tk_client); |
7576 | giveup = lgp->args.timestamp + timeo; | |
ed7e5423 BH |
7577 | now = jiffies; |
7578 | if (time_after(giveup, now)) { | |
7579 | unsigned long delay; | |
7580 | ||
7581 | /* Delay for: | |
7582 | * - Not less then NFS4_POLL_RETRY_MIN. | |
7583 | * - One last time a jiffie before we give up | |
7584 | * - exponential backoff (time_now minus start_attempt) | |
7585 | */ | |
7586 | delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN, | |
7587 | min((giveup - now - 1), | |
7588 | now - lgp->args.timestamp)); | |
7589 | ||
7590 | dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n", | |
7591 | __func__, delay); | |
7592 | rpc_delay(task, delay); | |
7593 | task->tk_status = 0; | |
7594 | rpc_restart_call_prepare(task); | |
7595 | goto out; /* Do not call nfs4_async_handle_error() */ | |
7596 | } | |
ee314c2a TM |
7597 | break; |
7598 | case -NFS4ERR_EXPIRED: | |
7599 | case -NFS4ERR_BAD_STATEID: | |
7600 | spin_lock(&inode->i_lock); | |
7601 | lo = NFS_I(inode)->layout; | |
7602 | if (!lo || list_empty(&lo->plh_segs)) { | |
7603 | spin_unlock(&inode->i_lock); | |
7604 | /* If the open stateid was bad, then recover it. */ | |
7605 | state = lgp->args.ctx->state; | |
7606 | } else { | |
7607 | LIST_HEAD(head); | |
7608 | ||
defb8460 CH |
7609 | /* |
7610 | * Mark the bad layout state as invalid, then retry | |
7611 | * with the current stateid. | |
7612 | */ | |
ee314c2a TM |
7613 | pnfs_mark_matching_lsegs_invalid(lo, &head, NULL); |
7614 | spin_unlock(&inode->i_lock); | |
ee314c2a | 7615 | pnfs_free_lseg_list(&head); |
defb8460 CH |
7616 | |
7617 | task->tk_status = 0; | |
7618 | rpc_restart_call_prepare(task); | |
b1f69b75 AA |
7619 | } |
7620 | } | |
8478eaa1 | 7621 | if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) |
ee314c2a TM |
7622 | rpc_restart_call_prepare(task); |
7623 | out: | |
b1f69b75 AA |
7624 | dprintk("<-- %s\n", __func__); |
7625 | } | |
7626 | ||
8554116e IK |
7627 | static size_t max_response_pages(struct nfs_server *server) |
7628 | { | |
7629 | u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; | |
7630 | return nfs_page_array_len(0, max_resp_sz); | |
7631 | } | |
7632 | ||
7633 | static void nfs4_free_pages(struct page **pages, size_t size) | |
7634 | { | |
7635 | int i; | |
7636 | ||
7637 | if (!pages) | |
7638 | return; | |
7639 | ||
7640 | for (i = 0; i < size; i++) { | |
7641 | if (!pages[i]) | |
7642 | break; | |
7643 | __free_page(pages[i]); | |
7644 | } | |
7645 | kfree(pages); | |
7646 | } | |
7647 | ||
7648 | static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags) | |
7649 | { | |
7650 | struct page **pages; | |
7651 | int i; | |
7652 | ||
7653 | pages = kcalloc(size, sizeof(struct page *), gfp_flags); | |
7654 | if (!pages) { | |
7655 | dprintk("%s: can't alloc array of %zu pages\n", __func__, size); | |
7656 | return NULL; | |
7657 | } | |
7658 | ||
7659 | for (i = 0; i < size; i++) { | |
7660 | pages[i] = alloc_page(gfp_flags); | |
7661 | if (!pages[i]) { | |
7662 | dprintk("%s: failed to allocate page\n", __func__); | |
7663 | nfs4_free_pages(pages, size); | |
7664 | return NULL; | |
7665 | } | |
7666 | } | |
7667 | ||
7668 | return pages; | |
7669 | } | |
7670 | ||
b1f69b75 AA |
7671 | static void nfs4_layoutget_release(void *calldata) |
7672 | { | |
7673 | struct nfs4_layoutget *lgp = calldata; | |
a47970ff WAA |
7674 | struct inode *inode = lgp->args.inode; |
7675 | struct nfs_server *server = NFS_SERVER(inode); | |
8554116e | 7676 | size_t max_pages = max_response_pages(server); |
b1f69b75 AA |
7677 | |
7678 | dprintk("--> %s\n", __func__); | |
8554116e | 7679 | nfs4_free_pages(lgp->args.layout.pages, max_pages); |
a47970ff | 7680 | pnfs_put_layout_hdr(NFS_I(inode)->layout); |
b1f69b75 AA |
7681 | put_nfs_open_context(lgp->args.ctx); |
7682 | kfree(calldata); | |
7683 | dprintk("<-- %s\n", __func__); | |
7684 | } | |
7685 | ||
7686 | static const struct rpc_call_ops nfs4_layoutget_call_ops = { | |
7687 | .rpc_call_prepare = nfs4_layoutget_prepare, | |
7688 | .rpc_call_done = nfs4_layoutget_done, | |
7689 | .rpc_release = nfs4_layoutget_release, | |
7690 | }; | |
7691 | ||
a0b0a6e3 TM |
7692 | struct pnfs_layout_segment * |
7693 | nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags) | |
b1f69b75 | 7694 | { |
a47970ff WAA |
7695 | struct inode *inode = lgp->args.inode; |
7696 | struct nfs_server *server = NFS_SERVER(inode); | |
8554116e | 7697 | size_t max_pages = max_response_pages(server); |
b1f69b75 AA |
7698 | struct rpc_task *task; |
7699 | struct rpc_message msg = { | |
7700 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET], | |
7701 | .rpc_argp = &lgp->args, | |
7702 | .rpc_resp = &lgp->res, | |
6ab59344 | 7703 | .rpc_cred = lgp->cred, |
b1f69b75 AA |
7704 | }; |
7705 | struct rpc_task_setup task_setup_data = { | |
7706 | .rpc_client = server->client, | |
7707 | .rpc_message = &msg, | |
7708 | .callback_ops = &nfs4_layoutget_call_ops, | |
7709 | .callback_data = lgp, | |
7710 | .flags = RPC_TASK_ASYNC, | |
7711 | }; | |
a0b0a6e3 | 7712 | struct pnfs_layout_segment *lseg = NULL; |
b1f69b75 AA |
7713 | int status = 0; |
7714 | ||
7715 | dprintk("--> %s\n", __func__); | |
7716 | ||
4bd5a980 PT |
7717 | /* nfs4_layoutget_release calls pnfs_put_layout_hdr */ |
7718 | pnfs_get_layout_hdr(NFS_I(inode)->layout); | |
7719 | ||
8554116e IK |
7720 | lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags); |
7721 | if (!lgp->args.layout.pages) { | |
7722 | nfs4_layoutget_release(lgp); | |
a0b0a6e3 | 7723 | return ERR_PTR(-ENOMEM); |
8554116e IK |
7724 | } |
7725 | lgp->args.layout.pglen = max_pages * PAGE_SIZE; | |
30005121 | 7726 | lgp->args.timestamp = jiffies; |
8554116e | 7727 | |
35124a09 | 7728 | lgp->res.layoutp = &lgp->args.layout; |
b1f69b75 | 7729 | lgp->res.seq_res.sr_slot = NULL; |
a9c92d6b | 7730 | nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0); |
a47970ff | 7731 | |
b1f69b75 AA |
7732 | task = rpc_run_task(&task_setup_data); |
7733 | if (IS_ERR(task)) | |
a0b0a6e3 | 7734 | return ERR_CAST(task); |
b1f69b75 | 7735 | status = nfs4_wait_for_completion_rpc_task(task); |
c31663d4 FI |
7736 | if (status == 0) |
7737 | status = task->tk_status; | |
1037e6ea TM |
7738 | trace_nfs4_layoutget(lgp->args.ctx, |
7739 | &lgp->args.range, | |
7740 | &lgp->res.range, | |
7741 | status); | |
085b7a45 WAA |
7742 | /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */ |
7743 | if (status == 0 && lgp->res.layoutp->len) | |
a0b0a6e3 | 7744 | lseg = pnfs_layout_process(lgp); |
b1f69b75 AA |
7745 | rpc_put_task(task); |
7746 | dprintk("<-- %s status=%d\n", __func__, status); | |
a0b0a6e3 TM |
7747 | if (status) |
7748 | return ERR_PTR(status); | |
7749 | return lseg; | |
b1f69b75 AA |
7750 | } |
7751 | ||
cbe82603 BH |
7752 | static void |
7753 | nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata) | |
7754 | { | |
7755 | struct nfs4_layoutreturn *lrp = calldata; | |
7756 | ||
7757 | dprintk("--> %s\n", __func__); | |
d9afbd1b TM |
7758 | nfs41_setup_sequence(lrp->clp->cl_session, |
7759 | &lrp->args.seq_args, | |
7760 | &lrp->res.seq_res, | |
7761 | task); | |
cbe82603 BH |
7762 | } |
7763 | ||
7764 | static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata) | |
7765 | { | |
7766 | struct nfs4_layoutreturn *lrp = calldata; | |
7767 | struct nfs_server *server; | |
7768 | ||
7769 | dprintk("--> %s\n", __func__); | |
7770 | ||
6ba7db34 | 7771 | if (!nfs41_sequence_done(task, &lrp->res.seq_res)) |
cbe82603 BH |
7772 | return; |
7773 | ||
7774 | server = NFS_SERVER(lrp->args.inode); | |
f22e5edd TM |
7775 | switch (task->tk_status) { |
7776 | default: | |
7777 | task->tk_status = 0; | |
7778 | case 0: | |
7779 | break; | |
7780 | case -NFS4ERR_DELAY: | |
8478eaa1 | 7781 | if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN) |
f22e5edd | 7782 | break; |
d00c5d43 | 7783 | rpc_restart_call_prepare(task); |
cbe82603 BH |
7784 | return; |
7785 | } | |
cbe82603 BH |
7786 | dprintk("<-- %s\n", __func__); |
7787 | } | |
7788 | ||
7789 | static void nfs4_layoutreturn_release(void *calldata) | |
7790 | { | |
7791 | struct nfs4_layoutreturn *lrp = calldata; | |
849b286f | 7792 | struct pnfs_layout_hdr *lo = lrp->args.layout; |
cbe82603 BH |
7793 | |
7794 | dprintk("--> %s\n", __func__); | |
849b286f TM |
7795 | spin_lock(&lo->plh_inode->i_lock); |
7796 | if (lrp->res.lrs_present) | |
7797 | pnfs_set_layout_stateid(lo, &lrp->res.stateid, true); | |
7798 | lo->plh_block_lgets--; | |
7799 | spin_unlock(&lo->plh_inode->i_lock); | |
70c3bd2b | 7800 | pnfs_put_layout_hdr(lrp->args.layout); |
cbe82603 BH |
7801 | kfree(calldata); |
7802 | dprintk("<-- %s\n", __func__); | |
7803 | } | |
7804 | ||
7805 | static const struct rpc_call_ops nfs4_layoutreturn_call_ops = { | |
7806 | .rpc_call_prepare = nfs4_layoutreturn_prepare, | |
7807 | .rpc_call_done = nfs4_layoutreturn_done, | |
7808 | .rpc_release = nfs4_layoutreturn_release, | |
7809 | }; | |
7810 | ||
7811 | int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp) | |
7812 | { | |
7813 | struct rpc_task *task; | |
7814 | struct rpc_message msg = { | |
7815 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN], | |
7816 | .rpc_argp = &lrp->args, | |
7817 | .rpc_resp = &lrp->res, | |
9556000d | 7818 | .rpc_cred = lrp->cred, |
cbe82603 BH |
7819 | }; |
7820 | struct rpc_task_setup task_setup_data = { | |
1771c577 | 7821 | .rpc_client = NFS_SERVER(lrp->args.inode)->client, |
cbe82603 BH |
7822 | .rpc_message = &msg, |
7823 | .callback_ops = &nfs4_layoutreturn_call_ops, | |
7824 | .callback_data = lrp, | |
7825 | }; | |
7826 | int status; | |
7827 | ||
7828 | dprintk("--> %s\n", __func__); | |
a9c92d6b | 7829 | nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1); |
cbe82603 BH |
7830 | task = rpc_run_task(&task_setup_data); |
7831 | if (IS_ERR(task)) | |
7832 | return PTR_ERR(task); | |
7833 | status = task->tk_status; | |
1037e6ea | 7834 | trace_nfs4_layoutreturn(lrp->args.inode, status); |
cbe82603 BH |
7835 | dprintk("<-- %s status=%d\n", __func__, status); |
7836 | rpc_put_task(task); | |
7837 | return status; | |
7838 | } | |
7839 | ||
b1f69b75 | 7840 | static int |
cd5875fe TM |
7841 | _nfs4_proc_getdeviceinfo(struct nfs_server *server, |
7842 | struct pnfs_device *pdev, | |
7843 | struct rpc_cred *cred) | |
b1f69b75 AA |
7844 | { |
7845 | struct nfs4_getdeviceinfo_args args = { | |
7846 | .pdev = pdev, | |
7847 | }; | |
7848 | struct nfs4_getdeviceinfo_res res = { | |
7849 | .pdev = pdev, | |
7850 | }; | |
7851 | struct rpc_message msg = { | |
7852 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO], | |
7853 | .rpc_argp = &args, | |
7854 | .rpc_resp = &res, | |
cd5875fe | 7855 | .rpc_cred = cred, |
b1f69b75 AA |
7856 | }; |
7857 | int status; | |
7858 | ||
7859 | dprintk("--> %s\n", __func__); | |
7c513058 | 7860 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
b1f69b75 AA |
7861 | dprintk("<-- %s status=%d\n", __func__, status); |
7862 | ||
7863 | return status; | |
7864 | } | |
7865 | ||
cd5875fe TM |
7866 | int nfs4_proc_getdeviceinfo(struct nfs_server *server, |
7867 | struct pnfs_device *pdev, | |
7868 | struct rpc_cred *cred) | |
b1f69b75 AA |
7869 | { |
7870 | struct nfs4_exception exception = { }; | |
7871 | int err; | |
7872 | ||
7873 | do { | |
7874 | err = nfs4_handle_exception(server, | |
cd5875fe | 7875 | _nfs4_proc_getdeviceinfo(server, pdev, cred), |
b1f69b75 AA |
7876 | &exception); |
7877 | } while (exception.retry); | |
7878 | return err; | |
7879 | } | |
7880 | EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo); | |
7881 | ||
863a3c6c AA |
7882 | static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata) |
7883 | { | |
7884 | struct nfs4_layoutcommit_data *data = calldata; | |
7885 | struct nfs_server *server = NFS_SERVER(data->args.inode); | |
6ba7db34 | 7886 | struct nfs4_session *session = nfs4_get_session(server); |
863a3c6c | 7887 | |
d9afbd1b TM |
7888 | nfs41_setup_sequence(session, |
7889 | &data->args.seq_args, | |
7890 | &data->res.seq_res, | |
7891 | task); | |
863a3c6c AA |
7892 | } |
7893 | ||
7894 | static void | |
7895 | nfs4_layoutcommit_done(struct rpc_task *task, void *calldata) | |
7896 | { | |
7897 | struct nfs4_layoutcommit_data *data = calldata; | |
7898 | struct nfs_server *server = NFS_SERVER(data->args.inode); | |
7899 | ||
6ba7db34 | 7900 | if (!nfs41_sequence_done(task, &data->res.seq_res)) |
863a3c6c AA |
7901 | return; |
7902 | ||
7903 | switch (task->tk_status) { /* Just ignore these failures */ | |
e59d27e0 TM |
7904 | case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */ |
7905 | case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */ | |
7906 | case -NFS4ERR_BADLAYOUT: /* no layout */ | |
7907 | case -NFS4ERR_GRACE: /* loca_recalim always false */ | |
863a3c6c | 7908 | task->tk_status = 0; |
e59d27e0 | 7909 | case 0: |
e59d27e0 TM |
7910 | break; |
7911 | default: | |
8478eaa1 | 7912 | if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) { |
e59d27e0 TM |
7913 | rpc_restart_call_prepare(task); |
7914 | return; | |
7915 | } | |
7916 | } | |
863a3c6c AA |
7917 | } |
7918 | ||
7919 | static void nfs4_layoutcommit_release(void *calldata) | |
7920 | { | |
7921 | struct nfs4_layoutcommit_data *data = calldata; | |
7922 | ||
db29c089 | 7923 | pnfs_cleanup_layoutcommit(data); |
d8c951c3 TM |
7924 | nfs_post_op_update_inode_force_wcc(data->args.inode, |
7925 | data->res.fattr); | |
863a3c6c AA |
7926 | put_rpccred(data->cred); |
7927 | kfree(data); | |
7928 | } | |
7929 | ||
7930 | static const struct rpc_call_ops nfs4_layoutcommit_ops = { | |
7931 | .rpc_call_prepare = nfs4_layoutcommit_prepare, | |
7932 | .rpc_call_done = nfs4_layoutcommit_done, | |
7933 | .rpc_release = nfs4_layoutcommit_release, | |
7934 | }; | |
7935 | ||
7936 | int | |
ef311537 | 7937 | nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync) |
863a3c6c AA |
7938 | { |
7939 | struct rpc_message msg = { | |
7940 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT], | |
7941 | .rpc_argp = &data->args, | |
7942 | .rpc_resp = &data->res, | |
7943 | .rpc_cred = data->cred, | |
7944 | }; | |
7945 | struct rpc_task_setup task_setup_data = { | |
7946 | .task = &data->task, | |
7947 | .rpc_client = NFS_CLIENT(data->args.inode), | |
7948 | .rpc_message = &msg, | |
7949 | .callback_ops = &nfs4_layoutcommit_ops, | |
7950 | .callback_data = data, | |
7951 | .flags = RPC_TASK_ASYNC, | |
7952 | }; | |
7953 | struct rpc_task *task; | |
7954 | int status = 0; | |
7955 | ||
7956 | dprintk("NFS: %4d initiating layoutcommit call. sync %d " | |
7957 | "lbw: %llu inode %lu\n", | |
7958 | data->task.tk_pid, sync, | |
7959 | data->args.lastbytewritten, | |
7960 | data->args.inode->i_ino); | |
7961 | ||
a9c92d6b | 7962 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1); |
863a3c6c AA |
7963 | task = rpc_run_task(&task_setup_data); |
7964 | if (IS_ERR(task)) | |
7965 | return PTR_ERR(task); | |
ef311537 | 7966 | if (sync == false) |
863a3c6c AA |
7967 | goto out; |
7968 | status = nfs4_wait_for_completion_rpc_task(task); | |
7969 | if (status != 0) | |
7970 | goto out; | |
7971 | status = task->tk_status; | |
1037e6ea | 7972 | trace_nfs4_layoutcommit(data->args.inode, status); |
863a3c6c AA |
7973 | out: |
7974 | dprintk("%s: status %d\n", __func__, status); | |
7975 | rpc_put_task(task); | |
7976 | return status; | |
7977 | } | |
fca78d6d | 7978 | |
97431204 AA |
7979 | /** |
7980 | * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if | |
7981 | * possible) as per RFC3530bis and RFC5661 Security Considerations sections | |
7982 | */ | |
fca78d6d BS |
7983 | static int |
7984 | _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle, | |
b1b3e136 WAA |
7985 | struct nfs_fsinfo *info, |
7986 | struct nfs4_secinfo_flavors *flavors, bool use_integrity) | |
fca78d6d BS |
7987 | { |
7988 | struct nfs41_secinfo_no_name_args args = { | |
7989 | .style = SECINFO_STYLE_CURRENT_FH, | |
7990 | }; | |
7991 | struct nfs4_secinfo_res res = { | |
7992 | .flavors = flavors, | |
7993 | }; | |
7994 | struct rpc_message msg = { | |
7995 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME], | |
7996 | .rpc_argp = &args, | |
7997 | .rpc_resp = &res, | |
7998 | }; | |
b1b3e136 | 7999 | struct rpc_clnt *clnt = server->client; |
7cb852df | 8000 | struct rpc_cred *cred = NULL; |
b1b3e136 WAA |
8001 | int status; |
8002 | ||
8003 | if (use_integrity) { | |
8004 | clnt = server->nfs_client->cl_rpcclient; | |
7cb852df WAA |
8005 | cred = nfs4_get_clid_cred(server->nfs_client); |
8006 | msg.rpc_cred = cred; | |
b1b3e136 WAA |
8007 | } |
8008 | ||
8009 | dprintk("--> %s\n", __func__); | |
8010 | status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, | |
8011 | &res.seq_res, 0); | |
8012 | dprintk("<-- %s status=%d\n", __func__, status); | |
8013 | ||
7cb852df WAA |
8014 | if (cred) |
8015 | put_rpccred(cred); | |
b1b3e136 WAA |
8016 | |
8017 | return status; | |
fca78d6d BS |
8018 | } |
8019 | ||
8020 | static int | |
8021 | nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle, | |
8022 | struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors) | |
8023 | { | |
8024 | struct nfs4_exception exception = { }; | |
8025 | int err; | |
8026 | do { | |
b1b3e136 WAA |
8027 | /* first try using integrity protection */ |
8028 | err = -NFS4ERR_WRONGSEC; | |
8029 | ||
8030 | /* try to use integrity protection with machine cred */ | |
8031 | if (_nfs4_is_integrity_protected(server->nfs_client)) | |
8032 | err = _nfs41_proc_secinfo_no_name(server, fhandle, info, | |
8033 | flavors, true); | |
8034 | ||
8035 | /* | |
8036 | * if unable to use integrity protection, or SECINFO with | |
8037 | * integrity protection returns NFS4ERR_WRONGSEC (which is | |
8038 | * disallowed by spec, but exists in deployed servers) use | |
8039 | * the current filesystem's rpc_client and the user cred. | |
8040 | */ | |
8041 | if (err == -NFS4ERR_WRONGSEC) | |
8042 | err = _nfs41_proc_secinfo_no_name(server, fhandle, info, | |
8043 | flavors, false); | |
8044 | ||
fca78d6d BS |
8045 | switch (err) { |
8046 | case 0: | |
8047 | case -NFS4ERR_WRONGSEC: | |
78b19bae | 8048 | case -ENOTSUPP: |
05e9cfb4 | 8049 | goto out; |
fca78d6d BS |
8050 | default: |
8051 | err = nfs4_handle_exception(server, err, &exception); | |
8052 | } | |
8053 | } while (exception.retry); | |
05e9cfb4 | 8054 | out: |
fca78d6d BS |
8055 | return err; |
8056 | } | |
8057 | ||
8058 | static int | |
8059 | nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, | |
8060 | struct nfs_fsinfo *info) | |
8061 | { | |
8062 | int err; | |
8063 | struct page *page; | |
367156d9 | 8064 | rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR; |
fca78d6d | 8065 | struct nfs4_secinfo_flavors *flavors; |
58a8cf12 WAA |
8066 | struct nfs4_secinfo4 *secinfo; |
8067 | int i; | |
fca78d6d BS |
8068 | |
8069 | page = alloc_page(GFP_KERNEL); | |
8070 | if (!page) { | |
8071 | err = -ENOMEM; | |
8072 | goto out; | |
8073 | } | |
8074 | ||
8075 | flavors = page_address(page); | |
8076 | err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors); | |
8077 | ||
8078 | /* | |
8079 | * Fall back on "guess and check" method if | |
8080 | * the server doesn't support SECINFO_NO_NAME | |
8081 | */ | |
78b19bae | 8082 | if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) { |
fca78d6d BS |
8083 | err = nfs4_find_root_sec(server, fhandle, info); |
8084 | goto out_freepage; | |
8085 | } | |
8086 | if (err) | |
8087 | goto out_freepage; | |
8088 | ||
58a8cf12 WAA |
8089 | for (i = 0; i < flavors->num_flavors; i++) { |
8090 | secinfo = &flavors->flavors[i]; | |
8091 | ||
8092 | switch (secinfo->flavor) { | |
8093 | case RPC_AUTH_NULL: | |
8094 | case RPC_AUTH_UNIX: | |
8095 | case RPC_AUTH_GSS: | |
8096 | flavor = rpcauth_get_pseudoflavor(secinfo->flavor, | |
8097 | &secinfo->flavor_info); | |
8098 | break; | |
8099 | default: | |
8100 | flavor = RPC_AUTH_MAXFLAVOR; | |
8101 | break; | |
8102 | } | |
8103 | ||
4d4b69dd WAA |
8104 | if (!nfs_auth_info_match(&server->auth_info, flavor)) |
8105 | flavor = RPC_AUTH_MAXFLAVOR; | |
8106 | ||
58a8cf12 WAA |
8107 | if (flavor != RPC_AUTH_MAXFLAVOR) { |
8108 | err = nfs4_lookup_root_sec(server, fhandle, | |
8109 | info, flavor); | |
8110 | if (!err) | |
8111 | break; | |
8112 | } | |
8113 | } | |
8114 | ||
8115 | if (flavor == RPC_AUTH_MAXFLAVOR) | |
8116 | err = -EPERM; | |
fca78d6d BS |
8117 | |
8118 | out_freepage: | |
8119 | put_page(page); | |
8120 | if (err == -EACCES) | |
8121 | return -EPERM; | |
8122 | out: | |
8123 | return err; | |
8124 | } | |
1cab0652 | 8125 | |
ab7cb0df TM |
8126 | static int _nfs41_test_stateid(struct nfs_server *server, |
8127 | nfs4_stateid *stateid, | |
8128 | struct rpc_cred *cred) | |
7d974794 BS |
8129 | { |
8130 | int status; | |
8131 | struct nfs41_test_stateid_args args = { | |
1cab0652 | 8132 | .stateid = stateid, |
7d974794 BS |
8133 | }; |
8134 | struct nfs41_test_stateid_res res; | |
8135 | struct rpc_message msg = { | |
8136 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID], | |
8137 | .rpc_argp = &args, | |
8138 | .rpc_resp = &res, | |
ab7cb0df | 8139 | .rpc_cred = cred, |
7d974794 | 8140 | }; |
3787d506 WAA |
8141 | struct rpc_clnt *rpc_client = server->client; |
8142 | ||
8143 | nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID, | |
8144 | &rpc_client, &msg); | |
1cab0652 | 8145 | |
38527b15 | 8146 | dprintk("NFS call test_stateid %p\n", stateid); |
a9c92d6b | 8147 | nfs4_init_sequence(&args.seq_args, &res.seq_res, 0); |
8fe72bac | 8148 | nfs4_set_sequence_privileged(&args.seq_args); |
3787d506 | 8149 | status = nfs4_call_sync_sequence(rpc_client, server, &msg, |
8fe72bac | 8150 | &args.seq_args, &res.seq_res); |
38527b15 CL |
8151 | if (status != NFS_OK) { |
8152 | dprintk("NFS reply test_stateid: failed, %d\n", status); | |
377e507d | 8153 | return status; |
38527b15 CL |
8154 | } |
8155 | dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status); | |
377e507d | 8156 | return -res.status; |
7d974794 BS |
8157 | } |
8158 | ||
38527b15 CL |
8159 | /** |
8160 | * nfs41_test_stateid - perform a TEST_STATEID operation | |
8161 | * | |
8162 | * @server: server / transport on which to perform the operation | |
8163 | * @stateid: state ID to test | |
ab7cb0df | 8164 | * @cred: credential |
38527b15 CL |
8165 | * |
8166 | * Returns NFS_OK if the server recognizes that "stateid" is valid. | |
8167 | * Otherwise a negative NFS4ERR value is returned if the operation | |
8168 | * failed or the state ID is not currently valid. | |
8169 | */ | |
ab7cb0df TM |
8170 | static int nfs41_test_stateid(struct nfs_server *server, |
8171 | nfs4_stateid *stateid, | |
8172 | struct rpc_cred *cred) | |
7d974794 BS |
8173 | { |
8174 | struct nfs4_exception exception = { }; | |
8175 | int err; | |
8176 | do { | |
ab7cb0df | 8177 | err = _nfs41_test_stateid(server, stateid, cred); |
377e507d CL |
8178 | if (err != -NFS4ERR_DELAY) |
8179 | break; | |
8180 | nfs4_handle_exception(server, err, &exception); | |
7d974794 BS |
8181 | } while (exception.retry); |
8182 | return err; | |
8183 | } | |
9aeda35f | 8184 | |
7c1d5fae TM |
8185 | struct nfs_free_stateid_data { |
8186 | struct nfs_server *server; | |
8187 | struct nfs41_free_stateid_args args; | |
9aeda35f | 8188 | struct nfs41_free_stateid_res res; |
7c1d5fae TM |
8189 | }; |
8190 | ||
8191 | static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata) | |
8192 | { | |
8193 | struct nfs_free_stateid_data *data = calldata; | |
8194 | nfs41_setup_sequence(nfs4_get_session(data->server), | |
8195 | &data->args.seq_args, | |
8196 | &data->res.seq_res, | |
8197 | task); | |
8198 | } | |
8199 | ||
8200 | static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata) | |
8201 | { | |
8202 | struct nfs_free_stateid_data *data = calldata; | |
8203 | ||
8204 | nfs41_sequence_done(task, &data->res.seq_res); | |
8205 | ||
8206 | switch (task->tk_status) { | |
8207 | case -NFS4ERR_DELAY: | |
8478eaa1 | 8208 | if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN) |
7c1d5fae TM |
8209 | rpc_restart_call_prepare(task); |
8210 | } | |
8211 | } | |
8212 | ||
8213 | static void nfs41_free_stateid_release(void *calldata) | |
8214 | { | |
8215 | kfree(calldata); | |
8216 | } | |
8217 | ||
17f26b12 | 8218 | static const struct rpc_call_ops nfs41_free_stateid_ops = { |
7c1d5fae TM |
8219 | .rpc_call_prepare = nfs41_free_stateid_prepare, |
8220 | .rpc_call_done = nfs41_free_stateid_done, | |
8221 | .rpc_release = nfs41_free_stateid_release, | |
8222 | }; | |
8223 | ||
8224 | static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server, | |
8225 | nfs4_stateid *stateid, | |
ab7cb0df | 8226 | struct rpc_cred *cred, |
7c1d5fae TM |
8227 | bool privileged) |
8228 | { | |
9aeda35f BS |
8229 | struct rpc_message msg = { |
8230 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID], | |
ab7cb0df | 8231 | .rpc_cred = cred, |
9aeda35f | 8232 | }; |
7c1d5fae TM |
8233 | struct rpc_task_setup task_setup = { |
8234 | .rpc_client = server->client, | |
8235 | .rpc_message = &msg, | |
8236 | .callback_ops = &nfs41_free_stateid_ops, | |
8237 | .flags = RPC_TASK_ASYNC, | |
8238 | }; | |
8239 | struct nfs_free_stateid_data *data; | |
9aeda35f | 8240 | |
3787d506 WAA |
8241 | nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID, |
8242 | &task_setup.rpc_client, &msg); | |
8243 | ||
38527b15 | 8244 | dprintk("NFS call free_stateid %p\n", stateid); |
7c1d5fae TM |
8245 | data = kmalloc(sizeof(*data), GFP_NOFS); |
8246 | if (!data) | |
8247 | return ERR_PTR(-ENOMEM); | |
8248 | data->server = server; | |
8249 | nfs4_stateid_copy(&data->args.stateid, stateid); | |
8250 | ||
8251 | task_setup.callback_data = data; | |
8252 | ||
8253 | msg.rpc_argp = &data->args; | |
8254 | msg.rpc_resp = &data->res; | |
a9c92d6b | 8255 | nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0); |
7c1d5fae TM |
8256 | if (privileged) |
8257 | nfs4_set_sequence_privileged(&data->args.seq_args); | |
8258 | ||
8259 | return rpc_run_task(&task_setup); | |
9aeda35f BS |
8260 | } |
8261 | ||
38527b15 CL |
8262 | /** |
8263 | * nfs41_free_stateid - perform a FREE_STATEID operation | |
8264 | * | |
8265 | * @server: server / transport on which to perform the operation | |
8266 | * @stateid: state ID to release | |
ab7cb0df | 8267 | * @cred: credential |
38527b15 CL |
8268 | * |
8269 | * Returns NFS_OK if the server freed "stateid". Otherwise a | |
8270 | * negative NFS4ERR value is returned. | |
8271 | */ | |
ab7cb0df TM |
8272 | static int nfs41_free_stateid(struct nfs_server *server, |
8273 | nfs4_stateid *stateid, | |
8274 | struct rpc_cred *cred) | |
9aeda35f | 8275 | { |
7c1d5fae TM |
8276 | struct rpc_task *task; |
8277 | int ret; | |
8278 | ||
ab7cb0df | 8279 | task = _nfs41_free_stateid(server, stateid, cred, true); |
7c1d5fae TM |
8280 | if (IS_ERR(task)) |
8281 | return PTR_ERR(task); | |
8282 | ret = rpc_wait_for_completion_task(task); | |
8283 | if (!ret) | |
8284 | ret = task->tk_status; | |
8285 | rpc_put_task(task); | |
8286 | return ret; | |
9aeda35f | 8287 | } |
36281caa | 8288 | |
f1cdae87 JL |
8289 | static void |
8290 | nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp) | |
c8b2d0bf TM |
8291 | { |
8292 | struct rpc_task *task; | |
ab7cb0df | 8293 | struct rpc_cred *cred = lsp->ls_state->owner->so_cred; |
c8b2d0bf | 8294 | |
ab7cb0df | 8295 | task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false); |
c8b2d0bf TM |
8296 | nfs4_free_lock_state(server, lsp); |
8297 | if (IS_ERR(task)) | |
f1cdae87 | 8298 | return; |
c8b2d0bf | 8299 | rpc_put_task(task); |
c8b2d0bf TM |
8300 | } |
8301 | ||
36281caa TM |
8302 | static bool nfs41_match_stateid(const nfs4_stateid *s1, |
8303 | const nfs4_stateid *s2) | |
8304 | { | |
2d2f24ad | 8305 | if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0) |
36281caa TM |
8306 | return false; |
8307 | ||
2d2f24ad | 8308 | if (s1->seqid == s2->seqid) |
36281caa | 8309 | return true; |
2d2f24ad | 8310 | if (s1->seqid == 0 || s2->seqid == 0) |
36281caa TM |
8311 | return true; |
8312 | ||
8313 | return false; | |
8314 | } | |
8315 | ||
557134a3 AA |
8316 | #endif /* CONFIG_NFS_V4_1 */ |
8317 | ||
36281caa TM |
8318 | static bool nfs4_match_stateid(const nfs4_stateid *s1, |
8319 | const nfs4_stateid *s2) | |
8320 | { | |
f597c537 | 8321 | return nfs4_stateid_match(s1, s2); |
36281caa TM |
8322 | } |
8323 | ||
8324 | ||
17280175 | 8325 | static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = { |
7eff03ae | 8326 | .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, |
b79a4a1b | 8327 | .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, |
1da177e4 LT |
8328 | .recover_open = nfs4_open_reclaim, |
8329 | .recover_lock = nfs4_lock_reclaim, | |
591d71cb | 8330 | .establish_clid = nfs4_init_clientid, |
05f4c350 | 8331 | .detect_trunking = nfs40_discover_server_trunking, |
1da177e4 LT |
8332 | }; |
8333 | ||
591d71cb | 8334 | #if defined(CONFIG_NFS_V4_1) |
17280175 | 8335 | static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = { |
591d71cb AA |
8336 | .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, |
8337 | .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, | |
8338 | .recover_open = nfs4_open_reclaim, | |
8339 | .recover_lock = nfs4_lock_reclaim, | |
4d643d1d | 8340 | .establish_clid = nfs41_init_clientid, |
fce5c838 | 8341 | .reclaim_complete = nfs41_proc_reclaim_complete, |
05f4c350 | 8342 | .detect_trunking = nfs41_discover_server_trunking, |
591d71cb AA |
8343 | }; |
8344 | #endif /* CONFIG_NFS_V4_1 */ | |
8345 | ||
17280175 | 8346 | static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = { |
591d71cb AA |
8347 | .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, |
8348 | .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, | |
4dfd4f7a | 8349 | .recover_open = nfs40_open_expired, |
591d71cb AA |
8350 | .recover_lock = nfs4_lock_expired, |
8351 | .establish_clid = nfs4_init_clientid, | |
8352 | }; | |
8353 | ||
8354 | #if defined(CONFIG_NFS_V4_1) | |
17280175 | 8355 | static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = { |
7eff03ae | 8356 | .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, |
b79a4a1b | 8357 | .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, |
f062eb6c BS |
8358 | .recover_open = nfs41_open_expired, |
8359 | .recover_lock = nfs41_lock_expired, | |
4d643d1d | 8360 | .establish_clid = nfs41_init_clientid, |
1da177e4 | 8361 | }; |
591d71cb | 8362 | #endif /* CONFIG_NFS_V4_1 */ |
1da177e4 | 8363 | |
17280175 | 8364 | static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = { |
29fba38b | 8365 | .sched_state_renewal = nfs4_proc_async_renew, |
a7b72103 | 8366 | .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked, |
8e69514f | 8367 | .renew_lease = nfs4_proc_renew, |
29fba38b BH |
8368 | }; |
8369 | ||
8370 | #if defined(CONFIG_NFS_V4_1) | |
17280175 | 8371 | static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = { |
29fba38b | 8372 | .sched_state_renewal = nfs41_proc_async_sequence, |
a7b72103 | 8373 | .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked, |
8e69514f | 8374 | .renew_lease = nfs4_proc_sequence, |
29fba38b BH |
8375 | }; |
8376 | #endif | |
8377 | ||
ec011fe8 | 8378 | static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = { |
b03d735b | 8379 | .get_locations = _nfs40_proc_get_locations, |
44c99933 | 8380 | .fsid_present = _nfs40_proc_fsid_present, |
ec011fe8 CL |
8381 | }; |
8382 | ||
8383 | #if defined(CONFIG_NFS_V4_1) | |
8384 | static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = { | |
b03d735b | 8385 | .get_locations = _nfs41_proc_get_locations, |
44c99933 | 8386 | .fsid_present = _nfs41_proc_fsid_present, |
ec011fe8 CL |
8387 | }; |
8388 | #endif /* CONFIG_NFS_V4_1 */ | |
8389 | ||
97dc1359 TM |
8390 | static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = { |
8391 | .minor_version = 0, | |
39c6daae TM |
8392 | .init_caps = NFS_CAP_READDIRPLUS |
8393 | | NFS_CAP_ATOMIC_OPEN | |
8394 | | NFS_CAP_CHANGE_ATTR | |
8395 | | NFS_CAP_POSIX_LOCK, | |
abf79bb3 CL |
8396 | .init_client = nfs40_init_client, |
8397 | .shutdown_client = nfs40_shutdown_client, | |
36281caa | 8398 | .match_stateid = nfs4_match_stateid, |
fca78d6d | 8399 | .find_root_sec = nfs4_find_root_sec, |
c8b2d0bf | 8400 | .free_lock_state = nfs4_release_lockowner, |
9915ea7e | 8401 | .call_sync_ops = &nfs40_call_sync_ops, |
c48f4f35 TM |
8402 | .reboot_recovery_ops = &nfs40_reboot_recovery_ops, |
8403 | .nograce_recovery_ops = &nfs40_nograce_recovery_ops, | |
8404 | .state_renewal_ops = &nfs40_state_renewal_ops, | |
ec011fe8 | 8405 | .mig_recovery_ops = &nfs40_mig_recovery_ops, |
97dc1359 TM |
8406 | }; |
8407 | ||
8408 | #if defined(CONFIG_NFS_V4_1) | |
8409 | static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = { | |
8410 | .minor_version = 1, | |
39c6daae TM |
8411 | .init_caps = NFS_CAP_READDIRPLUS |
8412 | | NFS_CAP_ATOMIC_OPEN | |
8413 | | NFS_CAP_CHANGE_ATTR | |
3b66486c | 8414 | | NFS_CAP_POSIX_LOCK |
49f9a0fa | 8415 | | NFS_CAP_STATEID_NFSV41 |
e983120e | 8416 | | NFS_CAP_ATOMIC_OPEN_V1, |
abf79bb3 CL |
8417 | .init_client = nfs41_init_client, |
8418 | .shutdown_client = nfs41_shutdown_client, | |
36281caa | 8419 | .match_stateid = nfs41_match_stateid, |
fca78d6d | 8420 | .find_root_sec = nfs41_find_root_sec, |
c8b2d0bf | 8421 | .free_lock_state = nfs41_free_lock_state, |
9915ea7e | 8422 | .call_sync_ops = &nfs41_call_sync_ops, |
c48f4f35 TM |
8423 | .reboot_recovery_ops = &nfs41_reboot_recovery_ops, |
8424 | .nograce_recovery_ops = &nfs41_nograce_recovery_ops, | |
8425 | .state_renewal_ops = &nfs41_state_renewal_ops, | |
ec011fe8 | 8426 | .mig_recovery_ops = &nfs41_mig_recovery_ops, |
97dc1359 TM |
8427 | }; |
8428 | #endif | |
8429 | ||
42c2c424 SD |
8430 | #if defined(CONFIG_NFS_V4_2) |
8431 | static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = { | |
8432 | .minor_version = 2, | |
7017310a BS |
8433 | .init_caps = NFS_CAP_READDIRPLUS |
8434 | | NFS_CAP_ATOMIC_OPEN | |
8435 | | NFS_CAP_CHANGE_ATTR | |
8436 | | NFS_CAP_POSIX_LOCK | |
8437 | | NFS_CAP_STATEID_NFSV41 | |
e983120e | 8438 | | NFS_CAP_ATOMIC_OPEN_V1 |
f4ac1674 | 8439 | | NFS_CAP_ALLOCATE |
624bd5b7 | 8440 | | NFS_CAP_DEALLOCATE |
e983120e | 8441 | | NFS_CAP_SEEK, |
abf79bb3 CL |
8442 | .init_client = nfs41_init_client, |
8443 | .shutdown_client = nfs41_shutdown_client, | |
42c2c424 SD |
8444 | .match_stateid = nfs41_match_stateid, |
8445 | .find_root_sec = nfs41_find_root_sec, | |
7017310a | 8446 | .free_lock_state = nfs41_free_lock_state, |
9915ea7e | 8447 | .call_sync_ops = &nfs41_call_sync_ops, |
42c2c424 SD |
8448 | .reboot_recovery_ops = &nfs41_reboot_recovery_ops, |
8449 | .nograce_recovery_ops = &nfs41_nograce_recovery_ops, | |
8450 | .state_renewal_ops = &nfs41_state_renewal_ops, | |
8451 | }; | |
8452 | #endif | |
8453 | ||
97dc1359 TM |
8454 | const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = { |
8455 | [0] = &nfs_v4_0_minor_ops, | |
8456 | #if defined(CONFIG_NFS_V4_1) | |
8457 | [1] = &nfs_v4_1_minor_ops, | |
8458 | #endif | |
42c2c424 SD |
8459 | #if defined(CONFIG_NFS_V4_2) |
8460 | [2] = &nfs_v4_2_minor_ops, | |
8461 | #endif | |
97dc1359 TM |
8462 | }; |
8463 | ||
17f26b12 | 8464 | static const struct inode_operations nfs4_dir_inode_operations = { |
73a79706 BS |
8465 | .create = nfs_create, |
8466 | .lookup = nfs_lookup, | |
8467 | .atomic_open = nfs_atomic_open, | |
8468 | .link = nfs_link, | |
8469 | .unlink = nfs_unlink, | |
8470 | .symlink = nfs_symlink, | |
8471 | .mkdir = nfs_mkdir, | |
8472 | .rmdir = nfs_rmdir, | |
8473 | .mknod = nfs_mknod, | |
8474 | .rename = nfs_rename, | |
8475 | .permission = nfs_permission, | |
8476 | .getattr = nfs_getattr, | |
8477 | .setattr = nfs_setattr, | |
8478 | .getxattr = generic_getxattr, | |
8479 | .setxattr = generic_setxattr, | |
8480 | .listxattr = generic_listxattr, | |
8481 | .removexattr = generic_removexattr, | |
8482 | }; | |
8483 | ||
92e1d5be | 8484 | static const struct inode_operations nfs4_file_inode_operations = { |
6b3b5496 BF |
8485 | .permission = nfs_permission, |
8486 | .getattr = nfs_getattr, | |
8487 | .setattr = nfs_setattr, | |
64c2ce8b AK |
8488 | .getxattr = generic_getxattr, |
8489 | .setxattr = generic_setxattr, | |
8490 | .listxattr = generic_listxattr, | |
8491 | .removexattr = generic_removexattr, | |
6b3b5496 BF |
8492 | }; |
8493 | ||
509de811 | 8494 | const struct nfs_rpc_ops nfs_v4_clientops = { |
1da177e4 LT |
8495 | .version = 4, /* protocol version */ |
8496 | .dentry_ops = &nfs4_dentry_operations, | |
8497 | .dir_inode_ops = &nfs4_dir_inode_operations, | |
6b3b5496 | 8498 | .file_inode_ops = &nfs4_file_inode_operations, |
1788ea6e | 8499 | .file_ops = &nfs4_file_operations, |
1da177e4 | 8500 | .getroot = nfs4_proc_get_root, |
281cad46 | 8501 | .submount = nfs4_submount, |
ff9099f2 | 8502 | .try_mount = nfs4_try_mount, |
1da177e4 LT |
8503 | .getattr = nfs4_proc_getattr, |
8504 | .setattr = nfs4_proc_setattr, | |
8505 | .lookup = nfs4_proc_lookup, | |
8506 | .access = nfs4_proc_access, | |
8507 | .readlink = nfs4_proc_readlink, | |
1da177e4 LT |
8508 | .create = nfs4_proc_create, |
8509 | .remove = nfs4_proc_remove, | |
8510 | .unlink_setup = nfs4_proc_unlink_setup, | |
34e137cc | 8511 | .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare, |
1da177e4 | 8512 | .unlink_done = nfs4_proc_unlink_done, |
d3d4152a | 8513 | .rename_setup = nfs4_proc_rename_setup, |
c6bfa1a1 | 8514 | .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare, |
d3d4152a | 8515 | .rename_done = nfs4_proc_rename_done, |
1da177e4 LT |
8516 | .link = nfs4_proc_link, |
8517 | .symlink = nfs4_proc_symlink, | |
8518 | .mkdir = nfs4_proc_mkdir, | |
8519 | .rmdir = nfs4_proc_remove, | |
8520 | .readdir = nfs4_proc_readdir, | |
8521 | .mknod = nfs4_proc_mknod, | |
8522 | .statfs = nfs4_proc_statfs, | |
8523 | .fsinfo = nfs4_proc_fsinfo, | |
8524 | .pathconf = nfs4_proc_pathconf, | |
e9326dca | 8525 | .set_capabilities = nfs4_server_capabilities, |
1da177e4 | 8526 | .decode_dirent = nfs4_decode_dirent, |
a4cdda59 | 8527 | .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare, |
1da177e4 | 8528 | .read_setup = nfs4_proc_read_setup, |
ec06c096 | 8529 | .read_done = nfs4_read_done, |
1da177e4 | 8530 | .write_setup = nfs4_proc_write_setup, |
788e7a89 | 8531 | .write_done = nfs4_write_done, |
1da177e4 | 8532 | .commit_setup = nfs4_proc_commit_setup, |
0b7c0153 | 8533 | .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare, |
788e7a89 | 8534 | .commit_done = nfs4_commit_done, |
1da177e4 | 8535 | .lock = nfs4_proc_lock, |
e50a1c2e | 8536 | .clear_acl_cache = nfs4_zap_acl_attr, |
7fe5c398 | 8537 | .close_context = nfs4_close_context, |
2b484297 | 8538 | .open_context = nfs4_atomic_open, |
011e2a7f | 8539 | .have_delegation = nfs4_have_delegation, |
57ec14c5 | 8540 | .return_delegation = nfs4_inode_return_delegation, |
6663ee7f | 8541 | .alloc_client = nfs4_alloc_client, |
45a52a02 | 8542 | .init_client = nfs4_init_client, |
cdb7eced | 8543 | .free_client = nfs4_free_client, |
1179acc6 BS |
8544 | .create_server = nfs4_create_server, |
8545 | .clone_server = nfs_clone_server, | |
1da177e4 LT |
8546 | }; |
8547 | ||
64c2ce8b AK |
8548 | static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = { |
8549 | .prefix = XATTR_NAME_NFSV4_ACL, | |
8550 | .list = nfs4_xattr_list_nfs4_acl, | |
8551 | .get = nfs4_xattr_get_nfs4_acl, | |
8552 | .set = nfs4_xattr_set_nfs4_acl, | |
8553 | }; | |
8554 | ||
8555 | const struct xattr_handler *nfs4_xattr_handlers[] = { | |
8556 | &nfs4_xattr_nfs4_acl_handler, | |
c9bccef6 DQ |
8557 | #ifdef CONFIG_NFS_V4_SECURITY_LABEL |
8558 | &nfs4_xattr_nfs4_label_handler, | |
8559 | #endif | |
64c2ce8b AK |
8560 | NULL |
8561 | }; | |
8562 | ||
1da177e4 LT |
8563 | /* |
8564 | * Local variables: | |
8565 | * c-basic-offset: 8 | |
8566 | * End: | |
8567 | */ |