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