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