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