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