Whamcloud - gitweb
80bf99b29a600e68f74a9aa38ceba06a383ac051
[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 #define D_CHANGELOG 0
1999
2000 struct changelog_show {
2001         __u64           cs_startrec;
2002         __u32           cs_flags;
2003         struct file     *cs_fp;
2004         char            *cs_buf;
2005         struct obd_device *cs_obd;
2006 };
2007
2008 static int changelog_kkuc_cb(const struct lu_env *env, struct llog_handle *llh,
2009                              struct llog_rec_hdr *hdr, void *data)
2010 {
2011         struct changelog_show *cs = data;
2012         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
2013         struct kuc_hdr *lh;
2014         int len, rc;
2015         ENTRY;
2016
2017         if (rec->cr_hdr.lrh_type != CHANGELOG_REC) {
2018                 rc = -EINVAL;
2019                 CERROR("%s: not a changelog rec %x/%d: rc = %d\n",
2020                        cs->cs_obd->obd_name, rec->cr_hdr.lrh_type,
2021                        rec->cr.cr_type, rc);
2022                 RETURN(rc);
2023         }
2024
2025         if (rec->cr.cr_index < cs->cs_startrec) {
2026                 /* Skip entries earlier than what we are interested in */
2027                 CDEBUG(D_CHANGELOG, "rec="LPU64" start="LPU64"\n",
2028                        rec->cr.cr_index, cs->cs_startrec);
2029                 RETURN(0);
2030         }
2031
2032         CDEBUG(D_CHANGELOG, LPU64" %02d%-5s "LPU64" 0x%x t="DFID" p="DFID
2033                 " %.*s\n", rec->cr.cr_index, rec->cr.cr_type,
2034                 changelog_type2str(rec->cr.cr_type), rec->cr.cr_time,
2035                 rec->cr.cr_flags & CLF_FLAGMASK,
2036                 PFID(&rec->cr.cr_tfid), PFID(&rec->cr.cr_pfid),
2037                 rec->cr.cr_namelen, changelog_rec_name(&rec->cr));
2038
2039         len = sizeof(*lh) + changelog_rec_size(&rec->cr) + rec->cr.cr_namelen;
2040
2041         /* Set up the message */
2042         lh = changelog_kuc_hdr(cs->cs_buf, len, cs->cs_flags);
2043         memcpy(lh + 1, &rec->cr, len - sizeof(*lh));
2044
2045         rc = libcfs_kkuc_msg_put(cs->cs_fp, lh);
2046         CDEBUG(D_CHANGELOG, "kucmsg fp %p len %d rc %d\n", cs->cs_fp, len,rc);
2047
2048         RETURN(rc);
2049 }
2050
2051 static int mdc_changelog_send_thread(void *csdata)
2052 {
2053         struct changelog_show *cs = csdata;
2054         struct llog_ctxt *ctxt = NULL;
2055         struct llog_handle *llh = NULL;
2056         struct kuc_hdr *kuch;
2057         int rc;
2058
2059         CDEBUG(D_CHANGELOG, "changelog to fp=%p start "LPU64"\n",
2060                cs->cs_fp, cs->cs_startrec);
2061
2062         OBD_ALLOC(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
2063         if (cs->cs_buf == NULL)
2064                 GOTO(out, rc = -ENOMEM);
2065
2066         /* Set up the remote catalog handle */
2067         ctxt = llog_get_context(cs->cs_obd, LLOG_CHANGELOG_REPL_CTXT);
2068         if (ctxt == NULL)
2069                 GOTO(out, rc = -ENOENT);
2070         rc = llog_open(NULL, ctxt, &llh, NULL, CHANGELOG_CATALOG,
2071                        LLOG_OPEN_EXISTS);
2072         if (rc) {
2073                 CERROR("%s: fail to open changelog catalog: rc = %d\n",
2074                        cs->cs_obd->obd_name, rc);
2075                 GOTO(out, rc);
2076         }
2077         rc = llog_init_handle(NULL, llh, LLOG_F_IS_CAT, NULL);
2078         if (rc) {
2079                 CERROR("llog_init_handle failed %d\n", rc);
2080                 GOTO(out, rc);
2081         }
2082
2083         rc = llog_cat_process(NULL, llh, changelog_kkuc_cb, cs, 0, 0);
2084
2085         /* Send EOF no matter what our result */
2086         if ((kuch = changelog_kuc_hdr(cs->cs_buf, sizeof(*kuch),
2087                                       cs->cs_flags))) {
2088                 kuch->kuc_msgtype = CL_EOF;
2089                 libcfs_kkuc_msg_put(cs->cs_fp, kuch);
2090         }
2091
2092 out:
2093         fput(cs->cs_fp);
2094         if (llh)
2095                 llog_cat_close(NULL, llh);
2096         if (ctxt)
2097                 llog_ctxt_put(ctxt);
2098         if (cs->cs_buf)
2099                 OBD_FREE(cs->cs_buf, KUC_CHANGELOG_MSG_MAXSIZE);
2100         OBD_FREE_PTR(cs);
2101         return rc;
2102 }
2103
2104 static int mdc_ioc_changelog_send(struct obd_device *obd,
2105                                   struct ioc_changelog *icc)
2106 {
2107         struct changelog_show *cs;
2108         struct task_struct *task;
2109         int rc;
2110
2111         /* Freed in mdc_changelog_send_thread */
2112         OBD_ALLOC_PTR(cs);
2113         if (!cs)
2114                 return -ENOMEM;
2115
2116         cs->cs_obd = obd;
2117         cs->cs_startrec = icc->icc_recno;
2118         /* matching fput in mdc_changelog_send_thread */
2119         cs->cs_fp = fget(icc->icc_id);
2120         cs->cs_flags = icc->icc_flags;
2121
2122         /*
2123          * New thread because we should return to user app before
2124          * writing into our pipe
2125          */
2126         task = kthread_run(mdc_changelog_send_thread, cs,
2127                            "mdc_clg_send_thread");
2128         if (IS_ERR(task)) {
2129                 rc = PTR_ERR(task);
2130                 CERROR("%s: cannot start changelog thread: rc = %d\n",
2131                        obd->obd_name, rc);
2132                 OBD_FREE_PTR(cs);
2133         } else {
2134                 rc = 0;
2135                 CDEBUG(D_CHANGELOG, "%s: started changelog thread\n",
2136                        obd->obd_name);
2137         }
2138
2139         return rc;
2140 }
2141
2142 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2143                                 struct lustre_kernelcomm *lk);
2144
2145 static int mdc_quotacheck(struct obd_device *unused, struct obd_export *exp,
2146                           struct obd_quotactl *oqctl)
2147 {
2148         struct client_obd       *cli = &exp->exp_obd->u.cli;
2149         struct ptlrpc_request   *req;
2150         struct obd_quotactl     *body;
2151         int                      rc;
2152         ENTRY;
2153
2154         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
2155                                         &RQF_MDS_QUOTACHECK, LUSTRE_MDS_VERSION,
2156                                         MDS_QUOTACHECK);
2157         if (req == NULL)
2158                 RETURN(-ENOMEM);
2159
2160         body = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2161         *body = *oqctl;
2162
2163         ptlrpc_request_set_replen(req);
2164
2165         /* the next poll will find -ENODATA, that means quotacheck is
2166          * going on */
2167         cli->cl_qchk_stat = -ENODATA;
2168         rc = ptlrpc_queue_wait(req);
2169         if (rc)
2170                 cli->cl_qchk_stat = rc;
2171         ptlrpc_req_finished(req);
2172         RETURN(rc);
2173 }
2174
2175 static int mdc_quota_poll_check(struct obd_export *exp,
2176                                 struct if_quotacheck *qchk)
2177 {
2178         struct client_obd *cli = &exp->exp_obd->u.cli;
2179         int rc;
2180         ENTRY;
2181
2182         qchk->obd_uuid = cli->cl_target_uuid;
2183         memcpy(qchk->obd_type, LUSTRE_MDS_NAME, strlen(LUSTRE_MDS_NAME));
2184
2185         rc = cli->cl_qchk_stat;
2186         /* the client is not the previous one */
2187         if (rc == CL_NOT_QUOTACHECKED)
2188                 rc = -EINTR;
2189         RETURN(rc);
2190 }
2191
2192 static int mdc_quotactl(struct obd_device *unused, struct obd_export *exp,
2193                         struct obd_quotactl *oqctl)
2194 {
2195         struct ptlrpc_request   *req;
2196         struct obd_quotactl     *oqc;
2197         int                      rc;
2198         ENTRY;
2199
2200         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
2201                                         &RQF_MDS_QUOTACTL, LUSTRE_MDS_VERSION,
2202                                         MDS_QUOTACTL);
2203         if (req == NULL)
2204                 RETURN(-ENOMEM);
2205
2206         oqc = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2207         *oqc = *oqctl;
2208
2209         ptlrpc_request_set_replen(req);
2210         ptlrpc_at_set_req_timeout(req);
2211         req->rq_no_resend = 1;
2212
2213         rc = ptlrpc_queue_wait(req);
2214         if (rc)
2215                 CERROR("ptlrpc_queue_wait failed, rc: %d\n", rc);
2216
2217         if (req->rq_repmsg &&
2218             (oqc = req_capsule_server_get(&req->rq_pill, &RMF_OBD_QUOTACTL))) {
2219                 *oqctl = *oqc;
2220         } else if (!rc) {
2221                 CERROR ("Can't unpack obd_quotactl\n");
2222                 rc = -EPROTO;
2223         }
2224         ptlrpc_req_finished(req);
2225
2226         RETURN(rc);
2227 }
2228
2229 static int mdc_ioc_swap_layouts(struct obd_export *exp,
2230                                 struct md_op_data *op_data)
2231 {
2232         struct list_head cancels = LIST_HEAD_INIT(cancels);
2233         struct ptlrpc_request   *req;
2234         int                      rc, count;
2235         struct mdc_swap_layouts *msl, *payload;
2236         ENTRY;
2237
2238         msl = op_data->op_data;
2239
2240         /* When the MDT will get the MDS_SWAP_LAYOUTS RPC the
2241          * first thing it will do is to cancel the 2 layout
2242          * locks hold by this client.
2243          * So the client must cancel its layout locks on the 2 fids
2244          * with the request RPC to avoid extra RPC round trips
2245          */
2246         count = mdc_resource_get_unused(exp, &op_data->op_fid1, &cancels,
2247                                         LCK_EX, MDS_INODELOCK_LAYOUT |
2248                                         MDS_INODELOCK_XATTR);
2249         count += mdc_resource_get_unused(exp, &op_data->op_fid2, &cancels,
2250                                          LCK_EX, MDS_INODELOCK_LAYOUT |
2251                                          MDS_INODELOCK_XATTR);
2252
2253         req = ptlrpc_request_alloc(class_exp2cliimp(exp),
2254                                    &RQF_MDS_SWAP_LAYOUTS);
2255         if (req == NULL) {
2256                 ldlm_lock_list_put(&cancels, l_bl_ast, count);
2257                 RETURN(-ENOMEM);
2258         }
2259
2260         mdc_set_capa_size(req, &RMF_CAPA1, op_data->op_capa1);
2261         mdc_set_capa_size(req, &RMF_CAPA2, op_data->op_capa2);
2262
2263         rc = mdc_prep_elc_req(exp, req, MDS_SWAP_LAYOUTS, &cancels, count);
2264         if (rc) {
2265                 ptlrpc_request_free(req);
2266                 RETURN(rc);
2267         }
2268
2269         mdc_swap_layouts_pack(req, op_data);
2270
2271         payload = req_capsule_client_get(&req->rq_pill, &RMF_SWAP_LAYOUTS);
2272         LASSERT(payload);
2273
2274         *payload = *msl;
2275
2276         ptlrpc_request_set_replen(req);
2277
2278         rc = ptlrpc_queue_wait(req);
2279         if (rc)
2280                 GOTO(out, rc);
2281         EXIT;
2282
2283 out:
2284         ptlrpc_req_finished(req);
2285         return rc;
2286 }
2287
2288 static int mdc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2289                          void *karg, void *uarg)
2290 {
2291         struct obd_device *obd = exp->exp_obd;
2292         struct obd_ioctl_data *data = karg;
2293         struct obd_import *imp = obd->u.cli.cl_import;
2294         int rc;
2295         ENTRY;
2296
2297         if (!try_module_get(THIS_MODULE)) {
2298                 CERROR("%s: cannot get module '%s'\n", obd->obd_name,
2299                        module_name(THIS_MODULE));
2300                 return -EINVAL;
2301         }
2302         switch (cmd) {
2303         case OBD_IOC_CHANGELOG_SEND:
2304                 rc = mdc_ioc_changelog_send(obd, karg);
2305                 GOTO(out, rc);
2306         case OBD_IOC_CHANGELOG_CLEAR: {
2307                 struct ioc_changelog *icc = karg;
2308                 struct changelog_setinfo cs =
2309                         {.cs_recno = icc->icc_recno, .cs_id = icc->icc_id};
2310                 rc = obd_set_info_async(NULL, exp, strlen(KEY_CHANGELOG_CLEAR),
2311                                         KEY_CHANGELOG_CLEAR, sizeof(cs), &cs,
2312                                         NULL);
2313                 GOTO(out, rc);
2314         }
2315         case OBD_IOC_FID2PATH:
2316                 rc = mdc_ioc_fid2path(exp, karg);
2317                 GOTO(out, rc);
2318         case LL_IOC_HSM_CT_START:
2319                 rc = mdc_ioc_hsm_ct_start(exp, karg);
2320                 /* ignore if it was already registered on this MDS. */
2321                 if (rc == -EEXIST)
2322                         rc = 0;
2323                 GOTO(out, rc);
2324         case LL_IOC_HSM_PROGRESS:
2325                 rc = mdc_ioc_hsm_progress(exp, karg);
2326                 GOTO(out, rc);
2327         case LL_IOC_HSM_STATE_GET:
2328                 rc = mdc_ioc_hsm_state_get(exp, karg);
2329                 GOTO(out, rc);
2330         case LL_IOC_HSM_STATE_SET:
2331                 rc = mdc_ioc_hsm_state_set(exp, karg);
2332                 GOTO(out, rc);
2333         case LL_IOC_HSM_ACTION:
2334                 rc = mdc_ioc_hsm_current_action(exp, karg);
2335                 GOTO(out, rc);
2336         case LL_IOC_HSM_REQUEST:
2337                 rc = mdc_ioc_hsm_request(exp, karg);
2338                 GOTO(out, rc);
2339         case OBD_IOC_CLIENT_RECOVER:
2340                 rc = ptlrpc_recover_import(imp, data->ioc_inlbuf1, 0);
2341                 if (rc < 0)
2342                         GOTO(out, rc);
2343                 GOTO(out, rc = 0);
2344         case IOC_OSC_SET_ACTIVE:
2345                 rc = ptlrpc_set_import_active(imp, data->ioc_offset);
2346                 GOTO(out, rc);
2347         case OBD_IOC_POLL_QUOTACHECK:
2348                 rc = mdc_quota_poll_check(exp, (struct if_quotacheck *)karg);
2349                 GOTO(out, rc);
2350         case OBD_IOC_PING_TARGET:
2351                 rc = ptlrpc_obd_ping(obd);
2352                 GOTO(out, rc);
2353         /*
2354          * Normally IOC_OBD_STATFS, OBD_IOC_QUOTACTL iocontrol are handled by
2355          * LMV instead of MDC. But when the cluster is upgraded from 1.8,
2356          * there'd be no LMV layer thus we might be called here. Eventually
2357          * this code should be removed.
2358          * bz20731, LU-592.
2359          */
2360         case IOC_OBD_STATFS: {
2361                 struct obd_statfs stat_buf = {0};
2362
2363                 if (*((__u32 *) data->ioc_inlbuf2) != 0)
2364                         GOTO(out, rc = -ENODEV);
2365
2366                 /* copy UUID */
2367                 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(obd),
2368                                  min((int)data->ioc_plen2,
2369                                      (int)sizeof(struct obd_uuid))))
2370                         GOTO(out, rc = -EFAULT);
2371
2372                 rc = mdc_statfs(NULL, obd->obd_self_export, &stat_buf,
2373                                 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
2374                                 0);
2375                 if (rc != 0)
2376                         GOTO(out, rc);
2377
2378                 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
2379                                      min((int) data->ioc_plen1,
2380                                          (int) sizeof(stat_buf))))
2381                         GOTO(out, rc = -EFAULT);
2382
2383                 GOTO(out, rc = 0);
2384         }
2385         case OBD_IOC_QUOTACTL: {
2386                 struct if_quotactl *qctl = karg;
2387                 struct obd_quotactl *oqctl;
2388
2389                 OBD_ALLOC_PTR(oqctl);
2390                 if (oqctl == NULL)
2391                         GOTO(out, rc = -ENOMEM);
2392
2393                 QCTL_COPY(oqctl, qctl);
2394                 rc = obd_quotactl(exp, oqctl);
2395                 if (rc == 0) {
2396                         QCTL_COPY(qctl, oqctl);
2397                         qctl->qc_valid = QC_MDTIDX;
2398                         qctl->obd_uuid = obd->u.cli.cl_target_uuid;
2399                 }
2400
2401                 OBD_FREE_PTR(oqctl);
2402                 GOTO(out, rc);
2403         }
2404         case LL_IOC_GET_CONNECT_FLAGS:
2405                 if (copy_to_user(uarg, exp_connect_flags_ptr(exp),
2406                                  sizeof(*exp_connect_flags_ptr(exp))))
2407                         GOTO(out, rc = -EFAULT);
2408
2409                 GOTO(out, rc = 0);
2410         case LL_IOC_LOV_SWAP_LAYOUTS:
2411                 rc = mdc_ioc_swap_layouts(exp, karg);
2412                 GOTO(out, rc);
2413         default:
2414                 CERROR("unrecognised ioctl: cmd = %#x\n", cmd);
2415                 GOTO(out, rc = -ENOTTY);
2416         }
2417 out:
2418         module_put(THIS_MODULE);
2419
2420         return rc;
2421 }
2422
2423 int mdc_get_info_rpc(struct obd_export *exp,
2424                      obd_count keylen, void *key,
2425                      int vallen, void *val)
2426 {
2427         struct obd_import      *imp = class_exp2cliimp(exp);
2428         struct ptlrpc_request  *req;
2429         char                   *tmp;
2430         int                     rc = -EINVAL;
2431         ENTRY;
2432
2433         req = ptlrpc_request_alloc(imp, &RQF_MDS_GET_INFO);
2434         if (req == NULL)
2435                 RETURN(-ENOMEM);
2436
2437         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_KEY,
2438                              RCL_CLIENT, keylen);
2439         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VALLEN,
2440                              RCL_CLIENT, sizeof(__u32));
2441
2442         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GET_INFO);
2443         if (rc) {
2444                 ptlrpc_request_free(req);
2445                 RETURN(rc);
2446         }
2447
2448         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_KEY);
2449         memcpy(tmp, key, keylen);
2450         tmp = req_capsule_client_get(&req->rq_pill, &RMF_GETINFO_VALLEN);
2451         memcpy(tmp, &vallen, sizeof(__u32));
2452
2453         req_capsule_set_size(&req->rq_pill, &RMF_GETINFO_VAL,
2454                              RCL_SERVER, vallen);
2455         ptlrpc_request_set_replen(req);
2456
2457         rc = ptlrpc_queue_wait(req);
2458         /* -EREMOTE means the get_info result is partial, and it needs to
2459          * continue on another MDT, see fid2path part in lmv_iocontrol */
2460         if (rc == 0 || rc == -EREMOTE) {
2461                 tmp = req_capsule_server_get(&req->rq_pill, &RMF_GETINFO_VAL);
2462                 memcpy(val, tmp, vallen);
2463                 if (ptlrpc_rep_need_swab(req)) {
2464                         if (KEY_IS(KEY_FID2PATH))
2465                                 lustre_swab_fid2path(val);
2466                 }
2467         }
2468         ptlrpc_req_finished(req);
2469
2470         RETURN(rc);
2471 }
2472
2473 static void lustre_swab_hai(struct hsm_action_item *h)
2474 {
2475         __swab32s(&h->hai_len);
2476         __swab32s(&h->hai_action);
2477         lustre_swab_lu_fid(&h->hai_fid);
2478         lustre_swab_lu_fid(&h->hai_dfid);
2479         __swab64s(&h->hai_cookie);
2480         __swab64s(&h->hai_extent.offset);
2481         __swab64s(&h->hai_extent.length);
2482         __swab64s(&h->hai_gid);
2483 }
2484
2485 static void lustre_swab_hal(struct hsm_action_list *h)
2486 {
2487         struct hsm_action_item  *hai;
2488         int                      i;
2489
2490         __swab32s(&h->hal_version);
2491         __swab32s(&h->hal_count);
2492         __swab32s(&h->hal_archive_id);
2493         __swab64s(&h->hal_flags);
2494         hai = hai_first(h);
2495         for (i = 0; i < h->hal_count; i++, hai = hai_next(hai))
2496                 lustre_swab_hai(hai);
2497 }
2498
2499 static void lustre_swab_kuch(struct kuc_hdr *l)
2500 {
2501         __swab16s(&l->kuc_magic);
2502         /* __u8 l->kuc_transport */
2503         __swab16s(&l->kuc_msgtype);
2504         __swab16s(&l->kuc_msglen);
2505 }
2506
2507 static int mdc_ioc_hsm_ct_start(struct obd_export *exp,
2508                                 struct lustre_kernelcomm *lk)
2509 {
2510         struct obd_import  *imp = class_exp2cliimp(exp);
2511         __u32               archive = lk->lk_data;
2512         int                 rc = 0;
2513
2514         if (lk->lk_group != KUC_GRP_HSM) {
2515                 CERROR("Bad copytool group %d\n", lk->lk_group);
2516                 return -EINVAL;
2517         }
2518
2519         CDEBUG(D_HSM, "CT start r%d w%d u%d g%d f%#x\n", lk->lk_rfd, lk->lk_wfd,
2520                lk->lk_uid, lk->lk_group, lk->lk_flags);
2521
2522         if (lk->lk_flags & LK_FLG_STOP) {
2523                 /* Unregister with the coordinator */
2524                 rc = mdc_ioc_hsm_ct_unregister(imp);
2525         } else {
2526                 rc = mdc_ioc_hsm_ct_register(imp, archive);
2527         }
2528
2529         return rc;
2530 }
2531
2532 /**
2533  * Send a message to any listening copytools
2534  * @param val KUC message (kuc_hdr + hsm_action_list)
2535  * @param len total length of message
2536  */
2537 static int mdc_hsm_copytool_send(int len, void *val)
2538 {
2539         struct kuc_hdr          *lh = (struct kuc_hdr *)val;
2540         struct hsm_action_list  *hal = (struct hsm_action_list *)(lh + 1);
2541         int                      rc;
2542         ENTRY;
2543
2544         if (len < sizeof(*lh) + sizeof(*hal)) {
2545                 CERROR("Short HSM message %d < %d\n", len,
2546                        (int) (sizeof(*lh) + sizeof(*hal)));
2547                 RETURN(-EPROTO);
2548         }
2549         if (lh->kuc_magic == __swab16(KUC_MAGIC)) {
2550                 lustre_swab_kuch(lh);
2551                 lustre_swab_hal(hal);
2552         } else if (lh->kuc_magic != KUC_MAGIC) {
2553                 CERROR("Bad magic %x!=%x\n", lh->kuc_magic, KUC_MAGIC);
2554                 RETURN(-EPROTO);
2555         }
2556
2557         CDEBUG(D_HSM, " Received message mg=%x t=%d m=%d l=%d actions=%d "
2558                "on %s\n",
2559                lh->kuc_magic, lh->kuc_transport, lh->kuc_msgtype,
2560                lh->kuc_msglen, hal->hal_count, hal->hal_fsname);
2561
2562         /* Broadcast to HSM listeners */
2563         rc = libcfs_kkuc_group_put(KUC_GRP_HSM, lh);
2564
2565         RETURN(rc);
2566 }
2567
2568 /**
2569  * callback function passed to kuc for re-registering each HSM copytool
2570  * running on MDC, after MDT shutdown/recovery.
2571  * @param data copytool registration data
2572  * @param cb_arg callback argument (obd_import)
2573  */
2574 static int mdc_hsm_ct_reregister(void *data, void *cb_arg)
2575 {
2576         struct kkuc_ct_data     *kcd = data;
2577         struct obd_import       *imp = (struct obd_import *)cb_arg;
2578         int                      rc;
2579
2580         if (kcd == NULL || kcd->kcd_magic != KKUC_CT_DATA_MAGIC)
2581                 return -EPROTO;
2582
2583         if (!obd_uuid_equals(&kcd->kcd_uuid, &imp->imp_obd->obd_uuid))
2584                 return 0;
2585
2586         CDEBUG(D_HA, "%s: recover copytool registration to MDT (archive=%#x)\n",
2587                imp->imp_obd->obd_name, kcd->kcd_archive);
2588         rc = mdc_ioc_hsm_ct_register(imp, kcd->kcd_archive);
2589
2590         /* ignore error if the copytool is already registered */
2591         return (rc == -EEXIST) ? 0 : rc;
2592 }
2593
2594 /**
2595  * Re-establish all kuc contexts with MDT
2596  * after MDT shutdown/recovery.
2597  */
2598 static int mdc_kuc_reregister(struct obd_import *imp)
2599 {
2600         /* re-register HSM agents */
2601         return libcfs_kkuc_group_foreach(KUC_GRP_HSM, mdc_hsm_ct_reregister,
2602                                          (void *)imp);
2603 }
2604
2605 int mdc_set_info_async(const struct lu_env *env,
2606                        struct obd_export *exp,
2607                        obd_count keylen, void *key,
2608                        obd_count vallen, void *val,
2609                        struct ptlrpc_request_set *set)
2610 {
2611         struct obd_import       *imp = class_exp2cliimp(exp);
2612         int                      rc;
2613         ENTRY;
2614
2615         if (KEY_IS(KEY_READ_ONLY)) {
2616                 if (vallen != sizeof(int))
2617                         RETURN(-EINVAL);
2618
2619                 spin_lock(&imp->imp_lock);
2620                 if (*((int *)val)) {
2621                         imp->imp_connect_flags_orig |= OBD_CONNECT_RDONLY;
2622                         imp->imp_connect_data.ocd_connect_flags |=
2623                                                         OBD_CONNECT_RDONLY;
2624                 } else {
2625                         imp->imp_connect_flags_orig &= ~OBD_CONNECT_RDONLY;
2626                         imp->imp_connect_data.ocd_connect_flags &=
2627                                                         ~OBD_CONNECT_RDONLY;
2628                 }
2629                 spin_unlock(&imp->imp_lock);
2630
2631                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2632                                        keylen, key, vallen, val, set);
2633                 RETURN(rc);
2634         }
2635         if (KEY_IS(KEY_SPTLRPC_CONF)) {
2636                 sptlrpc_conf_client_adapt(exp->exp_obd);
2637                 RETURN(0);
2638         }
2639         if (KEY_IS(KEY_FLUSH_CTX)) {
2640                 sptlrpc_import_flush_my_ctx(imp);
2641                 RETURN(0);
2642         }
2643         if (KEY_IS(KEY_CHANGELOG_CLEAR)) {
2644                 rc = do_set_info_async(imp, MDS_SET_INFO, LUSTRE_MDS_VERSION,
2645                                        keylen, key, vallen, val, set);
2646                 RETURN(rc);
2647         }
2648         if (KEY_IS(KEY_HSM_COPYTOOL_SEND)) {
2649                 rc = mdc_hsm_copytool_send(vallen, val);
2650                 RETURN(rc);
2651         }
2652
2653         CERROR("Unknown key %s\n", (char *)key);
2654         RETURN(-EINVAL);
2655 }
2656
2657 int mdc_get_info(const struct lu_env *env, struct obd_export *exp,
2658                  __u32 keylen, void *key, __u32 *vallen, void *val,
2659                  struct lov_stripe_md *lsm)
2660 {
2661         int rc = -EINVAL;
2662
2663         if (KEY_IS(KEY_MAX_EASIZE)) {
2664                 int mdsize, *max_easize;
2665
2666                 if (*vallen != sizeof(int))
2667                         RETURN(-EINVAL);
2668                 mdsize = *(int *)val;
2669                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_easize)
2670                         exp->exp_obd->u.cli.cl_max_mds_easize = mdsize;
2671                 max_easize = val;
2672                 *max_easize = exp->exp_obd->u.cli.cl_max_mds_easize;
2673                 RETURN(0);
2674         } else if (KEY_IS(KEY_DEFAULT_EASIZE)) {
2675                 int *default_easize;
2676
2677                 if (*vallen != sizeof(int))
2678                         RETURN(-EINVAL);
2679                 default_easize = val;
2680                 *default_easize = exp->exp_obd->u.cli.cl_default_mds_easize;
2681                 RETURN(0);
2682         } else if (KEY_IS(KEY_MAX_COOKIESIZE)) {
2683                 int mdsize, *max_cookiesize;
2684
2685                 if (*vallen != sizeof(int))
2686                         RETURN(-EINVAL);
2687                 mdsize = *(int *)val;
2688                 if (mdsize > exp->exp_obd->u.cli.cl_max_mds_cookiesize)
2689                         exp->exp_obd->u.cli.cl_max_mds_cookiesize = mdsize;
2690                 max_cookiesize = val;
2691                 *max_cookiesize = exp->exp_obd->u.cli.cl_max_mds_cookiesize;
2692                 RETURN(0);
2693         } else if (KEY_IS(KEY_DEFAULT_COOKIESIZE)) {
2694                 int *default_cookiesize;
2695
2696                 if (*vallen != sizeof(int))
2697                         RETURN(-EINVAL);
2698                 default_cookiesize = val;
2699                 *default_cookiesize =
2700                         exp->exp_obd->u.cli.cl_default_mds_cookiesize;
2701                 RETURN(0);
2702         } else if (KEY_IS(KEY_CONN_DATA)) {
2703                 struct obd_import *imp = class_exp2cliimp(exp);
2704                 struct obd_connect_data *data = val;
2705
2706                 if (*vallen != sizeof(*data))
2707                         RETURN(-EINVAL);
2708
2709                 *data = imp->imp_connect_data;
2710                 RETURN(0);
2711         } else if (KEY_IS(KEY_TGT_COUNT)) {
2712                 *((int *)val) = 1;
2713                 RETURN(0);
2714         }
2715
2716         rc = mdc_get_info_rpc(exp, keylen, key, *vallen, val);
2717
2718         RETURN(rc);
2719 }
2720
2721 int mdc_fsync(struct obd_export *exp, const struct lu_fid *fid,
2722               struct obd_capa *oc, struct ptlrpc_request **request)
2723 {
2724         struct ptlrpc_request *req;
2725         int                    rc;
2726         ENTRY;
2727
2728         *request = NULL;
2729         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_SYNC);
2730         if (req == NULL)
2731                 RETURN(-ENOMEM);
2732
2733         mdc_set_capa_size(req, &RMF_CAPA1, oc);
2734
2735         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_SYNC);
2736         if (rc) {
2737                 ptlrpc_request_free(req);
2738                 RETURN(rc);
2739         }
2740
2741         mdc_pack_body(req, fid, oc, 0, 0, -1, 0);
2742
2743         ptlrpc_request_set_replen(req);
2744
2745         rc = ptlrpc_queue_wait(req);
2746         if (rc)
2747                 ptlrpc_req_finished(req);
2748         else
2749                 *request = req;
2750         RETURN(rc);
2751 }
2752
2753 static int mdc_import_event(struct obd_device *obd, struct obd_import *imp,
2754                             enum obd_import_event event)
2755 {
2756         int rc = 0;
2757
2758         LASSERT(imp->imp_obd == obd);
2759
2760         switch (event) {
2761         case IMP_EVENT_DISCON: {
2762 #if 0
2763                 /* XXX Pass event up to OBDs stack. used only for FLD now */
2764                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_DISCON, NULL);
2765 #endif
2766                 break;
2767         }
2768         case IMP_EVENT_INACTIVE: {
2769                 struct client_obd *cli = &obd->u.cli;
2770                 /*
2771                  * Flush current sequence to make client obtain new one
2772                  * from server in case of disconnect/reconnect.
2773                  */
2774                 if (cli->cl_seq != NULL)
2775                         seq_client_flush(cli->cl_seq);
2776
2777                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_INACTIVE, NULL);
2778                 break;
2779         }
2780         case IMP_EVENT_INVALIDATE: {
2781                 struct ldlm_namespace *ns = obd->obd_namespace;
2782
2783                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
2784
2785                 break;
2786         }
2787         case IMP_EVENT_ACTIVE:
2788                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_ACTIVE, NULL);
2789                 /* redo the kuc registration after reconnecting */
2790                 if (rc == 0)
2791                         rc = mdc_kuc_reregister(imp);
2792                 break;
2793         case IMP_EVENT_OCD:
2794                 rc = obd_notify_observer(obd, obd, OBD_NOTIFY_OCD, NULL);
2795                 break;
2796         case IMP_EVENT_DEACTIVATE:
2797         case IMP_EVENT_ACTIVATE:
2798                 break;
2799         default:
2800                 CERROR("Unknown import event %x\n", event);
2801                 LBUG();
2802         }
2803         RETURN(rc);
2804 }
2805
2806 int mdc_fid_alloc(const struct lu_env *env, struct obd_export *exp,
2807                   struct lu_fid *fid, struct md_op_data *op_data)
2808 {
2809         struct client_obd *cli = &exp->exp_obd->u.cli;
2810         struct lu_client_seq *seq = cli->cl_seq;
2811         ENTRY;
2812         RETURN(seq_client_alloc_fid(env, seq, fid));
2813 }
2814
2815 struct obd_uuid *mdc_get_uuid(struct obd_export *exp) {
2816         struct client_obd *cli = &exp->exp_obd->u.cli;
2817         return &cli->cl_target_uuid;
2818 }
2819
2820 /**
2821  * Determine whether the lock can be canceled before replaying it during
2822  * recovery, non zero value will be return if the lock can be canceled,
2823  * or zero returned for not
2824  */
2825 static int mdc_cancel_weight(struct ldlm_lock *lock)
2826 {
2827         if (lock->l_resource->lr_type != LDLM_IBITS)
2828                 RETURN(0);
2829
2830         /* FIXME: if we ever get into a situation where there are too many
2831          * opened files with open locks on a single node, then we really
2832          * should replay these open locks to reget it */
2833         if (lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_OPEN)
2834                 RETURN(0);
2835
2836         RETURN(1);
2837 }
2838
2839 static int mdc_resource_inode_free(struct ldlm_resource *res)
2840 {
2841         if (res->lr_lvb_inode)
2842                 res->lr_lvb_inode = NULL;
2843
2844         return 0;
2845 }
2846
2847 struct ldlm_valblock_ops inode_lvbo = {
2848         .lvbo_free = mdc_resource_inode_free
2849 };
2850
2851 static int mdc_setup(struct obd_device *obd, struct lustre_cfg *cfg)
2852 {
2853         struct client_obd               *cli = &obd->u.cli;
2854         int                             rc;
2855         ENTRY;
2856
2857         OBD_ALLOC(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2858         if (!cli->cl_rpc_lock)
2859                 RETURN(-ENOMEM);
2860         mdc_init_rpc_lock(cli->cl_rpc_lock);
2861
2862         rc = ptlrpcd_addref();
2863         if (rc < 0)
2864                 GOTO(err_rpc_lock, rc);
2865
2866         OBD_ALLOC(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2867         if (!cli->cl_close_lock)
2868                 GOTO(err_ptlrpcd_decref, rc = -ENOMEM);
2869         mdc_init_rpc_lock(cli->cl_close_lock);
2870
2871         rc = client_obd_setup(obd, cfg);
2872         if (rc)
2873                 GOTO(err_close_lock, rc);
2874 #ifdef LPROCFS
2875         obd->obd_vars = lprocfs_mdc_obd_vars;
2876         lprocfs_obd_setup(obd);
2877         lprocfs_alloc_md_stats(obd, 0);
2878 #endif
2879         sptlrpc_lprocfs_cliobd_attach(obd);
2880         ptlrpc_lprocfs_register_obd(obd);
2881
2882         ns_register_cancel(obd->obd_namespace, mdc_cancel_weight);
2883
2884         obd->obd_namespace->ns_lvbo = &inode_lvbo;
2885
2886         rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
2887         if (rc) {
2888                 mdc_cleanup(obd);
2889                 CERROR("failed to setup llogging subsystems\n");
2890         }
2891
2892         RETURN(rc);
2893
2894 err_close_lock:
2895         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2896 err_ptlrpcd_decref:
2897         ptlrpcd_decref();
2898 err_rpc_lock:
2899         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2900         RETURN(rc);
2901 }
2902
2903 /* Initialize the default and maximum LOV EA and cookie sizes.  This allows
2904  * us to make MDS RPCs with large enough reply buffers to hold a default
2905  * sized EA and cookie without having to calculate this (via a call into the
2906  * LOV + OSCs) each time we make an RPC.  The maximum size is also tracked
2907  * but not used to avoid wastefully vmalloc()'ing large reply buffers when
2908  * a large number of stripes is possible.  If a larger reply buffer is
2909  * required it will be reallocated in the ptlrpc layer due to overflow.
2910  */
2911 static int mdc_init_ea_size(struct obd_export *exp, int easize,
2912                             int def_easize, int cookiesize, int def_cookiesize)
2913 {
2914         struct obd_device *obd = exp->exp_obd;
2915         struct client_obd *cli = &obd->u.cli;
2916         ENTRY;
2917
2918         if (cli->cl_max_mds_easize < easize)
2919                 cli->cl_max_mds_easize = easize;
2920
2921         if (cli->cl_default_mds_easize < def_easize)
2922                 cli->cl_default_mds_easize = def_easize;
2923
2924         if (cli->cl_max_mds_cookiesize < cookiesize)
2925                 cli->cl_max_mds_cookiesize = cookiesize;
2926
2927         if (cli->cl_default_mds_cookiesize < def_cookiesize)
2928                 cli->cl_default_mds_cookiesize = def_cookiesize;
2929
2930         RETURN(0);
2931 }
2932
2933 static int mdc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2934 {
2935         int rc = 0;
2936         ENTRY;
2937
2938         switch (stage) {
2939         case OBD_CLEANUP_EARLY:
2940                 break;
2941         case OBD_CLEANUP_EXPORTS:
2942                 /* Failsafe, ok if racy */
2943                 if (obd->obd_type->typ_refcnt <= 1)
2944                         libcfs_kkuc_group_rem(0, KUC_GRP_HSM, NULL);
2945
2946                 obd_cleanup_client_import(obd);
2947                 ptlrpc_lprocfs_unregister_obd(obd);
2948                 lprocfs_obd_cleanup(obd);
2949                 lprocfs_free_md_stats(obd);
2950
2951                 rc = obd_llog_finish(obd, 0);
2952                 if (rc != 0)
2953                         CERROR("failed to cleanup llogging subsystems\n");
2954                 break;
2955         }
2956         RETURN(rc);
2957 }
2958
2959 static int mdc_cleanup(struct obd_device *obd)
2960 {
2961         struct client_obd *cli = &obd->u.cli;
2962
2963         OBD_FREE(cli->cl_rpc_lock, sizeof (*cli->cl_rpc_lock));
2964         OBD_FREE(cli->cl_close_lock, sizeof (*cli->cl_close_lock));
2965
2966         ptlrpcd_decref();
2967
2968         return client_obd_cleanup(obd);
2969 }
2970
2971
2972 static int mdc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2973                          struct obd_device *tgt, int *index)
2974 {
2975         struct llog_ctxt        *ctxt;
2976         int                      rc;
2977
2978         ENTRY;
2979
2980         LASSERT(olg == &obd->obd_olg);
2981
2982         rc = llog_setup(NULL, obd, olg, LLOG_CHANGELOG_REPL_CTXT, tgt,
2983                         &llog_client_ops);
2984         if (rc)
2985                 RETURN(rc);
2986
2987         ctxt = llog_group_get_ctxt(olg, LLOG_CHANGELOG_REPL_CTXT);
2988         llog_initiator_connect(ctxt);
2989         llog_ctxt_put(ctxt);
2990
2991         RETURN(0);
2992 }
2993
2994 static int mdc_llog_finish(struct obd_device *obd, int count)
2995 {
2996         struct llog_ctxt *ctxt;
2997
2998         ENTRY;
2999
3000         ctxt = llog_get_context(obd, LLOG_CHANGELOG_REPL_CTXT);
3001         if (ctxt)
3002                 llog_cleanup(NULL, ctxt);
3003
3004         RETURN(0);
3005 }
3006
3007 static int mdc_process_config(struct obd_device *obd, obd_count len, void *buf)
3008 {
3009         struct lustre_cfg *lcfg = buf;
3010         int rc = class_process_proc_param(PARAM_MDC, obd->obd_vars, lcfg, obd);
3011         return (rc > 0 ? 0: rc);
3012 }
3013
3014
3015 /* get remote permission for current user on fid */
3016 int mdc_get_remote_perm(struct obd_export *exp, const struct lu_fid *fid,
3017                         struct obd_capa *oc, __u32 suppgid,
3018                         struct ptlrpc_request **request)
3019 {
3020         struct ptlrpc_request  *req;
3021         int                    rc;
3022         ENTRY;
3023
3024         LASSERT(client_is_remote(exp));
3025
3026         *request = NULL;
3027         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_MDS_GETATTR);
3028         if (req == NULL)
3029                 RETURN(-ENOMEM);
3030
3031         mdc_set_capa_size(req, &RMF_CAPA1, oc);
3032
3033         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, MDS_GETATTR);
3034         if (rc) {
3035                 ptlrpc_request_free(req);
3036                 RETURN(rc);
3037         }
3038
3039         mdc_pack_body(req, fid, oc, OBD_MD_FLRMTPERM, 0, suppgid, 0);
3040
3041         req_capsule_set_size(&req->rq_pill, &RMF_ACL, RCL_SERVER,
3042                              sizeof(struct mdt_remote_perm));
3043
3044         ptlrpc_request_set_replen(req);
3045
3046         rc = ptlrpc_queue_wait(req);
3047         if (rc)
3048                 ptlrpc_req_finished(req);
3049         else
3050                 *request = req;
3051         RETURN(rc);
3052 }
3053
3054 static int mdc_interpret_renew_capa(const struct lu_env *env,
3055                                     struct ptlrpc_request *req, void *args,
3056                                     int status)
3057 {
3058         struct mdc_renew_capa_args *ra = args;
3059         struct mdt_body *body = NULL;
3060         struct lustre_capa *capa;
3061         ENTRY;
3062
3063         if (status)
3064                 GOTO(out, capa = ERR_PTR(status));
3065
3066         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
3067         if (body == NULL)
3068                 GOTO(out, capa = ERR_PTR(-EFAULT));
3069
3070         if ((body->mbo_valid & OBD_MD_FLOSSCAPA) == 0)
3071                 GOTO(out, capa = ERR_PTR(-ENOENT));
3072
3073         capa = req_capsule_server_get(&req->rq_pill, &RMF_CAPA2);
3074         if (!capa)
3075                 GOTO(out, capa = ERR_PTR(-EFAULT));
3076         EXIT;
3077 out:
3078         ra->ra_cb(ra->ra_oc, capa);
3079         return 0;
3080 }
3081
3082 static int mdc_renew_capa(struct obd_export *exp, struct obd_capa *oc,
3083                           renew_capa_cb_t cb)
3084 {
3085         struct ptlrpc_request *req;
3086         struct mdc_renew_capa_args *ra;
3087         ENTRY;
3088
3089         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp), &RQF_MDS_GETATTR,
3090                                         LUSTRE_MDS_VERSION, MDS_GETATTR);
3091         if (req == NULL)
3092                 RETURN(-ENOMEM);
3093
3094         /* NB, OBD_MD_FLOSSCAPA is set here, but it doesn't necessarily mean the
3095          * capa to renew is oss capa.
3096          */
3097         mdc_pack_body(req, &oc->c_capa.lc_fid, oc, OBD_MD_FLOSSCAPA, 0, -1, 0);
3098         ptlrpc_request_set_replen(req);
3099
3100         CLASSERT(sizeof(*ra) <= sizeof(req->rq_async_args));
3101         ra = ptlrpc_req_async_args(req);
3102         ra->ra_oc = oc;
3103         ra->ra_cb = cb;
3104         req->rq_interpret_reply = mdc_interpret_renew_capa;
3105         ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
3106         RETURN(0);
3107 }
3108
3109 struct obd_ops mdc_obd_ops = {
3110         .o_owner            = THIS_MODULE,
3111         .o_setup            = mdc_setup,
3112         .o_precleanup       = mdc_precleanup,
3113         .o_cleanup          = mdc_cleanup,
3114         .o_add_conn         = client_import_add_conn,
3115         .o_del_conn         = client_import_del_conn,
3116         .o_connect          = client_connect_import,
3117         .o_disconnect       = client_disconnect_export,
3118         .o_iocontrol        = mdc_iocontrol,
3119         .o_set_info_async   = mdc_set_info_async,
3120         .o_statfs           = mdc_statfs,
3121         .o_fid_init         = client_fid_init,
3122         .o_fid_fini         = client_fid_fini,
3123         .o_fid_alloc        = mdc_fid_alloc,
3124         .o_import_event     = mdc_import_event,
3125         .o_llog_init        = mdc_llog_init,
3126         .o_llog_finish      = mdc_llog_finish,
3127         .o_get_info         = mdc_get_info,
3128         .o_process_config   = mdc_process_config,
3129         .o_get_uuid         = mdc_get_uuid,
3130         .o_quotactl         = mdc_quotactl,
3131         .o_quotacheck       = mdc_quotacheck
3132 };
3133
3134 struct md_ops mdc_md_ops = {
3135         .m_getstatus        = mdc_getstatus,
3136         .m_null_inode       = mdc_null_inode,
3137         .m_find_cbdata      = mdc_find_cbdata,
3138         .m_close            = mdc_close,
3139         .m_create           = mdc_create,
3140         .m_done_writing     = mdc_done_writing,
3141         .m_enqueue          = mdc_enqueue,
3142         .m_getattr          = mdc_getattr,
3143         .m_getattr_name     = mdc_getattr_name,
3144         .m_intent_lock      = mdc_intent_lock,
3145         .m_link             = mdc_link,
3146         .m_rename           = mdc_rename,
3147         .m_setattr          = mdc_setattr,
3148         .m_setxattr         = mdc_setxattr,
3149         .m_getxattr         = mdc_getxattr,
3150         .m_fsync                = mdc_fsync,
3151         .m_read_page            = mdc_read_page,
3152         .m_unlink           = mdc_unlink,
3153         .m_cancel_unused    = mdc_cancel_unused,
3154         .m_init_ea_size     = mdc_init_ea_size,
3155         .m_set_lock_data    = mdc_set_lock_data,
3156         .m_lock_match       = mdc_lock_match,
3157         .m_get_lustre_md    = mdc_get_lustre_md,
3158         .m_free_lustre_md   = mdc_free_lustre_md,
3159         .m_set_open_replay_data = mdc_set_open_replay_data,
3160         .m_clear_open_replay_data = mdc_clear_open_replay_data,
3161         .m_renew_capa       = mdc_renew_capa,
3162         .m_unpack_capa      = mdc_unpack_capa,
3163         .m_get_remote_perm  = mdc_get_remote_perm,
3164         .m_intent_getattr_async = mdc_intent_getattr_async,
3165         .m_revalidate_lock      = mdc_revalidate_lock
3166 };
3167
3168 int __init mdc_init(void)
3169 {
3170         return class_register_type(&mdc_obd_ops, &mdc_md_ops, true, NULL,
3171 #ifndef HAVE_ONLY_PROCFS_SEQ
3172                                    NULL,
3173 #endif
3174                                    LUSTRE_MDC_NAME, NULL);
3175 }
3176
3177 static void /*__exit*/ mdc_exit(void)
3178 {
3179         class_unregister_type(LUSTRE_MDC_NAME);
3180 }
3181
3182 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3183 MODULE_DESCRIPTION("Lustre Metadata Client");
3184 MODULE_LICENSE("GPL");
3185
3186 module_init(mdc_init);
3187 module_exit(mdc_exit);