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