linux-change-dentry-cleanup-20050619
[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 #endif
41
42 #ifdef pgoff2loff
43 #define pageoff(pp) pgoff2loff((pp)->index)
44 #else
45 #define pageoff(pp) pp->offset
46 #endif
47
48 #if defined(AFS_LINUX26_ENV)
49 #define UnlockPage(pp) unlock_page(pp)
50 #endif
51
52 extern struct vcache *afs_globalVp;
53 extern afs_rwlock_t afs_xvcache;
54
55 #if defined(AFS_LINUX24_ENV)
56 extern struct inode_operations afs_file_iops;
57 extern struct address_space_operations afs_file_aops;
58 struct address_space_operations afs_symlink_aops;
59 #endif
60 extern struct inode_operations afs_dir_iops;
61 extern struct inode_operations afs_symlink_iops;
62
63
64 static ssize_t
65 afs_linux_read(struct file *fp, char *buf, size_t count, loff_t * offp)
66 {
67     ssize_t code;
68     struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
69     cred_t *credp = crref();
70     struct vrequest treq;
71
72     AFS_GLOCK();
73     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
74                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
75                99999);
76
77     /* get a validated vcache entry */
78     code = afs_InitReq(&treq, credp);
79     if (!code)
80         code = afs_VerifyVCache(vcp, &treq);
81
82     if (code)
83         code = -code;
84     else {
85 #ifdef AFS_64BIT_CLIENT
86         if (*offp + count > afs_vmMappingEnd) {
87             uio_t tuio;
88             struct iovec iov;
89             afs_int32 xfered = 0;
90
91             if (*offp < afs_vmMappingEnd) {
92                 /* special case of a buffer crossing the VM mapping end */
93                 afs_int32 tcount = afs_vmMappingEnd - *offp;
94                 count -= tcount;
95                 osi_FlushPages(vcp, credp);     /* ensure stale pages are gone */
96                 AFS_GUNLOCK();
97                 code = generic_file_read(fp, buf, tcount, offp);
98                 AFS_GLOCK();
99                 if (code != tcount) {
100                     goto done;
101                 }
102                 xfered = tcount;
103             }
104             setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) * offp, count,
105                       UIO_READ, AFS_UIOSYS);
106             code = afs_read(vcp, &tuio, credp, 0, 0, 0);
107             xfered += count - tuio.uio_resid;
108             if (code != 0) {
109                 afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER,
110                            vcp, ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, -1,
111                            ICL_TYPE_INT32, code);
112                 code = xfered;
113                 *offp += count - tuio.uio_resid;
114             } else {
115                 code = xfered;
116                 *offp += count;
117             }
118           done:
119                 ;
120         } else {
121 #endif /* AFS_64BIT_CLIENT */
122             osi_FlushPages(vcp, credp); /* ensure stale pages are gone */
123             AFS_GUNLOCK();
124             code = generic_file_read(fp, buf, count, offp);
125             AFS_GLOCK();
126 #ifdef AFS_64BIT_CLIENT
127         }
128 #endif /* AFS_64BIT_CLIENT */
129     }
130
131     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
132                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
133                code);
134
135     AFS_GUNLOCK();
136     crfree(credp);
137     return code;
138 }
139
140
141 /* Now we have integrated VM for writes as well as reads. generic_file_write
142  * also takes care of re-positioning the pointer if file is open in append
143  * mode. Call fake open/close to ensure we do writes of core dumps.
144  */
145 static ssize_t
146 afs_linux_write(struct file *fp, const char *buf, size_t count, loff_t * offp)
147 {
148     ssize_t code = 0;
149     int code2 = 0;
150     struct vcache *vcp = ITOAFS(fp->f_dentry->d_inode);
151     struct vrequest treq;
152     cred_t *credp = crref();
153     afs_offs_t toffs;
154
155     AFS_GLOCK();
156
157     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
158                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
159                (fp->f_flags & O_APPEND) ? 99998 : 99999);
160
161
162     /* get a validated vcache entry */
163     code = (ssize_t) afs_InitReq(&treq, credp);
164     if (!code)
165         code = (ssize_t) afs_VerifyVCache(vcp, &treq);
166
167     ObtainWriteLock(&vcp->lock, 529);
168     afs_FakeOpen(vcp);
169     ReleaseWriteLock(&vcp->lock);
170     if (code)
171         code = -code;
172     else {
173 #ifdef AFS_64BIT_CLIENT
174         toffs = *offp;
175         if (fp->f_flags & O_APPEND)
176             toffs += vcp->m.Length;
177         if (toffs + count > afs_vmMappingEnd) {
178             uio_t tuio;
179             struct iovec iov;
180             afs_size_t oldOffset = *offp;
181             afs_int32 xfered = 0;
182
183             if (toffs < afs_vmMappingEnd) {
184                 /* special case of a buffer crossing the VM mapping end */
185                 afs_int32 tcount = afs_vmMappingEnd - *offp;
186                 count -= tcount;
187                 AFS_GUNLOCK();
188                 code = generic_file_write(fp, buf, tcount, offp);
189                 AFS_GLOCK();
190                 if (code != tcount) {
191                     goto done;
192                 }
193                 xfered = tcount;
194                 toffs += tcount;
195             }
196             setup_uio(&tuio, &iov, buf + xfered, (afs_offs_t) toffs, count,
197                       UIO_WRITE, AFS_UIOSYS);
198             code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
199             xfered += count - tuio.uio_resid;
200             if (code != 0) {
201                 code = xfered;
202                 *offp += count - tuio.uio_resid;
203             } else {
204                 /* Purge dirty chunks of file if there are too many dirty chunks.
205                  * Inside the write loop, we only do this at a chunk boundary.
206                  * Clean up partial chunk if necessary at end of loop.
207                  */
208                 if (AFS_CHUNKBASE(tuio.afsio_offset) !=
209                     AFS_CHUNKBASE(oldOffset)) {
210                     ObtainWriteLock(&vcp->lock, 402);
211                     code = afs_DoPartialWrite(vcp, &treq);
212                     vcp->states |= CDirty;
213                     ReleaseWriteLock(&vcp->lock);
214                 }
215                 code = xfered;
216                 *offp += count;
217                 toffs += count;
218                 ObtainWriteLock(&vcp->lock, 400);
219                 vcp->m.Date = osi_Time();       /* Set file date (for ranlib) */
220                 /* extend file */
221                 if (!(fp->f_flags & O_APPEND) && toffs > vcp->m.Length) {
222                     vcp->m.Length = toffs;
223                 }
224                 ReleaseWriteLock(&vcp->lock);
225             }
226           done:
227                 ;
228         } else {
229 #endif /* AFS_64BIT_CLIENT */
230             AFS_GUNLOCK();
231             code = generic_file_write(fp, buf, count, offp);
232             AFS_GLOCK();
233 #ifdef AFS_64BIT_CLIENT
234         }
235 #endif /* AFS_64BIT_CLIENT */
236     }
237
238     ObtainWriteLock(&vcp->lock, 530);
239     vcp->m.Date = osi_Time();   /* set modification time */
240     afs_FakeClose(vcp, credp);
241     if (code >= 0)
242         code2 = afs_DoPartialWrite(vcp, &treq);
243     if (code2 && code >= 0)
244         code = (ssize_t) - code2;
245     ReleaseWriteLock(&vcp->lock);
246
247     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
248                ICL_TYPE_OFFSET, offp, ICL_TYPE_INT32, count, ICL_TYPE_INT32,
249                code);
250
251     AFS_GUNLOCK();
252     crfree(credp);
253     return code;
254 }
255
256 extern int BlobScan(struct dcache * afile, afs_int32 ablob);
257
258 /* This is a complete rewrite of afs_readdir, since we can make use of
259  * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
260  * handling and use of bulkstats will need to be reflected here as well.
261  */
262 static int
263 afs_linux_readdir(struct file *fp, void *dirbuf, filldir_t filldir)
264 {
265     extern struct DirEntry *afs_dir_GetBlob();
266     struct vcache *avc = ITOAFS(FILE_INODE(fp));
267     struct vrequest treq;
268     register struct dcache *tdc;
269     int code;
270     int offset;
271     int dirpos;
272     struct DirEntry *de;
273     ino_t ino;
274     int len;
275     afs_size_t origOffset, tlen;
276     cred_t *credp = crref();
277     struct afs_fakestat_state fakestat;
278
279 #if defined(AFS_LINUX26_ENV)
280     lock_kernel();
281 #endif
282     AFS_GLOCK();
283     AFS_STATCNT(afs_readdir);
284
285     code = afs_InitReq(&treq, credp);
286     crfree(credp);
287     if (code)
288         goto out1;
289
290     afs_InitFakeStat(&fakestat);
291     code = afs_EvalFakeStat(&avc, &fakestat, &treq);
292     if (code)
293         goto out;
294
295     /* update the cache entry */
296   tagain:
297     code = afs_VerifyVCache(avc, &treq);
298     if (code)
299         goto out;
300
301     /* get a reference to the entire directory */
302     tdc = afs_GetDCache(avc, (afs_size_t) 0, &treq, &origOffset, &tlen, 1);
303     len = tlen;
304     if (!tdc) {
305         code = -ENOENT;
306         goto out;
307     }
308     ObtainReadLock(&avc->lock);
309     ObtainReadLock(&tdc->lock);
310     /*
311      * Make sure that the data in the cache is current. There are two
312      * cases we need to worry about:
313      * 1. The cache data is being fetched by another process.
314      * 2. The cache data is no longer valid
315      */
316     while ((avc->states & CStatd)
317            && (tdc->dflags & DFFetching)
318            && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
319         ReleaseReadLock(&tdc->lock);
320         ReleaseReadLock(&avc->lock);
321         afs_osi_Sleep(&tdc->validPos);
322         ObtainReadLock(&avc->lock);
323         ObtainReadLock(&tdc->lock);
324     }
325     if (!(avc->states & CStatd)
326         || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
327         ReleaseReadLock(&tdc->lock);
328         ReleaseReadLock(&avc->lock);
329         afs_PutDCache(tdc);
330         goto tagain;
331     }
332
333     /* Fill in until we get an error or we're done. This implementation
334      * takes an offset in units of blobs, rather than bytes.
335      */
336     code = 0;
337     offset = (int) fp->f_pos;
338     while (1) {
339         dirpos = BlobScan(tdc, offset);
340         if (!dirpos)
341             break;
342
343         de = afs_dir_GetBlob(tdc, dirpos);
344         if (!de)
345             break;
346
347         ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
348         ino &= 0x7fffffff;      /* Assumes 32 bit ino_t ..... */
349         if (de->name)
350             len = strlen(de->name);
351         else {
352             printf("afs_linux_readdir: afs_dir_GetBlob failed, null name (inode %lx, dirpos %d)\n", 
353                    (unsigned long)&tdc->f.inode, dirpos);
354             DRelease((struct buffer *) de, 0);
355             afs_PutDCache(tdc);
356             ReleaseReadLock(&avc->lock);
357             code = -ENOENT;
358             goto out;
359         }
360
361         /* filldir returns -EINVAL when the buffer is full. */
362 #if defined(AFS_LINUX26_ENV) || ((defined(AFS_LINUX24_ENV) || defined(pgoff2loff)) && defined(DECLARE_FSTYPE))
363         {
364             unsigned int type = DT_UNKNOWN;
365             struct VenusFid afid;
366             struct vcache *tvc;
367             int vtype;
368             afid.Cell = avc->fid.Cell;
369             afid.Fid.Volume = avc->fid.Fid.Volume;
370             afid.Fid.Vnode = ntohl(de->fid.vnode);
371             afid.Fid.Unique = ntohl(de->fid.vunique);
372             if ((avc->states & CForeign) == 0 && (ntohl(de->fid.vnode) & 1)) {
373                 type = DT_DIR;
374             } else if ((tvc = afs_FindVCache(&afid, 0, 0))) {
375                 if (tvc->mvstat) {
376                     type = DT_DIR;
377                 } else if (((tvc->states) & (CStatd | CTruth))) {
378                     /* CTruth will be set if the object has
379                      *ever* been statd */
380                     vtype = vType(tvc);
381                     if (vtype == VDIR)
382                         type = DT_DIR;
383                     else if (vtype == VREG)
384                         type = DT_REG;
385                     /* Don't do this until we're sure it can't be a mtpt */
386                     /* else if (vtype == VLNK)
387                      * type=DT_LNK; */
388                     /* what other types does AFS support? */
389                 }
390                 /* clean up from afs_FindVCache */
391                 afs_PutVCache(tvc);
392             }
393             code = (*filldir) (dirbuf, de->name, len, offset, ino, type);
394         }
395 #else
396         code = (*filldir) (dirbuf, de->name, len, offset, ino);
397 #endif
398         DRelease((struct buffer *)de, 0);
399         if (code)
400             break;
401         offset = dirpos + 1 + ((len + 16) >> 5);
402     }
403     /* If filldir didn't fill in the last one this is still pointing to that
404      * last attempt.
405      */
406     fp->f_pos = (loff_t) offset;
407
408     ReleaseReadLock(&tdc->lock);
409     afs_PutDCache(tdc);
410     ReleaseReadLock(&avc->lock);
411     code = 0;
412
413 out:
414     afs_PutFakeStat(&fakestat);
415 out1:
416     AFS_GUNLOCK();
417 #if defined(AFS_LINUX26_ENV)
418     unlock_kernel();
419 #endif
420     return code;
421 }
422
423
424 /* in afs_pioctl.c */
425 extern int afs_xioctl(struct inode *ip, struct file *fp, unsigned int com,
426                       unsigned long arg);
427
428 #if defined(HAVE_UNLOCKED_IOCTL) || defined(HAVE_COMPAT_IOCTL)
429 static long afs_unlocked_xioctl(struct file *fp, unsigned int com,
430                                unsigned long arg) {
431     return afs_xioctl(FILE_INODE(fp), fp, com, arg);
432
433 }
434 #endif
435
436 /* We need to detect unmap's after close. To do that, we need our own
437  * vm_operations_struct's. And we need to set them up for both the
438  * private and shared mappings. The fun part is that these are all static
439  * so we'll have to initialize on the fly!
440  */
441 static struct vm_operations_struct afs_private_mmap_ops;
442 static int afs_private_mmap_ops_inited = 0;
443 static struct vm_operations_struct afs_shared_mmap_ops;
444 static int afs_shared_mmap_ops_inited = 0;
445
446 void
447 afs_linux_vma_close(struct vm_area_struct *vmap)
448 {
449     struct vcache *vcp;
450     cred_t *credp;
451     int need_unlock = 0;
452
453     if (!vmap->vm_file)
454         return;
455
456     vcp = ITOAFS(FILE_INODE(vmap->vm_file));
457     if (!vcp)
458         return;
459
460     AFS_GLOCK();
461     afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE, ICL_TYPE_POINTER, vcp,
462                ICL_TYPE_INT32, vcp->mapcnt, ICL_TYPE_INT32, vcp->opens,
463                ICL_TYPE_INT32, vcp->execsOrWriters);
464     if ((&vcp->lock)->excl_locked == 0 || (&vcp->lock)->pid_writer == MyPidxx) {
465         ObtainWriteLock(&vcp->lock, 532);
466         need_unlock = 1;
467     } else
468         printk("AFS_VMA_CLOSE(%d): Skipping Already locked vcp=%p vmap=%p\n",
469                MyPidxx, &vcp, &vmap);
470     if (vcp->mapcnt) {
471         vcp->mapcnt--;
472         if (need_unlock)
473             ReleaseWriteLock(&vcp->lock);
474         if (!vcp->mapcnt) {
475             if (need_unlock && vcp->execsOrWriters < 2) {
476                 credp = crref();
477                 (void)afs_close(vcp, vmap->vm_file->f_flags, credp);
478                 /* only decrement the execsOrWriters flag if this is not a
479                  * writable file. */
480                 if (!(vcp->states & CRO) )
481                     if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
482                         vcp->execsOrWriters--;
483                 vcp->states &= ~CMAPPED;
484                 crfree(credp);
485             } else if ((vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
486                 vcp->execsOrWriters--;
487             /* If we did not have the lock */
488             if (!need_unlock) {
489                 vcp->mapcnt++;
490                 if (!vcp->execsOrWriters)
491                     vcp->execsOrWriters = 1;
492             }
493         }
494     } else {
495         if (need_unlock)
496             ReleaseWriteLock(&vcp->lock);
497     }
498
499     AFS_GUNLOCK();
500 }
501
502 static int
503 afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
504 {
505     struct vcache *vcp = ITOAFS(FILE_INODE(fp));
506     cred_t *credp = crref();
507     struct vrequest treq;
508     int code;
509
510     AFS_GLOCK();
511 #if defined(AFS_LINUX24_ENV)
512     afs_Trace3(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
513                ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
514                vmap->vm_end - vmap->vm_start);
515 #else
516     afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
517                ICL_TYPE_POINTER, vmap->vm_start, ICL_TYPE_INT32,
518                vmap->vm_end - vmap->vm_start, ICL_TYPE_INT32,
519                vmap->vm_offset);
520 #endif
521
522     /* get a validated vcache entry */
523     code = afs_InitReq(&treq, credp);
524     if (!code)
525         code = afs_VerifyVCache(vcp, &treq);
526
527     if (!code && (vcp->states & CRO) && 
528         (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
529         code = EACCES;
530
531     if (code)
532         code = -code;
533     else {
534         osi_FlushPages(vcp, credp);     /* ensure stale pages are gone */
535
536         AFS_GUNLOCK();
537         code = generic_file_mmap(fp, vmap);
538         AFS_GLOCK();
539     }
540
541     if (code == 0) {
542         ObtainWriteLock(&vcp->lock, 531);
543         /* Set out vma ops so we catch the close. The following test should be
544          * the same as used in generic_file_mmap.
545          */
546         if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
547             if (!afs_shared_mmap_ops_inited) {
548                 afs_shared_mmap_ops_inited = 1;
549                 afs_shared_mmap_ops = *vmap->vm_ops;
550                 afs_shared_mmap_ops.close = afs_linux_vma_close;
551             }
552             vmap->vm_ops = &afs_shared_mmap_ops;
553         } else {
554             if (!afs_private_mmap_ops_inited) {
555                 afs_private_mmap_ops_inited = 1;
556                 afs_private_mmap_ops = *vmap->vm_ops;
557                 afs_private_mmap_ops.close = afs_linux_vma_close;
558             }
559             vmap->vm_ops = &afs_private_mmap_ops;
560         }
561
562
563         /* Add an open reference on the first mapping. */
564         if (vcp->mapcnt == 0) {
565             if (!(vcp->states & CRO))
566                 vcp->execsOrWriters++;
567             vcp->opens++;
568             vcp->states |= CMAPPED;
569         }
570         ReleaseWriteLock(&vcp->lock);
571         vcp->mapcnt++;
572     }
573
574     AFS_GUNLOCK();
575     crfree(credp);
576     return code;
577 }
578
579 int
580 afs_linux_open(struct inode *ip, struct file *fp)
581 {
582     int code;
583     cred_t *credp = crref();
584
585 #ifdef AFS_LINUX24_ENV
586     lock_kernel();
587 #endif
588     AFS_GLOCK();
589     code = afs_open((struct vcache **)&ip, fp->f_flags, credp);
590     AFS_GUNLOCK();
591 #ifdef AFS_LINUX24_ENV
592     unlock_kernel();
593 #endif
594
595     crfree(credp);
596     return -code;
597 }
598
599 static int
600 afs_linux_release(struct inode *ip, struct file *fp)
601 {
602     struct vcache *vcp = ITOAFS(ip);
603     cred_t *credp = crref();
604     int code = 0;
605
606 #ifdef AFS_LINUX24_ENV
607     lock_kernel();
608 #endif
609     AFS_GLOCK();
610     code = afs_close(vcp, fp->f_flags, credp);
611     AFS_GUNLOCK();
612 #ifdef AFS_LINUX24_ENV
613     unlock_kernel();
614 #endif
615
616     crfree(credp);
617     return -code;
618 }
619
620 #if defined(AFS_LINUX24_ENV)
621 static int
622 afs_linux_fsync(struct file *fp, struct dentry *dp, int datasync)
623 #else
624 static int
625 afs_linux_fsync(struct file *fp, struct dentry *dp)
626 #endif
627 {
628     int code;
629     struct inode *ip = FILE_INODE(fp);
630     cred_t *credp = crref();
631
632 #ifdef AFS_LINUX24_ENV
633     lock_kernel();
634 #endif
635     AFS_GLOCK();
636     code = afs_fsync(ITOAFS(ip), credp);
637     AFS_GUNLOCK();
638 #ifdef AFS_LINUX24_ENV
639     unlock_kernel();
640 #endif
641     crfree(credp);
642     return -code;
643
644 }
645
646
647 static int
648 afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
649 {
650     int code = 0;
651     struct vcache *vcp = ITOAFS(FILE_INODE(fp));
652     cred_t *credp = crref();
653     struct AFS_FLOCK flock;
654     /* Convert to a lock format afs_lockctl understands. */
655     memset((char *)&flock, 0, sizeof(flock));
656     flock.l_type = flp->fl_type;
657     flock.l_pid = flp->fl_pid;
658     flock.l_whence = 0;
659     flock.l_start = flp->fl_start;
660     flock.l_len = flp->fl_end - flp->fl_start;
661
662     /* Safe because there are no large files, yet */
663 #if defined(F_GETLK64) && (F_GETLK != F_GETLK64)
664     if (cmd == F_GETLK64)
665         cmd = F_GETLK;
666     else if (cmd == F_SETLK64)
667         cmd = F_SETLK;
668     else if (cmd == F_SETLKW64)
669         cmd = F_SETLKW;
670 #endif /* F_GETLK64 && F_GETLK != F_GETLK64 */
671
672     AFS_GLOCK();
673     code = afs_lockctl(vcp, &flock, cmd, credp);
674     AFS_GUNLOCK();
675
676     /* Convert flock back to Linux's file_lock */
677     flp->fl_type = flock.l_type;
678     flp->fl_pid = flock.l_pid;
679     flp->fl_start = flock.l_start;
680     flp->fl_end = flock.l_start + flock.l_len;
681
682     crfree(credp);
683     return -code;
684
685 }
686
687 /* afs_linux_flush
688  * essentially the same as afs_fsync() but we need to get the return
689  * code for the sys_close() here, not afs_linux_release(), so call
690  * afs_StoreAllSegments() with AFS_LASTSTORE
691  */
692 int
693 afs_linux_flush(struct file *fp)
694 {
695     struct vrequest treq;
696     struct vcache *vcp = ITOAFS(FILE_INODE(fp));
697     cred_t *credp = crref();
698     int code;
699
700     AFS_GLOCK();
701
702     code = afs_InitReq(&treq, credp);
703     if (code)
704         goto out;
705
706     ObtainSharedLock(&vcp->lock, 535);
707     if (vcp->execsOrWriters > 0) {
708         UpgradeSToWLock(&vcp->lock, 536);
709         code = afs_StoreAllSegments(vcp, &treq, AFS_SYNC | AFS_LASTSTORE);
710         ConvertWToSLock(&vcp->lock);
711     }
712     code = afs_CheckCode(code, &treq, 54);
713     ReleaseSharedLock(&vcp->lock);
714
715 out:
716     AFS_GUNLOCK();
717
718     crfree(credp);
719     return -code;
720 }
721
722 #if !defined(AFS_LINUX24_ENV)
723 /* Not allowed to directly read a directory. */
724 ssize_t
725 afs_linux_dir_read(struct file * fp, char *buf, size_t count, loff_t * ppos)
726 {
727     return -EISDIR;
728 }
729 #endif
730
731
732
733 struct file_operations afs_dir_fops = {
734 #if !defined(AFS_LINUX24_ENV)
735   .read =       afs_linux_dir_read,
736   .lock =       afs_linux_lock,
737   .fsync =      afs_linux_fsync,
738 #else
739   .read =       generic_read_dir,
740 #endif
741   .readdir =    afs_linux_readdir,
742 #ifdef HAVE_UNLOCKED_IOCTL
743   .unlocked_ioctl = afs_unlocked_xioctl,
744 #else
745   .ioctl =      afs_xioctl,
746 #endif
747 #ifdef HAVE_COMPAT_IOCTL
748   .compat_ioctl = afs_unlocked_xioctl,
749 #endif
750   .open =       afs_linux_open,
751   .release =    afs_linux_release,
752 };
753
754 struct file_operations afs_file_fops = {
755   .read =       afs_linux_read,
756   .write =      afs_linux_write,
757 #ifdef HAVE_UNLOCKED_IOCTL
758   .unlocked_ioctl = afs_unlocked_xioctl,
759 #else
760   .ioctl =      afs_xioctl,
761 #endif
762 #ifdef HAVE_COMPAT_IOCTL
763   .compat_ioctl = afs_unlocked_xioctl,
764 #endif
765   .mmap =       afs_linux_mmap,
766   .open =       afs_linux_open,
767   .flush =      afs_linux_flush,
768 #ifdef AFS_LINUX26_ENV
769   .sendfile =   generic_file_sendfile,
770 #endif
771   .release =    afs_linux_release,
772   .fsync =      afs_linux_fsync,
773   .lock =       afs_linux_lock,
774 };
775
776
777 /**********************************************************************
778  * AFS Linux dentry operations
779  **********************************************************************/
780
781 /* afs_linux_revalidate
782  * Ensure vcache is stat'd before use. Return 0 if entry is valid.
783  */
784 static int
785 afs_linux_revalidate(struct dentry *dp)
786 {
787     int code;
788     cred_t *credp;
789     struct vrequest treq;
790     struct vcache *vcp = ITOAFS(dp->d_inode);
791     struct vcache *rootvp = NULL;
792
793 #ifdef AFS_LINUX24_ENV
794     lock_kernel();
795 #endif
796     AFS_GLOCK();
797
798     if (afs_fakestat_enable && vcp->mvstat == 1 && vcp->mvid
799         && (vcp->states & CMValid) && (vcp->states & CStatd)) {
800         ObtainSharedLock(&afs_xvcache, 680);
801         rootvp = afs_FindVCache(vcp->mvid, 0, 0);
802         ReleaseSharedLock(&afs_xvcache);
803     }
804
805     /* Make this a fast path (no crref), since it's called so often. */
806     if (vcp->states & CStatd) {
807         if (*dp->d_name.name != '/' && vcp->mvstat == 2)        /* root vnode */
808             check_bad_parent(dp);       /* check and correct mvid */
809         if (rootvp)
810             vcache2fakeinode(rootvp, vcp);
811         else
812             vcache2inode(vcp);
813         if (rootvp)
814             afs_PutVCache(rootvp);
815         AFS_GUNLOCK();
816 #ifdef AFS_LINUX24_ENV
817         unlock_kernel();
818 #endif
819         return 0;
820     }
821
822     credp = crref();
823     code = afs_InitReq(&treq, credp);
824     if (!code)
825         code = afs_VerifyVCache(vcp, &treq);
826
827     AFS_GUNLOCK();
828 #ifdef AFS_LINUX24_ENV
829     unlock_kernel();
830 #endif
831     crfree(credp);
832
833     return -code;
834 }
835
836 #if defined(AFS_LINUX26_ENV)
837 static int
838 afs_linux_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
839 {
840         int err = afs_linux_revalidate(dentry);
841         if (!err)
842                 generic_fillattr(dentry->d_inode, stat);
843         return err;
844 }
845 #endif
846
847 /* Validate a dentry. Return 1 if unchanged, 0 if VFS layer should re-evaluate.
848  * In kernels 2.2.10 and above, we are passed an additional flags var which
849  * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
850  * we are advised to follow the entry if it is a link or to make sure that 
851  * it is a directory. But since the kernel itself checks these possibilities
852  * later on, we shouldn't have to do it until later. Perhaps in the future..
853  */
854 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
855 #ifdef DOP_REVALIDATE_TAKES_NAMEIDATA
856 static int
857 afs_linux_dentry_revalidate(struct dentry *dp, struct nameidata *nd)
858 #else
859 static int
860 afs_linux_dentry_revalidate(struct dentry *dp, int flags)
861 #endif
862 #else
863 static int
864 afs_linux_dentry_revalidate(struct dentry *dp)
865 #endif
866 {
867     struct vrequest treq;
868     cred_t *credp = NULL;
869     struct vcache *vcp, *pvcp, *tvc = NULL;
870     int valid;
871
872 #ifdef AFS_LINUX24_ENV
873     lock_kernel();
874 #endif
875     AFS_GLOCK();
876
877     vcp = ITOAFS(dp->d_inode);
878     pvcp = ITOAFS(dp->d_parent->d_inode);               /* dget_parent()? */
879
880     if (vcp) {
881
882         /* If it's the AFS root no chance it needs revalidating */
883         if (vcp == afs_globalVp)
884             goto good_dentry;
885
886         /* check_bad_parent()? */
887
888 #ifdef notdef
889         /* If the last looker changes, we should make sure the current
890          * looker still has permission to examine this file.  This would
891          * always require a crref() which would be "slow".
892          */
893         if (vcp->last_looker != treq.uid) {
894             if (!afs_AccessOK(vcp, (vType(vcp) == VREG) ? PRSFS_READ : PRSFS_LOOKUP, &treq, CHECK_MODE_BITS))
895                 goto bad_dentry;
896
897             vcp->last_looker = treq.uid;
898         }
899 #endif
900
901         /* If the parent's DataVersion has changed or the vnode
902          * is longer valid, we need to do a full lookup.  VerifyVCache
903          * isn't enough since the vnode may have been renamed.
904          */
905         if (hgetlo(pvcp->m.DataVersion) > dp->d_time || !(vcp->states & CStatd)) {
906
907             credp = crref();
908             if (afs_InitReq(&treq, credp))
909                 goto bad_dentry;
910             afs_lookup(pvcp, dp->d_name.name, &tvc, credp);
911             if (!tvc || tvc != vcp)
912                 goto bad_dentry;
913             if (afs_VerifyVCache(vcp, &treq))   /* update inode attributes */
914                 goto bad_dentry;
915
916             dp->d_time = hgetlo(pvcp->m.DataVersion);
917         }
918
919     } else {
920         if (hgetlo(pvcp->m.DataVersion) > dp->d_time)
921             goto bad_dentry;
922
923         /* No change in parent's DataVersion so this negative
924          * lookup is still valid.
925          */
926     }
927
928   good_dentry:
929     valid = 1;
930
931   done:
932     /* Clean up */
933     if (tvc)
934         afs_PutVCache(tvc);
935     AFS_GUNLOCK();
936     if (credp)
937         crfree(credp);
938
939     if (!valid) {
940         shrink_dcache_parent(dp);
941         d_drop(dp);
942     }
943 #ifdef AFS_LINUX24_ENV
944     unlock_kernel();
945 #endif
946     return valid;
947
948   bad_dentry:
949     valid = 0;
950     goto done;
951 }
952
953 #if !defined(AFS_LINUX24_ENV)
954 /* afs_dentry_iput */
955 static void
956 afs_dentry_iput(struct dentry *dp, struct inode *ip)
957 {
958     osi_iput(ip);
959 }
960 #endif
961
962 static int
963 afs_dentry_delete(struct dentry *dp)
964 {
965     if (dp->d_inode && (ITOAFS(dp->d_inode)->states & CUnlinked))
966         return 1;               /* bad inode? */
967
968     return 0;
969 }
970
971 struct dentry_operations afs_dentry_operations = {
972   .d_revalidate =       afs_linux_dentry_revalidate,
973   .d_delete =           afs_dentry_delete,
974 #if !defined(AFS_LINUX24_ENV)
975   .d_iput =             afs_dentry_iput,
976 #endif
977 };
978
979 /**********************************************************************
980  * AFS Linux inode operations
981  **********************************************************************/
982
983 /* afs_linux_create
984  *
985  * Merely need to set enough of vattr to get us through the create. Note
986  * that the higher level code (open_namei) will take care of any tuncation
987  * explicitly. Exclusive open is also taken care of in open_namei.
988  *
989  * name is in kernel space at this point.
990  */
991 #ifdef IOP_CREATE_TAKES_NAMEIDATA
992 int
993 afs_linux_create(struct inode *dip, struct dentry *dp, int mode,
994                  struct nameidata *nd)
995 #else
996 int
997 afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
998 #endif
999 {
1000     int code;
1001     cred_t *credp = crref();
1002     struct vattr vattr;
1003     const char *name = dp->d_name.name;
1004     struct inode *ip;
1005
1006     VATTR_NULL(&vattr);
1007     vattr.va_mode = mode;
1008
1009 #if defined(AFS_LINUX26_ENV)
1010     lock_kernel();
1011 #endif
1012     AFS_GLOCK();
1013     code =
1014         afs_create(ITOAFS(dip), (char *)name, &vattr, NONEXCL, mode,
1015                    (struct vcache **)&ip, credp);
1016
1017     if (!code) {
1018         vattr2inode(ip, &vattr);
1019         /* Reset ops if symlink or directory. */
1020 #if defined(AFS_LINUX24_ENV)
1021         if (S_ISREG(ip->i_mode)) {
1022             ip->i_op = &afs_file_iops;
1023             ip->i_fop = &afs_file_fops;
1024             ip->i_data.a_ops = &afs_file_aops;
1025         } else if (S_ISDIR(ip->i_mode)) {
1026             ip->i_op = &afs_dir_iops;
1027             ip->i_fop = &afs_dir_fops;
1028         } else if (S_ISLNK(ip->i_mode)) {
1029             ip->i_op = &afs_symlink_iops;
1030             ip->i_data.a_ops = &afs_symlink_aops;
1031             ip->i_mapping = &ip->i_data;
1032         } else
1033             printk("afs_linux_create: FIXME\n");
1034 #else
1035         if (S_ISDIR(ip->i_mode))
1036             ip->i_op = &afs_dir_iops;
1037         else if (S_ISLNK(ip->i_mode))
1038             ip->i_op = &afs_symlink_iops;
1039 #endif
1040
1041         dp->d_op = &afs_dentry_operations;
1042         dp->d_time = hgetlo(ITOAFS(dip)->m.DataVersion);
1043         d_instantiate(dp, ip);
1044     }
1045
1046     AFS_GUNLOCK();
1047 #if defined(AFS_LINUX26_ENV)
1048     unlock_kernel();
1049 #endif
1050     crfree(credp);
1051     return -code;
1052 }
1053
1054 /* afs_linux_lookup */
1055 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1056 #ifdef IOP_LOOKUP_TAKES_NAMEIDATA
1057 struct dentry *
1058 afs_linux_lookup(struct inode *dip, struct dentry *dp,
1059                  struct nameidata *nd)
1060 #else
1061 struct dentry *
1062 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1063 #endif
1064 #else
1065 int
1066 afs_linux_lookup(struct inode *dip, struct dentry *dp)
1067 #endif
1068 {
1069     int code = 0;
1070     cred_t *credp = crref();
1071     struct vcache *vcp = NULL;
1072     const char *comp = dp->d_name.name;
1073 #if 1
1074     struct dentry *res = 0;
1075 #endif
1076
1077 #if defined(AFS_LINUX26_ENV)
1078     lock_kernel();
1079 #endif
1080     AFS_GLOCK();
1081     code = afs_lookup(ITOAFS(dip), comp, &vcp, credp);
1082     AFS_GUNLOCK();
1083     
1084     if (vcp) {
1085         struct inode *ip = AFSTOI(vcp);
1086         /* Reset ops if symlink or directory. */
1087 #if defined(AFS_LINUX24_ENV)
1088         if (S_ISREG(ip->i_mode)) {
1089             ip->i_op = &afs_file_iops;
1090             ip->i_fop = &afs_file_fops;
1091             ip->i_data.a_ops = &afs_file_aops;
1092         } else if (S_ISDIR(ip->i_mode)) {
1093             ip->i_op = &afs_dir_iops;
1094             ip->i_fop = &afs_dir_fops;
1095             d_prune_aliases(ip);
1096             res = d_find_alias(ip);
1097         } else if (S_ISLNK(ip->i_mode)) {
1098             ip->i_op = &afs_symlink_iops;
1099             ip->i_data.a_ops = &afs_symlink_aops;
1100             ip->i_mapping = &ip->i_data;
1101         } else
1102             printk
1103                 ("afs_linux_lookup: ip->i_mode 0x%x  dp->d_name.name %s  code %d\n",
1104                  ip->i_mode, dp->d_name.name, code);
1105 #ifdef STRUCT_INODE_HAS_I_SECURITY
1106        if (ip->i_security == NULL) {
1107            if (security_inode_alloc(ip))
1108                panic("afs_linux_lookup: Cannot allocate inode security");
1109        }
1110 #endif
1111 #else
1112         if (S_ISDIR(ip->i_mode))
1113             ip->i_op = &afs_dir_iops;
1114         else if (S_ISLNK(ip->i_mode))
1115             ip->i_op = &afs_symlink_iops;
1116 #endif
1117     }
1118     dp->d_op = &afs_dentry_operations;
1119     dp->d_time = hgetlo(ITOAFS(dip)->m.DataVersion);
1120 #if defined(AFS_LINUX24_ENV)
1121     if (res) {
1122         if (d_unhashed(res))
1123             d_rehash(res);
1124     } else
1125 #endif
1126     d_add(dp, AFSTOI(vcp));
1127
1128 #if defined(AFS_LINUX26_ENV)
1129     unlock_kernel();
1130 #endif
1131     crfree(credp);
1132
1133     /* It's ok for the file to not be found. That's noted by the caller by
1134      * seeing that the dp->d_inode field is NULL.
1135      */
1136 #if defined(AFS_LINUX24_ENV)
1137     if (code == 0)
1138         return res;
1139 #endif
1140 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
1141     if (code == ENOENT)
1142         return ERR_PTR(0);
1143     else
1144         return ERR_PTR(-code);
1145 #else
1146     if (code == ENOENT)
1147         code = 0;
1148     return -code;
1149 #endif
1150 }
1151
1152 int
1153 afs_linux_link(struct dentry *olddp, struct inode *dip, struct dentry *newdp)
1154 {
1155     int code;
1156     cred_t *credp = crref();
1157     const char *name = newdp->d_name.name;
1158     struct inode *oldip = olddp->d_inode;
1159
1160     /* If afs_link returned the vnode, we could instantiate the
1161      * dentry. Since it's not, we drop this one and do a new lookup.
1162      */
1163     d_drop(newdp);
1164
1165     AFS_GLOCK();
1166     code = afs_link(ITOAFS(oldip), ITOAFS(dip), name, credp);
1167
1168     AFS_GUNLOCK();
1169     crfree(credp);
1170     return -code;
1171 }
1172
1173 int
1174 afs_linux_unlink(struct inode *dip, struct dentry *dp)
1175 {
1176     int code = EBUSY;
1177     cred_t *credp = crref();
1178     const char *name = dp->d_name.name;
1179     struct vcache *tvc = ITOAFS(dp->d_inode);
1180
1181 #if defined(AFS_LINUX26_ENV)
1182     lock_kernel();
1183 #endif
1184     if (((VREFCOUNT(tvc) > 0) && tvc->opens > 0)
1185                                 && !(tvc->states & CUnlinked)) {
1186         struct dentry *__dp;
1187         char *__name;
1188         extern char *afs_newname();
1189
1190         __dp = NULL;
1191         __name = NULL;
1192         do {
1193             dput(__dp);
1194
1195             AFS_GLOCK();
1196             if (__name)
1197                 osi_FreeSmallSpace(__name);
1198             __name = afs_newname();
1199             AFS_GUNLOCK();
1200
1201             __dp = lookup_one_len(__name, dp->d_parent, strlen(__name));
1202                 
1203             if (IS_ERR(__dp))
1204                 goto out;
1205         } while (__dp->d_inode != NULL);
1206
1207         AFS_GLOCK();
1208         code = afs_rename(ITOAFS(dip), dp->d_name.name, ITOAFS(dip), __dp->d_name.name, credp);
1209         if (!code) {
1210             tvc->mvid = __name;
1211             crhold(credp);
1212             if (tvc->uncred) {
1213                 crfree(tvc->uncred);
1214             }
1215             tvc->uncred = credp;
1216             tvc->states |= CUnlinked;
1217         }
1218         AFS_GUNLOCK();
1219
1220         if (!code) {
1221             __dp->d_time = hgetlo(ITOAFS(dip)->m.DataVersion);
1222             d_move(dp, __dp);
1223         }
1224         dput(__dp);
1225
1226         goto out;
1227     }
1228
1229     AFS_GLOCK();
1230     code = afs_remove(ITOAFS(dip), name, credp);
1231     AFS_GUNLOCK();
1232     if (!code)
1233         d_drop(dp);
1234 out:
1235 #if defined(AFS_LINUX26_ENV)
1236     unlock_kernel();
1237 #endif
1238     crfree(credp);
1239     return -code;
1240 }
1241
1242
1243 int
1244 afs_linux_symlink(struct inode *dip, struct dentry *dp, const char *target)
1245 {
1246     int code;
1247     cred_t *credp = crref();
1248     struct vattr vattr;
1249     const char *name = dp->d_name.name;
1250
1251     /* If afs_symlink returned the vnode, we could instantiate the
1252      * dentry. Since it's not, we drop this one and do a new lookup.
1253      */
1254     d_drop(dp);
1255
1256     AFS_GLOCK();
1257     VATTR_NULL(&vattr);
1258     code = afs_symlink(ITOAFS(dip), name, &vattr, target, credp);
1259     AFS_GUNLOCK();
1260     crfree(credp);
1261     return -code;
1262 }
1263
1264 int
1265 afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
1266 {
1267     int code;
1268     cred_t *credp = crref();
1269     struct vcache *tvcp = NULL;
1270     struct vattr vattr;
1271     const char *name = dp->d_name.name;
1272
1273 #if defined(AFS_LINUX26_ENV)
1274     lock_kernel();
1275 #endif
1276     AFS_GLOCK();
1277     VATTR_NULL(&vattr);
1278     vattr.va_mask = ATTR_MODE;
1279     vattr.va_mode = mode;
1280     code = afs_mkdir(ITOAFS(dip), name, &vattr, &tvcp, credp);
1281     AFS_GUNLOCK();
1282
1283     if (tvcp) {
1284         tvcp->v.v_op = &afs_dir_iops;
1285 #if defined(AFS_LINUX24_ENV)
1286         tvcp->v.v_fop = &afs_dir_fops;
1287 #endif
1288         dp->d_op = &afs_dentry_operations;
1289         dp->d_time = hgetlo(ITOAFS(dip)->m.DataVersion);
1290         d_instantiate(dp, AFSTOI(tvcp));
1291     }
1292
1293 #if defined(AFS_LINUX26_ENV)
1294     unlock_kernel();
1295 #endif
1296     crfree(credp);
1297     return -code;
1298 }
1299
1300 int
1301 afs_linux_rmdir(struct inode *dip, struct dentry *dp)
1302 {
1303     int code;
1304     cred_t *credp = crref();
1305     const char *name = dp->d_name.name;
1306
1307 #if defined(AFS_LINUX26_ENV)
1308     lock_kernel();
1309 #endif
1310     AFS_GLOCK();
1311     code = afs_rmdir(ITOAFS(dip), name, credp);
1312     AFS_GUNLOCK();
1313
1314     /* Linux likes to see ENOTEMPTY returned from an rmdir() syscall
1315      * that failed because a directory is not empty. So, we map
1316      * EEXIST to ENOTEMPTY on linux.
1317      */
1318     if (code == EEXIST) {
1319         code = ENOTEMPTY;
1320     }
1321
1322     if (!code) {
1323         d_drop(dp);
1324     }
1325
1326 #if defined(AFS_LINUX26_ENV)
1327     unlock_kernel();
1328 #endif
1329     crfree(credp);
1330     return -code;
1331 }
1332
1333
1334
1335 int
1336 afs_linux_rename(struct inode *oldip, struct dentry *olddp,
1337                  struct inode *newip, struct dentry *newdp)
1338 {
1339     int code;
1340     cred_t *credp = crref();
1341     const char *oldname = olddp->d_name.name;
1342     const char *newname = newdp->d_name.name;
1343     struct dentry *rehash = NULL;
1344
1345 #if defined(AFS_LINUX26_ENV)
1346     /* Prevent any new references during rename operation. */
1347     lock_kernel();
1348 #endif
1349     /* Remove old and new entries from name hash. New one will change below.
1350      * While it's optimal to catch failures and re-insert newdp into hash,
1351      * it's also error prone and in that case we're already dealing with error
1352      * cases. Let another lookup put things right, if need be.
1353      */
1354 #if defined(AFS_LINUX26_ENV)
1355     if (!d_unhashed(newdp)) {
1356         d_drop(newdp);
1357         rehash = newdp;
1358     }
1359 #else
1360     if (!list_empty(&newdp->d_hash)) {
1361         d_drop(newdp);
1362         rehash = newdp;
1363     }
1364 #endif
1365
1366 #if defined(AFS_LINUX24_ENV)
1367     if (atomic_read(&olddp->d_count) > 1)
1368         shrink_dcache_parent(olddp);
1369 #endif
1370
1371     AFS_GLOCK();
1372     code = afs_rename(ITOAFS(oldip), oldname, ITOAFS(newip), newname, credp);
1373     AFS_GUNLOCK();
1374
1375     if (rehash)
1376         d_rehash(rehash);
1377
1378 #if defined(AFS_LINUX26_ENV)
1379     unlock_kernel();
1380 #endif
1381
1382     crfree(credp);
1383     return -code;
1384 }
1385
1386
1387 /* afs_linux_ireadlink 
1388  * Internal readlink which can return link contents to user or kernel space.
1389  * Note that the buffer is NOT supposed to be null-terminated.
1390  */
1391 static int
1392 afs_linux_ireadlink(struct inode *ip, char *target, int maxlen, uio_seg_t seg)
1393 {
1394     int code;
1395     cred_t *credp = crref();
1396     uio_t tuio;
1397     struct iovec iov;
1398
1399     setup_uio(&tuio, &iov, target, (afs_offs_t) 0, maxlen, UIO_READ, seg);
1400     code = afs_readlink(ITOAFS(ip), &tuio, credp);
1401     crfree(credp);
1402
1403     if (!code)
1404         return maxlen - tuio.uio_resid;
1405     else
1406         return -code;
1407 }
1408
1409 #if !defined(AFS_LINUX24_ENV)
1410 /* afs_linux_readlink 
1411  * Fill target (which is in user space) with contents of symlink.
1412  */
1413 int
1414 afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
1415 {
1416     int code;
1417     struct inode *ip = dp->d_inode;
1418
1419     AFS_GLOCK();
1420     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
1421     AFS_GUNLOCK();
1422     return code;
1423 }
1424
1425
1426 /* afs_linux_follow_link
1427  * a file system dependent link following routine.
1428  */
1429 struct dentry *
1430 afs_linux_follow_link(struct dentry *dp, struct dentry *basep,
1431                       unsigned int follow)
1432 {
1433     int code = 0;
1434     char *name;
1435     struct dentry *res;
1436
1437
1438     AFS_GLOCK();
1439     name = osi_Alloc(PATH_MAX + 1);
1440     if (!name) {
1441         AFS_GUNLOCK();
1442         dput(basep);
1443         return ERR_PTR(-EIO);
1444     }
1445
1446     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
1447     AFS_GUNLOCK();
1448
1449     if (code < 0) {
1450         dput(basep);
1451         res = ERR_PTR(code);
1452     } else {
1453         name[code] = '\0';
1454         res = lookup_dentry(name, basep, follow);
1455     }
1456
1457     AFS_GLOCK();
1458     osi_Free(name, PATH_MAX + 1);
1459     AFS_GUNLOCK();
1460     return res;
1461 }
1462 #endif
1463
1464 /* afs_linux_readpage
1465  * all reads come through here. A strategy-like read call.
1466  */
1467 int
1468 afs_linux_readpage(struct file *fp, struct page *pp)
1469 {
1470     int code;
1471     cred_t *credp = crref();
1472 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1473     char *address;
1474     afs_offs_t offset = pp->index << PAGE_CACHE_SHIFT;
1475 #else
1476     ulong address = afs_linux_page_address(pp);
1477     afs_offs_t offset = pageoff(pp);
1478 #endif
1479     uio_t tuio;
1480     struct iovec iovec;
1481     struct inode *ip = FILE_INODE(fp);
1482     int cnt = page_count(pp);
1483     struct vcache *avc = ITOAFS(ip);
1484
1485
1486 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1487     address = kmap(pp);
1488     ClearPageError(pp);
1489 #else
1490     atomic_add(1, &pp->count);
1491     set_bit(PG_locked, &pp->flags);     /* other bits? See mm.h */
1492     clear_bit(PG_error, &pp->flags);
1493 #endif
1494
1495     setup_uio(&tuio, &iovec, (char *)address, offset, PAGESIZE, UIO_READ,
1496               AFS_UIOSYS);
1497 #ifdef AFS_LINUX24_ENV
1498     lock_kernel();
1499 #endif
1500     AFS_GLOCK();
1501     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 */
1502     code = afs_rdwr(avc, &tuio, UIO_READ, 0, credp);
1503     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE, ICL_TYPE_POINTER, ip,
1504                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, cnt, ICL_TYPE_INT32,
1505                code);
1506     AFS_GUNLOCK();
1507 #ifdef AFS_LINUX24_ENV
1508     unlock_kernel();
1509 #endif
1510
1511     if (!code) {
1512         if (tuio.uio_resid)     /* zero remainder of page */
1513             memset((void *)(address + (PAGESIZE - tuio.uio_resid)), 0,
1514                    tuio.uio_resid);
1515 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1516         flush_dcache_page(pp);
1517         SetPageUptodate(pp);
1518 #else
1519         set_bit(PG_uptodate, &pp->flags);
1520 #endif
1521     }
1522
1523 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1524     kunmap(pp);
1525     UnlockPage(pp);
1526 #else
1527     clear_bit(PG_locked, &pp->flags);
1528     wake_up(&pp->wait);
1529     free_page(address);
1530 #endif
1531
1532     if (!code && AFS_CHUNKOFFSET(offset) == 0) {
1533         struct dcache *tdc;
1534         struct vrequest treq;
1535
1536         AFS_GLOCK();
1537         code = afs_InitReq(&treq, credp);
1538         if (!code && !NBObtainWriteLock(&avc->lock, 534)) {
1539             tdc = afs_FindDCache(avc, offset);
1540             if (tdc) {
1541                 if (!(tdc->mflags & DFNextStarted))
1542                     afs_PrefetchChunk(avc, tdc, credp, &treq);
1543                 afs_PutDCache(tdc);
1544             }
1545             ReleaseWriteLock(&avc->lock);
1546         }
1547         AFS_GUNLOCK();
1548     }
1549
1550     crfree(credp);
1551     return -code;
1552 }
1553
1554 #if defined(AFS_LINUX24_ENV)
1555 #ifdef AOP_WRITEPAGE_TAKES_WRITEBACK_CONTROL
1556 int
1557 afs_linux_writepage(struct page *pp, struct writeback_control *wbc)
1558 #else
1559 int
1560 afs_linux_writepage(struct page *pp)
1561 #endif
1562 {
1563     struct address_space *mapping = pp->mapping;
1564     struct inode *inode;
1565     unsigned long end_index;
1566     unsigned offset = PAGE_CACHE_SIZE;
1567     long status;
1568
1569 #if defined(AFS_LINUX26_ENV)
1570     if (PageReclaim(pp)) {
1571         return WRITEPAGE_ACTIVATE;
1572     }
1573 #else
1574     if (PageLaunder(pp)) {
1575         return(fail_writepage(pp));
1576     }
1577 #endif
1578
1579     inode = (struct inode *)mapping->host;
1580     end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1581
1582     /* easy case */
1583     if (pp->index < end_index)
1584         goto do_it;
1585     /* things got complicated... */
1586     offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
1587     /* OK, are we completely out? */
1588     if (pp->index >= end_index + 1 || !offset)
1589         return -EIO;
1590   do_it:
1591     status = afs_linux_writepage_sync(inode, pp, 0, offset);
1592     SetPageUptodate(pp);
1593     UnlockPage(pp);
1594     if (status == offset)
1595         return 0;
1596     else
1597         return status;
1598 }
1599 #endif
1600
1601 /* afs_linux_permission
1602  * Check access rights - returns error if can't check or permission denied.
1603  */
1604 #ifdef IOP_PERMISSION_TAKES_NAMEIDATA
1605 int
1606 afs_linux_permission(struct inode *ip, int mode, struct nameidata *nd)
1607 #else
1608 int
1609 afs_linux_permission(struct inode *ip, int mode)
1610 #endif
1611 {
1612     int code;
1613     cred_t *credp = crref();
1614     int tmp = 0;
1615
1616     AFS_GLOCK();
1617     if (mode & MAY_EXEC)
1618         tmp |= VEXEC;
1619     if (mode & MAY_READ)
1620         tmp |= VREAD;
1621     if (mode & MAY_WRITE)
1622         tmp |= VWRITE;
1623     code = afs_access(ITOAFS(ip), tmp, credp);
1624
1625     AFS_GUNLOCK();
1626     crfree(credp);
1627     return -code;
1628 }
1629
1630
1631 #if defined(AFS_LINUX24_ENV)
1632 int
1633 afs_linux_writepage_sync(struct inode *ip, struct page *pp,
1634                          unsigned long offset, unsigned int count)
1635 {
1636     struct vcache *vcp = ITOAFS(ip);
1637     char *buffer;
1638     afs_offs_t base;
1639     int code = 0;
1640     cred_t *credp;
1641     uio_t tuio;
1642     struct iovec iovec;
1643     int f_flags = 0;
1644
1645     buffer = kmap(pp) + offset;
1646     base = (pp->index << PAGE_CACHE_SHIFT) + offset;
1647
1648     credp = crref();
1649     lock_kernel();
1650     AFS_GLOCK();
1651     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1652                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1653                ICL_TYPE_INT32, 99999);
1654
1655     setup_uio(&tuio, &iovec, buffer, base, count, UIO_WRITE, AFS_UIOSYS);
1656
1657     code = afs_write(vcp, &tuio, f_flags, credp, 0);
1658
1659     vcache2inode(vcp);
1660
1661     if (!code
1662         && afs_stats_cmperf.cacheCurrDirtyChunks >
1663         afs_stats_cmperf.cacheMaxDirtyChunks) {
1664         struct vrequest treq;
1665
1666         ObtainWriteLock(&vcp->lock, 533);
1667         if (!afs_InitReq(&treq, credp))
1668             code = afs_DoPartialWrite(vcp, &treq);
1669         ReleaseWriteLock(&vcp->lock);
1670     }
1671     code = code ? -code : count - tuio.uio_resid;
1672
1673     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1674                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1675                ICL_TYPE_INT32, code);
1676
1677     AFS_GUNLOCK();
1678     unlock_kernel();
1679     crfree(credp);
1680     kunmap(pp);
1681
1682     return code;
1683 }
1684
1685 #else
1686 /* afs_linux_updatepage
1687  * What one would have thought was writepage - write dirty page to file.
1688  * Called from generic_file_write. buffer is still in user space. pagep
1689  * has been filled in with old data if we're updating less than a page.
1690  */
1691 int
1692 afs_linux_updatepage(struct file *fp, struct page *pp, unsigned long offset,
1693                      unsigned int count, int sync)
1694 {
1695     struct vcache *vcp = ITOAFS(FILE_INODE(fp));
1696     u8 *page_addr = (u8 *) afs_linux_page_address(pp);
1697     int code = 0;
1698     cred_t *credp;
1699     uio_t tuio;
1700     struct iovec iovec;
1701
1702     set_bit(PG_locked, &pp->flags);
1703
1704     credp = crref();
1705     AFS_GLOCK();
1706     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1707                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1708                ICL_TYPE_INT32, 99999);
1709     setup_uio(&tuio, &iovec, page_addr + offset,
1710               (afs_offs_t) (pageoff(pp) + offset), count, UIO_WRITE,
1711               AFS_UIOSYS);
1712
1713     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1714
1715     vcache2inode(vcp);
1716
1717     code = code ? -code : count - tuio.uio_resid;
1718     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1719                ICL_TYPE_POINTER, pp, ICL_TYPE_INT32, page_count(pp),
1720                ICL_TYPE_INT32, code);
1721
1722     AFS_GUNLOCK();
1723     crfree(credp);
1724
1725     clear_bit(PG_locked, &pp->flags);
1726     return code;
1727 }
1728 #endif
1729
1730 #if defined(AFS_LINUX24_ENV)
1731 static int
1732 afs_linux_commit_write(struct file *file, struct page *page, unsigned offset,
1733                        unsigned to)
1734 {
1735     int code;
1736
1737     code = afs_linux_writepage_sync(file->f_dentry->d_inode, page,
1738                                     offset, to - offset);
1739 #if !defined(AFS_LINUX26_ENV)
1740     kunmap(page);
1741 #endif
1742
1743     return code;
1744 }
1745
1746 static int
1747 afs_linux_prepare_write(struct file *file, struct page *page, unsigned from,
1748                         unsigned to)
1749 {
1750 /* sometime between 2.4.0 and 2.4.19, the callers of prepare_write began to
1751    call kmap directly instead of relying on us to do it */
1752 #if !defined(AFS_LINUX26_ENV)
1753     kmap(page);
1754 #endif
1755     return 0;
1756 }
1757
1758 extern int afs_notify_change(struct dentry *dp, struct iattr *iattrp);
1759 #endif
1760
1761 struct inode_operations afs_file_iops = {
1762 #if defined(AFS_LINUX26_ENV)
1763   .permission =         afs_linux_permission,
1764   .getattr =            afs_linux_getattr,
1765   .setattr =            afs_notify_change,
1766 #elif defined(AFS_LINUX24_ENV)
1767   .permission =         afs_linux_permission,
1768   .revalidate =         afs_linux_revalidate,
1769   .setattr =            afs_notify_change,
1770 #else
1771   .default_file_ops =   &afs_file_fops,
1772   .readpage =           afs_linux_readpage,
1773   .revalidate =         afs_linux_revalidate,
1774   .updatepage =         afs_linux_updatepage,
1775 #endif
1776 };
1777
1778 #if defined(AFS_LINUX24_ENV)
1779 struct address_space_operations afs_file_aops = {
1780   .readpage =           afs_linux_readpage,
1781   .writepage =          afs_linux_writepage,
1782   .commit_write =       afs_linux_commit_write,
1783   .prepare_write =      afs_linux_prepare_write,
1784 };
1785 #endif
1786
1787
1788 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1789  * by what sort of operation is allowed.....
1790  */
1791
1792 struct inode_operations afs_dir_iops = {
1793 #if !defined(AFS_LINUX24_ENV)
1794   .default_file_ops =   &afs_dir_fops,
1795 #else
1796   .setattr =            afs_notify_change,
1797 #endif
1798   .create =             afs_linux_create,
1799   .lookup =             afs_linux_lookup,
1800   .link =               afs_linux_link,
1801   .unlink =             afs_linux_unlink,
1802   .symlink =            afs_linux_symlink,
1803   .mkdir =              afs_linux_mkdir,
1804   .rmdir =              afs_linux_rmdir,
1805   .rename =             afs_linux_rename,
1806 #if defined(AFS_LINUX26_ENV)
1807   .getattr =            afs_linux_getattr,
1808 #else
1809   .revalidate =         afs_linux_revalidate,
1810 #endif
1811   .permission =         afs_linux_permission,
1812 };
1813
1814 /* We really need a separate symlink set of ops, since do_follow_link()
1815  * determines if it _is_ a link by checking if the follow_link op is set.
1816  */
1817 #if defined(AFS_LINUX24_ENV)
1818 static int
1819 afs_symlink_filler(struct file *file, struct page *page)
1820 {
1821     struct inode *ip = (struct inode *)page->mapping->host;
1822     char *p = (char *)kmap(page);
1823     int code;
1824
1825     lock_kernel();
1826     AFS_GLOCK();
1827     code = afs_linux_ireadlink(ip, p, PAGE_SIZE, AFS_UIOSYS);
1828     AFS_GUNLOCK();
1829
1830     if (code < 0)
1831         goto fail;
1832     p[code] = '\0';             /* null terminate? */
1833     unlock_kernel();
1834
1835     SetPageUptodate(page);
1836     kunmap(page);
1837     UnlockPage(page);
1838     return 0;
1839
1840   fail:
1841     unlock_kernel();
1842
1843     SetPageError(page);
1844     kunmap(page);
1845     UnlockPage(page);
1846     return code;
1847 }
1848
1849 struct address_space_operations afs_symlink_aops = {
1850   .readpage =   afs_symlink_filler
1851 };
1852 #endif
1853
1854 struct inode_operations afs_symlink_iops = {
1855 #if defined(AFS_LINUX24_ENV)
1856   .readlink =           page_readlink,
1857 #if defined(HAVE_KERNEL_PAGE_FOLLOW_LINK)
1858   .follow_link =        page_follow_link,
1859 #else
1860   .follow_link =        page_follow_link_light,
1861   .put_link =           page_put_link,
1862 #endif
1863   .setattr =            afs_notify_change,
1864 #else
1865   .readlink =           afs_linux_readlink,
1866   .follow_link =        afs_linux_follow_link,
1867   .permission =         afs_linux_permission,
1868   .revalidate =         afs_linux_revalidate,
1869 #endif
1870 };