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