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