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.
44 afs_DisconCreateSymlink(struct vcache *avc, char *aname,
45 struct vrequest *areq) {
47 struct osi_file *tfile;
48 afs_size_t offset, len;
50 tdc = afs_GetDCache(avc, 0, areq, &offset, &len, 0);
52 printf("afs_DisconCreateSymlink: can't get new dcache for symlink.\n");
57 avc->f.m.Length = len;
59 ObtainWriteLock(&tdc->lock, 720);
60 afs_AdjustSize(tdc, len);
62 tfile = afs_CFileOpen(tdc->f.inode);
63 afs_CFileWrite(tfile, 0, aname, len);
64 afs_CFileClose(tfile);
65 ReleaseWriteLock(&tdc->lock);
70 /* don't set CDirty in here because RPC is called synchronously */
72 afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
73 char *atargetName, struct AFS_UCRED *acred)
79 struct VenusFid newFid;
81 afs_size_t offset, len;
83 struct server *hostp = 0;
85 struct AFSStoreStatus InStatus;
86 struct AFSFetchStatus OutFidStatus, OutDirStatus;
87 struct AFSCallBack CallBack;
88 struct AFSVolSync tsync;
89 struct volume *volp = 0;
90 struct afs_fakestat_state fakestate;
94 AFS_STATCNT(afs_symlink);
95 afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
96 ICL_TYPE_STRING, aname);
98 if ((code = afs_InitReq(&treq, acred)))
101 afs_InitFakeStat(&fakestate);
105 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
109 if (strlen(aname) > AFSNAMEMAX || strlen(atargetName) > AFSPATHMAX) {
114 if (afs_IsDynroot(adp)) {
115 code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
118 if (afs_IsDynrootMount(adp)) {
123 code = afs_VerifyVCache(adp, &treq);
125 code = afs_CheckCode(code, &treq, 30);
129 /** If the volume is read-only, return error without making an RPC to the
132 if (adp->f.states & CRO) {
137 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
142 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE;
143 InStatus.ClientModTime = osi_Time();
144 alen = strlen(atargetName); /* we want it to include the null */
145 if ( (*atargetName == '#' || *atargetName == '%') && alen > 1 && atargetName[alen-1] == '.') {
146 InStatus.UnixModeBits = 0644; /* mt pt: null from "." at end */
148 alen++; /* Empty string */
150 InStatus.UnixModeBits = 0755;
151 alen++; /* add in the null */
153 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
154 volp = afs_FindVolume(&adp->f.fid, READ_LOCK); /*parent is also in same vol */
155 ObtainWriteLock(&adp->lock, 156);
157 ObtainWriteLock(&tdc->lock, 636);
158 ObtainSharedLock(&afs_xvcache, 17); /* prevent others from creating this entry */
159 /* XXX Pay attention to afs_xvcache around the whole thing!! XXX */
160 if (!AFS_IS_DISCON_RW) {
162 tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK);
164 hostp = tc->srvr->server;
165 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
166 if (adp->f.states & CForeign) {
170 RXAFS_DFSSymlink(tc->id,
171 (struct AFSFid *)&adp->f.fid.Fid,
172 aname, atargetName, &InStatus,
173 (struct AFSFid *)&newFid.Fid,
174 &OutFidStatus, &OutDirStatus,
180 RXAFS_Symlink(tc->id, (struct AFSFid *)&adp->f.fid.Fid,
181 aname, atargetName, &InStatus,
182 (struct AFSFid *)&newFid.Fid,
183 &OutFidStatus, &OutDirStatus, &tsync);
190 (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_SYMLINK,
193 #ifdef AFS_DISCON_ENV
194 newFid.Cell = adp->f.fid.Cell;
195 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
196 afs_GenFakeFid(&newFid, VREG, 0);
200 UpgradeSToWLock(&afs_xvcache, 40);
203 ObtainWriteLock(&afs_xcbhash, 499);
204 afs_DequeueCallback(adp);
205 adp->f.states &= ~CStatd;
206 ReleaseWriteLock(&afs_xcbhash);
207 osi_dnlc_purgedp(adp);
209 ReleaseWriteLock(&adp->lock);
210 ReleaseWriteLock(&afs_xvcache);
212 ReleaseWriteLock(&tdc->lock);
217 /* otherwise, we should see if we can make the change to the dir locally */
218 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
219 /* we can do it locally */
220 ObtainWriteLock(&afs_xdcache, 293);
221 code = afs_dir_Create(tdc, aname, &newFid.Fid);
222 ReleaseWriteLock(&afs_xdcache);
223 if (code && !AFS_IS_DISCON_RW) {
224 ZapDCE(tdc); /* surprise error -- use invalid value */
229 ReleaseWriteLock(&tdc->lock);
232 newFid.Cell = adp->f.fid.Cell;
233 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
234 ReleaseWriteLock(&adp->lock);
236 /* now we're done with parent dir, create the link's entry. Note that
237 * no one can get a pointer to the new cache entry until we release
238 * the xvcache lock. */
239 tvc = afs_NewVCache(&newFid, hostp);
243 ReleaseWriteLock(&afs_xvcache);
246 ObtainWriteLock(&tvc->lock, 157);
247 ObtainWriteLock(&afs_xcbhash, 500);
248 tvc->f.states |= CStatd; /* have valid info */
249 tvc->f.states &= ~CBulkFetching;
251 if (adp->f.states & CForeign) {
252 tvc->f.states |= CForeign;
253 /* We don't have to worry about losing the callback since we're doing it
254 * under the afs_xvcache lock actually, afs_NewVCache may drop the
255 * afs_xvcache lock, if it calls afs_FlushVCache */
256 tvc->cbExpires = CallBack.ExpirationTime + now;
257 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
259 tvc->cbExpires = 0x7fffffff; /* never expires, they can't change */
260 /* since it never expires, we don't have to queue the callback */
262 ReleaseWriteLock(&afs_xcbhash);
264 if (AFS_IS_DISCON_RW) {
265 #ifdef AFS_DISCON_ENV
266 attrs->va_mode = InStatus.UnixModeBits;
267 afs_GenDisconStatus(adp, tvc, &newFid, attrs, &treq, VLNK);
268 code = afs_DisconCreateSymlink(tvc, atargetName, &treq);
270 /* XXX - When this goes wrong, we need to tidy up the changes we made to
271 * the parent, and get rid of the vcache we just created */
272 ReleaseWriteLock(&tvc->lock);
273 ReleaseWriteLock(&afs_xvcache);
277 afs_DisconAddDirty(tvc, VDisconCreate, 0);
280 afs_ProcessFS(tvc, &OutFidStatus, &treq);
283 if (!tvc->linkData) {
284 tvc->linkData = (char *)afs_osi_Alloc(alen);
285 strncpy(tvc->linkData, atargetName, alen - 1);
286 tvc->linkData[alen - 1] = 0;
288 ReleaseWriteLock(&tvc->lock);
289 ReleaseWriteLock(&afs_xvcache);
293 afs_PutFakeStat(&fakestate);
295 afs_PutVolume(volp, READ_LOCK);
297 code = afs_CheckCode(code, &treq, 31);
303 afs_MemHandleLink(register struct vcache *avc, struct vrequest *areq)
305 register struct dcache *tdc;
306 register char *tp, *rbuf;
307 afs_size_t offset, len;
308 afs_int32 tlen, alen;
309 register afs_int32 code;
311 AFS_STATCNT(afs_MemHandleLink);
312 /* two different formats, one for links protected 644, have a "." at
313 * the end of the file name, which we turn into a null. Others,
314 * protected 755, we add a null to the end of */
315 if (!avc->linkData) {
317 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
321 /* otherwise we have the data loaded, go for it */
326 if (avc->f.m.Mode & 0111)
327 alen = len + 1; /* regular link */
329 alen = len; /* mt point */
330 rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
331 ObtainReadLock(&tdc->lock);
332 addr = afs_MemCacheOpen(tdc->f.inode);
334 code = afs_MemReadBlk(addr, 0, rbuf, tlen);
335 afs_MemCacheClose(addr);
336 ReleaseReadLock(&tdc->lock);
339 alen = strlen(rbuf) + 1;
340 tp = afs_osi_Alloc(alen); /* make room for terminating null */
341 memcpy(tp, rbuf, alen);
342 osi_FreeLargeSpace(rbuf);
344 afs_osi_Free(tp, alen);
353 afs_UFSHandleLink(register struct vcache *avc, struct vrequest *areq)
355 register struct dcache *tdc;
356 register char *tp, *rbuf;
358 afs_size_t offset, len;
359 afs_int32 tlen, alen;
360 register afs_int32 code;
362 /* two different formats, one for links protected 644, have a "." at the
363 * end of the file name, which we turn into a null. Others, protected
364 * 755, we add a null to the end of */
365 AFS_STATCNT(afs_UFSHandleLink);
366 if (!avc->linkData) {
367 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
368 afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
369 ICL_TYPE_POINTER, tdc, ICL_TYPE_OFFSET,
370 ICL_HANDLE_OFFSET(avc->f.m.Length));
372 if (AFS_IS_DISCONNECTED)
377 /* otherwise we have the data loaded, go for it */
382 if (avc->f.m.Mode & 0111)
383 alen = len + 1; /* regular link */
385 alen = len; /* mt point */
386 rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
388 ObtainReadLock(&tdc->lock);
389 #if defined(LINUX_USE_FH)
390 tfile = osi_UFSOpen_fh(&tdc->f.fh, tdc->f.fh_type);
392 tfile = osi_UFSOpen(tdc->f.inode);
394 code = afs_osi_Read(tfile, -1, rbuf, tlen);
396 ReleaseReadLock(&tdc->lock);
398 rbuf[alen - 1] = '\0';
399 alen = strlen(rbuf) + 1;
400 tp = afs_osi_Alloc(alen); /* make room for terminating null */
401 memcpy(tp, rbuf, alen);
402 osi_FreeLargeSpace(rbuf);
404 afs_osi_Free(tp, alen);
413 afs_readlink(OSI_VC_DECL(avc), struct uio *auio, struct AFS_UCRED *acred)
415 register afs_int32 code;
416 struct vrequest treq;
418 struct afs_fakestat_state fakestat;
421 AFS_STATCNT(afs_readlink);
422 afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
423 if ((code = afs_InitReq(&treq, acred)))
425 afs_InitFakeStat(&fakestat);
429 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
432 code = afs_VerifyVCache(avc, &treq);
435 if (vType(avc) != VLNK) {
439 ObtainWriteLock(&avc->lock, 158);
440 code = afs_HandleLink(avc, &treq);
441 /* finally uiomove it to user-land */
445 AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
450 ReleaseWriteLock(&avc->lock);
452 afs_PutFakeStat(&fakestat);
454 code = afs_CheckCode(code, &treq, 32);