Whamcloud - gitweb
* Changed find_route to always return an empty list, to complement
[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 #include <linux/lustre_version.h>
34
35 #include "llite_internal.h"
36
37 /* should NOT be called with the dcache lock, see fs/dcache.c */
38 static void ll_release(struct dentry *de)
39 {
40         struct ll_dentry_data *lld;
41         ENTRY;
42         LASSERT(de != NULL);
43         lld = ll_d2d(de);
44         if (lld == NULL) { /* NFS copies the de->d_op methods (bug 4655) */
45                 EXIT;
46                 return;
47         }
48         LASSERT(lld->lld_cwd_count == 0);
49         LASSERT(lld->lld_mnt_count == 0);
50         OBD_FREE(de->d_fsdata, sizeof(*lld));
51
52         EXIT;
53 }
54
55 /* Compare if two dentries are the same.  Don't match if the existing dentry
56  * is marked DCACHE_LUSTRE_INVALID.  Returns 1 if different, 0 if the same.
57  *
58  * This avoids a race where ll_lookup_it() instantiates a dentry, but we get
59  * an AST before calling d_revalidate_it().  The dentry still exists (marked
60  * INVALID) so d_lookup() matches it, but we have no lock on it (so
61  * lock_match() fails) and we spin around real_lookup(). */
62 static int ll_dcompare(struct dentry *parent, struct qstr *d_name,
63                        struct qstr *name)
64 {
65         struct dentry *dchild;
66         ENTRY;
67
68         if (d_name->len != name->len)
69                 RETURN(1);
70
71         if (memcmp(d_name->name, name->name, name->len))
72                 RETURN(1);
73
74         dchild = container_of(d_name, struct dentry, d_name); /* ugh */
75         if (dchild->d_flags & DCACHE_LUSTRE_INVALID) {
76                 CDEBUG(D_DENTRY,"INVALID dentry %p not matched, was bug 3784\n",
77                        dchild);
78                 RETURN(1);
79         }
80
81         RETURN(0);
82 }
83
84 /* should NOT be called with the dcache lock, see fs/dcache.c */
85 static int ll_ddelete(struct dentry *de)
86 {
87         ENTRY;
88         LASSERT(de);
89         CDEBUG(D_DENTRY, "%s dentry %.*s (%p, parent %p, inode %p) %s%s\n",
90                (de->d_flags & DCACHE_LUSTRE_INVALID ? "deleting" : "keeping"),
91                de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
92                d_unhashed(de) ? "" : "hashed,",
93                list_empty(&de->d_subdirs) ? "" : "subdirs");
94         RETURN(0);
95 }
96
97 void ll_set_dd(struct dentry *de)
98 {
99         ENTRY;
100         LASSERT(de != NULL);
101
102         CDEBUG(D_DENTRY, "ldd on dentry %.*s (%p) parent %p inode %p refc %d\n",
103                de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
104                atomic_read(&de->d_count));
105         lock_kernel();
106         if (de->d_fsdata == NULL) {
107                 OBD_ALLOC(de->d_fsdata, sizeof(struct ll_dentry_data));
108         }
109         unlock_kernel();
110
111         EXIT;
112 }
113
114 void ll_intent_drop_lock(struct lookup_intent *it)
115 {
116         struct lustre_handle *handle;
117
118         if (it->it_op && it->d.lustre.it_lock_mode) {
119                 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
120                 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
121                        " from it %p\n", handle->cookie, it);
122                 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
123
124                 /* bug 494: intent_release may be called multiple times, from
125                  * this thread and we don't want to double-decref this lock */
126                 it->d.lustre.it_lock_mode = 0;
127         }
128 }
129
130 void ll_intent_release(struct lookup_intent *it)
131 {
132         ENTRY;
133
134         ll_intent_drop_lock(it);
135         it->it_magic = 0;
136         it->it_op_release = 0;
137         it->d.lustre.it_disposition = 0;
138         it->d.lustre.it_data = NULL;
139         EXIT;
140 }
141
142 void ll_unhash_aliases(struct inode *inode)
143 {
144         struct list_head *tmp, *head;
145         struct ll_sb_info *sbi;
146         ENTRY;
147
148         if (inode == NULL) {
149                 CERROR("unexpected NULL inode, tell phil\n");
150                 return;
151         }
152
153         CDEBUG(D_INODE, "marking dentries for ino %lu/%u(%p) invalid\n",
154                inode->i_ino, inode->i_generation, inode);
155
156         sbi = ll_i2sbi(inode);
157         head = &inode->i_dentry;
158 restart:
159         spin_lock(&dcache_lock);
160         tmp = head;
161         while ((tmp = tmp->next) != head) {
162                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
163
164                 if (dentry->d_name.len == 1 && dentry->d_name.name[0] == '/') {
165                         CERROR("called on root (?) dentry=%p, inode=%p "
166                                "ino=%lu\n", dentry, inode, inode->i_ino);
167                         lustre_dump_dentry(dentry, 1);
168                         portals_debug_dumpstack(NULL);
169                 } else if (d_mountpoint(dentry)) {
170                         /* For mountpoints we skip removal of the dentry
171                            which happens solely because we have a lock on it
172                            obtained when this dentry was not a mountpoint yet */
173                         CDEBUG(D_DENTRY, "Skippind mountpoint dentry removal "
174                                          "%.*s (%p) parent %p\n",
175                                           dentry->d_name.len,
176                                           dentry->d_name.name,
177                                           dentry, dentry->d_parent);
178
179                         continue;
180                 }
181
182                 if (atomic_read(&dentry->d_count) == 0) {
183                         CDEBUG(D_DENTRY, "deleting dentry %.*s (%p) parent %p "
184                                "inode %p\n", dentry->d_name.len,
185                                dentry->d_name.name, dentry, dentry->d_parent,
186                                dentry->d_inode);
187                         dget_locked(dentry);
188                         __d_drop(dentry);
189 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
190                         INIT_HLIST_NODE(&dentry->d_hash);
191 #endif
192                         spin_unlock(&dcache_lock);
193                         dput(dentry);
194                         goto restart;
195                 } else if (!(dentry->d_flags & DCACHE_LUSTRE_INVALID)) {
196                         CDEBUG(D_DENTRY, "unhashing dentry %.*s (%p) parent %p "
197                                "inode %p refc %d\n", dentry->d_name.len,
198                                dentry->d_name.name, dentry, dentry->d_parent,
199                                dentry->d_inode, atomic_read(&dentry->d_count));
200                         hlist_del_init(&dentry->d_hash);
201                         dentry->d_flags |= DCACHE_LUSTRE_INVALID;
202                         hlist_add_head(&dentry->d_hash,
203                                        &sbi->ll_orphan_dentry_list);
204                 }
205         }
206         spin_unlock(&dcache_lock);
207         EXIT;
208 }
209
210 static int revalidate_it_finish(struct ptlrpc_request *request, int offset,
211                                 struct lookup_intent *it,
212                                 struct dentry *de)
213 {
214         struct ll_sb_info *sbi;
215         int rc = 0;
216         ENTRY;
217
218         if (!request)
219                 RETURN(0);
220
221         if (it_disposition(it, DISP_LOOKUP_NEG))
222                 RETURN(-ENOENT);
223
224         sbi = ll_i2sbi(de->d_inode);
225         rc = ll_prep_inode(sbi->ll_osc_exp, &de->d_inode, request, offset,NULL);
226
227         RETURN(rc);
228 }
229
230 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
231 {
232         LASSERT(it != NULL);
233         LASSERT(dentry != NULL);
234
235         if (it->d.lustre.it_lock_mode && dentry->d_inode != NULL) {
236                 struct inode *inode = dentry->d_inode;
237                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
238                        inode, inode->i_ino, inode->i_generation);
239                 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
240         }
241
242         /* drop lookup or getattr locks immediately */
243         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
244 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
245                 /* on 2.6 there are situation when several lookups and
246                  * revalidations may be requested during single operation.
247                  * therefore, we don't release intent here -bzzz */
248                 ll_intent_drop_lock(it);
249 #else
250                 ll_intent_release(it);
251 #endif
252         }
253 }
254
255 void ll_frob_intent(struct lookup_intent **itp, struct lookup_intent *deft)
256 {
257         struct lookup_intent *it = *itp;
258 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
259         if (it) {
260                 LASSERTF(it->it_magic == INTENT_MAGIC, "bad intent magic: %x\n",
261                          it->it_magic);
262         }
263 #endif
264
265         if (!it || it->it_op == IT_GETXATTR)
266                 it = *itp = deft;
267
268         it->it_op_release = ll_intent_release;
269 }
270
271 int ll_revalidate_it(struct dentry *de, int lookup_flags,
272                      struct lookup_intent *it)
273 {
274         int rc;
275         struct ll_fid pfid, cfid;
276         struct it_cb_data icbd;
277         struct ll_uctxt ctxt;
278         struct ptlrpc_request *req = NULL;
279         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
280         struct obd_export *exp;
281
282         ENTRY;
283         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%s\n", de->d_name.name,
284                LL_IT2STR(it));
285
286         /* Cached negative dentries are unsafe for now - look them up again */
287         if (de->d_inode == NULL)
288                 RETURN(0);
289
290         exp = ll_i2mdcexp(de->d_inode);
291         ll_inode2fid(&pfid, de->d_parent->d_inode);
292         ll_inode2fid(&cfid, de->d_inode);
293         icbd.icbd_parent = de->d_parent->d_inode;
294         icbd.icbd_childp = &de;
295
296         /* Never execute intents for mount points.
297          * Attributes will be fixed up in ll_inode_revalidate_it */
298         if (d_mountpoint(de))
299                 RETURN(1);
300
301         OBD_FAIL_TIMEOUT(OBD_FAIL_MDC_REVALIDATE_PAUSE, 5);
302         ll_frob_intent(&it, &lookup_it);
303         LASSERT(it);
304
305         ll_i2uctxt(&ctxt, de->d_parent->d_inode, de->d_inode);
306
307         rc = mdc_intent_lock(exp, &ctxt, &pfid, de->d_name.name, de->d_name.len,
308                              NULL, 0,
309                              &cfid, it, lookup_flags, &req,ll_mdc_blocking_ast);
310         /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
311          * if all was well, it will return 1 if it found locks, 0 otherwise. */
312         if (req == NULL && rc >= 0)
313                 GOTO(out, rc);
314
315         if (rc < 0) {
316                 if (rc != -ESTALE) {
317                         CDEBUG(D_INFO, "ll_intent_lock: rc %d : it->it_status "
318                                "%d\n", rc, it->d.lustre.it_status);
319                 }
320                 GOTO(out, rc = 0);
321         }
322
323         rc = revalidate_it_finish(req, 1, it, de);
324         if (rc != 0) {
325                 ll_intent_release(it);
326                 GOTO(out, rc = 0);
327         }
328         rc = 1;
329
330         /* unfortunately ll_intent_lock may cause a callback and revoke our
331          * dentry */
332         spin_lock(&dcache_lock);
333         hlist_del_init(&de->d_hash);
334         __d_rehash(de, 0);
335         spin_unlock(&dcache_lock);
336
337  out:
338         /* If we had succesful it lookup on mds, but it happened to be negative,
339            we do not free request as it will be reused during lookup (see
340            comment in mdc/mdc_locks.c::mdc_intent_lock(). But if
341            request was not completed, we need to free it. (bug 5154) */
342         if (req != NULL && (rc == 1 || !it_disposition(it, DISP_ENQ_COMPLETE)))
343                 ptlrpc_req_finished(req);
344         if (rc == 0) {
345                 ll_unhash_aliases(de->d_inode);
346                 /* done in ll_unhash_aliases()
347                 dentry->d_flags |= DCACHE_LUSTRE_INVALID; */
348         } else {
349                 CDEBUG(D_DENTRY, "revalidated dentry %.*s (%p) parent %p "
350                                "inode %p refc %d\n", de->d_name.len,
351                                de->d_name.name, de, de->d_parent, de->d_inode,
352                                atomic_read(&de->d_count));
353                 ll_lookup_finish_locks(it, de);
354                 de->d_flags &= ~DCACHE_LUSTRE_INVALID;
355         }
356         RETURN(rc);
357 }
358
359 /*static*/ void ll_pin(struct dentry *de, struct vfsmount *mnt, int flag)
360 {
361         struct inode *inode= de->d_inode;
362         struct ll_sb_info *sbi = ll_i2sbi(inode);
363         struct ll_dentry_data *ldd = ll_d2d(de);
364         struct obd_client_handle *handle;
365         int rc = 0;
366         ENTRY;
367         LASSERT(ldd);
368
369         lock_kernel();
370         /* Strictly speaking this introduces an additional race: the
371          * increments should wait until the rpc has returned.
372          * However, given that at present the function is void, this
373          * issue is moot. */
374         if (flag == 1 && (++ldd->lld_mnt_count) > 1) {
375                 unlock_kernel();
376                 EXIT;
377                 return;
378         }
379
380         if (flag == 0 && (++ldd->lld_cwd_count) > 1) {
381                 unlock_kernel();
382                 EXIT;
383                 return;
384         }
385         unlock_kernel();
386
387         handle = (flag) ? &ldd->lld_mnt_och : &ldd->lld_cwd_och;
388         rc = obd_pin(sbi->ll_mdc_exp, inode->i_ino, inode->i_generation,
389                      inode->i_mode & S_IFMT, handle, flag);
390
391         if (rc) {
392                 lock_kernel();
393                 memset(handle, 0, sizeof(*handle));
394                 if (flag == 0)
395                         ldd->lld_cwd_count--;
396                 else
397                         ldd->lld_mnt_count--;
398                 unlock_kernel();
399         }
400
401         EXIT;
402         return;
403 }
404
405 /*static*/ void ll_unpin(struct dentry *de, struct vfsmount *mnt, int flag)
406 {
407         struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
408         struct ll_dentry_data *ldd = ll_d2d(de);
409         struct obd_client_handle handle;
410         int count, rc = 0;
411         ENTRY;
412         LASSERT(ldd);
413
414         lock_kernel();
415         /* Strictly speaking this introduces an additional race: the
416          * increments should wait until the rpc has returned.
417          * However, given that at present the function is void, this
418          * issue is moot. */
419         handle = (flag) ? ldd->lld_mnt_och : ldd->lld_cwd_och;
420         if (handle.och_magic != OBD_CLIENT_HANDLE_MAGIC) {
421                 /* the "pin" failed */
422                 unlock_kernel();
423                 EXIT;
424                 return;
425         }
426
427         if (flag)
428                 count = --ldd->lld_mnt_count;
429         else
430                 count = --ldd->lld_cwd_count;
431         unlock_kernel();
432
433         if (count != 0) {
434                 EXIT;
435                 return;
436         }
437
438         rc = obd_unpin(sbi->ll_mdc_exp, &handle, flag);
439         EXIT;
440         return;
441 }
442
443 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
444 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
445 {
446         int rc;
447         ENTRY;
448
449         if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
450                 rc = ll_revalidate_it(dentry, nd->flags, &nd->intent);
451         else
452                 rc = ll_revalidate_it(dentry, 0, NULL);
453
454         RETURN(rc);
455 }
456 #endif
457
458 struct dentry_operations ll_d_ops = {
459 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
460         .d_revalidate = ll_revalidate_nd,
461 #else
462         .d_revalidate_it = ll_revalidate_it,
463 #endif
464         .d_release = ll_release,
465         .d_delete = ll_ddelete,
466         .d_compare = ll_dcompare,
467 #if 0
468         .d_pin = ll_pin,
469         .d_unpin = ll_unpin,
470 #endif
471 };