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