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