dentry-revalidate-cleanup-20050424
[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 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
430 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
431                                unsigned long arg) {
432     return afs_xioctl(FILE_INODE(fp), fp, com, arg);
433
434 }
435 #endif
436
437 /* We need to detect unmap's after close. To do that, we need our own
438  * vm_operations_struct's. And we need to set them up for both the
439  * private and shared mappings. The fun part is that these are all static
440  * so we'll have to initialize on the fly!
441  */
442 static struct vm_operations_struct afs_private_mmap_ops;
443 static int afs_private_mmap_ops_inited = 0;
444 static struct vm_operations_struct afs_shared_mmap_ops;
445 static int afs_shared_mmap_ops_inited = 0;
446
447 void
448 afs_linux_vma_close(struct vm_area_struct *vmap)
449 {
450     struct vcache *vcp;
451     cred_t *credp;
452     int need_unlock = 0;
453
454     if (!vmap->vm_file)
455         return;
456
457     vcp = ITOAFS(FILE_INODE(vmap->vm_file));
458     if (!vcp)
459         return;
460
461     AFS_GLOCK();
462     afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE, ICL_TYPE_POINTER, vcp,
463                ICL_TYPE_INT32, vcp->mapcnt, ICL_TYPE_INT32, vcp->opens,
464                ICL_TYPE_INT32, vcp->execsOrWriters);
465     if ((&vcp->lock)->excl_locked == 0 || (&vcp->lock)->pid_writer == MyPidxx) {
466         ObtainWriteLock(&vcp->lock, 532);
467         need_unlock = 1;
468     } else
469         printk("AFS_VMA_CLOSE(%d): Skipping Already locked vcp=%p vmap=%p\n",
470                MyPidxx, &vcp, &vmap);
471     if (vcp->mapcnt) {
472         vcp->mapcnt--;
473         if (need_unlock)
474             ReleaseWriteLock(&vcp->lock);
475         if (!vcp->mapcnt) {
476             if (need_unlock && vcp->execsOrWriters < 2) {
477                 credp = crref();
478                 (void)afs_close(vcp, vmap->vm_file->f_flags, credp);
479                 /* only decrement the execsOrWriters flag if this is not a
480                  * writable file. */
481                 if (!(vcp->states & CRO) )
482                     if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
483                         vcp->execsOrWriters--;
484                 vcp->states &= ~CMAPPED;
485                 crfree(credp);
486             } else if ((vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
487                 vcp->execsOrWriters--;
488             /* If we did not have the lock */
489             if (!need_unlock) {
490                 vcp->mapcnt++;
491                 if (!vcp->execsOrWriters)
492                     vcp->execsOrWriters = 1;
493             }
494         }
495     } else {
496         if (need_unlock)
497             ReleaseWriteLock(&vcp->lock);
498     }
499
500     AFS_GUNLOCK();
501 }
502
503 static int
504 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
505 {
506     struct vcache *vcp = ITOAFS(FILE_INODE(fp));
507     cred_t *credp = crref();
508     struct vrequest treq;
509     int code;
510
511     AFS_GLOCK();
512 #if defined(AFS_LINUX24_ENV)
513     afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
514                ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
515                vmap->vm_end - vmap->vm_start);
516 #else
517     afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
518                ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
519                vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
520                vmap->vm_offset);
521 #endif
522
523     /* get a validated vcache entry */
524     code = afs_InitReq(&treq, credp);
525     if (!code)
526         code = afs_VerifyVCache(vcp, &treq);
527
528     if (!code && (vcp->states & CRO) && 
529         (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
530         code = EACCES;
531
532     if (code)
533         code = -code;
534     else {
535         osi_FlushPages(vcp, credp);     /* ensure stale pages are gone */
536
537         AFS_GUNLOCK();
538         code = generic_file_mmap(fp, vmap);
539         AFS_GLOCK();
540     }
541
542     if (code == 0) {
543         ObtainWriteLock(&vcp->lock, 531);
544         /* Set out vma ops so we catch the close. The following test should be
545          * the same as used in generic_file_mmap.
546          */
547         if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
548             if (!afs_shared_mmap_ops_inited) {
549                 afs_shared_mmap_ops_inited = 1;
550                 afs_shared_mmap_ops = *vmap->vm_ops;
551                 afs_shared_mmap_ops.close = afs_linux_vma_close;
552             }
553             vmap->vm_ops = &afs_shared_mmap_ops;
554         } else {
555             if (!afs_private_mmap_ops_inited) {
556                 afs_private_mmap_ops_inited = 1;
557                 afs_private_mmap_ops = *vmap->vm_ops;
558                 afs_private_mmap_ops.close = afs_linux_vma_close;
559             }
560             vmap->vm_ops = &afs_private_mmap_ops;
561         }
562
563
564         /* Add an open reference on the first mapping. */
565         if (vcp->mapcnt == 0) {
566             if (!(vcp->states & CRO))
567                 vcp->execsOrWriters++;
568             vcp->opens++;
569             vcp->states |= CMAPPED;
570         }
571         ReleaseWriteLock(&vcp->lock);
572         vcp->mapcnt++;
573     }
574
575     AFS_GUNLOCK();
576     crfree(credp);
577     return code;
578 }
579
580 int
581 afs_linux_open(struct inode *ip, struct file *fp)
582 {
583     int code;
584     cred_t *credp = crref();
585
586 #ifdef AFS_LINUX24_ENV
587     lock_kernel();
588 #endif
589     AFS_GLOCK();
590     code = afs_open((struct vcache **)&ip, fp->f_flags, credp);
591     AFS_GUNLOCK();
592 #ifdef AFS_LINUX24_ENV
593     unlock_kernel();
594 #endif
595
596     crfree(credp);
597     return -code;
598 }
599
600 static int
601 afs_linux_release(struct inode *ip, struct file *fp)
602 {
603     struct vcache *vcp = ITOAFS(ip);
604     cred_t *credp = crref();
605     int code = 0;
606
607 #ifdef AFS_LINUX24_ENV
608     lock_kernel();
609 #endif
610     AFS_GLOCK();
611     code = afs_close(vcp, fp->f_flags, credp);
612     AFS_GUNLOCK();
613 #ifdef AFS_LINUX24_ENV
614     unlock_kernel();
615 #endif
616
617     crfree(credp);
618     return -code;
619 }
620
621 #if defined(AFS_LINUX24_ENV)
622 static int
623 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
624 #else
625 static int
626 afs_linux_fsync(struct file *fp, struct dentry *dp)
627 #endif
628 {
629     int code;
630     struct inode *ip = FILE_INODE(fp);
631     cred_t *credp = crref();
632
633 #ifdef AFS_LINUX24_ENV
634     lock_kernel();
635 #endif
636     AFS_GLOCK();
637     code = afs_fsync(ITOAFS(ip), credp);
638     AFS_GUNLOCK();
639 #ifdef AFS_LINUX24_ENV
640     unlock_kernel();
641 #endif
642     crfree(credp);
643     return -code;
644
645 }
646
647
648 static int
649 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
650 {
651     int code = 0;
652     struct vcache *vcp = ITOAFS(FILE_INODE(fp));
653     cred_t *credp = crref();
654     struct AFS_FLOCK flock;
655     /* Convert to a lock format afs_lockctl understands. */
656     memset((char *)&flock, 0, sizeof(flock));
657     flock.l_type = flp->fl_type;
658     flock.l_pid = flp->fl_pid;
659     flock.l_whence = 0;
660     flock.l_start = flp->fl_start;
661     flock.l_len = flp->fl_end - flp->fl_start;
662
663     /* Safe because there are no large files, yet */
664 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
665     if (cmd == F_GETLK64)
666         cmd = F_GETLK;
667     else if (cmd == F_SETLK64)
668         cmd = F_SETLK;
669     else if (cmd == F_SETLKW64)
670         cmd = F_SETLKW;
671 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
672
673     AFS_GLOCK();
674     code = afs_lockctl(vcp, &flock, cmd, credp);
675     AFS_GUNLOCK();
676
677     /* Convert flock back to Linux's file_lock */
678     flp->fl_type = flock.l_type;
679     flp->fl_pid = flock.l_pid;
680     flp->fl_start = flock.l_start;
681     flp->fl_end = flock.l_start + flock.l_len;
682
683     crfree(credp);
684     return -code;
685
686 }
687
688 /* afs_linux_flush
689  * essentially the same as afs_fsync() but we need to get the return
690  * code for the sys_close() here, not afs_linux_release(), so call
691  * afs_StoreAllSegments() with AFS_LASTSTORE
692  */
693 int
694 afs_linux_flush(struct file *fp)
695 {
696     struct vrequest treq;
697     struct vcache *vcp = ITOAFS(FILE_INODE(fp));
698     cred_t *credp = crref();
699     int code;
700
701     AFS_GLOCK();
702
703     code = afs_InitReq(&treq, credp);
704     if (code)
705         goto out;
706
707     ObtainSharedLock(&vcp->lock, 535);
708     if (vcp->execsOrWriters > 0) {
709         UpgradeSToWLock(&vcp->lock, 536);
710         code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
711         ConvertWToSLock(&vcp->lock);
712     }
713     code = afs_CheckCode(code, &treq, 54);
714     ReleaseSharedLock(&vcp->lock);
715
716 out:
717     AFS_GUNLOCK();
718
719     crfree(credp);
720     return -code;
721 }
722
723 #if !defined(AFS_LINUX24_ENV)
724 /* Not allowed to directly read a directory. */
725 ssize_t
726 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
727 {
728     return -EISDIR;
729 }
730 #endif
731
732
733
734 struct file_operations afs_dir_fops = {
735 #if !defined(AFS_LINUX24_ENV)
736   .read =       afs_linux_dir_read,
737   .lock =       afs_linux_lock,
738   .fsync =      afs_linux_fsync,
739 #else
740   .read =       generic_read_dir,
741 #endif
742   .readdir =    afs_linux_readdir,
743 #ifdef HAVE_UNLOCKED_IOCTL
744   .unlocked_ioctl = afs_unlocked_xioctl,
745 #else
746   .ioctl =      afs_xioctl,
747 #endif
748 #ifdef HAVE_COMPAT_IOCTL
749   .compat_ioctl = afs_unlocked_xioctl,
750 #endif
751   .open =       afs_linux_open,
752   .release =    afs_linux_release,
753 };
754
755 struct file_operations afs_file_fops = {
756   .read =       afs_linux_read,
757   .write =      afs_linux_write,
758 #ifdef HAVE_UNLOCKED_IOCTL
759   .unlocked_ioctl = afs_unlocked_xioctl,
760 #else
761   .ioctl =      afs_xioctl,
762 #endif
763 #ifdef HAVE_COMPAT_IOCTL
764   .compat_ioctl = afs_unlocked_xioctl,
765 #endif
766   .mmap =       afs_linux_mmap,
767   .open =       afs_linux_open,
768   .flush =      afs_linux_flush,
769 #ifdef AFS_LINUX26_ENV
770   .sendfile =   generic_file_sendfile,
771 #endif
772   .release =    afs_linux_release,
773   .fsync =      afs_linux_fsync,
774   .lock =       afs_linux_lock,
775 };
776
777
778 /**********************************************************************
779  * AFS Linux dentry operations
780  **********************************************************************/
781
782 /* afs_linux_revalidate
783  * Ensure vcache is stat'd before use. Return 0 if entry is valid.
784  */
785 static int
786 afs_linux_revalidate(struct dentry *dp)
787 {
788     int code;
789     cred_t *credp;
790     struct vrequest treq;
791     struct vcache *vcp = ITOAFS(dp->d_inode);
792     struct vcache *rootvp = NULL;
793
794 #ifdef AFS_LINUX24_ENV
795     lock_kernel();
796 #endif
797     AFS_GLOCK();
798
799     if (afs_fakestat_enable && vcp->mvstat == 1 && vcp->mvid
800         && (vcp->states & CMValid) && (vcp->states & CStatd)) {
801         ObtainSharedLock(&afs_xvcache, 680);
802         rootvp = afs_FindVCache(vcp->mvid, 0, 0);
803         ReleaseSharedLock(&afs_xvcache);
804     }
805
806     /* Make this a fast path (no crref), since it's called so often. */
807     if (vcp->states & CStatd) {
808         if (*dp->d_name.name != '/' && vcp->mvstat == 2)        /* root vnode */
809             check_bad_parent(dp);       /* check and correct mvid */
810         if (rootvp)
811             vcache2fakeinode(rootvp, vcp);
812         else
813             vcache2inode(vcp);
814         if (rootvp)
815             afs_PutVCache(rootvp);
816         AFS_GUNLOCK();
817 #ifdef AFS_LINUX24_ENV
818         unlock_kernel();
819 #endif
820         return 0;
821     }
822
823     credp = crref();
824     code = afs_InitReq(&treq, credp);
825     if (!code)
826         code = afs_VerifyVCache(vcp, &treq);
827
828     AFS_GUNLOCK();
829 #ifdef AFS_LINUX24_ENV
830     unlock_kernel();
831 #endif
832     crfree(credp);
833
834     return -code;
835 }
836
837 #if defined(AFS_LINUX26_ENV)
838 static int
839 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
840 {
841         int err = afs_linux_revalidate(dentry);
842         if (!err)
843                 generic_fillattr(dentry->d_inode, stat);
844         return err;
845 }
846 #endif
847
848 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
849  * In kernels 2.2.10 and above, we are passed an additional flags var which
850  * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
851  * we are advised to follow the entry if it is a link or to make sure that 
852  * it is a directory. But since the kernel itself checks these possibilities
853  * later on, we shouldn't have to do it until later. Perhaps in the future..
854  */
855 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
856 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
857 static int
858 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
859 #else
860 static int
861 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
862 #endif
863 #else
864 static int
865 afs_linux_dentry_revalidate(struct dentry *dp)
866 #endif
867 {
868     cred_t *credp = NULL;
869     struct vrequest treq;
870     struct vcache *lookupvcp = NULL;
871     int code, bad_dentry = 1;
872     struct vcache *vcp, *parentvcp;
873
874 #ifdef AFS_LINUX24_ENV
875     lock_kernel();
876 #endif
877     AFS_GLOCK();
878
879     vcp = ITOAFS(dp->d_inode);
880     parentvcp = ITOAFS(dp->d_parent->d_inode);
881
882     /* If it's a negative dentry, then there's nothing to do. */
883     if (!vcp || !parentvcp)
884         goto done;
885
886     /* If it is the AFS root, then there's no chance it needs 
887      * revalidating */
888     if (vcp == afs_globalVp) {
889         bad_dentry = 0;
890         goto done;
891     }
892
893     /* Make this a fast path (no crref), since it's called so often. */
894     if (vcp->states & CStatd) {
895         if (*dp->d_name.name != '/' && vcp->mvstat == 2)        /* root vnode */
896             check_bad_parent(dp);       /* check and correct mvid */
897         vcache2inode(vcp);
898         bad_dentry = 0;
899         goto done;
900     }
901
902     credp = crref();
903
904     /* get a validated vcache entry */
905     code = afs_InitReq(&treq, credp);
906     if (code)
907         goto done;
908     code = afs_VerifyVCache(vcp, &treq);
909     if (!code) {
910         bad_dentry = 0;
911         goto done;
912     }
913
914     code = afs_lookup(parentvcp, dp->d_name.name, &lookupvcp, credp);
915
916     /* Verify that the dentry does not point to an old inode */
917     if (vcp != lookupvcp)
918         goto done;
919
920     bad_dentry = 0;
921
922   done:
923     /* Clean up */
924     if (lookupvcp)
925         afs_PutVCache(lookupvcp);
926
927     AFS_GUNLOCK();
928
929     if (bad_dentry) {
930         shrink_dcache_parent(dp);
931         d_drop(dp);
932     }
933
934 #ifdef AFS_LINUX24_ENV
935     unlock_kernel();
936 #endif
937     if (credp)
938         crfree(credp);
939
940     return !bad_dentry;
941 }
942
943 #if !defined(AFS_LINUX26_ENV)
944 /* afs_dentry_iput */
945 static void
946 afs_dentry_iput(struct dentry *dp, struct inode *ip)
947 {
948     osi_iput(ip);
949 }
950 #endif
951
952 static int
953 afs_dentry_delete(struct dentry *dp)
954 {
955     if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
956         return 1;               /* bad inode? */
957
958     return 0;
959 }
960
961 struct dentry_operations afs_dentry_operations = {
962   .d_revalidate =       afs_linux_dentry_revalidate,
963   .d_delete =           afs_dentry_delete,
964 #if !defined(AFS_LINUX26_ENV)
965   .d_iput =             afs_dentry_iput,
966 #endif
967 };
968
969 /**********************************************************************
970  * AFS Linux inode operations
971  **********************************************************************/
972
973 /* afs_linux_create
974  *
975  * Merely need to set enough of vattr to get us through the create. Note
976  * that the higher level code (open_namei) will take care of any tuncation
977  * explicitly. Exclusive open is also taken care of in open_namei.
978  *
979  * name is in kernel space at this point.
980  */
981 #ifdef IOP_CREATE_TAKES_NAMEIDATA
982 int
983 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
984                  struct nameidata *nd)
985 #else
986 int
987 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
988 #endif
989 {
990     int code;
991     cred_t *credp = crref();
992     struct vattr vattr;
993     const char *name = dp->d_name.name;
994     struct inode *ip;
995
996     VATTR_NULL(&vattr);
997     vattr.va_mode = mode;
998
999 #if defined(AFS_LINUX26_ENV)
1000     lock_kernel();
1001 #endif
1002     AFS_GLOCK();
1003     code =
1004         afs_create(ITOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1005                    (struct vcache **)&ip, credp);
1006
1007     if (!code) {
1008         vattr2inode(ip, &vattr);
1009         /* Reset ops if symlink or directory. */
1010 #if defined(AFS_LINUX24_ENV)
1011         if (S_ISREG(ip->i_mode)) {
1012             ip->i_op = &afs_file_iops;
1013             ip->i_fop = &afs_file_fops;
1014             ip->i_data.a_ops = &afs_file_aops;
1015         } else if (S_ISDIR(ip->i_mode)) {
1016             ip->i_op = &afs_dir_iops;
1017             ip->i_fop = &afs_dir_fops;
1018         } else if (S_ISLNK(ip->i_mode)) {
1019             ip->i_op = &afs_symlink_iops;
1020             ip->i_data.a_ops = &afs_symlink_aops;
1021             ip->i_mapping = &ip->i_data;
1022         } else
1023             printk("afs_linux_create: FIXME\n");
1024 #else
1025         if (S_ISDIR(ip->i_mode))
1026             ip->i_op = &afs_dir_iops;
1027         else if (S_ISLNK(ip->i_mode))
1028             ip->i_op = &afs_symlink_iops;
1029 #endif
1030
1031         dp->d_op = &afs_dentry_operations;
1032         d_instantiate(dp, ip);
1033     }
1034
1035     AFS_GUNLOCK();
1036 #if defined(AFS_LINUX26_ENV)
1037     unlock_kernel();
1038 #endif
1039     crfree(credp);
1040     return -code;
1041 }
1042
1043 /* afs_linux_lookup */
1044 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1045 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1046 struct dentry *
1047 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1048                  struct nameidata *nd)
1049 #else
1050 struct dentry *
1051 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1052 #endif
1053 #else
1054 int
1055 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1056 #endif
1057 {
1058     int code = 0;
1059     cred_t *credp = crref();
1060     struct vcache *vcp = NULL;
1061     const char *comp = dp->d_name.name;
1062
1063 #if defined(AFS_LINUX26_ENV)
1064     lock_kernel();
1065 #endif
1066     AFS_GLOCK();
1067     code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1068     AFS_GUNLOCK();
1069     
1070     if (vcp) {
1071         struct inode *ip = AFSTOI(vcp);
1072         /* Reset ops if symlink or directory. */
1073 #if defined(AFS_LINUX24_ENV)
1074         if (S_ISREG(ip->i_mode)) {
1075             ip->i_op = &afs_file_iops;
1076             ip->i_fop = &afs_file_fops;
1077             ip->i_data.a_ops = &afs_file_aops;
1078         } else if (S_ISDIR(ip->i_mode)) {
1079             ip->i_op = &afs_dir_iops;
1080             ip->i_fop = &afs_dir_fops;
1081         } else if (S_ISLNK(ip->i_mode)) {
1082             ip->i_op = &afs_symlink_iops;
1083             ip->i_data.a_ops = &afs_symlink_aops;
1084             ip->i_mapping = &ip->i_data;
1085         } else
1086             printk
1087                 ("afs_linux_lookup: ip->i_mode 0x%x  dp->d_name.name %s  code %d\n",
1088                  ip->i_mode, dp->d_name.name, code);
1089 #ifdef STRUCT_INODE_HAS_I_SECURITY
1090        if (ip->i_security == NULL) {
1091            if (security_inode_alloc(ip))
1092                panic("afs_linux_lookup: Cannot allocate inode security");
1093        }
1094 #endif
1095 #else
1096         if (S_ISDIR(ip->i_mode))
1097             ip->i_op = &afs_dir_iops;
1098         else if (S_ISLNK(ip->i_mode))
1099             ip->i_op = &afs_symlink_iops;
1100 #endif
1101     }
1102     dp->d_op = &afs_dentry_operations;
1103     d_add(dp, AFSTOI(vcp));
1104
1105 #if defined(AFS_LINUX26_ENV)
1106     unlock_kernel();
1107 #endif
1108     crfree(credp);
1109
1110     /* It's ok for the file to not be found. That's noted by the caller by
1111      * seeing that the dp->d_inode field is NULL.
1112      */
1113 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1114     if (code == ENOENT)
1115         return ERR_PTR(0);
1116     else
1117         return ERR_PTR(-code);
1118 #else
1119     if (code == ENOENT)
1120         code = 0;
1121     return -code;
1122 #endif
1123 }
1124
1125 int
1126 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1127 {
1128     int code;
1129     cred_t *credp = crref();
1130     const char *name = newdp->d_name.name;
1131     struct inode *oldip = olddp->d_inode;
1132
1133     /* If afs_link returned the vnode, we could instantiate the
1134      * dentry. Since it's not, we drop this one and do a new lookup.
1135      */
1136     d_drop(newdp);
1137
1138     AFS_GLOCK();
1139     code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1140
1141     AFS_GUNLOCK();
1142     crfree(credp);
1143     return -code;
1144 }
1145
1146 int
1147 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1148 {
1149     int code = EBUSY;
1150     cred_t *credp = crref();
1151     const char *name = dp->d_name.name;
1152     struct vcache *tvc = ITOAFS(dp->d_inode);
1153
1154 #if defined(AFS_LINUX26_ENV)
1155     lock_kernel();
1156 #endif
1157     if (((VREFCOUNT(tvc) > 0) && tvc->opens > 0)
1158                                 && !(tvc->states & CUnlinked)) {
1159         struct dentry *__dp;
1160         char *__name;
1161         extern char *afs_newname();
1162
1163         __dp = NULL;
1164         __name = NULL;
1165         do {
1166             dput(__dp);
1167
1168             AFS_GLOCK();
1169             if (__name)
1170                 osi_FreeSmallSpace(__name);
1171             __name = afs_newname();
1172             AFS_GUNLOCK();
1173
1174             __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1175                 
1176             if (IS_ERR(__dp))
1177                 goto out;
1178         } while (__dp->d_inode != NULL);
1179
1180         AFS_GLOCK();
1181         code = afs_rename(ITOAFS(dip), dp->d_name.name, ITOAFS(dip), __dp->d_name.name, credp);
1182         if (!code) {
1183             tvc->mvid = __name;
1184             crhold(credp);
1185             if (tvc->uncred) {
1186                 crfree(tvc->uncred);
1187             }
1188             tvc->uncred = credp;
1189             tvc->states |= CUnlinked;
1190         }
1191         AFS_GUNLOCK();
1192
1193         if (!code)
1194             d_move(dp, __dp);
1195         dput(__dp);
1196
1197         goto out;
1198     }
1199
1200     AFS_GLOCK();
1201     code = afs_remove(ITOAFS(dip), name, credp);
1202     AFS_GUNLOCK();
1203     if (!code)
1204         d_drop(dp);
1205 out:
1206 #if defined(AFS_LINUX26_ENV)
1207     unlock_kernel();
1208 #endif
1209     crfree(credp);
1210     return -code;
1211 }
1212
1213
1214 int
1215 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1216 {
1217     int code;
1218     cred_t *credp = crref();
1219     struct vattr vattr;
1220     const char *name = dp->d_name.name;
1221
1222     /* If afs_symlink returned the vnode, we could instantiate the
1223      * dentry. Since it's not, we drop this one and do a new lookup.
1224      */
1225     d_drop(dp);
1226
1227     AFS_GLOCK();
1228     VATTR_NULL(&vattr);
1229     code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1230     AFS_GUNLOCK();
1231     crfree(credp);
1232     return -code;
1233 }
1234
1235 int
1236 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1237 {
1238     int code;
1239     cred_t *credp = crref();
1240     struct vcache *tvcp = NULL;
1241     struct vattr vattr;
1242     const char *name = dp->d_name.name;
1243
1244 #if defined(AFS_LINUX26_ENV)
1245     lock_kernel();
1246 #endif
1247     AFS_GLOCK();
1248     VATTR_NULL(&vattr);
1249     vattr.va_mask = ATTR_MODE;
1250     vattr.va_mode = mode;
1251     code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1252     AFS_GUNLOCK();
1253
1254     if (tvcp) {
1255         tvcp->v.v_op = &afs_dir_iops;
1256 #if defined(AFS_LINUX24_ENV)
1257         tvcp->v.v_fop = &afs_dir_fops;
1258 #endif
1259         dp->d_op = &afs_dentry_operations;
1260         d_instantiate(dp, AFSTOI(tvcp));
1261     }
1262
1263 #if defined(AFS_LINUX26_ENV)
1264     unlock_kernel();
1265 #endif
1266     crfree(credp);
1267     return -code;
1268 }
1269
1270 int
1271 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1272 {
1273     int code;
1274     cred_t *credp = crref();
1275     const char *name = dp->d_name.name;
1276
1277 #if defined(AFS_LINUX26_ENV)
1278     lock_kernel();
1279 #endif
1280     AFS_GLOCK();
1281     code = afs_rmdir(ITOAFS(dip), name, credp);
1282     AFS_GUNLOCK();
1283
1284     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1285      * that failed because a directory is not empty. So, we map
1286      * EEXIST to ENOTEMPTY on linux.
1287      */
1288     if (code == EEXIST) {
1289         code = ENOTEMPTY;
1290     }
1291
1292     if (!code) {
1293         d_drop(dp);
1294     }
1295
1296 #if defined(AFS_LINUX26_ENV)
1297     unlock_kernel();
1298 #endif
1299     crfree(credp);
1300     return -code;
1301 }
1302
1303
1304
1305 int
1306 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1307                  struct inode *newip, struct dentry *newdp)
1308 {
1309     int code;
1310     cred_t *credp = crref();
1311     const char *oldname = olddp->d_name.name;
1312     const char *newname = newdp->d_name.name;
1313     struct dentry *rehash = NULL;
1314
1315 #if defined(AFS_LINUX26_ENV)
1316     /* Prevent any new references during rename operation. */
1317     lock_kernel();
1318 #endif
1319     /* Remove old and new entries from name hash. New one will change below.
1320      * While it's optimal to catch failures and re-insert newdp into hash,
1321      * it's also error prone and in that case we're already dealing with error
1322      * cases. Let another lookup put things right, if need be.
1323      */
1324 #if defined(AFS_LINUX26_ENV)
1325     if (!d_unhashed(newdp)) {
1326         d_drop(newdp);
1327         rehash = newdp;
1328     }
1329 #else
1330     if (!list_empty(&newdp->d_hash)) {
1331         d_drop(newdp);
1332         rehash = newdp;
1333     }
1334 #endif
1335
1336 #if defined(AFS_LINUX24_ENV)
1337     if (atomic_read(&olddp->d_count) > 1)
1338         shrink_dcache_parent(olddp);
1339 #endif
1340
1341     AFS_GLOCK();
1342     code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip), newname, credp);
1343     AFS_GUNLOCK();
1344
1345     if (rehash)
1346         d_rehash(rehash);
1347
1348 #if defined(AFS_LINUX26_ENV)
1349     unlock_kernel();
1350 #endif
1351
1352     crfree(credp);
1353     return -code;
1354 }
1355
1356
1357 /* afs_linux_ireadlink 
1358  * Internal readlink which can return link contents to user or kernel space.
1359  * Note that the buffer is NOT supposed to be null-terminated.
1360  */
1361 static int
1362 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1363 {
1364     int code;
1365     cred_t *credp = crref();
1366     uio_t tuio;
1367     struct iovec iov;
1368
1369     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1370     code = afs_readlink(ITOAFS(ip), &tuio, credp);
1371     crfree(credp);
1372
1373     if (!code)
1374         return maxlen - tuio.uio_resid;
1375     else
1376         return -code;
1377 }
1378
1379 #if !defined(AFS_LINUX24_ENV)
1380 /* afs_linux_readlink 
1381  * Fill target (which is in user space) with contents of symlink.
1382  */
1383 int
1384 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1385 {
1386     int code;
1387     struct inode *ip = dp->d_inode;
1388
1389     AFS_GLOCK();
1390     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1391     AFS_GUNLOCK();
1392     return code;
1393 }
1394
1395
1396 /* afs_linux_follow_link
1397  * a file system dependent link following routine.
1398  */
1399 struct dentry *
1400 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1401                       unsigned int follow)
1402 {
1403     int code = 0;
1404     char *name;
1405     struct dentry *res;
1406
1407
1408     AFS_GLOCK();
1409     name = osi_Alloc(PATH_MAX + 1);
1410     if (!name) {
1411         AFS_GUNLOCK();
1412         dput(basep);
1413         return ERR_PTR(-EIO);
1414     }
1415
1416     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1417     AFS_GUNLOCK();
1418
1419     if (code < 0) {
1420         dput(basep);
1421         res = ERR_PTR(code);
1422     } else {
1423         name[code] = '\0';
1424         res = lookup_dentry(name, basep, follow);
1425     }
1426
1427     AFS_GLOCK();
1428     osi_Free(name, PATH_MAX + 1);
1429     AFS_GUNLOCK();
1430     return res;
1431 }
1432 #endif
1433
1434 /* afs_linux_readpage
1435  * all reads come through here. A strategy-like read call.
1436  */
1437 int
1438 afs_linux_readpage(struct file *fp, struct page *pp)
1439 {
1440     int code;
1441     cred_t *credp = crref();
1442 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1443     char *address;
1444     afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1445 #else
1446     ulong address = afs_linux_page_address(pp);
1447     afs_offs_t offset = pageoff(pp);
1448 #endif
1449     uio_t tuio;
1450     struct iovec iovec;
1451     struct inode *ip = FILE_INODE(fp);
1452     int cnt = page_count(pp);
1453     struct vcache *avc = ITOAFS(ip);
1454
1455
1456 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1457     address = kmap(pp);
1458     ClearPageError(pp);
1459 #else
1460     atomic_add(1, &pp->count);
1461     set_bit(PG_locked, &pp->flags);     /* other bits? See mm.h */
1462     clear_bit(PG_error, &pp->flags);
1463 #endif
1464
1465     setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1466               AFS_UIOSYS);
1467 #ifdef AFS_LINUX24_ENV
1468     lock_kernel();
1469 #endif
1470     AFS_GLOCK();
1471     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 */
1472     code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1473     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1474                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1475                code);
1476     AFS_GUNLOCK();
1477 #ifdef AFS_LINUX24_ENV
1478     unlock_kernel();
1479 #endif
1480
1481     if (!code) {
1482         if (tuio.uio_resid)     /* zero remainder of page */
1483             memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1484                    tuio.uio_resid);
1485 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1486         flush_dcache_page(pp);
1487         SetPageUptodate(pp);
1488 #else
1489         set_bit(PG_uptodate, &pp->flags);
1490 #endif
1491     }
1492
1493 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1494     kunmap(pp);
1495     UnlockPage(pp);
1496 #else
1497     clear_bit(PG_locked, &pp->flags);
1498     wake_up(&pp->wait);
1499     free_page(address);
1500 #endif
1501
1502     if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1503         struct dcache *tdc;
1504         struct vrequest treq;
1505
1506         AFS_GLOCK();
1507         code = afs_InitReq(&treq, credp);
1508         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1509             tdc = afs_FindDCache(avc, offset);
1510             if (tdc) {
1511                 if (!(tdc->mflags & DFNextStarted))
1512                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1513                 afs_PutDCache(tdc);
1514             }
1515             ReleaseWriteLock(&avc->lock);
1516         }
1517         AFS_GUNLOCK();
1518     }
1519
1520     crfree(credp);
1521     return -code;
1522 }
1523
1524 #if defined(AFS_LINUX24_ENV)
1525 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1526 int
1527 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1528 #else
1529 int
1530 afs_linux_writepage(struct page *pp)
1531 #endif
1532 {
1533     struct address_space *mapping = pp->mapping;
1534     struct inode *inode;
1535     unsigned long end_index;
1536     unsigned offset = PAGE_CACHE_SIZE;
1537     long status;
1538
1539 #if defined(AFS_LINUX26_ENV)
1540     if (PageReclaim(pp)) {
1541         return WRITEPAGE_ACTIVATE;
1542     }
1543 #else
1544     if (PageLaunder(pp)) {
1545         return(fail_writepage(pp));
1546     }
1547 #endif
1548
1549     inode = (struct inode *)mapping->host;
1550     end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1551
1552     /* easy case */
1553     if (pp->index < end_index)
1554         goto do_it;
1555     /* things got complicated... */
1556     offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1557     /* OK, are we completely out? */
1558     if (pp->index >= end_index + 1 || !offset)
1559         return -EIO;
1560   do_it:
1561     status = afs_linux_writepage_sync(inode, pp, 0, offset);
1562     SetPageUptodate(pp);
1563     UnlockPage(pp);
1564     if (status == offset)
1565         return 0;
1566     else
1567         return status;
1568 }
1569 #endif
1570
1571 /* afs_linux_permission
1572  * Check access rights - returns error if can't check or permission denied.
1573  */
1574 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1575 int
1576 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1577 #else
1578 int
1579 afs_linux_permission(struct inode *ip, int mode)
1580 #endif
1581 {
1582     int code;
1583     cred_t *credp = crref();
1584     int tmp = 0;
1585
1586     AFS_GLOCK();
1587     if (mode & MAY_EXEC)
1588         tmp |= VEXEC;
1589     if (mode & MAY_READ)
1590         tmp |= VREAD;
1591     if (mode & MAY_WRITE)
1592         tmp |= VWRITE;
1593     code = afs_access(ITOAFS(ip), tmp, credp);
1594
1595     AFS_GUNLOCK();
1596     crfree(credp);
1597     return -code;
1598 }
1599
1600
1601 #if defined(AFS_LINUX24_ENV)
1602 int
1603 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1604                          unsigned long offset, unsigned int count)
1605 {
1606     struct vcache *vcp = ITOAFS(ip);
1607     char *buffer;
1608     afs_offs_t base;
1609     int code = 0;
1610     cred_t *credp;
1611     uio_t tuio;
1612     struct iovec iovec;
1613     int f_flags = 0;
1614
1615     buffer = kmap(pp) + offset;
1616     base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1617
1618     credp = crref();
1619     lock_kernel();
1620     AFS_GLOCK();
1621     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1622                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1623                ICL_TYPE_INT32, 99999);
1624
1625     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1626
1627     code = afs_write(vcp, &tuio, f_flags, credp, 0);
1628
1629     vcache2inode(vcp);
1630
1631     if (!code
1632         && afs_stats_cmperf.cacheCurrDirtyChunks >
1633         afs_stats_cmperf.cacheMaxDirtyChunks) {
1634         struct vrequest treq;
1635
1636         ObtainWriteLock(&vcp->lock, 533);
1637         if (!afs_InitReq(&treq, credp))
1638             code = afs_DoPartialWrite(vcp, &treq);
1639         ReleaseWriteLock(&vcp->lock);
1640     }
1641     code = code ? -code : count - tuio.uio_resid;
1642
1643     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1644                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1645                ICL_TYPE_INT32, code);
1646
1647     AFS_GUNLOCK();
1648     unlock_kernel();
1649     crfree(credp);
1650     kunmap(pp);
1651
1652     return code;
1653 }
1654
1655 #else
1656 /* afs_linux_updatepage
1657  * What one would have thought was writepage - write dirty page to file.
1658  * Called from generic_file_write. buffer is still in user space. pagep
1659  * has been filled in with old data if we're updating less than a page.
1660  */
1661 int
1662 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1663                      unsigned int count, int sync)
1664 {
1665     struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1666     u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1667     int code = 0;
1668     cred_t *credp;
1669     uio_t tuio;
1670     struct iovec iovec;
1671
1672     set_bit(PG_locked, &pp->flags);
1673
1674     credp = crref();
1675     AFS_GLOCK();
1676     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1677                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1678                ICL_TYPE_INT32, 99999);
1679     setup_uio(&tuio, &iovec, page_addr + offset,
1680               (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1681               AFS_UIOSYS);
1682
1683     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1684
1685     vcache2inode(vcp);
1686
1687     code = code ? -code : count - tuio.uio_resid;
1688     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1689                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1690                ICL_TYPE_INT32, code);
1691
1692     AFS_GUNLOCK();
1693     crfree(credp);
1694
1695     clear_bit(PG_locked, &pp->flags);
1696     return code;
1697 }
1698 #endif
1699
1700 #if defined(AFS_LINUX24_ENV)
1701 static int
1702 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1703                        unsigned to)
1704 {
1705     int code;
1706
1707     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1708                                     offset, to - offset);
1709 #if !defined(AFS_LINUX26_ENV)
1710     kunmap(page);
1711 #endif
1712
1713     return code;
1714 }
1715
1716 static int
1717 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1718                         unsigned to)
1719 {
1720 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1721    call kmap directly instead of relying on us to do it */
1722 #if !defined(AFS_LINUX26_ENV)
1723     kmap(page);
1724 #endif
1725     return 0;
1726 }
1727
1728 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1729 #endif
1730
1731 struct inode_operations afs_file_iops = {
1732 #if defined(AFS_LINUX26_ENV)
1733   .permission =         afs_linux_permission,
1734   .getattr =            afs_linux_getattr,
1735   .setattr =            afs_notify_change,
1736 #elif defined(AFS_LINUX24_ENV)
1737   .permission =         afs_linux_permission,
1738   .revalidate =         afs_linux_revalidate,
1739   .setattr =            afs_notify_change,
1740 #else
1741   .default_file_ops =   &afs_file_fops,
1742   .readpage =           afs_linux_readpage,
1743   .revalidate =         afs_linux_revalidate,
1744   .updatepage =         afs_linux_updatepage,
1745 #endif
1746 };
1747
1748 #if defined(AFS_LINUX24_ENV)
1749 struct address_space_operations afs_file_aops = {
1750   .readpage =           afs_linux_readpage,
1751   .writepage =          afs_linux_writepage,
1752   .commit_write =       afs_linux_commit_write,
1753   .prepare_write =      afs_linux_prepare_write,
1754 };
1755 #endif
1756
1757
1758 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1759  * by what sort of operation is allowed.....
1760  */
1761
1762 struct inode_operations afs_dir_iops = {
1763 #if !defined(AFS_LINUX24_ENV)
1764   .default_file_ops =   &afs_dir_fops,
1765 #else
1766   .setattr =            afs_notify_change,
1767 #endif
1768   .create =             afs_linux_create,
1769   .lookup =             afs_linux_lookup,
1770   .link =               afs_linux_link,
1771   .unlink =             afs_linux_unlink,
1772   .symlink =            afs_linux_symlink,
1773   .mkdir =              afs_linux_mkdir,
1774   .rmdir =              afs_linux_rmdir,
1775   .rename =             afs_linux_rename,
1776 #if defined(AFS_LINUX26_ENV)
1777   .getattr =            afs_linux_getattr,
1778 #else
1779   .revalidate =         afs_linux_revalidate,
1780 #endif
1781   .permission =         afs_linux_permission,
1782 };
1783
1784 /* We really need a separate symlink set of ops, since do_follow_link()
1785  * determines if it _is_ a link by checking if the follow_link op is set.
1786  */
1787 #if defined(AFS_LINUX24_ENV)
1788 static int
1789 afs_symlink_filler(struct file *file, struct page *page)
1790 {
1791     struct inode *ip = (struct inode *)page->mapping->host;
1792     char *p = (char *)kmap(page);
1793     int code;
1794
1795     lock_kernel();
1796     AFS_GLOCK();
1797     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1798     AFS_GUNLOCK();
1799
1800     if (code < 0)
1801         goto fail;
1802     p[code] = '\0';             /* null terminate? */
1803     unlock_kernel();
1804
1805     SetPageUptodate(page);
1806     kunmap(page);
1807     UnlockPage(page);
1808     return 0;
1809
1810   fail:
1811     unlock_kernel();
1812
1813     SetPageError(page);
1814     kunmap(page);
1815     UnlockPage(page);
1816     return code;
1817 }
1818
1819 struct address_space_operations afs_symlink_aops = {
1820   .readpage =   afs_symlink_filler
1821 };
1822 #endif
1823
1824 struct inode_operations afs_symlink_iops = {
1825 #if defined(AFS_LINUX24_ENV)
1826   .readlink =           page_readlink,
1827 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1828   .follow_link =        page_follow_link,
1829 #else
1830   .follow_link =        page_follow_link_light,
1831   .put_link =           page_put_link,
1832 #endif
1833   .setattr =            afs_notify_change,
1834 #else
1835   .readlink =           afs_linux_readlink,
1836   .follow_link =        afs_linux_follow_link,
1837   .permission =         afs_linux_permission,
1838   .revalidate =         afs_linux_revalidate,
1839 #endif
1840 };