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>
17 #include <sys/types.h>
20 #if defined(AFS_AIX_ENV)
22 #include <sys/statfs.h>
25 #include <sys/ioccom.h>
28 #endif /* AFS_AIX_ENV */
29 #if defined(AFS_DUX40_ENV) || defined(AFS_OBSD_ENV) || defined(AFS_NBSD_ENV)
30 #include <sys/ioctl.h>
32 #include <afs/debug.h>
36 typedef off64_t osi_lloff_t;
37 #define osi_llseek lseek64
38 #else /* O_LARGEFILE */
39 #ifdef AFS_HAVE_LLSEEK
40 typedef offset_t osi_lloff_t;
41 #define osi_llseek llseek
42 #else /* AFS_HAVE_LLSEEK */
43 typedef off_t osi_lloff_t;
44 #define osi_llseek lseek
45 #endif /* AFS_HAVE_LLSEEK */
46 #endif /* O_LARGEFILE */
49 * This macro should be used inside assertions wherever offset/hyper
50 * conversion occur. A good compiler in a 64 bit environment will
51 * elide the entire statement if the offset type is 64 bits wide.
53 #define osi_hFitsInOff(ahyper, noff) \
54 ((sizeof(noff) == 4) ? hfitsin32(ahyper) : 1)
56 #define osi_h2off(ahyper, noff) \
57 ((sizeof(noff) == 4) \
58 ? ((noff) = (osi_lloff_t)hgetlo(ahyper))\
59 : ((noff) = ((osi_lloff_t)hgethi(ahyper)<<32) | (osi_lloff_t)hgetlo(ahyper)))
61 #define osi_off2h(noff, ahyper) \
62 ((sizeof(noff) == 4) \
63 ? (hset32(ahyper, (int)noff)) \
64 : (hset64(ahyper, (int)((noff>>32)&0xffffffff), ((int)noff&0xffffffff))))
67 /************ End of osi wrappers ***********************************/
69 /* Implementation of user space device I/O for regular POSIX files. */
71 static int usd_FileRead(
77 int fd = (int)(usd->handle);
80 got = read(fd, buf, nbytes);
91 static int usd_FileWrite(
97 int fd = (int)(usd->handle);
100 sent = write(fd, buf, nbytes);
111 extern osi_lloff_t osi_llseek(int, osi_lloff_t, int);
113 static int usd_FileSeek(
117 afs_hyper_t *curOffP)
119 int fd = (int)(usd->handle);
122 if (!osi_hFitsInOff(reqOff, lloff))
125 osi_h2off(reqOff, lloff);
126 lloff = osi_llseek(fd, lloff, whence);
127 if (lloff == (((osi_lloff_t)0) - 1))
130 osi_off2h(lloff, *curOffP);
135 static int usd_FileIoctl(usd_handle_t usd, int req, void *arg)
137 int fd = (int)(usd->handle);
140 #else /* O_LARGEFILE */
142 #endif /* O_LARGEFILE */
144 struct statfs fsinfo; /* AIX stat structure doesn't have st_blksize */
145 #endif /* AFS_AIX_ENV */
151 case USD_IOCTL_GETBLKSIZE:
153 code = fstatfs(fd, &fsinfo);
155 *((long *)arg) = (long)4096;
159 #endif /* AFS_AIX_ENV */
160 case USD_IOCTL_GETTYPE:
161 case USD_IOCTL_GETDEV:
162 case USD_IOCTL_GETSIZE:
164 code = fstat64(fd, &info);
165 #else /* O_LARGEFILE */
166 code = fstat(fd, &info);
167 #endif /* O_LARGEFILE */
174 case USD_IOCTL_GETTYPE:
175 *(int *)arg = info.st_mode & S_IFMT;
177 case USD_IOCTL_GETDEV:
178 if (!(S_ISCHR(info.st_mode) || S_ISBLK(info.st_mode)))
179 return ENODEV; /* not a device */
180 *(dev_t *)arg = info.st_rdev;
182 case USD_IOCTL_GETSIZE:
183 if (S_ISCHR(info.st_mode) || S_ISBLK(info.st_mode))
184 return ENOTTY; /* shouldn't be a device */
185 osi_off2h(info.st_size, *(afs_hyper_t *)arg);
187 case USD_IOCTL_GETFULLNAME:
188 *(char **)arg = usd->fullPathName;
191 case USD_IOCTL_SETSIZE:
193 /* We could just use ftruncate in all cases. (This even works on AIX;
194 * I tried it). -blake 931118 */
196 /* However, I'm pretty sure this doesn't work on Ultrix so I am
197 * unsure about OSF/1 and HP/UX. 931118 */
199 size = *(afs_hyper_t *)arg;
200 if (!osi_hFitsInOff(size, off))
202 osi_h2off(size, off);
204 code = ftruncate64 (fd, off);
205 #else /* O_LARGEFILE */
206 code = ftruncate (fd, off);
207 #endif /* O_LARGEFILE */
212 case USD_IOCTL_TAPEOPERATION:
214 usd_tapeop_t *tapeOpp = (usd_tapeop_t *)arg;
215 #if defined(AFS_AIX_ENV)
216 struct stop os_tapeop;
218 if (tapeOpp->tp_op == USDTAPE_WEOF) {
219 os_tapeop.st_op = STWEOF;
220 } else if (tapeOpp->tp_op == USDTAPE_REW) {
221 os_tapeop.st_op = STREW;
222 } else if (tapeOpp->tp_op == USDTAPE_FSF) {
223 os_tapeop.st_op = STFSF;
224 } else if (tapeOpp->tp_op == USDTAPE_BSF) {
225 os_tapeop.st_op = STRSF;
226 } else if (tapeOpp->tp_op == USDTAPE_PREPARE) {
228 } else if (tapeOpp->tp_op == USDTAPE_SHUTDOWN) {
231 /* unsupported tape operation */
234 os_tapeop.st_count = tapeOpp->tp_count;
236 code = ioctl(fd, STIOCTOP, &os_tapeop);
238 struct mtop os_tapeop;
240 if (tapeOpp->tp_op == USDTAPE_WEOF) {
241 os_tapeop.mt_op = MTWEOF;
242 } else if (tapeOpp->tp_op == USDTAPE_REW) {
243 os_tapeop.mt_op = MTREW;
244 } else if (tapeOpp->tp_op == USDTAPE_FSF) {
245 os_tapeop.mt_op = MTFSF;
246 } else if (tapeOpp->tp_op == USDTAPE_BSF) {
247 os_tapeop.mt_op = MTBSF;
248 } else if (tapeOpp->tp_op == USDTAPE_PREPARE) {
250 } else if (tapeOpp->tp_op == USDTAPE_SHUTDOWN) {
253 /* unsupported tape operation */
256 os_tapeop.mt_count = tapeOpp->tp_count;
258 code = ioctl(fd, MTIOCTOP, &os_tapeop);
259 #endif /* AFS_AIX_ENV */
269 case USD_IOCTL_GETBLKSIZE:
270 if (S_ISCHR(info.st_mode) || S_ISBLK(info.st_mode)) {
271 *((long *)arg) = (long)4096;
275 *((long *)arg) = (long)fsinfo.f_bsize;
276 #else /* AFS_AIX_ENV */
277 *((long *)arg) = (long)info.st_blksize;
278 #endif /* AFS_AIX_ENV */
287 static int usd_FileClose(usd_handle_t usd)
289 int fd = (int)(usd->handle);
293 /* I can't really believe this is necessary. On the one hand the user
294 * space code always uses character devices, which aren't supposed to do
295 * any buffering. On the other, I very much doubt fsyncing a regular file
296 * being salvaged is ever necessary. But the salvager used to do this
297 * before returning, so... */
299 if (usd->openFlags & (O_WRONLY|O_RDWR)) {
301 code = usd_FileIoctl(usd, USD_IOCTL_GETTYPE, &mode);
305 /* on AIX3.1 can't fsync raw disk device */
320 if (usd->fullPathName)
321 free(usd->fullPathName);
327 static int usd_FileOpen(
341 oflags = (flags & USD_OPEN_RDWR) ? O_RDWR : O_RDONLY;
343 #ifdef O_SYNC /* AFS_DARWIN_ENV XXX */
344 if (flags & USD_OPEN_SYNC)
348 if (flags & USD_OPEN_CREATE)
352 fd = open64(path, oflags | O_LARGEFILE, mode);
353 #else /* O_LARGEFILE */
354 fd = open(path, oflags, mode);
355 #endif /* O_LARGEFILE */
359 usd = (usd_handle_t) malloc(sizeof(*usd));
360 memset(usd, 0, sizeof(*usd));
361 usd->handle = (void *)fd;
362 usd->read = usd_FileRead;
363 usd->write = usd_FileWrite;
364 usd->seek = usd_FileSeek;
365 usd->ioctl = usd_FileIoctl;
366 usd->close = usd_FileClose;
367 usd->fullPathName = (char *)malloc(strlen(path)+1);
368 strcpy(usd->fullPathName, path);
369 usd->openFlags = flags;
372 if (flags & (USD_OPEN_RLOCK|USD_OPEN_WLOCK)) {
375 #else /* O_LARGEFILE */
377 #endif /* O_LARGEFILE */
379 /* make sure both lock bits aren't set */
380 assert(~flags & (USD_OPEN_RLOCK|USD_OPEN_WLOCK));
382 fl.l_type = ((flags & USD_OPEN_RLOCK) ? F_RDLCK : F_WRLCK);
383 fl.l_whence = SEEK_SET;
384 fl.l_start = (osi_lloff_t)0;
385 fl.l_len = (osi_lloff_t)0; /* whole file */
387 code = fcntl(fd, F_SETLK64, &fl);
388 #else /* O_LARGEFILE */
389 code = fcntl(fd, F_SETLK, &fl);
390 #endif /* O_LARGEFILE */
394 /* If we're trying to obtain a write lock on a real disk, then the
395 * aggregate must not be attached by the kernel. If so, unlock it
397 * WARNING: The code to check for the above has been removed when this
398 * file was ported from DFS src. It should be put back if
399 * this library is used to access hard disks
403 if (code == 0 && usdP)
410 static int usd_FileDummyClose(usd_handle_t usd)
416 int usd_Open(const char *path, int oflag, int mode, usd_handle_t *usdP)
418 return usd_FileOpen(path, oflag, mode, usdP);
421 static int usd_FileStandardInput(
429 usd = (usd_handle_t) malloc(sizeof(*usd));
430 memset(usd, 0, sizeof(*usd));
431 usd->handle = (void *)((unsigned long)0);
432 usd->read = usd_FileRead;
433 usd->write = usd_FileWrite;
434 usd->seek = usd_FileSeek;
435 usd->ioctl = usd_FileIoctl;
436 usd->close = usd_FileDummyClose;
437 usd->fullPathName = "STDIN";
444 int usd_StandardInput(usd_handle_t *usdP)
446 return usd_FileStandardInput(usdP);
449 static int usd_FileStandardOutput(
457 usd = (usd_handle_t) malloc(sizeof(*usd));
458 memset(usd, 0, sizeof(*usd));
459 usd->handle = (void *)((unsigned long)1);
460 usd->read = usd_FileRead;
461 usd->write = usd_FileWrite;
462 usd->seek = usd_FileSeek;
463 usd->ioctl = usd_FileIoctl;
464 usd->close = usd_FileDummyClose;
465 usd->fullPathName = "STDOUT";
472 int usd_StandardOutput(usd_handle_t *usdP)
474 return usd_FileStandardOutput(usdP);