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