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"
22 #include "afs/sysincludes.h" /* Standard vendor system headers */
24 #include "afsincludes.h" /* Afs-based standard headers */
25 #include "afs_usrops.h"
26 #include "afs/afs_stats.h"
28 #include "afs/cellconfig.h"
30 #include "afs/kautils.h"
31 #include "afs/afsutil.h"
32 #include "rx/rx_globals.h"
38 #define CACHEINFOFILE "cacheinfo"
39 #define AFSLOGFILE "AFSLog"
40 #define DCACHEFILE "CacheItems"
41 #define VOLINFOFILE "VolumeItems"
42 #define CELLINFOFILE "CellItems"
46 #define MIN(A,B) ((A)<(B)?(A):(B))
49 #define MAX(A,B) ((A)>(B)?(A):(B))
52 extern int cacheDiskType;
54 char afs_LclCellName[64];
56 struct usr_vnode *afs_FileTable[MAX_OSI_FILES];
57 int afs_FileFlags[MAX_OSI_FILES];
58 off_t afs_FileOffsets[MAX_OSI_FILES];
60 #define MAX_CACHE_LOOPS 4
62 struct usr_vfs afs_RootVfs;
63 struct usr_vnode *afs_RootVnode = NULL;
64 struct usr_vnode *afs_CurrentDir = NULL;
66 afs_int32 cacheBlocks; /* Num blocks in cache */
67 afs_int32 cacheFiles = 1000; /* Num files in workstation cache */
68 afs_int32 cacheStatEntries = 300; /* Num of stat cache entries */
69 char cacheBaseDir[1024]; /* AFS cache directory */
70 char confDir[1024]; /* AFS configuration directory */
71 char afs_mountDir[1024]; /* AFS mount point */
72 int afs_mountDirLen; /* strlen of AFS mount point */
73 char fullpn_DCacheFile[1024]; /* Full pathname of DCACHEFILE */
74 char fullpn_VolInfoFile[1024]; /* Full pathname of VOLINFOFILE */
75 char fullpn_CellInfoFile[1024]; /* Full pathname of CELLINFOFILE */
76 char fullpn_AFSLogFile[1024]; /* Full pathname of AFSLOGFILE */
77 char fullpn_CacheInfo[1024]; /* Full pathname of CACHEINFO */
78 char fullpn_VFile[1024]; /* Full pathname of data cache files */
79 char *vFileNumber; /* Ptr to number in file pathname */
80 char rootVolume[64] = "root.afs"; /* AFS root volume name */
81 afs_int32 isHomeCell; /* Is current cell info for home cell */
82 int createAndTrunc = O_CREAT | O_TRUNC; /* Create & truncate on open */
83 int ownerRWmode = 0600; /* Read/write OK by owner */
84 static int nDaemons = 2; /* Number of background daemons */
85 static int chunkSize = 0; /* 2^chunkSize bytes per chunk */
86 static int dCacheSize = 300; /* # of dcache entries */
87 static int vCacheSize = 50; /* # of volume cache entries */
88 static int cacheFlags = 0; /* Flags to cache manager */
89 static int preallocs = 400; /* Def # of allocated memory blocks */
90 int afsd_verbose = 0; /* Are we being chatty? */
91 int afsd_debug = 0; /* Are we printing debugging info? */
92 int afsd_CloseSynch = 0; /* Are closes synchronous or not? */
94 #define AFSD_INO_T afs_uint32
95 char **pathname_for_V; /* Array of cache file pathnames */
96 int missing_DCacheFile = 1; /* Is the DCACHEFILE missing? */
97 int missing_VolInfoFile = 1; /* Is the VOLINFOFILE missing? */
98 int missing_CellInfoFile = 1;
99 struct afs_cacheParams cparams; /* params passed to cache manager */
100 struct afsconf_dir *afs_cdir; /* config dir */
102 static int HandleMTab();
104 int afs_bufferpages = 100;
105 int usr_udpcksum = 0;
107 usr_key_t afs_global_u_key;
109 struct usr_proc *afs_global_procp = NULL;
110 struct usr_ucred *afs_global_ucredp = NULL;
111 struct usr_sysent usr_sysent[200];
113 #ifdef AFS_USR_OSF_ENV
115 #else /* AFS_USR_OSF_ENV */
117 #endif /* AFS_USR_OSF_ENV */
119 struct usr_ucred afs_osi_cred, *afs_osi_credp;
120 usr_mutex_t afs_global_lock;
121 usr_thread_t afs_global_owner;
122 usr_mutex_t rx_global_lock;
123 usr_thread_t rx_global_owner;
124 usr_mutex_t osi_inode_lock;
125 usr_mutex_t osi_waitq_lock;
126 usr_mutex_t osi_authenticate_lock;
128 afs_lock_t osi_flplock;
129 afs_lock_t osi_fsplock;
132 #ifndef NETSCAPE_NSAPI
135 * Mutex and condition variable used to implement sleep
137 pthread_mutex_t usr_sleep_mutex;
138 pthread_cond_t usr_sleep_cond;
140 #endif /* !NETSCAPE_NSAPI */
142 int call_syscall(long, long, long, long, long, long);
146 * Hash table mapping addresses onto wait structures for
147 * osi_Sleep/osi_Wakeup and osi_Wait/osi_Wakeup
149 typedef struct osi_wait {
153 struct osi_wait *next;
154 struct osi_wait *prev;
156 struct osi_wait *timedNext;
157 struct osi_wait *timedPrev;
161 * Head of the linked list of available waitq structures.
163 osi_wait_t *osi_waithash_avail;
166 * List of timed waits, NSAPI does not provide a cond_timed
167 * wait, so we need to keep track of the timed waits ourselves and
168 * periodically check for expirations
170 osi_wait_t *osi_timedwait_head;
171 osi_wait_t *osi_timedwait_tail;
176 } osi_waithash_table[OSI_WAITHASH_SIZE];
179 * Never call afs_brelse
182 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)
206 * Every user is a super user
209 afs_osi_suser(void *credp)
215 afs_suser(void *credp)
221 * These are no-ops in user space
225 afs_osi_SetTime(osi_timeval_t * atv)
231 * xflock should never fall through, the only files we know
232 * about are AFS files
242 * ioctl should never fall through, the only files we know
243 * about are AFS files
253 * We do not support the inode related system calls
256 afs_syscall_icreate(long a, long b, long c, long d, long e, long f)
263 afs_syscall_iincdec(int dev, int inode, int inode_p1, int amount)
270 afs_syscall_iopen(int dev, int inode, int usrmod)
277 afs_syscall_ireadwrite(void)
284 * these routines are referenced in the vfsops structure, but
285 * should never get called
316 * uiomove copies data between kernel buffers and uio buffers
319 usr_uiomove(char *kbuf, int n, int rw, struct usr_uio *uio)
326 nio = uio->uio_iovcnt;
336 while (nio > 0 && n > 0) {
337 len = MIN(n, iovp->iov_len);
338 if (rw == UIO_READ) {
339 memcpy(iovp->iov_base, ptr, len);
341 memcpy(ptr, iovp->iov_base, len);
345 uio->uio_resid -= len;
346 uio->uio_offset += len;
347 iovp->iov_base = (char *)(iovp->iov_base) + len;
348 iovp->iov_len -= len;
359 * routines to manage user credentials
362 usr_crcopy(struct usr_ucred *credp)
364 struct usr_ucred *newcredp;
366 newcredp = (struct usr_ucred *)afs_osi_Alloc(sizeof(struct usr_ucred));
368 newcredp->cr_ref = 1;
375 struct usr_ucred *newcredp;
377 newcredp = (struct usr_ucred *)afs_osi_Alloc(sizeof(struct usr_ucred));
378 newcredp->cr_ref = 1;
383 usr_crfree(struct usr_ucred *credp)
386 if (credp->cr_ref == 0) {
387 afs_osi_Free((char *)credp, sizeof(struct usr_ucred));
393 usr_crhold(struct usr_ucred *credp)
400 usr_vattr_null(struct usr_vattr *vap)
405 n = sizeof(struct usr_vattr);
413 * Initialize the thread specific data used to simulate the
414 * kernel environment for each thread. The user structure
415 * is stored in the thread specific data.
418 uafs_InitThread(void)
421 struct usr_user *uptr;
424 * initialize the thread specific user structure. Use malloc to
425 * allocate the data block, so pthread_finish can free the buffer
426 * when this thread terminates.
429 (struct usr_user *)malloc(sizeof(struct usr_user) +
430 sizeof(struct usr_ucred));
431 usr_assert(uptr != NULL);
434 uptr->u_procp = afs_global_procp;
435 uptr->u_cred = (struct usr_ucred *)(uptr + 1);
436 *uptr->u_cred = *afs_global_ucredp;
437 st = usr_setspecific(afs_global_u_key, (void *)uptr);
442 * routine to get the user structure from the thread specific data.
443 * this routine is used to implement the global 'u' structure. Initializes
444 * the thread if needed.
447 get_user_struct(void)
449 struct usr_user *uptr;
451 st = usr_getspecific(afs_global_u_key, (void **)&uptr);
455 st = usr_getspecific(afs_global_u_key, (void **)&uptr);
457 usr_assert(uptr != NULL);
463 * Hash an address for the waithash table
465 #define WAITHASH(X) \
466 (((long)(X)^((long)(X)>>4)^((long)(X)<<4))&(OSI_WAITHASH_SIZE-1))
472 afs_osi_Sleep(void *x)
476 int glockOwner = ISAFS_GLOCK();
478 usr_mutex_lock(&osi_waitq_lock);
483 if (osi_waithash_avail == NULL) {
484 waitp = (osi_wait_t *) afs_osi_Alloc(sizeof(osi_wait_t));
485 usr_cond_init(&waitp->cond);
487 waitp = osi_waithash_avail;
488 osi_waithash_avail = osi_waithash_avail->next;
492 DLL_INSERT_TAIL(waitp, osi_waithash_table[index].head,
493 osi_waithash_table[index].tail, next, prev);
494 waitp->expiration = 0;
495 waitp->timedNext = NULL;
496 waitp->timedPrev = NULL;
497 while (waitp->flag == 0) {
498 usr_cond_wait(&waitp->cond, &osi_waitq_lock);
500 DLL_DELETE(waitp, osi_waithash_table[index].head,
501 osi_waithash_table[index].tail, next, prev);
502 waitp->next = osi_waithash_avail;
503 osi_waithash_avail = waitp;
504 usr_mutex_unlock(&osi_waitq_lock);
511 afs_osi_SleepSig(void *x)
518 afs_osi_Wakeup(void *x)
524 usr_mutex_lock(&osi_waitq_lock);
525 waitp = osi_waithash_table[index].head;
527 if (waitp->addr == x && waitp->flag == 0) {
529 usr_cond_signal(&waitp->cond);
533 usr_mutex_unlock(&osi_waitq_lock);
538 afs_osi_Wait(afs_int32 msec, struct afs_osi_WaitHandle *handle, int intok)
544 int glockOwner = ISAFS_GLOCK();
546 tv.tv_sec = msec / 1000;
547 tv.tv_nsec = (msec % 1000) * 1000000;
548 if (handle == NULL) {
552 usr_thread_sleep(&tv);
558 usr_mutex_lock(&osi_waitq_lock);
562 index = WAITHASH((caddr_t) handle);
563 if (osi_waithash_avail == NULL) {
564 waitp = (osi_wait_t *) afs_osi_Alloc(sizeof(osi_wait_t));
565 usr_cond_init(&waitp->cond);
567 waitp = osi_waithash_avail;
568 osi_waithash_avail = osi_waithash_avail->next;
570 waitp->addr = (caddr_t) handle;
572 DLL_INSERT_TAIL(waitp, osi_waithash_table[index].head,
573 osi_waithash_table[index].tail, next, prev);
574 tv.tv_sec += time(NULL);
575 waitp->expiration = tv.tv_sec + ((tv.tv_nsec == 0) ? 0 : 1);
576 DLL_INSERT_TAIL(waitp, osi_timedwait_head, osi_timedwait_tail,
577 timedNext, timedPrev);
578 usr_cond_wait(&waitp->cond, &osi_waitq_lock);
584 DLL_DELETE(waitp, osi_waithash_table[index].head,
585 osi_waithash_table[index].tail, next, prev);
586 DLL_DELETE(waitp, osi_timedwait_head, osi_timedwait_tail, timedNext,
588 waitp->next = osi_waithash_avail;
589 osi_waithash_avail = waitp;
590 usr_mutex_unlock(&osi_waitq_lock);
599 afs_osi_CancelWait(struct afs_osi_WaitHandle *handle)
601 afs_osi_Wakeup(handle);
605 * Netscape NSAPI doesn't have a cond_timed_wait, so we need
606 * to explicitly signal cond_timed_waits when their timers expire
609 afs_osi_CheckTimedWaits(void)
614 curTime = time(NULL);
615 usr_mutex_lock(&osi_waitq_lock);
616 waitp = osi_timedwait_head;
617 while (waitp != NULL) {
618 usr_assert(waitp->expiration != 0);
619 if (waitp->expiration <= curTime) {
621 usr_cond_signal(&waitp->cond);
623 waitp = waitp->timedNext;
625 usr_mutex_unlock(&osi_waitq_lock);
630 * I-node numbers are indeces into a table containing a filename
631 * i-node structure and a vnode structure. When we create an i-node,
632 * we copy the name into the array and initialize enough of the fields
633 * in the inode and vnode structures to get the client to work.
636 struct usr_inode i_node;
639 osi_file_table_t *osi_file_table;
641 int max_osi_files = 0;
644 * Allocate a slot in the file table if there is not one there already,
645 * copy in the file name and kludge up the vnode and inode structures
648 lookupname(char *fnamep, int segflg, int followlink,
649 struct usr_vnode **compvpp)
653 struct usr_inode *ip;
654 struct usr_vnode *vp;
656 /*usr_assert(followlink == 0); */
659 * Assume relative pathnames refer to files in AFS
661 if (*fnamep != '/' || uafs_afsPathName(fnamep) != NULL) {
663 code = uafs_LookupName(fnamep, afs_CurrentDir, compvpp, 0, 0);
668 usr_mutex_lock(&osi_inode_lock);
670 for (i = 0; i < n_osi_files; i++) {
671 if (strcmp(fnamep, osi_file_table[i].name) == 0) {
672 *compvpp = &osi_file_table[i].i_node.i_vnode;
673 (*compvpp)->v_count++;
674 usr_mutex_unlock(&osi_inode_lock);
679 if (n_osi_files == max_osi_files) {
680 usr_mutex_unlock(&osi_inode_lock);
684 osi_file_table[n_osi_files].name = afs_osi_Alloc(strlen(fnamep) + 1);
685 usr_assert(osi_file_table[n_osi_files].name != NULL);
686 strcpy(osi_file_table[n_osi_files].name, fnamep);
687 ip = &osi_file_table[i].i_node;
689 vp->v_data = (caddr_t) ip;
692 ip->i_number = n_osi_files;
694 usr_mutex_unlock(&osi_inode_lock);
700 * open a file given its i-node number
703 osi_UFSOpen(afs_dcache_id_t *ino)
711 if (ino->ufs > n_osi_files) {
717 fp = (struct osi_file *)afs_osi_Alloc(sizeof(struct osi_file));
718 usr_assert(fp != NULL);
719 fp->fd = open(osi_file_table[ino->ufs - 1].name, O_RDWR | O_CREAT, 0);
722 afs_osi_Free((char *)fp, sizeof(struct osi_file));
726 rc = fstat(fp->fd, &st);
729 afs_osi_Free((void *)fp, sizeof(struct osi_file));
733 fp->size = st.st_size;
736 fp->vnode = (struct usr_vnode *)fp;
743 osi_UFSClose(struct osi_file *fp)
753 afs_osi_Free((void *)fp, sizeof(struct osi_file));
757 afs_osi_Free((void *)fp, sizeof(struct osi_file));
763 osi_UFSTruncate(struct osi_file *fp, afs_int32 len)
770 rc = ftruncate(fp->fd, len);
782 afs_osi_Read(struct osi_file *fp, int offset, void *buf, afs_int32 len)
792 rc = lseek(fp->fd, offset, SEEK_SET);
794 rc = lseek(fp->fd, fp->offset, SEEK_SET);
802 ret = read(fp->fd, buf, len);
809 rc = fstat(fp->fd, &st);
815 fp->size = st.st_size;
821 afs_osi_Write(struct osi_file *fp, afs_int32 offset, void *buf, afs_int32 len)
831 rc = lseek(fp->fd, offset, SEEK_SET);
833 rc = lseek(fp->fd, fp->offset, SEEK_SET);
841 ret = write(fp->fd, buf, len);
848 rc = fstat(fp->fd, &st);
854 fp->size = st.st_size;
860 afs_osi_Stat(struct osi_file *fp, struct osi_stat *stp)
866 rc = fstat(fp->fd, &st);
872 stp->size = st.st_size;
873 stp->mtime = st.st_mtime;
874 stp->atime = st.st_atime;
883 afs_osi_VOP_RDWR(struct usr_vnode *vnodeP, struct usr_uio *uioP, int rw,
884 int flags, struct usr_ucred *credP)
887 struct osi_file *fp = (struct osi_file *)vnodeP;
890 * We don't support readv/writev.
892 usr_assert(uioP->uio_iovcnt == 1);
893 usr_assert(uioP->uio_resid == uioP->uio_iov[0].iov_len);
895 if (rw == UIO_WRITE) {
896 usr_assert(uioP->uio_fmode == FWRITE);
897 rc = afs_osi_Write(fp, uioP->uio_offset, uioP->uio_iov[0].iov_base,
898 uioP->uio_iov[0].iov_len);
900 usr_assert(uioP->uio_fmode == FREAD);
901 rc = afs_osi_Read(fp, uioP->uio_offset, uioP->uio_iov[0].iov_base,
902 uioP->uio_iov[0].iov_len);
908 uioP->uio_resid -= rc;
909 uioP->uio_offset += rc;
910 uioP->uio_iov[0].iov_base = (char *)(uioP->uio_iov[0].iov_base) + rc;
911 uioP->uio_iov[0].iov_len -= rc;
916 * Use malloc/free routines with check patterns before and after each block
919 static char *afs_check_string1 = "UAFS";
920 static char *afs_check_string2 = "AFS_OSI_";
923 afs_osi_Alloc(size_t size)
929 afs_osi_Free(void *ptr, size_t size)
935 afs_osi_FreeStr(char *ptr)
941 osi_AllocLargeSpace(size_t size)
943 AFS_STATCNT(osi_AllocLargeSpace);
944 return afs_osi_Alloc(size);
948 osi_FreeLargeSpace(void *ptr)
950 AFS_STATCNT(osi_FreeLargeSpace);
951 afs_osi_Free(ptr, 0);
955 osi_AllocSmallSpace(size_t size)
957 AFS_STATCNT(osi_AllocSmallSpace);
958 return afs_osi_Alloc(size);
962 osi_FreeSmallSpace(void *ptr)
964 AFS_STATCNT(osi_FreeSmallSpace);
965 afs_osi_Free(ptr, 0);
971 AFS_STATCNT(shutdown_osi);
976 shutdown_osinet(void)
978 AFS_STATCNT(shutdown_osinet);
983 shutdown_osifile(void)
985 AFS_STATCNT(shutdown_osifile);
990 afs_nfsclient_init(void)
995 shutdown_nfsclnt(void)
1001 afs_osi_Invisible(void)
1007 afs_osi_Visible(void)
1013 osi_GetTime(struct timeval *tv)
1015 gettimeofday(tv, NULL);
1020 osi_SetTime(struct timeval *tv)
1026 osi_Active(struct vcache *avc)
1028 AFS_STATCNT(osi_Active);
1035 afs_osi_MapStrategy(int (*aproc) (), struct usr_buf *bp)
1037 afs_int32 returnCode;
1038 returnCode = (*aproc) (bp);
1043 osi_FlushPages(register struct vcache *avc, struct AFS_UCRED *credp)
1045 ObtainSharedLock(&avc->lock, 555);
1046 if ((hcmp((avc->f.m.DataVersion), (avc->mapDV)) <= 0)
1047 || ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
1048 ReleaseSharedLock(&avc->lock);
1051 UpgradeSToWLock(&avc->lock, 565);
1052 hset(avc->mapDV, avc->f.m.DataVersion);
1053 ReleaseWriteLock(&avc->lock);
1058 osi_FlushText_really(register struct vcache *vp)
1060 if (hcmp(vp->f.m.DataVersion, vp->flushDV) > 0) {
1061 hset(vp->flushDV, vp->f.m.DataVersion);
1067 osi_SyncVM(struct vcache *avc)
1073 osi_ReleaseVM(struct vcache *avc, int len, struct usr_ucred *credp)
1086 * Allocate the table used to implement psuedo-inodes.
1088 max_osi_files = cacheFiles + 100;
1089 osi_file_table = (osi_file_table_t *)
1090 afs_osi_Alloc(max_osi_files * sizeof(osi_file_table_t));
1091 usr_assert(osi_file_table != NULL);
1093 #ifndef NETSCAPE_NSAPI
1095 * Initialize the mutex and condition variable used to implement
1098 pthread_mutex_init(&usr_sleep_mutex, NULL);
1099 pthread_cond_init(&usr_sleep_cond, NULL);
1100 #endif /* !NETSCAPE_NSAPI */
1103 * Initialize the hash table used for sleep/wakeup
1105 for (i = 0; i < OSI_WAITHASH_SIZE; i++) {
1106 DLL_INIT_LIST(osi_waithash_table[i].head, osi_waithash_table[i].tail);
1108 DLL_INIT_LIST(osi_timedwait_head, osi_timedwait_tail);
1109 osi_waithash_avail = NULL;
1112 * Initialize the AFS file table
1114 for (i = 0; i < MAX_OSI_FILES; i++) {
1115 afs_FileTable[i] = NULL;
1119 * Initialize the global locks
1121 usr_mutex_init(&afs_global_lock);
1122 usr_mutex_init(&rx_global_lock);
1123 usr_mutex_init(&osi_inode_lock);
1124 usr_mutex_init(&osi_waitq_lock);
1125 usr_mutex_init(&osi_authenticate_lock);
1128 * Initialize the AFS OSI credentials
1130 afs_osi_cred = *afs_global_ucredp;
1131 afs_osi_credp = &afs_osi_cred;
1134 /* ParseArgs is now obsolete, being handled by cmd */
1136 /*---------------------------------------------------------------------
1140 * Given the final component of a filename expected to be a data cache file,
1141 * return the integer corresponding to the file. Note: we reject names that
1142 * are not a ``V'' followed by an integer. We also reject those names having
1143 * the right format but lying outside the range [0..cacheFiles-1].
1146 * fname : Char ptr to the filename to parse.
1149 * >= 0 iff the file is really a data cache file numbered from 0 to cacheFiles-1, or
1153 * Nothing interesting.
1157 *------------------------------------------------------------------------*/
1160 GetVFileNumber(char *fname)
1162 int computedVNumber; /*The computed file number we return */
1163 int filenameLen; /*Number of chars in filename */
1164 int currDigit; /*Current digit being processed */
1167 * The filename must have at least two characters, the first of which must be a ``V''
1168 * and the second of which cannot be a zero unless the file is exactly two chars long.
1170 filenameLen = strlen(fname);
1171 if (filenameLen < 2)
1173 if (fname[0] != 'V')
1175 if ((filenameLen > 2) && (fname[1] == '0'))
1179 * Scan through the characters in the given filename, failing immediately if a non-digit
1182 for (currDigit = 1; currDigit < filenameLen; currDigit++)
1183 if (isdigit(fname[currDigit]) == 0)
1187 * All relevant characters are digits. Pull out the decimal number they represent.
1188 * Reject it if it's out of range, otherwise return it.
1190 computedVNumber = atoi(++fname);
1191 if (computedVNumber < cacheFiles)
1192 return (computedVNumber);
1197 /*---------------------------------------------------------------------
1201 * Given a full pathname for a file we need to create for the workstation AFS
1202 * cache, go ahead and create the file.
1205 * fname : Full pathname of file to create.
1208 * 0 iff the file was created,
1212 * The given cache file has been found to be missing.
1216 *------------------------------------------------------------------------*/
1219 CreateCacheFile(char *fname)
1221 static char rn[] = "CreateCacheFile"; /*Routine name */
1222 int cfd; /*File descriptor to AFS cache file */
1223 int closeResult; /*Result of close() */
1226 printf("%s: Creating cache file '%s'\n", rn, fname);
1227 cfd = open(fname, createAndTrunc, ownerRWmode);
1229 printf("%s: Can't create '%s', error return is %d (%d)\n", rn, fname,
1233 closeResult = close(cfd);
1236 ("%s: Can't close newly-created AFS cache file '%s' (code %d)\n",
1244 /*---------------------------------------------------------------------
1248 * Sweep through the AFS cache directory, recording the inode number for
1249 * each valid data cache file there. Also, delete any file that doesn't beint32
1250 * in the cache directory during this sweep, and remember which of the other
1251 * residents of this directory were seen. After the sweep, we create any data
1252 * cache files that were missing.
1255 * vFilesFound : Set to the number of data cache files found.
1258 * 0 if everything went well,
1262 * This routine may be called several times. If the number of data cache files
1263 * found is less than the global cacheFiles, then the caller will need to call it
1264 * again to record the inodes of the missing zero-length data cache files created
1265 * in the previous call.
1268 * Fills up the global pathname_for_V array, may create and/or
1269 * delete files as explained above.
1270 *------------------------------------------------------------------------*/
1273 SweepAFSCache(int *vFilesFound)
1275 static char rn[] = "SweepAFSCache"; /*Routine name */
1276 char fullpn_FileToDelete[1024]; /*File to be deleted from cache */
1277 char *fileToDelete; /*Ptr to last component of above */
1278 DIR *cdirp; /*Ptr to cache directory structure */
1280 struct dirent *currp; /*Current directory entry */
1281 int vFileNum; /*Data cache file's associated number */
1283 if (cacheFlags & AFSCALL_INIT_MEMCACHE) {
1285 printf("%s: Memory Cache, no cache sweep done\n", rn);
1291 printf("%s: Opening cache directory '%s'\n", rn, cacheBaseDir);
1293 if (chmod(cacheBaseDir, 0700)) { /* force it to be 700 */
1294 printf("%s: Can't 'chmod 0700' the cache dir, '%s'.\n", rn,
1298 cdirp = opendir(cacheBaseDir);
1299 if (cdirp == (DIR *) 0) {
1300 printf("%s: Can't open AFS cache directory, '%s'.\n", rn,
1306 * Scan the directory entries, remembering data cache file inodes and the existance
1307 * of other important residents. Delete all files that don't belong here.
1310 sprintf(fullpn_FileToDelete, "%s/", cacheBaseDir);
1311 fileToDelete = fullpn_FileToDelete + strlen(fullpn_FileToDelete);
1313 for (currp = readdir(cdirp); currp; currp = readdir(cdirp)) {
1315 printf("%s: Current directory entry:\n", rn);
1316 #if defined(AFS_USR_DFBSD_ENV)
1317 printf("\tinode=%d, name='%s'\n", currp->d_ino,
1320 printf("\tinode=%d, reclen=%d, name='%s'\n", currp->d_ino,
1321 currp->d_reclen, currp->d_name);
1326 * Guess current entry is for a data cache file.
1328 vFileNum = GetVFileNumber(currp->d_name);
1329 if (vFileNum >= 0) {
1331 * Found a valid data cache filename. Remember this file's name
1332 * and bump the number of files found.
1334 pathname_for_V[vFileNum] =
1335 afs_osi_Alloc(strlen(currp->d_name) + strlen(cacheBaseDir) +
1337 usr_assert(pathname_for_V[vFileNum] != NULL);
1338 sprintf(pathname_for_V[vFileNum], "%s/%s", cacheBaseDir,
1341 } else if (strcmp(currp->d_name, DCACHEFILE) == 0) {
1343 * Found the file holding the dcache entries.
1345 missing_DCacheFile = 0;
1346 } else if (strcmp(currp->d_name, VOLINFOFILE) == 0) {
1348 * Found the file holding the volume info.
1350 missing_VolInfoFile = 0;
1351 } else if (strcmp(currp->d_name, CELLINFOFILE) == 0) {
1352 missing_CellInfoFile = 0;
1353 } else if ((strcmp(currp->d_name, ".") == 0)
1354 || (strcmp(currp->d_name, "..") == 0)
1355 || (strcmp(currp->d_name, "lost+found") == 0)) {
1357 * Don't do anything - this file is legit, and is to be left alone.
1361 * This file doesn't belong in the cache. Nuke it.
1363 sprintf(fileToDelete, "%s", currp->d_name);
1365 printf("%s: Deleting '%s'\n", rn, fullpn_FileToDelete);
1366 if (unlink(fullpn_FileToDelete)) {
1367 printf("%s: Can't unlink '%s', errno is %d\n", rn,
1368 fullpn_FileToDelete, errno);
1374 * Create all the cache files that are missing.
1376 if (missing_DCacheFile) {
1378 printf("%s: Creating '%s'\n", rn, fullpn_DCacheFile);
1379 if (CreateCacheFile(fullpn_DCacheFile))
1380 printf("%s: Can't create '%s'\n", rn, fullpn_DCacheFile);
1382 if (missing_VolInfoFile) {
1384 printf("%s: Creating '%s'\n", rn, fullpn_VolInfoFile);
1385 if (CreateCacheFile(fullpn_VolInfoFile))
1386 printf("%s: Can't create '%s'\n", rn, fullpn_VolInfoFile);
1388 if (missing_CellInfoFile) {
1390 printf("%s: Creating '%s'\n", rn, fullpn_CellInfoFile);
1391 if (CreateCacheFile(fullpn_CellInfoFile))
1392 printf("%s: Can't create '%s'\n", rn, fullpn_CellInfoFile);
1395 if (*vFilesFound < cacheFiles) {
1397 * We came up short on the number of data cache files found. Scan through the inode
1398 * list and create all missing files.
1400 for (vFileNum = 0; vFileNum < cacheFiles; vFileNum++)
1401 if (pathname_for_V[vFileNum] == (AFSD_INO_T) 0) {
1402 sprintf(vFileNumber, "%d", vFileNum);
1404 printf("%s: Creating '%s'\n", rn, fullpn_VFile);
1405 if (CreateCacheFile(fullpn_VFile))
1406 printf("%s: Can't create '%s'\n", rn, fullpn_VFile);
1411 * Close the directory, return success.
1414 printf("%s: Closing cache directory.\n", rn);
1420 ConfigCell(register struct afsconf_cell *aci, void *arock,
1421 struct afsconf_dir *adir)
1423 register int isHomeCell;
1425 afs_int32 cellFlags = 0;
1426 afs_int32 hosts[MAXHOSTSPERCELL];
1428 /* figure out if this is the home cell */
1429 isHomeCell = (strcmp(aci->name, afs_LclCellName) == 0);
1431 cellFlags = 2; /* not home, suid is forbidden */
1433 /* build address list */
1434 for (i = 0; i < MAXHOSTSPERCELL; i++)
1435 memcpy(&hosts[i], &aci->hostAddr[i].sin_addr, sizeof(afs_int32));
1437 if (aci->linkedCell)
1438 cellFlags |= 4; /* Flag that linkedCell arg exists,
1439 * for upwards compatibility */
1441 /* configure one cell */
1442 call_syscall(AFSCALL_CALL, AFSOP_ADDCELL2, (long)hosts, /* server addresses */
1443 (long)aci->name, /* cell name */
1444 (long)cellFlags, /* is this the home cell? */
1445 (long)aci->linkedCell); /* Linked cell, if any */
1450 ConfigCellAlias(struct afsconf_cellalias *aca, void *arock, struct afsconf_dir *adir)
1452 call_syscall(AFSOP_ADDCELLALIAS, (long)aca->aliasName,
1453 (long)aca->realName, 0, 0, 0);
1458 * Set the UDP port number RX uses for UDP datagrams
1461 uafs_SetRxPort(int port)
1463 usr_assert(usr_rx_port == 0);
1469 * Initialize the user space client.
1472 uafs_Init(char *rn, char *mountDirParam, char *confDirParam,
1473 char *cacheBaseDirParam, int cacheBlocksParam, int cacheFilesParam,
1474 int cacheStatEntriesParam, int dCacheSizeParam, int vCacheSizeParam,
1475 int chunkSizeParam, int closeSynchParam, int debugParam,
1476 int nDaemonsParam, int cacheFlagsParam, char *logFile)
1479 struct usr_proc *procp;
1480 struct usr_ucred *ucredp;
1483 int currVFile; /* Current AFS cache file number */
1484 int lookupResult; /* Result of GetLocalCellName() */
1485 int cacheIteration; /* cache verification loop counter */
1486 int vFilesFound; /* Num data cache files found in sweep */
1488 afs_int32 vfs1_type = -1;
1489 struct afs_ioctl iob;
1493 afs_int32 buffer[MAXIPADDRS];
1494 afs_int32 maskbuffer[MAXIPADDRS];
1495 afs_int32 mtubuffer[MAXIPADDRS];
1498 * Use the thread specific data to implement the user structure
1500 usr_keycreate(&afs_global_u_key, free);
1503 * Initialize the global ucred structure
1505 afs_global_ucredp = (struct usr_ucred *)
1506 afs_osi_Alloc(sizeof(struct usr_ucred));
1507 usr_assert(afs_global_ucredp != NULL);
1508 afs_global_ucredp->cr_ref = 1;
1509 afs_global_ucredp->cr_uid = geteuid();
1510 afs_global_ucredp->cr_gid = getegid();
1511 afs_global_ucredp->cr_ruid = getuid();
1512 afs_global_ucredp->cr_rgid = getgid();
1513 afs_global_ucredp->cr_suid = afs_global_ucredp->cr_ruid;
1514 afs_global_ucredp->cr_sgid = afs_global_ucredp->cr_rgid;
1515 st = getgroups(NGROUPS, &afs_global_ucredp->cr_groups[0]);
1516 usr_assert(st >= 0);
1517 afs_global_ucredp->cr_ngroups = (unsigned long)st;
1518 for (i = st; i < NGROUPS; i++) {
1519 afs_global_ucredp->cr_groups[i] = NOGROUP;
1523 * Initialize the global process structure
1525 afs_global_procp = (struct usr_proc *)
1526 afs_osi_Alloc(sizeof(struct usr_proc));
1527 usr_assert(afs_global_procp != NULL);
1528 afs_global_procp->p_pid = getpid();
1529 afs_global_procp->p_ppid = (pid_t) 1;
1530 afs_global_procp->p_ucred = afs_global_ucredp;
1533 * Initialize the AFS mount point, default is '/afs'.
1534 * Strip duplicate/trailing slashes from mount point string.
1535 * afs_mountDirLen is set to strlen(afs_mountDir).
1537 if (mountDirParam) {
1538 sprintf(tbuffer, "%s", mountDirParam);
1540 sprintf(tbuffer, "afs");
1542 afs_mountDir[0] = '/';
1543 afs_mountDirLen = 1;
1544 for (lastchar = '/', p = &tbuffer[0]; *p != '\0'; p++) {
1545 if (lastchar != '/' || *p != '/') {
1546 afs_mountDir[afs_mountDirLen++] = lastchar = *p;
1549 if (lastchar == '/' && afs_mountDirLen > 1)
1551 afs_mountDir[afs_mountDirLen] = '\0';
1552 usr_assert(afs_mountDirLen > 1);
1555 * Initialize cache parameters using the input arguments
1558 cacheBlocks = cacheBlocksParam;
1559 if (cacheFilesParam != 0) {
1560 cacheFiles = cacheFilesParam;
1562 cacheFiles = cacheBlocks / 10;
1564 if (cacheStatEntriesParam != 0) {
1565 cacheStatEntries = cacheStatEntriesParam;
1567 strcpy(cacheBaseDir, cacheBaseDirParam);
1568 if (nDaemonsParam != 0) {
1569 nDaemons = nDaemonsParam;
1573 afsd_verbose = debugParam;
1574 afsd_debug = debugParam;
1575 chunkSize = chunkSizeParam;
1576 if (dCacheSizeParam != 0) {
1577 dCacheSize = dCacheSizeParam;
1579 dCacheSize = cacheFiles / 2;
1581 if (vCacheSizeParam != 0) {
1582 vCacheSize = vCacheSizeParam;
1584 strcpy(confDir, confDirParam);
1585 afsd_CloseSynch = closeSynchParam;
1586 if (cacheFlagsParam >= 0) {
1587 cacheFlags = cacheFlagsParam;
1589 if (cacheFlags & AFSCALL_INIT_MEMCACHE) {
1590 cacheFiles = dCacheSize;
1593 sprintf(fullpn_CacheInfo, "%s/%s", confDir, CACHEINFOFILE);
1594 if (logFile == NULL) {
1595 sprintf(fullpn_AFSLogFile, "%s/%s", confDir, AFSLOGFILE);
1597 strcpy(fullpn_AFSLogFile, logFile);
1600 printf("\n%s: Initializing user space AFS client\n\n", rn);
1601 printf(" mountDir: %s\n", afs_mountDir);
1602 printf(" confDir: %s\n", confDir);
1603 printf(" cacheBaseDir: %s\n", cacheBaseDir);
1604 printf(" cacheBlocks: %d\n", cacheBlocks);
1605 printf(" cacheFiles: %d\n", cacheFiles);
1606 printf(" cacheStatEntries: %d\n", cacheStatEntries);
1607 printf(" dCacheSize: %d\n", dCacheSize);
1608 printf(" vCacheSize: %d\n", vCacheSize);
1609 printf(" chunkSize: %d\n", chunkSize);
1610 printf(" afsd_CloseSynch: %d\n", afsd_CloseSynch);
1611 printf(" afsd_debug/verbose: %d/%d\n", afsd_debug, afsd_verbose);
1612 printf(" nDaemons: %d\n", nDaemons);
1613 printf(" cacheFlags: %d\n", cacheFlags);
1614 printf(" logFile: %s\n", fullpn_AFSLogFile);
1619 * Initialize the AFS client
1624 * Pull out all the configuration info for the workstation's AFS cache and
1625 * the cellular community we're willing to let our users see.
1627 afs_cdir = afsconf_Open(confDir);
1629 printf("afsd: some file missing or bad in %s\n", confDir);
1634 afsconf_GetLocalCell(afs_cdir, afs_LclCellName,
1635 sizeof(afs_LclCellName));
1637 printf("%s: Can't get my home cell name! [Error is %d]\n", rn,
1641 printf("%s: My home cell is '%s'\n", rn, afs_LclCellName);
1644 if ((logfd = fopen(fullpn_AFSLogFile, "r+")) == 0) {
1646 printf("%s: Creating '%s'\n", rn, fullpn_AFSLogFile);
1647 if (CreateCacheFile(fullpn_AFSLogFile)) {
1649 ("%s: Can't create '%s' (You may want to use the -logfile option)\n",
1650 rn, fullpn_AFSLogFile);
1657 * Create and zero the pathname table for the desired cache files.
1659 pathname_for_V = (char **)afs_osi_Alloc(cacheFiles * sizeof(char *));
1660 if (pathname_for_V == NULL) {
1661 printf("%s: malloc() failed for cache file table with %d entries.\n",
1665 memset(pathname_for_V, 0, (cacheFiles * sizeof(char *)));
1667 printf("%s: %d pathname_for_V entries at 0x%x, %d bytes\n", rn,
1668 cacheFiles, pathname_for_V, (cacheFiles * sizeof(AFSD_INO_T)));
1671 * Set up all the pathnames we'll need for later.
1673 sprintf(fullpn_DCacheFile, "%s/%s", cacheBaseDir, DCACHEFILE);
1674 sprintf(fullpn_VolInfoFile, "%s/%s", cacheBaseDir, VOLINFOFILE);
1675 sprintf(fullpn_CellInfoFile, "%s/%s", cacheBaseDir, CELLINFOFILE);
1676 sprintf(fullpn_VFile, "%s/V", cacheBaseDir);
1677 vFileNumber = fullpn_VFile + strlen(fullpn_VFile);
1679 /* initialize AFS callback interface */
1681 /* parse multihomed address files */
1683 st = parseNetFiles((afs_uint32*)buffer,(afs_uint32*) maskbuffer, (afs_uint32*)mtubuffer, MAXIPADDRS, reason,
1684 AFSDIR_CLIENT_NETINFO_FILEPATH,
1685 AFSDIR_CLIENT_NETRESTRICT_FILEPATH);
1687 call_syscall(AFSCALL_CALL, AFSOP_ADVISEADDR, st,
1688 (long)(&buffer[0]), (long)(&maskbuffer[0]),
1689 (long)(&mtubuffer[0]));
1691 printf("ADVISEADDR: Error in specifying interface addresses:%s\n",
1698 * Start the RX listener.
1701 printf("%s: Calling AFSOP_RXLISTENER_DAEMON\n", rn);
1702 fork_syscall(AFSCALL_CALL, AFSOP_RXLISTENER_DAEMON, FALSE, FALSE, FALSE);
1705 printf("%s: Forking rx callback listener.\n", rn);
1707 if (preallocs < cacheStatEntries + 50)
1708 preallocs = cacheStatEntries + 50;
1709 fork_syscall(AFSCALL_CALL, AFSOP_START_RXCALLBACK, preallocs);
1712 * Start the RX event handler.
1715 printf("%s: Calling AFSOP_RXEVENT_DAEMON\n", rn);
1716 fork_syscall(AFSCALL_CALL, AFSOP_RXEVENT_DAEMON, FALSE);
1719 * Set up all the kernel processes needed for AFS.
1723 printf("%s: Initializing AFS daemon.\n", rn);
1724 call_syscall(AFSCALL_CALL, AFSOP_BASIC_INIT, 1, 0, 0, 0);
1727 * Tell the kernel some basic information about the workstation's cache.
1730 printf("%s: Calling AFSOP_CACHEINIT: %d stat cache entries,"
1731 " %d optimum cache files, %d blocks in the cache,"
1732 " flags = 0x%x, dcache entries %d\n", rn, cacheStatEntries,
1733 cacheFiles, cacheBlocks, cacheFlags, dCacheSize);
1734 memset(&cparams, 0, sizeof(cparams));
1735 cparams.cacheScaches = cacheStatEntries;
1736 cparams.cacheFiles = cacheFiles;
1737 cparams.cacheBlocks = cacheBlocks;
1738 cparams.cacheDcaches = dCacheSize;
1739 cparams.cacheVolumes = vCacheSize;
1740 cparams.chunkSize = chunkSize;
1741 cparams.setTimeFlag = FALSE;
1742 cparams.memCacheFlag = cacheFlags;
1743 call_syscall(AFSCALL_CALL, AFSOP_CACHEINIT, (long)&cparams, 0, 0, 0);
1744 if (afsd_CloseSynch)
1745 call_syscall(AFSCALL_CALL, AFSOP_CLOSEWAIT, 0, 0, 0, 0);
1748 * Sweep the workstation AFS cache directory, remembering the inodes of
1749 * valid files and deleting extraneous files. Keep sweeping until we
1750 * have the right number of data cache files or we've swept too many
1754 printf("%s: Sweeping workstation's AFS cache directory.\n", rn);
1756 /* Memory-cache based system doesn't need any of this */
1757 if (!(cacheFlags & AFSCALL_INIT_MEMCACHE)) {
1760 if (SweepAFSCache(&vFilesFound)) {
1761 printf("%s: Error on sweep %d of workstation AFS cache \
1762 directory.\n", rn, cacheIteration);
1767 ("%s: %d out of %d data cache files found in sweep %d.\n",
1768 rn, vFilesFound, cacheFiles, cacheIteration);
1769 } while ((vFilesFound < cacheFiles)
1770 && (cacheIteration < MAX_CACHE_LOOPS));
1771 } else if (afsd_verbose)
1772 printf("%s: Using memory cache, not swept\n", rn);
1775 * Pass the kernel the name of the workstation cache file holding the
1779 printf("%s: Calling AFSOP_CACHEINFO: dcache file is '%s'\n", rn,
1781 /* once again, meaningless for a memory-based cache. */
1782 if (!(cacheFlags & AFSCALL_INIT_MEMCACHE))
1783 call_syscall(AFSCALL_CALL, AFSOP_CACHEINFO, (long)fullpn_DCacheFile,
1786 call_syscall(AFSCALL_CALL, AFSOP_CELLINFO, (long)fullpn_CellInfoFile, 0,
1790 * Pass the kernel the name of the workstation cache file holding the
1791 * volume information.
1794 printf("%s: Calling AFSOP_VOLUMEINFO: volume info file is '%s'\n", rn,
1795 fullpn_VolInfoFile);
1796 call_syscall(AFSCALL_CALL, AFSOP_VOLUMEINFO, (long)fullpn_VolInfoFile, 0,
1800 * Pass the kernel the name of the afs logging file holding the volume
1804 printf("%s: Calling AFSOP_AFSLOG: volume info file is '%s'\n", rn,
1806 if (!(cacheFlags & AFSCALL_INIT_MEMCACHE)) /* ... nor this ... */
1807 call_syscall(AFSCALL_CALL, AFSOP_AFSLOG, (long)fullpn_AFSLogFile, 0,
1811 * Tell the kernel about each cell in the configuration.
1813 afsconf_CellApply(afs_cdir, ConfigCell, NULL);
1814 afsconf_CellAliasApply(afs_cdir, ConfigCellAlias, NULL);
1817 * Set the primary cell name.
1819 call_syscall(AFSCALL_CALL, AFSOP_SET_THISCELL, (long)afs_LclCellName, 0, 0, 0);
1822 printf("%s: Forking AFS daemon.\n", rn);
1823 fork_syscall(AFSCALL_CALL, AFSOP_START_AFS);
1826 printf("%s: Forking check server daemon.\n", rn);
1827 fork_syscall(AFSCALL_CALL, AFSOP_START_CS);
1830 printf("%s: Forking %d background daemons.\n", rn, nDaemons);
1831 for (i = 0; i < nDaemons; i++) {
1832 fork_syscall(AFSCALL_CALL, AFSOP_START_BKG);
1836 printf("%s: Calling AFSOP_ROOTVOLUME with '%s'\n", rn, rootVolume);
1837 call_syscall(AFSCALL_CALL, AFSOP_ROOTVOLUME, (long)rootVolume, 0, 0, 0);
1840 * Give the kernel the names of the AFS files cached on the workstation's
1845 ("%s: Calling AFSOP_CACHEFILES for each of the %d files in '%s'\n",
1846 rn, cacheFiles, cacheBaseDir);
1847 if (!(cacheFlags & AFSCALL_INIT_MEMCACHE)) /* ... and again ... */
1848 for (currVFile = 0; currVFile < cacheFiles; currVFile++) {
1849 call_syscall(AFSCALL_CALL, AFSOP_CACHEFILE,
1850 (long)pathname_for_V[currVFile], 0, 0, 0);
1853 /*#ifndef NETSCAPE_NSAPI*/
1855 /* this breaks solaris if the kernel-mode client has never been installed,
1856 * and it doesn't seem to work now anyway, so just disable it */
1859 * Copy our tokens from the kernel to the user space client
1861 for (i = 0; i < 200; i++) {
1863 * Get the i'th token from the kernel
1865 memset((void *)&tbuffer[0], 0, sizeof(tbuffer));
1866 memcpy((void *)&tbuffer[0], (void *)&i, sizeof(int));
1868 iob.in_size = sizeof(int);
1870 iob.out_size = sizeof(tbuffer);
1872 #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)
1873 rc = syscall(AFS_SYSCALL, AFSCALL_PIOCTL, 0, _VICEIOCTL(8), &iob, 0);
1874 #elif defined(AFS_USR_SGI_ENV)
1875 rc = syscall(AFS_PIOCTL, 0, _VICEIOCTL(8), &iob, 0);
1876 #else /* AFS_USR_AIX_ENV */
1877 rc = lpioctl(0, _VICEIOCTL(8), &iob, 0);
1880 usr_assert(errno == EDOM || errno == ENOSYS || errno == ERANGE);
1885 * Now pass the token into the user space kernel
1887 rc = uafs_SetTokens(tbuffer, iob.out_size);
1888 usr_assert(rc == 0);
1890 #endif /* !NETSCAPE_NSAPI */
1893 * All the necessary info has been passed into the kernel to run an AFS
1894 * system. Give the kernel our go-ahead.
1897 printf("%s: Calling AFSOP_GO\n", rn);
1898 call_syscall(AFSCALL_CALL, AFSOP_GO, FALSE, 0, 0, 0);
1901 * At this point, we have finished passing the kernel all the info
1902 * it needs to set up the AFS. Mount the AFS root.
1904 printf("%s: All AFS daemons started.\n", rn);
1907 printf("%s: Forking trunc-cache daemon.\n", rn);
1908 fork_syscall(AFSCALL_CALL, AFSOP_START_TRUNCDAEMON);
1911 * Mount the AFS filesystem
1914 rc = afs_mount(&afs_RootVfs, NULL, NULL);
1915 usr_assert(rc == 0);
1916 rc = afs_root(&afs_RootVfs, &afs_RootVnode);
1917 usr_assert(rc == 0);
1921 * initialize the current directory to the AFS root
1923 afs_CurrentDir = afs_RootVnode;
1924 VN_HOLD(afs_CurrentDir);
1937 VN_RELE(afs_CurrentDir);
1938 rc = afs_unmount(&afs_RootVfs);
1939 usr_assert(rc == 0);
1946 * Donate the current thread to the RX server pool.
1949 uafs_RxServerProc(void)
1953 struct rx_call *newcall = NULL;
1955 rxi_MorePackets(2); /* alloc more packets */
1956 threadID = rxi_availProcs++;
1959 sock = OSI_NULLSOCKET;
1960 rxi_ServerProc(threadID, newcall, &sock);
1961 if (sock == OSI_NULLSOCKET) {
1966 rxi_ListenerProc(sock, &threadID, &newcall);
1967 /* assert(threadID != -1); */
1968 /* assert(newcall != NULL); */
1972 struct syscallThreadArgs {
1981 #ifdef NETSCAPE_NSAPI
1983 syscallThread(void *argp)
1984 #else /* NETSCAPE_NSAPI */
1986 syscallThread(void *argp)
1987 #endif /* NETSCAPE_NSAPI */
1990 struct usr_ucred *crp;
1991 struct syscallThreadArgs *sysArgsP = (struct syscallThreadArgs *)argp;
1994 * AFS daemons run authenticated
1996 u.u_viceid = getuid();
1998 crp->cr_uid = getuid();
1999 crp->cr_ruid = getuid();
2000 crp->cr_suid = getuid();
2001 crp->cr_groups[0] = getgid();
2002 crp->cr_ngroups = 1;
2003 for (i = 1; i < NGROUPS; i++) {
2004 crp->cr_groups[i] = NOGROUP;
2007 call_syscall(sysArgsP->syscall, sysArgsP->afscall, sysArgsP->param1,
2008 sysArgsP->param2, sysArgsP->param3, sysArgsP->param4);
2010 afs_osi_Free(argp, -1);
2014 fork_syscall(syscall, afscall, param1, param2, param3, param4)
2015 long syscall, afscall, param1, param2, param3, param4;
2018 struct syscallThreadArgs *sysArgsP;
2020 sysArgsP = (struct syscallThreadArgs *)
2021 afs_osi_Alloc(sizeof(struct syscallThreadArgs));
2022 usr_assert(sysArgsP != NULL);
2023 sysArgsP->syscall = syscall;
2024 sysArgsP->afscall = afscall;
2025 sysArgsP->param1 = param1;
2026 sysArgsP->param2 = param2;
2027 sysArgsP->param3 = param3;
2028 sysArgsP->param4 = param4;
2030 usr_thread_create(&tid, syscallThread, sysArgsP);
2031 usr_thread_detach(tid);
2035 call_syscall(syscall, afscall, param1, param2, param3, param4)
2036 long syscall, afscall, param1, param2, param3, param4;
2048 a.syscall = syscall;
2049 a.afscall = afscall;
2056 u.u_ap = (char *)&a;
2058 code = Afs_syscall();
2063 uafs_SetTokens(char *tbuffer, int tlen)
2066 struct afs_ioctl iob;
2071 iob.out = &outbuf[0];
2072 iob.out_size = sizeof(outbuf);
2074 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(3), (long)&iob, 0, 0);
2083 uafs_RPCStatsEnableProc(void)
2086 struct afs_ioctl iob;
2089 flag = AFSCALL_RXSTATS_ENABLE;
2090 iob.in = (char *)&flag;
2091 iob.in_size = sizeof(afs_int32);
2094 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(53), (long)&iob, 0, 0);
2103 uafs_RPCStatsDisableProc(void)
2106 struct afs_ioctl iob;
2109 flag = AFSCALL_RXSTATS_DISABLE;
2110 iob.in = (char *)&flag;
2111 iob.in_size = sizeof(afs_int32);
2114 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(53), (long)&iob, 0, 0);
2123 uafs_RPCStatsClearProc(void)
2126 struct afs_ioctl iob;
2129 flag = AFSCALL_RXSTATS_CLEAR;
2130 iob.in = (char *)&flag;
2131 iob.in_size = sizeof(afs_int32);
2134 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(53), (long)&iob, 0, 0);
2143 uafs_RPCStatsEnablePeer(void)
2146 struct afs_ioctl iob;
2149 flag = AFSCALL_RXSTATS_ENABLE;
2150 iob.in = (char *)&flag;
2151 iob.in_size = sizeof(afs_int32);
2154 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(54), (long)&iob, 0, 0);
2163 uafs_RPCStatsDisablePeer(void)
2166 struct afs_ioctl iob;
2169 flag = AFSCALL_RXSTATS_DISABLE;
2170 iob.in = (char *)&flag;
2171 iob.in_size = sizeof(afs_int32);
2174 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(54), (long)&iob, 0, 0);
2183 uafs_RPCStatsClearPeer(void)
2186 struct afs_ioctl iob;
2189 flag = AFSCALL_RXSTATS_CLEAR;
2190 iob.in = (char *)&flag;
2191 iob.in_size = sizeof(afs_int32);
2194 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(54), (long)&iob, 0, 0);
2203 * Lookup a file or directory given its path.
2204 * Call VN_HOLD on the output vnode if successful.
2205 * Returns zero on success, error code on failure.
2207 * Note: Caller must hold the AFS global lock.
2210 uafs_LookupName(char *path, struct usr_vnode *parentVp,
2211 struct usr_vnode **vpp, int follow, int no_eval_mtpt)
2215 struct usr_vnode *vp;
2216 struct usr_vnode *nextVp;
2217 struct usr_vnode *linkVp;
2220 char *nextPathP = NULL;
2225 * Absolute paths must start with the AFS mount point.
2227 if (path[0] != '/') {
2230 path = uafs_afsPathName(path);
2238 * Loop through the path looking for the new directory
2240 tmpPath = afs_osi_Alloc(strlen(path) + 1);
2241 usr_assert(tmpPath != NULL);
2242 strcpy(tmpPath, path);
2245 while (pathP != NULL && *pathP != '\0') {
2246 usr_assert(*pathP != '/');
2249 * terminate the current component and skip over slashes
2251 nextPathP = afs_strchr(pathP, '/');
2252 if (nextPathP != NULL) {
2253 while (*nextPathP == '/') {
2254 *(nextPathP++) = '\0';
2259 * Don't call afs_lookup on non-directories
2261 if (vp->v_type != VDIR) {
2263 afs_osi_Free(tmpPath, strlen(path) + 1);
2267 if (vp == afs_RootVnode && strcmp(pathP, "..") == 0) {
2269 * The AFS root is its own parent
2271 nextVp = afs_RootVnode;
2274 * We need execute permission to search a directory
2276 code = afs_access(VTOAFS(vp), VEXEC, u.u_cred);
2279 afs_osi_Free(tmpPath, strlen(path) + 1);
2284 * lookup the next component in the path, we can release the
2285 * subdirectory since we hold the global lock
2288 #ifdef AFS_WEB_ENHANCEMENTS
2289 if ((nextPathP != NULL && *nextPathP != '\0') || !no_eval_mtpt)
2290 code = afs_lookup(vp, pathP, &nextVp, u.u_cred, 0);
2293 afs_lookup(vp, pathP, &nextVp, u.u_cred,
2296 code = afs_lookup(vp, pathP, &nextVp, u.u_cred, 0);
2297 #endif /* AFS_WEB_ENHANCEMENTS */
2300 afs_osi_Free(tmpPath, strlen(path) + 1);
2306 * Follow symbolic links for parent directories and
2307 * for leaves when the follow flag is set.
2309 if ((nextPathP != NULL && *nextPathP != '\0') || follow) {
2311 while (nextVp->v_type == VLNK) {
2312 if (++linkCount > MAX_OSI_LINKS) {
2315 afs_osi_Free(tmpPath, strlen(path) + 1);
2318 code = uafs_LookupLink(nextVp, vp, &linkVp);
2322 afs_osi_Free(tmpPath, strlen(path) + 1);
2336 * Special case, nextPathP is non-null if pathname ends in slash
2338 if (nextPathP != NULL && vp->v_type != VDIR) {
2340 afs_osi_Free(tmpPath, strlen(path) + 1);
2344 afs_osi_Free(tmpPath, strlen(path) + 1);
2350 * Lookup the target of a symbolic link
2351 * Call VN_HOLD on the output vnode if successful.
2352 * Returns zero on success, error code on failure.
2354 * Note: Caller must hold the AFS global lock.
2357 uafs_LookupLink(struct usr_vnode *vp, struct usr_vnode *parentVp,
2358 struct usr_vnode **vpp)
2363 struct usr_vnode *linkVp;
2365 struct iovec iov[1];
2369 pathP = afs_osi_Alloc(MAX_OSI_PATH + 1);
2370 usr_assert(pathP != NULL);
2373 * set up the uio buffer
2375 iov[0].iov_base = pathP;
2376 iov[0].iov_len = MAX_OSI_PATH + 1;
2377 uio.uio_iov = &iov[0];
2381 uio.uio_fmode = FREAD;
2382 uio.uio_resid = MAX_OSI_PATH + 1;
2385 * Read the link data
2387 code = afs_readlink(vp, &uio, u.u_cred);
2389 afs_osi_Free(pathP, MAX_OSI_PATH + 1);
2392 len = MAX_OSI_PATH + 1 - uio.uio_resid;
2396 * Find the target of the symbolic link
2398 code = uafs_LookupName(pathP, parentVp, &linkVp, 1, 0);
2400 afs_osi_Free(pathP, MAX_OSI_PATH + 1);
2404 afs_osi_Free(pathP, MAX_OSI_PATH + 1);
2410 * Lookup the parent of a file or directory given its path
2411 * Call VN_HOLD on the output vnode if successful.
2412 * Returns zero on success, error code on failure.
2414 * Note: Caller must hold the AFS global lock.
2417 uafs_LookupParent(char *path, struct usr_vnode **vpp)
2422 struct usr_vnode *parentP;
2427 * Absolute path names must start with the AFS mount point.
2430 pathP = uafs_afsPathName(path);
2431 if (pathP == NULL) {
2437 * Find the length of the parent path
2440 while (len > 0 && path[len - 1] == '/') {
2446 while (len > 0 && path[len - 1] != '/') {
2453 pathP = afs_osi_Alloc(len);
2454 usr_assert(pathP != NULL);
2455 memcpy(pathP, path, len - 1);
2456 pathP[len - 1] = '\0';
2459 * look up the parent
2461 code = uafs_LookupName(pathP, afs_CurrentDir, &parentP, 1, 0);
2462 afs_osi_Free(pathP, len);
2466 if (parentP->v_type != VDIR) {
2476 * Return a pointer to the first character in the last component
2480 uafs_LastPath(char *path)
2485 while (len > 0 && path[len - 1] == '/') {
2488 while (len > 0 && path[len - 1] != '/') {
2498 * Set the working directory.
2501 uafs_chdir(char *path)
2505 retval = uafs_chdir_r(path);
2511 uafs_chdir_r(char *path)
2516 code = uafs_LookupName(path, afs_CurrentDir, &dirP, 1, 0);
2521 if (dirP->v_type != VDIR) {
2526 VN_RELE(afs_CurrentDir);
2527 afs_CurrentDir = dirP;
2532 * Create a directory.
2535 uafs_mkdir(char *path, int mode)
2539 retval = uafs_mkdir_r(path, mode);
2545 uafs_mkdir_r(char *path, int mode)
2549 struct vnode *parentP;
2551 struct usr_vattr attrs;
2553 if (uafs_IsRoot(path)) {
2558 * Look up the parent directory.
2560 nameP = uafs_LastPath(path);
2561 if (nameP != NULL) {
2562 code = uafs_LookupParent(path, &parentP);
2568 parentP = afs_CurrentDir;
2574 * Make sure the directory has at least one character
2576 if (*nameP == '\0') {
2583 * Create the directory
2585 usr_vattr_null(&attrs);
2586 attrs.va_type = VREG;
2587 attrs.va_mode = mode;
2588 attrs.va_uid = u.u_cred->cr_uid;
2589 attrs.va_gid = u.u_cred->cr_gid;
2591 code = afs_mkdir(parentP, nameP, &attrs, &dirP, u.u_cred);
2602 * Return 1 if path is the AFS root, otherwise return 0
2605 uafs_IsRoot(char *path)
2607 while (*path == '/' && *(path + 1) == '/') {
2610 if (strncmp(path, afs_mountDir, afs_mountDirLen) != 0) {
2613 path += afs_mountDirLen;
2614 while (*path == '/') {
2617 if (*path != '\0') {
2625 * Note: file name may not end in a slash.
2628 uafs_open(char *path, int flags, int mode)
2632 retval = uafs_open_r(path, flags, mode);
2638 uafs_open_r(char *path, int flags, int mode)
2644 struct usr_vnode *fileP;
2645 struct usr_vnode *dirP;
2646 struct usr_vattr attrs;
2651 if (uafs_IsRoot(path)) {
2652 fileP = afs_RootVnode;
2656 * Look up the parent directory.
2658 nameP = uafs_LastPath(path);
2659 if (nameP != NULL) {
2660 code = uafs_LookupParent(path, &dirP);
2666 dirP = afs_CurrentDir;
2672 * Make sure the filename has at least one character
2674 if (*nameP == '\0') {
2681 * Get the VNODE for this file
2683 if (flags & O_CREAT) {
2684 usr_vattr_null(&attrs);
2685 attrs.va_type = VREG;
2686 attrs.va_mode = mode;
2687 attrs.va_uid = u.u_cred->cr_uid;
2688 attrs.va_gid = u.u_cred->cr_gid;
2689 if (flags & O_TRUNC) {
2695 afs_create(VTOAFS(dirP), nameP, &attrs,
2696 (flags & O_EXCL) ? usr_EXCL : usr_NONEXCL, mode,
2706 code = uafs_LookupName(nameP, dirP, &fileP, 1, 0);
2714 * Check whether we have access to this file
2717 if (flags & (O_RDONLY | O_RDWR)) {
2720 if (flags & (O_WRONLY | O_RDWR)) {
2724 fileMode = VREAD; /* since O_RDONLY is 0 */
2725 code = afs_access(VTOAFS(fileP), fileMode, u.u_cred);
2733 * Get the file attributes, all we need is the size
2735 code = afs_getattr(VTOAFS(fileP), &attrs, u.u_cred);
2745 * Setup the open flags
2748 if (flags & O_TRUNC) {
2749 openFlags |= FTRUNC;
2751 if (flags & O_APPEND) {
2752 openFlags |= FAPPEND;
2754 if (flags & O_SYNC) {
2757 if (flags & O_SYNC) {
2760 if (flags & (O_RDONLY | O_RDWR)) {
2763 if (flags & (O_WRONLY | O_RDWR)) {
2764 openFlags |= FWRITE;
2766 if ((openFlags & (FREAD | FWRITE)) == 0) {
2767 /* O_RDONLY is 0, so ... */
2772 * Truncate if necessary
2774 if ((flags & O_TRUNC) && (attrs.va_size != 0)) {
2775 usr_vattr_null(&attrs);
2776 attrs.va_mask = ATTR_SIZE;
2778 code = afs_setattr(VTOAFS(fileP), &attrs, u.u_cred);
2790 code = afs_open(&vc, openFlags, u.u_cred);
2798 * Put the vnode pointer into the file table
2800 for (fd = 0; fd < MAX_OSI_FILES; fd++) {
2801 if (afs_FileTable[fd] == NULL) {
2802 afs_FileTable[fd] = fileP;
2803 afs_FileFlags[fd] = openFlags;
2804 if (flags & O_APPEND) {
2805 afs_FileOffsets[fd] = attrs.va_size;
2807 afs_FileOffsets[fd] = 0;
2812 if (fd == MAX_OSI_FILES) {
2825 uafs_creat(char *path, int mode)
2828 rc = uafs_open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
2833 uafs_creat_r(char *path, int mode)
2836 rc = uafs_open_r(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
2844 uafs_write(int fd, char *buf, int len)
2848 retval = uafs_pwrite_r(fd, buf, len, afs_FileOffsets[fd]);
2854 uafs_pwrite(int fd, char *buf, int len, off_t offset)
2858 retval = uafs_pwrite_r(fd, buf, len, offset);
2864 uafs_pwrite_r(int fd, char *buf, int len, off_t offset)
2868 struct iovec iov[1];
2869 struct usr_vnode *fileP;
2872 * Make sure this is an open file
2874 fileP = afs_FileTable[fd];
2875 if (fileP == NULL) {
2881 * set up the uio buffer
2883 iov[0].iov_base = buf;
2884 iov[0].iov_len = len;
2885 uio.uio_iov = &iov[0];
2887 uio.uio_offset = offset;
2889 uio.uio_fmode = FWRITE;
2890 uio.uio_resid = len;
2896 code = afs_write(VTOAFS(fileP), &uio, afs_FileFlags[fd], u.u_cred, 0);
2902 afs_FileOffsets[fd] = uio.uio_offset;
2903 return (len - uio.uio_resid);
2910 uafs_read(int fd, char *buf, int len)
2914 retval = uafs_pread_r(fd, buf, len, afs_FileOffsets[fd]);
2920 uafs_pread(int fd, char *buf, int len, off_t offset)
2924 retval = uafs_pread_r(fd, buf, len, offset);
2930 uafs_pread_r(int fd, char *buf, int len, off_t offset)
2934 struct iovec iov[1];
2935 struct usr_vnode *fileP;
2936 struct usr_buf *bufP;
2939 * Make sure this is an open file
2941 fileP = afs_FileTable[fd];
2942 if (fileP == NULL) {
2948 * set up the uio buffer
2950 iov[0].iov_base = buf;
2951 iov[0].iov_len = len;
2952 uio.uio_iov = &iov[0];
2954 uio.uio_offset = offset;
2956 uio.uio_fmode = FREAD;
2957 uio.uio_resid = len;
2962 code = afs_read(VTOAFS(fileP), &uio, u.u_cred, 0, &bufP, 0);
2968 afs_FileOffsets[fd] = uio.uio_offset;
2969 return (len - uio.uio_resid);
2973 * Copy the attributes of a file into a stat structure.
2975 * NOTE: Caller must hold the global AFS lock.
2978 uafs_GetAttr(struct usr_vnode *vp, struct stat *stats)
2981 struct usr_vattr attrs;
2986 * Get the attributes
2988 code = afs_getattr(VTOAFS(vp), &attrs, u.u_cred);
2994 * Copy the attributes, zero fields that aren't set
2996 memset((void *)stats, 0, sizeof(struct stat));
2998 stats->st_ino = attrs.va_nodeid;
2999 stats->st_mode = attrs.va_mode;
3000 stats->st_nlink = attrs.va_nlink;
3001 stats->st_uid = attrs.va_uid;
3002 stats->st_gid = attrs.va_gid;
3003 stats->st_rdev = attrs.va_rdev;
3004 stats->st_size = attrs.va_size;
3005 stats->st_atime = attrs.va_atime.tv_sec;
3006 stats->st_mtime = attrs.va_mtime.tv_sec;
3007 stats->st_ctime = attrs.va_ctime.tv_sec;
3008 stats->st_blksize = attrs.va_blocksize;
3009 stats->st_blocks = attrs.va_blocks;
3015 * Get the attributes of a file, do follow links
3018 uafs_stat(char *path, struct stat *buf)
3022 retval = uafs_stat_r(path, buf);
3028 uafs_stat_r(char *path, struct stat *buf)
3033 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
3038 code = uafs_GetAttr(vp, buf);
3048 * Get the attributes of a file, don't follow links
3051 uafs_lstat(char *path, struct stat *buf)
3055 retval = uafs_lstat_r(path, buf);
3061 uafs_lstat_r(char *path, struct stat *buf)
3066 code = uafs_LookupName(path, afs_CurrentDir, &vp, 0, 0);
3071 code = uafs_GetAttr(vp, buf);
3081 * Get the attributes of an open file
3084 uafs_fstat(int fd, struct stat *buf)
3088 retval = uafs_fstat_r(fd, buf);
3094 uafs_fstat_r(int fd, struct stat *buf)
3099 vp = afs_FileTable[fd];
3104 code = uafs_GetAttr(vp, buf);
3113 * change the permissions on a file
3116 uafs_chmod(char *path, int mode)
3120 retval = uafs_chmod_r(path, mode);
3126 uafs_chmod_r(char *path, int mode)
3130 struct usr_vattr attrs;
3132 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
3137 usr_vattr_null(&attrs);
3138 attrs.va_mask = ATTR_MODE;
3139 attrs.va_mode = mode;
3140 code = afs_setattr(VTOAFS(vp), &attrs, u.u_cred);
3150 * change the permissions on an open file
3153 uafs_fchmod(int fd, int mode)
3157 retval = uafs_fchmod_r(fd, mode);
3163 uafs_fchmod_r(int fd, int mode)
3167 struct usr_vattr attrs;
3169 vp = afs_FileTable[fd];
3174 usr_vattr_null(&attrs);
3175 attrs.va_mask = ATTR_MODE;
3176 attrs.va_mode = mode;
3177 code = afs_setattr(VTOAFS(vp), &attrs, u.u_cred);
3189 uafs_truncate(char *path, int length)
3193 retval = uafs_truncate_r(path, length);
3199 uafs_truncate_r(char *path, int length)
3203 struct usr_vattr attrs;
3205 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
3210 usr_vattr_null(&attrs);
3211 attrs.va_mask = ATTR_SIZE;
3212 attrs.va_size = length;
3213 code = afs_setattr(VTOAFS(vp), &attrs, u.u_cred);
3223 * truncate an open file
3226 uafs_ftruncate(int fd, int length)
3230 retval = uafs_ftruncate_r(fd, length);
3236 uafs_ftruncate_r(int fd, int length)
3240 struct usr_vattr attrs;
3242 vp = afs_FileTable[fd];
3247 usr_vattr_null(&attrs);
3248 attrs.va_mask = ATTR_SIZE;
3249 attrs.va_size = length;
3250 code = afs_setattr(VTOAFS(vp), &attrs, u.u_cred);
3259 * set the read/write file pointer of an open file
3262 uafs_lseek(int fd, int offset, int whence)
3266 retval = uafs_lseek_r(fd, offset, whence);
3272 uafs_lseek_r(int fd, int offset, int whence)
3276 struct usr_vattr attrs;
3277 struct usr_vnode *vp;
3279 vp = afs_FileTable[fd];
3286 newpos = afs_FileOffsets[fd] + offset;
3292 code = afs_getattr(VTOAFS(vp), &attrs, u.u_cred);
3297 newpos = attrs.va_size + offset;
3307 afs_FileOffsets[fd] = newpos;
3319 retval = uafs_fsync_r(fd);
3325 uafs_fsync_r(int fd)
3328 struct usr_vnode *fileP;
3331 fileP = afs_FileTable[fd];
3332 if (fileP == NULL) {
3337 code = afs_fsync(fileP, u.u_cred);
3354 retval = uafs_close_r(fd);
3360 uafs_close_r(int fd)
3363 struct usr_vnode *fileP;
3365 fileP = afs_FileTable[fd];
3366 if (fileP == NULL) {
3370 afs_FileTable[fd] = NULL;
3372 code = afs_close(fileP, afs_FileFlags[fd], u.u_cred);
3383 * Create a hard link from the source to the target
3384 * Note: file names may not end in a slash.
3387 uafs_link(char *existing, char *new)
3391 retval = uafs_link_r(existing, new);
3397 uafs_link_r(char *existing, char *new)
3400 struct usr_vnode *existP;
3401 struct usr_vnode *dirP;
3404 if (uafs_IsRoot(new)) {
3409 * Look up the existing node.
3411 code = uafs_LookupName(existing, afs_CurrentDir, &existP, 1, 0);
3418 * Look up the parent directory.
3420 nameP = uafs_LastPath(new);
3421 if (nameP != NULL) {
3422 code = uafs_LookupParent(new, &dirP);
3429 dirP = afs_CurrentDir;
3435 * Make sure the filename has at least one character
3437 if (*nameP == '\0') {
3447 code = afs_link(existP, dirP, nameP, u.u_cred);
3458 * Create a symbolic link from the source to the target
3459 * Note: file names may not end in a slash.
3462 uafs_symlink(char *target, char *source)
3466 retval = uafs_symlink_r(target, source);
3472 uafs_symlink_r(char *target, char *source)
3475 struct usr_vnode *dirP;
3476 struct usr_vattr attrs;
3479 if (uafs_IsRoot(source)) {
3484 * Look up the parent directory.
3486 nameP = uafs_LastPath(source);
3487 if (nameP != NULL) {
3488 code = uafs_LookupParent(source, &dirP);
3494 dirP = afs_CurrentDir;
3500 * Make sure the filename has at least one character
3502 if (*nameP == '\0') {
3511 usr_vattr_null(&attrs);
3512 attrs.va_type = VLNK;
3513 attrs.va_mode = 0777;
3514 attrs.va_uid = u.u_cred->cr_uid;
3515 attrs.va_gid = u.u_cred->cr_gid;
3516 code = afs_symlink(dirP, nameP, &attrs, target, u.u_cred);
3526 * Read a symbolic link into the buffer
3529 uafs_readlink(char *path, char *buf, int len)
3533 retval = uafs_readlink_r(path, buf, len);
3539 uafs_readlink_r(char *path, char *buf, int len)
3542 struct usr_vnode *vp;
3544 struct iovec iov[1];
3546 code = uafs_LookupName(path, afs_CurrentDir, &vp, 0, 0);
3552 if (vp->v_type != VLNK) {
3559 * set up the uio buffer
3561 iov[0].iov_base = buf;
3562 iov[0].iov_len = len;
3563 uio.uio_iov = &iov[0];
3567 uio.uio_fmode = FREAD;
3568 uio.uio_resid = len;
3573 code = afs_readlink(vp, &uio, u.u_cred);
3581 * return the number of bytes read
3583 return (len - uio.uio_resid);
3587 * Remove a file (or directory)
3588 * Note: file name may not end in a slash.
3591 uafs_unlink(char *path)
3595 retval = uafs_unlink_r(path);
3601 uafs_unlink_r(char *path)
3605 struct usr_vnode *fileP;
3606 struct usr_vnode *dirP;
3609 if (uafs_IsRoot(path)) {
3614 * Look up the parent directory.
3616 nameP = uafs_LastPath(path);
3617 if (nameP != NULL) {
3618 code = uafs_LookupParent(path, &dirP);
3624 dirP = afs_CurrentDir;
3630 * Make sure the filename has at least one character
3632 if (*nameP == '\0') {
3641 code = afs_remove(dirP, nameP, u.u_cred);
3652 * Rename a file (or directory)
3655 uafs_rename(char *old, char *new)
3659 retval = uafs_rename_r(old, new);
3665 uafs_rename_r(char *old, char *new)
3670 struct usr_vnode *odirP;
3671 struct usr_vnode *ndirP;
3673 if (uafs_IsRoot(new)) {
3678 * Look up the parent directories.
3680 onameP = uafs_LastPath(old);
3681 if (onameP != NULL) {
3682 code = uafs_LookupParent(old, &odirP);
3688 odirP = afs_CurrentDir;
3692 nnameP = uafs_LastPath(new);
3693 if (nnameP != NULL) {
3694 code = uafs_LookupParent(new, &ndirP);
3700 ndirP = afs_CurrentDir;
3706 * Make sure the filename has at least one character
3708 if (*onameP == '\0' || *nnameP == '\0') {
3718 code = afs_rename(odirP, onameP, ndirP, nnameP, u.u_cred);
3730 * Remove a or directory
3731 * Note: file name may not end in a slash.
3734 uafs_rmdir(char *path)
3738 retval = uafs_rmdir_r(path);
3744 uafs_rmdir_r(char *path)
3748 struct usr_vnode *fileP;
3749 struct usr_vnode *dirP;
3752 if (uafs_IsRoot(path)) {
3757 * Look up the parent directory.
3759 nameP = uafs_LastPath(path);
3760 if (nameP != NULL) {
3761 code = uafs_LookupParent(path, &dirP);
3767 dirP = afs_CurrentDir;
3773 * Make sure the directory name has at least one character
3775 if (*nameP == '\0') {
3782 * Remove the directory
3784 code = afs_rmdir(dirP, nameP, u.u_cred);
3795 * Flush a file from the AFS cache
3798 uafs_FlushFile(char *path)
3801 struct afs_ioctl iob;
3809 call_syscall(AFSCALL_PIOCTL, (long)path, _VICEIOCTL(6), (long)&iob, 0,
3820 uafs_FlushFile_r(char *path)
3824 retval = uafs_FlushFile(path);
3833 uafs_opendir(char *path)
3837 retval = uafs_opendir_r(path);
3843 uafs_opendir_r(char *path)
3846 struct usr_vnode *fileP;
3850 * Open the directory for reading
3852 fd = uafs_open_r(path, O_RDONLY, 0);
3857 fileP = afs_FileTable[fd];
3858 if (fileP == NULL) {
3862 if (fileP->v_type != VDIR) {
3869 * Set up the directory structures
3872 (usr_DIR *) afs_osi_Alloc(sizeof(usr_DIR) + USR_DIRSIZE +
3873 sizeof(struct usr_dirent));
3874 usr_assert(dirp != NULL);
3875 dirp->dd_buf = (char *)(dirp + 1);
3885 * Read directory entries into a file system independent format.
3886 * This routine was developed to support AFS cache consistency testing.
3887 * You should use uafs_readdir instead.
3890 uafs_getdents(int fd, struct min_direct *buf, int len)
3894 retval = uafs_getdents_r(fd, buf, len);
3900 uafs_getdents_r(int fd, struct min_direct *buf, int len)
3904 struct usr_vnode *vp;
3905 struct iovec iov[1];
3908 * Make sure this is an open file
3910 vp = afs_FileTable[fd];
3918 * set up the uio buffer
3920 iov[0].iov_base = (char *)buf;
3921 iov[0].iov_len = len;
3922 uio.uio_iov = &iov[0];
3924 uio.uio_offset = afs_FileOffsets[fd];
3926 uio.uio_fmode = FREAD;
3927 uio.uio_resid = len;
3930 * read the next chunk from the directory
3932 code = afs_readdir(vp, &uio, u.u_cred);
3938 afs_FileOffsets[fd] = uio.uio_offset;
3939 return (len - uio.uio_resid);
3943 * read from a directory (names only)
3946 uafs_readdir(usr_DIR * dirp)
3948 struct usr_dirent *retval;
3950 retval = uafs_readdir_r(dirp);
3956 uafs_readdir_r(usr_DIR * dirp)
3962 struct usr_vnode *vp;
3963 struct iovec iov[1];
3964 struct usr_dirent *direntP;
3965 struct min_direct *directP;
3968 * Make sure this is an open file
3970 vp = afs_FileTable[dirp->dd_fd];
3977 * If there are no entries in the stream buffer
3978 * then read another chunk
3980 directP = (struct min_direct *)(dirp->dd_buf + dirp->dd_loc);
3981 if (dirp->dd_size == 0 || directP->d_fileno == 0) {
3983 * set up the uio buffer
3985 iov[0].iov_base = dirp->dd_buf;
3986 iov[0].iov_len = USR_DIRSIZE;
3987 uio.uio_iov = &iov[0];
3989 uio.uio_offset = afs_FileOffsets[dirp->dd_fd];
3991 uio.uio_fmode = FREAD;
3992 uio.uio_resid = USR_DIRSIZE;
3995 * read the next chunk from the directory
3997 code = afs_readdir(vp, &uio, u.u_cred);
4002 afs_FileOffsets[dirp->dd_fd] = uio.uio_offset;
4004 dirp->dd_size = USR_DIRSIZE - iov[0].iov_len;
4006 directP = (struct min_direct *)(dirp->dd_buf + dirp->dd_loc);
4010 * Check for end of file
4012 if (dirp->dd_size == 0 || directP->d_fileno == 0) {
4016 len = ((sizeof(struct min_direct) + directP->d_namlen + 4) & (~3));
4017 usr_assert(len <= dirp->dd_size);
4020 * Copy the next entry into the usr_dirent structure and advance
4022 direntP = (struct usr_dirent *)(dirp->dd_buf + USR_DIRSIZE);
4023 direntP->d_ino = directP->d_fileno;
4024 direntP->d_off = direntP->d_reclen;
4026 sizeof(struct usr_dirent) - MAXNAMLEN + directP->d_namlen + 1;
4027 memcpy(&direntP->d_name[0], (void *)(directP + 1), directP->d_namlen);
4028 direntP->d_name[directP->d_namlen] = '\0';
4029 dirp->dd_loc += len;
4030 dirp->dd_size -= len;
4039 uafs_closedir(usr_DIR * dirp)
4043 retval = uafs_closedir_r(dirp);
4049 uafs_closedir_r(usr_DIR * dirp)
4055 afs_osi_Free((char *)dirp,
4056 sizeof(usr_DIR) + USR_DIRSIZE + sizeof(struct usr_dirent));
4057 rc = uafs_close_r(fd);
4062 * Do AFS authentication
4065 uafs_klog(char *user, char *cell, char *passwd, char **reason)
4068 afs_int32 password_expires = -1;
4070 usr_mutex_lock(&osi_authenticate_lock);
4072 ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION +
4073 KA_USERAUTH_DOSETPAG2, user, NULL, cell,
4074 passwd, 0, &password_expires, 0, reason);
4075 usr_mutex_unlock(&osi_authenticate_lock);
4080 uafs_klog_r(char *user, char *cell, char *passwd, char **reason)
4084 retval = uafs_klog(user, cell, passwd, reason);
4090 * Destroy AFS credentials from the kernel cache
4097 usr_mutex_lock(&osi_authenticate_lock);
4098 code = ktc_ForgetAllTokens();
4099 usr_mutex_unlock(&osi_authenticate_lock);
4108 retval = uafs_unlog();
4114 * Strip the AFS mount point from a pathname string. Return
4115 * NULL if the path is a relative pathname or if the path
4116 * doesn't start with the AFS mount point string.
4119 uafs_afsPathName(char *path)
4128 for (i = 1, p = path + 1; *p != '\0'; p++) {
4129 /* Ignore duplicate slashes */
4130 if (*p == '/' && lastchar == '/')
4132 /* Is this a subdirectory of the AFS mount point? */
4133 if (afs_mountDir[i] == '\0' && *p == '/') {
4134 /* strip leading slashes */
4135 while (*(++p) == '/');
4138 /* Reject paths that are not within AFS */
4139 if (*p != afs_mountDir[i])
4144 /* Is this the AFS mount point? */
4145 if (afs_mountDir[i] == '\0') {
4146 usr_assert(*p == '\0');
4152 #ifdef AFS_WEB_ENHANCEMENTS
4155 * klog but don't allocate a new pag
4158 uafs_klog_nopag(char *user, char *cell, char *passwd, char **reason)
4161 afs_int32 password_expires = -1;
4163 usr_mutex_lock(&osi_authenticate_lock);
4164 code = ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION
4165 /*+KA_USERAUTH_DOSETPAG2 */ , user,
4166 NULL, cell, passwd, 0,
4167 &password_expires, 0, reason);
4168 usr_mutex_unlock(&osi_authenticate_lock);
4173 * uafs_getcellstatus
4174 * get the cell status
4177 uafs_getcellstatus(char *cell, afs_int32 * status)
4180 struct afs_ioctl iob;
4183 iob.in_size = strlen(cell) + 1;
4187 rc = call_syscall(AFSCALL_PIOCTL, /*path */ 0, _VICEIOCTL(35),
4195 *status = (afs_int32) iob.out;
4201 * Get quota of volume associated with path
4204 uafs_getvolquota(char *path, afs_int32 * BlocksInUse, afs_int32 * MaxQuota)
4207 struct afs_ioctl iob;
4208 VolumeStatus *status;
4214 iob.out_size = 1024;
4216 rc = call_syscall(AFSCALL_PIOCTL, (long)path, _VICEIOCTL(4), (long)&iob,
4224 status = (VolumeStatus *) buf;
4225 *BlocksInUse = status->BlocksInUse;
4226 *MaxQuota = status->MaxQuota;
4232 * Set quota of volume associated with path
4235 uafs_setvolquota(char *path, afs_int32 MaxQuota)
4238 struct afs_ioctl iob;
4239 VolumeStatus *status;
4247 memset(buf, 0, sizeof(VolumeStatus));
4248 status = (VolumeStatus *) buf;
4249 status->MaxQuota = MaxQuota;
4250 status->MinQuota = -1;
4252 rc = call_syscall(AFSCALL_PIOCTL, (long)path, _VICEIOCTL(5), (long)&iob,
4264 * uafs_statmountpoint
4265 * Determine whether a dir. is a mount point or not
4266 * return 1 if mount point, 0 if not
4269 uafs_statmountpoint(char *path)
4276 retval = uafs_statmountpoint_r(path);
4282 uafs_statmountpoint_r(char *path)
4287 struct vrequest treq;
4290 code = uafs_LookupName(path, afs_CurrentDir, &vp, 0, 1);
4305 * Get a list of rights for the current user on path.
4308 uafs_getRights(char *path)
4315 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
4323 PRSFS_READ | PRSFS_WRITE | PRSFS_INSERT | PRSFS_LOOKUP | PRSFS_DELETE
4324 | PRSFS_LOCK | PRSFS_ADMINISTER;
4326 afs_rights = afs_getRights(vp, afs_rights, u.u_cred);
4331 #endif /* AFS_WEB_ENHANCEMENTS */
4333 #endif /* UKERNEL */