ac18455f2a923ee0114d940ec9e8533a1a3896f7
[openafs.git] / src / afs / LINUX / osi_vnodeops.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  * Linux specific vnodeops. Also includes the glue routines required to call
12  * AFS vnodeops.
13  *
14  * So far the only truly scary part is that Linux relies on the inode cache
15  * to be up to date. Don't you dare break a callback and expect an fstat
16  * to give you meaningful information. This appears to be fixed in the 2.1
17  * development kernels. As it is we can fix this now by intercepting the 
18  * stat calls.
19  */
20
21 #include <afsconfig.h>
22 #include "afs/param.h"
23
24 RCSID
25     ("$Header$");
26
27 #include "afs/sysincludes.h"
28 #include "afsincludes.h"
29 #include "afs/afs_stats.h"
30 #include "h/mm.h"
31 #ifdef HAVE_MM_INLINE_H
32 #include "h/mm_inline.h"
33 #endif
34 #include "h/pagemap.h"
35 #if defined(AFS_LINUX24_ENV)
36 #include "h/smp_lock.h"
37 #endif
38 #if defined(AFS_LINUX26_ENV)
39 #include "h/writeback.h"
40 #endif
41
42 #ifdef pgoff2loff
43 #define pageoff(pp) pgoff2loff((pp)->index)
44 #else
45 #define pageoff(pp) pp->offset
46 #endif
47
48 #if defined(AFS_LINUX26_ENV)
49 #define UnlockPage(pp) unlock_page(pp)
50 #endif
51
52 extern struct vcache *afs_globalVp;
53 static ssize_t
54 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
55 {
56     ssize_t code;
57     struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
58     cred_t *credp = crref();
59     struct vrequest treq;
60
61     AFS_GLOCK();
62     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
63                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
64                99999);
65
66     /* get a validated vcache entry */
67     code = afs_InitReq(&treq, credp);
68     if (!code)
69         code = afs_VerifyVCache(vcp, &treq);
70
71     if (code)
72         code = -code;
73     else {
74             osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
75             AFS_GUNLOCK();
76 #ifdef DO_SYNC_READ
77             code = do_sync_read(fp, buf, count, offp);
78 #else
79             code = generic_file_read(fp, buf, count, offp);
80 #endif
81             AFS_GLOCK();
82     }
83
84     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
85                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
86                code);
87
88     AFS_GUNLOCK();
89     crfree(credp);
90     return code;
91 }
92
93
94 /* Now we have integrated VM for writes as well as reads. generic_file_write
95  * also takes care of re-positioning the pointer if file is open in append
96  * mode. Call fake open/close to ensure we do writes of core dumps.
97  */
98 static ssize_t
99 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
100 {
101     ssize_t code = 0;
102     struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
103     struct vrequest treq;
104     cred_t *credp = crref();
105
106     AFS_GLOCK();
107
108     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
109                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
110                (fp->f_flags & O_APPEND) ? 99998 : 99999);
111
112
113     /* get a validated vcache entry */
114     code = (ssize_t) afs_InitReq(&treq, credp);
115     if (!code)
116         code = (ssize_t) afs_VerifyVCache(vcp, &treq);
117
118     ObtainWriteLock(&vcp->lock, 529);
119     afs_FakeOpen(vcp);
120     ReleaseWriteLock(&vcp->lock);
121     if (code)
122         code = -code;
123     else {
124             AFS_GUNLOCK();
125 #ifdef DO_SYNC_READ
126             code = do_sync_write(fp, buf, count, offp);
127 #else
128             code = generic_file_write(fp, buf, count, offp);
129 #endif
130             AFS_GLOCK();
131     }
132
133     ObtainWriteLock(&vcp->lock, 530);
134     afs_FakeClose(vcp, credp);
135     ReleaseWriteLock(&vcp->lock);
136
137     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
138                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
139                code);
140
141     AFS_GUNLOCK();
142     crfree(credp);
143     return code;
144 }
145
146 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
147
148 /* This is a complete rewrite of afs_readdir, since we can make use of
149  * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
150  * handling and use of bulkstats will need to be reflected here as well.
151  */
152 static int
153 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
154 {
155     extern struct DirEntry *afs_dir_GetBlob();
156     struct vcache *avc = VTOAFS(FILE_INODE(fp));
157     struct vrequest treq;
158     register struct dcache *tdc;
159     int code;
160     int offset;
161     int dirpos;
162     struct DirEntry *de;
163     ino_t ino;
164     int len;
165     afs_size_t origOffset, tlen;
166     cred_t *credp = crref();
167     struct afs_fakestat_state fakestat;
168
169 #if defined(AFS_LINUX26_ENV)
170     lock_kernel();
171 #endif
172     AFS_GLOCK();
173     AFS_STATCNT(afs_readdir);
174
175     code = afs_InitReq(&treq, credp);
176     crfree(credp);
177     if (code)
178         goto out1;
179
180     afs_InitFakeStat(&fakestat);
181     code = afs_EvalFakeStat(&avc, &fakestat, &treq);
182     if (code)
183         goto out;
184
185     /* update the cache entry */
186   tagain:
187     code = afs_VerifyVCache(avc, &treq);
188     if (code)
189         goto out;
190
191     /* get a reference to the entire directory */
192     tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
193     len = tlen;
194     if (!tdc) {
195         code = -ENOENT;
196         goto out;
197     }
198     ObtainSharedLock(&avc->lock, 810);
199     UpgradeSToWLock(&avc->lock, 811);
200     ObtainReadLock(&tdc->lock);
201     /*
202      * Make sure that the data in the cache is current. There are two
203      * cases we need to worry about:
204      * 1. The cache data is being fetched by another process.
205      * 2. The cache data is no longer valid
206      */
207     while ((avc->states & CStatd)
208            && (tdc->dflags & DFFetching)
209            && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
210         ReleaseReadLock(&tdc->lock);
211         ReleaseSharedLock(&avc->lock);
212         afs_osi_Sleep(&tdc->validPos);
213         ObtainSharedLock(&avc->lock, 812);
214         ObtainReadLock(&tdc->lock);
215     }
216     if (!(avc->states & CStatd)
217         || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
218         ReleaseReadLock(&tdc->lock);
219         ReleaseSharedLock(&avc->lock);
220         afs_PutDCache(tdc);
221         goto tagain;
222     }
223
224     /* Set the readdir-in-progress flag, and downgrade the lock
225      * to shared so others will be able to acquire a read lock.
226      */
227     avc->states |= CReadDir;
228     avc->dcreaddir = tdc;
229     avc->readdir_pid = MyPidxx;
230     ConvertWToSLock(&avc->lock);
231
232     /* Fill in until we get an error or we're done. This implementation
233      * takes an offset in units of blobs, rather than bytes.
234      */
235     code = 0;
236     offset = (int) fp->f_pos;
237     while (1) {
238         dirpos = BlobScan(tdc, offset);
239         if (!dirpos)
240             break;
241
242         de = afs_dir_GetBlob(tdc, dirpos);
243         if (!de)
244             break;
245
246         ino = afs_calc_inum (avc->fid.Fid.Volume, ntohl(de->fid.vnode));
247
248         if (de->name)
249             len = strlen(de->name);
250         else {
251             printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n", 
252                    (unsigned long)&tdc->f.inode, dirpos);
253             DRelease((struct buffer *) de, 0);
254             ReleaseSharedLock(&avc->lock);
255             afs_PutDCache(tdc);
256             code = -ENOENT;
257             goto out;
258         }
259
260         /* filldir returns -EINVAL when the buffer is full. */
261 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
262         {
263             unsigned int type = DT_UNKNOWN;
264             struct VenusFid afid;
265             struct vcache *tvc;
266             int vtype;
267             afid.Cell = avc->fid.Cell;
268             afid.Fid.Volume = avc->fid.Fid.Volume;
269             afid.Fid.Vnode = ntohl(de->fid.vnode);
270             afid.Fid.Unique = ntohl(de->fid.vunique);
271             if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
272                 type = DT_DIR;
273             } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
274                 if (tvc->mvstat) {
275                     type = DT_DIR;
276                 } else if (((tvc->states) & (CStatd | CTruth))) {
277                     /* CTruth will be set if the object has
278                      *ever* been statd */
279                     vtype = vType(tvc);
280                     if (vtype == VDIR)
281                         type = DT_DIR;
282                     else if (vtype == VREG)
283                         type = DT_REG;
284                     /* Don't do this until we're sure it can't be a mtpt */
285                     /* else if (vtype == VLNK)
286                      * type=DT_LNK; */
287                     /* what other types does AFS support? */
288                 }
289                 /* clean up from afs_FindVCache */
290                 afs_PutVCache(tvc);
291             }
292             /* 
293              * If this is NFS readdirplus, then the filler is going to
294              * call getattr on this inode, which will deadlock if we're
295              * holding the GLOCK.
296              */
297             AFS_GUNLOCK();
298             code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
299             AFS_GLOCK();
300         }
301 #else
302         code = (*filldir) (dirbuf, de->name, len, offset, ino);
303 #endif
304         DRelease((struct buffer *)de, 0);
305         if (code)
306             break;
307         offset = dirpos + 1 + ((len + 16) >> 5);
308     }
309     /* If filldir didn't fill in the last one this is still pointing to that
310      * last attempt.
311      */
312     fp->f_pos = (loff_t) offset;
313
314     ReleaseReadLock(&tdc->lock);
315     afs_PutDCache(tdc);
316     UpgradeSToWLock(&avc->lock, 813);
317     avc->states &= ~CReadDir;
318     avc->dcreaddir = 0;
319     avc->readdir_pid = 0;
320     ReleaseSharedLock(&avc->lock);
321     code = 0;
322
323 out:
324     afs_PutFakeStat(&fakestat);
325 out1:
326     AFS_GUNLOCK();
327 #if defined(AFS_LINUX26_ENV)
328     unlock_kernel();
329 #endif
330     return code;
331 }
332
333
334 /* in afs_pioctl.c */
335 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
336                       unsigned long arg);
337
338 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
339 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
340                                unsigned long arg) {
341     return afs_xioctl(FILE_INODE(fp), fp, com, arg);
342
343 }
344 #endif
345
346
347 static int
348 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
349 {
350     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
351     cred_t *credp = crref();
352     struct vrequest treq;
353     int code;
354
355     AFS_GLOCK();
356 #if defined(AFS_LINUX24_ENV)
357     afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
358                ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
359                vmap->vm_end - vmap->vm_start);
360 #else
361     afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
362                ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
363                vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
364                vmap->vm_offset);
365 #endif
366
367     /* get a validated vcache entry */
368     code = afs_InitReq(&treq, credp);
369     if (code)
370         goto out_err;
371
372     code = afs_VerifyVCache(vcp, &treq);
373     if (code)
374         goto out_err;
375
376     osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
377
378     AFS_GUNLOCK();
379     code = generic_file_mmap(fp, vmap);
380     AFS_GLOCK();
381     if (!code)
382         vcp->states |= CMAPPED;
383
384 out:
385     AFS_GUNLOCK();
386     crfree(credp);
387     return code;
388
389 out_err:
390     code = -code;
391     goto out;
392 }
393
394 static int
395 afs_linux_open(struct inode *ip, struct file *fp)
396 {
397     struct vcache *vcp = VTOAFS(ip);
398     cred_t *credp = crref();
399     int code;
400
401 #ifdef AFS_LINUX24_ENV
402     lock_kernel();
403 #endif
404     AFS_GLOCK();
405     code = afs_open(&vcp, fp->f_flags, credp);
406     AFS_GUNLOCK();
407 #ifdef AFS_LINUX24_ENV
408     unlock_kernel();
409 #endif
410
411     crfree(credp);
412     return -code;
413 }
414
415 static int
416 afs_linux_release(struct inode *ip, struct file *fp)
417 {
418     struct vcache *vcp = VTOAFS(ip);
419     cred_t *credp = crref();
420     int code = 0;
421
422 #ifdef AFS_LINUX24_ENV
423     lock_kernel();
424 #endif
425     AFS_GLOCK();
426     code = afs_close(vcp, fp->f_flags, credp);
427     AFS_GUNLOCK();
428 #ifdef AFS_LINUX24_ENV
429     unlock_kernel();
430 #endif
431
432     crfree(credp);
433     return -code;
434 }
435
436 static int
437 #if defined(AFS_LINUX24_ENV)
438 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
439 #else
440 afs_linux_fsync(struct file *fp, struct dentry *dp)
441 #endif
442 {
443     int code;
444     struct inode *ip = FILE_INODE(fp);
445     cred_t *credp = crref();
446
447 #ifdef AFS_LINUX24_ENV
448     lock_kernel();
449 #endif
450     AFS_GLOCK();
451     code = afs_fsync(VTOAFS(ip), credp);
452     AFS_GUNLOCK();
453 #ifdef AFS_LINUX24_ENV
454     unlock_kernel();
455 #endif
456     crfree(credp);
457     return -code;
458
459 }
460
461
462 static int
463 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
464 {
465     int code = 0;
466     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
467     cred_t *credp = crref();
468     struct AFS_FLOCK flock;
469     /* Convert to a lock format afs_lockctl understands. */
470     memset((char *)&flock, 0, sizeof(flock));
471     flock.l_type = flp->fl_type;
472     flock.l_pid = flp->fl_pid;
473     flock.l_whence = 0;
474     flock.l_start = flp->fl_start;
475     flock.l_len = flp->fl_end - flp->fl_start;
476
477     /* Safe because there are no large files, yet */
478 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
479     if (cmd == F_GETLK64)
480         cmd = F_GETLK;
481     else if (cmd == F_SETLK64)
482         cmd = F_SETLK;
483     else if (cmd == F_SETLKW64)
484         cmd = F_SETLKW;
485 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
486
487     AFS_GLOCK();
488     code = afs_lockctl(vcp, &flock, cmd, credp);
489     AFS_GUNLOCK();
490
491 #ifdef AFS_LINUX24_ENV
492     if ((code == 0 || flp->fl_type == F_UNLCK) && 
493         (cmd == F_SETLK || cmd == F_SETLKW)) {
494 #ifdef POSIX_LOCK_FILE_WAIT_ARG
495         code = posix_lock_file(fp, flp, 0);
496 #else
497         flp->fl_flags &=~ FL_SLEEP;
498         code = posix_lock_file(fp, flp);
499 #endif 
500         if (code && flp->fl_type != F_UNLCK) {
501             struct AFS_FLOCK flock2;
502             flock2 = flock;
503             flock2.l_type = F_UNLCK;
504             AFS_GLOCK();
505             afs_lockctl(vcp, &flock2, F_SETLK, credp);
506             AFS_GUNLOCK();
507         }
508     }
509 #endif
510     /* Convert flock back to Linux's file_lock */
511     flp->fl_type = flock.l_type;
512     flp->fl_pid = flock.l_pid;
513     flp->fl_start = flock.l_start;
514     flp->fl_end = flock.l_start + flock.l_len;
515
516     crfree(credp);
517     return -code;
518
519 }
520
521 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
522 static int
523 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
524     int code = 0;
525     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
526     cred_t *credp = crref();
527     struct AFS_FLOCK flock;
528     /* Convert to a lock format afs_lockctl understands. */
529     memset((char *)&flock, 0, sizeof(flock));
530     flock.l_type = flp->fl_type;
531     flock.l_pid = flp->fl_pid;
532     flock.l_whence = 0;
533     flock.l_start = 0;
534     flock.l_len = OFFSET_MAX;
535
536     /* Safe because there are no large files, yet */
537 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
538     if (cmd == F_GETLK64)
539         cmd = F_GETLK;
540     else if (cmd == F_SETLK64)
541         cmd = F_SETLK;
542     else if (cmd == F_SETLKW64)
543         cmd = F_SETLKW;
544 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
545
546     AFS_GLOCK();
547     code = afs_lockctl(vcp, &flock, cmd, credp);
548     AFS_GUNLOCK();
549
550     if ((code == 0 || flp->fl_type == F_UNLCK) && 
551         (cmd == F_SETLK || cmd == F_SETLKW)) {
552         flp->fl_flags &=~ FL_SLEEP;
553         code = flock_lock_file_wait(fp, flp);
554         if (code && flp->fl_type != F_UNLCK) {
555             struct AFS_FLOCK flock2;
556             flock2 = flock;
557             flock2.l_type = F_UNLCK;
558             AFS_GLOCK();
559             afs_lockctl(vcp, &flock2, F_SETLK, credp);
560             AFS_GUNLOCK();
561         }
562     }
563     /* Convert flock back to Linux's file_lock */
564     flp->fl_type = flock.l_type;
565     flp->fl_pid = flock.l_pid;
566
567     crfree(credp);
568     return -code;
569 }
570 #endif
571
572 /* afs_linux_flush
573  * essentially the same as afs_fsync() but we need to get the return
574  * code for the sys_close() here, not afs_linux_release(), so call
575  * afs_StoreAllSegments() with AFS_LASTSTORE
576  */
577 static int
578 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
579 afs_linux_flush(struct file *fp, fl_owner_t id)
580 #else
581 afs_linux_flush(struct file *fp)
582 #endif
583 {
584     struct vrequest treq;
585     struct vcache *vcp;
586     cred_t *credp;
587     int code;
588
589     AFS_GLOCK();
590
591     if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
592         AFS_GUNLOCK();
593         return 0;
594     }
595
596     AFS_DISCON_LOCK();
597
598     credp = crref();
599     vcp = VTOAFS(FILE_INODE(fp));
600
601     code = afs_InitReq(&treq, credp);
602     if (code)
603         goto out;
604
605     ObtainSharedLock(&vcp->lock, 535);
606     if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
607         UpgradeSToWLock(&vcp->lock, 536);
608         if (!AFS_IS_DISCONNECTED) {
609                 code = afs_StoreAllSegments(vcp,
610                                 &treq,
611                                 AFS_SYNC | AFS_LASTSTORE);
612         } else {
613 #if defined(AFS_DISCON_ENV)
614                 if (!vcp->ddirty_flags ||
615                     (vcp->ddirty_flags == VDisconShadowed)) {
616
617                     ObtainWriteLock(&afs_DDirtyVCListLock, 710);
618                     AFS_DISCON_ADD_DIRTY(vcp);
619                     ReleaseWriteLock(&afs_DDirtyVCListLock);
620                 }
621
622                 /* Set disconnected write flag. */
623                 vcp->ddirty_flags |= VDisconWriteOsiFlush;
624 #endif
625         }
626
627         ConvertWToSLock(&vcp->lock);
628     }
629     code = afs_CheckCode(code, &treq, 54);
630     ReleaseSharedLock(&vcp->lock);
631
632 out:
633     AFS_DISCON_UNLOCK();
634     AFS_GUNLOCK();
635
636     crfree(credp);
637     return -code;
638 }
639
640 #if !defined(AFS_LINUX24_ENV)
641 /* Not allowed to directly read a directory. */
642 ssize_t
643 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
644 {
645     return -EISDIR;
646 }
647 #endif
648
649
650
651 struct file_operations afs_dir_fops = {
652 #if !defined(AFS_LINUX24_ENV)
653   .read =       afs_linux_dir_read,
654   .lock =       afs_linux_lock,
655   .fsync =      afs_linux_fsync,
656 #else
657   .read =       generic_read_dir,
658 #endif
659   .readdir =    afs_linux_readdir,
660 #ifdef HAVE_UNLOCKED_IOCTL
661   .unlocked_ioctl = afs_unlocked_xioctl,
662 #else
663   .ioctl =      afs_xioctl,
664 #endif
665 #ifdef HAVE_COMPAT_IOCTL
666   .compat_ioctl = afs_unlocked_xioctl,
667 #endif
668   .open =       afs_linux_open,
669   .release =    afs_linux_release,
670 };
671
672 struct file_operations afs_file_fops = {
673   .read =       afs_linux_read,
674   .write =      afs_linux_write,
675 #ifdef GENERIC_FILE_AIO_READ
676   .aio_read =   generic_file_aio_read,
677   .aio_write =  generic_file_aio_write,
678 #endif
679 #ifdef HAVE_UNLOCKED_IOCTL
680   .unlocked_ioctl = afs_unlocked_xioctl,
681 #else
682   .ioctl =      afs_xioctl,
683 #endif
684 #ifdef HAVE_COMPAT_IOCTL
685   .compat_ioctl = afs_unlocked_xioctl,
686 #endif
687   .mmap =       afs_linux_mmap,
688   .open =       afs_linux_open,
689   .flush =      afs_linux_flush,
690 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SENDFILE)
691   .sendfile =   generic_file_sendfile,
692 #endif
693 #if defined(AFS_LINUX26_ENV) && defined(STRUCT_FILE_OPERATIONS_HAS_SPLICE)
694   .splice_write = generic_file_splice_write,
695   .splice_read = generic_file_splice_read,
696 #endif
697   .release =    afs_linux_release,
698   .fsync =      afs_linux_fsync,
699   .lock =       afs_linux_lock,
700 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
701   .flock =      afs_linux_flock,
702 #endif
703 };
704
705
706 /**********************************************************************
707  * AFS Linux dentry operations
708  **********************************************************************/
709
710 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
711  * that has its mvid (parent dir's fid) pointer set to the wrong directory
712  * due to being mounted in multiple points at once. If so, check_bad_parent()
713  * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
714  * dotdotfid and mtpoint fid members.
715  * Parameters:
716  *   dp - dentry to be checked.
717  * Return Values:
718  *   None.
719  * Sideeffects:
720  *   This dentry's vcache's mvid will be set to the correct parent directory's
721  *   fid.
722  *   This root vnode's volume will have its dotdotfid and mtpoint fids set
723  *   to the correct parent and mountpoint fids.
724  */
725
726 static inline void
727 check_bad_parent(struct dentry *dp)
728 {
729     cred_t *credp;
730     struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
731     struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
732
733     if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
734         credp = crref();
735
736         /* force a lookup, so vcp->mvid is fixed up */
737         afs_lookup(pvc, dp->d_name.name, &avc, credp);
738         if (!avc || vcp != avc) {       /* bad, very bad.. */
739             afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
740                        "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
741                        ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
742                        ICL_TYPE_POINTER, dp);
743         }
744         if (avc)
745             AFS_RELE(AFSTOV(avc));
746         crfree(credp);
747     }
748
749     return;
750 }
751
752 /* afs_linux_revalidate
753  * Ensure vcache is stat'd before use. Return 0 if entry is valid.
754  */
755 static int
756 afs_linux_revalidate(struct dentry *dp)
757 {
758     struct vattr vattr;
759     struct vcache *vcp = VTOAFS(dp->d_inode);
760     cred_t *credp;
761     int code;
762
763 #ifdef AFS_LINUX24_ENV
764     lock_kernel();
765 #endif
766     AFS_GLOCK();
767
768 #ifdef notyet
769     /* Make this a fast path (no crref), since it's called so often. */
770     if (vcp->states & CStatd) {
771
772         if (*dp->d_name.name != '/' && vcp->mvstat == 2)        /* root vnode */
773             check_bad_parent(dp);       /* check and correct mvid */
774
775         AFS_GUNLOCK();
776 #ifdef AFS_LINUX24_ENV
777         unlock_kernel();
778 #endif
779         return 0;
780     }
781 #endif
782
783     credp = crref();
784     code = afs_getattr(vcp, &vattr, credp);
785     if (!code)
786         vattr2inode(AFSTOV(vcp), &vattr);
787
788     AFS_GUNLOCK();
789 #ifdef AFS_LINUX24_ENV
790     unlock_kernel();
791 #endif
792     crfree(credp);
793
794     return -code;
795 }
796
797 #if defined(AFS_LINUX26_ENV)
798 static int
799 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
800 {
801         int err = afs_linux_revalidate(dentry);
802         if (!err) {
803                 generic_fillattr(dentry->d_inode, stat);
804 }
805         return err;
806 }
807 #endif
808
809 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
810  * In kernels 2.2.10 and above, we are passed an additional flags var which
811  * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
812  * we are advised to follow the entry if it is a link or to make sure that 
813  * it is a directory. But since the kernel itself checks these possibilities
814  * later on, we shouldn't have to do it until later. Perhaps in the future..
815  */
816 static int
817 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
818 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
819 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
820 #else
821 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
822 #endif
823 #else
824 afs_linux_dentry_revalidate(struct dentry *dp)
825 #endif
826 {
827     struct vattr vattr;
828     cred_t *credp = NULL;
829     struct vcache *vcp, *pvcp, *tvc = NULL;
830     int valid;
831
832 #ifdef AFS_LINUX24_ENV
833     lock_kernel();
834 #endif
835     AFS_GLOCK();
836
837     if (dp->d_inode) {
838
839         vcp = VTOAFS(dp->d_inode);
840         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
841
842         if (vcp == afs_globalVp)
843             goto good_dentry;
844
845         if (vcp->mvstat == 1) {         /* mount point */
846             if (vcp->mvid && (vcp->states & CMValid)) {
847                 /* a mount point, not yet replaced by its directory */
848                 goto bad_dentry;
849             }
850         } else
851             if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
852                 check_bad_parent(dp);   /* check and correct mvid */
853
854 #ifdef notdef
855         /* If the last looker changes, we should make sure the current
856          * looker still has permission to examine this file.  This would
857          * always require a crref() which would be "slow".
858          */
859         if (vcp->last_looker != treq.uid) {
860             if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
861                 goto bad_dentry;
862
863             vcp->last_looker = treq.uid;
864         }
865 #endif
866
867         /* If the parent's DataVersion has changed or the vnode
868          * is longer valid, we need to do a full lookup.  VerifyVCache
869          * isn't enough since the vnode may have been renamed.
870          */
871
872         if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
873
874             credp = crref();
875             afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
876             if (!tvc || tvc != vcp)
877                 goto bad_dentry;
878
879             if (afs_getattr(vcp, &vattr, credp))
880                 goto bad_dentry;
881
882             vattr2inode(AFSTOV(vcp), &vattr);
883             dp->d_time = hgetlo(pvcp->m.DataVersion);
884         }
885
886         /* should we always update the attributes at this point? */
887         /* unlikely--the vcache entry hasn't changed */
888
889     } else {
890 #ifdef notyet
891         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
892         if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
893             goto bad_dentry;
894 #endif
895
896         /* No change in parent's DataVersion so this negative
897          * lookup is still valid.  BUT, if a server is down a
898          * negative lookup can result so there should be a
899          * liftime as well.  For now, always expire.
900          */
901
902         goto bad_dentry;
903     }
904
905   good_dentry:
906     valid = 1;
907
908   done:
909     /* Clean up */
910     if (tvc)
911         afs_PutVCache(tvc);
912     AFS_GUNLOCK();
913     if (credp)
914         crfree(credp);
915
916     if (!valid) {
917         shrink_dcache_parent(dp);
918         d_drop(dp);
919     }
920 #ifdef AFS_LINUX24_ENV
921     unlock_kernel();
922 #endif
923     return valid;
924
925   bad_dentry:
926     valid = 0;
927     goto done;
928 }
929
930 static void
931 afs_dentry_iput(struct dentry *dp, struct inode *ip)
932 {
933     struct vcache *vcp = VTOAFS(ip);
934
935     AFS_GLOCK();
936     (void) afs_InactiveVCache(vcp, NULL);
937     AFS_GUNLOCK();
938 #ifdef DCACHE_NFSFS_RENAMED
939 #ifdef AFS_LINUX26_ENV
940     spin_lock(&dp->d_lock);
941 #endif
942     dp->d_flags &= ~DCACHE_NFSFS_RENAMED;   
943 #ifdef AFS_LINUX26_ENV
944     spin_unlock(&dp->d_lock);
945 #endif
946 #endif
947
948     iput(ip);
949 }
950
951 static int
952 afs_dentry_delete(struct dentry *dp)
953 {
954     if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
955         return 1;               /* bad inode? */
956
957     return 0;
958 }
959
960 struct dentry_operations afs_dentry_operations = {
961   .d_revalidate =       afs_linux_dentry_revalidate,
962   .d_delete =           afs_dentry_delete,
963   .d_iput =             afs_dentry_iput,
964 };
965
966 /**********************************************************************
967  * AFS Linux inode operations
968  **********************************************************************/
969
970 /* afs_linux_create
971  *
972  * Merely need to set enough of vattr to get us through the create. Note
973  * that the higher level code (open_namei) will take care of any tuncation
974  * explicitly. Exclusive open is also taken care of in open_namei.
975  *
976  * name is in kernel space at this point.
977  */
978 static int
979 #ifdef IOP_CREATE_TAKES_NAMEIDATA
980 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
981                  struct nameidata *nd)
982 #else
983 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
984 #endif
985 {
986     struct vattr vattr;
987     cred_t *credp = crref();
988     const char *name = dp->d_name.name;
989     struct vcache *vcp;
990     int code;
991
992     VATTR_NULL(&vattr);
993     vattr.va_mode = mode;
994     vattr.va_type = mode & S_IFMT;
995
996 #if defined(AFS_LINUX26_ENV)
997     lock_kernel();
998 #endif
999     AFS_GLOCK();
1000     code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1001                       &vcp, credp);
1002
1003     if (!code) {
1004         struct inode *ip = AFSTOV(vcp);
1005
1006         afs_getattr(vcp, &vattr, credp);
1007         afs_fill_inode(ip, &vattr);
1008         insert_inode_hash(ip);
1009         dp->d_op = &afs_dentry_operations;
1010         dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1011         d_instantiate(dp, ip);
1012     }
1013     AFS_GUNLOCK();
1014
1015 #if defined(AFS_LINUX26_ENV)
1016     unlock_kernel();
1017 #endif
1018     crfree(credp);
1019     return -code;
1020 }
1021
1022 /* afs_linux_lookup */
1023 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1024 static struct dentry *
1025 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1026 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1027                  struct nameidata *nd)
1028 #else
1029 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1030 #endif
1031 #else
1032 static int
1033 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1034 #endif
1035 {
1036     cred_t *credp = crref();
1037     struct vcache *vcp = NULL;
1038     const char *comp = dp->d_name.name;
1039     struct inode *ip = NULL;
1040 #if defined(AFS_LINUX26_ENV)
1041     struct dentry *newdp = NULL;
1042 #endif
1043     int code;
1044
1045 #if defined(AFS_LINUX26_ENV)
1046     lock_kernel();
1047 #endif
1048     AFS_GLOCK();
1049     code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
1050     
1051     if (vcp) {
1052         struct vattr vattr;
1053
1054         ip = AFSTOV(vcp);
1055         afs_getattr(vcp, &vattr, credp);
1056         afs_fill_inode(ip, &vattr);
1057         if (
1058 #ifdef HAVE_KERNEL_HLIST_UNHASHED
1059             hlist_unhashed(&ip->i_hash)
1060 #elif defined(AFS_LINUX26_ENV)
1061             ip->i_hash.pprev == NULL
1062 #else
1063             ip->i_hash.prev == NULL
1064 #endif
1065             )
1066             insert_inode_hash(ip);
1067     }
1068     dp->d_op = &afs_dentry_operations;
1069     dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1070     AFS_GUNLOCK();
1071
1072 #if defined(AFS_LINUX24_ENV)
1073     if (ip && S_ISDIR(ip->i_mode)) {
1074         struct dentry *alias;
1075
1076         /* Try to invalidate an existing alias in favor of our new one */
1077         alias = d_find_alias(ip);
1078 #if defined(AFS_LINUX26_ENV)
1079         /* But not if it's disconnected; then we want d_splice_alias below */
1080         if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1081 #else
1082         if (alias) {
1083 #endif
1084             if (d_invalidate(alias) == 0) {
1085                 dput(alias);
1086             } else {
1087                 iput(ip);
1088 #if defined(AFS_LINUX26_ENV)
1089                 unlock_kernel();
1090 #endif
1091                 crfree(credp);
1092                 return alias;
1093             }
1094         }
1095     }
1096 #endif
1097 #if defined(AFS_LINUX26_ENV)
1098     newdp = d_splice_alias(ip, dp);
1099 #else
1100     d_add(dp, ip);
1101 #endif
1102
1103 #if defined(AFS_LINUX26_ENV)
1104     unlock_kernel();
1105 #endif
1106     crfree(credp);
1107
1108     /* It's ok for the file to not be found. That's noted by the caller by
1109      * seeing that the dp->d_inode field is NULL.
1110      */
1111 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1112 #if defined(AFS_LINUX26_ENV)
1113     if (!code || code == ENOENT)
1114         return newdp;
1115 #else
1116     if (code == ENOENT)
1117         return ERR_PTR(0);
1118 #endif
1119     else
1120         return ERR_PTR(-code);
1121 #else
1122     if (code == ENOENT)
1123         code = 0;
1124     return -code;
1125 #endif
1126 }
1127
1128 static int
1129 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1130 {
1131     int code;
1132     cred_t *credp = crref();
1133     const char *name = newdp->d_name.name;
1134     struct inode *oldip = olddp->d_inode;
1135
1136     /* If afs_link returned the vnode, we could instantiate the
1137      * dentry. Since it's not, we drop this one and do a new lookup.
1138      */
1139     d_drop(newdp);
1140
1141     AFS_GLOCK();
1142     code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1143
1144     AFS_GUNLOCK();
1145     crfree(credp);
1146     return -code;
1147 }
1148
1149 static int
1150 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1151 {
1152     int code = EBUSY;
1153     cred_t *credp = crref();
1154     const char *name = dp->d_name.name;
1155     struct vcache *tvc = VTOAFS(dp->d_inode);
1156
1157 #if defined(AFS_LINUX26_ENV)
1158     lock_kernel();
1159 #endif
1160     if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1161                                 && !(tvc->states & CUnlinked)) {
1162         struct dentry *__dp;
1163         char *__name;
1164         extern char *afs_newname();
1165
1166         __dp = NULL;
1167         __name = NULL;
1168         do {
1169             dput(__dp);
1170
1171             AFS_GLOCK();
1172             if (__name)
1173                 osi_FreeSmallSpace(__name);
1174             __name = afs_newname();
1175             AFS_GUNLOCK();
1176
1177             __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1178                 
1179             if (IS_ERR(__dp))
1180                 goto out;
1181         } while (__dp->d_inode != NULL);
1182
1183         AFS_GLOCK();
1184         code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1185         if (!code) {
1186             tvc->mvid = (void *) __name;
1187             crhold(credp);
1188             if (tvc->uncred) {
1189                 crfree(tvc->uncred);
1190             }
1191             tvc->uncred = credp;
1192             tvc->states |= CUnlinked;
1193 #ifdef DCACHE_NFSFS_RENAMED
1194 #ifdef AFS_LINUX26_ENV
1195             spin_lock(&dp->d_lock);
1196 #endif
1197             dp->d_flags |= DCACHE_NFSFS_RENAMED;   
1198 #ifdef AFS_LINUX26_ENV
1199             spin_unlock(&dp->d_lock);
1200 #endif
1201 #endif
1202         } else {
1203             osi_FreeSmallSpace(__name); 
1204         }
1205         AFS_GUNLOCK();
1206
1207         if (!code) {
1208             __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1209             d_move(dp, __dp);
1210         }
1211         dput(__dp);
1212
1213         goto out;
1214     }
1215
1216     AFS_GLOCK();
1217     code = afs_remove(VTOAFS(dip), name, credp);
1218     AFS_GUNLOCK();
1219     if (!code)
1220         d_drop(dp);
1221 out:
1222 #if defined(AFS_LINUX26_ENV)
1223     unlock_kernel();
1224 #endif
1225     crfree(credp);
1226     return -code;
1227 }
1228
1229
1230 static int
1231 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1232 {
1233     int code;
1234     cred_t *credp = crref();
1235     struct vattr vattr;
1236     const char *name = dp->d_name.name;
1237
1238     /* If afs_symlink returned the vnode, we could instantiate the
1239      * dentry. Since it's not, we drop this one and do a new lookup.
1240      */
1241     d_drop(dp);
1242
1243     VATTR_NULL(&vattr);
1244     AFS_GLOCK();
1245     code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1246     AFS_GUNLOCK();
1247     crfree(credp);
1248     return -code;
1249 }
1250
1251 static int
1252 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1253 {
1254     int code;
1255     cred_t *credp = crref();
1256     struct vcache *tvcp = NULL;
1257     struct vattr vattr;
1258     const char *name = dp->d_name.name;
1259
1260 #if defined(AFS_LINUX26_ENV)
1261     lock_kernel();
1262 #endif
1263     VATTR_NULL(&vattr);
1264     vattr.va_mask = ATTR_MODE;
1265     vattr.va_mode = mode;
1266     AFS_GLOCK();
1267     code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1268
1269     if (tvcp) {
1270         struct inode *ip = AFSTOV(tvcp);
1271
1272         afs_getattr(tvcp, &vattr, credp);
1273         afs_fill_inode(ip, &vattr);
1274
1275         dp->d_op = &afs_dentry_operations;
1276         dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1277         d_instantiate(dp, ip);
1278     }
1279     AFS_GUNLOCK();
1280
1281 #if defined(AFS_LINUX26_ENV)
1282     unlock_kernel();
1283 #endif
1284     crfree(credp);
1285     return -code;
1286 }
1287
1288 static int
1289 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1290 {
1291     int code;
1292     cred_t *credp = crref();
1293     const char *name = dp->d_name.name;
1294
1295     /* locking kernel conflicts with glock? */
1296
1297     AFS_GLOCK();
1298     code = afs_rmdir(VTOAFS(dip), name, credp);
1299     AFS_GUNLOCK();
1300
1301     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1302      * that failed because a directory is not empty. So, we map
1303      * EEXIST to ENOTEMPTY on linux.
1304      */
1305     if (code == EEXIST) {
1306         code = ENOTEMPTY;
1307     }
1308
1309     if (!code) {
1310         d_drop(dp);
1311     }
1312
1313     crfree(credp);
1314     return -code;
1315 }
1316
1317
1318 static int
1319 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1320                  struct inode *newip, struct dentry *newdp)
1321 {
1322     int code;
1323     cred_t *credp = crref();
1324     const char *oldname = olddp->d_name.name;
1325     const char *newname = newdp->d_name.name;
1326     struct dentry *rehash = NULL;
1327
1328 #if defined(AFS_LINUX26_ENV)
1329     /* Prevent any new references during rename operation. */
1330     lock_kernel();
1331
1332     if (!d_unhashed(newdp)) {
1333         d_drop(newdp);
1334         rehash = newdp;
1335     }
1336 #else
1337     if (!list_empty(&newdp->d_hash)) {
1338         d_drop(newdp);
1339         rehash = newdp;
1340     }
1341 #endif
1342
1343 #if defined(AFS_LINUX24_ENV)
1344     if (atomic_read(&olddp->d_count) > 1)
1345         shrink_dcache_parent(olddp);
1346 #endif
1347
1348     AFS_GLOCK();
1349     code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1350     AFS_GUNLOCK();
1351
1352     if (!code)
1353         olddp->d_time = 0;      /* force to revalidate */
1354
1355     if (rehash)
1356         d_rehash(rehash);
1357
1358 #if defined(AFS_LINUX26_ENV)
1359     unlock_kernel();
1360 #endif
1361
1362     crfree(credp);
1363     return -code;
1364 }
1365
1366
1367 /* afs_linux_ireadlink 
1368  * Internal readlink which can return link contents to user or kernel space.
1369  * Note that the buffer is NOT supposed to be null-terminated.
1370  */
1371 static int
1372 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1373 {
1374     int code;
1375     cred_t *credp = crref();
1376     uio_t tuio;
1377     struct iovec iov;
1378
1379     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1380     code = afs_readlink(VTOAFS(ip), &tuio, credp);
1381     crfree(credp);
1382
1383     if (!code)
1384         return maxlen - tuio.uio_resid;
1385     else
1386         return -code;
1387 }
1388
1389 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1390 /* afs_linux_readlink 
1391  * Fill target (which is in user space) with contents of symlink.
1392  */
1393 static int
1394 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1395 {
1396     int code;
1397     struct inode *ip = dp->d_inode;
1398
1399     AFS_GLOCK();
1400     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1401     AFS_GUNLOCK();
1402     return code;
1403 }
1404
1405
1406 /* afs_linux_follow_link
1407  * a file system dependent link following routine.
1408  */
1409 #if defined(AFS_LINUX24_ENV)
1410 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1411 {
1412     int code;
1413     char *name;
1414
1415     name = osi_Alloc(PATH_MAX);
1416     if (!name) {
1417         return -EIO;
1418     }
1419
1420     AFS_GLOCK();
1421     code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1422     AFS_GUNLOCK();
1423
1424     if (code < 0) {
1425         goto out;
1426     }
1427
1428     name[code] = '\0';
1429     code = vfs_follow_link(nd, name);
1430
1431 out:
1432     osi_Free(name, PATH_MAX);
1433
1434     return code;
1435 }
1436
1437 #else /* !defined(AFS_LINUX24_ENV) */
1438
1439 static struct dentry *
1440 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1441                       unsigned int follow)
1442 {
1443     int code = 0;
1444     char *name;
1445     struct dentry *res;
1446
1447
1448     AFS_GLOCK();
1449     name = osi_Alloc(PATH_MAX + 1);
1450     if (!name) {
1451         AFS_GUNLOCK();
1452         dput(basep);
1453         return ERR_PTR(-EIO);
1454     }
1455
1456     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1457     AFS_GUNLOCK();
1458
1459     if (code < 0) {
1460         dput(basep);
1461         res = ERR_PTR(code);
1462     } else {
1463         name[code] = '\0';
1464         res = lookup_dentry(name, basep, follow);
1465     }
1466
1467     AFS_GLOCK();
1468     osi_Free(name, PATH_MAX + 1);
1469     AFS_GUNLOCK();
1470     return res;
1471 }
1472 #endif /* AFS_LINUX24_ENV */
1473 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1474
1475 /* afs_linux_readpage
1476  * all reads come through here. A strategy-like read call.
1477  */
1478 static int
1479 afs_linux_readpage(struct file *fp, struct page *pp)
1480 {
1481     int code;
1482     cred_t *credp = crref();
1483 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1484     char *address;
1485     afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1486 #else
1487     ulong address = afs_linux_page_address(pp);
1488     afs_offs_t offset = pageoff(pp);
1489 #endif
1490     uio_t tuio;
1491     struct iovec iovec;
1492     struct inode *ip = FILE_INODE(fp);
1493     int cnt = page_count(pp);
1494     struct vcache *avc = VTOAFS(ip);
1495
1496
1497 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1498     address = kmap(pp);
1499     ClearPageError(pp);
1500 #else
1501     atomic_add(1, &pp->count);
1502     set_bit(PG_locked, &pp->flags);     /* other bits? See mm.h */
1503     clear_bit(PG_error, &pp->flags);
1504 #endif
1505
1506     setup_uio(&tuio, &iovec, (char *)address, offset, PAGE_SIZE, UIO_READ,
1507               AFS_UIOSYS);
1508 #ifdef AFS_LINUX24_ENV
1509     lock_kernel();
1510 #endif
1511     AFS_GLOCK();
1512     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip, ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32, 99999); /* not a possible code value */
1513     code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1514     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1515                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1516                code);
1517     AFS_GUNLOCK();
1518 #ifdef AFS_LINUX24_ENV
1519     unlock_kernel();
1520 #endif
1521
1522     if (!code) {
1523         if (tuio.uio_resid)     /* zero remainder of page */
1524             memset((void *)(address + (PAGE_SIZE - tuio.uio_resid)), 0,
1525                    tuio.uio_resid);
1526 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1527         flush_dcache_page(pp);
1528         SetPageUptodate(pp);
1529 #else
1530         set_bit(PG_uptodate, &pp->flags);
1531 #endif
1532     }
1533
1534 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1535     kunmap(pp);
1536     UnlockPage(pp);
1537 #else
1538     clear_bit(PG_locked, &pp->flags);
1539     wake_up(&pp->wait);
1540     free_page(address);
1541 #endif
1542
1543     if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1544         struct dcache *tdc;
1545         struct vrequest treq;
1546
1547         AFS_GLOCK();
1548         code = afs_InitReq(&treq, credp);
1549         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1550             tdc = afs_FindDCache(avc, offset);
1551             if (tdc) {
1552                 if (!(tdc->mflags & DFNextStarted))
1553                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1554                 afs_PutDCache(tdc);
1555             }
1556             ReleaseWriteLock(&avc->lock);
1557         }
1558         AFS_GUNLOCK();
1559     }
1560
1561     crfree(credp);
1562     return -code;
1563 }
1564
1565
1566 #if defined(AFS_LINUX24_ENV)
1567 static int
1568 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1569                          unsigned long offset, unsigned int count)
1570 {
1571     struct vcache *vcp = VTOAFS(ip);
1572     char *buffer;
1573     afs_offs_t base;
1574     int code = 0;
1575     cred_t *credp;
1576     uio_t tuio;
1577     struct iovec iovec;
1578     int f_flags = 0;
1579
1580     buffer = kmap(pp) + offset;
1581     base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT)  + offset;
1582
1583     credp = crref();
1584     lock_kernel();
1585     AFS_GLOCK();
1586     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1587                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1588                ICL_TYPE_INT32, 99999);
1589
1590     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1591
1592     code = afs_write(vcp, &tuio, f_flags, credp, 0);
1593
1594     ip->i_size = vcp->m.Length;
1595     ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1596
1597     if (!code) {
1598         struct vrequest treq;
1599
1600         ObtainWriteLock(&vcp->lock, 533);
1601         if (!afs_InitReq(&treq, credp))
1602             code = afs_DoPartialWrite(vcp, &treq);
1603         ReleaseWriteLock(&vcp->lock);
1604     }
1605     code = code ? -code : count - tuio.uio_resid;
1606
1607     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1608                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1609                ICL_TYPE_INT32, code);
1610
1611     AFS_GUNLOCK();
1612     unlock_kernel();
1613     crfree(credp);
1614     kunmap(pp);
1615
1616     return code;
1617 }
1618
1619
1620 static int
1621 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1622 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1623 #else
1624 afs_linux_writepage(struct page *pp)
1625 #endif
1626 {
1627     struct address_space *mapping = pp->mapping;
1628     struct inode *inode;
1629     unsigned long end_index;
1630     unsigned offset = PAGE_CACHE_SIZE;
1631     long status;
1632
1633 #if defined(AFS_LINUX26_ENV)
1634     if (PageReclaim(pp)) {
1635 # if defined(WRITEPAGE_ACTIVATE)
1636         return WRITEPAGE_ACTIVATE;
1637 # else 
1638         return AOP_WRITEPAGE_ACTIVATE;
1639 # endif
1640     }
1641 #else
1642     if (PageLaunder(pp)) {
1643         return(fail_writepage(pp));
1644     }
1645 #endif
1646
1647     inode = (struct inode *)mapping->host;
1648     end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1649
1650     /* easy case */
1651     if (pp->index < end_index)
1652         goto do_it;
1653     /* things got complicated... */
1654     offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1655     /* OK, are we completely out? */
1656     if (pp->index >= end_index + 1 || !offset)
1657         return -EIO;
1658   do_it:
1659     status = afs_linux_writepage_sync(inode, pp, 0, offset);
1660     SetPageUptodate(pp);
1661     UnlockPage(pp);
1662     if (status == offset)
1663         return 0;
1664     else
1665         return status;
1666 }
1667 #else
1668 /* afs_linux_updatepage
1669  * What one would have thought was writepage - write dirty page to file.
1670  * Called from generic_file_write. buffer is still in user space. pagep
1671  * has been filled in with old data if we're updating less than a page.
1672  */
1673 static int
1674 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1675                      unsigned int count, int sync)
1676 {
1677     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1678     u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1679     int code = 0;
1680     cred_t *credp;
1681     uio_t tuio;
1682     struct iovec iovec;
1683
1684     set_bit(PG_locked, &pp->flags);
1685
1686     credp = crref();
1687     AFS_GLOCK();
1688     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1689                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1690                ICL_TYPE_INT32, 99999);
1691     setup_uio(&tuio, &iovec, page_addr + offset,
1692               (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1693               AFS_UIOSYS);
1694
1695     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1696
1697     ip->i_size = vcp->m.Length;
1698     ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1699
1700     if (!code) {
1701         struct vrequest treq;
1702
1703         ObtainWriteLock(&vcp->lock, 533);
1704         vcp->m.Date = osi_Time();   /* set modification time */
1705         if (!afs_InitReq(&treq, credp))
1706             code = afs_DoPartialWrite(vcp, &treq);
1707         ReleaseWriteLock(&vcp->lock);
1708     }
1709
1710     code = code ? -code : count - tuio.uio_resid;
1711     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1712                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1713                ICL_TYPE_INT32, code);
1714
1715     AFS_GUNLOCK();
1716     crfree(credp);
1717
1718     clear_bit(PG_locked, &pp->flags);
1719     return code;
1720 }
1721 #endif
1722
1723 /* afs_linux_permission
1724  * Check access rights - returns error if can't check or permission denied.
1725  */
1726 static int
1727 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1728 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1729 #else
1730 afs_linux_permission(struct inode *ip, int mode)
1731 #endif
1732 {
1733     int code;
1734     cred_t *credp = crref();
1735     int tmp = 0;
1736
1737     AFS_GLOCK();
1738     if (mode & MAY_EXEC)
1739         tmp |= VEXEC;
1740     if (mode & MAY_READ)
1741         tmp |= VREAD;
1742     if (mode & MAY_WRITE)
1743         tmp |= VWRITE;
1744     code = afs_access(VTOAFS(ip), tmp, credp);
1745
1746     AFS_GUNLOCK();
1747     crfree(credp);
1748     return -code;
1749 }
1750
1751 #if defined(AFS_LINUX24_ENV)
1752 static int
1753 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1754                        unsigned to)
1755 {
1756     int code;
1757
1758     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1759                                     offset, to - offset);
1760 #if !defined(AFS_LINUX26_ENV)
1761     kunmap(page);
1762 #endif
1763
1764     return code;
1765 }
1766
1767 static int
1768 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1769                         unsigned to)
1770 {
1771 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1772    call kmap directly instead of relying on us to do it */
1773 #if !defined(AFS_LINUX26_ENV)
1774     kmap(page);
1775 #endif
1776     return 0;
1777 }
1778
1779 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1780 #endif
1781
1782 static struct inode_operations afs_file_iops = {
1783 #if defined(AFS_LINUX26_ENV)
1784   .permission =         afs_linux_permission,
1785   .getattr =            afs_linux_getattr,
1786   .setattr =            afs_notify_change,
1787 #elif defined(AFS_LINUX24_ENV)
1788   .permission =         afs_linux_permission,
1789   .revalidate =         afs_linux_revalidate,
1790   .setattr =            afs_notify_change,
1791 #else
1792   .default_file_ops =   &afs_file_fops,
1793   .readpage =           afs_linux_readpage,
1794   .revalidate =         afs_linux_revalidate,
1795   .updatepage =         afs_linux_updatepage,
1796 #endif
1797 };
1798
1799 #if defined(AFS_LINUX24_ENV)
1800 static struct address_space_operations afs_file_aops = {
1801   .readpage =           afs_linux_readpage,
1802   .writepage =          afs_linux_writepage,
1803   .commit_write =       afs_linux_commit_write,
1804   .prepare_write =      afs_linux_prepare_write,
1805 };
1806 #endif
1807
1808
1809 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1810  * by what sort of operation is allowed.....
1811  */
1812
1813 static struct inode_operations afs_dir_iops = {
1814 #if !defined(AFS_LINUX24_ENV)
1815   .default_file_ops =   &afs_dir_fops,
1816 #else
1817   .setattr =            afs_notify_change,
1818 #endif
1819   .create =             afs_linux_create,
1820   .lookup =             afs_linux_lookup,
1821   .link =               afs_linux_link,
1822   .unlink =             afs_linux_unlink,
1823   .symlink =            afs_linux_symlink,
1824   .mkdir =              afs_linux_mkdir,
1825   .rmdir =              afs_linux_rmdir,
1826   .rename =             afs_linux_rename,
1827 #if defined(AFS_LINUX26_ENV)
1828   .getattr =            afs_linux_getattr,
1829 #else
1830   .revalidate =         afs_linux_revalidate,
1831 #endif
1832   .permission =         afs_linux_permission,
1833 };
1834
1835 /* We really need a separate symlink set of ops, since do_follow_link()
1836  * determines if it _is_ a link by checking if the follow_link op is set.
1837  */
1838 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1839 static int
1840 afs_symlink_filler(struct file *file, struct page *page)
1841 {
1842     struct inode *ip = (struct inode *)page->mapping->host;
1843     char *p = (char *)kmap(page);
1844     int code;
1845
1846     lock_kernel();
1847     AFS_GLOCK();
1848     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1849     AFS_GUNLOCK();
1850
1851     if (code < 0)
1852         goto fail;
1853     p[code] = '\0';             /* null terminate? */
1854     unlock_kernel();
1855
1856     SetPageUptodate(page);
1857     kunmap(page);
1858     UnlockPage(page);
1859     return 0;
1860
1861   fail:
1862     unlock_kernel();
1863
1864     SetPageError(page);
1865     kunmap(page);
1866     UnlockPage(page);
1867     return code;
1868 }
1869
1870 static struct address_space_operations afs_symlink_aops = {
1871   .readpage =   afs_symlink_filler
1872 };
1873 #endif  /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1874
1875 static struct inode_operations afs_symlink_iops = {
1876 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1877   .readlink =           page_readlink,
1878 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1879   .follow_link =        page_follow_link,
1880 #else
1881   .follow_link =        page_follow_link_light,
1882   .put_link =           page_put_link,
1883 #endif
1884 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
1885   .readlink =           afs_linux_readlink,
1886   .follow_link =        afs_linux_follow_link,
1887 #if !defined(AFS_LINUX24_ENV)
1888   .permission =         afs_linux_permission,
1889   .revalidate =         afs_linux_revalidate,
1890 #endif
1891 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1892 #if defined(AFS_LINUX24_ENV)
1893   .setattr =            afs_notify_change,
1894 #endif
1895 };
1896
1897 void
1898 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1899 {
1900         
1901     if (vattr)
1902         vattr2inode(ip, vattr);
1903
1904 /* Reset ops if symlink or directory. */
1905     if (S_ISREG(ip->i_mode)) {
1906         ip->i_op = &afs_file_iops;
1907 #if defined(AFS_LINUX24_ENV)
1908         ip->i_fop = &afs_file_fops;
1909         ip->i_data.a_ops = &afs_file_aops;
1910 #endif
1911
1912     } else if (S_ISDIR(ip->i_mode)) {
1913         ip->i_op = &afs_dir_iops;
1914 #if defined(AFS_LINUX24_ENV)
1915         ip->i_fop = &afs_dir_fops;
1916 #endif
1917
1918     } else if (S_ISLNK(ip->i_mode)) {
1919         ip->i_op = &afs_symlink_iops;
1920 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1921         ip->i_data.a_ops = &afs_symlink_aops;
1922         ip->i_mapping = &ip->i_data;
1923 #endif
1924     }
1925
1926 }