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