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 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 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;
78 void llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
84 inode = pnode->p_base->pb_ino;
85 if (it->d.lustre.it_lock_mode && inode != NULL) {
86 struct llu_sb_info *sbi;
88 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%llu/%lu)\n",
89 inode, (long long)llu_i2stat(inode)->st_ino,
90 llu_i2info(inode)->lli_st_generation);
92 sbi = llu_i2sbi(inode);
93 md_set_lock_data(sbi->ll_md_exp,
94 &it->d.lustre.it_lock_handle, inode);
97 /* drop lookup/getattr locks */
98 if (it->it_op & (IT_LOOKUP | IT_GETATTR))
99 ll_intent_release(it);
103 static inline void llu_invalidate_inode_pages(struct inode * inode)
108 int llu_md_blocking_ast(struct ldlm_lock *lock,
109 struct ldlm_lock_desc *desc,
110 void *data, int flag)
112 struct lustre_handle lockh;
118 case LDLM_CB_BLOCKING:
119 ldlm_lock2handle(lock, &lockh);
120 rc = ldlm_cli_cancel(&lockh);
122 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
126 case LDLM_CB_CANCELING: {
127 struct inode *inode = llu_inode_from_lock(lock);
128 struct llu_inode_info *lli;
129 struct intnl_stat *st;
130 __u64 bits = lock->l_policy_data.l_inodebits.bits;
133 /* Invalidate all dentries associated with this inode */
137 lli = llu_i2info(inode);
138 st = llu_i2stat(inode);
140 if (bits & MDS_INODELOCK_UPDATE)
141 lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
144 if (lock->l_resource->lr_name.name[0] != fid_seq(fid) ||
145 lock->l_resource->lr_name.name[1] != fid_oid(fid) ||
146 lock->l_resource->lr_name.name[2] != fid_ver(fid)) {
147 LDLM_ERROR(lock,"data mismatch with ino %llu/%llu/%llu",
148 (long long)fid_seq(fid),
149 (long long)fid_oid(fid),
150 (long long)fid_ver(fid));
152 if (S_ISDIR(st->st_mode) &&
153 (bits & MDS_INODELOCK_UPDATE)) {
154 CDEBUG(D_INODE, "invalidating inode %llu\n",
155 (long long)st->st_ino);
157 llu_invalidate_inode_pages(inode);
161 if (inode->i_sb->s_root &&
162 inode != inode->i_sb->s_root->d_inode)
163 ll_unhash_aliases(inode);
175 static int pnode_revalidate_finish(struct ptlrpc_request *req,
176 struct lookup_intent *it,
179 struct inode *inode = pnode->p_base->pb_ino;
189 if (it_disposition(it, DISP_LOOKUP_NEG))
192 rc = md_get_lustre_md(llu_i2sbi(inode)->ll_md_exp, req,
193 llu_i2sbi(inode)->ll_dt_exp,
194 llu_i2sbi(inode)->ll_md_exp, &md);
198 llu_update_inode(inode, md.body, md.lsm);
203 static int llu_pb_revalidate(struct pnode *pnode, int flags,
204 struct lookup_intent *it)
206 struct pnode_base *pb = pnode->p_base;
207 struct it_cb_data icbd;
208 struct md_op_data op_data = {{ 0 }};
209 struct ptlrpc_request *req = NULL;
210 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
211 struct obd_export *exp;
215 CDEBUG(D_VFSTRACE, "VFS Op:name=%.*s,intent=%x\n",
216 (int)pb->pb_name.len, pb->pb_name.name, it ? it->it_op : 0);
218 /* We don't want to cache negative dentries, so return 0 immediately.
219 * We believe that this is safe, that negative dentries cannot be
220 * pinned by someone else */
221 if (pb->pb_ino == NULL) {
222 CDEBUG(D_INODE, "negative pb\n");
226 /* This is due to bad interaction with libsysio. remove this when we
227 * switched to libbsdio XXX
230 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
231 struct intnl_stat *st = llu_i2stat(pb->pb_ino);
233 CDEBUG(D_INODE, "inode %llu still have intent "
234 "%p(opc 0x%x), release it\n",
235 (long long) st->st_ino, lli->lli_it,
237 ll_intent_release(lli->lli_it);
238 OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
243 exp = llu_i2mdexp(pb->pb_ino);
244 icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
245 icbd.icbd_child = pnode;
249 it->it_op_release = ll_intent_release;
252 llu_prep_md_op_data(&op_data, pnode->p_parent->p_base->pb_ino,
253 pb->pb_ino, pb->pb_name.name, pb->pb_name.len,
256 rc = md_intent_lock(exp, &op_data, NULL, 0, it, flags,
257 &req, llu_md_blocking_ast,
258 LDLM_FL_CANCEL_ON_BLOCK);
259 /* If req is NULL, then md_intent_lock only tried to do a lock match;
260 * if all was well, it will return 1 if it found locks, 0 otherwise. */
261 if (req == NULL && rc >= 0)
267 rc = pnode_revalidate_finish(req, it, pnode);
269 ll_intent_release(it);
274 /* Note: ll_intent_lock may cause a callback, check this! */
276 if (it->it_op & IT_OPEN)
277 LL_SAVE_INTENT(pb->pb_ino, it);
281 ptlrpc_req_finished(req);
287 llu_lookup_finish_locks(it, pnode);
292 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
293 struct lookup_intent *it, void *data)
295 struct it_cb_data *icbd = data;
296 struct pnode *child = icbd->icbd_child;
297 struct inode *parent = icbd->icbd_parent;
298 struct llu_sb_info *sbi = llu_i2sbi(parent);
299 struct inode *inode = NULL;
302 /* libsysio require us generate inode right away if success.
303 * so if mds created new inode for us we need make sure it
304 * succeeded. thus for any error we can't delay to the
305 * llu_file_open() time. */
306 if (it_disposition(it, DISP_OPEN_CREATE) &&
307 it_open_error(DISP_OPEN_CREATE, it)) {
308 CDEBUG(D_INODE, "detect mds create error\n");
309 return it_open_error(DISP_OPEN_CREATE, it);
311 if (it_disposition(it, DISP_OPEN_OPEN) &&
312 it_open_error(DISP_OPEN_OPEN, it)) {
313 CDEBUG(D_INODE, "detect mds open error\n");
314 /* undo which did by md_intent_lock */
315 if (it_disposition(it, DISP_OPEN_CREATE) &&
316 !it_open_error(DISP_OPEN_CREATE, it)) {
318 LASSERT(atomic_read(&request->rq_refcount) > 1);
319 CDEBUG(D_INODE, "dec a ref of req %p\n", request);
320 ptlrpc_req_finished(request);
322 return it_open_error(DISP_OPEN_OPEN, it);
325 /* NB 1 request reference will be taken away by ll_intent_lock()
328 if (!it_disposition(it, DISP_LOOKUP_NEG) || (it->it_op & IT_CREAT)) {
330 struct llu_inode_info *lli;
331 struct intnl_stat *st;
334 if (it_disposition(it, DISP_OPEN_CREATE))
335 ptlrpc_req_finished(request);
337 rc = md_get_lustre_md(sbi->ll_md_exp, request,
338 sbi->ll_dt_exp, sbi->ll_md_exp, &md);
342 inode = llu_iget(parent->i_fs, &md);
343 if (!inode || IS_ERR(inode)) {
344 /* free the lsm if we allocated one above */
346 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
347 RETURN(inode ? PTR_ERR(inode) : -ENOMEM);
348 } else if (md.lsm != NULL &&
349 llu_i2info(inode)->lli_smd != md.lsm) {
350 obd_free_memmd(sbi->ll_dt_exp, &md.lsm);
353 lli = llu_i2info(inode);
354 st = llu_i2stat(inode);
356 /* If this is a stat, get the authoritative file size */
357 if (it->it_op == IT_GETATTR && S_ISREG(st->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;
386 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
389 lock_res_and_lock(lock);
391 if (lock->l_ast_data) {
392 inode = (struct inode *)lock->l_ast_data;
397 unlock_res_and_lock(lock);
401 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
402 struct lookup_intent *it, int flags)
404 struct md_op_data op_data = {{ 0 }};
405 struct it_cb_data icbd;
406 struct ptlrpc_request *req = NULL;
407 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
412 if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
413 RETURN(-ENAMETOOLONG);
417 it->it_op_release = ll_intent_release;
420 icbd.icbd_child = pnode;
421 icbd.icbd_parent = parent;
423 if (it->it_op & IT_CREAT ||
424 (it->it_op & IT_OPEN && it->it_create_mode & O_CREAT)) {
425 opc = LUSTRE_OPC_CREATE;
427 opc = LUSTRE_OPC_ANY;
430 llu_prep_md_op_data(&op_data, parent, NULL,
431 pnode->p_base->pb_name.name,
432 pnode->p_base->pb_name.len, flags, opc);
434 rc = md_intent_lock(llu_i2mdexp(parent), &op_data, NULL, 0, it,
435 flags, &req, llu_md_blocking_ast,
436 LDLM_FL_CANCEL_ON_BLOCK);
440 rc = lookup_it_finish(req, DLM_REPLY_REC_OFF, it, &icbd);
442 ll_intent_release(it);
446 llu_lookup_finish_locks(it, pnode);
450 ptlrpc_req_finished(req);
454 static struct lookup_intent*
455 translate_lookup_intent(struct intent *intent, const char *path)
457 struct lookup_intent *it;
461 if (!intent || path) {
462 CDEBUG(D_VFSTRACE, "not intent needed\n");
466 OBD_ALLOC(it, sizeof(*it));
469 memset(it, 0, sizeof(*it));
471 /* libsysio will assign intent like following:
472 * NOTE: INT_CREAT has include INT_UPDPARENT
474 * open: INT_OPEN [| INT_CREAT]
477 * unlink: INT_UPDPARENT
478 * rmdir: INT_UPDPARENT
483 * following logic is adjusted for libsysio
486 it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
488 if (intent->int_opmask & INT_OPEN) {
489 it->it_op |= IT_OPEN;
491 /* convert access mode from O_ to FMODE_ */
492 if (it->it_flags & O_WRONLY)
494 else if (it->it_flags & O_RDWR)
495 fmode = FMODE_READ | FMODE_WRITE;
498 it->it_flags &= ~O_ACCMODE;
499 it->it_flags |= fmode;
502 /* XXX libsysio has strange code on intent handling,
503 * more check later */
504 if (it->it_flags & O_CREAT) {
505 it->it_op |= IT_CREAT;
506 it->it_create_mode = *((int*)intent->int_arg1);
507 /* bug 7278: libsysio hack. For O_EXCL, libsysio depends on
508 this lookup to return negative result, but then there is no
509 way to find out original intent in ll_iop_open(). So we just
510 clear O_EXCL from libsysio flags here to avoid checking
511 for negative result. O_EXCL will be enforced by MDS. */
512 *((int*)intent->int_arg2) &= ~O_EXCL;
515 if (intent->int_opmask & INT_GETATTR)
516 it->it_op |= IT_GETATTR;
518 LASSERT(!(intent->int_opmask & INT_SETATTR));
520 /* libsysio is different to linux vfs when doing unlink/rmdir,
521 * INT_UPDPARENT was passed down during name resolution. Here
522 * we treat it as normal lookup, later unlink()/rmdir() will
523 * do the actual work */
525 /* conform to kernel code, if only IT_LOOKUP was set, don't
527 if (!it->it_op || it->it_op & IT_LOOKUP) {
528 OBD_FREE(it, sizeof(*it));
532 it->it_op_release = ll_intent_release;
534 CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
538 int llu_iop_lookup(struct pnode *pnode,
540 struct intent *intnt,
543 struct lookup_intent *it;
547 liblustre_wait_event(0);
551 /* the mount root inode have no name, so don't call
552 * remote in this case. but probably we need revalidate
554 if (pnode->p_mount->mnt_root == pnode) {
555 struct inode *i = pnode->p_base->pb_ino;
560 if (!pnode->p_base->pb_name.len)
563 it = translate_lookup_intent(intnt, path);
565 /* param flags is not used, let it be 0 */
566 if (llu_pb_revalidate(pnode, 0, it)) {
567 LASSERT(pnode->p_base->pb_ino);
568 *inop = pnode->p_base->pb_ino;
572 rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
574 if (!pnode->p_base->pb_ino)
577 *inop = pnode->p_base->pb_ino;
581 liblustre_wait_event(0);