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