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