Whamcloud - gitweb
LU-9868 llite: handle DCACHE_PAR_LOOKUP in ll_dcompare
[fs/lustre-release.git] / lustre / llite / dcache.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #include <linux/fs.h>
34 #include <linux/sched.h>
35 #include <linux/quotaops.h>
36 #include <linux/kernel.h>
37
38 #define DEBUG_SUBSYSTEM S_LLITE
39
40 #include <obd_support.h>
41 #include <lustre_dlm.h>
42
43 #include "llite_internal.h"
44
45 static void free_dentry_data(struct rcu_head *head)
46 {
47         struct ll_dentry_data *lld;
48
49         lld = container_of(head, struct ll_dentry_data, lld_rcu_head);
50         OBD_FREE_PTR(lld);
51 }
52
53 /* should NOT be called with the dcache lock, see fs/dcache.c */
54 static void ll_release(struct dentry *de)
55 {
56         struct ll_dentry_data *lld;
57         ENTRY;
58         LASSERT(de != NULL);
59         lld = ll_d2d(de);
60         if (lld == NULL) /* NFS copies the de->d_op methods (bug 4655) */
61                 RETURN_EXIT;
62
63         if (lld->lld_it) {
64                 ll_intent_release(lld->lld_it);
65                 OBD_FREE(lld->lld_it, sizeof(*lld->lld_it));
66         }
67
68         de->d_fsdata = NULL;
69         call_rcu(&lld->lld_rcu_head, free_dentry_data);
70
71         EXIT;
72 }
73
74 /* Compare if two dentries are the same.  Don't match if the existing dentry
75  * is marked invalid.  Returns 1 if different, 0 if the same.
76  *
77  * This avoids a race where ll_lookup_it() instantiates a dentry, but we get
78  * an AST before calling d_revalidate_it().  The dentry still exists (marked
79  * INVALID) so d_lookup() matches it, but we have no lock on it (so
80  * lock_match() fails) and we spin around real_lookup().
81  *
82  * This race doesn't apply to lookups in d_alloc_parallel(), and for
83  * those we want to ensure that only one dentry with a given name is
84  * in ll_lookup_nd() at a time.  So allow invalid dentries to match
85  * while d_in_lookup().  We will be called again when the lookup
86  * completes, and can give a different answer then.
87  */
88 #ifdef HAVE_D_COMPARE_7ARGS
89 static int ll_dcompare(const struct dentry *parent, const struct inode *pinode,
90                        const struct dentry *dentry, const struct inode *inode,
91                        unsigned int len, const char *str,
92                        const struct qstr *name)
93 #elif defined(HAVE_D_COMPARE_5ARGS)
94 static int ll_dcompare(const struct dentry *parent, const struct dentry *dentry,
95                        unsigned int len, const char *str,
96                        const struct qstr *name)
97 #elif defined(HAVE_D_COMPARE_4ARGS)
98 static int ll_dcompare(const struct dentry *dentry, unsigned int len,
99                        const char *str, const struct qstr *name)
100 #else
101 static int ll_dcompare(struct dentry *parent, struct qstr *d_name,
102                        struct qstr *name)
103 #endif
104 {
105 #if !defined(HAVE_D_COMPARE_7ARGS) && !defined(HAVE_D_COMPARE_5ARGS) && !defined(HAVE_D_COMPARE_4ARGS)
106         /* XXX: (ugh !) d_name must be in-dentry structure */
107         struct dentry *dentry = container_of(d_name, struct dentry, d_name);
108         unsigned int len = d_name->len;
109         const char *str = d_name->name;
110 #endif
111         ENTRY;
112
113         if (len != name->len)
114                 RETURN(1);
115
116         if (memcmp(str, name->name, len))
117                 RETURN(1);
118
119         CDEBUG(D_DENTRY, "found name %.*s(%p) flags %#x refc %d\n",
120                name->len, name->name, dentry, dentry->d_flags,
121                ll_d_count(dentry));
122
123         /* mountpoint is always valid */
124         if (d_mountpoint((struct dentry *)dentry))
125                 RETURN(0);
126
127         /* ensure exclusion against parallel lookup of the same name */
128         if (d_in_lookup((struct dentry *)dentry))
129                 return 0;
130
131         if (d_lustre_invalid(dentry))
132                 RETURN(1);
133
134         RETURN(0);
135 }
136
137 /**
138  * Called when last reference to a dentry is dropped and dcache wants to know
139  * whether or not it should cache it:
140  * - return 1 to delete the dentry immediately
141  * - return 0 to cache the dentry
142  * Should NOT be called with the dcache lock, see fs/dcache.c
143  */
144 static int ll_ddelete(HAVE_D_DELETE_CONST struct dentry *de)
145 {
146         ENTRY;
147         LASSERT(de);
148
149         CDEBUG(D_DENTRY, "%s dentry %.*s (%p, parent %p, inode %p) %s%s\n",
150                d_lustre_invalid((struct dentry *)de) ? "deleting" : "keeping",
151                de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
152                d_unhashed((struct dentry *)de) ? "" : "hashed,",
153                list_empty(&de->d_subdirs) ? "" : "subdirs");
154
155 #ifdef HAVE_DCACHE_LOCK
156         LASSERT(ll_d_count(de) == 0);
157 #else
158         /* kernel >= 2.6.38 last refcount is decreased after this function. */
159         LASSERT(ll_d_count(de) == 1);
160 #endif
161
162         if (d_lustre_invalid((struct dentry *)de))
163                 RETURN(1);
164         RETURN(0);
165 }
166
167 int ll_d_init(struct dentry *de)
168 {
169         ENTRY;
170         LASSERT(de != NULL);
171
172         CDEBUG(D_DENTRY, "ldd on dentry %.*s (%p) parent %p inode %p refc %d\n",
173                 de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
174                 ll_d_count(de));
175
176         if (de->d_fsdata == NULL) {
177                 struct ll_dentry_data *lld;
178
179                 OBD_ALLOC_PTR(lld);
180                 if (likely(lld != NULL)) {
181                         spin_lock(&de->d_lock);
182                         if (likely(de->d_fsdata == NULL)) {
183 #ifdef HAVE_DCACHE_LOCK
184                                 /* kernel >= 2.6.38 d_op is set in d_alloc() */
185                                 de->d_op = &ll_d_ops;
186                                 smp_mb();
187 #endif
188                                 de->d_fsdata = lld;
189                                 __d_lustre_invalidate(de);
190                         } else {
191                                 OBD_FREE_PTR(lld);
192                         }
193                         spin_unlock(&de->d_lock);
194                 } else {
195                         RETURN(-ENOMEM);
196                 }
197         }
198         LASSERT(de->d_op == &ll_d_ops);
199
200         RETURN(0);
201 }
202
203 void ll_intent_drop_lock(struct lookup_intent *it)
204 {
205         if (it->it_op && it->it_lock_mode) {
206                 struct lustre_handle handle;
207
208                 handle.cookie = it->it_lock_handle;
209
210                 CDEBUG(D_DLMTRACE, "releasing lock with cookie %#llx from it %p\n",
211                        handle.cookie, it);
212                 ldlm_lock_decref(&handle, it->it_lock_mode);
213
214                 /* bug 494: intent_release may be called multiple times, from
215                  * this thread and we don't want to double-decref this lock */
216                 it->it_lock_mode = 0;
217                 if (it->it_remote_lock_mode != 0) {
218                         handle.cookie = it->it_remote_lock_handle;
219
220                         CDEBUG(D_DLMTRACE, "releasing remote lock with cookie"
221                                "%#llx from it %p\n", handle.cookie, it);
222                         ldlm_lock_decref(&handle,
223                                          it->it_remote_lock_mode);
224                         it->it_remote_lock_mode = 0;
225                 }
226         }
227 }
228
229 void ll_intent_release(struct lookup_intent *it)
230 {
231         ENTRY;
232
233         CDEBUG(D_INFO, "intent %p released\n", it);
234         ll_intent_drop_lock(it);
235         /* We are still holding extra reference on a request, need to free it */
236         if (it_disposition(it, DISP_ENQ_OPEN_REF))
237                 ptlrpc_req_finished(it->it_request); /* ll_file_open */
238
239         if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
240                 ptlrpc_req_finished(it->it_request);
241
242         it->it_disposition = 0;
243         it->it_request = NULL;
244         EXIT;
245 }
246
247 void ll_invalidate_aliases(struct inode *inode)
248 {
249         struct dentry *dentry;
250         DECLARE_LL_D_HLIST_NODE_PTR(p);
251         ENTRY;
252
253         LASSERT(inode != NULL);
254
255         CDEBUG(D_INODE, "marking dentries for inode "DFID"(%p) invalid\n",
256                PFID(ll_inode2fid(inode)), inode);
257
258         ll_lock_dcache(inode);
259         ll_d_hlist_for_each_entry(dentry, p, &inode->i_dentry) {
260                 CDEBUG(D_DENTRY, "dentry in drop %.*s (%p) parent %p "
261                        "inode %p flags %d\n", dentry->d_name.len,
262                        dentry->d_name.name, dentry, dentry->d_parent,
263                        dentry->d_inode, dentry->d_flags);
264
265                 d_lustre_invalidate(dentry, 0);
266         }
267         ll_unlock_dcache(inode);
268
269         EXIT;
270 }
271
272 int ll_revalidate_it_finish(struct ptlrpc_request *request,
273                             struct lookup_intent *it,
274                             struct dentry *de)
275 {
276         int rc = 0;
277         ENTRY;
278
279         if (!request)
280                 RETURN(0);
281
282         if (it_disposition(it, DISP_LOOKUP_NEG))
283                 RETURN(-ENOENT);
284
285         rc = ll_prep_inode(&de->d_inode, request, NULL, it);
286
287         RETURN(rc);
288 }
289
290 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
291 {
292         LASSERT(it != NULL);
293         LASSERT(dentry != NULL);
294
295         if (it->it_lock_mode && dentry->d_inode != NULL) {
296                 struct inode *inode = dentry->d_inode;
297                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
298
299                 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)\n",
300                        PFID(ll_inode2fid(inode)), inode);
301                 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
302         }
303
304         /* drop lookup or getattr locks immediately */
305         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
306                 /* on 2.6 there are situation when several lookups and
307                  * revalidations may be requested during single operation.
308                  * therefore, we don't release intent here -bzzz */
309                 ll_intent_drop_lock(it);
310         }
311 }
312
313 static int ll_revalidate_dentry(struct dentry *dentry,
314                                 unsigned int lookup_flags)
315 {
316         struct inode *dir = dentry->d_parent->d_inode;
317
318         /* If this is intermediate component path lookup and we were able to get
319          * to this dentry, then its lock has not been revoked and the
320          * path component is valid. */
321         if (lookup_flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))
322                 return 1;
323
324         /* Symlink - always valid as long as the dentry was found */
325 #ifdef HAVE_IOP_GET_LINK
326         if (dentry->d_inode && dentry->d_inode->i_op->get_link)
327 #else
328         if (dentry->d_inode && dentry->d_inode->i_op->follow_link)
329 #endif
330                 return 1;
331
332         /*
333          * VFS warns us that this is the second go around and previous
334          * operation failed (most likely open|creat), so this time
335          * we better talk to the server via the lookup path by name,
336          * not by fid.
337          */
338         if (lookup_flags & LOOKUP_REVAL)
339                 return 0;
340
341 #ifndef HAVE_DCACHE_LOCK
342         if (lookup_flags & LOOKUP_RCU)
343                 return -ECHILD;
344 #endif
345
346         if (dentry_may_statahead(dir, dentry))
347                 ll_statahead(dir, &dentry, dentry->d_inode == NULL);
348
349         return 1;
350 }
351
352 /*
353  * Always trust cached dentries. Update statahead window if necessary.
354  */
355 #ifdef HAVE_IOP_ATOMIC_OPEN
356 static int ll_revalidate_nd(struct dentry *dentry, unsigned int flags)
357 {
358         int rc;
359         ENTRY;
360
361         CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
362                dentry->d_name.name, flags);
363
364         rc = ll_revalidate_dentry(dentry, flags);
365         RETURN(rc);
366 }
367 #else
368 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
369 {
370         int rc;
371         ENTRY;
372
373         /*
374          * this is normally called from NFS export, and we don't know whether
375          * this is the last component.
376          */
377         if (nd == NULL)
378                 RETURN(1);
379
380         CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
381                dentry->d_name.name, nd->flags);
382
383         rc = ll_revalidate_dentry(dentry, nd->flags);
384         RETURN(rc);
385 }
386 #endif
387
388 const struct dentry_operations ll_d_ops = {
389         .d_revalidate = ll_revalidate_nd,
390         .d_release = ll_release,
391         .d_delete  = ll_ddelete,
392         .d_compare = ll_dcompare,
393 };