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>
32 #include <sys/fcntl.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 && LUSTRE_IT(it)->it_lock_mode) {
55 handle = (struct lustre_handle *)&LUSTRE_IT(it)->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, LUSTRE_IT(it)->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 LUSTRE_IT(it)->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 LUSTRE_IT(it)->it_disposition = 0;
74 LUSTRE_IT(it)->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)
110 if (it && pnode->p_base->pb_ino != NULL) {
111 struct inode *inode = pnode->p_base->pb_ino;
112 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%lu)\n",
113 inode, llu_i2info(inode)->lli_st_ino,
114 llu_i2info(inode)->lli_st_generation);
115 mdc_set_lock_data(NULL, &LUSTRE_IT(it)->it_lock_handle, inode);
118 /* drop lookup/getattr locks */
119 if (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)
120 ll_intent_release(it);
124 static inline void llu_invalidate_inode_pages(struct inode * inode)
129 int llu_mdc_blocking_ast(struct ldlm_lock *lock,
130 struct ldlm_lock_desc *desc,
131 void *data, int flag)
134 struct lustre_handle lockh;
139 case LDLM_CB_BLOCKING:
140 ldlm_lock2handle(lock, &lockh);
141 rc = ldlm_cli_cancel(&lockh);
143 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
147 case LDLM_CB_CANCELING: {
148 struct inode *inode = llu_inode_from_lock(lock);
149 struct llu_inode_info *lli;
150 __u64 bits = lock->l_policy_data.l_inodebits.bits;
152 /* Invalidate all dentries associated with this inode */
156 lli = llu_i2info(inode);
158 if (bits & MDS_INODELOCK_UPDATE)
159 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
161 if (lock->l_resource->lr_name.name[0] != id_fid(&lli->lli_id) ||
162 lock->l_resource->lr_name.name[1] != id_group(&lli->lli_id)) {
163 LDLM_ERROR(lock, "data mismatch with object "DLID4,
164 OLID4(&lli->lli_id));
166 if (S_ISDIR(lli->lli_st_mode) &&
167 (bits & MDS_INODELOCK_UPDATE)) {
168 CDEBUG(D_INODE, "invalidating inode %lu\n",
171 llu_invalidate_inode_pages(inode);
184 static int pnode_revalidate_finish(struct ptlrpc_request *req,
186 struct lookup_intent *it,
189 struct inode *inode = pnode->p_base->pb_ino;
199 if (it_disposition(it, DISP_LOOKUP_NEG))
202 rc = mdc_req2lustre_md(llu_i2sbi(inode)->ll_md_exp, req, offset,
203 llu_i2sbi(inode)->ll_dt_exp, &md);
207 llu_update_inode(inode, md.body, md.lsm);
212 int llu_pb_revalidate(struct pnode *pnode, int flags, struct lookup_intent *it)
214 struct pnode_base *pb = pnode->p_base;
215 struct lustre_id pid, cid;
216 struct it_cb_data icbd;
217 struct ptlrpc_request *req = NULL;
218 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
219 struct obd_export *exp;
223 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
224 (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
226 /* We don't want to cache negative dentries, so return 0 immediately.
227 * We believe that this is safe, that negative dentries cannot be
228 * pinned by someone else */
229 if (pb->pb_ino == NULL) {
230 CDEBUG(D_INODE, "negative pb\n");
234 /* This is due to bad interaction with libsysio. remove this when we
235 * switched to libbsdio XXX
238 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
240 CDEBUG(D_INODE, "inode %lu still have intent "
241 "%p(opc 0x%x), release it\n",
242 lli->lli_st_ino, lli->lli_it,
244 ll_intent_release(lli->lli_it);
245 OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
250 exp = llu_i2mdexp(pb->pb_ino);
251 ll_inode2id(&pid, pnode->p_parent->p_base->pb_ino);
252 ll_inode2id(&cid, pb->pb_ino);
253 icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
254 icbd.icbd_child = pnode;
258 it->it_op_release = ll_intent_release;
261 rc = mdc_intent_lock(exp, &pid, pb->pb_name.name, pb->pb_name.len,
262 NULL, 0, &cid, it, flags, &req, llu_mdc_blocking_ast);
263 /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
264 * if all was well, it will return 1 if it found locks, 0 otherwise. */
265 if (req == NULL && rc >= 0)
271 rc = pnode_revalidate_finish(req, 1, it, pnode);
273 ll_intent_release(it);
278 /* Note: ll_intent_lock may cause a callback, check this! */
280 if (it->it_op & IT_OPEN)
281 LL_SAVE_INTENT(pb->pb_ino, it);
285 ptlrpc_req_finished(req);
291 llu_lookup_finish_locks(it, pnode);
296 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
297 struct lookup_intent *it, void *data)
299 struct it_cb_data *icbd = data;
300 struct pnode *child = icbd->icbd_child;
301 struct inode *parent = icbd->icbd_parent;
302 struct llu_sb_info *sbi = llu_i2sbi(parent);
303 struct inode *inode = NULL;
306 /* libsysio require us generate inode right away if success.
307 * so if mds created new inode for us we need make sure it
308 * succeeded. thus for any error we can't delay to the
309 * llu_file_open() time. */
310 if (it_disposition(it, DISP_OPEN_CREATE) &&
311 it_open_error(DISP_OPEN_CREATE, it)) {
312 CDEBUG(D_INODE, "detect mds create error\n");
313 return it_open_error(DISP_OPEN_CREATE, it);
315 if (it_disposition(it, DISP_OPEN_OPEN) &&
316 it_open_error(DISP_OPEN_OPEN, it)) {
317 CDEBUG(D_INODE, "detect mds open error\n");
318 /* undo which did by mdc_intent_lock */
319 if (it_disposition(it, DISP_OPEN_CREATE) &&
320 !it_open_error(DISP_OPEN_CREATE, it)) {
322 LASSERT(atomic_read(&request->rq_refcount) > 1);
323 CDEBUG(D_INODE, "dec a ref of req %p\n", request);
324 ptlrpc_req_finished(request);
326 return it_open_error(DISP_OPEN_OPEN, it);
329 /* NB 1 request reference will be taken away by ll_intent_lock()
332 if (!it_disposition(it, DISP_LOOKUP_NEG) ||
333 (it->it_op & IT_CREAT)) {
335 struct llu_inode_info *lli;
338 rc = mdc_req2lustre_md(sbi->ll_md_exp, request, offset,
339 sbi->ll_dt_exp, &md);
343 inode = llu_iget(parent->i_fs, &md);
344 if (!inode || IS_ERR(inode)) {
345 /* free the lsm if we allocated one above */
347 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
348 RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
349 } else if (md.lsm != NULL &&
350 llu_i2info(inode)->lli_smd != md.lsm) {
351 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
354 lli = llu_i2info(inode);
356 /* If this is a stat, get the authoritative file size */
357 if (it->it_op == IT_GETATTR && S_ISREG(lli->lli_st_mode) &&
358 lli->lli_smd != NULL) {
359 struct lov_stripe_md *lsm = lli->lli_smd;
362 LASSERT(lsm->lsm_object_id != 0);
364 /* bug 2334: drop MDS lock before acquiring OST lock */
365 ll_intent_drop_lock(it);
367 rc = llu_glimpse_size(inode);
377 /* intent will be further used in cases of open()/getattr() */
378 if (inode && (it->it_op & IT_OPEN))
379 LL_SAVE_INTENT(inode, it);
381 child->p_base->pb_ino = inode;
385 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
388 #warning "fix l_lock() using here!"
389 // l_lock(&lock->l_resource->lr_namespace->ns_lock);
391 if (lock->l_ast_data) {
392 inode = (struct inode *)lock->l_ast_data;
397 // l_unlock(&lock->l_resource->lr_namespace->ns_lock);
401 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
402 struct lookup_intent *it, int flags)
404 struct lustre_id pid;
405 struct it_cb_data icbd;
406 struct ptlrpc_request *req = NULL;
407 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
411 if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
412 RETURN(-ENAMETOOLONG);
416 it->it_op_release = ll_intent_release;
419 icbd.icbd_child = pnode;
420 icbd.icbd_parent = parent;
421 icbd.icbd_child = pnode;
422 ll_inode2id(&pid, parent);
424 rc = mdc_intent_lock(llu_i2mdexp(parent), &pid,
425 pnode->p_base->pb_name.name,
426 pnode->p_base->pb_name.len,
427 NULL, 0, NULL, it, flags, &req,
428 llu_mdc_blocking_ast);
432 rc = lookup_it_finish(req, 1, it, &icbd);
434 ll_intent_release(it);
438 llu_lookup_finish_locks(it, pnode);
442 ptlrpc_req_finished(req);
446 static struct lookup_intent*
447 translate_lookup_intent(struct intent *intent, const char *path)
449 struct lookup_intent *it;
453 if (!intent || path) {
454 CDEBUG(D_VFSTRACE, "not intent needed\n");
458 OBD_ALLOC(it, sizeof(*it));
461 memset(it, 0, sizeof(*it));
463 /* libsysio will assign intent like following:
464 * NOTE: INT_CREAT has include INT_UPDPARENT
466 * open: INT_OPEN [| INT_CREAT]
469 * unlink: INT_UPDPARENT
470 * rmdir: INT_UPDPARENT
475 * following logic is adjusted for libsysio
478 it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
480 if (intent->int_opmask & INT_OPEN) {
481 it->it_op |= IT_OPEN;
483 /* convert access mode from O_ to FMODE_ */
484 if (it->it_flags & O_WRONLY)
486 else if (it->it_flags & O_RDWR)
487 fmode = FMODE_READ | FMODE_WRITE;
490 it->it_flags &= ~O_ACCMODE;
491 it->it_flags |= fmode;
494 /* XXX libsysio has strange code on intent handling,
495 * more check later */
496 if (it->it_flags & O_CREAT) {
497 it->it_op |= IT_CREAT;
498 it->it_create_mode = *((int*)intent->int_arg1);
501 if (intent->int_opmask & INT_GETATTR)
502 it->it_op |= IT_GETATTR;
504 LASSERT(!(intent->int_opmask & INT_SETATTR));
506 /* libsysio is different to linux vfs when doing unlink/rmdir,
507 * INT_UPDPARENT was passed down during name resolution. Here
508 * we treat it as normal lookup, later unlink()/rmdir() will
509 * do the actual work */
511 /* conform to kernel code, if only IT_LOOKUP was set, don't
513 if (!it->it_op || it->it_op == IT_LOOKUP) {
514 OBD_FREE(it, sizeof(*it));
518 it->it_op_release = ll_intent_release;
520 CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
524 int llu_iop_lookup(struct pnode *pnode,
526 struct intent *intnt,
529 struct lookup_intent *it;
533 liblustre_wait_event(0);
537 /* the mount root inode have no name, so don't call
538 * remote in this case. but probably we need revalidate
540 if (pnode->p_mount->mnt_root == pnode) {
541 struct inode *i = pnode->p_base->pb_ino;
546 if (!pnode->p_base->pb_name.len)
549 it = translate_lookup_intent(intnt, path);
551 /* param flags is not used, let it be 0 */
552 if (llu_pb_revalidate(pnode, 0, it)) {
553 LASSERT(pnode->p_base->pb_ino);
554 *inop = pnode->p_base->pb_ino;
558 rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
560 if (!pnode->p_base->pb_ino)
563 *inop = pnode->p_base->pb_ino;