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