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