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