Whamcloud - gitweb
LU-4423 llite: use 64-bit times in another debug print
[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, 2016, 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/lustre_idl.h>
42 #include <lustre_dlm.h>
43
44 #include "llite_internal.h"
45
46 static void free_dentry_data(struct rcu_head *head)
47 {
48         struct ll_dentry_data *lld;
49
50         lld = container_of(head, struct ll_dentry_data, lld_rcu_head);
51         OBD_FREE_PTR(lld);
52 }
53
54 /* should NOT be called with the dcache lock, see fs/dcache.c */
55 static void ll_release(struct dentry *de)
56 {
57         struct ll_dentry_data *lld;
58         ENTRY;
59         LASSERT(de != NULL);
60         lld = ll_d2d(de);
61         if (lld == NULL) /* NFS copies the de->d_op methods (bug 4655) */
62                 RETURN_EXIT;
63
64         if (lld->lld_it) {
65                 ll_intent_release(lld->lld_it);
66                 OBD_FREE(lld->lld_it, sizeof(*lld->lld_it));
67         }
68
69         de->d_fsdata = NULL;
70         call_rcu(&lld->lld_rcu_head, free_dentry_data);
71
72         EXIT;
73 }
74
75 /* Compare if two dentries are the same.  Don't match if the existing dentry
76  * is marked invalid.  Returns 1 if different, 0 if the same.
77  *
78  * This avoids a race where ll_lookup_it() instantiates a dentry, but we get
79  * an AST before calling d_revalidate_it().  The dentry still exists (marked
80  * INVALID) so d_lookup() matches it, but we have no lock on it (so
81  * lock_match() fails) and we spin around real_lookup(). */
82 #ifdef HAVE_D_COMPARE_7ARGS
83 static int ll_dcompare(const struct dentry *parent, const struct inode *pinode,
84                        const struct dentry *dentry, const struct inode *inode,
85                        unsigned int len, const char *str,
86                        const struct qstr *name)
87 #elif defined(HAVE_D_COMPARE_5ARGS)
88 static int ll_dcompare(const struct dentry *parent, const struct dentry *dentry,
89                        unsigned int len, const char *str,
90                        const struct qstr *name)
91 #else
92 static int ll_dcompare(struct dentry *parent, struct qstr *d_name,
93                        struct qstr *name)
94 #endif
95 {
96 #if !defined(HAVE_D_COMPARE_7ARGS) && !defined(HAVE_D_COMPARE_5ARGS)
97         /* XXX: (ugh !) d_name must be in-dentry structure */
98         struct dentry *dentry = container_of(d_name, struct dentry, d_name);
99         unsigned int len = d_name->len;
100         const char *str = d_name->name;
101 #endif
102         ENTRY;
103
104         if (len != name->len)
105                 RETURN(1);
106
107         if (memcmp(str, name->name, len))
108                 RETURN(1);
109
110         CDEBUG(D_DENTRY, "found name %.*s(%p) flags %#x refc %d\n",
111                name->len, name->name, dentry, dentry->d_flags,
112                ll_d_count(dentry));
113
114         /* mountpoint is always valid */
115         if (d_mountpoint((struct dentry *)dentry))
116                 RETURN(0);
117
118         if (d_lustre_invalid(dentry))
119                 RETURN(1);
120
121         RETURN(0);
122 }
123
124 /**
125  * Called when last reference to a dentry is dropped and dcache wants to know
126  * whether or not it should cache it:
127  * - return 1 to delete the dentry immediately
128  * - return 0 to cache the dentry
129  * Should NOT be called with the dcache lock, see fs/dcache.c
130  */
131 static int ll_ddelete(HAVE_D_DELETE_CONST struct dentry *de)
132 {
133         ENTRY;
134         LASSERT(de);
135
136         CDEBUG(D_DENTRY, "%s dentry %.*s (%p, parent %p, inode %p) %s%s\n",
137                d_lustre_invalid((struct dentry *)de) ? "deleting" : "keeping",
138                de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
139                d_unhashed((struct dentry *)de) ? "" : "hashed,",
140                list_empty(&de->d_subdirs) ? "" : "subdirs");
141
142 #ifdef HAVE_DCACHE_LOCK
143         LASSERT(ll_d_count(de) == 0);
144 #else
145         /* kernel >= 2.6.38 last refcount is decreased after this function. */
146         LASSERT(ll_d_count(de) == 1);
147 #endif
148
149         if (d_lustre_invalid((struct dentry *)de))
150                 RETURN(1);
151         RETURN(0);
152 }
153
154 int ll_d_init(struct dentry *de)
155 {
156         ENTRY;
157         LASSERT(de != NULL);
158
159         CDEBUG(D_DENTRY, "ldd on dentry %.*s (%p) parent %p inode %p refc %d\n",
160                 de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
161                 ll_d_count(de));
162
163         if (de->d_fsdata == NULL) {
164                 struct ll_dentry_data *lld;
165
166                 OBD_ALLOC_PTR(lld);
167                 if (likely(lld != NULL)) {
168                         spin_lock(&de->d_lock);
169                         if (likely(de->d_fsdata == NULL)) {
170                                 de->d_fsdata = lld;
171                                 __d_lustre_invalidate(de);
172 #ifdef HAVE_DCACHE_LOCK
173                                 /* kernel >= 2.6.38 d_op is set in d_alloc() */
174                                 de->d_op = &ll_d_ops;
175 #endif
176                         } else {
177                                 OBD_FREE_PTR(lld);
178                         }
179                         spin_unlock(&de->d_lock);
180                 } else {
181                         RETURN(-ENOMEM);
182                 }
183         }
184         LASSERT(de->d_op == &ll_d_ops);
185
186         RETURN(0);
187 }
188
189 void ll_intent_drop_lock(struct lookup_intent *it)
190 {
191         if (it->it_op && it->it_lock_mode) {
192                 struct lustre_handle handle;
193
194                 handle.cookie = it->it_lock_handle;
195
196                 CDEBUG(D_DLMTRACE, "releasing lock with cookie %#llx from it %p\n",
197                        handle.cookie, it);
198                 ldlm_lock_decref(&handle, it->it_lock_mode);
199
200                 /* bug 494: intent_release may be called multiple times, from
201                  * this thread and we don't want to double-decref this lock */
202                 it->it_lock_mode = 0;
203                 if (it->it_remote_lock_mode != 0) {
204                         handle.cookie = it->it_remote_lock_handle;
205
206                         CDEBUG(D_DLMTRACE, "releasing remote lock with cookie"
207                                "%#llx from it %p\n", handle.cookie, it);
208                         ldlm_lock_decref(&handle,
209                                          it->it_remote_lock_mode);
210                         it->it_remote_lock_mode = 0;
211                 }
212         }
213 }
214
215 void ll_intent_release(struct lookup_intent *it)
216 {
217         ENTRY;
218
219         CDEBUG(D_INFO, "intent %p released\n", it);
220         ll_intent_drop_lock(it);
221         /* We are still holding extra reference on a request, need to free it */
222         if (it_disposition(it, DISP_ENQ_OPEN_REF))
223                 ptlrpc_req_finished(it->it_request); /* ll_file_open */
224
225         if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
226                 ptlrpc_req_finished(it->it_request);
227
228         it->it_disposition = 0;
229         it->it_request = NULL;
230         EXIT;
231 }
232
233 void ll_invalidate_aliases(struct inode *inode)
234 {
235         struct dentry *dentry;
236         DECLARE_LL_D_HLIST_NODE_PTR(p);
237         ENTRY;
238
239         LASSERT(inode != NULL);
240
241         CDEBUG(D_INODE, "marking dentries for inode "DFID"(%p) invalid\n",
242                PFID(ll_inode2fid(inode)), inode);
243
244         ll_lock_dcache(inode);
245         ll_d_hlist_for_each_entry(dentry, p, &inode->i_dentry) {
246                 CDEBUG(D_DENTRY, "dentry in drop %.*s (%p) parent %p "
247                        "inode %p flags %d\n", dentry->d_name.len,
248                        dentry->d_name.name, dentry, dentry->d_parent,
249                        dentry->d_inode, dentry->d_flags);
250
251                 if (unlikely(dentry == dentry->d_sb->s_root)) {
252                         CERROR("%s: called on root dentry=%p, fid="DFID"\n",
253                                ll_get_fsname(dentry->d_sb, NULL, 0),
254                                dentry, PFID(ll_inode2fid(inode)));
255                         lustre_dump_dentry(dentry, 1);
256                         libcfs_debug_dumpstack(NULL);
257                 }
258
259                 d_lustre_invalidate(dentry, 0);
260         }
261         ll_unlock_dcache(inode);
262
263         EXIT;
264 }
265
266 int ll_revalidate_it_finish(struct ptlrpc_request *request,
267                             struct lookup_intent *it,
268                             struct dentry *de)
269 {
270         int rc = 0;
271         ENTRY;
272
273         if (!request)
274                 RETURN(0);
275
276         if (it_disposition(it, DISP_LOOKUP_NEG))
277                 RETURN(-ENOENT);
278
279         rc = ll_prep_inode(&de->d_inode, request, NULL, it);
280
281         RETURN(rc);
282 }
283
284 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
285 {
286         LASSERT(it != NULL);
287         LASSERT(dentry != NULL);
288
289         if (it->it_lock_mode && dentry->d_inode != NULL) {
290                 struct inode *inode = dentry->d_inode;
291                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
292
293                 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)\n",
294                        PFID(ll_inode2fid(inode)), inode);
295                 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
296         }
297
298         /* drop lookup or getattr locks immediately */
299         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
300                 /* on 2.6 there are situation when several lookups and
301                  * revalidations may be requested during single operation.
302                  * therefore, we don't release intent here -bzzz */
303                 ll_intent_drop_lock(it);
304         }
305 }
306
307 static int ll_revalidate_dentry(struct dentry *dentry,
308                                 unsigned int lookup_flags)
309 {
310         struct inode *dir = dentry->d_parent->d_inode;
311
312         /* If this is intermediate component path lookup and we were able to get
313          * to this dentry, then its lock has not been revoked and the
314          * path component is valid. */
315         if (lookup_flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))
316                 return 1;
317
318         /* Symlink - always valid as long as the dentry was found */
319 #ifdef HAVE_IOP_GET_LINK
320         if (dentry->d_inode && dentry->d_inode->i_op->get_link)
321 #else
322         if (dentry->d_inode && dentry->d_inode->i_op->follow_link)
323 #endif
324                 return 1;
325
326         /*
327          * VFS warns us that this is the second go around and previous
328          * operation failed (most likely open|creat), so this time
329          * we better talk to the server via the lookup path by name,
330          * not by fid.
331          */
332         if (lookup_flags & LOOKUP_REVAL)
333                 return 0;
334
335         if (!dentry_may_statahead(dir, dentry))
336                 return 1;
337
338 #ifndef HAVE_DCACHE_LOCK
339         if (lookup_flags & LOOKUP_RCU)
340                 return -ECHILD;
341 #endif
342
343         ll_statahead(dir, &dentry, dentry->d_inode == NULL);
344         return 1;
345 }
346
347 /*
348  * Always trust cached dentries. Update statahead window if necessary.
349  */
350 #ifdef HAVE_IOP_ATOMIC_OPEN
351 static int ll_revalidate_nd(struct dentry *dentry, unsigned int flags)
352 {
353         int rc;
354         ENTRY;
355
356         CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
357                dentry->d_name.name, flags);
358
359         rc = ll_revalidate_dentry(dentry, flags);
360         RETURN(rc);
361 }
362 #else
363 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
364 {
365         int rc;
366         ENTRY;
367
368         /*
369          * this is normally called from NFS export, and we don't know whether
370          * this is the last component.
371          */
372         if (nd == NULL)
373                 RETURN(1);
374
375         CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
376                dentry->d_name.name, nd->flags);
377
378         rc = ll_revalidate_dentry(dentry, nd->flags);
379         RETURN(rc);
380 }
381 #endif
382
383 const struct dentry_operations ll_d_ops = {
384         .d_revalidate = ll_revalidate_nd,
385         .d_release = ll_release,
386         .d_delete  = ll_ddelete,
387         .d_compare = ll_dcompare,
388 };