LINUX24: explicit dir buffers fallout
[openafs.git] / src / afs / LINUX24 / 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 "h/mm.h"
29 #ifdef HAVE_MM_INLINE_H
30 #include "h/mm_inline.h"
31 #endif
32 #include "h/pagemap.h"
33 #if defined(AFS_LINUX24_ENV)
34 #include "h/smp_lock.h"
35 #endif
36 #include "afs/lock.h"
37 #include "afs/afs_bypasscache.h"
38
39 #ifdef pgoff2loff
40 #define pageoff(pp) pgoff2loff((pp)->index)
41 #else
42 #define pageoff(pp) pp->offset
43 #endif
44
45 #ifndef MAX_ERRNO
46 #define MAX_ERRNO 1000L
47 #endif
48
49 extern struct vcache *afs_globalVp;
50 #if defined(AFS_LINUX24_ENV)
51 /* Some uses of BKL are perhaps not needed for bypass or memcache--
52  * why don't we try it out? */
53 extern struct afs_cacheOps afs_UfsCacheOps;
54 #define maybe_lock_kernel()                     \
55     do {                                               \
56         if(afs_cacheType == &afs_UfsCacheOps)          \
57             lock_kernel();                             \
58     } while(0);
59
60
61 #define maybe_unlock_kernel()                   \
62     do {                                               \
63         if(afs_cacheType == &afs_UfsCacheOps)          \
64             unlock_kernel();                           \
65     } while(0);
66 #endif /* AFS_LINUX24_ENV */
67
68
69 /* This function converts a positive error code from AFS into a negative
70  * code suitable for passing into the Linux VFS layer. It checks that the
71  * error code is within the permissable bounds for the ERR_PTR mechanism.
72  *
73  * _All_ error codes which come from the AFS layer should be passed through
74  * this function before being returned to the kernel.
75  */
76
77 static inline int afs_convert_code(int code) {
78     if ((code >= 0) && (code <= MAX_ERRNO))
79         return -code;
80     else
81         return -EIO;
82 }
83
84 /* Linux doesn't require a credp for many functions, and crref is an expensive
85  * operation. This helper function avoids obtaining it for VerifyVCache calls
86  */
87
88 static inline int afs_linux_VerifyVCache(struct vcache *avc, cred_t **retcred) {
89     cred_t *credp = NULL;
90     struct vrequest treq;
91     int code;
92
93     if (avc->f.states & CStatd) {
94         if (retcred)
95             *retcred = NULL;
96         return 0;
97     }
98
99     credp = crref();
100
101     code = afs_InitReq(&treq, credp);
102     if (code == 0)
103         code = afs_VerifyVCache2(avc, &treq);
104
105     if (retcred != NULL)
106         *retcred = credp;
107     else
108         crfree(credp);
109
110     return afs_convert_code(code);
111 }
112
113 static ssize_t
114 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
115 {
116     ssize_t code = 0;
117     struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
118 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
119     afs_size_t isize, offindex;
120 #endif
121
122     AFS_GLOCK();
123     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
124                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
125                99999);
126     code = afs_linux_VerifyVCache(vcp, NULL);
127
128     if (code == 0) {
129 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
130         isize = (i_size_read(fp->f_mapping->host) - 1) >> PAGE_CACHE_SHIFT;
131         offindex = *offp >> PAGE_CACHE_SHIFT;
132         if(offindex > isize) {
133             code=0;
134             goto done;
135         }
136 #endif
137         /* Linux's FlushPages implementation doesn't ever use credp,
138          * so we optimise by not using it */
139         osi_FlushPages(vcp, NULL);      /* ensure stale pages are gone */
140         AFS_GUNLOCK();
141 #ifdef HAVE_LINUX_DO_SYNC_READ
142         code = do_sync_read(fp, buf, count, offp);
143 #else
144         code = generic_file_read(fp, buf, count, offp);
145 #endif
146         AFS_GLOCK();
147     }
148
149     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
150                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
151                code);
152 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
153 done:
154 #endif
155     AFS_GUNLOCK();
156     return code;
157 }
158
159
160 /* Now we have integrated VM for writes as well as reads. generic_file_write
161  * also takes care of re-positioning the pointer if file is open in append
162  * mode. Call fake open/close to ensure we do writes of core dumps.
163  */
164 static ssize_t
165 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
166 {
167     ssize_t code = 0;
168     struct vcache *vcp = VTOAFS(fp->f_dentry->d_inode);
169     cred_t *credp;
170
171     AFS_GLOCK();
172
173     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
174                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
175                (fp->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 #ifdef DO_SYNC_READ
185             code = do_sync_write(fp, buf, count, offp);
186 #else
187             code = generic_file_write(fp, buf, count, offp);
188 #endif
189             AFS_GLOCK();
190     }
191
192     ObtainWriteLock(&vcp->lock, 530);
193
194     if (vcp->execsOrWriters == 1 && !credp)
195       credp = crref();
196
197     afs_FakeClose(vcp, credp);
198     ReleaseWriteLock(&vcp->lock);
199
200     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
201                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
202                code);
203
204     if (credp)
205       crfree(credp);
206     AFS_GUNLOCK();
207     return code;
208 }
209
210 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
211
212 /* This is a complete rewrite of afs_readdir, since we can make use of
213  * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
214  * handling and use of bulkstats will need to be reflected here as well.
215  */
216 static int
217 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
218 {
219     struct vcache *avc = VTOAFS(FILE_INODE(fp));
220     struct vrequest treq;
221     struct dcache *tdc;
222     int code;
223     int offset;
224     int dirpos;
225     struct DirEntry *de;
226     struct DirBuffer entry;
227     ino_t ino;
228     int len;
229     afs_size_t origOffset, tlen;
230     cred_t *credp = crref();
231     struct afs_fakestat_state fakestat;
232
233     AFS_GLOCK();
234     AFS_STATCNT(afs_readdir);
235
236     code = afs_convert_code(afs_InitReq(&treq, credp));
237     crfree(credp);
238     if (code)
239         goto out1;
240
241     afs_InitFakeStat(&fakestat);
242     code = afs_convert_code(afs_EvalFakeStat(&avc, &fakestat, &treq));
243     if (code)
244         goto out;
245
246     /* update the cache entry */
247   tagain:
248     code = afs_convert_code(afs_VerifyVCache2(avc, &treq));
249     if (code)
250         goto out;
251
252     /* get a reference to the entire directory */
253     tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
254     len = tlen;
255     if (!tdc) {
256         code = -ENOENT;
257         goto out;
258     }
259     ObtainSharedLock(&avc->lock, 810);
260     UpgradeSToWLock(&avc->lock, 811);
261     ObtainReadLock(&tdc->lock);
262     /*
263      * Make sure that the data in the cache is current. There are two
264      * cases we need to worry about:
265      * 1. The cache data is being fetched by another process.
266      * 2. The cache data is no longer valid
267      */
268     while ((avc->f.states & CStatd)
269            && (tdc->dflags & DFFetching)
270            && hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
271         ReleaseReadLock(&tdc->lock);
272         ReleaseSharedLock(&avc->lock);
273         afs_osi_Sleep(&tdc->validPos);
274         ObtainSharedLock(&avc->lock, 812);
275         ObtainReadLock(&tdc->lock);
276     }
277     if (!(avc->f.states & CStatd)
278         || !hsame(avc->f.m.DataVersion, tdc->f.versionNo)) {
279         ReleaseReadLock(&tdc->lock);
280         ReleaseSharedLock(&avc->lock);
281         afs_PutDCache(tdc);
282         goto tagain;
283     }
284
285     /* Set the readdir-in-progress flag, and downgrade the lock
286      * to shared so others will be able to acquire a read lock.
287      */
288     avc->f.states |= CReadDir;
289     avc->dcreaddir = tdc;
290     avc->readdir_pid = MyPidxx2Pid(MyPidxx);
291     ConvertWToSLock(&avc->lock);
292
293     /* Fill in until we get an error or we're done. This implementation
294      * takes an offset in units of blobs, rather than bytes.
295      */
296     code = 0;
297     offset = (int) fp->f_pos;
298     while (1) {
299         dirpos = BlobScan(tdc, offset);
300         if (!dirpos)
301             break;
302
303         code = afs_dir_GetBlob(tdc, dirpos, &entry);
304         if (code)
305             break;
306         de = (struct DirEntry *)entry.data;
307
308         ino = afs_calc_inum(avc->f.fid.Cell, avc->f.fid.Fid.Volume,
309                             ntohl(de->fid.vnode));
310
311         if (de->name)
312             len = strlen(de->name);
313         else {
314             printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n", 
315                    (unsigned long)&tdc->f.inode, dirpos);
316             DRelease(&entry, 0);
317             ReleaseSharedLock(&avc->lock);
318             afs_PutDCache(tdc);
319             code = -ENOENT;
320             goto out;
321         }
322
323         /* filldir returns -EINVAL when the buffer is full. */
324 #if (defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE)
325         {
326             unsigned int type = DT_UNKNOWN;
327             struct VenusFid afid;
328             struct vcache *tvc;
329             int vtype;
330             afid.Cell = avc->f.fid.Cell;
331             afid.Fid.Volume = avc->f.fid.Fid.Volume;
332             afid.Fid.Vnode = ntohl(de->fid.vnode);
333             afid.Fid.Unique = ntohl(de->fid.vunique);
334             if ((avc->f.states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
335                 type = DT_DIR;
336             } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
337                 if (tvc->mvstat) {
338                     type = DT_DIR;
339                 } else if (((tvc->f.states) & (CStatd | CTruth))) {
340                     /* CTruth will be set if the object has
341                      *ever* been statd */
342                     vtype = vType(tvc);
343                     if (vtype == VDIR)
344                         type = DT_DIR;
345                     else if (vtype == VREG)
346                         type = DT_REG;
347                     /* Don't do this until we're sure it can't be a mtpt */
348                     /* else if (vtype == VLNK)
349                      * type=DT_LNK; */
350                     /* what other types does AFS support? */
351                 }
352                 /* clean up from afs_FindVCache */
353                 afs_PutVCache(tvc);
354             }
355             /* 
356              * If this is NFS readdirplus, then the filler is going to
357              * call getattr on this inode, which will deadlock if we're
358              * holding the GLOCK.
359              */
360             AFS_GUNLOCK();
361             code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
362             AFS_GLOCK();
363         }
364 #else
365         code = (*filldir) (dirbuf, de->name, len, offset, ino);
366 #endif
367         DRelease(&entry, 0);
368         if (code)
369             break;
370         offset = dirpos + 1 + ((len + 16) >> 5);
371     }
372     /* If filldir didn't fill in the last one this is still pointing to that
373      * last attempt.
374      */
375     fp->f_pos = (loff_t) offset;
376
377     ReleaseReadLock(&tdc->lock);
378     afs_PutDCache(tdc);
379     UpgradeSToWLock(&avc->lock, 813);
380     avc->f.states &= ~CReadDir;
381     avc->dcreaddir = 0;
382     avc->readdir_pid = 0;
383     ReleaseSharedLock(&avc->lock);
384     code = 0;
385
386 out:
387     afs_PutFakeStat(&fakestat);
388 out1:
389     AFS_GUNLOCK();
390     return code;
391 }
392
393
394 /* in afs_pioctl.c */
395 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
396                       unsigned long arg);
397
398 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
399 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
400                                unsigned long arg) {
401     return afs_xioctl(FILE_INODE(fp), fp, com, arg);
402
403 }
404 #endif
405
406
407 static int
408 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
409 {
410     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
411     int code;
412
413     AFS_GLOCK();
414 #if defined(AFS_LINUX24_ENV)
415     afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
416                ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
417                vmap->vm_end - vmap->vm_start);
418 #else
419     afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
420                ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
421                vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
422                vmap->vm_offset);
423 #endif
424
425     /* get a validated vcache entry */
426     code = afs_linux_VerifyVCache(vcp, NULL);
427
428     /* Linux's Flushpage implementation doesn't use credp, so optimise
429      * our code to not need to crref() it */
430     osi_FlushPages(vcp, NULL); /* ensure stale pages are gone */
431     AFS_GUNLOCK();
432     code = generic_file_mmap(fp, vmap);
433     AFS_GLOCK();
434     if (!code)
435         vcp->f.states |= CMAPPED;
436
437     AFS_GUNLOCK();
438     return code;
439 }
440
441 static int
442 afs_linux_open(struct inode *ip, struct file *fp)
443 {
444     struct vcache *vcp = VTOAFS(ip);
445     cred_t *credp = crref();
446     int code;
447
448 #ifdef AFS_LINUX24_ENV
449     maybe_lock_kernel();
450 #endif
451     AFS_GLOCK();
452     code = afs_open(&vcp, fp->f_flags, credp);
453     AFS_GUNLOCK();
454 #ifdef AFS_LINUX24_ENV
455     maybe_unlock_kernel();
456 #endif
457
458     crfree(credp);
459     return afs_convert_code(code);
460 }
461
462 static int
463 afs_linux_release(struct inode *ip, struct file *fp)
464 {
465     struct vcache *vcp = VTOAFS(ip);
466     cred_t *credp = crref();
467     int code = 0;
468
469 #ifdef AFS_LINUX24_ENV
470     maybe_lock_kernel();
471 #endif
472     AFS_GLOCK();
473     code = afs_close(vcp, fp->f_flags, credp);
474     AFS_GUNLOCK();
475 #ifdef AFS_LINUX24_ENV
476     maybe_unlock_kernel();
477 #endif
478
479     crfree(credp);
480     return afs_convert_code(code);
481 }
482
483 static int
484 #if defined(AFS_LINUX24_ENV)
485 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
486 #else
487 afs_linux_fsync(struct file *fp, struct dentry *dp)
488 #endif
489 {
490     int code;
491     struct inode *ip = FILE_INODE(fp);
492     cred_t *credp = crref();
493
494 #ifdef AFS_LINUX24_ENV
495     maybe_lock_kernel();
496 #endif
497     AFS_GLOCK();
498     code = afs_fsync(VTOAFS(ip), credp);
499     AFS_GUNLOCK();
500 #ifdef AFS_LINUX24_ENV
501     maybe_unlock_kernel();
502 #endif
503     crfree(credp);
504     return afs_convert_code(code);
505
506 }
507
508
509 static int
510 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
511 {
512     int code = 0;
513     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
514     cred_t *credp = crref();
515     struct AFS_FLOCK flock;
516 #if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
517     struct file_lock conflict;
518 #elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
519     struct file_lock *conflict;
520 #endif
521     
522     /* Convert to a lock format afs_lockctl understands. */
523     memset(&flock, 0, sizeof(flock));
524     flock.l_type = flp->fl_type;
525     flock.l_pid = flp->fl_pid;
526     flock.l_whence = 0;
527     flock.l_start = flp->fl_start;
528     if (flp->fl_end == OFFSET_MAX)
529         flock.l_len = 0; /* Lock to end of file */
530     else
531         flock.l_len = flp->fl_end - flp->fl_start + 1;
532
533     /* Safe because there are no large files, yet */
534 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
535     if (cmd == F_GETLK64)
536         cmd = F_GETLK;
537     else if (cmd == F_SETLK64)
538         cmd = F_SETLK;
539     else if (cmd == F_SETLKW64)
540         cmd = F_SETLKW;
541 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
542
543     AFS_GLOCK();
544     code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
545     AFS_GUNLOCK();
546
547 #ifdef AFS_LINUX24_ENV
548     if ((code == 0 || flp->fl_type == F_UNLCK) && 
549         (cmd == F_SETLK || cmd == F_SETLKW)) {
550 # ifdef POSIX_LOCK_FILE_WAIT_ARG
551         code = posix_lock_file(fp, flp, 0);
552 # else
553         flp->fl_flags &=~ FL_SLEEP;
554         code = posix_lock_file(fp, flp);
555 # endif 
556         if (code && flp->fl_type != F_UNLCK) {
557             struct AFS_FLOCK flock2;
558             flock2 = flock;
559             flock2.l_type = F_UNLCK;
560             AFS_GLOCK();
561             afs_lockctl(vcp, &flock2, F_SETLK, credp);
562             AFS_GUNLOCK();
563         }
564     }
565     /* If lockctl says there are no conflicting locks, then also check with the
566      * kernel, as lockctl knows nothing about byte range locks
567      */
568     if (code == 0 && cmd == F_GETLK && flock.l_type == F_UNLCK) {
569 # if defined(POSIX_TEST_LOCK_CONFLICT_ARG)
570         if (posix_test_lock(fp, flp, &conflict)) {
571             locks_copy_lock(flp, &conflict);
572             flp->fl_type = F_UNLCK;
573             crfree(credp);
574             return 0;
575         }
576 # elif defined(POSIX_TEST_LOCK_RETURNS_CONFLICT)
577         if ((conflict = posix_test_lock(fp, flp))) {
578             locks_copy_lock(flp, conflict);
579             flp->fl_type = F_UNLCK;
580             crfree(credp);
581             return 0;
582         }
583 # else
584         posix_test_lock(fp, flp);
585         /* If we found a lock in the kernel's structure, return it */
586         if (flp->fl_type != F_UNLCK) {
587             crfree(credp);
588             return 0;
589         }
590 # endif
591     }
592     
593 #endif
594     /* Convert flock back to Linux's file_lock */
595     flp->fl_type = flock.l_type;
596     flp->fl_pid = flock.l_pid;
597     flp->fl_start = flock.l_start;
598     if (flock.l_len == 0)
599         flp->fl_end = OFFSET_MAX; /* Lock to end of file */
600     else
601         flp->fl_end = flock.l_start + flock.l_len - 1;
602
603     crfree(credp);
604     return code;
605 }
606
607 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
608 static int
609 afs_linux_flock(struct file *fp, int cmd, struct file_lock *flp) {
610     int code = 0;
611     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
612     cred_t *credp = crref();
613     struct AFS_FLOCK flock;
614     /* Convert to a lock format afs_lockctl understands. */
615     memset(&flock, 0, sizeof(flock));
616     flock.l_type = flp->fl_type;
617     flock.l_pid = flp->fl_pid;
618     flock.l_whence = 0;
619     flock.l_start = 0;
620     flock.l_len = 0;
621
622     /* Safe because there are no large files, yet */
623 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
624     if (cmd == F_GETLK64)
625         cmd = F_GETLK;
626     else if (cmd == F_SETLK64)
627         cmd = F_SETLK;
628     else if (cmd == F_SETLKW64)
629         cmd = F_SETLKW;
630 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
631
632     AFS_GLOCK();
633     code = afs_convert_code(afs_lockctl(vcp, &flock, cmd, credp));
634     AFS_GUNLOCK();
635
636     if ((code == 0 || flp->fl_type == F_UNLCK) && 
637         (cmd == F_SETLK || cmd == F_SETLKW)) {
638         flp->fl_flags &=~ FL_SLEEP;
639         code = flock_lock_file_wait(fp, flp);
640         if (code && flp->fl_type != F_UNLCK) {
641             struct AFS_FLOCK flock2;
642             flock2 = flock;
643             flock2.l_type = F_UNLCK;
644             AFS_GLOCK();
645             afs_lockctl(vcp, &flock2, F_SETLK, credp);
646             AFS_GUNLOCK();
647         }
648     }
649     /* Convert flock back to Linux's file_lock */
650     flp->fl_type = flock.l_type;
651     flp->fl_pid = flock.l_pid;
652
653     crfree(credp);
654     return code;
655 }
656 #endif
657
658 /* afs_linux_flush
659  * essentially the same as afs_fsync() but we need to get the return
660  * code for the sys_close() here, not afs_linux_release(), so call
661  * afs_StoreAllSegments() with AFS_LASTSTORE
662  */
663 static int
664 #if defined(FOP_FLUSH_TAKES_FL_OWNER_T)
665 afs_linux_flush(struct file *fp, fl_owner_t id)
666 #else
667 afs_linux_flush(struct file *fp)
668 #endif
669 {
670     struct vrequest treq;
671     struct vcache *vcp;
672     cred_t *credp;
673     int code;
674     int bypasscache;
675
676     AFS_GLOCK();
677
678     if ((fp->f_flags & O_ACCMODE) == O_RDONLY) { /* readers dont flush */
679         AFS_GUNLOCK();
680         return 0;
681     }
682
683     AFS_DISCON_LOCK();
684
685     credp = crref();
686     vcp = VTOAFS(FILE_INODE(fp));
687
688     code = afs_InitReq(&treq, credp);
689     if (code)
690         goto out;
691         /* If caching is bypassed for this file, or globally, just return 0 */
692         if(cache_bypass_strategy == ALWAYS_BYPASS_CACHE)
693                 bypasscache = 1;
694         else {
695                 ObtainReadLock(&vcp->lock);
696                 if(vcp->cachingStates & FCSBypass)
697                         bypasscache = 1;
698                 ReleaseReadLock(&vcp->lock);
699         }
700         if(bypasscache) {
701             /* future proof: don't rely on 0 return from afs_InitReq */
702             code = 0; goto out;
703         }
704
705     ObtainSharedLock(&vcp->lock, 535);
706     if ((vcp->execsOrWriters > 0) && (file_count(fp) == 1)) {
707         UpgradeSToWLock(&vcp->lock, 536);
708         if (!AFS_IS_DISCONNECTED) {
709                 code = afs_StoreAllSegments(vcp,
710                                 &treq,
711                                 AFS_SYNC | AFS_LASTSTORE);
712         } else {
713                 afs_DisconAddDirty(vcp, VDisconWriteOsiFlush, 1);
714         }
715         ConvertWToSLock(&vcp->lock);
716     }
717     code = afs_CheckCode(code, &treq, 54);
718     ReleaseSharedLock(&vcp->lock);
719
720 out:
721     AFS_DISCON_UNLOCK();
722     AFS_GUNLOCK();
723
724     crfree(credp);
725     return afs_convert_code(code);
726 }
727
728 #if !defined(AFS_LINUX24_ENV)
729 /* Not allowed to directly read a directory. */
730 ssize_t
731 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
732 {
733     return -EISDIR;
734 }
735 #endif
736
737
738
739 struct file_operations afs_dir_fops = {
740 #if !defined(AFS_LINUX24_ENV)
741   .read =       afs_linux_dir_read,
742   .lock =       afs_linux_lock,
743   .fsync =      afs_linux_fsync,
744 #else
745   .read =       generic_read_dir,
746 #endif
747   .readdir =    afs_linux_readdir,
748 #ifdef HAVE_UNLOCKED_IOCTL
749   .unlocked_ioctl = afs_unlocked_xioctl,
750 #else
751   .ioctl =      afs_xioctl,
752 #endif
753 #ifdef HAVE_COMPAT_IOCTL
754   .compat_ioctl = afs_unlocked_xioctl,
755 #endif
756   .open =       afs_linux_open,
757   .release =    afs_linux_release,
758 };
759
760 struct file_operations afs_file_fops = {
761   .read =       afs_linux_read,
762   .write =      afs_linux_write,
763 #ifdef HAVE_LINUX_GENERIC_FILE_AIO_READ
764   .aio_read =   generic_file_aio_read,
765   .aio_write =  generic_file_aio_write,
766 #endif
767 #ifdef HAVE_UNLOCKED_IOCTL
768   .unlocked_ioctl = afs_unlocked_xioctl,
769 #else
770   .ioctl =      afs_xioctl,
771 #endif
772 #ifdef HAVE_COMPAT_IOCTL
773   .compat_ioctl = afs_unlocked_xioctl,
774 #endif
775   .mmap =       afs_linux_mmap,
776   .open =       afs_linux_open,
777   .flush =      afs_linux_flush,
778   .release =    afs_linux_release,
779   .fsync =      afs_linux_fsync,
780   .lock =       afs_linux_lock,
781 #ifdef STRUCT_FILE_OPERATIONS_HAS_FLOCK
782   .flock =      afs_linux_flock,
783 #endif
784 };
785
786
787 /**********************************************************************
788  * AFS Linux dentry operations
789  **********************************************************************/
790
791 /* check_bad_parent() : Checks if this dentry's vcache is a root vcache
792  * that has its mvid (parent dir's fid) pointer set to the wrong directory
793  * due to being mounted in multiple points at once. If so, check_bad_parent()
794  * calls afs_lookup() to correct the vcache's mvid, as well as the volume's
795  * dotdotfid and mtpoint fid members.
796  * Parameters:
797  *   dp - dentry to be checked.
798  * Return Values:
799  *   None.
800  * Sideeffects:
801  *   This dentry's vcache's mvid will be set to the correct parent directory's
802  *   fid.
803  *   This root vnode's volume will have its dotdotfid and mtpoint fids set
804  *   to the correct parent and mountpoint fids.
805  */
806
807 static inline void
808 check_bad_parent(struct dentry *dp)
809 {
810     cred_t *credp;
811     struct vcache *vcp = VTOAFS(dp->d_inode), *avc = NULL;
812     struct vcache *pvc = VTOAFS(dp->d_parent->d_inode);
813
814     if (vcp->mvid->Fid.Volume != pvc->f.fid.Fid.Volume) {       /* bad parent */
815         credp = crref();
816
817         /* force a lookup, so vcp->mvid is fixed up */
818         afs_lookup(pvc, (char *)dp->d_name.name, &avc, credp);
819         if (!avc || vcp != avc) {       /* bad, very bad.. */
820             afs_Trace4(afs_iclSetp, CM_TRACE_TMP_1S3L, ICL_TYPE_STRING,
821                        "check_bad_parent: bad pointer returned from afs_lookup origvc newvc dentry",
822                        ICL_TYPE_POINTER, vcp, ICL_TYPE_POINTER, avc,
823                        ICL_TYPE_POINTER, dp);
824         }
825         if (avc)
826             AFS_RELE(AFSTOV(avc));
827         crfree(credp);
828     }
829
830     return;
831 }
832
833 /* afs_linux_revalidate
834  * Ensure vcache is stat'd before use. Return 0 if entry is valid.
835  */
836 static int
837 afs_linux_revalidate(struct dentry *dp)
838 {
839     struct vattr vattr;
840     struct vcache *vcp = VTOAFS(dp->d_inode);
841     cred_t *credp;
842     int code;
843
844     if (afs_shuttingdown)
845         return EIO;
846
847 #ifdef AFS_LINUX24_ENV
848     maybe_lock_kernel();
849 #endif
850     AFS_GLOCK();
851
852 #ifdef notyet
853     /* Make this a fast path (no crref), since it's called so often. */
854     if (vcp->f.states & CStatd) {
855
856         if (*dp->d_name.name != '/' && vcp->mvstat == 2)        /* root vnode */
857             check_bad_parent(dp);       /* check and correct mvid */
858
859         AFS_GUNLOCK();
860 #ifdef AFS_LINUX24_ENV
861         unlock_kernel();
862 #endif
863         return 0;
864     }
865 #endif
866
867     /* This avoids the crref when we don't have to do it. Watch for
868      * changes in afs_getattr that don't get replicated here!
869      */
870     if (vcp->f.states & CStatd &&
871         (!afs_fakestat_enable || vcp->mvstat != 1) &&
872         !afs_nfsexporter &&
873         (vType(vcp) == VDIR || vType(vcp) == VLNK)) {
874         code = afs_CopyOutAttrs(vcp, &vattr);
875     } else {
876         credp = crref();
877         code = afs_getattr(vcp, &vattr, credp);
878         crfree(credp);
879     }
880     if (!code)
881         afs_fill_inode(AFSTOV(vcp), &vattr);
882
883     AFS_GUNLOCK();
884 #ifdef AFS_LINUX24_ENV
885     maybe_unlock_kernel();
886 #endif
887
888     return afs_convert_code(code);
889 }
890
891 /* vattr_setattr
892  * Set iattr data into vattr. Assume vattr cleared before call.
893  */
894 static void
895 iattr2vattr(struct vattr *vattrp, struct iattr *iattrp)
896 {
897     vattrp->va_mask = iattrp->ia_valid;
898     if (iattrp->ia_valid & ATTR_MODE)
899         vattrp->va_mode = iattrp->ia_mode;
900     if (iattrp->ia_valid & ATTR_UID)
901         vattrp->va_uid = iattrp->ia_uid;
902     if (iattrp->ia_valid & ATTR_GID)
903         vattrp->va_gid = iattrp->ia_gid;
904     if (iattrp->ia_valid & ATTR_SIZE)
905         vattrp->va_size = iattrp->ia_size;
906     if (iattrp->ia_valid & ATTR_ATIME) {
907         vattrp->va_atime.tv_sec = iattrp->ia_atime;
908         vattrp->va_atime.tv_usec = 0;
909     }
910     if (iattrp->ia_valid & ATTR_MTIME) {
911         vattrp->va_mtime.tv_sec = iattrp->ia_mtime;
912         vattrp->va_mtime.tv_usec = 0;
913     }
914     if (iattrp->ia_valid & ATTR_CTIME) {
915         vattrp->va_ctime.tv_sec = iattrp->ia_ctime;
916         vattrp->va_ctime.tv_usec = 0;
917     }
918 }
919
920 /* vattr2inode
921  * Rewrite the inode cache from the attr. Assumes all vattr fields are valid.
922  */
923 void
924 vattr2inode(struct inode *ip, struct vattr *vp)
925 {
926     ip->i_ino = vp->va_nodeid;
927     ip->i_nlink = vp->va_nlink;
928     ip->i_blocks = vp->va_blocks;
929 #ifdef STRUCT_INODE_HAS_I_BLKBITS
930     ip->i_blkbits = AFS_BLKBITS;
931 #endif
932 #ifdef STRUCT_INODE_HAS_I_BLKSIZE
933     ip->i_blksize = vp->va_blocksize;
934 #endif
935     ip->i_rdev = vp->va_rdev;
936     ip->i_mode = vp->va_mode;
937     ip->i_uid = vp->va_uid;
938     ip->i_gid = vp->va_gid;
939     i_size_write(ip, vp->va_size);
940     ip->i_atime = vp->va_atime.tv_sec;
941     ip->i_mtime = vp->va_mtime.tv_sec;
942     ip->i_ctime = vp->va_ctime.tv_sec;
943 }
944
945 /* afs_notify_change
946  * Linux version of setattr call. What to change is in the iattr struct.
947  * We need to set bits in both the Linux inode as well as the vcache.
948  */
949 int
950 afs_notify_change(struct dentry *dp, struct iattr *iattrp)
951 {
952     struct vattr vattr;
953     cred_t *credp = crref();
954     struct inode *ip = dp->d_inode;
955     int code;
956
957     VATTR_NULL(&vattr);
958     iattr2vattr(&vattr, iattrp);        /* Convert for AFS vnodeops call. */
959
960     AFS_GLOCK();
961     code = afs_setattr(VTOAFS(ip), &vattr, credp);
962     if (!code) {
963         afs_getattr(VTOAFS(ip), &vattr, credp);
964         vattr2inode(ip, &vattr);
965     }
966     AFS_GUNLOCK();
967     crfree(credp);
968     return afs_convert_code(code);
969 }
970
971 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
972  * In kernels 2.2.10 and above, we are passed an additional flags var which
973  * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
974  * we are advised to follow the entry if it is a link or to make sure that 
975  * it is a directory. But since the kernel itself checks these possibilities
976  * later on, we shouldn't have to do it until later. Perhaps in the future..
977  */
978 static int
979 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
980 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
981 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
982 #else
983 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
984 #endif
985 #else
986 afs_linux_dentry_revalidate(struct dentry *dp)
987 #endif
988 {
989     struct vattr vattr;
990     cred_t *credp = NULL;
991     struct vcache *vcp, *pvcp, *tvc = NULL;
992     int valid;
993     struct afs_fakestat_state fakestate;
994
995 #ifdef AFS_LINUX24_ENV
996     maybe_lock_kernel();
997 #endif
998     AFS_GLOCK();
999     afs_InitFakeStat(&fakestate);
1000
1001     if (dp->d_inode) {
1002
1003         vcp = VTOAFS(dp->d_inode);
1004         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
1005
1006         if (vcp == afs_globalVp)
1007             goto good_dentry;
1008
1009         if (vcp->mvstat == 1) {         /* mount point */
1010             if (vcp->mvid && (vcp->f.states & CMValid)) {
1011                 int tryEvalOnly = 0;
1012                 int code = 0;
1013                 struct vrequest treq;
1014
1015                 credp = crref();
1016                 code = afs_InitReq(&treq, credp);
1017                 if (
1018 #ifdef AFS_DARWIN_ENV
1019                     (strcmp(dp->d_name.name, ".DS_Store") == 0) ||
1020                     (strcmp(dp->d_name.name, "Contents") == 0) ||
1021 #endif
1022                     (strcmp(dp->d_name.name, ".directory") == 0)) {
1023                     tryEvalOnly = 1;
1024                 }
1025                 if (tryEvalOnly)
1026                     code = afs_TryEvalFakeStat(&vcp, &fakestate, &treq);
1027                 else
1028                     code = afs_EvalFakeStat(&vcp, &fakestate, &treq);
1029                 if ((tryEvalOnly && vcp->mvstat == 1) || code) {
1030                     /* a mount point, not yet replaced by its directory */
1031                     goto bad_dentry;
1032                 }
1033             }
1034         } else
1035             if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
1036                 check_bad_parent(dp);   /* check and correct mvid */
1037
1038 #ifdef notdef
1039         /* If the last looker changes, we should make sure the current
1040          * looker still has permission to examine this file.  This would
1041          * always require a crref() which would be "slow".
1042          */
1043         if (vcp->last_looker != treq.uid) {
1044             if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
1045                 goto bad_dentry;
1046
1047             vcp->last_looker = treq.uid;
1048         }
1049 #endif
1050
1051         /* If the parent's DataVersion has changed or the vnode
1052          * is longer valid, we need to do a full lookup.  VerifyVCache
1053          * isn't enough since the vnode may have been renamed.
1054          */
1055
1056         if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time || !(vcp->f.states & CStatd)) {
1057
1058             credp = crref();
1059             afs_lookup(pvcp, (char *)dp->d_name.name, &tvc, credp);
1060             if (!tvc || tvc != vcp)
1061                 goto bad_dentry;
1062
1063             if (afs_getattr(vcp, &vattr, credp))
1064                 goto bad_dentry;
1065
1066             vattr2inode(AFSTOV(vcp), &vattr);
1067             dp->d_time = hgetlo(pvcp->f.m.DataVersion);
1068         }
1069
1070         /* should we always update the attributes at this point? */
1071         /* unlikely--the vcache entry hasn't changed */
1072
1073     } else {
1074 #ifdef notyet
1075         pvcp = VTOAFS(dp->d_parent->d_inode);           /* dget_parent()? */
1076         if (hgetlo(pvcp->f.m.DataVersion) > dp->d_time)
1077             goto bad_dentry;
1078 #endif
1079
1080         /* No change in parent's DataVersion so this negative
1081          * lookup is still valid.  BUT, if a server is down a
1082          * negative lookup can result so there should be a
1083          * liftime as well.  For now, always expire.
1084          */
1085
1086         goto bad_dentry;
1087     }
1088
1089   good_dentry:
1090     valid = 1;
1091
1092   done:
1093     /* Clean up */
1094     if (tvc)
1095         afs_PutVCache(tvc);
1096     afs_PutFakeStat(&fakestate);
1097     AFS_GUNLOCK();
1098     if (credp)
1099         crfree(credp);
1100
1101     if (!valid) {
1102         shrink_dcache_parent(dp);
1103         d_drop(dp);
1104     }
1105 #ifdef AFS_LINUX24_ENV
1106     maybe_unlock_kernel();
1107 #endif
1108     return valid;
1109
1110   bad_dentry:
1111     if (have_submounts(dp))
1112         valid = 1;
1113     else 
1114         valid = 0;
1115     goto done;
1116 }
1117
1118 static void
1119 afs_dentry_iput(struct dentry *dp, struct inode *ip)
1120 {
1121     struct vcache *vcp = VTOAFS(ip);
1122
1123     AFS_GLOCK();
1124     if (!AFS_IS_DISCONNECTED || (vcp->f.states & CUnlinked)) {
1125         (void) afs_InactiveVCache(vcp, NULL);
1126     }
1127     AFS_GUNLOCK();
1128 #ifdef DCACHE_NFSFS_RENAMED
1129     dp->d_flags &= ~DCACHE_NFSFS_RENAMED;   
1130 #endif
1131
1132     iput(ip);
1133 }
1134
1135 static int
1136 afs_dentry_delete(struct dentry *dp)
1137 {
1138     if (dp->d_inode && (VTOAFS(dp->d_inode)->f.states & CUnlinked))
1139         return 1;               /* bad inode? */
1140
1141     return 0;
1142 }
1143
1144 struct dentry_operations afs_dentry_operations = {
1145   .d_revalidate =       afs_linux_dentry_revalidate,
1146   .d_delete =           afs_dentry_delete,
1147   .d_iput =             afs_dentry_iput,
1148 };
1149
1150 /**********************************************************************
1151  * AFS Linux inode operations
1152  **********************************************************************/
1153
1154 /* afs_linux_create
1155  *
1156  * Merely need to set enough of vattr to get us through the create. Note
1157  * that the higher level code (open_namei) will take care of any tuncation
1158  * explicitly. Exclusive open is also taken care of in open_namei.
1159  *
1160  * name is in kernel space at this point.
1161  */
1162 static int
1163 #ifdef IOP_CREATE_TAKES_NAMEIDATA
1164 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
1165                  struct nameidata *nd)
1166 #else
1167 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
1168 #endif
1169 {
1170     struct vattr vattr;
1171     cred_t *credp = crref();
1172     const char *name = dp->d_name.name;
1173     struct vcache *vcp;
1174     int code;
1175
1176     VATTR_NULL(&vattr);
1177     vattr.va_mode = mode;
1178     vattr.va_type = mode & S_IFMT;
1179
1180     AFS_GLOCK();
1181     code = afs_create(VTOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1182                       &vcp, credp);
1183
1184     if (!code) {
1185         struct inode *ip = AFSTOV(vcp);
1186
1187         afs_getattr(vcp, &vattr, credp);
1188         afs_fill_inode(ip, &vattr);
1189         insert_inode_hash(ip);
1190         dp->d_op = &afs_dentry_operations;
1191         dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1192         d_instantiate(dp, ip);
1193     }
1194     AFS_GUNLOCK();
1195
1196     crfree(credp);
1197     return afs_convert_code(code);
1198 }
1199
1200 /* afs_linux_lookup */
1201 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1202 static struct dentry *
1203 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1204 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1205                  struct nameidata *nd)
1206 #else
1207 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1208 #endif
1209 #else
1210 static int
1211 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1212 #endif
1213 {
1214     cred_t *credp = crref();
1215     struct vcache *vcp = NULL;
1216     const char *comp = dp->d_name.name;
1217     struct inode *ip = NULL;
1218     int code;
1219
1220     AFS_GLOCK();
1221     code = afs_lookup(VTOAFS(dip), (char *)comp, &vcp, credp);
1222     
1223     if (vcp) {
1224         struct vattr vattr;
1225
1226         ip = AFSTOV(vcp);
1227         afs_getattr(vcp, &vattr, credp);
1228         afs_fill_inode(ip, &vattr);
1229         if (
1230 #ifdef HAVE_LINUX_HLIST_UNHASHED
1231             hlist_unhashed(&ip->i_hash)
1232 #else
1233             ip->i_hash.prev == NULL
1234 #endif
1235             )
1236             insert_inode_hash(ip);
1237     }
1238     dp->d_op = &afs_dentry_operations;
1239     dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1240     AFS_GUNLOCK();
1241
1242 #if defined(AFS_LINUX24_ENV)
1243     if (ip && S_ISDIR(ip->i_mode)) {
1244         struct dentry *alias;
1245
1246         /* Try to invalidate an existing alias in favor of our new one */
1247         alias = d_find_alias(ip);
1248         if (alias) {
1249             if (d_invalidate(alias) == 0) {
1250                 dput(alias);
1251             } else {
1252                 iput(ip);
1253                 crfree(credp);
1254                 return alias;
1255             }
1256         }
1257     }
1258 #endif
1259     d_add(dp, ip);
1260
1261     crfree(credp);
1262
1263     /* It's ok for the file to not be found. That's noted by the caller by
1264      * seeing that the dp->d_inode field is NULL.
1265      */
1266 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1267     if (code == ENOENT)
1268         return ERR_PTR(0);
1269     else 
1270         return ERR_PTR(afs_convert_code(code));
1271 #else
1272     if (code == ENOENT)
1273         code = 0;
1274     return afs_convert_code(code);
1275 #endif
1276 }
1277
1278 static int
1279 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1280 {
1281     int code;
1282     cred_t *credp = crref();
1283     const char *name = newdp->d_name.name;
1284     struct inode *oldip = olddp->d_inode;
1285
1286     /* If afs_link returned the vnode, we could instantiate the
1287      * dentry. Since it's not, we drop this one and do a new lookup.
1288      */
1289     d_drop(newdp);
1290
1291     AFS_GLOCK();
1292     code = afs_link(VTOAFS(oldip), VTOAFS(dip), (char *)name, credp);
1293
1294     AFS_GUNLOCK();
1295     crfree(credp);
1296     return afs_convert_code(code);
1297 }
1298
1299 static int
1300 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1301 {
1302     int code = EBUSY;
1303     cred_t *credp = crref();
1304     const char *name = dp->d_name.name;
1305     struct vcache *tvc = VTOAFS(dp->d_inode);
1306
1307     if (VREFCOUNT(tvc) > 1 && tvc->opens > 0
1308                                 && !(tvc->f.states & CUnlinked)) {
1309         struct dentry *__dp;
1310         char *__name;
1311
1312         __dp = NULL;
1313         __name = NULL;
1314         do {
1315             dput(__dp);
1316
1317             AFS_GLOCK();
1318             if (__name)
1319                 osi_FreeSmallSpace(__name);
1320             __name = afs_newname();
1321             AFS_GUNLOCK();
1322
1323             __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1324                 
1325             if (IS_ERR(__dp))
1326                 goto out;
1327         } while (__dp->d_inode != NULL);
1328
1329         AFS_GLOCK();
1330         code = afs_rename(VTOAFS(dip), (char *)dp->d_name.name, VTOAFS(dip), (char *)__dp->d_name.name, credp);
1331         if (!code) {
1332             tvc->mvid = (void *) __name;
1333             crhold(credp);
1334             if (tvc->uncred) {
1335                 crfree(tvc->uncred);
1336             }
1337             tvc->uncred = credp;
1338             tvc->f.states |= CUnlinked;
1339 #ifdef DCACHE_NFSFS_RENAMED
1340             dp->d_flags |= DCACHE_NFSFS_RENAMED;   
1341 #endif
1342         } else {
1343             osi_FreeSmallSpace(__name); 
1344         }
1345         AFS_GUNLOCK();
1346
1347         if (!code) {
1348             __dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1349             d_move(dp, __dp);
1350         }
1351         dput(__dp);
1352
1353         goto out;
1354     }
1355
1356     AFS_GLOCK();
1357     code = afs_remove(VTOAFS(dip), (char *)name, credp);
1358     AFS_GUNLOCK();
1359     if (!code)
1360         d_drop(dp);
1361 out:
1362     crfree(credp);
1363     return afs_convert_code(code);
1364 }
1365
1366
1367 static int
1368 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1369 {
1370     int code;
1371     cred_t *credp = crref();
1372     struct vattr vattr;
1373     const char *name = dp->d_name.name;
1374
1375     /* If afs_symlink returned the vnode, we could instantiate the
1376      * dentry. Since it's not, we drop this one and do a new lookup.
1377      */
1378     d_drop(dp);
1379
1380     VATTR_NULL(&vattr);
1381     AFS_GLOCK();
1382     code = afs_symlink(VTOAFS(dip), (char *)name, &vattr, (char *)target, credp);
1383     AFS_GUNLOCK();
1384     crfree(credp);
1385     return afs_convert_code(code);
1386 }
1387
1388 static int
1389 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1390 {
1391     int code;
1392     cred_t *credp = crref();
1393     struct vcache *tvcp = NULL;
1394     struct vattr vattr;
1395     const char *name = dp->d_name.name;
1396
1397     VATTR_NULL(&vattr);
1398     vattr.va_mask = ATTR_MODE;
1399     vattr.va_mode = mode;
1400     AFS_GLOCK();
1401     code = afs_mkdir(VTOAFS(dip), (char *)name, &vattr, &tvcp, credp);
1402
1403     if (tvcp) {
1404         struct inode *ip = AFSTOV(tvcp);
1405
1406         afs_getattr(tvcp, &vattr, credp);
1407         afs_fill_inode(ip, &vattr);
1408
1409         dp->d_op = &afs_dentry_operations;
1410         dp->d_time = hgetlo(VTOAFS(dip)->f.m.DataVersion);
1411         d_instantiate(dp, ip);
1412     }
1413     AFS_GUNLOCK();
1414
1415     crfree(credp);
1416     return afs_convert_code(code);
1417 }
1418
1419 static int
1420 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1421 {
1422     int code;
1423     cred_t *credp = crref();
1424     const char *name = dp->d_name.name;
1425
1426     /* locking kernel conflicts with glock? */
1427
1428     AFS_GLOCK();
1429     code = afs_rmdir(VTOAFS(dip), (char *)name, credp);
1430     AFS_GUNLOCK();
1431
1432     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1433      * that failed because a directory is not empty. So, we map
1434      * EEXIST to ENOTEMPTY on linux.
1435      */
1436     if (code == EEXIST) {
1437         code = ENOTEMPTY;
1438     }
1439
1440     if (!code) {
1441         d_drop(dp);
1442     }
1443
1444     crfree(credp);
1445     return afs_convert_code(code);
1446 }
1447
1448
1449 static int
1450 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1451                  struct inode *newip, struct dentry *newdp)
1452 {
1453     int code;
1454     cred_t *credp = crref();
1455     const char *oldname = olddp->d_name.name;
1456     const char *newname = newdp->d_name.name;
1457     struct dentry *rehash = NULL;
1458
1459     if (!list_empty(&newdp->d_hash)) {
1460         d_drop(newdp);
1461         rehash = newdp;
1462     }
1463
1464 #if defined(AFS_LINUX24_ENV)
1465     if (atomic_read(&olddp->d_count) > 1)
1466         shrink_dcache_parent(olddp);
1467 #endif
1468
1469     AFS_GLOCK();
1470     code = afs_rename(VTOAFS(oldip), (char *)oldname, VTOAFS(newip), (char *)newname, credp);
1471     AFS_GUNLOCK();
1472
1473     if (!code)
1474         olddp->d_time = 0;      /* force to revalidate */
1475
1476     if (rehash)
1477         d_rehash(rehash);
1478
1479     crfree(credp);
1480     return afs_convert_code(code);
1481 }
1482
1483
1484 /* afs_linux_ireadlink 
1485  * Internal readlink which can return link contents to user or kernel space.
1486  * Note that the buffer is NOT supposed to be null-terminated.
1487  */
1488 static int
1489 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1490 {
1491     int code;
1492     cred_t *credp = crref();
1493     struct uio tuio;
1494     struct iovec iov;
1495
1496     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1497     code = afs_readlink(VTOAFS(ip), &tuio, credp);
1498     crfree(credp);
1499
1500     if (!code)
1501         return maxlen - tuio.uio_resid;
1502     else
1503         return afs_convert_code(code);
1504 }
1505
1506 #if !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
1507 /* afs_linux_readlink 
1508  * Fill target (which is in user space) with contents of symlink.
1509  */
1510 static int
1511 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1512 {
1513     int code;
1514     struct inode *ip = dp->d_inode;
1515
1516     AFS_GLOCK();
1517     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1518     AFS_GUNLOCK();
1519     return code;
1520 }
1521
1522
1523 /* afs_linux_follow_link
1524  * a file system dependent link following routine.
1525  */
1526 #if defined(AFS_LINUX24_ENV)
1527 static int afs_linux_follow_link(struct dentry *dentry, struct nameidata *nd)
1528 {
1529     int code;
1530     char *name;
1531
1532     name = osi_Alloc(PATH_MAX);
1533     if (!name) {
1534         return -EIO;
1535     }
1536
1537     AFS_GLOCK();
1538     code = afs_linux_ireadlink(dentry->d_inode, name, PATH_MAX - 1, AFS_UIOSYS);
1539     AFS_GUNLOCK();
1540
1541     if (code < 0) {
1542         goto out;
1543     }
1544
1545     name[code] = '\0';
1546     code = vfs_follow_link(nd, name);
1547
1548 out:
1549     osi_Free(name, PATH_MAX);
1550
1551     return code;
1552 }
1553
1554 #else /* !defined(AFS_LINUX24_ENV) */
1555
1556 static struct dentry *
1557 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1558                       unsigned int follow)
1559 {
1560     int code = 0;
1561     char *name;
1562     struct dentry *res;
1563
1564
1565     AFS_GLOCK();
1566     name = osi_Alloc(PATH_MAX + 1);
1567     if (!name) {
1568         AFS_GUNLOCK();
1569         dput(basep);
1570         return ERR_PTR(-EIO);
1571     }
1572
1573     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1574     AFS_GUNLOCK();
1575
1576     if (code < 0) {
1577         dput(basep);
1578         if (code < -MAX_ERRNO)
1579             res = ERR_PTR(-EIO);
1580         else
1581             res = ERR_PTR(code);
1582     } else {
1583         name[code] = '\0';
1584         res = lookup_dentry(name, basep, follow);
1585     }
1586
1587     AFS_GLOCK();
1588     osi_Free(name, PATH_MAX + 1);
1589     AFS_GUNLOCK();
1590     return res;
1591 }
1592 #endif /* AFS_LINUX24_ENV */
1593 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
1594
1595 static inline int
1596 afs_linux_can_bypass(struct inode *ip) {
1597     switch(cache_bypass_strategy) {
1598         case NEVER_BYPASS_CACHE:
1599             return 0;
1600         case ALWAYS_BYPASS_CACHE:
1601             return 1;
1602         case LARGE_FILES_BYPASS_CACHE:
1603             if(i_size_read(ip) > cache_bypass_threshold)
1604                 return 1;
1605         default:
1606             return 0;
1607      }
1608 }
1609
1610 /* afs_linux_readpage
1611  * all reads come through here. A strategy-like read call.
1612  */
1613 static int
1614 afs_linux_readpage(struct file *fp, struct page *pp)
1615 {
1616     afs_int32 code;
1617 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1618     char *address;
1619     afs_offs_t offset = ((loff_t) pp->index) << PAGE_CACHE_SHIFT;
1620 #else
1621     ulong address = afs_linux_page_address(pp);
1622     afs_offs_t offset = pageoff(pp);
1623 #endif
1624     afs_int32 bypasscache = 0; /* bypass for this read */
1625     struct nocache_read_request *ancr;
1626 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1627     afs_int32 isize;
1628 #endif
1629     struct uio *auio;
1630     struct iovec *iovecp;
1631     struct inode *ip = FILE_INODE(fp);
1632     afs_int32 cnt = page_count(pp);
1633     struct vcache *avc = VTOAFS(ip);
1634     cred_t *credp;
1635
1636     credp = crref();
1637 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1638     address = kmap(pp);
1639     ClearPageError(pp);
1640 #else
1641     atomic_add(1, &pp->count);
1642     set_bit(PG_locked, &pp->flags);     /* other bits? See mm.h */
1643     clear_bit(PG_error, &pp->flags);
1644 #endif
1645     /* if bypasscache, receiver frees, else we do */
1646     auio = osi_Alloc(sizeof(struct uio));
1647     iovecp = osi_Alloc(sizeof(struct iovec));
1648
1649     setup_uio(auio, iovecp, (char *)address, offset, PAGE_SIZE, UIO_READ,
1650               AFS_UIOSYS);
1651
1652     bypasscache = afs_linux_can_bypass(ip);
1653
1654     /* In the new incarnation of selective caching, a file's caching policy
1655      * can change, eg because file size exceeds threshold, etc. */
1656     trydo_cache_transition(avc, credp, bypasscache);
1657         
1658     if(bypasscache) {
1659         if(address)
1660             kunmap(pp);
1661         /* save the page for background map */
1662         auio->uio_iov->iov_base = (void*) pp;
1663         /* the background thread will free this */
1664         ancr = osi_Alloc(sizeof(struct nocache_read_request));
1665         ancr->auio = auio;
1666         ancr->offset = offset;
1667         ancr->length = PAGE_SIZE;
1668
1669         maybe_lock_kernel();
1670         code = afs_ReadNoCache(avc, ancr, credp);
1671         maybe_unlock_kernel();
1672
1673         goto done; /* skips release page, doing it in bg thread */
1674     }
1675                   
1676 #ifdef AFS_LINUX24_ENV
1677     maybe_lock_kernel();
1678 #endif
1679     AFS_GLOCK();
1680     AFS_DISCON_LOCK();
1681     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1682                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1683                99999);  /* not a possible code value */
1684
1685     code = afs_rdwr(avc, auio, UIO_READ, 0, credp);
1686         
1687     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1688                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1689                code);
1690     AFS_DISCON_UNLOCK();
1691     AFS_GUNLOCK();
1692 #ifdef AFS_LINUX24_ENV
1693     maybe_unlock_kernel();
1694 #endif
1695     if (!code) {
1696         /* XXX valid for no-cache also?  Check last bits of files... :)
1697          * Cognate code goes in afs_NoCacheFetchProc.  */
1698         if (auio->uio_resid)    /* zero remainder of page */
1699              memset((void *)(address + (PAGE_SIZE - auio->uio_resid)), 0,
1700                     auio->uio_resid);
1701
1702 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1703         flush_dcache_page(pp);
1704         SetPageUptodate(pp);
1705 #else
1706         set_bit(PG_uptodate, &pp->flags);
1707 #endif
1708     } /* !code */
1709
1710 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1711     kunmap(pp);
1712     UnlockPage(pp);
1713 #else
1714     clear_bit(PG_locked, &pp->flags);
1715     wake_up(&pp->wait);
1716     free_page(address);
1717 #endif
1718
1719     /* do not call afs_GetDCache if cache is bypassed */
1720     if(bypasscache)
1721         goto done;
1722
1723     /* free if not bypassing cache */
1724     osi_Free(auio, sizeof(struct uio));
1725     osi_Free(iovecp, sizeof(struct iovec));
1726
1727     if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1728         struct dcache *tdc;
1729         struct vrequest treq;
1730
1731         AFS_GLOCK();
1732         code = afs_InitReq(&treq, credp);
1733         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1734             tdc = afs_FindDCache(avc, offset);
1735             if (tdc) {
1736                 if (!(tdc->mflags & DFNextStarted))
1737                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1738                     afs_PutDCache(tdc);
1739             }
1740             ReleaseWriteLock(&avc->lock);
1741         }
1742         AFS_GUNLOCK();
1743     }
1744
1745 done:
1746     crfree(credp);
1747     return afs_convert_code(code);
1748 }
1749
1750 #if defined(AFS_LINUX24_ENV)
1751 static int
1752 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1753                          unsigned long offset, unsigned int count)
1754 {
1755     struct vcache *vcp = VTOAFS(ip);
1756     char *buffer;
1757     afs_offs_t base;
1758     int code = 0;
1759     cred_t *credp;
1760     struct uio tuio;
1761     struct iovec iovec;
1762     int f_flags = 0;
1763
1764     buffer = kmap(pp) + offset;
1765     base = (((loff_t) pp->index) << PAGE_CACHE_SHIFT)  + offset;
1766
1767     credp = crref();
1768     maybe_lock_kernel();
1769     AFS_GLOCK();
1770     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1771                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1772                ICL_TYPE_INT32, 99999);
1773
1774     ObtainWriteLock(&vcp->lock, 532);
1775     if (vcp->f.states & CPageWrite) {
1776         ReleaseWriteLock(&vcp->lock);
1777         AFS_GUNLOCK();
1778         maybe_unlock_kernel();
1779         crfree(credp);
1780         kunmap(pp);
1781         /* should mark it dirty? */
1782         return(0); 
1783     }
1784     vcp->f.states |= CPageWrite;
1785     ReleaseWriteLock(&vcp->lock);
1786
1787     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1788
1789     code = afs_write(vcp, &tuio, f_flags, credp, 0);
1790
1791     i_size_write(ip, vcp->f.m.Length);
1792     ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
1793
1794     ObtainWriteLock(&vcp->lock, 533);
1795     if (!code) {
1796         struct vrequest treq;
1797
1798         if (!afs_InitReq(&treq, credp))
1799             code = afs_DoPartialWrite(vcp, &treq);
1800     }
1801     code = code ? afs_convert_code(code) : count - tuio.uio_resid;
1802
1803     vcp->f.states &= ~CPageWrite;
1804     ReleaseWriteLock(&vcp->lock);
1805
1806     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1807                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1808                ICL_TYPE_INT32, code);
1809
1810     AFS_GUNLOCK();
1811     maybe_unlock_kernel();
1812     crfree(credp);
1813     kunmap(pp);
1814
1815     return code;
1816 }
1817
1818
1819 static int
1820 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1821 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1822 #else
1823 afs_linux_writepage(struct page *pp)
1824 #endif
1825 {
1826     struct address_space *mapping = pp->mapping;
1827     struct inode *inode;
1828     unsigned long end_index;
1829     unsigned offset = PAGE_CACHE_SIZE;
1830     long status;
1831
1832     if (PageLaunder(pp)) {
1833         return(fail_writepage(pp));
1834     }
1835
1836     inode = (struct inode *)mapping->host;
1837     end_index = i_size_read(inode) >> PAGE_CACHE_SHIFT;
1838
1839     /* easy case */
1840     if (pp->index < end_index)
1841         goto do_it;
1842     /* things got complicated... */
1843     offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
1844     /* OK, are we completely out? */
1845     if (pp->index >= end_index + 1 || !offset)
1846         return -EIO;
1847   do_it:
1848     status = afs_linux_writepage_sync(inode, pp, 0, offset);
1849     SetPageUptodate(pp);
1850     UnlockPage(pp);
1851     if (status == offset)
1852         return 0;
1853     else
1854         return status;
1855 }
1856 #else
1857 /* afs_linux_updatepage
1858  * What one would have thought was writepage - write dirty page to file.
1859  * Called from generic_file_write. buffer is still in user space. pagep
1860  * has been filled in with old data if we're updating less than a page.
1861  */
1862 static int
1863 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1864                      unsigned int count, int sync)
1865 {
1866     struct vcache *vcp = VTOAFS(FILE_INODE(fp));
1867     u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1868     int code = 0;
1869     cred_t *credp;
1870     struct uio tuio;
1871     struct iovec iovec;
1872
1873     set_bit(PG_locked, &pp->flags);
1874
1875     credp = crref();
1876     AFS_GLOCK();
1877     AFS_DISCON_LOCK();
1878     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1879                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1880                ICL_TYPE_INT32, 99999);
1881     setup_uio(&tuio, &iovec, page_addr + offset,
1882               (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1883               AFS_UIOSYS);
1884
1885     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1886
1887     i_size_write(ip, vcp->f.m.Length);
1888     ip->i_blocks = ((vcp->f.m.Length + 1023) >> 10) << 1;
1889
1890     if (!code) {
1891         struct vrequest treq;
1892
1893         ObtainWriteLock(&vcp->lock, 533);
1894         vcp->f.m.Date = osi_Time();   /* set modification time */
1895         if (!afs_InitReq(&treq, credp))
1896             code = afs_DoPartialWrite(vcp, &treq);
1897         ReleaseWriteLock(&vcp->lock);
1898     }
1899
1900     code = code ? afs_convert_code(code) : count - tuio.uio_resid;
1901     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1902                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1903                ICL_TYPE_INT32, code);
1904
1905     AFS_DISCON_UNLOCK();
1906     AFS_GUNLOCK();
1907     crfree(credp);
1908
1909     clear_bit(PG_locked, &pp->flags);
1910     return code;
1911 }
1912 #endif
1913
1914 /* afs_linux_permission
1915  * Check access rights - returns error if can't check or permission denied.
1916  */
1917 static int
1918 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1919 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1920 #else
1921 afs_linux_permission(struct inode *ip, int mode)
1922 #endif
1923 {
1924     int code;
1925     cred_t *credp = crref();
1926     int tmp = 0;
1927
1928     AFS_GLOCK();
1929     if (mode & MAY_EXEC)
1930         tmp |= VEXEC;
1931     if (mode & MAY_READ)
1932         tmp |= VREAD;
1933     if (mode & MAY_WRITE)
1934         tmp |= VWRITE;
1935     code = afs_access(VTOAFS(ip), tmp, credp);
1936
1937     AFS_GUNLOCK();
1938     crfree(credp);
1939     return afs_convert_code(code);
1940 }
1941
1942 #if defined(AFS_LINUX24_ENV) && !defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
1943 static int
1944 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1945                        unsigned to)
1946 {
1947     int code;
1948
1949     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1950                                     offset, to - offset);
1951     kunmap(page);
1952
1953     return code;
1954 }
1955
1956 static int
1957 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1958                         unsigned to)
1959 {
1960 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1961    call kmap directly instead of relying on us to do it */
1962     kmap(page);
1963     return 0;
1964 }
1965 #endif
1966
1967 #if defined(STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
1968 static int
1969 afs_linux_write_end(struct file *file, struct address_space *mapping,
1970                                 loff_t pos, unsigned len, unsigned copied,
1971                                 struct page *page, void *fsdata)
1972 {
1973     int code;
1974     unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1975
1976     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1977                                     from, copied);
1978     unlock_page(page);
1979     page_cache_release(page);
1980     return code;
1981 }
1982
1983 static int
1984 afs_linux_write_begin(struct file *file, struct address_space *mapping,
1985                                 loff_t pos, unsigned len, unsigned flags,
1986                                 struct page **pagep, void **fsdata)
1987 {
1988     struct page *page;
1989     pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1990 #if defined(HAVE_LINUX_GRAB_CACHE_PAGE_WRITE_BEGIN)
1991     page = grab_cache_page_write_begin(mapping, index, flags);
1992 #else
1993     page = __grab_cache_page(mapping, index);
1994 #endif
1995     *pagep = page;
1996
1997     return 0;
1998 }
1999 #endif
2000
2001
2002 static struct inode_operations afs_file_iops = {
2003 #if defined(AFS_LINUX24_ENV)
2004   .permission =         afs_linux_permission,
2005   .revalidate =         afs_linux_revalidate,
2006   .setattr =            afs_notify_change,
2007 #else
2008   .default_file_ops =   &afs_file_fops,
2009   .readpage =           afs_linux_readpage,  
2010   .revalidate =         afs_linux_revalidate,
2011   .updatepage =         afs_linux_updatepage,
2012 #endif
2013 };
2014
2015 #if defined(AFS_LINUX24_ENV)
2016 static struct address_space_operations afs_file_aops = {
2017   .readpage =           afs_linux_readpage,
2018   .writepage =          afs_linux_writepage,
2019 #if defined (STRUCT_ADDRESS_SPACE_OPERATIONS_HAS_WRITE_BEGIN)
2020   .write_begin =        afs_linux_write_begin,
2021   .write_end =          afs_linux_write_end,
2022 #else
2023   .commit_write =       afs_linux_commit_write,
2024   .prepare_write =      afs_linux_prepare_write,
2025 #endif
2026 };
2027 #endif
2028
2029
2030 /* Separate ops vector for directories. Linux 2.2 tests type of inode
2031  * by what sort of operation is allowed.....
2032  */
2033
2034 static struct inode_operations afs_dir_iops = {
2035 #if !defined(AFS_LINUX24_ENV)
2036   .default_file_ops =   &afs_dir_fops,
2037 #else
2038   .setattr =            afs_notify_change,
2039 #endif
2040   .create =             afs_linux_create,
2041   .lookup =             afs_linux_lookup,
2042   .link =               afs_linux_link,
2043   .unlink =             afs_linux_unlink,
2044   .symlink =            afs_linux_symlink,
2045   .mkdir =              afs_linux_mkdir,
2046   .rmdir =              afs_linux_rmdir,
2047   .rename =             afs_linux_rename,
2048   .revalidate =         afs_linux_revalidate,
2049   .permission =         afs_linux_permission,
2050 };
2051
2052 /* We really need a separate symlink set of ops, since do_follow_link()
2053  * determines if it _is_ a link by checking if the follow_link op is set.
2054  */
2055 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2056 static int
2057 afs_symlink_filler(struct file *file, struct page *page)
2058 {
2059     struct inode *ip = (struct inode *)page->mapping->host;
2060     char *p = (char *)kmap(page);
2061     int code;
2062
2063     maybe_lock_kernel();
2064     AFS_GLOCK();
2065     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
2066     AFS_GUNLOCK();
2067
2068     if (code < 0)
2069         goto fail;
2070     p[code] = '\0';             /* null terminate? */
2071     maybe_unlock_kernel();
2072
2073     SetPageUptodate(page);
2074     kunmap(page);
2075     UnlockPage(page);
2076     return 0;
2077
2078   fail:
2079     maybe_unlock_kernel();
2080
2081     SetPageError(page);
2082     kunmap(page);
2083     UnlockPage(page);
2084     return code;
2085 }
2086
2087 static struct address_space_operations afs_symlink_aops = {
2088   .readpage =   afs_symlink_filler
2089 };
2090 #endif  /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2091
2092 static struct inode_operations afs_symlink_iops = {
2093 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2094   .readlink =           page_readlink,
2095 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
2096   .follow_link =        page_follow_link,
2097 #else
2098   .follow_link =        page_follow_link_light,
2099   .put_link =           page_put_link,
2100 #endif
2101 #else /* !defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE) */
2102   .readlink =           afs_linux_readlink,
2103   .follow_link =        afs_linux_follow_link,
2104 #if !defined(AFS_LINUX24_ENV)
2105   .permission =         afs_linux_permission,
2106   .revalidate =         afs_linux_revalidate,
2107 #endif
2108 #endif /* USABLE_KERNEL_PAGE_SYMLINK_CACHE */
2109 #if defined(AFS_LINUX24_ENV)
2110   .setattr =            afs_notify_change,
2111 #endif
2112 };
2113
2114 void
2115 afs_fill_inode(struct inode *ip, struct vattr *vattr)
2116 {
2117         
2118     if (vattr)
2119         vattr2inode(ip, vattr);
2120
2121 /* Reset ops if symlink or directory. */
2122     if (S_ISREG(ip->i_mode)) {
2123         ip->i_op = &afs_file_iops;
2124 #if defined(AFS_LINUX24_ENV)
2125         ip->i_fop = &afs_file_fops;
2126         ip->i_data.a_ops = &afs_file_aops;
2127 #endif
2128
2129     } else if (S_ISDIR(ip->i_mode)) {
2130         ip->i_op = &afs_dir_iops;
2131 #if defined(AFS_LINUX24_ENV)
2132         ip->i_fop = &afs_dir_fops;
2133 #endif
2134
2135     } else if (S_ISLNK(ip->i_mode)) {
2136         ip->i_op = &afs_symlink_iops;
2137 #if defined(USABLE_KERNEL_PAGE_SYMLINK_CACHE)
2138         ip->i_data.a_ops = &afs_symlink_aops;
2139         ip->i_mapping = &ip->i_data;
2140 #endif
2141     }
2142
2143 }