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, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 #include <linux/sched.h>
34 #include <linux/quotaops.h>
35 #include <linux/kernel.h>
37 #define DEBUG_SUBSYSTEM S_LLITE
39 #include <obd_support.h>
40 #include <lustre_dlm.h>
42 #include "llite_internal.h"
44 static void free_dentry_data(struct rcu_head *head)
46 struct ll_dentry_data *lld;
48 lld = container_of(head, struct ll_dentry_data, lld_rcu_head);
52 /* should NOT be called with the dcache lock, see fs/dcache.c */
53 static void ll_release(struct dentry *de)
55 struct ll_dentry_data *lld;
59 if (lld == NULL) /* NFS copies the de->d_op methods (bug 4655) */
63 call_rcu(&lld->lld_rcu_head, free_dentry_data);
68 /* Compare if two dentries are the same. Don't match if the existing dentry
69 * is marked invalid. Returns 1 if different, 0 if the same.
71 * This avoids a race where ll_lookup_it() instantiates a dentry, but we get
72 * an AST before calling d_revalidate_it(). The dentry still exists (marked
73 * INVALID) so d_lookup() matches it, but we have no lock on it (so
74 * lock_match() fails) and we spin around real_lookup().
76 * This race doesn't apply to lookups in d_alloc_parallel(), and for
77 * those we want to ensure that only one dentry with a given name is
78 * in ll_lookup_nd() at a time. So allow invalid dentries to match
79 * while d_in_lookup(). We will be called again when the lookup
80 * completes, and can give a different answer then.
82 #if defined(HAVE_D_COMPARE_5ARGS)
83 static int ll_dcompare(const struct dentry *parent, const struct dentry *dentry,
84 unsigned int len, const char *str,
85 const struct qstr *name)
86 #elif defined(HAVE_D_COMPARE_4ARGS)
87 static int ll_dcompare(const struct dentry *dentry, unsigned int len,
88 const char *str, const struct qstr *name)
96 if (memcmp(str, name->name, len))
99 CDEBUG(D_DENTRY, "found name %.*s(%p) flags %#x refc %d\n",
100 name->len, name->name, dentry, dentry->d_flags,
103 /* mountpoint is always valid */
104 if (d_mountpoint((struct dentry *)dentry))
107 /* ensure exclusion against parallel lookup of the same name */
108 if (d_in_lookup((struct dentry *)dentry))
111 if (d_lustre_invalid(dentry))
118 * Called when last reference to a dentry is dropped and dcache wants to know
119 * whether or not it should cache it:
120 * - return 1 to delete the dentry immediately
121 * - return 0 to cache the dentry
122 * Should NOT be called with the dcache lock, see fs/dcache.c
124 static int ll_ddelete(const struct dentry *de)
129 CDEBUG(D_DENTRY, "%s dentry %pd (%p, parent %p, inode %p) %s%s\n",
130 d_lustre_invalid(de) ? "deleting" : "keeping",
131 de, de, de->d_parent, de->d_inode,
132 d_unhashed((struct dentry *)de) ? "" : "hashed,",
133 list_empty(&de->d_subdirs) ? "" : "subdirs");
135 /* kernel >= 2.6.38 last refcount is decreased after this function. */
136 LASSERT(ll_d_count(de) == 1);
138 if (d_lustre_invalid(de))
143 int ll_d_init(struct dentry *de)
148 CDEBUG(D_DENTRY, "ldd on dentry %pd (%p) parent %p inode %p refc %d\n",
149 de, de, de->d_parent, de->d_inode,
152 if (de->d_fsdata == NULL) {
153 struct ll_dentry_data *lld;
156 if (likely(lld != NULL)) {
157 spin_lock(&de->d_lock);
158 if (likely(de->d_fsdata == NULL)) {
160 __d_lustre_invalidate(de);
164 spin_unlock(&de->d_lock);
169 LASSERT(de->d_op == &ll_d_ops);
174 void ll_intent_drop_lock(struct lookup_intent *it)
176 if (it->it_op && it->it_lock_mode) {
177 struct lustre_handle handle;
179 handle.cookie = it->it_lock_handle;
181 CDEBUG(D_DLMTRACE, "releasing lock with cookie %#llx from it %p\n",
183 ldlm_lock_decref(&handle, it->it_lock_mode);
185 /* bug 494: intent_release may be called multiple times, from
186 * this thread and we don't want to double-decref this lock */
187 it->it_lock_mode = 0;
188 if (it->it_remote_lock_mode != 0) {
189 handle.cookie = it->it_remote_lock_handle;
192 "releasing remote lock with cookie %#llx from it %p\n",
194 ldlm_lock_decref(&handle,
195 it->it_remote_lock_mode);
196 it->it_remote_lock_mode = 0;
201 void ll_intent_release(struct lookup_intent *it)
205 CDEBUG(D_INFO, "intent %p released\n", it);
206 ll_intent_drop_lock(it);
207 /* We are still holding extra reference on a request, need to free it */
208 if (it_disposition(it, DISP_ENQ_OPEN_REF))
209 ptlrpc_req_finished(it->it_request); /* ll_file_open */
211 if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
212 ptlrpc_req_finished(it->it_request);
214 it->it_disposition = 0;
215 it->it_request = NULL;
219 /* mark aliases invalid and prune unused aliases */
220 void ll_prune_aliases(struct inode *inode)
222 struct dentry *dentry;
225 LASSERT(inode != NULL);
227 CDEBUG(D_INODE, "marking dentries for inode "DFID"(%p) invalid\n",
228 PFID(ll_inode2fid(inode)), inode);
230 spin_lock(&inode->i_lock);
231 hlist_for_each_entry(dentry, &inode->i_dentry, d_alias)
232 d_lustre_invalidate(dentry);
233 spin_unlock(&inode->i_lock);
235 d_prune_aliases(inode);
240 int ll_revalidate_it_finish(struct ptlrpc_request *request,
241 struct lookup_intent *it,
251 if (it_disposition(it, DISP_LOOKUP_NEG))
254 rc = ll_prep_inode(&de->d_inode, &request->rq_pill, NULL, it);
259 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
262 LASSERT(dentry != NULL);
264 if (it->it_lock_mode && dentry->d_inode != NULL) {
265 struct inode *inode = dentry->d_inode;
266 struct ll_sb_info *sbi = ll_i2sbi(inode);
268 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)\n",
269 PFID(ll_inode2fid(inode)), inode);
270 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
273 /* drop lookup or getattr locks immediately */
274 if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)
275 ll_intent_drop_lock(it);
278 static int ll_revalidate_dentry(struct dentry *dentry,
279 unsigned int lookup_flags)
281 struct inode *dir = dentry->d_parent->d_inode;
283 CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
284 dentry->d_name.name, lookup_flags);
286 /* If this is intermediate component path lookup and we were able to get
287 * to this dentry, then its lock has not been revoked and the
288 * path component is valid. */
289 if (lookup_flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))
292 /* Symlink - always valid as long as the dentry was found */
293 /* only special case is to prevent ELOOP error from VFS during open
294 * of a foreign symlink file/dir with O_NOFOLLOW, like it happens for
295 * real symlinks. This will allow to open foreign symlink file/dir
296 * for get[dir]stripe/unlock ioctl()s.
298 if (d_is_symlink(dentry)) {
299 if (!S_ISLNK(dentry->d_inode->i_mode) &&
300 !(lookup_flags & LOOKUP_FOLLOW))
307 * VFS warns us that this is the second go around and previous
308 * operation failed (most likely open|creat), so this time
309 * we better talk to the server via the lookup path by name,
312 if (lookup_flags & LOOKUP_REVAL)
315 if (lookup_flags & LOOKUP_RCU)
318 if (dentry_may_statahead(dir, dentry))
319 ll_revalidate_statahead(dir, &dentry, dentry->d_inode == NULL);
324 const struct dentry_operations ll_d_ops = {
325 .d_revalidate = ll_revalidate_dentry,
326 .d_release = ll_release,
327 .d_delete = ll_ddelete,
328 .d_compare = ll_dcompare,