Whamcloud - gitweb
New release 2.10.8
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ofd/ofd_dev.c
33  *
34  * This file contains OSD API methods for OBD Filter Device (OFD),
35  * request handlers and supplemental functions to set OFD up and clean it up.
36  *
37  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
38  * Author: Mike Pershin <mike.pershin@intel.com>
39  * Author: Johann Lombardi <johann.lombardi@intel.com>
40  */
41 /*
42  * The OBD Filter Device (OFD) module belongs to the Object Storage
43  * Server stack and connects the RPC oriented Unified Target (TGT)
44  * layer (see lustre/include/lu_target.h) to the storage oriented OSD
45  * layer (see Documentation/osd-api.txt).
46  *
47  *     TGT
48  *      |      DT and OBD APIs
49  *     OFD
50  *      |      DT API
51  *     OSD
52  *
53  * OFD implements the LU and OBD device APIs and is responsible for:
54  *
55  * - Handling client requests (create, destroy, bulk IO, setattr,
56  *   get_info, set_info, statfs) for the objects belonging to the OST
57  *   (together with TGT).
58  *
59  * - Providing grant space management which allows clients to reserve
60  *   disk space for data writeback. OFD tracks grants on global and
61  *   per client levels.
62  *
63  * - Handling object precreation requests from MDTs.
64  *
65  * - Operating the LDLM service that allows clients to maintain object
66  *   data cache coherence.
67  */
68
69 #define DEBUG_SUBSYSTEM S_FILTER
70
71 #include <obd_class.h>
72 #include <obd_cksum.h>
73 #include <uapi/linux/lustre_param.h>
74 #include <lustre_fid.h>
75 #include <lustre_lfsck.h>
76 #include <lustre/lustre_idl.h>
77 #include <lustre_dlm.h>
78 #include <lustre_quota.h>
79 #include <lustre_nodemap.h>
80 #include <lustre_log.h>
81
82 #include "ofd_internal.h"
83
84 /* Slab for OFD object allocation */
85 static struct kmem_cache *ofd_object_kmem;
86
87 static struct lu_kmem_descr ofd_caches[] = {
88         {
89                 .ckd_cache = &ofd_object_kmem,
90                 .ckd_name  = "ofd_obj",
91                 .ckd_size  = sizeof(struct ofd_object)
92         },
93         {
94                 .ckd_cache = NULL
95         }
96 };
97
98 /**
99  * Connect OFD to the next device in the stack.
100  *
101  * This function is used for device stack configuration and links OFD
102  * device with bottom OSD device.
103  *
104  * \param[in]  env      execution environment
105  * \param[in]  m        OFD device
106  * \param[in]  next     name of next device in the stack
107  * \param[out] exp      export to return
108  *
109  * \retval              0 and export in \a exp if successful
110  * \retval              negative value on error
111  */
112 static int ofd_connect_to_next(const struct lu_env *env, struct ofd_device *m,
113                                const char *next, struct obd_export **exp)
114 {
115         struct obd_connect_data *data = NULL;
116         struct obd_device       *obd;
117         int                      rc;
118         ENTRY;
119
120         OBD_ALLOC_PTR(data);
121         if (data == NULL)
122                 GOTO(out, rc = -ENOMEM);
123
124         obd = class_name2obd(next);
125         if (obd == NULL) {
126                 CERROR("%s: can't locate next device: %s\n",
127                        ofd_name(m), next);
128                 GOTO(out, rc = -ENOTCONN);
129         }
130
131         data->ocd_connect_flags = OBD_CONNECT_VERSION;
132         data->ocd_version = LUSTRE_VERSION_CODE;
133
134         rc = obd_connect(NULL, exp, obd, &obd->obd_uuid, data, NULL);
135         if (rc) {
136                 CERROR("%s: cannot connect to next dev %s: rc = %d\n",
137                        ofd_name(m), next, rc);
138                 GOTO(out, rc);
139         }
140
141         m->ofd_dt_dev.dd_lu_dev.ld_site =
142                 m->ofd_osd_exp->exp_obd->obd_lu_dev->ld_site;
143         LASSERT(m->ofd_dt_dev.dd_lu_dev.ld_site);
144         m->ofd_osd = lu2dt_dev(m->ofd_osd_exp->exp_obd->obd_lu_dev);
145         m->ofd_dt_dev.dd_lu_dev.ld_site->ls_top_dev = &m->ofd_dt_dev.dd_lu_dev;
146
147 out:
148         if (data)
149                 OBD_FREE_PTR(data);
150         RETURN(rc);
151 }
152
153 /**
154  * Initialize stack of devices.
155  *
156  * This function initializes OFD-OSD device stack to serve OST requests
157  *
158  * \param[in] env       execution environment
159  * \param[in] m         OFD device
160  * \param[in] cfg       Lustre config for this server
161  *
162  * \retval              0 if successful
163  * \retval              negative value on error
164  */
165 static int ofd_stack_init(const struct lu_env *env,
166                           struct ofd_device *m, struct lustre_cfg *cfg)
167 {
168         const char              *dev = lustre_cfg_string(cfg, 0);
169         struct lu_device        *d;
170         struct ofd_thread_info  *info = ofd_info(env);
171         struct lustre_mount_info *lmi;
172         struct lustre_mount_data *lmd;
173         int                      rc;
174         char                    *osdname;
175
176         ENTRY;
177
178         lmi = server_get_mount(dev);
179         if (lmi == NULL) {
180                 CERROR("Cannot get mount info for %s!\n", dev);
181                 RETURN(-ENODEV);
182         }
183
184         lmd = s2lsi(lmi->lmi_sb)->lsi_lmd;
185         if (lmd != NULL && lmd->lmd_flags & LMD_FLG_SKIP_LFSCK)
186                 m->ofd_skip_lfsck = 1;
187
188         /* find bottom osd */
189         OBD_ALLOC(osdname, MTI_NAME_MAXLEN);
190         if (osdname == NULL)
191                 RETURN(-ENOMEM);
192
193         snprintf(osdname, MTI_NAME_MAXLEN, "%s-osd", dev);
194         rc = ofd_connect_to_next(env, m, osdname, &m->ofd_osd_exp);
195         OBD_FREE(osdname, MTI_NAME_MAXLEN);
196         if (rc)
197                 RETURN(rc);
198
199         d = m->ofd_osd_exp->exp_obd->obd_lu_dev;
200         LASSERT(d);
201         m->ofd_osd = lu2dt_dev(d);
202
203         snprintf(info->fti_u.name, sizeof(info->fti_u.name),
204                  "%s-osd", lustre_cfg_string(cfg, 0));
205
206         RETURN(rc);
207 }
208
209 /**
210  * Finalize the device stack OFD-OSD.
211  *
212  * This function cleans OFD-OSD device stack and
213  * disconnects OFD from the OSD.
214  *
215  * \param[in] env       execution environment
216  * \param[in] m         OFD device
217  * \param[in] top       top device of stack
218  *
219  * \retval              0 if successful
220  * \retval              negative value on error
221  */
222 static void ofd_stack_fini(const struct lu_env *env, struct ofd_device *m,
223                            struct lu_device *top)
224 {
225         struct obd_device       *obd = ofd_obd(m);
226         struct lustre_cfg_bufs   bufs;
227         struct lustre_cfg       *lcfg;
228         char                     flags[3] = "";
229
230         ENTRY;
231
232         lu_site_purge(env, top->ld_site, ~0);
233         /* process cleanup, pass mdt obd name to get obd umount flags */
234         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
235         if (obd->obd_force)
236                 strcat(flags, "F");
237         if (obd->obd_fail)
238                 strcat(flags, "A");
239         lustre_cfg_bufs_set_string(&bufs, 1, flags);
240         OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount, bufs.lcfg_buflen));
241         if (!lcfg)
242                 RETURN_EXIT;
243         lustre_cfg_init(lcfg, LCFG_CLEANUP, &bufs);
244
245         LASSERT(top);
246         top->ld_ops->ldo_process_config(env, top, lcfg);
247         OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens));
248
249         lu_site_purge(env, top->ld_site, ~0);
250         if (!cfs_hash_is_empty(top->ld_site->ls_obj_hash)) {
251                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
252                 lu_site_print(env, top->ld_site, &msgdata, lu_cdebug_printer);
253         }
254
255         LASSERT(m->ofd_osd_exp);
256         obd_disconnect(m->ofd_osd_exp);
257
258         EXIT;
259 }
260
261 /* For interoperability, see mdt_interop_param[]. */
262 static struct cfg_interop_param ofd_interop_param[] = {
263         { "ost.quota_type",     NULL },
264         { NULL }
265 };
266
267 /**
268  * Check if parameters are symlinks to the OSD.
269  *
270  * Some parameters were moved from ofd to osd and only their
271  * symlinks were kept in ofd by LU-3106. They are:
272  * -writehthrough_cache_enable
273  * -readcache_max_filesize
274  * -read_cache_enable
275  * -brw_stats
276  *
277  * Since they are not included by the static lprocfs var list, a pre-check
278  * is added for them to avoid "unknown param" errors. If they are matched
279  * in this check, they will be passed to the OSD directly.
280  *
281  * \param[in] param     parameters to check
282  *
283  * \retval              true if param is symlink to OSD param
284  *                      false otherwise
285  */
286 static bool match_symlink_param(char *param)
287 {
288         char *sval;
289         int paramlen;
290
291         if (class_match_param(param, PARAM_OST, &param) == 0) {
292                 sval = strchr(param, '=');
293                 if (sval != NULL) {
294                         paramlen = sval - param;
295                         if (strncmp(param, "writethrough_cache_enable",
296                                     paramlen) == 0 ||
297                             strncmp(param, "readcache_max_filesize",
298                                     paramlen) == 0 ||
299                             strncmp(param, "read_cache_enable",
300                                     paramlen) == 0 ||
301                             strncmp(param, "brw_stats", paramlen) == 0)
302                                 return true;
303                 }
304         }
305
306         return false;
307 }
308
309 /**
310  * Process various configuration parameters.
311  *
312  * This function is used by MGS to process specific configurations and
313  * pass them through to the next device in server stack, i.e. the OSD.
314  *
315  * \param[in] env       execution environment
316  * \param[in] d         LU device of OFD
317  * \param[in] cfg       parameters to process
318  *
319  * \retval              0 if successful
320  * \retval              negative value on error
321  */
322 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
323                               struct lustre_cfg *cfg)
324 {
325         struct ofd_device       *m = ofd_dev(d);
326         struct dt_device        *dt_next = m->ofd_osd;
327         struct lu_device        *next = &dt_next->dd_lu_dev;
328         int                      rc;
329
330         ENTRY;
331
332         switch (cfg->lcfg_command) {
333         case LCFG_PARAM: {
334                 struct obd_device       *obd = ofd_obd(m);
335                 /* For interoperability */
336                 struct cfg_interop_param   *ptr = NULL;
337                 struct lustre_cfg          *old_cfg = NULL;
338                 char                       *param = NULL;
339
340                 param = lustre_cfg_string(cfg, 1);
341                 if (param == NULL) {
342                         CERROR("param is empty\n");
343                         rc = -EINVAL;
344                         break;
345                 }
346
347                 ptr = class_find_old_param(param, ofd_interop_param);
348                 if (ptr != NULL) {
349                         if (ptr->new_param == NULL) {
350                                 rc = 0;
351                                 CWARN("For interoperability, skip this %s."
352                                       " It is obsolete.\n", ptr->old_param);
353                                 break;
354                         }
355
356                         CWARN("Found old param %s, changed it to %s.\n",
357                               ptr->old_param, ptr->new_param);
358
359                         old_cfg = cfg;
360                         cfg = lustre_cfg_rename(old_cfg, ptr->new_param);
361                         if (IS_ERR(cfg)) {
362                                 rc = PTR_ERR(cfg);
363                                 break;
364                         }
365                 }
366
367                 if (match_symlink_param(param)) {
368                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
369                         break;
370                 }
371
372                 rc = class_process_proc_param(PARAM_OST, obd->obd_vars, cfg,
373                                               d->ld_obd);
374                 if (rc > 0 || rc == -ENOSYS) {
375                         CDEBUG(D_CONFIG, "pass param %s down the stack.\n",
376                                param);
377                         /* we don't understand; pass it on */
378                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
379                 }
380                 break;
381         }
382         case LCFG_SPTLRPC_CONF: {
383                 rc = -ENOTSUPP;
384                 break;
385         }
386         default:
387                 /* others are passed further */
388                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
389                 break;
390         }
391         RETURN(rc);
392 }
393
394 /**
395  * Implementation of lu_object_operations::loo_object_init for OFD
396  *
397  * Allocate just the next object (OSD) in stack.
398  *
399  * \param[in] env       execution environment
400  * \param[in] o         lu_object of OFD object
401  * \param[in] conf      additional configuration parameters, not used here
402  *
403  * \retval              0 if successful
404  * \retval              negative value on error
405  */
406 static int ofd_object_init(const struct lu_env *env, struct lu_object *o,
407                            const struct lu_object_conf *conf)
408 {
409         struct ofd_device       *d = ofd_dev(o->lo_dev);
410         struct lu_device        *under;
411         struct lu_object        *below;
412         int                      rc = 0;
413
414         ENTRY;
415
416         CDEBUG(D_INFO, "object init, fid = "DFID"\n",
417                PFID(lu_object_fid(o)));
418
419         under = &d->ofd_osd->dd_lu_dev;
420         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
421         if (below != NULL)
422                 lu_object_add(o, below);
423         else
424                 rc = -ENOMEM;
425
426         RETURN(rc);
427 }
428
429 /**
430  * Implementation of lu_object_operations::loo_object_free.
431  *
432  * Finish OFD object lifecycle and free its memory.
433  *
434  * \param[in] env       execution environment
435  * \param[in] o         LU object of OFD object
436  */
437 static void ofd_object_free(const struct lu_env *env, struct lu_object *o)
438 {
439         struct ofd_object       *of = ofd_obj(o);
440         struct lu_object_header *h;
441
442         ENTRY;
443
444         h = o->lo_header;
445         CDEBUG(D_INFO, "object free, fid = "DFID"\n",
446                PFID(lu_object_fid(o)));
447
448         lu_object_fini(o);
449         lu_object_header_fini(h);
450         OBD_SLAB_FREE_PTR(of, ofd_object_kmem);
451         EXIT;
452 }
453
454 /**
455  * Implementation of lu_object_operations::loo_object_print.
456  *
457  * Print OFD part of compound OFD-OSD object. See lu_object_print() and
458  * LU_OBJECT_DEBUG() for more details about the compound object printing.
459  *
460  * \param[in] env       execution environment
461  * \param[in] cookie    opaque data passed to the printer function
462  * \param[in] p         printer function to use
463  * \param[in] o         LU object of OFD object
464  *
465  * \retval              0 if successful
466  * \retval              negative value on error
467  */
468 static int ofd_object_print(const struct lu_env *env, void *cookie,
469                             lu_printer_t p, const struct lu_object *o)
470 {
471         return (*p)(env, cookie, LUSTRE_OST_NAME"-object@%p", o);
472 }
473
474 static struct lu_object_operations ofd_obj_ops = {
475         .loo_object_init        = ofd_object_init,
476         .loo_object_free        = ofd_object_free,
477         .loo_object_print       = ofd_object_print
478 };
479
480 /**
481  * Implementation of lu_device_operations::lod_object_alloc.
482  *
483  * This function allocates OFD part of compound OFD-OSD object and
484  * initializes its header, because OFD is the top device in stack
485  *
486  * \param[in] env       execution environment
487  * \param[in] hdr       object header, NULL for OFD
488  * \param[in] d         lu_device
489  *
490  * \retval              allocated object if successful
491  * \retval              NULL value on failed allocation
492  */
493 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
494                                           const struct lu_object_header *hdr,
495                                           struct lu_device *d)
496 {
497         struct ofd_object *of;
498
499         ENTRY;
500
501         OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, GFP_NOFS);
502         if (of != NULL) {
503                 struct lu_object        *o;
504                 struct lu_object_header *h;
505
506                 o = &of->ofo_obj.do_lu;
507                 h = &of->ofo_header;
508                 lu_object_header_init(h);
509                 lu_object_init(o, h, d);
510                 lu_object_add_top(h, o);
511                 o->lo_ops = &ofd_obj_ops;
512                 RETURN(o);
513         } else {
514                 RETURN(NULL);
515         }
516 }
517
518 /**
519  * Return the result of LFSCK run to the OFD.
520  *
521  * Notify OFD about result of LFSCK run. That may block the new object
522  * creation until problem is fixed by LFSCK.
523  *
524  * \param[in] env       execution environment
525  * \param[in] data      pointer to the OFD device
526  * \param[in] event     LFSCK event type
527  *
528  * \retval              0 if successful
529  * \retval              negative value on unknown event
530  */
531 static int ofd_lfsck_out_notify(const struct lu_env *env, void *data,
532                                 enum lfsck_events event)
533 {
534         struct ofd_device *ofd = data;
535         struct obd_device *obd = ofd_obd(ofd);
536
537         switch (event) {
538         case LE_LASTID_REBUILDING:
539                 CWARN("%s: Found crashed LAST_ID, deny creating new OST-object "
540                       "on the device until the LAST_ID rebuilt successfully.\n",
541                       obd->obd_name);
542                 down_write(&ofd->ofd_lastid_rwsem);
543                 ofd->ofd_lastid_rebuilding = 1;
544                 up_write(&ofd->ofd_lastid_rwsem);
545                 break;
546         case LE_LASTID_REBUILT: {
547                 down_write(&ofd->ofd_lastid_rwsem);
548                 ofd_seqs_free(env, ofd);
549                 ofd->ofd_lastid_rebuilding = 0;
550                 ofd->ofd_lastid_gen++;
551                 up_write(&ofd->ofd_lastid_rwsem);
552                 CWARN("%s: Rebuilt crashed LAST_ID files successfully.\n",
553                       obd->obd_name);
554                 break;
555         }
556         default:
557                 CERROR("%s: unknown lfsck event: rc = %d\n",
558                        ofd_name(ofd), event);
559                 return -EINVAL;
560         }
561
562         return 0;
563 }
564
565 /**
566  * Implementation of lu_device_operations::ldo_prepare.
567  *
568  * This method is called after layer has been initialized and before it starts
569  * serving user requests. In OFD it starts lfsk check routines and initializes
570  * recovery.
571  *
572  * \param[in] env       execution environment
573  * \param[in] pdev      higher device in stack, NULL for OFD
574  * \param[in] dev       lu_device of OFD device
575  *
576  * \retval              0 if successful
577  * \retval              negative value on error
578  */
579 static int ofd_prepare(const struct lu_env *env, struct lu_device *pdev,
580                        struct lu_device *dev)
581 {
582         struct ofd_thread_info          *info;
583         struct ofd_device               *ofd = ofd_dev(dev);
584         struct obd_device               *obd = ofd_obd(ofd);
585         struct lu_device                *next = &ofd->ofd_osd->dd_lu_dev;
586         int                              rc;
587
588         ENTRY;
589
590         info = ofd_info_init(env, NULL);
591         if (info == NULL)
592                 RETURN(-EFAULT);
593
594         /* initialize lower device */
595         rc = next->ld_ops->ldo_prepare(env, dev, next);
596         if (rc != 0)
597                 RETURN(rc);
598
599         rc = lfsck_register(env, ofd->ofd_osd, ofd->ofd_osd, obd,
600                             ofd_lfsck_out_notify, ofd, false);
601         if (rc != 0) {
602                 CERROR("%s: failed to initialize lfsck: rc = %d\n",
603                        obd->obd_name, rc);
604                 RETURN(rc);
605         }
606
607         rc = lfsck_register_namespace(env, ofd->ofd_osd, ofd->ofd_namespace);
608         /* The LFSCK instance is registered just now, so it must be there when
609          * register the namespace to such instance. */
610         LASSERTF(rc == 0, "register namespace failed: rc = %d\n", rc);
611
612         target_recovery_init(&ofd->ofd_lut, tgt_request_handle);
613         LASSERT(obd->obd_no_conn);
614         spin_lock(&obd->obd_dev_lock);
615         obd->obd_no_conn = 0;
616         spin_unlock(&obd->obd_dev_lock);
617
618         if (obd->obd_recovering == 0)
619                 ofd_postrecov(env, ofd);
620
621         RETURN(rc);
622 }
623
624 /**
625  * Implementation of lu_device_operations::ldo_recovery_complete.
626  *
627  * This method notifies all layers about 'recovery complete' event. That means
628  * device is in full state and consistent. An OFD calculates available grant
629  * space upon this event.
630  *
631  * \param[in] env       execution environment
632  * \param[in] dev       lu_device of OFD device
633  *
634  * \retval              0 if successful
635  * \retval              negative value on error
636  */
637 static int ofd_recovery_complete(const struct lu_env *env,
638                                  struct lu_device *dev)
639 {
640         struct ofd_thread_info  *oti = ofd_info(env);
641         struct ofd_device       *ofd = ofd_dev(dev);
642         struct lu_device        *next = &ofd->ofd_osd->dd_lu_dev;
643         int                      rc = 0;
644
645         ENTRY;
646
647         /*
648          * Grant space for object precreation on the self export.
649          * The initial reserved space (i.e. 10MB for zfs and 280KB for ldiskfs)
650          * is enough to create 10k objects. More space is then acquired for
651          * precreation in tgt_grant_create().
652          */
653         memset(&oti->fti_ocd, 0, sizeof(oti->fti_ocd));
654         oti->fti_ocd.ocd_grant = OST_MAX_PRECREATE / 2;
655         oti->fti_ocd.ocd_grant *= ofd->ofd_lut.lut_dt_conf.ddp_inodespace;
656         oti->fti_ocd.ocd_connect_flags = OBD_CONNECT_GRANT |
657                                          OBD_CONNECT_GRANT_PARAM;
658         tgt_grant_connect(env, dev->ld_obd->obd_self_export, &oti->fti_ocd,
659                           true);
660         rc = next->ld_ops->ldo_recovery_complete(env, next);
661         RETURN(rc);
662 }
663
664 /**
665  * lu_device_operations matrix for OFD device.
666  */
667 static struct lu_device_operations ofd_lu_ops = {
668         .ldo_object_alloc       = ofd_object_alloc,
669         .ldo_process_config     = ofd_process_config,
670         .ldo_recovery_complete  = ofd_recovery_complete,
671         .ldo_prepare            = ofd_prepare,
672 };
673
674 LPROC_SEQ_FOPS(lprocfs_nid_stats_clear);
675
676 /**
677  * Initialize all needed procfs entries for OFD device.
678  *
679  * \param[in] ofd       OFD device
680  *
681  * \retval              0 if successful
682  * \retval              negative value on error
683  */
684 static int ofd_procfs_init(struct ofd_device *ofd)
685 {
686         struct obd_device               *obd = ofd_obd(ofd);
687         struct proc_dir_entry           *entry;
688         int                              rc = 0;
689
690         ENTRY;
691
692         /* lprocfs must be setup before the ofd so state can be safely added
693          * to /proc incrementally as the ofd is setup */
694         obd->obd_vars = lprocfs_ofd_obd_vars;
695         rc = lprocfs_obd_setup(obd);
696         if (rc) {
697                 CERROR("%s: lprocfs_obd_setup failed: %d.\n",
698                        obd->obd_name, rc);
699                 RETURN(rc);
700         }
701
702         rc = lprocfs_alloc_obd_stats(obd, LPROC_OFD_STATS_LAST);
703         if (rc) {
704                 CERROR("%s: lprocfs_alloc_obd_stats failed: %d.\n",
705                        obd->obd_name, rc);
706                 GOTO(obd_cleanup, rc);
707         }
708
709         obd->obd_uses_nid_stats = 1;
710
711         entry = lprocfs_register("exports", obd->obd_proc_entry, NULL, NULL);
712         if (IS_ERR(entry)) {
713                 rc = PTR_ERR(entry);
714                 CERROR("%s: error %d setting up lprocfs for %s\n",
715                        obd->obd_name, rc, "exports");
716                 GOTO(obd_cleanup, rc);
717         }
718         obd->obd_proc_exports_entry = entry;
719
720         entry = lprocfs_add_simple(obd->obd_proc_exports_entry, "clear",
721                                    obd, &lprocfs_nid_stats_clear_fops);
722         if (IS_ERR(entry)) {
723                 rc = PTR_ERR(entry);
724                 CERROR("%s: add proc entry 'clear' failed: %d.\n",
725                        obd->obd_name, rc);
726                 GOTO(obd_cleanup, rc);
727         }
728
729         ofd_stats_counter_init(obd->obd_stats);
730
731         rc = lprocfs_job_stats_init(obd, LPROC_OFD_STATS_LAST,
732                                     ofd_stats_counter_init);
733         if (rc)
734                 GOTO(obd_cleanup, rc);
735         RETURN(0);
736 obd_cleanup:
737         lprocfs_obd_cleanup(obd);
738         lprocfs_free_obd_stats(obd);
739
740         return rc;
741 }
742
743 /**
744  * Expose OSD statistics to OFD layer.
745  *
746  * The osd interfaces to the backend file system exposes useful data
747  * such as brw_stats and read or write cache states. This same data
748  * needs to be exposed into the obdfilter (ofd) layer to maintain
749  * backwards compatibility. This function creates the symlinks in the
750  * proc layer to enable this.
751  *
752  * \param[in] ofd       OFD device
753  */
754 static void ofd_procfs_add_brw_stats_symlink(struct ofd_device *ofd)
755 {
756         struct obd_device       *obd = ofd_obd(ofd);
757         struct obd_device       *osd_obd = ofd->ofd_osd_exp->exp_obd;
758
759         if (obd->obd_proc_entry == NULL)
760                 return;
761
762         lprocfs_add_symlink("brw_stats", obd->obd_proc_entry,
763                             "../../%s/%s/brw_stats",
764                             osd_obd->obd_type->typ_name, obd->obd_name);
765
766         lprocfs_add_symlink("read_cache_enable", obd->obd_proc_entry,
767                             "../../%s/%s/read_cache_enable",
768                             osd_obd->obd_type->typ_name, obd->obd_name);
769
770         lprocfs_add_symlink("readcache_max_filesize",
771                             obd->obd_proc_entry,
772                             "../../%s/%s/readcache_max_filesize",
773                             osd_obd->obd_type->typ_name, obd->obd_name);
774
775         lprocfs_add_symlink("writethrough_cache_enable",
776                             obd->obd_proc_entry,
777                             "../../%s/%s/writethrough_cache_enable",
778                             osd_obd->obd_type->typ_name, obd->obd_name);
779 }
780
781 /**
782  * Cleanup all procfs entries in OFD.
783  *
784  * \param[in] ofd       OFD device
785  */
786 static void ofd_procfs_fini(struct ofd_device *ofd)
787 {
788         struct obd_device *obd = ofd_obd(ofd);
789
790         lprocfs_free_per_client_stats(obd);
791         lprocfs_obd_cleanup(obd);
792         lprocfs_free_obd_stats(obd);
793         lprocfs_job_stats_fini(obd);
794 }
795
796 /**
797  * Stop SEQ/FID server on OFD.
798  *
799  * \param[in] env       execution environment
800  * \param[in] ofd       OFD device
801  *
802  * \retval              0 if successful
803  * \retval              negative value on error
804  */
805 int ofd_fid_fini(const struct lu_env *env, struct ofd_device *ofd)
806 {
807         return seq_site_fini(env, &ofd->ofd_seq_site);
808 }
809
810 /**
811  * Start SEQ/FID server on OFD.
812  *
813  * The SEQ/FID server on OFD is needed to allocate FIDs for new objects.
814  * It also connects to the master server to get own FID sequence (SEQ) range
815  * to this particular OFD. Typically that happens when the OST is first
816  * formatted or in the rare case that it exhausts the local sequence range.
817  *
818  * The sequence range is allocated out to the MDTs for OST object allocations,
819  * and not directly to the clients.
820  *
821  * \param[in] env       execution environment
822  * \param[in] ofd       OFD device
823  *
824  * \retval              0 if successful
825  * \retval              negative value on error
826  */
827 int ofd_fid_init(const struct lu_env *env, struct ofd_device *ofd)
828 {
829         struct seq_server_site  *ss = &ofd->ofd_seq_site;
830         struct lu_device        *lu = &ofd->ofd_dt_dev.dd_lu_dev;
831         char                    *obd_name = ofd_name(ofd);
832         char                    *name = NULL;
833         int                     rc = 0;
834
835         ss = &ofd->ofd_seq_site;
836         lu->ld_site->ld_seq_site = ss;
837         ss->ss_lu = lu->ld_site;
838         ss->ss_node_id = ofd->ofd_lut.lut_lsd.lsd_osd_index;
839
840         OBD_ALLOC(name, sizeof(obd_name) * 2 + 10);
841         if (name == NULL)
842                 return -ENOMEM;
843
844         OBD_ALLOC_PTR(ss->ss_server_seq);
845         if (ss->ss_server_seq == NULL)
846                 GOTO(out_name, rc = -ENOMEM);
847
848         rc = seq_server_init(env, ss->ss_server_seq, ofd->ofd_osd, obd_name,
849                              LUSTRE_SEQ_SERVER, ss);
850         if (rc) {
851                 CERROR("%s : seq server init error %d\n", obd_name, rc);
852                 GOTO(out_server, rc);
853         }
854         ss->ss_server_seq->lss_space.lsr_index = ss->ss_node_id;
855
856         OBD_ALLOC_PTR(ss->ss_client_seq);
857         if (ss->ss_client_seq == NULL)
858                 GOTO(out_server, rc = -ENOMEM);
859
860         /*
861          * It always printed as "%p", so that the name is unique in the kernel,
862          * even if the filesystem is mounted twice. So sizeof(.) * 2 is enough.
863          */
864         snprintf(name, sizeof(obd_name) * 2 + 7, "%p-super", obd_name);
865         rc = seq_client_init(ss->ss_client_seq, NULL, LUSTRE_SEQ_DATA,
866                              name, NULL);
867         if (rc) {
868                 CERROR("%s : seq client init error %d\n", obd_name, rc);
869                 GOTO(out_client, rc);
870         }
871
872         rc = seq_server_set_cli(env, ss->ss_server_seq, ss->ss_client_seq);
873
874         if (rc) {
875 out_client:
876                 seq_client_fini(ss->ss_client_seq);
877                 OBD_FREE_PTR(ss->ss_client_seq);
878                 ss->ss_client_seq = NULL;
879 out_server:
880                 seq_server_fini(ss->ss_server_seq, env);
881                 OBD_FREE_PTR(ss->ss_server_seq);
882                 ss->ss_server_seq = NULL;
883         }
884 out_name:
885         OBD_FREE(name, sizeof(obd_name) * 2 + 10);
886
887         return rc;
888 }
889
890 /**
891  * OFD request handler for OST_SET_INFO RPC.
892  *
893  * This is OFD-specific part of request handling
894  *
895  * \param[in] tsi       target session environment for this request
896  *
897  * \retval              0 if successful
898  * \retval              negative value on error
899  */
900 static int ofd_set_info_hdl(struct tgt_session_info *tsi)
901 {
902         struct ptlrpc_request   *req = tgt_ses_req(tsi);
903         struct ost_body         *body = NULL, *repbody;
904         void                    *key, *val = NULL;
905         int                      keylen, vallen, rc = 0;
906         bool                     is_grant_shrink;
907
908         ENTRY;
909
910         key = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_KEY);
911         if (key == NULL) {
912                 DEBUG_REQ(D_HA, req, "no set_info key");
913                 RETURN(err_serious(-EFAULT));
914         }
915         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_KEY,
916                                       RCL_CLIENT);
917
918         val = req_capsule_client_get(tsi->tsi_pill, &RMF_SETINFO_VAL);
919         if (val == NULL) {
920                 DEBUG_REQ(D_HA, req, "no set_info val");
921                 RETURN(err_serious(-EFAULT));
922         }
923         vallen = req_capsule_get_size(tsi->tsi_pill, &RMF_SETINFO_VAL,
924                                       RCL_CLIENT);
925
926         is_grant_shrink = KEY_IS(KEY_GRANT_SHRINK);
927         if (is_grant_shrink)
928                 /* In this case the value is actually an RMF_OST_BODY, so we
929                  * transmutate the type of this PTLRPC */
930                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_SET_GRANT_INFO);
931
932         rc = req_capsule_server_pack(tsi->tsi_pill);
933         if (rc < 0)
934                 RETURN(rc);
935
936         if (is_grant_shrink) {
937                 body = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_BODY);
938
939                 repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
940                 *repbody = *body;
941
942                 /** handle grant shrink, similar to a read request */
943                 tgt_grant_prepare_read(tsi->tsi_env, tsi->tsi_exp,
944                                        &repbody->oa);
945         } else if (KEY_IS(KEY_EVICT_BY_NID)) {
946                 if (vallen > 0)
947                         obd_export_evict_by_nid(tsi->tsi_exp->exp_obd, val);
948                 rc = 0;
949         } else {
950                 CERROR("%s: Unsupported key %s\n",
951                        tgt_name(tsi->tsi_tgt), (char *)key);
952                 rc = -EOPNOTSUPP;
953         }
954         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SET_INFO,
955                          tsi->tsi_jobid, 1);
956
957         RETURN(rc);
958 }
959
960 /**
961  * Get FIEMAP (FIle Extent MAPping) for object with the given FID.
962  *
963  * This function returns a list of extents which describes how a file's
964  * blocks are laid out on the disk.
965  *
966  * \param[in] env       execution environment
967  * \param[in] ofd       OFD device
968  * \param[in] fid       FID of object
969  * \param[in] fiemap    fiemap structure to fill with data
970  *
971  * \retval              0 if \a fiemap is filled with data successfully
972  * \retval              negative value on error
973  */
974 int ofd_fiemap_get(const struct lu_env *env, struct ofd_device *ofd,
975                    struct lu_fid *fid, struct fiemap *fiemap)
976 {
977         struct ofd_object       *fo;
978         int                      rc;
979
980         fo = ofd_object_find(env, ofd, fid);
981         if (IS_ERR(fo)) {
982                 CERROR("%s: error finding object "DFID"\n",
983                        ofd_name(ofd), PFID(fid));
984                 return PTR_ERR(fo);
985         }
986
987         ofd_read_lock(env, fo);
988         if (ofd_object_exists(fo))
989                 rc = dt_fiemap_get(env, ofd_object_child(fo), fiemap);
990         else
991                 rc = -ENOENT;
992         ofd_read_unlock(env, fo);
993         ofd_object_put(env, fo);
994         return rc;
995 }
996
997 struct locked_region {
998         struct list_head        list;
999         struct lustre_handle    lh;
1000 };
1001
1002 /**
1003  * Lock single extent and save lock handle in the list.
1004  *
1005  * This is supplemental function for lock_zero_regions(). It allocates
1006  * new locked_region structure and locks it with extent lock, then adds
1007  * it to the list of all such regions.
1008  *
1009  * \param[in] ns        LDLM namespace
1010  * \param[in] res_id    resource ID
1011  * \param[in] begin     start of region
1012  * \param[in] end       end of region
1013  * \param[in] locked    list head of regions list
1014  *
1015  * \retval              0 if successful locking
1016  * \retval              negative value on error
1017  */
1018 static int lock_region(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
1019                        unsigned long long begin, unsigned long long end,
1020                        struct list_head *locked)
1021 {
1022         struct locked_region    *region = NULL;
1023         __u64                    flags = 0;
1024         int                      rc;
1025
1026         LASSERT(begin <= end);
1027         OBD_ALLOC_PTR(region);
1028         if (region == NULL)
1029                 return -ENOMEM;
1030
1031         rc = tgt_extent_lock(ns, res_id, begin, end, &region->lh,
1032                              LCK_PR, &flags);
1033         if (rc != 0)
1034                 return rc;
1035
1036         CDEBUG(D_OTHER, "ost lock [%llu,%llu], lh=%p\n", begin, end,
1037                &region->lh);
1038         list_add(&region->list, locked);
1039
1040         return 0;
1041 }
1042
1043 /**
1044  * Lock the sparse areas of given resource.
1045  *
1046  * The locking of sparse areas will cause dirty data to be flushed back from
1047  * clients. This is used when getting the FIEMAP of an object to make sure
1048  * there is no unaccounted cached data on clients.
1049  *
1050  * This function goes through \a fiemap list of extents and locks only sparse
1051  * areas between extents.
1052  *
1053  * \param[in] ns        LDLM namespace
1054  * \param[in] res_id    resource ID
1055  * \param[in] fiemap    file extents mapping on disk
1056  * \param[in] locked    list head of regions list
1057  *
1058  * \retval              0 if successful
1059  * \retval              negative value on error
1060  */
1061 static int lock_zero_regions(struct ldlm_namespace *ns,
1062                              struct ldlm_res_id *res_id,
1063                              struct fiemap *fiemap,
1064                              struct list_head *locked)
1065 {
1066         __u64 begin = fiemap->fm_start;
1067         unsigned int i;
1068         int rc = 0;
1069         struct fiemap_extent *fiemap_start = fiemap->fm_extents;
1070
1071         ENTRY;
1072
1073         CDEBUG(D_OTHER, "extents count %u\n", fiemap->fm_mapped_extents);
1074         for (i = 0; i < fiemap->fm_mapped_extents; i++) {
1075                 if (fiemap_start[i].fe_logical > begin) {
1076                         CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
1077                                begin, fiemap_start[i].fe_logical);
1078                         rc = lock_region(ns, res_id, begin,
1079                                          fiemap_start[i].fe_logical, locked);
1080                         if (rc)
1081                                 RETURN(rc);
1082                 }
1083
1084                 begin = fiemap_start[i].fe_logical + fiemap_start[i].fe_length;
1085         }
1086
1087         if (begin < (fiemap->fm_start + fiemap->fm_length)) {
1088                 CDEBUG(D_OTHER, "ost lock [%llu,%llu]\n",
1089                        begin, fiemap->fm_start + fiemap->fm_length);
1090                 rc = lock_region(ns, res_id, begin,
1091                                  fiemap->fm_start + fiemap->fm_length, locked);
1092         }
1093
1094         RETURN(rc);
1095 }
1096
1097 /**
1098  * Unlock all previously locked sparse areas for given resource.
1099  *
1100  * This function goes through list of locked regions, unlocking and freeing
1101  * them one-by-one.
1102  *
1103  * \param[in] ns        LDLM namespace
1104  * \param[in] locked    list head of regions list
1105  */
1106 static void
1107 unlock_zero_regions(struct ldlm_namespace *ns, struct list_head *locked)
1108 {
1109         struct locked_region *entry, *temp;
1110
1111         list_for_each_entry_safe(entry, temp, locked, list) {
1112                 CDEBUG(D_OTHER, "ost unlock lh=%p\n", &entry->lh);
1113                 tgt_extent_unlock(&entry->lh, LCK_PR);
1114                 list_del(&entry->list);
1115                 OBD_FREE_PTR(entry);
1116         }
1117 }
1118
1119 /**
1120  * OFD request handler for OST_GET_INFO RPC.
1121  *
1122  * This is OFD-specific part of request handling. The OFD-specific keys are:
1123  * - KEY_LAST_ID (obsolete)
1124  * - KEY_FIEMAP
1125  * - KEY_LAST_FID
1126  *
1127  * This function reads needed data from storage and fills reply with it.
1128  *
1129  * Note: the KEY_LAST_ID is obsolete, replaced by KEY_LAST_FID on newer MDTs,
1130  * and is kept for compatibility.
1131  *
1132  * \param[in] tsi       target session environment for this request
1133  *
1134  * \retval              0 if successful
1135  * \retval              negative value on error
1136  */
1137 static int ofd_get_info_hdl(struct tgt_session_info *tsi)
1138 {
1139         struct obd_export               *exp = tsi->tsi_exp;
1140         struct ofd_device               *ofd = ofd_exp(exp);
1141         struct ofd_thread_info          *fti = tsi2ofd_info(tsi);
1142         void                            *key;
1143         int                              keylen;
1144         int                              replylen, rc = 0;
1145
1146         ENTRY;
1147
1148         /* this common part for get_info rpc */
1149         key = req_capsule_client_get(tsi->tsi_pill, &RMF_GETINFO_KEY);
1150         if (key == NULL) {
1151                 DEBUG_REQ(D_HA, tgt_ses_req(tsi), "no get_info key");
1152                 RETURN(err_serious(-EPROTO));
1153         }
1154         keylen = req_capsule_get_size(tsi->tsi_pill, &RMF_GETINFO_KEY,
1155                                       RCL_CLIENT);
1156
1157         if (KEY_IS(KEY_LAST_ID)) {
1158                 u64             *last_id;
1159                 struct ofd_seq  *oseq;
1160
1161                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_ID);
1162                 rc = req_capsule_server_pack(tsi->tsi_pill);
1163                 if (rc)
1164                         RETURN(err_serious(rc));
1165
1166                 last_id = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_ID);
1167
1168                 oseq = ofd_seq_load(tsi->tsi_env, ofd,
1169                                     (u64)exp->exp_filter_data.fed_group);
1170                 if (IS_ERR(oseq))
1171                         rc = -EFAULT;
1172                 else
1173                         *last_id = ofd_seq_last_oid(oseq);
1174                 ofd_seq_put(tsi->tsi_env, oseq);
1175         } else if (KEY_IS(KEY_FIEMAP)) {
1176                 struct ll_fiemap_info_key       *fm_key;
1177                 struct fiemap                   *fiemap;
1178                 struct lu_fid                   *fid;
1179
1180                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_FIEMAP);
1181
1182                 fm_key = req_capsule_client_get(tsi->tsi_pill, &RMF_FIEMAP_KEY);
1183                 rc = tgt_validate_obdo(tsi, &fm_key->lfik_oa);
1184                 if (rc)
1185                         RETURN(err_serious(rc));
1186
1187                 fid = &fm_key->lfik_oa.o_oi.oi_fid;
1188
1189                 CDEBUG(D_INODE, "get FIEMAP of object "DFID"\n", PFID(fid));
1190
1191                 replylen = fiemap_count_to_size(
1192                                         fm_key->lfik_fiemap.fm_extent_count);
1193                 req_capsule_set_size(tsi->tsi_pill, &RMF_FIEMAP_VAL,
1194                                      RCL_SERVER, replylen);
1195
1196                 rc = req_capsule_server_pack(tsi->tsi_pill);
1197                 if (rc)
1198                         RETURN(err_serious(rc));
1199
1200                 fiemap = req_capsule_server_get(tsi->tsi_pill, &RMF_FIEMAP_VAL);
1201                 if (fiemap == NULL)
1202                         RETURN(-ENOMEM);
1203
1204                 *fiemap = fm_key->lfik_fiemap;
1205                 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid, fiemap);
1206
1207                 /* LU-3219: Lock the sparse areas to make sure dirty
1208                  * flushed back from client, then call fiemap again. */
1209                 if (fm_key->lfik_oa.o_valid & OBD_MD_FLFLAGS &&
1210                     fm_key->lfik_oa.o_flags & OBD_FL_SRVLOCK) {
1211                         struct list_head locked;
1212
1213                         INIT_LIST_HEAD(&locked);
1214                         ost_fid_build_resid(fid, &fti->fti_resid);
1215                         rc = lock_zero_regions(ofd->ofd_namespace,
1216                                                &fti->fti_resid, fiemap,
1217                                                &locked);
1218                         if (rc == 0 && !list_empty(&locked)) {
1219                                 rc = ofd_fiemap_get(tsi->tsi_env, ofd, fid,
1220                                                     fiemap);
1221                                 unlock_zero_regions(ofd->ofd_namespace,
1222                                                     &locked);
1223                         }
1224                 }
1225         } else if (KEY_IS(KEY_LAST_FID)) {
1226                 struct ofd_device       *ofd = ofd_exp(exp);
1227                 struct ofd_seq          *oseq;
1228                 struct lu_fid           *fid;
1229                 int                      rc;
1230
1231                 req_capsule_extend(tsi->tsi_pill, &RQF_OST_GET_INFO_LAST_FID);
1232                 rc = req_capsule_server_pack(tsi->tsi_pill);
1233                 if (rc)
1234                         RETURN(err_serious(rc));
1235
1236                 fid = req_capsule_client_get(tsi->tsi_pill, &RMF_FID);
1237                 if (fid == NULL)
1238                         RETURN(err_serious(-EPROTO));
1239
1240                 fid_le_to_cpu(&fti->fti_ostid.oi_fid, fid);
1241
1242                 fid = req_capsule_server_get(tsi->tsi_pill, &RMF_FID);
1243                 if (fid == NULL)
1244                         RETURN(-ENOMEM);
1245
1246                 oseq = ofd_seq_load(tsi->tsi_env, ofd,
1247                                     ostid_seq(&fti->fti_ostid));
1248                 if (IS_ERR(oseq))
1249                         RETURN(PTR_ERR(oseq));
1250
1251                 rc = ostid_to_fid(fid, &oseq->os_oi,
1252                                   ofd->ofd_lut.lut_lsd.lsd_osd_index);
1253                 if (rc != 0)
1254                         GOTO(out_put, rc);
1255
1256                 CDEBUG(D_HA, "%s: LAST FID is "DFID"\n", ofd_name(ofd),
1257                        PFID(fid));
1258 out_put:
1259                 ofd_seq_put(tsi->tsi_env, oseq);
1260         } else {
1261                 CERROR("%s: not supported key %s\n", tgt_name(tsi->tsi_tgt),
1262                        (char *)key);
1263                 rc = -EOPNOTSUPP;
1264         }
1265         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GET_INFO,
1266                          tsi->tsi_jobid, 1);
1267
1268         RETURN(rc);
1269 }
1270
1271 /**
1272  * OFD request handler for OST_GETATTR RPC.
1273  *
1274  * This is OFD-specific part of request handling. It finds the OFD object
1275  * by its FID, gets attributes from storage and packs result to the reply.
1276  *
1277  * \param[in] tsi       target session environment for this request
1278  *
1279  * \retval              0 if successful
1280  * \retval              negative value on error
1281  */
1282 static int ofd_getattr_hdl(struct tgt_session_info *tsi)
1283 {
1284         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1285         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1286         struct ost_body         *repbody;
1287         struct lustre_handle     lh = { 0 };
1288         struct ofd_object       *fo;
1289         __u64                    flags = 0;
1290         enum ldlm_mode           lock_mode = LCK_PR;
1291         bool                     srvlock;
1292         int                      rc;
1293         ENTRY;
1294
1295         LASSERT(tsi->tsi_ost_body != NULL);
1296
1297         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1298         if (repbody == NULL)
1299                 RETURN(-ENOMEM);
1300
1301         repbody->oa.o_oi = tsi->tsi_ost_body->oa.o_oi;
1302         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1303
1304         srvlock = tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
1305                   tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK;
1306
1307         if (srvlock) {
1308                 if (unlikely(tsi->tsi_ost_body->oa.o_flags & OBD_FL_FLUSH))
1309                         lock_mode = LCK_PW;
1310
1311                 rc = tgt_extent_lock(tsi->tsi_tgt->lut_obd->obd_namespace,
1312                                      &tsi->tsi_resid, 0, OBD_OBJECT_EOF, &lh,
1313                                      lock_mode, &flags);
1314                 if (rc != 0)
1315                         RETURN(rc);
1316         }
1317
1318         fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1319         if (IS_ERR(fo))
1320                 GOTO(out, rc = PTR_ERR(fo));
1321
1322         rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1323         if (rc == 0) {
1324                 __u64    curr_version;
1325
1326                 obdo_from_la(&repbody->oa, &fti->fti_attr,
1327                              OFD_VALID_FLAGS | LA_UID | LA_GID | LA_PROJID);
1328
1329                 /* Store object version in reply */
1330                 curr_version = dt_version_get(tsi->tsi_env,
1331                                               ofd_object_child(fo));
1332                 if ((__s64)curr_version != -EOPNOTSUPP) {
1333                         repbody->oa.o_valid |= OBD_MD_FLDATAVERSION;
1334                         repbody->oa.o_data_version = curr_version;
1335                 }
1336         }
1337
1338         ofd_object_put(tsi->tsi_env, fo);
1339 out:
1340         if (srvlock)
1341                 tgt_extent_unlock(&lh, lock_mode);
1342
1343         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_GETATTR,
1344                          tsi->tsi_jobid, 1);
1345
1346         repbody->oa.o_valid |= OBD_MD_FLFLAGS;
1347         repbody->oa.o_flags = OBD_FL_FLUSH;
1348
1349         RETURN(rc);
1350 }
1351
1352 /**
1353  * OFD request handler for OST_SETATTR RPC.
1354  *
1355  * This is OFD-specific part of request handling. It finds the OFD object
1356  * by its FID, sets attributes from request and packs result to the reply.
1357  *
1358  * \param[in] tsi       target session environment for this request
1359  *
1360  * \retval              0 if successful
1361  * \retval              negative value on error
1362  */
1363 static int ofd_setattr_hdl(struct tgt_session_info *tsi)
1364 {
1365         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1366         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1367         struct ost_body         *body = tsi->tsi_ost_body;
1368         struct ost_body         *repbody;
1369         struct ldlm_resource    *res;
1370         struct ofd_object       *fo;
1371         struct filter_fid       *ff = NULL;
1372         int                      rc = 0;
1373
1374         ENTRY;
1375
1376         LASSERT(body != NULL);
1377
1378         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1379         if (repbody == NULL)
1380                 RETURN(-ENOMEM);
1381
1382         repbody->oa.o_oi = body->oa.o_oi;
1383         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1384
1385         /* This would be very bad - accidentally truncating a file when
1386          * changing the time or similar - bug 12203. */
1387         if (body->oa.o_valid & OBD_MD_FLSIZE &&
1388             body->oa.o_size != OBD_OBJECT_EOF) {
1389                 static char mdsinum[48];
1390
1391                 if (body->oa.o_valid & OBD_MD_FLFID)
1392                         snprintf(mdsinum, sizeof(mdsinum) - 1,
1393                                  "of parent "DFID, body->oa.o_parent_seq,
1394                                  body->oa.o_parent_oid, 0);
1395                 else
1396                         mdsinum[0] = '\0';
1397
1398                 CERROR("%s: setattr from %s is trying to truncate object "DFID
1399                        " %s\n", ofd_name(ofd), obd_export_nid2str(tsi->tsi_exp),
1400                        PFID(&tsi->tsi_fid), mdsinum);
1401                 RETURN(-EPERM);
1402         }
1403
1404         fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1405         if (IS_ERR(fo))
1406                 GOTO(out, rc = PTR_ERR(fo));
1407
1408         la_from_obdo(&fti->fti_attr, &body->oa, body->oa.o_valid);
1409         fti->fti_attr.la_valid &= ~LA_TYPE;
1410
1411         if (body->oa.o_valid & OBD_MD_FLFID) {
1412                 ff = &fti->fti_mds_fid;
1413                 ofd_prepare_fidea(ff, &body->oa);
1414         }
1415
1416         /* setting objects attributes (including owner/group) */
1417         rc = ofd_attr_set(tsi->tsi_env, fo, &fti->fti_attr, ff);
1418         if (rc != 0)
1419                 GOTO(out_put, rc);
1420
1421         obdo_from_la(&repbody->oa, &fti->fti_attr,
1422                      OFD_VALID_FLAGS | LA_UID | LA_GID | LA_PROJID);
1423
1424         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SETATTR,
1425                          tsi->tsi_jobid, 1);
1426         EXIT;
1427 out_put:
1428         ofd_object_put(tsi->tsi_env, fo);
1429 out:
1430         if (rc == 0) {
1431                 /* we do not call this before to avoid lu_object_find() in
1432                  *  ->lvbo_update() holding another reference on the object.
1433                  * otherwise concurrent destroy can make the object unavailable
1434                  * for 2nd lu_object_find() waiting for the first reference
1435                  * to go... deadlock! */
1436                 res = ldlm_resource_get(ofd->ofd_namespace, NULL,
1437                                         &tsi->tsi_resid, LDLM_EXTENT, 0);
1438                 if (!IS_ERR(res)) {
1439                         ldlm_res_lvbo_update(res, NULL, 0);
1440                         ldlm_resource_putref(res);
1441                 }
1442         }
1443         return rc;
1444 }
1445
1446 /**
1447  * Destroy OST orphans.
1448  *
1449  * This is part of OST_CREATE RPC handling. If there is flag OBD_FL_DELORPHAN
1450  * set then we must destroy possible orphaned objects.
1451  *
1452  * \param[in] env       execution environment
1453  * \param[in] exp       OBD export
1454  * \param[in] ofd       OFD device
1455  * \param[in] oa        obdo structure for reply
1456  *
1457  * \retval              0 if successful
1458  * \retval              negative value on error
1459  */
1460 static int ofd_orphans_destroy(const struct lu_env *env,
1461                                struct obd_export *exp,
1462                                struct ofd_device *ofd, struct obdo *oa)
1463 {
1464         struct ofd_thread_info  *info   = ofd_info(env);
1465         struct lu_fid           *fid    = &info->fti_fid;
1466         struct ost_id           *oi     = &oa->o_oi;
1467         struct ofd_seq          *oseq;
1468         u64                      seq    = ostid_seq(oi);
1469         u64                      end_id = ostid_id(oi);
1470         u64                      last;
1471         u64                      oid;
1472         int                      skip_orphan;
1473         int                      rc     = 0;
1474
1475         ENTRY;
1476
1477         oseq = ofd_seq_get(ofd, seq);
1478         if (oseq == NULL) {
1479                 CERROR("%s: Can not find seq for "DOSTID"\n",
1480                        ofd_name(ofd), POSTID(oi));
1481                 RETURN(-EINVAL);
1482         }
1483
1484         *fid = oi->oi_fid;
1485         last = ofd_seq_last_oid(oseq);
1486         oid = last;
1487
1488         LASSERT(exp != NULL);
1489         skip_orphan = !!(exp_connect_flags(exp) & OBD_CONNECT_SKIP_ORPHAN);
1490
1491         if (OBD_FAIL_CHECK(OBD_FAIL_OST_NODESTROY))
1492                 goto done;
1493
1494         LCONSOLE(D_INFO, "%s: deleting orphan objects from "DOSTID
1495                  " to "DOSTID"\n", ofd_name(ofd), seq, end_id + 1, seq, last);
1496
1497         while (oid > end_id) {
1498                 rc = fid_set_id(fid, oid);
1499                 if (unlikely(rc != 0))
1500                         GOTO(out_put, rc);
1501
1502                 rc = ofd_destroy_by_fid(env, ofd, fid, 1);
1503                 if (rc != 0 && rc != -ENOENT && rc != -ESTALE &&
1504                     likely(rc != -EREMCHG && rc != -EINPROGRESS))
1505                         /* this is pretty fatal... */
1506                         CEMERG("%s: error destroying precreated id "
1507                                DFID": rc = %d\n",
1508                                ofd_name(ofd), PFID(fid), rc);
1509
1510                 oid--;
1511                 if (!skip_orphan) {
1512                         ofd_seq_last_oid_set(oseq, oid);
1513                         /* update last_id on disk periodically so that if we
1514                          * restart * we don't need to re-scan all of the just
1515                          * deleted objects. */
1516                         if ((oid & 511) == 0)
1517                                 ofd_seq_last_oid_write(env, ofd, oseq);
1518                 }
1519         }
1520
1521         CDEBUG(D_HA, "%s: after destroy: set last_id to "DOSTID"\n",
1522                ofd_name(ofd), seq, oid);
1523
1524 done:
1525         if (!skip_orphan) {
1526                 ofd_seq_last_oid_set(oseq, oid);
1527                 rc = ofd_seq_last_oid_write(env, ofd, oseq);
1528         } else {
1529                 /* don't reuse orphan object, return last used objid */
1530                 rc = ostid_set_id(oi, last);
1531         }
1532
1533         GOTO(out_put, rc);
1534
1535 out_put:
1536         ofd_seq_put(env, oseq);
1537         return rc;
1538 }
1539
1540 /**
1541  * OFD request handler for OST_CREATE RPC.
1542  *
1543  * This is OFD-specific part of request handling. Its main purpose is to
1544  * create new data objects on OST, but it also used to destroy orphans.
1545  *
1546  * \param[in] tsi       target session environment for this request
1547  *
1548  * \retval              0 if successful
1549  * \retval              negative value on error
1550  */
1551 static int ofd_create_hdl(struct tgt_session_info *tsi)
1552 {
1553         struct ptlrpc_request   *req = tgt_ses_req(tsi);
1554         struct ost_body         *repbody;
1555         const struct obdo       *oa = &tsi->tsi_ost_body->oa;
1556         struct obdo             *rep_oa;
1557         struct obd_export       *exp = tsi->tsi_exp;
1558         struct ofd_device       *ofd = ofd_exp(exp);
1559         u64                      seq = ostid_seq(&oa->o_oi);
1560         u64                      oid = ostid_id(&oa->o_oi);
1561         struct ofd_seq          *oseq;
1562         s64 diff;
1563         int rc = 0;
1564         int                      sync_trans = 0;
1565         long                     granted = 0;
1566
1567         ENTRY;
1568
1569         if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1570                 RETURN(-EROFS);
1571
1572         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1573         if (repbody == NULL)
1574                 RETURN(-ENOMEM);
1575
1576         down_read(&ofd->ofd_lastid_rwsem);
1577         /* Currently, for safe, we do not distinguish which LAST_ID is broken,
1578          * we may do that in the future.
1579          * Return -ENOSPC until the LAST_ID rebuilt. */
1580         if (unlikely(ofd->ofd_lastid_rebuilding))
1581                 GOTO(out_sem, rc = -ENOSPC);
1582
1583         rep_oa = &repbody->oa;
1584         rep_oa->o_oi = oa->o_oi;
1585
1586         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
1587
1588         CDEBUG(D_INFO, "ofd_create("DOSTID")\n", POSTID(&oa->o_oi));
1589
1590         oseq = ofd_seq_load(tsi->tsi_env, ofd, seq);
1591         if (IS_ERR(oseq)) {
1592                 CERROR("%s: Can't find FID Sequence %#llx: rc = %ld\n",
1593                        ofd_name(ofd), seq, PTR_ERR(oseq));
1594                 GOTO(out_sem, rc = -EINVAL);
1595         }
1596
1597         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1598             (oa->o_flags & OBD_FL_RECREATE_OBJS)) {
1599                 if (!ofd_obd(ofd)->obd_recovering ||
1600                     oid > ofd_seq_last_oid(oseq)) {
1601                         CERROR("%s: recreate objid "DOSTID" > last id %llu"
1602                                "\n", ofd_name(ofd), POSTID(&oa->o_oi),
1603                                ofd_seq_last_oid(oseq));
1604                         GOTO(out_nolock, rc = -EINVAL);
1605                 }
1606                 /* Do nothing here, we re-create objects during recovery
1607                  * upon write replay, see ofd_preprw_write() */
1608                 GOTO(out_nolock, rc = 0);
1609         }
1610         /* former ofd_handle_precreate */
1611         if ((oa->o_valid & OBD_MD_FLFLAGS) &&
1612             (oa->o_flags & OBD_FL_DELORPHAN)) {
1613                 exp->exp_filter_data.fed_lastid_gen = ofd->ofd_lastid_gen;
1614
1615                 /* destroy orphans */
1616                 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1617                     exp->exp_conn_cnt) {
1618                         CERROR("%s: dropping old orphan cleanup request\n",
1619                                ofd_name(ofd));
1620                         GOTO(out_nolock, rc = 0);
1621                 }
1622                 /* This causes inflight precreates to abort and drop lock */
1623                 oseq->os_destroys_in_progress = 1;
1624                 mutex_lock(&oseq->os_create_lock);
1625                 if (!oseq->os_destroys_in_progress) {
1626                         CDEBUG(D_HA,
1627                                "%s:[%llu] destroys_in_progress already cleared\n",
1628                                ofd_name(ofd), seq);
1629                         rc = ostid_set_id(&rep_oa->o_oi,
1630                                           ofd_seq_last_oid(oseq));
1631                         GOTO(out, rc);
1632                 }
1633                 diff = oid - ofd_seq_last_oid(oseq);
1634                 CDEBUG(D_HA, "ofd_last_id() = %llu -> diff = %lld\n",
1635                        ofd_seq_last_oid(oseq), diff);
1636                 if (-diff > OST_MAX_PRECREATE) {
1637                         LCONSOLE(D_INFO, "%s: too large difference between MDS "
1638                                  "LAST_ID "DFID" (%llu) and OST LAST_ID "DFID" "
1639                                  "(%llu), trust the OST\n",
1640                                  ofd_name(ofd), PFID(&oa->o_oi.oi_fid), oid,
1641                                  PFID(&oseq->os_oi.oi_fid),
1642                                  ofd_seq_last_oid(oseq));
1643
1644                         /* Let MDS know that we are so far ahead. */
1645                         rc = ostid_set_id(&rep_oa->o_oi,
1646                                           ofd_seq_last_oid(oseq) + 1);
1647                 } else if (diff < 0) {
1648                         rc = ofd_orphans_destroy(tsi->tsi_env, exp,
1649                                                  ofd, rep_oa);
1650                         oseq->os_destroys_in_progress = 0;
1651                 } else {
1652                         /* XXX: Used by MDS for the first time! */
1653                         oseq->os_destroys_in_progress = 0;
1654                 }
1655         } else {
1656                 if (unlikely(exp->exp_filter_data.fed_lastid_gen !=
1657                              ofd->ofd_lastid_gen)) {
1658                         /* Keep the export ref so we can send the reply. */
1659                         ofd_obd_disconnect(class_export_get(exp));
1660                         GOTO(out_nolock, rc = -ENOTCONN);
1661                 }
1662
1663                 mutex_lock(&oseq->os_create_lock);
1664                 if (lustre_msg_get_conn_cnt(tgt_ses_req(tsi)->rq_reqmsg) <
1665                     exp->exp_conn_cnt) {
1666                         CERROR("%s: dropping old precreate request\n",
1667                                ofd_name(ofd));
1668                         GOTO(out, rc = 0);
1669                 }
1670                 /* only precreate if seq is 0, IDIF or normal and also o_id
1671                  * must be specfied */
1672                 if ((!fid_seq_is_mdt(seq) && !fid_seq_is_norm(seq) &&
1673                      !fid_seq_is_idif(seq)) || oid == 0) {
1674                         diff = 1; /* shouldn't we create this right now? */
1675                 } else {
1676                         diff = oid - ofd_seq_last_oid(oseq);
1677                         /* Do sync create if the seq is about to used up */
1678                         if (fid_seq_is_idif(seq) || fid_seq_is_mdt0(seq)) {
1679                                 if (unlikely(oid >= IDIF_MAX_OID - 1))
1680                                         sync_trans = 1;
1681                         } else if (fid_seq_is_norm(seq)) {
1682                                 if (unlikely(oid >=
1683                                              LUSTRE_DATA_SEQ_MAX_WIDTH - 1))
1684                                         sync_trans = 1;
1685                         } else {
1686                                 CERROR("%s : invalid o_seq "DOSTID"\n",
1687                                        ofd_name(ofd), POSTID(&oa->o_oi));
1688                                 GOTO(out, rc = -EINVAL);
1689                         }
1690
1691                         if (diff < 0) {
1692                                 /* LU-5648 */
1693                                 CERROR("%s: invalid precreate request for "
1694                                        DOSTID", last_id %llu. "
1695                                        "Likely MDS last_id corruption\n",
1696                                        ofd_name(ofd), POSTID(&oa->o_oi),
1697                                        ofd_seq_last_oid(oseq));
1698                                 GOTO(out, rc = -EINVAL);
1699                         }
1700                 }
1701         }
1702         if (diff > 0) {
1703                 cfs_time_t       enough_time = cfs_time_shift(DISK_TIMEOUT);
1704                 u64              next_id;
1705                 int              created = 0;
1706                 int              count;
1707                 int              rc2;
1708
1709                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1710                     !(oa->o_flags & OBD_FL_DELORPHAN)) {
1711                         /* don't enforce grant during orphan recovery */
1712                         granted = tgt_grant_create(tsi->tsi_env,
1713                                                 ofd_obd(ofd)->obd_self_export,
1714                                                 &diff);
1715                         if (granted < 0) {
1716                                 rc = granted;
1717                                 granted = 0;
1718                                 CDEBUG(D_HA, "%s: failed to acquire grant "
1719                                        "space for precreate (%lld): rc = %d\n",
1720                                        ofd_name(ofd), diff, rc);
1721                                 diff = 0;
1722                         }
1723                 }
1724
1725                 /* This can happen if a new OST is formatted and installed
1726                  * in place of an old one at the same index.  Instead of
1727                  * precreating potentially millions of deleted old objects
1728                  * (possibly filling the OST), only precreate the last batch.
1729                  * LFSCK will eventually clean up any orphans. LU-14 */
1730                 if (diff > 5 * OST_MAX_PRECREATE) {
1731                         diff = OST_MAX_PRECREATE / 2;
1732                         LCONSOLE_WARN("%s: Too many FIDs to precreate "
1733                                       "OST replaced or reformatted: "
1734                                       "LFSCK will clean up",
1735                                       ofd_name(ofd));
1736
1737                         CDEBUG(D_HA, "%s: precreate FID "DOSTID" is over "
1738                                "%u larger than the LAST_ID "DOSTID", only "
1739                                "precreating the last %lld objects.\n",
1740                                ofd_name(ofd), POSTID(&oa->o_oi),
1741                                5 * OST_MAX_PRECREATE,
1742                                POSTID(&oseq->os_oi), diff);
1743                         ofd_seq_last_oid_set(oseq, ostid_id(&oa->o_oi) - diff);
1744                 }
1745
1746                 while (diff > 0) {
1747                         next_id = ofd_seq_last_oid(oseq) + 1;
1748                         count = ofd_precreate_batch(ofd, (int)diff);
1749
1750                         CDEBUG(D_HA, "%s: reserve %d objects in group %#llx"
1751                                " at %llu\n", ofd_name(ofd),
1752                                count, seq, next_id);
1753
1754                         if (!(lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1755                             && cfs_time_after(jiffies, enough_time)) {
1756                                 CDEBUG(D_HA, "%s: Slow creates, %d/%lld objects"
1757                                       " created at a rate of %d/s\n",
1758                                       ofd_name(ofd), created, diff + created,
1759                                       created / DISK_TIMEOUT);
1760                                 break;
1761                         }
1762
1763                         rc = ofd_precreate_objects(tsi->tsi_env, ofd, next_id,
1764                                                    oseq, count, sync_trans);
1765                         if (rc > 0) {
1766                                 created += rc;
1767                                 diff -= rc;
1768                         } else if (rc < 0) {
1769                                 break;
1770                         }
1771                 }
1772
1773                 if (diff > 0 &&
1774                     lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1775                         LCONSOLE_WARN("%s: can't create the same count of"
1776                                       " objects when replaying the request"
1777                                       " (diff is %lld). see LU-4621\n",
1778                                       ofd_name(ofd), diff);
1779
1780                 if (created > 0)
1781                         /* some objects got created, we can return
1782                          * them, even if last creation failed */
1783                         rc = 0;
1784                 else
1785                         CERROR("%s: unable to precreate: rc = %d\n",
1786                                ofd_name(ofd), rc);
1787
1788                 if (!(oa->o_valid & OBD_MD_FLFLAGS) ||
1789                     !(oa->o_flags & OBD_FL_DELORPHAN)) {
1790                         tgt_grant_commit(ofd_obd(ofd)->obd_self_export,
1791                                          granted, rc);
1792                         granted = 0;
1793                 }
1794
1795                 rc2 = ostid_set_id(&rep_oa->o_oi, ofd_seq_last_oid(oseq));
1796                 rc = rc ? : rc2;
1797         }
1798         EXIT;
1799         ofd_counter_incr(exp, LPROC_OFD_STATS_CREATE,
1800                          tsi->tsi_jobid, 1);
1801 out:
1802         mutex_unlock(&oseq->os_create_lock);
1803 out_nolock:
1804         if (rc == 0) {
1805 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
1806                 struct ofd_thread_info  *info = ofd_info(tsi->tsi_env);
1807                 struct lu_fid           *fid = &info->fti_fid;
1808
1809                 /* For compatible purpose, it needs to convert back to
1810                  * OST ID before put it on wire. */
1811                 *fid = rep_oa->o_oi.oi_fid;
1812                 fid_to_ostid(fid, &rep_oa->o_oi);
1813 #endif
1814                 rep_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP;
1815         }
1816         ofd_seq_put(tsi->tsi_env, oseq);
1817
1818 out_sem:
1819         up_read(&ofd->ofd_lastid_rwsem);
1820         return rc;
1821 }
1822
1823 /**
1824  * OFD request handler for OST_DESTROY RPC.
1825  *
1826  * This is OFD-specific part of request handling. It destroys data objects
1827  * related to destroyed object on MDT.
1828  *
1829  * \param[in] tsi       target session environment for this request
1830  *
1831  * \retval              0 if successful
1832  * \retval              negative value on error
1833  */
1834 static int ofd_destroy_hdl(struct tgt_session_info *tsi)
1835 {
1836         const struct ost_body   *body = tsi->tsi_ost_body;
1837         struct ost_body         *repbody;
1838         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1839         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1840         struct lu_fid           *fid = &fti->fti_fid;
1841         u64                      oid;
1842         u32                      count;
1843         int                      rc = 0;
1844
1845         ENTRY;
1846
1847         if (OBD_FAIL_CHECK(OBD_FAIL_OST_EROFS))
1848                 RETURN(-EROFS);
1849
1850         /* This is old case for clients before Lustre 2.4 */
1851         /* If there's a DLM request, cancel the locks mentioned in it */
1852         if (req_capsule_field_present(tsi->tsi_pill, &RMF_DLM_REQ,
1853                                       RCL_CLIENT)) {
1854                 struct ldlm_request *dlm;
1855
1856                 dlm = req_capsule_client_get(tsi->tsi_pill, &RMF_DLM_REQ);
1857                 if (dlm == NULL)
1858                         RETURN(-EFAULT);
1859                 ldlm_request_cancel(tgt_ses_req(tsi), dlm, 0, LATF_SKIP);
1860         }
1861
1862         *fid = body->oa.o_oi.oi_fid;
1863         oid = ostid_id(&body->oa.o_oi);
1864         LASSERT(oid != 0);
1865
1866         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1867
1868         /* check that o_misc makes sense */
1869         if (body->oa.o_valid & OBD_MD_FLOBJCOUNT)
1870                 count = body->oa.o_misc;
1871         else
1872                 count = 1; /* default case - single destroy */
1873
1874         CDEBUG(D_HA, "%s: Destroy object "DOSTID" count %d\n", ofd_name(ofd),
1875                POSTID(&body->oa.o_oi), count);
1876
1877         while (count > 0) {
1878                 int lrc;
1879
1880                 lrc = ofd_destroy_by_fid(tsi->tsi_env, ofd, fid, 0);
1881                 if (lrc == -ENOENT) {
1882                         CDEBUG(D_INODE,
1883                                "%s: destroying non-existent object "DFID"\n",
1884                                ofd_name(ofd), PFID(fid));
1885                         /* rewrite rc with -ENOENT only if it is 0 */
1886                         if (rc == 0)
1887                                 rc = lrc;
1888                 } else if (lrc != 0) {
1889                         CERROR("%s: error destroying object "DFID": %d\n",
1890                                ofd_name(ofd), PFID(fid), lrc);
1891                         rc = lrc;
1892                 }
1893
1894                 count--;
1895                 oid++;
1896                 lrc = fid_set_id(fid, oid);
1897                 if (unlikely(lrc != 0 && count > 0))
1898                         GOTO(out, rc = lrc);
1899         }
1900
1901         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_DESTROY,
1902                          tsi->tsi_jobid, 1);
1903
1904         GOTO(out, rc);
1905
1906 out:
1907         fid_to_ostid(fid, &repbody->oa.o_oi);
1908         return rc;
1909 }
1910
1911 /**
1912  * OFD request handler for OST_STATFS RPC.
1913  *
1914  * This function gets statfs data from storage as part of request
1915  * processing.
1916  *
1917  * \param[in] tsi       target session environment for this request
1918  *
1919  * \retval              0 if successful
1920  * \retval              negative value on error
1921  */
1922 static int ofd_statfs_hdl(struct tgt_session_info *tsi)
1923 {
1924         struct obd_statfs       *osfs;
1925         int                      rc;
1926
1927         ENTRY;
1928
1929         osfs = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_STATFS);
1930
1931         rc = ofd_statfs(tsi->tsi_env, tsi->tsi_exp, osfs,
1932                         cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS), 0);
1933         if (rc != 0)
1934                 CERROR("%s: statfs failed: rc = %d\n",
1935                        tgt_name(tsi->tsi_tgt), rc);
1936
1937         if (OBD_FAIL_CHECK(OBD_FAIL_OST_STATFS_EINPROGRESS))
1938                 rc = -EINPROGRESS;
1939
1940         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_STATFS,
1941                          tsi->tsi_jobid, 1);
1942
1943         RETURN(rc);
1944 }
1945
1946 /**
1947  * OFD request handler for OST_SYNC RPC.
1948  *
1949  * Sync object data or all filesystem data to the disk and pack the
1950  * result in reply.
1951  *
1952  * \param[in] tsi       target session environment for this request
1953  *
1954  * \retval              0 if successful
1955  * \retval              negative value on error
1956  */
1957 static int ofd_sync_hdl(struct tgt_session_info *tsi)
1958 {
1959         struct ost_body         *body = tsi->tsi_ost_body;
1960         struct ost_body         *repbody;
1961         struct ofd_thread_info  *fti = tsi2ofd_info(tsi);
1962         struct ofd_device       *ofd = ofd_exp(tsi->tsi_exp);
1963         struct ofd_object       *fo = NULL;
1964         int                      rc = 0;
1965
1966         ENTRY;
1967
1968         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
1969
1970         /* if no objid is specified, it means "sync whole filesystem" */
1971         if (!fid_is_zero(&tsi->tsi_fid)) {
1972                 fo = ofd_object_find_exists(tsi->tsi_env, ofd, &tsi->tsi_fid);
1973                 if (IS_ERR(fo))
1974                         RETURN(PTR_ERR(fo));
1975         }
1976
1977         rc = tgt_sync(tsi->tsi_env, tsi->tsi_tgt,
1978                       fo != NULL ? ofd_object_child(fo) : NULL,
1979                       repbody->oa.o_size, repbody->oa.o_blocks);
1980         if (rc)
1981                 GOTO(put, rc);
1982
1983         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_SYNC,
1984                          tsi->tsi_jobid, 1);
1985         if (fo == NULL)
1986                 RETURN(0);
1987
1988         repbody->oa.o_oi = body->oa.o_oi;
1989         repbody->oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
1990
1991         rc = ofd_attr_get(tsi->tsi_env, fo, &fti->fti_attr);
1992         if (rc == 0)
1993                 obdo_from_la(&repbody->oa, &fti->fti_attr,
1994                              OFD_VALID_FLAGS);
1995         else
1996                 /* don't return rc from getattr */
1997                 rc = 0;
1998         EXIT;
1999 put:
2000         if (fo != NULL)
2001                 ofd_object_put(tsi->tsi_env, fo);
2002         return rc;
2003 }
2004
2005 /**
2006  * OFD request handler for OST_PUNCH RPC.
2007  *
2008  * This is part of request processing. Validate request fields,
2009  * punch (truncate) the given OFD object and pack reply.
2010  *
2011  * \param[in] tsi       target session environment for this request
2012  *
2013  * \retval              0 if successful
2014  * \retval              negative value on error
2015  */
2016 static int ofd_punch_hdl(struct tgt_session_info *tsi)
2017 {
2018         const struct obdo       *oa = &tsi->tsi_ost_body->oa;
2019         struct ost_body         *repbody;
2020         struct ofd_thread_info  *info = tsi2ofd_info(tsi);
2021         struct ldlm_namespace   *ns = tsi->tsi_tgt->lut_obd->obd_namespace;
2022         struct ldlm_resource    *res;
2023         struct ofd_object       *fo;
2024         struct filter_fid       *ff = NULL;
2025         __u64                    flags = 0;
2026         struct lustre_handle     lh = { 0, };
2027         int                      rc;
2028         __u64                    start, end;
2029         bool                     srvlock;
2030
2031         ENTRY;
2032
2033         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_PAUSE_PUNCH, cfs_fail_val);
2034
2035         /* check that we do support OBD_CONNECT_TRUNCLOCK. */
2036         CLASSERT(OST_CONNECT_SUPPORTED & OBD_CONNECT_TRUNCLOCK);
2037
2038         if ((oa->o_valid & (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS)) !=
2039             (OBD_MD_FLSIZE | OBD_MD_FLBLOCKS))
2040                 RETURN(err_serious(-EPROTO));
2041
2042         repbody = req_capsule_server_get(tsi->tsi_pill, &RMF_OST_BODY);
2043         if (repbody == NULL)
2044                 RETURN(err_serious(-ENOMEM));
2045
2046         /* punch start,end are passed in o_size,o_blocks throught wire */
2047         start = oa->o_size;
2048         end = oa->o_blocks;
2049
2050         if (end != OBD_OBJECT_EOF) /* Only truncate is supported */
2051                 RETURN(-EPROTO);
2052
2053         /* standard truncate optimization: if file body is completely
2054          * destroyed, don't send data back to the server. */
2055         if (start == 0)
2056                 flags |= LDLM_FL_AST_DISCARD_DATA;
2057
2058         repbody->oa.o_oi = oa->o_oi;
2059         repbody->oa.o_valid = OBD_MD_FLID;
2060
2061         srvlock = oa->o_valid & OBD_MD_FLFLAGS &&
2062                   oa->o_flags & OBD_FL_SRVLOCK;
2063
2064         if (srvlock) {
2065                 rc = tgt_extent_lock(ns, &tsi->tsi_resid, start, end, &lh,
2066                                      LCK_PW, &flags);
2067                 if (rc != 0)
2068                         RETURN(rc);
2069         }
2070
2071         CDEBUG(D_INODE, "calling punch for object "DFID", valid = %#llx"
2072                ", start = %lld, end = %lld\n", PFID(&tsi->tsi_fid),
2073                oa->o_valid, start, end);
2074
2075         fo = ofd_object_find_exists(tsi->tsi_env, ofd_exp(tsi->tsi_exp),
2076                                     &tsi->tsi_fid);
2077         if (IS_ERR(fo))
2078                 GOTO(out, rc = PTR_ERR(fo));
2079
2080         la_from_obdo(&info->fti_attr, oa,
2081                      OBD_MD_FLMTIME | OBD_MD_FLATIME | OBD_MD_FLCTIME);
2082         info->fti_attr.la_size = start;
2083         info->fti_attr.la_valid |= LA_SIZE;
2084
2085         if (oa->o_valid & OBD_MD_FLFID) {
2086                 ff = &info->fti_mds_fid;
2087                 ofd_prepare_fidea(ff, oa);
2088         }
2089
2090         rc = ofd_object_punch(tsi->tsi_env, fo, start, end, &info->fti_attr,
2091                               ff, (struct obdo *)oa);
2092         if (rc)
2093                 GOTO(out_put, rc);
2094
2095         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_PUNCH,
2096                          tsi->tsi_jobid, 1);
2097         EXIT;
2098 out_put:
2099         ofd_object_put(tsi->tsi_env, fo);
2100 out:
2101         if (srvlock)
2102                 tgt_extent_unlock(&lh, LCK_PW);
2103         if (rc == 0) {
2104                 /* we do not call this before to avoid lu_object_find() in
2105                  *  ->lvbo_update() holding another reference on the object.
2106                  * otherwise concurrent destroy can make the object unavailable
2107                  * for 2nd lu_object_find() waiting for the first reference
2108                  * to go... deadlock! */
2109                 res = ldlm_resource_get(ns, NULL, &tsi->tsi_resid,
2110                                         LDLM_EXTENT, 0);
2111                 if (!IS_ERR(res)) {
2112                         ldlm_res_lvbo_update(res, NULL, 0);
2113                         ldlm_resource_putref(res);
2114                 }
2115         }
2116         return rc;
2117 }
2118
2119 static int ofd_ladvise_prefetch(const struct lu_env *env,
2120                                 struct ofd_object *fo,
2121                                 struct niobuf_local *lnb,
2122                                 __u64 start, __u64 end, enum dt_bufs_type dbt)
2123 {
2124         struct ofd_thread_info *info = ofd_info(env);
2125         pgoff_t start_index, end_index, pages;
2126         struct niobuf_remote rnb;
2127         unsigned long nr_local;
2128         int rc = 0;
2129
2130         if (end <= start)
2131                 RETURN(-EINVAL);
2132
2133         ofd_read_lock(env, fo);
2134         if (!ofd_object_exists(fo))
2135                 GOTO(out_unlock, rc = -ENOENT);
2136
2137         rc = ofd_attr_get(env, fo, &info->fti_attr);
2138         if (rc)
2139                 GOTO(out_unlock, rc);
2140
2141         if (end > info->fti_attr.la_size)
2142                 end = info->fti_attr.la_size;
2143
2144         if (end == 0)
2145                 GOTO(out_unlock, rc);
2146
2147         /* We need page aligned offset and length */
2148         start_index = start >> PAGE_SHIFT;
2149         end_index = (end - 1) >> PAGE_SHIFT;
2150         pages = end_index - start_index + 1;
2151         while (pages > 0) {
2152                 nr_local = pages <= PTLRPC_MAX_BRW_PAGES ? pages :
2153                         PTLRPC_MAX_BRW_PAGES;
2154                 rnb.rnb_offset = start_index << PAGE_SHIFT;
2155                 rnb.rnb_len = nr_local << PAGE_SHIFT;
2156                 rc = dt_bufs_get(env, ofd_object_child(fo), &rnb, lnb, dbt);
2157                 if (unlikely(rc < 0))
2158                         break;
2159                 nr_local = rc;
2160                 rc = dt_read_prep(env, ofd_object_child(fo), lnb, nr_local);
2161                 dt_bufs_put(env, ofd_object_child(fo), lnb, nr_local);
2162                 if (unlikely(rc))
2163                         break;
2164                 start_index += nr_local;
2165                 pages -= nr_local;
2166         }
2167
2168 out_unlock:
2169         ofd_read_unlock(env, fo);
2170         RETURN(rc);
2171 }
2172
2173 /**
2174  * OFD request handler for OST_LADVISE RPC.
2175  *
2176  * Tune cache or perfetch policies according to advices.
2177  *
2178  * \param[in] tsi       target session environment for this request
2179  *
2180  * \retval              0 if successful
2181  * \retval              negative errno on error
2182  */
2183 static int ofd_ladvise_hdl(struct tgt_session_info *tsi)
2184 {
2185         struct ptlrpc_request *req = tgt_ses_req(tsi);
2186         struct obd_export *exp = tsi->tsi_exp;
2187         struct ofd_device *ofd = ofd_exp(exp);
2188         struct ost_body *body, *repbody;
2189         struct ofd_thread_info *info;
2190         struct ofd_object *fo;
2191         struct ptlrpc_thread *svc_thread = req->rq_svc_thread;
2192         const struct lu_env *env = svc_thread->t_env;
2193         struct tgt_thread_big_cache *tbc = svc_thread->t_data;
2194         enum dt_bufs_type dbt = DT_BUFS_TYPE_READAHEAD;
2195         struct lu_ladvise *ladvise;
2196         int num_advise;
2197         struct ladvise_hdr *ladvise_hdr;
2198         struct obd_ioobj ioo;
2199         struct lustre_handle lockh = { 0 };
2200         __u64 flags = 0;
2201         int i;
2202         struct dt_object *dob;
2203         __u64 start;
2204         __u64 end;
2205         int rc = 0;
2206         ENTRY;
2207
2208         CFS_FAIL_TIMEOUT(OBD_FAIL_OST_LADVISE_PAUSE, cfs_fail_val);
2209         body = tsi->tsi_ost_body;
2210
2211         if ((body->oa.o_valid & OBD_MD_FLID) != OBD_MD_FLID)
2212                 RETURN(err_serious(-EPROTO));
2213
2214         ladvise_hdr = req_capsule_client_get(tsi->tsi_pill,
2215                                              &RMF_OST_LADVISE_HDR);
2216         if (ladvise_hdr == NULL)
2217                 RETURN(err_serious(-EPROTO));
2218
2219         if (ladvise_hdr->lah_magic != LADVISE_MAGIC ||
2220             ladvise_hdr->lah_count < 1)
2221                 RETURN(err_serious(-EPROTO));
2222
2223         if ((ladvise_hdr->lah_flags & (~LF_MASK)) != 0)
2224                 RETURN(err_serious(-EPROTO));
2225
2226         ladvise = req_capsule_client_get(tsi->tsi_pill, &RMF_OST_LADVISE);
2227         if (ladvise == NULL)
2228                 RETURN(err_serious(-EPROTO));
2229
2230         num_advise = req_capsule_get_size(&req->rq_pill,
2231                                           &RMF_OST_LADVISE, RCL_CLIENT) /
2232                                           sizeof(*ladvise);
2233         if (num_advise < ladvise_hdr->lah_count)
2234                 RETURN(err_serious(-EPROTO));
2235
2236         repbody = req_capsule_server_get(&req->rq_pill, &RMF_OST_BODY);
2237         repbody->oa = body->oa;
2238
2239         info = ofd_info_init(env, exp);
2240
2241         rc = ostid_to_fid(&info->fti_fid, &body->oa.o_oi,
2242                           ofd->ofd_lut.lut_lsd.lsd_osd_index);
2243         if (rc != 0)
2244                 RETURN(rc);
2245
2246         fo = ofd_object_find(env, ofd, &info->fti_fid);
2247         if (IS_ERR(fo)) {
2248                 rc = PTR_ERR(fo);
2249                 RETURN(rc);
2250         }
2251         LASSERT(fo != NULL);
2252         dob = ofd_object_child(fo);
2253
2254         if (ptlrpc_connection_is_local(exp->exp_connection))
2255                 dbt |= DT_BUFS_TYPE_LOCAL;
2256
2257         for (i = 0; i < num_advise; i++, ladvise++) {
2258                 start = ladvise->lla_start;
2259                 end = ladvise->lla_end;
2260                 if (end <= start) {
2261                         rc = err_serious(-EPROTO);
2262                         break;
2263                 }
2264
2265                 /* Handle different advice types */
2266                 switch (ladvise->lla_advice) {
2267                 default:
2268                         rc = -ENOTSUPP;
2269                         break;
2270                 case LU_LADVISE_WILLREAD:
2271                         if (tbc == NULL)
2272                                 RETURN(-ENOMEM);
2273
2274                         ioo.ioo_oid = body->oa.o_oi;
2275                         ioo.ioo_bufcnt = 1;
2276                         rc = tgt_extent_lock(exp->exp_obd->obd_namespace,
2277                                              &tsi->tsi_resid, start, end - 1,
2278                                              &lockh, LCK_PR, &flags);
2279                         if (rc != 0)
2280                                 break;
2281
2282                         req->rq_status = ofd_ladvise_prefetch(env, fo,
2283                                                               tbc->local,
2284                                                               start, end, dbt);
2285                         tgt_extent_unlock(&lockh, LCK_PR);
2286                         break;
2287                 case LU_LADVISE_DONTNEED:
2288                         rc = dt_ladvise(env, dob, ladvise->lla_start,
2289                                         ladvise->lla_end, LU_LADVISE_DONTNEED);
2290                         break;
2291                 }
2292                 if (rc != 0)
2293                         break;
2294         }
2295
2296         ofd_object_put(env, fo);
2297         req->rq_status = rc;
2298         RETURN(rc);
2299 }
2300
2301 /**
2302  * OFD request handler for OST_QUOTACTL RPC.
2303  *
2304  * This is part of request processing to validate incoming request fields,
2305  * get the requested data from OSD and pack reply.
2306  *
2307  * \param[in] tsi       target session environment for this request
2308  *
2309  * \retval              0 if successful
2310  * \retval              negative value on error
2311  */
2312 static int ofd_quotactl(struct tgt_session_info *tsi)
2313 {
2314         struct obd_quotactl *oqctl, *repoqc;
2315         struct lu_nodemap *nodemap;
2316         int id;
2317         int rc;
2318
2319         ENTRY;
2320
2321         oqctl = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
2322         if (oqctl == NULL)
2323                 RETURN(err_serious(-EPROTO));
2324
2325         repoqc = req_capsule_server_get(tsi->tsi_pill, &RMF_OBD_QUOTACTL);
2326         if (repoqc == NULL)
2327                 RETURN(err_serious(-ENOMEM));
2328
2329         *repoqc = *oqctl;
2330
2331         nodemap = nodemap_get_from_exp(tsi->tsi_exp);
2332         if (IS_ERR(nodemap))
2333                 RETURN(PTR_ERR(nodemap));
2334
2335         id = repoqc->qc_id;
2336         if (oqctl->qc_type == USRQUOTA)
2337                 id = nodemap_map_id(nodemap, NODEMAP_UID,
2338                                     NODEMAP_CLIENT_TO_FS,
2339                                     repoqc->qc_id);
2340         else if (oqctl->qc_type == GRPQUOTA)
2341                 id = nodemap_map_id(nodemap, NODEMAP_GID,
2342                                     NODEMAP_CLIENT_TO_FS,
2343                                     repoqc->qc_id);
2344
2345         nodemap_putref(nodemap);
2346
2347         if (repoqc->qc_id != id)
2348                 swap(repoqc->qc_id, id);
2349
2350         rc = lquotactl_slv(tsi->tsi_env, tsi->tsi_tgt->lut_bottom, repoqc);
2351
2352         ofd_counter_incr(tsi->tsi_exp, LPROC_OFD_STATS_QUOTACTL,
2353                          tsi->tsi_jobid, 1);
2354
2355         if (repoqc->qc_id != id)
2356                 swap(repoqc->qc_id, id);
2357
2358         RETURN(rc);
2359 }
2360
2361 /**
2362  * Calculate the amount of time for lock prolongation.
2363  *
2364  * This is helper for ofd_prolong_extent_locks() function to get
2365  * the timeout extra time.
2366  *
2367  * \param[in] req       current request
2368  *
2369  * \retval              amount of time to extend the timeout with
2370  */
2371 static inline int prolong_timeout(struct ptlrpc_request *req)
2372 {
2373         struct ptlrpc_service_part *svcpt = req->rq_rqbd->rqbd_svcpt;
2374         time_t req_timeout;
2375
2376         if (AT_OFF)
2377                 return obd_timeout / 2;
2378
2379         req_timeout = req->rq_deadline - req->rq_arrival_time.tv_sec;
2380         return max_t(time_t, at_est2timeout(at_get(&svcpt->scp_at_estimate)),
2381                      req_timeout);
2382 }
2383
2384 /**
2385  * Prolong lock timeout for the given extent.
2386  *
2387  * This function finds all locks related with incoming request and
2388  * prolongs their timeout.
2389  *
2390  * If a client is holding a lock for a long time while it sends
2391  * read or write RPCs to the OST for the object under this lock,
2392  * then we don't want the OST to evict the client. Otherwise,
2393  * if the network or disk is very busy then the client may not
2394  * be able to make any progress to clear out dirty pages under
2395  * the lock and the application will fail.
2396  *
2397  * Every time a Bulk Read/Write (BRW) request arrives for the object
2398  * covered by the lock, extend the timeout on that lock. The RPC should
2399  * contain a lock handle for the lock it is using, but this
2400  * isn't handled correctly by all client versions, and the
2401  * request may cover multiple locks.
2402  *
2403  * \param[in] tsi       target session environment for this request
2404  * \param[in] data      struct of data to prolong locks
2405  *
2406  */
2407 static void ofd_prolong_extent_locks(struct tgt_session_info *tsi,
2408                                     struct ldlm_prolong_args *data)
2409 {
2410         struct obdo             *oa  = &tsi->tsi_ost_body->oa;
2411         struct ldlm_lock        *lock;
2412
2413         ENTRY;
2414
2415         data->lpa_timeout = prolong_timeout(tgt_ses_req(tsi));
2416         data->lpa_export = tsi->tsi_exp;
2417         data->lpa_resid = tsi->tsi_resid;
2418
2419         CDEBUG(D_RPCTRACE, "Prolong locks for req %p with x%llu"
2420                " ext(%llu->%llu)\n", tgt_ses_req(tsi),
2421                tgt_ses_req(tsi)->rq_xid, data->lpa_extent.start,
2422                data->lpa_extent.end);
2423
2424         if (oa->o_valid & OBD_MD_FLHANDLE) {
2425                 /* mostly a request should be covered by only one lock, try
2426                  * fast path. */
2427                 lock = ldlm_handle2lock(&oa->o_handle);
2428                 if (lock != NULL) {
2429                         /* Fast path to check if the lock covers the whole IO
2430                          * region exclusively. */
2431                         if (ldlm_extent_contain(&lock->l_policy_data.l_extent,
2432                                                 &data->lpa_extent)) {
2433                                 /* bingo */
2434                                 LASSERT(lock->l_export == data->lpa_export);
2435                                 ldlm_lock_prolong_one(lock, data);
2436                                 LDLM_LOCK_PUT(lock);
2437                                 if (data->lpa_locks_cnt > 0)
2438                                         RETURN_EXIT;
2439                                 /* The lock was destroyed probably lets try
2440                                  * resource tree. */
2441                         } else {
2442                                 lock->l_last_used = ktime_get();
2443                                 LDLM_LOCK_PUT(lock);
2444                         }
2445                 }
2446         }
2447
2448         ldlm_resource_prolong(data);
2449         EXIT;
2450 }
2451
2452 /**
2453  * Implementation of ptlrpc_hpreq_ops::hpreq_lock_match for OFD RW requests.
2454  *
2455  * Determine if \a lock and the lock from request \a req are equivalent
2456  * by comparing their resource names, modes, and extents.
2457  *
2458  * It is used to give priority to read and write RPCs being done
2459  * under this lock so that the client can drop the contended
2460  * lock more quickly and let other clients use it. This improves
2461  * overall performance in the case where the first client gets a
2462  * very large lock extent that prevents other clients from
2463  * submitting their writes.
2464  *
2465  * \param[in] req       ptlrpc_request being processed
2466  * \param[in] lock      contended lock to match
2467  *
2468  * \retval              1 if lock is matched
2469  * \retval              0 otherwise
2470  */
2471 static int ofd_rw_hpreq_lock_match(struct ptlrpc_request *req,
2472                                    struct ldlm_lock *lock)
2473 {
2474         struct niobuf_remote *rnb;
2475         struct obd_ioobj *ioo;
2476         enum ldlm_mode  mode;
2477         struct ldlm_extent ext;
2478         __u32 opc = lustre_msg_get_opc(req->rq_reqmsg);
2479
2480         ENTRY;
2481
2482         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
2483         LASSERT(ioo != NULL);
2484
2485         rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
2486         LASSERT(rnb != NULL);
2487
2488         ext.start = rnb->rnb_offset;
2489         rnb += ioo->ioo_bufcnt - 1;
2490         ext.end = rnb->rnb_offset + rnb->rnb_len - 1;
2491
2492         LASSERT(lock->l_resource != NULL);
2493         if (!ostid_res_name_eq(&ioo->ioo_oid, &lock->l_resource->lr_name))
2494                 RETURN(0);
2495
2496         /* a bulk write can only hold a reference on a PW extent lock
2497          * or GROUP lock.
2498          */
2499         mode = LCK_PW | LCK_GROUP;
2500         if (opc == OST_READ)
2501                 /* whereas a bulk read can be protected by either a PR or PW
2502                  * extent lock */
2503                 mode |= LCK_PR;
2504
2505         if (!(lock->l_granted_mode & mode))
2506                 RETURN(0);
2507
2508         RETURN(ldlm_extent_overlap(&lock->l_policy_data.l_extent, &ext));
2509 }
2510
2511 /**
2512  * Implementation of ptlrpc_hpreq_ops::hpreq_lock_check for OFD RW requests.
2513  *
2514  * Check for whether the given PTLRPC request (\a req) is blocking
2515  * an LDLM lock cancel. Also checks whether the request is covered by an LDLM
2516  * lock.
2517  *
2518  * \param[in] req       the incoming request
2519  *
2520  * \retval              1 if \a req is blocking an LDLM lock cancel
2521  * \retval              0 if it is not
2522  * \retval              -ESTALE if lock is not found
2523  */
2524 static int ofd_rw_hpreq_check(struct ptlrpc_request *req)
2525 {
2526         struct tgt_session_info *tsi;
2527         struct obd_ioobj        *ioo;
2528         struct niobuf_remote    *rnb;
2529         int opc;
2530         struct ldlm_prolong_args pa = { 0 };
2531
2532         ENTRY;
2533
2534         /* Don't use tgt_ses_info() to get session info, because lock_match()
2535          * can be called while request has no processing thread yet. */
2536         tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
2537
2538         /*
2539          * Use LASSERT below because malformed RPCs should have
2540          * been filtered out in tgt_hpreq_handler().
2541          */
2542         opc = lustre_msg_get_opc(req->rq_reqmsg);
2543         LASSERT(opc == OST_READ || opc == OST_WRITE);
2544
2545         ioo = req_capsule_client_get(&req->rq_pill, &RMF_OBD_IOOBJ);
2546         LASSERT(ioo != NULL);
2547
2548         rnb = req_capsule_client_get(&req->rq_pill, &RMF_NIOBUF_REMOTE);
2549         LASSERT(rnb != NULL);
2550         LASSERT(!(rnb->rnb_flags & OBD_BRW_SRVLOCK));
2551
2552         pa.lpa_mode = LCK_PW | LCK_GROUP;
2553         if (opc == OST_READ)
2554                 pa.lpa_mode |= LCK_PR;
2555
2556         pa.lpa_extent.start = rnb->rnb_offset;
2557         rnb += ioo->ioo_bufcnt - 1;
2558         pa.lpa_extent.end = rnb->rnb_offset + rnb->rnb_len - 1;
2559
2560         DEBUG_REQ(D_RPCTRACE, req, "%s %s: refresh rw locks: "DFID
2561                   " (%llu->%llu)\n", tgt_name(tsi->tsi_tgt),
2562                   current->comm, PFID(&tsi->tsi_fid), pa.lpa_extent.start,
2563                   pa.lpa_extent.end);
2564
2565         ofd_prolong_extent_locks(tsi, &pa);
2566
2567         CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
2568                tgt_name(tsi->tsi_tgt), pa.lpa_blocks_cnt, req);
2569
2570         if (pa.lpa_blocks_cnt > 0)
2571                 RETURN(1);
2572
2573         RETURN(pa.lpa_locks_cnt > 0 ? 0 : -ESTALE);
2574 }
2575
2576 /**
2577  * Implementation of ptlrpc_hpreq_ops::hpreq_lock_fini for OFD RW requests.
2578  *
2579  * Called after the request has been handled. It refreshes lock timeout again
2580  * so that client has more time to send lock cancel RPC.
2581  *
2582  * \param[in] req       request which is being processed.
2583  */
2584 static void ofd_rw_hpreq_fini(struct ptlrpc_request *req)
2585 {
2586         ofd_rw_hpreq_check(req);
2587 }
2588
2589 /**
2590  * Implementation of ptlrpc_hpreq_ops::hpreq_lock_match for OST_PUNCH request.
2591  *
2592  * This function checks if the given lock is the same by its resname, mode
2593  * and extent as one taken from the request.
2594  * It is used to give priority to punch/truncate RPCs that might lead to
2595  * the fastest release of that lock when a lock is contended.
2596  *
2597  * \param[in] req       ptlrpc_request being processed
2598  * \param[in] lock      contended lock to match
2599  *
2600  * \retval              1 if lock is matched
2601  * \retval              0 otherwise
2602  */
2603 static int ofd_punch_hpreq_lock_match(struct ptlrpc_request *req,
2604                                       struct ldlm_lock *lock)
2605 {
2606         struct tgt_session_info *tsi;
2607         struct obdo             *oa;
2608         struct ldlm_extent       ext;
2609
2610         ENTRY;
2611
2612         /* Don't use tgt_ses_info() to get session info, because lock_match()
2613          * can be called while request has no processing thread yet. */
2614         tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
2615
2616         /*
2617          * Use LASSERT below because malformed RPCs should have
2618          * been filtered out in tgt_hpreq_handler().
2619          */
2620         LASSERT(tsi->tsi_ost_body != NULL);
2621         if (tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLHANDLE &&
2622             tsi->tsi_ost_body->oa.o_handle.cookie == lock->l_handle.h_cookie)
2623                 RETURN(1);
2624
2625         oa = &tsi->tsi_ost_body->oa;
2626         ext.start = oa->o_size;
2627         ext.end   = oa->o_blocks;
2628
2629         LASSERT(lock->l_resource != NULL);
2630         if (!ostid_res_name_eq(&oa->o_oi, &lock->l_resource->lr_name))
2631                 RETURN(0);
2632
2633         if (!(lock->l_granted_mode & (LCK_PW | LCK_GROUP)))
2634                 RETURN(0);
2635
2636         RETURN(ldlm_extent_overlap(&lock->l_policy_data.l_extent, &ext));
2637 }
2638
2639 /**
2640  * Implementation of ptlrpc_hpreq_ops::hpreq_lock_check for OST_PUNCH request.
2641  *
2642  * High-priority queue request check for whether the given punch request
2643  * (\a req) is blocking an LDLM lock cancel. Also checks whether the request is
2644  * covered by an LDLM lock.
2645  *
2646
2647  *
2648  * \param[in] req       the incoming request
2649  *
2650  * \retval              1 if \a req is blocking an LDLM lock cancel
2651  * \retval              0 if it is not
2652  * \retval              -ESTALE if lock is not found
2653  */
2654 static int ofd_punch_hpreq_check(struct ptlrpc_request *req)
2655 {
2656         struct tgt_session_info *tsi;
2657         struct obdo             *oa;
2658         struct ldlm_prolong_args pa = { 0 };
2659
2660         ENTRY;
2661
2662         /* Don't use tgt_ses_info() to get session info, because lock_match()
2663          * can be called while request has no processing thread yet. */
2664         tsi = lu_context_key_get(&req->rq_session, &tgt_session_key);
2665         LASSERT(tsi != NULL);
2666         oa = &tsi->tsi_ost_body->oa;
2667
2668         LASSERT(!(oa->o_valid & OBD_MD_FLFLAGS &&
2669                   oa->o_flags & OBD_FL_SRVLOCK));
2670
2671         pa.lpa_mode = LCK_PW | LCK_GROUP;
2672         pa.lpa_extent.start = oa->o_size;
2673         pa.lpa_extent.end   = oa->o_blocks;
2674
2675         CDEBUG(D_DLMTRACE,
2676                "%s: refresh locks: %llu/%llu (%llu->%llu)\n",
2677                tgt_name(tsi->tsi_tgt), tsi->tsi_resid.name[0],
2678                tsi->tsi_resid.name[1], pa.lpa_extent.start, pa.lpa_extent.end);
2679
2680         ofd_prolong_extent_locks(tsi, &pa);
2681
2682         CDEBUG(D_DLMTRACE, "%s: refreshed %u locks timeout for req %p.\n",
2683                tgt_name(tsi->tsi_tgt), pa.lpa_blocks_cnt, req);
2684
2685         if (pa.lpa_blocks_cnt > 0)
2686                 RETURN(1);
2687
2688         RETURN(pa.lpa_locks_cnt > 0 ? 0 : -ESTALE);
2689 }
2690
2691 /**
2692  * Implementation of ptlrpc_hpreq_ops::hpreq_lock_fini for OST_PUNCH request.
2693  *
2694  * Called after the request has been handled. It refreshes lock timeout again
2695  * so that client has more time to send lock cancel RPC.
2696  *
2697  * \param[in] req       request which is being processed.
2698  */
2699 static void ofd_punch_hpreq_fini(struct ptlrpc_request *req)
2700 {
2701         ofd_punch_hpreq_check(req);
2702 }
2703
2704 static struct ptlrpc_hpreq_ops ofd_hpreq_rw = {
2705         .hpreq_lock_match       = ofd_rw_hpreq_lock_match,
2706         .hpreq_check            = ofd_rw_hpreq_check,
2707         .hpreq_fini             = ofd_rw_hpreq_fini
2708 };
2709
2710 static struct ptlrpc_hpreq_ops ofd_hpreq_punch = {
2711         .hpreq_lock_match       = ofd_punch_hpreq_lock_match,
2712         .hpreq_check            = ofd_punch_hpreq_check,
2713         .hpreq_fini             = ofd_punch_hpreq_fini
2714 };
2715
2716 /**
2717  * Assign high priority operations to an IO request.
2718  *
2719  * Check if the incoming request is a candidate for
2720  * high-priority processing. If it is, assign it a high
2721  * priority operations table.
2722  *
2723  * \param[in] tsi       target session environment for this request
2724  */
2725 static void ofd_hp_brw(struct tgt_session_info *tsi)
2726 {
2727         struct niobuf_remote    *rnb;
2728         struct obd_ioobj        *ioo;
2729
2730         ENTRY;
2731
2732         ioo = req_capsule_client_get(tsi->tsi_pill, &RMF_OBD_IOOBJ);
2733         LASSERT(ioo != NULL); /* must exist after request preprocessing */
2734         if (ioo->ioo_bufcnt > 0) {
2735                 rnb = req_capsule_client_get(tsi->tsi_pill, &RMF_NIOBUF_REMOTE);
2736                 LASSERT(rnb != NULL); /* must exist after request preprocessing */
2737
2738                 /* no high priority if server lock is needed */
2739                 if (rnb->rnb_flags & OBD_BRW_SRVLOCK ||
2740                     (lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg)
2741                      & MSG_REPLAY))
2742                         return;
2743         }
2744         tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_rw;
2745 }
2746
2747 /**
2748  * Assign high priority operations to an punch request.
2749  *
2750  * Check if the incoming request is a candidate for
2751  * high-priority processing. If it is, assign it a high
2752  * priority operations table.
2753  *
2754  * \param[in] tsi       target session environment for this request
2755  */
2756 static void ofd_hp_punch(struct tgt_session_info *tsi)
2757 {
2758         LASSERT(tsi->tsi_ost_body != NULL); /* must exists if we are here */
2759         /* no high-priority if server lock is needed */
2760         if ((tsi->tsi_ost_body->oa.o_valid & OBD_MD_FLFLAGS &&
2761              tsi->tsi_ost_body->oa.o_flags & OBD_FL_SRVLOCK) ||
2762             tgt_conn_flags(tsi) & OBD_CONNECT_MDS ||
2763             lustre_msg_get_flags(tgt_ses_req(tsi)->rq_reqmsg) & MSG_REPLAY)
2764                 return;
2765         tgt_ses_req(tsi)->rq_ops = &ofd_hpreq_punch;
2766 }
2767
2768 #define OBD_FAIL_OST_READ_NET   OBD_FAIL_OST_BRW_NET
2769 #define OBD_FAIL_OST_WRITE_NET  OBD_FAIL_OST_BRW_NET
2770 #define OST_BRW_READ    OST_READ
2771 #define OST_BRW_WRITE   OST_WRITE
2772
2773 /**
2774  * Table of OFD-specific request handlers
2775  *
2776  * This table contains all opcodes accepted by OFD and
2777  * specifies handlers for them. The tgt_request_handler()
2778  * uses such table from each target to process incoming
2779  * requests.
2780  */
2781 static struct tgt_handler ofd_tgt_handlers[] = {
2782 TGT_RPC_HANDLER(OST_FIRST_OPC,
2783                 0,                      OST_CONNECT,    tgt_connect,
2784                 &RQF_CONNECT, LUSTRE_OBD_VERSION),
2785 TGT_RPC_HANDLER(OST_FIRST_OPC,
2786                 0,                      OST_DISCONNECT, tgt_disconnect,
2787                 &RQF_OST_DISCONNECT, LUSTRE_OBD_VERSION),
2788 TGT_RPC_HANDLER(OST_FIRST_OPC,
2789                 0,                      OST_SET_INFO,   ofd_set_info_hdl,
2790                 &RQF_OBD_SET_INFO, LUSTRE_OST_VERSION),
2791 TGT_OST_HDL(0,                          OST_GET_INFO,   ofd_get_info_hdl),
2792 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_GETATTR,    ofd_getattr_hdl),
2793 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
2794                                         OST_SETATTR,    ofd_setattr_hdl),
2795 TGT_OST_HDL(0           | HABEO_REFERO | MUTABOR,
2796                                         OST_CREATE,     ofd_create_hdl),
2797 TGT_OST_HDL(0           | HABEO_REFERO | MUTABOR,
2798                                         OST_DESTROY,    ofd_destroy_hdl),
2799 TGT_OST_HDL(0           | HABEO_REFERO, OST_STATFS,     ofd_statfs_hdl),
2800 TGT_OST_HDL_HP(HABEO_CORPUS| HABEO_REFERO,
2801                                         OST_BRW_READ,   tgt_brw_read,
2802                                                         ofd_hp_brw),
2803 /* don't set CORPUS flag for brw_write because -ENOENT may be valid case */
2804 TGT_OST_HDL_HP(HABEO_CORPUS| MUTABOR,   OST_BRW_WRITE,  tgt_brw_write,
2805                                                         ofd_hp_brw),
2806 TGT_OST_HDL_HP(HABEO_CORPUS| HABEO_REFERO | MUTABOR,
2807                                         OST_PUNCH,      ofd_punch_hdl,
2808                                                         ofd_hp_punch),
2809 TGT_OST_HDL(HABEO_CORPUS| HABEO_REFERO, OST_SYNC,       ofd_sync_hdl),
2810 TGT_OST_HDL(0           | HABEO_REFERO, OST_QUOTACTL,   ofd_quotactl),
2811 TGT_OST_HDL(HABEO_CORPUS | HABEO_REFERO, OST_LADVISE,   ofd_ladvise_hdl),
2812 };
2813
2814 static struct tgt_opc_slice ofd_common_slice[] = {
2815         {
2816                 .tos_opc_start  = OST_FIRST_OPC,
2817                 .tos_opc_end    = OST_LAST_OPC,
2818                 .tos_hs         = ofd_tgt_handlers
2819         },
2820         {
2821                 .tos_opc_start  = OBD_FIRST_OPC,
2822                 .tos_opc_end    = OBD_LAST_OPC,
2823                 .tos_hs         = tgt_obd_handlers
2824         },
2825         {
2826                 .tos_opc_start  = LDLM_FIRST_OPC,
2827                 .tos_opc_end    = LDLM_LAST_OPC,
2828                 .tos_hs         = tgt_dlm_handlers
2829         },
2830         {
2831                 .tos_opc_start  = OUT_UPDATE_FIRST_OPC,
2832                 .tos_opc_end    = OUT_UPDATE_LAST_OPC,
2833                 .tos_hs         = tgt_out_handlers
2834         },
2835         {
2836                 .tos_opc_start  = SEQ_FIRST_OPC,
2837                 .tos_opc_end    = SEQ_LAST_OPC,
2838                 .tos_hs         = seq_handlers
2839         },
2840         {
2841                 .tos_opc_start  = LFSCK_FIRST_OPC,
2842                 .tos_opc_end    = LFSCK_LAST_OPC,
2843                 .tos_hs         = tgt_lfsck_handlers
2844         },
2845         {
2846                 .tos_opc_start  = SEC_FIRST_OPC,
2847                 .tos_opc_end    = SEC_LAST_OPC,
2848                 .tos_hs         = tgt_sec_ctx_handlers
2849         },
2850         {
2851                 .tos_hs         = NULL
2852         }
2853 };
2854
2855 /* context key constructor/destructor: ofd_key_init(), ofd_key_fini() */
2856 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
2857
2858 /**
2859  * Implementation of lu_context_key::lct_key_exit.
2860  *
2861  * Optional method called on lu_context_exit() for all allocated
2862  * keys.
2863  * It is used in OFD to sanitize context values which may be re-used
2864  * during another request processing by the same thread.
2865  *
2866  * \param[in] ctx       execution context
2867  * \param[in] key       context key
2868  * \param[in] data      ofd_thread_info
2869  */
2870 static void ofd_key_exit(const struct lu_context *ctx,
2871                          struct lu_context_key *key, void *data)
2872 {
2873         struct ofd_thread_info *info = data;
2874
2875         info->fti_env = NULL;
2876         info->fti_exp = NULL;
2877
2878         info->fti_xid = 0;
2879         info->fti_pre_version = 0;
2880
2881         memset(&info->fti_attr, 0, sizeof info->fti_attr);
2882 }
2883
2884 struct lu_context_key ofd_thread_key = {
2885         .lct_tags = LCT_DT_THREAD,
2886         .lct_init = ofd_key_init,
2887         .lct_fini = ofd_key_fini,
2888         .lct_exit = ofd_key_exit
2889 };
2890
2891 /**
2892  * Initialize OFD device according to parameters in the config log \a cfg.
2893  *
2894  * This is the main starting point of OFD initialization. It fills all OFD
2895  * parameters with their initial values and calls other initializing functions
2896  * to set up all OFD subsystems.
2897  *
2898  * \param[in] env       execution environment
2899  * \param[in] m         OFD device
2900  * \param[in] ldt       LU device type of OFD
2901  * \param[in] cfg       configuration log
2902  *
2903  * \retval              0 if successful
2904  * \retval              negative value on error
2905  */
2906 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
2907                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
2908 {
2909         const char *dev = lustre_cfg_string(cfg, 0);
2910         struct ofd_thread_info *info = NULL;
2911         struct obd_device *obd;
2912         struct tg_grants_data *tgd = &m->ofd_lut.lut_tgd;
2913         struct obd_statfs *osfs;
2914         struct lu_fid fid;
2915         struct nm_config_file *nodemap_config;
2916         struct obd_device_target *obt;
2917         int rc;
2918
2919         ENTRY;
2920
2921         obd = class_name2obd(dev);
2922         if (obd == NULL) {
2923                 CERROR("Cannot find obd with name %s\n", dev);
2924                 RETURN(-ENODEV);
2925         }
2926
2927         rc = lu_env_refill((struct lu_env *)env);
2928         if (rc != 0)
2929                 RETURN(rc);
2930
2931         obt = &obd->u.obt;
2932         obt->obt_magic = OBT_MAGIC;
2933
2934         m->ofd_fmd_max_num = OFD_FMD_MAX_NUM_DEFAULT;
2935         m->ofd_fmd_max_age = OFD_FMD_MAX_AGE_DEFAULT;
2936
2937         spin_lock_init(&m->ofd_flags_lock);
2938         m->ofd_raid_degraded = 0;
2939         m->ofd_syncjournal = 0;
2940         ofd_slc_set(m);
2941         tgd->tgd_grant_compat_disable = 0;
2942         m->ofd_soft_sync_limit = OFD_SOFT_SYNC_LIMIT_DEFAULT;
2943
2944         /* statfs data */
2945         spin_lock_init(&tgd->tgd_osfs_lock);
2946         tgd->tgd_osfs_age = cfs_time_shift_64(-1000);
2947         tgd->tgd_osfs_unstable = 0;
2948         tgd->tgd_statfs_inflight = 0;
2949         tgd->tgd_osfs_inflight = 0;
2950
2951         /* grant data */
2952         spin_lock_init(&tgd->tgd_grant_lock);
2953         tgd->tgd_tot_dirty = 0;
2954         tgd->tgd_tot_granted = 0;
2955         tgd->tgd_tot_pending = 0;
2956
2957         m->ofd_seq_count = 0;
2958         init_waitqueue_head(&m->ofd_inconsistency_thread.t_ctl_waitq);
2959         INIT_LIST_HEAD(&m->ofd_inconsistency_list);
2960         spin_lock_init(&m->ofd_inconsistency_lock);
2961
2962         spin_lock_init(&m->ofd_batch_lock);
2963         init_rwsem(&m->ofd_lastid_rwsem);
2964
2965         m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
2966         m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
2967         /* set this lu_device to obd, because error handling need it */
2968         obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
2969
2970         rc = ofd_procfs_init(m);
2971         if (rc) {
2972                 CERROR("Can't init ofd lprocfs, rc %d\n", rc);
2973                 RETURN(rc);
2974         }
2975
2976         /* No connection accepted until configurations will finish */
2977         spin_lock(&obd->obd_dev_lock);
2978         obd->obd_no_conn = 1;
2979         spin_unlock(&obd->obd_dev_lock);
2980         obd->obd_replayable = 1;
2981         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
2982                 char *str = lustre_cfg_string(cfg, 4);
2983
2984                 if (strchr(str, 'n')) {
2985                         CWARN("%s: recovery disabled\n", obd->obd_name);
2986                         obd->obd_replayable = 0;
2987                 }
2988         }
2989
2990         info = ofd_info_init(env, NULL);
2991         if (info == NULL)
2992                 GOTO(err_fini_proc, rc = -EFAULT);
2993
2994         rc = ofd_stack_init(env, m, cfg);
2995         if (rc) {
2996                 CERROR("Can't init device stack, rc %d\n", rc);
2997                 GOTO(err_fini_proc, rc);
2998         }
2999
3000         ofd_procfs_add_brw_stats_symlink(m);
3001
3002         snprintf(info->fti_u.name, sizeof(info->fti_u.name), "%s-%s",
3003                  "filter"/*LUSTRE_OST_NAME*/, obd->obd_uuid.uuid);
3004         m->ofd_namespace = ldlm_namespace_new(obd, info->fti_u.name,
3005                                               LDLM_NAMESPACE_SERVER,
3006                                               LDLM_NAMESPACE_GREEDY,
3007                                               LDLM_NS_TYPE_OST);
3008         if (m->ofd_namespace == NULL)
3009                 GOTO(err_fini_stack, rc = -ENOMEM);
3010         /* set obd_namespace for compatibility with old code */
3011         obd->obd_namespace = m->ofd_namespace;
3012         ldlm_register_intent(m->ofd_namespace, ofd_intent_policy);
3013         m->ofd_namespace->ns_lvbo = &ofd_lvbo;
3014         m->ofd_namespace->ns_lvbp = m;
3015
3016         ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
3017                            "filter_ldlm_cb_client", &obd->obd_ldlm_client);
3018
3019         dt_conf_get(env, m->ofd_osd, &m->ofd_lut.lut_dt_conf);
3020
3021         rc = tgt_init(env, &m->ofd_lut, obd, m->ofd_osd, ofd_common_slice,
3022                       OBD_FAIL_OST_ALL_REQUEST_NET,
3023                       OBD_FAIL_OST_ALL_REPLY_NET);
3024         if (rc)
3025                 GOTO(err_free_ns, rc);
3026
3027         /* populate cached statfs data */
3028         osfs = &ofd_info(env)->fti_u.osfs;
3029         rc = tgt_statfs_internal(env, &m->ofd_lut, osfs, 0, NULL);
3030         if (rc != 0) {
3031                 CERROR("%s: can't get statfs data, rc %d\n", obd->obd_name, rc);
3032                 GOTO(err_fini_lut, rc);
3033         }
3034         if (!is_power_of_2(osfs->os_bsize)) {
3035                 CERROR("%s: blocksize (%d) is not a power of 2\n",
3036                         obd->obd_name, osfs->os_bsize);
3037                 GOTO(err_fini_lut, rc = -EPROTO);
3038         }
3039         tgd->tgd_blockbits = fls(osfs->os_bsize) - 1;
3040
3041         m->ofd_brw_size = m->ofd_lut.lut_dt_conf.ddp_brw_size;
3042         m->ofd_cksum_types_supported = cksum_types_supported_server();
3043         m->ofd_precreate_batch = OFD_PRECREATE_BATCH_DEFAULT;
3044         if (osfs->os_bsize * osfs->os_blocks < OFD_PRECREATE_SMALL_FS)
3045                 m->ofd_precreate_batch = OFD_PRECREATE_BATCH_SMALL;
3046
3047         rc = ofd_fs_setup(env, m, obd);
3048         if (rc)
3049                 GOTO(err_fini_lut, rc);
3050
3051         fid.f_seq = FID_SEQ_LOCAL_NAME;
3052         fid.f_oid = 1;
3053         fid.f_ver = 0;
3054         rc = local_oid_storage_init(env, m->ofd_osd, &fid,
3055                                     &m->ofd_los);
3056         if (rc != 0)
3057                 GOTO(err_fini_fs, rc);
3058
3059         nodemap_config = nm_config_file_register_tgt(env, m->ofd_osd,
3060                                                      m->ofd_los);
3061         if (IS_ERR(nodemap_config)) {
3062                 rc = PTR_ERR(nodemap_config);
3063                 if (rc != -EROFS)
3064                         GOTO(err_fini_los, rc);
3065         } else {
3066                 obt->obt_nodemap_config_file = nodemap_config;
3067         }
3068
3069         rc = ofd_start_inconsistency_verification_thread(m);
3070         if (rc != 0)
3071                 GOTO(err_fini_nm, rc);
3072
3073         tgt_adapt_sptlrpc_conf(&m->ofd_lut);
3074
3075         RETURN(0);
3076
3077 err_fini_nm:
3078         nm_config_file_deregister_tgt(env, obt->obt_nodemap_config_file);
3079         obt->obt_nodemap_config_file = NULL;
3080 err_fini_los:
3081         local_oid_storage_fini(env, m->ofd_los);
3082         m->ofd_los = NULL;
3083 err_fini_fs:
3084         ofd_fs_cleanup(env, m);
3085 err_fini_lut:
3086         tgt_fini(env, &m->ofd_lut);
3087 err_free_ns:
3088         ldlm_namespace_free(m->ofd_namespace, NULL, obd->obd_force);
3089         obd->obd_namespace = m->ofd_namespace = NULL;
3090 err_fini_stack:
3091         ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
3092 err_fini_proc:
3093         ofd_procfs_fini(m);
3094         return rc;
3095 }
3096
3097 /**
3098  * Stop the OFD device
3099  *
3100  * This function stops the OFD device and all its subsystems.
3101  * This is the end of OFD lifecycle.
3102  *
3103  * \param[in] env       execution environment
3104  * \param[in] m         OFD device
3105  */
3106 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
3107 {
3108         struct obd_device       *obd = ofd_obd(m);
3109         struct lu_device        *d   = &m->ofd_dt_dev.dd_lu_dev;
3110         struct lfsck_stop        stop;
3111
3112         stop.ls_status = LS_PAUSED;
3113         stop.ls_flags = 0;
3114         lfsck_stop(env, m->ofd_osd, &stop);
3115         target_recovery_fini(obd);
3116         if (m->ofd_namespace != NULL)
3117                 ldlm_namespace_free_prior(m->ofd_namespace, NULL,
3118                                           d->ld_obd->obd_force);
3119
3120         obd_exports_barrier(obd);
3121         obd_zombie_barrier();
3122
3123         tgt_fini(env, &m->ofd_lut);
3124         ofd_stop_inconsistency_verification_thread(m);
3125         lfsck_degister(env, m->ofd_osd);
3126         ofd_fs_cleanup(env, m);
3127         nm_config_file_deregister_tgt(env, obd->u.obt.obt_nodemap_config_file);
3128         obd->u.obt.obt_nodemap_config_file = NULL;
3129
3130         if (m->ofd_los != NULL) {
3131                 local_oid_storage_fini(env, m->ofd_los);
3132                 m->ofd_los = NULL;
3133         }
3134
3135         if (m->ofd_namespace != NULL) {
3136                 ldlm_namespace_free_post(m->ofd_namespace);
3137                 d->ld_obd->obd_namespace = m->ofd_namespace = NULL;
3138         }
3139
3140         ofd_stack_fini(env, m, &m->ofd_dt_dev.dd_lu_dev);
3141         ofd_procfs_fini(m);
3142         LASSERT(atomic_read(&d->ld_ref) == 0);
3143         server_put_mount(obd->obd_name, true);
3144         EXIT;
3145 }
3146
3147 /**
3148  * Implementation of lu_device_type_operations::ldto_device_fini.
3149  *
3150  * Finalize device. Dual to ofd_device_init(). It is called from
3151  * obd_precleanup() and stops the current device.
3152  *
3153  * \param[in] env       execution environment
3154  * \param[in] d         LU device of OFD
3155  *
3156  * \retval              NULL
3157  */
3158 static struct lu_device *ofd_device_fini(const struct lu_env *env,
3159                                          struct lu_device *d)
3160 {
3161         ENTRY;
3162         ofd_fini(env, ofd_dev(d));
3163         RETURN(NULL);
3164 }
3165
3166 /**
3167  * Implementation of lu_device_type_operations::ldto_device_free.
3168  *
3169  * Free OFD device. Dual to ofd_device_alloc().
3170  *
3171  * \param[in] env       execution environment
3172  * \param[in] d         LU device of OFD
3173  *
3174  * \retval              NULL
3175  */
3176 static struct lu_device *ofd_device_free(const struct lu_env *env,
3177                                          struct lu_device *d)
3178 {
3179         struct ofd_device *m = ofd_dev(d);
3180
3181         dt_device_fini(&m->ofd_dt_dev);
3182         OBD_FREE_PTR(m);
3183         RETURN(NULL);
3184 }
3185
3186 /**
3187  * Implementation of lu_device_type_operations::ldto_device_alloc.
3188  *
3189  * This function allocates the new OFD device. It is called from
3190  * obd_setup() if OBD device had lu_device_type defined.
3191  *
3192  * \param[in] env       execution environment
3193  * \param[in] t         lu_device_type of OFD device
3194  * \param[in] cfg       configuration log
3195  *
3196  * \retval              pointer to the lu_device of just allocated OFD
3197  * \retval              ERR_PTR of return value on error
3198  */
3199 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
3200                                           struct lu_device_type *t,
3201                                           struct lustre_cfg *cfg)
3202 {
3203         struct ofd_device *m;
3204         struct lu_device  *l;
3205         int                rc;
3206
3207         OBD_ALLOC_PTR(m);
3208         if (m == NULL)
3209                 return ERR_PTR(-ENOMEM);
3210
3211         l = &m->ofd_dt_dev.dd_lu_dev;
3212         dt_device_init(&m->ofd_dt_dev, t);
3213         rc = ofd_init0(env, m, t, cfg);
3214         if (rc != 0) {
3215                 ofd_device_free(env, l);
3216                 l = ERR_PTR(rc);
3217         }
3218
3219         return l;
3220 }
3221
3222 /* type constructor/destructor: ofd_type_init(), ofd_type_fini() */
3223 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
3224
3225 static struct lu_device_type_operations ofd_device_type_ops = {
3226         .ldto_init              = ofd_type_init,
3227         .ldto_fini              = ofd_type_fini,
3228
3229         .ldto_start             = ofd_type_start,
3230         .ldto_stop              = ofd_type_stop,
3231
3232         .ldto_device_alloc      = ofd_device_alloc,
3233         .ldto_device_free       = ofd_device_free,
3234         .ldto_device_fini       = ofd_device_fini
3235 };
3236
3237 static struct lu_device_type ofd_device_type = {
3238         .ldt_tags       = LU_DEVICE_DT,
3239         .ldt_name       = LUSTRE_OST_NAME,
3240         .ldt_ops        = &ofd_device_type_ops,
3241         .ldt_ctx_tags   = LCT_DT_THREAD
3242 };
3243
3244 /**
3245  * Initialize OFD module.
3246  *
3247  * This function is called upon module loading. It registers OFD device type
3248  * and prepares all in-memory structures used by all OFD devices.
3249  *
3250  * \retval              0 if successful
3251  * \retval              negative value on error
3252  */
3253 static int __init ofd_init(void)
3254 {
3255         int                             rc;
3256
3257         rc = lu_kmem_init(ofd_caches);
3258         if (rc)
3259                 return rc;
3260
3261         rc = ofd_fmd_init();
3262         if (rc) {
3263                 lu_kmem_fini(ofd_caches);
3264                 return(rc);
3265         }
3266
3267         rc = class_register_type(&ofd_obd_ops, NULL, true, NULL,
3268                                  LUSTRE_OST_NAME, &ofd_device_type);
3269         return rc;
3270 }
3271
3272 /**
3273  * Stop OFD module.
3274  *
3275  * This function is called upon OFD module unloading.
3276  * It frees all related structures and unregisters OFD device type.
3277  */
3278 static void __exit ofd_exit(void)
3279 {
3280         ofd_fmd_exit();
3281         lu_kmem_fini(ofd_caches);
3282         class_unregister_type(LUSTRE_OST_NAME);
3283 }
3284
3285 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3286 MODULE_DESCRIPTION("Lustre Object Filtering Device");
3287 MODULE_VERSION(LUSTRE_VERSION_STRING);
3288 MODULE_LICENSE("GPL");
3289
3290 module_init(ofd_init);
3291 module_exit(ofd_exit);