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