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 <obd_support.h>
30 #include <lustre_lite.h>
31 #include <lustre/lustre_idl.h>
32 #include <lustre_dlm.h>
33 #include <lustre_mdc.h>
34 //#include <lustre_ver.h>
35 //#include <lustre_version.h>
37 #include "llite_internal.h"
39 /* should NOT be called with the dcache lock, see fs/dcache.c */
40 static void ll_release(struct dentry *de)
42 struct ll_dentry_data *lld;
46 if (lld == NULL) { /* NFS copies the de->d_op methods (bug 4655) */
50 #ifndef HAVE_VFS_INTENT_PATCHES
52 ll_intent_release(lld->lld_it);
53 OBD_FREE(lld->lld_it, sizeof(*lld->lld_it));
56 LASSERT(lld->lld_cwd_count == 0);
57 LASSERT(lld->lld_mnt_count == 0);
58 OBD_FREE(de->d_fsdata, sizeof(*lld));
63 #ifdef DCACHE_LUSTRE_INVALID
64 /* Compare if two dentries are the same. Don't match if the existing dentry
65 * is marked DCACHE_LUSTRE_INVALID. Returns 1 if different, 0 if the same.
67 * This avoids a race where ll_lookup_it() instantiates a dentry, but we get
68 * an AST before calling d_revalidate_it(). The dentry still exists (marked
69 * INVALID) so d_lookup() matches it, but we have no lock on it (so
70 * lock_match() fails) and we spin around real_lookup(). */
71 int ll_dcompare(struct dentry *parent, struct qstr *d_name, struct qstr *name)
73 struct dentry *dchild;
76 if (d_name->len != name->len)
79 if (memcmp(d_name->name, name->name, name->len))
82 /* XXX: d_name must be in-dentry structure */
83 dchild = container_of(d_name, struct dentry, d_name); /* ugh */
84 if (dchild->d_flags & DCACHE_LUSTRE_INVALID) {
85 CDEBUG(D_DENTRY,"INVALID dentry %p not matched, was bug 3784\n",
94 /* should NOT be called with the dcache lock, see fs/dcache.c */
95 static int ll_ddelete(struct dentry *de)
99 #ifndef DCACHE_LUSTRE_INVALID
100 #define DCACHE_LUSTRE_INVALID 0
103 CDEBUG(D_DENTRY, "%s dentry %.*s (%p, parent %p, inode %p) %s%s\n",
104 (de->d_flags & DCACHE_LUSTRE_INVALID ? "deleting" : "keeping"),
105 de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
106 d_unhashed(de) ? "" : "hashed,",
107 list_empty(&de->d_subdirs) ? "" : "subdirs");
108 #if DCACHE_LUSTRE_INVALID == 0
109 #undef DCACHE_LUSTRE_INVALID
115 void ll_set_dd(struct dentry *de)
120 CDEBUG(D_DENTRY, "ldd on dentry %.*s (%p) parent %p inode %p refc %d\n",
121 de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
122 atomic_read(&de->d_count));
124 if (de->d_fsdata == NULL) {
125 OBD_ALLOC(de->d_fsdata, sizeof(struct ll_dentry_data));
132 void ll_intent_drop_lock(struct lookup_intent *it)
134 struct lustre_handle *handle;
136 if (it->it_op && it->d.lustre.it_lock_mode) {
137 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
138 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
139 " from it %p\n", handle->cookie, it);
140 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
142 /* bug 494: intent_release may be called multiple times, from
143 * this thread and we don't want to double-decref this lock */
144 it->d.lustre.it_lock_mode = 0;
148 void ll_intent_release(struct lookup_intent *it)
152 CDEBUG(D_INFO, "intent %p released\n", it);
153 ll_intent_drop_lock(it);
154 #ifdef HAVE_VFS_INTENT_PATCHES
156 it->it_op_release = 0;
158 /* We are still holding extra reference on a request, need to free it */
159 if (it_disposition(it, DISP_ENQ_OPEN_REF))
160 ptlrpc_req_finished(it->d.lustre.it_data); /* ll_file_open */
161 if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
162 ptlrpc_req_finished(it->d.lustre.it_data);
163 if (it_disposition(it, DISP_ENQ_COMPLETE)) /* saved req from revalidate
165 ptlrpc_req_finished(it->d.lustre.it_data);
167 it->d.lustre.it_disposition = 0;
168 it->d.lustre.it_data = NULL;
172 /* Drop dentry if it is not used already, unhash otherwise.
173 Should be called with dcache lock held!
174 Returns: 1 if dentry was dropped, 0 if unhashed. */
175 int ll_drop_dentry(struct dentry *dentry)
178 if (atomic_read(&dentry->d_count) == 0) {
179 CDEBUG(D_DENTRY, "deleting dentry %.*s (%p) parent %p "
180 "inode %p\n", dentry->d_name.len,
181 dentry->d_name.name, dentry, dentry->d_parent,
185 unlock_dentry(dentry);
186 spin_unlock(&dcache_lock);
188 spin_lock(&dcache_lock);
191 /* disconected dentry can not be find without lookup, because we
192 * not need his to unhash or mark invalid. */
193 if (dentry->d_flags & DCACHE_DISCONNECTED) {
194 unlock_dentry(dentry);
198 #ifdef DCACHE_LUSTRE_INVALID
199 if (!(dentry->d_flags & DCACHE_LUSTRE_INVALID)) {
201 if (!d_unhashed(dentry)) {
203 CDEBUG(D_DENTRY, "unhashing dentry %.*s (%p) parent %p "
204 "inode %p refc %d\n", dentry->d_name.len,
205 dentry->d_name.name, dentry, dentry->d_parent,
206 dentry->d_inode, atomic_read(&dentry->d_count));
207 /* actually we don't unhash the dentry, rather just
208 * mark it inaccessible for to __d_lookup(). otherwise
209 * sys_getcwd() could return -ENOENT -bzzz */
210 #ifdef DCACHE_LUSTRE_INVALID
211 dentry->d_flags |= DCACHE_LUSTRE_INVALID;
213 if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
217 unlock_dentry(dentry);
221 void ll_unhash_aliases(struct inode *inode)
223 struct list_head *tmp, *head;
227 CERROR("unexpected NULL inode, tell phil\n");
231 CDEBUG(D_INODE, "marking dentries for ino %lu/%u(%p) invalid\n",
232 inode->i_ino, inode->i_generation, inode);
234 head = &inode->i_dentry;
235 spin_lock(&dcache_lock);
238 while ((tmp = tmp->next) != head) {
239 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
241 CDEBUG(D_DENTRY, "dentry in drop %.*s (%p) parent %p "
242 "inode %p flags %d\n", dentry->d_name.len,
243 dentry->d_name.name, dentry, dentry->d_parent,
244 dentry->d_inode, dentry->d_flags);
246 if (dentry->d_name.len == 1 && dentry->d_name.name[0] == '/') {
247 CERROR("called on root (?) dentry=%p, inode=%p "
248 "ino=%lu\n", dentry, inode, inode->i_ino);
249 lustre_dump_dentry(dentry, 1);
250 libcfs_debug_dumpstack(NULL);
251 } else if (d_mountpoint(dentry)) {
252 /* For mountpoints we skip removal of the dentry
253 which happens solely because we have a lock on it
254 obtained when this dentry was not a mountpoint yet */
255 CDEBUG(D_DENTRY, "Skippind mountpoint dentry removal "
256 "%.*s (%p) parent %p\n",
259 dentry, dentry->d_parent);
264 if (ll_drop_dentry(dentry))
267 spin_unlock(&dcache_lock);
271 int ll_revalidate_it_finish(struct ptlrpc_request *request,
272 struct lookup_intent *it,
281 if (it_disposition(it, DISP_LOOKUP_NEG))
284 rc = ll_prep_inode(&de->d_inode, request, NULL);
289 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
292 LASSERT(dentry != NULL);
294 if (it->d.lustre.it_lock_mode && dentry->d_inode != NULL) {
295 struct inode *inode = dentry->d_inode;
296 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
298 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
299 inode, inode->i_ino, inode->i_generation);
300 md_set_lock_data(sbi->ll_md_exp, &it->d.lustre.it_lock_handle,
304 /* drop lookup or getattr locks immediately */
305 if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
306 /* on 2.6 there are situation when several lookups and
307 * revalidations may be requested during single operation.
308 * therefore, we don't release intent here -bzzz */
309 ll_intent_drop_lock(it);
313 void ll_frob_intent(struct lookup_intent **itp, struct lookup_intent *deft)
315 struct lookup_intent *it = *itp;
316 #ifdef HAVE_VFS_INTENT_PATCHES
318 LASSERTF(it->it_magic == INTENT_MAGIC,
319 "%p has bad intent magic: %x\n",
324 if (!it || it->it_op == IT_GETXATTR)
327 #ifdef HAVE_VFS_INTENT_PATCHES
328 it->it_op_release = ll_intent_release;
332 int ll_revalidate_it(struct dentry *de, int lookup_flags,
333 struct lookup_intent *it)
336 struct md_op_data *op_data;
337 struct ptlrpc_request *req = NULL;
338 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
339 struct obd_export *exp;
340 struct inode *parent;
343 CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%s\n", de->d_name.name,
346 if (de->d_inode == NULL) {
347 /* We can only use negative dentries if this is stat or lookup,
348 for opens and stuff we do need to query server. */
349 /* If there is IT_CREAT in intent op set, then we must throw
350 away this negative dentry and actually do the request to
351 kernel to create whatever needs to be created (if possible)*/
352 if (it && (it->it_op & IT_CREAT))
355 #ifdef DCACHE_LUSTRE_INVALID
356 if (de->d_flags & DCACHE_LUSTRE_INVALID)
360 rc = ll_have_md_lock(de->d_parent->d_inode,
361 MDS_INODELOCK_UPDATE);
365 exp = ll_i2mdexp(de->d_inode);
367 /* Never execute intents for mount points.
368 * Attributes will be fixed up in ll_inode_revalidate_it */
369 if (d_mountpoint(de))
372 /* Root of the lustre tree. Always valid.
373 * Attributes will be fixed up in ll_inode_revalidate_it */
374 if (de == de->d_sb->s_root)
377 OBD_FAIL_TIMEOUT(OBD_FAIL_MDC_REVALIDATE_PAUSE, 5);
378 ll_frob_intent(&it, &lookup_it);
380 parent = de->d_parent->d_inode;
382 op_data = ll_prep_md_op_data(NULL, parent, de->d_inode,
383 de->d_name.name, de->d_name.len,
384 0, LUSTRE_OPC_ANY, NULL);
386 RETURN(PTR_ERR(op_data));
388 if ((it->it_op == IT_OPEN) && de->d_inode) {
389 struct inode *inode = de->d_inode;
390 struct ll_inode_info *lli = ll_i2info(inode);
391 struct obd_client_handle **och_p;
395 * We used to check for MDS_INODELOCK_OPEN here, but in fact
396 * just having LOOKUP lock is enough to justify inode is the
397 * same. And if inode is the same and we have suitable
398 * openhandle, then there is no point in doing another OPEN RPC
399 * just to throw away newly received openhandle. There are no
400 * security implications too, if file owner or access mode is
401 * change, LOOKUP lock is revoked.
405 if (it->it_flags & FMODE_WRITE) {
406 och_p = &lli->lli_mds_write_och;
407 och_usecount = &lli->lli_open_fd_write_count;
408 } else if (it->it_flags & FMODE_EXEC) {
409 och_p = &lli->lli_mds_exec_och;
410 och_usecount = &lli->lli_open_fd_exec_count;
412 och_p = &lli->lli_mds_read_och;
413 och_usecount = &lli->lli_open_fd_read_count;
415 /* Check for the proper lock. */
416 if (!ll_have_md_lock(inode, MDS_INODELOCK_LOOKUP))
418 down(&lli->lli_och_sem);
419 if (*och_p) { /* Everything is open already, do nothing */
420 /*(*och_usecount)++; Do not let them steal our open
421 handle from under us */
422 /* XXX The code above was my original idea, but in case
423 we have the handle, but we cannot use it due to later
424 checks (e.g. O_CREAT|O_EXCL flags set), nobody
425 would decrement counter increased here. So we just
426 hope the lock won't be invalidated in between. But
427 if it would be, we'll reopen the open request to
428 MDS later during file open path */
429 up(&lli->lli_och_sem);
430 ll_finish_md_op_data(op_data);
433 up(&lli->lli_och_sem);
438 it->it_create_mode &= ~current->fs->umask;
439 it->it_flags |= O_CHECK_STALE;
440 rc = md_intent_lock(exp, op_data, NULL, 0, it,
442 &req, ll_md_blocking_ast, 0);
443 it->it_flags &= ~O_CHECK_STALE;
444 ll_finish_md_op_data(op_data);
445 /* If req is NULL, then md_intent_lock only tried to do a lock match;
446 * if all was well, it will return 1 if it found locks, 0 otherwise. */
447 if (req == NULL && rc >= 0) {
455 CDEBUG(D_INFO, "ll_intent_lock: rc %d : it->it_status "
456 "%d\n", rc, it->d.lustre.it_status);
462 rc = ll_revalidate_it_finish(req, it, de);
464 if (rc != -ESTALE && rc != -ENOENT)
465 ll_intent_release(it);
469 if ((it->it_op & IT_OPEN) && de->d_inode &&
470 !S_ISREG(de->d_inode->i_mode) &&
471 !S_ISDIR(de->d_inode->i_mode)) {
472 ll_release_openhandle(de, it);
476 /* unfortunately ll_intent_lock may cause a callback and revoke our
478 spin_lock(&dcache_lock);
482 d_rehash_cond(de, 0);
483 spin_unlock(&dcache_lock);
486 /* We do not free request as it may be reused during following lookup
487 * (see comment in mdc/mdc_locks.c::mdc_intent_lock()), request will
488 * be freed in ll_lookup_it or in ll_intent_release. But if
489 * request was not completed, we need to free it. (bug 5154, 9903) */
490 if (req != NULL && !it_disposition(it, DISP_ENQ_COMPLETE))
491 ptlrpc_req_finished(req);
493 #ifdef DCACHE_LUSTRE_INVALID
494 ll_unhash_aliases(de->d_inode);
495 /* done in ll_unhash_aliases()
496 dentry->d_flags |= DCACHE_LUSTRE_INVALID; */
498 /* We do not want d_invalidate to kill all child dentries too */
502 CDEBUG(D_DENTRY, "revalidated dentry %.*s (%p) parent %p "
503 "inode %p refc %d\n", de->d_name.len,
504 de->d_name.name, de, de->d_parent, de->d_inode,
505 atomic_read(&de->d_count));
506 ll_lookup_finish_locks(it, de);
507 #ifdef DCACHE_LUSTRE_INVALID
509 de->d_flags &= ~DCACHE_LUSTRE_INVALID;
516 * This part is here to combat evil-evil race in real_lookup on 2.6
517 * kernels. The race details are: We enter do_lookup() looking for some
518 * name, there is nothing in dcache for this name yet and d_lookup()
519 * returns NULL. We proceed to real_lookup(), and while we do this,
520 * another process does open on the same file we looking up (most simple
521 * reproducer), open succeeds and the dentry is added. Now back to
522 * us. In real_lookup() we do d_lookup() again and suddenly find the
523 * dentry, so we call d_revalidate on it, but there is no lock, so
524 * without this code we would return 0, but unpatched real_lookup just
525 * returns -ENOENT in such a case instead of retrying the lookup. Once
526 * this is dealt with in real_lookup(), all of this ugly mess can go and
527 * we can just check locks in ->d_revalidate without doing any RPCs
531 if (it != &lookup_it) {
532 /* MDS_INODELOCK_UPDATE needed for IT_GETATTR case. */
533 if (it->it_op == IT_GETATTR)
534 lookup_it.it_op = IT_GETATTR;
535 ll_lookup_finish_locks(it, de);
539 /* Do real lookup here. */
540 op_data = ll_prep_md_op_data(NULL, parent, NULL, de->d_name.name,
541 de->d_name.len, 0, (it->it_op & IT_CREAT ?
543 LUSTRE_OPC_ANY), NULL);
545 RETURN(PTR_ERR(op_data));
547 rc = md_intent_lock(exp, op_data, NULL, 0, it, 0, &req,
548 ll_md_blocking_ast, 0);
550 struct mdt_body *mdt_body;
551 struct lu_fid fid = {.f_seq = 0, .f_oid = 0, .f_ver = 0};
552 mdt_body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
555 fid = *ll_inode2fid(de->d_inode);
557 /* see if we got same inode, if not - return error */
558 if (lu_fid_eq(&fid, &mdt_body->fid1)) {
559 ll_finish_md_op_data(op_data);
561 goto revalidate_finish;
563 ll_intent_release(it);
565 ll_finish_md_op_data(op_data);
569 /*static*/ void ll_pin(struct dentry *de, struct vfsmount *mnt, int flag)
571 struct inode *inode= de->d_inode;
572 struct ll_sb_info *sbi = ll_i2sbi(inode);
573 struct ll_dentry_data *ldd = ll_d2d(de);
574 struct obd_client_handle *handle;
581 /* Strictly speaking this introduces an additional race: the
582 * increments should wait until the rpc has returned.
583 * However, given that at present the function is void, this
585 if (flag == 1 && (++ldd->lld_mnt_count) > 1) {
591 if (flag == 0 && (++ldd->lld_cwd_count) > 1) {
598 handle = (flag) ? &ldd->lld_mnt_och : &ldd->lld_cwd_och;
599 oc = ll_mdscapa_get(inode);
600 rc = obd_pin(sbi->ll_md_exp, ll_inode2fid(inode), oc, handle, flag);
604 memset(handle, 0, sizeof(*handle));
606 ldd->lld_cwd_count--;
608 ldd->lld_mnt_count--;
616 /*static*/ void ll_unpin(struct dentry *de, struct vfsmount *mnt, int flag)
618 struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
619 struct ll_dentry_data *ldd = ll_d2d(de);
620 struct obd_client_handle handle;
626 /* Strictly speaking this introduces an additional race: the
627 * increments should wait until the rpc has returned.
628 * However, given that at present the function is void, this
630 handle = (flag) ? ldd->lld_mnt_och : ldd->lld_cwd_och;
631 if (handle.och_magic != OBD_CLIENT_HANDLE_MAGIC) {
632 /* the "pin" failed */
639 count = --ldd->lld_mnt_count;
641 count = --ldd->lld_cwd_count;
649 rc = obd_unpin(sbi->ll_md_exp, &handle, flag);
654 #ifdef HAVE_VFS_INTENT_PATCHES
655 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
660 if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
661 rc = ll_revalidate_it(dentry, nd->flags, &nd->intent);
663 rc = ll_revalidate_it(dentry, 0, NULL);
668 int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
673 if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
674 struct lookup_intent *it;
675 it = ll_convert_intent(&nd->intent.open, nd->flags);
678 if (it->it_op == (IT_OPEN|IT_CREAT))
679 if (nd->intent.open.flags & O_EXCL) {
680 CDEBUG(D_VFSTRACE, "create O_EXCL, returning 0\n");
685 rc = ll_revalidate_it(dentry, nd->flags, it);
687 if (rc && (nd->flags & LOOKUP_OPEN) &&
688 it_disposition(it, DISP_OPEN_OPEN)) {/*Open*/
689 #ifdef HAVE_FILE_IN_STRUCT_INTENT
690 // XXX Code duplication with ll_lookup_nd
691 if (S_ISFIFO(dentry->d_inode->i_mode)) {
692 // We cannot call open here as it would
695 (struct ptlrpc_request *)
696 it->d.lustre.it_data);
700 nd->intent.open.file->private_data = it;
701 filp = lookup_instantiate_filp(nd, dentry,NULL);
702 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
703 /* 2.6.1[456] have a bug in open_namei() that forgets to check
704 * nd->intent.open.file for error, so we need to return it as lookup's result
711 ll_release_openhandle(dentry, it);
712 #endif /* HAVE_FILE_IN_STRUCT_INTENT */
714 if (!rc && (nd->flags & LOOKUP_CREATE) &&
715 it_disposition(it, DISP_OPEN_CREATE)) {
716 /* We created something but we may only return
717 * negative dentry here, so save request in dentry,
718 * if lookup will be called later on, it will
719 * pick the request, otherwise it would be freed
721 ll_d2d(dentry)->lld_it = it;
722 it = NULL; /* avoid freeing */
727 ll_intent_release(it);
728 OBD_FREE(it, sizeof(*it));
731 rc = ll_revalidate_it(dentry, 0, NULL);
738 struct dentry_operations ll_d_ops = {
739 .d_revalidate = ll_revalidate_nd,
740 .d_release = ll_release,
741 .d_delete = ll_ddelete,
742 #ifdef DCACHE_LUSTRE_INVALID
743 .d_compare = ll_dcompare,