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