Whamcloud - gitweb
d8196f6d55c57ca6d20576b3e0620285a425f119
[fs/lustre-release.git] / lustre / ofd / ofd_dev.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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 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  * lustre/ofd/ofd.c
37  *
38  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39  * Author: Mike Pershin <tappro@whamcloud.com>
40  * Author: Johann Lombardi <johann@whamcloud.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FILTER
44
45 #include <obd_class.h>
46 #include <lustre_param.h>
47 #include <lustre_fid.h>
48 #include <lustre_lfsck.h>
49 #include <lustre/lustre_idl.h>
50 #include <lustre_dlm.h>
51 #include <lustre_quota.h>
52
53 #include "ofd_internal.h"
54
55 /* Slab for OFD object allocation */
56 static struct kmem_cache *ofd_object_kmem;
57
58 static struct lu_kmem_descr ofd_caches[] = {
59         {
60                 .ckd_cache = &ofd_object_kmem,
61                 .ckd_name  = "ofd_obj",
62                 .ckd_size  = sizeof(struct ofd_object)
63         },
64         {
65                 .ckd_cache = NULL
66         }
67 };
68
69 static int ofd_connect_to_next(const struct lu_env *env, struct ofd_device *m,
70                                const char *next, struct obd_export **exp)
71 {
72         struct obd_connect_data *data = NULL;
73         struct obd_device       *obd;
74         int                      rc;
75         ENTRY;
76
77         OBD_ALLOC_PTR(data);
78         if (data == NULL)
79                 GOTO(out, rc = -ENOMEM);
80
81         obd = class_name2obd(next);
82         if (obd == NULL) {
83                 CERROR("%s: can't locate next device: %s\n",
84                        m->ofd_dt_dev.dd_lu_dev.ld_obd->obd_name, next);
85                 GOTO(out, rc = -ENOTCONN);
86         }
87
88         data->ocd_connect_flags = OBD_CONNECT_VERSION;
89         data->ocd_version = LUSTRE_VERSION_CODE;
90
91         rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
92         if (rc) {
93                 CERROR("%s: cannot connect to next dev %s: rc = %d\n",
94                        m->ofd_dt_dev.dd_lu_dev.ld_obd->obd_name, next, rc);
95                 GOTO(out, rc);
96         }
97
98         m->ofd_dt_dev.dd_lu_dev.ld_site =
99                 m->ofd_osd_exp->exp_obd->obd_lu_dev->ld_site;
100         LASSERT(m->ofd_dt_dev.dd_lu_dev.ld_site);
101         m->ofd_osd = lu2dt_dev(m->ofd_osd_exp->exp_obd->obd_lu_dev);
102         m->ofd_dt_dev.dd_lu_dev.ld_site->ls_top_dev = &m->ofd_dt_dev.dd_lu_dev;
103
104 out:
105         if (data)
106                 OBD_FREE_PTR(data);
107         RETURN(rc);
108 }
109
110 static int ofd_stack_init(const struct lu_env *env,
111                           struct ofd_device *m, struct lustre_cfg *cfg)
112 {
113         const char              *dev = lustre_cfg_string(cfg, 0);
114         struct lu_device        *d;
115         struct ofd_thread_info  *info = ofd_info(env);
116         struct lustre_mount_info *lmi;
117         int                      rc;
118         char                    *osdname;
119
120         ENTRY;
121
122         lmi = server_get_mount(dev);
123         if (lmi == NULL) {
124                 CERROR("Cannot get mount info for %s!\n", dev);
125                 RETURN(-ENODEV);
126         }
127
128         /* find bottom osd */
129         OBD_ALLOC(osdname, MTI_NAME_MAXLEN);
130         if (osdname == NULL)
131                 RETURN(-ENOMEM);
132
133         snprintf(osdname, MTI_NAME_MAXLEN, "%s-osd", dev);
134         rc = ofd_connect_to_next(env, m, osdname, &m->ofd_osd_exp);
135         OBD_FREE(osdname, MTI_NAME_MAXLEN);
136         if (rc)
137                 RETURN(rc);
138
139         d = m->ofd_osd_exp->exp_obd->obd_lu_dev;
140         LASSERT(d);
141         m->ofd_osd = lu2dt_dev(d);
142
143         snprintf(info->fti_u.name, sizeof(info->fti_u.name),
144                  "%s-osd", lustre_cfg_string(cfg, 0));
145
146         RETURN(rc);
147 }
148
149 static void ofd_stack_fini(const struct lu_env *env, struct ofd_device *m,
150                            struct lu_device *top)
151 {
152         struct obd_device       *obd = ofd_obd(m);
153         struct lustre_cfg_bufs   bufs;
154         struct lustre_cfg       *lcfg;
155         char                     flags[3] = "";
156
157         ENTRY;
158
159         lu_site_purge(env, top->ld_site, ~0);
160         /* process cleanup, pass mdt obd name to get obd umount flags */
161         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
162         if (obd->obd_force)
163                 strcat(flags, "F");
164         if (obd->obd_fail)
165                 strcat(flags, "A");
166         lustre_cfg_bufs_set_string(&bufs, 1, flags);
167         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
168         if (!lcfg) {
169                 CERROR("Cannot alloc lcfg!\n");
170                 RETURN_EXIT;
171         }
172
173         LASSERT(top);
174         top->ld_ops->ldo_process_config(env, top, lcfg);
175         lustre_cfg_free(lcfg);
176
177         lu_site_purge(env, top->ld_site, ~0);
178         if (!cfs_hash_is_empty(top->ld_site->ls_obj_hash)) {
179                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
180                 lu_site_print(env, top->ld_site, &msgdata, lu_cdebug_printer);
181         }
182
183         LASSERT(m->ofd_osd_exp);
184         obd_disconnect(m->ofd_osd_exp);
185         m->ofd_osd = NULL;
186
187         EXIT;
188 }
189
190 /* For interoperability, see mdt_interop_param[]. */
191 static struct cfg_interop_param ofd_interop_param[] = {
192         { "ost.quota_type",     NULL },
193         { NULL }
194 };
195
196 /* used by MGS to process specific configurations */
197 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
198                               struct lustre_cfg *cfg)
199 {
200         struct ofd_device       *m = ofd_dev(d);
201         struct dt_device        *dt_next = m->ofd_osd;
202         struct lu_device        *next = &dt_next->dd_lu_dev;
203         int                      rc;
204
205         ENTRY;
206
207         switch (cfg->lcfg_command) {
208         case LCFG_PARAM: {
209                 struct lprocfs_static_vars lvars;
210
211                 /* For interoperability */
212                 struct cfg_interop_param   *ptr = NULL;
213                 struct lustre_cfg          *old_cfg = NULL;
214                 char                       *param = NULL;
215
216                 param = lustre_cfg_string(cfg, 1);
217                 if (param == NULL) {
218                         CERROR("param is empty\n");
219                         rc = -EINVAL;
220                         break;
221                 }
222
223                 ptr = class_find_old_param(param, ofd_interop_param);
224                 if (ptr != NULL) {
225                         if (ptr->new_param == NULL) {
226                                 rc = 0;
227                                 CWARN("For interoperability, skip this %s."
228                                       " It is obsolete.\n", ptr->old_param);
229                                 break;
230                         }
231
232                         CWARN("Found old param %s, changed it to %s.\n",
233                               ptr->old_param, ptr->new_param);
234
235                         old_cfg = cfg;
236                         cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
237                         if (IS_ERR(cfg)) {
238                                 rc = PTR_ERR(cfg);
239                                 break;
240                         }
241                 }
242
243                 lprocfs_ofd_init_vars(&lvars);
244                 rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, cfg,
245                                               d->ld_obd);
246                 if (rc > 0 || rc == -ENOSYS)
247                         /* we don't understand; pass it on */
248                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
249                 break;
250         }
251         case LCFG_SPTLRPC_CONF: {
252                 rc = -ENOTSUPP;
253                 break;
254         }
255         default:
256                 /* others are passed further */
257                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
258                 break;
259         }
260         RETURN(rc);
261 }
262
263 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
264                            const struct lu_object_conf *conf)
265 {
266         struct ofd_device       *d = ofd_dev(o->lo_dev);
267         struct lu_device        *under;
268         struct lu_object        *below;
269         int                      rc = 0;
270
271         ENTRY;
272
273         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
274                PFID(lu_object_fid(o)));
275
276         under = &d->ofd_osd->dd_lu_dev;
277         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
278         if (below != NULL)
279                 lu_object_add(o, below);
280         else
281                 rc = -ENOMEM;
282
283         RETURN(rc);
284 }
285
286 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
287 {
288         struct ofd_object       *of = ofd_obj(o);
289         struct lu_object_header *h;
290
291         ENTRY;
292
293         h = o->lo_header;
294         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
295                PFID(lu_object_fid(o)));
296
297         lu_object_fini(o);
298         lu_object_header_fini(h);
299         OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
300         EXIT;
301 }
302
303 static int ofd_object_print(const struct lu_env *env, void *cookie,
304                             lu_printer_t p, const struct lu_object *o)
305 {
306         return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
307 }
308
309 struct lu_object_operations ofd_obj_ops = {
310         .loo_object_init        = ofd_object_init,
311         .loo_object_free        = ofd_object_free,
312         .loo_object_print       = ofd_object_print
313 };
314
315 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
316                                           const struct lu_object_header *hdr,
317                                           struct lu_device *d)
318 {
319         struct ofd_object *of;
320
321         ENTRY;
322
323         OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, __GFP_IO);
324         if (of != NULL) {
325                 struct lu_object        *o;
326                 struct lu_object_header *h;
327
328                 o = &of->ofo_obj.do_lu;
329                 h = &of->ofo_header;
330                 lu_object_header_init(h);
331                 lu_object_init(o, h, d);
332                 lu_object_add_top(h, o);
333                 o->lo_ops = &ofd_obj_ops;
334                 RETURN(o);
335         } else {
336                 RETURN(NULL);
337         }
338 }
339
340 extern int ost_handle(struct ptlrpc_request *req);
341
342 static int ofd_prepare(const struct lu_env *env, struct lu_device *pdev,
343                        struct lu_device *dev)
344 {
345         struct ofd_thread_info          *info;
346         struct ofd_device               *ofd = ofd_dev(dev);
347         struct obd_device               *obd = ofd_obd(ofd);
348         struct lu_device                *next = &ofd->ofd_osd->dd_lu_dev;
349         struct lfsck_start_param         lsp;
350         int                              rc;
351
352         ENTRY;
353
354         rc = lu_env_refill((struct lu_env *)env);
355         if (rc != 0) {
356                 CERROR("Failure to refill session: '%d'\n", rc);
357                 RETURN(rc);
358         }
359
360         info = ofd_info_init(env, NULL);
361         if (info == NULL)
362                 RETURN(-EFAULT);
363
364         /* initialize lower device */
365         rc = next->ld_ops->ldo_prepare(env, dev, next);
366         if (rc != 0)
367                 RETURN(rc);
368
369         rc = lfsck_register(env, ofd->ofd_osd, &ofd->ofd_dt_dev, false);
370         if (rc != 0) {
371                 CERROR("%s: failed to initialize lfsck: rc = %d\n",
372                        obd->obd_name, rc);
373                 RETURN(rc);
374         }
375
376         lsp.lsp_start = NULL;
377         lsp.lsp_namespace = ofd->ofd_namespace;
378         rc = lfsck_start(env, ofd->ofd_osd, &lsp);
379         if (rc != 0) {
380                 CWARN("%s: auto trigger paused LFSCK failed: rc = %d\n",
381                       obd->obd_name, rc);
382                 rc = 0;
383         }
384
385         target_recovery_init(&ofd->ofd_lut, tgt_request_handle);
386         LASSERT(obd->obd_no_conn);
387         spin_lock(&obd->obd_dev_lock);
388         obd->obd_no_conn = 0;
389         spin_unlock(&obd->obd_dev_lock);
390
391         if (obd->obd_recovering == 0)
392                 ofd_postrecov(env, ofd);
393
394         RETURN(rc);
395 }
396
397 static int ofd_recovery_complete(const struct lu_env *env,
398                                  struct lu_device *dev)
399 {
400         struct ofd_device       *ofd = ofd_dev(dev);
401         struct lu_device        *next = &ofd->ofd_osd->dd_lu_dev;
402         int                      rc = 0, max_precreate;
403
404         ENTRY;
405
406         /* Grant space for object precreation on the self export.
407          * This initial reserved space (i.e. 10MB for zfs and 280KB for ldiskfs)
408          * is enough to create 10k objects. More space is then acquired for
409          * precreation in ofd_grant_create().
410          */
411         max_precreate = OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace / 2;
412         ofd_grant_connect(env, dev->ld_obd->obd_self_export, max_precreate,
413                           false);
414         rc = next->ld_ops->ldo_recovery_complete(env, next);
415         RETURN(rc);
416 }
417
418 static struct lu_device_operations ofd_lu_ops = {
419         .ldo_object_alloc       = ofd_object_alloc,
420         .ldo_process_config     = ofd_process_config,
421         .ldo_recovery_complete  = ofd_recovery_complete,
422         .ldo_prepare            = ofd_prepare,
423 };
424
425 static int ofd_procfs_init(struct ofd_device *ofd)
426 {
427         struct lprocfs_static_vars       lvars;
428         struct obd_device               *obd = ofd_obd(ofd);
429         cfs_proc_dir_entry_t            *entry;
430         int                              rc = 0;
431
432         ENTRY;
433
434         /* lprocfs must be setup before the ofd so state can be safely added
435          * to /proc incrementally as the ofd is setup */
436         lprocfs_ofd_init_vars(&lvars);
437         rc = lprocfs_obd_setup(obd, lvars.obd_vars);
438         if (rc) {
439                 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
440                        obd->obd_name, rc);
441                 RETURN(rc);
442         }
443
444         rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_STATS_LAST);
445         if (rc) {
446                 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
447                        obd->obd_name, rc);
448                 GOTO(obd_cleanup, rc);
449         }
450
451         obd->obd_uses_nid_stats = 1;
452
453         entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
454         if (IS_ERR(entry)) {
455                 rc = PTR_ERR(entry);
456                 CERROR("%s: error %d setting up lprocfs for %s\n",
457                        obd->obd_name, rc, "exports");
458                 GOTO(obd_cleanup, rc);
459         }
460         obd->obd_proc_exports_entry = entry;
461
462         entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
463                                    lprocfs_nid_stats_clear_read,
464                                    lprocfs_nid_stats_clear_write, obd, NULL);
465         if (IS_ERR(entry)) {
466                 rc = PTR_ERR(entry);
467                 CERROR("%s: add proc entry 'clear' failed: %d.\n",
468                        obd->obd_name, rc);
469                 GOTO(obd_cleanup, rc);
470         }
471
472         ofd_stats_counter_init(obd->obd_stats);
473
474         rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
475                                     ofd_stats_counter_init);
476         if (rc)
477                 GOTO(remove_entry_clear, rc);
478         RETURN(0);
479 remove_entry_clear:
480         lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
481 obd_cleanup:
482         lprocfs_obd_cleanup(obd);
483         lprocfs_free_obd_stats(obd);
484
485         return rc;
486 }
487
488 static void ofd_procfs_add_brw_stats_symlink(struct ofd_device *ofd)
489 {
490         struct obd_device       *obd = ofd_obd(ofd);
491         struct obd_device       *osd_obd = ofd->ofd_osd_exp->exp_obd;
492         cfs_proc_dir_entry_t    *osd_root = osd_obd->obd_type->typ_procroot;
493         cfs_proc_dir_entry_t    *osd_dir;
494
495         osd_dir = lprocfs_srch(osd_root, obd->obd_name);
496         if (osd_dir == NULL)
497                 return;
498
499         if (lprocfs_srch(osd_dir, "brw_stats") != NULL)
500                 lprocfs_add_symlink("brw_stats", obd->obd_proc_entry,
501                                     "../../%s/%s/brw_stats",
502                                     osd_root->name, osd_dir->name);
503
504         if (lprocfs_srch(osd_dir, "read_cache_enable") != NULL)
505                 lprocfs_add_symlink("read_cache_enable", obd->obd_proc_entry,
506                                     "../../%s/%s/read_cache_enable",
507                                     osd_root->name, osd_dir->name);
508
509         if (lprocfs_srch(osd_dir, "readcache_max_filesize") != NULL)
510                 lprocfs_add_symlink("readcache_max_filesize",
511                                     obd->obd_proc_entry,
512                                     "../../%s/%s/readcache_max_filesize",
513                                     osd_root->name, osd_dir->name);
514
515         if (lprocfs_srch(osd_dir, "writethrough_cache_enable") != NULL)
516                 lprocfs_add_symlink("writethrough_cache_enable",
517                                     obd->obd_proc_entry,
518                                     "../../%s/%s/writethrough_cache_enable",
519                                     osd_root->name, osd_dir->name);
520 }
521
522 static void ofd_procfs_fini(struct ofd_device *ofd)
523 {
524         struct obd_device *obd = ofd_obd(ofd);
525
526         lprocfs_remove_proc_entry("writethrough_cache_enable",
527                                   obd->obd_proc_entry);
528         lprocfs_remove_proc_entry("readcache_max_filesize",
529                                   obd->obd_proc_entry);
530         lprocfs_remove_proc_entry("read_cache_enable", obd->obd_proc_entry);
531         lprocfs_remove_proc_entry("brw_stats", obd->obd_proc_entry);
532         lprocfs_remove_proc_entry("clear", obd->obd_proc_exports_entry);
533         lprocfs_free_per_client_stats(obd);
534         lprocfs_obd_cleanup(obd);
535         lprocfs_free_obd_stats(obd);
536         lprocfs_job_stats_fini(obd);
537 }
538
539 extern int ost_handle(struct ptlrpc_request *req);
540
541 int ofd_fid_fini(const struct lu_env *env, struct ofd_device *ofd)
542 {
543         return seq_site_fini(env, &ofd->ofd_seq_site);
544 }
545
546 int ofd_fid_init(const struct lu_env *env, struct ofd_device *ofd)
547 {
548         struct seq_server_site  *ss = &ofd->ofd_seq_site;
549         struct lu_device        *lu = &ofd->ofd_dt_dev.dd_lu_dev;
550         char                    *obd_name = ofd_name(ofd);
551         char                    *name = NULL;
552         int                     rc = 0;
553
554         ss = &ofd->ofd_seq_site;
555         lu->ld_site->ld_seq_site = ss;
556         ss->ss_lu = lu->ld_site;
557         ss->ss_node_id = ofd->ofd_lut.lut_lsd.lsd_osd_index;
558
559         OBD_ALLOC_PTR(ss->ss_server_seq);
560         if (ss->ss_server_seq == NULL)
561                 GOTO(out_free, rc = -ENOMEM);
562
563         OBD_ALLOC(name, strlen(obd_name) + 10);
564         if (!name) {
565                 OBD_FREE_PTR(ss->ss_server_seq);
566                 ss->ss_server_seq = NULL;
567                 GOTO(out_free, rc = -ENOMEM);
568         }
569
570         rc = seq_server_init(ss->ss_server_seq, ofd->ofd_osd, obd_name,
571                              LUSTRE_SEQ_SERVER, ss, env);
572         if (rc) {
573                 CERROR("%s : seq server init error %d\n", obd_name, rc);
574                 GOTO(out_free, rc);
575         }
576         ss->ss_server_seq->lss_space.lsr_index = ss->ss_node_id;
577
578         OBD_ALLOC_PTR(ss->ss_client_seq);
579         if (ss->ss_client_seq == NULL)
580                 GOTO(out_free, rc = -ENOMEM);
581
582         snprintf(name, strlen(obd_name) + 6, "%p-super", obd_name);
583         rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_DATA,
584                              name, NULL);
585         if (rc) {
586                 CERROR("%s : seq client init error %d\n", obd_name, rc);
587                 GOTO(out_free, rc);
588         }
589         OBD_FREE(name, strlen(obd_name) + 10);
590         name = NULL;
591
592         rc = seq_server_set_cli(ss->ss_server_seq, ss->ss_client_seq, env);
593
594 out_free:
595         if (rc) {
596                 if (ss->ss_server_seq) {
597                         seq_server_fini(ss->ss_server_seq, env);
598                         OBD_FREE_PTR(ss->ss_server_seq);
599                         ss->ss_server_seq = NULL;
600                 }
601
602                 if (ss->ss_client_seq) {
603                         seq_client_fini(ss->ss_client_seq);
604                         OBD_FREE_PTR(ss->ss_client_seq);
605                         ss->ss_client_seq = NULL;
606                 }
607
608                 if (name) {
609                         OBD_FREE(name, strlen(obd_name) + 10);
610                         name = NULL;
611                 }
612         }
613
614         return rc;
615 }
616
617 int ofd_set_info_hdl(struct tgt_session_info *tsi)
618 {
619         struct ptlrpc_request   *req = tgt_ses_req(tsi);
620         struct ost_body         *body = NULL, *repbody;
621         void                    *key, *val = NULL;
622         int                      keylen, vallen, rc = 0;
623         bool                     is_grant_shrink;
624         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
625
626         ENTRY;
627
628         key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
629         if (key == NULL) {
630                 DEBUG_REQ(D_HA, req, "no set_info key");
631                 RETURN(err_serious(-EFAULT));
632         }
633         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
634                                       RCL_CLIENT);
635
636         val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
637         if (val == NULL) {
638                 DEBUG_REQ(D_HA, req, "no set_info val");
639                 RETURN(err_serious(-EFAULT));
640         }
641         vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
642                                       RCL_CLIENT);
643
644         is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
645         if (is_grant_shrink)
646                 /* In this case the value is actually an RMF_OST_BODY, so we
647                  * transmutate the type of this PTLRPC */
648                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
649
650         rc = req_capsule_server_pack(tsi->tsi_pill);
651         if (rc < 0)
652                 RETURN(rc);
653
654         if (is_grant_shrink) {
655                 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
656
657                 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
658                 *repbody = *body;
659
660                 /** handle grant shrink, similar to a read request */
661                 ofd_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
662                                        &repbody->oa);
663         } else if (KEY_IS(KEY_EVICT_BY_NID)) {
664                 if (vallen > 0)
665                         obd_export_evict_by_nid(tsi->tsi_exp->exp_obd, val);
666                 rc = 0;
667         } else if (KEY_IS(KEY_CAPA_KEY)) {
668                 rc = ofd_update_capa_key(ofd, val);
669         } else if (KEY_IS(KEY_SPTLRPC_CONF)) {
670                 rc = tgt_adapt_sptlrpc_conf(tsi->tsi_tgt, 0);
671         } else {
672                 CERROR("%s: Unsupported key %s\n",
673                        tgt_name(tsi->tsi_tgt), (char *)key);
674                 rc = -EOPNOTSUPP;
675         }
676         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SET_INFO,
677                          tsi->tsi_jobid, 1);
678
679         RETURN(rc);
680 }
681
682 static int ofd_fiemap_get(const struct lu_env *env, struct ofd_device *ofd,
683                           struct lu_fid *fid, struct ll_user_fiemap *fiemap)
684 {
685         struct ofd_object       *fo;
686         int                      rc;
687
688         fo = ofd_object_find(env, ofd, fid);
689         if (IS_ERR(fo)) {
690                 CERROR("%s: error finding object "DFID"\n",
691                        ofd_name(ofd), PFID(fid));
692                 return PTR_ERR(fo);
693         }
694
695         ofd_read_lock(env, fo);
696         if (ofd_object_exists(fo))
697                 rc = dt_fiemap_get(env, ofd_object_child(fo), fiemap);
698         else
699                 rc = -ENOENT;
700         ofd_read_unlock(env, fo);
701         ofd_object_put(env, fo);
702         return rc;
703 }
704
705 struct locked_region {
706         cfs_list_t              list;
707         struct lustre_handle    lh;
708 };
709
710 static int lock_region(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
711                        unsigned long long begin, unsigned long long end,
712                        cfs_list_t *locked)
713 {
714         struct locked_region    *region = NULL;
715         __u64                    flags = 0;
716         int                      rc;
717
718         LASSERT(begin <= end);
719         OBD_ALLOC_PTR(region);
720         if (region == NULL)
721                 return -ENOMEM;
722
723         rc = tgt_extent_lock(ns, res_id, begin, end, &region->lh,
724                              LCK_PR, &flags);
725         if (rc != 0)
726                 return rc;
727
728         CDEBUG(D_OTHER, "ost lock [%llu,%llu], lh=%p\n", begin, end,
729                &region->lh);
730         cfs_list_add(&region->list, locked);
731
732         return 0;
733 }
734
735 static int lock_zero_regions(struct ldlm_namespace *ns,
736                              struct ldlm_res_id *res_id,
737                              struct ll_user_fiemap *fiemap,
738                              cfs_list_t *locked)
739 {
740         __u64 begin = fiemap->fm_start;
741         unsigned int i;
742         int rc = 0;
743         struct ll_fiemap_extent *fiemap_start = fiemap->fm_extents;
744
745         ENTRY;
746
747         CDEBUG(D_OTHER, "extents count %u\n", fiemap->fm_mapped_extents);
748         for (i = 0; i < fiemap->fm_mapped_extents; i++) {
749                 if (fiemap_start[i].fe_logical > begin) {
750                         CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
751                                begin, fiemap_start[i].fe_logical);
752                         rc = lock_region(ns, res_id, begin,
753                                          fiemap_start[i].fe_logical, locked);
754                         if (rc)
755                                 RETURN(rc);
756                 }
757
758                 begin = fiemap_start[i].fe_logical + fiemap_start[i].fe_length;
759         }
760
761         if (begin < (fiemap->fm_start + fiemap->fm_length)) {
762                 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
763                        begin, fiemap->fm_start + fiemap->fm_length);
764                 rc = lock_region(ns, res_id, begin,
765                                  fiemap->fm_start + fiemap->fm_length, locked);
766         }
767
768         RETURN(rc);
769 }
770
771 static void unlock_zero_regions(struct ldlm_namespace *ns, cfs_list_t *locked)
772 {
773         struct locked_region *entry, *temp;
774
775         cfs_list_for_each_entry_safe(entry, temp, locked, list) {
776                 CDEBUG(D_OTHER, "ost unlock lh=%p\n", &entry->lh);
777                 tgt_extent_unlock(&entry->lh, LCK_PR);
778                 cfs_list_del(&entry->list);
779                 OBD_FREE_PTR(entry);
780         }
781 }
782
783 int ofd_get_info_hdl(struct tgt_session_info *tsi)
784 {
785         struct obd_export               *exp = tsi->tsi_exp;
786         struct ofd_device               *ofd = ofd_exp(exp);
787         struct ofd_thread_info          *fti = tsi2ofd_info(tsi);
788         void                            *key;
789         int                              keylen;
790         int                              replylen, rc = 0;
791
792         ENTRY;
793
794         /* this common part for get_info rpc */
795         key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
796         if (key == NULL) {
797                 DEBUG_REQ(D_HA, tgt_ses_req(tsi), "no get_info key");
798                 RETURN(err_serious(-EPROTO));
799         }
800         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
801                                       RCL_CLIENT);
802
803         if (KEY_IS(KEY_LAST_ID)) {
804                 obd_id          *last_id;
805                 struct ofd_seq  *oseq;
806
807                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_ID);
808                 rc = req_capsule_server_pack(tsi->tsi_pill);
809                 if (rc)
810                         RETURN(err_serious(rc));
811
812                 last_id = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_ID);
813
814                 oseq = ofd_seq_load(tsi->tsi_env, ofd,
815                                     (obd_seq)exp->exp_filter_data.fed_group);
816                 if (IS_ERR(oseq))
817                         rc = -EFAULT;
818                 else
819                         *last_id = ofd_seq_last_oid(oseq);
820                 ofd_seq_put(tsi->tsi_env, oseq);
821         } else if (KEY_IS(KEY_FIEMAP)) {
822                 struct ll_fiemap_info_key       *fm_key;
823                 struct ll_user_fiemap           *fiemap;
824                 struct lu_fid                   *fid = &fti->fti_fid;
825
826                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_FIEMAP);
827
828                 fm_key = req_capsule_client_get(tsi->tsi_pill, &RMF_FIEMAP_KEY);
829                 rc = tgt_validate_obdo(tsi, &fm_key->oa);
830                 if (rc)
831                         RETURN(err_serious(rc));
832
833                 replylen = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
834                 req_capsule_set_size(tsi->tsi_pill, &RMF_FIEMAP_VAL,
835                                      RCL_SERVER, replylen);
836
837                 rc = req_capsule_server_pack(tsi->tsi_pill);
838                 if (rc)
839                         RETURN(err_serious(rc));
840
841                 fiemap = req_capsule_server_get(tsi->tsi_pill, &RMF_FIEMAP_VAL);
842                 if (fiemap == NULL)
843                         RETURN(-ENOMEM);
844
845                 rc = ostid_to_fid(fid, &fm_key->oa.o_oi, 0);
846                 if (rc != 0)
847                         RETURN(rc);
848
849                 CDEBUG(D_INODE, "get FIEMAP of object "DFID"\n", PFID(fid));
850
851                 *fiemap = fm_key->fiemap;
852                 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid, fiemap);
853
854                 /* LU-3219: Lock the sparse areas to make sure dirty
855                  * flushed back from client, then call fiemap again. */
856                 if (fm_key->oa.o_valid & OBD_MD_FLFLAGS &&
857                     fm_key->oa.o_flags & OBD_FL_SRVLOCK) {
858                         cfs_list_t locked = CFS_LIST_HEAD_INIT(locked);
859
860                         ost_fid_build_resid(fid, &fti->fti_resid);
861                         rc = lock_zero_regions(ofd->ofd_namespace,
862                                                &fti->fti_resid, fiemap,
863                                                &locked);
864                         if (rc == 0 && !cfs_list_empty(&locked)) {
865                                 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid,
866                                                     fiemap);
867                                 unlock_zero_regions(ofd->ofd_namespace,
868                                                     &locked);
869                         }
870                 }
871         } else if (KEY_IS(KEY_LAST_FID)) {
872                 struct ofd_device       *ofd = ofd_exp(exp);
873                 struct ofd_seq          *oseq;
874                 struct lu_fid           *fid;
875                 int                      rc;
876
877                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_FID);
878                 rc = req_capsule_server_pack(tsi->tsi_pill);
879                 if (rc)
880                         RETURN(err_serious(rc));
881
882                 fid = req_capsule_client_get(tsi->tsi_pill, &RMF_FID);
883                 if (fid == NULL)
884                         RETURN(err_serious(-EPROTO));
885
886                 fid_le_to_cpu(&fti->fti_ostid.oi_fid, fid);
887
888                 fid = req_capsule_server_get(tsi->tsi_pill, &RMF_FID);
889                 if (fid == NULL)
890                         RETURN(-ENOMEM);
891
892                 oseq = ofd_seq_load(tsi->tsi_env, ofd,
893                                     ostid_seq(&fti->fti_ostid));
894                 if (IS_ERR(oseq))
895                         RETURN(PTR_ERR(oseq));
896
897                 rc = ostid_to_fid(fid, &oseq->os_oi,
898                                   ofd->ofd_lut.lut_lsd.lsd_osd_index);
899                 if (rc != 0)
900                         GOTO(out_put, rc);
901
902                 CDEBUG(D_HA, "%s: LAST FID is "DFID"\n", ofd_name(ofd),
903                        PFID(fid));
904 out_put:
905                 ofd_seq_put(tsi->tsi_env, oseq);
906         } else {
907                 CERROR("%s: not supported key %s\n", tgt_name(tsi->tsi_tgt),
908                        (char *)key);
909                 rc = -EOPNOTSUPP;
910         }
911         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GET_INFO,
912                          tsi->tsi_jobid, 1);
913
914         RETURN(rc);
915 }
916
917 static int ofd_getattr_hdl(struct tgt_session_info *tsi)
918 {
919         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
920         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
921         struct ost_body         *repbody;
922         struct lustre_handle     lh = { 0 };
923         struct ofd_object       *fo;
924         __u64                    flags = 0;
925         ldlm_mode_t              lock_mode = LCK_PR;
926         bool                     srvlock;
927         int                      rc;
928
929         ENTRY;
930
931         LASSERT(tsi->tsi_ost_body != NULL);
932
933         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
934         if (repbody == NULL)
935                 RETURN(-ENOMEM);
936
937         repbody->oa.o_oi = tsi->tsi_ost_body->oa.o_oi;
938         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
939
940         srvlock = tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
941                   tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK;
942
943         if (srvlock) {
944                 if (unlikely(tsi->tsi_ost_body->oa.o_flags & OBD_FL_FLUSH))
945                         lock_mode = LCK_PW;
946
947                 rc = tgt_extent_lock(tsi->tsi_tgt->lut_obd->obd_namespace,
948                                      &tsi->tsi_resid, 0, OBD_OBJECT_EOF, &lh,
949                                      lock_mode, &flags);
950                 if (rc != 0)
951                         RETURN(rc);
952         }
953
954         fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
955         if (IS_ERR(fo))
956                 GOTO(out, rc = PTR_ERR(fo));
957
958         rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
959         if (rc == 0) {
960                 __u64    curr_version;
961
962                 obdo_from_la(&repbody->oa, &fti->fti_attr,
963                              OFD_VALID_FLAGS | LA_UID | LA_GID);
964                 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
965
966                 /* Store object version in reply */
967                 curr_version = dt_version_get(tsi->tsi_env,
968                                               ofd_object_child(fo));
969                 if ((__s64)curr_version != -EOPNOTSUPP) {
970                         repbody->oa.o_valid |= OBD_MD_FLDATAVERSION;
971                         repbody->oa.o_data_version = curr_version;
972                 }
973         }
974
975         ofd_object_put(tsi->tsi_env, fo);
976 out:
977         if (srvlock)
978                 tgt_extent_unlock(&lh, lock_mode);
979
980         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GETATTR,
981                          tsi->tsi_jobid, 1);
982
983         repbody->oa.o_valid |= OBD_MD_FLFLAGS;
984         repbody->oa.o_flags = OBD_FL_FLUSH;
985
986         RETURN(rc);
987 }
988
989 static int ofd_setattr_hdl(struct tgt_session_info *tsi)
990 {
991         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
992         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
993         struct ost_body         *body = tsi->tsi_ost_body;
994         struct ost_body         *repbody;
995         struct ldlm_resource    *res;
996         struct ofd_object       *fo;
997         struct filter_fid       *ff = NULL;
998         int                      rc = 0;
999
1000         ENTRY;
1001
1002         LASSERT(body != NULL);
1003
1004         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1005         if (repbody == NULL)
1006                 RETURN(-ENOMEM);
1007
1008         repbody->oa.o_oi = body->oa.o_oi;
1009         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1010
1011         /* This would be very bad - accidentally truncating a file when
1012          * changing the time or similar - bug 12203. */
1013         if (body->oa.o_valid & OBD_MD_FLSIZE &&
1014             body->oa.o_size != OBD_OBJECT_EOF) {
1015                 static char mdsinum[48];
1016
1017                 if (body->oa.o_valid & OBD_MD_FLFID)
1018                         snprintf(mdsinum, sizeof(mdsinum) - 1,
1019                                  "of parent "DFID, body->oa.o_parent_seq,
1020                                  body->oa.o_parent_oid, 0);
1021                 else
1022                         mdsinum[0] = '\0';
1023
1024                 CERROR("%s: setattr from %s is trying to truncate object "DFID
1025                        " %s\n", ofd_name(ofd), obd_export_nid2str(tsi->tsi_exp),
1026                        PFID(&tsi->tsi_fid), mdsinum);
1027                 RETURN(-EPERM);
1028         }
1029
1030         fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1031         if (IS_ERR(fo))
1032                 GOTO(out, rc = PTR_ERR(fo));
1033
1034         la_from_obdo(&fti->fti_attr, &body->oa, body->oa.o_valid);
1035         fti->fti_attr.la_valid &= ~LA_TYPE;
1036
1037         if (body->oa.o_valid & OBD_MD_FLFID) {
1038                 ff = &fti->fti_mds_fid;
1039                 ofd_prepare_fidea(ff, &body->oa);
1040         }
1041
1042         /* setting objects attributes (including owner/group) */
1043         rc = ofd_attr_set(tsi->tsi_env, fo, &fti->fti_attr, ff);
1044         if (rc != 0)
1045                 GOTO(out_put, rc);
1046
1047         obdo_from_la(&repbody->oa, &fti->fti_attr,
1048                      OFD_VALID_FLAGS | LA_UID | LA_GID);
1049         tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1050
1051         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SETATTR,
1052                          tsi->tsi_jobid, 1);
1053         EXIT;
1054 out_put:
1055         ofd_object_put(tsi->tsi_env, fo);
1056 out:
1057         if (rc == 0) {
1058                 /* we do not call this before to avoid lu_object_find() in
1059                  *  ->lvbo_update() holding another reference on the object.
1060                  * otherwise concurrent destroy can make the object unavailable
1061                  * for 2nd lu_object_find() waiting for the first reference
1062                  * to go... deadlock! */
1063                 res = ldlm_resource_get(ofd->ofd_namespace, NULL,
1064                                         &tsi->tsi_resid, LDLM_EXTENT, 0);
1065                 if (res != NULL) {
1066                         ldlm_res_lvbo_update(res, NULL, 0);
1067                         ldlm_resource_putref(res);
1068                 }
1069         }
1070         return rc;
1071 }
1072
1073 static int ofd_create_hdl(struct tgt_session_info *tsi)
1074 {
1075         struct ost_body         *repbody;
1076         const struct obdo       *oa = &tsi->tsi_ost_body->oa;
1077         struct obdo             *rep_oa;
1078         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1079         obd_seq                  seq = ostid_seq(&oa->o_oi);
1080         obd_id                   oid = ostid_id(&oa->o_oi);
1081         struct ofd_seq          *oseq;
1082         int                      rc = 0, diff;
1083         int                      sync_trans = 0;
1084
1085         ENTRY;
1086
1087         if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1088                 RETURN(-EROFS);
1089
1090         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1091         if (repbody == NULL)
1092                 RETURN(-ENOMEM);
1093
1094         rep_oa = &repbody->oa;
1095         rep_oa->o_oi = oa->o_oi;
1096
1097         LASSERT(seq >= FID_SEQ_OST_MDT0);
1098         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1099
1100         CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1101
1102         oseq = ofd_seq_load(tsi->tsi_env, ofd, seq);
1103         if (IS_ERR(oseq)) {
1104                 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
1105                        ofd_name(ofd), seq, PTR_ERR(oseq));
1106                 RETURN(-EINVAL);
1107         }
1108
1109         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1110             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1111                 if (!ofd_obd(ofd)->obd_recovering ||
1112                     oid > ofd_seq_last_oid(oseq)) {
1113                         CERROR("%s: recreate objid "DOSTID" > last id "LPU64
1114                                "\n", ofd_name(ofd), POSTID(&oa->o_oi),
1115                                ofd_seq_last_oid(oseq));
1116                         GOTO(out_nolock, rc = -EINVAL);
1117                 }
1118                 /* Do nothing here, we re-create objects during recovery
1119                  * upon write replay, see ofd_preprw_write() */
1120                 GOTO(out_nolock, rc = 0);
1121         }
1122         /* former ofd_handle_precreate */
1123         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1124             (oa->o_flags & OBD_FL_DELORPHAN)) {
1125                 /* destroy orphans */
1126                 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1127                     tsi->tsi_exp->exp_conn_cnt) {
1128                         CERROR("%s: dropping old orphan cleanup request\n",
1129                                ofd_name(ofd));
1130                         GOTO(out_nolock, rc = 0);
1131                 }
1132                 /* This causes inflight precreates to abort and drop lock */
1133                 oseq->os_destroys_in_progress = 1;
1134                 mutex_lock(&oseq->os_create_lock);
1135                 if (!oseq->os_destroys_in_progress) {
1136                         CERROR("%s:["LPU64"] destroys_in_progress already"
1137                                " cleared\n", ofd_name(ofd), seq);
1138                         ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1139                         GOTO(out, rc = 0);
1140                 }
1141                 diff = oid - ofd_seq_last_oid(oseq);
1142                 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1143                         ofd_seq_last_oid(oseq), diff);
1144                 if (-diff > OST_MAX_PRECREATE) {
1145                         /* FIXME: should reset precreate_next_id on MDS */
1146                         rc = 0;
1147                 } else if (diff < 0) {
1148                         rc = ofd_orphans_destroy(tsi->tsi_env, tsi->tsi_exp,
1149                                                  ofd, rep_oa);
1150                         oseq->os_destroys_in_progress = 0;
1151                 } else {
1152                         /* XXX: Used by MDS for the first time! */
1153                         oseq->os_destroys_in_progress = 0;
1154                 }
1155         } else {
1156                 mutex_lock(&oseq->os_create_lock);
1157                 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1158                     tsi->tsi_exp->exp_conn_cnt) {
1159                         CERROR("%s: dropping old precreate request\n",
1160                                ofd_name(ofd));
1161                         GOTO(out, rc = 0);
1162                 }
1163                 /* only precreate if seq is 0, IDIF or normal and also o_id
1164                  * must be specfied */
1165                 if ((!fid_seq_is_mdt(seq) && !fid_seq_is_norm(seq) &&
1166                      !fid_seq_is_idif(seq)) || oid == 0) {
1167                         diff = 1; /* shouldn't we create this right now? */
1168                 } else {
1169                         diff = oid - ofd_seq_last_oid(oseq);
1170                         /* Do sync create if the seq is about to used up */
1171                         if (fid_seq_is_idif(seq) || fid_seq_is_mdt0(seq)) {
1172                                 if (unlikely(oid >= IDIF_MAX_OID - 1))
1173                                         sync_trans = 1;
1174                         } else if (fid_seq_is_norm(seq)) {
1175                                 if (unlikely(oid >=
1176                                              LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
1177                                         sync_trans = 1;
1178                         } else {
1179                                 CERROR("%s : invalid o_seq "DOSTID"\n",
1180                                        ofd_name(ofd), POSTID(&oa->o_oi));
1181                                 GOTO(out, rc = -EINVAL);
1182                         }
1183                 }
1184         }
1185         if (diff > 0) {
1186                 cfs_time_t       enough_time = cfs_time_shift(DISK_TIMEOUT);
1187                 obd_id           next_id;
1188                 int              created = 0;
1189                 int              count;
1190
1191                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1192                     !(oa->o_flags & OBD_FL_DELORPHAN)) {
1193                         /* don't enforce grant during orphan recovery */
1194                         rc = ofd_grant_create(tsi->tsi_env,
1195                                               ofd_obd(ofd)->obd_self_export,
1196                                               &diff);
1197                         if (rc) {
1198                                 CDEBUG(D_HA, "%s: failed to acquire grant "
1199                                        "space for precreate (%d): rc = %d\n",
1200                                        ofd_name(ofd), diff, rc);
1201                                 diff = 0;
1202                         }
1203                 }
1204
1205                 /* This can happen if a new OST is formatted and installed
1206                  * in place of an old one at the same index.  Instead of
1207                  * precreating potentially millions of deleted old objects
1208                  * (possibly filling the OST), only precreate the last batch.
1209                  * LFSCK will eventually clean up any orphans. LU-14 */
1210                 if (diff > 5 * OST_MAX_PRECREATE) {
1211                         diff = OST_MAX_PRECREATE / 2;
1212                         LCONSOLE_WARN("%s: precreate FID "DOSTID" is over %u "
1213                                       "larger than the LAST_ID "DOSTID", only "
1214                                       "precreating the last %u objects.\n",
1215                                       ofd_name(ofd), POSTID(&oa->o_oi),
1216                                       5 * OST_MAX_PRECREATE,
1217                                       POSTID(&oseq->os_oi), diff);
1218                         ofd_seq_last_oid_set(oseq, ostid_id(&oa->o_oi) - diff);
1219                 }
1220
1221                 while (diff > 0) {
1222                         next_id = ofd_seq_last_oid(oseq) + 1;
1223                         count = ofd_precreate_batch(ofd, diff);
1224
1225                         CDEBUG(D_HA, "%s: reserve %d objects in group "LPX64
1226                                " at "LPU64"\n", ofd_name(ofd),
1227                                count, seq, next_id);
1228
1229                         if (cfs_time_after(jiffies, enough_time)) {
1230                                 LCONSOLE_WARN("%s: Slow creates, %d/%d objects"
1231                                               " created at a rate of %d/s\n",
1232                                               ofd_name(ofd), created,
1233                                               diff + created,
1234                                               created / DISK_TIMEOUT);
1235                                 break;
1236                         }
1237
1238                         rc = ofd_precreate_objects(tsi->tsi_env, ofd, next_id,
1239                                                    oseq, count, sync_trans);
1240                         if (rc > 0) {
1241                                 created += rc;
1242                                 diff -= rc;
1243                         } else if (rc < 0) {
1244                                 break;
1245                         }
1246                 }
1247                 if (created > 0)
1248                         /* some objects got created, we can return
1249                          * them, even if last creation failed */
1250                         rc = 0;
1251                 else
1252                         CERROR("%s: unable to precreate: rc = %d\n",
1253                                ofd_name(ofd), rc);
1254
1255                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1256                     !(oa->o_flags & OBD_FL_DELORPHAN))
1257                         ofd_grant_commit(tsi->tsi_env,
1258                                          ofd_obd(ofd)->obd_self_export, rc);
1259
1260                 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1261         }
1262         EXIT;
1263         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_CREATE,
1264                          tsi->tsi_jobid, 1);
1265 out:
1266         mutex_unlock(&oseq->os_create_lock);
1267 out_nolock:
1268         if (rc == 0)
1269                 rep_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1270
1271         ofd_seq_put(tsi->tsi_env, oseq);
1272         return rc;
1273 }
1274
1275 static int ofd_destroy_hdl(struct tgt_session_info *tsi)
1276 {
1277         const struct ost_body   *body = tsi->tsi_ost_body;
1278         struct ost_body         *repbody;
1279         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1280         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1281         obd_count                count;
1282         int                      rc = 0;
1283
1284         ENTRY;
1285
1286         if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1287                 RETURN(-EROFS);
1288
1289         /* This is old case for clients before Lustre 2.4 */
1290         /* If there's a DLM request, cancel the locks mentioned in it */
1291         if (req_capsule_field_present(tsi->tsi_pill, &RMF_DLM_REQ,
1292                                       RCL_CLIENT)) {
1293                 struct ldlm_request *dlm;
1294
1295                 dlm = req_capsule_client_get(tsi->tsi_pill, &RMF_DLM_REQ);
1296                 if (dlm == NULL)
1297                         RETURN(-EFAULT);
1298                 ldlm_request_cancel(tgt_ses_req(tsi), dlm, 0);
1299         }
1300
1301         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1302         repbody->oa.o_oi = body->oa.o_oi;
1303
1304         /* check that o_misc makes sense */
1305         if (body->oa.o_valid & OBD_MD_FLOBJCOUNT)
1306                 count = body->oa.o_misc;
1307         else
1308                 count = 1; /* default case - single destroy */
1309
1310         /**
1311          * There can be sequence of objects to destroy. Therefore this request
1312          * may have multiple transaction involved in. It is OK, we need only
1313          * the highest used transno to be reported back in reply but not for
1314          * replays, they must report their transno
1315          */
1316         if (fti->fti_transno == 0) /* not replay */
1317                 fti->fti_mult_trans = 1;
1318
1319         CDEBUG(D_HA, "%s: Destroy object "DOSTID" count %d\n", ofd_name(ofd),
1320                POSTID(&body->oa.o_oi), count);
1321         while (count > 0) {
1322                 int lrc;
1323
1324                 lrc = ostid_to_fid(&fti->fti_fid, &repbody->oa.o_oi, 0);
1325                 if (lrc != 0) {
1326                         if (rc == 0)
1327                                 rc = lrc;
1328                         GOTO(out, rc);
1329                 }
1330                 lrc = ofd_destroy_by_fid(tsi->tsi_env, ofd, &fti->fti_fid, 0);
1331                 if (lrc == -ENOENT) {
1332                         CDEBUG(D_INODE,
1333                                "%s: destroying non-existent object "DFID"\n",
1334                                ofd_name(ofd), PFID(&fti->fti_fid));
1335                         /* rewrite rc with -ENOENT only if it is 0 */
1336                         if (rc == 0)
1337                                 rc = lrc;
1338                 } else if (lrc != 0) {
1339                         CERROR("%s: error destroying object "DFID": %d\n",
1340                                ofd_name(ofd), PFID(&fti->fti_fid),
1341                                rc);
1342                         rc = lrc;
1343                 }
1344                 count--;
1345                 ostid_inc_id(&repbody->oa.o_oi);
1346         }
1347
1348         /* if we have transaction then there were some deletions, we don't
1349          * need to return ENOENT in that case because it will not wait
1350          * for commit of these deletions. The ENOENT must be returned only
1351          * if there were no transations.
1352          */
1353         if (rc == -ENOENT) {
1354                 if (fti->fti_transno != 0)
1355                         rc = 0;
1356         } else if (rc != 0) {
1357                 /*
1358                  * If we have at least one transaction then llog record
1359                  * on server will be removed upon commit, so for rc != 0
1360                  * we return no transno and llog record will be reprocessed.
1361                  */
1362                 fti->fti_transno = 0;
1363         }
1364         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_DESTROY,
1365                          tsi->tsi_jobid, 1);
1366 out:
1367         RETURN(rc);
1368 }
1369
1370 static int ofd_statfs_hdl(struct tgt_session_info *tsi)
1371 {
1372         struct obd_statfs       *osfs;
1373         int                      rc;
1374
1375         ENTRY;
1376
1377         osfs = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_STATFS);
1378
1379         rc = ofd_statfs(tsi->tsi_env, tsi->tsi_exp, osfs,
1380                         cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 0);
1381         if (rc != 0)
1382                 CERROR("%s: statfs failed: rc = %d\n",
1383                        tgt_name(tsi->tsi_tgt), rc);
1384
1385         if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_EINPROGRESS))
1386                 rc = -EINPROGRESS;
1387
1388         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_STATFS,
1389                          tsi->tsi_jobid, 1);
1390
1391         RETURN(rc);
1392 }
1393
1394 static int ofd_sync_hdl(struct tgt_session_info *tsi)
1395 {
1396         struct ost_body         *body = tsi->tsi_ost_body;
1397         struct ost_body         *repbody;
1398         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1399         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1400         struct ofd_object       *fo = NULL;
1401         int                      rc = 0;
1402
1403         ENTRY;
1404
1405         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1406
1407         /* if no objid is specified, it means "sync whole filesystem" */
1408         if (!fid_is_zero(&tsi->tsi_fid)) {
1409                 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1410                 if (IS_ERR(fo))
1411                         RETURN(PTR_ERR(fo));
1412         }
1413
1414         rc = tgt_sync(tsi->tsi_env, tsi->tsi_tgt,
1415                       fo != NULL ? ofd_object_child(fo) : NULL);
1416         if (rc)
1417                 GOTO(put, rc);
1418
1419         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SYNC,
1420                          tsi->tsi_jobid, 1);
1421         if (fo == NULL)
1422                 RETURN(0);
1423
1424         repbody->oa.o_oi = body->oa.o_oi;
1425         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1426
1427         rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1428         if (rc == 0)
1429                 obdo_from_la(&repbody->oa, &fti->fti_attr,
1430                              OFD_VALID_FLAGS);
1431         else
1432                 /* don't return rc from getattr */
1433                 rc = 0;
1434         EXIT;
1435 put:
1436         if (fo != NULL)
1437                 ofd_object_put(tsi->tsi_env, fo);
1438         return rc;
1439 }
1440
1441 static int ofd_punch_hdl(struct tgt_session_info *tsi)
1442 {
1443         const struct obdo       *oa = &tsi->tsi_ost_body->oa;
1444         struct ost_body         *repbody;
1445         struct ofd_thread_info  *info = tsi2ofd_info(tsi);
1446         struct ldlm_namespace   *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
1447         struct ldlm_resource    *res;
1448         struct ofd_object       *fo;
1449         struct filter_fid       *ff = NULL;
1450         __u64                    flags = 0;
1451         struct lustre_handle     lh = { 0, };
1452         int                      rc;
1453         __u64                    start, end;
1454         bool                     srvlock;
1455
1456         ENTRY;
1457
1458         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
1459         CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
1460
1461         if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
1462             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
1463                 RETURN(err_serious(-EPROTO));
1464
1465         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1466         if (repbody == NULL)
1467                 RETURN(err_serious(-ENOMEM));
1468
1469         /* punch start,end are passed in o_size,o_blocks throught wire */
1470         start = oa->o_size;
1471         end = oa->o_blocks;
1472
1473         if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
1474                 RETURN(-EPROTO);
1475
1476         /* standard truncate optimization: if file body is completely
1477          * destroyed, don't send data back to the server. */
1478         if (start == 0)
1479                 flags |= LDLM_FL_AST_DISCARD_DATA;
1480
1481         repbody->oa.o_oi = oa->o_oi;
1482         repbody->oa.o_valid = OBD_MD_FLID;
1483
1484         srvlock = oa->o_valid & OBD_MD_FLFLAGS &&
1485                   oa->o_flags & OBD_FL_SRVLOCK;
1486
1487         if (srvlock) {
1488                 rc = tgt_extent_lock(ns, &tsi->tsi_resid, start, end, &lh,
1489                                      LCK_PW, &flags);
1490                 if (rc != 0)
1491                         RETURN(rc);
1492         }
1493
1494         CDEBUG(D_INODE, "calling punch for object "DFID", valid = "LPX64
1495                ", start = "LPD64", end = "LPD64"\n", PFID(&tsi->tsi_fid),
1496                oa->o_valid, start, end);
1497
1498         fo = ofd_object_find_exists(tsi->tsi_env, ofd_exp(tsi->tsi_exp),
1499                                     &tsi->tsi_fid);
1500         if (IS_ERR(fo))
1501                 GOTO(out, rc = PTR_ERR(fo));
1502
1503         la_from_obdo(&info->fti_attr, oa,
1504                      OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
1505         info->fti_attr.la_size = start;
1506         info->fti_attr.la_valid |= LA_SIZE;
1507
1508         if (oa->o_valid & OBD_MD_FLFID) {
1509                 ff = &info->fti_mds_fid;
1510                 ofd_prepare_fidea(ff, oa);
1511         }
1512
1513         rc = ofd_object_punch(tsi->tsi_env, fo, start, end, &info->fti_attr,
1514                               ff);
1515         if (rc)
1516                 GOTO(out_put, rc);
1517
1518         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_PUNCH,
1519                          tsi->tsi_jobid, 1);
1520         EXIT;
1521 out_put:
1522         ofd_object_put(tsi->tsi_env, fo);
1523 out:
1524         if (srvlock)
1525                 tgt_extent_unlock(&lh, LCK_PW);
1526         if (rc == 0) {
1527                 /* we do not call this before to avoid lu_object_find() in
1528                  *  ->lvbo_update() holding another reference on the object.
1529                  * otherwise concurrent destroy can make the object unavailable
1530                  * for 2nd lu_object_find() waiting for the first reference
1531                  * to go... deadlock! */
1532                 res = ldlm_resource_get(ns, NULL, &tsi->tsi_resid,
1533                                         LDLM_EXTENT, 0);
1534                 if (res != NULL) {
1535                         ldlm_res_lvbo_update(res, NULL, 0);
1536                         ldlm_resource_putref(res);
1537                 }
1538         }
1539         return rc;
1540 }
1541
1542
1543 static int ofd_quotactl(struct tgt_session_info *tsi)
1544 {
1545         struct obd_quotactl     *oqctl, *repoqc;
1546         int                      rc;
1547
1548         ENTRY;
1549
1550         oqctl = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1551         if (oqctl == NULL)
1552                 RETURN(err_serious(-EPROTO));
1553
1554         repoqc = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1555         if (repoqc == NULL)
1556                 RETURN(err_serious(-ENOMEM));
1557
1558         /* report success for quota on/off for interoperability with current MDT
1559          * stack */
1560         if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1561                 RETURN(0);
1562
1563         *repoqc = *oqctl;
1564         rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom, repoqc);
1565
1566         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_QUOTACTL,
1567                          tsi->tsi_jobid, 1);
1568
1569         RETURN(rc);
1570 }
1571
1572 #define OBD_FAIL_OST_READ_NET   OBD_FAIL_OST_BRW_NET
1573 #define OBD_FAIL_OST_WRITE_NET  OBD_FAIL_OST_BRW_NET
1574 #define OST_BRW_READ    OST_READ
1575 #define OST_BRW_WRITE   OST_WRITE
1576
1577 static struct tgt_handler ofd_tgt_handlers[] = {
1578 TGT_RPC_HANDLER(OST_FIRST_OPC,
1579                 0,                      OST_CONNECT,    tgt_connect,
1580                 &RQF_CONNECT, LUSTRE_OBD_VERSION),
1581 TGT_RPC_HANDLER(OST_FIRST_OPC,
1582                 0,                      OST_DISCONNECT, tgt_disconnect,
1583                 &RQF_OST_DISCONNECT, LUSTRE_OBD_VERSION),
1584 TGT_RPC_HANDLER(OST_FIRST_OPC,
1585                 0,                      OST_SET_INFO,   ofd_set_info_hdl,
1586                 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
1587 TGT_OST_HDL(0,                          OST_GET_INFO,   ofd_get_info_hdl),
1588 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_GETATTR,    ofd_getattr_hdl),
1589 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
1590                                         OST_SETATTR,    ofd_setattr_hdl),
1591 TGT_OST_HDL(0           | HABEO_REFERO | MUTABOR,
1592                                         OST_CREATE,     ofd_create_hdl),
1593 TGT_OST_HDL(0           | HABEO_REFERO | MUTABOR,
1594                                         OST_DESTROY,    ofd_destroy_hdl),
1595 TGT_OST_HDL(0           | HABEO_REFERO, OST_STATFS,     ofd_statfs_hdl),
1596 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_BRW_READ,   tgt_brw_read),
1597 /* don't set CORPUS flag for brw_write because -ENOENT may be valid case */
1598 TGT_OST_HDL(MUTABOR,                    OST_BRW_WRITE,  tgt_brw_write),
1599 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
1600                                         OST_PUNCH,      ofd_punch_hdl),
1601 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_SYNC,       ofd_sync_hdl),
1602 TGT_OST_HDL(0           | HABEO_REFERO, OST_QUOTACTL,   ofd_quotactl),
1603 };
1604
1605 static struct tgt_opc_slice ofd_common_slice[] = {
1606         {
1607                 .tos_opc_start  = OST_FIRST_OPC,
1608                 .tos_opc_end    = OST_LAST_OPC,
1609                 .tos_hs         = ofd_tgt_handlers
1610         },
1611         {
1612                 .tos_opc_start  = OBD_FIRST_OPC,
1613                 .tos_opc_end    = OBD_LAST_OPC,
1614                 .tos_hs         = tgt_obd_handlers
1615         },
1616         {
1617                 .tos_opc_start  = LDLM_FIRST_OPC,
1618                 .tos_opc_end    = LDLM_LAST_OPC,
1619                 .tos_hs         = tgt_dlm_handlers
1620         },
1621         {
1622                 .tos_opc_start  = UPDATE_OBJ,
1623                 .tos_opc_end    = UPDATE_LAST_OPC,
1624                 .tos_hs         = tgt_out_handlers
1625         },
1626         {
1627                 .tos_opc_start  = SEQ_FIRST_OPC,
1628                 .tos_opc_end    = SEQ_LAST_OPC,
1629                 .tos_hs         = seq_handlers
1630         },
1631         {
1632                 .tos_hs         = NULL
1633         }
1634 };
1635
1636 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
1637                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
1638 {
1639         const char              *dev = lustre_cfg_string(cfg, 0);
1640         struct ofd_thread_info  *info = NULL;
1641         struct obd_device       *obd;
1642         struct obd_statfs       *osfs;
1643         int                      rc;
1644
1645         ENTRY;
1646
1647         obd = class_name2obd(dev);
1648         if (obd == NULL) {
1649                 CERROR("Cannot find obd with name %s\n", dev);
1650                 RETURN(-ENODEV);
1651         }
1652
1653         rc = lu_env_refill((struct lu_env *)env);
1654         if (rc != 0)
1655                 RETURN(rc);
1656
1657         obd->u.obt.obt_magic = OBT_MAGIC;
1658
1659         m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
1660         m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
1661
1662         spin_lock_init(&m->ofd_flags_lock);
1663         m->ofd_raid_degraded = 0;
1664         m->ofd_syncjournal = 0;
1665         ofd_slc_set(m);
1666         m->ofd_grant_compat_disable = 0;
1667
1668         /* statfs data */
1669         spin_lock_init(&m->ofd_osfs_lock);
1670         m->ofd_osfs_age = cfs_time_shift_64(-1000);
1671         m->ofd_osfs_unstable = 0;
1672         m->ofd_statfs_inflight = 0;
1673         m->ofd_osfs_inflight = 0;
1674
1675         /* grant data */
1676         spin_lock_init(&m->ofd_grant_lock);
1677         m->ofd_tot_dirty = 0;
1678         m->ofd_tot_granted = 0;
1679         m->ofd_tot_pending = 0;
1680         m->ofd_seq_count = 0;
1681
1682         spin_lock_init(&m->ofd_batch_lock);
1683         rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
1684         sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
1685
1686         obd->u.filter.fo_fl_oss_capa = 0;
1687         CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
1688         obd->u.filter.fo_capa_hash = init_capa_hash();
1689         if (obd->u.filter.fo_capa_hash == NULL)
1690                 RETURN(-ENOMEM);
1691
1692         m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
1693         m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
1694         /* set this lu_device to obd, because error handling need it */
1695         obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
1696
1697         rc = ofd_procfs_init(m);
1698         if (rc) {
1699                 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
1700                 RETURN(rc);
1701         }
1702
1703         /* No connection accepted until configurations will finish */
1704         spin_lock(&obd->obd_dev_lock);
1705         obd->obd_no_conn = 1;
1706         spin_unlock(&obd->obd_dev_lock);
1707         obd->obd_replayable = 1;
1708         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
1709                 char *str = lustre_cfg_string(cfg, 4);
1710
1711                 if (strchr(str, 'n')) {
1712                         CWARN("%s: recovery disabled\n", obd->obd_name);
1713                         obd->obd_replayable = 0;
1714                 }
1715         }
1716
1717         info = ofd_info_init(env, NULL);
1718         if (info == NULL)
1719                 RETURN(-EFAULT);
1720
1721         rc = ofd_stack_init(env, m, cfg);
1722         if (rc) {
1723                 CERROR("Can't init device stack, rc %d\n", rc);
1724                 GOTO(err_fini_proc, rc);
1725         }
1726
1727         ofd_procfs_add_brw_stats_symlink(m);
1728
1729         /* populate cached statfs data */
1730         osfs = &ofd_info(env)->fti_u.osfs;
1731         rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
1732         if (rc != 0) {
1733                 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
1734                 GOTO(err_fini_stack, rc);
1735         }
1736         if (!IS_PO2(osfs->os_bsize)) {
1737                 CERROR("%s: blocksize (%d) is not a power of 2\n",
1738                                 obd->obd_name, osfs->os_bsize);
1739                 GOTO(err_fini_stack, rc = -EPROTO);
1740         }
1741         m->ofd_blockbits = fls(osfs->os_bsize) - 1;
1742
1743         m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
1744         if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
1745                 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
1746
1747         snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
1748                  "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
1749         m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
1750                                               LDLM_NAMESPACE_SERVER,
1751                                               LDLM_NAMESPACE_GREEDY,
1752                                               LDLM_NS_TYPE_OST);
1753         if (m->ofd_namespace == NULL)
1754                 GOTO(err_fini_stack, rc = -ENOMEM);
1755         /* set obd_namespace for compatibility with old code */
1756         obd->obd_namespace = m->ofd_namespace;
1757         ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
1758         m->ofd_namespace->ns_lvbo = &ofd_lvbo;
1759         m->ofd_namespace->ns_lvbp = m;
1760
1761         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1762                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
1763
1764         dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
1765
1766         /* Allow at most ddp_grant_reserved% of the available filesystem space
1767          * to be granted to clients, so that any errors in the grant overhead
1768          * calculations do not allow granting more space to clients than can be
1769          * written. Assumes that in aggregate the grant overhead calculations do
1770          * not have more than ddp_grant_reserved% estimation error in them. */
1771         m->ofd_grant_ratio =
1772                 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
1773
1774         rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd, ofd_common_slice,
1775                       OBD_FAIL_OST_ALL_REQUEST_NET,
1776                       OBD_FAIL_OST_ALL_REPLY_NET);
1777         if (rc)
1778                 GOTO(err_free_ns, rc);
1779
1780         rc = ofd_fs_setup(env, m, obd);
1781         if (rc)
1782                 GOTO(err_fini_lut, rc);
1783
1784         RETURN(0);
1785 err_fini_lut:
1786         tgt_fini(env, &m->ofd_lut);
1787 err_free_ns:
1788         ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
1789         obd->obd_namespace = m->ofd_namespace = NULL;
1790 err_fini_stack:
1791         ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
1792 err_fini_proc:
1793         ofd_procfs_fini(m);
1794         return rc;
1795 }
1796
1797 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
1798 {
1799         struct obd_device *obd = ofd_obd(m);
1800         struct lu_device  *d = &m->ofd_dt_dev.dd_lu_dev;
1801
1802         lfsck_stop(env, m->ofd_osd, true);
1803         lfsck_degister(env, m->ofd_osd);
1804         target_recovery_fini(obd);
1805         obd_exports_barrier(obd);
1806         obd_zombie_barrier();
1807
1808         tgt_fini(env, &m->ofd_lut);
1809         ofd_fs_cleanup(env, m);
1810
1811         ofd_free_capa_keys(m);
1812         cleanup_capa_hash(obd->u.filter.fo_capa_hash);
1813
1814         if (m->ofd_namespace != NULL) {
1815                 ldlm_namespace_free(m->ofd_namespace, NULL,
1816                                     d->ld_obd->obd_force);
1817                 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
1818         }
1819
1820         ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
1821         ofd_procfs_fini(m);
1822         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
1823         server_put_mount(obd->obd_name);
1824         EXIT;
1825 }
1826
1827 static struct lu_device *ofd_device_fini(const struct lu_env *env,
1828                                          struct lu_device *d)
1829 {
1830         ENTRY;
1831         ofd_fini(env, ofd_dev(d));
1832         RETURN(NULL);
1833 }
1834
1835 static struct lu_device *ofd_device_free(const struct lu_env *env,
1836                                          struct lu_device *d)
1837 {
1838         struct ofd_device *m = ofd_dev(d);
1839
1840         dt_device_fini(&m->ofd_dt_dev);
1841         OBD_FREE_PTR(m);
1842         RETURN(NULL);
1843 }
1844
1845 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
1846                                           struct lu_device_type *t,
1847                                           struct lustre_cfg *cfg)
1848 {
1849         struct ofd_device *m;
1850         struct lu_device  *l;
1851         int                rc;
1852
1853         OBD_ALLOC_PTR(m);
1854         if (m == NULL)
1855                 return ERR_PTR(-ENOMEM);
1856
1857         l = &m->ofd_dt_dev.dd_lu_dev;
1858         dt_device_init(&m->ofd_dt_dev, t);
1859         rc = ofd_init0(env, m, t, cfg);
1860         if (rc != 0) {
1861                 ofd_device_free(env, l);
1862                 l = ERR_PTR(rc);
1863         }
1864
1865         return l;
1866 }
1867
1868 /* thread context key constructor/destructor */
1869 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
1870
1871 static void ofd_key_exit(const struct lu_context *ctx,
1872                          struct lu_context_key *key, void *data)
1873 {
1874         struct ofd_thread_info *info = data;
1875
1876         info->fti_env = NULL;
1877         info->fti_exp = NULL;
1878
1879         info->fti_xid = 0;
1880         info->fti_transno = 0;
1881         info->fti_pre_version = 0;
1882         info->fti_obj = NULL;
1883         info->fti_has_trans = 0;
1884         info->fti_mult_trans = 0;
1885         info->fti_used = 0;
1886
1887         memset(&info->fti_attr, 0, sizeof info->fti_attr);
1888 }
1889
1890 struct lu_context_key ofd_thread_key = {
1891         .lct_tags = LCT_DT_THREAD,
1892         .lct_init = ofd_key_init,
1893         .lct_fini = ofd_key_fini,
1894         .lct_exit = ofd_key_exit
1895 };
1896
1897 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
1898 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
1899
1900 static struct lu_device_type_operations ofd_device_type_ops = {
1901         .ldto_init              = ofd_type_init,
1902         .ldto_fini              = ofd_type_fini,
1903
1904         .ldto_start             = ofd_type_start,
1905         .ldto_stop              = ofd_type_stop,
1906
1907         .ldto_device_alloc      = ofd_device_alloc,
1908         .ldto_device_free       = ofd_device_free,
1909         .ldto_device_fini       = ofd_device_fini
1910 };
1911
1912 static struct lu_device_type ofd_device_type = {
1913         .ldt_tags       = LU_DEVICE_DT,
1914         .ldt_name       = LUSTRE_OST_NAME,
1915         .ldt_ops        = &ofd_device_type_ops,
1916         .ldt_ctx_tags   = LCT_DT_THREAD
1917 };
1918
1919 int __init ofd_init(void)
1920 {
1921         struct lprocfs_static_vars      lvars;
1922         int                             rc;
1923
1924         rc = lu_kmem_init(ofd_caches);
1925         if (rc)
1926                 return rc;
1927
1928         rc = ofd_fmd_init();
1929         if (rc) {
1930                 lu_kmem_fini(ofd_caches);
1931                 return(rc);
1932         }
1933
1934         lprocfs_ofd_init_vars(&lvars);
1935
1936         rc = class_register_type(&ofd_obd_ops, NULL, lvars.module_vars,
1937                                  LUSTRE_OST_NAME, &ofd_device_type);
1938         return rc;
1939 }
1940
1941 void __exit ofd_exit(void)
1942 {
1943         ofd_fmd_exit();
1944         lu_kmem_fini(ofd_caches);
1945         class_unregister_type(LUSTRE_OST_NAME);
1946 }
1947
1948 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
1949 MODULE_DESCRIPTION("Lustre Object Filtering Device");
1950 MODULE_LICENSE("GPL");
1951
1952 module_init(ofd_init);
1953 module_exit(ofd_exit);