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