Whamcloud - gitweb
LU-14989 sec: keep encryption context in xattr cache
[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         ENTRY;
57         LASSERT(de != NULL);
58         lld = ll_d2d(de);
59         if (lld == NULL) /* NFS copies the de->d_op methods (bug 4655) */
60                 RETURN_EXIT;
61
62         de->d_fsdata = NULL;
63         call_rcu(&lld->lld_rcu_head, free_dentry_data);
64
65         EXIT;
66 }
67
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.
70  *
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().
75  *
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.
81  */
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)
89 #endif
90 {
91         ENTRY;
92
93         if (len != name->len)
94                 RETURN(1);
95
96         if (memcmp(str, name->name, len))
97                 RETURN(1);
98
99         CDEBUG(D_DENTRY, "found name %.*s(%p) flags %#x refc %d\n",
100                name->len, name->name, dentry, dentry->d_flags,
101                ll_d_count(dentry));
102
103         /* mountpoint is always valid */
104         if (d_mountpoint((struct dentry *)dentry))
105                 RETURN(0);
106
107         /* ensure exclusion against parallel lookup of the same name */
108         if (d_in_lookup((struct dentry *)dentry))
109                 return 0;
110
111         if (d_lustre_invalid(dentry))
112                 RETURN(1);
113
114         RETURN(0);
115 }
116
117 /**
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
123  */
124 static int ll_ddelete(const struct dentry *de)
125 {
126         ENTRY;
127         LASSERT(de);
128
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");
134
135         /* kernel >= 2.6.38 last refcount is decreased after this function. */
136         LASSERT(ll_d_count(de) == 1);
137
138         if (d_lustre_invalid(de))
139                 RETURN(1);
140         RETURN(0);
141 }
142
143 #ifdef HAVE_D_INIT
144 static int ll_d_init(struct dentry *de)
145 {
146         struct ll_dentry_data *lld;
147
148         OBD_ALLOC_PTR(lld);
149         lld->lld_invalid = 1;
150         de->d_fsdata = lld;
151         return 0;
152 }
153 #else /* !HAVE_D_INIT */
154
155 bool ll_d_setup(struct dentry *de, bool do_put)
156 {
157         struct ll_dentry_data *lld;
158         bool success = true;
159
160         if (de->d_fsdata)
161                 return success;
162
163         OBD_ALLOC_PTR(lld);
164         if (likely(lld)) {
165                 spin_lock(&de->d_lock);
166                 /* Since the first d_fsdata test was not
167                  * done under the spinlock it could have
168                  * changed by time the memory is allocated.
169                  */
170                 if (!de->d_fsdata) {
171                         lld->lld_invalid = 1;
172                         de->d_fsdata = lld;
173                 }
174                 spin_unlock(&de->d_lock);
175                 /* See if we lost the race to set d_fsdata. */
176                 if (de->d_fsdata != lld)
177                         OBD_FREE_PTR(lld);
178         } else {
179                 success = false;
180                 if (do_put)
181                         dput(de);
182         }
183
184         return success;
185 }
186 #endif /* !HAVE_D_INIT */
187
188 void ll_intent_drop_lock(struct lookup_intent *it)
189 {
190         if (it->it_op && it->it_lock_mode) {
191                 struct lustre_handle handle;
192
193                 handle.cookie = it->it_lock_handle;
194
195                 CDEBUG(D_DLMTRACE, "releasing lock with cookie %#llx from it %p\n",
196                        handle.cookie, it);
197                 ldlm_lock_decref(&handle, it->it_lock_mode);
198
199                 /* bug 494: intent_release may be called multiple times, from
200                  * this thread and we don't want to double-decref this lock */
201                 it->it_lock_mode = 0;
202                 if (it->it_remote_lock_mode != 0) {
203                         handle.cookie = it->it_remote_lock_handle;
204
205                         CDEBUG(D_DLMTRACE,
206                                "releasing remote lock with cookie %#llx from it %p\n",
207                                handle.cookie, it);
208                         ldlm_lock_decref(&handle,
209                                          it->it_remote_lock_mode);
210                         it->it_remote_lock_mode = 0;
211                 }
212         }
213 }
214
215 void ll_intent_release(struct lookup_intent *it)
216 {
217         ENTRY;
218
219         CDEBUG(D_INFO, "intent %p released\n", it);
220         ll_intent_drop_lock(it);
221         /* We are still holding extra reference on a request, need to free it */
222         if (it_disposition(it, DISP_ENQ_OPEN_REF))
223                 ptlrpc_req_finished(it->it_request); /* ll_file_open */
224
225         if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
226                 ptlrpc_req_finished(it->it_request);
227
228         it->it_disposition = 0;
229         it->it_request = NULL;
230         EXIT;
231 }
232
233 /* mark aliases invalid and prune unused aliases */
234 void ll_prune_aliases(struct inode *inode)
235 {
236         struct dentry *dentry;
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         int rc = 0;
259
260         ENTRY;
261
262         if (!request)
263                 RETURN(0);
264
265         if (it_disposition(it, DISP_LOOKUP_NEG))
266                 RETURN(-ENOENT);
267
268         rc = ll_prep_inode(&de->d_inode, &request->rq_pill, NULL, it);
269
270         RETURN(rc);
271 }
272
273 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
274 {
275         LASSERT(it != NULL);
276         LASSERT(dentry != NULL);
277
278         if (it->it_lock_mode && dentry->d_inode != NULL) {
279                 struct inode *inode = dentry->d_inode;
280                 struct ll_sb_info *sbi = ll_i2sbi(inode);
281
282                 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)\n",
283                        PFID(ll_inode2fid(inode)), inode);
284                 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
285         }
286
287         /* drop lookup or getattr locks immediately */
288         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)
289                 ll_intent_drop_lock(it);
290 }
291
292 static int ll_revalidate_dentry(struct dentry *dentry,
293                                 unsigned int lookup_flags)
294 {
295         struct inode *dir = dentry->d_parent->d_inode;
296         int rc;
297
298         CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
299                dentry->d_name.name, lookup_flags);
300
301         rc = ll_revalidate_d_crypto(dentry, lookup_flags);
302         if (rc != 1)
303                 return rc;
304
305         /* If this is intermediate component path lookup and we were able to get
306          * to this dentry, then its lock has not been revoked and the
307          * path component is valid. */
308         if (lookup_flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))
309                 return 1;
310
311         /* Symlink - always valid as long as the dentry was found */
312         /* only special case is to prevent ELOOP error from VFS during open
313          * of a foreign symlink file/dir with O_NOFOLLOW, like it happens for
314          * real symlinks. This will allow to open foreign symlink file/dir
315          * for get[dir]stripe/unlock ioctl()s.
316          */
317         if (d_is_symlink(dentry)) {
318                 if (!S_ISLNK(dentry->d_inode->i_mode) &&
319                     !(lookup_flags & LOOKUP_FOLLOW))
320                         return 0;
321                 else
322                         return 1;
323         }
324
325         /*
326          * VFS warns us that this is the second go around and previous
327          * operation failed (most likely open|creat), so this time
328          * we better talk to the server via the lookup path by name,
329          * not by fid.
330          */
331         if (lookup_flags & LOOKUP_REVAL)
332                 return 0;
333
334         if (lookup_flags & LOOKUP_RCU)
335                 return -ECHILD;
336
337         if (dentry_may_statahead(dir, dentry))
338                 ll_revalidate_statahead(dir, &dentry, dentry->d_inode == NULL);
339
340         return 1;
341 }
342
343 const struct dentry_operations ll_d_ops = {
344 #ifdef HAVE_D_INIT
345         .d_init         = ll_d_init,
346 #endif
347         .d_revalidate   = ll_revalidate_dentry,
348         .d_release      = ll_release,
349         .d_delete       = ll_ddelete,
350         .d_compare      = ll_dcompare,
351 };