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