blob: 0e45d8fc4d7c84f14772893e9408b0a10db85bb0 [file] [log] [blame]
Peng Taod7e09d02013-05-02 16:46:55 +08001/*
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
Oleg Drokin6a5b99a2016-06-14 23:33:40 -040018 * http://www.gnu.org/licenses/gpl-2.0.html
Peng Taod7e09d02013-05-02 16:46:55 +080019 *
Peng Taod7e09d02013-05-02 16:46:55 +080020 * 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 *
Andreas Dilger1dc563a2015-11-08 18:09:37 -050026 * Copyright (c) 2011, 2015, Intel Corporation.
Peng Taod7e09d02013-05-02 16:46:55 +080027 */
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
37#define DEBUG_SUBSYSTEM S_LLITE
38
Greg Kroah-Hartman67a235f2014-07-11 21:51:41 -070039#include "../include/obd_support.h"
Greg Kroah-Hartman67a235f2014-07-11 21:51:41 -070040#include "../include/lustre/lustre_idl.h"
41#include "../include/lustre_dlm.h"
Peng Taod7e09d02013-05-02 16:46:55 +080042
43#include "llite_internal.h"
44
45static void free_dentry_data(struct rcu_head *head)
46{
47 struct ll_dentry_data *lld;
48
49 lld = container_of(head, struct ll_dentry_data, lld_rcu_head);
Julia Lawall97903a22015-04-12 22:55:02 +020050 kfree(lld);
Peng Taod7e09d02013-05-02 16:46:55 +080051}
52
53/* should NOT be called with the dcache lock, see fs/dcache.c */
54static void ll_release(struct dentry *de)
55{
56 struct ll_dentry_data *lld;
Greg Kroah-Hartman29aaf492013-08-02 18:14:51 +080057
Oleg Drokin6e168182016-02-16 00:46:46 -050058 LASSERT(de);
Peng Taod7e09d02013-05-02 16:46:55 +080059 lld = ll_d2d(de);
Bhumika Goyal61382aa2016-01-23 18:41:53 +053060 if (!lld) /* NFS copies the de->d_op methods (bug 4655) */
Greg Kroah-Hartmane05e02e2013-08-02 18:20:34 +080061 return;
Peng Taod7e09d02013-05-02 16:46:55 +080062
63 if (lld->lld_it) {
64 ll_intent_release(lld->lld_it);
Julia Lawall97903a22015-04-12 22:55:02 +020065 kfree(lld->lld_it);
Peng Taod7e09d02013-05-02 16:46:55 +080066 }
John L. Hammond2d95f102014-04-27 13:07:05 -040067
Peng Taod7e09d02013-05-02 16:46:55 +080068 de->d_fsdata = NULL;
69 call_rcu(&lld->lld_rcu_head, free_dentry_data);
Peng Taod7e09d02013-05-02 16:46:55 +080070}
71
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.
74 *
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
Oleg Drokinc0894c62016-02-24 22:00:30 -050078 * lock_match() fails) and we spin around real_lookup().
79 */
Al Viro6fa67e72016-07-31 16:37:25 -040080static int ll_dcompare(const struct dentry *dentry,
John L. Hammond2d95f102014-04-27 13:07:05 -040081 unsigned int len, const char *str,
82 const struct qstr *name)
Peng Taod7e09d02013-05-02 16:46:55 +080083{
Peng Taod7e09d02013-05-02 16:46:55 +080084 if (len != name->len)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +080085 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +080086
87 if (memcmp(str, name->name, len))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +080088 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +080089
90 CDEBUG(D_DENTRY, "found name %.*s(%p) flags %#x refc %d\n",
91 name->len, name->name, dentry, dentry->d_flags,
Al Viro193deee2013-07-05 19:06:16 +040092 d_count(dentry));
Peng Taod7e09d02013-05-02 16:46:55 +080093
94 /* mountpoint is always valid */
95 if (d_mountpoint((struct dentry *)dentry))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +080096 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +080097
98 if (d_lustre_invalid(dentry))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +080099 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +0800100
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800101 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800102}
103
Peng Taod7e09d02013-05-02 16:46:55 +0800104/**
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
110 */
111static int ll_ddelete(const struct dentry *de)
112{
Peng Taod7e09d02013-05-02 16:46:55 +0800113 LASSERT(de);
114
Al Viro09561a52014-05-07 21:10:24 -0400115 CDEBUG(D_DENTRY, "%s dentry %pd (%p, parent %p, inode %p) %s%s\n",
Peng Taod7e09d02013-05-02 16:46:55 +0800116 d_lustre_invalid((struct dentry *)de) ? "deleting" : "keeping",
David Howells2b0143b2015-03-17 22:25:59 +0000117 de, de, de->d_parent, d_inode(de),
Al Viro09561a52014-05-07 21:10:24 -0400118 d_unhashed(de) ? "" : "hashed,",
Peng Taod7e09d02013-05-02 16:46:55 +0800119 list_empty(&de->d_subdirs) ? "" : "subdirs");
120
121 /* kernel >= 2.6.38 last refcount is decreased after this function. */
Al Viro193deee2013-07-05 19:06:16 +0400122 LASSERT(d_count(de) == 1);
Peng Taod7e09d02013-05-02 16:46:55 +0800123
Peng Taod7e09d02013-05-02 16:46:55 +0800124 if (d_lustre_invalid((struct dentry *)de))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800125 return 1;
126 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800127}
128
Lai Siyao3ea8f3b2014-01-22 21:36:12 +0800129int ll_d_init(struct dentry *de)
Peng Taod7e09d02013-05-02 16:46:55 +0800130{
Al Viro09561a52014-05-07 21:10:24 -0400131 CDEBUG(D_DENTRY, "ldd on dentry %pd (%p) parent %p inode %p refc %d\n",
Oleg Drokine15ba452016-02-26 01:49:49 -0500132 de, de, de->d_parent, d_inode(de), d_count(de));
Peng Taod7e09d02013-05-02 16:46:55 +0800133
Bhumika Goyal61382aa2016-01-23 18:41:53 +0530134 if (!de->d_fsdata) {
Peng Taod7e09d02013-05-02 16:46:55 +0800135 struct ll_dentry_data *lld;
136
Julia Lawall496a51b2014-09-18 22:24:02 +0200137 lld = kzalloc(sizeof(*lld), GFP_NOFS);
138 if (likely(lld)) {
Peng Taod7e09d02013-05-02 16:46:55 +0800139 spin_lock(&de->d_lock);
Bhumika Goyal61382aa2016-01-23 18:41:53 +0530140 if (likely(!de->d_fsdata)) {
Peng Taod7e09d02013-05-02 16:46:55 +0800141 de->d_fsdata = lld;
Lai Siyao3ea8f3b2014-01-22 21:36:12 +0800142 __d_lustre_invalidate(de);
143 } else {
Julia Lawall97903a22015-04-12 22:55:02 +0200144 kfree(lld);
Lai Siyao3ea8f3b2014-01-22 21:36:12 +0800145 }
Peng Taod7e09d02013-05-02 16:46:55 +0800146 spin_unlock(&de->d_lock);
147 } else {
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800148 return -ENOMEM;
Peng Taod7e09d02013-05-02 16:46:55 +0800149 }
150 }
Lai Siyao3ea8f3b2014-01-22 21:36:12 +0800151 LASSERT(de->d_op == &ll_d_ops);
Peng Taod7e09d02013-05-02 16:46:55 +0800152
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800153 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800154}
155
Peng Taod7e09d02013-05-02 16:46:55 +0800156void ll_intent_drop_lock(struct lookup_intent *it)
157{
John L. Hammonde476f2e2016-06-20 16:55:38 -0400158 if (it->it_op && it->it_lock_mode) {
Peng Taod7e09d02013-05-02 16:46:55 +0800159 struct lustre_handle handle;
160
John L. Hammonde476f2e2016-06-20 16:55:38 -0400161 handle.cookie = it->it_lock_handle;
Peng Taod7e09d02013-05-02 16:46:55 +0800162
Greg Kroah-Hartman55f5a822014-07-12 20:26:07 -0700163 CDEBUG(D_DLMTRACE, "releasing lock with cookie %#llx from it %p\n",
164 handle.cookie, it);
John L. Hammonde476f2e2016-06-20 16:55:38 -0400165 ldlm_lock_decref(&handle, it->it_lock_mode);
Peng Taod7e09d02013-05-02 16:46:55 +0800166
167 /* bug 494: intent_release may be called multiple times, from
Oleg Drokinc0894c62016-02-24 22:00:30 -0500168 * this thread and we don't want to double-decref this lock
169 */
John L. Hammonde476f2e2016-06-20 16:55:38 -0400170 it->it_lock_mode = 0;
171 if (it->it_remote_lock_mode != 0) {
172 handle.cookie = it->it_remote_lock_handle;
Peng Taod7e09d02013-05-02 16:46:55 +0800173
Greg Kroah-Hartman55f5a822014-07-12 20:26:07 -0700174 CDEBUG(D_DLMTRACE, "releasing remote lock with cookie%#llx from it %p\n",
175 handle.cookie, it);
Peng Taod7e09d02013-05-02 16:46:55 +0800176 ldlm_lock_decref(&handle,
John L. Hammonde476f2e2016-06-20 16:55:38 -0400177 it->it_remote_lock_mode);
178 it->it_remote_lock_mode = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800179 }
180 }
181}
182
183void ll_intent_release(struct lookup_intent *it)
184{
Peng Taod7e09d02013-05-02 16:46:55 +0800185 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))
John L. Hammond8bf86fd2016-06-20 16:55:40 -0400189 ptlrpc_req_finished(it->it_request); /* ll_file_open */
John L. Hammond2d95f102014-04-27 13:07:05 -0400190
Peng Taod7e09d02013-05-02 16:46:55 +0800191 if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
John L. Hammond8bf86fd2016-06-20 16:55:40 -0400192 ptlrpc_req_finished(it->it_request);
Peng Taod7e09d02013-05-02 16:46:55 +0800193
John L. Hammonde476f2e2016-06-20 16:55:38 -0400194 it->it_disposition = 0;
John L. Hammond8bf86fd2016-06-20 16:55:40 -0400195 it->it_request = NULL;
Peng Taod7e09d02013-05-02 16:46:55 +0800196}
197
198void ll_invalidate_aliases(struct inode *inode)
199{
200 struct dentry *dentry;
Peng Taod7e09d02013-05-02 16:46:55 +0800201
James Nunez97a075c2016-04-27 18:21:01 -0400202 CDEBUG(D_INODE, "marking dentries for ino "DFID"(%p) invalid\n",
203 PFID(ll_inode2fid(inode)), inode);
Peng Taod7e09d02013-05-02 16:46:55 +0800204
Oleg Drokin2bbec0e2016-06-20 16:55:26 -0400205 spin_lock(&inode->i_lock);
Oleg Drokin9d5be522015-07-31 19:42:57 -0400206 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
Linus Torvaldsdab363f2014-12-15 18:06:13 -0800207 CDEBUG(D_DENTRY, "dentry in drop %pd (%p) parent %p inode %p flags %d\n",
208 dentry, dentry, dentry->d_parent,
David Howells2b0143b2015-03-17 22:25:59 +0000209 d_inode(dentry), dentry->d_flags);
Peng Taod7e09d02013-05-02 16:46:55 +0800210
Peng Taob1d2a122013-06-03 21:58:15 +0800211 d_lustre_invalidate(dentry, 0);
Peng Taod7e09d02013-05-02 16:46:55 +0800212 }
Oleg Drokin2bbec0e2016-06-20 16:55:26 -0400213 spin_unlock(&inode->i_lock);
Peng Taod7e09d02013-05-02 16:46:55 +0800214}
215
216int ll_revalidate_it_finish(struct ptlrpc_request *request,
217 struct lookup_intent *it,
Al Virodbca51d2015-01-18 23:31:19 -0500218 struct inode *inode)
Peng Taod7e09d02013-05-02 16:46:55 +0800219{
220 int rc = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800221
222 if (!request)
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800223 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800224
225 if (it_disposition(it, DISP_LOOKUP_NEG))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800226 return -ENOENT;
Peng Taod7e09d02013-05-02 16:46:55 +0800227
Al Virodbca51d2015-01-18 23:31:19 -0500228 rc = ll_prep_inode(&inode, request, NULL, it);
Peng Taod7e09d02013-05-02 16:46:55 +0800229
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800230 return rc;
Peng Taod7e09d02013-05-02 16:46:55 +0800231}
232
Al Virodbca51d2015-01-18 23:31:19 -0500233void ll_lookup_finish_locks(struct lookup_intent *it, struct inode *inode)
Peng Taod7e09d02013-05-02 16:46:55 +0800234{
John L. Hammonde476f2e2016-06-20 16:55:38 -0400235 if (it->it_lock_mode && inode) {
Al Virodbca51d2015-01-18 23:31:19 -0500236 struct ll_sb_info *sbi = ll_i2sbi(inode);
Peng Taod7e09d02013-05-02 16:46:55 +0800237
James Nunez97a075c2016-04-27 18:21:01 -0400238 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)\n",
239 PFID(ll_inode2fid(inode)), inode);
Peng Taod7e09d02013-05-02 16:46:55 +0800240 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
241 }
242
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.
Oleg Drokinc0894c62016-02-24 22:00:30 -0500247 * therefore, we don't release intent here -bzzz
248 */
Peng Taod7e09d02013-05-02 16:46:55 +0800249 ll_intent_drop_lock(it);
250 }
251}
252
Lai Siyaof236f692014-02-28 21:16:38 -0500253static int ll_revalidate_dentry(struct dentry *dentry,
254 unsigned int lookup_flags)
Peng Taod7e09d02013-05-02 16:46:55 +0800255{
David Howells2b0143b2015-03-17 22:25:59 +0000256 struct inode *dir = d_inode(dentry->d_parent);
Peng Taod7e09d02013-05-02 16:46:55 +0800257
Oleg Drokinaae5d552016-06-20 16:55:46 -0400258 /* 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.
261 */
262 if (lookup_flags & LOOKUP_PARENT)
263 return 1;
264
265 /* Symlink - always valid as long as the dentry was found */
266 if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode))
267 return 1;
268
Lai Siyaof236f692014-02-28 21:16:38 -0500269 /*
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.
273 */
274 if ((lookup_flags & (LOOKUP_OPEN | LOOKUP_CREATE)) ==
275 (LOOKUP_OPEN | LOOKUP_CREATE))
276 return 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800277
Lai Siyaof236f692014-02-28 21:16:38 -0500278 if (lookup_flags & (LOOKUP_PARENT | LOOKUP_OPEN | LOOKUP_CREATE))
Greg Kroah-Hartman0a3bdb02013-08-03 10:35:28 +0800279 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +0800280
Lai Siyao5231f762016-09-18 16:38:09 -0400281 if (!dentry_may_statahead(dir, dentry))
Lai Siyaof236f692014-02-28 21:16:38 -0500282 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +0800283
Lai Siyaof236f692014-02-28 21:16:38 -0500284 if (lookup_flags & LOOKUP_RCU)
285 return -ECHILD;
Peng Taod7e09d02013-05-02 16:46:55 +0800286
Lai Siyao5231f762016-09-18 16:38:09 -0400287 ll_statahead(dir, &dentry, !d_inode(dentry));
Lai Siyaof236f692014-02-28 21:16:38 -0500288 return 1;
Peng Taod7e09d02013-05-02 16:46:55 +0800289}
290
291/*
292 * Always trust cached dentries. Update statahead window if necessary.
293 */
John L. Hammond2d95f102014-04-27 13:07:05 -0400294static int ll_revalidate_nd(struct dentry *dentry, unsigned int flags)
Peng Taod7e09d02013-05-02 16:46:55 +0800295{
Al Viro09561a52014-05-07 21:10:24 -0400296 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, flags=%u\n",
297 dentry, flags);
Peng Taod7e09d02013-05-02 16:46:55 +0800298
Aya Mahfouz5a091a12015-02-27 15:07:58 +0200299 return ll_revalidate_dentry(dentry, flags);
Peng Taod7e09d02013-05-02 16:46:55 +0800300}
301
John L. Hammond2d95f102014-04-27 13:07:05 -0400302const struct dentry_operations ll_d_ops = {
Peng Taod7e09d02013-05-02 16:46:55 +0800303 .d_revalidate = ll_revalidate_nd,
304 .d_release = ll_release,
305 .d_delete = ll_ddelete,
Peng Taod7e09d02013-05-02 16:46:55 +0800306 .d_compare = ll_dcompare,
307};