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) || (defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN100_ENV))
27 #include <sys/ioctl.h>
29 #ifndef AFS_DARWIN100_ENV
32 #endif /* AFS_AIX_ENV */
41 #include <afs/debug.h>
45 typedef off64_t osi_lloff_t;
46 #define osi_llseek lseek64
47 #else /* O_LARGEFILE */
48 #ifdef AFS_HAVE_LLSEEK
49 typedef offset_t osi_lloff_t;
50 #define osi_llseek llseek
51 #else /* AFS_HAVE_LLSEEK */
52 typedef off_t osi_lloff_t;
53 #define osi_llseek lseek
54 #endif /* AFS_HAVE_LLSEEK */
55 #endif /* O_LARGEFILE */
58 * This macro should be used inside assertions wherever offset/hyper
59 * conversion occur. A good compiler in a 64 bit environment will
60 * elide the entire statement if the offset type is 64 bits wide.
62 #define osi_hFitsInOff(ahyper, noff) \
63 ((sizeof(noff) == 4) ? hfitsin32(ahyper) : 1)
65 #define osi_h2off(ahyper, noff) \
66 ((sizeof(noff) == 4) \
67 ? ((noff) = (osi_lloff_t)hgetlo(ahyper))\
68 : ((noff) = ((osi_lloff_t)hgethi(ahyper)<<32) | (osi_lloff_t)hgetlo(ahyper)))
70 #define osi_off2h(noff, ahyper) \
71 ((sizeof(noff) == 4) \
72 ? (hset32(ahyper, (int)noff)) \
73 : (hset64(ahyper, (int)((noff>>32)&0xffffffff), ((int)noff&0xffffffff))))
76 /************ End of osi wrappers ***********************************/
78 /* Implementation of user space device I/O for regular POSIX files. */
81 usd_FileRead(usd_handle_t usd, char *buf, afs_uint32 nbytes,
84 int fd = (intptr_t)(usd->handle);
87 got = read(fd, buf, nbytes);
99 usd_FileWrite(usd_handle_t usd, char *buf, afs_uint32 nbytes,
102 int fd = (intptr_t)(usd->handle);
105 sent = write(fd, buf, nbytes);
116 extern osi_lloff_t osi_llseek(int, osi_lloff_t, int);
119 usd_FileSeek(usd_handle_t usd, afs_hyper_t reqOff, int whence,
120 afs_hyper_t * curOffP)
122 int fd = (intptr_t)(usd->handle);
125 if (!osi_hFitsInOff(reqOff, lloff))
128 osi_h2off(reqOff, lloff);
129 lloff = osi_llseek(fd, lloff, whence);
130 if (lloff == (((osi_lloff_t) 0) - 1))
133 osi_off2h(lloff, *curOffP);
139 usd_FileIoctl(usd_handle_t usd, int req, void *arg)
141 int fd = (intptr_t)(usd->handle);
144 #else /* O_LARGEFILE */
146 #endif /* O_LARGEFILE */
148 struct statfs fsinfo; /* AIX stat structure doesn't have st_blksize */
149 #endif /* AFS_AIX_ENV */
155 case USD_IOCTL_GETBLKSIZE:
157 code = fstatfs(fd, &fsinfo);
159 *((long *)arg) = (long)4096;
163 #endif /* AFS_AIX_ENV */
164 case USD_IOCTL_GETTYPE:
165 case USD_IOCTL_GETDEV:
166 case USD_IOCTL_GETSIZE:
168 code = fstat64(fd, &info);
169 #else /* O_LARGEFILE */
170 code = fstat(fd, &info);
171 #endif /* O_LARGEFILE */
178 case USD_IOCTL_GETTYPE:
179 *(int *)arg = info.st_mode & S_IFMT;
181 case USD_IOCTL_GETDEV:
182 if (!(S_ISCHR(info.st_mode) || S_ISBLK(info.st_mode)))
183 return ENODEV; /* not a device */
184 *(dev_t *) arg = info.st_rdev;
186 case USD_IOCTL_GETSIZE:
187 if (S_ISCHR(info.st_mode) || S_ISBLK(info.st_mode))
188 return ENOTTY; /* shouldn't be a device */
189 osi_off2h(info.st_size, *(afs_hyper_t *) arg);
191 case USD_IOCTL_GETFULLNAME:
192 *(char **)arg = usd->fullPathName;
195 case USD_IOCTL_SETSIZE:
197 /* We could just use ftruncate in all cases. (This even works on AIX;
198 * I tried it). -blake 931118 */
200 /* However, I'm pretty sure this doesn't work on Ultrix so I am
201 * unsure about OSF/1 and HP/UX. 931118 */
203 size = *(afs_hyper_t *) arg;
204 if (!osi_hFitsInOff(size, off))
206 osi_h2off(size, off);
208 code = ftruncate64(fd, off);
209 #else /* O_LARGEFILE */
210 code = ftruncate(fd, off);
211 #endif /* O_LARGEFILE */
216 case USD_IOCTL_TAPEOPERATION:
218 #ifdef AFS_DARWIN100_ENV
221 usd_tapeop_t *tapeOpp = (usd_tapeop_t *) arg;
222 #if defined(AFS_AIX_ENV)
223 struct stop os_tapeop;
225 if (tapeOpp->tp_op == USDTAPE_WEOF) {
226 os_tapeop.st_op = STWEOF;
227 } else if (tapeOpp->tp_op == USDTAPE_REW) {
228 os_tapeop.st_op = STREW;
229 } else if (tapeOpp->tp_op == USDTAPE_FSF) {
230 os_tapeop.st_op = STFSF;
231 } else if (tapeOpp->tp_op == USDTAPE_BSF) {
232 os_tapeop.st_op = STRSF;
233 } else if (tapeOpp->tp_op == USDTAPE_PREPARE) {
235 } else if (tapeOpp->tp_op == USDTAPE_SHUTDOWN) {
238 /* unsupported tape operation */
241 os_tapeop.st_count = tapeOpp->tp_count;
243 code = ioctl(fd, STIOCTOP, &os_tapeop);
245 struct mtop os_tapeop;
247 if (tapeOpp->tp_op == USDTAPE_WEOF) {
248 os_tapeop.mt_op = MTWEOF;
249 } else if (tapeOpp->tp_op == USDTAPE_REW) {
250 os_tapeop.mt_op = MTREW;
251 } else if (tapeOpp->tp_op == USDTAPE_FSF) {
252 os_tapeop.mt_op = MTFSF;
253 } else if (tapeOpp->tp_op == USDTAPE_BSF) {
254 os_tapeop.mt_op = MTBSF;
255 } else if (tapeOpp->tp_op == USDTAPE_PREPARE) {
257 } else if (tapeOpp->tp_op == USDTAPE_SHUTDOWN) {
260 /* unsupported tape operation */
263 os_tapeop.mt_count = tapeOpp->tp_count;
265 code = ioctl(fd, MTIOCTOP, &os_tapeop);
266 #endif /* AFS_AIX_ENV */
276 case USD_IOCTL_GETBLKSIZE:
277 if (S_ISCHR(info.st_mode) || S_ISBLK(info.st_mode)) {
278 *((long *)arg) = (long)4096;
282 *((long *)arg) = (long)fsinfo.f_bsize;
283 #else /* AFS_AIX_ENV */
284 *((long *)arg) = (long)info.st_blksize;
285 #endif /* AFS_AIX_ENV */
295 usd_FileClose(usd_handle_t usd)
297 int fd = (intptr_t)(usd->handle);
301 /* I can't really believe this is necessary. On the one hand the user
302 * space code always uses character devices, which aren't supposed to do
303 * any buffering. On the other, I very much doubt fsyncing a regular file
304 * being salvaged is ever necessary. But the salvager used to do this
305 * before returning, so... */
307 if (usd->openFlags & (O_WRONLY | O_RDWR)) {
309 code = usd_FileIoctl(usd, USD_IOCTL_GETTYPE, &mode);
310 if (code == 0 && S_ISBLK(mode)) {
320 if (usd->fullPathName)
321 free(usd->fullPathName);
328 usd_FileOpen(const char *path, int flags, int mode, usd_handle_t * usdP)
338 oflags = (flags & USD_OPEN_RDWR) ? O_RDWR : O_RDONLY;
340 #ifdef O_SYNC /* AFS_DARWIN_ENV XXX */
341 if (flags & USD_OPEN_SYNC)
345 if (flags & USD_OPEN_CREATE)
349 fd = open64(path, oflags | O_LARGEFILE, mode);
350 #else /* O_LARGEFILE */
351 fd = open(path, oflags, mode);
352 #endif /* O_LARGEFILE */
356 usd = (usd_handle_t) malloc(sizeof(*usd));
357 memset(usd, 0, sizeof(*usd));
358 usd->handle = (void *)(intptr_t)fd;
359 usd->read = usd_FileRead;
360 usd->write = usd_FileWrite;
361 usd->seek = usd_FileSeek;
362 usd->ioctl = usd_FileIoctl;
363 usd->close = usd_FileClose;
364 usd->fullPathName = (char *)malloc(strlen(path) + 1);
365 strcpy(usd->fullPathName, path);
366 usd->openFlags = flags;
369 if (flags & (USD_OPEN_RLOCK | USD_OPEN_WLOCK)) {
372 #else /* O_LARGEFILE */
374 #endif /* O_LARGEFILE */
376 /* make sure both lock bits aren't set */
377 assert(~flags & (USD_OPEN_RLOCK | USD_OPEN_WLOCK));
379 fl.l_type = ((flags & USD_OPEN_RLOCK) ? F_RDLCK : F_WRLCK);
380 fl.l_whence = SEEK_SET;
381 fl.l_start = (osi_lloff_t) 0;
382 fl.l_len = (osi_lloff_t) 0; /* whole file */
384 code = fcntl(fd, F_SETLK64, &fl);
385 #else /* O_LARGEFILE */
386 code = fcntl(fd, F_SETLK, &fl);
387 #endif /* O_LARGEFILE */
391 /* If we're trying to obtain a write lock on a real disk, then the
392 * aggregate must not be attached by the kernel. If so, unlock it
394 * WARNING: The code to check for the above has been removed when this
395 * file was ported from DFS src. It should be put back if
396 * this library is used to access hard disks
400 if (code == 0 && usdP)
408 usd_FileDummyClose(usd_handle_t usd)
415 usd_Open(const char *path, int oflag, int mode, usd_handle_t * usdP)
417 return usd_FileOpen(path, oflag, mode, usdP);
421 usd_FileStandardInput(usd_handle_t * usdP)
428 usd = (usd_handle_t) malloc(sizeof(*usd));
429 memset(usd, 0, sizeof(*usd));
430 usd->handle = (void *)((unsigned long)0);
431 usd->read = usd_FileRead;
432 usd->write = usd_FileWrite;
433 usd->seek = usd_FileSeek;
434 usd->ioctl = usd_FileIoctl;
435 usd->close = usd_FileDummyClose;
436 usd->fullPathName = "STDIN";
444 usd_StandardInput(usd_handle_t * usdP)
446 return usd_FileStandardInput(usdP);
450 usd_FileStandardOutput(usd_handle_t * usdP)
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";
473 usd_StandardOutput(usd_handle_t * usdP)
475 return usd_FileStandardOutput(usdP);