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