aboutsummaryrefslogtreecommitdiff
path: root/sys/fs/p9fs/p9fs_subr.c
blob: d0f04f6c5e97417243e37eb33e32d52402d4f5a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*-
 * Copyright (c) 2017 Juniper Networks, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
/*-
 * 9P filesystem subroutines. This file consists of all the Non VFS subroutines.
 * It contains all of the functions related to the driver submission which form
 * the upper layer i.e, p9fs driver. This will interact with the client to make
 * sure we have correct API calls in the header.
 */

#include <sys/cdefs.h>
#include <sys/systm.h>
#include <sys/limits.h>
#include <sys/mount.h>
#include <sys/sysctl.h>
#include <sys/vnode.h>

#include "p9fs_proto.h"

#include <fs/p9fs/p9_client.h>
#include <fs/p9fs/p9_debug.h>
#include <fs/p9fs/p9_protocol.h>
#include <fs/p9fs/p9fs.h>

int
p9fs_proto_dotl(struct p9fs_session *vses)
{

	return (vses->flags & P9FS_PROTO_2000L);
}

/* Initialize a p9fs session */
struct p9_fid *
p9fs_init_session(struct mount *mp, int *error)
{
	struct p9fs_session *vses;
	struct p9fs_mount *virtmp;
	struct p9_fid *fid;
	char *access;

	virtmp = VFSTOP9(mp);
	vses = &virtmp->p9fs_session;
	vses->uid = P9_NONUNAME;
	vses->uname = P9_DEFUNAME;
	vses->aname = P9_DEFANAME;

	/*
	 * Create the client structure. Call into the driver to create
	 * driver structures for the actual IO transfer.
	 */
	vses->clnt = p9_client_create(mp, error, virtmp->mount_tag);

	if (vses->clnt == NULL) {
		P9_DEBUG(ERROR, "%s: p9_client_create failed\n", __func__);
		return (NULL);
	}
	/*
	 * Find the client version and cache the copy. We will use this copy
	 * throughout FS layer.
	 */
	if (p9_is_proto_dotl(vses->clnt))
		vses->flags |= P9FS_PROTO_2000L;
	else if (p9_is_proto_dotu(vses->clnt))
		vses->flags |= P9FS_PROTO_2000U;

	/* Set the access mode */
	access = vfs_getopts(mp->mnt_optnew, "access", error);
	if (access == NULL)
		vses->flags |= P9_ACCESS_USER;
	else if (!strcmp(access, "any"))
		vses->flags |= P9_ACCESS_ANY;
	else if (!strcmp(access, "single"))
		vses->flags |= P9_ACCESS_SINGLE;
	else if (!strcmp(access, "user"))
		vses->flags |= P9_ACCESS_USER;
	else {
		P9_DEBUG(ERROR, "%s: unknown access mode\n", __func__);
		*error = EINVAL;
		goto out;
	}

	*error = 0;
	/* Attach with the backend host*/
	fid = p9_client_attach(vses->clnt, NULL, vses->uname, P9_NONUNAME,
	    vses->aname, error);
	vses->mnt_fid = fid;

	if (*error != 0) {
		P9_DEBUG(ERROR, "%s: attach failed: %d\n", __func__, *error);
		goto out;
	}
	P9_DEBUG(SUBR, "%s: attach successful fid :%p\n", __func__, fid);
	fid->uid = vses->uid;

	/* initialize the node list for the session */
	STAILQ_INIT(&vses->virt_node_list);
	P9FS_LOCK_INIT(vses);

	P9_DEBUG(SUBR, "%s: INIT session successful\n", __func__);

	return (fid);
out:
	p9_client_destroy(vses->clnt);
	return (NULL);
}

/* Begin to terminate a session */
void
p9fs_prepare_to_close(struct mount *mp)
{
	struct p9fs_session *vses;
	struct p9fs_mount *vmp;
	struct p9fs_node *np, *pnp, *tmp;

	vmp = VFSTOP9(mp);
	vses = &vmp->p9fs_session;

	/* break the node->parent references */
	STAILQ_FOREACH_SAFE(np, &vses->virt_node_list, p9fs_node_next, tmp) {
		if (np->parent && np->parent != np) {
			pnp = np->parent;
			np->parent = NULL;
			vrele(P9FS_NTOV(pnp));
		}
	}

	/* We are about to teardown, we dont allow anything other than clunk after this.*/
	p9_client_begin_disconnect(vses->clnt);
}

/* Shutdown a session */
void
p9fs_complete_close(struct mount *mp)
{
	struct p9fs_session *vses;
	struct p9fs_mount *vmp;

	vmp = VFSTOP9(mp);
	vses = &vmp->p9fs_session;

	/* Finish the close*/
	p9_client_disconnect(vses->clnt);
}


/* Call from unmount. Close the session. */
void
p9fs_close_session(struct mount *mp)
{
	struct p9fs_session *vses;
	struct p9fs_mount *vmp;

	vmp = VFSTOP9(mp);
	vses = &vmp->p9fs_session;

	p9fs_complete_close(mp);
	/* Clean up the clnt structure. */
	p9_client_destroy(vses->clnt);
	P9FS_LOCK_DESTROY(vses);
	P9_DEBUG(SUBR, "%s: Clean close session .\n", __func__);
}

/*
 * Remove all the fids of a particular type from a p9fs node
 * as well as destroy/clunk them.
 */
void
p9fs_fid_remove_all(struct p9fs_node *np, int leave_ofids)
{
	struct p9_fid *fid, *tfid;

	STAILQ_FOREACH_SAFE(fid, &np->vfid_list, fid_next, tfid) {
		STAILQ_REMOVE(&np->vfid_list, fid, p9_fid, fid_next);
		p9_client_clunk(fid);
	}

	if (!leave_ofids) {
		STAILQ_FOREACH_SAFE(fid, &np->vofid_list, fid_next, tfid) {
			STAILQ_REMOVE(&np->vofid_list, fid, p9_fid, fid_next);
			p9_client_clunk(fid);
		}
	}
}


/* Remove a fid from its corresponding fid list */
void
p9fs_fid_remove(struct p9fs_node *np, struct p9_fid *fid, int fid_type)
{

	switch (fid_type) {
	case VFID:
		P9FS_VFID_LOCK(np);
		STAILQ_REMOVE(&np->vfid_list, fid, p9_fid, fid_next);
		P9FS_VFID_UNLOCK(np);
		break;
	case VOFID:
		P9FS_VOFID_LOCK(np);
		STAILQ_REMOVE(&np->vofid_list, fid, p9_fid, fid_next);
		P9FS_VOFID_UNLOCK(np);
		break;
	}
}

/* Add a fid to the corresponding fid list */
void
p9fs_fid_add(struct p9fs_node *np, struct p9_fid *fid, int fid_type)
{

	switch (fid_type) {
	case VFID:
		P9FS_VFID_LOCK(np);
		STAILQ_INSERT_TAIL(&np->vfid_list, fid, fid_next);
		P9FS_VFID_UNLOCK(np);
		break;
	case VOFID:
		P9FS_VOFID_LOCK(np);
		STAILQ_INSERT_TAIL(&np->vofid_list, fid, fid_next);
		P9FS_VOFID_UNLOCK(np);
		break;
	}
}

/* Build the path from root to current directory */
static int
p9fs_get_full_path(struct p9fs_node *np, char ***names)
{
	int i, n;
	struct p9fs_node *node;
	char **wnames;

	n = 0;
	for (node = np ; (node != NULL) && !IS_ROOT(node) ; node = node->parent)
		n++;

	if (node == NULL)
		return (0);

	wnames = malloc(n * sizeof(char *), M_TEMP, M_ZERO|M_WAITOK);

	for (i = n-1, node = np; i >= 0 ; i--, node = node->parent)
		wnames[i] = node->inode.i_name;

	*names = wnames;
	return (n);
}

/*
 * Return TRUE if this fid can be used for the requested mode.
 */
static int
p9fs_compatible_mode(struct p9_fid *fid, int mode)
{
	/*
	 * Return TRUE for an exact match. For OREAD and OWRITE, allow
	 * existing ORDWR fids to match. Only check the low two bits
	 * of mode.
	 *
	 * TODO: figure out if this is correct for O_APPEND
	 */
	int fid_mode = fid->mode & 3;
	if (fid_mode == mode)
		return (TRUE);
	if (fid_mode == P9PROTO_ORDWR)
		return (mode == P9PROTO_OREAD || mode == P9PROTO_OWRITE);
	return (FALSE);
}

/*
 * Retrieve fid structure corresponding to a particular
 * uid and fid type for a p9fs node
 */
static struct p9_fid *
p9fs_get_fid_from_uid(struct p9fs_node *np, uid_t uid, int fid_type, int mode)
{
	struct p9_fid *fid;

	switch (fid_type) {
	case VFID:
		P9FS_VFID_LOCK(np);
		STAILQ_FOREACH(fid, &np->vfid_list, fid_next) {
			if (fid->uid == uid) {
				P9FS_VFID_UNLOCK(np);
				return (fid);
			}
		}
		P9FS_VFID_UNLOCK(np);
		break;
	case VOFID:
		P9FS_VOFID_LOCK(np);
		STAILQ_FOREACH(fid, &np->vofid_list, fid_next) {
			if (fid->uid == uid && p9fs_compatible_mode(fid, mode)) {
				P9FS_VOFID_UNLOCK(np);
				return (fid);
			}
		}
		P9FS_VOFID_UNLOCK(np);
		break;
	}

	return (NULL);
}

/*
 * Function returns the fid sturcture for a file corresponding to current user id.
 * First it searches in the fid list of the corresponding p9fs node.
 * New fid will be created if not already present and added in the corresponding
 * fid list in the p9fs node.
 * If the user is not already attached then this will attach the user first
 * and then create a new fid for this particular file by doing dir walk.
 */
struct p9_fid *
p9fs_get_fid(struct p9_client *clnt, struct p9fs_node *np, struct ucred *cred,
    int fid_type, int mode, int *error)
{
	uid_t uid;
	struct p9_fid *fid, *oldfid;
	struct p9fs_node *root;
	struct p9fs_session *vses;
	int i, l, clone;
	char **wnames = NULL;
	uint16_t nwnames;

	oldfid = NULL;
	vses = np->p9fs_ses;

	if (vses->flags & P9_ACCESS_ANY)
		uid = vses->uid;
	else if (cred)
		uid = cred->cr_uid;
	else
		uid = 0;

	/*
	 * Search for the fid in corresponding fid list.
	 * We should return NULL for VOFID if it is not present in the list.
	 * Because VOFID should have been created during the file open.
	 * If VFID is not present in the list then we should create one.
	 */
	fid = p9fs_get_fid_from_uid(np, uid, fid_type, mode);
	if (fid != NULL || fid_type == VOFID)
		return (fid);

	/* Check root if the user is attached */
	root = &np->p9fs_ses->rnp;
	fid = p9fs_get_fid_from_uid(root, uid, fid_type, mode);
	if(fid == NULL) {
		/* Attach the user */
		fid = p9_client_attach(clnt, NULL, NULL, uid,
		    vses->aname, error);
		if (*error != 0)
			return (NULL);
		p9fs_fid_add(root, fid, fid_type);
	}

	/* If we are looking for root then return it */
	if (IS_ROOT(np))
		return (fid);

	/* Get full path from root to p9fs node */
	nwnames = p9fs_get_full_path(np, &wnames);

	/*
	 * Could not get full path.
	 * If p9fs node is not deleted, parent should exist.
	 */
	KASSERT(nwnames != 0, ("%s: Directory of %s doesn't exist", __func__, np->inode.i_name));

	clone = 1;
	i = 0;
	while (i < nwnames) {
		l = MIN(nwnames - i, P9_MAXWELEM);

		fid = p9_client_walk(fid, l, wnames, clone, error);
		if (*error != 0) {
			if (oldfid)
				p9_client_clunk(oldfid);
			fid = NULL;
			goto bail_out;
		}
		oldfid = fid;
		clone = 0;
		i += l ;
	}
	p9fs_fid_add(np, fid, fid_type);
bail_out:
	free(wnames, M_TEMP);
	return (fid);
}