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