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 the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
26 # define EXPORT_SYMTAB
28 #define DEBUG_SUBSYSTEM S_MDC
31 # include <linux/module.h>
32 # include <linux/pagemap.h>
33 # include <linux/miscdevice.h>
34 # include <linux/init.h>
36 # include <liblustre.h>
39 #include <linux/obd_class.h>
40 #include <linux/lustre_mds.h>
41 #include <linux/lustre_dlm.h>
42 #include <linux/lprocfs_status.h>
43 #include "mdc_internal.h"
45 int it_disposition(struct lookup_intent *it, int flag)
47 return it->d.lustre.it_disposition & flag;
49 EXPORT_SYMBOL(it_disposition);
51 void it_set_disposition(struct lookup_intent *it, int flag)
53 it->d.lustre.it_disposition |= flag;
55 EXPORT_SYMBOL(it_set_disposition);
57 static int it_to_lock_mode(struct lookup_intent *it)
59 /* CREAT needs to be tested before open (both could be set) */
60 if (it->it_op & IT_CREAT)
62 else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_LOOKUP))
69 int it_open_error(int phase, struct lookup_intent *it)
71 if (it_disposition(it, DISP_OPEN_OPEN)) {
72 if (phase >= DISP_OPEN_OPEN)
73 return it->d.lustre.it_status;
78 if (it_disposition(it, DISP_OPEN_CREATE)) {
79 if (phase >= DISP_OPEN_CREATE)
80 return it->d.lustre.it_status;
85 if (it_disposition(it, DISP_LOOKUP_EXECD)) {
86 if (phase >= DISP_LOOKUP_EXECD)
87 return it->d.lustre.it_status;
92 if (it_disposition(it, DISP_IT_EXECD)) {
93 if (phase >= DISP_IT_EXECD)
94 return it->d.lustre.it_status;
98 CERROR("it disp: %X, status: %d\n", it->d.lustre.it_disposition,
99 it->d.lustre.it_status);
103 EXPORT_SYMBOL(it_open_error);
105 /* this must be called on a lockh that is known to have a referenced lock */
106 void mdc_set_lock_data(__u64 *l, void *data)
108 struct ldlm_lock *lock;
109 struct lustre_handle *lockh = (struct lustre_handle *)l;
117 lock = ldlm_handle2lock(lockh);
119 LASSERT(lock != NULL);
120 l_lock(&lock->l_resource->lr_namespace->ns_lock);
122 if (lock->l_ast_data && lock->l_ast_data != data) {
123 struct inode *new_inode = data;
124 struct inode *old_inode = lock->l_ast_data;
125 LASSERTF(old_inode->i_state & I_FREEING,
126 "Found existing inode %p/%lu/%u state %lu in lock: "
127 "setting data to %p/%lu/%u\n", old_inode,
128 old_inode->i_ino, old_inode->i_generation,
130 new_inode, new_inode->i_ino, new_inode->i_generation);
133 lock->l_ast_data = data;
134 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
139 EXPORT_SYMBOL(mdc_set_lock_data);
141 int mdc_change_cbdata(struct obd_export *exp, struct ll_fid *fid,
142 ldlm_iterator_t it, void *data)
144 struct ldlm_res_id res_id = { .name = {0} };
147 res_id.name[0] = fid->id;
148 res_id.name[1] = fid->generation;
150 ldlm_change_cbdata(class_exp2obd(exp)->obd_namespace, &res_id, it,
157 static inline void mdc_clear_replay_flag(struct ptlrpc_request *req, int rc)
159 /* Don't hold error requests for replay. */
160 if (req->rq_replay) {
161 unsigned long irqflags;
162 spin_lock_irqsave(&req->rq_lock, irqflags);
164 spin_unlock_irqrestore(&req->rq_lock, irqflags);
166 if (rc && req->rq_transno != 0) {
167 DEBUG_REQ(D_ERROR, req, "transno returned on error rc %d", rc);
172 static int round_up(int val)
182 /* Save a large LOV EA into the request buffer so that it is available
183 * for replay. We don't do this in the initial request because the
184 * original request doesn't need this buffer (at most it sends just the
185 * lov_mds_md) and it is a waste of RAM/bandwidth to send the empty
186 * buffer and may also be difficult to allocate and save a very large
187 * request buffer for each open. (bug 5707)
189 * OOM here may cause recovery failure if lmm is needed (only for the
190 * original open if the MDS crashed just when this client also OOM'd)
191 * but this is incredibly unlikely, and questionable whether the client
192 * could do MDS recovery under OOM anyways... */
193 static void mdc_realloc_openmsg(struct ptlrpc_request *req,
194 struct mds_body *body, int size[5])
196 int new_size, old_size;
197 struct lustre_msg *new_msg;
200 old_size = lustre_msg_size(5, size);
202 size[4] = body->eadatasize;
203 new_size = lustre_msg_size(5, size);
204 OBD_ALLOC(new_msg, new_size);
205 if (new_msg != NULL) {
206 struct lustre_msg *old_msg = req->rq_reqmsg;
207 unsigned long irqflags;
209 DEBUG_REQ(D_INFO, req, "replace reqmsg for larger EA %u\n",
211 memcpy(new_msg, old_msg, old_size);
212 new_msg->buflens[4] = body->eadatasize;
214 spin_lock_irqsave(&req->rq_lock, irqflags);
215 req->rq_reqmsg = new_msg;
216 req->rq_reqlen = new_size;
217 spin_unlock_irqrestore(&req->rq_lock, irqflags);
219 OBD_FREE(old_msg, old_size);
221 body->valid &= ~OBD_MD_FLEASIZE;
222 body->eadatasize = 0;
226 /* We always reserve enough space in the reply packet for a stripe MD, because
227 * we don't know in advance the file type. */
228 int mdc_enqueue(struct obd_export *exp,
230 struct lookup_intent *it,
232 struct mdc_op_data *data,
233 struct lustre_handle *lockh,
236 ldlm_completion_callback cb_completion,
237 ldlm_blocking_callback cb_blocking,
238 void *cb_data, int extra_lock_flags)
240 struct ptlrpc_request *req;
241 struct obd_device *obddev = class_exp2obd(exp);
242 struct ldlm_res_id res_id =
243 { .name = {data->fid1.id, data->fid1.generation} };
244 int size[5] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
245 int rc, flags = extra_lock_flags | LDLM_FL_HAS_INTENT;
246 int repsize[4] = {sizeof(struct ldlm_reply),
247 sizeof(struct mds_body),
248 obddev->u.cli.cl_max_mds_easize,
249 obddev->u.cli.cl_max_mds_cookiesize};
250 struct ldlm_reply *dlm_rep;
251 struct ldlm_intent *lit;
252 struct ldlm_request *lockreq;
254 unsigned long irqflags;
255 int reply_buffers = 0;
258 // LDLM_DEBUG_NOLOCK("mdsintent=%s,name=%s,dir=%lu",
259 // ldlm_it2str(it->it_op), it_name, it_inode->i_ino);
261 if (it->it_op & IT_OPEN) {
262 it->it_create_mode |= S_IFREG;
264 size[2] = sizeof(struct mds_rec_create);
265 size[3] = data->namelen + 1;
266 /* As an optimization, we allocate an RPC request buffer for
267 * at least a default-sized LOV EA even if we aren't sending
268 * one. We grow the whole request to the next power-of-two
269 * size since we get that much from a slab allocation anyways.
270 * This avoids an allocation below in the common case where
271 * we need to save a default-sized LOV EA for open replay. */
272 size[4] = max(lmmsize, obddev->u.cli.cl_default_mds_easize);
273 rc = lustre_msg_size(5, size);
275 size[4] = min(size[4] + round_up(rc) - rc,
276 obddev->u.cli.cl_max_mds_easize);
277 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE,
282 spin_lock_irqsave (&req->rq_lock, irqflags);
284 spin_unlock_irqrestore (&req->rq_lock, irqflags);
286 /* pack the intent */
287 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
288 lit->opc = (__u64)it->it_op;
290 /* pack the intended request */
291 mdc_open_pack(req, 2, data, it->it_create_mode, 0,
292 it->it_flags, lmm, lmmsize);
293 /* get ready for the reply */
295 req->rq_replen = lustre_msg_size(3, repsize);
296 } else if (it->it_op & IT_UNLINK) {
297 size[2] = sizeof(struct mds_rec_unlink);
298 size[3] = data->namelen + 1;
299 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 4,
304 /* pack the intent */
305 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
306 lit->opc = (__u64)it->it_op;
308 /* pack the intended request */
309 mdc_unlink_pack(req, 2, data);
310 /* get ready for the reply */
312 req->rq_replen = lustre_msg_size(4, repsize);
313 } else if (it->it_op & (IT_GETATTR | IT_LOOKUP)) {
314 obd_valid valid = OBD_MD_FLGETATTR | OBD_MD_FLEASIZE;
315 size[2] = sizeof(struct mds_body);
316 size[3] = data->namelen + 1;
318 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 4,
323 /* pack the intent */
324 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
325 lit->opc = (__u64)it->it_op;
327 /* pack the intended request */
328 mdc_getattr_pack(req, valid, 2, it->it_flags, data);
329 /* get ready for the reply */
331 req->rq_replen = lustre_msg_size(3, repsize);
332 } else if (it->it_op == IT_READDIR) {
333 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 1,
338 /* get ready for the reply */
340 req->rq_replen = lustre_msg_size(1, repsize);
346 mdc_get_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
347 rc = ldlm_cli_enqueue(exp, req, obddev->obd_namespace, res_id,
348 lock_type, NULL, lock_mode, &flags, cb_blocking,
349 cb_completion, NULL, cb_data, NULL, 0, NULL,
351 mdc_put_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
353 /* Similarly, if we're going to replay this request, we don't want to
354 * actually get a lock, just perform the intent. */
355 if (req->rq_transno || req->rq_replay) {
356 lockreq = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*lockreq));
357 lockreq->lock_flags |= LDLM_FL_INTENT_ONLY;
360 /* This can go when we're sure that this can never happen */
361 LASSERT(rc != -ENOENT);
362 if (rc == ELDLM_LOCK_ABORTED) {
364 memset(lockh, 0, sizeof(*lockh));
366 } else if (rc != 0) {
367 CERROR("ldlm_cli_enqueue: %d\n", rc);
368 LASSERTF(rc < 0, "rc %d\n", rc);
369 mdc_clear_replay_flag(req, rc);
370 ptlrpc_req_finished(req);
372 } else { /* rc = 0 */
373 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
376 /* If the server gave us back a different lock mode, we should
377 * fix up our variables. */
378 if (lock->l_req_mode != lock_mode) {
379 ldlm_lock_addref(lockh, lock->l_req_mode);
380 ldlm_lock_decref(lockh, lock_mode);
381 lock_mode = lock->l_req_mode;
384 ldlm_lock_allow_match(lock);
388 dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
389 LASSERT(dlm_rep != NULL); /* checked by ldlm_cli_enqueue() */
390 LASSERT_REPSWABBED(req, 0); /* swabbed by ldlm_cli_enqueue() */
392 it->d.lustre.it_disposition = (int) dlm_rep->lock_policy_res1;
393 it->d.lustre.it_status = (int) dlm_rep->lock_policy_res2;
394 it->d.lustre.it_lock_mode = lock_mode;
395 it->d.lustre.it_data = req;
397 if (it->d.lustre.it_status < 0 && req->rq_replay)
398 mdc_clear_replay_flag(req, it->d.lustre.it_status);
400 DEBUG_REQ(D_RPCTRACE, req, "op: %d disposition: %x, status: %d",
401 it->it_op,it->d.lustre.it_disposition,it->d.lustre.it_status);
403 /* We know what to expect, so we do any byte flipping required here */
404 LASSERT(reply_buffers == 4 || reply_buffers == 3 || reply_buffers == 1);
405 if (reply_buffers >= 3) {
406 struct mds_body *body;
408 body = lustre_swab_repbuf(req, 1, sizeof (*body),
409 lustre_swab_mds_body);
411 CERROR ("Can't swab mds_body\n");
415 if ((body->valid & OBD_MD_FLEASIZE) != 0) {
416 /* The eadata is opaque; just check that it is there.
417 * Eventually, obd_unpackmd() will check the contents */
418 eadata = lustre_swab_repbuf(req, 2, body->eadatasize,
420 if (eadata == NULL) {
421 CERROR ("Missing/short eadata\n");
424 /* We save the reply LOV EA in case we have to replay
425 * a create for recovery. If we didn't allocate a
426 * large enough request buffer above we need to
427 * reallocate it here to hold the actual LOV EA. */
428 if (it->it_op & IT_OPEN) {
429 if (req->rq_reqmsg->buflens[4] <
431 mdc_realloc_openmsg(req, body, size);
433 lmm = lustre_msg_buf(req->rq_reqmsg, 4,
436 memcpy(lmm, eadata, body->eadatasize);
443 EXPORT_SYMBOL(mdc_enqueue);
446 * This long block is all about fixing up the lock and request state
447 * so that it is correct as of the moment _before_ the operation was
448 * applied; that way, the VFS will think that everything is normal and
449 * call Lustre's regular VFS methods.
451 * If we're performing a creation, that means that unless the creation
452 * failed with EEXIST, we should fake up a negative dentry.
454 * For everything else, we want to lookup to succeed.
456 * One additional note: if CREATE or OPEN succeeded, we add an extra
457 * reference to the request because we need to keep it around until
458 * ll_create/ll_open gets called.
460 * The server will return to us, in it_disposition, an indication of
461 * exactly what d.lustre.it_status refers to.
463 * If DISP_OPEN_OPEN is set, then d.lustre.it_status refers to the open() call,
464 * otherwise if DISP_OPEN_CREATE is set, then it status is the
465 * creation failure mode. In either case, one of DISP_LOOKUP_NEG or
466 * DISP_LOOKUP_POS will be set, indicating whether the child lookup
469 * Else, if DISP_LOOKUP_EXECD then d.lustre.it_status is the rc of the
472 int mdc_intent_lock(struct obd_export *exp, struct mdc_op_data *op_data,
473 void *lmm, int lmmsize, struct lookup_intent *it,
474 int lookup_flags, struct ptlrpc_request **reqp,
475 ldlm_blocking_callback cb_blocking, int extra_lock_flags)
477 struct lustre_handle lockh;
478 struct ptlrpc_request *request;
480 struct mds_body *mds_body;
481 struct lustre_handle old_lock;
482 struct ldlm_lock *lock;
486 CDEBUG(D_DLMTRACE,"name: %.*s in inode "LPU64", intent: %s flags %#o\n",
487 op_data->namelen, op_data->name, op_data->fid1.id,
488 ldlm_it2str(it->it_op), it->it_flags);
490 if (op_data->fid2.id &&
491 (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)) {
492 /* We could just return 1 immediately, but since we should only
493 * be called in revalidate_it if we already have a lock, let's
495 struct ldlm_res_id res_id = {.name ={op_data->fid2.id,
496 op_data->fid2.generation}};
497 struct lustre_handle lockh;
500 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
501 LDLM_FL_BLOCK_GRANTED, &res_id,
502 LDLM_PLAIN, NULL, LCK_PR, &lockh);
505 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
506 LDLM_FL_BLOCK_GRANTED, &res_id,
507 LDLM_PLAIN, NULL, LCK_PW, &lockh);
510 memcpy(&it->d.lustre.it_lock_handle, &lockh,
512 it->d.lustre.it_lock_mode = mode;
517 /* lookup_it may be called only after revalidate_it has run, because
518 * revalidate_it cannot return errors, only zero. Returning zero causes
519 * this call to lookup, which *can* return an error.
521 * We only want to execute the request associated with the intent one
522 * time, however, so don't send the request again. Instead, skip past
523 * this and use the request from revalidate. In this case, revalidate
524 * never dropped its reference, so the refcounts are all OK */
525 if (!it_disposition(it, DISP_ENQ_COMPLETE)) {
527 rc = mdc_enqueue(exp, LDLM_PLAIN, it, it_to_lock_mode(it),
528 op_data, &lockh, lmm, lmmsize,
529 ldlm_completion_ast, cb_blocking, NULL,
533 memcpy(&it->d.lustre.it_lock_handle, &lockh, sizeof(lockh));
535 request = *reqp = it->d.lustre.it_data;
536 LASSERT(request != NULL);
537 LASSERT(request != LP_POISON);
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
541 * this 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
546 if (it->it_op & IT_OPEN && request->rq_replay &&
547 (!it_disposition(it, DISP_OPEN_OPEN) ||it->d.lustre.it_status != 0))
548 mdc_clear_replay_flag(request, it->d.lustre.it_status);
550 if (!it_disposition(it, DISP_IT_EXECD)) {
551 /* The server failed before it even started executing the
552 * intent, i.e. because it couldn't unpack the request. */
553 LASSERT(it->d.lustre.it_status != 0);
554 RETURN(it->d.lustre.it_status);
556 rc = it_open_error(DISP_IT_EXECD, it);
560 mds_body = lustre_msg_buf(request->rq_repmsg, 1, sizeof(*mds_body));
561 LASSERT(mds_body != NULL); /* mdc_enqueue checked */
562 LASSERT_REPSWABBED(request, 1); /* mdc_enqueue swabbed */
564 /* If we were revalidating a fid/name pair, mark the intent in
565 * case we fail and get called again from lookup */
566 if (op_data->fid2.id) {
567 it_set_disposition(it, DISP_ENQ_COMPLETE);
568 /* Also: did we find the same inode? */
569 if (memcmp(&op_data->fid2, &mds_body->fid1, sizeof(op_data->fid2)))
573 rc = it_open_error(DISP_LOOKUP_EXECD, it);
577 /* keep requests around for the multiple phases of the call
578 * this shows the DISP_XX must guarantee we make it into the call
580 if (it_disposition(it, DISP_OPEN_CREATE) &&
581 !it_open_error(DISP_OPEN_CREATE, it))
582 ptlrpc_request_addref(request); /* balanced in ll_create_node */
583 if (it_disposition(it, DISP_OPEN_OPEN) &&
584 !it_open_error(DISP_OPEN_OPEN, it))
585 ptlrpc_request_addref(request); /* balanced in ll_file_open */
587 if (it->it_op & IT_CREAT) {
588 /* XXX this belongs in ll_create_it */
589 } else if (it->it_op == IT_OPEN) {
590 LASSERT(!it_disposition(it, DISP_OPEN_CREATE));
592 LASSERT(it->it_op & (IT_GETATTR | IT_LOOKUP));
595 /* If we already have a matching lock, then cancel the new
596 * one. We have to set the data here instead of in
597 * mdc_enqueue, because we need to use the child's inode as
598 * the l_ast_data to match, and that's not available until
599 * intent_finish has performed the iget().) */
600 lock = ldlm_handle2lock(&lockh);
602 LDLM_DEBUG(lock, "matching against this");
604 memcpy(&old_lock, &lockh, sizeof(lockh));
605 if (ldlm_lock_match(NULL, LDLM_FL_BLOCK_GRANTED, NULL,
606 LDLM_PLAIN, NULL, LCK_NL, &old_lock)) {
607 ldlm_lock_decref_and_cancel(&lockh,
608 it->d.lustre.it_lock_mode);
609 memcpy(&lockh, &old_lock, sizeof(old_lock));
610 memcpy(&it->d.lustre.it_lock_handle, &lockh,
614 CDEBUG(D_DENTRY,"D_IT dentry %.*s intent: %s status %d disp %x rc %d\n",
615 op_data->namelen, op_data->name, ldlm_it2str(it->it_op),
616 it->d.lustre.it_status, it->d.lustre.it_disposition, rc);
620 EXPORT_SYMBOL(mdc_intent_lock);