4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/liblustre/namei.c
38 * Lustre Light name resolution
41 #define DEBUG_SUBSYSTEM S_LLITE
49 #include <sys/types.h>
50 #include <libcfs/libcfs.h>
51 #include <lustre/lustre_idl.h>
52 #include <liblustre.h>
54 #include <lustre_dlm.h>
55 #include <lustre_fid.h>
56 #include <lustre_mdc.h>
57 #include <lustre_net.h>
59 #include <obd_class.h>
60 #include <obd_support.h>
61 #include "llite_lib.h"
63 static void ll_intent_drop_lock(struct lookup_intent *it)
65 struct lustre_handle *handle;
67 if (it->it_op && it->d.lustre.it_lock_mode) {
68 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
69 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
70 " from it %p\n", handle->cookie, it);
71 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
73 /* bug 494: intent_release may be called multiple times, from
74 * this thread and we don't want to double-decref this lock */
75 it->d.lustre.it_lock_mode = 0;
79 static void ll_intent_release(struct lookup_intent *it)
83 ll_intent_drop_lock(it);
85 it->it_op_release = 0;
86 it->d.lustre.it_disposition = 0;
87 it->d.lustre.it_data = NULL;
92 llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
98 inode = pnode->p_base->pb_ino;
99 if (it->d.lustre.it_lock_mode && inode != NULL) {
100 struct llu_sb_info *sbi;
102 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%llu/%lu)\n",
103 inode, (long long)llu_i2stat(inode)->st_ino,
104 llu_i2info(inode)->lli_st_generation);
106 sbi = llu_i2sbi(inode);
107 md_set_lock_data(sbi->ll_md_exp,
108 &it->d.lustre.it_lock_handle, inode, NULL);
111 /* drop lookup/getattr locks */
112 if (it->it_op & (IT_LOOKUP | IT_GETATTR))
113 ll_intent_release(it);
117 static inline void llu_invalidate_inode_pages(struct inode * inode)
122 int llu_md_blocking_ast(struct ldlm_lock *lock,
123 struct ldlm_lock_desc *desc,
124 void *data, int flag)
126 struct lustre_handle lockh;
132 case LDLM_CB_BLOCKING:
133 ldlm_lock2handle(lock, &lockh);
134 rc = ldlm_cli_cancel(&lockh, 0);
136 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
140 case LDLM_CB_CANCELING: {
141 struct inode *inode = llu_inode_from_resource_lock(lock);
142 struct llu_inode_info *lli;
143 struct intnl_stat *st;
144 __u64 bits = lock->l_policy_data.l_inodebits.bits;
147 /* Inode is set to lock->l_resource->lr_lvb_inode
148 * for mdc - bug 24555 */
149 LASSERT(lock->l_ast_data == NULL);
151 /* Invalidate all dentries associated with this inode */
155 lli = llu_i2info(inode);
156 st = llu_i2stat(inode);
158 if (bits & MDS_INODELOCK_UPDATE)
159 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
162 if (!fid_res_name_eq(fid, &lock->l_resource->lr_name))
163 LDLM_ERROR(lock, "data mismatch with object "
164 DFID" (%p)", PFID(fid), inode);
165 if (S_ISDIR(st->st_mode) &&
166 (bits & MDS_INODELOCK_UPDATE)) {
167 CDEBUG(D_INODE, "invalidating inode %llu\n",
168 (long long)st->st_ino);
170 llu_invalidate_inode_pages(inode);
174 if (inode->i_sb->s_root &&
175 inode != inode->i_sb->s_root->d_inode)
176 ll_unhash_aliases(inode);
188 static int pnode_revalidate_finish(struct ptlrpc_request *req,
189 struct lookup_intent *it,
192 struct inode *inode = pnode->p_base->pb_ino;
202 if (it_disposition(it, DISP_LOOKUP_NEG))
205 rc = md_get_lustre_md(llu_i2sbi(inode)->ll_md_exp, req,
206 llu_i2sbi(inode)->ll_dt_exp,
207 llu_i2sbi(inode)->ll_md_exp, &md);
211 llu_update_inode(inode, &md);
216 static int llu_pb_revalidate(struct pnode *pnode, int flags,
217 struct lookup_intent *it)
219 struct pnode_base *pb = pnode->p_base;
220 struct md_op_data op_data = {{ 0 }};
221 struct ptlrpc_request *req = NULL;
222 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
223 struct obd_export *exp;
227 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
228 (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
230 /* We don't want to cache negative dentries, so return 0 immediately.
231 * We believe that this is safe, that negative dentries cannot be
232 * pinned by someone else */
233 if (pb->pb_ino == NULL) {
234 CDEBUG(D_INODE, "negative pb\n");
238 /* This is due to bad interaction with libsysio. remove this when we
239 * switched to libbsdio XXX
242 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
243 struct intnl_stat *st = llu_i2stat(pb->pb_ino);
245 CDEBUG(D_INODE, "inode %llu still have intent "
246 "%p(opc 0x%x), release it\n",
247 (long long) st->st_ino, lli->lli_it,
249 ll_intent_release(lli->lli_it);
250 OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
255 exp = llu_i2mdexp(pb->pb_ino);
259 it->it_op_release = ll_intent_release;
262 llu_prep_md_op_data(&op_data, pnode->p_parent->p_base->pb_ino,
263 pb->pb_ino, pb->pb_name.name, pb->pb_name.len,
266 rc = md_intent_lock(exp, &op_data, it, &req, &llu_md_blocking_ast,
267 LDLM_FL_CANCEL_ON_BLOCK);
268 /* If req is NULL, then md_intent_lock only tried to do a lock match;
269 * if all was well, it will return 1 if it found locks, 0 otherwise. */
270 if (req == NULL && rc >= 0)
276 rc = pnode_revalidate_finish(req, it, pnode);
278 ll_intent_release(it);
283 /* Note: ll_intent_lock may cause a callback, check this! */
285 if (it->it_op & IT_OPEN)
286 LL_SAVE_INTENT(pb->pb_ino, it);
290 ptlrpc_req_finished(req);
296 llu_lookup_finish_locks(it, pnode);
301 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
302 struct lookup_intent *it,
303 struct inode *parent, struct pnode *child)
305 struct llu_sb_info *sbi = llu_i2sbi(parent);
306 struct inode *inode = NULL;
309 /* libsysio require us generate inode right away if success.
310 * so if mds created new inode for us we need make sure it
311 * succeeded. thus for any error we can't delay to the
312 * llu_file_open() time. */
313 if (it_disposition(it, DISP_OPEN_CREATE) &&
314 it_open_error(DISP_OPEN_CREATE, it)) {
315 CDEBUG(D_INODE, "detect mds create error\n");
316 return it_open_error(DISP_OPEN_CREATE, it);
318 if (it_disposition(it, DISP_OPEN_OPEN) &&
319 it_open_error(DISP_OPEN_OPEN, it)) {
320 CDEBUG(D_INODE, "detect mds open error\n");
321 /* undo which did by md_intent_lock */
322 if (it_disposition(it, DISP_OPEN_CREATE) &&
323 !it_open_error(DISP_OPEN_CREATE, it)) {
325 LASSERT(atomic_read(&request->rq_refcount) > 1);
326 CDEBUG(D_INODE, "dec a ref of req %p\n", request);
327 ptlrpc_req_finished(request);
329 return it_open_error(DISP_OPEN_OPEN, it);
332 /* NB 1 request reference will be taken away by ll_intent_lock()
335 if (!it_disposition(it, DISP_LOOKUP_NEG) || (it->it_op & IT_CREAT)) {
337 struct llu_inode_info *lli;
338 struct intnl_stat *st;
341 if (it_disposition(it, DISP_OPEN_CREATE))
342 ptlrpc_req_finished(request);
344 rc = md_get_lustre_md(sbi->ll_md_exp, request,
345 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
349 inode = llu_iget(parent->i_fs, &md);
350 if (!inode || IS_ERR(inode)) {
351 /* free the lsm if we allocated one above */
353 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
354 RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
355 } else if (md.lsm != NULL) {
356 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
359 lli = llu_i2info(inode);
360 st = llu_i2stat(inode);
362 /* If this is a stat, get the authoritative file size */
363 if (it->it_op == IT_GETATTR && S_ISREG(st->st_mode) &&
367 /* bug 2334: drop MDS lock before acquiring OST lock */
368 ll_intent_drop_lock(it);
370 rc = cl_glimpse_size(inode);
380 /* intent will be further used in cases of open()/getattr() */
381 if (inode && (it->it_op & IT_OPEN))
382 LL_SAVE_INTENT(inode, it);
384 child->p_base->pb_ino = inode;
389 struct inode *llu_inode_from_resource_lock(struct ldlm_lock *lock)
392 lock_res_and_lock(lock);
394 if (lock->l_resource->lr_lvb_inode) {
395 inode = (struct inode *)lock->l_resource->lr_lvb_inode;
400 unlock_res_and_lock(lock);
404 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
405 struct lookup_intent *it, int flags)
407 struct md_op_data op_data = {{ 0 }};
408 struct ptlrpc_request *req = NULL;
409 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
414 if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
415 RETURN(-ENAMETOOLONG);
419 it->it_op_release = ll_intent_release;
422 if (it->it_op & IT_CREAT)
423 opc = LUSTRE_OPC_CREATE;
425 opc = LUSTRE_OPC_ANY;
427 llu_prep_md_op_data(&op_data, parent, NULL,
428 pnode->p_base->pb_name.name,
429 pnode->p_base->pb_name.len, flags, opc);
431 rc = md_intent_lock(llu_i2mdexp(parent), &op_data, it, &req,
432 &llu_md_blocking_ast, LDLM_FL_CANCEL_ON_BLOCK);
436 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, parent, pnode);
438 ll_intent_release(it);
442 llu_lookup_finish_locks(it, pnode);
446 ptlrpc_req_finished(req);
450 static struct lookup_intent*
451 translate_lookup_intent(struct intent *intent, const char *path)
453 struct lookup_intent *it;
457 if (!intent || path) {
458 CDEBUG(D_VFSTRACE, "not intent needed\n");
462 OBD_ALLOC(it, sizeof(*it));
465 memset(it, 0, sizeof(*it));
467 /* libsysio will assign intent like following:
468 * NOTE: INT_CREAT has include INT_UPDPARENT
470 * open: INT_OPEN [| INT_CREAT]
473 * unlink: INT_UPDPARENT
474 * rmdir: INT_UPDPARENT
479 * following logic is adjusted for libsysio
482 it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
484 if (intent->int_opmask & INT_OPEN) {
485 it->it_op |= IT_OPEN;
487 /* convert access mode from O_ to FMODE_ */
488 if (it->it_flags & O_WRONLY)
490 else if (it->it_flags & O_RDWR)
491 fmode = FMODE_READ | FMODE_WRITE;
494 it->it_flags &= ~O_ACCMODE;
495 it->it_flags |= fmode;
498 /* XXX libsysio has strange code on intent handling,
499 * more check later */
500 if (it->it_flags & O_CREAT) {
501 it->it_op |= IT_CREAT;
502 it->it_create_mode = *((int*)intent->int_arg1);
503 /* bug 7278: libsysio hack. For O_EXCL, libsysio depends on
504 this lookup to return negative result, but then there is no
505 way to find out original intent in ll_iop_open(). So we just
506 clear O_EXCL from libsysio flags here to avoid checking
507 for negative result. O_EXCL will be enforced by MDS. */
508 *((int*)intent->int_arg2) &= ~O_EXCL;
511 if (intent->int_opmask & INT_GETATTR)
512 it->it_op |= IT_GETATTR;
514 LASSERT(!(intent->int_opmask & INT_SETATTR));
516 /* libsysio is different to linux vfs when doing unlink/rmdir,
517 * INT_UPDPARENT was passed down during name resolution. Here
518 * we treat it as normal lookup, later unlink()/rmdir() will
519 * do the actual work */
521 /* conform to kernel code, if only IT_LOOKUP was set, don't
523 if (!it->it_op || it->it_op & IT_LOOKUP) {
524 OBD_FREE(it, sizeof(*it));
528 it->it_op_release = ll_intent_release;
530 CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
534 int llu_iop_lookup(struct pnode *pnode,
536 struct intent *intnt,
539 struct lookup_intent *it;
543 liblustre_wait_event(0);
547 /* the mount root inode have no name, so don't call
548 * remote in this case. but probably we need revalidate
550 if (pnode->p_mount->mnt_root == pnode) {
551 struct inode *i = pnode->p_base->pb_ino;
556 if (!pnode->p_base->pb_name.len)
559 it = translate_lookup_intent(intnt, path);
561 /* param flags is not used, let it be 0 */
562 if (llu_pb_revalidate(pnode, 0, it)) {
563 LASSERT(pnode->p_base->pb_ino);
564 *inop = pnode->p_base->pb_ino;
568 rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
570 if (!pnode->p_base->pb_ino)
573 *inop = pnode->p_base->pb_ino;
578 OBD_FREE(it, sizeof(*it));
579 liblustre_wait_event(0);