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