DEVEL15-linux-locking-pre-2618-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         flp->fl_flags &=~ FL_SLEEP;
496         code = posix_lock_file(fp, flp);
497 #else
498         code = posix_lock_file(fp, flp, 0);
499 #endif
500        if (code && flp->fl_type != F_UNLCK) {
501            struct AFS_FLOCK flock2;
502            flock2 = flock;
503            flock2.l_type = F_UNLCK;
504            AFS_GLOCK();
505            afs_lockctl(vcp, &flock2, F_SETLK, credp);
506            AFS_GUNLOCK();
507        }
508     }
509 #endif
510     /* Convert flock back to Linux's file_lock */
511     flp->fl_type = flock.l_type;
512     flp->fl_pid = flock.l_pid;
513     flp->fl_start = flock.l_start;
514     flp->fl_end = flock.l_start + flock.l_len;
515
516     crfree(credp);
517     return -code;
518
519 }
520
521 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
522 static int
523 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
524     int code = 0;
525     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
526     cred_t *credp = crref();
527     struct AFS_FLOCK flock;
528     /* Convert to a lock format afs_lockctl understands. */
529     memset((char *)&flock, 0, sizeof(flock));
530     flock.l_type = flp->fl_type;
531     flock.l_pid = flp->fl_pid;
532     flock.l_whence = 0;
533     flock.l_start = 0;
534     flock.l_len = OFFSET_MAX;
535
536     /* Safe because there are no large files, yet */
537 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
538     if (cmd == F_GETLK64)
539         cmd = F_GETLK;
540     else if (cmd == F_SETLK64)
541         cmd = F_SETLK;
542     else if (cmd == F_SETLKW64)
543         cmd = F_SETLKW;
544 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
545
546     AFS_GLOCK();
547     code = afs_lockctl(vcp, &flock, cmd, credp);
548     AFS_GUNLOCK();
549
550     if ((code == 0 || flp->fl_type == F_UNLCK) && 
551         (cmd == F_SETLK || cmd == F_SETLKW)) {
552         flp->fl_flags &=~ FL_SLEEP;
553         code = flock_lock_file_wait(fp, flp);
554         if (code && flp->fl_type != F_UNLCK) {
555             struct AFS_FLOCK flock2;
556             flock2 = flock;
557             flock2.l_type = F_UNLCK;
558             AFS_GLOCK();
559             afs_lockctl(vcp, &flock2, F_SETLK, credp);
560             AFS_GUNLOCK();
561         }
562     }
563     /* Convert flock back to Linux's file_lock */
564     flp->fl_type = flock.l_type;
565     flp->fl_pid = flock.l_pid;
566
567     crfree(credp);
568     return -code;
569 }
570 #endif
571
572 /* afs_linux_flush
573  * essentially the same as afs_fsync() but we need to get the return
574  * code for the sys_close() here, not afs_linux_release(), so call
575  * afs_StoreAllSegments() with AFS_LASTSTORE
576  */
577 static int
578 afs_linux_flush(struct file *fp)
579 {
580     struct vrequest treq;
581     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
582     cred_t *credp = crref();
583     int code;
584
585     AFS_GLOCK();
586
587     code = afs_InitReq(&treq, credp);
588     if (code)
589         goto out;
590
591     ObtainSharedLock(&vcp->lock, 535);
592     if (vcp->execsOrWriters > 0) {
593         UpgradeSToWLock(&vcp->lock, 536);
594         code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
595         ConvertWToSLock(&vcp->lock);
596     }
597     code = afs_CheckCode(code, &treq, 54);
598     ReleaseSharedLock(&vcp->lock);
599
600 out:
601     AFS_GUNLOCK();
602
603     crfree(credp);
604     return -code;
605 }
606
607 #if !defined(AFS_LINUX24_ENV)
608 /* Not allowed to directly read a directory. */
609 ssize_t
610 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
611 {
612     return -EISDIR;
613 }
614 #endif
615
616
617
618 struct file_operations afs_dir_fops = {
619 #if !defined(AFS_LINUX24_ENV)
620   .read =       afs_linux_dir_read,
621   .lock =       afs_linux_lock,
622   .fsync =      afs_linux_fsync,
623 #else
624   .read =       generic_read_dir,
625 #endif
626   .readdir =    afs_linux_readdir,
627 #ifdef HAVE_UNLOCKED_IOCTL
628   .unlocked_ioctl = afs_unlocked_xioctl,
629 #else
630   .ioctl =      afs_xioctl,
631 #endif
632 #ifdef HAVE_COMPAT_IOCTL
633   .compat_ioctl = afs_unlocked_xioctl,
634 #endif
635   .open =       afs_linux_open,
636   .release =    afs_linux_release,
637 };
638
639 struct file_operations afs_file_fops = {
640   .read =       afs_linux_read,
641   .write =      afs_linux_write,
642 #ifdef GENERIC_FILE_AIO_READ
643   .aio_read =   generic_file_aio_read,
644   .aio_write =  generic_file_aio_write,
645 #endif
646 #ifdef HAVE_UNLOCKED_IOCTL
647   .unlocked_ioctl = afs_unlocked_xioctl,
648 #else
649   .ioctl =      afs_xioctl,
650 #endif
651 #ifdef HAVE_COMPAT_IOCTL
652   .compat_ioctl = afs_unlocked_xioctl,
653 #endif
654   .mmap =       afs_linux_mmap,
655   .open =       afs_linux_open,
656   .flush =      afs_linux_flush,
657 #ifdef AFS_LINUX26_ENV
658   .sendfile =   generic_file_sendfile,
659 #endif
660   .release =    afs_linux_release,
661   .fsync =      afs_linux_fsync,
662   .lock =       afs_linux_lock,
663 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
664   .flock =      afs_linux_flock,
665 #endif
666 };
667
668
669 /**********************************************************************
670  * AFS Linux dentry operations
671  **********************************************************************/
672
673 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
674  * that has its mvid (parent dir's fid) pointer set to the wrong directory
675  * due to being mounted in multiple points at once. If so, check_bad_parent()
676  * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
677  * dotdotfid and mtpoint fid members.
678  * Parameters:
679  *   dp - dentry to be checked.
680  * Return Values:
681  *   None.
682  * Sideeffects:
683  *   This dentry's vcache's mvid will be set to the correct parent directory's
684  *   fid.
685  *   This root vnode's volume will have its dotdotfid and mtpoint fids set
686  *   to the correct parent and mountpoint fids.
687  */
688
689 static inline void
690 check_bad_parent(struct dentry *dp)
691 {
692     cred_t *credp;
693     struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
694     struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
695
696     if (vcp->mvid->Fid.Volume != pvc->fid.Fid.Volume) { /* bad parent */
697         credp = crref();
698
699         /* force a lookup, so vcp->mvid is fixed up */
700         afs_lookup(pvc, dp->d_name.name, &avc, credp);
701         if (!avc || vcp != avc) {       /* bad, very bad.. */
702             afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
703                        "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
704                        ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
705                        ICL_TYPE_POINTER, dp);
706         }
707         if (avc)
708             AFS_RELE(AFSTOV(avc));
709         crfree(credp);
710     }
711
712     return;
713 }
714
715 /* afs_linux_revalidate
716  * Ensure vcache is stat'd before use. Return 0 if entry is valid.
717  */
718 static int
719 afs_linux_revalidate(struct dentry *dp)
720 {
721     struct vattr vattr;
722     struct vcache *vcp = VTOAFS(dp->d_inode);
723     cred_t *credp;
724     int code;
725
726 #ifdef AFS_LINUX24_ENV
727     lock_kernel();
728 #endif
729     AFS_GLOCK();
730
731 #ifdef notyet
732     /* Make this a fast path (no crref), since it's called so often. */
733     if (vcp->states & CStatd) {
734
735         if (*dp->d_name.name != '/' && vcp->mvstat == 2)        /* root vnode */
736             check_bad_parent(dp);       /* check and correct mvid */
737
738         AFS_GUNLOCK();
739 #ifdef AFS_LINUX24_ENV
740         unlock_kernel();
741 #endif
742         return 0;
743     }
744 #endif
745
746     credp = crref();
747     code = afs_getattr(vcp, &vattr, credp);
748     if (!code)
749         vattr2inode(AFSTOV(vcp), &vattr);
750
751     AFS_GUNLOCK();
752 #ifdef AFS_LINUX24_ENV
753     unlock_kernel();
754 #endif
755     crfree(credp);
756
757     return -code;
758 }
759
760 #if defined(AFS_LINUX26_ENV)
761 static int
762 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
763 {
764         int err = afs_linux_revalidate(dentry);
765         if (!err) {
766                 generic_fillattr(dentry->d_inode, stat);
767 }
768         return err;
769 }
770 #endif
771
772 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
773  * In kernels 2.2.10 and above, we are passed an additional flags var which
774  * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
775  * we are advised to follow the entry if it is a link or to make sure that 
776  * it is a directory. But since the kernel itself checks these possibilities
777  * later on, we shouldn't have to do it until later. Perhaps in the future..
778  */
779 static int
780 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
781 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
782 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
783 #else
784 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
785 #endif
786 #else
787 afs_linux_dentry_revalidate(struct dentry *dp)
788 #endif
789 {
790     struct vattr vattr;
791     cred_t *credp = NULL;
792     struct vcache *vcp, *pvcp, *tvc = NULL;
793     int valid;
794
795 #ifdef AFS_LINUX24_ENV
796     lock_kernel();
797 #endif
798     AFS_GLOCK();
799
800     if (dp->d_inode) {
801
802         vcp = VTOAFS(dp->d_inode);
803         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
804
805         if (vcp == afs_globalVp)
806             goto good_dentry;
807
808         if (*dp->d_name.name != '/' && vcp->mvstat == 2)        /* root vnode */
809             check_bad_parent(dp);       /* check and correct mvid */
810
811 #ifdef notdef
812         /* If the last looker changes, we should make sure the current
813          * looker still has permission to examine this file.  This would
814          * always require a crref() which would be "slow".
815          */
816         if (vcp->last_looker != treq.uid) {
817             if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
818                 goto bad_dentry;
819
820             vcp->last_looker = treq.uid;
821         }
822 #endif
823
824         /* If the parent's DataVersion has changed or the vnode
825          * is longer valid, we need to do a full lookup.  VerifyVCache
826          * isn't enough since the vnode may have been renamed.
827          */
828
829         if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
830
831             credp = crref();
832             afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
833             if (!tvc || tvc != vcp)
834                 goto bad_dentry;
835
836             if (afs_getattr(vcp, &vattr, credp))
837                 goto bad_dentry;
838
839             vattr2inode(AFSTOV(vcp), &vattr);
840             dp->d_time = hgetlo(pvcp->m.DataVersion);
841         }
842
843         /* should we always update the attributes at this point? */
844         /* unlikely--the vcache entry hasn't changed */
845
846     } else {
847 #ifdef notyet
848         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
849         if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
850             goto bad_dentry;
851 #endif
852
853         /* No change in parent's DataVersion so this negative
854          * lookup is still valid.  BUT, if a server is down a
855          * negative lookup can result so there should be a
856          * liftime as well.  For now, always expire.
857          */
858
859         goto bad_dentry;
860     }
861
862   good_dentry:
863     valid = 1;
864
865   done:
866     /* Clean up */
867     if (tvc)
868         afs_PutVCache(tvc);
869     AFS_GUNLOCK();
870     if (credp)
871         crfree(credp);
872
873     if (!valid) {
874         shrink_dcache_parent(dp);
875         d_drop(dp);
876     }
877 #ifdef AFS_LINUX24_ENV
878     unlock_kernel();
879 #endif
880     return valid;
881
882   bad_dentry:
883     valid = 0;
884     goto done;
885 }
886
887 static void
888 afs_dentry_iput(struct dentry *dp, struct inode *ip)
889 {
890     struct vcache *vcp = VTOAFS(ip);
891
892     AFS_GLOCK();
893     (void) afs_InactiveVCache(vcp, NULL);
894     AFS_GUNLOCK();
895
896     iput(ip);
897 }
898
899 static int
900 afs_dentry_delete(struct dentry *dp)
901 {
902     if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
903         return 1;               /* bad inode? */
904
905     return 0;
906 }
907
908 struct dentry_operations afs_dentry_operations = {
909   .d_revalidate =       afs_linux_dentry_revalidate,
910   .d_delete =           afs_dentry_delete,
911   .d_iput =             afs_dentry_iput,
912 };
913
914 /**********************************************************************
915  * AFS Linux inode operations
916  **********************************************************************/
917
918 /* afs_linux_create
919  *
920  * Merely need to set enough of vattr to get us through the create. Note
921  * that the higher level code (open_namei) will take care of any tuncation
922  * explicitly. Exclusive open is also taken care of in open_namei.
923  *
924  * name is in kernel space at this point.
925  */
926 static int
927 #ifdef IOP_CREATE_TAKES_NAMEIDATA
928 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
929                  struct nameidata *nd)
930 #else
931 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
932 #endif
933 {
934     struct vattr vattr;
935     cred_t *credp = crref();
936     const char *name = dp->d_name.name;
937     struct vcache *vcp;
938     int code;
939
940     VATTR_NULL(&vattr);
941     vattr.va_mode = mode;
942     vattr.va_type = mode & S_IFMT;
943
944 #if defined(AFS_LINUX26_ENV)
945     lock_kernel();
946 #endif
947     AFS_GLOCK();
948     code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
949                       &vcp, credp);
950
951     if (!code) {
952         struct inode *ip = AFSTOV(vcp);
953
954         afs_getattr(vcp, &vattr, credp);
955         afs_fill_inode(ip, &vattr);
956         dp->d_op = &afs_dentry_operations;
957         dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
958         d_instantiate(dp, ip);
959     }
960     AFS_GUNLOCK();
961
962 #if defined(AFS_LINUX26_ENV)
963     unlock_kernel();
964 #endif
965     crfree(credp);
966     return -code;
967 }
968
969 /* afs_linux_lookup */
970 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
971 static struct dentry *
972 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
973 afs_linux_lookup(struct inode *dip, struct dentry *dp,
974                  struct nameidata *nd)
975 #else
976 afs_linux_lookup(struct inode *dip, struct dentry *dp)
977 #endif
978 #else
979 static int
980 afs_linux_lookup(struct inode *dip, struct dentry *dp)
981 #endif
982 {
983     cred_t *credp = crref();
984     struct vcache *vcp = NULL;
985     const char *comp = dp->d_name.name;
986     struct inode *ip = NULL;
987 #if defined(AFS_LINUX26_ENV)
988     struct dentry *newdp = NULL;
989 #endif
990     int code;
991
992 #if defined(AFS_LINUX26_ENV)
993     lock_kernel();
994 #endif
995     AFS_GLOCK();
996     code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
997     
998     if (vcp) {
999         struct vattr vattr;
1000
1001         ip = AFSTOV(vcp);
1002         afs_getattr(vcp, &vattr, credp);
1003         afs_fill_inode(ip, &vattr);
1004     }
1005     dp->d_op = &afs_dentry_operations;
1006     dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1007     AFS_GUNLOCK();
1008
1009 #if defined(AFS_LINUX24_ENV)
1010     if (ip && S_ISDIR(ip->i_mode)) {
1011         struct dentry *alias;
1012
1013         /* Try to invalidate an existing alias in favor of our new one */
1014         alias = d_find_alias(ip);
1015 #if defined(AFS_LINUX26_ENV)
1016         /* But not if it's disconnected; then we want d_splice_alias below */
1017         if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
1018 #else
1019         if (alias) {
1020 #endif
1021             if (d_invalidate(alias) == 0) {
1022                 dput(alias);
1023             } else {
1024                 iput(ip);
1025 #if defined(AFS_LINUX26_ENV)
1026                 unlock_kernel();
1027 #endif
1028                 return alias;
1029             }
1030         }
1031     }
1032 #endif
1033 #if defined(AFS_LINUX26_ENV)
1034     newdp = d_splice_alias(ip, dp);
1035 #else
1036     d_add(dp, ip);
1037 #endif
1038
1039 #if defined(AFS_LINUX26_ENV)
1040     unlock_kernel();
1041 #endif
1042     crfree(credp);
1043
1044     /* It's ok for the file to not be found. That's noted by the caller by
1045      * seeing that the dp->d_inode field is NULL.
1046      */
1047 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1048 #if defined(AFS_LINUX26_ENV)
1049     if (!code || code == ENOENT)
1050         return newdp;
1051 #else
1052     if (code == ENOENT)
1053         return ERR_PTR(0);
1054 #endif
1055     else
1056         return ERR_PTR(-code);
1057 #else
1058     if (code == ENOENT)
1059         code = 0;
1060     return -code;
1061 #endif
1062 }
1063
1064 static int
1065 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1066 {
1067     int code;
1068     cred_t *credp = crref();
1069     const char *name = newdp->d_name.name;
1070     struct inode *oldip = olddp->d_inode;
1071
1072     /* If afs_link returned the vnode, we could instantiate the
1073      * dentry. Since it's not, we drop this one and do a new lookup.
1074      */
1075     d_drop(newdp);
1076
1077     AFS_GLOCK();
1078     code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1079
1080     AFS_GUNLOCK();
1081     crfree(credp);
1082     return -code;
1083 }
1084
1085 static int
1086 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1087 {
1088     int code = EBUSY;
1089     cred_t *credp = crref();
1090     const char *name = dp->d_name.name;
1091     struct vcache *tvc = VTOAFS(dp->d_inode);
1092
1093 #if defined(AFS_LINUX26_ENV)
1094     lock_kernel();
1095 #endif
1096     if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1097                                 && !(tvc->states & CUnlinked)) {
1098         struct dentry *__dp;
1099         char *__name;
1100         extern char *afs_newname();
1101
1102         __dp = NULL;
1103         __name = NULL;
1104         do {
1105             dput(__dp);
1106
1107             AFS_GLOCK();
1108             if (__name)
1109                 osi_FreeSmallSpace(__name);
1110             __name = afs_newname();
1111             AFS_GUNLOCK();
1112
1113             __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1114                 
1115             if (IS_ERR(__dp))
1116                 goto out;
1117         } while (__dp->d_inode != NULL);
1118
1119         AFS_GLOCK();
1120         code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1121         if (!code) {
1122             tvc->mvid = (void *) __name;
1123             crhold(credp);
1124             if (tvc->uncred) {
1125                 crfree(tvc->uncred);
1126             }
1127             tvc->uncred = credp;
1128             tvc->states |= CUnlinked;
1129         }
1130         AFS_GUNLOCK();
1131
1132         if (!code) {
1133             __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1134             d_move(dp, __dp);
1135         }
1136         dput(__dp);
1137
1138         goto out;
1139     }
1140
1141     AFS_GLOCK();
1142     code = afs_remove(VTOAFS(dip), name, credp);
1143     AFS_GUNLOCK();
1144     if (!code)
1145         d_drop(dp);
1146 out:
1147 #if defined(AFS_LINUX26_ENV)
1148     unlock_kernel();
1149 #endif
1150     crfree(credp);
1151     return -code;
1152 }
1153
1154
1155 static int
1156 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1157 {
1158     int code;
1159     cred_t *credp = crref();
1160     struct vattr vattr;
1161     const char *name = dp->d_name.name;
1162
1163     /* If afs_symlink returned the vnode, we could instantiate the
1164      * dentry. Since it's not, we drop this one and do a new lookup.
1165      */
1166     d_drop(dp);
1167
1168     VATTR_NULL(&vattr);
1169     AFS_GLOCK();
1170     code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1171     AFS_GUNLOCK();
1172     crfree(credp);
1173     return -code;
1174 }
1175
1176 static int
1177 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1178 {
1179     int code;
1180     cred_t *credp = crref();
1181     struct vcache *tvcp = NULL;
1182     struct vattr vattr;
1183     const char *name = dp->d_name.name;
1184
1185 #if defined(AFS_LINUX26_ENV)
1186     lock_kernel();
1187 #endif
1188     VATTR_NULL(&vattr);
1189     vattr.va_mask = ATTR_MODE;
1190     vattr.va_mode = mode;
1191     AFS_GLOCK();
1192     code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1193
1194     if (tvcp) {
1195         struct inode *ip = AFSTOV(tvcp);
1196
1197         afs_getattr(tvcp, &vattr, credp);
1198         afs_fill_inode(ip, &vattr);
1199
1200         dp->d_op = &afs_dentry_operations;
1201         dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1202         d_instantiate(dp, ip);
1203     }
1204     AFS_GUNLOCK();
1205
1206 #if defined(AFS_LINUX26_ENV)
1207     unlock_kernel();
1208 #endif
1209     crfree(credp);
1210     return -code;
1211 }
1212
1213 static int
1214 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1215 {
1216     int code;
1217     cred_t *credp = crref();
1218     const char *name = dp->d_name.name;
1219
1220     /* locking kernel conflicts with glock? */
1221
1222     AFS_GLOCK();
1223     code = afs_rmdir(VTOAFS(dip), name, credp);
1224     AFS_GUNLOCK();
1225
1226     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1227      * that failed because a directory is not empty. So, we map
1228      * EEXIST to ENOTEMPTY on linux.
1229      */
1230     if (code == EEXIST) {
1231         code = ENOTEMPTY;
1232     }
1233
1234     if (!code) {
1235         d_drop(dp);
1236     }
1237
1238     crfree(credp);
1239     return -code;
1240 }
1241
1242
1243 static int
1244 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1245                  struct inode *newip, struct dentry *newdp)
1246 {
1247     int code;
1248     cred_t *credp = crref();
1249     const char *oldname = olddp->d_name.name;
1250     const char *newname = newdp->d_name.name;
1251     struct dentry *rehash = NULL;
1252
1253 #if defined(AFS_LINUX26_ENV)
1254     /* Prevent any new references during rename operation. */
1255     lock_kernel();
1256 #endif
1257     /* Remove old and new entries from name hash. New one will change below.
1258      * While it's optimal to catch failures and re-insert newdp into hash,
1259      * it's also error prone and in that case we're already dealing with error
1260      * cases. Let another lookup put things right, if need be.
1261      */
1262 #if defined(AFS_LINUX26_ENV)
1263     if (!d_unhashed(newdp)) {
1264         d_drop(newdp);
1265         rehash = newdp;
1266     }
1267 #else
1268     if (!list_empty(&newdp->d_hash)) {
1269         d_drop(newdp);
1270         rehash = newdp;
1271     }
1272 #endif
1273
1274 #if defined(AFS_LINUX24_ENV)
1275     if (atomic_read(&olddp->d_count) > 1)
1276         shrink_dcache_parent(olddp);
1277 #endif
1278
1279     AFS_GLOCK();
1280     code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1281     AFS_GUNLOCK();
1282
1283     if (rehash)
1284         d_rehash(rehash);
1285
1286 #if defined(AFS_LINUX26_ENV)
1287     unlock_kernel();
1288 #endif
1289
1290     crfree(credp);
1291     return -code;
1292 }
1293
1294
1295 /* afs_linux_ireadlink 
1296  * Internal readlink which can return link contents to user or kernel space.
1297  * Note that the buffer is NOT supposed to be null-terminated.
1298  */
1299 static int
1300 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1301 {
1302     int code;
1303     cred_t *credp = crref();
1304     uio_t tuio;
1305     struct iovec iov;
1306
1307     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1308     code = afs_readlink(VTOAFS(ip), &tuio, credp);
1309     crfree(credp);
1310
1311     if (!code)
1312         return maxlen - tuio.uio_resid;
1313     else
1314         return -code;
1315 }
1316
1317 #if !defined(AFS_LINUX24_ENV)
1318 /* afs_linux_readlink 
1319  * Fill target (which is in user space) with contents of symlink.
1320  */
1321 static int
1322 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1323 {
1324     int code;
1325     struct inode *ip = dp->d_inode;
1326
1327     AFS_GLOCK();
1328     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1329     AFS_GUNLOCK();
1330     return code;
1331 }
1332
1333
1334 /* afs_linux_follow_link
1335  * a file system dependent link following routine.
1336  */
1337 static struct dentry *
1338 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1339                       unsigned int follow)
1340 {
1341     int code = 0;
1342     char *name;
1343     struct dentry *res;
1344
1345
1346     AFS_GLOCK();
1347     name = osi_Alloc(PATH_MAX + 1);
1348     if (!name) {
1349         AFS_GUNLOCK();
1350         dput(basep);
1351         return ERR_PTR(-EIO);
1352     }
1353
1354     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1355     AFS_GUNLOCK();
1356
1357     if (code < 0) {
1358         dput(basep);
1359         res = ERR_PTR(code);
1360     } else {
1361         name[code] = '\0';
1362         res = lookup_dentry(name, basep, follow);
1363     }
1364
1365     AFS_GLOCK();
1366     osi_Free(name, PATH_MAX + 1);
1367     AFS_GUNLOCK();
1368     return res;
1369 }
1370 #endif
1371
1372 /* afs_linux_readpage
1373  * all reads come through here. A strategy-like read call.
1374  */
1375 static int
1376 afs_linux_readpage(struct file *fp, struct page *pp)
1377 {
1378     int code;
1379     cred_t *credp = crref();
1380 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1381     char *address;
1382     afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1383 #else
1384     ulong address = afs_linux_page_address(pp);
1385     afs_offs_t offset = pageoff(pp);
1386 #endif
1387     uio_t tuio;
1388     struct iovec iovec;
1389     struct inode *ip = FILE_INODE(fp);
1390     int cnt = page_count(pp);
1391     struct vcache *avc = VTOAFS(ip);
1392
1393
1394 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1395     address = kmap(pp);
1396     ClearPageError(pp);
1397 #else
1398     atomic_add(1, &pp->count);
1399     set_bit(PG_locked, &pp->flags);     /* other bits? See mm.h */
1400     clear_bit(PG_error, &pp->flags);
1401 #endif
1402
1403     setup_uio(&tuio, &iovec, (char *)address, offset, PAGE_SIZE, UIO_READ,
1404               AFS_UIOSYS);
1405 #ifdef AFS_LINUX24_ENV
1406     lock_kernel();
1407 #endif
1408     AFS_GLOCK();
1409     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 */
1410     code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1411     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1412                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1413                code);
1414     AFS_GUNLOCK();
1415 #ifdef AFS_LINUX24_ENV
1416     unlock_kernel();
1417 #endif
1418
1419     if (!code) {
1420         if (tuio.uio_resid)     /* zero remainder of page */
1421             memset((void *)(address + (PAGE_SIZE - tuio.uio_resid)), 0,
1422                    tuio.uio_resid);
1423 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1424         flush_dcache_page(pp);
1425         SetPageUptodate(pp);
1426 #else
1427         set_bit(PG_uptodate, &pp->flags);
1428 #endif
1429     }
1430
1431 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1432     kunmap(pp);
1433     UnlockPage(pp);
1434 #else
1435     clear_bit(PG_locked, &pp->flags);
1436     wake_up(&pp->wait);
1437     free_page(address);
1438 #endif
1439
1440     if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1441         struct dcache *tdc;
1442         struct vrequest treq;
1443
1444         AFS_GLOCK();
1445         code = afs_InitReq(&treq, credp);
1446         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1447             tdc = afs_FindDCache(avc, offset);
1448             if (tdc) {
1449                 if (!(tdc->mflags & DFNextStarted))
1450                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1451                 afs_PutDCache(tdc);
1452             }
1453             ReleaseWriteLock(&avc->lock);
1454         }
1455         AFS_GUNLOCK();
1456     }
1457
1458     crfree(credp);
1459     return -code;
1460 }
1461
1462
1463 #if defined(AFS_LINUX24_ENV)
1464 static int
1465 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1466                          unsigned long offset, unsigned int count)
1467 {
1468     struct vcache *vcp = VTOAFS(ip);
1469     char *buffer;
1470     afs_offs_t base;
1471     int code = 0;
1472     cred_t *credp;
1473     uio_t tuio;
1474     struct iovec iovec;
1475     int f_flags = 0;
1476
1477     buffer = kmap(pp) + offset;
1478     base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT)  + offset;
1479
1480     credp = crref();
1481     lock_kernel();
1482     AFS_GLOCK();
1483     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1484                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1485                ICL_TYPE_INT32, 99999);
1486
1487     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1488
1489     code = afs_write(vcp, &tuio, f_flags, credp, 0);
1490
1491     ip->i_size = vcp->m.Length;
1492     ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1493
1494     if (!code) {
1495         struct vrequest treq;
1496
1497         ObtainWriteLock(&vcp->lock, 533);
1498         if (!afs_InitReq(&treq, credp))
1499             code = afs_DoPartialWrite(vcp, &treq);
1500         ReleaseWriteLock(&vcp->lock);
1501     }
1502     code = code ? -code : count - tuio.uio_resid;
1503
1504     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1505                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1506                ICL_TYPE_INT32, code);
1507
1508     AFS_GUNLOCK();
1509     unlock_kernel();
1510     crfree(credp);
1511     kunmap(pp);
1512
1513     return code;
1514 }
1515
1516
1517 static int
1518 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1519 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1520 #else
1521 afs_linux_writepage(struct page *pp)
1522 #endif
1523 {
1524     struct address_space *mapping = pp->mapping;
1525     struct inode *inode;
1526     unsigned long end_index;
1527     unsigned offset = PAGE_CACHE_SIZE;
1528     long status;
1529
1530 #if defined(AFS_LINUX26_ENV)
1531     if (PageReclaim(pp)) {
1532 # if defined(WRITEPAGE_ACTIVATE)
1533         return WRITEPAGE_ACTIVATE;
1534 # else 
1535         return AOP_WRITEPAGE_ACTIVATE;
1536 # endif
1537     }
1538 #else
1539     if (PageLaunder(pp)) {
1540         return(fail_writepage(pp));
1541     }
1542 #endif
1543
1544     inode = (struct inode *)mapping->host;
1545     end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1546
1547     /* easy case */
1548     if (pp->index < end_index)
1549         goto do_it;
1550     /* things got complicated... */
1551     offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1552     /* OK, are we completely out? */
1553     if (pp->index >= end_index + 1 || !offset)
1554         return -EIO;
1555   do_it:
1556     status = afs_linux_writepage_sync(inode, pp, 0, offset);
1557     SetPageUptodate(pp);
1558     UnlockPage(pp);
1559     if (status == offset)
1560         return 0;
1561     else
1562         return status;
1563 }
1564 #else
1565 /* afs_linux_updatepage
1566  * What one would have thought was writepage - write dirty page to file.
1567  * Called from generic_file_write. buffer is still in user space. pagep
1568  * has been filled in with old data if we're updating less than a page.
1569  */
1570 static int
1571 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1572                      unsigned int count, int sync)
1573 {
1574     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1575     u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1576     int code = 0;
1577     cred_t *credp;
1578     uio_t tuio;
1579     struct iovec iovec;
1580
1581     set_bit(PG_locked, &pp->flags);
1582
1583     credp = crref();
1584     AFS_GLOCK();
1585     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1586                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1587                ICL_TYPE_INT32, 99999);
1588     setup_uio(&tuio, &iovec, page_addr + offset,
1589               (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1590               AFS_UIOSYS);
1591
1592     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1593
1594     ip->i_size = vcp->m.Length;
1595     ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1596
1597     if (!code) {
1598         struct vrequest treq;
1599
1600         ObtainWriteLock(&vcp->lock, 533);
1601         vcp->m.Date = osi_Time();   /* set modification time */
1602         if (!afs_InitReq(&treq, credp))
1603             code = afs_DoPartialWrite(vcp, &treq);
1604         ReleaseWriteLock(&vcp->lock);
1605     }
1606
1607     code = code ? -code : count - tuio.uio_resid;
1608     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1609                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1610                ICL_TYPE_INT32, code);
1611
1612     AFS_GUNLOCK();
1613     crfree(credp);
1614
1615     clear_bit(PG_locked, &pp->flags);
1616     return code;
1617 }
1618 #endif
1619
1620 /* afs_linux_permission
1621  * Check access rights - returns error if can't check or permission denied.
1622  */
1623 static int
1624 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1625 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1626 #else
1627 afs_linux_permission(struct inode *ip, int mode)
1628 #endif
1629 {
1630     int code;
1631     cred_t *credp = crref();
1632     int tmp = 0;
1633
1634     AFS_GLOCK();
1635     if (mode & MAY_EXEC)
1636         tmp |= VEXEC;
1637     if (mode & MAY_READ)
1638         tmp |= VREAD;
1639     if (mode & MAY_WRITE)
1640         tmp |= VWRITE;
1641     code = afs_access(VTOAFS(ip), tmp, credp);
1642
1643     AFS_GUNLOCK();
1644     crfree(credp);
1645     return -code;
1646 }
1647
1648 #if defined(AFS_LINUX24_ENV)
1649 static int
1650 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1651                        unsigned to)
1652 {
1653     int code;
1654
1655     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1656                                     offset, to - offset);
1657 #if !defined(AFS_LINUX26_ENV)
1658     kunmap(page);
1659 #endif
1660
1661     return code;
1662 }
1663
1664 static int
1665 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1666                         unsigned to)
1667 {
1668 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1669    call kmap directly instead of relying on us to do it */
1670 #if !defined(AFS_LINUX26_ENV)
1671     kmap(page);
1672 #endif
1673     return 0;
1674 }
1675
1676 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1677 #endif
1678
1679 static struct inode_operations afs_file_iops = {
1680 #if defined(AFS_LINUX26_ENV)
1681   .permission =         afs_linux_permission,
1682   .getattr =            afs_linux_getattr,
1683   .setattr =            afs_notify_change,
1684 #elif defined(AFS_LINUX24_ENV)
1685   .permission =         afs_linux_permission,
1686   .revalidate =         afs_linux_revalidate,
1687   .setattr =            afs_notify_change,
1688 #else
1689   .default_file_ops =   &afs_file_fops,
1690   .readpage =           afs_linux_readpage,
1691   .revalidate =         afs_linux_revalidate,
1692   .updatepage =         afs_linux_updatepage,
1693 #endif
1694 };
1695
1696 #if defined(AFS_LINUX24_ENV)
1697 static struct address_space_operations afs_file_aops = {
1698   .readpage =           afs_linux_readpage,
1699   .writepage =          afs_linux_writepage,
1700   .commit_write =       afs_linux_commit_write,
1701   .prepare_write =      afs_linux_prepare_write,
1702 };
1703 #endif
1704
1705
1706 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1707  * by what sort of operation is allowed.....
1708  */
1709
1710 static struct inode_operations afs_dir_iops = {
1711 #if !defined(AFS_LINUX24_ENV)
1712   .default_file_ops =   &afs_dir_fops,
1713 #else
1714   .setattr =            afs_notify_change,
1715 #endif
1716   .create =             afs_linux_create,
1717   .lookup =             afs_linux_lookup,
1718   .link =               afs_linux_link,
1719   .unlink =             afs_linux_unlink,
1720   .symlink =            afs_linux_symlink,
1721   .mkdir =              afs_linux_mkdir,
1722   .rmdir =              afs_linux_rmdir,
1723   .rename =             afs_linux_rename,
1724 #if defined(AFS_LINUX26_ENV)
1725   .getattr =            afs_linux_getattr,
1726 #else
1727   .revalidate =         afs_linux_revalidate,
1728 #endif
1729   .permission =         afs_linux_permission,
1730 };
1731
1732 /* We really need a separate symlink set of ops, since do_follow_link()
1733  * determines if it _is_ a link by checking if the follow_link op is set.
1734  */
1735 #if defined(AFS_LINUX24_ENV)
1736 static int
1737 afs_symlink_filler(struct file *file, struct page *page)
1738 {
1739     struct inode *ip = (struct inode *)page->mapping->host;
1740     char *p = (char *)kmap(page);
1741     int code;
1742
1743     lock_kernel();
1744     AFS_GLOCK();
1745     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1746     AFS_GUNLOCK();
1747
1748     if (code < 0)
1749         goto fail;
1750     p[code] = '\0';             /* null terminate? */
1751     unlock_kernel();
1752
1753     SetPageUptodate(page);
1754     kunmap(page);
1755     UnlockPage(page);
1756     return 0;
1757
1758   fail:
1759     unlock_kernel();
1760
1761     SetPageError(page);
1762     kunmap(page);
1763     UnlockPage(page);
1764     return code;
1765 }
1766
1767 static struct address_space_operations afs_symlink_aops = {
1768   .readpage =   afs_symlink_filler
1769 };
1770 #endif
1771
1772 static struct inode_operations afs_symlink_iops = {
1773 #if defined(AFS_LINUX24_ENV)
1774   .readlink =           page_readlink,
1775 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1776   .follow_link =        page_follow_link,
1777 #else
1778   .follow_link =        page_follow_link_light,
1779   .put_link =           page_put_link,
1780 #endif
1781   .setattr =            afs_notify_change,
1782 #else
1783   .readlink =           afs_linux_readlink,
1784   .follow_link =        afs_linux_follow_link,
1785   .permission =         afs_linux_permission,
1786   .revalidate =         afs_linux_revalidate,
1787 #endif
1788 };
1789
1790 void
1791 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1792 {
1793         
1794     if (vattr)
1795         vattr2inode(ip, vattr);
1796
1797 /* Reset ops if symlink or directory. */
1798     if (S_ISREG(ip->i_mode)) {
1799         ip->i_op = &afs_file_iops;
1800 #if defined(AFS_LINUX24_ENV)
1801         ip->i_fop = &afs_file_fops;
1802         ip->i_data.a_ops = &afs_file_aops;
1803 #endif
1804
1805     } else if (S_ISDIR(ip->i_mode)) {
1806         ip->i_op = &afs_dir_iops;
1807 #if defined(AFS_LINUX24_ENV)
1808         ip->i_fop = &afs_dir_fops;
1809 #endif
1810
1811     } else if (S_ISLNK(ip->i_mode)) {
1812         ip->i_op = &afs_symlink_iops;
1813 #if defined(AFS_LINUX24_ENV)
1814         ip->i_data.a_ops = &afs_symlink_aops;
1815         ip->i_mapping = &ip->i_data;
1816 #endif
1817     }
1818
1819     /* insert_inode_hash(ip);   -- this would make iget() work (if we used it) */
1820 }