2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afsconfig.h>
11 #include <afs/param.h>
16 #include <sys/types.h>
19 #if defined(AFS_AIX_ENV)
21 #include <sys/statfs.h>
24 #include <sys/ioccom.h>
26 #if defined(AFS_DUX40_ENV) || defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
27 #include <sys/ioctl.h>
30 #endif /* AFS_AIX_ENV */
35 #include <afs/debug.h>
39 typedef off64_t osi_lloff_t;
40 #define osi_llseek lseek64
41 #else /* O_LARGEFILE */
42 #ifdef AFS_HAVE_LLSEEK
43 typedef offset_t osi_lloff_t;
44 #define osi_llseek llseek
45 #else /* AFS_HAVE_LLSEEK */
46 typedef off_t osi_lloff_t;
47 #define osi_llseek lseek
48 #endif /* AFS_HAVE_LLSEEK */
49 #endif /* O_LARGEFILE */
52 * This macro should be used inside assertions wherever offset/hyper
53 * conversion occur. A good compiler in a 64 bit environment will
54 * elide the entire statement if the offset type is 64 bits wide.
56 #define osi_hFitsInOff(ahyper, noff) \
57 ((sizeof(noff) == 4) ? hfitsin32(ahyper) : 1)
59 #define osi_h2off(ahyper, noff) \
60 ((sizeof(noff) == 4) \
61 ? ((noff) = (osi_lloff_t)hgetlo(ahyper))\
62 : ((noff) = ((osi_lloff_t)hgethi(ahyper)<<32) | (osi_lloff_t)hgetlo(ahyper)))
64 #define osi_off2h(noff, ahyper) \
65 ((sizeof(noff) == 4) \
66 ? (hset32(ahyper, (int)noff)) \
67 : (hset64(ahyper, (int)((noff>>32)&0xffffffff), ((int)noff&0xffffffff))))
70 /************ End of osi wrappers ***********************************/
72 /* Implementation of user space device I/O for regular POSIX files. */
75 usd_FileRead(usd_handle_t usd, char *buf, afs_uint32 nbytes,
78 int fd = (int)(usd->handle);
81 got = read(fd, buf, nbytes);
93 usd_FileWrite(usd_handle_t usd, char *buf, afs_uint32 nbytes,
96 int fd = (int)(usd->handle);
99 sent = write(fd, buf, nbytes);
110 extern osi_lloff_t osi_llseek(int, osi_lloff_t, int);
113 usd_FileSeek(usd_handle_t usd, afs_hyper_t reqOff, int whence,
114 afs_hyper_t * curOffP)
116 int fd = (int)(usd->handle);
119 if (!osi_hFitsInOff(reqOff, lloff))
122 osi_h2off(reqOff, lloff);
123 lloff = osi_llseek(fd, lloff, whence);
124 if (lloff == (((osi_lloff_t) 0) - 1))
127 osi_off2h(lloff, *curOffP);
133 usd_FileIoctl(usd_handle_t usd, int req, void *arg)
135 int fd = (int)(usd->handle);
138 #else /* O_LARGEFILE */
140 #endif /* O_LARGEFILE */
142 struct statfs fsinfo; /* AIX stat structure doesn't have st_blksize */
143 #endif /* AFS_AIX_ENV */
149 case USD_IOCTL_GETBLKSIZE:
151 code = fstatfs(fd, &fsinfo);
153 *((long *)arg) = (long)4096;
157 #endif /* AFS_AIX_ENV */
158 case USD_IOCTL_GETTYPE:
159 case USD_IOCTL_GETDEV:
160 case USD_IOCTL_GETSIZE:
162 code = fstat64(fd, &info);
163 #else /* O_LARGEFILE */
164 code = fstat(fd, &info);
165 #endif /* O_LARGEFILE */
172 case USD_IOCTL_GETTYPE:
173 *(int *)arg = info.st_mode & S_IFMT;
175 case USD_IOCTL_GETDEV:
176 if (!(S_ISCHR(info.st_mode) || S_ISBLK(info.st_mode)))
177 return ENODEV; /* not a device */
178 *(dev_t *) arg = info.st_rdev;
180 case USD_IOCTL_GETSIZE:
181 if (S_ISCHR(info.st_mode) || S_ISBLK(info.st_mode))
182 return ENOTTY; /* shouldn't be a device */
183 osi_off2h(info.st_size, *(afs_hyper_t *) arg);
185 case USD_IOCTL_GETFULLNAME:
186 *(char **)arg = usd->fullPathName;
189 case USD_IOCTL_SETSIZE:
191 /* We could just use ftruncate in all cases. (This even works on AIX;
192 * I tried it). -blake 931118 */
194 /* However, I'm pretty sure this doesn't work on Ultrix so I am
195 * unsure about OSF/1 and HP/UX. 931118 */
197 size = *(afs_hyper_t *) arg;
198 if (!osi_hFitsInOff(size, off))
200 osi_h2off(size, off);
202 code = ftruncate64(fd, off);
203 #else /* O_LARGEFILE */
204 code = ftruncate(fd, off);
205 #endif /* O_LARGEFILE */
210 case USD_IOCTL_TAPEOPERATION:
212 usd_tapeop_t *tapeOpp = (usd_tapeop_t *) arg;
213 #if defined(AFS_AIX_ENV)
214 struct stop os_tapeop;
216 if (tapeOpp->tp_op == USDTAPE_WEOF) {
217 os_tapeop.st_op = STWEOF;
218 } else if (tapeOpp->tp_op == USDTAPE_REW) {
219 os_tapeop.st_op = STREW;
220 } else if (tapeOpp->tp_op == USDTAPE_FSF) {
221 os_tapeop.st_op = STFSF;
222 } else if (tapeOpp->tp_op == USDTAPE_BSF) {
223 os_tapeop.st_op = STRSF;
224 } else if (tapeOpp->tp_op == USDTAPE_PREPARE) {
226 } else if (tapeOpp->tp_op == USDTAPE_SHUTDOWN) {
229 /* unsupported tape operation */
232 os_tapeop.st_count = tapeOpp->tp_count;
234 code = ioctl(fd, STIOCTOP, &os_tapeop);
236 struct mtop os_tapeop;
238 if (tapeOpp->tp_op == USDTAPE_WEOF) {
239 os_tapeop.mt_op = MTWEOF;
240 } else if (tapeOpp->tp_op == USDTAPE_REW) {
241 os_tapeop.mt_op = MTREW;
242 } else if (tapeOpp->tp_op == USDTAPE_FSF) {
243 os_tapeop.mt_op = MTFSF;
244 } else if (tapeOpp->tp_op == USDTAPE_BSF) {
245 os_tapeop.mt_op = MTBSF;
246 } else if (tapeOpp->tp_op == USDTAPE_PREPARE) {
248 } else if (tapeOpp->tp_op == USDTAPE_SHUTDOWN) {
251 /* unsupported tape operation */
254 os_tapeop.mt_count = tapeOpp->tp_count;
256 code = ioctl(fd, MTIOCTOP, &os_tapeop);
257 #endif /* AFS_AIX_ENV */
267 case USD_IOCTL_GETBLKSIZE:
268 if (S_ISCHR(info.st_mode) || S_ISBLK(info.st_mode)) {
269 *((long *)arg) = (long)4096;
273 *((long *)arg) = (long)fsinfo.f_bsize;
274 #else /* AFS_AIX_ENV */
275 *((long *)arg) = (long)info.st_blksize;
276 #endif /* AFS_AIX_ENV */
286 usd_FileClose(usd_handle_t usd)
288 int fd = (int)(usd->handle);
292 /* I can't really believe this is necessary. On the one hand the user
293 * space code always uses character devices, which aren't supposed to do
294 * any buffering. On the other, I very much doubt fsyncing a regular file
295 * being salvaged is ever necessary. But the salvager used to do this
296 * before returning, so... */
298 if (usd->openFlags & (O_WRONLY | O_RDWR)) {
300 code = usd_FileIoctl(usd, USD_IOCTL_GETTYPE, &mode);
301 if (code == 0 && S_ISBLK(mode)) {
311 if (usd->fullPathName)
312 free(usd->fullPathName);
319 usd_FileOpen(const char *path, int flags, int mode, usd_handle_t * usdP)
329 oflags = (flags & USD_OPEN_RDWR) ? O_RDWR : O_RDONLY;
331 #ifdef O_SYNC /* AFS_DARWIN_ENV XXX */
332 if (flags & USD_OPEN_SYNC)
336 if (flags & USD_OPEN_CREATE)
340 fd = open64(path, oflags | O_LARGEFILE, mode);
341 #else /* O_LARGEFILE */
342 fd = open(path, oflags, mode);
343 #endif /* O_LARGEFILE */
347 usd = (usd_handle_t) malloc(sizeof(*usd));
348 memset(usd, 0, sizeof(*usd));
349 usd->handle = (void *)fd;
350 usd->read = usd_FileRead;
351 usd->write = usd_FileWrite;
352 usd->seek = usd_FileSeek;
353 usd->ioctl = usd_FileIoctl;
354 usd->close = usd_FileClose;
355 usd->fullPathName = (char *)malloc(strlen(path) + 1);
356 strcpy(usd->fullPathName, path);
357 usd->openFlags = flags;
360 if (flags & (USD_OPEN_RLOCK | USD_OPEN_WLOCK)) {
363 #else /* O_LARGEFILE */
365 #endif /* O_LARGEFILE */
367 /* make sure both lock bits aren't set */
368 assert(~flags & (USD_OPEN_RLOCK | USD_OPEN_WLOCK));
370 fl.l_type = ((flags & USD_OPEN_RLOCK) ? F_RDLCK : F_WRLCK);
371 fl.l_whence = SEEK_SET;
372 fl.l_start = (osi_lloff_t) 0;
373 fl.l_len = (osi_lloff_t) 0; /* whole file */
375 code = fcntl(fd, F_SETLK64, &fl);
376 #else /* O_LARGEFILE */
377 code = fcntl(fd, F_SETLK, &fl);
378 #endif /* O_LARGEFILE */
382 /* If we're trying to obtain a write lock on a real disk, then the
383 * aggregate must not be attached by the kernel. If so, unlock it
385 * WARNING: The code to check for the above has been removed when this
386 * file was ported from DFS src. It should be put back if
387 * this library is used to access hard disks
391 if (code == 0 && usdP)
399 usd_FileDummyClose(usd_handle_t usd)
406 usd_Open(const char *path, int oflag, int mode, usd_handle_t * usdP)
408 return usd_FileOpen(path, oflag, mode, usdP);
412 usd_FileStandardInput(usd_handle_t * usdP)
419 usd = (usd_handle_t) malloc(sizeof(*usd));
420 memset(usd, 0, sizeof(*usd));
421 usd->handle = (void *)((unsigned long)0);
422 usd->read = usd_FileRead;
423 usd->write = usd_FileWrite;
424 usd->seek = usd_FileSeek;
425 usd->ioctl = usd_FileIoctl;
426 usd->close = usd_FileDummyClose;
427 usd->fullPathName = "STDIN";
435 usd_StandardInput(usd_handle_t * usdP)
437 return usd_FileStandardInput(usdP);
441 usd_FileStandardOutput(usd_handle_t * usdP)
448 usd = (usd_handle_t) malloc(sizeof(*usd));
449 memset(usd, 0, sizeof(*usd));
450 usd->handle = (void *)((unsigned long)1);
451 usd->read = usd_FileRead;
452 usd->write = usd_FileWrite;
453 usd->seek = usd_FileSeek;
454 usd->ioctl = usd_FileIoctl;
455 usd->close = usd_FileDummyClose;
456 usd->fullPathName = "STDOUT";
464 usd_StandardOutput(usd_handle_t * usdP)
466 return usd_FileStandardOutput(usdP);