fce8567aba38b885c4a007e295be6ac0bbbec7cc
[firefly-linux-kernel-4.4.55.git] / drivers / staging / lustre / lustre / mdc / mdc_request.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_MDC
38
39 # include <linux/module.h>
40 # include <linux/pagemap.h>
41 # include <linux/miscdevice.h>
42 # include <linux/init.h>
43 # include <linux/utsname.h>
44
45 #include "../include/lustre_acl.h"
46 #include "../include/obd_class.h"
47 #include "../include/lustre_fid.h"
48 #include "../include/lprocfs_status.h"
49 #include "../include/lustre_param.h"
50 #include "../include/lustre_log.h"
51
52 #include "mdc_internal.h"
53
54 #define REQUEST_MINOR 244
55
56 struct mdc_renew_capa_args {
57         struct obd_capa *ra_oc;
58         renew_capa_cb_t  ra_cb;
59 };
60
61 static int mdc_cleanup(struct obd_device *obd);
62
63 int mdc_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
64                     const struct req_msg_field *field, struct obd_capa **oc)
65 {
66         struct lustre_capa *capa;
67         struct obd_capa *c;
68
69         /* swabbed already in mdc_enqueue */
70         capa = req_capsule_server_get(&req->rq_pill, field);
71         if (capa == NULL)
72                 return -EPROTO;
73
74         c = alloc_capa(CAPA_SITE_CLIENT);
75         if (IS_ERR(c)) {
76                 CDEBUG(D_INFO, "alloc capa failed!\n");
77                 return PTR_ERR(c);
78         } else {
79                 c->c_capa = *capa;
80                 *oc = c;
81                 return 0;
82         }
83 }
84
85 static inline int mdc_queue_wait(struct ptlrpc_request *req)
86 {
87         struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
88         int rc;
89
90         /* mdc_enter_request() ensures that this client has no more
91          * than cl_max_rpcs_in_flight RPCs simultaneously inf light
92          * against an MDT. */
93         rc = mdc_enter_request(cli);
94         if (rc != 0)
95                 return rc;
96
97         rc = ptlrpc_queue_wait(req);
98         mdc_exit_request(cli);
99
100         return rc;
101 }
102
103 /* Helper that implements most of mdc_getstatus and signal_completed_replay. */
104 /* XXX this should become mdc_get_info("key"), sending MDS_GET_INFO RPC */
105 static int send_getstatus(struct obd_import *imp, struct lu_fid *rootfid,
106                           struct obd_capa **pc, int level, int msg_flags)
107 {
108         struct ptlrpc_request *req;
109         struct mdt_body       *body;
110         int                 rc;
111
112         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_GETSTATUS,
113                                         LUSTRE_MDS_VERSION, MDS_GETSTATUS);
114         if (req == NULL)
115                 return -ENOMEM;
116
117         mdc_pack_body(req, NULL, NULL, 0, 0, -1, 0);
118         lustre_msg_add_flags(req->rq_reqmsg, msg_flags);
119         req->rq_send_state = level;
120
121         ptlrpc_request_set_replen(req);
122
123         rc = ptlrpc_queue_wait(req);
124         if (rc)
125                 GOTO(out, rc);
126
127         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
128         if (body == NULL)
129                 GOTO(out, rc = -EPROTO);
130
131         if (body->valid & OBD_MD_FLMDSCAPA) {
132                 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, pc);
133                 if (rc)
134                         GOTO(out, rc);
135         }
136
137         *rootfid = body->fid1;
138         CDEBUG(D_NET,
139                "root fid="DFID", last_committed=%llu\n",
140                PFID(rootfid),
141                lustre_msg_get_last_committed(req->rq_repmsg));
142 out:
143         ptlrpc_req_finished(req);
144         return rc;
145 }
146
147 /* This should be mdc_get_info("rootfid") */
148 int mdc_getstatus(struct obd_export *exp, struct lu_fid *rootfid,
149                   struct obd_capa **pc)
150 {
151         return send_getstatus(class_exp2cliimp(exp), rootfid, pc,
152                               LUSTRE_IMP_FULL, 0);
153 }
154
155 /*
156  * This function now is known to always saying that it will receive 4 buffers
157  * from server. Even for cases when acl_size and md_size is zero, RPC header
158  * will contain 4 fields and RPC itself will contain zero size fields. This is
159  * because mdt_getattr*() _always_ returns 4 fields, but if acl is not needed
160  * and thus zero, it shrinks it, making zero size. The same story about
161  * md_size. And this is course of problem when client waits for smaller number
162  * of fields. This issue will be fixed later when client gets aware of RPC
163  * layouts.  --umka
164  */
165 static int mdc_getattr_common(struct obd_export *exp,
166                               struct ptlrpc_request *req)
167 {
168         struct req_capsule *pill = &req->rq_pill;
169         struct mdt_body    *body;
170         void           *eadata;
171         int              rc;
172
173         /* Request message already built. */
174         rc = ptlrpc_queue_wait(req);
175         if (rc != 0)
176                 return rc;
177
178         /* sanity check for the reply */
179         body = req_capsule_server_get(pill, &RMF_MDT_BODY);
180         if (body == NULL)
181                 return -EPROTO;
182
183         CDEBUG(D_NET, "mode: %o\n", body->mode);
184
185         if (body->eadatasize != 0) {
186                 mdc_update_max_ea_from_body(exp, body);
187
188                 eadata = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
189                                                       body->eadatasize);
190                 if (eadata == NULL)
191                         return -EPROTO;
192         }
193
194         if (body->valid & OBD_MD_FLRMTPERM) {
195                 struct mdt_remote_perm *perm;
196
197                 LASSERT(client_is_remote(exp));
198                 perm = req_capsule_server_swab_get(pill, &RMF_ACL,
199                                                 lustre_swab_mdt_remote_perm);
200                 if (perm == NULL)
201                         return -EPROTO;
202         }
203
204         if (body->valid & OBD_MD_FLMDSCAPA) {
205                 struct lustre_capa *capa;
206
207                 capa = req_capsule_server_get(pill, &RMF_CAPA1);
208                 if (capa == NULL)
209                         return -EPROTO;
210         }
211
212         return 0;
213 }
214
215 int mdc_getattr(struct obd_export *exp, struct md_op_data *op_data,
216                 struct ptlrpc_request **request)
217 {
218         struct ptlrpc_request *req;
219         int                 rc;
220
221         /* Single MDS without an LMV case */
222         if (op_data->op_flags & MF_GET_MDT_IDX) {
223                 op_data->op_mds = 0;
224                 return 0;
225         }
226         *request = NULL;
227         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
228         if (req == NULL)
229                 return -ENOMEM;
230
231         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
232
233         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
234         if (rc) {
235                 ptlrpc_request_free(req);
236                 return rc;
237         }
238
239         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
240                       op_data->op_valid, op_data->op_mode, -1, 0);
241
242         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
243                              op_data->op_mode);
244         if (op_data->op_valid & OBD_MD_FLRMTPERM) {
245                 LASSERT(client_is_remote(exp));
246                 req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
247                                      sizeof(struct mdt_remote_perm));
248         }
249         ptlrpc_request_set_replen(req);
250
251         rc = mdc_getattr_common(exp, req);
252         if (rc)
253                 ptlrpc_req_finished(req);
254         else
255                 *request = req;
256         return rc;
257 }
258
259 int mdc_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
260                      struct ptlrpc_request **request)
261 {
262         struct ptlrpc_request *req;
263         int                 rc;
264
265         *request = NULL;
266         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
267                                    &RQF_MDS_GETATTR_NAME);
268         if (req == NULL)
269                 return -ENOMEM;
270
271         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
272         req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
273                              op_data->op_namelen + 1);
274
275         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR_NAME);
276         if (rc) {
277                 ptlrpc_request_free(req);
278                 return rc;
279         }
280
281         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
282                       op_data->op_valid, op_data->op_mode,
283                       op_data->op_suppgids[0], 0);
284
285         if (op_data->op_name) {
286                 char *name = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
287
288                 LASSERT(strnlen(op_data->op_name, op_data->op_namelen) ==
289                                 op_data->op_namelen);
290                 memcpy(name, op_data->op_name, op_data->op_namelen);
291         }
292
293         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
294                              op_data->op_mode);
295         ptlrpc_request_set_replen(req);
296
297         rc = mdc_getattr_common(exp, req);
298         if (rc)
299                 ptlrpc_req_finished(req);
300         else
301                 *request = req;
302         return rc;
303 }
304
305 static int mdc_is_subdir(struct obd_export *exp,
306                          const struct lu_fid *pfid,
307                          const struct lu_fid *cfid,
308                          struct ptlrpc_request **request)
309 {
310         struct ptlrpc_request  *req;
311         int                  rc;
312
313         *request = NULL;
314         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
315                                         &RQF_MDS_IS_SUBDIR, LUSTRE_MDS_VERSION,
316                                         MDS_IS_SUBDIR);
317         if (req == NULL)
318                 return -ENOMEM;
319
320         mdc_is_subdir_pack(req, pfid, cfid, 0);
321         ptlrpc_request_set_replen(req);
322
323         rc = ptlrpc_queue_wait(req);
324         if (rc && rc != -EREMOTE)
325                 ptlrpc_req_finished(req);
326         else
327                 *request = req;
328         return rc;
329 }
330
331 static int mdc_xattr_common(struct obd_export *exp,
332                             const struct req_format *fmt,
333                             const struct lu_fid *fid,
334                             struct obd_capa *oc, int opcode, u64 valid,
335                             const char *xattr_name, const char *input,
336                             int input_size, int output_size, int flags,
337                             __u32 suppgid, struct ptlrpc_request **request)
338 {
339         struct ptlrpc_request *req;
340         int   xattr_namelen = 0;
341         char *tmp;
342         int   rc;
343
344         *request = NULL;
345         req = ptlrpc_request_alloc(class_exp2cliimp(exp), fmt);
346         if (req == NULL)
347                 return -ENOMEM;
348
349         mdc_set_capa_size(req, &RMF_CAPA1, oc);
350         if (xattr_name) {
351                 xattr_namelen = strlen(xattr_name) + 1;
352                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
353                                      xattr_namelen);
354         }
355         if (input_size) {
356                 LASSERT(input);
357                 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_CLIENT,
358                                      input_size);
359         }
360
361         /* Flush local XATTR locks to get rid of a possible cancel RPC */
362         if (opcode == MDS_REINT && fid_is_sane(fid) &&
363             exp->exp_connect_data.ocd_ibits_known & MDS_INODELOCK_XATTR) {
364                 LIST_HEAD(cancels);
365                 int count;
366
367                 /* Without that packing would fail */
368                 if (input_size == 0)
369                         req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
370                                              RCL_CLIENT, 0);
371
372                 count = mdc_resource_get_unused(exp, fid,
373                                                 &cancels, LCK_EX,
374                                                 MDS_INODELOCK_XATTR);
375
376                 rc = mdc_prep_elc_req(exp, req, MDS_REINT, &cancels, count);
377                 if (rc) {
378                         ptlrpc_request_free(req);
379                         return rc;
380                 }
381         } else {
382                 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, opcode);
383                 if (rc) {
384                         ptlrpc_request_free(req);
385                         return rc;
386                 }
387         }
388
389         if (opcode == MDS_REINT) {
390                 struct mdt_rec_setxattr *rec;
391
392                 CLASSERT(sizeof(struct mdt_rec_setxattr) ==
393                          sizeof(struct mdt_rec_reint));
394                 rec = req_capsule_client_get(&req->rq_pill, &RMF_REC_REINT);
395                 rec->sx_opcode = REINT_SETXATTR;
396                 rec->sx_fsuid  = from_kuid(&init_user_ns, current_fsuid());
397                 rec->sx_fsgid  = from_kgid(&init_user_ns, current_fsgid());
398                 rec->sx_cap    = cfs_curproc_cap_pack();
399                 rec->sx_suppgid1 = suppgid;
400                 rec->sx_suppgid2 = -1;
401                 rec->sx_fid    = *fid;
402                 rec->sx_valid  = valid | OBD_MD_FLCTIME;
403                 rec->sx_time   = get_seconds();
404                 rec->sx_size   = output_size;
405                 rec->sx_flags  = flags;
406
407                 mdc_pack_capa(req, &RMF_CAPA1, oc);
408         } else {
409                 mdc_pack_body(req, fid, oc, valid, output_size, suppgid, flags);
410         }
411
412         if (xattr_name) {
413                 tmp = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
414                 memcpy(tmp, xattr_name, xattr_namelen);
415         }
416         if (input_size) {
417                 tmp = req_capsule_client_get(&req->rq_pill, &RMF_EADATA);
418                 memcpy(tmp, input, input_size);
419         }
420
421         if (req_capsule_has_field(&req->rq_pill, &RMF_EADATA, RCL_SERVER))
422                 req_capsule_set_size(&req->rq_pill, &RMF_EADATA,
423                                      RCL_SERVER, output_size);
424         ptlrpc_request_set_replen(req);
425
426         /* make rpc */
427         if (opcode == MDS_REINT)
428                 mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
429
430         rc = ptlrpc_queue_wait(req);
431
432         if (opcode == MDS_REINT)
433                 mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
434
435         if (rc)
436                 ptlrpc_req_finished(req);
437         else
438                 *request = req;
439         return rc;
440 }
441
442 int mdc_setxattr(struct obd_export *exp, const struct lu_fid *fid,
443                  struct obd_capa *oc, u64 valid, const char *xattr_name,
444                  const char *input, int input_size, int output_size,
445                  int flags, __u32 suppgid, struct ptlrpc_request **request)
446 {
447         return mdc_xattr_common(exp, &RQF_MDS_REINT_SETXATTR,
448                                 fid, oc, MDS_REINT, valid, xattr_name,
449                                 input, input_size, output_size, flags,
450                                 suppgid, request);
451 }
452
453 int mdc_getxattr(struct obd_export *exp, const struct lu_fid *fid,
454                  struct obd_capa *oc, u64 valid, const char *xattr_name,
455                  const char *input, int input_size, int output_size,
456                  int flags, struct ptlrpc_request **request)
457 {
458         return mdc_xattr_common(exp, &RQF_MDS_GETXATTR,
459                                 fid, oc, MDS_GETXATTR, valid, xattr_name,
460                                 input, input_size, output_size, flags,
461                                 -1, request);
462 }
463
464 #ifdef CONFIG_FS_POSIX_ACL
465 static int mdc_unpack_acl(struct ptlrpc_request *req, struct lustre_md *md)
466 {
467         struct req_capsule     *pill = &req->rq_pill;
468         struct mdt_body *body = md->body;
469         struct posix_acl       *acl;
470         void               *buf;
471         int                  rc;
472
473         if (!body->aclsize)
474                 return 0;
475
476         buf = req_capsule_server_sized_get(pill, &RMF_ACL, body->aclsize);
477
478         if (!buf)
479                 return -EPROTO;
480
481         acl = posix_acl_from_xattr(&init_user_ns, buf, body->aclsize);
482         if (IS_ERR(acl)) {
483                 rc = PTR_ERR(acl);
484                 CERROR("convert xattr to acl: %d\n", rc);
485                 return rc;
486         }
487
488         rc = posix_acl_valid(acl);
489         if (rc) {
490                 CERROR("validate acl: %d\n", rc);
491                 posix_acl_release(acl);
492                 return rc;
493         }
494
495         md->posix_acl = acl;
496         return 0;
497 }
498 #else
499 #define mdc_unpack_acl(req, md) 0
500 #endif
501
502 int mdc_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
503                       struct obd_export *dt_exp, struct obd_export *md_exp,
504                       struct lustre_md *md)
505 {
506         struct req_capsule *pill = &req->rq_pill;
507         int rc;
508
509         LASSERT(md);
510         memset(md, 0, sizeof(*md));
511
512         md->body = req_capsule_server_get(pill, &RMF_MDT_BODY);
513         LASSERT(md->body != NULL);
514
515         if (md->body->valid & OBD_MD_FLEASIZE) {
516                 int lmmsize;
517                 struct lov_mds_md *lmm;
518
519                 if (!S_ISREG(md->body->mode)) {
520                         CDEBUG(D_INFO,
521                                "OBD_MD_FLEASIZE set, should be a regular file, but is not\n");
522                         GOTO(out, rc = -EPROTO);
523                 }
524
525                 if (md->body->eadatasize == 0) {
526                         CDEBUG(D_INFO,
527                                "OBD_MD_FLEASIZE set, but eadatasize 0\n");
528                         GOTO(out, rc = -EPROTO);
529                 }
530                 lmmsize = md->body->eadatasize;
531                 lmm = req_capsule_server_sized_get(pill, &RMF_MDT_MD, lmmsize);
532                 if (!lmm)
533                         GOTO(out, rc = -EPROTO);
534
535                 rc = obd_unpackmd(dt_exp, &md->lsm, lmm, lmmsize);
536                 if (rc < 0)
537                         GOTO(out, rc);
538
539                 if (rc < sizeof(*md->lsm)) {
540                         CDEBUG(D_INFO,
541                                "lsm size too small: rc < sizeof (*md->lsm) (%d < %d)\n",
542                                rc, (int)sizeof(*md->lsm));
543                         GOTO(out, rc = -EPROTO);
544                 }
545
546         } else if (md->body->valid & OBD_MD_FLDIREA) {
547                 int lmvsize;
548                 struct lov_mds_md *lmv;
549
550                 if (!S_ISDIR(md->body->mode)) {
551                         CDEBUG(D_INFO,
552                                "OBD_MD_FLDIREA set, should be a directory, but is not\n");
553                         GOTO(out, rc = -EPROTO);
554                 }
555
556                 if (md->body->eadatasize == 0) {
557                         CDEBUG(D_INFO,
558                                "OBD_MD_FLDIREA is set, but eadatasize 0\n");
559                         return -EPROTO;
560                 }
561                 if (md->body->valid & OBD_MD_MEA) {
562                         lmvsize = md->body->eadatasize;
563                         lmv = req_capsule_server_sized_get(pill, &RMF_MDT_MD,
564                                                            lmvsize);
565                         if (!lmv)
566                                 GOTO(out, rc = -EPROTO);
567
568                         rc = obd_unpackmd(md_exp, (void *)&md->mea, lmv,
569                                           lmvsize);
570                         if (rc < 0)
571                                 GOTO(out, rc);
572
573                         if (rc < sizeof(*md->mea)) {
574                                 CDEBUG(D_INFO,
575                                        "size too small: rc < sizeof(*md->mea) (%d < %d)\n",
576                                         rc, (int)sizeof(*md->mea));
577                                 GOTO(out, rc = -EPROTO);
578                         }
579                 }
580         }
581         rc = 0;
582
583         if (md->body->valid & OBD_MD_FLRMTPERM) {
584                 /* remote permission */
585                 LASSERT(client_is_remote(exp));
586                 md->remote_perm = req_capsule_server_swab_get(pill, &RMF_ACL,
587                                                 lustre_swab_mdt_remote_perm);
588                 if (!md->remote_perm)
589                         GOTO(out, rc = -EPROTO);
590         } else if (md->body->valid & OBD_MD_FLACL) {
591                 /* for ACL, it's possible that FLACL is set but aclsize is zero.
592                  * only when aclsize != 0 there's an actual segment for ACL
593                  * in reply buffer.
594                  */
595                 if (md->body->aclsize) {
596                         rc = mdc_unpack_acl(req, md);
597                         if (rc)
598                                 GOTO(out, rc);
599 #ifdef CONFIG_FS_POSIX_ACL
600                 } else {
601                         md->posix_acl = NULL;
602 #endif
603                 }
604         }
605         if (md->body->valid & OBD_MD_FLMDSCAPA) {
606                 struct obd_capa *oc = NULL;
607
608                 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA1, &oc);
609                 if (rc)
610                         GOTO(out, rc);
611                 md->mds_capa = oc;
612         }
613
614         if (md->body->valid & OBD_MD_FLOSSCAPA) {
615                 struct obd_capa *oc = NULL;
616
617                 rc = mdc_unpack_capa(NULL, req, &RMF_CAPA2, &oc);
618                 if (rc)
619                         GOTO(out, rc);
620                 md->oss_capa = oc;
621         }
622
623 out:
624         if (rc) {
625                 if (md->oss_capa) {
626                         capa_put(md->oss_capa);
627                         md->oss_capa = NULL;
628                 }
629                 if (md->mds_capa) {
630                         capa_put(md->mds_capa);
631                         md->mds_capa = NULL;
632                 }
633 #ifdef CONFIG_FS_POSIX_ACL
634                 posix_acl_release(md->posix_acl);
635 #endif
636                 if (md->lsm)
637                         obd_free_memmd(dt_exp, &md->lsm);
638         }
639         return rc;
640 }
641
642 int mdc_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
643 {
644         return 0;
645 }
646
647 /**
648  * Handles both OPEN and SETATTR RPCs for OPEN-CLOSE and SETATTR-DONE_WRITING
649  * RPC chains.
650  */
651 void mdc_replay_open(struct ptlrpc_request *req)
652 {
653         struct md_open_data *mod = req->rq_cb_data;
654         struct ptlrpc_request *close_req;
655         struct obd_client_handle *och;
656         struct lustre_handle old;
657         struct mdt_body *body;
658
659         if (mod == NULL) {
660                 DEBUG_REQ(D_ERROR, req,
661                           "Can't properly replay without open data.");
662                 return;
663         }
664
665         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
666         LASSERT(body != NULL);
667
668         och = mod->mod_och;
669         if (och != NULL) {
670                 struct lustre_handle *file_fh;
671
672                 LASSERT(och->och_magic == OBD_CLIENT_HANDLE_MAGIC);
673
674                 file_fh = &och->och_fh;
675                 CDEBUG(D_HA, "updating handle from %#llx to %#llx\n",
676                        file_fh->cookie, body->handle.cookie);
677                 old = *file_fh;
678                 *file_fh = body->handle;
679         }
680         close_req = mod->mod_close_req;
681         if (close_req != NULL) {
682                 __u32 opc = lustre_msg_get_opc(close_req->rq_reqmsg);
683                 struct mdt_ioepoch *epoch;
684
685                 LASSERT(opc == MDS_CLOSE || opc == MDS_DONE_WRITING);
686                 epoch = req_capsule_client_get(&close_req->rq_pill,
687                                                &RMF_MDT_EPOCH);
688                 LASSERT(epoch);
689
690                 if (och != NULL)
691                         LASSERT(!memcmp(&old, &epoch->handle, sizeof(old)));
692                 DEBUG_REQ(D_HA, close_req, "updating close body with new fh");
693                 epoch->handle = body->handle;
694         }
695 }
696
697 void mdc_commit_open(struct ptlrpc_request *req)
698 {
699         struct md_open_data *mod = req->rq_cb_data;
700
701         if (mod == NULL)
702                 return;
703
704         /**
705          * No need to touch md_open_data::mod_och, it holds a reference on
706          * \var mod and will zero references to each other, \var mod will be
707          * freed after that when md_open_data::mod_och will put the reference.
708          */
709
710         /**
711          * Do not let open request to disappear as it still may be needed
712          * for close rpc to happen (it may happen on evict only, otherwise
713          * ptlrpc_request::rq_replay does not let mdc_commit_open() to be
714          * called), just mark this rpc as committed to distinguish these 2
715          * cases, see mdc_close() for details. The open request reference will
716          * be put along with freeing \var mod.
717          */
718         ptlrpc_request_addref(req);
719         spin_lock(&req->rq_lock);
720         req->rq_committed = 1;
721         spin_unlock(&req->rq_lock);
722         req->rq_cb_data = NULL;
723         obd_mod_put(mod);
724 }
725
726 int mdc_set_open_replay_data(struct obd_export *exp,
727                              struct obd_client_handle *och,
728                              struct lookup_intent *it)
729 {
730         struct md_open_data   *mod;
731         struct mdt_rec_create *rec;
732         struct mdt_body       *body;
733         struct ptlrpc_request *open_req = it->d.lustre.it_data;
734         struct obd_import     *imp = open_req->rq_import;
735
736         if (!open_req->rq_replay)
737                 return 0;
738
739         rec = req_capsule_client_get(&open_req->rq_pill, &RMF_REC_REINT);
740         body = req_capsule_server_get(&open_req->rq_pill, &RMF_MDT_BODY);
741         LASSERT(rec != NULL);
742         /* Incoming message in my byte order (it's been swabbed). */
743         /* Outgoing messages always in my byte order. */
744         LASSERT(body != NULL);
745
746         /* Only if the import is replayable, we set replay_open data */
747         if (och && imp->imp_replayable) {
748                 mod = obd_mod_alloc();
749                 if (mod == NULL) {
750                         DEBUG_REQ(D_ERROR, open_req,
751                                   "Can't allocate md_open_data");
752                         return 0;
753                 }
754
755                 /**
756                  * Take a reference on \var mod, to be freed on mdc_close().
757                  * It protects \var mod from being freed on eviction (commit
758                  * callback is called despite rq_replay flag).
759                  * Another reference for \var och.
760                  */
761                 obd_mod_get(mod);
762                 obd_mod_get(mod);
763
764                 spin_lock(&open_req->rq_lock);
765                 och->och_mod = mod;
766                 mod->mod_och = och;
767                 mod->mod_is_create = it_disposition(it, DISP_OPEN_CREATE) ||
768                                      it_disposition(it, DISP_OPEN_STRIPE);
769                 mod->mod_open_req = open_req;
770                 open_req->rq_cb_data = mod;
771                 open_req->rq_commit_cb = mdc_commit_open;
772                 spin_unlock(&open_req->rq_lock);
773         }
774
775         rec->cr_fid2 = body->fid1;
776         rec->cr_ioepoch = body->ioepoch;
777         rec->cr_old_handle.cookie = body->handle.cookie;
778         open_req->rq_replay_cb = mdc_replay_open;
779         if (!fid_is_sane(&body->fid1)) {
780                 DEBUG_REQ(D_ERROR, open_req,
781                           "Saving replay request with insane fid");
782                 LBUG();
783         }
784
785         DEBUG_REQ(D_RPCTRACE, open_req, "Set up open replay data");
786         return 0;
787 }
788
789 static void mdc_free_open(struct md_open_data *mod)
790 {
791         int committed = 0;
792
793         if (mod->mod_is_create == 0 &&
794             imp_connect_disp_stripe(mod->mod_open_req->rq_import))
795                 committed = 1;
796
797         LASSERT(mod->mod_open_req->rq_replay == 0);
798
799         DEBUG_REQ(D_RPCTRACE, mod->mod_open_req, "free open request\n");
800
801         ptlrpc_request_committed(mod->mod_open_req, committed);
802         if (mod->mod_close_req)
803                 ptlrpc_request_committed(mod->mod_close_req, committed);
804 }
805
806 int mdc_clear_open_replay_data(struct obd_export *exp,
807                                struct obd_client_handle *och)
808 {
809         struct md_open_data *mod = och->och_mod;
810
811         /**
812          * It is possible to not have \var mod in a case of eviction between
813          * lookup and ll_file_open().
814          **/
815         if (mod == NULL)
816                 return 0;
817
818         LASSERT(mod != LP_POISON);
819         LASSERT(mod->mod_open_req != NULL);
820         mdc_free_open(mod);
821
822         mod->mod_och = NULL;
823         och->och_mod = NULL;
824         obd_mod_put(mod);
825
826         return 0;
827 }
828
829 /* Prepares the request for the replay by the given reply */
830 static void mdc_close_handle_reply(struct ptlrpc_request *req,
831                                    struct md_op_data *op_data, int rc) {
832         struct mdt_body  *repbody;
833         struct mdt_ioepoch *epoch;
834
835         if (req && rc == -EAGAIN) {
836                 repbody = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
837                 epoch = req_capsule_client_get(&req->rq_pill, &RMF_MDT_EPOCH);
838
839                 epoch->flags |= MF_SOM_AU;
840                 if (repbody->valid & OBD_MD_FLGETATTRLOCK)
841                         op_data->op_flags |= MF_GETATTR_LOCK;
842         }
843 }
844
845 int mdc_close(struct obd_export *exp, struct md_op_data *op_data,
846               struct md_open_data *mod, struct ptlrpc_request **request)
847 {
848         struct obd_device     *obd = class_exp2obd(exp);
849         struct ptlrpc_request *req;
850         struct req_format     *req_fmt;
851         int                    rc;
852         int                    saved_rc = 0;
853
854
855         req_fmt = &RQF_MDS_CLOSE;
856         if (op_data->op_bias & MDS_HSM_RELEASE) {
857                 req_fmt = &RQF_MDS_RELEASE_CLOSE;
858
859                 /* allocate a FID for volatile file */
860                 rc = mdc_fid_alloc(exp, &op_data->op_fid2, op_data);
861                 if (rc < 0) {
862                         CERROR("%s: "DFID" failed to allocate FID: %d\n",
863                                obd->obd_name, PFID(&op_data->op_fid1), rc);
864                         /* save the errcode and proceed to close */
865                         saved_rc = rc;
866                 }
867         }
868
869         *request = NULL;
870         req = ptlrpc_request_alloc(class_exp2cliimp(exp), req_fmt);
871         if (req == NULL)
872                 return -ENOMEM;
873
874         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
875
876         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_CLOSE);
877         if (rc) {
878                 ptlrpc_request_free(req);
879                 return rc;
880         }
881
882         /* To avoid a livelock (bug 7034), we need to send CLOSE RPCs to a
883          * portal whose threads are not taking any DLM locks and are therefore
884          * always progressing */
885         req->rq_request_portal = MDS_READPAGE_PORTAL;
886         ptlrpc_at_set_req_timeout(req);
887
888         /* Ensure that this close's handle is fixed up during replay. */
889         if (likely(mod != NULL)) {
890                 LASSERTF(mod->mod_open_req != NULL &&
891                          mod->mod_open_req->rq_type != LI_POISON,
892                          "POISONED open %p!\n", mod->mod_open_req);
893
894                 mod->mod_close_req = req;
895
896                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched open");
897                 /* We no longer want to preserve this open for replay even
898                  * though the open was committed. b=3632, b=3633 */
899                 spin_lock(&mod->mod_open_req->rq_lock);
900                 mod->mod_open_req->rq_replay = 0;
901                 spin_unlock(&mod->mod_open_req->rq_lock);
902         } else {
903                  CDEBUG(D_HA,
904                         "couldn't find open req; expecting close error\n");
905         }
906
907         mdc_close_pack(req, op_data);
908
909         req_capsule_set_size(&req->rq_pill, &RMF_MDT_MD, RCL_SERVER,
910                              obd->u.cli.cl_default_mds_easize);
911         req_capsule_set_size(&req->rq_pill, &RMF_LOGCOOKIES, RCL_SERVER,
912                              obd->u.cli.cl_default_mds_cookiesize);
913
914         ptlrpc_request_set_replen(req);
915
916         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
917         rc = ptlrpc_queue_wait(req);
918         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
919
920         if (req->rq_repmsg == NULL) {
921                 CDEBUG(D_RPCTRACE, "request failed to send: %p, %d\n", req,
922                        req->rq_status);
923                 if (rc == 0)
924                         rc = req->rq_status ?: -EIO;
925         } else if (rc == 0 || rc == -EAGAIN) {
926                 struct mdt_body *body;
927
928                 rc = lustre_msg_get_status(req->rq_repmsg);
929                 if (lustre_msg_get_type(req->rq_repmsg) == PTL_RPC_MSG_ERR) {
930                         DEBUG_REQ(D_ERROR, req,
931                                   "type == PTL_RPC_MSG_ERR, err = %d", rc);
932                         if (rc > 0)
933                                 rc = -rc;
934                 }
935                 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
936                 if (body == NULL)
937                         rc = -EPROTO;
938         } else if (rc == -ESTALE) {
939                 /**
940                  * it can be allowed error after 3633 if open was committed and
941                  * server failed before close was sent. Let's check if mod
942                  * exists and return no error in that case
943                  */
944                 if (mod) {
945                         DEBUG_REQ(D_HA, req, "Reset ESTALE = %d", rc);
946                         LASSERT(mod->mod_open_req != NULL);
947                         if (mod->mod_open_req->rq_committed)
948                                 rc = 0;
949                 }
950         }
951
952         if (mod) {
953                 if (rc != 0)
954                         mod->mod_close_req = NULL;
955                 /* Since now, mod is accessed through open_req only,
956                  * thus close req does not keep a reference on mod anymore. */
957                 obd_mod_put(mod);
958         }
959         *request = req;
960         mdc_close_handle_reply(req, op_data, rc);
961         return rc < 0 ? rc : saved_rc;
962 }
963
964 int mdc_done_writing(struct obd_export *exp, struct md_op_data *op_data,
965                      struct md_open_data *mod)
966 {
967         struct obd_device     *obd = class_exp2obd(exp);
968         struct ptlrpc_request *req;
969         int                 rc;
970
971         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
972                                    &RQF_MDS_DONE_WRITING);
973         if (req == NULL)
974                 return -ENOMEM;
975
976         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
977         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_DONE_WRITING);
978         if (rc) {
979                 ptlrpc_request_free(req);
980                 return rc;
981         }
982
983         if (mod != NULL) {
984                 LASSERTF(mod->mod_open_req != NULL &&
985                          mod->mod_open_req->rq_type != LI_POISON,
986                          "POISONED setattr %p!\n", mod->mod_open_req);
987
988                 mod->mod_close_req = req;
989                 DEBUG_REQ(D_HA, mod->mod_open_req, "matched setattr");
990                 /* We no longer want to preserve this setattr for replay even
991                  * though the open was committed. b=3632, b=3633 */
992                 spin_lock(&mod->mod_open_req->rq_lock);
993                 mod->mod_open_req->rq_replay = 0;
994                 spin_unlock(&mod->mod_open_req->rq_lock);
995         }
996
997         mdc_close_pack(req, op_data);
998         ptlrpc_request_set_replen(req);
999
1000         mdc_get_rpc_lock(obd->u.cli.cl_close_lock, NULL);
1001         rc = ptlrpc_queue_wait(req);
1002         mdc_put_rpc_lock(obd->u.cli.cl_close_lock, NULL);
1003
1004         if (rc == -ESTALE) {
1005                 /**
1006                  * it can be allowed error after 3633 if open or setattr were
1007                  * committed and server failed before close was sent.
1008                  * Let's check if mod exists and return no error in that case
1009                  */
1010                 if (mod) {
1011                         LASSERT(mod->mod_open_req != NULL);
1012                         if (mod->mod_open_req->rq_committed)
1013                                 rc = 0;
1014                 }
1015         }
1016
1017         if (mod) {
1018                 if (rc != 0)
1019                         mod->mod_close_req = NULL;
1020                 LASSERT(mod->mod_open_req != NULL);
1021                 mdc_free_open(mod);
1022
1023                 /* Since now, mod is accessed through setattr req only,
1024                  * thus DW req does not keep a reference on mod anymore. */
1025                 obd_mod_put(mod);
1026         }
1027
1028         mdc_close_handle_reply(req, op_data, rc);
1029         ptlrpc_req_finished(req);
1030         return rc;
1031 }
1032
1033
1034 int mdc_readpage(struct obd_export *exp, struct md_op_data *op_data,
1035                  struct page **pages, struct ptlrpc_request **request)
1036 {
1037         struct ptlrpc_request   *req;
1038         struct ptlrpc_bulk_desc *desc;
1039         int                   i;
1040         wait_queue_head_t             waitq;
1041         int                   resends = 0;
1042         struct l_wait_info       lwi;
1043         int                   rc;
1044
1045         *request = NULL;
1046         init_waitqueue_head(&waitq);
1047
1048 restart_bulk:
1049         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_READPAGE);
1050         if (req == NULL)
1051                 return -ENOMEM;
1052
1053         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1054
1055         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_READPAGE);
1056         if (rc) {
1057                 ptlrpc_request_free(req);
1058                 return rc;
1059         }
1060
1061         req->rq_request_portal = MDS_READPAGE_PORTAL;
1062         ptlrpc_at_set_req_timeout(req);
1063
1064         desc = ptlrpc_prep_bulk_imp(req, op_data->op_npages, 1, BULK_PUT_SINK,
1065                                     MDS_BULK_PORTAL);
1066         if (desc == NULL) {
1067                 ptlrpc_request_free(req);
1068                 return -ENOMEM;
1069         }
1070
1071         /* NB req now owns desc and will free it when it gets freed */
1072         for (i = 0; i < op_data->op_npages; i++)
1073                 ptlrpc_prep_bulk_page_pin(desc, pages[i], 0, PAGE_CACHE_SIZE);
1074
1075         mdc_readdir_pack(req, op_data->op_offset,
1076                          PAGE_CACHE_SIZE * op_data->op_npages,
1077                          &op_data->op_fid1, op_data->op_capa1);
1078
1079         ptlrpc_request_set_replen(req);
1080         rc = ptlrpc_queue_wait(req);
1081         if (rc) {
1082                 ptlrpc_req_finished(req);
1083                 if (rc != -ETIMEDOUT)
1084                         return rc;
1085
1086                 resends++;
1087                 if (!client_should_resend(resends, &exp->exp_obd->u.cli)) {
1088                         CERROR("too many resend retries, returning error\n");
1089                         return -EIO;
1090                 }
1091                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(resends),
1092                                        NULL, NULL, NULL);
1093                 l_wait_event(waitq, 0, &lwi);
1094
1095                 goto restart_bulk;
1096         }
1097
1098         rc = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk,
1099                                           req->rq_bulk->bd_nob_transferred);
1100         if (rc < 0) {
1101                 ptlrpc_req_finished(req);
1102                 return rc;
1103         }
1104
1105         if (req->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK) {
1106                 CERROR("Unexpected # bytes transferred: %d (%ld expected)\n",
1107                         req->rq_bulk->bd_nob_transferred,
1108                         PAGE_CACHE_SIZE * op_data->op_npages);
1109                 ptlrpc_req_finished(req);
1110                 return -EPROTO;
1111         }
1112
1113         *request = req;
1114         return 0;
1115 }
1116
1117 static int mdc_statfs(const struct lu_env *env,
1118                       struct obd_export *exp, struct obd_statfs *osfs,
1119                       __u64 max_age, __u32 flags)
1120 {
1121         struct obd_device     *obd = class_exp2obd(exp);
1122         struct ptlrpc_request *req;
1123         struct obd_statfs     *msfs;
1124         struct obd_import     *imp = NULL;
1125         int                 rc;
1126
1127         /*
1128          * Since the request might also come from lprocfs, so we need
1129          * sync this with client_disconnect_export Bug15684
1130          */
1131         down_read(&obd->u.cli.cl_sem);
1132         if (obd->u.cli.cl_import)
1133                 imp = class_import_get(obd->u.cli.cl_import);
1134         up_read(&obd->u.cli.cl_sem);
1135         if (!imp)
1136                 return -ENODEV;
1137
1138         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_STATFS,
1139                                         LUSTRE_MDS_VERSION, MDS_STATFS);
1140         if (req == NULL)
1141                 GOTO(output, rc = -ENOMEM);
1142
1143         ptlrpc_request_set_replen(req);
1144
1145         if (flags & OBD_STATFS_NODELAY) {
1146                 /* procfs requests not want stay in wait for avoid deadlock */
1147                 req->rq_no_resend = 1;
1148                 req->rq_no_delay = 1;
1149         }
1150
1151         rc = ptlrpc_queue_wait(req);
1152         if (rc) {
1153                 /* check connection error first */
1154                 if (imp->imp_connect_error)
1155                         rc = imp->imp_connect_error;
1156                 GOTO(out, rc);
1157         }
1158
1159         msfs = req_capsule_server_get(&req->rq_pill, &RMF_OBD_STATFS);
1160         if (msfs == NULL)
1161                 GOTO(out, rc = -EPROTO);
1162
1163         *osfs = *msfs;
1164 out:
1165         ptlrpc_req_finished(req);
1166 output:
1167         class_import_put(imp);
1168         return rc;
1169 }
1170
1171 static int mdc_ioc_fid2path(struct obd_export *exp, struct getinfo_fid2path *gf)
1172 {
1173         __u32 keylen, vallen;
1174         void *key;
1175         int rc;
1176
1177         if (gf->gf_pathlen > PATH_MAX)
1178                 return -ENAMETOOLONG;
1179         if (gf->gf_pathlen < 2)
1180                 return -EOVERFLOW;
1181
1182         /* Key is KEY_FID2PATH + getinfo_fid2path description */
1183         keylen = cfs_size_round(sizeof(KEY_FID2PATH)) + sizeof(*gf);
1184         OBD_ALLOC(key, keylen);
1185         if (key == NULL)
1186                 return -ENOMEM;
1187         memcpy(key, KEY_FID2PATH, sizeof(KEY_FID2PATH));
1188         memcpy(key + cfs_size_round(sizeof(KEY_FID2PATH)), gf, sizeof(*gf));
1189
1190         CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n",
1191                PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno);
1192
1193         if (!fid_is_sane(&gf->gf_fid))
1194                 GOTO(out, rc = -EINVAL);
1195
1196         /* Val is struct getinfo_fid2path result plus path */
1197         vallen = sizeof(*gf) + gf->gf_pathlen;
1198
1199         rc = obd_get_info(NULL, exp, keylen, key, &vallen, gf, NULL);
1200         if (rc != 0 && rc != -EREMOTE)
1201                 GOTO(out, rc);
1202
1203         if (vallen <= sizeof(*gf))
1204                 GOTO(out, rc = -EPROTO);
1205         else if (vallen > sizeof(*gf) + gf->gf_pathlen)
1206                 GOTO(out, rc = -EOVERFLOW);
1207
1208         CDEBUG(D_IOCTL, "path get "DFID" from %llu #%d\n%s\n",
1209                PFID(&gf->gf_fid), gf->gf_recno, gf->gf_linkno, gf->gf_path);
1210
1211 out:
1212         OBD_FREE(key, keylen);
1213         return rc;
1214 }
1215
1216 static int mdc_ioc_hsm_progress(struct obd_export *exp,
1217                                 struct hsm_progress_kernel *hpk)
1218 {
1219         struct obd_import               *imp = class_exp2cliimp(exp);
1220         struct hsm_progress_kernel      *req_hpk;
1221         struct ptlrpc_request           *req;
1222         int                              rc;
1223
1224         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_PROGRESS,
1225                                         LUSTRE_MDS_VERSION, MDS_HSM_PROGRESS);
1226         if (req == NULL)
1227                 GOTO(out, rc = -ENOMEM);
1228
1229         mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1230
1231         /* Copy hsm_progress struct */
1232         req_hpk = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_PROGRESS);
1233         if (req_hpk == NULL)
1234                 GOTO(out, rc = -EPROTO);
1235
1236         *req_hpk = *hpk;
1237         req_hpk->hpk_errval = lustre_errno_hton(hpk->hpk_errval);
1238
1239         ptlrpc_request_set_replen(req);
1240
1241         rc = mdc_queue_wait(req);
1242         GOTO(out, rc);
1243 out:
1244         ptlrpc_req_finished(req);
1245         return rc;
1246 }
1247
1248 static int mdc_ioc_hsm_ct_register(struct obd_import *imp, __u32 archives)
1249 {
1250         __u32                   *archive_mask;
1251         struct ptlrpc_request   *req;
1252         int                      rc;
1253
1254         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_REGISTER,
1255                                         LUSTRE_MDS_VERSION,
1256                                         MDS_HSM_CT_REGISTER);
1257         if (req == NULL)
1258                 GOTO(out, rc = -ENOMEM);
1259
1260         mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1261
1262         /* Copy hsm_progress struct */
1263         archive_mask = req_capsule_client_get(&req->rq_pill,
1264                                               &RMF_MDS_HSM_ARCHIVE);
1265         if (archive_mask == NULL)
1266                 GOTO(out, rc = -EPROTO);
1267
1268         *archive_mask = archives;
1269
1270         ptlrpc_request_set_replen(req);
1271
1272         rc = mdc_queue_wait(req);
1273         GOTO(out, rc);
1274 out:
1275         ptlrpc_req_finished(req);
1276         return rc;
1277 }
1278
1279 static int mdc_ioc_hsm_current_action(struct obd_export *exp,
1280                                       struct md_op_data *op_data)
1281 {
1282         struct hsm_current_action       *hca = op_data->op_data;
1283         struct hsm_current_action       *req_hca;
1284         struct ptlrpc_request           *req;
1285         int                              rc;
1286
1287         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1288                                    &RQF_MDS_HSM_ACTION);
1289         if (req == NULL)
1290                 return -ENOMEM;
1291
1292         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1293
1294         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_ACTION);
1295         if (rc) {
1296                 ptlrpc_request_free(req);
1297                 return rc;
1298         }
1299
1300         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1301                       OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1302
1303         ptlrpc_request_set_replen(req);
1304
1305         rc = mdc_queue_wait(req);
1306         if (rc)
1307                 GOTO(out, rc);
1308
1309         req_hca = req_capsule_server_get(&req->rq_pill,
1310                                          &RMF_MDS_HSM_CURRENT_ACTION);
1311         if (req_hca == NULL)
1312                 GOTO(out, rc = -EPROTO);
1313
1314         *hca = *req_hca;
1315
1316 out:
1317         ptlrpc_req_finished(req);
1318         return rc;
1319 }
1320
1321 static int mdc_ioc_hsm_ct_unregister(struct obd_import *imp)
1322 {
1323         struct ptlrpc_request   *req;
1324         int                      rc;
1325
1326         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_HSM_CT_UNREGISTER,
1327                                         LUSTRE_MDS_VERSION,
1328                                         MDS_HSM_CT_UNREGISTER);
1329         if (req == NULL)
1330                 GOTO(out, rc = -ENOMEM);
1331
1332         mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1333
1334         ptlrpc_request_set_replen(req);
1335
1336         rc = mdc_queue_wait(req);
1337         GOTO(out, rc);
1338 out:
1339         ptlrpc_req_finished(req);
1340         return rc;
1341 }
1342
1343 static int mdc_ioc_hsm_state_get(struct obd_export *exp,
1344                                  struct md_op_data *op_data)
1345 {
1346         struct hsm_user_state   *hus = op_data->op_data;
1347         struct hsm_user_state   *req_hus;
1348         struct ptlrpc_request   *req;
1349         int                      rc;
1350
1351         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1352                                    &RQF_MDS_HSM_STATE_GET);
1353         if (req == NULL)
1354                 return -ENOMEM;
1355
1356         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1357
1358         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_GET);
1359         if (rc != 0) {
1360                 ptlrpc_request_free(req);
1361                 return rc;
1362         }
1363
1364         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1365                       OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1366
1367         ptlrpc_request_set_replen(req);
1368
1369         rc = mdc_queue_wait(req);
1370         if (rc)
1371                 GOTO(out, rc);
1372
1373         req_hus = req_capsule_server_get(&req->rq_pill, &RMF_HSM_USER_STATE);
1374         if (req_hus == NULL)
1375                 GOTO(out, rc = -EPROTO);
1376
1377         *hus = *req_hus;
1378
1379 out:
1380         ptlrpc_req_finished(req);
1381         return rc;
1382 }
1383
1384 static int mdc_ioc_hsm_state_set(struct obd_export *exp,
1385                                  struct md_op_data *op_data)
1386 {
1387         struct hsm_state_set    *hss = op_data->op_data;
1388         struct hsm_state_set    *req_hss;
1389         struct ptlrpc_request   *req;
1390         int                      rc;
1391
1392         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1393                                    &RQF_MDS_HSM_STATE_SET);
1394         if (req == NULL)
1395                 return -ENOMEM;
1396
1397         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1398
1399         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_STATE_SET);
1400         if (rc) {
1401                 ptlrpc_request_free(req);
1402                 return rc;
1403         }
1404
1405         mdc_pack_body(req, &op_data->op_fid1, op_data->op_capa1,
1406                       OBD_MD_FLRMTPERM, 0, op_data->op_suppgids[0], 0);
1407
1408         /* Copy states */
1409         req_hss = req_capsule_client_get(&req->rq_pill, &RMF_HSM_STATE_SET);
1410         if (req_hss == NULL)
1411                 GOTO(out, rc = -EPROTO);
1412         *req_hss = *hss;
1413
1414         ptlrpc_request_set_replen(req);
1415
1416         rc = mdc_queue_wait(req);
1417         GOTO(out, rc);
1418
1419 out:
1420         ptlrpc_req_finished(req);
1421         return rc;
1422 }
1423
1424 static int mdc_ioc_hsm_request(struct obd_export *exp,
1425                                struct hsm_user_request *hur)
1426 {
1427         struct obd_import       *imp = class_exp2cliimp(exp);
1428         struct ptlrpc_request   *req;
1429         struct hsm_request      *req_hr;
1430         struct hsm_user_item    *req_hui;
1431         char                    *req_opaque;
1432         int                      rc;
1433
1434         req = ptlrpc_request_alloc(imp, &RQF_MDS_HSM_REQUEST);
1435         if (req == NULL)
1436                 GOTO(out, rc = -ENOMEM);
1437
1438         req_capsule_set_size(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM, RCL_CLIENT,
1439                              hur->hur_request.hr_itemcount
1440                              * sizeof(struct hsm_user_item));
1441         req_capsule_set_size(&req->rq_pill, &RMF_GENERIC_DATA, RCL_CLIENT,
1442                              hur->hur_request.hr_data_len);
1443
1444         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_HSM_REQUEST);
1445         if (rc) {
1446                 ptlrpc_request_free(req);
1447                 return rc;
1448         }
1449
1450         mdc_pack_body(req, NULL, NULL, OBD_MD_FLRMTPERM, 0, 0, 0);
1451
1452         /* Copy hsm_request struct */
1453         req_hr = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_REQUEST);
1454         if (req_hr == NULL)
1455                 GOTO(out, rc = -EPROTO);
1456         *req_hr = hur->hur_request;
1457
1458         /* Copy hsm_user_item structs */
1459         req_hui = req_capsule_client_get(&req->rq_pill, &RMF_MDS_HSM_USER_ITEM);
1460         if (req_hui == NULL)
1461                 GOTO(out, rc = -EPROTO);
1462         memcpy(req_hui, hur->hur_user_item,
1463                hur->hur_request.hr_itemcount * sizeof(struct hsm_user_item));
1464
1465         /* Copy opaque field */
1466         req_opaque = req_capsule_client_get(&req->rq_pill, &RMF_GENERIC_DATA);
1467         if (req_opaque == NULL)
1468                 GOTO(out, rc = -EPROTO);
1469         memcpy(req_opaque, hur_data(hur), hur->hur_request.hr_data_len);
1470
1471         ptlrpc_request_set_replen(req);
1472
1473         rc = mdc_queue_wait(req);
1474         GOTO(out, rc);
1475
1476 out:
1477         ptlrpc_req_finished(req);
1478         return rc;
1479 }
1480
1481 static struct kuc_hdr *changelog_kuc_hdr(char *buf, int len, int flags)
1482 {
1483         struct kuc_hdr *lh = (struct kuc_hdr *)buf;
1484
1485         LASSERT(len <= KUC_CHANGELOG_MSG_MAXSIZE);
1486
1487         lh->kuc_magic = KUC_MAGIC;
1488         lh->kuc_transport = KUC_TRANSPORT_CHANGELOG;
1489         lh->kuc_flags = flags;
1490         lh->kuc_msgtype = CL_RECORD;
1491         lh->kuc_msglen = len;
1492         return lh;
1493 }
1494
1495 #define D_CHANGELOG 0
1496
1497 struct changelog_show {
1498         __u64           cs_startrec;
1499         __u32           cs_flags;
1500         struct file     *cs_fp;
1501         char            *cs_buf;
1502         struct obd_device *cs_obd;
1503 };
1504
1505 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
1506                              struct llog_rec_hdr *hdr, void *data)
1507 {
1508         struct changelog_show *cs = data;
1509         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
1510         struct kuc_hdr *lh;
1511         int len, rc;
1512
1513         if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
1514                 rc = -EINVAL;
1515                 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
1516                        cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
1517                        rec->cr.cr_type, rc);
1518                 return rc;
1519         }
1520
1521         if (rec->cr.cr_index < cs->cs_startrec) {
1522                 /* Skip entries earlier than what we are interested in */
1523                 CDEBUG(D_CHANGELOG, "rec=%llu start=%llu\n",
1524                        rec->cr.cr_index, cs->cs_startrec);
1525                 return 0;
1526         }
1527
1528         CDEBUG(D_CHANGELOG, "%llu %02d%-5s %llu 0x%x t="DFID" p="DFID
1529                 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1530                 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1531                 rec->cr.cr_flags & CLF_FLAGMASK,
1532                 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1533                 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1534
1535         len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1536
1537         /* Set up the message */
1538         lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1539         memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1540
1541         rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1542         CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len, rc);
1543
1544         return rc;
1545 }
1546
1547 static int mdc_changelog_send_thread(void *csdata)
1548 {
1549         struct changelog_show *cs = csdata;
1550         struct llog_ctxt *ctxt = NULL;
1551         struct llog_handle *llh = NULL;
1552         struct kuc_hdr *kuch;
1553         int rc;
1554
1555         CDEBUG(D_CHANGELOG, "changelog to fp=%p start %llu\n",
1556                cs->cs_fp, cs->cs_startrec);
1557
1558         OBD_ALLOC(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
1559         if (cs->cs_buf == NULL)
1560                 GOTO(out, rc = -ENOMEM);
1561
1562         /* Set up the remote catalog handle */
1563         ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1564         if (ctxt == NULL)
1565                 GOTO(out, rc = -ENOENT);
1566         rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1567                        LLOG_OPEN_EXISTS);
1568         if (rc) {
1569                 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1570                        cs->cs_obd->obd_name, rc);
1571                 GOTO(out, rc);
1572         }
1573         rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1574         if (rc) {
1575                 CERROR("llog_init_handle failed %d\n", rc);
1576                 GOTO(out, rc);
1577         }
1578
1579         rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
1580
1581         /* Send EOF no matter what our result */
1582         kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch), cs->cs_flags);
1583         if (kuch) {
1584                 kuch->kuc_msgtype = CL_EOF;
1585                 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1586         }
1587
1588 out:
1589         fput(cs->cs_fp);
1590         if (llh)
1591                 llog_cat_close(NULL, llh);
1592         if (ctxt)
1593                 llog_ctxt_put(ctxt);
1594         if (cs->cs_buf)
1595                 OBD_FREE(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
1596         OBD_FREE_PTR(cs);
1597         return rc;
1598 }
1599
1600 static int mdc_ioc_changelog_send(struct obd_device *obd,
1601                                   struct ioc_changelog *icc)
1602 {
1603         struct changelog_show *cs;
1604         int rc;
1605
1606         /* Freed in mdc_changelog_send_thread */
1607         OBD_ALLOC_PTR(cs);
1608         if (!cs)
1609                 return -ENOMEM;
1610
1611         cs->cs_obd = obd;
1612         cs->cs_startrec = icc->icc_recno;
1613         /* matching fput in mdc_changelog_send_thread */
1614         cs->cs_fp = fget(icc->icc_id);
1615         cs->cs_flags = icc->icc_flags;
1616
1617         /*
1618          * New thread because we should return to user app before
1619          * writing into our pipe
1620          */
1621         rc = PTR_ERR(kthread_run(mdc_changelog_send_thread, cs,
1622                                  "mdc_clg_send_thread"));
1623         if (!IS_ERR_VALUE(rc)) {
1624                 CDEBUG(D_CHANGELOG, "start changelog thread\n");
1625                 return 0;
1626         }
1627
1628         CERROR("Failed to start changelog thread: %d\n", rc);
1629         OBD_FREE_PTR(cs);
1630         return rc;
1631 }
1632
1633 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1634                                 struct lustre_kernelcomm *lk);
1635
1636 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1637                           struct obd_quotactl *oqctl)
1638 {
1639         struct client_obd       *cli = &exp->exp_obd->u.cli;
1640         struct ptlrpc_request   *req;
1641         struct obd_quotactl     *body;
1642         int                   rc;
1643
1644         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1645                                         &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1646                                         MDS_QUOTACHECK);
1647         if (req == NULL)
1648                 return -ENOMEM;
1649
1650         body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1651         *body = *oqctl;
1652
1653         ptlrpc_request_set_replen(req);
1654
1655         /* the next poll will find -ENODATA, that means quotacheck is
1656          * going on */
1657         cli->cl_qchk_stat = -ENODATA;
1658         rc = ptlrpc_queue_wait(req);
1659         if (rc)
1660                 cli->cl_qchk_stat = rc;
1661         ptlrpc_req_finished(req);
1662         return rc;
1663 }
1664
1665 static int mdc_quota_poll_check(struct obd_export *exp,
1666                                 struct if_quotacheck *qchk)
1667 {
1668         struct client_obd *cli = &exp->exp_obd->u.cli;
1669         int rc;
1670
1671         qchk->obd_uuid = cli->cl_target_uuid;
1672         memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1673
1674         rc = cli->cl_qchk_stat;
1675         /* the client is not the previous one */
1676         if (rc == CL_NOT_QUOTACHECKED)
1677                 rc = -EINTR;
1678         return rc;
1679 }
1680
1681 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1682                         struct obd_quotactl *oqctl)
1683 {
1684         struct ptlrpc_request   *req;
1685         struct obd_quotactl     *oqc;
1686         int                   rc;
1687
1688         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1689                                         &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1690                                         MDS_QUOTACTL);
1691         if (req == NULL)
1692                 return -ENOMEM;
1693
1694         oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1695         *oqc = *oqctl;
1696
1697         ptlrpc_request_set_replen(req);
1698         ptlrpc_at_set_req_timeout(req);
1699         req->rq_no_resend = 1;
1700
1701         rc = ptlrpc_queue_wait(req);
1702         if (rc)
1703                 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1704
1705         if (req->rq_repmsg) {
1706                 oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1707                 if (oqc) {
1708                         *oqctl = *oqc;
1709                 } else if (!rc) {
1710                         CERROR("Can't unpack obd_quotactl\n");
1711                         rc = -EPROTO;
1712                 }
1713         } else if (!rc) {
1714                 CERROR("Can't unpack obd_quotactl\n");
1715                 rc = -EPROTO;
1716         }
1717         ptlrpc_req_finished(req);
1718
1719         return rc;
1720 }
1721
1722 static int mdc_ioc_swap_layouts(struct obd_export *exp,
1723                                 struct md_op_data *op_data)
1724 {
1725         LIST_HEAD(cancels);
1726         struct ptlrpc_request   *req;
1727         int                      rc, count;
1728         struct mdc_swap_layouts *msl, *payload;
1729
1730         msl = op_data->op_data;
1731
1732         /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1733          * first thing it will do is to cancel the 2 layout
1734          * locks hold by this client.
1735          * So the client must cancel its layout locks on the 2 fids
1736          * with the request RPC to avoid extra RPC round trips
1737          */
1738         count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1739                                         LCK_CR, MDS_INODELOCK_LAYOUT);
1740         count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1741                                          LCK_CR, MDS_INODELOCK_LAYOUT);
1742
1743         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1744                                    &RQF_MDS_SWAP_LAYOUTS);
1745         if (req == NULL) {
1746                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
1747                 return -ENOMEM;
1748         }
1749
1750         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1751         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
1752
1753         rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1754         if (rc) {
1755                 ptlrpc_request_free(req);
1756                 return rc;
1757         }
1758
1759         mdc_swap_layouts_pack(req, op_data);
1760
1761         payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1762         LASSERT(payload);
1763
1764         *payload = *msl;
1765
1766         ptlrpc_request_set_replen(req);
1767
1768         rc = ptlrpc_queue_wait(req);
1769         if (rc)
1770                 GOTO(out, rc);
1771
1772 out:
1773         ptlrpc_req_finished(req);
1774         return rc;
1775 }
1776
1777 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1778                          void *karg, void *uarg)
1779 {
1780         struct obd_device *obd = exp->exp_obd;
1781         struct obd_ioctl_data *data = karg;
1782         struct obd_import *imp = obd->u.cli.cl_import;
1783         struct llog_ctxt *ctxt;
1784         int rc;
1785
1786         if (!try_module_get(THIS_MODULE)) {
1787                 CERROR("Can't get module. Is it alive?");
1788                 return -EINVAL;
1789         }
1790         switch (cmd) {
1791         case OBD_IOC_CHANGELOG_SEND:
1792                 rc = mdc_ioc_changelog_send(obd, karg);
1793                 GOTO(out, rc);
1794         case OBD_IOC_CHANGELOG_CLEAR: {
1795                 struct ioc_changelog *icc = karg;
1796                 struct changelog_setinfo cs = {
1797                         .cs_recno = icc->icc_recno,
1798                         .cs_id = icc->icc_id
1799                 };
1800
1801                 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1802                                         KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1803                                         NULL);
1804                 GOTO(out, rc);
1805         }
1806         case OBD_IOC_FID2PATH:
1807                 rc = mdc_ioc_fid2path(exp, karg);
1808                 GOTO(out, rc);
1809         case LL_IOC_HSM_CT_START:
1810                 rc = mdc_ioc_hsm_ct_start(exp, karg);
1811                 /* ignore if it was already registered on this MDS. */
1812                 if (rc == -EEXIST)
1813                         rc = 0;
1814                 GOTO(out, rc);
1815         case LL_IOC_HSM_PROGRESS:
1816                 rc = mdc_ioc_hsm_progress(exp, karg);
1817                 GOTO(out, rc);
1818         case LL_IOC_HSM_STATE_GET:
1819                 rc = mdc_ioc_hsm_state_get(exp, karg);
1820                 GOTO(out, rc);
1821         case LL_IOC_HSM_STATE_SET:
1822                 rc = mdc_ioc_hsm_state_set(exp, karg);
1823                 GOTO(out, rc);
1824         case LL_IOC_HSM_ACTION:
1825                 rc = mdc_ioc_hsm_current_action(exp, karg);
1826                 GOTO(out, rc);
1827         case LL_IOC_HSM_REQUEST:
1828                 rc = mdc_ioc_hsm_request(exp, karg);
1829                 GOTO(out, rc);
1830         case OBD_IOC_CLIENT_RECOVER:
1831                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1832                 if (rc < 0)
1833                         GOTO(out, rc);
1834                 GOTO(out, rc = 0);
1835         case IOC_OSC_SET_ACTIVE:
1836                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1837                 GOTO(out, rc);
1838         case OBD_IOC_PARSE: {
1839                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1840                 rc = class_config_parse_llog(NULL, ctxt, data->ioc_inlbuf1,
1841                                              NULL);
1842                 llog_ctxt_put(ctxt);
1843                 GOTO(out, rc);
1844         }
1845         case OBD_IOC_LLOG_INFO:
1846         case OBD_IOC_LLOG_PRINT: {
1847                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1848                 rc = llog_ioctl(NULL, ctxt, cmd, data);
1849                 llog_ctxt_put(ctxt);
1850                 GOTO(out, rc);
1851         }
1852         case OBD_IOC_POLL_QUOTACHECK:
1853                 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1854                 GOTO(out, rc);
1855         case OBD_IOC_PING_TARGET:
1856                 rc = ptlrpc_obd_ping(obd);
1857                 GOTO(out, rc);
1858         /*
1859          * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1860          * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1861          * there'd be no LMV layer thus we might be called here. Eventually
1862          * this code should be removed.
1863          * bz20731, LU-592.
1864          */
1865         case IOC_OBD_STATFS: {
1866                 struct obd_statfs stat_buf = {0};
1867
1868                 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1869                         GOTO(out, rc = -ENODEV);
1870
1871                 /* copy UUID */
1872                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1873                                      min((int) data->ioc_plen2,
1874                                          (int) sizeof(struct obd_uuid))))
1875                         GOTO(out, rc = -EFAULT);
1876
1877                 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1878                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1879                                 0);
1880                 if (rc != 0)
1881                         GOTO(out, rc);
1882
1883                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
1884                                      min((int) data->ioc_plen1,
1885                                          (int) sizeof(stat_buf))))
1886                         GOTO(out, rc = -EFAULT);
1887
1888                 GOTO(out, rc = 0);
1889         }
1890         case OBD_IOC_QUOTACTL: {
1891                 struct if_quotactl *qctl = karg;
1892                 struct obd_quotactl *oqctl;
1893
1894                 OBD_ALLOC_PTR(oqctl);
1895                 if (oqctl == NULL)
1896                         GOTO(out, rc = -ENOMEM);
1897
1898                 QCTL_COPY(oqctl, qctl);
1899                 rc = obd_quotactl(exp, oqctl);
1900                 if (rc == 0) {
1901                         QCTL_COPY(qctl, oqctl);
1902                         qctl->qc_valid = QC_MDTIDX;
1903                         qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1904                 }
1905
1906                 OBD_FREE_PTR(oqctl);
1907                 GOTO(out, rc);
1908         }
1909         case LL_IOC_GET_CONNECT_FLAGS:
1910                 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
1911                                  sizeof(*exp_connect_flags_ptr(exp))))
1912                         GOTO(out, rc = -EFAULT);
1913
1914                 GOTO(out, rc = 0);
1915         case LL_IOC_LOV_SWAP_LAYOUTS:
1916                 rc = mdc_ioc_swap_layouts(exp, karg);
1917                 GOTO(out, rc);
1918         default:
1919                 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
1920                 GOTO(out, rc = -ENOTTY);
1921         }
1922 out:
1923         module_put(THIS_MODULE);
1924
1925         return rc;
1926 }
1927
1928 int mdc_get_info_rpc(struct obd_export *exp,
1929                      u32 keylen, void *key,
1930                      int vallen, void *val)
1931 {
1932         struct obd_import      *imp = class_exp2cliimp(exp);
1933         struct ptlrpc_request  *req;
1934         char               *tmp;
1935         int                  rc = -EINVAL;
1936
1937         req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1938         if (req == NULL)
1939                 return -ENOMEM;
1940
1941         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1942                              RCL_CLIENT, keylen);
1943         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1944                              RCL_CLIENT, sizeof(__u32));
1945
1946         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1947         if (rc) {
1948                 ptlrpc_request_free(req);
1949                 return rc;
1950         }
1951
1952         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1953         memcpy(tmp, key, keylen);
1954         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1955         memcpy(tmp, &vallen, sizeof(__u32));
1956
1957         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1958                              RCL_SERVER, vallen);
1959         ptlrpc_request_set_replen(req);
1960
1961         rc = ptlrpc_queue_wait(req);
1962         /* -EREMOTE means the get_info result is partial, and it needs to
1963          * continue on another MDT, see fid2path part in lmv_iocontrol */
1964         if (rc == 0 || rc == -EREMOTE) {
1965                 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1966                 memcpy(val, tmp, vallen);
1967                 if (ptlrpc_rep_need_swab(req)) {
1968                         if (KEY_IS(KEY_FID2PATH))
1969                                 lustre_swab_fid2path(val);
1970                 }
1971         }
1972         ptlrpc_req_finished(req);
1973
1974         return rc;
1975 }
1976
1977 static void lustre_swab_hai(struct hsm_action_item *h)
1978 {
1979         __swab32s(&h->hai_len);
1980         __swab32s(&h->hai_action);
1981         lustre_swab_lu_fid(&h->hai_fid);
1982         lustre_swab_lu_fid(&h->hai_dfid);
1983         __swab64s(&h->hai_cookie);
1984         __swab64s(&h->hai_extent.offset);
1985         __swab64s(&h->hai_extent.length);
1986         __swab64s(&h->hai_gid);
1987 }
1988
1989 static void lustre_swab_hal(struct hsm_action_list *h)
1990 {
1991         struct hsm_action_item  *hai;
1992         int                      i;
1993
1994         __swab32s(&h->hal_version);
1995         __swab32s(&h->hal_count);
1996         __swab32s(&h->hal_archive_id);
1997         __swab64s(&h->hal_flags);
1998         hai = hai_zero(h);
1999         for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
2000                 lustre_swab_hai(hai);
2001 }
2002
2003 static void lustre_swab_kuch(struct kuc_hdr *l)
2004 {
2005         __swab16s(&l->kuc_magic);
2006         /* __u8 l->kuc_transport */
2007         __swab16s(&l->kuc_msgtype);
2008         __swab16s(&l->kuc_msglen);
2009 }
2010
2011 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2012                                 struct lustre_kernelcomm *lk)
2013 {
2014         struct obd_import  *imp = class_exp2cliimp(exp);
2015         __u32               archive = lk->lk_data;
2016         int                 rc = 0;
2017
2018         if (lk->lk_group != KUC_GRP_HSM) {
2019                 CERROR("Bad copytool group %d\n", lk->lk_group);
2020                 return -EINVAL;
2021         }
2022
2023         CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2024                lk->lk_uid, lk->lk_group, lk->lk_flags);
2025
2026         if (lk->lk_flags & LK_FLG_STOP) {
2027                 /* Unregister with the coordinator */
2028                 rc = mdc_ioc_hsm_ct_unregister(imp);
2029         } else {
2030                 rc = mdc_ioc_hsm_ct_register(imp, archive);
2031         }
2032
2033         return rc;
2034 }
2035
2036 /**
2037  * Send a message to any listening copytools
2038  * @param val KUC message (kuc_hdr + hsm_action_list)
2039  * @param len total length of message
2040  */
2041 static int mdc_hsm_copytool_send(int len, void *val)
2042 {
2043         struct kuc_hdr          *lh = (struct kuc_hdr *)val;
2044         struct hsm_action_list  *hal = (struct hsm_action_list *)(lh + 1);
2045         int                      rc;
2046
2047         if (len < sizeof(*lh) + sizeof(*hal)) {
2048                 CERROR("Short HSM message %d < %d\n", len,
2049                        (int) (sizeof(*lh) + sizeof(*hal)));
2050                 return -EPROTO;
2051         }
2052         if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2053                 lustre_swab_kuch(lh);
2054                 lustre_swab_hal(hal);
2055         } else if (lh->kuc_magic != KUC_MAGIC) {
2056                 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2057                 return -EPROTO;
2058         }
2059
2060         CDEBUG(D_HSM,
2061                "Received message mg=%x t=%d m=%d l=%d actions=%d on %s\n",
2062                lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2063                lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2064
2065         /* Broadcast to HSM listeners */
2066         rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2067
2068         return rc;
2069 }
2070
2071 /**
2072  * callback function passed to kuc for re-registering each HSM copytool
2073  * running on MDC, after MDT shutdown/recovery.
2074  * @param data archive id served by the copytool
2075  * @param cb_arg callback argument (obd_import)
2076  */
2077 static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2078 {
2079         struct obd_import       *imp = (struct obd_import *)cb_arg;
2080         __u32                    archive = data;
2081         int                      rc;
2082
2083         CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2084                archive);
2085         rc = mdc_ioc_hsm_ct_register(imp, archive);
2086
2087         /* ignore error if the copytool is already registered */
2088         return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2089 }
2090
2091 /**
2092  * Re-establish all kuc contexts with MDT
2093  * after MDT shutdown/recovery.
2094  */
2095 static int mdc_kuc_reregister(struct obd_import *imp)
2096 {
2097         /* re-register HSM agents */
2098         return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2099                                          (void *)imp);
2100 }
2101
2102 int mdc_set_info_async(const struct lu_env *env,
2103                        struct obd_export *exp,
2104                        u32 keylen, void *key,
2105                        u32 vallen, void *val,
2106                        struct ptlrpc_request_set *set)
2107 {
2108         struct obd_import       *imp = class_exp2cliimp(exp);
2109         int                      rc;
2110
2111         if (KEY_IS(KEY_READ_ONLY)) {
2112                 if (vallen != sizeof(int))
2113                         return -EINVAL;
2114
2115                 spin_lock(&imp->imp_lock);
2116                 if (*((int *)val)) {
2117                         imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2118                         imp->imp_connect_data.ocd_connect_flags |=
2119                                                         OBD_CONNECT_RDONLY;
2120                 } else {
2121                         imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2122                         imp->imp_connect_data.ocd_connect_flags &=
2123                                                         ~OBD_CONNECT_RDONLY;
2124                 }
2125                 spin_unlock(&imp->imp_lock);
2126
2127                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2128                                        keylen, key, vallen, val, set);
2129                 return rc;
2130         }
2131         if (KEY_IS(KEY_SPTLRPC_CONF)) {
2132                 sptlrpc_conf_client_adapt(exp->exp_obd);
2133                 return 0;
2134         }
2135         if (KEY_IS(KEY_FLUSH_CTX)) {
2136                 sptlrpc_import_flush_my_ctx(imp);
2137                 return 0;
2138         }
2139         if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2140                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2141                                        keylen, key, vallen, val, set);
2142                 return rc;
2143         }
2144         if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2145                 rc = mdc_hsm_copytool_send(vallen, val);
2146                 return rc;
2147         }
2148
2149         CERROR("Unknown key %s\n", (char *)key);
2150         return -EINVAL;
2151 }
2152
2153 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2154                  __u32 keylen, void *key, __u32 *vallen, void *val,
2155                  struct lov_stripe_md *lsm)
2156 {
2157         int rc = -EINVAL;
2158
2159         if (KEY_IS(KEY_MAX_EASIZE)) {
2160                 int mdsize, *max_easize;
2161
2162                 if (*vallen != sizeof(int))
2163                         return -EINVAL;
2164                 mdsize = *(int *)val;
2165                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2166                         exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2167                 max_easize = val;
2168                 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2169                 return 0;
2170         } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2171                 int *default_easize;
2172
2173                 if (*vallen != sizeof(int))
2174                         return -EINVAL;
2175                 default_easize = val;
2176                 *default_easize = exp->exp_obd->u.cli.cl_default_mds_easize;
2177                 return 0;
2178         } else if (KEY_IS(KEY_MAX_COOKIESIZE)) {
2179                 int mdsize, *max_cookiesize;
2180
2181                 if (*vallen != sizeof(int))
2182                         return -EINVAL;
2183                 mdsize = *(int *)val;
2184                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_cookiesize)
2185                         exp->exp_obd->u.cli.cl_max_mds_cookiesize = mdsize;
2186                 max_cookiesize = val;
2187                 *max_cookiesize = exp->exp_obd->u.cli.cl_max_mds_cookiesize;
2188                 return 0;
2189         } else if (KEY_IS(KEY_DEFAULT_COOKIESIZE)) {
2190                 int *default_cookiesize;
2191
2192                 if (*vallen != sizeof(int))
2193                         return -EINVAL;
2194                 default_cookiesize = val;
2195                 *default_cookiesize =
2196                         exp->exp_obd->u.cli.cl_default_mds_cookiesize;
2197                 return 0;
2198         } else if (KEY_IS(KEY_CONN_DATA)) {
2199                 struct obd_import *imp = class_exp2cliimp(exp);
2200                 struct obd_connect_data *data = val;
2201
2202                 if (*vallen != sizeof(*data))
2203                         return -EINVAL;
2204
2205                 *data = imp->imp_connect_data;
2206                 return 0;
2207         } else if (KEY_IS(KEY_TGT_COUNT)) {
2208                 *((int *)val) = 1;
2209                 return 0;
2210         }
2211
2212         rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2213
2214         return rc;
2215 }
2216
2217 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2218                    struct obd_capa *oc, struct obd_client_handle *handle,
2219                    int flags)
2220 {
2221         struct ptlrpc_request *req;
2222         struct mdt_body       *body;
2223         int                 rc;
2224
2225         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2226         if (req == NULL)
2227                 return -ENOMEM;
2228
2229         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2230
2231         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2232         if (rc) {
2233                 ptlrpc_request_free(req);
2234                 return rc;
2235         }
2236
2237         mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2238
2239         ptlrpc_request_set_replen(req);
2240
2241         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2242         rc = ptlrpc_queue_wait(req);
2243         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2244         if (rc) {
2245                 CERROR("Pin failed: %d\n", rc);
2246                 GOTO(err_out, rc);
2247         }
2248
2249         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2250         if (body == NULL)
2251                 GOTO(err_out, rc = -EPROTO);
2252
2253         handle->och_fh = body->handle;
2254         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2255
2256         handle->och_mod = obd_mod_alloc();
2257         if (handle->och_mod == NULL) {
2258                 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2259                 GOTO(err_out, rc = -ENOMEM);
2260         }
2261         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2262
2263         return 0;
2264
2265 err_out:
2266         ptlrpc_req_finished(req);
2267         return rc;
2268 }
2269
2270 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2271                      int flag)
2272 {
2273         struct ptlrpc_request *req;
2274         struct mdt_body       *body;
2275         int                 rc;
2276
2277         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2278                                         LUSTRE_MDS_VERSION, MDS_UNPIN);
2279         if (req == NULL)
2280                 return -ENOMEM;
2281
2282         body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2283         body->handle = handle->och_fh;
2284         body->flags = flag;
2285
2286         ptlrpc_request_set_replen(req);
2287
2288         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2289         rc = ptlrpc_queue_wait(req);
2290         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2291
2292         if (rc != 0)
2293                 CERROR("Unpin failed: %d\n", rc);
2294
2295         ptlrpc_req_finished(req);
2296         ptlrpc_req_finished(handle->och_mod->mod_open_req);
2297
2298         obd_mod_put(handle->och_mod);
2299         return rc;
2300 }
2301
2302 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
2303              struct obd_capa *oc, struct ptlrpc_request **request)
2304 {
2305         struct ptlrpc_request *req;
2306         int                 rc;
2307
2308         *request = NULL;
2309         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2310         if (req == NULL)
2311                 return -ENOMEM;
2312
2313         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2314
2315         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2316         if (rc) {
2317                 ptlrpc_request_free(req);
2318                 return rc;
2319         }
2320
2321         mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2322
2323         ptlrpc_request_set_replen(req);
2324
2325         rc = ptlrpc_queue_wait(req);
2326         if (rc)
2327                 ptlrpc_req_finished(req);
2328         else
2329                 *request = req;
2330         return rc;
2331 }
2332
2333 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2334                             enum obd_import_event event)
2335 {
2336         int rc = 0;
2337
2338         LASSERT(imp->imp_obd == obd);
2339
2340         switch (event) {
2341         case IMP_EVENT_DISCON: {
2342 #if 0
2343                 /* XXX Pass event up to OBDs stack. used only for FLD now */
2344                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2345 #endif
2346                 break;
2347         }
2348         case IMP_EVENT_INACTIVE: {
2349                 struct client_obd *cli = &obd->u.cli;
2350                 /*
2351                  * Flush current sequence to make client obtain new one
2352                  * from server in case of disconnect/reconnect.
2353                  */
2354                 if (cli->cl_seq != NULL)
2355                         seq_client_flush(cli->cl_seq);
2356
2357                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2358                 break;
2359         }
2360         case IMP_EVENT_INVALIDATE: {
2361                 struct ldlm_namespace *ns = obd->obd_namespace;
2362
2363                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2364
2365                 break;
2366         }
2367         case IMP_EVENT_ACTIVE:
2368                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2369                 /* redo the kuc registration after reconnecting */
2370                 if (rc == 0)
2371                         rc = mdc_kuc_reregister(imp);
2372                 break;
2373         case IMP_EVENT_OCD:
2374                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2375                 break;
2376         case IMP_EVENT_DEACTIVATE:
2377         case IMP_EVENT_ACTIVATE:
2378                 break;
2379         default:
2380                 CERROR("Unknown import event %x\n", event);
2381                 LBUG();
2382         }
2383         return rc;
2384 }
2385
2386 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2387                   struct md_op_data *op_data)
2388 {
2389         struct client_obd *cli = &exp->exp_obd->u.cli;
2390         struct lu_client_seq *seq = cli->cl_seq;
2391
2392         return seq_client_alloc_fid(NULL, seq, fid);
2393 }
2394
2395 struct obd_uuid *mdc_get_uuid(struct obd_export *exp)
2396 {
2397         struct client_obd *cli = &exp->exp_obd->u.cli;
2398
2399         return &cli->cl_target_uuid;
2400 }
2401
2402 /**
2403  * Determine whether the lock can be canceled before replaying it during
2404  * recovery, non zero value will be return if the lock can be canceled,
2405  * or zero returned for not
2406  */
2407 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2408 {
2409         if (lock->l_resource->lr_type != LDLM_IBITS)
2410                 return 0;
2411
2412         /* FIXME: if we ever get into a situation where there are too many
2413          * opened files with open locks on a single node, then we really
2414          * should replay these open locks to reget it */
2415         if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2416                 return 0;
2417
2418         return 1;
2419 }
2420
2421 static int mdc_resource_inode_free(struct ldlm_resource *res)
2422 {
2423         if (res->lr_lvb_inode)
2424                 res->lr_lvb_inode = NULL;
2425
2426         return 0;
2427 }
2428
2429 struct ldlm_valblock_ops inode_lvbo = {
2430         .lvbo_free = mdc_resource_inode_free,
2431 };
2432
2433 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2434 {
2435         struct client_obd *cli = &obd->u.cli;
2436         struct lprocfs_static_vars lvars = { NULL };
2437         int rc;
2438
2439         OBD_ALLOC(cli->cl_rpc_lock, sizeof(*cli->cl_rpc_lock));
2440         if (!cli->cl_rpc_lock)
2441                 return -ENOMEM;
2442         mdc_init_rpc_lock(cli->cl_rpc_lock);
2443
2444         ptlrpcd_addref();
2445
2446         OBD_ALLOC(cli->cl_close_lock, sizeof(*cli->cl_close_lock));
2447         if (!cli->cl_close_lock)
2448                 GOTO(err_rpc_lock, rc = -ENOMEM);
2449         mdc_init_rpc_lock(cli->cl_close_lock);
2450
2451         rc = client_obd_setup(obd, cfg);
2452         if (rc)
2453                 GOTO(err_close_lock, rc);
2454         lprocfs_mdc_init_vars(&lvars);
2455         lprocfs_obd_setup(obd, lvars.obd_vars);
2456         sptlrpc_lprocfs_cliobd_attach(obd);
2457         ptlrpc_lprocfs_register_obd(obd);
2458
2459         ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2460
2461         obd->obd_namespace->ns_lvbo = &inode_lvbo;
2462
2463         rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2464         if (rc) {
2465                 mdc_cleanup(obd);
2466                 CERROR("failed to setup llogging subsystems\n");
2467         }
2468
2469         return rc;
2470
2471 err_close_lock:
2472         OBD_FREE(cli->cl_close_lock, sizeof(*cli->cl_close_lock));
2473 err_rpc_lock:
2474         OBD_FREE(cli->cl_rpc_lock, sizeof(*cli->cl_rpc_lock));
2475         ptlrpcd_decref();
2476         return rc;
2477 }
2478
2479 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
2480  * us to make MDS RPCs with large enough reply buffers to hold a default
2481  * sized EA and cookie without having to calculate this (via a call into the
2482  * LOV + OSCs) each time we make an RPC.  The maximum size is also tracked
2483  * but not used to avoid wastefully vmalloc()'ing large reply buffers when
2484  * a large number of stripes is possible.  If a larger reply buffer is
2485  * required it will be reallocated in the ptlrpc layer due to overflow.
2486  */
2487 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2488                             int def_easize, int cookiesize, int def_cookiesize)
2489 {
2490         struct obd_device *obd = exp->exp_obd;
2491         struct client_obd *cli = &obd->u.cli;
2492
2493         if (cli->cl_max_mds_easize < easize)
2494                 cli->cl_max_mds_easize = easize;
2495
2496         if (cli->cl_default_mds_easize < def_easize)
2497                 cli->cl_default_mds_easize = def_easize;
2498
2499         if (cli->cl_max_mds_cookiesize < cookiesize)
2500                 cli->cl_max_mds_cookiesize = cookiesize;
2501
2502         if (cli->cl_default_mds_cookiesize < def_cookiesize)
2503                 cli->cl_default_mds_cookiesize = def_cookiesize;
2504
2505         return 0;
2506 }
2507
2508 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2509 {
2510         int rc = 0;
2511
2512         switch (stage) {
2513         case OBD_CLEANUP_EARLY:
2514                 break;
2515         case OBD_CLEANUP_EXPORTS:
2516                 /* Failsafe, ok if racy */
2517                 if (obd->obd_type->typ_refcnt <= 1)
2518                         libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2519
2520                 obd_cleanup_client_import(obd);
2521                 ptlrpc_lprocfs_unregister_obd(obd);
2522                 lprocfs_obd_cleanup(obd);
2523
2524                 rc = obd_llog_finish(obd, 0);
2525                 if (rc != 0)
2526                         CERROR("failed to cleanup llogging subsystems\n");
2527                 break;
2528         }
2529         return rc;
2530 }
2531
2532 static int mdc_cleanup(struct obd_device *obd)
2533 {
2534         struct client_obd *cli = &obd->u.cli;
2535
2536         OBD_FREE(cli->cl_rpc_lock, sizeof(*cli->cl_rpc_lock));
2537         OBD_FREE(cli->cl_close_lock, sizeof(*cli->cl_close_lock));
2538
2539         ptlrpcd_decref();
2540
2541         return client_obd_cleanup(obd);
2542 }
2543
2544
2545 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2546                          struct obd_device *tgt, int *index)
2547 {
2548         struct llog_ctxt        *ctxt;
2549         int                      rc;
2550
2551         LASSERT(olg == &obd->obd_olg);
2552
2553         rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2554                         &llog_client_ops);
2555         if (rc)
2556                 return rc;
2557
2558         ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2559         llog_initiator_connect(ctxt);
2560         llog_ctxt_put(ctxt);
2561
2562         return 0;
2563 }
2564
2565 static int mdc_llog_finish(struct obd_device *obd, int count)
2566 {
2567         struct llog_ctxt *ctxt;
2568
2569         ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2570         if (ctxt)
2571                 llog_cleanup(NULL, ctxt);
2572
2573         return 0;
2574 }
2575
2576 static int mdc_process_config(struct obd_device *obd, u32 len, void *buf)
2577 {
2578         struct lustre_cfg *lcfg = buf;
2579         struct lprocfs_static_vars lvars = { NULL };
2580         int rc = 0;
2581
2582         lprocfs_mdc_init_vars(&lvars);
2583         switch (lcfg->lcfg_command) {
2584         default:
2585                 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2586                                               lcfg, obd);
2587                 if (rc > 0)
2588                         rc = 0;
2589                 break;
2590         }
2591         return rc;
2592 }
2593
2594
2595 /* get remote permission for current user on fid */
2596 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2597                         struct obd_capa *oc, __u32 suppgid,
2598                         struct ptlrpc_request **request)
2599 {
2600         struct ptlrpc_request  *req;
2601         int                 rc;
2602
2603         LASSERT(client_is_remote(exp));
2604
2605         *request = NULL;
2606         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2607         if (req == NULL)
2608                 return -ENOMEM;
2609
2610         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2611
2612         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2613         if (rc) {
2614                 ptlrpc_request_free(req);
2615                 return rc;
2616         }
2617
2618         mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2619
2620         req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2621                              sizeof(struct mdt_remote_perm));
2622
2623         ptlrpc_request_set_replen(req);
2624
2625         rc = ptlrpc_queue_wait(req);
2626         if (rc)
2627                 ptlrpc_req_finished(req);
2628         else
2629                 *request = req;
2630         return rc;
2631 }
2632
2633 static int mdc_interpret_renew_capa(const struct lu_env *env,
2634                                     struct ptlrpc_request *req, void *args,
2635                                     int status)
2636 {
2637         struct mdc_renew_capa_args *ra = args;
2638         struct mdt_body *body = NULL;
2639         struct lustre_capa *capa;
2640
2641         if (status)
2642                 GOTO(out, capa = ERR_PTR(status));
2643
2644         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2645         if (body == NULL)
2646                 GOTO(out, capa = ERR_PTR(-EFAULT));
2647
2648         if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2649                 GOTO(out, capa = ERR_PTR(-ENOENT));
2650
2651         capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2652         if (!capa)
2653                 GOTO(out, capa = ERR_PTR(-EFAULT));
2654 out:
2655         ra->ra_cb(ra->ra_oc, capa);
2656         return 0;
2657 }
2658
2659 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2660                           renew_capa_cb_t cb)
2661 {
2662         struct ptlrpc_request *req;
2663         struct mdc_renew_capa_args *ra;
2664
2665         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2666                                         LUSTRE_MDS_VERSION, MDS_GETATTR);
2667         if (req == NULL)
2668                 return -ENOMEM;
2669
2670         /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2671          * capa to renew is oss capa.
2672          */
2673         mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2674         ptlrpc_request_set_replen(req);
2675
2676         CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2677         ra = ptlrpc_req_async_args(req);
2678         ra->ra_oc = oc;
2679         ra->ra_cb = cb;
2680         req->rq_interpret_reply = mdc_interpret_renew_capa;
2681         ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2682         return 0;
2683 }
2684
2685 struct obd_ops mdc_obd_ops = {
2686         .o_owner            = THIS_MODULE,
2687         .o_setup            = mdc_setup,
2688         .o_precleanup       = mdc_precleanup,
2689         .o_cleanup        = mdc_cleanup,
2690         .o_add_conn      = client_import_add_conn,
2691         .o_del_conn      = client_import_del_conn,
2692         .o_connect          = client_connect_import,
2693         .o_disconnect       = client_disconnect_export,
2694         .o_iocontrol    = mdc_iocontrol,
2695         .o_set_info_async   = mdc_set_info_async,
2696         .o_statfs          = mdc_statfs,
2697         .o_pin        = mdc_pin,
2698         .o_unpin            = mdc_unpin,
2699         .o_fid_init         = client_fid_init,
2700         .o_fid_fini         = client_fid_fini,
2701         .o_fid_alloc    = mdc_fid_alloc,
2702         .o_import_event     = mdc_import_event,
2703         .o_llog_init    = mdc_llog_init,
2704         .o_llog_finish      = mdc_llog_finish,
2705         .o_get_info      = mdc_get_info,
2706         .o_process_config   = mdc_process_config,
2707         .o_get_uuid      = mdc_get_uuid,
2708         .o_quotactl      = mdc_quotactl,
2709         .o_quotacheck       = mdc_quotacheck
2710 };
2711
2712 struct md_ops mdc_md_ops = {
2713         .m_getstatus    = mdc_getstatus,
2714         .m_null_inode       = mdc_null_inode,
2715         .m_find_cbdata      = mdc_find_cbdata,
2716         .m_close            = mdc_close,
2717         .m_create          = mdc_create,
2718         .m_done_writing     = mdc_done_writing,
2719         .m_enqueue        = mdc_enqueue,
2720         .m_getattr        = mdc_getattr,
2721         .m_getattr_name     = mdc_getattr_name,
2722         .m_intent_lock      = mdc_intent_lock,
2723         .m_link      = mdc_link,
2724         .m_is_subdir    = mdc_is_subdir,
2725         .m_rename          = mdc_rename,
2726         .m_setattr        = mdc_setattr,
2727         .m_setxattr      = mdc_setxattr,
2728         .m_getxattr      = mdc_getxattr,
2729         .m_sync      = mdc_sync,
2730         .m_readpage      = mdc_readpage,
2731         .m_unlink          = mdc_unlink,
2732         .m_cancel_unused    = mdc_cancel_unused,
2733         .m_init_ea_size     = mdc_init_ea_size,
2734         .m_set_lock_data    = mdc_set_lock_data,
2735         .m_lock_match       = mdc_lock_match,
2736         .m_get_lustre_md    = mdc_get_lustre_md,
2737         .m_free_lustre_md   = mdc_free_lustre_md,
2738         .m_set_open_replay_data = mdc_set_open_replay_data,
2739         .m_clear_open_replay_data = mdc_clear_open_replay_data,
2740         .m_renew_capa       = mdc_renew_capa,
2741         .m_unpack_capa      = mdc_unpack_capa,
2742         .m_get_remote_perm  = mdc_get_remote_perm,
2743         .m_intent_getattr_async = mdc_intent_getattr_async,
2744         .m_revalidate_lock      = mdc_revalidate_lock
2745 };
2746
2747 int __init mdc_init(void)
2748 {
2749         int rc;
2750         struct lprocfs_static_vars lvars = { NULL };
2751
2752         lprocfs_mdc_init_vars(&lvars);
2753
2754         rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2755                                  LUSTRE_MDC_NAME, NULL);
2756         return rc;
2757 }
2758
2759 static void /*__exit*/ mdc_exit(void)
2760 {
2761         class_unregister_type(LUSTRE_MDC_NAME);
2762 }
2763
2764 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2765 MODULE_DESCRIPTION("Lustre Metadata Client");
2766 MODULE_LICENSE("GPL");
2767
2768 module_init(mdc_init);
2769 module_exit(mdc_exit);