[4] | 1 | /* This file contains a few general purpose utility routines.
|
---|
| 2 | *
|
---|
| 3 | * The entry points into this file are
|
---|
| 4 | * clock_time: ask the clock task for the real time
|
---|
| 5 | * copy: copy a block of data
|
---|
| 6 | * fetch_name: go get a path name from user space
|
---|
| 7 | * no_sys: reject a system call that FS does not handle
|
---|
| 8 | * panic: something awful has occurred; MINIX cannot continue
|
---|
| 9 | * conv2: do byte swapping on a 16-bit int
|
---|
| 10 | * conv4: do byte swapping on a 32-bit long
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | #include "fs.h"
|
---|
| 14 | #include <minix/com.h>
|
---|
| 15 | #include <unistd.h>
|
---|
| 16 | #include "buf.h"
|
---|
| 17 | #include "file.h"
|
---|
| 18 | #include "fproc.h"
|
---|
| 19 | #include "inode.h"
|
---|
| 20 | #include "param.h"
|
---|
| 21 |
|
---|
| 22 | PRIVATE int panicking; /* inhibits recursive panics during sync */
|
---|
| 23 |
|
---|
| 24 | /*===========================================================================*
|
---|
| 25 | * clock_time *
|
---|
| 26 | *===========================================================================*/
|
---|
| 27 | PUBLIC time_t clock_time()
|
---|
| 28 | {
|
---|
| 29 | /* This routine returns the time in seconds since 1.1.1970. MINIX is an
|
---|
| 30 | * astrophysically naive system that assumes the earth rotates at a constant
|
---|
| 31 | * rate and that such things as leap seconds do not exist.
|
---|
| 32 | */
|
---|
| 33 |
|
---|
| 34 | register int k;
|
---|
| 35 | clock_t uptime;
|
---|
| 36 |
|
---|
| 37 | if ( (k=getuptime(&uptime)) != OK) panic(__FILE__,"clock_time err", k);
|
---|
| 38 | return( (time_t) (boottime + (uptime/HZ)));
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /*===========================================================================*
|
---|
| 42 | * fetch_name *
|
---|
| 43 | *===========================================================================*/
|
---|
| 44 | PUBLIC int fetch_name(path, len, flag)
|
---|
| 45 | char *path; /* pointer to the path in user space */
|
---|
| 46 | int len; /* path length, including 0 byte */
|
---|
| 47 | int flag; /* M3 means path may be in message */
|
---|
| 48 | {
|
---|
| 49 | /* Go get path and put it in 'user_path'.
|
---|
| 50 | * If 'flag' = M3 and 'len' <= M3_STRING, the path is present in 'message'.
|
---|
| 51 | * If it is not, go copy it from user space.
|
---|
| 52 | */
|
---|
| 53 | register char *rpu, *rpm;
|
---|
| 54 | int r;
|
---|
| 55 |
|
---|
| 56 | /* Check name length for validity. */
|
---|
| 57 | if (len <= 0) {
|
---|
| 58 | err_code = EINVAL;
|
---|
| 59 | return(EGENERIC);
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | if (len > PATH_MAX) {
|
---|
| 63 | err_code = ENAMETOOLONG;
|
---|
| 64 | return(EGENERIC);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | if (flag == M3 && len <= M3_STRING) {
|
---|
| 68 | /* Just copy the path from the message to 'user_path'. */
|
---|
| 69 | rpu = &user_path[0];
|
---|
| 70 | rpm = m_in.pathname; /* contained in input message */
|
---|
| 71 | do { *rpu++ = *rpm++; } while (--len);
|
---|
| 72 | r = OK;
|
---|
| 73 | } else {
|
---|
| 74 | /* String is not contained in the message. Get it from user space. */
|
---|
| 75 | r = sys_datacopy(who, (vir_bytes) path,
|
---|
| 76 | FS_PROC_NR, (vir_bytes) user_path, (phys_bytes) len);
|
---|
| 77 | }
|
---|
| 78 | return(r);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /*===========================================================================*
|
---|
| 82 | * no_sys *
|
---|
| 83 | *===========================================================================*/
|
---|
| 84 | PUBLIC int no_sys()
|
---|
| 85 | {
|
---|
| 86 | /* Somebody has used an illegal system call number */
|
---|
| 87 | return(EINVAL);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /*===========================================================================*
|
---|
| 91 | * panic *
|
---|
| 92 | *===========================================================================*/
|
---|
| 93 | PUBLIC void panic(who, mess, num)
|
---|
| 94 | char *who; /* who caused the panic */
|
---|
| 95 | char *mess; /* panic message string */
|
---|
| 96 | int num; /* number to go with it */
|
---|
| 97 | {
|
---|
| 98 | /* Something awful has happened. Panics are caused when an internal
|
---|
| 99 | * inconsistency is detected, e.g., a programming error or illegal value of a
|
---|
| 100 | * defined constant.
|
---|
| 101 | */
|
---|
| 102 | if (panicking) return; /* do not panic during a sync */
|
---|
| 103 | panicking = TRUE; /* prevent another panic during the sync */
|
---|
| 104 |
|
---|
| 105 | printf("FS panic (%s): %s ", who, mess);
|
---|
| 106 | if (num != NO_NUM) printf("%d",num);
|
---|
| 107 | (void) do_sync(); /* flush everything to the disk */
|
---|
| 108 | sys_exit(1);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /*===========================================================================*
|
---|
| 112 | * conv2 *
|
---|
| 113 | *===========================================================================*/
|
---|
| 114 | PUBLIC unsigned conv2(norm, w)
|
---|
| 115 | int norm; /* TRUE if no swap, FALSE for byte swap */
|
---|
| 116 | int w; /* promotion of 16-bit word to be swapped */
|
---|
| 117 | {
|
---|
| 118 | /* Possibly swap a 16-bit word between 8086 and 68000 byte order. */
|
---|
| 119 | if (norm) return( (unsigned) w & 0xFFFF);
|
---|
| 120 | return( ((w&BYTE) << 8) | ( (w>>8) & BYTE));
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /*===========================================================================*
|
---|
| 124 | * conv4 *
|
---|
| 125 | *===========================================================================*/
|
---|
| 126 | PUBLIC long conv4(norm, x)
|
---|
| 127 | int norm; /* TRUE if no swap, FALSE for byte swap */
|
---|
| 128 | long x; /* 32-bit long to be byte swapped */
|
---|
| 129 | {
|
---|
| 130 | /* Possibly swap a 32-bit long between 8086 and 68000 byte order. */
|
---|
| 131 | unsigned lo, hi;
|
---|
| 132 | long l;
|
---|
| 133 |
|
---|
| 134 | if (norm) return(x); /* byte order was already ok */
|
---|
| 135 | lo = conv2(FALSE, (int) x & 0xFFFF); /* low-order half, byte swapped */
|
---|
| 136 | hi = conv2(FALSE, (int) (x>>16) & 0xFFFF); /* high-order half, swapped */
|
---|
| 137 | l = ( (long) lo <<16) | hi;
|
---|
| 138 | return(l);
|
---|
| 139 | }
|
---|
| 140 |
|
---|