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.
41 afs_DisconCreateSymlink(struct vcache *avc, char *aname,
42 struct vrequest *areq) {
44 struct osi_file *tfile;
45 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 afs_AdjustSize(tdc, len);
59 tfile = afs_CFileOpen(&tdc->f.inode);
60 afs_CFileWrite(tfile, 0, aname, len);
61 afs_CFileClose(tfile);
62 ReleaseWriteLock(&tdc->lock);
66 /* don't set CDirty in here because RPC is called synchronously */
68 afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
69 char *atargetName, struct vcache **tvcp, afs_ucred_t *acred)
72 struct vrequest *treq = NULL;
75 struct VenusFid newFid;
77 afs_size_t offset, len;
79 struct server *hostp = 0;
81 struct AFSStoreStatus InStatus;
82 struct AFSFetchStatus *OutFidStatus, *OutDirStatus;
83 struct AFSCallBack CallBack;
84 struct AFSVolSync tsync;
85 struct volume *volp = 0;
86 struct afs_fakestat_state fakestate;
87 struct rx_connection *rxconn;
91 AFS_STATCNT(afs_symlink);
92 afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
93 ICL_TYPE_STRING, aname);
95 OutFidStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
96 OutDirStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
98 if ((code = afs_CreateReq(&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 /* No further locks: if the SymLink succeeds, it does not matter what happens
159 * to our local copy of the directory. If somebody tampers with it in the meantime,
160 * the copy will be invalidated */
161 if (!AFS_IS_DISCON_RW) {
163 tc = afs_Conn(&adp->f.fid, treq, SHARED_LOCK, &rxconn);
165 hostp = tc->parent->srvr->server;
166 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
167 if (adp->f.states & CForeign) {
171 RXAFS_DFSSymlink(rxconn,
172 (struct AFSFid *)&adp->f.fid.Fid,
173 aname, atargetName, &InStatus,
174 (struct AFSFid *)&newFid.Fid,
175 OutFidStatus, OutDirStatus,
181 RXAFS_Symlink(rxconn, (struct AFSFid *)&adp->f.fid.Fid,
182 aname, atargetName, &InStatus,
183 (struct AFSFid *)&newFid.Fid,
184 OutFidStatus, OutDirStatus, &tsync);
191 (tc, rxconn, code, &adp->f.fid, treq, AFS_STATS_FS_RPCIDX_SYMLINK,
194 newFid.Cell = adp->f.fid.Cell;
195 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
196 afs_GenFakeFid(&newFid, VREG, 0);
199 ObtainWriteLock(&afs_xvcache, 40);
202 ObtainWriteLock(&afs_xcbhash, 499);
203 afs_DequeueCallback(adp);
204 adp->f.states &= ~CStatd;
205 ReleaseWriteLock(&afs_xcbhash);
206 osi_dnlc_purgedp(adp);
208 ReleaseWriteLock(&adp->lock);
209 ReleaseWriteLock(&afs_xvcache);
211 ReleaseWriteLock(&tdc->lock);
216 /* otherwise, we should see if we can make the change to the dir locally */
217 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, OutDirStatus, 1)) {
218 /* we can do it locally */
219 ObtainWriteLock(&afs_xdcache, 293);
220 /* If the following fails because the name has been created in the meantime, the
221 * directory is out-of-date - the file server knows best! */
222 code = afs_dir_Create(tdc, aname, &newFid.Fid);
223 ReleaseWriteLock(&afs_xdcache);
224 if (code && !AFS_IS_DISCON_RW) {
225 ZapDCE(tdc); /* surprise error -- use invalid value */
230 ReleaseWriteLock(&tdc->lock);
233 newFid.Cell = adp->f.fid.Cell;
234 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
235 ReleaseWriteLock(&adp->lock);
237 /* now we're done with parent dir, create the link's entry. Note that
238 * no one can get a pointer to the new cache entry until we release
239 * the xvcache lock. */
240 tvc = afs_NewVCache(&newFid, hostp);
244 ReleaseWriteLock(&afs_xvcache);
247 ObtainWriteLock(&tvc->lock, 157);
248 ObtainWriteLock(&afs_xcbhash, 500);
249 tvc->f.states |= CStatd; /* have valid info */
250 tvc->f.states &= ~CBulkFetching;
252 if (adp->f.states & CForeign) {
253 tvc->f.states |= CForeign;
254 /* We don't have to worry about losing the callback since we're doing it
255 * under the afs_xvcache lock actually, afs_NewVCache may drop the
256 * afs_xvcache lock, if it calls afs_FlushVCache */
257 tvc->cbExpires = CallBack.ExpirationTime + now;
258 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
260 tvc->cbExpires = 0x7fffffff; /* never expires, they can't change */
261 /* since it never expires, we don't have to queue the callback */
263 ReleaseWriteLock(&afs_xcbhash);
265 if (AFS_IS_DISCON_RW) {
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);
279 afs_ProcessFS(tvc, OutFidStatus, treq);
282 if (!tvc->linkData) {
283 tvc->linkData = afs_osi_Alloc(alen);
284 osi_Assert(tvc->linkData != NULL);
285 strncpy(tvc->linkData, atargetName, alen - 1);
286 tvc->linkData[alen - 1] = 0;
288 ReleaseWriteLock(&tvc->lock);
289 ReleaseWriteLock(&afs_xvcache);
296 afs_PutFakeStat(&fakestate);
298 afs_PutVolume(volp, READ_LOCK);
300 code = afs_CheckCode(code, treq, 31);
301 afs_DestroyReq(treq);
303 osi_FreeSmallSpace(OutFidStatus);
304 osi_FreeSmallSpace(OutDirStatus);
309 afs_MemHandleLink(struct vcache *avc, struct vrequest *areq)
313 afs_size_t offset, len;
314 afs_int32 tlen, alen;
317 AFS_STATCNT(afs_MemHandleLink);
318 /* two different formats, one for links protected 644, have a "." at
319 * the end of the file name, which we turn into a null. Others,
320 * protected 755, we add a null to the end of */
321 if (!avc->linkData) {
323 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
327 /* otherwise we have the data loaded, go for it */
332 if (avc->f.m.Mode & 0111)
333 alen = len + 1; /* regular link */
335 alen = len; /* mt point */
336 rbuf = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
337 ObtainReadLock(&tdc->lock);
338 addr = afs_MemCacheOpen(&tdc->f.inode);
340 code = afs_MemReadBlk(addr, 0, rbuf, tlen);
341 afs_MemCacheClose(addr);
342 ReleaseReadLock(&tdc->lock);
345 alen = strlen(rbuf) + 1;
346 tp = afs_osi_Alloc(alen); /* make room for terminating null */
347 osi_Assert(tp != NULL);
348 memcpy(tp, rbuf, alen);
349 osi_FreeLargeSpace(rbuf);
351 afs_osi_Free(tp, alen);
360 afs_UFSHandleLink(struct vcache *avc, struct vrequest *areq)
365 afs_size_t offset, len;
366 afs_int32 tlen, alen;
369 /* two different formats, one for links protected 644, have a "." at the
370 * end of the file name, which we turn into a null. Others, protected
371 * 755, we add a null to the end of */
372 AFS_STATCNT(afs_UFSHandleLink);
373 if (!avc->linkData) {
374 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
375 afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
376 ICL_TYPE_POINTER, tdc, ICL_TYPE_OFFSET,
377 ICL_HANDLE_OFFSET(avc->f.m.Length));
379 if (AFS_IS_DISCONNECTED)
384 /* otherwise we have the data loaded, go for it */
389 if (avc->f.m.Mode & 0111)
390 alen = len + 1; /* regular link */
392 alen = len; /* mt point */
393 rbuf = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
395 ObtainReadLock(&tdc->lock);
396 tfile = osi_UFSOpen(&tdc->f.inode);
398 ReleaseReadLock(&tdc->lock);
400 osi_FreeLargeSpace(rbuf);
403 code = afs_osi_Read(tfile, -1, rbuf, tlen);
405 ReleaseReadLock(&tdc->lock);
407 rbuf[alen - 1] = '\0';
408 alen = strlen(rbuf) + 1;
409 tp = afs_osi_Alloc(alen); /* make room for terminating null */
410 osi_Assert(tp != NULL);
411 memcpy(tp, rbuf, alen);
412 osi_FreeLargeSpace(rbuf);
414 afs_osi_Free(tp, alen);
423 afs_readlink(OSI_VC_DECL(avc), struct uio *auio, afs_ucred_t *acred)
426 struct vrequest *treq = NULL;
428 struct afs_fakestat_state fakestat;
431 AFS_STATCNT(afs_readlink);
432 afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
433 if ((code = afs_CreateReq(&treq, acred)))
435 afs_InitFakeStat(&fakestat);
439 code = afs_EvalFakeStat(&avc, &fakestat, treq);
442 code = afs_VerifyVCache(avc, treq);
445 if (vType(avc) != VLNK) {
449 ObtainWriteLock(&avc->lock, 158);
450 code = afs_HandleLink(avc, treq);
451 /* finally uiomove it to user-land */
455 AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
460 ReleaseWriteLock(&avc->lock);
462 afs_PutFakeStat(&fakestat);
464 code = afs_CheckCode(code, treq, 32);
465 afs_DestroyReq(treq);