xref: /illumos-gate/usr/src/uts/common/fs/hsfs/hsfs_susp_subr.c (revision 7ee93e3bbce920c0d0742deb6632b0939e30b783)
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  * System Use Sharing protocol subroutines for High Sierra filesystem
23  *
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <sys/types.h>
31 #include <sys/t_lock.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/sysmacros.h>
35 #include <sys/kmem.h>
36 #include <sys/signal.h>
37 #include <sys/user.h>
38 #include <sys/proc.h>
39 #include <sys/disp.h>
40 #include <sys/buf.h>
41 #include <sys/pathname.h>
42 #include <sys/vfs.h>
43 #include <sys/vnode.h>
44 #include <sys/file.h>
45 #include <sys/uio.h>
46 #include <sys/conf.h>
47 
48 #include <vm/page.h>
49 
50 #include <sys/fs/hsfs_spec.h>
51 #include <sys/fs/hsfs_isospec.h>
52 #include <sys/fs/hsfs_node.h>
53 #include <sys/fs/hsfs_impl.h>
54 #include <sys/fs/hsfs_susp.h>
55 #include <sys/fs/hsfs_rrip.h>
56 
57 #include <sys/statvfs.h>
58 #include <sys/mount.h>
59 #include <sys/swap.h>
60 #include <sys/errno.h>
61 #include <sys/debug.h>
62 #include "fs/fs_subr.h"
63 #include <sys/cmn_err.h>
64 
65 /* static declarations */
66 static void free_cont_area(uchar_t *);
67 static int get_cont_area(struct hsfs *, uchar_t **, cont_info_t *);
68 static int parse_signatures(sig_args_t *, int, uchar_t *, int);
69 
70 /*
71  * parse_sua()
72  *
73  * This is the main SUSP routine, that gets all the SUA areas and
74  * continuations.	It calls parse_signatures() to actually interpret
75  * the signature fields.
76  *
77  * XXX - need to implement signature searching to speed things up and
78  *		which is needed for the api, which isn't done yet.
79  */
80 int
81 parse_sua(
82 	uchar_t			*name_p,	/* location to copy name */
83 	int			*name_len_p,	/* location to put name len */
84 	int			*name_change_p,	/* flags to signal name chg */
85 	uchar_t			*dirp,		/* pointer to ISO dir entry */
86 	struct hs_direntry	*hdp,		/* loc to store dir info */
87 	struct hsfs		*fsp,		/* filesystem pointer */
88 	uchar_t			*search_sig,	/* signature to search for */
89 	int			search_num)	/* n^th sig to search for */
90 {
91 	uchar_t			*SUA_p = IDE_sys_use_area(dirp);
92 	int			SUA_len = IDE_SUA_LEN(dirp);
93 	uchar_t			*tmp_SUA_p = (SUA_p + fsp->hsfs_sua_off);
94 	int			tmp_SUA_len = (SUA_len - fsp->hsfs_sua_off);
95 	short			ret_val = -1;
96 	uchar_t			*cont_p = (uchar_t *)NULL;
97 	sig_args_t		sig_args;
98 	cont_info_t		cont_info;
99 
100 	/*
101 	 * If there is no SUA, just return, no error
102 	 */
103 
104 	if (SUA_len == 0)
105 		return (0);
106 
107 	/*
108 	 * Underflow on the length field means there's a mismatch
109 	 * between sizes of SUA and ISO directory entry. This entry
110 	 * is corrupted, return an appropriate error.
111 	 */
112 	if (SUA_len < 0)
113 		return (SUA_EINVAL);
114 
115 	/*
116 	 * Make sure that the continuation lenth is zero, as that is
117 	 * the way to tell if we must grab another continuation area.
118 	 */
119 	bzero((char *)&cont_info, sizeof (cont_info));
120 
121 	sig_args.dirp		= dirp;
122 	sig_args.name_p		= name_p;
123 	sig_args.name_len_p	= name_len_p;
124 	sig_args.SUF_ptr	= tmp_SUA_p;
125 	sig_args.hdp		= hdp;
126 	sig_args.fsp		= fsp;
127 	sig_args.cont_info_p	= &cont_info;
128 	sig_args.flags		= 0;
129 	sig_args.name_flags	= 0;
130 
131 	/*
132 	 * Get ready to put in a new name.	 If no "NM" is found, then
133 	 * hs_namecopy will come to the rescue.  Make sure you don't
134 	 * have NULL names, also.
135 	 */
136 	if (name_p)
137 		*(name_p) = '\0';
138 	if (name_len_p)
139 		*(name_len_p) = 0;
140 
141 	while (ret_val == -1) {
142 		switch (parse_signatures(&sig_args, tmp_SUA_len, search_sig,
143 					search_num)) {
144 		case END_OF_SUA :
145 			if (cont_info.cont_len) {
146 
147 				if (get_cont_area(fsp, &cont_p, &cont_info)) {
148 					ret_val = 1;
149 					goto clean_up;
150 				}
151 
152 				sig_args.SUF_ptr = cont_p +
153 					cont_info.cont_offset;
154 
155 				tmp_SUA_len = cont_info.cont_len;
156 				cont_info.cont_len = 0;
157 
158 				continue;
159 			}
160 			sig_args.flags = 0;	/* reset */
161 			ret_val = 0;		/* keep going */
162 			break;
163 		case SUA_NULL_POINTER:
164 			ret_val = SUA_NULL_POINTER;
165 			goto clean_up;
166 		case SUA_ENOMEM:
167 			ret_val = SUA_ENOMEM;
168 			goto clean_up;
169 		case SUA_EINVAL:
170 			ret_val = SUA_EINVAL;
171 			goto clean_up;
172 		case RELOC_DIR:
173 			ret_val = RELOC_DIR;
174 			goto clean_up;
175 		}
176 	}
177 
178 	if (ret_val != 0)
179 		goto clean_up;
180 
181 	if (IS_NAME_BIT_SET(sig_args.name_flags, RRIP_NAME_CHANGE))
182 		SET_NAME_BIT(*(name_change_p), RRIP_NAME_CHANGE);
183 
184 clean_up:
185 	free_cont_area(cont_p);
186 	return (ret_val);
187 
188 }
189 
190 /*
191  * parse_signatures()
192  *
193  * Find the correct handling function for the signature string that is
194  * passed to this function.
195  *
196  * signature searching:
197  *
198  * The two arguments of search_sig and search_num are for finding the
199  * search_num^th occurance of the signature search_sig.  This will come
200  * in handy with searching for the "NM" field and is part of the api
201  * for rrip (which really can be used for any extension).
202  */
203 /*ARGSUSED*/
204 static int
205 parse_signatures(
206 	sig_args_t	*sig_args_p,
207 	int		SUA_len,
208 	uchar_t		*search_sig,	/* possible signature to search for */
209 	int		search_num)	/* n^th occurance of search_sig to */
210 					/*   search for */
211 {
212 	uchar_t			*sig_string = sig_args_p->SUF_ptr;
213 	extension_name_t	*extnp;
214 	ext_signature_t		*ext_sigp;
215 	int			impl_bit_num = 0;
216 	int			SUA_rem = SUA_len; /* SUA length */
217 					/* remaining to be parsed */
218 
219 	/* This should never happen ... just so we don't panic, literally */
220 	if (sig_string == (uchar_t *)NULL)
221 		return (SUA_NULL_POINTER);
222 
223 	if (SUA_len < 0)
224 		return (SUA_EINVAL);
225 
226 	/*
227 	 * Until the end of SUA, search for the signatures
228 	 * (check for end of SUA (2 consecutive NULL bytes)) or the
229 	 * remaining  length of the SUA is <= 3.  The minimum signature
230 	 * field  is 4.
231 	 */
232 
233 	while ((SUA_rem >= SUF_MIN_LEN) && (*sig_string != '\0') &&
234 		(*(sig_string + 1) != '\0')) {
235 
236 		/*
237 		 * Find appropriate extension and signature table
238 		 */
239 		for (extnp = extension_name_table, impl_bit_num = 0;
240 		    extnp->extension_name != (char *)NULL;
241 		    extnp++, impl_bit_num++)  {
242 
243 			/*
244 			 * look at an extension only if it is implemented
245 			 * on the CD-ROM
246 			 */
247 			if (!IS_IMPL_BIT_SET(sig_args_p->fsp, impl_bit_num))
248 				continue;
249 
250 			/*
251 			 * Find the appropriate signature
252 			 */
253 			for (ext_sigp = extnp->signature_table;
254 			    ext_sigp->ext_signature != (char *)NULL;
255 			    ext_sigp++)  {
256 
257 				if (strncmp((char *)sig_string,
258 					    ext_sigp->ext_signature,
259 					    SUF_SIG_LEN) == 0) {
260 
261 					SUA_rem -= SUF_LEN(sig_string);
262 					if (SUA_rem < 0)
263 						return (END_OF_SUA);
264 
265 					/*
266 					 * The SUA_len parameter specifies the
267 					 * length of the SUA that the kernel
268 					 * expects. There is also a length
269 					 * encoded in the SUA data. If they
270 					 * do not agree, bail out.
271 					 */
272 					if (SUA_len < SUF_LEN(sig_string)) {
273 						cmn_err(CE_NOTE,
274 					"parse_signatures: SUA length too big: "
275 					"expected=%d, found=%d",
276 						    SUA_len,
277 						    SUF_LEN(sig_string));
278 						return (SUA_EINVAL);
279 					}
280 
281 					sig_args_p->SUF_ptr = sig_string;
282 					sig_string =
283 					    (ext_sigp->sig_handler)(sig_args_p);
284 
285 					switch (sig_args_p->flags) {
286 					case END_OF_SUA :
287 						return (END_OF_SUA);
288 					case SUA_ENOMEM :
289 						return (SUA_ENOMEM);
290 					case SUA_EINVAL :
291 						return (SUA_EINVAL);
292 					case RELOC_DIR :
293 						return (RELOC_DIR);
294 					default :
295 #if NAME_SEARCH
296 						case NAME_CONTINUE :
297 							/* nothing for now */
298 						case NAME_CHANGE :
299 							/* nothing for now */
300 #endif
301 						break;
302 					}
303 
304 					/* reset to be zero */
305 
306 					sig_args_p->flags = 0;
307 					goto next_signature;
308 				}
309 
310 				/* off to the next signature .... */
311 
312 			} /* for ext_sigp */
313 
314 		} /* for extnp	(extension parsing) */
315 
316 		/*
317 		 * Opps, did not find this signature. We must
318 		 * advance on the the next signature in the SUA
319 		 * and pray to persumedly omniscient, omnipresent,
320 		 * almighty transcendental being(s) that the next
321 		 * record is in the susp format, or we get hosed.
322 		 */
323 		if (SUA_rem < SUF_MIN_LEN)
324 			return (END_OF_SUA);
325 
326 		SUA_rem -= SUF_LEN(sig_string);
327 		sig_string += SUF_LEN(sig_string);
328 
329 next_signature:
330 		/*
331 		 * Failsafe
332 		 */
333 		if (SUA_rem < SUF_MIN_LEN ||
334 		    sig_string == NULL || SUF_LEN(sig_string) <= 0) {
335 			return (END_OF_SUA);
336 		}
337 
338 	} /* while */
339 
340 	return (END_OF_SUA);
341 }
342 
343 /*
344  * hs_fill_root_dirent()
345  *
346  *
347  * This function reads the root directory extent to get to the SUA of
348  * the "." entry of the root directory.  It the checks to see if the
349  * susp is implemented.
350  */
351 void
352 hs_check_root_dirent(struct vnode *vp, struct hs_direntry *hdp)
353 {
354 	struct buf	*secbp;
355 	uchar_t		*root_ptr;
356 	uchar_t		*secp;
357 	uint_t		secno;
358 	offset_t	secoff;
359 	sig_args_t	sig_args;
360 	struct hsfs	*fsp;
361 	int		error;
362 
363 	if (vp->v_type != VDIR) {
364 		cmn_err(CE_NOTE,
365 		    "hs_check_root_dirent: vp (0x%p) not a directory",
366 		    (void *)vp);
367 		return;
368 	}
369 
370 	bzero((caddr_t)&sig_args, sizeof (sig_args));
371 
372 	fsp = VFS_TO_HSFS(vp->v_vfsp);
373 	secno = LBN_TO_SEC(hdp->ext_lbn+hdp->xar_len, vp->v_vfsp);
374 	secoff = LBN_TO_BYTE(hdp->ext_lbn+hdp->xar_len, vp->v_vfsp) &
375 		MAXHSOFFSET;
376 	secbp = bread(fsp->hsfs_devvp->v_rdev, secno * 4, HS_SECTOR_SIZE);
377 	error = geterror(secbp);
378 
379 	if (error != 0) {
380 		cmn_err(CE_NOTE,
381 			"hs_check_root_dirent: bread: error=(%d)", error);
382 		goto end;
383 	}
384 
385 	secp = (uchar_t *)secbp->b_un.b_addr;
386 	root_ptr = &secp[secoff];
387 
388 	/* quick check */
389 	if (hdp->ext_lbn != HDE_EXT_LBN(root_ptr)) {
390 		cmn_err(CE_NOTE, "hs_check_root_dirent: dirent not match\n");
391 		/* keep on going */
392 	}
393 
394 	/*
395 	 * Here, we know that the "." entry is the first in the sector
396 	 * just read (ISO 9660).  Let's now check for the sharing
397 	 * protocol and set call the susp sig_handler() if we should.
398 	 * Then we run through the hs_parsedir() function to catch all
399 	 * the other possibilities of SUSP fields and continuations.
400 	 *
401 	 * If there is no SUA area, just return, and assume ISO.
402 	 *
403 	 * If the SUA area length is invalid (negative, due to a mismatch
404 	 * between dirent size and SUA size), return and hope for the best.
405 	 */
406 
407 	if (IDE_SUA_LEN(root_ptr) <= 0)
408 		goto end;
409 
410 	if (strncmp(SUSP_SP, (char *)IDE_sys_use_area(root_ptr),
411 		    SUF_SIG_LEN) == 0) {
412 		/*
413 		 * We have a match of the sharing signature, so let's
414 		 * call the sig_handler to do what is necessary. We can
415 		 * ignore the return value, as implemented bits are set.
416 		 */
417 		sig_args.SUF_ptr = IDE_sys_use_area(root_ptr);
418 		sig_args.fsp	 = fsp;
419 
420 		if ((susp_sp->sig_handler)(&sig_args) == (uchar_t *)NULL) {
421 			goto end;
422 		}
423 	} else
424 		goto end;
425 
426 	(void) hs_parsedir(fsp, root_ptr, hdp, (char *)NULL, (int *)NULL);
427 
428 	/*
429 	 * If we did not get at least 1 extension, let's assume ISO and
430 	 * NULL out the implementation bits.
431 	 */
432 	if (fsp->hsfs_ext_impl <= 1L)
433 		fsp->hsfs_ext_impl = 0L;
434 
435 end:
436 	brelse(secbp);
437 }
438 
439 
440 /*
441  * get_cont_area()
442  *
443  * This function allocates a memory block, if necessary, and reads the
444  * continuation area into the allocated space.
445  *
446  * Return value : 	0 if the read and allocation went OK.
447  * 			1 if there was an error.
448  */
449 static int
450 get_cont_area(struct hsfs *fsp, uchar_t **buf_pp, cont_info_t *cont_info_p)
451 {
452 	struct buf	*secbp;
453 	int		error;
454 	uint_t		secno;
455 
456 	/*
457 	 * Guard against invalid continuation area records.
458 	 * Both cont_offset and cont_len must be no longer than
459 	 * HS_SECTOR_SIZE. If they are, return an error.
460 	 */
461 	if (cont_info_p->cont_offset > HS_SECTOR_SIZE ||
462 	    cont_info_p->cont_len > HS_SECTOR_SIZE) {
463 		cmn_err(CE_NOTE, "get_cont_area: invalid offset/length");
464 		return (1);
465 	}
466 
467 	if (*buf_pp == (uchar_t *)NULL)
468 		*buf_pp = kmem_alloc((size_t)HS_SECTOR_SIZE, KM_SLEEP);
469 
470 	secno = (uint_t)LBN_TO_SEC(cont_info_p->cont_lbn, fsp->hsfs_vfs);
471 	secbp = bread(fsp->hsfs_devvp->v_rdev, secno * 4, HS_SECTOR_SIZE);
472 	error = geterror(secbp);
473 
474 	if (error != 0) {
475 		cmn_err(CE_NOTE, "get_cont_area: bread: error=(%d)", error);
476 		brelse(secbp);
477 		return (1);
478 	}
479 
480 	/*
481 	 * This continuation area does not extend into the next sector
482 	 * so just copy the data to the buffer.
483 	 */
484 	if ((cont_info_p->cont_offset + cont_info_p->cont_len) <=
485 	    HS_SECTOR_SIZE) {
486 		bcopy(secbp->b_un.b_addr, (char *)*buf_pp, HS_SECTOR_SIZE);
487 	}
488 	/*
489 	 * This continuation area extends into the next sector so we
490 	 * need to do some dancing:
491 	 *
492 	 * - zero the return buffer so nothing random is returned
493 	 * - copy the partial data to the *beginning* of the return buffer
494 	 * - release the first sector's buffer
495 	 * - read the next sector
496 	 * - copy the remainder of the data to the return buffer
497 	 */
498 	else {
499 		uint_t	partial_size;
500 
501 		bzero((char *)*buf_pp, HS_SECTOR_SIZE);
502 		partial_size = HS_SECTOR_SIZE - cont_info_p->cont_offset;
503 		bcopy(&secbp->b_un.b_addr[cont_info_p->cont_offset],
504 			(char *)*buf_pp, partial_size);
505 		cont_info_p->cont_offset = 0;
506 		brelse(secbp);
507 
508 		secbp = bread(fsp->hsfs_devvp->v_rdev, (secno + 1) * 4,
509 				HS_SECTOR_SIZE);
510 		error = geterror(secbp);
511 		if (error != 0) {
512 			cmn_err(CE_NOTE, "get_cont_area: bread(2): error=(%d)",
513 				error);
514 			brelse(secbp);
515 			return (1);
516 		}
517 		bcopy(secbp->b_un.b_addr, (char *)&(*buf_pp)[partial_size],
518 			cont_info_p->cont_len - partial_size);
519 	}
520 
521 	brelse(secbp);
522 	return (0);
523 }
524 
525 
526 /*
527  * free_cont_area
528  *
529  * simple function to just free up memory, if it exists
530  *
531  */
532 static void
533 free_cont_area(uchar_t *cont_p)
534 {
535 	if (cont_p)
536 		(void) kmem_free((caddr_t)cont_p, (size_t)HS_SECTOR_SIZE);
537 	cont_p = (uchar_t *)NULL;
538 }
539