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 "../afs/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 */
45 (ndp, attrs, atargetName)
46 struct nameidata *ndp;
48 register char *atargetName; {
49 register struct vcache *adp = VTOAFS(ndp->ni_dvp);
50 char *aname = ndp->ni_dent.d_name;
51 struct ucred *acred = ndp->ni_cred;
52 #else /* AFS_OSF_ENV */
53 (OSI_VC_ARG(adp), aname, attrs, atargetName, acred)
58 struct AFS_UCRED *acred; {
64 struct VenusFid newFid;
66 afs_size_t offset, len;
68 struct server *hostp=0;
70 struct AFSStoreStatus InStatus;
71 struct AFSFetchStatus OutFidStatus, OutDirStatus;
72 struct AFSCallBack CallBack;
73 struct AFSVolSync tsync;
74 struct volume* volp=0;
75 struct afs_fakestat_state fakestate;
79 AFS_STATCNT(afs_symlink);
80 afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
81 ICL_TYPE_STRING, aname);
83 if ((code = afs_InitReq(&treq, acred)))
86 afs_InitFakeStat(&fakestate);
87 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
91 if (strlen(aname) > AFSNAMEMAX || strlen(atargetName) > AFSPATHMAX) {
96 if (afs_IsDynroot(adp)) {
97 code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
101 code = afs_VerifyVCache(adp, &treq);
103 code = afs_CheckCode(code, &treq, 30);
107 /** If the volume is read-only, return error without making an RPC to the
110 if ( adp->states & CRO ) {
115 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE;
116 InStatus.ClientModTime = osi_Time();
117 alen = strlen(atargetName); /* we want it to include the null */
118 if (*atargetName == '#' || *atargetName == '%') {
119 InStatus.UnixModeBits = 0644; /* mt pt: null from "." at end */
120 if (alen == 1) alen++; /* Empty string */
122 InStatus.UnixModeBits = 0755;
123 alen++; /* add in the null */
125 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
126 volp = afs_FindVolume(&adp->fid, READ_LOCK); /*parent is also in same vol*/
127 ObtainWriteLock(&adp->lock,156);
128 if (tdc) ObtainWriteLock(&tdc->lock, 636);
129 ObtainSharedLock(&afs_xvcache,17); /* prevent others from creating this entry */
130 /* XXX Pay attention to afs_xvcache around the whole thing!! XXX */
132 tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
134 hostp = tc->srvr->server;
135 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
136 if (adp->states & CForeign) {
139 code = RXAFS_DFSSymlink(tc->id, (struct AFSFid *) &adp->fid.Fid, aname,
140 atargetName, &InStatus, (struct AFSFid *) &newFid.Fid,
141 &OutFidStatus, &OutDirStatus, &CallBack, &tsync);
145 code = RXAFS_Symlink(tc->id, (struct AFSFid *) &adp->fid.Fid, aname,
146 atargetName, &InStatus, (struct AFSFid *) &newFid.Fid,
147 &OutFidStatus, &OutDirStatus, &tsync);
154 (afs_Analyze(tc, code, &adp->fid, &treq,
155 AFS_STATS_FS_RPCIDX_SYMLINK, SHARED_LOCK, NULL));
157 UpgradeSToWLock(&afs_xvcache,40);
160 ObtainWriteLock(&afs_xcbhash, 499);
161 afs_DequeueCallback(adp);
162 adp->states &= ~CStatd;
163 ReleaseWriteLock(&afs_xcbhash);
164 osi_dnlc_purgedp(adp);
166 ReleaseWriteLock(&adp->lock);
167 ReleaseWriteLock(&afs_xvcache);
169 ReleaseWriteLock(&tdc->lock);
174 /* otherwise, we should see if we can make the change to the dir locally */
175 if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
176 /* we can do it locally */
177 code = afs_dir_Create(&tdc->f.inode, aname, &newFid.Fid);
179 ZapDCE(tdc); /* surprise error -- use invalid value */
184 ReleaseWriteLock(&tdc->lock);
187 newFid.Cell = adp->fid.Cell;
188 newFid.Fid.Volume = adp->fid.Fid.Volume;
189 ReleaseWriteLock(&adp->lock);
191 /* now we're done with parent dir, create the link's entry. Note that
192 * no one can get a pointer to the new cache entry until we release
193 * the xvcache lock. */
194 tvc = afs_NewVCache(&newFid, hostp);
195 ObtainWriteLock(&tvc->lock,157);
196 ObtainWriteLock(&afs_xcbhash, 500);
197 tvc->states |= CStatd; /* have valid info */
198 tvc->states &= ~CBulkFetching;
200 if (adp->states & CForeign) {
201 tvc->states |= CForeign;
202 /* We don't have to worry about losing the callback since we're doing it
203 * under the afs_xvcache lock actually, afs_NewVCache may drop the
204 * afs_xvcache lock, if it calls afs_FlushVCache */
205 tvc->cbExpires = CallBack.ExpirationTime + now;
206 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
208 tvc->cbExpires = 0x7fffffff; /* never expires, they can't change */
209 /* since it never expires, we don't have to queue the callback */
211 ReleaseWriteLock(&afs_xcbhash);
212 afs_ProcessFS(tvc, &OutFidStatus, &treq);
214 if (!tvc->linkData) {
215 tvc->linkData = (char *) afs_osi_Alloc(alen);
216 strncpy(tvc->linkData, atargetName, alen-1);
217 tvc->linkData[alen-1] = 0;
219 ReleaseWriteLock(&tvc->lock);
220 ReleaseWriteLock(&afs_xvcache);
224 afs_PutFakeStat(&fakestate);
226 afs_PutVolume(volp, READ_LOCK);
227 code = afs_CheckCode(code, &treq, 31);
230 AFS_RELE(ndp->ni_dvp);
231 #endif /* AFS_OSF_ENV */
235 int afs_MemHandleLink(register struct vcache *avc, struct vrequest *areq)
237 register struct dcache *tdc;
238 register char *tp, *rbuf;
239 afs_size_t offset, len;
240 afs_int32 tlen, alen;
241 register afs_int32 code;
243 AFS_STATCNT(afs_MemHandleLink);
244 /* two different formats, one for links protected 644, have a "." at
245 the end of the file name, which we turn into a null. Others,
246 protected 755, we add a null to the end of */
247 if (!avc->linkData) {
249 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
253 /* otherwise we have the data loaded, go for it */
258 if (avc->m.Mode & 0111) alen = len+1; /* regular link */
259 else alen = len; /* mt point */
260 rbuf = (char *) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
261 ObtainReadLock(&tdc->lock);
262 addr = afs_MemCacheOpen(tdc->f.inode);
264 code = afs_MemReadBlk(addr, 0, rbuf, tlen);
265 afs_MemCacheClose(addr);
266 ReleaseReadLock(&tdc->lock);
269 alen = strlen(rbuf) + 1;
270 tp = afs_osi_Alloc(alen); /* make room for terminating null */
271 memcpy(tp, rbuf, alen);
272 osi_FreeLargeSpace(rbuf);
274 afs_osi_Free(tp, alen);
282 int afs_UFSHandleLink(register struct vcache *avc, struct vrequest *areq)
284 register struct dcache *tdc;
285 register char *tp, *rbuf;
287 afs_size_t offset, len;
288 afs_int32 tlen, alen;
289 register afs_int32 code;
291 /* two different formats, one for links protected 644, have a "." at the end
292 of the file name, which we turn into a null. Others, protected 755,
293 we add a null to the end of */
294 AFS_STATCNT(afs_UFSHandleLink);
295 if (!avc->linkData) {
296 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
297 afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
298 ICL_TYPE_POINTER, tdc,
299 ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
303 /* otherwise we have the data loaded, go for it */
308 if (avc->m.Mode & 0111) alen = len+1; /* regular link */
309 else alen = len; /* mt point */
310 rbuf = (char *) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
312 ObtainReadLock(&tdc->lock);
313 tfile = osi_UFSOpen (tdc->f.inode);
314 code = afs_osi_Read(tfile, -1, rbuf, tlen);
316 ReleaseReadLock(&tdc->lock);
319 alen = strlen(rbuf) + 1;
320 tp = afs_osi_Alloc(alen); /* make room for terminating null */
321 memcpy(tp, rbuf, alen);
322 osi_FreeLargeSpace(rbuf);
324 afs_osi_Free(tp, alen);
332 afs_readlink(OSI_VC_ARG(avc), auio, acred)
335 struct AFS_UCRED *acred;
337 register afs_int32 code;
338 struct vrequest treq;
340 struct afs_fakestat_state fakestat;
343 AFS_STATCNT(afs_readlink);
344 afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
345 if ((code = afs_InitReq(&treq, acred))) return code;
346 afs_InitFakeStat(&fakestat);
347 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
349 code = afs_VerifyVCache(avc, &treq);
351 if (vType(avc) != VLNK) {
355 ObtainWriteLock(&avc->lock,158);
356 code = afs_HandleLink(avc, &treq);
357 /* finally uiomove it to user-land */
360 if (tp) AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
365 ReleaseWriteLock(&avc->lock);
367 afs_PutFakeStat(&fakestat);
368 code = afs_CheckCode(code, &treq, 32);