dux-vnops-cleanup-20041224
[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     code = afs_EvalFakeStat(&adp, &fakestate, &treq);
80     if (code)
81         goto done;
82
83     if (strlen(aname) > AFSNAMEMAX || strlen(atargetName) > AFSPATHMAX) {
84         code = ENAMETOOLONG;
85         goto done;
86     }
87
88     if (afs_IsDynroot(adp)) {
89         code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
90         goto done;
91     }
92
93     code = afs_VerifyVCache(adp, &treq);
94     if (code) {
95         code = afs_CheckCode(code, &treq, 30);
96         goto done;
97     }
98
99     /** If the volume is read-only, return error without making an RPC to the
100       * fileserver
101       */
102     if (adp->states & CRO) {
103         code = EROFS;
104         goto done;
105     }
106
107     InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE;
108     InStatus.ClientModTime = osi_Time();
109     alen = strlen(atargetName); /* we want it to include the null */
110     if (*atargetName == '#' || *atargetName == '%') {
111         InStatus.UnixModeBits = 0644;   /* mt pt: null from "." at end */
112         if (alen == 1)
113             alen++;             /* Empty string */
114     } else {
115         InStatus.UnixModeBits = 0755;
116         alen++;                 /* add in the null */
117     }
118     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
119     volp = afs_FindVolume(&adp->fid, READ_LOCK);        /*parent is also in same vol */
120     ObtainWriteLock(&adp->lock, 156);
121     if (tdc)
122         ObtainWriteLock(&tdc->lock, 636);
123     ObtainSharedLock(&afs_xvcache, 17); /* prevent others from creating this entry */
124     /* XXX Pay attention to afs_xvcache around the whole thing!! XXX */
125     do {
126         tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
127         if (tc) {
128             hostp = tc->srvr->server;
129             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
130             if (adp->states & CForeign) {
131                 now = osi_Time();
132                 RX_AFS_GUNLOCK();
133                 code =
134                     RXAFS_DFSSymlink(tc->id, (struct AFSFid *)&adp->fid.Fid,
135                                      aname, atargetName, &InStatus,
136                                      (struct AFSFid *)&newFid.Fid,
137                                      &OutFidStatus, &OutDirStatus, &CallBack,
138                                      &tsync);
139                 RX_AFS_GLOCK();
140             } else {
141                 RX_AFS_GUNLOCK();
142                 code =
143                     RXAFS_Symlink(tc->id, (struct AFSFid *)&adp->fid.Fid,
144                                   aname, atargetName, &InStatus,
145                                   (struct AFSFid *)&newFid.Fid, &OutFidStatus,
146                                   &OutDirStatus, &tsync);
147                 RX_AFS_GLOCK();
148             }
149             XSTATS_END_TIME;
150         } else
151             code = -1;
152     } while (afs_Analyze
153              (tc, code, &adp->fid, &treq, AFS_STATS_FS_RPCIDX_SYMLINK,
154               SHARED_LOCK, NULL));
155
156     UpgradeSToWLock(&afs_xvcache, 40);
157     if (code) {
158         if (code < 0) {
159             ObtainWriteLock(&afs_xcbhash, 499);
160             afs_DequeueCallback(adp);
161             adp->states &= ~CStatd;
162             ReleaseWriteLock(&afs_xcbhash);
163             osi_dnlc_purgedp(adp);
164         }
165         ReleaseWriteLock(&adp->lock);
166         ReleaseWriteLock(&afs_xvcache);
167         if (tdc) {
168             ReleaseWriteLock(&tdc->lock);
169             afs_PutDCache(tdc);
170         }
171         goto done;
172     }
173     /* otherwise, we should see if we can make the change to the dir locally */
174     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
175         /* we can do it locally */
176         code = afs_dir_Create(tdc, aname, &newFid.Fid);
177         if (code) {
178             ZapDCE(tdc);        /* surprise error -- use invalid value */
179             DZap(tdc);
180         }
181     }
182     if (tdc) {
183         ReleaseWriteLock(&tdc->lock);
184         afs_PutDCache(tdc);
185     }
186     newFid.Cell = adp->fid.Cell;
187     newFid.Fid.Volume = adp->fid.Fid.Volume;
188     ReleaseWriteLock(&adp->lock);
189
190     /* now we're done with parent dir, create the link's entry.  Note that
191      * no one can get a pointer to the new cache entry until we release 
192      * the xvcache lock. */
193     tvc = afs_NewVCache(&newFid, hostp);
194     ObtainWriteLock(&tvc->lock, 157);
195     ObtainWriteLock(&afs_xcbhash, 500);
196     tvc->states |= CStatd;      /* have valid info */
197     tvc->states &= ~CBulkFetching;
198
199     if (adp->states & CForeign) {
200         tvc->states |= CForeign;
201         /* We don't have to worry about losing the callback since we're doing it 
202          * under the afs_xvcache lock actually, afs_NewVCache may drop the 
203          * afs_xvcache lock, if it calls afs_FlushVCache */
204         tvc->cbExpires = CallBack.ExpirationTime + now;
205         afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
206     } else {
207         tvc->cbExpires = 0x7fffffff;    /* never expires, they can't change */
208         /* since it never expires, we don't have to queue the callback */
209     }
210     ReleaseWriteLock(&afs_xcbhash);
211     afs_ProcessFS(tvc, &OutFidStatus, &treq);
212
213     if (!tvc->linkData) {
214         tvc->linkData = (char *)afs_osi_Alloc(alen);
215         strncpy(tvc->linkData, atargetName, alen - 1);
216         tvc->linkData[alen - 1] = 0;
217     }
218     ReleaseWriteLock(&tvc->lock);
219     ReleaseWriteLock(&afs_xvcache);
220     afs_PutVCache(tvc);
221     code = 0;
222   done:
223     afs_PutFakeStat(&fakestate);
224     if (volp)
225         afs_PutVolume(volp, READ_LOCK);
226     code = afs_CheckCode(code, &treq, 31);
227   done2:
228     return code;
229 }
230
231 int
232 afs_MemHandleLink(register struct vcache *avc, struct vrequest *areq)
233 {
234     register struct dcache *tdc;
235     register char *tp, *rbuf;
236     afs_size_t offset, len;
237     afs_int32 tlen, alen;
238     register afs_int32 code;
239
240     AFS_STATCNT(afs_MemHandleLink);
241     /* two different formats, one for links protected 644, have a "." at
242      * the end of the file name, which we turn into a null.  Others, 
243      * protected 755, we add a null to the end of */
244     if (!avc->linkData) {
245         void *addr;
246         tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
247         if (!tdc) {
248             return EIO;
249         }
250         /* otherwise we have the data loaded, go for it */
251         if (len > 1024) {
252             afs_PutDCache(tdc);
253             return EFAULT;
254         }
255         if (avc->m.Mode & 0111)
256             alen = len + 1;     /* regular link */
257         else
258             alen = len;         /* mt point */
259         rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
260         ObtainReadLock(&tdc->lock);
261         addr = afs_MemCacheOpen(tdc->f.inode);
262         tlen = len;
263         code = afs_MemReadBlk(addr, 0, rbuf, tlen);
264         afs_MemCacheClose(addr);
265         ReleaseReadLock(&tdc->lock);
266         afs_PutDCache(tdc);
267         rbuf[alen - 1] = 0;
268         alen = strlen(rbuf) + 1;
269         tp = afs_osi_Alloc(alen);       /* make room for terminating null */
270         memcpy(tp, rbuf, alen);
271         osi_FreeLargeSpace(rbuf);
272         if (code != len) {
273             afs_osi_Free(tp, alen);
274             return EIO;
275         }
276         avc->linkData = tp;
277     }
278     return 0;
279 }
280
281 int
282 afs_UFSHandleLink(register struct vcache *avc, struct vrequest *areq)
283 {
284     register struct dcache *tdc;
285     register char *tp, *rbuf;
286     void *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
292      * end of the file name, which we turn into a null.  Others, protected
293      * 755, 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, ICL_TYPE_OFFSET,
299                    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)
309             alen = len + 1;     /* regular link */
310         else
311             alen = len;         /* mt point */
312         rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
313         tlen = len;
314         ObtainReadLock(&tdc->lock);
315         tfile = osi_UFSOpen(tdc->f.inode);
316         code = afs_osi_Read(tfile, -1, rbuf, tlen);
317         osi_UFSClose(tfile);
318         ReleaseReadLock(&tdc->lock);
319         afs_PutDCache(tdc);
320         rbuf[alen - 1] = '\0';
321         alen = strlen(rbuf) + 1;
322         tp = afs_osi_Alloc(alen);       /* make room for terminating null */
323         memcpy(tp, rbuf, alen);
324         osi_FreeLargeSpace(rbuf);
325         if (code != tlen) {
326             afs_osi_Free(tp, alen);
327             return EIO;
328         }
329         avc->linkData = tp;
330     }
331     return 0;
332 }
333
334 int
335 afs_readlink(OSI_VC_ARG(avc), auio, acred)
336      OSI_VC_DECL(avc);
337      struct uio *auio;
338      struct AFS_UCRED *acred;
339 {
340     register afs_int32 code;
341     struct vrequest treq;
342     register char *tp;
343     struct afs_fakestat_state fakestat;
344     OSI_VC_CONVERT(avc);
345
346     AFS_STATCNT(afs_readlink);
347     afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
348     if ((code = afs_InitReq(&treq, acred)))
349         return code;
350     afs_InitFakeStat(&fakestat);
351     code = afs_EvalFakeStat(&avc, &fakestat, &treq);
352     if (code)
353         goto done;
354     code = afs_VerifyVCache(avc, &treq);
355     if (code)
356         goto done;
357     if (vType(avc) != VLNK) {
358         code = EINVAL;
359         goto done;
360     }
361     ObtainWriteLock(&avc->lock, 158);
362     code = afs_HandleLink(avc, &treq);
363     /* finally uiomove it to user-land */
364     if (code == 0) {
365         tp = avc->linkData;
366         if (tp)
367             AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
368         else {
369             code = EIO;
370         }
371     }
372     ReleaseWriteLock(&avc->lock);
373   done:
374     afs_PutFakeStat(&fakestat);
375     code = afs_CheckCode(code, &treq, 32);
376     return code;
377 }