powerpc/kprobes: Remove kretprobe_trampoline_holder.
[linux/fpc-iii.git] / fs / nfs / nfs42proc.c
blobaa03ed09ba06f79763146b0a6f8cb171c9778b0c
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 nfs_wb_all(inode);
117 inode_lock(inode);
119 err = nfs42_proc_fallocate(&msg, filep, offset, len);
120 if (err == 0)
121 truncate_pagecache_range(inode, offset, (offset + len) -1);
122 if (err == -EOPNOTSUPP)
123 NFS_SERVER(inode)->caps &= ~NFS_CAP_DEALLOCATE;
125 inode_unlock(inode);
126 return err;
129 static ssize_t _nfs42_proc_copy(struct file *src, loff_t pos_src,
130 struct nfs_lock_context *src_lock,
131 struct file *dst, loff_t pos_dst,
132 struct nfs_lock_context *dst_lock,
133 size_t count)
135 struct nfs42_copy_args args = {
136 .src_fh = NFS_FH(file_inode(src)),
137 .src_pos = pos_src,
138 .dst_fh = NFS_FH(file_inode(dst)),
139 .dst_pos = pos_dst,
140 .count = count,
142 struct nfs42_copy_res res;
143 struct rpc_message msg = {
144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COPY],
145 .rpc_argp = &args,
146 .rpc_resp = &res,
148 struct inode *dst_inode = file_inode(dst);
149 struct nfs_server *server = NFS_SERVER(dst_inode);
150 int status;
152 status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
153 src_lock, FMODE_READ);
154 if (status)
155 return status;
157 status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
158 dst_lock, FMODE_WRITE);
159 if (status)
160 return status;
162 status = nfs4_call_sync(server->client, server, &msg,
163 &args.seq_args, &res.seq_res, 0);
164 if (status == -ENOTSUPP)
165 server->caps &= ~NFS_CAP_COPY;
166 if (status)
167 return status;
169 if (res.write_res.verifier.committed != NFS_FILE_SYNC) {
170 status = nfs_commit_file(dst, &res.write_res.verifier.verifier);
171 if (status)
172 return status;
175 truncate_pagecache_range(dst_inode, pos_dst,
176 pos_dst + res.write_res.count);
178 return res.write_res.count;
181 ssize_t nfs42_proc_copy(struct file *src, loff_t pos_src,
182 struct file *dst, loff_t pos_dst,
183 size_t count)
185 struct nfs_server *server = NFS_SERVER(file_inode(dst));
186 struct nfs_lock_context *src_lock;
187 struct nfs_lock_context *dst_lock;
188 struct nfs4_exception src_exception = { };
189 struct nfs4_exception dst_exception = { };
190 ssize_t err, err2;
192 if (!nfs_server_capable(file_inode(dst), NFS_CAP_COPY))
193 return -EOPNOTSUPP;
195 src_lock = nfs_get_lock_context(nfs_file_open_context(src));
196 if (IS_ERR(src_lock))
197 return PTR_ERR(src_lock);
199 src_exception.inode = file_inode(src);
200 src_exception.state = src_lock->open_context->state;
202 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst));
203 if (IS_ERR(dst_lock)) {
204 err = PTR_ERR(dst_lock);
205 goto out_put_src_lock;
208 dst_exception.inode = file_inode(dst);
209 dst_exception.state = dst_lock->open_context->state;
211 do {
212 inode_lock(file_inode(dst));
213 err = _nfs42_proc_copy(src, pos_src, src_lock,
214 dst, pos_dst, dst_lock, count);
215 inode_unlock(file_inode(dst));
217 if (err == -ENOTSUPP) {
218 err = -EOPNOTSUPP;
219 break;
222 err2 = nfs4_handle_exception(server, err, &src_exception);
223 err = nfs4_handle_exception(server, err, &dst_exception);
224 if (!err)
225 err = err2;
226 } while (src_exception.retry || dst_exception.retry);
228 nfs_put_lock_context(dst_lock);
229 out_put_src_lock:
230 nfs_put_lock_context(src_lock);
231 return err;
234 static loff_t _nfs42_proc_llseek(struct file *filep,
235 struct nfs_lock_context *lock, loff_t offset, int whence)
237 struct inode *inode = file_inode(filep);
238 struct nfs42_seek_args args = {
239 .sa_fh = NFS_FH(inode),
240 .sa_offset = offset,
241 .sa_what = (whence == SEEK_HOLE) ?
242 NFS4_CONTENT_HOLE : NFS4_CONTENT_DATA,
244 struct nfs42_seek_res res;
245 struct rpc_message msg = {
246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEEK],
247 .rpc_argp = &args,
248 .rpc_resp = &res,
250 struct nfs_server *server = NFS_SERVER(inode);
251 int status;
253 if (!nfs_server_capable(inode, NFS_CAP_SEEK))
254 return -ENOTSUPP;
256 status = nfs4_set_rw_stateid(&args.sa_stateid, lock->open_context,
257 lock, FMODE_READ);
258 if (status)
259 return status;
261 nfs_wb_all(inode);
262 status = nfs4_call_sync(server->client, server, &msg,
263 &args.seq_args, &res.seq_res, 0);
264 if (status == -ENOTSUPP)
265 server->caps &= ~NFS_CAP_SEEK;
266 if (status)
267 return status;
269 return vfs_setpos(filep, res.sr_offset, inode->i_sb->s_maxbytes);
272 loff_t nfs42_proc_llseek(struct file *filep, loff_t offset, int whence)
274 struct nfs_server *server = NFS_SERVER(file_inode(filep));
275 struct nfs4_exception exception = { };
276 struct nfs_lock_context *lock;
277 loff_t err;
279 lock = nfs_get_lock_context(nfs_file_open_context(filep));
280 if (IS_ERR(lock))
281 return PTR_ERR(lock);
283 exception.inode = file_inode(filep);
284 exception.state = lock->open_context->state;
286 do {
287 err = _nfs42_proc_llseek(filep, lock, offset, whence);
288 if (err >= 0)
289 break;
290 if (err == -ENOTSUPP) {
291 err = -EOPNOTSUPP;
292 break;
294 err = nfs4_handle_exception(server, err, &exception);
295 } while (exception.retry);
297 nfs_put_lock_context(lock);
298 return err;
302 static void
303 nfs42_layoutstat_prepare(struct rpc_task *task, void *calldata)
305 struct nfs42_layoutstat_data *data = calldata;
306 struct nfs_server *server = NFS_SERVER(data->args.inode);
308 nfs41_setup_sequence(nfs4_get_session(server), &data->args.seq_args,
309 &data->res.seq_res, task);
312 static void
313 nfs42_layoutstat_done(struct rpc_task *task, void *calldata)
315 struct nfs42_layoutstat_data *data = calldata;
316 struct inode *inode = data->inode;
317 struct pnfs_layout_hdr *lo;
319 if (!nfs4_sequence_done(task, &data->res.seq_res))
320 return;
322 switch (task->tk_status) {
323 case 0:
324 break;
325 case -NFS4ERR_EXPIRED:
326 case -NFS4ERR_STALE_STATEID:
327 case -NFS4ERR_OLD_STATEID:
328 case -NFS4ERR_BAD_STATEID:
329 spin_lock(&inode->i_lock);
330 lo = NFS_I(inode)->layout;
331 if (lo && nfs4_stateid_match(&data->args.stateid,
332 &lo->plh_stateid)) {
333 LIST_HEAD(head);
336 * Mark the bad layout state as invalid, then retry
337 * with the current stateid.
339 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
340 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL, 0);
341 spin_unlock(&inode->i_lock);
342 pnfs_free_lseg_list(&head);
343 } else
344 spin_unlock(&inode->i_lock);
345 break;
346 case -ENOTSUPP:
347 case -EOPNOTSUPP:
348 NFS_SERVER(inode)->caps &= ~NFS_CAP_LAYOUTSTATS;
349 default:
350 break;
353 dprintk("%s server returns %d\n", __func__, task->tk_status);
356 static void
357 nfs42_layoutstat_release(void *calldata)
359 struct nfs42_layoutstat_data *data = calldata;
360 struct nfs_server *nfss = NFS_SERVER(data->args.inode);
362 if (nfss->pnfs_curr_ld->cleanup_layoutstats)
363 nfss->pnfs_curr_ld->cleanup_layoutstats(data);
365 pnfs_put_layout_hdr(NFS_I(data->args.inode)->layout);
366 smp_mb__before_atomic();
367 clear_bit(NFS_INO_LAYOUTSTATS, &NFS_I(data->args.inode)->flags);
368 smp_mb__after_atomic();
369 nfs_iput_and_deactive(data->inode);
370 kfree(data->args.devinfo);
371 kfree(data);
374 static const struct rpc_call_ops nfs42_layoutstat_ops = {
375 .rpc_call_prepare = nfs42_layoutstat_prepare,
376 .rpc_call_done = nfs42_layoutstat_done,
377 .rpc_release = nfs42_layoutstat_release,
380 int nfs42_proc_layoutstats_generic(struct nfs_server *server,
381 struct nfs42_layoutstat_data *data)
383 struct rpc_message msg = {
384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTSTATS],
385 .rpc_argp = &data->args,
386 .rpc_resp = &data->res,
388 struct rpc_task_setup task_setup = {
389 .rpc_client = server->client,
390 .rpc_message = &msg,
391 .callback_ops = &nfs42_layoutstat_ops,
392 .callback_data = data,
393 .flags = RPC_TASK_ASYNC,
395 struct rpc_task *task;
397 data->inode = nfs_igrab_and_active(data->args.inode);
398 if (!data->inode) {
399 nfs42_layoutstat_release(data);
400 return -EAGAIN;
402 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
403 task = rpc_run_task(&task_setup);
404 if (IS_ERR(task))
405 return PTR_ERR(task);
406 return 0;
409 static int _nfs42_proc_clone(struct rpc_message *msg, struct file *src_f,
410 struct file *dst_f, struct nfs_lock_context *src_lock,
411 struct nfs_lock_context *dst_lock, loff_t src_offset,
412 loff_t dst_offset, loff_t count)
414 struct inode *src_inode = file_inode(src_f);
415 struct inode *dst_inode = file_inode(dst_f);
416 struct nfs_server *server = NFS_SERVER(dst_inode);
417 struct nfs42_clone_args args = {
418 .src_fh = NFS_FH(src_inode),
419 .dst_fh = NFS_FH(dst_inode),
420 .src_offset = src_offset,
421 .dst_offset = dst_offset,
422 .count = count,
423 .dst_bitmask = server->cache_consistency_bitmask,
425 struct nfs42_clone_res res = {
426 .server = server,
428 int status;
430 msg->rpc_argp = &args;
431 msg->rpc_resp = &res;
433 status = nfs4_set_rw_stateid(&args.src_stateid, src_lock->open_context,
434 src_lock, FMODE_READ);
435 if (status)
436 return status;
438 status = nfs4_set_rw_stateid(&args.dst_stateid, dst_lock->open_context,
439 dst_lock, FMODE_WRITE);
440 if (status)
441 return status;
443 res.dst_fattr = nfs_alloc_fattr();
444 if (!res.dst_fattr)
445 return -ENOMEM;
447 status = nfs4_call_sync(server->client, server, msg,
448 &args.seq_args, &res.seq_res, 0);
449 if (status == 0)
450 status = nfs_post_op_update_inode(dst_inode, res.dst_fattr);
452 kfree(res.dst_fattr);
453 return status;
456 int nfs42_proc_clone(struct file *src_f, struct file *dst_f,
457 loff_t src_offset, loff_t dst_offset, loff_t count)
459 struct rpc_message msg = {
460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLONE],
462 struct inode *inode = file_inode(src_f);
463 struct nfs_server *server = NFS_SERVER(file_inode(src_f));
464 struct nfs_lock_context *src_lock;
465 struct nfs_lock_context *dst_lock;
466 struct nfs4_exception src_exception = { };
467 struct nfs4_exception dst_exception = { };
468 int err, err2;
470 if (!nfs_server_capable(inode, NFS_CAP_CLONE))
471 return -EOPNOTSUPP;
473 src_lock = nfs_get_lock_context(nfs_file_open_context(src_f));
474 if (IS_ERR(src_lock))
475 return PTR_ERR(src_lock);
477 src_exception.inode = file_inode(src_f);
478 src_exception.state = src_lock->open_context->state;
480 dst_lock = nfs_get_lock_context(nfs_file_open_context(dst_f));
481 if (IS_ERR(dst_lock)) {
482 err = PTR_ERR(dst_lock);
483 goto out_put_src_lock;
486 dst_exception.inode = file_inode(dst_f);
487 dst_exception.state = dst_lock->open_context->state;
489 do {
490 err = _nfs42_proc_clone(&msg, src_f, dst_f, src_lock, dst_lock,
491 src_offset, dst_offset, count);
492 if (err == -ENOTSUPP || err == -EOPNOTSUPP) {
493 NFS_SERVER(inode)->caps &= ~NFS_CAP_CLONE;
494 err = -EOPNOTSUPP;
495 break;
498 err2 = nfs4_handle_exception(server, err, &src_exception);
499 err = nfs4_handle_exception(server, err, &dst_exception);
500 if (!err)
501 err = err2;
502 } while (src_exception.retry || dst_exception.retry);
504 nfs_put_lock_context(dst_lock);
505 out_put_src_lock:
506 nfs_put_lock_context(src_lock);
507 return err;