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