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