more-warnings-and-prototyping-work-20030619
[openafs.git] / src / volser / physio.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #include <sys/types.h>
16 #ifdef AFS_NT40_ENV
17 #include <fcntl.h>
18 #else
19 #include <sys/file.h>
20 #include <netinet/in.h>
21 #include <unistd.h>
22 #endif
23 #ifdef HAVE_STRING_H
24 #include <string.h>
25 #else
26 #ifdef HAVE_STRINGS_H
27 #include <strings.h>
28 #endif
29 #endif
30 #ifdef  AFS_SUN5_ENV
31 #include <sys/fcntl.h>
32 #endif
33 #include <errno.h>
34 #include <rx/xdr.h>
35 #include <rx/rx.h>
36 #include <stdio.h>
37 #include <afs/afsint.h>
38 #include <afs/nfs.h>
39 #include <afs/assert.h>
40 #include <afs/dir.h>
41 #include <afs/ihandle.h>
42 #include "vol.h"
43
44 /* returns 0 on success, errno on failure */
45 int ReallyRead(DirHandle *file, int block, char *data)
46 {
47     FdHandle_t *fdP;
48     int code;
49     errno = 0;
50     fdP = IH_OPEN(file->dirh_handle);
51     if (fdP == NULL) {
52         code = errno;
53         return code;
54     }
55     if (FDH_SEEK(fdP, block*AFS_PAGESIZE, SEEK_SET) < 0) {
56         code = errno;
57         FDH_REALLYCLOSE(fdP);
58         return code;
59     }
60     code = FDH_READ(fdP, data, AFS_PAGESIZE);
61     if (code != AFS_PAGESIZE) {
62         if (code < 0)
63             code = errno;
64         else 
65             code = EIO;
66         FDH_REALLYCLOSE(fdP);
67         return code;
68     }
69     FDH_CLOSE(fdP);
70     return 0;
71 }
72
73 /* returns 0 on success, errno on failure */
74 int ReallyWrite (DirHandle *file, int block, char *data)
75 {
76     FdHandle_t *fdP;
77     extern int VolumeChanged;
78     int code;
79
80     errno = 0;
81
82     fdP = IH_OPEN(file->dirh_handle);
83     if (fdP == NULL) {
84         code = errno;
85         return code;
86     }
87     if (FDH_SEEK(fdP, block*AFS_PAGESIZE, SEEK_SET) < 0) {
88         code = errno;
89         FDH_REALLYCLOSE(fdP);
90         return code;
91     }
92     code = FDH_WRITE(fdP, data, AFS_PAGESIZE);
93     if (code != AFS_PAGESIZE) {
94         if (code < 0)
95             code = errno;
96         else 
97             code = EIO;
98         FDH_REALLYCLOSE(fdP);
99         return code;
100     }
101     FDH_CLOSE(fdP);
102     VolumeChanged = 1;
103     return 0;
104 }
105
106 /* SetSalvageDirHandle:
107  * Create a handle to a directory entry and reference it (IH_INIT).
108  * The handle needs to be dereferenced with the FidZap() routine.
109  */
110 void SetSalvageDirHandle(DirHandle *dir, afs_int32 volume, Inode inode, afs_int32 device)
111 {
112     private SalvageCacheCheck = 1;
113     memset(dir, 0, sizeof(DirHandle));
114
115     dir->dirh_volume = volume;
116     dir->dirh_device = device;
117     dir->dirh_inode = inode;
118     IH_INIT(dir->dirh_handle, device, volume, inode);
119
120     /* Always re-read for a new dirhandle */
121     dir->dirh_cacheCheck = SalvageCacheCheck++; 
122 }
123
124 void FidZap(DirHandle *file)
125 {
126     IH_RELEASE(file->dirh_handle);
127     memset(file, 0, sizeof(DirHandle));
128 }
129
130 void FidZero(DirHandle *file)
131 {
132     memset(file, 0, sizeof(DirHandle));
133 }
134
135 int FidEq(DirHandle *afile, DirHandle *bfile)
136 {
137     if (afile->dirh_volume != bfile->dirh_volume) return 0;
138     if (afile->dirh_device != bfile->dirh_device) return 0;
139     if (afile->dirh_cacheCheck != bfile->dirh_cacheCheck) return 0;
140     if (afile->dirh_inode != bfile->dirh_inode) return 0;
141     return 1;
142 }
143
144 int FidVolEq(DirHandle *afile, afs_int32 vid)
145 {
146     if (afile->dirh_volume != vid) return 0;
147     return 1;
148 }
149
150 void FidCpy(DirHandle *tofile, DirHandle *fromfile)
151 {
152     *tofile = *fromfile;
153     IH_COPY(tofile->dirh_handle, fromfile->dirh_handle);
154 }
155
156 void Die (char *msg)
157 {
158     printf("%s\n",msg);
159     assert(1==2);
160 }