xref: /csrg-svn/sys/stand.att/sys.c (revision 30768)
1 /*
2  * Copyright (c) 1982, 1986 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)sys.c	7.3 (Berkeley) 04/02/87
7  */
8 
9 #include "param.h"
10 #include "inode.h"
11 #include "fs.h"
12 #include "dir.h"
13 #include "reboot.h"
14 #include "saio.h"
15 
16 ino_t	dlook();
17 
18 struct dirstuff {
19 	int loc;
20 	struct iob *io;
21 };
22 
23 static
24 openi(n, io)
25 	register struct iob *io;
26 {
27 	register struct dinode *dp;
28 	int cc;
29 
30 	io->i_offset = 0;
31 	io->i_bn = fsbtodb(&io->i_fs, itod(&io->i_fs, n)) + io->i_boff;
32 	io->i_cc = io->i_fs.fs_bsize;
33 	io->i_ma = io->i_buf;
34 	cc = devread(io);
35 	dp = (struct dinode *)io->i_buf;
36 	io->i_ino.i_ic = dp[itoo(&io->i_fs, n)].di_ic;
37 	return (cc);
38 }
39 
40 static
41 find(path, file)
42 	register char *path;
43 	struct iob *file;
44 {
45 	register char *q;
46 	char *dir;
47 	char c;
48 	int n;
49 
50 	if (path==NULL || *path=='\0') {
51 		printf("null path\n");
52 		return (0);
53 	}
54 
55 	if (openi((ino_t) ROOTINO, file) < 0) {
56 		printf("can't read root inode\n");
57 		return (0);
58 	}
59 	dir = path;
60 	while (*path) {
61 		while (*path == '/')
62 			path++;
63 		q = path;
64 		while(*q != '/' && *q != '\0')
65 			q++;
66 		c = *q;
67 		*q = '\0';
68 		if (q == path) path = "." ;	/* "/" means "/." */
69 
70 		if ((n = dlook(path, file, dir)) != 0) {
71 			if (c == '\0')
72 				break;
73 			if (openi(n, file) < 0)
74 				return (0);
75 			*q = c;
76 			path = q;
77 			continue;
78 		} else {
79 			printf("%s: not found\n", path);
80 			return (0);
81 		}
82 	}
83 	return (n);
84 }
85 
86 static daddr_t
87 sbmap(io, bn)
88 	register struct iob *io;
89 	daddr_t bn;
90 {
91 	register struct inode *ip;
92 	int i, j, sh;
93 	daddr_t nb, *bap;
94 
95 	ip = &io->i_ino;
96 	if (bn < 0) {
97 		printf("bn negative\n");
98 		return ((daddr_t)0);
99 	}
100 
101 	/*
102 	 * blocks 0..NDADDR are direct blocks
103 	 */
104 	if(bn < NDADDR) {
105 		nb = ip->i_db[bn];
106 		return (nb);
107 	}
108 
109 	/*
110 	 * addresses NIADDR have single and double indirect blocks.
111 	 * the first step is to determine how many levels of indirection.
112 	 */
113 	sh = 1;
114 	bn -= NDADDR;
115 	for (j = NIADDR; j > 0; j--) {
116 		sh *= NINDIR(&io->i_fs);
117 		if (bn < sh)
118 			break;
119 		bn -= sh;
120 	}
121 	if (j == 0) {
122 		printf("bn ovf %D\n", bn);
123 		return ((daddr_t)0);
124 	}
125 
126 	/*
127 	 * fetch the first indirect block address from the inode
128 	 */
129 	nb = ip->i_ib[NIADDR - j];
130 	if (nb == 0) {
131 		printf("bn void %D\n",bn);
132 		return ((daddr_t)0);
133 	}
134 
135 	/*
136 	 * fetch through the indirect blocks
137 	 */
138 	for (; j <= NIADDR; j++) {
139 		if (blknos[j] != nb) {
140 			io->i_bn = fsbtodb(&io->i_fs, nb) + io->i_boff;
141 			io->i_ma = b[j];
142 			io->i_cc = io->i_fs.fs_bsize;
143 			if (devread(io) != io->i_fs.fs_bsize) {
144 				if (io->i_error)
145 					errno = io->i_error;
146 				printf("bn %D: read error\n", io->i_bn);
147 				return ((daddr_t)0);
148 			}
149 			blknos[j] = nb;
150 		}
151 		bap = (daddr_t *)b[j];
152 		sh /= NINDIR(&io->i_fs);
153 		i = (bn / sh) % NINDIR(&io->i_fs);
154 		nb = bap[i];
155 		if(nb == 0) {
156 			printf("bn void %D\n",bn);
157 			return ((daddr_t)0);
158 		}
159 	}
160 	return (nb);
161 }
162 
163 static ino_t
164 dlook(s, io, dir)
165 	char *s;
166 	register struct iob *io;
167 	char *dir;
168 {
169 	register struct direct *dp;
170 	register struct inode *ip;
171 	struct dirstuff dirp;
172 	int len;
173 
174 	if (s == NULL || *s == '\0')
175 		return (0);
176 	ip = &io->i_ino;
177 	if ((ip->i_mode&IFMT) != IFDIR) {
178 		printf("not a directory\n");
179 		printf("%s: not a directory\n", dir);
180 		return (0);
181 	}
182 	if (ip->i_size == 0) {
183 		printf("%s: zero length directory\n", dir);
184 		return (0);
185 	}
186 	len = strlen(s);
187 	dirp.loc = 0;
188 	dirp.io = io;
189 	for (dp = readdir(&dirp); dp != NULL; dp = readdir(&dirp)) {
190 		if(dp->d_ino == 0)
191 			continue;
192 		if (dp->d_namlen == len && !strcmp(s, dp->d_name))
193 			return (dp->d_ino);
194 	}
195 	return (0);
196 }
197 
198 /*
199  * get next entry in a directory.
200  */
201 struct direct *
202 readdir(dirp)
203 	register struct dirstuff *dirp;
204 {
205 	register struct direct *dp;
206 	register struct iob *io;
207 	daddr_t lbn, d;
208 	int off;
209 
210 	io = dirp->io;
211 	for(;;) {
212 		if (dirp->loc >= io->i_ino.i_size)
213 			return (NULL);
214 		off = blkoff(&io->i_fs, dirp->loc);
215 		if (off == 0) {
216 			lbn = lblkno(&io->i_fs, dirp->loc);
217 			d = sbmap(io, lbn);
218 			if(d == 0)
219 				return NULL;
220 			io->i_bn = fsbtodb(&io->i_fs, d) + io->i_boff;
221 			io->i_ma = io->i_buf;
222 			io->i_cc = blksize(&io->i_fs, &io->i_ino, lbn);
223 			if (devread(io) < 0) {
224 				errno = io->i_error;
225 				printf("bn %D: directory read error\n",
226 					io->i_bn);
227 				return (NULL);
228 			}
229 		}
230 		dp = (struct direct *)(io->i_buf + off);
231 		dirp->loc += dp->d_reclen;
232 		if (dp->d_ino == 0)
233 			continue;
234 		return (dp);
235 	}
236 }
237 
238 lseek(fdesc, addr, ptr)
239 	int fdesc, ptr;
240 	off_t addr;
241 {
242 	register struct iob *io;
243 
244 #ifndef	SMALL
245 	if (ptr != 0) {
246 		printf("Seek not from beginning of file\n");
247 		errno = EOFFSET;
248 		return (-1);
249 	}
250 #endif SMALL
251 	fdesc -= 3;
252 	if (fdesc < 0 || fdesc >= NFILES ||
253 	    ((io = &iob[fdesc])->i_flgs & F_ALLOC) == 0) {
254 		errno = EBADF;
255 		return (-1);
256 	}
257 	io->i_offset = addr;
258 	io->i_bn = addr / DEV_BSIZE;
259 	io->i_cc = 0;
260 	return (0);
261 }
262 
263 getc(fdesc)
264 	int fdesc;
265 {
266 	register struct iob *io;
267 	register struct fs *fs;
268 	register char *p;
269 	int c, lbn, off, size, diff;
270 
271 
272 	if (fdesc >= 0 && fdesc <= 2)
273 		return (getchar());
274 	fdesc -= 3;
275 	if (fdesc < 0 || fdesc >= NFILES ||
276 	    ((io = &iob[fdesc])->i_flgs&F_ALLOC) == 0) {
277 		errno = EBADF;
278 		return (-1);
279 	}
280 	p = io->i_ma;
281 	if (io->i_cc <= 0) {
282 		if ((io->i_flgs & F_FILE) != 0) {
283 			diff = io->i_ino.i_size - io->i_offset;
284 			if (diff <= 0)
285 				return (-1);
286 			fs = &io->i_fs;
287 			lbn = lblkno(fs, io->i_offset);
288 			io->i_bn = fsbtodb(fs, sbmap(io, lbn)) + io->i_boff;
289 			off = blkoff(fs, io->i_offset);
290 			size = blksize(fs, &io->i_ino, lbn);
291 		} else {
292 			io->i_bn = io->i_offset / DEV_BSIZE;
293 			off = 0;
294 			size = DEV_BSIZE;
295 		}
296 		io->i_ma = io->i_buf;
297 		io->i_cc = size;
298 		if (devread(io) < 0) {
299 			errno = io->i_error;
300 			return (-1);
301 		}
302 		if ((io->i_flgs & F_FILE) != 0) {
303 			if (io->i_offset - off + size >= io->i_ino.i_size)
304 				io->i_cc = diff + off;
305 			io->i_cc -= off;
306 		}
307 		p = &io->i_buf[off];
308 	}
309 	io->i_cc--;
310 	io->i_offset++;
311 	c = (unsigned)*p++;
312 	io->i_ma = p;
313 	return (c);
314 }
315 
316 int	errno;
317 
318 read(fdesc, buf, count)
319 	int fdesc, count;
320 	char *buf;
321 {
322 	register i, size;
323 	register struct iob *file;
324 	register struct fs *fs;
325 	int lbn, off;
326 
327 	errno = 0;
328 	if (fdesc >= 0 & fdesc <= 2) {
329 		i = count;
330 		do {
331 			*buf = getchar();
332 		} while (--i && *buf++ != '\n');
333 		return (count - i);
334 	}
335 	fdesc -= 3;
336 	if (fdesc < 0 || fdesc >= NFILES ||
337 	    ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) {
338 		errno = EBADF;
339 		return (-1);
340 	}
341 	if ((file->i_flgs&F_READ) == 0) {
342 		errno = EBADF;
343 		return (-1);
344 	}
345 #ifndef	SMALL
346 	if ((file->i_flgs & F_FILE) == 0) {
347 		file->i_cc = count;
348 		file->i_ma = buf;
349 		file->i_bn = file->i_boff + (file->i_offset / DEV_BSIZE);
350 		i = devread(file);
351 		if (i < 0)
352 			errno = file->i_error;
353 		else
354 			file->i_offset += i;
355 		return (i);
356 	}
357 #endif SMALL
358 	if (file->i_offset+count > file->i_ino.i_size)
359 		count = file->i_ino.i_size - file->i_offset;
360 	if ((i = count) <= 0)
361 		return (0);
362 	/*
363 	 * While reading full blocks, do I/O into user buffer.
364 	 * Anything else uses getc().
365 	 */
366 	fs = &file->i_fs;
367 	while (i) {
368 		off = blkoff(fs, file->i_offset);
369 		lbn = lblkno(fs, file->i_offset);
370 		size = blksize(fs, &file->i_ino, lbn);
371 		if (off == 0 && size <= i) {
372 			file->i_bn = fsbtodb(fs, sbmap(file, lbn)) +
373 			    file->i_boff;
374 			file->i_cc = size;
375 			file->i_ma = buf;
376 			if (devread(file) < 0) {
377 				errno = file->i_error;
378 				return (-1);
379 			}
380 			file->i_offset += size;
381 			file->i_cc = 0;
382 			buf += size;
383 			i -= size;
384 		} else {
385 			size -= off;
386 			if (size > i)
387 				size = i;
388 			i -= size;
389 			do {
390 				*buf++ = getc(fdesc+3);
391 			} while (--size);
392 		}
393 	}
394 	return (count);
395 }
396 
397 #ifndef	SMALL
398 write(fdesc, buf, count)
399 	int fdesc, count;
400 	char *buf;
401 {
402 	register i;
403 	register struct iob *file;
404 
405 	errno = 0;
406 	if (fdesc >= 0 && fdesc <= 2) {
407 		i = count;
408 		while (i--)
409 			putchar(*buf++);
410 		return (count);
411 	}
412 	fdesc -= 3;
413 	if (fdesc < 0 || fdesc >= NFILES ||
414 	    ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) {
415 		errno = EBADF;
416 		return (-1);
417 	}
418 	if ((file->i_flgs&F_WRITE) == 0) {
419 		errno = EBADF;
420 		return (-1);
421 	}
422 	file->i_cc = count;
423 	file->i_ma = buf;
424 	file->i_bn = file->i_boff + (file->i_offset / DEV_BSIZE);
425 	i = devwrite(file);
426 	file->i_offset += count;
427 	if (i < 0)
428 		errno = file->i_error;
429 	return (i);
430 }
431 #endif SMALL
432 
433 int	openfirst = 1;
434 unsigned opendev;		/* last device opened */
435 extern	unsigned bootdev;
436 
437 open(str, how)
438 	char *str;
439 	int how;
440 {
441 	register char *cp;
442 	register int i;
443 	register struct iob *file;
444 	int fdesc;
445 	long atol();
446 
447 	if (openfirst) {
448 		for (i = 0; i < NFILES; i++)
449 			iob[i].i_flgs = 0;
450 		openfirst = 0;
451 	}
452 
453 	for (fdesc = 0; fdesc < NFILES; fdesc++)
454 		if (iob[fdesc].i_flgs == 0)
455 			goto gotfile;
456 	_stop("No more file slots");
457 gotfile:
458 	(file = &iob[fdesc])->i_flgs |= F_ALLOC;
459 
460 	for (cp = str; *cp && *cp != '/' && *cp != ':' && *cp != '('; cp++)
461 		;
462 	if (*cp == '(') {
463 		if ((file->i_ino.i_dev = getdev(str, cp - str)) == -1)
464 			goto bad;
465 		cp++;
466 		if ((file->i_unit = getunit(cp)) == -1)
467 			goto bad;
468 		for (; *cp != ','; cp++)
469 			if (*cp == NULL) {
470 				errno = EOFFSET;
471 				goto badspec;
472 			}
473 		file->i_boff = atol(++cp);
474 		for (;;) {
475 			if (*cp == ')')
476 				break;
477 			if (*cp++)
478 				continue;
479 			goto badspec;
480 		}
481 		cp++;
482 	} else if (*cp != ':') {
483 		/* default bootstrap unit and device */
484 		file->i_ino.i_dev = (bootdev >> B_TYPESHIFT) & B_TYPEMASK;
485 		file->i_unit = ((bootdev >> B_UNITSHIFT) & B_UNITMASK) +
486 		     (8 * ((bootdev >> B_ADAPTORSHIFT) & B_ADAPTORMASK));
487 		file->i_boff = (bootdev >> B_PARTITIONSHIFT) & B_PARTITIONMASK;
488 		cp = str;
489 	} else {
490 # define isdigit(n)	((n>='0') && (n<='9'))
491 		if (cp == str)
492 			goto badspec;
493 		/*
494 	 	 * syntax for possible device name:
495 	 	 *	<alpha-string><digit-string><letter>:
496 	 	 */
497 		for (cp = str; *cp != ':' && !isdigit(*cp); cp++)
498 			;
499 		if ((file->i_ino.i_dev = getdev(str, cp - str)) == -1)
500 			goto bad;
501 		if ((file->i_unit = getunit(cp)) == -1)
502 			goto bad;
503 		while (isdigit(*cp))
504 			cp++;
505 		file->i_boff = 0;
506 		if (*cp >= 'a' && *cp <= 'h')
507 			file->i_boff = *cp++ - 'a';
508 		if (*cp++ != ':') {
509 			errno = EOFFSET;
510 			goto badspec;
511 		}
512 	}
513 	opendev = file->i_ino.i_dev << B_TYPESHIFT;
514 	opendev |= ((file->i_unit % 8) << B_UNITSHIFT);
515 	opendev |= ((file->i_unit / 8) << B_ADAPTORSHIFT);
516 	opendev |= file->i_boff << B_PARTITIONSHIFT;
517 	opendev |= B_DEVMAGIC;
518 	if (errno = devopen(file))
519 		goto bad;
520 	if (cp != str && *cp == '\0') {
521 		file->i_flgs |= how+1;
522 		file->i_cc = 0;
523 		file->i_offset = 0;
524 		return (fdesc+3);
525 	}
526 	file->i_ma = (char *)(&file->i_fs);
527 	file->i_cc = SBSIZE;
528 	file->i_bn = SBOFF / DEV_BSIZE + file->i_boff;
529 	file->i_offset = 0;
530 	if (devread(file) < 0) {
531 		errno = file->i_error;
532 		printf("super block read error\n");
533 		goto bad;
534 	}
535 	if ((i = find(cp, file)) == 0) {
536 		errno = ESRCH;
537 		goto bad;
538 	}
539 #ifndef	SMALL
540 	if (how != 0) {
541 		printf("Can't write files yet.. Sorry\n");
542 		errno = EIO;
543 		goto bad;
544 	}
545 #endif SMALL
546 	if (openi(i, file) < 0) {
547 		errno = file->i_error;
548 		goto bad;
549 	}
550 	file->i_offset = 0;
551 	file->i_cc = 0;
552 	file->i_flgs |= F_FILE | (how+1);
553 	return (fdesc+3);
554 
555 badspec:
556 	printf("malformed device specification\n");
557 bad:
558 	file->i_flgs = 0;
559 	return (-1);
560 }
561 
562 static
563 getdev(str, len)
564 	char *str;
565 	int len;
566 {
567 	register struct devsw *dp;
568 
569 	for (dp = devsw; dp->dv_name; dp++) {
570 		if (!strncmp(str, dp->dv_name, len))
571 			return (dp - devsw);
572 	}
573 	printf("Unknown device\n");
574 	errno = ENXIO;
575 	return (-1);
576 }
577 
578 static
579 getunit(cp)
580 	register char *cp;
581 {
582 	register int i = 0;
583 
584 	while (*cp >= '0' && *cp <= '9')
585 		i = i * 10 + *cp++ - '0';
586 	if ((unsigned) i > 255) {
587 		printf("minor device number out of range (0-255)\n");
588 		errno = EUNIT;
589 		i = -1;
590 	}
591 	return (i);
592 }
593 
594 close(fdesc)
595 	int fdesc;
596 {
597 	struct iob *file;
598 
599 	fdesc -= 3;
600 	if (fdesc < 0 || fdesc >= NFILES ||
601 	    ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) {
602 		errno = EBADF;
603 		return (-1);
604 	}
605 	if ((file->i_flgs&F_FILE) == 0)
606 		devclose(file);
607 	file->i_flgs = 0;
608 	return (0);
609 }
610 
611 #ifndef	SMALL
612 ioctl(fdesc, cmd, arg)
613 	int fdesc, cmd;
614 	char *arg;
615 {
616 	register struct iob *file;
617 	int error = 0;
618 
619 	fdesc -= 3;
620 	if (fdesc < 0 || fdesc >= NFILES ||
621 	    ((file = &iob[fdesc])->i_flgs&F_ALLOC) == 0) {
622 		errno = EBADF;
623 		return (-1);
624 	}
625 	switch (cmd) {
626 
627 	case SAIOHDR:
628 		file->i_flgs |= F_HDR;
629 		break;
630 
631 	case SAIOCHECK:
632 		file->i_flgs |= F_CHECK;
633 		break;
634 
635 	case SAIOHCHECK:
636 		file->i_flgs |= F_HCHECK;
637 		break;
638 
639 	case SAIONOBAD:
640 		file->i_flgs |= F_NBSF;
641 		break;
642 
643 	case SAIODOBAD:
644 		file->i_flgs &= ~F_NBSF;
645 		break;
646 
647 	default:
648 		error = devioctl(file, cmd, arg);
649 		break;
650 	}
651 	if (error < 0)
652 		errno = file->i_error;
653 	return (error);
654 }
655 #endif SMALL
656 
657 exit()
658 {
659 	_stop("Exit called");
660 }
661 
662 _stop(s)
663 	char *s;
664 {
665 	int i;
666 
667 	for (i = 0; i < NFILES; i++)
668 		if (iob[i].i_flgs != 0)
669 			close(i);
670 	printf("%s\n", s);
671 	_rtt();
672 }
673