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