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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*	$NetBSD: coda_subr.c,v 1.31 2015/01/06 11:24:46 hannken Exp $	*/

/*
 *
 *             Coda: an Experimental Distributed File System
 *                              Release 3.1
 *
 *           Copyright (c) 1987-1998 Carnegie Mellon University
 *                          All Rights Reserved
 *
 * Permission  to  use, copy, modify and distribute this software and its
 * documentation is hereby granted,  provided  that  both  the  copyright
 * notice  and  this  permission  notice  appear  in  all  copies  of the
 * software, derivative works or  modified  versions,  and  any  portions
 * thereof, and that both notices appear in supporting documentation, and
 * that credit is given to Carnegie Mellon University  in  all  documents
 * and publicity pertaining to direct or indirect use of this code or its
 * derivatives.
 *
 * CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
 * SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
 * FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
 * DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
 * RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
 * ANY DERIVATIVE WORK.
 *
 * Carnegie  Mellon  encourages  users  of  this  software  to return any
 * improvements or extensions that  they  make,  and  to  grant  Carnegie
 * Mellon the rights to redistribute these changes without encumbrance.
 *
 * 	@(#) coda/coda_subr.c,v 1.1.1.1 1998/08/29 21:26:45 rvb Exp $
 */

/*
 * Mach Operating System
 * Copyright (c) 1989 Carnegie-Mellon University
 * All rights reserved.  The CMU software License Agreement specifies
 * the terms and conditions for use and redistribution.
 */

/*
 * This code was written for the Coda file system at Carnegie Mellon
 * University.  Contributers include David Steere, James Kistler, and
 * M. Satyanarayanan.  */

/* NOTES: rvb
 * 1.	Added coda_unmounting to mark all cnodes as being UNMOUNTING.  This has to
 *	 be done before dounmount is called.  Because some of the routines that
 *	 dounmount calls before coda_unmounted might try to force flushes to venus.
 *	 The vnode pager does this.
 * 2.	coda_unmounting marks all cnodes scanning coda_cache.
 * 3.	cfs_checkunmounting (under DEBUG) checks all cnodes by chasing the vnodes
 *	 under the /coda mount point.
 * 4.	coda_cacheprint (under DEBUG) prints names with vnode/cnode address
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: coda_subr.c,v 1.31 2015/01/06 11:24:46 hannken Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/select.h>
#include <sys/mount.h>
#include <sys/kauth.h>

#include <coda/coda.h>
#include <coda/cnode.h>
#include <coda/coda_subr.h>
#include <coda/coda_namecache.h>

int codadebug = 0;
int coda_printf_delay = 0;  /* in microseconds */
int coda_vnop_print_entry = 0;
int coda_vfsop_print_entry = 0;

#ifdef CODA_COMPAT_5
#define coda_hash(fid) \
    (((fid)->Volume + (fid)->Vnode) & (CODA_CACHESIZE-1))
#define IS_DIR(cnode)        (cnode.Vnode & 0x1)
#else
#define coda_hash(fid) \
    (coda_f2i(fid) & (CODA_CACHESIZE-1))
#define IS_DIR(cnode)        (cnode.opaque[2] & 0x1)
#endif

struct vnode *coda_ctlvp;

/*
 * Lookup a cnode by fid. If the cnode is dying, it is bogus so skip it.
 * The cnode is returned locked with the vnode referenced.
 */
struct cnode *
coda_find(CodaFid *fid)
{
	int i;
	struct vnode *vp;
	struct cnode *cp;

	for (i = 0; i < NVCODA; i++) {
		if (!coda_mnttbl[i].mi_started)
			continue;
		if (vcache_get(coda_mnttbl[i].mi_vfsp,
		    fid, sizeof(CodaFid), &vp) != 0)
			continue;
		mutex_enter(vp->v_interlock);
		cp = VTOC(vp);
		if (vp->v_type == VNON || cp == NULL || IS_UNMOUNTING(cp)) {
			mutex_exit(vp->v_interlock);
			vrele(vp);
			continue;
		}
		mutex_enter(&cp->c_lock);
		mutex_exit(vp->v_interlock);

		return cp;
	}

	return NULL;
}

/*
 * Iterate over all nodes attached to coda mounts.
 */
static void
coda_iterate(bool (*f)(void *, struct vnode *), void *cl)
{
	int i;
	struct vnode_iterator *marker;
	struct vnode *vp;

	for (i = 0; i < NVCODA; i++) { 
		if (coda_mnttbl[i].mi_vfsp == NULL)
			continue;
		vfs_vnode_iterator_init(coda_mnttbl[i].mi_vfsp, &marker);
		while ((vp = vfs_vnode_iterator_next(marker, f, cl)) != NULL)
			vrele(vp);
		vfs_vnode_iterator_destroy(marker);
	}
}

/*
 * coda_kill is called as a side effect to vcopen. To prevent any
 * cnodes left around from an earlier run of a venus or warden from
 * causing problems with the new instance, mark any outstanding cnodes
 * as dying. Future operations on these cnodes should fail (excepting
 * coda_inactive of course!). Since multiple venii/wardens can be
 * running, only kill the cnodes for a particular entry in the
 * coda_mnttbl. -- DCS 12/1/94 */

static bool
coda_kill_selector(void *cl, struct vnode *vp)
{
	int *count = cl;

	(*count)++;

	return false;
}

int
coda_kill(struct mount *whoIam, enum dc_status dcstat)
{
	int count = 0;
	struct vnode_iterator *marker;

	/*
	 * Algorithm is as follows:
	 *     Second, flush whatever vnodes we can from the name cache.
	 */

	/* This is slightly overkill, but should work. Eventually it'd be
	 * nice to only flush those entries from the namecache that
	 * reference a vnode in this vfs.  */
	coda_nc_flush(dcstat);


	vfs_vnode_iterator_init(whoIam, &marker);
	vfs_vnode_iterator_next(marker, coda_kill_selector, &count);
	vfs_vnode_iterator_destroy(marker);

	return count;
}

/*
 * There are two reasons why a cnode may be in use, it may be in the
 * name cache or it may be executing.
 */
static bool
coda_flush_selector(void *cl, struct vnode *vp)
{
	struct cnode *cp = VTOC(vp);

	if (cp != NULL && !IS_DIR(cp->c_fid)) /* only files can be executed */
		coda_vmflush(cp);

	return false;
}
void
coda_flush(enum dc_status dcstat)
{

    coda_clstat.ncalls++;
    coda_clstat.reqs[CODA_FLUSH]++;

    coda_nc_flush(dcstat);	    /* flush files from the name cache */

    coda_iterate(coda_flush_selector, NULL);
}

/*
 * As a debugging measure, print out any cnodes that lived through a
 * name cache flush.
 */
static bool
coda_testflush_selector(void *cl, struct vnode *vp)
{
	struct cnode *cp = VTOC(vp);

	if (cp != NULL)
		myprintf(("Live cnode fid %s count %d\n",
		     coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount));

	return false;
}
void
coda_testflush(void)
{

	coda_iterate(coda_testflush_selector, NULL);
}

/*
 *     First, step through all cnodes and mark them unmounting.
 *         NetBSD kernels may try to fsync them now that venus
 *         is dead, which would be a bad thing.
 *
 */
static bool
coda_unmounting_selector(void *cl, struct vnode *vp)
{
	struct cnode *cp = VTOC(vp);

	if (cp)
		cp->c_flags |= C_UNMOUNTING;

	return false;
}
void
coda_unmounting(struct mount *whoIam)
{
	struct vnode_iterator *marker;

	vfs_vnode_iterator_init(whoIam, &marker);
	vfs_vnode_iterator_next(marker, coda_unmounting_selector, NULL);
	vfs_vnode_iterator_destroy(marker);
}

#ifdef	DEBUG
static bool
coda_checkunmounting_selector(void *cl, struct vnode *vp)
{
	struct cnode *cp = VTOC(vp);

	if (cp && !(cp->c_flags & C_UNMOUNTING)) {
		printf("vp %p, cp %p missed\n", vp, cp);
		cp->c_flags |= C_UNMOUNTING;
	}

	return false;
}
void
coda_checkunmounting(struct mount *mp)
{
	struct vnode_iterator *marker;

	vfs_vnode_iterator_init(mp, &marker);
	vfs_vnode_iterator_next(marker, coda_checkunmounting_selector, NULL);
	vfs_vnode_iterator_destroy(marker);
}

void
coda_cacheprint(struct mount *whoIam)
{
	struct vnode *vp;
	struct vnode_iterator *marker;
	int count = 0;

	printf("coda_cacheprint: coda_ctlvp %p, cp %p", coda_ctlvp, VTOC(coda_ctlvp));
	coda_nc_name(VTOC(coda_ctlvp));
	printf("\n");

	vfs_vnode_iterator_init(whoIam, &marker);
	while ((vp = vfs_vnode_iterator_next(marker, NULL, NULL)) != NULL) {
		printf("coda_cacheprint: vp %p, cp %p", vp, VTOC(vp));
		coda_nc_name(VTOC(vp));
		printf("\n");
		count++;
		vrele(vp);
	}
	printf("coda_cacheprint: count %d\n", count);
	vfs_vnode_iterator_destroy(marker);
}
#endif

/*
 * There are 6 cases where invalidations occur. The semantics of each
 * is listed here.
 *
 * CODA_FLUSH     -- flush all entries from the name cache and the cnode cache.
 * CODA_PURGEUSER -- flush all entries from the name cache for a specific user
 *                  This call is a result of token expiration.
 *
 * The next two are the result of callbacks on a file or directory.
 * CODA_ZAPDIR    -- flush the attributes for the dir from its cnode.
 *                  Zap all children of this directory from the namecache.
 * CODA_ZAPFILE   -- flush the attributes for a file.
 *
 * The fifth is a result of Venus detecting an inconsistent file.
 * CODA_PURGEFID  -- flush the attribute for the file
 *                  If it is a dir (odd vnode), purge its
 *                  children from the namecache
 *                  remove the file from the namecache.
 *
 * The sixth allows Venus to replace local fids with global ones
 * during reintegration.
 *
 * CODA_REPLACE -- replace one CodaFid with another throughout the name cache
 */

int handleDownCall(int opcode, union outputArgs *out)
{
    int error;

    /* Handle invalidate requests. */
    switch (opcode) {
      case CODA_FLUSH : {

	  coda_flush(IS_DOWNCALL);

	  CODADEBUG(CODA_FLUSH,coda_testflush();)    /* print remaining cnodes */
	      return(0);
      }

      case CODA_PURGEUSER : {
	  coda_clstat.ncalls++;
	  coda_clstat.reqs[CODA_PURGEUSER]++;

	  /* XXX - need to prevent fsync's */
#ifdef CODA_COMPAT_5
	  coda_nc_purge_user(out->coda_purgeuser.cred.cr_uid, IS_DOWNCALL);
#else
	  coda_nc_purge_user(out->coda_purgeuser.uid, IS_DOWNCALL);
#endif
	  return(0);
      }

      case CODA_ZAPFILE : {
	  struct cnode *cp;

	  error = 0;
	  coda_clstat.ncalls++;
	  coda_clstat.reqs[CODA_ZAPFILE]++;

	  cp = coda_find(&out->coda_zapfile.Fid);
	  if (cp != NULL) {
	      cp->c_flags &= ~C_VATTR;
	      if (CTOV(cp)->v_iflag & VI_TEXT)
		  error = coda_vmflush(cp);
	      CODADEBUG(CODA_ZAPFILE, myprintf((
		    "zapfile: fid = %s, refcnt = %d, error = %d\n",
		    coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1, error)););
	      if (CTOV(cp)->v_usecount == 1) {
		  cp->c_flags |= C_PURGING;
	      }
	      mutex_exit(&cp->c_lock);
	      vrele(CTOV(cp));
	  }

	  return(error);
      }

      case CODA_ZAPDIR : {
	  struct cnode *cp;

	  coda_clstat.ncalls++;
	  coda_clstat.reqs[CODA_ZAPDIR]++;

	  cp = coda_find(&out->coda_zapdir.Fid);
	  if (cp != NULL) {
	      cp->c_flags &= ~C_VATTR;
	      coda_nc_zapParentfid(&out->coda_zapdir.Fid, IS_DOWNCALL);

	      CODADEBUG(CODA_ZAPDIR, myprintf((
		    "zapdir: fid = %s, refcnt = %d\n",
		    coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1)););
	      if (CTOV(cp)->v_usecount == 1) {
		  cp->c_flags |= C_PURGING;
	      }
	      mutex_exit(&cp->c_lock);
	      vrele(CTOV(cp));
	  }

	  return(0);
      }

      case CODA_PURGEFID : {
	  struct cnode *cp;

	  error = 0;
	  coda_clstat.ncalls++;
	  coda_clstat.reqs[CODA_PURGEFID]++;

	  cp = coda_find(&out->coda_purgefid.Fid);
	  if (cp != NULL) {
	      if (IS_DIR(out->coda_purgefid.Fid)) { /* Vnode is a directory */
		  coda_nc_zapParentfid(&out->coda_purgefid.Fid,
				     IS_DOWNCALL);
	      }
	      cp->c_flags &= ~C_VATTR;
	      coda_nc_zapfid(&out->coda_purgefid.Fid, IS_DOWNCALL);
	      if (!(IS_DIR(out->coda_purgefid.Fid))
		  && (CTOV(cp)->v_iflag & VI_TEXT)) {

		  error = coda_vmflush(cp);
	      }
	      CODADEBUG(CODA_PURGEFID, myprintf((
			 "purgefid: fid = %s, refcnt = %d, error = %d\n",
			 coda_f2s(&cp->c_fid), CTOV(cp)->v_usecount - 1, error)););
	      if (CTOV(cp)->v_usecount == 1) {
		  cp->c_flags |= C_PURGING;
	      }
	      mutex_exit(&cp->c_lock);
	      vrele(CTOV(cp));
	  }
	  return(error);
      }

      case CODA_REPLACE : {
	  struct cnode *cp = NULL;

	  coda_clstat.ncalls++;
	  coda_clstat.reqs[CODA_REPLACE]++;

	  cp = coda_find(&out->coda_replace.OldFid);
	  if (cp != NULL) {
	      error = vcache_rekey_enter(CTOV(cp)->v_mount, CTOV(cp),
		  &out->coda_replace.OldFid, sizeof(CodaFid),
		  &out->coda_replace.NewFid, sizeof(CodaFid));
	      if (error) {
		  mutex_exit(&cp->c_lock);
		  vrele(CTOV(cp));
		  return error;
	      }
	      cp->c_fid = out->coda_replace.NewFid;
	      vcache_rekey_exit(CTOV(cp)->v_mount, CTOV(cp),
		  &out->coda_replace.OldFid, sizeof(CodaFid),
		  &cp->c_fid, sizeof(CodaFid));

	      CODADEBUG(CODA_REPLACE, myprintf((
			"replace: oldfid = %s, newfid = %s, cp = %p\n",
			coda_f2s(&out->coda_replace.OldFid),
			coda_f2s(&cp->c_fid), cp));)
	      mutex_exit(&cp->c_lock);
	      vrele(CTOV(cp));
	  }
	  return (0);
      }
      default:
      	myprintf(("handleDownCall: unknown opcode %d\n", opcode));
	return (EINVAL);
    }
}

/* coda_grab_vnode: lives in either cfs_mach.c or cfs_nbsd.c */

int
coda_vmflush(struct cnode *cp)
{
    return 0;
}


/*
 * kernel-internal debugging switches
 */

void coda_debugon(void)
{
    codadebug = -1;
    coda_nc_debug = -1;
    coda_vnop_print_entry = 1;
    coda_psdev_print_entry = 1;
    coda_vfsop_print_entry = 1;
}

void coda_debugoff(void)
{
    codadebug = 0;
    coda_nc_debug = 0;
    coda_vnop_print_entry = 0;
    coda_psdev_print_entry = 0;
    coda_vfsop_print_entry = 0;
}

/* How to print a ucred */
void
coda_print_cred(kauth_cred_t cred)
{

	uint16_t ngroups;
	int i;

	myprintf(("ref %d\tuid %d\n", kauth_cred_getrefcnt(cred),
		 kauth_cred_geteuid(cred)));

	ngroups = kauth_cred_ngroups(cred);
	for (i=0; i < ngroups; i++)
		myprintf(("\tgroup %d: (%d)\n", i, kauth_cred_group(cred, i)));
	myprintf(("\n"));

}

/*
 * Utilities used by both client and server
 * Standard levels:
 * 0) no debugging
 * 1) hard failures
 * 2) soft failures
 * 3) current test software
 * 4) main procedure entry points
 * 5) main procedure exit points
 * 6) utility procedure entry points
 * 7) utility procedure exit points
 * 8) obscure procedure entry points
 * 9) obscure procedure exit points
 * 10) random stuff
 * 11) all <= 1
 * 12) all <= 2
 * 13) all <= 3
 * ...
 */