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