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