Whamcloud - gitweb
LU-17705 ptlrpc: replace synchronize_rcu() with rcu_barrier()
[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  */
31
32 #include <linux/fs.h>
33 #include <linux/sched.h>
34 #include <linux/quotaops.h>
35 #include <linux/kernel.h>
36
37 #define DEBUG_SUBSYSTEM S_LLITE
38
39 #include <obd_support.h>
40 #include <lustre_dlm.h>
41
42 #include "llite_internal.h"
43
44 static void free_dentry_data(struct rcu_head *head)
45 {
46         struct ll_dentry_data *lld;
47
48         lld = container_of(head, struct ll_dentry_data, lld_rcu_head);
49         OBD_FREE_PTR(lld);
50 }
51
52 /* should NOT be called with the dcache lock, see fs/dcache.c */
53 static void ll_release(struct dentry *de)
54 {
55         struct ll_dentry_data *lld;
56
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         de->d_fsdata = NULL;
64         call_rcu(&lld->lld_rcu_head, free_dentry_data);
65
66         EXIT;
67 }
68
69 /* Compare if two dentries are the same.  Don't match if the existing dentry
70  * is marked invalid.  Returns 1 if different, 0 if the same.
71  *
72  * This avoids a race where ll_lookup_it() instantiates a dentry, but we get
73  * an AST before calling d_revalidate_it().  The dentry still exists (marked
74  * INVALID) so d_lookup() matches it, but we have no lock on it (so
75  * lock_match() fails) and we spin around real_lookup().
76  *
77  * This race doesn't apply to lookups in d_alloc_parallel(), and for
78  * those we want to ensure that only one dentry with a given name is
79  * in ll_lookup_nd() at a time.  So allow invalid dentries to match
80  * while d_in_lookup().  We will be called again when the lookup
81  * completes, and can give a different answer then.
82  */
83 #if defined(HAVE_D_COMPARE_5ARGS)
84 static int ll_dcompare(const struct dentry *parent, const struct dentry *dentry,
85                        unsigned int len, const char *str,
86                        const struct qstr *name)
87 #elif defined(HAVE_D_COMPARE_4ARGS)
88 static int ll_dcompare(const struct dentry *dentry, unsigned int len,
89                        const char *str, const struct qstr *name)
90 #endif
91 {
92         ENTRY;
93
94         if (len != name->len)
95                 RETURN(1);
96
97         if (memcmp(str, name->name, len))
98                 RETURN(1);
99
100         CDEBUG(D_DENTRY, "found name %.*s(%p) flags %#x refc %d\n",
101                name->len, name->name, dentry, dentry->d_flags,
102                ll_d_count(dentry));
103
104         /* mountpoint is always valid */
105         if (d_mountpoint((struct dentry *)dentry))
106                 RETURN(0);
107
108         /* ensure exclusion against parallel lookup of the same name */
109         if (d_in_lookup((struct dentry *)dentry))
110                 return 0;
111
112         if (d_lustre_invalid(dentry))
113                 RETURN(1);
114
115         RETURN(0);
116 }
117
118 /**
119  * Called when last reference to a dentry is dropped and dcache wants to know
120  * whether or not it should cache it:
121  * - return 1 to delete the dentry immediately
122  * - return 0 to cache the dentry
123  * Should NOT be called with the dcache lock, see fs/dcache.c
124  */
125 static int ll_ddelete(const struct dentry *de)
126 {
127         ENTRY;
128         LASSERT(de);
129
130         CDEBUG(D_DENTRY, "%s dentry %pd (%p, parent %p, inode %p) %s%s\n",
131                d_lustre_invalid(de) ? "deleting" : "keeping",
132                de, de, de->d_parent, de->d_inode,
133                d_unhashed((struct dentry *)de) ? "" : "hashed,",
134                d_no_children(de) ? "" : "subdirs");
135
136         if (d_lustre_invalid(de))
137                 RETURN(1);
138         RETURN(0);
139 }
140
141 #ifdef HAVE_D_INIT
142 static int ll_d_init(struct dentry *de)
143 {
144         struct ll_dentry_data *lld;
145
146         OBD_ALLOC_PTR(lld);
147         lld->lld_invalid = 1;
148         de->d_fsdata = lld;
149         return 0;
150 }
151 #else /* !HAVE_D_INIT */
152
153 bool ll_d_setup(struct dentry *de, bool do_put)
154 {
155         struct ll_dentry_data *lld;
156         bool success = true;
157
158         if (de->d_fsdata)
159                 return success;
160
161         OBD_ALLOC_PTR(lld);
162         if (likely(lld)) {
163                 spin_lock(&de->d_lock);
164                 /* Since the first d_fsdata test was not
165                  * done under the spinlock it could have
166                  * changed by time the memory is allocated.
167                  */
168                 if (!de->d_fsdata) {
169                         lld->lld_invalid = 1;
170                         de->d_fsdata = lld;
171                 }
172                 spin_unlock(&de->d_lock);
173                 /* See if we lost the race to set d_fsdata. */
174                 if (de->d_fsdata != lld)
175                         OBD_FREE_PTR(lld);
176         } else {
177                 success = false;
178                 if (do_put)
179                         dput(de);
180         }
181
182         return success;
183 }
184 #endif /* !HAVE_D_INIT */
185
186 void ll_intent_drop_lock(struct lookup_intent *it)
187 {
188         if (it->it_op && it->it_lock_mode) {
189                 struct lustre_handle handle;
190
191                 handle.cookie = it->it_lock_handle;
192
193                 CDEBUG(D_DLMTRACE, "releasing lock with cookie %#llx from it %p\n",
194                        handle.cookie, it);
195                 ldlm_lock_decref(&handle, it->it_lock_mode);
196
197                 /* bug 494: intent_release may be called multiple times, from
198                  * this thread and we don't want to double-decref this lock
199                  */
200                 it->it_lock_mode = 0;
201                 if (it->it_remote_lock_mode != 0) {
202                         handle.cookie = it->it_remote_lock_handle;
203
204                         CDEBUG(D_DLMTRACE,
205                                "releasing remote lock with cookie %#llx from it %p\n",
206                                handle.cookie, it);
207                         ldlm_lock_decref(&handle,
208                                          it->it_remote_lock_mode);
209                         it->it_remote_lock_mode = 0;
210                 }
211         }
212 }
213
214 void ll_intent_release(struct lookup_intent *it)
215 {
216         ENTRY;
217
218         CDEBUG(D_INFO, "intent %p released\n", it);
219         ll_intent_drop_lock(it);
220         /* We are still holding extra reference on a request, need to free it */
221         if (it_disposition(it, DISP_ENQ_OPEN_REF))
222                 ptlrpc_req_finished(it->it_request); /* ll_file_open */
223
224         if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
225                 ptlrpc_req_finished(it->it_request);
226
227         it->it_disposition = 0;
228         it->it_request = NULL;
229         EXIT;
230 }
231
232 /* mark aliases invalid and prune unused aliases */
233 void ll_prune_aliases(struct inode *inode)
234 {
235         struct dentry *dentry;
236
237         ENTRY;
238
239         LASSERT(inode != NULL);
240
241         CDEBUG(D_INODE, "marking dentries for inode "DFID"(%p) invalid\n",
242                PFID(ll_inode2fid(inode)), inode);
243
244         spin_lock(&inode->i_lock);
245         hlist_for_each_entry(dentry, &inode->i_dentry, d_alias)
246                 d_lustre_invalidate(dentry);
247         spin_unlock(&inode->i_lock);
248
249         d_prune_aliases(inode);
250
251         EXIT;
252 }
253
254 int ll_revalidate_it_finish(struct ptlrpc_request *request,
255                             struct lookup_intent *it,
256                             struct dentry *de)
257 {
258         struct inode *inode = de->d_inode;
259         __u64 bits = 0;
260         int rc = 0;
261
262         ENTRY;
263
264         if (!request)
265                 RETURN(0);
266
267         if (it_disposition(it, DISP_LOOKUP_NEG))
268                 RETURN(-ENOENT);
269
270         rc = ll_prep_inode(&inode, &request->rq_pill, NULL, it);
271         if (rc)
272                 RETURN(rc);
273
274         ll_set_lock_data(ll_i2sbi(inode)->ll_md_exp, inode, it,
275                          &bits);
276         if (bits & MDS_INODELOCK_LOOKUP) {
277                 if (!ll_d_setup(de, true))
278                         RETURN(-ENOMEM);
279                 d_lustre_revalidate(de);
280                 if (S_ISDIR(inode->i_mode)) {
281                         struct dentry *parent = dget_parent(de);
282
283                         ll_update_dir_depth_dmv(d_inode(parent), de);
284                         dput(parent);
285                 }
286         }
287
288         RETURN(rc);
289 }
290
291 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
292 {
293         LASSERT(it != NULL);
294         LASSERT(dentry != NULL);
295
296         if (it->it_lock_mode && dentry->d_inode != NULL) {
297                 struct inode *inode = dentry->d_inode;
298                 struct ll_sb_info *sbi = ll_i2sbi(inode);
299
300                 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)\n",
301                        PFID(ll_inode2fid(inode)), inode);
302                 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
303         }
304
305         /* drop lookup or getattr locks immediately */
306         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)
307                 ll_intent_drop_lock(it);
308 }
309
310 static int ll_revalidate_dentry(struct dentry *dentry,
311                                 unsigned int lookup_flags)
312 {
313         struct dentry *parent;
314         struct inode *dir;
315         int rc;
316
317         CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
318                dentry->d_name.name, lookup_flags);
319
320         rc = llcrypt_d_revalidate(dentry, lookup_flags);
321         if (rc != 1)
322                 return rc;
323
324         /* If this is intermediate component path lookup and were able to get to
325          * this dentry, then its lock has not been revoked and the
326          * path component is valid.
327          */
328         if (lookup_flags & (LOOKUP_CONTINUE | LOOKUP_PARENT)) {
329                 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
330                         parent = dget_parent(dentry);
331                         ll_update_dir_depth_dmv(d_inode(parent), dentry);
332                         dput(parent);
333                 }
334                 return 1;
335         }
336
337         /* Symlink - always valid as long as the dentry was found */
338         /* only special case is to prevent ELOOP error from VFS during open
339          * of a foreign symlink file/dir with O_NOFOLLOW, like it happens for
340          * real symlinks. This will allow to open foreign symlink file/dir
341          * for get[dir]stripe/unlock ioctl()s.
342          */
343         if (d_is_symlink(dentry)) {
344                 if (!S_ISLNK(dentry->d_inode->i_mode) &&
345                     !(lookup_flags & LOOKUP_FOLLOW))
346                         return 0;
347                 else
348                         return 1;
349         }
350
351         /*
352          * VFS warns us that this is the second go around and previous
353          * operation failed (most likely open|creat), so this time
354          * we better talk to the server via the lookup path by name,
355          * not by fid.
356          */
357         if (lookup_flags & LOOKUP_REVAL)
358                 return 0;
359
360         if (lookup_flags & LOOKUP_RCU)
361                 return -ECHILD;
362
363         parent = dget_parent(dentry);
364         dir = d_inode(parent);
365         if (dentry_may_statahead(dir, dentry))
366                 ll_revalidate_statahead(dir, &dentry, dentry->d_inode == NULL);
367
368         if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
369                 ll_update_dir_depth_dmv(dir, dentry);
370         dput(parent);
371
372         return 1;
373 }
374
375 const struct dentry_operations ll_d_ops = {
376 #ifdef HAVE_D_INIT
377         .d_init         = ll_d_init,
378 #endif
379         .d_revalidate   = ll_revalidate_dentry,
380         .d_release      = ll_release,
381         .d_delete       = ll_ddelete,
382         .d_compare      = ll_dcompare,
383 };