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