1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 #include <linux/sched.h>
39 #include <linux/smp_lock.h>
40 #include <linux/quotaops.h>
42 #define DEBUG_SUBSYSTEM S_LLITE
44 #include <obd_support.h>
45 #include <lustre_lite.h>
46 #include <lustre/lustre_idl.h>
47 #include <lustre_dlm.h>
48 #include <linux/lustre_version.h>
50 #include "llite_internal.h"
52 spinlock_t ll_lookup_lock = SPIN_LOCK_UNLOCKED;
54 /* should NOT be called with the dcache lock, see fs/dcache.c */
55 static void ll_release(struct dentry *de)
57 struct ll_dentry_data *lld;
61 if (lld == NULL) { /* NFS copies the de->d_op methods (bug 4655) */
65 #ifndef HAVE_VFS_INTENT_PATCHES
67 ll_intent_release(lld->lld_it);
68 OBD_FREE(lld->lld_it, sizeof(*lld->lld_it));
71 LASSERT(lld->lld_cwd_count == 0);
72 LASSERT(lld->lld_mnt_count == 0);
73 OBD_FREE(de->d_fsdata, sizeof(*lld));
78 #ifdef DCACHE_LUSTRE_INVALID
79 /* Compare if two dentries are the same. Don't match if the existing dentry
80 * is marked DCACHE_LUSTRE_INVALID. Returns 1 if different, 0 if the same.
82 * This avoids a race where ll_lookup_it() instantiates a dentry, but we get
83 * an AST before calling d_revalidate_it(). The dentry still exists (marked
84 * INVALID) so d_lookup() matches it, but we have no lock on it (so
85 * lock_match() fails) and we spin around real_lookup(). */
86 int ll_dcompare(struct dentry *parent, struct qstr *d_name, struct qstr *name)
88 struct dentry *dchild;
91 if (d_name->len != name->len)
94 if (memcmp(d_name->name, name->name, name->len))
97 /* XXX: d_name must be in-dentry structure */
98 dchild = container_of(d_name, struct dentry, d_name); /* ugh */
99 if (dchild->d_flags & DCACHE_LUSTRE_INVALID) {
100 CDEBUG(D_DENTRY,"INVALID dentry %p not matched, was bug 3784\n",
109 /* should NOT be called with the dcache lock, see fs/dcache.c */
110 static int ll_ddelete(struct dentry *de)
114 #ifndef DCACHE_LUSTRE_INVALID
115 #define DCACHE_LUSTRE_INVALID 0
118 CDEBUG(D_DENTRY, "%s dentry %.*s (%p, parent %p, inode %p) %s%s\n",
119 (de->d_flags & DCACHE_LUSTRE_INVALID ? "deleting" : "keeping"),
120 de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
121 d_unhashed(de) ? "" : "hashed,",
122 list_empty(&de->d_subdirs) ? "" : "subdirs");
123 #if DCACHE_LUSTRE_INVALID == 0
124 #undef DCACHE_LUSTRE_INVALID
130 void ll_set_dd(struct dentry *de)
135 CDEBUG(D_DENTRY, "ldd on dentry %.*s (%p) parent %p inode %p refc %d\n",
136 de->d_name.len, de->d_name.name, de, de->d_parent, de->d_inode,
137 atomic_read(&de->d_count));
139 if (de->d_fsdata == NULL) {
140 struct ll_dentry_data *lld;
143 if (likely(lld != NULL)) {
145 if (likely(de->d_fsdata == NULL))
156 void ll_intent_drop_lock(struct lookup_intent *it)
158 struct lustre_handle *handle;
160 if (it->it_op && it->d.lustre.it_lock_mode) {
161 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
162 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
163 " from it %p\n", handle->cookie, it);
164 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
166 /* bug 494: intent_release may be called multiple times, from
167 * this thread and we don't want to double-decref this lock */
168 it->d.lustre.it_lock_mode = 0;
172 void ll_intent_release(struct lookup_intent *it)
176 ll_intent_drop_lock(it);
177 #ifdef HAVE_VFS_INTENT_PATCHES
179 it->it_op_release = 0;
181 /* We are still holding extra reference on a request, need to free it */
182 if (it_disposition(it, DISP_ENQ_OPEN_REF)) /* open req for llfile_open*/
183 ptlrpc_req_finished(it->d.lustre.it_data);
184 if (it_disposition(it, DISP_ENQ_CREATE_REF)) /* create rec */
185 ptlrpc_req_finished(it->d.lustre.it_data);
186 if (it_disposition(it, DISP_ENQ_COMPLETE)) /* saved req from revalidate
188 ptlrpc_req_finished(it->d.lustre.it_data);
190 it->d.lustre.it_disposition = 0;
191 it->d.lustre.it_data = NULL;
195 /* Drop dentry if it is not used already, unhash otherwise.
196 Should be called with dcache lock held!
197 Returns: 1 if dentry was dropped, 0 if unhashed. */
198 int ll_drop_dentry(struct dentry *dentry)
201 if (atomic_read(&dentry->d_count) == 0) {
202 CDEBUG(D_DENTRY, "deleting dentry %.*s (%p) parent %p "
203 "inode %p\n", dentry->d_name.len,
204 dentry->d_name.name, dentry, dentry->d_parent,
208 unlock_dentry(dentry);
209 spin_unlock(&dcache_lock);
210 spin_unlock(&ll_lookup_lock);
212 spin_lock(&ll_lookup_lock);
213 spin_lock(&dcache_lock);
216 /* disconected dentry can not be find without lookup, because we
217 * not need his to unhash or mark invalid. */
218 if (dentry->d_flags & DCACHE_DISCONNECTED) {
219 unlock_dentry(dentry);
223 #ifdef DCACHE_LUSTRE_INVALID
224 if (!(dentry->d_flags & DCACHE_LUSTRE_INVALID)) {
226 if (!d_unhashed(dentry)) {
228 CDEBUG(D_DENTRY, "unhashing dentry %.*s (%p) parent %p "
229 "inode %p refc %d\n", dentry->d_name.len,
230 dentry->d_name.name, dentry, dentry->d_parent,
231 dentry->d_inode, atomic_read(&dentry->d_count));
232 /* actually we don't unhash the dentry, rather just
233 * mark it inaccessible for to __d_lookup(). otherwise
234 * sys_getcwd() could return -ENOENT -bzzz */
235 #ifdef DCACHE_LUSTRE_INVALID
236 dentry->d_flags |= DCACHE_LUSTRE_INVALID;
238 if (!dentry->d_inode || !S_ISDIR(dentry->d_inode->i_mode))
243 unlock_dentry(dentry);
247 void ll_unhash_aliases(struct inode *inode)
249 struct list_head *tmp, *head;
253 CERROR("unexpected NULL inode, tell phil\n");
257 CDEBUG(D_INODE, "marking dentries for ino %lu/%u(%p) invalid\n",
258 inode->i_ino, inode->i_generation, inode);
260 head = &inode->i_dentry;
261 spin_lock(&ll_lookup_lock);
262 spin_lock(&dcache_lock);
265 while ((tmp = tmp->next) != head) {
266 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
268 if (dentry->d_name.len == 1 && dentry->d_name.name[0] == '/') {
269 CERROR("called on root (?) dentry=%p, inode=%p "
270 "ino=%lu\n", dentry, inode, inode->i_ino);
271 lustre_dump_dentry(dentry, 1);
272 libcfs_debug_dumpstack(NULL);
273 } else if (d_mountpoint(dentry)) {
274 /* For mountpoints we skip removal of the dentry
275 which happens solely because we have a lock on it
276 obtained when this dentry was not a mountpoint yet */
277 CDEBUG(D_DENTRY, "Skippind mountpoint dentry removal "
278 "%.*s (%p) parent %p\n",
281 dentry, dentry->d_parent);
286 if (ll_drop_dentry(dentry))
289 spin_unlock(&dcache_lock);
290 spin_unlock(&ll_lookup_lock);
295 int revalidate_it_finish(struct ptlrpc_request *request, int offset,
296 struct lookup_intent *it, struct dentry *de)
304 if (it_disposition(it, DISP_LOOKUP_NEG))
307 rc = ll_prep_inode(ll_i2sbi(de->d_inode)->ll_osc_exp, &de->d_inode,
308 request, offset, NULL);
313 void ll_lookup_finish_locks(struct lookup_intent *it, struct dentry *dentry)
316 LASSERT(dentry != NULL);
318 if (it->d.lustre.it_lock_mode && dentry->d_inode != NULL) {
319 struct inode *inode = dentry->d_inode;
320 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%u)\n",
321 inode, inode->i_ino, inode->i_generation);
322 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
325 /* drop lookup or getattr locks immediately */
326 if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR) {
327 /* on 2.6 there are situation when several lookups and
328 * revalidations may be requested during single operation.
329 * therefore, we don't release intent here -bzzz */
330 ll_intent_drop_lock(it);
334 void ll_frob_intent(struct lookup_intent **itp, struct lookup_intent *deft)
336 struct lookup_intent *it = *itp;
337 #ifdef HAVE_VFS_INTENT_PATCHES
339 LASSERTF(it->it_magic == INTENT_MAGIC, "bad intent magic: %x\n",
344 if (!it || it->it_op == IT_GETXATTR)
347 #ifdef HAVE_VFS_INTENT_PATCHES
348 it->it_op_release = ll_intent_release;
352 int ll_revalidate_it(struct dentry *de, int lookup_flags,
353 struct lookup_intent *it)
355 struct mdc_op_data op_data = { { 0 } };
356 struct ptlrpc_request *req = NULL;
357 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
358 struct obd_export *exp;
362 CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%s\n", de->d_name.name,
365 if (de->d_inode == NULL) {
366 /* We can only use negative dentries if this is stat or lookup,
367 for opens and stuff we do need to query server. */
368 /* If there is IT_CREAT in intent op set, then we must throw
369 away this negative dentry and actually do the request to
370 kernel to create whatever needs to be created (if possible)*/
371 if (it && (it->it_op & IT_CREAT))
374 #ifdef DCACHE_LUSTRE_INVALID
375 if (de->d_flags & DCACHE_LUSTRE_INVALID)
379 rc = ll_have_md_lock(de->d_parent->d_inode,
380 MDS_INODELOCK_UPDATE);
384 exp = ll_i2mdcexp(de->d_inode);
386 /* Never execute intents for mount points.
387 * Attributes will be fixed up in ll_inode_revalidate_it */
388 if (d_mountpoint(de))
389 GOTO(out_sa, rc = 1);
391 /* Root of the lustre tree. Always valid.
392 * Attributes will be fixed up in ll_inode_revalidate_it */
393 if (de == de->d_sb->s_root)
394 GOTO(out_sa, rc = 1);
396 OBD_FAIL_TIMEOUT(OBD_FAIL_MDC_REVALIDATE_PAUSE, 5);
397 ll_frob_intent(&it, &lookup_it);
400 ll_prepare_mdc_op_data(&op_data, de->d_parent->d_inode, de->d_inode,
401 de->d_name.name, de->d_name.len, 0, NULL);
403 if ((it->it_op == IT_OPEN) && de->d_inode) {
404 struct inode *inode = de->d_inode;
405 struct ll_inode_info *lli = ll_i2info(inode);
406 struct obd_client_handle **och_p;
408 /* We used to check for MDS_INODELOCK_OPEN here, but in fact
409 * just having LOOKUP lock is enough to justify inode is the
410 * same. And if inode is the same and we have suitable
411 * openhandle, then there is no point in doing another OPEN RPC
412 * just to throw away newly received openhandle.
413 * There are no security implications too, if file owner or
414 * access mode is change, LOOKUP lock is revoked */
416 if (it->it_flags & FMODE_WRITE) {
417 och_p = &lli->lli_mds_write_och;
418 och_usecount = &lli->lli_open_fd_write_count;
419 } else if (it->it_flags & FMODE_EXEC) {
420 och_p = &lli->lli_mds_exec_och;
421 och_usecount = &lli->lli_open_fd_exec_count;
423 och_p = &lli->lli_mds_read_och;
424 och_usecount = &lli->lli_open_fd_read_count;
426 /* Check for the proper lock. */
427 if (!ll_have_md_lock(inode, MDS_INODELOCK_LOOKUP))
429 down(&lli->lli_och_sem);
430 if (*och_p) { /* Everything is open already, do nothing */
431 /*(*och_usecount)++; Do not let them steal our open
432 handle from under us */
433 /* XXX The code above was my original idea, but in case
434 we have the handle, but we cannot use it due to later
435 checks (e.g. O_CREAT|O_EXCL flags set), nobody
436 would decrement counter increased here. So we just
437 hope the lock won't be invalidated in between. But
438 if it would be, we'll reopen the open request to
439 MDS later during file open path */
440 up(&lli->lli_och_sem);
443 up(&lli->lli_och_sem);
447 if (it->it_op == IT_GETATTR)
448 first = ll_statahead_enter(de->d_parent->d_inode, &de, 0);
451 it->it_create_mode &= ~current->fs->umask;
452 it->it_flags |= O_CHECK_STALE;
453 rc = mdc_intent_lock(exp, &op_data, NULL, 0, it, lookup_flags,
454 &req, ll_mdc_blocking_ast, 0);
455 it->it_flags &= ~O_CHECK_STALE;
456 if (it->it_op == IT_GETATTR && !first)
457 /* If there are too many locks on client-side, then some
458 * locks taken by statahead maybe dropped automatically
459 * before the real "revalidate" using them. */
460 ll_statahead_exit(de, req == NULL ? rc : 0);
461 else if (first == -EEXIST)
462 ll_statahead_mark(de);
464 /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
465 * if all was well, it will return 1 if it found locks, 0 otherwise. */
466 if (req == NULL && rc >= 0) {
474 CDEBUG(D_INFO, "ll_intent_lock: rc %d : it->it_status "
475 "%d\n", rc, it->d.lustre.it_status);
481 rc = revalidate_it_finish(req, DLM_REPLY_REC_OFF, it, de);
483 /* we are going release the intent, so clear DISP_ENQ_COMPLETE
484 * to prevent a double free of the request */
485 it_clear_disposition(it, DISP_ENQ_COMPLETE);
486 ll_intent_release(it);
489 if ((it->it_op & IT_OPEN) && de->d_inode &&
490 !S_ISREG(de->d_inode->i_mode) &&
491 !S_ISDIR(de->d_inode->i_mode)) {
492 ll_release_openhandle(de, it);
496 /* unfortunately ll_intent_lock may cause a callback and revoke our
498 spin_lock(&ll_lookup_lock);
499 spin_lock(&dcache_lock);
503 d_rehash_cond(de, 0);
504 spin_unlock(&dcache_lock);
505 spin_unlock(&ll_lookup_lock);
508 /* We do not free request as it may be reused during following lookup
509 * (see comment in mdc/mdc_locks.c::mdc_intent_lock()), request will
510 * be freed in ll_lookup_it or in ll_intent_release. But if
511 * request was not completed, we need to free it. (bug 5154, 9903) */
512 if (req != NULL && !it_disposition(it, DISP_ENQ_COMPLETE))
513 ptlrpc_req_finished(req);
515 #ifdef DCACHE_LUSTRE_INVALID
516 ll_unhash_aliases(de->d_inode);
517 /* done in ll_unhash_aliases()
518 dentry->d_flags |= DCACHE_LUSTRE_INVALID; */
520 /* We do not want d_invalidate to kill all child dentries too */
524 CDEBUG(D_DENTRY, "revalidated dentry %.*s (%p) parent %p "
525 "inode %p refc %d\n", de->d_name.len,
526 de->d_name.name, de, de->d_parent, de->d_inode,
527 atomic_read(&de->d_count));
528 ll_lookup_finish_locks(it, de);
529 #ifdef DCACHE_LUSTRE_INVALID
531 de->d_flags &= ~DCACHE_LUSTRE_INVALID;
536 /* This part is here to combat evil-evil race in real_lookup on 2.6 kernels.
537 * The race details are: We enter do_lookup() looking for some name,
538 * there is nothing in dcache for this name yet and d_lookup() returns NULL.
539 * We proceed to real_lookup(), and while we do this, another process does
540 * open on the same file we looking up (most simple reproducer), open succeeds
541 * and the dentry is added. Now back to us. In real_lookup() we do d_lookup()
542 * again and suddenly find the dentry, so we call d_revalidate on it, but there
543 * is no lock, so without this code we would return 0, but unpatched
544 * real_lookup just returns -ENOENT in such a case instead of retrying the
545 * lookup. Once this is dealt with in real_lookup(), all of this ugly mess
546 * can go and we can just check locks in ->d_revalidate without doing any
549 if (it != &lookup_it) {
550 ll_lookup_finish_locks(it, de);
553 /*do real lookup here */
554 ll_prepare_mdc_op_data(&op_data, de->d_parent->d_inode, NULL,
555 de->d_name.name, de->d_name.len, 0, NULL);
556 rc = mdc_intent_lock(exp, &op_data, NULL, 0, it, 0, &req,
557 ll_mdc_blocking_ast, 0);
559 struct mds_body *mds_body = lustre_msg_buf(req->rq_repmsg,
562 struct ll_fid fid = { 0 };
565 ll_inode2fid(&fid, de->d_inode);
567 /* see if we got same inode, if not - return error */
568 if(!memcmp(&fid, &mds_body->fid1, sizeof(struct ll_fid)))
569 goto revalidate_finish;
570 /* we are going release the intent, so clear DISP_ENQ_COMPLETE
571 * to prevent a double free of the request */
572 it_clear_disposition(it, DISP_ENQ_COMPLETE);
573 ll_intent_release(it);
579 * For rc == 1 case, should not return directly to prevent losing
580 * statahead windows; for rc == 0 case, the "lookup" will be done later.
582 if (it && it->it_op == IT_GETATTR && rc == 1) {
583 first = ll_statahead_enter(de->d_parent->d_inode, &de, 0);
585 ll_statahead_exit(de, 1);
586 else if (first == -EEXIST)
587 ll_statahead_mark(de);
593 /*static*/ void ll_pin(struct dentry *de, struct vfsmount *mnt, int flag)
595 struct inode *inode= de->d_inode;
596 struct ll_sb_info *sbi = ll_i2sbi(inode);
597 struct ll_dentry_data *ldd = ll_d2d(de);
598 struct obd_client_handle *handle;
604 /* Strictly speaking this introduces an additional race: the
605 * increments should wait until the rpc has returned.
606 * However, given that at present the function is void, this
608 if (flag == 1 && (++ldd->lld_mnt_count) > 1) {
614 if (flag == 0 && (++ldd->lld_cwd_count) > 1) {
621 handle = (flag) ? &ldd->lld_mnt_och : &ldd->lld_cwd_och;
622 rc = obd_pin(sbi->ll_mdc_exp, ll_inode_ll_fid(inode),
627 memset(handle, 0, sizeof(*handle));
629 ldd->lld_cwd_count--;
631 ldd->lld_mnt_count--;
639 /*static*/ void ll_unpin(struct dentry *de, struct vfsmount *mnt, int flag)
641 struct ll_sb_info *sbi = ll_i2sbi(de->d_inode);
642 struct ll_dentry_data *ldd = ll_d2d(de);
643 struct obd_client_handle handle;
649 /* Strictly speaking this introduces an additional race: the
650 * increments should wait until the rpc has returned.
651 * However, given that at present the function is void, this
653 handle = (flag) ? ldd->lld_mnt_och : ldd->lld_cwd_och;
654 if (handle.och_magic != OBD_CLIENT_HANDLE_MAGIC) {
655 /* the "pin" failed */
662 count = --ldd->lld_mnt_count;
664 count = --ldd->lld_cwd_count;
672 rc = obd_unpin(sbi->ll_mdc_exp, &handle, flag);
677 #ifdef HAVE_VFS_INTENT_PATCHES
678 static int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
683 if (nd && nd->flags & LOOKUP_LAST && !(nd->flags & LOOKUP_LINK_NOTLAST))
684 rc = ll_revalidate_it(dentry, nd->flags, &nd->intent);
686 rc = ll_revalidate_it(dentry, 0, NULL);
691 int ll_revalidate_nd(struct dentry *dentry, struct nameidata *nd)
696 if (nd && !(nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))) {
697 struct lookup_intent *it;
698 it = ll_convert_intent(&nd->intent.open, nd->flags);
701 if (it->it_op == (IT_OPEN|IT_CREAT))
702 if (nd->intent.open.flags & O_EXCL) {
704 "create O_EXCL, returning 0\n");
709 rc = ll_revalidate_it(dentry, nd->flags, it);
711 if (rc && (nd->flags & LOOKUP_OPEN) &&
712 it_disposition(it, DISP_OPEN_OPEN)) {/*Open*/
713 #ifdef HAVE_FILE_IN_STRUCT_INTENT
714 // XXX Code duplication with ll_lookup_nd
715 if (S_ISFIFO(dentry->d_inode->i_mode)) {
716 // We cannot call open here as it would
719 (struct ptlrpc_request *)
720 it->d.lustre.it_data);
724 nd->intent.open.file->private_data = it;
725 filp = lookup_instantiate_filp(nd, dentry,NULL);
726 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17))
727 /* 2.6.1[456] have a bug in open_namei() that forgets to check
728 * nd->intent.open.file for error, so we need to return it as lookup's result
735 ll_release_openhandle(dentry, it);
736 #endif /* HAVE_FILE_IN_STRUCT_INTENT */
738 if (!rc && (nd->flags & LOOKUP_CREATE) &&
739 it_disposition(it, DISP_OPEN_CREATE)) {
740 /* We created something but we may only return
741 * negative dentry here, so save request in dentry,
742 * if lookup will be called later on, it will
743 * pick the request, otherwise it would be freed
745 ll_d2d(dentry)->lld_it = it;
746 it = NULL; /* avoid freeing */
751 ll_intent_release(it);
752 OBD_FREE(it, sizeof(*it));
755 rc = ll_revalidate_it(dentry, 0, NULL);
762 struct dentry_operations ll_d_ops = {
763 .d_revalidate = ll_revalidate_nd,
764 .d_release = ll_release,
765 .d_delete = ll_ddelete,
766 #ifdef DCACHE_LUSTRE_INVALID
767 .d_compare = ll_dcompare,