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