4b0c668f7864f40e8c74ea51a1c82652db089b08
[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 = VTOAFS(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 = VTOAFS(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 = VTOAFS(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 = VTOAFS(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 = VTOAFS(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 = VTOAFS(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(VTOAFS(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 = VTOAFS(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 = VTOAFS(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 = VTOAFS(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 = VTOAFS(dp->d_inode);
806     struct vcache *parentvcp = VTOAFS(dp->d_parent->d_inode);
807
808     AFS_GLOCK();
809
810     sysState.allocked = 0;
811
812     /* If it's a negative dentry, then there's nothing to do. */
813     if (!vcp || !parentvcp)
814         goto done;
815
816     if (code = afs_InitReq(&treq, credp))
817         goto done;
818
819     Check_AtSys(parentvcp, dp->d_name.name, &sysState, &treq);
820     name = sysState.name;
821
822     /* First try looking up the DNLC */
823     if (lookupvcp = osi_dnlc_lookup(parentvcp, name, WRITE_LOCK)) {
824         /* Verify that the dentry does not point to an old inode */
825         if (vcp != lookupvcp)
826             goto done;
827         /* Check and correct mvid */
828         if (*name != '/' && vcp->mvstat == 2) 
829             check_bad_parent(dp);
830         vcache2inode(vcp);
831         bad_dentry = 0;
832         goto done;
833     }
834
835     /* A DNLC lookup failure cannot be trusted. Try a real lookup */
836     code = afs_lookup(parentvcp, name, &lookupvcp, credp);
837
838     /* Verify that the dentry does not point to an old inode */
839     if (vcp != lookupvcp)
840         goto done;
841
842     bad_dentry = 0;
843
844 done:
845     /* Clean up */
846     if (lookupvcp)
847         afs_PutVCache(lookupvcp, WRITE_LOCK);
848     if (sysState.allocked)
849         osi_FreeLargeSpace(name);
850
851     AFS_GUNLOCK();
852     crfree(credp);
853
854     if (bad_dentry) {
855         shrink_dcache_parent(dp);
856         d_drop(dp);
857     }
858
859     return !bad_dentry;
860 }
861
862 #ifdef notdef
863 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
864 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
865 #else
866 static int afs_linux_dentry_revalidate(struct dentry *dp)
867 #endif
868 {
869     int code;
870     cred_t *credp;
871     struct vrequest treq;
872     struct inode *ip = AFSTOV(dp->d_inode);
873
874     unsigned long timeout = 3*HZ; /* 3 seconds */
875
876 if (!ip)
877         printk("negative dentry: %s\n", dp->d_name.name);
878
879     if (!(flags & LOOKUP_CONTINUE)) {
880         long diff = CURRENT_TIME - dp->d_parent->d_inode->i_mtime;
881
882         if (diff < 15*60)
883             timeout = 0;
884     }
885
886     if (time_after(jiffies, dp->d_time + timeout))
887         goto out_bad;
888
889  out_valid:
890     return 1;
891
892  out_bad:
893     return 0;
894 }
895 #endif
896
897 /* afs_dentry_iput */
898 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
899 {
900     osi_iput(ip);
901 }
902
903 #if defined(AFS_LINUX24_ENV)
904 struct dentry_operations afs_dentry_operations = {
905        d_revalidate:   afs_linux_dentry_revalidate,
906        d_iput:         afs_dentry_iput,
907 };
908 struct dentry_operations *afs_dops = &afs_dentry_operations;
909 #else
910 struct dentry_operations afs_dentry_operations = {
911         afs_linux_dentry_revalidate,    /* d_validate(struct dentry *) */
912         NULL,                   /* d_hash */
913         NULL,                   /* d_compare */
914         NULL,                   /* d_delete(struct dentry *) */
915         NULL,                   /* d_release(struct dentry *) */
916         afs_dentry_iput         /* d_iput(struct dentry *, struct inode *) */
917 };
918 struct dentry_operations *afs_dops = &afs_dentry_operations;
919 #endif
920
921 /**********************************************************************
922  * AFS Linux inode operations
923  **********************************************************************/
924
925 /* afs_linux_create
926  *
927  * Merely need to set enough of vattr to get us through the create. Note
928  * that the higher level code (open_namei) will take care of any tuncation
929  * explicitly. Exclusive open is also taken care of in open_namei.
930  *
931  * name is in kernel space at this point.
932  */
933 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
934 {
935     int code;
936     cred_t *credp = crref();
937     struct vattr vattr;
938     enum vcexcl excl;
939     const char *name = dp->d_name.name;
940     struct inode *ip;
941
942     VATTR_NULL(&vattr);
943     vattr.va_mode = mode;
944
945     AFS_GLOCK();
946     code = afs_create(VTOAFS(dip), name, &vattr, NONEXCL, mode,
947                       (struct vcache**)&ip, credp);
948
949     if (!code) {
950         vattr2inode(ip, &vattr);
951         /* Reset ops if symlink or directory. */
952 #if defined(AFS_LINUX24_ENV)
953        if (S_ISREG(ip->i_mode)) {
954            ip->i_op = &afs_file_iops;
955            ip->i_fop = &afs_file_fops;
956            ip->i_data.a_ops = &afs_file_aops;
957         } else if (S_ISDIR(ip->i_mode)) {
958            ip->i_op = &afs_dir_iops;
959            ip->i_fop = &afs_dir_fops;
960         } else if (S_ISLNK(ip->i_mode)) {
961            ip->i_op = &afs_symlink_iops;
962            ip->i_data.a_ops = &afs_symlink_aops;
963            ip->i_mapping = &ip->i_data;
964         } else
965            printk("afs_linux_create: FIXME\n");
966 #else
967         if (S_ISDIR(ip->i_mode))
968             ip->i_op = &afs_dir_iops;
969         else if (S_ISLNK(ip->i_mode))
970             ip->i_op = &afs_symlink_iops;
971 #endif
972
973         dp->d_op = afs_dops;
974         dp->d_time = jiffies;
975         d_instantiate(dp, ip);
976     }
977
978     AFS_GUNLOCK();
979     crfree(credp);
980     return -code;
981 }
982
983 /* afs_linux_lookup */
984 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
985 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
986 #else
987 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
988 #endif
989 {
990     int code = 0;
991     cred_t *credp = crref();
992     struct vcache *vcp=NULL;
993     const char *comp = dp->d_name.name;
994     AFS_GLOCK();
995     code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
996
997     if (vcp) {
998         struct inode *ip = AFSTOV(vcp);
999         /* Reset ops if symlink or directory. */
1000 #if defined(AFS_LINUX24_ENV)
1001        if (S_ISREG(ip->i_mode)) {
1002            ip->i_op = &afs_file_iops;
1003            ip->i_fop = &afs_file_fops;
1004            ip->i_data.a_ops = &afs_file_aops;
1005         } else if (S_ISDIR(ip->i_mode)) {
1006            ip->i_op = &afs_dir_iops;
1007            ip->i_fop = &afs_dir_fops;
1008         } else if (S_ISLNK(ip->i_mode)) {
1009            ip->i_op = &afs_symlink_iops;
1010            ip->i_data.a_ops = &afs_symlink_aops;
1011            ip->i_mapping = &ip->i_data;
1012         } else
1013            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);
1014 #else
1015         if (S_ISDIR(ip->i_mode))
1016             ip->i_op = &afs_dir_iops;
1017         else if (S_ISLNK(ip->i_mode))
1018             ip->i_op = &afs_symlink_iops;
1019 #endif
1020     } 
1021     dp->d_time = jiffies;
1022     dp->d_op = afs_dops;
1023     d_add(dp, AFSTOV(vcp));
1024
1025     AFS_GUNLOCK();
1026     crfree(credp);
1027
1028     /* It's ok for the file to not be found. That's noted by the caller by
1029      * seeing that the dp->d_inode field is NULL.
1030      */
1031 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1032     if (code == ENOENT)
1033         return ERR_PTR(0);
1034     else
1035         return ERR_PTR(-code);
1036 #else
1037     if (code == ENOENT)
1038         code = 0;
1039     return -code;
1040 #endif
1041 }
1042
1043 int afs_linux_link(struct dentry *olddp, struct inode *dip,
1044                    struct dentry *newdp)
1045 {
1046     int code;
1047     cred_t *credp = crref();
1048     const char *name = newdp->d_name.name;
1049     struct inode *oldip = olddp->d_inode;
1050
1051     /* If afs_link returned the vnode, we could instantiate the
1052      * dentry. Since it's not, we drop this one and do a new lookup.
1053      */
1054     d_drop(newdp);
1055
1056     AFS_GLOCK();
1057     code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1058
1059     AFS_GUNLOCK();
1060     crfree(credp);
1061     return -code;
1062 }
1063
1064 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
1065 {
1066     int code;
1067     cred_t *credp = crref();
1068     const char *name = dp->d_name.name;
1069     int putback = 0;
1070
1071     AFS_GLOCK();
1072     code = afs_remove(VTOAFS(dip), name, credp);
1073     AFS_GUNLOCK();
1074     if (!code)
1075         d_drop(dp);
1076     crfree(credp);
1077     return -code;
1078 }
1079
1080
1081 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
1082                       const char *target)
1083 {
1084     int code;
1085     cred_t *credp = crref();
1086     struct vattr vattr;
1087     const char *name = dp->d_name.name;
1088
1089     /* If afs_symlink returned the vnode, we could instantiate the
1090      * dentry. Since it's not, we drop this one and do a new lookup.
1091      */
1092     d_drop(dp);
1093
1094     AFS_GLOCK();
1095     VATTR_NULL(&vattr);
1096     code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1097     AFS_GUNLOCK();
1098     crfree(credp);
1099     return -code;
1100 }
1101
1102 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1103 {
1104     int code;
1105     cred_t *credp = crref();
1106     struct vcache *tvcp = NULL;
1107     struct vattr vattr;
1108     const char *name = dp->d_name.name;
1109
1110     AFS_GLOCK();
1111     VATTR_NULL(&vattr);
1112     vattr.va_mask = ATTR_MODE;
1113     vattr.va_mode = mode;
1114     code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1115
1116     if (tvcp) {
1117         tvcp->v.v_op = &afs_dir_iops;
1118 #if defined(AFS_LINUX24_ENV)
1119         tvcp->v.v_fop = &afs_dir_fops;
1120 #endif
1121         dp->d_op = afs_dops;
1122         dp->d_time = jiffies;
1123         d_instantiate(dp, AFSTOV(tvcp));
1124     }
1125
1126     AFS_GUNLOCK();
1127     crfree(credp);
1128     return -code;
1129 }
1130
1131 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1132 {
1133     int code;
1134     cred_t *credp = crref();
1135     const char *name = dp->d_name.name;
1136
1137     AFS_GLOCK();
1138     code = afs_rmdir(VTOAFS(dip), name, credp);
1139
1140     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1141      * that failed because a directory is not empty. So, we map
1142      * EEXIST to ENOTEMPTY on linux.
1143      */
1144     if (code == EEXIST) {
1145         code = ENOTEMPTY;
1146     }
1147     
1148     if (!code) {
1149         d_drop(dp);
1150     }
1151
1152     AFS_GUNLOCK();
1153     crfree(credp);
1154     return -code;
1155 }
1156
1157
1158
1159 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1160                      struct inode *newip, struct dentry *newdp)
1161 {
1162     int code;
1163     cred_t *credp = crref();
1164     const char *oldname = olddp->d_name.name;
1165     const char *newname = newdp->d_name.name;
1166
1167     /* Remove old and new entries from name hash. New one will change below.
1168      * While it's optimal to catch failures and re-insert newdp into hash,
1169      * it's also error prone and in that case we're already dealing with error
1170      * cases. Let another lookup put things right, if need be.
1171      */
1172     if (!list_empty(&olddp->d_hash)) {
1173         d_drop(olddp);
1174     }
1175     if (!list_empty(&newdp->d_hash)) {
1176         d_drop(newdp);
1177     }
1178     AFS_GLOCK();
1179     code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip),
1180                       newname, credp);
1181     AFS_GUNLOCK();
1182
1183     if (!code) {
1184         /* update time so it doesn't expire immediately */
1185         newdp->d_time = jiffies;
1186         d_move(olddp, newdp);
1187     }
1188
1189     crfree(credp);
1190     return -code;
1191 }
1192
1193
1194 /* afs_linux_ireadlink 
1195  * Internal readlink which can return link contents to user or kernel space.
1196  * Note that the buffer is NOT supposed to be null-terminated.
1197  */
1198 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
1199                         uio_seg_t seg)
1200 {
1201     int code;
1202     cred_t *credp = crref();
1203     uio_t tuio;
1204     struct iovec iov;
1205
1206     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1207     code = afs_readlink(VTOAFS(ip), &tuio, credp);
1208     crfree(credp);
1209
1210     if (!code)
1211         return maxlen - tuio.uio_resid;
1212     else
1213         return -code;
1214 }
1215
1216 #if !defined(AFS_LINUX24_ENV)
1217 /* afs_linux_readlink 
1218  * Fill target (which is in user space) with contents of symlink.
1219  */
1220 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1221 {
1222     int code;
1223     struct inode *ip = dp->d_inode;
1224
1225     AFS_GLOCK();
1226     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1227     AFS_GUNLOCK();
1228     return code;
1229 }
1230
1231
1232 /* afs_linux_follow_link
1233  * a file system dependent link following routine.
1234  */
1235 struct dentry * afs_linux_follow_link(struct dentry *dp,
1236                                       struct dentry *basep,
1237                                       unsigned int follow)
1238 {
1239     int code = 0;
1240     char *name;
1241     struct dentry *res;
1242
1243
1244     AFS_GLOCK();
1245     name = osi_Alloc(PATH_MAX+1);
1246     if (!name) {
1247         AFS_GUNLOCK();
1248         dput(basep);
1249         return ERR_PTR(-EIO);
1250     }
1251
1252     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1253     AFS_GUNLOCK();
1254
1255     if (code<0) {
1256         dput(basep);
1257         res = ERR_PTR(code);
1258     }
1259     else {
1260         name[code] = '\0';
1261         res = lookup_dentry(name, basep, follow);
1262     }
1263
1264     AFS_GLOCK();
1265     osi_Free(name, PATH_MAX+1);
1266     AFS_GUNLOCK();
1267     return res;
1268 }
1269 #endif
1270
1271 /* afs_linux_readpage
1272  * all reads come through here. A strategy-like read call.
1273  */
1274 int afs_linux_readpage(struct file *fp, struct page *pp)
1275 {
1276     int code;
1277     cred_t *credp = crref();
1278 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1279     char *address;
1280     afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1281 #else
1282     ulong address = afs_linux_page_address(pp);
1283     afs_offs_t offset = pageoff(pp);
1284 #endif
1285     uio_t tuio;
1286     struct iovec iovec;
1287     struct inode *ip = FILE_INODE(fp);
1288     int cnt = atomic_read(&pp->count);
1289
1290     AFS_GLOCK();
1291     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1292                ICL_TYPE_POINTER, ip,
1293                ICL_TYPE_POINTER, pp,
1294                ICL_TYPE_INT32, cnt,
1295                ICL_TYPE_INT32, 99999); /* not a possible code value */
1296 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1297     address = kmap(pp);
1298     ClearPageError(pp);
1299
1300     lock_kernel();
1301 #else
1302     atomic_add(1, &pp->count);
1303     set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
1304     clear_bit(PG_error, &pp->flags);
1305 #endif
1306
1307     setup_uio(&tuio, &iovec, (char*)address, offset, PAGESIZE,
1308               UIO_READ, AFS_UIOSYS);
1309     code = afs_rdwr(VTOAFS(ip), &tuio, UIO_READ, 0, credp);
1310 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1311     unlock_kernel();
1312 #endif
1313
1314     if (!code) {
1315         if (tuio.uio_resid) /* zero remainder of page */
1316             memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
1317                    tuio.uio_resid);
1318 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1319         flush_dcache_page(pp);
1320         SetPageUptodate(pp);
1321 #else
1322         set_bit(PG_uptodate, &pp->flags);
1323 #endif
1324     }
1325
1326 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1327     kunmap(pp);
1328     UnlockPage(pp);
1329 #else
1330     clear_bit(PG_locked, &pp->flags);
1331     wake_up(&pp->wait);
1332     free_page(address);
1333 #endif
1334
1335     crfree(credp);
1336     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
1337                ICL_TYPE_POINTER, ip,
1338                ICL_TYPE_POINTER, pp,
1339                ICL_TYPE_INT32, cnt,
1340                ICL_TYPE_INT32, code);
1341     AFS_GUNLOCK();
1342     return -code;
1343 }
1344
1345 #if defined(AFS_LINUX24_ENV)
1346 int afs_linux_writepage(struct page *pp)
1347 {
1348     struct address_space *mapping = pp->mapping;
1349     struct inode *inode;
1350     unsigned long end_index;
1351     unsigned offset = PAGE_CACHE_SIZE;
1352     long status;
1353
1354     inode = (struct inode *) mapping->host;
1355     end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1356
1357     /* easy case */
1358     if (pp->index < end_index)
1359         goto do_it;
1360     /* things got complicated... */
1361     offset = inode->i_size & (PAGE_CACHE_SIZE-1);
1362     /* OK, are we completely out? */
1363     if (pp->index >= end_index+1 || !offset)
1364         return -EIO;
1365 do_it:
1366     AFS_GLOCK();
1367     status = afs_linux_writepage_sync(inode, pp, 0, offset);
1368     AFS_GUNLOCK();
1369     SetPageUptodate(pp);
1370     UnlockPage(pp);
1371     if (status == offset)
1372         return 0;
1373     else
1374         return status;
1375 }
1376 #endif
1377
1378 #ifdef NOTUSED
1379 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1380 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1381
1382 /* afs_linux_truncate
1383  * Handles discarding disk blocks if this were a device. ext2 indicates we
1384  * may need to zero partial last pages of memory mapped files.
1385  */
1386 void afs_linux_truncate(struct inode *ip)
1387 {
1388 }
1389 #endif
1390
1391 /* afs_linux_permission
1392  * Check access rights - returns error if can't check or permission denied.
1393  */
1394 int afs_linux_permission(struct inode *ip, int mode)
1395 {
1396     int code;
1397     cred_t *credp = crref();
1398     int tmp = 0;
1399
1400     AFS_GLOCK();
1401     if (mode & MAY_EXEC) tmp |= VEXEC;
1402     if (mode & MAY_READ) tmp |= VREAD;
1403     if (mode & MAY_WRITE) tmp |= VWRITE;
1404     code = afs_access(VTOAFS(ip), tmp, credp);
1405
1406     AFS_GUNLOCK();
1407     crfree(credp);
1408     return -code;
1409 }
1410
1411
1412 #ifdef NOTUSED
1413 /* msdos sector mapping hack for memory mapping. */
1414 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1415 #endif
1416
1417 #if defined(AFS_LINUX24_ENV)
1418 int afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1419                         unsigned long offset,
1420                         unsigned int count)
1421 {
1422     struct vcache *vcp = VTOAFS(ip);
1423     char *buffer;
1424     afs_offs_t base;
1425     int code = 0;
1426     cred_t *credp;
1427     uio_t tuio;
1428     struct iovec iovec;
1429     int f_flags = 0;
1430
1431     buffer = kmap(pp) + offset;
1432     base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1433
1434     credp = crref();
1435     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1436               ICL_TYPE_POINTER, pp,
1437               ICL_TYPE_INT32, atomic_read(&pp->count),
1438               ICL_TYPE_INT32, 99999);
1439     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1440
1441     code = afs_write(vcp, &tuio, f_flags, credp, 0);
1442
1443     vcache2inode(vcp);
1444
1445     code = code ? -code : count - tuio.uio_resid;
1446     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1447               ICL_TYPE_POINTER, pp,
1448               ICL_TYPE_INT32, atomic_read(&pp->count),
1449               ICL_TYPE_INT32, code);
1450
1451     crfree(credp);
1452     kunmap(pp);
1453
1454     return code;
1455
1456
1457 static int
1458 afs_linux_updatepage(struct file *file, struct page *page, 
1459                      unsigned long offset, unsigned int count)
1460 {
1461     struct dentry *dentry = file->f_dentry;
1462
1463     return afs_linux_writepage_sync(dentry->d_inode, page, offset, count);
1464 }
1465 #else
1466 /* afs_linux_updatepage
1467  * What one would have thought was writepage - write dirty page to file.
1468  * Called from generic_file_write. buffer is still in user space. pagep
1469  * has been filled in with old data if we're updating less than a page.
1470  */
1471 int afs_linux_updatepage(struct file *fp, struct page *pp,
1472                          unsigned long offset,
1473                          unsigned int count, int sync)
1474 {
1475     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1476     u8 *page_addr = (u8*) afs_linux_page_address(pp);
1477     int code = 0;
1478     cred_t *credp;
1479     uio_t tuio;
1480     struct iovec iovec;
1481     
1482     set_bit(PG_locked, &pp->flags);
1483
1484     credp = crref();
1485     AFS_GLOCK();
1486     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1487                ICL_TYPE_POINTER, pp,
1488                ICL_TYPE_INT32, atomic_read(&pp->count),
1489                ICL_TYPE_INT32, 99999);
1490     setup_uio(&tuio, &iovec, page_addr + offset, (afs_offs_t)(pageoff(pp) + offset),
1491                 count, UIO_WRITE, AFS_UIOSYS);
1492
1493     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1494
1495     vcache2inode(vcp);
1496
1497     code = code ? -code : count - tuio.uio_resid;
1498     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1499                ICL_TYPE_POINTER, pp,
1500                ICL_TYPE_INT32, atomic_read(&pp->count),
1501                ICL_TYPE_INT32, code);
1502
1503     AFS_GUNLOCK();
1504     crfree(credp);
1505
1506     clear_bit(PG_locked, &pp->flags);
1507     return code;
1508 }
1509 #endif
1510
1511 #if defined(AFS_LINUX24_ENV)
1512 static int afs_linux_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
1513 {
1514     int code;
1515
1516     AFS_GLOCK();
1517     lock_kernel();
1518     code = afs_linux_updatepage(file, page, offset, to-offset);
1519     unlock_kernel();
1520     AFS_GUNLOCK();
1521     kunmap(page);
1522
1523     return code;
1524 }
1525
1526 static int afs_linux_prepare_write(struct file *file, struct page *page,
1527                                    unsigned from, unsigned to)
1528 {
1529     kmap(page);
1530     return 0;
1531 }
1532
1533 extern int afs_notify_change(struct dentry *dp, struct iattr* iattrp);
1534 #endif
1535
1536 #if defined(AFS_LINUX24_ENV)
1537 struct inode_operations afs_file_iops = {
1538     revalidate:                afs_linux_revalidate,
1539     setattr:           afs_notify_change,
1540     permission:                afs_linux_permission,
1541 };
1542 struct address_space_operations afs_file_aops = {
1543         readpage: afs_linux_readpage,
1544         writepage: afs_linux_writepage,
1545         commit_write: afs_linux_commit_write,
1546         prepare_write: afs_linux_prepare_write,
1547 };
1548
1549 struct inode_operations *afs_ops = &afs_file_iops;
1550 #else
1551 struct inode_operations afs_iops = {
1552     &afs_file_fops,     /* file operations */
1553     NULL,               /* afs_linux_create */
1554     NULL,               /* afs_linux_lookup */
1555     NULL,               /* afs_linux_link */
1556     NULL,               /* afs_linux_unlink */
1557     NULL,               /* afs_linux_symlink */
1558     NULL,               /* afs_linux_mkdir */
1559     NULL,               /* afs_linux_rmdir */
1560     NULL,               /* afs_linux_mknod */
1561     NULL,               /* afs_linux_rename */
1562     NULL,               /* afs_linux_readlink */
1563     NULL,               /* afs_linux_follow_link */
1564     afs_linux_readpage,
1565     NULL,               /* afs_linux_writepage */
1566     NULL,               /* afs_linux_bmap */
1567     NULL,               /* afs_linux_truncate */
1568     afs_linux_permission,
1569     NULL,               /* afs_linux_smap */
1570     afs_linux_updatepage,
1571     afs_linux_revalidate,
1572 };
1573
1574 struct inode_operations *afs_ops = &afs_iops;
1575 #endif
1576
1577 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1578  * by what sort of operation is allowed.....
1579  */
1580 #if defined(AFS_LINUX24_ENV)
1581 struct inode_operations afs_dir_iops = {
1582     create:    afs_linux_create,
1583     lookup:    afs_linux_lookup,
1584     link:      afs_linux_link,
1585     unlink:    afs_linux_unlink,
1586     symlink:   afs_linux_symlink,
1587     mkdir:     afs_linux_mkdir,
1588     rmdir:     afs_linux_rmdir,
1589     rename:    afs_linux_rename,
1590     revalidate:        afs_linux_revalidate,
1591     setattr:   afs_notify_change,
1592     permission:        afs_linux_permission,
1593 };
1594 #else
1595 struct inode_operations afs_dir_iops = {
1596     &afs_dir_fops,      /* file operations for directories */
1597     afs_linux_create,
1598     afs_linux_lookup,
1599     afs_linux_link,
1600     afs_linux_unlink,
1601     afs_linux_symlink,
1602     afs_linux_mkdir,
1603     afs_linux_rmdir,
1604     NULL,               /* afs_linux_mknod */
1605     afs_linux_rename,
1606     NULL,               /* afs_linux_readlink */
1607     NULL,               /* afs_linux_follow_link */
1608     NULL,               /* afs_linux_readpage */
1609     NULL,               /* afs_linux_writepage */
1610     NULL,               /* afs_linux_bmap */
1611     NULL,               /* afs_linux_truncate */
1612     afs_linux_permission,
1613     NULL,               /* afs_linux_smap */
1614     NULL,               /* afs_linux_updatepage */
1615     afs_linux_revalidate,
1616 };
1617 #endif
1618
1619 /* We really need a separate symlink set of ops, since do_follow_link()
1620  * determines if it _is_ a link by checking if the follow_link op is set.
1621  */
1622 #if defined(AFS_LINUX24_ENV)
1623 static int afs_symlink_filler(struct file *file, struct page *page)
1624 {
1625     struct inode *ip = (struct inode *) page->mapping->host;
1626     char *p = (char *)kmap(page);
1627     int code;
1628
1629     AFS_GLOCK();
1630     lock_kernel();
1631     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1632
1633     if (code<0)
1634            goto fail;
1635     p[code] = '\0';            /* null terminate? */
1636     unlock_kernel();
1637     AFS_GUNLOCK();
1638
1639     SetPageUptodate(page);
1640     kunmap(page);
1641     UnlockPage(page);
1642     return 0;
1643
1644 fail:
1645     unlock_kernel();
1646     AFS_GUNLOCK();
1647
1648     SetPageError(page);
1649     kunmap(page);
1650     UnlockPage(page);
1651     return code;
1652 }
1653
1654 struct address_space_operations afs_symlink_aops = {
1655        readpage:       afs_symlink_filler
1656 };
1657
1658 struct inode_operations afs_symlink_iops = {
1659     readlink:          page_readlink,
1660     follow_link:       page_follow_link,
1661     setattr:           afs_notify_change,
1662 };
1663 #else
1664 struct inode_operations afs_symlink_iops = {
1665     NULL,               /* file operations */
1666     NULL,               /* create */
1667     NULL,               /* lookup */
1668     NULL,               /* link */
1669     NULL,               /* unlink */
1670     NULL,               /* symlink */
1671     NULL,               /* mkdir */
1672     NULL,               /* rmdir */
1673     NULL,               /* afs_linux_mknod */
1674     NULL,               /* rename */
1675     afs_linux_readlink,
1676     afs_linux_follow_link,
1677     NULL,               /* readpage */
1678     NULL,               /* afs_linux_writepage */
1679     NULL,               /* afs_linux_bmap */
1680     NULL,               /* afs_linux_truncate */
1681     afs_linux_permission, /* tho the code appears to indicate not used? */
1682     NULL,               /* afs_linux_smap */
1683     NULL,               /* updatepage */
1684     afs_linux_revalidate, /* tho the code appears to indicate not used? */
1685 };
1686 #endif