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