afs: remove vestigial externs for afs_xvcache
[openafs.git] / src / afs / VNOPS / afs_vnop_symlink.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * afs_vnop_symlink.c - symlink and readlink vnodeops.
12  *
13  * Implements:
14  * afs_symlink
15  * afs_MemHandleLink
16  * afs_UFSHandleLink
17  * afs_readlink
18  *
19  */
20
21 #include <afsconfig.h>
22 #include "afs/param.h"
23
24
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"
31
32 /* Note: There is the bare bones beginning of symlink hints in the now
33  * defunct afs/afs_lookup.c file. Since they are not in use, making the call
34  * is just a performance hit.
35  */
36
37 static int
38 afs_DisconCreateSymlink(struct vcache *avc, char *aname, 
39                         struct vrequest *areq) {
40     struct dcache *tdc;
41     struct osi_file *tfile;
42     afs_size_t offset, len;
43     int code = 0;
44
45     tdc = afs_GetDCache(avc, 0, areq, &offset, &len, 0);
46     if (!tdc) {
47         /* printf("afs_DisconCreateSymlink: can't get new dcache for symlink.\n"); */
48         return ENETDOWN;
49     }
50
51     len = strlen(aname);
52     avc->f.m.Length = len;
53
54     ObtainWriteLock(&tdc->lock, 720);
55     tfile = afs_CFileOpen(&tdc->f.inode);
56     if (!tfile) {
57         code = EIO;
58         goto done;
59     }
60     afs_AdjustSize(tdc, len);
61     tdc->validPos = len;
62     afs_CFileWrite(tfile, 0, aname, len);
63     afs_CFileClose(tfile);
64  done:
65     ReleaseWriteLock(&tdc->lock);
66     return code;
67 }
68
69 /* don't set CDirty in here because RPC is called synchronously */
70 int 
71 afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs, 
72             char *atargetName, struct vcache **tvcp, afs_ucred_t *acred)
73 {
74     afs_uint32 now = 0;
75     struct vrequest *treq = NULL;
76     afs_int32 code = 0;
77     struct afs_conn *tc;
78     struct VenusFid newFid;
79     struct dcache *tdc;
80     afs_size_t offset, len;
81     afs_int32 alen;
82     struct server *hostp = 0;
83     struct vcache *tvc;
84     struct AFSStoreStatus InStatus;
85     struct AFSFetchStatus *OutFidStatus, *OutDirStatus;
86     struct AFSCallBack CallBack;
87     struct AFSVolSync tsync;
88     struct volume *volp = 0;
89     struct afs_fakestat_state fakestate;
90     struct rx_connection *rxconn;
91     XSTATS_DECLS;
92     OSI_VC_CONVERT(adp);
93
94     AFS_STATCNT(afs_symlink);
95     afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
96                ICL_TYPE_STRING, aname);
97
98     OutFidStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
99     OutDirStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
100     memset(&InStatus, 0, sizeof(InStatus));
101
102     if ((code = afs_CreateReq(&treq, acred)))
103         goto done2;
104
105     afs_InitFakeStat(&fakestate);
106
107     AFS_DISCON_LOCK();
108     
109     code = afs_EvalFakeStat(&adp, &fakestate, treq);
110     if (code)
111         goto done;
112
113     if (strlen(aname) > AFSNAMEMAX || strlen(atargetName) > AFSPATHMAX) {
114         code = ENAMETOOLONG;
115         goto done;
116     }
117
118     if (afs_IsDynroot(adp)) {
119         code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
120         goto done;
121     }
122     if (afs_IsDynrootMount(adp)) {
123         code = EROFS;
124         goto done;
125     }
126
127     code = afs_VerifyVCache(adp, treq);
128     if (code) {
129         code = afs_CheckCode(code, treq, 30);
130         goto done;
131     }
132
133     /** If the volume is read-only, return error without making an RPC to the
134       * fileserver
135       */
136     if (adp->f.states & CRO) {
137         code = EROFS;
138         goto done;
139     }
140
141     if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
142         code = ENETDOWN;
143         goto done;
144     }
145     
146     InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE;
147     InStatus.ClientModTime = osi_Time();
148     alen = strlen(atargetName); /* we want it to include the null */
149     if ( (*atargetName == '#' || *atargetName == '%') && alen > 1 && atargetName[alen-1] == '.') {
150         InStatus.UnixModeBits = 0644;   /* mt pt: null from "." at end */
151         if (alen == 1)
152             alen++;             /* Empty string */
153     } else {
154         InStatus.UnixModeBits = 0755;
155         alen++;                 /* add in the null */
156     }
157     tdc = afs_GetDCache(adp, (afs_size_t) 0, treq, &offset, &len, 1);
158     volp = afs_FindVolume(&adp->f.fid, READ_LOCK);      /*parent is also in same vol */
159     ObtainWriteLock(&adp->lock, 156);
160     if (tdc)
161         ObtainWriteLock(&tdc->lock, 636);
162     /* No further locks: if the SymLink succeeds, it does not matter what happens
163      * to our local copy of the directory. If somebody tampers with it in the meantime,
164      * the copy will be invalidated */
165     if (!AFS_IS_DISCON_RW) {
166         do {
167             tc = afs_Conn(&adp->f.fid, treq, SHARED_LOCK, &rxconn);
168             if (tc) {
169                 hostp = tc->parent->srvr->server;
170                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
171                 if (adp->f.states & CForeign) {
172                     now = osi_Time();
173                     RX_AFS_GUNLOCK();
174                     code = 
175                         RXAFS_DFSSymlink(rxconn,
176                                          (struct AFSFid *)&adp->f.fid.Fid,
177                                          aname, atargetName, &InStatus,
178                                          (struct AFSFid *)&newFid.Fid,
179                                          OutFidStatus, OutDirStatus,
180                                          &CallBack, &tsync);
181                     RX_AFS_GLOCK();
182                 } else {
183                     RX_AFS_GUNLOCK();
184                     code =
185                         RXAFS_Symlink(rxconn, (struct AFSFid *)&adp->f.fid.Fid,
186                                       aname, atargetName, &InStatus,
187                                       (struct AFSFid *)&newFid.Fid, 
188                                       OutFidStatus, OutDirStatus, &tsync);
189                     RX_AFS_GLOCK();
190                 }
191                 XSTATS_END_TIME;
192             } else
193                 code = -1;
194         } while (afs_Analyze
195                     (tc, rxconn, code, &adp->f.fid, treq, AFS_STATS_FS_RPCIDX_SYMLINK,
196                      SHARED_LOCK, NULL));
197     } else {
198         newFid.Cell = adp->f.fid.Cell;
199         newFid.Fid.Volume = adp->f.fid.Fid.Volume;
200         afs_GenFakeFid(&newFid, VREG, 0);
201     }
202
203     ObtainWriteLock(&afs_xvcache, 40);
204     if (code) {
205         if (code < 0) {
206             afs_StaleVCache(adp);
207         }
208         ReleaseWriteLock(&adp->lock);
209         ReleaseWriteLock(&afs_xvcache);
210         if (tdc) {
211             ReleaseWriteLock(&tdc->lock);
212             afs_PutDCache(tdc);
213         }
214         goto done;
215     }
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 */
226             DZap(tdc);
227         }
228     }
229     if (tdc) {
230         ReleaseWriteLock(&tdc->lock);
231         afs_PutDCache(tdc);
232     }
233     newFid.Cell = adp->f.fid.Cell;
234     newFid.Fid.Volume = adp->f.fid.Fid.Volume;
235     ReleaseWriteLock(&adp->lock);
236
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);
241     if (!tvc)
242     {
243         code = -2;
244         ReleaseWriteLock(&afs_xvcache);
245         goto done;
246     }
247     ObtainWriteLock(&tvc->lock, 157);
248     ObtainWriteLock(&afs_xcbhash, 500);
249     tvc->f.states |= CStatd;    /* have valid info */
250     tvc->f.states &= ~CBulkFetching;
251
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);
259     } else {
260         tvc->cbExpires = 0x7fffffff;    /* never expires, they can't change */
261         /* since it never expires, we don't have to queue the callback */
262     }
263     ReleaseWriteLock(&afs_xcbhash);
264
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);
269         if (code) {
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);
274             afs_PutVCache(tvc);
275             goto done;
276         }
277         afs_DisconAddDirty(tvc, VDisconCreate, 0);
278     } else {
279         afs_ProcessFS(tvc, OutFidStatus, treq);
280     }
281
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;
287     }
288     ReleaseWriteLock(&tvc->lock);
289     ReleaseWriteLock(&afs_xvcache);
290     if (tvcp)
291         *tvcp = tvc;
292     else
293         afs_PutVCache(tvc);
294     code = 0;
295   done:
296     afs_PutFakeStat(&fakestate);
297     if (volp)
298         afs_PutVolume(volp, READ_LOCK);
299     AFS_DISCON_UNLOCK();
300     code = afs_CheckCode(code, treq, 31);
301     afs_DestroyReq(treq);
302   done2:
303     osi_FreeSmallSpace(OutFidStatus);
304     osi_FreeSmallSpace(OutDirStatus);
305     return code;
306 }
307
308 int
309 afs_MemHandleLink(struct vcache *avc, struct vrequest *areq)
310 {
311     struct dcache *tdc;
312     char *tp, *rbuf;
313     afs_size_t offset, len;
314     afs_int32 tlen, alen;
315     afs_int32 code;
316
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) {
322         void *addr;
323         tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
324         if (!tdc) {
325             return EIO;
326         }
327         /* otherwise we have the data loaded, go for it */
328         if (len > 1024) {
329             afs_PutDCache(tdc);
330             return EFAULT;
331         }
332         if (avc->f.m.Mode & 0111)
333             alen = len + 1;     /* regular link */
334         else
335             alen = len;         /* mt point */
336         rbuf = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
337         ObtainReadLock(&tdc->lock);
338         addr = afs_MemCacheOpen(&tdc->f.inode);
339         tlen = len;
340         code = afs_MemReadBlk(addr, 0, rbuf, tlen);
341         afs_MemCacheClose(addr);
342         ReleaseReadLock(&tdc->lock);
343         afs_PutDCache(tdc);
344         rbuf[alen - 1] = 0;
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);
350         if (code != len) {
351             afs_osi_Free(tp, alen);
352             return EIO;
353         }
354         avc->linkData = tp;
355     }
356     return 0;
357 }
358
359 int
360 afs_UFSHandleLink(struct vcache *avc, struct vrequest *areq)
361 {
362     struct dcache *tdc;
363     char *tp, *rbuf;
364     void *tfile;
365     afs_size_t offset, len;
366     afs_int32 tlen, alen;
367     afs_int32 code;
368
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));
378         if (!tdc) {
379             if (AFS_IS_DISCONNECTED)
380                 return ENETDOWN;
381             else
382                 return EIO;
383         }
384         /* otherwise we have the data loaded, go for it */
385         if (len > 1024) {
386             afs_PutDCache(tdc);
387             return EFAULT;
388         }
389         if (avc->f.m.Mode & 0111)
390             alen = len + 1;     /* regular link */
391         else
392             alen = len;         /* mt point */
393         rbuf = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
394         tlen = len;
395         ObtainReadLock(&tdc->lock);
396         tfile = osi_UFSOpen(&tdc->f.inode);
397         if (!tfile) {
398             ReleaseReadLock(&tdc->lock);
399             afs_PutDCache(tdc);
400             osi_FreeLargeSpace(rbuf);
401             return EIO;
402         }
403         code = afs_osi_Read(tfile, -1, rbuf, tlen);
404         osi_UFSClose(tfile);
405         ReleaseReadLock(&tdc->lock);
406         afs_PutDCache(tdc);
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);
413         if (code != tlen) {
414             afs_osi_Free(tp, alen);
415             return EIO;
416         }
417         avc->linkData = tp;
418     }
419     return 0;
420 }
421
422 int
423 afs_readlink(OSI_VC_DECL(avc), struct uio *auio, afs_ucred_t *acred)
424 {
425     afs_int32 code;
426     struct vrequest *treq = NULL;
427     char *tp;
428     struct afs_fakestat_state fakestat;
429     OSI_VC_CONVERT(avc);
430
431     AFS_STATCNT(afs_readlink);
432     afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
433     if ((code = afs_CreateReq(&treq, acred)))
434         return code;
435     afs_InitFakeStat(&fakestat);
436
437     AFS_DISCON_LOCK();
438     
439     code = afs_EvalFakeStat(&avc, &fakestat, treq);
440     if (code)
441         goto done;
442     code = afs_VerifyVCache(avc, treq);
443     if (code)
444         goto done;
445     if (vType(avc) != VLNK) {
446         code = EINVAL;
447         goto done;
448     }
449     ObtainWriteLock(&avc->lock, 158);
450     code = afs_HandleLink(avc, treq);
451     /* finally uiomove it to user-land */
452     if (code == 0) {
453         tp = avc->linkData;
454         if (tp)
455             AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
456         else {
457             code = EIO;
458         }
459     }
460     ReleaseWriteLock(&avc->lock);
461   done:
462     afs_PutFakeStat(&fakestat);
463     AFS_DISCON_UNLOCK();
464     code = afs_CheckCode(code, treq, 32);
465     afs_DestroyReq(treq);
466     return code;
467 }