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