Whamcloud - gitweb
Merge b_md into HEAD
[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 /* should NOT be called with the dcache lock, see fs/dcache.c */
35 void ll_release(struct dentry *de)
36 {
37         ENTRY;
38         OBD_FREE(de->d_fsdata, sizeof(struct ll_dentry_data));
39         EXIT;
40 }
41
42 void ll_set_dd(struct dentry *de)
43 {
44         ENTRY;
45         LASSERT(de != NULL);
46
47         lock_kernel();
48
49         if (de->d_fsdata == NULL) {
50                 OBD_ALLOC(de->d_fsdata, sizeof(struct ll_dentry_data));
51                 sema_init(&ll_d2d(de)->lld_it_sem, 1);
52         }
53
54         unlock_kernel();
55
56         EXIT;
57 }
58
59 void ll_intent_release(struct dentry *de, struct lookup_intent *it)
60 {
61         struct lustre_handle *handle;
62         ENTRY;
63
64         LASSERT(ll_d2d(de) != NULL);
65         mdc_put_rpc_lock(&mdc_rpc_lock, it);
66
67         if (it->it_lock_mode) {
68                 handle = (struct lustre_handle *)it->it_lock_handle;
69                 if (it->it_op == IT_SETATTR)
70                         ldlm_lock_decref_and_cancel(handle, it->it_lock_mode);
71                 else
72                         ldlm_lock_decref(handle, it->it_lock_mode);
73
74                 /* intent_release may be called multiple times, from
75                    this thread and we don't want to double-decref this
76                    lock (see bug 494) */
77                 it->it_lock_mode = 0;
78         }
79
80         if (!de->d_it || it->it_op == IT_RELEASED_MAGIC) {
81                 EXIT;
82                 return;
83         }
84
85         if (de->d_it == it)
86                 LL_GET_INTENT(de, it);
87         else 
88                 CERROR("STRANGE intent release: %p %p\n", de->d_it, it);
89
90         EXIT;
91 }
92
93 extern struct dentry *ll_find_alias(struct inode *, struct dentry *);
94
95 static int revalidate2_finish(int flag, struct ptlrpc_request *request,
96                               struct dentry **de, struct lookup_intent *it,
97                               int offset, obd_id ino)
98 {
99         struct mds_body *body;
100         struct lov_mds_md *lmm = NULL;
101         int rc = 0; 
102         ENTRY;
103
104         if (!(flag & LL_LOOKUP_NEGATIVE)) {
105                 body = lustre_msg_buf(request->rq_repmsg, offset);
106                 if (body->valid & OBD_MD_FLEASIZE)
107                         lmm = lustre_msg_buf(request->rq_repmsg, offset + 1);
108                 ll_update_inode((*de)->d_inode, body, lmm);
109                 mdc_lock_set_inode((struct lustre_handle *)it->it_lock_handle,
110                                    (*de)->d_inode);
111         } else 
112                 rc = -ENOENT;
113
114         ptlrpc_req_finished(request);
115         RETURN(rc);
116 }
117
118 int ll_have_md_lock(struct dentry *de)
119 {
120         struct ll_sb_info *sbi = ll_s2sbi(de->d_sb);
121         struct lustre_handle lockh;
122         struct ldlm_res_id res_id = { .name = {0} };
123         struct obd_device *obddev;
124         ENTRY;
125
126         if (!de->d_inode)
127                RETURN(0);
128
129         obddev = class_conn2obd(&sbi->ll_mdc_conn);
130         res_id.name[0] = de->d_inode->i_ino;
131         res_id.name[1] = de->d_inode->i_generation;
132
133         CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
134
135         if (ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
136                             &res_id, LDLM_PLAIN, NULL, 0, LCK_PR, &lockh)) {
137                 ldlm_lock_decref(&lockh, LCK_PR);
138                 RETURN(1);
139         }
140
141         if (ldlm_lock_match(obddev->obd_namespace, LDLM_FL_BLOCK_GRANTED,
142                             &res_id, LDLM_PLAIN, NULL, 0, LCK_PW, &lockh)) {
143                 ldlm_lock_decref(&lockh, LCK_PW);
144                 RETURN(1);
145         }
146         RETURN(0);
147 }
148
149 int ll_revalidate2(struct dentry *de, int flags, struct lookup_intent *it)
150 {
151         int rc;
152         ENTRY;
153
154         /* We don't want to cache negative dentries, so return 0 immediately.
155          * We believe that this is safe, that negative dentries cannot be
156          * pinned by someone else */
157         if (de->d_inode == NULL) {
158                 CDEBUG(D_INODE, "negative dentry: ret 0 to force lookup2\n");
159                 RETURN(0);
160         }
161
162         if (it && it->it_op == IT_TRUNC)
163                 it->it_op = IT_SETATTR;
164
165         if (it == NULL || it->it_op == IT_GETATTR) {
166                 /* We could just return 1 immediately, but since we should only
167                  * be called in revalidate2 if we already have a lock, let's
168                  * verify that. */
169                 struct inode *inode = de->d_inode;
170                 struct ll_sb_info *sbi = ll_i2sbi(inode);
171                 struct obd_device *obddev = class_conn2obd(&sbi->ll_mdc_conn);
172                 struct ldlm_res_id res_id =
173                         { .name = {inode->i_ino, (__u64)inode->i_generation} };
174                 struct lustre_handle lockh;
175                 rc = ldlm_lock_match(obddev->obd_namespace,
176                                      LDLM_FL_BLOCK_GRANTED, &res_id,
177                                      LDLM_PLAIN, NULL, 0, LCK_PR, &lockh);
178                 if (rc) {
179                         de->d_flags &= ~DCACHE_LUSTRE_INVALID;
180                         if (it && it->it_op == IT_GETATTR) {
181                                 memcpy(it->it_lock_handle, &lockh,
182                                        sizeof(lockh));
183                                 it->it_lock_mode = LCK_PR;
184                                 LL_SAVE_INTENT(de, it);
185                         } else {
186                                 ldlm_lock_decref(&lockh, LCK_PR);
187                         }
188                         RETURN(1);
189                 }
190                 rc = ldlm_lock_match(obddev->obd_namespace,
191                                      LDLM_FL_BLOCK_GRANTED, &res_id,
192                                      LDLM_PLAIN, NULL, 0, LCK_PW, &lockh);
193                 if (rc) {
194                         de->d_flags &= ~DCACHE_LUSTRE_INVALID;
195                         if (it && it->it_op == IT_GETATTR) {
196                                 memcpy(it->it_lock_handle, &lockh,
197                                        sizeof(lockh));
198                                 it->it_lock_mode = LCK_PW;
199                                 LL_SAVE_INTENT(de, it);
200                         } else {
201                                 ldlm_lock_decref(&lockh, LCK_PW);
202                         }
203                         RETURN(1);
204                 }
205                 if (S_ISDIR(de->d_inode->i_mode))
206                         ll_invalidate_inode_pages(de->d_inode);
207                 d_unhash_aliases(de->d_inode);
208                 RETURN(0);
209         }
210
211         rc = ll_intent_lock(de->d_parent->d_inode, &de, it, revalidate2_finish);
212         if (rc == -ESTALE)
213                 RETURN(0);
214         if (rc < 0 && it->it_status) {
215                 CERROR("ll_intent_lock: rc %d : it->it_status %d\n", rc,
216                        it->it_status);
217                 RETURN(0);
218         }
219         /* unfortunately ll_intent_lock may cause a callback and revoke our
220            dentry */
221         spin_lock(&dcache_lock);
222         list_del_init(&de->d_hash);
223         spin_unlock(&dcache_lock);
224         d_rehash(de);
225
226         RETURN(1);
227 }
228
229 struct dentry_operations ll_d_ops = {
230         .d_revalidate2 = ll_revalidate2,
231         .d_intent_release = ll_intent_release,
232         .d_release = ll_release,
233 };