1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Light Super operations
6 * Copyright (c) 2002, 2003 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>
41 #include "llite_lib.h"
43 static void ll_intent_release(struct lookup_intent *it)
45 struct lustre_handle *handle;
48 /* LASSERT(ll_d2d(de) != NULL); */
50 if (it->d.lustre.it_lock_mode) {
51 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
52 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
55 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
57 /* intent_release may be called multiple times, from
58 this thread and we don't want to double-decref this
60 it->d.lustre.it_lock_mode = 0;
63 it->it_op_release = 0;
68 static void llu_mdc_lock_set_inode(struct lustre_handle *lockh,
71 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
74 LASSERT(lock != NULL);
80 static int pnode_revalidate_finish(struct ptlrpc_request *request,
81 struct inode *parent, struct pnode *pnode,
82 struct lookup_intent *it, int offset,
85 struct llu_sb_info *sbi = llu_i2sbi(parent);
86 struct pnode_base *pb = pnode->p_base;
87 struct mds_body *body;
88 struct lov_stripe_md *lsm = NULL;
89 struct lov_mds_md *lmm;
94 /* NB 1 request reference will be taken away by ll_intent_lock()
97 if (it_disposition(it, DISP_LOOKUP_NEG))
100 /* We only get called if the mdc_enqueue() called from
101 * ll_intent_lock() was successful. Therefore the mds_body is
102 * present and correct, and the eadata is present (but still
103 * opaque, so only obd_unpackmd() can check the size) */
104 body = lustre_msg_buf(request->rq_repmsg, offset, sizeof (*body));
105 LASSERT (body != NULL);
106 LASSERT_REPSWABBED (request, offset);
108 if (body->valid & OBD_MD_FLEASIZE) {
109 /* Only bother with this if inodes's LSM not set? */
111 if (body->eadatasize == 0) {
112 CERROR ("OBD_MD_FLEASIZE set, but eadatasize 0\n");
113 GOTO (out, rc = -EPROTO);
115 lmmsize = body->eadatasize;
116 lmm = lustre_msg_buf (request->rq_repmsg, offset + 1, lmmsize);
117 LASSERT (lmm != NULL);
118 LASSERT_REPSWABBED (request, offset + 1);
120 rc = obd_unpackmd (&sbi->ll_osc_conn,
123 CERROR ("Error %d unpacking eadata\n", rc);
125 /* XXX don't know if I should do this... */
127 /* or skip the ll_update_inode but still do
128 * mdc_lock_set_inode() */
130 LASSERT (rc >= sizeof (*lsm));
134 llu_update_inode(pb->pb_ino, body, lsm);
137 llu_i2info(pb->pb_ino)->lli_smd != lsm)
138 obd_free_memmd (&sbi->ll_osc_conn, &lsm);
140 llu_mdc_lock_set_inode((struct lustre_handle *)&it->d.lustre.it_lock_handle,
148 * remove the stale inode from pnode
150 void unhook_stale_inode(struct pnode *pno)
152 struct inode *inode = pno->p_base->pb_ino;
156 LASSERT(llu_i2info(inode)->lli_stale_flag);
158 pno->p_base->pb_ino = NULL;
160 if (!llu_i2info(inode)->lli_open_count) {
161 CDEBUG(D_INODE, "unhook inode %p (ino %lu) from pno %p\n",
162 inode, llu_i2info(inode)->lli_st_ino, pno);
165 _sysio_i_gone(inode);
172 void llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
177 if (it && pnode->p_base->pb_ino != NULL) {
178 struct inode *inode = pnode->p_base->pb_ino;
179 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%lu)\n",
180 inode, llu_i2info(inode)->lli_st_ino,
181 llu_i2info(inode)->lli_st_generation);
182 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
185 /* drop IT_LOOKUP locks */
186 if (it->it_op == IT_LOOKUP)
187 ll_intent_release(it);
191 static inline void ll_invalidate_inode_pages(struct inode * inode)
196 static int llu_mdc_blocking_ast(struct ldlm_lock *lock,
197 struct ldlm_lock_desc *desc,
198 void *data, int flag)
201 struct lustre_handle lockh;
206 case LDLM_CB_BLOCKING:
207 ldlm_lock2handle(lock, &lockh);
208 rc = ldlm_cli_cancel(&lockh);
210 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
214 case LDLM_CB_CANCELING: {
215 struct inode *inode = llu_inode_from_lock(lock);
216 struct llu_inode_info *lli;
218 /* Invalidate all dentries associated with this inode */
222 lli = llu_i2info(inode);
224 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
226 if (lock->l_resource->lr_name.name[0] != lli->lli_st_ino ||
227 lock->l_resource->lr_name.name[1] != lli->lli_st_generation) {
228 LDLM_ERROR(lock, "data mismatch with ino %lu/%lu",
229 lli->lli_st_ino, lli->lli_st_generation);
231 if (S_ISDIR(lli->lli_st_mode)) {
232 CDEBUG(D_INODE, "invalidating inode %lu\n",
235 ll_invalidate_inode_pages(inode);
239 if (inode->i_sb->s_root &&
240 inode != inode->i_sb->s_root->d_inode)
241 ll_unhash_aliases(inode);
253 int llu_pb_revalidate(struct pnode *pnode, int flags, struct lookup_intent *it)
255 struct pnode_base *pb = pnode->p_base;
256 struct ll_fid pfid, cfid;
257 struct it_cb_data icbd;
258 struct ll_uctxt ctxt;
259 struct ptlrpc_request *req = NULL;
260 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
261 struct obd_export *exp;
265 CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%x\n",
266 pb->pb_name.name, it ? it->it_op : 0);
268 /* We don't want to cache negative dentries, so return 0 immediately.
269 * We believe that this is safe, that negative dentries cannot be
270 * pinned by someone else */
271 if (pb->pb_ino == NULL) {
272 CDEBUG(D_INODE, "negative pb\n");
276 /* check stale inode */
277 if (llu_i2info(pb->pb_ino)->lli_stale_flag)
278 unhook_stale_inode(pnode);
280 /* check again because unhook_stale_inode() might generate
282 if (pb->pb_ino == NULL) {
283 CDEBUG(D_INODE, "negative pb\n");
287 /* This is due to bad interaction with libsysio. remove this when we
288 * switched to libbsdio
291 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
293 CDEBUG(D_INODE, "inode %lu still have intent "
294 "%p(opc 0x%x), release it\n",
295 lli->lli_st_ino, lli->lli_it,
297 ll_intent_release(lli->lli_it);
298 OBD_FREE(lli->lli_it, sizeof(*lli->lli_it));
303 exp = llu_i2mdcexp(pb->pb_ino);
304 ll_inode2fid(&pfid, pnode->p_parent->p_base->pb_ino);
305 ll_inode2fid(&cfid, pb->pb_ino);
306 icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
307 icbd.icbd_child = pnode;
311 it->it_op_release = ll_intent_release;
314 ll_i2uctxt(&ctxt, pnode->p_parent->p_base->pb_ino, pb->pb_ino);
316 rc = mdc_intent_lock(exp, &ctxt, &pfid,
317 pb->pb_name.name, pb->pb_name.len,
318 NULL, 0, &cfid, it, flags, &req,
319 llu_mdc_blocking_ast);
320 /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
321 * if all was well, it will return 1 if it found locks, 0 otherwise. */
322 if (req == NULL && rc >= 0)
325 /* unfortunately ll_intent_lock may cause a callback and revoke our
328 spin_lock(&dcache_lock);
329 list_del_init(&de->d_hash);
330 spin_unlock(&dcache_lock);
333 if (it->it_op & (IT_OPEN | IT_GETATTR))
334 LL_SAVE_INTENT(pb->pb_ino, it);
338 ptlrpc_req_finished(req);
341 if (S_ISDIR(llu_i2info(pb->pb_ino)->lli_st_mode))
342 ll_invalidate_inode_pages(pb->pb_ino);
343 llu_i2info(pb->pb_ino)->lli_stale_flag = 1;
344 unhook_stale_inode(pnode);
346 llu_lookup_finish_locks(it, pnode);
347 llu_i2info(pb->pb_ino)->lli_stale_flag = 0;
348 if (it->it_op & (IT_OPEN | IT_GETATTR))
349 LL_SAVE_INTENT(pb->pb_ino, it);
354 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
355 struct lookup_intent *it, void *data)
357 struct it_cb_data *icbd = data;
358 struct pnode *child = icbd->icbd_child;
359 struct inode *parent = icbd->icbd_parent;
360 struct llu_sb_info *sbi = llu_i2sbi(parent);
361 struct inode *inode = NULL;
364 /* NB 1 request reference will be taken away by ll_intent_lock()
366 /* XXX libsysio require the inode must be generated here XXX */
367 if ((it->it_op & IT_CREAT) || !it_disposition(it, DISP_LOOKUP_NEG)) {
369 struct llu_inode_info *lli;
372 rc = mdc_req2lustre_md(request, offset, sbi->ll_osc_exp, &md);
376 inode = llu_iget(parent->i_fs, &md);
378 /* free the lsm if we allocated one above */
380 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
382 } else if (md.lsm != NULL &&
383 llu_i2info(inode)->lli_smd != md.lsm) {
384 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
387 lli = llu_i2info(inode);
389 /* If this is a stat, get the authoritative file size */
390 if (it->it_op == IT_GETATTR && S_ISREG(lli->lli_st_mode) &&
391 lli->lli_smd != NULL) {
392 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
393 struct lustre_handle lockh = {0};
394 struct lov_stripe_md *lsm = lli->lli_smd;
397 LASSERT(lsm->lsm_object_id != 0);
399 rc = llu_extent_lock(NULL, inode, lsm, LCK_PR, &extent,
401 if (rc != ELDLM_OK) {
405 llu_extent_unlock(NULL, inode, lsm, LCK_PR, &lockh);
411 if (inode && (it->it_op & (IT_OPEN | IT_GETATTR)))
412 LL_SAVE_INTENT(inode, it);
414 dentry->d_op = &ll_d_ops;
418 d_add(dentry, inode);
420 child->p_base->pb_ino = inode;
425 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
428 l_lock(&lock->l_resource->lr_namespace->ns_lock);
430 if (lock->l_ast_data) {
431 inode = (struct inode *)lock->l_ast_data;
436 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
441 #define EXT2_NAME_LEN (255)
443 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
444 struct lookup_intent *it, int flags)
447 struct ll_uctxt ctxt;
448 struct it_cb_data icbd;
449 struct ptlrpc_request *req = NULL;
450 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
454 if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
455 RETURN(-ENAMETOOLONG);
459 CDEBUG(D_VFSTRACE, "VFS Op:name=%s,dir=%lu/%u(%p),intent=%s\n",
460 dentry->d_name.name, parent->i_ino, parent->i_generation,
461 parent, LL_IT2STR(it));
463 if (d_mountpoint(dentry))
464 CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
466 ll_frob_intent(&it, &lookup_it);
471 it->it_op_release = ll_intent_release;
474 icbd.icbd_child = pnode;
475 icbd.icbd_parent = parent;
476 icbd.icbd_child = pnode;
477 ll_inode2fid(&pfid, parent);
478 ll_i2uctxt(&ctxt, parent, NULL);
480 rc = mdc_intent_lock(llu_i2mdcexp(parent), &ctxt, &pfid,
481 pnode->p_base->pb_name.name,
482 pnode->p_base->pb_name.len,
483 NULL, 0, NULL, it, flags, &req,
484 llu_mdc_blocking_ast);
488 rc = lookup_it_finish(req, 1, it, &icbd);
490 ll_intent_release(it);
494 llu_lookup_finish_locks(it, pnode);
498 GOTO(out, retval = NULL);
500 GOTO(out, retval = dentry);
504 ptlrpc_req_finished(req);
508 static struct lookup_intent*
509 translate_lookup_intent(struct intent *intent, const char *path)
511 struct lookup_intent *it;
515 if (!intent || path) {
516 CDEBUG(D_VFSTRACE, "not intent needed\n");
520 OBD_ALLOC(it, sizeof(*it));
523 memset(it, 0, sizeof(*it));
525 /* libsysio will assign intent like following:
526 * NOTE: INT_CREAT has include INT_UPDPARENT
528 * open: INT_OPEN [| INT_CREAT]
531 * unlink: INT_UPDPARENT
532 * rmdir: INT_UPDPARENT
537 * following logic is adjusted for libsysio
540 it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
542 if (intent->int_opmask & INT_OPEN) {
543 it->it_op |= IT_OPEN;
545 /* convert access mode from O_ to FMODE_ */
546 if (it->it_flags & O_WRONLY)
548 else if (it->it_flags & O_RDWR)
549 fmode = FMODE_READ | FMODE_WRITE;
552 it->it_flags &= ~O_ACCMODE;
553 it->it_flags |= fmode;
557 else if (intent->int_opmask & INT_CREAT)
558 it->it_op |= IT_LOOKUP;
561 /* FIXME libsysio has strange code on intent handling,
562 * more check later */
563 if (it->it_flags & O_CREAT) {
564 it->it_op |= IT_CREAT;
565 it->it_create_mode = *((int*)intent->int_arg1);
568 if (intent->int_opmask & INT_GETATTR)
569 it->it_op |= IT_GETATTR;
571 if (intent->int_opmask & INT_SETATTR)
574 /* libsysio is different to linux vfs when doing unlink/rmdir,
575 * INT_UPDPARENT was passed down during name resolution. Here
576 * we treat it as normal lookup, later unlink()/rmdir() will
577 * do the actual work */
579 /* conform to kernel code, if only IT_LOOKUP was set, don't
581 if (!it->it_op || it->it_op == IT_LOOKUP) {
582 OBD_FREE(it, sizeof(*it));
586 it->it_op_release = ll_intent_release;
588 CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
592 int llu_iop_lookup(struct pnode *pnode,
594 struct intent *intnt,
597 struct lookup_intent *it;
603 /* the mount root inode have no name, so don't call
604 * remote in this case. but probably we need revalidate
606 if (pnode->p_mount->mnt_root == pnode) {
607 struct inode *i = pnode->p_base->pb_ino;
612 if (!pnode->p_base->pb_name.len)
615 it = translate_lookup_intent(intnt, path);
617 /* param flags is not used, let it be 0 */
618 if (llu_pb_revalidate(pnode, 0, it)) {
619 LASSERT(pnode->p_base->pb_ino);
620 *inop = pnode->p_base->pb_ino;
624 rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
626 if (!pnode->p_base->pb_ino)
629 *inop = pnode->p_base->pb_ino;