Merge 1.8.0~pre4 packaging into master
[pkg-k5-afs_openafs.git] / src / afs / VNOPS / afs_vnop_symlink.c
blob1dd461125727948cc8ed35e7d300b0a7edb1c0b4
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 */
11 * afs_vnop_symlink.c - symlink and readlink vnodeops.
13 * Implements:
14 * afs_symlink
15 * afs_MemHandleLink
16 * afs_UFSHandleLink
17 * afs_readlink
21 #include <afsconfig.h>
22 #include "afs/param.h"
25 #include "afs/sysincludes.h" /* Standard vendor system headers */
26 #include "afsincludes.h" /* Afs-based standard headers */
27 #include "afs/afs_stats.h" /* statistics */
28 #include "afs/afs_cbqueue.h"
29 #include "afs/nfsclient.h"
30 #include "afs/afs_osidnlc.h"
32 extern afs_rwlock_t afs_xvcache;
33 extern afs_rwlock_t afs_xcbhash;
35 /* Note: There is the bare bones beginning of symlink hints in the now
36 * defunct afs/afs_lookup.c file. Since they are not in use, making the call
37 * is just a performance hit.
40 static int
41 afs_DisconCreateSymlink(struct vcache *avc, char *aname,
42 struct vrequest *areq) {
43 struct dcache *tdc;
44 struct osi_file *tfile;
45 afs_size_t offset, len;
47 tdc = afs_GetDCache(avc, 0, areq, &offset, &len, 0);
48 if (!tdc) {
49 /* printf("afs_DisconCreateSymlink: can't get new dcache for symlink.\n"); */
50 return ENETDOWN;
53 len = strlen(aname);
54 avc->f.m.Length = len;
56 ObtainWriteLock(&tdc->lock, 720);
57 afs_AdjustSize(tdc, len);
58 tdc->validPos = len;
59 tfile = afs_CFileOpen(&tdc->f.inode);
60 afs_CFileWrite(tfile, 0, aname, len);
61 afs_CFileClose(tfile);
62 ReleaseWriteLock(&tdc->lock);
63 return 0;
66 /* don't set CDirty in here because RPC is called synchronously */
67 int
68 afs_symlink(OSI_VC_DECL(adp), char *aname, struct vattr *attrs,
69 char *atargetName, struct vcache **tvcp, afs_ucred_t *acred)
71 afs_uint32 now = 0;
72 struct vrequest *treq = NULL;
73 afs_int32 code = 0;
74 struct afs_conn *tc;
75 struct VenusFid newFid;
76 struct dcache *tdc;
77 afs_size_t offset, len;
78 afs_int32 alen;
79 struct server *hostp = 0;
80 struct vcache *tvc;
81 struct AFSStoreStatus InStatus;
82 struct AFSFetchStatus *OutFidStatus, *OutDirStatus;
83 struct AFSCallBack CallBack;
84 struct AFSVolSync tsync;
85 struct volume *volp = 0;
86 struct afs_fakestat_state fakestate;
87 struct rx_connection *rxconn;
88 XSTATS_DECLS;
89 OSI_VC_CONVERT(adp);
91 AFS_STATCNT(afs_symlink);
92 afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
93 ICL_TYPE_STRING, aname);
95 OutFidStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
96 OutDirStatus = osi_AllocSmallSpace(sizeof(struct AFSFetchStatus));
97 memset(&InStatus, 0, sizeof(InStatus));
99 if ((code = afs_CreateReq(&treq, acred)))
100 goto done2;
102 afs_InitFakeStat(&fakestate);
104 AFS_DISCON_LOCK();
106 code = afs_EvalFakeStat(&adp, &fakestate, treq);
107 if (code)
108 goto done;
110 if (strlen(aname) > AFSNAMEMAX || strlen(atargetName) > AFSPATHMAX) {
111 code = ENAMETOOLONG;
112 goto done;
115 if (afs_IsDynroot(adp)) {
116 code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
117 goto done;
119 if (afs_IsDynrootMount(adp)) {
120 code = EROFS;
121 goto done;
124 code = afs_VerifyVCache(adp, treq);
125 if (code) {
126 code = afs_CheckCode(code, treq, 30);
127 goto done;
130 /** If the volume is read-only, return error without making an RPC to the
131 * fileserver
133 if (adp->f.states & CRO) {
134 code = EROFS;
135 goto done;
138 if (AFS_IS_DISCONNECTED && !AFS_IS_DISCON_RW) {
139 code = ENETDOWN;
140 goto done;
143 InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE;
144 InStatus.ClientModTime = osi_Time();
145 alen = strlen(atargetName); /* we want it to include the null */
146 if ( (*atargetName == '#' || *atargetName == '%') && alen > 1 && atargetName[alen-1] == '.') {
147 InStatus.UnixModeBits = 0644; /* mt pt: null from "." at end */
148 if (alen == 1)
149 alen++; /* Empty string */
150 } else {
151 InStatus.UnixModeBits = 0755;
152 alen++; /* add in the null */
154 tdc = afs_GetDCache(adp, (afs_size_t) 0, treq, &offset, &len, 1);
155 volp = afs_FindVolume(&adp->f.fid, READ_LOCK); /*parent is also in same vol */
156 ObtainWriteLock(&adp->lock, 156);
157 if (tdc)
158 ObtainWriteLock(&tdc->lock, 636);
159 /* No further locks: if the SymLink succeeds, it does not matter what happens
160 * to our local copy of the directory. If somebody tampers with it in the meantime,
161 * the copy will be invalidated */
162 if (!AFS_IS_DISCON_RW) {
163 do {
164 tc = afs_Conn(&adp->f.fid, treq, SHARED_LOCK, &rxconn);
165 if (tc) {
166 hostp = tc->parent->srvr->server;
167 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
168 if (adp->f.states & CForeign) {
169 now = osi_Time();
170 RX_AFS_GUNLOCK();
171 code =
172 RXAFS_DFSSymlink(rxconn,
173 (struct AFSFid *)&adp->f.fid.Fid,
174 aname, atargetName, &InStatus,
175 (struct AFSFid *)&newFid.Fid,
176 OutFidStatus, OutDirStatus,
177 &CallBack, &tsync);
178 RX_AFS_GLOCK();
179 } else {
180 RX_AFS_GUNLOCK();
181 code =
182 RXAFS_Symlink(rxconn, (struct AFSFid *)&adp->f.fid.Fid,
183 aname, atargetName, &InStatus,
184 (struct AFSFid *)&newFid.Fid,
185 OutFidStatus, OutDirStatus, &tsync);
186 RX_AFS_GLOCK();
188 XSTATS_END_TIME;
189 } else
190 code = -1;
191 } while (afs_Analyze
192 (tc, rxconn, code, &adp->f.fid, treq, AFS_STATS_FS_RPCIDX_SYMLINK,
193 SHARED_LOCK, NULL));
194 } else {
195 newFid.Cell = adp->f.fid.Cell;
196 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
197 afs_GenFakeFid(&newFid, VREG, 0);
200 ObtainWriteLock(&afs_xvcache, 40);
201 if (code) {
202 if (code < 0) {
203 afs_StaleVCache(adp);
205 ReleaseWriteLock(&adp->lock);
206 ReleaseWriteLock(&afs_xvcache);
207 if (tdc) {
208 ReleaseWriteLock(&tdc->lock);
209 afs_PutDCache(tdc);
211 goto done;
213 /* otherwise, we should see if we can make the change to the dir locally */
214 if (AFS_IS_DISCON_RW || afs_LocalHero(adp, tdc, OutDirStatus, 1)) {
215 /* we can do it locally */
216 ObtainWriteLock(&afs_xdcache, 293);
217 /* If the following fails because the name has been created in the meantime, the
218 * directory is out-of-date - the file server knows best! */
219 code = afs_dir_Create(tdc, aname, &newFid.Fid);
220 ReleaseWriteLock(&afs_xdcache);
221 if (code && !AFS_IS_DISCON_RW) {
222 ZapDCE(tdc); /* surprise error -- use invalid value */
223 DZap(tdc);
226 if (tdc) {
227 ReleaseWriteLock(&tdc->lock);
228 afs_PutDCache(tdc);
230 newFid.Cell = adp->f.fid.Cell;
231 newFid.Fid.Volume = adp->f.fid.Fid.Volume;
232 ReleaseWriteLock(&adp->lock);
234 /* now we're done with parent dir, create the link's entry. Note that
235 * no one can get a pointer to the new cache entry until we release
236 * the xvcache lock. */
237 tvc = afs_NewVCache(&newFid, hostp);
238 if (!tvc)
240 code = -2;
241 ReleaseWriteLock(&afs_xvcache);
242 goto done;
244 ObtainWriteLock(&tvc->lock, 157);
245 ObtainWriteLock(&afs_xcbhash, 500);
246 tvc->f.states |= CStatd; /* have valid info */
247 tvc->f.states &= ~CBulkFetching;
249 if (adp->f.states & CForeign) {
250 tvc->f.states |= CForeign;
251 /* We don't have to worry about losing the callback since we're doing it
252 * under the afs_xvcache lock actually, afs_NewVCache may drop the
253 * afs_xvcache lock, if it calls afs_FlushVCache */
254 tvc->cbExpires = CallBack.ExpirationTime + now;
255 afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
256 } else {
257 tvc->cbExpires = 0x7fffffff; /* never expires, they can't change */
258 /* since it never expires, we don't have to queue the callback */
260 ReleaseWriteLock(&afs_xcbhash);
262 if (AFS_IS_DISCON_RW) {
263 attrs->va_mode = InStatus.UnixModeBits;
264 afs_GenDisconStatus(adp, tvc, &newFid, attrs, treq, VLNK);
265 code = afs_DisconCreateSymlink(tvc, atargetName, treq);
266 if (code) {
267 /* XXX - When this goes wrong, we need to tidy up the changes we made to
268 * the parent, and get rid of the vcache we just created */
269 ReleaseWriteLock(&tvc->lock);
270 ReleaseWriteLock(&afs_xvcache);
271 afs_PutVCache(tvc);
272 goto done;
274 afs_DisconAddDirty(tvc, VDisconCreate, 0);
275 } else {
276 afs_ProcessFS(tvc, OutFidStatus, treq);
279 if (!tvc->linkData) {
280 tvc->linkData = afs_osi_Alloc(alen);
281 osi_Assert(tvc->linkData != NULL);
282 strncpy(tvc->linkData, atargetName, alen - 1);
283 tvc->linkData[alen - 1] = 0;
285 ReleaseWriteLock(&tvc->lock);
286 ReleaseWriteLock(&afs_xvcache);
287 if (tvcp)
288 *tvcp = tvc;
289 else
290 afs_PutVCache(tvc);
291 code = 0;
292 done:
293 afs_PutFakeStat(&fakestate);
294 if (volp)
295 afs_PutVolume(volp, READ_LOCK);
296 AFS_DISCON_UNLOCK();
297 code = afs_CheckCode(code, treq, 31);
298 afs_DestroyReq(treq);
299 done2:
300 osi_FreeSmallSpace(OutFidStatus);
301 osi_FreeSmallSpace(OutDirStatus);
302 return code;
306 afs_MemHandleLink(struct vcache *avc, struct vrequest *areq)
308 struct dcache *tdc;
309 char *tp, *rbuf;
310 afs_size_t offset, len;
311 afs_int32 tlen, alen;
312 afs_int32 code;
314 AFS_STATCNT(afs_MemHandleLink);
315 /* two different formats, one for links protected 644, have a "." at
316 * the end of the file name, which we turn into a null. Others,
317 * protected 755, we add a null to the end of */
318 if (!avc->linkData) {
319 void *addr;
320 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
321 if (!tdc) {
322 return EIO;
324 /* otherwise we have the data loaded, go for it */
325 if (len > 1024) {
326 afs_PutDCache(tdc);
327 return EFAULT;
329 if (avc->f.m.Mode & 0111)
330 alen = len + 1; /* regular link */
331 else
332 alen = len; /* mt point */
333 rbuf = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
334 ObtainReadLock(&tdc->lock);
335 addr = afs_MemCacheOpen(&tdc->f.inode);
336 tlen = len;
337 code = afs_MemReadBlk(addr, 0, rbuf, tlen);
338 afs_MemCacheClose(addr);
339 ReleaseReadLock(&tdc->lock);
340 afs_PutDCache(tdc);
341 rbuf[alen - 1] = 0;
342 alen = strlen(rbuf) + 1;
343 tp = afs_osi_Alloc(alen); /* make room for terminating null */
344 osi_Assert(tp != NULL);
345 memcpy(tp, rbuf, alen);
346 osi_FreeLargeSpace(rbuf);
347 if (code != len) {
348 afs_osi_Free(tp, alen);
349 return EIO;
351 avc->linkData = tp;
353 return 0;
357 afs_UFSHandleLink(struct vcache *avc, struct vrequest *areq)
359 struct dcache *tdc;
360 char *tp, *rbuf;
361 void *tfile;
362 afs_size_t offset, len;
363 afs_int32 tlen, alen;
364 afs_int32 code;
366 /* two different formats, one for links protected 644, have a "." at the
367 * end of the file name, which we turn into a null. Others, protected
368 * 755, we add a null to the end of */
369 AFS_STATCNT(afs_UFSHandleLink);
370 if (!avc->linkData) {
371 tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
372 afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
373 ICL_TYPE_POINTER, tdc, ICL_TYPE_OFFSET,
374 ICL_HANDLE_OFFSET(avc->f.m.Length));
375 if (!tdc) {
376 if (AFS_IS_DISCONNECTED)
377 return ENETDOWN;
378 else
379 return EIO;
381 /* otherwise we have the data loaded, go for it */
382 if (len > 1024) {
383 afs_PutDCache(tdc);
384 return EFAULT;
386 if (avc->f.m.Mode & 0111)
387 alen = len + 1; /* regular link */
388 else
389 alen = len; /* mt point */
390 rbuf = osi_AllocLargeSpace(AFS_LRALLOCSIZ);
391 tlen = len;
392 ObtainReadLock(&tdc->lock);
393 tfile = osi_UFSOpen(&tdc->f.inode);
394 if (!tfile) {
395 ReleaseReadLock(&tdc->lock);
396 afs_PutDCache(tdc);
397 osi_FreeLargeSpace(rbuf);
398 return EIO;
400 code = afs_osi_Read(tfile, -1, rbuf, tlen);
401 osi_UFSClose(tfile);
402 ReleaseReadLock(&tdc->lock);
403 afs_PutDCache(tdc);
404 rbuf[alen - 1] = '\0';
405 alen = strlen(rbuf) + 1;
406 tp = afs_osi_Alloc(alen); /* make room for terminating null */
407 osi_Assert(tp != NULL);
408 memcpy(tp, rbuf, alen);
409 osi_FreeLargeSpace(rbuf);
410 if (code != tlen) {
411 afs_osi_Free(tp, alen);
412 return EIO;
414 avc->linkData = tp;
416 return 0;
420 afs_readlink(OSI_VC_DECL(avc), struct uio *auio, afs_ucred_t *acred)
422 afs_int32 code;
423 struct vrequest *treq = NULL;
424 char *tp;
425 struct afs_fakestat_state fakestat;
426 OSI_VC_CONVERT(avc);
428 AFS_STATCNT(afs_readlink);
429 afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
430 if ((code = afs_CreateReq(&treq, acred)))
431 return code;
432 afs_InitFakeStat(&fakestat);
434 AFS_DISCON_LOCK();
436 code = afs_EvalFakeStat(&avc, &fakestat, treq);
437 if (code)
438 goto done;
439 code = afs_VerifyVCache(avc, treq);
440 if (code)
441 goto done;
442 if (vType(avc) != VLNK) {
443 code = EINVAL;
444 goto done;
446 ObtainWriteLock(&avc->lock, 158);
447 code = afs_HandleLink(avc, treq);
448 /* finally uiomove it to user-land */
449 if (code == 0) {
450 tp = avc->linkData;
451 if (tp)
452 AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
453 else {
454 code = EIO;
457 ReleaseWriteLock(&avc->lock);
458 done:
459 afs_PutFakeStat(&fakestat);
460 AFS_DISCON_UNLOCK();
461 code = afs_CheckCode(code, treq, 32);
462 afs_DestroyReq(treq);
463 return code;