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