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