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