1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Light name resolution
6 * Copyright (c) 2002-2004 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_LLITE
30 #include <sys/types.h>
33 #include <sys/queue.h>
48 #include "llite_lib.h"
50 static void ll_intent_drop_lock(struct lookup_intent *it)
52 struct lustre_handle *handle;
54 if (it->it_op && it->d.lustre.it_lock_mode) {
55 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
56 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
57 " from it %p\n", handle->cookie, it);
58 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
60 /* bug 494: intent_release may be called multiple times, from
61 * this thread and we don't want to double-decref this lock */
62 it->d.lustre.it_lock_mode = 0;
66 static void ll_intent_release(struct lookup_intent *it)
70 ll_intent_drop_lock(it);
72 it->it_op_release = 0;
73 it->d.lustre.it_disposition = 0;
74 it->d.lustre.it_data = NULL;
80 * remove the stale inode from pnode
82 void unhook_stale_inode(struct pnode *pno)
84 struct inode *inode = pno->p_base->pb_ino;
88 LASSERT(llu_i2info(inode)->lli_stale_flag);
90 pno->p_base->pb_ino = NULL;
93 if (!llu_i2info(inode)->lli_open_count) {
94 CDEBUG(D_INODE, "unhook inode %p (ino %lu) from pno %p\n",
95 inode, llu_i2info(inode)->lli_st_ino, pno);
105 void llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
107 struct llu_sb_info *sbi;
112 inode = pnode->p_base->pb_ino;
113 sbi = llu_i2sbi(inode);
114 if (it->d.lustre.it_lock_mode && inode != NULL) {
115 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%llu/%lu)\n",
116 inode, (long long)llu_i2stat(inode)->st_ino,
117 llu_i2info(inode)->lli_st_generation);
118 md_set_lock_data(sbi->ll_md_exp,
119 &it->d.lustre.it_lock_handle, inode);
122 /* drop lookup/getattr locks */
123 if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)
124 ll_intent_release(it);
128 static inline void llu_invalidate_inode_pages(struct inode * inode)
133 int llu_mdc_blocking_ast(struct ldlm_lock *lock,
134 struct ldlm_lock_desc *desc,
135 void *data, int flag)
137 struct lustre_handle lockh;
143 case LDLM_CB_BLOCKING:
144 ldlm_lock2handle(lock, &lockh);
145 rc = ldlm_cli_cancel(&lockh);
147 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
151 case LDLM_CB_CANCELING: {
152 struct inode *inode = llu_inode_from_lock(lock);
153 struct llu_inode_info *lli;
154 struct intnl_stat *st;
155 __u64 bits = lock->l_policy_data.l_inodebits.bits;
157 /* Invalidate all dentries associated with this inode */
161 lli = llu_i2info(inode);
162 st = llu_i2stat(inode);
164 if (bits & MDS_INODELOCK_UPDATE)
165 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
167 if (lock->l_resource->lr_name.name[0] != fid_seq(&lli->lli_fid) ||
168 lock->l_resource->lr_name.name[1] != fid_num(&lli->lli_fid)) {
169 LDLM_ERROR(lock, "data mismatch with ino %llu/%llu",
170 (long long)fid_seq(&lli->lli_fid),
171 (long long)fid_num(&lli->lli_fid));
173 if (S_ISDIR(st->st_mode) &&
174 (bits & MDS_INODELOCK_UPDATE)) {
175 CDEBUG(D_INODE, "invalidating inode %llu\n",
176 (long long)st->st_ino);
178 llu_invalidate_inode_pages(inode);
182 if (inode->i_sb->s_root &&
183 inode != inode->i_sb->s_root->d_inode)
184 ll_unhash_aliases(inode);
196 static int pnode_revalidate_finish(struct ptlrpc_request *req,
198 struct lookup_intent *it,
201 struct inode *inode = pnode->p_base->pb_ino;
211 if (it_disposition(it, DISP_LOOKUP_NEG))
214 rc = md_get_lustre_md(llu_i2sbi(inode)->ll_md_exp, req,
215 offset, llu_i2sbi(inode)->ll_dt_exp, &md);
219 llu_update_inode(inode, md.body, md.lsm);
224 static int llu_pb_revalidate(struct pnode *pnode, int flags,
225 struct lookup_intent *it)
227 struct pnode_base *pb = pnode->p_base;
228 struct it_cb_data icbd;
229 struct md_op_data op_data;
230 struct ptlrpc_request *req = NULL;
231 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
232 struct obd_export *exp;
236 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
237 (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
239 /* We don't want to cache negative dentries, so return 0 immediately.
240 * We believe that this is safe, that negative dentries cannot be
241 * pinned by someone else */
242 if (pb->pb_ino == NULL) {
243 CDEBUG(D_INODE, "negative pb\n");
247 /* This is due to bad interaction with libsysio. remove this when we
248 * switched to libbsdio XXX
251 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
252 struct intnl_stat *st = llu_i2stat(pb->pb_ino);
254 CDEBUG(D_INODE, "inode %llu still have intent "
255 "%p(opc 0x%x), release it\n",
256 (long long) st->st_ino, lli->lli_it,
258 ll_intent_release(lli->lli_it);
259 OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
264 exp = llu_i2mdcexp(pb->pb_ino);
265 icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
266 icbd.icbd_child = pnode;
270 it->it_op_release = ll_intent_release;
273 llu_prepare_mdc_op_data(&op_data, pnode->p_parent->p_base->pb_ino,
274 pb->pb_ino, pb->pb_name.name,pb->pb_name.len,0);
276 rc = md_intent_lock(exp, &op_data, NULL, 0, it, flags,
277 &req, llu_mdc_blocking_ast,
278 LDLM_FL_CANCEL_ON_BLOCK);
279 /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
280 * if all was well, it will return 1 if it found locks, 0 otherwise. */
281 if (req == NULL && rc >= 0)
287 rc = pnode_revalidate_finish(req, 1, it, pnode);
289 ll_intent_release(it);
294 /* Note: ll_intent_lock may cause a callback, check this! */
296 if (it->it_op & IT_OPEN)
297 LL_SAVE_INTENT(pb->pb_ino, it);
301 ptlrpc_req_finished(req);
307 llu_lookup_finish_locks(it, pnode);
312 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
313 struct lookup_intent *it, void *data)
315 struct it_cb_data *icbd = data;
316 struct pnode *child = icbd->icbd_child;
317 struct inode *parent = icbd->icbd_parent;
318 struct llu_sb_info *sbi = llu_i2sbi(parent);
319 struct inode *inode = NULL;
322 /* libsysio require us generate inode right away if success.
323 * so if mds created new inode for us we need make sure it
324 * succeeded. thus for any error we can't delay to the
325 * llu_file_open() time. */
326 if (it_disposition(it, DISP_OPEN_CREATE) &&
327 it_open_error(DISP_OPEN_CREATE, it)) {
328 CDEBUG(D_INODE, "detect mds create error\n");
329 return it_open_error(DISP_OPEN_CREATE, it);
331 if (it_disposition(it, DISP_OPEN_OPEN) &&
332 it_open_error(DISP_OPEN_OPEN, it)) {
333 CDEBUG(D_INODE, "detect mds open error\n");
334 /* undo which did by mdc_intent_lock */
335 if (it_disposition(it, DISP_OPEN_CREATE) &&
336 !it_open_error(DISP_OPEN_CREATE, it)) {
338 LASSERT(atomic_read(&request->rq_refcount) > 1);
339 CDEBUG(D_INODE, "dec a ref of req %p\n", request);
340 ptlrpc_req_finished(request);
342 return it_open_error(DISP_OPEN_OPEN, it);
345 /* NB 1 request reference will be taken away by ll_intent_lock()
348 if (!it_disposition(it, DISP_LOOKUP_NEG) ||
349 (it->it_op & IT_CREAT)) {
351 struct llu_inode_info *lli;
352 struct intnl_stat *st;
355 if (it_disposition(it, DISP_OPEN_CREATE))
356 ptlrpc_req_finished(request);
358 rc = md_get_lustre_md(sbi->ll_md_exp, request, offset,
359 sbi->ll_dt_exp, &md);
363 inode = llu_iget(parent->i_fs, &md);
364 if (!inode || IS_ERR(inode)) {
365 /* free the lsm if we allocated one above */
367 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
368 RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
369 } else if (md.lsm != NULL &&
370 llu_i2info(inode)->lli_smd != md.lsm) {
371 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
374 lli = llu_i2info(inode);
375 st = llu_i2stat(inode);
377 /* If this is a stat, get the authoritative file size */
378 if (it->it_op == IT_GETATTR && S_ISREG(st->st_mode) &&
379 lli->lli_smd != NULL) {
380 struct lov_stripe_md *lsm = lli->lli_smd;
383 LASSERT(lsm->lsm_object_id != 0);
385 /* bug 2334: drop MDS lock before acquiring OST lock */
386 ll_intent_drop_lock(it);
388 rc = llu_glimpse_size(inode);
398 /* intent will be further used in cases of open()/getattr() */
399 if (inode && (it->it_op & IT_OPEN))
400 LL_SAVE_INTENT(inode, it);
402 child->p_base->pb_ino = inode;
407 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
410 l_lock(&lock->l_resource->lr_namespace->ns_lock);
412 if (lock->l_ast_data) {
413 inode = (struct inode *)lock->l_ast_data;
418 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
422 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
423 struct lookup_intent *it, int flags)
425 struct md_op_data op_data = { { 0 } };
426 struct it_cb_data icbd;
427 struct ptlrpc_request *req = NULL;
428 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
432 if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
433 RETURN(-ENAMETOOLONG);
437 it->it_op_release = ll_intent_release;
440 icbd.icbd_child = pnode;
441 icbd.icbd_parent = parent;
443 /* allocate new fid for child */
444 if (it->it_op == IT_OPEN || it->it_op == IT_CREAT) {
445 rc = llu_fid_md_alloc(llu_i2sbi(parent), &op_data.fid2);
447 CERROR("can't allocate new fid, rc %d\n", rc);
451 llu_prepare_mdc_op_data(&op_data, parent, NULL,
452 pnode->p_base->pb_name.name,
453 pnode->p_base->pb_name.len, flags);
455 rc = md_intent_lock(llu_i2mdcexp(parent), &op_data, NULL, 0, it,
456 flags, &req, llu_mdc_blocking_ast,
457 LDLM_FL_CANCEL_ON_BLOCK);
461 rc = lookup_it_finish(req, 1, it, &icbd);
463 ll_intent_release(it);
467 llu_lookup_finish_locks(it, pnode);
471 ptlrpc_req_finished(req);
475 static struct lookup_intent*
476 translate_lookup_intent(struct intent *intent, const char *path)
478 struct lookup_intent *it;
482 if (!intent || path) {
483 CDEBUG(D_VFSTRACE, "not intent needed\n");
487 OBD_ALLOC(it, sizeof(*it));
490 memset(it, 0, sizeof(*it));
492 /* libsysio will assign intent like following:
493 * NOTE: INT_CREAT has include INT_UPDPARENT
495 * open: INT_OPEN [| INT_CREAT]
498 * unlink: INT_UPDPARENT
499 * rmdir: INT_UPDPARENT
504 * following logic is adjusted for libsysio
507 it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
509 if (intent->int_opmask & INT_OPEN) {
510 it->it_op |= IT_OPEN;
512 /* convert access mode from O_ to FMODE_ */
513 if (it->it_flags & O_WRONLY)
515 else if (it->it_flags & O_RDWR)
516 fmode = FMODE_READ | FMODE_WRITE;
519 it->it_flags &= ~O_ACCMODE;
520 it->it_flags |= fmode;
523 /* XXX libsysio has strange code on intent handling,
524 * more check later */
525 if (it->it_flags & O_CREAT) {
526 it->it_op |= IT_CREAT;
527 it->it_create_mode = *((int*)intent->int_arg1);
528 /* bug 7278: libsysio hack. For O_EXCL, libsysio depends on
529 this lookup to return negative result, but then there is no
530 way to find out original intent in ll_iop_open(). So we just
531 clear O_EXCL from libsysio flags here to avoid checking
532 for negative result. O_EXCL will be enforced by MDS. */
533 *((int*)intent->int_arg2) &= ~O_EXCL;
536 if (intent->int_opmask & INT_GETATTR)
537 it->it_op |= IT_GETATTR;
539 LASSERT(!(intent->int_opmask & INT_SETATTR));
541 /* libsysio is different to linux vfs when doing unlink/rmdir,
542 * INT_UPDPARENT was passed down during name resolution. Here
543 * we treat it as normal lookup, later unlink()/rmdir() will
544 * do the actual work */
546 /* conform to kernel code, if only IT_LOOKUP was set, don't
548 if (!it->it_op || it->it_op == IT_LOOKUP) {
549 OBD_FREE(it, sizeof(*it));
553 it->it_op_release = ll_intent_release;
555 CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
559 int llu_iop_lookup(struct pnode *pnode,
561 struct intent *intnt,
564 struct lookup_intent *it;
568 liblustre_wait_event(0);
572 /* the mount root inode have no name, so don't call
573 * remote in this case. but probably we need revalidate
575 if (pnode->p_mount->mnt_root == pnode) {
576 struct inode *i = pnode->p_base->pb_ino;
581 if (!pnode->p_base->pb_name.len)
584 it = translate_lookup_intent(intnt, path);
586 /* param flags is not used, let it be 0 */
587 if (llu_pb_revalidate(pnode, 0, it)) {
588 LASSERT(pnode->p_base->pb_ino);
589 *inop = pnode->p_base->pb_ino;
593 rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
595 if (!pnode->p_base->pb_ino)
598 *inop = pnode->p_base->pb_ino;
602 liblustre_wait_event(0);