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