Standardize License information
[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 "../afs/param.h"       /* Should be always first */
17 #include "../afs/sysincludes.h" /* Standard vendor system headers */
18 #include "../afs/afsincludes.h" /* Afs-based standard headers */
19 #include "../afs/afs_stats.h" /* statistics */
20 #include "../afs/afs_cbqueue.h"
21 #include "../afs/nfsclient.h"
22 #include "../afs/afs_osidnlc.h"
23
24 extern afs_rwlock_t afs_xcbhash;
25
26 /* Note that we don't set CDirty here, this is OK because the link
27  * RPC is called synchronously. */
28
29 #ifdef  AFS_OSF_ENV
30 afs_link(avc, ndp)
31     register struct vcache *avc;
32     struct nameidata *ndp; {
33     register struct vcache *adp = (struct vcache *)ndp->ni_dvp;
34     char *aname = ndp->ni_dent.d_name;
35     struct ucred *acred = ndp->ni_cred;
36 #else   /* AFS_OSF_ENV */
37 #if     defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
38 afs_link(OSI_VC_ARG(adp), avc, aname, acred)
39 #else
40 afs_link(avc, OSI_VC_ARG(adp), aname, acred)
41 #endif
42     OSI_VC_DECL(adp);
43     register struct vcache *avc;
44     char *aname;
45     struct AFS_UCRED *acred;
46 {
47 #endif
48     struct vrequest treq;
49     register struct dcache *tdc;
50     register afs_int32 code;
51     register struct conn *tc;
52     afs_int32 offset, len;
53     struct AFSFetchStatus OutFidStatus, OutDirStatus;
54     struct AFSVolSync tsync;
55     XSTATS_DECLS
56     OSI_VC_CONVERT(adp)
57
58     AFS_STATCNT(afs_link);
59     afs_Trace3(afs_iclSetp, CM_TRACE_LINK, ICL_TYPE_POINTER, adp,
60                ICL_TYPE_POINTER, avc, ICL_TYPE_STRING, aname);
61     /* create a hard link; new entry is aname in dir adp */
62     if (code = afs_InitReq(&treq, acred)) return code;
63     if (avc->fid.Cell != adp->fid.Cell || avc->fid.Fid.Volume != adp->fid.Fid.Volume) {
64         code = EXDEV;
65         goto done;
66     }
67     code = afs_VerifyVCache(adp, &treq);
68     if (code) goto done;
69
70     /** If the volume is read-only, return error without making an RPC to the
71       * fileserver
72       */
73     if ( adp->states & CRO ) {
74         code = EROFS;
75         goto done;
76     }
77
78     tdc = afs_GetDCache(adp, 0, &treq, &offset, &len, 1);  /* test for error below */
79     ObtainWriteLock(&adp->lock,145);
80     do {
81         tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
82         if (tc) {
83           XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_LINK);
84 #ifdef RX_ENABLE_LOCKS
85           AFS_GUNLOCK();
86 #endif /* RX_ENABLE_LOCKS */
87             code = RXAFS_Link(tc->id, (struct AFSFid *) &adp->fid.Fid, aname,
88                               (struct AFSFid *) &avc->fid.Fid, &OutFidStatus,
89                               &OutDirStatus, &tsync);
90 #ifdef RX_ENABLE_LOCKS
91           AFS_GLOCK();
92 #endif /* RX_ENABLE_LOCKS */
93           XSTATS_END_TIME;
94
95         }
96         else code = -1;
97     } while
98       (afs_Analyze(tc, code, &adp->fid, &treq,
99                    AFS_STATS_FS_RPCIDX_LINK, SHARED_LOCK, (struct cell *)0));
100
101     if (code) {
102         if (tdc) afs_PutDCache(tdc);
103         if (code < 0) {
104           ObtainWriteLock(&afs_xcbhash, 492);
105           afs_DequeueCallback(adp);
106           adp->states &= ~CStatd;
107           ReleaseWriteLock(&afs_xcbhash);
108           osi_dnlc_purgedp(adp);
109         }
110         ReleaseWriteLock(&adp->lock);
111         goto done;
112     }
113     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
114         /* we can do it locally */
115         code = afs_dir_Create(&tdc->f.inode, aname, &avc->fid.Fid);
116         if (code) {
117             ZapDCE(tdc);        /* surprise error -- invalid value */
118             DZap(&tdc->f.inode);
119         }
120     }
121     if (tdc) {
122         afs_PutDCache(tdc);     /* drop ref count */
123     }
124     ReleaseWriteLock(&adp->lock);
125     ObtainWriteLock(&avc->lock,146);    /* correct link count */
126
127     /* we could lock both dir and file; since we get the new fid
128      * status back, you'd think we could put it in the cache status
129      * entry at that point.  Note that if we don't lock the file over
130      * the rpc call, we have no guarantee that the status info
131      * returned in ustat is the most recent to store in the file's
132      * cache entry */
133
134     ObtainWriteLock(&afs_xcbhash, 493);
135     afs_DequeueCallback(avc);
136     avc->states &= ~CStatd;     /* don't really know new link count */
137     ReleaseWriteLock(&afs_xcbhash);
138     if (avc->fid.Fid.Vnode & 1 || (vType(avc) == VDIR))
139         osi_dnlc_purgedp(avc);
140     ReleaseWriteLock(&avc->lock);
141     code = 0;
142 done:
143 #ifdef  AFS_OSF_ENV
144     afs_PutVCache(adp, WRITE_LOCK);
145 #endif  /* AFS_OSF_ENV */
146     return afs_CheckCode(code, &treq);
147 }
148