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