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>
33 #include <linux/lustre_version.h>
35 #include "llite_internal.h"
37 /* should NOT be called with the dcache lock, see fs/dcache.c */
38 static void ll_release(struct dentry *de)
40 struct ll_dentry_data *lld;
45 LASSERT(lld->lld_cwd_count == 0);
46 LASSERT(lld->lld_mnt_count == 0);
47 OBD_FREE(de->d_fsdata, sizeof(struct ll_dentry_data));
52 /* should NOT be called with the dcache lock, see fs/dcache.c */
53 static int ll_ddelete(struct dentry *de)
57 CDEBUG(D_DENTRY, "%s dentry %*s (%p, parent %p, inode %p) %s%s\n",
58 (de->d_flags & DCACHE_LUSTRE_INVALID ? "keeping" : "deleting"),
59 de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
60 d_unhashed(de) ? "" : "hashed,",
61 list_empty(&de->d_subdirs) ? "" : "subdirs");
65 void ll_set_dd(struct dentry *de)
70 CDEBUG(D_DENTRY, "ldd on dentry %*s (%p) parent %p inode %p refc %d\n",
71 de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
72 atomic_read(&de->d_count));
74 if (de->d_fsdata == NULL) {
75 OBD_ALLOC(de->d_fsdata, sizeof(struct ll_dentry_data));
82 void ll_intent_drop_lock(struct lookup_intent *it)
84 struct lustre_handle *handle;
86 if (it->it_op && it->d.lustre.it_lock_mode) {
87 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
88 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
89 " from it %p\n", handle->cookie, it);
90 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
92 /* bug 494: intent_release may be called multiple times, from
93 * this thread and we don't want to double-decref this lock */
94 it->d.lustre.it_lock_mode = 0;
98 void ll_intent_release(struct lookup_intent *it)
102 ll_intent_drop_lock(it);
104 it->it_op_release = 0;
105 it->d.lustre.it_disposition = 0;
106 it->d.lustre.it_data = NULL;
110 void ll_unhash_aliases(struct inode *inode)
112 struct list_head *tmp, *head;
113 struct ll_sb_info *sbi;
117 CERROR("unexpected NULL inode, tell phil\n");
121 CDEBUG(D_INODE, "marking dentries for ino %lu/%u(%p) invalid\n",
122 inode->i_ino, inode->i_generation, inode);
124 sbi = ll_i2sbi(inode);
125 head = &inode->i_dentry;
127 spin_lock(&dcache_lock);
129 while ((tmp = tmp->next) != head) {
130 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
131 if (atomic_read(&dentry->d_count) == 0) {
132 CDEBUG(D_DENTRY, "deleting dentry %*s (%p) parent %p "
133 "inode %p\n", dentry->d_name.len,
134 dentry->d_name.name, dentry, dentry->d_parent,
138 spin_unlock(&dcache_lock);
141 } else if (!(dentry->d_flags & DCACHE_LUSTRE_INVALID)) {
142 CDEBUG(D_DENTRY, "unhashing dentry %*s (%p) parent %p "
143 "inode %p refc %d\n", dentry->d_name.len,
144 dentry->d_name.name, dentry, dentry->d_parent,
145 dentry->d_inode, atomic_read(&dentry->d_count));
146 hlist_del_init(&dentry->d_hash);
147 dentry->d_flags |= DCACHE_LUSTRE_INVALID;
148 hlist_add_head(&dentry->d_hash,
149 &sbi->ll_orphan_dentry_list);
152 spin_unlock(&dcache_lock);
156 extern struct dentry *ll_find_alias(struct inode *, struct dentry *);
158 static int revalidate_it_finish(struct ptlrpc_request *request,
159 int offset, struct lookup_intent *it,
162 struct ll_sb_info *sbi;
169 if (it_disposition(it, DISP_LOOKUP_NEG))
172 sbi = ll_i2sbi(de->d_inode);
173 rc = ll_prep_inode(sbi->ll_osc_exp, sbi->ll_mdc_exp,
174 &de->d_inode, request, offset, NULL);
179 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
182 LASSERT(dentry != NULL);
184 if (it->d.lustre.it_lock_mode && dentry->d_inode != NULL) {
185 struct inode *inode = dentry->d_inode;
186 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
187 inode, inode->i_ino, inode->i_generation);
188 mdc_set_lock_data(NULL, &it->d.lustre.it_lock_handle, inode);
191 /* drop lookup or getattr locks immediately */
192 if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
193 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
194 /* on 2.6 there are situation when several lookups and
195 * revalidations may be requested during single operation.
196 * therefore, we don't release intent here -bzzz */
197 ll_intent_drop_lock(it);
199 ll_intent_release(it);
204 void ll_frob_intent(struct lookup_intent **itp, struct lookup_intent *deft)
206 struct lookup_intent *it = *itp;
207 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
208 if (it && it->it_magic != INTENT_MAGIC) {
209 CERROR("WARNING: uninitialized intent\n");
212 if (it && (it->it_op == IT_GETATTR || it->it_op == 0))
213 it->it_op = IT_LOOKUP;
216 if (!it || it->it_op == IT_GETXATTR)
219 it->it_op_release = ll_intent_release;
222 int ll_revalidate_it(struct dentry *de, int flags, struct nameidata *nd,
223 struct lookup_intent *it)
226 struct ll_fid pfid, cfid;
227 struct it_cb_data icbd;
228 struct ll_uctxt ctxt;
229 struct ptlrpc_request *req = NULL;
230 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
231 struct obd_export *exp;
234 CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%s\n", de->d_name.name,
237 /* Cached negative dentries are unsafe for now - look them up again */
238 if (de->d_inode == NULL)
241 CDEBUG(D_INODE, "revalidate 0x%p: %*s -> %lu/%lu\n",
242 de, de->d_name.len, de->d_name.name,
243 (unsigned long) de->d_inode->i_ino,
244 (unsigned long) de->d_inode->i_generation);
246 exp = ll_i2mdcexp(de->d_inode);
247 ll_inode2fid(&pfid, de->d_parent->d_inode);
248 ll_inode2fid(&cfid, de->d_inode);
249 icbd.icbd_parent = de->d_parent->d_inode;
250 icbd.icbd_childp = &de;
252 /* Never execute intents for mount points.
253 * Attributes will be fixed up in ll_inode_revalidate_it */
254 if (d_mountpoint(de))
258 nd->mnt->mnt_last_used = jiffies;
260 ll_frob_intent(&it, &lookup_it);
263 ll_i2uctxt(&ctxt, de->d_parent->d_inode, de->d_inode);
265 if (it->it_op == IT_GETATTR) { /* We need to check for LOOKUP lock
267 rc = md_intent_lock(exp, &ctxt, &pfid, de->d_name.name,
268 de->d_name.len, NULL, 0, &cfid, &lookup_it,
269 flags, &req, ll_mdc_blocking_ast);
270 /* If there was no lookup lock, no point in even checking for
276 if (it_disposition(&lookup_it, DISP_LOOKUP_NEG)) {
277 ll_intent_release(&lookup_it);
283 ptlrpc_req_finished(req);
285 ll_lookup_finish_locks(&lookup_it, de);
288 rc = md_intent_lock(exp, &ctxt, &pfid, de->d_name.name, de->d_name.len,
289 NULL, 0, &cfid, it, flags, &req,
290 ll_mdc_blocking_ast);
291 /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
292 * if all was well, it will return 1 if it found locks, 0 otherwise. */
293 if (req == NULL && rc >= 0)
298 CDEBUG(D_INFO, "ll_intent_lock: rc %d : it->it_status "
299 "%d\n", rc, it->d.lustre.it_status);
304 rc = revalidate_it_finish(req, 1, it, de);
306 ll_intent_release(it);
311 /* unfortunately ll_intent_lock may cause a callback and revoke our
313 spin_lock(&dcache_lock);
314 hlist_del_init(&de->d_hash);
316 spin_unlock(&dcache_lock);
320 if (req != NULL && rc == 1)
321 ptlrpc_req_finished(req);
323 ll_unhash_aliases(de->d_inode);
327 CDEBUG(D_DENTRY, "revalidated dentry %*s (%p) parent %p "
328 "inode %p refc %d\n", de->d_name.len,
329 de->d_name.name, de, de->d_parent, de->d_inode,
330 atomic_read(&de->d_count));
331 ll_lookup_finish_locks(it, de);
332 de->d_flags &= ~DCACHE_LUSTRE_INVALID;
333 if (!((de->d_inode->i_mode & S_ISUID) &&S_ISDIR(de->d_inode->i_mode)) ||
334 !(flags & LOOKUP_CONTINUE || (it->it_op & (IT_CHDIR | IT_OPEN))))
338 (void)ll_dir_process_mount_object(de, nd->mnt);
342 /*static*/ void ll_pin(struct dentry *de, struct vfsmount *mnt, int flag)
344 struct inode *inode= de->d_inode;
345 struct ll_sb_info *sbi = ll_i2sbi(inode);
346 struct ll_dentry_data *ldd = ll_d2d(de);
347 struct obd_client_handle *handle;
353 /* Strictly speaking this introduces an additional race: the
354 * increments should wait until the rpc has returned.
355 * However, given that at present the function is void, this
357 if (flag == 1 && (++ldd->lld_mnt_count) > 1) {
363 if (flag == 0 && (++ldd->lld_cwd_count) > 1) {
370 handle = (flag) ? &ldd->lld_mnt_och : &ldd->lld_cwd_och;
371 rc = obd_pin(sbi->ll_mdc_exp, inode->i_ino, inode->i_generation,
372 inode->i_mode & S_IFMT, handle, flag);
376 memset(handle, 0, sizeof(*handle));
378 ldd->lld_cwd_count--;
380 ldd->lld_mnt_count--;
388 /*static*/ void ll_unpin(struct dentry *de, struct vfsmount *mnt, int flag)
390 struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
391 struct ll_dentry_data *ldd = ll_d2d(de);
392 struct obd_client_handle handle;
398 /* Strictly speaking this introduces an additional race: the
399 * increments should wait until the rpc has returned.
400 * However, given that at present the function is void, this
402 handle = (flag) ? ldd->lld_mnt_och : ldd->lld_cwd_och;
403 if (handle.och_magic != OBD_CLIENT_HANDLE_MAGIC) {
404 /* the "pin" failed */
411 count = --ldd->lld_mnt_count;
413 count = --ldd->lld_cwd_count;
421 rc = obd_unpin(sbi->ll_mdc_exp, &handle, flag);
426 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
427 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
432 if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
433 rc = ll_revalidate_it(dentry, nd->flags, nd, &nd->intent);
435 rc = ll_revalidate_it(dentry, 0, nd, NULL);
441 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
442 static void ll_dentry_iput(struct dentry *dentry, struct inode *inode)
444 struct ll_sb_info *sbi = ll_i2sbi(inode);
445 struct ll_fid parent, child;
447 LASSERT(dentry->d_parent && dentry->d_parent->d_inode);
448 ll_inode2fid(&parent, dentry->d_parent->d_inode);
449 ll_inode2fid(&child, inode);
450 md_change_cbdata_name(sbi->ll_mdc_exp, &parent,
451 (char *)dentry->d_name.name,
452 dentry->d_name.len, &child,
453 null_if_equal, inode);
457 #error "implement ->d_iput() for 2.6"
460 struct dentry_operations ll_d_ops = {
461 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
462 .d_revalidate = ll_revalidate_nd,
464 .d_revalidate_it = ll_revalidate_it,
466 .d_release = ll_release,
467 .d_iput = ll_dentry_iput,
468 .d_delete = ll_ddelete,