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