| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Copyright (c) 2014 Anna Schumaker <[email protected]> |
| 4 |
*/ |
| 5 |
#include <linux/fs.h> |
| 6 |
#include <linux/sunrpc/addr.h> |
| 7 |
#include <linux/sunrpc/sched.h> |
| 8 |
#include <linux/nfs.h> |
| 9 |
#include <linux/nfs3.h> |
| 10 |
#include <linux/nfs4.h> |
| 11 |
#include <linux/nfs_xdr.h> |
| 12 |
#include <linux/nfs_fs.h> |
| 13 |
#include "nfs4_fs.h" |
| 14 |
#include "nfs42.h" |
| 15 |
#include "iostat.h" |
| 16 |
#include "pnfs.h" |
| 17 |
#include "nfs4session.h" |
| 18 |
#include "internal.h" |
| 19 |
#include "delegation.h" |
| 20 |
#include "nfs4trace.h" |
| 21 |
|
| 22 |
#define NFSDBG_FACILITY NFSDBG_PROC |
| 23 |
static int nfs42_do_offload_cancel_async(struct file *dst, nfs4_stateid *std); |
| 24 |
static int nfs42_proc_offload_status(struct file *file, nfs4_stateid *stateid, |
| 25 |
u64 *copied); |
| 26 |
|
| 27 |
static void nfs42_set_netaddr(struct file *filep, struct nfs42_netaddr *naddr) |
| 28 |
{ |
| 29 |
struct nfs_client *clp = (NFS_SERVER(file_inode(filep)))->nfs_client; |
| 30 |
unsigned short port = 2049; |
| 31 |
|
| 32 |
rcu_read_lock(); |
| 33 |
naddr->netid_len = scnprintf(naddr->netid, |
| 34 |
sizeof(naddr->netid), "%s", |
| 35 |
rpc_peeraddr2str(clp->cl_rpcclient, |
| 36 |
RPC_DISPLAY_NETID)); |
| 37 |
naddr->addr_len = scnprintf(naddr->addr, |
| 38 |
sizeof(naddr->addr), |
| 39 |
"%s.%u.%u", |
| 40 |
rpc_peeraddr2str(clp->cl_rpcclient, |
| 41 |
RPC_DISPLAY_ADDR), |
| 42 |
port >> 8, port & 255); |
| 43 |
rcu_read_unlock(); |
| 44 |
} |
| 45 |
|
| 46 |
static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, |
| 47 |
struct nfs_lock_context *lock, loff_t offset, loff_t len) |
| 48 |
{ |
| 49 |
struct inode *inode = file_inode(filep); |
| 50 |
struct nfs_server *server = NFS_SERVER(inode); |
| 51 |
u32 bitmask[NFS_BITMASK_SZ]; |
| 52 |
struct nfs42_falloc_args args = { |
| 53 |
.falloc_fh = NFS_FH(inode), |
| 54 |
.falloc_offset = offset, |
| 55 |
.falloc_length = len, |
| 56 |
.falloc_bitmask = bitmask, |
| 57 |
}; |
| 58 |
struct nfs42_falloc_res res = { |
| 59 |
.falloc_server = server, |
| 60 |
}; |
| 61 |
int status; |
| 62 |
|
| 63 |
msg->rpc_argp = &args; |
| 64 |
msg->rpc_resp = &res; |
| 65 |
|
| 66 |
status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context, |
| 67 |
lock, FMODE_WRITE); |
| 68 |
if (status) { |
| 69 |
if (status == -EAGAIN) |
| 70 |
status = -NFS4ERR_BAD_STATEID; |
| 71 |
return status; |
| 72 |
} |
| 73 |
|
| 74 |
nfs4_bitmask_set(bitmask, server->cache_consistency_bitmask, inode, |
| 75 |
NFS_INO_INVALID_BLOCKS); |
| 76 |
|
| 77 |
res.falloc_fattr = nfs_alloc_fattr(); |
| 78 |
if (!res.falloc_fattr) |
| 79 |
return -ENOMEM; |
| 80 |
|
| 81 |
status = nfs4_call_sync(server->client, server, msg, |
| 82 |
&args.seq_args, &res.seq_res, 0); |
| 83 |
if (status == 0) { |
| 84 |
if (nfs_should_remove_suid(inode)) { |
| 85 |
spin_lock(&inode->i_lock); |
| 86 |
nfs_set_cache_invalid(inode, |
| 87 |
NFS_INO_REVAL_FORCED | NFS_INO_INVALID_MODE); |
| 88 |
spin_unlock(&inode->i_lock); |
| 89 |
} |
| 90 |
status = nfs_post_op_update_inode_force_wcc(inode, |
| 91 |
res.falloc_fattr); |
| 92 |
} |
| 93 |
if (msg->rpc_proc == &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE]) |
| 94 |
trace_nfs4_fallocate(inode, &args, status); |
| 95 |
else |
| 96 |
trace_nfs4_deallocate(inode, &args, status); |
| 97 |
kfree(res.falloc_fattr); |
| 98 |
return status; |
| 99 |
} |
| 100 |
|
| 101 |
static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep, |
| 102 |
loff_t offset, loff_t len) |
| 103 |
{ |
| 104 |
struct inode *inode = file_inode(filep); |
| 105 |
struct nfs_server *server = NFS_SERVER(inode); |
| 106 |
struct nfs4_exception exception = { }; |
| 107 |
struct nfs_lock_context *lock; |
| 108 |
int err; |
| 109 |
|
| 110 |
lock = nfs_get_lock_context(nfs_file_open_context(filep)); |
| 111 |
if (IS_ERR(lock)) |
| 112 |
return PTR_ERR(lock); |
| 113 |
|
| 114 |
exception.inode = inode; |
| 115 |
exception.state = lock->open_context->state; |
| 116 |
|
| 117 |
err = nfs_sync_inode(inode); |
| 118 |
if (err) |
| 119 |
goto out; |
| 120 |
|
| 121 |
do { |
| 122 |
err = _nfs42_proc_fallocate(msg, filep, lock, offset, len); |
| 123 |
if (err == -ENOTSUPP) { |
| 124 |
err = -EOPNOTSUPP; |
| 125 |
break; |
| 126 |
} |
| 127 |
err = nfs4_handle_exception(server, err, &exception); |
| 128 |
} while (exception.retry); |
| 129 |
out: |
| 130 |
nfs_put_lock_context(lock); |
| 131 |
return err; |
| 132 |
} |
| 133 |
|
| 134 |
int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len) |
| 135 |
{ |
| 136 |
struct rpc_message msg = { |
| 137 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE], |
| 138 |
}; |
| 139 |
struct inode *inode = file_inode(filep); |
| 140 |
loff_t oldsize; |
| 141 |
int err; |
| 142 |
|
| 143 |
if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE)) |
| 144 |
return -EOPNOTSUPP; |
| 145 |
|
| 146 |
err = nfs_start_io_write(inode); |
| 147 |
if (err) |
| 148 |
return err; |
| 149 |
|
| 150 |
oldsize = i_size_read(inode); |
| 151 |
|
| 152 |
err = nfs42_proc_fallocate(&msg, filep, offset, len); |
| 153 |
|
| 154 |
if (err == 0) |
| 155 |
nfs_truncate_last_folio(inode->i_mapping, oldsize, |
| 156 |
offset + len); |
| 157 |
else if (err == -EOPNOTSUPP) |
| 158 |
NFS_SERVER(inode)->caps &= ~(NFS_CAP_ALLOCATE | |
| 159 |
NFS_CAP_ZERO_RANGE); |
| 160 |
|
| 161 |
nfs_end_io_write(inode); |
| 162 |
return err; |
| 163 |
} |
| 164 |
|
| 165 |
int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len) |
| 166 |
{ |
| 167 |
struct rpc_message msg = { |
| 168 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE], |
| 169 |
}; |
| 170 |
struct inode *inode = file_inode(filep); |
| 171 |
int err; |
| 172 |
|
| 173 |
if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE)) |
| 174 |
return -EOPNOTSUPP; |
| 175 |
|
| 176 |
err = nfs_start_io_write(inode); |
| 177 |
if (err) |
| 178 |
return err; |
| 179 |
|
| 180 |
err = nfs42_proc_fallocate(&msg, filep, offset, len); |
| 181 |
if (err == 0) |
| 182 |
truncate_pagecache_range(inode, offset, (offset + len) -1); |
| 183 |
if (err == -EOPNOTSUPP) |
| 184 |
NFS_SERVER(inode)->caps &= ~(NFS_CAP_DEALLOCATE | |
| 185 |
NFS_CAP_ZERO_RANGE); |
| 186 |
|
| 187 |
nfs_end_io_write(inode); |
| 188 |
return err; |
| 189 |
} |
| 190 |
|
| 191 |
int nfs42_proc_zero_range(struct file *filep, loff_t offset, loff_t len) |
| 192 |
{ |
| 193 |
struct rpc_message msg = { |
| 194 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ZERO_RANGE], |
| 195 |
}; |
| 196 |
struct inode *inode = file_inode(filep); |
| 197 |
loff_t oldsize; |
| 198 |
int err; |
| 199 |
|
| 200 |
if (!nfs_server_capable(inode, NFS_CAP_ZERO_RANGE)) |
| 201 |
return -EOPNOTSUPP; |
| 202 |
|
| 203 |
err = nfs_start_io_write(inode); |
| 204 |
if (err) |
| 205 |
return err; |
| 206 |
|
| 207 |
oldsize = i_size_read(inode); |
| 208 |
err = nfs42_proc_fallocate(&msg, filep, offset, len); |
| 209 |
if (err == 0) { |
| 210 |
nfs_truncate_last_folio(inode->i_mapping, oldsize, |
| 211 |
offset + len); |
| 212 |
truncate_pagecache_range(inode, offset, (offset + len) -1); |
| 213 |
} else if (err == -EOPNOTSUPP) |
| 214 |
NFS_SERVER(inode)->caps &= ~NFS_CAP_ZERO_RANGE; |
| 215 |
|
| 216 |
nfs_end_io_write(inode); |
| 217 |
return err; |
| 218 |
} |
| 219 |
|
| 220 |
static void nfs4_copy_dequeue_callback(struct nfs_server *dst_server, |
| 221 |
struct nfs_server *src_server, |
| 222 |
struct nfs4_copy_state *copy) |
| 223 |
{ |
| 224 |
spin_lock(&dst_server->nfs_client->cl_lock); |
| 225 |
list_del_init(©->copies); |
| 226 |
spin_unlock(&dst_server->nfs_client->cl_lock); |
| 227 |
if (dst_server != src_server) { |
| 228 |
spin_lock(&src_server->nfs_client->cl_lock); |
| 229 |
list_del_init(©->src_copies); |
| 230 |
spin_unlock(&src_server->nfs_client->cl_lock); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
static int handle_async_copy(struct nfs42_copy_res *res, |
| 235 |
struct nfs_server *dst_server, |
| 236 |
struct nfs_server *src_server, |
| 237 |
struct file *src, |
| 238 |
struct file *dst, |
| 239 |
nfs4_stateid *src_stateid, |
| 240 |
bool *restart) |
| 241 |
{ |
| 242 |
struct nfs4_copy_state *copy, *tmp_copy = NULL, *iter; |
| 243 |
struct nfs_open_context *dst_ctx = nfs_file_open_context(dst); |
| 244 |
struct nfs_open_context *src_ctx = nfs_file_open_context(src); |
| 245 |
struct nfs_client *clp = dst_server->nfs_client; |
| 246 |
unsigned long timeout = 3 * HZ; |
| 247 |
int status = NFS4_OK; |
| 248 |
u64 copied; |
| 249 |
|
| 250 |
copy = kzalloc_obj(struct nfs4_copy_state); |
| 251 |
if (!copy) |
| 252 |
return -ENOMEM; |
| 253 |
|
| 254 |
spin_lock(&dst_server->nfs_client->cl_lock); |
| 255 |
list_for_each_entry(iter, |
| 256 |
&dst_server->nfs_client->pending_cb_stateids, |
| 257 |
copies) { |
| 258 |
if (memcmp(&res->write_res.stateid, &iter->stateid, |
| 259 |
NFS4_STATEID_SIZE)) |
| 260 |
continue; |
| 261 |
tmp_copy = iter; |
| 262 |
list_del(&iter->copies); |
| 263 |
break; |
| 264 |
} |
| 265 |
if (tmp_copy) { |
| 266 |
spin_unlock(&dst_server->nfs_client->cl_lock); |
| 267 |
kfree(copy); |
| 268 |
copy = tmp_copy; |
| 269 |
goto out; |
| 270 |
} |
| 271 |
|
| 272 |
memcpy(©->stateid, &res->write_res.stateid, NFS4_STATEID_SIZE); |
| 273 |
init_completion(©->completion); |
| 274 |
copy->parent_dst_state = dst_ctx->state; |
| 275 |
copy->parent_src_state = src_ctx->state; |
| 276 |
|
| 277 |
list_add_tail(©->copies, &dst_server->ss_copies); |
| 278 |
spin_unlock(&dst_server->nfs_client->cl_lock); |
| 279 |
|
| 280 |
if (dst_server != src_server) { |
| 281 |
spin_lock(&src_server->nfs_client->cl_lock); |
| 282 |
list_add_tail(©->src_copies, &src_server->ss_src_copies); |
| 283 |
spin_unlock(&src_server->nfs_client->cl_lock); |
| 284 |
} |
| 285 |
|
| 286 |
wait: |
| 287 |
status = wait_for_completion_interruptible_timeout(©->completion, |
| 288 |
timeout); |
| 289 |
if (!status) |
| 290 |
goto timeout; |
| 291 |
nfs4_copy_dequeue_callback(dst_server, src_server, copy); |
| 292 |
if (status == -ERESTARTSYS) { |
| 293 |
goto out_cancel; |
| 294 |
} else if (copy->flags || copy->error == NFS4ERR_PARTNER_NO_AUTH) { |
| 295 |
status = -EAGAIN; |
| 296 |
*restart = true; |
| 297 |
goto out_cancel; |
| 298 |
} |
| 299 |
out: |
| 300 |
res->write_res.count = copy->count; |
| 301 |
/* Copy out the updated write verifier provided by CB_OFFLOAD. */ |
| 302 |
memcpy(&res->write_res.verifier, ©->verf, sizeof(copy->verf)); |
| 303 |
status = -copy->error; |
| 304 |
|
| 305 |
out_free: |
| 306 |
kfree(copy); |
| 307 |
return status; |
| 308 |
out_cancel: |
| 309 |
nfs42_do_offload_cancel_async(dst, ©->stateid); |
| 310 |
if (!nfs42_files_from_same_server(src, dst)) |
| 311 |
nfs42_do_offload_cancel_async(src, src_stateid); |
| 312 |
goto out_free; |
| 313 |
timeout: |
| 314 |
timeout <<= 1; |
| 315 |
if (timeout > (clp->cl_lease_time >> 1)) |
| 316 |
timeout = clp->cl_lease_time >> 1; |
| 317 |
status = nfs42_proc_offload_status(dst, ©->stateid, &copied); |
| 318 |
if (status == -EINPROGRESS) |
| 319 |
goto wait; |
| 320 |
nfs4_copy_dequeue_callback(dst_server, src_server, copy); |
| 321 |
switch (status) { |
| 322 |
case 0: |
| 323 |
/* The server recognized the copy stateid, so it hasn't |
| 324 |
* rebooted. Don't overwrite the verifier returned in the |
| 325 |
* COPY result. */ |
| 326 |
res->write_res.count = copied; |
| 327 |
goto out_free; |
| 328 |
case -EREMOTEIO: |
| 329 |
/* COPY operation failed on the server. */ |
| 330 |
status = -EOPNOTSUPP; |
| 331 |
res->write_res.count = copied; |
| 332 |
goto out_free; |
| 333 |
case -EBADF: |
| 334 |
/* Server did not recognize the copy stateid. It has |
| 335 |
* probably restarted and lost the plot. */ |
| 336 |
res->write_res.count = 0; |
| 337 |
status = -EOPNOTSUPP; |
| 338 |
break; |
| 339 |
case -EOPNOTSUPP: |
| 340 |
/* RFC 7862 REQUIREs server to support OFFLOAD_STATUS when |
| 341 |
* it has signed up for an async COPY, so server is not |
| 342 |
* spec-compliant. */ |
| 343 |
res->write_res.count = 0; |
| 344 |
} |
| 345 |
goto out_free; |
| 346 |
} |
| 347 |
|
| 348 |
static int process_copy_commit(struct file *dst, loff_t pos_dst, |
| 349 |
struct nfs42_copy_res *res) |
| 350 |
{ |
| 351 |
struct nfs_commitres cres; |
| 352 |
int status = -ENOMEM; |
| 353 |
|
| 354 |
cres.verf = kzalloc_obj(struct nfs_writeverf); |
| 355 |
if (!cres.verf) |
| 356 |
goto out; |
| 357 |
|
| 358 |
status = nfs4_proc_commit(dst, pos_dst, res->write_res.count, &cres); |
| 359 |
if (status) |
| 360 |
goto out_free; |
| 361 |
if (nfs_write_verifier_cmp(&res->write_res.verifier.verifier, |
| 362 |
&cres.verf->verifier)) { |
| 363 |
dprintk("commit verf differs from copy verf\n"); |
| 364 |
status = -EAGAIN; |
| 365 |
} |
| 366 |
out_free: |
| 367 |
kfree(cres.verf); |
| 368 |
out: |
| 369 |
return status; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* nfs42_copy_dest_done - perform inode cache updates after clone/copy offload |
| 374 |
* @file: pointer to destination file |
| 375 |
* @pos: destination offset |
| 376 |
* @len: copy length |
| 377 |
* @oldsize: length of the file prior to clone/copy |
| 378 |
* |
| 379 |
* Punch a hole in the inode page cache, so that the NFS client will |
| 380 |
* know to retrieve new data. |
| 381 |
* Update the file size if necessary, and then mark the inode as having |
| 382 |
* invalid cached values for change attribute, ctime, mtime and space used. |
| 383 |
*/ |
| 384 |
static void nfs42_copy_dest_done(struct file *file, loff_t pos, loff_t len, |
| 385 |
loff_t oldsize) |
| 386 |
{ |
| 387 |
struct inode *inode = file_inode(file); |
| 388 |
struct address_space *mapping = file->f_mapping; |
| 389 |
loff_t newsize = pos + len; |
| 390 |
loff_t end = newsize - 1; |
| 391 |
|
| 392 |
nfs_truncate_last_folio(mapping, oldsize, pos); |
| 393 |
WARN_ON_ONCE(invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT, |
| 394 |
end >> PAGE_SHIFT)); |
| 395 |
|
| 396 |
spin_lock(&inode->i_lock); |
| 397 |
if (newsize > i_size_read(inode)) |
| 398 |
i_size_write(inode, newsize); |
| 399 |
nfs_set_cache_invalid(inode, NFS_INO_INVALID_CHANGE | |
| 400 |
NFS_INO_INVALID_CTIME | |
| 401 |
NFS_INO_INVALID_MTIME | |
| 402 |
NFS_INO_INVALID_BLOCKS); |
| 403 |
spin_unlock(&inode->i_lock); |
| 404 |
} |
| 405 |
|
| 406 |
static ssize_t _nfs42_proc_copy(struct file *src, |
| 407 |
struct nfs_lock_context *src_lock, |
| 408 |
struct file *dst, |
| 409 |
struct nfs_lock_context *dst_lock, |
| 410 |
struct nfs42_copy_args *args, |
| 411 |
struct nfs42_copy_res *res, |
| 412 |
struct nl4_server *nss, |
| 413 |
nfs4_stateid *cnr_stateid, |
| 414 |
bool *restart) |
| 415 |
{ |
| 416 |
struct rpc_message msg = { |
| 417 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY], |
| 418 |
.rpc_argp = args, |
| 419 |
.rpc_resp = res, |
| 420 |
}; |
| 421 |
struct inode *dst_inode = file_inode(dst); |
| 422 |
struct inode *src_inode = file_inode(src); |
| 423 |
struct nfs_server *dst_server = NFS_SERVER(dst_inode); |
| 424 |
struct nfs_server *src_server = NFS_SERVER(src_inode); |
| 425 |
loff_t pos_src = args->src_pos; |
| 426 |
loff_t pos_dst = args->dst_pos; |
| 427 |
loff_t oldsize_dst; |
| 428 |
size_t count = args->count; |
| 429 |
ssize_t status; |
| 430 |
|
| 431 |
if (nss) { |
| 432 |
args->cp_src = nss; |
| 433 |
nfs4_stateid_copy(&args->src_stateid, cnr_stateid); |
| 434 |
} else { |
| 435 |
status = nfs4_set_rw_stateid(&args->src_stateid, |
| 436 |
src_lock->open_context, src_lock, FMODE_READ); |
| 437 |
if (status) { |
| 438 |
if (status == -EAGAIN) |
| 439 |
status = -NFS4ERR_BAD_STATEID; |
| 440 |
return status; |
| 441 |
} |
| 442 |
} |
| 443 |
status = nfs_filemap_write_and_wait_range(src->f_mapping, |
| 444 |
pos_src, pos_src + (loff_t)count - 1); |
| 445 |
if (status) |
| 446 |
return status; |
| 447 |
|
| 448 |
status = nfs4_set_rw_stateid(&args->dst_stateid, dst_lock->open_context, |
| 449 |
dst_lock, FMODE_WRITE); |
| 450 |
if (status) { |
| 451 |
if (status == -EAGAIN) |
| 452 |
status = -NFS4ERR_BAD_STATEID; |
| 453 |
return status; |
| 454 |
} |
| 455 |
|
| 456 |
nfs_file_block_o_direct(NFS_I(dst_inode)); |
| 457 |
status = nfs_sync_inode(dst_inode); |
| 458 |
if (status) |
| 459 |
return status; |
| 460 |
|
| 461 |
res->commit_res.verf = NULL; |
| 462 |
if (args->sync) { |
| 463 |
res->commit_res.verf = |
| 464 |
kzalloc_obj(struct nfs_writeverf); |
| 465 |
if (!res->commit_res.verf) |
| 466 |
return -ENOMEM; |
| 467 |
} |
| 468 |
set_bit(NFS_CLNT_SRC_SSC_COPY_STATE, |
| 469 |
&src_lock->open_context->state->flags); |
| 470 |
set_bit(NFS_CLNT_DST_SSC_COPY_STATE, |
| 471 |
&dst_lock->open_context->state->flags); |
| 472 |
oldsize_dst = i_size_read(dst_inode); |
| 473 |
|
| 474 |
status = nfs4_call_sync(dst_server->client, dst_server, &msg, |
| 475 |
&args->seq_args, &res->seq_res, 0); |
| 476 |
trace_nfs4_copy(src_inode, dst_inode, args, res, nss, status); |
| 477 |
if (status == -ENOTSUPP) |
| 478 |
dst_server->caps &= ~NFS_CAP_COPY; |
| 479 |
if (status) |
| 480 |
goto out; |
| 481 |
|
| 482 |
if (args->sync && |
| 483 |
nfs_write_verifier_cmp(&res->write_res.verifier.verifier, |
| 484 |
&res->commit_res.verf->verifier)) { |
| 485 |
status = -EAGAIN; |
| 486 |
goto out; |
| 487 |
} |
| 488 |
|
| 489 |
if (!res->synchronous) { |
| 490 |
status = handle_async_copy(res, dst_server, src_server, src, |
| 491 |
dst, &args->src_stateid, restart); |
| 492 |
if (status) |
| 493 |
goto out; |
| 494 |
} |
| 495 |
|
| 496 |
if ((!res->synchronous || !args->sync) && |
| 497 |
res->write_res.verifier.committed != NFS_FILE_SYNC) { |
| 498 |
status = process_copy_commit(dst, pos_dst, res); |
| 499 |
if (status) |
| 500 |
goto out; |
| 501 |
} |
| 502 |
|
| 503 |
nfs42_copy_dest_done(dst, pos_dst, res->write_res.count, oldsize_dst); |
| 504 |
nfs_invalidate_atime(src_inode); |
| 505 |
status = res->write_res.count; |
| 506 |
out: |
| 507 |
if (args->sync) |
| 508 |
kfree(res->commit_res.verf); |
| 509 |
return status; |
| 510 |
} |
| 511 |
|
| 512 |
ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src, |
| 513 |
struct file *dst, loff_t pos_dst, size_t count, |
| 514 |
struct nl4_server *nss, |
| 515 |
nfs4_stateid *cnr_stateid, bool sync) |
| 516 |
{ |
| 517 |
struct nfs_server *server = NFS_SERVER(file_inode(dst)); |
| 518 |
struct nfs_lock_context *src_lock; |
| 519 |
struct nfs_lock_context *dst_lock; |
| 520 |
struct nfs42_copy_args args = { |
| 521 |
.src_fh = NFS_FH(file_inode(src)), |
| 522 |
.src_pos = pos_src, |
| 523 |
.dst_fh = NFS_FH(file_inode(dst)), |
| 524 |
.dst_pos = pos_dst, |
| 525 |
.count = count, |
| 526 |
.sync = sync, |
| 527 |
}; |
| 528 |
struct nfs42_copy_res res; |
| 529 |
struct nfs4_exception src_exception = { |
| 530 |
.inode = file_inode(src), |
| 531 |
.stateid = &args.src_stateid, |
| 532 |
}; |
| 533 |
struct nfs4_exception dst_exception = { |
| 534 |
.inode = file_inode(dst), |
| 535 |
.stateid = &args.dst_stateid, |
| 536 |
}; |
| 537 |
ssize_t err, err2; |
| 538 |
bool restart = false; |
| 539 |
|
| 540 |
src_lock = nfs_get_lock_context(nfs_file_open_context(src)); |
| 541 |
if (IS_ERR(src_lock)) |
| 542 |
return PTR_ERR(src_lock); |
| 543 |
|
| 544 |
src_exception.state = src_lock->open_context->state; |
| 545 |
|
| 546 |
dst_lock = nfs_get_lock_context(nfs_file_open_context(dst)); |
| 547 |
if (IS_ERR(dst_lock)) { |
| 548 |
err = PTR_ERR(dst_lock); |
| 549 |
goto out_put_src_lock; |
| 550 |
} |
| 551 |
|
| 552 |
dst_exception.state = dst_lock->open_context->state; |
| 553 |
|
| 554 |
do { |
| 555 |
inode_lock(file_inode(dst)); |
| 556 |
err = _nfs42_proc_copy(src, src_lock, |
| 557 |
dst, dst_lock, |
| 558 |
&args, &res, |
| 559 |
nss, cnr_stateid, &restart); |
| 560 |
inode_unlock(file_inode(dst)); |
| 561 |
|
| 562 |
if (err >= 0) |
| 563 |
break; |
| 564 |
if ((err == -ENOTSUPP || |
| 565 |
err == -NFS4ERR_OFFLOAD_DENIED) && |
| 566 |
nfs42_files_from_same_server(src, dst)) { |
| 567 |
err = -EOPNOTSUPP; |
| 568 |
break; |
| 569 |
} else if (err == -EAGAIN) { |
| 570 |
if (!restart) { |
| 571 |
dst_exception.retry = 1; |
| 572 |
continue; |
| 573 |
} |
| 574 |
break; |
| 575 |
} else if (err == -NFS4ERR_OFFLOAD_NO_REQS && |
| 576 |
args.sync != res.synchronous) { |
| 577 |
args.sync = res.synchronous; |
| 578 |
dst_exception.retry = 1; |
| 579 |
continue; |
| 580 |
} else if ((err == -ESTALE || |
| 581 |
err == -NFS4ERR_OFFLOAD_DENIED || |
| 582 |
err == -ENOTSUPP) && |
| 583 |
!nfs42_files_from_same_server(src, dst)) { |
| 584 |
nfs42_do_offload_cancel_async(src, &args.src_stateid); |
| 585 |
err = -EOPNOTSUPP; |
| 586 |
break; |
| 587 |
} |
| 588 |
|
| 589 |
err2 = nfs4_handle_exception(server, err, &src_exception); |
| 590 |
err = nfs4_handle_exception(server, err, &dst_exception); |
| 591 |
if (!err) |
| 592 |
err = err2; |
| 593 |
} while (src_exception.retry || dst_exception.retry); |
| 594 |
|
| 595 |
nfs_put_lock_context(dst_lock); |
| 596 |
out_put_src_lock: |
| 597 |
nfs_put_lock_context(src_lock); |
| 598 |
return err; |
| 599 |
} |
| 600 |
|
| 601 |
struct nfs42_offload_data { |
| 602 |
struct nfs_server *seq_server; |
| 603 |
struct nfs42_offload_status_args args; |
| 604 |
struct nfs42_offload_status_res res; |
| 605 |
}; |
| 606 |
|
| 607 |
static void nfs42_offload_prepare(struct rpc_task *task, void *calldata) |
| 608 |
{ |
| 609 |
struct nfs42_offload_data *data = calldata; |
| 610 |
|
| 611 |
nfs4_setup_sequence(data->seq_server->nfs_client, |
| 612 |
&data->args.osa_seq_args, |
| 613 |
&data->res.osr_seq_res, task); |
| 614 |
} |
| 615 |
|
| 616 |
static void nfs42_offload_cancel_done(struct rpc_task *task, void *calldata) |
| 617 |
{ |
| 618 |
struct nfs42_offload_data *data = calldata; |
| 619 |
|
| 620 |
trace_nfs4_offload_cancel(&data->args, task->tk_status); |
| 621 |
nfs41_sequence_done(task, &data->res.osr_seq_res); |
| 622 |
if (task->tk_status && |
| 623 |
nfs4_async_handle_error(task, data->seq_server, NULL, |
| 624 |
NULL) == -EAGAIN) |
| 625 |
rpc_restart_call_prepare(task); |
| 626 |
} |
| 627 |
|
| 628 |
static void nfs42_offload_release(void *data) |
| 629 |
{ |
| 630 |
kfree(data); |
| 631 |
} |
| 632 |
|
| 633 |
static const struct rpc_call_ops nfs42_offload_cancel_ops = { |
| 634 |
.rpc_call_prepare = nfs42_offload_prepare, |
| 635 |
.rpc_call_done = nfs42_offload_cancel_done, |
| 636 |
.rpc_release = nfs42_offload_release, |
| 637 |
}; |
| 638 |
|
| 639 |
static int nfs42_do_offload_cancel_async(struct file *dst, |
| 640 |
nfs4_stateid *stateid) |
| 641 |
{ |
| 642 |
struct nfs_server *dst_server = NFS_SERVER(file_inode(dst)); |
| 643 |
struct nfs42_offload_data *data = NULL; |
| 644 |
struct nfs_open_context *ctx = nfs_file_open_context(dst); |
| 645 |
struct rpc_task *task; |
| 646 |
struct rpc_message msg = { |
| 647 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OFFLOAD_CANCEL], |
| 648 |
.rpc_cred = ctx->cred, |
| 649 |
}; |
| 650 |
struct rpc_task_setup task_setup_data = { |
| 651 |
.rpc_client = dst_server->client, |
| 652 |
.rpc_message = &msg, |
| 653 |
.callback_ops = &nfs42_offload_cancel_ops, |
| 654 |
.workqueue = nfsiod_workqueue, |
| 655 |
.flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE, |
| 656 |
}; |
| 657 |
int status; |
| 658 |
|
| 659 |
if (!(dst_server->caps & NFS_CAP_OFFLOAD_CANCEL)) |
| 660 |
return -EOPNOTSUPP; |
| 661 |
|
| 662 |
data = kzalloc_obj(struct nfs42_offload_data); |
| 663 |
if (data == NULL) |
| 664 |
return -ENOMEM; |
| 665 |
|
| 666 |
data->seq_server = dst_server; |
| 667 |
data->args.osa_src_fh = NFS_FH(file_inode(dst)); |
| 668 |
memcpy(&data->args.osa_stateid, stateid, |
| 669 |
sizeof(data->args.osa_stateid)); |
| 670 |
msg.rpc_argp = &data->args; |
| 671 |
msg.rpc_resp = &data->res; |
| 672 |
task_setup_data.callback_data = data; |
| 673 |
nfs4_init_sequence(dst_server->nfs_client, &data->args.osa_seq_args, |
| 674 |
&data->res.osr_seq_res, 1, 0); |
| 675 |
task = rpc_run_task(&task_setup_data); |
| 676 |
if (IS_ERR(task)) |
| 677 |
return PTR_ERR(task); |
| 678 |
status = rpc_wait_for_completion_task(task); |
| 679 |
if (status == -ENOTSUPP) |
| 680 |
dst_server->caps &= ~NFS_CAP_OFFLOAD_CANCEL; |
| 681 |
rpc_put_task(task); |
| 682 |
return status; |
| 683 |
} |
| 684 |
|
| 685 |
static int |
| 686 |
_nfs42_proc_offload_status(struct nfs_server *server, struct file *file, |
| 687 |
struct nfs42_offload_data *data) |
| 688 |
{ |
| 689 |
struct nfs_open_context *ctx = nfs_file_open_context(file); |
| 690 |
struct rpc_message msg = { |
| 691 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OFFLOAD_STATUS], |
| 692 |
.rpc_argp = &data->args, |
| 693 |
.rpc_resp = &data->res, |
| 694 |
.rpc_cred = ctx->cred, |
| 695 |
}; |
| 696 |
int status; |
| 697 |
|
| 698 |
status = nfs4_call_sync(server->client, server, &msg, |
| 699 |
&data->args.osa_seq_args, |
| 700 |
&data->res.osr_seq_res, 1); |
| 701 |
trace_nfs4_offload_status(&data->args, status); |
| 702 |
switch (status) { |
| 703 |
case 0: |
| 704 |
break; |
| 705 |
|
| 706 |
case -NFS4ERR_ADMIN_REVOKED: |
| 707 |
case -NFS4ERR_BAD_STATEID: |
| 708 |
case -NFS4ERR_OLD_STATEID: |
| 709 |
/* |
| 710 |
* Server does not recognize the COPY stateid. CB_OFFLOAD |
| 711 |
* could have purged it, or server might have rebooted. |
| 712 |
* Since COPY stateids don't have an associated inode, |
| 713 |
* avoid triggering state recovery. |
| 714 |
*/ |
| 715 |
status = -EBADF; |
| 716 |
break; |
| 717 |
case -NFS4ERR_NOTSUPP: |
| 718 |
case -ENOTSUPP: |
| 719 |
case -EOPNOTSUPP: |
| 720 |
server->caps &= ~NFS_CAP_OFFLOAD_STATUS; |
| 721 |
status = -EOPNOTSUPP; |
| 722 |
break; |
| 723 |
} |
| 724 |
|
| 725 |
return status; |
| 726 |
} |
| 727 |
|
| 728 |
/** |
| 729 |
* nfs42_proc_offload_status - Poll completion status of an async copy operation |
| 730 |
* @dst: handle of file being copied into |
| 731 |
* @stateid: copy stateid (from async COPY result) |
| 732 |
* @copied: OUT: number of bytes copied so far |
| 733 |
* |
| 734 |
* Return values: |
| 735 |
* %0: Server returned an NFS4_OK completion status |
| 736 |
* %-EINPROGRESS: Server returned no completion status |
| 737 |
* %-EREMOTEIO: Server returned an error completion status |
| 738 |
* %-EBADF: Server did not recognize the copy stateid |
| 739 |
* %-EOPNOTSUPP: Server does not support OFFLOAD_STATUS |
| 740 |
* %-ERESTARTSYS: Wait interrupted by signal |
| 741 |
* |
| 742 |
* Other negative errnos indicate the client could not complete the |
| 743 |
* request. |
| 744 |
*/ |
| 745 |
static int |
| 746 |
nfs42_proc_offload_status(struct file *dst, nfs4_stateid *stateid, u64 *copied) |
| 747 |
{ |
| 748 |
struct inode *inode = file_inode(dst); |
| 749 |
struct nfs_server *server = NFS_SERVER(inode); |
| 750 |
struct nfs4_exception exception = { |
| 751 |
.inode = inode, |
| 752 |
}; |
| 753 |
struct nfs42_offload_data *data; |
| 754 |
int status; |
| 755 |
|
| 756 |
if (!(server->caps & NFS_CAP_OFFLOAD_STATUS)) |
| 757 |
return -EOPNOTSUPP; |
| 758 |
|
| 759 |
data = kzalloc_obj(*data); |
| 760 |
if (!data) |
| 761 |
return -ENOMEM; |
| 762 |
data->seq_server = server; |
| 763 |
data->args.osa_src_fh = NFS_FH(inode); |
| 764 |
memcpy(&data->args.osa_stateid, stateid, |
| 765 |
sizeof(data->args.osa_stateid)); |
| 766 |
exception.stateid = &data->args.osa_stateid; |
| 767 |
do { |
| 768 |
status = _nfs42_proc_offload_status(server, dst, data); |
| 769 |
if (status == -EOPNOTSUPP) |
| 770 |
goto out; |
| 771 |
status = nfs4_handle_exception(server, status, &exception); |
| 772 |
} while (exception.retry); |
| 773 |
if (status) |
| 774 |
goto out; |
| 775 |
|
| 776 |
*copied = data->res.osr_count; |
| 777 |
if (!data->res.complete_count) |
| 778 |
status = -EINPROGRESS; |
| 779 |
else if (data->res.osr_complete != NFS_OK) |
| 780 |
status = -EREMOTEIO; |
| 781 |
|
| 782 |
out: |
| 783 |
kfree(data); |
| 784 |
return status; |
| 785 |
} |
| 786 |
|
| 787 |
static int _nfs42_proc_copy_notify(struct file *src, struct file *dst, |
| 788 |
struct nfs42_copy_notify_args *args, |
| 789 |
struct nfs42_copy_notify_res *res) |
| 790 |
{ |
| 791 |
struct nfs_server *src_server = NFS_SERVER(file_inode(src)); |
| 792 |
struct rpc_message msg = { |
| 793 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY_NOTIFY], |
| 794 |
.rpc_argp = args, |
| 795 |
.rpc_resp = res, |
| 796 |
}; |
| 797 |
int status; |
| 798 |
struct nfs_open_context *ctx; |
| 799 |
struct nfs_lock_context *l_ctx; |
| 800 |
|
| 801 |
ctx = get_nfs_open_context(nfs_file_open_context(src)); |
| 802 |
l_ctx = nfs_get_lock_context(ctx); |
| 803 |
if (IS_ERR(l_ctx)) { |
| 804 |
status = PTR_ERR(l_ctx); |
| 805 |
goto out; |
| 806 |
} |
| 807 |
|
| 808 |
status = nfs4_set_rw_stateid(&args->cna_src_stateid, ctx, l_ctx, |
| 809 |
FMODE_READ); |
| 810 |
nfs_put_lock_context(l_ctx); |
| 811 |
if (status) { |
| 812 |
if (status == -EAGAIN) |
| 813 |
status = -NFS4ERR_BAD_STATEID; |
| 814 |
goto out; |
| 815 |
} |
| 816 |
|
| 817 |
status = nfs4_call_sync(src_server->client, src_server, &msg, |
| 818 |
&args->cna_seq_args, &res->cnr_seq_res, 0); |
| 819 |
trace_nfs4_copy_notify(file_inode(src), args, res, status); |
| 820 |
if (status == -ENOTSUPP) |
| 821 |
src_server->caps &= ~NFS_CAP_COPY_NOTIFY; |
| 822 |
|
| 823 |
out: |
| 824 |
put_nfs_open_context(nfs_file_open_context(src)); |
| 825 |
return status; |
| 826 |
} |
| 827 |
|
| 828 |
int nfs42_proc_copy_notify(struct file *src, struct file *dst, |
| 829 |
struct nfs42_copy_notify_res *res) |
| 830 |
{ |
| 831 |
struct nfs_server *src_server = NFS_SERVER(file_inode(src)); |
| 832 |
struct nfs42_copy_notify_args *args; |
| 833 |
struct nfs4_exception exception = { |
| 834 |
.inode = file_inode(src), |
| 835 |
}; |
| 836 |
int status; |
| 837 |
|
| 838 |
if (!(src_server->caps & NFS_CAP_COPY_NOTIFY)) |
| 839 |
return -EOPNOTSUPP; |
| 840 |
|
| 841 |
args = kzalloc_obj(struct nfs42_copy_notify_args); |
| 842 |
if (args == NULL) |
| 843 |
return -ENOMEM; |
| 844 |
|
| 845 |
args->cna_src_fh = NFS_FH(file_inode(src)), |
| 846 |
args->cna_dst.nl4_type = NL4_NETADDR; |
| 847 |
nfs42_set_netaddr(dst, &args->cna_dst.u.nl4_addr); |
| 848 |
exception.stateid = &args->cna_src_stateid; |
| 849 |
|
| 850 |
do { |
| 851 |
status = _nfs42_proc_copy_notify(src, dst, args, res); |
| 852 |
if (status == -ENOTSUPP) { |
| 853 |
status = -EOPNOTSUPP; |
| 854 |
goto out; |
| 855 |
} |
| 856 |
status = nfs4_handle_exception(src_server, status, &exception); |
| 857 |
} while (exception.retry); |
| 858 |
|
| 859 |
out: |
| 860 |
kfree(args); |
| 861 |
return status; |
| 862 |
} |
| 863 |
|
| 864 |
static loff_t _nfs42_proc_llseek(struct file *filep, |
| 865 |
struct nfs_lock_context *lock, loff_t offset, int whence) |
| 866 |
{ |
| 867 |
struct inode *inode = file_inode(filep); |
| 868 |
struct nfs42_seek_args args = { |
| 869 |
.sa_fh = NFS_FH(inode), |
| 870 |
.sa_offset = offset, |
| 871 |
.sa_what = (whence == SEEK_HOLE) ? |
| 872 |
NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA, |
| 873 |
}; |
| 874 |
struct nfs42_seek_res res; |
| 875 |
struct rpc_message msg = { |
| 876 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK], |
| 877 |
.rpc_argp = &args, |
| 878 |
.rpc_resp = &res, |
| 879 |
}; |
| 880 |
struct nfs_server *server = NFS_SERVER(inode); |
| 881 |
int status; |
| 882 |
|
| 883 |
if (!nfs_server_capable(inode, NFS_CAP_SEEK)) |
| 884 |
return -ENOTSUPP; |
| 885 |
|
| 886 |
status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context, |
| 887 |
lock, FMODE_READ); |
| 888 |
if (status) { |
| 889 |
if (status == -EAGAIN) |
| 890 |
status = -NFS4ERR_BAD_STATEID; |
| 891 |
return status; |
| 892 |
} |
| 893 |
|
| 894 |
status = nfs_filemap_write_and_wait_range(inode->i_mapping, |
| 895 |
offset, LLONG_MAX); |
| 896 |
if (status) |
| 897 |
return status; |
| 898 |
|
| 899 |
status = nfs4_call_sync(server->client, server, &msg, |
| 900 |
&args.seq_args, &res.seq_res, 0); |
| 901 |
trace_nfs4_llseek(inode, &args, &res, status); |
| 902 |
if (status == -ENOTSUPP) |
| 903 |
server->caps &= ~NFS_CAP_SEEK; |
| 904 |
if (status) |
| 905 |
return status; |
| 906 |
|
| 907 |
if (whence == SEEK_DATA && res.sr_eof) |
| 908 |
return -NFS4ERR_NXIO; |
| 909 |
else |
| 910 |
return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes); |
| 911 |
} |
| 912 |
|
| 913 |
loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence) |
| 914 |
{ |
| 915 |
struct nfs_server *server = NFS_SERVER(file_inode(filep)); |
| 916 |
struct nfs4_exception exception = { }; |
| 917 |
struct nfs_lock_context *lock; |
| 918 |
loff_t err; |
| 919 |
|
| 920 |
lock = nfs_get_lock_context(nfs_file_open_context(filep)); |
| 921 |
if (IS_ERR(lock)) |
| 922 |
return PTR_ERR(lock); |
| 923 |
|
| 924 |
exception.inode = file_inode(filep); |
| 925 |
exception.state = lock->open_context->state; |
| 926 |
|
| 927 |
do { |
| 928 |
err = _nfs42_proc_llseek(filep, lock, offset, whence); |
| 929 |
if (err >= 0) |
| 930 |
break; |
| 931 |
if (err == -ENOTSUPP) { |
| 932 |
err = -EOPNOTSUPP; |
| 933 |
break; |
| 934 |
} |
| 935 |
err = nfs4_handle_exception(server, err, &exception); |
| 936 |
} while (exception.retry); |
| 937 |
|
| 938 |
nfs_put_lock_context(lock); |
| 939 |
return err; |
| 940 |
} |
| 941 |
|
| 942 |
|
| 943 |
static void |
| 944 |
nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata) |
| 945 |
{ |
| 946 |
struct nfs42_layoutstat_data *data = calldata; |
| 947 |
struct inode *inode = data->inode; |
| 948 |
struct nfs_server *server = NFS_SERVER(inode); |
| 949 |
struct pnfs_layout_hdr *lo; |
| 950 |
|
| 951 |
spin_lock(&inode->i_lock); |
| 952 |
lo = NFS_I(inode)->layout; |
| 953 |
if (!pnfs_layout_is_valid(lo)) { |
| 954 |
spin_unlock(&inode->i_lock); |
| 955 |
rpc_exit(task, 0); |
| 956 |
return; |
| 957 |
} |
| 958 |
nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid); |
| 959 |
spin_unlock(&inode->i_lock); |
| 960 |
nfs4_setup_sequence(server->nfs_client, &data->args.seq_args, |
| 961 |
&data->res.seq_res, task); |
| 962 |
} |
| 963 |
|
| 964 |
static void |
| 965 |
nfs42_layoutstat_done(struct rpc_task *task, void *calldata) |
| 966 |
{ |
| 967 |
struct nfs42_layoutstat_data *data = calldata; |
| 968 |
struct inode *inode = data->inode; |
| 969 |
struct pnfs_layout_hdr *lo; |
| 970 |
|
| 971 |
if (!nfs4_sequence_done(task, &data->res.seq_res)) |
| 972 |
return; |
| 973 |
|
| 974 |
switch (task->tk_status) { |
| 975 |
case 0: |
| 976 |
return; |
| 977 |
case -NFS4ERR_BADHANDLE: |
| 978 |
case -ESTALE: |
| 979 |
pnfs_destroy_layout(NFS_I(inode)); |
| 980 |
break; |
| 981 |
case -NFS4ERR_EXPIRED: |
| 982 |
case -NFS4ERR_ADMIN_REVOKED: |
| 983 |
case -NFS4ERR_DELEG_REVOKED: |
| 984 |
case -NFS4ERR_STALE_STATEID: |
| 985 |
case -NFS4ERR_BAD_STATEID: |
| 986 |
spin_lock(&inode->i_lock); |
| 987 |
lo = NFS_I(inode)->layout; |
| 988 |
if (pnfs_layout_is_valid(lo) && |
| 989 |
nfs4_stateid_match(&data->args.stateid, |
| 990 |
&lo->plh_stateid)) { |
| 991 |
LIST_HEAD(head); |
| 992 |
|
| 993 |
/* |
| 994 |
* Mark the bad layout state as invalid, then retry |
| 995 |
* with the current stateid. |
| 996 |
*/ |
| 997 |
pnfs_mark_layout_stateid_invalid(lo, &head); |
| 998 |
spin_unlock(&inode->i_lock); |
| 999 |
pnfs_free_lseg_list(&head); |
| 1000 |
nfs_commit_inode(inode, 0); |
| 1001 |
} else |
| 1002 |
spin_unlock(&inode->i_lock); |
| 1003 |
break; |
| 1004 |
case -NFS4ERR_OLD_STATEID: |
| 1005 |
spin_lock(&inode->i_lock); |
| 1006 |
lo = NFS_I(inode)->layout; |
| 1007 |
if (pnfs_layout_is_valid(lo) && |
| 1008 |
nfs4_stateid_match_other(&data->args.stateid, |
| 1009 |
&lo->plh_stateid)) { |
| 1010 |
/* Do we need to delay before resending? */ |
| 1011 |
if (!nfs4_stateid_is_newer(&lo->plh_stateid, |
| 1012 |
&data->args.stateid)) |
| 1013 |
rpc_delay(task, HZ); |
| 1014 |
rpc_restart_call_prepare(task); |
| 1015 |
} |
| 1016 |
spin_unlock(&inode->i_lock); |
| 1017 |
break; |
| 1018 |
case -ENOTSUPP: |
| 1019 |
case -EOPNOTSUPP: |
| 1020 |
NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS; |
| 1021 |
} |
| 1022 |
|
| 1023 |
trace_nfs4_layoutstats(inode, &data->args.stateid, task->tk_status); |
| 1024 |
} |
| 1025 |
|
| 1026 |
static void |
| 1027 |
nfs42_layoutstat_release(void *calldata) |
| 1028 |
{ |
| 1029 |
struct nfs42_layoutstat_data *data = calldata; |
| 1030 |
struct nfs42_layoutstat_devinfo *devinfo = data->args.devinfo; |
| 1031 |
int i; |
| 1032 |
|
| 1033 |
for (i = 0; i < data->args.num_dev; i++) { |
| 1034 |
if (devinfo[i].ld_private.ops && devinfo[i].ld_private.ops->free) |
| 1035 |
devinfo[i].ld_private.ops->free(&devinfo[i].ld_private); |
| 1036 |
} |
| 1037 |
|
| 1038 |
pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout); |
| 1039 |
smp_mb__before_atomic(); |
| 1040 |
clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags); |
| 1041 |
smp_mb__after_atomic(); |
| 1042 |
nfs_iput_and_deactive(data->inode); |
| 1043 |
kfree(data->args.devinfo); |
| 1044 |
kfree(data); |
| 1045 |
} |
| 1046 |
|
| 1047 |
static const struct rpc_call_ops nfs42_layoutstat_ops = { |
| 1048 |
.rpc_call_prepare = nfs42_layoutstat_prepare, |
| 1049 |
.rpc_call_done = nfs42_layoutstat_done, |
| 1050 |
.rpc_release = nfs42_layoutstat_release, |
| 1051 |
}; |
| 1052 |
|
| 1053 |
int nfs42_proc_layoutstats_generic(struct nfs_server *server, |
| 1054 |
struct nfs42_layoutstat_data *data) |
| 1055 |
{ |
| 1056 |
struct rpc_message msg = { |
| 1057 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS], |
| 1058 |
.rpc_argp = &data->args, |
| 1059 |
.rpc_resp = &data->res, |
| 1060 |
}; |
| 1061 |
struct rpc_task_setup task_setup = { |
| 1062 |
.rpc_client = server->client, |
| 1063 |
.rpc_message = &msg, |
| 1064 |
.callback_ops = &nfs42_layoutstat_ops, |
| 1065 |
.callback_data = data, |
| 1066 |
.flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE, |
| 1067 |
}; |
| 1068 |
struct rpc_task *task; |
| 1069 |
|
| 1070 |
data->inode = nfs_igrab_and_active(data->args.inode); |
| 1071 |
if (!data->inode) { |
| 1072 |
nfs42_layoutstat_release(data); |
| 1073 |
return -EAGAIN; |
| 1074 |
} |
| 1075 |
nfs4_init_sequence(server->nfs_client, &data->args.seq_args, |
| 1076 |
&data->res.seq_res, 0, 0); |
| 1077 |
task = rpc_run_task(&task_setup); |
| 1078 |
if (IS_ERR(task)) |
| 1079 |
return PTR_ERR(task); |
| 1080 |
rpc_put_task(task); |
| 1081 |
return 0; |
| 1082 |
} |
| 1083 |
|
| 1084 |
static struct nfs42_layouterror_data * |
| 1085 |
nfs42_alloc_layouterror_data(struct pnfs_layout_segment *lseg, gfp_t gfp_flags) |
| 1086 |
{ |
| 1087 |
struct nfs42_layouterror_data *data; |
| 1088 |
struct inode *inode = lseg->pls_layout->plh_inode; |
| 1089 |
|
| 1090 |
data = kzalloc_obj(*data, gfp_flags); |
| 1091 |
if (data) { |
| 1092 |
data->args.inode = data->inode = nfs_igrab_and_active(inode); |
| 1093 |
if (data->inode) { |
| 1094 |
data->lseg = pnfs_get_lseg(lseg); |
| 1095 |
if (data->lseg) |
| 1096 |
return data; |
| 1097 |
nfs_iput_and_deactive(data->inode); |
| 1098 |
} |
| 1099 |
kfree(data); |
| 1100 |
} |
| 1101 |
return NULL; |
| 1102 |
} |
| 1103 |
|
| 1104 |
static void |
| 1105 |
nfs42_free_layouterror_data(struct nfs42_layouterror_data *data) |
| 1106 |
{ |
| 1107 |
pnfs_put_lseg(data->lseg); |
| 1108 |
nfs_iput_and_deactive(data->inode); |
| 1109 |
kfree(data); |
| 1110 |
} |
| 1111 |
|
| 1112 |
static void |
| 1113 |
nfs42_layouterror_prepare(struct rpc_task *task, void *calldata) |
| 1114 |
{ |
| 1115 |
struct nfs42_layouterror_data *data = calldata; |
| 1116 |
struct inode *inode = data->inode; |
| 1117 |
struct nfs_server *server = NFS_SERVER(inode); |
| 1118 |
struct pnfs_layout_hdr *lo = data->lseg->pls_layout; |
| 1119 |
unsigned i; |
| 1120 |
|
| 1121 |
spin_lock(&inode->i_lock); |
| 1122 |
if (!pnfs_layout_is_valid(lo)) { |
| 1123 |
spin_unlock(&inode->i_lock); |
| 1124 |
rpc_exit(task, 0); |
| 1125 |
return; |
| 1126 |
} |
| 1127 |
for (i = 0; i < data->args.num_errors; i++) |
| 1128 |
nfs4_stateid_copy(&data->args.errors[i].stateid, |
| 1129 |
&lo->plh_stateid); |
| 1130 |
spin_unlock(&inode->i_lock); |
| 1131 |
nfs4_setup_sequence(server->nfs_client, &data->args.seq_args, |
| 1132 |
&data->res.seq_res, task); |
| 1133 |
} |
| 1134 |
|
| 1135 |
static void |
| 1136 |
nfs42_layouterror_done(struct rpc_task *task, void *calldata) |
| 1137 |
{ |
| 1138 |
struct nfs42_layouterror_data *data = calldata; |
| 1139 |
struct inode *inode = data->inode; |
| 1140 |
struct pnfs_layout_hdr *lo = data->lseg->pls_layout; |
| 1141 |
|
| 1142 |
if (!nfs4_sequence_done(task, &data->res.seq_res)) |
| 1143 |
return; |
| 1144 |
|
| 1145 |
switch (task->tk_status) { |
| 1146 |
case 0: |
| 1147 |
return; |
| 1148 |
case -NFS4ERR_BADHANDLE: |
| 1149 |
case -ESTALE: |
| 1150 |
pnfs_destroy_layout(NFS_I(inode)); |
| 1151 |
break; |
| 1152 |
case -NFS4ERR_EXPIRED: |
| 1153 |
case -NFS4ERR_ADMIN_REVOKED: |
| 1154 |
case -NFS4ERR_DELEG_REVOKED: |
| 1155 |
case -NFS4ERR_STALE_STATEID: |
| 1156 |
case -NFS4ERR_BAD_STATEID: |
| 1157 |
spin_lock(&inode->i_lock); |
| 1158 |
if (pnfs_layout_is_valid(lo) && |
| 1159 |
nfs4_stateid_match(&data->args.errors[0].stateid, |
| 1160 |
&lo->plh_stateid)) { |
| 1161 |
LIST_HEAD(head); |
| 1162 |
|
| 1163 |
/* |
| 1164 |
* Mark the bad layout state as invalid, then retry |
| 1165 |
* with the current stateid. |
| 1166 |
*/ |
| 1167 |
pnfs_mark_layout_stateid_invalid(lo, &head); |
| 1168 |
spin_unlock(&inode->i_lock); |
| 1169 |
pnfs_free_lseg_list(&head); |
| 1170 |
nfs_commit_inode(inode, 0); |
| 1171 |
} else |
| 1172 |
spin_unlock(&inode->i_lock); |
| 1173 |
break; |
| 1174 |
case -NFS4ERR_OLD_STATEID: |
| 1175 |
spin_lock(&inode->i_lock); |
| 1176 |
if (pnfs_layout_is_valid(lo) && |
| 1177 |
nfs4_stateid_match_other(&data->args.errors[0].stateid, |
| 1178 |
&lo->plh_stateid)) { |
| 1179 |
/* Do we need to delay before resending? */ |
| 1180 |
if (!nfs4_stateid_is_newer(&lo->plh_stateid, |
| 1181 |
&data->args.errors[0].stateid)) |
| 1182 |
rpc_delay(task, HZ); |
| 1183 |
rpc_restart_call_prepare(task); |
| 1184 |
} |
| 1185 |
spin_unlock(&inode->i_lock); |
| 1186 |
break; |
| 1187 |
case -ENOTSUPP: |
| 1188 |
case -EOPNOTSUPP: |
| 1189 |
NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTERROR; |
| 1190 |
} |
| 1191 |
|
| 1192 |
trace_nfs4_layouterror(inode, &data->args.errors[0].stateid, |
| 1193 |
task->tk_status); |
| 1194 |
} |
| 1195 |
|
| 1196 |
static void |
| 1197 |
nfs42_layouterror_release(void *calldata) |
| 1198 |
{ |
| 1199 |
struct nfs42_layouterror_data *data = calldata; |
| 1200 |
|
| 1201 |
nfs42_free_layouterror_data(data); |
| 1202 |
} |
| 1203 |
|
| 1204 |
static const struct rpc_call_ops nfs42_layouterror_ops = { |
| 1205 |
.rpc_call_prepare = nfs42_layouterror_prepare, |
| 1206 |
.rpc_call_done = nfs42_layouterror_done, |
| 1207 |
.rpc_release = nfs42_layouterror_release, |
| 1208 |
}; |
| 1209 |
|
| 1210 |
int nfs42_proc_layouterror(struct pnfs_layout_segment *lseg, |
| 1211 |
const struct nfs42_layout_error *errors, size_t n) |
| 1212 |
{ |
| 1213 |
struct inode *inode = lseg->pls_layout->plh_inode; |
| 1214 |
struct nfs_server *server = NFS_SERVER(inode); |
| 1215 |
struct nfs42_layouterror_data *data; |
| 1216 |
struct rpc_task *task; |
| 1217 |
struct rpc_message msg = { |
| 1218 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTERROR], |
| 1219 |
}; |
| 1220 |
struct rpc_task_setup task_setup = { |
| 1221 |
.rpc_message = &msg, |
| 1222 |
.callback_ops = &nfs42_layouterror_ops, |
| 1223 |
.flags = RPC_TASK_ASYNC | RPC_TASK_MOVEABLE, |
| 1224 |
}; |
| 1225 |
unsigned int i; |
| 1226 |
|
| 1227 |
if (!nfs_server_capable(inode, NFS_CAP_LAYOUTERROR)) |
| 1228 |
return -EOPNOTSUPP; |
| 1229 |
if (n > NFS42_LAYOUTERROR_MAX) |
| 1230 |
return -EINVAL; |
| 1231 |
data = nfs42_alloc_layouterror_data(lseg, nfs_io_gfp_mask()); |
| 1232 |
if (!data) |
| 1233 |
return -ENOMEM; |
| 1234 |
for (i = 0; i < n; i++) { |
| 1235 |
data->args.errors[i] = errors[i]; |
| 1236 |
data->args.num_errors++; |
| 1237 |
data->res.num_errors++; |
| 1238 |
} |
| 1239 |
msg.rpc_argp = &data->args; |
| 1240 |
msg.rpc_resp = &data->res; |
| 1241 |
task_setup.callback_data = data; |
| 1242 |
task_setup.rpc_client = server->client; |
| 1243 |
nfs4_init_sequence(server->nfs_client, &data->args.seq_args, |
| 1244 |
&data->res.seq_res, 0, 0); |
| 1245 |
task = rpc_run_task(&task_setup); |
| 1246 |
if (IS_ERR(task)) |
| 1247 |
return PTR_ERR(task); |
| 1248 |
rpc_put_task(task); |
| 1249 |
return 0; |
| 1250 |
} |
| 1251 |
EXPORT_SYMBOL_GPL(nfs42_proc_layouterror); |
| 1252 |
|
| 1253 |
static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f, |
| 1254 |
struct file *dst_f, struct nfs_lock_context *src_lock, |
| 1255 |
struct nfs_lock_context *dst_lock, loff_t src_offset, |
| 1256 |
loff_t dst_offset, loff_t count) |
| 1257 |
{ |
| 1258 |
struct inode *src_inode = file_inode(src_f); |
| 1259 |
struct inode *dst_inode = file_inode(dst_f); |
| 1260 |
struct nfs_server *server = NFS_SERVER(dst_inode); |
| 1261 |
__u32 dst_bitmask[NFS_BITMASK_SZ]; |
| 1262 |
struct nfs42_clone_args args = { |
| 1263 |
.src_fh = NFS_FH(src_inode), |
| 1264 |
.dst_fh = NFS_FH(dst_inode), |
| 1265 |
.src_offset = src_offset, |
| 1266 |
.dst_offset = dst_offset, |
| 1267 |
.count = count, |
| 1268 |
.dst_bitmask = dst_bitmask, |
| 1269 |
}; |
| 1270 |
struct nfs42_clone_res res = { |
| 1271 |
.server = server, |
| 1272 |
}; |
| 1273 |
loff_t oldsize_dst = i_size_read(dst_inode); |
| 1274 |
int status; |
| 1275 |
|
| 1276 |
msg->rpc_argp = &args; |
| 1277 |
msg->rpc_resp = &res; |
| 1278 |
|
| 1279 |
status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context, |
| 1280 |
src_lock, FMODE_READ); |
| 1281 |
if (status) { |
| 1282 |
if (status == -EAGAIN) |
| 1283 |
status = -NFS4ERR_BAD_STATEID; |
| 1284 |
return status; |
| 1285 |
} |
| 1286 |
status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context, |
| 1287 |
dst_lock, FMODE_WRITE); |
| 1288 |
if (status) { |
| 1289 |
if (status == -EAGAIN) |
| 1290 |
status = -NFS4ERR_BAD_STATEID; |
| 1291 |
return status; |
| 1292 |
} |
| 1293 |
|
| 1294 |
res.dst_fattr = nfs_alloc_fattr(); |
| 1295 |
if (!res.dst_fattr) |
| 1296 |
return -ENOMEM; |
| 1297 |
|
| 1298 |
nfs4_bitmask_set(dst_bitmask, server->cache_consistency_bitmask, |
| 1299 |
dst_inode, NFS_INO_INVALID_BLOCKS); |
| 1300 |
|
| 1301 |
status = nfs4_call_sync(server->client, server, msg, |
| 1302 |
&args.seq_args, &res.seq_res, 0); |
| 1303 |
trace_nfs4_clone(src_inode, dst_inode, &args, status); |
| 1304 |
if (status == 0) { |
| 1305 |
/* a zero-length count means clone to EOF in src */ |
| 1306 |
if (count == 0 && res.dst_fattr->valid & NFS_ATTR_FATTR_SIZE) |
| 1307 |
count = nfs_size_to_loff_t(res.dst_fattr->size) - dst_offset; |
| 1308 |
nfs42_copy_dest_done(dst_f, dst_offset, count, oldsize_dst); |
| 1309 |
status = nfs_post_op_update_inode(dst_inode, res.dst_fattr); |
| 1310 |
} |
| 1311 |
|
| 1312 |
kfree(res.dst_fattr); |
| 1313 |
return status; |
| 1314 |
} |
| 1315 |
|
| 1316 |
int nfs42_proc_clone(struct file *src_f, struct file *dst_f, |
| 1317 |
loff_t src_offset, loff_t dst_offset, loff_t count) |
| 1318 |
{ |
| 1319 |
struct rpc_message msg = { |
| 1320 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE], |
| 1321 |
}; |
| 1322 |
struct inode *inode = file_inode(src_f); |
| 1323 |
struct nfs_server *server = NFS_SERVER(file_inode(src_f)); |
| 1324 |
struct nfs_lock_context *src_lock; |
| 1325 |
struct nfs_lock_context *dst_lock; |
| 1326 |
struct nfs4_exception src_exception = { }; |
| 1327 |
struct nfs4_exception dst_exception = { }; |
| 1328 |
int err, err2; |
| 1329 |
|
| 1330 |
if (!nfs_server_capable(inode, NFS_CAP_CLONE)) |
| 1331 |
return -EOPNOTSUPP; |
| 1332 |
|
| 1333 |
src_lock = nfs_get_lock_context(nfs_file_open_context(src_f)); |
| 1334 |
if (IS_ERR(src_lock)) |
| 1335 |
return PTR_ERR(src_lock); |
| 1336 |
|
| 1337 |
src_exception.inode = file_inode(src_f); |
| 1338 |
src_exception.state = src_lock->open_context->state; |
| 1339 |
|
| 1340 |
dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f)); |
| 1341 |
if (IS_ERR(dst_lock)) { |
| 1342 |
err = PTR_ERR(dst_lock); |
| 1343 |
goto out_put_src_lock; |
| 1344 |
} |
| 1345 |
|
| 1346 |
dst_exception.inode = file_inode(dst_f); |
| 1347 |
dst_exception.state = dst_lock->open_context->state; |
| 1348 |
|
| 1349 |
do { |
| 1350 |
err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock, |
| 1351 |
src_offset, dst_offset, count); |
| 1352 |
if (err == -ENOTSUPP || err == -EOPNOTSUPP) { |
| 1353 |
NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE; |
| 1354 |
err = -EOPNOTSUPP; |
| 1355 |
break; |
| 1356 |
} |
| 1357 |
|
| 1358 |
err2 = nfs4_handle_exception(server, err, &src_exception); |
| 1359 |
err = nfs4_handle_exception(server, err, &dst_exception); |
| 1360 |
if (!err) |
| 1361 |
err = err2; |
| 1362 |
} while (src_exception.retry || dst_exception.retry); |
| 1363 |
|
| 1364 |
nfs_put_lock_context(dst_lock); |
| 1365 |
out_put_src_lock: |
| 1366 |
nfs_put_lock_context(src_lock); |
| 1367 |
return err; |
| 1368 |
} |
| 1369 |
|
| 1370 |
#define NFS4XATTR_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE) |
| 1371 |
|
| 1372 |
static int _nfs42_proc_removexattr(struct inode *inode, const char *name) |
| 1373 |
{ |
| 1374 |
struct nfs_server *server = NFS_SERVER(inode); |
| 1375 |
struct nfs42_removexattrargs args = { |
| 1376 |
.fh = NFS_FH(inode), |
| 1377 |
.xattr_name = name, |
| 1378 |
}; |
| 1379 |
struct nfs42_removexattrres res; |
| 1380 |
struct rpc_message msg = { |
| 1381 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVEXATTR], |
| 1382 |
.rpc_argp = &args, |
| 1383 |
.rpc_resp = &res, |
| 1384 |
}; |
| 1385 |
int ret; |
| 1386 |
unsigned long timestamp = jiffies; |
| 1387 |
|
| 1388 |
ret = nfs4_call_sync(server->client, server, &msg, &args.seq_args, |
| 1389 |
&res.seq_res, 1); |
| 1390 |
trace_nfs4_removexattr(inode, name, ret); |
| 1391 |
if (!ret) |
| 1392 |
nfs4_update_changeattr(inode, &res.cinfo, timestamp, 0); |
| 1393 |
|
| 1394 |
return ret; |
| 1395 |
} |
| 1396 |
|
| 1397 |
static int _nfs42_proc_setxattr(struct inode *inode, const char *name, |
| 1398 |
const void *buf, size_t buflen, int flags) |
| 1399 |
{ |
| 1400 |
struct nfs_server *server = NFS_SERVER(inode); |
| 1401 |
__u32 bitmask[NFS_BITMASK_SZ]; |
| 1402 |
struct page *pages[NFS4XATTR_MAXPAGES]; |
| 1403 |
struct nfs42_setxattrargs arg = { |
| 1404 |
.fh = NFS_FH(inode), |
| 1405 |
.bitmask = bitmask, |
| 1406 |
.xattr_pages = pages, |
| 1407 |
.xattr_len = buflen, |
| 1408 |
.xattr_name = name, |
| 1409 |
.xattr_flags = flags, |
| 1410 |
}; |
| 1411 |
struct nfs42_setxattrres res = { |
| 1412 |
.server = server, |
| 1413 |
}; |
| 1414 |
struct rpc_message msg = { |
| 1415 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETXATTR], |
| 1416 |
.rpc_argp = &arg, |
| 1417 |
.rpc_resp = &res, |
| 1418 |
}; |
| 1419 |
int ret, np; |
| 1420 |
unsigned long timestamp = jiffies; |
| 1421 |
|
| 1422 |
if (buflen > server->sxasize) |
| 1423 |
return -ERANGE; |
| 1424 |
|
| 1425 |
res.fattr = nfs_alloc_fattr(); |
| 1426 |
if (!res.fattr) |
| 1427 |
return -ENOMEM; |
| 1428 |
|
| 1429 |
if (buflen > 0) { |
| 1430 |
np = nfs4_buf_to_pages_noslab(buf, buflen, arg.xattr_pages); |
| 1431 |
if (np < 0) { |
| 1432 |
ret = np; |
| 1433 |
goto out; |
| 1434 |
} |
| 1435 |
} else |
| 1436 |
np = 0; |
| 1437 |
|
| 1438 |
nfs4_bitmask_set(bitmask, server->cache_consistency_bitmask, |
| 1439 |
inode, NFS_INO_INVALID_CHANGE); |
| 1440 |
|
| 1441 |
ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, |
| 1442 |
&res.seq_res, 1); |
| 1443 |
trace_nfs4_setxattr(inode, name, ret); |
| 1444 |
|
| 1445 |
for (; np > 0; np--) |
| 1446 |
put_page(pages[np - 1]); |
| 1447 |
|
| 1448 |
if (!ret) { |
| 1449 |
nfs4_update_changeattr(inode, &res.cinfo, timestamp, 0); |
| 1450 |
ret = nfs_post_op_update_inode(inode, res.fattr); |
| 1451 |
} |
| 1452 |
|
| 1453 |
out: |
| 1454 |
kfree(res.fattr); |
| 1455 |
return ret; |
| 1456 |
} |
| 1457 |
|
| 1458 |
static ssize_t _nfs42_proc_getxattr(struct inode *inode, const char *name, |
| 1459 |
void *buf, size_t buflen, struct page **pages, |
| 1460 |
size_t plen) |
| 1461 |
{ |
| 1462 |
struct nfs_server *server = NFS_SERVER(inode); |
| 1463 |
struct nfs42_getxattrargs arg = { |
| 1464 |
.fh = NFS_FH(inode), |
| 1465 |
.xattr_name = name, |
| 1466 |
}; |
| 1467 |
struct nfs42_getxattrres res; |
| 1468 |
struct rpc_message msg = { |
| 1469 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETXATTR], |
| 1470 |
.rpc_argp = &arg, |
| 1471 |
.rpc_resp = &res, |
| 1472 |
}; |
| 1473 |
ssize_t ret; |
| 1474 |
|
| 1475 |
arg.xattr_len = plen; |
| 1476 |
arg.xattr_pages = pages; |
| 1477 |
|
| 1478 |
ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, |
| 1479 |
&res.seq_res, 0); |
| 1480 |
trace_nfs4_getxattr(inode, name, ret); |
| 1481 |
if (ret < 0) |
| 1482 |
return ret; |
| 1483 |
|
| 1484 |
/* |
| 1485 |
* Normally, the caching is done one layer up, but for successful |
| 1486 |
* RPCS, always cache the result here, even if the caller was |
| 1487 |
* just querying the length, or if the reply was too big for |
| 1488 |
* the caller. This avoids a second RPC in the case of the |
| 1489 |
* common query-alloc-retrieve cycle for xattrs. |
| 1490 |
* |
| 1491 |
* Note that xattr_len is always capped to XATTR_SIZE_MAX. |
| 1492 |
*/ |
| 1493 |
|
| 1494 |
nfs4_xattr_cache_add(inode, name, NULL, pages, res.xattr_len); |
| 1495 |
|
| 1496 |
if (buflen) { |
| 1497 |
if (res.xattr_len > buflen) |
| 1498 |
return -ERANGE; |
| 1499 |
_copy_from_pages(buf, pages, 0, res.xattr_len); |
| 1500 |
} |
| 1501 |
|
| 1502 |
return res.xattr_len; |
| 1503 |
} |
| 1504 |
|
| 1505 |
static ssize_t _nfs42_proc_listxattrs(struct inode *inode, void *buf, |
| 1506 |
size_t buflen, u64 *cookiep, bool *eofp) |
| 1507 |
{ |
| 1508 |
struct nfs_server *server = NFS_SERVER(inode); |
| 1509 |
struct page **pages; |
| 1510 |
struct nfs42_listxattrsargs arg = { |
| 1511 |
.fh = NFS_FH(inode), |
| 1512 |
.cookie = *cookiep, |
| 1513 |
}; |
| 1514 |
struct nfs42_listxattrsres res = { |
| 1515 |
.eof = false, |
| 1516 |
.xattr_buf = buf, |
| 1517 |
.xattr_len = buflen, |
| 1518 |
}; |
| 1519 |
struct rpc_message msg = { |
| 1520 |
.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LISTXATTRS], |
| 1521 |
.rpc_argp = &arg, |
| 1522 |
.rpc_resp = &res, |
| 1523 |
}; |
| 1524 |
u32 xdrlen; |
| 1525 |
int ret, np, i; |
| 1526 |
|
| 1527 |
|
| 1528 |
ret = -ENOMEM; |
| 1529 |
res.scratch = folio_alloc(GFP_KERNEL, 0); |
| 1530 |
if (!res.scratch) |
| 1531 |
goto out; |
| 1532 |
|
| 1533 |
xdrlen = nfs42_listxattr_xdrsize(buflen); |
| 1534 |
if (xdrlen > server->lxasize) |
| 1535 |
xdrlen = server->lxasize; |
| 1536 |
np = xdrlen / PAGE_SIZE + 1; |
| 1537 |
|
| 1538 |
pages = kzalloc_objs(struct page *, np); |
| 1539 |
if (!pages) |
| 1540 |
goto out_free_scratch; |
| 1541 |
for (i = 0; i < np; i++) { |
| 1542 |
pages[i] = alloc_page(GFP_KERNEL); |
| 1543 |
if (!pages[i]) |
| 1544 |
goto out_free_pages; |
| 1545 |
} |
| 1546 |
|
| 1547 |
arg.xattr_pages = pages; |
| 1548 |
arg.count = xdrlen; |
| 1549 |
|
| 1550 |
ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, |
| 1551 |
&res.seq_res, 0); |
| 1552 |
trace_nfs4_listxattr(inode, ret); |
| 1553 |
|
| 1554 |
if (ret >= 0) { |
| 1555 |
ret = res.copied; |
| 1556 |
*cookiep = res.cookie; |
| 1557 |
*eofp = res.eof; |
| 1558 |
} |
| 1559 |
|
| 1560 |
out_free_pages: |
| 1561 |
while (--np >= 0) { |
| 1562 |
if (pages[np]) |
| 1563 |
__free_page(pages[np]); |
| 1564 |
} |
| 1565 |
kfree(pages); |
| 1566 |
out_free_scratch: |
| 1567 |
folio_put(res.scratch); |
| 1568 |
out: |
| 1569 |
return ret; |
| 1570 |
|
| 1571 |
} |
| 1572 |
|
| 1573 |
ssize_t nfs42_proc_getxattr(struct inode *inode, const char *name, |
| 1574 |
void *buf, size_t buflen) |
| 1575 |
{ |
| 1576 |
struct nfs4_exception exception = { }; |
| 1577 |
ssize_t err, np, i; |
| 1578 |
struct page **pages; |
| 1579 |
|
| 1580 |
np = nfs_page_array_len(0, buflen ?: XATTR_SIZE_MAX); |
| 1581 |
pages = kmalloc_objs(*pages, np); |
| 1582 |
if (!pages) |
| 1583 |
return -ENOMEM; |
| 1584 |
|
| 1585 |
for (i = 0; i < np; i++) { |
| 1586 |
pages[i] = alloc_page(GFP_KERNEL); |
| 1587 |
if (!pages[i]) { |
| 1588 |
err = -ENOMEM; |
| 1589 |
goto out; |
| 1590 |
} |
| 1591 |
} |
| 1592 |
|
| 1593 |
/* |
| 1594 |
* The GETXATTR op has no length field in the call, and the |
| 1595 |
* xattr data is at the end of the reply. |
| 1596 |
* |
| 1597 |
* There is no downside in using the page-aligned length. It will |
| 1598 |
* allow receiving and caching xattrs that are too large for the |
| 1599 |
* caller but still fit in the page-rounded value. |
| 1600 |
*/ |
| 1601 |
do { |
| 1602 |
err = _nfs42_proc_getxattr(inode, name, buf, buflen, |
| 1603 |
pages, np * PAGE_SIZE); |
| 1604 |
if (err >= 0) |
| 1605 |
break; |
| 1606 |
err = nfs4_handle_exception(NFS_SERVER(inode), err, |
| 1607 |
&exception); |
| 1608 |
} while (exception.retry); |
| 1609 |
|
| 1610 |
out: |
| 1611 |
while (--i >= 0) |
| 1612 |
__free_page(pages[i]); |
| 1613 |
kfree(pages); |
| 1614 |
|
| 1615 |
return err; |
| 1616 |
} |
| 1617 |
|
| 1618 |
int nfs42_proc_setxattr(struct inode *inode, const char *name, |
| 1619 |
const void *buf, size_t buflen, int flags) |
| 1620 |
{ |
| 1621 |
struct nfs4_exception exception = { }; |
| 1622 |
int err; |
| 1623 |
|
| 1624 |
do { |
| 1625 |
err = _nfs42_proc_setxattr(inode, name, buf, buflen, flags); |
| 1626 |
if (!err) |
| 1627 |
break; |
| 1628 |
err = nfs4_handle_exception(NFS_SERVER(inode), err, |
| 1629 |
&exception); |
| 1630 |
} while (exception.retry); |
| 1631 |
|
| 1632 |
return err; |
| 1633 |
} |
| 1634 |
|
| 1635 |
ssize_t nfs42_proc_listxattrs(struct inode *inode, void *buf, |
| 1636 |
size_t buflen, u64 *cookiep, bool *eofp) |
| 1637 |
{ |
| 1638 |
struct nfs4_exception exception = { }; |
| 1639 |
ssize_t err; |
| 1640 |
|
| 1641 |
do { |
| 1642 |
err = _nfs42_proc_listxattrs(inode, buf, buflen, |
| 1643 |
cookiep, eofp); |
| 1644 |
if (err >= 0) |
| 1645 |
break; |
| 1646 |
err = nfs4_handle_exception(NFS_SERVER(inode), err, |
| 1647 |
&exception); |
| 1648 |
} while (exception.retry); |
| 1649 |
|
| 1650 |
return err; |
| 1651 |
} |
| 1652 |
|
| 1653 |
int nfs42_proc_removexattr(struct inode *inode, const char *name) |
| 1654 |
{ |
| 1655 |
struct nfs4_exception exception = { }; |
| 1656 |
int err; |
| 1657 |
|
| 1658 |
do { |
| 1659 |
err = _nfs42_proc_removexattr(inode, name); |
| 1660 |
if (!err) |
| 1661 |
break; |
| 1662 |
err = nfs4_handle_exception(NFS_SERVER(inode), err, |
| 1663 |
&exception); |
| 1664 |
} while (exception.retry); |
| 1665 |
|
| 1666 |
return err; |
| 1667 |
} |