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