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