linux26-rename-more-cleanup-20050116
[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 #ifdef AFS_LINUX26_ENV
760   .sendfile =   generic_file_sendfile,
761 #endif
762   .release =    afs_linux_release,
763   .fsync =      afs_linux_fsync,
764   .lock =       afs_linux_lock,
765 };
766
767
768 /**********************************************************************
769  * AFS Linux dentry operations
770  **********************************************************************/
771
772 /* afs_linux_revalidate
773  * Ensure vcache is stat'd before use. Return 0 if entry is valid.
774  */
775 static int
776 afs_linux_revalidate(struct dentry *dp)
777 {
778     int code;
779     cred_t *credp;
780     struct vrequest treq;
781     struct vcache *vcp = ITOAFS(dp->d_inode);
782     struct vcache *rootvp = NULL;
783
784 #ifdef AFS_LINUX24_ENV
785     lock_kernel();
786 #endif
787     AFS_GLOCK();
788
789     if (afs_fakestat_enable && vcp->mvstat == 1 && vcp->mvid
790         && (vcp->states & CMValid) && (vcp->states & CStatd)) {
791         ObtainSharedLock(&afs_xvcache, 680);
792         rootvp = afs_FindVCache(vcp->mvid, 0, 0);
793         ReleaseSharedLock(&afs_xvcache);
794     }
795
796     /* Make this a fast path (no crref), since it's called so often. */
797     if (vcp->states & CStatd) {
798         if (*dp->d_name.name != '/' && vcp->mvstat == 2)        /* root vnode */
799             check_bad_parent(dp);       /* check and correct mvid */
800         if (rootvp)
801             vcache2fakeinode(rootvp, vcp);
802         else
803             vcache2inode(vcp);
804         if (rootvp)
805             afs_PutVCache(rootvp);
806         AFS_GUNLOCK();
807 #ifdef AFS_LINUX24_ENV
808         unlock_kernel();
809 #endif
810         return 0;
811     }
812
813     credp = crref();
814     code = afs_InitReq(&treq, credp);
815     if (!code)
816         code = afs_VerifyVCache(vcp, &treq);
817
818     AFS_GUNLOCK();
819 #ifdef AFS_LINUX24_ENV
820     unlock_kernel();
821 #endif
822     crfree(credp);
823
824     return -code;
825 }
826
827 #if defined(AFS_LINUX26_ENV)
828 static int
829 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
830 {
831         int err = afs_linux_revalidate(dentry);
832         if (!err)
833                 generic_fillattr(dentry->d_inode, stat);
834         return err;
835 }
836 #endif
837
838 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
839  * In kernels 2.2.10 and above, we are passed an additional flags var which
840  * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
841  * we are advised to follow the entry if it is a link or to make sure that 
842  * it is a directory. But since the kernel itself checks these possibilities
843  * later on, we shouldn't have to do it until later. Perhaps in the future..
844  */
845 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
846 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
847 static int
848 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
849 #else
850 static int
851 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
852 #endif
853 #else
854 static int
855 afs_linux_dentry_revalidate(struct dentry *dp)
856 #endif
857 {
858     char *name = NULL;
859     cred_t *credp = crref();
860     struct vrequest treq;
861     struct vcache *lookupvcp = NULL;
862     int code, bad_dentry = 1;
863     struct sysname_info sysState;
864     struct vcache *vcp, *parentvcp;
865
866     sysState.allocked = 0;
867
868 #ifdef AFS_LINUX24_ENV
869     lock_kernel();
870 #endif
871     AFS_GLOCK();
872
873     vcp = ITOAFS(dp->d_inode);
874     parentvcp = ITOAFS(dp->d_parent->d_inode);
875
876     /* If it's a negative dentry, then there's nothing to do. */
877     if (!vcp || !parentvcp)
878         goto done;
879
880     /* If it is the AFS root, then there's no chance it needs 
881      * revalidating */
882     if (vcp == afs_globalVp) {
883         bad_dentry = 0;
884         goto done;
885     }
886
887     if ((code = afs_InitReq(&treq, credp)))
888         goto done;
889
890     Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
891     name = sysState.name;
892
893     /* First try looking up the DNLC */
894     if ((lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK))) {
895         /* Verify that the dentry does not point to an old inode */
896         if (vcp != lookupvcp)
897             goto done;
898         /* Check and correct mvid */
899         if (*name != '/' && vcp->mvstat == 2)
900             check_bad_parent(dp);
901         vcache2inode(vcp);
902         bad_dentry = 0;
903         goto done;
904     }
905
906     /* A DNLC lookup failure cannot be trusted. Try a real lookup. 
907        Make sure to try the real name and not the @sys expansion; 
908        afs_lookup will expand @sys itself. */
909   
910     code = afs_lookup(parentvcp, dp->d_name.name, &lookupvcp, credp);
911
912     /* Verify that the dentry does not point to an old inode */
913     if (vcp != lookupvcp)
914         goto done;
915
916     bad_dentry = 0;
917
918   done:
919     /* Clean up */
920     if (lookupvcp)
921         afs_PutVCache(lookupvcp);
922     if (sysState.allocked)
923         osi_FreeLargeSpace(name);
924
925     AFS_GUNLOCK();
926
927     if (bad_dentry) {
928         shrink_dcache_parent(dp);
929         d_drop(dp);
930     }
931
932 #ifdef AFS_LINUX24_ENV
933     unlock_kernel();
934 #endif
935     crfree(credp);
936
937     return !bad_dentry;
938 }
939
940 #if !defined(AFS_LINUX26_ENV)
941 /* afs_dentry_iput */
942 static void
943 afs_dentry_iput(struct dentry *dp, struct inode *ip)
944 {
945     int isglock;
946
947     if (ICL_SETACTIVE(afs_iclSetp)) {
948         isglock = ISAFS_GLOCK();
949         if (!isglock) AFS_GLOCK();
950         afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYIPUT, ICL_TYPE_POINTER, ip,
951                    ICL_TYPE_STRING, dp->d_parent->d_name.name,
952                    ICL_TYPE_STRING, dp->d_name.name);
953         if (!isglock) AFS_GUNLOCK();
954     }
955
956     osi_iput(ip);
957 }
958 #endif
959
960 static int
961 afs_dentry_delete(struct dentry *dp)
962 {
963     int isglock;
964     if (ICL_SETACTIVE(afs_iclSetp)) {
965         isglock = ISAFS_GLOCK();
966         if (!isglock) AFS_GLOCK();
967         afs_Trace3(afs_iclSetp, CM_TRACE_DENTRYDELETE, ICL_TYPE_POINTER,
968                    dp->d_inode, ICL_TYPE_STRING, dp->d_parent->d_name.name,
969                    ICL_TYPE_STRING, dp->d_name.name);
970         if (!isglock) AFS_GUNLOCK();
971     }
972
973     if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
974         return 1;               /* bad inode? */
975
976     return 0;
977 }
978
979 struct dentry_operations afs_dentry_operations = {
980   .d_revalidate =       afs_linux_dentry_revalidate,
981   .d_delete =           afs_dentry_delete,
982 #if !defined(AFS_LINUX26_ENV)
983   .d_iput =             afs_dentry_iput,
984 #endif
985 };
986
987 /**********************************************************************
988  * AFS Linux inode operations
989  **********************************************************************/
990
991 /* afs_linux_create
992  *
993  * Merely need to set enough of vattr to get us through the create. Note
994  * that the higher level code (open_namei) will take care of any tuncation
995  * explicitly. Exclusive open is also taken care of in open_namei.
996  *
997  * name is in kernel space at this point.
998  */
999 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1000 int
1001 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1002                  struct nameidata *nd)
1003 #else
1004 int
1005 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1006 #endif
1007 {
1008     int code;
1009     cred_t *credp = crref();
1010     struct vattr vattr;
1011     const char *name = dp->d_name.name;
1012     struct inode *ip;
1013
1014     VATTR_NULL(&vattr);
1015     vattr.va_mode = mode;
1016
1017 #if defined(AFS_LINUX26_ENV)
1018     lock_kernel();
1019 #endif
1020     AFS_GLOCK();
1021     code =
1022         afs_create(ITOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1023                    (struct vcache **)&ip, credp);
1024
1025     if (!code) {
1026         vattr2inode(ip, &vattr);
1027         /* Reset ops if symlink or directory. */
1028 #if defined(AFS_LINUX24_ENV)
1029         if (S_ISREG(ip->i_mode)) {
1030             ip->i_op = &afs_file_iops;
1031             ip->i_fop = &afs_file_fops;
1032             ip->i_data.a_ops = &afs_file_aops;
1033         } else if (S_ISDIR(ip->i_mode)) {
1034             ip->i_op = &afs_dir_iops;
1035             ip->i_fop = &afs_dir_fops;
1036         } else if (S_ISLNK(ip->i_mode)) {
1037             ip->i_op = &afs_symlink_iops;
1038             ip->i_data.a_ops = &afs_symlink_aops;
1039             ip->i_mapping = &ip->i_data;
1040         } else
1041             printk("afs_linux_create: FIXME\n");
1042 #else
1043         if (S_ISDIR(ip->i_mode))
1044             ip->i_op = &afs_dir_iops;
1045         else if (S_ISLNK(ip->i_mode))
1046             ip->i_op = &afs_symlink_iops;
1047 #endif
1048
1049         dp->d_op = &afs_dentry_operations;
1050         d_instantiate(dp, ip);
1051     }
1052
1053     AFS_GUNLOCK();
1054 #if defined(AFS_LINUX26_ENV)
1055     unlock_kernel();
1056 #endif
1057     crfree(credp);
1058     return -code;
1059 }
1060
1061 /* afs_linux_lookup */
1062 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1063 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1064 struct dentry *
1065 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1066                  struct nameidata *nd)
1067 #else
1068 struct dentry *
1069 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1070 #endif
1071 #else
1072 int
1073 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1074 #endif
1075 {
1076     int code = 0;
1077     cred_t *credp = crref();
1078     struct vcache *vcp = NULL;
1079     const char *comp = dp->d_name.name;
1080
1081 #if defined(AFS_LINUX26_ENV)
1082     lock_kernel();
1083 #endif
1084     AFS_GLOCK();
1085     code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1086     AFS_GUNLOCK();
1087     
1088     if (vcp) {
1089         struct inode *ip = AFSTOI(vcp);
1090         /* Reset ops if symlink or directory. */
1091 #if defined(AFS_LINUX24_ENV)
1092         if (S_ISREG(ip->i_mode)) {
1093             ip->i_op = &afs_file_iops;
1094             ip->i_fop = &afs_file_fops;
1095             ip->i_data.a_ops = &afs_file_aops;
1096         } else if (S_ISDIR(ip->i_mode)) {
1097             ip->i_op = &afs_dir_iops;
1098             ip->i_fop = &afs_dir_fops;
1099         } else if (S_ISLNK(ip->i_mode)) {
1100             ip->i_op = &afs_symlink_iops;
1101             ip->i_data.a_ops = &afs_symlink_aops;
1102             ip->i_mapping = &ip->i_data;
1103         } else
1104             printk
1105                 ("afs_linux_lookup: ip->i_mode 0x%x  dp->d_name.name %s  code %d\n",
1106                  ip->i_mode, dp->d_name.name, code);
1107 #ifdef STRUCT_INODE_HAS_I_SECURITY
1108        if (ip->i_security == NULL) {
1109            if (security_inode_alloc(ip))
1110                panic("afs_linux_lookup: Cannot allocate inode security");
1111        }
1112 #endif
1113 #else
1114         if (S_ISDIR(ip->i_mode))
1115             ip->i_op = &afs_dir_iops;
1116         else if (S_ISLNK(ip->i_mode))
1117             ip->i_op = &afs_symlink_iops;
1118 #endif
1119     }
1120     dp->d_op = &afs_dentry_operations;
1121     d_add(dp, AFSTOI(vcp));
1122
1123 #if defined(AFS_LINUX26_ENV)
1124     unlock_kernel();
1125 #endif
1126     crfree(credp);
1127
1128     /* It's ok for the file to not be found. That's noted by the caller by
1129      * seeing that the dp->d_inode field is NULL.
1130      */
1131 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1132     if (code == ENOENT)
1133         return ERR_PTR(0);
1134     else
1135         return ERR_PTR(-code);
1136 #else
1137     if (code == ENOENT)
1138         code = 0;
1139     return -code;
1140 #endif
1141 }
1142
1143 int
1144 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1145 {
1146     int code;
1147     cred_t *credp = crref();
1148     const char *name = newdp->d_name.name;
1149     struct inode *oldip = olddp->d_inode;
1150
1151     /* If afs_link returned the vnode, we could instantiate the
1152      * dentry. Since it's not, we drop this one and do a new lookup.
1153      */
1154     d_drop(newdp);
1155
1156     AFS_GLOCK();
1157     code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1158
1159     AFS_GUNLOCK();
1160     crfree(credp);
1161     return -code;
1162 }
1163
1164 int
1165 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1166 {
1167     int code = EBUSY;
1168     cred_t *credp = crref();
1169     const char *name = dp->d_name.name;
1170     struct vcache *tvc = ITOAFS(dp->d_inode);
1171
1172 #if defined(AFS_LINUX26_ENV)
1173     lock_kernel();
1174 #endif
1175     if (((VREFCOUNT(tvc) > 0) && tvc->opens > 0)
1176                                 && !(tvc->states & CUnlinked)) {
1177         struct dentry *__dp;
1178         char *__name;
1179         extern char *afs_newname();
1180
1181         __dp = NULL;
1182         __name = NULL;
1183         do {
1184             dput(__dp);
1185
1186             AFS_GLOCK();
1187             if (__name)
1188                 osi_FreeSmallSpace(__name);
1189             __name = afs_newname();
1190             AFS_GUNLOCK();
1191
1192             __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1193                 
1194             if (IS_ERR(__dp))
1195                 goto out;
1196         } while (__dp->d_inode != NULL);
1197
1198         AFS_GLOCK();
1199         code = afs_rename(ITOAFS(dip), dp->d_name.name, ITOAFS(dip), __dp->d_name.name, credp);
1200         if (!code) {
1201             tvc->mvid = __name;
1202             crhold(credp);
1203             if (tvc->uncred) {
1204                 crfree(tvc->uncred);
1205             }
1206             tvc->uncred = credp;
1207             tvc->states |= CUnlinked;
1208         }
1209         AFS_GUNLOCK();
1210
1211         if (!code)
1212             d_move(dp, __dp);
1213         dput(__dp);
1214
1215         goto out;
1216     }
1217
1218     AFS_GLOCK();
1219     code = afs_remove(ITOAFS(dip), name, credp);
1220     AFS_GUNLOCK();
1221     if (!code)
1222         d_drop(dp);
1223 out:
1224 #if defined(AFS_LINUX26_ENV)
1225     unlock_kernel();
1226 #endif
1227     crfree(credp);
1228     return -code;
1229 }
1230
1231
1232 int
1233 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1234 {
1235     int code;
1236     cred_t *credp = crref();
1237     struct vattr vattr;
1238     const char *name = dp->d_name.name;
1239
1240     /* If afs_symlink returned the vnode, we could instantiate the
1241      * dentry. Since it's not, we drop this one and do a new lookup.
1242      */
1243     d_drop(dp);
1244
1245     AFS_GLOCK();
1246     VATTR_NULL(&vattr);
1247     code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1248     AFS_GUNLOCK();
1249     crfree(credp);
1250     return -code;
1251 }
1252
1253 int
1254 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1255 {
1256     int code;
1257     cred_t *credp = crref();
1258     struct vcache *tvcp = NULL;
1259     struct vattr vattr;
1260     const char *name = dp->d_name.name;
1261
1262 #if defined(AFS_LINUX26_ENV)
1263     lock_kernel();
1264 #endif
1265     AFS_GLOCK();
1266     VATTR_NULL(&vattr);
1267     vattr.va_mask = ATTR_MODE;
1268     vattr.va_mode = mode;
1269     code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1270     AFS_GUNLOCK();
1271
1272     if (tvcp) {
1273         tvcp->v.v_op = &afs_dir_iops;
1274 #if defined(AFS_LINUX24_ENV)
1275         tvcp->v.v_fop = &afs_dir_fops;
1276 #endif
1277         dp->d_op = &afs_dentry_operations;
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     struct dentry *rehash = NULL;
1332
1333 #if defined(AFS_LINUX26_ENV)
1334     /* Prevent any new references during rename operation. */
1335     lock_kernel();
1336 #endif
1337     /* Remove old and new entries from name hash. New one will change below.
1338      * While it's optimal to catch failures and re-insert newdp into hash,
1339      * it's also error prone and in that case we're already dealing with error
1340      * cases. Let another lookup put things right, if need be.
1341      */
1342 #if defined(AFS_LINUX26_ENV)
1343     if (!d_unhashed(newdp)) {
1344         d_drop(newdp);
1345         rehash = newdp;
1346     }
1347 #else
1348     if (!list_empty(&newdp->d_hash)) {
1349         d_drop(newdp);
1350         rehash = newdp;
1351     }
1352 #endif
1353
1354 #if defined(AFS_LINUX24_ENV)
1355     if (atomic_read(&olddp->d_count) > 1)
1356         shrink_dcache_parent(olddp);
1357 #endif
1358
1359     AFS_GLOCK();
1360     code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip), newname, credp);
1361     AFS_GUNLOCK();
1362
1363     if (rehash)
1364         d_rehash(rehash);
1365
1366 #if defined(AFS_LINUX26_ENV)
1367     unlock_kernel();
1368 #endif
1369
1370     crfree(credp);
1371     return -code;
1372 }
1373
1374
1375 /* afs_linux_ireadlink 
1376  * Internal readlink which can return link contents to user or kernel space.
1377  * Note that the buffer is NOT supposed to be null-terminated.
1378  */
1379 static int
1380 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1381 {
1382     int code;
1383     cred_t *credp = crref();
1384     uio_t tuio;
1385     struct iovec iov;
1386
1387     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1388     code = afs_readlink(ITOAFS(ip), &tuio, credp);
1389     crfree(credp);
1390
1391     if (!code)
1392         return maxlen - tuio.uio_resid;
1393     else
1394         return -code;
1395 }
1396
1397 #if !defined(AFS_LINUX24_ENV)
1398 /* afs_linux_readlink 
1399  * Fill target (which is in user space) with contents of symlink.
1400  */
1401 int
1402 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1403 {
1404     int code;
1405     struct inode *ip = dp->d_inode;
1406
1407     AFS_GLOCK();
1408     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1409     AFS_GUNLOCK();
1410     return code;
1411 }
1412
1413
1414 /* afs_linux_follow_link
1415  * a file system dependent link following routine.
1416  */
1417 struct dentry *
1418 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1419                       unsigned int follow)
1420 {
1421     int code = 0;
1422     char *name;
1423     struct dentry *res;
1424
1425
1426     AFS_GLOCK();
1427     name = osi_Alloc(PATH_MAX + 1);
1428     if (!name) {
1429         AFS_GUNLOCK();
1430         dput(basep);
1431         return ERR_PTR(-EIO);
1432     }
1433
1434     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1435     AFS_GUNLOCK();
1436
1437     if (code < 0) {
1438         dput(basep);
1439         res = ERR_PTR(code);
1440     } else {
1441         name[code] = '\0';
1442         res = lookup_dentry(name, basep, follow);
1443     }
1444
1445     AFS_GLOCK();
1446     osi_Free(name, PATH_MAX + 1);
1447     AFS_GUNLOCK();
1448     return res;
1449 }
1450 #endif
1451
1452 /* afs_linux_readpage
1453  * all reads come through here. A strategy-like read call.
1454  */
1455 int
1456 afs_linux_readpage(struct file *fp, struct page *pp)
1457 {
1458     int code;
1459     cred_t *credp = crref();
1460 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1461     char *address;
1462     afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1463 #else
1464     ulong address = afs_linux_page_address(pp);
1465     afs_offs_t offset = pageoff(pp);
1466 #endif
1467     uio_t tuio;
1468     struct iovec iovec;
1469     struct inode *ip = FILE_INODE(fp);
1470     int cnt = page_count(pp);
1471     struct vcache *avc = ITOAFS(ip);
1472
1473
1474 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1475     address = kmap(pp);
1476     ClearPageError(pp);
1477 #else
1478     atomic_add(1, &pp->count);
1479     set_bit(PG_locked, &pp->flags);     /* other bits? See mm.h */
1480     clear_bit(PG_error, &pp->flags);
1481 #endif
1482
1483     setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1484               AFS_UIOSYS);
1485 #ifdef AFS_LINUX24_ENV
1486     lock_kernel();
1487 #endif
1488     AFS_GLOCK();
1489     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 */
1490     code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1491     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1492                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1493                code);
1494     AFS_GUNLOCK();
1495 #ifdef AFS_LINUX24_ENV
1496     unlock_kernel();
1497 #endif
1498
1499     if (!code) {
1500         if (tuio.uio_resid)     /* zero remainder of page */
1501             memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1502                    tuio.uio_resid);
1503 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1504         flush_dcache_page(pp);
1505         SetPageUptodate(pp);
1506 #else
1507         set_bit(PG_uptodate, &pp->flags);
1508 #endif
1509     }
1510
1511 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1512     kunmap(pp);
1513     UnlockPage(pp);
1514 #else
1515     clear_bit(PG_locked, &pp->flags);
1516     wake_up(&pp->wait);
1517     free_page(address);
1518 #endif
1519
1520     if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1521         struct dcache *tdc;
1522         struct vrequest treq;
1523
1524         AFS_GLOCK();
1525         code = afs_InitReq(&treq, credp);
1526         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1527             tdc = afs_FindDCache(avc, offset);
1528             if (tdc) {
1529                 if (!(tdc->mflags & DFNextStarted))
1530                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1531                 afs_PutDCache(tdc);
1532             }
1533             ReleaseWriteLock(&avc->lock);
1534         }
1535         AFS_GUNLOCK();
1536     }
1537
1538     crfree(credp);
1539     return -code;
1540 }
1541
1542 #if defined(AFS_LINUX24_ENV)
1543 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1544 int
1545 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1546 #else
1547 int
1548 afs_linux_writepage(struct page *pp)
1549 #endif
1550 {
1551     struct address_space *mapping = pp->mapping;
1552     struct inode *inode;
1553     unsigned long end_index;
1554     unsigned offset = PAGE_CACHE_SIZE;
1555     long status;
1556
1557 #if defined(AFS_LINUX26_ENV)
1558     if (PageReclaim(pp)) {
1559         return WRITEPAGE_ACTIVATE;
1560     }
1561 #else
1562     if (PageLaunder(pp)) {
1563         return(fail_writepage(pp));
1564     }
1565 #endif
1566
1567     inode = (struct inode *)mapping->host;
1568     end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1569
1570     /* easy case */
1571     if (pp->index < end_index)
1572         goto do_it;
1573     /* things got complicated... */
1574     offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1575     /* OK, are we completely out? */
1576     if (pp->index >= end_index + 1 || !offset)
1577         return -EIO;
1578   do_it:
1579     AFS_GLOCK();
1580     status = afs_linux_writepage_sync(inode, pp, 0, offset);
1581     AFS_GUNLOCK();
1582     SetPageUptodate(pp);
1583     UnlockPage(pp);
1584     if (status == offset)
1585         return 0;
1586     else
1587         return status;
1588 }
1589 #endif
1590
1591 /* afs_linux_permission
1592  * Check access rights - returns error if can't check or permission denied.
1593  */
1594 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1595 int
1596 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1597 #else
1598 int
1599 afs_linux_permission(struct inode *ip, int mode)
1600 #endif
1601 {
1602     int code;
1603     cred_t *credp = crref();
1604     int tmp = 0;
1605
1606     AFS_GLOCK();
1607     if (mode & MAY_EXEC)
1608         tmp |= VEXEC;
1609     if (mode & MAY_READ)
1610         tmp |= VREAD;
1611     if (mode & MAY_WRITE)
1612         tmp |= VWRITE;
1613     code = afs_access(ITOAFS(ip), tmp, credp);
1614
1615     AFS_GUNLOCK();
1616     crfree(credp);
1617     return -code;
1618 }
1619
1620
1621 #if defined(AFS_LINUX24_ENV)
1622 int
1623 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1624                          unsigned long offset, unsigned int count)
1625 {
1626     struct vcache *vcp = ITOAFS(ip);
1627     char *buffer;
1628     afs_offs_t base;
1629     int code = 0;
1630     cred_t *credp;
1631     uio_t tuio;
1632     struct iovec iovec;
1633     int f_flags = 0;
1634
1635     buffer = kmap(pp) + offset;
1636     base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1637
1638     credp = crref();
1639     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1640                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1641                ICL_TYPE_INT32, 99999);
1642
1643     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1644
1645     code = afs_write(vcp, &tuio, f_flags, credp, 0);
1646
1647     vcache2inode(vcp);
1648
1649     if (!code
1650         && afs_stats_cmperf.cacheCurrDirtyChunks >
1651         afs_stats_cmperf.cacheMaxDirtyChunks) {
1652         struct vrequest treq;
1653
1654         ObtainWriteLock(&vcp->lock, 533);
1655         if (!afs_InitReq(&treq, credp))
1656             code = afs_DoPartialWrite(vcp, &treq);
1657         ReleaseWriteLock(&vcp->lock);
1658     }
1659     code = code ? -code : count - tuio.uio_resid;
1660
1661     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1662                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1663                ICL_TYPE_INT32, code);
1664
1665     crfree(credp);
1666     kunmap(pp);
1667
1668     return code;
1669 }
1670
1671 static int
1672 afs_linux_updatepage(struct file *file, struct page *page,
1673                      unsigned long offset, unsigned int count)
1674 {
1675     struct dentry *dentry = file->f_dentry;
1676
1677     return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1678 }
1679 #else
1680 /* afs_linux_updatepage
1681  * What one would have thought was writepage - write dirty page to file.
1682  * Called from generic_file_write. buffer is still in user space. pagep
1683  * has been filled in with old data if we're updating less than a page.
1684  */
1685 int
1686 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1687                      unsigned int count, int sync)
1688 {
1689     struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1690     u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1691     int code = 0;
1692     cred_t *credp;
1693     uio_t tuio;
1694     struct iovec iovec;
1695
1696     set_bit(PG_locked, &pp->flags);
1697
1698     credp = crref();
1699     AFS_GLOCK();
1700     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1701                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1702                ICL_TYPE_INT32, 99999);
1703     setup_uio(&tuio, &iovec, page_addr + offset,
1704               (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1705               AFS_UIOSYS);
1706
1707     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1708
1709     vcache2inode(vcp);
1710
1711     code = code ? -code : count - tuio.uio_resid;
1712     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1713                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1714                ICL_TYPE_INT32, code);
1715
1716     AFS_GUNLOCK();
1717     crfree(credp);
1718
1719     clear_bit(PG_locked, &pp->flags);
1720     return code;
1721 }
1722 #endif
1723
1724 #if defined(AFS_LINUX24_ENV)
1725 static int
1726 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1727                        unsigned to)
1728 {
1729     int code;
1730
1731     lock_kernel();
1732     AFS_GLOCK();
1733     code = afs_linux_updatepage(file, page, offset, to - offset);
1734     AFS_GUNLOCK();
1735     unlock_kernel();
1736     kunmap(page);
1737
1738     return code;
1739 }
1740
1741 static int
1742 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1743                         unsigned to)
1744 {
1745     kmap(page);
1746     return 0;
1747 }
1748
1749 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1750 #endif
1751
1752 struct inode_operations afs_file_iops = {
1753 #if defined(AFS_LINUX26_ENV)
1754   .permission =         afs_linux_permission,
1755   .getattr =            afs_linux_getattr,
1756   .setattr =            afs_notify_change,
1757 #elif defined(AFS_LINUX24_ENV)
1758   .permission =         afs_linux_permission,
1759   .revalidate =         afs_linux_revalidate,
1760   .setattr =            afs_notify_change,
1761 #else
1762   .default_file_ops =   &afs_file_fops,
1763   .readpage =           afs_linux_readpage,
1764   .revalidate =         afs_linux_revalidate,
1765   .updatepage =         afs_linux_updatepage,
1766 #endif
1767 };
1768
1769 #if defined(AFS_LINUX24_ENV)
1770 struct address_space_operations afs_file_aops = {
1771   .readpage =           afs_linux_readpage,
1772   .writepage =          afs_linux_writepage,
1773   .commit_write =       afs_linux_commit_write,
1774   .prepare_write =      afs_linux_prepare_write,
1775 };
1776 #endif
1777
1778
1779 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1780  * by what sort of operation is allowed.....
1781  */
1782
1783 struct inode_operations afs_dir_iops = {
1784 #if !defined(AFS_LINUX24_ENV)
1785   .default_file_ops =   &afs_dir_fops,
1786 #else
1787   .setattr =            afs_notify_change,
1788 #endif
1789   .create =             afs_linux_create,
1790   .lookup =             afs_linux_lookup,
1791   .link =               afs_linux_link,
1792   .unlink =             afs_linux_unlink,
1793   .symlink =            afs_linux_symlink,
1794   .mkdir =              afs_linux_mkdir,
1795   .rmdir =              afs_linux_rmdir,
1796   .rename =             afs_linux_rename,
1797 #if defined(AFS_LINUX26_ENV)
1798   .getattr =            afs_linux_getattr,
1799 #else
1800   .revalidate =         afs_linux_revalidate,
1801 #endif
1802   .permission =         afs_linux_permission,
1803 };
1804
1805 /* We really need a separate symlink set of ops, since do_follow_link()
1806  * determines if it _is_ a link by checking if the follow_link op is set.
1807  */
1808 #if defined(AFS_LINUX24_ENV)
1809 static int
1810 afs_symlink_filler(struct file *file, struct page *page)
1811 {
1812     struct inode *ip = (struct inode *)page->mapping->host;
1813     char *p = (char *)kmap(page);
1814     int code;
1815
1816     lock_kernel();
1817     AFS_GLOCK();
1818     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1819     AFS_GUNLOCK();
1820
1821     if (code < 0)
1822         goto fail;
1823     p[code] = '\0';             /* null terminate? */
1824     unlock_kernel();
1825
1826     SetPageUptodate(page);
1827     kunmap(page);
1828     UnlockPage(page);
1829     return 0;
1830
1831   fail:
1832     unlock_kernel();
1833
1834     SetPageError(page);
1835     kunmap(page);
1836     UnlockPage(page);
1837     return code;
1838 }
1839
1840 struct address_space_operations afs_symlink_aops = {
1841   .readpage =   afs_symlink_filler
1842 };
1843 #endif
1844
1845 struct inode_operations afs_symlink_iops = {
1846 #if defined(AFS_LINUX24_ENV)
1847   .readlink =           page_readlink,
1848 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1849   .follow_link =        page_follow_link,
1850 #else
1851   .follow_link =        page_follow_link_light,
1852 #endif
1853   .setattr =            afs_notify_change,
1854 #else
1855   .readlink =           afs_linux_readlink,
1856   .follow_link =        afs_linux_follow_link,
1857   .permission =         afs_linux_permission,
1858   .revalidate =         afs_linux_revalidate,
1859 #endif
1860 };