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