xref: /titanic_44/usr/src/uts/common/fs/hsfs/hsfs_rrip.c (revision 0398691684c2596072212e4ca9d7033ad7ccfa54)
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 2007 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 	if (SUF_LEN(px_ptr) >= RRIP_PX_SIZE)
133 		hdp->inode = (ino64_t)RRIP_INO(px_ptr);
134 	else
135 		hdp->inode = 0;
136 
137 	hdp->type = IFTOVT(hdp->mode);
138 
139 	return (px_ptr + SUF_LEN(px_ptr));
140 }
141 
142 /*
143  * rrip_file_time()
144  *
145  * support function for rrip_file_time()
146  *
147  * This function decides whether to parse the times in a long time form
148  * (17 bytes) or a short time form (7 bytes).  These time formats are
149  * defined in the ISO 9660 specification.
150  */
151 static void
152 form_time(int time_length, uchar_t *file_time, struct timeval *tvp)
153 {
154 	if (time_length == ISO_DATE_LEN)
155 		hs_parse_longdate(file_time, tvp);
156 	else
157 		hs_parse_dirdate(file_time, tvp);
158 
159 }
160 
161 /*
162  * rrip_file_time()
163  *
164  * sig_handler() for RRIP signature RRIP_TF
165  *
166  * This function parses out the file time attributes of a file from the
167  * "TI" SUF.  The times it parses are : st_mtime, st_atime and st_ctime.
168  *
169  * The function form_time is a support function only used in this
170  * function.
171  */
172 uchar_t *
173 rrip_file_time(sig_args_t *sig_args_p)
174 {
175 	uchar_t *tf_ptr = sig_args_p->SUF_ptr;
176 
177 	if (IS_TIME_BIT_SET(RRIP_TF_FLAGS(tf_ptr), RRIP_TF_ACCESS_BIT)) {
178 		form_time(RRIP_TF_TIME_LENGTH(tf_ptr),
179 		    RRIP_tf_access(tf_ptr),
180 		    &sig_args_p->hdp->adate);
181 	}
182 
183 	if (IS_TIME_BIT_SET(RRIP_TF_FLAGS(tf_ptr), RRIP_TF_MODIFY_BIT)) {
184 		form_time(RRIP_TF_TIME_LENGTH(tf_ptr), RRIP_tf_modify(tf_ptr),
185 		    &sig_args_p->hdp->mdate);
186 	}
187 
188 	if (IS_TIME_BIT_SET(RRIP_TF_FLAGS(tf_ptr), RRIP_TF_ATTRIBUTES_BIT)) {
189 		form_time(RRIP_TF_TIME_LENGTH(tf_ptr),
190 		    RRIP_tf_attributes(tf_ptr),
191 		    &sig_args_p->hdp->cdate);
192 	}
193 
194 	return (tf_ptr + SUF_LEN(tf_ptr));
195 }
196 
197 
198 
199 /*
200  * name_parse()
201  *
202  * This is a generic fuction used for sym links and filenames.  The
203  * flags passed to it effect the way the name/component field is parsed.
204  *
205  * The return value will be the NAME_CONTINUE or NAME_CHANGE value.
206  *
207  */
208 static void
209 name_parse(
210 	int		rrip_flags,	/* component/name flag */
211 	uchar_t		*SUA_string,	/* string from SUA */
212 	size_t		SUA_string_len, /* length of SUA string */
213 	uchar_t		*dst,		/* string to copy to */
214 	int		*dst_lenp,	/* ptr to cur. str len */
215 	ulong_t		*name_flags_p,	/* internal name flags */
216 	int		dst_size)	/* limit dest string to */
217 						/* this value */
218 {
219 	size_t	off;
220 	size_t	len;
221 
222 	if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_ROOT))
223 		(void) strcpy((char *)dst, "/");
224 
225 	if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_CURRENT)) {
226 		SUA_string = (uchar_t *)".";
227 		SUA_string_len = 1;
228 	}
229 
230 	if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_PARENT)) {
231 		SUA_string = (uchar_t *)"..";
232 		SUA_string_len = 2;
233 	}
234 
235 	/*
236 	 * XXX
237 	 * For now, ignore the following flags and return.
238 	 * have to figure out how to get host name in kernel.
239 	 * Unsure if this even should be done.
240 	 */
241 	if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_VOLROOT) ||
242 	    IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_HOST)) {
243 		cmn_err(CE_NOTE,
244 			"VOLUME ROOT and NAME_HOST currently unsupported\n");
245 		return;
246 	}
247 
248 	/*
249 	 * strlcat() has two nice properties:
250 	 * - the size of the output buffer includes the trailing '\0'
251 	 * - we pass "total size" not "remaining size"
252 	 * It'd be the ideal candidate for this codeblock - make it:
253 	 *
254 	 *	strlcat(dst, SUA_string,
255 	 *	    MIN(dstsize, strlen(dst) + SUA_string_len + 1));
256 	 *
257 	 * Unfortunately, strlcat() cannot deal with input strings
258 	 * that are not NULL-terminated - like SUA_string can be in
259 	 * our case here. So we can't use it :(
260 	 * Now strncat() doesn't work either - because it doesn't deal
261 	 * with strings for which the 'potential NULL-termination' isn't
262 	 * accessible - strncat(dst, NULL, 0) crashes although it copies
263 	 * nothing in any case. If the SUA ends on a mapping boundary,
264 	 * then telling strncat() to copy no more than the remaining bytes
265 	 * in the buffer is of no avail if there's no NULL byte in them.
266 	 *
267 	 * Hence - binary copy. What are all these str* funcs for ??
268 	 */
269 	dst_size--;	/* trailing '\0' */
270 
271 	off = strlen((char *)dst);
272 	len = MIN(dst_size - off, SUA_string_len);
273 	bcopy((char *)SUA_string, (char *)(dst + off), len);
274 	dst[off + len] = '\0';
275 	*dst_lenp = strlen((char *)dst);
276 
277 	if (IS_NAME_BIT_SET(rrip_flags, RRIP_NAME_CONTINUE))
278 		SET_NAME_BIT(*(name_flags_p), RRIP_NAME_CONTINUE);
279 	else
280 		SET_NAME_BIT(*(name_flags_p), RRIP_NAME_CHANGE);
281 
282 }
283 
284 /*
285  * rrip_name()
286  *
287  * sig_handler() for RRIP signature RRIP_NM
288  *
289  * This function handles the name of the current file.  It is case
290  * sensitive to whatever was put into the field and does NO
291  * translation. It will take whatever characters were in the field.
292  *
293  * Because the flags effect the way the name is parsed the same way
294  * that the sym_link component parsing is done, we will use the same
295  * function to do the actual parsing.
296  */
297 uchar_t  *
298 rrip_name(sig_args_t *sig_args_p)
299 {
300 	uchar_t *nm_ptr = sig_args_p->SUF_ptr;
301 
302 	if ((sig_args_p->name_p == (uchar_t *)NULL) ||
303 	    (sig_args_p->name_len_p == (int *)NULL))
304 		goto end;
305 	/*
306 	 * If we have a "." or ".." directory, we should not look for
307 	 * an alternate name
308 	 */
309 	if (HDE_NAME_LEN(sig_args_p->dirp) == 1) {
310 		if (*((char *)HDE_name(sig_args_p->dirp)) == '\0') {
311 			(void) strcpy((char *)sig_args_p->name_p, ".");
312 			*sig_args_p->name_len_p = 1;
313 			goto end;
314 		} else if (*((char *)HDE_name(sig_args_p->dirp)) == '\1') {
315 			(void) strcpy((char *)sig_args_p->name_p, "..");
316 			*sig_args_p->name_len_p = 2;
317 			goto end;
318 		}
319 	}
320 
321 	name_parse((int)RRIP_NAME_FLAGS(nm_ptr), RRIP_name(nm_ptr),
322 	    (size_t)RRIP_NAME_LEN(nm_ptr), sig_args_p->name_p,
323 	    sig_args_p->name_len_p, &(sig_args_p->name_flags),
324 	    MAXNAMELEN);
325 
326 end:
327 	return (nm_ptr + SUF_LEN(nm_ptr));
328 }
329 
330 
331 /*
332  * rrip_sym_link()
333  *
334  * sig_handler() for RRIP signature RRIP_SL
335  *
336  * creates a symlink buffer to simulate sym_links.
337  */
338 uchar_t *
339 rrip_sym_link(sig_args_t *sig_args_p)
340 {
341 	uchar_t	*sl_ptr = sig_args_p->SUF_ptr;
342 	uchar_t	*comp_ptr;
343 	char 	*tmp_sym_link;
344 	struct hs_direntry *hdp = sig_args_p->hdp;
345 	int	sym_link_len;
346 	char	*sym_link;
347 
348 	if (hdp->type != VLNK)
349 		goto end;
350 
351 	/*
352 	 * If the sym link has already been created, don't recreate it
353 	 */
354 	if (IS_NAME_BIT_SET(sig_args_p->name_flags, RRIP_SYM_LINK_COMPLETE))
355 		goto end;
356 
357 	sym_link = kmem_alloc(MAXPATHLEN + 1, KM_SLEEP);
358 
359 	/*
360 	 * If there is an original string put it into sym_link[], otherwise
361 	 * initialize sym_link[] to the empty string.
362 	 */
363 	if (hdp->sym_link != (char *)NULL) {
364 		sym_link_len = (int)strlen(strcpy(sym_link, hdp->sym_link));
365 	} else {
366 		sym_link[0] = '\0';
367 		sym_link_len = 0;
368 	}
369 
370 	/* for all components */
371 	for (comp_ptr = RRIP_sl_comp(sl_ptr);
372 	    comp_ptr < (sl_ptr + SUF_LEN(sl_ptr));
373 	    comp_ptr += RRIP_COMP_LEN(comp_ptr)) {
374 
375 		name_parse((int)RRIP_COMP_FLAGS(comp_ptr),
376 		    RRIP_comp(comp_ptr),
377 		    (size_t)RRIP_COMP_NAME_LEN(comp_ptr), (uchar_t *)sym_link,
378 		    &sym_link_len, &(sig_args_p->name_flags),
379 		    MAXPATHLEN);
380 
381 		/*
382 		 * If the component is continued, Don't put a
383 		 * '/' in the pathname, but do NULL terminate it.
384 		 * And avoid 2 '//' in a row, or if '/' was wanted
385 		 */
386 		if (IS_NAME_BIT_SET(RRIP_COMP_FLAGS(comp_ptr),
387 		    RRIP_NAME_CONTINUE) ||
388 		    (sym_link[sym_link_len - 1] == '/')) {
389 
390 			sym_link[sym_link_len] = '\0';
391 		} else {
392 			sym_link[sym_link_len] = '/';
393 			sym_link[sym_link_len + 1] = '\0';
394 
395 			/* add 1 to sym_link_len for '/' */
396 			sym_link_len++;
397 		}
398 
399 	}
400 
401 	/*
402 	 * take out  the last slash
403 	 */
404 	if (sym_link[sym_link_len - 1] == '/')
405 		sym_link[--sym_link_len] = '\0';
406 
407 	/*
408 	 * if no memory has been allocated, get some, otherwise, append
409 	 * to the current allocation
410 	 */
411 
412 	tmp_sym_link = kmem_alloc(SYM_LINK_LEN(sym_link), KM_SLEEP);
413 
414 	(void) strcpy(tmp_sym_link, sym_link);
415 
416 	if (hdp->sym_link != (char *)NULL)
417 		kmem_free(hdp->sym_link, (size_t)(hdp->ext_size + 1));
418 
419 	hdp->sym_link = (char *)&tmp_sym_link[0];
420 	/* the size of a symlink is its length */
421 	hdp->ext_size = (uint_t)strlen(tmp_sym_link);
422 
423 	if (!IS_NAME_BIT_SET(RRIP_SL_FLAGS(sl_ptr), RRIP_NAME_CONTINUE)) {
424 		/* reached the end of the symbolic link */
425 		SET_NAME_BIT(sig_args_p->name_flags, RRIP_SYM_LINK_COMPLETE);
426 	}
427 
428 	kmem_free(sym_link, MAXPATHLEN + 1);
429 end:
430 	return (sl_ptr + SUF_LEN(sl_ptr));
431 }
432 
433 /*
434  * rrip_namecopy()
435  *
436  * This function will copy the rrip name to the "to" buffer, if it
437  * exists.
438  *
439  * XXX -  We should speed this up by implementing the search in
440  * parse_sua().  It works right now, so I don't want to mess with it.
441  */
442 int
443 rrip_namecopy(
444 	char 	*from,			/* name to copy */
445 	char 	*to,			/* string to copy "from" to */
446 	char  	*tmp_name,		/* temp storage for original name */
447 	uchar_t	*dirp,			/* directory entry pointer */
448 	uint_t	last_offset,		/* last index into current dir block */
449 	struct 	hsfs *fsp,		/* filesystem pointer */
450 	struct 	hs_direntry *hdp)	/* directory entry pointer to put */
451 					/* all that good info in */
452 {
453 	int	size = 0;
454 	int	change_flag = 0;
455 	int	ret_val;
456 
457 	if ((to == (char *)NULL) ||
458 	    (from == (char *)NULL) ||
459 	    (dirp == (uchar_t *)NULL)) {
460 		return (0);
461 	}
462 
463 	/* special handling for '.' and '..' */
464 
465 	if (HDE_NAME_LEN(dirp) == 1) {
466 		if (*((char *)HDE_name(dirp)) == '\0') {
467 			(void) strcpy(to, ".");
468 			return (1);
469 		} else if (*((char *)HDE_name(dirp)) == '\1') {
470 			(void) strcpy(to, "..");
471 			return (2);
472 		}
473 	}
474 
475 
476 	ret_val = parse_sua((uchar_t *)to, &size, &change_flag,
477 			dirp, last_offset,
478 			hdp, fsp, (uchar_t *)NULL, NULL);
479 
480 	if (IS_NAME_BIT_SET(change_flag, RRIP_NAME_CHANGE) && !ret_val)
481 		return (size);
482 
483 	/*
484 	 * Well, the name was not found
485 	 *
486 	 * make rripname an upper case "nm" (to), so that
487 	 * we can compare it the current HDE_DIR_NAME()
488 	 * without nuking the original "nm", for future case
489 	 * sensitive name comparing
490 	 */
491 	(void) strcpy(tmp_name, from);		/* keep original */
492 	size = hs_uppercase_copy(tmp_name, from, (int)strlen(from));
493 
494 	return (-1);
495 }
496 
497 
498 
499 /*
500  * rrip_reloc_dir()
501  *
502  * This function is fairly bogus.  All it does is cause a failure of
503  * the hs_parsedir, so that no vnode will be made for it and
504  * essentially, the directory will no longer be seen.  This is part
505  * of the directory hierarchy mess, where the relocated directory will
506  * be hidden as far as ISO 9660 is concerned.  When we hit the child
507  * link "CL" SUF, then we will read the new directory.
508  */
509 uchar_t *
510 rrip_reloc_dir(sig_args_t *sig_args_p)
511 {
512 	uchar_t *re_ptr = sig_args_p->SUF_ptr;
513 
514 	sig_args_p->flags = RELOC_DIR;
515 
516 	return (re_ptr + SUF_LEN(re_ptr));
517 }
518 
519 
520 
521 /*
522  * rrip_child_link()
523  *
524  * This is the child link part of the directory hierarchy stuff and
525  * this does not really do much anyway.  All it does is read the
526  * directory entry that the child link SUF contains.  All should be
527  * fine then.
528  */
529 uchar_t *
530 rrip_child_link(sig_args_t *sig_args_p)
531 {
532 	uchar_t *cl_ptr = sig_args_p->SUF_ptr;
533 
534 	sig_args_p->hdp->ext_lbn = RRIP_CHILD_LBN(cl_ptr);
535 
536 	hs_filldirent(sig_args_p->fsp->hsfs_rootvp, sig_args_p->hdp);
537 
538 	sig_args_p->flags = 0;
539 
540 	return (cl_ptr + SUF_LEN(cl_ptr));
541 }
542 
543 
544 /*
545  * rrip_parent_link()
546  *
547  * This is the parent link part of the directory hierarchy stuff and
548  * this does not really do much anyway.  All it does is read the
549  * directory entry that the parent link SUF contains.  All should be
550  * fine then.
551  */
552 uchar_t *
553 rrip_parent_link(sig_args_t *sig_args_p)
554 {
555 	uchar_t *pl_ptr = sig_args_p->SUF_ptr;
556 
557 	sig_args_p->hdp->ext_lbn = RRIP_PARENT_LBN(pl_ptr);
558 
559 	hs_filldirent(sig_args_p->fsp->hsfs_rootvp, sig_args_p->hdp);
560 
561 	sig_args_p->flags = 0;
562 
563 	return (pl_ptr + SUF_LEN(pl_ptr));
564 }
565 
566 
567 /*
568  * rrip_rock_ridge()
569  *
570  * This function is supposed to aid in speed of the filesystem.
571  *
572  * XXX - It is only here as a place holder so far.
573  */
574 uchar_t *
575 rrip_rock_ridge(sig_args_t *sig_args_p)
576 {
577 	uchar_t *rr_ptr = sig_args_p->SUF_ptr;
578 
579 	return (rr_ptr + SUF_LEN(rr_ptr));
580 }
581