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