Whamcloud - gitweb
cc03462fb456a0a3cbe513044a4965b692b53605
[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         de->d_fsdata = NULL;
64         call_rcu(&lld->lld_rcu_head, free_dentry_data);
65
66         EXIT;
67 }
68
69 /**
70  * Called when last reference to a dentry is dropped and dcache wants to know
71  * whether or not it should cache it:
72  * - return 1 to delete the dentry immediately
73  * - return 0 to cache the dentry
74  * Should NOT be called with the dcache lock, see fs/dcache.c
75  */
76 static int ll_ddelete(const struct dentry *de)
77 {
78         ENTRY;
79         LASSERT(de);
80
81         CDEBUG(D_DENTRY, "%s dentry %.*s (%p, parent %p, inode %p) %s%s\n",
82                d_lustre_invalid((struct dentry *)de) ? "deleting" : "keeping",
83                de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
84                d_unhashed((struct dentry *)de) ? "" : "hashed,",
85                list_empty(&de->d_subdirs) ? "" : "subdirs");
86
87         /* kernel >= 2.6.38 last refcount is decreased after this function. */
88         LASSERT(ll_d_count(de) == 1);
89
90         if (d_lustre_invalid((struct dentry *)de))
91                 RETURN(1);
92         RETURN(0);
93 }
94
95 int ll_d_init(struct dentry *de)
96 {
97         ENTRY;
98         LASSERT(de != NULL);
99
100         CDEBUG(D_DENTRY, "ldd on dentry %.*s (%p) parent %p inode %p refc %d\n",
101                 de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
102                 ll_d_count(de));
103
104         if (de->d_fsdata == NULL) {
105                 struct ll_dentry_data *lld;
106
107                 OBD_ALLOC_PTR(lld);
108                 if (likely(lld != NULL)) {
109                         spin_lock(&de->d_lock);
110                         if (likely(de->d_fsdata == NULL)) {
111                                 de->d_fsdata = lld;
112                                 __d_lustre_invalidate(de);
113                         } else {
114                                 OBD_FREE_PTR(lld);
115                         }
116                         spin_unlock(&de->d_lock);
117                 } else {
118                         RETURN(-ENOMEM);
119                 }
120         }
121         LASSERT(de->d_op == &ll_d_ops);
122
123         RETURN(0);
124 }
125
126 void ll_intent_drop_lock(struct lookup_intent *it)
127 {
128         if (it->it_op && it->it_lock_mode) {
129                 struct lustre_handle handle;
130
131                 handle.cookie = it->it_lock_handle;
132
133                 CDEBUG(D_DLMTRACE, "releasing lock with cookie %#llx from it %p\n",
134                        handle.cookie, it);
135                 ldlm_lock_decref(&handle, it->it_lock_mode);
136
137                 /* bug 494: intent_release may be called multiple times, from
138                  * this thread and we don't want to double-decref this lock */
139                 it->it_lock_mode = 0;
140                 if (it->it_remote_lock_mode != 0) {
141                         handle.cookie = it->it_remote_lock_handle;
142
143                         CDEBUG(D_DLMTRACE,
144                                "releasing remote lock with cookie %#llx from it %p\n",
145                                handle.cookie, it);
146                         ldlm_lock_decref(&handle,
147                                          it->it_remote_lock_mode);
148                         it->it_remote_lock_mode = 0;
149                 }
150         }
151 }
152
153 void ll_intent_release(struct lookup_intent *it)
154 {
155         ENTRY;
156
157         CDEBUG(D_INFO, "intent %p released\n", it);
158         ll_intent_drop_lock(it);
159         /* We are still holding extra reference on a request, need to free it */
160         if (it_disposition(it, DISP_ENQ_OPEN_REF))
161                 ptlrpc_req_finished(it->it_request); /* ll_file_open */
162
163         if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
164                 ptlrpc_req_finished(it->it_request);
165
166         it->it_disposition = 0;
167         it->it_request = NULL;
168         EXIT;
169 }
170
171 void ll_invalidate_aliases(struct inode *inode)
172 {
173         struct dentry *dentry;
174         ENTRY;
175
176         LASSERT(inode != NULL);
177
178         CDEBUG(D_INODE, "marking dentries for inode "DFID"(%p) invalid\n",
179                PFID(ll_inode2fid(inode)), inode);
180
181         spin_lock(&inode->i_lock);
182         hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
183                 CDEBUG(D_DENTRY, "dentry in drop %.*s (%p) parent %p "
184                        "inode %p flags %d\n", dentry->d_name.len,
185                        dentry->d_name.name, dentry, dentry->d_parent,
186                        dentry->d_inode, dentry->d_flags);
187
188                 d_lustre_invalidate(dentry, 0);
189         }
190         spin_unlock(&inode->i_lock);
191
192         EXIT;
193 }
194
195 int ll_revalidate_it_finish(struct ptlrpc_request *request,
196                             struct lookup_intent *it,
197                             struct dentry *de)
198 {
199         int rc = 0;
200         ENTRY;
201
202         if (!request)
203                 RETURN(0);
204
205         if (it_disposition(it, DISP_LOOKUP_NEG))
206                 RETURN(-ENOENT);
207
208         rc = ll_prep_inode(&de->d_inode, request, NULL, it);
209
210         RETURN(rc);
211 }
212
213 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
214 {
215         LASSERT(it != NULL);
216         LASSERT(dentry != NULL);
217
218         if (it->it_lock_mode && dentry->d_inode != NULL) {
219                 struct inode *inode = dentry->d_inode;
220                 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
221
222                 CDEBUG(D_DLMTRACE, "setting l_data to inode "DFID"(%p)\n",
223                        PFID(ll_inode2fid(inode)), inode);
224                 ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
225         }
226
227         /* drop lookup or getattr locks immediately */
228         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
229                 /* on 2.6 there are situation when several lookups and
230                  * revalidations may be requested during single operation.
231                  * therefore, we don't release intent here -bzzz */
232                 ll_intent_drop_lock(it);
233         }
234 }
235
236 static int ll_revalidate_dentry(struct dentry *dentry,
237                                 unsigned int lookup_flags)
238 {
239         struct inode *dir = dentry->d_parent->d_inode;
240
241         CDEBUG(D_VFSTRACE, "VFS Op:name=%s, flags=%u\n",
242                dentry->d_name.name, lookup_flags);
243
244         /* mountpoint is always valid */
245         if (d_mountpoint((struct dentry *)dentry))
246                 return 1;
247
248         /* No lock -> invalid dentry */
249         if (d_lustre_invalid(dentry))
250                 return 0;
251
252         /* If this is intermediate component path lookup and we were able to get
253          * to this dentry, then its lock has not been revoked and the
254          * path component is valid. */
255         if (lookup_flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))
256                 return 1;
257
258         /* Symlink - always valid as long as the dentry was found */
259 #ifdef HAVE_IOP_GET_LINK
260         if (dentry->d_inode && dentry->d_inode->i_op->get_link)
261 #else
262         if (dentry->d_inode && dentry->d_inode->i_op->follow_link)
263 #endif
264                 return 1;
265
266         /*
267          * VFS warns us that this is the second go around and previous
268          * operation failed (most likely open|creat), so this time
269          * we better talk to the server via the lookup path by name,
270          * not by fid.
271          */
272         if (lookup_flags & LOOKUP_REVAL)
273                 return 0;
274
275         if (lookup_flags & LOOKUP_RCU)
276                 return -ECHILD;
277
278         if (dentry_may_statahead(dir, dentry))
279                 ll_statahead(dir, &dentry, dentry->d_inode == NULL);
280
281         return 1;
282 }
283
284 const struct dentry_operations ll_d_ops = {
285         .d_revalidate   = ll_revalidate_dentry,
286         .d_release = ll_release,
287         .d_delete  = ll_ddelete,
288 };