ab516a17c964ff0e63c86255b032255509ca2180
[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 #if defined(AFS_LINUX26_ENV)
274     lock_kernel();
275 #endif
276     AFS_GLOCK();
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     AFS_GUNLOCK();
411 #if defined(AFS_LINUX26_ENV)
412     unlock_kernel();
413 #endif
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 #ifdef AFS_LINUX24_ENV
571     lock_kernel();
572 #endif
573     AFS_GLOCK();
574     code = afs_open((struct vcache **)&ip, fp->f_flags, credp);
575     AFS_GUNLOCK();
576 #ifdef AFS_LINUX24_ENV
577     unlock_kernel();
578 #endif
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 #ifdef AFS_LINUX24_ENV
599     lock_kernel();
600 #endif
601     AFS_GLOCK();
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     AFS_GUNLOCK();
608 #ifdef AFS_LINUX24_ENV
609     unlock_kernel();
610 #endif
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 #ifdef AFS_LINUX24_ENV
629     lock_kernel();
630 #endif
631     AFS_GLOCK();
632     code = afs_fsync(ITOAFS(ip), credp);
633     AFS_GUNLOCK();
634 #ifdef AFS_LINUX24_ENV
635     unlock_kernel();
636 #endif
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 #ifdef AFS_LINUX24_ENV
781     lock_kernel();
782 #endif
783     AFS_GLOCK();
784
785     if (afs_fakestat_enable && vcp->mvstat == 1 && vcp->mvid
786         && (vcp->states & CMValid) && (vcp->states & CStatd)) {
787         ObtainSharedLock(&afs_xvcache, 680);
788         rootvp = afs_FindVCache(vcp->mvid, 0, 0);
789         ReleaseSharedLock(&afs_xvcache);
790     }
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         if (rootvp)
801             afs_PutVCache(rootvp);
802         AFS_GUNLOCK();
803 #ifdef AFS_LINUX24_ENV
804         unlock_kernel();
805 #endif
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     AFS_GUNLOCK();
815 #ifdef AFS_LINUX24_ENV
816     unlock_kernel();
817 #endif
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 #ifdef AFS_LINUX24_ENV
860     lock_kernel();
861 #endif
862     AFS_GLOCK();
863
864     vcp = ITOAFS(dp->d_inode);
865     parentvcp = ITOAFS(dp->d_parent->d_inode);
866
867     /* If it's a negative dentry, then there's nothing to do. */
868     if (!vcp || !parentvcp)
869         goto done;
870
871     /* If it is the AFS root, then there's no chance it needs 
872      * revalidating */
873     if (vcp == afs_globalVp) {
874         bad_dentry = 0;
875         goto done;
876     }
877
878     if ((code = afs_InitReq(&treq, credp)))
879         goto done;
880
881     Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
882     name = sysState.name;
883
884     /* First try looking up the DNLC */
885     if ((lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK))) {
886         /* Verify that the dentry does not point to an old inode */
887         if (vcp != lookupvcp)
888             goto done;
889         /* Check and correct mvid */
890         if (*name != '/' && vcp->mvstat == 2)
891             check_bad_parent(dp);
892         vcache2inode(vcp);
893         bad_dentry = 0;
894         goto done;
895     }
896
897     /* A DNLC lookup failure cannot be trusted. Try a real lookup. 
898        Make sure to try the real name and not the @sys expansion; 
899        afs_lookup will expand @sys itself. */
900   
901     code = afs_lookup(parentvcp, dp->d_name.name, &lookupvcp, credp);
902
903     /* Verify that the dentry does not point to an old inode */
904     if (vcp != lookupvcp)
905         goto done;
906
907     bad_dentry = 0;
908
909   done:
910     /* Clean up */
911     if (lookupvcp)
912         afs_PutVCache(lookupvcp);
913     if (sysState.allocked)
914         osi_FreeLargeSpace(name);
915
916     AFS_GUNLOCK();
917
918     if (bad_dentry) {
919         shrink_dcache_parent(dp);
920         d_drop(dp);
921     }
922
923 #ifdef AFS_LINUX24_ENV
924     unlock_kernel();
925 #endif
926     crfree(credp);
927
928     return !bad_dentry;
929 }
930
931 #if !defined(AFS_LINUX26_ENV)
932 /* afs_dentry_iput */
933 static void
934 afs_dentry_iput(struct dentry *dp, struct inode *ip)
935 {
936     int isglock;
937
938     if (ICL_SETACTIVE(afs_iclSetp)) {
939         isglock = ISAFS_GLOCK();
940         if (!isglock) AFS_GLOCK();
941         afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYIPUT, ICL_TYPE_POINTER, ip,
942                    ICL_TYPE_STRING, dp->d_parent->d_name.name,
943                    ICL_TYPE_STRING, dp->d_name.name);
944         if (!isglock) AFS_GUNLOCK();
945     }
946
947     osi_iput(ip);
948 }
949 #endif
950
951 static int
952 afs_dentry_delete(struct dentry *dp)
953 {
954     int isglock;
955     if (ICL_SETACTIVE(afs_iclSetp)) {
956         isglock = ISAFS_GLOCK();
957         if (!isglock) AFS_GLOCK();
958         afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYDELETE, ICL_TYPE_POINTER,
959                    dp->d_inode, ICL_TYPE_STRING, dp->d_parent->d_name.name,
960                    ICL_TYPE_STRING, dp->d_name.name);
961         if (!isglock) AFS_GUNLOCK();
962     }
963
964     if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
965         return 1;               /* bad inode? */
966
967     return 0;
968 }
969
970 struct dentry_operations afs_dentry_operations = {
971   .d_revalidate =       afs_linux_dentry_revalidate,
972   .d_delete =           afs_dentry_delete,
973 #if !defined(AFS_LINUX26_ENV)
974   .d_iput =             afs_dentry_iput,
975 #endif
976 };
977
978 /**********************************************************************
979  * AFS Linux inode operations
980  **********************************************************************/
981
982 /* afs_linux_create
983  *
984  * Merely need to set enough of vattr to get us through the create. Note
985  * that the higher level code (open_namei) will take care of any tuncation
986  * explicitly. Exclusive open is also taken care of in open_namei.
987  *
988  * name is in kernel space at this point.
989  */
990 int
991 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
992 {
993     int code;
994     cred_t *credp = crref();
995     struct vattr vattr;
996     enum vcexcl excl;
997     const char *name = dp->d_name.name;
998     struct inode *ip;
999
1000     VATTR_NULL(&vattr);
1001     vattr.va_mode = mode;
1002
1003 #if defined(AFS_LINUX26_ENV)
1004     lock_kernel();
1005 #endif
1006     AFS_GLOCK();
1007     code =
1008         afs_create(ITOAFS(dip), name, &vattr, NONEXCL, mode,
1009                    (struct vcache **)&ip, credp);
1010
1011     if (!code) {
1012         vattr2inode(ip, &vattr);
1013         /* Reset ops if symlink or directory. */
1014 #if defined(AFS_LINUX24_ENV)
1015         if (S_ISREG(ip->i_mode)) {
1016             ip->i_op = &afs_file_iops;
1017             ip->i_fop = &afs_file_fops;
1018             ip->i_data.a_ops = &afs_file_aops;
1019         } else if (S_ISDIR(ip->i_mode)) {
1020             ip->i_op = &afs_dir_iops;
1021             ip->i_fop = &afs_dir_fops;
1022         } else if (S_ISLNK(ip->i_mode)) {
1023             ip->i_op = &afs_symlink_iops;
1024             ip->i_data.a_ops = &afs_symlink_aops;
1025             ip->i_mapping = &ip->i_data;
1026         } else
1027             printk("afs_linux_create: FIXME\n");
1028 #else
1029         if (S_ISDIR(ip->i_mode))
1030             ip->i_op = &afs_dir_iops;
1031         else if (S_ISLNK(ip->i_mode))
1032             ip->i_op = &afs_symlink_iops;
1033 #endif
1034
1035         dp->d_op = &afs_dentry_operations;
1036         dp->d_time = jiffies;
1037         d_instantiate(dp, ip);
1038     }
1039
1040     AFS_GUNLOCK();
1041 #if defined(AFS_LINUX26_ENV)
1042     unlock_kernel();
1043 #endif
1044     crfree(credp);
1045     return -code;
1046 }
1047
1048 /* afs_linux_lookup */
1049 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1050 struct dentry *
1051 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1052 #else
1053 int
1054 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1055 #endif
1056 {
1057     int code = 0;
1058     cred_t *credp = crref();
1059     struct vcache *vcp = NULL;
1060     const char *comp = dp->d_name.name;
1061
1062 #if defined(AFS_LINUX26_ENV)
1063     lock_kernel();
1064 #endif
1065     AFS_GLOCK();
1066     code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1067     AFS_GUNLOCK();
1068     
1069     if (vcp) {
1070         struct inode *ip = AFSTOI(vcp);
1071         /* Reset ops if symlink or directory. */
1072 #if defined(AFS_LINUX24_ENV)
1073         if (S_ISREG(ip->i_mode)) {
1074             ip->i_op = &afs_file_iops;
1075             ip->i_fop = &afs_file_fops;
1076             ip->i_data.a_ops = &afs_file_aops;
1077         } else if (S_ISDIR(ip->i_mode)) {
1078             ip->i_op = &afs_dir_iops;
1079             ip->i_fop = &afs_dir_fops;
1080         } else if (S_ISLNK(ip->i_mode)) {
1081             ip->i_op = &afs_symlink_iops;
1082             ip->i_data.a_ops = &afs_symlink_aops;
1083             ip->i_mapping = &ip->i_data;
1084         } else
1085             printk
1086                 ("afs_linux_lookup: ip->i_mode 0x%x  dp->d_name.name %s  code %d\n",
1087                  ip->i_mode, dp->d_name.name, code);
1088 #else
1089         if (S_ISDIR(ip->i_mode))
1090             ip->i_op = &afs_dir_iops;
1091         else if (S_ISLNK(ip->i_mode))
1092             ip->i_op = &afs_symlink_iops;
1093 #endif
1094     }
1095     dp->d_time = jiffies;
1096     dp->d_op = &afs_dentry_operations;
1097     d_add(dp, AFSTOI(vcp));
1098
1099 #if defined(AFS_LINUX26_ENV)
1100     unlock_kernel();
1101 #endif
1102     crfree(credp);
1103
1104     /* It's ok for the file to not be found. That's noted by the caller by
1105      * seeing that the dp->d_inode field is NULL.
1106      */
1107 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1108     if (code == ENOENT)
1109         return ERR_PTR(0);
1110     else
1111         return ERR_PTR(-code);
1112 #else
1113     if (code == ENOENT)
1114         code = 0;
1115     return -code;
1116 #endif
1117 }
1118
1119 int
1120 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1121 {
1122     int code;
1123     cred_t *credp = crref();
1124     const char *name = newdp->d_name.name;
1125     struct inode *oldip = olddp->d_inode;
1126
1127     /* If afs_link returned the vnode, we could instantiate the
1128      * dentry. Since it's not, we drop this one and do a new lookup.
1129      */
1130     d_drop(newdp);
1131
1132     AFS_GLOCK();
1133     code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1134
1135     AFS_GUNLOCK();
1136     crfree(credp);
1137     return -code;
1138 }
1139
1140 int
1141 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1142 {
1143     int code;
1144     cred_t *credp = crref();
1145     const char *name = dp->d_name.name;
1146
1147 #if defined(AFS_LINUX26_ENV)
1148     lock_kernel();
1149 #endif
1150     AFS_GLOCK();
1151     code = afs_remove(ITOAFS(dip), name, credp);
1152     AFS_GUNLOCK();
1153     if (!code)
1154         d_drop(dp);
1155 #if defined(AFS_LINUX26_ENV)
1156     unlock_kernel();
1157 #endif
1158     crfree(credp);
1159     return -code;
1160 }
1161
1162
1163 int
1164 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1165 {
1166     int code;
1167     cred_t *credp = crref();
1168     struct vattr vattr;
1169     const char *name = dp->d_name.name;
1170
1171     /* If afs_symlink 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(dp);
1175
1176     AFS_GLOCK();
1177     VATTR_NULL(&vattr);
1178     code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1179     AFS_GUNLOCK();
1180     crfree(credp);
1181     return -code;
1182 }
1183
1184 int
1185 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1186 {
1187     int code;
1188     cred_t *credp = crref();
1189     struct vcache *tvcp = NULL;
1190     struct vattr vattr;
1191     const char *name = dp->d_name.name;
1192
1193 #if defined(AFS_LINUX26_ENV)
1194     lock_kernel();
1195 #endif
1196     AFS_GLOCK();
1197     VATTR_NULL(&vattr);
1198     vattr.va_mask = ATTR_MODE;
1199     vattr.va_mode = mode;
1200     code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1201     AFS_GUNLOCK();
1202
1203     if (tvcp) {
1204         tvcp->v.v_op = &afs_dir_iops;
1205 #if defined(AFS_LINUX24_ENV)
1206         tvcp->v.v_fop = &afs_dir_fops;
1207 #endif
1208         dp->d_op = &afs_dentry_operations;
1209         dp->d_time = jiffies;
1210         d_instantiate(dp, AFSTOI(tvcp));
1211     }
1212
1213 #if defined(AFS_LINUX26_ENV)
1214     unlock_kernel();
1215 #endif
1216     crfree(credp);
1217     return -code;
1218 }
1219
1220 int
1221 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1222 {
1223     int code;
1224     cred_t *credp = crref();
1225     const char *name = dp->d_name.name;
1226
1227 #if defined(AFS_LINUX26_ENV)
1228     lock_kernel();
1229 #endif
1230     AFS_GLOCK();
1231     code = afs_rmdir(ITOAFS(dip), name, credp);
1232     AFS_GUNLOCK();
1233
1234     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1235      * that failed because a directory is not empty. So, we map
1236      * EEXIST to ENOTEMPTY on linux.
1237      */
1238     if (code == EEXIST) {
1239         code = ENOTEMPTY;
1240     }
1241
1242     if (!code) {
1243         d_drop(dp);
1244     }
1245
1246 #if defined(AFS_LINUX26_ENV)
1247     unlock_kernel();
1248 #endif
1249     crfree(credp);
1250     return -code;
1251 }
1252
1253
1254
1255 int
1256 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1257                  struct inode *newip, struct dentry *newdp)
1258 {
1259     int code;
1260     cred_t *credp = crref();
1261     const char *oldname = olddp->d_name.name;
1262     const char *newname = newdp->d_name.name;
1263
1264 #if defined(AFS_LINUX26_ENV)
1265     lock_kernel();
1266 #endif
1267     /* Remove old and new entries from name hash. New one will change below.
1268      * While it's optimal to catch failures and re-insert newdp into hash,
1269      * it's also error prone and in that case we're already dealing with error
1270      * cases. Let another lookup put things right, if need be.
1271      */
1272 #if defined(AFS_LINUX26_ENV)
1273     if (!d_unhashed(olddp))
1274         d_drop(olddp);
1275     if (!d_unhashed(newdp))
1276         d_drop(newdp);
1277 #else
1278     if (!list_empty(&olddp->d_hash))
1279         d_drop(olddp);
1280     if (!list_empty(&newdp->d_hash))
1281         d_drop(newdp);
1282 #endif
1283     AFS_GLOCK();
1284     code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip), newname, credp);
1285     AFS_GUNLOCK();
1286
1287     if (!code) {
1288         /* update time so it doesn't expire immediately */
1289         newdp->d_time = jiffies;
1290         d_move(olddp, newdp);
1291     }
1292
1293 #if defined(AFS_LINUX26_ENV)
1294     unlock_kernel();
1295 #endif
1296
1297     crfree(credp);
1298     return -code;
1299 }
1300
1301
1302 /* afs_linux_ireadlink 
1303  * Internal readlink which can return link contents to user or kernel space.
1304  * Note that the buffer is NOT supposed to be null-terminated.
1305  */
1306 static int
1307 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1308 {
1309     int code;
1310     cred_t *credp = crref();
1311     uio_t tuio;
1312     struct iovec iov;
1313
1314     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1315     code = afs_readlink(ITOAFS(ip), &tuio, credp);
1316     crfree(credp);
1317
1318     if (!code)
1319         return maxlen - tuio.uio_resid;
1320     else
1321         return -code;
1322 }
1323
1324 #if !defined(AFS_LINUX24_ENV)
1325 /* afs_linux_readlink 
1326  * Fill target (which is in user space) with contents of symlink.
1327  */
1328 int
1329 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1330 {
1331     int code;
1332     struct inode *ip = dp->d_inode;
1333
1334     AFS_GLOCK();
1335     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1336     AFS_GUNLOCK();
1337     return code;
1338 }
1339
1340
1341 /* afs_linux_follow_link
1342  * a file system dependent link following routine.
1343  */
1344 struct dentry *
1345 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1346                       unsigned int follow)
1347 {
1348     int code = 0;
1349     char *name;
1350     struct dentry *res;
1351
1352
1353     AFS_GLOCK();
1354     name = osi_Alloc(PATH_MAX + 1);
1355     if (!name) {
1356         AFS_GUNLOCK();
1357         dput(basep);
1358         return ERR_PTR(-EIO);
1359     }
1360
1361     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1362     AFS_GUNLOCK();
1363
1364     if (code < 0) {
1365         dput(basep);
1366         res = ERR_PTR(code);
1367     } else {
1368         name[code] = '\0';
1369         res = lookup_dentry(name, basep, follow);
1370     }
1371
1372     AFS_GLOCK();
1373     osi_Free(name, PATH_MAX + 1);
1374     AFS_GUNLOCK();
1375     return res;
1376 }
1377 #endif
1378
1379 /* afs_linux_readpage
1380  * all reads come through here. A strategy-like read call.
1381  */
1382 int
1383 afs_linux_readpage(struct file *fp, struct page *pp)
1384 {
1385     int code;
1386     cred_t *credp = crref();
1387 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1388     char *address;
1389     afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1390 #else
1391     ulong address = afs_linux_page_address(pp);
1392     afs_offs_t offset = pageoff(pp);
1393 #endif
1394     uio_t tuio;
1395     struct iovec iovec;
1396     struct inode *ip = FILE_INODE(fp);
1397     int cnt = page_count(pp);
1398     struct vcache *avc = ITOAFS(ip);
1399
1400
1401 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1402     address = kmap(pp);
1403     ClearPageError(pp);
1404 #else
1405     atomic_add(1, &pp->count);
1406     set_bit(PG_locked, &pp->flags);     /* other bits? See mm.h */
1407     clear_bit(PG_error, &pp->flags);
1408 #endif
1409
1410     setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1411               AFS_UIOSYS);
1412 #ifdef AFS_LINUX24_ENV
1413     lock_kernel();
1414 #endif
1415     AFS_GLOCK();
1416     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 */
1417     code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1418     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1419                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1420                code);
1421     AFS_GUNLOCK();
1422 #ifdef AFS_LINUX24_ENV
1423     unlock_kernel();
1424 #endif
1425
1426     if (!code) {
1427         if (tuio.uio_resid)     /* zero remainder of page */
1428             memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1429                    tuio.uio_resid);
1430 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1431         flush_dcache_page(pp);
1432         SetPageUptodate(pp);
1433 #else
1434         set_bit(PG_uptodate, &pp->flags);
1435 #endif
1436     }
1437
1438 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1439     kunmap(pp);
1440     UnlockPage(pp);
1441 #else
1442     clear_bit(PG_locked, &pp->flags);
1443     wake_up(&pp->wait);
1444     free_page(address);
1445 #endif
1446
1447     if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1448         struct dcache *tdc;
1449         struct vrequest treq;
1450
1451         AFS_GLOCK();
1452         code = afs_InitReq(&treq, credp);
1453         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1454             tdc = afs_FindDCache(avc, offset);
1455             if (tdc) {
1456                 if (!(tdc->mflags & DFNextStarted))
1457                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1458                 afs_PutDCache(tdc);
1459             }
1460             ReleaseWriteLock(&avc->lock);
1461         }
1462         AFS_GUNLOCK();
1463     }
1464
1465     crfree(credp);
1466     return -code;
1467 }
1468
1469 #if defined(AFS_LINUX24_ENV)
1470 int
1471 afs_linux_writepage(struct page *pp)
1472 {
1473     struct address_space *mapping = pp->mapping;
1474     struct inode *inode;
1475     unsigned long end_index;
1476     unsigned offset = PAGE_CACHE_SIZE;
1477     long status;
1478
1479     inode = (struct inode *)mapping->host;
1480     end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1481
1482     /* easy case */
1483     if (pp->index < end_index)
1484         goto do_it;
1485     /* things got complicated... */
1486     offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1487     /* OK, are we completely out? */
1488     if (pp->index >= end_index + 1 || !offset)
1489         return -EIO;
1490   do_it:
1491     AFS_GLOCK();
1492     status = afs_linux_writepage_sync(inode, pp, 0, offset);
1493     AFS_GUNLOCK();
1494     SetPageUptodate(pp);
1495     UnlockPage(pp);
1496     if (status == offset)
1497         return 0;
1498     else
1499         return status;
1500 }
1501 #endif
1502
1503 /* afs_linux_permission
1504  * Check access rights - returns error if can't check or permission denied.
1505  */
1506 int
1507 afs_linux_permission(struct inode *ip, int mode)
1508 {
1509     int code;
1510     cred_t *credp = crref();
1511     int tmp = 0;
1512
1513     AFS_GLOCK();
1514     if (mode & MAY_EXEC)
1515         tmp |= VEXEC;
1516     if (mode & MAY_READ)
1517         tmp |= VREAD;
1518     if (mode & MAY_WRITE)
1519         tmp |= VWRITE;
1520     code = afs_access(ITOAFS(ip), tmp, credp);
1521
1522     AFS_GUNLOCK();
1523     crfree(credp);
1524     return -code;
1525 }
1526
1527
1528 #if defined(AFS_LINUX24_ENV)
1529 int
1530 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1531                          unsigned long offset, unsigned int count)
1532 {
1533     struct vcache *vcp = ITOAFS(ip);
1534     char *buffer;
1535     afs_offs_t base;
1536     int code = 0;
1537     cred_t *credp;
1538     uio_t tuio;
1539     struct iovec iovec;
1540     int f_flags = 0;
1541
1542     buffer = kmap(pp) + offset;
1543     base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1544
1545     credp = crref();
1546     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1547                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1548                ICL_TYPE_INT32, 99999);
1549
1550     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1551
1552     code = afs_write(vcp, &tuio, f_flags, credp, 0);
1553
1554     vcache2inode(vcp);
1555
1556     if (!code
1557         && afs_stats_cmperf.cacheCurrDirtyChunks >
1558         afs_stats_cmperf.cacheMaxDirtyChunks) {
1559         struct vrequest treq;
1560
1561         ObtainWriteLock(&vcp->lock, 533);
1562         if (!afs_InitReq(&treq, credp))
1563             code = afs_DoPartialWrite(vcp, &treq);
1564         ReleaseWriteLock(&vcp->lock);
1565     }
1566     code = code ? -code : count - tuio.uio_resid;
1567
1568     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1569                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1570                ICL_TYPE_INT32, code);
1571
1572     crfree(credp);
1573     kunmap(pp);
1574
1575     return code;
1576 }
1577
1578 static int
1579 afs_linux_updatepage(struct file *file, struct page *page,
1580                      unsigned long offset, unsigned int count)
1581 {
1582     struct dentry *dentry = file->f_dentry;
1583
1584     return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1585 }
1586 #else
1587 /* afs_linux_updatepage
1588  * What one would have thought was writepage - write dirty page to file.
1589  * Called from generic_file_write. buffer is still in user space. pagep
1590  * has been filled in with old data if we're updating less than a page.
1591  */
1592 int
1593 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1594                      unsigned int count, int sync)
1595 {
1596     struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1597     u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1598     int code = 0;
1599     cred_t *credp;
1600     uio_t tuio;
1601     struct iovec iovec;
1602
1603     set_bit(PG_locked, &pp->flags);
1604
1605     credp = crref();
1606     AFS_GLOCK();
1607     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1608                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1609                ICL_TYPE_INT32, 99999);
1610     setup_uio(&tuio, &iovec, page_addr + offset,
1611               (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1612               AFS_UIOSYS);
1613
1614     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1615
1616     vcache2inode(vcp);
1617
1618     code = code ? -code : count - tuio.uio_resid;
1619     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1620                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1621                ICL_TYPE_INT32, code);
1622
1623     AFS_GUNLOCK();
1624     crfree(credp);
1625
1626     clear_bit(PG_locked, &pp->flags);
1627     return code;
1628 }
1629 #endif
1630
1631 #if defined(AFS_LINUX24_ENV)
1632 static int
1633 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1634                        unsigned to)
1635 {
1636     int code;
1637
1638     lock_kernel();
1639     AFS_GLOCK();
1640     code = afs_linux_updatepage(file, page, offset, to - offset);
1641     AFS_GUNLOCK();
1642     unlock_kernel();
1643     kunmap(page);
1644
1645     return code;
1646 }
1647
1648 static int
1649 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1650                         unsigned to)
1651 {
1652     kmap(page);
1653     return 0;
1654 }
1655
1656 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1657 #endif
1658
1659 struct inode_operations afs_file_iops = {
1660 #if defined(AFS_LINUX26_ENV)
1661   .permission =         afs_linux_permission,
1662   .getattr =            afs_linux_getattr,
1663   .setattr =            afs_notify_change,
1664 #elif defined(AFS_LINUX24_ENV)
1665   .permission =         afs_linux_permission,
1666   .revalidate =         afs_linux_revalidate,
1667   .setattr =            afs_notify_change,
1668 #else
1669   .default_file_ops =   &afs_file_fops,
1670   .readpage =           afs_linux_readpage,
1671   .revalidate =         afs_linux_revalidate,
1672   .updatepage =         afs_linux_updatepage,
1673 #endif
1674 };
1675
1676 #if defined(AFS_LINUX24_ENV)
1677 struct address_space_operations afs_file_aops = {
1678   .readpage =           afs_linux_readpage,
1679   .writepage =          afs_linux_writepage,
1680   .commit_write =       afs_linux_commit_write,
1681   .prepare_write =      afs_linux_prepare_write,
1682 };
1683 #endif
1684
1685
1686 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1687  * by what sort of operation is allowed.....
1688  */
1689
1690 struct inode_operations afs_dir_iops = {
1691 #if !defined(AFS_LINUX24_ENV)
1692   .default_file_ops =   &afs_dir_fops,
1693 #else
1694   .setattr =            afs_notify_change,
1695 #endif
1696   .create =             afs_linux_create,
1697   .lookup =             afs_linux_lookup,
1698   .link =               afs_linux_link,
1699   .unlink =             afs_linux_unlink,
1700   .symlink =            afs_linux_symlink,
1701   .mkdir =              afs_linux_mkdir,
1702   .rmdir =              afs_linux_rmdir,
1703   .rename =             afs_linux_rename,
1704 #if defined(AFS_LINUX26_ENV)
1705   .getattr =            afs_linux_getattr,
1706 #else
1707   .revalidate =         afs_linux_revalidate,
1708 #endif
1709   .permission =         afs_linux_permission,
1710 };
1711
1712 /* We really need a separate symlink set of ops, since do_follow_link()
1713  * determines if it _is_ a link by checking if the follow_link op is set.
1714  */
1715 #if defined(AFS_LINUX24_ENV)
1716 static int
1717 afs_symlink_filler(struct file *file, struct page *page)
1718 {
1719     struct inode *ip = (struct inode *)page->mapping->host;
1720     char *p = (char *)kmap(page);
1721     int code;
1722
1723     lock_kernel();
1724     AFS_GLOCK();
1725     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1726     AFS_GUNLOCK();
1727
1728     if (code < 0)
1729         goto fail;
1730     p[code] = '\0';             /* null terminate? */
1731     unlock_kernel();
1732
1733     SetPageUptodate(page);
1734     kunmap(page);
1735     UnlockPage(page);
1736     return 0;
1737
1738   fail:
1739     unlock_kernel();
1740
1741     SetPageError(page);
1742     kunmap(page);
1743     UnlockPage(page);
1744     return code;
1745 }
1746
1747 struct address_space_operations afs_symlink_aops = {
1748   .readpage =   afs_symlink_filler
1749 };
1750 #endif
1751
1752 struct inode_operations afs_symlink_iops = {
1753 #if defined(AFS_LINUX24_ENV)
1754   .readlink =           page_readlink,
1755   .follow_link =        page_follow_link,
1756   .setattr =            afs_notify_change,
1757 #else
1758   .readlink =           afs_linux_readlink,
1759   .follow_link =        afs_linux_follow_link,
1760   .permission =         afs_linux_permission,
1761   .revalidate =         afs_linux_revalidate,
1762 #endif
1763 };