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