Whamcloud - gitweb
LU-1095 ofd: Silence 'slow create' warnings
[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                        ofd_name(m), 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                        ofd_name(m), 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
186         EXIT;
187 }
188
189 /* For interoperability, see mdt_interop_param[]. */
190 static struct cfg_interop_param ofd_interop_param[] = {
191         { "ost.quota_type",     NULL },
192         { NULL }
193 };
194
195 /* Some parameters were moved from ofd to osd and only their
196  * symlinks were kept in ofd by LU-3106. They are:
197  * -writehthrough_cache_enable
198  * -readcache_max_filese
199  * -read_cache_enable
200  * -brw_stats
201  * Since they are not included by the static lprocfs var list,
202  * a pre-check is added for them to avoid "unknown param" error
203  * message confuses the customer. If they are matched in this
204  * check, they will be passed to the osd directly.
205  */
206 static bool match_symlink_param(char *param)
207 {
208         char *sval;
209         int paramlen;
210
211         if (class_match_param(param, PARAM_OST, &param) == 0) {
212                 sval = strchr(param, '=');
213                 if (sval != NULL) {
214                         paramlen = sval - param;
215                         if (strncmp(param, "writethrough_cache_enable",
216                                     paramlen) == 0 ||
217                             strncmp(param, "readcache_max_filesize",
218                                     paramlen) == 0 ||
219                             strncmp(param, "read_cache_enable",
220                                     paramlen) == 0 ||
221                             strncmp(param, "brw_stats", paramlen) == 0)
222                                 return true;
223                 }
224         }
225
226         return false;
227 }
228
229 /* used by MGS to process specific configurations */
230 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
231                               struct lustre_cfg *cfg)
232 {
233         struct ofd_device       *m = ofd_dev(d);
234         struct dt_device        *dt_next = m->ofd_osd;
235         struct lu_device        *next = &dt_next->dd_lu_dev;
236         int                      rc;
237
238         ENTRY;
239
240         switch (cfg->lcfg_command) {
241         case LCFG_PARAM: {
242                 struct obd_device       *obd = ofd_obd(m);
243                 /* For interoperability */
244                 struct cfg_interop_param   *ptr = NULL;
245                 struct lustre_cfg          *old_cfg = NULL;
246                 char                       *param = NULL;
247
248                 param = lustre_cfg_string(cfg, 1);
249                 if (param == NULL) {
250                         CERROR("param is empty\n");
251                         rc = -EINVAL;
252                         break;
253                 }
254
255                 ptr = class_find_old_param(param, ofd_interop_param);
256                 if (ptr != NULL) {
257                         if (ptr->new_param == NULL) {
258                                 rc = 0;
259                                 CWARN("For interoperability, skip this %s."
260                                       " It is obsolete.\n", ptr->old_param);
261                                 break;
262                         }
263
264                         CWARN("Found old param %s, changed it to %s.\n",
265                               ptr->old_param, ptr->new_param);
266
267                         old_cfg = cfg;
268                         cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
269                         if (IS_ERR(cfg)) {
270                                 rc = PTR_ERR(cfg);
271                                 break;
272                         }
273                 }
274
275                 if (match_symlink_param(param)) {
276                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
277                         break;
278                 }
279
280                 rc = class_process_proc_seq_param(PARAM_OST, obd->obd_vars, cfg,
281                                               d->ld_obd);
282                 if (rc > 0 || rc == -ENOSYS) {
283                         CDEBUG(D_CONFIG, "pass param %s down the stack.\n",
284                                param);
285                         /* we don't understand; pass it on */
286                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
287                 }
288                 break;
289         }
290         case LCFG_SPTLRPC_CONF: {
291                 rc = -ENOTSUPP;
292                 break;
293         }
294         default:
295                 /* others are passed further */
296                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
297                 break;
298         }
299         RETURN(rc);
300 }
301
302 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
303                            const struct lu_object_conf *conf)
304 {
305         struct ofd_device       *d = ofd_dev(o->lo_dev);
306         struct lu_device        *under;
307         struct lu_object        *below;
308         int                      rc = 0;
309
310         ENTRY;
311
312         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
313                PFID(lu_object_fid(o)));
314
315         under = &d->ofd_osd->dd_lu_dev;
316         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
317         if (below != NULL)
318                 lu_object_add(o, below);
319         else
320                 rc = -ENOMEM;
321
322         RETURN(rc);
323 }
324
325 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
326 {
327         struct ofd_object       *of = ofd_obj(o);
328         struct lu_object_header *h;
329
330         ENTRY;
331
332         h = o->lo_header;
333         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
334                PFID(lu_object_fid(o)));
335
336         lu_object_fini(o);
337         lu_object_header_fini(h);
338         OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
339         EXIT;
340 }
341
342 static int ofd_object_print(const struct lu_env *env, void *cookie,
343                             lu_printer_t p, const struct lu_object *o)
344 {
345         return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
346 }
347
348 struct lu_object_operations ofd_obj_ops = {
349         .loo_object_init        = ofd_object_init,
350         .loo_object_free        = ofd_object_free,
351         .loo_object_print       = ofd_object_print
352 };
353
354 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
355                                           const struct lu_object_header *hdr,
356                                           struct lu_device *d)
357 {
358         struct ofd_object *of;
359
360         ENTRY;
361
362         OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, GFP_NOFS);
363         if (of != NULL) {
364                 struct lu_object        *o;
365                 struct lu_object_header *h;
366
367                 o = &of->ofo_obj.do_lu;
368                 h = &of->ofo_header;
369                 lu_object_header_init(h);
370                 lu_object_init(o, h, d);
371                 lu_object_add_top(h, o);
372                 o->lo_ops = &ofd_obj_ops;
373                 RETURN(o);
374         } else {
375                 RETURN(NULL);
376         }
377 }
378
379 extern int ost_handle(struct ptlrpc_request *req);
380
381 static int ofd_lfsck_out_notify(const struct lu_env *env, void *data,
382                                 enum lfsck_events event)
383 {
384         struct ofd_device *ofd = data;
385         struct obd_device *obd = ofd_obd(ofd);
386
387         switch (event) {
388         case LE_LASTID_REBUILDING:
389                 CWARN("%s: Found crashed LAST_ID, deny creating new OST-object "
390                       "on the device until the LAST_ID rebuilt successfully.\n",
391                       obd->obd_name);
392                 down_write(&ofd->ofd_lastid_rwsem);
393                 ofd->ofd_lastid_rebuilding = 1;
394                 up_write(&ofd->ofd_lastid_rwsem);
395                 break;
396         case LE_LASTID_REBUILT: {
397                 down_write(&ofd->ofd_lastid_rwsem);
398                 ofd_seqs_free(env, ofd);
399                 ofd->ofd_lastid_rebuilding = 0;
400                 ofd->ofd_lastid_gen++;
401                 up_write(&ofd->ofd_lastid_rwsem);
402                 break;
403         }
404         default:
405                 CERROR("%s: unknown lfsck event: rc = %d\n",
406                        ofd_name(ofd), event);
407                 return -EINVAL;
408         }
409
410         return 0;
411 }
412
413 static int ofd_prepare(const struct lu_env *env, struct lu_device *pdev,
414                        struct lu_device *dev)
415 {
416         struct ofd_thread_info          *info;
417         struct ofd_device               *ofd = ofd_dev(dev);
418         struct obd_device               *obd = ofd_obd(ofd);
419         struct lu_device                *next = &ofd->ofd_osd->dd_lu_dev;
420         int                              rc;
421
422         ENTRY;
423
424         info = ofd_info_init(env, NULL);
425         if (info == NULL)
426                 RETURN(-EFAULT);
427
428         /* initialize lower device */
429         rc = next->ld_ops->ldo_prepare(env, dev, next);
430         if (rc != 0)
431                 RETURN(rc);
432
433         rc = lfsck_register(env, ofd->ofd_osd, ofd->ofd_osd, obd,
434                             ofd_lfsck_out_notify, ofd, false);
435         if (rc != 0) {
436                 CERROR("%s: failed to initialize lfsck: rc = %d\n",
437                        obd->obd_name, rc);
438                 RETURN(rc);
439         }
440
441         rc = lfsck_register_namespace(env, ofd->ofd_osd, ofd->ofd_namespace);
442         /* The LFSCK instance is registered just now, so it must be there when
443          * register the namespace to such instance. */
444         LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
445
446         target_recovery_init(&ofd->ofd_lut, tgt_request_handle);
447         LASSERT(obd->obd_no_conn);
448         spin_lock(&obd->obd_dev_lock);
449         obd->obd_no_conn = 0;
450         spin_unlock(&obd->obd_dev_lock);
451
452         if (obd->obd_recovering == 0)
453                 ofd_postrecov(env, ofd);
454
455         RETURN(rc);
456 }
457
458 static int ofd_recovery_complete(const struct lu_env *env,
459                                  struct lu_device *dev)
460 {
461         struct ofd_device       *ofd = ofd_dev(dev);
462         struct lu_device        *next = &ofd->ofd_osd->dd_lu_dev;
463         int                      rc = 0, max_precreate;
464
465         ENTRY;
466
467         /* Grant space for object precreation on the self export.
468          * This initial reserved space (i.e. 10MB for zfs and 280KB for ldiskfs)
469          * is enough to create 10k objects. More space is then acquired for
470          * precreation in ofd_grant_create().
471          */
472         max_precreate = OST_MAX_PRECREATE * ofd->ofd_dt_conf.ddp_inodespace / 2;
473         ofd_grant_connect(env, dev->ld_obd->obd_self_export, max_precreate,
474                           false);
475         rc = next->ld_ops->ldo_recovery_complete(env, next);
476         RETURN(rc);
477 }
478
479 static struct lu_device_operations ofd_lu_ops = {
480         .ldo_object_alloc       = ofd_object_alloc,
481         .ldo_process_config     = ofd_process_config,
482         .ldo_recovery_complete  = ofd_recovery_complete,
483         .ldo_prepare            = ofd_prepare,
484 };
485
486 LPROC_SEQ_FOPS(lprocfs_nid_stats_clear);
487
488 static int ofd_procfs_init(struct ofd_device *ofd)
489 {
490         struct obd_device               *obd = ofd_obd(ofd);
491         cfs_proc_dir_entry_t            *entry;
492         int                              rc = 0;
493
494         ENTRY;
495
496         /* lprocfs must be setup before the ofd so state can be safely added
497          * to /proc incrementally as the ofd is setup */
498         obd->obd_vars = lprocfs_ofd_obd_vars;
499         rc = lprocfs_seq_obd_setup(obd);
500         if (rc) {
501                 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
502                        obd->obd_name, rc);
503                 RETURN(rc);
504         }
505
506         rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_STATS_LAST);
507         if (rc) {
508                 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
509                        obd->obd_name, rc);
510                 GOTO(obd_cleanup, rc);
511         }
512
513         obd->obd_uses_nid_stats = 1;
514
515         entry = lprocfs_seq_register("exports", obd->obd_proc_entry, NULL,
516                                      NULL);
517         if (IS_ERR(entry)) {
518                 rc = PTR_ERR(entry);
519                 CERROR("%s: error %d setting up lprocfs for %s\n",
520                        obd->obd_name, rc, "exports");
521                 GOTO(obd_cleanup, rc);
522         }
523         obd->obd_proc_exports_entry = entry;
524
525         entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
526 #ifndef HAVE_ONLY_PROCFS_SEQ
527                                    NULL, NULL,
528 #endif
529                                    obd, &lprocfs_nid_stats_clear_fops);
530         if (IS_ERR(entry)) {
531                 rc = PTR_ERR(entry);
532                 CERROR("%s: add proc entry 'clear' failed: %d.\n",
533                        obd->obd_name, rc);
534                 GOTO(obd_cleanup, rc);
535         }
536
537         ofd_stats_counter_init(obd->obd_stats);
538
539         rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
540                                     ofd_stats_counter_init);
541         if (rc)
542                 GOTO(obd_cleanup, rc);
543         RETURN(0);
544 obd_cleanup:
545         lprocfs_obd_cleanup(obd);
546         lprocfs_free_obd_stats(obd);
547
548         return rc;
549 }
550
551 /**
552  * ofd_procfs_add_brw_stats_symlink - expose osd stats to ofd layer
553  *
554  * The osd interfaces to the backend file system exposes useful data
555  * such as brw_stats and read or write cache states. This same data
556  * needs to be exposed into the obdfilter (ofd) layer to maintain
557  * backwards compatibility. This function creates the symlinks in the
558  * proc layer to enable this.
559  */
560 static void ofd_procfs_add_brw_stats_symlink(struct ofd_device *ofd)
561 {
562         struct obd_device       *obd = ofd_obd(ofd);
563         struct obd_device       *osd_obd = ofd->ofd_osd_exp->exp_obd;
564
565         if (obd->obd_proc_entry == NULL)
566                 return;
567
568         lprocfs_add_symlink("brw_stats", obd->obd_proc_entry,
569                             "../../%s/%s/brw_stats",
570                             osd_obd->obd_type->typ_name, obd->obd_name);
571
572         lprocfs_add_symlink("read_cache_enable", obd->obd_proc_entry,
573                             "../../%s/%s/read_cache_enable",
574                             osd_obd->obd_type->typ_name, obd->obd_name);
575
576         lprocfs_add_symlink("readcache_max_filesize",
577                             obd->obd_proc_entry,
578                             "../../%s/%s/readcache_max_filesize",
579                             osd_obd->obd_type->typ_name, obd->obd_name);
580
581         lprocfs_add_symlink("writethrough_cache_enable",
582                             obd->obd_proc_entry,
583                             "../../%s/%s/writethrough_cache_enable",
584                             osd_obd->obd_type->typ_name, obd->obd_name);
585 }
586
587 static void ofd_procfs_fini(struct ofd_device *ofd)
588 {
589         struct obd_device *obd = ofd_obd(ofd);
590
591         lprocfs_free_per_client_stats(obd);
592         lprocfs_obd_cleanup(obd);
593         lprocfs_free_obd_stats(obd);
594         lprocfs_job_stats_fini(obd);
595 }
596
597 extern int ost_handle(struct ptlrpc_request *req);
598
599 int ofd_fid_fini(const struct lu_env *env, struct ofd_device *ofd)
600 {
601         return seq_site_fini(env, &ofd->ofd_seq_site);
602 }
603
604 int ofd_fid_init(const struct lu_env *env, struct ofd_device *ofd)
605 {
606         struct seq_server_site  *ss = &ofd->ofd_seq_site;
607         struct lu_device        *lu = &ofd->ofd_dt_dev.dd_lu_dev;
608         char                    *obd_name = ofd_name(ofd);
609         char                    *name = NULL;
610         int                     rc = 0;
611
612         ss = &ofd->ofd_seq_site;
613         lu->ld_site->ld_seq_site = ss;
614         ss->ss_lu = lu->ld_site;
615         ss->ss_node_id = ofd->ofd_lut.lut_lsd.lsd_osd_index;
616
617         OBD_ALLOC_PTR(ss->ss_server_seq);
618         if (ss->ss_server_seq == NULL)
619                 GOTO(out_free, rc = -ENOMEM);
620
621         OBD_ALLOC(name, strlen(obd_name) + 10);
622         if (!name) {
623                 OBD_FREE_PTR(ss->ss_server_seq);
624                 ss->ss_server_seq = NULL;
625                 GOTO(out_free, rc = -ENOMEM);
626         }
627
628         rc = seq_server_init(env, ss->ss_server_seq, ofd->ofd_osd, obd_name,
629                              LUSTRE_SEQ_SERVER, ss);
630         if (rc) {
631                 CERROR("%s : seq server init error %d\n", obd_name, rc);
632                 GOTO(out_free, rc);
633         }
634         ss->ss_server_seq->lss_space.lsr_index = ss->ss_node_id;
635
636         OBD_ALLOC_PTR(ss->ss_client_seq);
637         if (ss->ss_client_seq == NULL)
638                 GOTO(out_free, rc = -ENOMEM);
639
640         snprintf(name, strlen(obd_name) + 6, "%p-super", obd_name);
641         rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_DATA,
642                              name, NULL);
643         if (rc) {
644                 CERROR("%s : seq client init error %d\n", obd_name, rc);
645                 GOTO(out_free, rc);
646         }
647         OBD_FREE(name, strlen(obd_name) + 10);
648         name = NULL;
649
650         rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
651
652 out_free:
653         if (rc) {
654                 if (ss->ss_server_seq) {
655                         seq_server_fini(ss->ss_server_seq, env);
656                         OBD_FREE_PTR(ss->ss_server_seq);
657                         ss->ss_server_seq = NULL;
658                 }
659
660                 if (ss->ss_client_seq) {
661                         seq_client_fini(ss->ss_client_seq);
662                         OBD_FREE_PTR(ss->ss_client_seq);
663                         ss->ss_client_seq = NULL;
664                 }
665
666                 if (name) {
667                         OBD_FREE(name, strlen(obd_name) + 10);
668                         name = NULL;
669                 }
670         }
671
672         return rc;
673 }
674
675 int ofd_set_info_hdl(struct tgt_session_info *tsi)
676 {
677         struct ptlrpc_request   *req = tgt_ses_req(tsi);
678         struct ost_body         *body = NULL, *repbody;
679         void                    *key, *val = NULL;
680         int                      keylen, vallen, rc = 0;
681         bool                     is_grant_shrink;
682         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
683
684         ENTRY;
685
686         key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
687         if (key == NULL) {
688                 DEBUG_REQ(D_HA, req, "no set_info key");
689                 RETURN(err_serious(-EFAULT));
690         }
691         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
692                                       RCL_CLIENT);
693
694         val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
695         if (val == NULL) {
696                 DEBUG_REQ(D_HA, req, "no set_info val");
697                 RETURN(err_serious(-EFAULT));
698         }
699         vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
700                                       RCL_CLIENT);
701
702         is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
703         if (is_grant_shrink)
704                 /* In this case the value is actually an RMF_OST_BODY, so we
705                  * transmutate the type of this PTLRPC */
706                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
707
708         rc = req_capsule_server_pack(tsi->tsi_pill);
709         if (rc < 0)
710                 RETURN(rc);
711
712         if (is_grant_shrink) {
713                 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
714
715                 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
716                 *repbody = *body;
717
718                 /** handle grant shrink, similar to a read request */
719                 ofd_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
720                                        &repbody->oa);
721         } else if (KEY_IS(KEY_EVICT_BY_NID)) {
722                 if (vallen > 0)
723                         obd_export_evict_by_nid(tsi->tsi_exp->exp_obd, val);
724                 rc = 0;
725         } else if (KEY_IS(KEY_CAPA_KEY)) {
726                 rc = ofd_update_capa_key(ofd, val);
727         } else if (KEY_IS(KEY_SPTLRPC_CONF)) {
728                 rc = tgt_adapt_sptlrpc_conf(tsi->tsi_tgt, 0);
729         } else {
730                 CERROR("%s: Unsupported key %s\n",
731                        tgt_name(tsi->tsi_tgt), (char *)key);
732                 rc = -EOPNOTSUPP;
733         }
734         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SET_INFO,
735                          tsi->tsi_jobid, 1);
736
737         RETURN(rc);
738 }
739
740 int ofd_fiemap_get(const struct lu_env *env, struct ofd_device *ofd,
741                    struct lu_fid *fid, struct ll_user_fiemap *fiemap)
742 {
743         struct ofd_object       *fo;
744         int                      rc;
745
746         fo = ofd_object_find(env, ofd, fid);
747         if (IS_ERR(fo)) {
748                 CERROR("%s: error finding object "DFID"\n",
749                        ofd_name(ofd), PFID(fid));
750                 return PTR_ERR(fo);
751         }
752
753         ofd_read_lock(env, fo);
754         if (ofd_object_exists(fo))
755                 rc = dt_fiemap_get(env, ofd_object_child(fo), fiemap);
756         else
757                 rc = -ENOENT;
758         ofd_read_unlock(env, fo);
759         ofd_object_put(env, fo);
760         return rc;
761 }
762
763 struct locked_region {
764         cfs_list_t              list;
765         struct lustre_handle    lh;
766 };
767
768 static int lock_region(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
769                        unsigned long long begin, unsigned long long end,
770                        cfs_list_t *locked)
771 {
772         struct locked_region    *region = NULL;
773         __u64                    flags = 0;
774         int                      rc;
775
776         LASSERT(begin <= end);
777         OBD_ALLOC_PTR(region);
778         if (region == NULL)
779                 return -ENOMEM;
780
781         rc = tgt_extent_lock(ns, res_id, begin, end, &region->lh,
782                              LCK_PR, &flags);
783         if (rc != 0)
784                 return rc;
785
786         CDEBUG(D_OTHER, "ost lock [%llu,%llu], lh=%p\n", begin, end,
787                &region->lh);
788         cfs_list_add(&region->list, locked);
789
790         return 0;
791 }
792
793 static int lock_zero_regions(struct ldlm_namespace *ns,
794                              struct ldlm_res_id *res_id,
795                              struct ll_user_fiemap *fiemap,
796                              cfs_list_t *locked)
797 {
798         __u64 begin = fiemap->fm_start;
799         unsigned int i;
800         int rc = 0;
801         struct ll_fiemap_extent *fiemap_start = fiemap->fm_extents;
802
803         ENTRY;
804
805         CDEBUG(D_OTHER, "extents count %u\n", fiemap->fm_mapped_extents);
806         for (i = 0; i < fiemap->fm_mapped_extents; i++) {
807                 if (fiemap_start[i].fe_logical > begin) {
808                         CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
809                                begin, fiemap_start[i].fe_logical);
810                         rc = lock_region(ns, res_id, begin,
811                                          fiemap_start[i].fe_logical, locked);
812                         if (rc)
813                                 RETURN(rc);
814                 }
815
816                 begin = fiemap_start[i].fe_logical + fiemap_start[i].fe_length;
817         }
818
819         if (begin < (fiemap->fm_start + fiemap->fm_length)) {
820                 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
821                        begin, fiemap->fm_start + fiemap->fm_length);
822                 rc = lock_region(ns, res_id, begin,
823                                  fiemap->fm_start + fiemap->fm_length, locked);
824         }
825
826         RETURN(rc);
827 }
828
829 static void unlock_zero_regions(struct ldlm_namespace *ns, cfs_list_t *locked)
830 {
831         struct locked_region *entry, *temp;
832
833         cfs_list_for_each_entry_safe(entry, temp, locked, list) {
834                 CDEBUG(D_OTHER, "ost unlock lh=%p\n", &entry->lh);
835                 tgt_extent_unlock(&entry->lh, LCK_PR);
836                 cfs_list_del(&entry->list);
837                 OBD_FREE_PTR(entry);
838         }
839 }
840
841 int ofd_get_info_hdl(struct tgt_session_info *tsi)
842 {
843         struct obd_export               *exp = tsi->tsi_exp;
844         struct ofd_device               *ofd = ofd_exp(exp);
845         struct ofd_thread_info          *fti = tsi2ofd_info(tsi);
846         void                            *key;
847         int                              keylen;
848         int                              replylen, rc = 0;
849
850         ENTRY;
851
852         /* this common part for get_info rpc */
853         key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
854         if (key == NULL) {
855                 DEBUG_REQ(D_HA, tgt_ses_req(tsi), "no get_info key");
856                 RETURN(err_serious(-EPROTO));
857         }
858         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
859                                       RCL_CLIENT);
860
861         if (KEY_IS(KEY_LAST_ID)) {
862                 obd_id          *last_id;
863                 struct ofd_seq  *oseq;
864
865                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_ID);
866                 rc = req_capsule_server_pack(tsi->tsi_pill);
867                 if (rc)
868                         RETURN(err_serious(rc));
869
870                 last_id = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_ID);
871
872                 oseq = ofd_seq_load(tsi->tsi_env, ofd,
873                                     (obd_seq)exp->exp_filter_data.fed_group);
874                 if (IS_ERR(oseq))
875                         rc = -EFAULT;
876                 else
877                         *last_id = ofd_seq_last_oid(oseq);
878                 ofd_seq_put(tsi->tsi_env, oseq);
879         } else if (KEY_IS(KEY_FIEMAP)) {
880                 struct ll_fiemap_info_key       *fm_key;
881                 struct ll_user_fiemap           *fiemap;
882                 struct lu_fid                   *fid;
883
884                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_FIEMAP);
885
886                 fm_key = req_capsule_client_get(tsi->tsi_pill, &RMF_FIEMAP_KEY);
887                 rc = tgt_validate_obdo(tsi, &fm_key->oa);
888                 if (rc)
889                         RETURN(err_serious(rc));
890
891                 fid = &fm_key->oa.o_oi.oi_fid;
892
893                 CDEBUG(D_INODE, "get FIEMAP of object "DFID"\n", PFID(fid));
894
895                 replylen = fiemap_count_to_size(fm_key->fiemap.fm_extent_count);
896                 req_capsule_set_size(tsi->tsi_pill, &RMF_FIEMAP_VAL,
897                                      RCL_SERVER, replylen);
898
899                 rc = req_capsule_server_pack(tsi->tsi_pill);
900                 if (rc)
901                         RETURN(err_serious(rc));
902
903                 fiemap = req_capsule_server_get(tsi->tsi_pill, &RMF_FIEMAP_VAL);
904                 if (fiemap == NULL)
905                         RETURN(-ENOMEM);
906
907                 *fiemap = fm_key->fiemap;
908                 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid, fiemap);
909
910                 /* LU-3219: Lock the sparse areas to make sure dirty
911                  * flushed back from client, then call fiemap again. */
912                 if (fm_key->oa.o_valid & OBD_MD_FLFLAGS &&
913                     fm_key->oa.o_flags & OBD_FL_SRVLOCK) {
914                         cfs_list_t locked = CFS_LIST_HEAD_INIT(locked);
915
916                         ost_fid_build_resid(fid, &fti->fti_resid);
917                         rc = lock_zero_regions(ofd->ofd_namespace,
918                                                &fti->fti_resid, fiemap,
919                                                &locked);
920                         if (rc == 0 && !cfs_list_empty(&locked)) {
921                                 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid,
922                                                     fiemap);
923                                 unlock_zero_regions(ofd->ofd_namespace,
924                                                     &locked);
925                         }
926                 }
927         } else if (KEY_IS(KEY_LAST_FID)) {
928                 struct ofd_device       *ofd = ofd_exp(exp);
929                 struct ofd_seq          *oseq;
930                 struct lu_fid           *fid;
931                 int                      rc;
932
933                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_FID);
934                 rc = req_capsule_server_pack(tsi->tsi_pill);
935                 if (rc)
936                         RETURN(err_serious(rc));
937
938                 fid = req_capsule_client_get(tsi->tsi_pill, &RMF_FID);
939                 if (fid == NULL)
940                         RETURN(err_serious(-EPROTO));
941
942                 fid_le_to_cpu(&fti->fti_ostid.oi_fid, fid);
943
944                 fid = req_capsule_server_get(tsi->tsi_pill, &RMF_FID);
945                 if (fid == NULL)
946                         RETURN(-ENOMEM);
947
948                 oseq = ofd_seq_load(tsi->tsi_env, ofd,
949                                     ostid_seq(&fti->fti_ostid));
950                 if (IS_ERR(oseq))
951                         RETURN(PTR_ERR(oseq));
952
953                 rc = ostid_to_fid(fid, &oseq->os_oi,
954                                   ofd->ofd_lut.lut_lsd.lsd_osd_index);
955                 if (rc != 0)
956                         GOTO(out_put, rc);
957
958                 CDEBUG(D_HA, "%s: LAST FID is "DFID"\n", ofd_name(ofd),
959                        PFID(fid));
960 out_put:
961                 ofd_seq_put(tsi->tsi_env, oseq);
962         } else {
963                 CERROR("%s: not supported key %s\n", tgt_name(tsi->tsi_tgt),
964                        (char *)key);
965                 rc = -EOPNOTSUPP;
966         }
967         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GET_INFO,
968                          tsi->tsi_jobid, 1);
969
970         RETURN(rc);
971 }
972
973 static int ofd_getattr_hdl(struct tgt_session_info *tsi)
974 {
975         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
976         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
977         struct ost_body         *repbody;
978         struct lustre_handle     lh = { 0 };
979         struct ofd_object       *fo;
980         __u64                    flags = 0;
981         ldlm_mode_t              lock_mode = LCK_PR;
982         bool                     srvlock;
983         int                      rc;
984         ENTRY;
985
986         LASSERT(tsi->tsi_ost_body != NULL);
987
988         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
989         if (repbody == NULL)
990                 RETURN(-ENOMEM);
991
992         repbody->oa.o_oi = tsi->tsi_ost_body->oa.o_oi;
993         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
994
995         srvlock = tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
996                   tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK;
997
998         if (srvlock) {
999                 if (unlikely(tsi->tsi_ost_body->oa.o_flags & OBD_FL_FLUSH))
1000                         lock_mode = LCK_PW;
1001
1002                 rc = tgt_extent_lock(tsi->tsi_tgt->lut_obd->obd_namespace,
1003                                      &tsi->tsi_resid, 0, OBD_OBJECT_EOF, &lh,
1004                                      lock_mode, &flags);
1005                 if (rc != 0)
1006                         RETURN(rc);
1007         }
1008
1009         fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1010         if (IS_ERR(fo))
1011                 GOTO(out, rc = PTR_ERR(fo));
1012
1013         rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1014         if (rc == 0) {
1015                 __u64    curr_version;
1016
1017                 obdo_from_la(&repbody->oa, &fti->fti_attr,
1018                              OFD_VALID_FLAGS | LA_UID | LA_GID);
1019                 tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1020
1021                 /* Store object version in reply */
1022                 curr_version = dt_version_get(tsi->tsi_env,
1023                                               ofd_object_child(fo));
1024                 if ((__s64)curr_version != -EOPNOTSUPP) {
1025                         repbody->oa.o_valid |= OBD_MD_FLDATAVERSION;
1026                         repbody->oa.o_data_version = curr_version;
1027                 }
1028         }
1029
1030         ofd_object_put(tsi->tsi_env, fo);
1031 out:
1032         if (srvlock)
1033                 tgt_extent_unlock(&lh, lock_mode);
1034
1035         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GETATTR,
1036                          tsi->tsi_jobid, 1);
1037
1038         repbody->oa.o_valid |= OBD_MD_FLFLAGS;
1039         repbody->oa.o_flags = OBD_FL_FLUSH;
1040
1041         RETURN(rc);
1042 }
1043
1044 static int ofd_setattr_hdl(struct tgt_session_info *tsi)
1045 {
1046         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1047         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1048         struct ost_body         *body = tsi->tsi_ost_body;
1049         struct ost_body         *repbody;
1050         struct ldlm_resource    *res;
1051         struct ofd_object       *fo;
1052         struct filter_fid       *ff = NULL;
1053         int                      rc = 0;
1054
1055         ENTRY;
1056
1057         LASSERT(body != NULL);
1058
1059         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1060         if (repbody == NULL)
1061                 RETURN(-ENOMEM);
1062
1063         repbody->oa.o_oi = body->oa.o_oi;
1064         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1065
1066         /* This would be very bad - accidentally truncating a file when
1067          * changing the time or similar - bug 12203. */
1068         if (body->oa.o_valid & OBD_MD_FLSIZE &&
1069             body->oa.o_size != OBD_OBJECT_EOF) {
1070                 static char mdsinum[48];
1071
1072                 if (body->oa.o_valid & OBD_MD_FLFID)
1073                         snprintf(mdsinum, sizeof(mdsinum) - 1,
1074                                  "of parent "DFID, body->oa.o_parent_seq,
1075                                  body->oa.o_parent_oid, 0);
1076                 else
1077                         mdsinum[0] = '\0';
1078
1079                 CERROR("%s: setattr from %s is trying to truncate object "DFID
1080                        " %s\n", ofd_name(ofd), obd_export_nid2str(tsi->tsi_exp),
1081                        PFID(&tsi->tsi_fid), mdsinum);
1082                 RETURN(-EPERM);
1083         }
1084
1085         fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1086         if (IS_ERR(fo))
1087                 GOTO(out, rc = PTR_ERR(fo));
1088
1089         la_from_obdo(&fti->fti_attr, &body->oa, body->oa.o_valid);
1090         fti->fti_attr.la_valid &= ~LA_TYPE;
1091
1092         if (body->oa.o_valid & OBD_MD_FLFID) {
1093                 ff = &fti->fti_mds_fid;
1094                 ofd_prepare_fidea(ff, &body->oa);
1095         }
1096
1097         /* setting objects attributes (including owner/group) */
1098         rc = ofd_attr_set(tsi->tsi_env, fo, &fti->fti_attr, ff);
1099         if (rc != 0)
1100                 GOTO(out_put, rc);
1101
1102         obdo_from_la(&repbody->oa, &fti->fti_attr,
1103                      OFD_VALID_FLAGS | LA_UID | LA_GID);
1104         tgt_drop_id(tsi->tsi_exp, &repbody->oa);
1105
1106         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SETATTR,
1107                          tsi->tsi_jobid, 1);
1108         EXIT;
1109 out_put:
1110         ofd_object_put(tsi->tsi_env, fo);
1111 out:
1112         if (rc == 0) {
1113                 /* we do not call this before to avoid lu_object_find() in
1114                  *  ->lvbo_update() holding another reference on the object.
1115                  * otherwise concurrent destroy can make the object unavailable
1116                  * for 2nd lu_object_find() waiting for the first reference
1117                  * to go... deadlock! */
1118                 res = ldlm_resource_get(ofd->ofd_namespace, NULL,
1119                                         &tsi->tsi_resid, LDLM_EXTENT, 0);
1120                 if (!IS_ERR(res)) {
1121                         ldlm_res_lvbo_update(res, NULL, 0);
1122                         ldlm_resource_putref(res);
1123                 }
1124         }
1125         return rc;
1126 }
1127
1128 static int ofd_orphans_destroy(const struct lu_env *env,
1129                                struct obd_export *exp,
1130                                struct ofd_device *ofd, struct obdo *oa)
1131 {
1132         struct ofd_thread_info  *info   = ofd_info(env);
1133         struct lu_fid           *fid    = &info->fti_fid;
1134         struct ost_id           *oi     = &oa->o_oi;
1135         struct ofd_seq          *oseq;
1136         obd_seq                  seq    = ostid_seq(oi);
1137         obd_id                   end_id = ostid_id(oi);
1138         obd_id                   last;
1139         obd_id                   oid;
1140         int                      skip_orphan;
1141         int                      rc     = 0;
1142
1143         ENTRY;
1144
1145         oseq = ofd_seq_get(ofd, seq);
1146         if (oseq == NULL) {
1147                 CERROR("%s: Can not find seq for "DOSTID"\n",
1148                        ofd_name(ofd), POSTID(oi));
1149                 RETURN(-EINVAL);
1150         }
1151
1152         *fid = oi->oi_fid;
1153         last = ofd_seq_last_oid(oseq);
1154         oid = last;
1155
1156         LASSERT(exp != NULL);
1157         skip_orphan = !!(exp_connect_flags(exp) & OBD_CONNECT_SKIP_ORPHAN);
1158
1159         if (OBD_FAIL_CHECK(OBD_FAIL_OST_NODESTROY))
1160                 goto done;
1161
1162         LCONSOLE(D_INFO, "%s: deleting orphan objects from "DOSTID
1163                  " to "DOSTID"\n", ofd_name(ofd), seq, end_id + 1, seq, last);
1164
1165         while (oid > end_id) {
1166                 rc = fid_set_id(fid, oid);
1167                 if (unlikely(rc != 0))
1168                         GOTO(out_put, rc);
1169
1170                 rc = ofd_destroy_by_fid(env, ofd, fid, 1);
1171                 if (rc != 0 && rc != -ENOENT && rc != -ESTALE &&
1172                     likely(rc != -EREMCHG && rc != -EINPROGRESS))
1173                         /* this is pretty fatal... */
1174                         CEMERG("%s: error destroying precreated id "
1175                                DFID": rc = %d\n",
1176                                ofd_name(ofd), PFID(fid), rc);
1177
1178                 oid--;
1179                 if (!skip_orphan) {
1180                         ofd_seq_last_oid_set(oseq, oid);
1181                         /* update last_id on disk periodically so that if we
1182                          * restart * we don't need to re-scan all of the just
1183                          * deleted objects. */
1184                         if ((oid & 511) == 0)
1185                                 ofd_seq_last_oid_write(env, ofd, oseq);
1186                 }
1187         }
1188
1189         CDEBUG(D_HA, "%s: after destroy: set last_id to "DOSTID"\n",
1190                ofd_name(ofd), seq, oid);
1191
1192 done:
1193         if (!skip_orphan) {
1194                 ofd_seq_last_oid_set(oseq, oid);
1195                 rc = ofd_seq_last_oid_write(env, ofd, oseq);
1196         } else {
1197                 /* don't reuse orphan object, return last used objid */
1198                 ostid_set_id(oi, last);
1199                 rc = 0;
1200         }
1201
1202         GOTO(out_put, rc);
1203
1204 out_put:
1205         ofd_seq_put(env, oseq);
1206         return rc;
1207 }
1208
1209 static int ofd_create_hdl(struct tgt_session_info *tsi)
1210 {
1211         struct ost_body         *repbody;
1212         const struct obdo       *oa = &tsi->tsi_ost_body->oa;
1213         struct obdo             *rep_oa;
1214         struct obd_export       *exp = tsi->tsi_exp;
1215         struct ofd_device       *ofd = ofd_exp(exp);
1216         obd_seq                  seq = ostid_seq(&oa->o_oi);
1217         obd_id                   oid = ostid_id(&oa->o_oi);
1218         struct ofd_seq          *oseq;
1219         int                      rc = 0, diff;
1220         int                      sync_trans = 0;
1221
1222         ENTRY;
1223
1224         if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1225                 RETURN(-EROFS);
1226
1227         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1228         if (repbody == NULL)
1229                 RETURN(-ENOMEM);
1230
1231         down_read(&ofd->ofd_lastid_rwsem);
1232         /* Currently, for safe, we do not distinguish which LAST_ID is broken,
1233          * we may do that in the future.
1234          * Return -ENOSPC until the LAST_ID rebuilt. */
1235         if (unlikely(ofd->ofd_lastid_rebuilding))
1236                 GOTO(out_sem, rc = -ENOSPC);
1237
1238         rep_oa = &repbody->oa;
1239         rep_oa->o_oi = oa->o_oi;
1240
1241         LASSERT(seq >= FID_SEQ_OST_MDT0);
1242         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1243
1244         CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1245
1246         oseq = ofd_seq_load(tsi->tsi_env, ofd, seq);
1247         if (IS_ERR(oseq)) {
1248                 CERROR("%s: Can't find FID Sequence "LPX64": rc = %ld\n",
1249                        ofd_name(ofd), seq, PTR_ERR(oseq));
1250                 GOTO(out_sem, rc = -EINVAL);
1251         }
1252
1253         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1254             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1255                 if (!ofd_obd(ofd)->obd_recovering ||
1256                     oid > ofd_seq_last_oid(oseq)) {
1257                         CERROR("%s: recreate objid "DOSTID" > last id "LPU64
1258                                "\n", ofd_name(ofd), POSTID(&oa->o_oi),
1259                                ofd_seq_last_oid(oseq));
1260                         GOTO(out_nolock, rc = -EINVAL);
1261                 }
1262                 /* Do nothing here, we re-create objects during recovery
1263                  * upon write replay, see ofd_preprw_write() */
1264                 GOTO(out_nolock, rc = 0);
1265         }
1266         /* former ofd_handle_precreate */
1267         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1268             (oa->o_flags & OBD_FL_DELORPHAN)) {
1269                 exp->exp_filter_data.fed_lastid_gen = ofd->ofd_lastid_gen;
1270
1271                 /* destroy orphans */
1272                 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1273                     exp->exp_conn_cnt) {
1274                         CERROR("%s: dropping old orphan cleanup request\n",
1275                                ofd_name(ofd));
1276                         GOTO(out_nolock, rc = 0);
1277                 }
1278                 /* This causes inflight precreates to abort and drop lock */
1279                 oseq->os_destroys_in_progress = 1;
1280                 mutex_lock(&oseq->os_create_lock);
1281                 if (!oseq->os_destroys_in_progress) {
1282                         CERROR("%s:["LPU64"] destroys_in_progress already"
1283                                " cleared\n", ofd_name(ofd), seq);
1284                         ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1285                         GOTO(out, rc = 0);
1286                 }
1287                 diff = oid - ofd_seq_last_oid(oseq);
1288                 CDEBUG(D_HA, "ofd_last_id() = "LPU64" -> diff = %d\n",
1289                         ofd_seq_last_oid(oseq), diff);
1290                 if (-diff > OST_MAX_PRECREATE) {
1291                         /* FIXME: should reset precreate_next_id on MDS */
1292                         rc = 0;
1293                 } else if (diff < 0) {
1294                         rc = ofd_orphans_destroy(tsi->tsi_env, exp,
1295                                                  ofd, rep_oa);
1296                         oseq->os_destroys_in_progress = 0;
1297                 } else {
1298                         /* XXX: Used by MDS for the first time! */
1299                         oseq->os_destroys_in_progress = 0;
1300                 }
1301         } else {
1302                 if (unlikely(exp->exp_filter_data.fed_lastid_gen !=
1303                              ofd->ofd_lastid_gen)) {
1304                         ofd_obd_disconnect(exp);
1305                         GOTO(out_nolock, rc = -ENOTCONN);
1306                 }
1307
1308                 mutex_lock(&oseq->os_create_lock);
1309                 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1310                     exp->exp_conn_cnt) {
1311                         CERROR("%s: dropping old precreate request\n",
1312                                ofd_name(ofd));
1313                         GOTO(out, rc = 0);
1314                 }
1315                 /* only precreate if seq is 0, IDIF or normal and also o_id
1316                  * must be specfied */
1317                 if ((!fid_seq_is_mdt(seq) && !fid_seq_is_norm(seq) &&
1318                      !fid_seq_is_idif(seq)) || oid == 0) {
1319                         diff = 1; /* shouldn't we create this right now? */
1320                 } else {
1321                         diff = oid - ofd_seq_last_oid(oseq);
1322                         /* Do sync create if the seq is about to used up */
1323                         if (fid_seq_is_idif(seq) || fid_seq_is_mdt0(seq)) {
1324                                 if (unlikely(oid >= IDIF_MAX_OID - 1))
1325                                         sync_trans = 1;
1326                         } else if (fid_seq_is_norm(seq)) {
1327                                 if (unlikely(oid >=
1328                                              LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
1329                                         sync_trans = 1;
1330                         } else {
1331                                 CERROR("%s : invalid o_seq "DOSTID"\n",
1332                                        ofd_name(ofd), POSTID(&oa->o_oi));
1333                                 GOTO(out, rc = -EINVAL);
1334                         }
1335                 }
1336         }
1337         if (diff > 0) {
1338                 cfs_time_t       enough_time = cfs_time_shift(DISK_TIMEOUT);
1339                 obd_id           next_id;
1340                 int              created = 0;
1341                 int              count;
1342
1343                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1344                     !(oa->o_flags & OBD_FL_DELORPHAN)) {
1345                         /* don't enforce grant during orphan recovery */
1346                         rc = ofd_grant_create(tsi->tsi_env,
1347                                               ofd_obd(ofd)->obd_self_export,
1348                                               &diff);
1349                         if (rc) {
1350                                 CDEBUG(D_HA, "%s: failed to acquire grant "
1351                                        "space for precreate (%d): rc = %d\n",
1352                                        ofd_name(ofd), diff, rc);
1353                                 diff = 0;
1354                         }
1355                 }
1356
1357                 /* This can happen if a new OST is formatted and installed
1358                  * in place of an old one at the same index.  Instead of
1359                  * precreating potentially millions of deleted old objects
1360                  * (possibly filling the OST), only precreate the last batch.
1361                  * LFSCK will eventually clean up any orphans. LU-14 */
1362                 if (diff > 5 * OST_MAX_PRECREATE) {
1363                         diff = OST_MAX_PRECREATE / 2;
1364                         LCONSOLE_WARN("%s: precreate FID "DOSTID" is over %u "
1365                                       "larger than the LAST_ID "DOSTID", only "
1366                                       "precreating the last %u objects.\n",
1367                                       ofd_name(ofd), POSTID(&oa->o_oi),
1368                                       5 * OST_MAX_PRECREATE,
1369                                       POSTID(&oseq->os_oi), diff);
1370                         ofd_seq_last_oid_set(oseq, ostid_id(&oa->o_oi) - diff);
1371                 }
1372
1373                 while (diff > 0) {
1374                         next_id = ofd_seq_last_oid(oseq) + 1;
1375                         count = ofd_precreate_batch(ofd, diff);
1376
1377                         CDEBUG(D_HA, "%s: reserve %d objects in group "LPX64
1378                                " at "LPU64"\n", ofd_name(ofd),
1379                                count, seq, next_id);
1380
1381                         if (cfs_time_after(jiffies, enough_time)) {
1382                                 CDEBUG(D_HA, "%s: Slow creates, %d/%d objects"
1383                                       " created at a rate of %d/s\n",
1384                                       ofd_name(ofd), created, diff + created,
1385                                       created / DISK_TIMEOUT);
1386                                 break;
1387                         }
1388
1389                         rc = ofd_precreate_objects(tsi->tsi_env, ofd, next_id,
1390                                                    oseq, count, sync_trans);
1391                         if (rc > 0) {
1392                                 created += rc;
1393                                 diff -= rc;
1394                         } else if (rc < 0) {
1395                                 break;
1396                         }
1397                 }
1398                 if (created > 0)
1399                         /* some objects got created, we can return
1400                          * them, even if last creation failed */
1401                         rc = 0;
1402                 else
1403                         CERROR("%s: unable to precreate: rc = %d\n",
1404                                ofd_name(ofd), rc);
1405
1406                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1407                     !(oa->o_flags & OBD_FL_DELORPHAN))
1408                         ofd_grant_commit(tsi->tsi_env,
1409                                          ofd_obd(ofd)->obd_self_export, rc);
1410
1411                 ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1412         }
1413         EXIT;
1414         ofd_counter_incr(exp, LPROC_OFD_STATS_CREATE,
1415                          tsi->tsi_jobid, 1);
1416 out:
1417         mutex_unlock(&oseq->os_create_lock);
1418 out_nolock:
1419         if (rc == 0) {
1420 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
1421                 struct ofd_thread_info  *info = ofd_info(tsi->tsi_env);
1422                 struct lu_fid           *fid = &info->fti_fid;
1423
1424                 /* For compatible purpose, it needs to convert back to
1425                  * OST ID before put it on wire. */
1426                 *fid = rep_oa->o_oi.oi_fid;
1427                 fid_to_ostid(fid, &rep_oa->o_oi);
1428 #endif
1429                 rep_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1430         }
1431         ofd_seq_put(tsi->tsi_env, oseq);
1432
1433 out_sem:
1434         up_read(&ofd->ofd_lastid_rwsem);
1435         return rc;
1436 }
1437
1438 static int ofd_destroy_hdl(struct tgt_session_info *tsi)
1439 {
1440         const struct ost_body   *body = tsi->tsi_ost_body;
1441         struct ost_body         *repbody;
1442         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1443         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1444         struct lu_fid           *fid = &fti->fti_fid;
1445         obd_id                   oid;
1446         obd_count                count;
1447         int                      rc = 0;
1448
1449         ENTRY;
1450
1451         if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1452                 RETURN(-EROFS);
1453
1454         /* This is old case for clients before Lustre 2.4 */
1455         /* If there's a DLM request, cancel the locks mentioned in it */
1456         if (req_capsule_field_present(tsi->tsi_pill, &RMF_DLM_REQ,
1457                                       RCL_CLIENT)) {
1458                 struct ldlm_request *dlm;
1459
1460                 dlm = req_capsule_client_get(tsi->tsi_pill, &RMF_DLM_REQ);
1461                 if (dlm == NULL)
1462                         RETURN(-EFAULT);
1463                 ldlm_request_cancel(tgt_ses_req(tsi), dlm, 0);
1464         }
1465
1466         *fid = body->oa.o_oi.oi_fid;
1467         oid = ostid_id(&body->oa.o_oi);
1468         LASSERT(oid != 0);
1469
1470         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1471
1472         /* check that o_misc makes sense */
1473         if (body->oa.o_valid & OBD_MD_FLOBJCOUNT)
1474                 count = body->oa.o_misc;
1475         else
1476                 count = 1; /* default case - single destroy */
1477
1478         CDEBUG(D_HA, "%s: Destroy object "DOSTID" count %d\n", ofd_name(ofd),
1479                POSTID(&body->oa.o_oi), count);
1480
1481         while (count > 0) {
1482                 int lrc;
1483
1484                 lrc = ofd_destroy_by_fid(tsi->tsi_env, ofd, fid, 0);
1485                 if (lrc == -ENOENT) {
1486                         CDEBUG(D_INODE,
1487                                "%s: destroying non-existent object "DFID"\n",
1488                                ofd_name(ofd), PFID(fid));
1489                         /* rewrite rc with -ENOENT only if it is 0 */
1490                         if (rc == 0)
1491                                 rc = lrc;
1492                 } else if (lrc != 0) {
1493                         CERROR("%s: error destroying object "DFID": %d\n",
1494                                ofd_name(ofd), PFID(fid), lrc);
1495                         rc = lrc;
1496                 }
1497
1498                 count--;
1499                 oid++;
1500                 lrc = fid_set_id(fid, oid);
1501                 if (unlikely(lrc != 0 && count > 0))
1502                         GOTO(out, rc = lrc);
1503         }
1504
1505         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_DESTROY,
1506                          tsi->tsi_jobid, 1);
1507
1508         GOTO(out, rc);
1509
1510 out:
1511         fid_to_ostid(fid, &repbody->oa.o_oi);
1512         return rc;
1513 }
1514
1515 static int ofd_statfs_hdl(struct tgt_session_info *tsi)
1516 {
1517         struct obd_statfs       *osfs;
1518         int                      rc;
1519
1520         ENTRY;
1521
1522         osfs = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_STATFS);
1523
1524         rc = ofd_statfs(tsi->tsi_env, tsi->tsi_exp, osfs,
1525                         cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 0);
1526         if (rc != 0)
1527                 CERROR("%s: statfs failed: rc = %d\n",
1528                        tgt_name(tsi->tsi_tgt), rc);
1529
1530         if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_EINPROGRESS))
1531                 rc = -EINPROGRESS;
1532
1533         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_STATFS,
1534                          tsi->tsi_jobid, 1);
1535
1536         RETURN(rc);
1537 }
1538
1539 static int ofd_sync_hdl(struct tgt_session_info *tsi)
1540 {
1541         struct ost_body         *body = tsi->tsi_ost_body;
1542         struct ost_body         *repbody;
1543         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1544         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1545         struct ofd_object       *fo = NULL;
1546         int                      rc = 0;
1547
1548         ENTRY;
1549
1550         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1551
1552         /* if no objid is specified, it means "sync whole filesystem" */
1553         if (!fid_is_zero(&tsi->tsi_fid)) {
1554                 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1555                 if (IS_ERR(fo))
1556                         RETURN(PTR_ERR(fo));
1557         }
1558
1559         rc = tgt_sync(tsi->tsi_env, tsi->tsi_tgt,
1560                       fo != NULL ? ofd_object_child(fo) : NULL,
1561                       repbody->oa.o_size, repbody->oa.o_blocks);
1562         if (rc)
1563                 GOTO(put, rc);
1564
1565         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SYNC,
1566                          tsi->tsi_jobid, 1);
1567         if (fo == NULL)
1568                 RETURN(0);
1569
1570         repbody->oa.o_oi = body->oa.o_oi;
1571         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1572
1573         rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1574         if (rc == 0)
1575                 obdo_from_la(&repbody->oa, &fti->fti_attr,
1576                              OFD_VALID_FLAGS);
1577         else
1578                 /* don't return rc from getattr */
1579                 rc = 0;
1580         EXIT;
1581 put:
1582         if (fo != NULL)
1583                 ofd_object_put(tsi->tsi_env, fo);
1584         return rc;
1585 }
1586
1587 static int ofd_punch_hdl(struct tgt_session_info *tsi)
1588 {
1589         const struct obdo       *oa = &tsi->tsi_ost_body->oa;
1590         struct ost_body         *repbody;
1591         struct ofd_thread_info  *info = tsi2ofd_info(tsi);
1592         struct ldlm_namespace   *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
1593         struct ldlm_resource    *res;
1594         struct ofd_object       *fo;
1595         struct filter_fid       *ff = NULL;
1596         __u64                    flags = 0;
1597         struct lustre_handle     lh = { 0, };
1598         int                      rc;
1599         __u64                    start, end;
1600         bool                     srvlock;
1601
1602         ENTRY;
1603
1604         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
1605         CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
1606
1607         if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
1608             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
1609                 RETURN(err_serious(-EPROTO));
1610
1611         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1612         if (repbody == NULL)
1613                 RETURN(err_serious(-ENOMEM));
1614
1615         /* punch start,end are passed in o_size,o_blocks throught wire */
1616         start = oa->o_size;
1617         end = oa->o_blocks;
1618
1619         if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
1620                 RETURN(-EPROTO);
1621
1622         /* standard truncate optimization: if file body is completely
1623          * destroyed, don't send data back to the server. */
1624         if (start == 0)
1625                 flags |= LDLM_FL_AST_DISCARD_DATA;
1626
1627         repbody->oa.o_oi = oa->o_oi;
1628         repbody->oa.o_valid = OBD_MD_FLID;
1629
1630         srvlock = oa->o_valid & OBD_MD_FLFLAGS &&
1631                   oa->o_flags & OBD_FL_SRVLOCK;
1632
1633         if (srvlock) {
1634                 rc = tgt_extent_lock(ns, &tsi->tsi_resid, start, end, &lh,
1635                                      LCK_PW, &flags);
1636                 if (rc != 0)
1637                         RETURN(rc);
1638         }
1639
1640         CDEBUG(D_INODE, "calling punch for object "DFID", valid = "LPX64
1641                ", start = "LPD64", end = "LPD64"\n", PFID(&tsi->tsi_fid),
1642                oa->o_valid, start, end);
1643
1644         fo = ofd_object_find_exists(tsi->tsi_env, ofd_exp(tsi->tsi_exp),
1645                                     &tsi->tsi_fid);
1646         if (IS_ERR(fo))
1647                 GOTO(out, rc = PTR_ERR(fo));
1648
1649         la_from_obdo(&info->fti_attr, oa,
1650                      OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
1651         info->fti_attr.la_size = start;
1652         info->fti_attr.la_valid |= LA_SIZE;
1653
1654         if (oa->o_valid & OBD_MD_FLFID) {
1655                 ff = &info->fti_mds_fid;
1656                 ofd_prepare_fidea(ff, oa);
1657         }
1658
1659         rc = ofd_object_punch(tsi->tsi_env, fo, start, end, &info->fti_attr,
1660                               ff, (struct obdo *)oa);
1661         if (rc)
1662                 GOTO(out_put, rc);
1663
1664         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_PUNCH,
1665                          tsi->tsi_jobid, 1);
1666         EXIT;
1667 out_put:
1668         ofd_object_put(tsi->tsi_env, fo);
1669 out:
1670         if (srvlock)
1671                 tgt_extent_unlock(&lh, LCK_PW);
1672         if (rc == 0) {
1673                 /* we do not call this before to avoid lu_object_find() in
1674                  *  ->lvbo_update() holding another reference on the object.
1675                  * otherwise concurrent destroy can make the object unavailable
1676                  * for 2nd lu_object_find() waiting for the first reference
1677                  * to go... deadlock! */
1678                 res = ldlm_resource_get(ns, NULL, &tsi->tsi_resid,
1679                                         LDLM_EXTENT, 0);
1680                 if (!IS_ERR(res)) {
1681                         ldlm_res_lvbo_update(res, NULL, 0);
1682                         ldlm_resource_putref(res);
1683                 }
1684         }
1685         return rc;
1686 }
1687
1688 static int ofd_quotactl(struct tgt_session_info *tsi)
1689 {
1690         struct obd_quotactl     *oqctl, *repoqc;
1691         int                      rc;
1692
1693         ENTRY;
1694
1695         oqctl = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1696         if (oqctl == NULL)
1697                 RETURN(err_serious(-EPROTO));
1698
1699         repoqc = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
1700         if (repoqc == NULL)
1701                 RETURN(err_serious(-ENOMEM));
1702
1703         /* report success for quota on/off for interoperability with current MDT
1704          * stack */
1705         if (oqctl->qc_cmd == Q_QUOTAON || oqctl->qc_cmd == Q_QUOTAOFF)
1706                 RETURN(0);
1707
1708         *repoqc = *oqctl;
1709         rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom, repoqc);
1710
1711         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_QUOTACTL,
1712                          tsi->tsi_jobid, 1);
1713
1714         RETURN(rc);
1715 }
1716
1717 /* High priority request handlers for OFD */
1718
1719 /* prolong locks for the current service time of the corresponding
1720  * portal (= OST_IO_PORTAL)
1721  */
1722 static inline int prolong_timeout(struct ptlrpc_request *req)
1723 {
1724         struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
1725
1726         if (AT_OFF)
1727                 return obd_timeout / 2;
1728
1729         return max(at_est2timeout(at_get(&svcpt->scp_at_estimate)),
1730                    ldlm_timeout);
1731 }
1732
1733 static int ofd_prolong_one_lock(struct tgt_session_info *tsi,
1734                                 struct ldlm_lock *lock,
1735                                 struct ldlm_extent *extent, int timeout)
1736 {
1737
1738         if (lock->l_flags & LDLM_FL_DESTROYED) /* lock already cancelled */
1739                 return 0;
1740
1741         /* XXX: never try to grab resource lock here because we're inside
1742          * exp_bl_list_lock; in ldlm_lockd.c to handle waiting list we take
1743          * res lock and then exp_bl_list_lock. */
1744
1745         if (!(lock->l_flags & LDLM_FL_AST_SENT))
1746                 /* ignore locks not being cancelled */
1747                 return 0;
1748
1749         LDLM_DEBUG(lock, "refreshed for req x"LPU64" ext("LPU64"->"LPU64") "
1750                          "to %ds.\n", tgt_ses_req(tsi)->rq_xid, extent->start,
1751                          extent->end, timeout);
1752
1753         /* OK. this is a possible lock the user holds doing I/O
1754          * let's refresh eviction timer for it */
1755         ldlm_refresh_waiting_lock(lock, timeout);
1756         return 1;
1757 }
1758
1759 static int ofd_prolong_extent_locks(struct tgt_session_info *tsi,
1760                                     __u64 start, __u64 end)
1761 {
1762         struct obd_export       *exp = tsi->tsi_exp;
1763         struct obdo             *oa  = &tsi->tsi_ost_body->oa;
1764         struct ldlm_extent       extent = {
1765                 .start = start,
1766                 .end = end
1767         };
1768         struct ldlm_lock        *lock;
1769         int                      timeout = prolong_timeout(tgt_ses_req(tsi));
1770         int                      lock_count = 0;
1771
1772         ENTRY;
1773
1774         if (oa->o_valid & OBD_MD_FLHANDLE) {
1775                 /* mostly a request should be covered by only one lock, try
1776                  * fast path. */
1777                 lock = ldlm_handle2lock(&oa->o_handle);
1778                 if (lock != NULL) {
1779                         /* Fast path to check if the lock covers the whole IO
1780                          * region exclusively. */
1781                         if (lock->l_granted_mode == LCK_PW &&
1782                             ldlm_extent_contain(&lock->l_policy_data.l_extent,
1783                                                 &extent)) {
1784                                 /* bingo */
1785                                 LASSERT(lock->l_export == exp);
1786                                 lock_count = ofd_prolong_one_lock(tsi, lock,
1787                                                              &extent, timeout);
1788                                 LDLM_LOCK_PUT(lock);
1789                                 RETURN(lock_count);
1790                         }
1791                         LDLM_LOCK_PUT(lock);
1792                 }
1793         }
1794
1795         spin_lock_bh(&exp->exp_bl_list_lock);
1796         list_for_each_entry(lock, &exp->exp_bl_list, l_exp_list) {
1797                 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1798                 LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
1799
1800                 if (!ldlm_res_eq(&tsi->tsi_resid, &lock->l_resource->lr_name))
1801                         continue;
1802
1803                 if (!ldlm_extent_overlap(&lock->l_policy_data.l_extent,
1804                                          &extent))
1805                         continue;
1806
1807                 lock_count += ofd_prolong_one_lock(tsi, lock, &extent, timeout);
1808         }
1809         spin_unlock_bh(&exp->exp_bl_list_lock);
1810
1811         RETURN(lock_count);
1812 }
1813
1814 /**
1815  * Returns 1 if the given PTLRPC matches the given LDLM lock, or 0 if it does
1816  * not.
1817  */
1818 static int ofd_rw_hpreq_lock_match(struct ptlrpc_request *req,
1819                                    struct ldlm_lock *lock)
1820 {
1821         struct niobuf_remote    *rnb;
1822         struct obd_ioobj        *ioo;
1823         ldlm_mode_t              mode;
1824         struct ldlm_extent       ext;
1825         __u32                    opc = lustre_msg_get_opc(req->rq_reqmsg);
1826
1827         ENTRY;
1828
1829         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1830         LASSERT(ioo != NULL);
1831
1832         rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1833         LASSERT(rnb != NULL);
1834
1835         ext.start = rnb->offset;
1836         rnb += ioo->ioo_bufcnt - 1;
1837         ext.end = rnb->offset + rnb->len - 1;
1838
1839         LASSERT(lock->l_resource != NULL);
1840         if (!ostid_res_name_eq(&ioo->ioo_oid, &lock->l_resource->lr_name))
1841                 RETURN(0);
1842
1843         mode = LCK_PW;
1844         if (opc == OST_READ)
1845                 mode |= LCK_PR;
1846
1847         if (!(lock->l_granted_mode & mode))
1848                 RETURN(0);
1849
1850         RETURN(ldlm_extent_overlap(&lock->l_policy_data.l_extent, &ext));
1851 }
1852
1853 /**
1854  * High-priority queue request check for whether the given PTLRPC request
1855  * (\a req) is blocking an LDLM lock cancel.
1856  *
1857  * Returns 1 if the given given PTLRPC request (\a req) is blocking an LDLM lock
1858  * cancel, 0 if it is not, and -EFAULT if the request is malformed.
1859  *
1860  * Only OST_READs, OST_WRITEs and OST_PUNCHes go on the h-p RPC queue.  This
1861  * function looks only at OST_READs and OST_WRITEs.
1862  */
1863 static int ofd_rw_hpreq_check(struct ptlrpc_request *req)
1864 {
1865         struct tgt_session_info *tsi;
1866         struct obd_ioobj        *ioo;
1867         struct niobuf_remote    *rnb;
1868         __u64                    start, end;
1869         int                      lock_count;
1870
1871         ENTRY;
1872
1873         /* Don't use tgt_ses_info() to get session info, because lock_match()
1874          * can be called while request has no processing thread yet. */
1875         tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
1876         LASSERT(tsi != NULL);
1877
1878         /*
1879          * Use LASSERT below because malformed RPCs should have
1880          * been filtered out in tgt_hpreq_handler().
1881          */
1882         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
1883         LASSERT(ioo != NULL);
1884
1885         rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
1886         LASSERT(rnb != NULL);
1887         LASSERT(!(rnb->flags & OBD_BRW_SRVLOCK));
1888
1889         start = rnb->offset;
1890         rnb += ioo->ioo_bufcnt - 1;
1891         end = rnb->offset + rnb->len - 1;
1892
1893         DEBUG_REQ(D_RPCTRACE, req, "%s %s: refresh rw locks: "DFID
1894                                    " ("LPU64"->"LPU64")\n",
1895                   tgt_name(tsi->tsi_tgt), current->comm,
1896                   PFID(&tsi->tsi_fid), start, end);
1897
1898         lock_count = ofd_prolong_extent_locks(tsi, start, end);
1899
1900         CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
1901                tgt_name(tsi->tsi_tgt), lock_count, req);
1902
1903         RETURN(lock_count > 0);
1904 }
1905
1906 static void ofd_rw_hpreq_fini(struct ptlrpc_request *req)
1907 {
1908         ofd_rw_hpreq_check(req);
1909 }
1910
1911 /**
1912  * Like tgt_rw_hpreq_lock_match(), but for OST_PUNCH RPCs.
1913  */
1914 static int ofd_punch_hpreq_lock_match(struct ptlrpc_request *req,
1915                                       struct ldlm_lock *lock)
1916 {
1917         struct tgt_session_info *tsi;
1918
1919         /* Don't use tgt_ses_info() to get session info, because lock_match()
1920          * can be called while request has no processing thread yet. */
1921         tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
1922         LASSERT(tsi != NULL);
1923
1924         LASSERT(tsi->tsi_ost_body != NULL);
1925         if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLHANDLE &&
1926             tsi->tsi_ost_body->oa.o_handle.cookie == lock->l_handle.h_cookie)
1927                 return 1;
1928
1929         return 0;
1930 }
1931
1932 /**
1933  * Like ost_rw_hpreq_check(), but for OST_PUNCH RPCs.
1934  */
1935 static int ofd_punch_hpreq_check(struct ptlrpc_request *req)
1936 {
1937         struct tgt_session_info *tsi;
1938         struct obdo             *oa;
1939         int                      lock_count;
1940
1941         ENTRY;
1942
1943         /* Don't use tgt_ses_info() to get session info, because lock_match()
1944          * can be called while request has no processing thread yet. */
1945         tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
1946         LASSERT(tsi != NULL);
1947         oa = &tsi->tsi_ost_body->oa;
1948
1949         LASSERT(!(oa->o_valid & OBD_MD_FLFLAGS &&
1950                   oa->o_flags & OBD_FL_SRVLOCK));
1951
1952         CDEBUG(D_DLMTRACE,
1953                "%s: refresh locks: "LPU64"/"LPU64" ("LPU64"->"LPU64")\n",
1954                tgt_name(tsi->tsi_tgt), tsi->tsi_resid.name[0],
1955                tsi->tsi_resid.name[1], oa->o_size, oa->o_blocks);
1956
1957         lock_count = ofd_prolong_extent_locks(tsi, oa->o_size, oa->o_blocks);
1958
1959         CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
1960                tgt_name(tsi->tsi_tgt), lock_count, req);
1961
1962         RETURN(lock_count > 0);
1963 }
1964
1965 static void ofd_punch_hpreq_fini(struct ptlrpc_request *req)
1966 {
1967         ofd_punch_hpreq_check(req);
1968 }
1969
1970 struct ptlrpc_hpreq_ops ofd_hpreq_rw = {
1971         .hpreq_lock_match       = ofd_rw_hpreq_lock_match,
1972         .hpreq_check            = ofd_rw_hpreq_check,
1973         .hpreq_fini             = ofd_rw_hpreq_fini
1974 };
1975
1976 struct ptlrpc_hpreq_ops ofd_hpreq_punch = {
1977         .hpreq_lock_match       = ofd_punch_hpreq_lock_match,
1978         .hpreq_check            = ofd_punch_hpreq_check,
1979         .hpreq_fini             = ofd_punch_hpreq_fini
1980 };
1981
1982 /** Assign high priority operations to the IO requests */
1983 static void ofd_hp_brw(struct tgt_session_info *tsi)
1984 {
1985         struct niobuf_remote    *rnb;
1986         struct obd_ioobj        *ioo;
1987
1988         ENTRY;
1989
1990         ioo = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_IOOBJ);
1991         LASSERT(ioo != NULL); /* must exist after request preprocessing */
1992         if (ioo->ioo_bufcnt > 0) {
1993                 rnb = req_capsule_client_get(tsi->tsi_pill, &RMF_NIOBUF_REMOTE);
1994                 LASSERT(rnb != NULL); /* must exist after request preprocessing */
1995
1996                 /* no high priority if server lock is needed */
1997                 if (rnb->flags & OBD_BRW_SRVLOCK)
1998                         return;
1999         }
2000         tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_rw;
2001 }
2002
2003 static void ofd_hp_punch(struct tgt_session_info *tsi)
2004 {
2005         LASSERT(tsi->tsi_ost_body != NULL); /* must exists if we are here */
2006         /* no high-priority if server lock is needed */
2007         if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
2008             tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK)
2009                 return;
2010         tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_punch;
2011 }
2012
2013 #define OBD_FAIL_OST_READ_NET   OBD_FAIL_OST_BRW_NET
2014 #define OBD_FAIL_OST_WRITE_NET  OBD_FAIL_OST_BRW_NET
2015 #define OST_BRW_READ    OST_READ
2016 #define OST_BRW_WRITE   OST_WRITE
2017
2018 static struct tgt_handler ofd_tgt_handlers[] = {
2019 TGT_RPC_HANDLER(OST_FIRST_OPC,
2020                 0,                      OST_CONNECT,    tgt_connect,
2021                 &RQF_CONNECT, LUSTRE_OBD_VERSION),
2022 TGT_RPC_HANDLER(OST_FIRST_OPC,
2023                 0,                      OST_DISCONNECT, tgt_disconnect,
2024                 &RQF_OST_DISCONNECT, LUSTRE_OBD_VERSION),
2025 TGT_RPC_HANDLER(OST_FIRST_OPC,
2026                 0,                      OST_SET_INFO,   ofd_set_info_hdl,
2027                 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
2028 TGT_OST_HDL(0,                          OST_GET_INFO,   ofd_get_info_hdl),
2029 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_GETATTR,    ofd_getattr_hdl),
2030 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
2031                                         OST_SETATTR,    ofd_setattr_hdl),
2032 TGT_OST_HDL(0           | HABEO_REFERO | MUTABOR,
2033                                         OST_CREATE,     ofd_create_hdl),
2034 TGT_OST_HDL(0           | HABEO_REFERO | MUTABOR,
2035                                         OST_DESTROY,    ofd_destroy_hdl),
2036 TGT_OST_HDL(0           | HABEO_REFERO, OST_STATFS,     ofd_statfs_hdl),
2037 TGT_OST_HDL_HP(HABEO_CORPUS| HABEO_REFERO,
2038                                         OST_BRW_READ,   tgt_brw_read,
2039                                                         ofd_hp_brw),
2040 /* don't set CORPUS flag for brw_write because -ENOENT may be valid case */
2041 TGT_OST_HDL_HP(HABEO_CORPUS| MUTABOR,   OST_BRW_WRITE,  tgt_brw_write,
2042                                                         ofd_hp_brw),
2043 TGT_OST_HDL_HP(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
2044                                         OST_PUNCH,      ofd_punch_hdl,
2045                                                         ofd_hp_punch),
2046 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_SYNC,       ofd_sync_hdl),
2047 TGT_OST_HDL(0           | HABEO_REFERO, OST_QUOTACTL,   ofd_quotactl),
2048 };
2049
2050 static struct tgt_opc_slice ofd_common_slice[] = {
2051         {
2052                 .tos_opc_start  = OST_FIRST_OPC,
2053                 .tos_opc_end    = OST_LAST_OPC,
2054                 .tos_hs         = ofd_tgt_handlers
2055         },
2056         {
2057                 .tos_opc_start  = OBD_FIRST_OPC,
2058                 .tos_opc_end    = OBD_LAST_OPC,
2059                 .tos_hs         = tgt_obd_handlers
2060         },
2061         {
2062                 .tos_opc_start  = LDLM_FIRST_OPC,
2063                 .tos_opc_end    = LDLM_LAST_OPC,
2064                 .tos_hs         = tgt_dlm_handlers
2065         },
2066         {
2067                 .tos_opc_start  = OUT_UPDATE_FIRST_OPC,
2068                 .tos_opc_end    = OUT_UPDATE_LAST_OPC,
2069                 .tos_hs         = tgt_out_handlers
2070         },
2071         {
2072                 .tos_opc_start  = SEQ_FIRST_OPC,
2073                 .tos_opc_end    = SEQ_LAST_OPC,
2074                 .tos_hs         = seq_handlers
2075         },
2076         {
2077                 .tos_opc_start  = LFSCK_FIRST_OPC,
2078                 .tos_opc_end    = LFSCK_LAST_OPC,
2079                 .tos_hs         = tgt_lfsck_handlers
2080         },
2081         {
2082                 .tos_hs         = NULL
2083         }
2084 };
2085
2086 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
2087                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
2088 {
2089         const char              *dev = lustre_cfg_string(cfg, 0);
2090         struct ofd_thread_info  *info = NULL;
2091         struct obd_device       *obd;
2092         struct obd_statfs       *osfs;
2093         int                      rc;
2094
2095         ENTRY;
2096
2097         obd = class_name2obd(dev);
2098         if (obd == NULL) {
2099                 CERROR("Cannot find obd with name %s\n", dev);
2100                 RETURN(-ENODEV);
2101         }
2102
2103         rc = lu_env_refill((struct lu_env *)env);
2104         if (rc != 0)
2105                 RETURN(rc);
2106
2107         obd->u.obt.obt_magic = OBT_MAGIC;
2108
2109         m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
2110         m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
2111
2112         spin_lock_init(&m->ofd_flags_lock);
2113         m->ofd_raid_degraded = 0;
2114         m->ofd_syncjournal = 0;
2115         ofd_slc_set(m);
2116         m->ofd_grant_compat_disable = 0;
2117         m->ofd_soft_sync_limit = OFD_SOFT_SYNC_LIMIT_DEFAULT;
2118
2119         /* statfs data */
2120         spin_lock_init(&m->ofd_osfs_lock);
2121         m->ofd_osfs_age = cfs_time_shift_64(-1000);
2122         m->ofd_osfs_unstable = 0;
2123         m->ofd_statfs_inflight = 0;
2124         m->ofd_osfs_inflight = 0;
2125
2126         /* grant data */
2127         spin_lock_init(&m->ofd_grant_lock);
2128         m->ofd_tot_dirty = 0;
2129         m->ofd_tot_granted = 0;
2130         m->ofd_tot_pending = 0;
2131         m->ofd_seq_count = 0;
2132         init_waitqueue_head(&m->ofd_inconsistency_thread.t_ctl_waitq);
2133         INIT_LIST_HEAD(&m->ofd_inconsistency_list);
2134         spin_lock_init(&m->ofd_inconsistency_lock);
2135
2136         spin_lock_init(&m->ofd_batch_lock);
2137         init_rwsem(&m->ofd_lastid_rwsem);
2138
2139         obd->u.filter.fo_fl_oss_capa = 0;
2140         CFS_INIT_LIST_HEAD(&obd->u.filter.fo_capa_keys);
2141         obd->u.filter.fo_capa_hash = init_capa_hash();
2142         if (obd->u.filter.fo_capa_hash == NULL)
2143                 RETURN(-ENOMEM);
2144
2145         m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
2146         m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
2147         /* set this lu_device to obd, because error handling need it */
2148         obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
2149
2150         rc = ofd_procfs_init(m);
2151         if (rc) {
2152                 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
2153                 RETURN(rc);
2154         }
2155
2156         /* No connection accepted until configurations will finish */
2157         spin_lock(&obd->obd_dev_lock);
2158         obd->obd_no_conn = 1;
2159         spin_unlock(&obd->obd_dev_lock);
2160         obd->obd_replayable = 1;
2161         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
2162                 char *str = lustre_cfg_string(cfg, 4);
2163
2164                 if (strchr(str, 'n')) {
2165                         CWARN("%s: recovery disabled\n", obd->obd_name);
2166                         obd->obd_replayable = 0;
2167                 }
2168         }
2169
2170         info = ofd_info_init(env, NULL);
2171         if (info == NULL)
2172                 RETURN(-EFAULT);
2173
2174         rc = ofd_stack_init(env, m, cfg);
2175         if (rc) {
2176                 CERROR("Can't init device stack, rc %d\n", rc);
2177                 GOTO(err_fini_proc, rc);
2178         }
2179
2180         ofd_procfs_add_brw_stats_symlink(m);
2181
2182         /* populate cached statfs data */
2183         osfs = &ofd_info(env)->fti_u.osfs;
2184         rc = ofd_statfs_internal(env, m, osfs, 0, NULL);
2185         if (rc != 0) {
2186                 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
2187                 GOTO(err_fini_stack, rc);
2188         }
2189         if (!IS_PO2(osfs->os_bsize)) {
2190                 CERROR("%s: blocksize (%d) is not a power of 2\n",
2191                                 obd->obd_name, osfs->os_bsize);
2192                 GOTO(err_fini_stack, rc = -EPROTO);
2193         }
2194         m->ofd_blockbits = fls(osfs->os_bsize) - 1;
2195
2196         m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
2197         if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
2198                 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
2199
2200         snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
2201                  "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
2202         m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
2203                                               LDLM_NAMESPACE_SERVER,
2204                                               LDLM_NAMESPACE_GREEDY,
2205                                               LDLM_NS_TYPE_OST);
2206         if (m->ofd_namespace == NULL)
2207                 GOTO(err_fini_stack, rc = -ENOMEM);
2208         /* set obd_namespace for compatibility with old code */
2209         obd->obd_namespace = m->ofd_namespace;
2210         ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
2211         m->ofd_namespace->ns_lvbo = &ofd_lvbo;
2212         m->ofd_namespace->ns_lvbp = m;
2213
2214         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
2215                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
2216
2217         dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
2218
2219         /* Allow at most ddp_grant_reserved% of the available filesystem space
2220          * to be granted to clients, so that any errors in the grant overhead
2221          * calculations do not allow granting more space to clients than can be
2222          * written. Assumes that in aggregate the grant overhead calculations do
2223          * not have more than ddp_grant_reserved% estimation error in them. */
2224         m->ofd_grant_ratio =
2225                 ofd_grant_ratio_conv(m->ofd_dt_conf.ddp_grant_reserved);
2226
2227         rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd, ofd_common_slice,
2228                       OBD_FAIL_OST_ALL_REQUEST_NET,
2229                       OBD_FAIL_OST_ALL_REPLY_NET);
2230         if (rc)
2231                 GOTO(err_free_ns, rc);
2232
2233         rc = ofd_fs_setup(env, m, obd);
2234         if (rc)
2235                 GOTO(err_fini_lut, rc);
2236
2237         rc = ofd_start_inconsistency_verification_thread(m);
2238         if (rc != 0)
2239                 GOTO(err_fini_fs, rc);
2240
2241         RETURN(0);
2242
2243 err_fini_fs:
2244         ofd_fs_cleanup(env, m);
2245 err_fini_lut:
2246         tgt_fini(env, &m->ofd_lut);
2247 err_free_ns:
2248         ldlm_namespace_free(m->ofd_namespace, 0, obd->obd_force);
2249         obd->obd_namespace = m->ofd_namespace = NULL;
2250 err_fini_stack:
2251         ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
2252 err_fini_proc:
2253         ofd_procfs_fini(m);
2254         return rc;
2255 }
2256
2257 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
2258 {
2259         struct obd_device       *obd = ofd_obd(m);
2260         struct lu_device        *d   = &m->ofd_dt_dev.dd_lu_dev;
2261         struct lfsck_stop        stop;
2262
2263         stop.ls_status = LS_PAUSED;
2264         stop.ls_flags = 0;
2265         lfsck_stop(env, m->ofd_osd, &stop);
2266         target_recovery_fini(obd);
2267         obd_exports_barrier(obd);
2268         obd_zombie_barrier();
2269
2270         tgt_fini(env, &m->ofd_lut);
2271         ofd_stop_inconsistency_verification_thread(m);
2272         lfsck_degister(env, m->ofd_osd);
2273         ofd_fs_cleanup(env, m);
2274
2275         ofd_free_capa_keys(m);
2276         cleanup_capa_hash(obd->u.filter.fo_capa_hash);
2277
2278         if (m->ofd_namespace != NULL) {
2279                 ldlm_namespace_free(m->ofd_namespace, NULL,
2280                                     d->ld_obd->obd_force);
2281                 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
2282         }
2283
2284         ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
2285         ofd_procfs_fini(m);
2286         LASSERT(atomic_read(&d->ld_ref) == 0);
2287         server_put_mount(obd->obd_name, true);
2288         EXIT;
2289 }
2290
2291 static struct lu_device *ofd_device_fini(const struct lu_env *env,
2292                                          struct lu_device *d)
2293 {
2294         ENTRY;
2295         ofd_fini(env, ofd_dev(d));
2296         RETURN(NULL);
2297 }
2298
2299 static struct lu_device *ofd_device_free(const struct lu_env *env,
2300                                          struct lu_device *d)
2301 {
2302         struct ofd_device *m = ofd_dev(d);
2303
2304         dt_device_fini(&m->ofd_dt_dev);
2305         OBD_FREE_PTR(m);
2306         RETURN(NULL);
2307 }
2308
2309 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
2310                                           struct lu_device_type *t,
2311                                           struct lustre_cfg *cfg)
2312 {
2313         struct ofd_device *m;
2314         struct lu_device  *l;
2315         int                rc;
2316
2317         OBD_ALLOC_PTR(m);
2318         if (m == NULL)
2319                 return ERR_PTR(-ENOMEM);
2320
2321         l = &m->ofd_dt_dev.dd_lu_dev;
2322         dt_device_init(&m->ofd_dt_dev, t);
2323         rc = ofd_init0(env, m, t, cfg);
2324         if (rc != 0) {
2325                 ofd_device_free(env, l);
2326                 l = ERR_PTR(rc);
2327         }
2328
2329         return l;
2330 }
2331
2332 /* thread context key constructor/destructor */
2333 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
2334
2335 static void ofd_key_exit(const struct lu_context *ctx,
2336                          struct lu_context_key *key, void *data)
2337 {
2338         struct ofd_thread_info *info = data;
2339
2340         info->fti_env = NULL;
2341         info->fti_exp = NULL;
2342
2343         info->fti_xid = 0;
2344         info->fti_pre_version = 0;
2345         info->fti_used = 0;
2346
2347         memset(&info->fti_attr, 0, sizeof info->fti_attr);
2348 }
2349
2350 struct lu_context_key ofd_thread_key = {
2351         .lct_tags = LCT_DT_THREAD,
2352         .lct_init = ofd_key_init,
2353         .lct_fini = ofd_key_fini,
2354         .lct_exit = ofd_key_exit
2355 };
2356
2357 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
2358 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
2359
2360 static struct lu_device_type_operations ofd_device_type_ops = {
2361         .ldto_init              = ofd_type_init,
2362         .ldto_fini              = ofd_type_fini,
2363
2364         .ldto_start             = ofd_type_start,
2365         .ldto_stop              = ofd_type_stop,
2366
2367         .ldto_device_alloc      = ofd_device_alloc,
2368         .ldto_device_free       = ofd_device_free,
2369         .ldto_device_fini       = ofd_device_fini
2370 };
2371
2372 static struct lu_device_type ofd_device_type = {
2373         .ldt_tags       = LU_DEVICE_DT,
2374         .ldt_name       = LUSTRE_OST_NAME,
2375         .ldt_ops        = &ofd_device_type_ops,
2376         .ldt_ctx_tags   = LCT_DT_THREAD
2377 };
2378
2379 int __init ofd_init(void)
2380 {
2381         int                             rc;
2382
2383         rc = lu_kmem_init(ofd_caches);
2384         if (rc)
2385                 return rc;
2386
2387         rc = ofd_fmd_init();
2388         if (rc) {
2389                 lu_kmem_fini(ofd_caches);
2390                 return(rc);
2391         }
2392
2393         rc = class_register_type(&ofd_obd_ops, NULL, true, NULL,
2394 #ifndef HAVE_ONLY_PROCFS_SEQ
2395                                  NULL,
2396 #endif
2397                                  LUSTRE_OST_NAME, &ofd_device_type);
2398         return rc;
2399 }
2400
2401 void __exit ofd_exit(void)
2402 {
2403         ofd_fmd_exit();
2404         lu_kmem_fini(ofd_caches);
2405         class_unregister_type(LUSTRE_OST_NAME);
2406 }
2407
2408 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
2409 MODULE_DESCRIPTION("Lustre Object Filtering Device");
2410 MODULE_LICENSE("GPL");
2411
2412 module_init(ofd_init);
2413 module_exit(ofd_exit);