1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
23 #include <linux/sched.h>
24 #include <linux/smp_lock.h>
25 #include <linux/quotaops.h>
27 #define DEBUG_SUBSYSTEM S_LLITE
29 #include <linux/obd_support.h>
30 #include <linux/lustre_lite.h>
31 #include <linux/lustre_idl.h>
32 #include <linux/lustre_dlm.h>
34 #include "llite_internal.h"
36 /* should NOT be called with the dcache lock, see fs/dcache.c */
37 static void ll_release(struct dentry *de)
39 struct ll_dentry_data *lld = ll_d2d(de);
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));
49 void ll_set_dd(struct dentry *de)
55 if (de->d_fsdata == NULL) {
56 OBD_ALLOC(de->d_fsdata, sizeof(struct ll_dentry_data));
63 void ll_intent_release(struct lookup_intent *it)
65 struct lustre_handle *handle;
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
73 ldlm_lock_decref(handle, it->it_lock_mode);
75 /* intent_release may be called multiple times, from
76 this thread and we don't want to double-decref this
81 it->it_op_release = 0;
85 void ll_unhash_aliases(struct inode *inode)
87 struct dentry *dentry = NULL;
88 struct list_head *tmp;
89 struct ll_sb_info *sbi;
93 CERROR("unexpected NULL inode, tell phil\n");
97 sbi = ll_i2sbi(inode);
99 CDEBUG(D_INODE, "marking dentries for ino %lx/%x invalid\n",
100 inode->i_ino, inode->i_generation);
102 spin_lock(&dcache_lock);
103 list_for_each(tmp, &inode->i_dentry) {
104 dentry = list_entry(tmp, struct dentry, d_alias);
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);
111 spin_unlock(&dcache_lock);
115 extern struct dentry *ll_find_alias(struct inode *, struct dentry *);
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)
121 struct ll_sb_info *sbi = ll_i2sbi(parent);
126 /* NB 1 request reference will be taken away by ll_intent_lock()
129 if (it_disposition(it, DISP_LOOKUP_NEG))
132 /* ll_intent_lock was successful, now prepare the lustre_md) */
133 rc = mdc_req2lustre_md(request, offset, &sbi->ll_osc_conn, &md);
137 ll_update_inode((*de)->d_inode, md.body, md.lsm);
139 if (md.lsm != NULL && ll_i2info((*de)->d_inode)->lli_smd != md.lsm)
140 obd_free_memmd (&sbi->ll_osc_conn, &md.lsm);
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,
150 int ll_have_md_lock(struct dentry *de)
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;
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;
166 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
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);
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);
183 int ll_revalidate_it(struct dentry *de, int flags, struct lookup_intent *it)
187 CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%s\n", de->d_name.name,
190 /* Cached negative dentries are unsafe for now - look them up again */
191 if (de->d_inode == NULL)
195 * never execute intents for mount points
196 * - attrs will be fixed up in ll_revalidate_inode
198 if (d_mountpoint(de))
202 it->it_op_release = ll_intent_release;
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
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;
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,
220 de->d_flags &= ~DCACHE_LUSTRE_INVALID;
221 if (it && it->it_op == IT_GETATTR) {
222 memcpy(it->it_lock_handle, &lockh,
224 it->it_lock_mode = LCK_PR;
226 ldlm_lock_decref(&lockh, LCK_PR);
230 rc = ldlm_lock_match(obddev->obd_namespace, flags, &res_id,
231 LDLM_PLAIN, NULL, 0, LCK_PW, inode,
234 de->d_flags &= ~DCACHE_LUSTRE_INVALID;
235 if (it && it->it_op == IT_GETATTR) {
236 memcpy(it->it_lock_handle, &lockh,
238 it->it_lock_mode = LCK_PW;
240 ldlm_lock_decref(&lockh, LCK_PW);
244 if (S_ISDIR(de->d_inode->i_mode))
245 ll_invalidate_inode_pages(de->d_inode);
246 ll_unhash_aliases(de->d_inode);
250 rc = ll_intent_lock(de->d_parent->d_inode, &de, it, flags,
251 revalidate_it_finish);
254 CERROR("ll_intent_lock: rc %d : it->it_status %d\n", rc,
257 ll_unhash_aliases(de->d_inode);
260 /* unfortunately ll_intent_lock may cause a callback and revoke our
262 spin_lock(&dcache_lock);
263 hlist_del_init(&de->d_hash);
265 spin_unlock(&dcache_lock);
270 static void ll_pin(struct dentry *de, struct vfsmount *mnt, int flag)
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;
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
285 if (flag == 1 && (++ldd->lld_mnt_count) > 1) {
291 if (flag == 0 && (++ldd->lld_cwd_count) > 1) {
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);
304 memset(handle, 0, sizeof(*handle));
306 ldd->lld_cwd_count--;
308 ldd->lld_mnt_count--;
316 static void ll_unpin(struct dentry *de, struct vfsmount *mnt, int flag)
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;
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
330 handle = (flag) ? ldd->lld_mnt_och : ldd->lld_cwd_och;
331 if (handle.och_magic != OBD_CLIENT_HANDLE_MAGIC) {
332 /* the "pin" failed */
339 count = --ldd->lld_mnt_count;
341 count = --ldd->lld_cwd_count;
349 rc = obd_unpin(&sbi->ll_mdc_conn, &handle, flag);
354 struct dentry_operations ll_d_ops = {
355 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
356 .d_revalidate_nd = ll_revalidate_nd,
358 .d_revalidate_it = ll_revalidate_it,
360 .d_release = ll_release,