Whamcloud - gitweb
b=4834
[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                         CERROR("called on mountpoint (?) dentry=%p, inode=%p "
171                                "ino=%lu\n", dentry, inode, inode->i_ino);
172                         lustre_dump_dentry(dentry, 1);
173                         portals_debug_dumpstack(NULL);
174                 }
175
176                 if (atomic_read(&dentry->d_count) == 0) {
177                         CDEBUG(D_DENTRY, "deleting dentry %.*s (%p) parent %p "
178                                "inode %p\n", dentry->d_name.len,
179                                dentry->d_name.name, dentry, dentry->d_parent,
180                                dentry->d_inode);
181                         dget_locked(dentry);
182                         __d_drop(dentry);
183 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
184                         INIT_HLIST_NODE(&dentry->d_hash);
185 #endif
186                         spin_unlock(&dcache_lock);
187                         dput(dentry);
188                         goto restart;
189                 } else if (!(dentry->d_flags & DCACHE_LUSTRE_INVALID)) {
190                         CDEBUG(D_DENTRY, "unhashing dentry %.*s (%p) parent %p "
191                                "inode %p refc %d\n", dentry->d_name.len,
192                                dentry->d_name.name, dentry, dentry->d_parent,
193                                dentry->d_inode, atomic_read(&dentry->d_count));
194                         hlist_del_init(&dentry->d_hash);
195                         dentry->d_flags |= DCACHE_LUSTRE_INVALID;
196                         hlist_add_head(&dentry->d_hash,
197                                        &sbi->ll_orphan_dentry_list);
198                 }
199         }
200         spin_unlock(&dcache_lock);
201         EXIT;
202 }
203
204 static int revalidate_it_finish(struct ptlrpc_request *request, int offset,
205                                 struct lookup_intent *it,
206                                 struct dentry *de)
207 {
208         struct ll_sb_info *sbi;
209         int rc = 0;
210         ENTRY;
211
212         if (!request)
213                 RETURN(0);
214
215         if (it_disposition(it, DISP_LOOKUP_NEG))
216                 RETURN(-ENOENT);
217
218         sbi = ll_i2sbi(de->d_inode);
219         rc = ll_prep_inode(sbi->ll_osc_exp, &de->d_inode, request, offset,NULL);
220
221         RETURN(rc);
222 }
223
224 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
225 {
226         LASSERT(it != NULL);
227         LASSERT(dentry != NULL);
228
229         if (it->d.lustre.it_lock_mode && dentry->d_inode != NULL) {
230                 struct inode *inode = dentry->d_inode;
231                 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
232                        inode, inode->i_ino, inode->i_generation);
233                 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
234         }
235
236         /* drop lookup or getattr locks immediately */
237         if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
238 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
239                 /* on 2.6 there are situation when several lookups and
240                  * revalidations may be requested during single operation.
241                  * therefore, we don't release intent here -bzzz */
242                 ll_intent_drop_lock(it);
243 #else
244                 ll_intent_release(it);
245 #endif
246         }
247 }
248
249 void ll_frob_intent(struct lookup_intent **itp, struct lookup_intent *deft)
250 {
251         struct lookup_intent *it = *itp;
252 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
253         if (it) {
254                 LASSERTF(it->it_magic == INTENT_MAGIC, "bad intent magic: %x\n",
255                          it->it_magic);
256         }
257 #endif
258
259         if (!it || it->it_op == IT_GETXATTR)
260                 it = *itp = deft;
261
262         it->it_op_release = ll_intent_release;
263 }
264
265 int ll_revalidate_it(struct dentry *de, int lookup_flags,
266                      struct lookup_intent *it)
267 {
268         int rc;
269         struct ll_fid pfid, cfid;
270         struct it_cb_data icbd;
271         struct ll_uctxt ctxt;
272         struct ptlrpc_request *req = NULL;
273         struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
274         struct obd_export *exp;
275
276         ENTRY;
277         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%s\n", de->d_name.name,
278                LL_IT2STR(it));
279
280         /* Cached negative dentries are unsafe for now - look them up again */
281         if (de->d_inode == NULL)
282                 RETURN(0);
283
284         exp = ll_i2mdcexp(de->d_inode);
285         ll_inode2fid(&pfid, de->d_parent->d_inode);
286         ll_inode2fid(&cfid, de->d_inode);
287         icbd.icbd_parent = de->d_parent->d_inode;
288         icbd.icbd_childp = &de;
289
290         /* Never execute intents for mount points.
291          * Attributes will be fixed up in ll_inode_revalidate_it */
292         if (d_mountpoint(de))
293                 RETURN(1);
294
295         OBD_FAIL_TIMEOUT(OBD_FAIL_MDC_REVALIDATE_PAUSE, 5);
296         ll_frob_intent(&it, &lookup_it);
297         LASSERT(it);
298
299         ll_i2uctxt(&ctxt, de->d_parent->d_inode, de->d_inode);
300
301         rc = mdc_intent_lock(exp, &ctxt, &pfid, de->d_name.name, de->d_name.len,
302                              NULL, 0,
303                              &cfid, it, lookup_flags, &req,ll_mdc_blocking_ast);
304         /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
305          * if all was well, it will return 1 if it found locks, 0 otherwise. */
306         if (req == NULL && rc >= 0)
307                 GOTO(out, rc);
308
309         if (rc < 0) {
310                 if (rc != -ESTALE) {
311                         CDEBUG(D_INFO, "ll_intent_lock: rc %d : it->it_status "
312                                "%d\n", rc, it->d.lustre.it_status);
313                 }
314                 GOTO(out, rc = 0);
315         }
316
317         rc = revalidate_it_finish(req, 1, it, de);
318         if (rc != 0) {
319                 ll_intent_release(it);
320                 GOTO(out, rc = 0);
321         }
322         rc = 1;
323
324         /* unfortunately ll_intent_lock may cause a callback and revoke our
325          * dentry */
326         spin_lock(&dcache_lock);
327         hlist_del_init(&de->d_hash);
328         __d_rehash(de, 0);
329         spin_unlock(&dcache_lock);
330
331  out:
332         /* If we had succesful it lookup on mds, but it happened to be negative,
333            we do not free request as it will be reused during lookup (see
334            comment in mdc/mdc_locks.c::mdc_intent_lock(). But if
335            request was not completed, we need to free it. (bug 5154) */
336         if (req != NULL && (rc == 1 || !it_disposition(it, DISP_ENQ_COMPLETE)))
337                 ptlrpc_req_finished(req);
338         if (rc == 0) {
339                 ll_unhash_aliases(de->d_inode);
340                 /* done in ll_unhash_aliases()
341                 dentry->d_flags |= DCACHE_LUSTRE_INVALID; */
342         } else {
343                 CDEBUG(D_DENTRY, "revalidated dentry %.*s (%p) parent %p "
344                                "inode %p refc %d\n", de->d_name.len,
345                                de->d_name.name, de, de->d_parent, de->d_inode,
346                                atomic_read(&de->d_count));
347                 ll_lookup_finish_locks(it, de);
348                 de->d_flags &= ~DCACHE_LUSTRE_INVALID;
349         }
350         RETURN(rc);
351 }
352
353 /*static*/ void ll_pin(struct dentry *de, struct vfsmount *mnt, int flag)
354 {
355         struct inode *inode= de->d_inode;
356         struct ll_sb_info *sbi = ll_i2sbi(inode);
357         struct ll_dentry_data *ldd = ll_d2d(de);
358         struct obd_client_handle *handle;
359         int rc = 0;
360         ENTRY;
361         LASSERT(ldd);
362
363         lock_kernel();
364         /* Strictly speaking this introduces an additional race: the
365          * increments should wait until the rpc has returned.
366          * However, given that at present the function is void, this
367          * issue is moot. */
368         if (flag == 1 && (++ldd->lld_mnt_count) > 1) {
369                 unlock_kernel();
370                 EXIT;
371                 return;
372         }
373
374         if (flag == 0 && (++ldd->lld_cwd_count) > 1) {
375                 unlock_kernel();
376                 EXIT;
377                 return;
378         }
379         unlock_kernel();
380
381         handle = (flag) ? &ldd->lld_mnt_och : &ldd->lld_cwd_och;
382         rc = obd_pin(sbi->ll_mdc_exp, inode->i_ino, inode->i_generation,
383                      inode->i_mode & S_IFMT, handle, flag);
384
385         if (rc) {
386                 lock_kernel();
387                 memset(handle, 0, sizeof(*handle));
388                 if (flag == 0)
389                         ldd->lld_cwd_count--;
390                 else
391                         ldd->lld_mnt_count--;
392                 unlock_kernel();
393         }
394
395         EXIT;
396         return;
397 }
398
399 /*static*/ void ll_unpin(struct dentry *de, struct vfsmount *mnt, int flag)
400 {
401         struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
402         struct ll_dentry_data *ldd = ll_d2d(de);
403         struct obd_client_handle handle;
404         int count, rc = 0;
405         ENTRY;
406         LASSERT(ldd);
407
408         lock_kernel();
409         /* Strictly speaking this introduces an additional race: the
410          * increments should wait until the rpc has returned.
411          * However, given that at present the function is void, this
412          * issue is moot. */
413         handle = (flag) ? ldd->lld_mnt_och : ldd->lld_cwd_och;
414         if (handle.och_magic != OBD_CLIENT_HANDLE_MAGIC) {
415                 /* the "pin" failed */
416                 unlock_kernel();
417                 EXIT;
418                 return;
419         }
420
421         if (flag)
422                 count = --ldd->lld_mnt_count;
423         else
424                 count = --ldd->lld_cwd_count;
425         unlock_kernel();
426
427         if (count != 0) {
428                 EXIT;
429                 return;
430         }
431
432         rc = obd_unpin(sbi->ll_mdc_exp, &handle, flag);
433         EXIT;
434         return;
435 }
436
437 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
438 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
439 {
440         int rc;
441         ENTRY;
442
443         if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
444                 rc = ll_revalidate_it(dentry, nd->flags, &nd->intent);
445         else
446                 rc = ll_revalidate_it(dentry, 0, NULL);
447
448         RETURN(rc);
449 }
450 #endif
451
452 struct dentry_operations ll_d_ops = {
453 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
454         .d_revalidate = ll_revalidate_nd,
455 #else
456         .d_revalidate_it = ll_revalidate_it,
457 #endif
458         .d_release = ll_release,
459         .d_delete = ll_ddelete,
460         .d_compare = ll_dcompare,
461 #if 0
462         .d_pin = ll_pin,
463         .d_unpin = ll_unpin,
464 #endif
465 };