xref: /freebsd/sys/ufs/ffs/ffs_subr.c (revision 848ee2a3a8b47c9905fc51fefcf60eb371edbb98)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)ffs_subr.c	8.5 (Berkeley) 3/21/95
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 
39 #ifndef _KERNEL
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <time.h>
44 #include <sys/errno.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ffs/fs.h>
47 
48 uint32_t calculate_crc32c(uint32_t, const void *, size_t);
49 uint32_t ffs_calc_sbhash(struct fs *);
50 struct malloc_type;
51 #define UFS_MALLOC(size, type, flags) malloc(size)
52 #define UFS_FREE(ptr, type) free(ptr)
53 
54 #else /* _KERNEL */
55 #include <sys/systm.h>
56 #include <sys/gsb_crc32.h>
57 #include <sys/lock.h>
58 #include <sys/malloc.h>
59 #include <sys/mount.h>
60 #include <sys/vnode.h>
61 #include <sys/bio.h>
62 #include <sys/buf.h>
63 #include <sys/ucred.h>
64 
65 #include <ufs/ufs/quota.h>
66 #include <ufs/ufs/inode.h>
67 #include <ufs/ufs/extattr.h>
68 #include <ufs/ufs/ufsmount.h>
69 #include <ufs/ufs/ufs_extern.h>
70 #include <ufs/ffs/ffs_extern.h>
71 #include <ufs/ffs/fs.h>
72 
73 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
74 #define UFS_FREE(ptr, type) free(ptr, type)
75 
76 #endif /* _KERNEL */
77 
78 /*
79  * Verify an inode check-hash.
80  */
81 int
82 ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
83 {
84 	uint32_t ckhash, save_ckhash;
85 
86 	/*
87 	 * Return success if unallocated or we are not doing inode check-hash.
88 	 */
89 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
90 		return (0);
91 	/*
92 	 * Exclude di_ckhash from the crc32 calculation, e.g., always use
93 	 * a check-hash value of zero when calculating the check-hash.
94 	 */
95 	save_ckhash = dip->di_ckhash;
96 	dip->di_ckhash = 0;
97 	ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
98 	dip->di_ckhash = save_ckhash;
99 	if (save_ckhash == ckhash)
100 		return (0);
101 	return (EINVAL);
102 }
103 
104 /*
105  * Update an inode check-hash.
106  */
107 void
108 ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
109 {
110 
111 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
112 		return;
113 	/*
114 	 * Exclude old di_ckhash from the crc32 calculation, e.g., always use
115 	 * a check-hash value of zero when calculating the new check-hash.
116 	 */
117 	dip->di_ckhash = 0;
118 	dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
119 }
120 
121 /*
122  * These are the low-level functions that actually read and write
123  * the superblock and its associated data.
124  */
125 static off_t sblock_try[] = SBLOCKSEARCH;
126 static int readsuper(void *, struct fs **, off_t, int, int,
127 	int (*)(void *, off_t, void **, int));
128 
129 /*
130  * Read a superblock from the devfd device.
131  *
132  * If an alternate superblock is specified, it is read. Otherwise the
133  * set of locations given in the SBLOCKSEARCH list is searched for a
134  * superblock. Memory is allocated for the superblock by the readfunc and
135  * is returned. If filltype is non-NULL, additional memory is allocated
136  * of type filltype and filled in with the superblock summary information.
137  * All memory is freed when any error is returned.
138  *
139  * If a superblock is found, zero is returned. Otherwise one of the
140  * following error values is returned:
141  *     EIO: non-existent or truncated superblock.
142  *     EIO: error reading summary information.
143  *     ENOENT: no usable known superblock found.
144  *     ENOSPC: failed to allocate space for the superblock.
145  *     EINVAL: The previous newfs operation on this volume did not complete.
146  *         The administrator must complete newfs before using this volume.
147  */
148 int
149 ffs_sbget(void *devfd, struct fs **fsp, off_t altsblock,
150     struct malloc_type *filltype,
151     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
152 {
153 	struct fs *fs;
154 	struct fs_summary_info *fs_si;
155 	int i, error, size, blks;
156 	uint8_t *space;
157 	int32_t *lp;
158 	char *buf;
159 
160 	fs = NULL;
161 	*fsp = NULL;
162 	if (altsblock >= 0) {
163 		if ((error = readsuper(devfd, &fs, altsblock, 1, 0,
164 		     readfunc)) != 0) {
165 			if (fs != NULL)
166 				UFS_FREE(fs, filltype);
167 			return (error);
168 		}
169 	} else {
170 		for (i = 0; sblock_try[i] != -1; i++) {
171 			if ((error = readsuper(devfd, &fs, sblock_try[i], 0,
172 			     altsblock, readfunc)) == 0)
173 				break;
174 			if (fs != NULL) {
175 				UFS_FREE(fs, filltype);
176 				fs = NULL;
177 			}
178 			if (error == ENOENT)
179 				continue;
180 			return (error);
181 		}
182 		if (sblock_try[i] == -1)
183 			return (ENOENT);
184 	}
185 	/*
186 	 * Read in the superblock summary information.
187 	 */
188 	size = fs->fs_cssize;
189 	blks = howmany(size, fs->fs_fsize);
190 	if (fs->fs_contigsumsize > 0)
191 		size += fs->fs_ncg * sizeof(int32_t);
192 	size += fs->fs_ncg * sizeof(u_int8_t);
193 	/* When running in libufs or libsa, UFS_MALLOC may fail */
194 	if ((fs_si = UFS_MALLOC(sizeof(*fs_si), filltype, M_WAITOK)) == NULL) {
195 		UFS_FREE(fs, filltype);
196 		return (ENOSPC);
197 	}
198 	bzero(fs_si, sizeof(*fs_si));
199 	fs->fs_si = fs_si;
200 	if ((space = UFS_MALLOC(size, filltype, M_WAITOK)) == NULL) {
201 		UFS_FREE(fs->fs_si, filltype);
202 		UFS_FREE(fs, filltype);
203 		return (ENOSPC);
204 	}
205 	fs->fs_csp = (struct csum *)space;
206 	for (i = 0; i < blks; i += fs->fs_frag) {
207 		size = fs->fs_bsize;
208 		if (i + fs->fs_frag > blks)
209 			size = (blks - i) * fs->fs_fsize;
210 		buf = NULL;
211 		error = (*readfunc)(devfd,
212 		    dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size);
213 		if (error) {
214 			if (buf != NULL)
215 				UFS_FREE(buf, filltype);
216 			UFS_FREE(fs->fs_csp, filltype);
217 			UFS_FREE(fs->fs_si, filltype);
218 			UFS_FREE(fs, filltype);
219 			return (error);
220 		}
221 		memcpy(space, buf, size);
222 		UFS_FREE(buf, filltype);
223 		space += size;
224 	}
225 	if (fs->fs_contigsumsize > 0) {
226 		fs->fs_maxcluster = lp = (int32_t *)space;
227 		for (i = 0; i < fs->fs_ncg; i++)
228 			*lp++ = fs->fs_contigsumsize;
229 		space = (uint8_t *)lp;
230 	}
231 	size = fs->fs_ncg * sizeof(u_int8_t);
232 	fs->fs_contigdirs = (u_int8_t *)space;
233 	bzero(fs->fs_contigdirs, size);
234 	*fsp = fs;
235 	return (0);
236 }
237 
238 /*
239  * Try to read a superblock from the location specified by sblockloc.
240  * Return zero on success or an errno on failure.
241  */
242 static int
243 readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int isaltsblk,
244     int chkhash, int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
245 {
246 	struct fs *fs;
247 	int error, res;
248 	uint32_t ckhash;
249 
250 	error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE);
251 	if (error != 0)
252 		return (error);
253 	fs = *fsp;
254 	if (fs->fs_magic == FS_BAD_MAGIC)
255 		return (EINVAL);
256 	if (((fs->fs_magic == FS_UFS1_MAGIC && (isaltsblk ||
257 	      sblockloc <= SBLOCK_UFS1)) ||
258 	     (fs->fs_magic == FS_UFS2_MAGIC && (isaltsblk ||
259 	      sblockloc == fs->fs_sblockloc))) &&
260 	    fs->fs_ncg >= 1 &&
261 	    fs->fs_bsize >= MINBSIZE &&
262 	    fs->fs_bsize <= MAXBSIZE &&
263 	    fs->fs_bsize >= roundup(sizeof(struct fs), DEV_BSIZE) &&
264 	    fs->fs_sbsize <= SBLOCKSIZE) {
265 		/*
266 		 * If the filesystem has been run on a kernel without
267 		 * metadata check hashes, disable them.
268 		 */
269 		if ((fs->fs_flags & FS_METACKHASH) == 0)
270 			fs->fs_metackhash = 0;
271 		/*
272 		 * Clear any check-hashes that are not maintained
273 		 * by this kernel. Also clear any unsupported flags.
274 		 */
275 		fs->fs_metackhash &= CK_SUPPORTED;
276 		fs->fs_flags &= FS_SUPPORTED;
277 		if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) {
278 			if (chkhash == STDSB_NOMSG)
279 				return (EINTEGRITY);
280 			if (chkhash == STDSB_NOHASHFAIL_NOMSG) {
281 				fs->fs_flags |= FS_NEEDSFSCK;
282 				fs->fs_fmod = 1;
283 				return (0);
284 			}
285 #ifdef _KERNEL
286 			res = uprintf("Superblock check-hash failed: recorded "
287 			    "check-hash 0x%x != computed check-hash 0x%x%s\n",
288 			    fs->fs_ckhash, ckhash,
289 			    chkhash == STDSB_NOHASHFAIL ? " (Ignored)" : "");
290 #else
291 			res = 0;
292 #endif
293 			/*
294 			 * Print check-hash failure if no controlling terminal
295 			 * in kernel or always if in user-mode (libufs).
296 			 */
297 			if (res == 0)
298 				printf("Superblock check-hash failed: recorded "
299 				    "check-hash 0x%x != computed check-hash "
300 				    "0x%x%s\n", fs->fs_ckhash, ckhash,
301 				    chkhash == STDSB_NOHASHFAIL ?
302 				    " (Ignored)" : "");
303 			if (chkhash == STDSB)
304 				return (EINTEGRITY);
305 			/* chkhash == STDSB_NOHASHFAIL */
306 			fs->fs_flags |= FS_NEEDSFSCK;
307 			fs->fs_fmod = 1;
308 			return (0);
309 		}
310 		/* Have to set for old filesystems that predate this field */
311 		fs->fs_sblockactualloc = sblockloc;
312 		/* Not yet any summary information */
313 		fs->fs_si = NULL;
314 		return (0);
315 	}
316 	return (ENOENT);
317 }
318 
319 /*
320  * Write a superblock to the devfd device from the memory pointed to by fs.
321  * Write out the superblock summary information if it is present.
322  *
323  * If the write is successful, zero is returned. Otherwise one of the
324  * following error values is returned:
325  *     EIO: failed to write superblock.
326  *     EIO: failed to write superblock summary information.
327  */
328 int
329 ffs_sbput(void *devfd, struct fs *fs, off_t loc,
330     int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
331 {
332 	int i, error, blks, size;
333 	uint8_t *space;
334 
335 	/*
336 	 * If there is summary information, write it first, so if there
337 	 * is an error, the superblock will not be marked as clean.
338 	 */
339 	if (fs->fs_si != NULL && fs->fs_csp != NULL) {
340 		blks = howmany(fs->fs_cssize, fs->fs_fsize);
341 		space = (uint8_t *)fs->fs_csp;
342 		for (i = 0; i < blks; i += fs->fs_frag) {
343 			size = fs->fs_bsize;
344 			if (i + fs->fs_frag > blks)
345 				size = (blks - i) * fs->fs_fsize;
346 			if ((error = (*writefunc)(devfd,
347 			     dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
348 			     space, size)) != 0)
349 				return (error);
350 			space += size;
351 		}
352 	}
353 	fs->fs_fmod = 0;
354 #ifndef _KERNEL
355 	{
356 		struct fs_summary_info *fs_si;
357 
358 		fs->fs_time = time(NULL);
359 		/* Clear the pointers for the duration of writing. */
360 		fs_si = fs->fs_si;
361 		fs->fs_si = NULL;
362 		fs->fs_ckhash = ffs_calc_sbhash(fs);
363 		error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
364 		fs->fs_si = fs_si;
365 	}
366 #else /* _KERNEL */
367 	fs->fs_time = time_second;
368 	fs->fs_ckhash = ffs_calc_sbhash(fs);
369 	error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
370 #endif /* _KERNEL */
371 	return (error);
372 }
373 
374 /*
375  * Calculate the check-hash for a superblock.
376  */
377 uint32_t
378 ffs_calc_sbhash(struct fs *fs)
379 {
380 	uint32_t ckhash, save_ckhash;
381 
382 	/*
383 	 * A filesystem that was using a superblock ckhash may be moved
384 	 * to an older kernel that does not support ckhashes. The
385 	 * older kernel will clear the FS_METACKHASH flag indicating
386 	 * that it does not update hashes. When the disk is moved back
387 	 * to a kernel capable of ckhashes it disables them on mount:
388 	 *
389 	 *	if ((fs->fs_flags & FS_METACKHASH) == 0)
390 	 *		fs->fs_metackhash = 0;
391 	 *
392 	 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an
393 	 * old stale value in the fs->fs_ckhash field. Thus the need to
394 	 * just accept what is there.
395 	 */
396 	if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0)
397 		return (fs->fs_ckhash);
398 
399 	save_ckhash = fs->fs_ckhash;
400 	fs->fs_ckhash = 0;
401 	/*
402 	 * If newly read from disk, the caller is responsible for
403 	 * verifying that fs->fs_sbsize <= SBLOCKSIZE.
404 	 */
405 	ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize);
406 	fs->fs_ckhash = save_ckhash;
407 	return (ckhash);
408 }
409 
410 /*
411  * Update the frsum fields to reflect addition or deletion
412  * of some frags.
413  */
414 void
415 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
416 {
417 	int inblk;
418 	int field, subfield;
419 	int siz, pos;
420 
421 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
422 	fragmap <<= 1;
423 	for (siz = 1; siz < fs->fs_frag; siz++) {
424 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
425 			continue;
426 		field = around[siz];
427 		subfield = inside[siz];
428 		for (pos = siz; pos <= fs->fs_frag; pos++) {
429 			if ((fragmap & field) == subfield) {
430 				fraglist[siz] += cnt;
431 				pos += siz;
432 				field <<= siz;
433 				subfield <<= siz;
434 			}
435 			field <<= 1;
436 			subfield <<= 1;
437 		}
438 	}
439 }
440 
441 /*
442  * block operations
443  *
444  * check if a block is available
445  */
446 int
447 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
448 {
449 	unsigned char mask;
450 
451 	switch ((int)fs->fs_frag) {
452 	case 8:
453 		return (cp[h] == 0xff);
454 	case 4:
455 		mask = 0x0f << ((h & 0x1) << 2);
456 		return ((cp[h >> 1] & mask) == mask);
457 	case 2:
458 		mask = 0x03 << ((h & 0x3) << 1);
459 		return ((cp[h >> 2] & mask) == mask);
460 	case 1:
461 		mask = 0x01 << (h & 0x7);
462 		return ((cp[h >> 3] & mask) == mask);
463 	default:
464 #ifdef _KERNEL
465 		panic("ffs_isblock");
466 #endif
467 		break;
468 	}
469 	return (0);
470 }
471 
472 /*
473  * check if a block is free
474  */
475 int
476 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
477 {
478 
479 	switch ((int)fs->fs_frag) {
480 	case 8:
481 		return (cp[h] == 0);
482 	case 4:
483 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
484 	case 2:
485 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
486 	case 1:
487 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
488 	default:
489 #ifdef _KERNEL
490 		panic("ffs_isfreeblock");
491 #endif
492 		break;
493 	}
494 	return (0);
495 }
496 
497 /*
498  * take a block out of the map
499  */
500 void
501 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
502 {
503 
504 	switch ((int)fs->fs_frag) {
505 	case 8:
506 		cp[h] = 0;
507 		return;
508 	case 4:
509 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
510 		return;
511 	case 2:
512 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
513 		return;
514 	case 1:
515 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
516 		return;
517 	default:
518 #ifdef _KERNEL
519 		panic("ffs_clrblock");
520 #endif
521 		break;
522 	}
523 }
524 
525 /*
526  * put a block into the map
527  */
528 void
529 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
530 {
531 
532 	switch ((int)fs->fs_frag) {
533 	case 8:
534 		cp[h] = 0xff;
535 		return;
536 	case 4:
537 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
538 		return;
539 	case 2:
540 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
541 		return;
542 	case 1:
543 		cp[h >> 3] |= (0x01 << (h & 0x7));
544 		return;
545 	default:
546 #ifdef _KERNEL
547 		panic("ffs_setblock");
548 #endif
549 		break;
550 	}
551 }
552 
553 /*
554  * Update the cluster map because of an allocation or free.
555  *
556  * Cnt == 1 means free; cnt == -1 means allocating.
557  */
558 void
559 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
560 {
561 	int32_t *sump;
562 	int32_t *lp;
563 	u_char *freemapp, *mapp;
564 	int i, start, end, forw, back, map;
565 	u_int bit;
566 
567 	if (fs->fs_contigsumsize <= 0)
568 		return;
569 	freemapp = cg_clustersfree(cgp);
570 	sump = cg_clustersum(cgp);
571 	/*
572 	 * Allocate or clear the actual block.
573 	 */
574 	if (cnt > 0)
575 		setbit(freemapp, blkno);
576 	else
577 		clrbit(freemapp, blkno);
578 	/*
579 	 * Find the size of the cluster going forward.
580 	 */
581 	start = blkno + 1;
582 	end = start + fs->fs_contigsumsize;
583 	if (end >= cgp->cg_nclusterblks)
584 		end = cgp->cg_nclusterblks;
585 	mapp = &freemapp[start / NBBY];
586 	map = *mapp++;
587 	bit = 1U << (start % NBBY);
588 	for (i = start; i < end; i++) {
589 		if ((map & bit) == 0)
590 			break;
591 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
592 			bit <<= 1;
593 		} else {
594 			map = *mapp++;
595 			bit = 1;
596 		}
597 	}
598 	forw = i - start;
599 	/*
600 	 * Find the size of the cluster going backward.
601 	 */
602 	start = blkno - 1;
603 	end = start - fs->fs_contigsumsize;
604 	if (end < 0)
605 		end = -1;
606 	mapp = &freemapp[start / NBBY];
607 	map = *mapp--;
608 	bit = 1U << (start % NBBY);
609 	for (i = start; i > end; i--) {
610 		if ((map & bit) == 0)
611 			break;
612 		if ((i & (NBBY - 1)) != 0) {
613 			bit >>= 1;
614 		} else {
615 			map = *mapp--;
616 			bit = 1U << (NBBY - 1);
617 		}
618 	}
619 	back = start - i;
620 	/*
621 	 * Account for old cluster and the possibly new forward and
622 	 * back clusters.
623 	 */
624 	i = back + forw + 1;
625 	if (i > fs->fs_contigsumsize)
626 		i = fs->fs_contigsumsize;
627 	sump[i] += cnt;
628 	if (back > 0)
629 		sump[back] -= cnt;
630 	if (forw > 0)
631 		sump[forw] -= cnt;
632 	/*
633 	 * Update cluster summary information.
634 	 */
635 	lp = &sump[fs->fs_contigsumsize];
636 	for (i = fs->fs_contigsumsize; i > 0; i--)
637 		if (*lp-- > 0)
638 			break;
639 	fs->fs_maxcluster[cgp->cg_cgx] = i;
640 }
641