staging/lustre/llite: Get rid of ll_lock_dcache/ll_unlock_dcache
[deliverable/linux.git] / drivers / staging / lustre / lustre / llite / dcache.c
CommitLineData
d7e09d03
PT
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
6a5b99a4 18 * http://www.gnu.org/licenses/gpl-2.0.html
d7e09d03 19 *
d7e09d03
PT
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 *
1dc563a6 26 * Copyright (c) 2011, 2015, Intel Corporation.
d7e09d03
PT
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
37#define DEBUG_SUBSYSTEM S_LLITE
38
67a235f5
GKH
39#include "../include/obd_support.h"
40#include "../include/lustre_lite.h"
41#include "../include/lustre/lustre_idl.h"
42#include "../include/lustre_dlm.h"
d7e09d03
PT
43
44#include "llite_internal.h"
45
46static 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);
97903a26 51 kfree(lld);
d7e09d03
PT
52}
53
54/* should NOT be called with the dcache lock, see fs/dcache.c */
55static void ll_release(struct dentry *de)
56{
57 struct ll_dentry_data *lld;
29aaf496 58
6e16818b 59 LASSERT(de);
d7e09d03 60 lld = ll_d2d(de);
61382aa8 61 if (!lld) /* NFS copies the de->d_op methods (bug 4655) */
e05e02e4 62 return;
d7e09d03
PT
63
64 if (lld->lld_it) {
65 ll_intent_release(lld->lld_it);
97903a26 66 kfree(lld->lld_it);
d7e09d03 67 }
2d95f10e 68
d7e09d03
PT
69 de->d_fsdata = NULL;
70 call_rcu(&lld->lld_rcu_head, free_dentry_data);
d7e09d03
PT
71}
72
73/* Compare if two dentries are the same. Don't match if the existing dentry
74 * is marked invalid. Returns 1 if different, 0 if the same.
75 *
76 * This avoids a race where ll_lookup_it() instantiates a dentry, but we get
77 * an AST before calling d_revalidate_it(). The dentry still exists (marked
78 * INVALID) so d_lookup() matches it, but we have no lock on it (so
c0894c6c
OD
79 * lock_match() fails) and we spin around real_lookup().
80 */
2d95f10e
JH
81static int ll_dcompare(const struct dentry *parent, const struct dentry *dentry,
82 unsigned int len, const char *str,
83 const struct qstr *name)
d7e09d03 84{
d7e09d03 85 if (len != name->len)
0a3bdb00 86 return 1;
d7e09d03
PT
87
88 if (memcmp(str, name->name, len))
0a3bdb00 89 return 1;
d7e09d03
PT
90
91 CDEBUG(D_DENTRY, "found name %.*s(%p) flags %#x refc %d\n",
92 name->len, name->name, dentry, dentry->d_flags,
193deee1 93 d_count(dentry));
d7e09d03
PT
94
95 /* mountpoint is always valid */
96 if (d_mountpoint((struct dentry *)dentry))
0a3bdb00 97 return 0;
d7e09d03
PT
98
99 if (d_lustre_invalid(dentry))
0a3bdb00 100 return 1;
d7e09d03 101
0a3bdb00 102 return 0;
d7e09d03
PT
103}
104
105static inline int return_if_equal(struct ldlm_lock *lock, void *data)
106{
5a9a80ba
BK
107 return (ldlm_is_canceling(lock) && ldlm_is_discard_data(lock)) ?
108 LDLM_ITER_CONTINUE : LDLM_ITER_STOP;
d7e09d03
PT
109}
110
111/* find any ldlm lock of the inode in mdc and lov
112 * return 0 not find
113 * 1 find one
c0894c6c
OD
114 * < 0 error
115 */
d7e09d03
PT
116static int find_cbdata(struct inode *inode)
117{
118 struct ll_sb_info *sbi = ll_i2sbi(inode);
119 struct lov_stripe_md *lsm;
120 int rc = 0;
d7e09d03
PT
121
122 LASSERT(inode);
123 rc = md_find_cbdata(sbi->ll_md_exp, ll_inode2fid(inode),
124 return_if_equal, NULL);
125 if (rc != 0)
717842fe 126 return rc;
d7e09d03
PT
127
128 lsm = ccc_inode_lsm_get(inode);
61382aa8 129 if (!lsm)
0a3bdb00 130 return rc;
d7e09d03
PT
131
132 rc = obd_find_cbdata(sbi->ll_dt_exp, lsm, return_if_equal, NULL);
133 ccc_inode_lsm_put(inode, lsm);
134
0a3bdb00 135 return rc;
d7e09d03
PT
136}
137
138/**
139 * Called when last reference to a dentry is dropped and dcache wants to know
140 * whether or not it should cache it:
141 * - return 1 to delete the dentry immediately
142 * - return 0 to cache the dentry
143 * Should NOT be called with the dcache lock, see fs/dcache.c
144 */
145static int ll_ddelete(const struct dentry *de)
146{
d7e09d03
PT
147 LASSERT(de);
148
09561a53 149 CDEBUG(D_DENTRY, "%s dentry %pd (%p, parent %p, inode %p) %s%s\n",
d7e09d03 150 d_lustre_invalid((struct dentry *)de) ? "deleting" : "keeping",
2b0143b5 151 de, de, de->d_parent, d_inode(de),
09561a53 152 d_unhashed(de) ? "" : "hashed,",
d7e09d03
PT
153 list_empty(&de->d_subdirs) ? "" : "subdirs");
154
155 /* kernel >= 2.6.38 last refcount is decreased after this function. */
193deee1 156 LASSERT(d_count(de) == 1);
d7e09d03 157
d0a0acc3 158 /* Disable this piece of code temporarily because this is called
d7e09d03
PT
159 * inside dcache_lock so it's not appropriate to do lots of work
160 * here. ATTENTION: Before this piece of code enabling, LU-2487 must be
c0894c6c
OD
161 * resolved.
162 */
d7e09d03
PT
163#if 0
164 /* if not ldlm lock for this inode, set i_nlink to 0 so that
c0894c6c
OD
165 * this inode can be recycled later b=20433
166 */
2b0143b5
DH
167 if (d_really_is_positive(de) && !find_cbdata(d_inode(de)))
168 clear_nlink(d_inode(de));
d7e09d03
PT
169#endif
170
171 if (d_lustre_invalid((struct dentry *)de))
0a3bdb00
GKH
172 return 1;
173 return 0;
d7e09d03
PT
174}
175
3ea8f3bc 176int ll_d_init(struct dentry *de)
d7e09d03 177{
09561a53 178 CDEBUG(D_DENTRY, "ldd on dentry %pd (%p) parent %p inode %p refc %d\n",
e15ba45d 179 de, de, de->d_parent, d_inode(de), d_count(de));
d7e09d03 180
61382aa8 181 if (!de->d_fsdata) {
d7e09d03
PT
182 struct ll_dentry_data *lld;
183
496a51bd
JL
184 lld = kzalloc(sizeof(*lld), GFP_NOFS);
185 if (likely(lld)) {
d7e09d03 186 spin_lock(&de->d_lock);
61382aa8 187 if (likely(!de->d_fsdata)) {
d7e09d03 188 de->d_fsdata = lld;
3ea8f3bc
LS
189 __d_lustre_invalidate(de);
190 } else {
97903a26 191 kfree(lld);
3ea8f3bc 192 }
d7e09d03
PT
193 spin_unlock(&de->d_lock);
194 } else {
0a3bdb00 195 return -ENOMEM;
d7e09d03
PT
196 }
197 }
3ea8f3bc 198 LASSERT(de->d_op == &ll_d_ops);
d7e09d03 199
0a3bdb00 200 return 0;
d7e09d03
PT
201}
202
d7e09d03
PT
203void ll_intent_drop_lock(struct lookup_intent *it)
204{
205 if (it->it_op && it->d.lustre.it_lock_mode) {
206 struct lustre_handle handle;
207
208 handle.cookie = it->d.lustre.it_lock_handle;
209
55f5a824
GKH
210 CDEBUG(D_DLMTRACE, "releasing lock with cookie %#llx from it %p\n",
211 handle.cookie, it);
d7e09d03
PT
212 ldlm_lock_decref(&handle, it->d.lustre.it_lock_mode);
213
214 /* bug 494: intent_release may be called multiple times, from
c0894c6c
OD
215 * this thread and we don't want to double-decref this lock
216 */
d7e09d03
PT
217 it->d.lustre.it_lock_mode = 0;
218 if (it->d.lustre.it_remote_lock_mode != 0) {
219 handle.cookie = it->d.lustre.it_remote_lock_handle;
220
55f5a824
GKH
221 CDEBUG(D_DLMTRACE, "releasing remote lock with cookie%#llx from it %p\n",
222 handle.cookie, it);
d7e09d03
PT
223 ldlm_lock_decref(&handle,
224 it->d.lustre.it_remote_lock_mode);
225 it->d.lustre.it_remote_lock_mode = 0;
226 }
227 }
228}
229
230void ll_intent_release(struct lookup_intent *it)
231{
d7e09d03
PT
232 CDEBUG(D_INFO, "intent %p released\n", it);
233 ll_intent_drop_lock(it);
234 /* We are still holding extra reference on a request, need to free it */
235 if (it_disposition(it, DISP_ENQ_OPEN_REF))
2d95f10e
JH
236 ptlrpc_req_finished(it->d.lustre.it_data); /* ll_file_open */
237
d7e09d03
PT
238 if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
239 ptlrpc_req_finished(it->d.lustre.it_data);
d7e09d03
PT
240
241 it->d.lustre.it_disposition = 0;
242 it->d.lustre.it_data = NULL;
d7e09d03
PT
243}
244
245void ll_invalidate_aliases(struct inode *inode)
246{
247 struct dentry *dentry;
d7e09d03 248
97a075cd
JN
249 CDEBUG(D_INODE, "marking dentries for ino "DFID"(%p) invalid\n",
250 PFID(ll_inode2fid(inode)), inode);
d7e09d03 251
2bbec0ed 252 spin_lock(&inode->i_lock);
9d5be52f 253 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
dab363f9
LT
254 CDEBUG(D_DENTRY, "dentry in drop %pd (%p) parent %p inode %p flags %d\n",
255 dentry, dentry, dentry->d_parent,
2b0143b5 256 d_inode(dentry), dentry->d_flags);
d7e09d03 257
b1d2a127 258 d_lustre_invalidate(dentry, 0);
d7e09d03 259 }
2bbec0ed 260 spin_unlock(&inode->i_lock);
d7e09d03
PT
261}
262
263int ll_revalidate_it_finish(struct ptlrpc_request *request,
264 struct lookup_intent *it,
dbca51dd 265 struct inode *inode)
d7e09d03
PT
266{
267 int rc = 0;
d7e09d03
PT
268
269 if (!request)
0a3bdb00 270 return 0;
d7e09d03
PT
271
272 if (it_disposition(it, DISP_LOOKUP_NEG))
0a3bdb00 273 return -ENOENT;
d7e09d03 274
dbca51dd 275 rc = ll_prep_inode(&inode, request, NULL, it);
d7e09d03 276
0a3bdb00 277 return rc;
d7e09d03
PT
278}
279
dbca51dd 280void ll_lookup_finish_locks(struct lookup_intent *it, struct inode *inode)
d7e09d03 281{
6e16818b 282 if (it->d.lustre.it_lock_mode && inode) {
dbca51dd 283 struct ll_sb_info *sbi = ll_i2sbi(inode);
d7e09d03 284
97a075cd
JN
285 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)\n",
286 PFID(ll_inode2fid(inode)), inode);
d7e09d03
PT
287 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
288 }
289
290 /* drop lookup or getattr locks immediately */
291 if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
292 /* on 2.6 there are situation when several lookups and
293 * revalidations may be requested during single operation.
c0894c6c
OD
294 * therefore, we don't release intent here -bzzz
295 */
d7e09d03
PT
296 ll_intent_drop_lock(it);
297 }
298}
299
f236f69b
LS
300static int ll_revalidate_dentry(struct dentry *dentry,
301 unsigned int lookup_flags)
d7e09d03 302{
2b0143b5 303 struct inode *dir = d_inode(dentry->d_parent);
d7e09d03 304
f236f69b
LS
305 /*
306 * if open&create is set, talk to MDS to make sure file is created if
307 * necessary, because we can't do this in ->open() later since that's
308 * called on an inode. return 0 here to let lookup to handle this.
309 */
310 if ((lookup_flags & (LOOKUP_OPEN | LOOKUP_CREATE)) ==
311 (LOOKUP_OPEN | LOOKUP_CREATE))
312 return 0;
d7e09d03 313
f236f69b 314 if (lookup_flags & (LOOKUP_PARENT | LOOKUP_OPEN | LOOKUP_CREATE))
0a3bdb00 315 return 1;
d7e09d03 316
f236f69b
LS
317 if (d_need_statahead(dir, dentry) <= 0)
318 return 1;
d7e09d03 319
f236f69b
LS
320 if (lookup_flags & LOOKUP_RCU)
321 return -ECHILD;
d7e09d03 322
61382aa8 323 do_statahead_enter(dir, &dentry, !d_inode(dentry));
f236f69b
LS
324 ll_statahead_mark(dir, dentry);
325 return 1;
d7e09d03
PT
326}
327
328/*
329 * Always trust cached dentries. Update statahead window if necessary.
330 */
2d95f10e 331static int ll_revalidate_nd(struct dentry *dentry, unsigned int flags)
d7e09d03 332{
09561a53
AV
333 CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, flags=%u\n",
334 dentry, flags);
d7e09d03 335
5a091a1f 336 return ll_revalidate_dentry(dentry, flags);
d7e09d03
PT
337}
338
2d95f10e 339static void ll_d_iput(struct dentry *de, struct inode *inode)
d7e09d03
PT
340{
341 LASSERT(inode);
342 if (!find_cbdata(inode))
343 clear_nlink(inode);
344 iput(inode);
345}
346
2d95f10e 347const struct dentry_operations ll_d_ops = {
d7e09d03
PT
348 .d_revalidate = ll_revalidate_nd,
349 .d_release = ll_release,
350 .d_delete = ll_ddelete,
351 .d_iput = ll_d_iput,
352 .d_compare = ll_dcompare,
353};
This page took 0.524045 seconds and 5 git commands to generate.