no-copy-libafs-builds-20021015
[openafs.git] / src / afs / VNOPS / afs_vnop_open.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * Implements:
12  * afs_open
13  *
14  */
15
16
17 #include <afsconfig.h>
18 #include "afs/param.h"
19
20 RCSID("$Header$");
21
22 #include "afs/sysincludes.h"    /* Standard vendor system headers */
23 #include "afsincludes.h"        /* Afs-based standard headers */
24 #include "afs/afs_stats.h" /* statistics */
25 #include "afs/afs_cbqueue.h"
26 #include "afs/nfsclient.h"
27 #include "afs/afs_osidnlc.h"
28
29
30
31 /* given a vnode ptr, open flags and credentials, open the file.  No access
32  * checks are done here, instead they're done by afs_create or afs_access,
33  * both called by the vn_open call.
34  */
35 #ifdef AFS_SGI64_ENV
36 afs_open(bhv, avcp, aflags, acred)
37     bhv_desc_t *bhv;
38 #else
39 afs_open(avcp, aflags, acred)
40 #endif
41     register struct vcache **avcp;
42     afs_int32 aflags;
43     struct AFS_UCRED *acred; 
44 {
45     register afs_int32 code;
46     struct vrequest treq;
47     struct vcache *tvc;
48     int writing;
49     struct afs_fakestat_state fakestate;
50     
51     AFS_STATCNT(afs_open);
52     if ((code = afs_InitReq(&treq, acred))) return code;
53 #ifdef AFS_SGI64_ENV
54     /* avcpp can be, but is not necesarily, bhp's vnode. */
55     tvc = VTOAFS(BHV_TO_VNODE(bhv));
56 #else
57     tvc = *avcp;
58 #endif
59     afs_Trace2(afs_iclSetp, CM_TRACE_OPEN, ICL_TYPE_POINTER, tvc,
60                ICL_TYPE_INT32, aflags);
61     afs_InitFakeStat(&fakestate);
62     code = afs_EvalFakeStat(&tvc, &fakestate, &treq);
63     if (code) goto done;
64     code = afs_VerifyVCache(tvc, &treq);
65     if (code) goto done;
66     if (aflags & (FWRITE | FTRUNC)) writing = 1;
67     else writing = 0;
68     if (vType(tvc) == VDIR) {
69         /* directory */
70         if (writing) {
71             code = EISDIR;
72             goto done;
73         }
74         else {
75             if (!afs_AccessOK(tvc, 
76                         ((tvc->states & CForeign) ? PRSFS_READ: PRSFS_LOOKUP),
77                         &treq, CHECK_MODE_BITS)) {
78                 code = EACCES;
79                 goto done;
80             }
81         }
82     }
83     else {
84 #ifdef  AFS_SUN5_ENV
85         if (AFS_NFSXLATORREQ(acred) &&  (aflags & FREAD)) {
86             if (!afs_AccessOK(tvc, PRSFS_READ, &treq,
87                               CHECK_MODE_BITS|CMB_ALLOW_EXEC_AS_READ)) {
88                 code = EACCES;
89                 goto done;
90             }
91         }
92 #endif
93 #ifdef  AFS_AIX41_ENV
94         if (aflags & FRSHARE) {
95             /*
96              * Hack for AIX 4.1:
97              *  Apparently it is possible for a file to get mapped without
98              *  either VNOP_MAP or VNOP_RDWR being called, if (1) it is a
99              *  sharable library, and (2) it has already been loaded.  We must
100              *  ensure that the credp is up to date.  We detect the situation
101              *  by checking for O_RSHARE at open time.
102              */
103             /*
104              * We keep the caller's credentials since an async daemon will
105              * handle the request at some point. We assume that the same
106              * credentials will be used.
107              */
108             ObtainWriteLock(&tvc->lock,140);
109             if (!tvc->credp || (tvc->credp != acred)) {
110                 crhold(acred);
111                 if (tvc->credp) {
112                     struct ucred *crp = tvc->credp;
113                     tvc->credp = NULL;
114                     crfree(crp);
115                 }
116                 tvc->credp = acred;
117             }
118             ReleaseWriteLock(&tvc->lock);
119         }
120 #endif
121         /* normal file or symlink */
122         osi_FlushText(tvc); /* only needed to flush text if text locked last time */
123 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
124         afs_BozonLock(&tvc->pvnLock, tvc);
125 #endif
126         osi_FlushPages(tvc, acred);
127 #if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
128         afs_BozonUnlock(&tvc->pvnLock, tvc);
129 #endif
130     }
131     /* set date on file if open in O_TRUNC mode */
132     if (aflags & FTRUNC) {
133         /* this fixes touch */
134         ObtainWriteLock(&tvc->lock,123);
135         tvc->m.Date = osi_Time();
136         tvc->states |= CDirty;
137         ReleaseWriteLock(&tvc->lock);
138     }
139     ObtainReadLock(&tvc->lock);
140     if (writing) tvc->execsOrWriters++;
141     tvc->opens++;
142 #if defined(AFS_SGI_ENV)
143     if (writing && tvc->cred == NULL) {
144         crhold(acred);
145         tvc->cred = acred;
146     }
147 #endif
148     ReleaseReadLock(&tvc->lock);
149 done:
150     afs_PutFakeStat(&fakestate);
151     code = afs_CheckCode(code, &treq, 4); /* avoid AIX -O bug */
152
153     afs_Trace2(afs_iclSetp, CM_TRACE_OPEN, ICL_TYPE_POINTER, tvc,
154                ICL_TYPE_INT32, 999999);
155
156     return code;
157 }