Version:  2.0.40 2.2.26 2.4.37 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8

Linux/drivers/staging/lustre/lustre/mdc/mdc_request.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us