xref: /openbmc/linux/fs/afs/fs_operation.c (revision cca37d45d547434144409ae648a19b7eb6db5eb4)
1e49c7b2fSDavid Howells // SPDX-License-Identifier: GPL-2.0-or-later
2e49c7b2fSDavid Howells /* Fileserver-directed operation handling.
3e49c7b2fSDavid Howells  *
4e49c7b2fSDavid Howells  * Copyright (C) 2020 Red Hat, Inc. All Rights Reserved.
5e49c7b2fSDavid Howells  * Written by David Howells (dhowells@redhat.com)
6e49c7b2fSDavid Howells  */
7e49c7b2fSDavid Howells 
8e49c7b2fSDavid Howells #include <linux/kernel.h>
9e49c7b2fSDavid Howells #include <linux/slab.h>
10e49c7b2fSDavid Howells #include <linux/fs.h>
11e49c7b2fSDavid Howells #include "internal.h"
12e49c7b2fSDavid Howells 
13e49c7b2fSDavid Howells static atomic_t afs_operation_debug_counter;
14e49c7b2fSDavid Howells 
15e49c7b2fSDavid Howells /*
16e49c7b2fSDavid Howells  * Create an operation against a volume.
17e49c7b2fSDavid Howells  */
18e49c7b2fSDavid Howells struct afs_operation *afs_alloc_operation(struct key *key, struct afs_volume *volume)
19e49c7b2fSDavid Howells {
20e49c7b2fSDavid Howells 	struct afs_operation *op;
21e49c7b2fSDavid Howells 
22e49c7b2fSDavid Howells 	_enter("");
23e49c7b2fSDavid Howells 
24e49c7b2fSDavid Howells 	op = kzalloc(sizeof(*op), GFP_KERNEL);
25e49c7b2fSDavid Howells 	if (!op)
26e49c7b2fSDavid Howells 		return ERR_PTR(-ENOMEM);
27e49c7b2fSDavid Howells 
28e49c7b2fSDavid Howells 	if (!key) {
29e49c7b2fSDavid Howells 		key = afs_request_key(volume->cell);
30e49c7b2fSDavid Howells 		if (IS_ERR(key)) {
31e49c7b2fSDavid Howells 			kfree(op);
32e49c7b2fSDavid Howells 			return ERR_CAST(key);
33e49c7b2fSDavid Howells 		}
34e49c7b2fSDavid Howells 	} else {
35e49c7b2fSDavid Howells 		key_get(key);
36e49c7b2fSDavid Howells 	}
37e49c7b2fSDavid Howells 
38e49c7b2fSDavid Howells 	op->key		= key;
39*cca37d45SDavid Howells 	op->volume	= afs_get_volume(volume, afs_volume_trace_get_new_op);
40e49c7b2fSDavid Howells 	op->net		= volume->cell->net;
41e49c7b2fSDavid Howells 	op->cb_v_break	= volume->cb_v_break;
42e49c7b2fSDavid Howells 	op->debug_id	= atomic_inc_return(&afs_operation_debug_counter);
43e49c7b2fSDavid Howells 	op->error	= -EDESTADDRREQ;
44e49c7b2fSDavid Howells 	op->ac.error	= SHRT_MAX;
45e49c7b2fSDavid Howells 
46e49c7b2fSDavid Howells 	_leave(" = [op=%08x]", op->debug_id);
47e49c7b2fSDavid Howells 	return op;
48e49c7b2fSDavid Howells }
49e49c7b2fSDavid Howells 
50e49c7b2fSDavid Howells /*
51e49c7b2fSDavid Howells  * Lock the vnode(s) being operated upon.
52e49c7b2fSDavid Howells  */
53e49c7b2fSDavid Howells static bool afs_get_io_locks(struct afs_operation *op)
54e49c7b2fSDavid Howells {
55e49c7b2fSDavid Howells 	struct afs_vnode *vnode = op->file[0].vnode;
56e49c7b2fSDavid Howells 	struct afs_vnode *vnode2 = op->file[1].vnode;
57e49c7b2fSDavid Howells 
58e49c7b2fSDavid Howells 	_enter("");
59e49c7b2fSDavid Howells 
60e49c7b2fSDavid Howells 	if (op->flags & AFS_OPERATION_UNINTR) {
61e49c7b2fSDavid Howells 		mutex_lock(&vnode->io_lock);
62e49c7b2fSDavid Howells 		op->flags |= AFS_OPERATION_LOCK_0;
63e49c7b2fSDavid Howells 		_leave(" = t [1]");
64e49c7b2fSDavid Howells 		return true;
65e49c7b2fSDavid Howells 	}
66e49c7b2fSDavid Howells 
67e49c7b2fSDavid Howells 	if (!vnode2 || !op->file[1].need_io_lock || vnode == vnode2)
68e49c7b2fSDavid Howells 		vnode2 = NULL;
69e49c7b2fSDavid Howells 
70e49c7b2fSDavid Howells 	if (vnode2 > vnode)
71e49c7b2fSDavid Howells 		swap(vnode, vnode2);
72e49c7b2fSDavid Howells 
73e49c7b2fSDavid Howells 	if (mutex_lock_interruptible(&vnode->io_lock) < 0) {
74e49c7b2fSDavid Howells 		op->error = -EINTR;
75e49c7b2fSDavid Howells 		op->flags |= AFS_OPERATION_STOP;
76e49c7b2fSDavid Howells 		_leave(" = f [I 0]");
77e49c7b2fSDavid Howells 		return false;
78e49c7b2fSDavid Howells 	}
79e49c7b2fSDavid Howells 	op->flags |= AFS_OPERATION_LOCK_0;
80e49c7b2fSDavid Howells 
81e49c7b2fSDavid Howells 	if (vnode2) {
82e49c7b2fSDavid Howells 		if (mutex_lock_interruptible_nested(&vnode2->io_lock, 1) < 0) {
83e49c7b2fSDavid Howells 			op->error = -EINTR;
84e49c7b2fSDavid Howells 			op->flags |= AFS_OPERATION_STOP;
85e49c7b2fSDavid Howells 			mutex_unlock(&vnode->io_lock);
86e49c7b2fSDavid Howells 			op->flags &= ~AFS_OPERATION_LOCK_0;
87e49c7b2fSDavid Howells 			_leave(" = f [I 1]");
88e49c7b2fSDavid Howells 			return false;
89e49c7b2fSDavid Howells 		}
90e49c7b2fSDavid Howells 		op->flags |= AFS_OPERATION_LOCK_1;
91e49c7b2fSDavid Howells 	}
92e49c7b2fSDavid Howells 
93e49c7b2fSDavid Howells 	_leave(" = t [2]");
94e49c7b2fSDavid Howells 	return true;
95e49c7b2fSDavid Howells }
96e49c7b2fSDavid Howells 
97e49c7b2fSDavid Howells static void afs_drop_io_locks(struct afs_operation *op)
98e49c7b2fSDavid Howells {
99e49c7b2fSDavid Howells 	struct afs_vnode *vnode = op->file[0].vnode;
100e49c7b2fSDavid Howells 	struct afs_vnode *vnode2 = op->file[1].vnode;
101e49c7b2fSDavid Howells 
102e49c7b2fSDavid Howells 	_enter("");
103e49c7b2fSDavid Howells 
104e49c7b2fSDavid Howells 	if (op->flags & AFS_OPERATION_LOCK_1)
105e49c7b2fSDavid Howells 		mutex_unlock(&vnode2->io_lock);
106e49c7b2fSDavid Howells 	if (op->flags & AFS_OPERATION_LOCK_0)
107e49c7b2fSDavid Howells 		mutex_unlock(&vnode->io_lock);
108e49c7b2fSDavid Howells }
109e49c7b2fSDavid Howells 
110e49c7b2fSDavid Howells static void afs_prepare_vnode(struct afs_operation *op, struct afs_vnode_param *vp,
111e49c7b2fSDavid Howells 			      unsigned int index)
112e49c7b2fSDavid Howells {
113e49c7b2fSDavid Howells 	struct afs_vnode *vnode = vp->vnode;
114e49c7b2fSDavid Howells 
115e49c7b2fSDavid Howells 	if (vnode) {
116e49c7b2fSDavid Howells 		vp->fid			= vnode->fid;
117e49c7b2fSDavid Howells 		vp->dv_before		= vnode->status.data_version;
118e49c7b2fSDavid Howells 		vp->cb_break_before	= afs_calc_vnode_cb_break(vnode);
119e49c7b2fSDavid Howells 		if (vnode->lock_state != AFS_VNODE_LOCK_NONE)
120e49c7b2fSDavid Howells 			op->flags	|= AFS_OPERATION_CUR_ONLY;
121e49c7b2fSDavid Howells 	}
122e49c7b2fSDavid Howells 
123e49c7b2fSDavid Howells 	if (vp->fid.vnode)
124e49c7b2fSDavid Howells 		_debug("PREP[%u] {%llx:%llu.%u}",
125e49c7b2fSDavid Howells 		       index, vp->fid.vid, vp->fid.vnode, vp->fid.unique);
126e49c7b2fSDavid Howells }
127e49c7b2fSDavid Howells 
128e49c7b2fSDavid Howells /*
129e49c7b2fSDavid Howells  * Begin an operation on the fileserver.
130e49c7b2fSDavid Howells  *
131e49c7b2fSDavid Howells  * Fileserver operations are serialised on the server by vnode, so we serialise
132e49c7b2fSDavid Howells  * them here also using the io_lock.
133e49c7b2fSDavid Howells  */
134e49c7b2fSDavid Howells bool afs_begin_vnode_operation(struct afs_operation *op)
135e49c7b2fSDavid Howells {
136e49c7b2fSDavid Howells 	struct afs_vnode *vnode = op->file[0].vnode;
137e49c7b2fSDavid Howells 
138e49c7b2fSDavid Howells 	ASSERT(vnode);
139e49c7b2fSDavid Howells 
140e49c7b2fSDavid Howells 	_enter("");
141e49c7b2fSDavid Howells 
142e49c7b2fSDavid Howells 	if (op->file[0].need_io_lock)
143e49c7b2fSDavid Howells 		if (!afs_get_io_locks(op))
144e49c7b2fSDavid Howells 			return false;
145e49c7b2fSDavid Howells 
146e49c7b2fSDavid Howells 	read_seqlock_excl(&vnode->cb_lock);
147e49c7b2fSDavid Howells 	op->cbi = afs_get_cb_interest(
148e49c7b2fSDavid Howells 		rcu_dereference_protected(vnode->cb_interest,
149e49c7b2fSDavid Howells 					  lockdep_is_held(&vnode->cb_lock.lock)));
150e49c7b2fSDavid Howells 	read_sequnlock_excl(&vnode->cb_lock);
151e49c7b2fSDavid Howells 
152e49c7b2fSDavid Howells 	afs_prepare_vnode(op, &op->file[0], 0);
153e49c7b2fSDavid Howells 	afs_prepare_vnode(op, &op->file[1], 1);
154e49c7b2fSDavid Howells 	op->cb_v_break = op->volume->cb_v_break;
155e49c7b2fSDavid Howells 	_leave(" = true");
156e49c7b2fSDavid Howells 	return true;
157e49c7b2fSDavid Howells }
158e49c7b2fSDavid Howells 
159e49c7b2fSDavid Howells /*
160e49c7b2fSDavid Howells  * Tidy up a filesystem cursor and unlock the vnode.
161e49c7b2fSDavid Howells  */
162e49c7b2fSDavid Howells static void afs_end_vnode_operation(struct afs_operation *op)
163e49c7b2fSDavid Howells {
164e49c7b2fSDavid Howells 	_enter("");
165e49c7b2fSDavid Howells 
166e49c7b2fSDavid Howells 	if (op->error == -EDESTADDRREQ ||
167e49c7b2fSDavid Howells 	    op->error == -EADDRNOTAVAIL ||
168e49c7b2fSDavid Howells 	    op->error == -ENETUNREACH ||
169e49c7b2fSDavid Howells 	    op->error == -EHOSTUNREACH)
170e49c7b2fSDavid Howells 		afs_dump_edestaddrreq(op);
171e49c7b2fSDavid Howells 
172e49c7b2fSDavid Howells 	afs_drop_io_locks(op);
173e49c7b2fSDavid Howells 
174e49c7b2fSDavid Howells 	if (op->error == -ECONNABORTED)
175e49c7b2fSDavid Howells 		op->error = afs_abort_to_error(op->ac.abort_code);
176e49c7b2fSDavid Howells }
177e49c7b2fSDavid Howells 
178e49c7b2fSDavid Howells /*
179e49c7b2fSDavid Howells  * Wait for an in-progress operation to complete.
180e49c7b2fSDavid Howells  */
181e49c7b2fSDavid Howells void afs_wait_for_operation(struct afs_operation *op)
182e49c7b2fSDavid Howells {
183e49c7b2fSDavid Howells 	_enter("");
184e49c7b2fSDavid Howells 
185e49c7b2fSDavid Howells 	while (afs_select_fileserver(op)) {
186e49c7b2fSDavid Howells 		op->cb_s_break = op->cbi->server->cb_s_break;
187e49c7b2fSDavid Howells 		if (test_bit(AFS_SERVER_FL_IS_YFS, &op->cbi->server->flags) &&
188e49c7b2fSDavid Howells 		    op->ops->issue_yfs_rpc)
189e49c7b2fSDavid Howells 			op->ops->issue_yfs_rpc(op);
190e49c7b2fSDavid Howells 		else
191e49c7b2fSDavid Howells 			op->ops->issue_afs_rpc(op);
192e49c7b2fSDavid Howells 
193e49c7b2fSDavid Howells 		op->error = afs_wait_for_call_to_complete(op->call, &op->ac);
194e49c7b2fSDavid Howells 	}
195e49c7b2fSDavid Howells 
196e49c7b2fSDavid Howells 	if (op->error == 0) {
197e49c7b2fSDavid Howells 		_debug("success");
198e49c7b2fSDavid Howells 		op->ops->success(op);
199e49c7b2fSDavid Howells 	}
200e49c7b2fSDavid Howells 
201e49c7b2fSDavid Howells 	afs_end_vnode_operation(op);
202e49c7b2fSDavid Howells 
203e49c7b2fSDavid Howells 	if (op->error == 0 && op->ops->edit_dir) {
204e49c7b2fSDavid Howells 		_debug("edit_dir");
205e49c7b2fSDavid Howells 		op->ops->edit_dir(op);
206e49c7b2fSDavid Howells 	}
207e49c7b2fSDavid Howells 	_leave("");
208e49c7b2fSDavid Howells }
209e49c7b2fSDavid Howells 
210e49c7b2fSDavid Howells /*
211e49c7b2fSDavid Howells  * Dispose of an operation.
212e49c7b2fSDavid Howells  */
213e49c7b2fSDavid Howells int afs_put_operation(struct afs_operation *op)
214e49c7b2fSDavid Howells {
215e49c7b2fSDavid Howells 	int i, ret = op->error;
216e49c7b2fSDavid Howells 
217e49c7b2fSDavid Howells 	_enter("op=%08x,%d", op->debug_id, ret);
218e49c7b2fSDavid Howells 
219e49c7b2fSDavid Howells 	if (op->ops && op->ops->put)
220e49c7b2fSDavid Howells 		op->ops->put(op);
221e49c7b2fSDavid Howells 	if (op->file[0].put_vnode)
222e49c7b2fSDavid Howells 		iput(&op->file[0].vnode->vfs_inode);
223e49c7b2fSDavid Howells 	if (op->file[1].put_vnode)
224e49c7b2fSDavid Howells 		iput(&op->file[1].vnode->vfs_inode);
225e49c7b2fSDavid Howells 
226e49c7b2fSDavid Howells 	if (op->more_files) {
227e49c7b2fSDavid Howells 		for (i = 0; i < op->nr_files - 2; i++)
228e49c7b2fSDavid Howells 			if (op->more_files[i].put_vnode)
229e49c7b2fSDavid Howells 				iput(&op->more_files[i].vnode->vfs_inode);
230e49c7b2fSDavid Howells 		kfree(op->more_files);
231e49c7b2fSDavid Howells 	}
232e49c7b2fSDavid Howells 
233e49c7b2fSDavid Howells 	afs_end_cursor(&op->ac);
234e49c7b2fSDavid Howells 	afs_put_cb_interest(op->net, op->cbi);
235e49c7b2fSDavid Howells 	afs_put_serverlist(op->net, op->server_list);
236*cca37d45SDavid Howells 	afs_put_volume(op->net, op->volume, afs_volume_trace_put_put_op);
237e49c7b2fSDavid Howells 	kfree(op);
238e49c7b2fSDavid Howells 	return ret;
239e49c7b2fSDavid Howells }
240e49c7b2fSDavid Howells 
241e49c7b2fSDavid Howells int afs_do_sync_operation(struct afs_operation *op)
242e49c7b2fSDavid Howells {
243e49c7b2fSDavid Howells 	afs_begin_vnode_operation(op);
244e49c7b2fSDavid Howells 	afs_wait_for_operation(op);
245e49c7b2fSDavid Howells 	return afs_put_operation(op);
246e49c7b2fSDavid Howells }
247