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
11 * User space client specific interface glue
14 #include <afsconfig.h>
15 #include "afs/param.h"
23 #include "afs/sysincludes.h" /* Standard vendor system headers */
25 #include "afsincludes.h" /* Afs-based standard headers */
26 #include "afs_usrops.h"
27 #include "afs/afs_stats.h"
29 #include "afs/cellconfig.h"
31 #include "afs/kautils.h"
32 #include "afs/afsutil.h"
33 #include "rx/rx_globals.h"
39 #define CACHEINFOFILE "cacheinfo"
40 #define AFSLOGFILE "AFSLog"
41 #define DCACHEFILE "CacheItems"
42 #define VOLINFOFILE "VolumeItems"
43 #define CELLINFOFILE "CellItems"
47 #define MIN(A,B) ((A)<(B)?(A):(B))
50 #define MAX(A,B) ((A)>(B)?(A):(B))
53 extern int cacheDiskType;
55 char afs_LclCellName[64];
57 struct usr_vnode *afs_FileTable[MAX_OSI_FILES];
58 int afs_FileFlags[MAX_OSI_FILES];
59 int afs_FileOffsets[MAX_OSI_FILES];
61 #define MAX_CACHE_LOOPS 4
63 struct usr_vfs afs_RootVfs;
64 struct usr_vnode *afs_RootVnode = NULL;
65 struct usr_vnode *afs_CurrentDir = NULL;
67 afs_int32 cacheBlocks; /* Num blocks in cache */
68 afs_int32 cacheFiles = 1000; /* Num files in workstation cache */
69 afs_int32 cacheStatEntries = 300; /* Num of stat cache entries */
70 char cacheBaseDir[1024]; /* AFS cache directory */
71 char confDir[1024]; /* AFS configuration directory */
72 char afs_mountDir[1024]; /* AFS mount point */
73 int afs_mountDirLen; /* strlen of AFS mount point */
74 char fullpn_DCacheFile[1024]; /* Full pathname of DCACHEFILE */
75 char fullpn_VolInfoFile[1024]; /* Full pathname of VOLINFOFILE */
76 char fullpn_CellInfoFile[1024]; /* Full pathname of CELLINFOFILE */
77 char fullpn_AFSLogFile[1024]; /* Full pathname of AFSLOGFILE */
78 char fullpn_CacheInfo[1024]; /* Full pathname of CACHEINFO */
79 char fullpn_VFile[1024]; /* Full pathname of data cache files */
80 char *vFileNumber; /* Ptr to number in file pathname */
81 char rootVolume[64] = "root.afs"; /* AFS root volume name */
82 afs_int32 isHomeCell; /* Is current cell info for home cell */
83 int createAndTrunc = O_CREAT | O_TRUNC; /* Create & truncate on open */
84 int ownerRWmode = 0600; /* Read/write OK by owner */
85 static int nDaemons = 2; /* Number of background daemons */
86 static int chunkSize = 0; /* 2^chunkSize bytes per chunk */
87 static int dCacheSize = 300; /* # of dcache entries */
88 static int vCacheSize = 50; /* # of volume cache entries */
89 static int cacheFlags = 0; /* Flags to cache manager */
90 static int preallocs = 400; /* Def # of allocated memory blocks */
91 int afsd_verbose = 0; /* Are we being chatty? */
92 int afsd_debug = 0; /* Are we printing debugging info? */
93 int afsd_CloseSynch = 0; /* Are closes synchronous or not? */
95 #define AFSD_INO_T afs_uint32
96 char **pathname_for_V; /* Array of cache file pathnames */
97 int missing_DCacheFile = 1; /* Is the DCACHEFILE missing? */
98 int missing_VolInfoFile = 1; /* Is the VOLINFOFILE missing? */
99 int missing_CellInfoFile = 1;
100 struct afs_cacheParams cparams; /* params passed to cache manager */
101 struct afsconf_dir *afs_cdir; /* config dir */
103 static int HandleMTab();
105 int afs_bufferpages = 100;
106 int usr_udpcksum = 0;
108 usr_key_t afs_global_u_key;
110 struct usr_proc *afs_global_procp;
111 struct usr_ucred *afs_global_ucredp;
112 struct usr_sysent usr_sysent[200];
114 #ifdef AFS_USR_OSF_ENV
116 #else /* AFS_USR_OSF_ENV */
118 #endif /* AFS_USR_OSF_ENV */
120 struct usr_ucred afs_osi_cred, *afs_osi_credp;
121 usr_mutex_t afs_global_lock;
122 usr_thread_t afs_global_owner;
123 usr_mutex_t rx_global_lock;
124 usr_thread_t rx_global_owner;
125 usr_mutex_t osi_inode_lock;
126 usr_mutex_t osi_waitq_lock;
127 usr_mutex_t osi_authenticate_lock;
129 afs_lock_t osi_flplock;
130 afs_lock_t osi_fsplock;
133 #ifndef NETSCAPE_NSAPI
136 * Mutex and condition variable used to implement sleep
138 pthread_mutex_t usr_sleep_mutex;
139 pthread_cond_t usr_sleep_cond;
141 #endif /* !NETSCAPE_NSAPI */
143 int call_syscall(long, long, long, long, long, long);
147 * Hash table mapping addresses onto wait structures for
148 * osi_Sleep/osi_Wakeup and osi_Wait/osi_Wakeup
150 typedef struct osi_wait {
154 struct osi_wait *next;
155 struct osi_wait *prev;
157 struct osi_wait *timedNext;
158 struct osi_wait *timedPrev;
162 * Head of the linked list of available waitq structures.
164 osi_wait_t *osi_waithash_avail;
167 * List of timed waits, NSAPI does not provide a cond_timed
168 * wait, so we need to keep track of the timed waits ourselves and
169 * periodically check for expirations
171 osi_wait_t *osi_timedwait_head;
172 osi_wait_t *osi_timedwait_tail;
177 } osi_waithash_table[OSI_WAITHASH_SIZE];
180 * Never call afs_brelse
183 ufs_brelse(struct usr_vnode *vp, struct usr_buf *bp)
189 * I am not sure what to do with these, they assert for now
192 iodone(struct usr_buf *bp)
204 * Every user is a super user
207 afs_osi_suser(void *credp)
213 afs_suser(void *credp)
219 * These are no-ops in user space
223 afs_osi_SetTime(osi_timeval_t * atv)
229 * xflock should never fall through, the only files we know
230 * about are AFS files
239 * ioctl should never fall through, the only files we know
240 * about are AFS files
249 * We do not support the inode related system calls
252 afs_syscall_icreate(void)
258 afs_syscall_iincdec(void)
264 afs_syscall_iopen(void)
270 afs_syscall_ireadwrite(void)
276 * these routines are referenced in the vfsops structure, but
277 * should never get called
304 * uiomove copies data between kernel buffers and uio buffers
307 usr_uiomove(char *kbuf, int n, int rw, struct usr_uio *uio)
314 nio = uio->uio_iovcnt;
324 while (nio > 0 && n > 0) {
325 len = MIN(n, iovp->iov_len);
326 if (rw == UIO_READ) {
327 memcpy(iovp->iov_base, ptr, len);
329 memcpy(ptr, iovp->iov_base, len);
333 uio->uio_resid -= len;
334 uio->uio_offset += len;
335 iovp->iov_base = (char *)(iovp->iov_base) + len;
336 iovp->iov_len -= len;
347 * routines to manage user credentials
350 usr_crcopy(struct usr_ucred *credp)
352 struct usr_ucred *newcredp;
354 newcredp = (struct usr_ucred *)afs_osi_Alloc(sizeof(struct usr_ucred));
356 newcredp->cr_ref = 1;
363 struct usr_ucred *newcredp;
365 newcredp = (struct usr_ucred *)afs_osi_Alloc(sizeof(struct usr_ucred));
366 newcredp->cr_ref = 1;
371 usr_crfree(struct usr_ucred *credp)
374 if (credp->cr_ref == 0) {
375 afs_osi_Free((char *)credp, sizeof(struct usr_ucred));
380 usr_crhold(struct usr_ucred *credp)
386 usr_vattr_null(struct usr_vattr *vap)
391 n = sizeof(struct usr_vattr);
399 * Initialize the thread specific data used to simulate the
400 * kernel environment for each thread. The user structure
401 * is stored in the thread specific data.
404 uafs_InitThread(void)
407 struct usr_user *uptr;
410 * initialize the thread specific user structure. Use malloc to
411 * allocate the data block, so pthread_finish can free the buffer
412 * when this thread terminates.
415 (struct usr_user *)malloc(sizeof(struct usr_user) +
416 sizeof(struct usr_ucred));
417 usr_assert(uptr != NULL);
420 uptr->u_procp = afs_global_procp;
421 uptr->u_cred = (struct usr_ucred *)(uptr + 1);
422 *uptr->u_cred = *afs_global_ucredp;
423 st = usr_setspecific(afs_global_u_key, (void *)uptr);
428 * routine to get the user structure from the thread specific data.
429 * this routine is used to implement the global 'u' structure. Initializes
430 * the thread if needed.
433 get_user_struct(void)
435 struct usr_user *uptr;
437 st = usr_getspecific(afs_global_u_key, (void **)&uptr);
441 st = usr_getspecific(afs_global_u_key, (void **)&uptr);
443 usr_assert(uptr != NULL);
449 * Hash an address for the waithash table
451 #define WAITHASH(X) \
452 (((long)(X)^((long)(X)>>4)^((long)(X)<<4))&(OSI_WAITHASH_SIZE-1))
458 afs_osi_Sleep(void *x)
462 int glockOwner = ISAFS_GLOCK();
464 usr_mutex_lock(&osi_waitq_lock);
469 if (osi_waithash_avail == NULL) {
470 waitp = (osi_wait_t *) afs_osi_Alloc(sizeof(osi_wait_t));
471 usr_cond_init(&waitp->cond);
473 waitp = osi_waithash_avail;
474 osi_waithash_avail = osi_waithash_avail->next;
478 DLL_INSERT_TAIL(waitp, osi_waithash_table[index].head,
479 osi_waithash_table[index].tail, next, prev);
480 waitp->expiration = 0;
481 waitp->timedNext = NULL;
482 waitp->timedPrev = NULL;
483 while (waitp->flag == 0) {
484 usr_cond_wait(&waitp->cond, &osi_waitq_lock);
486 DLL_DELETE(waitp, osi_waithash_table[index].head,
487 osi_waithash_table[index].tail, next, prev);
488 waitp->next = osi_waithash_avail;
489 osi_waithash_avail = waitp;
490 usr_mutex_unlock(&osi_waitq_lock);
497 afs_osi_SleepSig(void *x)
504 afs_osi_Wakeup(void *x)
510 usr_mutex_lock(&osi_waitq_lock);
511 waitp = osi_waithash_table[index].head;
513 if (waitp->addr == x && waitp->flag == 0) {
515 usr_cond_signal(&waitp->cond);
519 usr_mutex_unlock(&osi_waitq_lock);
523 afs_osi_Wait(afs_int32 msec, struct afs_osi_WaitHandle *handle, int intok)
529 int glockOwner = ISAFS_GLOCK();
531 tv.tv_sec = msec / 1000;
532 tv.tv_nsec = (msec % 1000) * 1000000;
533 if (handle == NULL) {
537 usr_thread_sleep(&tv);
543 usr_mutex_lock(&osi_waitq_lock);
547 index = WAITHASH((caddr_t) handle);
548 if (osi_waithash_avail == NULL) {
549 waitp = (osi_wait_t *) afs_osi_Alloc(sizeof(osi_wait_t));
550 usr_cond_init(&waitp->cond);
552 waitp = osi_waithash_avail;
553 osi_waithash_avail = osi_waithash_avail->next;
555 waitp->addr = (caddr_t) handle;
557 DLL_INSERT_TAIL(waitp, osi_waithash_table[index].head,
558 osi_waithash_table[index].tail, next, prev);
559 tv.tv_sec += time(NULL);
560 waitp->expiration = tv.tv_sec + ((tv.tv_nsec == 0) ? 0 : 1);
561 DLL_INSERT_TAIL(waitp, osi_timedwait_head, osi_timedwait_tail,
562 timedNext, timedPrev);
563 usr_cond_wait(&waitp->cond, &osi_waitq_lock);
569 DLL_DELETE(waitp, osi_waithash_table[index].head,
570 osi_waithash_table[index].tail, next, prev);
571 DLL_DELETE(waitp, osi_timedwait_head, osi_timedwait_tail, timedNext,
573 waitp->next = osi_waithash_avail;
574 osi_waithash_avail = waitp;
575 usr_mutex_unlock(&osi_waitq_lock);
584 afs_osi_CancelWait(struct afs_osi_WaitHandle *handle)
586 afs_osi_Wakeup(handle);
590 * Netscape NSAPI doesn't have a cond_timed_wait, so we need
591 * to explicitly signal cond_timed_waits when their timers expire
594 afs_osi_CheckTimedWaits(void)
599 curTime = time(NULL);
600 usr_mutex_lock(&osi_waitq_lock);
601 waitp = osi_timedwait_head;
602 while (waitp != NULL) {
603 usr_assert(waitp->expiration != 0);
604 if (waitp->expiration <= curTime) {
606 usr_cond_signal(&waitp->cond);
608 waitp = waitp->timedNext;
610 usr_mutex_unlock(&osi_waitq_lock);
614 * I-node numbers are indeces into a table containing a filename
615 * i-node structure and a vnode structure. When we create an i-node,
616 * we copy the name into the array and initialize enough of the fields
617 * in the inode and vnode structures to get the client to work.
620 struct usr_inode i_node;
623 osi_file_table_t *osi_file_table;
625 int max_osi_files = 0;
628 * Allocate a slot in the file table if there is not one there already,
629 * copy in the file name and kludge up the vnode and inode structures
632 lookupname(char *fnamep, int segflg, int followlink,
633 struct usr_vnode **compvpp)
637 struct usr_inode *ip;
638 struct usr_vnode *vp;
640 /*usr_assert(followlink == 0); */
643 * Assume relative pathnames refer to files in AFS
645 if (*fnamep != '/' || uafs_afsPathName(fnamep) != NULL) {
647 code = uafs_LookupName(fnamep, afs_CurrentDir, compvpp, 0, 0);
652 usr_mutex_lock(&osi_inode_lock);
654 for (i = 0; i < n_osi_files; i++) {
655 if (strcmp(fnamep, osi_file_table[i].name) == 0) {
656 *compvpp = &osi_file_table[i].i_node.i_vnode;
657 (*compvpp)->v_count++;
658 usr_mutex_unlock(&osi_inode_lock);
663 if (n_osi_files == max_osi_files) {
664 usr_mutex_unlock(&osi_inode_lock);
668 osi_file_table[n_osi_files].name = afs_osi_Alloc(strlen(fnamep) + 1);
669 usr_assert(osi_file_table[n_osi_files].name != NULL);
670 strcpy(osi_file_table[n_osi_files].name, fnamep);
671 ip = &osi_file_table[i].i_node;
673 vp->v_data = (caddr_t) ip;
676 ip->i_number = n_osi_files;
678 usr_mutex_unlock(&osi_inode_lock);
684 * open a file given its i-node number
687 osi_UFSOpen(afs_int32 ino)
695 if (ino > n_osi_files) {
701 fp = (struct osi_file *)afs_osi_Alloc(sizeof(struct osi_file));
702 usr_assert(fp != NULL);
703 fp->fd = open(osi_file_table[ino - 1].name, O_RDWR | O_CREAT, 0);
706 afs_osi_Free((char *)fp, sizeof(struct osi_file));
710 rc = fstat(fp->fd, &st);
713 afs_osi_Free((void *)fp, sizeof(struct osi_file));
717 fp->size = st.st_size;
720 fp->vnode = (struct usr_vnode *)fp;
727 osi_UFSClose(struct osi_file *fp)
737 afs_osi_Free((void *)fp, sizeof(struct osi_file));
741 afs_osi_Free((void *)fp, sizeof(struct osi_file));
747 osi_UFSTruncate(struct osi_file *fp, afs_int32 len)
754 rc = ftruncate(fp->fd, len);
766 afs_osi_Read(struct osi_file *fp, int offset, void *buf, afs_int32 len)
776 rc = lseek(fp->fd, offset, SEEK_SET);
778 rc = lseek(fp->fd, fp->offset, SEEK_SET);
786 ret = read(fp->fd, buf, len);
793 rc = fstat(fp->fd, &st);
799 fp->size = st.st_size;
805 afs_osi_Write(struct osi_file *fp, afs_int32 offset, void *buf, afs_int32 len)
815 rc = lseek(fp->fd, offset, SEEK_SET);
817 rc = lseek(fp->fd, fp->offset, SEEK_SET);
825 ret = write(fp->fd, buf, len);
832 rc = fstat(fp->fd, &st);
838 fp->size = st.st_size;
844 afs_osi_Stat(struct osi_file *fp, struct osi_stat *stp)
850 rc = fstat(fp->fd, &st);
856 stp->size = st.st_size;
857 stp->mtime = st.st_mtime;
858 stp->atime = st.st_atime;
867 afs_osi_VOP_RDWR(struct usr_vnode *vnodeP, struct usr_uio *uioP, int rw,
868 int flags, struct usr_ucred *credP)
871 struct osi_file *fp = (struct osi_file *)vnodeP;
874 * We don't support readv/writev.
876 usr_assert(uioP->uio_iovcnt == 1);
877 usr_assert(uioP->uio_resid == uioP->uio_iov[0].iov_len);
879 if (rw == UIO_WRITE) {
880 usr_assert(uioP->uio_fmode == FWRITE);
881 rc = afs_osi_Write(fp, uioP->uio_offset, uioP->uio_iov[0].iov_base,
882 uioP->uio_iov[0].iov_len);
884 usr_assert(uioP->uio_fmode == FREAD);
885 rc = afs_osi_Read(fp, uioP->uio_offset, uioP->uio_iov[0].iov_base,
886 uioP->uio_iov[0].iov_len);
892 uioP->uio_resid -= rc;
893 uioP->uio_offset += rc;
894 uioP->uio_iov[0].iov_base = (char *)(uioP->uio_iov[0].iov_base) + rc;
895 uioP->uio_iov[0].iov_len -= rc;
900 * Use malloc/free routines with check patterns before and after each block
903 static char *afs_check_string1 = "UAFS";
904 static char *afs_check_string2 = "AFS_OSI_";
907 afs_osi_Alloc(size_t size)
913 afs_osi_Free(void *ptr, size_t size)
919 afs_osi_FreeStr(char *ptr)
925 osi_AllocLargeSpace(size_t size)
927 AFS_STATCNT(osi_AllocLargeSpace);
928 return afs_osi_Alloc(size);
932 osi_FreeLargeSpace(void *ptr)
934 AFS_STATCNT(osi_FreeLargeSpace);
935 afs_osi_Free(ptr, 0);
939 osi_AllocSmallSpace(size_t size)
941 AFS_STATCNT(osi_AllocSmallSpace);
942 return afs_osi_Alloc(size);
946 osi_FreeSmallSpace(void *ptr)
948 AFS_STATCNT(osi_FreeSmallSpace);
949 afs_osi_Free(ptr, 0);
955 AFS_STATCNT(shutdown_osi);
960 shutdown_osinet(void)
962 AFS_STATCNT(shutdown_osinet);
967 shutdown_osifile(void)
969 AFS_STATCNT(shutdown_osifile);
974 afs_nfsclient_init(void)
979 shutdown_nfsclnt(void)
985 afs_osi_Invisible(void)
991 osi_GetTime(struct timeval *tv)
993 gettimeofday(tv, NULL);
998 osi_SetTime(struct timeval *tv)
1004 osi_Active(struct vcache *avc)
1006 AFS_STATCNT(osi_Active);
1013 afs_osi_MapStrategy(int (*aproc) (), struct usr_buf *bp)
1015 afs_int32 returnCode;
1016 returnCode = (*aproc) (bp);
1021 osi_FlushPages(register struct vcache *avc, struct AFS_UCRED *credp)
1023 ObtainSharedLock(&avc->lock, 555);
1024 if ((hcmp((avc->m.DataVersion), (avc->mapDV)) <= 0)
1025 || ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
1026 ReleaseSharedLock(&avc->lock);
1029 UpgradeSToWLock(&avc->lock, 565);
1030 hset(avc->mapDV, avc->m.DataVersion);
1031 ReleaseWriteLock(&avc->lock);
1036 osi_FlushText_really(register struct vcache *vp)
1038 if (hcmp(vp->m.DataVersion, vp->flushDV) > 0) {
1039 hset(vp->flushDV, vp->m.DataVersion);
1045 osi_SyncVM(struct vcache *avc)
1051 osi_ReleaseVM(struct vcache *avc, int len, struct usr_ucred *credp)
1064 * Allocate the table used to implement psuedo-inodes.
1066 max_osi_files = cacheFiles + 100;
1067 osi_file_table = (osi_file_table_t *)
1068 afs_osi_Alloc(max_osi_files * sizeof(osi_file_table_t));
1069 usr_assert(osi_file_table != NULL);
1071 #ifndef NETSCAPE_NSAPI
1073 * Initialize the mutex and condition variable used to implement
1076 pthread_mutex_init(&usr_sleep_mutex, NULL);
1077 pthread_cond_init(&usr_sleep_cond, NULL);
1078 #endif /* !NETSCAPE_NSAPI */
1081 * Initialize the hash table used for sleep/wakeup
1083 for (i = 0; i < OSI_WAITHASH_SIZE; i++) {
1084 DLL_INIT_LIST(osi_waithash_table[i].head, osi_waithash_table[i].tail);
1086 DLL_INIT_LIST(osi_timedwait_head, osi_timedwait_tail);
1087 osi_waithash_avail = NULL;
1090 * Initialize the AFS file table
1092 for (i = 0; i < MAX_OSI_FILES; i++) {
1093 afs_FileTable[i] = NULL;
1097 * Initialize the global locks
1099 usr_mutex_init(&afs_global_lock);
1100 usr_mutex_init(&rx_global_lock);
1101 usr_mutex_init(&osi_inode_lock);
1102 usr_mutex_init(&osi_waitq_lock);
1103 usr_mutex_init(&osi_authenticate_lock);
1106 * Initialize the AFS OSI credentials
1108 afs_osi_cred = *afs_global_ucredp;
1109 afs_osi_credp = &afs_osi_cred;
1112 /* ParseArgs is now obsolete, being handled by cmd */
1114 /*---------------------------------------------------------------------
1118 * Given the final component of a filename expected to be a data cache file,
1119 * return the integer corresponding to the file. Note: we reject names that
1120 * are not a ``V'' followed by an integer. We also reject those names having
1121 * the right format but lying outside the range [0..cacheFiles-1].
1124 * fname : Char ptr to the filename to parse.
1127 * >= 0 iff the file is really a data cache file numbered from 0 to cacheFiles-1, or
1131 * Nothing interesting.
1135 *------------------------------------------------------------------------*/
1138 GetVFileNumber(char *fname)
1140 int computedVNumber; /*The computed file number we return */
1141 int filenameLen; /*Number of chars in filename */
1142 int currDigit; /*Current digit being processed */
1145 * The filename must have at least two characters, the first of which must be a ``V''
1146 * and the second of which cannot be a zero unless the file is exactly two chars long.
1148 filenameLen = strlen(fname);
1149 if (filenameLen < 2)
1151 if (fname[0] != 'V')
1153 if ((filenameLen > 2) && (fname[1] == '0'))
1157 * Scan through the characters in the given filename, failing immediately if a non-digit
1160 for (currDigit = 1; currDigit < filenameLen; currDigit++)
1161 if (isdigit(fname[currDigit]) == 0)
1165 * All relevant characters are digits. Pull out the decimal number they represent.
1166 * Reject it if it's out of range, otherwise return it.
1168 computedVNumber = atoi(++fname);
1169 if (computedVNumber < cacheFiles)
1170 return (computedVNumber);
1175 /*---------------------------------------------------------------------
1179 * Given a full pathname for a file we need to create for the workstation AFS
1180 * cache, go ahead and create the file.
1183 * fname : Full pathname of file to create.
1186 * 0 iff the file was created,
1190 * The given cache file has been found to be missing.
1194 *------------------------------------------------------------------------*/
1197 CreateCacheFile(char *fname)
1199 static char rn[] = "CreateCacheFile"; /*Routine name */
1200 int cfd; /*File descriptor to AFS cache file */
1201 int closeResult; /*Result of close() */
1204 printf("%s: Creating cache file '%s'\n", rn, fname);
1205 cfd = open(fname, createAndTrunc, ownerRWmode);
1207 printf("%s: Can't create '%s', error return is %d (%d)\n", rn, fname,
1211 closeResult = close(cfd);
1214 ("%s: Can't close newly-created AFS cache file '%s' (code %d)\n",
1222 /*---------------------------------------------------------------------
1226 * Sweep through the AFS cache directory, recording the inode number for
1227 * each valid data cache file there. Also, delete any file that doesn't beint32
1228 * in the cache directory during this sweep, and remember which of the other
1229 * residents of this directory were seen. After the sweep, we create any data
1230 * cache files that were missing.
1233 * vFilesFound : Set to the number of data cache files found.
1236 * 0 if everything went well,
1240 * This routine may be called several times. If the number of data cache files
1241 * found is less than the global cacheFiles, then the caller will need to call it
1242 * again to record the inodes of the missing zero-length data cache files created
1243 * in the previous call.
1246 * Fills up the global pathname_for_V array, may create and/or
1247 * delete files as explained above.
1248 *------------------------------------------------------------------------*/
1251 SweepAFSCache(int *vFilesFound)
1253 static char rn[] = "SweepAFSCache"; /*Routine name */
1254 char fullpn_FileToDelete[1024]; /*File to be deleted from cache */
1255 char *fileToDelete; /*Ptr to last component of above */
1256 DIR *cdirp; /*Ptr to cache directory structure */
1258 struct dirent *currp; /*Current directory entry */
1259 int vFileNum; /*Data cache file's associated number */
1261 if (cacheFlags & AFSCALL_INIT_MEMCACHE) {
1263 printf("%s: Memory Cache, no cache sweep done\n", rn);
1269 printf("%s: Opening cache directory '%s'\n", rn, cacheBaseDir);
1271 if (chmod(cacheBaseDir, 0700)) { /* force it to be 700 */
1272 printf("%s: Can't 'chmod 0700' the cache dir, '%s'.\n", rn,
1276 cdirp = opendir(cacheBaseDir);
1277 if (cdirp == (DIR *) 0) {
1278 printf("%s: Can't open AFS cache directory, '%s'.\n", rn,
1284 * Scan the directory entries, remembering data cache file inodes and the existance
1285 * of other important residents. Delete all files that don't belong here.
1288 sprintf(fullpn_FileToDelete, "%s/", cacheBaseDir);
1289 fileToDelete = fullpn_FileToDelete + strlen(fullpn_FileToDelete);
1291 for (currp = readdir(cdirp); currp; currp = readdir(cdirp)) {
1293 printf("%s: Current directory entry:\n", rn);
1294 printf("\tinode=%d, reclen=%d, name='%s'\n", currp->d_ino,
1295 currp->d_reclen, currp->d_name);
1299 * Guess current entry is for a data cache file.
1301 vFileNum = GetVFileNumber(currp->d_name);
1302 if (vFileNum >= 0) {
1304 * Found a valid data cache filename. Remember this file's name
1305 * and bump the number of files found.
1307 pathname_for_V[vFileNum] =
1308 afs_osi_Alloc(strlen(currp->d_name) + strlen(cacheBaseDir) +
1310 usr_assert(pathname_for_V[vFileNum] != NULL);
1311 sprintf(pathname_for_V[vFileNum], "%s/%s", cacheBaseDir,
1314 } else if (strcmp(currp->d_name, DCACHEFILE) == 0) {
1316 * Found the file holding the dcache entries.
1318 missing_DCacheFile = 0;
1319 } else if (strcmp(currp->d_name, VOLINFOFILE) == 0) {
1321 * Found the file holding the volume info.
1323 missing_VolInfoFile = 0;
1324 } else if (strcmp(currp->d_name, CELLINFOFILE) == 0) {
1325 missing_CellInfoFile = 0;
1326 } else if ((strcmp(currp->d_name, ".") == 0)
1327 || (strcmp(currp->d_name, "..") == 0)
1328 || (strcmp(currp->d_name, "lost+found") == 0)) {
1330 * Don't do anything - this file is legit, and is to be left alone.
1334 * This file doesn't belong in the cache. Nuke it.
1336 sprintf(fileToDelete, "%s", currp->d_name);
1338 printf("%s: Deleting '%s'\n", rn, fullpn_FileToDelete);
1339 if (unlink(fullpn_FileToDelete)) {
1340 printf("%s: Can't unlink '%s', errno is %d\n", rn,
1341 fullpn_FileToDelete, errno);
1347 * Create all the cache files that are missing.
1349 if (missing_DCacheFile) {
1351 printf("%s: Creating '%s'\n", rn, fullpn_DCacheFile);
1352 if (CreateCacheFile(fullpn_DCacheFile))
1353 printf("%s: Can't create '%s'\n", rn, fullpn_DCacheFile);
1355 if (missing_VolInfoFile) {
1357 printf("%s: Creating '%s'\n", rn, fullpn_VolInfoFile);
1358 if (CreateCacheFile(fullpn_VolInfoFile))
1359 printf("%s: Can't create '%s'\n", rn, fullpn_VolInfoFile);
1361 if (missing_CellInfoFile) {
1363 printf("%s: Creating '%s'\n", rn, fullpn_CellInfoFile);
1364 if (CreateCacheFile(fullpn_CellInfoFile))
1365 printf("%s: Can't create '%s'\n", rn, fullpn_CellInfoFile);
1368 if (*vFilesFound < cacheFiles) {
1370 * We came up short on the number of data cache files found. Scan through the inode
1371 * list and create all missing files.
1373 for (vFileNum = 0; vFileNum < cacheFiles; vFileNum++)
1374 if (pathname_for_V[vFileNum] == (AFSD_INO_T) 0) {
1375 sprintf(vFileNumber, "%d", vFileNum);
1377 printf("%s: Creating '%s'\n", rn, fullpn_VFile);
1378 if (CreateCacheFile(fullpn_VFile))
1379 printf("%s: Can't create '%s'\n", rn, fullpn_VFile);
1384 * Close the directory, return success.
1387 printf("%s: Closing cache directory.\n", rn);
1393 ConfigCell(register struct afsconf_cell *aci, void *arock,
1394 struct afsconf_dir *adir)
1396 register int isHomeCell;
1398 afs_int32 cellFlags = 0;
1399 afs_int32 hosts[MAXHOSTSPERCELL];
1401 /* figure out if this is the home cell */
1402 isHomeCell = (strcmp(aci->name, afs_LclCellName) == 0);
1404 cellFlags = 2; /* not home, suid is forbidden */
1406 /* build address list */
1407 for (i = 0; i < MAXHOSTSPERCELL; i++)
1408 memcpy(&hosts[i], &aci->hostAddr[i].sin_addr, sizeof(afs_int32));
1410 if (aci->linkedCell)
1411 cellFlags |= 4; /* Flag that linkedCell arg exists,
1412 * for upwards compatibility */
1414 /* configure one cell */
1415 call_syscall(AFSCALL_CALL, AFSOP_ADDCELL2, (long)hosts, /* server addresses */
1416 (long)aci->name, /* cell name */
1417 (long)cellFlags, /* is this the home cell? */
1418 (long)aci->linkedCell); /* Linked cell, if any */
1423 ConfigCellAlias(struct afsconf_cellalias *aca, void *arock, struct afsconf_dir *adir)
1425 call_syscall(AFSOP_ADDCELLALIAS, (long)aca->aliasName,
1426 (long)aca->realName, 0, 0, 0);
1431 * Set the UDP port number RX uses for UDP datagrams
1434 uafs_SetRxPort(int port)
1436 usr_assert(usr_rx_port == 0);
1442 * Initialize the user space client.
1445 uafs_Init(char *rn, char *mountDirParam, char *confDirParam,
1446 char *cacheBaseDirParam, int cacheBlocksParam, int cacheFilesParam,
1447 int cacheStatEntriesParam, int dCacheSizeParam, int vCacheSizeParam,
1448 int chunkSizeParam, int closeSynchParam, int debugParam,
1449 int nDaemonsParam, int cacheFlagsParam, char *logFile)
1452 struct usr_proc *procp;
1453 struct usr_ucred *ucredp;
1456 int currVFile; /* Current AFS cache file number */
1457 int lookupResult; /* Result of GetLocalCellName() */
1458 int cacheIteration; /* cache verification loop counter */
1459 int vFilesFound; /* Num data cache files found in sweep */
1461 afs_int32 vfs1_type = -1;
1462 struct afs_ioctl iob;
1466 afs_int32 buffer[MAXIPADDRS];
1467 afs_int32 maskbuffer[MAXIPADDRS];
1468 afs_int32 mtubuffer[MAXIPADDRS];
1471 * Use the thread specific data to implement the user structure
1473 usr_keycreate(&afs_global_u_key, free);
1476 * Initialize the global ucred structure
1478 afs_global_ucredp = (struct usr_ucred *)
1479 afs_osi_Alloc(sizeof(struct usr_ucred));
1480 usr_assert(afs_global_ucredp != NULL);
1481 afs_global_ucredp->cr_ref = 1;
1482 afs_global_ucredp->cr_uid = geteuid();
1483 afs_global_ucredp->cr_gid = getegid();
1484 afs_global_ucredp->cr_ruid = getuid();
1485 afs_global_ucredp->cr_rgid = getgid();
1486 afs_global_ucredp->cr_suid = afs_global_ucredp->cr_ruid;
1487 afs_global_ucredp->cr_sgid = afs_global_ucredp->cr_rgid;
1488 st = getgroups(NGROUPS, &afs_global_ucredp->cr_groups[0]);
1489 usr_assert(st >= 0);
1490 afs_global_ucredp->cr_ngroups = (unsigned long)st;
1491 for (i = st; i < NGROUPS; i++) {
1492 afs_global_ucredp->cr_groups[i] = NOGROUP;
1496 * Initialize the global process structure
1498 afs_global_procp = (struct usr_proc *)
1499 afs_osi_Alloc(sizeof(struct usr_proc));
1500 usr_assert(afs_global_procp != NULL);
1501 afs_global_procp->p_pid = getpid();
1502 afs_global_procp->p_ppid = (pid_t) 1;
1503 afs_global_procp->p_ucred = afs_global_ucredp;
1506 * Initialize the AFS mount point, default is '/afs'.
1507 * Strip duplicate/trailing slashes from mount point string.
1508 * afs_mountDirLen is set to strlen(afs_mountDir).
1510 if (mountDirParam) {
1511 sprintf(tbuffer, "%s", mountDirParam);
1513 sprintf(tbuffer, "afs");
1515 afs_mountDir[0] = '/';
1516 afs_mountDirLen = 1;
1517 for (lastchar = '/', p = &tbuffer[0]; *p != '\0'; p++) {
1518 if (lastchar != '/' || *p != '/') {
1519 afs_mountDir[afs_mountDirLen++] = lastchar = *p;
1522 if (lastchar == '/' && afs_mountDirLen > 1)
1524 afs_mountDir[afs_mountDirLen] = '\0';
1525 usr_assert(afs_mountDirLen > 1);
1528 * Initialize cache parameters using the input arguments
1531 cacheBlocks = cacheBlocksParam;
1532 if (cacheFilesParam != 0) {
1533 cacheFiles = cacheFilesParam;
1535 cacheFiles = cacheBlocks / 10;
1537 if (cacheStatEntriesParam != 0) {
1538 cacheStatEntries = cacheStatEntriesParam;
1540 strcpy(cacheBaseDir, cacheBaseDirParam);
1541 if (nDaemons != 0) {
1542 nDaemons = nDaemonsParam;
1546 afsd_verbose = debugParam;
1547 afsd_debug = debugParam;
1548 chunkSize = chunkSizeParam;
1549 if (dCacheSizeParam != 0) {
1550 dCacheSize = dCacheSizeParam;
1552 dCacheSize = cacheFiles / 2;
1554 if (vCacheSizeParam != 0) {
1555 vCacheSize = vCacheSizeParam;
1557 strcpy(confDir, confDirParam);
1558 afsd_CloseSynch = closeSynchParam;
1559 if (cacheFlagsParam >= 0) {
1560 cacheFlags = cacheFlagsParam;
1562 if (cacheFlags & AFSCALL_INIT_MEMCACHE) {
1563 cacheFiles = dCacheSize;
1566 sprintf(fullpn_CacheInfo, "%s/%s", confDir, CACHEINFOFILE);
1567 if (logFile == NULL) {
1568 sprintf(fullpn_AFSLogFile, "%s/%s", confDir, AFSLOGFILE);
1570 strcpy(fullpn_AFSLogFile, logFile);
1573 printf("\n%s: Initializing user space AFS client\n\n", rn);
1574 printf(" mountDir: %s\n", afs_mountDir);
1575 printf(" confDir: %s\n", confDir);
1576 printf(" cacheBaseDir: %s\n", cacheBaseDir);
1577 printf(" cacheBlocks: %d\n", cacheBlocks);
1578 printf(" cacheFiles: %d\n", cacheFiles);
1579 printf(" cacheStatEntries: %d\n", cacheStatEntries);
1580 printf(" dCacheSize: %d\n", dCacheSize);
1581 printf(" vCacheSize: %d\n", vCacheSize);
1582 printf(" chunkSize: %d\n", chunkSize);
1583 printf(" afsd_CloseSynch: %d\n", afsd_CloseSynch);
1584 printf(" afsd_debug/verbose: %d/%d\n", afsd_debug, afsd_verbose);
1585 printf(" nDaemons: %d\n", nDaemons);
1586 printf(" cacheFlags: %d\n", cacheFlags);
1587 printf(" logFile: %s\n", fullpn_AFSLogFile);
1592 * Initialize the AFS client
1597 * Pull out all the configuration info for the workstation's AFS cache and
1598 * the cellular community we're willing to let our users see.
1600 afs_cdir = afsconf_Open(confDir);
1602 printf("afsd: some file missing or bad in %s\n", confDir);
1607 afsconf_GetLocalCell(afs_cdir, afs_LclCellName,
1608 sizeof(afs_LclCellName));
1610 printf("%s: Can't get my home cell name! [Error is %d]\n", rn,
1614 printf("%s: My home cell is '%s'\n", rn, afs_LclCellName);
1618 * Set the primary cell name.
1620 call_syscall(AFSOP_SET_THISCELL, (long)afs_LclCellName, 0, 0, 0, 0);
1622 if ((logfd = fopen(fullpn_AFSLogFile, "r+")) == 0) {
1624 printf("%s: Creating '%s'\n", rn, fullpn_AFSLogFile);
1625 if (CreateCacheFile(fullpn_AFSLogFile)) {
1627 ("%s: Can't create '%s' (You may want to use the -logfile option)\n",
1628 rn, fullpn_AFSLogFile);
1635 * Create and zero the pathname table for the desired cache files.
1637 pathname_for_V = (char **)afs_osi_Alloc(cacheFiles * sizeof(char *));
1638 if (pathname_for_V == NULL) {
1639 printf("%s: malloc() failed for cache file table with %d entries.\n",
1643 memset(pathname_for_V, 0, (cacheFiles * sizeof(char *)));
1645 printf("%s: %d pathname_for_V entries at 0x%x, %d bytes\n", rn,
1646 cacheFiles, pathname_for_V, (cacheFiles * sizeof(AFSD_INO_T)));
1649 * Set up all the pathnames we'll need for later.
1651 sprintf(fullpn_DCacheFile, "%s/%s", cacheBaseDir, DCACHEFILE);
1652 sprintf(fullpn_VolInfoFile, "%s/%s", cacheBaseDir, VOLINFOFILE);
1653 sprintf(fullpn_CellInfoFile, "%s/%s", cacheBaseDir, CELLINFOFILE);
1654 sprintf(fullpn_VFile, "%s/V", cacheBaseDir);
1655 vFileNumber = fullpn_VFile + strlen(fullpn_VFile);
1658 * Start the RX listener.
1661 printf("%s: Calling AFSOP_RXLISTENER_DAEMON\n", rn);
1662 fork_syscall(AFSCALL_CALL, AFSOP_RXLISTENER_DAEMON, FALSE, FALSE, FALSE);
1665 * Start the RX event handler.
1668 printf("%s: Calling AFSOP_RXEVENT_DAEMON\n", rn);
1669 fork_syscall(AFSCALL_CALL, AFSOP_RXEVENT_DAEMON, FALSE);
1672 * Set up all the kernel processes needed for AFS.
1675 /* initialize AFS callback interface */
1677 /* parse multihomed address files */
1679 st = parseNetFiles((afs_uint32*)buffer,(afs_uint32*) maskbuffer, (afs_uint32*)mtubuffer, MAXIPADDRS, reason,
1680 AFSDIR_CLIENT_NETINFO_FILEPATH,
1681 AFSDIR_CLIENT_NETRESTRICT_FILEPATH);
1683 call_syscall(AFSCALL_CALL, AFSOP_ADVISEADDR, st,
1684 (long)(&buffer[0]), (long)(&maskbuffer[0]),
1685 (long)(&mtubuffer[0]));
1687 printf("ADVISEADDR: Error in specifying interface addresses:%s\n",
1694 printf("%s: Forking rx callback listener.\n", rn);
1696 if (preallocs < cacheStatEntries + 50)
1697 preallocs = cacheStatEntries + 50;
1698 fork_syscall(AFSCALL_CALL, AFSOP_START_RXCALLBACK, preallocs);
1701 printf("%s: Initializing AFS daemon.\n", rn);
1702 call_syscall(AFSCALL_CALL, AFSOP_BASIC_INIT, 1, 0, 0, 0);
1705 * Tell the kernel some basic information about the workstation's cache.
1708 printf("%s: Calling AFSOP_CACHEINIT: %d stat cache entries,"
1709 " %d optimum cache files, %d blocks in the cache,"
1710 " flags = 0x%x, dcache entries %d\n", rn, cacheStatEntries,
1711 cacheFiles, cacheBlocks, cacheFlags, dCacheSize);
1712 memset(&cparams, 0, sizeof(cparams));
1713 cparams.cacheScaches = cacheStatEntries;
1714 cparams.cacheFiles = cacheFiles;
1715 cparams.cacheBlocks = cacheBlocks;
1716 cparams.cacheDcaches = dCacheSize;
1717 cparams.cacheVolumes = vCacheSize;
1718 cparams.chunkSize = chunkSize;
1719 cparams.setTimeFlag = FALSE;
1720 cparams.memCacheFlag = cacheFlags;
1721 call_syscall(AFSCALL_CALL, AFSOP_CACHEINIT, (long)&cparams, 0, 0, 0);
1722 if (afsd_CloseSynch)
1723 call_syscall(AFSCALL_CALL, AFSOP_CLOSEWAIT, 0, 0, 0, 0);
1726 * Sweep the workstation AFS cache directory, remembering the inodes of
1727 * valid files and deleting extraneous files. Keep sweeping until we
1728 * have the right number of data cache files or we've swept too many
1732 printf("%s: Sweeping workstation's AFS cache directory.\n", rn);
1734 /* Memory-cache based system doesn't need any of this */
1735 if (!(cacheFlags & AFSCALL_INIT_MEMCACHE)) {
1738 if (SweepAFSCache(&vFilesFound)) {
1739 printf("%s: Error on sweep %d of workstation AFS cache \
1740 directory.\n", rn, cacheIteration);
1745 ("%s: %d out of %d data cache files found in sweep %d.\n",
1746 rn, vFilesFound, cacheFiles, cacheIteration);
1747 } while ((vFilesFound < cacheFiles)
1748 && (cacheIteration < MAX_CACHE_LOOPS));
1749 } else if (afsd_verbose)
1750 printf("%s: Using memory cache, not swept\n", rn);
1753 * Pass the kernel the name of the workstation cache file holding the
1757 printf("%s: Calling AFSOP_CACHEINFO: dcache file is '%s'\n", rn,
1759 /* once again, meaningless for a memory-based cache. */
1760 if (!(cacheFlags & AFSCALL_INIT_MEMCACHE))
1761 call_syscall(AFSCALL_CALL, AFSOP_CACHEINFO, (long)fullpn_DCacheFile,
1764 call_syscall(AFSCALL_CALL, AFSOP_CELLINFO, (long)fullpn_CellInfoFile, 0,
1768 * Pass the kernel the name of the workstation cache file holding the
1769 * volume information.
1772 printf("%s: Calling AFSOP_VOLUMEINFO: volume info file is '%s'\n", rn,
1773 fullpn_VolInfoFile);
1774 call_syscall(AFSCALL_CALL, AFSOP_VOLUMEINFO, (long)fullpn_VolInfoFile, 0,
1778 * Pass the kernel the name of the afs logging file holding the volume
1782 printf("%s: Calling AFSOP_AFSLOG: volume info file is '%s'\n", rn,
1784 if (!(cacheFlags & AFSCALL_INIT_MEMCACHE)) /* ... nor this ... */
1785 call_syscall(AFSCALL_CALL, AFSOP_AFSLOG, (long)fullpn_AFSLogFile, 0,
1789 * Tell the kernel about each cell in the configuration.
1791 afsconf_CellApply(afs_cdir, ConfigCell, NULL);
1792 afsconf_CellAliasApply(afs_cdir, ConfigCellAlias, NULL);
1795 printf("%s: Forking AFS daemon.\n", rn);
1796 fork_syscall(AFSCALL_CALL, AFSOP_START_AFS);
1799 printf("%s: Forking check server daemon.\n", rn);
1800 fork_syscall(AFSCALL_CALL, AFSOP_START_CS);
1803 printf("%s: Forking %d background daemons.\n", rn, nDaemons);
1804 for (i = 0; i < nDaemons; i++) {
1805 fork_syscall(AFSCALL_CALL, AFSOP_START_BKG);
1809 printf("%s: Calling AFSOP_ROOTVOLUME with '%s'\n", rn, rootVolume);
1810 call_syscall(AFSCALL_CALL, AFSOP_ROOTVOLUME, (long)rootVolume, 0, 0, 0);
1813 * Give the kernel the names of the AFS files cached on the workstation's
1818 ("%s: Calling AFSOP_CACHEFILES for each of the %d files in '%s'\n",
1819 rn, cacheFiles, cacheBaseDir);
1820 if (!(cacheFlags & AFSCALL_INIT_MEMCACHE)) /* ... and again ... */
1821 for (currVFile = 0; currVFile < cacheFiles; currVFile++) {
1822 call_syscall(AFSCALL_CALL, AFSOP_CACHEFILE,
1823 (long)pathname_for_V[currVFile], 0, 0, 0);
1826 #ifndef NETSCAPE_NSAPI
1828 * Copy our tokens from the kernel to the user space client
1830 for (i = 0; i < 200; i++) {
1832 * Get the i'th token from the kernel
1834 memset((void *)&tbuffer[0], 0, sizeof(tbuffer));
1835 memcpy((void *)&tbuffer[0], (void *)&i, sizeof(int));
1837 iob.in_size = sizeof(int);
1839 iob.out_size = sizeof(tbuffer);
1841 #if defined(AFS_USR_SUN5_ENV) || defined(AFS_USR_OSF_ENV) || defined(AFS_USR_HPUX_ENV) || defined(AFS_USR_LINUX22_ENV) || defined(AFS_USR_DARWIN_ENV) || defined(AFS_USR_FBSD_ENV)
1842 rc = syscall(AFS_SYSCALL, AFSCALL_PIOCTL, 0, _VICEIOCTL(8), &iob, 0);
1843 #elif defined(AFS_USR_SGI_ENV)
1844 rc = syscall(AFS_PIOCTL, 0, _VICEIOCTL(8), &iob, 0);
1845 #else /* AFS_USR_AIX_ENV */
1846 rc = lpioctl(0, _VICEIOCTL(8), &iob, 0);
1849 usr_assert(errno == EDOM || errno == ENOSYS);
1854 * Now pass the token into the user space kernel
1856 rc = uafs_SetTokens(tbuffer, iob.out_size);
1857 usr_assert(rc == 0);
1859 #endif /* !NETSCAPE_NSAPI */
1862 * All the necessary info has been passed into the kernel to run an AFS
1863 * system. Give the kernel our go-ahead.
1866 printf("%s: Calling AFSOP_GO\n", rn);
1867 call_syscall(AFSCALL_CALL, AFSOP_GO, FALSE, 0, 0, 0);
1870 * At this point, we have finished passing the kernel all the info
1871 * it needs to set up the AFS. Mount the AFS root.
1873 printf("%s: All AFS daemons started.\n", rn);
1876 printf("%s: Forking trunc-cache daemon.\n", rn);
1877 fork_syscall(AFSCALL_CALL, AFSOP_START_TRUNCDAEMON);
1880 * Mount the AFS filesystem
1883 rc = afs_mount(&afs_RootVfs, NULL, NULL);
1884 usr_assert(rc == 0);
1885 rc = afs_root(&afs_RootVfs, &afs_RootVnode);
1886 usr_assert(rc == 0);
1890 * initialize the current directory to the AFS root
1892 afs_CurrentDir = afs_RootVnode;
1893 VN_HOLD(afs_CurrentDir);
1906 VN_RELE(afs_CurrentDir);
1907 rc = afs_unmount(&afs_RootVfs);
1908 usr_assert(rc == 0);
1915 * Donate the current thread to the RX server pool.
1918 uafs_RxServerProc(void)
1922 struct rx_call *newcall = NULL;
1924 rxi_MorePackets(2); /* alloc more packets */
1925 threadID = rxi_availProcs++;
1928 sock = OSI_NULLSOCKET;
1929 rxi_ServerProc(threadID, newcall, &sock);
1930 if (sock == OSI_NULLSOCKET) {
1935 rxi_ListenerProc(sock, &threadID, &newcall);
1936 /* assert(threadID != -1); */
1937 /* assert(newcall != NULL); */
1941 struct syscallThreadArgs {
1950 #ifdef NETSCAPE_NSAPI
1952 syscallThread(void *argp)
1953 #else /* NETSCAPE_NSAPI */
1955 syscallThread(void *argp)
1956 #endif /* NETSCAPE_NSAPI */
1959 struct usr_ucred *crp;
1960 struct syscallThreadArgs *sysArgsP = (struct syscallThreadArgs *)argp;
1963 * AFS daemons run authenticated
1965 u.u_viceid = getuid();
1967 crp->cr_uid = getuid();
1968 crp->cr_ruid = getuid();
1969 crp->cr_suid = getuid();
1970 crp->cr_groups[0] = getgid();
1971 crp->cr_ngroups = 1;
1972 for (i = 1; i < NGROUPS; i++) {
1973 crp->cr_groups[i] = NOGROUP;
1976 call_syscall(sysArgsP->syscall, sysArgsP->afscall, sysArgsP->param1,
1977 sysArgsP->param2, sysArgsP->param3, sysArgsP->param4);
1979 afs_osi_Free(argp, -1);
1982 fork_syscall(syscall, afscall, param1, param2, param3, param4)
1983 long syscall, afscall, param1, param2, param3, param4;
1986 struct syscallThreadArgs *sysArgsP;
1988 sysArgsP = (struct syscallThreadArgs *)
1989 afs_osi_Alloc(sizeof(struct syscallThreadArgs));
1990 usr_assert(sysArgsP != NULL);
1991 sysArgsP->syscall = syscall;
1992 sysArgsP->afscall = afscall;
1993 sysArgsP->param1 = param1;
1994 sysArgsP->param2 = param2;
1995 sysArgsP->param3 = param3;
1996 sysArgsP->param4 = param4;
1998 usr_thread_create(&tid, syscallThread, sysArgsP);
1999 usr_thread_detach(tid);
2002 call_syscall(syscall, afscall, param1, param2, param3, param4)
2003 long syscall, afscall, param1, param2, param3, param4;
2015 a.syscall = syscall;
2016 a.afscall = afscall;
2023 u.u_ap = (char *)&a;
2025 code = Afs_syscall();
2030 uafs_SetTokens(char *tbuffer, int tlen)
2033 struct afs_ioctl iob;
2038 iob.out = &outbuf[0];
2039 iob.out_size = sizeof(outbuf);
2040 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(3), (long)&iob, 0, 0);
2049 uafs_RPCStatsEnableProc(void)
2052 struct afs_ioctl iob;
2055 flag = AFSCALL_RXSTATS_ENABLE;
2056 iob.in = (char *)&flag;
2057 iob.in_size = sizeof(afs_int32);
2060 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(53), (long)&iob, 0, 0);
2069 uafs_RPCStatsDisableProc(void)
2072 struct afs_ioctl iob;
2075 flag = AFSCALL_RXSTATS_DISABLE;
2076 iob.in = (char *)&flag;
2077 iob.in_size = sizeof(afs_int32);
2080 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(53), (long)&iob, 0, 0);
2089 uafs_RPCStatsClearProc(void)
2092 struct afs_ioctl iob;
2095 flag = AFSCALL_RXSTATS_CLEAR;
2096 iob.in = (char *)&flag;
2097 iob.in_size = sizeof(afs_int32);
2100 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(53), (long)&iob, 0, 0);
2109 uafs_RPCStatsEnablePeer(void)
2112 struct afs_ioctl iob;
2115 flag = AFSCALL_RXSTATS_ENABLE;
2116 iob.in = (char *)&flag;
2117 iob.in_size = sizeof(afs_int32);
2120 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(54), (long)&iob, 0, 0);
2129 uafs_RPCStatsDisablePeer(void)
2132 struct afs_ioctl iob;
2135 flag = AFSCALL_RXSTATS_DISABLE;
2136 iob.in = (char *)&flag;
2137 iob.in_size = sizeof(afs_int32);
2140 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(54), (long)&iob, 0, 0);
2149 uafs_RPCStatsClearPeer(void)
2152 struct afs_ioctl iob;
2155 flag = AFSCALL_RXSTATS_CLEAR;
2156 iob.in = (char *)&flag;
2157 iob.in_size = sizeof(afs_int32);
2160 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(54), (long)&iob, 0, 0);
2169 * Lookup a file or directory given its path.
2170 * Call VN_HOLD on the output vnode if successful.
2171 * Returns zero on success, error code on failure.
2173 * Note: Caller must hold the AFS global lock.
2176 uafs_LookupName(char *path, struct usr_vnode *parentVp,
2177 struct usr_vnode **vpp, int follow, int no_eval_mtpt)
2181 struct usr_vnode *vp;
2182 struct usr_vnode *nextVp;
2183 struct usr_vnode *linkVp;
2191 * Absolute paths must start with the AFS mount point.
2193 if (path[0] != '/') {
2196 path = uafs_afsPathName(path);
2204 * Loop through the path looking for the new directory
2206 tmpPath = afs_osi_Alloc(strlen(path) + 1);
2207 usr_assert(tmpPath != NULL);
2208 strcpy(tmpPath, path);
2211 while (pathP != NULL && *pathP != '\0') {
2212 usr_assert(*pathP != '/');
2215 * terminate the current component and skip over slashes
2217 nextPathP = afs_strchr(pathP, '/');
2218 if (nextPathP != NULL) {
2219 while (*nextPathP == '/') {
2220 *(nextPathP++) = '\0';
2225 * Don't call afs_lookup on non-directories
2227 if (vp->v_type != VDIR) {
2229 afs_osi_Free(tmpPath, strlen(path) + 1);
2233 if (vp == afs_RootVnode && strcmp(pathP, "..") == 0) {
2235 * The AFS root is its own parent
2237 nextVp = afs_RootVnode;
2240 * We need execute permission to search a directory
2242 code = afs_access(VTOAFS(vp), VEXEC, u.u_cred);
2245 afs_osi_Free(tmpPath, strlen(path) + 1);
2250 * lookup the next component in the path, we can release the
2251 * subdirectory since we hold the global lock
2254 #ifdef AFS_WEB_ENHANCEMENTS
2255 if ((nextPathP != NULL && *nextPathP != '\0') || !no_eval_mtpt)
2256 code = afs_lookup(vp, pathP, &nextVp, u.u_cred, 0);
2259 afs_lookup(vp, pathP, &nextVp, u.u_cred,
2262 code = afs_lookup(vp, pathP, &nextVp, u.u_cred, 0);
2263 #endif /* AFS_WEB_ENHANCEMENTS */
2266 afs_osi_Free(tmpPath, strlen(path) + 1);
2272 * Follow symbolic links for parent directories and
2273 * for leaves when the follow flag is set.
2275 if ((nextPathP != NULL && *nextPathP != '\0') || follow) {
2277 while (nextVp->v_type == VLNK) {
2278 if (++linkCount > MAX_OSI_LINKS) {
2281 afs_osi_Free(tmpPath, strlen(path) + 1);
2284 code = uafs_LookupLink(nextVp, vp, &linkVp);
2288 afs_osi_Free(tmpPath, strlen(path) + 1);
2302 * Special case, nextPathP is non-null if pathname ends in slash
2304 if (nextPathP != NULL && vp->v_type != VDIR) {
2306 afs_osi_Free(tmpPath, strlen(path) + 1);
2310 afs_osi_Free(tmpPath, strlen(path) + 1);
2316 * Lookup the target of a symbolic link
2317 * Call VN_HOLD on the output vnode if successful.
2318 * Returns zero on success, error code on failure.
2320 * Note: Caller must hold the AFS global lock.
2323 uafs_LookupLink(struct usr_vnode *vp, struct usr_vnode *parentVp,
2324 struct usr_vnode **vpp)
2329 struct usr_vnode *linkVp;
2331 struct iovec iov[1];
2335 pathP = afs_osi_Alloc(MAX_OSI_PATH + 1);
2336 usr_assert(pathP != NULL);
2339 * set up the uio buffer
2341 iov[0].iov_base = pathP;
2342 iov[0].iov_len = MAX_OSI_PATH + 1;
2343 uio.uio_iov = &iov[0];
2347 uio.uio_fmode = FREAD;
2348 uio.uio_resid = MAX_OSI_PATH + 1;
2351 * Read the link data
2353 code = afs_readlink(vp, &uio, u.u_cred);
2355 afs_osi_Free(pathP, MAX_OSI_PATH + 1);
2358 len = MAX_OSI_PATH + 1 - uio.uio_resid;
2362 * Find the target of the symbolic link
2364 code = uafs_LookupName(pathP, parentVp, &linkVp, 1, 0);
2366 afs_osi_Free(pathP, MAX_OSI_PATH + 1);
2370 afs_osi_Free(pathP, MAX_OSI_PATH + 1);
2376 * Lookup the parent of a file or directory given its path
2377 * Call VN_HOLD on the output vnode if successful.
2378 * Returns zero on success, error code on failure.
2380 * Note: Caller must hold the AFS global lock.
2383 uafs_LookupParent(char *path, struct usr_vnode **vpp)
2388 struct usr_vnode *parentP;
2393 * Absolute path names must start with the AFS mount point.
2396 pathP = uafs_afsPathName(path);
2397 if (pathP == NULL) {
2403 * Find the length of the parent path
2406 while (len > 0 && path[len - 1] == '/') {
2412 while (len > 0 && path[len - 1] != '/') {
2419 pathP = afs_osi_Alloc(len);
2420 usr_assert(pathP != NULL);
2421 memcpy(pathP, path, len - 1);
2422 pathP[len - 1] = '\0';
2425 * look up the parent
2427 code = uafs_LookupName(pathP, afs_CurrentDir, &parentP, 1, 0);
2428 afs_osi_Free(pathP, len);
2432 if (parentP->v_type != VDIR) {
2442 * Return a pointer to the first character in the last component
2446 uafs_LastPath(char *path)
2451 while (len > 0 && path[len - 1] == '/') {
2454 while (len > 0 && path[len - 1] != '/') {
2464 * Set the working directory.
2467 uafs_chdir(char *path)
2471 retval = uafs_chdir_r(path);
2477 uafs_chdir_r(char *path)
2482 code = uafs_LookupName(path, afs_CurrentDir, &dirP, 1, 0);
2487 if (dirP->v_type != VDIR) {
2492 VN_RELE(afs_CurrentDir);
2493 afs_CurrentDir = dirP;
2498 * Create a directory.
2501 uafs_mkdir(char *path, int mode)
2505 retval = uafs_mkdir_r(path, mode);
2511 uafs_mkdir_r(char *path, int mode)
2515 struct vnode *parentP;
2517 struct usr_vattr attrs;
2519 if (uafs_IsRoot(path)) {
2524 * Look up the parent directory.
2526 nameP = uafs_LastPath(path);
2527 if (nameP != NULL) {
2528 code = uafs_LookupParent(path, &parentP);
2534 parentP = afs_CurrentDir;
2540 * Make sure the directory has at least one character
2542 if (*nameP == '\0') {
2549 * Create the directory
2551 usr_vattr_null(&attrs);
2552 attrs.va_type = VREG;
2553 attrs.va_mode = mode;
2554 attrs.va_uid = u.u_cred->cr_uid;
2555 attrs.va_gid = u.u_cred->cr_gid;
2557 code = afs_mkdir(parentP, nameP, &attrs, &dirP, u.u_cred);
2568 * Return 1 if path is the AFS root, otherwise return 0
2571 uafs_IsRoot(char *path)
2573 while (*path == '/' && *(path + 1) == '/') {
2576 if (strncmp(path, afs_mountDir, afs_mountDirLen) != 0) {
2579 path += afs_mountDirLen;
2580 while (*path == '/') {
2583 if (*path != '\0') {
2591 * Note: file name may not end in a slash.
2594 uafs_open(char *path, int flags, int mode)
2598 retval = uafs_open_r(path, flags, mode);
2604 uafs_open_r(char *path, int flags, int mode)
2610 struct usr_vnode *fileP;
2611 struct usr_vnode *dirP;
2612 struct usr_vattr attrs;
2617 if (uafs_IsRoot(path)) {
2618 fileP = afs_RootVnode;
2622 * Look up the parent directory.
2624 nameP = uafs_LastPath(path);
2625 if (nameP != NULL) {
2626 code = uafs_LookupParent(path, &dirP);
2632 dirP = afs_CurrentDir;
2638 * Make sure the filename has at least one character
2640 if (*nameP == '\0') {
2647 * Get the VNODE for this file
2649 if (flags & O_CREAT) {
2650 usr_vattr_null(&attrs);
2651 attrs.va_type = VREG;
2652 attrs.va_mode = mode;
2653 attrs.va_uid = u.u_cred->cr_uid;
2654 attrs.va_gid = u.u_cred->cr_gid;
2655 if (flags & O_TRUNC) {
2661 afs_create(VTOAFS(dirP), nameP, &attrs,
2662 (flags & O_EXCL) ? usr_EXCL : usr_NONEXCL, mode,
2671 code = uafs_LookupName(nameP, dirP, &fileP, 1, 0);
2679 * Check whether we have access to this file
2682 if (flags & (O_RDONLY | O_RDWR)) {
2685 if (flags & (O_WRONLY | O_RDWR)) {
2689 fileMode = VREAD; /* since O_RDONLY is 0 */
2690 code = afs_access(VTOAFS(fileP), fileMode, u.u_cred);
2698 * Get the file attributes, all we need is the size
2700 code = afs_getattr(VTOAFS(fileP), &attrs, u.u_cred);
2710 * Setup the open flags
2713 if (flags & O_TRUNC) {
2714 openFlags |= FTRUNC;
2716 if (flags & O_APPEND) {
2717 openFlags |= FAPPEND;
2719 if (flags & O_SYNC) {
2722 if (flags & O_SYNC) {
2725 if (flags & (O_RDONLY | O_RDWR)) {
2728 if (flags & (O_WRONLY | O_RDWR)) {
2729 openFlags |= FWRITE;
2731 if ((openFlags & (FREAD | FWRITE)) == 0) {
2732 /* O_RDONLY is 0, so ... */
2737 * Truncate if necessary
2739 if ((flags & O_TRUNC) && (attrs.va_size != 0)) {
2740 usr_vattr_null(&attrs);
2742 code = afs_setattr(VTOAFS(fileP), &attrs, u.u_cred);
2754 code = afs_open(&vc, openFlags, u.u_cred);
2762 * Put the vnode pointer into the file table
2764 for (fd = 0; fd < MAX_OSI_FILES; fd++) {
2765 if (afs_FileTable[fd] == NULL) {
2766 afs_FileTable[fd] = fileP;
2767 afs_FileFlags[fd] = openFlags;
2768 if (flags & O_APPEND) {
2769 afs_FileOffsets[fd] = attrs.va_size;
2771 afs_FileOffsets[fd] = 0;
2776 if (fd == MAX_OSI_FILES) {
2789 uafs_creat(char *path, int mode)
2792 rc = uafs_open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
2797 uafs_creat_r(char *path, int mode)
2800 rc = uafs_open_r(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
2808 uafs_write(int fd, char *buf, int len)
2812 retval = uafs_write_r(fd, buf, len);
2818 uafs_write_r(int fd, char *buf, int len)
2822 struct iovec iov[1];
2823 struct usr_vnode *fileP;
2826 * Make sure this is an open file
2828 fileP = afs_FileTable[fd];
2829 if (fileP == NULL) {
2835 * set up the uio buffer
2837 iov[0].iov_base = buf;
2838 iov[0].iov_len = len;
2839 uio.uio_iov = &iov[0];
2841 uio.uio_offset = afs_FileOffsets[fd];
2843 uio.uio_fmode = FWRITE;
2844 uio.uio_resid = len;
2850 code = afs_write(VTOAFS(fileP), &uio, afs_FileFlags[fd], u.u_cred, 0);
2856 afs_FileOffsets[fd] = uio.uio_offset;
2857 return (len - uio.uio_resid);
2864 uafs_read(int fd, char *buf, int len)
2868 retval = uafs_read_r(fd, buf, len);
2874 uafs_read_r(int fd, char *buf, int len)
2878 struct iovec iov[1];
2879 struct usr_vnode *fileP;
2880 struct usr_buf *bufP;
2883 * Make sure this is an open file
2885 fileP = afs_FileTable[fd];
2886 if (fileP == NULL) {
2892 * set up the uio buffer
2894 iov[0].iov_base = buf;
2895 iov[0].iov_len = len;
2896 uio.uio_iov = &iov[0];
2898 uio.uio_offset = afs_FileOffsets[fd];
2900 uio.uio_fmode = FREAD;
2901 uio.uio_resid = len;
2906 code = afs_read(VTOAFS(fileP), &uio, u.u_cred, 0, &bufP, 0);
2912 afs_FileOffsets[fd] = uio.uio_offset;
2913 return (len - uio.uio_resid);
2917 * Copy the attributes of a file into a stat structure.
2919 * NOTE: Caller must hold the global AFS lock.
2922 uafs_GetAttr(struct usr_vnode *vp, struct stat *stats)
2925 struct usr_vattr attrs;
2930 * Get the attributes
2932 code = afs_getattr(VTOAFS(vp), &attrs, u.u_cred);
2938 * Copy the attributes, zero fields that aren't set
2940 memset((void *)stats, 0, sizeof(struct stat));
2942 stats->st_ino = attrs.va_nodeid;
2943 stats->st_mode = attrs.va_mode;
2944 stats->st_nlink = attrs.va_nlink;
2945 stats->st_uid = attrs.va_uid;
2946 stats->st_gid = attrs.va_gid;
2947 stats->st_rdev = attrs.va_rdev;
2948 stats->st_size = attrs.va_size;
2949 stats->st_atime = attrs.va_atime.tv_sec;
2950 stats->st_mtime = attrs.va_mtime.tv_sec;
2951 stats->st_ctime = attrs.va_ctime.tv_sec;
2952 stats->st_blksize = attrs.va_blocksize;
2953 stats->st_blocks = attrs.va_blocks;
2959 * Get the attributes of a file, do follow links
2962 uafs_stat(char *path, struct stat *buf)
2966 retval = uafs_stat_r(path, buf);
2972 uafs_stat_r(char *path, struct stat *buf)
2977 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
2982 code = uafs_GetAttr(vp, buf);
2992 * Get the attributes of a file, don't follow links
2995 uafs_lstat(char *path, struct stat *buf)
2999 retval = uafs_lstat_r(path, buf);
3005 uafs_lstat_r(char *path, struct stat *buf)
3010 code = uafs_LookupName(path, afs_CurrentDir, &vp, 0, 0);
3015 code = uafs_GetAttr(vp, buf);
3025 * Get the attributes of an open file
3028 uafs_fstat(int fd, struct stat *buf)
3032 retval = uafs_fstat_r(fd, buf);
3038 uafs_fstat_r(int fd, struct stat *buf)
3043 vp = afs_FileTable[fd];
3048 code = uafs_GetAttr(vp, buf);
3057 * change the permissions on a file
3060 uafs_chmod(char *path, int mode)
3064 retval = uafs_chmod_r(path, mode);
3070 uafs_chmod_r(char *path, int mode)
3074 struct usr_vattr attrs;
3076 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
3081 usr_vattr_null(&attrs);
3082 attrs.va_mode = mode;
3083 code = afs_setattr(VTOAFS(vp), &attrs, u.u_cred);
3093 * change the permissions on an open file
3096 uafs_fchmod(int fd, int mode)
3100 retval = uafs_fchmod_r(fd, mode);
3106 uafs_fchmod_r(int fd, int mode)
3110 struct usr_vattr attrs;
3112 vp = afs_FileTable[fd];
3117 usr_vattr_null(&attrs);
3118 attrs.va_mode = mode;
3119 code = afs_setattr(VTOAFS(vp), &attrs, u.u_cred);
3131 uafs_truncate(char *path, int length)
3135 retval = uafs_truncate_r(path, length);
3141 uafs_truncate_r(char *path, int length)
3145 struct usr_vattr attrs;
3147 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
3152 usr_vattr_null(&attrs);
3153 attrs.va_size = length;
3154 code = afs_setattr(VTOAFS(vp), &attrs, u.u_cred);
3164 * truncate an open file
3167 uafs_ftruncate(int fd, int length)
3171 retval = uafs_ftruncate_r(fd, length);
3177 uafs_ftruncate_r(int fd, int length)
3181 struct usr_vattr attrs;
3183 vp = afs_FileTable[fd];
3188 usr_vattr_null(&attrs);
3189 attrs.va_size = length;
3190 code = afs_setattr(VTOAFS(vp), &attrs, u.u_cred);
3199 * set the read/write file pointer of an open file
3202 uafs_lseek(int fd, int offset, int whence)
3206 retval = uafs_lseek_r(fd, offset, whence);
3212 uafs_lseek_r(int fd, int offset, int whence)
3216 struct usr_vattr attrs;
3217 struct usr_vnode *vp;
3219 vp = afs_FileTable[fd];
3226 newpos = afs_FileOffsets[fd] + offset;
3232 code = afs_getattr(VTOAFS(vp), &attrs, u.u_cred);
3237 newpos = attrs.va_size + offset;
3247 afs_FileOffsets[fd] = newpos;
3259 retval = uafs_fsync_r(fd);
3265 uafs_fsync_r(int fd)
3268 struct usr_vnode *fileP;
3271 fileP = afs_FileTable[fd];
3272 if (fileP == NULL) {
3277 code = afs_fsync(fileP, u.u_cred);
3294 retval = uafs_close_r(fd);
3300 uafs_close_r(int fd)
3303 struct usr_vnode *fileP;
3305 fileP = afs_FileTable[fd];
3306 if (fileP == NULL) {
3310 afs_FileTable[fd] = NULL;
3312 code = afs_close(fileP, afs_FileFlags[fd], u.u_cred);
3323 * Create a hard link from the source to the target
3324 * Note: file names may not end in a slash.
3327 uafs_link(char *existing, char *new)
3331 retval = uafs_link_r(existing, new);
3337 uafs_link_r(char *existing, char *new)
3340 struct usr_vnode *existP;
3341 struct usr_vnode *dirP;
3344 if (uafs_IsRoot(new)) {
3349 * Look up the existing node.
3351 code = uafs_LookupName(existing, afs_CurrentDir, &existP, 1, 0);
3358 * Look up the parent directory.
3360 nameP = uafs_LastPath(new);
3361 if (nameP != NULL) {
3362 code = uafs_LookupParent(new, &dirP);
3369 dirP = afs_CurrentDir;
3375 * Make sure the filename has at least one character
3377 if (*nameP == '\0') {
3387 code = afs_link(existP, dirP, nameP, u.u_cred);
3398 * Create a symbolic link from the source to the target
3399 * Note: file names may not end in a slash.
3402 uafs_symlink(char *target, char *source)
3406 retval = uafs_symlink_r(target, source);
3412 uafs_symlink_r(char *target, char *source)
3415 struct usr_vnode *dirP;
3416 struct usr_vattr attrs;
3419 if (uafs_IsRoot(source)) {
3424 * Look up the parent directory.
3426 nameP = uafs_LastPath(source);
3427 if (nameP != NULL) {
3428 code = uafs_LookupParent(source, &dirP);
3434 dirP = afs_CurrentDir;
3440 * Make sure the filename has at least one character
3442 if (*nameP == '\0') {
3451 usr_vattr_null(&attrs);
3452 attrs.va_type = VLNK;
3453 attrs.va_mode = 0777;
3454 attrs.va_uid = u.u_cred->cr_uid;
3455 attrs.va_gid = u.u_cred->cr_gid;
3456 code = afs_symlink(dirP, nameP, &attrs, target, u.u_cred);
3466 * Read a symbolic link into the buffer
3469 uafs_readlink(char *path, char *buf, int len)
3473 retval = uafs_readlink_r(path, buf, len);
3479 uafs_readlink_r(char *path, char *buf, int len)
3482 struct usr_vnode *vp;
3484 struct iovec iov[1];
3486 code = uafs_LookupName(path, afs_CurrentDir, &vp, 0, 0);
3492 if (vp->v_type != VLNK) {
3499 * set up the uio buffer
3501 iov[0].iov_base = buf;
3502 iov[0].iov_len = len;
3503 uio.uio_iov = &iov[0];
3507 uio.uio_fmode = FREAD;
3508 uio.uio_resid = len;
3513 code = afs_readlink(vp, &uio, u.u_cred);
3521 * return the number of bytes read
3523 return (len - uio.uio_resid);
3527 * Remove a file (or directory)
3528 * Note: file name may not end in a slash.
3531 uafs_unlink(char *path)
3535 retval = uafs_unlink_r(path);
3541 uafs_unlink_r(char *path)
3545 struct usr_vnode *fileP;
3546 struct usr_vnode *dirP;
3549 if (uafs_IsRoot(path)) {
3554 * Look up the parent directory.
3556 nameP = uafs_LastPath(path);
3557 if (nameP != NULL) {
3558 code = uafs_LookupParent(path, &dirP);
3564 dirP = afs_CurrentDir;
3570 * Make sure the filename has at least one character
3572 if (*nameP == '\0') {
3581 code = afs_remove(dirP, nameP, u.u_cred);
3592 * Rename a file (or directory)
3595 uafs_rename(char *old, char *new)
3599 retval = uafs_rename_r(old, new);
3605 uafs_rename_r(char *old, char *new)
3610 struct usr_vnode *odirP;
3611 struct usr_vnode *ndirP;
3613 if (uafs_IsRoot(new)) {
3618 * Look up the parent directories.
3620 onameP = uafs_LastPath(old);
3621 if (onameP != NULL) {
3622 code = uafs_LookupParent(old, &odirP);
3628 odirP = afs_CurrentDir;
3632 nnameP = uafs_LastPath(new);
3633 if (nnameP != NULL) {
3634 code = uafs_LookupParent(new, &ndirP);
3640 ndirP = afs_CurrentDir;
3646 * Make sure the filename has at least one character
3648 if (*onameP == '\0' || *nnameP == '\0') {
3658 code = afs_rename(odirP, onameP, ndirP, nnameP, u.u_cred);
3670 * Remove a or directory
3671 * Note: file name may not end in a slash.
3674 uafs_rmdir(char *path)
3678 retval = uafs_rmdir_r(path);
3684 uafs_rmdir_r(char *path)
3688 struct usr_vnode *fileP;
3689 struct usr_vnode *dirP;
3692 if (uafs_IsRoot(path)) {
3697 * Look up the parent directory.
3699 nameP = uafs_LastPath(path);
3700 if (nameP != NULL) {
3701 code = uafs_LookupParent(path, &dirP);
3707 dirP = afs_CurrentDir;
3713 * Make sure the directory name has at least one character
3715 if (*nameP == '\0') {
3722 * Remove the directory
3724 code = afs_rmdir(dirP, nameP, u.u_cred);
3735 * Flush a file from the AFS cache
3738 uafs_FlushFile(char *path)
3741 struct afs_ioctl iob;
3749 call_syscall(AFSCALL_PIOCTL, (long)path, _VICEIOCTL(6), (long)&iob, 0,
3760 uafs_FlushFile_r(char *path)
3764 retval = uafs_FlushFile(path);
3773 uafs_opendir(char *path)
3777 retval = uafs_opendir_r(path);
3783 uafs_opendir_r(char *path)
3786 struct usr_vnode *fileP;
3790 * Open the directory for reading
3792 fd = uafs_open_r(path, O_RDONLY, 0);
3797 fileP = afs_FileTable[fd];
3798 if (fileP == NULL) {
3802 if (fileP->v_type != VDIR) {
3809 * Set up the directory structures
3812 (usr_DIR *) afs_osi_Alloc(sizeof(usr_DIR) + USR_DIRSIZE +
3813 sizeof(struct usr_dirent));
3814 usr_assert(dirp != NULL);
3815 dirp->dd_buf = (char *)(dirp + 1);
3825 * Read directory entries into a file system independent format.
3826 * This routine was developed to support AFS cache consistency testing.
3827 * You should use uafs_readdir instead.
3830 uafs_getdents(int fd, struct min_direct *buf, int len)
3834 retval = uafs_getdents_r(fd, buf, len);
3840 uafs_getdents_r(int fd, struct min_direct *buf, int len)
3844 struct usr_vnode *vp;
3845 struct iovec iov[1];
3848 * Make sure this is an open file
3850 vp = afs_FileTable[fd];
3858 * set up the uio buffer
3860 iov[0].iov_base = (char *)buf;
3861 iov[0].iov_len = len;
3862 uio.uio_iov = &iov[0];
3864 uio.uio_offset = afs_FileOffsets[fd];
3866 uio.uio_fmode = FREAD;
3867 uio.uio_resid = len;
3870 * read the next chunk from the directory
3872 code = afs_readdir(vp, &uio, u.u_cred);
3878 afs_FileOffsets[fd] = uio.uio_offset;
3879 return (len - uio.uio_resid);
3883 * read from a directory (names only)
3886 uafs_readdir(usr_DIR * dirp)
3888 struct usr_dirent *retval;
3890 retval = uafs_readdir_r(dirp);
3896 uafs_readdir_r(usr_DIR * dirp)
3902 struct usr_vnode *vp;
3903 struct iovec iov[1];
3904 struct usr_dirent *direntP;
3905 struct min_direct *directP;
3908 * Make sure this is an open file
3910 vp = afs_FileTable[dirp->dd_fd];
3917 * If there are no entries in the stream buffer
3918 * then read another chunk
3920 directP = (struct min_direct *)(dirp->dd_buf + dirp->dd_loc);
3921 if (dirp->dd_size == 0 || directP->d_fileno == 0) {
3923 * set up the uio buffer
3925 iov[0].iov_base = dirp->dd_buf;
3926 iov[0].iov_len = USR_DIRSIZE;
3927 uio.uio_iov = &iov[0];
3929 uio.uio_offset = afs_FileOffsets[dirp->dd_fd];
3931 uio.uio_fmode = FREAD;
3932 uio.uio_resid = USR_DIRSIZE;
3935 * read the next chunk from the directory
3937 code = afs_readdir(vp, &uio, u.u_cred);
3942 afs_FileOffsets[dirp->dd_fd] = uio.uio_offset;
3944 dirp->dd_size = USR_DIRSIZE - iov[0].iov_len;
3946 directP = (struct min_direct *)(dirp->dd_buf + dirp->dd_loc);
3950 * Check for end of file
3952 if (dirp->dd_size == 0 || directP->d_fileno == 0) {
3956 len = ((sizeof(struct min_direct) + directP->d_namlen + 4) & (~3));
3957 usr_assert(len <= dirp->dd_size);
3960 * Copy the next entry into the usr_dirent structure and advance
3962 direntP = (struct usr_dirent *)(dirp->dd_buf + USR_DIRSIZE);
3963 direntP->d_ino = directP->d_fileno;
3964 direntP->d_off = direntP->d_reclen;
3966 sizeof(struct usr_dirent) - MAXNAMLEN + directP->d_namlen + 1;
3967 memcpy(&direntP->d_name[0], (void *)(directP + 1), directP->d_namlen);
3968 direntP->d_name[directP->d_namlen] = '\0';
3969 dirp->dd_loc += len;
3970 dirp->dd_size -= len;
3979 uafs_closedir(usr_DIR * dirp)
3983 retval = uafs_closedir_r(dirp);
3989 uafs_closedir_r(usr_DIR * dirp)
3995 afs_osi_Free((char *)dirp,
3996 sizeof(usr_DIR) + USR_DIRSIZE + sizeof(struct usr_dirent));
3997 rc = uafs_close_r(fd);
4002 * Do AFS authentication
4005 uafs_klog(char *user, char *cell, char *passwd, char **reason)
4008 afs_int32 password_expires = -1;
4010 usr_mutex_lock(&osi_authenticate_lock);
4012 ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION +
4013 KA_USERAUTH_DOSETPAG2, user, NULL, cell,
4014 passwd, 0, &password_expires, 0, reason);
4015 usr_mutex_unlock(&osi_authenticate_lock);
4020 uafs_klog_r(char *user, char *cell, char *passwd, char **reason)
4024 retval = uafs_klog(user, cell, passwd, reason);
4030 * Destroy AFS credentials from the kernel cache
4037 usr_mutex_lock(&osi_authenticate_lock);
4038 code = ktc_ForgetAllTokens();
4039 usr_mutex_unlock(&osi_authenticate_lock);
4048 retval = uafs_unlog();
4054 * Strip the AFS mount point from a pathname string. Return
4055 * NULL if the path is a relative pathname or if the path
4056 * doesn't start with the AFS mount point string.
4059 uafs_afsPathName(char *path)
4068 for (i = 1, p = path + 1; *p != '\0'; p++) {
4069 /* Ignore duplicate slashes */
4070 if (*p == '/' && lastchar == '/')
4072 /* Is this a subdirectory of the AFS mount point? */
4073 if (afs_mountDir[i] == '\0' && *p == '/') {
4074 /* strip leading slashes */
4075 while (*(++p) == '/');
4078 /* Reject paths that are not within AFS */
4079 if (*p != afs_mountDir[i])
4084 /* Is this the AFS mount point? */
4085 if (afs_mountDir[i] == '\0') {
4086 usr_assert(*p == '\0');
4092 #ifdef AFS_WEB_ENHANCEMENTS
4095 * klog but don't allocate a new pag
4098 uafs_klog_nopag(char *user, char *cell, char *passwd, char **reason)
4101 afs_int32 password_expires = -1;
4103 usr_mutex_lock(&osi_authenticate_lock);
4104 code = ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION
4105 /*+KA_USERAUTH_DOSETPAG2 */ , user,
4106 NULL, cell, passwd, 0,
4107 &password_expires, 0, reason);
4108 usr_mutex_unlock(&osi_authenticate_lock);
4113 * uafs_getcellstatus
4114 * get the cell status
4117 uafs_getcellstatus(char *cell, afs_int32 * status)
4120 struct afs_ioctl iob;
4123 iob.in_size = strlen(cell) + 1;
4127 rc = call_syscall(AFSCALL_PIOCTL, /*path */ 0, _VICEIOCTL(35),
4135 *status = (afs_int32) iob.out;
4141 * Get quota of volume associated with path
4144 uafs_getvolquota(char *path, afs_int32 * BlocksInUse, afs_int32 * MaxQuota)
4147 struct afs_ioctl iob;
4148 VolumeStatus *status;
4154 iob.out_size = 1024;
4156 rc = call_syscall(AFSCALL_PIOCTL, (long)path, _VICEIOCTL(4), (long)&iob,
4164 status = (VolumeStatus *) buf;
4165 *BlocksInUse = status->BlocksInUse;
4166 *MaxQuota = status->MaxQuota;
4172 * Set quota of volume associated with path
4175 uafs_setvolquota(char *path, afs_int32 MaxQuota)
4178 struct afs_ioctl iob;
4179 VolumeStatus *status;
4187 memset(buf, 0, sizeof(VolumeStatus));
4188 status = (VolumeStatus *) buf;
4189 status->MaxQuota = MaxQuota;
4190 status->MinQuota = -1;
4192 rc = call_syscall(AFSCALL_PIOCTL, (long)path, _VICEIOCTL(5), (long)&iob,
4204 * uafs_statmountpoint
4205 * Determine whether a dir. is a mount point or not
4206 * return 1 if mount point, 0 if not
4209 uafs_statmountpoint(char *path)
4216 retval = uafs_statmountpoint_r(path);
4222 uafs_statmountpoint_r(char *path)
4227 struct vrequest treq;
4230 code = uafs_LookupName(path, afs_CurrentDir, &vp, 0, 1);
4245 * Get a list of rights for the current user on path.
4248 uafs_getRights(char *path)
4255 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
4263 PRSFS_READ | PRSFS_WRITE | PRSFS_INSERT | PRSFS_LOOKUP | PRSFS_DELETE
4264 | PRSFS_LOCK | PRSFS_ADMINISTER;
4266 afs_rights = afs_getRights(vp, afs_rights, u.u_cred);
4271 #endif /* AFS_WEB_ENHANCEMENTS */
4273 #endif /* UKERNEL */