unix cm rx-oblivious connection pooling
[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
25 #include "afs/sysincludes.h"    /* Standard vendor system headers */
26 #include "afsincludes.h"        /* Afs-based standard headers */
27 #include "afs/afs_stats.h"      /* statistics */
28 #include "afs/afs_cbqueue.h"
29 #include "afs/nfsclient.h"
30 #include "afs/afs_osidnlc.h"
31
32 extern afs_rwlock_t afs_xvcache;
33 extern afs_rwlock_t afs_xcbhash;
34
35 /* Note: There is the bare bones beginning of symlink hints in the now
36  * defunct afs/afs_lookup.c file. Since they are not in use, making the call
37  * is just a performance hit.
38  */
39
40 static int
41 afs_DisconCreateSymlink(struct vcache *avc, char *aname, 
42                         struct vrequest *areq) {
43     struct dcache *tdc;
44     struct osi_file *tfile;
45     afs_size_t offset, len;
46
47     tdc = afs_GetDCache(avc, 0, areq, &offset, &len, 0);
48     if (!tdc) {
49         /* printf("afs_DisconCreateSymlink: can't get new dcache for symlink.\n"); */
50         return ENOENT;
51     }
52
53     len = strlen(aname);
54     avc->f.m.Length = len;
55
56     ObtainWriteLock(&tdc->lock, 720);
57     afs_AdjustSize(tdc, len);
58     tdc->validPos = len;
59     tfile = afs_CFileOpen(&tdc->f.inode);
60     afs_CFileWrite(tfile, 0, aname, len);
61     afs_CFileClose(tfile);
62     ReleaseWriteLock(&tdc->lock);
63     return 0;
64 }
65
66 /* don't set CDirty in here because RPC is called synchronously */
67 int 
68 afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs, 
69             char *atargetName, afs_ucred_t *acred)
70 {
71     afs_uint32 now = 0;
72     struct vrequest treq;
73     afs_int32 code = 0;
74     struct afs_conn *tc;
75     struct VenusFid newFid;
76     struct dcache *tdc;
77     afs_size_t offset, len;
78     afs_int32 alen;
79     struct server *hostp = 0;
80     struct vcache *tvc;
81     struct AFSStoreStatus InStatus;
82     struct AFSFetchStatus OutFidStatus, OutDirStatus;
83     struct AFSCallBack CallBack;
84     struct AFSVolSync tsync;
85     struct volume *volp = 0;
86     struct afs_fakestat_state fakestate;
87     XSTATS_DECLS;
88     OSI_VC_CONVERT(adp);
89
90     AFS_STATCNT(afs_symlink);
91     afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
92                ICL_TYPE_STRING, aname);
93
94     if ((code = afs_InitReq(&treq, acred)))
95         goto done2;
96
97     afs_InitFakeStat(&fakestate);
98
99     AFS_DISCON_LOCK();
100     
101     code = afs_EvalFakeStat(&adp, &fakestate, &treq);
102     if (code)
103         goto done;
104
105     if (strlen(aname) > AFSNAMEMAX || strlen(atargetName) > AFSPATHMAX) {
106         code = ENAMETOOLONG;
107         goto done;
108     }
109
110     if (afs_IsDynroot(adp)) {
111         code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
112         goto done;
113     }
114     if (afs_IsDynrootMount(adp)) {
115         code = EROFS;
116         goto done;
117     }
118
119     code = afs_VerifyVCache(adp, &treq);
120     if (code) {
121         code = afs_CheckCode(code, &treq, 30);
122         goto done;
123     }
124
125     /** If the volume is read-only, return error without making an RPC to the
126       * fileserver
127       */
128     if (adp->f.states & CRO) {
129         code = EROFS;
130         goto done;
131     }
132
133     if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
134         code = ENETDOWN;
135         goto done;
136     }
137     
138     InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE;
139     InStatus.ClientModTime = osi_Time();
140     alen = strlen(atargetName); /* we want it to include the null */
141     if ( (*atargetName == '#' || *atargetName == '%') && alen > 1 && atargetName[alen-1] == '.') {
142         InStatus.UnixModeBits = 0644;   /* mt pt: null from "." at end */
143         if (alen == 1)
144             alen++;             /* Empty string */
145     } else {
146         InStatus.UnixModeBits = 0755;
147         alen++;                 /* add in the null */
148     }
149     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
150     volp = afs_FindVolume(&adp->f.fid, READ_LOCK);      /*parent is also in same vol */
151     ObtainWriteLock(&adp->lock, 156);
152     if (tdc)
153         ObtainWriteLock(&tdc->lock, 636);
154     /* No further locks: if the SymLink succeeds, it does not matter what happens
155      * to our local copy of the directory. If somebody tampers with it in the meantime,
156      * the copy will be invalidated */
157     if (!AFS_IS_DISCON_RW) {
158         do {
159             tc = afs_Conn(&adp->f.fid, &treq, SHARED_LOCK);
160             if (tc) {
161                 hostp = tc->parent->srvr->server;
162                 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
163                 if (adp->f.states & CForeign) {
164                     now = osi_Time();
165                     RX_AFS_GUNLOCK();
166                     code = 
167                         RXAFS_DFSSymlink(tc->id, 
168                                          (struct AFSFid *)&adp->f.fid.Fid,
169                                          aname, atargetName, &InStatus,
170                                          (struct AFSFid *)&newFid.Fid,
171                                          &OutFidStatus, &OutDirStatus, 
172                                          &CallBack, &tsync);
173                     RX_AFS_GLOCK();
174                 } else {
175                     RX_AFS_GUNLOCK();
176                     code =
177                         RXAFS_Symlink(tc->id, (struct AFSFid *)&adp->f.fid.Fid,
178                                       aname, atargetName, &InStatus,
179                                       (struct AFSFid *)&newFid.Fid, 
180                                       &OutFidStatus, &OutDirStatus, &tsync);
181                     RX_AFS_GLOCK();
182                 }
183                 XSTATS_END_TIME;
184             } else
185                 code = -1;
186         } while (afs_Analyze
187                     (tc, code, &adp->f.fid, &treq, AFS_STATS_FS_RPCIDX_SYMLINK,
188                      SHARED_LOCK, NULL));
189     } else {
190         newFid.Cell = adp->f.fid.Cell;
191         newFid.Fid.Volume = adp->f.fid.Fid.Volume;
192         afs_GenFakeFid(&newFid, VREG, 0);
193     }
194
195     ObtainWriteLock(&afs_xvcache, 40);
196     if (code) {
197         if (code < 0) {
198             ObtainWriteLock(&afs_xcbhash, 499);
199             afs_DequeueCallback(adp);
200             adp->f.states &= ~CStatd;
201             ReleaseWriteLock(&afs_xcbhash);
202             osi_dnlc_purgedp(adp);
203         }
204         ReleaseWriteLock(&adp->lock);
205         ReleaseWriteLock(&afs_xvcache);
206         if (tdc) {
207             ReleaseWriteLock(&tdc->lock);
208             afs_PutDCache(tdc);
209         }
210         goto done;
211     }
212     /* otherwise, we should see if we can make the change to the dir locally */
213     if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
214         /* we can do it locally */
215         ObtainWriteLock(&afs_xdcache, 293);
216         /* If the following fails because the name has been created in the meantime, the
217          * directory is out-of-date - the file server knows best! */
218         code = afs_dir_Create(tdc, aname, &newFid.Fid);
219         ReleaseWriteLock(&afs_xdcache);
220         if (code && !AFS_IS_DISCON_RW) {
221             ZapDCE(tdc);        /* surprise error -- use invalid value */
222             DZap(tdc);
223         }
224     }
225     if (tdc) {
226         ReleaseWriteLock(&tdc->lock);
227         afs_PutDCache(tdc);
228     }
229     newFid.Cell = adp->f.fid.Cell;
230     newFid.Fid.Volume = adp->f.fid.Fid.Volume;
231     ReleaseWriteLock(&adp->lock);
232
233     /* now we're done with parent dir, create the link's entry.  Note that
234      * no one can get a pointer to the new cache entry until we release 
235      * the xvcache lock. */
236     tvc = afs_NewVCache(&newFid, hostp);
237     if (!tvc)
238     {
239         code = -2;
240         ReleaseWriteLock(&afs_xvcache);
241         goto done;
242     }
243     ObtainWriteLock(&tvc->lock, 157);
244     ObtainWriteLock(&afs_xcbhash, 500);
245     tvc->f.states |= CStatd;    /* have valid info */
246     tvc->f.states &= ~CBulkFetching;
247
248     if (adp->f.states & CForeign) {
249         tvc->f.states |= CForeign;
250         /* We don't have to worry about losing the callback since we're doing it 
251          * under the afs_xvcache lock actually, afs_NewVCache may drop the 
252          * afs_xvcache lock, if it calls afs_FlushVCache */
253         tvc->cbExpires = CallBack.ExpirationTime + now;
254         afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
255     } else {
256         tvc->cbExpires = 0x7fffffff;    /* never expires, they can't change */
257         /* since it never expires, we don't have to queue the callback */
258     }
259     ReleaseWriteLock(&afs_xcbhash);
260
261     if (AFS_IS_DISCON_RW) {
262         attrs->va_mode = InStatus.UnixModeBits;
263         afs_GenDisconStatus(adp, tvc, &newFid, attrs, &treq, VLNK);
264         code = afs_DisconCreateSymlink(tvc, atargetName, &treq);
265         if (code) {
266             /* XXX - When this goes wrong, we need to tidy up the changes we made to
267              * the parent, and get rid of the vcache we just created */
268             ReleaseWriteLock(&tvc->lock);
269             ReleaseWriteLock(&afs_xvcache);
270             afs_PutVCache(tvc);
271             goto done;
272         }
273         afs_DisconAddDirty(tvc, VDisconCreate, 0);
274     } else {
275         afs_ProcessFS(tvc, &OutFidStatus, &treq);
276     }
277
278     if (!tvc->linkData) {
279         tvc->linkData = afs_osi_Alloc(alen);
280         osi_Assert(tvc->linkData != NULL);
281         strncpy(tvc->linkData, atargetName, alen - 1);
282         tvc->linkData[alen - 1] = 0;
283     }
284     ReleaseWriteLock(&tvc->lock);
285     ReleaseWriteLock(&afs_xvcache);
286     afs_PutVCache(tvc);
287     code = 0;
288   done:
289     afs_PutFakeStat(&fakestate);
290     if (volp)
291         afs_PutVolume(volp, READ_LOCK);
292     AFS_DISCON_UNLOCK();
293     code = afs_CheckCode(code, &treq, 31);
294   done2:
295     return code;
296 }
297
298 int
299 afs_MemHandleLink(struct vcache *avc, struct vrequest *areq)
300 {
301     struct dcache *tdc;
302     char *tp, *rbuf;
303     afs_size_t offset, len;
304     afs_int32 tlen, alen;
305     afs_int32 code;
306
307     AFS_STATCNT(afs_MemHandleLink);
308     /* two different formats, one for links protected 644, have a "." at
309      * the end of the file name, which we turn into a null.  Others, 
310      * protected 755, we add a null to the end of */
311     if (!avc->linkData) {
312         void *addr;
313         tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
314         if (!tdc) {
315             return EIO;
316         }
317         /* otherwise we have the data loaded, go for it */
318         if (len > 1024) {
319             afs_PutDCache(tdc);
320             return EFAULT;
321         }
322         if (avc->f.m.Mode & 0111)
323             alen = len + 1;     /* regular link */
324         else
325             alen = len;         /* mt point */
326         rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
327         ObtainReadLock(&tdc->lock);
328         addr = afs_MemCacheOpen(&tdc->f.inode);
329         tlen = len;
330         code = afs_MemReadBlk(addr, 0, rbuf, tlen);
331         afs_MemCacheClose(addr);
332         ReleaseReadLock(&tdc->lock);
333         afs_PutDCache(tdc);
334         rbuf[alen - 1] = 0;
335         alen = strlen(rbuf) + 1;
336         tp = afs_osi_Alloc(alen);       /* make room for terminating null */
337         osi_Assert(tp != NULL);
338         memcpy(tp, rbuf, alen);
339         osi_FreeLargeSpace(rbuf);
340         if (code != len) {
341             afs_osi_Free(tp, alen);
342             return EIO;
343         }
344         avc->linkData = tp;
345     }
346     return 0;
347 }
348
349 int
350 afs_UFSHandleLink(struct vcache *avc, struct vrequest *areq)
351 {
352     struct dcache *tdc;
353     char *tp, *rbuf;
354     void *tfile;
355     afs_size_t offset, len;
356     afs_int32 tlen, alen;
357     afs_int32 code;
358
359     /* two different formats, one for links protected 644, have a "." at the
360      * end of the file name, which we turn into a null.  Others, protected
361      * 755, we add a null to the end of */
362     AFS_STATCNT(afs_UFSHandleLink);
363     if (!avc->linkData) {
364         tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
365         afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
366                    ICL_TYPE_POINTER, tdc, ICL_TYPE_OFFSET,
367                    ICL_HANDLE_OFFSET(avc->f.m.Length));
368         if (!tdc) {
369             if (AFS_IS_DISCONNECTED)
370                 return ENETDOWN;
371             else
372                 return EIO;
373         }
374         /* otherwise we have the data loaded, go for it */
375         if (len > 1024) {
376             afs_PutDCache(tdc);
377             return EFAULT;
378         }
379         if (avc->f.m.Mode & 0111)
380             alen = len + 1;     /* regular link */
381         else
382             alen = len;         /* mt point */
383         rbuf = (char *)osi_AllocLargeSpace(AFS_LRALLOCSIZ);
384         tlen = len;
385         ObtainReadLock(&tdc->lock);
386         tfile = osi_UFSOpen(&tdc->f.inode);
387         code = afs_osi_Read(tfile, -1, rbuf, tlen);
388         osi_UFSClose(tfile);
389         ReleaseReadLock(&tdc->lock);
390         afs_PutDCache(tdc);
391         rbuf[alen - 1] = '\0';
392         alen = strlen(rbuf) + 1;
393         tp = afs_osi_Alloc(alen);       /* make room for terminating null */
394         osi_Assert(tp != NULL);
395         memcpy(tp, rbuf, alen);
396         osi_FreeLargeSpace(rbuf);
397         if (code != tlen) {
398             afs_osi_Free(tp, alen);
399             return EIO;
400         }
401         avc->linkData = tp;
402     }
403     return 0;
404 }
405
406 int
407 afs_readlink(OSI_VC_DECL(avc), struct uio *auio, afs_ucred_t *acred)
408 {
409     afs_int32 code;
410     struct vrequest treq;
411     char *tp;
412     struct afs_fakestat_state fakestat;
413     OSI_VC_CONVERT(avc);
414
415     AFS_STATCNT(afs_readlink);
416     afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
417     if ((code = afs_InitReq(&treq, acred)))
418         return code;
419     afs_InitFakeStat(&fakestat);
420
421     AFS_DISCON_LOCK();
422     
423     code = afs_EvalFakeStat(&avc, &fakestat, &treq);
424     if (code)
425         goto done;
426     code = afs_VerifyVCache(avc, &treq);
427     if (code)
428         goto done;
429     if (vType(avc) != VLNK) {
430         code = EINVAL;
431         goto done;
432     }
433     ObtainWriteLock(&avc->lock, 158);
434     code = afs_HandleLink(avc, &treq);
435     /* finally uiomove it to user-land */
436     if (code == 0) {
437         tp = avc->linkData;
438         if (tp)
439             AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
440         else {
441             code = EIO;
442         }
443     }
444     ReleaseWriteLock(&avc->lock);
445   done:
446     afs_PutFakeStat(&fakestat);
447     AFS_DISCON_UNLOCK();
448     code = afs_CheckCode(code, &treq, 32);
449     return code;
450 }