Whamcloud - gitweb
LU-2986 mdc: Kernel Oops on ioctl LL_IOC_GET_MDTIDX
[fs/lustre-release.git] / lustre / mdc / mdc_request.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_MDC
38
39 #ifdef __KERNEL__
40 # include <linux/module.h>
41 # include <linux/pagemap.h>
42 # include <linux/miscdevice.h>
43 # include <linux/init.h>
44 #else
45 # include <liblustre.h>
46 #endif
47
48 #include <lustre_acl.h>
49 #include <obd_class.h>
50 #include <lustre_fid.h>
51 #include <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_show_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             (rec->cr.cr_type >= CL_LAST)) {
1531                 CERROR("Not a changelog rec %d/%d\n", rec->cr_hdr.lrh_type,
1532                        rec->cr.cr_type);
1533                 RETURN(-EINVAL);
1534         }
1535
1536         if (rec->cr.cr_index < cs->cs_startrec) {
1537                 /* Skip entries earlier than what we are interested in */
1538                 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
1539                        rec->cr.cr_index, cs->cs_startrec);
1540                 RETURN(0);
1541         }
1542
1543         CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
1544                 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
1545                 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
1546                 rec->cr.cr_flags & CLF_FLAGMASK,
1547                 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
1548                 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
1549
1550         len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
1551
1552         /* Set up the message */
1553         lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
1554         memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
1555
1556         rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
1557         CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
1558
1559         RETURN(rc);
1560 }
1561
1562 static int mdc_changelog_send_thread(void *csdata)
1563 {
1564         struct changelog_show *cs = csdata;
1565         struct llog_ctxt *ctxt = NULL;
1566         struct llog_handle *llh = NULL;
1567         struct kuc_hdr *kuch;
1568         int rc;
1569
1570         CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
1571                cs->cs_fp, cs->cs_startrec);
1572
1573         /*
1574          * It's important to daemonize here to close unused FDs.
1575          * The write fd from pipe is already opened by the caller,
1576          * so it's fine to clear all files here
1577          */
1578         cfs_daemonize("mdc_clg_send_thread");
1579
1580         OBD_ALLOC(cs->cs_buf, CR_MAXSIZE);
1581         if (cs->cs_buf == NULL)
1582                 GOTO(out, rc = -ENOMEM);
1583
1584         /* Set up the remote catalog handle */
1585         ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
1586         if (ctxt == NULL)
1587                 GOTO(out, rc = -ENOENT);
1588         rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
1589                        LLOG_OPEN_EXISTS);
1590         if (rc) {
1591                 CERROR("%s: fail to open changelog catalog: rc = %d\n",
1592                        cs->cs_obd->obd_name, rc);
1593                 GOTO(out, rc);
1594         }
1595         rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
1596         if (rc) {
1597                 CERROR("llog_init_handle failed %d\n", rc);
1598                 GOTO(out, rc);
1599         }
1600
1601         rc = llog_cat_process(NULL, llh, changelog_show_cb, cs, 0, 0);
1602
1603         /* Send EOF no matter what our result */
1604         if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
1605                                       cs->cs_flags))) {
1606                 kuch->kuc_msgtype = CL_EOF;
1607                 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
1608         }
1609
1610 out:
1611         fput(cs->cs_fp);
1612         if (llh)
1613                 llog_cat_close(NULL, llh);
1614         if (ctxt)
1615                 llog_ctxt_put(ctxt);
1616         if (cs->cs_buf)
1617                 OBD_FREE(cs->cs_buf, CR_MAXSIZE);
1618         OBD_FREE_PTR(cs);
1619         /* detach from parent process so we get cleaned up */
1620         cfs_daemonize("cl_send");
1621         return rc;
1622 }
1623
1624 static int mdc_ioc_changelog_send(struct obd_device *obd,
1625                                   struct ioc_changelog *icc)
1626 {
1627         struct changelog_show *cs;
1628         int rc;
1629
1630         /* Freed in mdc_changelog_send_thread */
1631         OBD_ALLOC_PTR(cs);
1632         if (!cs)
1633                 return -ENOMEM;
1634
1635         cs->cs_obd = obd;
1636         cs->cs_startrec = icc->icc_recno;
1637         /* matching fput in mdc_changelog_send_thread */
1638         cs->cs_fp = fget(icc->icc_id);
1639         cs->cs_flags = icc->icc_flags;
1640
1641         /* New thread because we should return to user app before
1642            writing into our pipe */
1643         rc = cfs_create_thread(mdc_changelog_send_thread, cs, CFS_DAEMON_FLAGS);
1644         if (rc >= 0) {
1645                 CDEBUG(D_CHANGELOG, "start changelog thread: %d\n", rc);
1646                 return 0;
1647         }
1648
1649         CERROR("Failed to start changelog thread: %d\n", rc);
1650         OBD_FREE_PTR(cs);
1651         return rc;
1652 }
1653
1654 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
1655                                 struct lustre_kernelcomm *lk);
1656
1657 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
1658                           struct obd_quotactl *oqctl)
1659 {
1660         struct client_obd       *cli = &exp->exp_obd->u.cli;
1661         struct ptlrpc_request   *req;
1662         struct obd_quotactl     *body;
1663         int                      rc;
1664         ENTRY;
1665
1666         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1667                                         &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
1668                                         MDS_QUOTACHECK);
1669         if (req == NULL)
1670                 RETURN(-ENOMEM);
1671
1672         body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1673         *body = *oqctl;
1674
1675         ptlrpc_request_set_replen(req);
1676
1677         /* the next poll will find -ENODATA, that means quotacheck is
1678          * going on */
1679         cli->cl_qchk_stat = -ENODATA;
1680         rc = ptlrpc_queue_wait(req);
1681         if (rc)
1682                 cli->cl_qchk_stat = rc;
1683         ptlrpc_req_finished(req);
1684         RETURN(rc);
1685 }
1686
1687 static int mdc_quota_poll_check(struct obd_export *exp,
1688                                 struct if_quotacheck *qchk)
1689 {
1690         struct client_obd *cli = &exp->exp_obd->u.cli;
1691         int rc;
1692         ENTRY;
1693
1694         qchk->obd_uuid = cli->cl_target_uuid;
1695         memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
1696
1697         rc = cli->cl_qchk_stat;
1698         /* the client is not the previous one */
1699         if (rc == CL_NOT_QUOTACHECKED)
1700                 rc = -EINTR;
1701         RETURN(rc);
1702 }
1703
1704 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
1705                         struct obd_quotactl *oqctl)
1706 {
1707         struct ptlrpc_request   *req;
1708         struct obd_quotactl     *oqc;
1709         int                      rc;
1710         ENTRY;
1711
1712         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1713                                         &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
1714                                         MDS_QUOTACTL);
1715         if (req == NULL)
1716                 RETURN(-ENOMEM);
1717
1718         oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
1719         *oqc = *oqctl;
1720
1721         ptlrpc_request_set_replen(req);
1722         ptlrpc_at_set_req_timeout(req);
1723         req->rq_no_resend = 1;
1724
1725         rc = ptlrpc_queue_wait(req);
1726         if (rc)
1727                 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
1728
1729         if (req->rq_repmsg &&
1730             (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
1731                 *oqctl = *oqc;
1732         } else if (!rc) {
1733                 CERROR ("Can't unpack obd_quotactl\n");
1734                 rc = -EPROTO;
1735         }
1736         ptlrpc_req_finished(req);
1737
1738         RETURN(rc);
1739 }
1740
1741 static int mdc_ioc_swap_layouts(struct obd_export *exp,
1742                                 struct md_op_data *op_data)
1743 {
1744         CFS_LIST_HEAD(cancels);
1745         struct ptlrpc_request   *req;
1746         int                      rc, count;
1747         struct mdc_swap_layouts *msl, *payload;
1748         ENTRY;
1749
1750         msl = op_data->op_data;
1751
1752         /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
1753          * first thing it will do is to cancel the 2 layout
1754          * locks hold by this client.
1755          * So the client must cancel its layout locks on the 2 fids
1756          * with the request RPC to avoid extra RPC round trips
1757          */
1758         count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
1759                                         LCK_CR, MDS_INODELOCK_LAYOUT);
1760         count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
1761                                          LCK_CR, MDS_INODELOCK_LAYOUT);
1762
1763         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
1764                                    &RQF_MDS_SWAP_LAYOUTS);
1765         if (req == NULL) {
1766                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
1767                 RETURN(-ENOMEM);
1768         }
1769
1770         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
1771         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
1772
1773         rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
1774         if (rc) {
1775                 ptlrpc_request_free(req);
1776                 RETURN(rc);
1777         }
1778
1779         mdc_swap_layouts_pack(req, op_data);
1780
1781         payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
1782         LASSERT(payload);
1783
1784         *payload = *msl;
1785
1786         ptlrpc_request_set_replen(req);
1787
1788         rc = ptlrpc_queue_wait(req);
1789         if (rc)
1790                 GOTO(out, rc);
1791         EXIT;
1792
1793 out:
1794         ptlrpc_req_finished(req);
1795         return rc;
1796 }
1797
1798 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1799                          void *karg, void *uarg)
1800 {
1801         struct obd_device *obd = exp->exp_obd;
1802         struct obd_ioctl_data *data = karg;
1803         struct obd_import *imp = obd->u.cli.cl_import;
1804         struct llog_ctxt *ctxt;
1805         int rc;
1806         ENTRY;
1807
1808         if (!cfs_try_module_get(THIS_MODULE)) {
1809                 CERROR("Can't get module. Is it alive?");
1810                 return -EINVAL;
1811         }
1812         switch (cmd) {
1813         case OBD_IOC_CHANGELOG_SEND:
1814                 rc = mdc_ioc_changelog_send(obd, karg);
1815                 GOTO(out, rc);
1816         case OBD_IOC_CHANGELOG_CLEAR: {
1817                 struct ioc_changelog *icc = karg;
1818                 struct changelog_setinfo cs =
1819                         {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
1820                 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
1821                                         KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
1822                                         NULL);
1823                 GOTO(out, rc);
1824         }
1825         case OBD_IOC_FID2PATH:
1826                 rc = mdc_ioc_fid2path(exp, karg);
1827                 GOTO(out, rc);
1828         case LL_IOC_HSM_CT_START:
1829                 rc = mdc_ioc_hsm_ct_start(exp, karg);
1830                 GOTO(out, rc);
1831         case LL_IOC_HSM_PROGRESS:
1832                 rc = mdc_ioc_hsm_progress(exp, karg);
1833                 GOTO(out, rc);
1834         case LL_IOC_HSM_STATE_GET:
1835                 rc = mdc_ioc_hsm_state_get(exp, karg);
1836                 GOTO(out, rc);
1837         case LL_IOC_HSM_STATE_SET:
1838                 rc = mdc_ioc_hsm_state_set(exp, karg);
1839         case LL_IOC_HSM_ACTION:
1840                 rc = mdc_ioc_hsm_current_action(exp, karg);
1841                 GOTO(out, rc);
1842         case LL_IOC_HSM_REQUEST:
1843                 rc = mdc_ioc_hsm_request(exp, karg);
1844                 GOTO(out, rc);
1845         case OBD_IOC_CLIENT_RECOVER:
1846                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
1847                 if (rc < 0)
1848                         GOTO(out, rc);
1849                 GOTO(out, rc = 0);
1850         case IOC_OSC_SET_ACTIVE:
1851                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
1852                 GOTO(out, rc);
1853         case OBD_IOC_PARSE: {
1854                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
1855                 rc = class_config_parse_llog(NULL, ctxt, data->ioc_inlbuf1,
1856                                              NULL);
1857                 llog_ctxt_put(ctxt);
1858                 GOTO(out, rc);
1859         }
1860 #ifdef __KERNEL__
1861         case OBD_IOC_LLOG_INFO:
1862         case OBD_IOC_LLOG_PRINT: {
1863                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1864                 rc = llog_ioctl(NULL, ctxt, cmd, data);
1865                 llog_ctxt_put(ctxt);
1866                 GOTO(out, rc);
1867         }
1868 #endif
1869         case OBD_IOC_POLL_QUOTACHECK:
1870                 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
1871                 GOTO(out, rc);
1872         case OBD_IOC_PING_TARGET:
1873                 rc = ptlrpc_obd_ping(obd);
1874                 GOTO(out, rc);
1875         /*
1876          * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
1877          * LMV instead of MDC. But when the cluster is upgraded from 1.8,
1878          * there'd be no LMV layer thus we might be called here. Eventually
1879          * this code should be removed.
1880          * bz20731, LU-592.
1881          */
1882         case IOC_OBD_STATFS: {
1883                 struct obd_statfs stat_buf = {0};
1884
1885                 if (*((__u32 *) data->ioc_inlbuf2) != 0)
1886                         GOTO(out, rc = -ENODEV);
1887
1888                 /* copy UUID */
1889                 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
1890                                      min((int) data->ioc_plen2,
1891                                          (int) sizeof(struct obd_uuid))))
1892                         GOTO(out, rc = -EFAULT);
1893
1894                 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
1895                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
1896                                 0);
1897                 if (rc != 0)
1898                         GOTO(out, rc);
1899
1900                 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
1901                                      min((int) data->ioc_plen1,
1902                                          (int) sizeof(stat_buf))))
1903                         GOTO(out, rc = -EFAULT);
1904
1905                 GOTO(out, rc = 0);
1906         }
1907         case OBD_IOC_QUOTACTL: {
1908                 struct if_quotactl *qctl = karg;
1909                 struct obd_quotactl *oqctl;
1910
1911                 OBD_ALLOC_PTR(oqctl);
1912                 if (!oqctl)
1913                         RETURN(-ENOMEM);
1914
1915                 QCTL_COPY(oqctl, qctl);
1916                 rc = obd_quotactl(exp, oqctl);
1917                 if (rc == 0) {
1918                         QCTL_COPY(qctl, oqctl);
1919                         qctl->qc_valid = QC_MDTIDX;
1920                         qctl->obd_uuid = obd->u.cli.cl_target_uuid;
1921                 }
1922                 OBD_FREE_PTR(oqctl);
1923                 break;
1924         }
1925         case LL_IOC_GET_CONNECT_FLAGS: {
1926                 if (cfs_copy_to_user(uarg,
1927                                      exp_connect_flags_ptr(exp),
1928                                      sizeof(__u64)))
1929                         GOTO(out, rc = -EFAULT);
1930                 else
1931                         GOTO(out, rc = 0);
1932         }
1933         case LL_IOC_LOV_SWAP_LAYOUTS: {
1934                 rc = mdc_ioc_swap_layouts(exp, karg);
1935                 break;
1936         }
1937         default:
1938                 CERROR("mdc_ioctl(): unrecognised ioctl %#x\n", cmd);
1939                 GOTO(out, rc = -ENOTTY);
1940         }
1941 out:
1942         cfs_module_put(THIS_MODULE);
1943
1944         return rc;
1945 }
1946
1947 int mdc_get_info_rpc(struct obd_export *exp,
1948                      obd_count keylen, void *key,
1949                      int vallen, void *val)
1950 {
1951         struct obd_import      *imp = class_exp2cliimp(exp);
1952         struct ptlrpc_request  *req;
1953         char                   *tmp;
1954         int                     rc = -EINVAL;
1955         ENTRY;
1956
1957         req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
1958         if (req == NULL)
1959                 RETURN(-ENOMEM);
1960
1961         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
1962                              RCL_CLIENT, keylen);
1963         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
1964                              RCL_CLIENT, sizeof(__u32));
1965
1966         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
1967         if (rc) {
1968                 ptlrpc_request_free(req);
1969                 RETURN(rc);
1970         }
1971
1972         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
1973         memcpy(tmp, key, keylen);
1974         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
1975         memcpy(tmp, &vallen, sizeof(__u32));
1976
1977         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
1978                              RCL_SERVER, vallen);
1979         ptlrpc_request_set_replen(req);
1980
1981         rc = ptlrpc_queue_wait(req);
1982         /* -EREMOTE means the get_info result is partial, and it needs to
1983          * continue on another MDT, see fid2path part in lmv_iocontrol */
1984         if (rc == 0 || rc == -EREMOTE) {
1985                 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
1986                 memcpy(val, tmp, vallen);
1987                 if (ptlrpc_rep_need_swab(req)) {
1988                         if (KEY_IS(KEY_FID2PATH))
1989                                 lustre_swab_fid2path(val);
1990                 }
1991         }
1992         ptlrpc_req_finished(req);
1993
1994         RETURN(rc);
1995 }
1996
1997 static void lustre_swab_hai(struct hsm_action_item *h)
1998 {
1999         __swab32s(&h->hai_len);
2000         __swab32s(&h->hai_action);
2001         lustre_swab_lu_fid(&h->hai_fid);
2002         lustre_swab_lu_fid(&h->hai_dfid);
2003         __swab64s(&h->hai_cookie);
2004         __swab64s(&h->hai_extent.offset);
2005         __swab64s(&h->hai_extent.length);
2006         __swab64s(&h->hai_gid);
2007 }
2008
2009 static void lustre_swab_hal(struct hsm_action_list *h)
2010 {
2011         struct hsm_action_item  *hai;
2012         int                      i;
2013
2014         __swab32s(&h->hal_version);
2015         __swab32s(&h->hal_count);
2016         __swab32s(&h->hal_archive_id);
2017         __swab64s(&h->hal_flags);
2018         hai = hai_zero(h);
2019         for (i = 0; i < h->hal_count; i++) {
2020                 lustre_swab_hai(hai);
2021                 hai = hai_next(hai);
2022         }
2023 }
2024
2025 static void lustre_swab_kuch(struct kuc_hdr *l)
2026 {
2027         __swab16s(&l->kuc_magic);
2028         /* __u8 l->kuc_transport */
2029         __swab16s(&l->kuc_msgtype);
2030         __swab16s(&l->kuc_msglen);
2031 }
2032
2033 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2034                                 struct lustre_kernelcomm *lk)
2035 {
2036         struct obd_import  *imp = class_exp2cliimp(exp);
2037         __u32               archive = lk->lk_data;
2038         int                 rc = 0;
2039
2040         if (lk->lk_group != KUC_GRP_HSM) {
2041                 CERROR("Bad copytool group %d\n", lk->lk_group);
2042                 return -EINVAL;
2043         }
2044
2045         CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2046                lk->lk_uid, lk->lk_group, lk->lk_flags);
2047
2048         if (lk->lk_flags & LK_FLG_STOP) {
2049                 rc = libcfs_kkuc_group_rem(lk->lk_uid, lk->lk_group);
2050                 /* Unregister with the coordinator */
2051                 if (rc == 0)
2052                         rc = mdc_ioc_hsm_ct_unregister(imp);
2053         } else {
2054                 struct file *fp = fget(lk->lk_wfd);
2055
2056                 rc = libcfs_kkuc_group_add(fp, lk->lk_uid, lk->lk_group,
2057                                            lk->lk_data);
2058                 if (rc && fp)
2059                         fput(fp);
2060                 if (rc == 0)
2061                         rc = mdc_ioc_hsm_ct_register(imp, archive);
2062         }
2063
2064         return rc;
2065 }
2066
2067 /**
2068  * Send a message to any listening copytools
2069  * @param val KUC message (kuc_hdr + hsm_action_list)
2070  * @param len total length of message
2071  */
2072 static int mdc_hsm_copytool_send(int len, void *val)
2073 {
2074         struct kuc_hdr          *lh = (struct kuc_hdr *)val;
2075         struct hsm_action_list  *hal = (struct hsm_action_list *)(lh + 1);
2076         int                      rc;
2077         ENTRY;
2078
2079         if (len < sizeof(*lh) + sizeof(*hal)) {
2080                 CERROR("Short HSM message %d < %d\n", len,
2081                        (int) (sizeof(*lh) + sizeof(*hal)));
2082                 RETURN(-EPROTO);
2083         }
2084         if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2085                 lustre_swab_kuch(lh);
2086                 lustre_swab_hal(hal);
2087         } else if (lh->kuc_magic != KUC_MAGIC) {
2088                 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2089                 RETURN(-EPROTO);
2090         }
2091
2092         CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2093                "on %s\n",
2094                lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2095                lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2096
2097         /* Broadcast to HSM listeners */
2098         rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2099
2100         RETURN(rc);
2101 }
2102
2103 /**
2104  * callback function passed to kuc for re-registering each HSM copytool
2105  * running on MDC, after MDT shutdown/recovery.
2106  * @param data archive id served by the copytool
2107  * @param cb_arg callback argument (obd_import)
2108  */
2109 static int mdc_hsm_ct_reregister(__u32 data, void *cb_arg)
2110 {
2111         struct obd_import       *imp = (struct obd_import *)cb_arg;
2112         __u32                    archive = data;
2113         int                      rc;
2114
2115         CDEBUG(D_HA, "recover copytool registration to MDT (archive=%#x)\n",
2116                archive);
2117         rc = mdc_ioc_hsm_ct_register(imp, archive);
2118
2119         /* ignore error if the copytool is already registered */
2120         return ((rc != 0) && (rc != -EEXIST)) ? rc : 0;
2121 }
2122
2123 /**
2124  * Re-establish all kuc contexts with MDT
2125  * after MDT shutdown/recovery.
2126  */
2127 static int mdc_kuc_reregister(struct obd_import *imp)
2128 {
2129         /* re-register HSM agents */
2130         return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2131                                          (void *)imp);
2132 }
2133
2134 int mdc_set_info_async(const struct lu_env *env,
2135                        struct obd_export *exp,
2136                        obd_count keylen, void *key,
2137                        obd_count vallen, void *val,
2138                        struct ptlrpc_request_set *set)
2139 {
2140         struct obd_import       *imp = class_exp2cliimp(exp);
2141         int                      rc;
2142         ENTRY;
2143
2144         if (KEY_IS(KEY_READ_ONLY)) {
2145                 if (vallen != sizeof(int))
2146                         RETURN(-EINVAL);
2147
2148                 spin_lock(&imp->imp_lock);
2149                 if (*((int *)val)) {
2150                         imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2151                         imp->imp_connect_data.ocd_connect_flags |=
2152                                                         OBD_CONNECT_RDONLY;
2153                 } else {
2154                         imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2155                         imp->imp_connect_data.ocd_connect_flags &=
2156                                                         ~OBD_CONNECT_RDONLY;
2157                 }
2158                 spin_unlock(&imp->imp_lock);
2159
2160                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2161                                        keylen, key, vallen, val, set);
2162                 RETURN(rc);
2163         }
2164         if (KEY_IS(KEY_SPTLRPC_CONF)) {
2165                 sptlrpc_conf_client_adapt(exp->exp_obd);
2166                 RETURN(0);
2167         }
2168         if (KEY_IS(KEY_FLUSH_CTX)) {
2169                 sptlrpc_import_flush_my_ctx(imp);
2170                 RETURN(0);
2171         }
2172         if (KEY_IS(KEY_MDS_CONN)) {
2173                 /* mds-mds import */
2174                 spin_lock(&imp->imp_lock);
2175                 imp->imp_server_timeout = 1;
2176                 spin_unlock(&imp->imp_lock);
2177                 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2178                 CDEBUG(D_OTHER, "%s: timeout / 2\n", exp->exp_obd->obd_name);
2179                 RETURN(0);
2180         }
2181         if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2182                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2183                                        keylen, key, vallen, val, set);
2184                 RETURN(rc);
2185         }
2186         if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2187                 rc = mdc_hsm_copytool_send(vallen, val);
2188                 RETURN(rc);
2189         }
2190
2191         CERROR("Unknown key %s\n", (char *)key);
2192         RETURN(-EINVAL);
2193 }
2194
2195 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2196                  __u32 keylen, void *key, __u32 *vallen, void *val,
2197                  struct lov_stripe_md *lsm)
2198 {
2199         int rc = -EINVAL;
2200
2201         if (KEY_IS(KEY_MAX_EASIZE)) {
2202                 int mdsize, *max_easize;
2203
2204                 if (*vallen != sizeof(int))
2205                         RETURN(-EINVAL);
2206                 mdsize = *(int*)val;
2207                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2208                         exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2209                 max_easize = val;
2210                 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2211                 RETURN(0);
2212         } else if (KEY_IS(KEY_CONN_DATA)) {
2213                 struct obd_import *imp = class_exp2cliimp(exp);
2214                 struct obd_connect_data *data = val;
2215
2216                 if (*vallen != sizeof(*data))
2217                         RETURN(-EINVAL);
2218
2219                 *data = imp->imp_connect_data;
2220                 RETURN(0);
2221         } else if (KEY_IS(KEY_TGT_COUNT)) {
2222                 *((int *)val) = 1;
2223                 RETURN(0);
2224         }
2225
2226         rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2227
2228         RETURN(rc);
2229 }
2230
2231 static int mdc_pin(struct obd_export *exp, const struct lu_fid *fid,
2232                    struct obd_capa *oc, struct obd_client_handle *handle,
2233                    int flags)
2234 {
2235         struct ptlrpc_request *req;
2236         struct mdt_body       *body;
2237         int                    rc;
2238         ENTRY;
2239
2240         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_PIN);
2241         if (req == NULL)
2242                 RETURN(-ENOMEM);
2243
2244         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2245
2246         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_PIN);
2247         if (rc) {
2248                 ptlrpc_request_free(req);
2249                 RETURN(rc);
2250         }
2251
2252         mdc_pack_body(req, fid, oc, 0, 0, -1, flags);
2253
2254         ptlrpc_request_set_replen(req);
2255
2256         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2257         rc = ptlrpc_queue_wait(req);
2258         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2259         if (rc) {
2260                 CERROR("Pin failed: %d\n", rc);
2261                 GOTO(err_out, rc);
2262         }
2263
2264         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2265         if (body == NULL)
2266                 GOTO(err_out, rc = -EPROTO);
2267
2268         handle->och_fh = body->handle;
2269         handle->och_magic = OBD_CLIENT_HANDLE_MAGIC;
2270
2271         handle->och_mod = obd_mod_alloc();
2272         if (handle->och_mod == NULL) {
2273                 DEBUG_REQ(D_ERROR, req, "can't allocate md_open_data");
2274                 GOTO(err_out, rc = -ENOMEM);
2275         }
2276         handle->och_mod->mod_open_req = req; /* will be dropped by unpin */
2277
2278         RETURN(0);
2279
2280 err_out:
2281         ptlrpc_req_finished(req);
2282         RETURN(rc);
2283 }
2284
2285 static int mdc_unpin(struct obd_export *exp, struct obd_client_handle *handle,
2286                      int flag)
2287 {
2288         struct ptlrpc_request *req;
2289         struct mdt_body       *body;
2290         int                    rc;
2291         ENTRY;
2292
2293         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_UNPIN,
2294                                         LUSTRE_MDS_VERSION, MDS_UNPIN);
2295         if (req == NULL)
2296                 RETURN(-ENOMEM);
2297
2298         body = req_capsule_client_get(&req->rq_pill, &RMF_MDT_BODY);
2299         body->handle = handle->och_fh;
2300         body->flags = flag;
2301
2302         ptlrpc_request_set_replen(req);
2303
2304         mdc_get_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2305         rc = ptlrpc_queue_wait(req);
2306         mdc_put_rpc_lock(exp->exp_obd->u.cli.cl_rpc_lock, NULL);
2307
2308         if (rc != 0)
2309                 CERROR("Unpin failed: %d\n", rc);
2310
2311         ptlrpc_req_finished(req);
2312         ptlrpc_req_finished(handle->och_mod->mod_open_req);
2313
2314         obd_mod_put(handle->och_mod);
2315         RETURN(rc);
2316 }
2317
2318 int mdc_sync(struct obd_export *exp, const struct lu_fid *fid,
2319              struct obd_capa *oc, struct ptlrpc_request **request)
2320 {
2321         struct ptlrpc_request *req;
2322         int                    rc;
2323         ENTRY;
2324
2325         *request = NULL;
2326         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2327         if (req == NULL)
2328                 RETURN(-ENOMEM);
2329
2330         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2331
2332         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2333         if (rc) {
2334                 ptlrpc_request_free(req);
2335                 RETURN(rc);
2336         }
2337
2338         mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2339
2340         ptlrpc_request_set_replen(req);
2341
2342         rc = ptlrpc_queue_wait(req);
2343         if (rc)
2344                 ptlrpc_req_finished(req);
2345         else
2346                 *request = req;
2347         RETURN(rc);
2348 }
2349
2350 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2351                             enum obd_import_event event)
2352 {
2353         int rc = 0;
2354
2355         LASSERT(imp->imp_obd == obd);
2356
2357         switch (event) {
2358         case IMP_EVENT_DISCON: {
2359 #if 0
2360                 /* XXX Pass event up to OBDs stack. used only for FLD now */
2361                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2362 #endif
2363                 break;
2364         }
2365         case IMP_EVENT_INACTIVE: {
2366                 struct client_obd *cli = &obd->u.cli;
2367                 /*
2368                  * Flush current sequence to make client obtain new one
2369                  * from server in case of disconnect/reconnect.
2370                  */
2371                 if (cli->cl_seq != NULL)
2372                         seq_client_flush(cli->cl_seq);
2373
2374                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2375                 break;
2376         }
2377         case IMP_EVENT_INVALIDATE: {
2378                 struct ldlm_namespace *ns = obd->obd_namespace;
2379
2380                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2381
2382                 break;
2383         }
2384         case IMP_EVENT_ACTIVE:
2385                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2386                 /* restore re-establish kuc registration after reconnecting */
2387                 if (rc == 0)
2388                         rc = mdc_kuc_reregister(imp);
2389                 break;
2390         case IMP_EVENT_OCD:
2391                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2392                 break;
2393         case IMP_EVENT_DEACTIVATE:
2394         case IMP_EVENT_ACTIVATE:
2395                 break;
2396         default:
2397                 CERROR("Unknown import event %x\n", event);
2398                 LBUG();
2399         }
2400         RETURN(rc);
2401 }
2402
2403 int mdc_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
2404                   struct md_op_data *op_data)
2405 {
2406         struct client_obd *cli = &exp->exp_obd->u.cli;
2407         struct lu_client_seq *seq = cli->cl_seq;
2408         ENTRY;
2409         RETURN(seq_client_alloc_fid(NULL, seq, fid));
2410 }
2411
2412 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
2413         struct client_obd *cli = &exp->exp_obd->u.cli;
2414         return &cli->cl_target_uuid;
2415 }
2416
2417 /**
2418  * Determine whether the lock can be canceled before replaying it during
2419  * recovery, non zero value will be return if the lock can be canceled,
2420  * or zero returned for not
2421  */
2422 static int mdc_cancel_for_recovery(struct ldlm_lock *lock)
2423 {
2424         if (lock->l_resource->lr_type != LDLM_IBITS)
2425                 RETURN(0);
2426
2427         /* FIXME: if we ever get into a situation where there are too many
2428          * opened files with open locks on a single node, then we really
2429          * should replay these open locks to reget it */
2430         if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2431                 RETURN(0);
2432
2433         RETURN(1);
2434 }
2435
2436 static int mdc_resource_inode_free(struct ldlm_resource *res)
2437 {
2438         if (res->lr_lvb_inode)
2439                 res->lr_lvb_inode = NULL;
2440
2441         return 0;
2442 }
2443
2444 struct ldlm_valblock_ops inode_lvbo = {
2445         lvbo_free: mdc_resource_inode_free
2446 };
2447
2448 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2449 {
2450         struct client_obd *cli = &obd->u.cli;
2451         struct lprocfs_static_vars lvars = { 0 };
2452         int rc;
2453         ENTRY;
2454
2455         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2456         if (!cli->cl_rpc_lock)
2457                 RETURN(-ENOMEM);
2458         mdc_init_rpc_lock(cli->cl_rpc_lock);
2459
2460         ptlrpcd_addref();
2461
2462         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2463         if (!cli->cl_close_lock)
2464                 GOTO(err_rpc_lock, rc = -ENOMEM);
2465         mdc_init_rpc_lock(cli->cl_close_lock);
2466
2467         rc = client_obd_setup(obd, cfg);
2468         if (rc)
2469                 GOTO(err_close_lock, rc);
2470         lprocfs_mdc_init_vars(&lvars);
2471         lprocfs_obd_setup(obd, lvars.obd_vars);
2472         sptlrpc_lprocfs_cliobd_attach(obd);
2473         ptlrpc_lprocfs_register_obd(obd);
2474
2475         ns_register_cancel(obd->obd_namespace, mdc_cancel_for_recovery);
2476
2477         obd->obd_namespace->ns_lvbo = &inode_lvbo;
2478
2479         rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2480         if (rc) {
2481                 mdc_cleanup(obd);
2482                 CERROR("failed to setup llogging subsystems\n");
2483         }
2484
2485         RETURN(rc);
2486
2487 err_close_lock:
2488         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2489 err_rpc_lock:
2490         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2491         ptlrpcd_decref();
2492         RETURN(rc);
2493 }
2494
2495 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
2496  * us to make MDS RPCs with large enough reply buffers to hold the
2497  * maximum-sized (= maximum striped) EA and cookie without having to
2498  * calculate this (via a call into the LOV + OSCs) each time we make an RPC. */
2499 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2500                      int def_easize, int cookiesize)
2501 {
2502         struct obd_device *obd = exp->exp_obd;
2503         struct client_obd *cli = &obd->u.cli;
2504         ENTRY;
2505
2506         if (cli->cl_max_mds_easize < easize)
2507                 cli->cl_max_mds_easize = easize;
2508
2509         if (cli->cl_default_mds_easize < def_easize)
2510                 cli->cl_default_mds_easize = def_easize;
2511
2512         if (cli->cl_max_mds_cookiesize < cookiesize)
2513                 cli->cl_max_mds_cookiesize = cookiesize;
2514
2515         RETURN(0);
2516 }
2517
2518 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2519 {
2520         int rc = 0;
2521         ENTRY;
2522
2523         switch (stage) {
2524         case OBD_CLEANUP_EARLY:
2525                 break;
2526         case OBD_CLEANUP_EXPORTS:
2527                 /* Failsafe, ok if racy */
2528                 if (obd->obd_type->typ_refcnt <= 1)
2529                         libcfs_kkuc_group_rem(0, KUC_GRP_HSM);
2530
2531                 obd_cleanup_client_import(obd);
2532                 ptlrpc_lprocfs_unregister_obd(obd);
2533                 lprocfs_obd_cleanup(obd);
2534
2535                 rc = obd_llog_finish(obd, 0);
2536                 if (rc != 0)
2537                         CERROR("failed to cleanup llogging subsystems\n");
2538                 break;
2539         }
2540         RETURN(rc);
2541 }
2542
2543 static int mdc_cleanup(struct obd_device *obd)
2544 {
2545         struct client_obd *cli = &obd->u.cli;
2546
2547         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2548         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2549
2550         ptlrpcd_decref();
2551
2552         return client_obd_cleanup(obd);
2553 }
2554
2555
2556 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2557                          struct obd_device *tgt, int *index)
2558 {
2559         struct llog_ctxt        *ctxt;
2560         int                      rc;
2561
2562         ENTRY;
2563
2564         LASSERT(olg == &obd->obd_olg);
2565
2566         rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2567                         &llog_client_ops);
2568         if (rc)
2569                 RETURN(rc);
2570
2571         ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2572         llog_initiator_connect(ctxt);
2573         llog_ctxt_put(ctxt);
2574
2575         RETURN(0);
2576 }
2577
2578 static int mdc_llog_finish(struct obd_device *obd, int count)
2579 {
2580         struct llog_ctxt *ctxt;
2581
2582         ENTRY;
2583
2584         ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
2585         if (ctxt)
2586                 llog_cleanup(NULL, ctxt);
2587
2588         RETURN(0);
2589 }
2590
2591 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
2592 {
2593         struct lustre_cfg *lcfg = buf;
2594         struct lprocfs_static_vars lvars = { 0 };
2595         int rc = 0;
2596
2597         lprocfs_mdc_init_vars(&lvars);
2598         switch (lcfg->lcfg_command) {
2599         default:
2600                 rc = class_process_proc_param(PARAM_MDC, lvars.obd_vars,
2601                                               lcfg, obd);
2602                 if (rc > 0)
2603                         rc = 0;
2604                 break;
2605         }
2606         return(rc);
2607 }
2608
2609
2610 /* get remote permission for current user on fid */
2611 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
2612                         struct obd_capa *oc, __u32 suppgid,
2613                         struct ptlrpc_request **request)
2614 {
2615         struct ptlrpc_request  *req;
2616         int                    rc;
2617         ENTRY;
2618
2619         LASSERT(client_is_remote(exp));
2620
2621         *request = NULL;
2622         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
2623         if (req == NULL)
2624                 RETURN(-ENOMEM);
2625
2626         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2627
2628         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
2629         if (rc) {
2630                 ptlrpc_request_free(req);
2631                 RETURN(rc);
2632         }
2633
2634         mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
2635
2636         req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
2637                              sizeof(struct mdt_remote_perm));
2638
2639         ptlrpc_request_set_replen(req);
2640
2641         rc = ptlrpc_queue_wait(req);
2642         if (rc)
2643                 ptlrpc_req_finished(req);
2644         else
2645                 *request = req;
2646         RETURN(rc);
2647 }
2648
2649 static int mdc_interpret_renew_capa(const struct lu_env *env,
2650                                     struct ptlrpc_request *req, void *args,
2651                                     int status)
2652 {
2653         struct mdc_renew_capa_args *ra = args;
2654         struct mdt_body *body = NULL;
2655         struct lustre_capa *capa;
2656         ENTRY;
2657
2658         if (status)
2659                 GOTO(out, capa = ERR_PTR(status));
2660
2661         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
2662         if (body == NULL)
2663                 GOTO(out, capa = ERR_PTR(-EFAULT));
2664
2665         if ((body->valid & OBD_MD_FLOSSCAPA) == 0)
2666                 GOTO(out, capa = ERR_PTR(-ENOENT));
2667
2668         capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
2669         if (!capa)
2670                 GOTO(out, capa = ERR_PTR(-EFAULT));
2671         EXIT;
2672 out:
2673         ra->ra_cb(ra->ra_oc, capa);
2674         return 0;
2675 }
2676
2677 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2678                           renew_capa_cb_t cb)
2679 {
2680         struct ptlrpc_request *req;
2681         struct mdc_renew_capa_args *ra;
2682         ENTRY;
2683
2684         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
2685                                         LUSTRE_MDS_VERSION, MDS_GETATTR);
2686         if (req == NULL)
2687                 RETURN(-ENOMEM);
2688
2689         /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
2690          * capa to renew is oss capa.
2691          */
2692         mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
2693         ptlrpc_request_set_replen(req);
2694
2695         CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
2696         ra = ptlrpc_req_async_args(req);
2697         ra->ra_oc = oc;
2698         ra->ra_cb = cb;
2699         req->rq_interpret_reply = mdc_interpret_renew_capa;
2700         ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2701         RETURN(0);
2702 }
2703
2704 static int mdc_connect(const struct lu_env *env,
2705                        struct obd_export **exp,
2706                        struct obd_device *obd, struct obd_uuid *cluuid,
2707                        struct obd_connect_data *data,
2708                        void *localdata)
2709 {
2710         struct obd_import *imp = obd->u.cli.cl_import;
2711
2712         /* mds-mds import features */
2713         if (data && (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS)) {
2714                 spin_lock(&imp->imp_lock);
2715                 imp->imp_server_timeout = 1;
2716                 spin_unlock(&imp->imp_lock);
2717                 imp->imp_client->cli_request_portal = MDS_MDS_PORTAL;
2718                 CDEBUG(D_OTHER, "%s: Set 'mds' portal and timeout\n",
2719                        obd->obd_name);
2720         }
2721
2722         return client_connect_import(env, exp, obd, cluuid, data, NULL);
2723 }
2724
2725 struct obd_ops mdc_obd_ops = {
2726         .o_owner            = THIS_MODULE,
2727         .o_setup            = mdc_setup,
2728         .o_precleanup       = mdc_precleanup,
2729         .o_cleanup          = mdc_cleanup,
2730         .o_add_conn         = client_import_add_conn,
2731         .o_del_conn         = client_import_del_conn,
2732         .o_connect          = mdc_connect,
2733         .o_disconnect       = client_disconnect_export,
2734         .o_iocontrol        = mdc_iocontrol,
2735         .o_set_info_async   = mdc_set_info_async,
2736         .o_statfs           = mdc_statfs,
2737         .o_pin              = mdc_pin,
2738         .o_unpin            = mdc_unpin,
2739         .o_fid_init         = client_fid_init,
2740         .o_fid_fini         = client_fid_fini,
2741         .o_fid_alloc        = mdc_fid_alloc,
2742         .o_import_event     = mdc_import_event,
2743         .o_llog_init        = mdc_llog_init,
2744         .o_llog_finish      = mdc_llog_finish,
2745         .o_get_info         = mdc_get_info,
2746         .o_process_config   = mdc_process_config,
2747         .o_get_uuid         = mdc_get_uuid,
2748         .o_quotactl         = mdc_quotactl,
2749         .o_quotacheck       = mdc_quotacheck
2750 };
2751
2752 struct md_ops mdc_md_ops = {
2753         .m_getstatus        = mdc_getstatus,
2754         .m_null_inode       = mdc_null_inode,
2755         .m_find_cbdata      = mdc_find_cbdata,
2756         .m_close            = mdc_close,
2757         .m_create           = mdc_create,
2758         .m_done_writing     = mdc_done_writing,
2759         .m_enqueue          = mdc_enqueue,
2760         .m_getattr          = mdc_getattr,
2761         .m_getattr_name     = mdc_getattr_name,
2762         .m_intent_lock      = mdc_intent_lock,
2763         .m_link             = mdc_link,
2764         .m_is_subdir        = mdc_is_subdir,
2765         .m_rename           = mdc_rename,
2766         .m_setattr          = mdc_setattr,
2767         .m_setxattr         = mdc_setxattr,
2768         .m_getxattr         = mdc_getxattr,
2769         .m_sync             = mdc_sync,
2770         .m_readpage         = mdc_readpage,
2771         .m_unlink           = mdc_unlink,
2772         .m_cancel_unused    = mdc_cancel_unused,
2773         .m_init_ea_size     = mdc_init_ea_size,
2774         .m_set_lock_data    = mdc_set_lock_data,
2775         .m_lock_match       = mdc_lock_match,
2776         .m_get_lustre_md    = mdc_get_lustre_md,
2777         .m_free_lustre_md   = mdc_free_lustre_md,
2778         .m_set_open_replay_data = mdc_set_open_replay_data,
2779         .m_clear_open_replay_data = mdc_clear_open_replay_data,
2780         .m_renew_capa       = mdc_renew_capa,
2781         .m_unpack_capa      = mdc_unpack_capa,
2782         .m_get_remote_perm  = mdc_get_remote_perm,
2783         .m_intent_getattr_async = mdc_intent_getattr_async,
2784         .m_revalidate_lock      = mdc_revalidate_lock
2785 };
2786
2787 int __init mdc_init(void)
2788 {
2789         int rc;
2790         struct lprocfs_static_vars lvars = { 0 };
2791         lprocfs_mdc_init_vars(&lvars);
2792
2793         rc = class_register_type(&mdc_obd_ops, &mdc_md_ops, lvars.module_vars,
2794                                  LUSTRE_MDC_NAME, NULL);
2795         RETURN(rc);
2796 }
2797
2798 #ifdef __KERNEL__
2799 static void /*__exit*/ mdc_exit(void)
2800 {
2801         class_unregister_type(LUSTRE_MDC_NAME);
2802 }
2803
2804 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
2805 MODULE_DESCRIPTION("Lustre Metadata Client");
2806 MODULE_LICENSE("GPL");
2807
2808 module_init(mdc_init);
2809 module_exit(mdc_exit);
2810 #endif