3 * Copyright 2000, International Business Machines Corporation and others.
6 * This software has been released under the terms of the IBM Public
7 * License. For details, see the LICENSE file in the top-level source
8 * directory or online at http://www.openafs.org/dl/license10.html
12 * User space client specific interface glue
15 #include <afsconfig.h>
16 #include "afs/param.h"
20 #include "afs/sysincludes.h" /* Standard vendor system headers */
23 #include "afsincludes.h" /* Afs-based standard headers */
24 #include "afs_usrops.h"
25 #include "afs/afs_stats.h"
27 #include "afs/cellconfig.h"
29 #include "afs/kauth.h"
30 #include "afs/kautils.h"
31 #include "afs/afsutil.h"
32 #include "afs/afs_bypasscache.h"
33 #include "rx/rx_globals.h"
34 #include "afsd/afsd.h"
40 #ifndef AFS_CACHE_VNODE_PATH
41 #error You must compile UKERNEL code with -DAFS_CACHE_VNODE_PATH
44 #define CACHEINFOFILE "cacheinfo"
45 #define AFSLOGFILE "AFSLog"
46 #define DCACHEFILE "CacheItems"
47 #define VOLINFOFILE "VolumeItems"
48 #define CELLINFOFILE "CellItems"
52 #define MIN(A,B) ((A)<(B)?(A):(B))
55 #define MAX(A,B) ((A)>(B)?(A):(B))
58 extern int cacheDiskType;
60 char afs_LclCellName[64];
62 static struct usr_vnode *afs_FileTable[MAX_OSI_FILES];
63 static int afs_FileFlags[MAX_OSI_FILES];
64 static off_t afs_FileOffsets[MAX_OSI_FILES];
66 #define MAX_CACHE_LOOPS 4
68 static struct usr_vfs afs_RootVfs;
69 static struct usr_vnode *afs_RootVnode = NULL;
70 static struct usr_vnode *afs_CurrentDir = NULL;
72 static char afs_mountDir[1024]; /* AFS mount point */
73 static int afs_mountDirLen; /* strlen of AFS mount point */
75 struct afsconf_dir *afs_cdir; /* config dir */
77 int afs_bufferpages = 100;
79 static usr_key_t afs_global_u_key;
81 static struct usr_proc *afs_global_procp = NULL;
82 static struct usr_ucred *afs_global_ucredp = NULL;
84 struct usr_ucred afs_osi_cred, *afs_osi_credp;
85 usr_mutex_t afs_global_lock;
86 usr_thread_t afs_global_owner;
87 usr_mutex_t rx_global_lock;
88 usr_thread_t rx_global_owner;
90 static usr_mutex_t osi_dummy_lock;
91 static usr_mutex_t osi_waitq_lock;
92 static usr_mutex_t osi_authenticate_lock;
94 afs_lock_t osi_flplock;
95 afs_lock_t osi_fsplock;
97 #ifndef NETSCAPE_NSAPI
100 * Mutex and condition variable used to implement sleep
102 pthread_mutex_t usr_sleep_mutex;
103 pthread_cond_t usr_sleep_cond;
105 #endif /* !NETSCAPE_NSAPI */
107 int call_syscall(long, long, long, long, long, long);
108 int fork_syscall(long, long, long, long, long, long);
112 * Hash table mapping addresses onto wait structures for
113 * osi_Sleep/osi_Wakeup and osi_Wait/osi_Wakeup
115 typedef struct osi_wait {
119 struct osi_wait *next;
120 struct osi_wait *prev;
122 struct osi_wait *timedNext;
123 struct osi_wait *timedPrev;
127 * Head of the linked list of available waitq structures.
129 static osi_wait_t *osi_waithash_avail;
132 * List of timed waits, NSAPI does not provide a cond_timed
133 * wait, so we need to keep track of the timed waits ourselves and
134 * periodically check for expirations
136 static osi_wait_t *osi_timedwait_head;
137 static osi_wait_t *osi_timedwait_tail;
142 } osi_waithash_table[OSI_WAITHASH_SIZE];
145 * Never call afs_brelse
148 ufs_brelse(struct usr_vnode *vp, struct usr_buf *bp)
155 * I am not sure what to do with these, they assert for now
158 iodone(struct usr_buf *bp)
172 * Every user is a super user
175 afs_osi_suser(void *credp)
181 afs_suser(void *credp)
187 * These are no-ops in user space
191 afs_osi_SetTime(osi_timeval_t * atv)
197 * xflock should never fall through, the only files we know
198 * about are AFS files
208 * ioctl should never fall through, the only files we know
209 * about are AFS files
219 * We do not support the inode related system calls
222 afs_syscall_icreate(long a, long b, long c, long d, long e, long f)
229 afs_syscall_iincdec(int dev, int inode, int inode_p1, int amount)
236 afs_syscall_iopen(int dev, int inode, int usrmod)
243 afs_syscall_ireadwrite(void)
250 * these routines are referenced in the vfsops structure, but
251 * should never get called
282 * uiomove copies data between kernel buffers and uio buffers
285 usr_uiomove(char *kbuf, int n, int rw, struct usr_uio *uio)
292 nio = uio->uio_iovcnt;
302 while (nio > 0 && n > 0) {
303 len = MIN(n, iovp->iov_len);
304 if (rw == UIO_READ) {
305 memcpy(iovp->iov_base, ptr, len);
307 memcpy(ptr, iovp->iov_base, len);
311 uio->uio_resid -= len;
312 uio->uio_offset += len;
313 iovp->iov_base = (char *)(iovp->iov_base) + len;
314 iovp->iov_len -= len;
325 * routines to manage user credentials
328 usr_crcopy(struct usr_ucred *credp)
330 struct usr_ucred *newcredp;
332 newcredp = afs_osi_Alloc(sizeof(struct usr_ucred));
334 newcredp->cr_ref = 1;
341 struct usr_ucred *newcredp;
343 newcredp = afs_osi_Alloc(sizeof(struct usr_ucred));
344 newcredp->cr_ref = 1;
349 usr_crfree(struct usr_ucred *credp)
352 if (credp->cr_ref == 0) {
353 afs_osi_Free((char *)credp, sizeof(struct usr_ucred));
359 usr_crhold(struct usr_ucred *credp)
366 usr_vattr_null(struct usr_vattr *vap)
371 n = sizeof(struct usr_vattr);
379 * Initialize the thread specific data used to simulate the
380 * kernel environment for each thread. The user structure
381 * is stored in the thread specific data.
384 uafs_InitThread(void)
387 struct usr_user *uptr;
390 * initialize the thread specific user structure. Use malloc to
391 * allocate the data block, so pthread_finish can free the buffer
392 * when this thread terminates.
394 uptr = malloc(sizeof(struct usr_user) + sizeof(struct usr_ucred));
395 usr_assert(uptr != NULL);
398 uptr->u_procp = afs_global_procp;
399 uptr->u_cred = (struct usr_ucred *)(uptr + 1);
400 *uptr->u_cred = *afs_global_ucredp;
401 st = usr_setspecific(afs_global_u_key, (void *)uptr);
406 * routine to get the user structure from the thread specific data.
407 * this routine is used to implement the global 'u' structure. Initializes
408 * the thread if needed.
411 get_user_struct(void)
413 struct usr_user *uptr;
416 st = usr_getspecific(afs_global_u_key, &uptr);
420 st = usr_getspecific(afs_global_u_key, &uptr);
422 usr_assert(uptr != NULL);
428 * Hash an address for the waithash table
430 #define WAITHASH(X) \
431 (((long)(X)^((long)(X)>>4)^((long)(X)<<4))&(OSI_WAITHASH_SIZE-1))
437 afs_osi_Sleep(void *x)
441 int glockOwner = ISAFS_GLOCK();
443 usr_mutex_lock(&osi_waitq_lock);
448 if (osi_waithash_avail == NULL) {
449 waitp = afs_osi_Alloc(sizeof(osi_wait_t));
450 usr_cond_init(&waitp->cond);
452 waitp = osi_waithash_avail;
453 osi_waithash_avail = osi_waithash_avail->next;
457 DLL_INSERT_TAIL(waitp, osi_waithash_table[index].head,
458 osi_waithash_table[index].tail, next, prev);
459 waitp->expiration = 0;
460 waitp->timedNext = NULL;
461 waitp->timedPrev = NULL;
462 while (waitp->flag == 0) {
463 usr_cond_wait(&waitp->cond, &osi_waitq_lock);
465 DLL_DELETE(waitp, osi_waithash_table[index].head,
466 osi_waithash_table[index].tail, next, prev);
467 waitp->next = osi_waithash_avail;
468 osi_waithash_avail = waitp;
469 usr_mutex_unlock(&osi_waitq_lock);
476 afs_osi_SleepSig(void *x)
483 afs_osi_Wakeup(void *x)
489 usr_mutex_lock(&osi_waitq_lock);
490 waitp = osi_waithash_table[index].head;
492 if (waitp->addr == x && waitp->flag == 0) {
494 usr_cond_signal(&waitp->cond);
498 usr_mutex_unlock(&osi_waitq_lock);
503 afs_osi_TimedSleep(void *event, afs_int32 ams, int aintok)
505 return afs_osi_Wait(ams, event, aintok);
509 afs_osi_Wait(afs_int32 msec, struct afs_osi_WaitHandle *handle, int intok)
515 int glockOwner = ISAFS_GLOCK();
517 tv.tv_sec = msec / 1000;
518 tv.tv_nsec = (msec % 1000) * 1000000;
519 if (handle == NULL) {
523 usr_thread_sleep(&tv);
529 usr_mutex_lock(&osi_waitq_lock);
533 index = WAITHASH((caddr_t) handle);
534 if (osi_waithash_avail == NULL) {
535 waitp = afs_osi_Alloc(sizeof(osi_wait_t));
536 usr_cond_init(&waitp->cond);
538 waitp = osi_waithash_avail;
539 osi_waithash_avail = osi_waithash_avail->next;
541 waitp->addr = (caddr_t) handle;
543 DLL_INSERT_TAIL(waitp, osi_waithash_table[index].head,
544 osi_waithash_table[index].tail, next, prev);
545 tv.tv_sec += time(NULL);
546 waitp->expiration = tv.tv_sec + ((tv.tv_nsec == 0) ? 0 : 1);
547 DLL_INSERT_TAIL(waitp, osi_timedwait_head, osi_timedwait_tail,
548 timedNext, timedPrev);
549 usr_cond_wait(&waitp->cond, &osi_waitq_lock);
555 DLL_DELETE(waitp, osi_waithash_table[index].head,
556 osi_waithash_table[index].tail, next, prev);
557 DLL_DELETE(waitp, osi_timedwait_head, osi_timedwait_tail, timedNext,
559 waitp->next = osi_waithash_avail;
560 osi_waithash_avail = waitp;
561 usr_mutex_unlock(&osi_waitq_lock);
570 afs_osi_CancelWait(struct afs_osi_WaitHandle *handle)
572 afs_osi_Wakeup(handle);
576 * Netscape NSAPI doesn't have a cond_timed_wait, so we need
577 * to explicitly signal cond_timed_waits when their timers expire
580 afs_osi_CheckTimedWaits(void)
585 curTime = time(NULL);
586 usr_mutex_lock(&osi_waitq_lock);
587 waitp = osi_timedwait_head;
588 while (waitp != NULL) {
589 usr_assert(waitp->expiration != 0);
590 if (waitp->expiration <= curTime) {
592 usr_cond_signal(&waitp->cond);
594 waitp = waitp->timedNext;
596 usr_mutex_unlock(&osi_waitq_lock);
601 * 'dummy' vnode, for non-AFS files. We don't actually need most vnode
602 * information for non-AFS files, so point all of them towards this vnode
605 static struct usr_vnode dummy_vnode = {
616 * Allocate a slot in the file table if there is not one there already,
617 * copy in the file name and kludge up the vnode and inode structures
620 lookupname(char *fnamep, int segflg, int followlink,
621 struct usr_vnode **compvpp)
626 * Assume relative pathnames refer to files in AFS
628 if (*fnamep != '/' || uafs_afsPathName(fnamep) != NULL) {
630 code = uafs_LookupName(fnamep, afs_CurrentDir, compvpp, 0, 0);
635 /* For non-afs files, nobody really looks at the meaningful values in the
636 * returned vnode, so we can return a 'fake' one. The vnode can be held,
637 * released, etc. and some callers check for a NULL vnode anyway, so we
638 * to return something. */
640 usr_mutex_lock(&osi_dummy_lock);
641 VN_HOLD(&dummy_vnode);
642 usr_mutex_unlock(&osi_dummy_lock);
644 *compvpp = &dummy_vnode;
650 * open a file given its i-node number
653 osi_UFSOpen(afs_dcache_id_t *ino)
662 fp = afs_osi_Alloc(sizeof(struct osi_file));
663 usr_assert(fp != NULL);
665 usr_assert(ino->ufs);
667 fp->fd = open(ino->ufs, O_RDWR | O_CREAT, 0);
669 get_user_struct()->u_error = errno;
670 afs_osi_Free((char *)fp, sizeof(struct osi_file));
674 rc = fstat(fp->fd, &st);
676 get_user_struct()->u_error = errno;
677 afs_osi_Free((void *)fp, sizeof(struct osi_file));
681 fp->size = st.st_size;
683 fp->vnode = (struct usr_vnode *)fp;
690 osi_UFSClose(struct osi_file *fp)
699 get_user_struct()->u_error = errno;
700 afs_osi_Free((void *)fp, sizeof(struct osi_file));
704 afs_osi_Free((void *)fp, sizeof(struct osi_file));
710 osi_UFSTruncate(struct osi_file *fp, afs_int32 len)
717 rc = ftruncate(fp->fd, len);
719 get_user_struct()->u_error = errno;
729 afs_osi_Read(struct osi_file *fp, int offset, void *buf, afs_int32 len)
738 rc = lseek(fp->fd, offset, SEEK_SET);
740 rc = lseek(fp->fd, fp->offset, SEEK_SET);
743 get_user_struct()->u_error = errno;
748 ret = read(fp->fd, buf, len);
750 get_user_struct()->u_error = errno;
755 rc = fstat(fp->fd, &st);
757 get_user_struct()->u_error = errno;
761 fp->size = st.st_size;
767 afs_osi_Write(struct osi_file *fp, afs_int32 offset, void *buf, afs_int32 len)
776 rc = lseek(fp->fd, offset, SEEK_SET);
778 rc = lseek(fp->fd, fp->offset, SEEK_SET);
781 get_user_struct()->u_error = errno;
786 ret = write(fp->fd, buf, len);
788 get_user_struct()->u_error = errno;
793 rc = fstat(fp->fd, &st);
795 get_user_struct()->u_error = errno;
799 fp->size = st.st_size;
805 afs_osi_Stat(struct osi_file *fp, struct osi_stat *stp)
811 rc = fstat(fp->fd, &st);
813 get_user_struct()->u_error = errno;
817 stp->size = st.st_size;
818 stp->mtime = st.st_mtime;
819 stp->atime = st.st_atime;
828 afs_osi_VOP_RDWR(struct usr_vnode *vnodeP, struct usr_uio *uioP, int rw,
829 int flags, struct usr_ucred *credP)
832 struct osi_file *fp = (struct osi_file *)vnodeP;
835 * We don't support readv/writev.
837 usr_assert(uioP->uio_iovcnt == 1);
838 usr_assert(uioP->uio_resid == uioP->uio_iov[0].iov_len);
840 if (rw == UIO_WRITE) {
841 usr_assert(uioP->uio_fmode == FWRITE);
842 rc = afs_osi_Write(fp, uioP->uio_offset, uioP->uio_iov[0].iov_base,
843 uioP->uio_iov[0].iov_len);
845 usr_assert(uioP->uio_fmode == FREAD);
846 rc = afs_osi_Read(fp, uioP->uio_offset, uioP->uio_iov[0].iov_base,
847 uioP->uio_iov[0].iov_len);
850 return get_user_struct()->u_error;
853 uioP->uio_resid -= rc;
854 uioP->uio_offset += rc;
855 uioP->uio_iov[0].iov_base = (char *)(uioP->uio_iov[0].iov_base) + rc;
856 uioP->uio_iov[0].iov_len -= rc;
861 afs_osi_Alloc(size_t size)
867 afs_osi_Free(void *ptr, size_t size)
873 afs_osi_FreeStr(char *ptr)
879 osi_AllocLargeSpace(size_t size)
881 AFS_STATCNT(osi_AllocLargeSpace);
882 return afs_osi_Alloc(size);
886 osi_FreeLargeSpace(void *ptr)
888 AFS_STATCNT(osi_FreeLargeSpace);
889 afs_osi_Free(ptr, 0);
893 osi_AllocSmallSpace(size_t size)
895 AFS_STATCNT(osi_AllocSmallSpace);
896 return afs_osi_Alloc(size);
900 osi_FreeSmallSpace(void *ptr)
902 AFS_STATCNT(osi_FreeSmallSpace);
903 afs_osi_Free(ptr, 0);
909 AFS_STATCNT(shutdown_osi);
914 shutdown_osinet(void)
916 AFS_STATCNT(shutdown_osinet);
921 shutdown_osifile(void)
923 AFS_STATCNT(shutdown_osifile);
928 afs_nfsclient_init(void)
933 shutdown_nfsclnt(void)
939 afs_osi_Invisible(void)
945 afs_osi_Visible(void)
951 osi_GetTime(struct timeval *tv)
953 gettimeofday(tv, NULL);
958 osi_SetTime(struct timeval *tv)
964 osi_Active(struct vcache *avc)
966 AFS_STATCNT(osi_Active);
973 afs_osi_MapStrategy(int (*aproc) (struct usr_buf *), struct usr_buf *bp)
975 afs_int32 returnCode;
976 returnCode = (*aproc) (bp);
981 osi_FlushPages(struct vcache *avc, afs_ucred_t *credp)
983 ObtainSharedLock(&avc->lock, 555);
984 if ((hcmp((avc->f.m.DataVersion), (avc->mapDV)) <= 0)
985 || ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
986 ReleaseSharedLock(&avc->lock);
989 UpgradeSToWLock(&avc->lock, 565);
990 hset(avc->mapDV, avc->f.m.DataVersion);
991 ReleaseWriteLock(&avc->lock);
996 osi_FlushText_really(struct vcache *vp)
998 if (hcmp(vp->f.m.DataVersion, vp->flushDV) > 0) {
999 hset(vp->flushDV, vp->f.m.DataVersion);
1005 osi_SyncVM(struct vcache *avc)
1011 osi_ReleaseVM(struct vcache *avc, int len, struct usr_ucred *credp)
1023 * Use the thread specific data to implement the user structure
1025 usr_keycreate(&afs_global_u_key, free);
1028 * Initialize the global ucred structure
1030 afs_global_ucredp = (struct usr_ucred *)
1031 afs_osi_Alloc(sizeof(struct usr_ucred));
1032 usr_assert(afs_global_ucredp != NULL);
1033 afs_global_ucredp->cr_ref = 1;
1034 afs_set_cr_uid(afs_global_ucredp, geteuid());
1035 afs_set_cr_gid(afs_global_ucredp, getegid());
1036 afs_set_cr_ruid(afs_global_ucredp, getuid());
1037 afs_set_cr_rgid(afs_global_ucredp, getgid());
1038 afs_global_ucredp->cr_suid = afs_cr_ruid(afs_global_ucredp);
1039 afs_global_ucredp->cr_sgid = afs_cr_rgid(afs_global_ucredp);
1040 st = getgroups(NGROUPS, &afs_global_ucredp->cr_groups[0]);
1041 usr_assert(st >= 0);
1042 afs_global_ucredp->cr_ngroups = (unsigned long)st;
1043 for (i = st; i < NGROUPS; i++) {
1044 afs_global_ucredp->cr_groups[i] = NOGROUP;
1048 * Initialize the global process structure
1050 afs_global_procp = (struct usr_proc *)
1051 afs_osi_Alloc(sizeof(struct usr_proc));
1052 usr_assert(afs_global_procp != NULL);
1053 afs_global_procp->p_pid = osi_getpid();
1054 afs_global_procp->p_ppid = (pid_t) 1;
1055 afs_global_procp->p_ucred = afs_global_ucredp;
1057 #ifndef NETSCAPE_NSAPI
1059 * Initialize the mutex and condition variable used to implement
1062 pthread_mutex_init(&usr_sleep_mutex, NULL);
1063 pthread_cond_init(&usr_sleep_cond, NULL);
1064 #endif /* !NETSCAPE_NSAPI */
1067 * Initialize the hash table used for sleep/wakeup
1069 for (i = 0; i < OSI_WAITHASH_SIZE; i++) {
1070 DLL_INIT_LIST(osi_waithash_table[i].head, osi_waithash_table[i].tail);
1072 DLL_INIT_LIST(osi_timedwait_head, osi_timedwait_tail);
1073 osi_waithash_avail = NULL;
1076 * Initialize the AFS file table
1078 for (i = 0; i < MAX_OSI_FILES; i++) {
1079 afs_FileTable[i] = NULL;
1083 * Initialize the global locks
1085 usr_mutex_init(&afs_global_lock);
1086 usr_mutex_init(&rx_global_lock);
1087 usr_mutex_init(&osi_dummy_lock);
1088 usr_mutex_init(&osi_waitq_lock);
1089 usr_mutex_init(&osi_authenticate_lock);
1092 * Initialize the AFS OSI credentials
1094 afs_osi_cred = *afs_global_ucredp;
1095 afs_osi_credp = &afs_osi_cred;
1097 init_et_to_sys_error();
1101 * Set the UDP port number RX uses for UDP datagrams
1104 uafs_SetRxPort(int port)
1106 usr_assert(usr_rx_port == 0);
1111 * uafs_Init is for backwards compatibility only! Do not use it; use
1112 * uafs_Setup, uafs_ParseArgs, and uafs_Run instead.
1115 uafs_Init(char *rn, char *mountDirParam, char *confDirParam,
1116 char *cacheBaseDirParam, int cacheBlocksParam, int cacheFilesParam,
1117 int cacheStatEntriesParam, int dCacheSizeParam, int vCacheSizeParam,
1118 int chunkSizeParam, int closeSynchParam, int debugParam,
1119 int nDaemonsParam, int cacheFlagsParam, char *logFile)
1129 code = uafs_Setup(mountDirParam);
1130 usr_assert(code == 0);
1133 if (mountDirParam) {
1134 argv[argc++] = "-mountdir";
1135 argv[argc++] = mountDirParam;
1138 argv[argc++] = "-confdir";
1139 argv[argc++] = confDirParam;
1141 if (cacheBaseDirParam) {
1142 argv[argc++] = "-cachedir";
1143 argv[argc++] = cacheBaseDirParam;
1145 if (cacheBlocksParam) {
1146 snprintf(buf, sizeof(buf), "%d", cacheBlocksParam);
1148 argv[argc++] = "-blocks";
1149 argv[argc++] = freeargv[freeargc++] = strdup(buf);
1151 if (cacheFilesParam) {
1152 snprintf(buf, sizeof(buf), "%d", cacheFilesParam);
1154 argv[argc++] = "-files";
1155 argv[argc++] = freeargv[freeargc++] = strdup(buf);
1157 if (cacheStatEntriesParam) {
1158 snprintf(buf, sizeof(buf), "%d", cacheStatEntriesParam);
1160 argv[argc++] = "-stat";
1161 argv[argc++] = freeargv[freeargc++] = strdup(buf);
1163 if (dCacheSizeParam) {
1164 snprintf(buf, sizeof(buf), "%d", dCacheSizeParam);
1166 argv[argc++] = "-dcache";
1167 argv[argc++] = freeargv[freeargc++] = strdup(buf);
1169 if (vCacheSizeParam) {
1170 snprintf(buf, sizeof(buf), "%d", vCacheSizeParam);
1172 argv[argc++] = "-volumes";
1173 argv[argc++] = freeargv[freeargc++] = strdup(buf);
1175 if (chunkSizeParam) {
1176 snprintf(buf, sizeof(buf), "%d", chunkSizeParam);
1178 argv[argc++] = "-chunksize";
1179 argv[argc++] = freeargv[freeargc++] = strdup(buf);
1181 if (closeSynchParam) {
1182 argv[argc++] = "-waitclose";
1185 argv[argc++] = "-debug";
1187 if (nDaemonsParam) {
1188 snprintf(buf, sizeof(buf), "%d", nDaemonsParam);
1190 argv[argc++] = "-daemons";
1191 argv[argc++] = freeargv[freeargc++] = strdup(buf);
1193 if (cacheFlagsParam) {
1194 if (cacheFlagsParam & AFSCALL_INIT_MEMCACHE) {
1195 argv[argc++] = "-memcache";
1199 argv[argc++] = "-logfile";
1200 argv[argc++] = logFile;
1205 code = uafs_ParseArgs(argc, argv);
1206 usr_assert(code == 0);
1208 for (i = 0; i < freeargc; i++) {
1213 usr_assert(code == 0);
1217 * Calculate the cacheMountDir used for a specified dir.
1219 * @param[in] dir Desired mount dir
1220 * @param[out] mountdir On success, contains the literal string that should
1221 * be used as the cache mount dir.
1222 * @param[in] size The number of bytes allocated in mountdir
1224 * @post On success, mountdir begins with a slash, and does not contain two
1225 * slashes adjacent to each other
1227 * @return operation status
1229 * @retval ENAMETOOLONG the specified dir is too long to fix in the given
1231 * @retval EINVAL the specified dir does not actually specify any meaningful
1235 calcMountDir(const char *dir, char *mountdir, size_t size)
1242 if (dir && strlen(dir) > size-1) {
1243 return ENAMETOOLONG;
1247 * Initialize the AFS mount point, default is '/afs'.
1248 * Strip duplicate/trailing slashes from mount point string.
1249 * afs_mountDirLen is set to strlen(afs_mountDir).
1254 sprintf(buf, "%s", dir);
1258 for (lastchar = '/', p = &buf[0]; *p != '\0'; p++) {
1259 if (lastchar != '/' || *p != '/') {
1260 mountdir[len++] = lastchar = *p;
1263 if (lastchar == '/' && len > 1)
1265 mountdir[len] = '\0';
1278 * Mount the AFS filesystem
1281 rc = afs_mount(&afs_RootVfs, NULL, NULL);
1282 usr_assert(rc == 0);
1283 rc = afs_root(&afs_RootVfs, &afs_RootVnode);
1284 usr_assert(rc == 0);
1288 * initialize the current directory to the AFS root
1290 afs_CurrentDir = afs_RootVnode;
1291 VN_HOLD(afs_CurrentDir);
1297 uafs_setMountDir(const char *dir)
1301 char tmp_mountDir[1024];
1303 rc = calcMountDir(dir, tmp_mountDir, sizeof(tmp_mountDir));
1305 afs_warn("Invalid mount dir specification (error %d): %s\n", rc, dir);
1307 if (strcmp(tmp_mountDir, afs_mountDir) != 0) {
1308 /* mount dir changed */
1309 strcpy(afs_mountDir, tmp_mountDir);
1310 afs_mountDirLen = strlen(afs_mountDir);
1317 uafs_statvfs(struct statvfs *buf)
1323 rc = afs_statvfs(&afs_RootVfs, buf);
1343 if (afs_CurrentDir) {
1344 VN_RELE(afs_CurrentDir);
1346 rc = afs_unmount(&afs_RootVfs);
1347 usr_assert(rc == 0);
1354 * Donate the current thread to the RX server pool.
1357 uafs_RxServerProc(void)
1361 struct rx_call *newcall = NULL;
1363 rxi_MorePackets(2); /* alloc more packets */
1364 threadID = rxi_availProcs++;
1367 sock = OSI_NULLSOCKET;
1368 rxi_ServerProc(threadID, newcall, &sock);
1369 if (sock == OSI_NULLSOCKET) {
1374 rxi_ListenerProc(sock, &threadID, &newcall);
1375 /* assert(threadID != -1); */
1376 /* assert(newcall != NULL); */
1380 struct syscallThreadArgs {
1389 #ifdef NETSCAPE_NSAPI
1391 syscallThread(void *argp)
1392 #else /* NETSCAPE_NSAPI */
1394 syscallThread(void *argp)
1395 #endif /* NETSCAPE_NSAPI */
1398 struct usr_ucred *crp;
1399 struct syscallThreadArgs *sysArgsP = (struct syscallThreadArgs *)argp;
1402 * AFS daemons run authenticated
1404 get_user_struct()->u_viceid = getuid();
1405 crp = get_user_struct()->u_cred;
1406 afs_set_cr_uid(crp, getuid());
1407 afs_set_cr_ruid(crp, getuid());
1408 crp->cr_suid = getuid();
1409 crp->cr_groups[0] = getgid();
1410 crp->cr_ngroups = 1;
1411 for (i = 1; i < NGROUPS; i++) {
1412 crp->cr_groups[i] = NOGROUP;
1415 call_syscall(sysArgsP->syscall, sysArgsP->afscall, sysArgsP->param1,
1416 sysArgsP->param2, sysArgsP->param3, sysArgsP->param4);
1418 afs_osi_Free(argp, -1);
1423 fork_syscall(long syscall, long afscall, long param1, long param2,
1424 long param3, long param4)
1427 struct syscallThreadArgs *sysArgsP;
1429 sysArgsP = (struct syscallThreadArgs *)
1430 afs_osi_Alloc(sizeof(struct syscallThreadArgs));
1431 usr_assert(sysArgsP != NULL);
1432 sysArgsP->syscall = syscall;
1433 sysArgsP->afscall = afscall;
1434 sysArgsP->param1 = param1;
1435 sysArgsP->param2 = param2;
1436 sysArgsP->param3 = param3;
1437 sysArgsP->param4 = param4;
1439 usr_thread_create(&tid, syscallThread, sysArgsP);
1440 usr_thread_detach(tid);
1445 call_syscall(long syscall, long afscall, long param1, long param2,
1446 long param3, long param4)
1458 a.syscall = syscall;
1459 a.afscall = afscall;
1465 get_user_struct()->u_error = 0;
1466 get_user_struct()->u_ap = (char *)&a;
1468 code = Afs_syscall();
1473 uafs_Setup(const char *mount)
1476 static int inited = 0;
1483 rc = calcMountDir(mount, afs_mountDir, sizeof(afs_mountDir));
1487 afs_mountDirLen = strlen(afs_mountDir);
1489 /* initialize global vars and such */
1492 /* initialize cache manager foo */
1499 uafs_ParseArgs(int argc, char **argv)
1501 return afsd_parse(argc, argv);
1513 return afsd_cacheMountDir;
1517 uafs_SetTokens(char *tbuffer, int tlen)
1520 struct afs_ioctl iob;
1525 iob.out = &outbuf[0];
1526 iob.out_size = sizeof(outbuf);
1528 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(3), (long)&iob, 0, 0);
1537 uafs_RPCStatsEnableProc(void)
1540 struct afs_ioctl iob;
1543 flag = AFSCALL_RXSTATS_ENABLE;
1544 iob.in = (char *)&flag;
1545 iob.in_size = sizeof(afs_int32);
1548 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(53), (long)&iob, 0, 0);
1557 uafs_RPCStatsDisableProc(void)
1560 struct afs_ioctl iob;
1563 flag = AFSCALL_RXSTATS_DISABLE;
1564 iob.in = (char *)&flag;
1565 iob.in_size = sizeof(afs_int32);
1568 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(53), (long)&iob, 0, 0);
1577 uafs_RPCStatsClearProc(void)
1580 struct afs_ioctl iob;
1583 flag = AFSCALL_RXSTATS_CLEAR;
1584 iob.in = (char *)&flag;
1585 iob.in_size = sizeof(afs_int32);
1588 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(53), (long)&iob, 0, 0);
1597 uafs_RPCStatsEnablePeer(void)
1600 struct afs_ioctl iob;
1603 flag = AFSCALL_RXSTATS_ENABLE;
1604 iob.in = (char *)&flag;
1605 iob.in_size = sizeof(afs_int32);
1608 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(54), (long)&iob, 0, 0);
1617 uafs_RPCStatsDisablePeer(void)
1620 struct afs_ioctl iob;
1623 flag = AFSCALL_RXSTATS_DISABLE;
1624 iob.in = (char *)&flag;
1625 iob.in_size = sizeof(afs_int32);
1628 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(54), (long)&iob, 0, 0);
1637 uafs_RPCStatsClearPeer(void)
1640 struct afs_ioctl iob;
1643 flag = AFSCALL_RXSTATS_CLEAR;
1644 iob.in = (char *)&flag;
1645 iob.in_size = sizeof(afs_int32);
1648 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(54), (long)&iob, 0, 0);
1657 * Lookup the target of a symbolic link
1658 * Call VN_HOLD on the output vnode if successful.
1659 * Returns zero on success, error code on failure.
1660 * If provided, use a path for confirming we are not linked to ourself.
1662 * Note: Caller must hold the AFS global lock.
1665 uafs_LookupLinkPath(struct usr_vnode *vp, struct usr_vnode *parentVp,
1666 char *ppathP, struct usr_vnode **vpp)
1671 struct usr_vnode *linkVp;
1673 struct iovec iov[1];
1677 pathP = afs_osi_Alloc(MAX_OSI_PATH + 1);
1678 usr_assert(pathP != NULL);
1681 * set up the uio buffer
1683 iov[0].iov_base = pathP;
1684 iov[0].iov_len = MAX_OSI_PATH + 1;
1685 uio.uio_iov = &iov[0];
1689 uio.uio_fmode = FREAD;
1690 uio.uio_resid = MAX_OSI_PATH + 1;
1693 * Read the link data
1695 code = afs_readlink(VTOAFS(vp), &uio, get_user_struct()->u_cred);
1697 afs_osi_Free(pathP, MAX_OSI_PATH + 1);
1700 len = MAX_OSI_PATH + 1 - uio.uio_resid;
1703 /* are we linked to ourname or ./ourname? ELOOP */
1705 if ((strcmp(pathP, ppathP) == 0) ||
1706 ((pathP[0] == '.') &&
1707 (pathP[1] == '/') &&
1708 (strcmp(&(pathP[2]), ppathP) == 0))) {
1714 * Find the target of the symbolic link
1716 code = uafs_LookupName(pathP, parentVp, &linkVp, 1, 0);
1718 afs_osi_Free(pathP, MAX_OSI_PATH + 1);
1722 afs_osi_Free(pathP, MAX_OSI_PATH + 1);
1728 * Lookup a file or directory given its path.
1729 * Call VN_HOLD on the output vnode if successful.
1730 * Returns zero on success, error code on failure.
1732 * Note: Caller must hold the AFS global lock.
1735 uafs_LookupName(char *path, struct usr_vnode *parentVp,
1736 struct usr_vnode **vpp, int follow, int no_eval_mtpt)
1740 struct usr_vnode *vp;
1741 struct usr_vnode *nextVp;
1742 struct usr_vnode *linkVp;
1743 struct vcache *nextVc;
1746 char *nextPathP = NULL;
1751 * Absolute paths must start with the AFS mount point.
1753 if (path[0] != '/') {
1756 path = uafs_afsPathName(path);
1764 * Loop through the path looking for the new directory
1766 tmpPath = afs_osi_Alloc(strlen(path) + 1);
1767 usr_assert(tmpPath != NULL);
1768 strcpy(tmpPath, path);
1771 while (pathP != NULL && *pathP != '\0') {
1772 usr_assert(*pathP != '/');
1775 * terminate the current component and skip over slashes
1777 nextPathP = afs_strchr(pathP, '/');
1778 if (nextPathP != NULL) {
1779 while (*nextPathP == '/') {
1780 *(nextPathP++) = '\0';
1785 * Don't call afs_lookup on non-directories
1787 if (vp->v_type != VDIR) {
1789 afs_osi_Free(tmpPath, strlen(path) + 1);
1793 if (vp == afs_RootVnode && strcmp(pathP, "..") == 0) {
1795 * The AFS root is its own parent
1797 nextVp = afs_RootVnode;
1800 * We need execute permission to search a directory
1802 code = afs_access(VTOAFS(vp), VEXEC, get_user_struct()->u_cred);
1805 afs_osi_Free(tmpPath, strlen(path) + 1);
1810 * lookup the next component in the path, we can release the
1811 * subdirectory since we hold the global lock
1815 if ((nextPathP != NULL && *nextPathP != '\0') || !no_eval_mtpt)
1816 code = afs_lookup(VTOAFS(vp), pathP, &nextVc, get_user_struct()->u_cred, 0);
1819 afs_lookup(VTOAFS(vp), pathP, &nextVc, get_user_struct()->u_cred,
1822 nextVp=AFSTOV(nextVc);
1825 afs_osi_Free(tmpPath, strlen(path) + 1);
1831 * Follow symbolic links for parent directories and
1832 * for leaves when the follow flag is set.
1834 if ((nextPathP != NULL && *nextPathP != '\0') || follow) {
1836 while (nextVp->v_type == VLNK) {
1837 if (++linkCount > MAX_OSI_LINKS) {
1840 afs_osi_Free(tmpPath, strlen(path) + 1);
1843 code = uafs_LookupLinkPath(nextVp, vp, NULL, &linkVp);
1847 afs_osi_Free(tmpPath, strlen(path) + 1);
1861 * Special case, nextPathP is non-null if pathname ends in slash
1863 if (nextPathP != NULL && vp->v_type != VDIR) {
1865 afs_osi_Free(tmpPath, strlen(path) + 1);
1869 afs_osi_Free(tmpPath, strlen(path) + 1);
1875 uafs_LookupLink(struct usr_vnode *vp, struct usr_vnode *parentVp,
1876 struct usr_vnode **vpp)
1878 return uafs_LookupLinkPath(vp, parentVp, NULL, vpp);
1882 * Lookup the parent of a file or directory given its path
1883 * Call VN_HOLD on the output vnode if successful.
1884 * Returns zero on success, error code on failure.
1886 * Note: Caller must hold the AFS global lock.
1889 uafs_LookupParent(char *path, struct usr_vnode **vpp)
1894 struct usr_vnode *parentP;
1899 * Absolute path names must start with the AFS mount point.
1902 pathP = uafs_afsPathName(path);
1903 if (pathP == NULL) {
1909 * Find the length of the parent path
1912 while (len > 0 && path[len - 1] == '/') {
1918 while (len > 0 && path[len - 1] != '/') {
1925 pathP = afs_osi_Alloc(len);
1926 usr_assert(pathP != NULL);
1927 memcpy(pathP, path, len - 1);
1928 pathP[len - 1] = '\0';
1931 * look up the parent
1933 code = uafs_LookupName(pathP, afs_CurrentDir, &parentP, 1, 0);
1934 afs_osi_Free(pathP, len);
1938 if (parentP->v_type != VDIR) {
1948 * Return a pointer to the first character in the last component
1952 uafs_LastPath(char *path)
1957 while (len > 0 && path[len - 1] == '/') {
1960 while (len > 0 && path[len - 1] != '/') {
1970 * Set the working directory.
1973 uafs_chdir(char *path)
1977 retval = uafs_chdir_r(path);
1983 uafs_chdir_r(char *path)
1988 code = uafs_LookupName(path, afs_CurrentDir, &dirP, 1, 0);
1993 if (dirP->v_type != VDIR) {
1998 VN_RELE(afs_CurrentDir);
1999 afs_CurrentDir = dirP;
2004 * Create a directory.
2007 uafs_mkdir(char *path, int mode)
2011 retval = uafs_mkdir_r(path, mode);
2017 uafs_mkdir_r(char *path, int mode)
2021 struct vnode *parentP;
2022 struct vcache *dirP;
2023 struct usr_vattr attrs;
2025 if (uafs_IsRoot(path)) {
2030 * Look up the parent directory.
2032 nameP = uafs_LastPath(path);
2033 if (nameP != NULL) {
2034 code = uafs_LookupParent(path, &parentP);
2040 parentP = afs_CurrentDir;
2046 * Make sure the directory has at least one character
2048 if (*nameP == '\0') {
2055 * Create the directory
2057 usr_vattr_null(&attrs);
2058 attrs.va_type = VREG;
2059 attrs.va_mode = mode;
2060 attrs.va_uid = afs_cr_uid(get_user_struct()->u_cred);
2061 attrs.va_gid = afs_cr_gid(get_user_struct()->u_cred);
2063 code = afs_mkdir(VTOAFS(parentP), nameP, &attrs, &dirP, get_user_struct()->u_cred);
2069 VN_RELE(AFSTOV(dirP));
2074 * Return 1 if path is the AFS root, otherwise return 0
2077 uafs_IsRoot(char *path)
2079 while (*path == '/' && *(path + 1) == '/') {
2082 if (strncmp(path, afs_mountDir, afs_mountDirLen) != 0) {
2085 path += afs_mountDirLen;
2086 while (*path == '/') {
2089 if (*path != '\0') {
2097 * Note: file name may not end in a slash.
2100 uafs_open(char *path, int flags, int mode)
2104 retval = uafs_open_r(path, flags, mode);
2110 uafs_open_r(char *path, int flags, int mode)
2116 struct usr_vnode *fileP;
2117 struct usr_vnode *dirP;
2118 struct usr_vattr attrs;
2123 if (uafs_IsRoot(path)) {
2124 fileP = afs_RootVnode;
2128 * Look up the parent directory.
2130 nameP = uafs_LastPath(path);
2131 if (nameP != NULL) {
2132 code = uafs_LookupParent(path, &dirP);
2138 dirP = afs_CurrentDir;
2144 * Make sure the filename has at least one character
2146 if (*nameP == '\0') {
2153 * Get the VNODE for this file
2155 if (flags & O_CREAT) {
2156 usr_vattr_null(&attrs);
2157 attrs.va_type = VREG;
2158 attrs.va_mode = mode;
2159 attrs.va_uid = afs_cr_uid(get_user_struct()->u_cred);
2160 attrs.va_gid = afs_cr_gid(get_user_struct()->u_cred);
2161 if (flags & O_TRUNC) {
2167 afs_create(VTOAFS(dirP), nameP, &attrs,
2168 (flags & O_EXCL) ? usr_EXCL : usr_NONEXCL, mode,
2169 &vc, get_user_struct()->u_cred);
2178 code = uafs_LookupName(nameP, dirP, &fileP, 1, 0);
2186 * Check whether we have access to this file
2189 if (flags & (O_RDONLY | O_RDWR)) {
2192 if (flags & (O_WRONLY | O_RDWR)) {
2196 fileMode = VREAD; /* since O_RDONLY is 0 */
2197 code = afs_access(VTOAFS(fileP), fileMode, get_user_struct()->u_cred);
2205 * Get the file attributes, all we need is the size
2207 code = afs_getattr(VTOAFS(fileP), &attrs, get_user_struct()->u_cred);
2217 * Setup the open flags
2220 if (flags & O_TRUNC) {
2221 openFlags |= FTRUNC;
2223 if (flags & O_APPEND) {
2224 openFlags |= FAPPEND;
2226 if (flags & O_SYNC) {
2229 if (flags & O_SYNC) {
2232 if (flags & (O_RDONLY | O_RDWR)) {
2235 if (flags & (O_WRONLY | O_RDWR)) {
2236 openFlags |= FWRITE;
2238 if ((openFlags & (FREAD | FWRITE)) == 0) {
2239 /* O_RDONLY is 0, so ... */
2244 * Truncate if necessary
2246 if ((flags & O_TRUNC) && (attrs.va_size != 0)) {
2247 usr_vattr_null(&attrs);
2248 attrs.va_mask = ATTR_SIZE;
2250 code = afs_setattr(VTOAFS(fileP), &attrs, get_user_struct()->u_cred);
2262 code = afs_open(&vc, openFlags, get_user_struct()->u_cred);
2270 * Put the vnode pointer into the file table
2272 for (fd = 0; fd < MAX_OSI_FILES; fd++) {
2273 if (afs_FileTable[fd] == NULL) {
2274 afs_FileTable[fd] = fileP;
2275 afs_FileFlags[fd] = openFlags;
2276 if (flags & O_APPEND) {
2277 afs_FileOffsets[fd] = attrs.va_size;
2279 afs_FileOffsets[fd] = 0;
2284 if (fd == MAX_OSI_FILES) {
2297 uafs_creat(char *path, int mode)
2300 rc = uafs_open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
2305 uafs_creat_r(char *path, int mode)
2308 rc = uafs_open_r(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
2316 uafs_write(int fd, char *buf, int len)
2320 retval = uafs_pwrite_r(fd, buf, len, afs_FileOffsets[fd]);
2326 uafs_pwrite(int fd, char *buf, int len, off_t offset)
2330 retval = uafs_pwrite_r(fd, buf, len, offset);
2336 uafs_pwrite_r(int fd, char *buf, int len, off_t offset)
2340 struct iovec iov[1];
2341 struct usr_vnode *fileP;
2344 * Make sure this is an open file
2346 fileP = afs_FileTable[fd];
2347 if (fileP == NULL) {
2353 * set up the uio buffer
2355 iov[0].iov_base = buf;
2356 iov[0].iov_len = len;
2357 uio.uio_iov = &iov[0];
2359 uio.uio_offset = offset;
2361 uio.uio_fmode = FWRITE;
2362 uio.uio_resid = len;
2368 code = afs_write(VTOAFS(fileP), &uio, afs_FileFlags[fd], get_user_struct()->u_cred, 0);
2374 afs_FileOffsets[fd] = uio.uio_offset;
2375 return (len - uio.uio_resid);
2382 uafs_read(int fd, char *buf, int len)
2386 retval = uafs_pread_r(fd, buf, len, afs_FileOffsets[fd]);
2392 uafs_pread_nocache(int fd, char *buf, int len, off_t offset)
2396 retval = uafs_pread_nocache_r(fd, buf, len, offset);
2402 uafs_pread_nocache_r(int fd, char *buf, int len, off_t offset)
2405 struct iovec iov[1];
2406 struct usr_vnode *fileP;
2407 struct nocache_read_request *bparms;
2411 * Make sure this is an open file
2413 fileP = afs_FileTable[fd];
2414 if (fileP == NULL) {
2419 /* these get freed in PrefetchNoCache, so... */
2420 bparms = afs_osi_Alloc(sizeof(struct nocache_read_request));
2421 bparms->areq = afs_osi_Alloc(sizeof(struct vrequest));
2423 afs_InitReq(bparms->areq, get_user_struct()->u_cred);
2425 bparms->auio = &uio;
2426 bparms->offset = offset;
2427 bparms->length = len;
2430 * set up the uio buffer
2432 iov[0].iov_base = buf;
2433 iov[0].iov_len = len;
2434 uio.uio_iov = &iov[0];
2436 uio.uio_offset = offset;
2438 uio.uio_fmode = FREAD;
2439 uio.uio_resid = len;
2444 code = afs_PrefetchNoCache(VTOAFS(fileP), get_user_struct()->u_cred,
2452 afs_FileOffsets[fd] = uio.uio_offset;
2453 return (len - uio.uio_resid);
2457 uafs_pread(int fd, char *buf, int len, off_t offset)
2461 retval = uafs_pread_r(fd, buf, len, offset);
2467 uafs_pread_r(int fd, char *buf, int len, off_t offset)
2471 struct iovec iov[1];
2472 struct usr_vnode *fileP;
2475 * Make sure this is an open file
2477 fileP = afs_FileTable[fd];
2478 if (fileP == NULL) {
2484 * set up the uio buffer
2486 iov[0].iov_base = buf;
2487 iov[0].iov_len = len;
2488 uio.uio_iov = &iov[0];
2490 uio.uio_offset = offset;
2492 uio.uio_fmode = FREAD;
2493 uio.uio_resid = len;
2498 code = afs_read(VTOAFS(fileP), &uio, get_user_struct()->u_cred, 0);
2504 afs_FileOffsets[fd] = uio.uio_offset;
2505 return (len - uio.uio_resid);
2509 * Copy the attributes of a file into a stat structure.
2511 * NOTE: Caller must hold the global AFS lock.
2514 uafs_GetAttr(struct usr_vnode *vp, struct stat *stats)
2517 struct usr_vattr attrs;
2522 * Get the attributes
2524 code = afs_getattr(VTOAFS(vp), &attrs, get_user_struct()->u_cred);
2530 * Copy the attributes, zero fields that aren't set
2532 memset((void *)stats, 0, sizeof(struct stat));
2534 stats->st_ino = attrs.va_nodeid;
2535 stats->st_mode = attrs.va_mode;
2536 stats->st_nlink = attrs.va_nlink;
2537 stats->st_uid = attrs.va_uid;
2538 stats->st_gid = attrs.va_gid;
2539 stats->st_rdev = attrs.va_rdev;
2540 stats->st_size = attrs.va_size;
2541 stats->st_atime = attrs.va_atime.tv_sec;
2542 stats->st_mtime = attrs.va_mtime.tv_sec;
2543 stats->st_ctime = attrs.va_ctime.tv_sec;
2544 /* preserve dv if possible */
2545 #if defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
2546 stats->st_atimespec.tv_nsec = attrs.va_atime.tv_usec * 1000;
2547 stats->st_mtimespec.tv_nsec = attrs.va_mtime.tv_usec * 1000;
2548 stats->st_ctimespec.tv_nsec = attrs.va_ctime.tv_usec * 1000;
2549 #elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
2550 stats->st_atimensec = attrs.va_atime.tv_usec * 1000;
2551 stats->st_mtimensec = attrs.va_mtime.tv_usec * 1000;
2552 stats->st_ctimensec = attrs.va_ctime.tv_usec * 1000;
2554 stats->st_blksize = attrs.va_blocksize;
2555 stats->st_blocks = attrs.va_blocks;
2561 * Get the attributes of a file, do follow links
2564 uafs_stat(char *path, struct stat *buf)
2568 retval = uafs_stat_r(path, buf);
2574 uafs_stat_r(char *path, struct stat *buf)
2579 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
2584 code = uafs_GetAttr(vp, buf);
2594 * Get the attributes of a file, don't follow links
2597 uafs_lstat(char *path, struct stat *buf)
2601 retval = uafs_lstat_r(path, buf);
2607 uafs_lstat_r(char *path, struct stat *buf)
2612 code = uafs_LookupName(path, afs_CurrentDir, &vp, 0, 0);
2617 code = uafs_GetAttr(vp, buf);
2627 * Get the attributes of an open file
2630 uafs_fstat(int fd, struct stat *buf)
2634 retval = uafs_fstat_r(fd, buf);
2640 uafs_fstat_r(int fd, struct stat *buf)
2645 vp = afs_FileTable[fd];
2650 code = uafs_GetAttr(vp, buf);
2659 * change the permissions on a file
2662 uafs_chmod(char *path, int mode)
2666 retval = uafs_chmod_r(path, mode);
2672 uafs_chmod_r(char *path, int mode)
2676 struct usr_vattr attrs;
2678 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
2683 usr_vattr_null(&attrs);
2684 attrs.va_mask = ATTR_MODE;
2685 attrs.va_mode = mode;
2686 code = afs_setattr(VTOAFS(vp), &attrs, get_user_struct()->u_cred);
2696 * change the permissions on an open file
2699 uafs_fchmod(int fd, int mode)
2703 retval = uafs_fchmod_r(fd, mode);
2709 uafs_fchmod_r(int fd, int mode)
2713 struct usr_vattr attrs;
2715 vp = afs_FileTable[fd];
2720 usr_vattr_null(&attrs);
2721 attrs.va_mask = ATTR_MODE;
2722 attrs.va_mode = mode;
2723 code = afs_setattr(VTOAFS(vp), &attrs, get_user_struct()->u_cred);
2735 uafs_truncate(char *path, int length)
2739 retval = uafs_truncate_r(path, length);
2745 uafs_truncate_r(char *path, int length)
2749 struct usr_vattr attrs;
2751 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
2756 usr_vattr_null(&attrs);
2757 attrs.va_mask = ATTR_SIZE;
2758 attrs.va_size = length;
2759 code = afs_setattr(VTOAFS(vp), &attrs, get_user_struct()->u_cred);
2769 * truncate an open file
2772 uafs_ftruncate(int fd, int length)
2776 retval = uafs_ftruncate_r(fd, length);
2782 uafs_ftruncate_r(int fd, int length)
2786 struct usr_vattr attrs;
2788 vp = afs_FileTable[fd];
2793 usr_vattr_null(&attrs);
2794 attrs.va_mask = ATTR_SIZE;
2795 attrs.va_size = length;
2796 code = afs_setattr(VTOAFS(vp), &attrs, get_user_struct()->u_cred);
2805 * set the read/write file pointer of an open file
2808 uafs_lseek(int fd, int offset, int whence)
2812 retval = uafs_lseek_r(fd, offset, whence);
2818 uafs_lseek_r(int fd, int offset, int whence)
2822 struct usr_vattr attrs;
2823 struct usr_vnode *vp;
2825 vp = afs_FileTable[fd];
2832 newpos = afs_FileOffsets[fd] + offset;
2838 code = afs_getattr(VTOAFS(vp), &attrs, get_user_struct()->u_cred);
2843 newpos = attrs.va_size + offset;
2853 afs_FileOffsets[fd] = newpos;
2865 retval = uafs_fsync_r(fd);
2871 uafs_fsync_r(int fd)
2874 struct usr_vnode *fileP;
2877 fileP = afs_FileTable[fd];
2878 if (fileP == NULL) {
2883 code = afs_fsync(VTOAFS(fileP), get_user_struct()->u_cred);
2900 retval = uafs_close_r(fd);
2906 uafs_close_r(int fd)
2909 struct usr_vnode *fileP;
2911 fileP = afs_FileTable[fd];
2912 if (fileP == NULL) {
2916 afs_FileTable[fd] = NULL;
2918 code = afs_close(VTOAFS(fileP), afs_FileFlags[fd], get_user_struct()->u_cred);
2929 * Create a hard link from the source to the target
2930 * Note: file names may not end in a slash.
2933 uafs_link(char *existing, char *new)
2937 retval = uafs_link_r(existing, new);
2943 uafs_link_r(char *existing, char *new)
2946 struct usr_vnode *existP;
2947 struct usr_vnode *dirP;
2950 if (uafs_IsRoot(new)) {
2955 * Look up the existing node.
2957 code = uafs_LookupName(existing, afs_CurrentDir, &existP, 1, 0);
2964 * Look up the parent directory.
2966 nameP = uafs_LastPath(new);
2967 if (nameP != NULL) {
2968 code = uafs_LookupParent(new, &dirP);
2975 dirP = afs_CurrentDir;
2981 * Make sure the filename has at least one character
2983 if (*nameP == '\0') {
2993 code = afs_link(VTOAFS(existP), VTOAFS(dirP), nameP, get_user_struct()->u_cred);
3004 * Create a symbolic link from the source to the target
3005 * Note: file names may not end in a slash.
3008 uafs_symlink(char *target, char *source)
3012 retval = uafs_symlink_r(target, source);
3018 uafs_symlink_r(char *target, char *source)
3021 struct usr_vnode *dirP;
3022 struct usr_vattr attrs;
3025 if (uafs_IsRoot(source)) {
3030 * Look up the parent directory.
3032 nameP = uafs_LastPath(source);
3033 if (nameP != NULL) {
3034 code = uafs_LookupParent(source, &dirP);
3040 dirP = afs_CurrentDir;
3046 * Make sure the filename has at least one character
3048 if (*nameP == '\0') {
3057 usr_vattr_null(&attrs);
3058 attrs.va_type = VLNK;
3059 attrs.va_mode = 0777;
3060 attrs.va_uid = afs_cr_uid(get_user_struct()->u_cred);
3061 attrs.va_gid = afs_cr_gid(get_user_struct()->u_cred);
3062 code = afs_symlink(VTOAFS(dirP), nameP, &attrs, target, get_user_struct()->u_cred);
3072 * Read a symbolic link into the buffer
3075 uafs_readlink(char *path, char *buf, int len)
3079 retval = uafs_readlink_r(path, buf, len);
3085 uafs_readlink_r(char *path, char *buf, int len)
3088 struct usr_vnode *vp;
3090 struct iovec iov[1];
3092 code = uafs_LookupName(path, afs_CurrentDir, &vp, 0, 0);
3098 if (vp->v_type != VLNK) {
3105 * set up the uio buffer
3107 iov[0].iov_base = buf;
3108 iov[0].iov_len = len;
3109 uio.uio_iov = &iov[0];
3113 uio.uio_fmode = FREAD;
3114 uio.uio_resid = len;
3119 code = afs_readlink(VTOAFS(vp), &uio, get_user_struct()->u_cred);
3127 * return the number of bytes read
3129 return (len - uio.uio_resid);
3133 * Remove a file (or directory)
3134 * Note: file name may not end in a slash.
3137 uafs_unlink(char *path)
3141 retval = uafs_unlink_r(path);
3147 uafs_unlink_r(char *path)
3150 struct usr_vnode *dirP;
3153 if (uafs_IsRoot(path)) {
3158 * Look up the parent directory.
3160 nameP = uafs_LastPath(path);
3161 if (nameP != NULL) {
3162 code = uafs_LookupParent(path, &dirP);
3168 dirP = afs_CurrentDir;
3174 * Make sure the filename has at least one character
3176 if (*nameP == '\0') {
3185 code = afs_remove(VTOAFS(dirP), nameP, get_user_struct()->u_cred);
3196 * Rename a file (or directory)
3199 uafs_rename(char *old, char *new)
3203 retval = uafs_rename_r(old, new);
3209 uafs_rename_r(char *old, char *new)
3214 struct usr_vnode *odirP;
3215 struct usr_vnode *ndirP;
3217 if (uafs_IsRoot(new)) {
3222 * Look up the parent directories.
3224 onameP = uafs_LastPath(old);
3225 if (onameP != NULL) {
3226 code = uafs_LookupParent(old, &odirP);
3232 odirP = afs_CurrentDir;
3236 nnameP = uafs_LastPath(new);
3237 if (nnameP != NULL) {
3238 code = uafs_LookupParent(new, &ndirP);
3244 ndirP = afs_CurrentDir;
3250 * Make sure the filename has at least one character
3252 if (*onameP == '\0' || *nnameP == '\0') {
3262 code = afs_rename(VTOAFS(odirP), onameP, VTOAFS(ndirP), nnameP, get_user_struct()->u_cred);
3274 * Remove a or directory
3275 * Note: file name may not end in a slash.
3278 uafs_rmdir(char *path)
3282 retval = uafs_rmdir_r(path);
3288 uafs_rmdir_r(char *path)
3291 struct usr_vnode *dirP;
3294 if (uafs_IsRoot(path)) {
3299 * Look up the parent directory.
3301 nameP = uafs_LastPath(path);
3302 if (nameP != NULL) {
3303 code = uafs_LookupParent(path, &dirP);
3309 dirP = afs_CurrentDir;
3315 * Make sure the directory name has at least one character
3317 if (*nameP == '\0') {
3324 * Remove the directory
3326 code = afs_rmdir(VTOAFS(dirP), nameP, get_user_struct()->u_cred);
3337 * Flush a file from the AFS cache
3340 uafs_FlushFile(char *path)
3343 struct afs_ioctl iob;
3351 call_syscall(AFSCALL_PIOCTL, (long)path, _VICEIOCTL(6), (long)&iob, 0,
3362 uafs_FlushFile_r(char *path)
3366 retval = uafs_FlushFile(path);
3375 uafs_opendir(char *path)
3379 retval = uafs_opendir_r(path);
3385 uafs_opendir_r(char *path)
3388 struct usr_vnode *fileP;
3392 * Open the directory for reading
3394 fd = uafs_open_r(path, O_RDONLY, 0);
3399 fileP = afs_FileTable[fd];
3400 if (fileP == NULL) {
3404 if (fileP->v_type != VDIR) {
3411 * Set up the directory structures
3413 dirp = afs_osi_Alloc(sizeof(usr_DIR) + USR_DIRSIZE +
3414 sizeof(struct usr_dirent));
3415 usr_assert(dirp != NULL);
3416 dirp->dd_buf = (char *)(dirp + 1);
3426 * Read directory entries into a file system independent format.
3427 * This routine was developed to support AFS cache consistency testing.
3428 * You should use uafs_readdir instead.
3431 uafs_getdents(int fd, struct min_direct *buf, int len)
3435 retval = uafs_getdents_r(fd, buf, len);
3441 uafs_getdents_r(int fd, struct min_direct *buf, int len)
3445 struct usr_vnode *vp;
3446 struct iovec iov[1];
3449 * Make sure this is an open file
3451 vp = afs_FileTable[fd];
3459 * set up the uio buffer
3461 iov[0].iov_base = (char *)buf;
3462 iov[0].iov_len = len;
3463 uio.uio_iov = &iov[0];
3465 uio.uio_offset = afs_FileOffsets[fd];
3467 uio.uio_fmode = FREAD;
3468 uio.uio_resid = len;
3471 * read the next chunk from the directory
3473 code = afs_readdir(VTOAFS(vp), &uio, get_user_struct()->u_cred);
3479 afs_FileOffsets[fd] = uio.uio_offset;
3480 return (len - uio.uio_resid);
3484 * read from a directory (names only)
3487 uafs_readdir(usr_DIR * dirp)
3489 struct usr_dirent *retval;
3491 retval = uafs_readdir_r(dirp);
3497 uafs_readdir_r(usr_DIR * dirp)
3502 struct usr_vnode *vp;
3503 struct iovec iov[1];
3504 struct usr_dirent *direntP;
3505 struct min_direct *directP;
3513 * Make sure this is an open file
3515 vp = afs_FileTable[dirp->dd_fd];
3522 * If there are no entries in the stream buffer
3523 * then read another chunk
3525 directP = (struct min_direct *)(dirp->dd_buf + dirp->dd_loc);
3526 if (dirp->dd_size == 0 || directP->d_fileno == 0) {
3528 * set up the uio buffer
3530 iov[0].iov_base = dirp->dd_buf;
3531 iov[0].iov_len = USR_DIRSIZE;
3532 uio.uio_iov = &iov[0];
3534 uio.uio_offset = afs_FileOffsets[dirp->dd_fd];
3536 uio.uio_fmode = FREAD;
3537 uio.uio_resid = USR_DIRSIZE;
3540 * read the next chunk from the directory
3542 code = afs_readdir(VTOAFS(vp), &uio, get_user_struct()->u_cred);
3547 afs_FileOffsets[dirp->dd_fd] = uio.uio_offset;
3549 dirp->dd_size = USR_DIRSIZE - iov[0].iov_len;
3551 directP = (struct min_direct *)(dirp->dd_buf + dirp->dd_loc);
3555 * Check for end of file
3557 if (dirp->dd_size == 0 || directP->d_fileno == 0) {
3561 len = ((sizeof(struct min_direct) + directP->d_namlen + 4) & (~3));
3562 usr_assert(len <= dirp->dd_size);
3565 * Copy the next entry into the usr_dirent structure and advance
3567 direntP = (struct usr_dirent *)(dirp->dd_buf + USR_DIRSIZE);
3568 direntP->d_ino = directP->d_fileno;
3569 direntP->d_off = direntP->d_reclen;
3571 sizeof(struct usr_dirent) - MAXNAMLEN + directP->d_namlen + 1;
3572 memcpy(&direntP->d_name[0], (void *)(directP + 1), directP->d_namlen);
3573 direntP->d_name[directP->d_namlen] = '\0';
3574 dirp->dd_loc += len;
3575 dirp->dd_size -= len;
3584 uafs_closedir(usr_DIR * dirp)
3588 retval = uafs_closedir_r(dirp);
3594 uafs_closedir_r(usr_DIR * dirp)
3605 afs_osi_Free((char *)dirp,
3606 sizeof(usr_DIR) + USR_DIRSIZE + sizeof(struct usr_dirent));
3607 rc = uafs_close_r(fd);
3612 * Destroy AFS credentials from the kernel cache
3619 usr_mutex_lock(&osi_authenticate_lock);
3620 code = ktc_ForgetAllTokens();
3621 usr_mutex_unlock(&osi_authenticate_lock);
3630 retval = uafs_unlog();
3636 * Strip the AFS mount point from a pathname string. Return
3637 * NULL if the path is a relative pathname or if the path
3638 * doesn't start with the AFS mount point string.
3641 uafs_afsPathName(char *path)
3650 for (i = 1, p = path + 1; *p != '\0'; p++) {
3651 /* Ignore duplicate slashes */
3652 if (*p == '/' && lastchar == '/')
3654 /* Is this a subdirectory of the AFS mount point? */
3655 if (afs_mountDir[i] == '\0' && *p == '/') {
3656 /* strip leading slashes */
3657 while (*(++p) == '/');
3660 /* Reject paths that are not within AFS */
3661 if (*p != afs_mountDir[i])
3666 /* Is this the AFS mount point? */
3667 if (afs_mountDir[i] == '\0') {
3668 usr_assert(*p == '\0');
3675 * uafs_getcellstatus
3676 * get the cell status
3679 uafs_getcellstatus(char *cell, afs_int32 * status)
3682 struct afs_ioctl iob;
3685 iob.in_size = strlen(cell) + 1;
3689 rc = call_syscall(AFSCALL_PIOCTL, /*path */ 0, _VICEIOCTL(35),
3697 *status = (intptr_t)iob.out;
3703 * Get quota of volume associated with path
3706 uafs_getvolquota(char *path, afs_int32 * BlocksInUse, afs_int32 * MaxQuota)
3709 struct afs_ioctl iob;
3710 VolumeStatus status;
3714 iob.out = (char *)&status;
3715 iob.out_size = sizeof status;
3717 rc = call_syscall(AFSCALL_PIOCTL, (long)path, _VICEIOCTL(4), (long)&iob,
3725 *BlocksInUse = status.BlocksInUse;
3726 *MaxQuota = status.MaxQuota;
3732 * Set quota of volume associated with path
3735 uafs_setvolquota(char *path, afs_int32 MaxQuota)
3738 struct afs_ioctl iob;
3739 VolumeStatus status = { 0 };
3741 iob.in = (char *)&status;
3742 iob.in_size = sizeof status;
3746 status.MaxQuota = MaxQuota;
3747 status.MinQuota = -1;
3749 rc = call_syscall(AFSCALL_PIOCTL, (long)path, _VICEIOCTL(5), (long)&iob,
3761 * uafs_statmountpoint
3762 * Determine whether a dir. is a mount point or not
3763 * return 1 if mount point, 0 if not
3766 uafs_statmountpoint(char *path)
3771 retval = uafs_statmountpoint_r(path);
3777 uafs_statmountpoint_r(char *path)
3784 code = uafs_LookupName(path, afs_CurrentDir, &vp, 0, 1);
3799 * Get a list of rights for the current user on path.
3802 uafs_access(char *path, int flags)
3819 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
3826 code = afs_access(VTOAFS(vp), fileMode, get_user_struct()->u_cred);
3833 return code ? -1 : 0;
3838 * Get a list of rights for the current user on path.
3841 uafs_getRights(char *path)
3848 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
3856 PRSFS_READ | PRSFS_WRITE | PRSFS_INSERT | PRSFS_LOOKUP | PRSFS_DELETE
3857 | PRSFS_LOCK | PRSFS_ADMINISTER;
3859 afs_rights = afs_getRights(VTOAFS(vp), afs_rights, get_user_struct()->u_cred);
3864 #endif /* UKERNEL */