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