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