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