2c652227ea91943eb52b25167dd3f6a4c30612a1
[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     register struct vcache *avc;
36     struct nameidata *ndp; {
37     register struct vcache *adp = (struct vcache *)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     register 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_int32 offset, len;
57     struct AFSFetchStatus OutFidStatus, OutDirStatus;
58     struct AFSVolSync tsync;
59     XSTATS_DECLS
60     OSI_VC_CONVERT(adp)
61
62     AFS_STATCNT(afs_link);
63     afs_Trace3(afs_iclSetp, CM_TRACE_LINK, ICL_TYPE_POINTER, adp,
64                ICL_TYPE_POINTER, avc, ICL_TYPE_STRING, aname);
65     /* create a hard link; new entry is aname in dir adp */
66     if (code = afs_InitReq(&treq, acred)) return code;
67     if (avc->fid.Cell != adp->fid.Cell || avc->fid.Fid.Volume != adp->fid.Fid.Volume) {
68         code = EXDEV;
69         goto done;
70     }
71     code = afs_VerifyVCache(adp, &treq);
72     if (code) goto done;
73
74     /** If the volume is read-only, return error without making an RPC to the
75       * fileserver
76       */
77     if ( adp->states & CRO ) {
78         code = EROFS;
79         goto done;
80     }
81
82     tdc = afs_GetDCache(adp, 0, &treq, &offset, &len, 1);  /* test for error below */
83     ObtainWriteLock(&adp->lock,145);
84     do {
85         tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
86         if (tc) {
87           XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_LINK);
88 #ifdef RX_ENABLE_LOCKS
89           AFS_GUNLOCK();
90 #endif /* RX_ENABLE_LOCKS */
91             code = RXAFS_Link(tc->id, (struct AFSFid *) &adp->fid.Fid, aname,
92                               (struct AFSFid *) &avc->fid.Fid, &OutFidStatus,
93                               &OutDirStatus, &tsync);
94 #ifdef RX_ENABLE_LOCKS
95           AFS_GLOCK();
96 #endif /* RX_ENABLE_LOCKS */
97           XSTATS_END_TIME;
98
99         }
100         else code = -1;
101     } while
102       (afs_Analyze(tc, code, &adp->fid, &treq,
103                    AFS_STATS_FS_RPCIDX_LINK, SHARED_LOCK, (struct cell *)0));
104
105     if (code) {
106         if (tdc) afs_PutDCache(tdc);
107         if (code < 0) {
108           ObtainWriteLock(&afs_xcbhash, 492);
109           afs_DequeueCallback(adp);
110           adp->states &= ~CStatd;
111           ReleaseWriteLock(&afs_xcbhash);
112           osi_dnlc_purgedp(adp);
113         }
114         ReleaseWriteLock(&adp->lock);
115         goto done;
116     }
117     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
118         /* we can do it locally */
119         code = afs_dir_Create(&tdc->f.inode, aname, &avc->fid.Fid);
120         if (code) {
121             ZapDCE(tdc);        /* surprise error -- invalid value */
122             DZap(&tdc->f.inode);
123         }
124     }
125     if (tdc) {
126         afs_PutDCache(tdc);     /* drop ref count */
127     }
128     ReleaseWriteLock(&adp->lock);
129     ObtainWriteLock(&avc->lock,146);    /* correct link count */
130
131     /* we could lock both dir and file; since we get the new fid
132      * status back, you'd think we could put it in the cache status
133      * entry at that point.  Note that if we don't lock the file over
134      * the rpc call, we have no guarantee that the status info
135      * returned in ustat is the most recent to store in the file's
136      * cache entry */
137
138     ObtainWriteLock(&afs_xcbhash, 493);
139     afs_DequeueCallback(avc);
140     avc->states &= ~CStatd;     /* don't really know new link count */
141     ReleaseWriteLock(&afs_xcbhash);
142     if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
143         osi_dnlc_purgedp(avc);
144     ReleaseWriteLock(&avc->lock);
145     code = 0;
146 done:
147 #ifdef  AFS_OSF_ENV
148     afs_PutVCache(adp, WRITE_LOCK);
149 #endif  /* AFS_OSF_ENV */
150     return afs_CheckCode(code, &treq);
151 }
152