Whamcloud - gitweb
b=2334
[fs/lustre-release.git] / lustre / llite / dcache.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/fs.h>
23 #include <linux/sched.h>
24 #include <linux/smp_lock.h>
25 #include <linux/quotaops.h>
26
27 #define DEBUG_SUBSYSTEM S_LLITE
28
29 #include <linux/obd_support.h>
30 #include <linux/lustre_lite.h>
31 #include <linux/lustre_idl.h>
32 #include <linux/lustre_dlm.h>
33
34 #include "llite_internal.h"
35
36 /* should NOT be called with the dcache lock, see fs/dcache.c */
37 static void ll_release(struct dentry *de)
38 {
39         struct ll_dentry_data *lld;
40         ENTRY;
41         LASSERT(de != NULL);
42         lld = ll_d2d(de);
43         LASSERT(lld != NULL);
44         LASSERT(lld->lld_cwd_count == 0);
45         LASSERT(lld->lld_mnt_count == 0);
46         OBD_FREE(de->d_fsdata, sizeof(struct ll_dentry_data));
47
48         EXIT;
49 }
50
51 void ll_set_dd(struct dentry *de)
52 {
53         ENTRY;
54         LASSERT(de != NULL);
55
56         lock_kernel();
57         if (de->d_fsdata == NULL) {
58                 OBD_ALLOC(de->d_fsdata, sizeof(struct ll_dentry_data));
59         }
60         unlock_kernel();
61
62         EXIT;
63 }
64
65 void ll_intent_release(struct lookup_intent *it)
66 {
67         struct lustre_handle *handle;
68         ENTRY;
69
70         if (it->it_op && it->d.lustre.it_lock_mode) {
71                 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
72                 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
73                        " from it %p\n", handle->cookie, it);
74                 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
75
76                 /* intent_release may be called multiple times, from
77                    this thread and we don't want to double-decref this
78                    lock (see bug 494) */
79                 it->d.lustre.it_lock_mode = 0;
80         }
81         it->it_magic = 0;
82         it->it_op_release = 0;
83         it->d.lustre.it_disposition = 0;
84         it->d.lustre.it_data = NULL;
85         EXIT;
86 }
87
88 void ll_unhash_aliases(struct inode *inode)
89 {
90         struct list_head *tmp, *head;
91         struct ll_sb_info *sbi;
92         ENTRY;
93
94         sbi = ll_i2sbi(inode);
95
96         CDEBUG(D_INODE, "marking dentries for ino %lu/%u(%p) invalid\n",
97                inode->i_ino, inode->i_generation, inode);
98
99         if (inode == NULL) {
100                 CERROR("unexpected NULL inode, tell phil\n");
101                 return;
102         }
103         head = &inode->i_dentry;
104 restart:
105         spin_lock(&dcache_lock);
106         tmp = head;
107         while ((tmp = tmp->next) != head) {
108                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
109                 if (!atomic_read(&dentry->d_count)) {
110                         dget_locked(dentry);
111                         __d_drop(dentry);
112                         spin_unlock(&dcache_lock);
113                         dput(dentry);
114                         goto restart;
115                 } else {
116                         hlist_del_init(&dentry->d_hash);
117                         dentry->d_flags |= DCACHE_LUSTRE_INVALID;
118                         hlist_add_head(&dentry->d_hash, 
119                                        &sbi->ll_orphan_dentry_list);
120                 }
121         }
122         spin_unlock(&dcache_lock);
123         EXIT;
124 }
125
126 extern struct dentry *ll_find_alias(struct inode *, struct dentry *);
127
128 static int revalidate_it_finish(struct ptlrpc_request *request, int offset,
129                                 struct lookup_intent *it,
130                                 struct dentry *de)
131 {
132         struct ll_sb_info *sbi;
133         int rc = 0;
134         ENTRY;
135
136         if (!request)
137                 RETURN(0);
138
139         if (it_disposition(it, DISP_LOOKUP_NEG))
140                 RETURN(-ENOENT);
141
142         sbi = ll_i2sbi(de->d_inode);
143         rc = ll_prep_inode(sbi->ll_osc_exp, &de->d_inode, request, offset,NULL);
144
145         RETURN(rc);
146 }
147
148 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
149 {
150         LASSERT(it != NULL);
151         LASSERT(dentry != NULL);
152
153         if (it->d.lustre.it_lock_mode && dentry->d_inode != NULL) {
154                 struct inode *inode = dentry->d_inode;
155                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
156                        inode, inode->i_ino, inode->i_generation);
157                 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
158         }
159
160         /* drop lookup or getattr locks immediately */
161         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)
162                 ll_intent_release(it);
163 }
164
165 void ll_frob_intent(struct lookup_intent **itp, struct lookup_intent *deft)
166 {
167         struct lookup_intent *it = *itp;
168 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
169         if (it && it->it_magic != INTENT_MAGIC) { 
170                 CERROR("WARNING: uninitialized intent\n");
171                 LBUG();
172         }
173         if (it && (it->it_op == IT_GETATTR || it->it_op == 0))
174                 it->it_op = IT_LOOKUP;
175         
176 #endif
177         if (!it || it->it_op == IT_GETXATTR)
178                 it = *itp = deft;
179
180         it->it_op_release = ll_intent_release;
181 }
182
183 int ll_revalidate_it(struct dentry *de, int flags, struct lookup_intent *it)
184 {
185         int rc;
186         struct ll_fid pfid, cfid;
187         struct it_cb_data icbd;
188         struct ll_uctxt ctxt;
189         struct ptlrpc_request *req = NULL;
190         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
191         struct obd_export *exp;
192
193         ENTRY;
194         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%s\n", de->d_name.name,
195                LL_IT2STR(it));
196
197         /* Cached negative dentries are unsafe for now - look them up again */
198         if (de->d_inode == NULL)
199                 RETURN(0);
200
201         exp = ll_i2mdcexp(de->d_inode);
202         ll_inode2fid(&pfid, de->d_parent->d_inode);
203         ll_inode2fid(&cfid, de->d_inode);
204         icbd.icbd_parent = de->d_parent->d_inode;
205         icbd.icbd_childp = &de;
206
207         /* 
208          * never execute intents for mount points
209          * - attrs will be fixed up in ll_revalidate_inode
210          */
211         if (d_mountpoint(de))
212                 RETURN(1);
213
214         ll_frob_intent(&it, &lookup_it);
215         LASSERT(it);
216
217         ll_i2uctxt(&ctxt, de->d_parent->d_inode, de->d_inode);
218
219         rc = mdc_intent_lock(exp, &ctxt, &pfid, de->d_name.name, de->d_name.len,
220                              NULL, 0,
221                              &cfid, it, flags, &req, ll_mdc_blocking_ast);
222         /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
223          * if all was well, it will return 1 if it found locks, 0 otherwise. */
224         if (req == NULL && rc >= 0)
225                 GOTO(out, rc);
226
227         if (rc < 0) {
228                 if (rc != -ESTALE) {
229                         CDEBUG(D_INFO, "ll_intent_lock: rc %d : it->it_status "
230                                "%d\n", rc, it->d.lustre.it_status);
231                 }
232                 GOTO(out, rc = 0);
233         }
234
235         rc = revalidate_it_finish(req, 1, it, de);
236         if (rc != 0) {
237                 ll_intent_release(it);
238                 GOTO(out, rc = 0);
239         }
240         rc = 1;
241
242         /* unfortunately ll_intent_lock may cause a callback and revoke our
243            dentry */
244         spin_lock(&dcache_lock);
245         hlist_del_init(&de->d_hash);
246         __d_rehash(de, 0);
247         spin_unlock(&dcache_lock);
248
249  out:
250         if (req != NULL && rc == 1)
251                 ptlrpc_req_finished(req);
252         if (rc == 0) { 
253                 ll_unhash_aliases(de->d_inode);
254                 de->d_flags |= DCACHE_LUSTRE_INVALID;
255         } else { 
256                 ll_lookup_finish_locks(it, de);
257                 de->d_flags &= ~DCACHE_LUSTRE_INVALID;
258         }
259         RETURN(rc);
260 }
261
262 /*static*/ void ll_pin(struct dentry *de, struct vfsmount *mnt, int flag)
263 {
264         struct inode *inode= de->d_inode;
265         struct ll_sb_info *sbi = ll_i2sbi(inode);
266         struct ll_dentry_data *ldd = ll_d2d(de);
267         struct obd_client_handle *handle;
268         int rc = 0;
269         ENTRY;
270         LASSERT(ldd);
271
272         lock_kernel();
273         /* Strictly speaking this introduces an additional race: the
274          * increments should wait until the rpc has returned.
275          * However, given that at present the function is void, this
276          * issue is moot. */
277         if (flag == 1 && (++ldd->lld_mnt_count) > 1) {
278                 unlock_kernel();
279                 EXIT;
280                 return;
281         }
282
283         if (flag == 0 && (++ldd->lld_cwd_count) > 1) {
284                 unlock_kernel();
285                 EXIT;
286                 return;
287         }
288         unlock_kernel();
289
290         handle = (flag) ? &ldd->lld_mnt_och : &ldd->lld_cwd_och;
291         rc = obd_pin(sbi->ll_mdc_exp, inode->i_ino, inode->i_generation,
292                      inode->i_mode & S_IFMT, handle, flag);
293
294         if (rc) {
295                 lock_kernel();
296                 memset(handle, 0, sizeof(*handle));
297                 if (flag == 0)
298                         ldd->lld_cwd_count--;
299                 else
300                         ldd->lld_mnt_count--;
301                 unlock_kernel();
302         }
303
304         EXIT;
305         return;
306 }
307
308 /*static*/ void ll_unpin(struct dentry *de, struct vfsmount *mnt, int flag)
309 {
310         struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
311         struct ll_dentry_data *ldd = ll_d2d(de);
312         struct obd_client_handle handle;
313         int count, rc = 0;
314         ENTRY;
315         LASSERT(ldd);
316
317         lock_kernel();
318         /* Strictly speaking this introduces an additional race: the
319          * increments should wait until the rpc has returned.
320          * However, given that at present the function is void, this
321          * issue is moot. */
322         handle = (flag) ? ldd->lld_mnt_och : ldd->lld_cwd_och;
323         if (handle.och_magic != OBD_CLIENT_HANDLE_MAGIC) {
324                 /* the "pin" failed */
325                 unlock_kernel();
326                 EXIT;
327                 return;
328         }
329
330         if (flag)
331                 count = --ldd->lld_mnt_count;
332         else
333                 count = --ldd->lld_cwd_count;
334         unlock_kernel();
335
336         if (count != 0) {
337                 EXIT;
338                 return;
339         }
340
341         rc = obd_unpin(sbi->ll_mdc_exp, &handle, flag);
342         EXIT;
343         return;
344 }
345
346 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
347 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
348 {
349         int rc;
350         ENTRY;
351
352         if (nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
353                 rc = ll_revalidate_it(dentry, nd->flags, &nd->intent);
354         else 
355                 rc = ll_revalidate_it(dentry, 0, NULL);
356
357         RETURN(rc);
358 }
359 #endif
360
361 struct dentry_operations ll_d_ops = {
362 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
363         .d_revalidate = ll_revalidate_nd,
364 #else
365         .d_revalidate_it = ll_revalidate_it,
366 #endif
367         .d_release = ll_release,
368 #if 0
369         .d_pin = ll_pin,
370         .d_unpin = ll_unpin,
371 #endif
372 };