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