source: branches/minix3-book/servers/fs/utility.c@ 23

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

Importazione sorgenti libro

File size: 4.6 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 <unistd.h>
16#include "buf.h"
17#include "file.h"
18#include "fproc.h"
19#include "inode.h"
20#include "param.h"
21
22PRIVATE int panicking; /* inhibits recursive panics during sync */
23
24/*===========================================================================*
25 * clock_time *
26 *===========================================================================*/
27PUBLIC 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 *===========================================================================*/
44PUBLIC int fetch_name(path, len, flag)
45char *path; /* pointer to the path in user space */
46int len; /* path length, including 0 byte */
47int 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 *===========================================================================*/
84PUBLIC int no_sys()
85{
86/* Somebody has used an illegal system call number */
87 return(EINVAL);
88}
89
90/*===========================================================================*
91 * panic *
92 *===========================================================================*/
93PUBLIC void panic(who, mess, num)
94char *who; /* who caused the panic */
95char *mess; /* panic message string */
96int 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 *===========================================================================*/
114PUBLIC unsigned conv2(norm, w)
115int norm; /* TRUE if no swap, FALSE for byte swap */
116int 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 *===========================================================================*/
126PUBLIC long conv4(norm, x)
127int norm; /* TRUE if no swap, FALSE for byte swap */
128long 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
Note: See TracBrowser for help on using the repository browser.