Whamcloud - gitweb
LU-3974 client: use standard dentry locking function
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include <linux/fs.h>
38 #include <linux/sched.h>
39 #include <linux/quotaops.h>
40 #include <linux/kernel.h>
41
42 #define DEBUG_SUBSYSTEM S_LLITE
43
44 #include <obd_support.h>
45 #include <lustre_lite.h>
46 #include <lustre/lustre_idl.h>
47 #include <lustre_dlm.h>
48
49 #include "llite_internal.h"
50
51 static void free_dentry_data(struct rcu_head *head)
52 {
53         struct ll_dentry_data *lld;
54
55         lld = container_of(head, struct ll_dentry_data, lld_rcu_head);
56         OBD_FREE_PTR(lld);
57 }
58
59 /* should NOT be called with the dcache lock, see fs/dcache.c */
60 static void ll_release(struct dentry *de)
61 {
62         struct ll_dentry_data *lld;
63         ENTRY;
64         LASSERT(de != NULL);
65         lld = ll_d2d(de);
66         if (lld == NULL) /* NFS copies the de->d_op methods (bug 4655) */
67                 RETURN_EXIT;
68
69         if (lld->lld_it) {
70                 ll_intent_release(lld->lld_it);
71                 OBD_FREE(lld->lld_it, sizeof(*lld->lld_it));
72         }
73         LASSERT(lld->lld_cwd_count == 0);
74         LASSERT(lld->lld_mnt_count == 0);
75         de->d_fsdata = NULL;
76         call_rcu(&lld->lld_rcu_head, free_dentry_data);
77
78         EXIT;
79 }
80
81 /* Compare if two dentries are the same.  Don't match if the existing dentry
82  * is marked invalid.  Returns 1 if different, 0 if the same.
83  *
84  * This avoids a race where ll_lookup_it() instantiates a dentry, but we get
85  * an AST before calling d_revalidate_it().  The dentry still exists (marked
86  * INVALID) so d_lookup() matches it, but we have no lock on it (so
87  * lock_match() fails) and we spin around real_lookup(). */
88 #ifdef HAVE_D_COMPARE_7ARGS
89 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, const struct qstr *name)
92 #else
93 int ll_dcompare(struct dentry *parent, struct qstr *d_name, struct qstr *name)
94 #endif
95 {
96 #ifdef HAVE_D_COMPARE_7ARGS
97         ENTRY;
98
99         if (len != name->len)
100                 RETURN(1);
101
102         if (memcmp(str, name->name, len))
103                 RETURN(1);
104 #else
105         struct dentry *dentry;
106         ENTRY;
107
108         if (d_name->len != name->len)
109                 RETURN(1);
110
111         if (memcmp(d_name->name, name->name, name->len))
112                 RETURN(1);
113
114         /* XXX: d_name must be in-dentry structure */
115         dentry = container_of(d_name, struct dentry, d_name); /* ugh */
116 #endif
117
118         CDEBUG(D_DENTRY, "found name %.*s(%p) flags %#x refc %d\n",
119                name->len, name->name, dentry, dentry->d_flags,
120                d_count(dentry));
121
122         /* mountpoint is always valid */
123         if (d_mountpoint((struct dentry *)dentry))
124                 RETURN(0);
125
126         if (d_lustre_invalid(dentry))
127                 RETURN(1);
128
129         RETURN(0);
130 }
131
132 static inline int return_if_equal(struct ldlm_lock *lock, void *data)
133 {
134         return (ldlm_is_canceling(lock) && ldlm_is_discard_data(lock)) ?
135                 LDLM_ITER_CONTINUE : LDLM_ITER_STOP;
136 }
137
138 /* find any ldlm lock of the inode in mdc and lov
139  * return 0    not find
140  *        1    find one
141  *      < 0    error */
142 static int find_cbdata(struct inode *inode)
143 {
144         struct ll_sb_info *sbi = ll_i2sbi(inode);
145         struct lov_stripe_md *lsm;
146         int rc = 0;
147         ENTRY;
148
149         LASSERT(inode);
150         rc = md_find_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
151                             return_if_equal, NULL);
152         if (rc != 0)
153                  RETURN(rc);
154
155         lsm = ccc_inode_lsm_get(inode);
156         if (lsm == NULL)
157                 RETURN(rc);
158
159         rc = obd_find_cbdata(sbi->ll_dt_exp, lsm, return_if_equal, NULL);
160         ccc_inode_lsm_put(inode, lsm);
161
162         RETURN(rc);
163 }
164
165 /**
166  * Called when last reference to a dentry is dropped and dcache wants to know
167  * whether or not it should cache it:
168  * - return 1 to delete the dentry immediately
169  * - return 0 to cache the dentry
170  * Should NOT be called with the dcache lock, see fs/dcache.c
171  */
172 static int ll_ddelete(HAVE_D_DELETE_CONST struct dentry *de)
173 {
174         ENTRY;
175         LASSERT(de);
176
177         CDEBUG(D_DENTRY, "%s dentry %.*s (%p, parent %p, inode %p) %s%s\n",
178                d_lustre_invalid((struct dentry *)de) ? "deleting" : "keeping",
179                de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
180                d_unhashed((struct dentry *)de) ? "" : "hashed,",
181                list_empty(&de->d_subdirs) ? "" : "subdirs");
182
183 #ifdef HAVE_DCACHE_LOCK
184         LASSERT(d_count(de) == 0);
185 #else
186         /* kernel >= 2.6.38 last refcount is decreased after this function. */
187         LASSERT(d_count(de) == 1);
188 #endif
189
190         /* Disable this piece of code temproarily because this is called
191          * inside dcache_lock so it's not appropriate to do lots of work
192          * here. ATTENTION: Before this piece of code enabling, LU-2487 must be
193          * resolved. */
194 #if 0
195         /* if not ldlm lock for this inode, set i_nlink to 0 so that
196          * this inode can be recycled later b=20433 */
197         if (de->d_inode && !find_cbdata(de->d_inode))
198                 clear_nlink(de->d_inode);
199 #endif
200
201         if (d_lustre_invalid((struct dentry *)de))
202                 RETURN(1);
203         RETURN(0);
204 }
205
206 int ll_d_init(struct dentry *de)
207 {
208         ENTRY;
209         LASSERT(de != NULL);
210
211         CDEBUG(D_DENTRY, "ldd on dentry %.*s (%p) parent %p inode %p refc %d\n",
212                 de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
213                 d_count(de));
214
215         if (de->d_fsdata == NULL) {
216                 struct ll_dentry_data *lld;
217
218                 OBD_ALLOC_PTR(lld);
219                 if (likely(lld != NULL)) {
220                         spin_lock(&de->d_lock);
221                         if (likely(de->d_fsdata == NULL)) {
222                                 de->d_fsdata = lld;
223                                 __d_lustre_invalidate(de);
224 #ifdef HAVE_DCACHE_LOCK
225                                 /* kernel >= 2.6.38 d_op is set in d_alloc() */
226                                 de->d_op = &ll_d_ops;
227 #endif
228                         } else {
229                                 OBD_FREE_PTR(lld);
230                         }
231                         spin_unlock(&de->d_lock);
232                 } else {
233                         RETURN(-ENOMEM);
234                 }
235         }
236         LASSERT(de->d_op == &ll_d_ops);
237
238         RETURN(0);
239 }
240
241 void ll_intent_drop_lock(struct lookup_intent *it)
242 {
243         if (it->it_op && it->d.lustre.it_lock_mode) {
244                 struct lustre_handle handle;
245
246                 handle.cookie = it->d.lustre.it_lock_handle;
247
248                 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
249                        " from it %p\n", handle.cookie, it);
250                 ldlm_lock_decref(&handle, it->d.lustre.it_lock_mode);
251
252                 /* bug 494: intent_release may be called multiple times, from
253                  * this thread and we don't want to double-decref this lock */
254                 it->d.lustre.it_lock_mode = 0;
255                 if (it->d.lustre.it_remote_lock_mode != 0) {
256                         handle.cookie = it->d.lustre.it_remote_lock_handle;
257
258                         CDEBUG(D_DLMTRACE, "releasing remote lock with cookie"
259                                LPX64" from it %p\n", handle.cookie, it);
260                         ldlm_lock_decref(&handle,
261                                          it->d.lustre.it_remote_lock_mode);
262                         it->d.lustre.it_remote_lock_mode = 0;
263                 }
264         }
265 }
266
267 void ll_intent_release(struct lookup_intent *it)
268 {
269         ENTRY;
270
271         CDEBUG(D_INFO, "intent %p released\n", it);
272         ll_intent_drop_lock(it);
273         /* We are still holding extra reference on a request, need to free it */
274         if (it_disposition(it, DISP_ENQ_OPEN_REF))
275                  ptlrpc_req_finished(it->d.lustre.it_data); /* ll_file_open */
276         if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
277                 ptlrpc_req_finished(it->d.lustre.it_data);
278
279         it->d.lustre.it_disposition = 0;
280         it->d.lustre.it_data = NULL;
281         EXIT;
282 }
283
284 void ll_invalidate_aliases(struct inode *inode)
285 {
286         struct dentry *dentry;
287         DECLARE_LL_D_HLIST_NODE_PTR(p);
288         ENTRY;
289
290         LASSERT(inode != NULL);
291
292         CDEBUG(D_INODE, "marking dentries for inode "DFID"(%p) invalid\n",
293                PFID(ll_inode2fid(inode)), inode);
294
295         ll_lock_dcache(inode);
296         ll_d_hlist_for_each_entry(dentry, p, &inode->i_dentry, d_alias) {
297                 CDEBUG(D_DENTRY, "dentry in drop %.*s (%p) parent %p "
298                        "inode %p flags %d\n", dentry->d_name.len,
299                        dentry->d_name.name, dentry, dentry->d_parent,
300                        dentry->d_inode, dentry->d_flags);
301
302                 if (unlikely(dentry == dentry->d_sb->s_root)) {
303                         CERROR("%s: called on root dentry=%p, fid="DFID"\n",
304                                ll_get_fsname(dentry->d_sb, NULL, 0),
305                                dentry, PFID(ll_inode2fid(inode)));
306                         lustre_dump_dentry(dentry, 1);
307                         libcfs_debug_dumpstack(NULL);
308                 }
309
310                 d_lustre_invalidate(dentry, 0);
311         }
312         ll_unlock_dcache(inode);
313
314         EXIT;
315 }
316
317 int ll_revalidate_it_finish(struct ptlrpc_request *request,
318                             struct lookup_intent *it,
319                             struct dentry *de)
320 {
321         int rc = 0;
322         ENTRY;
323
324         if (!request)
325                 RETURN(0);
326
327         if (it_disposition(it, DISP_LOOKUP_NEG))
328                 RETURN(-ENOENT);
329
330         rc = ll_prep_inode(&de->d_inode, request, NULL, it);
331
332         RETURN(rc);
333 }
334
335 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
336 {
337         LASSERT(it != NULL);
338         LASSERT(dentry != NULL);
339
340         if (it->d.lustre.it_lock_mode && dentry->d_inode != NULL) {
341                 struct inode *inode = dentry->d_inode;
342                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
343
344                 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)\n",
345                        PFID(ll_inode2fid(inode)), inode);
346                 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
347         }
348
349         /* drop lookup or getattr locks immediately */
350         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
351                 /* on 2.6 there are situation when several lookups and
352                  * revalidations may be requested during single operation.
353                  * therefore, we don't release intent here -bzzz */
354                 ll_intent_drop_lock(it);
355         }
356 }
357
358 void ll_frob_intent(struct lookup_intent **itp, struct lookup_intent *deft)
359 {
360         struct lookup_intent *it = *itp;
361
362         if (!it || it->it_op == IT_GETXATTR)
363                 it = *itp = deft;
364
365 }
366
367 static int ll_revalidate_dentry(struct dentry *dentry,
368                                 unsigned int lookup_flags)
369 {
370         struct inode *dir = dentry->d_parent->d_inode;
371
372         /*
373          * if open&create is set, talk to MDS to make sure file is created if
374          * necessary, because we can't do this in ->open() later since that's
375          * called on an inode. return 0 here to let lookup to handle this.
376          */
377         if ((lookup_flags & (LOOKUP_OPEN | LOOKUP_CREATE)) ==
378                         (LOOKUP_OPEN | LOOKUP_CREATE))
379                 return 0;
380
381         if (lookup_flags & (LOOKUP_PARENT | LOOKUP_OPEN | LOOKUP_CREATE))
382                 return 1;
383
384         if (d_need_statahead(dir, dentry) <= 0)
385                 return 1;
386
387 #ifndef HAVE_DCACHE_LOCK
388         if (lookup_flags & LOOKUP_RCU)
389                 return -ECHILD;
390 #endif
391
392         do_statahead_enter(dir, &dentry, dentry->d_inode == NULL);
393         ll_statahead_mark(dir, dentry);
394         return 1;
395 }
396
397 /*
398  * Always trust cached dentries. Update statahead window if necessary.
399  */
400 #ifdef HAVE_IOP_ATOMIC_OPEN
401 int ll_revalidate_nd(struct dentry *dentry, unsigned int flags)
402 {
403         int rc;
404         ENTRY;
405
406         CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
407                dentry->d_name.name, flags);
408
409         rc = ll_revalidate_dentry(dentry, flags);
410         RETURN(rc);
411 }
412 #else
413 int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
414 {
415         int rc;
416         ENTRY;
417
418         /*
419          * this is normally called from NFS export, and we don't know whether
420          * this is the last component.
421          */
422         if (nd == NULL)
423                 RETURN(1);
424
425         CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
426                dentry->d_name.name, nd->flags);
427
428         rc = ll_revalidate_dentry(dentry, nd->flags);
429         RETURN(rc);
430 }
431 #endif
432
433 void ll_d_iput(struct dentry *de, struct inode *inode)
434 {
435         LASSERT(inode);
436         if (!find_cbdata(inode))
437                 clear_nlink(inode);
438         iput(inode);
439 }
440
441 struct dentry_operations ll_d_ops = {
442         .d_revalidate = ll_revalidate_nd,
443         .d_release = ll_release,
444         .d_delete  = ll_ddelete,
445         .d_iput    = ll_d_iput,
446         .d_compare = ll_dcompare,
447 };