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