| 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> |
| 39 | #include <linux/delay.h> |
| 40 | #include <linux/errno.h> |
| 41 | #include <linux/string.h> |
| 42 | #include <linux/ratelimit.h> |
| 43 | #include <linux/printk.h> |
| 44 | #include <linux/slab.h> |
| 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> |
| 50 | #include <linux/nfs_mount.h> |
| 51 | #include <linux/namei.h> |
| 52 | #include <linux/mount.h> |
| 53 | #include <linux/module.h> |
| 54 | #include <linux/nfs_idmap.h> |
| 55 | #include <linux/xattr.h> |
| 56 | #include <linux/utsname.h> |
| 57 | #include <linux/freezer.h> |
| 58 | |
| 59 | #include "nfs4_fs.h" |
| 60 | #include "delegation.h" |
| 61 | #include "internal.h" |
| 62 | #include "iostat.h" |
| 63 | #include "callback.h" |
| 64 | #include "pnfs.h" |
| 65 | #include "netns.h" |
| 66 | #include "nfs4session.h" |
| 67 | #include "fscache.h" |
| 68 | |
| 69 | #define NFSDBG_FACILITY NFSDBG_PROC |
| 70 | |
| 71 | #define NFS4_POLL_RETRY_MIN (HZ/10) |
| 72 | #define NFS4_POLL_RETRY_MAX (15*HZ) |
| 73 | |
| 74 | struct nfs4_opendata; |
| 75 | static int _nfs4_proc_open(struct nfs4_opendata *data); |
| 76 | static int _nfs4_recover_proc_open(struct nfs4_opendata *data); |
| 77 | static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *); |
| 78 | static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *); |
| 79 | static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr); |
| 80 | static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *); |
| 81 | static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr); |
| 82 | static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred, |
| 83 | struct nfs_fattr *fattr, struct iattr *sattr, |
| 84 | struct nfs4_state *state); |
| 85 | #ifdef CONFIG_NFS_V4_1 |
| 86 | static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *); |
| 87 | static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *); |
| 88 | #endif |
| 89 | /* Prevent leaks of NFSv4 errors into userland */ |
| 90 | static int nfs4_map_errors(int err) |
| 91 | { |
| 92 | if (err >= -1000) |
| 93 | return err; |
| 94 | switch (err) { |
| 95 | case -NFS4ERR_RESOURCE: |
| 96 | case -NFS4ERR_LAYOUTTRYLATER: |
| 97 | case -NFS4ERR_RECALLCONFLICT: |
| 98 | return -EREMOTEIO; |
| 99 | case -NFS4ERR_WRONGSEC: |
| 100 | return -EPERM; |
| 101 | case -NFS4ERR_BADOWNER: |
| 102 | case -NFS4ERR_BADNAME: |
| 103 | return -EINVAL; |
| 104 | case -NFS4ERR_SHARE_DENIED: |
| 105 | return -EACCES; |
| 106 | case -NFS4ERR_MINOR_VERS_MISMATCH: |
| 107 | return -EPROTONOSUPPORT; |
| 108 | case -NFS4ERR_ACCESS: |
| 109 | return -EACCES; |
| 110 | case -NFS4ERR_FILE_OPEN: |
| 111 | return -EBUSY; |
| 112 | default: |
| 113 | dprintk("%s could not handle NFSv4 error %d\n", |
| 114 | __func__, -err); |
| 115 | break; |
| 116 | } |
| 117 | return -EIO; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * This is our standard bitmap for GETATTR requests. |
| 122 | */ |
| 123 | const u32 nfs4_fattr_bitmap[3] = { |
| 124 | FATTR4_WORD0_TYPE |
| 125 | | FATTR4_WORD0_CHANGE |
| 126 | | FATTR4_WORD0_SIZE |
| 127 | | FATTR4_WORD0_FSID |
| 128 | | FATTR4_WORD0_FILEID, |
| 129 | FATTR4_WORD1_MODE |
| 130 | | FATTR4_WORD1_NUMLINKS |
| 131 | | FATTR4_WORD1_OWNER |
| 132 | | FATTR4_WORD1_OWNER_GROUP |
| 133 | | FATTR4_WORD1_RAWDEV |
| 134 | | FATTR4_WORD1_SPACE_USED |
| 135 | | FATTR4_WORD1_TIME_ACCESS |
| 136 | | FATTR4_WORD1_TIME_METADATA |
| 137 | | FATTR4_WORD1_TIME_MODIFY |
| 138 | }; |
| 139 | |
| 140 | static const u32 nfs4_pnfs_open_bitmap[3] = { |
| 141 | FATTR4_WORD0_TYPE |
| 142 | | FATTR4_WORD0_CHANGE |
| 143 | | FATTR4_WORD0_SIZE |
| 144 | | FATTR4_WORD0_FSID |
| 145 | | FATTR4_WORD0_FILEID, |
| 146 | FATTR4_WORD1_MODE |
| 147 | | FATTR4_WORD1_NUMLINKS |
| 148 | | FATTR4_WORD1_OWNER |
| 149 | | FATTR4_WORD1_OWNER_GROUP |
| 150 | | FATTR4_WORD1_RAWDEV |
| 151 | | FATTR4_WORD1_SPACE_USED |
| 152 | | FATTR4_WORD1_TIME_ACCESS |
| 153 | | FATTR4_WORD1_TIME_METADATA |
| 154 | | FATTR4_WORD1_TIME_MODIFY, |
| 155 | FATTR4_WORD2_MDSTHRESHOLD |
| 156 | }; |
| 157 | |
| 158 | static const u32 nfs4_open_noattr_bitmap[3] = { |
| 159 | FATTR4_WORD0_TYPE |
| 160 | | FATTR4_WORD0_CHANGE |
| 161 | | FATTR4_WORD0_FILEID, |
| 162 | }; |
| 163 | |
| 164 | const u32 nfs4_statfs_bitmap[2] = { |
| 165 | FATTR4_WORD0_FILES_AVAIL |
| 166 | | FATTR4_WORD0_FILES_FREE |
| 167 | | FATTR4_WORD0_FILES_TOTAL, |
| 168 | FATTR4_WORD1_SPACE_AVAIL |
| 169 | | FATTR4_WORD1_SPACE_FREE |
| 170 | | FATTR4_WORD1_SPACE_TOTAL |
| 171 | }; |
| 172 | |
| 173 | const u32 nfs4_pathconf_bitmap[2] = { |
| 174 | FATTR4_WORD0_MAXLINK |
| 175 | | FATTR4_WORD0_MAXNAME, |
| 176 | 0 |
| 177 | }; |
| 178 | |
| 179 | const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE |
| 180 | | FATTR4_WORD0_MAXREAD |
| 181 | | FATTR4_WORD0_MAXWRITE |
| 182 | | FATTR4_WORD0_LEASE_TIME, |
| 183 | FATTR4_WORD1_TIME_DELTA |
| 184 | | FATTR4_WORD1_FS_LAYOUT_TYPES, |
| 185 | FATTR4_WORD2_LAYOUT_BLKSIZE |
| 186 | }; |
| 187 | |
| 188 | const u32 nfs4_fs_locations_bitmap[2] = { |
| 189 | FATTR4_WORD0_TYPE |
| 190 | | FATTR4_WORD0_CHANGE |
| 191 | | FATTR4_WORD0_SIZE |
| 192 | | FATTR4_WORD0_FSID |
| 193 | | FATTR4_WORD0_FILEID |
| 194 | | FATTR4_WORD0_FS_LOCATIONS, |
| 195 | FATTR4_WORD1_MODE |
| 196 | | FATTR4_WORD1_NUMLINKS |
| 197 | | FATTR4_WORD1_OWNER |
| 198 | | FATTR4_WORD1_OWNER_GROUP |
| 199 | | FATTR4_WORD1_RAWDEV |
| 200 | | FATTR4_WORD1_SPACE_USED |
| 201 | | FATTR4_WORD1_TIME_ACCESS |
| 202 | | FATTR4_WORD1_TIME_METADATA |
| 203 | | FATTR4_WORD1_TIME_MODIFY |
| 204 | | FATTR4_WORD1_MOUNTED_ON_FILEID |
| 205 | }; |
| 206 | |
| 207 | static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry, |
| 208 | struct nfs4_readdir_arg *readdir) |
| 209 | { |
| 210 | __be32 *start, *p; |
| 211 | |
| 212 | if (cookie > 2) { |
| 213 | readdir->cookie = cookie; |
| 214 | memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier)); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | readdir->cookie = 0; |
| 219 | memset(&readdir->verifier, 0, sizeof(readdir->verifier)); |
| 220 | if (cookie == 2) |
| 221 | return; |
| 222 | |
| 223 | /* |
| 224 | * NFSv4 servers do not return entries for '.' and '..' |
| 225 | * Therefore, we fake these entries here. We let '.' |
| 226 | * have cookie 0 and '..' have cookie 1. Note that |
| 227 | * when talking to the server, we always send cookie 0 |
| 228 | * instead of 1 or 2. |
| 229 | */ |
| 230 | start = p = kmap_atomic(*readdir->pages); |
| 231 | |
| 232 | if (cookie == 0) { |
| 233 | *p++ = xdr_one; /* next */ |
| 234 | *p++ = xdr_zero; /* cookie, first word */ |
| 235 | *p++ = xdr_one; /* cookie, second word */ |
| 236 | *p++ = xdr_one; /* entry len */ |
| 237 | memcpy(p, ".\0\0\0", 4); /* entry */ |
| 238 | p++; |
| 239 | *p++ = xdr_one; /* bitmap length */ |
| 240 | *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */ |
| 241 | *p++ = htonl(8); /* attribute buffer length */ |
| 242 | p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode)); |
| 243 | } |
| 244 | |
| 245 | *p++ = xdr_one; /* next */ |
| 246 | *p++ = xdr_zero; /* cookie, first word */ |
| 247 | *p++ = xdr_two; /* cookie, second word */ |
| 248 | *p++ = xdr_two; /* entry len */ |
| 249 | memcpy(p, "..\0\0", 4); /* entry */ |
| 250 | p++; |
| 251 | *p++ = xdr_one; /* bitmap length */ |
| 252 | *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */ |
| 253 | *p++ = htonl(8); /* attribute buffer length */ |
| 254 | p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode)); |
| 255 | |
| 256 | readdir->pgbase = (char *)p - (char *)start; |
| 257 | readdir->count -= readdir->pgbase; |
| 258 | kunmap_atomic(start); |
| 259 | } |
| 260 | |
| 261 | static int nfs4_delay(struct rpc_clnt *clnt, long *timeout) |
| 262 | { |
| 263 | int res = 0; |
| 264 | |
| 265 | might_sleep(); |
| 266 | |
| 267 | if (*timeout <= 0) |
| 268 | *timeout = NFS4_POLL_RETRY_MIN; |
| 269 | if (*timeout > NFS4_POLL_RETRY_MAX) |
| 270 | *timeout = NFS4_POLL_RETRY_MAX; |
| 271 | freezable_schedule_timeout_killable(*timeout); |
| 272 | if (fatal_signal_pending(current)) |
| 273 | res = -ERESTARTSYS; |
| 274 | *timeout <<= 1; |
| 275 | return res; |
| 276 | } |
| 277 | |
| 278 | /* This is the error handling routine for processes that are allowed |
| 279 | * to sleep. |
| 280 | */ |
| 281 | static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception) |
| 282 | { |
| 283 | struct nfs_client *clp = server->nfs_client; |
| 284 | struct nfs4_state *state = exception->state; |
| 285 | struct inode *inode = exception->inode; |
| 286 | int ret = errorcode; |
| 287 | |
| 288 | exception->retry = 0; |
| 289 | switch(errorcode) { |
| 290 | case 0: |
| 291 | return 0; |
| 292 | case -NFS4ERR_OPENMODE: |
| 293 | if (inode && nfs4_have_delegation(inode, FMODE_READ)) { |
| 294 | nfs4_inode_return_delegation(inode); |
| 295 | exception->retry = 1; |
| 296 | return 0; |
| 297 | } |
| 298 | if (state == NULL) |
| 299 | break; |
| 300 | ret = nfs4_schedule_stateid_recovery(server, state); |
| 301 | if (ret < 0) |
| 302 | break; |
| 303 | goto wait_on_recovery; |
| 304 | case -NFS4ERR_DELEG_REVOKED: |
| 305 | case -NFS4ERR_ADMIN_REVOKED: |
| 306 | case -NFS4ERR_BAD_STATEID: |
| 307 | if (state == NULL) |
| 308 | break; |
| 309 | nfs_remove_bad_delegation(state->inode); |
| 310 | ret = nfs4_schedule_stateid_recovery(server, state); |
| 311 | if (ret < 0) |
| 312 | break; |
| 313 | goto wait_on_recovery; |
| 314 | case -NFS4ERR_EXPIRED: |
| 315 | if (state != NULL) { |
| 316 | ret = nfs4_schedule_stateid_recovery(server, state); |
| 317 | if (ret < 0) |
| 318 | break; |
| 319 | } |
| 320 | case -NFS4ERR_STALE_STATEID: |
| 321 | case -NFS4ERR_STALE_CLIENTID: |
| 322 | nfs4_schedule_lease_recovery(clp); |
| 323 | goto wait_on_recovery; |
| 324 | #if defined(CONFIG_NFS_V4_1) |
| 325 | case -NFS4ERR_BADSESSION: |
| 326 | case -NFS4ERR_BADSLOT: |
| 327 | case -NFS4ERR_BAD_HIGH_SLOT: |
| 328 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: |
| 329 | case -NFS4ERR_DEADSESSION: |
| 330 | case -NFS4ERR_SEQ_FALSE_RETRY: |
| 331 | case -NFS4ERR_SEQ_MISORDERED: |
| 332 | dprintk("%s ERROR: %d Reset session\n", __func__, |
| 333 | errorcode); |
| 334 | nfs4_schedule_session_recovery(clp->cl_session, errorcode); |
| 335 | goto wait_on_recovery; |
| 336 | #endif /* defined(CONFIG_NFS_V4_1) */ |
| 337 | case -NFS4ERR_FILE_OPEN: |
| 338 | if (exception->timeout > HZ) { |
| 339 | /* We have retried a decent amount, time to |
| 340 | * fail |
| 341 | */ |
| 342 | ret = -EBUSY; |
| 343 | break; |
| 344 | } |
| 345 | case -NFS4ERR_GRACE: |
| 346 | case -NFS4ERR_DELAY: |
| 347 | ret = nfs4_delay(server->client, &exception->timeout); |
| 348 | if (ret != 0) |
| 349 | break; |
| 350 | case -NFS4ERR_RETRY_UNCACHED_REP: |
| 351 | case -NFS4ERR_OLD_STATEID: |
| 352 | exception->retry = 1; |
| 353 | break; |
| 354 | case -NFS4ERR_BADOWNER: |
| 355 | /* The following works around a Linux server bug! */ |
| 356 | case -NFS4ERR_BADNAME: |
| 357 | if (server->caps & NFS_CAP_UIDGID_NOMAP) { |
| 358 | server->caps &= ~NFS_CAP_UIDGID_NOMAP; |
| 359 | exception->retry = 1; |
| 360 | printk(KERN_WARNING "NFS: v4 server %s " |
| 361 | "does not accept raw " |
| 362 | "uid/gids. " |
| 363 | "Reenabling the idmapper.\n", |
| 364 | server->nfs_client->cl_hostname); |
| 365 | } |
| 366 | } |
| 367 | /* We failed to handle the error */ |
| 368 | return nfs4_map_errors(ret); |
| 369 | wait_on_recovery: |
| 370 | ret = nfs4_wait_clnt_recover(clp); |
| 371 | if (ret == 0) |
| 372 | exception->retry = 1; |
| 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp) |
| 378 | { |
| 379 | spin_lock(&clp->cl_lock); |
| 380 | if (time_before(clp->cl_last_renewal,timestamp)) |
| 381 | clp->cl_last_renewal = timestamp; |
| 382 | spin_unlock(&clp->cl_lock); |
| 383 | } |
| 384 | |
| 385 | static void renew_lease(const struct nfs_server *server, unsigned long timestamp) |
| 386 | { |
| 387 | do_renew_lease(server->nfs_client, timestamp); |
| 388 | } |
| 389 | |
| 390 | #if defined(CONFIG_NFS_V4_1) |
| 391 | |
| 392 | static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res) |
| 393 | { |
| 394 | struct nfs4_session *session; |
| 395 | struct nfs4_slot_table *tbl; |
| 396 | bool send_new_highest_used_slotid = false; |
| 397 | |
| 398 | if (!res->sr_slot) { |
| 399 | /* just wake up the next guy waiting since |
| 400 | * we may have not consumed a slot after all */ |
| 401 | dprintk("%s: No slot\n", __func__); |
| 402 | return; |
| 403 | } |
| 404 | tbl = res->sr_slot->table; |
| 405 | session = tbl->session; |
| 406 | |
| 407 | spin_lock(&tbl->slot_tbl_lock); |
| 408 | /* Be nice to the server: try to ensure that the last transmitted |
| 409 | * value for highest_user_slotid <= target_highest_slotid |
| 410 | */ |
| 411 | if (tbl->highest_used_slotid > tbl->target_highest_slotid) |
| 412 | send_new_highest_used_slotid = true; |
| 413 | |
| 414 | if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) { |
| 415 | send_new_highest_used_slotid = false; |
| 416 | goto out_unlock; |
| 417 | } |
| 418 | nfs4_free_slot(tbl, res->sr_slot); |
| 419 | |
| 420 | if (tbl->highest_used_slotid != NFS4_NO_SLOT) |
| 421 | send_new_highest_used_slotid = false; |
| 422 | out_unlock: |
| 423 | spin_unlock(&tbl->slot_tbl_lock); |
| 424 | res->sr_slot = NULL; |
| 425 | if (send_new_highest_used_slotid) |
| 426 | nfs41_server_notify_highest_slotid_update(session->clp); |
| 427 | } |
| 428 | |
| 429 | static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res) |
| 430 | { |
| 431 | struct nfs4_session *session; |
| 432 | struct nfs4_slot *slot; |
| 433 | struct nfs_client *clp; |
| 434 | bool interrupted = false; |
| 435 | int ret = 1; |
| 436 | |
| 437 | /* don't increment the sequence number if the task wasn't sent */ |
| 438 | if (!RPC_WAS_SENT(task)) |
| 439 | goto out; |
| 440 | |
| 441 | slot = res->sr_slot; |
| 442 | session = slot->table->session; |
| 443 | |
| 444 | if (slot->interrupted) { |
| 445 | slot->interrupted = 0; |
| 446 | interrupted = true; |
| 447 | } |
| 448 | |
| 449 | /* Check the SEQUENCE operation status */ |
| 450 | switch (res->sr_status) { |
| 451 | case 0: |
| 452 | /* Update the slot's sequence and clientid lease timer */ |
| 453 | ++slot->seq_nr; |
| 454 | clp = session->clp; |
| 455 | do_renew_lease(clp, res->sr_timestamp); |
| 456 | /* Check sequence flags */ |
| 457 | if (res->sr_status_flags != 0) |
| 458 | nfs4_schedule_lease_recovery(clp); |
| 459 | nfs41_update_target_slotid(slot->table, slot, res); |
| 460 | break; |
| 461 | case 1: |
| 462 | /* |
| 463 | * sr_status remains 1 if an RPC level error occurred. |
| 464 | * The server may or may not have processed the sequence |
| 465 | * operation.. |
| 466 | * Mark the slot as having hosted an interrupted RPC call. |
| 467 | */ |
| 468 | slot->interrupted = 1; |
| 469 | goto out; |
| 470 | case -NFS4ERR_DELAY: |
| 471 | /* The server detected a resend of the RPC call and |
| 472 | * returned NFS4ERR_DELAY as per Section 2.10.6.2 |
| 473 | * of RFC5661. |
| 474 | */ |
| 475 | dprintk("%s: slot=%u seq=%u: Operation in progress\n", |
| 476 | __func__, |
| 477 | slot->slot_nr, |
| 478 | slot->seq_nr); |
| 479 | goto out_retry; |
| 480 | case -NFS4ERR_BADSLOT: |
| 481 | /* |
| 482 | * The slot id we used was probably retired. Try again |
| 483 | * using a different slot id. |
| 484 | */ |
| 485 | goto retry_nowait; |
| 486 | case -NFS4ERR_SEQ_MISORDERED: |
| 487 | /* |
| 488 | * Was the last operation on this sequence interrupted? |
| 489 | * If so, retry after bumping the sequence number. |
| 490 | */ |
| 491 | if (interrupted) { |
| 492 | ++slot->seq_nr; |
| 493 | goto retry_nowait; |
| 494 | } |
| 495 | /* |
| 496 | * Could this slot have been previously retired? |
| 497 | * If so, then the server may be expecting seq_nr = 1! |
| 498 | */ |
| 499 | if (slot->seq_nr != 1) { |
| 500 | slot->seq_nr = 1; |
| 501 | goto retry_nowait; |
| 502 | } |
| 503 | break; |
| 504 | case -NFS4ERR_SEQ_FALSE_RETRY: |
| 505 | ++slot->seq_nr; |
| 506 | goto retry_nowait; |
| 507 | default: |
| 508 | /* Just update the slot sequence no. */ |
| 509 | ++slot->seq_nr; |
| 510 | } |
| 511 | out: |
| 512 | /* The session may be reset by one of the error handlers. */ |
| 513 | dprintk("%s: Error %d free the slot \n", __func__, res->sr_status); |
| 514 | nfs41_sequence_free_slot(res); |
| 515 | return ret; |
| 516 | retry_nowait: |
| 517 | if (rpc_restart_call_prepare(task)) { |
| 518 | task->tk_status = 0; |
| 519 | ret = 0; |
| 520 | } |
| 521 | goto out; |
| 522 | out_retry: |
| 523 | if (!rpc_restart_call(task)) |
| 524 | goto out; |
| 525 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static int nfs4_sequence_done(struct rpc_task *task, |
| 530 | struct nfs4_sequence_res *res) |
| 531 | { |
| 532 | if (res->sr_slot == NULL) |
| 533 | return 1; |
| 534 | return nfs41_sequence_done(task, res); |
| 535 | } |
| 536 | |
| 537 | static void nfs41_init_sequence(struct nfs4_sequence_args *args, |
| 538 | struct nfs4_sequence_res *res, int cache_reply) |
| 539 | { |
| 540 | args->sa_slot = NULL; |
| 541 | args->sa_cache_this = 0; |
| 542 | args->sa_privileged = 0; |
| 543 | if (cache_reply) |
| 544 | args->sa_cache_this = 1; |
| 545 | res->sr_slot = NULL; |
| 546 | } |
| 547 | |
| 548 | static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args) |
| 549 | { |
| 550 | args->sa_privileged = 1; |
| 551 | } |
| 552 | |
| 553 | int nfs41_setup_sequence(struct nfs4_session *session, |
| 554 | struct nfs4_sequence_args *args, |
| 555 | struct nfs4_sequence_res *res, |
| 556 | struct rpc_task *task) |
| 557 | { |
| 558 | struct nfs4_slot *slot; |
| 559 | struct nfs4_slot_table *tbl; |
| 560 | |
| 561 | dprintk("--> %s\n", __func__); |
| 562 | /* slot already allocated? */ |
| 563 | if (res->sr_slot != NULL) |
| 564 | goto out_success; |
| 565 | |
| 566 | tbl = &session->fc_slot_table; |
| 567 | |
| 568 | task->tk_timeout = 0; |
| 569 | |
| 570 | spin_lock(&tbl->slot_tbl_lock); |
| 571 | if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) && |
| 572 | !args->sa_privileged) { |
| 573 | /* The state manager will wait until the slot table is empty */ |
| 574 | dprintk("%s session is draining\n", __func__); |
| 575 | goto out_sleep; |
| 576 | } |
| 577 | |
| 578 | slot = nfs4_alloc_slot(tbl); |
| 579 | if (IS_ERR(slot)) { |
| 580 | /* If out of memory, try again in 1/4 second */ |
| 581 | if (slot == ERR_PTR(-ENOMEM)) |
| 582 | task->tk_timeout = HZ >> 2; |
| 583 | dprintk("<-- %s: no free slots\n", __func__); |
| 584 | goto out_sleep; |
| 585 | } |
| 586 | spin_unlock(&tbl->slot_tbl_lock); |
| 587 | |
| 588 | args->sa_slot = slot; |
| 589 | |
| 590 | dprintk("<-- %s slotid=%d seqid=%d\n", __func__, |
| 591 | slot->slot_nr, slot->seq_nr); |
| 592 | |
| 593 | res->sr_slot = slot; |
| 594 | res->sr_timestamp = jiffies; |
| 595 | res->sr_status_flags = 0; |
| 596 | /* |
| 597 | * sr_status is only set in decode_sequence, and so will remain |
| 598 | * set to 1 if an rpc level failure occurs. |
| 599 | */ |
| 600 | res->sr_status = 1; |
| 601 | out_success: |
| 602 | rpc_call_start(task); |
| 603 | return 0; |
| 604 | out_sleep: |
| 605 | /* Privileged tasks are queued with top priority */ |
| 606 | if (args->sa_privileged) |
| 607 | rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task, |
| 608 | NULL, RPC_PRIORITY_PRIVILEGED); |
| 609 | else |
| 610 | rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL); |
| 611 | spin_unlock(&tbl->slot_tbl_lock); |
| 612 | return -EAGAIN; |
| 613 | } |
| 614 | EXPORT_SYMBOL_GPL(nfs41_setup_sequence); |
| 615 | |
| 616 | int nfs4_setup_sequence(const struct nfs_server *server, |
| 617 | struct nfs4_sequence_args *args, |
| 618 | struct nfs4_sequence_res *res, |
| 619 | struct rpc_task *task) |
| 620 | { |
| 621 | struct nfs4_session *session = nfs4_get_session(server); |
| 622 | int ret = 0; |
| 623 | |
| 624 | if (session == NULL) { |
| 625 | rpc_call_start(task); |
| 626 | goto out; |
| 627 | } |
| 628 | |
| 629 | dprintk("--> %s clp %p session %p sr_slot %d\n", |
| 630 | __func__, session->clp, session, res->sr_slot ? |
| 631 | res->sr_slot->slot_nr : -1); |
| 632 | |
| 633 | ret = nfs41_setup_sequence(session, args, res, task); |
| 634 | out: |
| 635 | dprintk("<-- %s status=%d\n", __func__, ret); |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | struct nfs41_call_sync_data { |
| 640 | const struct nfs_server *seq_server; |
| 641 | struct nfs4_sequence_args *seq_args; |
| 642 | struct nfs4_sequence_res *seq_res; |
| 643 | }; |
| 644 | |
| 645 | static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata) |
| 646 | { |
| 647 | struct nfs41_call_sync_data *data = calldata; |
| 648 | struct nfs4_session *session = nfs4_get_session(data->seq_server); |
| 649 | |
| 650 | dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server); |
| 651 | |
| 652 | nfs41_setup_sequence(session, data->seq_args, data->seq_res, task); |
| 653 | } |
| 654 | |
| 655 | static void nfs41_call_sync_done(struct rpc_task *task, void *calldata) |
| 656 | { |
| 657 | struct nfs41_call_sync_data *data = calldata; |
| 658 | |
| 659 | nfs41_sequence_done(task, data->seq_res); |
| 660 | } |
| 661 | |
| 662 | static const struct rpc_call_ops nfs41_call_sync_ops = { |
| 663 | .rpc_call_prepare = nfs41_call_sync_prepare, |
| 664 | .rpc_call_done = nfs41_call_sync_done, |
| 665 | }; |
| 666 | |
| 667 | static int nfs4_call_sync_sequence(struct rpc_clnt *clnt, |
| 668 | struct nfs_server *server, |
| 669 | struct rpc_message *msg, |
| 670 | struct nfs4_sequence_args *args, |
| 671 | struct nfs4_sequence_res *res) |
| 672 | { |
| 673 | int ret; |
| 674 | struct rpc_task *task; |
| 675 | struct nfs41_call_sync_data data = { |
| 676 | .seq_server = server, |
| 677 | .seq_args = args, |
| 678 | .seq_res = res, |
| 679 | }; |
| 680 | struct rpc_task_setup task_setup = { |
| 681 | .rpc_client = clnt, |
| 682 | .rpc_message = msg, |
| 683 | .callback_ops = &nfs41_call_sync_ops, |
| 684 | .callback_data = &data |
| 685 | }; |
| 686 | |
| 687 | task = rpc_run_task(&task_setup); |
| 688 | if (IS_ERR(task)) |
| 689 | ret = PTR_ERR(task); |
| 690 | else { |
| 691 | ret = task->tk_status; |
| 692 | rpc_put_task(task); |
| 693 | } |
| 694 | return ret; |
| 695 | } |
| 696 | |
| 697 | #else |
| 698 | static |
| 699 | void nfs41_init_sequence(struct nfs4_sequence_args *args, |
| 700 | struct nfs4_sequence_res *res, int cache_reply) |
| 701 | { |
| 702 | } |
| 703 | |
| 704 | static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args) |
| 705 | { |
| 706 | } |
| 707 | |
| 708 | |
| 709 | static int nfs4_sequence_done(struct rpc_task *task, |
| 710 | struct nfs4_sequence_res *res) |
| 711 | { |
| 712 | return 1; |
| 713 | } |
| 714 | #endif /* CONFIG_NFS_V4_1 */ |
| 715 | |
| 716 | static |
| 717 | int _nfs4_call_sync(struct rpc_clnt *clnt, |
| 718 | struct nfs_server *server, |
| 719 | struct rpc_message *msg, |
| 720 | struct nfs4_sequence_args *args, |
| 721 | struct nfs4_sequence_res *res) |
| 722 | { |
| 723 | return rpc_call_sync(clnt, msg, 0); |
| 724 | } |
| 725 | |
| 726 | static |
| 727 | int nfs4_call_sync(struct rpc_clnt *clnt, |
| 728 | struct nfs_server *server, |
| 729 | struct rpc_message *msg, |
| 730 | struct nfs4_sequence_args *args, |
| 731 | struct nfs4_sequence_res *res, |
| 732 | int cache_reply) |
| 733 | { |
| 734 | nfs41_init_sequence(args, res, cache_reply); |
| 735 | return server->nfs_client->cl_mvops->call_sync(clnt, server, msg, |
| 736 | args, res); |
| 737 | } |
| 738 | |
| 739 | static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo) |
| 740 | { |
| 741 | struct nfs_inode *nfsi = NFS_I(dir); |
| 742 | |
| 743 | spin_lock(&dir->i_lock); |
| 744 | nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA; |
| 745 | if (!cinfo->atomic || cinfo->before != dir->i_version) |
| 746 | nfs_force_lookup_revalidate(dir); |
| 747 | dir->i_version = cinfo->after; |
| 748 | nfs_fscache_invalidate(dir); |
| 749 | spin_unlock(&dir->i_lock); |
| 750 | } |
| 751 | |
| 752 | struct nfs4_opendata { |
| 753 | struct kref kref; |
| 754 | struct nfs_openargs o_arg; |
| 755 | struct nfs_openres o_res; |
| 756 | struct nfs_open_confirmargs c_arg; |
| 757 | struct nfs_open_confirmres c_res; |
| 758 | struct nfs4_string owner_name; |
| 759 | struct nfs4_string group_name; |
| 760 | struct nfs_fattr f_attr; |
| 761 | struct dentry *dir; |
| 762 | struct dentry *dentry; |
| 763 | struct nfs4_state_owner *owner; |
| 764 | struct nfs4_state *state; |
| 765 | struct iattr attrs; |
| 766 | unsigned long timestamp; |
| 767 | unsigned int rpc_done : 1; |
| 768 | int rpc_status; |
| 769 | int cancelled; |
| 770 | }; |
| 771 | |
| 772 | static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server, |
| 773 | int err, struct nfs4_exception *exception) |
| 774 | { |
| 775 | if (err != -EINVAL) |
| 776 | return false; |
| 777 | if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1)) |
| 778 | return false; |
| 779 | server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1; |
| 780 | exception->retry = 1; |
| 781 | return true; |
| 782 | } |
| 783 | |
| 784 | static enum open_claim_type4 |
| 785 | nfs4_map_atomic_open_claim(struct nfs_server *server, |
| 786 | enum open_claim_type4 claim) |
| 787 | { |
| 788 | if (server->caps & NFS_CAP_ATOMIC_OPEN_V1) |
| 789 | return claim; |
| 790 | switch (claim) { |
| 791 | default: |
| 792 | return claim; |
| 793 | case NFS4_OPEN_CLAIM_FH: |
| 794 | return NFS4_OPEN_CLAIM_NULL; |
| 795 | case NFS4_OPEN_CLAIM_DELEG_CUR_FH: |
| 796 | return NFS4_OPEN_CLAIM_DELEGATE_CUR; |
| 797 | case NFS4_OPEN_CLAIM_DELEG_PREV_FH: |
| 798 | return NFS4_OPEN_CLAIM_DELEGATE_PREV; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | static void nfs4_init_opendata_res(struct nfs4_opendata *p) |
| 803 | { |
| 804 | p->o_res.f_attr = &p->f_attr; |
| 805 | p->o_res.seqid = p->o_arg.seqid; |
| 806 | p->c_res.seqid = p->c_arg.seqid; |
| 807 | p->o_res.server = p->o_arg.server; |
| 808 | p->o_res.access_request = p->o_arg.access; |
| 809 | nfs_fattr_init(&p->f_attr); |
| 810 | nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name); |
| 811 | } |
| 812 | |
| 813 | static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry, |
| 814 | struct nfs4_state_owner *sp, fmode_t fmode, int flags, |
| 815 | const struct iattr *attrs, |
| 816 | enum open_claim_type4 claim, |
| 817 | gfp_t gfp_mask) |
| 818 | { |
| 819 | struct dentry *parent = dget_parent(dentry); |
| 820 | struct inode *dir = parent->d_inode; |
| 821 | struct nfs_server *server = NFS_SERVER(dir); |
| 822 | struct nfs4_opendata *p; |
| 823 | |
| 824 | p = kzalloc(sizeof(*p), gfp_mask); |
| 825 | if (p == NULL) |
| 826 | goto err; |
| 827 | p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask); |
| 828 | if (p->o_arg.seqid == NULL) |
| 829 | goto err_free; |
| 830 | nfs_sb_active(dentry->d_sb); |
| 831 | p->dentry = dget(dentry); |
| 832 | p->dir = parent; |
| 833 | p->owner = sp; |
| 834 | atomic_inc(&sp->so_count); |
| 835 | p->o_arg.open_flags = flags; |
| 836 | p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE); |
| 837 | /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS |
| 838 | * will return permission denied for all bits until close */ |
| 839 | if (!(flags & O_EXCL)) { |
| 840 | /* ask server to check for all possible rights as results |
| 841 | * are cached */ |
| 842 | p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY | |
| 843 | NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE; |
| 844 | } |
| 845 | p->o_arg.clientid = server->nfs_client->cl_clientid; |
| 846 | p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time); |
| 847 | p->o_arg.id.uniquifier = sp->so_seqid.owner_id; |
| 848 | p->o_arg.name = &dentry->d_name; |
| 849 | p->o_arg.server = server; |
| 850 | p->o_arg.bitmask = server->attr_bitmask; |
| 851 | p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0]; |
| 852 | p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim); |
| 853 | switch (p->o_arg.claim) { |
| 854 | case NFS4_OPEN_CLAIM_NULL: |
| 855 | case NFS4_OPEN_CLAIM_DELEGATE_CUR: |
| 856 | case NFS4_OPEN_CLAIM_DELEGATE_PREV: |
| 857 | p->o_arg.fh = NFS_FH(dir); |
| 858 | break; |
| 859 | case NFS4_OPEN_CLAIM_PREVIOUS: |
| 860 | case NFS4_OPEN_CLAIM_FH: |
| 861 | case NFS4_OPEN_CLAIM_DELEG_CUR_FH: |
| 862 | case NFS4_OPEN_CLAIM_DELEG_PREV_FH: |
| 863 | p->o_arg.fh = NFS_FH(dentry->d_inode); |
| 864 | } |
| 865 | if (attrs != NULL && attrs->ia_valid != 0) { |
| 866 | __be32 verf[2]; |
| 867 | |
| 868 | p->o_arg.u.attrs = &p->attrs; |
| 869 | memcpy(&p->attrs, attrs, sizeof(p->attrs)); |
| 870 | |
| 871 | verf[0] = jiffies; |
| 872 | verf[1] = current->pid; |
| 873 | memcpy(p->o_arg.u.verifier.data, verf, |
| 874 | sizeof(p->o_arg.u.verifier.data)); |
| 875 | } |
| 876 | p->c_arg.fh = &p->o_res.fh; |
| 877 | p->c_arg.stateid = &p->o_res.stateid; |
| 878 | p->c_arg.seqid = p->o_arg.seqid; |
| 879 | nfs4_init_opendata_res(p); |
| 880 | kref_init(&p->kref); |
| 881 | return p; |
| 882 | err_free: |
| 883 | kfree(p); |
| 884 | err: |
| 885 | dput(parent); |
| 886 | return NULL; |
| 887 | } |
| 888 | |
| 889 | static void nfs4_opendata_free(struct kref *kref) |
| 890 | { |
| 891 | struct nfs4_opendata *p = container_of(kref, |
| 892 | struct nfs4_opendata, kref); |
| 893 | struct super_block *sb = p->dentry->d_sb; |
| 894 | |
| 895 | nfs_free_seqid(p->o_arg.seqid); |
| 896 | if (p->state != NULL) |
| 897 | nfs4_put_open_state(p->state); |
| 898 | nfs4_put_state_owner(p->owner); |
| 899 | dput(p->dir); |
| 900 | dput(p->dentry); |
| 901 | nfs_sb_deactive(sb); |
| 902 | nfs_fattr_free_names(&p->f_attr); |
| 903 | kfree(p); |
| 904 | } |
| 905 | |
| 906 | static void nfs4_opendata_put(struct nfs4_opendata *p) |
| 907 | { |
| 908 | if (p != NULL) |
| 909 | kref_put(&p->kref, nfs4_opendata_free); |
| 910 | } |
| 911 | |
| 912 | static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task) |
| 913 | { |
| 914 | int ret; |
| 915 | |
| 916 | ret = rpc_wait_for_completion_task(task); |
| 917 | return ret; |
| 918 | } |
| 919 | |
| 920 | static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode) |
| 921 | { |
| 922 | int ret = 0; |
| 923 | |
| 924 | if (open_mode & (O_EXCL|O_TRUNC)) |
| 925 | goto out; |
| 926 | switch (mode & (FMODE_READ|FMODE_WRITE)) { |
| 927 | case FMODE_READ: |
| 928 | ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0 |
| 929 | && state->n_rdonly != 0; |
| 930 | break; |
| 931 | case FMODE_WRITE: |
| 932 | ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0 |
| 933 | && state->n_wronly != 0; |
| 934 | break; |
| 935 | case FMODE_READ|FMODE_WRITE: |
| 936 | ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0 |
| 937 | && state->n_rdwr != 0; |
| 938 | } |
| 939 | out: |
| 940 | return ret; |
| 941 | } |
| 942 | |
| 943 | static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode) |
| 944 | { |
| 945 | if (delegation == NULL) |
| 946 | return 0; |
| 947 | if ((delegation->type & fmode) != fmode) |
| 948 | return 0; |
| 949 | if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags)) |
| 950 | return 0; |
| 951 | if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) |
| 952 | return 0; |
| 953 | nfs_mark_delegation_referenced(delegation); |
| 954 | return 1; |
| 955 | } |
| 956 | |
| 957 | static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode) |
| 958 | { |
| 959 | switch (fmode) { |
| 960 | case FMODE_WRITE: |
| 961 | state->n_wronly++; |
| 962 | break; |
| 963 | case FMODE_READ: |
| 964 | state->n_rdonly++; |
| 965 | break; |
| 966 | case FMODE_READ|FMODE_WRITE: |
| 967 | state->n_rdwr++; |
| 968 | } |
| 969 | nfs4_state_set_mode_locked(state, state->state | fmode); |
| 970 | } |
| 971 | |
| 972 | static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode) |
| 973 | { |
| 974 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) |
| 975 | nfs4_stateid_copy(&state->stateid, stateid); |
| 976 | nfs4_stateid_copy(&state->open_stateid, stateid); |
| 977 | switch (fmode) { |
| 978 | case FMODE_READ: |
| 979 | set_bit(NFS_O_RDONLY_STATE, &state->flags); |
| 980 | break; |
| 981 | case FMODE_WRITE: |
| 982 | set_bit(NFS_O_WRONLY_STATE, &state->flags); |
| 983 | break; |
| 984 | case FMODE_READ|FMODE_WRITE: |
| 985 | set_bit(NFS_O_RDWR_STATE, &state->flags); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode) |
| 990 | { |
| 991 | write_seqlock(&state->seqlock); |
| 992 | nfs_set_open_stateid_locked(state, stateid, fmode); |
| 993 | write_sequnlock(&state->seqlock); |
| 994 | } |
| 995 | |
| 996 | static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode) |
| 997 | { |
| 998 | /* |
| 999 | * Protect the call to nfs4_state_set_mode_locked and |
| 1000 | * serialise the stateid update |
| 1001 | */ |
| 1002 | write_seqlock(&state->seqlock); |
| 1003 | if (deleg_stateid != NULL) { |
| 1004 | nfs4_stateid_copy(&state->stateid, deleg_stateid); |
| 1005 | set_bit(NFS_DELEGATED_STATE, &state->flags); |
| 1006 | } |
| 1007 | if (open_stateid != NULL) |
| 1008 | nfs_set_open_stateid_locked(state, open_stateid, fmode); |
| 1009 | write_sequnlock(&state->seqlock); |
| 1010 | spin_lock(&state->owner->so_lock); |
| 1011 | update_open_stateflags(state, fmode); |
| 1012 | spin_unlock(&state->owner->so_lock); |
| 1013 | } |
| 1014 | |
| 1015 | static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode) |
| 1016 | { |
| 1017 | struct nfs_inode *nfsi = NFS_I(state->inode); |
| 1018 | struct nfs_delegation *deleg_cur; |
| 1019 | int ret = 0; |
| 1020 | |
| 1021 | fmode &= (FMODE_READ|FMODE_WRITE); |
| 1022 | |
| 1023 | rcu_read_lock(); |
| 1024 | deleg_cur = rcu_dereference(nfsi->delegation); |
| 1025 | if (deleg_cur == NULL) |
| 1026 | goto no_delegation; |
| 1027 | |
| 1028 | spin_lock(&deleg_cur->lock); |
| 1029 | if (nfsi->delegation != deleg_cur || |
| 1030 | test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) || |
| 1031 | (deleg_cur->type & fmode) != fmode) |
| 1032 | goto no_delegation_unlock; |
| 1033 | |
| 1034 | if (delegation == NULL) |
| 1035 | delegation = &deleg_cur->stateid; |
| 1036 | else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation)) |
| 1037 | goto no_delegation_unlock; |
| 1038 | |
| 1039 | nfs_mark_delegation_referenced(deleg_cur); |
| 1040 | __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode); |
| 1041 | ret = 1; |
| 1042 | no_delegation_unlock: |
| 1043 | spin_unlock(&deleg_cur->lock); |
| 1044 | no_delegation: |
| 1045 | rcu_read_unlock(); |
| 1046 | |
| 1047 | if (!ret && open_stateid != NULL) { |
| 1048 | __update_open_stateid(state, open_stateid, NULL, fmode); |
| 1049 | ret = 1; |
| 1050 | } |
| 1051 | |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
| 1055 | |
| 1056 | static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode) |
| 1057 | { |
| 1058 | struct nfs_delegation *delegation; |
| 1059 | |
| 1060 | rcu_read_lock(); |
| 1061 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
| 1062 | if (delegation == NULL || (delegation->type & fmode) == fmode) { |
| 1063 | rcu_read_unlock(); |
| 1064 | return; |
| 1065 | } |
| 1066 | rcu_read_unlock(); |
| 1067 | nfs4_inode_return_delegation(inode); |
| 1068 | } |
| 1069 | |
| 1070 | static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata) |
| 1071 | { |
| 1072 | struct nfs4_state *state = opendata->state; |
| 1073 | struct nfs_inode *nfsi = NFS_I(state->inode); |
| 1074 | struct nfs_delegation *delegation; |
| 1075 | int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC); |
| 1076 | fmode_t fmode = opendata->o_arg.fmode; |
| 1077 | nfs4_stateid stateid; |
| 1078 | int ret = -EAGAIN; |
| 1079 | |
| 1080 | for (;;) { |
| 1081 | if (can_open_cached(state, fmode, open_mode)) { |
| 1082 | spin_lock(&state->owner->so_lock); |
| 1083 | if (can_open_cached(state, fmode, open_mode)) { |
| 1084 | update_open_stateflags(state, fmode); |
| 1085 | spin_unlock(&state->owner->so_lock); |
| 1086 | goto out_return_state; |
| 1087 | } |
| 1088 | spin_unlock(&state->owner->so_lock); |
| 1089 | } |
| 1090 | rcu_read_lock(); |
| 1091 | delegation = rcu_dereference(nfsi->delegation); |
| 1092 | if (!can_open_delegated(delegation, fmode)) { |
| 1093 | rcu_read_unlock(); |
| 1094 | break; |
| 1095 | } |
| 1096 | /* Save the delegation */ |
| 1097 | nfs4_stateid_copy(&stateid, &delegation->stateid); |
| 1098 | rcu_read_unlock(); |
| 1099 | ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode); |
| 1100 | if (ret != 0) |
| 1101 | goto out; |
| 1102 | ret = -EAGAIN; |
| 1103 | |
| 1104 | /* Try to update the stateid using the delegation */ |
| 1105 | if (update_open_stateid(state, NULL, &stateid, fmode)) |
| 1106 | goto out_return_state; |
| 1107 | } |
| 1108 | out: |
| 1109 | return ERR_PTR(ret); |
| 1110 | out_return_state: |
| 1111 | atomic_inc(&state->count); |
| 1112 | return state; |
| 1113 | } |
| 1114 | |
| 1115 | static void |
| 1116 | nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state) |
| 1117 | { |
| 1118 | struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client; |
| 1119 | struct nfs_delegation *delegation; |
| 1120 | int delegation_flags = 0; |
| 1121 | |
| 1122 | rcu_read_lock(); |
| 1123 | delegation = rcu_dereference(NFS_I(state->inode)->delegation); |
| 1124 | if (delegation) |
| 1125 | delegation_flags = delegation->flags; |
| 1126 | rcu_read_unlock(); |
| 1127 | if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) { |
| 1128 | pr_err_ratelimited("NFS: Broken NFSv4 server %s is " |
| 1129 | "returning a delegation for " |
| 1130 | "OPEN(CLAIM_DELEGATE_CUR)\n", |
| 1131 | clp->cl_hostname); |
| 1132 | } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0) |
| 1133 | nfs_inode_set_delegation(state->inode, |
| 1134 | data->owner->so_cred, |
| 1135 | &data->o_res); |
| 1136 | else |
| 1137 | nfs_inode_reclaim_delegation(state->inode, |
| 1138 | data->owner->so_cred, |
| 1139 | &data->o_res); |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * Check the inode attributes against the CLAIM_PREVIOUS returned attributes |
| 1144 | * and update the nfs4_state. |
| 1145 | */ |
| 1146 | static struct nfs4_state * |
| 1147 | _nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data) |
| 1148 | { |
| 1149 | struct inode *inode = data->state->inode; |
| 1150 | struct nfs4_state *state = data->state; |
| 1151 | int ret; |
| 1152 | |
| 1153 | if (!data->rpc_done) { |
| 1154 | ret = data->rpc_status; |
| 1155 | goto err; |
| 1156 | } |
| 1157 | |
| 1158 | ret = -ESTALE; |
| 1159 | if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) || |
| 1160 | !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) || |
| 1161 | !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE)) |
| 1162 | goto err; |
| 1163 | |
| 1164 | ret = -ENOMEM; |
| 1165 | state = nfs4_get_open_state(inode, data->owner); |
| 1166 | if (state == NULL) |
| 1167 | goto err; |
| 1168 | |
| 1169 | ret = nfs_refresh_inode(inode, &data->f_attr); |
| 1170 | if (ret) |
| 1171 | goto err; |
| 1172 | |
| 1173 | if (data->o_res.delegation_type != 0) |
| 1174 | nfs4_opendata_check_deleg(data, state); |
| 1175 | update_open_stateid(state, &data->o_res.stateid, NULL, |
| 1176 | data->o_arg.fmode); |
| 1177 | |
| 1178 | return state; |
| 1179 | err: |
| 1180 | return ERR_PTR(ret); |
| 1181 | |
| 1182 | } |
| 1183 | |
| 1184 | static struct nfs4_state * |
| 1185 | _nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) |
| 1186 | { |
| 1187 | struct inode *inode; |
| 1188 | struct nfs4_state *state = NULL; |
| 1189 | int ret; |
| 1190 | |
| 1191 | if (!data->rpc_done) { |
| 1192 | state = nfs4_try_open_cached(data); |
| 1193 | goto out; |
| 1194 | } |
| 1195 | |
| 1196 | ret = -EAGAIN; |
| 1197 | if (!(data->f_attr.valid & NFS_ATTR_FATTR)) |
| 1198 | goto err; |
| 1199 | inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr); |
| 1200 | ret = PTR_ERR(inode); |
| 1201 | if (IS_ERR(inode)) |
| 1202 | goto err; |
| 1203 | ret = -ENOMEM; |
| 1204 | state = nfs4_get_open_state(inode, data->owner); |
| 1205 | if (state == NULL) |
| 1206 | goto err_put_inode; |
| 1207 | if (data->o_res.delegation_type != 0) |
| 1208 | nfs4_opendata_check_deleg(data, state); |
| 1209 | update_open_stateid(state, &data->o_res.stateid, NULL, |
| 1210 | data->o_arg.fmode); |
| 1211 | iput(inode); |
| 1212 | out: |
| 1213 | nfs_release_seqid(data->o_arg.seqid); |
| 1214 | return state; |
| 1215 | err_put_inode: |
| 1216 | iput(inode); |
| 1217 | err: |
| 1218 | return ERR_PTR(ret); |
| 1219 | } |
| 1220 | |
| 1221 | static struct nfs4_state * |
| 1222 | nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) |
| 1223 | { |
| 1224 | if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) |
| 1225 | return _nfs4_opendata_reclaim_to_nfs4_state(data); |
| 1226 | return _nfs4_opendata_to_nfs4_state(data); |
| 1227 | } |
| 1228 | |
| 1229 | static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state) |
| 1230 | { |
| 1231 | struct nfs_inode *nfsi = NFS_I(state->inode); |
| 1232 | struct nfs_open_context *ctx; |
| 1233 | |
| 1234 | spin_lock(&state->inode->i_lock); |
| 1235 | list_for_each_entry(ctx, &nfsi->open_files, list) { |
| 1236 | if (ctx->state != state) |
| 1237 | continue; |
| 1238 | get_nfs_open_context(ctx); |
| 1239 | spin_unlock(&state->inode->i_lock); |
| 1240 | return ctx; |
| 1241 | } |
| 1242 | spin_unlock(&state->inode->i_lock); |
| 1243 | return ERR_PTR(-ENOENT); |
| 1244 | } |
| 1245 | |
| 1246 | static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, |
| 1247 | struct nfs4_state *state, enum open_claim_type4 claim) |
| 1248 | { |
| 1249 | struct nfs4_opendata *opendata; |
| 1250 | |
| 1251 | opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, |
| 1252 | NULL, claim, GFP_NOFS); |
| 1253 | if (opendata == NULL) |
| 1254 | return ERR_PTR(-ENOMEM); |
| 1255 | opendata->state = state; |
| 1256 | atomic_inc(&state->count); |
| 1257 | return opendata; |
| 1258 | } |
| 1259 | |
| 1260 | static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res) |
| 1261 | { |
| 1262 | struct nfs4_state *newstate; |
| 1263 | int ret; |
| 1264 | |
| 1265 | opendata->o_arg.open_flags = 0; |
| 1266 | opendata->o_arg.fmode = fmode; |
| 1267 | memset(&opendata->o_res, 0, sizeof(opendata->o_res)); |
| 1268 | memset(&opendata->c_res, 0, sizeof(opendata->c_res)); |
| 1269 | nfs4_init_opendata_res(opendata); |
| 1270 | ret = _nfs4_recover_proc_open(opendata); |
| 1271 | if (ret != 0) |
| 1272 | return ret; |
| 1273 | newstate = nfs4_opendata_to_nfs4_state(opendata); |
| 1274 | if (IS_ERR(newstate)) |
| 1275 | return PTR_ERR(newstate); |
| 1276 | nfs4_close_state(newstate, fmode); |
| 1277 | *res = newstate; |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state) |
| 1282 | { |
| 1283 | struct nfs4_state *newstate; |
| 1284 | int ret; |
| 1285 | |
| 1286 | /* memory barrier prior to reading state->n_* */ |
| 1287 | clear_bit(NFS_DELEGATED_STATE, &state->flags); |
| 1288 | smp_rmb(); |
| 1289 | if (state->n_rdwr != 0) { |
| 1290 | clear_bit(NFS_O_RDWR_STATE, &state->flags); |
| 1291 | ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate); |
| 1292 | if (ret != 0) |
| 1293 | return ret; |
| 1294 | if (newstate != state) |
| 1295 | return -ESTALE; |
| 1296 | } |
| 1297 | if (state->n_wronly != 0) { |
| 1298 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); |
| 1299 | ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate); |
| 1300 | if (ret != 0) |
| 1301 | return ret; |
| 1302 | if (newstate != state) |
| 1303 | return -ESTALE; |
| 1304 | } |
| 1305 | if (state->n_rdonly != 0) { |
| 1306 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); |
| 1307 | ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate); |
| 1308 | if (ret != 0) |
| 1309 | return ret; |
| 1310 | if (newstate != state) |
| 1311 | return -ESTALE; |
| 1312 | } |
| 1313 | /* |
| 1314 | * We may have performed cached opens for all three recoveries. |
| 1315 | * Check if we need to update the current stateid. |
| 1316 | */ |
| 1317 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 && |
| 1318 | !nfs4_stateid_match(&state->stateid, &state->open_stateid)) { |
| 1319 | write_seqlock(&state->seqlock); |
| 1320 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) |
| 1321 | nfs4_stateid_copy(&state->stateid, &state->open_stateid); |
| 1322 | write_sequnlock(&state->seqlock); |
| 1323 | } |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | /* |
| 1328 | * OPEN_RECLAIM: |
| 1329 | * reclaim state on the server after a reboot. |
| 1330 | */ |
| 1331 | static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) |
| 1332 | { |
| 1333 | struct nfs_delegation *delegation; |
| 1334 | struct nfs4_opendata *opendata; |
| 1335 | fmode_t delegation_type = 0; |
| 1336 | int status; |
| 1337 | |
| 1338 | opendata = nfs4_open_recoverdata_alloc(ctx, state, |
| 1339 | NFS4_OPEN_CLAIM_PREVIOUS); |
| 1340 | if (IS_ERR(opendata)) |
| 1341 | return PTR_ERR(opendata); |
| 1342 | rcu_read_lock(); |
| 1343 | delegation = rcu_dereference(NFS_I(state->inode)->delegation); |
| 1344 | if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0) |
| 1345 | delegation_type = delegation->type; |
| 1346 | rcu_read_unlock(); |
| 1347 | opendata->o_arg.u.delegation_type = delegation_type; |
| 1348 | status = nfs4_open_recover(opendata, state); |
| 1349 | nfs4_opendata_put(opendata); |
| 1350 | return status; |
| 1351 | } |
| 1352 | |
| 1353 | static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) |
| 1354 | { |
| 1355 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 1356 | struct nfs4_exception exception = { }; |
| 1357 | int err; |
| 1358 | do { |
| 1359 | err = _nfs4_do_open_reclaim(ctx, state); |
| 1360 | if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) |
| 1361 | continue; |
| 1362 | if (err != -NFS4ERR_DELAY) |
| 1363 | break; |
| 1364 | nfs4_handle_exception(server, err, &exception); |
| 1365 | } while (exception.retry); |
| 1366 | return err; |
| 1367 | } |
| 1368 | |
| 1369 | static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state) |
| 1370 | { |
| 1371 | struct nfs_open_context *ctx; |
| 1372 | int ret; |
| 1373 | |
| 1374 | ctx = nfs4_state_find_open_context(state); |
| 1375 | if (IS_ERR(ctx)) |
| 1376 | return -EAGAIN; |
| 1377 | ret = nfs4_do_open_reclaim(ctx, state); |
| 1378 | put_nfs_open_context(ctx); |
| 1379 | return ret; |
| 1380 | } |
| 1381 | |
| 1382 | static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid) |
| 1383 | { |
| 1384 | struct nfs4_opendata *opendata; |
| 1385 | int ret; |
| 1386 | |
| 1387 | opendata = nfs4_open_recoverdata_alloc(ctx, state, |
| 1388 | NFS4_OPEN_CLAIM_DELEG_CUR_FH); |
| 1389 | if (IS_ERR(opendata)) |
| 1390 | return PTR_ERR(opendata); |
| 1391 | nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid); |
| 1392 | ret = nfs4_open_recover(opendata, state); |
| 1393 | nfs4_opendata_put(opendata); |
| 1394 | return ret; |
| 1395 | } |
| 1396 | |
| 1397 | int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid) |
| 1398 | { |
| 1399 | struct nfs4_exception exception = { }; |
| 1400 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 1401 | int err; |
| 1402 | do { |
| 1403 | err = _nfs4_open_delegation_recall(ctx, state, stateid); |
| 1404 | switch (err) { |
| 1405 | case 0: |
| 1406 | case -ENOENT: |
| 1407 | case -ESTALE: |
| 1408 | goto out; |
| 1409 | case -NFS4ERR_BADSESSION: |
| 1410 | case -NFS4ERR_BADSLOT: |
| 1411 | case -NFS4ERR_BAD_HIGH_SLOT: |
| 1412 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: |
| 1413 | case -NFS4ERR_DEADSESSION: |
| 1414 | set_bit(NFS_DELEGATED_STATE, &state->flags); |
| 1415 | nfs4_schedule_session_recovery(server->nfs_client->cl_session, err); |
| 1416 | err = -EAGAIN; |
| 1417 | goto out; |
| 1418 | case -NFS4ERR_STALE_CLIENTID: |
| 1419 | case -NFS4ERR_STALE_STATEID: |
| 1420 | set_bit(NFS_DELEGATED_STATE, &state->flags); |
| 1421 | case -NFS4ERR_EXPIRED: |
| 1422 | /* Don't recall a delegation if it was lost */ |
| 1423 | nfs4_schedule_lease_recovery(server->nfs_client); |
| 1424 | err = -EAGAIN; |
| 1425 | goto out; |
| 1426 | case -NFS4ERR_DELEG_REVOKED: |
| 1427 | case -NFS4ERR_ADMIN_REVOKED: |
| 1428 | case -NFS4ERR_BAD_STATEID: |
| 1429 | nfs_inode_find_state_and_recover(state->inode, |
| 1430 | stateid); |
| 1431 | nfs4_schedule_stateid_recovery(server, state); |
| 1432 | case -ENOMEM: |
| 1433 | err = 0; |
| 1434 | goto out; |
| 1435 | } |
| 1436 | set_bit(NFS_DELEGATED_STATE, &state->flags); |
| 1437 | err = nfs4_handle_exception(server, err, &exception); |
| 1438 | } while (exception.retry); |
| 1439 | out: |
| 1440 | return err; |
| 1441 | } |
| 1442 | |
| 1443 | static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata) |
| 1444 | { |
| 1445 | struct nfs4_opendata *data = calldata; |
| 1446 | |
| 1447 | data->rpc_status = task->tk_status; |
| 1448 | if (data->rpc_status == 0) { |
| 1449 | nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid); |
| 1450 | nfs_confirm_seqid(&data->owner->so_seqid, 0); |
| 1451 | renew_lease(data->o_res.server, data->timestamp); |
| 1452 | data->rpc_done = 1; |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | static void nfs4_open_confirm_release(void *calldata) |
| 1457 | { |
| 1458 | struct nfs4_opendata *data = calldata; |
| 1459 | struct nfs4_state *state = NULL; |
| 1460 | |
| 1461 | /* If this request hasn't been cancelled, do nothing */ |
| 1462 | if (data->cancelled == 0) |
| 1463 | goto out_free; |
| 1464 | /* In case of error, no cleanup! */ |
| 1465 | if (!data->rpc_done) |
| 1466 | goto out_free; |
| 1467 | state = nfs4_opendata_to_nfs4_state(data); |
| 1468 | if (!IS_ERR(state)) |
| 1469 | nfs4_close_state(state, data->o_arg.fmode); |
| 1470 | out_free: |
| 1471 | nfs4_opendata_put(data); |
| 1472 | } |
| 1473 | |
| 1474 | static const struct rpc_call_ops nfs4_open_confirm_ops = { |
| 1475 | .rpc_call_done = nfs4_open_confirm_done, |
| 1476 | .rpc_release = nfs4_open_confirm_release, |
| 1477 | }; |
| 1478 | |
| 1479 | /* |
| 1480 | * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata |
| 1481 | */ |
| 1482 | static int _nfs4_proc_open_confirm(struct nfs4_opendata *data) |
| 1483 | { |
| 1484 | struct nfs_server *server = NFS_SERVER(data->dir->d_inode); |
| 1485 | struct rpc_task *task; |
| 1486 | struct rpc_message msg = { |
| 1487 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM], |
| 1488 | .rpc_argp = &data->c_arg, |
| 1489 | .rpc_resp = &data->c_res, |
| 1490 | .rpc_cred = data->owner->so_cred, |
| 1491 | }; |
| 1492 | struct rpc_task_setup task_setup_data = { |
| 1493 | .rpc_client = server->client, |
| 1494 | .rpc_message = &msg, |
| 1495 | .callback_ops = &nfs4_open_confirm_ops, |
| 1496 | .callback_data = data, |
| 1497 | .workqueue = nfsiod_workqueue, |
| 1498 | .flags = RPC_TASK_ASYNC, |
| 1499 | }; |
| 1500 | int status; |
| 1501 | |
| 1502 | kref_get(&data->kref); |
| 1503 | data->rpc_done = 0; |
| 1504 | data->rpc_status = 0; |
| 1505 | data->timestamp = jiffies; |
| 1506 | task = rpc_run_task(&task_setup_data); |
| 1507 | if (IS_ERR(task)) |
| 1508 | return PTR_ERR(task); |
| 1509 | status = nfs4_wait_for_completion_rpc_task(task); |
| 1510 | if (status != 0) { |
| 1511 | data->cancelled = 1; |
| 1512 | smp_wmb(); |
| 1513 | } else |
| 1514 | status = data->rpc_status; |
| 1515 | rpc_put_task(task); |
| 1516 | return status; |
| 1517 | } |
| 1518 | |
| 1519 | static void nfs4_open_prepare(struct rpc_task *task, void *calldata) |
| 1520 | { |
| 1521 | struct nfs4_opendata *data = calldata; |
| 1522 | struct nfs4_state_owner *sp = data->owner; |
| 1523 | |
| 1524 | if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0) |
| 1525 | goto out_wait; |
| 1526 | /* |
| 1527 | * Check if we still need to send an OPEN call, or if we can use |
| 1528 | * a delegation instead. |
| 1529 | */ |
| 1530 | if (data->state != NULL) { |
| 1531 | struct nfs_delegation *delegation; |
| 1532 | |
| 1533 | if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags)) |
| 1534 | goto out_no_action; |
| 1535 | rcu_read_lock(); |
| 1536 | delegation = rcu_dereference(NFS_I(data->state->inode)->delegation); |
| 1537 | if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR && |
| 1538 | can_open_delegated(delegation, data->o_arg.fmode)) |
| 1539 | goto unlock_no_action; |
| 1540 | rcu_read_unlock(); |
| 1541 | } |
| 1542 | /* Update client id. */ |
| 1543 | data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid; |
| 1544 | if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) { |
| 1545 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR]; |
| 1546 | data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0]; |
| 1547 | nfs_copy_fh(&data->o_res.fh, data->o_arg.fh); |
| 1548 | } |
| 1549 | data->timestamp = jiffies; |
| 1550 | if (nfs4_setup_sequence(data->o_arg.server, |
| 1551 | &data->o_arg.seq_args, |
| 1552 | &data->o_res.seq_res, |
| 1553 | task) != 0) |
| 1554 | nfs_release_seqid(data->o_arg.seqid); |
| 1555 | return; |
| 1556 | unlock_no_action: |
| 1557 | rcu_read_unlock(); |
| 1558 | out_no_action: |
| 1559 | task->tk_action = NULL; |
| 1560 | out_wait: |
| 1561 | nfs4_sequence_done(task, &data->o_res.seq_res); |
| 1562 | } |
| 1563 | |
| 1564 | static void nfs4_open_done(struct rpc_task *task, void *calldata) |
| 1565 | { |
| 1566 | struct nfs4_opendata *data = calldata; |
| 1567 | |
| 1568 | data->rpc_status = task->tk_status; |
| 1569 | |
| 1570 | if (!nfs4_sequence_done(task, &data->o_res.seq_res)) |
| 1571 | return; |
| 1572 | |
| 1573 | if (task->tk_status == 0) { |
| 1574 | if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) { |
| 1575 | switch (data->o_res.f_attr->mode & S_IFMT) { |
| 1576 | case S_IFREG: |
| 1577 | break; |
| 1578 | case S_IFLNK: |
| 1579 | data->rpc_status = -ELOOP; |
| 1580 | break; |
| 1581 | case S_IFDIR: |
| 1582 | data->rpc_status = -EISDIR; |
| 1583 | break; |
| 1584 | default: |
| 1585 | data->rpc_status = -ENOTDIR; |
| 1586 | } |
| 1587 | } |
| 1588 | renew_lease(data->o_res.server, data->timestamp); |
| 1589 | if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)) |
| 1590 | nfs_confirm_seqid(&data->owner->so_seqid, 0); |
| 1591 | } |
| 1592 | data->rpc_done = 1; |
| 1593 | } |
| 1594 | |
| 1595 | static void nfs4_open_release(void *calldata) |
| 1596 | { |
| 1597 | struct nfs4_opendata *data = calldata; |
| 1598 | struct nfs4_state *state = NULL; |
| 1599 | |
| 1600 | /* If this request hasn't been cancelled, do nothing */ |
| 1601 | if (data->cancelled == 0) |
| 1602 | goto out_free; |
| 1603 | /* In case of error, no cleanup! */ |
| 1604 | if (data->rpc_status != 0 || !data->rpc_done) |
| 1605 | goto out_free; |
| 1606 | /* In case we need an open_confirm, no cleanup! */ |
| 1607 | if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) |
| 1608 | goto out_free; |
| 1609 | state = nfs4_opendata_to_nfs4_state(data); |
| 1610 | if (!IS_ERR(state)) |
| 1611 | nfs4_close_state(state, data->o_arg.fmode); |
| 1612 | out_free: |
| 1613 | nfs4_opendata_put(data); |
| 1614 | } |
| 1615 | |
| 1616 | static const struct rpc_call_ops nfs4_open_ops = { |
| 1617 | .rpc_call_prepare = nfs4_open_prepare, |
| 1618 | .rpc_call_done = nfs4_open_done, |
| 1619 | .rpc_release = nfs4_open_release, |
| 1620 | }; |
| 1621 | |
| 1622 | static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover) |
| 1623 | { |
| 1624 | struct inode *dir = data->dir->d_inode; |
| 1625 | struct nfs_server *server = NFS_SERVER(dir); |
| 1626 | struct nfs_openargs *o_arg = &data->o_arg; |
| 1627 | struct nfs_openres *o_res = &data->o_res; |
| 1628 | struct rpc_task *task; |
| 1629 | struct rpc_message msg = { |
| 1630 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN], |
| 1631 | .rpc_argp = o_arg, |
| 1632 | .rpc_resp = o_res, |
| 1633 | .rpc_cred = data->owner->so_cred, |
| 1634 | }; |
| 1635 | struct rpc_task_setup task_setup_data = { |
| 1636 | .rpc_client = server->client, |
| 1637 | .rpc_message = &msg, |
| 1638 | .callback_ops = &nfs4_open_ops, |
| 1639 | .callback_data = data, |
| 1640 | .workqueue = nfsiod_workqueue, |
| 1641 | .flags = RPC_TASK_ASYNC, |
| 1642 | }; |
| 1643 | int status; |
| 1644 | |
| 1645 | nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1); |
| 1646 | kref_get(&data->kref); |
| 1647 | data->rpc_done = 0; |
| 1648 | data->rpc_status = 0; |
| 1649 | data->cancelled = 0; |
| 1650 | if (isrecover) |
| 1651 | nfs4_set_sequence_privileged(&o_arg->seq_args); |
| 1652 | task = rpc_run_task(&task_setup_data); |
| 1653 | if (IS_ERR(task)) |
| 1654 | return PTR_ERR(task); |
| 1655 | status = nfs4_wait_for_completion_rpc_task(task); |
| 1656 | if (status != 0) { |
| 1657 | data->cancelled = 1; |
| 1658 | smp_wmb(); |
| 1659 | } else |
| 1660 | status = data->rpc_status; |
| 1661 | rpc_put_task(task); |
| 1662 | |
| 1663 | return status; |
| 1664 | } |
| 1665 | |
| 1666 | static int _nfs4_recover_proc_open(struct nfs4_opendata *data) |
| 1667 | { |
| 1668 | struct inode *dir = data->dir->d_inode; |
| 1669 | struct nfs_openres *o_res = &data->o_res; |
| 1670 | int status; |
| 1671 | |
| 1672 | status = nfs4_run_open_task(data, 1); |
| 1673 | if (status != 0 || !data->rpc_done) |
| 1674 | return status; |
| 1675 | |
| 1676 | nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr); |
| 1677 | |
| 1678 | if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { |
| 1679 | status = _nfs4_proc_open_confirm(data); |
| 1680 | if (status != 0) |
| 1681 | return status; |
| 1682 | } |
| 1683 | |
| 1684 | return status; |
| 1685 | } |
| 1686 | |
| 1687 | static int nfs4_opendata_access(struct rpc_cred *cred, |
| 1688 | struct nfs4_opendata *opendata, |
| 1689 | struct nfs4_state *state, fmode_t fmode, |
| 1690 | int openflags) |
| 1691 | { |
| 1692 | struct nfs_access_entry cache; |
| 1693 | u32 mask; |
| 1694 | |
| 1695 | /* access call failed or for some reason the server doesn't |
| 1696 | * support any access modes -- defer access call until later */ |
| 1697 | if (opendata->o_res.access_supported == 0) |
| 1698 | return 0; |
| 1699 | |
| 1700 | mask = 0; |
| 1701 | /* don't check MAY_WRITE - a newly created file may not have |
| 1702 | * write mode bits, but POSIX allows the creating process to write. |
| 1703 | * use openflags to check for exec, because fmode won't |
| 1704 | * always have FMODE_EXEC set when file open for exec. */ |
| 1705 | if (openflags & __FMODE_EXEC) { |
| 1706 | /* ONLY check for exec rights */ |
| 1707 | mask = MAY_EXEC; |
| 1708 | } else if (fmode & FMODE_READ) |
| 1709 | mask = MAY_READ; |
| 1710 | |
| 1711 | cache.cred = cred; |
| 1712 | cache.jiffies = jiffies; |
| 1713 | nfs_access_set_mask(&cache, opendata->o_res.access_result); |
| 1714 | nfs_access_add_cache(state->inode, &cache); |
| 1715 | |
| 1716 | if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0) |
| 1717 | return 0; |
| 1718 | |
| 1719 | /* even though OPEN succeeded, access is denied. Close the file */ |
| 1720 | nfs4_close_state(state, fmode); |
| 1721 | return -EACCES; |
| 1722 | } |
| 1723 | |
| 1724 | /* |
| 1725 | * Note: On error, nfs4_proc_open will free the struct nfs4_opendata |
| 1726 | */ |
| 1727 | static int _nfs4_proc_open(struct nfs4_opendata *data) |
| 1728 | { |
| 1729 | struct inode *dir = data->dir->d_inode; |
| 1730 | struct nfs_server *server = NFS_SERVER(dir); |
| 1731 | struct nfs_openargs *o_arg = &data->o_arg; |
| 1732 | struct nfs_openres *o_res = &data->o_res; |
| 1733 | int status; |
| 1734 | |
| 1735 | status = nfs4_run_open_task(data, 0); |
| 1736 | if (!data->rpc_done) |
| 1737 | return status; |
| 1738 | if (status != 0) { |
| 1739 | if (status == -NFS4ERR_BADNAME && |
| 1740 | !(o_arg->open_flags & O_CREAT)) |
| 1741 | return -ENOENT; |
| 1742 | return status; |
| 1743 | } |
| 1744 | |
| 1745 | nfs_fattr_map_and_free_names(server, &data->f_attr); |
| 1746 | |
| 1747 | if (o_arg->open_flags & O_CREAT) |
| 1748 | update_changeattr(dir, &o_res->cinfo); |
| 1749 | if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0) |
| 1750 | server->caps &= ~NFS_CAP_POSIX_LOCK; |
| 1751 | if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { |
| 1752 | status = _nfs4_proc_open_confirm(data); |
| 1753 | if (status != 0) |
| 1754 | return status; |
| 1755 | } |
| 1756 | if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) |
| 1757 | _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr); |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
| 1761 | static int nfs4_recover_expired_lease(struct nfs_server *server) |
| 1762 | { |
| 1763 | return nfs4_client_recover_expired_lease(server->nfs_client); |
| 1764 | } |
| 1765 | |
| 1766 | /* |
| 1767 | * OPEN_EXPIRED: |
| 1768 | * reclaim state on the server after a network partition. |
| 1769 | * Assumes caller holds the appropriate lock |
| 1770 | */ |
| 1771 | static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) |
| 1772 | { |
| 1773 | struct nfs4_opendata *opendata; |
| 1774 | int ret; |
| 1775 | |
| 1776 | opendata = nfs4_open_recoverdata_alloc(ctx, state, |
| 1777 | NFS4_OPEN_CLAIM_FH); |
| 1778 | if (IS_ERR(opendata)) |
| 1779 | return PTR_ERR(opendata); |
| 1780 | ret = nfs4_open_recover(opendata, state); |
| 1781 | if (ret == -ESTALE) |
| 1782 | d_drop(ctx->dentry); |
| 1783 | nfs4_opendata_put(opendata); |
| 1784 | return ret; |
| 1785 | } |
| 1786 | |
| 1787 | static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) |
| 1788 | { |
| 1789 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 1790 | struct nfs4_exception exception = { }; |
| 1791 | int err; |
| 1792 | |
| 1793 | do { |
| 1794 | err = _nfs4_open_expired(ctx, state); |
| 1795 | if (nfs4_clear_cap_atomic_open_v1(server, err, &exception)) |
| 1796 | continue; |
| 1797 | switch (err) { |
| 1798 | default: |
| 1799 | goto out; |
| 1800 | case -NFS4ERR_GRACE: |
| 1801 | case -NFS4ERR_DELAY: |
| 1802 | nfs4_handle_exception(server, err, &exception); |
| 1803 | err = 0; |
| 1804 | } |
| 1805 | } while (exception.retry); |
| 1806 | out: |
| 1807 | return err; |
| 1808 | } |
| 1809 | |
| 1810 | static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) |
| 1811 | { |
| 1812 | struct nfs_open_context *ctx; |
| 1813 | int ret; |
| 1814 | |
| 1815 | ctx = nfs4_state_find_open_context(state); |
| 1816 | if (IS_ERR(ctx)) |
| 1817 | return -EAGAIN; |
| 1818 | ret = nfs4_do_open_expired(ctx, state); |
| 1819 | put_nfs_open_context(ctx); |
| 1820 | return ret; |
| 1821 | } |
| 1822 | |
| 1823 | #if defined(CONFIG_NFS_V4_1) |
| 1824 | static void nfs41_clear_delegation_stateid(struct nfs4_state *state) |
| 1825 | { |
| 1826 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 1827 | nfs4_stateid *stateid = &state->stateid; |
| 1828 | int status; |
| 1829 | |
| 1830 | /* If a state reset has been done, test_stateid is unneeded */ |
| 1831 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) |
| 1832 | return; |
| 1833 | |
| 1834 | status = nfs41_test_stateid(server, stateid); |
| 1835 | if (status != NFS_OK) { |
| 1836 | /* Free the stateid unless the server explicitly |
| 1837 | * informs us the stateid is unrecognized. */ |
| 1838 | if (status != -NFS4ERR_BAD_STATEID) |
| 1839 | nfs41_free_stateid(server, stateid); |
| 1840 | nfs_remove_bad_delegation(state->inode); |
| 1841 | |
| 1842 | write_seqlock(&state->seqlock); |
| 1843 | nfs4_stateid_copy(&state->stateid, &state->open_stateid); |
| 1844 | write_sequnlock(&state->seqlock); |
| 1845 | clear_bit(NFS_DELEGATED_STATE, &state->flags); |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | /** |
| 1850 | * nfs41_check_open_stateid - possibly free an open stateid |
| 1851 | * |
| 1852 | * @state: NFSv4 state for an inode |
| 1853 | * |
| 1854 | * Returns NFS_OK if recovery for this stateid is now finished. |
| 1855 | * Otherwise a negative NFS4ERR value is returned. |
| 1856 | */ |
| 1857 | static int nfs41_check_open_stateid(struct nfs4_state *state) |
| 1858 | { |
| 1859 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 1860 | nfs4_stateid *stateid = &state->open_stateid; |
| 1861 | int status; |
| 1862 | |
| 1863 | /* If a state reset has been done, test_stateid is unneeded */ |
| 1864 | if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) && |
| 1865 | (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) && |
| 1866 | (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0)) |
| 1867 | return -NFS4ERR_BAD_STATEID; |
| 1868 | |
| 1869 | status = nfs41_test_stateid(server, stateid); |
| 1870 | if (status != NFS_OK) { |
| 1871 | /* Free the stateid unless the server explicitly |
| 1872 | * informs us the stateid is unrecognized. */ |
| 1873 | if (status != -NFS4ERR_BAD_STATEID) |
| 1874 | nfs41_free_stateid(server, stateid); |
| 1875 | |
| 1876 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); |
| 1877 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); |
| 1878 | clear_bit(NFS_O_RDWR_STATE, &state->flags); |
| 1879 | } |
| 1880 | return status; |
| 1881 | } |
| 1882 | |
| 1883 | static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) |
| 1884 | { |
| 1885 | int status; |
| 1886 | |
| 1887 | nfs41_clear_delegation_stateid(state); |
| 1888 | status = nfs41_check_open_stateid(state); |
| 1889 | if (status != NFS_OK) |
| 1890 | status = nfs4_open_expired(sp, state); |
| 1891 | return status; |
| 1892 | } |
| 1893 | #endif |
| 1894 | |
| 1895 | /* |
| 1896 | * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-* |
| 1897 | * fields corresponding to attributes that were used to store the verifier. |
| 1898 | * Make sure we clobber those fields in the later setattr call |
| 1899 | */ |
| 1900 | static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr) |
| 1901 | { |
| 1902 | if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) && |
| 1903 | !(sattr->ia_valid & ATTR_ATIME_SET)) |
| 1904 | sattr->ia_valid |= ATTR_ATIME; |
| 1905 | |
| 1906 | if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) && |
| 1907 | !(sattr->ia_valid & ATTR_MTIME_SET)) |
| 1908 | sattr->ia_valid |= ATTR_MTIME; |
| 1909 | } |
| 1910 | |
| 1911 | static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata, |
| 1912 | fmode_t fmode, |
| 1913 | int flags, |
| 1914 | struct nfs4_state **res) |
| 1915 | { |
| 1916 | struct nfs4_state_owner *sp = opendata->owner; |
| 1917 | struct nfs_server *server = sp->so_server; |
| 1918 | struct nfs4_state *state; |
| 1919 | unsigned int seq; |
| 1920 | int ret; |
| 1921 | |
| 1922 | seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); |
| 1923 | |
| 1924 | ret = _nfs4_proc_open(opendata); |
| 1925 | if (ret != 0) |
| 1926 | goto out; |
| 1927 | |
| 1928 | state = nfs4_opendata_to_nfs4_state(opendata); |
| 1929 | ret = PTR_ERR(state); |
| 1930 | if (IS_ERR(state)) |
| 1931 | goto out; |
| 1932 | if (server->caps & NFS_CAP_POSIX_LOCK) |
| 1933 | set_bit(NFS_STATE_POSIX_LOCKS, &state->flags); |
| 1934 | |
| 1935 | ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags); |
| 1936 | if (ret != 0) |
| 1937 | goto out; |
| 1938 | |
| 1939 | if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) |
| 1940 | nfs4_schedule_stateid_recovery(server, state); |
| 1941 | *res = state; |
| 1942 | out: |
| 1943 | return ret; |
| 1944 | } |
| 1945 | |
| 1946 | /* |
| 1947 | * Returns a referenced nfs4_state |
| 1948 | */ |
| 1949 | static int _nfs4_do_open(struct inode *dir, |
| 1950 | struct dentry *dentry, |
| 1951 | fmode_t fmode, |
| 1952 | int flags, |
| 1953 | struct iattr *sattr, |
| 1954 | struct rpc_cred *cred, |
| 1955 | struct nfs4_state **res, |
| 1956 | struct nfs4_threshold **ctx_th) |
| 1957 | { |
| 1958 | struct nfs4_state_owner *sp; |
| 1959 | struct nfs4_state *state = NULL; |
| 1960 | struct nfs_server *server = NFS_SERVER(dir); |
| 1961 | struct nfs4_opendata *opendata; |
| 1962 | enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL; |
| 1963 | int status; |
| 1964 | |
| 1965 | /* Protect against reboot recovery conflicts */ |
| 1966 | status = -ENOMEM; |
| 1967 | sp = nfs4_get_state_owner(server, cred, GFP_KERNEL); |
| 1968 | if (sp == NULL) { |
| 1969 | dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n"); |
| 1970 | goto out_err; |
| 1971 | } |
| 1972 | status = nfs4_recover_expired_lease(server); |
| 1973 | if (status != 0) |
| 1974 | goto err_put_state_owner; |
| 1975 | if (dentry->d_inode != NULL) |
| 1976 | nfs4_return_incompatible_delegation(dentry->d_inode, fmode); |
| 1977 | status = -ENOMEM; |
| 1978 | if (dentry->d_inode) |
| 1979 | claim = NFS4_OPEN_CLAIM_FH; |
| 1980 | opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, |
| 1981 | claim, GFP_KERNEL); |
| 1982 | if (opendata == NULL) |
| 1983 | goto err_put_state_owner; |
| 1984 | |
| 1985 | if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) { |
| 1986 | opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc(); |
| 1987 | if (!opendata->f_attr.mdsthreshold) |
| 1988 | goto err_opendata_put; |
| 1989 | opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0]; |
| 1990 | } |
| 1991 | if (dentry->d_inode != NULL) |
| 1992 | opendata->state = nfs4_get_open_state(dentry->d_inode, sp); |
| 1993 | |
| 1994 | status = _nfs4_open_and_get_state(opendata, fmode, flags, &state); |
| 1995 | if (status != 0) |
| 1996 | goto err_opendata_put; |
| 1997 | |
| 1998 | if (opendata->o_arg.open_flags & O_EXCL) { |
| 1999 | nfs4_exclusive_attrset(opendata, sattr); |
| 2000 | |
| 2001 | nfs_fattr_init(opendata->o_res.f_attr); |
| 2002 | status = nfs4_do_setattr(state->inode, cred, |
| 2003 | opendata->o_res.f_attr, sattr, |
| 2004 | state); |
| 2005 | if (status == 0) |
| 2006 | nfs_setattr_update_inode(state->inode, sattr); |
| 2007 | nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr); |
| 2008 | } |
| 2009 | |
| 2010 | if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) |
| 2011 | *ctx_th = opendata->f_attr.mdsthreshold; |
| 2012 | else |
| 2013 | kfree(opendata->f_attr.mdsthreshold); |
| 2014 | opendata->f_attr.mdsthreshold = NULL; |
| 2015 | |
| 2016 | nfs4_opendata_put(opendata); |
| 2017 | nfs4_put_state_owner(sp); |
| 2018 | *res = state; |
| 2019 | return 0; |
| 2020 | err_opendata_put: |
| 2021 | kfree(opendata->f_attr.mdsthreshold); |
| 2022 | nfs4_opendata_put(opendata); |
| 2023 | err_put_state_owner: |
| 2024 | nfs4_put_state_owner(sp); |
| 2025 | out_err: |
| 2026 | *res = NULL; |
| 2027 | return status; |
| 2028 | } |
| 2029 | |
| 2030 | |
| 2031 | static struct nfs4_state *nfs4_do_open(struct inode *dir, |
| 2032 | struct dentry *dentry, |
| 2033 | fmode_t fmode, |
| 2034 | int flags, |
| 2035 | struct iattr *sattr, |
| 2036 | struct rpc_cred *cred, |
| 2037 | struct nfs4_threshold **ctx_th) |
| 2038 | { |
| 2039 | struct nfs_server *server = NFS_SERVER(dir); |
| 2040 | struct nfs4_exception exception = { }; |
| 2041 | struct nfs4_state *res; |
| 2042 | int status; |
| 2043 | |
| 2044 | fmode &= FMODE_READ|FMODE_WRITE|FMODE_EXEC; |
| 2045 | do { |
| 2046 | status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, |
| 2047 | &res, ctx_th); |
| 2048 | if (status == 0) |
| 2049 | break; |
| 2050 | /* NOTE: BAD_SEQID means the server and client disagree about the |
| 2051 | * book-keeping w.r.t. state-changing operations |
| 2052 | * (OPEN/CLOSE/LOCK/LOCKU...) |
| 2053 | * It is actually a sign of a bug on the client or on the server. |
| 2054 | * |
| 2055 | * If we receive a BAD_SEQID error in the particular case of |
| 2056 | * doing an OPEN, we assume that nfs_increment_open_seqid() will |
| 2057 | * have unhashed the old state_owner for us, and that we can |
| 2058 | * therefore safely retry using a new one. We should still warn |
| 2059 | * the user though... |
| 2060 | */ |
| 2061 | if (status == -NFS4ERR_BAD_SEQID) { |
| 2062 | pr_warn_ratelimited("NFS: v4 server %s " |
| 2063 | " returned a bad sequence-id error!\n", |
| 2064 | NFS_SERVER(dir)->nfs_client->cl_hostname); |
| 2065 | exception.retry = 1; |
| 2066 | continue; |
| 2067 | } |
| 2068 | /* |
| 2069 | * BAD_STATEID on OPEN means that the server cancelled our |
| 2070 | * state before it received the OPEN_CONFIRM. |
| 2071 | * Recover by retrying the request as per the discussion |
| 2072 | * on Page 181 of RFC3530. |
| 2073 | */ |
| 2074 | if (status == -NFS4ERR_BAD_STATEID) { |
| 2075 | exception.retry = 1; |
| 2076 | continue; |
| 2077 | } |
| 2078 | if (status == -EAGAIN) { |
| 2079 | /* We must have found a delegation */ |
| 2080 | exception.retry = 1; |
| 2081 | continue; |
| 2082 | } |
| 2083 | if (nfs4_clear_cap_atomic_open_v1(server, status, &exception)) |
| 2084 | continue; |
| 2085 | res = ERR_PTR(nfs4_handle_exception(server, |
| 2086 | status, &exception)); |
| 2087 | } while (exception.retry); |
| 2088 | return res; |
| 2089 | } |
| 2090 | |
| 2091 | static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred, |
| 2092 | struct nfs_fattr *fattr, struct iattr *sattr, |
| 2093 | struct nfs4_state *state) |
| 2094 | { |
| 2095 | struct nfs_server *server = NFS_SERVER(inode); |
| 2096 | struct nfs_setattrargs arg = { |
| 2097 | .fh = NFS_FH(inode), |
| 2098 | .iap = sattr, |
| 2099 | .server = server, |
| 2100 | .bitmask = server->attr_bitmask, |
| 2101 | }; |
| 2102 | struct nfs_setattrres res = { |
| 2103 | .fattr = fattr, |
| 2104 | .server = server, |
| 2105 | }; |
| 2106 | struct rpc_message msg = { |
| 2107 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], |
| 2108 | .rpc_argp = &arg, |
| 2109 | .rpc_resp = &res, |
| 2110 | .rpc_cred = cred, |
| 2111 | }; |
| 2112 | unsigned long timestamp = jiffies; |
| 2113 | int status; |
| 2114 | |
| 2115 | nfs_fattr_init(fattr); |
| 2116 | |
| 2117 | if (state != NULL && nfs4_valid_open_stateid(state)) { |
| 2118 | struct nfs_lockowner lockowner = { |
| 2119 | .l_owner = current->files, |
| 2120 | .l_pid = current->tgid, |
| 2121 | }; |
| 2122 | nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE, |
| 2123 | &lockowner); |
| 2124 | } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode, |
| 2125 | FMODE_WRITE)) { |
| 2126 | /* Use that stateid */ |
| 2127 | } else |
| 2128 | nfs4_stateid_copy(&arg.stateid, &zero_stateid); |
| 2129 | |
| 2130 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
| 2131 | if (status == 0 && state != NULL) |
| 2132 | renew_lease(server, timestamp); |
| 2133 | return status; |
| 2134 | } |
| 2135 | |
| 2136 | static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred, |
| 2137 | struct nfs_fattr *fattr, struct iattr *sattr, |
| 2138 | struct nfs4_state *state) |
| 2139 | { |
| 2140 | struct nfs_server *server = NFS_SERVER(inode); |
| 2141 | struct nfs4_exception exception = { |
| 2142 | .state = state, |
| 2143 | .inode = inode, |
| 2144 | }; |
| 2145 | int err; |
| 2146 | do { |
| 2147 | err = _nfs4_do_setattr(inode, cred, fattr, sattr, state); |
| 2148 | switch (err) { |
| 2149 | case -NFS4ERR_OPENMODE: |
| 2150 | if (state && !(state->state & FMODE_WRITE)) { |
| 2151 | err = -EBADF; |
| 2152 | if (sattr->ia_valid & ATTR_OPEN) |
| 2153 | err = -EACCES; |
| 2154 | goto out; |
| 2155 | } |
| 2156 | } |
| 2157 | err = nfs4_handle_exception(server, err, &exception); |
| 2158 | } while (exception.retry); |
| 2159 | out: |
| 2160 | return err; |
| 2161 | } |
| 2162 | |
| 2163 | struct nfs4_closedata { |
| 2164 | struct inode *inode; |
| 2165 | struct nfs4_state *state; |
| 2166 | struct nfs_closeargs arg; |
| 2167 | struct nfs_closeres res; |
| 2168 | struct nfs_fattr fattr; |
| 2169 | unsigned long timestamp; |
| 2170 | bool roc; |
| 2171 | u32 roc_barrier; |
| 2172 | }; |
| 2173 | |
| 2174 | static void nfs4_free_closedata(void *data) |
| 2175 | { |
| 2176 | struct nfs4_closedata *calldata = data; |
| 2177 | struct nfs4_state_owner *sp = calldata->state->owner; |
| 2178 | struct super_block *sb = calldata->state->inode->i_sb; |
| 2179 | |
| 2180 | if (calldata->roc) |
| 2181 | pnfs_roc_release(calldata->state->inode); |
| 2182 | nfs4_put_open_state(calldata->state); |
| 2183 | nfs_free_seqid(calldata->arg.seqid); |
| 2184 | nfs4_put_state_owner(sp); |
| 2185 | nfs_sb_deactive(sb); |
| 2186 | kfree(calldata); |
| 2187 | } |
| 2188 | |
| 2189 | static void nfs4_close_clear_stateid_flags(struct nfs4_state *state, |
| 2190 | fmode_t fmode) |
| 2191 | { |
| 2192 | spin_lock(&state->owner->so_lock); |
| 2193 | if (!(fmode & FMODE_READ)) |
| 2194 | clear_bit(NFS_O_RDONLY_STATE, &state->flags); |
| 2195 | if (!(fmode & FMODE_WRITE)) |
| 2196 | clear_bit(NFS_O_WRONLY_STATE, &state->flags); |
| 2197 | clear_bit(NFS_O_RDWR_STATE, &state->flags); |
| 2198 | spin_unlock(&state->owner->so_lock); |
| 2199 | } |
| 2200 | |
| 2201 | static void nfs4_close_done(struct rpc_task *task, void *data) |
| 2202 | { |
| 2203 | struct nfs4_closedata *calldata = data; |
| 2204 | struct nfs4_state *state = calldata->state; |
| 2205 | struct nfs_server *server = NFS_SERVER(calldata->inode); |
| 2206 | |
| 2207 | dprintk("%s: begin!\n", __func__); |
| 2208 | if (!nfs4_sequence_done(task, &calldata->res.seq_res)) |
| 2209 | return; |
| 2210 | /* hmm. we are done with the inode, and in the process of freeing |
| 2211 | * the state_owner. we keep this around to process errors |
| 2212 | */ |
| 2213 | switch (task->tk_status) { |
| 2214 | case 0: |
| 2215 | if (calldata->roc) |
| 2216 | pnfs_roc_set_barrier(state->inode, |
| 2217 | calldata->roc_barrier); |
| 2218 | nfs_set_open_stateid(state, &calldata->res.stateid, 0); |
| 2219 | renew_lease(server, calldata->timestamp); |
| 2220 | nfs4_close_clear_stateid_flags(state, |
| 2221 | calldata->arg.fmode); |
| 2222 | break; |
| 2223 | case -NFS4ERR_STALE_STATEID: |
| 2224 | case -NFS4ERR_OLD_STATEID: |
| 2225 | case -NFS4ERR_BAD_STATEID: |
| 2226 | case -NFS4ERR_EXPIRED: |
| 2227 | if (calldata->arg.fmode == 0) |
| 2228 | break; |
| 2229 | default: |
| 2230 | if (nfs4_async_handle_error(task, server, state) == -EAGAIN) |
| 2231 | rpc_restart_call_prepare(task); |
| 2232 | } |
| 2233 | nfs_release_seqid(calldata->arg.seqid); |
| 2234 | nfs_refresh_inode(calldata->inode, calldata->res.fattr); |
| 2235 | dprintk("%s: done, ret = %d!\n", __func__, task->tk_status); |
| 2236 | } |
| 2237 | |
| 2238 | static void nfs4_close_prepare(struct rpc_task *task, void *data) |
| 2239 | { |
| 2240 | struct nfs4_closedata *calldata = data; |
| 2241 | struct nfs4_state *state = calldata->state; |
| 2242 | struct inode *inode = calldata->inode; |
| 2243 | int call_close = 0; |
| 2244 | |
| 2245 | dprintk("%s: begin!\n", __func__); |
| 2246 | if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) |
| 2247 | goto out_wait; |
| 2248 | |
| 2249 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE]; |
| 2250 | calldata->arg.fmode = FMODE_READ|FMODE_WRITE; |
| 2251 | spin_lock(&state->owner->so_lock); |
| 2252 | /* Calculate the change in open mode */ |
| 2253 | if (state->n_rdwr == 0) { |
| 2254 | if (state->n_rdonly == 0) { |
| 2255 | call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags); |
| 2256 | call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags); |
| 2257 | calldata->arg.fmode &= ~FMODE_READ; |
| 2258 | } |
| 2259 | if (state->n_wronly == 0) { |
| 2260 | call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags); |
| 2261 | call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags); |
| 2262 | calldata->arg.fmode &= ~FMODE_WRITE; |
| 2263 | } |
| 2264 | } |
| 2265 | if (!nfs4_valid_open_stateid(state)) |
| 2266 | call_close = 0; |
| 2267 | spin_unlock(&state->owner->so_lock); |
| 2268 | |
| 2269 | if (!call_close) { |
| 2270 | /* Note: exit _without_ calling nfs4_close_done */ |
| 2271 | goto out_no_action; |
| 2272 | } |
| 2273 | |
| 2274 | if (calldata->arg.fmode == 0) { |
| 2275 | task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE]; |
| 2276 | if (calldata->roc && |
| 2277 | pnfs_roc_drain(inode, &calldata->roc_barrier, task)) |
| 2278 | goto out_wait; |
| 2279 | } |
| 2280 | |
| 2281 | nfs_fattr_init(calldata->res.fattr); |
| 2282 | calldata->timestamp = jiffies; |
| 2283 | if (nfs4_setup_sequence(NFS_SERVER(inode), |
| 2284 | &calldata->arg.seq_args, |
| 2285 | &calldata->res.seq_res, |
| 2286 | task) != 0) |
| 2287 | nfs_release_seqid(calldata->arg.seqid); |
| 2288 | dprintk("%s: done!\n", __func__); |
| 2289 | return; |
| 2290 | out_no_action: |
| 2291 | task->tk_action = NULL; |
| 2292 | out_wait: |
| 2293 | nfs4_sequence_done(task, &calldata->res.seq_res); |
| 2294 | } |
| 2295 | |
| 2296 | static const struct rpc_call_ops nfs4_close_ops = { |
| 2297 | .rpc_call_prepare = nfs4_close_prepare, |
| 2298 | .rpc_call_done = nfs4_close_done, |
| 2299 | .rpc_release = nfs4_free_closedata, |
| 2300 | }; |
| 2301 | |
| 2302 | /* |
| 2303 | * It is possible for data to be read/written from a mem-mapped file |
| 2304 | * after the sys_close call (which hits the vfs layer as a flush). |
| 2305 | * This means that we can't safely call nfsv4 close on a file until |
| 2306 | * the inode is cleared. This in turn means that we are not good |
| 2307 | * NFSv4 citizens - we do not indicate to the server to update the file's |
| 2308 | * share state even when we are done with one of the three share |
| 2309 | * stateid's in the inode. |
| 2310 | * |
| 2311 | * NOTE: Caller must be holding the sp->so_owner semaphore! |
| 2312 | */ |
| 2313 | int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait) |
| 2314 | { |
| 2315 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 2316 | struct nfs4_closedata *calldata; |
| 2317 | struct nfs4_state_owner *sp = state->owner; |
| 2318 | struct rpc_task *task; |
| 2319 | struct rpc_message msg = { |
| 2320 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE], |
| 2321 | .rpc_cred = state->owner->so_cred, |
| 2322 | }; |
| 2323 | struct rpc_task_setup task_setup_data = { |
| 2324 | .rpc_client = server->client, |
| 2325 | .rpc_message = &msg, |
| 2326 | .callback_ops = &nfs4_close_ops, |
| 2327 | .workqueue = nfsiod_workqueue, |
| 2328 | .flags = RPC_TASK_ASYNC, |
| 2329 | }; |
| 2330 | int status = -ENOMEM; |
| 2331 | |
| 2332 | calldata = kzalloc(sizeof(*calldata), gfp_mask); |
| 2333 | if (calldata == NULL) |
| 2334 | goto out; |
| 2335 | nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1); |
| 2336 | calldata->inode = state->inode; |
| 2337 | calldata->state = state; |
| 2338 | calldata->arg.fh = NFS_FH(state->inode); |
| 2339 | calldata->arg.stateid = &state->open_stateid; |
| 2340 | /* Serialization for the sequence id */ |
| 2341 | calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask); |
| 2342 | if (calldata->arg.seqid == NULL) |
| 2343 | goto out_free_calldata; |
| 2344 | calldata->arg.fmode = 0; |
| 2345 | calldata->arg.bitmask = server->cache_consistency_bitmask; |
| 2346 | calldata->res.fattr = &calldata->fattr; |
| 2347 | calldata->res.seqid = calldata->arg.seqid; |
| 2348 | calldata->res.server = server; |
| 2349 | calldata->roc = pnfs_roc(state->inode); |
| 2350 | nfs_sb_active(calldata->inode->i_sb); |
| 2351 | |
| 2352 | msg.rpc_argp = &calldata->arg; |
| 2353 | msg.rpc_resp = &calldata->res; |
| 2354 | task_setup_data.callback_data = calldata; |
| 2355 | task = rpc_run_task(&task_setup_data); |
| 2356 | if (IS_ERR(task)) |
| 2357 | return PTR_ERR(task); |
| 2358 | status = 0; |
| 2359 | if (wait) |
| 2360 | status = rpc_wait_for_completion_task(task); |
| 2361 | rpc_put_task(task); |
| 2362 | return status; |
| 2363 | out_free_calldata: |
| 2364 | kfree(calldata); |
| 2365 | out: |
| 2366 | nfs4_put_open_state(state); |
| 2367 | nfs4_put_state_owner(sp); |
| 2368 | return status; |
| 2369 | } |
| 2370 | |
| 2371 | static struct inode * |
| 2372 | nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr) |
| 2373 | { |
| 2374 | struct nfs4_state *state; |
| 2375 | |
| 2376 | /* Protect against concurrent sillydeletes */ |
| 2377 | state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, |
| 2378 | ctx->cred, &ctx->mdsthreshold); |
| 2379 | if (IS_ERR(state)) |
| 2380 | return ERR_CAST(state); |
| 2381 | ctx->state = state; |
| 2382 | return igrab(state->inode); |
| 2383 | } |
| 2384 | |
| 2385 | static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync) |
| 2386 | { |
| 2387 | if (ctx->state == NULL) |
| 2388 | return; |
| 2389 | if (is_sync) |
| 2390 | nfs4_close_sync(ctx->state, ctx->mode); |
| 2391 | else |
| 2392 | nfs4_close_state(ctx->state, ctx->mode); |
| 2393 | } |
| 2394 | |
| 2395 | static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) |
| 2396 | { |
| 2397 | struct nfs4_server_caps_arg args = { |
| 2398 | .fhandle = fhandle, |
| 2399 | }; |
| 2400 | struct nfs4_server_caps_res res = {}; |
| 2401 | struct rpc_message msg = { |
| 2402 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS], |
| 2403 | .rpc_argp = &args, |
| 2404 | .rpc_resp = &res, |
| 2405 | }; |
| 2406 | int status; |
| 2407 | |
| 2408 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
| 2409 | if (status == 0) { |
| 2410 | memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask)); |
| 2411 | server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS| |
| 2412 | NFS_CAP_SYMLINKS|NFS_CAP_FILEID| |
| 2413 | NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER| |
| 2414 | NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME| |
| 2415 | NFS_CAP_CTIME|NFS_CAP_MTIME); |
| 2416 | if (res.attr_bitmask[0] & FATTR4_WORD0_ACL) |
| 2417 | server->caps |= NFS_CAP_ACLS; |
| 2418 | if (res.has_links != 0) |
| 2419 | server->caps |= NFS_CAP_HARDLINKS; |
| 2420 | if (res.has_symlinks != 0) |
| 2421 | server->caps |= NFS_CAP_SYMLINKS; |
| 2422 | if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID) |
| 2423 | server->caps |= NFS_CAP_FILEID; |
| 2424 | if (res.attr_bitmask[1] & FATTR4_WORD1_MODE) |
| 2425 | server->caps |= NFS_CAP_MODE; |
| 2426 | if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS) |
| 2427 | server->caps |= NFS_CAP_NLINK; |
| 2428 | if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER) |
| 2429 | server->caps |= NFS_CAP_OWNER; |
| 2430 | if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP) |
| 2431 | server->caps |= NFS_CAP_OWNER_GROUP; |
| 2432 | if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS) |
| 2433 | server->caps |= NFS_CAP_ATIME; |
| 2434 | if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA) |
| 2435 | server->caps |= NFS_CAP_CTIME; |
| 2436 | if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY) |
| 2437 | server->caps |= NFS_CAP_MTIME; |
| 2438 | |
| 2439 | memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask)); |
| 2440 | server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE; |
| 2441 | server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY; |
| 2442 | server->acl_bitmask = res.acl_bitmask; |
| 2443 | server->fh_expire_type = res.fh_expire_type; |
| 2444 | } |
| 2445 | |
| 2446 | return status; |
| 2447 | } |
| 2448 | |
| 2449 | int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) |
| 2450 | { |
| 2451 | struct nfs4_exception exception = { }; |
| 2452 | int err; |
| 2453 | do { |
| 2454 | err = nfs4_handle_exception(server, |
| 2455 | _nfs4_server_capabilities(server, fhandle), |
| 2456 | &exception); |
| 2457 | } while (exception.retry); |
| 2458 | return err; |
| 2459 | } |
| 2460 | |
| 2461 | static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, |
| 2462 | struct nfs_fsinfo *info) |
| 2463 | { |
| 2464 | struct nfs4_lookup_root_arg args = { |
| 2465 | .bitmask = nfs4_fattr_bitmap, |
| 2466 | }; |
| 2467 | struct nfs4_lookup_res res = { |
| 2468 | .server = server, |
| 2469 | .fattr = info->fattr, |
| 2470 | .fh = fhandle, |
| 2471 | }; |
| 2472 | struct rpc_message msg = { |
| 2473 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT], |
| 2474 | .rpc_argp = &args, |
| 2475 | .rpc_resp = &res, |
| 2476 | }; |
| 2477 | |
| 2478 | nfs_fattr_init(info->fattr); |
| 2479 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
| 2480 | } |
| 2481 | |
| 2482 | static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, |
| 2483 | struct nfs_fsinfo *info) |
| 2484 | { |
| 2485 | struct nfs4_exception exception = { }; |
| 2486 | int err; |
| 2487 | do { |
| 2488 | err = _nfs4_lookup_root(server, fhandle, info); |
| 2489 | switch (err) { |
| 2490 | case 0: |
| 2491 | case -NFS4ERR_WRONGSEC: |
| 2492 | goto out; |
| 2493 | default: |
| 2494 | err = nfs4_handle_exception(server, err, &exception); |
| 2495 | } |
| 2496 | } while (exception.retry); |
| 2497 | out: |
| 2498 | return err; |
| 2499 | } |
| 2500 | |
| 2501 | static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, |
| 2502 | struct nfs_fsinfo *info, rpc_authflavor_t flavor) |
| 2503 | { |
| 2504 | struct rpc_auth *auth; |
| 2505 | int ret; |
| 2506 | |
| 2507 | auth = rpcauth_create(flavor, server->client); |
| 2508 | if (IS_ERR(auth)) { |
| 2509 | ret = -EACCES; |
| 2510 | goto out; |
| 2511 | } |
| 2512 | ret = nfs4_lookup_root(server, fhandle, info); |
| 2513 | out: |
| 2514 | return ret; |
| 2515 | } |
| 2516 | |
| 2517 | static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, |
| 2518 | struct nfs_fsinfo *info) |
| 2519 | { |
| 2520 | int i, len, status = 0; |
| 2521 | rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS]; |
| 2522 | |
| 2523 | len = rpcauth_list_flavors(flav_array, ARRAY_SIZE(flav_array)); |
| 2524 | if (len < 0) |
| 2525 | return len; |
| 2526 | |
| 2527 | for (i = 0; i < len; i++) { |
| 2528 | /* AUTH_UNIX is the default flavor if none was specified, |
| 2529 | * thus has already been tried. */ |
| 2530 | if (flav_array[i] == RPC_AUTH_UNIX) |
| 2531 | continue; |
| 2532 | |
| 2533 | status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]); |
| 2534 | if (status == -NFS4ERR_WRONGSEC || status == -EACCES) |
| 2535 | continue; |
| 2536 | break; |
| 2537 | } |
| 2538 | /* |
| 2539 | * -EACCESS could mean that the user doesn't have correct permissions |
| 2540 | * to access the mount. It could also mean that we tried to mount |
| 2541 | * with a gss auth flavor, but rpc.gssd isn't running. Either way, |
| 2542 | * existing mount programs don't handle -EACCES very well so it should |
| 2543 | * be mapped to -EPERM instead. |
| 2544 | */ |
| 2545 | if (status == -EACCES) |
| 2546 | status = -EPERM; |
| 2547 | return status; |
| 2548 | } |
| 2549 | |
| 2550 | static int nfs4_do_find_root_sec(struct nfs_server *server, |
| 2551 | struct nfs_fh *fhandle, struct nfs_fsinfo *info) |
| 2552 | { |
| 2553 | int mv = server->nfs_client->cl_minorversion; |
| 2554 | return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info); |
| 2555 | } |
| 2556 | |
| 2557 | /** |
| 2558 | * nfs4_proc_get_rootfh - get file handle for server's pseudoroot |
| 2559 | * @server: initialized nfs_server handle |
| 2560 | * @fhandle: we fill in the pseudo-fs root file handle |
| 2561 | * @info: we fill in an FSINFO struct |
| 2562 | * |
| 2563 | * Returns zero on success, or a negative errno. |
| 2564 | */ |
| 2565 | int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle, |
| 2566 | struct nfs_fsinfo *info) |
| 2567 | { |
| 2568 | int status; |
| 2569 | |
| 2570 | status = nfs4_lookup_root(server, fhandle, info); |
| 2571 | if ((status == -NFS4ERR_WRONGSEC) && |
| 2572 | !(server->flags & NFS_MOUNT_SECFLAVOUR)) |
| 2573 | status = nfs4_do_find_root_sec(server, fhandle, info); |
| 2574 | |
| 2575 | if (status == 0) |
| 2576 | status = nfs4_server_capabilities(server, fhandle); |
| 2577 | if (status == 0) |
| 2578 | status = nfs4_do_fsinfo(server, fhandle, info); |
| 2579 | |
| 2580 | return nfs4_map_errors(status); |
| 2581 | } |
| 2582 | |
| 2583 | static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh, |
| 2584 | struct nfs_fsinfo *info) |
| 2585 | { |
| 2586 | int error; |
| 2587 | struct nfs_fattr *fattr = info->fattr; |
| 2588 | |
| 2589 | error = nfs4_server_capabilities(server, mntfh); |
| 2590 | if (error < 0) { |
| 2591 | dprintk("nfs4_get_root: getcaps error = %d\n", -error); |
| 2592 | return error; |
| 2593 | } |
| 2594 | |
| 2595 | error = nfs4_proc_getattr(server, mntfh, fattr); |
| 2596 | if (error < 0) { |
| 2597 | dprintk("nfs4_get_root: getattr error = %d\n", -error); |
| 2598 | return error; |
| 2599 | } |
| 2600 | |
| 2601 | if (fattr->valid & NFS_ATTR_FATTR_FSID && |
| 2602 | !nfs_fsid_equal(&server->fsid, &fattr->fsid)) |
| 2603 | memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid)); |
| 2604 | |
| 2605 | return error; |
| 2606 | } |
| 2607 | |
| 2608 | /* |
| 2609 | * Get locations and (maybe) other attributes of a referral. |
| 2610 | * Note that we'll actually follow the referral later when |
| 2611 | * we detect fsid mismatch in inode revalidation |
| 2612 | */ |
| 2613 | static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir, |
| 2614 | const struct qstr *name, struct nfs_fattr *fattr, |
| 2615 | struct nfs_fh *fhandle) |
| 2616 | { |
| 2617 | int status = -ENOMEM; |
| 2618 | struct page *page = NULL; |
| 2619 | struct nfs4_fs_locations *locations = NULL; |
| 2620 | |
| 2621 | page = alloc_page(GFP_KERNEL); |
| 2622 | if (page == NULL) |
| 2623 | goto out; |
| 2624 | locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); |
| 2625 | if (locations == NULL) |
| 2626 | goto out; |
| 2627 | |
| 2628 | status = nfs4_proc_fs_locations(client, dir, name, locations, page); |
| 2629 | if (status != 0) |
| 2630 | goto out; |
| 2631 | /* Make sure server returned a different fsid for the referral */ |
| 2632 | if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) { |
| 2633 | dprintk("%s: server did not return a different fsid for" |
| 2634 | " a referral at %s\n", __func__, name->name); |
| 2635 | status = -EIO; |
| 2636 | goto out; |
| 2637 | } |
| 2638 | /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */ |
| 2639 | nfs_fixup_referral_attributes(&locations->fattr); |
| 2640 | |
| 2641 | /* replace the lookup nfs_fattr with the locations nfs_fattr */ |
| 2642 | memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr)); |
| 2643 | memset(fhandle, 0, sizeof(struct nfs_fh)); |
| 2644 | out: |
| 2645 | if (page) |
| 2646 | __free_page(page); |
| 2647 | kfree(locations); |
| 2648 | return status; |
| 2649 | } |
| 2650 | |
| 2651 | static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
| 2652 | { |
| 2653 | struct nfs4_getattr_arg args = { |
| 2654 | .fh = fhandle, |
| 2655 | .bitmask = server->attr_bitmask, |
| 2656 | }; |
| 2657 | struct nfs4_getattr_res res = { |
| 2658 | .fattr = fattr, |
| 2659 | .server = server, |
| 2660 | }; |
| 2661 | struct rpc_message msg = { |
| 2662 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], |
| 2663 | .rpc_argp = &args, |
| 2664 | .rpc_resp = &res, |
| 2665 | }; |
| 2666 | |
| 2667 | nfs_fattr_init(fattr); |
| 2668 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
| 2669 | } |
| 2670 | |
| 2671 | static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
| 2672 | { |
| 2673 | struct nfs4_exception exception = { }; |
| 2674 | int err; |
| 2675 | do { |
| 2676 | err = nfs4_handle_exception(server, |
| 2677 | _nfs4_proc_getattr(server, fhandle, fattr), |
| 2678 | &exception); |
| 2679 | } while (exception.retry); |
| 2680 | return err; |
| 2681 | } |
| 2682 | |
| 2683 | /* |
| 2684 | * The file is not closed if it is opened due to the a request to change |
| 2685 | * the size of the file. The open call will not be needed once the |
| 2686 | * VFS layer lookup-intents are implemented. |
| 2687 | * |
| 2688 | * Close is called when the inode is destroyed. |
| 2689 | * If we haven't opened the file for O_WRONLY, we |
| 2690 | * need to in the size_change case to obtain a stateid. |
| 2691 | * |
| 2692 | * Got race? |
| 2693 | * Because OPEN is always done by name in nfsv4, it is |
| 2694 | * possible that we opened a different file by the same |
| 2695 | * name. We can recognize this race condition, but we |
| 2696 | * can't do anything about it besides returning an error. |
| 2697 | * |
| 2698 | * This will be fixed with VFS changes (lookup-intent). |
| 2699 | */ |
| 2700 | static int |
| 2701 | nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr, |
| 2702 | struct iattr *sattr) |
| 2703 | { |
| 2704 | struct inode *inode = dentry->d_inode; |
| 2705 | struct rpc_cred *cred = NULL; |
| 2706 | struct nfs4_state *state = NULL; |
| 2707 | int status; |
| 2708 | |
| 2709 | if (pnfs_ld_layoutret_on_setattr(inode)) |
| 2710 | pnfs_commit_and_return_layout(inode); |
| 2711 | |
| 2712 | nfs_fattr_init(fattr); |
| 2713 | |
| 2714 | /* Deal with open(O_TRUNC) */ |
| 2715 | if (sattr->ia_valid & ATTR_OPEN) |
| 2716 | sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN); |
| 2717 | |
| 2718 | /* Optimization: if the end result is no change, don't RPC */ |
| 2719 | if ((sattr->ia_valid & ~(ATTR_FILE)) == 0) |
| 2720 | return 0; |
| 2721 | |
| 2722 | /* Search for an existing open(O_WRITE) file */ |
| 2723 | if (sattr->ia_valid & ATTR_FILE) { |
| 2724 | struct nfs_open_context *ctx; |
| 2725 | |
| 2726 | ctx = nfs_file_open_context(sattr->ia_file); |
| 2727 | if (ctx) { |
| 2728 | cred = ctx->cred; |
| 2729 | state = ctx->state; |
| 2730 | } |
| 2731 | } |
| 2732 | |
| 2733 | status = nfs4_do_setattr(inode, cred, fattr, sattr, state); |
| 2734 | if (status == 0) |
| 2735 | nfs_setattr_update_inode(inode, sattr); |
| 2736 | return status; |
| 2737 | } |
| 2738 | |
| 2739 | static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, |
| 2740 | const struct qstr *name, struct nfs_fh *fhandle, |
| 2741 | struct nfs_fattr *fattr) |
| 2742 | { |
| 2743 | struct nfs_server *server = NFS_SERVER(dir); |
| 2744 | int status; |
| 2745 | struct nfs4_lookup_arg args = { |
| 2746 | .bitmask = server->attr_bitmask, |
| 2747 | .dir_fh = NFS_FH(dir), |
| 2748 | .name = name, |
| 2749 | }; |
| 2750 | struct nfs4_lookup_res res = { |
| 2751 | .server = server, |
| 2752 | .fattr = fattr, |
| 2753 | .fh = fhandle, |
| 2754 | }; |
| 2755 | struct rpc_message msg = { |
| 2756 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP], |
| 2757 | .rpc_argp = &args, |
| 2758 | .rpc_resp = &res, |
| 2759 | }; |
| 2760 | |
| 2761 | nfs_fattr_init(fattr); |
| 2762 | |
| 2763 | dprintk("NFS call lookup %s\n", name->name); |
| 2764 | status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0); |
| 2765 | dprintk("NFS reply lookup: %d\n", status); |
| 2766 | return status; |
| 2767 | } |
| 2768 | |
| 2769 | static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr) |
| 2770 | { |
| 2771 | fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | |
| 2772 | NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT; |
| 2773 | fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; |
| 2774 | fattr->nlink = 2; |
| 2775 | } |
| 2776 | |
| 2777 | static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir, |
| 2778 | struct qstr *name, struct nfs_fh *fhandle, |
| 2779 | struct nfs_fattr *fattr) |
| 2780 | { |
| 2781 | struct nfs4_exception exception = { }; |
| 2782 | struct rpc_clnt *client = *clnt; |
| 2783 | int err; |
| 2784 | do { |
| 2785 | err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr); |
| 2786 | switch (err) { |
| 2787 | case -NFS4ERR_BADNAME: |
| 2788 | err = -ENOENT; |
| 2789 | goto out; |
| 2790 | case -NFS4ERR_MOVED: |
| 2791 | err = nfs4_get_referral(client, dir, name, fattr, fhandle); |
| 2792 | goto out; |
| 2793 | case -NFS4ERR_WRONGSEC: |
| 2794 | err = -EPERM; |
| 2795 | if (client != *clnt) |
| 2796 | goto out; |
| 2797 | |
| 2798 | client = nfs4_create_sec_client(client, dir, name); |
| 2799 | if (IS_ERR(client)) |
| 2800 | return PTR_ERR(client); |
| 2801 | |
| 2802 | exception.retry = 1; |
| 2803 | break; |
| 2804 | default: |
| 2805 | err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception); |
| 2806 | } |
| 2807 | } while (exception.retry); |
| 2808 | |
| 2809 | out: |
| 2810 | if (err == 0) |
| 2811 | *clnt = client; |
| 2812 | else if (client != *clnt) |
| 2813 | rpc_shutdown_client(client); |
| 2814 | |
| 2815 | return err; |
| 2816 | } |
| 2817 | |
| 2818 | static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, |
| 2819 | struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
| 2820 | { |
| 2821 | int status; |
| 2822 | struct rpc_clnt *client = NFS_CLIENT(dir); |
| 2823 | |
| 2824 | status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr); |
| 2825 | if (client != NFS_CLIENT(dir)) { |
| 2826 | rpc_shutdown_client(client); |
| 2827 | nfs_fixup_secinfo_attributes(fattr); |
| 2828 | } |
| 2829 | return status; |
| 2830 | } |
| 2831 | |
| 2832 | struct rpc_clnt * |
| 2833 | nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name, |
| 2834 | struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
| 2835 | { |
| 2836 | int status; |
| 2837 | struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir)); |
| 2838 | |
| 2839 | status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr); |
| 2840 | if (status < 0) { |
| 2841 | rpc_shutdown_client(client); |
| 2842 | return ERR_PTR(status); |
| 2843 | } |
| 2844 | return client; |
| 2845 | } |
| 2846 | |
| 2847 | static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) |
| 2848 | { |
| 2849 | struct nfs_server *server = NFS_SERVER(inode); |
| 2850 | struct nfs4_accessargs args = { |
| 2851 | .fh = NFS_FH(inode), |
| 2852 | .bitmask = server->cache_consistency_bitmask, |
| 2853 | }; |
| 2854 | struct nfs4_accessres res = { |
| 2855 | .server = server, |
| 2856 | }; |
| 2857 | struct rpc_message msg = { |
| 2858 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS], |
| 2859 | .rpc_argp = &args, |
| 2860 | .rpc_resp = &res, |
| 2861 | .rpc_cred = entry->cred, |
| 2862 | }; |
| 2863 | int mode = entry->mask; |
| 2864 | int status; |
| 2865 | |
| 2866 | /* |
| 2867 | * Determine which access bits we want to ask for... |
| 2868 | */ |
| 2869 | if (mode & MAY_READ) |
| 2870 | args.access |= NFS4_ACCESS_READ; |
| 2871 | if (S_ISDIR(inode->i_mode)) { |
| 2872 | if (mode & MAY_WRITE) |
| 2873 | args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE; |
| 2874 | if (mode & MAY_EXEC) |
| 2875 | args.access |= NFS4_ACCESS_LOOKUP; |
| 2876 | } else { |
| 2877 | if (mode & MAY_WRITE) |
| 2878 | args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND; |
| 2879 | if (mode & MAY_EXEC) |
| 2880 | args.access |= NFS4_ACCESS_EXECUTE; |
| 2881 | } |
| 2882 | |
| 2883 | res.fattr = nfs_alloc_fattr(); |
| 2884 | if (res.fattr == NULL) |
| 2885 | return -ENOMEM; |
| 2886 | |
| 2887 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
| 2888 | if (!status) { |
| 2889 | nfs_access_set_mask(entry, res.access); |
| 2890 | nfs_refresh_inode(inode, res.fattr); |
| 2891 | } |
| 2892 | nfs_free_fattr(res.fattr); |
| 2893 | return status; |
| 2894 | } |
| 2895 | |
| 2896 | static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) |
| 2897 | { |
| 2898 | struct nfs4_exception exception = { }; |
| 2899 | int err; |
| 2900 | do { |
| 2901 | err = nfs4_handle_exception(NFS_SERVER(inode), |
| 2902 | _nfs4_proc_access(inode, entry), |
| 2903 | &exception); |
| 2904 | } while (exception.retry); |
| 2905 | return err; |
| 2906 | } |
| 2907 | |
| 2908 | /* |
| 2909 | * TODO: For the time being, we don't try to get any attributes |
| 2910 | * along with any of the zero-copy operations READ, READDIR, |
| 2911 | * READLINK, WRITE. |
| 2912 | * |
| 2913 | * In the case of the first three, we want to put the GETATTR |
| 2914 | * after the read-type operation -- this is because it is hard |
| 2915 | * to predict the length of a GETATTR response in v4, and thus |
| 2916 | * align the READ data correctly. This means that the GETATTR |
| 2917 | * may end up partially falling into the page cache, and we should |
| 2918 | * shift it into the 'tail' of the xdr_buf before processing. |
| 2919 | * To do this efficiently, we need to know the total length |
| 2920 | * of data received, which doesn't seem to be available outside |
| 2921 | * of the RPC layer. |
| 2922 | * |
| 2923 | * In the case of WRITE, we also want to put the GETATTR after |
| 2924 | * the operation -- in this case because we want to make sure |
| 2925 | * we get the post-operation mtime and size. |
| 2926 | * |
| 2927 | * Both of these changes to the XDR layer would in fact be quite |
| 2928 | * minor, but I decided to leave them for a subsequent patch. |
| 2929 | */ |
| 2930 | static int _nfs4_proc_readlink(struct inode *inode, struct page *page, |
| 2931 | unsigned int pgbase, unsigned int pglen) |
| 2932 | { |
| 2933 | struct nfs4_readlink args = { |
| 2934 | .fh = NFS_FH(inode), |
| 2935 | .pgbase = pgbase, |
| 2936 | .pglen = pglen, |
| 2937 | .pages = &page, |
| 2938 | }; |
| 2939 | struct nfs4_readlink_res res; |
| 2940 | struct rpc_message msg = { |
| 2941 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK], |
| 2942 | .rpc_argp = &args, |
| 2943 | .rpc_resp = &res, |
| 2944 | }; |
| 2945 | |
| 2946 | return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0); |
| 2947 | } |
| 2948 | |
| 2949 | static int nfs4_proc_readlink(struct inode *inode, struct page *page, |
| 2950 | unsigned int pgbase, unsigned int pglen) |
| 2951 | { |
| 2952 | struct nfs4_exception exception = { }; |
| 2953 | int err; |
| 2954 | do { |
| 2955 | err = nfs4_handle_exception(NFS_SERVER(inode), |
| 2956 | _nfs4_proc_readlink(inode, page, pgbase, pglen), |
| 2957 | &exception); |
| 2958 | } while (exception.retry); |
| 2959 | return err; |
| 2960 | } |
| 2961 | |
| 2962 | /* |
| 2963 | * This is just for mknod. open(O_CREAT) will always do ->open_context(). |
| 2964 | */ |
| 2965 | static int |
| 2966 | nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, |
| 2967 | int flags) |
| 2968 | { |
| 2969 | struct nfs_open_context *ctx; |
| 2970 | struct nfs4_state *state; |
| 2971 | int status = 0; |
| 2972 | |
| 2973 | ctx = alloc_nfs_open_context(dentry, FMODE_READ); |
| 2974 | if (IS_ERR(ctx)) |
| 2975 | return PTR_ERR(ctx); |
| 2976 | |
| 2977 | sattr->ia_mode &= ~current_umask(); |
| 2978 | state = nfs4_do_open(dir, dentry, ctx->mode, |
| 2979 | flags, sattr, ctx->cred, |
| 2980 | &ctx->mdsthreshold); |
| 2981 | d_drop(dentry); |
| 2982 | if (IS_ERR(state)) { |
| 2983 | status = PTR_ERR(state); |
| 2984 | goto out; |
| 2985 | } |
| 2986 | d_add(dentry, igrab(state->inode)); |
| 2987 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
| 2988 | ctx->state = state; |
| 2989 | out: |
| 2990 | put_nfs_open_context(ctx); |
| 2991 | return status; |
| 2992 | } |
| 2993 | |
| 2994 | static int _nfs4_proc_remove(struct inode *dir, struct qstr *name) |
| 2995 | { |
| 2996 | struct nfs_server *server = NFS_SERVER(dir); |
| 2997 | struct nfs_removeargs args = { |
| 2998 | .fh = NFS_FH(dir), |
| 2999 | .name = *name, |
| 3000 | }; |
| 3001 | struct nfs_removeres res = { |
| 3002 | .server = server, |
| 3003 | }; |
| 3004 | struct rpc_message msg = { |
| 3005 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE], |
| 3006 | .rpc_argp = &args, |
| 3007 | .rpc_resp = &res, |
| 3008 | }; |
| 3009 | int status; |
| 3010 | |
| 3011 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1); |
| 3012 | if (status == 0) |
| 3013 | update_changeattr(dir, &res.cinfo); |
| 3014 | return status; |
| 3015 | } |
| 3016 | |
| 3017 | static int nfs4_proc_remove(struct inode *dir, struct qstr *name) |
| 3018 | { |
| 3019 | struct nfs4_exception exception = { }; |
| 3020 | int err; |
| 3021 | do { |
| 3022 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 3023 | _nfs4_proc_remove(dir, name), |
| 3024 | &exception); |
| 3025 | } while (exception.retry); |
| 3026 | return err; |
| 3027 | } |
| 3028 | |
| 3029 | static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir) |
| 3030 | { |
| 3031 | struct nfs_server *server = NFS_SERVER(dir); |
| 3032 | struct nfs_removeargs *args = msg->rpc_argp; |
| 3033 | struct nfs_removeres *res = msg->rpc_resp; |
| 3034 | |
| 3035 | res->server = server; |
| 3036 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE]; |
| 3037 | nfs41_init_sequence(&args->seq_args, &res->seq_res, 1); |
| 3038 | } |
| 3039 | |
| 3040 | static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data) |
| 3041 | { |
| 3042 | nfs4_setup_sequence(NFS_SERVER(data->dir), |
| 3043 | &data->args.seq_args, |
| 3044 | &data->res.seq_res, |
| 3045 | task); |
| 3046 | } |
| 3047 | |
| 3048 | static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir) |
| 3049 | { |
| 3050 | struct nfs_removeres *res = task->tk_msg.rpc_resp; |
| 3051 | |
| 3052 | if (!nfs4_sequence_done(task, &res->seq_res)) |
| 3053 | return 0; |
| 3054 | if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN) |
| 3055 | return 0; |
| 3056 | update_changeattr(dir, &res->cinfo); |
| 3057 | return 1; |
| 3058 | } |
| 3059 | |
| 3060 | static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir) |
| 3061 | { |
| 3062 | struct nfs_server *server = NFS_SERVER(dir); |
| 3063 | struct nfs_renameargs *arg = msg->rpc_argp; |
| 3064 | struct nfs_renameres *res = msg->rpc_resp; |
| 3065 | |
| 3066 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME]; |
| 3067 | res->server = server; |
| 3068 | nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1); |
| 3069 | } |
| 3070 | |
| 3071 | static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data) |
| 3072 | { |
| 3073 | nfs4_setup_sequence(NFS_SERVER(data->old_dir), |
| 3074 | &data->args.seq_args, |
| 3075 | &data->res.seq_res, |
| 3076 | task); |
| 3077 | } |
| 3078 | |
| 3079 | static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir, |
| 3080 | struct inode *new_dir) |
| 3081 | { |
| 3082 | struct nfs_renameres *res = task->tk_msg.rpc_resp; |
| 3083 | |
| 3084 | if (!nfs4_sequence_done(task, &res->seq_res)) |
| 3085 | return 0; |
| 3086 | if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN) |
| 3087 | return 0; |
| 3088 | |
| 3089 | update_changeattr(old_dir, &res->old_cinfo); |
| 3090 | update_changeattr(new_dir, &res->new_cinfo); |
| 3091 | return 1; |
| 3092 | } |
| 3093 | |
| 3094 | static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name, |
| 3095 | struct inode *new_dir, struct qstr *new_name) |
| 3096 | { |
| 3097 | struct nfs_server *server = NFS_SERVER(old_dir); |
| 3098 | struct nfs_renameargs arg = { |
| 3099 | .old_dir = NFS_FH(old_dir), |
| 3100 | .new_dir = NFS_FH(new_dir), |
| 3101 | .old_name = old_name, |
| 3102 | .new_name = new_name, |
| 3103 | }; |
| 3104 | struct nfs_renameres res = { |
| 3105 | .server = server, |
| 3106 | }; |
| 3107 | struct rpc_message msg = { |
| 3108 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME], |
| 3109 | .rpc_argp = &arg, |
| 3110 | .rpc_resp = &res, |
| 3111 | }; |
| 3112 | int status = -ENOMEM; |
| 3113 | |
| 3114 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
| 3115 | if (!status) { |
| 3116 | update_changeattr(old_dir, &res.old_cinfo); |
| 3117 | update_changeattr(new_dir, &res.new_cinfo); |
| 3118 | } |
| 3119 | return status; |
| 3120 | } |
| 3121 | |
| 3122 | static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name, |
| 3123 | struct inode *new_dir, struct qstr *new_name) |
| 3124 | { |
| 3125 | struct nfs4_exception exception = { }; |
| 3126 | int err; |
| 3127 | do { |
| 3128 | err = nfs4_handle_exception(NFS_SERVER(old_dir), |
| 3129 | _nfs4_proc_rename(old_dir, old_name, |
| 3130 | new_dir, new_name), |
| 3131 | &exception); |
| 3132 | } while (exception.retry); |
| 3133 | return err; |
| 3134 | } |
| 3135 | |
| 3136 | static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name) |
| 3137 | { |
| 3138 | struct nfs_server *server = NFS_SERVER(inode); |
| 3139 | struct nfs4_link_arg arg = { |
| 3140 | .fh = NFS_FH(inode), |
| 3141 | .dir_fh = NFS_FH(dir), |
| 3142 | .name = name, |
| 3143 | .bitmask = server->attr_bitmask, |
| 3144 | }; |
| 3145 | struct nfs4_link_res res = { |
| 3146 | .server = server, |
| 3147 | }; |
| 3148 | struct rpc_message msg = { |
| 3149 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK], |
| 3150 | .rpc_argp = &arg, |
| 3151 | .rpc_resp = &res, |
| 3152 | }; |
| 3153 | int status = -ENOMEM; |
| 3154 | |
| 3155 | res.fattr = nfs_alloc_fattr(); |
| 3156 | if (res.fattr == NULL) |
| 3157 | goto out; |
| 3158 | |
| 3159 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
| 3160 | if (!status) { |
| 3161 | update_changeattr(dir, &res.cinfo); |
| 3162 | nfs_post_op_update_inode(inode, res.fattr); |
| 3163 | } |
| 3164 | out: |
| 3165 | nfs_free_fattr(res.fattr); |
| 3166 | return status; |
| 3167 | } |
| 3168 | |
| 3169 | static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name) |
| 3170 | { |
| 3171 | struct nfs4_exception exception = { }; |
| 3172 | int err; |
| 3173 | do { |
| 3174 | err = nfs4_handle_exception(NFS_SERVER(inode), |
| 3175 | _nfs4_proc_link(inode, dir, name), |
| 3176 | &exception); |
| 3177 | } while (exception.retry); |
| 3178 | return err; |
| 3179 | } |
| 3180 | |
| 3181 | struct nfs4_createdata { |
| 3182 | struct rpc_message msg; |
| 3183 | struct nfs4_create_arg arg; |
| 3184 | struct nfs4_create_res res; |
| 3185 | struct nfs_fh fh; |
| 3186 | struct nfs_fattr fattr; |
| 3187 | }; |
| 3188 | |
| 3189 | static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir, |
| 3190 | struct qstr *name, struct iattr *sattr, u32 ftype) |
| 3191 | { |
| 3192 | struct nfs4_createdata *data; |
| 3193 | |
| 3194 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 3195 | if (data != NULL) { |
| 3196 | struct nfs_server *server = NFS_SERVER(dir); |
| 3197 | |
| 3198 | data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE]; |
| 3199 | data->msg.rpc_argp = &data->arg; |
| 3200 | data->msg.rpc_resp = &data->res; |
| 3201 | data->arg.dir_fh = NFS_FH(dir); |
| 3202 | data->arg.server = server; |
| 3203 | data->arg.name = name; |
| 3204 | data->arg.attrs = sattr; |
| 3205 | data->arg.ftype = ftype; |
| 3206 | data->arg.bitmask = server->attr_bitmask; |
| 3207 | data->res.server = server; |
| 3208 | data->res.fh = &data->fh; |
| 3209 | data->res.fattr = &data->fattr; |
| 3210 | nfs_fattr_init(data->res.fattr); |
| 3211 | } |
| 3212 | return data; |
| 3213 | } |
| 3214 | |
| 3215 | static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data) |
| 3216 | { |
| 3217 | int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg, |
| 3218 | &data->arg.seq_args, &data->res.seq_res, 1); |
| 3219 | if (status == 0) { |
| 3220 | update_changeattr(dir, &data->res.dir_cinfo); |
| 3221 | status = nfs_instantiate(dentry, data->res.fh, data->res.fattr); |
| 3222 | } |
| 3223 | return status; |
| 3224 | } |
| 3225 | |
| 3226 | static void nfs4_free_createdata(struct nfs4_createdata *data) |
| 3227 | { |
| 3228 | kfree(data); |
| 3229 | } |
| 3230 | |
| 3231 | static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, |
| 3232 | struct page *page, unsigned int len, struct iattr *sattr) |
| 3233 | { |
| 3234 | struct nfs4_createdata *data; |
| 3235 | int status = -ENAMETOOLONG; |
| 3236 | |
| 3237 | if (len > NFS4_MAXPATHLEN) |
| 3238 | goto out; |
| 3239 | |
| 3240 | status = -ENOMEM; |
| 3241 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK); |
| 3242 | if (data == NULL) |
| 3243 | goto out; |
| 3244 | |
| 3245 | data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK]; |
| 3246 | data->arg.u.symlink.pages = &page; |
| 3247 | data->arg.u.symlink.len = len; |
| 3248 | |
| 3249 | status = nfs4_do_create(dir, dentry, data); |
| 3250 | |
| 3251 | nfs4_free_createdata(data); |
| 3252 | out: |
| 3253 | return status; |
| 3254 | } |
| 3255 | |
| 3256 | static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, |
| 3257 | struct page *page, unsigned int len, struct iattr *sattr) |
| 3258 | { |
| 3259 | struct nfs4_exception exception = { }; |
| 3260 | int err; |
| 3261 | do { |
| 3262 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 3263 | _nfs4_proc_symlink(dir, dentry, page, |
| 3264 | len, sattr), |
| 3265 | &exception); |
| 3266 | } while (exception.retry); |
| 3267 | return err; |
| 3268 | } |
| 3269 | |
| 3270 | static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, |
| 3271 | struct iattr *sattr) |
| 3272 | { |
| 3273 | struct nfs4_createdata *data; |
| 3274 | int status = -ENOMEM; |
| 3275 | |
| 3276 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR); |
| 3277 | if (data == NULL) |
| 3278 | goto out; |
| 3279 | |
| 3280 | status = nfs4_do_create(dir, dentry, data); |
| 3281 | |
| 3282 | nfs4_free_createdata(data); |
| 3283 | out: |
| 3284 | return status; |
| 3285 | } |
| 3286 | |
| 3287 | static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, |
| 3288 | struct iattr *sattr) |
| 3289 | { |
| 3290 | struct nfs4_exception exception = { }; |
| 3291 | int err; |
| 3292 | |
| 3293 | sattr->ia_mode &= ~current_umask(); |
| 3294 | do { |
| 3295 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 3296 | _nfs4_proc_mkdir(dir, dentry, sattr), |
| 3297 | &exception); |
| 3298 | } while (exception.retry); |
| 3299 | return err; |
| 3300 | } |
| 3301 | |
| 3302 | static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, |
| 3303 | u64 cookie, struct page **pages, unsigned int count, int plus) |
| 3304 | { |
| 3305 | struct inode *dir = dentry->d_inode; |
| 3306 | struct nfs4_readdir_arg args = { |
| 3307 | .fh = NFS_FH(dir), |
| 3308 | .pages = pages, |
| 3309 | .pgbase = 0, |
| 3310 | .count = count, |
| 3311 | .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask, |
| 3312 | .plus = plus, |
| 3313 | }; |
| 3314 | struct nfs4_readdir_res res; |
| 3315 | struct rpc_message msg = { |
| 3316 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR], |
| 3317 | .rpc_argp = &args, |
| 3318 | .rpc_resp = &res, |
| 3319 | .rpc_cred = cred, |
| 3320 | }; |
| 3321 | int status; |
| 3322 | |
| 3323 | dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__, |
| 3324 | dentry->d_parent->d_name.name, |
| 3325 | dentry->d_name.name, |
| 3326 | (unsigned long long)cookie); |
| 3327 | nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args); |
| 3328 | res.pgbase = args.pgbase; |
| 3329 | status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0); |
| 3330 | if (status >= 0) { |
| 3331 | memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE); |
| 3332 | status += args.pgbase; |
| 3333 | } |
| 3334 | |
| 3335 | nfs_invalidate_atime(dir); |
| 3336 | |
| 3337 | dprintk("%s: returns %d\n", __func__, status); |
| 3338 | return status; |
| 3339 | } |
| 3340 | |
| 3341 | static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, |
| 3342 | u64 cookie, struct page **pages, unsigned int count, int plus) |
| 3343 | { |
| 3344 | struct nfs4_exception exception = { }; |
| 3345 | int err; |
| 3346 | do { |
| 3347 | err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), |
| 3348 | _nfs4_proc_readdir(dentry, cred, cookie, |
| 3349 | pages, count, plus), |
| 3350 | &exception); |
| 3351 | } while (exception.retry); |
| 3352 | return err; |
| 3353 | } |
| 3354 | |
| 3355 | static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, |
| 3356 | struct iattr *sattr, dev_t rdev) |
| 3357 | { |
| 3358 | struct nfs4_createdata *data; |
| 3359 | int mode = sattr->ia_mode; |
| 3360 | int status = -ENOMEM; |
| 3361 | |
| 3362 | data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK); |
| 3363 | if (data == NULL) |
| 3364 | goto out; |
| 3365 | |
| 3366 | if (S_ISFIFO(mode)) |
| 3367 | data->arg.ftype = NF4FIFO; |
| 3368 | else if (S_ISBLK(mode)) { |
| 3369 | data->arg.ftype = NF4BLK; |
| 3370 | data->arg.u.device.specdata1 = MAJOR(rdev); |
| 3371 | data->arg.u.device.specdata2 = MINOR(rdev); |
| 3372 | } |
| 3373 | else if (S_ISCHR(mode)) { |
| 3374 | data->arg.ftype = NF4CHR; |
| 3375 | data->arg.u.device.specdata1 = MAJOR(rdev); |
| 3376 | data->arg.u.device.specdata2 = MINOR(rdev); |
| 3377 | } else if (!S_ISSOCK(mode)) { |
| 3378 | status = -EINVAL; |
| 3379 | goto out_free; |
| 3380 | } |
| 3381 | |
| 3382 | status = nfs4_do_create(dir, dentry, data); |
| 3383 | out_free: |
| 3384 | nfs4_free_createdata(data); |
| 3385 | out: |
| 3386 | return status; |
| 3387 | } |
| 3388 | |
| 3389 | static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, |
| 3390 | struct iattr *sattr, dev_t rdev) |
| 3391 | { |
| 3392 | struct nfs4_exception exception = { }; |
| 3393 | int err; |
| 3394 | |
| 3395 | sattr->ia_mode &= ~current_umask(); |
| 3396 | do { |
| 3397 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 3398 | _nfs4_proc_mknod(dir, dentry, sattr, rdev), |
| 3399 | &exception); |
| 3400 | } while (exception.retry); |
| 3401 | return err; |
| 3402 | } |
| 3403 | |
| 3404 | static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, |
| 3405 | struct nfs_fsstat *fsstat) |
| 3406 | { |
| 3407 | struct nfs4_statfs_arg args = { |
| 3408 | .fh = fhandle, |
| 3409 | .bitmask = server->attr_bitmask, |
| 3410 | }; |
| 3411 | struct nfs4_statfs_res res = { |
| 3412 | .fsstat = fsstat, |
| 3413 | }; |
| 3414 | struct rpc_message msg = { |
| 3415 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS], |
| 3416 | .rpc_argp = &args, |
| 3417 | .rpc_resp = &res, |
| 3418 | }; |
| 3419 | |
| 3420 | nfs_fattr_init(fsstat->fattr); |
| 3421 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
| 3422 | } |
| 3423 | |
| 3424 | static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat) |
| 3425 | { |
| 3426 | struct nfs4_exception exception = { }; |
| 3427 | int err; |
| 3428 | do { |
| 3429 | err = nfs4_handle_exception(server, |
| 3430 | _nfs4_proc_statfs(server, fhandle, fsstat), |
| 3431 | &exception); |
| 3432 | } while (exception.retry); |
| 3433 | return err; |
| 3434 | } |
| 3435 | |
| 3436 | static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, |
| 3437 | struct nfs_fsinfo *fsinfo) |
| 3438 | { |
| 3439 | struct nfs4_fsinfo_arg args = { |
| 3440 | .fh = fhandle, |
| 3441 | .bitmask = server->attr_bitmask, |
| 3442 | }; |
| 3443 | struct nfs4_fsinfo_res res = { |
| 3444 | .fsinfo = fsinfo, |
| 3445 | }; |
| 3446 | struct rpc_message msg = { |
| 3447 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO], |
| 3448 | .rpc_argp = &args, |
| 3449 | .rpc_resp = &res, |
| 3450 | }; |
| 3451 | |
| 3452 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
| 3453 | } |
| 3454 | |
| 3455 | static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) |
| 3456 | { |
| 3457 | struct nfs4_exception exception = { }; |
| 3458 | unsigned long now = jiffies; |
| 3459 | int err; |
| 3460 | |
| 3461 | do { |
| 3462 | err = _nfs4_do_fsinfo(server, fhandle, fsinfo); |
| 3463 | if (err == 0) { |
| 3464 | struct nfs_client *clp = server->nfs_client; |
| 3465 | |
| 3466 | spin_lock(&clp->cl_lock); |
| 3467 | clp->cl_lease_time = fsinfo->lease_time * HZ; |
| 3468 | clp->cl_last_renewal = now; |
| 3469 | spin_unlock(&clp->cl_lock); |
| 3470 | break; |
| 3471 | } |
| 3472 | err = nfs4_handle_exception(server, err, &exception); |
| 3473 | } while (exception.retry); |
| 3474 | return err; |
| 3475 | } |
| 3476 | |
| 3477 | static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) |
| 3478 | { |
| 3479 | int error; |
| 3480 | |
| 3481 | nfs_fattr_init(fsinfo->fattr); |
| 3482 | error = nfs4_do_fsinfo(server, fhandle, fsinfo); |
| 3483 | if (error == 0) { |
| 3484 | /* block layout checks this! */ |
| 3485 | server->pnfs_blksize = fsinfo->blksize; |
| 3486 | set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype); |
| 3487 | } |
| 3488 | |
| 3489 | return error; |
| 3490 | } |
| 3491 | |
| 3492 | static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, |
| 3493 | struct nfs_pathconf *pathconf) |
| 3494 | { |
| 3495 | struct nfs4_pathconf_arg args = { |
| 3496 | .fh = fhandle, |
| 3497 | .bitmask = server->attr_bitmask, |
| 3498 | }; |
| 3499 | struct nfs4_pathconf_res res = { |
| 3500 | .pathconf = pathconf, |
| 3501 | }; |
| 3502 | struct rpc_message msg = { |
| 3503 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF], |
| 3504 | .rpc_argp = &args, |
| 3505 | .rpc_resp = &res, |
| 3506 | }; |
| 3507 | |
| 3508 | /* None of the pathconf attributes are mandatory to implement */ |
| 3509 | if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) { |
| 3510 | memset(pathconf, 0, sizeof(*pathconf)); |
| 3511 | return 0; |
| 3512 | } |
| 3513 | |
| 3514 | nfs_fattr_init(pathconf->fattr); |
| 3515 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
| 3516 | } |
| 3517 | |
| 3518 | static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, |
| 3519 | struct nfs_pathconf *pathconf) |
| 3520 | { |
| 3521 | struct nfs4_exception exception = { }; |
| 3522 | int err; |
| 3523 | |
| 3524 | do { |
| 3525 | err = nfs4_handle_exception(server, |
| 3526 | _nfs4_proc_pathconf(server, fhandle, pathconf), |
| 3527 | &exception); |
| 3528 | } while (exception.retry); |
| 3529 | return err; |
| 3530 | } |
| 3531 | |
| 3532 | int nfs4_set_rw_stateid(nfs4_stateid *stateid, |
| 3533 | const struct nfs_open_context *ctx, |
| 3534 | const struct nfs_lock_context *l_ctx, |
| 3535 | fmode_t fmode) |
| 3536 | { |
| 3537 | const struct nfs_lockowner *lockowner = NULL; |
| 3538 | |
| 3539 | if (l_ctx != NULL) |
| 3540 | lockowner = &l_ctx->lockowner; |
| 3541 | return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner); |
| 3542 | } |
| 3543 | EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid); |
| 3544 | |
| 3545 | static bool nfs4_stateid_is_current(nfs4_stateid *stateid, |
| 3546 | const struct nfs_open_context *ctx, |
| 3547 | const struct nfs_lock_context *l_ctx, |
| 3548 | fmode_t fmode) |
| 3549 | { |
| 3550 | nfs4_stateid current_stateid; |
| 3551 | |
| 3552 | if (nfs4_set_rw_stateid(¤t_stateid, ctx, l_ctx, fmode)) |
| 3553 | return false; |
| 3554 | return nfs4_stateid_match(stateid, ¤t_stateid); |
| 3555 | } |
| 3556 | |
| 3557 | static bool nfs4_error_stateid_expired(int err) |
| 3558 | { |
| 3559 | switch (err) { |
| 3560 | case -NFS4ERR_DELEG_REVOKED: |
| 3561 | case -NFS4ERR_ADMIN_REVOKED: |
| 3562 | case -NFS4ERR_BAD_STATEID: |
| 3563 | case -NFS4ERR_STALE_STATEID: |
| 3564 | case -NFS4ERR_OLD_STATEID: |
| 3565 | case -NFS4ERR_OPENMODE: |
| 3566 | case -NFS4ERR_EXPIRED: |
| 3567 | return true; |
| 3568 | } |
| 3569 | return false; |
| 3570 | } |
| 3571 | |
| 3572 | void __nfs4_read_done_cb(struct nfs_read_data *data) |
| 3573 | { |
| 3574 | nfs_invalidate_atime(data->header->inode); |
| 3575 | } |
| 3576 | |
| 3577 | static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data) |
| 3578 | { |
| 3579 | struct nfs_server *server = NFS_SERVER(data->header->inode); |
| 3580 | |
| 3581 | if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) { |
| 3582 | rpc_restart_call_prepare(task); |
| 3583 | return -EAGAIN; |
| 3584 | } |
| 3585 | |
| 3586 | __nfs4_read_done_cb(data); |
| 3587 | if (task->tk_status > 0) |
| 3588 | renew_lease(server, data->timestamp); |
| 3589 | return 0; |
| 3590 | } |
| 3591 | |
| 3592 | static bool nfs4_read_stateid_changed(struct rpc_task *task, |
| 3593 | struct nfs_readargs *args) |
| 3594 | { |
| 3595 | |
| 3596 | if (!nfs4_error_stateid_expired(task->tk_status) || |
| 3597 | nfs4_stateid_is_current(&args->stateid, |
| 3598 | args->context, |
| 3599 | args->lock_context, |
| 3600 | FMODE_READ)) |
| 3601 | return false; |
| 3602 | rpc_restart_call_prepare(task); |
| 3603 | return true; |
| 3604 | } |
| 3605 | |
| 3606 | static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data) |
| 3607 | { |
| 3608 | |
| 3609 | dprintk("--> %s\n", __func__); |
| 3610 | |
| 3611 | if (!nfs4_sequence_done(task, &data->res.seq_res)) |
| 3612 | return -EAGAIN; |
| 3613 | if (nfs4_read_stateid_changed(task, &data->args)) |
| 3614 | return -EAGAIN; |
| 3615 | return data->read_done_cb ? data->read_done_cb(task, data) : |
| 3616 | nfs4_read_done_cb(task, data); |
| 3617 | } |
| 3618 | |
| 3619 | static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg) |
| 3620 | { |
| 3621 | data->timestamp = jiffies; |
| 3622 | data->read_done_cb = nfs4_read_done_cb; |
| 3623 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ]; |
| 3624 | nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0); |
| 3625 | } |
| 3626 | |
| 3627 | static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data) |
| 3628 | { |
| 3629 | if (nfs4_setup_sequence(NFS_SERVER(data->header->inode), |
| 3630 | &data->args.seq_args, |
| 3631 | &data->res.seq_res, |
| 3632 | task)) |
| 3633 | return; |
| 3634 | nfs4_set_rw_stateid(&data->args.stateid, data->args.context, |
| 3635 | data->args.lock_context, FMODE_READ); |
| 3636 | } |
| 3637 | |
| 3638 | static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data) |
| 3639 | { |
| 3640 | struct inode *inode = data->header->inode; |
| 3641 | |
| 3642 | if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) { |
| 3643 | rpc_restart_call_prepare(task); |
| 3644 | return -EAGAIN; |
| 3645 | } |
| 3646 | if (task->tk_status >= 0) { |
| 3647 | renew_lease(NFS_SERVER(inode), data->timestamp); |
| 3648 | nfs_post_op_update_inode_force_wcc(inode, &data->fattr); |
| 3649 | } |
| 3650 | return 0; |
| 3651 | } |
| 3652 | |
| 3653 | static bool nfs4_write_stateid_changed(struct rpc_task *task, |
| 3654 | struct nfs_writeargs *args) |
| 3655 | { |
| 3656 | |
| 3657 | if (!nfs4_error_stateid_expired(task->tk_status) || |
| 3658 | nfs4_stateid_is_current(&args->stateid, |
| 3659 | args->context, |
| 3660 | args->lock_context, |
| 3661 | FMODE_WRITE)) |
| 3662 | return false; |
| 3663 | rpc_restart_call_prepare(task); |
| 3664 | return true; |
| 3665 | } |
| 3666 | |
| 3667 | static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data) |
| 3668 | { |
| 3669 | if (!nfs4_sequence_done(task, &data->res.seq_res)) |
| 3670 | return -EAGAIN; |
| 3671 | if (nfs4_write_stateid_changed(task, &data->args)) |
| 3672 | return -EAGAIN; |
| 3673 | return data->write_done_cb ? data->write_done_cb(task, data) : |
| 3674 | nfs4_write_done_cb(task, data); |
| 3675 | } |
| 3676 | |
| 3677 | static |
| 3678 | bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data) |
| 3679 | { |
| 3680 | const struct nfs_pgio_header *hdr = data->header; |
| 3681 | |
| 3682 | /* Don't request attributes for pNFS or O_DIRECT writes */ |
| 3683 | if (data->ds_clp != NULL || hdr->dreq != NULL) |
| 3684 | return false; |
| 3685 | /* Otherwise, request attributes if and only if we don't hold |
| 3686 | * a delegation |
| 3687 | */ |
| 3688 | return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0; |
| 3689 | } |
| 3690 | |
| 3691 | static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg) |
| 3692 | { |
| 3693 | struct nfs_server *server = NFS_SERVER(data->header->inode); |
| 3694 | |
| 3695 | if (!nfs4_write_need_cache_consistency_data(data)) { |
| 3696 | data->args.bitmask = NULL; |
| 3697 | data->res.fattr = NULL; |
| 3698 | } else |
| 3699 | data->args.bitmask = server->cache_consistency_bitmask; |
| 3700 | |
| 3701 | if (!data->write_done_cb) |
| 3702 | data->write_done_cb = nfs4_write_done_cb; |
| 3703 | data->res.server = server; |
| 3704 | data->timestamp = jiffies; |
| 3705 | |
| 3706 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE]; |
| 3707 | nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1); |
| 3708 | } |
| 3709 | |
| 3710 | static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data) |
| 3711 | { |
| 3712 | if (nfs4_setup_sequence(NFS_SERVER(data->header->inode), |
| 3713 | &data->args.seq_args, |
| 3714 | &data->res.seq_res, |
| 3715 | task)) |
| 3716 | return; |
| 3717 | nfs4_set_rw_stateid(&data->args.stateid, data->args.context, |
| 3718 | data->args.lock_context, FMODE_WRITE); |
| 3719 | } |
| 3720 | |
| 3721 | static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data) |
| 3722 | { |
| 3723 | nfs4_setup_sequence(NFS_SERVER(data->inode), |
| 3724 | &data->args.seq_args, |
| 3725 | &data->res.seq_res, |
| 3726 | task); |
| 3727 | } |
| 3728 | |
| 3729 | static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data) |
| 3730 | { |
| 3731 | struct inode *inode = data->inode; |
| 3732 | |
| 3733 | if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) { |
| 3734 | rpc_restart_call_prepare(task); |
| 3735 | return -EAGAIN; |
| 3736 | } |
| 3737 | return 0; |
| 3738 | } |
| 3739 | |
| 3740 | static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data) |
| 3741 | { |
| 3742 | if (!nfs4_sequence_done(task, &data->res.seq_res)) |
| 3743 | return -EAGAIN; |
| 3744 | return data->commit_done_cb(task, data); |
| 3745 | } |
| 3746 | |
| 3747 | static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg) |
| 3748 | { |
| 3749 | struct nfs_server *server = NFS_SERVER(data->inode); |
| 3750 | |
| 3751 | if (data->commit_done_cb == NULL) |
| 3752 | data->commit_done_cb = nfs4_commit_done_cb; |
| 3753 | data->res.server = server; |
| 3754 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT]; |
| 3755 | nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1); |
| 3756 | } |
| 3757 | |
| 3758 | struct nfs4_renewdata { |
| 3759 | struct nfs_client *client; |
| 3760 | unsigned long timestamp; |
| 3761 | }; |
| 3762 | |
| 3763 | /* |
| 3764 | * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special |
| 3765 | * standalone procedure for queueing an asynchronous RENEW. |
| 3766 | */ |
| 3767 | static void nfs4_renew_release(void *calldata) |
| 3768 | { |
| 3769 | struct nfs4_renewdata *data = calldata; |
| 3770 | struct nfs_client *clp = data->client; |
| 3771 | |
| 3772 | if (atomic_read(&clp->cl_count) > 1) |
| 3773 | nfs4_schedule_state_renewal(clp); |
| 3774 | nfs_put_client(clp); |
| 3775 | kfree(data); |
| 3776 | } |
| 3777 | |
| 3778 | static void nfs4_renew_done(struct rpc_task *task, void *calldata) |
| 3779 | { |
| 3780 | struct nfs4_renewdata *data = calldata; |
| 3781 | struct nfs_client *clp = data->client; |
| 3782 | unsigned long timestamp = data->timestamp; |
| 3783 | |
| 3784 | if (task->tk_status < 0) { |
| 3785 | /* Unless we're shutting down, schedule state recovery! */ |
| 3786 | if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0) |
| 3787 | return; |
| 3788 | if (task->tk_status != NFS4ERR_CB_PATH_DOWN) { |
| 3789 | nfs4_schedule_lease_recovery(clp); |
| 3790 | return; |
| 3791 | } |
| 3792 | nfs4_schedule_path_down_recovery(clp); |
| 3793 | } |
| 3794 | do_renew_lease(clp, timestamp); |
| 3795 | } |
| 3796 | |
| 3797 | static const struct rpc_call_ops nfs4_renew_ops = { |
| 3798 | .rpc_call_done = nfs4_renew_done, |
| 3799 | .rpc_release = nfs4_renew_release, |
| 3800 | }; |
| 3801 | |
| 3802 | static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags) |
| 3803 | { |
| 3804 | struct rpc_message msg = { |
| 3805 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], |
| 3806 | .rpc_argp = clp, |
| 3807 | .rpc_cred = cred, |
| 3808 | }; |
| 3809 | struct nfs4_renewdata *data; |
| 3810 | |
| 3811 | if (renew_flags == 0) |
| 3812 | return 0; |
| 3813 | if (!atomic_inc_not_zero(&clp->cl_count)) |
| 3814 | return -EIO; |
| 3815 | data = kmalloc(sizeof(*data), GFP_NOFS); |
| 3816 | if (data == NULL) |
| 3817 | return -ENOMEM; |
| 3818 | data->client = clp; |
| 3819 | data->timestamp = jiffies; |
| 3820 | return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT, |
| 3821 | &nfs4_renew_ops, data); |
| 3822 | } |
| 3823 | |
| 3824 | static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred) |
| 3825 | { |
| 3826 | struct rpc_message msg = { |
| 3827 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], |
| 3828 | .rpc_argp = clp, |
| 3829 | .rpc_cred = cred, |
| 3830 | }; |
| 3831 | unsigned long now = jiffies; |
| 3832 | int status; |
| 3833 | |
| 3834 | status = rpc_call_sync(clp->cl_rpcclient, &msg, 0); |
| 3835 | if (status < 0) |
| 3836 | return status; |
| 3837 | do_renew_lease(clp, now); |
| 3838 | return 0; |
| 3839 | } |
| 3840 | |
| 3841 | static inline int nfs4_server_supports_acls(struct nfs_server *server) |
| 3842 | { |
| 3843 | return (server->caps & NFS_CAP_ACLS) |
| 3844 | && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL) |
| 3845 | && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL); |
| 3846 | } |
| 3847 | |
| 3848 | /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that |
| 3849 | * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on |
| 3850 | * the stack. |
| 3851 | */ |
| 3852 | #define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE) |
| 3853 | |
| 3854 | static int buf_to_pages_noslab(const void *buf, size_t buflen, |
| 3855 | struct page **pages, unsigned int *pgbase) |
| 3856 | { |
| 3857 | struct page *newpage, **spages; |
| 3858 | int rc = 0; |
| 3859 | size_t len; |
| 3860 | spages = pages; |
| 3861 | |
| 3862 | do { |
| 3863 | len = min_t(size_t, PAGE_SIZE, buflen); |
| 3864 | newpage = alloc_page(GFP_KERNEL); |
| 3865 | |
| 3866 | if (newpage == NULL) |
| 3867 | goto unwind; |
| 3868 | memcpy(page_address(newpage), buf, len); |
| 3869 | buf += len; |
| 3870 | buflen -= len; |
| 3871 | *pages++ = newpage; |
| 3872 | rc++; |
| 3873 | } while (buflen != 0); |
| 3874 | |
| 3875 | return rc; |
| 3876 | |
| 3877 | unwind: |
| 3878 | for(; rc > 0; rc--) |
| 3879 | __free_page(spages[rc-1]); |
| 3880 | return -ENOMEM; |
| 3881 | } |
| 3882 | |
| 3883 | struct nfs4_cached_acl { |
| 3884 | int cached; |
| 3885 | size_t len; |
| 3886 | char data[0]; |
| 3887 | }; |
| 3888 | |
| 3889 | static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl) |
| 3890 | { |
| 3891 | struct nfs_inode *nfsi = NFS_I(inode); |
| 3892 | |
| 3893 | spin_lock(&inode->i_lock); |
| 3894 | kfree(nfsi->nfs4_acl); |
| 3895 | nfsi->nfs4_acl = acl; |
| 3896 | spin_unlock(&inode->i_lock); |
| 3897 | } |
| 3898 | |
| 3899 | static void nfs4_zap_acl_attr(struct inode *inode) |
| 3900 | { |
| 3901 | nfs4_set_cached_acl(inode, NULL); |
| 3902 | } |
| 3903 | |
| 3904 | static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen) |
| 3905 | { |
| 3906 | struct nfs_inode *nfsi = NFS_I(inode); |
| 3907 | struct nfs4_cached_acl *acl; |
| 3908 | int ret = -ENOENT; |
| 3909 | |
| 3910 | spin_lock(&inode->i_lock); |
| 3911 | acl = nfsi->nfs4_acl; |
| 3912 | if (acl == NULL) |
| 3913 | goto out; |
| 3914 | if (buf == NULL) /* user is just asking for length */ |
| 3915 | goto out_len; |
| 3916 | if (acl->cached == 0) |
| 3917 | goto out; |
| 3918 | ret = -ERANGE; /* see getxattr(2) man page */ |
| 3919 | if (acl->len > buflen) |
| 3920 | goto out; |
| 3921 | memcpy(buf, acl->data, acl->len); |
| 3922 | out_len: |
| 3923 | ret = acl->len; |
| 3924 | out: |
| 3925 | spin_unlock(&inode->i_lock); |
| 3926 | return ret; |
| 3927 | } |
| 3928 | |
| 3929 | static void nfs4_write_cached_acl(struct inode *inode, struct page **pages, size_t pgbase, size_t acl_len) |
| 3930 | { |
| 3931 | struct nfs4_cached_acl *acl; |
| 3932 | size_t buflen = sizeof(*acl) + acl_len; |
| 3933 | |
| 3934 | if (buflen <= PAGE_SIZE) { |
| 3935 | acl = kmalloc(buflen, GFP_KERNEL); |
| 3936 | if (acl == NULL) |
| 3937 | goto out; |
| 3938 | acl->cached = 1; |
| 3939 | _copy_from_pages(acl->data, pages, pgbase, acl_len); |
| 3940 | } else { |
| 3941 | acl = kmalloc(sizeof(*acl), GFP_KERNEL); |
| 3942 | if (acl == NULL) |
| 3943 | goto out; |
| 3944 | acl->cached = 0; |
| 3945 | } |
| 3946 | acl->len = acl_len; |
| 3947 | out: |
| 3948 | nfs4_set_cached_acl(inode, acl); |
| 3949 | } |
| 3950 | |
| 3951 | /* |
| 3952 | * The getxattr API returns the required buffer length when called with a |
| 3953 | * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating |
| 3954 | * the required buf. On a NULL buf, we send a page of data to the server |
| 3955 | * guessing that the ACL request can be serviced by a page. If so, we cache |
| 3956 | * up to the page of ACL data, and the 2nd call to getxattr is serviced by |
| 3957 | * the cache. If not so, we throw away the page, and cache the required |
| 3958 | * length. The next getxattr call will then produce another round trip to |
| 3959 | * the server, this time with the input buf of the required size. |
| 3960 | */ |
| 3961 | static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) |
| 3962 | { |
| 3963 | struct page *pages[NFS4ACL_MAXPAGES] = {NULL, }; |
| 3964 | struct nfs_getaclargs args = { |
| 3965 | .fh = NFS_FH(inode), |
| 3966 | .acl_pages = pages, |
| 3967 | .acl_len = buflen, |
| 3968 | }; |
| 3969 | struct nfs_getaclres res = { |
| 3970 | .acl_len = buflen, |
| 3971 | }; |
| 3972 | struct rpc_message msg = { |
| 3973 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL], |
| 3974 | .rpc_argp = &args, |
| 3975 | .rpc_resp = &res, |
| 3976 | }; |
| 3977 | unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE); |
| 3978 | int ret = -ENOMEM, i; |
| 3979 | |
| 3980 | /* As long as we're doing a round trip to the server anyway, |
| 3981 | * let's be prepared for a page of acl data. */ |
| 3982 | if (npages == 0) |
| 3983 | npages = 1; |
| 3984 | if (npages > ARRAY_SIZE(pages)) |
| 3985 | return -ERANGE; |
| 3986 | |
| 3987 | for (i = 0; i < npages; i++) { |
| 3988 | pages[i] = alloc_page(GFP_KERNEL); |
| 3989 | if (!pages[i]) |
| 3990 | goto out_free; |
| 3991 | } |
| 3992 | |
| 3993 | /* for decoding across pages */ |
| 3994 | res.acl_scratch = alloc_page(GFP_KERNEL); |
| 3995 | if (!res.acl_scratch) |
| 3996 | goto out_free; |
| 3997 | |
| 3998 | args.acl_len = npages * PAGE_SIZE; |
| 3999 | args.acl_pgbase = 0; |
| 4000 | |
| 4001 | dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n", |
| 4002 | __func__, buf, buflen, npages, args.acl_len); |
| 4003 | ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), |
| 4004 | &msg, &args.seq_args, &res.seq_res, 0); |
| 4005 | if (ret) |
| 4006 | goto out_free; |
| 4007 | |
| 4008 | /* Handle the case where the passed-in buffer is too short */ |
| 4009 | if (res.acl_flags & NFS4_ACL_TRUNC) { |
| 4010 | /* Did the user only issue a request for the acl length? */ |
| 4011 | if (buf == NULL) |
| 4012 | goto out_ok; |
| 4013 | ret = -ERANGE; |
| 4014 | goto out_free; |
| 4015 | } |
| 4016 | nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len); |
| 4017 | if (buf) { |
| 4018 | if (res.acl_len > buflen) { |
| 4019 | ret = -ERANGE; |
| 4020 | goto out_free; |
| 4021 | } |
| 4022 | _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len); |
| 4023 | } |
| 4024 | out_ok: |
| 4025 | ret = res.acl_len; |
| 4026 | out_free: |
| 4027 | for (i = 0; i < npages; i++) |
| 4028 | if (pages[i]) |
| 4029 | __free_page(pages[i]); |
| 4030 | if (res.acl_scratch) |
| 4031 | __free_page(res.acl_scratch); |
| 4032 | return ret; |
| 4033 | } |
| 4034 | |
| 4035 | static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) |
| 4036 | { |
| 4037 | struct nfs4_exception exception = { }; |
| 4038 | ssize_t ret; |
| 4039 | do { |
| 4040 | ret = __nfs4_get_acl_uncached(inode, buf, buflen); |
| 4041 | if (ret >= 0) |
| 4042 | break; |
| 4043 | ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception); |
| 4044 | } while (exception.retry); |
| 4045 | return ret; |
| 4046 | } |
| 4047 | |
| 4048 | static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen) |
| 4049 | { |
| 4050 | struct nfs_server *server = NFS_SERVER(inode); |
| 4051 | int ret; |
| 4052 | |
| 4053 | if (!nfs4_server_supports_acls(server)) |
| 4054 | return -EOPNOTSUPP; |
| 4055 | ret = nfs_revalidate_inode(server, inode); |
| 4056 | if (ret < 0) |
| 4057 | return ret; |
| 4058 | if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL) |
| 4059 | nfs_zap_acl_cache(inode); |
| 4060 | ret = nfs4_read_cached_acl(inode, buf, buflen); |
| 4061 | if (ret != -ENOENT) |
| 4062 | /* -ENOENT is returned if there is no ACL or if there is an ACL |
| 4063 | * but no cached acl data, just the acl length */ |
| 4064 | return ret; |
| 4065 | return nfs4_get_acl_uncached(inode, buf, buflen); |
| 4066 | } |
| 4067 | |
| 4068 | static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) |
| 4069 | { |
| 4070 | struct nfs_server *server = NFS_SERVER(inode); |
| 4071 | struct page *pages[NFS4ACL_MAXPAGES]; |
| 4072 | struct nfs_setaclargs arg = { |
| 4073 | .fh = NFS_FH(inode), |
| 4074 | .acl_pages = pages, |
| 4075 | .acl_len = buflen, |
| 4076 | }; |
| 4077 | struct nfs_setaclres res; |
| 4078 | struct rpc_message msg = { |
| 4079 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL], |
| 4080 | .rpc_argp = &arg, |
| 4081 | .rpc_resp = &res, |
| 4082 | }; |
| 4083 | unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE); |
| 4084 | int ret, i; |
| 4085 | |
| 4086 | if (!nfs4_server_supports_acls(server)) |
| 4087 | return -EOPNOTSUPP; |
| 4088 | if (npages > ARRAY_SIZE(pages)) |
| 4089 | return -ERANGE; |
| 4090 | i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase); |
| 4091 | if (i < 0) |
| 4092 | return i; |
| 4093 | nfs4_inode_return_delegation(inode); |
| 4094 | ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
| 4095 | |
| 4096 | /* |
| 4097 | * Free each page after tx, so the only ref left is |
| 4098 | * held by the network stack |
| 4099 | */ |
| 4100 | for (; i > 0; i--) |
| 4101 | put_page(pages[i-1]); |
| 4102 | |
| 4103 | /* |
| 4104 | * Acl update can result in inode attribute update. |
| 4105 | * so mark the attribute cache invalid. |
| 4106 | */ |
| 4107 | spin_lock(&inode->i_lock); |
| 4108 | NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR; |
| 4109 | spin_unlock(&inode->i_lock); |
| 4110 | nfs_access_zap_cache(inode); |
| 4111 | nfs_zap_acl_cache(inode); |
| 4112 | return ret; |
| 4113 | } |
| 4114 | |
| 4115 | static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) |
| 4116 | { |
| 4117 | struct nfs4_exception exception = { }; |
| 4118 | int err; |
| 4119 | do { |
| 4120 | err = nfs4_handle_exception(NFS_SERVER(inode), |
| 4121 | __nfs4_proc_set_acl(inode, buf, buflen), |
| 4122 | &exception); |
| 4123 | } while (exception.retry); |
| 4124 | return err; |
| 4125 | } |
| 4126 | |
| 4127 | static int |
| 4128 | nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state) |
| 4129 | { |
| 4130 | struct nfs_client *clp = server->nfs_client; |
| 4131 | |
| 4132 | if (task->tk_status >= 0) |
| 4133 | return 0; |
| 4134 | switch(task->tk_status) { |
| 4135 | case -NFS4ERR_DELEG_REVOKED: |
| 4136 | case -NFS4ERR_ADMIN_REVOKED: |
| 4137 | case -NFS4ERR_BAD_STATEID: |
| 4138 | if (state == NULL) |
| 4139 | break; |
| 4140 | nfs_remove_bad_delegation(state->inode); |
| 4141 | case -NFS4ERR_OPENMODE: |
| 4142 | if (state == NULL) |
| 4143 | break; |
| 4144 | if (nfs4_schedule_stateid_recovery(server, state) < 0) |
| 4145 | goto stateid_invalid; |
| 4146 | goto wait_on_recovery; |
| 4147 | case -NFS4ERR_EXPIRED: |
| 4148 | if (state != NULL) { |
| 4149 | if (nfs4_schedule_stateid_recovery(server, state) < 0) |
| 4150 | goto stateid_invalid; |
| 4151 | } |
| 4152 | case -NFS4ERR_STALE_STATEID: |
| 4153 | case -NFS4ERR_STALE_CLIENTID: |
| 4154 | nfs4_schedule_lease_recovery(clp); |
| 4155 | goto wait_on_recovery; |
| 4156 | #if defined(CONFIG_NFS_V4_1) |
| 4157 | case -NFS4ERR_BADSESSION: |
| 4158 | case -NFS4ERR_BADSLOT: |
| 4159 | case -NFS4ERR_BAD_HIGH_SLOT: |
| 4160 | case -NFS4ERR_DEADSESSION: |
| 4161 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: |
| 4162 | case -NFS4ERR_SEQ_FALSE_RETRY: |
| 4163 | case -NFS4ERR_SEQ_MISORDERED: |
| 4164 | dprintk("%s ERROR %d, Reset session\n", __func__, |
| 4165 | task->tk_status); |
| 4166 | nfs4_schedule_session_recovery(clp->cl_session, task->tk_status); |
| 4167 | task->tk_status = 0; |
| 4168 | return -EAGAIN; |
| 4169 | #endif /* CONFIG_NFS_V4_1 */ |
| 4170 | case -NFS4ERR_DELAY: |
| 4171 | nfs_inc_server_stats(server, NFSIOS_DELAY); |
| 4172 | case -NFS4ERR_GRACE: |
| 4173 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
| 4174 | task->tk_status = 0; |
| 4175 | return -EAGAIN; |
| 4176 | case -NFS4ERR_RETRY_UNCACHED_REP: |
| 4177 | case -NFS4ERR_OLD_STATEID: |
| 4178 | task->tk_status = 0; |
| 4179 | return -EAGAIN; |
| 4180 | } |
| 4181 | task->tk_status = nfs4_map_errors(task->tk_status); |
| 4182 | return 0; |
| 4183 | stateid_invalid: |
| 4184 | task->tk_status = -EIO; |
| 4185 | return 0; |
| 4186 | wait_on_recovery: |
| 4187 | rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL); |
| 4188 | if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0) |
| 4189 | rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task); |
| 4190 | task->tk_status = 0; |
| 4191 | return -EAGAIN; |
| 4192 | } |
| 4193 | |
| 4194 | static void nfs4_init_boot_verifier(const struct nfs_client *clp, |
| 4195 | nfs4_verifier *bootverf) |
| 4196 | { |
| 4197 | __be32 verf[2]; |
| 4198 | |
| 4199 | if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) { |
| 4200 | /* An impossible timestamp guarantees this value |
| 4201 | * will never match a generated boot time. */ |
| 4202 | verf[0] = 0; |
| 4203 | verf[1] = (__be32)(NSEC_PER_SEC + 1); |
| 4204 | } else { |
| 4205 | struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); |
| 4206 | verf[0] = (__be32)nn->boot_time.tv_sec; |
| 4207 | verf[1] = (__be32)nn->boot_time.tv_nsec; |
| 4208 | } |
| 4209 | memcpy(bootverf->data, verf, sizeof(bootverf->data)); |
| 4210 | } |
| 4211 | |
| 4212 | static unsigned int |
| 4213 | nfs4_init_nonuniform_client_string(const struct nfs_client *clp, |
| 4214 | char *buf, size_t len) |
| 4215 | { |
| 4216 | unsigned int result; |
| 4217 | |
| 4218 | rcu_read_lock(); |
| 4219 | result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s", |
| 4220 | clp->cl_ipaddr, |
| 4221 | rpc_peeraddr2str(clp->cl_rpcclient, |
| 4222 | RPC_DISPLAY_ADDR), |
| 4223 | rpc_peeraddr2str(clp->cl_rpcclient, |
| 4224 | RPC_DISPLAY_PROTO)); |
| 4225 | rcu_read_unlock(); |
| 4226 | return result; |
| 4227 | } |
| 4228 | |
| 4229 | static unsigned int |
| 4230 | nfs4_init_uniform_client_string(const struct nfs_client *clp, |
| 4231 | char *buf, size_t len) |
| 4232 | { |
| 4233 | char *nodename = clp->cl_rpcclient->cl_nodename; |
| 4234 | |
| 4235 | if (nfs4_client_id_uniquifier[0] != '\0') |
| 4236 | nodename = nfs4_client_id_uniquifier; |
| 4237 | return scnprintf(buf, len, "Linux NFSv%u.%u %s", |
| 4238 | clp->rpc_ops->version, clp->cl_minorversion, |
| 4239 | nodename); |
| 4240 | } |
| 4241 | |
| 4242 | /** |
| 4243 | * nfs4_proc_setclientid - Negotiate client ID |
| 4244 | * @clp: state data structure |
| 4245 | * @program: RPC program for NFSv4 callback service |
| 4246 | * @port: IP port number for NFS4 callback service |
| 4247 | * @cred: RPC credential to use for this call |
| 4248 | * @res: where to place the result |
| 4249 | * |
| 4250 | * Returns zero, a negative errno, or a negative NFS4ERR status code. |
| 4251 | */ |
| 4252 | int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, |
| 4253 | unsigned short port, struct rpc_cred *cred, |
| 4254 | struct nfs4_setclientid_res *res) |
| 4255 | { |
| 4256 | nfs4_verifier sc_verifier; |
| 4257 | struct nfs4_setclientid setclientid = { |
| 4258 | .sc_verifier = &sc_verifier, |
| 4259 | .sc_prog = program, |
| 4260 | .sc_cb_ident = clp->cl_cb_ident, |
| 4261 | }; |
| 4262 | struct rpc_message msg = { |
| 4263 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID], |
| 4264 | .rpc_argp = &setclientid, |
| 4265 | .rpc_resp = res, |
| 4266 | .rpc_cred = cred, |
| 4267 | }; |
| 4268 | int status; |
| 4269 | |
| 4270 | /* nfs_client_id4 */ |
| 4271 | nfs4_init_boot_verifier(clp, &sc_verifier); |
| 4272 | if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags)) |
| 4273 | setclientid.sc_name_len = |
| 4274 | nfs4_init_uniform_client_string(clp, |
| 4275 | setclientid.sc_name, |
| 4276 | sizeof(setclientid.sc_name)); |
| 4277 | else |
| 4278 | setclientid.sc_name_len = |
| 4279 | nfs4_init_nonuniform_client_string(clp, |
| 4280 | setclientid.sc_name, |
| 4281 | sizeof(setclientid.sc_name)); |
| 4282 | /* cb_client4 */ |
| 4283 | rcu_read_lock(); |
| 4284 | setclientid.sc_netid_len = scnprintf(setclientid.sc_netid, |
| 4285 | sizeof(setclientid.sc_netid), |
| 4286 | rpc_peeraddr2str(clp->cl_rpcclient, |
| 4287 | RPC_DISPLAY_NETID)); |
| 4288 | rcu_read_unlock(); |
| 4289 | setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr, |
| 4290 | sizeof(setclientid.sc_uaddr), "%s.%u.%u", |
| 4291 | clp->cl_ipaddr, port >> 8, port & 255); |
| 4292 | |
| 4293 | dprintk("NFS call setclientid auth=%s, '%.*s'\n", |
| 4294 | clp->cl_rpcclient->cl_auth->au_ops->au_name, |
| 4295 | setclientid.sc_name_len, setclientid.sc_name); |
| 4296 | status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
| 4297 | dprintk("NFS reply setclientid: %d\n", status); |
| 4298 | return status; |
| 4299 | } |
| 4300 | |
| 4301 | /** |
| 4302 | * nfs4_proc_setclientid_confirm - Confirm client ID |
| 4303 | * @clp: state data structure |
| 4304 | * @res: result of a previous SETCLIENTID |
| 4305 | * @cred: RPC credential to use for this call |
| 4306 | * |
| 4307 | * Returns zero, a negative errno, or a negative NFS4ERR status code. |
| 4308 | */ |
| 4309 | int nfs4_proc_setclientid_confirm(struct nfs_client *clp, |
| 4310 | struct nfs4_setclientid_res *arg, |
| 4311 | struct rpc_cred *cred) |
| 4312 | { |
| 4313 | struct rpc_message msg = { |
| 4314 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM], |
| 4315 | .rpc_argp = arg, |
| 4316 | .rpc_cred = cred, |
| 4317 | }; |
| 4318 | int status; |
| 4319 | |
| 4320 | dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n", |
| 4321 | clp->cl_rpcclient->cl_auth->au_ops->au_name, |
| 4322 | clp->cl_clientid); |
| 4323 | status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
| 4324 | dprintk("NFS reply setclientid_confirm: %d\n", status); |
| 4325 | return status; |
| 4326 | } |
| 4327 | |
| 4328 | struct nfs4_delegreturndata { |
| 4329 | struct nfs4_delegreturnargs args; |
| 4330 | struct nfs4_delegreturnres res; |
| 4331 | struct nfs_fh fh; |
| 4332 | nfs4_stateid stateid; |
| 4333 | unsigned long timestamp; |
| 4334 | struct nfs_fattr fattr; |
| 4335 | int rpc_status; |
| 4336 | }; |
| 4337 | |
| 4338 | static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata) |
| 4339 | { |
| 4340 | struct nfs4_delegreturndata *data = calldata; |
| 4341 | |
| 4342 | if (!nfs4_sequence_done(task, &data->res.seq_res)) |
| 4343 | return; |
| 4344 | |
| 4345 | switch (task->tk_status) { |
| 4346 | case -NFS4ERR_STALE_STATEID: |
| 4347 | case -NFS4ERR_EXPIRED: |
| 4348 | case 0: |
| 4349 | renew_lease(data->res.server, data->timestamp); |
| 4350 | break; |
| 4351 | default: |
| 4352 | if (nfs4_async_handle_error(task, data->res.server, NULL) == |
| 4353 | -EAGAIN) { |
| 4354 | rpc_restart_call_prepare(task); |
| 4355 | return; |
| 4356 | } |
| 4357 | } |
| 4358 | data->rpc_status = task->tk_status; |
| 4359 | } |
| 4360 | |
| 4361 | static void nfs4_delegreturn_release(void *calldata) |
| 4362 | { |
| 4363 | kfree(calldata); |
| 4364 | } |
| 4365 | |
| 4366 | #if defined(CONFIG_NFS_V4_1) |
| 4367 | static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data) |
| 4368 | { |
| 4369 | struct nfs4_delegreturndata *d_data; |
| 4370 | |
| 4371 | d_data = (struct nfs4_delegreturndata *)data; |
| 4372 | |
| 4373 | nfs4_setup_sequence(d_data->res.server, |
| 4374 | &d_data->args.seq_args, |
| 4375 | &d_data->res.seq_res, |
| 4376 | task); |
| 4377 | } |
| 4378 | #endif /* CONFIG_NFS_V4_1 */ |
| 4379 | |
| 4380 | static const struct rpc_call_ops nfs4_delegreturn_ops = { |
| 4381 | #if defined(CONFIG_NFS_V4_1) |
| 4382 | .rpc_call_prepare = nfs4_delegreturn_prepare, |
| 4383 | #endif /* CONFIG_NFS_V4_1 */ |
| 4384 | .rpc_call_done = nfs4_delegreturn_done, |
| 4385 | .rpc_release = nfs4_delegreturn_release, |
| 4386 | }; |
| 4387 | |
| 4388 | static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync) |
| 4389 | { |
| 4390 | struct nfs4_delegreturndata *data; |
| 4391 | struct nfs_server *server = NFS_SERVER(inode); |
| 4392 | struct rpc_task *task; |
| 4393 | struct rpc_message msg = { |
| 4394 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN], |
| 4395 | .rpc_cred = cred, |
| 4396 | }; |
| 4397 | struct rpc_task_setup task_setup_data = { |
| 4398 | .rpc_client = server->client, |
| 4399 | .rpc_message = &msg, |
| 4400 | .callback_ops = &nfs4_delegreturn_ops, |
| 4401 | .flags = RPC_TASK_ASYNC, |
| 4402 | }; |
| 4403 | int status = 0; |
| 4404 | |
| 4405 | data = kzalloc(sizeof(*data), GFP_NOFS); |
| 4406 | if (data == NULL) |
| 4407 | return -ENOMEM; |
| 4408 | nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1); |
| 4409 | data->args.fhandle = &data->fh; |
| 4410 | data->args.stateid = &data->stateid; |
| 4411 | data->args.bitmask = server->cache_consistency_bitmask; |
| 4412 | nfs_copy_fh(&data->fh, NFS_FH(inode)); |
| 4413 | nfs4_stateid_copy(&data->stateid, stateid); |
| 4414 | data->res.fattr = &data->fattr; |
| 4415 | data->res.server = server; |
| 4416 | nfs_fattr_init(data->res.fattr); |
| 4417 | data->timestamp = jiffies; |
| 4418 | data->rpc_status = 0; |
| 4419 | |
| 4420 | task_setup_data.callback_data = data; |
| 4421 | msg.rpc_argp = &data->args; |
| 4422 | msg.rpc_resp = &data->res; |
| 4423 | task = rpc_run_task(&task_setup_data); |
| 4424 | if (IS_ERR(task)) |
| 4425 | return PTR_ERR(task); |
| 4426 | if (!issync) |
| 4427 | goto out; |
| 4428 | status = nfs4_wait_for_completion_rpc_task(task); |
| 4429 | if (status != 0) |
| 4430 | goto out; |
| 4431 | status = data->rpc_status; |
| 4432 | if (status == 0) |
| 4433 | nfs_post_op_update_inode_force_wcc(inode, &data->fattr); |
| 4434 | else |
| 4435 | nfs_refresh_inode(inode, &data->fattr); |
| 4436 | out: |
| 4437 | rpc_put_task(task); |
| 4438 | return status; |
| 4439 | } |
| 4440 | |
| 4441 | int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync) |
| 4442 | { |
| 4443 | struct nfs_server *server = NFS_SERVER(inode); |
| 4444 | struct nfs4_exception exception = { }; |
| 4445 | int err; |
| 4446 | do { |
| 4447 | err = _nfs4_proc_delegreturn(inode, cred, stateid, issync); |
| 4448 | switch (err) { |
| 4449 | case -NFS4ERR_STALE_STATEID: |
| 4450 | case -NFS4ERR_EXPIRED: |
| 4451 | case 0: |
| 4452 | return 0; |
| 4453 | } |
| 4454 | err = nfs4_handle_exception(server, err, &exception); |
| 4455 | } while (exception.retry); |
| 4456 | return err; |
| 4457 | } |
| 4458 | |
| 4459 | #define NFS4_LOCK_MINTIMEOUT (1 * HZ) |
| 4460 | #define NFS4_LOCK_MAXTIMEOUT (30 * HZ) |
| 4461 | |
| 4462 | /* |
| 4463 | * sleep, with exponential backoff, and retry the LOCK operation. |
| 4464 | */ |
| 4465 | static unsigned long |
| 4466 | nfs4_set_lock_task_retry(unsigned long timeout) |
| 4467 | { |
| 4468 | freezable_schedule_timeout_killable(timeout); |
| 4469 | timeout <<= 1; |
| 4470 | if (timeout > NFS4_LOCK_MAXTIMEOUT) |
| 4471 | return NFS4_LOCK_MAXTIMEOUT; |
| 4472 | return timeout; |
| 4473 | } |
| 4474 | |
| 4475 | static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 4476 | { |
| 4477 | struct inode *inode = state->inode; |
| 4478 | struct nfs_server *server = NFS_SERVER(inode); |
| 4479 | struct nfs_client *clp = server->nfs_client; |
| 4480 | struct nfs_lockt_args arg = { |
| 4481 | .fh = NFS_FH(inode), |
| 4482 | .fl = request, |
| 4483 | }; |
| 4484 | struct nfs_lockt_res res = { |
| 4485 | .denied = request, |
| 4486 | }; |
| 4487 | struct rpc_message msg = { |
| 4488 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT], |
| 4489 | .rpc_argp = &arg, |
| 4490 | .rpc_resp = &res, |
| 4491 | .rpc_cred = state->owner->so_cred, |
| 4492 | }; |
| 4493 | struct nfs4_lock_state *lsp; |
| 4494 | int status; |
| 4495 | |
| 4496 | arg.lock_owner.clientid = clp->cl_clientid; |
| 4497 | status = nfs4_set_lock_state(state, request); |
| 4498 | if (status != 0) |
| 4499 | goto out; |
| 4500 | lsp = request->fl_u.nfs4_fl.owner; |
| 4501 | arg.lock_owner.id = lsp->ls_seqid.owner_id; |
| 4502 | arg.lock_owner.s_dev = server->s_dev; |
| 4503 | status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1); |
| 4504 | switch (status) { |
| 4505 | case 0: |
| 4506 | request->fl_type = F_UNLCK; |
| 4507 | break; |
| 4508 | case -NFS4ERR_DENIED: |
| 4509 | status = 0; |
| 4510 | } |
| 4511 | request->fl_ops->fl_release_private(request); |
| 4512 | out: |
| 4513 | return status; |
| 4514 | } |
| 4515 | |
| 4516 | static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 4517 | { |
| 4518 | struct nfs4_exception exception = { }; |
| 4519 | int err; |
| 4520 | |
| 4521 | do { |
| 4522 | err = nfs4_handle_exception(NFS_SERVER(state->inode), |
| 4523 | _nfs4_proc_getlk(state, cmd, request), |
| 4524 | &exception); |
| 4525 | } while (exception.retry); |
| 4526 | return err; |
| 4527 | } |
| 4528 | |
| 4529 | static int do_vfs_lock(struct file *file, struct file_lock *fl) |
| 4530 | { |
| 4531 | int res = 0; |
| 4532 | switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { |
| 4533 | case FL_POSIX: |
| 4534 | res = posix_lock_file_wait(file, fl); |
| 4535 | break; |
| 4536 | case FL_FLOCK: |
| 4537 | res = flock_lock_file_wait(file, fl); |
| 4538 | break; |
| 4539 | default: |
| 4540 | BUG(); |
| 4541 | } |
| 4542 | return res; |
| 4543 | } |
| 4544 | |
| 4545 | struct nfs4_unlockdata { |
| 4546 | struct nfs_locku_args arg; |
| 4547 | struct nfs_locku_res res; |
| 4548 | struct nfs4_lock_state *lsp; |
| 4549 | struct nfs_open_context *ctx; |
| 4550 | struct file_lock fl; |
| 4551 | const struct nfs_server *server; |
| 4552 | unsigned long timestamp; |
| 4553 | }; |
| 4554 | |
| 4555 | static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl, |
| 4556 | struct nfs_open_context *ctx, |
| 4557 | struct nfs4_lock_state *lsp, |
| 4558 | struct nfs_seqid *seqid) |
| 4559 | { |
| 4560 | struct nfs4_unlockdata *p; |
| 4561 | struct inode *inode = lsp->ls_state->inode; |
| 4562 | |
| 4563 | p = kzalloc(sizeof(*p), GFP_NOFS); |
| 4564 | if (p == NULL) |
| 4565 | return NULL; |
| 4566 | p->arg.fh = NFS_FH(inode); |
| 4567 | p->arg.fl = &p->fl; |
| 4568 | p->arg.seqid = seqid; |
| 4569 | p->res.seqid = seqid; |
| 4570 | p->arg.stateid = &lsp->ls_stateid; |
| 4571 | p->lsp = lsp; |
| 4572 | atomic_inc(&lsp->ls_count); |
| 4573 | /* Ensure we don't close file until we're done freeing locks! */ |
| 4574 | p->ctx = get_nfs_open_context(ctx); |
| 4575 | memcpy(&p->fl, fl, sizeof(p->fl)); |
| 4576 | p->server = NFS_SERVER(inode); |
| 4577 | return p; |
| 4578 | } |
| 4579 | |
| 4580 | static void nfs4_locku_release_calldata(void *data) |
| 4581 | { |
| 4582 | struct nfs4_unlockdata *calldata = data; |
| 4583 | nfs_free_seqid(calldata->arg.seqid); |
| 4584 | nfs4_put_lock_state(calldata->lsp); |
| 4585 | put_nfs_open_context(calldata->ctx); |
| 4586 | kfree(calldata); |
| 4587 | } |
| 4588 | |
| 4589 | static void nfs4_locku_done(struct rpc_task *task, void *data) |
| 4590 | { |
| 4591 | struct nfs4_unlockdata *calldata = data; |
| 4592 | |
| 4593 | if (!nfs4_sequence_done(task, &calldata->res.seq_res)) |
| 4594 | return; |
| 4595 | switch (task->tk_status) { |
| 4596 | case 0: |
| 4597 | nfs4_stateid_copy(&calldata->lsp->ls_stateid, |
| 4598 | &calldata->res.stateid); |
| 4599 | renew_lease(calldata->server, calldata->timestamp); |
| 4600 | break; |
| 4601 | case -NFS4ERR_BAD_STATEID: |
| 4602 | case -NFS4ERR_OLD_STATEID: |
| 4603 | case -NFS4ERR_STALE_STATEID: |
| 4604 | case -NFS4ERR_EXPIRED: |
| 4605 | break; |
| 4606 | default: |
| 4607 | if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN) |
| 4608 | rpc_restart_call_prepare(task); |
| 4609 | } |
| 4610 | nfs_release_seqid(calldata->arg.seqid); |
| 4611 | } |
| 4612 | |
| 4613 | static void nfs4_locku_prepare(struct rpc_task *task, void *data) |
| 4614 | { |
| 4615 | struct nfs4_unlockdata *calldata = data; |
| 4616 | |
| 4617 | if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) |
| 4618 | goto out_wait; |
| 4619 | if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) { |
| 4620 | /* Note: exit _without_ running nfs4_locku_done */ |
| 4621 | goto out_no_action; |
| 4622 | } |
| 4623 | calldata->timestamp = jiffies; |
| 4624 | if (nfs4_setup_sequence(calldata->server, |
| 4625 | &calldata->arg.seq_args, |
| 4626 | &calldata->res.seq_res, |
| 4627 | task) != 0) |
| 4628 | nfs_release_seqid(calldata->arg.seqid); |
| 4629 | return; |
| 4630 | out_no_action: |
| 4631 | task->tk_action = NULL; |
| 4632 | out_wait: |
| 4633 | nfs4_sequence_done(task, &calldata->res.seq_res); |
| 4634 | } |
| 4635 | |
| 4636 | static const struct rpc_call_ops nfs4_locku_ops = { |
| 4637 | .rpc_call_prepare = nfs4_locku_prepare, |
| 4638 | .rpc_call_done = nfs4_locku_done, |
| 4639 | .rpc_release = nfs4_locku_release_calldata, |
| 4640 | }; |
| 4641 | |
| 4642 | static struct rpc_task *nfs4_do_unlck(struct file_lock *fl, |
| 4643 | struct nfs_open_context *ctx, |
| 4644 | struct nfs4_lock_state *lsp, |
| 4645 | struct nfs_seqid *seqid) |
| 4646 | { |
| 4647 | struct nfs4_unlockdata *data; |
| 4648 | struct rpc_message msg = { |
| 4649 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU], |
| 4650 | .rpc_cred = ctx->cred, |
| 4651 | }; |
| 4652 | struct rpc_task_setup task_setup_data = { |
| 4653 | .rpc_client = NFS_CLIENT(lsp->ls_state->inode), |
| 4654 | .rpc_message = &msg, |
| 4655 | .callback_ops = &nfs4_locku_ops, |
| 4656 | .workqueue = nfsiod_workqueue, |
| 4657 | .flags = RPC_TASK_ASYNC, |
| 4658 | }; |
| 4659 | |
| 4660 | /* Ensure this is an unlock - when canceling a lock, the |
| 4661 | * canceled lock is passed in, and it won't be an unlock. |
| 4662 | */ |
| 4663 | fl->fl_type = F_UNLCK; |
| 4664 | |
| 4665 | data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid); |
| 4666 | if (data == NULL) { |
| 4667 | nfs_free_seqid(seqid); |
| 4668 | return ERR_PTR(-ENOMEM); |
| 4669 | } |
| 4670 | |
| 4671 | nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1); |
| 4672 | msg.rpc_argp = &data->arg; |
| 4673 | msg.rpc_resp = &data->res; |
| 4674 | task_setup_data.callback_data = data; |
| 4675 | return rpc_run_task(&task_setup_data); |
| 4676 | } |
| 4677 | |
| 4678 | static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 4679 | { |
| 4680 | struct inode *inode = state->inode; |
| 4681 | struct nfs4_state_owner *sp = state->owner; |
| 4682 | struct nfs_inode *nfsi = NFS_I(inode); |
| 4683 | struct nfs_seqid *seqid; |
| 4684 | struct nfs4_lock_state *lsp; |
| 4685 | struct rpc_task *task; |
| 4686 | int status = 0; |
| 4687 | unsigned char fl_flags = request->fl_flags; |
| 4688 | |
| 4689 | status = nfs4_set_lock_state(state, request); |
| 4690 | /* Unlock _before_ we do the RPC call */ |
| 4691 | request->fl_flags |= FL_EXISTS; |
| 4692 | /* Exclude nfs_delegation_claim_locks() */ |
| 4693 | mutex_lock(&sp->so_delegreturn_mutex); |
| 4694 | /* Exclude nfs4_reclaim_open_stateid() - note nesting! */ |
| 4695 | down_read(&nfsi->rwsem); |
| 4696 | if (do_vfs_lock(request->fl_file, request) == -ENOENT) { |
| 4697 | up_read(&nfsi->rwsem); |
| 4698 | mutex_unlock(&sp->so_delegreturn_mutex); |
| 4699 | goto out; |
| 4700 | } |
| 4701 | up_read(&nfsi->rwsem); |
| 4702 | mutex_unlock(&sp->so_delegreturn_mutex); |
| 4703 | if (status != 0) |
| 4704 | goto out; |
| 4705 | /* Is this a delegated lock? */ |
| 4706 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) |
| 4707 | goto out; |
| 4708 | lsp = request->fl_u.nfs4_fl.owner; |
| 4709 | seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL); |
| 4710 | status = -ENOMEM; |
| 4711 | if (seqid == NULL) |
| 4712 | goto out; |
| 4713 | task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid); |
| 4714 | status = PTR_ERR(task); |
| 4715 | if (IS_ERR(task)) |
| 4716 | goto out; |
| 4717 | status = nfs4_wait_for_completion_rpc_task(task); |
| 4718 | rpc_put_task(task); |
| 4719 | out: |
| 4720 | request->fl_flags = fl_flags; |
| 4721 | return status; |
| 4722 | } |
| 4723 | |
| 4724 | struct nfs4_lockdata { |
| 4725 | struct nfs_lock_args arg; |
| 4726 | struct nfs_lock_res res; |
| 4727 | struct nfs4_lock_state *lsp; |
| 4728 | struct nfs_open_context *ctx; |
| 4729 | struct file_lock fl; |
| 4730 | unsigned long timestamp; |
| 4731 | int rpc_status; |
| 4732 | int cancelled; |
| 4733 | struct nfs_server *server; |
| 4734 | }; |
| 4735 | |
| 4736 | static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl, |
| 4737 | struct nfs_open_context *ctx, struct nfs4_lock_state *lsp, |
| 4738 | gfp_t gfp_mask) |
| 4739 | { |
| 4740 | struct nfs4_lockdata *p; |
| 4741 | struct inode *inode = lsp->ls_state->inode; |
| 4742 | struct nfs_server *server = NFS_SERVER(inode); |
| 4743 | |
| 4744 | p = kzalloc(sizeof(*p), gfp_mask); |
| 4745 | if (p == NULL) |
| 4746 | return NULL; |
| 4747 | |
| 4748 | p->arg.fh = NFS_FH(inode); |
| 4749 | p->arg.fl = &p->fl; |
| 4750 | p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask); |
| 4751 | if (p->arg.open_seqid == NULL) |
| 4752 | goto out_free; |
| 4753 | p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask); |
| 4754 | if (p->arg.lock_seqid == NULL) |
| 4755 | goto out_free_seqid; |
| 4756 | p->arg.lock_stateid = &lsp->ls_stateid; |
| 4757 | p->arg.lock_owner.clientid = server->nfs_client->cl_clientid; |
| 4758 | p->arg.lock_owner.id = lsp->ls_seqid.owner_id; |
| 4759 | p->arg.lock_owner.s_dev = server->s_dev; |
| 4760 | p->res.lock_seqid = p->arg.lock_seqid; |
| 4761 | p->lsp = lsp; |
| 4762 | p->server = server; |
| 4763 | atomic_inc(&lsp->ls_count); |
| 4764 | p->ctx = get_nfs_open_context(ctx); |
| 4765 | memcpy(&p->fl, fl, sizeof(p->fl)); |
| 4766 | return p; |
| 4767 | out_free_seqid: |
| 4768 | nfs_free_seqid(p->arg.open_seqid); |
| 4769 | out_free: |
| 4770 | kfree(p); |
| 4771 | return NULL; |
| 4772 | } |
| 4773 | |
| 4774 | static void nfs4_lock_prepare(struct rpc_task *task, void *calldata) |
| 4775 | { |
| 4776 | struct nfs4_lockdata *data = calldata; |
| 4777 | struct nfs4_state *state = data->lsp->ls_state; |
| 4778 | |
| 4779 | dprintk("%s: begin!\n", __func__); |
| 4780 | if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0) |
| 4781 | goto out_wait; |
| 4782 | /* Do we need to do an open_to_lock_owner? */ |
| 4783 | if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) { |
| 4784 | if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) { |
| 4785 | goto out_release_lock_seqid; |
| 4786 | } |
| 4787 | data->arg.open_stateid = &state->stateid; |
| 4788 | data->arg.new_lock_owner = 1; |
| 4789 | data->res.open_seqid = data->arg.open_seqid; |
| 4790 | } else |
| 4791 | data->arg.new_lock_owner = 0; |
| 4792 | if (!nfs4_valid_open_stateid(state)) { |
| 4793 | data->rpc_status = -EBADF; |
| 4794 | task->tk_action = NULL; |
| 4795 | goto out_release_open_seqid; |
| 4796 | } |
| 4797 | data->timestamp = jiffies; |
| 4798 | if (nfs4_setup_sequence(data->server, |
| 4799 | &data->arg.seq_args, |
| 4800 | &data->res.seq_res, |
| 4801 | task) == 0) |
| 4802 | return; |
| 4803 | out_release_open_seqid: |
| 4804 | nfs_release_seqid(data->arg.open_seqid); |
| 4805 | out_release_lock_seqid: |
| 4806 | nfs_release_seqid(data->arg.lock_seqid); |
| 4807 | out_wait: |
| 4808 | nfs4_sequence_done(task, &data->res.seq_res); |
| 4809 | dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status); |
| 4810 | } |
| 4811 | |
| 4812 | static void nfs4_lock_done(struct rpc_task *task, void *calldata) |
| 4813 | { |
| 4814 | struct nfs4_lockdata *data = calldata; |
| 4815 | |
| 4816 | dprintk("%s: begin!\n", __func__); |
| 4817 | |
| 4818 | if (!nfs4_sequence_done(task, &data->res.seq_res)) |
| 4819 | return; |
| 4820 | |
| 4821 | data->rpc_status = task->tk_status; |
| 4822 | if (data->arg.new_lock_owner != 0) { |
| 4823 | if (data->rpc_status == 0) |
| 4824 | nfs_confirm_seqid(&data->lsp->ls_seqid, 0); |
| 4825 | else |
| 4826 | goto out; |
| 4827 | } |
| 4828 | if (data->rpc_status == 0) { |
| 4829 | nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid); |
| 4830 | set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags); |
| 4831 | renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp); |
| 4832 | } |
| 4833 | out: |
| 4834 | dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status); |
| 4835 | } |
| 4836 | |
| 4837 | static void nfs4_lock_release(void *calldata) |
| 4838 | { |
| 4839 | struct nfs4_lockdata *data = calldata; |
| 4840 | |
| 4841 | dprintk("%s: begin!\n", __func__); |
| 4842 | nfs_free_seqid(data->arg.open_seqid); |
| 4843 | if (data->cancelled != 0) { |
| 4844 | struct rpc_task *task; |
| 4845 | task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp, |
| 4846 | data->arg.lock_seqid); |
| 4847 | if (!IS_ERR(task)) |
| 4848 | rpc_put_task_async(task); |
| 4849 | dprintk("%s: cancelling lock!\n", __func__); |
| 4850 | } else |
| 4851 | nfs_free_seqid(data->arg.lock_seqid); |
| 4852 | nfs4_put_lock_state(data->lsp); |
| 4853 | put_nfs_open_context(data->ctx); |
| 4854 | kfree(data); |
| 4855 | dprintk("%s: done!\n", __func__); |
| 4856 | } |
| 4857 | |
| 4858 | static const struct rpc_call_ops nfs4_lock_ops = { |
| 4859 | .rpc_call_prepare = nfs4_lock_prepare, |
| 4860 | .rpc_call_done = nfs4_lock_done, |
| 4861 | .rpc_release = nfs4_lock_release, |
| 4862 | }; |
| 4863 | |
| 4864 | static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error) |
| 4865 | { |
| 4866 | switch (error) { |
| 4867 | case -NFS4ERR_ADMIN_REVOKED: |
| 4868 | case -NFS4ERR_BAD_STATEID: |
| 4869 | lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; |
| 4870 | if (new_lock_owner != 0 || |
| 4871 | test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) |
| 4872 | nfs4_schedule_stateid_recovery(server, lsp->ls_state); |
| 4873 | break; |
| 4874 | case -NFS4ERR_STALE_STATEID: |
| 4875 | lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED; |
| 4876 | case -NFS4ERR_EXPIRED: |
| 4877 | nfs4_schedule_lease_recovery(server->nfs_client); |
| 4878 | }; |
| 4879 | } |
| 4880 | |
| 4881 | static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type) |
| 4882 | { |
| 4883 | struct nfs4_lockdata *data; |
| 4884 | struct rpc_task *task; |
| 4885 | struct rpc_message msg = { |
| 4886 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK], |
| 4887 | .rpc_cred = state->owner->so_cred, |
| 4888 | }; |
| 4889 | struct rpc_task_setup task_setup_data = { |
| 4890 | .rpc_client = NFS_CLIENT(state->inode), |
| 4891 | .rpc_message = &msg, |
| 4892 | .callback_ops = &nfs4_lock_ops, |
| 4893 | .workqueue = nfsiod_workqueue, |
| 4894 | .flags = RPC_TASK_ASYNC, |
| 4895 | }; |
| 4896 | int ret; |
| 4897 | |
| 4898 | dprintk("%s: begin!\n", __func__); |
| 4899 | data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file), |
| 4900 | fl->fl_u.nfs4_fl.owner, |
| 4901 | recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS); |
| 4902 | if (data == NULL) |
| 4903 | return -ENOMEM; |
| 4904 | if (IS_SETLKW(cmd)) |
| 4905 | data->arg.block = 1; |
| 4906 | nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1); |
| 4907 | msg.rpc_argp = &data->arg; |
| 4908 | msg.rpc_resp = &data->res; |
| 4909 | task_setup_data.callback_data = data; |
| 4910 | if (recovery_type > NFS_LOCK_NEW) { |
| 4911 | if (recovery_type == NFS_LOCK_RECLAIM) |
| 4912 | data->arg.reclaim = NFS_LOCK_RECLAIM; |
| 4913 | nfs4_set_sequence_privileged(&data->arg.seq_args); |
| 4914 | } |
| 4915 | task = rpc_run_task(&task_setup_data); |
| 4916 | if (IS_ERR(task)) |
| 4917 | return PTR_ERR(task); |
| 4918 | ret = nfs4_wait_for_completion_rpc_task(task); |
| 4919 | if (ret == 0) { |
| 4920 | ret = data->rpc_status; |
| 4921 | if (ret) |
| 4922 | nfs4_handle_setlk_error(data->server, data->lsp, |
| 4923 | data->arg.new_lock_owner, ret); |
| 4924 | } else |
| 4925 | data->cancelled = 1; |
| 4926 | rpc_put_task(task); |
| 4927 | dprintk("%s: done, ret = %d!\n", __func__, ret); |
| 4928 | return ret; |
| 4929 | } |
| 4930 | |
| 4931 | static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request) |
| 4932 | { |
| 4933 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 4934 | struct nfs4_exception exception = { |
| 4935 | .inode = state->inode, |
| 4936 | }; |
| 4937 | int err; |
| 4938 | |
| 4939 | do { |
| 4940 | /* Cache the lock if possible... */ |
| 4941 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) |
| 4942 | return 0; |
| 4943 | err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM); |
| 4944 | if (err != -NFS4ERR_DELAY) |
| 4945 | break; |
| 4946 | nfs4_handle_exception(server, err, &exception); |
| 4947 | } while (exception.retry); |
| 4948 | return err; |
| 4949 | } |
| 4950 | |
| 4951 | static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request) |
| 4952 | { |
| 4953 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 4954 | struct nfs4_exception exception = { |
| 4955 | .inode = state->inode, |
| 4956 | }; |
| 4957 | int err; |
| 4958 | |
| 4959 | err = nfs4_set_lock_state(state, request); |
| 4960 | if (err != 0) |
| 4961 | return err; |
| 4962 | do { |
| 4963 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) |
| 4964 | return 0; |
| 4965 | err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED); |
| 4966 | switch (err) { |
| 4967 | default: |
| 4968 | goto out; |
| 4969 | case -NFS4ERR_GRACE: |
| 4970 | case -NFS4ERR_DELAY: |
| 4971 | nfs4_handle_exception(server, err, &exception); |
| 4972 | err = 0; |
| 4973 | } |
| 4974 | } while (exception.retry); |
| 4975 | out: |
| 4976 | return err; |
| 4977 | } |
| 4978 | |
| 4979 | #if defined(CONFIG_NFS_V4_1) |
| 4980 | /** |
| 4981 | * nfs41_check_expired_locks - possibly free a lock stateid |
| 4982 | * |
| 4983 | * @state: NFSv4 state for an inode |
| 4984 | * |
| 4985 | * Returns NFS_OK if recovery for this stateid is now finished. |
| 4986 | * Otherwise a negative NFS4ERR value is returned. |
| 4987 | */ |
| 4988 | static int nfs41_check_expired_locks(struct nfs4_state *state) |
| 4989 | { |
| 4990 | int status, ret = -NFS4ERR_BAD_STATEID; |
| 4991 | struct nfs4_lock_state *lsp; |
| 4992 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 4993 | |
| 4994 | list_for_each_entry(lsp, &state->lock_states, ls_locks) { |
| 4995 | if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) { |
| 4996 | status = nfs41_test_stateid(server, &lsp->ls_stateid); |
| 4997 | if (status != NFS_OK) { |
| 4998 | /* Free the stateid unless the server |
| 4999 | * informs us the stateid is unrecognized. */ |
| 5000 | if (status != -NFS4ERR_BAD_STATEID) |
| 5001 | nfs41_free_stateid(server, |
| 5002 | &lsp->ls_stateid); |
| 5003 | clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags); |
| 5004 | ret = status; |
| 5005 | } |
| 5006 | } |
| 5007 | }; |
| 5008 | |
| 5009 | return ret; |
| 5010 | } |
| 5011 | |
| 5012 | static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request) |
| 5013 | { |
| 5014 | int status = NFS_OK; |
| 5015 | |
| 5016 | if (test_bit(LK_STATE_IN_USE, &state->flags)) |
| 5017 | status = nfs41_check_expired_locks(state); |
| 5018 | if (status != NFS_OK) |
| 5019 | status = nfs4_lock_expired(state, request); |
| 5020 | return status; |
| 5021 | } |
| 5022 | #endif |
| 5023 | |
| 5024 | static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 5025 | { |
| 5026 | struct nfs4_state_owner *sp = state->owner; |
| 5027 | struct nfs_inode *nfsi = NFS_I(state->inode); |
| 5028 | unsigned char fl_flags = request->fl_flags; |
| 5029 | unsigned int seq; |
| 5030 | int status = -ENOLCK; |
| 5031 | |
| 5032 | if ((fl_flags & FL_POSIX) && |
| 5033 | !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags)) |
| 5034 | goto out; |
| 5035 | /* Is this a delegated open? */ |
| 5036 | status = nfs4_set_lock_state(state, request); |
| 5037 | if (status != 0) |
| 5038 | goto out; |
| 5039 | request->fl_flags |= FL_ACCESS; |
| 5040 | status = do_vfs_lock(request->fl_file, request); |
| 5041 | if (status < 0) |
| 5042 | goto out; |
| 5043 | down_read(&nfsi->rwsem); |
| 5044 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { |
| 5045 | /* Yes: cache locks! */ |
| 5046 | /* ...but avoid races with delegation recall... */ |
| 5047 | request->fl_flags = fl_flags & ~FL_SLEEP; |
| 5048 | status = do_vfs_lock(request->fl_file, request); |
| 5049 | goto out_unlock; |
| 5050 | } |
| 5051 | seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); |
| 5052 | up_read(&nfsi->rwsem); |
| 5053 | status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW); |
| 5054 | if (status != 0) |
| 5055 | goto out; |
| 5056 | down_read(&nfsi->rwsem); |
| 5057 | if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) { |
| 5058 | status = -NFS4ERR_DELAY; |
| 5059 | goto out_unlock; |
| 5060 | } |
| 5061 | /* Note: we always want to sleep here! */ |
| 5062 | request->fl_flags = fl_flags | FL_SLEEP; |
| 5063 | if (do_vfs_lock(request->fl_file, request) < 0) |
| 5064 | printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock " |
| 5065 | "manager!\n", __func__); |
| 5066 | out_unlock: |
| 5067 | up_read(&nfsi->rwsem); |
| 5068 | out: |
| 5069 | request->fl_flags = fl_flags; |
| 5070 | return status; |
| 5071 | } |
| 5072 | |
| 5073 | static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
| 5074 | { |
| 5075 | struct nfs4_exception exception = { |
| 5076 | .state = state, |
| 5077 | .inode = state->inode, |
| 5078 | }; |
| 5079 | int err; |
| 5080 | |
| 5081 | do { |
| 5082 | err = _nfs4_proc_setlk(state, cmd, request); |
| 5083 | if (err == -NFS4ERR_DENIED) |
| 5084 | err = -EAGAIN; |
| 5085 | err = nfs4_handle_exception(NFS_SERVER(state->inode), |
| 5086 | err, &exception); |
| 5087 | } while (exception.retry); |
| 5088 | return err; |
| 5089 | } |
| 5090 | |
| 5091 | static int |
| 5092 | nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request) |
| 5093 | { |
| 5094 | struct nfs_open_context *ctx; |
| 5095 | struct nfs4_state *state; |
| 5096 | unsigned long timeout = NFS4_LOCK_MINTIMEOUT; |
| 5097 | int status; |
| 5098 | |
| 5099 | /* verify open state */ |
| 5100 | ctx = nfs_file_open_context(filp); |
| 5101 | state = ctx->state; |
| 5102 | |
| 5103 | if (request->fl_start < 0 || request->fl_end < 0) |
| 5104 | return -EINVAL; |
| 5105 | |
| 5106 | if (IS_GETLK(cmd)) { |
| 5107 | if (state != NULL) |
| 5108 | return nfs4_proc_getlk(state, F_GETLK, request); |
| 5109 | return 0; |
| 5110 | } |
| 5111 | |
| 5112 | if (!(IS_SETLK(cmd) || IS_SETLKW(cmd))) |
| 5113 | return -EINVAL; |
| 5114 | |
| 5115 | if (request->fl_type == F_UNLCK) { |
| 5116 | if (state != NULL) |
| 5117 | return nfs4_proc_unlck(state, cmd, request); |
| 5118 | return 0; |
| 5119 | } |
| 5120 | |
| 5121 | if (state == NULL) |
| 5122 | return -ENOLCK; |
| 5123 | /* |
| 5124 | * Don't rely on the VFS having checked the file open mode, |
| 5125 | * since it won't do this for flock() locks. |
| 5126 | */ |
| 5127 | switch (request->fl_type) { |
| 5128 | case F_RDLCK: |
| 5129 | if (!(filp->f_mode & FMODE_READ)) |
| 5130 | return -EBADF; |
| 5131 | break; |
| 5132 | case F_WRLCK: |
| 5133 | if (!(filp->f_mode & FMODE_WRITE)) |
| 5134 | return -EBADF; |
| 5135 | } |
| 5136 | |
| 5137 | do { |
| 5138 | status = nfs4_proc_setlk(state, cmd, request); |
| 5139 | if ((status != -EAGAIN) || IS_SETLK(cmd)) |
| 5140 | break; |
| 5141 | timeout = nfs4_set_lock_task_retry(timeout); |
| 5142 | status = -ERESTARTSYS; |
| 5143 | if (signalled()) |
| 5144 | break; |
| 5145 | } while(status < 0); |
| 5146 | return status; |
| 5147 | } |
| 5148 | |
| 5149 | int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl) |
| 5150 | { |
| 5151 | struct nfs_server *server = NFS_SERVER(state->inode); |
| 5152 | struct nfs4_exception exception = { }; |
| 5153 | int err; |
| 5154 | |
| 5155 | err = nfs4_set_lock_state(state, fl); |
| 5156 | if (err != 0) |
| 5157 | goto out; |
| 5158 | do { |
| 5159 | err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW); |
| 5160 | switch (err) { |
| 5161 | default: |
| 5162 | printk(KERN_ERR "NFS: %s: unhandled error " |
| 5163 | "%d.\n", __func__, err); |
| 5164 | case 0: |
| 5165 | case -ESTALE: |
| 5166 | goto out; |
| 5167 | case -NFS4ERR_STALE_CLIENTID: |
| 5168 | case -NFS4ERR_STALE_STATEID: |
| 5169 | set_bit(NFS_DELEGATED_STATE, &state->flags); |
| 5170 | case -NFS4ERR_EXPIRED: |
| 5171 | nfs4_schedule_lease_recovery(server->nfs_client); |
| 5172 | err = -EAGAIN; |
| 5173 | goto out; |
| 5174 | case -NFS4ERR_BADSESSION: |
| 5175 | case -NFS4ERR_BADSLOT: |
| 5176 | case -NFS4ERR_BAD_HIGH_SLOT: |
| 5177 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: |
| 5178 | case -NFS4ERR_DEADSESSION: |
| 5179 | set_bit(NFS_DELEGATED_STATE, &state->flags); |
| 5180 | nfs4_schedule_session_recovery(server->nfs_client->cl_session, err); |
| 5181 | err = -EAGAIN; |
| 5182 | goto out; |
| 5183 | case -NFS4ERR_DELEG_REVOKED: |
| 5184 | case -NFS4ERR_ADMIN_REVOKED: |
| 5185 | case -NFS4ERR_BAD_STATEID: |
| 5186 | case -NFS4ERR_OPENMODE: |
| 5187 | nfs4_schedule_stateid_recovery(server, state); |
| 5188 | err = 0; |
| 5189 | goto out; |
| 5190 | case -ENOMEM: |
| 5191 | case -NFS4ERR_DENIED: |
| 5192 | /* kill_proc(fl->fl_pid, SIGLOST, 1); */ |
| 5193 | err = 0; |
| 5194 | goto out; |
| 5195 | } |
| 5196 | set_bit(NFS_DELEGATED_STATE, &state->flags); |
| 5197 | err = nfs4_handle_exception(server, err, &exception); |
| 5198 | } while (exception.retry); |
| 5199 | out: |
| 5200 | return err; |
| 5201 | } |
| 5202 | |
| 5203 | struct nfs_release_lockowner_data { |
| 5204 | struct nfs4_lock_state *lsp; |
| 5205 | struct nfs_server *server; |
| 5206 | struct nfs_release_lockowner_args args; |
| 5207 | }; |
| 5208 | |
| 5209 | static void nfs4_release_lockowner_release(void *calldata) |
| 5210 | { |
| 5211 | struct nfs_release_lockowner_data *data = calldata; |
| 5212 | nfs4_free_lock_state(data->server, data->lsp); |
| 5213 | kfree(calldata); |
| 5214 | } |
| 5215 | |
| 5216 | static const struct rpc_call_ops nfs4_release_lockowner_ops = { |
| 5217 | .rpc_release = nfs4_release_lockowner_release, |
| 5218 | }; |
| 5219 | |
| 5220 | int nfs4_release_lockowner(struct nfs4_lock_state *lsp) |
| 5221 | { |
| 5222 | struct nfs_server *server = lsp->ls_state->owner->so_server; |
| 5223 | struct nfs_release_lockowner_data *data; |
| 5224 | struct rpc_message msg = { |
| 5225 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER], |
| 5226 | }; |
| 5227 | |
| 5228 | if (server->nfs_client->cl_mvops->minor_version != 0) |
| 5229 | return -EINVAL; |
| 5230 | data = kmalloc(sizeof(*data), GFP_NOFS); |
| 5231 | if (!data) |
| 5232 | return -ENOMEM; |
| 5233 | data->lsp = lsp; |
| 5234 | data->server = server; |
| 5235 | data->args.lock_owner.clientid = server->nfs_client->cl_clientid; |
| 5236 | data->args.lock_owner.id = lsp->ls_seqid.owner_id; |
| 5237 | data->args.lock_owner.s_dev = server->s_dev; |
| 5238 | msg.rpc_argp = &data->args; |
| 5239 | rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data); |
| 5240 | return 0; |
| 5241 | } |
| 5242 | |
| 5243 | #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl" |
| 5244 | |
| 5245 | static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key, |
| 5246 | const void *buf, size_t buflen, |
| 5247 | int flags, int type) |
| 5248 | { |
| 5249 | if (strcmp(key, "") != 0) |
| 5250 | return -EINVAL; |
| 5251 | |
| 5252 | return nfs4_proc_set_acl(dentry->d_inode, buf, buflen); |
| 5253 | } |
| 5254 | |
| 5255 | static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key, |
| 5256 | void *buf, size_t buflen, int type) |
| 5257 | { |
| 5258 | if (strcmp(key, "") != 0) |
| 5259 | return -EINVAL; |
| 5260 | |
| 5261 | return nfs4_proc_get_acl(dentry->d_inode, buf, buflen); |
| 5262 | } |
| 5263 | |
| 5264 | static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list, |
| 5265 | size_t list_len, const char *name, |
| 5266 | size_t name_len, int type) |
| 5267 | { |
| 5268 | size_t len = sizeof(XATTR_NAME_NFSV4_ACL); |
| 5269 | |
| 5270 | if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode))) |
| 5271 | return 0; |
| 5272 | |
| 5273 | if (list && len <= list_len) |
| 5274 | memcpy(list, XATTR_NAME_NFSV4_ACL, len); |
| 5275 | return len; |
| 5276 | } |
| 5277 | |
| 5278 | /* |
| 5279 | * nfs_fhget will use either the mounted_on_fileid or the fileid |
| 5280 | */ |
| 5281 | static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr) |
| 5282 | { |
| 5283 | if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) || |
| 5284 | (fattr->valid & NFS_ATTR_FATTR_FILEID)) && |
| 5285 | (fattr->valid & NFS_ATTR_FATTR_FSID) && |
| 5286 | (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS))) |
| 5287 | return; |
| 5288 | |
| 5289 | fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE | |
| 5290 | NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL; |
| 5291 | fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO; |
| 5292 | fattr->nlink = 2; |
| 5293 | } |
| 5294 | |
| 5295 | static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, |
| 5296 | const struct qstr *name, |
| 5297 | struct nfs4_fs_locations *fs_locations, |
| 5298 | struct page *page) |
| 5299 | { |
| 5300 | struct nfs_server *server = NFS_SERVER(dir); |
| 5301 | u32 bitmask[2] = { |
| 5302 | [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, |
| 5303 | }; |
| 5304 | struct nfs4_fs_locations_arg args = { |
| 5305 | .dir_fh = NFS_FH(dir), |
| 5306 | .name = name, |
| 5307 | .page = page, |
| 5308 | .bitmask = bitmask, |
| 5309 | }; |
| 5310 | struct nfs4_fs_locations_res res = { |
| 5311 | .fs_locations = fs_locations, |
| 5312 | }; |
| 5313 | struct rpc_message msg = { |
| 5314 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], |
| 5315 | .rpc_argp = &args, |
| 5316 | .rpc_resp = &res, |
| 5317 | }; |
| 5318 | int status; |
| 5319 | |
| 5320 | dprintk("%s: start\n", __func__); |
| 5321 | |
| 5322 | /* Ask for the fileid of the absent filesystem if mounted_on_fileid |
| 5323 | * is not supported */ |
| 5324 | if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID) |
| 5325 | bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID; |
| 5326 | else |
| 5327 | bitmask[0] |= FATTR4_WORD0_FILEID; |
| 5328 | |
| 5329 | nfs_fattr_init(&fs_locations->fattr); |
| 5330 | fs_locations->server = server; |
| 5331 | fs_locations->nlocations = 0; |
| 5332 | status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0); |
| 5333 | dprintk("%s: returned status = %d\n", __func__, status); |
| 5334 | return status; |
| 5335 | } |
| 5336 | |
| 5337 | int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir, |
| 5338 | const struct qstr *name, |
| 5339 | struct nfs4_fs_locations *fs_locations, |
| 5340 | struct page *page) |
| 5341 | { |
| 5342 | struct nfs4_exception exception = { }; |
| 5343 | int err; |
| 5344 | do { |
| 5345 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 5346 | _nfs4_proc_fs_locations(client, dir, name, fs_locations, page), |
| 5347 | &exception); |
| 5348 | } while (exception.retry); |
| 5349 | return err; |
| 5350 | } |
| 5351 | |
| 5352 | static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors) |
| 5353 | { |
| 5354 | int status; |
| 5355 | struct nfs4_secinfo_arg args = { |
| 5356 | .dir_fh = NFS_FH(dir), |
| 5357 | .name = name, |
| 5358 | }; |
| 5359 | struct nfs4_secinfo_res res = { |
| 5360 | .flavors = flavors, |
| 5361 | }; |
| 5362 | struct rpc_message msg = { |
| 5363 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO], |
| 5364 | .rpc_argp = &args, |
| 5365 | .rpc_resp = &res, |
| 5366 | }; |
| 5367 | |
| 5368 | dprintk("NFS call secinfo %s\n", name->name); |
| 5369 | status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0); |
| 5370 | dprintk("NFS reply secinfo: %d\n", status); |
| 5371 | return status; |
| 5372 | } |
| 5373 | |
| 5374 | int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, |
| 5375 | struct nfs4_secinfo_flavors *flavors) |
| 5376 | { |
| 5377 | struct nfs4_exception exception = { }; |
| 5378 | int err; |
| 5379 | do { |
| 5380 | err = nfs4_handle_exception(NFS_SERVER(dir), |
| 5381 | _nfs4_proc_secinfo(dir, name, flavors), |
| 5382 | &exception); |
| 5383 | } while (exception.retry); |
| 5384 | return err; |
| 5385 | } |
| 5386 | |
| 5387 | #ifdef CONFIG_NFS_V4_1 |
| 5388 | /* |
| 5389 | * Check the exchange flags returned by the server for invalid flags, having |
| 5390 | * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or |
| 5391 | * DS flags set. |
| 5392 | */ |
| 5393 | static int nfs4_check_cl_exchange_flags(u32 flags) |
| 5394 | { |
| 5395 | if (flags & ~EXCHGID4_FLAG_MASK_R) |
| 5396 | goto out_inval; |
| 5397 | if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) && |
| 5398 | (flags & EXCHGID4_FLAG_USE_NON_PNFS)) |
| 5399 | goto out_inval; |
| 5400 | if (!(flags & (EXCHGID4_FLAG_MASK_PNFS))) |
| 5401 | goto out_inval; |
| 5402 | return NFS_OK; |
| 5403 | out_inval: |
| 5404 | return -NFS4ERR_INVAL; |
| 5405 | } |
| 5406 | |
| 5407 | static bool |
| 5408 | nfs41_same_server_scope(struct nfs41_server_scope *a, |
| 5409 | struct nfs41_server_scope *b) |
| 5410 | { |
| 5411 | if (a->server_scope_sz == b->server_scope_sz && |
| 5412 | memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0) |
| 5413 | return true; |
| 5414 | |
| 5415 | return false; |
| 5416 | } |
| 5417 | |
| 5418 | /* |
| 5419 | * nfs4_proc_bind_conn_to_session() |
| 5420 | * |
| 5421 | * The 4.1 client currently uses the same TCP connection for the |
| 5422 | * fore and backchannel. |
| 5423 | */ |
| 5424 | int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred) |
| 5425 | { |
| 5426 | int status; |
| 5427 | struct nfs41_bind_conn_to_session_res res; |
| 5428 | struct rpc_message msg = { |
| 5429 | .rpc_proc = |
| 5430 | &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION], |
| 5431 | .rpc_argp = clp, |
| 5432 | .rpc_resp = &res, |
| 5433 | .rpc_cred = cred, |
| 5434 | }; |
| 5435 | |
| 5436 | dprintk("--> %s\n", __func__); |
| 5437 | |
| 5438 | res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS); |
| 5439 | if (unlikely(res.session == NULL)) { |
| 5440 | status = -ENOMEM; |
| 5441 | goto out; |
| 5442 | } |
| 5443 | |
| 5444 | status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
| 5445 | if (status == 0) { |
| 5446 | if (memcmp(res.session->sess_id.data, |
| 5447 | clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) { |
| 5448 | dprintk("NFS: %s: Session ID mismatch\n", __func__); |
| 5449 | status = -EIO; |
| 5450 | goto out_session; |
| 5451 | } |
| 5452 | if (res.dir != NFS4_CDFS4_BOTH) { |
| 5453 | dprintk("NFS: %s: Unexpected direction from server\n", |
| 5454 | __func__); |
| 5455 | status = -EIO; |
| 5456 | goto out_session; |
| 5457 | } |
| 5458 | if (res.use_conn_in_rdma_mode) { |
| 5459 | dprintk("NFS: %s: Server returned RDMA mode = true\n", |
| 5460 | __func__); |
| 5461 | status = -EIO; |
| 5462 | goto out_session; |
| 5463 | } |
| 5464 | } |
| 5465 | out_session: |
| 5466 | kfree(res.session); |
| 5467 | out: |
| 5468 | dprintk("<-- %s status= %d\n", __func__, status); |
| 5469 | return status; |
| 5470 | } |
| 5471 | |
| 5472 | /* |
| 5473 | * nfs4_proc_exchange_id() |
| 5474 | * |
| 5475 | * Returns zero, a negative errno, or a negative NFS4ERR status code. |
| 5476 | * |
| 5477 | * Since the clientid has expired, all compounds using sessions |
| 5478 | * associated with the stale clientid will be returning |
| 5479 | * NFS4ERR_BADSESSION in the sequence operation, and will therefore |
| 5480 | * be in some phase of session reset. |
| 5481 | */ |
| 5482 | int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred) |
| 5483 | { |
| 5484 | nfs4_verifier verifier; |
| 5485 | struct nfs41_exchange_id_args args = { |
| 5486 | .verifier = &verifier, |
| 5487 | .client = clp, |
| 5488 | .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER, |
| 5489 | }; |
| 5490 | struct nfs41_exchange_id_res res = { |
| 5491 | 0 |
| 5492 | }; |
| 5493 | int status; |
| 5494 | struct rpc_message msg = { |
| 5495 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID], |
| 5496 | .rpc_argp = &args, |
| 5497 | .rpc_resp = &res, |
| 5498 | .rpc_cred = cred, |
| 5499 | }; |
| 5500 | |
| 5501 | nfs4_init_boot_verifier(clp, &verifier); |
| 5502 | args.id_len = nfs4_init_uniform_client_string(clp, args.id, |
| 5503 | sizeof(args.id)); |
| 5504 | dprintk("NFS call exchange_id auth=%s, '%.*s'\n", |
| 5505 | clp->cl_rpcclient->cl_auth->au_ops->au_name, |
| 5506 | args.id_len, args.id); |
| 5507 | |
| 5508 | res.server_owner = kzalloc(sizeof(struct nfs41_server_owner), |
| 5509 | GFP_NOFS); |
| 5510 | if (unlikely(res.server_owner == NULL)) { |
| 5511 | status = -ENOMEM; |
| 5512 | goto out; |
| 5513 | } |
| 5514 | |
| 5515 | res.server_scope = kzalloc(sizeof(struct nfs41_server_scope), |
| 5516 | GFP_NOFS); |
| 5517 | if (unlikely(res.server_scope == NULL)) { |
| 5518 | status = -ENOMEM; |
| 5519 | goto out_server_owner; |
| 5520 | } |
| 5521 | |
| 5522 | res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS); |
| 5523 | if (unlikely(res.impl_id == NULL)) { |
| 5524 | status = -ENOMEM; |
| 5525 | goto out_server_scope; |
| 5526 | } |
| 5527 | |
| 5528 | status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
| 5529 | if (status == 0) |
| 5530 | status = nfs4_check_cl_exchange_flags(res.flags); |
| 5531 | |
| 5532 | if (status == 0) { |
| 5533 | clp->cl_clientid = res.clientid; |
| 5534 | clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R); |
| 5535 | if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) |
| 5536 | clp->cl_seqid = res.seqid; |
| 5537 | |
| 5538 | kfree(clp->cl_serverowner); |
| 5539 | clp->cl_serverowner = res.server_owner; |
| 5540 | res.server_owner = NULL; |
| 5541 | |
| 5542 | /* use the most recent implementation id */ |
| 5543 | kfree(clp->cl_implid); |
| 5544 | clp->cl_implid = res.impl_id; |
| 5545 | |
| 5546 | if (clp->cl_serverscope != NULL && |
| 5547 | !nfs41_same_server_scope(clp->cl_serverscope, |
| 5548 | res.server_scope)) { |
| 5549 | dprintk("%s: server_scope mismatch detected\n", |
| 5550 | __func__); |
| 5551 | set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state); |
| 5552 | kfree(clp->cl_serverscope); |
| 5553 | clp->cl_serverscope = NULL; |
| 5554 | } |
| 5555 | |
| 5556 | if (clp->cl_serverscope == NULL) { |
| 5557 | clp->cl_serverscope = res.server_scope; |
| 5558 | goto out; |
| 5559 | } |
| 5560 | } else |
| 5561 | kfree(res.impl_id); |
| 5562 | |
| 5563 | out_server_owner: |
| 5564 | kfree(res.server_owner); |
| 5565 | out_server_scope: |
| 5566 | kfree(res.server_scope); |
| 5567 | out: |
| 5568 | if (clp->cl_implid != NULL) |
| 5569 | dprintk("NFS reply exchange_id: Server Implementation ID: " |
| 5570 | "domain: %s, name: %s, date: %llu,%u\n", |
| 5571 | clp->cl_implid->domain, clp->cl_implid->name, |
| 5572 | clp->cl_implid->date.seconds, |
| 5573 | clp->cl_implid->date.nseconds); |
| 5574 | dprintk("NFS reply exchange_id: %d\n", status); |
| 5575 | return status; |
| 5576 | } |
| 5577 | |
| 5578 | static int _nfs4_proc_destroy_clientid(struct nfs_client *clp, |
| 5579 | struct rpc_cred *cred) |
| 5580 | { |
| 5581 | struct rpc_message msg = { |
| 5582 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID], |
| 5583 | .rpc_argp = clp, |
| 5584 | .rpc_cred = cred, |
| 5585 | }; |
| 5586 | int status; |
| 5587 | |
| 5588 | status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
| 5589 | if (status) |
| 5590 | dprintk("NFS: Got error %d from the server %s on " |
| 5591 | "DESTROY_CLIENTID.", status, clp->cl_hostname); |
| 5592 | return status; |
| 5593 | } |
| 5594 | |
| 5595 | static int nfs4_proc_destroy_clientid(struct nfs_client *clp, |
| 5596 | struct rpc_cred *cred) |
| 5597 | { |
| 5598 | unsigned int loop; |
| 5599 | int ret; |
| 5600 | |
| 5601 | for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) { |
| 5602 | ret = _nfs4_proc_destroy_clientid(clp, cred); |
| 5603 | switch (ret) { |
| 5604 | case -NFS4ERR_DELAY: |
| 5605 | case -NFS4ERR_CLIENTID_BUSY: |
| 5606 | ssleep(1); |
| 5607 | break; |
| 5608 | default: |
| 5609 | return ret; |
| 5610 | } |
| 5611 | } |
| 5612 | return 0; |
| 5613 | } |
| 5614 | |
| 5615 | int nfs4_destroy_clientid(struct nfs_client *clp) |
| 5616 | { |
| 5617 | struct rpc_cred *cred; |
| 5618 | int ret = 0; |
| 5619 | |
| 5620 | if (clp->cl_mvops->minor_version < 1) |
| 5621 | goto out; |
| 5622 | if (clp->cl_exchange_flags == 0) |
| 5623 | goto out; |
| 5624 | if (clp->cl_preserve_clid) |
| 5625 | goto out; |
| 5626 | cred = nfs4_get_exchange_id_cred(clp); |
| 5627 | ret = nfs4_proc_destroy_clientid(clp, cred); |
| 5628 | if (cred) |
| 5629 | put_rpccred(cred); |
| 5630 | switch (ret) { |
| 5631 | case 0: |
| 5632 | case -NFS4ERR_STALE_CLIENTID: |
| 5633 | clp->cl_exchange_flags = 0; |
| 5634 | } |
| 5635 | out: |
| 5636 | return ret; |
| 5637 | } |
| 5638 | |
| 5639 | struct nfs4_get_lease_time_data { |
| 5640 | struct nfs4_get_lease_time_args *args; |
| 5641 | struct nfs4_get_lease_time_res *res; |
| 5642 | struct nfs_client *clp; |
| 5643 | }; |
| 5644 | |
| 5645 | static void nfs4_get_lease_time_prepare(struct rpc_task *task, |
| 5646 | void *calldata) |
| 5647 | { |
| 5648 | struct nfs4_get_lease_time_data *data = |
| 5649 | (struct nfs4_get_lease_time_data *)calldata; |
| 5650 | |
| 5651 | dprintk("--> %s\n", __func__); |
| 5652 | /* just setup sequence, do not trigger session recovery |
| 5653 | since we're invoked within one */ |
| 5654 | nfs41_setup_sequence(data->clp->cl_session, |
| 5655 | &data->args->la_seq_args, |
| 5656 | &data->res->lr_seq_res, |
| 5657 | task); |
| 5658 | dprintk("<-- %s\n", __func__); |
| 5659 | } |
| 5660 | |
| 5661 | /* |
| 5662 | * Called from nfs4_state_manager thread for session setup, so don't recover |
| 5663 | * from sequence operation or clientid errors. |
| 5664 | */ |
| 5665 | static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata) |
| 5666 | { |
| 5667 | struct nfs4_get_lease_time_data *data = |
| 5668 | (struct nfs4_get_lease_time_data *)calldata; |
| 5669 | |
| 5670 | dprintk("--> %s\n", __func__); |
| 5671 | if (!nfs41_sequence_done(task, &data->res->lr_seq_res)) |
| 5672 | return; |
| 5673 | switch (task->tk_status) { |
| 5674 | case -NFS4ERR_DELAY: |
| 5675 | case -NFS4ERR_GRACE: |
| 5676 | dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status); |
| 5677 | rpc_delay(task, NFS4_POLL_RETRY_MIN); |
| 5678 | task->tk_status = 0; |
| 5679 | /* fall through */ |
| 5680 | case -NFS4ERR_RETRY_UNCACHED_REP: |
| 5681 | rpc_restart_call_prepare(task); |
| 5682 | return; |
| 5683 | } |
| 5684 | dprintk("<-- %s\n", __func__); |
| 5685 | } |
| 5686 | |
| 5687 | static const struct rpc_call_ops nfs4_get_lease_time_ops = { |
| 5688 | .rpc_call_prepare = nfs4_get_lease_time_prepare, |
| 5689 | .rpc_call_done = nfs4_get_lease_time_done, |
| 5690 | }; |
| 5691 | |
| 5692 | int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo) |
| 5693 | { |
| 5694 | struct rpc_task *task; |
| 5695 | struct nfs4_get_lease_time_args args; |
| 5696 | struct nfs4_get_lease_time_res res = { |
| 5697 | .lr_fsinfo = fsinfo, |
| 5698 | }; |
| 5699 | struct nfs4_get_lease_time_data data = { |
| 5700 | .args = &args, |
| 5701 | .res = &res, |
| 5702 | .clp = clp, |
| 5703 | }; |
| 5704 | struct rpc_message msg = { |
| 5705 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME], |
| 5706 | .rpc_argp = &args, |
| 5707 | .rpc_resp = &res, |
| 5708 | }; |
| 5709 | struct rpc_task_setup task_setup = { |
| 5710 | .rpc_client = clp->cl_rpcclient, |
| 5711 | .rpc_message = &msg, |
| 5712 | .callback_ops = &nfs4_get_lease_time_ops, |
| 5713 | .callback_data = &data, |
| 5714 | .flags = RPC_TASK_TIMEOUT, |
| 5715 | }; |
| 5716 | int status; |
| 5717 | |
| 5718 | nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0); |
| 5719 | nfs4_set_sequence_privileged(&args.la_seq_args); |
| 5720 | dprintk("--> %s\n", __func__); |
| 5721 | task = rpc_run_task(&task_setup); |
| 5722 | |
| 5723 | if (IS_ERR(task)) |
| 5724 | status = PTR_ERR(task); |
| 5725 | else { |
| 5726 | status = task->tk_status; |
| 5727 | rpc_put_task(task); |
| 5728 | } |
| 5729 | dprintk("<-- %s return %d\n", __func__, status); |
| 5730 | |
| 5731 | return status; |
| 5732 | } |
| 5733 | |
| 5734 | /* |
| 5735 | * Initialize the values to be used by the client in CREATE_SESSION |
| 5736 | * If nfs4_init_session set the fore channel request and response sizes, |
| 5737 | * use them. |
| 5738 | * |
| 5739 | * Set the back channel max_resp_sz_cached to zero to force the client to |
| 5740 | * always set csa_cachethis to FALSE because the current implementation |
| 5741 | * of the back channel DRC only supports caching the CB_SEQUENCE operation. |
| 5742 | */ |
| 5743 | static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args) |
| 5744 | { |
| 5745 | struct nfs4_session *session = args->client->cl_session; |
| 5746 | unsigned int mxrqst_sz = session->fc_target_max_rqst_sz, |
| 5747 | mxresp_sz = session->fc_target_max_resp_sz; |
| 5748 | |
| 5749 | if (mxrqst_sz == 0) |
| 5750 | mxrqst_sz = NFS_MAX_FILE_IO_SIZE; |
| 5751 | if (mxresp_sz == 0) |
| 5752 | mxresp_sz = NFS_MAX_FILE_IO_SIZE; |
| 5753 | /* Fore channel attributes */ |
| 5754 | args->fc_attrs.max_rqst_sz = mxrqst_sz; |
| 5755 | args->fc_attrs.max_resp_sz = mxresp_sz; |
| 5756 | args->fc_attrs.max_ops = NFS4_MAX_OPS; |
| 5757 | args->fc_attrs.max_reqs = max_session_slots; |
| 5758 | |
| 5759 | dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u " |
| 5760 | "max_ops=%u max_reqs=%u\n", |
| 5761 | __func__, |
| 5762 | args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz, |
| 5763 | args->fc_attrs.max_ops, args->fc_attrs.max_reqs); |
| 5764 | |
| 5765 | /* Back channel attributes */ |
| 5766 | args->bc_attrs.max_rqst_sz = PAGE_SIZE; |
| 5767 | args->bc_attrs.max_resp_sz = PAGE_SIZE; |
| 5768 | args->bc_attrs.max_resp_sz_cached = 0; |
| 5769 | args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS; |
| 5770 | args->bc_attrs.max_reqs = 1; |
| 5771 | |
| 5772 | dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u " |
| 5773 | "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n", |
| 5774 | __func__, |
| 5775 | args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz, |
| 5776 | args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops, |
| 5777 | args->bc_attrs.max_reqs); |
| 5778 | } |
| 5779 | |
| 5780 | static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session) |
| 5781 | { |
| 5782 | struct nfs4_channel_attrs *sent = &args->fc_attrs; |
| 5783 | struct nfs4_channel_attrs *rcvd = &session->fc_attrs; |
| 5784 | |
| 5785 | if (rcvd->max_resp_sz > sent->max_resp_sz) |
| 5786 | return -EINVAL; |
| 5787 | /* |
| 5788 | * Our requested max_ops is the minimum we need; we're not |
| 5789 | * prepared to break up compounds into smaller pieces than that. |
| 5790 | * So, no point even trying to continue if the server won't |
| 5791 | * cooperate: |
| 5792 | */ |
| 5793 | if (rcvd->max_ops < sent->max_ops) |
| 5794 | return -EINVAL; |
| 5795 | if (rcvd->max_reqs == 0) |
| 5796 | return -EINVAL; |
| 5797 | if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE) |
| 5798 | rcvd->max_reqs = NFS4_MAX_SLOT_TABLE; |
| 5799 | return 0; |
| 5800 | } |
| 5801 | |
| 5802 | static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session) |
| 5803 | { |
| 5804 | struct nfs4_channel_attrs *sent = &args->bc_attrs; |
| 5805 | struct nfs4_channel_attrs *rcvd = &session->bc_attrs; |
| 5806 | |
| 5807 | if (rcvd->max_rqst_sz > sent->max_rqst_sz) |
| 5808 | return -EINVAL; |
| 5809 | if (rcvd->max_resp_sz < sent->max_resp_sz) |
| 5810 | return -EINVAL; |
| 5811 | if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached) |
| 5812 | return -EINVAL; |
| 5813 | /* These would render the backchannel useless: */ |
| 5814 | if (rcvd->max_ops != sent->max_ops) |
| 5815 | return -EINVAL; |
| 5816 | if (rcvd->max_reqs != sent->max_reqs) |
| 5817 | return -EINVAL; |
| 5818 | return 0; |
| 5819 | } |
| 5820 | |
| 5821 | static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args, |
| 5822 | struct nfs4_session *session) |
| 5823 | { |
| 5824 | int ret; |
| 5825 | |
| 5826 | ret = nfs4_verify_fore_channel_attrs(args, session); |
| 5827 | if (ret) |
| 5828 | return ret; |
| 5829 | return nfs4_verify_back_channel_attrs(args, session); |
| 5830 | } |
| 5831 | |
| 5832 | static int _nfs4_proc_create_session(struct nfs_client *clp, |
| 5833 | struct rpc_cred *cred) |
| 5834 | { |
| 5835 | struct nfs4_session *session = clp->cl_session; |
| 5836 | struct nfs41_create_session_args args = { |
| 5837 | .client = clp, |
| 5838 | .cb_program = NFS4_CALLBACK, |
| 5839 | }; |
| 5840 | struct nfs41_create_session_res res = { |
| 5841 | .client = clp, |
| 5842 | }; |
| 5843 | struct rpc_message msg = { |
| 5844 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION], |
| 5845 | .rpc_argp = &args, |
| 5846 | .rpc_resp = &res, |
| 5847 | .rpc_cred = cred, |
| 5848 | }; |
| 5849 | int status; |
| 5850 | |
| 5851 | nfs4_init_channel_attrs(&args); |
| 5852 | args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN); |
| 5853 | |
| 5854 | status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
| 5855 | |
| 5856 | if (!status) { |
| 5857 | /* Verify the session's negotiated channel_attrs values */ |
| 5858 | status = nfs4_verify_channel_attrs(&args, session); |
| 5859 | /* Increment the clientid slot sequence id */ |
| 5860 | clp->cl_seqid++; |
| 5861 | } |
| 5862 | |
| 5863 | return status; |
| 5864 | } |
| 5865 | |
| 5866 | /* |
| 5867 | * Issues a CREATE_SESSION operation to the server. |
| 5868 | * It is the responsibility of the caller to verify the session is |
| 5869 | * expired before calling this routine. |
| 5870 | */ |
| 5871 | int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred) |
| 5872 | { |
| 5873 | int status; |
| 5874 | unsigned *ptr; |
| 5875 | struct nfs4_session *session = clp->cl_session; |
| 5876 | |
| 5877 | dprintk("--> %s clp=%p session=%p\n", __func__, clp, session); |
| 5878 | |
| 5879 | status = _nfs4_proc_create_session(clp, cred); |
| 5880 | if (status) |
| 5881 | goto out; |
| 5882 | |
| 5883 | /* Init or reset the session slot tables */ |
| 5884 | status = nfs4_setup_session_slot_tables(session); |
| 5885 | dprintk("slot table setup returned %d\n", status); |
| 5886 | if (status) |
| 5887 | goto out; |
| 5888 | |
| 5889 | ptr = (unsigned *)&session->sess_id.data[0]; |
| 5890 | dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__, |
| 5891 | clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 5892 | out: |
| 5893 | dprintk("<-- %s\n", __func__); |
| 5894 | return status; |
| 5895 | } |
| 5896 | |
| 5897 | /* |
| 5898 | * Issue the over-the-wire RPC DESTROY_SESSION. |
| 5899 | * The caller must serialize access to this routine. |
| 5900 | */ |
| 5901 | int nfs4_proc_destroy_session(struct nfs4_session *session, |
| 5902 | struct rpc_cred *cred) |
| 5903 | { |
| 5904 | struct rpc_message msg = { |
| 5905 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION], |
| 5906 | .rpc_argp = session, |
| 5907 | .rpc_cred = cred, |
| 5908 | }; |
| 5909 | int status = 0; |
| 5910 | |
| 5911 | dprintk("--> nfs4_proc_destroy_session\n"); |
| 5912 | |
| 5913 | /* session is still being setup */ |
| 5914 | if (session->clp->cl_cons_state != NFS_CS_READY) |
| 5915 | return status; |
| 5916 | |
| 5917 | status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT); |
| 5918 | |
| 5919 | if (status) |
| 5920 | dprintk("NFS: Got error %d from the server on DESTROY_SESSION. " |
| 5921 | "Session has been destroyed regardless...\n", status); |
| 5922 | |
| 5923 | dprintk("<-- nfs4_proc_destroy_session\n"); |
| 5924 | return status; |
| 5925 | } |
| 5926 | |
| 5927 | /* |
| 5928 | * Renew the cl_session lease. |
| 5929 | */ |
| 5930 | struct nfs4_sequence_data { |
| 5931 | struct nfs_client *clp; |
| 5932 | struct nfs4_sequence_args args; |
| 5933 | struct nfs4_sequence_res res; |
| 5934 | }; |
| 5935 | |
| 5936 | static void nfs41_sequence_release(void *data) |
| 5937 | { |
| 5938 | struct nfs4_sequence_data *calldata = data; |
| 5939 | struct nfs_client *clp = calldata->clp; |
| 5940 | |
| 5941 | if (atomic_read(&clp->cl_count) > 1) |
| 5942 | nfs4_schedule_state_renewal(clp); |
| 5943 | nfs_put_client(clp); |
| 5944 | kfree(calldata); |
| 5945 | } |
| 5946 | |
| 5947 | static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp) |
| 5948 | { |
| 5949 | switch(task->tk_status) { |
| 5950 | case -NFS4ERR_DELAY: |
| 5951 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
| 5952 | return -EAGAIN; |
| 5953 | default: |
| 5954 | nfs4_schedule_lease_recovery(clp); |
| 5955 | } |
| 5956 | return 0; |
| 5957 | } |
| 5958 | |
| 5959 | static void nfs41_sequence_call_done(struct rpc_task *task, void *data) |
| 5960 | { |
| 5961 | struct nfs4_sequence_data *calldata = data; |
| 5962 | struct nfs_client *clp = calldata->clp; |
| 5963 | |
| 5964 | if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp)) |
| 5965 | return; |
| 5966 | |
| 5967 | if (task->tk_status < 0) { |
| 5968 | dprintk("%s ERROR %d\n", __func__, task->tk_status); |
| 5969 | if (atomic_read(&clp->cl_count) == 1) |
| 5970 | goto out; |
| 5971 | |
| 5972 | if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) { |
| 5973 | rpc_restart_call_prepare(task); |
| 5974 | return; |
| 5975 | } |
| 5976 | } |
| 5977 | dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred); |
| 5978 | out: |
| 5979 | dprintk("<-- %s\n", __func__); |
| 5980 | } |
| 5981 | |
| 5982 | static void nfs41_sequence_prepare(struct rpc_task *task, void *data) |
| 5983 | { |
| 5984 | struct nfs4_sequence_data *calldata = data; |
| 5985 | struct nfs_client *clp = calldata->clp; |
| 5986 | struct nfs4_sequence_args *args; |
| 5987 | struct nfs4_sequence_res *res; |
| 5988 | |
| 5989 | args = task->tk_msg.rpc_argp; |
| 5990 | res = task->tk_msg.rpc_resp; |
| 5991 | |
| 5992 | nfs41_setup_sequence(clp->cl_session, args, res, task); |
| 5993 | } |
| 5994 | |
| 5995 | static const struct rpc_call_ops nfs41_sequence_ops = { |
| 5996 | .rpc_call_done = nfs41_sequence_call_done, |
| 5997 | .rpc_call_prepare = nfs41_sequence_prepare, |
| 5998 | .rpc_release = nfs41_sequence_release, |
| 5999 | }; |
| 6000 | |
| 6001 | static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, |
| 6002 | struct rpc_cred *cred, |
| 6003 | bool is_privileged) |
| 6004 | { |
| 6005 | struct nfs4_sequence_data *calldata; |
| 6006 | struct rpc_message msg = { |
| 6007 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE], |
| 6008 | .rpc_cred = cred, |
| 6009 | }; |
| 6010 | struct rpc_task_setup task_setup_data = { |
| 6011 | .rpc_client = clp->cl_rpcclient, |
| 6012 | .rpc_message = &msg, |
| 6013 | .callback_ops = &nfs41_sequence_ops, |
| 6014 | .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT, |
| 6015 | }; |
| 6016 | |
| 6017 | if (!atomic_inc_not_zero(&clp->cl_count)) |
| 6018 | return ERR_PTR(-EIO); |
| 6019 | calldata = kzalloc(sizeof(*calldata), GFP_NOFS); |
| 6020 | if (calldata == NULL) { |
| 6021 | nfs_put_client(clp); |
| 6022 | return ERR_PTR(-ENOMEM); |
| 6023 | } |
| 6024 | nfs41_init_sequence(&calldata->args, &calldata->res, 0); |
| 6025 | if (is_privileged) |
| 6026 | nfs4_set_sequence_privileged(&calldata->args); |
| 6027 | msg.rpc_argp = &calldata->args; |
| 6028 | msg.rpc_resp = &calldata->res; |
| 6029 | calldata->clp = clp; |
| 6030 | task_setup_data.callback_data = calldata; |
| 6031 | |
| 6032 | return rpc_run_task(&task_setup_data); |
| 6033 | } |
| 6034 | |
| 6035 | static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags) |
| 6036 | { |
| 6037 | struct rpc_task *task; |
| 6038 | int ret = 0; |
| 6039 | |
| 6040 | if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0) |
| 6041 | return 0; |
| 6042 | task = _nfs41_proc_sequence(clp, cred, false); |
| 6043 | if (IS_ERR(task)) |
| 6044 | ret = PTR_ERR(task); |
| 6045 | else |
| 6046 | rpc_put_task_async(task); |
| 6047 | dprintk("<-- %s status=%d\n", __func__, ret); |
| 6048 | return ret; |
| 6049 | } |
| 6050 | |
| 6051 | static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred) |
| 6052 | { |
| 6053 | struct rpc_task *task; |
| 6054 | int ret; |
| 6055 | |
| 6056 | task = _nfs41_proc_sequence(clp, cred, true); |
| 6057 | if (IS_ERR(task)) { |
| 6058 | ret = PTR_ERR(task); |
| 6059 | goto out; |
| 6060 | } |
| 6061 | ret = rpc_wait_for_completion_task(task); |
| 6062 | if (!ret) { |
| 6063 | struct nfs4_sequence_res *res = task->tk_msg.rpc_resp; |
| 6064 | |
| 6065 | if (task->tk_status == 0) |
| 6066 | nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags); |
| 6067 | ret = task->tk_status; |
| 6068 | } |
| 6069 | rpc_put_task(task); |
| 6070 | out: |
| 6071 | dprintk("<-- %s status=%d\n", __func__, ret); |
| 6072 | return ret; |
| 6073 | } |
| 6074 | |
| 6075 | struct nfs4_reclaim_complete_data { |
| 6076 | struct nfs_client *clp; |
| 6077 | struct nfs41_reclaim_complete_args arg; |
| 6078 | struct nfs41_reclaim_complete_res res; |
| 6079 | }; |
| 6080 | |
| 6081 | static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data) |
| 6082 | { |
| 6083 | struct nfs4_reclaim_complete_data *calldata = data; |
| 6084 | |
| 6085 | nfs41_setup_sequence(calldata->clp->cl_session, |
| 6086 | &calldata->arg.seq_args, |
| 6087 | &calldata->res.seq_res, |
| 6088 | task); |
| 6089 | } |
| 6090 | |
| 6091 | static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp) |
| 6092 | { |
| 6093 | switch(task->tk_status) { |
| 6094 | case 0: |
| 6095 | case -NFS4ERR_COMPLETE_ALREADY: |
| 6096 | case -NFS4ERR_WRONG_CRED: /* What to do here? */ |
| 6097 | break; |
| 6098 | case -NFS4ERR_DELAY: |
| 6099 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
| 6100 | /* fall through */ |
| 6101 | case -NFS4ERR_RETRY_UNCACHED_REP: |
| 6102 | return -EAGAIN; |
| 6103 | default: |
| 6104 | nfs4_schedule_lease_recovery(clp); |
| 6105 | } |
| 6106 | return 0; |
| 6107 | } |
| 6108 | |
| 6109 | static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data) |
| 6110 | { |
| 6111 | struct nfs4_reclaim_complete_data *calldata = data; |
| 6112 | struct nfs_client *clp = calldata->clp; |
| 6113 | struct nfs4_sequence_res *res = &calldata->res.seq_res; |
| 6114 | |
| 6115 | dprintk("--> %s\n", __func__); |
| 6116 | if (!nfs41_sequence_done(task, res)) |
| 6117 | return; |
| 6118 | |
| 6119 | if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) { |
| 6120 | rpc_restart_call_prepare(task); |
| 6121 | return; |
| 6122 | } |
| 6123 | dprintk("<-- %s\n", __func__); |
| 6124 | } |
| 6125 | |
| 6126 | static void nfs4_free_reclaim_complete_data(void *data) |
| 6127 | { |
| 6128 | struct nfs4_reclaim_complete_data *calldata = data; |
| 6129 | |
| 6130 | kfree(calldata); |
| 6131 | } |
| 6132 | |
| 6133 | static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = { |
| 6134 | .rpc_call_prepare = nfs4_reclaim_complete_prepare, |
| 6135 | .rpc_call_done = nfs4_reclaim_complete_done, |
| 6136 | .rpc_release = nfs4_free_reclaim_complete_data, |
| 6137 | }; |
| 6138 | |
| 6139 | /* |
| 6140 | * Issue a global reclaim complete. |
| 6141 | */ |
| 6142 | static int nfs41_proc_reclaim_complete(struct nfs_client *clp) |
| 6143 | { |
| 6144 | struct nfs4_reclaim_complete_data *calldata; |
| 6145 | struct rpc_task *task; |
| 6146 | struct rpc_message msg = { |
| 6147 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE], |
| 6148 | }; |
| 6149 | struct rpc_task_setup task_setup_data = { |
| 6150 | .rpc_client = clp->cl_rpcclient, |
| 6151 | .rpc_message = &msg, |
| 6152 | .callback_ops = &nfs4_reclaim_complete_call_ops, |
| 6153 | .flags = RPC_TASK_ASYNC, |
| 6154 | }; |
| 6155 | int status = -ENOMEM; |
| 6156 | |
| 6157 | dprintk("--> %s\n", __func__); |
| 6158 | calldata = kzalloc(sizeof(*calldata), GFP_NOFS); |
| 6159 | if (calldata == NULL) |
| 6160 | goto out; |
| 6161 | calldata->clp = clp; |
| 6162 | calldata->arg.one_fs = 0; |
| 6163 | |
| 6164 | nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0); |
| 6165 | nfs4_set_sequence_privileged(&calldata->arg.seq_args); |
| 6166 | msg.rpc_argp = &calldata->arg; |
| 6167 | msg.rpc_resp = &calldata->res; |
| 6168 | task_setup_data.callback_data = calldata; |
| 6169 | task = rpc_run_task(&task_setup_data); |
| 6170 | if (IS_ERR(task)) { |
| 6171 | status = PTR_ERR(task); |
| 6172 | goto out; |
| 6173 | } |
| 6174 | status = nfs4_wait_for_completion_rpc_task(task); |
| 6175 | if (status == 0) |
| 6176 | status = task->tk_status; |
| 6177 | rpc_put_task(task); |
| 6178 | return 0; |
| 6179 | out: |
| 6180 | dprintk("<-- %s status=%d\n", __func__, status); |
| 6181 | return status; |
| 6182 | } |
| 6183 | |
| 6184 | static void |
| 6185 | nfs4_layoutget_prepare(struct rpc_task *task, void *calldata) |
| 6186 | { |
| 6187 | struct nfs4_layoutget *lgp = calldata; |
| 6188 | struct nfs_server *server = NFS_SERVER(lgp->args.inode); |
| 6189 | struct nfs4_session *session = nfs4_get_session(server); |
| 6190 | |
| 6191 | dprintk("--> %s\n", __func__); |
| 6192 | /* Note the is a race here, where a CB_LAYOUTRECALL can come in |
| 6193 | * right now covering the LAYOUTGET we are about to send. |
| 6194 | * However, that is not so catastrophic, and there seems |
| 6195 | * to be no way to prevent it completely. |
| 6196 | */ |
| 6197 | if (nfs41_setup_sequence(session, &lgp->args.seq_args, |
| 6198 | &lgp->res.seq_res, task)) |
| 6199 | return; |
| 6200 | if (pnfs_choose_layoutget_stateid(&lgp->args.stateid, |
| 6201 | NFS_I(lgp->args.inode)->layout, |
| 6202 | lgp->args.ctx->state)) { |
| 6203 | rpc_exit(task, NFS4_OK); |
| 6204 | } |
| 6205 | } |
| 6206 | |
| 6207 | static void nfs4_layoutget_done(struct rpc_task *task, void *calldata) |
| 6208 | { |
| 6209 | struct nfs4_layoutget *lgp = calldata; |
| 6210 | struct inode *inode = lgp->args.inode; |
| 6211 | struct nfs_server *server = NFS_SERVER(inode); |
| 6212 | struct pnfs_layout_hdr *lo; |
| 6213 | struct nfs4_state *state = NULL; |
| 6214 | unsigned long timeo, giveup; |
| 6215 | |
| 6216 | dprintk("--> %s\n", __func__); |
| 6217 | |
| 6218 | if (!nfs41_sequence_done(task, &lgp->res.seq_res)) |
| 6219 | goto out; |
| 6220 | |
| 6221 | switch (task->tk_status) { |
| 6222 | case 0: |
| 6223 | goto out; |
| 6224 | case -NFS4ERR_LAYOUTTRYLATER: |
| 6225 | case -NFS4ERR_RECALLCONFLICT: |
| 6226 | timeo = rpc_get_timeout(task->tk_client); |
| 6227 | giveup = lgp->args.timestamp + timeo; |
| 6228 | if (time_after(giveup, jiffies)) |
| 6229 | task->tk_status = -NFS4ERR_DELAY; |
| 6230 | break; |
| 6231 | case -NFS4ERR_EXPIRED: |
| 6232 | case -NFS4ERR_BAD_STATEID: |
| 6233 | spin_lock(&inode->i_lock); |
| 6234 | lo = NFS_I(inode)->layout; |
| 6235 | if (!lo || list_empty(&lo->plh_segs)) { |
| 6236 | spin_unlock(&inode->i_lock); |
| 6237 | /* If the open stateid was bad, then recover it. */ |
| 6238 | state = lgp->args.ctx->state; |
| 6239 | } else { |
| 6240 | LIST_HEAD(head); |
| 6241 | |
| 6242 | pnfs_mark_matching_lsegs_invalid(lo, &head, NULL); |
| 6243 | spin_unlock(&inode->i_lock); |
| 6244 | /* Mark the bad layout state as invalid, then |
| 6245 | * retry using the open stateid. */ |
| 6246 | pnfs_free_lseg_list(&head); |
| 6247 | } |
| 6248 | } |
| 6249 | if (nfs4_async_handle_error(task, server, state) == -EAGAIN) |
| 6250 | rpc_restart_call_prepare(task); |
| 6251 | out: |
| 6252 | dprintk("<-- %s\n", __func__); |
| 6253 | } |
| 6254 | |
| 6255 | static size_t max_response_pages(struct nfs_server *server) |
| 6256 | { |
| 6257 | u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; |
| 6258 | return nfs_page_array_len(0, max_resp_sz); |
| 6259 | } |
| 6260 | |
| 6261 | static void nfs4_free_pages(struct page **pages, size_t size) |
| 6262 | { |
| 6263 | int i; |
| 6264 | |
| 6265 | if (!pages) |
| 6266 | return; |
| 6267 | |
| 6268 | for (i = 0; i < size; i++) { |
| 6269 | if (!pages[i]) |
| 6270 | break; |
| 6271 | __free_page(pages[i]); |
| 6272 | } |
| 6273 | kfree(pages); |
| 6274 | } |
| 6275 | |
| 6276 | static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags) |
| 6277 | { |
| 6278 | struct page **pages; |
| 6279 | int i; |
| 6280 | |
| 6281 | pages = kcalloc(size, sizeof(struct page *), gfp_flags); |
| 6282 | if (!pages) { |
| 6283 | dprintk("%s: can't alloc array of %zu pages\n", __func__, size); |
| 6284 | return NULL; |
| 6285 | } |
| 6286 | |
| 6287 | for (i = 0; i < size; i++) { |
| 6288 | pages[i] = alloc_page(gfp_flags); |
| 6289 | if (!pages[i]) { |
| 6290 | dprintk("%s: failed to allocate page\n", __func__); |
| 6291 | nfs4_free_pages(pages, size); |
| 6292 | return NULL; |
| 6293 | } |
| 6294 | } |
| 6295 | |
| 6296 | return pages; |
| 6297 | } |
| 6298 | |
| 6299 | static void nfs4_layoutget_release(void *calldata) |
| 6300 | { |
| 6301 | struct nfs4_layoutget *lgp = calldata; |
| 6302 | struct inode *inode = lgp->args.inode; |
| 6303 | struct nfs_server *server = NFS_SERVER(inode); |
| 6304 | size_t max_pages = max_response_pages(server); |
| 6305 | |
| 6306 | dprintk("--> %s\n", __func__); |
| 6307 | nfs4_free_pages(lgp->args.layout.pages, max_pages); |
| 6308 | pnfs_put_layout_hdr(NFS_I(inode)->layout); |
| 6309 | put_nfs_open_context(lgp->args.ctx); |
| 6310 | kfree(calldata); |
| 6311 | dprintk("<-- %s\n", __func__); |
| 6312 | } |
| 6313 | |
| 6314 | static const struct rpc_call_ops nfs4_layoutget_call_ops = { |
| 6315 | .rpc_call_prepare = nfs4_layoutget_prepare, |
| 6316 | .rpc_call_done = nfs4_layoutget_done, |
| 6317 | .rpc_release = nfs4_layoutget_release, |
| 6318 | }; |
| 6319 | |
| 6320 | struct pnfs_layout_segment * |
| 6321 | nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags) |
| 6322 | { |
| 6323 | struct inode *inode = lgp->args.inode; |
| 6324 | struct nfs_server *server = NFS_SERVER(inode); |
| 6325 | size_t max_pages = max_response_pages(server); |
| 6326 | struct rpc_task *task; |
| 6327 | struct rpc_message msg = { |
| 6328 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET], |
| 6329 | .rpc_argp = &lgp->args, |
| 6330 | .rpc_resp = &lgp->res, |
| 6331 | }; |
| 6332 | struct rpc_task_setup task_setup_data = { |
| 6333 | .rpc_client = server->client, |
| 6334 | .rpc_message = &msg, |
| 6335 | .callback_ops = &nfs4_layoutget_call_ops, |
| 6336 | .callback_data = lgp, |
| 6337 | .flags = RPC_TASK_ASYNC, |
| 6338 | }; |
| 6339 | struct pnfs_layout_segment *lseg = NULL; |
| 6340 | int status = 0; |
| 6341 | |
| 6342 | dprintk("--> %s\n", __func__); |
| 6343 | |
| 6344 | lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags); |
| 6345 | if (!lgp->args.layout.pages) { |
| 6346 | nfs4_layoutget_release(lgp); |
| 6347 | return ERR_PTR(-ENOMEM); |
| 6348 | } |
| 6349 | lgp->args.layout.pglen = max_pages * PAGE_SIZE; |
| 6350 | lgp->args.timestamp = jiffies; |
| 6351 | |
| 6352 | lgp->res.layoutp = &lgp->args.layout; |
| 6353 | lgp->res.seq_res.sr_slot = NULL; |
| 6354 | nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0); |
| 6355 | |
| 6356 | /* nfs4_layoutget_release calls pnfs_put_layout_hdr */ |
| 6357 | pnfs_get_layout_hdr(NFS_I(inode)->layout); |
| 6358 | |
| 6359 | task = rpc_run_task(&task_setup_data); |
| 6360 | if (IS_ERR(task)) |
| 6361 | return ERR_CAST(task); |
| 6362 | status = nfs4_wait_for_completion_rpc_task(task); |
| 6363 | if (status == 0) |
| 6364 | status = task->tk_status; |
| 6365 | /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */ |
| 6366 | if (status == 0 && lgp->res.layoutp->len) |
| 6367 | lseg = pnfs_layout_process(lgp); |
| 6368 | rpc_put_task(task); |
| 6369 | dprintk("<-- %s status=%d\n", __func__, status); |
| 6370 | if (status) |
| 6371 | return ERR_PTR(status); |
| 6372 | return lseg; |
| 6373 | } |
| 6374 | |
| 6375 | static void |
| 6376 | nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata) |
| 6377 | { |
| 6378 | struct nfs4_layoutreturn *lrp = calldata; |
| 6379 | |
| 6380 | dprintk("--> %s\n", __func__); |
| 6381 | nfs41_setup_sequence(lrp->clp->cl_session, |
| 6382 | &lrp->args.seq_args, |
| 6383 | &lrp->res.seq_res, |
| 6384 | task); |
| 6385 | } |
| 6386 | |
| 6387 | static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata) |
| 6388 | { |
| 6389 | struct nfs4_layoutreturn *lrp = calldata; |
| 6390 | struct nfs_server *server; |
| 6391 | |
| 6392 | dprintk("--> %s\n", __func__); |
| 6393 | |
| 6394 | if (!nfs41_sequence_done(task, &lrp->res.seq_res)) |
| 6395 | return; |
| 6396 | |
| 6397 | server = NFS_SERVER(lrp->args.inode); |
| 6398 | if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) { |
| 6399 | rpc_restart_call_prepare(task); |
| 6400 | return; |
| 6401 | } |
| 6402 | dprintk("<-- %s\n", __func__); |
| 6403 | } |
| 6404 | |
| 6405 | static void nfs4_layoutreturn_release(void *calldata) |
| 6406 | { |
| 6407 | struct nfs4_layoutreturn *lrp = calldata; |
| 6408 | struct pnfs_layout_hdr *lo = lrp->args.layout; |
| 6409 | |
| 6410 | dprintk("--> %s\n", __func__); |
| 6411 | spin_lock(&lo->plh_inode->i_lock); |
| 6412 | if (lrp->res.lrs_present) |
| 6413 | pnfs_set_layout_stateid(lo, &lrp->res.stateid, true); |
| 6414 | lo->plh_block_lgets--; |
| 6415 | spin_unlock(&lo->plh_inode->i_lock); |
| 6416 | pnfs_put_layout_hdr(lrp->args.layout); |
| 6417 | kfree(calldata); |
| 6418 | dprintk("<-- %s\n", __func__); |
| 6419 | } |
| 6420 | |
| 6421 | static const struct rpc_call_ops nfs4_layoutreturn_call_ops = { |
| 6422 | .rpc_call_prepare = nfs4_layoutreturn_prepare, |
| 6423 | .rpc_call_done = nfs4_layoutreturn_done, |
| 6424 | .rpc_release = nfs4_layoutreturn_release, |
| 6425 | }; |
| 6426 | |
| 6427 | int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp) |
| 6428 | { |
| 6429 | struct rpc_task *task; |
| 6430 | struct rpc_message msg = { |
| 6431 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN], |
| 6432 | .rpc_argp = &lrp->args, |
| 6433 | .rpc_resp = &lrp->res, |
| 6434 | }; |
| 6435 | struct rpc_task_setup task_setup_data = { |
| 6436 | .rpc_client = lrp->clp->cl_rpcclient, |
| 6437 | .rpc_message = &msg, |
| 6438 | .callback_ops = &nfs4_layoutreturn_call_ops, |
| 6439 | .callback_data = lrp, |
| 6440 | }; |
| 6441 | int status; |
| 6442 | |
| 6443 | dprintk("--> %s\n", __func__); |
| 6444 | nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1); |
| 6445 | task = rpc_run_task(&task_setup_data); |
| 6446 | if (IS_ERR(task)) |
| 6447 | return PTR_ERR(task); |
| 6448 | status = task->tk_status; |
| 6449 | dprintk("<-- %s status=%d\n", __func__, status); |
| 6450 | rpc_put_task(task); |
| 6451 | return status; |
| 6452 | } |
| 6453 | |
| 6454 | /* |
| 6455 | * Retrieve the list of Data Server devices from the MDS. |
| 6456 | */ |
| 6457 | static int _nfs4_getdevicelist(struct nfs_server *server, |
| 6458 | const struct nfs_fh *fh, |
| 6459 | struct pnfs_devicelist *devlist) |
| 6460 | { |
| 6461 | struct nfs4_getdevicelist_args args = { |
| 6462 | .fh = fh, |
| 6463 | .layoutclass = server->pnfs_curr_ld->id, |
| 6464 | }; |
| 6465 | struct nfs4_getdevicelist_res res = { |
| 6466 | .devlist = devlist, |
| 6467 | }; |
| 6468 | struct rpc_message msg = { |
| 6469 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST], |
| 6470 | .rpc_argp = &args, |
| 6471 | .rpc_resp = &res, |
| 6472 | }; |
| 6473 | int status; |
| 6474 | |
| 6475 | dprintk("--> %s\n", __func__); |
| 6476 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, |
| 6477 | &res.seq_res, 0); |
| 6478 | dprintk("<-- %s status=%d\n", __func__, status); |
| 6479 | return status; |
| 6480 | } |
| 6481 | |
| 6482 | int nfs4_proc_getdevicelist(struct nfs_server *server, |
| 6483 | const struct nfs_fh *fh, |
| 6484 | struct pnfs_devicelist *devlist) |
| 6485 | { |
| 6486 | struct nfs4_exception exception = { }; |
| 6487 | int err; |
| 6488 | |
| 6489 | do { |
| 6490 | err = nfs4_handle_exception(server, |
| 6491 | _nfs4_getdevicelist(server, fh, devlist), |
| 6492 | &exception); |
| 6493 | } while (exception.retry); |
| 6494 | |
| 6495 | dprintk("%s: err=%d, num_devs=%u\n", __func__, |
| 6496 | err, devlist->num_devs); |
| 6497 | |
| 6498 | return err; |
| 6499 | } |
| 6500 | EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist); |
| 6501 | |
| 6502 | static int |
| 6503 | _nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev) |
| 6504 | { |
| 6505 | struct nfs4_getdeviceinfo_args args = { |
| 6506 | .pdev = pdev, |
| 6507 | }; |
| 6508 | struct nfs4_getdeviceinfo_res res = { |
| 6509 | .pdev = pdev, |
| 6510 | }; |
| 6511 | struct rpc_message msg = { |
| 6512 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO], |
| 6513 | .rpc_argp = &args, |
| 6514 | .rpc_resp = &res, |
| 6515 | }; |
| 6516 | int status; |
| 6517 | |
| 6518 | dprintk("--> %s\n", __func__); |
| 6519 | status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
| 6520 | dprintk("<-- %s status=%d\n", __func__, status); |
| 6521 | |
| 6522 | return status; |
| 6523 | } |
| 6524 | |
| 6525 | int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev) |
| 6526 | { |
| 6527 | struct nfs4_exception exception = { }; |
| 6528 | int err; |
| 6529 | |
| 6530 | do { |
| 6531 | err = nfs4_handle_exception(server, |
| 6532 | _nfs4_proc_getdeviceinfo(server, pdev), |
| 6533 | &exception); |
| 6534 | } while (exception.retry); |
| 6535 | return err; |
| 6536 | } |
| 6537 | EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo); |
| 6538 | |
| 6539 | static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata) |
| 6540 | { |
| 6541 | struct nfs4_layoutcommit_data *data = calldata; |
| 6542 | struct nfs_server *server = NFS_SERVER(data->args.inode); |
| 6543 | struct nfs4_session *session = nfs4_get_session(server); |
| 6544 | |
| 6545 | nfs41_setup_sequence(session, |
| 6546 | &data->args.seq_args, |
| 6547 | &data->res.seq_res, |
| 6548 | task); |
| 6549 | } |
| 6550 | |
| 6551 | static void |
| 6552 | nfs4_layoutcommit_done(struct rpc_task *task, void *calldata) |
| 6553 | { |
| 6554 | struct nfs4_layoutcommit_data *data = calldata; |
| 6555 | struct nfs_server *server = NFS_SERVER(data->args.inode); |
| 6556 | |
| 6557 | if (!nfs41_sequence_done(task, &data->res.seq_res)) |
| 6558 | return; |
| 6559 | |
| 6560 | switch (task->tk_status) { /* Just ignore these failures */ |
| 6561 | case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */ |
| 6562 | case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */ |
| 6563 | case -NFS4ERR_BADLAYOUT: /* no layout */ |
| 6564 | case -NFS4ERR_GRACE: /* loca_recalim always false */ |
| 6565 | task->tk_status = 0; |
| 6566 | break; |
| 6567 | case 0: |
| 6568 | nfs_post_op_update_inode_force_wcc(data->args.inode, |
| 6569 | data->res.fattr); |
| 6570 | break; |
| 6571 | default: |
| 6572 | if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) { |
| 6573 | rpc_restart_call_prepare(task); |
| 6574 | return; |
| 6575 | } |
| 6576 | } |
| 6577 | } |
| 6578 | |
| 6579 | static void nfs4_layoutcommit_release(void *calldata) |
| 6580 | { |
| 6581 | struct nfs4_layoutcommit_data *data = calldata; |
| 6582 | |
| 6583 | pnfs_cleanup_layoutcommit(data); |
| 6584 | put_rpccred(data->cred); |
| 6585 | kfree(data); |
| 6586 | } |
| 6587 | |
| 6588 | static const struct rpc_call_ops nfs4_layoutcommit_ops = { |
| 6589 | .rpc_call_prepare = nfs4_layoutcommit_prepare, |
| 6590 | .rpc_call_done = nfs4_layoutcommit_done, |
| 6591 | .rpc_release = nfs4_layoutcommit_release, |
| 6592 | }; |
| 6593 | |
| 6594 | int |
| 6595 | nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync) |
| 6596 | { |
| 6597 | struct rpc_message msg = { |
| 6598 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT], |
| 6599 | .rpc_argp = &data->args, |
| 6600 | .rpc_resp = &data->res, |
| 6601 | .rpc_cred = data->cred, |
| 6602 | }; |
| 6603 | struct rpc_task_setup task_setup_data = { |
| 6604 | .task = &data->task, |
| 6605 | .rpc_client = NFS_CLIENT(data->args.inode), |
| 6606 | .rpc_message = &msg, |
| 6607 | .callback_ops = &nfs4_layoutcommit_ops, |
| 6608 | .callback_data = data, |
| 6609 | .flags = RPC_TASK_ASYNC, |
| 6610 | }; |
| 6611 | struct rpc_task *task; |
| 6612 | int status = 0; |
| 6613 | |
| 6614 | dprintk("NFS: %4d initiating layoutcommit call. sync %d " |
| 6615 | "lbw: %llu inode %lu\n", |
| 6616 | data->task.tk_pid, sync, |
| 6617 | data->args.lastbytewritten, |
| 6618 | data->args.inode->i_ino); |
| 6619 | |
| 6620 | nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1); |
| 6621 | task = rpc_run_task(&task_setup_data); |
| 6622 | if (IS_ERR(task)) |
| 6623 | return PTR_ERR(task); |
| 6624 | if (sync == false) |
| 6625 | goto out; |
| 6626 | status = nfs4_wait_for_completion_rpc_task(task); |
| 6627 | if (status != 0) |
| 6628 | goto out; |
| 6629 | status = task->tk_status; |
| 6630 | out: |
| 6631 | dprintk("%s: status %d\n", __func__, status); |
| 6632 | rpc_put_task(task); |
| 6633 | return status; |
| 6634 | } |
| 6635 | |
| 6636 | static int |
| 6637 | _nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle, |
| 6638 | struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors) |
| 6639 | { |
| 6640 | struct nfs41_secinfo_no_name_args args = { |
| 6641 | .style = SECINFO_STYLE_CURRENT_FH, |
| 6642 | }; |
| 6643 | struct nfs4_secinfo_res res = { |
| 6644 | .flavors = flavors, |
| 6645 | }; |
| 6646 | struct rpc_message msg = { |
| 6647 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME], |
| 6648 | .rpc_argp = &args, |
| 6649 | .rpc_resp = &res, |
| 6650 | }; |
| 6651 | return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0); |
| 6652 | } |
| 6653 | |
| 6654 | static int |
| 6655 | nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle, |
| 6656 | struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors) |
| 6657 | { |
| 6658 | struct nfs4_exception exception = { }; |
| 6659 | int err; |
| 6660 | do { |
| 6661 | err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors); |
| 6662 | switch (err) { |
| 6663 | case 0: |
| 6664 | case -NFS4ERR_WRONGSEC: |
| 6665 | case -NFS4ERR_NOTSUPP: |
| 6666 | goto out; |
| 6667 | default: |
| 6668 | err = nfs4_handle_exception(server, err, &exception); |
| 6669 | } |
| 6670 | } while (exception.retry); |
| 6671 | out: |
| 6672 | return err; |
| 6673 | } |
| 6674 | |
| 6675 | static int |
| 6676 | nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle, |
| 6677 | struct nfs_fsinfo *info) |
| 6678 | { |
| 6679 | int err; |
| 6680 | struct page *page; |
| 6681 | rpc_authflavor_t flavor; |
| 6682 | struct nfs4_secinfo_flavors *flavors; |
| 6683 | |
| 6684 | page = alloc_page(GFP_KERNEL); |
| 6685 | if (!page) { |
| 6686 | err = -ENOMEM; |
| 6687 | goto out; |
| 6688 | } |
| 6689 | |
| 6690 | flavors = page_address(page); |
| 6691 | err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors); |
| 6692 | |
| 6693 | /* |
| 6694 | * Fall back on "guess and check" method if |
| 6695 | * the server doesn't support SECINFO_NO_NAME |
| 6696 | */ |
| 6697 | if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) { |
| 6698 | err = nfs4_find_root_sec(server, fhandle, info); |
| 6699 | goto out_freepage; |
| 6700 | } |
| 6701 | if (err) |
| 6702 | goto out_freepage; |
| 6703 | |
| 6704 | flavor = nfs_find_best_sec(flavors); |
| 6705 | if (err == 0) |
| 6706 | err = nfs4_lookup_root_sec(server, fhandle, info, flavor); |
| 6707 | |
| 6708 | out_freepage: |
| 6709 | put_page(page); |
| 6710 | if (err == -EACCES) |
| 6711 | return -EPERM; |
| 6712 | out: |
| 6713 | return err; |
| 6714 | } |
| 6715 | |
| 6716 | static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid) |
| 6717 | { |
| 6718 | int status; |
| 6719 | struct nfs41_test_stateid_args args = { |
| 6720 | .stateid = stateid, |
| 6721 | }; |
| 6722 | struct nfs41_test_stateid_res res; |
| 6723 | struct rpc_message msg = { |
| 6724 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID], |
| 6725 | .rpc_argp = &args, |
| 6726 | .rpc_resp = &res, |
| 6727 | }; |
| 6728 | |
| 6729 | dprintk("NFS call test_stateid %p\n", stateid); |
| 6730 | nfs41_init_sequence(&args.seq_args, &res.seq_res, 0); |
| 6731 | nfs4_set_sequence_privileged(&args.seq_args); |
| 6732 | status = nfs4_call_sync_sequence(server->client, server, &msg, |
| 6733 | &args.seq_args, &res.seq_res); |
| 6734 | if (status != NFS_OK) { |
| 6735 | dprintk("NFS reply test_stateid: failed, %d\n", status); |
| 6736 | return status; |
| 6737 | } |
| 6738 | dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status); |
| 6739 | return -res.status; |
| 6740 | } |
| 6741 | |
| 6742 | /** |
| 6743 | * nfs41_test_stateid - perform a TEST_STATEID operation |
| 6744 | * |
| 6745 | * @server: server / transport on which to perform the operation |
| 6746 | * @stateid: state ID to test |
| 6747 | * |
| 6748 | * Returns NFS_OK if the server recognizes that "stateid" is valid. |
| 6749 | * Otherwise a negative NFS4ERR value is returned if the operation |
| 6750 | * failed or the state ID is not currently valid. |
| 6751 | */ |
| 6752 | static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid) |
| 6753 | { |
| 6754 | struct nfs4_exception exception = { }; |
| 6755 | int err; |
| 6756 | do { |
| 6757 | err = _nfs41_test_stateid(server, stateid); |
| 6758 | if (err != -NFS4ERR_DELAY) |
| 6759 | break; |
| 6760 | nfs4_handle_exception(server, err, &exception); |
| 6761 | } while (exception.retry); |
| 6762 | return err; |
| 6763 | } |
| 6764 | |
| 6765 | static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid) |
| 6766 | { |
| 6767 | struct nfs41_free_stateid_args args = { |
| 6768 | .stateid = stateid, |
| 6769 | }; |
| 6770 | struct nfs41_free_stateid_res res; |
| 6771 | struct rpc_message msg = { |
| 6772 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID], |
| 6773 | .rpc_argp = &args, |
| 6774 | .rpc_resp = &res, |
| 6775 | }; |
| 6776 | int status; |
| 6777 | |
| 6778 | dprintk("NFS call free_stateid %p\n", stateid); |
| 6779 | nfs41_init_sequence(&args.seq_args, &res.seq_res, 0); |
| 6780 | nfs4_set_sequence_privileged(&args.seq_args); |
| 6781 | status = nfs4_call_sync_sequence(server->client, server, &msg, |
| 6782 | &args.seq_args, &res.seq_res); |
| 6783 | dprintk("NFS reply free_stateid: %d\n", status); |
| 6784 | return status; |
| 6785 | } |
| 6786 | |
| 6787 | /** |
| 6788 | * nfs41_free_stateid - perform a FREE_STATEID operation |
| 6789 | * |
| 6790 | * @server: server / transport on which to perform the operation |
| 6791 | * @stateid: state ID to release |
| 6792 | * |
| 6793 | * Returns NFS_OK if the server freed "stateid". Otherwise a |
| 6794 | * negative NFS4ERR value is returned. |
| 6795 | */ |
| 6796 | static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid) |
| 6797 | { |
| 6798 | struct nfs4_exception exception = { }; |
| 6799 | int err; |
| 6800 | do { |
| 6801 | err = _nfs4_free_stateid(server, stateid); |
| 6802 | if (err != -NFS4ERR_DELAY) |
| 6803 | break; |
| 6804 | nfs4_handle_exception(server, err, &exception); |
| 6805 | } while (exception.retry); |
| 6806 | return err; |
| 6807 | } |
| 6808 | |
| 6809 | static bool nfs41_match_stateid(const nfs4_stateid *s1, |
| 6810 | const nfs4_stateid *s2) |
| 6811 | { |
| 6812 | if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0) |
| 6813 | return false; |
| 6814 | |
| 6815 | if (s1->seqid == s2->seqid) |
| 6816 | return true; |
| 6817 | if (s1->seqid == 0 || s2->seqid == 0) |
| 6818 | return true; |
| 6819 | |
| 6820 | return false; |
| 6821 | } |
| 6822 | |
| 6823 | #endif /* CONFIG_NFS_V4_1 */ |
| 6824 | |
| 6825 | static bool nfs4_match_stateid(const nfs4_stateid *s1, |
| 6826 | const nfs4_stateid *s2) |
| 6827 | { |
| 6828 | return nfs4_stateid_match(s1, s2); |
| 6829 | } |
| 6830 | |
| 6831 | |
| 6832 | static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = { |
| 6833 | .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, |
| 6834 | .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, |
| 6835 | .recover_open = nfs4_open_reclaim, |
| 6836 | .recover_lock = nfs4_lock_reclaim, |
| 6837 | .establish_clid = nfs4_init_clientid, |
| 6838 | .get_clid_cred = nfs4_get_setclientid_cred, |
| 6839 | .detect_trunking = nfs40_discover_server_trunking, |
| 6840 | }; |
| 6841 | |
| 6842 | #if defined(CONFIG_NFS_V4_1) |
| 6843 | static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = { |
| 6844 | .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT, |
| 6845 | .state_flag_bit = NFS_STATE_RECLAIM_REBOOT, |
| 6846 | .recover_open = nfs4_open_reclaim, |
| 6847 | .recover_lock = nfs4_lock_reclaim, |
| 6848 | .establish_clid = nfs41_init_clientid, |
| 6849 | .get_clid_cred = nfs4_get_exchange_id_cred, |
| 6850 | .reclaim_complete = nfs41_proc_reclaim_complete, |
| 6851 | .detect_trunking = nfs41_discover_server_trunking, |
| 6852 | }; |
| 6853 | #endif /* CONFIG_NFS_V4_1 */ |
| 6854 | |
| 6855 | static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = { |
| 6856 | .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, |
| 6857 | .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, |
| 6858 | .recover_open = nfs4_open_expired, |
| 6859 | .recover_lock = nfs4_lock_expired, |
| 6860 | .establish_clid = nfs4_init_clientid, |
| 6861 | .get_clid_cred = nfs4_get_setclientid_cred, |
| 6862 | }; |
| 6863 | |
| 6864 | #if defined(CONFIG_NFS_V4_1) |
| 6865 | static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = { |
| 6866 | .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE, |
| 6867 | .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE, |
| 6868 | .recover_open = nfs41_open_expired, |
| 6869 | .recover_lock = nfs41_lock_expired, |
| 6870 | .establish_clid = nfs41_init_clientid, |
| 6871 | .get_clid_cred = nfs4_get_exchange_id_cred, |
| 6872 | }; |
| 6873 | #endif /* CONFIG_NFS_V4_1 */ |
| 6874 | |
| 6875 | static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = { |
| 6876 | .sched_state_renewal = nfs4_proc_async_renew, |
| 6877 | .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked, |
| 6878 | .renew_lease = nfs4_proc_renew, |
| 6879 | }; |
| 6880 | |
| 6881 | #if defined(CONFIG_NFS_V4_1) |
| 6882 | static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = { |
| 6883 | .sched_state_renewal = nfs41_proc_async_sequence, |
| 6884 | .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked, |
| 6885 | .renew_lease = nfs4_proc_sequence, |
| 6886 | }; |
| 6887 | #endif |
| 6888 | |
| 6889 | static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = { |
| 6890 | .minor_version = 0, |
| 6891 | .init_caps = NFS_CAP_READDIRPLUS |
| 6892 | | NFS_CAP_ATOMIC_OPEN |
| 6893 | | NFS_CAP_CHANGE_ATTR |
| 6894 | | NFS_CAP_POSIX_LOCK, |
| 6895 | .call_sync = _nfs4_call_sync, |
| 6896 | .match_stateid = nfs4_match_stateid, |
| 6897 | .find_root_sec = nfs4_find_root_sec, |
| 6898 | .reboot_recovery_ops = &nfs40_reboot_recovery_ops, |
| 6899 | .nograce_recovery_ops = &nfs40_nograce_recovery_ops, |
| 6900 | .state_renewal_ops = &nfs40_state_renewal_ops, |
| 6901 | }; |
| 6902 | |
| 6903 | #if defined(CONFIG_NFS_V4_1) |
| 6904 | static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = { |
| 6905 | .minor_version = 1, |
| 6906 | .init_caps = NFS_CAP_READDIRPLUS |
| 6907 | | NFS_CAP_ATOMIC_OPEN |
| 6908 | | NFS_CAP_CHANGE_ATTR |
| 6909 | | NFS_CAP_POSIX_LOCK |
| 6910 | | NFS_CAP_STATEID_NFSV41 |
| 6911 | | NFS_CAP_ATOMIC_OPEN_V1, |
| 6912 | .call_sync = nfs4_call_sync_sequence, |
| 6913 | .match_stateid = nfs41_match_stateid, |
| 6914 | .find_root_sec = nfs41_find_root_sec, |
| 6915 | .reboot_recovery_ops = &nfs41_reboot_recovery_ops, |
| 6916 | .nograce_recovery_ops = &nfs41_nograce_recovery_ops, |
| 6917 | .state_renewal_ops = &nfs41_state_renewal_ops, |
| 6918 | }; |
| 6919 | #endif |
| 6920 | |
| 6921 | const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = { |
| 6922 | [0] = &nfs_v4_0_minor_ops, |
| 6923 | #if defined(CONFIG_NFS_V4_1) |
| 6924 | [1] = &nfs_v4_1_minor_ops, |
| 6925 | #endif |
| 6926 | }; |
| 6927 | |
| 6928 | const struct inode_operations nfs4_dir_inode_operations = { |
| 6929 | .create = nfs_create, |
| 6930 | .lookup = nfs_lookup, |
| 6931 | .atomic_open = nfs_atomic_open, |
| 6932 | .link = nfs_link, |
| 6933 | .unlink = nfs_unlink, |
| 6934 | .symlink = nfs_symlink, |
| 6935 | .mkdir = nfs_mkdir, |
| 6936 | .rmdir = nfs_rmdir, |
| 6937 | .mknod = nfs_mknod, |
| 6938 | .rename = nfs_rename, |
| 6939 | .permission = nfs_permission, |
| 6940 | .getattr = nfs_getattr, |
| 6941 | .setattr = nfs_setattr, |
| 6942 | .getxattr = generic_getxattr, |
| 6943 | .setxattr = generic_setxattr, |
| 6944 | .listxattr = generic_listxattr, |
| 6945 | .removexattr = generic_removexattr, |
| 6946 | }; |
| 6947 | |
| 6948 | static const struct inode_operations nfs4_file_inode_operations = { |
| 6949 | .permission = nfs_permission, |
| 6950 | .getattr = nfs_getattr, |
| 6951 | .setattr = nfs_setattr, |
| 6952 | .getxattr = generic_getxattr, |
| 6953 | .setxattr = generic_setxattr, |
| 6954 | .listxattr = generic_listxattr, |
| 6955 | .removexattr = generic_removexattr, |
| 6956 | }; |
| 6957 | |
| 6958 | const struct nfs_rpc_ops nfs_v4_clientops = { |
| 6959 | .version = 4, /* protocol version */ |
| 6960 | .dentry_ops = &nfs4_dentry_operations, |
| 6961 | .dir_inode_ops = &nfs4_dir_inode_operations, |
| 6962 | .file_inode_ops = &nfs4_file_inode_operations, |
| 6963 | .file_ops = &nfs4_file_operations, |
| 6964 | .getroot = nfs4_proc_get_root, |
| 6965 | .submount = nfs4_submount, |
| 6966 | .try_mount = nfs4_try_mount, |
| 6967 | .getattr = nfs4_proc_getattr, |
| 6968 | .setattr = nfs4_proc_setattr, |
| 6969 | .lookup = nfs4_proc_lookup, |
| 6970 | .access = nfs4_proc_access, |
| 6971 | .readlink = nfs4_proc_readlink, |
| 6972 | .create = nfs4_proc_create, |
| 6973 | .remove = nfs4_proc_remove, |
| 6974 | .unlink_setup = nfs4_proc_unlink_setup, |
| 6975 | .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare, |
| 6976 | .unlink_done = nfs4_proc_unlink_done, |
| 6977 | .rename = nfs4_proc_rename, |
| 6978 | .rename_setup = nfs4_proc_rename_setup, |
| 6979 | .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare, |
| 6980 | .rename_done = nfs4_proc_rename_done, |
| 6981 | .link = nfs4_proc_link, |
| 6982 | .symlink = nfs4_proc_symlink, |
| 6983 | .mkdir = nfs4_proc_mkdir, |
| 6984 | .rmdir = nfs4_proc_remove, |
| 6985 | .readdir = nfs4_proc_readdir, |
| 6986 | .mknod = nfs4_proc_mknod, |
| 6987 | .statfs = nfs4_proc_statfs, |
| 6988 | .fsinfo = nfs4_proc_fsinfo, |
| 6989 | .pathconf = nfs4_proc_pathconf, |
| 6990 | .set_capabilities = nfs4_server_capabilities, |
| 6991 | .decode_dirent = nfs4_decode_dirent, |
| 6992 | .read_setup = nfs4_proc_read_setup, |
| 6993 | .read_pageio_init = pnfs_pageio_init_read, |
| 6994 | .read_rpc_prepare = nfs4_proc_read_rpc_prepare, |
| 6995 | .read_done = nfs4_read_done, |
| 6996 | .write_setup = nfs4_proc_write_setup, |
| 6997 | .write_pageio_init = pnfs_pageio_init_write, |
| 6998 | .write_rpc_prepare = nfs4_proc_write_rpc_prepare, |
| 6999 | .write_done = nfs4_write_done, |
| 7000 | .commit_setup = nfs4_proc_commit_setup, |
| 7001 | .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare, |
| 7002 | .commit_done = nfs4_commit_done, |
| 7003 | .lock = nfs4_proc_lock, |
| 7004 | .clear_acl_cache = nfs4_zap_acl_attr, |
| 7005 | .close_context = nfs4_close_context, |
| 7006 | .open_context = nfs4_atomic_open, |
| 7007 | .have_delegation = nfs4_have_delegation, |
| 7008 | .return_delegation = nfs4_inode_return_delegation, |
| 7009 | .alloc_client = nfs4_alloc_client, |
| 7010 | .init_client = nfs4_init_client, |
| 7011 | .free_client = nfs4_free_client, |
| 7012 | .create_server = nfs4_create_server, |
| 7013 | .clone_server = nfs_clone_server, |
| 7014 | }; |
| 7015 | |
| 7016 | static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = { |
| 7017 | .prefix = XATTR_NAME_NFSV4_ACL, |
| 7018 | .list = nfs4_xattr_list_nfs4_acl, |
| 7019 | .get = nfs4_xattr_get_nfs4_acl, |
| 7020 | .set = nfs4_xattr_set_nfs4_acl, |
| 7021 | }; |
| 7022 | |
| 7023 | const struct xattr_handler *nfs4_xattr_handlers[] = { |
| 7024 | &nfs4_xattr_nfs4_acl_handler, |
| 7025 | NULL |
| 7026 | }; |
| 7027 | |
| 7028 | /* |
| 7029 | * Local variables: |
| 7030 | * c-basic-offset: 8 |
| 7031 | * End: |
| 7032 | */ |