Whamcloud - gitweb
LU-3319 procfs: provide framework for seq_file handling
[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         ENTRY;
929
930         LASSERT(tsi->tsi_ost_body != NULL);
931
932         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
933         if (repbody == NULL)
934                 RETURN(-ENOMEM);
935
936         repbody->oa.o_oi = tsi->tsi_ost_body->oa.o_oi;
937         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
938
939         srvlock = tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
940                   tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK;
941
942         if (srvlock) {
943                 if (unlikely(tsi->tsi_ost_body->oa.o_flags & OBD_FL_FLUSH))
944                         lock_mode = LCK_PW;
945
946                 rc = tgt_extent_lock(tsi->tsi_tgt->lut_obd->obd_namespace,
947                                      &tsi->tsi_resid, 0, OBD_OBJECT_EOF, &lh,
948                                      lock_mode, &flags);
949                 if (rc != 0)
950                         RETURN(rc);
951         }
952
953         fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
954         if (IS_ERR(fo))
955                 GOTO(out, rc = PTR_ERR(fo));
956
957         rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
958         if (rc == 0) {
959                 __u64    curr_version;
960
961                 obdo_from_la(&repbody->oa, &fti->fti_attr,
962                              OFD_VALID_FLAGS | LA_UID | LA_GID);
963                 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
964
965                 /* Store object version in reply */
966                 curr_version = dt_version_get(tsi->tsi_env,
967                                               ofd_object_child(fo));
968                 if ((__s64)curr_version != -EOPNOTSUPP) {
969                         repbody->oa.o_valid |= OBD_MD_FLDATAVERSION;
970                         repbody->oa.o_data_version = curr_version;
971                 }
972         }
973
974         ofd_object_put(tsi->tsi_env, fo);
975 out:
976         if (srvlock)
977                 tgt_extent_unlock(&lh, lock_mode);
978
979         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GETATTR,
980                          tsi->tsi_jobid, 1);
981
982         repbody->oa.o_valid |= OBD_MD_FLFLAGS;
983         repbody->oa.o_flags = OBD_FL_FLUSH;
984
985         RETURN(rc);
986 }
987
988 static int ofd_setattr_hdl(struct tgt_session_info *tsi)
989 {
990         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
991         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
992         struct ost_body         *body = tsi->tsi_ost_body;
993         struct ost_body         *repbody;
994         struct ldlm_resource    *res;
995         struct ofd_object       *fo;
996         struct filter_fid       *ff = NULL;
997         int                      rc = 0;
998
999         ENTRY;
1000
1001         LASSERT(body != NULL);
1002
1003         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1004         if (repbody == NULL)
1005                 RETURN(-ENOMEM);
1006
1007         repbody->oa.o_oi = body->oa.o_oi;
1008         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1009
1010         /* This would be very bad - accidentally truncating a file when
1011          * changing the time or similar - bug 12203. */
1012         if (body->oa.o_valid & OBD_MD_FLSIZE &&
1013             body->oa.o_size != OBD_OBJECT_EOF) {
1014                 static char mdsinum[48];
1015
1016                 if (body->oa.o_valid & OBD_MD_FLFID)
1017                         snprintf(mdsinum, sizeof(mdsinum) - 1,
1018                                  "of parent "DFID, body->oa.o_parent_seq,
1019                                  body->oa.o_parent_oid, 0);
1020                 else
1021                         mdsinum[0] = '\0';
1022
1023                 CERROR("%s: setattr from %s is trying to truncate object "DFID
1024                        " %s\n", ofd_name(ofd), obd_export_nid2str(tsi->tsi_exp),
1025                        PFID(&tsi->tsi_fid), mdsinum);
1026                 RETURN(-EPERM);
1027         }
1028
1029         fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1030         if (IS_ERR(fo))
1031                 GOTO(out, rc = PTR_ERR(fo));
1032
1033         la_from_obdo(&fti->fti_attr, &body->oa, body->oa.o_valid);
1034         fti->fti_attr.la_valid &= ~LA_TYPE;
1035
1036         if (body->oa.o_valid & OBD_MD_FLFID) {
1037                 ff = &fti->fti_mds_fid;
1038                 ofd_prepare_fidea(ff, &body->oa);
1039         }
1040
1041         /* setting objects attributes (including owner/group) */
1042         rc = ofd_attr_set(tsi->tsi_env, fo, &fti->fti_attr, ff);
1043         if (rc != 0)
1044                 GOTO(out_put, rc);
1045
1046         obdo_from_la(&repbody->oa, &fti->fti_attr,
1047                      OFD_VALID_FLAGS | LA_UID | LA_GID);
1048         tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1049
1050         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SETATTR,
1051                          tsi->tsi_jobid, 1);
1052         EXIT;
1053 out_put:
1054         ofd_object_put(tsi->tsi_env, fo);
1055 out:
1056         if (rc == 0) {
1057                 /* we do not call this before to avoid lu_object_find() in
1058                  *  ->lvbo_update() holding another reference on the object.
1059                  * otherwise concurrent destroy can make the object unavailable
1060                  * for 2nd lu_object_find() waiting for the first reference
1061                  * to go... deadlock! */
1062                 res = ldlm_resource_get(ofd->ofd_namespace, NULL,
1063                                         &tsi->tsi_resid, LDLM_EXTENT, 0);
1064                 if (res != NULL) {
1065                         ldlm_res_lvbo_update(res, NULL, 0);
1066                         ldlm_resource_putref(res);
1067                 }
1068         }
1069         return rc;
1070 }
1071
1072 static int ofd_create_hdl(struct tgt_session_info *tsi)
1073 {
1074         struct ost_body         *repbody;
1075         const struct obdo       *oa = &tsi->tsi_ost_body->oa;
1076         struct obdo             *rep_oa;
1077         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1078         obd_seq                  seq = ostid_seq(&oa->o_oi);
1079         obd_id                   oid = ostid_id(&oa->o_oi);
1080         struct ofd_seq          *oseq;
1081         int                      rc = 0, diff;
1082         int                      sync_trans = 0;
1083
1084         ENTRY;
1085
1086         if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1087                 RETURN(-EROFS);
1088
1089         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1090         if (repbody == NULL)
1091                 RETURN(-ENOMEM);
1092
1093         rep_oa = &repbody->oa;
1094         rep_oa->o_oi = oa->o_oi;
1095
1096         LASSERT(seq >= FID_SEQ_OST_MDT0);
1097         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1098
1099         CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1100
1101         oseq = ofd_seq_load(tsi->tsi_env, ofd, seq);
1102         if (IS_ERR(oseq)) {
1103                 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
1104                        ofd_name(ofd), seq, PTR_ERR(oseq));
1105                 RETURN(-EINVAL);
1106         }
1107
1108         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1109             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1110                 if (!ofd_obd(ofd)->obd_recovering ||
1111                     oid > ofd_seq_last_oid(oseq)) {
1112                         CERROR("%s: recreate objid "DOSTID" > last id "LPU64
1113                                "\n", ofd_name(ofd), POSTID(&oa->o_oi),
1114                                ofd_seq_last_oid(oseq));
1115                         GOTO(out_nolock, rc = -EINVAL);
1116                 }
1117                 /* Do nothing here, we re-create objects during recovery
1118                  * upon write replay, see ofd_preprw_write() */
1119                 GOTO(out_nolock, rc = 0);
1120         }
1121         /* former ofd_handle_precreate */
1122         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1123             (oa->o_flags & OBD_FL_DELORPHAN)) {
1124                 /* destroy orphans */
1125                 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1126                     tsi->tsi_exp->exp_conn_cnt) {
1127                         CERROR("%s: dropping old orphan cleanup request\n",
1128                                ofd_name(ofd));
1129                         GOTO(out_nolock, rc = 0);
1130                 }
1131                 /* This causes inflight precreates to abort and drop lock */
1132                 oseq->os_destroys_in_progress = 1;
1133                 mutex_lock(&oseq->os_create_lock);
1134                 if (!oseq->os_destroys_in_progress) {
1135                         CERROR("%s:["LPU64"] destroys_in_progress already"
1136                                " cleared\n", ofd_name(ofd), seq);
1137                         ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1138                         GOTO(out, rc = 0);
1139                 }
1140                 diff = oid - ofd_seq_last_oid(oseq);
1141                 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1142                         ofd_seq_last_oid(oseq), diff);
1143                 if (-diff > OST_MAX_PRECREATE) {
1144                         /* FIXME: should reset precreate_next_id on MDS */
1145                         rc = 0;
1146                 } else if (diff < 0) {
1147                         rc = ofd_orphans_destroy(tsi->tsi_env, tsi->tsi_exp,
1148                                                  ofd, rep_oa);
1149                         oseq->os_destroys_in_progress = 0;
1150                 } else {
1151                         /* XXX: Used by MDS for the first time! */
1152                         oseq->os_destroys_in_progress = 0;
1153                 }
1154         } else {
1155                 mutex_lock(&oseq->os_create_lock);
1156                 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1157                     tsi->tsi_exp->exp_conn_cnt) {
1158                         CERROR("%s: dropping old precreate request\n",
1159                                ofd_name(ofd));
1160                         GOTO(out, rc = 0);
1161                 }
1162                 /* only precreate if seq is 0, IDIF or normal and also o_id
1163                  * must be specfied */
1164                 if ((!fid_seq_is_mdt(seq) && !fid_seq_is_norm(seq) &&
1165                      !fid_seq_is_idif(seq)) || oid == 0) {
1166                         diff = 1; /* shouldn't we create this right now? */
1167                 } else {
1168                         diff = oid - ofd_seq_last_oid(oseq);
1169                         /* Do sync create if the seq is about to used up */
1170                         if (fid_seq_is_idif(seq) || fid_seq_is_mdt0(seq)) {
1171                                 if (unlikely(oid >= IDIF_MAX_OID - 1))
1172                                         sync_trans = 1;
1173                         } else if (fid_seq_is_norm(seq)) {
1174                                 if (unlikely(oid >=
1175                                              LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
1176                                         sync_trans = 1;
1177                         } else {
1178                                 CERROR("%s : invalid o_seq "DOSTID"\n",
1179                                        ofd_name(ofd), POSTID(&oa->o_oi));
1180                                 GOTO(out, rc = -EINVAL);
1181                         }
1182                 }
1183         }
1184         if (diff > 0) {
1185                 cfs_time_t       enough_time = cfs_time_shift(DISK_TIMEOUT);
1186                 obd_id           next_id;
1187                 int              created = 0;
1188                 int              count;
1189
1190                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1191                     !(oa->o_flags & OBD_FL_DELORPHAN)) {
1192                         /* don't enforce grant during orphan recovery */
1193                         rc = ofd_grant_create(tsi->tsi_env,
1194                                               ofd_obd(ofd)->obd_self_export,
1195                                               &diff);
1196                         if (rc) {
1197                                 CDEBUG(D_HA, "%s: failed to acquire grant "
1198                                        "space for precreate (%d): rc = %d\n",
1199                                        ofd_name(ofd), diff, rc);
1200                                 diff = 0;
1201                         }
1202                 }
1203
1204                 /* This can happen if a new OST is formatted and installed
1205                  * in place of an old one at the same index.  Instead of
1206                  * precreating potentially millions of deleted old objects
1207                  * (possibly filling the OST), only precreate the last batch.
1208                  * LFSCK will eventually clean up any orphans. LU-14 */
1209                 if (diff > 5 * OST_MAX_PRECREATE) {
1210                         diff = OST_MAX_PRECREATE / 2;
1211                         LCONSOLE_WARN("%s: precreate FID "DOSTID" is over %u "
1212                                       "larger than the LAST_ID "DOSTID", only "
1213                                       "precreating the last %u objects.\n",
1214                                       ofd_name(ofd), POSTID(&oa->o_oi),
1215                                       5 * OST_MAX_PRECREATE,
1216                                       POSTID(&oseq->os_oi), diff);
1217                         ofd_seq_last_oid_set(oseq, ostid_id(&oa->o_oi) - diff);
1218                 }
1219
1220                 while (diff > 0) {
1221                         next_id = ofd_seq_last_oid(oseq) + 1;
1222                         count = ofd_precreate_batch(ofd, diff);
1223
1224                         CDEBUG(D_HA, "%s: reserve %d objects in group "LPX64
1225                                " at "LPU64"\n", ofd_name(ofd),
1226                                count, seq, next_id);
1227
1228                         if (cfs_time_after(jiffies, enough_time)) {
1229                                 LCONSOLE_WARN("%s: Slow creates, %d/%d objects"
1230                                               " created at a rate of %d/s\n",
1231                                               ofd_name(ofd), created,
1232                                               diff + created,
1233                                               created / DISK_TIMEOUT);
1234                                 break;
1235                         }
1236
1237                         rc = ofd_precreate_objects(tsi->tsi_env, ofd, next_id,
1238                                                    oseq, count, sync_trans);
1239                         if (rc > 0) {
1240                                 created += rc;
1241                                 diff -= rc;
1242                         } else if (rc < 0) {
1243                                 break;
1244                         }
1245                 }
1246                 if (created > 0)
1247                         /* some objects got created, we can return
1248                          * them, even if last creation failed */
1249                         rc = 0;
1250                 else
1251                         CERROR("%s: unable to precreate: rc = %d\n",
1252                                ofd_name(ofd), rc);
1253
1254                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1255                     !(oa->o_flags & OBD_FL_DELORPHAN))
1256                         ofd_grant_commit(tsi->tsi_env,
1257                                          ofd_obd(ofd)->obd_self_export, rc);
1258
1259                 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1260         }
1261         EXIT;
1262         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_CREATE,
1263                          tsi->tsi_jobid, 1);
1264 out:
1265         mutex_unlock(&oseq->os_create_lock);
1266 out_nolock:
1267         if (rc == 0)
1268                 rep_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1269
1270         ofd_seq_put(tsi->tsi_env, oseq);
1271         return rc;
1272 }
1273
1274 static int ofd_destroy_hdl(struct tgt_session_info *tsi)
1275 {
1276         const struct ost_body   *body = tsi->tsi_ost_body;
1277         struct ost_body         *repbody;
1278         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1279         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1280         obd_count                count;
1281         int                      rc = 0;
1282
1283         ENTRY;
1284
1285         if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1286                 RETURN(-EROFS);
1287
1288         /* This is old case for clients before Lustre 2.4 */
1289         /* If there's a DLM request, cancel the locks mentioned in it */
1290         if (req_capsule_field_present(tsi->tsi_pill, &RMF_DLM_REQ,
1291                                       RCL_CLIENT)) {
1292                 struct ldlm_request *dlm;
1293
1294                 dlm = req_capsule_client_get(tsi->tsi_pill, &RMF_DLM_REQ);
1295                 if (dlm == NULL)
1296                         RETURN(-EFAULT);
1297                 ldlm_request_cancel(tgt_ses_req(tsi), dlm, 0);
1298         }
1299
1300         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1301         repbody->oa.o_oi = body->oa.o_oi;
1302
1303         /* check that o_misc makes sense */
1304         if (body->oa.o_valid & OBD_MD_FLOBJCOUNT)
1305                 count = body->oa.o_misc;
1306         else
1307                 count = 1; /* default case - single destroy */
1308
1309         CDEBUG(D_HA, "%s: Destroy object "DOSTID" count %d\n", ofd_name(ofd),
1310                POSTID(&body->oa.o_oi), count);
1311         while (count > 0) {
1312                 int lrc;
1313
1314                 lrc = ostid_to_fid(&fti->fti_fid, &repbody->oa.o_oi, 0);
1315                 if (lrc != 0) {
1316                         if (rc == 0)
1317                                 rc = lrc;
1318                         GOTO(out, rc);
1319                 }
1320                 lrc = ofd_destroy_by_fid(tsi->tsi_env, ofd, &fti->fti_fid, 0);
1321                 if (lrc == -ENOENT) {
1322                         CDEBUG(D_INODE,
1323                                "%s: destroying non-existent object "DFID"\n",
1324                                ofd_name(ofd), PFID(&fti->fti_fid));
1325                         /* rewrite rc with -ENOENT only if it is 0 */
1326                         if (rc == 0)
1327                                 rc = lrc;
1328                 } else if (lrc != 0) {
1329                         CERROR("%s: error destroying object "DFID": %d\n",
1330                                ofd_name(ofd), PFID(&fti->fti_fid),
1331                                rc);
1332                         rc = lrc;
1333                 }
1334                 count--;
1335                 ostid_inc_id(&repbody->oa.o_oi);
1336         }
1337
1338         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_DESTROY,
1339                          tsi->tsi_jobid, 1);
1340 out:
1341         RETURN(rc);
1342 }
1343
1344 static int ofd_statfs_hdl(struct tgt_session_info *tsi)
1345 {
1346         struct obd_statfs       *osfs;
1347         int                      rc;
1348
1349         ENTRY;
1350
1351         osfs = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_STATFS);
1352
1353         rc = ofd_statfs(tsi->tsi_env, tsi->tsi_exp, osfs,
1354                         cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 0);
1355         if (rc != 0)
1356                 CERROR("%s: statfs failed: rc = %d\n",
1357                        tgt_name(tsi->tsi_tgt), rc);
1358
1359         if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_EINPROGRESS))
1360                 rc = -EINPROGRESS;
1361
1362         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_STATFS,
1363                          tsi->tsi_jobid, 1);
1364
1365         RETURN(rc);
1366 }
1367
1368 static int ofd_sync_hdl(struct tgt_session_info *tsi)
1369 {
1370         struct ost_body         *body = tsi->tsi_ost_body;
1371         struct ost_body         *repbody;
1372         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1373         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1374         struct ofd_object       *fo = NULL;
1375         int                      rc = 0;
1376
1377         ENTRY;
1378
1379         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1380
1381         /* if no objid is specified, it means "sync whole filesystem" */
1382         if (!fid_is_zero(&tsi->tsi_fid)) {
1383                 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1384                 if (IS_ERR(fo))
1385                         RETURN(PTR_ERR(fo));
1386         }
1387
1388         rc = tgt_sync(tsi->tsi_env, tsi->tsi_tgt,
1389                       fo != NULL ? ofd_object_child(fo) : NULL);
1390         if (rc)
1391                 GOTO(put, rc);
1392
1393         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SYNC,
1394                          tsi->tsi_jobid, 1);
1395         if (fo == NULL)
1396                 RETURN(0);
1397
1398         repbody->oa.o_oi = body->oa.o_oi;
1399         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1400
1401         rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1402         if (rc == 0)
1403                 obdo_from_la(&repbody->oa, &fti->fti_attr,
1404                              OFD_VALID_FLAGS);
1405         else
1406                 /* don't return rc from getattr */
1407                 rc = 0;
1408         EXIT;
1409 put:
1410         if (fo != NULL)
1411                 ofd_object_put(tsi->tsi_env, fo);
1412         return rc;
1413 }
1414
1415 static int ofd_punch_hdl(struct tgt_session_info *tsi)
1416 {
1417         const struct obdo       *oa = &tsi->tsi_ost_body->oa;
1418         struct ost_body         *repbody;
1419         struct ofd_thread_info  *info = tsi2ofd_info(tsi);
1420         struct ldlm_namespace   *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
1421         struct ldlm_resource    *res;
1422         struct ofd_object       *fo;
1423         struct filter_fid       *ff = NULL;
1424         __u64                    flags = 0;
1425         struct lustre_handle     lh = { 0, };
1426         int                      rc;
1427         __u64                    start, end;
1428         bool                     srvlock;
1429
1430         ENTRY;
1431
1432         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
1433         CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
1434
1435         if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
1436             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
1437                 RETURN(err_serious(-EPROTO));
1438
1439         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1440         if (repbody == NULL)
1441                 RETURN(err_serious(-ENOMEM));
1442
1443         /* punch start,end are passed in o_size,o_blocks throught wire */
1444         start = oa->o_size;
1445         end = oa->o_blocks;
1446
1447         if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
1448                 RETURN(-EPROTO);
1449
1450         /* standard truncate optimization: if file body is completely
1451          * destroyed, don't send data back to the server. */
1452         if (start == 0)
1453                 flags |= LDLM_FL_AST_DISCARD_DATA;
1454
1455         repbody->oa.o_oi = oa->o_oi;
1456         repbody->oa.o_valid = OBD_MD_FLID;
1457
1458         srvlock = oa->o_valid & OBD_MD_FLFLAGS &&
1459                   oa->o_flags & OBD_FL_SRVLOCK;
1460
1461         if (srvlock) {
1462                 rc = tgt_extent_lock(ns, &tsi->tsi_resid, start, end, &lh,
1463                                      LCK_PW, &flags);
1464                 if (rc != 0)
1465                         RETURN(rc);
1466         }
1467
1468         CDEBUG(D_INODE, "calling punch for object "DFID", valid = "LPX64
1469                ", start = "LPD64", end = "LPD64"\n", PFID(&tsi->tsi_fid),
1470                oa->o_valid, start, end);
1471
1472         fo = ofd_object_find_exists(tsi->tsi_env, ofd_exp(tsi->tsi_exp),
1473                                     &tsi->tsi_fid);
1474         if (IS_ERR(fo))
1475                 GOTO(out, rc = PTR_ERR(fo));
1476
1477         la_from_obdo(&info->fti_attr, oa,
1478                      OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
1479         info->fti_attr.la_size = start;
1480         info->fti_attr.la_valid |= LA_SIZE;
1481
1482         if (oa->o_valid & OBD_MD_FLFID) {
1483                 ff = &info->fti_mds_fid;
1484                 ofd_prepare_fidea(ff, oa);
1485         }
1486
1487         rc = ofd_object_punch(tsi->tsi_env, fo, start, end, &info->fti_attr,
1488                               ff);
1489         if (rc)
1490                 GOTO(out_put, rc);
1491
1492         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_PUNCH,
1493                          tsi->tsi_jobid, 1);
1494         EXIT;
1495 out_put:
1496         ofd_object_put(tsi->tsi_env, fo);
1497 out:
1498         if (srvlock)
1499                 tgt_extent_unlock(&lh, LCK_PW);
1500         if (rc == 0) {
1501                 /* we do not call this before to avoid lu_object_find() in
1502                  *  ->lvbo_update() holding another reference on the object.
1503                  * otherwise concurrent destroy can make the object unavailable
1504                  * for 2nd lu_object_find() waiting for the first reference
1505                  * to go... deadlock! */
1506                 res = ldlm_resource_get(ns, NULL, &tsi->tsi_resid,
1507                                         LDLM_EXTENT, 0);
1508                 if (res != NULL) {
1509                         ldlm_res_lvbo_update(res, NULL, 0);
1510                         ldlm_resource_putref(res);
1511                 }
1512         }
1513         return rc;
1514 }
1515
1516
1517 static int ofd_quotactl(struct tgt_session_info *tsi)
1518 {
1519         struct obd_quotactl     *oqctl, *repoqc;
1520         int                      rc;
1521
1522         ENTRY;
1523
1524         oqctl = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1525         if (oqctl == NULL)
1526                 RETURN(err_serious(-EPROTO));
1527
1528         repoqc = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1529         if (repoqc == NULL)
1530                 RETURN(err_serious(-ENOMEM));
1531
1532         /* report success for quota on/off for interoperability with current MDT
1533          * stack */
1534         if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1535                 RETURN(0);
1536
1537         *repoqc = *oqctl;
1538         rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom, repoqc);
1539
1540         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_QUOTACTL,
1541                          tsi->tsi_jobid, 1);
1542
1543         RETURN(rc);
1544 }
1545
1546 #define OBD_FAIL_OST_READ_NET   OBD_FAIL_OST_BRW_NET
1547 #define OBD_FAIL_OST_WRITE_NET  OBD_FAIL_OST_BRW_NET
1548 #define OST_BRW_READ    OST_READ
1549 #define OST_BRW_WRITE   OST_WRITE
1550
1551 static struct tgt_handler ofd_tgt_handlers[] = {
1552 TGT_RPC_HANDLER(OST_FIRST_OPC,
1553                 0,                      OST_CONNECT,    tgt_connect,
1554                 &RQF_CONNECT, LUSTRE_OBD_VERSION),
1555 TGT_RPC_HANDLER(OST_FIRST_OPC,
1556                 0,                      OST_DISCONNECT, tgt_disconnect,
1557                 &RQF_OST_DISCONNECT, LUSTRE_OBD_VERSION),
1558 TGT_RPC_HANDLER(OST_FIRST_OPC,
1559                 0,                      OST_SET_INFO,   ofd_set_info_hdl,
1560                 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
1561 TGT_OST_HDL(0,                          OST_GET_INFO,   ofd_get_info_hdl),
1562 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_GETATTR,    ofd_getattr_hdl),
1563 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
1564                                         OST_SETATTR,    ofd_setattr_hdl),
1565 TGT_OST_HDL(0           | HABEO_REFERO | MUTABOR,
1566                                         OST_CREATE,     ofd_create_hdl),
1567 TGT_OST_HDL(0           | HABEO_REFERO | MUTABOR,
1568                                         OST_DESTROY,    ofd_destroy_hdl),
1569 TGT_OST_HDL(0           | HABEO_REFERO, OST_STATFS,     ofd_statfs_hdl),
1570 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_BRW_READ,   tgt_brw_read),
1571 /* don't set CORPUS flag for brw_write because -ENOENT may be valid case */
1572 TGT_OST_HDL(MUTABOR,                    OST_BRW_WRITE,  tgt_brw_write),
1573 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
1574                                         OST_PUNCH,      ofd_punch_hdl),
1575 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_SYNC,       ofd_sync_hdl),
1576 TGT_OST_HDL(0           | HABEO_REFERO, OST_QUOTACTL,   ofd_quotactl),
1577 };
1578
1579 static struct tgt_opc_slice ofd_common_slice[] = {
1580         {
1581                 .tos_opc_start  = OST_FIRST_OPC,
1582                 .tos_opc_end    = OST_LAST_OPC,
1583                 .tos_hs         = ofd_tgt_handlers
1584         },
1585         {
1586                 .tos_opc_start  = OBD_FIRST_OPC,
1587                 .tos_opc_end    = OBD_LAST_OPC,
1588                 .tos_hs         = tgt_obd_handlers
1589         },
1590         {
1591                 .tos_opc_start  = LDLM_FIRST_OPC,
1592                 .tos_opc_end    = LDLM_LAST_OPC,
1593                 .tos_hs         = tgt_dlm_handlers
1594         },
1595         {
1596                 .tos_opc_start  = UPDATE_OBJ,
1597                 .tos_opc_end    = UPDATE_LAST_OPC,
1598                 .tos_hs         = tgt_out_handlers
1599         },
1600         {
1601                 .tos_opc_start  = SEQ_FIRST_OPC,
1602                 .tos_opc_end    = SEQ_LAST_OPC,
1603                 .tos_hs         = seq_handlers
1604         },
1605         {
1606                 .tos_hs         = NULL
1607         }
1608 };
1609
1610 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
1611                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
1612 {
1613         const char              *dev = lustre_cfg_string(cfg, 0);
1614         struct ofd_thread_info  *info = NULL;
1615         struct obd_device       *obd;
1616         struct obd_statfs       *osfs;
1617         int                      rc;
1618
1619         ENTRY;
1620
1621         obd = class_name2obd(dev);
1622         if (obd == NULL) {
1623                 CERROR("Cannot find obd with name %s\n", dev);
1624                 RETURN(-ENODEV);
1625         }
1626
1627         rc = lu_env_refill((struct lu_env *)env);
1628         if (rc != 0)
1629                 RETURN(rc);
1630
1631         obd->u.obt.obt_magic = OBT_MAGIC;
1632
1633         m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
1634         m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
1635
1636         spin_lock_init(&m->ofd_flags_lock);
1637         m->ofd_raid_degraded = 0;
1638         m->ofd_syncjournal = 0;
1639         ofd_slc_set(m);
1640         m->ofd_grant_compat_disable = 0;
1641
1642         /* statfs data */
1643         spin_lock_init(&m->ofd_osfs_lock);
1644         m->ofd_osfs_age = cfs_time_shift_64(-1000);
1645         m->ofd_osfs_unstable = 0;
1646         m->ofd_statfs_inflight = 0;
1647         m->ofd_osfs_inflight = 0;
1648
1649         /* grant data */
1650         spin_lock_init(&m->ofd_grant_lock);
1651         m->ofd_tot_dirty = 0;
1652         m->ofd_tot_granted = 0;
1653         m->ofd_tot_pending = 0;
1654         m->ofd_seq_count = 0;
1655
1656         spin_lock_init(&m->ofd_batch_lock);
1657         rwlock_init(&obd->u.filter.fo_sptlrpc_lock);
1658         sptlrpc_rule_set_init(&obd->u.filter.fo_sptlrpc_rset);
1659
1660         obd->u.filter.fo_fl_oss_capa = 0;
1661         CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
1662         obd->u.filter.fo_capa_hash = init_capa_hash();
1663         if (obd->u.filter.fo_capa_hash == NULL)
1664                 RETURN(-ENOMEM);
1665
1666         m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
1667         m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
1668         /* set this lu_device to obd, because error handling need it */
1669         obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
1670
1671         rc = ofd_procfs_init(m);
1672         if (rc) {
1673                 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
1674                 RETURN(rc);
1675         }
1676
1677         /* No connection accepted until configurations will finish */
1678         spin_lock(&obd->obd_dev_lock);
1679         obd->obd_no_conn = 1;
1680         spin_unlock(&obd->obd_dev_lock);
1681         obd->obd_replayable = 1;
1682         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
1683                 char *str = lustre_cfg_string(cfg, 4);
1684
1685                 if (strchr(str, 'n')) {
1686                         CWARN("%s: recovery disabled\n", obd->obd_name);
1687                         obd->obd_replayable = 0;
1688                 }
1689         }
1690
1691         info = ofd_info_init(env, NULL);
1692         if (info == NULL)
1693                 RETURN(-EFAULT);
1694
1695         rc = ofd_stack_init(env, m, cfg);
1696         if (rc) {
1697                 CERROR("Can't init device stack, rc %d\n", rc);
1698                 GOTO(err_fini_proc, rc);
1699         }
1700
1701         ofd_procfs_add_brw_stats_symlink(m);
1702
1703         /* populate cached statfs data */
1704         osfs = &ofd_info(env)->fti_u.osfs;
1705         rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
1706         if (rc != 0) {
1707                 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
1708                 GOTO(err_fini_stack, rc);
1709         }
1710         if (!IS_PO2(osfs->os_bsize)) {
1711                 CERROR("%s: blocksize (%d) is not a power of 2\n",
1712                                 obd->obd_name, osfs->os_bsize);
1713                 GOTO(err_fini_stack, rc = -EPROTO);
1714         }
1715         m->ofd_blockbits = fls(osfs->os_bsize) - 1;
1716
1717         m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
1718         if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
1719                 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
1720
1721         snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
1722                  "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
1723         m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
1724                                               LDLM_NAMESPACE_SERVER,
1725                                               LDLM_NAMESPACE_GREEDY,
1726                                               LDLM_NS_TYPE_OST);
1727         if (m->ofd_namespace == NULL)
1728                 GOTO(err_fini_stack, rc = -ENOMEM);
1729         /* set obd_namespace for compatibility with old code */
1730         obd->obd_namespace = m->ofd_namespace;
1731         ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
1732         m->ofd_namespace->ns_lvbo = &ofd_lvbo;
1733         m->ofd_namespace->ns_lvbp = m;
1734
1735         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1736                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
1737
1738         dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
1739
1740         /* Allow at most ddp_grant_reserved% of the available filesystem space
1741          * to be granted to clients, so that any errors in the grant overhead
1742          * calculations do not allow granting more space to clients than can be
1743          * written. Assumes that in aggregate the grant overhead calculations do
1744          * not have more than ddp_grant_reserved% estimation error in them. */
1745         m->ofd_grant_ratio =
1746                 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
1747
1748         rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd, ofd_common_slice,
1749                       OBD_FAIL_OST_ALL_REQUEST_NET,
1750                       OBD_FAIL_OST_ALL_REPLY_NET);
1751         if (rc)
1752                 GOTO(err_free_ns, rc);
1753
1754         rc = ofd_fs_setup(env, m, obd);
1755         if (rc)
1756                 GOTO(err_fini_lut, rc);
1757
1758         RETURN(0);
1759 err_fini_lut:
1760         tgt_fini(env, &m->ofd_lut);
1761 err_free_ns:
1762         ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
1763         obd->obd_namespace = m->ofd_namespace = NULL;
1764 err_fini_stack:
1765         ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
1766 err_fini_proc:
1767         ofd_procfs_fini(m);
1768         return rc;
1769 }
1770
1771 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
1772 {
1773         struct obd_device *obd = ofd_obd(m);
1774         struct lu_device  *d = &m->ofd_dt_dev.dd_lu_dev;
1775
1776         lfsck_stop(env, m->ofd_osd, true);
1777         lfsck_degister(env, m->ofd_osd);
1778         target_recovery_fini(obd);
1779         obd_exports_barrier(obd);
1780         obd_zombie_barrier();
1781
1782         tgt_fini(env, &m->ofd_lut);
1783         ofd_fs_cleanup(env, m);
1784
1785         ofd_free_capa_keys(m);
1786         cleanup_capa_hash(obd->u.filter.fo_capa_hash);
1787
1788         if (m->ofd_namespace != NULL) {
1789                 ldlm_namespace_free(m->ofd_namespace, NULL,
1790                                     d->ld_obd->obd_force);
1791                 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
1792         }
1793
1794         ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
1795         ofd_procfs_fini(m);
1796         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
1797         server_put_mount(obd->obd_name);
1798         EXIT;
1799 }
1800
1801 static struct lu_device *ofd_device_fini(const struct lu_env *env,
1802                                          struct lu_device *d)
1803 {
1804         ENTRY;
1805         ofd_fini(env, ofd_dev(d));
1806         RETURN(NULL);
1807 }
1808
1809 static struct lu_device *ofd_device_free(const struct lu_env *env,
1810                                          struct lu_device *d)
1811 {
1812         struct ofd_device *m = ofd_dev(d);
1813
1814         dt_device_fini(&m->ofd_dt_dev);
1815         OBD_FREE_PTR(m);
1816         RETURN(NULL);
1817 }
1818
1819 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
1820                                           struct lu_device_type *t,
1821                                           struct lustre_cfg *cfg)
1822 {
1823         struct ofd_device *m;
1824         struct lu_device  *l;
1825         int                rc;
1826
1827         OBD_ALLOC_PTR(m);
1828         if (m == NULL)
1829                 return ERR_PTR(-ENOMEM);
1830
1831         l = &m->ofd_dt_dev.dd_lu_dev;
1832         dt_device_init(&m->ofd_dt_dev, t);
1833         rc = ofd_init0(env, m, t, cfg);
1834         if (rc != 0) {
1835                 ofd_device_free(env, l);
1836                 l = ERR_PTR(rc);
1837         }
1838
1839         return l;
1840 }
1841
1842 /* thread context key constructor/destructor */
1843 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
1844
1845 static void ofd_key_exit(const struct lu_context *ctx,
1846                          struct lu_context_key *key, void *data)
1847 {
1848         struct ofd_thread_info *info = data;
1849
1850         info->fti_env = NULL;
1851         info->fti_exp = NULL;
1852
1853         info->fti_xid = 0;
1854         info->fti_pre_version = 0;
1855         info->fti_used = 0;
1856
1857         memset(&info->fti_attr, 0, sizeof info->fti_attr);
1858 }
1859
1860 struct lu_context_key ofd_thread_key = {
1861         .lct_tags = LCT_DT_THREAD,
1862         .lct_init = ofd_key_init,
1863         .lct_fini = ofd_key_fini,
1864         .lct_exit = ofd_key_exit
1865 };
1866
1867 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
1868 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
1869
1870 static struct lu_device_type_operations ofd_device_type_ops = {
1871         .ldto_init              = ofd_type_init,
1872         .ldto_fini              = ofd_type_fini,
1873
1874         .ldto_start             = ofd_type_start,
1875         .ldto_stop              = ofd_type_stop,
1876
1877         .ldto_device_alloc      = ofd_device_alloc,
1878         .ldto_device_free       = ofd_device_free,
1879         .ldto_device_fini       = ofd_device_fini
1880 };
1881
1882 static struct lu_device_type ofd_device_type = {
1883         .ldt_tags       = LU_DEVICE_DT,
1884         .ldt_name       = LUSTRE_OST_NAME,
1885         .ldt_ops        = &ofd_device_type_ops,
1886         .ldt_ctx_tags   = LCT_DT_THREAD
1887 };
1888
1889 int __init ofd_init(void)
1890 {
1891         struct lprocfs_static_vars      lvars;
1892         int                             rc;
1893
1894         rc = lu_kmem_init(ofd_caches);
1895         if (rc)
1896                 return rc;
1897
1898         rc = ofd_fmd_init();
1899         if (rc) {
1900                 lu_kmem_fini(ofd_caches);
1901                 return(rc);
1902         }
1903
1904         lprocfs_ofd_init_vars(&lvars);
1905
1906         rc = class_register_type(&ofd_obd_ops, NULL, NULL,
1907 #ifndef HAVE_ONLY_PROCFS_SEQ
1908                                 lvars.module_vars,
1909 #endif
1910                                 LUSTRE_OST_NAME, &ofd_device_type);
1911         return rc;
1912 }
1913
1914 void __exit ofd_exit(void)
1915 {
1916         ofd_fmd_exit();
1917         lu_kmem_fini(ofd_caches);
1918         class_unregister_type(LUSTRE_OST_NAME);
1919 }
1920
1921 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
1922 MODULE_DESCRIPTION("Lustre Object Filtering Device");
1923 MODULE_LICENSE("GPL");
1924
1925 module_init(ofd_init);
1926 module_exit(ofd_exit);