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