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"
25 #include "afs/sysincludes.h" /* Standard vendor system headers */
26 #include "afsincludes.h" /* Afs-based standard headers */
27 #include "afs/afs_stats.h" /* statistics */
28 #include "afs/afs_cbqueue.h"
29 #include "afs/nfsclient.h"
30 #include "afs/afs_osidnlc.h"
32 extern afs_rwlock_t afs_xvcache;
33 extern afs_rwlock_t afs_xcbhash;
35 /* Note: There is the bare bones beginning of symlink hints in the now
36 * defunct afs/afs_lookup.c file. Since they are not in use, making the call
37 * is just a performance hit.
42 afs_DisconCreateSymlink(struct vcache *avc, char *aname,
43 struct vrequest *areq) {
45 struct osi_file *tfile;
46 afs_size_t offset, len;
48 tdc = afs_GetDCache(avc, 0, areq, &offset, &len, 0);
50 printf("afs_DisconCreateSymlink: can't get new dcache for symlink.\n");
55 avc->f.m.Length = len;
57 ObtainWriteLock(&tdc->lock, 720);
58 afs_AdjustSize(tdc, len);
60 tfile = afs_CFileOpen(&tdc->f.inode);
61 afs_CFileWrite(tfile, 0, aname, len);
62 afs_CFileClose(tfile);
63 ReleaseWriteLock(&tdc->lock);
68 /* don't set CDirty in here because RPC is called synchronously */
70 afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
71 char *atargetName, afs_ucred_t *acred)
77 struct VenusFid newFid;
79 afs_size_t offset, len;
81 struct server *hostp = 0;
83 struct AFSStoreStatus InStatus;
84 struct AFSFetchStatus OutFidStatus, OutDirStatus;
85 struct AFSCallBack CallBack;
86 struct AFSVolSync tsync;
87 struct volume *volp = 0;
88 struct afs_fakestat_state fakestate;
92 AFS_STATCNT(afs_symlink);
93 afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
94 ICL_TYPE_STRING, aname);
96 if ((code = afs_InitReq(&treq, acred)))
99 afs_InitFakeStat(&fakestate);
103 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
107 if (strlen(aname) > AFSNAMEMAX || strlen(atargetName) > AFSPATHMAX) {
112 if (afs_IsDynroot(adp)) {
113 code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
116 if (afs_IsDynrootMount(adp)) {
121 code = afs_VerifyVCache(adp, &treq);
123 code = afs_CheckCode(code, &treq, 30);
127 /** If the volume is read-only, return error without making an RPC to the
130 if (adp->f.states & CRO) {
135 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
140 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE;
141 InStatus.ClientModTime = osi_Time();
142 alen = strlen(atargetName); /* we want it to include the null */
143 if ( (*atargetName == '#' || *atargetName == '%') && alen > 1 && atargetName[alen-1] == '.') {
144 InStatus.UnixModeBits = 0644; /* mt pt: null from "." at end */
146 alen++; /* Empty string */
148 InStatus.UnixModeBits = 0755;
149 alen++; /* add in the null */
151 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
152 volp = afs_FindVolume(&adp->f.fid, READ_LOCK); /*parent is also in same vol */
153 ObtainWriteLock(&adp->lock, 156);
155 ObtainWriteLock(&tdc->lock, 636);
156 ObtainSharedLock(&afs_xvcache, 17); /* prevent others from creating this entry */
157 /* XXX Pay attention to afs_xvcache around the whole thing!! XXX */
158 if (!AFS_IS_DISCON_RW) {
160 tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK);
162 hostp = tc->srvr->server;
163 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
164 if (adp->f.states & CForeign) {
168 RXAFS_DFSSymlink(tc->id,
169 (struct AFSFid *)&adp->f.fid.Fid,
170 aname, atargetName, &InStatus,
171 (struct AFSFid *)&newFid.Fid,
172 &OutFidStatus, &OutDirStatus,
178 RXAFS_Symlink(tc->id, (struct AFSFid *)&adp->f.fid.Fid,
179 aname, atargetName, &InStatus,
180 (struct AFSFid *)&newFid.Fid,
181 &OutFidStatus, &OutDirStatus, &tsync);
188 (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_SYMLINK,
191 #ifdef AFS_DISCON_ENV
192 newFid.Cell = adp->f.fid.Cell;
193 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
194 afs_GenFakeFid(&newFid, VREG, 0);
198 UpgradeSToWLock(&afs_xvcache, 40);
201 ObtainWriteLock(&afs_xcbhash, 499);
202 afs_DequeueCallback(adp);
203 adp->f.states &= ~CStatd;
204 ReleaseWriteLock(&afs_xcbhash);
205 osi_dnlc_purgedp(adp);
207 ReleaseWriteLock(&adp->lock);
208 ReleaseWriteLock(&afs_xvcache);
210 ReleaseWriteLock(&tdc->lock);
215 /* otherwise, we should see if we can make the change to the dir locally */
216 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
217 /* we can do it locally */
218 ObtainWriteLock(&afs_xdcache, 293);
219 code = afs_dir_Create(tdc, aname, &newFid.Fid);
220 ReleaseWriteLock(&afs_xdcache);
221 if (code && !AFS_IS_DISCON_RW) {
222 ZapDCE(tdc); /* surprise error -- use invalid value */
227 ReleaseWriteLock(&tdc->lock);
230 newFid.Cell = adp->f.fid.Cell;
231 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
232 ReleaseWriteLock(&adp->lock);
234 /* now we're done with parent dir, create the link's entry. Note that
235 * no one can get a pointer to the new cache entry until we release
236 * the xvcache lock. */
237 tvc = afs_NewVCache(&newFid, hostp);
241 ReleaseWriteLock(&afs_xvcache);
244 ObtainWriteLock(&tvc->lock, 157);
245 ObtainWriteLock(&afs_xcbhash, 500);
246 tvc->f.states |= CStatd; /* have valid info */
247 tvc->f.states &= ~CBulkFetching;
249 if (adp->f.states & CForeign) {
250 tvc->f.states |= CForeign;
251 /* We don't have to worry about losing the callback since we're doing it
252 * under the afs_xvcache lock actually, afs_NewVCache may drop the
253 * afs_xvcache lock, if it calls afs_FlushVCache */
254 tvc->cbExpires = CallBack.ExpirationTime + now;
255 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
257 tvc->cbExpires = 0x7fffffff; /* never expires, they can't change */
258 /* since it never expires, we don't have to queue the callback */
260 ReleaseWriteLock(&afs_xcbhash);
262 if (AFS_IS_DISCON_RW) {
263 #ifdef AFS_DISCON_ENV
264 attrs->va_mode = InStatus.UnixModeBits;
265 afs_GenDisconStatus(adp, tvc, &newFid, attrs, &treq, VLNK);
266 code = afs_DisconCreateSymlink(tvc, atargetName, &treq);
268 /* XXX - When this goes wrong, we need to tidy up the changes we made to
269 * the parent, and get rid of the vcache we just created */
270 ReleaseWriteLock(&tvc->lock);
271 ReleaseWriteLock(&afs_xvcache);
275 afs_DisconAddDirty(tvc, VDisconCreate, 0);
278 afs_ProcessFS(tvc, &OutFidStatus, &treq);
281 if (!tvc->linkData) {
282 tvc->linkData = (char *)afs_osi_Alloc(alen);
283 strncpy(tvc->linkData, atargetName, alen - 1);
284 tvc->linkData[alen - 1] = 0;
286 ReleaseWriteLock(&tvc->lock);
287 ReleaseWriteLock(&afs_xvcache);
291 afs_PutFakeStat(&fakestate);
293 afs_PutVolume(volp, READ_LOCK);
295 code = afs_CheckCode(code, &treq, 31);
301 afs_MemHandleLink(register struct vcache *avc, struct vrequest *areq)
303 register struct dcache *tdc;
304 register char *tp, *rbuf;
305 afs_size_t offset, len;
306 afs_int32 tlen, alen;
307 register afs_int32 code;
309 AFS_STATCNT(afs_MemHandleLink);
310 /* two different formats, one for links protected 644, have a "." at
311 * the end of the file name, which we turn into a null. Others,
312 * protected 755, we add a null to the end of */
313 if (!avc->linkData) {
315 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
319 /* otherwise we have the data loaded, go for it */
324 if (avc->f.m.Mode & 0111)
325 alen = len + 1; /* regular link */
327 alen = len; /* mt point */
328 rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
329 ObtainReadLock(&tdc->lock);
330 addr = afs_MemCacheOpen(&tdc->f.inode);
332 code = afs_MemReadBlk(addr, 0, rbuf, tlen);
333 afs_MemCacheClose(addr);
334 ReleaseReadLock(&tdc->lock);
337 alen = strlen(rbuf) + 1;
338 tp = afs_osi_Alloc(alen); /* make room for terminating null */
339 memcpy(tp, rbuf, alen);
340 osi_FreeLargeSpace(rbuf);
342 afs_osi_Free(tp, alen);
351 afs_UFSHandleLink(register struct vcache *avc, struct vrequest *areq)
353 register struct dcache *tdc;
354 register char *tp, *rbuf;
356 afs_size_t offset, len;
357 afs_int32 tlen, alen;
358 register afs_int32 code;
360 /* two different formats, one for links protected 644, have a "." at the
361 * end of the file name, which we turn into a null. Others, protected
362 * 755, we add a null to the end of */
363 AFS_STATCNT(afs_UFSHandleLink);
364 if (!avc->linkData) {
365 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
366 afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
367 ICL_TYPE_POINTER, tdc, ICL_TYPE_OFFSET,
368 ICL_HANDLE_OFFSET(avc->f.m.Length));
370 if (AFS_IS_DISCONNECTED)
375 /* otherwise we have the data loaded, go for it */
380 if (avc->f.m.Mode & 0111)
381 alen = len + 1; /* regular link */
383 alen = len; /* mt point */
384 rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
386 ObtainReadLock(&tdc->lock);
387 tfile = osi_UFSOpen(&tdc->f.inode);
388 code = afs_osi_Read(tfile, -1, rbuf, tlen);
390 ReleaseReadLock(&tdc->lock);
392 rbuf[alen - 1] = '\0';
393 alen = strlen(rbuf) + 1;
394 tp = afs_osi_Alloc(alen); /* make room for terminating null */
395 memcpy(tp, rbuf, alen);
396 osi_FreeLargeSpace(rbuf);
398 afs_osi_Free(tp, alen);
407 afs_readlink(OSI_VC_DECL(avc), struct uio *auio, afs_ucred_t *acred)
409 register afs_int32 code;
410 struct vrequest treq;
412 struct afs_fakestat_state fakestat;
415 AFS_STATCNT(afs_readlink);
416 afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
417 if ((code = afs_InitReq(&treq, acred)))
419 afs_InitFakeStat(&fakestat);
423 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
426 code = afs_VerifyVCache(avc, &treq);
429 if (vType(avc) != VLNK) {
433 ObtainWriteLock(&avc->lock, 158);
434 code = afs_HandleLink(avc, &treq);
435 /* finally uiomove it to user-land */
439 AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
444 ReleaseWriteLock(&avc->lock);
446 afs_PutFakeStat(&fakestat);
448 code = afs_CheckCode(code, &treq, 32);