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"
27 #include "afs/sysincludes.h" /* Standard vendor system headers */
28 #include "afsincludes.h" /* Afs-based standard headers */
29 #include "afs/afs_stats.h" /* statistics */
30 #include "afs/afs_cbqueue.h"
31 #include "afs/nfsclient.h"
32 #include "afs/afs_osidnlc.h"
34 extern afs_rwlock_t afs_xvcache;
35 extern afs_rwlock_t afs_xcbhash;
37 /* Note: There is the bare bones beginning of symlink hints in the now
38 * defunct afs/afs_lookup.c file. Since they are not in use, making the call
39 * is just a performance hit.
43 /* don't set CDirty in here because RPC is called synchronously */
47 (ndp, attrs, atargetName)
48 struct nameidata *ndp;
50 register char *atargetName;
52 register struct vcache *adp = VTOAFS(ndp->ni_dvp);
53 char *aname = ndp->ni_dent.d_name;
54 struct ucred *acred = ndp->ni_cred;
55 #else /* AFS_OSF_ENV */
56 (OSI_VC_ARG(adp), aname, attrs, atargetName, acred)
61 struct AFS_UCRED *acred; {
67 struct VenusFid newFid;
69 afs_size_t offset, len;
71 struct server *hostp = 0;
73 struct AFSStoreStatus InStatus;
74 struct AFSFetchStatus OutFidStatus, OutDirStatus;
75 struct AFSCallBack CallBack;
76 struct AFSVolSync tsync;
77 struct volume *volp = 0;
78 struct afs_fakestat_state fakestate;
79 XSTATS_DECLS OSI_VC_CONVERT(adp)
81 AFS_STATCNT(afs_symlink);
82 afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
83 ICL_TYPE_STRING, aname);
85 if ((code = afs_InitReq(&treq, acred)))
88 afs_InitFakeStat(&fakestate);
89 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
93 if (strlen(aname) > AFSNAMEMAX || strlen(atargetName) > AFSPATHMAX) {
98 if (afs_IsDynroot(adp)) {
99 code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
103 code = afs_VerifyVCache(adp, &treq);
105 code = afs_CheckCode(code, &treq, 30);
109 /** If the volume is read-only, return error without making an RPC to the
112 if (adp->states & CRO) {
117 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE;
118 InStatus.ClientModTime = osi_Time();
119 alen = strlen(atargetName); /* we want it to include the null */
120 if (*atargetName == '#' || *atargetName == '%') {
121 InStatus.UnixModeBits = 0644; /* mt pt: null from "." at end */
123 alen++; /* Empty string */
125 InStatus.UnixModeBits = 0755;
126 alen++; /* add in the null */
128 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
129 volp = afs_FindVolume(&adp->fid, READ_LOCK); /*parent is also in same vol */
130 ObtainWriteLock(&adp->lock, 156);
132 ObtainWriteLock(&tdc->lock, 636);
133 ObtainSharedLock(&afs_xvcache, 17); /* prevent others from creating this entry */
134 /* XXX Pay attention to afs_xvcache around the whole thing!! XXX */
136 tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
138 hostp = tc->srvr->server;
139 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
140 if (adp->states & CForeign) {
144 RXAFS_DFSSymlink(tc->id, (struct AFSFid *)&adp->fid.Fid,
145 aname, atargetName, &InStatus,
146 (struct AFSFid *)&newFid.Fid,
147 &OutFidStatus, &OutDirStatus, &CallBack,
153 RXAFS_Symlink(tc->id, (struct AFSFid *)&adp->fid.Fid,
154 aname, atargetName, &InStatus,
155 (struct AFSFid *)&newFid.Fid, &OutFidStatus,
156 &OutDirStatus, &tsync);
163 (tc, code, &adp->fid, &treq, AFS_STATS_FS_RPCIDX_SYMLINK,
166 UpgradeSToWLock(&afs_xvcache, 40);
169 ObtainWriteLock(&afs_xcbhash, 499);
170 afs_DequeueCallback(adp);
171 adp->states &= ~CStatd;
172 ReleaseWriteLock(&afs_xcbhash);
173 osi_dnlc_purgedp(adp);
175 ReleaseWriteLock(&adp->lock);
176 ReleaseWriteLock(&afs_xvcache);
178 ReleaseWriteLock(&tdc->lock);
183 /* otherwise, we should see if we can make the change to the dir locally */
184 if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
185 /* we can do it locally */
186 code = afs_dir_Create(&tdc->f.inode, aname, &newFid.Fid);
188 ZapDCE(tdc); /* surprise error -- use invalid value */
193 ReleaseWriteLock(&tdc->lock);
196 newFid.Cell = adp->fid.Cell;
197 newFid.Fid.Volume = adp->fid.Fid.Volume;
198 ReleaseWriteLock(&adp->lock);
200 /* now we're done with parent dir, create the link's entry. Note that
201 * no one can get a pointer to the new cache entry until we release
202 * the xvcache lock. */
203 tvc = afs_NewVCache(&newFid, hostp);
204 ObtainWriteLock(&tvc->lock, 157);
205 ObtainWriteLock(&afs_xcbhash, 500);
206 tvc->states |= CStatd; /* have valid info */
207 tvc->states &= ~CBulkFetching;
209 if (adp->states & CForeign) {
210 tvc->states |= CForeign;
211 /* We don't have to worry about losing the callback since we're doing it
212 * under the afs_xvcache lock actually, afs_NewVCache may drop the
213 * afs_xvcache lock, if it calls afs_FlushVCache */
214 tvc->cbExpires = CallBack.ExpirationTime + now;
215 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
217 tvc->cbExpires = 0x7fffffff; /* never expires, they can't change */
218 /* since it never expires, we don't have to queue the callback */
220 ReleaseWriteLock(&afs_xcbhash);
221 afs_ProcessFS(tvc, &OutFidStatus, &treq);
223 if (!tvc->linkData) {
224 tvc->linkData = (char *)afs_osi_Alloc(alen);
225 strncpy(tvc->linkData, atargetName, alen - 1);
226 tvc->linkData[alen - 1] = 0;
228 ReleaseWriteLock(&tvc->lock);
229 ReleaseWriteLock(&afs_xvcache);
233 afs_PutFakeStat(&fakestate);
235 afs_PutVolume(volp, READ_LOCK);
236 code = afs_CheckCode(code, &treq, 31);
239 AFS_RELE(ndp->ni_dvp);
240 #endif /* AFS_OSF_ENV */
245 afs_MemHandleLink(register struct vcache *avc, struct vrequest *areq)
247 register struct dcache *tdc;
248 register char *tp, *rbuf;
249 afs_size_t offset, len;
250 afs_int32 tlen, alen;
251 register afs_int32 code;
253 AFS_STATCNT(afs_MemHandleLink);
254 /* two different formats, one for links protected 644, have a "." at
255 * the end of the file name, which we turn into a null. Others,
256 * protected 755, we add a null to the end of */
257 if (!avc->linkData) {
259 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
263 /* otherwise we have the data loaded, go for it */
268 if (avc->m.Mode & 0111)
269 alen = len + 1; /* regular link */
271 alen = len; /* mt point */
272 rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
273 ObtainReadLock(&tdc->lock);
274 addr = afs_MemCacheOpen(tdc->f.inode);
276 code = afs_MemReadBlk(addr, 0, rbuf, tlen);
277 afs_MemCacheClose(addr);
278 ReleaseReadLock(&tdc->lock);
281 alen = strlen(rbuf) + 1;
282 tp = afs_osi_Alloc(alen); /* make room for terminating null */
283 memcpy(tp, rbuf, alen);
284 osi_FreeLargeSpace(rbuf);
286 afs_osi_Free(tp, alen);
295 afs_UFSHandleLink(register struct vcache *avc, struct vrequest *areq)
297 register struct dcache *tdc;
298 register char *tp, *rbuf;
300 afs_size_t offset, len;
301 afs_int32 tlen, alen;
302 register afs_int32 code;
304 /* two different formats, one for links protected 644, have a "." at the
305 * end of the file name, which we turn into a null. Others, protected
306 * 755, we add a null to the end of */
307 AFS_STATCNT(afs_UFSHandleLink);
308 if (!avc->linkData) {
309 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
310 afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
311 ICL_TYPE_POINTER, tdc, ICL_TYPE_OFFSET,
312 ICL_HANDLE_OFFSET(avc->m.Length));
316 /* otherwise we have the data loaded, go for it */
321 if (avc->m.Mode & 0111)
322 alen = len + 1; /* regular link */
324 alen = len; /* mt point */
325 rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
327 ObtainReadLock(&tdc->lock);
328 tfile = osi_UFSOpen(tdc->f.inode);
329 code = afs_osi_Read(tfile, -1, rbuf, tlen);
331 ReleaseReadLock(&tdc->lock);
333 rbuf[alen - 1] = '\0';
334 alen = strlen(rbuf) + 1;
335 tp = afs_osi_Alloc(alen); /* make room for terminating null */
336 memcpy(tp, rbuf, alen);
337 osi_FreeLargeSpace(rbuf);
339 afs_osi_Free(tp, alen);
348 afs_readlink(OSI_VC_ARG(avc), auio, acred)
351 struct AFS_UCRED *acred;
353 register afs_int32 code;
354 struct vrequest treq;
356 struct afs_fakestat_state fakestat;
359 AFS_STATCNT(afs_readlink);
360 afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
361 if ((code = afs_InitReq(&treq, acred)))
363 afs_InitFakeStat(&fakestat);
364 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
367 code = afs_VerifyVCache(avc, &treq);
370 if (vType(avc) != VLNK) {
374 ObtainWriteLock(&avc->lock, 158);
375 code = afs_HandleLink(avc, &treq);
376 /* finally uiomove it to user-land */
380 AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
385 ReleaseWriteLock(&avc->lock);
387 afs_PutFakeStat(&fakestat);
388 code = afs_CheckCode(code, &treq, 32);