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_snap.h>
40 #include <linux/lprocfs_status.h>
41 #include "mdc_internal.h"
43 int it_disposition(struct lookup_intent *it, int flag)
45 return it->d.lustre.it_disposition & flag;
47 EXPORT_SYMBOL(it_disposition);
49 void it_set_disposition(struct lookup_intent *it, int flag)
51 it->d.lustre.it_disposition |= flag;
53 EXPORT_SYMBOL(it_set_disposition);
55 static void mdc_fid2mdc_op_data(struct mdc_op_data *data, struct ll_uctxt *ctxt,
56 struct ll_fid *f1, struct ll_fid *f2,
57 const char *name, int namelen, int mode)
68 memset(&data->fid2, 0, sizeof(data->fid2));
70 data->namelen = namelen;
71 data->create_mode = mode;
72 data->mod_time = LTIME_S(CURRENT_TIME);
75 static int it_to_lock_mode(struct lookup_intent *it)
77 /* CREAT needs to be tested before open (both could be set) */
78 if (it->it_op & IT_CREAT)
80 else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_LOOKUP |
88 int it_open_error(int phase, struct lookup_intent *it)
90 if (it_disposition(it, DISP_OPEN_OPEN)) {
91 if (phase == DISP_OPEN_OPEN)
92 return it->d.lustre.it_status;
97 if (it_disposition(it, DISP_OPEN_CREATE)) {
98 if (phase == DISP_OPEN_CREATE)
99 return it->d.lustre.it_status;
104 if (it_disposition(it, DISP_LOOKUP_EXECD)) {
105 if (phase == DISP_LOOKUP_EXECD)
106 return it->d.lustre.it_status;
111 if (it_disposition(it, DISP_IT_EXECD)) {
112 if (phase == DISP_IT_EXECD)
113 return it->d.lustre.it_status;
117 CERROR("it disp: %X, status: %d\n", it->d.lustre.it_disposition,
118 it->d.lustre.it_status);
122 EXPORT_SYMBOL(it_open_error);
124 /* this must be called on a lockh that is known to have a referenced lock */
125 int mdc_set_lock_data(struct obd_export *exp, __u64 *l, void *data)
127 struct ldlm_lock *lock;
128 struct lustre_handle *lockh = (struct lustre_handle *)l;
136 lock = ldlm_handle2lock(lockh);
138 LASSERT(lock != NULL);
139 l_lock(&lock->l_resource->lr_namespace->ns_lock);
141 if (lock->l_ast_data && lock->l_ast_data != data) {
142 struct inode *new_inode = data;
143 struct inode *old_inode = lock->l_ast_data;
144 LASSERTF(old_inode->i_state & I_FREEING,
145 "Found existing inode %p/%lu/%u state %lu in lock: "
146 "setting data to %p/%lu/%u\n", old_inode,
147 old_inode->i_ino, old_inode->i_generation,
149 new_inode, new_inode->i_ino, new_inode->i_generation);
152 lock->l_ast_data = data;
153 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
159 EXPORT_SYMBOL(mdc_set_lock_data);
161 int mdc_change_cbdata(struct obd_export *exp, struct ll_fid *fid,
162 ldlm_iterator_t it, void *data)
164 struct ldlm_res_id res_id = { .name = {0} };
167 res_id.name[0] = fid->id;
168 res_id.name[1] = fid->generation;
170 ldlm_change_cbdata(class_exp2obd(exp)->obd_namespace, &res_id, it,
178 int mdc_set_clone_info(struct obd_import *climp, struct lustre_msg *msg,
181 struct client_obd *cli_obd = &climp->imp_obd->u.cli;
182 struct clonefs_info *cl_info;
185 cl_info = (struct clonefs_info *)lustre_msg_buf(msg, offset,
187 memcpy(cl_info, cli_obd->cl_clone_info, sizeof(*cl_info));
192 /* We always reserve enough space in the reply packet for a stripe MD, because
193 * we don't know in advance the file type. */
194 int mdc_enqueue(struct obd_export *exp,
196 struct lookup_intent *it,
198 struct mdc_op_data *data,
199 struct lustre_handle *lockh,
202 ldlm_completion_callback cb_completion,
203 ldlm_blocking_callback cb_blocking,
206 struct ptlrpc_request *req;
207 struct obd_device *obddev = class_exp2obd(exp);
208 struct ldlm_res_id res_id =
209 { .name = {data->fid1.id, data->fid1.generation} };
210 ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_LOOKUP } };
211 int size[6] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
212 int rc, flags = LDLM_FL_HAS_INTENT;
213 int repsize[4] = {sizeof(struct ldlm_reply),
214 sizeof(struct mds_body),
215 obddev->u.cli.cl_max_mds_easize,
216 obddev->u.cli.cl_max_mds_cookiesize};
217 struct ldlm_reply *dlm_rep;
218 struct ldlm_intent *lit;
219 struct ldlm_request *lockreq;
221 unsigned long irqflags;
222 int reply_buffers = 0;
225 // LDLM_DEBUG_NOLOCK("mdsintent=%s,name=%s,dir=%lu",
226 // ldlm_it2str(it->it_op), it_name, it_inode->i_ino);
228 if (it->it_op & IT_OPEN) {
229 it->it_create_mode |= S_IFREG;
230 it->it_create_mode &= ~current->fs->umask;
232 size[2] = sizeof(struct mds_rec_create);
233 size[3] = data->namelen + 1;
234 size[4] = obddev->u.cli.cl_max_mds_easize;
235 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE,
240 spin_lock_irqsave (&req->rq_lock, irqflags);
242 spin_unlock_irqrestore (&req->rq_lock, irqflags);
244 /* pack the intent */
245 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
246 lit->opc = (__u64)it->it_op;
248 /* pack the intended request */
249 mdc_open_pack(req->rq_reqmsg, 2, data, it->it_create_mode, 0,
250 it->it_flags, lmm, lmmsize);
251 /* get ready for the reply */
253 req->rq_replen = lustre_msg_size(3, repsize);
254 } else if (it->it_op & (IT_GETATTR | IT_LOOKUP | IT_CHDIR)) {
255 int valid = OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE;
256 size[2] = sizeof(struct mds_body);
257 size[3] = data->namelen + 1;
259 if (it->it_op & IT_GETATTR)
260 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
262 size[4] = sizeof(struct clonefs_info);
263 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 5,
266 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 4,
273 /* pack the intent */
274 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
275 lit->opc = (__u64)it->it_op;
277 /* pack the intended request */
278 mdc_getattr_pack(req->rq_reqmsg, valid, 2, it->it_flags, data);
280 mdc_set_clone_info(class_exp2cliimp(exp), req->rq_reqmsg, 4);
282 /* get ready for the reply */
284 req->rq_replen = lustre_msg_size(3, repsize);
285 } else if (it->it_op == IT_READDIR) {
286 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
288 size[1] = sizeof(struct clonefs_info);
289 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 2,
292 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 1,
299 mdc_set_clone_info(class_exp2cliimp(exp), req->rq_reqmsg, 1);
301 /* get ready for the reply */
303 req->rq_replen = lustre_msg_size(1, repsize);
304 } else if (it->it_op == IT_UNLINK) {
305 size[2] = sizeof(struct mds_body);
306 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
307 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 3,
312 /* pack the intended request */
313 mdc_getattr_pack(req->rq_reqmsg, 0, 2, 0, data);
315 /* pack the intent */
316 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
317 lit->opc = (__u64)it->it_op;
319 /* get ready for the reply */
321 req->rq_replen = lustre_msg_size(3, repsize);
326 mdc_get_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
327 rc = ldlm_cli_enqueue(exp, req, obddev->obd_namespace, res_id,
328 lock_type, &policy, lock_mode, &flags,cb_blocking,
329 cb_completion, NULL, cb_data, NULL, 0, NULL,
331 mdc_put_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
333 /* Similarly, if we're going to replay this request, we don't want to
334 * actually get a lock, just perform the intent. */
335 if (req->rq_transno || req->rq_replay) {
336 lockreq = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*lockreq));
337 lockreq->lock_flags |= LDLM_FL_INTENT_ONLY;
340 /* This can go when we're sure that this can never happen */
341 LASSERT(rc != -ENOENT);
342 if (rc == ELDLM_LOCK_ABORTED) {
344 memset(lockh, 0, sizeof(*lockh));
346 } else if (rc != 0) {
347 CERROR("ldlm_cli_enqueue: %d\n", rc);
349 ptlrpc_req_finished(req);
351 } else { /* rc = 0 */
352 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
355 /* If the server gave us back a different lock mode, we should
356 * fix up our variables. */
357 if (lock->l_req_mode != lock_mode) {
358 ldlm_lock_addref(lockh, lock->l_req_mode);
359 ldlm_lock_decref(lockh, lock_mode);
360 lock_mode = lock->l_req_mode;
363 ldlm_lock_allow_match(lock);
367 dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
368 LASSERT(dlm_rep != NULL); /* checked by ldlm_cli_enqueue() */
369 LASSERT_REPSWABBED(req, 0); /* swabbed by ldlm_cli_enqueue() */
371 it->d.lustre.it_disposition = (int) dlm_rep->lock_policy_res1;
372 it->d.lustre.it_status = (int) dlm_rep->lock_policy_res2;
373 it->d.lustre.it_lock_mode = lock_mode;
374 it->d.lustre.it_data = req;
376 if (it->d.lustre.it_status < 0 && req->rq_replay) {
377 LASSERT(req->rq_transno == 0);
378 /* Don't hold error requests for replay. */
379 spin_lock(&req->rq_lock);
381 spin_unlock(&req->rq_lock);
384 DEBUG_REQ(D_RPCTRACE, req, "disposition: %x, status: %d",
385 it->d.lustre.it_disposition, it->d.lustre.it_status);
387 /* We know what to expect, so we do any byte flipping required here */
388 LASSERT(reply_buffers == 4 || reply_buffers == 3 || reply_buffers == 1);
389 if (reply_buffers >= 3) {
390 struct mds_body *body;
392 body = lustre_swab_repbuf(req, 1, sizeof (*body),
393 lustre_swab_mds_body);
395 CERROR ("Can't swab mds_body\n");
399 if ((body->valid & OBD_MD_FLEASIZE) != 0) {
401 /* The eadata is opaque; just check that it is
402 * there. Eventually, obd_unpackmd() will check
404 eadata = lustre_swab_repbuf(req, 2, body->eadatasize,
406 if (eadata == NULL) {
407 CERROR ("Missing/short eadata\n");
410 if (it->it_op & IT_OPEN) {
411 replayea = lustre_msg_buf(req->rq_reqmsg, 4,
412 obddev->u.cli.cl_max_mds_easize);
414 memcpy(replayea, eadata, body->eadatasize);
421 EXPORT_SYMBOL(mdc_enqueue);
424 * This long block is all about fixing up the lock and request state
425 * so that it is correct as of the moment _before_ the operation was
426 * applied; that way, the VFS will think that everything is normal and
427 * call Lustre's regular VFS methods.
429 * If we're performing a creation, that means that unless the creation
430 * failed with EEXIST, we should fake up a negative dentry.
432 * For everything else, we want to lookup to succeed.
434 * One additional note: if CREATE or OPEN succeeded, we add an extra
435 * reference to the request because we need to keep it around until
436 * ll_create/ll_open gets called.
438 * The server will return to us, in it_disposition, an indication of
439 * exactly what d.lustre.it_status refers to.
441 * If DISP_OPEN_OPEN is set, then d.lustre.it_status refers to the open() call,
442 * otherwise if DISP_OPEN_CREATE is set, then it status is the
443 * creation failure mode. In either case, one of DISP_LOOKUP_NEG or
444 * DISP_LOOKUP_POS will be set, indicating whether the child lookup
447 * Else, if DISP_LOOKUP_EXECD then d.lustre.it_status is the rc of the
450 int mdc_intent_lock(struct obd_export *exp, struct ll_uctxt *uctxt,
451 struct ll_fid *pfid, const char *name, int len,
452 void *lmm, int lmmsize, struct ll_fid *cfid,
453 struct lookup_intent *it, int lookup_flags,
454 struct ptlrpc_request **reqp,
455 ldlm_blocking_callback cb_blocking)
457 struct lustre_handle lockh;
458 struct ptlrpc_request *request;
460 struct mds_body *mds_body;
461 struct lustre_handle old_lock;
462 struct ldlm_lock *lock;
466 CDEBUG(D_DLMTRACE, "name: %*s in %ld, intent: %s\n", len, name,
467 pfid ? (unsigned long) pfid->id : 0 , ldlm_it2str(it->it_op));
469 if (cfid && (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR ||
470 it->it_op == IT_CHDIR)) {
471 /* We could just return 1 immediately, but since we should only
472 * be called in revalidate_it if we already have a lock, let's
474 struct ldlm_res_id res_id ={.name = {cfid->id,
476 struct lustre_handle lockh;
477 ldlm_policy_data_t policy;
480 /* For the GETATTR case, ll_revalidate_it issues two separate
481 queries - for LOOKUP and for UPDATE lock because if cannot
482 check them together - we might have those two bits to be
483 present in two separate granted locks */
484 policy.l_inodebits.bits =
485 (it->it_op == IT_GETATTR)?MDS_INODELOCK_UPDATE:
486 MDS_INODELOCK_LOOKUP;
488 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
489 LDLM_FL_BLOCK_GRANTED, &res_id,
490 LDLM_IBITS, &policy, LCK_PR, &lockh);
493 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
494 LDLM_FL_BLOCK_GRANTED, &res_id,
495 LDLM_IBITS, &policy, LCK_PW,
499 memcpy(&it->d.lustre.it_lock_handle, &lockh,
501 it->d.lustre.it_lock_mode = mode;
506 /* lookup_it may be called only after revalidate_it has run, because
507 * revalidate_it cannot return errors, only zero. Returning zero causes
508 * this call to lookup, which *can* return an error.
510 * We only want to execute the request associated with the intent one
511 * time, however, so don't send the request again. Instead, skip past
512 * this and use the request from revalidate. In this case, revalidate
513 * never dropped its reference, so the refcounts are all OK */
514 if (!it_disposition(it, DISP_ENQ_COMPLETE)) {
515 struct mdc_op_data op_data;
516 mdc_fid2mdc_op_data(&op_data, uctxt, pfid, cfid, name, len, 0);
518 rc = mdc_enqueue(exp, LDLM_IBITS, it, it_to_lock_mode(it),
519 &op_data, &lockh, lmm, lmmsize,
520 ldlm_completion_ast, cb_blocking, NULL);
523 memcpy(&it->d.lustre.it_lock_handle, &lockh, sizeof(lockh));
525 request = *reqp = it->d.lustre.it_data;
526 LASSERT(request != NULL);
528 if (!it_disposition(it, DISP_IT_EXECD)) {
529 /* The server failed before it even started executing the
530 * intent, i.e. because it couldn't unpack the request. */
531 LASSERT(it->d.lustre.it_status != 0);
532 RETURN(it->d.lustre.it_status);
534 rc = it_open_error(DISP_IT_EXECD, it);
538 mds_body = lustre_msg_buf(request->rq_repmsg, 1, sizeof(*mds_body));
539 LASSERT(mds_body != NULL); /* mdc_enqueue checked */
540 LASSERT_REPSWABBED(request, 1); /* mdc_enqueue swabbed */
542 /* If we were revalidating a fid/name pair, mark the intent in
543 * case we fail and get called again from lookup */
545 it_set_disposition(it, DISP_ENQ_COMPLETE);
546 /* Also: did we find the same inode? */
547 /* we have to compare all the fields but type, because
548 * MDS can return mds/ino/generation triple if inode
549 * lives on another MDS -bzzz */
550 if (cfid->generation != mds_body->fid1.generation ||
551 cfid->id != mds_body->fid1.id ||
552 cfid->mds != mds_body->fid1.mds)
556 /* If we're doing an IT_OPEN which did not result in an actual
557 * successful open, then we need to remove the bit which saves
558 * this request for unconditional replay. */
559 if (it->it_op & IT_OPEN) {
560 if (!it_disposition(it, DISP_OPEN_OPEN) ||
561 it->d.lustre.it_status != 0) {
562 unsigned long irqflags;
564 spin_lock_irqsave(&request->rq_lock, irqflags);
565 request->rq_replay = 0;
566 spin_unlock_irqrestore(&request->rq_lock, irqflags);
570 rc = it_open_error(DISP_LOOKUP_EXECD, it);
574 /* keep requests around for the multiple phases of the call
575 * this shows the DISP_XX must guarantee we make it into the call
577 if (it_disposition(it, DISP_OPEN_CREATE) &&
578 !it_open_error(DISP_OPEN_CREATE, it))
579 ptlrpc_request_addref(request);
580 if (it_disposition(it, DISP_OPEN_OPEN) &&
581 !it_open_error(DISP_OPEN_OPEN, it))
582 ptlrpc_request_addref(request);
584 if (it->it_op & IT_CREAT) {
585 /* XXX this belongs in ll_create_iit */
586 } else if (it->it_op == IT_OPEN) {
587 LASSERT(!it_disposition(it, DISP_OPEN_CREATE));
589 LASSERT(it->it_op & (IT_GETATTR | IT_LOOKUP | IT_CHDIR));
592 /* If we already have a matching lock, then cancel the new
593 * one. We have to set the data here instead of in
594 * mdc_enqueue, because we need to use the child's inode as
595 * the l_ast_data to match, and that's not available until
596 * intent_finish has performed the iget().) */
597 lock = ldlm_handle2lock(&lockh);
599 ldlm_policy_data_t policy = lock->l_policy_data;
600 LDLM_DEBUG(lock, "matching against this");
602 memcpy(&old_lock, &lockh, sizeof(lockh));
603 if (ldlm_lock_match(NULL, LDLM_FL_BLOCK_GRANTED, NULL,
604 LDLM_IBITS, &policy, LCK_NL, &old_lock)) {
605 ldlm_lock_decref_and_cancel(&lockh,
606 it->d.lustre.it_lock_mode);
607 memcpy(&lockh, &old_lock, sizeof(old_lock));
608 memcpy(&it->d.lustre.it_lock_handle, &lockh,
612 CDEBUG(D_DENTRY, "D_IT dentry %*s intent: %s status %d disp %x rc %d\n",
613 len, name, ldlm_it2str(it->it_op), it->d.lustre.it_status,
614 it->d.lustre.it_disposition, rc);
618 EXPORT_SYMBOL(mdc_intent_lock);