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 */
45 afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
46 char *atargetName, struct AFS_UCRED *acred)
52 struct VenusFid newFid;
54 afs_size_t offset, len;
56 struct server *hostp = 0;
58 struct AFSStoreStatus InStatus;
59 struct AFSFetchStatus OutFidStatus, OutDirStatus;
60 struct AFSCallBack CallBack;
61 struct AFSVolSync tsync;
62 struct volume *volp = 0;
63 struct afs_fakestat_state fakestate;
67 AFS_STATCNT(afs_symlink);
68 afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
69 ICL_TYPE_STRING, aname);
71 if ((code = afs_InitReq(&treq, acred)))
74 afs_InitFakeStat(&fakestate);
78 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
82 if (strlen(aname) > AFSNAMEMAX || strlen(atargetName) > AFSPATHMAX) {
87 if (afs_IsDynroot(adp)) {
88 code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
91 if (afs_IsDynrootMount(adp)) {
96 code = afs_VerifyVCache(adp, &treq);
98 code = afs_CheckCode(code, &treq, 30);
102 /** If the volume is read-only, return error without making an RPC to the
105 if (adp->states & CRO) {
110 if (AFS_IS_DISCONNECTED && !AFS_IS_LOGGING) {
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 == '%') && alen > 1 && atargetName[alen-1] == '.') {
119 InStatus.UnixModeBits = 0644; /* mt pt: null from "." at end */
121 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);
130 ObtainWriteLock(&tdc->lock, 636);
131 ObtainSharedLock(&afs_xvcache, 17); /* prevent others from creating this entry */
132 /* XXX Pay attention to afs_xvcache around the whole thing!! XXX */
134 tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
136 hostp = tc->srvr->server;
137 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
138 if (adp->states & CForeign) {
142 RXAFS_DFSSymlink(tc->id, (struct AFSFid *)&adp->fid.Fid,
143 aname, atargetName, &InStatus,
144 (struct AFSFid *)&newFid.Fid,
145 &OutFidStatus, &OutDirStatus, &CallBack,
151 RXAFS_Symlink(tc->id, (struct AFSFid *)&adp->fid.Fid,
152 aname, atargetName, &InStatus,
153 (struct AFSFid *)&newFid.Fid, &OutFidStatus,
154 &OutDirStatus, &tsync);
161 (tc, code, &adp->fid, &treq, AFS_STATS_FS_RPCIDX_SYMLINK,
164 UpgradeSToWLock(&afs_xvcache, 40);
167 ObtainWriteLock(&afs_xcbhash, 499);
168 afs_DequeueCallback(adp);
169 adp->states &= ~CStatd;
170 ReleaseWriteLock(&afs_xcbhash);
171 osi_dnlc_purgedp(adp);
173 ReleaseWriteLock(&adp->lock);
174 ReleaseWriteLock(&afs_xvcache);
176 ReleaseWriteLock(&tdc->lock);
181 /* otherwise, we should see if we can make the change to the dir locally */
182 if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
183 /* we can do it locally */
184 ObtainWriteLock(&afs_xdcache, 293);
185 code = afs_dir_Create(tdc, aname, &newFid.Fid);
186 ReleaseWriteLock(&afs_xdcache);
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);
207 ReleaseWriteLock(&afs_xvcache);
210 ObtainWriteLock(&tvc->lock, 157);
211 ObtainWriteLock(&afs_xcbhash, 500);
212 tvc->states |= CStatd; /* have valid info */
213 tvc->states &= ~CBulkFetching;
215 if (adp->states & CForeign) {
216 tvc->states |= CForeign;
217 /* We don't have to worry about losing the callback since we're doing it
218 * under the afs_xvcache lock actually, afs_NewVCache may drop the
219 * afs_xvcache lock, if it calls afs_FlushVCache */
220 tvc->cbExpires = CallBack.ExpirationTime + now;
221 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
223 tvc->cbExpires = 0x7fffffff; /* never expires, they can't change */
224 /* since it never expires, we don't have to queue the callback */
226 ReleaseWriteLock(&afs_xcbhash);
227 afs_ProcessFS(tvc, &OutFidStatus, &treq);
229 if (!tvc->linkData) {
230 tvc->linkData = (char *)afs_osi_Alloc(alen);
231 strncpy(tvc->linkData, atargetName, alen - 1);
232 tvc->linkData[alen - 1] = 0;
234 ReleaseWriteLock(&tvc->lock);
235 ReleaseWriteLock(&afs_xvcache);
239 afs_PutFakeStat(&fakestate);
241 afs_PutVolume(volp, READ_LOCK);
243 code = afs_CheckCode(code, &treq, 31);
249 afs_MemHandleLink(register struct vcache *avc, struct vrequest *areq)
251 register struct dcache *tdc;
252 register char *tp, *rbuf;
253 afs_size_t offset, len;
254 afs_int32 tlen, alen;
255 register afs_int32 code;
257 AFS_STATCNT(afs_MemHandleLink);
258 /* two different formats, one for links protected 644, have a "." at
259 * the end of the file name, which we turn into a null. Others,
260 * protected 755, we add a null to the end of */
261 if (!avc->linkData) {
263 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
267 /* otherwise we have the data loaded, go for it */
272 if (avc->m.Mode & 0111)
273 alen = len + 1; /* regular link */
275 alen = len; /* mt point */
276 rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
277 ObtainReadLock(&tdc->lock);
278 addr = afs_MemCacheOpen(tdc->f.inode);
280 code = afs_MemReadBlk(addr, 0, rbuf, tlen);
281 afs_MemCacheClose(addr);
282 ReleaseReadLock(&tdc->lock);
285 alen = strlen(rbuf) + 1;
286 tp = afs_osi_Alloc(alen); /* make room for terminating null */
287 memcpy(tp, rbuf, alen);
288 osi_FreeLargeSpace(rbuf);
290 afs_osi_Free(tp, alen);
299 afs_UFSHandleLink(register struct vcache *avc, struct vrequest *areq)
301 register struct dcache *tdc;
302 register char *tp, *rbuf;
304 afs_size_t offset, len;
305 afs_int32 tlen, alen;
306 register afs_int32 code;
308 /* two different formats, one for links protected 644, have a "." at the
309 * end of the file name, which we turn into a null. Others, protected
310 * 755, we add a null to the end of */
311 AFS_STATCNT(afs_UFSHandleLink);
312 if (!avc->linkData) {
313 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
314 afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
315 ICL_TYPE_POINTER, tdc, ICL_TYPE_OFFSET,
316 ICL_HANDLE_OFFSET(avc->m.Length));
318 if (AFS_IS_DISCONNECTED)
323 /* otherwise we have the data loaded, go for it */
328 if (avc->m.Mode & 0111)
329 alen = len + 1; /* regular link */
331 alen = len; /* mt point */
332 rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
334 ObtainReadLock(&tdc->lock);
335 #if defined(LINUX_USE_FH)
336 tfile = osi_UFSOpen_fh(&tdc->f.fh, tdc->f.fh_type);
338 tfile = osi_UFSOpen(tdc->f.inode);
340 code = afs_osi_Read(tfile, -1, rbuf, tlen);
342 ReleaseReadLock(&tdc->lock);
344 rbuf[alen - 1] = '\0';
345 alen = strlen(rbuf) + 1;
346 tp = afs_osi_Alloc(alen); /* make room for terminating null */
347 memcpy(tp, rbuf, alen);
348 osi_FreeLargeSpace(rbuf);
350 afs_osi_Free(tp, alen);
359 afs_readlink(OSI_VC_DECL(avc), struct uio *auio, struct AFS_UCRED *acred)
361 register afs_int32 code;
362 struct vrequest treq;
364 struct afs_fakestat_state fakestat;
367 AFS_STATCNT(afs_readlink);
368 afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
369 if ((code = afs_InitReq(&treq, acred)))
371 afs_InitFakeStat(&fakestat);
375 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
378 code = afs_VerifyVCache(avc, &treq);
381 if (vType(avc) != VLNK) {
385 ObtainWriteLock(&avc->lock, 158);
386 code = afs_HandleLink(avc, &treq);
387 /* finally uiomove it to user-land */
391 AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
396 ReleaseWriteLock(&avc->lock);
398 afs_PutFakeStat(&fakestat);
400 code = afs_CheckCode(code, &treq, 32);