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