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