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