tru64-avoid-leaking-vnodes-when-failure-20020320
[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 RCSID("$Header$");
25
26 #include "../afs/sysincludes.h" /* Standard vendor system headers */
27 #include "../afs/afsincludes.h" /* Afs-based standard headers */
28 #include "../afs/afs_stats.h" /* statistics */
29 #include "../afs/afs_cbqueue.h"
30 #include "../afs/nfsclient.h"
31 #include "../afs/afs_osidnlc.h"
32
33 extern afs_rwlock_t afs_xvcache;
34 extern afs_rwlock_t afs_xcbhash;
35
36 /* Note: There is the bare bones beginning of symlink hints in the now
37  * defunct afs/afs_lookup.c file. Since they are not in use, making the call
38  * is just a performance hit.
39  */
40
41
42 /* don't set CDirty in here because RPC is called synchronously */
43 afs_symlink
44 #ifdef  AFS_OSF_ENV
45   (ndp, attrs, atargetName)
46     struct nameidata *ndp;
47     struct vattr *attrs;
48     register char *atargetName; {
49     register struct vcache *adp = (struct vcache *)ndp->ni_dvp;
50     char *aname = ndp->ni_dent.d_name;
51     struct ucred *acred = ndp->ni_cred;
52 #else   /* AFS_OSF_ENV */
53   (OSI_VC_ARG(adp), aname, attrs, atargetName, acred)
54     OSI_VC_DECL(adp);
55     char *atargetName;
56     char *aname;
57     struct vattr *attrs;
58     struct AFS_UCRED *acred; {
59 #endif
60     afs_uint32 now;
61     struct vrequest treq;
62     afs_int32 code;
63     struct conn *tc;
64     struct VenusFid newFid;
65     struct dcache *tdc;
66     afs_size_t offset, len;
67     afs_int32 alen;
68     struct server *hostp=0;
69     struct vcache *tvc;
70     struct AFSStoreStatus InStatus;
71     struct AFSFetchStatus OutFidStatus, OutDirStatus;
72     struct AFSCallBack CallBack;
73     struct AFSVolSync tsync;
74     struct volume*    volp=0;
75     XSTATS_DECLS
76     OSI_VC_CONVERT(adp)
77     
78     AFS_STATCNT(afs_symlink);
79     afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
80                 ICL_TYPE_STRING, aname);
81
82     if (afs_IsDynroot(adp)) {
83         code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
84         goto done2;
85     }
86
87     if (code = afs_InitReq(&treq, acred))
88         goto done2;
89
90     code = afs_VerifyVCache(adp, &treq);
91     if (code) { 
92       code = afs_CheckCode(code, &treq, 30);
93       goto done2;
94     }
95
96     /** If the volume is read-only, return error without making an RPC to the
97       * fileserver
98       */
99     if ( adp->states & CRO ) {
100         code = EROFS;
101         goto done2;
102     }
103
104     InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE;
105     InStatus.ClientModTime = osi_Time();
106     alen = strlen(atargetName);     /* we want it to include the null */
107     if (*atargetName == '#' || *atargetName == '%') {
108         InStatus.UnixModeBits = 0644;   /* mt pt: null from "." at end */
109         if (alen == 1) alen++;  /* Empty string */
110     } else {
111         InStatus.UnixModeBits = 0755;
112         alen++;     /* add in the null */
113     }
114     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
115     volp = afs_FindVolume(&adp->fid, READ_LOCK); /*parent is also in same vol*/
116     ObtainWriteLock(&adp->lock,156);
117     ObtainWriteLock(&tdc->lock, 636);
118     ObtainSharedLock(&afs_xvcache,17);  /* prevent others from creating this entry */
119     /* XXX Pay attention to afs_xvcache around the whole thing!! XXX */
120     do {
121         tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
122         if (tc) {
123             hostp = tc->srvr->server;
124             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
125             if (adp->states & CForeign) {
126                 now = osi_Time();
127 #ifdef RX_ENABLE_LOCKS
128                 AFS_GUNLOCK();
129 #endif /* RX_ENABLE_LOCKS */
130                 code = RXAFS_DFSSymlink(tc->id, (struct AFSFid *) &adp->fid.Fid, aname,
131                                      atargetName, &InStatus, (struct AFSFid *) &newFid.Fid,
132                                      &OutFidStatus, &OutDirStatus, &CallBack, &tsync);
133 #ifdef RX_ENABLE_LOCKS
134                 AFS_GLOCK();
135 #endif /* RX_ENABLE_LOCKS */
136             } else {
137 #ifdef RX_ENABLE_LOCKS
138                 AFS_GUNLOCK();
139 #endif /* RX_ENABLE_LOCKS */
140                 code = RXAFS_Symlink(tc->id, (struct AFSFid *) &adp->fid.Fid, aname,
141                                      atargetName, &InStatus, (struct AFSFid *) &newFid.Fid,
142                                      &OutFidStatus, &OutDirStatus, &tsync);
143 #ifdef RX_ENABLE_LOCKS
144                 AFS_GLOCK();
145 #endif /* RX_ENABLE_LOCKS */
146             }
147           XSTATS_END_TIME;
148         }
149         else code = -1;
150     } while 
151        (afs_Analyze(tc, code, &adp->fid, &treq,
152                    AFS_STATS_FS_RPCIDX_SYMLINK, SHARED_LOCK, (struct cell *)0));
153
154     UpgradeSToWLock(&afs_xvcache,40);
155     if (code) {
156         if (code < 0) {
157           ObtainWriteLock(&afs_xcbhash, 499);
158           afs_DequeueCallback(adp);
159           adp->states &= ~CStatd;
160           ReleaseWriteLock(&afs_xcbhash);
161           osi_dnlc_purgedp(adp);
162         }
163         ReleaseWriteLock(&adp->lock);
164         ReleaseWriteLock(&afs_xvcache);
165         if (tdc) {
166             ReleaseWriteLock(&tdc->lock);
167             afs_PutDCache(tdc);
168         }
169         goto done;
170     }
171     /* otherwise, we should see if we can make the change to the dir locally */
172     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
173         /* we can do it locally */
174         code = afs_dir_Create(&tdc->f.inode, aname, &newFid.Fid);
175         if (code) { 
176             ZapDCE(tdc);        /* surprise error -- use invalid value */
177             DZap(&tdc->f.inode);
178         }
179     }
180     if (tdc) {
181         ReleaseWriteLock(&tdc->lock);
182         afs_PutDCache(tdc);
183     }
184     newFid.Cell = adp->fid.Cell;
185     newFid.Fid.Volume = adp->fid.Fid.Volume;
186     ReleaseWriteLock(&adp->lock);
187
188     /* now we're done with parent dir, create the link's entry.  Note that
189      * no one can get a pointer to the new cache entry until we release 
190      * the xvcache lock. */
191     tvc = afs_NewVCache(&newFid, hostp, 1, WRITE_LOCK);
192     ObtainWriteLock(&tvc->lock,157);
193     ObtainWriteLock(&afs_xcbhash, 500);
194     tvc->states |= CStatd;              /* have valid info */
195     tvc->states &= ~CBulkFetching;
196
197     if (adp->states & CForeign) {
198         tvc->states |= CForeign;
199         /* We don't have to worry about losing the callback since we're doing it 
200          * under the afs_xvcache lock actually, afs_NewVCache may drop the 
201          * afs_xvcache lock, if it calls afs_FlushVCache */
202         tvc->cbExpires = CallBack.ExpirationTime + now;
203         afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
204     } else {
205         tvc->cbExpires = 0x7fffffff;    /* never expires, they can't change */
206      /* since it never expires, we don't have to queue the callback */
207     }
208     ReleaseWriteLock(&afs_xcbhash);
209     afs_ProcessFS(tvc, &OutFidStatus, &treq);
210
211     if (!tvc->linkData) {
212         tvc->linkData = (char *) afs_osi_Alloc(alen);
213         strncpy(tvc->linkData, atargetName, alen-1);
214         tvc->linkData[alen-1] = 0;
215     }
216     ReleaseWriteLock(&tvc->lock);
217     ReleaseWriteLock(&afs_xvcache);
218     afs_PutVCache(tvc, WRITE_LOCK);
219     code = 0;
220 done:
221     if ( volp ) 
222         afs_PutVolume(volp, READ_LOCK);
223     code = afs_CheckCode(code, &treq, 31);
224 done2:
225 #ifdef  AFS_OSF_ENV
226     AFS_RELE(ndp->ni_dvp);
227 #endif  /* AFS_OSF_ENV */
228     return code;
229 }
230
231 afs_MemHandleLink(avc, areq)
232      register struct vcache *avc;
233      struct vrequest *areq;
234   {
235       register struct dcache *tdc;
236       register char *tp, *rbuf;
237       afs_size_t offset, len;
238       afs_int32 tlen, alen;
239       register afs_int32 code;
240
241       AFS_STATCNT(afs_MemHandleLink);
242       /* two different formats, one for links protected 644, have a "." at
243          the end of the file name, which we turn into a null.  Others, 
244          protected 755, we add a null to the end of */
245       if (!avc->linkData) {
246           void *addr;
247           tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
248           if (!tdc) {
249               return EIO;
250           }
251           /* otherwise we have the data loaded, go for it */
252           if (len > 1024) {
253               afs_PutDCache(tdc);
254               return EFAULT;
255           }
256           if (avc->m.Mode       & 0111) alen = len+1;   /* regular link */
257           else alen = len;                      /* mt point */
258           rbuf = (char *) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
259           ObtainReadLock(&tdc->lock);
260           addr = afs_MemCacheOpen(tdc->f.inode);
261           tlen = len;
262           code = afs_MemReadBlk(addr, 0, rbuf, tlen);
263           afs_MemCacheClose(addr);
264           ReleaseReadLock(&tdc->lock);
265           afs_PutDCache(tdc);
266           rbuf[alen-1] = 0;
267           alen = strlen(rbuf) + 1;
268           tp = afs_osi_Alloc(alen); /* make room for terminating null */
269           memcpy(tp, rbuf, alen);
270           osi_FreeLargeSpace(rbuf);
271           if (code != len) {
272               afs_osi_Free(tp, alen);
273               return EIO;
274           }
275           avc->linkData = tp;
276       }
277       return 0;
278   }
279
280 afs_UFSHandleLink(avc, areq)
281     register struct vcache *avc;
282     struct vrequest *areq; 
283 {
284     register struct dcache *tdc;
285     register char *tp, *rbuf;
286     char *tfile;
287     afs_size_t offset, len;
288     afs_int32 tlen, alen;
289     register afs_int32 code;
290
291     /* two different formats, one for links protected 644, have a "." at the end
292         of the file name, which we turn into a null.  Others, protected 755,
293         we add a null to the end of */
294    AFS_STATCNT(afs_UFSHandleLink);
295     if (!avc->linkData) {
296         tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
297         afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
298                         ICL_TYPE_POINTER, tdc,
299                         ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
300         if (!tdc) {
301             return EIO;
302         }
303         /* otherwise we have the data loaded, go for it */
304         if (len > 1024) {
305             afs_PutDCache(tdc);
306             return EFAULT;
307         }
308         if (avc->m.Mode & 0111) alen = len+1;   /* regular link */
309         else alen = len;                        /* mt point */
310         rbuf = (char *) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
311         tlen = len;
312         ObtainReadLock(&tdc->lock);
313         tfile = osi_UFSOpen (tdc->f.inode);
314         code = afs_osi_Read(tfile, -1, rbuf, tlen);
315         osi_UFSClose(tfile);
316         ReleaseReadLock(&tdc->lock);
317         afs_PutDCache(tdc);
318         rbuf[alen-1] = '\0';
319         alen = strlen(rbuf) + 1;
320         tp = afs_osi_Alloc(alen);       /* make room for terminating null */
321         memcpy(tp, rbuf, alen);
322         osi_FreeLargeSpace(rbuf);
323         if (code != tlen) {
324             afs_osi_Free(tp, alen);
325             return EIO;
326         }
327         avc->linkData = tp;
328     }
329     return 0;
330 }
331
332 afs_readlink(OSI_VC_ARG(avc), auio, acred)
333     OSI_VC_DECL(avc);
334     struct uio *auio;
335     struct AFS_UCRED *acred; 
336 {
337     register afs_int32 code;
338     struct vrequest treq;
339     register char *tp;
340     OSI_VC_CONVERT(avc)
341
342     AFS_STATCNT(afs_readlink);
343     afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
344     if (code = afs_InitReq(&treq, acred)) return code;
345     code = afs_VerifyVCache(avc, &treq);
346     if (code) goto done;
347     if (vType(avc) != VLNK) {
348         code = EINVAL;
349         goto done;
350     }
351     ObtainWriteLock(&avc->lock,158);
352     code = afs_HandleLink(avc, &treq);
353     /* finally uiomove it to user-land */
354     if (code == 0) {
355         tp = avc->linkData;
356         if (tp) AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
357         else {
358             code = EIO;
359         }
360     }
361     ReleaseWriteLock(&avc->lock);
362 done:
363     code = afs_CheckCode(code, &treq, 32);
364     return code;
365 }