1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.sf.net/projects/lustre/
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_MDC
28 # include <linux/module.h>
29 # include <linux/pagemap.h>
30 # include <linux/miscdevice.h>
31 # include <linux/init.h>
33 # include <liblustre.h>
36 #include <linux/obd_class.h>
37 #include <linux/lustre_mds.h>
38 #include <linux/lustre_dlm.h>
39 #include <linux/lustre_sec.h>
40 #include <linux/lprocfs_status.h>
41 #include <linux/lustre_acl.h>
42 #include <linux/lustre_lite.h>
43 #include "mdc_internal.h"
45 int it_disposition(struct lookup_intent *it, int flag)
47 return LUSTRE_IT(it)->it_disposition & flag;
49 EXPORT_SYMBOL(it_disposition);
51 void it_set_disposition(struct lookup_intent *it, int flag)
53 LUSTRE_IT(it)->it_disposition |= flag;
55 EXPORT_SYMBOL(it_set_disposition);
57 static void mdc_id2mdc_data(struct mdc_op_data *data,
61 int namelen, int mode)
72 data->namelen = namelen;
73 data->create_mode = mode;
74 data->mod_time = LTIME_S(CURRENT_TIME);
77 static int it_to_lock_mode(struct lookup_intent *it)
79 /* CREAT needs to be tested before open (both could be set) */
80 if (it->it_op & IT_CREAT)
82 else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_LOOKUP |
90 int it_open_error(int phase, struct lookup_intent *it)
92 if (it_disposition(it, DISP_OPEN_OPEN)) {
93 if (phase == DISP_OPEN_OPEN)
94 return LUSTRE_IT(it)->it_status;
99 if (it_disposition(it, DISP_OPEN_CREATE)) {
100 if (phase == DISP_OPEN_CREATE)
101 return LUSTRE_IT(it)->it_status;
106 if (it_disposition(it, DISP_LOOKUP_EXECD)) {
107 if (phase == DISP_LOOKUP_EXECD)
108 return LUSTRE_IT(it)->it_status;
113 if (it_disposition(it, DISP_IT_EXECD)) {
114 if (phase == DISP_IT_EXECD)
115 return LUSTRE_IT(it)->it_status;
119 CERROR("it disp: %X, status: %d\n", LUSTRE_IT(it)->it_disposition,
120 LUSTRE_IT(it)->it_status);
124 EXPORT_SYMBOL(it_open_error);
126 /* this must be called on a lockh that is known to have a referenced lock */
127 int mdc_set_lock_data(struct obd_export *exp, __u64 *l, void *data)
129 struct ldlm_lock *lock;
130 struct lustre_handle *lockh = (struct lustre_handle *)l;
138 lock = ldlm_handle2lock(lockh);
140 LASSERT(lock != NULL);
141 l_lock(&lock->l_resource->lr_namespace->ns_lock);
143 if (lock->l_ast_data && lock->l_ast_data != data) {
144 struct inode *new_inode = data;
145 struct inode *old_inode = lock->l_ast_data;
146 LASSERTF(old_inode->i_state & I_FREEING,
147 "Found existing inode %p/%lu/%u state %lu in lock: "
148 "setting data to %p/%lu/%u\n", old_inode,
149 old_inode->i_ino, old_inode->i_generation,
150 old_inode->i_state, new_inode, new_inode->i_ino,
151 new_inode->i_generation);
154 lock->l_ast_data = data;
155 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
161 EXPORT_SYMBOL(mdc_set_lock_data);
163 int mdc_change_cbdata(struct obd_export *exp, struct lustre_id *id,
164 ldlm_iterator_t it, void *data)
166 struct ldlm_res_id res_id = { .name = {0} };
169 res_id.name[0] = id_fid(id);
170 res_id.name[1] = id_group(id);
172 ldlm_change_cbdata(class_exp2obd(exp)->obd_namespace,
180 mdc_clear_replay_flag(struct ptlrpc_request *req, int rc)
182 /* Don't hold error requests for replay. */
183 if (req->rq_replay) {
184 unsigned long irqflags;
185 spin_lock_irqsave(&req->rq_lock, irqflags);
187 spin_unlock_irqrestore(&req->rq_lock, irqflags);
189 if (rc && req->rq_transno != 0) {
190 DEBUG_REQ(D_ERROR, req, "transno returned on error rc %d", rc);
195 /* We always reserve enough space in the reply packet for a stripe MD, because
196 * we don't know in advance the file type. */
197 int mdc_enqueue(struct obd_export *exp,
199 struct lookup_intent *it,
201 struct mdc_op_data *data,
202 struct lustre_handle *lockh,
205 ldlm_completion_callback cb_completion,
206 ldlm_blocking_callback cb_blocking,
209 struct ptlrpc_request *req;
210 struct ldlm_res_id res_id = {
211 .name = {id_fid(&data->id1), id_group(&data->id1)}
213 struct obd_device *obddev = class_exp2obd(exp);
214 ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_LOOKUP } };
215 struct ldlm_intent *lit;
216 struct ldlm_request *lockreq;
217 int reqsize[6] = {[MDS_REQ_SECDESC_OFF] = 0,
218 [MDS_REQ_INTENT_LOCKREQ_OFF] = sizeof(*lockreq),
219 [MDS_REQ_INTENT_IT_OFF] = sizeof(*lit)};
220 int repsize[5] = {sizeof(struct ldlm_reply),
221 sizeof(struct mds_body),
222 obddev->u.cli.cl_max_mds_easize};
223 int req_buffers = 3, reply_buffers = 0;
224 int rc, flags = LDLM_FL_HAS_INTENT;
225 struct ldlm_reply *dlm_rep = NULL;
227 unsigned long irqflags;
230 // LDLM_DEBUG_NOLOCK("mdsintent=%s,name=%s,dir=%lu",
231 // ldlm_it2str(it->it_op), it_name, it_inode->i_ino);
233 reqsize[0] = lustre_secdesc_size();
235 if (it->it_op & IT_OPEN) {
236 it->it_create_mode |= S_IFREG;
237 it->it_create_mode &= ~current->fs->umask;
239 reqsize[req_buffers++] = sizeof(struct mds_rec_create);
240 reqsize[req_buffers++] = data->namelen + 1;
241 reqsize[req_buffers++] = obddev->u.cli.cl_max_mds_easize;
243 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
244 LDLM_ENQUEUE, req_buffers, reqsize, NULL);
248 spin_lock_irqsave (&req->rq_lock, irqflags);
250 spin_unlock_irqrestore (&req->rq_lock, irqflags);
252 /* pack the intent */
253 lit = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_INTENT_IT_OFF,
255 lit->opc = (__u64)it->it_op;
257 /* pack the intended request */
258 mdc_open_pack(req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF, data,
259 it->it_create_mode, 0, it->it_flags, lmm, lmmsize);
260 /* get ready for the reply */
262 repsize[4] = xattr_acl_size(LL_ACL_MAX_ENTRIES);
264 req->rq_replen = lustre_msg_size(5, repsize);
265 } else if (it->it_op & (IT_GETATTR | IT_LOOKUP | IT_CHDIR)) {
266 __u64 valid = data->valid | OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE |
269 reqsize[req_buffers++] = sizeof(struct mds_body);
270 reqsize[req_buffers++] = data->namelen + 1;
272 if (it->it_op & IT_GETATTR)
273 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
275 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
276 LDLM_ENQUEUE, req_buffers, reqsize, NULL);
281 /* pack the intent */
282 lit = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_INTENT_IT_OFF,
284 lit->opc = (__u64)it->it_op;
286 /* pack the intended request */
287 mdc_getattr_pack(req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF,
288 valid, it->it_flags, data);
290 /* get ready for the reply */
292 repsize[4] = xattr_acl_size(LL_ACL_MAX_ENTRIES);
294 req->rq_replen = lustre_msg_size(5, repsize);
295 } else if (it->it_op == IT_READDIR) {
296 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
297 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
298 LDLM_ENQUEUE, 2, reqsize, NULL);
302 /* get ready for the reply */
304 req->rq_replen = lustre_msg_size(1, repsize);
305 } else if (it->it_op == IT_UNLINK) {
306 reqsize[req_buffers++] = sizeof(struct mds_body);
307 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
308 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
309 LDLM_ENQUEUE, req_buffers, reqsize, NULL);
313 /* pack the intended request */
314 mdc_getattr_pack(req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF,
317 /* pack the intent */
318 lit = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_INTENT_IT_OFF,
320 lit->opc = (__u64)it->it_op;
322 /* get ready for the reply */
324 req->rq_replen = lustre_msg_size(3, repsize);
330 lustre_pack_secdesc(req, reqsize[0]);
332 mdc_get_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
333 rc = ldlm_cli_enqueue(exp, req, obddev->obd_namespace, res_id,
334 lock_type, &policy, lock_mode, &flags,cb_blocking,
335 cb_completion, NULL, cb_data, NULL, 0, NULL,
337 mdc_put_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
339 /* Similarly, if we're going to replay this request, we don't want to
340 * actually get a lock, just perform the intent. */
341 if (req->rq_transno || req->rq_replay) {
342 lockreq = lustre_msg_buf(req->rq_reqmsg,
343 MDS_REQ_INTENT_LOCKREQ_OFF,
345 lockreq->lock_flags |= LDLM_FL_INTENT_ONLY;
348 /* This can go when we're sure that this can never happen */
349 LASSERT(rc != -ENOENT);
350 /* We need dlm_rep to be assigned this early, to check lock mode of
351 returned lock from request to avoid possible race with lock
353 if (rc == ELDLM_LOCK_ABORTED || !rc) {
354 dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
355 LASSERT(dlm_rep != NULL); /* checked by ldlm_cli_enqueue() */
357 if (rc == ELDLM_LOCK_ABORTED) {
359 memset(lockh, 0, sizeof(*lockh));
361 } else if (rc != 0) {
362 CERROR("ldlm_cli_enqueue: %d\n", rc);
363 LASSERTF(rc < 0, "rc = %d\n", rc);
364 mdc_clear_replay_flag(req, rc);
365 ptlrpc_req_finished(req);
367 } else { /* rc = 0 */
368 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
371 /* If the server gave us back a different lock mode, we should
372 * fix up our variables. */
373 if (dlm_rep->lock_desc.l_req_mode != lock_mode) {
374 ldlm_lock_addref(lockh, dlm_rep->lock_desc.l_req_mode);
375 ldlm_lock_decref(lockh, lock_mode);
376 lock_mode = dlm_rep->lock_desc.l_req_mode;
379 ldlm_lock_allow_match(lock);
383 LASSERT_REPSWABBED(req, 0); /* swabbed by ldlm_cli_enqueue() */
385 LUSTRE_IT(it)->it_disposition = (int) dlm_rep->lock_policy_res1;
386 LUSTRE_IT(it)->it_status = (int) dlm_rep->lock_policy_res2;
387 LUSTRE_IT(it)->it_lock_mode = lock_mode;
388 LUSTRE_IT(it)->it_data = req;
390 if (LUSTRE_IT(it)->it_status < 0 && req->rq_replay)
391 mdc_clear_replay_flag(req, LUSTRE_IT(it)->it_status);
393 DEBUG_REQ(D_DLMTRACE, req, "disposition: %x, status: %d",
394 LUSTRE_IT(it)->it_disposition, LUSTRE_IT(it)->it_status);
396 /* We know what to expect, so we do any byte flipping required here */
397 LASSERT(reply_buffers == 5 || reply_buffers == 4 ||
398 reply_buffers == 3 || reply_buffers == 1);
399 if (reply_buffers >= 3) {
400 struct mds_body *body;
402 body = lustre_swab_repbuf(req, 1, sizeof (*body),
403 lustre_swab_mds_body);
405 CERROR ("Can't swab mds_body\n");
409 if ((body->valid & OBD_MD_FLEASIZE) != 0) {
410 /* The eadata is opaque; just check that it is there.
411 * Eventually, obd_unpackmd() will check the contents */
412 eadata = lustre_swab_repbuf(req, 2, body->eadatasize,
414 if (eadata == NULL) {
415 CERROR ("Missing/short eadata\n");
418 if (it->it_op & IT_OPEN) {
421 replayea = lustre_msg_buf(req->rq_reqmsg,
422 MDS_REQ_INTENT_REC_OFF + 2,
425 memcpy(replayea, eadata, body->eadatasize);
427 LASSERT(req->rq_reqmsg->bufcount == 6);
428 req->rq_reqmsg->buflens[5] = body->eadatasize;
429 /* If this isn't the last buffer, we might
430 * have to shift other data around. */
437 EXPORT_SYMBOL(mdc_enqueue);
440 * This long block is all about fixing up the lock and request state
441 * so that it is correct as of the moment _before_ the operation was
442 * applied; that way, the VFS will think that everything is normal and
443 * call Lustre's regular VFS methods.
445 * If we're performing a creation, that means that unless the creation
446 * failed with EEXIST, we should fake up a negative dentry.
448 * For everything else, we want to lookup to succeed.
450 * One additional note: if CREATE or OPEN succeeded, we add an extra
451 * reference to the request because we need to keep it around until
452 * ll_create/ll_open gets called.
454 * The server will return to us, in it_disposition, an indication of
455 * exactly what d.lustre->it_status refers to.
457 * If DISP_OPEN_OPEN is set, then d.lustre->it_status refers to the open() call,
458 * otherwise if DISP_OPEN_CREATE is set, then it status is the
459 * creation failure mode. In either case, one of DISP_LOOKUP_NEG or
460 * DISP_LOOKUP_POS will be set, indicating whether the child lookup
463 * Else, if DISP_LOOKUP_EXECD then d.lustre->it_status is the rc of the
466 int mdc_intent_lock(struct obd_export *exp, struct lustre_id *pid,
467 const char *name, int len, void *lmm, int lmmsize,
468 struct lustre_id *cid, struct lookup_intent *it,
469 int lookup_flags, struct ptlrpc_request **reqp,
470 ldlm_blocking_callback cb_blocking)
472 struct lustre_handle lockh;
473 struct ptlrpc_request *request;
474 struct mds_body *mds_body;
475 struct lustre_handle old_lock;
476 struct ldlm_lock *lock;
481 CDEBUG(D_DLMTRACE, "name: %*s in obj "DLID4", intent: %s flags %#o\n",
482 len, name, OLID4(pid), ldlm_it2str(it->it_op), it->it_flags);
484 if (cid && (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR ||
485 it->it_op == IT_CHDIR)) {
486 /* We could just return 1 immediately, but since we should only
487 * be called in revalidate_it if we already have a lock, let's
489 struct ldlm_res_id res_id = {.name = {id_fid(cid),
491 struct lustre_handle lockh;
492 ldlm_policy_data_t policy;
495 /* For the GETATTR case, ll_revalidate_it issues two separate
496 queries - for LOOKUP and for UPDATE lock because it cannot
497 check them together - we might have those two bits to be
498 present in two separate granted locks */
499 policy.l_inodebits.bits = (it->it_op == IT_GETATTR) ?
500 MDS_INODELOCK_UPDATE : MDS_INODELOCK_LOOKUP;
503 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
504 LDLM_FL_BLOCK_GRANTED, &res_id,
505 LDLM_IBITS, &policy, mode,
510 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
511 LDLM_FL_BLOCK_GRANTED, &res_id,
512 LDLM_IBITS, &policy, mode,
517 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
518 LDLM_FL_BLOCK_GRANTED, &res_id,
519 LDLM_IBITS, &policy, mode,
524 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
525 LDLM_FL_BLOCK_GRANTED, &res_id,
526 LDLM_IBITS, &policy, mode,
530 if (ptlrpcs_check_cred(exp->exp_obd->u.cli.cl_import)) {
531 /* return immediately if no credential held */
532 ldlm_lock_decref(&lockh, mode);
535 memcpy(&LUSTRE_IT(it)->it_lock_handle, &lockh,
537 LUSTRE_IT(it)->it_lock_mode = mode;
540 /* Only return failure if it was not GETATTR by cid (from
546 /* lookup_it may be called only after revalidate_it has run, because
547 * revalidate_it cannot return errors, only zero. Returning zero causes
548 * this call to lookup, which *can* return an error.
550 * We only want to execute the request associated with the intent one
551 * time, however, so don't send the request again. Instead, skip past
552 * this and use the request from revalidate. In this case, revalidate
553 * never dropped its reference, so the refcounts are all OK */
554 if (!it_disposition(it, DISP_ENQ_COMPLETE)) {
555 struct mdc_op_data *op_data;
557 OBD_ALLOC(op_data, sizeof(*op_data));
561 mdc_id2mdc_data(op_data, pid, cid, name, len, 0);
564 op_data->valid |= OBD_MD_FID;
566 rc = mdc_enqueue(exp, LDLM_IBITS, it, it_to_lock_mode(it),
567 op_data, &lockh, lmm, lmmsize,
568 ldlm_completion_ast, cb_blocking, NULL);
569 OBD_FREE(op_data, sizeof(*op_data));
573 memcpy(&LUSTRE_IT(it)->it_lock_handle, &lockh, sizeof(lockh));
575 request = *reqp = LUSTRE_IT(it)->it_data;
576 LASSERT(request != NULL);
578 /* If we're doing an IT_OPEN which did not result in an actual
579 * successful open, then we need to remove the bit which saves this
580 * request for unconditional replay.
582 * It's important that we do this first! Otherwise we might exit the
583 * function without doing so, and try to replay a failed create (bug
585 if (it->it_op & IT_OPEN && request->rq_replay &&
586 (!it_disposition(it, DISP_OPEN_OPEN) || LUSTRE_IT(it)->it_status != 0))
587 mdc_clear_replay_flag(request, LUSTRE_IT(it)->it_status);
589 if (!it_disposition(it, DISP_IT_EXECD)) {
590 /* The server failed before it even started executing the
591 * intent, i.e. because it couldn't unpack the request. */
592 LASSERT(LUSTRE_IT(it)->it_status != 0);
593 RETURN(LUSTRE_IT(it)->it_status);
595 rc = it_open_error(DISP_IT_EXECD, it);
599 mds_body = lustre_msg_buf(request->rq_repmsg, 1, sizeof(*mds_body));
600 LASSERT(mds_body != NULL); /* mdc_enqueue checked */
601 LASSERT_REPSWABBED(request, 1); /* mdc_enqueue swabbed */
603 /* If we were revalidating a fid/name pair, mark the intent in case we
604 * fail and get called again from lookup */
606 it_set_disposition(it, DISP_ENQ_COMPLETE);
607 /* Also: did we find the same inode? */
609 /* we have to compare all the fields but type, because MDS can
610 * return fid/mds/ino/gen if inode lives on another MDS -bzzz */
611 if (!(lookup_flags & LOOKUP_COBD) && !id_equal(cid, &mds_body->id1))
615 rc = it_open_error(DISP_LOOKUP_EXECD, it);
620 * keep requests around for the multiple phases of the call this shows
621 * the DISP_XX must guarantee we make it into the call.
623 if (it_disposition(it, DISP_OPEN_CREATE) &&
624 !it_open_error(DISP_OPEN_CREATE, it))
625 ptlrpc_request_addref(request); /* balanced in ll_create_node */
626 if (it_disposition(it, DISP_OPEN_OPEN) &&
627 !it_open_error(DISP_OPEN_OPEN, it))
628 ptlrpc_request_addref(request); /* balanced in ll_file_open */
630 if (it->it_op & IT_CREAT) {
631 /* XXX this belongs in ll_create_it */
632 } else if (it->it_op == IT_OPEN) {
633 LASSERT(!it_disposition(it, DISP_OPEN_CREATE));
635 LASSERT(it->it_op & (IT_GETATTR | IT_LOOKUP | IT_CHDIR));
639 * if we already have a matching lock, then cancel the new one. We have
640 * to set the data here instead of in mdc_enqueue, because we need to
641 * use the child's inode as the l_ast_data to match, and that's not
642 * available until intent_finish has performed the iget().)
644 lock = ldlm_handle2lock(&lockh);
646 ldlm_policy_data_t policy = lock->l_policy_data;
647 LDLM_DEBUG(lock, "matching against this");
650 LASSERTF(id_fid(&mds_body->id1) == lock->l_resource->lr_name.name[0] &&
651 id_group(&mds_body->id1) == lock->l_resource->lr_name.name[1],
652 "Invalid lock is returned to client. Lock res_is: %lu/%lu, "
653 "response res_id: %lu/%lu.\n",
654 (unsigned long)lock->l_resource->lr_name.name[0],
655 (unsigned long)lock->l_resource->lr_name.name[1],
656 (unsigned long)id_fid(&mds_body->id1),
657 (unsigned long)id_group(&mds_body->id1));
659 memcpy(&old_lock, &lockh, sizeof(lockh));
660 if (ldlm_lock_match(NULL, LDLM_FL_BLOCK_GRANTED, NULL,
661 LDLM_IBITS, &policy, LCK_NL, &old_lock)) {
662 ldlm_lock_decref_and_cancel(&lockh,
663 LUSTRE_IT(it)->it_lock_mode);
664 memcpy(&lockh, &old_lock, sizeof(old_lock));
665 memcpy(&LUSTRE_IT(it)->it_lock_handle, &lockh,
669 CDEBUG(D_DENTRY, "D_IT dentry %*s intent: %s status %d disp %x rc %d\n",
670 len, name, ldlm_it2str(it->it_op), LUSTRE_IT(it)->it_status,
671 LUSTRE_IT(it)->it_disposition, rc);
675 EXPORT_SYMBOL(mdc_intent_lock);