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