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