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 */
26 #include <sys/types.h>
29 #include "afsincludes.h" /* Afs-based standard headers */
30 #include "afs_usrops.h"
31 #include "afs/afs_stats.h"
33 #include "afs/cellconfig.h"
35 #include "afs/kautils.h"
36 #include "afs/afsutil.h"
37 #include "rx/rx_globals.h"
43 #define CACHEINFOFILE "cacheinfo"
44 #define AFSLOGFILE "AFSLog"
45 #define DCACHEFILE "CacheItems"
46 #define VOLINFOFILE "VolumeItems"
47 #define CELLINFOFILE "CellItems"
51 #define MIN(A,B) ((A)<(B)?(A):(B))
54 #define MAX(A,B) ((A)>(B)?(A):(B))
57 extern int cacheDiskType;
59 char afs_LclCellName[64];
61 struct usr_vnode *afs_FileTable[MAX_OSI_FILES];
62 int afs_FileFlags[MAX_OSI_FILES];
63 off_t afs_FileOffsets[MAX_OSI_FILES];
65 #define MAX_CACHE_LOOPS 4
67 struct usr_vfs afs_RootVfs;
68 struct usr_vnode *afs_RootVnode = NULL;
69 struct usr_vnode *afs_CurrentDir = NULL;
71 afs_int32 cacheBlocks; /* Num blocks in cache */
72 afs_int32 cacheFiles = 1000; /* Num files in workstation cache */
73 afs_int32 cacheStatEntries = 300; /* Num of stat cache entries */
74 char cacheBaseDir[1024]; /* AFS cache directory */
75 char confDir[1024]; /* AFS configuration directory */
76 char afs_mountDir[1024]; /* AFS mount point */
77 int afs_mountDirLen; /* strlen of AFS mount point */
78 char fullpn_DCacheFile[1024]; /* Full pathname of DCACHEFILE */
79 char fullpn_VolInfoFile[1024]; /* Full pathname of VOLINFOFILE */
80 char fullpn_CellInfoFile[1024]; /* Full pathname of CELLINFOFILE */
81 char fullpn_AFSLogFile[1024]; /* Full pathname of AFSLOGFILE */
82 char fullpn_CacheInfo[1024]; /* Full pathname of CACHEINFO */
83 char fullpn_VFile[1024]; /* Full pathname of data cache files */
84 char *vFileNumber; /* Ptr to number in file pathname */
85 char rootVolume[64] = "root.afs"; /* AFS root volume name */
86 afs_int32 isHomeCell; /* Is current cell info for home cell */
87 int createAndTrunc = O_CREAT | O_TRUNC; /* Create & truncate on open */
88 int ownerRWmode = 0600; /* Read/write OK by owner */
89 static int nDaemons = 2; /* Number of background daemons */
90 static int chunkSize = 0; /* 2^chunkSize bytes per chunk */
91 static int dCacheSize = 300; /* # of dcache entries */
92 static int vCacheSize = 50; /* # of volume cache entries */
93 static int cacheFlags = 0; /* Flags to cache manager */
94 static int preallocs = 400; /* Def # of allocated memory blocks */
95 int afsd_verbose = 0; /* Are we being chatty? */
96 int afsd_debug = 0; /* Are we printing debugging info? */
97 int afsd_CloseSynch = 0; /* Are closes synchronous or not? */
99 #define AFSD_INO_T afs_uint32
100 char **pathname_for_V; /* Array of cache file pathnames */
101 int missing_DCacheFile = 1; /* Is the DCACHEFILE missing? */
102 int missing_VolInfoFile = 1; /* Is the VOLINFOFILE missing? */
103 int missing_CellInfoFile = 1;
104 struct afs_cacheParams cparams; /* params passed to cache manager */
105 struct afsconf_dir *afs_cdir; /* config dir */
107 static int HandleMTab();
109 int afs_bufferpages = 100;
110 int usr_udpcksum = 0;
112 usr_key_t afs_global_u_key;
114 struct usr_proc *afs_global_procp = NULL;
115 struct usr_ucred *afs_global_ucredp = NULL;
116 struct usr_sysent usr_sysent[200];
118 #ifdef AFS_USR_OSF_ENV
120 #else /* AFS_USR_OSF_ENV */
122 #endif /* AFS_USR_OSF_ENV */
124 struct usr_ucred afs_osi_cred, *afs_osi_credp;
125 usr_mutex_t afs_global_lock;
126 usr_thread_t afs_global_owner;
127 usr_mutex_t rx_global_lock;
128 usr_thread_t rx_global_owner;
129 usr_mutex_t osi_inode_lock;
130 usr_mutex_t osi_waitq_lock;
131 usr_mutex_t osi_authenticate_lock;
133 afs_lock_t osi_flplock;
134 afs_lock_t osi_fsplock;
137 #ifndef NETSCAPE_NSAPI
140 * Mutex and condition variable used to implement sleep
142 pthread_mutex_t usr_sleep_mutex;
143 pthread_cond_t usr_sleep_cond;
145 #endif /* !NETSCAPE_NSAPI */
147 int call_syscall(long, long, long, long, long, long);
151 * Hash table mapping addresses onto wait structures for
152 * osi_Sleep/osi_Wakeup and osi_Wait/osi_Wakeup
154 typedef struct osi_wait {
158 struct osi_wait *next;
159 struct osi_wait *prev;
161 struct osi_wait *timedNext;
162 struct osi_wait *timedPrev;
166 * Head of the linked list of available waitq structures.
168 osi_wait_t *osi_waithash_avail;
171 * List of timed waits, NSAPI does not provide a cond_timed
172 * wait, so we need to keep track of the timed waits ourselves and
173 * periodically check for expirations
175 osi_wait_t *osi_timedwait_head;
176 osi_wait_t *osi_timedwait_tail;
181 } osi_waithash_table[OSI_WAITHASH_SIZE];
184 * Never call afs_brelse
187 ufs_brelse(struct usr_vnode *vp, struct usr_buf *bp)
194 * I am not sure what to do with these, they assert for now
197 iodone(struct usr_buf *bp)
211 * Every user is a super user
214 afs_osi_suser(void *credp)
220 afs_suser(void *credp)
226 * These are no-ops in user space
230 afs_osi_SetTime(osi_timeval_t * atv)
236 * xflock should never fall through, the only files we know
237 * about are AFS files
247 * ioctl should never fall through, the only files we know
248 * about are AFS files
258 * We do not support the inode related system calls
261 afs_syscall_icreate(long a, long b, long c, long d, long e, long f)
268 afs_syscall_iincdec(int dev, int inode, int inode_p1, int amount)
275 afs_syscall_iopen(int dev, int inode, int usrmod)
282 afs_syscall_ireadwrite(void)
289 * these routines are referenced in the vfsops structure, but
290 * should never get called
321 * uiomove copies data between kernel buffers and uio buffers
324 usr_uiomove(char *kbuf, int n, int rw, struct usr_uio *uio)
331 nio = uio->uio_iovcnt;
341 while (nio > 0 && n > 0) {
342 len = MIN(n, iovp->iov_len);
343 if (rw == UIO_READ) {
344 memcpy(iovp->iov_base, ptr, len);
346 memcpy(ptr, iovp->iov_base, len);
350 uio->uio_resid -= len;
351 uio->uio_offset += len;
352 iovp->iov_base = (char *)(iovp->iov_base) + len;
353 iovp->iov_len -= len;
364 * routines to manage user credentials
367 usr_crcopy(struct usr_ucred *credp)
369 struct usr_ucred *newcredp;
371 newcredp = (struct usr_ucred *)afs_osi_Alloc(sizeof(struct usr_ucred));
373 newcredp->cr_ref = 1;
380 struct usr_ucred *newcredp;
382 newcredp = (struct usr_ucred *)afs_osi_Alloc(sizeof(struct usr_ucred));
383 newcredp->cr_ref = 1;
388 usr_crfree(struct usr_ucred *credp)
391 if (credp->cr_ref == 0) {
392 afs_osi_Free((char *)credp, sizeof(struct usr_ucred));
398 usr_crhold(struct usr_ucred *credp)
405 usr_vattr_null(struct usr_vattr *vap)
410 n = sizeof(struct usr_vattr);
418 * Initialize the thread specific data used to simulate the
419 * kernel environment for each thread. The user structure
420 * is stored in the thread specific data.
423 uafs_InitThread(void)
426 struct usr_user *uptr;
429 * initialize the thread specific user structure. Use malloc to
430 * allocate the data block, so pthread_finish can free the buffer
431 * when this thread terminates.
434 (struct usr_user *)malloc(sizeof(struct usr_user) +
435 sizeof(struct usr_ucred));
436 usr_assert(uptr != NULL);
439 uptr->u_procp = afs_global_procp;
440 uptr->u_cred = (struct usr_ucred *)(uptr + 1);
441 *uptr->u_cred = *afs_global_ucredp;
442 st = usr_setspecific(afs_global_u_key, (void *)uptr);
447 * routine to get the user structure from the thread specific data.
448 * this routine is used to implement the global 'u' structure. Initializes
449 * the thread if needed.
452 get_user_struct(void)
454 struct usr_user *uptr;
456 st = usr_getspecific(afs_global_u_key, (void **)&uptr);
460 st = usr_getspecific(afs_global_u_key, (void **)&uptr);
462 usr_assert(uptr != NULL);
468 * Hash an address for the waithash table
470 #define WAITHASH(X) \
471 (((long)(X)^((long)(X)>>4)^((long)(X)<<4))&(OSI_WAITHASH_SIZE-1))
477 afs_osi_Sleep(void *x)
481 int glockOwner = ISAFS_GLOCK();
483 usr_mutex_lock(&osi_waitq_lock);
488 if (osi_waithash_avail == NULL) {
489 waitp = (osi_wait_t *) afs_osi_Alloc(sizeof(osi_wait_t));
490 usr_cond_init(&waitp->cond);
492 waitp = osi_waithash_avail;
493 osi_waithash_avail = osi_waithash_avail->next;
497 DLL_INSERT_TAIL(waitp, osi_waithash_table[index].head,
498 osi_waithash_table[index].tail, next, prev);
499 waitp->expiration = 0;
500 waitp->timedNext = NULL;
501 waitp->timedPrev = NULL;
502 while (waitp->flag == 0) {
503 usr_cond_wait(&waitp->cond, &osi_waitq_lock);
505 DLL_DELETE(waitp, osi_waithash_table[index].head,
506 osi_waithash_table[index].tail, next, prev);
507 waitp->next = osi_waithash_avail;
508 osi_waithash_avail = waitp;
509 usr_mutex_unlock(&osi_waitq_lock);
516 afs_osi_SleepSig(void *x)
523 afs_osi_Wakeup(void *x)
529 usr_mutex_lock(&osi_waitq_lock);
530 waitp = osi_waithash_table[index].head;
532 if (waitp->addr == x && waitp->flag == 0) {
534 usr_cond_signal(&waitp->cond);
538 usr_mutex_unlock(&osi_waitq_lock);
543 afs_osi_Wait(afs_int32 msec, struct afs_osi_WaitHandle *handle, int intok)
549 int glockOwner = ISAFS_GLOCK();
551 tv.tv_sec = msec / 1000;
552 tv.tv_nsec = (msec % 1000) * 1000000;
553 if (handle == NULL) {
557 usr_thread_sleep(&tv);
563 usr_mutex_lock(&osi_waitq_lock);
567 index = WAITHASH((caddr_t) handle);
568 if (osi_waithash_avail == NULL) {
569 waitp = (osi_wait_t *) afs_osi_Alloc(sizeof(osi_wait_t));
570 usr_cond_init(&waitp->cond);
572 waitp = osi_waithash_avail;
573 osi_waithash_avail = osi_waithash_avail->next;
575 waitp->addr = (caddr_t) handle;
577 DLL_INSERT_TAIL(waitp, osi_waithash_table[index].head,
578 osi_waithash_table[index].tail, next, prev);
579 tv.tv_sec += time(NULL);
580 waitp->expiration = tv.tv_sec + ((tv.tv_nsec == 0) ? 0 : 1);
581 DLL_INSERT_TAIL(waitp, osi_timedwait_head, osi_timedwait_tail,
582 timedNext, timedPrev);
583 usr_cond_wait(&waitp->cond, &osi_waitq_lock);
589 DLL_DELETE(waitp, osi_waithash_table[index].head,
590 osi_waithash_table[index].tail, next, prev);
591 DLL_DELETE(waitp, osi_timedwait_head, osi_timedwait_tail, timedNext,
593 waitp->next = osi_waithash_avail;
594 osi_waithash_avail = waitp;
595 usr_mutex_unlock(&osi_waitq_lock);
604 afs_osi_CancelWait(struct afs_osi_WaitHandle *handle)
606 afs_osi_Wakeup(handle);
610 * Netscape NSAPI doesn't have a cond_timed_wait, so we need
611 * to explicitly signal cond_timed_waits when their timers expire
614 afs_osi_CheckTimedWaits(void)
619 curTime = time(NULL);
620 usr_mutex_lock(&osi_waitq_lock);
621 waitp = osi_timedwait_head;
622 while (waitp != NULL) {
623 usr_assert(waitp->expiration != 0);
624 if (waitp->expiration <= curTime) {
626 usr_cond_signal(&waitp->cond);
628 waitp = waitp->timedNext;
630 usr_mutex_unlock(&osi_waitq_lock);
635 * I-node numbers are indeces into a table containing a filename
636 * i-node structure and a vnode structure. When we create an i-node,
637 * we copy the name into the array and initialize enough of the fields
638 * in the inode and vnode structures to get the client to work.
641 struct usr_inode i_node;
644 osi_file_table_t *osi_file_table;
646 int max_osi_files = 0;
649 * Allocate a slot in the file table if there is not one there already,
650 * copy in the file name and kludge up the vnode and inode structures
653 lookupname(char *fnamep, int segflg, int followlink,
654 struct usr_vnode **compvpp)
658 struct usr_inode *ip;
659 struct usr_vnode *vp;
661 /*usr_assert(followlink == 0); */
664 * Assume relative pathnames refer to files in AFS
666 if (*fnamep != '/' || uafs_afsPathName(fnamep) != NULL) {
668 code = uafs_LookupName(fnamep, afs_CurrentDir, compvpp, 0, 0);
673 usr_mutex_lock(&osi_inode_lock);
675 for (i = 0; i < n_osi_files; i++) {
676 if (strcmp(fnamep, osi_file_table[i].name) == 0) {
677 *compvpp = &osi_file_table[i].i_node.i_vnode;
678 (*compvpp)->v_count++;
679 usr_mutex_unlock(&osi_inode_lock);
684 if (n_osi_files == max_osi_files) {
685 usr_mutex_unlock(&osi_inode_lock);
689 osi_file_table[n_osi_files].name = afs_osi_Alloc(strlen(fnamep) + 1);
690 usr_assert(osi_file_table[n_osi_files].name != NULL);
691 strcpy(osi_file_table[n_osi_files].name, fnamep);
692 ip = &osi_file_table[i].i_node;
694 vp->v_data = (caddr_t) ip;
697 ip->i_number = n_osi_files;
699 usr_mutex_unlock(&osi_inode_lock);
705 * open a file given its i-node number
708 osi_UFSOpen(afs_dcache_id_t *ino)
716 if (ino->ufs > n_osi_files) {
722 fp = (struct osi_file *)afs_osi_Alloc(sizeof(struct osi_file));
723 usr_assert(fp != NULL);
724 fp->fd = open(osi_file_table[ino->ufs - 1].name, O_RDWR | O_CREAT, 0);
727 afs_osi_Free((char *)fp, sizeof(struct osi_file));
731 rc = fstat(fp->fd, &st);
734 afs_osi_Free((void *)fp, sizeof(struct osi_file));
738 fp->size = st.st_size;
741 fp->vnode = (struct usr_vnode *)fp;
748 osi_UFSClose(struct osi_file *fp)
758 afs_osi_Free((void *)fp, sizeof(struct osi_file));
762 afs_osi_Free((void *)fp, sizeof(struct osi_file));
768 osi_UFSTruncate(struct osi_file *fp, afs_int32 len)
775 rc = ftruncate(fp->fd, len);
787 afs_osi_Read(struct osi_file *fp, int offset, void *buf, afs_int32 len)
797 rc = lseek(fp->fd, offset, SEEK_SET);
799 rc = lseek(fp->fd, fp->offset, SEEK_SET);
807 ret = read(fp->fd, buf, len);
814 rc = fstat(fp->fd, &st);
820 fp->size = st.st_size;
826 afs_osi_Write(struct osi_file *fp, afs_int32 offset, void *buf, afs_int32 len)
836 rc = lseek(fp->fd, offset, SEEK_SET);
838 rc = lseek(fp->fd, fp->offset, SEEK_SET);
846 ret = write(fp->fd, buf, len);
853 rc = fstat(fp->fd, &st);
859 fp->size = st.st_size;
865 afs_osi_Stat(struct osi_file *fp, struct osi_stat *stp)
871 rc = fstat(fp->fd, &st);
877 stp->size = st.st_size;
878 stp->mtime = st.st_mtime;
879 stp->atime = st.st_atime;
888 afs_osi_VOP_RDWR(struct usr_vnode *vnodeP, struct usr_uio *uioP, int rw,
889 int flags, struct usr_ucred *credP)
892 struct osi_file *fp = (struct osi_file *)vnodeP;
895 * We don't support readv/writev.
897 usr_assert(uioP->uio_iovcnt == 1);
898 usr_assert(uioP->uio_resid == uioP->uio_iov[0].iov_len);
900 if (rw == UIO_WRITE) {
901 usr_assert(uioP->uio_fmode == FWRITE);
902 rc = afs_osi_Write(fp, uioP->uio_offset, uioP->uio_iov[0].iov_base,
903 uioP->uio_iov[0].iov_len);
905 usr_assert(uioP->uio_fmode == FREAD);
906 rc = afs_osi_Read(fp, uioP->uio_offset, uioP->uio_iov[0].iov_base,
907 uioP->uio_iov[0].iov_len);
913 uioP->uio_resid -= rc;
914 uioP->uio_offset += rc;
915 uioP->uio_iov[0].iov_base = (char *)(uioP->uio_iov[0].iov_base) + rc;
916 uioP->uio_iov[0].iov_len -= rc;
921 * Use malloc/free routines with check patterns before and after each block
924 static char *afs_check_string1 = "UAFS";
925 static char *afs_check_string2 = "AFS_OSI_";
928 afs_osi_Alloc(size_t size)
934 afs_osi_Free(void *ptr, size_t size)
940 afs_osi_FreeStr(char *ptr)
946 osi_AllocLargeSpace(size_t size)
948 AFS_STATCNT(osi_AllocLargeSpace);
949 return afs_osi_Alloc(size);
953 osi_FreeLargeSpace(void *ptr)
955 AFS_STATCNT(osi_FreeLargeSpace);
956 afs_osi_Free(ptr, 0);
960 osi_AllocSmallSpace(size_t size)
962 AFS_STATCNT(osi_AllocSmallSpace);
963 return afs_osi_Alloc(size);
967 osi_FreeSmallSpace(void *ptr)
969 AFS_STATCNT(osi_FreeSmallSpace);
970 afs_osi_Free(ptr, 0);
976 AFS_STATCNT(shutdown_osi);
981 shutdown_osinet(void)
983 AFS_STATCNT(shutdown_osinet);
988 shutdown_osifile(void)
990 AFS_STATCNT(shutdown_osifile);
995 afs_nfsclient_init(void)
1000 shutdown_nfsclnt(void)
1006 afs_osi_Invisible(void)
1012 afs_osi_Visible(void)
1018 osi_GetTime(struct timeval *tv)
1020 gettimeofday(tv, NULL);
1025 osi_SetTime(struct timeval *tv)
1031 osi_Active(struct vcache *avc)
1033 AFS_STATCNT(osi_Active);
1040 afs_osi_MapStrategy(int (*aproc) (), struct usr_buf *bp)
1042 afs_int32 returnCode;
1043 returnCode = (*aproc) (bp);
1048 osi_FlushPages(register struct vcache *avc, struct AFS_UCRED *credp)
1050 ObtainSharedLock(&avc->lock, 555);
1051 if ((hcmp((avc->f.m.DataVersion), (avc->mapDV)) <= 0)
1052 || ((avc->execsOrWriters > 0) && afs_DirtyPages(avc))) {
1053 ReleaseSharedLock(&avc->lock);
1056 UpgradeSToWLock(&avc->lock, 565);
1057 hset(avc->mapDV, avc->f.m.DataVersion);
1058 ReleaseWriteLock(&avc->lock);
1063 osi_FlushText_really(register struct vcache *vp)
1065 if (hcmp(vp->f.m.DataVersion, vp->flushDV) > 0) {
1066 hset(vp->flushDV, vp->f.m.DataVersion);
1072 osi_SyncVM(struct vcache *avc)
1078 osi_ReleaseVM(struct vcache *avc, int len, struct usr_ucred *credp)
1091 * Allocate the table used to implement psuedo-inodes.
1093 max_osi_files = cacheFiles + 100;
1094 osi_file_table = (osi_file_table_t *)
1095 afs_osi_Alloc(max_osi_files * sizeof(osi_file_table_t));
1096 usr_assert(osi_file_table != NULL);
1098 #ifndef NETSCAPE_NSAPI
1100 * Initialize the mutex and condition variable used to implement
1103 pthread_mutex_init(&usr_sleep_mutex, NULL);
1104 pthread_cond_init(&usr_sleep_cond, NULL);
1105 #endif /* !NETSCAPE_NSAPI */
1108 * Initialize the hash table used for sleep/wakeup
1110 for (i = 0; i < OSI_WAITHASH_SIZE; i++) {
1111 DLL_INIT_LIST(osi_waithash_table[i].head, osi_waithash_table[i].tail);
1113 DLL_INIT_LIST(osi_timedwait_head, osi_timedwait_tail);
1114 osi_waithash_avail = NULL;
1117 * Initialize the AFS file table
1119 for (i = 0; i < MAX_OSI_FILES; i++) {
1120 afs_FileTable[i] = NULL;
1124 * Initialize the global locks
1126 usr_mutex_init(&afs_global_lock);
1127 usr_mutex_init(&rx_global_lock);
1128 usr_mutex_init(&osi_inode_lock);
1129 usr_mutex_init(&osi_waitq_lock);
1130 usr_mutex_init(&osi_authenticate_lock);
1133 * Initialize the AFS OSI credentials
1135 afs_osi_cred = *afs_global_ucredp;
1136 afs_osi_credp = &afs_osi_cred;
1139 /* ParseArgs is now obsolete, being handled by cmd */
1141 /*---------------------------------------------------------------------
1145 * Given the final component of a filename expected to be a data cache file,
1146 * return the integer corresponding to the file. Note: we reject names that
1147 * are not a ``V'' followed by an integer. We also reject those names having
1148 * the right format but lying outside the range [0..cacheFiles-1].
1151 * fname : Char ptr to the filename to parse.
1154 * >= 0 iff the file is really a data cache file numbered from 0 to cacheFiles-1, or
1158 * Nothing interesting.
1162 *------------------------------------------------------------------------*/
1165 GetVFileNumber(char *fname)
1167 int computedVNumber; /*The computed file number we return */
1168 int filenameLen; /*Number of chars in filename */
1169 int currDigit; /*Current digit being processed */
1172 * The filename must have at least two characters, the first of which must be a ``V''
1173 * and the second of which cannot be a zero unless the file is exactly two chars long.
1175 filenameLen = strlen(fname);
1176 if (filenameLen < 2)
1178 if (fname[0] != 'V')
1180 if ((filenameLen > 2) && (fname[1] == '0'))
1184 * Scan through the characters in the given filename, failing immediately if a non-digit
1187 for (currDigit = 1; currDigit < filenameLen; currDigit++)
1188 if (isdigit(fname[currDigit]) == 0)
1192 * All relevant characters are digits. Pull out the decimal number they represent.
1193 * Reject it if it's out of range, otherwise return it.
1195 computedVNumber = atoi(++fname);
1196 if (computedVNumber < cacheFiles)
1197 return (computedVNumber);
1202 /*---------------------------------------------------------------------
1206 * Given a full pathname for a file we need to create for the workstation AFS
1207 * cache, go ahead and create the file.
1210 * fname : Full pathname of file to create.
1213 * 0 iff the file was created,
1217 * The given cache file has been found to be missing.
1221 *------------------------------------------------------------------------*/
1224 CreateCacheFile(char *fname)
1226 static char rn[] = "CreateCacheFile"; /*Routine name */
1227 int cfd; /*File descriptor to AFS cache file */
1228 int closeResult; /*Result of close() */
1231 printf("%s: Creating cache file '%s'\n", rn, fname);
1232 cfd = open(fname, createAndTrunc, ownerRWmode);
1234 printf("%s: Can't create '%s', error return is %d (%d)\n", rn, fname,
1238 closeResult = close(cfd);
1241 ("%s: Can't close newly-created AFS cache file '%s' (code %d)\n",
1249 /*---------------------------------------------------------------------
1253 * Sweep through the AFS cache directory, recording the inode number for
1254 * each valid data cache file there. Also, delete any file that doesn't beint32
1255 * in the cache directory during this sweep, and remember which of the other
1256 * residents of this directory were seen. After the sweep, we create any data
1257 * cache files that were missing.
1260 * vFilesFound : Set to the number of data cache files found.
1263 * 0 if everything went well,
1267 * This routine may be called several times. If the number of data cache files
1268 * found is less than the global cacheFiles, then the caller will need to call it
1269 * again to record the inodes of the missing zero-length data cache files created
1270 * in the previous call.
1273 * Fills up the global pathname_for_V array, may create and/or
1274 * delete files as explained above.
1275 *------------------------------------------------------------------------*/
1278 SweepAFSCache(int *vFilesFound)
1280 static char rn[] = "SweepAFSCache"; /*Routine name */
1281 char fullpn_FileToDelete[1024]; /*File to be deleted from cache */
1282 char *fileToDelete; /*Ptr to last component of above */
1283 DIR *cdirp; /*Ptr to cache directory structure */
1285 struct dirent *currp; /*Current directory entry */
1286 int vFileNum; /*Data cache file's associated number */
1288 if (cacheFlags & AFSCALL_INIT_MEMCACHE) {
1290 printf("%s: Memory Cache, no cache sweep done\n", rn);
1296 printf("%s: Opening cache directory '%s'\n", rn, cacheBaseDir);
1298 if (chmod(cacheBaseDir, 0700)) { /* force it to be 700 */
1299 printf("%s: Can't 'chmod 0700' the cache dir, '%s'.\n", rn,
1303 cdirp = opendir(cacheBaseDir);
1304 if (cdirp == (DIR *) 0) {
1305 printf("%s: Can't open AFS cache directory, '%s'.\n", rn,
1311 * Scan the directory entries, remembering data cache file inodes and the existance
1312 * of other important residents. Delete all files that don't belong here.
1315 sprintf(fullpn_FileToDelete, "%s/", cacheBaseDir);
1316 fileToDelete = fullpn_FileToDelete + strlen(fullpn_FileToDelete);
1318 for (currp = readdir(cdirp); currp; currp = readdir(cdirp)) {
1320 printf("%s: Current directory entry:\n", rn);
1321 #if defined(AFS_USR_DFBSD_ENV)
1322 printf("\tinode=%d, name='%s'\n", currp->d_ino,
1325 printf("\tinode=%d, reclen=%d, name='%s'\n", currp->d_ino,
1326 currp->d_reclen, currp->d_name);
1331 * Guess current entry is for a data cache file.
1333 vFileNum = GetVFileNumber(currp->d_name);
1334 if (vFileNum >= 0) {
1336 * Found a valid data cache filename. Remember this file's name
1337 * and bump the number of files found.
1339 pathname_for_V[vFileNum] =
1340 afs_osi_Alloc(strlen(currp->d_name) + strlen(cacheBaseDir) +
1342 usr_assert(pathname_for_V[vFileNum] != NULL);
1343 sprintf(pathname_for_V[vFileNum], "%s/%s", cacheBaseDir,
1346 } else if (strcmp(currp->d_name, DCACHEFILE) == 0) {
1348 * Found the file holding the dcache entries.
1350 missing_DCacheFile = 0;
1351 } else if (strcmp(currp->d_name, VOLINFOFILE) == 0) {
1353 * Found the file holding the volume info.
1355 missing_VolInfoFile = 0;
1356 } else if (strcmp(currp->d_name, CELLINFOFILE) == 0) {
1357 missing_CellInfoFile = 0;
1358 } else if ((strcmp(currp->d_name, ".") == 0)
1359 || (strcmp(currp->d_name, "..") == 0)
1360 || (strcmp(currp->d_name, "lost+found") == 0)) {
1362 * Don't do anything - this file is legit, and is to be left alone.
1366 * This file doesn't belong in the cache. Nuke it.
1368 sprintf(fileToDelete, "%s", currp->d_name);
1370 printf("%s: Deleting '%s'\n", rn, fullpn_FileToDelete);
1371 if (unlink(fullpn_FileToDelete)) {
1372 printf("%s: Can't unlink '%s', errno is %d\n", rn,
1373 fullpn_FileToDelete, errno);
1379 * Create all the cache files that are missing.
1381 if (missing_DCacheFile) {
1383 printf("%s: Creating '%s'\n", rn, fullpn_DCacheFile);
1384 if (CreateCacheFile(fullpn_DCacheFile))
1385 printf("%s: Can't create '%s'\n", rn, fullpn_DCacheFile);
1387 if (missing_VolInfoFile) {
1389 printf("%s: Creating '%s'\n", rn, fullpn_VolInfoFile);
1390 if (CreateCacheFile(fullpn_VolInfoFile))
1391 printf("%s: Can't create '%s'\n", rn, fullpn_VolInfoFile);
1393 if (missing_CellInfoFile) {
1395 printf("%s: Creating '%s'\n", rn, fullpn_CellInfoFile);
1396 if (CreateCacheFile(fullpn_CellInfoFile))
1397 printf("%s: Can't create '%s'\n", rn, fullpn_CellInfoFile);
1400 if (*vFilesFound < cacheFiles) {
1402 * We came up short on the number of data cache files found. Scan through the inode
1403 * list and create all missing files.
1405 for (vFileNum = 0; vFileNum < cacheFiles; vFileNum++)
1406 if (pathname_for_V[vFileNum] == (AFSD_INO_T) 0) {
1407 sprintf(vFileNumber, "%d", vFileNum);
1409 printf("%s: Creating '%s'\n", rn, fullpn_VFile);
1410 if (CreateCacheFile(fullpn_VFile))
1411 printf("%s: Can't create '%s'\n", rn, fullpn_VFile);
1416 * Close the directory, return success.
1419 printf("%s: Closing cache directory.\n", rn);
1425 ConfigCell(register struct afsconf_cell *aci, void *arock,
1426 struct afsconf_dir *adir)
1428 register int isHomeCell;
1430 afs_int32 cellFlags = 0;
1431 afs_int32 hosts[MAXHOSTSPERCELL];
1433 /* figure out if this is the home cell */
1434 isHomeCell = (strcmp(aci->name, afs_LclCellName) == 0);
1436 cellFlags = 2; /* not home, suid is forbidden */
1438 /* build address list */
1439 for (i = 0; i < MAXHOSTSPERCELL; i++)
1440 memcpy(&hosts[i], &aci->hostAddr[i].sin_addr, sizeof(afs_int32));
1442 if (aci->linkedCell)
1443 cellFlags |= 4; /* Flag that linkedCell arg exists,
1444 * for upwards compatibility */
1446 /* configure one cell */
1447 call_syscall(AFSCALL_CALL, AFSOP_ADDCELL2, (long)hosts, /* server addresses */
1448 (long)aci->name, /* cell name */
1449 (long)cellFlags, /* is this the home cell? */
1450 (long)aci->linkedCell); /* Linked cell, if any */
1455 ConfigCellAlias(struct afsconf_cellalias *aca, void *arock, struct afsconf_dir *adir)
1457 call_syscall(AFSOP_ADDCELLALIAS, (long)aca->aliasName,
1458 (long)aca->realName, 0, 0, 0);
1463 * Set the UDP port number RX uses for UDP datagrams
1466 uafs_SetRxPort(int port)
1468 usr_assert(usr_rx_port == 0);
1474 * Initialize the user space client.
1477 uafs_Init(char *rn, char *mountDirParam, char *confDirParam,
1478 char *cacheBaseDirParam, int cacheBlocksParam, int cacheFilesParam,
1479 int cacheStatEntriesParam, int dCacheSizeParam, int vCacheSizeParam,
1480 int chunkSizeParam, int closeSynchParam, int debugParam,
1481 int nDaemonsParam, int cacheFlagsParam, char *logFile)
1484 struct usr_proc *procp;
1485 struct usr_ucred *ucredp;
1488 int currVFile; /* Current AFS cache file number */
1489 int lookupResult; /* Result of GetLocalCellName() */
1490 int cacheIteration; /* cache verification loop counter */
1491 int vFilesFound; /* Num data cache files found in sweep */
1493 afs_int32 vfs1_type = -1;
1494 struct afs_ioctl iob;
1498 afs_int32 buffer[MAXIPADDRS];
1499 afs_int32 maskbuffer[MAXIPADDRS];
1500 afs_int32 mtubuffer[MAXIPADDRS];
1503 * Use the thread specific data to implement the user structure
1505 usr_keycreate(&afs_global_u_key, free);
1508 * Initialize the global ucred structure
1510 afs_global_ucredp = (struct usr_ucred *)
1511 afs_osi_Alloc(sizeof(struct usr_ucred));
1512 usr_assert(afs_global_ucredp != NULL);
1513 afs_global_ucredp->cr_ref = 1;
1514 afs_global_ucredp->cr_uid = geteuid();
1515 afs_global_ucredp->cr_gid = getegid();
1516 afs_global_ucredp->cr_ruid = getuid();
1517 afs_global_ucredp->cr_rgid = getgid();
1518 afs_global_ucredp->cr_suid = afs_global_ucredp->cr_ruid;
1519 afs_global_ucredp->cr_sgid = afs_global_ucredp->cr_rgid;
1520 st = getgroups(NGROUPS, &afs_global_ucredp->cr_groups[0]);
1521 usr_assert(st >= 0);
1522 afs_global_ucredp->cr_ngroups = (unsigned long)st;
1523 for (i = st; i < NGROUPS; i++) {
1524 afs_global_ucredp->cr_groups[i] = NOGROUP;
1528 * Initialize the global process structure
1530 afs_global_procp = (struct usr_proc *)
1531 afs_osi_Alloc(sizeof(struct usr_proc));
1532 usr_assert(afs_global_procp != NULL);
1533 afs_global_procp->p_pid = getpid();
1534 afs_global_procp->p_ppid = (pid_t) 1;
1535 afs_global_procp->p_ucred = afs_global_ucredp;
1538 * Initialize the AFS mount point, default is '/afs'.
1539 * Strip duplicate/trailing slashes from mount point string.
1540 * afs_mountDirLen is set to strlen(afs_mountDir).
1542 if (mountDirParam) {
1543 sprintf(tbuffer, "%s", mountDirParam);
1545 sprintf(tbuffer, "afs");
1547 afs_mountDir[0] = '/';
1548 afs_mountDirLen = 1;
1549 for (lastchar = '/', p = &tbuffer[0]; *p != '\0'; p++) {
1550 if (lastchar != '/' || *p != '/') {
1551 afs_mountDir[afs_mountDirLen++] = lastchar = *p;
1554 if (lastchar == '/' && afs_mountDirLen > 1)
1556 afs_mountDir[afs_mountDirLen] = '\0';
1557 usr_assert(afs_mountDirLen > 1);
1560 * Initialize cache parameters using the input arguments
1563 cacheBlocks = cacheBlocksParam;
1564 if (cacheFilesParam != 0) {
1565 cacheFiles = cacheFilesParam;
1567 cacheFiles = cacheBlocks / 10;
1569 if (cacheStatEntriesParam != 0) {
1570 cacheStatEntries = cacheStatEntriesParam;
1572 strcpy(cacheBaseDir, cacheBaseDirParam);
1573 if (nDaemonsParam != 0) {
1574 nDaemons = nDaemonsParam;
1578 afsd_verbose = debugParam;
1579 afsd_debug = debugParam;
1580 chunkSize = chunkSizeParam;
1581 if (dCacheSizeParam != 0) {
1582 dCacheSize = dCacheSizeParam;
1584 dCacheSize = cacheFiles / 2;
1586 if (vCacheSizeParam != 0) {
1587 vCacheSize = vCacheSizeParam;
1589 strcpy(confDir, confDirParam);
1590 afsd_CloseSynch = closeSynchParam;
1591 if (cacheFlagsParam >= 0) {
1592 cacheFlags = cacheFlagsParam;
1594 if (cacheFlags & AFSCALL_INIT_MEMCACHE) {
1595 cacheFiles = dCacheSize;
1598 sprintf(fullpn_CacheInfo, "%s/%s", confDir, CACHEINFOFILE);
1599 if (logFile == NULL) {
1600 sprintf(fullpn_AFSLogFile, "%s/%s", confDir, AFSLOGFILE);
1602 strcpy(fullpn_AFSLogFile, logFile);
1605 printf("\n%s: Initializing user space AFS client\n\n", rn);
1606 printf(" mountDir: %s\n", afs_mountDir);
1607 printf(" confDir: %s\n", confDir);
1608 printf(" cacheBaseDir: %s\n", cacheBaseDir);
1609 printf(" cacheBlocks: %d\n", cacheBlocks);
1610 printf(" cacheFiles: %d\n", cacheFiles);
1611 printf(" cacheStatEntries: %d\n", cacheStatEntries);
1612 printf(" dCacheSize: %d\n", dCacheSize);
1613 printf(" vCacheSize: %d\n", vCacheSize);
1614 printf(" chunkSize: %d\n", chunkSize);
1615 printf(" afsd_CloseSynch: %d\n", afsd_CloseSynch);
1616 printf(" afsd_debug/verbose: %d/%d\n", afsd_debug, afsd_verbose);
1617 printf(" nDaemons: %d\n", nDaemons);
1618 printf(" cacheFlags: %d\n", cacheFlags);
1619 printf(" logFile: %s\n", fullpn_AFSLogFile);
1624 * Initialize the AFS client
1629 * Pull out all the configuration info for the workstation's AFS cache and
1630 * the cellular community we're willing to let our users see.
1632 afs_cdir = afsconf_Open(confDir);
1634 printf("afsd: some file missing or bad in %s\n", confDir);
1639 afsconf_GetLocalCell(afs_cdir, afs_LclCellName,
1640 sizeof(afs_LclCellName));
1642 printf("%s: Can't get my home cell name! [Error is %d]\n", rn,
1646 printf("%s: My home cell is '%s'\n", rn, afs_LclCellName);
1649 if ((logfd = fopen(fullpn_AFSLogFile, "r+")) == 0) {
1651 printf("%s: Creating '%s'\n", rn, fullpn_AFSLogFile);
1652 if (CreateCacheFile(fullpn_AFSLogFile)) {
1654 ("%s: Can't create '%s' (You may want to use the -logfile option)\n",
1655 rn, fullpn_AFSLogFile);
1662 * Create and zero the pathname table for the desired cache files.
1664 pathname_for_V = (char **)afs_osi_Alloc(cacheFiles * sizeof(char *));
1665 if (pathname_for_V == NULL) {
1666 printf("%s: malloc() failed for cache file table with %d entries.\n",
1670 memset(pathname_for_V, 0, (cacheFiles * sizeof(char *)));
1672 printf("%s: %d pathname_for_V entries at 0x%x, %d bytes\n", rn,
1673 cacheFiles, pathname_for_V, (cacheFiles * sizeof(AFSD_INO_T)));
1676 * Set up all the pathnames we'll need for later.
1678 sprintf(fullpn_DCacheFile, "%s/%s", cacheBaseDir, DCACHEFILE);
1679 sprintf(fullpn_VolInfoFile, "%s/%s", cacheBaseDir, VOLINFOFILE);
1680 sprintf(fullpn_CellInfoFile, "%s/%s", cacheBaseDir, CELLINFOFILE);
1681 sprintf(fullpn_VFile, "%s/V", cacheBaseDir);
1682 vFileNumber = fullpn_VFile + strlen(fullpn_VFile);
1684 /* initialize AFS callback interface */
1686 /* parse multihomed address files */
1688 st = parseNetFiles((afs_uint32*)buffer,(afs_uint32*) maskbuffer, (afs_uint32*)mtubuffer, MAXIPADDRS, reason,
1689 AFSDIR_CLIENT_NETINFO_FILEPATH,
1690 AFSDIR_CLIENT_NETRESTRICT_FILEPATH);
1692 call_syscall(AFSCALL_CALL, AFSOP_ADVISEADDR, st,
1693 (long)(&buffer[0]), (long)(&maskbuffer[0]),
1694 (long)(&mtubuffer[0]));
1696 printf("ADVISEADDR: Error in specifying interface addresses:%s\n",
1703 * Start the RX listener.
1706 printf("%s: Calling AFSOP_RXLISTENER_DAEMON\n", rn);
1707 fork_syscall(AFSCALL_CALL, AFSOP_RXLISTENER_DAEMON, FALSE, FALSE, FALSE);
1710 printf("%s: Forking rx callback listener.\n", rn);
1712 if (preallocs < cacheStatEntries + 50)
1713 preallocs = cacheStatEntries + 50;
1714 fork_syscall(AFSCALL_CALL, AFSOP_START_RXCALLBACK, preallocs);
1717 * Start the RX event handler.
1720 printf("%s: Calling AFSOP_RXEVENT_DAEMON\n", rn);
1721 fork_syscall(AFSCALL_CALL, AFSOP_RXEVENT_DAEMON, FALSE);
1724 * Set up all the kernel processes needed for AFS.
1728 printf("%s: Initializing AFS daemon.\n", rn);
1729 call_syscall(AFSCALL_CALL, AFSOP_BASIC_INIT, 1, 0, 0, 0);
1732 * Tell the kernel some basic information about the workstation's cache.
1735 printf("%s: Calling AFSOP_CACHEINIT: %d stat cache entries,"
1736 " %d optimum cache files, %d blocks in the cache,"
1737 " flags = 0x%x, dcache entries %d\n", rn, cacheStatEntries,
1738 cacheFiles, cacheBlocks, cacheFlags, dCacheSize);
1739 memset(&cparams, 0, sizeof(cparams));
1740 cparams.cacheScaches = cacheStatEntries;
1741 cparams.cacheFiles = cacheFiles;
1742 cparams.cacheBlocks = cacheBlocks;
1743 cparams.cacheDcaches = dCacheSize;
1744 cparams.cacheVolumes = vCacheSize;
1745 cparams.chunkSize = chunkSize;
1746 cparams.setTimeFlag = FALSE;
1747 cparams.memCacheFlag = cacheFlags;
1748 call_syscall(AFSCALL_CALL, AFSOP_CACHEINIT, (long)&cparams, 0, 0, 0);
1749 if (afsd_CloseSynch)
1750 call_syscall(AFSCALL_CALL, AFSOP_CLOSEWAIT, 0, 0, 0, 0);
1753 * Sweep the workstation AFS cache directory, remembering the inodes of
1754 * valid files and deleting extraneous files. Keep sweeping until we
1755 * have the right number of data cache files or we've swept too many
1759 printf("%s: Sweeping workstation's AFS cache directory.\n", rn);
1761 /* Memory-cache based system doesn't need any of this */
1762 if (!(cacheFlags & AFSCALL_INIT_MEMCACHE)) {
1765 if (SweepAFSCache(&vFilesFound)) {
1766 printf("%s: Error on sweep %d of workstation AFS cache \
1767 directory.\n", rn, cacheIteration);
1772 ("%s: %d out of %d data cache files found in sweep %d.\n",
1773 rn, vFilesFound, cacheFiles, cacheIteration);
1774 } while ((vFilesFound < cacheFiles)
1775 && (cacheIteration < MAX_CACHE_LOOPS));
1776 } else if (afsd_verbose)
1777 printf("%s: Using memory cache, not swept\n", rn);
1780 * Pass the kernel the name of the workstation cache file holding the
1784 printf("%s: Calling AFSOP_CACHEINFO: dcache file is '%s'\n", rn,
1786 /* once again, meaningless for a memory-based cache. */
1787 if (!(cacheFlags & AFSCALL_INIT_MEMCACHE))
1788 call_syscall(AFSCALL_CALL, AFSOP_CACHEINFO, (long)fullpn_DCacheFile,
1791 call_syscall(AFSCALL_CALL, AFSOP_CELLINFO, (long)fullpn_CellInfoFile, 0,
1795 * Pass the kernel the name of the workstation cache file holding the
1796 * volume information.
1799 printf("%s: Calling AFSOP_VOLUMEINFO: volume info file is '%s'\n", rn,
1800 fullpn_VolInfoFile);
1801 call_syscall(AFSCALL_CALL, AFSOP_VOLUMEINFO, (long)fullpn_VolInfoFile, 0,
1805 * Pass the kernel the name of the afs logging file holding the volume
1809 printf("%s: Calling AFSOP_AFSLOG: volume info file is '%s'\n", rn,
1811 if (!(cacheFlags & AFSCALL_INIT_MEMCACHE)) /* ... nor this ... */
1812 call_syscall(AFSCALL_CALL, AFSOP_AFSLOG, (long)fullpn_AFSLogFile, 0,
1816 * Tell the kernel about each cell in the configuration.
1818 afsconf_CellApply(afs_cdir, ConfigCell, NULL);
1819 afsconf_CellAliasApply(afs_cdir, ConfigCellAlias, NULL);
1822 * Set the primary cell name.
1824 call_syscall(AFSCALL_CALL, AFSOP_SET_THISCELL, (long)afs_LclCellName, 0, 0, 0);
1827 printf("%s: Forking AFS daemon.\n", rn);
1828 fork_syscall(AFSCALL_CALL, AFSOP_START_AFS);
1831 printf("%s: Forking check server daemon.\n", rn);
1832 fork_syscall(AFSCALL_CALL, AFSOP_START_CS);
1835 printf("%s: Forking %d background daemons.\n", rn, nDaemons);
1836 for (i = 0; i < nDaemons; i++) {
1837 fork_syscall(AFSCALL_CALL, AFSOP_START_BKG);
1841 printf("%s: Calling AFSOP_ROOTVOLUME with '%s'\n", rn, rootVolume);
1842 call_syscall(AFSCALL_CALL, AFSOP_ROOTVOLUME, (long)rootVolume, 0, 0, 0);
1845 * Give the kernel the names of the AFS files cached on the workstation's
1850 ("%s: Calling AFSOP_CACHEFILES for each of the %d files in '%s'\n",
1851 rn, cacheFiles, cacheBaseDir);
1852 if (!(cacheFlags & AFSCALL_INIT_MEMCACHE)) /* ... and again ... */
1853 for (currVFile = 0; currVFile < cacheFiles; currVFile++) {
1854 call_syscall(AFSCALL_CALL, AFSOP_CACHEFILE,
1855 (long)pathname_for_V[currVFile], 0, 0, 0);
1858 /*#ifndef NETSCAPE_NSAPI*/
1860 /* this breaks solaris if the kernel-mode client has never been installed,
1861 * and it doesn't seem to work now anyway, so just disable it */
1864 * Copy our tokens from the kernel to the user space client
1866 for (i = 0; i < 200; i++) {
1868 * Get the i'th token from the kernel
1870 memset((void *)&tbuffer[0], 0, sizeof(tbuffer));
1871 memcpy((void *)&tbuffer[0], (void *)&i, sizeof(int));
1873 iob.in_size = sizeof(int);
1875 iob.out_size = sizeof(tbuffer);
1877 #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)
1878 rc = syscall(AFS_SYSCALL, AFSCALL_PIOCTL, 0, _VICEIOCTL(8), &iob, 0);
1879 #elif defined(AFS_USR_SGI_ENV)
1880 rc = syscall(AFS_PIOCTL, 0, _VICEIOCTL(8), &iob, 0);
1881 #else /* AFS_USR_AIX_ENV */
1882 rc = lpioctl(0, _VICEIOCTL(8), &iob, 0);
1885 usr_assert(errno == EDOM || errno == ENOSYS || errno == ERANGE);
1890 * Now pass the token into the user space kernel
1892 rc = uafs_SetTokens(tbuffer, iob.out_size);
1893 usr_assert(rc == 0);
1895 #endif /* !NETSCAPE_NSAPI */
1898 * All the necessary info has been passed into the kernel to run an AFS
1899 * system. Give the kernel our go-ahead.
1902 printf("%s: Calling AFSOP_GO\n", rn);
1903 call_syscall(AFSCALL_CALL, AFSOP_GO, FALSE, 0, 0, 0);
1906 * At this point, we have finished passing the kernel all the info
1907 * it needs to set up the AFS. Mount the AFS root.
1909 printf("%s: All AFS daemons started.\n", rn);
1912 printf("%s: Forking trunc-cache daemon.\n", rn);
1913 fork_syscall(AFSCALL_CALL, AFSOP_START_TRUNCDAEMON);
1916 * Mount the AFS filesystem
1919 rc = afs_mount(&afs_RootVfs, NULL, NULL);
1920 usr_assert(rc == 0);
1921 rc = afs_root(&afs_RootVfs, &afs_RootVnode);
1922 usr_assert(rc == 0);
1926 * initialize the current directory to the AFS root
1928 afs_CurrentDir = afs_RootVnode;
1929 VN_HOLD(afs_CurrentDir);
1942 VN_RELE(afs_CurrentDir);
1943 rc = afs_unmount(&afs_RootVfs);
1944 usr_assert(rc == 0);
1951 * Donate the current thread to the RX server pool.
1954 uafs_RxServerProc(void)
1958 struct rx_call *newcall = NULL;
1960 rxi_MorePackets(2); /* alloc more packets */
1961 threadID = rxi_availProcs++;
1964 sock = OSI_NULLSOCKET;
1965 rxi_ServerProc(threadID, newcall, &sock);
1966 if (sock == OSI_NULLSOCKET) {
1971 rxi_ListenerProc(sock, &threadID, &newcall);
1972 /* assert(threadID != -1); */
1973 /* assert(newcall != NULL); */
1977 struct syscallThreadArgs {
1986 #ifdef NETSCAPE_NSAPI
1988 syscallThread(void *argp)
1989 #else /* NETSCAPE_NSAPI */
1991 syscallThread(void *argp)
1992 #endif /* NETSCAPE_NSAPI */
1995 struct usr_ucred *crp;
1996 struct syscallThreadArgs *sysArgsP = (struct syscallThreadArgs *)argp;
1999 * AFS daemons run authenticated
2001 u.u_viceid = getuid();
2003 crp->cr_uid = getuid();
2004 crp->cr_ruid = getuid();
2005 crp->cr_suid = getuid();
2006 crp->cr_groups[0] = getgid();
2007 crp->cr_ngroups = 1;
2008 for (i = 1; i < NGROUPS; i++) {
2009 crp->cr_groups[i] = NOGROUP;
2012 call_syscall(sysArgsP->syscall, sysArgsP->afscall, sysArgsP->param1,
2013 sysArgsP->param2, sysArgsP->param3, sysArgsP->param4);
2015 afs_osi_Free(argp, -1);
2019 fork_syscall(syscall, afscall, param1, param2, param3, param4)
2020 long syscall, afscall, param1, param2, param3, param4;
2023 struct syscallThreadArgs *sysArgsP;
2025 sysArgsP = (struct syscallThreadArgs *)
2026 afs_osi_Alloc(sizeof(struct syscallThreadArgs));
2027 usr_assert(sysArgsP != NULL);
2028 sysArgsP->syscall = syscall;
2029 sysArgsP->afscall = afscall;
2030 sysArgsP->param1 = param1;
2031 sysArgsP->param2 = param2;
2032 sysArgsP->param3 = param3;
2033 sysArgsP->param4 = param4;
2035 usr_thread_create(&tid, syscallThread, sysArgsP);
2036 usr_thread_detach(tid);
2040 call_syscall(syscall, afscall, param1, param2, param3, param4)
2041 long syscall, afscall, param1, param2, param3, param4;
2053 a.syscall = syscall;
2054 a.afscall = afscall;
2061 u.u_ap = (char *)&a;
2063 code = Afs_syscall();
2068 uafs_SetTokens(char *tbuffer, int tlen)
2071 struct afs_ioctl iob;
2076 iob.out = &outbuf[0];
2077 iob.out_size = sizeof(outbuf);
2079 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(3), (long)&iob, 0, 0);
2088 uafs_RPCStatsEnableProc(void)
2091 struct afs_ioctl iob;
2094 flag = AFSCALL_RXSTATS_ENABLE;
2095 iob.in = (char *)&flag;
2096 iob.in_size = sizeof(afs_int32);
2099 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(53), (long)&iob, 0, 0);
2108 uafs_RPCStatsDisableProc(void)
2111 struct afs_ioctl iob;
2114 flag = AFSCALL_RXSTATS_DISABLE;
2115 iob.in = (char *)&flag;
2116 iob.in_size = sizeof(afs_int32);
2119 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(53), (long)&iob, 0, 0);
2128 uafs_RPCStatsClearProc(void)
2131 struct afs_ioctl iob;
2134 flag = AFSCALL_RXSTATS_CLEAR;
2135 iob.in = (char *)&flag;
2136 iob.in_size = sizeof(afs_int32);
2139 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(53), (long)&iob, 0, 0);
2148 uafs_RPCStatsEnablePeer(void)
2151 struct afs_ioctl iob;
2154 flag = AFSCALL_RXSTATS_ENABLE;
2155 iob.in = (char *)&flag;
2156 iob.in_size = sizeof(afs_int32);
2159 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(54), (long)&iob, 0, 0);
2168 uafs_RPCStatsDisablePeer(void)
2171 struct afs_ioctl iob;
2174 flag = AFSCALL_RXSTATS_DISABLE;
2175 iob.in = (char *)&flag;
2176 iob.in_size = sizeof(afs_int32);
2179 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(54), (long)&iob, 0, 0);
2188 uafs_RPCStatsClearPeer(void)
2191 struct afs_ioctl iob;
2194 flag = AFSCALL_RXSTATS_CLEAR;
2195 iob.in = (char *)&flag;
2196 iob.in_size = sizeof(afs_int32);
2199 rc = call_syscall(AFSCALL_PIOCTL, 0, _VICEIOCTL(54), (long)&iob, 0, 0);
2208 * Lookup a file or directory given its path.
2209 * Call VN_HOLD on the output vnode if successful.
2210 * Returns zero on success, error code on failure.
2212 * Note: Caller must hold the AFS global lock.
2215 uafs_LookupName(char *path, struct usr_vnode *parentVp,
2216 struct usr_vnode **vpp, int follow, int no_eval_mtpt)
2220 struct usr_vnode *vp;
2221 struct usr_vnode *nextVp;
2222 struct usr_vnode *linkVp;
2225 char *nextPathP = NULL;
2230 * Absolute paths must start with the AFS mount point.
2232 if (path[0] != '/') {
2235 path = uafs_afsPathName(path);
2243 * Loop through the path looking for the new directory
2245 tmpPath = afs_osi_Alloc(strlen(path) + 1);
2246 usr_assert(tmpPath != NULL);
2247 strcpy(tmpPath, path);
2250 while (pathP != NULL && *pathP != '\0') {
2251 usr_assert(*pathP != '/');
2254 * terminate the current component and skip over slashes
2256 nextPathP = afs_strchr(pathP, '/');
2257 if (nextPathP != NULL) {
2258 while (*nextPathP == '/') {
2259 *(nextPathP++) = '\0';
2264 * Don't call afs_lookup on non-directories
2266 if (vp->v_type != VDIR) {
2268 afs_osi_Free(tmpPath, strlen(path) + 1);
2272 if (vp == afs_RootVnode && strcmp(pathP, "..") == 0) {
2274 * The AFS root is its own parent
2276 nextVp = afs_RootVnode;
2279 * We need execute permission to search a directory
2281 code = afs_access(VTOAFS(vp), VEXEC, u.u_cred);
2284 afs_osi_Free(tmpPath, strlen(path) + 1);
2289 * lookup the next component in the path, we can release the
2290 * subdirectory since we hold the global lock
2293 #ifdef AFS_WEB_ENHANCEMENTS
2294 if ((nextPathP != NULL && *nextPathP != '\0') || !no_eval_mtpt)
2295 code = afs_lookup(vp, pathP, &nextVp, u.u_cred, 0);
2298 afs_lookup(vp, pathP, &nextVp, u.u_cred,
2301 code = afs_lookup(vp, pathP, &nextVp, u.u_cred, 0);
2302 #endif /* AFS_WEB_ENHANCEMENTS */
2305 afs_osi_Free(tmpPath, strlen(path) + 1);
2311 * Follow symbolic links for parent directories and
2312 * for leaves when the follow flag is set.
2314 if ((nextPathP != NULL && *nextPathP != '\0') || follow) {
2316 while (nextVp->v_type == VLNK) {
2317 if (++linkCount > MAX_OSI_LINKS) {
2320 afs_osi_Free(tmpPath, strlen(path) + 1);
2323 code = uafs_LookupLink(nextVp, vp, &linkVp);
2327 afs_osi_Free(tmpPath, strlen(path) + 1);
2341 * Special case, nextPathP is non-null if pathname ends in slash
2343 if (nextPathP != NULL && vp->v_type != VDIR) {
2345 afs_osi_Free(tmpPath, strlen(path) + 1);
2349 afs_osi_Free(tmpPath, strlen(path) + 1);
2355 * Lookup the target of a symbolic link
2356 * Call VN_HOLD on the output vnode if successful.
2357 * Returns zero on success, error code on failure.
2359 * Note: Caller must hold the AFS global lock.
2362 uafs_LookupLink(struct usr_vnode *vp, struct usr_vnode *parentVp,
2363 struct usr_vnode **vpp)
2368 struct usr_vnode *linkVp;
2370 struct iovec iov[1];
2374 pathP = afs_osi_Alloc(MAX_OSI_PATH + 1);
2375 usr_assert(pathP != NULL);
2378 * set up the uio buffer
2380 iov[0].iov_base = pathP;
2381 iov[0].iov_len = MAX_OSI_PATH + 1;
2382 uio.uio_iov = &iov[0];
2386 uio.uio_fmode = FREAD;
2387 uio.uio_resid = MAX_OSI_PATH + 1;
2390 * Read the link data
2392 code = afs_readlink(vp, &uio, u.u_cred);
2394 afs_osi_Free(pathP, MAX_OSI_PATH + 1);
2397 len = MAX_OSI_PATH + 1 - uio.uio_resid;
2401 * Find the target of the symbolic link
2403 code = uafs_LookupName(pathP, parentVp, &linkVp, 1, 0);
2405 afs_osi_Free(pathP, MAX_OSI_PATH + 1);
2409 afs_osi_Free(pathP, MAX_OSI_PATH + 1);
2415 * Lookup the parent of a file or directory given its path
2416 * Call VN_HOLD on the output vnode if successful.
2417 * Returns zero on success, error code on failure.
2419 * Note: Caller must hold the AFS global lock.
2422 uafs_LookupParent(char *path, struct usr_vnode **vpp)
2427 struct usr_vnode *parentP;
2432 * Absolute path names must start with the AFS mount point.
2435 pathP = uafs_afsPathName(path);
2436 if (pathP == NULL) {
2442 * Find the length of the parent path
2445 while (len > 0 && path[len - 1] == '/') {
2451 while (len > 0 && path[len - 1] != '/') {
2458 pathP = afs_osi_Alloc(len);
2459 usr_assert(pathP != NULL);
2460 memcpy(pathP, path, len - 1);
2461 pathP[len - 1] = '\0';
2464 * look up the parent
2466 code = uafs_LookupName(pathP, afs_CurrentDir, &parentP, 1, 0);
2467 afs_osi_Free(pathP, len);
2471 if (parentP->v_type != VDIR) {
2481 * Return a pointer to the first character in the last component
2485 uafs_LastPath(char *path)
2490 while (len > 0 && path[len - 1] == '/') {
2493 while (len > 0 && path[len - 1] != '/') {
2503 * Set the working directory.
2506 uafs_chdir(char *path)
2510 retval = uafs_chdir_r(path);
2516 uafs_chdir_r(char *path)
2521 code = uafs_LookupName(path, afs_CurrentDir, &dirP, 1, 0);
2526 if (dirP->v_type != VDIR) {
2531 VN_RELE(afs_CurrentDir);
2532 afs_CurrentDir = dirP;
2537 * Create a directory.
2540 uafs_mkdir(char *path, int mode)
2544 retval = uafs_mkdir_r(path, mode);
2550 uafs_mkdir_r(char *path, int mode)
2554 struct vnode *parentP;
2556 struct usr_vattr attrs;
2558 if (uafs_IsRoot(path)) {
2563 * Look up the parent directory.
2565 nameP = uafs_LastPath(path);
2566 if (nameP != NULL) {
2567 code = uafs_LookupParent(path, &parentP);
2573 parentP = afs_CurrentDir;
2579 * Make sure the directory has at least one character
2581 if (*nameP == '\0') {
2588 * Create the directory
2590 usr_vattr_null(&attrs);
2591 attrs.va_type = VREG;
2592 attrs.va_mode = mode;
2593 attrs.va_uid = u.u_cred->cr_uid;
2594 attrs.va_gid = u.u_cred->cr_gid;
2596 code = afs_mkdir(parentP, nameP, &attrs, &dirP, u.u_cred);
2607 * Return 1 if path is the AFS root, otherwise return 0
2610 uafs_IsRoot(char *path)
2612 while (*path == '/' && *(path + 1) == '/') {
2615 if (strncmp(path, afs_mountDir, afs_mountDirLen) != 0) {
2618 path += afs_mountDirLen;
2619 while (*path == '/') {
2622 if (*path != '\0') {
2630 * Note: file name may not end in a slash.
2633 uafs_open(char *path, int flags, int mode)
2637 retval = uafs_open_r(path, flags, mode);
2643 uafs_open_r(char *path, int flags, int mode)
2649 struct usr_vnode *fileP;
2650 struct usr_vnode *dirP;
2651 struct usr_vattr attrs;
2656 if (uafs_IsRoot(path)) {
2657 fileP = afs_RootVnode;
2661 * Look up the parent directory.
2663 nameP = uafs_LastPath(path);
2664 if (nameP != NULL) {
2665 code = uafs_LookupParent(path, &dirP);
2671 dirP = afs_CurrentDir;
2677 * Make sure the filename has at least one character
2679 if (*nameP == '\0') {
2686 * Get the VNODE for this file
2688 if (flags & O_CREAT) {
2689 usr_vattr_null(&attrs);
2690 attrs.va_type = VREG;
2691 attrs.va_mode = mode;
2692 attrs.va_uid = u.u_cred->cr_uid;
2693 attrs.va_gid = u.u_cred->cr_gid;
2694 if (flags & O_TRUNC) {
2700 afs_create(VTOAFS(dirP), nameP, &attrs,
2701 (flags & O_EXCL) ? usr_EXCL : usr_NONEXCL, mode,
2711 code = uafs_LookupName(nameP, dirP, &fileP, 1, 0);
2719 * Check whether we have access to this file
2722 if (flags & (O_RDONLY | O_RDWR)) {
2725 if (flags & (O_WRONLY | O_RDWR)) {
2729 fileMode = VREAD; /* since O_RDONLY is 0 */
2730 code = afs_access(VTOAFS(fileP), fileMode, u.u_cred);
2738 * Get the file attributes, all we need is the size
2740 code = afs_getattr(VTOAFS(fileP), &attrs, u.u_cred);
2750 * Setup the open flags
2753 if (flags & O_TRUNC) {
2754 openFlags |= FTRUNC;
2756 if (flags & O_APPEND) {
2757 openFlags |= FAPPEND;
2759 if (flags & O_SYNC) {
2762 if (flags & O_SYNC) {
2765 if (flags & (O_RDONLY | O_RDWR)) {
2768 if (flags & (O_WRONLY | O_RDWR)) {
2769 openFlags |= FWRITE;
2771 if ((openFlags & (FREAD | FWRITE)) == 0) {
2772 /* O_RDONLY is 0, so ... */
2777 * Truncate if necessary
2779 if ((flags & O_TRUNC) && (attrs.va_size != 0)) {
2780 usr_vattr_null(&attrs);
2781 attrs.va_mask = ATTR_SIZE;
2783 code = afs_setattr(VTOAFS(fileP), &attrs, u.u_cred);
2795 code = afs_open(&vc, openFlags, u.u_cred);
2803 * Put the vnode pointer into the file table
2805 for (fd = 0; fd < MAX_OSI_FILES; fd++) {
2806 if (afs_FileTable[fd] == NULL) {
2807 afs_FileTable[fd] = fileP;
2808 afs_FileFlags[fd] = openFlags;
2809 if (flags & O_APPEND) {
2810 afs_FileOffsets[fd] = attrs.va_size;
2812 afs_FileOffsets[fd] = 0;
2817 if (fd == MAX_OSI_FILES) {
2830 uafs_creat(char *path, int mode)
2833 rc = uafs_open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
2838 uafs_creat_r(char *path, int mode)
2841 rc = uafs_open_r(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
2849 uafs_write(int fd, char *buf, int len)
2853 retval = uafs_pwrite_r(fd, buf, len, afs_FileOffsets[fd]);
2859 uafs_pwrite(int fd, char *buf, int len, off_t offset)
2863 retval = uafs_pwrite_r(fd, buf, len, offset);
2869 uafs_pwrite_r(int fd, char *buf, int len, off_t offset)
2873 struct iovec iov[1];
2874 struct usr_vnode *fileP;
2877 * Make sure this is an open file
2879 fileP = afs_FileTable[fd];
2880 if (fileP == NULL) {
2886 * set up the uio buffer
2888 iov[0].iov_base = buf;
2889 iov[0].iov_len = len;
2890 uio.uio_iov = &iov[0];
2892 uio.uio_offset = offset;
2894 uio.uio_fmode = FWRITE;
2895 uio.uio_resid = len;
2901 code = afs_write(VTOAFS(fileP), &uio, afs_FileFlags[fd], u.u_cred, 0);
2907 afs_FileOffsets[fd] = uio.uio_offset;
2908 return (len - uio.uio_resid);
2915 uafs_read(int fd, char *buf, int len)
2919 retval = uafs_pread_r(fd, buf, len, afs_FileOffsets[fd]);
2925 uafs_pread(int fd, char *buf, int len, off_t offset)
2929 retval = uafs_pread_r(fd, buf, len, offset);
2935 uafs_pread_r(int fd, char *buf, int len, off_t offset)
2939 struct iovec iov[1];
2940 struct usr_vnode *fileP;
2941 struct usr_buf *bufP;
2944 * Make sure this is an open file
2946 fileP = afs_FileTable[fd];
2947 if (fileP == NULL) {
2953 * set up the uio buffer
2955 iov[0].iov_base = buf;
2956 iov[0].iov_len = len;
2957 uio.uio_iov = &iov[0];
2959 uio.uio_offset = offset;
2961 uio.uio_fmode = FREAD;
2962 uio.uio_resid = len;
2967 code = afs_read(VTOAFS(fileP), &uio, u.u_cred, 0, &bufP, 0);
2973 afs_FileOffsets[fd] = uio.uio_offset;
2974 return (len - uio.uio_resid);
2978 * Copy the attributes of a file into a stat structure.
2980 * NOTE: Caller must hold the global AFS lock.
2983 uafs_GetAttr(struct usr_vnode *vp, struct stat *stats)
2986 struct usr_vattr attrs;
2991 * Get the attributes
2993 code = afs_getattr(VTOAFS(vp), &attrs, u.u_cred);
2999 * Copy the attributes, zero fields that aren't set
3001 memset((void *)stats, 0, sizeof(struct stat));
3003 stats->st_ino = attrs.va_nodeid;
3004 stats->st_mode = attrs.va_mode;
3005 stats->st_nlink = attrs.va_nlink;
3006 stats->st_uid = attrs.va_uid;
3007 stats->st_gid = attrs.va_gid;
3008 stats->st_rdev = attrs.va_rdev;
3009 stats->st_size = attrs.va_size;
3010 stats->st_atime = attrs.va_atime.tv_sec;
3011 stats->st_mtime = attrs.va_mtime.tv_sec;
3012 stats->st_ctime = attrs.va_ctime.tv_sec;
3013 stats->st_blksize = attrs.va_blocksize;
3014 stats->st_blocks = attrs.va_blocks;
3020 * Get the attributes of a file, do follow links
3023 uafs_stat(char *path, struct stat *buf)
3027 retval = uafs_stat_r(path, buf);
3033 uafs_stat_r(char *path, struct stat *buf)
3038 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
3043 code = uafs_GetAttr(vp, buf);
3053 * Get the attributes of a file, don't follow links
3056 uafs_lstat(char *path, struct stat *buf)
3060 retval = uafs_lstat_r(path, buf);
3066 uafs_lstat_r(char *path, struct stat *buf)
3071 code = uafs_LookupName(path, afs_CurrentDir, &vp, 0, 0);
3076 code = uafs_GetAttr(vp, buf);
3086 * Get the attributes of an open file
3089 uafs_fstat(int fd, struct stat *buf)
3093 retval = uafs_fstat_r(fd, buf);
3099 uafs_fstat_r(int fd, struct stat *buf)
3104 vp = afs_FileTable[fd];
3109 code = uafs_GetAttr(vp, buf);
3118 * change the permissions on a file
3121 uafs_chmod(char *path, int mode)
3125 retval = uafs_chmod_r(path, mode);
3131 uafs_chmod_r(char *path, int mode)
3135 struct usr_vattr attrs;
3137 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
3142 usr_vattr_null(&attrs);
3143 attrs.va_mask = ATTR_MODE;
3144 attrs.va_mode = mode;
3145 code = afs_setattr(VTOAFS(vp), &attrs, u.u_cred);
3155 * change the permissions on an open file
3158 uafs_fchmod(int fd, int mode)
3162 retval = uafs_fchmod_r(fd, mode);
3168 uafs_fchmod_r(int fd, int mode)
3172 struct usr_vattr attrs;
3174 vp = afs_FileTable[fd];
3179 usr_vattr_null(&attrs);
3180 attrs.va_mask = ATTR_MODE;
3181 attrs.va_mode = mode;
3182 code = afs_setattr(VTOAFS(vp), &attrs, u.u_cred);
3194 uafs_truncate(char *path, int length)
3198 retval = uafs_truncate_r(path, length);
3204 uafs_truncate_r(char *path, int length)
3208 struct usr_vattr attrs;
3210 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
3215 usr_vattr_null(&attrs);
3216 attrs.va_mask = ATTR_SIZE;
3217 attrs.va_size = length;
3218 code = afs_setattr(VTOAFS(vp), &attrs, u.u_cred);
3228 * truncate an open file
3231 uafs_ftruncate(int fd, int length)
3235 retval = uafs_ftruncate_r(fd, length);
3241 uafs_ftruncate_r(int fd, int length)
3245 struct usr_vattr attrs;
3247 vp = afs_FileTable[fd];
3252 usr_vattr_null(&attrs);
3253 attrs.va_mask = ATTR_SIZE;
3254 attrs.va_size = length;
3255 code = afs_setattr(VTOAFS(vp), &attrs, u.u_cred);
3264 * set the read/write file pointer of an open file
3267 uafs_lseek(int fd, int offset, int whence)
3271 retval = uafs_lseek_r(fd, offset, whence);
3277 uafs_lseek_r(int fd, int offset, int whence)
3281 struct usr_vattr attrs;
3282 struct usr_vnode *vp;
3284 vp = afs_FileTable[fd];
3291 newpos = afs_FileOffsets[fd] + offset;
3297 code = afs_getattr(VTOAFS(vp), &attrs, u.u_cred);
3302 newpos = attrs.va_size + offset;
3312 afs_FileOffsets[fd] = newpos;
3324 retval = uafs_fsync_r(fd);
3330 uafs_fsync_r(int fd)
3333 struct usr_vnode *fileP;
3336 fileP = afs_FileTable[fd];
3337 if (fileP == NULL) {
3342 code = afs_fsync(fileP, u.u_cred);
3359 retval = uafs_close_r(fd);
3365 uafs_close_r(int fd)
3368 struct usr_vnode *fileP;
3370 fileP = afs_FileTable[fd];
3371 if (fileP == NULL) {
3375 afs_FileTable[fd] = NULL;
3377 code = afs_close(fileP, afs_FileFlags[fd], u.u_cred);
3388 * Create a hard link from the source to the target
3389 * Note: file names may not end in a slash.
3392 uafs_link(char *existing, char *new)
3396 retval = uafs_link_r(existing, new);
3402 uafs_link_r(char *existing, char *new)
3405 struct usr_vnode *existP;
3406 struct usr_vnode *dirP;
3409 if (uafs_IsRoot(new)) {
3414 * Look up the existing node.
3416 code = uafs_LookupName(existing, afs_CurrentDir, &existP, 1, 0);
3423 * Look up the parent directory.
3425 nameP = uafs_LastPath(new);
3426 if (nameP != NULL) {
3427 code = uafs_LookupParent(new, &dirP);
3434 dirP = afs_CurrentDir;
3440 * Make sure the filename has at least one character
3442 if (*nameP == '\0') {
3452 code = afs_link(existP, dirP, nameP, u.u_cred);
3463 * Create a symbolic link from the source to the target
3464 * Note: file names may not end in a slash.
3467 uafs_symlink(char *target, char *source)
3471 retval = uafs_symlink_r(target, source);
3477 uafs_symlink_r(char *target, char *source)
3480 struct usr_vnode *dirP;
3481 struct usr_vattr attrs;
3484 if (uafs_IsRoot(source)) {
3489 * Look up the parent directory.
3491 nameP = uafs_LastPath(source);
3492 if (nameP != NULL) {
3493 code = uafs_LookupParent(source, &dirP);
3499 dirP = afs_CurrentDir;
3505 * Make sure the filename has at least one character
3507 if (*nameP == '\0') {
3516 usr_vattr_null(&attrs);
3517 attrs.va_type = VLNK;
3518 attrs.va_mode = 0777;
3519 attrs.va_uid = u.u_cred->cr_uid;
3520 attrs.va_gid = u.u_cred->cr_gid;
3521 code = afs_symlink(dirP, nameP, &attrs, target, u.u_cred);
3531 * Read a symbolic link into the buffer
3534 uafs_readlink(char *path, char *buf, int len)
3538 retval = uafs_readlink_r(path, buf, len);
3544 uafs_readlink_r(char *path, char *buf, int len)
3547 struct usr_vnode *vp;
3549 struct iovec iov[1];
3551 code = uafs_LookupName(path, afs_CurrentDir, &vp, 0, 0);
3557 if (vp->v_type != VLNK) {
3564 * set up the uio buffer
3566 iov[0].iov_base = buf;
3567 iov[0].iov_len = len;
3568 uio.uio_iov = &iov[0];
3572 uio.uio_fmode = FREAD;
3573 uio.uio_resid = len;
3578 code = afs_readlink(vp, &uio, u.u_cred);
3586 * return the number of bytes read
3588 return (len - uio.uio_resid);
3592 * Remove a file (or directory)
3593 * Note: file name may not end in a slash.
3596 uafs_unlink(char *path)
3600 retval = uafs_unlink_r(path);
3606 uafs_unlink_r(char *path)
3610 struct usr_vnode *fileP;
3611 struct usr_vnode *dirP;
3614 if (uafs_IsRoot(path)) {
3619 * Look up the parent directory.
3621 nameP = uafs_LastPath(path);
3622 if (nameP != NULL) {
3623 code = uafs_LookupParent(path, &dirP);
3629 dirP = afs_CurrentDir;
3635 * Make sure the filename has at least one character
3637 if (*nameP == '\0') {
3646 code = afs_remove(dirP, nameP, u.u_cred);
3657 * Rename a file (or directory)
3660 uafs_rename(char *old, char *new)
3664 retval = uafs_rename_r(old, new);
3670 uafs_rename_r(char *old, char *new)
3675 struct usr_vnode *odirP;
3676 struct usr_vnode *ndirP;
3678 if (uafs_IsRoot(new)) {
3683 * Look up the parent directories.
3685 onameP = uafs_LastPath(old);
3686 if (onameP != NULL) {
3687 code = uafs_LookupParent(old, &odirP);
3693 odirP = afs_CurrentDir;
3697 nnameP = uafs_LastPath(new);
3698 if (nnameP != NULL) {
3699 code = uafs_LookupParent(new, &ndirP);
3705 ndirP = afs_CurrentDir;
3711 * Make sure the filename has at least one character
3713 if (*onameP == '\0' || *nnameP == '\0') {
3723 code = afs_rename(odirP, onameP, ndirP, nnameP, u.u_cred);
3735 * Remove a or directory
3736 * Note: file name may not end in a slash.
3739 uafs_rmdir(char *path)
3743 retval = uafs_rmdir_r(path);
3749 uafs_rmdir_r(char *path)
3753 struct usr_vnode *fileP;
3754 struct usr_vnode *dirP;
3757 if (uafs_IsRoot(path)) {
3762 * Look up the parent directory.
3764 nameP = uafs_LastPath(path);
3765 if (nameP != NULL) {
3766 code = uafs_LookupParent(path, &dirP);
3772 dirP = afs_CurrentDir;
3778 * Make sure the directory name has at least one character
3780 if (*nameP == '\0') {
3787 * Remove the directory
3789 code = afs_rmdir(dirP, nameP, u.u_cred);
3800 * Flush a file from the AFS cache
3803 uafs_FlushFile(char *path)
3806 struct afs_ioctl iob;
3814 call_syscall(AFSCALL_PIOCTL, (long)path, _VICEIOCTL(6), (long)&iob, 0,
3825 uafs_FlushFile_r(char *path)
3829 retval = uafs_FlushFile(path);
3838 uafs_opendir(char *path)
3842 retval = uafs_opendir_r(path);
3848 uafs_opendir_r(char *path)
3851 struct usr_vnode *fileP;
3855 * Open the directory for reading
3857 fd = uafs_open_r(path, O_RDONLY, 0);
3862 fileP = afs_FileTable[fd];
3863 if (fileP == NULL) {
3867 if (fileP->v_type != VDIR) {
3874 * Set up the directory structures
3877 (usr_DIR *) afs_osi_Alloc(sizeof(usr_DIR) + USR_DIRSIZE +
3878 sizeof(struct usr_dirent));
3879 usr_assert(dirp != NULL);
3880 dirp->dd_buf = (char *)(dirp + 1);
3890 * Read directory entries into a file system independent format.
3891 * This routine was developed to support AFS cache consistency testing.
3892 * You should use uafs_readdir instead.
3895 uafs_getdents(int fd, struct min_direct *buf, int len)
3899 retval = uafs_getdents_r(fd, buf, len);
3905 uafs_getdents_r(int fd, struct min_direct *buf, int len)
3909 struct usr_vnode *vp;
3910 struct iovec iov[1];
3913 * Make sure this is an open file
3915 vp = afs_FileTable[fd];
3923 * set up the uio buffer
3925 iov[0].iov_base = (char *)buf;
3926 iov[0].iov_len = len;
3927 uio.uio_iov = &iov[0];
3929 uio.uio_offset = afs_FileOffsets[fd];
3931 uio.uio_fmode = FREAD;
3932 uio.uio_resid = len;
3935 * read the next chunk from the directory
3937 code = afs_readdir(vp, &uio, u.u_cred);
3943 afs_FileOffsets[fd] = uio.uio_offset;
3944 return (len - uio.uio_resid);
3948 * read from a directory (names only)
3951 uafs_readdir(usr_DIR * dirp)
3953 struct usr_dirent *retval;
3955 retval = uafs_readdir_r(dirp);
3961 uafs_readdir_r(usr_DIR * dirp)
3967 struct usr_vnode *vp;
3968 struct iovec iov[1];
3969 struct usr_dirent *direntP;
3970 struct min_direct *directP;
3973 * Make sure this is an open file
3975 vp = afs_FileTable[dirp->dd_fd];
3982 * If there are no entries in the stream buffer
3983 * then read another chunk
3985 directP = (struct min_direct *)(dirp->dd_buf + dirp->dd_loc);
3986 if (dirp->dd_size == 0 || directP->d_fileno == 0) {
3988 * set up the uio buffer
3990 iov[0].iov_base = dirp->dd_buf;
3991 iov[0].iov_len = USR_DIRSIZE;
3992 uio.uio_iov = &iov[0];
3994 uio.uio_offset = afs_FileOffsets[dirp->dd_fd];
3996 uio.uio_fmode = FREAD;
3997 uio.uio_resid = USR_DIRSIZE;
4000 * read the next chunk from the directory
4002 code = afs_readdir(vp, &uio, u.u_cred);
4007 afs_FileOffsets[dirp->dd_fd] = uio.uio_offset;
4009 dirp->dd_size = USR_DIRSIZE - iov[0].iov_len;
4011 directP = (struct min_direct *)(dirp->dd_buf + dirp->dd_loc);
4015 * Check for end of file
4017 if (dirp->dd_size == 0 || directP->d_fileno == 0) {
4021 len = ((sizeof(struct min_direct) + directP->d_namlen + 4) & (~3));
4022 usr_assert(len <= dirp->dd_size);
4025 * Copy the next entry into the usr_dirent structure and advance
4027 direntP = (struct usr_dirent *)(dirp->dd_buf + USR_DIRSIZE);
4028 direntP->d_ino = directP->d_fileno;
4029 direntP->d_off = direntP->d_reclen;
4031 sizeof(struct usr_dirent) - MAXNAMLEN + directP->d_namlen + 1;
4032 memcpy(&direntP->d_name[0], (void *)(directP + 1), directP->d_namlen);
4033 direntP->d_name[directP->d_namlen] = '\0';
4034 dirp->dd_loc += len;
4035 dirp->dd_size -= len;
4044 uafs_closedir(usr_DIR * dirp)
4048 retval = uafs_closedir_r(dirp);
4054 uafs_closedir_r(usr_DIR * dirp)
4060 afs_osi_Free((char *)dirp,
4061 sizeof(usr_DIR) + USR_DIRSIZE + sizeof(struct usr_dirent));
4062 rc = uafs_close_r(fd);
4067 * Do AFS authentication
4070 uafs_klog(char *user, char *cell, char *passwd, char **reason)
4073 afs_int32 password_expires = -1;
4075 usr_mutex_lock(&osi_authenticate_lock);
4077 ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION +
4078 KA_USERAUTH_DOSETPAG2, user, NULL, cell,
4079 passwd, 0, &password_expires, 0, reason);
4080 usr_mutex_unlock(&osi_authenticate_lock);
4085 uafs_klog_r(char *user, char *cell, char *passwd, char **reason)
4089 retval = uafs_klog(user, cell, passwd, reason);
4095 * Destroy AFS credentials from the kernel cache
4102 usr_mutex_lock(&osi_authenticate_lock);
4103 code = ktc_ForgetAllTokens();
4104 usr_mutex_unlock(&osi_authenticate_lock);
4113 retval = uafs_unlog();
4119 * Strip the AFS mount point from a pathname string. Return
4120 * NULL if the path is a relative pathname or if the path
4121 * doesn't start with the AFS mount point string.
4124 uafs_afsPathName(char *path)
4133 for (i = 1, p = path + 1; *p != '\0'; p++) {
4134 /* Ignore duplicate slashes */
4135 if (*p == '/' && lastchar == '/')
4137 /* Is this a subdirectory of the AFS mount point? */
4138 if (afs_mountDir[i] == '\0' && *p == '/') {
4139 /* strip leading slashes */
4140 while (*(++p) == '/');
4143 /* Reject paths that are not within AFS */
4144 if (*p != afs_mountDir[i])
4149 /* Is this the AFS mount point? */
4150 if (afs_mountDir[i] == '\0') {
4151 usr_assert(*p == '\0');
4157 #ifdef AFS_WEB_ENHANCEMENTS
4160 * klog but don't allocate a new pag
4163 uafs_klog_nopag(char *user, char *cell, char *passwd, char **reason)
4166 afs_int32 password_expires = -1;
4168 usr_mutex_lock(&osi_authenticate_lock);
4169 code = ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION
4170 /*+KA_USERAUTH_DOSETPAG2 */ , user,
4171 NULL, cell, passwd, 0,
4172 &password_expires, 0, reason);
4173 usr_mutex_unlock(&osi_authenticate_lock);
4178 * uafs_getcellstatus
4179 * get the cell status
4182 uafs_getcellstatus(char *cell, afs_int32 * status)
4185 struct afs_ioctl iob;
4188 iob.in_size = strlen(cell) + 1;
4192 rc = call_syscall(AFSCALL_PIOCTL, /*path */ 0, _VICEIOCTL(35),
4200 *status = (afs_int32) iob.out;
4206 * Get quota of volume associated with path
4209 uafs_getvolquota(char *path, afs_int32 * BlocksInUse, afs_int32 * MaxQuota)
4212 struct afs_ioctl iob;
4213 VolumeStatus *status;
4219 iob.out_size = 1024;
4221 rc = call_syscall(AFSCALL_PIOCTL, (long)path, _VICEIOCTL(4), (long)&iob,
4229 status = (VolumeStatus *) buf;
4230 *BlocksInUse = status->BlocksInUse;
4231 *MaxQuota = status->MaxQuota;
4237 * Set quota of volume associated with path
4240 uafs_setvolquota(char *path, afs_int32 MaxQuota)
4243 struct afs_ioctl iob;
4244 VolumeStatus *status;
4252 memset(buf, 0, sizeof(VolumeStatus));
4253 status = (VolumeStatus *) buf;
4254 status->MaxQuota = MaxQuota;
4255 status->MinQuota = -1;
4257 rc = call_syscall(AFSCALL_PIOCTL, (long)path, _VICEIOCTL(5), (long)&iob,
4269 * uafs_statmountpoint
4270 * Determine whether a dir. is a mount point or not
4271 * return 1 if mount point, 0 if not
4274 uafs_statmountpoint(char *path)
4281 retval = uafs_statmountpoint_r(path);
4287 uafs_statmountpoint_r(char *path)
4292 struct vrequest treq;
4295 code = uafs_LookupName(path, afs_CurrentDir, &vp, 0, 1);
4310 * Get a list of rights for the current user on path.
4313 uafs_getRights(char *path)
4320 code = uafs_LookupName(path, afs_CurrentDir, &vp, 1, 0);
4328 PRSFS_READ | PRSFS_WRITE | PRSFS_INSERT | PRSFS_LOOKUP | PRSFS_DELETE
4329 | PRSFS_LOCK | PRSFS_ADMINISTER;
4331 afs_rights = afs_getRights(vp, afs_rights, u.u_cred);
4336 #endif /* AFS_WEB_ENHANCEMENTS */
4338 #endif /* UKERNEL */