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