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