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