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