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