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