dux-vnops-cleanup-20041224
[openafs.git] / src / afs / VNOPS / afs_vnop_link.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  * Implements:
12  * afs_link
13  *
14  */
15
16 #include <afsconfig.h>
17 #include "afs/param.h"
18
19 RCSID
20     ("$Header$");
21
22 #include "afs/sysincludes.h"    /* Standard vendor system headers */
23 #include "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_xcbhash;
30
31 /* Note that we don't set CDirty here, this is OK because the link
32  * RPC is called synchronously. */
33
34 int
35 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
36 afs_link(OSI_VC_ARG(adp), avc, aname, acred)
37 #else
38 afs_link(avc, OSI_VC_ARG(adp), aname, acred)
39 #endif
40      OSI_VC_DECL(adp);
41      struct vcache *avc;
42      char *aname;
43      struct AFS_UCRED *acred;
44 {
45     struct vrequest treq;
46     register struct dcache *tdc;
47     register afs_int32 code;
48     register struct conn *tc;
49     afs_size_t offset, len;
50     struct AFSFetchStatus OutFidStatus, OutDirStatus;
51     struct AFSVolSync tsync;
52     struct afs_fakestat_state vfakestate, dfakestate;
53     XSTATS_DECLS;
54     OSI_VC_CONVERT(adp);
55
56     AFS_STATCNT(afs_link);
57     afs_Trace3(afs_iclSetp, CM_TRACE_LINK, ICL_TYPE_POINTER, adp,
58                ICL_TYPE_POINTER, avc, ICL_TYPE_STRING, aname);
59     /* create a hard link; new entry is aname in dir adp */
60     if ((code = afs_InitReq(&treq, acred)))
61         goto done2;
62
63     afs_InitFakeStat(&vfakestate);
64     afs_InitFakeStat(&dfakestate);
65     code = afs_EvalFakeStat(&avc, &vfakestate, &treq);
66     if (code)
67         goto done;
68     code = afs_EvalFakeStat(&adp, &dfakestate, &treq);
69     if (code)
70         goto done;
71
72     if (avc->fid.Cell != adp->fid.Cell
73         || avc->fid.Fid.Volume != adp->fid.Fid.Volume) {
74         code = EXDEV;
75         goto done;
76     }
77     if (strlen(aname) > AFSNAMEMAX) {
78         code = ENAMETOOLONG;
79         goto done;
80     }
81     code = afs_VerifyVCache(adp, &treq);
82     if (code)
83         goto done;
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         goto done;
91     }
92
93     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);  /* test for error below */
94     ObtainWriteLock(&adp->lock, 145);
95     do {
96         tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
97         if (tc) {
98             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_LINK);
99             RX_AFS_GUNLOCK();
100             code =
101                 RXAFS_Link(tc->id, (struct AFSFid *)&adp->fid.Fid, aname,
102                            (struct AFSFid *)&avc->fid.Fid, &OutFidStatus,
103                            &OutDirStatus, &tsync);
104             RX_AFS_GLOCK();
105             XSTATS_END_TIME;
106
107         } else
108             code = -1;
109     } while (afs_Analyze
110              (tc, code, &adp->fid, &treq, AFS_STATS_FS_RPCIDX_LINK,
111               SHARED_LOCK, NULL));
112
113     if (code) {
114         if (tdc)
115             afs_PutDCache(tdc);
116         if (code < 0) {
117             ObtainWriteLock(&afs_xcbhash, 492);
118             afs_DequeueCallback(adp);
119             adp->states &= ~CStatd;
120             ReleaseWriteLock(&afs_xcbhash);
121             osi_dnlc_purgedp(adp);
122         }
123         ReleaseWriteLock(&adp->lock);
124         goto done;
125     }
126     if (tdc)
127         ObtainWriteLock(&tdc->lock, 635);
128     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
129         /* we can do it locally */
130         code = afs_dir_Create(tdc, aname, &avc->fid.Fid);
131         if (code) {
132             ZapDCE(tdc);        /* surprise error -- invalid value */
133             DZap(tdc);
134         }
135     }
136     if (tdc) {
137         ReleaseWriteLock(&tdc->lock);
138         afs_PutDCache(tdc);     /* drop ref count */
139     }
140     ReleaseWriteLock(&adp->lock);
141     ObtainWriteLock(&avc->lock, 146);   /* correct link count */
142
143     /* we could lock both dir and file; since we get the new fid
144      * status back, you'd think we could put it in the cache status
145      * entry at that point.  Note that if we don't lock the file over
146      * the rpc call, we have no guarantee that the status info
147      * returned in ustat is the most recent to store in the file's
148      * cache entry */
149
150     ObtainWriteLock(&afs_xcbhash, 493);
151     afs_DequeueCallback(avc);
152     avc->states &= ~CStatd;     /* don't really know new link count */
153     ReleaseWriteLock(&afs_xcbhash);
154     if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
155         osi_dnlc_purgedp(avc);
156     ReleaseWriteLock(&avc->lock);
157     code = 0;
158   done:
159     code = afs_CheckCode(code, &treq, 24);
160     afs_PutFakeStat(&vfakestate);
161     afs_PutFakeStat(&dfakestate);
162   done2:
163     return code;
164 }