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