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