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.
36 * lustre/liblustre/namei.c
38 * Lustre Light name resolution
41 #define DEBUG_SUBSYSTEM S_LLITE
47 #include <sys/types.h>
50 #include <sys/queue.h>
65 #include "llite_lib.h"
67 void ll_intent_drop_lock(struct lookup_intent *it)
69 struct lustre_handle *handle;
71 if (it->it_op && it->d.lustre.it_lock_mode) {
72 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
73 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
74 " from it %p\n", handle->cookie, it);
75 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
77 /* bug 494: intent_release may be called multiple times, from
78 * this thread and we don't want to double-decref this lock */
79 it->d.lustre.it_lock_mode = 0;
83 void ll_intent_release(struct lookup_intent *it)
87 ll_intent_drop_lock(it);
89 it->it_op_release = 0;
90 it->d.lustre.it_disposition = 0;
91 it->d.lustre.it_data = NULL;
97 * remove the stale inode from pnode
99 void unhook_stale_inode(struct pnode *pno)
101 struct inode *inode = pno->p_base->pb_ino;
105 LASSERT(llu_i2info(inode)->lli_stale_flag);
107 pno->p_base->pb_ino = NULL;
110 if (!llu_i2info(inode)->lli_open_count) {
111 CDEBUG(D_INODE, "unhook inode %p (ino %lu) from pno %p\n",
112 inode, llu_i2info(inode)->lli_st_ino, pno);
114 _sysio_i_gone(inode);
122 void llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
128 inode = pnode->p_base->pb_ino;
129 if (it->d.lustre.it_lock_mode && inode != NULL) {
130 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%llu/%lu)\n",
131 inode, (long long)llu_i2stat(inode)->st_ino,
132 llu_i2info(inode)->lli_st_generation);
133 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode, NULL);
136 /* drop lookup/getattr locks */
137 if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)
138 ll_intent_release(it);
142 static inline void llu_invalidate_inode_pages(struct inode * inode)
147 int llu_mdc_blocking_ast(struct ldlm_lock *lock,
148 struct ldlm_lock_desc *desc,
149 void *data, int flag)
152 struct lustre_handle lockh;
157 case LDLM_CB_BLOCKING:
158 ldlm_lock2handle(lock, &lockh);
159 rc = ldlm_cli_cancel(&lockh);
161 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
165 case LDLM_CB_CANCELING: {
166 struct inode *inode = llu_inode_from_lock(lock);
167 struct llu_inode_info *lli;
168 struct intnl_stat *st;
169 __u64 bits = lock->l_policy_data.l_inodebits.bits;
171 /* Invalidate all dentries associated with this inode */
175 lli = llu_i2info(inode);
176 st = llu_i2stat(inode);
178 if (bits & MDS_INODELOCK_UPDATE)
179 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
181 if (lock->l_resource->lr_name.name[0] != st->st_ino ||
182 lock->l_resource->lr_name.name[1] !=lli->lli_st_generation){
183 LDLM_ERROR(lock, "data mismatch with ino %llu/%lu",
184 (long long)st->st_ino,lli->lli_st_generation);
186 if (S_ISDIR(st->st_mode) &&
187 (bits & MDS_INODELOCK_UPDATE)) {
188 CDEBUG(D_INODE, "invalidating inode %llu\n",
189 (long long)st->st_ino);
191 llu_invalidate_inode_pages(inode);
195 if (inode->i_sb->s_root &&
196 inode != inode->i_sb->s_root->d_inode)
197 ll_unhash_aliases(inode);
209 static int pnode_revalidate_finish(struct ptlrpc_request *req,
211 struct lookup_intent *it,
214 struct inode *inode = pnode->p_base->pb_ino;
224 if (it_disposition(it, DISP_LOOKUP_NEG))
227 rc = mdc_req2lustre_md(req, offset, llu_i2sbi(inode)->ll_osc_exp, &md);
231 llu_update_inode(inode, md.body, md.lsm);
236 static int llu_pb_revalidate(struct pnode *pnode, int flags,
237 struct lookup_intent *it)
239 struct pnode_base *pb = pnode->p_base;
240 struct it_cb_data icbd;
241 struct mdc_op_data op_data;
242 struct ptlrpc_request *req = NULL;
243 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
244 struct obd_export *exp;
248 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
249 (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
251 /* We don't want to cache negative dentries, so return 0 immediately.
252 * We believe that this is safe, that negative dentries cannot be
253 * pinned by someone else */
254 if (pb->pb_ino == NULL) {
255 CDEBUG(D_INODE, "negative pb\n");
259 /* This is due to bad interaction with libsysio. remove this when we
260 * switched to libbsdio XXX
263 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
264 struct intnl_stat *st = llu_i2stat(pb->pb_ino);
266 CDEBUG(D_INODE, "inode %llu still have intent "
267 "%p(opc 0x%x), release it\n",
268 (long long) st->st_ino, lli->lli_it,
270 ll_intent_release(lli->lli_it);
271 OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
276 exp = llu_i2mdcexp(pb->pb_ino);
277 icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
278 icbd.icbd_child = pnode;
282 it->it_op_release = ll_intent_release;
285 llu_prepare_mdc_op_data(&op_data, pnode->p_parent->p_base->pb_ino,
286 pb->pb_ino, pb->pb_name.name,pb->pb_name.len,0);
288 rc = mdc_intent_lock(exp, &op_data, NULL, 0, it, flags,
289 &req, llu_mdc_blocking_ast,
290 LDLM_FL_CANCEL_ON_BLOCK);
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)
299 rc = pnode_revalidate_finish(req, DLM_REPLY_REC_OFF, it, pnode);
301 ll_intent_release(it);
306 /* Note: ll_intent_lock may cause a callback, check this! */
308 if (it->it_op & IT_OPEN)
309 LL_SAVE_INTENT(pb->pb_ino, it);
313 ptlrpc_req_finished(req);
319 llu_lookup_finish_locks(it, pnode);
324 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
325 struct lookup_intent *it, void *data)
327 struct it_cb_data *icbd = data;
328 struct pnode *child = icbd->icbd_child;
329 struct inode *parent = icbd->icbd_parent;
330 struct llu_sb_info *sbi = llu_i2sbi(parent);
331 struct inode *inode = NULL;
334 /* libsysio require us generate inode right away if success.
335 * so if mds created new inode for us we need make sure it
336 * succeeded. thus for any error we can't delay to the
337 * llu_file_open() time. */
338 if (it_disposition(it, DISP_OPEN_CREATE) &&
339 it_open_error(DISP_OPEN_CREATE, it)) {
340 CDEBUG(D_INODE, "detect mds create error\n");
341 return it_open_error(DISP_OPEN_CREATE, it);
343 if (it_disposition(it, DISP_OPEN_OPEN) &&
344 it_open_error(DISP_OPEN_OPEN, it)) {
345 CDEBUG(D_INODE, "detect mds open error\n");
346 /* undo which did by mdc_intent_lock */
347 if (it_disposition(it, DISP_OPEN_CREATE) &&
348 !it_open_error(DISP_OPEN_CREATE, it)) {
350 LASSERT(atomic_read(&request->rq_refcount) > 1);
351 CDEBUG(D_INODE, "dec a ref of req %p\n", request);
352 ptlrpc_req_finished(request);
354 return it_open_error(DISP_OPEN_OPEN, it);
357 /* NB 1 request reference will be taken away by ll_intent_lock()
360 if (!it_disposition(it, DISP_LOOKUP_NEG) || (it->it_op & IT_CREAT)) {
362 struct llu_inode_info *lli;
363 struct intnl_stat *st;
366 if (it_disposition(it, DISP_OPEN_CREATE))
367 ptlrpc_req_finished(request);
369 rc = mdc_req2lustre_md(request, offset, sbi->ll_osc_exp, &md);
373 inode = llu_iget(parent->i_fs, &md);
374 if (!inode || IS_ERR(inode)) {
375 /* free the lsm if we allocated one above */
377 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
378 RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
379 } else if (md.lsm != NULL &&
380 llu_i2info(inode)->lli_smd != md.lsm) {
381 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
384 lli = llu_i2info(inode);
385 st = llu_i2stat(inode);
387 /* If this is a stat, get the authoritative file size */
388 if (it->it_op == IT_GETATTR && S_ISREG(st->st_mode) &&
389 lli->lli_smd != NULL) {
390 struct lov_stripe_md *lsm = lli->lli_smd;
393 LASSERT(lsm->lsm_object_id != 0);
395 /* bug 2334: drop MDS lock before acquiring OST lock */
396 ll_intent_drop_lock(it);
398 rc = llu_glimpse_size(inode);
408 /* intent will be further used in cases of open()/getattr() */
409 if (inode && (it->it_op & IT_OPEN))
410 LL_SAVE_INTENT(inode, it);
412 child->p_base->pb_ino = inode;
417 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
420 lock_res_and_lock(lock);
422 if (lock->l_ast_data) {
423 inode = (struct inode *)lock->l_ast_data;
428 unlock_res_and_lock(lock);
432 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
433 struct lookup_intent *it, int flags)
435 struct mdc_op_data op_data;
436 struct it_cb_data icbd;
437 struct ptlrpc_request *req = NULL;
438 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
442 if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
443 RETURN(-ENAMETOOLONG);
447 it->it_op_release = ll_intent_release;
450 icbd.icbd_child = pnode;
451 icbd.icbd_parent = parent;
453 llu_prepare_mdc_op_data(&op_data, parent, NULL,
454 pnode->p_base->pb_name.name,
455 pnode->p_base->pb_name.len, flags);
457 rc = mdc_intent_lock(llu_i2mdcexp(parent), &op_data, NULL, 0, it,
458 flags, &req, llu_mdc_blocking_ast,
459 LDLM_FL_CANCEL_ON_BLOCK);
463 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
465 ll_intent_release(it);
469 llu_lookup_finish_locks(it, pnode);
473 ptlrpc_req_finished(req);
477 static struct lookup_intent*
478 translate_lookup_intent(struct intent *intent, const char *path)
480 struct lookup_intent *it;
484 if (!intent || path) {
485 CDEBUG(D_VFSTRACE, "not intent needed\n");
489 OBD_ALLOC(it, sizeof(*it));
492 memset(it, 0, sizeof(*it));
494 /* libsysio will assign intent like following:
495 * NOTE: INT_CREAT has include INT_UPDPARENT
497 * open: INT_OPEN [| INT_CREAT]
500 * unlink: INT_UPDPARENT
501 * rmdir: INT_UPDPARENT
506 * following logic is adjusted for libsysio
509 it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
511 if (intent->int_opmask & INT_OPEN) {
512 it->it_op |= IT_OPEN;
514 /* convert access mode from O_ to FMODE_ */
515 if (it->it_flags & O_WRONLY)
517 else if (it->it_flags & O_RDWR)
518 fmode = FMODE_READ | FMODE_WRITE;
521 it->it_flags &= ~O_ACCMODE;
522 it->it_flags |= fmode;
525 /* XXX libsysio has strange code on intent handling,
526 * more check later */
527 if (it->it_flags & O_CREAT) {
528 it->it_op |= IT_CREAT;
529 it->it_create_mode = *((int*)intent->int_arg1);
530 /* bug 7278: libsysio hack. For O_EXCL, libsysio depends on
531 this lookup to return negative result, but then there is no
532 way to find out original intent in ll_iop_open(). So we just
533 clear O_EXCL from libsysio flags here to avoid checking
534 for negative result. O_EXCL will be enforced by MDS. */
535 *((int*)intent->int_arg2) &= ~O_EXCL;
538 if (intent->int_opmask & INT_GETATTR)
539 it->it_op |= IT_GETATTR;
541 LASSERT(!(intent->int_opmask & INT_SETATTR));
543 /* libsysio is different to linux vfs when doing unlink/rmdir,
544 * INT_UPDPARENT was passed down during name resolution. Here
545 * we treat it as normal lookup, later unlink()/rmdir() will
546 * do the actual work */
548 /* conform to kernel code, if only IT_LOOKUP was set, don't
550 if (!it->it_op || it->it_op == IT_LOOKUP) {
551 OBD_FREE(it, sizeof(*it));
555 it->it_op_release = ll_intent_release;
557 CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
561 int llu_iop_lookup(struct pnode *pnode,
563 struct intent *intnt,
566 struct lookup_intent *it;
570 liblustre_wait_event(0);
574 /* the mount root inode have no name, so don't call
575 * remote in this case. but probably we need revalidate
577 if (pnode->p_mount->mnt_root == pnode) {
578 struct inode *i = pnode->p_base->pb_ino;
583 if (!pnode->p_base->pb_name.len)
586 it = translate_lookup_intent(intnt, path);
588 /* param flags is not used, let it be 0 */
589 if (llu_pb_revalidate(pnode, 0, it)) {
590 LASSERT(pnode->p_base->pb_ino);
591 *inop = pnode->p_base->pb_ino;
595 rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
597 if (!pnode->p_base->pb_ino)
600 *inop = pnode->p_base->pb_ino;
605 OBD_FREE(it, sizeof(*it));
606 liblustre_wait_event(0);