summaryrefslogtreecommitdiff
path: root/dialects/sun/dnode1.c
blob: ac279e5d960048a10e661424f63c3273e914210c (plain)
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
/*
 * dnode1.h - Solaris AFS support
 */


/*
 * Copyright 1996 Purdue Research Foundation, West Lafayette, Indiana
 * 47907.  All rights reserved.
 *
 * Written by Victor A. Abell
 *
 * This software is not subject to any license of the American Telephone
 * and Telegraph Company or the Regents of the University of California.
 *
 * Permission is granted to anyone to use this software for any purpose on
 * any computer system, and to alter it and redistribute it freely, subject
 * to the following restrictions:
 *
 * 1. Neither the authors nor Purdue University are responsible for any
 *    consequences of the use of this software.
 *
 * 2. The origin of this software must not be misrepresented, either by
 *    explicit claim or by omission.  Credit to the authors and Purdue
 *    University must appear in documentation and sources.
 *
 * 3. Altered versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 *
 * 4. This notice may not be removed or altered.
 */


#ifndef lint
static char copyright[] =
"@(#) Copyright 1996 Purdue Research Foundation.\nAll rights reserved.\n";
static char *rcsid = "$Id: dnode1.c,v 1.9 2005/08/08 19:55:41 abe Exp $";
#endif


#if	defined(HAS_AFS)
#include "lsof.h"

#include <rpc/xdr.h>
#define __XDR_INCLUDE__
#define int32   old_solaris_int32

# if	solaris>=20600
#undef	SHARED
#undef	PRIVATE
# endif	/* solaris>=20600 */

#include <afs/param.h>
#include <afs/afsint.h>
#include <afs/vldbint.h>


/*
 * This is an emulation of the afs_rwlock_t definition that appears in
 * the AFS sources in afs/lock.h.
 */

# if	defined(AFS_SUN5_ENV)
#define AFS_NOBOZO_LOCK
# endif	/* defined(AFS_SUN5_ENV) */

#define INSTRUMENT_LOCKS

# if	defined(AFS_FINEGR_SUNLOCK)
typedef	kmutex_	afs_lock_t;
typedef	krwlock_t afs_rwlock_t;
# endif	/* !defined(AFS_FINEGR_SUNLOCK) */

struct afs_lock {

# if	solaris>=20500
    unsigned char d1[2];
    unsigned short d1_5[3];
# else	/* solaris < 20500 */
    unsigned char d1[4];
# endif	/* solaris>=20500 */

    struct timeval d2;

# if	defined(INSTRUMENT_LOCKS)
    unsigned int d3[3];
# endif	/* defined(INSTRUMENT_LOCKS) */

};
typedef struct afs_lock afs_lock_t;
typedef struct afs_lock afs_rwlock_t;


/*
 * This is an emulation of the afs_bozoLock_t definition that appears in
 * the AFS sources in afs/lock.h.
 */

struct afs_bozoLock {
    short d1;
    char d2[2];
    char *d3;
};

# if	!defined(AFS_NOBOZO_LOCK)
typedef struct afs_bozoLock afs_bozoLock_t;
# else	/* defined(AFS_NOBOZO_LOCK) */
#  if	defined(AFS_SUN5_ENV)
typedef kmutex_t afs_bozoLock_t;
#  else	/* !defined(AFS_SUN5_ENV) */
typedef struct afs_bozoLock afs_bozoLock_t;
#  endif	/* defined(AFS_SUN5_ENV) */
# endif	/* !defined(AFS_NOBOZO_LOCK) */

#define	KERNEL
#include <afs/afs.h>
#undef	KERNEL


/*
 * Local function prototypes
 */

_PROTOTYPE(static struct volume *getvolume,(struct VenusFid *f, int *vols));
_PROTOTYPE(static int is_rootFid,(struct vcache *vc, int *rfid));


/*
 * alloc_vcache() - allocate space for vcache structure
 */

struct vnode *
alloc_vcache()
{
	return((struct vnode *)malloc(sizeof(struct vcache)));
}


/*
 * ckAFSsym() - check for missing X_AFS_* symbols in AFS name list file
 */

void
ckAFSsym(nl)
	struct nlist *nl;		/* copy of Nl[] when empty */
{
	char *path = AFSAPATHDEF;
	int i;
	KA_T v;

# if	defined(HASAOPT)
	if (AFSApath)
	    path = AFSApath;
# endif	/* defined(HASAOPT) */

/*
 * If an alternate AFS name list file was specified, see if it can be read.
 */
	if (!is_readable(path, 0)) {
	    if (!Fwarn)
		(void) fprintf(stderr,
		    "%s: WARNING: can't access AFS name list file: %s\n",
		    Pn, path);
	    return;
	}
/*
 * Read the AFS modload symbols and compare its non-zero values with
 * the non-zero values in Nl[].  Quit if there is any mis-match.
 */
	if (nlist(path, nl) < 0)
	    return;
	for (i = 0; Nl[i].n_name && Nl[i].n_name[0]; i++) {
	    if (!nl[i].n_value || !Nl[i].n_value)
		continue;
	    if (nl[i].n_value != Nl[i].n_value)
		return;
	}
/*
 * If any AFS symbol that doesn't have a value in Nl[] has one from
 * the AFS modload file, copy its modload value to Nl[].
 */
	if ((i = get_Nl_value("arFid", Drive_Nl, &v)) >= 0
	&&  !Nl[i].n_value && nl[i].n_value)
	    Nl[i].n_value = nl[i].n_value;
	if ((i = get_Nl_value("avops", Drive_Nl, &v)) >= 0
	&&  !Nl[i].n_value && nl[i].n_value)
	    Nl[i].n_value = nl[i].n_value;
	if ((i = get_Nl_value("avol", Drive_Nl, &v)) >= 0
	&&  !Nl[i].n_value && nl[i].n_value)
	    Nl[i].n_value = nl[i].n_value;
}


/*
 * getvolume() - get volume structure
 */

static struct volume *
getvolume(f, vols)
	struct VenusFid *f;		/* file ID pointer */
	int *vols;			/* afs_volumes status return */
{
	int i;
	static KA_T ka = 0;
	KA_T kh;
	static struct volume v;
	KA_T vp;
	static int w = 0;

	if (!ka) {
	    if (get_Nl_value("avol", Drive_Nl, &ka) < 0 || !ka) {
		if (!w && !Fwarn) {
		    (void) fprintf(stderr,
			"%s: WARNING: no kernel address for afs_volumes\n", Pn);
		    (void) fprintf(stderr,
			"      This may hamper AFS node number reporting.\n");
		    w = 1;
		}
		*vols = 0;
		return((struct volume *)NULL);
	    }
	}
	*vols = 1;
	i = (NVOLS - 1) & f->Fid.Volume;
	kh = (KA_T)((char *)ka + (i * sizeof(struct volume *)));
	if (kread(kh, (char *)&vp, sizeof(vp)))
	    return((struct volume *)NULL);
	while (vp) {
	    if (kread((KA_T)vp, (char *)&v, sizeof(v)))
		return((struct volume *)NULL);
	    if (v.volume == f->Fid.Volume && v.cell == f->Cell)
		return(&v);
	    vp = (KA_T)v.next;
	}
	return((struct volume *)NULL);
}


/*
 * hasAFS() - test for AFS presence via vfs structure
 */

int
hasAFS(vp)
	struct vnode *vp;		/* vnode pointer */
{
	struct mounts *mp;
	int n;
	struct vfs v;
/*
 * If this vnode has a v_data pointer, then it probably isn't an AFS vnode;
 * return FALSE.
 *
 * If the vfs struct address of /afs is known and this vnode's v_vfsp matches
 * it, return TRUE.
 *
 * Read this vnode's vfs structure and see if it's device (fsid.val[0]) is
 * AFSdev.  If it is, record the AFS vfs struct address and return TRUE.
 */
	if (AFSVfsp && !vp->v_data && (KA_T)vp->v_vfsp == AFSVfsp)
	    return(1);
	if (!AFSdevStat)
	    (void) readmnt();
	if (!AFSdevStat
	||  vp->v_data
	||  !vp->v_vfsp
	||  kread((KA_T)vp->v_vfsp, (char *)&v, sizeof(v))
	||  v.vfs_data)
	    return(0);
	if ((dev_t)v.vfs_fsid.val[0] == AFSdev) {
	    AFSVfsp = (KA_T)vp->v_vfsp;
	    return(1);
	}
/*
 * Search the local mount table for /afs devices.  Count /afs devices,
 * and skip a device number test for them.  A match on device number for
 * non-AFS devices produces a FALSE return.
 */
	for (mp = readmnt(), n = 0; mp; mp = mp->next) {
	    if (AFSdevStat && mp->dev == AFSdev
	    &&  mp->dir && strcmp(mp->dir, "/afs") == 0
	    &&  mp->fsname && strcmp(mp->fsname, "AFS") == 0)
		n++;
	    else if (mp->dev == (dev_t)v.vfs_fsid.val[0])
		return(0);
	}
/*
 * If there is exactly one /afs device, assume its vfs struct address is
 * the one for this vnode, record it, and return TRUE.
 */
	if (n == 1) {
	    AFSVfsp = (KA_T)vp->v_vfsp;
	    return(1);
	}
	return(0);
}


/*
 * is_rootFid() - is the file ID the root file ID
 *
 * return: 0	= is not root file ID
 *	   1	= is root file ID
 *	   rfid = 0 if root file ID structure address not available
 *		  1 if root file ID structure address available
 */

static int
is_rootFid(vc, rfid)
	struct vcache *vc;		/* vcache structure */
	int *rfid;			/* root file ID pointer status return */
{
	KA_T arFid;
	char *err;
	static int f = 0;		/* rootFid structure status:
					 *     -1 = unavailable
					 *	0 = not yet accessed
					 *	1 = available */
	static struct VenusFid r;
	static int w = 0;

	switch (f) {
	case -1:
	    if (vc->v.v_flag & VROOT) {
		*rfid = 1;
		return(1);
	    }
	    *rfid = 0;
	    return(0);
	case 0:
	    if (get_Nl_value("arFid", Drive_Nl, &arFid) < 0 || !arFid) {
		err = "no afs_rootFid kernel address";

rfid_unavailable:

		if (!w && !Fwarn) {
		    (void) fprintf(stderr,
			"%s: WARNING: AFS root Fid error: %s\n", Pn, err);
		    (void) fprintf(stderr,
			"      This may hamper AFS node number reporting.\n");
		    w = 1;
		}
		f = -1;
		if (vc->v.v_flag & VROOT) {
		    *rfid = 1;
		    return(1);
		}
		*rfid = 0;
		return(0);
	    }
	    if (kread(arFid, (char *)&r, sizeof(r))) {
		err = "can't read afs_rootFid from kernel";
		goto rfid_unavailable;
	    }
	    f = 1;
	    /* fall through */
	case 1:
	    *rfid = 1;
	    if (vc->fid.Fid.Unique == r.Fid.Unique
	    &&  vc->fid.Fid.Vnode == r.Fid.Vnode
	    &&  vc->fid.Fid.Volume == r.Fid.Volume
	    &&  vc->fid.Cell == r.Cell)
		return(1);
	}
	*rfid = 0;
	return(0);
}


/*
 * readafsnode() - read AFS node
 */

int
readafsnode(va, v, an)
	KA_T va;			/* kernel vnode address */
	struct vnode *v;		/* vnode buffer pointer */
	struct afsnode *an;		/* afsnode recipient */
{
	char *cp, tbuf[32];
	KA_T ka;
	int len, rfid, vols;
	struct vcache *vc;
	struct volume *vp;

	cp = ((char *)v + sizeof(struct vnode));
	ka = (KA_T)((char *)va + sizeof(struct vnode));
	len = sizeof(struct vcache) - sizeof(struct vnode);
	if (kread(ka, cp, len)) {
	    (void) snpf(Namech, Namechl,
		"vnode at %s: can't read vcache remainder from %s",
		print_kptr(va, tbuf, sizeof(tbuf)),
		print_kptr(ka, (char *)NULL, 0));
	    enter_nm(Namech);
	    return(1);
	}
	vc = (struct vcache *)v;
	if (!AFSdevStat)
	    (void) readmnt();
	an->dev = AFSdevStat ? AFSdev: 0;
	an->size = (unsigned long)vc->m.Length;
	an->nlink = (long)vc->m.LinkCount;
	an->nlink_st = 1;
/*
 * Manufacture the "inode" number.
 */
	if (vc->mvstat == 2) {
	    if ((vp = getvolume(&vc->fid, &vols))) {
		an->inode = (INODETYPE)((vp->mtpoint.Fid.Vnode
			  +		(vp->mtpoint.Fid.Volume << 16))
			  & 0x7fffffff);
		if (an->inode == (INODETYPE)0) {
		    if (is_rootFid(vc, &rfid))
			an->ino_st = 1;
		    else if (rfid) {
			an->inode = (INODETYPE)2;
			an->ino_st = 1;
		    } else
			an->ino_st = 0;
		} else
		    an->ino_st = 1;
	    } else {
		if (vols) {
		    an->inode = (INODETYPE)2;
		    an->ino_st = 1;
		} else {
		    if (v->v_flag & VROOT) {
			an->inode = (INODETYPE)0;
			an->ino_st = 1;
		    } else
			an->ino_st = 0;
		}
	    }
	} else {
	    an->inode = (INODETYPE)((vc->fid.Fid.Vnode
		      +			(vc->fid.Fid.Volume << 16))
		      & 0x7fffffff);
	    an->ino_st = 1;
	}
	return(0);
}
#endif	/* defined(HAS_AFS) */