linux: defer vcache evictions when sleep would be needed
[openafs.git] / src / afs / LINUX24 / osi_vcache.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 #include <afsconfig.h>
11 #include "afs/param.h"
12
13 #include "afs/sysincludes.h"    /*Standard vendor system headers */
14 #include "afsincludes.h"        /*AFS-based standard headers */
15
16 #if defined(AFS_LINUX24_ENV)
17 # define afs_linux_lock_dcache() spin_lock(&dcache_lock)
18 # define afs_linux_unlock_dcache() spin_unlock(&dcache_lock)
19 #else
20 # define afs_linux_lock_dcache()
21 # define afs_linux_unlock_dcache()
22 #endif
23
24 int
25 osi_TryEvictVCache(struct vcache *avc, int *slept, int defersleep) {
26     int code;
27     struct dentry *dentry;
28     struct list_head *cur, *head;
29
30     /* First, see if we can evict the inode from the dcache */
31     if (defersleep && avc != afs_globalVp && VREFCOUNT(avc) > 1 && avc->opens == 0) {
32         *slept = 1;
33         ReleaseWriteLock(&afs_xvcache);
34         AFS_GUNLOCK();
35         afs_linux_lock_dcache();
36         head = &(AFSTOV(avc))->i_dentry;
37
38 restart:
39         cur = head;
40         while ((cur = cur->next) != head) {
41             dentry = list_entry(cur, struct dentry, d_alias);
42
43             if (d_unhashed(dentry))
44                 continue;
45
46             dget_locked(dentry);
47
48             afs_linux_unlock_dcache();
49
50             if (d_invalidate(dentry) == -EBUSY) {
51                 dput(dentry);
52                 /* perhaps lock and try to continue? (use cur as head?) */
53                 goto inuse;
54             }
55             dput(dentry);
56             afs_linux_lock_dcache();
57             goto restart;
58         }
59         afs_linux_unlock_dcache();
60 inuse:
61         AFS_GLOCK();
62         ObtainWriteLock(&afs_xvcache, 733);
63     }
64
65     /* See if we can evict it from the VLRUQ */
66     if (VREFCOUNT_GT(avc,0) && !VREFCOUNT_GT(avc,1) && avc->opens == 0
67         && (avc->f.states & CUnlinkedDel) == 0) {
68         int didsleep = *slept;
69
70         code = afs_FlushVCache(avc, slept);
71         /* flushvcache wipes slept; restore slept if we did before */
72         if (didsleep)
73             *slept = didsleep;
74
75         if (code == 0)
76            return 1;
77     }
78
79     return 0;
80 }
81
82 struct vcache *
83 osi_NewVnode(void)
84 {
85     struct inode *ip;
86     struct vcache *tvc;
87
88     AFS_GUNLOCK();
89     ip = new_inode(afs_globalVFS);
90     if (!ip)
91         osi_Panic("afs_NewVCache: no more inodes");
92     AFS_GLOCK();
93 #if defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
94     tvc = VTOAFS(ip);
95 #else
96     tvc = afs_osi_Alloc(sizeof(struct vcache));
97     osi_Assert(tvc != NULL);
98     ip->u.generic_ip = tvc;
99     tvc->v = ip;
100 #endif
101
102     return tvc;
103 }
104
105 /* XXX - This should probably be inline */
106 void
107 osi_PrePopulateVCache(struct vcache *avc) {
108     avc->uncred = 0;
109     memset(&(avc->f), 0, sizeof(struct fvcache));
110 }
111
112 /* XXX - This should become inline, or a #define */
113 void
114 osi_AttachVnode(struct vcache *avc, int seq) { }
115
116 /* XXX - Again, inline or a #define */
117 void
118 osi_PostPopulateVCache(struct vcache *avc) {
119     vSetType(avc, VREG);
120 }
121