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