Whamcloud - gitweb
merge b_devel into HEAD, which will become 0.7.3
[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 #include "llite_internal.h"
35
36 /* should NOT be called with the dcache lock, see fs/dcache.c */
37 static void ll_release(struct dentry *de)
38 {
39         struct ll_dentry_data *lld = ll_d2d(de);
40         ENTRY;
41
42         LASSERT(lld->lld_cwd_count == 0);
43         LASSERT(lld->lld_mnt_count == 0);
44         OBD_FREE(de->d_fsdata, sizeof(struct ll_dentry_data));
45
46         EXIT;
47 }
48
49 void ll_set_dd(struct dentry *de)
50 {
51         ENTRY;
52         LASSERT(de != NULL);
53
54         lock_kernel();
55         if (de->d_fsdata == NULL) {
56                 OBD_ALLOC(de->d_fsdata, sizeof(struct ll_dentry_data));
57         }
58         unlock_kernel();
59
60         EXIT;
61 }
62
63 void ll_intent_release(struct lookup_intent *it)
64 {
65         struct lustre_handle *handle;
66         ENTRY;
67
68         if (it->it_op && it->it_lock_mode) {
69                 handle = (struct lustre_handle *)it->it_lock_handle;
70                 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
71                        " from it %p\n",
72                        handle->cookie, it);
73                 ldlm_lock_decref(handle, it->it_lock_mode);
74
75                 /* intent_release may be called multiple times, from
76                    this thread and we don't want to double-decref this
77                    lock (see bug 494) */
78                 it->it_lock_mode = 0;
79         }
80         it->it_magic = 0;
81         it->it_op_release = 0;
82         EXIT;
83 }
84
85 void ll_unhash_aliases(struct inode *inode)
86 {
87         struct dentry *dentry = NULL;
88         struct list_head *tmp;
89         struct ll_sb_info *sbi;
90         ENTRY;
91
92         if (inode == NULL) {
93                 CERROR("unexpected NULL inode, tell phil\n");
94                 return;
95         }
96
97         sbi = ll_i2sbi(inode);
98
99         CDEBUG(D_INODE, "marking dentries for ino %lx/%x invalid\n",
100                inode->i_ino, inode->i_generation);
101
102         spin_lock(&dcache_lock);
103         list_for_each(tmp, &inode->i_dentry) {
104                 dentry = list_entry(tmp, struct dentry, d_alias);
105
106                 list_del_init(&dentry->d_hash);
107                 dentry->d_flags |= DCACHE_LUSTRE_INVALID;
108                 list_add(&dentry->d_hash, &sbi->ll_orphan_dentry_list);
109         }
110
111         spin_unlock(&dcache_lock);
112         EXIT;
113 }
114
115 extern struct dentry *ll_find_alias(struct inode *, struct dentry *);
116
117 static int revalidate_it_finish(struct ptlrpc_request *request,
118                               struct inode *parent, struct dentry **de,
119                               struct lookup_intent *it, int offset, obd_id ino)
120 {
121         struct ll_sb_info     *sbi = ll_i2sbi(parent);
122         struct lustre_md      md;
123         int                    rc = 0;
124         ENTRY;
125
126         /* NB 1 request reference will be taken away by ll_intent_lock()
127          * when I return */
128
129         if (it_disposition(it, DISP_LOOKUP_NEG))
130                 RETURN(-ENOENT);
131
132         /* ll_intent_lock was successful, now prepare the lustre_md) */
133         rc = mdc_req2lustre_md(request, offset, &sbi->ll_osc_conn, &md);
134         if (rc)
135                 RETURN(rc);
136
137         ll_update_inode((*de)->d_inode, md.body, md.lsm);
138
139         if (md.lsm != NULL && ll_i2info((*de)->d_inode)->lli_smd != md.lsm)
140                 obd_free_memmd (&sbi->ll_osc_conn, &md.lsm);
141
142         CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
143                (*de)->d_inode, (*de)->d_inode->i_ino,
144                (*de)->d_inode->i_generation);
145         ldlm_lock_set_data((struct lustre_handle *)it->it_lock_handle,
146                            (*de)->d_inode);
147         RETURN(rc);
148 }
149
150 int ll_have_md_lock(struct dentry *de)
151 {
152         struct ll_sb_info *sbi = ll_s2sbi(de->d_sb);
153         struct lustre_handle lockh;
154         struct ldlm_res_id res_id = { .name = {0} };
155         struct obd_device *obddev;
156         int flags;
157         ENTRY;
158
159         if (!de->d_inode)
160                RETURN(0);
161
162         obddev = class_conn2obd(&sbi->ll_mdc_conn);
163         res_id.name[0] = de->d_inode->i_ino;
164         res_id.name[1] = de->d_inode->i_generation;
165
166         CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
167
168         flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_MATCH_DATA;
169         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
170                             NULL, 0, LCK_PR, de->d_inode, &lockh)) {
171                 ldlm_lock_decref(&lockh, LCK_PR);
172                 RETURN(1);
173         }
174
175         if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
176                             NULL, 0, LCK_PW, de->d_inode, &lockh)) {
177                 ldlm_lock_decref(&lockh, LCK_PW);
178                 RETURN(1);
179         }
180         RETURN(0);
181 }
182
183 int ll_revalidate_it(struct dentry *de, int flags, struct lookup_intent *it)
184 {
185         int rc;
186         ENTRY;
187         CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%s\n", de->d_name.name,
188                LL_IT2STR(it));
189
190         /* Cached negative dentries are unsafe for now - look them up again */
191         if (de->d_inode == NULL)
192                 RETURN(0);
193
194         /* 
195          * never execute intents for mount points
196          * - attrs will be fixed up in ll_revalidate_inode
197          */
198         if (d_mountpoint(de))
199                 RETURN(1);
200
201         if (it)
202                 it->it_op_release = ll_intent_release;
203
204         if (it == NULL || it->it_op == IT_GETATTR) {
205                 /* We could just return 1 immediately, but since we should only
206                  * be called in revalidate2 if we already have a lock, let's
207                  * verify that. */
208                 struct inode *inode = de->d_inode;
209                 struct ll_sb_info *sbi = ll_i2sbi(inode);
210                 struct obd_device *obddev = class_conn2obd(&sbi->ll_mdc_conn);
211                 struct ldlm_res_id res_id =
212                         { .name = {inode->i_ino, (__u64)inode->i_generation} };
213                 struct lustre_handle lockh;
214                 int flags;
215                 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_MATCH_DATA;
216                 rc = ldlm_lock_match(obddev->obd_namespace, flags, &res_id,
217                                      LDLM_PLAIN, NULL, 0, LCK_PR, inode,
218                                      &lockh);
219                 if (rc) {
220                         de->d_flags &= ~DCACHE_LUSTRE_INVALID;
221                         if (it && it->it_op == IT_GETATTR) {
222                                 memcpy(it->it_lock_handle, &lockh,
223                                        sizeof(lockh));
224                                 it->it_lock_mode = LCK_PR;
225                         } else {
226                                 ldlm_lock_decref(&lockh, LCK_PR);
227                         }
228                         RETURN(1);
229                 }
230                 rc = ldlm_lock_match(obddev->obd_namespace, flags, &res_id,
231                                      LDLM_PLAIN, NULL, 0, LCK_PW, inode,
232                                      &lockh);
233                 if (rc) {
234                         de->d_flags &= ~DCACHE_LUSTRE_INVALID;
235                         if (it && it->it_op == IT_GETATTR) {
236                                 memcpy(it->it_lock_handle, &lockh,
237                                        sizeof(lockh));
238                                 it->it_lock_mode = LCK_PW;
239                         } else {
240                                 ldlm_lock_decref(&lockh, LCK_PW);
241                         }
242                         RETURN(1);
243                 }
244                 if (S_ISDIR(de->d_inode->i_mode))
245                         ll_invalidate_inode_pages(de->d_inode);
246                 ll_unhash_aliases(de->d_inode);
247                 RETURN(0);
248         }
249
250         rc = ll_intent_lock(de->d_parent->d_inode, &de, it, flags,
251                             revalidate_it_finish);
252         if (rc < 0) {
253                 if (rc != -ESTALE) {
254                         CERROR("ll_intent_lock: rc %d : it->it_status %d\n", rc,
255                                it->it_status);
256                 }
257                 ll_unhash_aliases(de->d_inode);
258                 RETURN(0);
259         }
260         /* unfortunately ll_intent_lock may cause a callback and revoke our
261            dentry */
262         spin_lock(&dcache_lock);
263         hlist_del_init(&de->d_hash);
264         __d_rehash(de, 0);
265         spin_unlock(&dcache_lock);
266
267         RETURN(1);
268 }
269
270 static void ll_pin(struct dentry *de, struct vfsmount *mnt, int flag)
271 {
272         struct inode *inode= de->d_inode;
273         struct ll_sb_info *sbi = ll_i2sbi(inode);
274         struct ll_dentry_data *ldd = ll_d2d(de);
275         struct obd_client_handle *handle;
276         int rc = 0;
277         ENTRY;
278         LASSERT(ldd);
279
280         lock_kernel();
281         /* Strictly speaking this introduces an additional race: the
282          * increments should wait until the rpc has returned.
283          * However, given that at present the function is void, this
284          * issue is moot. */
285         if (flag == 1 && (++ldd->lld_mnt_count) > 1) {
286                 unlock_kernel();
287                 EXIT;
288                 return;
289         }
290
291         if (flag == 0 && (++ldd->lld_cwd_count) > 1) {
292                 unlock_kernel();
293                 EXIT;
294                 return;
295         }
296         unlock_kernel();
297
298         handle = (flag) ? &ldd->lld_mnt_och : &ldd->lld_cwd_och;
299         rc = obd_pin(&sbi->ll_mdc_conn, inode->i_ino, inode->i_generation,
300                      inode->i_mode & S_IFMT, handle, flag);
301
302         if (rc) {
303                 lock_kernel();
304                 memset(handle, 0, sizeof(*handle));
305                 if (flag == 0)
306                         ldd->lld_cwd_count--;
307                 else
308                         ldd->lld_mnt_count--;
309                 unlock_kernel();
310         }
311
312         EXIT;
313         return;
314 }
315
316 static void ll_unpin(struct dentry *de, struct vfsmount *mnt, int flag)
317 {
318         struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
319         struct ll_dentry_data *ldd = ll_d2d(de);
320         struct obd_client_handle handle;
321         int count, rc = 0;
322         ENTRY;
323         LASSERT(ldd);
324
325         lock_kernel();
326         /* Strictly speaking this introduces an additional race: the
327          * increments should wait until the rpc has returned.
328          * However, given that at present the function is void, this
329          * issue is moot. */
330         handle = (flag) ? ldd->lld_mnt_och : ldd->lld_cwd_och;
331         if (handle.och_magic != OBD_CLIENT_HANDLE_MAGIC) {
332                 /* the "pin" failed */
333                 unlock_kernel();
334                 EXIT;
335                 return;
336         }
337
338         if (flag)
339                 count = --ldd->lld_mnt_count;
340         else
341                 count = --ldd->lld_cwd_count;
342         unlock_kernel();
343
344         if (count != 0) {
345                 EXIT;
346                 return;
347         }
348
349         rc = obd_unpin(&sbi->ll_mdc_conn, &handle, flag);
350         EXIT;
351         return;
352 }
353
354 struct dentry_operations ll_d_ops = {
355 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
356         .d_revalidate_nd = ll_revalidate_nd,
357 #else
358         .d_revalidate_it = ll_revalidate_it,
359 #endif
360         .d_release = ll_release,
361 #if 0
362         .d_pin = ll_pin,
363         .d_unpin = ll_unpin,
364 #endif
365 };