2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
17 #include <afsconfig.h>
18 #include "afs/param.h"
23 #include "afs/sysincludes.h" /* Standard vendor system headers */
24 #include "afsincludes.h" /* Afs-based standard headers */
25 #include "afs/afs_stats.h" /* statistics */
26 #include "afs/afs_cbqueue.h"
27 #include "afs/nfsclient.h"
28 #include "afs/afs_osidnlc.h"
32 /* given a vnode ptr, open flags and credentials, open the file. No access
33 * checks are done here, instead they're done by afs_create or afs_access,
34 * both called by the vn_open call.
38 afs_open(bhv_desc_t * bhv, struct vcache **avcp, afs_int32 aflags,
39 struct AFS_UCRED *acred)
41 afs_open(struct vcache **avcp, afs_int32 aflags, struct AFS_UCRED *acred)
44 register afs_int32 code;
48 struct afs_fakestat_state fakestate;
50 AFS_STATCNT(afs_open);
51 if ((code = afs_InitReq(&treq, acred)))
54 /* avcpp can be, but is not necesarily, bhp's vnode. */
55 tvc = VTOAFS(BHV_TO_VNODE(bhv));
59 afs_Trace2(afs_iclSetp, CM_TRACE_OPEN, ICL_TYPE_POINTER, tvc,
60 ICL_TYPE_INT32, aflags);
61 afs_InitFakeStat(&fakestate);
65 code = afs_EvalFakeStat(&tvc, &fakestate, &treq);
68 code = afs_VerifyVCache(tvc, &treq);
72 ObtainReadLock(&tvc->lock);
75 if (AFS_IS_DISCONNECTED && (afs_DCacheMissingChunks(tvc) != 0)) {
76 ReleaseReadLock(&tvc->lock);
77 printf("Network is down in afs_open: missing chunks\n");
83 ReleaseReadLock(&tvc->lock);
85 if (aflags & (FWRITE | FTRUNC))
89 if (vType(tvc) == VDIR) {
96 (tvc, ((tvc->f.states & CForeign) ? PRSFS_READ : PRSFS_LOOKUP),
97 &treq, CHECK_MODE_BITS)) {
99 printf("afs_Open: no access for dir\n");
105 if (AFS_NFSXLATORREQ(acred) && (aflags & FREAD)) {
107 (tvc, PRSFS_READ, &treq,
108 CHECK_MODE_BITS | CMB_ALLOW_EXEC_AS_READ)) {
115 if (aflags & FRSHARE) {
118 * Apparently it is possible for a file to get mapped without
119 * either VNOP_MAP or VNOP_RDWR being called, if (1) it is a
120 * sharable library, and (2) it has already been loaded. We must
121 * ensure that the credp is up to date. We detect the situation
122 * by checking for O_RSHARE at open time.
125 * We keep the caller's credentials since an async daemon will
126 * handle the request at some point. We assume that the same
127 * credentials will be used.
129 ObtainWriteLock(&tvc->lock, 140);
130 if (!tvc->credp || (tvc->credp != acred)) {
133 struct ucred *crp = tvc->credp;
139 ReleaseWriteLock(&tvc->lock);
142 /* normal file or symlink */
143 osi_FlushText(tvc); /* only needed to flush text if text locked last time */
144 #ifdef AFS_BOZONLOCK_ENV
145 afs_BozonLock(&tvc->pvnLock, tvc);
147 osi_FlushPages(tvc, acred);
148 #ifdef AFS_BOZONLOCK_ENV
149 afs_BozonUnlock(&tvc->pvnLock, tvc);
152 /* set date on file if open in O_TRUNC mode */
153 if (aflags & FTRUNC) {
154 /* this fixes touch */
155 ObtainWriteLock(&tvc->lock, 123);
156 tvc->f.m.Date = osi_Time();
157 tvc->f.states |= CDirty;
158 ReleaseWriteLock(&tvc->lock);
160 ObtainReadLock(&tvc->lock);
162 tvc->execsOrWriters++;
164 #if defined(AFS_SGI_ENV)
165 if (writing && tvc->cred == NULL) {
170 ReleaseReadLock(&tvc->lock);
171 if ((afs_preCache != 0) && (writing == 0) && (vType(tvc) != VDIR) &&
173 register struct dcache *tdc;
174 afs_size_t offset, len, totallen = 0;
176 tdc = afs_GetDCache(tvc, 0, &treq, &offset, &len, 1);
178 ObtainSharedLock(&tdc->mflock, 865);
179 if (!(tdc->mflags & DFFetchReq)) {
182 /* start the daemon (may already be running, however) */
183 UpgradeSToWLock(&tdc->mflock, 666);
184 tdc->mflags |= DFFetchReq; /* guaranteed to be cleared by BKG or
186 /* last parm (1) tells bkg daemon to do an afs_PutDCache when it
187 is done, since we don't want to wait for it to finish before
190 bp = afs_BQueue(BOP_FETCH, tvc, B_DONTWAIT, 0, acred,
191 (afs_size_t) 0, (afs_size_t) 1, tdc);
193 tdc->mflags &= ~DFFetchReq;
195 ReleaseWriteLock(&tdc->mflock);
197 ReleaseSharedLock(&tdc->mflock);
201 afs_PutFakeStat(&fakestate);
204 code = afs_CheckCode(code, &treq, 4); /* avoid AIX -O bug */
206 afs_Trace2(afs_iclSetp, CM_TRACE_OPEN, ICL_TYPE_POINTER, tvc,
207 ICL_TYPE_INT32, 999999);