venus: Remove dedebug
[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
20 #include "afs/sysincludes.h"    /* Standard vendor system headers */
21 #include "afsincludes.h"        /* Afs-based standard headers */
22 #include "afs/afs_stats.h"      /* statistics */
23 #include "afs/afs_cbqueue.h"
24 #include "afs/nfsclient.h"
25 #include "afs/afs_osidnlc.h"
26
27 /* Note that we don't set CDirty here, this is OK because the link
28  * RPC is called synchronously. */
29
30 int
31 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
32 afs_link(OSI_VC_DECL(adp), struct vcache *avc, char *aname, 
33          afs_ucred_t *acred)
34 #else
35 afs_link(struct vcache *avc, OSI_VC_DECL(adp), char *aname, 
36          afs_ucred_t *acred)
37 #endif
38 {
39     struct vrequest *treq = NULL;
40     struct dcache *tdc;
41     afs_int32 code;
42     struct afs_conn *tc;
43     afs_size_t offset, len;
44     struct AFSFetchStatus *OutFidStatus, *OutDirStatus;
45     struct AFSVolSync tsync;
46     struct afs_fakestat_state vfakestate, dfakestate;
47     struct rx_connection *rxconn;
48     XSTATS_DECLS;
49     OSI_VC_CONVERT(adp);
50
51     AFS_STATCNT(afs_link);
52     afs_Trace3(afs_iclSetp, CM_TRACE_LINK, ICL_TYPE_POINTER, adp,
53                ICL_TYPE_POINTER, avc, ICL_TYPE_STRING, aname);
54
55     OutFidStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
56     OutDirStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
57
58     /* create a hard link; new entry is aname in dir adp */
59     if ((code = afs_CreateReq(&treq, acred)))
60         goto done2;
61
62     afs_InitFakeStat(&vfakestate);
63     afs_InitFakeStat(&dfakestate);
64
65     AFS_DISCON_LOCK();
66
67     code = afs_EvalFakeStat(&avc, &vfakestate, treq);
68     if (code)
69         goto done;
70     code = afs_EvalFakeStat(&adp, &dfakestate, treq);
71     if (code)
72         goto done;
73
74     if (avc->f.fid.Cell != adp->f.fid.Cell
75         || avc->f.fid.Fid.Volume != adp->f.fid.Fid.Volume) {
76         code = EXDEV;
77         goto done;
78     }
79     if (strlen(aname) > AFSNAMEMAX) {
80         code = ENAMETOOLONG;
81         goto done;
82     }
83     code = afs_VerifyVCache(adp, treq);
84     if (code)
85         goto done;
86
87     /** If the volume is read-only, return error without making an RPC to the
88       * fileserver
89       */
90     if (adp->f.states & CRO) {
91         code = EROFS;
92         goto done;
93     }
94
95     if (AFS_IS_DISCONNECTED) {
96         code = ENETDOWN;
97         goto done;
98     }
99
100     tdc = afs_GetDCache(adp, (afs_size_t) 0, treq, &offset, &len, 1);   /* test for error below */
101     ObtainWriteLock(&adp->lock, 145);
102     do {
103         tc = afs_Conn(&adp->f.fid, treq, SHARED_LOCK, &rxconn);
104         if (tc) {
105             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_LINK);
106             RX_AFS_GUNLOCK();
107             code =
108                 RXAFS_Link(rxconn, (struct AFSFid *)&adp->f.fid.Fid, aname,
109                            (struct AFSFid *)&avc->f.fid.Fid, OutFidStatus,
110                            OutDirStatus, &tsync);
111             RX_AFS_GLOCK();
112             XSTATS_END_TIME;
113
114         } else
115             code = -1;
116     } while (afs_Analyze
117              (tc, rxconn, code, &adp->f.fid, treq, AFS_STATS_FS_RPCIDX_LINK,
118               SHARED_LOCK, NULL));
119
120     if (code) {
121         if (tdc)
122             afs_PutDCache(tdc);
123         if (code < 0) {
124             afs_StaleVCache(adp);
125         }
126         ReleaseWriteLock(&adp->lock);
127         goto done;
128     }
129     if (tdc)
130         ObtainWriteLock(&tdc->lock, 635);
131     if (afs_LocalHero(adp, tdc, OutDirStatus, 1)) {
132         /* we can do it locally */
133         ObtainWriteLock(&afs_xdcache, 290);
134         code = afs_dir_Create(tdc, aname, &avc->f.fid.Fid);
135         ReleaseWriteLock(&afs_xdcache);
136         if (code) {
137             ZapDCE(tdc);        /* surprise error -- invalid value */
138             DZap(tdc);
139         }
140     }
141     if (tdc) {
142         ReleaseWriteLock(&tdc->lock);
143         afs_PutDCache(tdc);     /* drop ref count */
144     }
145     ReleaseWriteLock(&adp->lock);
146     ObtainWriteLock(&avc->lock, 146);   /* correct link count */
147
148     /* we could lock both dir and file; since we get the new fid
149      * status back, you'd think we could put it in the cache status
150      * entry at that point.  Note that if we don't lock the file over
151      * the rpc call, we have no guarantee that the status info
152      * returned in ustat is the most recent to store in the file's
153      * cache entry */
154
155     afs_StaleVCache(avc); /* don't really know new link count */
156     ReleaseWriteLock(&avc->lock);
157     code = 0;
158   done:
159     code = afs_CheckCode(code, treq, 24);
160     afs_DestroyReq(treq);
161     afs_PutFakeStat(&vfakestate);
162     afs_PutFakeStat(&dfakestate);
163     AFS_DISCON_UNLOCK();
164   done2:
165     osi_FreeSmallSpace(OutFidStatus);
166     osi_FreeSmallSpace(OutDirStatus);
167     return code;
168 }