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>
43 #include "llite_lib.h"
45 static void ll_intent_drop_lock(struct lookup_intent *it)
47 struct lustre_handle *handle;
49 if (it->it_op && it->d.lustre.it_lock_mode) {
50 handle = (struct lustre_handle *)&it->d.lustre.it_lock_handle;
51 CDEBUG(D_DLMTRACE, "releasing lock with cookie "LPX64
52 " from it %p\n", handle->cookie, it);
53 ldlm_lock_decref(handle, it->d.lustre.it_lock_mode);
55 /* bug 494: intent_release may be called multiple times, from
56 * this thread and we don't want to double-decref this lock */
57 it->d.lustre.it_lock_mode = 0;
61 static void ll_intent_release(struct lookup_intent *it)
65 ll_intent_drop_lock(it);
67 it->it_op_release = 0;
68 it->d.lustre.it_disposition = 0;
69 it->d.lustre.it_data = NULL;
74 * remove the stale inode from pnode
76 void unhook_stale_inode(struct pnode *pno)
78 struct inode *inode = pno->p_base->pb_ino;
82 LASSERT(llu_i2info(inode)->lli_stale_flag);
84 pno->p_base->pb_ino = NULL;
87 if (!llu_i2info(inode)->lli_open_count) {
88 CDEBUG(D_INODE, "unhook inode %p (ino %lu) from pno %p\n",
89 inode, llu_i2info(inode)->lli_st_ino, pno);
98 void llu_lookup_finish_locks(struct lookup_intent *it, struct pnode *pnode)
103 if (it && pnode->p_base->pb_ino != NULL) {
104 struct inode *inode = pnode->p_base->pb_ino;
105 CDEBUG(D_DLMTRACE, "setting l_data to inode %p (%lu/%lu)\n",
106 inode, llu_i2info(inode)->lli_st_ino,
107 llu_i2info(inode)->lli_st_generation);
108 mdc_set_lock_data(&it->d.lustre.it_lock_handle, inode);
111 /* drop IT_LOOKUP locks */
112 if (it->it_op == IT_LOOKUP)
113 ll_intent_release(it);
117 static inline void llu_invalidate_inode_pages(struct inode * inode)
122 int llu_mdc_blocking_ast(struct ldlm_lock *lock,
123 struct ldlm_lock_desc *desc,
124 void *data, int flag)
127 struct lustre_handle lockh;
132 case LDLM_CB_BLOCKING:
133 ldlm_lock2handle(lock, &lockh);
134 rc = ldlm_cli_cancel(&lockh);
136 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
140 case LDLM_CB_CANCELING: {
141 struct inode *inode = llu_inode_from_lock(lock);
142 struct llu_inode_info *lli;
144 /* Invalidate all dentries associated with this inode */
148 lli = llu_i2info(inode);
150 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
152 if (lock->l_resource->lr_name.name[0] != lli->lli_st_ino ||
153 lock->l_resource->lr_name.name[1] != lli->lli_st_generation) {
154 LDLM_ERROR(lock, "data mismatch with ino %lu/%lu",
155 lli->lli_st_ino, lli->lli_st_generation);
157 if (S_ISDIR(lli->lli_st_mode)) {
158 CDEBUG(D_INODE, "invalidating inode %lu\n",
161 llu_invalidate_inode_pages(inode);
165 if (inode->i_sb->s_root &&
166 inode != inode->i_sb->s_root->d_inode)
167 ll_unhash_aliases(inode);
179 static int pnode_revalidate_finish(struct ptlrpc_request *req,
181 struct lookup_intent *it,
184 struct inode *inode = pnode->p_base->pb_ino;
194 if (it_disposition(it, DISP_LOOKUP_NEG))
197 rc = mdc_req2lustre_md(req, offset, llu_i2sbi(inode)->ll_osc_exp, &md);
201 llu_update_inode(inode, md.body, md.lsm);
206 int llu_pb_revalidate(struct pnode *pnode, int flags, struct lookup_intent *it)
208 struct pnode_base *pb = pnode->p_base;
209 struct ll_fid pfid, cfid;
210 struct it_cb_data icbd;
211 struct ll_uctxt ctxt;
212 struct ptlrpc_request *req = NULL;
213 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
214 struct obd_export *exp;
218 CDEBUG(D_VFSTRACE, "VFS Op:name=%s,intent=%x\n",
219 pb->pb_name.name, it ? it->it_op : 0);
221 /* We don't want to cache negative dentries, so return 0 immediately.
222 * We believe that this is safe, that negative dentries cannot be
223 * pinned by someone else */
224 if (pb->pb_ino == NULL) {
225 CDEBUG(D_INODE, "negative pb\n");
229 /* check stale inode */
230 if (llu_i2info(pb->pb_ino)->lli_stale_flag)
231 unhook_stale_inode(pnode);
233 /* check again because unhook_stale_inode() might generate
235 if (pb->pb_ino == NULL) {
236 CDEBUG(D_INODE, "negative pb\n");
240 /* This is due to bad interaction with libsysio. remove this when we
241 * switched to libbsdio XXX
244 struct llu_inode_info *lli = llu_i2info(pb->pb_ino);
246 CDEBUG(D_INODE, "inode %lu still have intent "
247 "%p(opc 0x%x), release it\n",
248 lli->lli_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 ll_inode2fid(&pfid, pnode->p_parent->p_base->pb_ino);
258 ll_inode2fid(&cfid, pb->pb_ino);
259 icbd.icbd_parent = pnode->p_parent->p_base->pb_ino;
260 icbd.icbd_child = pnode;
264 it->it_op_release = ll_intent_release;
267 ll_i2uctxt(&ctxt, pnode->p_parent->p_base->pb_ino, pb->pb_ino);
269 rc = mdc_intent_lock(exp, &ctxt, &pfid,
270 pb->pb_name.name, pb->pb_name.len,
271 NULL, 0, &cfid, it, flags, &req,
272 llu_mdc_blocking_ast);
273 /* If req is NULL, then mdc_intent_lock only tried to do a lock match;
274 * if all was well, it will return 1 if it found locks, 0 otherwise. */
275 if (req == NULL && rc >= 0)
281 rc = pnode_revalidate_finish(req, 1, it, pnode);
283 /* Note: ll_intent_lock may cause a callback, check this! */
285 if (it->it_op & (IT_OPEN | IT_GETATTR))
286 LL_SAVE_INTENT(pb->pb_ino, it);
290 ptlrpc_req_finished(req);
293 if (S_ISDIR(llu_i2info(pb->pb_ino)->lli_st_mode))
294 llu_invalidate_inode_pages(pb->pb_ino);
295 llu_i2info(pb->pb_ino)->lli_stale_flag = 1;
296 unhook_stale_inode(pnode);
298 llu_lookup_finish_locks(it, pnode);
299 llu_i2info(pb->pb_ino)->lli_stale_flag = 0;
300 if (it->it_op & (IT_OPEN | IT_GETATTR))
301 LL_SAVE_INTENT(pb->pb_ino, it);
306 static int lookup_it_finish(struct ptlrpc_request *request, int offset,
307 struct lookup_intent *it, void *data)
309 struct it_cb_data *icbd = data;
310 struct pnode *child = icbd->icbd_child;
311 struct inode *parent = icbd->icbd_parent;
312 struct llu_sb_info *sbi = llu_i2sbi(parent);
313 struct inode *inode = NULL;
316 /* NB 1 request reference will be taken away by ll_intent_lock()
318 * Note: libsysio require the inode must be generated here
320 if ((it->it_op & IT_CREAT) || !it_disposition(it, DISP_LOOKUP_NEG)) {
322 struct llu_inode_info *lli;
325 rc = mdc_req2lustre_md(request, offset, sbi->ll_osc_exp, &md);
329 inode = llu_iget(parent->i_fs, &md);
331 /* free the lsm if we allocated one above */
333 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
335 } else if (md.lsm != NULL &&
336 llu_i2info(inode)->lli_smd != md.lsm) {
337 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
340 lli = llu_i2info(inode);
342 /* If this is a stat, get the authoritative file size */
343 if (it->it_op == IT_GETATTR && S_ISREG(lli->lli_st_mode) &&
344 lli->lli_smd != NULL) {
345 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
346 struct lustre_handle lockh = {0};
347 struct lov_stripe_md *lsm = lli->lli_smd;
350 LASSERT(lsm->lsm_object_id != 0);
352 /* bug 2334: drop MDS lock before acquiring OST lock */
353 ll_intent_drop_lock(it);
355 rc = llu_extent_lock(NULL, inode, lsm, LCK_PR, &extent,
357 if (rc != ELDLM_OK) {
361 llu_extent_unlock(NULL, inode, lsm, LCK_PR, &lockh);
367 /* intent will be further used in cases of open()/getattr() */
368 if (inode && (it->it_op & (IT_OPEN | IT_GETATTR)))
369 LL_SAVE_INTENT(inode, it);
371 child->p_base->pb_ino = inode;
376 struct inode *llu_inode_from_lock(struct ldlm_lock *lock)
379 l_lock(&lock->l_resource->lr_namespace->ns_lock);
381 if (lock->l_ast_data) {
382 inode = (struct inode *)lock->l_ast_data;
387 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
391 static int llu_lookup_it(struct inode *parent, struct pnode *pnode,
392 struct lookup_intent *it, int flags)
395 struct ll_uctxt ctxt;
396 struct it_cb_data icbd;
397 struct ptlrpc_request *req = NULL;
398 struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
402 if (pnode->p_base->pb_name.len > EXT2_NAME_LEN)
403 RETURN(-ENAMETOOLONG);
407 it->it_op_release = ll_intent_release;
410 icbd.icbd_child = pnode;
411 icbd.icbd_parent = parent;
412 icbd.icbd_child = pnode;
413 ll_inode2fid(&pfid, parent);
414 ll_i2uctxt(&ctxt, parent, NULL);
416 rc = mdc_intent_lock(llu_i2mdcexp(parent), &ctxt, &pfid,
417 pnode->p_base->pb_name.name,
418 pnode->p_base->pb_name.len,
419 NULL, 0, NULL, it, flags, &req,
420 llu_mdc_blocking_ast);
424 rc = lookup_it_finish(req, 1, it, &icbd);
426 ll_intent_release(it);
430 llu_lookup_finish_locks(it, pnode);
434 ptlrpc_req_finished(req);
438 static struct lookup_intent*
439 translate_lookup_intent(struct intent *intent, const char *path)
441 struct lookup_intent *it;
445 if (!intent || path) {
446 CDEBUG(D_VFSTRACE, "not intent needed\n");
450 OBD_ALLOC(it, sizeof(*it));
453 memset(it, 0, sizeof(*it));
455 /* libsysio will assign intent like following:
456 * NOTE: INT_CREAT has include INT_UPDPARENT
458 * open: INT_OPEN [| INT_CREAT]
461 * unlink: INT_UPDPARENT
462 * rmdir: INT_UPDPARENT
467 * following logic is adjusted for libsysio
470 it->it_flags = intent->int_arg2 ? *((int*)intent->int_arg2) : 0;
472 if (intent->int_opmask & INT_OPEN) {
473 it->it_op |= IT_OPEN;
475 /* convert access mode from O_ to FMODE_ */
476 if (it->it_flags & O_WRONLY)
478 else if (it->it_flags & O_RDWR)
479 fmode = FMODE_READ | FMODE_WRITE;
482 it->it_flags &= ~O_ACCMODE;
483 it->it_flags |= fmode;
486 /* XXX libsysio has strange code on intent handling,
487 * more check later */
488 if (it->it_flags & O_CREAT) {
489 it->it_op |= IT_CREAT;
490 it->it_create_mode = *((int*)intent->int_arg1);
493 if (intent->int_opmask & INT_GETATTR)
494 it->it_op |= IT_GETATTR;
496 LASSERT(!(intent->int_opmask & INT_SETATTR));
498 /* libsysio is different to linux vfs when doing unlink/rmdir,
499 * INT_UPDPARENT was passed down during name resolution. Here
500 * we treat it as normal lookup, later unlink()/rmdir() will
501 * do the actual work */
503 /* conform to kernel code, if only IT_LOOKUP was set, don't
505 if (!it->it_op || it->it_op == IT_LOOKUP) {
506 OBD_FREE(it, sizeof(*it));
510 it->it_op_release = ll_intent_release;
512 CDEBUG(D_VFSTRACE, "final intent 0x%x\n", it ? it->it_op : 0);
516 int llu_iop_lookup(struct pnode *pnode,
518 struct intent *intnt,
521 struct lookup_intent *it;
527 /* the mount root inode have no name, so don't call
528 * remote in this case. but probably we need revalidate
530 if (pnode->p_mount->mnt_root == pnode) {
531 struct inode *i = pnode->p_base->pb_ino;
536 if (!pnode->p_base->pb_name.len)
539 it = translate_lookup_intent(intnt, path);
541 /* param flags is not used, let it be 0 */
542 if (llu_pb_revalidate(pnode, 0, it)) {
543 LASSERT(pnode->p_base->pb_ino);
544 *inop = pnode->p_base->pb_ino;
548 rc = llu_lookup_it(pnode->p_parent->p_base->pb_ino, pnode, it, 0);
550 if (!pnode->p_base->pb_ino)
553 *inop = pnode->p_base->pb_ino;