linux-updates-20060309
[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 #endif
41
42 #ifdef pgoff2loff
43 #define pageoff(pp) pgoff2loff((pp)->index)
44 #else
45 #define pageoff(pp) pp->offset
46 #endif
47
48 #if defined(AFS_LINUX26_ENV)
49 #define UnlockPage(pp) unlock_page(pp)
50 #endif
51
52 extern struct vcache *afs_globalVp;
53
54
55 static ssize_t
56 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
57 {
58     ssize_t code;
59     struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
60     cred_t *credp = crref();
61     struct vrequest treq;
62
63     AFS_GLOCK();
64     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
65                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
66                99999);
67
68     /* get a validated vcache entry */
69     code = afs_InitReq(&treq, credp);
70     if (!code)
71         code = afs_VerifyVCache(vcp, &treq);
72
73     if (code)
74         code = -code;
75     else {
76             osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
77             AFS_GUNLOCK();
78             code = generic_file_read(fp, buf, count, offp);
79             AFS_GLOCK();
80     }
81
82     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
83                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
84                code);
85
86     AFS_GUNLOCK();
87     crfree(credp);
88     return code;
89 }
90
91
92 /* Now we have integrated VM for writes as well as reads. generic_file_write
93  * also takes care of re-positioning the pointer if file is open in append
94  * mode. Call fake open/close to ensure we do writes of core dumps.
95  */
96 static ssize_t
97 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
98 {
99     ssize_t code = 0;
100     int code2 = 0;
101     struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
102     struct vrequest treq;
103     cred_t *credp = crref();
104
105     AFS_GLOCK();
106
107     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
108                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
109                (fp->f_flags & O_APPEND) ? 99998 : 99999);
110
111
112     /* get a validated vcache entry */
113     code = (ssize_t) afs_InitReq(&treq, credp);
114     if (!code)
115         code = (ssize_t) afs_VerifyVCache(vcp, &treq);
116
117     ObtainWriteLock(&vcp->lock, 529);
118     afs_FakeOpen(vcp);
119     ReleaseWriteLock(&vcp->lock);
120     if (code)
121         code = -code;
122     else {
123             AFS_GUNLOCK();
124             code = generic_file_write(fp, buf, count, offp);
125             AFS_GLOCK();
126     }
127
128     ObtainWriteLock(&vcp->lock, 530);
129     afs_FakeClose(vcp, credp);
130     ReleaseWriteLock(&vcp->lock);
131
132     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
133                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
134                code);
135
136     AFS_GUNLOCK();
137     crfree(credp);
138     return code;
139 }
140
141 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
142
143 /* This is a complete rewrite of afs_readdir, since we can make use of
144  * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
145  * handling and use of bulkstats will need to be reflected here as well.
146  */
147 static int
148 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
149 {
150     extern struct DirEntry *afs_dir_GetBlob();
151     struct vcache *avc = VTOAFS(FILE_INODE(fp));
152     struct vrequest treq;
153     register struct dcache *tdc;
154     int code;
155     int offset;
156     int dirpos;
157     struct DirEntry *de;
158     ino_t ino;
159     int len;
160     afs_size_t origOffset, tlen;
161     cred_t *credp = crref();
162     struct afs_fakestat_state fakestat;
163
164 #if defined(AFS_LINUX26_ENV)
165     lock_kernel();
166 #endif
167     AFS_GLOCK();
168     AFS_STATCNT(afs_readdir);
169
170     code = afs_InitReq(&treq, credp);
171     crfree(credp);
172     if (code)
173         goto out1;
174
175     afs_InitFakeStat(&fakestat);
176     code = afs_EvalFakeStat(&avc, &fakestat, &treq);
177     if (code)
178         goto out;
179
180     /* update the cache entry */
181   tagain:
182     code = afs_VerifyVCache(avc, &treq);
183     if (code)
184         goto out;
185
186     /* get a reference to the entire directory */
187     tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
188     len = tlen;
189     if (!tdc) {
190         code = -ENOENT;
191         goto out;
192     }
193     ObtainReadLock(&avc->lock);
194     ObtainReadLock(&tdc->lock);
195     /*
196      * Make sure that the data in the cache is current. There are two
197      * cases we need to worry about:
198      * 1. The cache data is being fetched by another process.
199      * 2. The cache data is no longer valid
200      */
201     while ((avc->states & CStatd)
202            && (tdc->dflags & DFFetching)
203            && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
204         ReleaseReadLock(&tdc->lock);
205         ReleaseReadLock(&avc->lock);
206         afs_osi_Sleep(&tdc->validPos);
207         ObtainReadLock(&avc->lock);
208         ObtainReadLock(&tdc->lock);
209     }
210     if (!(avc->states & CStatd)
211         || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
212         ReleaseReadLock(&tdc->lock);
213         ReleaseReadLock(&avc->lock);
214         afs_PutDCache(tdc);
215         goto tagain;
216     }
217
218     /* Fill in until we get an error or we're done. This implementation
219      * takes an offset in units of blobs, rather than bytes.
220      */
221     code = 0;
222     offset = (int) fp->f_pos;
223     while (1) {
224         dirpos = BlobScan(tdc, offset);
225         if (!dirpos)
226             break;
227
228         de = afs_dir_GetBlob(tdc, dirpos);
229         if (!de)
230             break;
231
232         ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
233         ino &= 0x7fffffff;      /* Assumes 32 bit ino_t ..... */
234         if (de->name)
235             len = strlen(de->name);
236         else {
237             printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n", 
238                    (unsigned long)&tdc->f.inode, dirpos);
239             DRelease((struct buffer *) de, 0);
240             afs_PutDCache(tdc);
241             ReleaseReadLock(&avc->lock);
242             code = -ENOENT;
243             goto out;
244         }
245
246         /* filldir returns -EINVAL when the buffer is full. */
247 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
248         {
249             unsigned int type = DT_UNKNOWN;
250             struct VenusFid afid;
251             struct vcache *tvc;
252             int vtype;
253             afid.Cell = avc->fid.Cell;
254             afid.Fid.Volume = avc->fid.Fid.Volume;
255             afid.Fid.Vnode = ntohl(de->fid.vnode);
256             afid.Fid.Unique = ntohl(de->fid.vunique);
257             if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
258                 type = DT_DIR;
259             } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
260                 if (tvc->mvstat) {
261                     type = DT_DIR;
262                 } else if (((tvc->states) & (CStatd | CTruth))) {
263                     /* CTruth will be set if the object has
264                      *ever* been statd */
265                     vtype = vType(tvc);
266                     if (vtype == VDIR)
267                         type = DT_DIR;
268                     else if (vtype == VREG)
269                         type = DT_REG;
270                     /* Don't do this until we're sure it can't be a mtpt */
271                     /* else if (vtype == VLNK)
272                      * type=DT_LNK; */
273                     /* what other types does AFS support? */
274                 }
275                 /* clean up from afs_FindVCache */
276                 afs_PutVCache(tvc);
277             }
278             code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
279         }
280 #else
281         code = (*filldir) (dirbuf, de->name, len, offset, ino);
282 #endif
283         DRelease((struct buffer *)de, 0);
284         if (code)
285             break;
286         offset = dirpos + 1 + ((len + 16) >> 5);
287     }
288     /* If filldir didn't fill in the last one this is still pointing to that
289      * last attempt.
290      */
291     fp->f_pos = (loff_t) offset;
292
293     ReleaseReadLock(&tdc->lock);
294     afs_PutDCache(tdc);
295     ReleaseReadLock(&avc->lock);
296     code = 0;
297
298 out:
299     afs_PutFakeStat(&fakestat);
300 out1:
301     AFS_GUNLOCK();
302 #if defined(AFS_LINUX26_ENV)
303     unlock_kernel();
304 #endif
305     return code;
306 }
307
308
309 /* in afs_pioctl.c */
310 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
311                       unsigned long arg);
312
313 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
314 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
315                                unsigned long arg) {
316     return afs_xioctl(FILE_INODE(fp), fp, com, arg);
317
318 }
319 #endif
320
321
322 static int
323 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
324 {
325     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
326     cred_t *credp = crref();
327     struct vrequest treq;
328     int code;
329
330     AFS_GLOCK();
331 #if defined(AFS_LINUX24_ENV)
332     afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
333                ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
334                vmap->vm_end - vmap->vm_start);
335 #else
336     afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
337                ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
338                vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
339                vmap->vm_offset);
340 #endif
341
342     /* get a validated vcache entry */
343     code = afs_InitReq(&treq, credp);
344     if (code)
345         goto out_err;
346
347     code = afs_VerifyVCache(vcp, &treq);
348     if (code)
349         goto out_err;
350
351     osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
352
353     AFS_GUNLOCK();
354     code = generic_file_mmap(fp, vmap);
355     AFS_GLOCK();
356     if (!code)
357         vcp->states |= CMAPPED;
358
359 out:
360     AFS_GUNLOCK();
361     crfree(credp);
362     return code;
363
364 out_err:
365     code = -code;
366     goto out;
367 }
368
369 static int
370 afs_linux_open(struct inode *ip, struct file *fp)
371 {
372     struct vcache *vcp = VTOAFS(ip);
373     cred_t *credp = crref();
374     int code;
375
376 #ifdef AFS_LINUX24_ENV
377     lock_kernel();
378 #endif
379     AFS_GLOCK();
380     code = afs_open(&vcp, fp->f_flags, credp);
381     AFS_GUNLOCK();
382 #ifdef AFS_LINUX24_ENV
383     unlock_kernel();
384 #endif
385
386     crfree(credp);
387     return -code;
388 }
389
390 static int
391 afs_linux_release(struct inode *ip, struct file *fp)
392 {
393     struct vcache *vcp = VTOAFS(ip);
394     cred_t *credp = crref();
395     int code = 0;
396
397 #ifdef AFS_LINUX24_ENV
398     lock_kernel();
399 #endif
400     AFS_GLOCK();
401     code = afs_close(vcp, fp->f_flags, credp);
402     AFS_GUNLOCK();
403 #ifdef AFS_LINUX24_ENV
404     unlock_kernel();
405 #endif
406
407     crfree(credp);
408     return -code;
409 }
410
411 static int
412 #if defined(AFS_LINUX24_ENV)
413 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
414 #else
415 afs_linux_fsync(struct file *fp, struct dentry *dp)
416 #endif
417 {
418     int code;
419     struct inode *ip = FILE_INODE(fp);
420     cred_t *credp = crref();
421
422 #ifdef AFS_LINUX24_ENV
423     lock_kernel();
424 #endif
425     AFS_GLOCK();
426     code = afs_fsync(VTOAFS(ip), credp);
427     AFS_GUNLOCK();
428 #ifdef AFS_LINUX24_ENV
429     unlock_kernel();
430 #endif
431     crfree(credp);
432     return -code;
433
434 }
435
436
437 static int
438 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
439 {
440     int code = 0;
441     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
442     cred_t *credp = crref();
443     struct AFS_FLOCK flock;
444     /* Convert to a lock format afs_lockctl understands. */
445     memset((char *)&flock, 0, sizeof(flock));
446     flock.l_type = flp->fl_type;
447     flock.l_pid = flp->fl_pid;
448     flock.l_whence = 0;
449     flock.l_start = flp->fl_start;
450     flock.l_len = flp->fl_end - flp->fl_start;
451
452     /* Safe because there are no large files, yet */
453 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
454     if (cmd == F_GETLK64)
455         cmd = F_GETLK;
456     else if (cmd == F_SETLK64)
457         cmd = F_SETLK;
458     else if (cmd == F_SETLKW64)
459         cmd = F_SETLKW;
460 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
461
462     AFS_GLOCK();
463     code = afs_lockctl(vcp, &flock, cmd, credp);
464     AFS_GUNLOCK();
465
466     /* Convert flock back to Linux's file_lock */
467     flp->fl_type = flock.l_type;
468     flp->fl_pid = flock.l_pid;
469     flp->fl_start = flock.l_start;
470     flp->fl_end = flock.l_start + flock.l_len;
471
472     crfree(credp);
473     return -code;
474
475 }
476
477 /* afs_linux_flush
478  * essentially the same as afs_fsync() but we need to get the return
479  * code for the sys_close() here, not afs_linux_release(), so call
480  * afs_StoreAllSegments() with AFS_LASTSTORE
481  */
482 static int
483 afs_linux_flush(struct file *fp)
484 {
485     struct vrequest treq;
486     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
487     cred_t *credp = crref();
488     int code;
489
490     AFS_GLOCK();
491
492     code = afs_InitReq(&treq, credp);
493     if (code)
494         goto out;
495
496     ObtainSharedLock(&vcp->lock, 535);
497     if (vcp->execsOrWriters > 0) {
498         UpgradeSToWLock(&vcp->lock, 536);
499         code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
500         ConvertWToSLock(&vcp->lock);
501     }
502     code = afs_CheckCode(code, &treq, 54);
503     ReleaseSharedLock(&vcp->lock);
504
505 out:
506     AFS_GUNLOCK();
507
508     crfree(credp);
509     return -code;
510 }
511
512 #if !defined(AFS_LINUX24_ENV)
513 /* Not allowed to directly read a directory. */
514 ssize_t
515 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
516 {
517     return -EISDIR;
518 }
519 #endif
520
521
522
523 struct file_operations afs_dir_fops = {
524 #if !defined(AFS_LINUX24_ENV)
525   .read =       afs_linux_dir_read,
526   .lock =       afs_linux_lock,
527   .fsync =      afs_linux_fsync,
528 #else
529   .read =       generic_read_dir,
530 #endif
531   .readdir =    afs_linux_readdir,
532 #ifdef HAVE_UNLOCKED_IOCTL
533   .unlocked_ioctl = afs_unlocked_xioctl,
534 #else
535   .ioctl =      afs_xioctl,
536 #endif
537 #ifdef HAVE_COMPAT_IOCTL
538   .compat_ioctl = afs_unlocked_xioctl,
539 #endif
540   .open =       afs_linux_open,
541   .release =    afs_linux_release,
542 };
543
544 struct file_operations afs_file_fops = {
545   .read =       afs_linux_read,
546   .write =      afs_linux_write,
547 #ifdef HAVE_UNLOCKED_IOCTL
548   .unlocked_ioctl = afs_unlocked_xioctl,
549 #else
550   .ioctl =      afs_xioctl,
551 #endif
552 #ifdef HAVE_COMPAT_IOCTL
553   .compat_ioctl = afs_unlocked_xioctl,
554 #endif
555   .mmap =       afs_linux_mmap,
556   .open =       afs_linux_open,
557   .flush =      afs_linux_flush,
558 #ifdef AFS_LINUX26_ENV
559   .sendfile =   generic_file_sendfile,
560 #endif
561   .release =    afs_linux_release,
562   .fsync =      afs_linux_fsync,
563   .lock =       afs_linux_lock,
564 };
565
566
567 /**********************************************************************
568  * AFS Linux dentry operations
569  **********************************************************************/
570
571 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
572  * that has its mvid (parent dir's fid) pointer set to the wrong directory
573  * due to being mounted in multiple points at once. If so, check_bad_parent()
574  * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
575  * dotdotfid and mtpoint fid members.
576  * Parameters:
577  *   dp - dentry to be checked.
578  * Return Values:
579  *   None.
580  * Sideeffects:
581  *   This dentry's vcache's mvid will be set to the correct parent directory's
582  *   fid.
583  *   This root vnode's volume will have its dotdotfid and mtpoint fids set
584  *   to the correct parent and mountpoint fids.
585  */
586
587 static inline void
588 check_bad_parent(struct dentry *dp)
589 {
590     cred_t *credp;
591     struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
592     struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
593
594     if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
595         credp = crref();
596
597         /* force a lookup, so vcp->mvid is fixed up */
598         afs_lookup(pvc, dp->d_name.name, &avc, credp);
599         if (!avc || vcp != avc) {       /* bad, very bad.. */
600             afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
601                        "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
602                        ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
603                        ICL_TYPE_POINTER, dp);
604         }
605         if (avc)
606             AFS_RELE(AFSTOV(avc));
607         crfree(credp);
608     }
609
610     return;
611 }
612
613 /* afs_linux_revalidate
614  * Ensure vcache is stat'd before use. Return 0 if entry is valid.
615  */
616 static int
617 afs_linux_revalidate(struct dentry *dp)
618 {
619     struct vattr vattr;
620     struct vcache *vcp = VTOAFS(dp->d_inode);
621     cred_t *credp;
622     int code;
623
624 #ifdef AFS_LINUX24_ENV
625     lock_kernel();
626 #endif
627     AFS_GLOCK();
628
629 #ifdef notyet
630     /* Make this a fast path (no crref), since it's called so often. */
631     if (vcp->states & CStatd) {
632
633         if (*dp->d_name.name != '/' && vcp->mvstat == 2)        /* root vnode */
634             check_bad_parent(dp);       /* check and correct mvid */
635
636         AFS_GUNLOCK();
637 #ifdef AFS_LINUX24_ENV
638         unlock_kernel();
639 #endif
640         return 0;
641     }
642 #endif
643
644     credp = crref();
645     code = afs_getattr(vcp, &vattr, credp);
646     if (!code)
647         vattr2inode(AFSTOV(vcp), &vattr);
648
649     AFS_GUNLOCK();
650 #ifdef AFS_LINUX24_ENV
651     unlock_kernel();
652 #endif
653     crfree(credp);
654
655     return -code;
656 }
657
658 #if defined(AFS_LINUX26_ENV)
659 static int
660 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
661 {
662         int err = afs_linux_revalidate(dentry);
663         if (!err) {
664                 generic_fillattr(dentry->d_inode, stat);
665 }
666         return err;
667 }
668 #endif
669
670 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
671  * In kernels 2.2.10 and above, we are passed an additional flags var which
672  * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
673  * we are advised to follow the entry if it is a link or to make sure that 
674  * it is a directory. But since the kernel itself checks these possibilities
675  * later on, we shouldn't have to do it until later. Perhaps in the future..
676  */
677 static int
678 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
679 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
680 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
681 #else
682 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
683 #endif
684 #else
685 afs_linux_dentry_revalidate(struct dentry *dp)
686 #endif
687 {
688     struct vattr vattr;
689     cred_t *credp = NULL;
690     struct vcache *vcp, *pvcp, *tvc = NULL;
691     int valid;
692
693 #ifdef AFS_LINUX24_ENV
694     lock_kernel();
695 #endif
696     AFS_GLOCK();
697
698     if (dp->d_inode) {
699
700         vcp = VTOAFS(dp->d_inode);
701         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
702
703         if (vcp == afs_globalVp)
704             goto good_dentry;
705
706         if (*dp->d_name.name != '/' && vcp->mvstat == 2)        /* root vnode */
707             check_bad_parent(dp);       /* check and correct mvid */
708
709 #ifdef notdef
710         /* If the last looker changes, we should make sure the current
711          * looker still has permission to examine this file.  This would
712          * always require a crref() which would be "slow".
713          */
714         if (vcp->last_looker != treq.uid) {
715             if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
716                 goto bad_dentry;
717
718             vcp->last_looker = treq.uid;
719         }
720 #endif
721
722         /* If the parent's DataVersion has changed or the vnode
723          * is longer valid, we need to do a full lookup.  VerifyVCache
724          * isn't enough since the vnode may have been renamed.
725          */
726
727         if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
728
729             credp = crref();
730             afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
731             if (!tvc || tvc != vcp)
732                 goto bad_dentry;
733
734             if (afs_getattr(vcp, &vattr, credp))
735                 goto bad_dentry;
736
737             vattr2inode(AFSTOV(vcp), &vattr);
738             dp->d_time = hgetlo(pvcp->m.DataVersion);
739         }
740
741         /* should we always update the attributes at this point? */
742         /* unlikely--the vcache entry hasn't changed */
743
744     } else {
745 #ifdef notyet
746         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
747         if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
748             goto bad_dentry;
749 #endif
750
751         /* No change in parent's DataVersion so this negative
752          * lookup is still valid.  BUT, if a server is down a
753          * negative lookup can result so there should be a
754          * liftime as well.  For now, always expire.
755          */
756
757         goto bad_dentry;
758     }
759
760   good_dentry:
761     valid = 1;
762
763   done:
764     /* Clean up */
765     if (tvc)
766         afs_PutVCache(tvc);
767     AFS_GUNLOCK();
768     if (credp)
769         crfree(credp);
770
771     if (!valid) {
772         shrink_dcache_parent(dp);
773         d_drop(dp);
774     }
775 #ifdef AFS_LINUX24_ENV
776     unlock_kernel();
777 #endif
778     return valid;
779
780   bad_dentry:
781     valid = 0;
782     goto done;
783 }
784
785 static void
786 afs_dentry_iput(struct dentry *dp, struct inode *ip)
787 {
788     struct vcache *vcp = VTOAFS(ip);
789
790     AFS_GLOCK();
791     if (vcp->states & CUnlinked)
792         (void) afs_InactiveVCache(vcp, NULL);
793     AFS_GUNLOCK();
794
795     iput(ip);
796 }
797
798 static int
799 afs_dentry_delete(struct dentry *dp)
800 {
801     if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
802         return 1;               /* bad inode? */
803
804     return 0;
805 }
806
807 struct dentry_operations afs_dentry_operations = {
808   .d_revalidate =       afs_linux_dentry_revalidate,
809   .d_delete =           afs_dentry_delete,
810   .d_iput =             afs_dentry_iput,
811 };
812
813 /**********************************************************************
814  * AFS Linux inode operations
815  **********************************************************************/
816
817 /* afs_linux_create
818  *
819  * Merely need to set enough of vattr to get us through the create. Note
820  * that the higher level code (open_namei) will take care of any tuncation
821  * explicitly. Exclusive open is also taken care of in open_namei.
822  *
823  * name is in kernel space at this point.
824  */
825 static int
826 #ifdef IOP_CREATE_TAKES_NAMEIDATA
827 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
828                  struct nameidata *nd)
829 #else
830 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
831 #endif
832 {
833     struct vattr vattr;
834     cred_t *credp = crref();
835     const char *name = dp->d_name.name;
836     struct vcache *vcp;
837     int code;
838
839     VATTR_NULL(&vattr);
840     vattr.va_mode = mode;
841     vattr.va_type = mode & S_IFMT;
842
843 #if defined(AFS_LINUX26_ENV)
844     lock_kernel();
845 #endif
846     AFS_GLOCK();
847     code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
848                       &vcp, credp);
849
850     if (!code) {
851         struct inode *ip = AFSTOV(vcp);
852
853         afs_getattr(vcp, &vattr, credp);
854         afs_fill_inode(ip, &vattr);
855         dp->d_op = &afs_dentry_operations;
856         dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
857         d_instantiate(dp, ip);
858     }
859     AFS_GUNLOCK();
860
861 #if defined(AFS_LINUX26_ENV)
862     unlock_kernel();
863 #endif
864     crfree(credp);
865     return -code;
866 }
867
868 /* afs_linux_lookup */
869 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
870 static struct dentry *
871 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
872 afs_linux_lookup(struct inode *dip, struct dentry *dp,
873                  struct nameidata *nd)
874 #else
875 afs_linux_lookup(struct inode *dip, struct dentry *dp)
876 #endif
877 #else
878 static int
879 afs_linux_lookup(struct inode *dip, struct dentry *dp)
880 #endif
881 {
882     cred_t *credp = crref();
883     struct vcache *vcp = NULL;
884     const char *comp = dp->d_name.name;
885     struct inode *ip = NULL;
886     int code;
887
888 #if defined(AFS_LINUX26_ENV)
889     lock_kernel();
890 #endif
891     AFS_GLOCK();
892     code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
893     
894     if (vcp) {
895         struct vattr vattr;
896
897         ip = AFSTOV(vcp);
898         afs_getattr(vcp, &vattr, credp);
899         afs_fill_inode(ip, &vattr);
900     }
901     dp->d_op = &afs_dentry_operations;
902     dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
903     AFS_GUNLOCK();
904
905 #if defined(AFS_LINUX24_ENV)
906     if (ip && S_ISDIR(ip->i_mode)) {
907         struct dentry *alias;
908
909         alias = d_find_alias(ip);
910         if (alias) {
911             if (d_invalidate(alias) == 0) {
912                 dput(alias);
913             } else {
914                 iput(ip);
915 #if defined(AFS_LINUX26_ENV)
916                 unlock_kernel();
917 #endif
918                 return alias;
919             }
920         }
921     }
922 #endif
923     d_add(dp, ip);
924
925 #if defined(AFS_LINUX26_ENV)
926     unlock_kernel();
927 #endif
928     crfree(credp);
929
930     /* It's ok for the file to not be found. That's noted by the caller by
931      * seeing that the dp->d_inode field is NULL.
932      */
933 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
934     if (code == ENOENT)
935         return ERR_PTR(0);
936     else
937         return ERR_PTR(-code);
938 #else
939     if (code == ENOENT)
940         code = 0;
941     return -code;
942 #endif
943 }
944
945 static int
946 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
947 {
948     int code;
949     cred_t *credp = crref();
950     const char *name = newdp->d_name.name;
951     struct inode *oldip = olddp->d_inode;
952
953     /* If afs_link returned the vnode, we could instantiate the
954      * dentry. Since it's not, we drop this one and do a new lookup.
955      */
956     d_drop(newdp);
957
958     AFS_GLOCK();
959     code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
960
961     AFS_GUNLOCK();
962     crfree(credp);
963     return -code;
964 }
965
966 static int
967 afs_linux_unlink(struct inode *dip, struct dentry *dp)
968 {
969     int code = EBUSY;
970     cred_t *credp = crref();
971     const char *name = dp->d_name.name;
972     struct vcache *tvc = VTOAFS(dp->d_inode);
973
974 #if defined(AFS_LINUX26_ENV)
975     lock_kernel();
976 #endif
977     if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
978                                 && !(tvc->states & CUnlinked)) {
979         struct dentry *__dp;
980         char *__name;
981         extern char *afs_newname();
982
983         __dp = NULL;
984         __name = NULL;
985         do {
986             dput(__dp);
987
988             AFS_GLOCK();
989             if (__name)
990                 osi_FreeSmallSpace(__name);
991             __name = afs_newname();
992             AFS_GUNLOCK();
993
994             __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
995                 
996             if (IS_ERR(__dp))
997                 goto out;
998         } while (__dp->d_inode != NULL);
999
1000         AFS_GLOCK();
1001         code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1002         if (!code) {
1003             tvc->mvid = (void *) __name;
1004             crhold(credp);
1005             if (tvc->uncred) {
1006                 crfree(tvc->uncred);
1007             }
1008             tvc->uncred = credp;
1009             tvc->states |= CUnlinked;
1010         }
1011         AFS_GUNLOCK();
1012
1013         if (!code) {
1014             __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1015             d_move(dp, __dp);
1016         }
1017         dput(__dp);
1018
1019         goto out;
1020     }
1021
1022     AFS_GLOCK();
1023     code = afs_remove(VTOAFS(dip), name, credp);
1024     AFS_GUNLOCK();
1025     if (!code)
1026         d_drop(dp);
1027 out:
1028 #if defined(AFS_LINUX26_ENV)
1029     unlock_kernel();
1030 #endif
1031     crfree(credp);
1032     return -code;
1033 }
1034
1035
1036 static int
1037 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1038 {
1039     int code;
1040     cred_t *credp = crref();
1041     struct vattr vattr;
1042     const char *name = dp->d_name.name;
1043
1044     /* If afs_symlink returned the vnode, we could instantiate the
1045      * dentry. Since it's not, we drop this one and do a new lookup.
1046      */
1047     d_drop(dp);
1048
1049     VATTR_NULL(&vattr);
1050     AFS_GLOCK();
1051     code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1052     AFS_GUNLOCK();
1053     crfree(credp);
1054     return -code;
1055 }
1056
1057 static int
1058 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1059 {
1060     int code;
1061     cred_t *credp = crref();
1062     struct vcache *tvcp = NULL;
1063     struct vattr vattr;
1064     const char *name = dp->d_name.name;
1065
1066 #if defined(AFS_LINUX26_ENV)
1067     lock_kernel();
1068 #endif
1069     VATTR_NULL(&vattr);
1070     vattr.va_mask = ATTR_MODE;
1071     vattr.va_mode = mode;
1072     AFS_GLOCK();
1073     code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1074
1075     if (tvcp) {
1076         struct inode *ip = AFSTOV(tvcp);
1077
1078         afs_getattr(tvcp, &vattr, credp);
1079         afs_fill_inode(ip, &vattr);
1080
1081         dp->d_op = &afs_dentry_operations;
1082         dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1083         d_instantiate(dp, ip);
1084     }
1085     AFS_GUNLOCK();
1086
1087 #if defined(AFS_LINUX26_ENV)
1088     unlock_kernel();
1089 #endif
1090     crfree(credp);
1091     return -code;
1092 }
1093
1094 static int
1095 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1096 {
1097     int code;
1098     cred_t *credp = crref();
1099     const char *name = dp->d_name.name;
1100
1101     /* locking kernel conflicts with glock? */
1102
1103     AFS_GLOCK();
1104     code = afs_rmdir(VTOAFS(dip), name, credp);
1105     AFS_GUNLOCK();
1106
1107     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1108      * that failed because a directory is not empty. So, we map
1109      * EEXIST to ENOTEMPTY on linux.
1110      */
1111     if (code == EEXIST) {
1112         code = ENOTEMPTY;
1113     }
1114
1115     if (!code) {
1116         d_drop(dp);
1117     }
1118
1119     crfree(credp);
1120     return -code;
1121 }
1122
1123
1124 static int
1125 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1126                  struct inode *newip, struct dentry *newdp)
1127 {
1128     int code;
1129     cred_t *credp = crref();
1130     const char *oldname = olddp->d_name.name;
1131     const char *newname = newdp->d_name.name;
1132     struct dentry *rehash = NULL;
1133
1134 #if defined(AFS_LINUX26_ENV)
1135     /* Prevent any new references during rename operation. */
1136     lock_kernel();
1137 #endif
1138     /* Remove old and new entries from name hash. New one will change below.
1139      * While it's optimal to catch failures and re-insert newdp into hash,
1140      * it's also error prone and in that case we're already dealing with error
1141      * cases. Let another lookup put things right, if need be.
1142      */
1143 #if defined(AFS_LINUX26_ENV)
1144     if (!d_unhashed(newdp)) {
1145         d_drop(newdp);
1146         rehash = newdp;
1147     }
1148 #else
1149     if (!list_empty(&newdp->d_hash)) {
1150         d_drop(newdp);
1151         rehash = newdp;
1152     }
1153 #endif
1154
1155 #if defined(AFS_LINUX24_ENV)
1156     if (atomic_read(&olddp->d_count) > 1)
1157         shrink_dcache_parent(olddp);
1158 #endif
1159
1160     AFS_GLOCK();
1161     code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1162     AFS_GUNLOCK();
1163
1164     if (rehash)
1165         d_rehash(rehash);
1166
1167 #if defined(AFS_LINUX26_ENV)
1168     unlock_kernel();
1169 #endif
1170
1171     crfree(credp);
1172     return -code;
1173 }
1174
1175
1176 /* afs_linux_ireadlink 
1177  * Internal readlink which can return link contents to user or kernel space.
1178  * Note that the buffer is NOT supposed to be null-terminated.
1179  */
1180 static int
1181 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1182 {
1183     int code;
1184     cred_t *credp = crref();
1185     uio_t tuio;
1186     struct iovec iov;
1187
1188     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1189     code = afs_readlink(VTOAFS(ip), &tuio, credp);
1190     crfree(credp);
1191
1192     if (!code)
1193         return maxlen - tuio.uio_resid;
1194     else
1195         return -code;
1196 }
1197
1198 #if !defined(AFS_LINUX24_ENV)
1199 /* afs_linux_readlink 
1200  * Fill target (which is in user space) with contents of symlink.
1201  */
1202 static int
1203 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1204 {
1205     int code;
1206     struct inode *ip = dp->d_inode;
1207
1208     AFS_GLOCK();
1209     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1210     AFS_GUNLOCK();
1211     return code;
1212 }
1213
1214
1215 /* afs_linux_follow_link
1216  * a file system dependent link following routine.
1217  */
1218 static struct dentry *
1219 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1220                       unsigned int follow)
1221 {
1222     int code = 0;
1223     char *name;
1224     struct dentry *res;
1225
1226
1227     AFS_GLOCK();
1228     name = osi_Alloc(PATH_MAX + 1);
1229     if (!name) {
1230         AFS_GUNLOCK();
1231         dput(basep);
1232         return ERR_PTR(-EIO);
1233     }
1234
1235     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1236     AFS_GUNLOCK();
1237
1238     if (code < 0) {
1239         dput(basep);
1240         res = ERR_PTR(code);
1241     } else {
1242         name[code] = '\0';
1243         res = lookup_dentry(name, basep, follow);
1244     }
1245
1246     AFS_GLOCK();
1247     osi_Free(name, PATH_MAX + 1);
1248     AFS_GUNLOCK();
1249     return res;
1250 }
1251 #endif
1252
1253 /* afs_linux_readpage
1254  * all reads come through here. A strategy-like read call.
1255  */
1256 static int
1257 afs_linux_readpage(struct file *fp, struct page *pp)
1258 {
1259     int code;
1260     cred_t *credp = crref();
1261 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1262     char *address;
1263     afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1264 #else
1265     ulong address = afs_linux_page_address(pp);
1266     afs_offs_t offset = pageoff(pp);
1267 #endif
1268     uio_t tuio;
1269     struct iovec iovec;
1270     struct inode *ip = FILE_INODE(fp);
1271     int cnt = page_count(pp);
1272     struct vcache *avc = VTOAFS(ip);
1273
1274
1275 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1276     address = kmap(pp);
1277     ClearPageError(pp);
1278 #else
1279     atomic_add(1, &pp->count);
1280     set_bit(PG_locked, &pp->flags);     /* other bits? See mm.h */
1281     clear_bit(PG_error, &pp->flags);
1282 #endif
1283
1284     setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1285               AFS_UIOSYS);
1286 #ifdef AFS_LINUX24_ENV
1287     lock_kernel();
1288 #endif
1289     AFS_GLOCK();
1290     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 */
1291     code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1292     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1293                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1294                code);
1295     AFS_GUNLOCK();
1296 #ifdef AFS_LINUX24_ENV
1297     unlock_kernel();
1298 #endif
1299
1300     if (!code) {
1301         if (tuio.uio_resid)     /* zero remainder of page */
1302             memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1303                    tuio.uio_resid);
1304 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1305         flush_dcache_page(pp);
1306         SetPageUptodate(pp);
1307 #else
1308         set_bit(PG_uptodate, &pp->flags);
1309 #endif
1310     }
1311
1312 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1313     kunmap(pp);
1314     UnlockPage(pp);
1315 #else
1316     clear_bit(PG_locked, &pp->flags);
1317     wake_up(&pp->wait);
1318     free_page(address);
1319 #endif
1320
1321     if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1322         struct dcache *tdc;
1323         struct vrequest treq;
1324
1325         AFS_GLOCK();
1326         code = afs_InitReq(&treq, credp);
1327         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1328             tdc = afs_FindDCache(avc, offset);
1329             if (tdc) {
1330                 if (!(tdc->mflags & DFNextStarted))
1331                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1332                 afs_PutDCache(tdc);
1333             }
1334             ReleaseWriteLock(&avc->lock);
1335         }
1336         AFS_GUNLOCK();
1337     }
1338
1339     crfree(credp);
1340     return -code;
1341 }
1342
1343
1344 #if defined(AFS_LINUX24_ENV)
1345 static int
1346 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1347                          unsigned long offset, unsigned int count)
1348 {
1349     struct vcache *vcp = VTOAFS(ip);
1350     char *buffer;
1351     afs_offs_t base;
1352     int code = 0;
1353     cred_t *credp;
1354     uio_t tuio;
1355     struct iovec iovec;
1356     int f_flags = 0;
1357
1358     buffer = kmap(pp) + offset;
1359     base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT)  + offset;
1360
1361     credp = crref();
1362     lock_kernel();
1363     AFS_GLOCK();
1364     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1365                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1366                ICL_TYPE_INT32, 99999);
1367
1368     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1369
1370     code = afs_write(vcp, &tuio, f_flags, credp, 0);
1371
1372     ip->i_size = vcp->m.Length;
1373     ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1374
1375     if (!code) {
1376         struct vrequest treq;
1377
1378         ObtainWriteLock(&vcp->lock, 533);
1379         if (!afs_InitReq(&treq, credp))
1380             code = afs_DoPartialWrite(vcp, &treq);
1381         ReleaseWriteLock(&vcp->lock);
1382     }
1383     code = code ? -code : count - tuio.uio_resid;
1384
1385     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1386                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1387                ICL_TYPE_INT32, code);
1388
1389     AFS_GUNLOCK();
1390     unlock_kernel();
1391     crfree(credp);
1392     kunmap(pp);
1393
1394     return code;
1395 }
1396
1397
1398 static int
1399 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1400 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1401 #else
1402 afs_linux_writepage(struct page *pp)
1403 #endif
1404 {
1405     struct address_space *mapping = pp->mapping;
1406     struct inode *inode;
1407     unsigned long end_index;
1408     unsigned offset = PAGE_CACHE_SIZE;
1409     long status;
1410
1411 #if defined(AFS_LINUX26_ENV)
1412     if (PageReclaim(pp)) {
1413 # if defined(WRITEPAGE_ACTIVATE)
1414         return WRITEPAGE_ACTIVATE;
1415 # else 
1416         return AOP_WRITEPAGE_ACTIVATE;
1417 # endif
1418     }
1419 #else
1420     if (PageLaunder(pp)) {
1421         return(fail_writepage(pp));
1422     }
1423 #endif
1424
1425     inode = (struct inode *)mapping->host;
1426     end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1427
1428     /* easy case */
1429     if (pp->index < end_index)
1430         goto do_it;
1431     /* things got complicated... */
1432     offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1433     /* OK, are we completely out? */
1434     if (pp->index >= end_index + 1 || !offset)
1435         return -EIO;
1436   do_it:
1437     status = afs_linux_writepage_sync(inode, pp, 0, offset);
1438     SetPageUptodate(pp);
1439     UnlockPage(pp);
1440     if (status == offset)
1441         return 0;
1442     else
1443         return status;
1444 }
1445 #else
1446 /* afs_linux_updatepage
1447  * What one would have thought was writepage - write dirty page to file.
1448  * Called from generic_file_write. buffer is still in user space. pagep
1449  * has been filled in with old data if we're updating less than a page.
1450  */
1451 static int
1452 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1453                      unsigned int count, int sync)
1454 {
1455     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1456     u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1457     int code = 0;
1458     cred_t *credp;
1459     uio_t tuio;
1460     struct iovec iovec;
1461
1462     set_bit(PG_locked, &pp->flags);
1463
1464     credp = crref();
1465     AFS_GLOCK();
1466     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1467                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1468                ICL_TYPE_INT32, 99999);
1469     setup_uio(&tuio, &iovec, page_addr + offset,
1470               (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1471               AFS_UIOSYS);
1472
1473     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1474
1475     ip->i_size = vcp->m.Length;
1476     ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1477
1478     if (!code) {
1479         struct vrequest treq;
1480
1481         ObtainWriteLock(&vcp->lock, 533);
1482         vcp->m.Date = osi_Time();   /* set modification time */
1483         if (!afs_InitReq(&treq, credp))
1484             code = afs_DoPartialWrite(vcp, &treq);
1485         ReleaseWriteLock(&vcp->lock);
1486     }
1487
1488     code = code ? -code : count - tuio.uio_resid;
1489     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1490                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1491                ICL_TYPE_INT32, code);
1492
1493     AFS_GUNLOCK();
1494     crfree(credp);
1495
1496     clear_bit(PG_locked, &pp->flags);
1497     return code;
1498 }
1499 #endif
1500
1501 /* afs_linux_permission
1502  * Check access rights - returns error if can't check or permission denied.
1503  */
1504 static int
1505 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1506 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1507 #else
1508 afs_linux_permission(struct inode *ip, int mode)
1509 #endif
1510 {
1511     int code;
1512     cred_t *credp = crref();
1513     int tmp = 0;
1514
1515     AFS_GLOCK();
1516     if (mode & MAY_EXEC)
1517         tmp |= VEXEC;
1518     if (mode & MAY_READ)
1519         tmp |= VREAD;
1520     if (mode & MAY_WRITE)
1521         tmp |= VWRITE;
1522     code = afs_access(VTOAFS(ip), tmp, credp);
1523
1524     AFS_GUNLOCK();
1525     crfree(credp);
1526     return -code;
1527 }
1528
1529 #if defined(AFS_LINUX24_ENV)
1530 static int
1531 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1532                        unsigned to)
1533 {
1534     int code;
1535
1536     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1537                                     offset, to - offset);
1538 #if !defined(AFS_LINUX26_ENV)
1539     kunmap(page);
1540 #endif
1541
1542     return code;
1543 }
1544
1545 static int
1546 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1547                         unsigned to)
1548 {
1549 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1550    call kmap directly instead of relying on us to do it */
1551 #if !defined(AFS_LINUX26_ENV)
1552     kmap(page);
1553 #endif
1554     return 0;
1555 }
1556
1557 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1558 #endif
1559
1560 static struct inode_operations afs_file_iops = {
1561 #if defined(AFS_LINUX26_ENV)
1562   .permission =         afs_linux_permission,
1563   .getattr =            afs_linux_getattr,
1564   .setattr =            afs_notify_change,
1565 #elif defined(AFS_LINUX24_ENV)
1566   .permission =         afs_linux_permission,
1567   .revalidate =         afs_linux_revalidate,
1568   .setattr =            afs_notify_change,
1569 #else
1570   .default_file_ops =   &afs_file_fops,
1571   .readpage =           afs_linux_readpage,
1572   .revalidate =         afs_linux_revalidate,
1573   .updatepage =         afs_linux_updatepage,
1574 #endif
1575 };
1576
1577 #if defined(AFS_LINUX24_ENV)
1578 static struct address_space_operations afs_file_aops = {
1579   .readpage =           afs_linux_readpage,
1580   .writepage =          afs_linux_writepage,
1581   .commit_write =       afs_linux_commit_write,
1582   .prepare_write =      afs_linux_prepare_write,
1583 };
1584 #endif
1585
1586
1587 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1588  * by what sort of operation is allowed.....
1589  */
1590
1591 static struct inode_operations afs_dir_iops = {
1592 #if !defined(AFS_LINUX24_ENV)
1593   .default_file_ops =   &afs_dir_fops,
1594 #else
1595   .setattr =            afs_notify_change,
1596 #endif
1597   .create =             afs_linux_create,
1598   .lookup =             afs_linux_lookup,
1599   .link =               afs_linux_link,
1600   .unlink =             afs_linux_unlink,
1601   .symlink =            afs_linux_symlink,
1602   .mkdir =              afs_linux_mkdir,
1603   .rmdir =              afs_linux_rmdir,
1604   .rename =             afs_linux_rename,
1605 #if defined(AFS_LINUX26_ENV)
1606   .getattr =            afs_linux_getattr,
1607 #else
1608   .revalidate =         afs_linux_revalidate,
1609 #endif
1610   .permission =         afs_linux_permission,
1611 };
1612
1613 /* We really need a separate symlink set of ops, since do_follow_link()
1614  * determines if it _is_ a link by checking if the follow_link op is set.
1615  */
1616 #if defined(AFS_LINUX24_ENV)
1617 static int
1618 afs_symlink_filler(struct file *file, struct page *page)
1619 {
1620     struct inode *ip = (struct inode *)page->mapping->host;
1621     char *p = (char *)kmap(page);
1622     int code;
1623
1624     lock_kernel();
1625     AFS_GLOCK();
1626     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1627     AFS_GUNLOCK();
1628
1629     if (code < 0)
1630         goto fail;
1631     p[code] = '\0';             /* null terminate? */
1632     unlock_kernel();
1633
1634     SetPageUptodate(page);
1635     kunmap(page);
1636     UnlockPage(page);
1637     return 0;
1638
1639   fail:
1640     unlock_kernel();
1641
1642     SetPageError(page);
1643     kunmap(page);
1644     UnlockPage(page);
1645     return code;
1646 }
1647
1648 static struct address_space_operations afs_symlink_aops = {
1649   .readpage =   afs_symlink_filler
1650 };
1651 #endif
1652
1653 static struct inode_operations afs_symlink_iops = {
1654 #if defined(AFS_LINUX24_ENV)
1655   .readlink =           page_readlink,
1656 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1657   .follow_link =        page_follow_link,
1658 #else
1659   .follow_link =        page_follow_link_light,
1660   .put_link =           page_put_link,
1661 #endif
1662   .setattr =            afs_notify_change,
1663 #else
1664   .readlink =           afs_linux_readlink,
1665   .follow_link =        afs_linux_follow_link,
1666   .permission =         afs_linux_permission,
1667   .revalidate =         afs_linux_revalidate,
1668 #endif
1669 };
1670
1671 void
1672 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1673 {
1674         
1675     if (vattr)
1676         vattr2inode(ip, vattr);
1677
1678 /* Reset ops if symlink or directory. */
1679     if (S_ISREG(ip->i_mode)) {
1680         ip->i_op = &afs_file_iops;
1681 #if defined(AFS_LINUX24_ENV)
1682         ip->i_fop = &afs_file_fops;
1683         ip->i_data.a_ops = &afs_file_aops;
1684 #endif
1685
1686     } else if (S_ISDIR(ip->i_mode)) {
1687         ip->i_op = &afs_dir_iops;
1688 #if defined(AFS_LINUX24_ENV)
1689         ip->i_fop = &afs_dir_fops;
1690 #endif
1691
1692     } else if (S_ISLNK(ip->i_mode)) {
1693         ip->i_op = &afs_symlink_iops;
1694 #if defined(AFS_LINUX24_ENV)
1695         ip->i_data.a_ops = &afs_symlink_aops;
1696         ip->i_mapping = &ip->i_data;
1697 #endif
1698     }
1699
1700     /* insert_inode_hash(ip);   -- this would make iget() work (if we used it) */
1701 }