Whamcloud - gitweb
small fixups to revalidate2 lock matching
[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, 2002 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 extern struct address_space_operations ll_aops;
35
36 void ll_release(struct dentry *de)
37 {
38         ENTRY;
39
40         OBD_FREE(de->d_fsdata, sizeof(struct ll_dentry_data));
41         EXIT;
42 }
43
44 extern void d_delete_aliases(struct inode *);
45 void ll_intent_release(struct dentry *de, struct lookup_intent *it)
46 {
47         struct lustre_handle *handle;
48         ENTRY;
49
50         /* XXX the check for RENAME2 is a workaround for old kernels 
51            which call intent_release twice in rename 
52         */
53         if (it == NULL || it->it_op == IT_RENAME2) {
54                 EXIT;
55                 return;
56         }
57
58         LASSERT(ll_d2d(de) != NULL);
59
60         if (it->it_lock_mode) {
61                 handle = (struct lustre_handle *)it->it_lock_handle;
62                 if (it->it_op == IT_SETATTR) {
63                         int rc;
64                         ldlm_lock_decref(handle, it->it_lock_mode);
65                         rc = ldlm_cli_cancel(handle);
66                         if (rc < 0)
67                                 CERROR("ldlm_cli_cancel: %d\n", rc);
68                 } else
69                         ldlm_lock_decref(handle, it->it_lock_mode);
70         }
71
72         if (it->it_op == IT_RELEASED_MAGIC) {
73                 EXIT; 
74                 return;
75         }
76
77         if (de->d_it && de->d_it == it) { 
78                 de->d_it = NULL;
79                 up(&ll_d2d(de)->lld_it_sem);
80                 it->it_op = IT_RELEASED_MAGIC;
81         }
82
83         EXIT;
84 }
85
86 extern struct dentry *ll_find_alias(struct inode *, struct dentry *);
87
88 static int revalidate2_finish(int flag, struct ptlrpc_request *request, 
89                           struct dentry **de,
90                           struct lookup_intent *it, 
91                           int offset, obd_id ino)
92 {
93         ldlm_lock_set_data((struct lustre_handle *)it->it_lock_handle,
94                            (*de)->d_inode, sizeof(*((*de)->d_inode)));
95         ptlrpc_req_finished(request);
96         return 0;
97 }
98
99 static int ll_have_lock(struct dentry *de)
100 {
101         struct ll_sb_info *sbi = ll_s2sbi(de->d_sb);
102         struct lustre_handle lockh;
103         __u64 res_id[RES_NAME_SIZE] = {0};
104         struct obd_device *obddev;
105         ENTRY;
106
107         if (!de->d_inode)
108                RETURN(0);
109
110         obddev = class_conn2obd(&sbi->ll_mdc_conn);
111         res_id[0] = de->d_inode->i_ino;
112         res_id[1] = de->d_inode->i_generation;
113
114         CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id[0]);
115
116         if (ldlm_lock_match(obddev->obd_namespace, res_id, LDLM_PLAIN,
117                             NULL, 0, LCK_PR, &lockh)) {
118                 ldlm_lock_decref(&lockh, LCK_PR);
119                 RETURN(1);
120         }
121
122         if (ldlm_lock_match(obddev->obd_namespace, res_id, LDLM_PLAIN,
123                             NULL, 0, LCK_PW, &lockh)) {
124                 ldlm_lock_decref(&lockh, LCK_PW);
125                 RETURN(1);
126         }
127         RETURN(0);
128 }
129
130 int ll_revalidate2(struct dentry *de, int flags, struct lookup_intent *it)
131 {
132         int rc;
133         ENTRY;
134
135         /* We don't want to cache negative dentries, so return 0 immediately.
136          * We believe that this is safe, that negative dentries cannot be
137          * pinned by someone else */
138         if (de->d_inode == NULL) {
139                 CDEBUG(D_INODE, "negative dentry: ret 0 to force lookup2\n");
140                 RETURN(0);
141         }
142
143         if (it == NULL && ll_have_lock(de))
144                 GOTO(out, rc = 0);
145
146         rc = ll_intent_lock(de->d_parent->d_inode, &de, it, revalidate2_finish);
147         if (rc < 0) {
148                 /* Something bad happened; overwrite it_status? */
149                 CERROR("ll_intent_lock: %d\n", rc);
150         }
151         /* unfortunately ll_intent_lock may cause a callback and revoke our 
152            dentry */
153         spin_lock(&dcache_lock);
154         list_del_init(&de->d_hash);
155         spin_unlock(&dcache_lock);
156         d_rehash(de);
157
158  out:
159         if (!it)
160                 de->d_it = NULL;
161
162         RETURN(1);
163 }
164
165 int ll_set_dd(struct dentry *de)
166 {
167         ENTRY;
168         LASSERT(de != NULL);
169
170         lock_kernel();
171
172         if (de->d_fsdata != NULL) {
173                 CERROR("dentry %p already has d_fsdata set\n", de);
174         } else {
175                 OBD_ALLOC(de->d_fsdata, sizeof(struct ll_dentry_data));
176                 sema_init(&ll_d2d(de)->lld_it_sem, 1);
177         }
178
179         unlock_kernel();
180
181         RETURN(0);
182 }
183
184 struct dentry_operations ll_d_ops = {
185         .d_revalidate2 = ll_revalidate2,
186         .d_intent_release = ll_intent_release,
187         .d_release = ll_release,
188 };