4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 #include <linux/sched.h>
35 #include <linux/quotaops.h>
36 #include <linux/kernel.h>
38 #define DEBUG_SUBSYSTEM S_LLITE
40 #include <obd_support.h>
41 #include <lustre/lustre_idl.h>
42 #include <lustre_dlm.h>
44 #include "llite_internal.h"
46 static void free_dentry_data(struct rcu_head *head)
48 struct ll_dentry_data *lld;
50 lld = container_of(head, struct ll_dentry_data, lld_rcu_head);
54 /* should NOT be called with the dcache lock, see fs/dcache.c */
55 static void ll_release(struct dentry *de)
57 struct ll_dentry_data *lld;
61 if (lld == NULL) /* NFS copies the de->d_op methods (bug 4655) */
65 ll_intent_release(lld->lld_it);
66 OBD_FREE(lld->lld_it, sizeof(*lld->lld_it));
70 call_rcu(&lld->lld_rcu_head, free_dentry_data);
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.
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 #elif defined(HAVE_D_COMPARE_4ARGS)
92 static int ll_dcompare(const struct dentry *dentry, unsigned int len,
93 const char *str, const struct qstr *name)
95 static int ll_dcompare(struct dentry *parent, struct qstr *d_name,
99 #if !defined(HAVE_D_COMPARE_7ARGS) && !defined(HAVE_D_COMPARE_5ARGS) && !defined(HAVE_D_COMPARE_4ARGS)
100 /* XXX: (ugh !) d_name must be in-dentry structure */
101 struct dentry *dentry = container_of(d_name, struct dentry, d_name);
102 unsigned int len = d_name->len;
103 const char *str = d_name->name;
107 if (len != name->len)
110 if (memcmp(str, name->name, len))
113 CDEBUG(D_DENTRY, "found name %.*s(%p) flags %#x refc %d\n",
114 name->len, name->name, dentry, dentry->d_flags,
117 /* mountpoint is always valid */
118 if (d_mountpoint((struct dentry *)dentry))
121 if (d_lustre_invalid(dentry))
128 * Called when last reference to a dentry is dropped and dcache wants to know
129 * whether or not it should cache it:
130 * - return 1 to delete the dentry immediately
131 * - return 0 to cache the dentry
132 * Should NOT be called with the dcache lock, see fs/dcache.c
134 static int ll_ddelete(HAVE_D_DELETE_CONST struct dentry *de)
139 CDEBUG(D_DENTRY, "%s dentry %.*s (%p, parent %p, inode %p) %s%s\n",
140 d_lustre_invalid((struct dentry *)de) ? "deleting" : "keeping",
141 de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
142 d_unhashed((struct dentry *)de) ? "" : "hashed,",
143 list_empty(&de->d_subdirs) ? "" : "subdirs");
145 #ifdef HAVE_DCACHE_LOCK
146 LASSERT(ll_d_count(de) == 0);
148 /* kernel >= 2.6.38 last refcount is decreased after this function. */
149 LASSERT(ll_d_count(de) == 1);
152 if (d_lustre_invalid((struct dentry *)de))
157 int ll_d_init(struct dentry *de)
162 CDEBUG(D_DENTRY, "ldd on dentry %.*s (%p) parent %p inode %p refc %d\n",
163 de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
166 if (de->d_fsdata == NULL) {
167 struct ll_dentry_data *lld;
170 if (likely(lld != NULL)) {
171 spin_lock(&de->d_lock);
172 if (likely(de->d_fsdata == NULL)) {
173 #ifdef HAVE_DCACHE_LOCK
174 /* kernel >= 2.6.38 d_op is set in d_alloc() */
175 de->d_op = &ll_d_ops;
179 __d_lustre_invalidate(de);
183 spin_unlock(&de->d_lock);
188 LASSERT(de->d_op == &ll_d_ops);
193 void ll_intent_drop_lock(struct lookup_intent *it)
195 if (it->it_op && it->it_lock_mode) {
196 struct lustre_handle handle;
198 handle.cookie = it->it_lock_handle;
200 CDEBUG(D_DLMTRACE, "releasing lock with cookie %#llx from it %p\n",
202 ldlm_lock_decref(&handle, it->it_lock_mode);
204 /* bug 494: intent_release may be called multiple times, from
205 * this thread and we don't want to double-decref this lock */
206 it->it_lock_mode = 0;
207 if (it->it_remote_lock_mode != 0) {
208 handle.cookie = it->it_remote_lock_handle;
210 CDEBUG(D_DLMTRACE, "releasing remote lock with cookie"
211 "%#llx from it %p\n", handle.cookie, it);
212 ldlm_lock_decref(&handle,
213 it->it_remote_lock_mode);
214 it->it_remote_lock_mode = 0;
219 void ll_intent_release(struct lookup_intent *it)
223 CDEBUG(D_INFO, "intent %p released\n", it);
224 ll_intent_drop_lock(it);
225 /* We are still holding extra reference on a request, need to free it */
226 if (it_disposition(it, DISP_ENQ_OPEN_REF))
227 ptlrpc_req_finished(it->it_request); /* ll_file_open */
229 if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
230 ptlrpc_req_finished(it->it_request);
232 it->it_disposition = 0;
233 it->it_request = NULL;
237 void ll_invalidate_aliases(struct inode *inode)
239 struct dentry *dentry;
240 DECLARE_LL_D_HLIST_NODE_PTR(p);
243 LASSERT(inode != NULL);
245 CDEBUG(D_INODE, "marking dentries for inode "DFID"(%p) invalid\n",
246 PFID(ll_inode2fid(inode)), inode);
248 ll_lock_dcache(inode);
249 ll_d_hlist_for_each_entry(dentry, p, &inode->i_dentry) {
250 CDEBUG(D_DENTRY, "dentry in drop %.*s (%p) parent %p "
251 "inode %p flags %d\n", dentry->d_name.len,
252 dentry->d_name.name, dentry, dentry->d_parent,
253 dentry->d_inode, dentry->d_flags);
255 d_lustre_invalidate(dentry, 0);
257 ll_unlock_dcache(inode);
262 int ll_revalidate_it_finish(struct ptlrpc_request *request,
263 struct lookup_intent *it,
272 if (it_disposition(it, DISP_LOOKUP_NEG))
275 rc = ll_prep_inode(&de->d_inode, request, NULL, it);
280 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
283 LASSERT(dentry != NULL);
285 if (it->it_lock_mode && dentry->d_inode != NULL) {
286 struct inode *inode = dentry->d_inode;
287 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
289 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)\n",
290 PFID(ll_inode2fid(inode)), inode);
291 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
294 /* drop lookup or getattr locks immediately */
295 if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
296 /* on 2.6 there are situation when several lookups and
297 * revalidations may be requested during single operation.
298 * therefore, we don't release intent here -bzzz */
299 ll_intent_drop_lock(it);
303 static int ll_revalidate_dentry(struct dentry *dentry,
304 unsigned int lookup_flags)
306 struct inode *dir = dentry->d_parent->d_inode;
308 /* If this is intermediate component path lookup and we were able to get
309 * to this dentry, then its lock has not been revoked and the
310 * path component is valid. */
311 if (lookup_flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))
314 /* Symlink - always valid as long as the dentry was found */
315 #ifdef HAVE_IOP_GET_LINK
316 if (dentry->d_inode && dentry->d_inode->i_op->get_link)
318 if (dentry->d_inode && dentry->d_inode->i_op->follow_link)
323 * VFS warns us that this is the second go around and previous
324 * operation failed (most likely open|creat), so this time
325 * we better talk to the server via the lookup path by name,
328 if (lookup_flags & LOOKUP_REVAL)
331 #ifndef HAVE_DCACHE_LOCK
332 if (lookup_flags & LOOKUP_RCU)
336 if (dentry_may_statahead(dir, dentry))
337 ll_statahead(dir, &dentry, dentry->d_inode == NULL);
343 * Always trust cached dentries. Update statahead window if necessary.
345 #ifdef HAVE_IOP_ATOMIC_OPEN
346 static int ll_revalidate_nd(struct dentry *dentry, unsigned int flags)
351 CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
352 dentry->d_name.name, flags);
354 rc = ll_revalidate_dentry(dentry, flags);
358 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
364 * this is normally called from NFS export, and we don't know whether
365 * this is the last component.
370 CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
371 dentry->d_name.name, nd->flags);
373 rc = ll_revalidate_dentry(dentry, nd->flags);
378 const struct dentry_operations ll_d_ops = {
379 .d_revalidate = ll_revalidate_nd,
380 .d_release = ll_release,
381 .d_delete = ll_ddelete,
382 .d_compare = ll_dcompare,