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