pull-prototypes-to-head-20020821
[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("$Header$");
20
21 #include "../afs/sysincludes.h" /* Standard vendor system headers */
22 #include "../afs/afsincludes.h" /* Afs-based standard headers */
23 #include "../afs/afs_stats.h" /* statistics */
24 #include "../afs/afs_cbqueue.h"
25 #include "../afs/nfsclient.h"
26 #include "../afs/afs_osidnlc.h"
27
28 extern afs_rwlock_t afs_xcbhash;
29
30 /* Note that we don't set CDirty here, this is OK because the link
31  * RPC is called synchronously. */
32
33 #ifdef  AFS_OSF_ENV
34 afs_link(avc, ndp)
35     struct vcache *avc;
36     struct nameidata *ndp; {
37     struct vcache *adp = VTOAFS(ndp->ni_dvp);
38     char *aname = ndp->ni_dent.d_name;
39     struct ucred *acred = ndp->ni_cred;
40 #else   /* AFS_OSF_ENV */
41 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
42 afs_link(OSI_VC_ARG(adp), avc, aname, acred)
43 #else
44 afs_link(avc, OSI_VC_ARG(adp), aname, acred)
45 #endif
46     OSI_VC_DECL(adp);
47     struct vcache *avc;
48     char *aname;
49     struct AFS_UCRED *acred;
50 {
51 #endif
52     struct vrequest treq;
53     register struct dcache *tdc;
54     register afs_int32 code;
55     register struct conn *tc;
56     afs_size_t offset, len;
57     struct AFSFetchStatus OutFidStatus, OutDirStatus;
58     struct AFSVolSync tsync;
59     struct afs_fakestat_state vfakestate, dfakestate;
60     XSTATS_DECLS
61     OSI_VC_CONVERT(adp)
62
63     AFS_STATCNT(afs_link);
64     afs_Trace3(afs_iclSetp, CM_TRACE_LINK, ICL_TYPE_POINTER, adp,
65                ICL_TYPE_POINTER, avc, ICL_TYPE_STRING, aname);
66     /* create a hard link; new entry is aname in dir adp */
67     if ((code = afs_InitReq(&treq, acred))) 
68         goto done2;
69
70     afs_InitFakeStat(&vfakestate);
71     afs_InitFakeStat(&dfakestate);
72     code = afs_EvalFakeStat(&avc, &vfakestate, &treq);
73     if (code) goto done;
74     code = afs_EvalFakeStat(&adp, &dfakestate, &treq);
75     if (code) goto done;
76
77     if (avc->fid.Cell != adp->fid.Cell || avc->fid.Fid.Volume != adp->fid.Fid.Volume) {
78         code = EXDEV;
79         goto done;
80     }
81     if (strlen(aname) > AFSNAMEMAX) {
82         code = ENAMETOOLONG;
83         goto done;
84     }
85     code = afs_VerifyVCache(adp, &treq);
86     if (code) goto done;
87
88     /** If the volume is read-only, return error without making an RPC to the
89       * fileserver
90       */
91     if ( adp->states & CRO ) {
92         code = EROFS;
93         goto done;
94     }
95
96     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);  /* test for error below */
97     ObtainWriteLock(&adp->lock,145);
98     do {
99         tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
100         if (tc) {
101           XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_LINK);
102           RX_AFS_GUNLOCK();
103             code = RXAFS_Link(tc->id, (struct AFSFid *) &adp->fid.Fid, aname,
104                               (struct AFSFid *) &avc->fid.Fid, &OutFidStatus,
105                               &OutDirStatus, &tsync);
106           RX_AFS_GLOCK();
107           XSTATS_END_TIME;
108
109         }
110         else code = -1;
111     } while
112       (afs_Analyze(tc, code, &adp->fid, &treq,
113                    AFS_STATS_FS_RPCIDX_LINK, SHARED_LOCK, NULL));
114
115     if (code) {
116         if (tdc) afs_PutDCache(tdc);
117         if (code < 0) {
118           ObtainWriteLock(&afs_xcbhash, 492);
119           afs_DequeueCallback(adp);
120           adp->states &= ~CStatd;
121           ReleaseWriteLock(&afs_xcbhash);
122           osi_dnlc_purgedp(adp);
123         }
124         ReleaseWriteLock(&adp->lock);
125         goto done;
126     }
127     if (tdc) ObtainWriteLock(&tdc->lock, 635);
128     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
129         /* we can do it locally */
130         code = afs_dir_Create(&tdc->f.inode, aname, &avc->fid.Fid);
131         if (code) {
132             ZapDCE(tdc);        /* surprise error -- invalid value */
133             DZap(&tdc->f.inode);
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 #ifdef  AFS_OSF_ENV
164     afs_PutVCache(adp);
165 #endif  /* AFS_OSF_ENV */
166     return code;
167 }
168