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