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