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