625db177bd042b0299ce80172b2b968399e2930a
[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         code = afs_CopyOutAttrs(vcp, &vattr);
878     } else {
879         credp = crref();
880         code = afs_getattr(vcp, &vattr, credp);
881         crfree(credp);
882     }
883     if (!code)
884         afs_fill_inode(AFSTOV(vcp), &vattr);
885
886     AFS_GUNLOCK();
887 #ifdef AFS_LINUX24_ENV
888     maybe_unlock_kernel();
889 #endif
890
891     return afs_convert_code(code);
892 }
893
894 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
895  * In kernels 2.2.10 and above, we are passed an additional flags var which
896  * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
897  * we are advised to follow the entry if it is a link or to make sure that 
898  * it is a directory. But since the kernel itself checks these possibilities
899  * later on, we shouldn't have to do it until later. Perhaps in the future..
900  */
901 static int
902 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
903 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
904 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
905 #else
906 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
907 #endif
908 #else
909 afs_linux_dentry_revalidate(struct dentry *dp)
910 #endif
911 {
912     struct vattr vattr;
913     cred_t *credp = NULL;
914     struct vcache *vcp, *pvcp, *tvc = NULL;
915     int valid;
916     struct afs_fakestat_state fakestate;
917
918 #ifdef AFS_LINUX24_ENV
919     maybe_lock_kernel();
920 #endif
921     AFS_GLOCK();
922     afs_InitFakeStat(&fakestate);
923
924     if (dp->d_inode) {
925
926         vcp = VTOAFS(dp->d_inode);
927         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
928
929         if (vcp == afs_globalVp)
930             goto good_dentry;
931
932         if (vcp->mvstat == 1) {         /* mount point */
933             if (vcp->mvid && (vcp->f.states & CMValid)) {
934                 int tryEvalOnly = 0;
935                 int code = 0;
936                 struct vrequest treq;
937
938                 credp = crref();
939                 code = afs_InitReq(&treq, credp);
940                 if (
941 #ifdef AFS_DARWIN_ENV
942                     (strcmp(dp->d_name.name, ".DS_Store") == 0) ||
943                     (strcmp(dp->d_name.name, "Contents") == 0) ||
944 #endif
945                     (strcmp(dp->d_name.name, ".directory") == 0)) {
946                     tryEvalOnly = 1;
947                 }
948                 if (tryEvalOnly)
949                     code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
950                 else
951                     code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
952                 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
953                     /* a mount point, not yet replaced by its directory */
954                     goto bad_dentry;
955                 }
956             }
957         } else
958             if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
959                 check_bad_parent(dp);   /* check and correct mvid */
960
961 #ifdef notdef
962         /* If the last looker changes, we should make sure the current
963          * looker still has permission to examine this file.  This would
964          * always require a crref() which would be "slow".
965          */
966         if (vcp->last_looker != treq.uid) {
967             if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
968                 goto bad_dentry;
969
970             vcp->last_looker = treq.uid;
971         }
972 #endif
973
974         /* If the parent's DataVersion has changed or the vnode
975          * is longer valid, we need to do a full lookup.  VerifyVCache
976          * isn't enough since the vnode may have been renamed.
977          */
978
979         if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
980
981             credp = crref();
982             afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
983             if (!tvc || tvc != vcp)
984                 goto bad_dentry;
985
986             if (afs_getattr(vcp, &vattr, credp))
987                 goto bad_dentry;
988
989             vattr2inode(AFSTOV(vcp), &vattr);
990             dp->d_time = hgetlo(pvcp->f.m.DataVersion);
991         }
992
993         /* should we always update the attributes at this point? */
994         /* unlikely--the vcache entry hasn't changed */
995
996     } else {
997 #ifdef notyet
998         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
999         if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1000             goto bad_dentry;
1001 #endif
1002
1003         /* No change in parent's DataVersion so this negative
1004          * lookup is still valid.  BUT, if a server is down a
1005          * negative lookup can result so there should be a
1006          * liftime as well.  For now, always expire.
1007          */
1008
1009         goto bad_dentry;
1010     }
1011
1012   good_dentry:
1013     valid = 1;
1014
1015   done:
1016     /* Clean up */
1017     if (tvc)
1018         afs_PutVCache(tvc);
1019     afs_PutFakeStat(&fakestate);
1020     AFS_GUNLOCK();
1021     if (credp)
1022         crfree(credp);
1023
1024     if (!valid) {
1025         shrink_dcache_parent(dp);
1026         d_drop(dp);
1027     }
1028 #ifdef AFS_LINUX24_ENV
1029     maybe_unlock_kernel();
1030 #endif
1031     return valid;
1032
1033   bad_dentry:
1034     if (have_submounts(dp))
1035         valid = 1;
1036     else 
1037         valid = 0;
1038     goto done;
1039 }
1040
1041 static void
1042 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1043 {
1044     struct vcache *vcp = VTOAFS(ip);
1045
1046     AFS_GLOCK();
1047     if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1048         (void) afs_InactiveVCache(vcp, NULL);
1049     }
1050     AFS_GUNLOCK();
1051 #ifdef DCACHE_NFSFS_RENAMED
1052     dp->d_flags &= ~DCACHE_NFSFS_RENAMED;   
1053 #endif
1054
1055     iput(ip);
1056 }
1057
1058 static int
1059 afs_dentry_delete(struct dentry *dp)
1060 {
1061     if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1062         return 1;               /* bad inode? */
1063
1064     return 0;
1065 }
1066
1067 struct dentry_operations afs_dentry_operations = {
1068   .d_revalidate =       afs_linux_dentry_revalidate,
1069   .d_delete =           afs_dentry_delete,
1070   .d_iput =             afs_dentry_iput,
1071 };
1072
1073 /**********************************************************************
1074  * AFS Linux inode operations
1075  **********************************************************************/
1076
1077 /* afs_linux_create
1078  *
1079  * Merely need to set enough of vattr to get us through the create. Note
1080  * that the higher level code (open_namei) will take care of any tuncation
1081  * explicitly. Exclusive open is also taken care of in open_namei.
1082  *
1083  * name is in kernel space at this point.
1084  */
1085 static int
1086 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1087 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1088                  struct nameidata *nd)
1089 #else
1090 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1091 #endif
1092 {
1093     struct vattr vattr;
1094     cred_t *credp = crref();
1095     const char *name = dp->d_name.name;
1096     struct vcache *vcp;
1097     int code;
1098
1099     VATTR_NULL(&vattr);
1100     vattr.va_mode = mode;
1101     vattr.va_type = mode & S_IFMT;
1102
1103     AFS_GLOCK();
1104     code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1105                       &vcp, credp);
1106
1107     if (!code) {
1108         struct inode *ip = AFSTOV(vcp);
1109
1110         afs_getattr(vcp, &vattr, credp);
1111         afs_fill_inode(ip, &vattr);
1112         insert_inode_hash(ip);
1113         dp->d_op = &afs_dentry_operations;
1114         dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1115         d_instantiate(dp, ip);
1116     }
1117     AFS_GUNLOCK();
1118
1119     crfree(credp);
1120     return afs_convert_code(code);
1121 }
1122
1123 /* afs_linux_lookup */
1124 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1125 static struct dentry *
1126 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1127 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1128                  struct nameidata *nd)
1129 #else
1130 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1131 #endif
1132 #else
1133 static int
1134 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1135 #endif
1136 {
1137     cred_t *credp = crref();
1138     struct vcache *vcp = NULL;
1139     const char *comp = dp->d_name.name;
1140     struct inode *ip = NULL;
1141     int code;
1142
1143     AFS_GLOCK();
1144     code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1145     
1146     if (vcp) {
1147         struct vattr vattr;
1148
1149         ip = AFSTOV(vcp);
1150         afs_getattr(vcp, &vattr, credp);
1151         afs_fill_inode(ip, &vattr);
1152         if (
1153 #ifdef HAVE_KERNEL_HLIST_UNHASHED
1154             hlist_unhashed(&ip->i_hash)
1155 #else
1156             ip->i_hash.prev == NULL
1157 #endif
1158             )
1159             insert_inode_hash(ip);
1160     }
1161     dp->d_op = &afs_dentry_operations;
1162     dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1163     AFS_GUNLOCK();
1164
1165 #if defined(AFS_LINUX24_ENV)
1166     if (ip && S_ISDIR(ip->i_mode)) {
1167         struct dentry *alias;
1168
1169         /* Try to invalidate an existing alias in favor of our new one */
1170         alias = d_find_alias(ip);
1171         if (alias) {
1172             if (d_invalidate(alias) == 0) {
1173                 dput(alias);
1174             } else {
1175                 iput(ip);
1176                 crfree(credp);
1177                 return alias;
1178             }
1179         }
1180     }
1181 #endif
1182     d_add(dp, ip);
1183
1184     crfree(credp);
1185
1186     /* It's ok for the file to not be found. That's noted by the caller by
1187      * seeing that the dp->d_inode field is NULL.
1188      */
1189 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1190     if (code == ENOENT)
1191         return ERR_PTR(0);
1192     else 
1193         return ERR_PTR(afs_convert_code(code));
1194 #else
1195     if (code == ENOENT)
1196         code = 0;
1197     return afs_convert_code(code);
1198 #endif
1199 }
1200
1201 static int
1202 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1203 {
1204     int code;
1205     cred_t *credp = crref();
1206     const char *name = newdp->d_name.name;
1207     struct inode *oldip = olddp->d_inode;
1208
1209     /* If afs_link returned the vnode, we could instantiate the
1210      * dentry. Since it's not, we drop this one and do a new lookup.
1211      */
1212     d_drop(newdp);
1213
1214     AFS_GLOCK();
1215     code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1216
1217     AFS_GUNLOCK();
1218     crfree(credp);
1219     return afs_convert_code(code);
1220 }
1221
1222 static int
1223 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1224 {
1225     int code = EBUSY;
1226     cred_t *credp = crref();
1227     const char *name = dp->d_name.name;
1228     struct vcache *tvc = VTOAFS(dp->d_inode);
1229
1230     if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1231                                 && !(tvc->f.states & CUnlinked)) {
1232         struct dentry *__dp;
1233         char *__name;
1234
1235         __dp = NULL;
1236         __name = NULL;
1237         do {
1238             dput(__dp);
1239
1240             AFS_GLOCK();
1241             if (__name)
1242                 osi_FreeSmallSpace(__name);
1243             __name = afs_newname();
1244             AFS_GUNLOCK();
1245
1246             __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1247                 
1248             if (IS_ERR(__dp))
1249                 goto out;
1250         } while (__dp->d_inode != NULL);
1251
1252         AFS_GLOCK();
1253         code = afs_rename(VTOAFS(dip), (char *)dp->d_name.name, VTOAFS(dip), (char *)__dp->d_name.name, credp);
1254         if (!code) {
1255             tvc->mvid = (void *) __name;
1256             crhold(credp);
1257             if (tvc->uncred) {
1258                 crfree(tvc->uncred);
1259             }
1260             tvc->uncred = credp;
1261             tvc->f.states |= CUnlinked;
1262 #ifdef DCACHE_NFSFS_RENAMED
1263             dp->d_flags |= DCACHE_NFSFS_RENAMED;   
1264 #endif
1265         } else {
1266             osi_FreeSmallSpace(__name); 
1267         }
1268         AFS_GUNLOCK();
1269
1270         if (!code) {
1271             __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1272             d_move(dp, __dp);
1273         }
1274         dput(__dp);
1275
1276         goto out;
1277     }
1278
1279     AFS_GLOCK();
1280     code = afs_remove(VTOAFS(dip), (char *)name, credp);
1281     AFS_GUNLOCK();
1282     if (!code)
1283         d_drop(dp);
1284 out:
1285     crfree(credp);
1286     return afs_convert_code(code);
1287 }
1288
1289
1290 static int
1291 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1292 {
1293     int code;
1294     cred_t *credp = crref();
1295     struct vattr vattr;
1296     const char *name = dp->d_name.name;
1297
1298     /* If afs_symlink returned the vnode, we could instantiate the
1299      * dentry. Since it's not, we drop this one and do a new lookup.
1300      */
1301     d_drop(dp);
1302
1303     VATTR_NULL(&vattr);
1304     AFS_GLOCK();
1305     code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1306     AFS_GUNLOCK();
1307     crfree(credp);
1308     return afs_convert_code(code);
1309 }
1310
1311 static int
1312 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1313 {
1314     int code;
1315     cred_t *credp = crref();
1316     struct vcache *tvcp = NULL;
1317     struct vattr vattr;
1318     const char *name = dp->d_name.name;
1319
1320     VATTR_NULL(&vattr);
1321     vattr.va_mask = ATTR_MODE;
1322     vattr.va_mode = mode;
1323     AFS_GLOCK();
1324     code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1325
1326     if (tvcp) {
1327         struct inode *ip = AFSTOV(tvcp);
1328
1329         afs_getattr(tvcp, &vattr, credp);
1330         afs_fill_inode(ip, &vattr);
1331
1332         dp->d_op = &afs_dentry_operations;
1333         dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1334         d_instantiate(dp, ip);
1335     }
1336     AFS_GUNLOCK();
1337
1338     crfree(credp);
1339     return afs_convert_code(code);
1340 }
1341
1342 static int
1343 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1344 {
1345     int code;
1346     cred_t *credp = crref();
1347     const char *name = dp->d_name.name;
1348
1349     /* locking kernel conflicts with glock? */
1350
1351     AFS_GLOCK();
1352     code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1353     AFS_GUNLOCK();
1354
1355     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1356      * that failed because a directory is not empty. So, we map
1357      * EEXIST to ENOTEMPTY on linux.
1358      */
1359     if (code == EEXIST) {
1360         code = ENOTEMPTY;
1361     }
1362
1363     if (!code) {
1364         d_drop(dp);
1365     }
1366
1367     crfree(credp);
1368     return afs_convert_code(code);
1369 }
1370
1371
1372 static int
1373 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1374                  struct inode *newip, struct dentry *newdp)
1375 {
1376     int code;
1377     cred_t *credp = crref();
1378     const char *oldname = olddp->d_name.name;
1379     const char *newname = newdp->d_name.name;
1380     struct dentry *rehash = NULL;
1381
1382     if (!list_empty(&newdp->d_hash)) {
1383         d_drop(newdp);
1384         rehash = newdp;
1385     }
1386
1387 #if defined(AFS_LINUX24_ENV)
1388     if (atomic_read(&olddp->d_count) > 1)
1389         shrink_dcache_parent(olddp);
1390 #endif
1391
1392     AFS_GLOCK();
1393     code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1394     AFS_GUNLOCK();
1395
1396     if (!code)
1397         olddp->d_time = 0;      /* force to revalidate */
1398
1399     if (rehash)
1400         d_rehash(rehash);
1401
1402     crfree(credp);
1403     return afs_convert_code(code);
1404 }
1405
1406
1407 /* afs_linux_ireadlink 
1408  * Internal readlink which can return link contents to user or kernel space.
1409  * Note that the buffer is NOT supposed to be null-terminated.
1410  */
1411 static int
1412 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1413 {
1414     int code;
1415     cred_t *credp = crref();
1416     uio_t tuio;
1417     struct iovec iov;
1418
1419     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1420     code = afs_readlink(VTOAFS(ip), &tuio, credp);
1421     crfree(credp);
1422
1423     if (!code)
1424         return maxlen - tuio.uio_resid;
1425     else
1426         return afs_convert_code(code);
1427 }
1428
1429 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1430 /* afs_linux_readlink 
1431  * Fill target (which is in user space) with contents of symlink.
1432  */
1433 static int
1434 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1435 {
1436     int code;
1437     struct inode *ip = dp->d_inode;
1438
1439     AFS_GLOCK();
1440     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1441     AFS_GUNLOCK();
1442     return code;
1443 }
1444
1445
1446 /* afs_linux_follow_link
1447  * a file system dependent link following routine.
1448  */
1449 #if defined(AFS_LINUX24_ENV)
1450 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1451 {
1452     int code;
1453     char *name;
1454
1455     name = osi_Alloc(PATH_MAX);
1456     if (!name) {
1457         return -EIO;
1458     }
1459
1460     AFS_GLOCK();
1461     code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1462     AFS_GUNLOCK();
1463
1464     if (code < 0) {
1465         goto out;
1466     }
1467
1468     name[code] = '\0';
1469     code = vfs_follow_link(nd, name);
1470
1471 out:
1472     osi_Free(name, PATH_MAX);
1473
1474     return code;
1475 }
1476
1477 #else /* !defined(AFS_LINUX24_ENV) */
1478
1479 static struct dentry *
1480 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1481                       unsigned int follow)
1482 {
1483     int code = 0;
1484     char *name;
1485     struct dentry *res;
1486
1487
1488     AFS_GLOCK();
1489     name = osi_Alloc(PATH_MAX + 1);
1490     if (!name) {
1491         AFS_GUNLOCK();
1492         dput(basep);
1493         return ERR_PTR(-EIO);
1494     }
1495
1496     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1497     AFS_GUNLOCK();
1498
1499     if (code < 0) {
1500         dput(basep);
1501         if (code < -MAX_ERRNO)
1502             res = ERR_PTR(-EIO);
1503         else
1504             res = ERR_PTR(code);
1505     } else {
1506         name[code] = '\0';
1507         res = lookup_dentry(name, basep, follow);
1508     }
1509
1510     AFS_GLOCK();
1511     osi_Free(name, PATH_MAX + 1);
1512     AFS_GUNLOCK();
1513     return res;
1514 }
1515 #endif /* AFS_LINUX24_ENV */
1516 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1517
1518 #if defined(AFS_CACHE_BYPASS)
1519
1520 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1521
1522 static inline int
1523 afs_linux_can_bypass(struct inode *ip) {
1524     switch(cache_bypass_strategy) {
1525         case NEVER_BYPASS_CACHE:
1526             return 0;
1527         case ALWAYS_BYPASS_CACHE:
1528             return 1;
1529         case LARGE_FILES_BYPASS_CACHE:
1530             if(i_size_read(ip) > cache_bypass_threshold)
1531                 return 1;
1532         default:
1533      }
1534      return 0;
1535 }
1536
1537 static int
1538 afs_linux_cache_bypass_read(struct file *fp, struct address_space *mapping,
1539                             struct list_head *page_list, unsigned num_pages)
1540 {
1541     afs_int32 page_ix;
1542     uio_t *auio;
1543     afs_offs_t offset;
1544     struct iovec* iovecp;
1545     struct nocache_read_request *ancr;
1546     struct page *pp, *ppt;
1547     struct pagevec lrupv;
1548     afs_int32 code = 0; 
1549
1550     cred_t *credp;
1551     struct inode *ip = FILE_INODE(fp);
1552     struct vcache *avc = VTOAFS(ip);
1553     afs_int32 bypasscache = 0; /* bypass for this read */
1554     afs_int32 base_index = 0;
1555     afs_int32 page_count = 0;
1556     afs_int32 isize;
1557         
1558     /* background thread must free: iovecp, auio, ancr */
1559     iovecp = osi_Alloc(num_pages * sizeof(struct iovec));
1560
1561     auio = osi_Alloc(sizeof(uio_t));
1562     auio->uio_iov = iovecp;     
1563     auio->uio_iovcnt = num_pages;
1564     auio->uio_flag = UIO_READ;
1565     auio->uio_seg = AFS_UIOSYS;
1566     auio->uio_resid = num_pages * PAGE_SIZE;
1567         
1568     ancr = osi_Alloc(sizeof(struct nocache_read_request));
1569     ancr->auio = auio;
1570     ancr->offset = auio->uio_offset;
1571     ancr->length = auio->uio_resid;
1572         
1573     pagevec_init(&lrupv, 0);    
1574         
1575     for(page_ix = 0; page_ix < num_pages; ++page_ix) {
1576         
1577         if(list_empty(page_list))
1578             break;
1579
1580         pp = list_entry(page_list->prev, struct page, lru);
1581         /* If we allocate a page and don't remove it from page_list,
1582          * the page cache gets upset. */
1583         list_del(&pp->lru);
1584         isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1585         if(pp->index > isize) {
1586             if(PageLocked(pp))
1587                 UnlockPage(pp);
1588             continue;
1589         }
1590
1591         if(page_ix == 0) {
1592             offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1593             auio->uio_offset = offset;
1594             base_index = pp->index;
1595         }
1596         iovecp[page_ix].iov_len = PAGE_SIZE;
1597         code = add_to_page_cache(pp, mapping, pp->index, GFP_KERNEL);
1598         if(base_index != pp->index) {   
1599             if(PageLocked(pp))
1600                                  UnlockPage(pp);
1601             page_cache_release(pp);
1602             iovecp[page_ix].iov_base = (void *) 0;
1603             base_index++;
1604             continue;
1605         }
1606         base_index++;
1607         if(code) {
1608             if(PageLocked(pp))
1609                 UnlockPage(pp);
1610             page_cache_release(pp);
1611             iovecp[page_ix].iov_base = (void *) 0;
1612         } else {
1613             page_count++;
1614             if(!PageLocked(pp)) {
1615                 LockPage(pp);
1616             }   
1617             
1618             /* save the page for background map */
1619             iovecp[page_ix].iov_base = (void*) pp;
1620
1621             /* and put it on the LRU cache */
1622             if (!pagevec_add(&lrupv, pp))
1623                 __pagevec_lru_add(&lrupv);
1624         }
1625     }
1626
1627     /* If there were useful pages in the page list, make sure all pages
1628      * are in the LRU cache, then schedule the read */
1629     if(page_count) {
1630         pagevec_lru_add(&lrupv);
1631         credp = crref();
1632         code = afs_ReadNoCache(avc, ancr, credp);
1633         crfree(credp);
1634     } else {
1635         /* If there is nothing for the background thread to handle,
1636          * it won't be freeing the things that we never gave it */
1637         osi_Free(iovecp, num_pages * sizeof(struct iovec));
1638         osi_Free(auio, sizeof(uio_t));
1639         osi_Free(ancr, sizeof(struct nocache_read_request));
1640     }
1641     /* we do not flush, release, or unmap pages--that will be 
1642      * done for us by the background thread as each page comes in
1643      * from the fileserver */
1644 out:    
1645     return afs_convert_code(code);
1646 }
1647
1648 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) */
1649 #endif /* defined(AFS_CACHE_BYPASS */
1650
1651 static int
1652 afs_linux_read_cache(struct file *cachefp, struct page *page,
1653                      int chunk, struct pagevec *lrupv,
1654                      struct afs_pagecopy_task *task) {
1655     loff_t offset = page_offset(page);
1656     struct page *newpage, *cachepage;
1657     struct address_space *cachemapping;
1658     int pageindex;
1659     int code = 0;
1660
1661     cachemapping = cachefp->f_dentry->d_inode->i_mapping;
1662     newpage = NULL;
1663     cachepage = NULL;
1664
1665     /* From our offset, we now need to work out which page in the disk
1666      * file it corresponds to. This will be fun ... */
1667     pageindex = (offset - AFS_CHUNKTOBASE(chunk)) >> PAGE_CACHE_SHIFT;
1668
1669     while (cachepage == NULL) {
1670         cachepage = find_get_page(cachemapping, pageindex);
1671         if (!cachepage) {
1672             if (!newpage)
1673                 newpage = page_cache_alloc_cold(cachemapping);
1674             if (!newpage) {
1675                 code = -ENOMEM;
1676                 goto out;
1677             }
1678
1679             code = add_to_page_cache(newpage, cachemapping,
1680                                      pageindex, GFP_KERNEL);
1681             if (code == 0) {
1682                 cachepage = newpage;
1683                 newpage = NULL;
1684
1685                 page_cache_get(cachepage);
1686                 if (!pagevec_add(lrupv, cachepage))
1687                     __pagevec_lru_add_file(lrupv);
1688
1689             } else {
1690                 page_cache_release(newpage);
1691                 newpage = NULL;
1692                 if (code != -EEXIST)
1693                     goto out;
1694             }
1695         } else {
1696             lock_page(cachepage);
1697         }
1698     }
1699
1700     if (!PageUptodate(cachepage)) {
1701         ClearPageError(cachepage);
1702         code = cachemapping->a_ops->readpage(NULL, cachepage);
1703         if (!code && !task) {
1704             wait_on_page_locked(cachepage);
1705         }
1706     } else {
1707         unlock_page(cachepage);
1708     }
1709
1710     if (!code) {
1711         if (PageUptodate(cachepage)) {
1712             copy_highpage(page, cachepage);
1713             flush_dcache_page(page);
1714             SetPageUptodate(page);
1715             UnlockPage(page);
1716         } else if (task) {
1717             afs_pagecopy_queue_page(task, cachepage, page);
1718         } else {
1719             code = -EIO;
1720         }
1721     }
1722
1723     if (code) {
1724         UnlockPage(page);
1725     }
1726
1727 out:
1728     if (cachepage)
1729         page_cache_release(cachepage);
1730
1731     return code;
1732 }
1733
1734 static int inline
1735 afs_linux_readpage_fastpath(struct file *fp, struct page *pp, int *codep)
1736 {
1737     loff_t offset = page_offset(pp);
1738     struct inode *ip = FILE_INODE(fp);
1739     struct vcache *avc = VTOAFS(ip);
1740     struct dcache *tdc;
1741     struct file *cacheFp = NULL;
1742     int code;
1743     int dcLocked = 0;
1744     struct pagevec lrupv;
1745
1746     /* Not a UFS cache, don't do anything */
1747     if (cacheDiskType != AFS_FCACHE_TYPE_UFS)
1748         return 0;
1749
1750     /* Can't do anything if the vcache isn't statd , or if the read
1751      * crosses a chunk boundary.
1752      */
1753     if (!(avc->f.states & CStatd) ||
1754         AFS_CHUNK(offset) != AFS_CHUNK(offset + PAGE_SIZE)) {
1755         return 0;
1756     }
1757
1758     ObtainWriteLock(&avc->lock, 911);
1759
1760     /* XXX - See if hinting actually makes things faster !!! */
1761
1762     /* See if we have a suitable entry already cached */
1763     tdc = avc->dchint;
1764
1765     if (tdc) {
1766         /* We need to lock xdcache, then dcache, to handle situations where
1767          * the hint is on the free list. However, we can't safely do this
1768          * according to the locking hierarchy. So, use a non blocking lock.
1769          */
1770         ObtainReadLock(&afs_xdcache);
1771         dcLocked = ( 0 == NBObtainReadLock(&tdc->lock));
1772
1773         if (dcLocked && (tdc->index != NULLIDX)
1774             && !FidCmp(&tdc->f.fid, &avc->f.fid)
1775             && tdc->f.chunk == AFS_CHUNK(offset)
1776             && !(afs_indexFlags[tdc->index] & (IFFree | IFDiscarded))) {
1777             /* Bonus - the hint was correct */
1778             afs_RefDCache(tdc);
1779         } else {
1780             /* Only destroy the hint if its actually invalid, not if there's
1781              * just been a locking failure */
1782             if (dcLocked) {
1783                 ReleaseReadLock(&tdc->lock);
1784                 avc->dchint = NULL;
1785             }
1786
1787             tdc = NULL;
1788             dcLocked = 0;
1789         }
1790         ReleaseReadLock(&afs_xdcache);
1791     }
1792
1793     /* No hint, or hint is no longer valid - see if we can get something
1794      * directly from the dcache
1795      */
1796     if (!tdc)
1797         tdc = afs_FindDCache(avc, offset);
1798
1799     if (!tdc) {
1800         ReleaseWriteLock(&avc->lock);
1801         return 0;
1802     }
1803
1804     if (!dcLocked)
1805         ObtainReadLock(&tdc->lock);
1806
1807     /* Is the dcache we've been given currently up to date */
1808     if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
1809         (tdc->dflags & DFFetching)) {
1810         ReleaseWriteLock(&avc->lock);
1811         ReleaseReadLock(&tdc->lock);
1812         afs_PutDCache(tdc);
1813         return 0;
1814     }
1815
1816     /* Update our hint for future abuse */
1817     avc->dchint = tdc;
1818
1819     /* Okay, so we've now got a cache file that is up to date */
1820
1821     /* XXX - I suspect we should be locking the inodes before we use them! */
1822     AFS_GUNLOCK();
1823     cacheFp = afs_linux_raw_open(&tdc->f.inode, NULL);
1824     pagevec_init(&lrupv, 0);
1825
1826     code = afs_linux_read_cache(cacheFp, pp, tdc->f.chunk, &lrupv, NULL);
1827
1828     if (pagevec_count(&lrupv))
1829        __pagevec_lru_add_file(&lrupv);
1830
1831     filp_close(cacheFp, NULL);
1832     AFS_GLOCK();
1833
1834     ReleaseReadLock(&tdc->lock);
1835     ReleaseWriteLock(&avc->lock);
1836     afs_PutDCache(tdc);
1837
1838     *codep = code;
1839     return 1;
1840 }
1841
1842 /* afs_linux_readpage
1843  * all reads come through here. A strategy-like read call.
1844  */
1845 static int
1846 afs_linux_readpage(struct file *fp, struct page *pp)
1847 {
1848     afs_int32 code;
1849 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1850     char *address;
1851     afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1852 #else
1853     ulong address = afs_linux_page_address(pp);
1854     afs_offs_t offset = pageoff(pp);
1855 #endif
1856 #if defined(AFS_CACHE_BYPASS)
1857     afs_int32 bypasscache = 0; /* bypass for this read */
1858     struct nocache_read_request *ancr;
1859 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1860     afs_int32 isize;
1861 #endif
1862 #endif
1863     uio_t *auio;
1864     struct iovec *iovecp;
1865     struct inode *ip = FILE_INODE(fp);
1866     afs_int32 cnt = page_count(pp);
1867     struct vcache *avc = VTOAFS(ip);
1868     cred_t *credp;
1869
1870 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1871     AFS_GLOCK();
1872     if (afs_linux_readpage_fastpath(fp, pp, &code)) {
1873         AFS_GUNLOCK();
1874         return code;
1875     }
1876     AFS_GUNLOCK();
1877 #endif
1878
1879     credp = crref();
1880 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1881     address = kmap(pp);
1882     ClearPageError(pp);
1883 #else
1884     atomic_add(1, &pp->count);
1885     set_bit(PG_locked, &pp->flags);     /* other bits? See mm.h */
1886     clear_bit(PG_error, &pp->flags);
1887 #endif
1888 #if defined(AFS_CACHE_BYPASS)
1889 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1890     /* If the page is past the end of the file, skip it */
1891     isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
1892     if(pp->index > isize) {
1893         if(PageLocked(pp))
1894             UnlockPage(pp);
1895         goto done;
1896     }
1897 #endif
1898 #endif
1899     /* if bypasscache, receiver frees, else we do */
1900     auio = osi_Alloc(sizeof(uio_t));
1901     iovecp = osi_Alloc(sizeof(struct iovec));
1902
1903     setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1904               AFS_UIOSYS);
1905
1906 #if defined(AFS_CACHE_BYPASS)
1907     bypasscache = afs_linux_can_bypass(ip);
1908
1909     /* In the new incarnation of selective caching, a file's caching policy
1910      * can change, eg because file size exceeds threshold, etc. */
1911     trydo_cache_transition(avc, credp, bypasscache);
1912         
1913     if(bypasscache) {
1914         if(address)
1915             kunmap(pp);
1916         /* save the page for background map */
1917         auio->uio_iov->iov_base = (void*) pp;
1918         /* the background thread will free this */
1919         ancr = osi_Alloc(sizeof(struct nocache_read_request));
1920         ancr->auio = auio;
1921         ancr->offset = offset;
1922         ancr->length = PAGE_SIZE;
1923
1924         maybe_lock_kernel();
1925         code = afs_ReadNoCache(avc, ancr, credp);
1926         maybe_unlock_kernel();
1927
1928         goto done; /* skips release page, doing it in bg thread */
1929     }
1930 #endif 
1931                   
1932 #ifdef AFS_LINUX24_ENV
1933     maybe_lock_kernel();
1934 #endif
1935     AFS_GLOCK();
1936     AFS_DISCON_LOCK();
1937     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1938                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1939                99999);  /* not a possible code value */
1940
1941     code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1942         
1943     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1944                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1945                code);
1946     AFS_DISCON_UNLOCK();
1947     AFS_GUNLOCK();
1948 #ifdef AFS_LINUX24_ENV
1949     maybe_unlock_kernel();
1950 #endif
1951     if (!code) {
1952         /* XXX valid for no-cache also?  Check last bits of files... :)
1953          * Cognate code goes in afs_NoCacheFetchProc.  */
1954         if (auio->uio_resid)    /* zero remainder of page */
1955              memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1956                     auio->uio_resid);
1957
1958 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1959         flush_dcache_page(pp);
1960         SetPageUptodate(pp);
1961 #else
1962         set_bit(PG_uptodate, &pp->flags);
1963 #endif
1964     } /* !code */
1965
1966 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1967     kunmap(pp);
1968     UnlockPage(pp);
1969 #else
1970     clear_bit(PG_locked, &pp->flags);
1971     wake_up(&pp->wait);
1972     free_page(address);
1973 #endif
1974
1975 #if defined(AFS_CACHE_BYPASS)
1976     /* do not call afs_GetDCache if cache is bypassed */
1977     if(bypasscache)
1978         goto done;
1979 #endif
1980
1981     /* free if not bypassing cache */
1982     osi_Free(auio, sizeof(uio_t));
1983     osi_Free(iovecp, sizeof(struct iovec));
1984
1985     if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1986         struct dcache *tdc;
1987         struct vrequest treq;
1988
1989         AFS_GLOCK();
1990         code = afs_InitReq(&treq, credp);
1991         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1992             tdc = afs_FindDCache(avc, offset);
1993             if (tdc) {
1994                 if (!(tdc->mflags & DFNextStarted))
1995                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1996                     afs_PutDCache(tdc);
1997             }
1998             ReleaseWriteLock(&avc->lock);
1999         }
2000         AFS_GUNLOCK();
2001     }
2002
2003 #if defined(AFS_CACHE_BYPASS)
2004 done:
2005 #endif
2006     crfree(credp);
2007     return afs_convert_code(code);
2008 }
2009
2010 /* Readpages reads a number of pages for a particular file. We use
2011  * this to optimise the reading, by limiting the number of times upon which
2012  * we have to lookup, lock and open vcaches and dcaches
2013  */
2014
2015 static int
2016 afs_linux_readpages(struct file *fp, struct address_space *mapping,
2017                     struct list_head *page_list, unsigned int num_pages)
2018 {
2019     struct inode *inode = mapping->host;
2020     struct vcache *avc = VTOAFS(inode);
2021     struct dcache *tdc;
2022     struct file *cacheFp = NULL;
2023     int code;
2024     unsigned int page_idx;
2025     loff_t offset;
2026     struct pagevec lrupv;
2027     struct afs_pagecopy_task *task;
2028
2029 #if defined(AFS_CACHE_BYPASS)
2030     bypasscache = afs_linux_can_bypass(ip);
2031
2032     /* In the new incarnation of selective caching, a file's caching policy
2033      * can change, eg because file size exceeds threshold, etc. */
2034     trydo_cache_transition(avc, credp, bypasscache);
2035
2036     if (bypasscache)
2037         return afs_linux_cache_bypass_read(ip, mapping, page_list, num_pages);
2038 #endif
2039
2040     AFS_GLOCK();
2041     if ((code = afs_linux_VerifyVCache(avc, NULL))) {
2042         AFS_GUNLOCK();
2043         return code;
2044     }
2045
2046     ObtainWriteLock(&avc->lock, 912);
2047     AFS_GUNLOCK();
2048
2049     task = afs_pagecopy_init_task();
2050
2051     tdc = NULL;
2052     pagevec_init(&lrupv, 0);
2053     for (page_idx = 0; page_idx < num_pages; page_idx++) {
2054         struct page *page = list_entry(page_list->prev, struct page, lru);
2055         list_del(&page->lru);
2056         offset = page_offset(page);
2057
2058         if (tdc && tdc->f.chunk != AFS_CHUNK(offset)) {
2059             AFS_GLOCK();
2060             ReleaseReadLock(&tdc->lock);
2061             afs_PutDCache(tdc);
2062             AFS_GUNLOCK();
2063             tdc = NULL;
2064             if (cacheFp)
2065                 filp_close(cacheFp, NULL);
2066         }
2067
2068         if (!tdc) {
2069             AFS_GLOCK();
2070             if ((tdc = afs_FindDCache(avc, offset))) {
2071                 ObtainReadLock(&tdc->lock);
2072                 if (!hsame(avc->f.m.DataVersion, tdc->f.versionNo) ||
2073                     (tdc->dflags & DFFetching)) {
2074                     ReleaseReadLock(&tdc->lock);
2075                     afs_PutDCache(tdc);
2076                     tdc = NULL;
2077                 }
2078             }
2079             AFS_GUNLOCK();
2080             if (tdc)
2081                 cacheFp = afs_linux_raw_open(&tdc->f.inode, NULL);
2082         }
2083
2084         if (tdc && !add_to_page_cache(page, mapping, page->index,
2085                                       GFP_KERNEL)) {
2086             page_cache_get(page);
2087             if (!pagevec_add(&lrupv, page))
2088                 __pagevec_lru_add_file(&lrupv);
2089
2090             afs_linux_read_cache(cacheFp, page, tdc->f.chunk, &lrupv, task);
2091         }
2092         page_cache_release(page);
2093     }
2094     if (pagevec_count(&lrupv))
2095        __pagevec_lru_add_file(&lrupv);
2096
2097     if (tdc)
2098         filp_close(cacheFp, NULL);
2099
2100     afs_pagecopy_put_task(task);
2101
2102     AFS_GLOCK();
2103     if (tdc) {
2104         ReleaseReadLock(&tdc->lock);
2105         afs_PutDCache(tdc);
2106     }
2107
2108     ReleaseWriteLock(&avc->lock);
2109     AFS_GUNLOCK();
2110     return 0;
2111 }
2112
2113 #if defined(AFS_LINUX24_ENV)
2114 static int
2115 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
2116                          unsigned long offset, unsigned int count)
2117 {
2118     struct vcache *vcp = VTOAFS(ip);
2119     char *buffer;
2120     afs_offs_t base;
2121     int code = 0;
2122     cred_t *credp;
2123     uio_t tuio;
2124     struct iovec iovec;
2125     int f_flags = 0;
2126
2127     buffer = kmap(pp) + offset;
2128     base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT)  + offset;
2129
2130     credp = crref();
2131     maybe_lock_kernel();
2132     AFS_GLOCK();
2133     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2134                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2135                ICL_TYPE_INT32, 99999);
2136
2137     ObtainWriteLock(&vcp->lock, 532);
2138     if (vcp->f.states & CPageWrite) {
2139         ReleaseWriteLock(&vcp->lock);
2140         AFS_GUNLOCK();
2141         maybe_unlock_kernel();
2142         crfree(credp);
2143         kunmap(pp);
2144         /* should mark it dirty? */
2145         return(0); 
2146     }
2147     vcp->f.states |= CPageWrite;
2148     ReleaseWriteLock(&vcp->lock);
2149
2150     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
2151
2152     code = afs_write(vcp, &tuio, f_flags, credp, 0);
2153
2154     i_size_write(ip, vcp->f.m.Length);
2155     ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2156
2157     ObtainWriteLock(&vcp->lock, 533);
2158     if (!code) {
2159         struct vrequest treq;
2160
2161         if (!afs_InitReq(&treq, credp))
2162             code = afs_DoPartialWrite(vcp, &treq);
2163     }
2164     code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2165
2166     vcp->f.states &= ~CPageWrite;
2167     ReleaseWriteLock(&vcp->lock);
2168
2169     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2170                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2171                ICL_TYPE_INT32, code);
2172
2173     AFS_GUNLOCK();
2174     maybe_unlock_kernel();
2175     crfree(credp);
2176     kunmap(pp);
2177
2178     return code;
2179 }
2180
2181
2182 static int
2183 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
2184 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
2185 #else
2186 afs_linux_writepage(struct page *pp)
2187 #endif
2188 {
2189     struct address_space *mapping = pp->mapping;
2190     struct inode *inode;
2191     unsigned long end_index;
2192     unsigned offset = PAGE_CACHE_SIZE;
2193     long status;
2194
2195     if (PageLaunder(pp)) {
2196         return(fail_writepage(pp));
2197     }
2198
2199     inode = (struct inode *)mapping->host;
2200     end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
2201
2202     /* easy case */
2203     if (pp->index < end_index)
2204         goto do_it;
2205     /* things got complicated... */
2206     offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
2207     /* OK, are we completely out? */
2208     if (pp->index >= end_index + 1 || !offset)
2209         return -EIO;
2210   do_it:
2211     status = afs_linux_writepage_sync(inode, pp, 0, offset);
2212     SetPageUptodate(pp);
2213     UnlockPage(pp);
2214     if (status == offset)
2215         return 0;
2216     else
2217         return status;
2218 }
2219 #else
2220 /* afs_linux_updatepage
2221  * What one would have thought was writepage - write dirty page to file.
2222  * Called from generic_file_write. buffer is still in user space. pagep
2223  * has been filled in with old data if we're updating less than a page.
2224  */
2225 static int
2226 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
2227                      unsigned int count, int sync)
2228 {
2229     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
2230     u8 *page_addr = (u8 *) afs_linux_page_address(pp);
2231     int code = 0;
2232     cred_t *credp;
2233     uio_t tuio;
2234     struct iovec iovec;
2235
2236     set_bit(PG_locked, &pp->flags);
2237
2238     credp = crref();
2239     AFS_GLOCK();
2240     AFS_DISCON_LOCK();
2241     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2242                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2243                ICL_TYPE_INT32, 99999);
2244     setup_uio(&tuio, &iovec, page_addr + offset,
2245               (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
2246               AFS_UIOSYS);
2247
2248     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
2249
2250     i_size_write(ip, vcp->f.m.Length);
2251     ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
2252
2253     if (!code) {
2254         struct vrequest treq;
2255
2256         ObtainWriteLock(&vcp->lock, 533);
2257         vcp->f.m.Date = osi_Time();   /* set modification time */
2258         if (!afs_InitReq(&treq, credp))
2259             code = afs_DoPartialWrite(vcp, &treq);
2260         ReleaseWriteLock(&vcp->lock);
2261     }
2262
2263     code = code ? afs_convert_code(code) : count - tuio.uio_resid;
2264     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
2265                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
2266                ICL_TYPE_INT32, code);
2267
2268     AFS_DISCON_UNLOCK();
2269     AFS_GUNLOCK();
2270     crfree(credp);
2271
2272     clear_bit(PG_locked, &pp->flags);
2273     return code;
2274 }
2275 #endif
2276
2277 /* afs_linux_permission
2278  * Check access rights - returns error if can't check or permission denied.
2279  */
2280 static int
2281 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
2282 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
2283 #else
2284 afs_linux_permission(struct inode *ip, int mode)
2285 #endif
2286 {
2287     int code;
2288     cred_t *credp = crref();
2289     int tmp = 0;
2290
2291     AFS_GLOCK();
2292     if (mode & MAY_EXEC)
2293         tmp |= VEXEC;
2294     if (mode & MAY_READ)
2295         tmp |= VREAD;
2296     if (mode & MAY_WRITE)
2297         tmp |= VWRITE;
2298     code = afs_access(VTOAFS(ip), tmp, credp);
2299
2300     AFS_GUNLOCK();
2301     crfree(credp);
2302     return afs_convert_code(code);
2303 }
2304
2305 #if defined(AFS_LINUX24_ENV) && !defined(HAVE_WRITE_BEGIN)
2306 static int
2307 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
2308                        unsigned to)
2309 {
2310     int code;
2311
2312     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2313                                     offset, to - offset);
2314     kunmap(page);
2315
2316     return code;
2317 }
2318
2319 static int
2320 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
2321                         unsigned to)
2322 {
2323 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
2324    call kmap directly instead of relying on us to do it */
2325     kmap(page);
2326     return 0;
2327 }
2328 #endif
2329
2330 #if defined(HAVE_WRITE_BEGIN)
2331 static int
2332 afs_linux_write_end(struct file *file, struct address_space *mapping,
2333                                 loff_t pos, unsigned len, unsigned copied,
2334                                 struct page *page, void *fsdata)
2335 {
2336     int code;
2337     unsigned from = pos & (PAGE_CACHE_SIZE - 1);
2338
2339     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
2340                                     from, copied);
2341     unlock_page(page);
2342     page_cache_release(page);
2343     return code;
2344 }
2345
2346 static int
2347 afs_linux_write_begin(struct file *file, struct address_space *mapping,
2348                                 loff_t pos, unsigned len, unsigned flags,
2349                                 struct page **pagep, void **fsdata)
2350 {
2351     struct page *page;
2352     pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2353 #if defined(HAVE_GRAB_CACHE_PAGE_WRITE_BEGIN)
2354     page = grab_cache_page_write_begin(mapping, index, flags);
2355 #else
2356     page = __grab_cache_page(mapping, index);
2357 #endif
2358     *pagep = page;
2359
2360     return 0;
2361 }
2362 #endif
2363
2364
2365 static struct inode_operations afs_file_iops = {
2366 #if defined(AFS_LINUX24_ENV)
2367   .permission =         afs_linux_permission,
2368   .revalidate =         afs_linux_revalidate,
2369   .setattr =            afs_notify_change,
2370 #else
2371   .default_file_ops =   &afs_file_fops,
2372   .readpage =           afs_linux_readpage,  
2373   .revalidate =         afs_linux_revalidate,
2374   .updatepage =         afs_linux_updatepage,
2375 #endif
2376 };
2377
2378 #if defined(AFS_LINUX24_ENV)
2379 static struct address_space_operations afs_file_aops = {
2380   .readpage =           afs_linux_readpage,
2381   .readpages =          afs_linux_readpages,
2382   .writepage =          afs_linux_writepage,
2383 #if defined (HAVE_WRITE_BEGIN)
2384   .write_begin =        afs_linux_write_begin,
2385   .write_end =          afs_linux_write_end,
2386 #else
2387   .commit_write =       afs_linux_commit_write,
2388   .prepare_write =      afs_linux_prepare_write,
2389 #endif
2390 };
2391 #endif
2392
2393
2394 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2395  * by what sort of operation is allowed.....
2396  */
2397
2398 static struct inode_operations afs_dir_iops = {
2399 #if !defined(AFS_LINUX24_ENV)
2400   .default_file_ops =   &afs_dir_fops,
2401 #else
2402   .setattr =            afs_notify_change,
2403 #endif
2404   .create =             afs_linux_create,
2405   .lookup =             afs_linux_lookup,
2406   .link =               afs_linux_link,
2407   .unlink =             afs_linux_unlink,
2408   .symlink =            afs_linux_symlink,
2409   .mkdir =              afs_linux_mkdir,
2410   .rmdir =              afs_linux_rmdir,
2411   .rename =             afs_linux_rename,
2412   .revalidate =         afs_linux_revalidate,
2413   .permission =         afs_linux_permission,
2414 };
2415
2416 /* We really need a separate symlink set of ops, since do_follow_link()
2417  * determines if it _is_ a link by checking if the follow_link op is set.
2418  */
2419 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2420 static int
2421 afs_symlink_filler(struct file *file, struct page *page)
2422 {
2423     struct inode *ip = (struct inode *)page->mapping->host;
2424     char *p = (char *)kmap(page);
2425     int code;
2426
2427     maybe_lock_kernel();
2428     AFS_GLOCK();
2429     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2430     AFS_GUNLOCK();
2431
2432     if (code < 0)
2433         goto fail;
2434     p[code] = '\0';             /* null terminate? */
2435     maybe_unlock_kernel();
2436
2437     SetPageUptodate(page);
2438     kunmap(page);
2439     UnlockPage(page);
2440     return 0;
2441
2442   fail:
2443     maybe_unlock_kernel();
2444
2445     SetPageError(page);
2446     kunmap(page);
2447     UnlockPage(page);
2448     return code;
2449 }
2450
2451 static struct address_space_operations afs_symlink_aops = {
2452   .readpage =   afs_symlink_filler
2453 };
2454 #endif  /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2455
2456 static struct inode_operations afs_symlink_iops = {
2457 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2458   .readlink =           page_readlink,
2459 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2460   .follow_link =        page_follow_link,
2461 #else
2462   .follow_link =        page_follow_link_light,
2463   .put_link =           page_put_link,
2464 #endif
2465 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2466   .readlink =           afs_linux_readlink,
2467   .follow_link =        afs_linux_follow_link,
2468 #if !defined(AFS_LINUX24_ENV)
2469   .permission =         afs_linux_permission,
2470   .revalidate =         afs_linux_revalidate,
2471 #endif
2472 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2473 #if defined(AFS_LINUX24_ENV)
2474   .setattr =            afs_notify_change,
2475 #endif
2476 };
2477
2478 void
2479 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2480 {
2481         
2482     if (vattr)
2483         vattr2inode(ip, vattr);
2484
2485 /* Reset ops if symlink or directory. */
2486     if (S_ISREG(ip->i_mode)) {
2487         ip->i_op = &afs_file_iops;
2488 #if defined(AFS_LINUX24_ENV)
2489         ip->i_fop = &afs_file_fops;
2490         ip->i_data.a_ops = &afs_file_aops;
2491 #endif
2492
2493     } else if (S_ISDIR(ip->i_mode)) {
2494         ip->i_op = &afs_dir_iops;
2495 #if defined(AFS_LINUX24_ENV)
2496         ip->i_fop = &afs_dir_fops;
2497 #endif
2498
2499     } else if (S_ISLNK(ip->i_mode)) {
2500         ip->i_op = &afs_symlink_iops;
2501 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2502         ip->i_data.a_ops = &afs_symlink_aops;
2503         ip->i_mapping = &ip->i_data;
2504 #endif
2505     }
2506
2507 }