Whamcloud - gitweb
LU-6142 tests: Fix style issues for chownmany.c
[fs/lustre-release.git] / lustre / llite / dcache.c
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
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
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  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
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 #include <linux/kernel.h>
37
38 #define DEBUG_SUBSYSTEM S_LLITE
39
40 #include <obd_support.h>
41 #include <lustre_dlm.h>
42
43 #include "llite_internal.h"
44
45 static 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);
50         OBD_FREE_PTR(lld);
51 }
52
53 /* should NOT be called with the dcache lock, see fs/dcache.c */
54 static void ll_release(struct dentry *de)
55 {
56         struct ll_dentry_data *lld;
57         ENTRY;
58         LASSERT(de != NULL);
59         lld = ll_d2d(de);
60         if (lld == NULL) /* NFS copies the de->d_op methods (bug 4655) */
61                 RETURN_EXIT;
62
63         if (lld->lld_it) {
64                 ll_intent_release(lld->lld_it);
65                 OBD_FREE(lld->lld_it, sizeof(*lld->lld_it));
66         }
67
68         de->d_fsdata = NULL;
69         call_rcu(&lld->lld_rcu_head, free_dentry_data);
70
71         EXIT;
72 }
73
74 /* Compare if two dentries are the same.  Don't match if the existing dentry
75  * is marked invalid.  Returns 1 if different, 0 if the same.
76  *
77  * This avoids a race where ll_lookup_it() instantiates a dentry, but we get
78  * an AST before calling d_revalidate_it().  The dentry still exists (marked
79  * INVALID) so d_lookup() matches it, but we have no lock on it (so
80  * lock_match() fails) and we spin around real_lookup().
81  *
82  * This race doesn't apply to lookups in d_alloc_parallel(), and for
83  * those we want to ensure that only one dentry with a given name is
84  * in ll_lookup_nd() at a time.  So allow invalid dentries to match
85  * while d_in_lookup().  We will be called again when the lookup
86  * completes, and can give a different answer then.
87  */
88 #if defined(HAVE_D_COMPARE_5ARGS)
89 static int ll_dcompare(const struct dentry *parent, const struct dentry *dentry,
90                        unsigned int len, const char *str,
91                        const struct qstr *name)
92 #elif defined(HAVE_D_COMPARE_4ARGS)
93 static int ll_dcompare(const struct dentry *dentry, unsigned int len,
94                        const char *str, const struct qstr *name)
95 #endif
96 {
97         ENTRY;
98
99         if (len != name->len)
100                 RETURN(1);
101
102         if (memcmp(str, name->name, len))
103                 RETURN(1);
104
105         CDEBUG(D_DENTRY, "found name %.*s(%p) flags %#x refc %d\n",
106                name->len, name->name, dentry, dentry->d_flags,
107                ll_d_count(dentry));
108
109         /* mountpoint is always valid */
110         if (d_mountpoint((struct dentry *)dentry))
111                 RETURN(0);
112
113         /* ensure exclusion against parallel lookup of the same name */
114         if (d_in_lookup((struct dentry *)dentry))
115                 return 0;
116
117         if (d_lustre_invalid(dentry))
118                 RETURN(1);
119
120         RETURN(0);
121 }
122
123 /**
124  * Called when last reference to a dentry is dropped and dcache wants to know
125  * whether or not it should cache it:
126  * - return 1 to delete the dentry immediately
127  * - return 0 to cache the dentry
128  * Should NOT be called with the dcache lock, see fs/dcache.c
129  */
130 static int ll_ddelete(const struct dentry *de)
131 {
132         ENTRY;
133         LASSERT(de);
134
135         CDEBUG(D_DENTRY, "%s dentry %.*s (%p, parent %p, inode %p) %s%s\n",
136                d_lustre_invalid((struct dentry *)de) ? "deleting" : "keeping",
137                de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
138                d_unhashed((struct dentry *)de) ? "" : "hashed,",
139                list_empty(&de->d_subdirs) ? "" : "subdirs");
140
141         /* kernel >= 2.6.38 last refcount is decreased after this function. */
142         LASSERT(ll_d_count(de) == 1);
143
144         if (d_lustre_invalid((struct dentry *)de))
145                 RETURN(1);
146         RETURN(0);
147 }
148
149 int ll_d_init(struct dentry *de)
150 {
151         ENTRY;
152         LASSERT(de != NULL);
153
154         CDEBUG(D_DENTRY, "ldd on dentry %.*s (%p) parent %p inode %p refc %d\n",
155                 de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
156                 ll_d_count(de));
157
158         if (de->d_fsdata == NULL) {
159                 struct ll_dentry_data *lld;
160
161                 OBD_ALLOC_PTR(lld);
162                 if (likely(lld != NULL)) {
163                         spin_lock(&de->d_lock);
164                         if (likely(de->d_fsdata == NULL)) {
165                                 de->d_fsdata = lld;
166                                 __d_lustre_invalidate(de);
167                         } else {
168                                 OBD_FREE_PTR(lld);
169                         }
170                         spin_unlock(&de->d_lock);
171                 } else {
172                         RETURN(-ENOMEM);
173                 }
174         }
175         LASSERT(de->d_op == &ll_d_ops);
176
177         RETURN(0);
178 }
179
180 void ll_intent_drop_lock(struct lookup_intent *it)
181 {
182         if (it->it_op && it->it_lock_mode) {
183                 struct lustre_handle handle;
184
185                 handle.cookie = it->it_lock_handle;
186
187                 CDEBUG(D_DLMTRACE, "releasing lock with cookie %#llx from it %p\n",
188                        handle.cookie, it);
189                 ldlm_lock_decref(&handle, it->it_lock_mode);
190
191                 /* bug 494: intent_release may be called multiple times, from
192                  * this thread and we don't want to double-decref this lock */
193                 it->it_lock_mode = 0;
194                 if (it->it_remote_lock_mode != 0) {
195                         handle.cookie = it->it_remote_lock_handle;
196
197                         CDEBUG(D_DLMTRACE, "releasing remote lock with cookie"
198                                "%#llx from it %p\n", handle.cookie, it);
199                         ldlm_lock_decref(&handle,
200                                          it->it_remote_lock_mode);
201                         it->it_remote_lock_mode = 0;
202                 }
203         }
204 }
205
206 void ll_intent_release(struct lookup_intent *it)
207 {
208         ENTRY;
209
210         CDEBUG(D_INFO, "intent %p released\n", it);
211         ll_intent_drop_lock(it);
212         /* We are still holding extra reference on a request, need to free it */
213         if (it_disposition(it, DISP_ENQ_OPEN_REF))
214                 ptlrpc_req_finished(it->it_request); /* ll_file_open */
215
216         if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
217                 ptlrpc_req_finished(it->it_request);
218
219         it->it_disposition = 0;
220         it->it_request = NULL;
221         EXIT;
222 }
223
224 void ll_invalidate_aliases(struct inode *inode)
225 {
226         struct dentry *dentry;
227         DECLARE_LL_D_HLIST_NODE_PTR(p);
228         ENTRY;
229
230         LASSERT(inode != NULL);
231
232         CDEBUG(D_INODE, "marking dentries for inode "DFID"(%p) invalid\n",
233                PFID(ll_inode2fid(inode)), inode);
234
235         spin_lock(&inode->i_lock);
236         ll_d_hlist_for_each_entry(dentry, p, &inode->i_dentry) {
237                 CDEBUG(D_DENTRY, "dentry in drop %.*s (%p) parent %p "
238                        "inode %p flags %d\n", dentry->d_name.len,
239                        dentry->d_name.name, dentry, dentry->d_parent,
240                        dentry->d_inode, dentry->d_flags);
241
242                 d_lustre_invalidate(dentry, 0);
243         }
244         spin_unlock(&inode->i_lock);
245
246         EXIT;
247 }
248
249 int ll_revalidate_it_finish(struct ptlrpc_request *request,
250                             struct lookup_intent *it,
251                             struct dentry *de)
252 {
253         int rc = 0;
254         ENTRY;
255
256         if (!request)
257                 RETURN(0);
258
259         if (it_disposition(it, DISP_LOOKUP_NEG))
260                 RETURN(-ENOENT);
261
262         rc = ll_prep_inode(&de->d_inode, request, NULL, it);
263
264         RETURN(rc);
265 }
266
267 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
268 {
269         LASSERT(it != NULL);
270         LASSERT(dentry != NULL);
271
272         if (it->it_lock_mode && dentry->d_inode != NULL) {
273                 struct inode *inode = dentry->d_inode;
274                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
275
276                 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)\n",
277                        PFID(ll_inode2fid(inode)), inode);
278                 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
279         }
280
281         /* drop lookup or getattr locks immediately */
282         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
283                 /* on 2.6 there are situation when several lookups and
284                  * revalidations may be requested during single operation.
285                  * therefore, we don't release intent here -bzzz */
286                 ll_intent_drop_lock(it);
287         }
288 }
289
290 static int ll_revalidate_dentry(struct dentry *dentry,
291                                 unsigned int lookup_flags)
292 {
293         struct inode *dir = dentry->d_parent->d_inode;
294
295         /* If this is intermediate component path lookup and we were able to get
296          * to this dentry, then its lock has not been revoked and the
297          * path component is valid. */
298         if (lookup_flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))
299                 return 1;
300
301         /* Symlink - always valid as long as the dentry was found */
302 #ifdef HAVE_IOP_GET_LINK
303         if (dentry->d_inode && dentry->d_inode->i_op->get_link)
304 #else
305         if (dentry->d_inode && dentry->d_inode->i_op->follow_link)
306 #endif
307                 return 1;
308
309         /*
310          * VFS warns us that this is the second go around and previous
311          * operation failed (most likely open|creat), so this time
312          * we better talk to the server via the lookup path by name,
313          * not by fid.
314          */
315         if (lookup_flags & LOOKUP_REVAL)
316                 return 0;
317
318         if (lookup_flags & LOOKUP_RCU)
319                 return -ECHILD;
320
321         if (dentry_may_statahead(dir, dentry))
322                 ll_statahead(dir, &dentry, dentry->d_inode == NULL);
323
324         return 1;
325 }
326
327 /*
328  * Always trust cached dentries. Update statahead window if necessary.
329  */
330 #ifdef HAVE_IOP_ATOMIC_OPEN
331 static int ll_revalidate_nd(struct dentry *dentry, unsigned int flags)
332 {
333         int rc;
334         ENTRY;
335
336         CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
337                dentry->d_name.name, flags);
338
339         rc = ll_revalidate_dentry(dentry, flags);
340         RETURN(rc);
341 }
342 #else
343 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
344 {
345         int rc;
346         ENTRY;
347
348         /*
349          * this is normally called from NFS export, and we don't know whether
350          * this is the last component.
351          */
352         if (nd == NULL)
353                 RETURN(1);
354
355         CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
356                dentry->d_name.name, nd->flags);
357
358         rc = ll_revalidate_dentry(dentry, nd->flags);
359         RETURN(rc);
360 }
361 #endif
362
363 const struct dentry_operations ll_d_ops = {
364         .d_revalidate = ll_revalidate_nd,
365         .d_release = ll_release,
366         .d_delete  = ll_ddelete,
367         .d_compare = ll_dcompare,
368 };