Whamcloud - gitweb
f438bcb9bcba79add5585f262d900e967f08fa1c
[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, 2012, Whamcloud, Inc.
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
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 dt_quota_ctxt;
69 struct niobuf_local;
70 struct niobuf_remote;
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         void              *ddp_mnt; /* XXX: old code can retrieve mnt -bzzz */
84         int                ddp_mount_type;
85         unsigned long long ddp_maxbytes;
86         /* percentage of available space to reserve for grant error margin */
87         int                ddp_grant_reserved;
88         /* per-inode space consumption */
89         short              ddp_inodespace;
90         /* per-fragment grant overhead to be used by client for grant
91          * calculation */
92         int                ddp_grant_frag;
93 };
94
95 /**
96  * Per-transaction commit callback function
97  */
98 struct dt_txn_commit_cb;
99 typedef void (*dt_cb_t)(struct lu_env *env, struct thandle *th,
100                         struct dt_txn_commit_cb *cb, int err);
101 /**
102  * Special per-transaction callback for cases when just commit callback
103  * is needed and per-device callback are not convenient to use
104  */
105 struct dt_txn_commit_cb {
106         cfs_list_t dcb_linkage;
107         dt_cb_t    dcb_func;
108 };
109
110 /**
111  * Operations on dt device.
112  */
113 struct dt_device_operations {
114         /**
115          * Return device-wide statistics.
116          */
117         int   (*dt_statfs)(const struct lu_env *env,
118                            struct dt_device *dev, struct obd_statfs *osfs);
119         /**
120          * Create transaction, described by \a param.
121          */
122         struct thandle *(*dt_trans_create)(const struct lu_env *env,
123                                            struct dt_device *dev);
124         /**
125          * Start transaction, described by \a param.
126          */
127         int   (*dt_trans_start)(const struct lu_env *env,
128                                 struct dt_device *dev, struct thandle *th);
129         /**
130          * Finish previously started transaction.
131          */
132         int   (*dt_trans_stop)(const struct lu_env *env,
133                                struct thandle *th);
134         /**
135          * Add commit callback to the transaction.
136          */
137         int   (*dt_trans_cb_add)(struct thandle *th,
138                                  struct dt_txn_commit_cb *dcb);
139         /**
140          * Return fid of root index object.
141          */
142         int   (*dt_root_get)(const struct lu_env *env,
143                              struct dt_device *dev, struct lu_fid *f);
144         /**
145          * Return device configuration data.
146          */
147         void  (*dt_conf_get)(const struct lu_env *env,
148                              const struct dt_device *dev,
149                              struct dt_device_param *param);
150         /**
151          *  handling device state, mostly for tests
152          */
153         int   (*dt_sync)(const struct lu_env *env, struct dt_device *dev);
154         int   (*dt_ro)(const struct lu_env *env, struct dt_device *dev);
155         /**
156           * Start a transaction commit asynchronously
157           *
158           * \param env environment
159           * \param dev dt_device to start commit on
160           *
161           * \return 0 success, negative value if error
162           */
163          int   (*dt_commit_async)(const struct lu_env *env,
164                                   struct dt_device *dev);
165         /**
166          * Initialize capability context.
167          */
168         int   (*dt_init_capa_ctxt)(const struct lu_env *env,
169                                    struct dt_device *dev,
170                                    int mode, unsigned long timeout,
171                                    __u32 alg, struct lustre_capa_key *keys);
172         /**
173          * Initialize quota context.
174          */
175         void (*dt_init_quota_ctxt)(const struct lu_env *env,
176                                    struct dt_device *dev,
177                                    struct dt_quota_ctxt *ctxt, void *data);
178 };
179
180 struct dt_index_features {
181         /** required feature flags from enum dt_index_flags */
182         __u32 dif_flags;
183         /** minimal required key size */
184         size_t dif_keysize_min;
185         /** maximal required key size, 0 if no limit */
186         size_t dif_keysize_max;
187         /** minimal required record size */
188         size_t dif_recsize_min;
189         /** maximal required record size, 0 if no limit */
190         size_t dif_recsize_max;
191         /** pointer size for record */
192         size_t dif_ptrsize;
193 };
194
195 enum dt_index_flags {
196         /** index supports variable sized keys */
197         DT_IND_VARKEY = 1 << 0,
198         /** index supports variable sized records */
199         DT_IND_VARREC = 1 << 1,
200         /** index can be modified */
201         DT_IND_UPDATE = 1 << 2,
202         /** index supports records with non-unique (duplicate) keys */
203         DT_IND_NONUNQ = 1 << 3,
204         /**
205          * index support fixed-size keys sorted with natural numerical way
206          * and is able to return left-side value if no exact value found
207          */
208         DT_IND_RANGE = 1 << 4,
209 };
210
211 /**
212  * Features, required from index to support file system directories (mapping
213  * names to fids).
214  */
215 extern const struct dt_index_features dt_directory_features;
216 extern const struct dt_index_features dt_otable_features;
217
218 /* index features supported by the accounting objects */
219 extern const struct dt_index_features dt_acct_features;
220
221 /**
222  * This is a general purpose dt allocation hint.
223  * It now contains the parent object.
224  * It can contain any allocation hint in the future.
225  */
226 struct dt_allocation_hint {
227         struct dt_object           *dah_parent;
228         __u32                       dah_mode;
229 };
230
231 /**
232  * object type specifier.
233  */
234
235 enum dt_format_type {
236         DFT_REGULAR,
237         DFT_DIR,
238         /** for mknod */
239         DFT_NODE,
240         /** for special index */
241         DFT_INDEX,
242         /** for symbolic link */
243         DFT_SYM,
244 };
245
246 /**
247  * object format specifier.
248  */
249 struct dt_object_format {
250         /** type for dt object */
251         enum dt_format_type dof_type;
252         union {
253                 struct dof_regular {
254                 } dof_reg;
255                 struct dof_dir {
256                 } dof_dir;
257                 struct dof_node {
258                 } dof_node;
259                 /**
260                  * special index need feature as parameter to create
261                  * special idx
262                  */
263                 struct dof_index {
264                         const struct dt_index_features *di_feat;
265                 } dof_idx;
266         } u;
267 };
268
269 enum dt_format_type dt_mode_to_dft(__u32 mode);
270
271 typedef __u64 dt_obj_version_t;
272
273 /**
274  * Per-dt-object operations.
275  */
276 struct dt_object_operations {
277         void  (*do_read_lock)(const struct lu_env *env,
278                               struct dt_object *dt, unsigned role);
279         void  (*do_write_lock)(const struct lu_env *env,
280                                struct dt_object *dt, unsigned role);
281         void  (*do_read_unlock)(const struct lu_env *env,
282                                 struct dt_object *dt);
283         void  (*do_write_unlock)(const struct lu_env *env,
284                                  struct dt_object *dt);
285         int  (*do_write_locked)(const struct lu_env *env,
286                                 struct dt_object *dt);
287         /**
288          * Note: following ->do_{x,}attr_{set,get}() operations are very
289          * similar to ->moo_{x,}attr_{set,get}() operations in struct
290          * md_object_operations (see md_object.h). These operations are not in
291          * lu_object_operations, because ->do_{x,}attr_set() versions take
292          * transaction handle as an argument (this transaction is started by
293          * caller). We might factor ->do_{x,}attr_get() into
294          * lu_object_operations, but that would break existing symmetry.
295          */
296
297         /**
298          * Return standard attributes.
299          *
300          * precondition: lu_object_exists(&dt->do_lu);
301          */
302         int   (*do_attr_get)(const struct lu_env *env,
303                              struct dt_object *dt, struct lu_attr *attr,
304                              struct lustre_capa *capa);
305         /**
306          * Set standard attributes.
307          *
308          * precondition: dt_object_exists(dt);
309          */
310         int   (*do_declare_attr_set)(const struct lu_env *env,
311                                      struct dt_object *dt,
312                                      const struct lu_attr *attr,
313                                      struct thandle *handle);
314         int   (*do_attr_set)(const struct lu_env *env,
315                              struct dt_object *dt,
316                              const struct lu_attr *attr,
317                              struct thandle *handle,
318                              struct lustre_capa *capa);
319         /**
320          * Return a value of an extended attribute.
321          *
322          * precondition: dt_object_exists(dt);
323          */
324         int   (*do_xattr_get)(const struct lu_env *env, struct dt_object *dt,
325                               struct lu_buf *buf, const char *name,
326                               struct lustre_capa *capa);
327         /**
328          * Set value of an extended attribute.
329          *
330          * \a fl - flags from enum lu_xattr_flags
331          *
332          * precondition: dt_object_exists(dt);
333          */
334         int   (*do_declare_xattr_set)(const struct lu_env *env,
335                                       struct dt_object *dt,
336                                       const struct lu_buf *buf,
337                                       const char *name, int fl,
338                                       struct thandle *handle);
339         int   (*do_xattr_set)(const struct lu_env *env,
340                               struct dt_object *dt, const struct lu_buf *buf,
341                               const char *name, int fl, struct thandle *handle,
342                               struct lustre_capa *capa);
343         /**
344          * Delete existing extended attribute.
345          *
346          * precondition: dt_object_exists(dt);
347          */
348         int   (*do_declare_xattr_del)(const struct lu_env *env,
349                                       struct dt_object *dt,
350                                       const char *name, struct thandle *handle);
351         int   (*do_xattr_del)(const struct lu_env *env,
352                               struct dt_object *dt,
353                               const char *name, struct thandle *handle,
354                               struct lustre_capa *capa);
355         /**
356          * Place list of existing extended attributes into \a buf (which has
357          * length len).
358          *
359          * precondition: dt_object_exists(dt);
360          */
361         int   (*do_xattr_list)(const struct lu_env *env,
362                                struct dt_object *dt, struct lu_buf *buf,
363                                struct lustre_capa *capa);
364         /**
365          * Init allocation hint using parent object and child mode.
366          * (1) The \a parent might be NULL if this is a partial creation for
367          *     remote object.
368          * (2) The type of child is in \a child_mode.
369          * (3) The result hint is stored in \a ah;
370          */
371         void  (*do_ah_init)(const struct lu_env *env,
372                             struct dt_allocation_hint *ah,
373                             struct dt_object *parent,
374                             cfs_umode_t child_mode);
375         /**
376          * Create new object on this device.
377          *
378          * precondition: !dt_object_exists(dt);
379          * postcondition: ergo(result == 0, dt_object_exists(dt));
380          */
381         int   (*do_declare_create)(const struct lu_env *env,
382                                    struct dt_object *dt,
383                                    struct lu_attr *attr,
384                                    struct dt_allocation_hint *hint,
385                                    struct dt_object_format *dof,
386                                    struct thandle *th);
387         int   (*do_create)(const struct lu_env *env, struct dt_object *dt,
388                            struct lu_attr *attr,
389                            struct dt_allocation_hint *hint,
390                            struct dt_object_format *dof,
391                            struct thandle *th);
392
393         /**
394           Destroy object on this device
395          * precondition: !dt_object_exists(dt);
396          * postcondition: ergo(result == 0, dt_object_exists(dt));
397          */
398         int   (*do_declare_destroy)(const struct lu_env *env,
399                                     struct dt_object *dt,
400                                     struct thandle *th);
401         int   (*do_destroy)(const struct lu_env *env, struct dt_object *dt,
402                             struct thandle *th);
403
404         /**
405          * Announce that this object is going to be used as an index. This
406          * operation check that object supports indexing operations and
407          * installs appropriate dt_index_operations vector on success.
408          *
409          * Also probes for features. Operation is successful if all required
410          * features are supported.
411          */
412         int   (*do_index_try)(const struct lu_env *env,
413                               struct dt_object *dt,
414                               const struct dt_index_features *feat);
415         /**
416          * Add nlink of the object
417          * precondition: dt_object_exists(dt);
418          */
419         int   (*do_declare_ref_add)(const struct lu_env *env,
420                                     struct dt_object *dt, struct thandle *th);
421         int   (*do_ref_add)(const struct lu_env *env,
422                             struct dt_object *dt, struct thandle *th);
423         /**
424          * Del nlink of the object
425          * precondition: dt_object_exists(dt);
426          */
427         int   (*do_declare_ref_del)(const struct lu_env *env,
428                                     struct dt_object *dt, struct thandle *th);
429         int   (*do_ref_del)(const struct lu_env *env,
430                             struct dt_object *dt, struct thandle *th);
431
432         struct obd_capa *(*do_capa_get)(const struct lu_env *env,
433                                         struct dt_object *dt,
434                                         struct lustre_capa *old,
435                                         __u64 opc);
436         int (*do_object_sync)(const struct lu_env *, struct dt_object *);
437         /**
438          * Get object info of next level. Currently, only get inode from osd.
439          * This is only used by quota b=16542
440          * precondition: dt_object_exists(dt);
441          */
442         int (*do_data_get)(const struct lu_env *env, struct dt_object *dt,
443                            void **data);
444 };
445
446 /**
447  * Per-dt-object operations on "file body".
448  */
449 struct dt_body_operations {
450         /**
451          * precondition: dt_object_exists(dt);
452          */
453         ssize_t (*dbo_read)(const struct lu_env *env, struct dt_object *dt,
454                             struct lu_buf *buf, loff_t *pos,
455                             struct lustre_capa *capa);
456         /**
457          * precondition: dt_object_exists(dt);
458          */
459         ssize_t (*dbo_declare_write)(const struct lu_env *env,
460                                      struct dt_object *dt,
461                                      const loff_t size, loff_t pos,
462                                      struct thandle *handle);
463         ssize_t (*dbo_write)(const struct lu_env *env, struct dt_object *dt,
464                              const struct lu_buf *buf, loff_t *pos,
465                              struct thandle *handle, struct lustre_capa *capa,
466                              int ignore_quota);
467         /*
468          * methods for zero-copy IO
469          */
470
471         /*
472          * precondition: dt_object_exists(dt);
473          * returns:
474          * < 0 - error code
475          * = 0 - illegal
476          * > 0 - number of local buffers prepared
477          */
478         int (*dbo_bufs_get)(const struct lu_env *env, struct dt_object *dt,
479                             loff_t pos, ssize_t len, struct niobuf_local *lb,
480                             int rw, struct lustre_capa *capa);
481         /*
482          * precondition: dt_object_exists(dt);
483          */
484         int (*dbo_bufs_put)(const struct lu_env *env, struct dt_object *dt,
485                             struct niobuf_local *lb, int nr);
486         /*
487          * precondition: dt_object_exists(dt);
488          */
489         int (*dbo_write_prep)(const struct lu_env *env, struct dt_object *dt,
490                               struct niobuf_local *lb, int nr);
491         /*
492          * precondition: dt_object_exists(dt);
493          */
494         int (*dbo_declare_write_commit)(const struct lu_env *env,
495                                         struct dt_object *dt,
496                                         struct niobuf_local *,
497                                         int, struct thandle *);
498         /*
499          * precondition: dt_object_exists(dt);
500          */
501         int (*dbo_write_commit)(const struct lu_env *env, struct dt_object *dt,
502                                 struct niobuf_local *, int, struct thandle *);
503         /*
504          * precondition: dt_object_exists(dt);
505          */
506         int (*dbo_read_prep)(const struct lu_env *env, struct dt_object *dt,
507                              struct niobuf_local *lnb, int nr);
508         int (*dbo_fiemap_get)(const struct lu_env *env, struct dt_object *dt,
509                               struct ll_user_fiemap *fm);
510         /**
511          * Punch object's content
512          * precondition: regular object, not index
513          */
514         int   (*do_declare_punch)(const struct lu_env *, struct dt_object *,
515                                   __u64, __u64, struct thandle *th);
516         int   (*do_punch)(const struct lu_env *env, struct dt_object *dt,
517                           __u64 start, __u64 end, struct thandle *th,
518                           struct lustre_capa *capa);
519 };
520
521 /**
522  * Incomplete type of index record.
523  */
524 struct dt_rec;
525
526 /**
527  * Incomplete type of index key.
528  */
529 struct dt_key;
530
531 /**
532  * Incomplete type of dt iterator.
533  */
534 struct dt_it;
535
536 /**
537  * Per-dt-object operations on object as index.
538  */
539 struct dt_index_operations {
540         /**
541          * precondition: dt_object_exists(dt);
542          */
543         int (*dio_lookup)(const struct lu_env *env, struct dt_object *dt,
544                           struct dt_rec *rec, const struct dt_key *key,
545                           struct lustre_capa *capa);
546         /**
547          * precondition: dt_object_exists(dt);
548          */
549         int (*dio_declare_insert)(const struct lu_env *env,
550                                   struct dt_object *dt,
551                                   const struct dt_rec *rec,
552                                   const struct dt_key *key,
553                                   struct thandle *handle);
554         int (*dio_insert)(const struct lu_env *env, struct dt_object *dt,
555                           const struct dt_rec *rec, const struct dt_key *key,
556                           struct thandle *handle, struct lustre_capa *capa,
557                           int ignore_quota);
558         /**
559          * precondition: dt_object_exists(dt);
560          */
561         int (*dio_declare_delete)(const struct lu_env *env,
562                                   struct dt_object *dt,
563                                   const struct dt_key *key,
564                                   struct thandle *handle);
565         int (*dio_delete)(const struct lu_env *env, struct dt_object *dt,
566                           const struct dt_key *key, struct thandle *handle,
567                           struct lustre_capa *capa);
568         /**
569          * Iterator interface
570          */
571         struct dt_it_ops {
572                 /**
573                  * Allocate and initialize new iterator.
574                  *
575                  * precondition: dt_object_exists(dt);
576                  */
577                 struct dt_it *(*init)(const struct lu_env *env,
578                                       struct dt_object *dt,
579                                       __u32 attr,
580                                       struct lustre_capa *capa);
581                 void          (*fini)(const struct lu_env *env,
582                                       struct dt_it *di);
583                 int            (*get)(const struct lu_env *env,
584                                       struct dt_it *di,
585                                       const struct dt_key *key);
586                 void           (*put)(const struct lu_env *env,
587                                       struct dt_it *di);
588                 int           (*next)(const struct lu_env *env,
589                                       struct dt_it *di);
590                 struct dt_key *(*key)(const struct lu_env *env,
591                                       const struct dt_it *di);
592                 int       (*key_size)(const struct lu_env *env,
593                                       const struct dt_it *di);
594                 int            (*rec)(const struct lu_env *env,
595                                       const struct dt_it *di,
596                                       struct dt_rec *rec,
597                                       __u32 attr);
598                 __u64        (*store)(const struct lu_env *env,
599                                       const struct dt_it *di);
600                 int           (*load)(const struct lu_env *env,
601                                       const struct dt_it *di, __u64 hash);
602                 int        (*key_rec)(const struct lu_env *env,
603                                       const struct dt_it *di, void* key_rec);
604         } dio_it;
605 };
606
607 enum dt_otable_it_valid {
608         DOIV_ERROR_HANDLE       = 0x0001,
609 };
610
611 enum dt_otable_it_flags {
612         /* Exit when fail. */
613         DOIF_FAILOUT    = 0x0001,
614
615         /* Reset iteration position to the device beginning. */
616         DOIF_RESET      = 0x0002,
617
618         /* There is up layer component uses the iteration. */
619         DOIF_OUTUSED    = 0x0004,
620 };
621
622 /* otable based iteration needs to use the common DT interation APIs.
623  * To initialize the iteration, it needs call dio_it::init() firstly.
624  * Here is how the otable based iteration should prepare arguments to
625  * call dt_it_ops::init().
626  *
627  * For otable based iteration, the 32-bits 'attr' for dt_it_ops::init()
628  * is composed of two parts:
629  * low 16-bits is for valid bits, high 16-bits is for flags bits. */
630 #define DT_OTABLE_IT_FLAGS_SHIFT        16
631 #define DT_OTABLE_IT_FLAGS_MASK         0xffff0000
632
633 struct dt_device {
634         struct lu_device                   dd_lu_dev;
635         const struct dt_device_operations *dd_ops;
636
637         /**
638          * List of dt_txn_callback (see below). This is not protected in any
639          * way, because callbacks are supposed to be added/deleted only during
640          * single-threaded start-up shut-down procedures.
641          */
642         cfs_list_t                         dd_txn_callbacks;
643 };
644
645 int  dt_device_init(struct dt_device *dev, struct lu_device_type *t);
646 void dt_device_fini(struct dt_device *dev);
647
648 static inline int lu_device_is_dt(const struct lu_device *d)
649 {
650         return ergo(d != NULL, d->ld_type->ldt_tags & LU_DEVICE_DT);
651 }
652
653 static inline struct dt_device * lu2dt_dev(struct lu_device *l)
654 {
655         LASSERT(lu_device_is_dt(l));
656         return container_of0(l, struct dt_device, dd_lu_dev);
657 }
658
659 struct dt_object {
660         struct lu_object                   do_lu;
661         const struct dt_object_operations *do_ops;
662         const struct dt_body_operations   *do_body_ops;
663         const struct dt_index_operations  *do_index_ops;
664 };
665
666 static inline struct dt_object *lu2dt(struct lu_object *l)
667 {
668         LASSERT(l == NULL || IS_ERR(l) || lu_device_is_dt(l->lo_dev));
669         return container_of0(l, struct dt_object, do_lu);
670 }
671
672 int  dt_object_init(struct dt_object *obj,
673                     struct lu_object_header *h, struct lu_device *d);
674
675 void dt_object_fini(struct dt_object *obj);
676
677 static inline int dt_object_exists(const struct dt_object *dt)
678 {
679         return lu_object_exists(&dt->do_lu);
680 }
681
682 /**
683  * This is the general purpose transaction handle.
684  * 1. Transaction Life Cycle
685  *      This transaction handle is allocated upon starting a new transaction,
686  *      and deallocated after this transaction is committed.
687  * 2. Transaction Nesting
688  *      We do _NOT_ support nested transaction. So, every thread should only
689  *      have one active transaction, and a transaction only belongs to one
690  *      thread. Due to this, transaction handle need no reference count.
691  * 3. Transaction & dt_object locking
692  *      dt_object locks should be taken inside transaction.
693  * 4. Transaction & RPC
694  *      No RPC request should be issued inside transaction.
695  */
696 struct thandle {
697         /** the dt device on which the transactions are executed */
698         struct dt_device *th_dev;
699
700         /** additional tags (layers can add in declare) */
701         __u32             th_tags;
702
703         /** context for this transaction, tag is LCT_TX_HANDLE */
704         struct lu_context th_ctx;
705
706         /** the last operation result in this transaction.
707          * this value is used in recovery */
708         __s32             th_result;
709
710         /** whether we need sync commit */
711         int               th_sync:1;
712
713         /* local transation, no need to inform other layers */
714         int               th_local:1;
715 };
716
717 /**
718  * Transaction call-backs.
719  *
720  * These are invoked by osd (or underlying transaction engine) when
721  * transaction changes state.
722  *
723  * Call-backs are used by upper layers to modify transaction parameters and to
724  * perform some actions on for each transaction state transition. Typical
725  * example is mdt registering call-back to write into last-received file
726  * before each transaction commit.
727  */
728 struct dt_txn_callback {
729         int (*dtc_txn_start)(const struct lu_env *env,
730                              struct thandle *txn, void *cookie);
731         int (*dtc_txn_stop)(const struct lu_env *env,
732                             struct thandle *txn, void *cookie);
733         void (*dtc_txn_commit)(struct thandle *txn, void *cookie);
734         void                *dtc_cookie;
735         __u32                dtc_tag;
736         cfs_list_t           dtc_linkage;
737 };
738
739 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb);
740 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb);
741
742 int dt_txn_hook_start(const struct lu_env *env,
743                       struct dt_device *dev, struct thandle *txn);
744 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *txn);
745 void dt_txn_hook_commit(struct thandle *txn);
746
747 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj);
748
749 /**
750  * Callback function used for parsing path.
751  * \see llo_store_resolve
752  */
753 typedef int (*dt_entry_func_t)(const struct lu_env *env,
754                             const char *name,
755                             void *pvt);
756
757 #define DT_MAX_PATH 1024
758
759 int dt_path_parser(const struct lu_env *env,
760                    char *local, dt_entry_func_t entry_func,
761                    void *data);
762
763 struct dt_object *dt_store_open(const struct lu_env *env,
764                                 struct dt_device *dt,
765                                 const char *dirname,
766                                 const char *filename,
767                                 struct lu_fid *fid);
768
769 struct dt_object *dt_find_or_create(const struct lu_env *env,
770                                     struct dt_device *dt,
771                                     const struct lu_fid *fid,
772                                     struct dt_object_format *dof,
773                                     struct lu_attr *attr);
774
775 struct dt_object *dt_locate(const struct lu_env *env,
776                             struct dt_device *dev,
777                             const struct lu_fid *fid);
778
779 static inline int dt_object_sync(const struct lu_env *env,
780                                  struct dt_object *o)
781 {
782         LASSERT(o);
783         LASSERT(o->do_ops);
784         LASSERT(o->do_ops->do_object_sync);
785         return o->do_ops->do_object_sync(env, o);
786 }
787
788 int dt_declare_version_set(const struct lu_env *env, struct dt_object *o,
789                            struct thandle *th);
790 void dt_version_set(const struct lu_env *env, struct dt_object *o,
791                     dt_obj_version_t version, struct thandle *th);
792 dt_obj_version_t dt_version_get(const struct lu_env *env, struct dt_object *o);
793
794
795 int dt_read(const struct lu_env *env, struct dt_object *dt,
796             struct lu_buf *buf, loff_t *pos);
797 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
798                    struct lu_buf *buf, loff_t *pos);
799 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
800                     const struct lu_buf *buf, loff_t *pos, struct thandle *th);
801
802 static inline struct thandle *dt_trans_create(const struct lu_env *env,
803                                               struct dt_device *d)
804 {
805         LASSERT(d->dd_ops->dt_trans_create);
806         return d->dd_ops->dt_trans_create(env, d);
807 }
808
809 static inline int dt_trans_start(const struct lu_env *env,
810                                  struct dt_device *d, struct thandle *th)
811 {
812         LASSERT(d->dd_ops->dt_trans_start);
813         return d->dd_ops->dt_trans_start(env, d, th);
814 }
815
816 /* for this transaction hooks shouldn't be called */
817 static inline int dt_trans_start_local(const struct lu_env *env,
818                                        struct dt_device *d, struct thandle *th)
819 {
820         LASSERT(d->dd_ops->dt_trans_start);
821         th->th_local = 1;
822         return d->dd_ops->dt_trans_start(env, d, th);
823 }
824
825 static inline int dt_trans_stop(const struct lu_env *env,
826                                 struct dt_device *d, struct thandle *th)
827 {
828         LASSERT(d->dd_ops->dt_trans_stop);
829         return d->dd_ops->dt_trans_stop(env, th);
830 }
831
832 static inline int dt_trans_cb_add(struct thandle *th,
833                                   struct dt_txn_commit_cb *dcb)
834 {
835         LASSERT(th->th_dev->dd_ops->dt_trans_cb_add);
836         return th->th_dev->dd_ops->dt_trans_cb_add(th, dcb);
837 }
838 /** @} dt */
839
840
841 static inline int dt_declare_record_write(const struct lu_env *env,
842                                           struct dt_object *dt,
843                                           int size, loff_t pos,
844                                           struct thandle *th)
845 {
846         int rc;
847
848         LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
849         LASSERT(th != NULL);
850         LASSERT(dt->do_body_ops);
851         LASSERT(dt->do_body_ops->dbo_declare_write);
852         rc = dt->do_body_ops->dbo_declare_write(env, dt, size, pos, th);
853         return rc;
854 }
855
856 static inline int dt_declare_create(const struct lu_env *env,
857                                     struct dt_object *dt,
858                                     struct lu_attr *attr,
859                                     struct dt_allocation_hint *hint,
860                                     struct dt_object_format *dof,
861                                     struct thandle *th)
862 {
863         LASSERT(dt);
864         LASSERT(dt->do_ops);
865         LASSERT(dt->do_ops->do_declare_create);
866         return dt->do_ops->do_declare_create(env, dt, attr, hint, dof, th);
867 }
868
869 static inline int dt_create(const struct lu_env *env,
870                                     struct dt_object *dt,
871                                     struct lu_attr *attr,
872                                     struct dt_allocation_hint *hint,
873                                     struct dt_object_format *dof,
874                                     struct thandle *th)
875 {
876         LASSERT(dt);
877         LASSERT(dt->do_ops);
878         LASSERT(dt->do_ops->do_create);
879         return dt->do_ops->do_create(env, dt, attr, hint, dof, th);
880 }
881
882 static inline int dt_declare_destroy(const struct lu_env *env,
883                                      struct dt_object *dt,
884                                      struct thandle *th)
885 {
886         LASSERT(dt);
887         LASSERT(dt->do_ops);
888         LASSERT(dt->do_ops->do_declare_destroy);
889         return dt->do_ops->do_declare_destroy(env, dt, th);
890 }
891
892 static inline int dt_destroy(const struct lu_env *env,
893                              struct dt_object *dt,
894                              struct thandle *th)
895 {
896         LASSERT(dt);
897         LASSERT(dt->do_ops);
898         LASSERT(dt->do_ops->do_destroy);
899         return dt->do_ops->do_destroy(env, dt, th);
900 }
901
902 static inline void dt_read_lock(const struct lu_env *env,
903                                 struct dt_object *dt,
904                                 unsigned role)
905 {
906         LASSERT(dt);
907         LASSERT(dt->do_ops);
908         LASSERT(dt->do_ops->do_read_lock);
909         dt->do_ops->do_read_lock(env, dt, role);
910 }
911
912 static inline void dt_write_lock(const struct lu_env *env,
913                                 struct dt_object *dt,
914                                 unsigned role)
915 {
916         LASSERT(dt);
917         LASSERT(dt->do_ops);
918         LASSERT(dt->do_ops->do_write_lock);
919         dt->do_ops->do_write_lock(env, dt, role);
920 }
921
922 static inline void dt_read_unlock(const struct lu_env *env,
923                                 struct dt_object *dt)
924 {
925         LASSERT(dt);
926         LASSERT(dt->do_ops);
927         LASSERT(dt->do_ops->do_read_unlock);
928         dt->do_ops->do_read_unlock(env, dt);
929 }
930
931 static inline void dt_write_unlock(const struct lu_env *env,
932                                 struct dt_object *dt)
933 {
934         LASSERT(dt);
935         LASSERT(dt->do_ops);
936         LASSERT(dt->do_ops->do_write_unlock);
937         dt->do_ops->do_write_unlock(env, dt);
938 }
939
940 static inline int dt_write_locked(const struct lu_env *env,
941                                   struct dt_object *dt)
942 {
943         LASSERT(dt);
944         LASSERT(dt->do_ops);
945         LASSERT(dt->do_ops->do_write_locked);
946         return dt->do_ops->do_write_locked(env, dt);
947 }
948
949 static inline int dt_attr_get(const struct lu_env *env, struct dt_object *dt,
950                               struct lu_attr *la, void *arg)
951 {
952         LASSERT(dt);
953         LASSERT(dt->do_ops);
954         LASSERT(dt->do_ops->do_attr_get);
955         return dt->do_ops->do_attr_get(env, dt, la, arg);
956 }
957
958 static inline int dt_declare_attr_set(const struct lu_env *env,
959                                       struct dt_object *dt,
960                                       const struct lu_attr *la,
961                                       struct thandle *th)
962 {
963         LASSERT(dt);
964         LASSERT(dt->do_ops);
965         LASSERT(dt->do_ops->do_declare_attr_set);
966         return dt->do_ops->do_declare_attr_set(env, dt, la, th);
967 }
968
969 static inline int dt_attr_set(const struct lu_env *env, struct dt_object *dt,
970                               const struct lu_attr *la, struct thandle *th,
971                               struct lustre_capa *capa)
972 {
973         LASSERT(dt);
974         LASSERT(dt->do_ops);
975         LASSERT(dt->do_ops->do_attr_set);
976         return dt->do_ops->do_attr_set(env, dt, la, th, capa);
977 }
978
979 static inline int dt_declare_ref_add(const struct lu_env *env,
980                                      struct dt_object *dt, struct thandle *th)
981 {
982         LASSERT(dt);
983         LASSERT(dt->do_ops);
984         LASSERT(dt->do_ops->do_declare_ref_add);
985         return dt->do_ops->do_declare_ref_add(env, dt, th);
986 }
987
988 static inline int dt_ref_add(const struct lu_env *env,
989                              struct dt_object *dt, struct thandle *th)
990 {
991         LASSERT(dt);
992         LASSERT(dt->do_ops);
993         LASSERT(dt->do_ops->do_ref_add);
994         return dt->do_ops->do_ref_add(env, dt, th);
995 }
996
997 static inline int dt_declare_ref_del(const struct lu_env *env,
998                                      struct dt_object *dt, struct thandle *th)
999 {
1000         LASSERT(dt);
1001         LASSERT(dt->do_ops);
1002         LASSERT(dt->do_ops->do_declare_ref_del);
1003         return dt->do_ops->do_declare_ref_del(env, dt, th);
1004 }
1005
1006 static inline int dt_ref_del(const struct lu_env *env,
1007                              struct dt_object *dt, struct thandle *th)
1008 {
1009         LASSERT(dt);
1010         LASSERT(dt->do_ops);
1011         LASSERT(dt->do_ops->do_ref_del);
1012         return dt->do_ops->do_ref_del(env, dt, th);
1013 }
1014
1015 static inline struct obd_capa *dt_capa_get(const struct lu_env *env,
1016                                            struct dt_object *dt,
1017                                            struct lustre_capa *old, __u64 opc)
1018 {
1019         LASSERT(dt);
1020         LASSERT(dt->do_ops);
1021         LASSERT(dt->do_ops->do_ref_del);
1022         return dt->do_ops->do_capa_get(env, dt, old, opc);
1023 }
1024
1025 static inline int dt_bufs_get(const struct lu_env *env, struct dt_object *d,
1026                               struct niobuf_remote *rnb,
1027                               struct niobuf_local *lnb, int rw,
1028                               struct lustre_capa *capa)
1029 {
1030         LASSERT(d);
1031         LASSERT(d->do_body_ops);
1032         LASSERT(d->do_body_ops->dbo_bufs_get);
1033         return d->do_body_ops->dbo_bufs_get(env, d, rnb->offset,
1034                                             rnb->len, lnb, rw, capa);
1035 }
1036
1037 static inline int dt_bufs_put(const struct lu_env *env, struct dt_object *d,
1038                               struct niobuf_local *lnb, int n)
1039 {
1040         LASSERT(d);
1041         LASSERT(d->do_body_ops);
1042         LASSERT(d->do_body_ops->dbo_bufs_put);
1043         return d->do_body_ops->dbo_bufs_put(env, d, lnb, n);
1044 }
1045
1046 static inline int dt_write_prep(const struct lu_env *env, struct dt_object *d,
1047                                 struct niobuf_local *lnb, int n)
1048 {
1049         LASSERT(d);
1050         LASSERT(d->do_body_ops);
1051         LASSERT(d->do_body_ops->dbo_write_prep);
1052         return d->do_body_ops->dbo_write_prep(env, d, lnb, n);
1053 }
1054
1055 static inline int dt_declare_write_commit(const struct lu_env *env,
1056                                           struct dt_object *d,
1057                                           struct niobuf_local *lnb,
1058                                           int n, struct thandle *th)
1059 {
1060         LASSERTF(d != NULL, "dt is NULL when we want to declare write\n");
1061         LASSERT(th != NULL);
1062         return d->do_body_ops->dbo_declare_write_commit(env, d, lnb, n, th);
1063 }
1064
1065
1066 static inline int dt_write_commit(const struct lu_env *env,
1067                                   struct dt_object *d, struct niobuf_local *lnb,
1068                                   int n, struct thandle *th)
1069 {
1070         LASSERT(d);
1071         LASSERT(d->do_body_ops);
1072         LASSERT(d->do_body_ops->dbo_write_commit);
1073         return d->do_body_ops->dbo_write_commit(env, d, lnb, n, th);
1074 }
1075
1076 static inline int dt_read_prep(const struct lu_env *env, struct dt_object *d,
1077                                struct niobuf_local *lnb, int n)
1078 {
1079         LASSERT(d);
1080         LASSERT(d->do_body_ops);
1081         LASSERT(d->do_body_ops->dbo_read_prep);
1082         return d->do_body_ops->dbo_read_prep(env, d, lnb, n);
1083 }
1084
1085 static inline int dt_declare_punch(const struct lu_env *env,
1086                                    struct dt_object *dt, __u64 start,
1087                                    __u64 end, struct thandle *th)
1088 {
1089         LASSERT(dt);
1090         LASSERT(dt->do_body_ops);
1091         LASSERT(dt->do_body_ops->do_declare_punch);
1092         return dt->do_body_ops->do_declare_punch(env, dt, start, end, th);
1093 }
1094
1095 static inline int dt_punch(const struct lu_env *env, struct dt_object *dt,
1096                            __u64 start, __u64 end, struct thandle *th,
1097                            struct lustre_capa *capa)
1098 {
1099         LASSERT(dt);
1100         LASSERT(dt->do_body_ops);
1101         LASSERT(dt->do_body_ops->do_punch);
1102         return dt->do_body_ops->do_punch(env, dt, start, end, th, capa);
1103 }
1104
1105 static inline int dt_fiemap_get(const struct lu_env *env, struct dt_object *d,
1106                                 struct ll_user_fiemap *fm)
1107 {
1108         LASSERT(d);
1109         if (d->do_body_ops == NULL)
1110                 return -EPROTO;
1111         LASSERT(d->do_body_ops->dbo_fiemap_get);
1112         return d->do_body_ops->dbo_fiemap_get(env, d, fm);
1113 }
1114
1115 static inline int dt_statfs(const struct lu_env *env, struct dt_device *dev,
1116                             struct obd_statfs *osfs)
1117 {
1118         LASSERT(dev);
1119         LASSERT(dev->dd_ops);
1120         LASSERT(dev->dd_ops->dt_statfs);
1121         return dev->dd_ops->dt_statfs(env, dev, osfs);
1122 }
1123
1124 static inline int dt_root_get(const struct lu_env *env, struct dt_device *dev,
1125                               struct lu_fid *f)
1126 {
1127         LASSERT(dev);
1128         LASSERT(dev->dd_ops);
1129         LASSERT(dev->dd_ops->dt_root_get);
1130         return dev->dd_ops->dt_root_get(env, dev, f);
1131 }
1132
1133 static inline void dt_conf_get(const struct lu_env *env,
1134                                const struct dt_device *dev,
1135                                struct dt_device_param *param)
1136 {
1137         LASSERT(dev);
1138         LASSERT(dev->dd_ops);
1139         LASSERT(dev->dd_ops->dt_conf_get);
1140         return dev->dd_ops->dt_conf_get(env, dev, param);
1141 }
1142
1143 static inline int dt_sync(const struct lu_env *env, struct dt_device *dev)
1144 {
1145         LASSERT(dev);
1146         LASSERT(dev->dd_ops);
1147         LASSERT(dev->dd_ops->dt_sync);
1148         return dev->dd_ops->dt_sync(env, dev);
1149 }
1150
1151 static inline int dt_ro(const struct lu_env *env, struct dt_device *dev)
1152 {
1153         LASSERT(dev);
1154         LASSERT(dev->dd_ops);
1155         LASSERT(dev->dd_ops->dt_ro);
1156         return dev->dd_ops->dt_ro(env, dev);
1157 }
1158
1159 static inline int dt_declare_insert(const struct lu_env *env,
1160                                     struct dt_object *dt,
1161                                     const struct dt_rec *rec,
1162                                     const struct dt_key *key,
1163                                     struct thandle *th)
1164 {
1165         LASSERT(dt);
1166         LASSERT(dt->do_index_ops);
1167         LASSERT(dt->do_index_ops->dio_declare_insert);
1168         return dt->do_index_ops->dio_declare_insert(env, dt, rec, key, th);
1169 }
1170
1171 static inline int dt_insert(const struct lu_env *env,
1172                                     struct dt_object *dt,
1173                                     const struct dt_rec *rec,
1174                                     const struct dt_key *key,
1175                                     struct thandle *th,
1176                                     struct lustre_capa *capa,
1177                                     int noquota)
1178 {
1179         LASSERT(dt);
1180         LASSERT(dt->do_index_ops);
1181         LASSERT(dt->do_index_ops->dio_insert);
1182         return dt->do_index_ops->dio_insert(env, dt, rec, key, th,
1183                                             capa, noquota);
1184 }
1185
1186 static inline int dt_declare_xattr_del(const struct lu_env *env,
1187                                        struct dt_object *dt,
1188                                        const char *name,
1189                                        struct thandle *th)
1190 {
1191         LASSERT(dt);
1192         LASSERT(dt->do_ops);
1193         LASSERT(dt->do_ops->do_declare_xattr_del);
1194         return dt->do_ops->do_declare_xattr_del(env, dt, name, th);
1195 }
1196
1197 static inline int dt_xattr_del(const struct lu_env *env,
1198                                struct dt_object *dt, const char *name,
1199                                struct thandle *th,
1200                                struct lustre_capa *capa)
1201 {
1202         LASSERT(dt);
1203         LASSERT(dt->do_ops);
1204         LASSERT(dt->do_ops->do_xattr_del);
1205         return dt->do_ops->do_xattr_del(env, dt, name, th, capa);
1206 }
1207
1208 static inline int dt_declare_xattr_set(const struct lu_env *env,
1209                                       struct dt_object *dt,
1210                                       const struct lu_buf *buf,
1211                                       const char *name, int fl,
1212                                       struct thandle *th)
1213 {
1214         LASSERT(dt);
1215         LASSERT(dt->do_ops);
1216         LASSERT(dt->do_ops->do_declare_xattr_set);
1217         return dt->do_ops->do_declare_xattr_set(env, dt, buf, name, fl, th);
1218 }
1219
1220 static inline int dt_xattr_set(const struct lu_env *env,
1221                               struct dt_object *dt, const struct lu_buf *buf,
1222                               const char *name, int fl, struct thandle *th,
1223                               struct lustre_capa *capa)
1224 {
1225         LASSERT(dt);
1226         LASSERT(dt->do_ops);
1227         LASSERT(dt->do_ops->do_xattr_set);
1228         return dt->do_ops->do_xattr_set(env, dt, buf, name, fl, th, capa);
1229 }
1230
1231 static inline int dt_xattr_get(const struct lu_env *env,
1232                               struct dt_object *dt, struct lu_buf *buf,
1233                               const char *name, struct lustre_capa *capa)
1234 {
1235         LASSERT(dt);
1236         LASSERT(dt->do_ops);
1237         LASSERT(dt->do_ops->do_xattr_get);
1238         return dt->do_ops->do_xattr_get(env, dt, buf, name, capa);
1239 }
1240
1241 static inline int dt_xattr_list(const struct lu_env *env,
1242                                struct dt_object *dt, struct lu_buf *buf,
1243                                struct lustre_capa *capa)
1244 {
1245         LASSERT(dt);
1246         LASSERT(dt->do_ops);
1247         LASSERT(dt->do_ops->do_xattr_list);
1248         return dt->do_ops->do_xattr_list(env, dt, buf, capa);
1249 }
1250
1251 static inline int dt_declare_delete(const struct lu_env *env,
1252                                     struct dt_object *dt,
1253                                     const struct dt_key *key,
1254                                     struct thandle *th)
1255 {
1256         LASSERT(dt);
1257         LASSERT(dt->do_index_ops);
1258         LASSERT(dt->do_index_ops->dio_declare_delete);
1259         return dt->do_index_ops->dio_declare_delete(env, dt, key, th);
1260 }
1261
1262 static inline int dt_delete(const struct lu_env *env,
1263                             struct dt_object *dt,
1264                             const struct dt_key *key,
1265                             struct thandle *th,
1266                             struct lustre_capa *capa)
1267 {
1268         LASSERT(dt);
1269         LASSERT(dt->do_index_ops);
1270         LASSERT(dt->do_index_ops->dio_delete);
1271         return dt->do_index_ops->dio_delete(env, dt, key, th, capa);
1272 }
1273
1274 static inline int dt_commit_async(const struct lu_env *env,
1275                                   struct dt_device *dev)
1276 {
1277         LASSERT(dev);
1278         LASSERT(dev->dd_ops);
1279         LASSERT(dev->dd_ops->dt_commit_async);
1280         return dev->dd_ops->dt_commit_async(env, dev);
1281 }
1282
1283 static inline int dt_lookup(const struct lu_env *env,
1284                             struct dt_object *dt,
1285                             struct dt_rec *rec,
1286                             const struct dt_key *key,
1287                             struct lustre_capa *capa)
1288 {
1289         int ret;
1290
1291         LASSERT(dt);
1292         LASSERT(dt->do_index_ops);
1293         LASSERT(dt->do_index_ops->dio_lookup);
1294
1295         ret = dt->do_index_ops->dio_lookup(env, dt, rec, key, capa);
1296         if (ret > 0)
1297                 ret = 0;
1298         else if (ret == 0)
1299                 ret = -ENOENT;
1300         return ret;
1301 }
1302
1303 #define LU221_BAD_TIME (0x80000000U + 24 * 3600)
1304
1305 #endif /* __LUSTRE_DT_OBJECT_H */