afs: Zero uninitialized uio structs
[openafs.git] / src / afs / LINUX24 / 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
25 #include "afs/sysincludes.h"
26 #include "afsincludes.h"
27 #include "afs/afs_stats.h"
28 #include "h/mm.h"
29 #ifdef HAVE_MM_INLINE_H
30 #include "h/mm_inline.h"
31 #endif
32 #include "h/pagemap.h"
33 #if defined(AFS_LINUX24_ENV)
34 #include "h/smp_lock.h"
35 #endif
36 #include "afs/lock.h"
37 #include "afs/afs_bypasscache.h"
38
39 #ifdef pgoff2loff
40 #define pageoff(pp) pgoff2loff((pp)->index)
41 #else
42 #define pageoff(pp) pp->offset
43 #endif
44
45 #ifndef MAX_ERRNO
46 #define MAX_ERRNO 1000L
47 #endif
48
49 extern struct vcache *afs_globalVp;
50 #if defined(AFS_LINUX24_ENV)
51 /* Some uses of BKL are perhaps not needed for bypass or memcache--
52  * why don't we try it out? */
53 extern struct afs_cacheOps afs_UfsCacheOps;
54 #define maybe_lock_kernel()                     \
55     do {                                               \
56         if(afs_cacheType == &afs_UfsCacheOps)          \
57             lock_kernel();                             \
58     } while(0);
59
60
61 #define maybe_unlock_kernel()                   \
62     do {                                               \
63         if(afs_cacheType == &afs_UfsCacheOps)          \
64             unlock_kernel();                           \
65     } while(0);
66 #endif /* AFS_LINUX24_ENV */
67
68
69 /* This function converts a positive error code from AFS into a negative
70  * code suitable for passing into the Linux VFS layer. It checks that the
71  * error code is within the permissable bounds for the ERR_PTR mechanism.
72  *
73  * _All_ error codes which come from the AFS layer should be passed through
74  * this function before being returned to the kernel.
75  */
76
77 static inline int afs_convert_code(int code) {
78     if ((code >= 0) && (code <= MAX_ERRNO))
79         return -code;
80     else
81         return -EIO;
82 }
83
84 /* Linux doesn't require a credp for many functions, and crref is an expensive
85  * operation. This helper function avoids obtaining it for VerifyVCache calls
86  */
87
88 static inline int afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
89     cred_t *credp = NULL;
90     struct vrequest treq;
91     int code;
92
93     if (avc->f.states & CStatd) {
94         if (retcred)
95             *retcred = NULL;
96         return 0;
97     }
98
99     credp = crref();
100
101     code = afs_InitReq(&treq, credp);
102     if (code == 0)
103         code = afs_VerifyVCache2(avc, &treq);
104
105     if (retcred != NULL)
106         *retcred = credp;
107     else
108         crfree(credp);
109
110     return afs_convert_code(code);
111 }
112
113 static ssize_t
114 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
115 {
116     ssize_t code = 0;
117     struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
118 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
119     afs_size_t isize, offindex;
120 #endif
121
122     AFS_GLOCK();
123     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
124                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
125                99999);
126     code = afs_linux_VerifyVCache(vcp, NULL);
127
128     if (code == 0) {
129 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
130         isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
131         offindex = *offp >> PAGE_CACHE_SHIFT;
132         if(offindex > isize) {
133             code=0;
134             goto done;
135         }
136 #endif
137         /* Linux's FlushPages implementation doesn't ever use credp,
138          * so we optimise by not using it */
139         osi_FlushPages(vcp, NULL);      /* ensure stale pages are gone */
140         AFS_GUNLOCK();
141 #ifdef HAVE_LINUX_DO_SYNC_READ
142         code = do_sync_read(fp, buf, count, offp);
143 #else
144         code = generic_file_read(fp, buf, count, offp);
145 #endif
146         AFS_GLOCK();
147     }
148
149     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
150                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
151                code);
152 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
153 done:
154 #endif
155     AFS_GUNLOCK();
156     return code;
157 }
158
159
160 /* Now we have integrated VM for writes as well as reads. generic_file_write
161  * also takes care of re-positioning the pointer if file is open in append
162  * mode. Call fake open/close to ensure we do writes of core dumps.
163  */
164 static ssize_t
165 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
166 {
167     ssize_t code = 0;
168     struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
169     cred_t *credp;
170
171     AFS_GLOCK();
172
173     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
174                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
175                (fp->f_flags & O_APPEND) ? 99998 : 99999);
176
177     code = afs_linux_VerifyVCache(vcp, &credp);
178
179     ObtainWriteLock(&vcp->lock, 529);
180     afs_FakeOpen(vcp);
181     ReleaseWriteLock(&vcp->lock);
182     if (code == 0) {
183             AFS_GUNLOCK();
184 #ifdef DO_SYNC_READ
185             code = do_sync_write(fp, buf, count, offp);
186 #else
187             code = generic_file_write(fp, buf, count, offp);
188 #endif
189             AFS_GLOCK();
190     }
191
192     ObtainWriteLock(&vcp->lock, 530);
193
194     if (vcp->execsOrWriters == 1 && !credp)
195       credp = crref();
196
197     afs_FakeClose(vcp, credp);
198     ReleaseWriteLock(&vcp->lock);
199
200     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
201                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
202                code);
203
204     if (credp)
205       crfree(credp);
206     AFS_GUNLOCK();
207     return code;
208 }
209
210 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
211
212 /* This is a complete rewrite of afs_readdir, since we can make use of
213  * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
214  * handling and use of bulkstats will need to be reflected here as well.
215  */
216 static int
217 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
218 {
219     struct vcache *avc = VTOAFS(FILE_INODE(fp));
220     struct vrequest treq;
221     struct dcache *tdc;
222     int code;
223     int offset;
224     int dirpos;
225     struct DirEntry *de;
226     struct DirBuffer entry;
227     ino_t ino;
228     int len;
229     afs_size_t origOffset, tlen;
230     cred_t *credp = crref();
231     struct afs_fakestat_state fakestat;
232
233     AFS_GLOCK();
234     AFS_STATCNT(afs_readdir);
235
236     code = afs_convert_code(afs_InitReq(&treq, credp));
237     crfree(credp);
238     if (code)
239         goto out1;
240
241     afs_InitFakeStat(&fakestat);
242     code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, &treq));
243     if (code)
244         goto out;
245
246     /* update the cache entry */
247   tagain:
248     code = afs_convert_code(afs_VerifyVCache2(avc, &treq));
249     if (code)
250         goto out;
251
252     /* get a reference to the entire directory */
253     tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
254     len = tlen;
255     if (!tdc) {
256         code = -ENOENT;
257         goto out;
258     }
259     ObtainSharedLock(&avc->lock, 810);
260     UpgradeSToWLock(&avc->lock, 811);
261     ObtainReadLock(&tdc->lock);
262     /*
263      * Make sure that the data in the cache is current. There are two
264      * cases we need to worry about:
265      * 1. The cache data is being fetched by another process.
266      * 2. The cache data is no longer valid
267      */
268     while ((avc->f.states & CStatd)
269            && (tdc->dflags & DFFetching)
270            && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
271         ReleaseReadLock(&tdc->lock);
272         ReleaseSharedLock(&avc->lock);
273         afs_osi_Sleep(&tdc->validPos);
274         ObtainSharedLock(&avc->lock, 812);
275         ObtainReadLock(&tdc->lock);
276     }
277     if (!(avc->f.states & CStatd)
278         || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
279         ReleaseReadLock(&tdc->lock);
280         ReleaseSharedLock(&avc->lock);
281         afs_PutDCache(tdc);
282         goto tagain;
283     }
284
285     /* Set the readdir-in-progress flag, and downgrade the lock
286      * to shared so others will be able to acquire a read lock.
287      */
288     avc->f.states |= CReadDir;
289     avc->dcreaddir = tdc;
290     avc->readdir_pid = MyPidxx2Pid(MyPidxx);
291     ConvertWToSLock(&avc->lock);
292
293     /* Fill in until we get an error or we're done. This implementation
294      * takes an offset in units of blobs, rather than bytes.
295      */
296     code = 0;
297     offset = (int) fp->f_pos;
298     while (1) {
299         dirpos = BlobScan(tdc, offset);
300         if (!dirpos)
301             break;
302
303         code = afs_dir_GetBlob(tdc, dirpos, &entry);
304         if (code)
305             break;
306         de = (struct DirEntry *)entry.data;
307
308         ino = afs_calc_inum(avc->f.fid.Cell, avc->f.fid.Fid.Volume,
309                             ntohl(de->fid.vnode));
310
311         if (de->name)
312             len = strlen(de->name);
313         else {
314             printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n", 
315                    (unsigned long)&tdc->f.inode, dirpos);
316             DRelease(&entry, 0);
317             ReleaseSharedLock(&avc->lock);
318             afs_PutDCache(tdc);
319             code = -ENOENT;
320             goto out;
321         }
322
323         /* filldir returns -EINVAL when the buffer is full. */
324 #if (defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE)
325         {
326             unsigned int type = DT_UNKNOWN;
327             struct VenusFid afid;
328             struct vcache *tvc;
329             int vtype;
330             afid.Cell = avc->f.fid.Cell;
331             afid.Fid.Volume = avc->f.fid.Fid.Volume;
332             afid.Fid.Vnode = ntohl(de->fid.vnode);
333             afid.Fid.Unique = ntohl(de->fid.vunique);
334             if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
335                 type = DT_DIR;
336             } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
337                 if (tvc->mvstat) {
338                     type = DT_DIR;
339                 } else if (((tvc->f.states) & (CStatd | CTruth))) {
340                     /* CTruth will be set if the object has
341                      *ever* been statd */
342                     vtype = vType(tvc);
343                     if (vtype == VDIR)
344                         type = DT_DIR;
345                     else if (vtype == VREG)
346                         type = DT_REG;
347                     /* Don't do this until we're sure it can't be a mtpt */
348                     /* else if (vtype == VLNK)
349                      * type=DT_LNK; */
350                     /* what other types does AFS support? */
351                 }
352                 /* clean up from afs_FindVCache */
353                 afs_PutVCache(tvc);
354             }
355             /* 
356              * If this is NFS readdirplus, then the filler is going to
357              * call getattr on this inode, which will deadlock if we're
358              * holding the GLOCK.
359              */
360             AFS_GUNLOCK();
361             code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
362             AFS_GLOCK();
363         }
364 #else
365         code = (*filldir) (dirbuf, de->name, len, offset, ino);
366 #endif
367         DRelease(&entry, 0);
368         if (code)
369             break;
370         offset = dirpos + 1 + ((len + 16) >> 5);
371     }
372     /* If filldir didn't fill in the last one this is still pointing to that
373      * last attempt.
374      */
375     fp->f_pos = (loff_t) offset;
376
377     ReleaseReadLock(&tdc->lock);
378     afs_PutDCache(tdc);
379     UpgradeSToWLock(&avc->lock, 813);
380     avc->f.states &= ~CReadDir;
381     avc->dcreaddir = 0;
382     avc->readdir_pid = 0;
383     ReleaseSharedLock(&avc->lock);
384     code = 0;
385
386 out:
387     afs_PutFakeStat(&fakestat);
388 out1:
389     AFS_GUNLOCK();
390     return code;
391 }
392
393
394 /* in afs_pioctl.c */
395 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
396                       unsigned long arg);
397
398 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
399 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
400                                unsigned long arg) {
401     return afs_xioctl(FILE_INODE(fp), fp, com, arg);
402
403 }
404 #endif
405
406
407 static int
408 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
409 {
410     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
411     int code;
412
413     AFS_GLOCK();
414 #if defined(AFS_LINUX24_ENV)
415     afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
416                ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
417                vmap->vm_end - vmap->vm_start);
418 #else
419     afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
420                ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
421                vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
422                vmap->vm_offset);
423 #endif
424
425     /* get a validated vcache entry */
426     code = afs_linux_VerifyVCache(vcp, NULL);
427
428     /* Linux's Flushpage implementation doesn't use credp, so optimise
429      * our code to not need to crref() it */
430     osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
431     AFS_GUNLOCK();
432     code = generic_file_mmap(fp, vmap);
433     AFS_GLOCK();
434     if (!code)
435         vcp->f.states |= CMAPPED;
436
437     AFS_GUNLOCK();
438     return code;
439 }
440
441 static int
442 afs_linux_open(struct inode *ip, struct file *fp)
443 {
444     struct vcache *vcp = VTOAFS(ip);
445     cred_t *credp = crref();
446     int code;
447
448 #ifdef AFS_LINUX24_ENV
449     maybe_lock_kernel();
450 #endif
451     AFS_GLOCK();
452     code = afs_open(&vcp, fp->f_flags, credp);
453     AFS_GUNLOCK();
454 #ifdef AFS_LINUX24_ENV
455     maybe_unlock_kernel();
456 #endif
457
458     crfree(credp);
459     return afs_convert_code(code);
460 }
461
462 static int
463 afs_linux_release(struct inode *ip, struct file *fp)
464 {
465     struct vcache *vcp = VTOAFS(ip);
466     cred_t *credp = crref();
467     int code = 0;
468
469 #ifdef AFS_LINUX24_ENV
470     maybe_lock_kernel();
471 #endif
472     AFS_GLOCK();
473     code = afs_close(vcp, fp->f_flags, credp);
474     AFS_GUNLOCK();
475 #ifdef AFS_LINUX24_ENV
476     maybe_unlock_kernel();
477 #endif
478
479     crfree(credp);
480     return afs_convert_code(code);
481 }
482
483 static int
484 #if defined(AFS_LINUX24_ENV)
485 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
486 #else
487 afs_linux_fsync(struct file *fp, struct dentry *dp)
488 #endif
489 {
490     int code;
491     struct inode *ip = FILE_INODE(fp);
492     cred_t *credp = crref();
493
494 #ifdef AFS_LINUX24_ENV
495     maybe_lock_kernel();
496 #endif
497     AFS_GLOCK();
498     code = afs_fsync(VTOAFS(ip), credp);
499     AFS_GUNLOCK();
500 #ifdef AFS_LINUX24_ENV
501     maybe_unlock_kernel();
502 #endif
503     crfree(credp);
504     return afs_convert_code(code);
505
506 }
507
508
509 static int
510 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
511 {
512     int code = 0;
513     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
514     cred_t *credp = crref();
515     struct AFS_FLOCK flock;
516 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
517     struct file_lock conflict;
518 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
519     struct file_lock *conflict;
520 #endif
521     
522     /* Convert to a lock format afs_lockctl understands. */
523     memset(&flock, 0, sizeof(flock));
524     flock.l_type = flp->fl_type;
525     flock.l_pid = flp->fl_pid;
526     flock.l_whence = 0;
527     flock.l_start = flp->fl_start;
528     if (flp->fl_end == OFFSET_MAX)
529         flock.l_len = 0; /* Lock to end of file */
530     else
531         flock.l_len = flp->fl_end - flp->fl_start + 1;
532
533     /* Safe because there are no large files, yet */
534 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
535     if (cmd == F_GETLK64)
536         cmd = F_GETLK;
537     else if (cmd == F_SETLK64)
538         cmd = F_SETLK;
539     else if (cmd == F_SETLKW64)
540         cmd = F_SETLKW;
541 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
542
543     AFS_GLOCK();
544     code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
545     AFS_GUNLOCK();
546
547 #ifdef AFS_LINUX24_ENV
548     if ((code == 0 || flp->fl_type == F_UNLCK) && 
549         (cmd == F_SETLK || cmd == F_SETLKW)) {
550 # ifdef POSIX_LOCK_FILE_WAIT_ARG
551         code = posix_lock_file(fp, flp, 0);
552 # else
553         flp->fl_flags &=~ FL_SLEEP;
554         code = posix_lock_file(fp, flp);
555 # endif 
556         if (code && flp->fl_type != F_UNLCK) {
557             struct AFS_FLOCK flock2;
558             flock2 = flock;
559             flock2.l_type = F_UNLCK;
560             AFS_GLOCK();
561             afs_lockctl(vcp, &flock2, F_SETLK, credp);
562             AFS_GUNLOCK();
563         }
564     }
565     /* If lockctl says there are no conflicting locks, then also check with the
566      * kernel, as lockctl knows nothing about byte range locks
567      */
568     if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
569 # if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
570         if (posix_test_lock(fp, flp, &conflict)) {
571             locks_copy_lock(flp, &conflict);
572             flp->fl_type = F_UNLCK;
573             crfree(credp);
574             return 0;
575         }
576 # elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
577         if ((conflict = posix_test_lock(fp, flp))) {
578             locks_copy_lock(flp, conflict);
579             flp->fl_type = F_UNLCK;
580             crfree(credp);
581             return 0;
582         }
583 # else
584         posix_test_lock(fp, flp);
585         /* If we found a lock in the kernel's structure, return it */
586         if (flp->fl_type != F_UNLCK) {
587             crfree(credp);
588             return 0;
589         }
590 # endif
591     }
592     
593 #endif
594     /* Convert flock back to Linux's file_lock */
595     flp->fl_type = flock.l_type;
596     flp->fl_pid = flock.l_pid;
597     flp->fl_start = flock.l_start;
598     if (flock.l_len == 0)
599         flp->fl_end = OFFSET_MAX; /* Lock to end of file */
600     else
601         flp->fl_end = flock.l_start + flock.l_len - 1;
602
603     crfree(credp);
604     return code;
605 }
606
607 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
608 static int
609 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
610     int code = 0;
611     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
612     cred_t *credp = crref();
613     struct AFS_FLOCK flock;
614     /* Convert to a lock format afs_lockctl understands. */
615     memset(&flock, 0, sizeof(flock));
616     flock.l_type = flp->fl_type;
617     flock.l_pid = flp->fl_pid;
618     flock.l_whence = 0;
619     flock.l_start = 0;
620     flock.l_len = 0;
621
622     /* Safe because there are no large files, yet */
623 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
624     if (cmd == F_GETLK64)
625         cmd = F_GETLK;
626     else if (cmd == F_SETLK64)
627         cmd = F_SETLK;
628     else if (cmd == F_SETLKW64)
629         cmd = F_SETLKW;
630 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
631
632     AFS_GLOCK();
633     code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
634     AFS_GUNLOCK();
635
636     if ((code == 0 || flp->fl_type == F_UNLCK) && 
637         (cmd == F_SETLK || cmd == F_SETLKW)) {
638         flp->fl_flags &=~ FL_SLEEP;
639         code = flock_lock_file_wait(fp, flp);
640         if (code && flp->fl_type != F_UNLCK) {
641             struct AFS_FLOCK flock2;
642             flock2 = flock;
643             flock2.l_type = F_UNLCK;
644             AFS_GLOCK();
645             afs_lockctl(vcp, &flock2, F_SETLK, credp);
646             AFS_GUNLOCK();
647         }
648     }
649     /* Convert flock back to Linux's file_lock */
650     flp->fl_type = flock.l_type;
651     flp->fl_pid = flock.l_pid;
652
653     crfree(credp);
654     return code;
655 }
656 #endif
657
658 /* afs_linux_flush
659  * essentially the same as afs_fsync() but we need to get the return
660  * code for the sys_close() here, not afs_linux_release(), so call
661  * afs_StoreAllSegments() with AFS_LASTSTORE
662  */
663 static int
664 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
665 afs_linux_flush(struct file *fp, fl_owner_t id)
666 #else
667 afs_linux_flush(struct file *fp)
668 #endif
669 {
670     struct vrequest treq;
671     struct vcache *vcp;
672     cred_t *credp;
673     int code;
674     int bypasscache;
675
676     AFS_GLOCK();
677
678     if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
679         AFS_GUNLOCK();
680         return 0;
681     }
682
683     AFS_DISCON_LOCK();
684
685     credp = crref();
686     vcp = VTOAFS(FILE_INODE(fp));
687
688     code = afs_InitReq(&treq, credp);
689     if (code)
690         goto out;
691         /* If caching is bypassed for this file, or globally, just return 0 */
692         if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
693                 bypasscache = 1;
694         else {
695                 ObtainReadLock(&vcp->lock);
696                 if(vcp->cachingStates & FCSBypass)
697                         bypasscache = 1;
698                 ReleaseReadLock(&vcp->lock);
699         }
700         if(bypasscache) {
701             /* future proof: don't rely on 0 return from afs_InitReq */
702             code = 0; goto out;
703         }
704
705     ObtainSharedLock(&vcp->lock, 535);
706     if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
707         UpgradeSToWLock(&vcp->lock, 536);
708         if (!AFS_IS_DISCONNECTED) {
709                 code = afs_StoreAllSegments(vcp,
710                                 &treq,
711                                 AFS_SYNC | AFS_LASTSTORE);
712         } else {
713                 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
714         }
715         ConvertWToSLock(&vcp->lock);
716     }
717     code = afs_CheckCode(code, &treq, 54);
718     ReleaseSharedLock(&vcp->lock);
719
720 out:
721     AFS_DISCON_UNLOCK();
722     AFS_GUNLOCK();
723
724     crfree(credp);
725     return afs_convert_code(code);
726 }
727
728 #if !defined(AFS_LINUX24_ENV)
729 /* Not allowed to directly read a directory. */
730 ssize_t
731 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
732 {
733     return -EISDIR;
734 }
735 #endif
736
737
738
739 struct file_operations afs_dir_fops = {
740 #if !defined(AFS_LINUX24_ENV)
741   .read =       afs_linux_dir_read,
742   .lock =       afs_linux_lock,
743   .fsync =      afs_linux_fsync,
744 #else
745   .read =       generic_read_dir,
746 #endif
747   .readdir =    afs_linux_readdir,
748 #ifdef HAVE_UNLOCKED_IOCTL
749   .unlocked_ioctl = afs_unlocked_xioctl,
750 #else
751   .ioctl =      afs_xioctl,
752 #endif
753 #ifdef HAVE_COMPAT_IOCTL
754   .compat_ioctl = afs_unlocked_xioctl,
755 #endif
756   .open =       afs_linux_open,
757   .release =    afs_linux_release,
758 };
759
760 struct file_operations afs_file_fops = {
761   .read =       afs_linux_read,
762   .write =      afs_linux_write,
763 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
764   .aio_read =   generic_file_aio_read,
765   .aio_write =  generic_file_aio_write,
766 #endif
767 #ifdef HAVE_UNLOCKED_IOCTL
768   .unlocked_ioctl = afs_unlocked_xioctl,
769 #else
770   .ioctl =      afs_xioctl,
771 #endif
772 #ifdef HAVE_COMPAT_IOCTL
773   .compat_ioctl = afs_unlocked_xioctl,
774 #endif
775   .mmap =       afs_linux_mmap,
776   .open =       afs_linux_open,
777   .flush =      afs_linux_flush,
778   .release =    afs_linux_release,
779   .fsync =      afs_linux_fsync,
780   .lock =       afs_linux_lock,
781 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
782   .flock =      afs_linux_flock,
783 #endif
784 };
785
786 static struct dentry *
787 canonical_dentry(struct inode *ip)
788 {
789     struct vcache *vcp = VTOAFS(ip);
790     struct dentry *first = NULL, *ret = NULL, *cur;
791     struct list_head *head, *prev, *tmp;
792
793     /* general strategy:
794      * if vcp->target_link is set, and can be found in ip->i_dentry, use that.
795      * otherwise, use the first dentry in ip->i_dentry.
796      * if ip->i_dentry is empty, use the 'dentry' argument we were given.
797      */
798     /* note that vcp->target_link specifies which dentry to use, but we have
799      * no reference held on that dentry. so, we cannot use or dereference
800      * vcp->target_link itself, since it may have been freed. instead, we only
801      * use it to compare to pointers in the ip->i_dentry list. */
802
803     d_prune_aliases(ip);
804
805     spin_lock(&dcache_lock);
806
807     head = &ip->i_dentry;
808     prev = ip->i_dentry.prev;
809
810     while (prev != head) {
811         tmp = prev;
812         prev = tmp->prev;
813         cur = list_entry(tmp, struct dentry, d_alias);
814
815         if (!vcp->target_link || cur == vcp->target_link) {
816             ret = cur;
817             break;
818         }
819
820         if (!first) {
821             first = cur;
822         }
823     }
824     if (!ret && first) {
825         ret = first;
826     }
827
828     vcp->target_link = ret;
829
830     if (ret) {
831         dget_locked(ret);
832     }
833     spin_unlock(&dcache_lock);
834
835     return ret;
836 }
837
838 /**********************************************************************
839  * AFS Linux dentry operations
840  **********************************************************************/
841
842 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
843  * that has its mvid (parent dir's fid) pointer set to the wrong directory
844  * due to being mounted in multiple points at once. If so, check_bad_parent()
845  * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
846  * dotdotfid and mtpoint fid members.
847  * Parameters:
848  *   dp - dentry to be checked.
849  * Return Values:
850  *   None.
851  * Sideeffects:
852  *   This dentry's vcache's mvid will be set to the correct parent directory's
853  *   fid.
854  *   This root vnode's volume will have its dotdotfid and mtpoint fids set
855  *   to the correct parent and mountpoint fids.
856  */
857
858 static inline void
859 check_bad_parent(struct dentry *dp)
860 {
861     cred_t *credp;
862     struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
863     struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
864
865     if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) {       /* bad parent */
866         credp = crref();
867
868         /* force a lookup, so vcp->mvid is fixed up */
869         afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
870         if (!avc || vcp != avc) {       /* bad, very bad.. */
871             afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
872                        "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
873                        ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
874                        ICL_TYPE_POINTER, dp);
875         }
876         if (avc)
877             AFS_RELE(AFSTOV(avc));
878         crfree(credp);
879     }
880
881     return;
882 }
883
884 /* afs_linux_revalidate
885  * Ensure vcache is stat'd before use. Return 0 if entry is valid.
886  */
887 static int
888 afs_linux_revalidate(struct dentry *dp)
889 {
890     struct vattr vattr;
891     struct vcache *vcp = VTOAFS(dp->d_inode);
892     cred_t *credp;
893     int code;
894
895     if (afs_shuttingdown)
896         return EIO;
897
898 #ifdef AFS_LINUX24_ENV
899     maybe_lock_kernel();
900 #endif
901     AFS_GLOCK();
902
903 #ifdef notyet
904     /* Make this a fast path (no crref), since it's called so often. */
905     if (vcp->f.states & CStatd) {
906
907         if (*dp->d_name.name != '/' && vcp->mvstat == 2)        /* root vnode */
908             check_bad_parent(dp);       /* check and correct mvid */
909
910         AFS_GUNLOCK();
911 #ifdef AFS_LINUX24_ENV
912         unlock_kernel();
913 #endif
914         return 0;
915     }
916 #endif
917
918     /* This avoids the crref when we don't have to do it. Watch for
919      * changes in afs_getattr that don't get replicated here!
920      */
921     if (vcp->f.states & CStatd &&
922         (!afs_fakestat_enable || vcp->mvstat != 1) &&
923         !afs_nfsexporter &&
924         (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
925         code = afs_CopyOutAttrs(vcp, &vattr);
926     } else {
927         credp = crref();
928         code = afs_getattr(vcp, &vattr, credp);
929         crfree(credp);
930     }
931     if (!code)
932         afs_fill_inode(AFSTOV(vcp), &vattr);
933
934     AFS_GUNLOCK();
935 #ifdef AFS_LINUX24_ENV
936     maybe_unlock_kernel();
937 #endif
938
939     return afs_convert_code(code);
940 }
941
942 /* vattr_setattr
943  * Set iattr data into vattr. Assume vattr cleared before call.
944  */
945 static void
946 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
947 {
948     vattrp->va_mask = iattrp->ia_valid;
949     if (iattrp->ia_valid & ATTR_MODE)
950         vattrp->va_mode = iattrp->ia_mode;
951     if (iattrp->ia_valid & ATTR_UID)
952         vattrp->va_uid = iattrp->ia_uid;
953     if (iattrp->ia_valid & ATTR_GID)
954         vattrp->va_gid = iattrp->ia_gid;
955     if (iattrp->ia_valid & ATTR_SIZE)
956         vattrp->va_size = iattrp->ia_size;
957     if (iattrp->ia_valid & ATTR_ATIME) {
958         vattrp->va_atime.tv_sec = iattrp->ia_atime;
959         vattrp->va_atime.tv_usec = 0;
960     }
961     if (iattrp->ia_valid & ATTR_MTIME) {
962         vattrp->va_mtime.tv_sec = iattrp->ia_mtime;
963         vattrp->va_mtime.tv_usec = 0;
964     }
965     if (iattrp->ia_valid & ATTR_CTIME) {
966         vattrp->va_ctime.tv_sec = iattrp->ia_ctime;
967         vattrp->va_ctime.tv_usec = 0;
968     }
969 }
970
971 /* vattr2inode
972  * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
973  */
974 void
975 vattr2inode(struct inode *ip, struct vattr *vp)
976 {
977     ip->i_ino = vp->va_nodeid;
978     ip->i_nlink = vp->va_nlink;
979     ip->i_blocks = vp->va_blocks;
980 #ifdef STRUCT_INODE_HAS_I_BLKBITS
981     ip->i_blkbits = AFS_BLKBITS;
982 #endif
983 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
984     ip->i_blksize = vp->va_blocksize;
985 #endif
986     ip->i_rdev = vp->va_rdev;
987     ip->i_mode = vp->va_mode;
988     ip->i_uid = vp->va_uid;
989     ip->i_gid = vp->va_gid;
990     i_size_write(ip, vp->va_size);
991     ip->i_atime = vp->va_atime.tv_sec;
992     ip->i_mtime = vp->va_mtime.tv_sec;
993     ip->i_ctime = vp->va_ctime.tv_sec;
994 }
995
996 /* afs_notify_change
997  * Linux version of setattr call. What to change is in the iattr struct.
998  * We need to set bits in both the Linux inode as well as the vcache.
999  */
1000 int
1001 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
1002 {
1003     struct vattr vattr;
1004     cred_t *credp = crref();
1005     struct inode *ip = dp->d_inode;
1006     int code;
1007
1008     VATTR_NULL(&vattr);
1009     iattr2vattr(&vattr, iattrp);        /* Convert for AFS vnodeops call. */
1010
1011     AFS_GLOCK();
1012     code = afs_setattr(VTOAFS(ip), &vattr, credp);
1013     if (!code) {
1014         afs_getattr(VTOAFS(ip), &vattr, credp);
1015         vattr2inode(ip, &vattr);
1016     }
1017     AFS_GUNLOCK();
1018     crfree(credp);
1019     return afs_convert_code(code);
1020 }
1021
1022 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
1023  * In kernels 2.2.10 and above, we are passed an additional flags var which
1024  * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
1025  * we are advised to follow the entry if it is a link or to make sure that 
1026  * it is a directory. But since the kernel itself checks these possibilities
1027  * later on, we shouldn't have to do it until later. Perhaps in the future..
1028  */
1029 static int
1030 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1031 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
1032 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
1033 #else
1034 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
1035 #endif
1036 #else
1037 afs_linux_dentry_revalidate(struct dentry *dp)
1038 #endif
1039 {
1040     struct vattr vattr;
1041     cred_t *credp = NULL;
1042     struct vcache *vcp, *pvcp, *tvc = NULL;
1043     int valid;
1044     struct afs_fakestat_state fakestate;
1045
1046 #ifdef AFS_LINUX24_ENV
1047     maybe_lock_kernel();
1048 #endif
1049     AFS_GLOCK();
1050     afs_InitFakeStat(&fakestate);
1051
1052     if (dp->d_inode) {
1053
1054         vcp = VTOAFS(dp->d_inode);
1055         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
1056
1057         if (vcp == afs_globalVp)
1058             goto good_dentry;
1059
1060         if (vcp->mvstat == 1) {         /* mount point */
1061             if (vcp->mvid && (vcp->f.states & CMValid)) {
1062                 int tryEvalOnly = 0;
1063                 int code = 0;
1064                 struct vrequest treq;
1065
1066                 credp = crref();
1067                 code = afs_InitReq(&treq, credp);
1068                 if (
1069 #ifdef AFS_DARWIN_ENV
1070                     (strcmp(dp->d_name.name, ".DS_Store") == 0) ||
1071                     (strcmp(dp->d_name.name, "Contents") == 0) ||
1072 #endif
1073                     (strcmp(dp->d_name.name, ".directory") == 0)) {
1074                     tryEvalOnly = 1;
1075                 }
1076                 if (tryEvalOnly)
1077                     code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
1078                 else
1079                     code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
1080                 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
1081                     /* a mount point, not yet replaced by its directory */
1082                     goto bad_dentry;
1083                 }
1084             }
1085         } else
1086             if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
1087                 check_bad_parent(dp);   /* check and correct mvid */
1088
1089 #ifdef notdef
1090         /* If the last looker changes, we should make sure the current
1091          * looker still has permission to examine this file.  This would
1092          * always require a crref() which would be "slow".
1093          */
1094         if (vcp->last_looker != treq.uid) {
1095             if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
1096                 goto bad_dentry;
1097
1098             vcp->last_looker = treq.uid;
1099         }
1100 #endif
1101
1102         /* If the parent's DataVersion has changed or the vnode
1103          * is longer valid, we need to do a full lookup.  VerifyVCache
1104          * isn't enough since the vnode may have been renamed.
1105          */
1106
1107         if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
1108
1109             credp = crref();
1110             afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1111             if (!tvc || tvc != vcp)
1112                 goto bad_dentry;
1113
1114             if (afs_getattr(vcp, &vattr, credp))
1115                 goto bad_dentry;
1116
1117             vattr2inode(AFSTOV(vcp), &vattr);
1118             dp->d_time = hgetlo(pvcp->f.m.DataVersion);
1119         }
1120
1121         /* should we always update the attributes at this point? */
1122         /* unlikely--the vcache entry hasn't changed */
1123
1124     } else {
1125 #ifdef notyet
1126         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
1127         if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1128             goto bad_dentry;
1129 #endif
1130
1131         /* No change in parent's DataVersion so this negative
1132          * lookup is still valid.  BUT, if a server is down a
1133          * negative lookup can result so there should be a
1134          * liftime as well.  For now, always expire.
1135          */
1136
1137         goto bad_dentry;
1138     }
1139
1140   good_dentry:
1141     valid = 1;
1142
1143   done:
1144     /* Clean up */
1145     if (tvc)
1146         afs_PutVCache(tvc);
1147     afs_PutFakeStat(&fakestate);
1148     AFS_GUNLOCK();
1149     if (credp)
1150         crfree(credp);
1151
1152     if (!valid) {
1153         shrink_dcache_parent(dp);
1154         d_drop(dp);
1155     }
1156 #ifdef AFS_LINUX24_ENV
1157     maybe_unlock_kernel();
1158 #endif
1159     return valid;
1160
1161   bad_dentry:
1162     if (have_submounts(dp))
1163         valid = 1;
1164     else 
1165         valid = 0;
1166     goto done;
1167 }
1168
1169 static void
1170 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1171 {
1172     struct vcache *vcp = VTOAFS(ip);
1173
1174     AFS_GLOCK();
1175     if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1176         (void) afs_InactiveVCache(vcp, NULL);
1177     }
1178     AFS_GUNLOCK();
1179 #ifdef DCACHE_NFSFS_RENAMED
1180     dp->d_flags &= ~DCACHE_NFSFS_RENAMED;   
1181 #endif
1182
1183     iput(ip);
1184 }
1185
1186 static int
1187 afs_dentry_delete(struct dentry *dp)
1188 {
1189     if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1190         return 1;               /* bad inode? */
1191
1192     return 0;
1193 }
1194
1195 struct dentry_operations afs_dentry_operations = {
1196   .d_revalidate =       afs_linux_dentry_revalidate,
1197   .d_delete =           afs_dentry_delete,
1198   .d_iput =             afs_dentry_iput,
1199 };
1200
1201 /**********************************************************************
1202  * AFS Linux inode operations
1203  **********************************************************************/
1204
1205 /* afs_linux_create
1206  *
1207  * Merely need to set enough of vattr to get us through the create. Note
1208  * that the higher level code (open_namei) will take care of any tuncation
1209  * explicitly. Exclusive open is also taken care of in open_namei.
1210  *
1211  * name is in kernel space at this point.
1212  */
1213 static int
1214 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1215 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1216                  struct nameidata *nd)
1217 #else
1218 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1219 #endif
1220 {
1221     struct vattr vattr;
1222     cred_t *credp = crref();
1223     const char *name = dp->d_name.name;
1224     struct vcache *vcp;
1225     int code;
1226
1227     VATTR_NULL(&vattr);
1228     vattr.va_mode = mode;
1229     vattr.va_type = mode & S_IFMT;
1230
1231     AFS_GLOCK();
1232     code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1233                       &vcp, credp);
1234
1235     if (!code) {
1236         struct inode *ip = AFSTOV(vcp);
1237
1238         afs_getattr(vcp, &vattr, credp);
1239         afs_fill_inode(ip, &vattr);
1240         insert_inode_hash(ip);
1241         dp->d_op = &afs_dentry_operations;
1242         dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1243         d_instantiate(dp, ip);
1244     }
1245     AFS_GUNLOCK();
1246
1247     crfree(credp);
1248     return afs_convert_code(code);
1249 }
1250
1251 /* afs_linux_lookup */
1252 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1253 static struct dentry *
1254 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1255 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1256                  struct nameidata *nd)
1257 #else
1258 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1259 #endif
1260 #else
1261 static int
1262 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1263 #endif
1264 {
1265     cred_t *credp = crref();
1266     struct vcache *vcp = NULL;
1267     const char *comp = dp->d_name.name;
1268     struct inode *ip = NULL;
1269     int code;
1270
1271     AFS_GLOCK();
1272     code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1273     
1274     if (vcp) {
1275         struct vattr vattr;
1276         struct vcache *parent_vc = VTOAFS(dip);
1277
1278         if (parent_vc == vcp) {
1279             /* This is possible if the parent dir is a mountpoint to a volume,
1280              * and the dir entry we looked up is a mountpoint to the same
1281              * volume. Linux cannot cope with this, so return an error instead
1282              * of risking a deadlock or panic. */
1283             afs_PutVCache(vcp);
1284             code = EDEADLK;
1285             AFS_GUNLOCK();
1286             goto done;
1287         }
1288
1289         ip = AFSTOV(vcp);
1290         afs_getattr(vcp, &vattr, credp);
1291         afs_fill_inode(ip, &vattr);
1292         if (
1293 #ifdef HAVE_LINUX_HLIST_UNHASHED
1294             hlist_unhashed(&ip->i_hash)
1295 #else
1296             ip->i_hash.prev == NULL
1297 #endif
1298             )
1299             insert_inode_hash(ip);
1300     }
1301     dp->d_op = &afs_dentry_operations;
1302     dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1303     AFS_GUNLOCK();
1304
1305 #if defined(AFS_LINUX24_ENV)
1306     if (ip && S_ISDIR(ip->i_mode)) {
1307         d_prune_aliases(ip);
1308     }
1309 #endif
1310     d_add(dp, ip);
1311
1312  done:
1313     crfree(credp);
1314
1315     /* It's ok for the file to not be found. That's noted by the caller by
1316      * seeing that the dp->d_inode field is NULL.
1317      */
1318 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1319     if (code == ENOENT)
1320         return ERR_PTR(0);
1321     else 
1322         return ERR_PTR(afs_convert_code(code));
1323 #else
1324     if (code == ENOENT)
1325         code = 0;
1326     return afs_convert_code(code);
1327 #endif
1328 }
1329
1330 static int
1331 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1332 {
1333     int code;
1334     cred_t *credp = crref();
1335     const char *name = newdp->d_name.name;
1336     struct inode *oldip = olddp->d_inode;
1337
1338     /* If afs_link returned the vnode, we could instantiate the
1339      * dentry. Since it's not, we drop this one and do a new lookup.
1340      */
1341     d_drop(newdp);
1342
1343     AFS_GLOCK();
1344     code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1345
1346     AFS_GUNLOCK();
1347     crfree(credp);
1348     return afs_convert_code(code);
1349 }
1350
1351 static int
1352 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1353 {
1354     int code = EBUSY;
1355     cred_t *credp = crref();
1356     const char *name = dp->d_name.name;
1357     struct vcache *tvc = VTOAFS(dp->d_inode);
1358
1359     if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1360                                 && !(tvc->f.states & CUnlinked)) {
1361         struct dentry *__dp;
1362         char *__name;
1363
1364         __dp = NULL;
1365         __name = NULL;
1366         do {
1367             dput(__dp);
1368
1369             AFS_GLOCK();
1370             if (__name)
1371                 osi_FreeSmallSpace(__name);
1372             __name = afs_newname();
1373             AFS_GUNLOCK();
1374
1375             __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1376                 
1377             if (IS_ERR(__dp))
1378                 goto out;
1379         } while (__dp->d_inode != NULL);
1380
1381         AFS_GLOCK();
1382         code = afs_rename(VTOAFS(dip), (char *)dp->d_name.name, VTOAFS(dip), (char *)__dp->d_name.name, credp);
1383         if (!code) {
1384             tvc->mvid = (void *) __name;
1385             crhold(credp);
1386             if (tvc->uncred) {
1387                 crfree(tvc->uncred);
1388             }
1389             tvc->uncred = credp;
1390             tvc->f.states |= CUnlinked;
1391 #ifdef DCACHE_NFSFS_RENAMED
1392             dp->d_flags |= DCACHE_NFSFS_RENAMED;   
1393 #endif
1394         } else {
1395             osi_FreeSmallSpace(__name); 
1396         }
1397         AFS_GUNLOCK();
1398
1399         if (!code) {
1400             __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1401             d_move(dp, __dp);
1402         }
1403         dput(__dp);
1404
1405         goto out;
1406     }
1407
1408     AFS_GLOCK();
1409     code = afs_remove(VTOAFS(dip), (char *)name, credp);
1410     AFS_GUNLOCK();
1411     if (!code)
1412         d_drop(dp);
1413 out:
1414     crfree(credp);
1415     return afs_convert_code(code);
1416 }
1417
1418
1419 static int
1420 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1421 {
1422     int code;
1423     cred_t *credp = crref();
1424     struct vattr vattr;
1425     const char *name = dp->d_name.name;
1426
1427     /* If afs_symlink returned the vnode, we could instantiate the
1428      * dentry. Since it's not, we drop this one and do a new lookup.
1429      */
1430     d_drop(dp);
1431
1432     VATTR_NULL(&vattr);
1433     AFS_GLOCK();
1434     code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, NULL,
1435                        credp);
1436     AFS_GUNLOCK();
1437     crfree(credp);
1438     return afs_convert_code(code);
1439 }
1440
1441 static int
1442 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1443 {
1444     int code;
1445     cred_t *credp = crref();
1446     struct vcache *tvcp = NULL;
1447     struct vattr vattr;
1448     const char *name = dp->d_name.name;
1449
1450     VATTR_NULL(&vattr);
1451     vattr.va_mask = ATTR_MODE;
1452     vattr.va_mode = mode;
1453     AFS_GLOCK();
1454     code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1455
1456     if (tvcp) {
1457         struct inode *ip = AFSTOV(tvcp);
1458
1459         afs_getattr(tvcp, &vattr, credp);
1460         afs_fill_inode(ip, &vattr);
1461
1462         dp->d_op = &afs_dentry_operations;
1463         dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1464         d_instantiate(dp, ip);
1465     }
1466     AFS_GUNLOCK();
1467
1468     crfree(credp);
1469     return afs_convert_code(code);
1470 }
1471
1472 static int
1473 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1474 {
1475     int code;
1476     cred_t *credp = crref();
1477     const char *name = dp->d_name.name;
1478
1479     /* locking kernel conflicts with glock? */
1480
1481     AFS_GLOCK();
1482     code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1483     AFS_GUNLOCK();
1484
1485     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1486      * that failed because a directory is not empty. So, we map
1487      * EEXIST to ENOTEMPTY on linux.
1488      */
1489     if (code == EEXIST) {
1490         code = ENOTEMPTY;
1491     }
1492
1493     if (!code) {
1494         d_drop(dp);
1495     }
1496
1497     crfree(credp);
1498     return afs_convert_code(code);
1499 }
1500
1501
1502 static int
1503 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1504                  struct inode *newip, struct dentry *newdp)
1505 {
1506     int code;
1507     cred_t *credp = crref();
1508     const char *oldname = olddp->d_name.name;
1509     const char *newname = newdp->d_name.name;
1510     struct dentry *rehash = NULL;
1511
1512     if (!list_empty(&newdp->d_hash)) {
1513         d_drop(newdp);
1514         rehash = newdp;
1515     }
1516
1517 #if defined(AFS_LINUX24_ENV)
1518     if (atomic_read(&olddp->d_count) > 1)
1519         shrink_dcache_parent(olddp);
1520 #endif
1521
1522     AFS_GLOCK();
1523     code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1524     AFS_GUNLOCK();
1525
1526     if (!code)
1527         olddp->d_time = 0;      /* force to revalidate */
1528
1529     if (rehash)
1530         d_rehash(rehash);
1531
1532     crfree(credp);
1533     return afs_convert_code(code);
1534 }
1535
1536
1537 /* afs_linux_ireadlink 
1538  * Internal readlink which can return link contents to user or kernel space.
1539  * Note that the buffer is NOT supposed to be null-terminated.
1540  */
1541 static int
1542 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1543 {
1544     int code;
1545     cred_t *credp = crref();
1546     struct uio tuio;
1547     struct iovec iov;
1548
1549     memset(&tuio, 0, sizeof(tuio));
1550     memset(&iov, 0, sizeof(iov));
1551
1552     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1553     code = afs_readlink(VTOAFS(ip), &tuio, credp);
1554     crfree(credp);
1555
1556     if (!code)
1557         return maxlen - tuio.uio_resid;
1558     else
1559         return afs_convert_code(code);
1560 }
1561
1562 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1563 /* afs_linux_readlink 
1564  * Fill target (which is in user space) with contents of symlink.
1565  */
1566 static int
1567 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1568 {
1569     int code;
1570     struct inode *ip = dp->d_inode;
1571
1572     AFS_GLOCK();
1573     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1574     AFS_GUNLOCK();
1575     return code;
1576 }
1577
1578
1579 /* afs_linux_follow_link
1580  * a file system dependent link following routine.
1581  */
1582 #if defined(AFS_LINUX24_ENV)
1583 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1584 {
1585     int code;
1586     char *name;
1587
1588     name = osi_Alloc(PATH_MAX);
1589     if (!name) {
1590         return -EIO;
1591     }
1592
1593     AFS_GLOCK();
1594     code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1595     AFS_GUNLOCK();
1596
1597     if (code < 0) {
1598         goto out;
1599     }
1600
1601     name[code] = '\0';
1602     code = vfs_follow_link(nd, name);
1603
1604 out:
1605     osi_Free(name, PATH_MAX);
1606
1607     return code;
1608 }
1609
1610 #else /* !defined(AFS_LINUX24_ENV) */
1611
1612 static struct dentry *
1613 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1614                       unsigned int follow)
1615 {
1616     int code = 0;
1617     char *name;
1618     struct dentry *res;
1619
1620
1621     AFS_GLOCK();
1622     name = osi_Alloc(PATH_MAX + 1);
1623     if (!name) {
1624         AFS_GUNLOCK();
1625         dput(basep);
1626         return ERR_PTR(-EIO);
1627     }
1628
1629     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1630     AFS_GUNLOCK();
1631
1632     if (code < 0) {
1633         dput(basep);
1634         if (code < -MAX_ERRNO)
1635             res = ERR_PTR(-EIO);
1636         else
1637             res = ERR_PTR(code);
1638     } else {
1639         name[code] = '\0';
1640         res = lookup_dentry(name, basep, follow);
1641     }
1642
1643     AFS_GLOCK();
1644     osi_Free(name, PATH_MAX + 1);
1645     AFS_GUNLOCK();
1646     return res;
1647 }
1648 #endif /* AFS_LINUX24_ENV */
1649 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1650
1651 static inline int
1652 afs_linux_can_bypass(struct inode *ip) {
1653     switch(cache_bypass_strategy) {
1654         case NEVER_BYPASS_CACHE:
1655             return 0;
1656         case ALWAYS_BYPASS_CACHE:
1657             return 1;
1658         case LARGE_FILES_BYPASS_CACHE:
1659             if(i_size_read(ip) > cache_bypass_threshold)
1660                 return 1;
1661         default:
1662             return 0;
1663      }
1664 }
1665
1666 /* afs_linux_readpage
1667  * all reads come through here. A strategy-like read call.
1668  */
1669 static int
1670 afs_linux_readpage(struct file *fp, struct page *pp)
1671 {
1672     afs_int32 code;
1673 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1674     char *address;
1675     afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1676 #else
1677     ulong address = afs_linux_page_address(pp);
1678     afs_offs_t offset = pageoff(pp);
1679 #endif
1680     afs_int32 bypasscache = 0; /* bypass for this read */
1681     struct nocache_read_request *ancr;
1682 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1683     afs_int32 isize;
1684 #endif
1685     struct uio *auio;
1686     struct iovec *iovecp;
1687     struct inode *ip = FILE_INODE(fp);
1688     afs_int32 cnt = page_count(pp);
1689     struct vcache *avc = VTOAFS(ip);
1690     cred_t *credp;
1691
1692     credp = crref();
1693 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1694     address = kmap(pp);
1695     ClearPageError(pp);
1696 #else
1697     atomic_add(1, &pp->count);
1698     set_bit(PG_locked, &pp->flags);     /* other bits? See mm.h */
1699     clear_bit(PG_error, &pp->flags);
1700 #endif
1701     /* if bypasscache, receiver frees, else we do */
1702     auio = osi_Alloc(sizeof(struct uio));
1703     iovecp = osi_Alloc(sizeof(struct iovec));
1704
1705     setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1706               AFS_UIOSYS);
1707
1708     bypasscache = afs_linux_can_bypass(ip);
1709
1710     /* In the new incarnation of selective caching, a file's caching policy
1711      * can change, eg because file size exceeds threshold, etc. */
1712     trydo_cache_transition(avc, credp, bypasscache);
1713         
1714     if(bypasscache) {
1715         if(address)
1716             kunmap(pp);
1717         /* save the page for background map */
1718         auio->uio_iov->iov_base = (void*) pp;
1719         /* the background thread will free this */
1720         ancr = osi_Alloc(sizeof(struct nocache_read_request));
1721         ancr->auio = auio;
1722         ancr->offset = offset;
1723         ancr->length = PAGE_SIZE;
1724
1725         maybe_lock_kernel();
1726         code = afs_ReadNoCache(avc, ancr, credp);
1727         maybe_unlock_kernel();
1728
1729         goto done; /* skips release page, doing it in bg thread */
1730     }
1731                   
1732 #ifdef AFS_LINUX24_ENV
1733     maybe_lock_kernel();
1734 #endif
1735     AFS_GLOCK();
1736     AFS_DISCON_LOCK();
1737     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1738                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1739                99999);  /* not a possible code value */
1740
1741     code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1742         
1743     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1744                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1745                code);
1746     AFS_DISCON_UNLOCK();
1747     AFS_GUNLOCK();
1748 #ifdef AFS_LINUX24_ENV
1749     maybe_unlock_kernel();
1750 #endif
1751     if (!code) {
1752         /* XXX valid for no-cache also?  Check last bits of files... :)
1753          * Cognate code goes in afs_NoCacheFetchProc.  */
1754         if (auio->uio_resid)    /* zero remainder of page */
1755              memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1756                     auio->uio_resid);
1757
1758 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1759         flush_dcache_page(pp);
1760         SetPageUptodate(pp);
1761 #else
1762         set_bit(PG_uptodate, &pp->flags);
1763 #endif
1764     } /* !code */
1765
1766 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1767     kunmap(pp);
1768     UnlockPage(pp);
1769 #else
1770     clear_bit(PG_locked, &pp->flags);
1771     wake_up(&pp->wait);
1772     free_page(address);
1773 #endif
1774
1775     /* do not call afs_GetDCache if cache is bypassed */
1776     if(bypasscache)
1777         goto done;
1778
1779     /* free if not bypassing cache */
1780     osi_Free(auio, sizeof(struct uio));
1781     osi_Free(iovecp, sizeof(struct iovec));
1782
1783     if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1784         struct dcache *tdc;
1785         struct vrequest treq;
1786
1787         AFS_GLOCK();
1788         code = afs_InitReq(&treq, credp);
1789         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1790             tdc = afs_FindDCache(avc, offset);
1791             if (tdc) {
1792                 if (!(tdc->mflags & DFNextStarted))
1793                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1794                     afs_PutDCache(tdc);
1795             }
1796             ReleaseWriteLock(&avc->lock);
1797         }
1798         AFS_GUNLOCK();
1799     }
1800
1801 done:
1802     crfree(credp);
1803     return afs_convert_code(code);
1804 }
1805
1806 #if defined(AFS_LINUX24_ENV)
1807 static int
1808 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1809                          unsigned long offset, unsigned int count)
1810 {
1811     struct vcache *vcp = VTOAFS(ip);
1812     char *buffer;
1813     afs_offs_t base;
1814     int code = 0;
1815     cred_t *credp;
1816     struct uio tuio;
1817     struct iovec iovec;
1818     int f_flags = 0;
1819
1820     memset(&tuio, 0, sizeof(tuio));
1821     memset(&iovec, 0, sizeof(iovec));
1822
1823     buffer = kmap(pp) + offset;
1824     base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT)  + offset;
1825
1826     credp = crref();
1827     maybe_lock_kernel();
1828     AFS_GLOCK();
1829     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1830                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1831                ICL_TYPE_INT32, 99999);
1832
1833     ObtainWriteLock(&vcp->lock, 532);
1834     if (vcp->f.states & CPageWrite) {
1835         ReleaseWriteLock(&vcp->lock);
1836         AFS_GUNLOCK();
1837         maybe_unlock_kernel();
1838         crfree(credp);
1839         kunmap(pp);
1840         /* should mark it dirty? */
1841         return(0); 
1842     }
1843     vcp->f.states |= CPageWrite;
1844     ReleaseWriteLock(&vcp->lock);
1845
1846     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1847
1848     code = afs_write(vcp, &tuio, f_flags, credp, 0);
1849
1850     i_size_write(ip, vcp->f.m.Length);
1851     ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
1852
1853     ObtainWriteLock(&vcp->lock, 533);
1854     if (!code) {
1855         struct vrequest treq;
1856
1857         if (!afs_InitReq(&treq, credp))
1858             code = afs_DoPartialWrite(vcp, &treq);
1859     }
1860     code = code ? afs_convert_code(code) : count - tuio.uio_resid;
1861
1862     vcp->f.states &= ~CPageWrite;
1863     ReleaseWriteLock(&vcp->lock);
1864
1865     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1866                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1867                ICL_TYPE_INT32, code);
1868
1869     AFS_GUNLOCK();
1870     maybe_unlock_kernel();
1871     crfree(credp);
1872     kunmap(pp);
1873
1874     return code;
1875 }
1876
1877
1878 static int
1879 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1880 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1881 #else
1882 afs_linux_writepage(struct page *pp)
1883 #endif
1884 {
1885     struct address_space *mapping = pp->mapping;
1886     struct inode *inode;
1887     unsigned long end_index;
1888     unsigned offset = PAGE_CACHE_SIZE;
1889     long status;
1890
1891     if (PageLaunder(pp)) {
1892         return(fail_writepage(pp));
1893     }
1894
1895     inode = (struct inode *)mapping->host;
1896     end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
1897
1898     /* easy case */
1899     if (pp->index < end_index)
1900         goto do_it;
1901     /* things got complicated... */
1902     offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
1903     /* OK, are we completely out? */
1904     if (pp->index >= end_index + 1 || !offset)
1905         return -EIO;
1906   do_it:
1907     status = afs_linux_writepage_sync(inode, pp, 0, offset);
1908     SetPageUptodate(pp);
1909     UnlockPage(pp);
1910     if (status == offset)
1911         return 0;
1912     else
1913         return status;
1914 }
1915 #else
1916 /* afs_linux_updatepage
1917  * What one would have thought was writepage - write dirty page to file.
1918  * Called from generic_file_write. buffer is still in user space. pagep
1919  * has been filled in with old data if we're updating less than a page.
1920  */
1921 static int
1922 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1923                      unsigned int count, int sync)
1924 {
1925     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1926     u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1927     int code = 0;
1928     cred_t *credp;
1929     struct uio tuio;
1930     struct iovec iovec;
1931
1932     memset(&tuio, 0, sizeof(tuio));
1933     memset(&iovec, 0, sizeof(iovec));
1934
1935     set_bit(PG_locked, &pp->flags);
1936
1937     credp = crref();
1938     AFS_GLOCK();
1939     AFS_DISCON_LOCK();
1940     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1941                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1942                ICL_TYPE_INT32, 99999);
1943     setup_uio(&tuio, &iovec, page_addr + offset,
1944               (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1945               AFS_UIOSYS);
1946
1947     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1948
1949     i_size_write(ip, vcp->f.m.Length);
1950     ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
1951
1952     if (!code) {
1953         struct vrequest treq;
1954
1955         ObtainWriteLock(&vcp->lock, 533);
1956         vcp->f.m.Date = osi_Time();   /* set modification time */
1957         if (!afs_InitReq(&treq, credp))
1958             code = afs_DoPartialWrite(vcp, &treq);
1959         ReleaseWriteLock(&vcp->lock);
1960     }
1961
1962     code = code ? afs_convert_code(code) : count - tuio.uio_resid;
1963     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1964                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1965                ICL_TYPE_INT32, code);
1966
1967     AFS_DISCON_UNLOCK();
1968     AFS_GUNLOCK();
1969     crfree(credp);
1970
1971     clear_bit(PG_locked, &pp->flags);
1972     return code;
1973 }
1974 #endif
1975
1976 /* afs_linux_permission
1977  * Check access rights - returns error if can't check or permission denied.
1978  */
1979 static int
1980 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1981 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1982 #else
1983 afs_linux_permission(struct inode *ip, int mode)
1984 #endif
1985 {
1986     int code;
1987     cred_t *credp = crref();
1988     int tmp = 0;
1989
1990     AFS_GLOCK();
1991     if (mode & MAY_EXEC)
1992         tmp |= VEXEC;
1993     if (mode & MAY_READ)
1994         tmp |= VREAD;
1995     if (mode & MAY_WRITE)
1996         tmp |= VWRITE;
1997     code = afs_access(VTOAFS(ip), tmp, credp);
1998
1999     AFS_GUNLOCK();
2000     crfree(credp);
2001     return afs_convert_code(code);
2002 }
2003
2004 #if defined(AFS_LINUX24_ENV) && !defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2005 static int
2006 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2007                        unsigned to)
2008 {
2009     int code;
2010
2011     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2012                                     offset, to - offset);
2013     kunmap(page);
2014
2015     return code;
2016 }
2017
2018 static int
2019 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2020                         unsigned to)
2021 {
2022 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
2023    call kmap directly instead of relying on us to do it */
2024     kmap(page);
2025     return 0;
2026 }
2027 #endif
2028
2029 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2030 static int
2031 afs_linux_write_end(struct file *file, struct address_space *mapping,
2032                                 loff_t pos, unsigned len, unsigned copied,
2033                                 struct page *page, void *fsdata)
2034 {
2035     int code;
2036     unsigned from = pos & (PAGE_CACHE_SIZE - 1);
2037
2038     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2039                                     from, copied);
2040     unlock_page(page);
2041     page_cache_release(page);
2042     return code;
2043 }
2044
2045 static int
2046 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2047                                 loff_t pos, unsigned len, unsigned flags,
2048                                 struct page **pagep, void **fsdata)
2049 {
2050     struct page *page;
2051     pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2052 #if defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN)
2053     page = grab_cache_page_write_begin(mapping, index, flags);
2054 #else
2055     page = __grab_cache_page(mapping, index);
2056 #endif
2057     *pagep = page;
2058
2059     return 0;
2060 }
2061 #endif
2062
2063 static int
2064 afs_linux_dir_follow_link(struct dentry *dentry, struct nameidata *nd)
2065 {
2066     struct dentry **dpp;
2067     struct dentry *target;
2068
2069     target = canonical_dentry(dentry->d_inode);
2070
2071     dpp = &nd->dentry;
2072
2073     dput(*dpp);
2074
2075     if (target) {
2076         *dpp = target;
2077     } else {
2078         *dpp = dget(dentry);
2079     }
2080
2081     nd->last_type = LAST_BIND;
2082
2083     return 0;
2084 }
2085
2086 static struct inode_operations afs_file_iops = {
2087 #if defined(AFS_LINUX24_ENV)
2088   .permission =         afs_linux_permission,
2089   .revalidate =         afs_linux_revalidate,
2090   .setattr =            afs_notify_change,
2091 #else
2092   .default_file_ops =   &afs_file_fops,
2093   .readpage =           afs_linux_readpage,  
2094   .revalidate =         afs_linux_revalidate,
2095   .updatepage =         afs_linux_updatepage,
2096 #endif
2097 };
2098
2099 #if defined(AFS_LINUX24_ENV)
2100 static struct address_space_operations afs_file_aops = {
2101   .readpage =           afs_linux_readpage,
2102   .writepage =          afs_linux_writepage,
2103 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2104   .write_begin =        afs_linux_write_begin,
2105   .write_end =          afs_linux_write_end,
2106 #else
2107   .commit_write =       afs_linux_commit_write,
2108   .prepare_write =      afs_linux_prepare_write,
2109 #endif
2110 };
2111 #endif
2112
2113
2114 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2115  * by what sort of operation is allowed.....
2116  */
2117
2118 static struct inode_operations afs_dir_iops = {
2119 #if !defined(AFS_LINUX24_ENV)
2120   .default_file_ops =   &afs_dir_fops,
2121 #else
2122   .setattr =            afs_notify_change,
2123 #endif
2124   .create =             afs_linux_create,
2125   .lookup =             afs_linux_lookup,
2126   .link =               afs_linux_link,
2127   .unlink =             afs_linux_unlink,
2128   .symlink =            afs_linux_symlink,
2129   .mkdir =              afs_linux_mkdir,
2130   .rmdir =              afs_linux_rmdir,
2131   .rename =             afs_linux_rename,
2132   .revalidate =         afs_linux_revalidate,
2133   .permission =         afs_linux_permission,
2134   .follow_link =        afs_linux_dir_follow_link,
2135 };
2136
2137 /* We really need a separate symlink set of ops, since do_follow_link()
2138  * determines if it _is_ a link by checking if the follow_link op is set.
2139  */
2140 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2141 static int
2142 afs_symlink_filler(struct file *file, struct page *page)
2143 {
2144     struct inode *ip = (struct inode *)page->mapping->host;
2145     char *p = (char *)kmap(page);
2146     int code;
2147
2148     maybe_lock_kernel();
2149     AFS_GLOCK();
2150     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2151     AFS_GUNLOCK();
2152
2153     if (code < 0)
2154         goto fail;
2155     p[code] = '\0';             /* null terminate? */
2156     maybe_unlock_kernel();
2157
2158     SetPageUptodate(page);
2159     kunmap(page);
2160     UnlockPage(page);
2161     return 0;
2162
2163   fail:
2164     maybe_unlock_kernel();
2165
2166     SetPageError(page);
2167     kunmap(page);
2168     UnlockPage(page);
2169     return code;
2170 }
2171
2172 static struct address_space_operations afs_symlink_aops = {
2173   .readpage =   afs_symlink_filler
2174 };
2175 #endif  /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2176
2177 static struct inode_operations afs_symlink_iops = {
2178 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2179   .readlink =           page_readlink,
2180 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2181   .follow_link =        page_follow_link,
2182 #else
2183   .follow_link =        page_follow_link_light,
2184   .put_link =           page_put_link,
2185 #endif
2186 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2187   .readlink =           afs_linux_readlink,
2188   .follow_link =        afs_linux_follow_link,
2189 #if !defined(AFS_LINUX24_ENV)
2190   .permission =         afs_linux_permission,
2191   .revalidate =         afs_linux_revalidate,
2192 #endif
2193 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2194 #if defined(AFS_LINUX24_ENV)
2195   .setattr =            afs_notify_change,
2196 #endif
2197 };
2198
2199 void
2200 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2201 {
2202         
2203     if (vattr)
2204         vattr2inode(ip, vattr);
2205
2206 /* Reset ops if symlink or directory. */
2207     if (S_ISREG(ip->i_mode)) {
2208         ip->i_op = &afs_file_iops;
2209 #if defined(AFS_LINUX24_ENV)
2210         ip->i_fop = &afs_file_fops;
2211         ip->i_data.a_ops = &afs_file_aops;
2212 #endif
2213
2214     } else if (S_ISDIR(ip->i_mode)) {
2215         ip->i_op = &afs_dir_iops;
2216 #if defined(AFS_LINUX24_ENV)
2217         ip->i_fop = &afs_dir_fops;
2218 #endif
2219
2220     } else if (S_ISLNK(ip->i_mode)) {
2221         ip->i_op = &afs_symlink_iops;
2222 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2223         ip->i_data.a_ops = &afs_symlink_aops;
2224         ip->i_mapping = &ip->i_data;
2225 #endif
2226     }
2227
2228 }