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