Whamcloud - gitweb
82ad397b0da14dcba90b20bae70142782b9ac8ab
[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/lustre_acl.h>
40 #include <obd_class.h>
41 #include <lustre_dlm.h>
42 /* fid_res_name_eq() */
43 #include <lustre_fid.h>
44 #include <lprocfs_status.h>
45 #include "mdc_internal.h"
46
47 int it_disposition(struct lookup_intent *it, int flag)
48 {
49         return it->d.lustre.it_disposition & flag;
50 }
51 EXPORT_SYMBOL(it_disposition);
52
53 void it_set_disposition(struct lookup_intent *it, int flag)
54 {
55         it->d.lustre.it_disposition |= flag;
56 }
57 EXPORT_SYMBOL(it_set_disposition);
58
59 void it_clear_disposition(struct lookup_intent *it, int flag)
60 {
61         it->d.lustre.it_disposition &= ~flag;
62 }
63 EXPORT_SYMBOL(it_clear_disposition);
64
65 static int it_to_lock_mode(struct lookup_intent *it)
66 {
67         ENTRY;
68
69         /* CREAT needs to be tested before open (both could be set) */
70         if (it->it_op & IT_CREAT)
71                 return LCK_PW;
72         else if (it->it_op & (IT_READDIR | IT_GETATTR | IT_OPEN | IT_LOOKUP))
73                 return LCK_PR;
74
75         LBUG();
76         RETURN(-EINVAL);
77 }
78
79 int it_open_error(int phase, struct lookup_intent *it)
80 {
81         if (it_disposition(it, DISP_OPEN_OPEN)) {
82                 if (phase >= DISP_OPEN_OPEN)
83                         return it->d.lustre.it_status;
84                 else
85                         return 0;
86         }
87
88         if (it_disposition(it, DISP_OPEN_CREATE)) {
89                 if (phase >= DISP_OPEN_CREATE)
90                         return it->d.lustre.it_status;
91                 else
92                         return 0;
93         }
94
95         if (it_disposition(it, DISP_LOOKUP_EXECD)) {
96                 if (phase >= DISP_LOOKUP_EXECD)
97                         return it->d.lustre.it_status;
98                 else
99                         return 0;
100         }
101
102         if (it_disposition(it, DISP_IT_EXECD)) {
103                 if (phase >= DISP_IT_EXECD)
104                         return it->d.lustre.it_status;
105                 else
106                         return 0;
107         }
108         CERROR("it disp: %X, status: %d\n", it->d.lustre.it_disposition,
109                it->d.lustre.it_status);
110         LBUG();
111         return 0;
112 }
113 EXPORT_SYMBOL(it_open_error);
114
115 /* this must be called on a lockh that is known to have a referenced lock */
116 int mdc_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data)
117 {
118         struct ldlm_lock *lock;
119         ENTRY;
120
121         if (!*lockh) {
122                 EXIT;
123                 RETURN(0);
124         }
125
126         lock = ldlm_handle2lock((struct lustre_handle *)lockh);
127
128         LASSERT(lock != NULL);
129         lock_res_and_lock(lock);
130 #ifdef __KERNEL__
131         if (lock->l_ast_data && lock->l_ast_data != data) {
132                 struct inode *new_inode = data;
133                 struct inode *old_inode = lock->l_ast_data;
134                 LASSERTF(old_inode->i_state & I_FREEING,
135                          "Found existing inode %p/%lu/%u state %lu in lock: "
136                          "setting data to %p/%lu/%u\n", old_inode,
137                          old_inode->i_ino, old_inode->i_generation,
138                          old_inode->i_state,
139                          new_inode, new_inode->i_ino, new_inode->i_generation);
140         }
141 #endif
142         lock->l_ast_data = data;
143         unlock_res_and_lock(lock);
144         LDLM_LOCK_PUT(lock);
145
146         RETURN(0);
147 }
148
149 int mdc_lock_match(struct obd_export *exp, int flags,
150                    const struct lu_fid *fid, ldlm_type_t type,
151                    ldlm_policy_data_t *policy, ldlm_mode_t mode,
152                    struct lustre_handle *lockh)
153 {
154         struct ldlm_res_id res_id =
155                 { .name = {fid_seq(fid),
156                            fid_oid(fid),
157                            fid_ver(fid)} };
158         struct obd_device *obd = class_exp2obd(exp);
159         int rc;
160         ENTRY;
161
162         rc = ldlm_lock_match(obd->obd_namespace, flags,
163                              &res_id, type, policy, mode, lockh);
164
165         RETURN(rc);
166 }
167
168 int mdc_cancel_unused(struct obd_export *exp,
169                       const struct lu_fid *fid,
170                       ldlm_policy_data_t *policy,
171                       ldlm_mode_t mode, int flags, void *opaque)
172 {
173         struct ldlm_res_id res_id =
174                 { .name = {fid_seq(fid),
175                            fid_oid(fid),
176                            fid_ver(fid)} };
177         struct obd_device *obd = class_exp2obd(exp);
178         int rc;
179
180         ENTRY;
181
182         rc = ldlm_cli_cancel_unused_resource(obd->obd_namespace, &res_id,
183                                              policy, mode, flags, opaque);
184         RETURN(rc);
185 }
186
187 int mdc_change_cbdata(struct obd_export *exp,
188                       const struct lu_fid *fid,
189                       ldlm_iterator_t it, void *data)
190 {
191         struct ldlm_res_id res_id = { .name = {0} };
192         ENTRY;
193
194         res_id.name[0] = fid_seq(fid);
195         res_id.name[1] = fid_oid(fid);
196         res_id.name[2] = fid_ver(fid);
197
198         ldlm_resource_iterate(class_exp2obd(exp)->obd_namespace,
199                               &res_id, it, data);
200
201         EXIT;
202         return 0;
203 }
204
205 static inline void mdc_clear_replay_flag(struct ptlrpc_request *req, int rc)
206 {
207         /* Don't hold error requests for replay. */
208         if (req->rq_replay) {
209                 spin_lock(&req->rq_lock);
210                 req->rq_replay = 0;
211                 spin_unlock(&req->rq_lock);
212         }
213         if (rc && req->rq_transno != 0) {
214                 DEBUG_REQ(D_ERROR, req, "transno returned on error rc %d", rc);
215                 LBUG();
216         }
217 }
218
219 /* Save a large LOV EA into the request buffer so that it is available
220  * for replay.  We don't do this in the initial request because the
221  * original request doesn't need this buffer (at most it sends just the
222  * lov_mds_md) and it is a waste of RAM/bandwidth to send the empty
223  * buffer and may also be difficult to allocate and save a very large
224  * request buffer for each open. (bug 5707)
225  *
226  * OOM here may cause recovery failure if lmm is needed (only for the
227  * original open if the MDS crashed just when this client also OOM'd)
228  * but this is incredibly unlikely, and questionable whether the client
229  * could do MDS recovery under OOM anyways... */
230 static void mdc_realloc_openmsg(struct ptlrpc_request *req,
231                                 struct mdt_body *body, int size[9])
232 {
233         int     rc;
234         ENTRY;
235
236         rc = sptlrpc_cli_enlarge_reqbuf(req, DLM_INTENT_REC_OFF + 4,
237                                         body->eadatasize);
238         if (rc) {
239                 CERROR("Can't enlarge segment %d size to %d\n",
240                        DLM_INTENT_REC_OFF + 4, body->eadatasize);
241                 body->valid &= ~OBD_MD_FLEASIZE;
242                 body->eadatasize = 0;
243         }
244         EXIT;
245 }
246
247 /* We always reserve enough space in the reply packet for a stripe MD, because
248  * we don't know in advance the file type. */
249 int mdc_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
250                 struct lookup_intent *it, struct md_op_data *op_data,
251                 struct lustre_handle *lockh, void *lmm, int lmmsize,
252                 int extra_lock_flags)
253 {
254         struct ptlrpc_request *req;
255         struct obd_device *obddev = class_exp2obd(exp);
256         struct ldlm_res_id res_id =
257                 { .name = {fid_seq(&op_data->op_fid1),
258                            fid_oid(&op_data->op_fid1),
259                            fid_ver(&op_data->op_fid1)} };
260         ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_LOOKUP } };
261         struct ldlm_request *lockreq;
262         struct ldlm_intent *lit;
263         struct ldlm_reply *lockrep;
264         int size[9] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
265                         [DLM_LOCKREQ_OFF]     = sizeof(*lockreq),
266                         [DLM_INTENT_IT_OFF]   = sizeof(*lit),
267                         0, 0, 0, 0, 0, 0 };
268         int repsize[7] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
269                            [DLM_LOCKREPLY_OFF]   = sizeof(*lockrep),
270                            [DLM_REPLY_REC_OFF]   = sizeof(struct mdt_body),
271                            [DLM_REPLY_REC_OFF+1] = obddev->u.cli.
272                                                    cl_max_mds_easize,
273                            0, 0, 0 };
274         int flags = extra_lock_flags | LDLM_FL_HAS_INTENT;
275         int repbufcnt = 4, rc;
276         ENTRY;
277
278         LASSERTF(einfo->ei_type == LDLM_IBITS,"lock type %d\n", einfo->ei_type);
279
280         if (it->it_op & IT_OPEN) {
281                 int do_join = !!(it->it_flags & O_JOIN_FILE);
282                 CFS_LIST_HEAD(cancels);
283                 int count = 0;
284                 int mode;
285
286                 it->it_create_mode = (it->it_create_mode & ~S_IFMT) | S_IFREG;
287
288                 size[DLM_INTENT_REC_OFF] = sizeof(struct mdt_rec_create);
289                 /* parent capability */
290                 size[DLM_INTENT_REC_OFF + 1] = op_data->op_capa1 ?
291                                                sizeof(struct lustre_capa) : 0;
292                 /* child capability, used for replay only */
293                 size[DLM_INTENT_REC_OFF + 2] = sizeof(struct lustre_capa);
294                 size[DLM_INTENT_REC_OFF + 3] = op_data->op_namelen + 1;
295                 /* As an optimization, we allocate an RPC request buffer for
296                  * at least a default-sized LOV EA even if we aren't sending
297                  * one.
298                  */
299                 size[DLM_INTENT_REC_OFF + 4] = max(lmmsize,
300                                            obddev->u.cli.cl_default_mds_easize);
301
302                 /* XXX: openlock is not cancelled for cross-refs. */
303                 /* If inode is known, cancel conflicting OPEN locks. */
304                 if (fid_is_sane(&op_data->op_fid2)) {
305                         if (it->it_flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
306                                 mode = LCK_CW;
307 #ifdef FMODE_EXEC
308                         else if (it->it_flags & FMODE_EXEC)
309                                 mode = LCK_PR;
310 #endif
311                         else 
312                                 mode = LCK_CR;
313                         count = mdc_resource_get_unused(exp, &op_data->op_fid2,
314                                                         &cancels, mode,
315                                                         MDS_INODELOCK_OPEN);
316                 }
317
318                 /* If CREATE or JOIN_FILE, cancel parent's UPDATE lock. */
319                 if (it->it_op & IT_CREAT || it->it_flags & O_JOIN_FILE)
320                         mode = LCK_EX;
321                 else
322                         mode = LCK_CR;
323                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
324                                                  &cancels, mode,
325                                                  MDS_INODELOCK_UPDATE);
326
327                 if (do_join)
328                         size[DLM_INTENT_REC_OFF + 5] =
329                                                 sizeof(struct mdt_rec_join);
330
331                 req = ldlm_prep_enqueue_req(exp, 8 + do_join, size, &cancels,
332                                             count);
333                 if (!req)
334                         RETURN(-ENOMEM);
335
336                 if (do_join) {
337                         /* join is like an unlink of the tail */
338                         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
339                         mdc_join_pack(req, DLM_INTENT_REC_OFF + 5, op_data,
340                                       (*(__u64 *)op_data->op_data));
341                 }
342
343                 spin_lock(&req->rq_lock);
344                 req->rq_replay = 1;
345                 spin_unlock(&req->rq_lock);
346
347                 /* pack the intent */
348                 lit = lustre_msg_buf(req->rq_reqmsg, DLM_INTENT_IT_OFF,
349                                      sizeof(*lit));
350                 lit->opc = (__u64)it->it_op;
351
352                 /* pack the intended request */
353                 mdc_open_pack(req, DLM_INTENT_REC_OFF, op_data,
354                               it->it_create_mode, 0, it->it_flags,
355                               lmm, lmmsize);
356
357                 /* for remote client, fetch remote perm for current user */
358                 repsize[repbufcnt++] = client_is_remote(exp) ?
359                                        sizeof(struct mdt_remote_perm) :
360                                        LUSTRE_POSIX_ACL_MAX_SIZE;
361                 repsize[repbufcnt++] = sizeof(struct lustre_capa);
362                 repsize[repbufcnt++] = sizeof(struct lustre_capa);
363         } else if (it->it_op & IT_UNLINK) {
364                 size[DLM_INTENT_REC_OFF] = sizeof(struct mdt_rec_unlink);
365                 size[DLM_INTENT_REC_OFF + 1] = op_data->op_capa1 ?
366                                                sizeof(struct lustre_capa) : 0;
367                 size[DLM_INTENT_REC_OFF + 2] = op_data->op_namelen + 1;
368                 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
369                 req = ldlm_prep_enqueue_req(exp, 6, size, NULL, 0);
370                 if (!req)
371                         RETURN(-ENOMEM);
372
373                 /* pack the intent */
374                 lit = lustre_msg_buf(req->rq_reqmsg, DLM_INTENT_IT_OFF,
375                                      sizeof(*lit));
376                 lit->opc = (__u64)it->it_op;
377
378                 /* pack the intended request */
379                 mdc_unlink_pack(req, DLM_INTENT_REC_OFF, op_data);
380
381                 repsize[repbufcnt++] = obddev->u.cli.cl_max_mds_cookiesize;
382         } else if (it->it_op & (IT_GETATTR | IT_LOOKUP)) {
383                 obd_valid valid = OBD_MD_FLGETATTR | OBD_MD_FLEASIZE |
384                         OBD_MD_FLMODEASIZE | OBD_MD_FLDIREA |
385                         OBD_MD_FLMDSCAPA | OBD_MD_MEA;
386                 valid |= client_is_remote(exp) ? OBD_MD_FLRMTPERM :
387                                                  OBD_MD_FLACL;
388                 size[DLM_INTENT_REC_OFF] = sizeof(struct mdt_body);
389                 size[DLM_INTENT_REC_OFF + 1] = op_data->op_capa1 ?
390                                                sizeof(struct lustre_capa) : 0;
391                 size[DLM_INTENT_REC_OFF + 2] = op_data->op_namelen + 1;
392
393                 if (it->it_op & IT_GETATTR)
394                         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
395
396                 req = ldlm_prep_enqueue_req(exp, 6, size, NULL, 0);
397                 if (!req)
398                         RETURN(-ENOMEM);
399
400                 /* pack the intent */
401                 lit = lustre_msg_buf(req->rq_reqmsg, DLM_INTENT_IT_OFF,
402                                      sizeof(*lit));
403                 lit->opc = (__u64)it->it_op;
404
405                 /* pack the intended request */
406                 mdc_getattr_pack(req, DLM_INTENT_REC_OFF, valid,
407                                  it->it_flags, op_data);
408
409                 repsize[repbufcnt++] = client_is_remote(exp) ?
410                                        sizeof(struct mdt_remote_perm) :
411                                        LUSTRE_POSIX_ACL_MAX_SIZE;
412                 repsize[repbufcnt++] = sizeof(struct lustre_capa);
413         } else if (it->it_op == IT_READDIR) {
414                 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
415                 req = ldlm_prep_enqueue_req(exp, 2, size, NULL, 0);
416                 if (!req)
417                         RETURN(-ENOMEM);
418
419                 repbufcnt = 2;
420         } else {
421                 LBUG();
422                 RETURN(-EINVAL);
423         }
424
425         /* get ready for the reply */
426         ptlrpc_req_set_repsize(req, repbufcnt, repsize);
427
428          /* It is important to obtain rpc_lock first (if applicable), so that
429           * threads that are serialised with rpc_lock are not polluting our
430           * rpcs in flight counter */
431         mdc_get_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
432         mdc_enter_request(&obddev->u.cli);
433         rc = ldlm_cli_enqueue(exp, &req, einfo, &res_id, &policy, &flags, NULL,
434                               0, NULL, lockh, 0);
435         mdc_exit_request(&obddev->u.cli);
436         mdc_put_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
437
438         /* Similarly, if we're going to replay this request, we don't want to
439          * actually get a lock, just perform the intent. */
440         if (req->rq_transno || req->rq_replay) {
441                 lockreq = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF,
442                                          sizeof(*lockreq));
443                 lockreq->lock_flags |= LDLM_FL_INTENT_ONLY;
444         }
445
446         if (rc == ELDLM_LOCK_ABORTED) {
447                 einfo->ei_mode = 0;
448                 memset(lockh, 0, sizeof(*lockh));
449                 rc = 0;
450         } else if (rc != 0) {
451                 CERROR("ldlm_cli_enqueue: %d\n", rc);
452                 LASSERTF(rc < 0, "rc %d\n", rc);
453                 mdc_clear_replay_flag(req, rc);
454                 ptlrpc_req_finished(req);
455                 RETURN(rc);
456         } else { /* rc = 0 */
457                 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
458                 LASSERT(lock);
459
460                 /* If the server gave us back a different lock mode, we should
461                  * fix up our variables. */
462                 if (lock->l_req_mode != einfo->ei_mode) {
463                         ldlm_lock_addref(lockh, lock->l_req_mode);
464                         ldlm_lock_decref(lockh, einfo->ei_mode);
465                         einfo->ei_mode = lock->l_req_mode;
466                 }
467                 LDLM_LOCK_PUT(lock);
468         }
469
470         lockrep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
471                                  sizeof(*lockrep));
472         LASSERT(lockrep != NULL);                 /* checked by ldlm_cli_enqueue() */
473         /* swabbed by ldlm_cli_enqueue() */
474         LASSERT(lustre_rep_swabbed(req, DLM_LOCKREPLY_OFF));
475
476         it->d.lustre.it_disposition = (int)lockrep->lock_policy_res1;
477         it->d.lustre.it_status = (int)lockrep->lock_policy_res2;
478         it->d.lustre.it_lock_mode = einfo->ei_mode;
479         it->d.lustre.it_data = req;
480
481         if (it->d.lustre.it_status < 0 && req->rq_replay)
482                 mdc_clear_replay_flag(req, it->d.lustre.it_status);
483
484         /* If we're doing an IT_OPEN which did not result in an actual
485          * successful open, then we need to remove the bit which saves
486          * this request for unconditional replay.
487          *
488          * It's important that we do this first!  Otherwise we might exit the
489          * function without doing so, and try to replay a failed create
490          * (bug 3440) */
491         if (it->it_op & IT_OPEN && req->rq_replay &&
492             (!it_disposition(it, DISP_OPEN_OPEN) ||it->d.lustre.it_status != 0))
493                 mdc_clear_replay_flag(req, it->d.lustre.it_status);
494
495         DEBUG_REQ(D_RPCTRACE, req, "op: %d disposition: %x, status: %d",
496                   it->it_op,it->d.lustre.it_disposition,it->d.lustre.it_status);
497
498         /* We know what to expect, so we do any byte flipping required here */
499         LASSERT(repbufcnt == 7 || repbufcnt == 6 || repbufcnt == 2);
500         if (repbufcnt >= 6) {
501                 int reply_off = DLM_REPLY_REC_OFF;
502                 struct mdt_body *body;
503
504                 body = lustre_swab_repbuf(req, reply_off++, sizeof(*body),
505                                          lustre_swab_mdt_body);
506                 if (body == NULL) {
507                         CERROR ("Can't swab mdt_body\n");
508                         RETURN (-EPROTO);
509                 }
510
511                 if (req->rq_replay && it_disposition(it, DISP_OPEN_OPEN) &&
512                     !it_open_error(DISP_OPEN_OPEN, it)) {
513                         /*
514                          * If this is a successful OPEN request, we need to set
515                          * replay handler and data early, so that if replay
516                          * happens immediately after swabbing below, new reply
517                          * is swabbed by that handler correctly.
518                          */
519                         mdc_set_open_replay_data(NULL, NULL, req);
520                 }
521
522                 if ((body->valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE)) != 0) {
523                         void *eadata;
524
525                         /*
526                          * The eadata is opaque; just check that it is there.
527                          * Eventually, obd_unpackmd() will check the contents.
528                          */
529                         eadata = lustre_swab_repbuf(req, reply_off++,
530                                                     body->eadatasize, NULL);
531                         if (eadata == NULL) {
532                                 CERROR("Missing/short eadata\n");
533                                 RETURN(-EPROTO);
534                         }
535                         if (body->valid & OBD_MD_FLMODEASIZE) {
536                                 if (obddev->u.cli.cl_max_mds_easize <
537                                     body->max_mdsize) {
538                                         obddev->u.cli.cl_max_mds_easize =
539                                                 body->max_mdsize;
540                                         CDEBUG(D_INFO, "maxeasize become %d\n",
541                                                body->max_mdsize);
542                                 }
543                                 if (obddev->u.cli.cl_max_mds_cookiesize <
544                                     body->max_cookiesize) {
545                                         obddev->u.cli.cl_max_mds_cookiesize =
546                                                 body->max_cookiesize;
547                                         CDEBUG(D_INFO, "cookiesize become %d\n",
548                                                body->max_cookiesize);
549                                 }
550                         }
551
552                         /*
553                          * We save the reply LOV EA in case we have to replay a
554                          * create for recovery.  If we didn't allocate a large
555                          * enough request buffer above we need to reallocate it
556                          * here to hold the actual LOV EA.
557                          *
558                          * To not save LOV EA if request is not going to replay
559                          * (for example error one).
560                          */
561                         if ((it->it_op & IT_OPEN) && req->rq_replay) {
562                                 if (lustre_msg_buflen(req->rq_reqmsg,
563                                                       DLM_INTENT_REC_OFF + 4) <
564                                     body->eadatasize)
565                                         mdc_realloc_openmsg(req, body, size);
566
567                                 lmm = lustre_msg_buf(req->rq_reqmsg,
568                                                      DLM_INTENT_REC_OFF + 4,
569                                                      body->eadatasize);
570                                 if (lmm)
571                                         memcpy(lmm, eadata, body->eadatasize);
572                         }
573                 }
574                 if (body->valid & OBD_MD_FLRMTPERM) {
575                         struct mdt_remote_perm *perm;
576
577                         LASSERT(client_is_remote(exp));
578                         perm = lustre_swab_repbuf(req, reply_off++,
579                                                   sizeof(*perm),
580                                                   lustre_swab_mdt_remote_perm);
581                         if (perm == NULL) {
582                                 CERROR("missing remote permission!\n");
583                                 RETURN(-EPROTO);
584                         }
585                 } else if ((body->valid & OBD_MD_FLACL) && body->aclsize) {
586                         reply_off++;
587                 }
588                 if (body->valid & OBD_MD_FLMDSCAPA) {
589                         struct lustre_capa *capa, *p;
590
591                         capa = lustre_unpack_capa(req->rq_repmsg, reply_off++);
592                         if (capa == NULL) {
593                                 CERROR("Missing/short MDS capability\n");
594                                 RETURN(-EPROTO);
595                         }
596
597                         if (it->it_op & IT_OPEN) {
598                                 /* client fid capa will be checked in replay */
599                                 p = lustre_msg_buf(req->rq_reqmsg,
600                                                    DLM_INTENT_REC_OFF + 2,
601                                                    sizeof(*p));
602                                 LASSERT(p);
603                                 *p = *capa;
604                         }
605                 }
606                 if (body->valid & OBD_MD_FLOSSCAPA) {
607                         struct lustre_capa *capa;
608
609                         capa = lustre_unpack_capa(req->rq_repmsg, reply_off++);
610                         if (capa == NULL) {
611                                 CERROR("Missing/short OSS capability\n");
612                                 RETURN(-EPROTO);
613                         }
614                 }
615         }
616
617         RETURN(rc);
618 }
619 /*
620  * This long block is all about fixing up the lock and request state
621  * so that it is correct as of the moment _before_ the operation was
622  * applied; that way, the VFS will think that everything is normal and
623  * call Lustre's regular VFS methods.
624  *
625  * If we're performing a creation, that means that unless the creation
626  * failed with EEXIST, we should fake up a negative dentry.
627  *
628  * For everything else, we want to lookup to succeed.
629  *
630  * One additional note: if CREATE or OPEN succeeded, we add an extra
631  * reference to the request because we need to keep it around until
632  * ll_create/ll_open gets called.
633  *
634  * The server will return to us, in it_disposition, an indication of
635  * exactly what d.lustre.it_status refers to.
636  *
637  * If DISP_OPEN_OPEN is set, then d.lustre.it_status refers to the open() call,
638  * otherwise if DISP_OPEN_CREATE is set, then it status is the
639  * creation failure mode.  In either case, one of DISP_LOOKUP_NEG or
640  * DISP_LOOKUP_POS will be set, indicating whether the child lookup
641  * was successful.
642  *
643  * Else, if DISP_LOOKUP_EXECD then d.lustre.it_status is the rc of the
644  * child lookup.
645  */
646 int mdc_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
647                     void *lmm, int lmmsize, struct lookup_intent *it,
648                     int lookup_flags, struct ptlrpc_request **reqp,
649                     ldlm_blocking_callback cb_blocking,
650                     int extra_lock_flags)
651 {
652         struct ptlrpc_request *request;
653         struct lustre_handle old_lock;
654         struct lustre_handle lockh;
655         struct mdt_body *mdt_body;
656         struct ldlm_lock *lock;
657         int rc = 0;
658         ENTRY;
659         LASSERT(it);
660
661         CDEBUG(D_DLMTRACE, "(name: %.*s,"DFID") in obj "DFID
662                ", intent: %s flags %#o\n", op_data->op_namelen,
663                op_data->op_name, PFID(&op_data->op_fid2),
664                PFID(&op_data->op_fid1), ldlm_it2str(it->it_op),
665                it->it_flags);
666
667         if (fid_is_sane(&op_data->op_fid2) &&
668             (it->it_op & (IT_LOOKUP | IT_GETATTR))) {
669                 /* We could just return 1 immediately, but since we should only
670                  * be called in revalidate_it if we already have a lock, let's
671                  * verify that. */
672                 struct ldlm_res_id res_id = { .name = { fid_seq(&op_data->op_fid2),
673                                                         fid_oid(&op_data->op_fid2),
674                                                         fid_ver(&op_data->op_fid2) } };
675                 ldlm_policy_data_t policy;
676                 ldlm_mode_t mode = LCK_CR;
677
678                 /* As not all attributes are kept under update lock, e.g.
679                    owner/group/acls are under lookup lock, we need both
680                    ibits for GETATTR. */
681
682                 /* For CMD, UPDATE lock and LOOKUP lock can not be got
683                  * at the same for cross-object, so we can not match
684                  * the 2 lock at the same time FIXME: but how to handle
685                  * the above situation */
686                 policy.l_inodebits.bits = (it->it_op == IT_GETATTR) ?
687                         MDS_INODELOCK_UPDATE : MDS_INODELOCK_LOOKUP;
688
689                 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
690                                      LDLM_FL_BLOCK_GRANTED, &res_id,
691                                      LDLM_IBITS, &policy, mode, &lockh);
692                 if (!rc) {
693                         mode = LCK_CW;
694                         rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
695                                              LDLM_FL_BLOCK_GRANTED, &res_id,
696                                              LDLM_IBITS, &policy, mode, &lockh);
697                 }
698                 if (!rc) {
699                         mode = LCK_PR;
700                         rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
701                                              LDLM_FL_BLOCK_GRANTED, &res_id,
702                                              LDLM_IBITS, &policy, mode, &lockh);
703                 }
704
705                 if (!rc) {
706                         mode = LCK_PW;
707                         rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
708                                              LDLM_FL_BLOCK_GRANTED, &res_id,
709                                              LDLM_IBITS, &policy, mode, &lockh);
710                 }
711
712                 if (rc) {
713                         memcpy(&it->d.lustre.it_lock_handle, &lockh,
714                                sizeof(lockh));
715                         it->d.lustre.it_lock_mode = mode;
716                 }
717
718                 /* Only return failure if it was not GETATTR by cfid
719                    (from inode_revalidate) */
720                 if (rc || op_data->op_namelen != 0)
721                         RETURN(rc);
722         }
723
724         /* lookup_it may be called only after revalidate_it has run, because
725          * revalidate_it cannot return errors, only zero.  Returning zero causes
726          * this call to lookup, which *can* return an error.
727          *
728          * We only want to execute the request associated with the intent one
729          * time, however, so don't send the request again.  Instead, skip past
730          * this and use the request from revalidate.  In this case, revalidate
731          * never dropped its reference, so the refcounts are all OK */
732         if (!it_disposition(it, DISP_ENQ_COMPLETE)) {
733                 struct ldlm_enqueue_info einfo =
734                         { LDLM_IBITS, it_to_lock_mode(it), cb_blocking,
735                           ldlm_completion_ast, NULL, NULL };
736
737                 /* For case if upper layer did not alloc fid, do it now. */
738                 if (!fid_is_sane(&op_data->op_fid2) && it->it_op & IT_CREAT) {
739                         rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
740                         if (rc < 0) {
741                                 CERROR("Can't alloc new fid, rc %d\n", rc);
742                                 RETURN(rc);
743                         }
744                 }
745                 rc = mdc_enqueue(exp, &einfo, it, op_data, &lockh,
746                                  lmm, lmmsize, extra_lock_flags);
747                 if (rc < 0)
748                         RETURN(rc);
749                 memcpy(&it->d.lustre.it_lock_handle, &lockh, sizeof(lockh));
750         } else if (!fid_is_sane(&op_data->op_fid2) ||
751                    !(it->it_flags & O_CHECK_STALE)) {
752                 /* DISP_ENQ_COMPLETE set means there is extra reference on
753                  * request referenced from this intent, saved for subsequent
754                  * lookup.  This path is executed when we proceed to this
755                  * lookup, so we clear DISP_ENQ_COMPLETE */
756                 it_clear_disposition(it, DISP_ENQ_COMPLETE);
757         }
758         request = *reqp = it->d.lustre.it_data;
759         LASSERT(request != NULL);
760         LASSERT(request != LP_POISON);
761         LASSERT(request->rq_repmsg != LP_POISON);
762
763         if (!it_disposition(it, DISP_IT_EXECD)) {
764                 /* The server failed before it even started executing the
765                  * intent, i.e. because it couldn't unpack the request. */
766                 LASSERT(it->d.lustre.it_status != 0);
767                 RETURN(it->d.lustre.it_status);
768         }
769         rc = it_open_error(DISP_IT_EXECD, it);
770         if (rc)
771                 RETURN(rc);
772
773         mdt_body = lustre_msg_buf(request->rq_repmsg, DLM_REPLY_REC_OFF,
774                                   sizeof(*mdt_body));
775         /* mdc_enqueue checked */
776         LASSERT(mdt_body != NULL);
777         /* mdc_enqueue swabbed */
778         LASSERT(lustre_rep_swabbed(request, 1));
779
780         /* If we were revalidating a fid/name pair, mark the intent in
781          * case we fail and get called again from lookup */
782         if (fid_is_sane(&op_data->op_fid2) && (it->it_flags & O_CHECK_STALE) &&
783             (it->it_op != IT_GETATTR)) {
784                 it_set_disposition(it, DISP_ENQ_COMPLETE);
785
786                 /* Also: did we find the same inode? */
787                 if (!lu_fid_eq(&op_data->op_fid2, &mdt_body->fid1))
788                         RETURN(-ESTALE);
789         }
790
791         rc = it_open_error(DISP_LOOKUP_EXECD, it);
792         if (rc)
793                 RETURN(rc);
794
795         /* keep requests around for the multiple phases of the call
796          * this shows the DISP_XX must guarantee we make it into the call
797          */
798         if (!it_disposition(it, DISP_ENQ_CREATE_REF) &&
799             it_disposition(it, DISP_OPEN_CREATE) &&
800             !it_open_error(DISP_OPEN_CREATE, it)) {
801                 it_set_disposition(it, DISP_ENQ_CREATE_REF);
802                 ptlrpc_request_addref(request); /* balanced in ll_create_node */
803         }
804         if (!it_disposition(it, DISP_ENQ_OPEN_REF) &&
805             it_disposition(it, DISP_OPEN_OPEN) &&
806             !it_open_error(DISP_OPEN_OPEN, it)) {
807                 it_set_disposition(it, DISP_ENQ_OPEN_REF);
808                 ptlrpc_request_addref(request); /* balanced in ll_file_open */
809                 /* BUG 11546 - eviction in the middle of open rpc processing */
810                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDC_ENQUEUE_PAUSE, obd_timeout);
811         }
812
813         if (it->it_op & IT_CREAT) {
814                 /* XXX this belongs in ll_create_it */
815         } else if (it->it_op == IT_OPEN) {
816                 LASSERT(!it_disposition(it, DISP_OPEN_CREATE));
817         } else {
818                 LASSERT(it->it_op & (IT_GETATTR | IT_LOOKUP));
819         }
820
821         /* If we already have a matching lock, then cancel the new
822          * one.  We have to set the data here instead of in
823          * mdc_enqueue, because we need to use the child's inode as
824          * the l_ast_data to match, and that's not available until
825          * intent_finish has performed the iget().) */
826         lock = ldlm_handle2lock(&lockh);
827         if (lock) {
828                 ldlm_policy_data_t policy = lock->l_policy_data;
829                 LDLM_DEBUG(lock, "matching against this");
830
831                 LASSERTF(fid_res_name_eq(&mdt_body->fid1,
832                                          &lock->l_resource->lr_name),
833                          "Lock res_id: %lu/%lu/%lu, fid: %lu/%lu/%lu.\n",
834                          (unsigned long)lock->l_resource->lr_name.name[0],
835                          (unsigned long)lock->l_resource->lr_name.name[1],
836                          (unsigned long)lock->l_resource->lr_name.name[2],
837                          (unsigned long)fid_seq(&mdt_body->fid1),
838                          (unsigned long)fid_oid(&mdt_body->fid1),
839                          (unsigned long)fid_ver(&mdt_body->fid1));
840                 LDLM_LOCK_PUT(lock);
841
842                 memcpy(&old_lock, &lockh, sizeof(lockh));
843                 if (ldlm_lock_match(NULL, LDLM_FL_BLOCK_GRANTED, NULL,
844                                     LDLM_IBITS, &policy, LCK_NL, &old_lock)) {
845                         ldlm_lock_decref_and_cancel(&lockh,
846                                                     it->d.lustre.it_lock_mode);
847                         memcpy(&lockh, &old_lock, sizeof(old_lock));
848                         memcpy(&it->d.lustre.it_lock_handle, &lockh,
849                                sizeof(lockh));
850                 }
851         }
852         CDEBUG(D_DENTRY,"D_IT dentry %.*s intent: %s status %d disp %x rc %d\n",
853                op_data->op_namelen, op_data->op_name, ldlm_it2str(it->it_op),
854                it->d.lustre.it_status, it->d.lustre.it_disposition, rc);
855
856         RETURN(rc);
857 }