Whamcloud - gitweb
3df2027861020e092ab96ee7ed78a9110d495132
[fs/lustre-release.git] / lustre / include / dt_object.h
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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #ifndef __LUSTRE_DT_OBJECT_H
33 #define __LUSTRE_DT_OBJECT_H
34
35 /** \defgroup dt dt
36  * Sub-class of lu_object with methods common for "data" objects in OST stack.
37  *
38  * Data objects behave like regular files: you can read/write them, get and
39  * set their attributes. Implementation of dt interface is supposed to
40  * implement some form of garbage collection, normally reference counting
41  * (nlink) based one.
42  *
43  * Examples: osd (lustre/osd) is an implementation of dt interface.
44  * @{
45  */
46
47 #include <obd_support.h>
48 /*
49  * super-class definitions.
50  */
51 #include <lu_object.h>
52
53 #include <libcfs/libcfs.h>
54
55 struct seq_file;
56 struct proc_dir_entry;
57 struct lustre_cfg;
58
59 struct thandle;
60 struct dt_device;
61 struct dt_object;
62 struct dt_index_features;
63 struct niobuf_local;
64 struct niobuf_remote;
65 struct ldlm_enqueue_info;
66
67 typedef enum {
68         MNTOPT_USERXATTR        = 0x00000001,
69         MNTOPT_ACL              = 0x00000002,
70 } mntopt_t;
71
72 struct dt_device_param {
73         unsigned           ddp_max_name_len;
74         unsigned           ddp_max_nlink;
75         unsigned           ddp_symlink_max;
76         mntopt_t           ddp_mntopts;
77         unsigned           ddp_max_ea_size;
78         unsigned           ddp_mount_type;
79         unsigned long long ddp_maxbytes;
80         /* per-inode space consumption */
81         short              ddp_inodespace;
82         /* maximum number of blocks in an extent */
83         unsigned           ddp_max_extent_blks;
84         /* per-extent insertion overhead to be used by client for grant
85          * calculation */
86         unsigned int       ddp_extent_tax;
87         unsigned int       ddp_brw_size;        /* optimal RPC size */
88         /* T10PI checksum type, zero if not supported */
89         enum cksum_types   ddp_t10_cksum_type;
90         bool               ddp_has_lseek_data_hole;
91 };
92
93 /**
94  * Per-transaction commit callback function
95  */
96 struct dt_txn_commit_cb;
97 typedef void (*dt_cb_t)(struct lu_env *env, struct thandle *th,
98                         struct dt_txn_commit_cb *cb, int err);
99 /**
100  * Special per-transaction callback for cases when just commit callback
101  * is needed and per-device callback are not convenient to use
102  */
103 #define TRANS_COMMIT_CB_MAGIC   0xa0a00a0a
104 #define MAX_COMMIT_CB_STR_LEN   32
105
106 #define DCB_TRANS_STOP          0x1
107 struct dt_txn_commit_cb {
108         struct list_head        dcb_linkage;
109         dt_cb_t                 dcb_func;
110         void                    *dcb_data;
111         __u32                   dcb_magic;
112         __u32                   dcb_flags;
113         char                    dcb_name[MAX_COMMIT_CB_STR_LEN];
114 };
115
116 /**
117  * Operations on dt device.
118  */
119 struct dt_device_operations {
120         /**
121          * Return device-wide statistics.
122          *
123          * Return device-wide stats including block size, total and
124          * free blocks, total and free objects, etc. See struct obd_statfs
125          * for the details.
126          *
127          * \param[in] env       execution environment for this thread
128          * \param[in] dev       dt device
129          * \param[out] osfs     stats information
130          *
131          * \retval 0            on success
132          * \retval negative     negated errno on error
133          */
134         int   (*dt_statfs)(const struct lu_env *env,
135                            struct dt_device *dev,
136                            struct obd_statfs *osfs,
137                            struct obd_statfs_info *info);
138
139         /**
140          * Create transaction.
141          *
142          * Create in-memory structure representing the transaction for the
143          * caller. The structure returned will be used by the calling thread
144          * to specify the transaction the updates belong to. Once created
145          * successfully ->dt_trans_stop() must be called in any case (with
146          * ->dt_trans_start() and updates or not) so that the transaction
147          * handle and other resources can be released by the layers below.
148          *
149          * \param[in] env       execution environment for this thread
150          * \param[in] dev       dt device
151          *
152          * \retval pointer to handle    if creation succeeds
153          * \retval ERR_PTR(errno)       if creation fails
154          */
155         struct thandle *(*dt_trans_create)(const struct lu_env *env,
156                                            struct dt_device *dev);
157
158         /**
159          * Start transaction.
160          *
161          * Start the transaction. The transaction described by \a th can be
162          * started only once. Another start is considered as an error.
163          * A thread is not supposed to start a transaction while another
164          * transaction isn't closed by the thread (though multiple handles
165          * can be created). The caller should start the transaction once
166          * all possible updates are declared (see the ->do_declare_* methods
167          * below) and all the needed resources are reserved.
168          *
169          * \param[in] env       execution environment for this thread
170          * \param[in] dev       dt device
171          * \param[in] th        transaction handle
172          *
173          * \retval 0            on success
174          * \retval negative     negated errno on error
175          */
176         int   (*dt_trans_start)(const struct lu_env *env,
177                                 struct dt_device *dev,
178                                 struct thandle *th);
179
180         /**
181          * Stop transaction.
182          *
183          * Once stopped the transaction described by \a th is complete (all
184          * the needed updates are applied) and further processing such as
185          * flushing to disk, sending to another target, etc, is handled by
186          * lower layers. The caller can't access this transaction by the
187          * handle anymore (except from the commit callbacks, see below).
188          *
189          * \param[in] env       execution environment for this thread
190          * \param[in] dev       dt device
191          * \param[in] th        transaction handle
192          *
193          * \retval 0            on success
194          * \retval negative     negated errno on error
195          */
196         int   (*dt_trans_stop)(const struct lu_env *env,
197                                struct dt_device *dev,
198                                struct thandle *th);
199
200         /**
201          * Add commit callback to the transaction.
202          *
203          * Add a commit callback to the given transaction handle. The callback
204          * will be called when the associated transaction is stored. I.e. the
205          * transaction will survive an event like power off if the callback did
206          * run. The number of callbacks isn't limited, but you should note that
207          * some disk filesystems do handle the commit callbacks in the thread
208          * handling commit/flush of all the transactions, meaning that new
209          * transactions are blocked from commit and flush until all the
210          * callbacks are done. Also, note multiple callbacks can be running
211          * concurrently using multiple CPU cores. The callbacks will be running
212          * in a special environment which can not be used to pass data around.
213          *
214          * \param[in] th        transaction handle
215          * \param[in] dcb       commit callback description
216          *
217          * \retval 0            on success
218          * \retval negative     negated errno on error
219          */
220         int   (*dt_trans_cb_add)(struct thandle *th,
221                                  struct dt_txn_commit_cb *dcb);
222
223         /**
224          * Return FID of root index object.
225          *
226          * Return the FID of the root object in the filesystem. This object
227          * is usually provided as a bootstrap point by a disk filesystem.
228          * This is up to the implementation which FID to use, though
229          * [FID_SEQ_ROOT:1:0] is reserved for this purpose.
230          *
231          * \param[in] env       execution environment for this thread
232          * \param[in] dev       dt device
233          * \param[out] fid      FID of the root object
234          *
235          * \retval 0            on success
236          * \retval negative     negated errno on error
237          */
238         int   (*dt_root_get)(const struct lu_env *env,
239                              struct dt_device *dev,
240                              struct lu_fid *f);
241
242         /**
243          * Return device configuration data.
244          *
245          * Return device (disk fs, actually) specific configuration.
246          * The configuration isn't subject to change at runtime.
247          * See struct dt_device_param for the details.
248          *
249          * \param[in] env       execution environment for this thread
250          * \param[in] dev       dt device
251          * \param[out] param    configuration parameters
252          */
253         void  (*dt_conf_get)(const struct lu_env *env,
254                              const struct dt_device *dev,
255                              struct dt_device_param *param);
256
257         /**
258          * Return device's super block.
259          *
260          * \param[in] dev       dt device
261          */
262         struct super_block *(*dt_mnt_sb_get)(const struct dt_device *dev);
263
264         /**
265          * Sync the device.
266          *
267          * Sync all the cached state (dirty buffers, pages, etc) to the
268          * persistent storage. The method returns control once the sync is
269          * complete. This operation may incur significant I/O to disk and
270          * should be reserved for cases where a global sync is strictly
271          * necessary.
272          *
273          * \param[in] env       execution environment for this thread
274          * \param[in] dev       dt device
275          *
276          * \retval 0            on success
277          * \retval negative     negated errno on error
278          */
279         int   (*dt_sync)(const struct lu_env *env,
280                          struct dt_device *dev);
281
282         /**
283          * Make device read-only.
284          *
285          * Prevent new modifications to the device. This is a very specific
286          * state where all the changes are accepted successfully and the
287          * commit callbacks are called, but persistent state never changes.
288          * Used only in the tests to simulate power-off scenario.
289          *
290          * \param[in] env       execution environment for this thread
291          * \param[in] dev       dt device
292          *
293          * \retval 0            on success
294          * \retval negative     negated errno on error
295          */
296         int   (*dt_ro)(const struct lu_env *env,
297                        struct dt_device *dev);
298
299         /**
300          * Start transaction commit asynchronously.
301          *
302
303          * Provide a hint to the underlying filesystem that it should start
304          * committing soon. The control returns immediately. It's up to the
305          * layer implementing the method how soon to start committing. Usually
306          * this should be throttled to some extent, otherwise the number of
307          * aggregated transaction goes too high causing performance drop.
308          *
309          * \param[in] env       execution environment for this thread
310          * \param[in] dev       dt device
311          *
312          * \retval 0            on success
313          * \retval negative     negated errno on error
314          */
315         int   (*dt_commit_async)(const struct lu_env *env,
316                                  struct dt_device *dev);
317
318         /**
319          * The unit of \a count is byte for block or inodes for metadata.
320          *
321          * If \a count > 0, reserve quota in advance of an operation that
322          * changes the quota assignment, such as chgrp() or rename() into
323          * a directory with a different group ID.
324          *
325          * If \a count < 0, free the reserved quota previously.
326          *
327          * \param[in] env       execution environment for this thread
328          * \param[in] dev       the bottom OSD device to reserve quota
329          * \param[in] type      quota type (LQUOTA_RES_DT or LQUOTA_RES_MD)
330          * \param[in] uid       quota uid
331          * \param[in] gid       quota gid
332          * \param[in] count     space (bytes or inodes) to reserve or free
333          * \param[in] md        true for inode, false for block
334          *
335          * \retval 0            on success
336          * \retval negative     negated errno on error
337          */
338         int   (*dt_reserve_or_free_quota)(const struct lu_env *env,
339                                           struct dt_device *dev,
340                                           enum quota_type type, __u64 uid,
341                                           __u64 gid, __s64 count, bool md);
342 };
343
344 struct dt_index_features {
345         /** required feature flags from enum dt_index_flags */
346         __u32 dif_flags;
347         /** minimal required key size */
348         size_t dif_keysize_min;
349         /** maximal required key size, 0 if no limit */
350         size_t dif_keysize_max;
351         /** minimal required record size */
352         size_t dif_recsize_min;
353         /** maximal required record size, 0 if no limit */
354         size_t dif_recsize_max;
355         /** pointer size for record */
356         size_t dif_ptrsize;
357 };
358
359 enum dt_index_flags {
360         /** index supports variable sized keys */
361         DT_IND_VARKEY = BIT(0),
362         /** index supports variable sized records */
363         DT_IND_VARREC = BIT(1),
364         /** index can be modified */
365         DT_IND_UPDATE = BIT(2),
366         /** index supports records with non-unique (duplicate) keys */
367         DT_IND_NONUNQ = BIT(3),
368         /**
369          * index support fixed-size keys sorted with natural numerical way
370          * and is able to return left-side value if no exact value found
371          */
372         DT_IND_RANGE = BIT(4),
373 };
374
375 /* for dt_read_lock() and dt_write_lock() object lock rule */
376 enum dt_object_role {
377         DT_SRC_PARENT,
378         DT_SRC_CHILD,
379         DT_TGT_PARENT,
380         DT_TGT_CHILD,
381         DT_TGT_ORPHAN,
382         DT_LASTID,
383 };
384
385 /**
386  * Features, required from index to support file system directories (mapping
387  * names to fids).
388  */
389 extern const struct dt_index_features dt_directory_features;
390 extern const struct dt_index_features dt_otable_features;
391 extern const struct dt_index_features dt_lfsck_layout_orphan_features;
392 extern const struct dt_index_features dt_lfsck_layout_dangling_features;
393 extern const struct dt_index_features dt_lfsck_namespace_features;
394
395 /* index features supported by the accounting objects */
396 extern const struct dt_index_features dt_acct_features;
397
398 /* index features supported by the quota global indexes */
399 extern const struct dt_index_features dt_quota_glb_features;
400
401 /* index features supported by the quota slave indexes */
402 extern const struct dt_index_features dt_quota_slv_features;
403
404 /* index features supported by the nodemap index */
405 extern const struct dt_index_features dt_nodemap_features;
406
407 /**
408  * This is a general purpose dt allocation hint.
409  * It now contains the parent object.
410  * It can contain any allocation hint in the future.
411  */
412 struct dt_allocation_hint {
413         struct dt_object        *dah_parent;
414         const void              *dah_eadata;
415         int                     dah_eadata_len;
416         int                     dah_acl_len;
417         __u32                   dah_mode;
418         int                     dah_append_stripes;
419         bool                    dah_can_block;
420         char                    *dah_append_pool;
421 };
422
423 /**
424  * object type specifier.
425  */
426
427 enum dt_format_type {
428         DFT_REGULAR,
429         DFT_DIR,
430         /** for mknod */
431         DFT_NODE,
432         /** for special index */
433         DFT_INDEX,
434         /** for symbolic link */
435         DFT_SYM,
436 };
437
438 /**
439  * object format specifier.
440  */
441 struct dt_object_format {
442         /** type for dt object */
443         enum dt_format_type dof_type;
444         union {
445                 struct dof_regular {
446                         int striped;
447                 } dof_reg;
448                 struct dof_dir {
449                 } dof_dir;
450                 struct dof_node {
451                 } dof_node;
452                 /**
453                  * special index need feature as parameter to create
454                  * special idx
455                  */
456                 struct dof_index {
457                         const struct dt_index_features *di_feat;
458                 } dof_idx;
459         } u;
460 };
461
462 enum dt_format_type dt_mode_to_dft(__u32 mode);
463
464 typedef __u64 dt_obj_version_t;
465
466 union ldlm_policy_data;
467
468 struct md_layout_change;
469
470 /**
471  * A dt_object provides common operations to create and destroy
472  * objects and to manage regular and extended attributes.
473  */
474 struct dt_object_operations {
475         /**
476          * Get read lock on object.
477          *
478          * Read lock is compatible with other read locks, so it's shared.
479          * Read lock is not compatible with write lock which is exclusive.
480          * The lock is blocking and can't be used from an interrupt context.
481          *
482          * \param[in] env       execution environment for this thread
483          * \param[in] dt        object to lock for reading
484          * \param[in] role      a hint to debug locks (see kernel's mutexes)
485          */
486         void  (*do_read_lock)(const struct lu_env *env,
487                               struct dt_object *dt,
488                               unsigned role);
489
490         /*
491          * Get write lock on object.
492          *
493          * Write lock is exclusive and cannot be shared. The lock is blocking
494          * and can't be used from an interrupt context.
495          *
496          * \param[in] env       execution environment for this thread
497          * \param[in] dt        object to lock for writing
498          * \param[in] role      a hint to debug locks (see kernel's mutexes)
499          *
500          */
501         void  (*do_write_lock)(const struct lu_env *env,
502                                struct dt_object *dt,
503                                unsigned role);
504
505         /**
506          * Release read lock.
507          *
508          * \param[in] env       execution environment for this thread
509          * \param[in] dt        object
510          */
511         void  (*do_read_unlock)(const struct lu_env *env,
512                                 struct dt_object *dt);
513
514         /**
515          * Release write lock.
516          *
517          * \param[in] env       execution environment for this thread
518          * \param[in] dt        object
519          */
520         void  (*do_write_unlock)(const struct lu_env *env,
521                                  struct dt_object *dt);
522
523         /**
524          * Check whether write lock is held.
525          *
526          * The caller can learn whether write lock is held on the object
527          *
528          * \param[in] env       execution environment for this thread
529          * \param[in] dt        object
530          *
531          * \retval 0            no write lock
532          * \retval 1            write lock is held
533          */
534         int  (*do_write_locked)(const struct lu_env *env,
535                                 struct dt_object *dt);
536
537         /**
538          * Declare intention to request reqular attributes.
539          *
540          * Notity the underlying filesystem that the caller may request regular
541          * attributes with ->do_attr_get() soon. This allows OSD to implement
542          * prefetching logic in an object-oriented manner. The implementation
543          * can be noop. This method should avoid expensive delays such as
544          * waiting on disk I/O, otherwise the goal of enabling a performance
545          * optimization would be defeated.
546          *
547          * \param[in] env       execution environment for this thread
548          * \param[in] dt        object
549          *
550          * \retval 0            on success
551          * \retval negative     negated errno on error
552          */
553         int   (*do_declare_attr_get)(const struct lu_env *env,
554                                      struct dt_object *dt);
555
556         /**
557          * Return regular attributes.
558          *
559          * The object must exist. Currently all the attributes should be
560          * returned, but in the future this can be improved so that only
561          * a selected set is returned. This can improve performance as in
562          * some cases attributes are stored in different places and
563          * getting them all can be an iterative and expensive process.
564          *
565          * \param[in] env       execution environment for this thread
566          * \param[in] dt        object
567          * \param[out] attr     attributes to fill
568          *
569          * \retval 0            on success
570          * \retval negative     negated errno on error
571          */
572         int   (*do_attr_get)(const struct lu_env *env,
573                              struct dt_object *dt,
574                              struct lu_attr *attr);
575
576         /**
577          * Declare intention to change regular object's attributes.
578          *
579          * Notify the underlying filesystem that the regular attributes may
580          * change in this transaction. This enables the layer below to prepare
581          * resources (e.g. journal credits in ext4).  This method should be
582          * called between creating the transaction and starting it. Note that
583          * the la_valid field of \a attr specifies which attributes will change.
584          * The object need not exist.
585          *
586          * \param[in] env       execution environment for this thread
587          * \param[in] dt        object
588          * \param[in] attr      attributes to change specified in attr.la_valid
589          * \param[in] th        transaction handle
590          *
591          * \retval 0            on success
592          * \retval negative     negated errno on error
593          */
594         int   (*do_declare_attr_set)(const struct lu_env *env,
595                                      struct dt_object *dt,
596                                      const struct lu_attr *attr,
597                                      struct thandle *th);
598
599         /**
600          * Change regular attributes.
601          *
602          * Change regular attributes in the given transaction. Note only
603          * attributes flagged by attr.la_valid change. The object must
604          * exist. If the layer implementing this method is responsible for
605          * quota, then the method should maintain object accounting for the
606          * given credentials when la_uid/la_gid changes.
607          *
608          * \param[in] env       execution environment for this thread
609          * \param[in] dt        object
610          * \param[in] attr      new attributes to apply
611          * \param[in] th        transaction handle
612          *
613          * \retval 0            on success
614          * \retval negative     negated errno on error
615          */
616         int   (*do_attr_set)(const struct lu_env *env,
617                              struct dt_object *dt,
618                              const struct lu_attr *attr,
619                              struct thandle *th);
620
621         /**
622          * Declare intention to request extented attribute.
623          *
624          * Notify the underlying filesystem that the caller may request extended
625          * attribute with ->do_xattr_get() soon. This allows OSD to implement
626          * prefetching logic in an object-oriented manner. The implementation
627          * can be noop. This method should avoid expensive delays such as
628          * waiting on disk I/O, otherwise the goal of enabling a performance
629          * optimization would be defeated.
630          *
631          * \param[in] env       execution environment for this thread
632          * \param[in] dt        object
633          * \param[in] buf       unused, may be removed in the future
634          * \param[in] name      name of the extended attribute
635          *
636          * \retval 0            on success
637          * \retval negative     negated errno on error
638          */
639         int   (*do_declare_xattr_get)(const struct lu_env *env,
640                                       struct dt_object *dt,
641                                       struct lu_buf *buf,
642                                       const char *name);
643
644         /**
645          * Return a value of an extended attribute.
646          *
647          * The object must exist. If the buffer is NULL, then the method
648          * must return the size of the value.
649          *
650          * \param[in] env       execution environment for this thread
651          * \param[in] dt        object
652          * \param[out] buf      buffer in which to store the value
653          * \param[in] name      name of the extended attribute
654          *
655          * \retval 0            on success
656          * \retval -ERANGE      if \a buf is too small
657          * \retval negative     negated errno on error
658          * \retval positive     value's size if \a buf is NULL or has zero size
659          */
660         int   (*do_xattr_get)(const struct lu_env *env,
661                               struct dt_object *dt,
662                               struct lu_buf *buf,
663                               const char *name);
664
665         /**
666          * Declare intention to change an extended attribute.
667          *
668          * Notify the underlying filesystem that the extended attribute may
669          * change in this transaction.  This enables the layer below to prepare
670          * resources (e.g. journal credits in ext4).  This method should be
671          * called between creating the transaction and starting it. The object
672          * need not exist.
673          *
674          * \param[in] env       execution environment for this thread
675          * \param[in] dt        object
676          * \param[in] buf       buffer storing new value of the attribute
677          * \param[in] name      name of the attribute
678          * \param[in] fl        LU_XATTR_CREATE - fail if EA exists
679          *                      LU_XATTR_REPLACE - fail if EA doesn't exist
680          * \param[in] th        transaction handle
681          *
682          * \retval 0            on success
683          * \retval negative     negated errno on error
684          */
685         int   (*do_declare_xattr_set)(const struct lu_env *env,
686                                       struct dt_object *dt,
687                                       const struct lu_buf *buf,
688                                       const char *name,
689                                       int fl,
690                                       struct thandle *th);
691
692         /**
693          * Set an extended attribute.
694          *
695          * Change or replace the specified extended attribute (EA).
696          * The flags passed in \a fl dictate whether the EA is to be
697          * created or replaced, as follows.
698          *   LU_XATTR_CREATE - fail if EA exists
699          *   LU_XATTR_REPLACE - fail if EA doesn't exist
700          * The object must exist.
701          *
702          * \param[in] env       execution environment for this thread
703          * \param[in] dt        object
704          * \param[in] buf       buffer storing new value of the attribute
705          * \param[in] name      name of the attribute
706          * \param[in] fl        flags indicating EA creation or replacement
707          * \param[in] th        transaction handle
708          *
709          * \retval 0            on success
710          * \retval negative     negated errno on error
711          */
712         int   (*do_xattr_set)(const struct lu_env *env,
713                               struct dt_object *dt,
714                               const struct lu_buf *buf,
715                               const char *name,
716                               int fl,
717                               struct thandle *th);
718
719         /**
720          * Declare intention to delete an extended attribute.
721          *
722          * Notify the underlying filesystem that the extended attribute may
723          * be deleted in this transaction. This enables the layer below to
724          * prepare resources (e.g. journal credits in ext4).  This method
725          * should be called between creating the transaction and starting it.
726          * The object need not exist.
727          *
728          * \param[in] env       execution environment for this thread
729          * \param[in] dt        object
730          * \param[in] name      name of the attribute
731          * \param[in] th        transaction handle
732          *
733          * \retval 0            on success
734          * \retval negative     negated errno on error
735          */
736         int   (*do_declare_xattr_del)(const struct lu_env *env,
737                                       struct dt_object *dt,
738                                       const char *name,
739                                       struct thandle *th);
740
741         /**
742          * Delete an extended attribute.
743          *
744          * This method deletes the specified extended attribute. The object
745          * must exist.
746          *
747          * \param[in] env       execution environment for this thread
748          * \param[in] dt        object
749          * \param[in] name      name of the attribute
750          * \param[in] th        transaction handle
751          *
752          * \retval 0            on success
753          * \retval negative     negated errno on error
754          */
755         int   (*do_xattr_del)(const struct lu_env *env,
756                               struct dt_object *dt,
757                               const char *name,
758                               struct thandle *th);
759
760         /**
761          * Return a list of the extended attributes.
762          *
763          * Fills the passed buffer with a list of the extended attributes
764          * found in the object. The names are separated with '\0'.
765          * The object must exist.
766          *
767          * \param[in] env       execution environment for this thread
768          * \param[in] dt        object
769          * \param[out] buf      buffer to put the list in
770          *
771          * \retval positive     bytes used/required in the buffer
772          * \retval negative     negated errno on error
773          */
774         int   (*do_xattr_list)(const struct lu_env *env,
775                                struct dt_object *dt,
776                                const struct lu_buf *buf);
777
778         /**
779          * Prepare allocation hint for a new object.
780          *
781          * This method is used by the caller to inform OSD of the parent-child
782          * relationship between two objects and enable efficient object
783          * allocation. Filled allocation hint will be passed to ->do_create()
784          * later.
785          *
786          * \param[in] env       execution environment for this thread
787          * \param[out] ah       allocation hint
788          * \param[in] parent    parent object (can be NULL)
789          * \param[in] child     child object
790          * \param[in] _mode     type of the child object
791          */
792         void  (*do_ah_init)(const struct lu_env *env,
793                             struct dt_allocation_hint *ah,
794                             struct dt_object *parent,
795                             struct dt_object *child,
796                             umode_t mode);
797
798         /**
799          * Declare intention to create a new object.
800          *
801          * Notify the underlying filesystem that the object may be created
802          * in this transaction. This enables the layer below to prepare
803          * resources (e.g. journal credits in ext4).  This method should be
804          * called between creating the transaction and starting it.
805          *
806          * If the layer implementing this method is responsible for quota,
807          * then the method should reserve an object for the given credentials
808          * and return an error if quota is over. If object creation later
809          * fails for some reason, then the reservation should be released
810          * properly (usually in ->dt_trans_stop()).
811          *
812          * \param[in] env       execution environment for this thread
813          * \param[in] dt        object
814          * \param[in] attr      attributes of the new object
815          * \param[in] hint      allocation hint
816          * \param[in] dof       object format
817          * \param[in] th        transaction handle
818          *
819          * \retval 0            on success
820          * \retval negative     negated errno on error
821          */
822         int   (*do_declare_create)(const struct lu_env *env,
823                                    struct dt_object *dt,
824                                    struct lu_attr *attr,
825                                    struct dt_allocation_hint *hint,
826                                    struct dt_object_format *dof,
827                                    struct thandle *th);
828
829         /**
830          * Create new object.
831          *
832          * The method creates the object passed with the specified attributes
833          * and object format. Object allocation procedure can use information
834          * stored in the allocation hint. Different object formats are supported
835          * (see enum dt_format_type and struct dt_object_format) depending on
836          * the device. If creation succeeds, then LOHA_EXISTS flag must be set
837          * in the LU-object header attributes.
838          *
839          * If the layer implementing this method is responsible for quota,
840          * then the method should maintain object accounting for the given
841          * credentials.
842          *
843          * \param[in] env       execution environment for this thread
844          * \param[in] dt        object
845          * \param[in] attr      attributes of the new object
846          * \param[in] hint      allocation hint
847          * \param[in] dof       object format
848          * \param[in] th        transaction handle
849          *
850          * \retval 0            on success
851          * \retval negative     negated errno on error
852          */
853         int   (*do_create)(const struct lu_env *env,
854                            struct dt_object *dt,
855                            struct lu_attr *attr,
856                            struct dt_allocation_hint *hint,
857                            struct dt_object_format *dof,
858                            struct thandle *th);
859
860         /**
861          * Declare intention to destroy an object.
862          *
863          * Notify the underlying filesystem that the object may be destroyed
864          * in this transaction. This enables the layer below to prepare
865          * resources (e.g. journal credits in ext4).  This method should be
866          * called between creating the transaction and starting it. The object
867          * need not exist.
868          *
869          * \param[in] env       execution environment for this thread
870          * \param[in] dt        object
871          * \param[in] th        transaction handle
872          *
873          * \retval 0            on success
874          * \retval negative     negated errno on error
875          */
876         int   (*do_declare_destroy)(const struct lu_env *env,
877                                     struct dt_object *dt,
878                                     struct thandle *th);
879
880         /**
881          * Destroy an object.
882          *
883          * This method destroys the object and all the resources associated
884          * with the object (data, key/value pairs, extended attributes, etc).
885          * The object must exist. If destroy is successful, then flag
886          * LU_OBJECT_HEARD_BANSHEE should be set to forbid access to this
887          * instance of in-core object. Any subsequent access to the same FID
888          * should get another instance with no LOHA_EXIST flag set.
889          *
890          * If the layer implementing this method is responsible for quota,
891          * then the method should maintain object accounting for the given
892          * credentials.
893          *
894          * \param[in] env       execution environment for this thread
895          * \param[in] dt        object
896          * \param[in] th        transaction handle
897          *
898          * \retval 0            on success
899          * \retval negative     negated errno on error
900          */
901         int   (*do_destroy)(const struct lu_env *env,
902                             struct dt_object *dt,
903                             struct thandle *th);
904
905         /**
906          * Try object as an index.
907          *
908          * Announce that this object is going to be used as an index. This
909          * operation checks that object supports indexing operations and
910          * installs appropriate dt_index_operations vector on success.
911          * Also probes for features. Operation is successful if all required
912          * features are supported. It's not possible to access the object
913          * with index methods before ->do_index_try() returns success.
914          *
915          * \param[in] env       execution environment for this thread
916          * \param[in] dt        object
917          * \param[in] feat      index features
918          *
919          * \retval 0            on success
920          * \retval negative     negated errno on error
921          */
922         int   (*do_index_try)(const struct lu_env *env,
923                               struct dt_object *dt,
924                               const struct dt_index_features *feat);
925
926         /**
927          * Declare intention to increment nlink count.
928          *
929          * Notify the underlying filesystem that the nlink regular attribute
930          * be changed in this transaction. This enables the layer below to
931          * prepare resources (e.g. journal credits in ext4).  This method
932          * should be called between creating the transaction and starting it.
933          * The object need not exist.
934          *
935          * \param[in] env       execution environment for this thread
936          * \param[in] dt        object
937          * \param[in] th        transaction handle
938          *
939          * \retval 0            on success
940          * \retval negative     negated errno on error
941          */
942         int   (*do_declare_ref_add)(const struct lu_env *env,
943                                     struct dt_object *dt,
944                                     struct thandle *th);
945
946         /**
947          * Increment nlink.
948          *
949          * Increment nlink (from the regular attributes set) in the given
950          * transaction. Note the absolute limit for nlink should be learnt
951          * from struct dt_device_param::ddp_max_nlink. The object must exist.
952          *
953          * \param[in] env       execution environment for this thread
954          * \param[in] dt        object
955          * \param[in] th        transaction handle
956          *
957          * \retval 0            on success
958          * \retval negative     negated errno on error
959          */
960         int   (*do_ref_add)(const struct lu_env *env,
961                             struct dt_object *dt, struct thandle *th);
962
963         /**
964          * Declare intention to decrement nlink count.
965          *
966          * Notify the underlying filesystem that the nlink regular attribute
967          * be changed in this transaction. This enables the layer below to
968          * prepare resources (e.g. journal credits in ext4).  This method
969          * should be called between creating the transaction and starting it.
970          * The object need not exist.
971          *
972          * \param[in] env       execution environment for this thread
973          * \param[in] dt        object
974          * \param[in] th        transaction handle
975          *
976          * \retval 0            on success
977          * \retval negative     negated errno on error
978          */
979         int   (*do_declare_ref_del)(const struct lu_env *env,
980                                     struct dt_object *dt,
981                                     struct thandle *th);
982
983         /**
984          * Decrement nlink.
985          *
986          * Decrement nlink (from the regular attributes set) in the given
987          * transaction. The object must exist.
988          *
989          * \param[in] env       execution environment for this thread
990          * \param[in] dt        object
991          * \param[in] th        transaction handle
992          *
993          * \retval 0            on success
994          * \retval negative     negated errno on error
995          */
996         int   (*do_ref_del)(const struct lu_env *env,
997                             struct dt_object *dt,
998                             struct thandle *th);
999
1000         /**
1001          * Sync obect.
1002          *
1003          * The method is called to sync specified range of the object to a
1004          * persistent storage. The control is returned once the operation is
1005          * complete. The difference from ->do_sync() is that the object can
1006          * be in-sync with the persistent storage (nothing to flush), then
1007          * the method returns quickly with no I/O overhead. So, this method
1008          * should be preferred over ->do_sync() where possible. Also note that
1009          * if the object isn't clean, then some disk filesystems will call
1010          * ->do_sync() to maintain overall consistency, in which case it's
1011          * still very expensive.
1012          *
1013          * \param[in] env       execution environment for this thread
1014          * \param[in] dt        object
1015          * \param[in] start     start of the range to sync
1016          * \param[in] end       end of the range to sync
1017          *
1018          * \retval 0            on success
1019          * \retval negative     negated errno on error
1020          */
1021         int (*do_object_sync)(const struct lu_env *env, struct dt_object *obj,
1022                               __u64 start, __u64 end);
1023
1024         /**
1025          * Lock object.
1026          *
1027          * Lock object(s) using Distributed Lock Manager (LDLM).
1028          *
1029          * Get LDLM locks for the object. Currently used to lock "remote"
1030          * objects in DNE configuration - a service running on MDTx needs
1031          * to lock an object on MDTy.
1032          *
1033          * \param[in] env       execution environment for this thread
1034          * \param[in] dt        object
1035          * \param[out] lh       lock handle, sometimes used, sometimes not
1036          * \param[in] einfo     ldlm callbacks, locking type and mode
1037          * \param[out] einfo    private data to be passed to unlock later
1038          * \param[in] policy    inodebits data
1039          *
1040          * \retval 0            on success
1041          * \retval negative     negated errno on error
1042          */
1043         int (*do_object_lock)(const struct lu_env *env, struct dt_object *dt,
1044                               struct lustre_handle *lh,
1045                               struct ldlm_enqueue_info *einfo,
1046                               union ldlm_policy_data *policy);
1047
1048         /**
1049          * Unlock object.
1050          *
1051          * Release LDLM lock(s) granted with ->do_object_lock().
1052          *
1053          * \param[in] env       execution environment for this thread
1054          * \param[in] dt        object
1055          * \param[in] einfo     lock handles, from ->do_object_lock()
1056          * \param[in] policy    inodebits data
1057          *
1058          * \retval 0            on success
1059          * \retval negative     negated errno on error
1060          */
1061         int (*do_object_unlock)(const struct lu_env *env,
1062                                 struct dt_object *dt,
1063                                 struct ldlm_enqueue_info *einfo,
1064                                 union ldlm_policy_data *policy);
1065
1066         /**
1067          * Invalidate attribute cache.
1068          *
1069          * This method invalidate attribute cache of the object, which is on OSP
1070          * only.
1071          *
1072          * \param[in] env       execution envionment for this thread
1073          * \param[in] dt        object
1074          *
1075          * \retval 0            on success
1076          * \retval negative     negated errno on error
1077          */
1078         int   (*do_invalidate)(const struct lu_env *env, struct dt_object *dt);
1079
1080         /**
1081          * Check object stale state.
1082          *
1083          * OSP only.
1084          *
1085          * \param[in] dt        object
1086          *
1087          * \retval true         for stale object
1088          * \retval false        for not stale object
1089          */
1090         bool (*do_check_stale)(struct dt_object *dt);
1091
1092         /**
1093          * Declare intention to instaintiate extended layout component.
1094          *
1095          * \param[in] env       execution environment
1096          * \param[in] dt        DT object
1097          * \param[in] layout    data structure to describe the changes to
1098          *                      the DT object's layout
1099          * \param[in] buf       buffer containing client's lovea or empty
1100          *
1101          * \retval 0            success
1102          * \retval -ne          error code
1103          */
1104         int (*do_declare_layout_change)(const struct lu_env *env,
1105                                         struct dt_object *dt,
1106                                         struct md_layout_change *mlc,
1107                                         struct thandle *th);
1108
1109         /**
1110          * Client is trying to write to un-instantiated layout component.
1111          *
1112          * \param[in] env       execution environment
1113          * \param[in] dt        DT object
1114          * \param[in] layout    data structure to describe the changes to
1115          *                      the DT object's layout
1116          * \param[in] buf       buffer containing client's lovea or empty
1117          *
1118          * \retval 0            success
1119          * \retval -ne          error code
1120          */
1121         int (*do_layout_change)(const struct lu_env *env, struct dt_object *dt,
1122                                 struct md_layout_change *mlc,
1123                                 struct thandle *th);
1124 };
1125
1126 enum dt_bufs_type {
1127         DT_BUFS_TYPE_READ       = 0x0000,
1128         DT_BUFS_TYPE_WRITE      = 0x0001,
1129         DT_BUFS_TYPE_READAHEAD  = 0x0002,
1130         DT_BUFS_TYPE_LOCAL      = 0x0004,
1131 };
1132
1133 /**
1134  * Per-dt-object operations on "file body" - unstructure raw data.
1135  */
1136 struct dt_body_operations {
1137         /**
1138          * Read data.
1139          *
1140          * Read unstructured data from an existing regular object.
1141          * Only data before attr.la_size is returned.
1142          *
1143          * \param[in] env       execution environment for this thread
1144          * \param[in] dt        object
1145          * \param[out] buf      buffer (including size) to copy data in
1146          * \param[in] pos       position in the object to start
1147          * \param[out] pos      original value of \a pos + bytes returned
1148          *
1149          * \retval positive     bytes read on success
1150          * \retval negative     negated errno on error
1151          */
1152         ssize_t (*dbo_read)(const struct lu_env *env,
1153                             struct dt_object *dt,
1154                             struct lu_buf *buf,
1155                             loff_t *pos);
1156
1157         /**
1158          * Declare intention to write data to object.
1159          *
1160          * Notify the underlying filesystem that data may be written in
1161          * this transaction. This enables the layer below to prepare resources
1162          * (e.g. journal credits in ext4).  This method should be called
1163          * between creating the transaction and starting it. The object need
1164          * not exist. If the layer implementing this method is responsible for
1165          * quota, then the method should reserve space for the given credentials
1166          * and return an error if quota is over. If the write later fails
1167          * for some reason, then the reserve should be released properly
1168          * (usually in ->dt_trans_stop()).
1169          *
1170          * \param[in] env       execution environment for this thread
1171          * \param[in] dt        object
1172          * \param[in] buf       buffer (including size) to copy data from
1173          * \param[in] pos       position in the object to start
1174          * \param[in] th        transaction handle
1175          *
1176          * \retval 0            on success
1177          * \retval negative     negated errno on error
1178          */
1179         ssize_t (*dbo_declare_write)(const struct lu_env *env,
1180                                      struct dt_object *dt,
1181                                      const struct lu_buf *buf,
1182                                      loff_t pos,
1183                                      struct thandle *th);
1184
1185         /**
1186          * Write unstructured data to regular existing object.
1187          *
1188          * The method allocates space and puts data in. Also, the method should
1189          * maintain attr.la_size properly. Partial writes are possible.
1190          *
1191          * If the layer implementing this method is responsible for quota,
1192          * then the method should maintain space accounting for the given
1193          * credentials.
1194          *
1195          * \param[in] env       execution environment for this thread
1196          * \param[in] dt        object
1197          * \param[in] buf       buffer (including size) to copy data from
1198          * \param[in] pos       position in the object to start
1199          * \param[out] pos      \a pos + bytes written
1200          * \param[in] th        transaction handle
1201          *
1202          * \retval positive     bytes written on success
1203          * \retval negative     negated errno on error
1204          */
1205         ssize_t (*dbo_write)(const struct lu_env *env,
1206                              struct dt_object *dt,
1207                              const struct lu_buf *buf,
1208                              loff_t *pos,
1209                              struct thandle *th);
1210
1211         /**
1212          * Return buffers for data.
1213          *
1214          * This method is used to access data with no copying. It's so-called
1215          * zero-copy I/O. The method returns the descriptors for the internal
1216          * buffers where data are managed by the disk filesystem. For example,
1217          * pagecache in case of ext4 or ARC with ZFS. Then other components
1218          * (e.g. networking) can transfer data from or to the buffers with no
1219          * additional copying.
1220          *
1221          * The method should fill an array of struct niobuf_local, where
1222          * each element describes a full or partial page for data at specific
1223          * offset. The caller should use page/lnb_page_offset/len to find data
1224          * at object's offset lnb_file_offset.
1225          *
1226          * The memory referenced by the descriptors can't change its purpose
1227          * until the complementary ->dbo_bufs_put() is called. The caller should
1228          * specify if the buffers are used to read or modify data so that OSD
1229          * can decide how to initialize the buffers: bring all the data for
1230          * reads or just bring partial buffers for write. Note: the method does
1231          * not check whether output array is large enough.
1232          *
1233          * \param[in] env       execution environment for this thread
1234          * \param[in] dt        object
1235          * \param[in] pos       position in the object to start
1236          * \param[in] len       size of region in bytes
1237          * \param[out] lb       array of descriptors to fill
1238          * \param[in] maxlnb    max slots in @lnb array
1239          * \param[in] rw        0 if used to read, 1 if used for write
1240          *
1241          * \retval positive     number of descriptors on success
1242          * \retval negative     negated errno on error
1243          */
1244         int (*dbo_bufs_get)(const struct lu_env *env,
1245                             struct dt_object *dt,
1246                             loff_t pos,
1247                             ssize_t len,
1248                             struct niobuf_local *lb,
1249                             int maxlnb,
1250                             enum dt_bufs_type rw);
1251
1252         /**
1253          * Release reference granted by ->dbo_bufs_get().
1254          *
1255          * Release the reference granted by the previous ->dbo_bufs_get().
1256          * Note the references are counted.
1257          *
1258          * \param[in] env       execution environment for this thread
1259          * \param[in] dt        object
1260          * \param[out] lb       array of descriptors to fill
1261          * \param[in] nr        size of the array
1262          *
1263          * \retval 0            on success
1264          * \retval negative     negated errno on error
1265          */
1266         int (*dbo_bufs_put)(const struct lu_env *env,
1267                             struct dt_object *dt,
1268                             struct niobuf_local *lb,
1269                             int nr);
1270
1271         /**
1272          * Prepare buffers for reading.
1273          *
1274          * The method is called on the given buffers to fill them with data
1275          * if that wasn't done in ->dbo_bufs_get(). The idea is that the
1276          * caller should be able to get few buffers for discontiguous regions
1277          * using few calls to ->dbo_bufs_get() and then request them all for
1278          * the preparation with a single call, so that OSD can fire many I/Os
1279          * to run concurrently. It's up to the specific OSD whether to implement
1280          * this logic in ->dbo_read_prep() or just use ->dbo_bufs_get() to
1281          * prepare data for every requested region individually.
1282          *
1283          * \param[in] env       execution environment for this thread
1284          * \param[in] dt        object
1285          * \param[in] lnb       array of buffer descriptors
1286          * \param[in] nr        size of the array
1287          *
1288          * \retval 0            on success
1289          * \retval negative     negated errno on error
1290          */
1291         int (*dbo_read_prep)(const struct lu_env *env,
1292                              struct dt_object *dt,
1293                              struct niobuf_local *lnb,
1294                              int nr);
1295
1296         /**
1297          * Prepare buffers for write.
1298          *
1299          * This method is called on the given buffers to ensure the partial
1300          * buffers contain correct data. The underlying idea is the same as
1301          * in ->db_read_prep().
1302          *
1303          * \param[in] env       execution environment for this thread
1304          * \param[in] dt        object
1305          * \param[in] lb        array of buffer descriptors
1306          * \param[in] nr        size of the array
1307          *
1308          * \retval 0            on success
1309          * \retval negative     negated errno on error
1310          */
1311         int (*dbo_write_prep)(const struct lu_env *env,
1312                               struct dt_object *dt,
1313                               struct niobuf_local *lb,
1314                               int nr);
1315
1316         /**
1317          * Declare intention to write data stored in the buffers.
1318          *
1319          * Notify the underlying filesystem that data may be written in
1320          * this transaction. This enables the layer below to prepare resources
1321          * (e.g. journal credits in ext4).  This method should be called
1322          * between creating the transaction and starting it.
1323          *
1324          * If the layer implementing this method is responsible for quota,
1325          * then the method should be reserving a space for the given
1326          * credentials and return an error if quota is exceeded. If the write
1327          * later fails for some reason, then the reserve should be released
1328          * properly (usually in ->dt_trans_stop()).
1329          *
1330          * \param[in] env       execution environment for this thread
1331          * \param[in] dt        object
1332          * \param[in] lb        array of descriptors
1333          * \param[in] nr        size of the array
1334          * \param[in] th        transaction handle
1335          *
1336          * \retval 0            on success
1337          * \retval negative     negated errno on error
1338          */
1339         int (*dbo_declare_write_commit)(const struct lu_env *env,
1340                                         struct dt_object *dt,
1341                                         struct niobuf_local *lb,
1342                                         int nr,
1343                                         struct thandle *th);
1344
1345         /**
1346          * Write to existing object.
1347          *
1348          * This method is used to write data to a persistent storage using
1349          * the buffers returned by ->dbo_bufs_get(). The caller puts new
1350          * data into the buffers using own mechanisms (e.g. direct transfer
1351          * from a NIC). The method should maintain attr.la_size. Also,
1352          * attr.la_blocks should be maintained but this can be done in lazy
1353          * manner, when actual allocation happens.
1354          *
1355          * If the layer implementing this method is responsible for quota,
1356          * then the method should maintain space accounting for the given
1357          * credentials.
1358          *
1359          * user_size parameter is the apparent size of the file, ie the size
1360          * of the clear text version of the file. It can differ from the actual
1361          * amount of valuable data received when a file is encrypted,
1362          * because encrypted pages always contain PAGE_SIZE bytes of data,
1363          * even if clear text data is only a few bytes.
1364          * In case of encrypted file, apparent size will be stored as the inode
1365          * size, so that servers return to clients an object size they can use
1366          * to determine clear text size.
1367          *
1368          * \param[in] env       execution environment for this thread
1369          * \param[in] dt        object
1370          * \param[in] lb        array of descriptors for the buffers
1371          * \param[in] nr        size of the array
1372          * \param[in] th        transaction handle
1373          * \param[in] user_size apparent size
1374          *
1375          * \retval 0            on success
1376          * \retval negative     negated errno on error
1377          */
1378         int (*dbo_write_commit)(const struct lu_env *env,
1379                                 struct dt_object *dt,
1380                                 struct niobuf_local *lb,
1381                                 int nr,
1382                                 struct thandle *th,
1383                                 __u64 user_size);
1384
1385         /**
1386          * Return logical to physical block mapping for a given extent
1387          *
1388          * \param[in] env       execution environment for this thread
1389          * \param[in] dt        object
1390          * \param[in] fm        describe the region to map and the output buffer
1391          *                      see the details in include/linux/fiemap.h
1392          *
1393          * \retval 0            on success
1394          * \retval negative     negated errno on error
1395          */
1396         int (*dbo_fiemap_get)(const struct lu_env *env,
1397                               struct dt_object *dt,
1398                               struct fiemap *fm);
1399
1400         /**
1401          * Declare intention to deallocate space from an object.
1402          *
1403          * Notify the underlying filesystem that space may be deallocated in
1404          * this transactions. This enables the layer below to prepare resources
1405          * (e.g. journal credits in ext4).  This method should be called between
1406          * creating the transaction and starting it. The object need not exist.
1407          *
1408          * \param[in] env       execution environment for this thread
1409          * \param[in] dt        object
1410          * \param[in] start     the start of the region to deallocate
1411          * \param[in] end       the end of the region to deallocate
1412          * \param[in] th        transaction handle
1413          *
1414          * \retval 0            on success
1415          * \retval negative     negated errno on error
1416          */
1417         int   (*dbo_declare_punch)(const struct lu_env *env,
1418                                    struct dt_object *dt,
1419                                    __u64 start,
1420                                    __u64 end,
1421                                    struct thandle *th);
1422
1423         /**
1424          * Deallocate specified region in an object.
1425          *
1426          * This method is used to deallocate (release) space possibly consumed
1427          * by the given region of the object. If the layer implementing this
1428          * method is responsible for quota, then the method should maintain
1429          * space accounting for the given credentials.
1430          *
1431          * \param[in] env       execution environment for this thread
1432          * \param[in] dt        object
1433          * \param[in] start     the start of the region to deallocate
1434          * \param[in] end       the end of the region to deallocate
1435          * \param[in] th        transaction handle
1436          *
1437          * \retval 0            on success
1438          * \retval negative     negated errno on error
1439          */
1440         int   (*dbo_punch)(const struct lu_env *env,
1441                            struct dt_object *dt,
1442                            __u64 start,
1443                            __u64 end,
1444                            struct thandle *th);
1445         /**
1446          * Give advices on specified region in an object.
1447          *
1448          * This method is used to give advices about access pattern on an
1449          * given region of the object. The disk filesystem understands
1450          * the advices and tunes cache/read-ahead policies.
1451          *
1452          * \param[in] env       execution environment for this thread
1453          * \param[in] dt        object
1454          * \param[in] start     the start of the region affected
1455          * \param[in] end       the end of the region affected
1456          * \param[in] advice    advice type
1457          *
1458          * \retval 0            on success
1459          * \retval negative     negated errno on error
1460          */
1461         int   (*dbo_ladvise)(const struct lu_env *env,
1462                              struct dt_object *dt,
1463                              __u64 start,
1464                              __u64 end,
1465                              enum lu_ladvise_type advice);
1466
1467         /**
1468          * Declare intention to preallocate space for an object
1469          *
1470          * \param[in] env       execution environment for this thread
1471          * \param[in] dt        object
1472          * \param[in] th        transaction handle
1473          *
1474          * \retval 0            on success
1475          * \retval negative     negated errno on error
1476          */
1477         int (*dbo_declare_fallocate)(const struct lu_env *env,
1478                                     struct dt_object *dt, __u64 start,
1479                                     __u64 end, int mode, struct thandle *th);
1480         /**
1481          * Allocate specified region for an object
1482          *
1483          * \param[in] env       execution environment for this thread
1484          * \param[in] dt        object
1485          * \param[in] start     the start of the region to allocate
1486          * \param[in] end       the end of the region to allocate
1487          * \param[in] mode      fallocate mode
1488          * \param[in] th        transaction handle
1489          *
1490          * \retval 0            on success
1491          * \retval negative     negated errno on error
1492          */
1493         int (*dbo_fallocate)(const struct lu_env *env,
1494                             struct dt_object *dt,
1495                             __u64 start,
1496                             __u64 end,
1497                             int mode,
1498                             struct thandle *th);
1499         /**
1500          * Do SEEK_HOLE/SEEK_DATA request on object
1501          *
1502          * \param[in] env       execution environment for this thread
1503          * \param[in] dt        object
1504          * \param[in] offset    the offset to start seek from
1505          * \param[in] whence    seek mode, SEEK_HOLE or SEEK_DATA
1506          *
1507          * \retval hole/data offset     on success
1508          * \retval negative             negated errno on error
1509          */
1510         loff_t (*dbo_lseek)(const struct lu_env *env, struct dt_object *dt,
1511                             loff_t offset, int whence);
1512 };
1513
1514 /**
1515  * Incomplete type of index record.
1516  */
1517 struct dt_rec;
1518
1519 /**
1520  * Incomplete type of index key.
1521  */
1522 struct dt_key;
1523
1524 /**
1525  * Incomplete type of dt iterator.
1526  */
1527 struct dt_it;
1528
1529 /**
1530  * Per-dt-object operations on object as index. Index is a set of key/value
1531  * pairs abstracted from an on-disk representation. An index supports the
1532  * number of operations including lookup by key, insert and delete. Also,
1533  * an index can be iterated to find the pairs one by one, from a beginning
1534  * or specified point.
1535  */
1536 struct dt_index_operations {
1537         /**
1538          * Lookup in an index by key.
1539          *
1540          * The method returns a value for the given key. Key/value format
1541          * and size should have been negotiated with ->do_index_try() before.
1542          * Thus it's the caller's responsibility to provide the method with
1543          * proper key and big enough buffer. No external locking is required,
1544          * all the internal consistency should be implemented by the method
1545          * or lower layers. The object should should have been created with
1546          * type DFT_INDEX or DFT_DIR.
1547          *
1548          * \param[in] env       execution environment for this thread
1549          * \param[in] dt        object
1550          * \param[out] rec      buffer where value will be stored
1551          * \param[in] key       key
1552          *
1553          * \retval 0            on success
1554          * \retval -ENOENT      if key isn't found
1555          * \retval negative     negated errno on error
1556          */
1557         int (*dio_lookup)(const struct lu_env *env,
1558                           struct dt_object *dt,
1559                           struct dt_rec *rec,
1560                           const struct dt_key *key);
1561
1562         /**
1563          * Declare intention to insert a key/value into an index.
1564          *
1565          * Notify the underlying filesystem that new key/value may be inserted
1566          * in this transaction. This enables the layer below to prepare
1567          * resources (e.g. journal credits in ext4). This method should be
1568          * called between creating the transaction and starting it. key/value
1569          * format and size is subject to ->do_index_try().
1570          *
1571          * \param[in] env       execution environment for this thread
1572          * \param[in] dt        object
1573          * \param[in] rec       buffer storing value
1574          * \param[in] key       key
1575          * \param[in] th        transaction handle
1576          *
1577          * \retval 0            on success
1578          * \retval negative     negated errno on error
1579          */
1580         int (*dio_declare_insert)(const struct lu_env *env,
1581                                   struct dt_object *dt,
1582                                   const struct dt_rec *rec,
1583                                   const struct dt_key *key,
1584                                   struct thandle *th);
1585
1586         /**
1587          * Insert a new key/value pair into an index.
1588          *
1589          * The method inserts specified key/value pair into the given index
1590          * object. The internal consistency is maintained by the method or
1591          * the functionality below. The format and size of key/value should
1592          * have been negotiated before using ->do_index_try(), no additional
1593          * information can be specified to the method. The keys are unique
1594          * in a given index.
1595          *
1596          * \param[in] env       execution environment for this thread
1597          * \param[in] dt        object
1598          * \param[in] rec       buffer storing value
1599          * \param[in] key       key
1600          * \param[in] th        transaction handle
1601          *
1602          * \retval 0            on success
1603          * \retval negative     negated errno on error
1604          */
1605         int (*dio_insert)(const struct lu_env *env,
1606                           struct dt_object *dt,
1607                           const struct dt_rec *rec,
1608                           const struct dt_key *key,
1609                           struct thandle *th);
1610
1611         /**
1612          * Declare intention to delete a key/value from an index.
1613          *
1614          * Notify the underlying filesystem that key/value may be deleted in
1615          * this transaction. This enables the layer below to prepare resources
1616          * (e.g. journal credits in ext4).  This method should be called
1617          * between creating the transaction and starting it. Key/value format
1618          * and size is subject to ->do_index_try(). The object need not exist.
1619          *
1620          * \param[in] env       execution environment for this thread
1621          * \param[in] dt        object
1622          * \param[in] key       key
1623          * \param[in] th        transaction handle
1624          *
1625          * \retval 0            on success
1626          * \retval negative     negated errno on error
1627          */
1628         int (*dio_declare_delete)(const struct lu_env *env,
1629                                   struct dt_object *dt,
1630                                   const struct dt_key *key,
1631                                   struct thandle *th);
1632
1633         /**
1634          * Delete key/value pair from an index.
1635          *
1636          * The method deletes specified key and corresponding value from the
1637          * given index object. The internal consistency is maintained by the
1638          * method or the functionality below. The format and size of the key
1639          * should have been negotiated before using ->do_index_try(), no
1640          * additional information can be specified to the method.
1641          *
1642          * \param[in] env       execution environment for this thread
1643          * \param[in] dt        object
1644          * \param[in] key       key
1645          * \param[in] th        transaction handle
1646          *
1647          * \retval 0            on success
1648          * \retval negative     negated errno on error
1649          */
1650         int (*dio_delete)(const struct lu_env *env,
1651                           struct dt_object *dt,
1652                           const struct dt_key *key,
1653                           struct thandle *th);
1654
1655         /**
1656          * Iterator interface.
1657          *
1658          * Methods to iterate over an existing index, list the keys stored and
1659          * associated values, get key/value size, etc.
1660          */
1661         struct dt_it_ops {
1662                 /**
1663                  * Allocate and initialize new iterator.
1664                  *
1665                  * The iterator is a handler to be used in the subsequent
1666                  * methods to access index's content. Note the position is
1667                  * not defined at this point and should be initialized with
1668                  * ->get() or ->load() method.
1669                  *
1670                  * \param[in] env       execution environment for this thread
1671                  * \param[in] dt        object
1672                  * \param[in] attr      ask the iterator to return part of
1673                                         the records, see LUDA_* for details
1674                  *
1675                  * \retval pointer      iterator pointer on success
1676                  * \retval ERR_PTR(errno)       on error
1677                  */
1678                 struct dt_it *(*init)(const struct lu_env *env,
1679                                       struct dt_object *dt,
1680                                       __u32 attr);
1681
1682                 /**
1683                  * Release iterator.
1684                  *
1685                  * Release the specified iterator and all the resources
1686                  * associated (e.g. the object, index cache, etc).
1687                  *
1688                  * \param[in] env       execution environment for this thread
1689                  * \param[in] di        iterator to release
1690                  */
1691                 void          (*fini)(const struct lu_env *env,
1692                                       struct dt_it *di);
1693
1694                 /**
1695                  * Move position of iterator.
1696                  *
1697                  * Move the position of the specified iterator to the specified
1698                  * key.
1699                  *
1700                  * \param[in] env       execution environment for this thread
1701                  * \param[in] di        iterator
1702                  * \param[in] key       key to position to
1703                  *
1704                  * \retval 0            if exact key is found
1705                  * \retval 1            if at the record with least key
1706                  *                      not larger than the key
1707                  * \retval negative     negated errno on error
1708                  */
1709                 int            (*get)(const struct lu_env *env,
1710                                       struct dt_it *di,
1711                                       const struct dt_key *key);
1712
1713                 /**
1714                  * Release position
1715                  *
1716                  * Complimentary method for dt_it_ops::get() above. Some
1717                  * implementation can increase a reference on the iterator in
1718                  * dt_it_ops::get(). So the caller should be able to release
1719                  * with dt_it_ops::put().
1720                  *
1721                  * \param[in] env       execution environment for this thread
1722                  * \param[in] di        iterator
1723                  */
1724                 void           (*put)(const struct lu_env *env,
1725                                       struct dt_it *di);
1726
1727                 /**
1728                  * Move to next record.
1729                  *
1730                  * Moves the position of the iterator to a next record
1731                  *
1732                  * \param[in] env       execution environment for this thread
1733                  * \param[in] di        iterator
1734                  *
1735                  * \retval 1            if no more records
1736                  * \retval 0            on success, the next record is found
1737                  * \retval negative     negated errno on error
1738                  */
1739                 int           (*next)(const struct lu_env *env,
1740                                       struct dt_it *di);
1741
1742                 /**
1743                  * Return key.
1744                  *
1745                  * Returns a pointer to a buffer containing the key of the
1746                  * record at the current position. The pointer is valid and
1747                  * retains data until ->get(), ->load() and ->fini() methods
1748                  * are called.
1749                  *
1750                  * \param[in] env       execution environment for this thread
1751                  * \param[in] di        iterator
1752                  *
1753                  * \retval pointer to key       on success
1754                  * \retval ERR_PTR(errno)       on error
1755                  */
1756                 struct dt_key *(*key)(const struct lu_env *env,
1757                                       const struct dt_it *di);
1758
1759                 /**
1760                  * Return key size.
1761                  *
1762                  * Returns size of the key at the current position.
1763                  *
1764                  * \param[in] env       execution environment for this thread
1765                  * \param[in] di        iterator
1766                  *
1767                  * \retval key's size   on success
1768                  * \retval negative     negated errno on error
1769                  */
1770                 int       (*key_size)(const struct lu_env *env,
1771                                       const struct dt_it *di);
1772
1773                 /**
1774                  * Return record.
1775                  *
1776                  * Stores the value of the record at the current position. The
1777                  * buffer must be big enough (as negotiated with
1778                  * ->do_index_try() or ->rec_size()). The caller can specify
1779                  * she is interested only in part of the record, using attr
1780                  * argument (see LUDA_* definitions for the details).
1781                  *
1782                  * \param[in] env       execution environment for this thread
1783                  * \param[in] di        iterator
1784                  * \param[out] rec      buffer to store value in
1785                  * \param[in] attr      specify part of the value to copy
1786                  *
1787                  * \retval 0            on success
1788                  * \retval negative     negated errno on error
1789                  */
1790                 int            (*rec)(const struct lu_env *env,
1791                                       const struct dt_it *di,
1792                                       struct dt_rec *rec,
1793                                       __u32 attr);
1794
1795                 /**
1796                  * Return record size.
1797                  *
1798                  * Returns size of the record at the current position. The
1799                  * \a attr can be used to specify only the parts of the record
1800                  * needed to be returned. (see LUDA_* definitions for the
1801                  * details).
1802                  *
1803                  * \param[in] env       execution environment for this thread
1804                  * \param[in] di        iterator
1805                  * \param[in] attr      part of the record to return
1806                  *
1807                  * \retval record's size        on success
1808                  * \retval negative             negated errno on error
1809                  */
1810                 int        (*rec_size)(const struct lu_env *env,
1811                                        const struct dt_it *di,
1812                                       __u32 attr);
1813
1814                 /**
1815                  * Return a cookie (hash).
1816                  *
1817                  * Returns the cookie (usually hash) of the key at the current
1818                  * position. This allows the caller to resume iteration at this
1819                  * position later. The exact value is specific to implementation
1820                  * and should not be interpreted by the caller.
1821                  *
1822                  * \param[in] env       execution environment for this thread
1823                  * \param[in] di        iterator
1824                  *
1825                  * \retval cookie/hash of the key
1826                  */
1827                 __u64        (*store)(const struct lu_env *env,
1828                                       const struct dt_it *di);
1829
1830                 /**
1831                  * Initialize position using cookie/hash.
1832                  *
1833                  * Initializes the current position of the iterator to one
1834                  * described by the cookie/hash as returned by ->store()
1835                  * previously.
1836                  *
1837                  * \param[in] env       execution environment for this thread
1838                  * \param[in] di        iterator
1839                  * \param[in] hash      cookie/hash value
1840                  *
1841                  * \retval positive     if current position points to
1842                  *                      record with least cookie not larger
1843                  *                      than cookie
1844                  * \retval 0            if current position matches cookie
1845                  * \retval negative     negated errno on error
1846                  */
1847                 int           (*load)(const struct lu_env *env,
1848                                       const struct dt_it *di,
1849                                       __u64 hash);
1850
1851                 /**
1852                  * Not used
1853                  */
1854                 int        (*key_rec)(const struct lu_env *env,
1855                                       const struct dt_it *di,
1856                                       void *key_rec);
1857         } dio_it;
1858 };
1859
1860 enum dt_otable_it_valid {
1861         DOIV_ERROR_HANDLE       = 0x0001,
1862         DOIV_DRYRUN             = 0x0002,
1863 };
1864
1865 enum dt_otable_it_flags {
1866         /* Exit when fail. */
1867         DOIF_FAILOUT    = 0x0001,
1868
1869         /* Reset iteration position to the device beginning. */
1870         DOIF_RESET      = 0x0002,
1871
1872         /* There is up layer component uses the iteration. */
1873         DOIF_OUTUSED    = 0x0004,
1874
1875         /* Check only without repairing. */
1876         DOIF_DRYRUN     = 0x0008,
1877 };
1878
1879 /* otable based iteration needs to use the common DT iteration APIs.
1880  * To initialize the iteration, it needs call dio_it::init() firstly.
1881  * Here is how the otable based iteration should prepare arguments to
1882  * call dt_it_ops::init().
1883  *
1884  * For otable based iteration, the 32-bits 'attr' for dt_it_ops::init()
1885  * is composed of two parts:
1886  * low 16-bits is for valid bits, high 16-bits is for flags bits. */
1887 #define DT_OTABLE_IT_FLAGS_SHIFT        16
1888 #define DT_OTABLE_IT_FLAGS_MASK         0xffff0000
1889
1890 struct dt_device {
1891         struct lu_device                   dd_lu_dev;
1892         const struct dt_device_operations *dd_ops;
1893
1894         /**
1895          * List of dt_txn_callback (see below). This is not protected in any
1896          * way, because callbacks are supposed to be added/deleted only during
1897          * single-threaded start-up shut-down procedures.
1898          */
1899         struct list_head                   dd_txn_callbacks;
1900         unsigned int                       dd_record_fid_accessed:1,
1901                                            dd_rdonly:1;
1902
1903         /* sysfs and debugfs handling */
1904         struct dentry                     *dd_debugfs_entry;
1905
1906         const struct attribute           **dd_def_attrs;
1907         struct kobject                     dd_kobj;
1908         struct kobj_type                   dd_ktype;
1909         struct completion                  dd_kobj_unregister;
1910 };
1911
1912 int  dt_device_init(struct dt_device *dev, struct lu_device_type *t);
1913 void dt_device_fini(struct dt_device *dev);
1914
1915 static inline int lu_device_is_dt(const struct lu_device *d)
1916 {
1917         return ergo(d != NULL, d->ld_type->ldt_tags & LU_DEVICE_DT);
1918 }
1919
1920 static inline struct dt_device * lu2dt_dev(struct lu_device *l)
1921 {
1922         LASSERT(lu_device_is_dt(l));
1923         return container_of_safe(l, struct dt_device, dd_lu_dev);
1924 }
1925
1926 struct dt_object {
1927         struct lu_object                   do_lu;
1928         const struct dt_object_operations *do_ops;
1929         const struct dt_body_operations   *do_body_ops;
1930         const struct dt_index_operations  *do_index_ops;
1931 };
1932
1933 /*
1934  * In-core representation of per-device local object OID storage
1935  */
1936 struct local_oid_storage {
1937         /* all initialized llog systems on this node linked by this */
1938         struct list_head  los_list;
1939
1940         /* how many handle's reference this los has */
1941         atomic_t          los_refcount;
1942         struct dt_device *los_dev;
1943         struct dt_object *los_obj;
1944
1945         /* data used to generate new fids */
1946         struct mutex      los_id_lock;
1947         __u64             los_seq;
1948         __u32             los_last_oid;
1949 };
1950
1951 static inline struct lu_device *dt2lu_dev(struct dt_device *d)
1952 {
1953         return &d->dd_lu_dev;
1954 }
1955
1956 static inline struct dt_object *lu2dt(struct lu_object *l)
1957 {
1958         LASSERT(l == NULL || IS_ERR(l) || lu_device_is_dt(l->lo_dev));
1959         return container_of_safe(l, struct dt_object, do_lu);
1960 }
1961
1962 int  dt_object_init(struct dt_object *obj,
1963                     struct lu_object_header *h, struct lu_device *d);
1964
1965 void dt_object_fini(struct dt_object *obj);
1966
1967 static inline int dt_object_exists(const struct dt_object *dt)
1968 {
1969         return lu_object_exists(&dt->do_lu);
1970 }
1971
1972 static inline int dt_object_remote(const struct dt_object *dt)
1973 {
1974         return lu_object_remote(&dt->do_lu);
1975 }
1976
1977 static inline struct dt_object *lu2dt_obj(struct lu_object *o)
1978 {
1979         LASSERT(ergo(o != NULL, lu_device_is_dt(o->lo_dev)));
1980         return container_of_safe(o, struct dt_object, do_lu);
1981 }
1982
1983 static inline struct dt_object *dt_object_child(struct dt_object *o)
1984 {
1985         return container_of(lu_object_next(&(o)->do_lu),
1986                             struct dt_object, do_lu);
1987 }
1988
1989 struct dt_quota_reserve_rec {
1990         enum quota_type  qrr_type;
1991         union lquota_id  qrr_id;
1992         __u64            qrr_count;
1993 };
1994
1995 /**
1996  * This is the general purpose transaction handle.
1997  * 1. Transaction Life Cycle
1998  *      This transaction handle is allocated upon starting a new transaction,
1999  *      and deallocated after this transaction is committed.
2000  * 2. Transaction Nesting
2001  *      We do _NOT_ support nested transaction. So, every thread should only
2002  *      have one active transaction, and a transaction only belongs to one
2003  *      thread. Due to this, transaction handle need no reference count.
2004  * 3. Transaction & dt_object locking
2005  *      dt_object locks should be taken inside transaction.
2006  * 4. Transaction & RPC
2007  *      No RPC request should be issued inside transaction.
2008  */
2009 struct thandle {
2010         /** the dt device on which the transactions are executed */
2011         struct dt_device *th_dev;
2012
2013         /* point to the top thandle, XXX this is a bit hacky right now,
2014          * but normal device trans callback triggered by the bottom
2015          * device (OSP/OSD == sub thandle layer) needs to get the
2016          * top_thandle (see dt_txn_hook_start/stop()), so we put the
2017          * top thandle here for now, will fix it when we have better
2018          * callback mechanism */
2019         struct thandle  *th_top;
2020
2021         /* reserved quota for this handle */
2022         struct dt_quota_reserve_rec th_reserved_quota;
2023
2024         /** the last operation result in this transaction.
2025          * this value is used in recovery */
2026         __s32             th_result;
2027
2028         /** whether we need sync commit */
2029         unsigned int            th_sync:1,
2030         /* local transation, no need to inform other layers */
2031                                 th_local:1,
2032         /* Whether we need wait the transaction to be submitted
2033          * (send to remote target) */
2034                                 th_wait_submit:1,
2035         /* complex transaction which will track updates on all targets,
2036          * including OSTs */
2037                                 th_complex:1,
2038         /* whether ignore quota */
2039                                 th_ignore_quota:1,
2040         /* whether restart transaction */
2041                                 th_restart_tran:1;
2042 };
2043
2044 /**
2045  * Transaction call-backs.
2046  *
2047  * These are invoked by osd (or underlying transaction engine) when
2048  * transaction changes state.
2049  *
2050  * Call-backs are used by upper layers to modify transaction parameters and to
2051  * perform some actions on for each transaction state transition. Typical
2052  * example is mdt registering call-back to write into last-received file
2053  * before each transaction commit.
2054  */
2055 struct dt_txn_callback {
2056         int (*dtc_txn_start)(const struct lu_env *env,
2057                              struct thandle *txn, void *cookie);
2058         int (*dtc_txn_stop)(const struct lu_env *env,
2059                             struct thandle *txn, void *cookie);
2060         void                    *dtc_cookie;
2061         __u32                   dtc_tag;
2062         struct list_head        dtc_linkage;
2063 };
2064
2065 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb);
2066 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb);
2067
2068 int dt_txn_hook_start(const struct lu_env *env,
2069                       struct dt_device *dev, struct thandle *txn);
2070 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *txn);
2071
2072 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj);
2073
2074 /**
2075  * Callback function used for parsing path.
2076  * \see llo_store_resolve
2077  */
2078 typedef int (*dt_entry_func_t)(const struct lu_env *env,
2079                             const char *name,
2080                             void *pvt);
2081
2082 #define DT_MAX_PATH 1024
2083
2084 int dt_path_parser(const struct lu_env *env,
2085                    char *local, dt_entry_func_t entry_func,
2086                    void *data);
2087
2088 struct dt_object *
2089 dt_store_resolve(const struct lu_env *env, struct dt_device *dt,
2090                  const char *path, struct lu_fid *fid);
2091
2092 struct dt_object *dt_store_open(const struct lu_env *env,
2093                                 struct dt_device *dt,
2094                                 const char *dirname,
2095                                 const char *filename,
2096                                 struct lu_fid *fid);
2097
2098 struct dt_object *dt_find_or_create(const struct lu_env *env,
2099                                     struct dt_device *dt,
2100                                     const struct lu_fid *fid,
2101                                     struct dt_object_format *dof,
2102                                     struct lu_attr *attr);
2103
2104 struct dt_object *dt_locate_at(const struct lu_env *env,
2105                                struct dt_device *dev,
2106                                const struct lu_fid *fid,
2107                                struct lu_device *top_dev,
2108                                const struct lu_object_conf *conf);
2109
2110 static inline struct dt_object *
2111 dt_locate(const struct lu_env *env, struct dt_device *dev,
2112           const struct lu_fid *fid)
2113 {
2114         return dt_locate_at(env, dev, fid,
2115                             dev->dd_lu_dev.ld_site->ls_top_dev, NULL);
2116 }
2117
2118 static inline struct dt_object *
2119 dt_object_locate(struct dt_object *dto, struct dt_device *dt_dev)
2120 {
2121         struct lu_object *lo;
2122
2123         list_for_each_entry(lo, &dto->do_lu.lo_header->loh_layers, lo_linkage) {
2124                 if (lo->lo_dev == &dt_dev->dd_lu_dev)
2125                         return container_of(lo, struct dt_object, do_lu);
2126         }
2127         return NULL;
2128 }
2129
2130 static inline void dt_object_put(const struct lu_env *env,
2131                                  struct dt_object *dto)
2132 {
2133         lu_object_put(env, &dto->do_lu);
2134 }
2135
2136 static inline void dt_object_put_nocache(const struct lu_env *env,
2137                                          struct dt_object *dto)
2138 {
2139         lu_object_put_nocache(env, &dto->do_lu);
2140 }
2141
2142 int local_oid_storage_init(const struct lu_env *env, struct dt_device *dev,
2143                            const struct lu_fid *first_fid,
2144                            struct local_oid_storage **los);
2145 void local_oid_storage_fini(const struct lu_env *env,
2146                             struct local_oid_storage *los);
2147 int local_object_fid_generate(const struct lu_env *env,
2148                               struct local_oid_storage *los,
2149                               struct lu_fid *fid);
2150 int local_object_declare_create(const struct lu_env *env,
2151                                 struct local_oid_storage *los,
2152                                 struct dt_object *o,
2153                                 struct lu_attr *attr,
2154                                 struct dt_object_format *dof,
2155                                 struct thandle *th);
2156 int local_object_create(const struct lu_env *env,
2157                         struct local_oid_storage *los,
2158                         struct dt_object *o,
2159                         struct lu_attr *attr, struct dt_object_format *dof,
2160                         struct thandle *th);
2161 struct dt_object *local_file_find(const struct lu_env *env,
2162                                   struct local_oid_storage *los,
2163                                   struct dt_object *parent,
2164                                   const char *name);
2165 struct dt_object *local_file_find_or_create(const struct lu_env *env,
2166                                             struct local_oid_storage *los,
2167                                             struct dt_object *parent,
2168                                             const char *name, __u32 mode);
2169 struct dt_object *local_file_find_or_create_with_fid(const struct lu_env *env,
2170                                                      struct dt_device *dt,
2171                                                      const struct lu_fid *fid,
2172                                                      struct dt_object *parent,
2173                                                      const char *name,
2174                                                      __u32 mode);
2175 struct dt_object *
2176 local_index_find_or_create(const struct lu_env *env,
2177                            struct local_oid_storage *los,
2178                            struct dt_object *parent,
2179                            const char *name, __u32 mode,
2180                            const struct dt_index_features *ft);
2181 struct dt_object *
2182 local_index_find_or_create_with_fid(const struct lu_env *env,
2183                                     struct dt_device *dt,
2184                                     const struct lu_fid *fid,
2185                                     struct dt_object *parent,
2186                                     const char *name, __u32 mode,
2187                                     const struct dt_index_features *ft);
2188 int local_object_unlink(const struct lu_env *env, struct dt_device *dt,
2189                         struct dt_object *parent, const char *name);
2190
2191 static inline int dt_object_lock(const struct lu_env *env,
2192                                  struct dt_object *o, struct lustre_handle *lh,
2193                                  struct ldlm_enqueue_info *einfo,
2194                                  union ldlm_policy_data *policy)
2195 {
2196         LASSERT(o != NULL);
2197         LASSERT(o->do_ops != NULL);
2198         LASSERT(o->do_ops->do_object_lock != NULL);
2199         return o->do_ops->do_object_lock(env, o, lh, einfo, policy);
2200 }
2201
2202 static inline int dt_object_unlock(const struct lu_env *env,
2203                                    struct dt_object *o,
2204                                    struct ldlm_enqueue_info *einfo,
2205                                    union ldlm_policy_data *policy)
2206 {
2207         LASSERT(o != NULL);
2208         LASSERT(o->do_ops != NULL);
2209         LASSERT(o->do_ops->do_object_unlock != NULL);
2210         return o->do_ops->do_object_unlock(env, o, einfo, policy);
2211 }
2212
2213 int dt_lookup_dir(const struct lu_env *env, struct dt_object *dir,
2214                   const char *name, struct lu_fid *fid);
2215
2216 static inline int dt_object_sync(const struct lu_env *env, struct dt_object *o,
2217                                  __u64 start, __u64 end)
2218 {
2219         LASSERT(o);
2220         LASSERT(o->do_ops);
2221         LASSERT(o->do_ops->do_object_sync);
2222         return o->do_ops->do_object_sync(env, o, start, end);
2223 }
2224
2225 static inline int dt_fid_alloc(const struct lu_env *env,
2226                                struct dt_device *d,
2227                                struct lu_fid *fid,
2228                                struct lu_object *parent,
2229                                const struct lu_name *name)
2230 {
2231         struct lu_device *l = dt2lu_dev(d);
2232
2233         return l->ld_ops->ldo_fid_alloc(env, l, fid, parent, name);
2234 }
2235
2236 int dt_declare_version_set(const struct lu_env *env, struct dt_object *o,
2237                            struct thandle *th);
2238 void dt_version_set(const struct lu_env *env, struct dt_object *o,
2239                     dt_obj_version_t version, struct thandle *th);
2240 dt_obj_version_t dt_version_get(const struct lu_env *env, struct dt_object *o);
2241
2242
2243 int dt_read(const struct lu_env *env, struct dt_object *dt,
2244             struct lu_buf *buf, loff_t *pos);
2245 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
2246                    struct lu_buf *buf, loff_t *pos);
2247 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
2248                     const struct lu_buf *buf, loff_t *pos, struct thandle *th);
2249 typedef int (*dt_index_page_build_t)(const struct lu_env *env,
2250                                      struct dt_object *obj, union lu_page *lp,
2251                                      size_t bytes, const struct dt_it_ops *iops,
2252                                      struct dt_it *it, __u32 attr, void *arg);
2253 int dt_index_walk(const struct lu_env *env, struct dt_object *obj,
2254                   const struct lu_rdpg *rdpg, dt_index_page_build_t filler,
2255                   void *arg);
2256 int dt_index_read(const struct lu_env *env, struct dt_device *dev,
2257                   struct idx_info *ii, const struct lu_rdpg *rdpg);
2258
2259 static inline struct thandle *dt_trans_create(const struct lu_env *env,
2260                                               struct dt_device *d)
2261 {
2262         LASSERT(d->dd_ops->dt_trans_create);
2263         return d->dd_ops->dt_trans_create(env, d);
2264 }
2265
2266 static inline int dt_trans_start(const struct lu_env *env,
2267                                  struct dt_device *d, struct thandle *th)
2268 {
2269         LASSERT(d->dd_ops->dt_trans_start);
2270         return d->dd_ops->dt_trans_start(env, d, th);
2271 }
2272
2273 /* for this transaction hooks shouldn't be called */
2274 static inline int dt_trans_start_local(const struct lu_env *env,
2275                                        struct dt_device *d, struct thandle *th)
2276 {
2277         LASSERT(d->dd_ops->dt_trans_start);
2278         th->th_local = 1;
2279         return d->dd_ops->dt_trans_start(env, d, th);
2280 }
2281
2282 static inline int dt_trans_stop(const struct lu_env *env,
2283                                 struct dt_device *d, struct thandle *th)
2284 {
2285         LASSERT(d->dd_ops->dt_trans_stop);
2286         return d->dd_ops->dt_trans_stop(env, d, th);
2287 }
2288
2289 static inline int dt_trans_cb_add(struct thandle *th,
2290                                   struct dt_txn_commit_cb *dcb)
2291 {
2292         LASSERT(th->th_dev->dd_ops->dt_trans_cb_add);
2293         dcb->dcb_magic = TRANS_COMMIT_CB_MAGIC;
2294         return th->th_dev->dd_ops->dt_trans_cb_add(th, dcb);
2295 }
2296 /** @} dt */
2297
2298
2299 static inline int dt_declare_record_write(const struct lu_env *env,
2300                                           struct dt_object *dt,
2301                                           const struct lu_buf *buf,
2302                                           loff_t pos,
2303                                           struct thandle *th)
2304 {
2305         int rc;
2306
2307         LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
2308         LASSERT(th != NULL);
2309         LASSERTF(dt->do_body_ops, DFID" doesn't exit\n",
2310                  PFID(lu_object_fid(&dt->do_lu)));
2311         LASSERT(dt->do_body_ops->dbo_declare_write);
2312         rc = dt->do_body_ops->dbo_declare_write(env, dt, buf, pos, th);
2313         return rc;
2314 }
2315
2316 static inline int dt_declare_create(const struct lu_env *env,
2317                                     struct dt_object *dt,
2318                                     struct lu_attr *attr,
2319                                     struct dt_allocation_hint *hint,
2320                                     struct dt_object_format *dof,
2321                                     struct thandle *th)
2322 {
2323         LASSERT(dt);
2324         LASSERT(dt->do_ops);
2325         LASSERT(dt->do_ops->do_declare_create);
2326
2327         if (CFS_FAULT_CHECK(OBD_FAIL_DT_DECLARE_CREATE))
2328                 return cfs_fail_err;
2329
2330         return dt->do_ops->do_declare_create(env, dt, attr, hint, dof, th);
2331 }
2332
2333 static inline int dt_create(const struct lu_env *env,
2334                                     struct dt_object *dt,
2335                                     struct lu_attr *attr,
2336                                     struct dt_allocation_hint *hint,
2337                                     struct dt_object_format *dof,
2338                                     struct thandle *th)
2339 {
2340         LASSERT(dt);
2341         LASSERT(dt->do_ops);
2342         LASSERT(dt->do_ops->do_create);
2343
2344         if (CFS_FAULT_CHECK(OBD_FAIL_DT_CREATE))
2345                 return cfs_fail_err;
2346
2347         return dt->do_ops->do_create(env, dt, attr, hint, dof, th);
2348 }
2349
2350 static inline int dt_declare_destroy(const struct lu_env *env,
2351                                      struct dt_object *dt,
2352                                      struct thandle *th)
2353 {
2354         LASSERT(dt);
2355         LASSERT(dt->do_ops);
2356         LASSERT(dt->do_ops->do_declare_destroy);
2357
2358         if (CFS_FAULT_CHECK(OBD_FAIL_DT_DECLARE_DESTROY))
2359                 return cfs_fail_err;
2360
2361         return dt->do_ops->do_declare_destroy(env, dt, th);
2362 }
2363
2364 static inline int dt_destroy(const struct lu_env *env,
2365                              struct dt_object *dt,
2366                              struct thandle *th)
2367 {
2368         LASSERT(dt);
2369         LASSERT(dt->do_ops);
2370         LASSERT(dt->do_ops->do_destroy);
2371
2372         if (CFS_FAULT_CHECK(OBD_FAIL_DT_DESTROY))
2373                 return cfs_fail_err;
2374
2375         return dt->do_ops->do_destroy(env, dt, th);
2376 }
2377
2378 static inline void dt_read_lock(const struct lu_env *env,
2379                                 struct dt_object *dt,
2380                                 unsigned role)
2381 {
2382         LASSERT(dt);
2383         LASSERT(dt->do_ops);
2384         LASSERT(dt->do_ops->do_read_lock);
2385         dt->do_ops->do_read_lock(env, dt, role);
2386 }
2387
2388 static inline void dt_write_lock(const struct lu_env *env,
2389                                 struct dt_object *dt,
2390                                 unsigned role)
2391 {
2392         LASSERT(dt);
2393         LASSERT(dt->do_ops);
2394         LASSERT(dt->do_ops->do_write_lock);
2395         dt->do_ops->do_write_lock(env, dt, role);
2396 }
2397
2398 static inline void dt_read_unlock(const struct lu_env *env,
2399                                 struct dt_object *dt)
2400 {
2401         LASSERT(dt);
2402         LASSERT(dt->do_ops);
2403         LASSERT(dt->do_ops->do_read_unlock);
2404         dt->do_ops->do_read_unlock(env, dt);
2405 }
2406
2407 static inline void dt_write_unlock(const struct lu_env *env,
2408                                 struct dt_object *dt)
2409 {
2410         LASSERT(dt);
2411         LASSERT(dt->do_ops);
2412         LASSERT(dt->do_ops->do_write_unlock);
2413         dt->do_ops->do_write_unlock(env, dt);
2414 }
2415
2416 static inline int dt_write_locked(const struct lu_env *env,
2417                                   struct dt_object *dt)
2418 {
2419         LASSERT(dt);
2420         LASSERT(dt->do_ops);
2421         LASSERT(dt->do_ops->do_write_locked);
2422         return dt->do_ops->do_write_locked(env, dt);
2423 }
2424
2425 static inline bool dt_object_stale(struct dt_object *dt)
2426 {
2427         LASSERT(dt);
2428         LASSERT(dt->do_ops);
2429         LASSERT(dt->do_ops->do_check_stale);
2430
2431         return dt->do_ops->do_check_stale(dt);
2432 }
2433
2434 static inline int dt_declare_attr_get(const struct lu_env *env,
2435                                       struct dt_object *dt)
2436 {
2437         LASSERT(dt);
2438         LASSERT(dt->do_ops);
2439         LASSERT(dt->do_ops->do_declare_attr_get);
2440
2441         if (CFS_FAULT_CHECK(OBD_FAIL_DT_DECLARE_ATTR_GET))
2442                 return cfs_fail_err;
2443
2444         return dt->do_ops->do_declare_attr_get(env, dt);
2445 }
2446
2447 static inline int dt_attr_get(const struct lu_env *env, struct dt_object *dt,
2448                               struct lu_attr *la)
2449 {
2450         LASSERT(dt);
2451         LASSERT(dt->do_ops);
2452         LASSERT(dt->do_ops->do_attr_get);
2453
2454         if (CFS_FAULT_CHECK(OBD_FAIL_DT_ATTR_GET))
2455                 return cfs_fail_err;
2456
2457         return dt->do_ops->do_attr_get(env, dt, la);
2458 }
2459
2460 static inline int dt_declare_attr_set(const struct lu_env *env,
2461                                       struct dt_object *dt,
2462                                       const struct lu_attr *la,
2463                                       struct thandle *th)
2464 {
2465         LASSERT(dt);
2466         LASSERT(dt->do_ops);
2467         LASSERT(dt->do_ops->do_declare_attr_set);
2468
2469         if (CFS_FAULT_CHECK(OBD_FAIL_DT_DECLARE_ATTR_SET))
2470                 return cfs_fail_err;
2471
2472         return dt->do_ops->do_declare_attr_set(env, dt, la, th);
2473 }
2474
2475 static inline int dt_attr_set(const struct lu_env *env, struct dt_object *dt,
2476                               const struct lu_attr *la, struct thandle *th)
2477 {
2478         LASSERT(dt);
2479         LASSERT(dt->do_ops);
2480         LASSERT(dt->do_ops->do_attr_set);
2481
2482         if (CFS_FAULT_CHECK(OBD_FAIL_DT_ATTR_SET))
2483                 return cfs_fail_err;
2484
2485         return dt->do_ops->do_attr_set(env, dt, la, th);
2486 }
2487
2488 static inline int dt_declare_ref_add(const struct lu_env *env,
2489                                      struct dt_object *dt, struct thandle *th)
2490 {
2491         LASSERT(dt);
2492         LASSERT(dt->do_ops);
2493         LASSERT(dt->do_ops->do_declare_ref_add);
2494
2495         if (CFS_FAULT_CHECK(OBD_FAIL_DT_DECLARE_REF_ADD))
2496                 return cfs_fail_err;
2497
2498         return dt->do_ops->do_declare_ref_add(env, dt, th);
2499 }
2500
2501 static inline int dt_ref_add(const struct lu_env *env,
2502                              struct dt_object *dt, struct thandle *th)
2503 {
2504         LASSERT(dt);
2505         LASSERT(dt->do_ops);
2506         LASSERT(dt->do_ops->do_ref_add);
2507
2508         if (CFS_FAULT_CHECK(OBD_FAIL_DT_REF_ADD))
2509                 return cfs_fail_err;
2510
2511         return dt->do_ops->do_ref_add(env, dt, th);
2512 }
2513
2514 static inline int dt_declare_ref_del(const struct lu_env *env,
2515                                      struct dt_object *dt, struct thandle *th)
2516 {
2517         LASSERT(dt);
2518         LASSERT(dt->do_ops);
2519         LASSERT(dt->do_ops->do_declare_ref_del);
2520
2521         if (CFS_FAULT_CHECK(OBD_FAIL_DT_DECLARE_REF_DEL))
2522                 return cfs_fail_err;
2523
2524         return dt->do_ops->do_declare_ref_del(env, dt, th);
2525 }
2526
2527 static inline int dt_ref_del(const struct lu_env *env,
2528                              struct dt_object *dt, struct thandle *th)
2529 {
2530         LASSERT(dt);
2531         LASSERT(dt->do_ops);
2532         LASSERT(dt->do_ops->do_ref_del);
2533
2534         if (CFS_FAULT_CHECK(OBD_FAIL_DT_REF_DEL))
2535                 return cfs_fail_err;
2536
2537         return dt->do_ops->do_ref_del(env, dt, th);
2538 }
2539
2540 static inline int dt_bufs_get(const struct lu_env *env, struct dt_object *d,
2541                               struct niobuf_remote *rnb,
2542                               struct niobuf_local *lnb, int maxlnb,
2543                               enum dt_bufs_type rw)
2544 {
2545         LASSERT(d);
2546         LASSERT(d->do_body_ops);
2547         LASSERT(d->do_body_ops->dbo_bufs_get);
2548         return d->do_body_ops->dbo_bufs_get(env, d, rnb->rnb_offset,
2549                                             rnb->rnb_len, lnb, maxlnb, rw);
2550 }
2551
2552 static inline int dt_bufs_put(const struct lu_env *env, struct dt_object *d,
2553                               struct niobuf_local *lnb, int n)
2554 {
2555         LASSERT(d);
2556         LASSERT(d->do_body_ops);
2557         LASSERT(d->do_body_ops->dbo_bufs_put);
2558         return d->do_body_ops->dbo_bufs_put(env, d, lnb, n);
2559 }
2560
2561 static inline int dt_write_prep(const struct lu_env *env, struct dt_object *d,
2562                                 struct niobuf_local *lnb, int n)
2563 {
2564         LASSERT(d);
2565         LASSERT(d->do_body_ops);
2566         LASSERT(d->do_body_ops->dbo_write_prep);
2567         return d->do_body_ops->dbo_write_prep(env, d, lnb, n);
2568 }
2569
2570 static inline int dt_declare_write_commit(const struct lu_env *env,
2571                                           struct dt_object *d,
2572                                           struct niobuf_local *lnb,
2573                                           int n, struct thandle *th)
2574 {
2575         LASSERTF(d != NULL, "dt is NULL when we want to declare write\n");
2576         LASSERT(th != NULL);
2577         return d->do_body_ops->dbo_declare_write_commit(env, d, lnb, n, th);
2578 }
2579
2580
2581 static inline int dt_write_commit(const struct lu_env *env,
2582                                   struct dt_object *d, struct niobuf_local *lnb,
2583                                   int n, struct thandle *th, __u64 size)
2584 {
2585         LASSERT(d);
2586         LASSERT(d->do_body_ops);
2587         LASSERT(d->do_body_ops->dbo_write_commit);
2588         return d->do_body_ops->dbo_write_commit(env, d, lnb, n, th, size);
2589 }
2590
2591 static inline int dt_read_prep(const struct lu_env *env, struct dt_object *d,
2592                                struct niobuf_local *lnb, int n)
2593 {
2594         LASSERT(d);
2595         LASSERT(d->do_body_ops);
2596         LASSERT(d->do_body_ops->dbo_read_prep);
2597         return d->do_body_ops->dbo_read_prep(env, d, lnb, n);
2598 }
2599
2600 static inline int dt_declare_write(const struct lu_env *env,
2601                                    struct dt_object *dt,
2602                                    const struct lu_buf *buf, loff_t pos,
2603                                    struct thandle *th)
2604 {
2605         LASSERT(dt);
2606         LASSERT(dt->do_body_ops);
2607         LASSERT(dt->do_body_ops->dbo_declare_write);
2608         return dt->do_body_ops->dbo_declare_write(env, dt, buf, pos, th);
2609 }
2610
2611 static inline ssize_t dt_write(const struct lu_env *env, struct dt_object *dt,
2612                                const struct lu_buf *buf, loff_t *pos,
2613                                struct thandle *th)
2614 {
2615         LASSERT(dt);
2616         LASSERT(dt->do_body_ops);
2617         LASSERT(dt->do_body_ops->dbo_write);
2618         return dt->do_body_ops->dbo_write(env, dt, buf, pos, th);
2619 }
2620
2621 static inline int dt_declare_punch(const struct lu_env *env,
2622                                    struct dt_object *dt, __u64 start,
2623                                    __u64 end, struct thandle *th)
2624 {
2625         LASSERT(dt);
2626         LASSERT(dt->do_body_ops);
2627         LASSERT(dt->do_body_ops->dbo_declare_punch);
2628         return dt->do_body_ops->dbo_declare_punch(env, dt, start, end, th);
2629 }
2630
2631 static inline int dt_punch(const struct lu_env *env, struct dt_object *dt,
2632                            __u64 start, __u64 end, struct thandle *th)
2633 {
2634         LASSERT(dt);
2635         LASSERT(dt->do_body_ops);
2636         LASSERT(dt->do_body_ops->dbo_punch);
2637         return dt->do_body_ops->dbo_punch(env, dt, start, end, th);
2638 }
2639
2640 static inline int dt_ladvise(const struct lu_env *env, struct dt_object *dt,
2641                              __u64 start, __u64 end, int advice)
2642 {
2643         LASSERT(dt);
2644         LASSERT(dt->do_body_ops);
2645         LASSERT(dt->do_body_ops->dbo_ladvise);
2646         return dt->do_body_ops->dbo_ladvise(env, dt, start, end, advice);
2647 }
2648
2649 static inline int dt_declare_fallocate(const struct lu_env *env,
2650                                        struct dt_object *dt, __u64 start,
2651                                        __u64 end, int mode, struct thandle *th)
2652 {
2653         LASSERT(dt);
2654         if (!dt->do_body_ops)
2655                 return -EOPNOTSUPP;
2656         LASSERT(dt->do_body_ops);
2657         LASSERT(dt->do_body_ops->dbo_declare_fallocate);
2658         return dt->do_body_ops->dbo_declare_fallocate(env, dt, start, end,
2659                                                       mode, th);
2660 }
2661
2662 static inline int dt_falloc(const struct lu_env *env, struct dt_object *dt,
2663                               __u64 start, __u64 end, int mode,
2664                               struct thandle *th)
2665 {
2666         LASSERT(dt);
2667         if (!dt->do_body_ops)
2668                 return -EOPNOTSUPP;
2669         LASSERT(dt->do_body_ops);
2670         LASSERT(dt->do_body_ops->dbo_fallocate);
2671         return dt->do_body_ops->dbo_fallocate(env, dt, start, end, mode, th);
2672 }
2673
2674 static inline int dt_fiemap_get(const struct lu_env *env, struct dt_object *d,
2675                                 struct fiemap *fm)
2676 {
2677         LASSERT(d);
2678         if (d->do_body_ops == NULL)
2679                 return -EPROTO;
2680         if (d->do_body_ops->dbo_fiemap_get == NULL)
2681                 return -EOPNOTSUPP;
2682         return d->do_body_ops->dbo_fiemap_get(env, d, fm);
2683 }
2684
2685 static inline loff_t dt_lseek(const struct lu_env *env, struct dt_object *d,
2686                               loff_t offset, int whence)
2687 {
2688         LASSERT(d);
2689         if (d->do_body_ops == NULL)
2690                 return -EPROTO;
2691         if (d->do_body_ops->dbo_lseek == NULL)
2692                 return -EOPNOTSUPP;
2693         return d->do_body_ops->dbo_lseek(env, d, offset, whence);
2694 }
2695
2696 static inline int dt_statfs_info(const struct lu_env *env,
2697                                  struct dt_device *dev,
2698                                 struct obd_statfs *osfs,
2699                                 struct obd_statfs_info *info)
2700 {
2701         LASSERT(dev);
2702         LASSERT(dev->dd_ops);
2703         LASSERT(dev->dd_ops->dt_statfs);
2704         return dev->dd_ops->dt_statfs(env, dev, osfs, info);
2705 }
2706
2707 static inline int dt_statfs(const struct lu_env *env, struct dt_device *dev,
2708                             struct obd_statfs *osfs)
2709 {
2710         return dt_statfs_info(env, dev, osfs, NULL);
2711 }
2712
2713 static inline int dt_root_get(const struct lu_env *env, struct dt_device *dev,
2714                               struct lu_fid *f)
2715 {
2716         LASSERT(dev);
2717         LASSERT(dev->dd_ops);
2718         LASSERT(dev->dd_ops->dt_root_get);
2719         return dev->dd_ops->dt_root_get(env, dev, f);
2720 }
2721
2722 static inline void dt_conf_get(const struct lu_env *env,
2723                                const struct dt_device *dev,
2724                                struct dt_device_param *param)
2725 {
2726         LASSERT(dev);
2727         LASSERT(dev->dd_ops);
2728         LASSERT(dev->dd_ops->dt_conf_get);
2729         return dev->dd_ops->dt_conf_get(env, dev, param);
2730 }
2731
2732 static inline struct super_block *dt_mnt_sb_get(const struct dt_device *dev)
2733 {
2734         LASSERT(dev);
2735         LASSERT(dev->dd_ops);
2736         if (dev->dd_ops->dt_mnt_sb_get)
2737                 return dev->dd_ops->dt_mnt_sb_get(dev);
2738
2739         return ERR_PTR(-EOPNOTSUPP);
2740 }
2741
2742 static inline int dt_sync(const struct lu_env *env, struct dt_device *dev)
2743 {
2744         LASSERT(dev);
2745         LASSERT(dev->dd_ops);
2746         LASSERT(dev->dd_ops->dt_sync);
2747         return dev->dd_ops->dt_sync(env, dev);
2748 }
2749
2750 static inline int dt_ro(const struct lu_env *env, struct dt_device *dev)
2751 {
2752         LASSERT(dev);
2753         LASSERT(dev->dd_ops);
2754         LASSERT(dev->dd_ops->dt_ro);
2755         return dev->dd_ops->dt_ro(env, dev);
2756 }
2757
2758 static inline int dt_declare_insert(const struct lu_env *env,
2759                                     struct dt_object *dt,
2760                                     const struct dt_rec *rec,
2761                                     const struct dt_key *key,
2762                                     struct thandle *th)
2763 {
2764         LASSERT(dt);
2765         LASSERT(dt->do_index_ops);
2766         LASSERT(dt->do_index_ops->dio_declare_insert);
2767
2768         if (CFS_FAULT_CHECK(OBD_FAIL_DT_DECLARE_INSERT))
2769                 return cfs_fail_err;
2770
2771         return dt->do_index_ops->dio_declare_insert(env, dt, rec, key, th);
2772 }
2773
2774 static inline int dt_insert(const struct lu_env *env,
2775                             struct dt_object *dt,
2776                             const struct dt_rec *rec,
2777                             const struct dt_key *key,
2778                             struct thandle *th)
2779 {
2780         LASSERT(dt);
2781         LASSERT(dt->do_index_ops);
2782         LASSERT(dt->do_index_ops->dio_insert);
2783
2784         if (CFS_FAULT_CHECK(OBD_FAIL_DT_INSERT))
2785                 return cfs_fail_err;
2786
2787         return dt->do_index_ops->dio_insert(env, dt, rec, key, th);
2788 }
2789
2790 static inline int dt_declare_xattr_del(const struct lu_env *env,
2791                                        struct dt_object *dt,
2792                                        const char *name,
2793                                        struct thandle *th)
2794 {
2795         LASSERT(dt);
2796         LASSERT(dt->do_ops);
2797         LASSERT(dt->do_ops->do_declare_xattr_del);
2798
2799         if (CFS_FAULT_CHECK(OBD_FAIL_DT_DECLARE_XATTR_DEL))
2800                 return cfs_fail_err;
2801
2802         return dt->do_ops->do_declare_xattr_del(env, dt, name, th);
2803 }
2804
2805 static inline int dt_xattr_del(const struct lu_env *env,
2806                                struct dt_object *dt, const char *name,
2807                                struct thandle *th)
2808 {
2809         LASSERT(dt);
2810         LASSERT(dt->do_ops);
2811         LASSERT(dt->do_ops->do_xattr_del);
2812
2813         if (CFS_FAULT_CHECK(OBD_FAIL_DT_XATTR_DEL))
2814                 return cfs_fail_err;
2815
2816         return dt->do_ops->do_xattr_del(env, dt, name, th);
2817 }
2818
2819 static inline int dt_declare_xattr_set(const struct lu_env *env,
2820                                       struct dt_object *dt,
2821                                       const struct lu_buf *buf,
2822                                       const char *name, int fl,
2823                                       struct thandle *th)
2824 {
2825         LASSERT(dt);
2826         LASSERT(dt->do_ops);
2827         LASSERT(dt->do_ops->do_declare_xattr_set);
2828
2829         if (CFS_FAULT_CHECK(OBD_FAIL_DT_DECLARE_XATTR_SET))
2830                 return cfs_fail_err;
2831
2832         return dt->do_ops->do_declare_xattr_set(env, dt, buf, name, fl, th);
2833 }
2834
2835 static inline int dt_xattr_set(const struct lu_env *env,
2836                                struct dt_object *dt, const struct lu_buf *buf,
2837                                const char *name, int fl, struct thandle *th)
2838 {
2839         LASSERT(dt);
2840         LASSERT(dt->do_ops);
2841         LASSERT(dt->do_ops->do_xattr_set);
2842
2843         if (CFS_FAULT_CHECK(OBD_FAIL_DT_XATTR_SET))
2844                 return cfs_fail_err;
2845
2846         return dt->do_ops->do_xattr_set(env, dt, buf, name, fl, th);
2847 }
2848
2849 static inline int dt_declare_xattr_get(const struct lu_env *env,
2850                                        struct dt_object *dt,
2851                                        struct lu_buf *buf,
2852                                        const char *name)
2853 {
2854         LASSERT(dt);
2855         LASSERT(dt->do_ops);
2856         LASSERT(dt->do_ops->do_declare_xattr_get);
2857
2858         if (CFS_FAULT_CHECK(OBD_FAIL_DT_DECLARE_XATTR_GET))
2859                 return cfs_fail_err;
2860
2861         return dt->do_ops->do_declare_xattr_get(env, dt, buf, name);
2862 }
2863
2864 static inline int dt_xattr_get(const struct lu_env *env,
2865                                struct dt_object *dt, struct lu_buf *buf,
2866                                const char *name)
2867 {
2868         LASSERT(dt);
2869         LASSERT(dt->do_ops);
2870         LASSERT(dt->do_ops->do_xattr_get);
2871
2872         if (CFS_FAULT_CHECK(OBD_FAIL_DT_XATTR_GET))
2873                 return cfs_fail_err;
2874
2875         return dt->do_ops->do_xattr_get(env, dt, buf, name);
2876 }
2877
2878 static inline int dt_xattr_list(const struct lu_env *env, struct dt_object *dt,
2879                                 const struct lu_buf *buf)
2880 {
2881         LASSERT(dt);
2882         LASSERT(dt->do_ops);
2883         LASSERT(dt->do_ops->do_xattr_list);
2884
2885         if (CFS_FAULT_CHECK(OBD_FAIL_DT_XATTR_LIST))
2886                 return cfs_fail_err;
2887
2888         return dt->do_ops->do_xattr_list(env, dt, buf);
2889 }
2890
2891 static inline int dt_invalidate(const struct lu_env *env, struct dt_object *dt)
2892 {
2893         LASSERT(dt);
2894         LASSERT(dt->do_ops);
2895         LASSERT(dt->do_ops->do_invalidate);
2896
2897         return dt->do_ops->do_invalidate(env, dt);
2898 }
2899
2900 static inline int dt_declare_delete(const struct lu_env *env,
2901                                     struct dt_object *dt,
2902                                     const struct dt_key *key,
2903                                     struct thandle *th)
2904 {
2905         LASSERT(dt);
2906         LASSERT(dt->do_index_ops);
2907         LASSERT(dt->do_index_ops->dio_declare_delete);
2908
2909         if (CFS_FAULT_CHECK(OBD_FAIL_DT_DECLARE_DELETE))
2910                 return cfs_fail_err;
2911
2912         return dt->do_index_ops->dio_declare_delete(env, dt, key, th);
2913 }
2914
2915 static inline int dt_delete(const struct lu_env *env,
2916                             struct dt_object *dt,
2917                             const struct dt_key *key,
2918                             struct thandle *th)
2919 {
2920         LASSERT(dt);
2921         LASSERT(dt->do_index_ops);
2922         LASSERT(dt->do_index_ops->dio_delete);
2923
2924         if (CFS_FAULT_CHECK(OBD_FAIL_DT_DELETE))
2925                 return cfs_fail_err;
2926
2927         return dt->do_index_ops->dio_delete(env, dt, key, th);
2928 }
2929
2930 static inline int dt_commit_async(const struct lu_env *env,
2931                                   struct dt_device *dev)
2932 {
2933         LASSERT(dev);
2934         LASSERT(dev->dd_ops);
2935         LASSERT(dev->dd_ops->dt_commit_async);
2936         return dev->dd_ops->dt_commit_async(env, dev);
2937 }
2938
2939 static inline int dt_reserve_or_free_quota(const struct lu_env *env,
2940                                            struct dt_device *dev,
2941                                            enum quota_type type, __u64 uid,
2942                                            __u64 gid, int count, bool is_md)
2943 {
2944         LASSERT(dev);
2945         LASSERT(dev->dd_ops);
2946         LASSERT(dev->dd_ops->dt_reserve_or_free_quota);
2947         return dev->dd_ops->dt_reserve_or_free_quota(env, dev, type, uid, gid,
2948                                                      count, is_md);
2949 }
2950
2951 static inline int dt_lookup(const struct lu_env *env,
2952                             struct dt_object *dt,
2953                             struct dt_rec *rec,
2954                             const struct dt_key *key)
2955 {
2956         int ret;
2957
2958         LASSERT(dt);
2959         LASSERT(dt->do_index_ops);
2960         LASSERT(dt->do_index_ops->dio_lookup);
2961
2962         if (CFS_FAULT_CHECK(OBD_FAIL_DT_LOOKUP))
2963                 return cfs_fail_err;
2964
2965         ret = dt->do_index_ops->dio_lookup(env, dt, rec, key);
2966         if (ret > 0)
2967                 ret = 0;
2968         else if (ret == 0)
2969                 ret = -ENOENT;
2970         return ret;
2971 }
2972
2973 static inline int dt_declare_layout_change(const struct lu_env *env,
2974                                            struct dt_object *o,
2975                                            struct md_layout_change *mlc,
2976                                            struct thandle *th)
2977 {
2978         LASSERT(o);
2979         LASSERT(o->do_ops);
2980         LASSERT(o->do_ops->do_declare_layout_change);
2981         return o->do_ops->do_declare_layout_change(env, o, mlc, th);
2982 }
2983
2984 static inline int dt_layout_change(const struct lu_env *env,
2985                                    struct dt_object *o,
2986                                    struct md_layout_change *mlc,
2987                                    struct thandle *th)
2988 {
2989         LASSERT(o);
2990         LASSERT(o->do_ops);
2991         LASSERT(o->do_ops->do_layout_change);
2992         return o->do_ops->do_layout_change(env, o, mlc, th);
2993 }
2994
2995 struct dt_find_hint {
2996         struct lu_fid        *dfh_fid;
2997         struct dt_device     *dfh_dt;
2998         struct dt_object     *dfh_o;
2999 };
3000
3001 struct dt_insert_rec {
3002         union {
3003                 const struct lu_fid     *rec_fid;
3004                 void                    *rec_data;
3005         };
3006         union {
3007                 struct {
3008                         __u32            rec_type;
3009                         __u32            rec_padding;
3010                 };
3011                 __u64                    rec_misc;
3012         };
3013 };
3014
3015 struct dt_thread_info {
3016         char                     dti_buf[DT_MAX_PATH];
3017         struct dt_find_hint      dti_dfh;
3018         struct lu_attr           dti_attr;
3019         struct lu_fid            dti_fid;
3020         struct dt_object_format  dti_dof;
3021         struct lustre_mdt_attrs  dti_lma;
3022         struct lu_buf            dti_lb;
3023         struct lu_object_conf    dti_conf;
3024         loff_t                   dti_off;
3025         struct dt_insert_rec     dti_dt_rec;
3026 };
3027
3028 extern struct lu_context_key dt_key;
3029
3030 static inline struct dt_thread_info *dt_info(const struct lu_env *env)
3031 {
3032         struct dt_thread_info *dti;
3033
3034         dti = lu_context_key_get(&env->le_ctx, &dt_key);
3035         LASSERT(dti);
3036         return dti;
3037 }
3038
3039 int dt_global_init(void);
3040 void dt_global_fini(void);
3041 int dt_tunables_init(struct dt_device *dt, struct obd_type *type,
3042                      const char *name, struct ldebugfs_vars *list);
3043 int dt_tunables_fini(struct dt_device *dt);
3044
3045 # ifdef CONFIG_PROC_FS
3046 int lprocfs_dt_blksize_seq_show(struct seq_file *m, void *v);
3047 int lprocfs_dt_kbytestotal_seq_show(struct seq_file *m, void *v);
3048 int lprocfs_dt_kbytesfree_seq_show(struct seq_file *m, void *v);
3049 int lprocfs_dt_kbytesavail_seq_show(struct seq_file *m, void *v);
3050 int lprocfs_dt_filestotal_seq_show(struct seq_file *m, void *v);
3051 int lprocfs_dt_filesfree_seq_show(struct seq_file *m, void *v);
3052 # endif /* CONFIG_PROC_FS */
3053
3054 #endif /* __LUSTRE_DT_OBJECT_H */