Whamcloud - gitweb
Branch 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 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,
250                 int lock_type,
251                 struct lookup_intent *it,
252                 int lock_mode,
253                 struct md_op_data *op_data,
254                 struct lustre_handle *lockh,
255                 void *lmm,
256                 int lmmsize,
257                 ldlm_completion_callback cb_completion,
258                 ldlm_blocking_callback cb_blocking,
259                 void *cb_data, int extra_lock_flags)
260 {
261         struct ptlrpc_request *req;
262         struct obd_device *obddev = class_exp2obd(exp);
263         struct ldlm_res_id res_id =
264                 { .name = {fid_seq(&op_data->op_fid1),
265                            fid_oid(&op_data->op_fid1),
266                            fid_ver(&op_data->op_fid1)} };
267         ldlm_policy_data_t policy = { .l_inodebits = { MDS_INODELOCK_LOOKUP } };
268         struct ldlm_request *lockreq;
269         struct ldlm_intent *lit;
270         struct ldlm_reply *lockrep;
271         int size[9] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
272                         [DLM_LOCKREQ_OFF]     = sizeof(*lockreq),
273                         [DLM_INTENT_IT_OFF]   = sizeof(*lit),
274                         0, 0, 0, 0, 0, 0 };
275         int repsize[7] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
276                            [DLM_LOCKREPLY_OFF]   = sizeof(*lockrep),
277                            [DLM_REPLY_REC_OFF]   = sizeof(struct mdt_body),
278                            [DLM_REPLY_REC_OFF+1] = obddev->u.cli.
279                                                    cl_max_mds_easize };
280         int flags = extra_lock_flags | LDLM_FL_HAS_INTENT;
281         int repbufcnt = 4, rc;
282         ENTRY;
283
284         LASSERTF(lock_type == LDLM_IBITS, "lock type %d\n", lock_type);
285 //        LDLM_DEBUG_NOLOCK("mdsintent=%s,name=%s,dir=%lu",
286 //                          ldlm_it2str(it->it_op), it_name, it_inode->i_ino);
287
288         if (it->it_op & IT_OPEN) {
289                 int do_join = !!(it->it_flags & O_JOIN_FILE);
290                 CFS_LIST_HEAD(cancels);
291                 int count = 0;
292                 int mode;
293
294                 it->it_create_mode = (it->it_create_mode & ~S_IFMT) | S_IFREG;
295
296                 size[DLM_INTENT_REC_OFF] = sizeof(struct mdt_rec_create);
297                 /* parent capability */
298                 size[DLM_INTENT_REC_OFF + 1] = op_data->op_capa1 ?
299                                                sizeof(struct lustre_capa) : 0;
300                 /* child capability, used for replay only */
301                 size[DLM_INTENT_REC_OFF + 2] = sizeof(struct lustre_capa);
302                 size[DLM_INTENT_REC_OFF + 3] = op_data->op_namelen + 1;
303                 /* As an optimization, we allocate an RPC request buffer for
304                  * at least a default-sized LOV EA even if we aren't sending
305                  * one.
306                  */
307                 size[DLM_INTENT_REC_OFF + 4] = max(lmmsize,
308                                            obddev->u.cli.cl_default_mds_easize);
309
310                 /* XXX: openlock is not cancelled for cross-refs. */
311                 /* If inode is known, cancel conflicting OPEN locks. */
312                 if (fid_is_sane(&op_data->op_fid2)) {
313                         if (it->it_flags & (FMODE_WRITE|MDS_OPEN_TRUNC))
314                                 mode = LCK_CW;
315 #ifdef FMODE_EXEC
316                         else if (it->it_flags & FMODE_EXEC)
317                                 mode = LCK_PR;
318 #endif
319                         else 
320                                 mode = LCK_CR;
321                         count = mdc_resource_get_unused(exp, &op_data->op_fid2,
322                                                         &cancels, mode,
323                                                         MDS_INODELOCK_OPEN);
324                 }
325
326                 /* If CREATE or JOIN_FILE, cancel parent's UPDATE lock. */
327                 if (it->it_op & IT_CREAT || it->it_flags & O_JOIN_FILE)
328                         mode = LCK_EX;
329                 else
330                         mode = LCK_CR;
331                 count += mdc_resource_get_unused(exp, &op_data->op_fid1,
332                                                  &cancels, mode,
333                                                  MDS_INODELOCK_UPDATE);
334
335                 if (do_join)
336                         size[DLM_INTENT_REC_OFF + 5] =
337                                                 sizeof(struct mdt_rec_join);
338
339                 req = ldlm_prep_enqueue_req(exp, 8 + do_join, size, &cancels,
340                                             count);
341                 if (!req)
342                         RETURN(-ENOMEM);
343
344                 if (do_join) {
345                         /* join is like an unlink of the tail */
346                         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
347                         mdc_join_pack(req, DLM_INTENT_REC_OFF + 5, op_data,
348                                       (*(__u64 *)op_data->op_data));
349                 }
350
351                 spin_lock(&req->rq_lock);
352                 req->rq_replay = 1;
353                 spin_unlock(&req->rq_lock);
354
355                 /* pack the intent */
356                 lit = lustre_msg_buf(req->rq_reqmsg, DLM_INTENT_IT_OFF,
357                                      sizeof(*lit));
358                 lit->opc = (__u64)it->it_op;
359
360                 /* pack the intended request */
361                 mdc_open_pack(req, DLM_INTENT_REC_OFF, op_data,
362                               it->it_create_mode, 0, it->it_flags,
363                               lmm, lmmsize);
364
365                 /* for remote client, fetch remote perm for current user */
366                 repsize[repbufcnt++] = client_is_remote(exp) ?
367                                        sizeof(struct mdt_remote_perm) :
368                                        LUSTRE_POSIX_ACL_MAX_SIZE;
369                 repsize[repbufcnt++] = sizeof(struct lustre_capa);
370                 repsize[repbufcnt++] = sizeof(struct lustre_capa);
371         } else if (it->it_op & IT_UNLINK) {
372                 size[DLM_INTENT_REC_OFF] = sizeof(struct mdt_rec_unlink);
373                 size[DLM_INTENT_REC_OFF + 1] = op_data->op_capa1 ?
374                                                sizeof(struct lustre_capa) : 0;
375                 size[DLM_INTENT_REC_OFF + 2] = op_data->op_namelen + 1;
376                 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
377                 req = ldlm_prep_enqueue_req(exp, 6, size, NULL, 0);
378                 if (!req)
379                         RETURN(-ENOMEM);
380
381                 /* pack the intent */
382                 lit = lustre_msg_buf(req->rq_reqmsg, DLM_INTENT_IT_OFF,
383                                      sizeof(*lit));
384                 lit->opc = (__u64)it->it_op;
385
386                 /* pack the intended request */
387                 mdc_unlink_pack(req, DLM_INTENT_REC_OFF, op_data);
388
389                 repsize[repbufcnt++] = obddev->u.cli.cl_max_mds_cookiesize;
390         } else if (it->it_op & (IT_GETATTR | IT_LOOKUP)) {
391                 obd_valid valid = OBD_MD_FLGETATTR | OBD_MD_FLEASIZE |
392                         OBD_MD_FLMODEASIZE | OBD_MD_FLDIREA |
393                         OBD_MD_FLMDSCAPA | OBD_MD_MEA;
394                 valid |= client_is_remote(exp) ? OBD_MD_FLRMTPERM :
395                                                  OBD_MD_FLACL;
396                 size[DLM_INTENT_REC_OFF] = sizeof(struct mdt_body);
397                 size[DLM_INTENT_REC_OFF + 1] = op_data->op_capa1 ?
398                                                sizeof(struct lustre_capa) : 0;
399                 size[DLM_INTENT_REC_OFF + 2] = op_data->op_namelen + 1;
400
401                 if (it->it_op & IT_GETATTR)
402                         policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
403
404                 req = ldlm_prep_enqueue_req(exp, 6, size, NULL, 0);
405                 if (!req)
406                         RETURN(-ENOMEM);
407
408                 /* pack the intent */
409                 lit = lustre_msg_buf(req->rq_reqmsg, DLM_INTENT_IT_OFF,
410                                      sizeof(*lit));
411                 lit->opc = (__u64)it->it_op;
412
413                 /* pack the intended request */
414                 mdc_getattr_pack(req, DLM_INTENT_REC_OFF, valid,
415                                  it->it_flags, op_data);
416
417                 repsize[repbufcnt++] = client_is_remote(exp) ?
418                                        sizeof(struct mdt_remote_perm) :
419                                        LUSTRE_POSIX_ACL_MAX_SIZE;
420                 repsize[repbufcnt++] = sizeof(struct lustre_capa);
421         } else if (it->it_op == IT_READDIR) {
422                 policy.l_inodebits.bits = MDS_INODELOCK_UPDATE;
423                 req = ldlm_prep_enqueue_req(exp, 2, size, NULL, 0);
424                 if (!req)
425                         RETURN(-ENOMEM);
426
427                 repbufcnt = 2;
428         } else {
429                 LBUG();
430                 RETURN(-EINVAL);
431         }
432
433         /* get ready for the reply */
434         ptlrpc_req_set_repsize(req, repbufcnt, repsize);
435
436          /* It is important to obtain rpc_lock first (if applicable), so that
437           * threads that are serialised with rpc_lock are not polluting our
438           * rpcs in flight counter */
439         mdc_get_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
440         mdc_enter_request(&obddev->u.cli);
441         rc = ldlm_cli_enqueue(exp, &req, &res_id, lock_type, &policy,
442                               lock_mode, &flags, cb_blocking, cb_completion,
443                               NULL, cb_data, NULL, 0, NULL, lockh, 0);
444         mdc_exit_request(&obddev->u.cli);
445         mdc_put_rpc_lock(obddev->u.cli.cl_rpc_lock, it);
446
447         /* Similarly, if we're going to replay this request, we don't want to
448          * actually get a lock, just perform the intent. */
449         if (req->rq_transno || req->rq_replay) {
450                 lockreq = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF,
451                                          sizeof(*lockreq));
452                 lockreq->lock_flags |= LDLM_FL_INTENT_ONLY;
453         }
454
455         /* This can go when we're sure that this can never happen */
456         LASSERT(rc != -ENOENT);
457         if (rc == ELDLM_LOCK_ABORTED) {
458                 lock_mode = 0;
459                 memset(lockh, 0, sizeof(*lockh));
460                 rc = 0;
461         } else if (rc != 0) {
462                 CERROR("ldlm_cli_enqueue: %d\n", rc);
463                 LASSERTF(rc < 0, "rc %d\n", rc);
464                 mdc_clear_replay_flag(req, rc);
465                 ptlrpc_req_finished(req);
466                 RETURN(rc);
467         } else { /* rc = 0 */
468                 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
469                 LASSERT(lock);
470
471                 /* If the server gave us back a different lock mode, we should
472                  * fix up our variables. */
473                 if (lock->l_req_mode != lock_mode) {
474                         ldlm_lock_addref(lockh, lock->l_req_mode);
475                         ldlm_lock_decref(lockh, lock_mode);
476                         lock_mode = lock->l_req_mode;
477                 }
478                 LDLM_LOCK_PUT(lock);
479         }
480
481         lockrep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
482                                  sizeof(*lockrep));
483         LASSERT(lockrep != NULL);                 /* checked by ldlm_cli_enqueue() */
484         LASSERT_REPSWABBED(req, DLM_LOCKREPLY_OFF); /* swabbed by ldlm_cli_enqueue() */
485
486         it->d.lustre.it_disposition = (int)lockrep->lock_policy_res1;
487         it->d.lustre.it_status = (int)lockrep->lock_policy_res2;
488         it->d.lustre.it_lock_mode = lock_mode;
489         it->d.lustre.it_data = req;
490
491         if (it->d.lustre.it_status < 0 && req->rq_replay)
492                 mdc_clear_replay_flag(req, it->d.lustre.it_status);
493
494         /* If we're doing an IT_OPEN which did not result in an actual
495          * successful open, then we need to remove the bit which saves
496          * this request for unconditional replay.
497          *
498          * It's important that we do this first!  Otherwise we might exit the
499          * function without doing so, and try to replay a failed create
500          * (bug 3440) */
501         if (it->it_op & IT_OPEN && req->rq_replay &&
502             (!it_disposition(it, DISP_OPEN_OPEN) ||it->d.lustre.it_status != 0))
503                 mdc_clear_replay_flag(req, it->d.lustre.it_status);
504
505         DEBUG_REQ(D_RPCTRACE, req, "op: %d disposition: %x, status: %d",
506                   it->it_op,it->d.lustre.it_disposition,it->d.lustre.it_status);
507
508         /* We know what to expect, so we do any byte flipping required here */
509         LASSERT(repbufcnt == 7 || repbufcnt == 6 || repbufcnt == 2);
510         if (repbufcnt >= 6) {
511                 int reply_off = DLM_REPLY_REC_OFF;
512                 struct mdt_body *body;
513
514                 body = lustre_swab_repbuf(req, reply_off++, sizeof(*body),
515                                          lustre_swab_mdt_body);
516                 if (body == NULL) {
517                         CERROR ("Can't swab mdt_body\n");
518                         RETURN (-EPROTO);
519                 }
520
521                 if (req->rq_replay && it_disposition(it, DISP_OPEN_OPEN) &&
522                     !it_open_error(DISP_OPEN_OPEN, it)) {
523                         /*
524                          * If this is a successful OPEN request, we need to set
525                          * replay handler and data early, so that if replay
526                          * happens immediately after swabbing below, new reply
527                          * is swabbed by that handler correctly.
528                          */
529                         mdc_set_open_replay_data(NULL, NULL, req);
530                 }
531
532                 if ((body->valid & (OBD_MD_FLDIREA | OBD_MD_FLEASIZE)) != 0) {
533                         void *eadata;
534
535                         /*
536                          * The eadata is opaque; just check that it is there.
537                          * Eventually, obd_unpackmd() will check the contents.
538                          */
539                         eadata = lustre_swab_repbuf(req, reply_off++,
540                                                     body->eadatasize, NULL);
541                         if (eadata == NULL) {
542                                 CERROR("Missing/short eadata\n");
543                                 RETURN(-EPROTO);
544                         }
545                         if (body->valid & OBD_MD_FLMODEASIZE) {
546                                 if (obddev->u.cli.cl_max_mds_easize <
547                                     body->max_mdsize) {
548                                         obddev->u.cli.cl_max_mds_easize =
549                                                 body->max_mdsize;
550                                         CDEBUG(D_INFO, "maxeasize become %d\n",
551                                                body->max_mdsize);
552                                 }
553                                 if (obddev->u.cli.cl_max_mds_cookiesize <
554                                     body->max_cookiesize) {
555                                         obddev->u.cli.cl_max_mds_cookiesize =
556                                                 body->max_cookiesize;
557                                         CDEBUG(D_INFO, "cookiesize become %d\n",
558                                                body->max_cookiesize);
559                                 }
560                         }
561
562                         /*
563                          * We save the reply LOV EA in case we have to replay a
564                          * create for recovery.  If we didn't allocate a large
565                          * enough request buffer above we need to reallocate it
566                          * here to hold the actual LOV EA.
567                          *
568                          * To not save LOV EA if request is not going to replay
569                          * (for example error one).
570                          */
571                         if ((it->it_op & IT_OPEN) && req->rq_replay) {
572                                 if (lustre_msg_buflen(req->rq_reqmsg,
573                                                       DLM_INTENT_REC_OFF + 4) <
574                                     body->eadatasize)
575                                         mdc_realloc_openmsg(req, body, size);
576
577                                 lmm = lustre_msg_buf(req->rq_reqmsg,
578                                                      DLM_INTENT_REC_OFF + 4,
579                                                      body->eadatasize);
580                                 if (lmm)
581                                         memcpy(lmm, eadata, body->eadatasize);
582                         }
583                 }
584                 if (body->valid & OBD_MD_FLRMTPERM) {
585                         struct mdt_remote_perm *perm;
586
587                         LASSERT(client_is_remote(exp));
588                         perm = lustre_swab_repbuf(req, reply_off++,
589                                                   sizeof(*perm),
590                                                   lustre_swab_mdt_remote_perm);
591                         if (perm == NULL) {
592                                 CERROR("missing remote permission!\n");
593                                 RETURN(-EPROTO);
594                         }
595                 } else if ((body->valid & OBD_MD_FLACL) && body->aclsize) {
596                         reply_off++;
597                 }
598                 if (body->valid & OBD_MD_FLMDSCAPA) {
599                         struct lustre_capa *capa, *p;
600
601                         capa = lustre_unpack_capa(req->rq_repmsg, reply_off++);
602                         if (capa == NULL) {
603                                 CERROR("Missing/short MDS capability\n");
604                                 RETURN(-EPROTO);
605                         }
606
607                         if (it->it_op & IT_OPEN) {
608                                 /* client fid capa will be checked in replay */
609                                 p = lustre_msg_buf(req->rq_reqmsg,
610                                                    DLM_INTENT_REC_OFF + 2,
611                                                    sizeof(*p));
612                                 LASSERT(p);
613                                 *p = *capa;
614                         }
615                 }
616                 if (body->valid & OBD_MD_FLOSSCAPA) {
617                         struct lustre_capa *capa;
618
619                         capa = lustre_unpack_capa(req->rq_repmsg, reply_off++);
620                         if (capa == NULL) {
621                                 CERROR("Missing/short OSS capability\n");
622                                 RETURN(-EPROTO);
623                         }
624                 }
625         }
626
627         RETURN(rc);
628 }
629 /*
630  * This long block is all about fixing up the lock and request state
631  * so that it is correct as of the moment _before_ the operation was
632  * applied; that way, the VFS will think that everything is normal and
633  * call Lustre's regular VFS methods.
634  *
635  * If we're performing a creation, that means that unless the creation
636  * failed with EEXIST, we should fake up a negative dentry.
637  *
638  * For everything else, we want to lookup to succeed.
639  *
640  * One additional note: if CREATE or OPEN succeeded, we add an extra
641  * reference to the request because we need to keep it around until
642  * ll_create/ll_open gets called.
643  *
644  * The server will return to us, in it_disposition, an indication of
645  * exactly what d.lustre.it_status refers to.
646  *
647  * If DISP_OPEN_OPEN is set, then d.lustre.it_status refers to the open() call,
648  * otherwise if DISP_OPEN_CREATE is set, then it status is the
649  * creation failure mode.  In either case, one of DISP_LOOKUP_NEG or
650  * DISP_LOOKUP_POS will be set, indicating whether the child lookup
651  * was successful.
652  *
653  * Else, if DISP_LOOKUP_EXECD then d.lustre.it_status is the rc of the
654  * child lookup.
655  */
656 int mdc_intent_lock(struct obd_export *exp, struct md_op_data *op_data,
657                     void *lmm, int lmmsize, struct lookup_intent *it,
658                     int lookup_flags, struct ptlrpc_request **reqp,
659                     ldlm_blocking_callback cb_blocking,
660                     int extra_lock_flags)
661 {
662         struct ptlrpc_request *request;
663         struct lustre_handle old_lock;
664         struct lustre_handle lockh;
665         struct mdt_body *mdt_body;
666         struct ldlm_lock *lock;
667         int rc = 0;
668         ENTRY;
669         LASSERT(it);
670
671         CDEBUG(D_DLMTRACE, "(name: %.*s,"DFID") in obj "DFID
672                ", intent: %s flags %#o\n", op_data->op_namelen,
673                op_data->op_name, PFID(&op_data->op_fid2),
674                PFID(&op_data->op_fid1), ldlm_it2str(it->it_op),
675                it->it_flags);
676
677         if (fid_is_sane(&op_data->op_fid2) &&
678             (it->it_op & (IT_LOOKUP | IT_GETATTR))) {
679                 /* We could just return 1 immediately, but since we should only
680                  * be called in revalidate_it if we already have a lock, let's
681                  * verify that. */
682                 struct ldlm_res_id res_id = { .name = { fid_seq(&op_data->op_fid2),
683                                                         fid_oid(&op_data->op_fid2),
684                                                         fid_ver(&op_data->op_fid2) } };
685                 ldlm_policy_data_t policy;
686                 ldlm_mode_t mode = LCK_CR;
687
688                 /* As not all attributes are kept under update lock, e.g.
689                    owner/group/acls are under lookup lock, we need both
690                    ibits for GETATTR. */
691
692                 /* For CMD, UPDATE lock and LOOKUP lock can not be got
693                  * at the same for cross-object, so we can not match
694                  * the 2 lock at the same time FIXME: but how to handle
695                  * the above situation */
696                 policy.l_inodebits.bits = (it->it_op == IT_GETATTR) ?
697                         MDS_INODELOCK_UPDATE : MDS_INODELOCK_LOOKUP;
698
699                 rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
700                                      LDLM_FL_BLOCK_GRANTED, &res_id,
701                                      LDLM_IBITS, &policy, mode, &lockh);
702                 if (!rc) {
703                         mode = LCK_CW;
704                         rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
705                                              LDLM_FL_BLOCK_GRANTED, &res_id,
706                                              LDLM_IBITS, &policy, mode, &lockh);
707                 }
708                 if (!rc) {
709                         mode = LCK_PR;
710                         rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
711                                              LDLM_FL_BLOCK_GRANTED, &res_id,
712                                              LDLM_IBITS, &policy, mode, &lockh);
713                 }
714
715                 if (!rc) {
716                         mode = LCK_PW;
717                         rc = ldlm_lock_match(exp->exp_obd->obd_namespace,
718                                              LDLM_FL_BLOCK_GRANTED, &res_id,
719                                              LDLM_IBITS, &policy, mode, &lockh);
720                 }
721
722                 if (rc) {
723                         memcpy(&it->d.lustre.it_lock_handle, &lockh,
724                                sizeof(lockh));
725                         it->d.lustre.it_lock_mode = mode;
726                 }
727
728                 /* Only return failure if it was not GETATTR by cfid
729                    (from inode_revalidate) */
730                 if (rc || op_data->op_namelen != 0)
731                         RETURN(rc);
732         }
733
734         /* lookup_it may be called only after revalidate_it has run, because
735          * revalidate_it cannot return errors, only zero.  Returning zero causes
736          * this call to lookup, which *can* return an error.
737          *
738          * We only want to execute the request associated with the intent one
739          * time, however, so don't send the request again.  Instead, skip past
740          * this and use the request from revalidate.  In this case, revalidate
741          * never dropped its reference, so the refcounts are all OK */
742         if (!it_disposition(it, DISP_ENQ_COMPLETE)) {
743                 /* For case if upper layer did not alloc fid, do it now. */
744                 if (!fid_is_sane(&op_data->op_fid2) && it->it_op & IT_CREAT) {
745                         rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
746                         if (rc < 0) {
747                                 CERROR("Can't alloc new fid, rc %d\n", rc);
748                                 RETURN(rc);
749                         }
750                 }
751                 
752                 rc = mdc_enqueue(exp, LDLM_IBITS, it, it_to_lock_mode(it),
753                                  op_data, &lockh, lmm, lmmsize,
754                                  ldlm_completion_ast, cb_blocking, NULL,
755                                  extra_lock_flags);
756                 if (rc < 0)
757                         RETURN(rc);
758                 memcpy(&it->d.lustre.it_lock_handle, &lockh, sizeof(lockh));
759         } else if (!fid_is_sane(&op_data->op_fid2) ||
760                    !(it->it_flags & O_CHECK_STALE)) {
761                 /* DISP_ENQ_COMPLETE set means there is extra reference on
762                  * request referenced from this intent, saved for subsequent
763                  * lookup.  This path is executed when we proceed to this
764                  * lookup, so we clear DISP_ENQ_COMPLETE */
765                 it_clear_disposition(it, DISP_ENQ_COMPLETE);
766         }
767         request = *reqp = it->d.lustre.it_data;
768         LASSERT(request != NULL);
769         LASSERT(request != LP_POISON);
770         LASSERT(request->rq_repmsg != LP_POISON);
771
772         if (!it_disposition(it, DISP_IT_EXECD)) {
773                 /* The server failed before it even started executing the
774                  * intent, i.e. because it couldn't unpack the request. */
775                 LASSERT(it->d.lustre.it_status != 0);
776                 RETURN(it->d.lustre.it_status);
777         }
778         rc = it_open_error(DISP_IT_EXECD, it);
779         if (rc)
780                 RETURN(rc);
781
782         mdt_body = lustre_msg_buf(request->rq_repmsg, DLM_REPLY_REC_OFF,
783                                   sizeof(*mdt_body));
784         LASSERT(mdt_body != NULL);      /* mdc_enqueue checked */
785         LASSERT_REPSWABBED(request, 1); /* mdc_enqueue swabbed */
786
787         /* If we were revalidating a fid/name pair, mark the intent in
788          * case we fail and get called again from lookup */
789         if (fid_is_sane(&op_data->op_fid2) && (it->it_flags & O_CHECK_STALE) &&
790             (it->it_op != IT_GETATTR)) {
791                 it_set_disposition(it, DISP_ENQ_COMPLETE);
792
793                 /* Also: did we find the same inode? */
794                 if (!lu_fid_eq(&op_data->op_fid2, &mdt_body->fid1))
795                         RETURN(-ESTALE);
796         }
797
798         rc = it_open_error(DISP_LOOKUP_EXECD, it);
799         if (rc)
800                 RETURN(rc);
801
802         /* keep requests around for the multiple phases of the call
803          * this shows the DISP_XX must guarantee we make it into the call
804          */
805         if (!it_disposition(it, DISP_ENQ_CREATE_REF) &&
806             it_disposition(it, DISP_OPEN_CREATE) &&
807             !it_open_error(DISP_OPEN_CREATE, it)) {
808                 it_set_disposition(it, DISP_ENQ_CREATE_REF);
809                 ptlrpc_request_addref(request); /* balanced in ll_create_node */
810         }
811         if (!it_disposition(it, DISP_ENQ_OPEN_REF) &&
812             it_disposition(it, DISP_OPEN_OPEN) &&
813             !it_open_error(DISP_OPEN_OPEN, it)) {
814                 it_set_disposition(it, DISP_ENQ_OPEN_REF);
815                 ptlrpc_request_addref(request); /* balanced in ll_file_open */
816                 /* BUG 11546 - eviction in the middle of open rpc processing */
817                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDC_ENQUEUE_PAUSE, obd_timeout);
818         }
819
820         if (it->it_op & IT_CREAT) {
821                 /* XXX this belongs in ll_create_it */
822         } else if (it->it_op == IT_OPEN) {
823                 LASSERT(!it_disposition(it, DISP_OPEN_CREATE));
824         } else {
825                 LASSERT(it->it_op & (IT_GETATTR | IT_LOOKUP));
826         }
827
828         /* If we already have a matching lock, then cancel the new
829          * one.  We have to set the data here instead of in
830          * mdc_enqueue, because we need to use the child's inode as
831          * the l_ast_data to match, and that's not available until
832          * intent_finish has performed the iget().) */
833         lock = ldlm_handle2lock(&lockh);
834         if (lock) {
835                 ldlm_policy_data_t policy = lock->l_policy_data;
836                 LDLM_DEBUG(lock, "matching against this");
837
838                 LASSERTF(fid_res_name_eq(&mdt_body->fid1,
839                                          &lock->l_resource->lr_name),
840                          "Lock res_id: %lu/%lu/%lu, fid: %lu/%lu/%lu.\n",
841                          (unsigned long)lock->l_resource->lr_name.name[0],
842                          (unsigned long)lock->l_resource->lr_name.name[1],
843                          (unsigned long)lock->l_resource->lr_name.name[2],
844                          (unsigned long)fid_seq(&mdt_body->fid1),
845                          (unsigned long)fid_oid(&mdt_body->fid1),
846                          (unsigned long)fid_ver(&mdt_body->fid1));
847                 LDLM_LOCK_PUT(lock);
848
849                 memcpy(&old_lock, &lockh, sizeof(lockh));
850                 if (ldlm_lock_match(NULL, LDLM_FL_BLOCK_GRANTED, NULL,
851                                     LDLM_IBITS, &policy, LCK_NL, &old_lock)) {
852                         ldlm_lock_decref_and_cancel(&lockh,
853                                                     it->d.lustre.it_lock_mode);
854                         memcpy(&lockh, &old_lock, sizeof(old_lock));
855                         memcpy(&it->d.lustre.it_lock_handle, &lockh,
856                                sizeof(lockh));
857                 }
858         }
859         CDEBUG(D_DENTRY,"D_IT dentry %.*s intent: %s status %d disp %x rc %d\n",
860                op_data->op_namelen, op_data->op_name, ldlm_it2str(it->it_op),
861                it->d.lustre.it_status, it->d.lustre.it_disposition, rc);
862
863         RETURN(rc);
864 }