2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * afs_vnop_symlink.c - symlink and readlink vnodeops.
21 #include <afsconfig.h>
22 #include "afs/param.h"
26 #include "afs/sysincludes.h" /* Standard vendor system headers */
27 #include "afsincludes.h" /* Afs-based standard headers */
28 #include "afs/afs_stats.h" /* statistics */
29 #include "afs/afs_cbqueue.h"
30 #include "afs/nfsclient.h"
31 #include "afs/afs_osidnlc.h"
33 extern afs_rwlock_t afs_xvcache;
34 extern afs_rwlock_t afs_xcbhash;
36 /* Note: There is the bare bones beginning of symlink hints in the now
37 * defunct afs/afs_lookup.c file. Since they are not in use, making the call
38 * is just a performance hit.
42 /* don't set CDirty in here because RPC is called synchronously */
46 (ndp, attrs, atargetName)
47 struct nameidata *ndp;
49 register char *atargetName; {
50 register struct vcache *adp = VTOAFS(ndp->ni_dvp);
51 char *aname = ndp->ni_dent.d_name;
52 struct ucred *acred = ndp->ni_cred;
53 #else /* AFS_OSF_ENV */
54 (OSI_VC_ARG(adp), aname, attrs, atargetName, acred)
59 struct AFS_UCRED *acred; {
65 struct VenusFid newFid;
67 afs_size_t offset, len;
69 struct server *hostp=0;
71 struct AFSStoreStatus InStatus;
72 struct AFSFetchStatus OutFidStatus, OutDirStatus;
73 struct AFSCallBack CallBack;
74 struct AFSVolSync tsync;
75 struct volume* volp=0;
76 struct afs_fakestat_state fakestate;
80 AFS_STATCNT(afs_symlink);
81 afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
82 ICL_TYPE_STRING, aname);
84 if ((code = afs_InitReq(&treq, acred)))
87 afs_InitFakeStat(&fakestate);
88 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
92 if (strlen(aname) > AFSNAMEMAX || strlen(atargetName) > AFSPATHMAX) {
97 if (afs_IsDynroot(adp)) {
98 code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
102 code = afs_VerifyVCache(adp, &treq);
104 code = afs_CheckCode(code, &treq, 30);
108 /** If the volume is read-only, return error without making an RPC to the
111 if ( adp->states & CRO ) {
116 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE;
117 InStatus.ClientModTime = osi_Time();
118 alen = strlen(atargetName); /* we want it to include the null */
119 if (*atargetName == '#' || *atargetName == '%') {
120 InStatus.UnixModeBits = 0644; /* mt pt: null from "." at end */
121 if (alen == 1) alen++; /* Empty string */
123 InStatus.UnixModeBits = 0755;
124 alen++; /* add in the null */
126 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
127 volp = afs_FindVolume(&adp->fid, READ_LOCK); /*parent is also in same vol*/
128 ObtainWriteLock(&adp->lock,156);
129 if (tdc) ObtainWriteLock(&tdc->lock, 636);
130 ObtainSharedLock(&afs_xvcache,17); /* prevent others from creating this entry */
131 /* XXX Pay attention to afs_xvcache around the whole thing!! XXX */
133 tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
135 hostp = tc->srvr->server;
136 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
137 if (adp->states & CForeign) {
140 code = RXAFS_DFSSymlink(tc->id, (struct AFSFid *) &adp->fid.Fid, aname,
141 atargetName, &InStatus, (struct AFSFid *) &newFid.Fid,
142 &OutFidStatus, &OutDirStatus, &CallBack, &tsync);
146 code = RXAFS_Symlink(tc->id, (struct AFSFid *) &adp->fid.Fid, aname,
147 atargetName, &InStatus, (struct AFSFid *) &newFid.Fid,
148 &OutFidStatus, &OutDirStatus, &tsync);
155 (afs_Analyze(tc, code, &adp->fid, &treq,
156 AFS_STATS_FS_RPCIDX_SYMLINK, SHARED_LOCK, NULL));
158 UpgradeSToWLock(&afs_xvcache,40);
161 ObtainWriteLock(&afs_xcbhash, 499);
162 afs_DequeueCallback(adp);
163 adp->states &= ~CStatd;
164 ReleaseWriteLock(&afs_xcbhash);
165 osi_dnlc_purgedp(adp);
167 ReleaseWriteLock(&adp->lock);
168 ReleaseWriteLock(&afs_xvcache);
170 ReleaseWriteLock(&tdc->lock);
175 /* otherwise, we should see if we can make the change to the dir locally */
176 if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
177 /* we can do it locally */
178 code = afs_dir_Create(&tdc->f.inode, aname, &newFid.Fid);
180 ZapDCE(tdc); /* surprise error -- use invalid value */
185 ReleaseWriteLock(&tdc->lock);
188 newFid.Cell = adp->fid.Cell;
189 newFid.Fid.Volume = adp->fid.Fid.Volume;
190 ReleaseWriteLock(&adp->lock);
192 /* now we're done with parent dir, create the link's entry. Note that
193 * no one can get a pointer to the new cache entry until we release
194 * the xvcache lock. */
195 tvc = afs_NewVCache(&newFid, hostp);
196 ObtainWriteLock(&tvc->lock,157);
197 ObtainWriteLock(&afs_xcbhash, 500);
198 tvc->states |= CStatd; /* have valid info */
199 tvc->states &= ~CBulkFetching;
201 if (adp->states & CForeign) {
202 tvc->states |= CForeign;
203 /* We don't have to worry about losing the callback since we're doing it
204 * under the afs_xvcache lock actually, afs_NewVCache may drop the
205 * afs_xvcache lock, if it calls afs_FlushVCache */
206 tvc->cbExpires = CallBack.ExpirationTime + now;
207 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
209 tvc->cbExpires = 0x7fffffff; /* never expires, they can't change */
210 /* since it never expires, we don't have to queue the callback */
212 ReleaseWriteLock(&afs_xcbhash);
213 afs_ProcessFS(tvc, &OutFidStatus, &treq);
215 if (!tvc->linkData) {
216 tvc->linkData = (char *) afs_osi_Alloc(alen);
217 strncpy(tvc->linkData, atargetName, alen-1);
218 tvc->linkData[alen-1] = 0;
220 ReleaseWriteLock(&tvc->lock);
221 ReleaseWriteLock(&afs_xvcache);
225 afs_PutFakeStat(&fakestate);
227 afs_PutVolume(volp, READ_LOCK);
228 code = afs_CheckCode(code, &treq, 31);
231 AFS_RELE(ndp->ni_dvp);
232 #endif /* AFS_OSF_ENV */
236 int afs_MemHandleLink(register struct vcache *avc, struct vrequest *areq)
238 register struct dcache *tdc;
239 register char *tp, *rbuf;
240 afs_size_t offset, len;
241 afs_int32 tlen, alen;
242 register afs_int32 code;
244 AFS_STATCNT(afs_MemHandleLink);
245 /* two different formats, one for links protected 644, have a "." at
246 the end of the file name, which we turn into a null. Others,
247 protected 755, we add a null to the end of */
248 if (!avc->linkData) {
250 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
254 /* otherwise we have the data loaded, go for it */
259 if (avc->m.Mode & 0111) alen = len+1; /* regular link */
260 else alen = len; /* mt point */
261 rbuf = (char *) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
262 ObtainReadLock(&tdc->lock);
263 addr = afs_MemCacheOpen(tdc->f.inode);
265 code = afs_MemReadBlk(addr, 0, rbuf, tlen);
266 afs_MemCacheClose(addr);
267 ReleaseReadLock(&tdc->lock);
270 alen = strlen(rbuf) + 1;
271 tp = afs_osi_Alloc(alen); /* make room for terminating null */
272 memcpy(tp, rbuf, alen);
273 osi_FreeLargeSpace(rbuf);
275 afs_osi_Free(tp, alen);
283 int afs_UFSHandleLink(register struct vcache *avc, struct vrequest *areq)
285 register struct dcache *tdc;
286 register char *tp, *rbuf;
288 afs_size_t offset, len;
289 afs_int32 tlen, alen;
290 register afs_int32 code;
292 /* two different formats, one for links protected 644, have a "." at the
293 end of the file name, which we turn into a null. Others, protected
294 755, we add a null to the end of */
295 AFS_STATCNT(afs_UFSHandleLink);
296 if (!avc->linkData) {
297 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
298 afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
299 ICL_TYPE_POINTER, tdc,
300 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
304 /* otherwise we have the data loaded, go for it */
309 if (avc->m.Mode & 0111) alen = len+1; /* regular link */
310 else alen = len; /* mt point */
311 rbuf = (char *) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
313 ObtainReadLock(&tdc->lock);
314 tfile = osi_UFSOpen (tdc->f.inode);
315 code = afs_osi_Read(tfile, -1, rbuf, tlen);
317 ReleaseReadLock(&tdc->lock);
320 alen = strlen(rbuf) + 1;
321 tp = afs_osi_Alloc(alen); /* make room for terminating null */
322 memcpy(tp, rbuf, alen);
323 osi_FreeLargeSpace(rbuf);
325 afs_osi_Free(tp, alen);
334 afs_readlink(OSI_VC_ARG(avc), auio, acred)
337 struct AFS_UCRED *acred;
339 register afs_int32 code;
340 struct vrequest treq;
342 struct afs_fakestat_state fakestat;
345 AFS_STATCNT(afs_readlink);
346 afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
347 if ((code = afs_InitReq(&treq, acred))) return code;
348 afs_InitFakeStat(&fakestat);
349 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
351 code = afs_VerifyVCache(avc, &treq);
353 if (vType(avc) != VLNK) {
357 ObtainWriteLock(&avc->lock,158);
358 code = afs_HandleLink(avc, &treq);
359 /* finally uiomove it to user-land */
362 if (tp) AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
367 ReleaseWriteLock(&avc->lock);
369 afs_PutFakeStat(&fakestat);
370 code = afs_CheckCode(code, &treq, 32);