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