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