vfio-pci: use 32-bit comparisons for register address for gcc-4.5
[linux/fpc-iii.git] / fs / nfs / nfs42proc.c
blob5cda392028ce10c66ea235217ad0b1094d0913e2
1 /*
2 * Copyright (c) 2014 Anna Schumaker <Anna.Schumaker@Netapp.com>
3 */
4 #include <linux/fs.h>
5 #include <linux/sunrpc/sched.h>
6 #include <linux/nfs.h>
7 #include <linux/nfs3.h>
8 #include <linux/nfs4.h>
9 #include <linux/nfs_xdr.h>
10 #include <linux/nfs_fs.h>
11 #include "nfs4_fs.h"
12 #include "nfs42.h"
13 #include "iostat.h"
14 #include "pnfs.h"
15 #include "internal.h"
17 #define NFSDBG_FACILITY NFSDBG_PROC
19 static int _nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
20 struct nfs_lock_context *lock, loff_t offset, loff_t len)
22 struct inode *inode = file_inode(filep);
23 struct nfs_server *server = NFS_SERVER(inode);
24 struct nfs42_falloc_args args = {
25 .falloc_fh = NFS_FH(inode),
26 .falloc_offset = offset,
27 .falloc_length = len,
28 .falloc_bitmask = server->cache_consistency_bitmask,
30 struct nfs42_falloc_res res = {
31 .falloc_server = server,
33 int status;
35 msg->rpc_argp = &args;
36 msg->rpc_resp = &res;
38 status = nfs4_set_rw_stateid(&args.falloc_stateid, lock->open_context,
39 lock, FMODE_WRITE);
40 if (status)
41 return status;
43 res.falloc_fattr = nfs_alloc_fattr();
44 if (!res.falloc_fattr)
45 return -ENOMEM;
47 status = nfs4_call_sync(server->client, server, msg,
48 &args.seq_args, &res.seq_res, 0);
49 if (status == 0)
50 status = nfs_post_op_update_inode(inode, res.falloc_fattr);
52 kfree(res.falloc_fattr);
53 return status;
56 static int nfs42_proc_fallocate(struct rpc_message *msg, struct file *filep,
57 loff_t offset, loff_t len)
59 struct nfs_server *server = NFS_SERVER(file_inode(filep));
60 struct nfs4_exception exception = { };
61 struct nfs_lock_context *lock;
62 int err;
64 lock = nfs_get_lock_context(nfs_file_open_context(filep));
65 if (IS_ERR(lock))
66 return PTR_ERR(lock);
68 exception.inode = file_inode(filep);
69 exception.state = lock->open_context->state;
71 do {
72 err = _nfs42_proc_fallocate(msg, filep, lock, offset, len);
73 if (err == -ENOTSUPP) {
74 err = -EOPNOTSUPP;
75 break;
77 err = nfs4_handle_exception(server, err, &exception);
78 } while (exception.retry);
80 nfs_put_lock_context(lock);
81 return err;
84 int nfs42_proc_allocate(struct file *filep, loff_t offset, loff_t len)
86 struct rpc_message msg = {
87 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ALLOCATE],
89 struct inode *inode = file_inode(filep);
90 int err;
92 if (!nfs_server_capable(inode, NFS_CAP_ALLOCATE))
93 return -EOPNOTSUPP;
95 inode_lock(inode);
97 err = nfs42_proc_fallocate(&msg, filep, offset, len);
98 if (err == -EOPNOTSUPP)
99 NFS_SERVER(inode)->caps &= ~NFS_CAP_ALLOCATE;
101 inode_unlock(inode);
102 return err;
105 int nfs42_proc_deallocate(struct file *filep, loff_t offset, loff_t len)
107 struct rpc_message msg = {
108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DEALLOCATE],
110 struct inode *inode = file_inode(filep);
111 int err;
113 if (!nfs_server_capable(inode, NFS_CAP_DEALLOCATE))
114 return -EOPNOTSUPP;
116 inode_lock(inode);
117 err = nfs_sync_inode(inode);
118 if (err)
119 goto out_unlock;
121 err = nfs42_proc_fallocate(&msg, filep, offset, len);
122 if (err == 0)
123 truncate_pagecache_range(inode, offset, (offset + len) -1);
124 if (err == -EOPNOTSUPP)
125 NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
126 out_unlock:
127 inode_unlock(inode);
128 return err;
131 static ssize_t _nfs42_proc_copy(struct file *src,
132 struct nfs_lock_context *src_lock,
133 struct file *dst,
134 struct nfs_lock_context *dst_lock,
135 struct nfs42_copy_args *args,
136 struct nfs42_copy_res *res)
138 struct rpc_message msg = {
139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY],
140 .rpc_argp = args,
141 .rpc_resp = res,
143 struct inode *dst_inode = file_inode(dst);
144 struct nfs_server *server = NFS_SERVER(dst_inode);
145 loff_t pos_src = args->src_pos;
146 loff_t pos_dst = args->dst_pos;
147 size_t count = args->count;
148 int status;
150 status = nfs4_set_rw_stateid(&args->src_stateid, src_lock->open_context,
151 src_lock, FMODE_READ);
152 if (status)
153 return status;
155 status = nfs_filemap_write_and_wait_range(file_inode(src)->i_mapping,
156 pos_src, pos_src + (loff_t)count - 1);
157 if (status)
158 return status;
160 status = nfs4_set_rw_stateid(&args->dst_stateid, dst_lock->open_context,
161 dst_lock, FMODE_WRITE);
162 if (status)
163 return status;
165 status = nfs_sync_inode(dst_inode);
166 if (status)
167 return status;
169 status = nfs4_call_sync(server->client, server, &msg,
170 &args->seq_args, &res->seq_res, 0);
171 if (status == -ENOTSUPP)
172 server->caps &= ~NFS_CAP_COPY;
173 if (status)
174 return status;
176 if (res->write_res.verifier.committed != NFS_FILE_SYNC) {
177 status = nfs_commit_file(dst, &res->write_res.verifier.verifier);
178 if (status)
179 return status;
182 truncate_pagecache_range(dst_inode, pos_dst,
183 pos_dst + res->write_res.count);
185 return res->write_res.count;
188 ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src,
189 struct file *dst, loff_t pos_dst,
190 size_t count)
192 struct nfs_server *server = NFS_SERVER(file_inode(dst));
193 struct nfs_lock_context *src_lock;
194 struct nfs_lock_context *dst_lock;
195 struct nfs42_copy_args args = {
196 .src_fh = NFS_FH(file_inode(src)),
197 .src_pos = pos_src,
198 .dst_fh = NFS_FH(file_inode(dst)),
199 .dst_pos = pos_dst,
200 .count = count,
202 struct nfs42_copy_res res;
203 struct nfs4_exception src_exception = {
204 .inode = file_inode(src),
205 .stateid = &args.src_stateid,
207 struct nfs4_exception dst_exception = {
208 .inode = file_inode(dst),
209 .stateid = &args.dst_stateid,
211 ssize_t err, err2;
213 if (!nfs_server_capable(file_inode(dst), NFS_CAP_COPY))
214 return -EOPNOTSUPP;
216 src_lock = nfs_get_lock_context(nfs_file_open_context(src));
217 if (IS_ERR(src_lock))
218 return PTR_ERR(src_lock);
220 src_exception.state = src_lock->open_context->state;
222 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst));
223 if (IS_ERR(dst_lock)) {
224 err = PTR_ERR(dst_lock);
225 goto out_put_src_lock;
228 dst_exception.state = dst_lock->open_context->state;
230 do {
231 inode_lock(file_inode(dst));
232 err = _nfs42_proc_copy(src, src_lock,
233 dst, dst_lock,
234 &args, &res);
235 inode_unlock(file_inode(dst));
237 if (err >= 0)
238 break;
239 if (err == -ENOTSUPP) {
240 err = -EOPNOTSUPP;
241 break;
244 err2 = nfs4_handle_exception(server, err, &src_exception);
245 err = nfs4_handle_exception(server, err, &dst_exception);
246 if (!err)
247 err = err2;
248 } while (src_exception.retry || dst_exception.retry);
250 nfs_put_lock_context(dst_lock);
251 out_put_src_lock:
252 nfs_put_lock_context(src_lock);
253 return err;
256 static loff_t _nfs42_proc_llseek(struct file *filep,
257 struct nfs_lock_context *lock, loff_t offset, int whence)
259 struct inode *inode = file_inode(filep);
260 struct nfs42_seek_args args = {
261 .sa_fh = NFS_FH(inode),
262 .sa_offset = offset,
263 .sa_what = (whence == SEEK_HOLE) ?
264 NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
266 struct nfs42_seek_res res;
267 struct rpc_message msg = {
268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
269 .rpc_argp = &args,
270 .rpc_resp = &res,
272 struct nfs_server *server = NFS_SERVER(inode);
273 int status;
275 if (!nfs_server_capable(inode, NFS_CAP_SEEK))
276 return -ENOTSUPP;
278 status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
279 lock, FMODE_READ);
280 if (status)
281 return status;
283 status = nfs_filemap_write_and_wait_range(inode->i_mapping,
284 offset, LLONG_MAX);
285 if (status)
286 return status;
288 status = nfs4_call_sync(server->client, server, &msg,
289 &args.seq_args, &res.seq_res, 0);
290 if (status == -ENOTSUPP)
291 server->caps &= ~NFS_CAP_SEEK;
292 if (status)
293 return status;
295 return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
298 loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
300 struct nfs_server *server = NFS_SERVER(file_inode(filep));
301 struct nfs4_exception exception = { };
302 struct nfs_lock_context *lock;
303 loff_t err;
305 lock = nfs_get_lock_context(nfs_file_open_context(filep));
306 if (IS_ERR(lock))
307 return PTR_ERR(lock);
309 exception.inode = file_inode(filep);
310 exception.state = lock->open_context->state;
312 do {
313 err = _nfs42_proc_llseek(filep, lock, offset, whence);
314 if (err >= 0)
315 break;
316 if (err == -ENOTSUPP) {
317 err = -EOPNOTSUPP;
318 break;
320 err = nfs4_handle_exception(server, err, &exception);
321 } while (exception.retry);
323 nfs_put_lock_context(lock);
324 return err;
328 static void
329 nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
331 struct nfs42_layoutstat_data *data = calldata;
332 struct inode *inode = data->inode;
333 struct nfs_server *server = NFS_SERVER(inode);
334 struct pnfs_layout_hdr *lo;
336 spin_lock(&inode->i_lock);
337 lo = NFS_I(inode)->layout;
338 if (!pnfs_layout_is_valid(lo)) {
339 spin_unlock(&inode->i_lock);
340 rpc_exit(task, 0);
341 return;
343 nfs4_stateid_copy(&data->args.stateid, &lo->plh_stateid);
344 spin_unlock(&inode->i_lock);
345 nfs41_setup_sequence(nfs4_get_session(server), &data->args.seq_args,
346 &data->res.seq_res, task);
350 static void
351 nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
353 struct nfs42_layoutstat_data *data = calldata;
354 struct inode *inode = data->inode;
355 struct pnfs_layout_hdr *lo;
357 if (!nfs4_sequence_done(task, &data->res.seq_res))
358 return;
360 switch (task->tk_status) {
361 case 0:
362 break;
363 case -NFS4ERR_EXPIRED:
364 case -NFS4ERR_ADMIN_REVOKED:
365 case -NFS4ERR_DELEG_REVOKED:
366 case -NFS4ERR_STALE_STATEID:
367 case -NFS4ERR_BAD_STATEID:
368 spin_lock(&inode->i_lock);
369 lo = NFS_I(inode)->layout;
370 if (pnfs_layout_is_valid(lo) &&
371 nfs4_stateid_match(&data->args.stateid,
372 &lo->plh_stateid)) {
373 LIST_HEAD(head);
376 * Mark the bad layout state as invalid, then retry
377 * with the current stateid.
379 pnfs_mark_layout_stateid_invalid(lo, &head);
380 spin_unlock(&inode->i_lock);
381 pnfs_free_lseg_list(&head);
382 } else
383 spin_unlock(&inode->i_lock);
384 break;
385 case -NFS4ERR_OLD_STATEID:
386 spin_lock(&inode->i_lock);
387 lo = NFS_I(inode)->layout;
388 if (pnfs_layout_is_valid(lo) &&
389 nfs4_stateid_match_other(&data->args.stateid,
390 &lo->plh_stateid)) {
391 /* Do we need to delay before resending? */
392 if (!nfs4_stateid_is_newer(&lo->plh_stateid,
393 &data->args.stateid))
394 rpc_delay(task, HZ);
395 rpc_restart_call_prepare(task);
397 spin_unlock(&inode->i_lock);
398 break;
399 case -ENOTSUPP:
400 case -EOPNOTSUPP:
401 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
404 dprintk("%s server returns %d\n", __func__, task->tk_status);
407 static void
408 nfs42_layoutstat_release(void *calldata)
410 struct nfs42_layoutstat_data *data = calldata;
411 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
413 if (nfss->pnfs_curr_ld->cleanup_layoutstats)
414 nfss->pnfs_curr_ld->cleanup_layoutstats(data);
416 pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
417 smp_mb__before_atomic();
418 clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
419 smp_mb__after_atomic();
420 nfs_iput_and_deactive(data->inode);
421 kfree(data->args.devinfo);
422 kfree(data);
425 static const struct rpc_call_ops nfs42_layoutstat_ops = {
426 .rpc_call_prepare = nfs42_layoutstat_prepare,
427 .rpc_call_done = nfs42_layoutstat_done,
428 .rpc_release = nfs42_layoutstat_release,
431 int nfs42_proc_layoutstats_generic(struct nfs_server *server,
432 struct nfs42_layoutstat_data *data)
434 struct rpc_message msg = {
435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
436 .rpc_argp = &data->args,
437 .rpc_resp = &data->res,
439 struct rpc_task_setup task_setup = {
440 .rpc_client = server->client,
441 .rpc_message = &msg,
442 .callback_ops = &nfs42_layoutstat_ops,
443 .callback_data = data,
444 .flags = RPC_TASK_ASYNC,
446 struct rpc_task *task;
448 data->inode = nfs_igrab_and_active(data->args.inode);
449 if (!data->inode) {
450 nfs42_layoutstat_release(data);
451 return -EAGAIN;
453 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
454 task = rpc_run_task(&task_setup);
455 if (IS_ERR(task))
456 return PTR_ERR(task);
457 rpc_put_task(task);
458 return 0;
461 static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
462 struct file *dst_f, struct nfs_lock_context *src_lock,
463 struct nfs_lock_context *dst_lock, loff_t src_offset,
464 loff_t dst_offset, loff_t count)
466 struct inode *src_inode = file_inode(src_f);
467 struct inode *dst_inode = file_inode(dst_f);
468 struct nfs_server *server = NFS_SERVER(dst_inode);
469 struct nfs42_clone_args args = {
470 .src_fh = NFS_FH(src_inode),
471 .dst_fh = NFS_FH(dst_inode),
472 .src_offset = src_offset,
473 .dst_offset = dst_offset,
474 .count = count,
475 .dst_bitmask = server->cache_consistency_bitmask,
477 struct nfs42_clone_res res = {
478 .server = server,
480 int status;
482 msg->rpc_argp = &args;
483 msg->rpc_resp = &res;
485 status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
486 src_lock, FMODE_READ);
487 if (status)
488 return status;
490 status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
491 dst_lock, FMODE_WRITE);
492 if (status)
493 return status;
495 res.dst_fattr = nfs_alloc_fattr();
496 if (!res.dst_fattr)
497 return -ENOMEM;
499 status = nfs4_call_sync(server->client, server, msg,
500 &args.seq_args, &res.seq_res, 0);
501 if (status == 0)
502 status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
504 kfree(res.dst_fattr);
505 return status;
508 int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
509 loff_t src_offset, loff_t dst_offset, loff_t count)
511 struct rpc_message msg = {
512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
514 struct inode *inode = file_inode(src_f);
515 struct nfs_server *server = NFS_SERVER(file_inode(src_f));
516 struct nfs_lock_context *src_lock;
517 struct nfs_lock_context *dst_lock;
518 struct nfs4_exception src_exception = { };
519 struct nfs4_exception dst_exception = { };
520 int err, err2;
522 if (!nfs_server_capable(inode, NFS_CAP_CLONE))
523 return -EOPNOTSUPP;
525 src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
526 if (IS_ERR(src_lock))
527 return PTR_ERR(src_lock);
529 src_exception.inode = file_inode(src_f);
530 src_exception.state = src_lock->open_context->state;
532 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f));
533 if (IS_ERR(dst_lock)) {
534 err = PTR_ERR(dst_lock);
535 goto out_put_src_lock;
538 dst_exception.inode = file_inode(dst_f);
539 dst_exception.state = dst_lock->open_context->state;
541 do {
542 err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
543 src_offset, dst_offset, count);
544 if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
545 NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
546 err = -EOPNOTSUPP;
547 break;
550 err2 = nfs4_handle_exception(server, err, &src_exception);
551 err = nfs4_handle_exception(server, err, &dst_exception);
552 if (!err)
553 err = err2;
554 } while (src_exception.retry || dst_exception.retry);
556 nfs_put_lock_context(dst_lock);
557 out_put_src_lock:
558 nfs_put_lock_context(src_lock);
559 return err;