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, 2015, 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>
37 #define DEBUG_SUBSYSTEM S_LLITE
39 #include "../include/obd_support.h"
40 #include "../include/lustre/lustre_idl.h"
41 #include "../include/lustre_dlm.h"
43 #include "llite_internal.h"
45 static void free_dentry_data(struct rcu_head *head)
47 struct ll_dentry_data *lld;
49 lld = container_of(head, struct ll_dentry_data, lld_rcu_head);
53 /* should NOT be called with the dcache lock, see fs/dcache.c */
54 static void ll_release(struct dentry *de)
56 struct ll_dentry_data *lld;
60 if (!lld) /* NFS copies the de->d_op methods (bug 4655) */
64 ll_intent_release(lld->lld_it);
69 call_rcu(&lld->lld_rcu_head, free_dentry_data);
72 /* Compare if two dentries are the same. Don't match if the existing dentry
73 * is marked invalid. Returns 1 if different, 0 if the same.
75 * This avoids a race where ll_lookup_it() instantiates a dentry, but we get
76 * an AST before calling d_revalidate_it(). The dentry still exists (marked
77 * INVALID) so d_lookup() matches it, but we have no lock on it (so
78 * lock_match() fails) and we spin around real_lookup().
80 static int ll_dcompare(const struct dentry *dentry,
81 unsigned int len, const char *str,
82 const struct qstr *name)
87 if (memcmp(str, name->name, len))
90 CDEBUG(D_DENTRY, "found name %.*s(%p) flags %#x refc %d\n",
91 name->len, name->name, dentry, dentry->d_flags,
94 /* mountpoint is always valid */
95 if (d_mountpoint((struct dentry *)dentry))
98 if (d_lustre_invalid(dentry))
105 * Called when last reference to a dentry is dropped and dcache wants to know
106 * whether or not it should cache it:
107 * - return 1 to delete the dentry immediately
108 * - return 0 to cache the dentry
109 * Should NOT be called with the dcache lock, see fs/dcache.c
111 static int ll_ddelete(const struct dentry *de)
115 CDEBUG(D_DENTRY, "%s dentry %pd (%p, parent %p, inode %p) %s%s\n",
116 d_lustre_invalid((struct dentry *)de) ? "deleting" : "keeping",
117 de, de, de->d_parent, d_inode(de),
118 d_unhashed(de) ? "" : "hashed,",
119 list_empty(&de->d_subdirs) ? "" : "subdirs");
121 /* kernel >= 2.6.38 last refcount is decreased after this function. */
122 LASSERT(d_count(de) == 1);
124 if (d_lustre_invalid((struct dentry *)de))
129 int ll_d_init(struct dentry *de)
131 CDEBUG(D_DENTRY, "ldd on dentry %pd (%p) parent %p inode %p refc %d\n",
132 de, de, de->d_parent, d_inode(de), d_count(de));
135 struct ll_dentry_data *lld;
137 lld = kzalloc(sizeof(*lld), GFP_NOFS);
139 spin_lock(&de->d_lock);
140 if (likely(!de->d_fsdata)) {
142 __d_lustre_invalidate(de);
146 spin_unlock(&de->d_lock);
151 LASSERT(de->d_op == &ll_d_ops);
156 void ll_intent_drop_lock(struct lookup_intent *it)
158 if (it->it_op && it->it_lock_mode) {
159 struct lustre_handle handle;
161 handle.cookie = it->it_lock_handle;
163 CDEBUG(D_DLMTRACE, "releasing lock with cookie %#llx from it %p\n",
165 ldlm_lock_decref(&handle, it->it_lock_mode);
167 /* bug 494: intent_release may be called multiple times, from
168 * this thread and we don't want to double-decref this lock
170 it->it_lock_mode = 0;
171 if (it->it_remote_lock_mode != 0) {
172 handle.cookie = it->it_remote_lock_handle;
174 CDEBUG(D_DLMTRACE, "releasing remote lock with cookie%#llx from it %p\n",
176 ldlm_lock_decref(&handle,
177 it->it_remote_lock_mode);
178 it->it_remote_lock_mode = 0;
183 void ll_intent_release(struct lookup_intent *it)
185 CDEBUG(D_INFO, "intent %p released\n", it);
186 ll_intent_drop_lock(it);
187 /* We are still holding extra reference on a request, need to free it */
188 if (it_disposition(it, DISP_ENQ_OPEN_REF))
189 ptlrpc_req_finished(it->it_request); /* ll_file_open */
191 if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
192 ptlrpc_req_finished(it->it_request);
194 it->it_disposition = 0;
195 it->it_request = NULL;
198 void ll_invalidate_aliases(struct inode *inode)
200 struct dentry *dentry;
202 CDEBUG(D_INODE, "marking dentries for ino "DFID"(%p) invalid\n",
203 PFID(ll_inode2fid(inode)), inode);
205 spin_lock(&inode->i_lock);
206 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
207 CDEBUG(D_DENTRY, "dentry in drop %pd (%p) parent %p inode %p flags %d\n",
208 dentry, dentry, dentry->d_parent,
209 d_inode(dentry), dentry->d_flags);
211 d_lustre_invalidate(dentry, 0);
213 spin_unlock(&inode->i_lock);
216 int ll_revalidate_it_finish(struct ptlrpc_request *request,
217 struct lookup_intent *it,
225 if (it_disposition(it, DISP_LOOKUP_NEG))
228 rc = ll_prep_inode(&inode, request, NULL, it);
233 void ll_lookup_finish_locks(struct lookup_intent *it, struct inode *inode)
235 if (it->it_lock_mode && inode) {
236 struct ll_sb_info *sbi = ll_i2sbi(inode);
238 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)\n",
239 PFID(ll_inode2fid(inode)), inode);
240 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
243 /* drop lookup or getattr locks immediately */
244 if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
245 /* on 2.6 there are situation when several lookups and
246 * revalidations may be requested during single operation.
247 * therefore, we don't release intent here -bzzz
249 ll_intent_drop_lock(it);
253 static int ll_revalidate_dentry(struct dentry *dentry,
254 unsigned int lookup_flags)
256 struct inode *dir = d_inode(dentry->d_parent);
258 /* If this is intermediate component path lookup and we were able to get
259 * to this dentry, then its lock has not been revoked and the
260 * path component is valid.
262 if (lookup_flags & LOOKUP_PARENT)
265 /* Symlink - always valid as long as the dentry was found */
266 if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode))
270 * if open&create is set, talk to MDS to make sure file is created if
271 * necessary, because we can't do this in ->open() later since that's
272 * called on an inode. return 0 here to let lookup to handle this.
274 if ((lookup_flags & (LOOKUP_OPEN | LOOKUP_CREATE)) ==
275 (LOOKUP_OPEN | LOOKUP_CREATE))
278 if (lookup_flags & (LOOKUP_PARENT | LOOKUP_OPEN | LOOKUP_CREATE))
281 if (!dentry_may_statahead(dir, dentry))
284 if (lookup_flags & LOOKUP_RCU)
287 ll_statahead(dir, &dentry, !d_inode(dentry));
292 * Always trust cached dentries. Update statahead window if necessary.
294 static int ll_revalidate_nd(struct dentry *dentry, unsigned int flags)
296 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, flags=%u\n",
299 return ll_revalidate_dentry(dentry, flags);
302 const struct dentry_operations ll_d_ops = {
303 .d_revalidate = ll_revalidate_nd,
304 .d_release = ll_release,
305 .d_delete = ll_ddelete,
306 .d_compare = ll_dcompare,