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/lprocfs_status.h>
40 #include <linux/lustre_acl.h>
41 #include <linux/lustre_lite.h>
42 #include "mdc_internal.h"
44 int it_disposition(struct lookup_intent *it, int flag)
46 return LUSTRE_IT(it)->it_disposition & flag;
48 EXPORT_SYMBOL(it_disposition);
50 void it_set_disposition(struct lookup_intent *it, int flag)
52 LUSTRE_IT(it)->it_disposition |= flag;
54 EXPORT_SYMBOL(it_set_disposition);
56 static void mdc_id2mdc_data(struct mdc_op_data *data,
60 int namelen, int mode)
71 data->namelen = namelen;
72 data->create_mode = mode;
73 data->mod_time = LTIME_S(CURRENT_TIME);
76 static int it_to_lock_mode(struct lookup_intent *it)
78 /* CREAT needs to be tested before open (both could be set) */
79 if (it->it_op & IT_CREAT)
81 else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_LOOKUP |
89 int it_open_error(int phase, struct lookup_intent *it)
91 if (it_disposition(it, DISP_OPEN_OPEN)) {
92 if (phase == DISP_OPEN_OPEN)
93 return LUSTRE_IT(it)->it_status;
98 if (it_disposition(it, DISP_OPEN_CREATE)) {
99 if (phase == DISP_OPEN_CREATE)
100 return LUSTRE_IT(it)->it_status;
105 if (it_disposition(it, DISP_LOOKUP_EXECD)) {
106 if (phase == DISP_LOOKUP_EXECD)
107 return LUSTRE_IT(it)->it_status;
112 if (it_disposition(it, DISP_IT_EXECD)) {
113 if (phase == DISP_IT_EXECD)
114 return LUSTRE_IT(it)->it_status;
118 CERROR("it disp: %X, status: %d\n", LUSTRE_IT(it)->it_disposition,
119 LUSTRE_IT(it)->it_status);
123 EXPORT_SYMBOL(it_open_error);
125 /* this must be called on a lockh that is known to have a referenced lock */
126 int mdc_set_lock_data(struct obd_export *exp, __u64 *l, void *data)
128 struct ldlm_lock *lock;
129 struct lustre_handle *lockh = (struct lustre_handle *)l;
137 lock = ldlm_handle2lock(lockh);
139 LASSERT(lock != NULL);
140 l_lock(&lock->l_resource->lr_namespace->ns_lock);
142 if (lock->l_ast_data && lock->l_ast_data != data) {
143 struct inode *new_inode = data;
144 struct inode *old_inode = lock->l_ast_data;
145 LASSERTF(old_inode->i_state & I_FREEING,
146 "Found existing inode %p/%lu/%u state %lu in lock: "
147 "setting data to %p/%lu/%u\n", old_inode,
148 old_inode->i_ino, old_inode->i_generation,
149 old_inode->i_state, new_inode, new_inode->i_ino,
150 new_inode->i_generation);
153 lock->l_ast_data = data;
154 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
160 EXPORT_SYMBOL(mdc_set_lock_data);
162 int mdc_change_cbdata(struct obd_export *exp, struct lustre_id *id,
163 ldlm_iterator_t it, void *data)
165 struct ldlm_res_id res_id = { .name = {0} };
168 res_id.name[0] = id_fid(id);
169 res_id.name[1] = id_group(id);
171 ldlm_change_cbdata(class_exp2obd(exp)->obd_namespace,
178 /* We always reserve enough space in the reply packet for a stripe MD, because
179 * we don't know in advance the file type. */
180 int mdc_enqueue(struct obd_export *exp,
182 struct lookup_intent *it,
184 struct mdc_op_data *data,
185 struct lustre_handle *lockh,
188 ldlm_completion_callback cb_completion,
189 ldlm_blocking_callback cb_blocking,
192 struct ptlrpc_request *req;
193 struct ldlm_res_id res_id = {
194 .name = {id_fid(&data->id1), id_group(&data->id1)}
196 struct obd_device *obddev = class_exp2obd(exp);
197 ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_LOOKUP } };
198 struct ldlm_intent *lit;
199 struct ldlm_request *lockreq;
200 struct ldlm_reply *dlm_rep;
201 int reqsize[6] = {[MDS_REQ_SECDESC_OFF] = 0,
202 [MDS_REQ_INTENT_LOCKREQ_OFF] = sizeof(*lockreq),
203 [MDS_REQ_INTENT_IT_OFF] = sizeof(*lit)};
204 int repsize[5] = {sizeof(struct ldlm_reply),
205 sizeof(struct mds_body),
206 obddev->u.cli.cl_max_mds_easize};
207 int req_buffers = 3, reply_buffers = 0;
208 int rc, flags = LDLM_FL_HAS_INTENT;
210 unsigned long irqflags;
213 // LDLM_DEBUG_NOLOCK("mdsintent=%s,name=%s,dir=%lu",
214 // ldlm_it2str(it->it_op), it_name, it_inode->i_ino);
216 reqsize[0] = mdc_get_secdesc_size();
218 if (it->it_op & IT_OPEN) {
219 it->it_create_mode |= S_IFREG;
220 it->it_create_mode &= ~current->fs->umask;
222 reqsize[req_buffers++] = sizeof(struct mds_rec_create);
223 reqsize[req_buffers++] = data->namelen + 1;
224 reqsize[req_buffers++] = obddev->u.cli.cl_max_mds_easize;
225 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
226 LDLM_ENQUEUE, req_buffers, reqsize, NULL);
230 spin_lock_irqsave (&req->rq_lock, irqflags);
232 spin_unlock_irqrestore (&req->rq_lock, irqflags);
234 /* pack the intent */
235 lit = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_INTENT_IT_OFF,
237 lit->opc = (__u64)it->it_op;
239 /* pack the intended request */
240 mdc_open_pack(req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF, data,
241 it->it_create_mode, 0, it->it_flags,
243 /* get ready for the reply */
245 repsize[4] = xattr_acl_size(LL_ACL_MAX_ENTRIES);
247 req->rq_replen = lustre_msg_size(5, repsize);
248 } else if (it->it_op & (IT_GETATTR | IT_LOOKUP | IT_CHDIR)) {
249 __u64 valid = data->valid | OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE |
252 reqsize[req_buffers++] = sizeof(struct mds_body);
253 reqsize[req_buffers++] = data->namelen + 1;
255 if (it->it_op & IT_GETATTR)
256 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
258 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
259 LDLM_ENQUEUE, req_buffers, reqsize, NULL);
264 /* pack the intent */
265 lit = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_INTENT_IT_OFF,
267 lit->opc = (__u64)it->it_op;
269 /* pack the intended request */
270 mdc_getattr_pack(req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF,
271 valid, it->it_flags, data);
273 /* get ready for the reply */
275 repsize[4] = xattr_acl_size(LL_ACL_MAX_ENTRIES);
277 req->rq_replen = lustre_msg_size(5, repsize);
278 } else if (it->it_op == IT_READDIR) {
279 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
280 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
281 LDLM_ENQUEUE, 2, reqsize, NULL);
285 /* get ready for the reply */
287 req->rq_replen = lustre_msg_size(1, repsize);
288 } else if (it->it_op == IT_UNLINK) {
289 reqsize[req_buffers++] = sizeof(struct mds_body);
290 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
291 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
292 LDLM_ENQUEUE, req_buffers, reqsize, NULL);
296 /* pack the intended request */
297 mdc_getattr_pack(req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF,
300 /* pack the intent */
301 lit = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_INTENT_IT_OFF,
303 lit->opc = (__u64)it->it_op;
305 /* get ready for the reply */
307 req->rq_replen = lustre_msg_size(3, repsize);
313 mdc_pack_secdesc(req, reqsize[0]);
315 mdc_get_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
316 rc = ldlm_cli_enqueue(exp, req, obddev->obd_namespace, res_id,
317 lock_type, &policy, lock_mode, &flags,cb_blocking,
318 cb_completion, NULL, cb_data, NULL, 0, NULL,
320 mdc_put_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
322 /* Similarly, if we're going to replay this request, we don't want to
323 * actually get a lock, just perform the intent. */
324 if (req->rq_transno || req->rq_replay) {
325 lockreq = lustre_msg_buf(req->rq_reqmsg,
326 MDS_REQ_INTENT_LOCKREQ_OFF,
328 lockreq->lock_flags |= LDLM_FL_INTENT_ONLY;
331 /* This can go when we're sure that this can never happen */
332 LASSERT(rc != -ENOENT);
333 if (rc == ELDLM_LOCK_ABORTED) {
335 memset(lockh, 0, sizeof(*lockh));
337 } else if (rc != 0) {
338 CERROR("ldlm_cli_enqueue: %d\n", rc);
340 ptlrpc_req_finished(req);
342 } else { /* rc = 0 */
343 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
346 /* If the server gave us back a different lock mode, we should
347 * fix up our variables. */
348 if (lock->l_req_mode != lock_mode) {
349 ldlm_lock_addref(lockh, lock->l_req_mode);
350 ldlm_lock_decref(lockh, lock_mode);
351 lock_mode = lock->l_req_mode;
354 ldlm_lock_allow_match(lock);
358 dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
359 LASSERT(dlm_rep != NULL); /* checked by ldlm_cli_enqueue() */
360 LASSERT_REPSWABBED(req, 0); /* swabbed by ldlm_cli_enqueue() */
362 LUSTRE_IT(it)->it_disposition = (int) dlm_rep->lock_policy_res1;
363 LUSTRE_IT(it)->it_status = (int) dlm_rep->lock_policy_res2;
364 LUSTRE_IT(it)->it_lock_mode = lock_mode;
365 LUSTRE_IT(it)->it_data = req;
367 if (LUSTRE_IT(it)->it_status < 0 && req->rq_replay) {
368 LASSERT(req->rq_transno == 0);
369 /* Don't hold error requests for replay. */
370 spin_lock(&req->rq_lock);
372 spin_unlock(&req->rq_lock);
375 DEBUG_REQ(D_RPCTRACE, req, "disposition: %x, status: %d",
376 LUSTRE_IT(it)->it_disposition, LUSTRE_IT(it)->it_status);
378 /* We know what to expect, so we do any byte flipping required here */
379 LASSERT(reply_buffers == 5 || reply_buffers == 4 ||
380 reply_buffers == 3 || reply_buffers == 1);
381 if (reply_buffers >= 3) {
382 struct mds_body *body;
384 body = lustre_swab_repbuf(req, 1, sizeof (*body),
385 lustre_swab_mds_body);
387 CERROR ("Can't swab mds_body\n");
391 if ((body->valid & OBD_MD_FLEASIZE) != 0) {
392 /* The eadata is opaque; just check that it is there.
393 * Eventually, obd_unpackmd() will check the contents */
394 eadata = lustre_swab_repbuf(req, 2, body->eadatasize,
396 if (eadata == NULL) {
397 CERROR ("Missing/short eadata\n");
400 if (it->it_op & IT_OPEN) {
403 replayea = lustre_msg_buf(req->rq_reqmsg,
404 MDS_REQ_INTENT_REC_OFF + 2,
407 memcpy(replayea, eadata, body->eadatasize);
409 LASSERT(req->rq_reqmsg->bufcount == 6);
410 req->rq_reqmsg->buflens[5] = body->eadatasize;
411 /* If this isn't the last buffer, we might
412 * have to shift other data around. */
419 EXPORT_SYMBOL(mdc_enqueue);
422 * This long block is all about fixing up the lock and request state
423 * so that it is correct as of the moment _before_ the operation was
424 * applied; that way, the VFS will think that everything is normal and
425 * call Lustre's regular VFS methods.
427 * If we're performing a creation, that means that unless the creation
428 * failed with EEXIST, we should fake up a negative dentry.
430 * For everything else, we want to lookup to succeed.
432 * One additional note: if CREATE or OPEN succeeded, we add an extra
433 * reference to the request because we need to keep it around until
434 * ll_create/ll_open gets called.
436 * The server will return to us, in it_disposition, an indication of
437 * exactly what d.lustre->it_status refers to.
439 * If DISP_OPEN_OPEN is set, then d.lustre->it_status refers to the open() call,
440 * otherwise if DISP_OPEN_CREATE is set, then it status is the
441 * creation failure mode. In either case, one of DISP_LOOKUP_NEG or
442 * DISP_LOOKUP_POS will be set, indicating whether the child lookup
445 * Else, if DISP_LOOKUP_EXECD then d.lustre->it_status is the rc of the
448 int mdc_intent_lock(struct obd_export *exp, struct lustre_id *pid,
449 const char *name, int len, void *lmm, int lmmsize,
450 struct lustre_id *cid, struct lookup_intent *it,
451 int lookup_flags, struct ptlrpc_request **reqp,
452 ldlm_blocking_callback cb_blocking)
454 struct lustre_handle lockh;
455 struct ptlrpc_request *request;
456 struct mds_body *mds_body;
457 struct lustre_handle old_lock;
458 struct ldlm_lock *lock;
463 CDEBUG(D_DLMTRACE, "name: %*s in obj "DLID4", intent: %s flags %#o\n",
464 len, name, OLID4(pid), ldlm_it2str(it->it_op), it->it_flags);
466 if (cid && (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR ||
467 it->it_op == IT_CHDIR)) {
468 /* We could just return 1 immediately, but since we should only
469 * be called in revalidate_it if we already have a lock, let's
471 struct ldlm_res_id res_id = {.name = {id_fid(cid),
473 struct lustre_handle lockh;
474 ldlm_policy_data_t policy;
477 /* For the GETATTR case, ll_revalidate_it issues two separate
478 queries - for LOOKUP and for UPDATE lock because it cannot
479 check them together - we might have those two bits to be
480 present in two separate granted locks */
481 policy.l_inodebits.bits = (it->it_op == IT_GETATTR) ?
482 MDS_INODELOCK_UPDATE: MDS_INODELOCK_LOOKUP;
485 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
486 LDLM_FL_BLOCK_GRANTED, &res_id,
487 LDLM_IBITS, &policy, LCK_PR, &lockh);
490 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
491 LDLM_FL_BLOCK_GRANTED, &res_id,
492 LDLM_IBITS, &policy, LCK_PW,
496 memcpy(&LUSTRE_IT(it)->it_lock_handle, &lockh,
498 LUSTRE_IT(it)->it_lock_mode = mode;
501 /* Only return failure if it was not GETATTR by cid (from
507 /* lookup_it may be called only after revalidate_it has run, because
508 * revalidate_it cannot return errors, only zero. Returning zero causes
509 * this call to lookup, which *can* return an error.
511 * We only want to execute the request associated with the intent one
512 * time, however, so don't send the request again. Instead, skip past
513 * this and use the request from revalidate. In this case, revalidate
514 * never dropped its reference, so the refcounts are all OK */
515 if (!it_disposition(it, DISP_ENQ_COMPLETE)) {
516 struct mdc_op_data *op_data;
518 OBD_ALLOC(op_data, sizeof(*op_data));
522 mdc_id2mdc_data(op_data, pid, cid, name, len, 0);
525 op_data->valid |= OBD_MD_FID;
527 rc = mdc_enqueue(exp, LDLM_IBITS, it, it_to_lock_mode(it),
528 op_data, &lockh, lmm, lmmsize,
529 ldlm_completion_ast, cb_blocking, NULL);
530 OBD_FREE(op_data, sizeof(*op_data));
534 memcpy(&LUSTRE_IT(it)->it_lock_handle, &lockh, sizeof(lockh));
536 request = *reqp = LUSTRE_IT(it)->it_data;
537 LASSERT(request != NULL);
539 /* If we're doing an IT_OPEN which did not result in an actual
540 * successful open, then we need to remove the bit which saves this
541 * request for unconditional replay.
543 * It's important that we do this first! Otherwise we might exit the
544 * function without doing so, and try to replay a failed create (bug
546 if (it->it_op & IT_OPEN) {
547 if (!it_disposition(it, DISP_OPEN_OPEN) ||
548 LUSTRE_IT(it)->it_status != 0) {
549 unsigned long irqflags;
551 spin_lock_irqsave(&request->rq_lock, irqflags);
552 request->rq_replay = 0;
553 spin_unlock_irqrestore(&request->rq_lock, irqflags);
556 if (!it_disposition(it, DISP_IT_EXECD)) {
557 /* The server failed before it even started executing the
558 * intent, i.e. because it couldn't unpack the request. */
559 LASSERT(LUSTRE_IT(it)->it_status != 0);
560 RETURN(LUSTRE_IT(it)->it_status);
562 rc = it_open_error(DISP_IT_EXECD, it);
566 mds_body = lustre_msg_buf(request->rq_repmsg, 1, sizeof(*mds_body));
567 LASSERT(mds_body != NULL); /* mdc_enqueue checked */
568 LASSERT_REPSWABBED(request, 1); /* mdc_enqueue swabbed */
570 /* If we were revalidating a fid/name pair, mark the intent in case we
571 * fail and get called again from lookup */
573 it_set_disposition(it, DISP_ENQ_COMPLETE);
574 /* Also: did we find the same inode? */
576 /* we have to compare all the fields but type, because MDS can
577 * return fid/mds/ino/gen if inode lives on another MDS -bzzz */
578 if (!id_equal(cid, &mds_body->id1))
582 rc = it_open_error(DISP_LOOKUP_EXECD, it);
587 * keep requests around for the multiple phases of the call this shows
588 * the DISP_XX must guarantee we make it into the call.
590 if (it_disposition(it, DISP_OPEN_CREATE) &&
591 !it_open_error(DISP_OPEN_CREATE, it))
592 ptlrpc_request_addref(request); /* balanced in ll_create_node */
593 if (it_disposition(it, DISP_OPEN_OPEN) &&
594 !it_open_error(DISP_OPEN_OPEN, it))
595 ptlrpc_request_addref(request); /* balanced in ll_file_open */
597 if (it->it_op & IT_CREAT) {
598 /* XXX this belongs in ll_create_it */
599 } else if (it->it_op == IT_OPEN) {
600 LASSERT(!it_disposition(it, DISP_OPEN_CREATE));
602 LASSERT(it->it_op & (IT_GETATTR | IT_LOOKUP | IT_CHDIR));
606 * if we already have a matching lock, then cancel the new one. We have
607 * to set the data here instead of in mdc_enqueue, because we need to
608 * use the child's inode as the l_ast_data to match, and that's not
609 * available until intent_finish has performed the iget().)
611 lock = ldlm_handle2lock(&lockh);
613 ldlm_policy_data_t policy = lock->l_policy_data;
614 LDLM_DEBUG(lock, "matching against this");
617 LASSERTF(id_fid(&mds_body->id1) == lock->l_resource->lr_name.name[0] &&
618 id_group(&mds_body->id1) == lock->l_resource->lr_name.name[1],
619 "Invalid lock is returned to client. Lock res_is: %lu/%lu, "
620 "response res_id: %lu/%lu.\n",
621 (unsigned long)lock->l_resource->lr_name.name[0],
622 (unsigned long)lock->l_resource->lr_name.name[1],
623 (unsigned long)id_fid(&mds_body->id1),
624 (unsigned long)id_group(&mds_body->id1));
626 memcpy(&old_lock, &lockh, sizeof(lockh));
627 if (ldlm_lock_match(NULL, LDLM_FL_BLOCK_GRANTED, NULL,
628 LDLM_IBITS, &policy, LCK_NL, &old_lock)) {
629 ldlm_lock_decref_and_cancel(&lockh,
630 LUSTRE_IT(it)->it_lock_mode);
631 memcpy(&lockh, &old_lock, sizeof(old_lock));
632 memcpy(&LUSTRE_IT(it)->it_lock_handle, &lockh,
636 CDEBUG(D_DENTRY, "D_IT dentry %*s intent: %s status %d disp %x rc %d\n",
637 len, name, ldlm_it2str(it->it_op), LUSTRE_IT(it)->it_status,
638 LUSTRE_IT(it)->it_disposition, rc);
642 EXPORT_SYMBOL(mdc_intent_lock);