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