bozo: Introduce bnode_Wait()
[openafs.git] / src / afs / LINUX / 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 #include "osi_compat.h"
17
18 static void
19 TryEvictDirDentries(struct inode *inode)
20 {
21     struct dentry *dentry;
22 #if defined(D_ALIAS_IS_HLIST) && !defined(HLIST_ITERATOR_NO_NODE)
23     struct hlist_node *p;
24 #endif
25
26     afs_d_alias_lock(inode);
27
28  restart:
29 #if defined(D_ALIAS_IS_HLIST)
30 # if defined(HLIST_ITERATOR_NO_NODE)
31     hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
32 # else
33     hlist_for_each_entry(dentry, p, &inode->i_dentry, d_alias) {
34 # endif
35 #else
36     list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
37 #endif
38         spin_lock(&dentry->d_lock);
39         if (d_unhashed(dentry)) {
40             spin_unlock(&dentry->d_lock);
41             continue;
42         }
43         spin_unlock(&dentry->d_lock);
44         afs_linux_dget(dentry);
45
46         afs_d_alias_unlock(inode);
47
48         /*
49          * Once we have dropped the d_alias lock (above), it is no longer safe
50          * to 'continue' our iteration over d_alias because the list may change
51          * out from under us.  Therefore, we must either leave the loop, or
52          * restart from the beginning.  To avoid looping forever, we must only
53          * restart if we know we've d_drop'd an alias.  In all other cases we
54          * must leave the loop.
55          */
56
57         /*
58          * For a long time we used d_invalidate() for this purpose, but
59          * using shrink_dcache_parent() and checking the refcount ourselves is
60          * better, for two reasons: it avoids causing ENOENT issues for the
61          * CWD in linux versions since 3.11, and it avoids dropping Linux
62          * submounts.
63          *
64          * For non-fakestat, AFS mountpoints look like directories and end up here.
65          */
66
67         shrink_dcache_parent(dentry);
68         spin_lock(&dentry->d_lock);
69         if (afs_dentry_count(dentry) > 1)       /* still has references */ {
70             if (dentry->d_inode != NULL) /* is not a negative dentry */ {
71                 spin_unlock(&dentry->d_lock);
72                 dput(dentry);
73                 goto inuse;
74             }
75         }
76         /*
77          * This is either a negative dentry, or a dentry with no references.
78          * Either way, it is okay to unhash it now.
79          * Do so under the d_lock (that is, via __d_drop() instead of d_drop())
80          * to avoid a race with another process picking up a reference.
81          */
82         __d_drop(dentry);
83         spin_unlock(&dentry->d_lock);
84
85         dput(dentry);
86         afs_d_alias_lock(inode);
87         goto restart;
88     }
89     afs_d_alias_unlock(inode);
90
91  inuse:
92     return;
93 }
94
95
96 int
97 osi_TryEvictVCache(struct vcache *avc, int *slept, int defersleep)
98 {
99     int code;
100
101     /* First, see if we can evict the inode from the dcache */
102     if (defersleep && avc != afs_globalVp && VREFCOUNT(avc) > 1
103         && avc->opens == 0) {
104         struct inode *ip = AFSTOV(avc);
105
106         if (osi_vnhold(avc) != 0) {
107             /* Can't grab a ref on avc; bail out. */
108             return 0;
109         }
110         *slept = 1;
111         ReleaseWriteLock(&afs_xvcache);
112         AFS_GUNLOCK();
113
114         if (S_ISDIR(ip->i_mode))
115             TryEvictDirDentries(ip);
116         else
117             d_prune_aliases(ip);
118
119         AFS_GLOCK();
120         ObtainWriteLock(&afs_xvcache, 733);
121         AFS_FAST_RELE(avc);
122     }
123
124     /* See if we can evict it from the VLRUQ */
125     if (VREFCOUNT_GT(avc, 0) && !VREFCOUNT_GT(avc, 1) && avc->opens == 0
126         && (avc->f.states & CUnlinkedDel) == 0) {
127         int didsleep = *slept;
128
129         code = afs_FlushVCache(avc, slept);
130         /* flushvcache wipes slept; restore slept if we did before */
131         if (didsleep)
132             *slept = didsleep;
133
134         if (code == 0)
135             return 1;
136     }
137
138     return 0;
139 }
140
141 struct vcache *
142 osi_NewVnode(void)
143 {
144     struct inode *ip;
145     struct vcache *tvc;
146
147     AFS_GUNLOCK();
148     ip = new_inode(afs_globalVFS);
149     if (!ip)
150         osi_Panic("afs_NewVCache: no more inodes");
151     AFS_GLOCK();
152 #if defined(STRUCT_SUPER_OPERATIONS_HAS_ALLOC_INODE)
153     tvc = VTOAFS(ip);
154 #else
155     tvc = afs_osi_Alloc(sizeof(struct vcache));
156     ip->u.generic_ip = tvc;
157     tvc->v = ip;
158 #endif
159
160     INIT_LIST_HEAD(&tvc->pagewriters);
161     spin_lock_init(&tvc->pagewriter_lock);
162
163     return tvc;
164 }
165
166 void
167 osi_PrePopulateVCache(struct vcache *avc)
168 {
169     avc->uncred = 0;
170     memset(&(avc->f), 0, sizeof(struct fvcache));
171     avc->cred = NULL;
172 }
173
174 void
175 osi_AttachVnode(struct vcache *avc, int seq)
176 {
177     /* Nada */
178 }
179
180 void
181 osi_PostPopulateVCache(struct vcache *avc)
182 {
183     vSetType(avc, VREG);
184 }
185
186 /**
187  * osi_ResetRootVCache - Reset the root vcache
188  * Reset the dentry associated with the afs root.
189  * Called from afs_CheckRootVolume when we notice that
190  * the root volume ID has changed.
191  *
192  * @volid: volume ID for the afs root
193  */
194 void
195 osi_ResetRootVCache(afs_uint32 volid)
196 {
197     struct vrequest *treq = NULL;
198     struct vattr vattr;
199     cred_t *credp;
200     struct dentry *dp;
201     struct vcache *vcp;
202     struct inode *root = AFSTOV(afs_globalVp);
203
204     afs_rootFid.Fid.Volume = volid;
205     afs_rootFid.Fid.Vnode = 1;
206     afs_rootFid.Fid.Unique = 1;
207
208     credp = crref();
209     if (afs_CreateReq(&treq, credp))
210         goto out;
211     vcp = afs_GetVCache(&afs_rootFid, treq);
212     if (!vcp)
213         goto out;
214     afs_getattr(vcp, &vattr, credp);
215     afs_fill_inode(AFSTOV(vcp), &vattr);
216
217     dp = d_find_alias(root);
218
219     afs_d_alias_lock(AFSTOV(vcp));
220
221     spin_lock(&dp->d_lock);
222 #if defined(D_ALIAS_IS_HLIST)
223     hlist_del_init(&dp->d_alias);
224     hlist_add_head(&dp->d_alias, &(AFSTOV(vcp)->i_dentry));
225 #else
226     list_del_init(&dp->d_alias);
227     list_add(&dp->d_alias, &(AFSTOV(vcp)->i_dentry));
228 #endif
229     dp->d_inode = AFSTOV(vcp);
230     spin_unlock(&dp->d_lock);
231
232     afs_d_alias_unlock(AFSTOV(vcp));
233
234     dput(dp);
235
236     AFS_RELE(root);
237     afs_globalVp = vcp;
238  out:
239     crfree(credp);
240     afs_DestroyReq(treq);
241 }
242
243 int
244 osi_vnhold(struct vcache *avc)
245 {
246     VN_HOLD(AFSTOV(avc));
247     return 0;
248 }