DEVEL15-linux-posix-lock-file-has-wait-arg-now-20070517
[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 POSIX_LOCK_FILE_WAIT_ARG
495         code = posix_lock_file(fp, flp, 0);
496 #else
497         flp->fl_flags &=~ FL_SLEEP;
498         code = posix_lock_file(fp, flp);
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                 crfree(credp);
1029                 return alias;
1030             }
1031         }
1032     }
1033 #endif
1034 #if defined(AFS_LINUX26_ENV)
1035     newdp = d_splice_alias(ip, dp);
1036 #else
1037     d_add(dp, ip);
1038 #endif
1039
1040 #if defined(AFS_LINUX26_ENV)
1041     unlock_kernel();
1042 #endif
1043     crfree(credp);
1044
1045     /* It's ok for the file to not be found. That's noted by the caller by
1046      * seeing that the dp->d_inode field is NULL.
1047      */
1048 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1049 #if defined(AFS_LINUX26_ENV)
1050     if (!code || code == ENOENT)
1051         return newdp;
1052 #else
1053     if (code == ENOENT)
1054         return ERR_PTR(0);
1055 #endif
1056     else
1057         return ERR_PTR(-code);
1058 #else
1059     if (code == ENOENT)
1060         code = 0;
1061     return -code;
1062 #endif
1063 }
1064
1065 static int
1066 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1067 {
1068     int code;
1069     cred_t *credp = crref();
1070     const char *name = newdp->d_name.name;
1071     struct inode *oldip = olddp->d_inode;
1072
1073     /* If afs_link returned the vnode, we could instantiate the
1074      * dentry. Since it's not, we drop this one and do a new lookup.
1075      */
1076     d_drop(newdp);
1077
1078     AFS_GLOCK();
1079     code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1080
1081     AFS_GUNLOCK();
1082     crfree(credp);
1083     return -code;
1084 }
1085
1086 static int
1087 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1088 {
1089     int code = EBUSY;
1090     cred_t *credp = crref();
1091     const char *name = dp->d_name.name;
1092     struct vcache *tvc = VTOAFS(dp->d_inode);
1093
1094 #if defined(AFS_LINUX26_ENV)
1095     lock_kernel();
1096 #endif
1097     if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1098                                 && !(tvc->states & CUnlinked)) {
1099         struct dentry *__dp;
1100         char *__name;
1101         extern char *afs_newname();
1102
1103         __dp = NULL;
1104         __name = NULL;
1105         do {
1106             dput(__dp);
1107
1108             AFS_GLOCK();
1109             if (__name)
1110                 osi_FreeSmallSpace(__name);
1111             __name = afs_newname();
1112             AFS_GUNLOCK();
1113
1114             __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1115                 
1116             if (IS_ERR(__dp))
1117                 goto out;
1118         } while (__dp->d_inode != NULL);
1119
1120         AFS_GLOCK();
1121         code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1122         if (!code) {
1123             tvc->mvid = (void *) __name;
1124             crhold(credp);
1125             if (tvc->uncred) {
1126                 crfree(tvc->uncred);
1127             }
1128             tvc->uncred = credp;
1129             tvc->states |= CUnlinked;
1130         } else {
1131             osi_FreeSmallSpace(__name); 
1132         }
1133         AFS_GUNLOCK();
1134
1135         if (!code) {
1136             __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1137             d_move(dp, __dp);
1138         }
1139         dput(__dp);
1140
1141         goto out;
1142     }
1143
1144     AFS_GLOCK();
1145     code = afs_remove(VTOAFS(dip), name, credp);
1146     AFS_GUNLOCK();
1147     if (!code)
1148         d_drop(dp);
1149 out:
1150 #if defined(AFS_LINUX26_ENV)
1151     unlock_kernel();
1152 #endif
1153     crfree(credp);
1154     return -code;
1155 }
1156
1157
1158 static int
1159 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1160 {
1161     int code;
1162     cred_t *credp = crref();
1163     struct vattr vattr;
1164     const char *name = dp->d_name.name;
1165
1166     /* If afs_symlink returned the vnode, we could instantiate the
1167      * dentry. Since it's not, we drop this one and do a new lookup.
1168      */
1169     d_drop(dp);
1170
1171     VATTR_NULL(&vattr);
1172     AFS_GLOCK();
1173     code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1174     AFS_GUNLOCK();
1175     crfree(credp);
1176     return -code;
1177 }
1178
1179 static int
1180 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1181 {
1182     int code;
1183     cred_t *credp = crref();
1184     struct vcache *tvcp = NULL;
1185     struct vattr vattr;
1186     const char *name = dp->d_name.name;
1187
1188 #if defined(AFS_LINUX26_ENV)
1189     lock_kernel();
1190 #endif
1191     VATTR_NULL(&vattr);
1192     vattr.va_mask = ATTR_MODE;
1193     vattr.va_mode = mode;
1194     AFS_GLOCK();
1195     code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1196
1197     if (tvcp) {
1198         struct inode *ip = AFSTOV(tvcp);
1199
1200         afs_getattr(tvcp, &vattr, credp);
1201         afs_fill_inode(ip, &vattr);
1202
1203         dp->d_op = &afs_dentry_operations;
1204         dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1205         d_instantiate(dp, ip);
1206     }
1207     AFS_GUNLOCK();
1208
1209 #if defined(AFS_LINUX26_ENV)
1210     unlock_kernel();
1211 #endif
1212     crfree(credp);
1213     return -code;
1214 }
1215
1216 static int
1217 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1218 {
1219     int code;
1220     cred_t *credp = crref();
1221     const char *name = dp->d_name.name;
1222
1223     /* locking kernel conflicts with glock? */
1224
1225     AFS_GLOCK();
1226     code = afs_rmdir(VTOAFS(dip), name, credp);
1227     AFS_GUNLOCK();
1228
1229     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1230      * that failed because a directory is not empty. So, we map
1231      * EEXIST to ENOTEMPTY on linux.
1232      */
1233     if (code == EEXIST) {
1234         code = ENOTEMPTY;
1235     }
1236
1237     if (!code) {
1238         d_drop(dp);
1239     }
1240
1241     crfree(credp);
1242     return -code;
1243 }
1244
1245
1246 static int
1247 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1248                  struct inode *newip, struct dentry *newdp)
1249 {
1250     int code;
1251     cred_t *credp = crref();
1252     const char *oldname = olddp->d_name.name;
1253     const char *newname = newdp->d_name.name;
1254     struct dentry *rehash = NULL;
1255
1256 #if defined(AFS_LINUX26_ENV)
1257     /* Prevent any new references during rename operation. */
1258     lock_kernel();
1259 #endif
1260     /* Remove old and new entries from name hash. New one will change below.
1261      * While it's optimal to catch failures and re-insert newdp into hash,
1262      * it's also error prone and in that case we're already dealing with error
1263      * cases. Let another lookup put things right, if need be.
1264      */
1265 #if defined(AFS_LINUX26_ENV)
1266     if (!d_unhashed(newdp)) {
1267         d_drop(newdp);
1268         rehash = newdp;
1269     }
1270 #else
1271     if (!list_empty(&newdp->d_hash)) {
1272         d_drop(newdp);
1273         rehash = newdp;
1274     }
1275 #endif
1276
1277 #if defined(AFS_LINUX24_ENV)
1278     if (atomic_read(&olddp->d_count) > 1)
1279         shrink_dcache_parent(olddp);
1280 #endif
1281
1282     AFS_GLOCK();
1283     code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1284     AFS_GUNLOCK();
1285
1286     if (rehash)
1287         d_rehash(rehash);
1288
1289 #if defined(AFS_LINUX26_ENV)
1290     unlock_kernel();
1291 #endif
1292
1293     crfree(credp);
1294     return -code;
1295 }
1296
1297
1298 /* afs_linux_ireadlink 
1299  * Internal readlink which can return link contents to user or kernel space.
1300  * Note that the buffer is NOT supposed to be null-terminated.
1301  */
1302 static int
1303 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1304 {
1305     int code;
1306     cred_t *credp = crref();
1307     uio_t tuio;
1308     struct iovec iov;
1309
1310     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1311     code = afs_readlink(VTOAFS(ip), &tuio, credp);
1312     crfree(credp);
1313
1314     if (!code)
1315         return maxlen - tuio.uio_resid;
1316     else
1317         return -code;
1318 }
1319
1320 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1321 /* afs_linux_readlink 
1322  * Fill target (which is in user space) with contents of symlink.
1323  */
1324 static int
1325 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1326 {
1327     int code;
1328     struct inode *ip = dp->d_inode;
1329
1330     AFS_GLOCK();
1331     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1332     AFS_GUNLOCK();
1333     return code;
1334 }
1335
1336
1337 /* afs_linux_follow_link
1338  * a file system dependent link following routine.
1339  */
1340 #if defined(AFS_LINUX24_ENV)
1341 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1342 {
1343     int code;
1344     char *name;
1345
1346     name = osi_Alloc(PATH_MAX);
1347     if (!name) {
1348         return -EIO;
1349     }
1350
1351     AFS_GLOCK();
1352     code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1353     AFS_GUNLOCK();
1354
1355     if (code < 0) {
1356         goto out;
1357     }
1358
1359     name[code] = '\0';
1360     code = vfs_follow_link(nd, name);
1361
1362 out:
1363     osi_Free(name, PATH_MAX);
1364
1365     return code;
1366 }
1367
1368 #else /* !defined(AFS_LINUX24_ENV) */
1369
1370 static struct dentry *
1371 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1372                       unsigned int follow)
1373 {
1374     int code = 0;
1375     char *name;
1376     struct dentry *res;
1377
1378
1379     AFS_GLOCK();
1380     name = osi_Alloc(PATH_MAX + 1);
1381     if (!name) {
1382         AFS_GUNLOCK();
1383         dput(basep);
1384         return ERR_PTR(-EIO);
1385     }
1386
1387     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1388     AFS_GUNLOCK();
1389
1390     if (code < 0) {
1391         dput(basep);
1392         res = ERR_PTR(code);
1393     } else {
1394         name[code] = '\0';
1395         res = lookup_dentry(name, basep, follow);
1396     }
1397
1398     AFS_GLOCK();
1399     osi_Free(name, PATH_MAX + 1);
1400     AFS_GUNLOCK();
1401     return res;
1402 }
1403 #endif /* AFS_LINUX24_ENV */
1404 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1405
1406 /* afs_linux_readpage
1407  * all reads come through here. A strategy-like read call.
1408  */
1409 static int
1410 afs_linux_readpage(struct file *fp, struct page *pp)
1411 {
1412     int code;
1413     cred_t *credp = crref();
1414 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1415     char *address;
1416     afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1417 #else
1418     ulong address = afs_linux_page_address(pp);
1419     afs_offs_t offset = pageoff(pp);
1420 #endif
1421     uio_t tuio;
1422     struct iovec iovec;
1423     struct inode *ip = FILE_INODE(fp);
1424     int cnt = page_count(pp);
1425     struct vcache *avc = VTOAFS(ip);
1426
1427
1428 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1429     address = kmap(pp);
1430     ClearPageError(pp);
1431 #else
1432     atomic_add(1, &pp->count);
1433     set_bit(PG_locked, &pp->flags);     /* other bits? See mm.h */
1434     clear_bit(PG_error, &pp->flags);
1435 #endif
1436
1437     setup_uio(&tuio, &iovec, (char *)address, offset, PAGE_SIZE, UIO_READ,
1438               AFS_UIOSYS);
1439 #ifdef AFS_LINUX24_ENV
1440     lock_kernel();
1441 #endif
1442     AFS_GLOCK();
1443     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 */
1444     code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1445     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1446                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1447                code);
1448     AFS_GUNLOCK();
1449 #ifdef AFS_LINUX24_ENV
1450     unlock_kernel();
1451 #endif
1452
1453     if (!code) {
1454         if (tuio.uio_resid)     /* zero remainder of page */
1455             memset((void *)(address + (PAGE_SIZE - tuio.uio_resid)), 0,
1456                    tuio.uio_resid);
1457 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1458         flush_dcache_page(pp);
1459         SetPageUptodate(pp);
1460 #else
1461         set_bit(PG_uptodate, &pp->flags);
1462 #endif
1463     }
1464
1465 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1466     kunmap(pp);
1467     UnlockPage(pp);
1468 #else
1469     clear_bit(PG_locked, &pp->flags);
1470     wake_up(&pp->wait);
1471     free_page(address);
1472 #endif
1473
1474     if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1475         struct dcache *tdc;
1476         struct vrequest treq;
1477
1478         AFS_GLOCK();
1479         code = afs_InitReq(&treq, credp);
1480         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1481             tdc = afs_FindDCache(avc, offset);
1482             if (tdc) {
1483                 if (!(tdc->mflags & DFNextStarted))
1484                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1485                 afs_PutDCache(tdc);
1486             }
1487             ReleaseWriteLock(&avc->lock);
1488         }
1489         AFS_GUNLOCK();
1490     }
1491
1492     crfree(credp);
1493     return -code;
1494 }
1495
1496
1497 #if defined(AFS_LINUX24_ENV)
1498 static int
1499 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1500                          unsigned long offset, unsigned int count)
1501 {
1502     struct vcache *vcp = VTOAFS(ip);
1503     char *buffer;
1504     afs_offs_t base;
1505     int code = 0;
1506     cred_t *credp;
1507     uio_t tuio;
1508     struct iovec iovec;
1509     int f_flags = 0;
1510
1511     buffer = kmap(pp) + offset;
1512     base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT)  + offset;
1513
1514     credp = crref();
1515     lock_kernel();
1516     AFS_GLOCK();
1517     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1518                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1519                ICL_TYPE_INT32, 99999);
1520
1521     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1522
1523     code = afs_write(vcp, &tuio, f_flags, credp, 0);
1524
1525     ip->i_size = vcp->m.Length;
1526     ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1527
1528     if (!code) {
1529         struct vrequest treq;
1530
1531         ObtainWriteLock(&vcp->lock, 533);
1532         if (!afs_InitReq(&treq, credp))
1533             code = afs_DoPartialWrite(vcp, &treq);
1534         ReleaseWriteLock(&vcp->lock);
1535     }
1536     code = code ? -code : count - tuio.uio_resid;
1537
1538     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1539                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1540                ICL_TYPE_INT32, code);
1541
1542     AFS_GUNLOCK();
1543     unlock_kernel();
1544     crfree(credp);
1545     kunmap(pp);
1546
1547     return code;
1548 }
1549
1550
1551 static int
1552 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1553 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1554 #else
1555 afs_linux_writepage(struct page *pp)
1556 #endif
1557 {
1558     struct address_space *mapping = pp->mapping;
1559     struct inode *inode;
1560     unsigned long end_index;
1561     unsigned offset = PAGE_CACHE_SIZE;
1562     long status;
1563
1564 #if defined(AFS_LINUX26_ENV)
1565     if (PageReclaim(pp)) {
1566 # if defined(WRITEPAGE_ACTIVATE)
1567         return WRITEPAGE_ACTIVATE;
1568 # else 
1569         return AOP_WRITEPAGE_ACTIVATE;
1570 # endif
1571     }
1572 #else
1573     if (PageLaunder(pp)) {
1574         return(fail_writepage(pp));
1575     }
1576 #endif
1577
1578     inode = (struct inode *)mapping->host;
1579     end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1580
1581     /* easy case */
1582     if (pp->index < end_index)
1583         goto do_it;
1584     /* things got complicated... */
1585     offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1586     /* OK, are we completely out? */
1587     if (pp->index >= end_index + 1 || !offset)
1588         return -EIO;
1589   do_it:
1590     status = afs_linux_writepage_sync(inode, pp, 0, offset);
1591     SetPageUptodate(pp);
1592     UnlockPage(pp);
1593     if (status == offset)
1594         return 0;
1595     else
1596         return status;
1597 }
1598 #else
1599 /* afs_linux_updatepage
1600  * What one would have thought was writepage - write dirty page to file.
1601  * Called from generic_file_write. buffer is still in user space. pagep
1602  * has been filled in with old data if we're updating less than a page.
1603  */
1604 static int
1605 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1606                      unsigned int count, int sync)
1607 {
1608     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1609     u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1610     int code = 0;
1611     cred_t *credp;
1612     uio_t tuio;
1613     struct iovec iovec;
1614
1615     set_bit(PG_locked, &pp->flags);
1616
1617     credp = crref();
1618     AFS_GLOCK();
1619     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1620                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1621                ICL_TYPE_INT32, 99999);
1622     setup_uio(&tuio, &iovec, page_addr + offset,
1623               (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1624               AFS_UIOSYS);
1625
1626     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1627
1628     ip->i_size = vcp->m.Length;
1629     ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1630
1631     if (!code) {
1632         struct vrequest treq;
1633
1634         ObtainWriteLock(&vcp->lock, 533);
1635         vcp->m.Date = osi_Time();   /* set modification time */
1636         if (!afs_InitReq(&treq, credp))
1637             code = afs_DoPartialWrite(vcp, &treq);
1638         ReleaseWriteLock(&vcp->lock);
1639     }
1640
1641     code = code ? -code : count - tuio.uio_resid;
1642     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1643                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1644                ICL_TYPE_INT32, code);
1645
1646     AFS_GUNLOCK();
1647     crfree(credp);
1648
1649     clear_bit(PG_locked, &pp->flags);
1650     return code;
1651 }
1652 #endif
1653
1654 /* afs_linux_permission
1655  * Check access rights - returns error if can't check or permission denied.
1656  */
1657 static int
1658 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1659 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1660 #else
1661 afs_linux_permission(struct inode *ip, int mode)
1662 #endif
1663 {
1664     int code;
1665     cred_t *credp = crref();
1666     int tmp = 0;
1667
1668     AFS_GLOCK();
1669     if (mode & MAY_EXEC)
1670         tmp |= VEXEC;
1671     if (mode & MAY_READ)
1672         tmp |= VREAD;
1673     if (mode & MAY_WRITE)
1674         tmp |= VWRITE;
1675     code = afs_access(VTOAFS(ip), tmp, credp);
1676
1677     AFS_GUNLOCK();
1678     crfree(credp);
1679     return -code;
1680 }
1681
1682 #if defined(AFS_LINUX24_ENV)
1683 static int
1684 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1685                        unsigned to)
1686 {
1687     int code;
1688
1689     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1690                                     offset, to - offset);
1691 #if !defined(AFS_LINUX26_ENV)
1692     kunmap(page);
1693 #endif
1694
1695     return code;
1696 }
1697
1698 static int
1699 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1700                         unsigned to)
1701 {
1702 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1703    call kmap directly instead of relying on us to do it */
1704 #if !defined(AFS_LINUX26_ENV)
1705     kmap(page);
1706 #endif
1707     return 0;
1708 }
1709
1710 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1711 #endif
1712
1713 static struct inode_operations afs_file_iops = {
1714 #if defined(AFS_LINUX26_ENV)
1715   .permission =         afs_linux_permission,
1716   .getattr =            afs_linux_getattr,
1717   .setattr =            afs_notify_change,
1718 #elif defined(AFS_LINUX24_ENV)
1719   .permission =         afs_linux_permission,
1720   .revalidate =         afs_linux_revalidate,
1721   .setattr =            afs_notify_change,
1722 #else
1723   .default_file_ops =   &afs_file_fops,
1724   .readpage =           afs_linux_readpage,
1725   .revalidate =         afs_linux_revalidate,
1726   .updatepage =         afs_linux_updatepage,
1727 #endif
1728 };
1729
1730 #if defined(AFS_LINUX24_ENV)
1731 static struct address_space_operations afs_file_aops = {
1732   .readpage =           afs_linux_readpage,
1733   .writepage =          afs_linux_writepage,
1734   .commit_write =       afs_linux_commit_write,
1735   .prepare_write =      afs_linux_prepare_write,
1736 };
1737 #endif
1738
1739
1740 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1741  * by what sort of operation is allowed.....
1742  */
1743
1744 static struct inode_operations afs_dir_iops = {
1745 #if !defined(AFS_LINUX24_ENV)
1746   .default_file_ops =   &afs_dir_fops,
1747 #else
1748   .setattr =            afs_notify_change,
1749 #endif
1750   .create =             afs_linux_create,
1751   .lookup =             afs_linux_lookup,
1752   .link =               afs_linux_link,
1753   .unlink =             afs_linux_unlink,
1754   .symlink =            afs_linux_symlink,
1755   .mkdir =              afs_linux_mkdir,
1756   .rmdir =              afs_linux_rmdir,
1757   .rename =             afs_linux_rename,
1758 #if defined(AFS_LINUX26_ENV)
1759   .getattr =            afs_linux_getattr,
1760 #else
1761   .revalidate =         afs_linux_revalidate,
1762 #endif
1763   .permission =         afs_linux_permission,
1764 };
1765
1766 /* We really need a separate symlink set of ops, since do_follow_link()
1767  * determines if it _is_ a link by checking if the follow_link op is set.
1768  */
1769 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1770 static int
1771 afs_symlink_filler(struct file *file, struct page *page)
1772 {
1773     struct inode *ip = (struct inode *)page->mapping->host;
1774     char *p = (char *)kmap(page);
1775     int code;
1776
1777     lock_kernel();
1778     AFS_GLOCK();
1779     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1780     AFS_GUNLOCK();
1781
1782     if (code < 0)
1783         goto fail;
1784     p[code] = '\0';             /* null terminate? */
1785     unlock_kernel();
1786
1787     SetPageUptodate(page);
1788     kunmap(page);
1789     UnlockPage(page);
1790     return 0;
1791
1792   fail:
1793     unlock_kernel();
1794
1795     SetPageError(page);
1796     kunmap(page);
1797     UnlockPage(page);
1798     return code;
1799 }
1800
1801 static struct address_space_operations afs_symlink_aops = {
1802   .readpage =   afs_symlink_filler
1803 };
1804 #endif  /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1805
1806 static struct inode_operations afs_symlink_iops = {
1807 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1808   .readlink =           page_readlink,
1809 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1810   .follow_link =        page_follow_link,
1811 #else
1812   .follow_link =        page_follow_link_light,
1813   .put_link =           page_put_link,
1814 #endif
1815 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
1816   .readlink =           afs_linux_readlink,
1817   .follow_link =        afs_linux_follow_link,
1818 #if !defined(AFS_LINUX24_ENV)
1819   .permission =         afs_linux_permission,
1820   .revalidate =         afs_linux_revalidate,
1821 #endif
1822 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1823 #if defined(AFS_LINUX24_ENV)
1824   .setattr =            afs_notify_change,
1825 #endif
1826 };
1827
1828 void
1829 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1830 {
1831         
1832     if (vattr)
1833         vattr2inode(ip, vattr);
1834
1835 /* Reset ops if symlink or directory. */
1836     if (S_ISREG(ip->i_mode)) {
1837         ip->i_op = &afs_file_iops;
1838 #if defined(AFS_LINUX24_ENV)
1839         ip->i_fop = &afs_file_fops;
1840         ip->i_data.a_ops = &afs_file_aops;
1841 #endif
1842
1843     } else if (S_ISDIR(ip->i_mode)) {
1844         ip->i_op = &afs_dir_iops;
1845 #if defined(AFS_LINUX24_ENV)
1846         ip->i_fop = &afs_dir_fops;
1847 #endif
1848
1849     } else if (S_ISLNK(ip->i_mode)) {
1850         ip->i_op = &afs_symlink_iops;
1851 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1852         ip->i_data.a_ops = &afs_symlink_aops;
1853         ip->i_mapping = &ip->i_data;
1854 #endif
1855     }
1856
1857     /* insert_inode_hash(ip);   -- this would make iget() work (if we used it) */
1858 }