Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*-
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright (c) 2001 Dag-Erling Coïdan Smørgrav
 * 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
 *    in this position and unchanged.
 * 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.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * 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.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "opt_pseudofs.h"

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mount.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/sbuf.h>
#include <sys/sysctl.h>
#include <sys/vnode.h>

#include <fs/pseudofs/pseudofs.h>
#include <fs/pseudofs/pseudofs_internal.h>

static MALLOC_DEFINE(M_PFSNODES, "pfs_nodes", "pseudofs nodes");

SYSCTL_NODE(_vfs, OID_AUTO, pfs, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
    "pseudofs");

#ifdef PSEUDOFS_TRACE
int pfs_trace;
SYSCTL_INT(_vfs_pfs, OID_AUTO, trace, CTLFLAG_RW, &pfs_trace, 0,
    "enable tracing of pseudofs vnode operations");
#endif

#if PFS_FSNAMELEN != MFSNAMELEN
#error "PFS_FSNAMELEN is not equal to MFSNAMELEN"
#endif

/*
 * Allocate and initialize a node
 */
static struct pfs_node *
pfs_alloc_node_flags(struct pfs_info *pi, const char *name, pfs_type_t type, int flags)
{
	struct pfs_node *pn;
	int malloc_flags;

	KASSERT(strlen(name) < PFS_NAMELEN,
	    ("%s(): node name is too long", __func__));
	if (flags & PFS_NOWAIT)
		malloc_flags = M_NOWAIT | M_ZERO;
	else
		malloc_flags = M_WAITOK | M_ZERO;
	pn = malloc(sizeof *pn, M_PFSNODES, malloc_flags);
	if (pn == NULL)
		return (NULL);
	mtx_init(&pn->pn_mutex, "pfs_node", NULL, MTX_DEF | MTX_DUPOK);
	strlcpy(pn->pn_name, name, sizeof pn->pn_name);
	pn->pn_type = type;
	pn->pn_info = pi;
	return (pn);
}

static struct pfs_node *
pfs_alloc_node(struct pfs_info *pi, const char *name, pfs_type_t type)
{
	return (pfs_alloc_node_flags(pi, name, type, 0));
}

/*
 * Add a node to a directory
 */
static void
pfs_add_node(struct pfs_node *parent, struct pfs_node *pn)
{
#ifdef INVARIANTS
	struct pfs_node *iter;
#endif

	KASSERT(parent != NULL,
	    ("%s(): parent is NULL", __func__));
	KASSERT(pn->pn_parent == NULL,
	    ("%s(): node already has a parent", __func__));
	KASSERT(parent->pn_info != NULL,
	    ("%s(): parent has no pn_info", __func__));
	KASSERT(parent->pn_type == pfstype_dir ||
	    parent->pn_type == pfstype_procdir ||
	    parent->pn_type == pfstype_root,
	    ("%s(): parent is not a directory", __func__));

#ifdef INVARIANTS
	/* XXX no locking! */
	if (pn->pn_type == pfstype_procdir)
		for (iter = parent; iter != NULL; iter = iter->pn_parent)
			KASSERT(iter->pn_type != pfstype_procdir,
			    ("%s(): nested process directories", __func__));
	for (iter = parent->pn_nodes; iter != NULL; iter = iter->pn_next) {
		KASSERT(strcmp(pn->pn_name, iter->pn_name) != 0,
		    ("%s(): homonymous siblings", __func__));
		if (pn->pn_type == pfstype_procdir)
			KASSERT(iter->pn_type != pfstype_procdir,
			    ("%s(): sibling process directories", __func__));
	}
#endif

	pn->pn_parent = parent;
	pfs_fileno_alloc(pn);

	pfs_lock(parent);
	if ((parent->pn_flags & PFS_PROCDEP) != 0)
		pn->pn_flags |= PFS_PROCDEP;
	if (parent->pn_nodes == NULL) {
		KASSERT(parent->pn_last_node == NULL,
		    ("%s(): pn_last_node not NULL", __func__));
		parent->pn_nodes = pn;
		parent->pn_last_node = pn;
	} else {
		KASSERT(parent->pn_last_node != NULL,
		    ("%s(): pn_last_node is NULL", __func__));
		KASSERT(parent->pn_last_node->pn_next == NULL,
		    ("%s(): pn_last_node->pn_next not NULL", __func__));
		parent->pn_last_node->pn_next = pn;
		parent->pn_last_node = pn;
	}
	pfs_unlock(parent);
}

/*
 * Detach a node from its aprent
 */
static void
pfs_detach_node(struct pfs_node *pn)
{
	struct pfs_node *node, *parent = pn->pn_parent;
	struct pfs_node **iter;

	KASSERT(parent != NULL, ("%s(): node has no parent", __func__));
	KASSERT(parent->pn_info == pn->pn_info,
	    ("%s(): parent has different pn_info", __func__));

	pfs_lock(parent);
	if (pn == parent->pn_last_node) {
		if (pn == pn->pn_nodes) {
			parent->pn_last_node = NULL;
		} else {
			for (node = parent->pn_nodes;
			    node->pn_next != pn; node = node->pn_next)
				continue;
			parent->pn_last_node = node;
		}
	}
	iter = &parent->pn_nodes;
	while (*iter != NULL) {
		if (*iter == pn) {
			*iter = pn->pn_next;
			break;
		}
		iter = &(*iter)->pn_next;
	}
	pn->pn_parent = NULL;
	pfs_unlock(parent);
}

/*
 * Add . and .. to a directory
 */
static int
pfs_fixup_dir_flags(struct pfs_node *parent, int flags)
{
	struct pfs_node *dot, *dotdot;

	dot = pfs_alloc_node_flags(parent->pn_info, ".", pfstype_this, flags);
	if (dot == NULL)
		return (ENOMEM);
	dotdot = pfs_alloc_node_flags(parent->pn_info, "..", pfstype_parent, flags);
	if (dotdot == NULL) {
		pfs_destroy(dot);
		return (ENOMEM);
	}
	pfs_add_node(parent, dot);
	pfs_add_node(parent, dotdot);
	return (0);
}

static void
pfs_fixup_dir(struct pfs_node *parent)
{

	pfs_fixup_dir_flags(parent, 0);
}

/*
 * Create a directory
 */
struct pfs_node	*
pfs_create_dir(struct pfs_node *parent, const char *name,
	       pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
	       int flags)
{
	struct pfs_node *pn;
	int rc;

	pn = pfs_alloc_node_flags(parent->pn_info, name,
			 (flags & PFS_PROCDEP) ? pfstype_procdir : pfstype_dir, flags);
	if (pn == NULL)
		return (NULL);
	pn->pn_attr = attr;
	pn->pn_vis = vis;
	pn->pn_destroy = destroy;
	pn->pn_flags = flags;
	pfs_add_node(parent, pn);
	rc = pfs_fixup_dir_flags(pn, flags);
	if (rc) {
		pfs_destroy(pn);
		return (NULL);
	}
	return (pn);
}

/*
 * Create a file
 */
struct pfs_node	*
pfs_create_file(struct pfs_node *parent, const char *name, pfs_fill_t fill,
		pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
		int flags)
{
	struct pfs_node *pn;

	pn = pfs_alloc_node_flags(parent->pn_info, name, pfstype_file, flags);
	if (pn == NULL)
		return (NULL);
	pn->pn_fill = fill;
	pn->pn_attr = attr;
	pn->pn_vis = vis;
	pn->pn_destroy = destroy;
	pn->pn_flags = flags;
	pfs_add_node(parent, pn);

	return (pn);
}

/*
 * Create a symlink
 */
struct pfs_node	*
pfs_create_link(struct pfs_node *parent, const char *name, pfs_fill_t fill,
		pfs_attr_t attr, pfs_vis_t vis, pfs_destroy_t destroy,
		int flags)
{
	struct pfs_node *pn;

	pn = pfs_alloc_node_flags(parent->pn_info, name, pfstype_symlink, flags);
	if (pn == NULL)
		return (NULL);
	pn->pn_fill = fill;
	pn->pn_attr = attr;
	pn->pn_vis = vis;
	pn->pn_destroy = destroy;
	pn->pn_flags = flags;
	pfs_add_node(parent, pn);

	return (pn);
}

/*
 * Locate a node by name
 */
struct pfs_node *
pfs_find_node(struct pfs_node *parent, const char *name)
{
	struct pfs_node *pn;

	pfs_lock(parent);
	for (pn = parent->pn_nodes; pn != NULL; pn = pn->pn_next)
		if (strcmp(pn->pn_name, name) == 0)
			break;
	pfs_unlock(parent);
	return (pn);
}

/*
 * Destroy a node and all its descendants.  If the node to be destroyed
 * has a parent, the parent's mutex must be held.
 */
int
pfs_destroy(struct pfs_node *pn)
{
	struct pfs_node *iter;

	KASSERT(pn != NULL,
	    ("%s(): node is NULL", __func__));
	KASSERT(pn->pn_info != NULL,
	    ("%s(): node has no pn_info", __func__));

	if (pn->pn_parent)
		pfs_detach_node(pn);

	/* destroy children */
	if (pn->pn_type == pfstype_dir ||
	    pn->pn_type == pfstype_procdir ||
	    pn->pn_type == pfstype_root) {
		pfs_lock(pn);
		while (pn->pn_nodes != NULL) {
			iter = pn->pn_nodes;
			pn->pn_nodes = iter->pn_next;
			iter->pn_parent = NULL;
			pfs_unlock(pn);
			pfs_destroy(iter);
			pfs_lock(pn);
		}
		pfs_unlock(pn);
	}

	/* revoke vnodes and fileno */
	pfs_purge(pn);

	/* callback to free any private resources */
	if (pn->pn_destroy != NULL)
		pn_destroy(pn);

	/* destroy the node */
	pfs_fileno_free(pn);
	mtx_destroy(&pn->pn_mutex);
	free(pn, M_PFSNODES);

	return (0);
}

/*
 * Mount a pseudofs instance
 */
int
pfs_mount(struct pfs_info *pi, struct mount *mp)
{
	struct statfs *sbp;

	if (mp->mnt_flag & MNT_UPDATE)
		return (EOPNOTSUPP);

	MNT_ILOCK(mp);
	mp->mnt_flag |= MNT_LOCAL;
	mp->mnt_kern_flag |= MNTK_NOMSYNC;
	MNT_IUNLOCK(mp);
	mp->mnt_data = pi;
	vfs_getnewfsid(mp);

	sbp = &mp->mnt_stat;
	vfs_mountedfrom(mp, pi->pi_name);
	sbp->f_bsize = PAGE_SIZE;
	sbp->f_iosize = PAGE_SIZE;
	sbp->f_blocks = 1;
	sbp->f_bfree = 0;
	sbp->f_bavail = 0;
	sbp->f_files = 1;
	sbp->f_ffree = 0;

	return (0);
}

/*
 * Compatibility shim for old mount(2) system call
 */
int
pfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
{
	int error;

	error = kernel_mount(ma, flags);
	return (error);
}

/*
 * Unmount a pseudofs instance
 */
int
pfs_unmount(struct mount *mp, int mntflags)
{
	int error;

	error = vflush(mp, 0, (mntflags & MNT_FORCE) ?  FORCECLOSE : 0,
	    curthread);
	return (error);
}

/*
 * Return a root vnode
 */
int
pfs_root(struct mount *mp, int flags, struct vnode **vpp)
{
	struct pfs_info *pi;

	pi = (struct pfs_info *)mp->mnt_data;
	return (pfs_vncache_alloc(mp, vpp, pi->pi_root, NO_PID));
}

/*
 * Return filesystem stats
 */
int
pfs_statfs(struct mount *mp, struct statfs *sbp)
{
	/* no-op:  always called with mp->mnt_stat */
	return (0);
}

/*
 * Initialize a pseudofs instance
 */
int
pfs_init(struct pfs_info *pi, struct vfsconf *vfc)
{
	struct pfs_node *root;
	int error;

	pfs_fileno_init(pi);

	/* set up the root directory */
	root = pfs_alloc_node(pi, "/", pfstype_root);
	pi->pi_root = root;
	pfs_fileno_alloc(root);
	pfs_fixup_dir(root);

	/* construct file hierarchy */
	error = (pi->pi_init)(pi, vfc);
	if (error) {
		pfs_destroy(root);
		pi->pi_root = NULL;
		return (error);
	}

	if (bootverbose)
		printf("%s registered\n", pi->pi_name);
	return (0);
}

/*
 * Destroy a pseudofs instance
 */
int
pfs_uninit(struct pfs_info *pi, struct vfsconf *vfc)
{
	int error;

	pfs_destroy(pi->pi_root);
	pi->pi_root = NULL;
	pfs_fileno_uninit(pi);
	if (bootverbose)
		printf("%s unregistered\n", pi->pi_name);
	error = (pi->pi_uninit)(pi, vfc);
	return (error);
}

/*
 * Handle load / unload events
 */
static int
pfs_modevent(module_t mod, int evt, void *arg)
{
	switch (evt) {
	case MOD_LOAD:
		pfs_vncache_load();
		break;
	case MOD_UNLOAD:
	case MOD_SHUTDOWN:
		pfs_vncache_unload();
		break;
	default:
		return EOPNOTSUPP;
		break;
	}
	return 0;
}

/*
 * Module declaration
 */
static moduledata_t pseudofs_data = {
	"pseudofs",
	pfs_modevent,
	NULL
};
DECLARE_MODULE(pseudofs, pseudofs_data, SI_SUB_EXEC, SI_ORDER_FIRST);
MODULE_VERSION(pseudofs, 1);