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