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, 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;
90 AFS_STATCNT(afs_symlink);
91 afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
92 ICL_TYPE_STRING, aname);
94 if ((code = afs_InitReq(&treq, acred)))
97 afs_InitFakeStat(&fakestate);
101 code = afs_EvalFakeStat(&adp, &fakestate, &treq);
105 if (strlen(aname) > AFSNAMEMAX || strlen(atargetName) > AFSPATHMAX) {
110 if (afs_IsDynroot(adp)) {
111 code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
114 if (afs_IsDynrootMount(adp)) {
119 code = afs_VerifyVCache(adp, &treq);
121 code = afs_CheckCode(code, &treq, 30);
125 /** If the volume is read-only, return error without making an RPC to the
128 if (adp->f.states & CRO) {
133 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
138 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE;
139 InStatus.ClientModTime = osi_Time();
140 alen = strlen(atargetName); /* we want it to include the null */
141 if ( (*atargetName == '#' || *atargetName == '%') && alen > 1 && atargetName[alen-1] == '.') {
142 InStatus.UnixModeBits = 0644; /* mt pt: null from "." at end */
144 alen++; /* Empty string */
146 InStatus.UnixModeBits = 0755;
147 alen++; /* add in the null */
149 tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
150 volp = afs_FindVolume(&adp->f.fid, READ_LOCK); /*parent is also in same vol */
151 ObtainWriteLock(&adp->lock, 156);
153 ObtainWriteLock(&tdc->lock, 636);
154 /* No further locks: if the SymLink succeeds, it does not matter what happens
155 * to our local copy of the directory. If somebody tampers with it in the meantime,
156 * the copy will be invalidated */
157 if (!AFS_IS_DISCON_RW) {
159 tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK);
161 hostp = tc->parent->srvr->server;
162 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
163 if (adp->f.states & CForeign) {
167 RXAFS_DFSSymlink(tc->id,
168 (struct AFSFid *)&adp->f.fid.Fid,
169 aname, atargetName, &InStatus,
170 (struct AFSFid *)&newFid.Fid,
171 &OutFidStatus, &OutDirStatus,
177 RXAFS_Symlink(tc->id, (struct AFSFid *)&adp->f.fid.Fid,
178 aname, atargetName, &InStatus,
179 (struct AFSFid *)&newFid.Fid,
180 &OutFidStatus, &OutDirStatus, &tsync);
187 (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_SYMLINK,
190 newFid.Cell = adp->f.fid.Cell;
191 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
192 afs_GenFakeFid(&newFid, VREG, 0);
195 ObtainWriteLock(&afs_xvcache, 40);
198 ObtainWriteLock(&afs_xcbhash, 499);
199 afs_DequeueCallback(adp);
200 adp->f.states &= ~CStatd;
201 ReleaseWriteLock(&afs_xcbhash);
202 osi_dnlc_purgedp(adp);
204 ReleaseWriteLock(&adp->lock);
205 ReleaseWriteLock(&afs_xvcache);
207 ReleaseWriteLock(&tdc->lock);
212 /* otherwise, we should see if we can make the change to the dir locally */
213 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
214 /* we can do it locally */
215 ObtainWriteLock(&afs_xdcache, 293);
216 /* If the following fails because the name has been created in the meantime, the
217 * directory is out-of-date - the file server knows best! */
218 code = afs_dir_Create(tdc, aname, &newFid.Fid);
219 ReleaseWriteLock(&afs_xdcache);
220 if (code && !AFS_IS_DISCON_RW) {
221 ZapDCE(tdc); /* surprise error -- use invalid value */
226 ReleaseWriteLock(&tdc->lock);
229 newFid.Cell = adp->f.fid.Cell;
230 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
231 ReleaseWriteLock(&adp->lock);
233 /* now we're done with parent dir, create the link's entry. Note that
234 * no one can get a pointer to the new cache entry until we release
235 * the xvcache lock. */
236 tvc = afs_NewVCache(&newFid, hostp);
240 ReleaseWriteLock(&afs_xvcache);
243 ObtainWriteLock(&tvc->lock, 157);
244 ObtainWriteLock(&afs_xcbhash, 500);
245 tvc->f.states |= CStatd; /* have valid info */
246 tvc->f.states &= ~CBulkFetching;
248 if (adp->f.states & CForeign) {
249 tvc->f.states |= CForeign;
250 /* We don't have to worry about losing the callback since we're doing it
251 * under the afs_xvcache lock actually, afs_NewVCache may drop the
252 * afs_xvcache lock, if it calls afs_FlushVCache */
253 tvc->cbExpires = CallBack.ExpirationTime + now;
254 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
256 tvc->cbExpires = 0x7fffffff; /* never expires, they can't change */
257 /* since it never expires, we don't have to queue the callback */
259 ReleaseWriteLock(&afs_xcbhash);
261 if (AFS_IS_DISCON_RW) {
262 attrs->va_mode = InStatus.UnixModeBits;
263 afs_GenDisconStatus(adp, tvc, &newFid, attrs, &treq, VLNK);
264 code = afs_DisconCreateSymlink(tvc, atargetName, &treq);
266 /* XXX - When this goes wrong, we need to tidy up the changes we made to
267 * the parent, and get rid of the vcache we just created */
268 ReleaseWriteLock(&tvc->lock);
269 ReleaseWriteLock(&afs_xvcache);
273 afs_DisconAddDirty(tvc, VDisconCreate, 0);
275 afs_ProcessFS(tvc, &OutFidStatus, &treq);
278 if (!tvc->linkData) {
279 tvc->linkData = afs_osi_Alloc(alen);
280 osi_Assert(tvc->linkData != NULL);
281 strncpy(tvc->linkData, atargetName, alen - 1);
282 tvc->linkData[alen - 1] = 0;
284 ReleaseWriteLock(&tvc->lock);
285 ReleaseWriteLock(&afs_xvcache);
289 afs_PutFakeStat(&fakestate);
291 afs_PutVolume(volp, READ_LOCK);
293 code = afs_CheckCode(code, &treq, 31);
299 afs_MemHandleLink(struct vcache *avc, struct vrequest *areq)
303 afs_size_t offset, len;
304 afs_int32 tlen, alen;
307 AFS_STATCNT(afs_MemHandleLink);
308 /* two different formats, one for links protected 644, have a "." at
309 * the end of the file name, which we turn into a null. Others,
310 * protected 755, we add a null to the end of */
311 if (!avc->linkData) {
313 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
317 /* otherwise we have the data loaded, go for it */
322 if (avc->f.m.Mode & 0111)
323 alen = len + 1; /* regular link */
325 alen = len; /* mt point */
326 rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
327 ObtainReadLock(&tdc->lock);
328 addr = afs_MemCacheOpen(&tdc->f.inode);
330 code = afs_MemReadBlk(addr, 0, rbuf, tlen);
331 afs_MemCacheClose(addr);
332 ReleaseReadLock(&tdc->lock);
335 alen = strlen(rbuf) + 1;
336 tp = afs_osi_Alloc(alen); /* make room for terminating null */
337 osi_Assert(tp != NULL);
338 memcpy(tp, rbuf, alen);
339 osi_FreeLargeSpace(rbuf);
341 afs_osi_Free(tp, alen);
350 afs_UFSHandleLink(struct vcache *avc, struct vrequest *areq)
355 afs_size_t offset, len;
356 afs_int32 tlen, alen;
359 /* two different formats, one for links protected 644, have a "." at the
360 * end of the file name, which we turn into a null. Others, protected
361 * 755, we add a null to the end of */
362 AFS_STATCNT(afs_UFSHandleLink);
363 if (!avc->linkData) {
364 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
365 afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
366 ICL_TYPE_POINTER, tdc, ICL_TYPE_OFFSET,
367 ICL_HANDLE_OFFSET(avc->f.m.Length));
369 if (AFS_IS_DISCONNECTED)
374 /* otherwise we have the data loaded, go for it */
379 if (avc->f.m.Mode & 0111)
380 alen = len + 1; /* regular link */
382 alen = len; /* mt point */
383 rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
385 ObtainReadLock(&tdc->lock);
386 tfile = osi_UFSOpen(&tdc->f.inode);
387 code = afs_osi_Read(tfile, -1, rbuf, tlen);
389 ReleaseReadLock(&tdc->lock);
391 rbuf[alen - 1] = '\0';
392 alen = strlen(rbuf) + 1;
393 tp = afs_osi_Alloc(alen); /* make room for terminating null */
394 osi_Assert(tp != 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)
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);