Whamcloud - gitweb
merge b_devel into HEAD (20030626 merge tag) for 0.7.1
[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 int ll_delete(struct dentry *de)
43 {
44         if (de->d_it != 0) {
45                 CERROR("%s put dentry %p+%p with d_it %p\n", current->comm,
46                        de, de->d_fsdata, de->d_it);
47                 LBUG();
48         }
49         return 0;
50 }
51
52 void ll_set_dd(struct dentry *de)
53 {
54         ENTRY;
55         LASSERT(de != NULL);
56
57         lock_kernel();
58
59         if (de->d_fsdata == NULL) {
60                 OBD_ALLOC(de->d_fsdata, sizeof(struct ll_dentry_data));
61                 sema_init(&ll_d2d(de)->lld_it_sem, 1);
62         }
63
64         unlock_kernel();
65
66         EXIT;
67 }
68
69 void ll_intent_release(struct dentry *de, struct lookup_intent *it)
70 {
71         struct lustre_handle *handle;
72         ENTRY;
73
74         if (it->it_lock_mode) {
75                 handle = (struct lustre_handle *)it->it_lock_handle;
76                 ldlm_lock_decref(handle, it->it_lock_mode);
77
78                 /* intent_release may be called multiple times, from
79                    this thread and we don't want to double-decref this
80                    lock (see bug 494) */
81                 it->it_lock_mode = 0;
82         }
83
84         if (!de->d_it || it->it_op == IT_RELEASED_MAGIC) {
85                 EXIT;
86                 return;
87         }
88
89         if (de->d_it == it)
90                 LL_GET_INTENT(de, it);
91         else
92                 CDEBUG(D_INODE, "STRANGE intent release: %p %p\n",
93                        de->d_it, it);
94
95         EXIT;
96 }
97
98 extern struct dentry *ll_find_alias(struct inode *, struct dentry *);
99
100 static int revalidate2_finish(int flag, struct ptlrpc_request *request,
101                               struct inode *parent, struct dentry **de,
102                               struct lookup_intent *it, int offset, obd_id ino)
103 {
104         struct ll_sb_info     *sbi = ll_i2sbi(parent);
105         struct mds_body       *body;
106         struct lov_stripe_md  *lsm = NULL;
107         struct lov_mds_md     *lmm;
108         int                    lmmsize;
109         int                    rc = 0;
110         ENTRY;
111
112         /* NB 1 request reference will be taken away by ll_intent_lock()
113          * when I return */
114
115         if ((flag & LL_LOOKUP_NEGATIVE) != 0)
116                 GOTO (out, rc = -ENOENT);
117
118         /* We only get called if the mdc_enqueue() called from
119          * ll_intent_lock() was successful.  Therefore the mds_body is
120          * present and correct, and the eadata is present (but still
121          * opaque, so only obd_unpackmd() can check the size) */
122         body = lustre_msg_buf(request->rq_repmsg, offset, sizeof (*body));
123         LASSERT (body != NULL);
124         LASSERT_REPSWABBED (request, offset);
125
126         if (body->valid & OBD_MD_FLEASIZE) {
127                 /* Only bother with this if inodes's LSM not set? */
128
129                 if (body->eadatasize == 0) {
130                         CERROR ("OBD_MD_FLEASIZE set, but eadatasize 0\n");
131                         GOTO (out, rc = -EPROTO);
132                 }
133                 lmmsize = body->eadatasize;
134                 lmm = lustre_msg_buf (request->rq_repmsg, offset + 1, lmmsize);
135                 LASSERT (lmm != NULL);
136                 LASSERT_REPSWABBED (request, offset + 1);
137
138                 rc = obd_unpackmd (&sbi->ll_osc_conn,
139                                    &lsm, lmm, lmmsize);
140                 if (rc < 0) {
141                         CERROR ("Error %d unpacking eadata\n", rc);
142                         LBUG();
143                         /* XXX don't know if I should do this... */
144                         GOTO (out, rc);
145                         /* or skip the ll_update_inode but still do
146                          * mdc_lock_set_inode() */
147                 }
148                 LASSERT (rc >= sizeof (*lsm));
149                 rc = 0;
150         }
151
152         ll_update_inode((*de)->d_inode, body, lsm);
153
154         if (lsm != NULL &&
155             ll_i2info((*de)->d_inode)->lli_smd != lsm)
156                 obd_free_memmd (&sbi->ll_osc_conn, &lsm);
157
158         ll_mdc_lock_set_inode((struct lustre_handle *)it->it_lock_handle,
159                               (*de)->d_inode);
160  out:
161         RETURN(rc);
162 }
163
164 int ll_have_md_lock(struct dentry *de)
165 {
166         struct ll_sb_info *sbi = ll_s2sbi(de->d_sb);
167         struct lustre_handle lockh;
168         struct ldlm_res_id res_id = { .name = {0} };
169         struct obd_device *obddev;
170         int flags;
171         ENTRY;
172
173         if (!de->d_inode)
174                RETURN(0);
175
176         obddev = class_conn2obd(&sbi->ll_mdc_conn);
177         res_id.name[0] = de->d_inode->i_ino;
178         res_id.name[1] = de->d_inode->i_generation;
179
180         CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
181
182         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_MATCH_DATA;
183         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
184                             NULL, 0, LCK_PR, de->d_inode, &lockh)) {
185                 ldlm_lock_decref(&lockh, LCK_PR);
186                 RETURN(1);
187         }
188
189         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
190                             NULL, 0, LCK_PW, de->d_inode, &lockh)) {
191                 ldlm_lock_decref(&lockh, LCK_PW);
192                 RETURN(1);
193         }
194         RETURN(0);
195 }
196
197 int ll_revalidate2(struct dentry *de, int flags, struct lookup_intent *it)
198 {
199         int rc;
200         ENTRY;
201         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%s\n", de->d_name.name,
202                LL_IT2STR(it));
203
204         /* We don't want to cache negative dentries, so return 0 immediately.
205          * We believe that this is safe, that negative dentries cannot be
206          * pinned by someone else */
207         if (de->d_inode == NULL) {
208                 CDEBUG(D_INODE, "negative dentry: ret 0 to force lookup2\n");
209                 RETURN(0);
210         }
211
212         if (it == NULL || it->it_op == IT_GETATTR) {
213                 /* We could just return 1 immediately, but since we should only
214                  * be called in revalidate2 if we already have a lock, let's
215                  * verify that. */
216                 struct inode *inode = de->d_inode;
217                 struct ll_sb_info *sbi = ll_i2sbi(inode);
218                 struct obd_device *obddev = class_conn2obd(&sbi->ll_mdc_conn);
219                 struct ldlm_res_id res_id =
220                         { .name = {inode->i_ino, (__u64)inode->i_generation} };
221                 struct lustre_handle lockh;
222                 int flags;
223                 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_MATCH_DATA;
224                 rc = ldlm_lock_match(obddev->obd_namespace, flags, &res_id,
225                                      LDLM_PLAIN, NULL, 0, LCK_PR, inode,
226                                      &lockh);
227                 if (rc) {
228                         de->d_flags &= ~DCACHE_LUSTRE_INVALID;
229                         if (it && it->it_op == IT_GETATTR) {
230                                 memcpy(it->it_lock_handle, &lockh,
231                                        sizeof(lockh));
232                                 it->it_lock_mode = LCK_PR;
233                                 LL_SAVE_INTENT(de, it);
234                         } else {
235                                 ldlm_lock_decref(&lockh, LCK_PR);
236                         }
237                         RETURN(1);
238                 }
239                 rc = ldlm_lock_match(obddev->obd_namespace, flags, &res_id,
240                                      LDLM_PLAIN, NULL, 0, LCK_PW, inode,
241                                      &lockh);
242                 if (rc) {
243                         de->d_flags &= ~DCACHE_LUSTRE_INVALID;
244                         if (it && it->it_op == IT_GETATTR) {
245                                 memcpy(it->it_lock_handle, &lockh,
246                                        sizeof(lockh));
247                                 it->it_lock_mode = LCK_PW;
248                                 LL_SAVE_INTENT(de, it);
249                         } else {
250                                 ldlm_lock_decref(&lockh, LCK_PW);
251                         }
252                         RETURN(1);
253                 }
254                 if (S_ISDIR(de->d_inode->i_mode))
255                         ll_invalidate_inode_pages(de->d_inode);
256                 d_unhash_aliases(de->d_inode);
257                 RETURN(0);
258         }
259
260         rc = ll_intent_lock(de->d_parent->d_inode, &de, it, revalidate2_finish);
261         if (rc < 0) {
262                 CERROR("ll_intent_lock: rc %d : it->it_status %d\n", rc,
263                        it->it_status);
264                 RETURN(0);
265         }
266         /* unfortunately ll_intent_lock may cause a callback and revoke our
267            dentry */
268         spin_lock(&dcache_lock);
269         list_del_init(&de->d_hash);
270         spin_unlock(&dcache_lock);
271         d_rehash(de);
272
273         RETURN(1);
274 }
275
276 struct dentry_operations ll_d_ops = {
277         .d_revalidate2 = ll_revalidate2,
278         .d_intent_release = ll_intent_release,
279         .d_release = ll_release,
280         .d_delete = ll_delete,
281 };