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