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)
111 inode = pnode->p_base->pb_ino;
112 if (it->d.lustre.it_lock_mode && inode != NULL) {
113 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%llu/%lu)\n",
114 inode, (long long)llu_i2stat(inode)->st_ino,
115 llu_i2info(inode)->lli_st_generation);
116 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
119 /* drop lookup/getattr locks */
120 if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)
121 ll_intent_release(it);
125 static inline void llu_invalidate_inode_pages(struct inode * inode)
130 int llu_mdc_blocking_ast(struct ldlm_lock *lock,
131 struct ldlm_lock_desc *desc,
132 void *data, int flag)
135 struct lustre_handle lockh;
140 case LDLM_CB_BLOCKING:
141 ldlm_lock2handle(lock, &lockh);
142 rc = ldlm_cli_cancel(&lockh);
144 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
148 case LDLM_CB_CANCELING: {
149 struct inode *inode = llu_inode_from_lock(lock);
150 struct llu_inode_info *lli;
151 struct intnl_stat *st;
153 /* Invalidate all dentries associated with this inode */
157 lli = llu_i2info(inode);
158 st = llu_i2stat(inode);
160 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
162 if (lock->l_resource->lr_name.name[0] != st->st_ino ||
163 lock->l_resource->lr_name.name[1] !=lli->lli_st_generation){
164 LDLM_ERROR(lock, "data mismatch with ino %llu/%lu",
165 (long long)st->st_ino,lli->lli_st_generation);
167 if (S_ISDIR(st->st_mode)) {
168 CDEBUG(D_INODE, "invalidating inode %llu\n",
169 (long long)st->st_ino);
171 llu_invalidate_inode_pages(inode);
175 if (inode->i_sb->s_root &&
176 inode != inode->i_sb->s_root->d_inode)
177 ll_unhash_aliases(inode);
189 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 = mdc_req2lustre_md(req, offset, llu_i2sbi(inode)->ll_osc_exp, &md);
211 llu_update_inode(inode, md.body, md.lsm);
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 it_cb_data icbd;
221 struct mdc_op_data op_data;
222 struct ptlrpc_request *req = NULL;
223 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
224 struct obd_export *exp;
228 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
229 (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
231 /* We don't want to cache negative dentries, so return 0 immediately.
232 * We believe that this is safe, that negative dentries cannot be
233 * pinned by someone else */
234 if (pb->pb_ino == NULL) {
235 CDEBUG(D_INODE, "negative pb\n");
239 /* This is due to bad interaction with libsysio. remove this when we
240 * switched to libbsdio XXX
243 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
244 struct intnl_stat *st = llu_i2stat(pb->pb_ino);
246 CDEBUG(D_INODE, "inode %llu still have intent "
247 "%p(opc 0x%x), release it\n",
248 (long long) st->st_ino, lli->lli_it,
250 ll_intent_release(lli->lli_it);
251 OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
256 exp = llu_i2mdcexp(pb->pb_ino);
257 icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
258 icbd.icbd_child = pnode;
262 it->it_op_release = ll_intent_release;
265 llu_prepare_mdc_op_data(&op_data, pnode->p_parent->p_base->pb_ino,
266 pb->pb_ino, pb->pb_name.name,pb->pb_name.len,0);
268 rc = mdc_intent_lock(exp, &op_data, NULL, 0, it, flags,
269 &req, llu_mdc_blocking_ast,
270 LDLM_FL_CANCEL_ON_BLOCK);
271 /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
272 * if all was well, it will return 1 if it found locks, 0 otherwise. */
273 if (req == NULL && rc >= 0)
279 rc = pnode_revalidate_finish(req, 1, it, pnode);
281 ll_intent_release(it);
286 /* Note: ll_intent_lock may cause a callback, check this! */
288 if (it->it_op & IT_OPEN)
289 LL_SAVE_INTENT(pb->pb_ino, it);
293 ptlrpc_req_finished(req);
299 llu_lookup_finish_locks(it, pnode);
304 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
305 struct lookup_intent *it, void *data)
307 struct it_cb_data *icbd = data;
308 struct pnode *child = icbd->icbd_child;
309 struct inode *parent = icbd->icbd_parent;
310 struct llu_sb_info *sbi = llu_i2sbi(parent);
311 struct inode *inode = NULL;
314 /* libsysio require us generate inode right away if success.
315 * so if mds created new inode for us we need make sure it
316 * succeeded. thus for any error we can't delay to the
317 * llu_file_open() time. */
318 if (it_disposition(it, DISP_OPEN_CREATE) &&
319 it_open_error(DISP_OPEN_CREATE, it)) {
320 CDEBUG(D_INODE, "detect mds create error\n");
321 return it_open_error(DISP_OPEN_CREATE, it);
323 if (it_disposition(it, DISP_OPEN_OPEN) &&
324 it_open_error(DISP_OPEN_OPEN, it)) {
325 CDEBUG(D_INODE, "detect mds open error\n");
326 /* undo which did by mdc_intent_lock */
327 if (it_disposition(it, DISP_OPEN_CREATE) &&
328 !it_open_error(DISP_OPEN_CREATE, it)) {
330 LASSERT(atomic_read(&request->rq_refcount) > 1);
331 CDEBUG(D_INODE, "dec a ref of req %p\n", request);
332 ptlrpc_req_finished(request);
334 return it_open_error(DISP_OPEN_OPEN, it);
337 /* NB 1 request reference will be taken away by ll_intent_lock()
340 if (!it_disposition(it, DISP_LOOKUP_NEG) ||
341 (it->it_op & IT_CREAT)) {
343 struct llu_inode_info *lli;
344 struct intnl_stat *st;
347 rc = mdc_req2lustre_md(request, offset, sbi->ll_osc_exp, &md);
351 inode = llu_iget(parent->i_fs, &md);
352 if (!inode || IS_ERR(inode)) {
353 /* free the lsm if we allocated one above */
355 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
356 RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
357 } else if (md.lsm != NULL &&
358 llu_i2info(inode)->lli_smd != md.lsm) {
359 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
362 lli = llu_i2info(inode);
363 st = llu_i2stat(inode);
365 /* If this is a stat, get the authoritative file size */
366 if (it->it_op == IT_GETATTR && S_ISREG(st->st_mode) &&
367 lli->lli_smd != NULL) {
368 struct lov_stripe_md *lsm = lli->lli_smd;
371 LASSERT(lsm->lsm_object_id != 0);
373 /* bug 2334: drop MDS lock before acquiring OST lock */
374 ll_intent_drop_lock(it);
376 rc = llu_glimpse_size(inode);
386 /* intent will be further used in cases of open()/getattr() */
387 if (inode && (it->it_op & IT_OPEN))
388 LL_SAVE_INTENT(inode, it);
390 child->p_base->pb_ino = inode;
395 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
398 l_lock(&lock->l_resource->lr_namespace->ns_lock);
400 if (lock->l_ast_data) {
401 inode = (struct inode *)lock->l_ast_data;
406 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
410 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
411 struct lookup_intent *it, int flags)
413 struct mdc_op_data op_data;
414 struct it_cb_data icbd;
415 struct ptlrpc_request *req = NULL;
416 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
420 if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
421 RETURN(-ENAMETOOLONG);
425 it->it_op_release = ll_intent_release;
428 icbd.icbd_child = pnode;
429 icbd.icbd_parent = parent;
431 llu_prepare_mdc_op_data(&op_data, parent, NULL,
432 pnode->p_base->pb_name.name,
433 pnode->p_base->pb_name.len, flags);
435 rc = mdc_intent_lock(llu_i2mdcexp(parent), &op_data, NULL, 0, it,
436 flags, &req, llu_mdc_blocking_ast,
437 LDLM_FL_CANCEL_ON_BLOCK);
441 rc = lookup_it_finish(req, 1, it, &icbd);
443 ll_intent_release(it);
447 llu_lookup_finish_locks(it, pnode);
451 ptlrpc_req_finished(req);
455 static struct lookup_intent*
456 translate_lookup_intent(struct intent *intent, const char *path)
458 struct lookup_intent *it;
462 if (!intent || path) {
463 CDEBUG(D_VFSTRACE, "not intent needed\n");
467 OBD_ALLOC(it, sizeof(*it));
470 memset(it, 0, sizeof(*it));
472 /* libsysio will assign intent like following:
473 * NOTE: INT_CREAT has include INT_UPDPARENT
475 * open: INT_OPEN [| INT_CREAT]
478 * unlink: INT_UPDPARENT
479 * rmdir: INT_UPDPARENT
484 * following logic is adjusted for libsysio
487 it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
489 if (intent->int_opmask & INT_OPEN) {
490 it->it_op |= IT_OPEN;
492 /* convert access mode from O_ to FMODE_ */
493 if (it->it_flags & O_WRONLY)
495 else if (it->it_flags & O_RDWR)
496 fmode = FMODE_READ | FMODE_WRITE;
499 it->it_flags &= ~O_ACCMODE;
500 it->it_flags |= fmode;
503 /* XXX libsysio has strange code on intent handling,
504 * more check later */
505 if (it->it_flags & O_CREAT) {
506 it->it_op |= IT_CREAT;
507 it->it_create_mode = *((int*)intent->int_arg1);
508 /* bug 7278: libsysio hack. For O_EXCL, libsysio depends on
509 this lookup to return negative result, but then there is no
510 way to find out original intent in ll_iop_open(). So we just
511 clear O_EXCL from libsysio flags here to avoid checking
512 for negative result. O_EXCL will be enforced by MDS. */
513 *((int*)intent->int_arg2) &= ~O_EXCL;
516 if (intent->int_opmask & INT_GETATTR)
517 it->it_op |= IT_GETATTR;
519 LASSERT(!(intent->int_opmask & INT_SETATTR));
521 /* libsysio is different to linux vfs when doing unlink/rmdir,
522 * INT_UPDPARENT was passed down during name resolution. Here
523 * we treat it as normal lookup, later unlink()/rmdir() will
524 * do the actual work */
526 /* conform to kernel code, if only IT_LOOKUP was set, don't
528 if (!it->it_op || it->it_op == IT_LOOKUP) {
529 OBD_FREE(it, sizeof(*it));
533 it->it_op_release = ll_intent_release;
535 CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
539 int llu_iop_lookup(struct pnode *pnode,
541 struct intent *intnt,
544 struct lookup_intent *it;
548 liblustre_wait_event(0);
552 /* the mount root inode have no name, so don't call
553 * remote in this case. but probably we need revalidate
555 if (pnode->p_mount->mnt_root == pnode) {
556 struct inode *i = pnode->p_base->pb_ino;
561 if (!pnode->p_base->pb_name.len)
564 it = translate_lookup_intent(intnt, path);
566 /* param flags is not used, let it be 0 */
567 if (llu_pb_revalidate(pnode, 0, it)) {
568 LASSERT(pnode->p_base->pb_ino);
569 *inop = pnode->p_base->pb_ino;
573 rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
575 if (!pnode->p_base->pb_ino)
578 *inop = pnode->p_base->pb_ino;
582 liblustre_wait_event(0);