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