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