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