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