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