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;
34 /* Note: There is the bare bones beginning of symlink hints in the now
35 * defunct afs/afs_lookup.c file. Since they are not in use, making the call
36 * is just a performance hit.
40 afs_DisconCreateSymlink(struct vcache *avc, char *aname,
41 struct vrequest *areq) {
43 struct osi_file *tfile;
44 afs_size_t offset, len;
47 tdc = afs_GetDCache(avc, 0, areq, &offset, &len, 0);
49 /* printf("afs_DisconCreateSymlink: can't get new dcache for symlink.\n"); */
54 avc->f.m.Length = len;
56 ObtainWriteLock(&tdc->lock, 720);
57 tfile = afs_CFileOpen(&tdc->f.inode);
62 afs_AdjustSize(tdc, len);
64 afs_CFileWrite(tfile, 0, aname, len);
65 afs_CFileClose(tfile);
67 ReleaseWriteLock(&tdc->lock);
71 /* don't set CDirty in here because RPC is called synchronously */
73 afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
74 char *atargetName, struct vcache **tvcp, afs_ucred_t *acred)
77 struct vrequest *treq = NULL;
80 struct VenusFid newFid;
82 afs_size_t offset, len;
84 struct server *hostp = 0;
86 struct AFSStoreStatus InStatus;
87 struct AFSFetchStatus *OutFidStatus, *OutDirStatus;
88 struct AFSCallBack CallBack;
89 struct AFSVolSync tsync;
90 struct volume *volp = 0;
91 struct afs_fakestat_state fakestate;
92 struct rx_connection *rxconn;
96 AFS_STATCNT(afs_symlink);
97 afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
98 ICL_TYPE_STRING, aname);
100 OutFidStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
101 OutDirStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
102 memset(&InStatus, 0, sizeof(InStatus));
104 if ((code = afs_CreateReq(&treq, acred)))
107 afs_InitFakeStat(&fakestate);
111 code = afs_EvalFakeStat(&adp, &fakestate, treq);
115 if (strlen(aname) > AFSNAMEMAX || strlen(atargetName) > AFSPATHMAX) {
120 if (afs_IsDynroot(adp)) {
121 code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
124 if (afs_IsDynrootMount(adp)) {
129 code = afs_VerifyVCache(adp, treq);
131 code = afs_CheckCode(code, treq, 30);
135 /** If the volume is read-only, return error without making an RPC to the
138 if (adp->f.states & CRO) {
143 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
148 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE;
149 InStatus.ClientModTime = osi_Time();
150 alen = strlen(atargetName); /* we want it to include the null */
151 if ( (*atargetName == '#' || *atargetName == '%') && alen > 1 && atargetName[alen-1] == '.') {
152 InStatus.UnixModeBits = 0644; /* mt pt: null from "." at end */
154 alen++; /* Empty string */
156 InStatus.UnixModeBits = 0755;
157 alen++; /* add in the null */
159 tdc = afs_GetDCache(adp, (afs_size_t) 0, treq, &offset, &len, 1);
160 volp = afs_FindVolume(&adp->f.fid, READ_LOCK); /*parent is also in same vol */
161 ObtainWriteLock(&adp->lock, 156);
163 ObtainWriteLock(&tdc->lock, 636);
164 /* No further locks: if the SymLink succeeds, it does not matter what happens
165 * to our local copy of the directory. If somebody tampers with it in the meantime,
166 * the copy will be invalidated */
167 if (!AFS_IS_DISCON_RW) {
169 tc = afs_Conn(&adp->f.fid, treq, SHARED_LOCK, &rxconn);
171 hostp = tc->parent->srvr->server;
172 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
173 if (adp->f.states & CForeign) {
177 RXAFS_DFSSymlink(rxconn,
178 (struct AFSFid *)&adp->f.fid.Fid,
179 aname, atargetName, &InStatus,
180 (struct AFSFid *)&newFid.Fid,
181 OutFidStatus, OutDirStatus,
187 RXAFS_Symlink(rxconn, (struct AFSFid *)&adp->f.fid.Fid,
188 aname, atargetName, &InStatus,
189 (struct AFSFid *)&newFid.Fid,
190 OutFidStatus, OutDirStatus, &tsync);
197 (tc, rxconn, code, &adp->f.fid, treq, AFS_STATS_FS_RPCIDX_SYMLINK,
200 newFid.Cell = adp->f.fid.Cell;
201 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
202 afs_GenFakeFid(&newFid, VREG, 0);
205 ObtainWriteLock(&afs_xvcache, 40);
208 afs_StaleVCache(adp);
210 ReleaseWriteLock(&adp->lock);
211 ReleaseWriteLock(&afs_xvcache);
213 ReleaseWriteLock(&tdc->lock);
218 /* otherwise, we should see if we can make the change to the dir locally */
219 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, OutDirStatus, 1)) {
220 /* we can do it locally */
221 ObtainWriteLock(&afs_xdcache, 293);
222 /* If the following fails because the name has been created in the meantime, the
223 * directory is out-of-date - the file server knows best! */
224 code = afs_dir_Create(tdc, aname, &newFid.Fid);
225 ReleaseWriteLock(&afs_xdcache);
226 if (code && !AFS_IS_DISCON_RW) {
227 ZapDCE(tdc); /* surprise error -- use invalid value */
232 ReleaseWriteLock(&tdc->lock);
235 newFid.Cell = adp->f.fid.Cell;
236 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
237 ReleaseWriteLock(&adp->lock);
239 /* now we're done with parent dir, create the link's entry. Note that
240 * no one can get a pointer to the new cache entry until we release
241 * the xvcache lock. */
242 tvc = afs_NewVCache(&newFid, hostp);
246 ReleaseWriteLock(&afs_xvcache);
249 ObtainWriteLock(&tvc->lock, 157);
250 ObtainWriteLock(&afs_xcbhash, 500);
251 tvc->f.states |= CStatd; /* have valid info */
252 tvc->f.states &= ~CBulkFetching;
254 if (adp->f.states & CForeign) {
255 tvc->f.states |= CForeign;
256 /* We don't have to worry about losing the callback since we're doing it
257 * under the afs_xvcache lock actually, afs_NewVCache may drop the
258 * afs_xvcache lock, if it calls afs_FlushVCache */
259 tvc->cbExpires = CallBack.ExpirationTime + now;
260 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
262 tvc->cbExpires = 0x7fffffff; /* never expires, they can't change */
263 /* since it never expires, we don't have to queue the callback */
265 ReleaseWriteLock(&afs_xcbhash);
267 if (AFS_IS_DISCON_RW) {
268 attrs->va_mode = InStatus.UnixModeBits;
269 afs_GenDisconStatus(adp, tvc, &newFid, attrs, treq, VLNK);
270 code = afs_DisconCreateSymlink(tvc, atargetName, treq);
272 /* XXX - When this goes wrong, we need to tidy up the changes we made to
273 * the parent, and get rid of the vcache we just created */
274 ReleaseWriteLock(&tvc->lock);
275 ReleaseWriteLock(&afs_xvcache);
279 afs_DisconAddDirty(tvc, VDisconCreate, 0);
281 afs_ProcessFS(tvc, OutFidStatus, treq);
284 if (!tvc->linkData) {
285 tvc->linkData = afs_osi_Alloc(alen);
286 osi_Assert(tvc->linkData != NULL);
287 strncpy(tvc->linkData, atargetName, alen - 1);
288 tvc->linkData[alen - 1] = 0;
290 ReleaseWriteLock(&tvc->lock);
291 ReleaseWriteLock(&afs_xvcache);
298 afs_PutFakeStat(&fakestate);
300 afs_PutVolume(volp, READ_LOCK);
302 code = afs_CheckCode(code, treq, 31);
303 afs_DestroyReq(treq);
305 osi_FreeSmallSpace(OutFidStatus);
306 osi_FreeSmallSpace(OutDirStatus);
311 afs_MemHandleLink(struct vcache *avc, struct vrequest *areq)
315 afs_size_t offset, len;
316 afs_int32 tlen, alen;
319 AFS_STATCNT(afs_MemHandleLink);
320 /* two different formats, one for links protected 644, have a "." at
321 * the end of the file name, which we turn into a null. Others,
322 * protected 755, we add a null to the end of */
323 if (!avc->linkData) {
325 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
329 /* otherwise we have the data loaded, go for it */
334 if (avc->f.m.Mode & 0111)
335 alen = len + 1; /* regular link */
337 alen = len; /* mt point */
338 rbuf = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
339 ObtainReadLock(&tdc->lock);
340 addr = afs_MemCacheOpen(&tdc->f.inode);
342 code = afs_MemReadBlk(addr, 0, rbuf, tlen);
343 afs_MemCacheClose(addr);
344 ReleaseReadLock(&tdc->lock);
347 alen = strlen(rbuf) + 1;
348 tp = afs_osi_Alloc(alen); /* make room for terminating null */
349 osi_Assert(tp != NULL);
350 memcpy(tp, rbuf, alen);
351 osi_FreeLargeSpace(rbuf);
353 afs_osi_Free(tp, alen);
362 afs_UFSHandleLink(struct vcache *avc, struct vrequest *areq)
367 afs_size_t offset, len;
368 afs_int32 tlen, alen;
371 /* two different formats, one for links protected 644, have a "." at the
372 * end of the file name, which we turn into a null. Others, protected
373 * 755, we add a null to the end of */
374 AFS_STATCNT(afs_UFSHandleLink);
375 if (!avc->linkData) {
376 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
377 afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
378 ICL_TYPE_POINTER, tdc, ICL_TYPE_OFFSET,
379 ICL_HANDLE_OFFSET(avc->f.m.Length));
381 if (AFS_IS_DISCONNECTED)
386 /* otherwise we have the data loaded, go for it */
391 if (avc->f.m.Mode & 0111)
392 alen = len + 1; /* regular link */
394 alen = len; /* mt point */
395 rbuf = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
397 ObtainReadLock(&tdc->lock);
398 tfile = osi_UFSOpen(&tdc->f.inode);
400 ReleaseReadLock(&tdc->lock);
402 osi_FreeLargeSpace(rbuf);
405 code = afs_osi_Read(tfile, -1, rbuf, tlen);
407 ReleaseReadLock(&tdc->lock);
409 rbuf[alen - 1] = '\0';
410 alen = strlen(rbuf) + 1;
411 tp = afs_osi_Alloc(alen); /* make room for terminating null */
412 osi_Assert(tp != NULL);
413 memcpy(tp, rbuf, alen);
414 osi_FreeLargeSpace(rbuf);
416 afs_osi_Free(tp, alen);
425 afs_readlink(OSI_VC_DECL(avc), struct uio *auio, afs_ucred_t *acred)
428 struct vrequest *treq = NULL;
430 struct afs_fakestat_state fakestat;
433 AFS_STATCNT(afs_readlink);
434 afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
435 if ((code = afs_CreateReq(&treq, acred)))
437 afs_InitFakeStat(&fakestat);
441 code = afs_EvalFakeStat(&avc, &fakestat, treq);
444 code = afs_VerifyVCache(avc, treq);
447 if (vType(avc) != VLNK) {
451 ObtainWriteLock(&avc->lock, 158);
452 code = afs_HandleLink(avc, treq);
453 /* finally uiomove it to user-land */
457 AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
462 ReleaseWriteLock(&avc->lock);
464 afs_PutFakeStat(&fakestat);
466 code = afs_CheckCode(code, treq, 32);
467 afs_DestroyReq(treq);