Whamcloud - gitweb
b0856bb7303d0434d16c0f2b136c653880b9de48
[fs/lustre-release.git] / lustre / mdc / mdc_locks.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.sf.net/projects/lustre/
7  *
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.
11  *
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.
16  *
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.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_MDC
26
27 #ifdef __KERNEL__
28 # include <linux/module.h>
29 # include <linux/pagemap.h>
30 # include <linux/miscdevice.h>
31 # include <linux/init.h>
32 #else
33 # include <liblustre.h>
34 #endif
35
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"
44
45 int it_disposition(struct lookup_intent *it, int flag)
46 {
47         return LUSTRE_IT(it)->it_disposition & flag;
48 }
49 EXPORT_SYMBOL(it_disposition);
50
51 void it_set_disposition(struct lookup_intent *it, int flag)
52 {
53         LUSTRE_IT(it)->it_disposition |= flag;
54 }
55 EXPORT_SYMBOL(it_set_disposition);
56
57 static void mdc_id2mdc_data(struct mdc_op_data *data,
58                             struct lustre_id *f1, 
59                             struct lustre_id *f2,
60                             const char *name, 
61                             int namelen, int mode)
62 {
63         LASSERT(data);
64         LASSERT(f1);
65
66         data->id1 = *f1;
67         if (f2)
68                 data->id2 = *f2;
69
70         data->valid = 0;
71         data->name = name;
72         data->namelen = namelen;
73         data->create_mode = mode;
74         data->mod_time = LTIME_S(CURRENT_TIME);
75 }
76
77 static int it_to_lock_mode(struct lookup_intent *it)
78 {
79         /* CREAT needs to be tested before open (both could be set) */
80         if (it->it_op & IT_CREAT)
81                 return LCK_PW;
82         else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_LOOKUP |
83                               IT_CHDIR))
84                 return LCK_PR;
85
86         LBUG();
87         RETURN(-EINVAL);
88 }
89
90 int it_open_error(int phase, struct lookup_intent *it)
91 {
92         if (it_disposition(it, DISP_OPEN_OPEN)) {
93                 if (phase == DISP_OPEN_OPEN)
94                         return LUSTRE_IT(it)->it_status;
95                 else
96                         return 0;
97         }
98
99         if (it_disposition(it, DISP_OPEN_CREATE)) {
100                 if (phase == DISP_OPEN_CREATE)
101                         return LUSTRE_IT(it)->it_status;
102                 else
103                         return 0;
104         }
105
106         if (it_disposition(it, DISP_LOOKUP_EXECD)) {
107                 if (phase == DISP_LOOKUP_EXECD)
108                         return LUSTRE_IT(it)->it_status;
109                 else
110                         return 0;
111         }
112
113         if (it_disposition(it, DISP_IT_EXECD)) {
114                 if (phase == DISP_IT_EXECD)
115                         return LUSTRE_IT(it)->it_status;
116                 else
117                         return 0;
118         }
119         CERROR("it disp: %X, status: %d\n", LUSTRE_IT(it)->it_disposition,
120                LUSTRE_IT(it)->it_status);
121         LBUG();
122         return 0;
123 }
124 EXPORT_SYMBOL(it_open_error);
125
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)
128 {
129         struct ldlm_lock *lock;
130         struct lustre_handle *lockh = (struct lustre_handle *)l;
131         ENTRY;
132
133         if (!*l) {
134                 EXIT;
135                 return 0;
136         }
137
138         lock = ldlm_handle2lock(lockh);
139
140         LASSERT(lock != NULL);
141         lock_res_and_lock(lock);
142 #ifdef __KERNEL__
143         if (lock->l_ast_data && lock->l_ast_data != data) {
144                 struct ll_inode_info *lli = ll_i2info(data);
145                 struct inode *new_inode = data;
146                 LASSERT(lli->lli_inode_magic == LLI_INODE_MAGIC);
147                 struct inode *old_inode = lock->l_ast_data;
148                 if (!(old_inode->i_state & I_FREEING)) {
149                         CERROR("Found existing inode %p/%lu/%u state %lu "
150                                "in lock: setting data to %p/%lu/%u\n",
151                                old_inode, old_inode->i_ino,
152                                old_inode->i_generation, old_inode->i_state,
153                                new_inode, new_inode->i_ino,
154                                new_inode->i_generation);
155                         unlock_res_and_lock(lock);
156                         LBUG();
157                 }
158         }
159 #endif
160         lock->l_ast_data = data;
161         unlock_res_and_lock(lock);
162         LDLM_LOCK_PUT(lock);
163
164         EXIT;
165         return 0;
166 }
167 EXPORT_SYMBOL(mdc_set_lock_data);
168
169 int mdc_change_cbdata(struct obd_export *exp, struct lustre_id *id, 
170                       ldlm_iterator_t it, void *data)
171 {
172         struct ldlm_res_id res_id = { .name = {0} };
173         ENTRY;
174
175         res_id.name[0] = id_fid(id);
176         res_id.name[1] = id_group(id);
177
178         ldlm_change_cbdata(class_exp2obd(exp)->obd_namespace,
179                            &res_id, it, data);
180
181         EXIT;
182         return 0;
183 }
184
185 static inline void
186 mdc_clear_replay_flag(struct ptlrpc_request *req, int rc)
187 {
188         /* Don't hold error requests for replay. */
189         if (req->rq_replay) {
190                 unsigned long irqflags;
191                 spin_lock_irqsave(&req->rq_lock, irqflags);
192                 req->rq_replay = 0;
193                 spin_unlock_irqrestore(&req->rq_lock, irqflags);
194         }
195         if (rc && req->rq_transno != 0) {
196                 DEBUG_REQ(D_ERROR, req, "transno returned on error rc %d", rc);
197                 LBUG();
198         }
199 }
200
201 /* We always reserve enough space in the reply packet for a stripe MD, because
202  * we don't know in advance the file type. */
203 int mdc_enqueue(struct obd_export *exp,
204                 int lock_type,
205                 struct lookup_intent *it,
206                 int lock_mode,
207                 struct mdc_op_data *data,
208                 struct lustre_handle *lockh,
209                 void *lmm,
210                 int lmmsize,
211                 ldlm_completion_callback cb_completion,
212                 ldlm_blocking_callback cb_blocking,
213                 void *cb_data)
214 {
215         struct ptlrpc_request *req;
216         struct ldlm_res_id res_id = {
217                 .name = {id_fid(&data->id1), id_group(&data->id1)}
218         };
219         struct obd_device *obddev = class_exp2obd(exp);
220         ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_LOOKUP } };
221         struct ldlm_intent *lit;
222         struct ldlm_request *lockreq;
223         int reqsize[6] = {[MDS_REQ_SECDESC_OFF] = 0,
224                           [MDS_REQ_INTENT_LOCKREQ_OFF] = sizeof(*lockreq),
225                           [MDS_REQ_INTENT_IT_OFF] = sizeof(*lit)};
226         int repsize[5] = {sizeof(struct ldlm_reply),
227                           sizeof(struct mds_body),
228                           obddev->u.cli.cl_max_mds_easize};
229         int req_buffers = 3, reply_buffers = 0;
230         int rc, flags = LDLM_FL_HAS_INTENT;
231         struct ldlm_reply *dlm_rep = NULL;
232         void *eadata;
233         unsigned long irqflags;
234         ENTRY;
235
236 //        LDLM_DEBUG_NOLOCK("mdsintent=%s,name=%s,dir=%lu",
237 //                          ldlm_it2str(it->it_op), it_name, it_inode->i_ino);
238
239         reqsize[0] = lustre_secdesc_size();
240
241         if (it->it_op & IT_OPEN) {
242                 it->it_create_mode |= S_IFREG;
243                 it->it_create_mode &= ~current->fs->umask;
244
245                 reqsize[req_buffers++] = sizeof(struct mds_rec_create);
246                 reqsize[req_buffers++] = data->namelen + 1;
247                 reqsize[req_buffers++] = obddev->u.cli.cl_max_mds_easize;
248
249                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
250                                       LDLM_ENQUEUE, req_buffers, reqsize, NULL);
251                 if (!req)
252                         RETURN(-ENOMEM);
253
254                 spin_lock_irqsave (&req->rq_lock, irqflags);
255                 req->rq_replay = 1;
256                 spin_unlock_irqrestore (&req->rq_lock, irqflags);
257
258                 /* pack the intent */
259                 lit = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_INTENT_IT_OFF,
260                                      sizeof (*lit));
261                 lit->opc = (__u64)it->it_op;
262
263                 /* pack the intended request */
264                 mdc_open_pack(req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF, data,
265                               it->it_create_mode, 0, it->it_flags, lmm, lmmsize);
266                 /* get ready for the reply */
267                 repsize[3] = 4;
268                 repsize[4] = xattr_acl_size(LL_ACL_MAX_ENTRIES);
269                 reply_buffers = 5;
270                 req->rq_replen = lustre_msg_size(5, repsize);
271         } else if (it->it_op & (IT_GETATTR | IT_LOOKUP | IT_CHDIR)) {
272                 __u64 valid = data->valid | OBD_MD_FLNOTOBD | OBD_MD_FLEASIZE |
273                               OBD_MD_FLACL;
274
275                 /* we don't expect xattr retrieve could reach here */
276                 LASSERT(!(valid & (OBD_MD_FLXATTR | OBD_MD_FLXATTRLIST)));
277
278                 reqsize[req_buffers++] = sizeof(struct mds_body);
279                 reqsize[req_buffers++] = data->namelen + 1;
280
281                 if (it->it_op & IT_GETATTR)
282                         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
283
284                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
285                                       LDLM_ENQUEUE, req_buffers, reqsize, NULL);
286
287                 if (!req)
288                         RETURN(-ENOMEM);
289
290                 /* pack the intent */
291                 lit = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_INTENT_IT_OFF,
292                                      sizeof (*lit));
293                 lit->opc = (__u64)it->it_op;
294
295                 /* pack the intended request */
296                 mdc_getattr_pack(req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF,
297                                  valid, it->it_flags, data);
298                 
299                 /* get ready for the reply */
300                 repsize[3] = 4;
301                 repsize[4] = xattr_acl_size(LL_ACL_MAX_ENTRIES);
302                 reply_buffers = 5;
303                 req->rq_replen = lustre_msg_size(5, repsize);
304         } else if (it->it_op == IT_READDIR) {
305                 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
306                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
307                                       LDLM_ENQUEUE, 2, reqsize, NULL);
308                 
309                 if (!req)
310                         RETURN(-ENOMEM);
311                 /* get ready for the reply */
312                 reply_buffers = 1;
313                 req->rq_replen = lustre_msg_size(1, repsize);
314         } else if (it->it_op == IT_UNLINK) {
315                 reqsize[req_buffers++] = sizeof(struct mds_body);
316                 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
317                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
318                                       LDLM_ENQUEUE, req_buffers, reqsize, NULL);
319                 if (!req)
320                         RETURN(-ENOMEM);
321
322                 /* pack the intended request */
323                 mdc_getattr_pack(req->rq_reqmsg, MDS_REQ_INTENT_REC_OFF,
324                                  0, 0, data);
325
326                 /* pack the intent */
327                 lit = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_INTENT_IT_OFF,
328                                      sizeof (*lit));
329                 lit->opc = (__u64)it->it_op;
330
331                 /* get ready for the reply */
332                 reply_buffers = 3;
333                 req->rq_replen = lustre_msg_size(3, repsize);
334         } else {
335                 LBUG();
336                 RETURN(-EINVAL);
337         }
338
339         lustre_pack_secdesc(req, reqsize[0]);
340
341         mdc_get_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
342         rc = ldlm_cli_enqueue(exp, req, obddev->obd_namespace, res_id,
343                               lock_type, &policy, lock_mode, &flags,cb_blocking,
344                               cb_completion, NULL, cb_data, NULL, 0, NULL,
345                               lockh);
346         mdc_put_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
347
348         /* Similarly, if we're going to replay this request, we don't want to
349          * actually get a lock, just perform the intent. */
350         if (req->rq_transno || req->rq_replay) {
351                 lockreq = lustre_msg_buf(req->rq_reqmsg,
352                                          MDS_REQ_INTENT_LOCKREQ_OFF,
353                                          sizeof (*lockreq));
354                 lockreq->lock_flags |= LDLM_FL_INTENT_ONLY;
355         }
356
357         /* This can go when we're sure that this can never happen */
358         LASSERT(rc != -ENOENT);
359         /* We need dlm_rep to be assigned this early, to check lock mode of
360            returned lock from request to avoid possible race with lock
361            conversion */
362         if (rc == ELDLM_LOCK_ABORTED || !rc) {
363                 dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
364                 LASSERT(dlm_rep != NULL);   /* checked by ldlm_cli_enqueue() */
365         }
366         if (rc == ELDLM_LOCK_ABORTED) {
367                 lock_mode = 0;
368                 memset(lockh, 0, sizeof(*lockh));
369                 rc = 0;
370         } else if (rc != 0) {
371                 CERROR("ldlm_cli_enqueue: %d\n", rc);
372                 LASSERTF(rc < 0, "rc = %d\n", rc);
373                 mdc_clear_replay_flag(req, rc);
374                 ptlrpc_req_finished(req);
375                 RETURN(rc);
376         } else { /* rc = 0 */
377                 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
378                 LASSERT(lock);
379
380                 /* If the server gave us back a different lock mode, we should
381                  * fix up our variables. */
382                 if (dlm_rep->lock_desc.l_req_mode != lock_mode) {
383                         ldlm_lock_addref(lockh, dlm_rep->lock_desc.l_req_mode);
384                         ldlm_lock_decref(lockh, lock_mode);
385                         lock_mode = dlm_rep->lock_desc.l_req_mode;
386                 }
387
388                 ldlm_lock_allow_match(lock);
389                 LDLM_LOCK_PUT(lock);
390         }
391
392         LASSERT_REPSWABBED(req, 0);         /* swabbed by ldlm_cli_enqueue() */
393
394         LUSTRE_IT(it)->it_disposition = (int) dlm_rep->lock_policy_res1;
395         LUSTRE_IT(it)->it_status = (int) dlm_rep->lock_policy_res2;
396         LUSTRE_IT(it)->it_lock_mode = lock_mode;
397         LUSTRE_IT(it)->it_data = req;
398
399         if (LUSTRE_IT(it)->it_status < 0 && req->rq_replay)
400                 mdc_clear_replay_flag(req, LUSTRE_IT(it)->it_status);
401
402         DEBUG_REQ(D_DLMTRACE, req, "disposition: %x, status: %d",
403                   LUSTRE_IT(it)->it_disposition, LUSTRE_IT(it)->it_status);
404
405         /* We know what to expect, so we do any byte flipping required here */
406         LASSERT(reply_buffers == 5 || reply_buffers == 4 || 
407                 reply_buffers == 3 || reply_buffers == 1);
408         if (reply_buffers >= 3) {
409                 struct mds_body *body;
410
411                 body = lustre_swab_repbuf(req, 1, sizeof (*body),
412                                           lustre_swab_mds_body);
413                 if (body == NULL) {
414                         CERROR ("Can't swab mds_body\n");
415                         RETURN (-EPROTO);
416                 }
417
418                 if ((body->valid & OBD_MD_FLEASIZE) != 0) {
419                         /* The eadata is opaque; just check that it is there.
420                          * Eventually, obd_unpackmd() will check the contents */
421                         eadata = lustre_swab_repbuf(req, 2, body->eadatasize,
422                                                     NULL);
423                         if (eadata == NULL) {
424                                 CERROR ("Missing/short eadata\n");
425                                 RETURN (-EPROTO);
426                         }
427                         if (it->it_op & IT_OPEN) {
428                                 void *replayea;
429
430                                 replayea = lustre_msg_buf(req->rq_reqmsg,
431                                                           MDS_REQ_INTENT_REC_OFF + 2,
432                                                           body->eadatasize);
433                                 LASSERT(replayea);
434                                 memcpy(replayea, eadata, body->eadatasize);
435
436                                 LASSERT(req->rq_reqmsg->bufcount == 6);
437                                 req->rq_reqmsg->buflens[5] = body->eadatasize;
438                                 /* If this isn't the last buffer, we might
439                                  * have to shift other data around. */
440                         }
441                 }
442         }
443
444         RETURN(rc);
445 }
446 EXPORT_SYMBOL(mdc_enqueue);
447
448 /* 
449  * This long block is all about fixing up the lock and request state
450  * so that it is correct as of the moment _before_ the operation was
451  * applied; that way, the VFS will think that everything is normal and
452  * call Lustre's regular VFS methods.
453  *
454  * If we're performing a creation, that means that unless the creation
455  * failed with EEXIST, we should fake up a negative dentry.
456  *
457  * For everything else, we want to lookup to succeed.
458  *
459  * One additional note: if CREATE or OPEN succeeded, we add an extra
460  * reference to the request because we need to keep it around until
461  * ll_create/ll_open gets called.
462  *
463  * The server will return to us, in it_disposition, an indication of
464  * exactly what d.lustre->it_status refers to.
465  *
466  * If DISP_OPEN_OPEN is set, then d.lustre->it_status refers to the open() call,
467  * otherwise if DISP_OPEN_CREATE is set, then it status is the
468  * creation failure mode.  In either case, one of DISP_LOOKUP_NEG or
469  * DISP_LOOKUP_POS will be set, indicating whether the child lookup
470  * was successful.
471  *
472  * Else, if DISP_LOOKUP_EXECD then d.lustre->it_status is the rc of the
473  * child lookup.
474  */
475 int mdc_intent_lock(struct obd_export *exp, struct lustre_id *pid, 
476                     const char *name, int len, void *lmm, int lmmsize, 
477                     struct lustre_id *cid, struct lookup_intent *it, 
478                     int lookup_flags, struct ptlrpc_request **reqp,
479                     ldlm_blocking_callback cb_blocking)
480 {
481         struct lustre_handle lockh;
482         struct ptlrpc_request *request;
483         struct mds_body *mds_body;
484         struct lustre_handle old_lock;
485         struct ldlm_lock *lock;
486         int rc = 0;
487         ENTRY;
488         LASSERT(it);
489
490         CDEBUG(D_DLMTRACE, "name: %*s in obj "DLID4", intent: %s flags %#o\n",
491                len, name, OLID4(pid), ldlm_it2str(it->it_op), it->it_flags);
492
493         if (cid && (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR ||
494                     it->it_op == IT_CHDIR)) {
495                 /* We could just return 1 immediately, but since we should only
496                  * be called in revalidate_it if we already have a lock, let's
497                  * verify that. */
498                 struct ldlm_res_id res_id = {.name = {id_fid(cid),
499                                                       id_group(cid)}};
500                 struct lustre_handle lockh;
501                 ldlm_policy_data_t policy;
502                 int mode;
503
504                 /* For the GETATTR case, ll_revalidate_it issues two separate
505                    queries - for LOOKUP and for UPDATE lock because it cannot
506                    check them together - we might have those two bits to be
507                    present in two separate granted locks */
508                 policy.l_inodebits.bits = (it->it_op == IT_GETATTR) ?
509                         MDS_INODELOCK_UPDATE : MDS_INODELOCK_LOOKUP;
510                 
511                 mode = LCK_PR;
512                 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
513                                      LDLM_FL_BLOCK_GRANTED, &res_id,
514                                      LDLM_IBITS, &policy, mode,
515                                      &lockh);
516
517                 if (!rc) {
518                         mode = LCK_CR;
519                         rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
520                                              LDLM_FL_BLOCK_GRANTED, &res_id,
521                                              LDLM_IBITS, &policy, mode,
522                                              &lockh);
523                 }
524                 if (!rc) {
525                         mode = LCK_PW;
526                         rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
527                                              LDLM_FL_BLOCK_GRANTED, &res_id,
528                                              LDLM_IBITS, &policy, mode,
529                                              &lockh);
530                 }
531                 if (!rc) {
532                         mode = LCK_CW;
533                         rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
534                                              LDLM_FL_BLOCK_GRANTED, &res_id,
535                                              LDLM_IBITS, &policy, mode,
536                                              &lockh);
537                 }
538                 if (rc) {
539                         if (ptlrpcs_check_cred(exp->exp_obd->u.cli.cl_import)) {
540                                 /* return immediately if no credential held */
541                                 ldlm_lock_decref(&lockh, mode);
542                                 RETURN(-EACCES);
543                         }
544                         memcpy(&LUSTRE_IT(it)->it_lock_handle, &lockh,
545                                sizeof(lockh));
546                         LUSTRE_IT(it)->it_lock_mode = mode;
547                 }
548
549                 /* Only return failure if it was not GETATTR by cid (from
550                    inode_revalidate) */
551                 if (rc || name)
552                         RETURN(rc);
553         }
554
555         /* lookup_it may be called only after revalidate_it has run, because
556          * revalidate_it cannot return errors, only zero.  Returning zero causes
557          * this call to lookup, which *can* return an error.
558          *
559          * We only want to execute the request associated with the intent one
560          * time, however, so don't send the request again.  Instead, skip past
561          * this and use the request from revalidate.  In this case, revalidate
562          * never dropped its reference, so the refcounts are all OK */
563         if (!it_disposition(it, DISP_ENQ_COMPLETE)) {
564                 struct mdc_op_data *op_data;
565
566                 OBD_ALLOC(op_data, sizeof(*op_data));
567                 if (op_data == NULL)
568                         RETURN(-ENOMEM);
569
570                 mdc_id2mdc_data(op_data, pid, cid, name, len, 0);
571
572                 if (name != NULL)
573                         op_data->valid |= OBD_MD_FID;
574
575                 rc = mdc_enqueue(exp, LDLM_IBITS, it, it_to_lock_mode(it),
576                                  op_data, &lockh, lmm, lmmsize,
577                                  ldlm_completion_ast, cb_blocking, NULL);
578                 OBD_FREE(op_data, sizeof(*op_data));
579                 if (rc < 0)
580                         RETURN(rc);
581                 
582                 memcpy(&LUSTRE_IT(it)->it_lock_handle, &lockh, sizeof(lockh));
583         }
584         request = *reqp = LUSTRE_IT(it)->it_data;
585         LASSERT(request != NULL);
586         
587         /* If we're doing an IT_OPEN which did not result in an actual
588          * successful open, then we need to remove the bit which saves this
589          * request for unconditional replay.
590          *
591          * It's important that we do this first!  Otherwise we might exit the
592          * function without doing so, and try to replay a failed create (bug
593          * 3440) */
594         if (it->it_op & IT_OPEN && request->rq_replay &&
595             (!it_disposition(it, DISP_OPEN_OPEN) || LUSTRE_IT(it)->it_status != 0))
596                 mdc_clear_replay_flag(request, LUSTRE_IT(it)->it_status);
597  
598         if (!it_disposition(it, DISP_IT_EXECD)) {
599                 /* The server failed before it even started executing the
600                  * intent, i.e. because it couldn't unpack the request. */
601                 LASSERT(LUSTRE_IT(it)->it_status != 0);
602                 RETURN(LUSTRE_IT(it)->it_status);
603         }
604         rc = it_open_error(DISP_IT_EXECD, it);
605         if (rc)
606                 RETURN(rc);
607
608         mds_body = lustre_msg_buf(request->rq_repmsg, 1, sizeof(*mds_body));
609         LASSERT(mds_body != NULL);      /* mdc_enqueue checked */
610         LASSERT_REPSWABBED(request, 1); /* mdc_enqueue swabbed */
611
612         /* If we were revalidating a fid/name pair, mark the intent in case we
613          * fail and get called again from lookup */
614         if (cid != NULL) {
615                 it_set_disposition(it, DISP_ENQ_COMPLETE);
616                 /* Also: did we find the same inode? */
617                 
618                 /* we have to compare all the fields but type, because MDS can
619                  * return fid/mds/ino/gen if inode lives on another MDS -bzzz */
620                 if (!(lookup_flags & LOOKUP_COBD) && !id_equal(cid, &mds_body->id1))
621                         RETURN(-ESTALE);
622         }
623
624         rc = it_open_error(DISP_LOOKUP_EXECD, it);
625         if (rc)
626                 RETURN(rc);
627
628         /*
629          * keep requests around for the multiple phases of the call this shows
630          * the DISP_XX must guarantee we make it into the call.
631          */
632         if (it_disposition(it, DISP_OPEN_CREATE) &&
633             !it_open_error(DISP_OPEN_CREATE, it))
634                 ptlrpc_request_addref(request); /* balanced in ll_create_node */
635         if (it_disposition(it, DISP_OPEN_OPEN) &&
636             !it_open_error(DISP_OPEN_OPEN, it))
637                 ptlrpc_request_addref(request); /* balanced in ll_file_open */
638
639         if (it->it_op & IT_CREAT) {
640                 /* XXX this belongs in ll_create_it */
641         } else if (it->it_op == IT_OPEN) {
642                 LASSERT(!it_disposition(it, DISP_OPEN_CREATE));
643         } else {
644                 LASSERT(it->it_op & (IT_GETATTR | IT_LOOKUP | IT_CHDIR));
645         }
646
647         /*
648          * if we already have a matching lock, then cancel the new one. We have
649          * to set the data here instead of in mdc_enqueue, because we need to
650          * use the child's inode as the l_ast_data to match, and that's not
651          * available until intent_finish has performed the iget().)
652          */
653         lock = ldlm_handle2lock(&lockh);
654         if (lock) {
655                 ldlm_policy_data_t policy = lock->l_policy_data;
656                 LDLM_DEBUG(lock, "matching against this");
657                 LDLM_LOCK_PUT(lock);
658                 
659                 LASSERTF(id_fid(&mds_body->id1) == lock->l_resource->lr_name.name[0] &&
660                          id_group(&mds_body->id1) == lock->l_resource->lr_name.name[1],
661                          "Invalid lock is returned to client. Lock res_is: %lu/%lu, "
662                          "response res_id: %lu/%lu.\n",
663                          (unsigned long)lock->l_resource->lr_name.name[0],
664                          (unsigned long)lock->l_resource->lr_name.name[1],
665                          (unsigned long)id_fid(&mds_body->id1),
666                          (unsigned long)id_group(&mds_body->id1));
667                 
668                 memcpy(&old_lock, &lockh, sizeof(lockh));
669                 if (ldlm_lock_match(NULL, LDLM_FL_BLOCK_GRANTED, NULL,
670                                     LDLM_IBITS, &policy, LCK_NL, &old_lock)) {
671                         ldlm_lock_decref_and_cancel(&lockh,
672                                                     LUSTRE_IT(it)->it_lock_mode);
673                         memcpy(&lockh, &old_lock, sizeof(old_lock));
674                         memcpy(&LUSTRE_IT(it)->it_lock_handle, &lockh,
675                                sizeof(lockh));
676                 }
677         }
678         CDEBUG(D_DENTRY, "D_IT dentry %*s intent: %s status %d disp %x rc %d\n",
679                len, name, ldlm_it2str(it->it_op), LUSTRE_IT(it)->it_status,
680                LUSTRE_IT(it)->it_disposition, rc);
681
682         RETURN(rc);
683 }
684 EXPORT_SYMBOL(mdc_intent_lock);