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