2 * parsenfsfh.c - portable parser for NFS file handles
3 * uses all sorts of heuristics
6 * Digital Equipment Corporation
7 * Western Research Laboratory
11 static const char rcsid[] =
12 "@(#) $Header: parsenfsfh.c,v 1.14 97/06/15 13:20:27 leres Exp $ (LBL)";
15 #include <sys/types.h>
25 #include "interface.h"
29 * This routine attempts to parse a file handle (in network byte order),
30 * using heuristics to guess what kind of format it is in. See the
31 * file "fhandle_layouts" for a detailed description of the various
32 * patterns we know about.
34 * The file handle is parsed into our internal representation of a
35 * file-system id, and an internal representation of an inode-number.
52 /* Nasty hack to keep the Ultrix C compiler from emitting bogus warnings */
53 #define XFF(x) ((u_int32_t)(x))
58 #define make_uint32(msb,b,c,lsb)\
59 (XFF(lsb) + (XFF(c)<<8) + (XFF(b)<<16) + (XFF(msb)<<24))
61 #define make_uint24(msb,b, lsb)\
62 (XFF(lsb) + (XFF(b)<<8) + (XFF(msb)<<16))
64 #define make_uint16(msb,lsb)\
65 (XFF(lsb) + (XFF(msb)<<8))
68 /* or other 64-bit systems */
69 #define make_uint48(msb,b,c,d,e,lsb)\
70 ((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24) + ((b)<<32) + ((msb)<<40))
72 /* on 32-bit systems ignore high-order bits */
73 #define make_uint48(msb,b,c,d,e,lsb)\
74 ((lsb) + ((e)<<8) + ((d)<<16) + ((c)<<24))
77 static int is_UCX(unsigned char *);
80 Parse_fh(fh, len, fsidp, inop, osnamep, fsnamep, ourself)
85 char **osnamep; /* if non-NULL, return OS name here */
86 char **fsnamep; /* if non-NULL, return server fs name here (for VMS) */
87 int ourself; /* true if file handle was generated on this host */
89 register unsigned char *fhp = (unsigned char *)fh;
91 int fhtype = FHT_UNKNOWN;
94 /* File handle generated on this host, no need for guessing */
118 * This is basically a big decision tree
120 else if ((fhp[0] == 0) && (fhp[1] == 0)) {
121 /* bytes[0,1] == (0,0); rules out Ultrix, IRIX5, SUNOS5 */
122 /* probably rules out HP-UX, AIX unless they allow major=0 */
123 if ((fhp[2] == 0) && (fhp[3] == 0)) {
124 /* bytes[2,3] == (0,0); must be Auspex */
125 /* XXX or could be Ultrix+MASSBUS "hp" disk? */
130 * bytes[2,3] != (0,0); rules out Auspex, could be
131 * DECOSF, SUNOS4, or IRIX4
133 if ((fhp[4] != 0) && (fhp[5] == 0) &&
134 (fhp[8] == 12) && (fhp[9] == 0)) {
135 /* seems to be DECOSF, with minor == 0 */
139 /* could be SUNOS4 or IRIX4 */
140 /* XXX the test of fhp[5] == 8 could be wrong */
141 if ((fhp[4] == 0) && (fhp[5] == 8) && (fhp[6] == 0) &&
143 /* looks like a length, not a file system typecode */
155 * bytes[0,1] != (0,0); rules out Auspex, IRIX4, SUNOS4
156 * could be IRIX5, DECOSF, UCX, Ultrix, SUNOS5
157 * could be AIX, HP-UX
159 if ((fhp[2] == 0) && (fhp[3] == 0)) {
161 * bytes[2,3] == (0,0); rules out OSF, probably not UCX
162 * (unless the exported device name is just one letter!),
163 * could be Ultrix, IRIX5, AIX, or SUNOS5
164 * might be HP-UX (depends on their values for minor devs)
166 /*XXX we probably only need to test of these two bytes */
167 if ((fhp[21] == 0) && (fhp[23] == 0)) {
171 /* Could be SUNOS5/IRIX5, maybe AIX */
172 /* XXX no obvious difference between SUNOS5 and IRIX5 */
175 /* XXX what about AIX? */
180 * bytes[2,3] != (0,0); rules out Ultrix, could be
181 * DECOSF, SUNOS5, IRIX5, AIX, HP-UX, or UCX
183 if ((fhp[8] == 12) && (fhp[9] == 0)) {
186 else if ((fhp[8] == 0) && (fhp[9] == 10)) {
187 /* could be SUNOS5/IRIX5, AIX, HP-UX */
188 if ((fhp[7] == 0) && (fhp[6] == 0) &&
189 (fhp[5] == 0) && (fhp[4] == 0)) {
190 /* XXX is this always true of HP-UX? */
193 else if (fhp[7] == 2) {
194 /* This would be MNT_NFS on AIX, which is impossible */
195 fhtype = FHT_SUNOS5; /* or maybe IRIX5 */
199 * XXX Could be SUNOS5/IRIX5 or AIX. I don't
200 * XXX see any way to disambiguate these, so
201 * XXX I'm going with the more likely guess.
202 * XXX Sorry, Big Blue.
204 fhtype = FHT_SUNOS5; /* or maybe IRIX5 */
212 fhtype = FHT_UNKNOWN;
218 /* XXX still needs to handle SUNOS3 */
222 fsidp->Fsid_dev.Minor = fhp[7];
223 fsidp->Fsid_dev.Major = fhp[6];
224 fsidp->fsid_code = 0;
226 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
234 fsidp->fsid_code = make_uint32(fhp[7], fhp[6], fhp[5], fhp[4]);
235 /* XXX could ignore 3 high-order bytes */
237 temp = make_uint32(fhp[3], fhp[2], fhp[1], fhp[0]);
238 fsidp->Fsid_dev.Minor = temp & 0xFFFFF;
239 fsidp->Fsid_dev.Major = (temp>>20) & 0xFFF;
241 temp = make_uint32(fhp[15], fhp[14], fhp[13], fhp[12]);
248 fsidp->Fsid_dev.Minor = fhp[3];
249 fsidp->Fsid_dev.Major = fhp[2];
250 fsidp->fsid_code = 0;
252 temp = make_uint32(fhp[8], fhp[9], fhp[10], fhp[11]);
260 fsidp->Fsid_dev.Minor = make_uint16(fhp[2], fhp[3]);
261 fsidp->Fsid_dev.Major = make_uint16(fhp[0], fhp[1]);
262 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
264 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
277 fsidp->Fsid_dev.Minor = fhp[3];
278 fsidp->Fsid_dev.Major = fhp[2];
279 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
281 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
289 temp = make_uint16(fhp[0], fhp[1]);
290 fsidp->Fsid_dev.Major = (temp>>2) & 0x3FFF;
291 temp = make_uint24(fhp[1], fhp[2], fhp[3]);
292 fsidp->Fsid_dev.Minor = temp & 0x3FFFF;
293 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
295 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
303 fsidp->fsid_code = 0;
304 fsidp->Fsid_dev.Minor = fhp[0];
305 fsidp->Fsid_dev.Major = fhp[1];
307 temp = make_uint32(fhp[7], fhp[6], fhp[5], fhp[4]);
314 /* No numeric file system ID, so hash on the device-name */
315 if (sizeof(*fsidp) >= 14) {
316 if (sizeof(*fsidp) > 14)
317 memset((char *)fsidp, 0, sizeof(*fsidp));
318 /* just use the whole thing */
319 memcpy((char *)fsidp, (char *)fh, 14);
322 u_int32_t tempa[4]; /* at least 16 bytes, maybe more */
324 memset((char *)tempa, 0, sizeof(tempa));
325 memcpy((char *)tempa, (char *)fh, 14); /* ensure alignment */
326 fsidp->Fsid_dev.Minor = tempa[0] + (tempa[1]<<1);
327 fsidp->Fsid_dev.Major = tempa[2] + (tempa[3]<<1);
328 fsidp->fsid_code = 0;
331 /* VMS file ID is: (RVN, FidHi, FidLo) */
332 *inop = make_uint32(fhp[26], fhp[27], fhp[23], fhp[22]);
334 /* Caller must save (and null-terminate?) this value */
336 *fsnamep = (char *)&(fhp[1]);
343 fsidp->Fsid_dev.Minor = make_uint16(fhp[2], fhp[3]);
344 fsidp->Fsid_dev.Major = make_uint16(fhp[0], fhp[1]);
345 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
347 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
355 fsidp->Fsid_dev.Major = fhp[0];
356 temp = make_uint24(fhp[1], fhp[2], fhp[3]);
357 fsidp->Fsid_dev.Minor = temp;
358 fsidp->fsid_code = make_uint32(fhp[4], fhp[5], fhp[6], fhp[7]);
360 temp = make_uint32(fhp[12], fhp[13], fhp[14], fhp[15]);
372 for (i = 0; i < 32; i++)
373 (void)fprintf(stderr, "%x.", fhp[i]);
374 (void)fprintf(stderr, "\n");
377 /* XXX for now, give "bogus" values to aid debugging */
378 fsidp->fsid_code = 0;
379 fsidp->Fsid_dev.Minor = 257;
380 fsidp->Fsid_dev.Major = 257;
383 /* display will show this string instead of (257,257) */
385 *fsnamep = "Unknown";
388 *osnamep = "Unknown";
395 * Is this a VMS UCX file handle?
397 * (1) leading code byte [XXX not yet]
398 * (2) followed by string of printing chars & spaces
399 * (3) followed by string of nulls
408 for (i = 1; i < 14; i++) {
409 if (isprint(fhp[i])) {
415 else if (fhp[i] == 0) {