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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/liblustre/namei.c
40 * Lustre Light name resolution
43 #define DEBUG_SUBSYSTEM S_LLITE
49 #include <sys/types.h>
52 #include <sys/queue.h>
54 #include "llite_lib.h"
56 void ll_intent_drop_lock(struct lookup_intent *it)
58 struct lustre_handle *handle;
60 if (it->it_op && it->d.lustre.it_lock_mode) {
61 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
62 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
63 " from it %p\n", handle->cookie, it);
64 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
66 /* bug 494: intent_release may be called multiple times, from
67 * this thread and we don't want to double-decref this lock */
68 it->d.lustre.it_lock_mode = 0;
72 void ll_intent_release(struct lookup_intent *it)
76 ll_intent_drop_lock(it);
78 it->it_op_release = 0;
79 it->d.lustre.it_disposition = 0;
80 it->d.lustre.it_data = NULL;
84 void llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
90 inode = pnode->p_base->pb_ino;
91 if (it->d.lustre.it_lock_mode && inode != NULL) {
92 struct llu_sb_info *sbi;
94 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%llu/%lu)\n",
95 inode, (long long)llu_i2stat(inode)->st_ino,
96 llu_i2info(inode)->lli_st_generation);
98 sbi = llu_i2sbi(inode);
99 md_set_lock_data(sbi->ll_md_exp,
100 &it->d.lustre.it_lock_handle, inode, NULL);
103 /* drop lookup/getattr locks */
104 if (it->it_op & (IT_LOOKUP | IT_GETATTR))
105 ll_intent_release(it);
109 static inline void llu_invalidate_inode_pages(struct inode * inode)
114 int llu_md_blocking_ast(struct ldlm_lock *lock,
115 struct ldlm_lock_desc *desc,
116 void *data, int flag)
118 struct lustre_handle lockh;
124 case LDLM_CB_BLOCKING:
125 ldlm_lock2handle(lock, &lockh);
126 rc = ldlm_cli_cancel(&lockh);
128 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
132 case LDLM_CB_CANCELING: {
133 struct inode *inode = llu_inode_from_lock(lock);
134 struct llu_inode_info *lli;
135 struct intnl_stat *st;
136 __u64 bits = lock->l_policy_data.l_inodebits.bits;
139 /* Invalidate all dentries associated with this inode */
143 lli = llu_i2info(inode);
144 st = llu_i2stat(inode);
146 if (bits & MDS_INODELOCK_UPDATE)
147 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
150 if (lock->l_resource->lr_name.name[0] != fid_seq(fid) ||
151 lock->l_resource->lr_name.name[1] != fid_oid(fid) ||
152 lock->l_resource->lr_name.name[2] != fid_ver(fid)) {
153 LDLM_ERROR(lock,"data mismatch with ino %llu/%llu/%llu",
154 (long long)fid_seq(fid),
155 (long long)fid_oid(fid),
156 (long long)fid_ver(fid));
158 if (S_ISDIR(st->st_mode) &&
159 (bits & MDS_INODELOCK_UPDATE)) {
160 CDEBUG(D_INODE, "invalidating inode %llu\n",
161 (long long)st->st_ino);
163 llu_invalidate_inode_pages(inode);
167 if (inode->i_sb->s_root &&
168 inode != inode->i_sb->s_root->d_inode)
169 ll_unhash_aliases(inode);
181 static int pnode_revalidate_finish(struct ptlrpc_request *req,
182 struct lookup_intent *it,
185 struct inode *inode = pnode->p_base->pb_ino;
195 if (it_disposition(it, DISP_LOOKUP_NEG))
198 rc = md_get_lustre_md(llu_i2sbi(inode)->ll_md_exp, req,
199 llu_i2sbi(inode)->ll_dt_exp,
200 llu_i2sbi(inode)->ll_md_exp, &md);
204 llu_update_inode(inode, &md);
209 static int llu_pb_revalidate(struct pnode *pnode, int flags,
210 struct lookup_intent *it)
212 struct pnode_base *pb = pnode->p_base;
213 struct md_op_data op_data = {{ 0 }};
214 struct ptlrpc_request *req = NULL;
215 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
216 struct obd_export *exp;
220 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
221 (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
223 /* We don't want to cache negative dentries, so return 0 immediately.
224 * We believe that this is safe, that negative dentries cannot be
225 * pinned by someone else */
226 if (pb->pb_ino == NULL) {
227 CDEBUG(D_INODE, "negative pb\n");
231 /* This is due to bad interaction with libsysio. remove this when we
232 * switched to libbsdio XXX
235 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
236 struct intnl_stat *st = llu_i2stat(pb->pb_ino);
238 CDEBUG(D_INODE, "inode %llu still have intent "
239 "%p(opc 0x%x), release it\n",
240 (long long) st->st_ino, lli->lli_it,
242 ll_intent_release(lli->lli_it);
243 OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
248 exp = llu_i2mdexp(pb->pb_ino);
252 it->it_op_release = ll_intent_release;
255 llu_prep_md_op_data(&op_data, pnode->p_parent->p_base->pb_ino,
256 pb->pb_ino, pb->pb_name.name, pb->pb_name.len,
259 rc = md_intent_lock(exp, &op_data, NULL, 0, it, flags,
260 &req, llu_md_blocking_ast,
261 LDLM_FL_CANCEL_ON_BLOCK);
262 /* If req is NULL, then md_intent_lock only tried to do a lock match;
263 * if all was well, it will return 1 if it found locks, 0 otherwise. */
264 if (req == NULL && rc >= 0)
270 rc = pnode_revalidate_finish(req, it, pnode);
272 ll_intent_release(it);
277 /* Note: ll_intent_lock may cause a callback, check this! */
279 if (it->it_op & IT_OPEN)
280 LL_SAVE_INTENT(pb->pb_ino, it);
284 ptlrpc_req_finished(req);
290 llu_lookup_finish_locks(it, pnode);
295 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
296 struct lookup_intent *it, void *data)
298 struct it_cb_data *icbd = data;
299 struct pnode *child = icbd->icbd_child;
300 struct inode *parent = icbd->icbd_parent;
301 struct llu_sb_info *sbi = llu_i2sbi(parent);
302 struct inode *inode = NULL;
305 /* libsysio require us generate inode right away if success.
306 * so if mds created new inode for us we need make sure it
307 * succeeded. thus for any error we can't delay to the
308 * llu_file_open() time. */
309 if (it_disposition(it, DISP_OPEN_CREATE) &&
310 it_open_error(DISP_OPEN_CREATE, it)) {
311 CDEBUG(D_INODE, "detect mds create error\n");
312 return it_open_error(DISP_OPEN_CREATE, it);
314 if (it_disposition(it, DISP_OPEN_OPEN) &&
315 it_open_error(DISP_OPEN_OPEN, it)) {
316 CDEBUG(D_INODE, "detect mds open error\n");
317 /* undo which did by md_intent_lock */
318 if (it_disposition(it, DISP_OPEN_CREATE) &&
319 !it_open_error(DISP_OPEN_CREATE, it)) {
321 LASSERT(cfs_atomic_read(&request->rq_refcount) > 1);
322 CDEBUG(D_INODE, "dec a ref of req %p\n", request);
323 ptlrpc_req_finished(request);
325 return it_open_error(DISP_OPEN_OPEN, it);
328 /* NB 1 request reference will be taken away by ll_intent_lock()
331 if (!it_disposition(it, DISP_LOOKUP_NEG) || (it->it_op & IT_CREAT)) {
333 struct llu_inode_info *lli;
334 struct intnl_stat *st;
337 if (it_disposition(it, DISP_OPEN_CREATE))
338 ptlrpc_req_finished(request);
340 rc = md_get_lustre_md(sbi->ll_md_exp, request,
341 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
345 inode = llu_iget(parent->i_fs, &md);
346 if (!inode || IS_ERR(inode)) {
347 /* free the lsm if we allocated one above */
349 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
350 RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
351 } else if (md.lsm != NULL &&
352 llu_i2info(inode)->lli_smd != md.lsm) {
353 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
356 lli = llu_i2info(inode);
357 st = llu_i2stat(inode);
359 /* If this is a stat, get the authoritative file size */
360 if (it->it_op == IT_GETATTR && S_ISREG(st->st_mode) &&
361 lli->lli_smd != NULL) {
362 struct lov_stripe_md *lsm = lli->lli_smd;
365 LASSERT(lsm->lsm_object_id != 0);
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_lock(struct ldlm_lock *lock)
392 lock_res_and_lock(lock);
394 if (lock->l_ast_data) {
395 inode = (struct inode *)lock->l_ast_data;
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 it_cb_data icbd;
409 struct ptlrpc_request *req = NULL;
410 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
415 if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
416 RETURN(-ENAMETOOLONG);
420 it->it_op_release = ll_intent_release;
423 icbd.icbd_child = pnode;
424 icbd.icbd_parent = parent;
426 if (it->it_op & IT_CREAT ||
427 (it->it_op & IT_OPEN && it->it_create_mode & O_CREAT)) {
428 opc = LUSTRE_OPC_CREATE;
430 opc = LUSTRE_OPC_ANY;
433 llu_prep_md_op_data(&op_data, parent, NULL,
434 pnode->p_base->pb_name.name,
435 pnode->p_base->pb_name.len, flags, opc);
437 rc = md_intent_lock(llu_i2mdexp(parent), &op_data, NULL, 0, it,
438 flags, &req, llu_md_blocking_ast,
439 LDLM_FL_CANCEL_ON_BLOCK);
443 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
445 ll_intent_release(it);
449 llu_lookup_finish_locks(it, pnode);
453 ptlrpc_req_finished(req);
457 static struct lookup_intent*
458 translate_lookup_intent(struct intent *intent, const char *path)
460 struct lookup_intent *it;
464 if (!intent || path) {
465 CDEBUG(D_VFSTRACE, "not intent needed\n");
469 OBD_ALLOC(it, sizeof(*it));
472 memset(it, 0, sizeof(*it));
474 /* libsysio will assign intent like following:
475 * NOTE: INT_CREAT has include INT_UPDPARENT
477 * open: INT_OPEN [| INT_CREAT]
480 * unlink: INT_UPDPARENT
481 * rmdir: INT_UPDPARENT
486 * following logic is adjusted for libsysio
489 it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
491 if (intent->int_opmask & INT_OPEN) {
492 it->it_op |= IT_OPEN;
494 /* convert access mode from O_ to FMODE_ */
495 if (it->it_flags & O_WRONLY)
497 else if (it->it_flags & O_RDWR)
498 fmode = FMODE_READ | FMODE_WRITE;
501 it->it_flags &= ~O_ACCMODE;
502 it->it_flags |= fmode;
505 /* XXX libsysio has strange code on intent handling,
506 * more check later */
507 if (it->it_flags & O_CREAT) {
508 it->it_op |= IT_CREAT;
509 it->it_create_mode = *((int*)intent->int_arg1);
510 /* bug 7278: libsysio hack. For O_EXCL, libsysio depends on
511 this lookup to return negative result, but then there is no
512 way to find out original intent in ll_iop_open(). So we just
513 clear O_EXCL from libsysio flags here to avoid checking
514 for negative result. O_EXCL will be enforced by MDS. */
515 *((int*)intent->int_arg2) &= ~O_EXCL;
518 if (intent->int_opmask & INT_GETATTR)
519 it->it_op |= IT_GETATTR;
521 LASSERT(!(intent->int_opmask & INT_SETATTR));
523 /* libsysio is different to linux vfs when doing unlink/rmdir,
524 * INT_UPDPARENT was passed down during name resolution. Here
525 * we treat it as normal lookup, later unlink()/rmdir() will
526 * do the actual work */
528 /* conform to kernel code, if only IT_LOOKUP was set, don't
530 if (!it->it_op || it->it_op & IT_LOOKUP) {
531 OBD_FREE(it, sizeof(*it));
535 it->it_op_release = ll_intent_release;
537 CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
541 int llu_iop_lookup(struct pnode *pnode,
543 struct intent *intnt,
546 struct lookup_intent *it;
550 liblustre_wait_event(0);
554 /* the mount root inode have no name, so don't call
555 * remote in this case. but probably we need revalidate
557 if (pnode->p_mount->mnt_root == pnode) {
558 struct inode *i = pnode->p_base->pb_ino;
563 if (!pnode->p_base->pb_name.len)
566 it = translate_lookup_intent(intnt, path);
568 /* param flags is not used, let it be 0 */
569 if (llu_pb_revalidate(pnode, 0, it)) {
570 LASSERT(pnode->p_base->pb_ino);
571 *inop = pnode->p_base->pb_ino;
575 rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
577 if (!pnode->p_base->pb_ino)
580 *inop = pnode->p_base->pb_ino;
585 OBD_FREE(it, sizeof(*it));
586 liblustre_wait_event(0);