linux-2619-aio-and-read-write-changes-20061006
[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     ObtainWriteLock(&vcp->lock, 537);
843     (void) afs_InactiveVCache(vcp, NULL);
844     ReleaseWriteLock(&vcp->lock);
845     AFS_GUNLOCK();
846
847     iput(ip);
848 }
849
850 static int
851 afs_dentry_delete(struct dentry *dp)
852 {
853     if (dp->d_inode && (VTOAFS(dp->d_inode)->states & CUnlinked))
854         return 1;               /* bad inode? */
855
856     return 0;
857 }
858
859 struct dentry_operations afs_dentry_operations = {
860   .d_revalidate =       afs_linux_dentry_revalidate,
861   .d_delete =           afs_dentry_delete,
862   .d_iput =             afs_dentry_iput,
863 };
864
865 /**********************************************************************
866  * AFS Linux inode operations
867  **********************************************************************/
868
869 /* afs_linux_create
870  *
871  * Merely need to set enough of vattr to get us through the create. Note
872  * that the higher level code (open_namei) will take care of any tuncation
873  * explicitly. Exclusive open is also taken care of in open_namei.
874  *
875  * name is in kernel space at this point.
876  */
877 static int
878 #ifdef IOP_CREATE_TAKES_NAMEIDATA
879 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
880                  struct nameidata *nd)
881 #else
882 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
883 #endif
884 {
885     struct vattr vattr;
886     cred_t *credp = crref();
887     const char *name = dp->d_name.name;
888     struct vcache *vcp;
889     int code;
890
891     VATTR_NULL(&vattr);
892     vattr.va_mode = mode;
893     vattr.va_type = mode & S_IFMT;
894
895 #if defined(AFS_LINUX26_ENV)
896     lock_kernel();
897 #endif
898     AFS_GLOCK();
899     code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
900                       &vcp, credp);
901
902     if (!code) {
903         struct inode *ip = AFSTOV(vcp);
904
905         afs_getattr(vcp, &vattr, credp);
906         afs_fill_inode(ip, &vattr);
907         dp->d_op = &afs_dentry_operations;
908         dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
909         d_instantiate(dp, ip);
910     }
911     AFS_GUNLOCK();
912
913 #if defined(AFS_LINUX26_ENV)
914     unlock_kernel();
915 #endif
916     crfree(credp);
917     return -code;
918 }
919
920 /* afs_linux_lookup */
921 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
922 static struct dentry *
923 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
924 afs_linux_lookup(struct inode *dip, struct dentry *dp,
925                  struct nameidata *nd)
926 #else
927 afs_linux_lookup(struct inode *dip, struct dentry *dp)
928 #endif
929 #else
930 static int
931 afs_linux_lookup(struct inode *dip, struct dentry *dp)
932 #endif
933 {
934     cred_t *credp = crref();
935     struct vcache *vcp = NULL;
936     const char *comp = dp->d_name.name;
937     struct inode *ip = NULL;
938 #if defined(AFS_LINUX26_ENV)
939     struct dentry *newdp = NULL;
940 #endif
941     int code;
942
943 #if defined(AFS_LINUX26_ENV)
944     lock_kernel();
945 #endif
946     AFS_GLOCK();
947     code = afs_lookup(VTOAFS(dip), comp, &vcp, credp);
948     
949     if (vcp) {
950         struct vattr vattr;
951
952         ip = AFSTOV(vcp);
953         afs_getattr(vcp, &vattr, credp);
954         afs_fill_inode(ip, &vattr);
955     }
956     dp->d_op = &afs_dentry_operations;
957     dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
958     AFS_GUNLOCK();
959
960 #if defined(AFS_LINUX24_ENV)
961     if (ip && S_ISDIR(ip->i_mode)) {
962         struct dentry *alias;
963
964         /* Try to invalidate an existing alias in favor of our new one */
965         alias = d_find_alias(ip);
966 #if defined(AFS_LINUX26_ENV)
967         /* But not if it's disconnected; then we want d_splice_alias below */
968         if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
969 #else
970         if (alias) {
971 #endif
972             if (d_invalidate(alias) == 0) {
973                 dput(alias);
974             } else {
975                 iput(ip);
976 #if defined(AFS_LINUX26_ENV)
977                 unlock_kernel();
978 #endif
979                 return alias;
980             }
981         }
982     }
983 #endif
984 #if defined(AFS_LINUX26_ENV)
985     newdp = d_splice_alias(ip, dp);
986 #else
987     d_add(dp, ip);
988 #endif
989
990 #if defined(AFS_LINUX26_ENV)
991     unlock_kernel();
992 #endif
993     crfree(credp);
994
995     /* It's ok for the file to not be found. That's noted by the caller by
996      * seeing that the dp->d_inode field is NULL.
997      */
998 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
999 #if defined(AFS_LINUX26_ENV)
1000     if (!code || code == ENOENT)
1001         return newdp;
1002 #else
1003     if (code == ENOENT)
1004         return ERR_PTR(0);
1005 #endif
1006     else
1007         return ERR_PTR(-code);
1008 #else
1009     if (code == ENOENT)
1010         code = 0;
1011     return -code;
1012 #endif
1013 }
1014
1015 static int
1016 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1017 {
1018     int code;
1019     cred_t *credp = crref();
1020     const char *name = newdp->d_name.name;
1021     struct inode *oldip = olddp->d_inode;
1022
1023     /* If afs_link returned the vnode, we could instantiate the
1024      * dentry. Since it's not, we drop this one and do a new lookup.
1025      */
1026     d_drop(newdp);
1027
1028     AFS_GLOCK();
1029     code = afs_link(VTOAFS(oldip), VTOAFS(dip), name, credp);
1030
1031     AFS_GUNLOCK();
1032     crfree(credp);
1033     return -code;
1034 }
1035
1036 static int
1037 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1038 {
1039     int code = EBUSY;
1040     cred_t *credp = crref();
1041     const char *name = dp->d_name.name;
1042     struct vcache *tvc = VTOAFS(dp->d_inode);
1043
1044 #if defined(AFS_LINUX26_ENV)
1045     lock_kernel();
1046 #endif
1047     if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1048                                 && !(tvc->states & CUnlinked)) {
1049         struct dentry *__dp;
1050         char *__name;
1051         extern char *afs_newname();
1052
1053         __dp = NULL;
1054         __name = NULL;
1055         do {
1056             dput(__dp);
1057
1058             AFS_GLOCK();
1059             if (__name)
1060                 osi_FreeSmallSpace(__name);
1061             __name = afs_newname();
1062             AFS_GUNLOCK();
1063
1064             __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1065                 
1066             if (IS_ERR(__dp))
1067                 goto out;
1068         } while (__dp->d_inode != NULL);
1069
1070         AFS_GLOCK();
1071         code = afs_rename(VTOAFS(dip), dp->d_name.name, VTOAFS(dip), __dp->d_name.name, credp);
1072         if (!code) {
1073             tvc->mvid = (void *) __name;
1074             crhold(credp);
1075             if (tvc->uncred) {
1076                 crfree(tvc->uncred);
1077             }
1078             tvc->uncred = credp;
1079             tvc->states |= CUnlinked;
1080         }
1081         AFS_GUNLOCK();
1082
1083         if (!code) {
1084             __dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1085             d_move(dp, __dp);
1086         }
1087         dput(__dp);
1088
1089         goto out;
1090     }
1091
1092     AFS_GLOCK();
1093     code = afs_remove(VTOAFS(dip), name, credp);
1094     AFS_GUNLOCK();
1095     if (!code)
1096         d_drop(dp);
1097 out:
1098 #if defined(AFS_LINUX26_ENV)
1099     unlock_kernel();
1100 #endif
1101     crfree(credp);
1102     return -code;
1103 }
1104
1105
1106 static int
1107 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1108 {
1109     int code;
1110     cred_t *credp = crref();
1111     struct vattr vattr;
1112     const char *name = dp->d_name.name;
1113
1114     /* If afs_symlink returned the vnode, we could instantiate the
1115      * dentry. Since it's not, we drop this one and do a new lookup.
1116      */
1117     d_drop(dp);
1118
1119     VATTR_NULL(&vattr);
1120     AFS_GLOCK();
1121     code = afs_symlink(VTOAFS(dip), name, &vattr, target, credp);
1122     AFS_GUNLOCK();
1123     crfree(credp);
1124     return -code;
1125 }
1126
1127 static int
1128 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1129 {
1130     int code;
1131     cred_t *credp = crref();
1132     struct vcache *tvcp = NULL;
1133     struct vattr vattr;
1134     const char *name = dp->d_name.name;
1135
1136 #if defined(AFS_LINUX26_ENV)
1137     lock_kernel();
1138 #endif
1139     VATTR_NULL(&vattr);
1140     vattr.va_mask = ATTR_MODE;
1141     vattr.va_mode = mode;
1142     AFS_GLOCK();
1143     code = afs_mkdir(VTOAFS(dip), name, &vattr, &tvcp, credp);
1144
1145     if (tvcp) {
1146         struct inode *ip = AFSTOV(tvcp);
1147
1148         afs_getattr(tvcp, &vattr, credp);
1149         afs_fill_inode(ip, &vattr);
1150
1151         dp->d_op = &afs_dentry_operations;
1152         dp->d_time = hgetlo(VTOAFS(dip)->m.DataVersion);
1153         d_instantiate(dp, ip);
1154     }
1155     AFS_GUNLOCK();
1156
1157 #if defined(AFS_LINUX26_ENV)
1158     unlock_kernel();
1159 #endif
1160     crfree(credp);
1161     return -code;
1162 }
1163
1164 static int
1165 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1166 {
1167     int code;
1168     cred_t *credp = crref();
1169     const char *name = dp->d_name.name;
1170
1171     /* locking kernel conflicts with glock? */
1172
1173     AFS_GLOCK();
1174     code = afs_rmdir(VTOAFS(dip), name, credp);
1175     AFS_GUNLOCK();
1176
1177     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1178      * that failed because a directory is not empty. So, we map
1179      * EEXIST to ENOTEMPTY on linux.
1180      */
1181     if (code == EEXIST) {
1182         code = ENOTEMPTY;
1183     }
1184
1185     if (!code) {
1186         d_drop(dp);
1187     }
1188
1189     crfree(credp);
1190     return -code;
1191 }
1192
1193
1194 static int
1195 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1196                  struct inode *newip, struct dentry *newdp)
1197 {
1198     int code;
1199     cred_t *credp = crref();
1200     const char *oldname = olddp->d_name.name;
1201     const char *newname = newdp->d_name.name;
1202     struct dentry *rehash = NULL;
1203
1204 #if defined(AFS_LINUX26_ENV)
1205     /* Prevent any new references during rename operation. */
1206     lock_kernel();
1207 #endif
1208     /* Remove old and new entries from name hash. New one will change below.
1209      * While it's optimal to catch failures and re-insert newdp into hash,
1210      * it's also error prone and in that case we're already dealing with error
1211      * cases. Let another lookup put things right, if need be.
1212      */
1213 #if defined(AFS_LINUX26_ENV)
1214     if (!d_unhashed(newdp)) {
1215         d_drop(newdp);
1216         rehash = newdp;
1217     }
1218 #else
1219     if (!list_empty(&newdp->d_hash)) {
1220         d_drop(newdp);
1221         rehash = newdp;
1222     }
1223 #endif
1224
1225 #if defined(AFS_LINUX24_ENV)
1226     if (atomic_read(&olddp->d_count) > 1)
1227         shrink_dcache_parent(olddp);
1228 #endif
1229
1230     AFS_GLOCK();
1231     code = afs_rename(VTOAFS(oldip), oldname, VTOAFS(newip), newname, credp);
1232     AFS_GUNLOCK();
1233
1234     if (rehash)
1235         d_rehash(rehash);
1236
1237 #if defined(AFS_LINUX26_ENV)
1238     unlock_kernel();
1239 #endif
1240
1241     crfree(credp);
1242     return -code;
1243 }
1244
1245
1246 /* afs_linux_ireadlink 
1247  * Internal readlink which can return link contents to user or kernel space.
1248  * Note that the buffer is NOT supposed to be null-terminated.
1249  */
1250 static int
1251 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1252 {
1253     int code;
1254     cred_t *credp = crref();
1255     uio_t tuio;
1256     struct iovec iov;
1257
1258     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1259     code = afs_readlink(VTOAFS(ip), &tuio, credp);
1260     crfree(credp);
1261
1262     if (!code)
1263         return maxlen - tuio.uio_resid;
1264     else
1265         return -code;
1266 }
1267
1268 #if !defined(AFS_LINUX24_ENV)
1269 /* afs_linux_readlink 
1270  * Fill target (which is in user space) with contents of symlink.
1271  */
1272 static int
1273 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1274 {
1275     int code;
1276     struct inode *ip = dp->d_inode;
1277
1278     AFS_GLOCK();
1279     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1280     AFS_GUNLOCK();
1281     return code;
1282 }
1283
1284
1285 /* afs_linux_follow_link
1286  * a file system dependent link following routine.
1287  */
1288 static struct dentry *
1289 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1290                       unsigned int follow)
1291 {
1292     int code = 0;
1293     char *name;
1294     struct dentry *res;
1295
1296
1297     AFS_GLOCK();
1298     name = osi_Alloc(PATH_MAX + 1);
1299     if (!name) {
1300         AFS_GUNLOCK();
1301         dput(basep);
1302         return ERR_PTR(-EIO);
1303     }
1304
1305     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1306     AFS_GUNLOCK();
1307
1308     if (code < 0) {
1309         dput(basep);
1310         res = ERR_PTR(code);
1311     } else {
1312         name[code] = '\0';
1313         res = lookup_dentry(name, basep, follow);
1314     }
1315
1316     AFS_GLOCK();
1317     osi_Free(name, PATH_MAX + 1);
1318     AFS_GUNLOCK();
1319     return res;
1320 }
1321 #endif
1322
1323 /* afs_linux_readpage
1324  * all reads come through here. A strategy-like read call.
1325  */
1326 static int
1327 afs_linux_readpage(struct file *fp, struct page *pp)
1328 {
1329     int code;
1330     cred_t *credp = crref();
1331 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1332     char *address;
1333     afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1334 #else
1335     ulong address = afs_linux_page_address(pp);
1336     afs_offs_t offset = pageoff(pp);
1337 #endif
1338     uio_t tuio;
1339     struct iovec iovec;
1340     struct inode *ip = FILE_INODE(fp);
1341     int cnt = page_count(pp);
1342     struct vcache *avc = VTOAFS(ip);
1343
1344
1345 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1346     address = kmap(pp);
1347     ClearPageError(pp);
1348 #else
1349     atomic_add(1, &pp->count);
1350     set_bit(PG_locked, &pp->flags);     /* other bits? See mm.h */
1351     clear_bit(PG_error, &pp->flags);
1352 #endif
1353
1354     setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1355               AFS_UIOSYS);
1356 #ifdef AFS_LINUX24_ENV
1357     lock_kernel();
1358 #endif
1359     AFS_GLOCK();
1360     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 */
1361     code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1362     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1363                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1364                code);
1365     AFS_GUNLOCK();
1366 #ifdef AFS_LINUX24_ENV
1367     unlock_kernel();
1368 #endif
1369
1370     if (!code) {
1371         if (tuio.uio_resid)     /* zero remainder of page */
1372             memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1373                    tuio.uio_resid);
1374 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1375         flush_dcache_page(pp);
1376         SetPageUptodate(pp);
1377 #else
1378         set_bit(PG_uptodate, &pp->flags);
1379 #endif
1380     }
1381
1382 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1383     kunmap(pp);
1384     UnlockPage(pp);
1385 #else
1386     clear_bit(PG_locked, &pp->flags);
1387     wake_up(&pp->wait);
1388     free_page(address);
1389 #endif
1390
1391     if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1392         struct dcache *tdc;
1393         struct vrequest treq;
1394
1395         AFS_GLOCK();
1396         code = afs_InitReq(&treq, credp);
1397         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1398             tdc = afs_FindDCache(avc, offset);
1399             if (tdc) {
1400                 if (!(tdc->mflags & DFNextStarted))
1401                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1402                 afs_PutDCache(tdc);
1403             }
1404             ReleaseWriteLock(&avc->lock);
1405         }
1406         AFS_GUNLOCK();
1407     }
1408
1409     crfree(credp);
1410     return -code;
1411 }
1412
1413
1414 #if defined(AFS_LINUX24_ENV)
1415 static int
1416 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1417                          unsigned long offset, unsigned int count)
1418 {
1419     struct vcache *vcp = VTOAFS(ip);
1420     char *buffer;
1421     afs_offs_t base;
1422     int code = 0;
1423     cred_t *credp;
1424     uio_t tuio;
1425     struct iovec iovec;
1426     int f_flags = 0;
1427
1428     buffer = kmap(pp) + offset;
1429     base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT)  + offset;
1430
1431     credp = crref();
1432     lock_kernel();
1433     AFS_GLOCK();
1434     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1435                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1436                ICL_TYPE_INT32, 99999);
1437
1438     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1439
1440     code = afs_write(vcp, &tuio, f_flags, credp, 0);
1441
1442     ip->i_size = vcp->m.Length;
1443     ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1444
1445     if (!code) {
1446         struct vrequest treq;
1447
1448         ObtainWriteLock(&vcp->lock, 533);
1449         if (!afs_InitReq(&treq, credp))
1450             code = afs_DoPartialWrite(vcp, &treq);
1451         ReleaseWriteLock(&vcp->lock);
1452     }
1453     code = code ? -code : count - tuio.uio_resid;
1454
1455     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1456                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1457                ICL_TYPE_INT32, code);
1458
1459     AFS_GUNLOCK();
1460     unlock_kernel();
1461     crfree(credp);
1462     kunmap(pp);
1463
1464     return code;
1465 }
1466
1467
1468 static int
1469 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1470 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1471 #else
1472 afs_linux_writepage(struct page *pp)
1473 #endif
1474 {
1475     struct address_space *mapping = pp->mapping;
1476     struct inode *inode;
1477     unsigned long end_index;
1478     unsigned offset = PAGE_CACHE_SIZE;
1479     long status;
1480
1481 #if defined(AFS_LINUX26_ENV)
1482     if (PageReclaim(pp)) {
1483 # if defined(WRITEPAGE_ACTIVATE)
1484         return WRITEPAGE_ACTIVATE;
1485 # else 
1486         return AOP_WRITEPAGE_ACTIVATE;
1487 # endif
1488     }
1489 #else
1490     if (PageLaunder(pp)) {
1491         return(fail_writepage(pp));
1492     }
1493 #endif
1494
1495     inode = (struct inode *)mapping->host;
1496     end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1497
1498     /* easy case */
1499     if (pp->index < end_index)
1500         goto do_it;
1501     /* things got complicated... */
1502     offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1503     /* OK, are we completely out? */
1504     if (pp->index >= end_index + 1 || !offset)
1505         return -EIO;
1506   do_it:
1507     status = afs_linux_writepage_sync(inode, pp, 0, offset);
1508     SetPageUptodate(pp);
1509     UnlockPage(pp);
1510     if (status == offset)
1511         return 0;
1512     else
1513         return status;
1514 }
1515 #else
1516 /* afs_linux_updatepage
1517  * What one would have thought was writepage - write dirty page to file.
1518  * Called from generic_file_write. buffer is still in user space. pagep
1519  * has been filled in with old data if we're updating less than a page.
1520  */
1521 static int
1522 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1523                      unsigned int count, int sync)
1524 {
1525     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1526     u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1527     int code = 0;
1528     cred_t *credp;
1529     uio_t tuio;
1530     struct iovec iovec;
1531
1532     set_bit(PG_locked, &pp->flags);
1533
1534     credp = crref();
1535     AFS_GLOCK();
1536     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1537                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1538                ICL_TYPE_INT32, 99999);
1539     setup_uio(&tuio, &iovec, page_addr + offset,
1540               (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1541               AFS_UIOSYS);
1542
1543     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1544
1545     ip->i_size = vcp->m.Length;
1546     ip->i_blocks = ((vcp->m.Length + 1023) >> 10) << 1;
1547
1548     if (!code) {
1549         struct vrequest treq;
1550
1551         ObtainWriteLock(&vcp->lock, 533);
1552         vcp->m.Date = osi_Time();   /* set modification time */
1553         if (!afs_InitReq(&treq, credp))
1554             code = afs_DoPartialWrite(vcp, &treq);
1555         ReleaseWriteLock(&vcp->lock);
1556     }
1557
1558     code = code ? -code : count - tuio.uio_resid;
1559     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1560                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1561                ICL_TYPE_INT32, code);
1562
1563     AFS_GUNLOCK();
1564     crfree(credp);
1565
1566     clear_bit(PG_locked, &pp->flags);
1567     return code;
1568 }
1569 #endif
1570
1571 /* afs_linux_permission
1572  * Check access rights - returns error if can't check or permission denied.
1573  */
1574 static int
1575 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1576 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1577 #else
1578 afs_linux_permission(struct inode *ip, int mode)
1579 #endif
1580 {
1581     int code;
1582     cred_t *credp = crref();
1583     int tmp = 0;
1584
1585     AFS_GLOCK();
1586     if (mode & MAY_EXEC)
1587         tmp |= VEXEC;
1588     if (mode & MAY_READ)
1589         tmp |= VREAD;
1590     if (mode & MAY_WRITE)
1591         tmp |= VWRITE;
1592     code = afs_access(VTOAFS(ip), tmp, credp);
1593
1594     AFS_GUNLOCK();
1595     crfree(credp);
1596     return -code;
1597 }
1598
1599 #if defined(AFS_LINUX24_ENV)
1600 static int
1601 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1602                        unsigned to)
1603 {
1604     int code;
1605
1606     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1607                                     offset, to - offset);
1608 #if !defined(AFS_LINUX26_ENV)
1609     kunmap(page);
1610 #endif
1611
1612     return code;
1613 }
1614
1615 static int
1616 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1617                         unsigned to)
1618 {
1619 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1620    call kmap directly instead of relying on us to do it */
1621 #if !defined(AFS_LINUX26_ENV)
1622     kmap(page);
1623 #endif
1624     return 0;
1625 }
1626
1627 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1628 #endif
1629
1630 static struct inode_operations afs_file_iops = {
1631 #if defined(AFS_LINUX26_ENV)
1632   .permission =         afs_linux_permission,
1633   .getattr =            afs_linux_getattr,
1634   .setattr =            afs_notify_change,
1635 #elif defined(AFS_LINUX24_ENV)
1636   .permission =         afs_linux_permission,
1637   .revalidate =         afs_linux_revalidate,
1638   .setattr =            afs_notify_change,
1639 #else
1640   .default_file_ops =   &afs_file_fops,
1641   .readpage =           afs_linux_readpage,
1642   .revalidate =         afs_linux_revalidate,
1643   .updatepage =         afs_linux_updatepage,
1644 #endif
1645 };
1646
1647 #if defined(AFS_LINUX24_ENV)
1648 static struct address_space_operations afs_file_aops = {
1649   .readpage =           afs_linux_readpage,
1650   .writepage =          afs_linux_writepage,
1651   .commit_write =       afs_linux_commit_write,
1652   .prepare_write =      afs_linux_prepare_write,
1653 };
1654 #endif
1655
1656
1657 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1658  * by what sort of operation is allowed.....
1659  */
1660
1661 static struct inode_operations afs_dir_iops = {
1662 #if !defined(AFS_LINUX24_ENV)
1663   .default_file_ops =   &afs_dir_fops,
1664 #else
1665   .setattr =            afs_notify_change,
1666 #endif
1667   .create =             afs_linux_create,
1668   .lookup =             afs_linux_lookup,
1669   .link =               afs_linux_link,
1670   .unlink =             afs_linux_unlink,
1671   .symlink =            afs_linux_symlink,
1672   .mkdir =              afs_linux_mkdir,
1673   .rmdir =              afs_linux_rmdir,
1674   .rename =             afs_linux_rename,
1675 #if defined(AFS_LINUX26_ENV)
1676   .getattr =            afs_linux_getattr,
1677 #else
1678   .revalidate =         afs_linux_revalidate,
1679 #endif
1680   .permission =         afs_linux_permission,
1681 };
1682
1683 /* We really need a separate symlink set of ops, since do_follow_link()
1684  * determines if it _is_ a link by checking if the follow_link op is set.
1685  */
1686 #if defined(AFS_LINUX24_ENV)
1687 static int
1688 afs_symlink_filler(struct file *file, struct page *page)
1689 {
1690     struct inode *ip = (struct inode *)page->mapping->host;
1691     char *p = (char *)kmap(page);
1692     int code;
1693
1694     lock_kernel();
1695     AFS_GLOCK();
1696     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1697     AFS_GUNLOCK();
1698
1699     if (code < 0)
1700         goto fail;
1701     p[code] = '\0';             /* null terminate? */
1702     unlock_kernel();
1703
1704     SetPageUptodate(page);
1705     kunmap(page);
1706     UnlockPage(page);
1707     return 0;
1708
1709   fail:
1710     unlock_kernel();
1711
1712     SetPageError(page);
1713     kunmap(page);
1714     UnlockPage(page);
1715     return code;
1716 }
1717
1718 static struct address_space_operations afs_symlink_aops = {
1719   .readpage =   afs_symlink_filler
1720 };
1721 #endif
1722
1723 static struct inode_operations afs_symlink_iops = {
1724 #if defined(AFS_LINUX24_ENV)
1725   .readlink =           page_readlink,
1726 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1727   .follow_link =        page_follow_link,
1728 #else
1729   .follow_link =        page_follow_link_light,
1730   .put_link =           page_put_link,
1731 #endif
1732   .setattr =            afs_notify_change,
1733 #else
1734   .readlink =           afs_linux_readlink,
1735   .follow_link =        afs_linux_follow_link,
1736   .permission =         afs_linux_permission,
1737   .revalidate =         afs_linux_revalidate,
1738 #endif
1739 };
1740
1741 void
1742 afs_fill_inode(struct inode *ip, struct vattr *vattr)
1743 {
1744         
1745     if (vattr)
1746         vattr2inode(ip, vattr);
1747
1748 /* Reset ops if symlink or directory. */
1749     if (S_ISREG(ip->i_mode)) {
1750         ip->i_op = &afs_file_iops;
1751 #if defined(AFS_LINUX24_ENV)
1752         ip->i_fop = &afs_file_fops;
1753         ip->i_data.a_ops = &afs_file_aops;
1754 #endif
1755
1756     } else if (S_ISDIR(ip->i_mode)) {
1757         ip->i_op = &afs_dir_iops;
1758 #if defined(AFS_LINUX24_ENV)
1759         ip->i_fop = &afs_dir_fops;
1760 #endif
1761
1762     } else if (S_ISLNK(ip->i_mode)) {
1763         ip->i_op = &afs_symlink_iops;
1764 #if defined(AFS_LINUX24_ENV)
1765         ip->i_data.a_ops = &afs_symlink_aops;
1766         ip->i_mapping = &ip->i_data;
1767 #endif
1768     }
1769
1770     /* insert_inode_hash(ip);   -- this would make iget() work (if we used it) */
1771 }