Whamcloud - gitweb
Branch b1_4_mountconf
[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 the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_MDC
29
30 #ifdef __KERNEL__
31 # include <linux/module.h>
32 # include <linux/pagemap.h>
33 # include <linux/miscdevice.h>
34 # include <linux/init.h>
35 #else
36 # include <liblustre.h>
37 #endif
38
39 #include <linux/obd_class.h>
40 #include <linux/lustre_mds.h>
41 #include <linux/lustre_dlm.h>
42 #include <linux/lprocfs_status.h>
43 #include "mdc_internal.h"
44
45 int it_disposition(struct lookup_intent *it, int flag)
46 {
47         return it->d.lustre.it_disposition & flag;
48 }
49 EXPORT_SYMBOL(it_disposition);
50
51 void it_set_disposition(struct lookup_intent *it, int flag)
52 {
53         it->d.lustre.it_disposition |= flag;
54 }
55 EXPORT_SYMBOL(it_set_disposition);
56
57 static int it_to_lock_mode(struct lookup_intent *it)
58 {
59         /* CREAT needs to be tested before open (both could be set) */
60         if (it->it_op & IT_CREAT)
61                 return LCK_PW;
62         else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_LOOKUP))
63                 return LCK_PR;
64
65         LBUG();
66         RETURN(-EINVAL);
67 }
68
69 int it_open_error(int phase, struct lookup_intent *it)
70 {
71         if (it_disposition(it, DISP_OPEN_OPEN)) {
72                 if (phase >= DISP_OPEN_OPEN)
73                         return it->d.lustre.it_status;
74                 else
75                         return 0;
76         }
77
78         if (it_disposition(it, DISP_OPEN_CREATE)) {
79                 if (phase >= DISP_OPEN_CREATE)
80                         return it->d.lustre.it_status;
81                 else
82                         return 0;
83         }
84
85         if (it_disposition(it, DISP_LOOKUP_EXECD)) {
86                 if (phase >= DISP_LOOKUP_EXECD)
87                         return it->d.lustre.it_status;
88                 else
89                         return 0;
90         }
91
92         if (it_disposition(it, DISP_IT_EXECD)) {
93                 if (phase >= DISP_IT_EXECD)
94                         return it->d.lustre.it_status;
95                 else
96                         return 0;
97         }
98         CERROR("it disp: %X, status: %d\n", it->d.lustre.it_disposition,
99                it->d.lustre.it_status);
100         LBUG();
101         return 0;
102 }
103 EXPORT_SYMBOL(it_open_error);
104
105 /* this must be called on a lockh that is known to have a referenced lock */
106 void mdc_set_lock_data(__u64 *l, void *data)
107 {
108         struct ldlm_lock *lock;
109         struct lustre_handle *lockh = (struct lustre_handle *)l;
110         ENTRY;
111
112         if (!*l) {
113                 EXIT;
114                 return;
115         }
116
117         lock = ldlm_handle2lock(lockh);
118
119         LASSERT(lock != NULL);
120         l_lock(&lock->l_resource->lr_namespace->ns_lock);
121 #ifdef __KERNEL__
122         if (lock->l_ast_data && lock->l_ast_data != data) {
123                 struct inode *new_inode = data;
124                 struct inode *old_inode = lock->l_ast_data;
125                 LASSERTF(old_inode->i_state & I_FREEING,
126                          "Found existing inode %p/%lu/%u state %lu in lock: "
127                          "setting data to %p/%lu/%u\n", old_inode,
128                          old_inode->i_ino, old_inode->i_generation,
129                          old_inode->i_state,
130                          new_inode, new_inode->i_ino, new_inode->i_generation);
131         }
132 #endif
133         lock->l_ast_data = data;
134         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
135         LDLM_LOCK_PUT(lock);
136
137         EXIT;
138 }
139 EXPORT_SYMBOL(mdc_set_lock_data);
140
141 int mdc_change_cbdata(struct obd_export *exp, struct ll_fid *fid, 
142                       ldlm_iterator_t it, void *data)
143 {
144         struct ldlm_res_id res_id = { .name = {0} };
145         ENTRY;
146
147         res_id.name[0] = fid->id;
148         res_id.name[1] = fid->generation;
149
150         ldlm_change_cbdata(class_exp2obd(exp)->obd_namespace, &res_id, it,
151                            data);
152
153         EXIT;
154         return 0;
155 }
156
157 static inline void mdc_clear_replay_flag(struct ptlrpc_request *req, int rc)
158 {
159         /* Don't hold error requests for replay. */
160         if (req->rq_replay) {
161                 unsigned long irqflags;
162                 spin_lock_irqsave(&req->rq_lock, irqflags);
163                 req->rq_replay = 0;
164                 spin_unlock_irqrestore(&req->rq_lock, irqflags);
165         }
166         if (rc && req->rq_transno != 0) {
167                 DEBUG_REQ(D_ERROR, req, "transno returned on error rc %d", rc);
168                 LBUG();
169         }
170 }
171
172 static int round_up(int val)
173 {
174         int ret = 1;
175         while (val) {
176                 val >>= 1;
177                 ret <<= 1;
178         }
179         return ret;
180 }
181
182 /* Save a large LOV EA into the request buffer so that it is available
183  * for replay.  We don't do this in the initial request because the
184  * original request doesn't need this buffer (at most it sends just the
185  * lov_mds_md) and it is a waste of RAM/bandwidth to send the empty
186  * buffer and may also be difficult to allocate and save a very large
187  * request buffer for each open. (bug 5707)
188  *
189  * OOM here may cause recovery failure if lmm is needed (only for the
190  * original open if the MDS crashed just when this client also OOM'd)
191  * but this is incredibly unlikely, and questionable whether the client
192  * could do MDS recovery under OOM anyways... */
193 static void mdc_realloc_openmsg(struct ptlrpc_request *req,
194                                 struct mds_body *body, int size[5])
195 {
196         int new_size, old_size;
197         struct lustre_msg *new_msg;
198
199         /* save old size */
200         old_size = lustre_msg_size(5, size);
201
202         size[4] = body->eadatasize;
203         new_size = lustre_msg_size(5, size);
204         OBD_ALLOC(new_msg, new_size);
205         if (new_msg != NULL) {
206                 struct lustre_msg *old_msg = req->rq_reqmsg;
207                 unsigned long irqflags;
208
209                 DEBUG_REQ(D_INFO, req, "replace reqmsg for larger EA %u\n",
210                           body->eadatasize);
211                 memcpy(new_msg, old_msg, old_size);
212                 new_msg->buflens[4] = body->eadatasize;
213
214                 spin_lock_irqsave(&req->rq_lock, irqflags);
215                 req->rq_reqmsg = new_msg;
216                 req->rq_reqlen = new_size;
217                 spin_unlock_irqrestore(&req->rq_lock, irqflags);
218
219                 OBD_FREE(old_msg, old_size);
220         } else {
221                 body->valid &= ~OBD_MD_FLEASIZE;
222                 body->eadatasize = 0;
223         }
224 }
225
226 /* We always reserve enough space in the reply packet for a stripe MD, because
227  * we don't know in advance the file type. */
228 int mdc_enqueue(struct obd_export *exp,
229                 int lock_type,
230                 struct lookup_intent *it,
231                 int lock_mode,
232                 struct mdc_op_data *data,
233                 struct lustre_handle *lockh,
234                 void *lmm,
235                 int lmmsize,
236                 ldlm_completion_callback cb_completion,
237                 ldlm_blocking_callback cb_blocking,
238                 void *cb_data, int extra_lock_flags)
239 {
240         struct ptlrpc_request *req;
241         struct obd_device *obddev = class_exp2obd(exp);
242         struct ldlm_res_id res_id =
243                 { .name = {data->fid1.id, data->fid1.generation} };
244         int size[5] = {sizeof(struct ldlm_request), sizeof(struct ldlm_intent)};
245         int rc, flags = extra_lock_flags | LDLM_FL_HAS_INTENT;
246         int repsize[4] = {sizeof(struct ldlm_reply),
247                           sizeof(struct mds_body),
248                           obddev->u.cli.cl_max_mds_easize,
249                           obddev->u.cli.cl_max_mds_cookiesize};
250         struct ldlm_reply *dlm_rep;
251         struct ldlm_intent *lit;
252         struct ldlm_request *lockreq;
253         void *eadata;
254         unsigned long irqflags;
255         int   reply_buffers = 0;
256         ENTRY;
257
258 //        LDLM_DEBUG_NOLOCK("mdsintent=%s,name=%s,dir=%lu",
259 //                          ldlm_it2str(it->it_op), it_name, it_inode->i_ino);
260
261         if (it->it_op & IT_OPEN) {
262                 it->it_create_mode |= S_IFREG;
263
264                 size[2] = sizeof(struct mds_rec_create);
265                 size[3] = data->namelen + 1;
266                 /* As an optimization, we allocate an RPC request buffer for
267                  * at least a default-sized LOV EA even if we aren't sending
268                  * one.  We grow the whole request to the next power-of-two
269                  * size since we get that much from a slab allocation anyways.
270                  * This avoids an allocation below in the common case where
271                  * we need to save a default-sized LOV EA for open replay. */
272                 size[4] = max(lmmsize, obddev->u.cli.cl_default_mds_easize);
273                 rc = lustre_msg_size(5, size);
274                 if (rc & (rc - 1))
275                         size[4] = min(size[4] + round_up(rc) - rc,
276                                       obddev->u.cli.cl_max_mds_easize);
277                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE,
278                                       5, size, NULL);
279                 if (!req)
280                         RETURN(-ENOMEM);
281
282                 spin_lock_irqsave (&req->rq_lock, irqflags);
283                 req->rq_replay = 1;
284                 spin_unlock_irqrestore (&req->rq_lock, irqflags);
285
286                 /* pack the intent */
287                 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
288                 lit->opc = (__u64)it->it_op;
289
290                 /* pack the intended request */
291                 mdc_open_pack(req, 2, data, it->it_create_mode, 0,
292                               it->it_flags, lmm, lmmsize);
293                 /* get ready for the reply */
294                 reply_buffers = 3;
295                 req->rq_replen = lustre_msg_size(3, repsize);
296         } else if (it->it_op & IT_UNLINK) {
297                 size[2] = sizeof(struct mds_rec_unlink);
298                 size[3] = data->namelen + 1;
299                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 4,
300                                       size, NULL);
301                 if (!req)
302                         RETURN(-ENOMEM);
303
304                 /* pack the intent */
305                 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
306                 lit->opc = (__u64)it->it_op;
307
308                 /* pack the intended request */
309                 mdc_unlink_pack(req, 2, data);
310                 /* get ready for the reply */
311                 reply_buffers = 4;
312                 req->rq_replen = lustre_msg_size(4, repsize);
313         } else if (it->it_op & (IT_GETATTR | IT_LOOKUP)) {
314                 obd_valid valid = OBD_MD_FLGETATTR | OBD_MD_FLEASIZE;
315                 size[2] = sizeof(struct mds_body);
316                 size[3] = data->namelen + 1;
317
318                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 4,
319                                       size, NULL);
320                 if (!req)
321                         RETURN(-ENOMEM);
322
323                 /* pack the intent */
324                 lit = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*lit));
325                 lit->opc = (__u64)it->it_op;
326
327                 /* pack the intended request */
328                 mdc_getattr_pack(req, valid, 2, it->it_flags, data);
329                 /* get ready for the reply */
330                 reply_buffers = 3;
331                 req->rq_replen = lustre_msg_size(3, repsize);
332         } else if (it->it_op == IT_READDIR) {
333                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 1,
334                                       size, NULL);
335                 if (!req)
336                         RETURN(-ENOMEM);
337
338                 /* get ready for the reply */
339                 reply_buffers = 1;
340                 req->rq_replen = lustre_msg_size(1, repsize);
341         }  else {
342                 LBUG();
343                 RETURN(-EINVAL);
344         }
345
346         mdc_get_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
347         rc = ldlm_cli_enqueue(exp, req, obddev->obd_namespace, res_id,
348                               lock_type, NULL, lock_mode, &flags, cb_blocking,
349                               cb_completion, NULL, cb_data, NULL, 0, NULL,
350                               lockh);
351         mdc_put_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
352
353         /* Similarly, if we're going to replay this request, we don't want to
354          * actually get a lock, just perform the intent. */
355         if (req->rq_transno || req->rq_replay) {
356                 lockreq = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*lockreq));
357                 lockreq->lock_flags |= LDLM_FL_INTENT_ONLY;
358         }
359
360         /* This can go when we're sure that this can never happen */
361         LASSERT(rc != -ENOENT);
362         if (rc == ELDLM_LOCK_ABORTED) {
363                 lock_mode = 0;
364                 memset(lockh, 0, sizeof(*lockh));
365                 rc = 0;
366         } else if (rc != 0) {
367                 CERROR("ldlm_cli_enqueue: %d\n", rc);
368                 LASSERTF(rc < 0, "rc %d\n", rc);
369                 mdc_clear_replay_flag(req, rc);
370                 ptlrpc_req_finished(req);
371                 RETURN(rc);
372         } else { /* rc = 0 */
373                 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
374                 LASSERT(lock);
375
376                 /* If the server gave us back a different lock mode, we should
377                  * fix up our variables. */
378                 if (lock->l_req_mode != lock_mode) {
379                         ldlm_lock_addref(lockh, lock->l_req_mode);
380                         ldlm_lock_decref(lockh, lock_mode);
381                         lock_mode = lock->l_req_mode;
382                 }
383
384                 ldlm_lock_allow_match(lock);
385                 LDLM_LOCK_PUT(lock);
386         }
387
388         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
389         LASSERT(dlm_rep != NULL);           /* checked by ldlm_cli_enqueue() */
390         LASSERT_REPSWABBED(req, 0);         /* swabbed by ldlm_cli_enqueue() */
391
392         it->d.lustre.it_disposition = (int) dlm_rep->lock_policy_res1;
393         it->d.lustre.it_status = (int) dlm_rep->lock_policy_res2;
394         it->d.lustre.it_lock_mode = lock_mode;
395         it->d.lustre.it_data = req;
396
397         if (it->d.lustre.it_status < 0 && req->rq_replay)
398                 mdc_clear_replay_flag(req, it->d.lustre.it_status);
399
400         DEBUG_REQ(D_RPCTRACE, req, "op: %d disposition: %x, status: %d",
401                   it->it_op,it->d.lustre.it_disposition,it->d.lustre.it_status);
402
403         /* We know what to expect, so we do any byte flipping required here */
404         LASSERT(reply_buffers == 4 || reply_buffers == 3 || reply_buffers == 1);
405         if (reply_buffers >= 3) {
406                 struct mds_body *body;
407
408                 body = lustre_swab_repbuf(req, 1, sizeof (*body),
409                                           lustre_swab_mds_body);
410                 if (body == NULL) {
411                         CERROR ("Can't swab mds_body\n");
412                         RETURN (-EPROTO);
413                 }
414
415                 if ((body->valid & OBD_MD_FLEASIZE) != 0) {
416                         /* The eadata is opaque; just check that it is there.
417                          * Eventually, obd_unpackmd() will check the contents */
418                         eadata = lustre_swab_repbuf(req, 2, body->eadatasize,
419                                                     NULL);
420                         if (eadata == NULL) {
421                                 CERROR ("Missing/short eadata\n");
422                                 RETURN (-EPROTO);
423                         }
424                         /* We save the reply LOV EA in case we have to replay
425                          * a create for recovery.  If we didn't allocate a
426                          * large enough request buffer above we need to
427                          * reallocate it here to hold the actual LOV EA. */
428                         if (it->it_op & IT_OPEN) {
429                                 if (req->rq_reqmsg->buflens[4] <
430                                     body->eadatasize)
431                                         mdc_realloc_openmsg(req, body, size);
432
433                                 lmm = lustre_msg_buf(req->rq_reqmsg, 4,
434                                                      body->eadatasize);
435                                 if (lmm)
436                                         memcpy(lmm, eadata, body->eadatasize);
437                         }
438                 }
439         }
440
441         RETURN(rc);
442 }
443 EXPORT_SYMBOL(mdc_enqueue);
444
445 /* 
446  * This long block is all about fixing up the lock and request state
447  * so that it is correct as of the moment _before_ the operation was
448  * applied; that way, the VFS will think that everything is normal and
449  * call Lustre's regular VFS methods.
450  *
451  * If we're performing a creation, that means that unless the creation
452  * failed with EEXIST, we should fake up a negative dentry.
453  *
454  * For everything else, we want to lookup to succeed.
455  *
456  * One additional note: if CREATE or OPEN succeeded, we add an extra
457  * reference to the request because we need to keep it around until
458  * ll_create/ll_open gets called.
459  *
460  * The server will return to us, in it_disposition, an indication of
461  * exactly what d.lustre.it_status refers to.
462  *
463  * If DISP_OPEN_OPEN is set, then d.lustre.it_status refers to the open() call,
464  * otherwise if DISP_OPEN_CREATE is set, then it status is the
465  * creation failure mode.  In either case, one of DISP_LOOKUP_NEG or
466  * DISP_LOOKUP_POS will be set, indicating whether the child lookup
467  * was successful.
468  *
469  * Else, if DISP_LOOKUP_EXECD then d.lustre.it_status is the rc of the
470  * child lookup.
471  */
472 int mdc_intent_lock(struct obd_export *exp, struct mdc_op_data *op_data,
473                     void *lmm, int lmmsize, struct lookup_intent *it,
474                     int lookup_flags, struct ptlrpc_request **reqp,
475                     ldlm_blocking_callback cb_blocking, int extra_lock_flags)
476 {
477         struct lustre_handle lockh;
478         struct ptlrpc_request *request;
479         int rc = 0;
480         struct mds_body *mds_body;
481         struct lustre_handle old_lock;
482         struct ldlm_lock *lock;
483         ENTRY;
484         LASSERT(it);
485
486         CDEBUG(D_DLMTRACE,"name: %.*s in inode "LPU64", intent: %s flags %#o\n",
487                op_data->namelen, op_data->name, op_data->fid1.id,
488                ldlm_it2str(it->it_op), it->it_flags);
489
490         if (op_data->fid2.id &&
491             (it->it_op == IT_LOOKUP || it->it_op == IT_GETATTR)) {
492                 /* We could just return 1 immediately, but since we should only
493                  * be called in revalidate_it if we already have a lock, let's
494                  * verify that. */
495                 struct ldlm_res_id res_id = {.name ={op_data->fid2.id,
496                                                      op_data->fid2.generation}};
497                 struct lustre_handle lockh;
498                 int mode = LCK_PR;
499
500                 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
501                                      LDLM_FL_BLOCK_GRANTED, &res_id,
502                                      LDLM_PLAIN, NULL, LCK_PR, &lockh);
503                 if (!rc) {
504                         mode = LCK_PW;
505                         rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
506                                              LDLM_FL_BLOCK_GRANTED, &res_id,
507                                              LDLM_PLAIN, NULL, LCK_PW, &lockh);
508                 }
509                 if (rc) {
510                         memcpy(&it->d.lustre.it_lock_handle, &lockh,
511                                sizeof(lockh));
512                         it->d.lustre.it_lock_mode = mode;
513                 }
514                 RETURN(rc);
515         }
516
517         /* lookup_it may be called only after revalidate_it has run, because
518          * revalidate_it cannot return errors, only zero.  Returning zero causes
519          * this call to lookup, which *can* return an error.
520          *
521          * We only want to execute the request associated with the intent one
522          * time, however, so don't send the request again.  Instead, skip past
523          * this and use the request from revalidate.  In this case, revalidate
524          * never dropped its reference, so the refcounts are all OK */
525         if (!it_disposition(it, DISP_ENQ_COMPLETE)) {
526
527                 rc = mdc_enqueue(exp, LDLM_PLAIN, it, it_to_lock_mode(it),
528                                  op_data, &lockh, lmm, lmmsize,
529                                  ldlm_completion_ast, cb_blocking, NULL,
530                                  extra_lock_flags);
531                 if (rc < 0)
532                         RETURN(rc);
533                 memcpy(&it->d.lustre.it_lock_handle, &lockh, sizeof(lockh));
534         }
535         request = *reqp = it->d.lustre.it_data;
536         LASSERT(request != NULL);
537         LASSERT(request != LP_POISON);
538
539         /* If we're doing an IT_OPEN which did not result in an actual
540          * successful open, then we need to remove the bit which saves
541          * this request for unconditional replay.
542          *
543          * It's important that we do this first!  Otherwise we might exit the
544          * function without doing so, and try to replay a failed create
545          * (bug 3440) */
546         if (it->it_op & IT_OPEN && request->rq_replay &&
547             (!it_disposition(it, DISP_OPEN_OPEN) ||it->d.lustre.it_status != 0))
548                 mdc_clear_replay_flag(request, it->d.lustre.it_status);
549
550         if (!it_disposition(it, DISP_IT_EXECD)) {
551                 /* The server failed before it even started executing the
552                  * intent, i.e. because it couldn't unpack the request. */
553                 LASSERT(it->d.lustre.it_status != 0);
554                 RETURN(it->d.lustre.it_status);
555         }
556         rc = it_open_error(DISP_IT_EXECD, it);
557         if (rc)
558                 RETURN(rc);
559
560         mds_body = lustre_msg_buf(request->rq_repmsg, 1, sizeof(*mds_body));
561         LASSERT(mds_body != NULL);           /* mdc_enqueue checked */
562         LASSERT_REPSWABBED(request, 1); /* mdc_enqueue swabbed */
563
564         /* If we were revalidating a fid/name pair, mark the intent in
565          * case we fail and get called again from lookup */
566         if (op_data->fid2.id) {
567                 it_set_disposition(it, DISP_ENQ_COMPLETE);
568                 /* Also: did we find the same inode? */
569                 if (memcmp(&op_data->fid2, &mds_body->fid1, sizeof(op_data->fid2)))
570                         RETURN(-ESTALE);
571         }
572
573         rc = it_open_error(DISP_LOOKUP_EXECD, it);
574         if (rc)
575                 RETURN(rc);
576
577         /* keep requests around for the multiple phases of the call
578          * this shows the DISP_XX must guarantee we make it into the call
579          */
580         if (it_disposition(it, DISP_OPEN_CREATE) &&
581             !it_open_error(DISP_OPEN_CREATE, it))
582                 ptlrpc_request_addref(request); /* balanced in ll_create_node */
583         if (it_disposition(it, DISP_OPEN_OPEN) &&
584             !it_open_error(DISP_OPEN_OPEN, it))
585                 ptlrpc_request_addref(request); /* balanced in ll_file_open */
586
587         if (it->it_op & IT_CREAT) {
588                 /* XXX this belongs in ll_create_it */
589         } else if (it->it_op == IT_OPEN) {
590                 LASSERT(!it_disposition(it, DISP_OPEN_CREATE));
591         } else {
592                 LASSERT(it->it_op & (IT_GETATTR | IT_LOOKUP));
593         }
594
595         /* If we already have a matching lock, then cancel the new
596          * one.  We have to set the data here instead of in
597          * mdc_enqueue, because we need to use the child's inode as
598          * the l_ast_data to match, and that's not available until
599          * intent_finish has performed the iget().) */
600         lock = ldlm_handle2lock(&lockh);
601         if (lock) {
602                 LDLM_DEBUG(lock, "matching against this");
603                 LDLM_LOCK_PUT(lock);
604                 memcpy(&old_lock, &lockh, sizeof(lockh));
605                 if (ldlm_lock_match(NULL, LDLM_FL_BLOCK_GRANTED, NULL,
606                                     LDLM_PLAIN, NULL, LCK_NL, &old_lock)) {
607                         ldlm_lock_decref_and_cancel(&lockh,
608                                                     it->d.lustre.it_lock_mode);
609                         memcpy(&lockh, &old_lock, sizeof(old_lock));
610                         memcpy(&it->d.lustre.it_lock_handle, &lockh,
611                                sizeof(lockh));
612                 }
613         }
614         CDEBUG(D_DENTRY,"D_IT dentry %.*s intent: %s status %d disp %x rc %d\n",
615                op_data->namelen, op_data->name, ldlm_it2str(it->it_op),
616                it->d.lustre.it_status, it->d.lustre.it_disposition, rc);
617
618         RETURN(rc);
619 }
620 EXPORT_SYMBOL(mdc_intent_lock);