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