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)
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(AFS_SMALLOCSIZ);
96 OutDirStatus = osi_AllocSmallSpace(AFS_SMALLOCSIZ);
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 /* 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);
302 osi_FreeSmallSpace(OutFidStatus);
303 osi_FreeSmallSpace(OutDirStatus);
308 afs_MemHandleLink(struct vcache *avc, struct vrequest *areq)
312 afs_size_t offset, len;
313 afs_int32 tlen, alen;
316 AFS_STATCNT(afs_MemHandleLink);
317 /* two different formats, one for links protected 644, have a "." at
318 * the end of the file name, which we turn into a null. Others,
319 * protected 755, we add a null to the end of */
320 if (!avc->linkData) {
322 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
326 /* otherwise we have the data loaded, go for it */
331 if (avc->f.m.Mode & 0111)
332 alen = len + 1; /* regular link */
334 alen = len; /* mt point */
335 rbuf = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
336 ObtainReadLock(&tdc->lock);
337 addr = afs_MemCacheOpen(&tdc->f.inode);
339 code = afs_MemReadBlk(addr, 0, rbuf, tlen);
340 afs_MemCacheClose(addr);
341 ReleaseReadLock(&tdc->lock);
344 alen = strlen(rbuf) + 1;
345 tp = afs_osi_Alloc(alen); /* make room for terminating null */
346 osi_Assert(tp != NULL);
347 memcpy(tp, rbuf, alen);
348 osi_FreeLargeSpace(rbuf);
350 afs_osi_Free(tp, alen);
359 afs_UFSHandleLink(struct vcache *avc, struct vrequest *areq)
364 afs_size_t offset, len;
365 afs_int32 tlen, alen;
368 /* two different formats, one for links protected 644, have a "." at the
369 * end of the file name, which we turn into a null. Others, protected
370 * 755, we add a null to the end of */
371 AFS_STATCNT(afs_UFSHandleLink);
372 if (!avc->linkData) {
373 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
374 afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
375 ICL_TYPE_POINTER, tdc, ICL_TYPE_OFFSET,
376 ICL_HANDLE_OFFSET(avc->f.m.Length));
378 if (AFS_IS_DISCONNECTED)
383 /* otherwise we have the data loaded, go for it */
388 if (avc->f.m.Mode & 0111)
389 alen = len + 1; /* regular link */
391 alen = len; /* mt point */
392 rbuf = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
394 ObtainReadLock(&tdc->lock);
395 tfile = osi_UFSOpen(&tdc->f.inode);
396 code = afs_osi_Read(tfile, -1, rbuf, tlen);
398 ReleaseReadLock(&tdc->lock);
400 rbuf[alen - 1] = '\0';
401 alen = strlen(rbuf) + 1;
402 tp = afs_osi_Alloc(alen); /* make room for terminating null */
403 osi_Assert(tp != NULL);
404 memcpy(tp, rbuf, alen);
405 osi_FreeLargeSpace(rbuf);
407 afs_osi_Free(tp, alen);
416 afs_readlink(OSI_VC_DECL(avc), struct uio *auio, afs_ucred_t *acred)
419 struct vrequest treq;
421 struct afs_fakestat_state fakestat;
424 AFS_STATCNT(afs_readlink);
425 afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
426 if ((code = afs_InitReq(&treq, acred)))
428 afs_InitFakeStat(&fakestat);
432 code = afs_EvalFakeStat(&avc, &fakestat, &treq);
435 code = afs_VerifyVCache(avc, &treq);
438 if (vType(avc) != VLNK) {
442 ObtainWriteLock(&avc->lock, 158);
443 code = afs_HandleLink(avc, &treq);
444 /* finally uiomove it to user-land */
448 AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
453 ReleaseWriteLock(&avc->lock);
455 afs_PutFakeStat(&fakestat);
457 code = afs_CheckCode(code, &treq, 32);