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