xref: /titanic_51/usr/src/uts/common/fs/hsfs/hsfs_rrip.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1990,1997,2000,2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Rock Ridge extensions to the System Use Sharing protocol
31  * for the High Sierra filesystem
32  */
33 
34 #include <sys/types.h>
35 #include <sys/t_lock.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kmem.h>
39 #include <sys/signal.h>
40 #include <sys/user.h>
41 #include <sys/proc.h>
42 #include <sys/disp.h>
43 #include <sys/buf.h>
44 #include <sys/pathname.h>
45 #include <sys/vfs.h>
46 #include <sys/vnode.h>
47 #include <sys/file.h>
48 #include <sys/uio.h>
49 #include <sys/conf.h>
50 #include <sys/stat.h>
51 #include <sys/mode.h>
52 #include <sys/mkdev.h>
53 #include <sys/ddi.h>
54 
55 #include <vm/page.h>
56 
57 #include <sys/fs/hsfs_spec.h>
58 #include <sys/fs/hsfs_isospec.h>
59 #include <sys/fs/hsfs_node.h>
60 #include <sys/fs/hsfs_impl.h>
61 #include <sys/fs/hsfs_susp.h>
62 #include <sys/fs/hsfs_rrip.h>
63 
64 #include <sys/statvfs.h>
65 #include <sys/mount.h>
66 #include <sys/swap.h>
67 #include <sys/errno.h>
68 #include <sys/debug.h>
69 #include "fs/fs_subr.h"
70 #include <sys/cmn_err.h>
71 
72 static void form_time(int, uchar_t *, struct timeval *);
73 static void name_parse(int, uchar_t *, size_t, uchar_t *, int *,
74     ulong_t *, int);
75 
76 /*
77  *  Signature table for RRIP
78  */
79 ext_signature_t  rrip_signature_table[ ] = {
80 	RRIP_CL,	rrip_child_link,
81 	RRIP_NM,	rrip_name,
82 	RRIP_PL,	rrip_parent_link,
83 	RRIP_PN,	rrip_dev_nodes,
84 	RRIP_PX,	rrip_file_attr,
85 	RRIP_RE,	rrip_reloc_dir,
86 	RRIP_RR,	rrip_rock_ridge,
87 	RRIP_SL,	rrip_sym_link,
88 	RRIP_TF,	rrip_file_time,
89 	(char *)NULL,   NULL
90 };
91 
92 
93 /*
94  * rrip_dev_nodes()
95  *
96  * sig_handler() for RRIP signature "PN"
97  *
98  * This function parses out the major and minor numbers from the "PN
99  * " SUF.
100  */
101 uchar_t *
102 rrip_dev_nodes(sig_args_t *sig_args_p)
103 {
104 	uchar_t *pn_ptr = sig_args_p->SUF_ptr;
105 	major_t	major_dev = (major_t)RRIP_MAJOR(pn_ptr);
106 	minor_t	minor_dev = (minor_t)RRIP_MINOR(pn_ptr);
107 
108 	sig_args_p->hdp->r_dev = makedevice(major_dev, minor_dev);
109 
110 	return (pn_ptr + SUF_LEN(pn_ptr));
111 }
112 
113 /*
114  * rrip_file_attr()
115  *
116  * sig_handler() for RRIP signature "PX"
117  *
118  * This function parses out the file attributes of a file from the "PX"
119  * SUF.  The attributes is finds are : st_mode, st_nlink, st_uid,
120  * and st_gid.
121  */
122 uchar_t *
123 rrip_file_attr(sig_args_t *sig_args_p)
124 {
125 	uchar_t *px_ptr = sig_args_p->SUF_ptr;
126 	struct hs_direntry *hdp    = sig_args_p->hdp;
127 
128 	hdp->mode  = RRIP_MODE(px_ptr);
129 	hdp->nlink = RRIP_NLINK(px_ptr);
130 	hdp->uid   = RRIP_UID(px_ptr);
131 	hdp->gid   = RRIP_GID(px_ptr);
132 
133 	hdp->type = IFTOVT(hdp->mode);
134 
135 	return (px_ptr + SUF_LEN(px_ptr));
136 }
137 
138 /*
139  * rrip_file_time()
140  *
141  * support function for rrip_file_time()
142  *
143  * This function decides whether to parse the times in a long time form
144  * (17 bytes) or a short time form (7 bytes).  These time formats are
145  * defined in the ISO 9660 specification.
146  */
147 static void
148 form_time(int time_length, uchar_t *file_time, struct timeval *tvp)
149 {
150 	if (time_length == ISO_DATE_LEN)
151 		hs_parse_longdate(file_time, tvp);
152 	else
153 		hs_parse_dirdate(file_time, tvp);
154 
155 }
156 
157 /*
158  * rrip_file_time()
159  *
160  * sig_handler() for RRIP signature RRIP_TF
161  *
162  * This function parses out the file time attributes of a file from the
163  * "TI" SUF.  The times it parses are : st_mtime, st_atime and st_ctime.
164  *
165  * The function form_time is a support function only used in this
166  * function.
167  */
168 uchar_t *
169 rrip_file_time(sig_args_t *sig_args_p)
170 {
171 	uchar_t *tf_ptr = sig_args_p->SUF_ptr;
172 
173 	if (IS_TIME_BIT_SET(RRIP_TF_FLAGS(tf_ptr), RRIP_TF_ACCESS_BIT)) {
174 		form_time(RRIP_TF_TIME_LENGTH(tf_ptr),
175 		    RRIP_tf_access(tf_ptr),
176 		    &sig_args_p->hdp->adate);
177 	}
178 
179 	if (IS_TIME_BIT_SET(RRIP_TF_FLAGS(tf_ptr), RRIP_TF_MODIFY_BIT)) {
180 		form_time(RRIP_TF_TIME_LENGTH(tf_ptr), RRIP_tf_modify(tf_ptr),
181 		    &sig_args_p->hdp->mdate);
182 	}
183 
184 	if (IS_TIME_BIT_SET(RRIP_TF_FLAGS(tf_ptr), RRIP_TF_ATTRIBUTES_BIT)) {
185 		form_time(RRIP_TF_TIME_LENGTH(tf_ptr),
186 		    RRIP_tf_attributes(tf_ptr),
187 		    &sig_args_p->hdp->cdate);
188 	}
189 
190 	return (tf_ptr + SUF_LEN(tf_ptr));
191 }
192 
193 
194 
195 /*
196  * name_parse()
197  *
198  * This is a generic fuction used for sym links and filenames.  The
199  * flags passed to it effect the way the name/component field is parsed.
200  *
201  * The return value will be the NAME_CONTINUE or NAME_CHANGE value.
202  *
203  */
204 static void
205 name_parse(
206 	int		rrip_flags,	/* component/name flag */
207 	uchar_t		*SUA_string,	/* string from SUA */
208 	size_t		SUA_string_len, /* length of SUA string */
209 	uchar_t		*to_string,	/* string to copy to */
210 	int		*to_string_len_p, /* ptr to cur. str len */
211 	ulong_t		*name_flags_p,	/* internal name flags */
212 	int		max_name_len)	/* limit dest string to */
213 						/* this value */
214 {
215 	size_t	tmp_name_len;
216 
217 	if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_ROOT)) {
218 		(void) strcpy((char *)to_string, "/");
219 		*to_string_len_p = 1;
220 	}
221 
222 	if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_CURRENT)) {
223 		SUA_string = (uchar_t *)".";
224 		SUA_string_len = 1;
225 	}
226 
227 	if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_PARENT)) {
228 		SUA_string = (uchar_t *)"..";
229 		SUA_string_len = 2;
230 	}
231 
232 	/*
233 	 * XXX
234 	 * For now, ignore the following flags and return.
235 	 * have to figure out how to get host name in kernel.
236 	 * Unsure if this even should be done.
237 	 */
238 	if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_VOLROOT) ||
239 	    IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_HOST)) {
240 		cmn_err(CE_NOTE,
241 			"VOLUME ROOT and NAME_HOST currently unsupported\n");
242 		return;
243 	}
244 
245 	/*
246 	 * Remember we must strncpy to the end of the curent string
247 	 * name because there might be something there.  Also, don't
248 	 * go past the max_name_len system boundry.
249 	 */
250 	tmp_name_len = strlen((char *)to_string);
251 
252 	SUA_string_len = (tmp_name_len + SUA_string_len) > max_name_len ?
253 		(max_name_len - tmp_name_len) : (SUA_string_len);
254 
255 	(void) strncpy((char *)(to_string + tmp_name_len),
256 		(char *)SUA_string, SUA_string_len);
257 
258 	/* NULL terminate string */
259 	*(to_string + tmp_name_len + SUA_string_len) = '\0';
260 	*(to_string_len_p) += (int)SUA_string_len;
261 
262 	if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_CONTINUE))
263 		SET_NAME_BIT(*(name_flags_p), RRIP_NAME_CONTINUE);
264 	else
265 		SET_NAME_BIT(*(name_flags_p), RRIP_NAME_CHANGE);
266 
267 }
268 
269 /*
270  * rrip_name()
271  *
272  * sig_handler() for RRIP signature RRIP_NM
273  *
274  * This function handles the name of the current file.  It is case
275  * sensitive to whatever was put into the field and does NO
276  * translation. It will take whatever characters were in the field.
277  *
278  * Because the flags effect the way the name is parsed the same way
279  * that the sym_link component parsing is done, we will use the same
280  * function to do the actual parsing.
281  */
282 uchar_t  *
283 rrip_name(sig_args_t *sig_args_p)
284 {
285 	uchar_t *nm_ptr = sig_args_p->SUF_ptr;
286 
287 	if ((sig_args_p->name_p == (uchar_t *)NULL) ||
288 	    (sig_args_p->name_len_p == (int *)NULL))
289 		goto end;
290 	/*
291 	 * If we have a "." or ".." directory, we should not look for
292 	 * an alternate name
293 	 */
294 	if (HDE_NAME_LEN(sig_args_p->dirp) == 1) {
295 		if (*((char *)HDE_name(sig_args_p->dirp)) == '\0') {
296 			(void) strcpy((char *)sig_args_p->name_p, ".");
297 			*sig_args_p->name_len_p = 1;
298 			goto end;
299 		} else if (*((char *)HDE_name(sig_args_p->dirp)) == '\1') {
300 			(void) strcpy((char *)sig_args_p->name_p, "..");
301 			*sig_args_p->name_len_p = 2;
302 			goto end;
303 		}
304 	}
305 
306 	name_parse((int)RRIP_NAME_FLAGS(nm_ptr), RRIP_name(nm_ptr),
307 	    (size_t)RRIP_NAME_LEN(nm_ptr), sig_args_p->name_p,
308 	    sig_args_p->name_len_p, &(sig_args_p->name_flags),
309 	    MAXNAMELEN);
310 
311 end:
312 	return (nm_ptr + SUF_LEN(nm_ptr));
313 }
314 
315 
316 /*
317  * rrip_sym_link()
318  *
319  * sig_handler() for RRIP signature RRIP_SL
320  *
321  * creates a symlink buffer to simulate sym_links.
322  */
323 uchar_t *
324 rrip_sym_link(sig_args_t *sig_args_p)
325 {
326 	uchar_t	*sl_ptr = sig_args_p->SUF_ptr;
327 	uchar_t	*comp_ptr;
328 	char 	*tmp_sym_link;
329 	struct hs_direntry *hdp = sig_args_p->hdp;
330 	int	sym_link_len;
331 	char	*sym_link;
332 
333 	if (hdp->type != VLNK)
334 		goto end;
335 
336 	/*
337 	 * If the sym link has already been created, don't recreate it
338 	 */
339 	if (IS_NAME_BIT_SET(sig_args_p->name_flags, RRIP_SYM_LINK_COMPLETE))
340 		goto end;
341 
342 	sym_link = kmem_alloc(MAXPATHLEN + 1, KM_SLEEP);
343 
344 	/*
345 	 * If there is an original string put it into sym_link[], otherwise
346 	 * initialize sym_link[] to the empty string.
347 	 */
348 	if (hdp->sym_link != (char *)NULL) {
349 		sym_link_len = (int)strlen(strcpy(sym_link, hdp->sym_link));
350 	} else {
351 		sym_link[0] = '\0';
352 		sym_link_len = 0;
353 	}
354 
355 	/* for all components */
356 	for (comp_ptr = RRIP_sl_comp(sl_ptr);
357 		comp_ptr < (sl_ptr + SUF_LEN(sl_ptr));
358 		comp_ptr += RRIP_COMP_LEN(comp_ptr)) {
359 
360 		name_parse((int)RRIP_COMP_FLAGS(comp_ptr),
361 		    RRIP_comp(comp_ptr),
362 		    (size_t)RRIP_COMP_NAME_LEN(comp_ptr), (uchar_t *)sym_link,
363 		    &sym_link_len, &(sig_args_p->name_flags),
364 		    MAXPATHLEN);
365 
366 		/*
367 		 * If the component is continued, Don't put a
368 		 * '/' in the pathname, but do NULL terminate it.
369 		 * And avoid 2 '//' in a row, or if '/' was wanted
370 		 */
371 		if (IS_NAME_BIT_SET(RRIP_COMP_FLAGS(comp_ptr),
372 				    RRIP_NAME_CONTINUE) ||
373 		    (sym_link[sym_link_len - 1] == '/')) {
374 
375 			sym_link[sym_link_len] = '\0';
376 		} else {
377 			sym_link[sym_link_len] = '/';
378 			sym_link[sym_link_len + 1] = '\0';
379 
380 			/* add 1 to sym_link_len for '/' */
381 			sym_link_len++;
382 		}
383 
384 	}
385 
386 	/*
387 	 * take out  the last slash
388 	 */
389 	if (sym_link[sym_link_len - 1] == '/')
390 		sym_link[--sym_link_len] = '\0';
391 
392 	/*
393 	 * if no memory has been allocated, get some, otherwise, append
394 	 * to the current allocation
395 	 */
396 
397 	tmp_sym_link = kmem_alloc(SYM_LINK_LEN(sym_link), KM_SLEEP);
398 
399 	(void) strcpy(tmp_sym_link, sym_link);
400 
401 	if (hdp->sym_link != (char *)NULL)
402 		kmem_free(hdp->sym_link, (size_t)(hdp->ext_size + 1));
403 
404 	hdp->sym_link = (char *)&tmp_sym_link[0];
405 	/* the size of a symlink is its length */
406 	hdp->ext_size = (uint_t)strlen(tmp_sym_link);
407 
408 	if (!IS_NAME_BIT_SET(RRIP_SL_FLAGS(sl_ptr), RRIP_NAME_CONTINUE)) {
409 		/* reached the end of the symbolic link */
410 		SET_NAME_BIT(sig_args_p->name_flags, RRIP_SYM_LINK_COMPLETE);
411 	}
412 
413 	kmem_free(sym_link, MAXPATHLEN + 1);
414 end:
415 	return (sl_ptr + SUF_LEN(sl_ptr));
416 }
417 
418 /*
419  * rrip_namecopy()
420  *
421  * This function will copy the rrip name to the "to" buffer, if it
422  * exists.
423  *
424  * XXX -  We should speed this up by implementing the search in
425  * parse_sua().  It works right now, so I don't want to mess with it.
426  */
427 int
428 rrip_namecopy(
429 	char 	*from,			/* name to copy */
430 	char 	*to,			/* string to copy "from" to */
431 	char  	*tmp_name,		/* temp storage for original name */
432 	uchar_t	*dirp,			/* directory entry pointer */
433 	struct 	hsfs *fsp,		/* filesystem pointer */
434 	struct 	hs_direntry *hdp)	/* directory entry pointer to put */
435 					/* all that good info in */
436 {
437 	int	size = 0;
438 	int	change_flag = 0;
439 	int	ret_val;
440 
441 	if ((to == (char *)NULL) ||
442 	    (from == (char *)NULL) ||
443 	    (dirp == (uchar_t *)NULL)) {
444 		return (0);
445 	}
446 
447 	/* special handling for '.' and '..' */
448 
449 	if (HDE_NAME_LEN(dirp) == 1) {
450 		if (*((char *)HDE_name(dirp)) == '\0') {
451 			(void) strcpy(to, ".");
452 			return (1);
453 		} else if (*((char *)HDE_name(dirp)) == '\1') {
454 			(void) strcpy(to, "..");
455 			return (2);
456 		}
457 	}
458 
459 
460 	ret_val = parse_sua((uchar_t *)to, &size, &change_flag, dirp,
461 			hdp, fsp, (uchar_t *)NULL, NULL);
462 
463 	if (IS_NAME_BIT_SET(change_flag, RRIP_NAME_CHANGE) && !ret_val)
464 		return (size);
465 
466 	/*
467 	 * Well, the name was not found
468 	 *
469 	 * make rripname an upper case "nm" (to), so that
470 	 * we can compare it the current HDE_DIR_NAME()
471 	 * without nuking the original "nm", for future case
472 	 * sensitive name comparing
473 	 */
474 	(void) strcpy(tmp_name, from);		/* keep original */
475 	size = hs_uppercase_copy(tmp_name, from, (int)strlen(from));
476 
477 	return (-1);
478 }
479 
480 
481 
482 /*
483  * rrip_reloc_dir()
484  *
485  * This function is fairly bogus.  All it does is cause a failure of
486  * the hs_parsedir, so that no vnode will be made for it and
487  * essentially, the directory will no longer be seen.  This is part
488  * of the directory hierarchy mess, where the relocated directory will
489  * be hidden as far as ISO 9660 is concerned.  When we hit the child
490  * link "CL" SUF, then we will read the new directory.
491  */
492 uchar_t *
493 rrip_reloc_dir(sig_args_t *sig_args_p)
494 {
495 	uchar_t *re_ptr = sig_args_p->SUF_ptr;
496 
497 	sig_args_p->flags = RELOC_DIR;
498 
499 	return (re_ptr + SUF_LEN(re_ptr));
500 }
501 
502 
503 
504 /*
505  * rrip_child_link()
506  *
507  * This is the child link part of the directory hierarchy stuff and
508  * this does not really do much anyway.  All it does is read the
509  * directory entry that the child link SUF contains.  All should be
510  * fine then.
511  */
512 uchar_t *
513 rrip_child_link(sig_args_t *sig_args_p)
514 {
515 	uchar_t *cl_ptr = sig_args_p->SUF_ptr;
516 
517 	sig_args_p->hdp->ext_lbn = RRIP_CHILD_LBN(cl_ptr);
518 
519 	hs_filldirent(sig_args_p->fsp->hsfs_rootvp, sig_args_p->hdp);
520 
521 	sig_args_p->flags = 0;
522 
523 	return (cl_ptr + SUF_LEN(cl_ptr));
524 }
525 
526 
527 /*
528  * rrip_parent_link()
529  *
530  * This is the parent link part of the directory hierarchy stuff and
531  * this does not really do much anyway.  All it does is read the
532  * directory entry that the parent link SUF contains.  All should be
533  * fine then.
534  */
535 uchar_t *
536 rrip_parent_link(sig_args_t *sig_args_p)
537 {
538 	uchar_t *pl_ptr = sig_args_p->SUF_ptr;
539 
540 	sig_args_p->hdp->ext_lbn = RRIP_PARENT_LBN(pl_ptr);
541 
542 	hs_filldirent(sig_args_p->fsp->hsfs_rootvp, sig_args_p->hdp);
543 
544 	sig_args_p->flags = 0;
545 
546 	return (pl_ptr + SUF_LEN(pl_ptr));
547 }
548 
549 
550 /*
551  * rrip_rock_ridge()
552  *
553  * This function is supposed to aid in speed of the filesystem.
554  *
555  * XXX - It is only here as a place holder so far.
556  */
557 uchar_t *
558 rrip_rock_ridge(sig_args_t *sig_args_p)
559 {
560 	uchar_t *rr_ptr = sig_args_p->SUF_ptr;
561 
562 	return (rr_ptr + SUF_LEN(rr_ptr));
563 }
564