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>
63 #include "llite_lib.h"
65 void ll_intent_drop_lock(struct lookup_intent *it)
67 struct lustre_handle *handle;
69 if (it->it_op && it->d.lustre.it_lock_mode) {
70 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
71 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
72 " from it %p\n", handle->cookie, it);
73 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
75 /* bug 494: intent_release may be called multiple times, from
76 * this thread and we don't want to double-decref this lock */
77 it->d.lustre.it_lock_mode = 0;
81 void ll_intent_release(struct lookup_intent *it)
85 ll_intent_drop_lock(it);
87 it->it_op_release = 0;
88 it->d.lustre.it_disposition = 0;
89 it->d.lustre.it_data = NULL;
93 void llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
99 inode = pnode->p_base->pb_ino;
100 if (it->d.lustre.it_lock_mode && inode != NULL) {
101 struct llu_sb_info *sbi;
103 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%llu/%lu)\n",
104 inode, (long long)llu_i2stat(inode)->st_ino,
105 llu_i2info(inode)->lli_st_generation);
107 sbi = llu_i2sbi(inode);
108 md_set_lock_data(sbi->ll_md_exp,
109 &it->d.lustre.it_lock_handle, inode, NULL);
112 /* drop lookup/getattr locks */
113 if (it->it_op & (IT_LOOKUP | IT_GETATTR))
114 ll_intent_release(it);
118 static inline void llu_invalidate_inode_pages(struct inode * inode)
123 int llu_md_blocking_ast(struct ldlm_lock *lock,
124 struct ldlm_lock_desc *desc,
125 void *data, int flag)
127 struct lustre_handle lockh;
133 case LDLM_CB_BLOCKING:
134 ldlm_lock2handle(lock, &lockh);
135 rc = ldlm_cli_cancel(&lockh);
137 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
141 case LDLM_CB_CANCELING: {
142 struct inode *inode = llu_inode_from_lock(lock);
143 struct llu_inode_info *lli;
144 struct intnl_stat *st;
145 __u64 bits = lock->l_policy_data.l_inodebits.bits;
148 /* Invalidate all dentries associated with this inode */
152 lli = llu_i2info(inode);
153 st = llu_i2stat(inode);
155 if (bits & MDS_INODELOCK_UPDATE)
156 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
159 if (lock->l_resource->lr_name.name[0] != fid_seq(fid) ||
160 lock->l_resource->lr_name.name[1] != fid_oid(fid) ||
161 lock->l_resource->lr_name.name[2] != fid_ver(fid)) {
162 LDLM_ERROR(lock,"data mismatch with ino %llu/%llu/%llu",
163 (long long)fid_seq(fid),
164 (long long)fid_oid(fid),
165 (long long)fid_ver(fid));
167 if (S_ISDIR(st->st_mode) &&
168 (bits & MDS_INODELOCK_UPDATE)) {
169 CDEBUG(D_INODE, "invalidating inode %llu\n",
170 (long long)st->st_ino);
172 llu_invalidate_inode_pages(inode);
176 if (inode->i_sb->s_root &&
177 inode != inode->i_sb->s_root->d_inode)
178 ll_unhash_aliases(inode);
190 static int pnode_revalidate_finish(struct ptlrpc_request *req,
191 struct lookup_intent *it,
194 struct inode *inode = pnode->p_base->pb_ino;
204 if (it_disposition(it, DISP_LOOKUP_NEG))
207 rc = md_get_lustre_md(llu_i2sbi(inode)->ll_md_exp, req,
208 llu_i2sbi(inode)->ll_dt_exp,
209 llu_i2sbi(inode)->ll_md_exp, &md);
213 llu_update_inode(inode, &md);
218 static int llu_pb_revalidate(struct pnode *pnode, int flags,
219 struct lookup_intent *it)
221 struct pnode_base *pb = pnode->p_base;
222 struct it_cb_data icbd;
223 struct md_op_data op_data = {{ 0 }};
224 struct ptlrpc_request *req = NULL;
225 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
226 struct obd_export *exp;
230 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
231 (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
233 /* We don't want to cache negative dentries, so return 0 immediately.
234 * We believe that this is safe, that negative dentries cannot be
235 * pinned by someone else */
236 if (pb->pb_ino == NULL) {
237 CDEBUG(D_INODE, "negative pb\n");
241 /* This is due to bad interaction with libsysio. remove this when we
242 * switched to libbsdio XXX
245 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
246 struct intnl_stat *st = llu_i2stat(pb->pb_ino);
248 CDEBUG(D_INODE, "inode %llu still have intent "
249 "%p(opc 0x%x), release it\n",
250 (long long) st->st_ino, lli->lli_it,
252 ll_intent_release(lli->lli_it);
253 OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
258 exp = llu_i2mdexp(pb->pb_ino);
259 icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
260 icbd.icbd_child = pnode;
264 it->it_op_release = ll_intent_release;
267 llu_prep_md_op_data(&op_data, pnode->p_parent->p_base->pb_ino,
268 pb->pb_ino, pb->pb_name.name, pb->pb_name.len,
271 rc = md_intent_lock(exp, &op_data, NULL, 0, it, flags,
272 &req, llu_md_blocking_ast,
273 LDLM_FL_CANCEL_ON_BLOCK);
274 /* If req is NULL, then md_intent_lock only tried to do a lock match;
275 * if all was well, it will return 1 if it found locks, 0 otherwise. */
276 if (req == NULL && rc >= 0)
282 rc = pnode_revalidate_finish(req, it, pnode);
284 ll_intent_release(it);
289 /* Note: ll_intent_lock may cause a callback, check this! */
291 if (it->it_op & IT_OPEN)
292 LL_SAVE_INTENT(pb->pb_ino, it);
296 ptlrpc_req_finished(req);
302 llu_lookup_finish_locks(it, pnode);
307 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
308 struct lookup_intent *it, void *data)
310 struct it_cb_data *icbd = data;
311 struct pnode *child = icbd->icbd_child;
312 struct inode *parent = icbd->icbd_parent;
313 struct llu_sb_info *sbi = llu_i2sbi(parent);
314 struct inode *inode = NULL;
317 /* libsysio require us generate inode right away if success.
318 * so if mds created new inode for us we need make sure it
319 * succeeded. thus for any error we can't delay to the
320 * llu_file_open() time. */
321 if (it_disposition(it, DISP_OPEN_CREATE) &&
322 it_open_error(DISP_OPEN_CREATE, it)) {
323 CDEBUG(D_INODE, "detect mds create error\n");
324 return it_open_error(DISP_OPEN_CREATE, it);
326 if (it_disposition(it, DISP_OPEN_OPEN) &&
327 it_open_error(DISP_OPEN_OPEN, it)) {
328 CDEBUG(D_INODE, "detect mds open error\n");
329 /* undo which did by md_intent_lock */
330 if (it_disposition(it, DISP_OPEN_CREATE) &&
331 !it_open_error(DISP_OPEN_CREATE, it)) {
333 LASSERT(cfs_atomic_read(&request->rq_refcount) > 1);
334 CDEBUG(D_INODE, "dec a ref of req %p\n", request);
335 ptlrpc_req_finished(request);
337 return it_open_error(DISP_OPEN_OPEN, it);
340 /* NB 1 request reference will be taken away by ll_intent_lock()
343 if (!it_disposition(it, DISP_LOOKUP_NEG) || (it->it_op & IT_CREAT)) {
345 struct llu_inode_info *lli;
346 struct intnl_stat *st;
349 if (it_disposition(it, DISP_OPEN_CREATE))
350 ptlrpc_req_finished(request);
352 rc = md_get_lustre_md(sbi->ll_md_exp, request,
353 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
357 inode = llu_iget(parent->i_fs, &md);
358 if (!inode || IS_ERR(inode)) {
359 /* free the lsm if we allocated one above */
361 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
362 RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
363 } else if (md.lsm != NULL &&
364 llu_i2info(inode)->lli_smd != md.lsm) {
365 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
368 lli = llu_i2info(inode);
369 st = llu_i2stat(inode);
371 /* If this is a stat, get the authoritative file size */
372 if (it->it_op == IT_GETATTR && S_ISREG(st->st_mode) &&
373 lli->lli_smd != NULL) {
374 struct lov_stripe_md *lsm = lli->lli_smd;
377 LASSERT(lsm->lsm_object_id != 0);
379 /* bug 2334: drop MDS lock before acquiring OST lock */
380 ll_intent_drop_lock(it);
382 rc = cl_glimpse_size(inode);
392 /* intent will be further used in cases of open()/getattr() */
393 if (inode && (it->it_op & IT_OPEN))
394 LL_SAVE_INTENT(inode, it);
396 child->p_base->pb_ino = inode;
401 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
404 lock_res_and_lock(lock);
406 if (lock->l_ast_data) {
407 inode = (struct inode *)lock->l_ast_data;
412 unlock_res_and_lock(lock);
416 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
417 struct lookup_intent *it, int flags)
419 struct md_op_data op_data = {{ 0 }};
420 struct it_cb_data icbd;
421 struct ptlrpc_request *req = NULL;
422 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
427 if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
428 RETURN(-ENAMETOOLONG);
432 it->it_op_release = ll_intent_release;
435 icbd.icbd_child = pnode;
436 icbd.icbd_parent = parent;
438 if (it->it_op & IT_CREAT ||
439 (it->it_op & IT_OPEN && it->it_create_mode & O_CREAT)) {
440 opc = LUSTRE_OPC_CREATE;
442 opc = LUSTRE_OPC_ANY;
445 llu_prep_md_op_data(&op_data, parent, NULL,
446 pnode->p_base->pb_name.name,
447 pnode->p_base->pb_name.len, flags, opc);
449 rc = md_intent_lock(llu_i2mdexp(parent), &op_data, NULL, 0, it,
450 flags, &req, llu_md_blocking_ast,
451 LDLM_FL_CANCEL_ON_BLOCK);
455 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
457 ll_intent_release(it);
461 llu_lookup_finish_locks(it, pnode);
465 ptlrpc_req_finished(req);
469 static struct lookup_intent*
470 translate_lookup_intent(struct intent *intent, const char *path)
472 struct lookup_intent *it;
476 if (!intent || path) {
477 CDEBUG(D_VFSTRACE, "not intent needed\n");
481 OBD_ALLOC(it, sizeof(*it));
484 memset(it, 0, sizeof(*it));
486 /* libsysio will assign intent like following:
487 * NOTE: INT_CREAT has include INT_UPDPARENT
489 * open: INT_OPEN [| INT_CREAT]
492 * unlink: INT_UPDPARENT
493 * rmdir: INT_UPDPARENT
498 * following logic is adjusted for libsysio
501 it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
503 if (intent->int_opmask & INT_OPEN) {
504 it->it_op |= IT_OPEN;
506 /* convert access mode from O_ to FMODE_ */
507 if (it->it_flags & O_WRONLY)
509 else if (it->it_flags & O_RDWR)
510 fmode = FMODE_READ | FMODE_WRITE;
513 it->it_flags &= ~O_ACCMODE;
514 it->it_flags |= fmode;
517 /* XXX libsysio has strange code on intent handling,
518 * more check later */
519 if (it->it_flags & O_CREAT) {
520 it->it_op |= IT_CREAT;
521 it->it_create_mode = *((int*)intent->int_arg1);
522 /* bug 7278: libsysio hack. For O_EXCL, libsysio depends on
523 this lookup to return negative result, but then there is no
524 way to find out original intent in ll_iop_open(). So we just
525 clear O_EXCL from libsysio flags here to avoid checking
526 for negative result. O_EXCL will be enforced by MDS. */
527 *((int*)intent->int_arg2) &= ~O_EXCL;
530 if (intent->int_opmask & INT_GETATTR)
531 it->it_op |= IT_GETATTR;
533 LASSERT(!(intent->int_opmask & INT_SETATTR));
535 /* libsysio is different to linux vfs when doing unlink/rmdir,
536 * INT_UPDPARENT was passed down during name resolution. Here
537 * we treat it as normal lookup, later unlink()/rmdir() will
538 * do the actual work */
540 /* conform to kernel code, if only IT_LOOKUP was set, don't
542 if (!it->it_op || it->it_op & IT_LOOKUP) {
543 OBD_FREE(it, sizeof(*it));
547 it->it_op_release = ll_intent_release;
549 CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
553 int llu_iop_lookup(struct pnode *pnode,
555 struct intent *intnt,
558 struct lookup_intent *it;
562 liblustre_wait_event(0);
566 /* the mount root inode have no name, so don't call
567 * remote in this case. but probably we need revalidate
569 if (pnode->p_mount->mnt_root == pnode) {
570 struct inode *i = pnode->p_base->pb_ino;
575 if (!pnode->p_base->pb_name.len)
578 it = translate_lookup_intent(intnt, path);
580 /* param flags is not used, let it be 0 */
581 if (llu_pb_revalidate(pnode, 0, it)) {
582 LASSERT(pnode->p_base->pb_ino);
583 *inop = pnode->p_base->pb_ino;
587 rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
589 if (!pnode->p_base->pb_ino)
592 *inop = pnode->p_base->pb_ino;
597 OBD_FREE(it, sizeof(*it));
598 liblustre_wait_event(0);