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