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