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