Initial IBM OpenAFS 1.0 tree
[openafs.git] / src / afs / LINUX / osi_vnodeops.c
1 /* Copyright (C) 1995, 1989, 1998 Transarc Corporation - All rights reserved */
2 /*
3  * (C) COPYRIGHT IBM CORPORATION 1987, 1988
4  * LICENSED MATERIALS - PROPERTY OF IBM
5  */
6 /*
7  *
8  * Linux specific vnodeops. Also includes the glue routines required to call
9  * AFS vnodeops. The "NOTUSED" #define is used to indicate routines and
10  * calling sequences present in an ops table that we don't actually use.
11  * They are present solely for documentation purposes.
12  *
13  * So far the only truly scary part is that Linux relies on the inode cache
14  * to be up to date. Don't you dare break a callback and expect an fstat
15  * to give you meaningful information. This appears to be fixed in the 2.1
16  * development kernels. As it is we can fix this now by intercepting the 
17  * stat calls.
18  */
19
20 #include "../afs/param.h"
21 #include "../afs/sysincludes.h"
22 #include "../afs/afsincludes.h"
23 #include "../afs/afs_stats.h"
24 #include "../h/mm.h"
25 #include "../h/pagemap.h"
26
27 extern struct vcache *afs_globalVp;
28
29 extern struct dentry_operations *afs_dops;
30 extern struct inode_operations afs_dir_iops;
31 extern struct inode_operations afs_symlink_iops;
32
33
34 #ifdef NOTUSED
35 static int afs_linux_lseek(struct inode *ip, struct file *fp, off_t, int) {}
36 #endif
37
38 static ssize_t afs_linux_read(struct file *fp, char *buf, size_t count,
39                               loff_t *offp)
40 {
41     ssize_t code;
42     struct vcache *vcp = (struct vcache*)fp->f_dentry->d_inode;
43     cred_t *credp = crref();
44     struct vrequest treq;
45
46     AFS_GLOCK();
47     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
48                ICL_TYPE_INT32, (int)*offp,
49                ICL_TYPE_INT32, count,
50                ICL_TYPE_INT32, 99999);
51
52     /* get a validated vcache entry */
53     code = afs_InitReq(&treq, credp);
54     if (!code)
55         code = afs_VerifyVCache(vcp, &treq);
56
57     if (code)
58         code = -code;
59     else {
60         osi_FlushPages(vcp, credp);     /* ensure stale pages are gone */
61         AFS_GUNLOCK();
62         code = generic_file_read(fp, buf, count, offp);
63         AFS_GLOCK();
64     }
65
66     afs_Trace4(afs_iclSetp, CM_TRACE_READOP, ICL_TYPE_POINTER, vcp,
67                ICL_TYPE_INT32, (int)*offp,
68                ICL_TYPE_INT32, count,
69                ICL_TYPE_INT32, code);
70
71     AFS_GUNLOCK();
72     crfree(credp);
73     return code;
74 }
75
76
77 /* Now we have integrated VM for writes as well as reads. generic_file_write
78  * also takes care of re-positioning the pointer if file is open in append
79  * mode. Call fake open/close to ensure we do writes of core dumps.
80  */
81 static ssize_t afs_linux_write(struct file *fp, const char *buf, size_t count,
82                            loff_t *offp)
83 {
84     ssize_t code = 0;
85     int code2;
86     struct vcache *vcp = (struct vcache *)fp->f_dentry->d_inode;
87     struct vrequest treq;
88     cred_t *credp = crref();
89
90     AFS_GLOCK();
91
92     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
93                ICL_TYPE_INT32, (int)*offp, ICL_TYPE_INT32, count,
94                ICL_TYPE_INT32, (fp->f_flags & O_APPEND) ? 99998 : 99999);
95
96
97     /* get a validated vcache entry */
98     code = (ssize_t)afs_InitReq(&treq, credp);
99     if (!code)
100         code = (ssize_t)afs_VerifyVCache(vcp, &treq);
101
102     ObtainWriteLock(&vcp->lock, 529);
103     afs_FakeOpen(vcp);
104     ReleaseWriteLock(&vcp->lock);
105     AFS_GUNLOCK();
106     if (code)
107         code = -code;
108     else {
109         code = generic_file_write(fp, buf, count, offp);
110     }
111     AFS_GLOCK();
112
113     ObtainWriteLock(&vcp->lock, 530);
114     vcp->m.Date = osi_Time(); /* set modification time */
115     afs_FakeClose(vcp, credp);
116     if (code>=0)
117         code2 = afs_DoPartialWrite(vcp, &treq);
118     if (code2 && code >=0)
119         code = (ssize_t) -code2;
120     ReleaseWriteLock(&vcp->lock);
121         
122     afs_Trace4(afs_iclSetp, CM_TRACE_WRITEOP, ICL_TYPE_POINTER, vcp,
123                ICL_TYPE_INT32, (int)*offp, ICL_TYPE_INT32, count,
124                ICL_TYPE_INT32, code);
125
126     AFS_GUNLOCK();
127     crfree(credp);
128     return code;
129 }
130
131 /* This is a complete rewrite of afs_readdir, since we can make use of
132  * filldir instead of afs_readdir_move. Note that changes to vcache/dcache
133  * handling and use of bulkstats will need to be reflected here as well.
134  */
135 static int afs_linux_readdir(struct file *fp,
136                              void *dirbuf, filldir_t filldir)
137 {
138     struct vcache *avc = (struct vcache*)FILE_INODE(fp);
139     struct vrequest treq;
140     register struct dcache *tdc;
141     int code;
142     int offset;
143     int dirpos;
144     struct DirEntry *de;
145     ino_t ino;
146     int len;
147     int origOffset;
148     cred_t *credp = crref();
149
150     AFS_GLOCK();
151     AFS_STATCNT(afs_readdir);
152
153     code = afs_InitReq(&treq, credp);
154     crfree(credp);
155     if (code) {
156         AFS_GUNLOCK();
157         return -code;
158     }
159
160     /* update the cache entry */
161 tagain:
162     code = afs_VerifyVCache(avc, &treq);
163     if (code) {
164         AFS_GUNLOCK();
165         return -code;
166     }
167
168     /* get a reference to the entire directory */
169     tdc = afs_GetDCache(avc, 0, &treq, &origOffset, &len, 1);
170     if (!tdc) {
171         AFS_GUNLOCK();
172         return -ENOENT;
173     }
174     ObtainReadLock(&avc->lock);
175     /*
176      * Make sure that the data in the cache is current. There are two
177      * cases we need to worry about:
178      * 1. The cache data is being fetched by another process.
179      * 2. The cache data is no longer valid
180      */
181     while ((avc->states & CStatd)
182            && (tdc->flags & DFFetching)
183            && hsame(avc->m.DataVersion, tdc->f.versionNo)) {
184         tdc->flags |= DFWaiting;
185         ReleaseReadLock(&avc->lock);
186         afs_osi_Sleep(&tdc->validPos);
187         ObtainReadLock(&avc->lock);
188     }
189     if (!(avc->states & CStatd)
190         || !hsame(avc->m.DataVersion, tdc->f.versionNo)) {
191         ReleaseReadLock(&avc->lock);
192         afs_PutDCache(tdc);
193         goto tagain;
194     }
195
196     /* Fill in until we get an error or we're done. This implementation
197      * takes an offset in units of blobs, rather than bytes.
198      */
199     code = 0;
200     offset = (int)fp->f_pos;
201     while(1) { 
202         dirpos = BlobScan(&tdc->f.inode, offset);
203         if (!dirpos)
204             break;
205
206         de = (struct DirEntry*)afs_dir_GetBlob(&tdc->f.inode, dirpos);
207         if (!de)
208             break;
209
210         ino = (avc->fid.Fid.Volume << 16) + ntohl(de->fid.vnode);
211         ino &= 0x7fffffff; /* Assumes 32 bit ino_t ..... */
212         len = strlen(de->name);
213
214         /* filldir returns -EINVAL when the buffer is full. */
215         code = (*filldir)(dirbuf, de->name, len, offset, ino);
216         DRelease(de, 0);
217         if (code)
218             break;
219         offset = dirpos + 1 + ((len+16)>>5);
220     }
221     /* If filldir didn't fill in the last one this is still pointing to that
222      * last attempt.
223      */
224     fp->f_pos = (loff_t)offset;
225
226     afs_PutDCache(tdc);
227     ReleaseReadLock(&avc->lock);
228     AFS_GUNLOCK();
229     return 0;
230 }
231
232 #ifdef NOTUSED
233 int afs_linux_select(struct inode *ip, struct file *fp, int, select_table *);
234 #endif
235
236 /* in afs_pioctl.c */
237 extern int afs_xioctl(struct inode *ip, struct file *fp,
238                           unsigned int com, unsigned long arg);
239
240
241 /* We need to detect unmap's after close. To do that, we need our own
242  * vm_operations_struct's. And we need to set them up for both the
243  * private and shared mappings. The fun part is that these are all static
244  * so we'll have to initialize on the fly!
245  */
246 static struct vm_operations_struct afs_private_mmap_ops;
247 static int afs_private_mmap_ops_inited = 0;
248 static struct vm_operations_struct afs_shared_mmap_ops;
249 static int afs_shared_mmap_ops_inited = 0;
250
251 void afs_linux_vma_close(struct vm_area_struct *vmap)
252 {
253     struct vcache *vcp;
254     cred_t *credp;
255
256     if (!vmap->vm_file)
257         return;
258
259     vcp = (struct vcache*)FILE_INODE(vmap->vm_file);
260     if (!vcp)
261         return;
262
263     AFS_GLOCK();
264     afs_Trace4(afs_iclSetp, CM_TRACE_VM_CLOSE,
265                ICL_TYPE_POINTER, vcp,
266                ICL_TYPE_INT32, vcp->mapcnt,
267                ICL_TYPE_INT32, vcp->opens,
268                ICL_TYPE_INT32, vcp->execsOrWriters);
269     ObtainWriteLock(&vcp->lock, 532);
270     if (vcp->mapcnt) {
271         vcp->mapcnt--;
272         ReleaseWriteLock(&vcp->lock);
273         if (!vcp->mapcnt) {
274             credp = crref();
275             (void) afs_close(vcp, vmap->vm_file->f_flags, credp);
276             /* only decrement the execsOrWriters flag if this is not a writable
277              * file. */
278             if (! (vmap->vm_file->f_flags & (FWRITE | FTRUNC)))
279                 vcp->execsOrWriters--;
280
281             vcp->states &= ~CMAPPED;
282             crfree(credp);
283         }
284     }
285     else {
286         ReleaseWriteLock(&vcp->lock);
287     }
288
289  unlock_exit:
290     AFS_GUNLOCK();
291 }
292
293 static int afs_linux_mmap(struct file *fp, struct vm_area_struct *vmap)
294 {
295     struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
296     cred_t *credp = crref();
297     struct vrequest treq;
298     int code;
299
300     AFS_GLOCK();
301     afs_Trace4(afs_iclSetp, CM_TRACE_GMAP, ICL_TYPE_POINTER, vcp,
302                ICL_TYPE_POINTER, vmap->vm_start,
303                ICL_TYPE_INT32, vmap->vm_end - vmap->vm_start,
304                ICL_TYPE_INT32, vmap->vm_offset);
305
306     /* get a validated vcache entry */
307     code = afs_InitReq(&treq, credp);
308     if (!code)
309         code = afs_VerifyVCache(vcp, &treq);
310
311
312     if (code)
313         code = -code;
314     else {
315         osi_FlushPages(vcp, credp);     /* ensure stale pages are gone */
316
317         AFS_GUNLOCK();
318         code = generic_file_mmap(fp, vmap);
319         AFS_GLOCK();
320     }
321
322     ObtainWriteLock(&vcp->lock,531);
323     /* Set out vma ops so we catch the close. The following test should be
324      * the same as used in generic_file_mmap.
325      */
326     if ((vmap->vm_flags & VM_SHARED) && (vmap->vm_flags & VM_MAYWRITE)) {
327         if (!afs_shared_mmap_ops_inited) {
328             afs_shared_mmap_ops_inited = 1;
329             afs_shared_mmap_ops = *vmap->vm_ops;
330             afs_shared_mmap_ops.close = afs_linux_vma_close;
331         }
332         vmap->vm_ops = &afs_shared_mmap_ops;
333     }
334     else {
335         if (!afs_private_mmap_ops_inited) {
336             afs_private_mmap_ops_inited = 1;
337             afs_private_mmap_ops = *vmap->vm_ops;
338             afs_private_mmap_ops.close = afs_linux_vma_close;
339         }
340         vmap->vm_ops = &afs_private_mmap_ops;
341     }
342     
343     
344     /* Add an open reference on the first mapping. */
345     if (vcp->mapcnt == 0) {
346         vcp->execsOrWriters++;
347         vcp->opens++;
348         vcp->states |= CMAPPED;
349     }
350     ReleaseWriteLock(&vcp->lock);
351     vcp->mapcnt++;
352
353     AFS_GUNLOCK();
354     crfree(credp);
355     return code;
356 }
357
358 int afs_linux_open(struct inode *ip, struct file *fp)
359 {
360     int code;
361     cred_t *credp = crref();
362
363     AFS_GLOCK();
364     code = afs_open((struct vcache**)&ip, fp->f_flags, credp);
365     AFS_GUNLOCK();
366
367     crfree(credp);
368     return -code;
369 }
370
371 /* afs_Close is called from release, since release is used to handle all
372  * file closings. In addition afs_linux_flush is called from sys_close to
373  * handle flushing the data back to the server. The kicker is that we could
374  * ignore flush completely if only sys_close took it's return value from
375  * fput. See afs_linux_flush for notes on interactions between release and
376  * flush.
377  */
378 static int afs_linux_release(struct inode *ip, struct file *fp)
379 {
380     int code = 0;
381     cred_t *credp = crref();
382     struct vcache *vcp = (struct vcache*)ip;
383
384     AFS_GLOCK();
385     if (vcp->flushcnt) {
386         vcp->flushcnt--; /* protected by AFS global lock. */
387     }
388     else {
389         code = afs_close(vcp, fp->f_flags, credp);
390     }
391     AFS_GUNLOCK();
392
393     crfree(credp);
394     return -code;
395 }
396
397 static int afs_linux_fsync(struct file *fp, struct dentry *dp)
398 {
399     int code;
400     struct inode *ip = FILE_INODE(fp);
401     cred_t *credp = crref();
402
403     AFS_GLOCK();
404     code = afs_fsync((struct vcache*)ip, credp);
405     AFS_GUNLOCK();
406     crfree(credp);
407     return -code;
408     
409 }
410
411 #ifdef NOTUSED
412 /* No support for async i/o */
413 int afs_linux_fasync(struct inode *ip, struct file *fp, int);
414
415 /* I don't think it will, at least not as can be detected here. */
416 int afs_linux_check_media_change(kdev_t dev);
417
418 /* Revalidate media and file system. */
419 int afs_linux_file_revalidate(kdev_t dev);
420 #endif /* NOTUSED */
421
422 static int afs_linux_lock(struct file *fp, int cmd, struct file_lock *flp)
423 {
424     int code = 0;
425     struct vcache *vcp = (struct vcache*)FILE_INODE(fp);
426     cred_t *credp = crref();
427     struct flock flock;
428     
429     /* Convert to a lock format afs_lockctl understands. */
430     memset((char*)&flock, 0, sizeof(flock));
431     flock.l_type = flp->fl_type;
432     flock.l_pid = flp->fl_pid;
433     flock.l_whence = 0;
434     flock.l_start = flp->fl_start;
435     flock.l_len = flp->fl_end - flp->fl_start;
436
437     AFS_GLOCK();
438     code = afs_lockctl(vcp, &flock, cmd, credp);
439     AFS_GUNLOCK();
440     crfree(credp);
441     return -code;
442     
443 }
444
445 /* afs_linux_flush
446  * flush is called from sys_close. We could ignore it, but sys_close return
447  * code comes from flush, not release. We need to use release to keep
448  * the vcache open count correct. Note that flush is called before release
449  * (via fput) in sys_close. vcp->flushcnt is a bit of ugliness to avoid
450  * races and also avoid calling afs_close twice when closing the file.
451  * If we merely checked for opens > 0 in afs_linux_release, then if an
452  * new open occurred when storing back the file, afs_linux_release would
453  * incorrectly close the file and decrement the opens count. Calling afs_close
454  * on the just flushed file is wasteful, since the background daemon will
455  * execute the code that finally decides there is nothing to do.
456  */
457 int afs_linux_flush(struct file *fp)
458 {
459     struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
460     int code = 0;
461     cred_t *credp;
462
463     /* Only do this on the last close of the file pointer. */
464     if (fp->f_count > 1)
465         return 0;
466
467     credp = crref();
468
469     AFS_GLOCK();
470     code = afs_close(vcp, fp->f_flags, credp);
471     vcp->flushcnt++; /* protected by AFS global lock. */
472     AFS_GUNLOCK();
473
474     crfree(credp);
475     return -code;
476 }
477
478 /* Not allowed to directly read a directory. */
479 int afs_linux_dir_read(struct file *fp, char *buf, size_t count, loff_t *ppos)
480 {
481     return -EISDIR;
482 }
483
484
485
486 struct file_operations afs_dir_fops = {
487     NULL,               /* afs_linux_lseek */
488     afs_linux_dir_read,
489     NULL,               /* afs_linux_write */
490     afs_linux_readdir,
491     NULL,               /* afs_linux_select */
492     afs_xioctl,         /* close enough to use the ported AFS one */
493     NULL,               /* afs_linux_mmap */
494     afs_linux_open,
495     NULL,               /* afs_linux_flush */
496     afs_linux_release,
497     afs_linux_fsync,
498     NULL,               /* afs_linux_fasync */
499     NULL,               /* afs_linux_check_media_change */
500     NULL,               /* afs_linux_file_revalidate */
501     afs_linux_lock,
502 };
503
504 struct file_operations afs_file_fops = {
505     NULL,               /* afs_linux_lseek */
506     afs_linux_read,
507     afs_linux_write,
508     NULL,               /* afs_linux_readdir */
509     NULL,               /* afs_linux_select */
510     afs_xioctl,         /* close enough to use the ported AFS one */
511     afs_linux_mmap,
512     afs_linux_open,
513     afs_linux_flush,
514     afs_linux_release,
515     afs_linux_fsync,
516     NULL,               /* afs_linux_fasync */
517     NULL,               /* afs_linux_check_media_change */
518     NULL,               /* afs_linux_file_revalidate */
519     afs_linux_lock,
520 };
521    
522
523 /**********************************************************************
524  * AFS Linux dentry operations
525  **********************************************************************/
526
527 /* afs_linux_revalidate
528  * Ensure vcache is stat'd before use. Return 0 if entry is valid.
529  */
530 static int afs_linux_revalidate(struct dentry *dp)
531 {
532     int code;
533     cred_t *credp;
534     struct vrequest treq;
535     struct vcache *vcp = (struct vcache*)dp->d_inode;
536
537     AFS_GLOCK();
538
539     /* If it's a negative dentry, then there's nothing to do. */
540     if (!vcp) {
541         AFS_GUNLOCK();
542         return 0;
543     }
544
545     /* Make this a fast path (no crref), since it's called so often. */
546     if (vcp->states & CStatd) {
547         if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
548             check_bad_parent(dp); /* check and correct mvid */
549         vcache2inode(vcp);
550         AFS_GUNLOCK();
551         return 0;
552     }
553
554     credp = crref();
555     code = afs_InitReq(&treq, credp);
556     if (!code)
557         code = afs_VerifyVCache(vcp, &treq);
558
559     AFS_GUNLOCK();
560     crfree(credp);
561
562     return -code ;
563 }
564
565 /* Validate a dentry. Return 0 if unchanged, 1 if VFS layer should re-evaluate.
566  * In kernels 2.2.10 and above, we are passed an additional flags var which
567  * may have either the LOOKUP_FOLLOW OR LOOKUP_DIRECTORY set in which case
568  * we are advised to follow the entry if it is a link or to make sure that 
569  * it is a directory. But since the kernel itself checks these possibilities
570  * later on, we shouldn't have to do it until later. Perhaps in the future..
571  */
572 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
573 static int afs_linux_dentry_revalidate(struct dentry *dp, int flags)
574 #else
575 static int afs_linux_dentry_revalidate(struct dentry *dp)
576 #endif
577 {
578     int code;
579     cred_t *credp;
580     struct vrequest treq;
581     struct vcache *vcp = (struct vcache*)dp->d_inode;
582
583     AFS_GLOCK();
584
585     /* If it's a negative dentry, then there's nothing to do. */
586     if (!vcp) {
587         AFS_GUNLOCK();
588         return 0;
589     }
590
591     /* Make this a fast path (no crref), since it's called so often. */
592     if (vcp->states & CStatd) {
593         if (*dp->d_name.name != '/' && vcp->mvstat == 2) /* root vnode */
594             check_bad_parent(dp); /* check and correct mvid */
595         vcache2inode(vcp);
596         AFS_GUNLOCK();
597         return 0;
598     }
599
600     credp = crref();
601     code = afs_InitReq(&treq, credp);
602     if (!code)
603         code = afs_VerifyVCache(vcp, &treq);
604
605     AFS_GUNLOCK();
606     crfree(credp);
607
608     return 1;
609 }
610
611 /* afs_dentry_iput */
612 static void afs_dentry_iput(struct dentry *dp, struct inode *ip)
613 {
614     if (ip->i_count == 0 || ip->i_count & 0xffff0000) {
615         osi_Panic("Bad refCount %d on inode 0x%x\n",
616                   ip->i_count, ip);
617     }
618     ip->i_count --;
619     if (!ip->i_count) {
620         afs_delete_inode(ip);
621     }
622 }
623
624 struct dentry_operations afs_dentry_operations = {
625         afs_linux_dentry_revalidate,    /* d_validate(struct dentry *) */
626         NULL,                   /* d_hash */
627         NULL,                   /* d_compare */
628         NULL,                   /* d_delete(struct dentry *) */
629         NULL,                   /* d_release(struct dentry *) */
630         afs_dentry_iput         /* d_iput(struct dentry *, struct inode *) */
631 };
632 struct dentry_operations *afs_dops = &afs_dentry_operations;
633
634 /**********************************************************************
635  * AFS Linux inode operations
636  **********************************************************************/
637
638 /* afs_linux_create
639  *
640  * Merely need to set enough of vattr to get us through the create. Note
641  * that the higher level code (open_namei) will take care of any tuncation
642  * explicitly. Exclusive open is also taken care of in open_namei.
643  *
644  * name is in kernel space at this point.
645  */
646 int afs_linux_create(struct inode *dip, struct dentry *dp, int mode)
647 {
648     int code;
649     cred_t *credp = crref();
650     struct vattr vattr;
651     enum vcexcl excl;
652     const char *name = dp->d_name.name;
653     struct inode *ip;
654
655     VATTR_NULL(&vattr);
656     vattr.va_mode = mode;
657
658     AFS_GLOCK();
659     code = afs_create((struct vcache*)dip, name, &vattr, NONEXCL, mode,
660                       (struct vcache**)&ip, credp);
661
662     if (!code) {
663         vattr2inode(ip, &vattr);
664         /* Reset ops if symlink or directory. */
665         if (S_ISDIR(ip->i_mode))
666             ip->i_op = &afs_dir_iops;
667         else if (S_ISLNK(ip->i_mode))
668             ip->i_op = &afs_symlink_iops;
669
670         dp->d_op = afs_dops;
671         d_instantiate(dp, ip);
672     }
673
674     AFS_GUNLOCK();
675     crfree(credp);
676     return -code;
677 }
678
679 /* afs_linux_lookup */
680 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
681 struct dentry *afs_linux_lookup(struct inode *dip, struct dentry *dp)
682 #else
683 int afs_linux_lookup(struct inode *dip, struct dentry *dp)
684 #endif
685 {
686     int code = 0;
687     cred_t *credp = crref();
688     struct vcache *vcp=NULL;
689     const char *comp = dp->d_name.name;
690     AFS_GLOCK();
691     code = afs_lookup((struct vcache *)dip, comp, &vcp, credp);
692
693     if (vcp) {
694         struct inode *ip = (struct inode*)vcp;
695         /* Reset ops if symlink or directory. */
696         if (S_ISDIR(ip->i_mode))
697             ip->i_op = &afs_dir_iops;
698         else if (S_ISLNK(ip->i_mode))
699             ip->i_op = &afs_symlink_iops;
700     }
701     dp->d_op = afs_dops;
702     d_add(dp, (struct inode*)vcp);
703
704     AFS_GUNLOCK();
705     crfree(credp);
706
707     /* It's ok for the file to not be found. That's noted by the caller by
708      * seeing that the dp->d_inode field is NULL.
709      */
710 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,10)
711     if (code == ENOENT)
712         return ERR_PTR(0);
713     else
714         return ERR_PTR(-code);
715 #else
716     if (code == ENOENT)
717         code = 0;
718     return -code;
719 #endif
720 }
721
722 int afs_linux_link(struct dentry *olddp, struct inode *dip,
723                    struct dentry *newdp)
724 {
725     int code;
726     cred_t *credp = crref();
727     const char *name = newdp->d_name.name;
728     struct inode *oldip = olddp->d_inode;
729
730     /* If afs_link returned the vnode, we could instantiate the
731      * dentry. Since it's not, we drop this one and do a new lookup.
732      */
733     d_drop(newdp);
734
735     AFS_GLOCK();
736     code = afs_link((struct vcache*)oldip, (struct vcache*)dip, name, credp);
737
738     AFS_GUNLOCK();
739     crfree(credp);
740     return -code;
741 }
742
743 int afs_linux_unlink(struct inode *dip, struct dentry *dp)
744 {
745     int code;
746     cred_t *credp = crref();
747     const char *name = dp->d_name.name;
748     int putback = 0;
749
750     if (!list_empty(&dp->d_hash)) {
751         d_drop(dp);
752         /* Install a definite non-existence if we're the only user. */
753         if (dp->d_count == 1)
754             putback = 1;
755     }
756
757     AFS_GLOCK();
758     code = afs_remove((struct vcache*)dip, name, credp);
759     AFS_GUNLOCK();
760     if (!code) {
761         d_delete(dp);
762         if (putback)
763             d_add(dp, NULL); /* means definitely does _not_ exist */
764     }
765     crfree(credp);
766     return -code;
767 }
768
769
770 int afs_linux_symlink(struct inode *dip, struct dentry *dp,
771                       const char *target)
772 {
773     int code;
774     cred_t *credp = crref();
775     struct vattr vattr;
776     const char *name = dp->d_name.name;
777
778     /* If afs_symlink returned the vnode, we could instantiate the
779      * dentry. Since it's not, we drop this one and do a new lookup.
780      */
781     d_drop(dp);
782
783     AFS_GLOCK();
784     VATTR_NULL(&vattr);
785     code = afs_symlink((struct vcache*)dip, name, &vattr, target, credp);
786     AFS_GUNLOCK();
787     crfree(credp);
788     return -code;
789 }
790
791 int afs_linux_mkdir(struct inode *dip, struct dentry *dp, int mode)
792 {
793     int code;
794     cred_t *credp = crref();
795     struct vcache *tvcp = NULL;
796     struct vattr vattr;
797     const char *name = dp->d_name.name;
798
799     AFS_GLOCK();
800     VATTR_NULL(&vattr);
801     vattr.va_mask = ATTR_MODE;
802     vattr.va_mode = mode;
803     code = afs_mkdir((struct vcache*)dip, name, &vattr, &tvcp, credp);
804
805     if (tvcp) {
806         tvcp->v.v_op = &afs_dir_iops;
807         dp->d_op = afs_dops;
808         d_instantiate(dp, (struct inode*)tvcp);
809     }
810     AFS_GUNLOCK();
811     crfree(credp);
812     return -code;
813 }
814
815 int afs_linux_rmdir(struct inode *dip, struct dentry *dp)
816 {
817     int code;
818     cred_t *credp = crref();
819     const char *name = dp->d_name.name;
820
821     AFS_GLOCK();
822     code = afs_rmdir((struct vcache*)dip, name, credp);
823
824     /* Linux likes to see ENOTDIR returned from an rmdir() syscall
825      * that failed because a directory is not empty. So, we map
826      * EEXIST to ENOTDIR on linux.
827      */
828     if (code == EEXIST) {
829         code = ENOTDIR;
830     }
831     
832     if (!code) {
833         d_delete(dp);
834     }
835
836     AFS_GUNLOCK();
837     crfree(credp);
838     return -code;
839 }
840
841
842
843 int afs_linux_rename(struct inode *oldip, struct dentry *olddp,
844                      struct inode *newip, struct dentry *newdp)
845 {
846     int code;
847     cred_t *credp = crref();
848     const char *oldname = olddp->d_name.name;
849     const char *newname = newdp->d_name.name;
850
851     /* Remove old and new entries from name hash. New one will change below.
852      * While it's optimal to catch failures and re-insert newdp into hash,
853      * it's also error prone and in that case we're already dealing with error
854      * cases. Let another lookup put things right, if need be.
855      */
856     if (!list_empty(&olddp->d_hash)) {
857         d_drop(olddp);
858     }
859     if (!list_empty(&newdp->d_hash)) {
860         d_drop(newdp);
861     }
862     AFS_GLOCK();
863     code = afs_rename((struct vcache*)oldip, oldname, (struct vcache*)newip,
864                       newname, credp);
865     AFS_GUNLOCK();
866
867     if (!code)
868         d_move(olddp, newdp);
869
870     crfree(credp);
871     return -code;
872 }
873
874
875 /* afs_linux_ireadlink 
876  * Internal readlink which can return link contents to user or kernel space.
877  * Note that the buffer is NOT supposed to be null-terminated.
878  */
879 static int afs_linux_ireadlink(struct inode *ip, char *target, int maxlen,
880                         uio_seg_t seg)
881 {
882     int code;
883     cred_t *credp = crref();
884     uio_t tuio;
885     struct iovec iov;
886
887     setup_uio(&tuio, &iov, target, 0, maxlen, UIO_READ, seg);
888     code = afs_readlink((struct vcache*)ip, &tuio, credp);
889     crfree(credp);
890
891     if (!code)
892         return maxlen - tuio.uio_resid;
893     else
894         return -code;
895 }
896
897 /* afs_linux_readlink 
898  * Fill target (which is in user space) with contents of symlink.
899  */
900 int afs_linux_readlink(struct dentry *dp, char *target, int maxlen)
901 {
902     int code;
903     struct inode *ip = dp->d_inode;
904
905     AFS_GLOCK();
906     code = afs_linux_ireadlink(ip, target, maxlen, AFS_UIOUSER);
907     AFS_GUNLOCK();
908     return code;
909 }
910
911
912 /* afs_linux_follow_link
913  * a file system dependent link following routine.
914  */
915 struct dentry * afs_linux_follow_link(struct dentry *dp,
916                                       struct dentry *basep,
917                                       unsigned int follow)
918 {
919     int code = 0;
920     char *name;
921     struct dentry *res;
922
923
924     AFS_GLOCK();
925     name = osi_Alloc(PATH_MAX+1);
926     if (!name) {
927         AFS_GUNLOCK();
928         dput(basep);
929         return ERR_PTR(-EIO);
930     }
931
932     code = afs_linux_ireadlink(dp->d_inode, name, PATH_MAX, AFS_UIOSYS);
933     AFS_GUNLOCK();
934
935     if (code<0) {
936         dput(basep);
937         res = ERR_PTR(code);
938     }
939     else {
940         name[code] = '\0';
941         res = lookup_dentry(name, basep, follow);
942     }
943
944     AFS_GLOCK();
945     osi_Free(name, PATH_MAX+1);
946     AFS_GUNLOCK();
947     return res;
948 }
949
950 /* afs_linux_readpage
951  * all reads come through here. A strategy-like read call.
952  */
953 int afs_linux_readpage(struct file *fp, struct page *pp)
954 {
955     int code;
956     cred_t *credp = crref();
957     ulong address = afs_linux_page_address(pp);
958     uio_t tuio;
959     struct iovec iovec;
960     struct inode *ip = FILE_INODE(fp);
961     int cnt = atomic_read(&pp->count);
962
963     AFS_GLOCK();
964     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
965                ICL_TYPE_POINTER, ip,
966                ICL_TYPE_POINTER, pp,
967                ICL_TYPE_INT32, cnt,
968                ICL_TYPE_INT32, 99999); /* not a possible code value */
969     atomic_add(1, &pp->count);
970     set_bit(PG_locked, &pp->flags); /* other bits? See mm.h */
971     clear_bit(PG_error, &pp->flags);
972
973     setup_uio(&tuio, &iovec, (char*)address, pp->offset, PAGESIZE,
974               UIO_READ, AFS_UIOSYS);
975     code = afs_rdwr((struct vcache*)ip, &tuio, UIO_READ, 0, credp);
976
977     if (!code) {
978         if (tuio.uio_resid) /* zero remainder of page */
979             memset((void*)(address+(PAGESIZE-tuio.uio_resid)), 0,
980                    tuio.uio_resid);
981         set_bit(PG_uptodate, &pp->flags);
982     }
983
984     clear_bit(PG_locked, &pp->flags);
985     wake_up(&pp->wait);
986     free_page(address);
987
988     crfree(credp);
989     afs_Trace4(afs_iclSetp, CM_TRACE_READPAGE,
990                ICL_TYPE_POINTER, ip,
991                ICL_TYPE_POINTER, pp,
992                ICL_TYPE_INT32, cnt,
993                ICL_TYPE_INT32, code);
994     AFS_GUNLOCK();
995     return -code;
996 }
997
998 #ifdef NOTUSED
999 /* afs_linux_writepage - is this used anywhere? */
1000 int afs_linux_writepage(struct inode *ip, struct page *) { return -EINVAL };
1001
1002 /* afs_linux_bmap - supports generic_readpage, but we roll our own. */
1003 int afs_linux_bmap(struct inode *ip, int) { return -EINVAL; }
1004
1005 /* afs_linux_truncate
1006  * Handles discarding disk blocks if this were a device. ext2 indicates we
1007  * may need to zero partial last pages of memory mapped files.
1008  */
1009 void afs_linux_truncate(struct inode *ip)
1010 {
1011 }
1012 #endif
1013
1014 /* afs_linux_permission
1015  * Check access rights - returns error if can't check or permission denied.
1016  */
1017 int afs_linux_permission(struct inode *ip, int mode)
1018 {
1019     int code;
1020     cred_t *credp = crref();
1021     int tmp = 0;
1022
1023     AFS_GLOCK();
1024     if (mode & MAY_EXEC) tmp |= VEXEC;
1025     if (mode & MAY_READ) tmp |= VREAD;
1026     if (mode & MAY_WRITE) tmp |= VWRITE;
1027     code = afs_access((struct vcache*)ip, tmp, credp);
1028
1029     AFS_GUNLOCK();
1030     crfree(credp);
1031     return -code;
1032 }
1033
1034
1035 #ifdef NOTUSED
1036 /* msdos sector mapping hack for memory mapping. */
1037 int afs_linux_smap(struct inode *ip, int) { return -EINVAL; }
1038 #endif
1039
1040 /* afs_linux_updatepage
1041  * What one would have thought was writepage - write dirty page to file.
1042  * Called from generic_file_write. buffer is still in user space. pagep
1043  * has been filled in with old data if we're updating less than a page.
1044  */
1045 int afs_linux_updatepage(struct file *fp, struct page *pp,
1046                          unsigned long offset,
1047                          unsigned int count, int sync)
1048 {
1049     struct vcache *vcp = (struct vcache *)FILE_INODE(fp);
1050     u8 *page_addr = (u8*) afs_linux_page_address(pp);
1051     int code = 0;
1052     cred_t *credp;
1053     uio_t tuio;
1054     struct iovec iovec;
1055     
1056     set_bit(PG_locked, &pp->flags);
1057
1058     credp = crref();
1059     AFS_GLOCK();
1060     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1061                ICL_TYPE_POINTER, pp,
1062                ICL_TYPE_INT32, atomic_read(&pp->count),
1063                ICL_TYPE_INT32, 99999);
1064     setup_uio(&tuio, &iovec, page_addr + offset, pp->offset + offset, count,
1065               UIO_WRITE, AFS_UIOSYS);
1066
1067     code = afs_write(vcp, &tuio, fp->f_flags, credp, 0);
1068
1069     vcache2inode(vcp);
1070
1071     code = code ? -code : count - tuio.uio_resid;
1072     afs_Trace4(afs_iclSetp, CM_TRACE_UPDATEPAGE, ICL_TYPE_POINTER, vcp,
1073                ICL_TYPE_POINTER, pp,
1074                ICL_TYPE_INT32, atomic_read(&pp->count),
1075                ICL_TYPE_INT32, code);
1076
1077     AFS_GUNLOCK();
1078     crfree(credp);
1079
1080     clear_bit(PG_locked, &pp->flags);
1081     return code;
1082 }
1083
1084
1085
1086 struct inode_operations afs_iops = {
1087     &afs_file_fops,     /* file operations */
1088     NULL,               /* afs_linux_create */
1089     NULL,               /* afs_linux_lookup */
1090     NULL,               /* afs_linux_link */
1091     NULL,               /* afs_linux_unlink */
1092     NULL,               /* afs_linux_symlink */
1093     NULL,               /* afs_linux_mkdir */
1094     NULL,               /* afs_linux_rmdir */
1095     NULL,               /* afs_linux_mknod */
1096     NULL,               /* afs_linux_rename */
1097     NULL,               /* afs_linux_readlink */
1098     NULL,               /* afs_linux_follow_link */
1099     afs_linux_readpage,
1100     NULL,               /* afs_linux_writepage */
1101     NULL,               /* afs_linux_bmap */
1102     NULL,               /* afs_linux_truncate */
1103     afs_linux_permission,
1104     NULL,               /* afs_linux_smap */
1105     afs_linux_updatepage,
1106     afs_linux_revalidate,
1107 };
1108
1109 /* Separate ops vector for directories. Linux 2.2 tests type of inode
1110  * by what sort of operation is allowed.....
1111  */
1112 struct inode_operations afs_dir_iops = {
1113     &afs_dir_fops,      /* file operations for directories */
1114     afs_linux_create,
1115     afs_linux_lookup,
1116     afs_linux_link,
1117     afs_linux_unlink,
1118     afs_linux_symlink,
1119     afs_linux_mkdir,
1120     afs_linux_rmdir,
1121     NULL,               /* afs_linux_mknod */
1122     afs_linux_rename,
1123     NULL,               /* afs_linux_readlink */
1124     NULL,               /* afs_linux_follow_link */
1125     NULL,               /* afs_linux_readpage */
1126     NULL,               /* afs_linux_writepage */
1127     NULL,               /* afs_linux_bmap */
1128     NULL,               /* afs_linux_truncate */
1129     afs_linux_permission,
1130     NULL,               /* afs_linux_smap */
1131     NULL,               /* afs_linux_updatepage */
1132     afs_linux_revalidate,
1133 };
1134
1135 struct inode_operations *afs_ops = &afs_iops;
1136
1137 /* We really need a separate symlink set of ops, since do_follow_link()
1138  * determines if it _is_ a link by checking if the follow_link op is set.
1139  */
1140 struct inode_operations afs_symlink_iops = {
1141     NULL,               /* file operations */
1142     NULL,               /* create */
1143     NULL,               /* lookup */
1144     NULL,               /* link */
1145     NULL,               /* unlink */
1146     NULL,               /* symlink */
1147     NULL,               /* mkdir */
1148     NULL,               /* rmdir */
1149     NULL,               /* afs_linux_mknod */
1150     NULL,               /* rename */
1151     afs_linux_readlink,
1152     afs_linux_follow_link,
1153     NULL,               /* readpage */
1154     NULL,               /* afs_linux_writepage */
1155     NULL,               /* afs_linux_bmap */
1156     NULL,               /* afs_linux_truncate */
1157     afs_linux_permission, /* tho the code appears to indicate not used? */
1158     NULL,               /* afs_linux_smap */
1159     NULL,               /* updatepage */
1160     afs_linux_revalidate, /* tho the code appears to indicate not used? */
1161 };