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