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