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