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