source: trunk/minix/servers/fs/utility.c@ 21

Last change on this file since 21 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 5.4 KB
Line 
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 <minix/endpoint.h>
16#include <unistd.h>
17#include "buf.h"
18#include "file.h"
19#include "fproc.h"
20#include "inode.h"
21#include "param.h"
22
23PRIVATE int panicking; /* inhibits recursive panics during sync */
24
25/*===========================================================================*
26 * clock_time *
27 *===========================================================================*/
28PUBLIC time_t clock_time()
29{
30/* This routine returns the time in seconds since 1.1.1970. MINIX is an
31 * astrophysically naive system that assumes the earth rotates at a constant
32 * rate and that such things as leap seconds do not exist.
33 */
34
35 register int k;
36 clock_t uptime;
37
38 if ( (k=getuptime(&uptime)) != OK) panic(__FILE__,"clock_time err", k);
39 return( (time_t) (boottime + (uptime/HZ)));
40}
41
42/*===========================================================================*
43 * fetch_name *
44 *===========================================================================*/
45PUBLIC int fetch_name(path, len, flag)
46char *path; /* pointer to the path in user space */
47int len; /* path length, including 0 byte */
48int flag; /* M3 means path may be in message */
49{
50/* Go get path and put it in 'user_path'.
51 * If 'flag' = M3 and 'len' <= M3_STRING, the path is present in 'message'.
52 * If it is not, go copy it from user space.
53 */
54 register char *rpu, *rpm;
55 int r;
56
57 /* Check name length for validity. */
58 if (len <= 0) {
59 err_code = EINVAL;
60 return(EGENERIC);
61 }
62
63 if (len > PATH_MAX) {
64 err_code = ENAMETOOLONG;
65 return(EGENERIC);
66 }
67
68 if (flag == M3 && len <= M3_STRING) {
69 /* Just copy the path from the message to 'user_path'. */
70 rpu = &user_path[0];
71 rpm = m_in.pathname; /* contained in input message */
72 do { *rpu++ = *rpm++; } while (--len);
73 r = OK;
74 } else {
75 /* String is not contained in the message. Get it from user space. */
76 r = sys_datacopy(who_e, (vir_bytes) path,
77 FS_PROC_NR, (vir_bytes) user_path, (phys_bytes) len);
78 }
79 return(r);
80}
81
82/*===========================================================================*
83 * no_sys *
84 *===========================================================================*/
85PUBLIC int no_sys()
86{
87/* Somebody has used an illegal system call number */
88 return(EINVAL);
89}
90
91/*===========================================================================*
92 * panic *
93 *===========================================================================*/
94PUBLIC void panic(who, mess, num)
95char *who; /* who caused the panic */
96char *mess; /* panic message string */
97int num; /* number to go with it */
98{
99/* Something awful has happened. Panics are caused when an internal
100 * inconsistency is detected, e.g., a programming error or illegal value of a
101 * defined constant.
102 */
103 if (panicking) return; /* do not panic during a sync */
104 panicking = TRUE; /* prevent another panic during the sync */
105
106 printf("FS panic (%s): %s ", who, mess);
107 if (num != NO_NUM) printf("%d",num);
108 (void) do_sync(); /* flush everything to the disk */
109 sys_exit(SELF);
110}
111
112/*===========================================================================*
113 * conv2 *
114 *===========================================================================*/
115PUBLIC unsigned conv2(norm, w)
116int norm; /* TRUE if no swap, FALSE for byte swap */
117int w; /* promotion of 16-bit word to be swapped */
118{
119/* Possibly swap a 16-bit word between 8086 and 68000 byte order. */
120 if (norm) return( (unsigned) w & 0xFFFF);
121 return( ((w&BYTE) << 8) | ( (w>>8) & BYTE));
122}
123
124/*===========================================================================*
125 * conv4 *
126 *===========================================================================*/
127PUBLIC long conv4(norm, x)
128int norm; /* TRUE if no swap, FALSE for byte swap */
129long x; /* 32-bit long to be byte swapped */
130{
131/* Possibly swap a 32-bit long between 8086 and 68000 byte order. */
132 unsigned lo, hi;
133 long l;
134
135 if (norm) return(x); /* byte order was already ok */
136 lo = conv2(FALSE, (int) x & 0xFFFF); /* low-order half, byte swapped */
137 hi = conv2(FALSE, (int) (x>>16) & 0xFFFF); /* high-order half, swapped */
138 l = ( (long) lo <<16) | hi;
139 return(l);
140}
141
142/*===========================================================================*
143 * isokendpt_f *
144 *===========================================================================*/
145PUBLIC int isokendpt_f(char *file, int line, int endpoint, int *proc, int fatal)
146{
147 int failed = 0;
148 *proc = _ENDPOINT_P(endpoint);
149 if(*proc < 0 || *proc >= NR_PROCS) {
150 printf("FS:%s:%d: proc (%d) from endpoint (%d) out of range\n",
151 file, line, *proc, endpoint);
152 failed = 1;
153 } else if(fproc[*proc].fp_endpoint != endpoint) {
154 printf("FS:%s:%d: proc (%d) from endpoint (%d) doesn't match "
155 "known endpoint (%d)\n",
156 file, line, *proc, endpoint, fproc[*proc].fp_endpoint);
157 failed = 1;
158 }
159
160 if(failed && fatal)
161 panic(__FILE__, "isokendpt_f failed", NO_NUM);
162
163 return failed ? EDEADSRCDST : OK;
164}
165
Note: See TracBrowser for help on using the repository browser.