Whamcloud - gitweb
cdd321a9ad2b165e1244d63e7da42c87fd4f830f
[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, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef __LUSTRE_DT_OBJECT_H
38 #define __LUSTRE_DT_OBJECT_H
39
40 /** \defgroup dt dt
41  * Sub-class of lu_object with methods common for "data" objects in OST stack.
42  *
43  * Data objects behave like regular files: you can read/write them, get and
44  * set their attributes. Implementation of dt interface is supposed to
45  * implement some form of garbage collection, normally reference counting
46  * (nlink) based one.
47  *
48  * Examples: osd (lustre/osd) is an implementation of dt interface.
49  * @{
50  */
51
52 #include <obd_support.h>
53 /*
54  * super-class definitions.
55  */
56 #include <lu_object.h>
57
58 #include <libcfs/libcfs.h>
59
60 struct seq_file;
61 struct proc_dir_entry;
62 struct lustre_cfg;
63
64 struct thandle;
65 struct dt_device;
66 struct dt_object;
67 struct dt_index_features;
68 struct niobuf_local;
69 struct niobuf_remote;
70 struct ldlm_enqueue_info;
71
72 typedef enum {
73         MNTOPT_USERXATTR        = 0x00000001,
74         MNTOPT_ACL              = 0x00000002,
75 } mntopt_t;
76
77 struct dt_device_param {
78         unsigned           ddp_max_name_len;
79         unsigned           ddp_max_nlink;
80         unsigned           ddp_block_shift;
81         mntopt_t           ddp_mntopts;
82         unsigned           ddp_max_ea_size;
83         int                ddp_mount_type;
84         unsigned long long ddp_maxbytes;
85         /* percentage of available space to reserve for grant error margin */
86         int                ddp_grant_reserved;
87         /* per-inode space consumption */
88         short              ddp_inodespace;
89         /* per-fragment grant overhead to be used by client for grant
90          * calculation */
91         int                ddp_grant_frag;
92 };
93
94 /**
95  * Per-transaction commit callback function
96  */
97 struct dt_txn_commit_cb;
98 typedef void (*dt_cb_t)(struct lu_env *env, struct thandle *th,
99                         struct dt_txn_commit_cb *cb, int err);
100 /**
101  * Special per-transaction callback for cases when just commit callback
102  * is needed and per-device callback are not convenient to use
103  */
104 #define TRANS_COMMIT_CB_MAGIC   0xa0a00a0a
105 #define MAX_COMMIT_CB_STR_LEN   32
106
107 struct dt_txn_commit_cb {
108         cfs_list_t      dcb_linkage;
109         dt_cb_t         dcb_func;
110         __u32           dcb_magic;
111         char            dcb_name[MAX_COMMIT_CB_STR_LEN];
112 };
113
114 /**
115  * Operations on dt device.
116  */
117 struct dt_device_operations {
118         /**
119          * Return device-wide statistics.
120          */
121         int   (*dt_statfs)(const struct lu_env *env,
122                            struct dt_device *dev, struct obd_statfs *osfs);
123         /**
124          * Create transaction, described by \a param.
125          */
126         struct thandle *(*dt_trans_create)(const struct lu_env *env,
127                                            struct dt_device *dev);
128         /**
129          * Start transaction, described by \a param.
130          */
131         int   (*dt_trans_start)(const struct lu_env *env,
132                                 struct dt_device *dev, struct thandle *th);
133         /**
134          * Finish previously started transaction.
135          */
136         int   (*dt_trans_stop)(const struct lu_env *env, struct dt_device *dev,
137                                struct thandle *th);
138         /**
139          * Add commit callback to the transaction.
140          */
141         int   (*dt_trans_cb_add)(struct thandle *th,
142                                  struct dt_txn_commit_cb *dcb);
143         /**
144          * Return fid of root index object.
145          */
146         int   (*dt_root_get)(const struct lu_env *env,
147                              struct dt_device *dev, struct lu_fid *f);
148         /**
149          * Return device configuration data.
150          */
151         void  (*dt_conf_get)(const struct lu_env *env,
152                              const struct dt_device *dev,
153                              struct dt_device_param *param);
154         /**
155          *  handling device state, mostly for tests
156          */
157         int   (*dt_sync)(const struct lu_env *env, struct dt_device *dev);
158         int   (*dt_ro)(const struct lu_env *env, struct dt_device *dev);
159         /**
160           * Start a transaction commit asynchronously
161           *
162           * \param env environment
163           * \param dev dt_device to start commit on
164           *
165           * \return 0 success, negative value if error
166           */
167          int   (*dt_commit_async)(const struct lu_env *env,
168                                   struct dt_device *dev);
169         /**
170          * Initialize capability context.
171          */
172         int   (*dt_init_capa_ctxt)(const struct lu_env *env,
173                                    struct dt_device *dev,
174                                    int mode, unsigned long timeout,
175                                    __u32 alg, struct lustre_capa_key *keys);
176 };
177
178 struct dt_index_features {
179         /** required feature flags from enum dt_index_flags */
180         __u32 dif_flags;
181         /** minimal required key size */
182         size_t dif_keysize_min;
183         /** maximal required key size, 0 if no limit */
184         size_t dif_keysize_max;
185         /** minimal required record size */
186         size_t dif_recsize_min;
187         /** maximal required record size, 0 if no limit */
188         size_t dif_recsize_max;
189         /** pointer size for record */
190         size_t dif_ptrsize;
191 };
192
193 enum dt_index_flags {
194         /** index supports variable sized keys */
195         DT_IND_VARKEY = 1 << 0,
196         /** index supports variable sized records */
197         DT_IND_VARREC = 1 << 1,
198         /** index can be modified */
199         DT_IND_UPDATE = 1 << 2,
200         /** index supports records with non-unique (duplicate) keys */
201         DT_IND_NONUNQ = 1 << 3,
202         /**
203          * index support fixed-size keys sorted with natural numerical way
204          * and is able to return left-side value if no exact value found
205          */
206         DT_IND_RANGE = 1 << 4,
207 };
208
209 /**
210  * Features, required from index to support file system directories (mapping
211  * names to fids).
212  */
213 extern const struct dt_index_features dt_directory_features;
214 extern const struct dt_index_features dt_otable_features;
215 extern const struct dt_index_features dt_lfsck_features;
216
217 /* index features supported by the accounting objects */
218 extern const struct dt_index_features dt_acct_features;
219
220 /* index features supported by the quota global indexes */
221 extern const struct dt_index_features dt_quota_glb_features;
222
223 /* index features supported by the quota slave indexes */
224 extern const struct dt_index_features dt_quota_slv_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         const void              *dah_eadata;
234         int                     dah_eadata_len;
235         __u32                   dah_mode;
236 };
237
238 /**
239  * object type specifier.
240  */
241
242 enum dt_format_type {
243         DFT_REGULAR,
244         DFT_DIR,
245         /** for mknod */
246         DFT_NODE,
247         /** for special index */
248         DFT_INDEX,
249         /** for symbolic link */
250         DFT_SYM,
251 };
252
253 /**
254  * object format specifier.
255  */
256 struct dt_object_format {
257         /** type for dt object */
258         enum dt_format_type dof_type;
259         union {
260                 struct dof_regular {
261                         int striped;
262                 } dof_reg;
263                 struct dof_dir {
264                 } dof_dir;
265                 struct dof_node {
266                 } dof_node;
267                 /**
268                  * special index need feature as parameter to create
269                  * special idx
270                  */
271                 struct dof_index {
272                         const struct dt_index_features *di_feat;
273                 } dof_idx;
274         } u;
275 };
276
277 enum dt_format_type dt_mode_to_dft(__u32 mode);
278
279 typedef __u64 dt_obj_version_t;
280
281 /**
282  * Per-dt-object operations.
283  */
284 struct dt_object_operations {
285         void  (*do_read_lock)(const struct lu_env *env,
286                               struct dt_object *dt, unsigned role);
287         void  (*do_write_lock)(const struct lu_env *env,
288                                struct dt_object *dt, unsigned role);
289         void  (*do_read_unlock)(const struct lu_env *env,
290                                 struct dt_object *dt);
291         void  (*do_write_unlock)(const struct lu_env *env,
292                                  struct dt_object *dt);
293         int  (*do_write_locked)(const struct lu_env *env,
294                                 struct dt_object *dt);
295         /**
296          * Note: following ->do_{x,}attr_{set,get}() operations are very
297          * similar to ->moo_{x,}attr_{set,get}() operations in struct
298          * md_object_operations (see md_object.h). These operations are not in
299          * lu_object_operations, because ->do_{x,}attr_set() versions take
300          * transaction handle as an argument (this transaction is started by
301          * caller). We might factor ->do_{x,}attr_get() into
302          * lu_object_operations, but that would break existing symmetry.
303          */
304
305         int   (*do_declare_attr_get)(const struct lu_env *env,
306                                      struct dt_object *dt,
307                                      struct lustre_capa *capa);
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         int   (*do_declare_xattr_get)(const struct lu_env *env,
332                                       struct dt_object *dt,
333                                       struct lu_buf *buf,
334                                       const char *name,
335                                       struct lustre_capa *capa);
336
337         /**
338          * Return a value of an extended attribute.
339          *
340          * precondition: dt_object_exists(dt);
341          */
342         int   (*do_xattr_get)(const struct lu_env *env, struct dt_object *dt,
343                               struct lu_buf *buf, const char *name,
344                               struct lustre_capa *capa);
345         /**
346          * Set value of an extended attribute.
347          *
348          * \a fl - flags from enum lu_xattr_flags
349          *
350          * precondition: dt_object_exists(dt);
351          */
352         int   (*do_declare_xattr_set)(const struct lu_env *env,
353                                       struct dt_object *dt,
354                                       const struct lu_buf *buf,
355                                       const char *name, int fl,
356                                       struct thandle *handle);
357         int   (*do_xattr_set)(const struct lu_env *env,
358                               struct dt_object *dt, const struct lu_buf *buf,
359                               const char *name, int fl, struct thandle *handle,
360                               struct lustre_capa *capa);
361         /**
362          * Delete existing extended attribute.
363          *
364          * precondition: dt_object_exists(dt);
365          */
366         int   (*do_declare_xattr_del)(const struct lu_env *env,
367                                       struct dt_object *dt,
368                                       const char *name, struct thandle *handle);
369         int   (*do_xattr_del)(const struct lu_env *env,
370                               struct dt_object *dt,
371                               const char *name, struct thandle *handle,
372                               struct lustre_capa *capa);
373         /**
374          * Place list of existing extended attributes into \a buf (which has
375          * length len).
376          *
377          * precondition: dt_object_exists(dt);
378          */
379         int   (*do_xattr_list)(const struct lu_env *env,
380                                struct dt_object *dt, struct lu_buf *buf,
381                                struct lustre_capa *capa);
382         /**
383          * Init allocation hint using parent object and child mode.
384          * (1) The \a parent might be NULL if this is a partial creation for
385          *     remote object.
386          * (2) The type of child is in \a child_mode.
387          * (3) The result hint is stored in \a ah;
388          */
389         void  (*do_ah_init)(const struct lu_env *env,
390                             struct dt_allocation_hint *ah,
391                             struct dt_object *parent,
392                             struct dt_object *child,
393                             umode_t child_mode);
394         /**
395          * Create new object on this device.
396          *
397          * precondition: !dt_object_exists(dt);
398          * postcondition: ergo(result == 0, dt_object_exists(dt));
399          */
400         int   (*do_declare_create)(const struct lu_env *env,
401                                    struct dt_object *dt,
402                                    struct lu_attr *attr,
403                                    struct dt_allocation_hint *hint,
404                                    struct dt_object_format *dof,
405                                    struct thandle *th);
406         int   (*do_create)(const struct lu_env *env, struct dt_object *dt,
407                            struct lu_attr *attr,
408                            struct dt_allocation_hint *hint,
409                            struct dt_object_format *dof,
410                            struct thandle *th);
411
412         /**
413           Destroy object on this device
414          * precondition: !dt_object_exists(dt);
415          * postcondition: ergo(result == 0, dt_object_exists(dt));
416          */
417         int   (*do_declare_destroy)(const struct lu_env *env,
418                                     struct dt_object *dt,
419                                     struct thandle *th);
420         int   (*do_destroy)(const struct lu_env *env, struct dt_object *dt,
421                             struct thandle *th);
422
423         /**
424          * Announce that this object is going to be used as an index. This
425          * operation check that object supports indexing operations and
426          * installs appropriate dt_index_operations vector on success.
427          *
428          * Also probes for features. Operation is successful if all required
429          * features are supported.
430          */
431         int   (*do_index_try)(const struct lu_env *env,
432                               struct dt_object *dt,
433                               const struct dt_index_features *feat);
434         /**
435          * Add nlink of the object
436          * precondition: dt_object_exists(dt);
437          */
438         int   (*do_declare_ref_add)(const struct lu_env *env,
439                                     struct dt_object *dt, struct thandle *th);
440         int   (*do_ref_add)(const struct lu_env *env,
441                             struct dt_object *dt, struct thandle *th);
442         /**
443          * Del nlink of the object
444          * precondition: dt_object_exists(dt);
445          */
446         int   (*do_declare_ref_del)(const struct lu_env *env,
447                                     struct dt_object *dt, struct thandle *th);
448         int   (*do_ref_del)(const struct lu_env *env,
449                             struct dt_object *dt, struct thandle *th);
450
451         struct obd_capa *(*do_capa_get)(const struct lu_env *env,
452                                         struct dt_object *dt,
453                                         struct lustre_capa *old,
454                                         __u64 opc);
455         int (*do_object_sync)(const struct lu_env *, struct dt_object *);
456         /**
457          * Get object info of next level. Currently, only get inode from osd.
458          * This is only used by quota b=16542
459          * precondition: dt_object_exists(dt);
460          */
461         int (*do_data_get)(const struct lu_env *env, struct dt_object *dt,
462                            void **data);
463
464         /**
465          * Lock object.
466          */
467         int (*do_object_lock)(const struct lu_env *env, struct dt_object *dt,
468                               struct lustre_handle *lh,
469                               struct ldlm_enqueue_info *einfo,
470                               void *policy);
471 };
472
473 /**
474  * Per-dt-object operations on "file body".
475  */
476 struct dt_body_operations {
477         /**
478          * precondition: dt_object_exists(dt);
479          */
480         ssize_t (*dbo_read)(const struct lu_env *env, struct dt_object *dt,
481                             struct lu_buf *buf, loff_t *pos,
482                             struct lustre_capa *capa);
483         /**
484          * precondition: dt_object_exists(dt);
485          */
486         ssize_t (*dbo_declare_write)(const struct lu_env *env,
487                                      struct dt_object *dt,
488                                      const loff_t size, loff_t pos,
489                                      struct thandle *handle);
490         ssize_t (*dbo_write)(const struct lu_env *env, struct dt_object *dt,
491                              const struct lu_buf *buf, loff_t *pos,
492                              struct thandle *handle, struct lustre_capa *capa,
493                              int ignore_quota);
494         /*
495          * methods for zero-copy IO
496          */
497
498         /*
499          * precondition: dt_object_exists(dt);
500          * returns:
501          * < 0 - error code
502          * = 0 - illegal
503          * > 0 - number of local buffers prepared
504          */
505         int (*dbo_bufs_get)(const struct lu_env *env, struct dt_object *dt,
506                             loff_t pos, ssize_t len, struct niobuf_local *lb,
507                             int rw, struct lustre_capa *capa);
508         /*
509          * precondition: dt_object_exists(dt);
510          */
511         int (*dbo_bufs_put)(const struct lu_env *env, struct dt_object *dt,
512                             struct niobuf_local *lb, int nr);
513         /*
514          * precondition: dt_object_exists(dt);
515          */
516         int (*dbo_write_prep)(const struct lu_env *env, struct dt_object *dt,
517                               struct niobuf_local *lb, int nr);
518         /*
519          * precondition: dt_object_exists(dt);
520          */
521         int (*dbo_declare_write_commit)(const struct lu_env *env,
522                                         struct dt_object *dt,
523                                         struct niobuf_local *,
524                                         int, struct thandle *);
525         /*
526          * precondition: dt_object_exists(dt);
527          */
528         int (*dbo_write_commit)(const struct lu_env *env, struct dt_object *dt,
529                                 struct niobuf_local *, int, struct thandle *);
530         /*
531          * precondition: dt_object_exists(dt);
532          */
533         int (*dbo_read_prep)(const struct lu_env *env, struct dt_object *dt,
534                              struct niobuf_local *lnb, int nr);
535         int (*dbo_fiemap_get)(const struct lu_env *env, struct dt_object *dt,
536                               struct ll_user_fiemap *fm);
537         /**
538          * Punch object's content
539          * precondition: regular object, not index
540          */
541         int   (*dbo_declare_punch)(const struct lu_env *, struct dt_object *,
542                                   __u64, __u64, struct thandle *th);
543         int   (*dbo_punch)(const struct lu_env *env, struct dt_object *dt,
544                           __u64 start, __u64 end, struct thandle *th,
545                           struct lustre_capa *capa);
546 };
547
548 /**
549  * Incomplete type of index record.
550  */
551 struct dt_rec;
552
553 /**
554  * Incomplete type of index key.
555  */
556 struct dt_key;
557
558 /**
559  * Incomplete type of dt iterator.
560  */
561 struct dt_it;
562
563 /**
564  * Per-dt-object operations on object as index.
565  */
566 struct dt_index_operations {
567         /**
568          * precondition: dt_object_exists(dt);
569          */
570         int (*dio_lookup)(const struct lu_env *env, struct dt_object *dt,
571                           struct dt_rec *rec, const struct dt_key *key,
572                           struct lustre_capa *capa);
573         /**
574          * precondition: dt_object_exists(dt);
575          */
576         int (*dio_declare_insert)(const struct lu_env *env,
577                                   struct dt_object *dt,
578                                   const struct dt_rec *rec,
579                                   const struct dt_key *key,
580                                   struct thandle *handle);
581         int (*dio_insert)(const struct lu_env *env, struct dt_object *dt,
582                           const struct dt_rec *rec, const struct dt_key *key,
583                           struct thandle *handle, struct lustre_capa *capa,
584                           int ignore_quota);
585         /**
586          * precondition: dt_object_exists(dt);
587          */
588         int (*dio_declare_delete)(const struct lu_env *env,
589                                   struct dt_object *dt,
590                                   const struct dt_key *key,
591                                   struct thandle *handle);
592         int (*dio_delete)(const struct lu_env *env, struct dt_object *dt,
593                           const struct dt_key *key, struct thandle *handle,
594                           struct lustre_capa *capa);
595         /**
596          * Iterator interface
597          */
598         struct dt_it_ops {
599                 /**
600                  * Allocate and initialize new iterator.
601                  *
602                  * precondition: dt_object_exists(dt);
603                  */
604                 struct dt_it *(*init)(const struct lu_env *env,
605                                       struct dt_object *dt,
606                                       __u32 attr,
607                                       struct lustre_capa *capa);
608                 void          (*fini)(const struct lu_env *env,
609                                       struct dt_it *di);
610                 int            (*get)(const struct lu_env *env,
611                                       struct dt_it *di,
612                                       const struct dt_key *key);
613                 void           (*put)(const struct lu_env *env,
614                                       struct dt_it *di);
615                 int           (*next)(const struct lu_env *env,
616                                       struct dt_it *di);
617                 struct dt_key *(*key)(const struct lu_env *env,
618                                       const struct dt_it *di);
619                 int       (*key_size)(const struct lu_env *env,
620                                       const struct dt_it *di);
621                 int            (*rec)(const struct lu_env *env,
622                                       const struct dt_it *di,
623                                       struct dt_rec *rec,
624                                       __u32 attr);
625                 __u64        (*store)(const struct lu_env *env,
626                                       const struct dt_it *di);
627                 int           (*load)(const struct lu_env *env,
628                                       const struct dt_it *di, __u64 hash);
629                 int        (*key_rec)(const struct lu_env *env,
630                                       const struct dt_it *di, void* key_rec);
631         } dio_it;
632 };
633
634 enum dt_otable_it_valid {
635         DOIV_ERROR_HANDLE       = 0x0001,
636         DOIV_DRYRUN             = 0x0002,
637 };
638
639 enum dt_otable_it_flags {
640         /* Exit when fail. */
641         DOIF_FAILOUT    = 0x0001,
642
643         /* Reset iteration position to the device beginning. */
644         DOIF_RESET      = 0x0002,
645
646         /* There is up layer component uses the iteration. */
647         DOIF_OUTUSED    = 0x0004,
648
649         /* Check only without repairing. */
650         DOIF_DRYRUN     = 0x0008,
651 };
652
653 /* otable based iteration needs to use the common DT interation APIs.
654  * To initialize the iteration, it needs call dio_it::init() firstly.
655  * Here is how the otable based iteration should prepare arguments to
656  * call dt_it_ops::init().
657  *
658  * For otable based iteration, the 32-bits 'attr' for dt_it_ops::init()
659  * is composed of two parts:
660  * low 16-bits is for valid bits, high 16-bits is for flags bits. */
661 #define DT_OTABLE_IT_FLAGS_SHIFT        16
662 #define DT_OTABLE_IT_FLAGS_MASK         0xffff0000
663
664 struct dt_device {
665         struct lu_device                   dd_lu_dev;
666         const struct dt_device_operations *dd_ops;
667
668         /**
669          * List of dt_txn_callback (see below). This is not protected in any
670          * way, because callbacks are supposed to be added/deleted only during
671          * single-threaded start-up shut-down procedures.
672          */
673         cfs_list_t                         dd_txn_callbacks;
674         unsigned int                       dd_record_fid_accessed:1;
675 };
676
677 int  dt_device_init(struct dt_device *dev, struct lu_device_type *t);
678 void dt_device_fini(struct dt_device *dev);
679
680 static inline int lu_device_is_dt(const struct lu_device *d)
681 {
682         return ergo(d != NULL, d->ld_type->ldt_tags & LU_DEVICE_DT);
683 }
684
685 static inline struct dt_device * lu2dt_dev(struct lu_device *l)
686 {
687         LASSERT(lu_device_is_dt(l));
688         return container_of0(l, struct dt_device, dd_lu_dev);
689 }
690
691 struct dt_object {
692         struct lu_object                   do_lu;
693         const struct dt_object_operations *do_ops;
694         const struct dt_body_operations   *do_body_ops;
695         const struct dt_index_operations  *do_index_ops;
696 };
697
698 /*
699  * In-core representation of per-device local object OID storage
700  */
701 struct local_oid_storage {
702         /* all initialized llog systems on this node linked by this */
703         cfs_list_t        los_list;
704
705         /* how many handle's reference this los has */
706         atomic_t          los_refcount;
707         struct dt_device *los_dev;
708         struct dt_object *los_obj;
709
710         /* data used to generate new fids */
711         struct mutex      los_id_lock;
712         __u64             los_seq;
713         __u32             los_last_oid;
714 };
715
716 static inline struct lu_device *dt2lu_dev(struct dt_device *d)
717 {
718         return &d->dd_lu_dev;
719 }
720
721 static inline struct dt_object *lu2dt(struct lu_object *l)
722 {
723         LASSERT(l == NULL || IS_ERR(l) || lu_device_is_dt(l->lo_dev));
724         return container_of0(l, struct dt_object, do_lu);
725 }
726
727 int  dt_object_init(struct dt_object *obj,
728                     struct lu_object_header *h, struct lu_device *d);
729
730 void dt_object_fini(struct dt_object *obj);
731
732 static inline int dt_object_exists(const struct dt_object *dt)
733 {
734         return lu_object_exists(&dt->do_lu);
735 }
736
737 static inline int dt_object_remote(const struct dt_object *dt)
738 {
739         return lu_object_remote(&dt->do_lu);
740 }
741
742 static inline struct dt_object *lu2dt_obj(struct lu_object *o)
743 {
744         LASSERT(ergo(o != NULL, lu_device_is_dt(o->lo_dev)));
745         return container_of0(o, struct dt_object, do_lu);
746 }
747
748 struct thandle_update {
749         /* In DNE, one transaction can be disassembled into
750          * updates on several different MDTs, and these updates
751          * will be attached to tu_remote_update_list per target.
752          * Only single thread will access the list, no need lock
753          */
754         struct list_head        tu_remote_update_list;
755
756         /* sent after or before local transaction */
757         unsigned int            tu_sent_after_local_trans:1,
758                                 tu_only_remote_trans:1;
759 };
760
761 /**
762  * This is the general purpose transaction handle.
763  * 1. Transaction Life Cycle
764  *      This transaction handle is allocated upon starting a new transaction,
765  *      and deallocated after this transaction is committed.
766  * 2. Transaction Nesting
767  *      We do _NOT_ support nested transaction. So, every thread should only
768  *      have one active transaction, and a transaction only belongs to one
769  *      thread. Due to this, transaction handle need no reference count.
770  * 3. Transaction & dt_object locking
771  *      dt_object locks should be taken inside transaction.
772  * 4. Transaction & RPC
773  *      No RPC request should be issued inside transaction.
774  */
775 struct thandle {
776         /** the dt device on which the transactions are executed */
777         struct dt_device *th_dev;
778
779         atomic_t        th_refc;
780         /* the size of transaction */
781         int             th_alloc_size;
782
783         /** context for this transaction, tag is LCT_TX_HANDLE */
784         struct lu_context th_ctx;
785
786         /** additional tags (layers can add in declare) */
787         __u32             th_tags;
788
789         /** the last operation result in this transaction.
790          * this value is used in recovery */
791         __s32             th_result;
792
793         /** whether we need sync commit */
794         unsigned int            th_sync:1;
795
796         /* local transation, no need to inform other layers */
797         unsigned int            th_local:1;
798
799         struct thandle_update   *th_update;
800 };
801
802 static inline void thandle_get(struct thandle *thandle)
803 {
804         atomic_inc(&thandle->th_refc);
805 }
806
807 static inline void thandle_put(struct thandle *thandle)
808 {
809         if (atomic_dec_and_test(&thandle->th_refc)) {
810                 if (thandle->th_update != NULL)
811                         OBD_FREE_PTR(thandle->th_update);
812                 OBD_FREE(thandle, thandle->th_alloc_size);
813         }
814 }
815 /**
816  * Transaction call-backs.
817  *
818  * These are invoked by osd (or underlying transaction engine) when
819  * transaction changes state.
820  *
821  * Call-backs are used by upper layers to modify transaction parameters and to
822  * perform some actions on for each transaction state transition. Typical
823  * example is mdt registering call-back to write into last-received file
824  * before each transaction commit.
825  */
826 struct dt_txn_callback {
827         int (*dtc_txn_start)(const struct lu_env *env,
828                              struct thandle *txn, void *cookie);
829         int (*dtc_txn_stop)(const struct lu_env *env,
830                             struct thandle *txn, void *cookie);
831         void (*dtc_txn_commit)(struct thandle *txn, void *cookie);
832         void                *dtc_cookie;
833         __u32                dtc_tag;
834         cfs_list_t           dtc_linkage;
835 };
836
837 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb);
838 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb);
839
840 int dt_txn_hook_start(const struct lu_env *env,
841                       struct dt_device *dev, struct thandle *txn);
842 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *txn);
843 void dt_txn_hook_commit(struct thandle *txn);
844
845 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj);
846
847 /**
848  * Callback function used for parsing path.
849  * \see llo_store_resolve
850  */
851 typedef int (*dt_entry_func_t)(const struct lu_env *env,
852                             const char *name,
853                             void *pvt);
854
855 #define DT_MAX_PATH 1024
856
857 int dt_path_parser(const struct lu_env *env,
858                    char *local, dt_entry_func_t entry_func,
859                    void *data);
860
861 struct dt_object *
862 dt_store_resolve(const struct lu_env *env, struct dt_device *dt,
863                  const char *path, struct lu_fid *fid);
864
865 struct dt_object *dt_store_open(const struct lu_env *env,
866                                 struct dt_device *dt,
867                                 const char *dirname,
868                                 const char *filename,
869                                 struct lu_fid *fid);
870
871 struct dt_object *dt_find_or_create(const struct lu_env *env,
872                                     struct dt_device *dt,
873                                     const struct lu_fid *fid,
874                                     struct dt_object_format *dof,
875                                     struct lu_attr *attr);
876
877 struct dt_object *dt_locate_at(const struct lu_env *env,
878                                struct dt_device *dev,
879                                const struct lu_fid *fid,
880                                struct lu_device *top_dev,
881                                const struct lu_object_conf *conf);
882
883 static inline struct dt_object *
884 dt_locate(const struct lu_env *env, struct dt_device *dev,
885           const struct lu_fid *fid)
886 {
887         return dt_locate_at(env, dev, fid,
888                             dev->dd_lu_dev.ld_site->ls_top_dev, NULL);
889 }
890
891 int local_oid_storage_init(const struct lu_env *env, struct dt_device *dev,
892                            const struct lu_fid *first_fid,
893                            struct local_oid_storage **los);
894 void local_oid_storage_fini(const struct lu_env *env,
895                             struct local_oid_storage *los);
896 int local_object_fid_generate(const struct lu_env *env,
897                               struct local_oid_storage *los,
898                               struct lu_fid *fid);
899 int local_object_declare_create(const struct lu_env *env,
900                                 struct local_oid_storage *los,
901                                 struct dt_object *o,
902                                 struct lu_attr *attr,
903                                 struct dt_object_format *dof,
904                                 struct thandle *th);
905 int local_object_create(const struct lu_env *env,
906                         struct local_oid_storage *los,
907                         struct dt_object *o,
908                         struct lu_attr *attr, struct dt_object_format *dof,
909                         struct thandle *th);
910 struct dt_object *local_file_find_or_create(const struct lu_env *env,
911                                             struct local_oid_storage *los,
912                                             struct dt_object *parent,
913                                             const char *name, __u32 mode);
914 struct dt_object *local_file_find_or_create_with_fid(const struct lu_env *env,
915                                                      struct dt_device *dt,
916                                                      const struct lu_fid *fid,
917                                                      struct dt_object *parent,
918                                                      const char *name,
919                                                      __u32 mode);
920 struct dt_object *
921 local_index_find_or_create(const struct lu_env *env,
922                            struct local_oid_storage *los,
923                            struct dt_object *parent,
924                            const char *name, __u32 mode,
925                            const struct dt_index_features *ft);
926 struct dt_object *
927 local_index_find_or_create_with_fid(const struct lu_env *env,
928                                     struct dt_device *dt,
929                                     const struct lu_fid *fid,
930                                     struct dt_object *parent,
931                                     const char *name, __u32 mode,
932                                     const struct dt_index_features *ft);
933 int local_object_unlink(const struct lu_env *env, struct dt_device *dt,
934                         struct dt_object *parent, const char *name);
935
936 static inline int dt_object_lock(const struct lu_env *env,
937                                  struct dt_object *o, struct lustre_handle *lh,
938                                  struct ldlm_enqueue_info *einfo,
939                                  void *policy)
940 {
941         LASSERT(o);
942         LASSERT(o->do_ops);
943         LASSERT(o->do_ops->do_object_lock);
944         return o->do_ops->do_object_lock(env, o, lh, einfo, policy);
945 }
946
947 int dt_lookup_dir(const struct lu_env *env, struct dt_object *dir,
948                   const char *name, struct lu_fid *fid);
949
950 static inline int dt_object_sync(const struct lu_env *env,
951                                  struct dt_object *o)
952 {
953         LASSERT(o);
954         LASSERT(o->do_ops);
955         LASSERT(o->do_ops->do_object_sync);
956         return o->do_ops->do_object_sync(env, o);
957 }
958
959 int dt_declare_version_set(const struct lu_env *env, struct dt_object *o,
960                            struct thandle *th);
961 void dt_version_set(const struct lu_env *env, struct dt_object *o,
962                     dt_obj_version_t version, struct thandle *th);
963 dt_obj_version_t dt_version_get(const struct lu_env *env, struct dt_object *o);
964
965
966 int dt_read(const struct lu_env *env, struct dt_object *dt,
967             struct lu_buf *buf, loff_t *pos);
968 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
969                    struct lu_buf *buf, loff_t *pos);
970 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
971                     const struct lu_buf *buf, loff_t *pos, struct thandle *th);
972 typedef int (*dt_index_page_build_t)(const struct lu_env *env,
973                                      union lu_page *lp, int nob,
974                                      const struct dt_it_ops *iops,
975                                      struct dt_it *it, __u32 attr, void *arg);
976 int dt_index_walk(const struct lu_env *env, struct dt_object *obj,
977                   const struct lu_rdpg *rdpg, dt_index_page_build_t filler,
978                   void *arg);
979 int dt_index_read(const struct lu_env *env, struct dt_device *dev,
980                   struct idx_info *ii, const struct lu_rdpg *rdpg);
981
982 static inline struct thandle *dt_trans_create(const struct lu_env *env,
983                                               struct dt_device *d)
984 {
985         LASSERT(d->dd_ops->dt_trans_create);
986         return d->dd_ops->dt_trans_create(env, d);
987 }
988
989 static inline int dt_trans_start(const struct lu_env *env,
990                                  struct dt_device *d, struct thandle *th)
991 {
992         LASSERT(d->dd_ops->dt_trans_start);
993         return d->dd_ops->dt_trans_start(env, d, th);
994 }
995
996 /* for this transaction hooks shouldn't be called */
997 static inline int dt_trans_start_local(const struct lu_env *env,
998                                        struct dt_device *d, struct thandle *th)
999 {
1000         LASSERT(d->dd_ops->dt_trans_start);
1001         th->th_local = 1;
1002         return d->dd_ops->dt_trans_start(env, d, th);
1003 }
1004
1005 static inline int dt_trans_stop(const struct lu_env *env,
1006                                 struct dt_device *d, struct thandle *th)
1007 {
1008         LASSERT(d->dd_ops->dt_trans_stop);
1009         return d->dd_ops->dt_trans_stop(env, d, th);
1010 }
1011
1012 static inline int dt_trans_cb_add(struct thandle *th,
1013                                   struct dt_txn_commit_cb *dcb)
1014 {
1015         LASSERT(th->th_dev->dd_ops->dt_trans_cb_add);
1016         dcb->dcb_magic = TRANS_COMMIT_CB_MAGIC;
1017         return th->th_dev->dd_ops->dt_trans_cb_add(th, dcb);
1018 }
1019 /** @} dt */
1020
1021
1022 static inline int dt_declare_record_write(const struct lu_env *env,
1023                                           struct dt_object *dt,
1024                                           int size, loff_t pos,
1025                                           struct thandle *th)
1026 {
1027         int rc;
1028
1029         LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
1030         LASSERT(th != NULL);
1031         LASSERT(dt->do_body_ops);
1032         LASSERT(dt->do_body_ops->dbo_declare_write);
1033         rc = dt->do_body_ops->dbo_declare_write(env, dt, size, pos, th);
1034         return rc;
1035 }
1036
1037 static inline int dt_declare_create(const struct lu_env *env,
1038                                     struct dt_object *dt,
1039                                     struct lu_attr *attr,
1040                                     struct dt_allocation_hint *hint,
1041                                     struct dt_object_format *dof,
1042                                     struct thandle *th)
1043 {
1044         LASSERT(dt);
1045         LASSERT(dt->do_ops);
1046         LASSERT(dt->do_ops->do_declare_create);
1047         return dt->do_ops->do_declare_create(env, dt, attr, hint, dof, th);
1048 }
1049
1050 static inline int dt_create(const struct lu_env *env,
1051                                     struct dt_object *dt,
1052                                     struct lu_attr *attr,
1053                                     struct dt_allocation_hint *hint,
1054                                     struct dt_object_format *dof,
1055                                     struct thandle *th)
1056 {
1057         LASSERT(dt);
1058         LASSERT(dt->do_ops);
1059         LASSERT(dt->do_ops->do_create);
1060         return dt->do_ops->do_create(env, dt, attr, hint, dof, th);
1061 }
1062
1063 static inline int dt_declare_destroy(const struct lu_env *env,
1064                                      struct dt_object *dt,
1065                                      struct thandle *th)
1066 {
1067         LASSERT(dt);
1068         LASSERT(dt->do_ops);
1069         LASSERT(dt->do_ops->do_declare_destroy);
1070         return dt->do_ops->do_declare_destroy(env, dt, th);
1071 }
1072
1073 static inline int dt_destroy(const struct lu_env *env,
1074                              struct dt_object *dt,
1075                              struct thandle *th)
1076 {
1077         LASSERT(dt);
1078         LASSERT(dt->do_ops);
1079         LASSERT(dt->do_ops->do_destroy);
1080         return dt->do_ops->do_destroy(env, dt, th);
1081 }
1082
1083 static inline void dt_read_lock(const struct lu_env *env,
1084                                 struct dt_object *dt,
1085                                 unsigned role)
1086 {
1087         LASSERT(dt);
1088         LASSERT(dt->do_ops);
1089         LASSERT(dt->do_ops->do_read_lock);
1090         dt->do_ops->do_read_lock(env, dt, role);
1091 }
1092
1093 static inline void dt_write_lock(const struct lu_env *env,
1094                                 struct dt_object *dt,
1095                                 unsigned role)
1096 {
1097         LASSERT(dt);
1098         LASSERT(dt->do_ops);
1099         LASSERT(dt->do_ops->do_write_lock);
1100         dt->do_ops->do_write_lock(env, dt, role);
1101 }
1102
1103 static inline void dt_read_unlock(const struct lu_env *env,
1104                                 struct dt_object *dt)
1105 {
1106         LASSERT(dt);
1107         LASSERT(dt->do_ops);
1108         LASSERT(dt->do_ops->do_read_unlock);
1109         dt->do_ops->do_read_unlock(env, dt);
1110 }
1111
1112 static inline void dt_write_unlock(const struct lu_env *env,
1113                                 struct dt_object *dt)
1114 {
1115         LASSERT(dt);
1116         LASSERT(dt->do_ops);
1117         LASSERT(dt->do_ops->do_write_unlock);
1118         dt->do_ops->do_write_unlock(env, dt);
1119 }
1120
1121 static inline int dt_write_locked(const struct lu_env *env,
1122                                   struct dt_object *dt)
1123 {
1124         LASSERT(dt);
1125         LASSERT(dt->do_ops);
1126         LASSERT(dt->do_ops->do_write_locked);
1127         return dt->do_ops->do_write_locked(env, dt);
1128 }
1129
1130 static inline int dt_declare_attr_get(const struct lu_env *env,
1131                                       struct dt_object *dt,
1132                                       struct lustre_capa *capa)
1133 {
1134         LASSERT(dt);
1135         LASSERT(dt->do_ops);
1136         LASSERT(dt->do_ops->do_declare_attr_get);
1137         return dt->do_ops->do_declare_attr_get(env, dt, capa);
1138 }
1139
1140 static inline int dt_attr_get(const struct lu_env *env, struct dt_object *dt,
1141                               struct lu_attr *la, void *arg)
1142 {
1143         LASSERT(dt);
1144         LASSERT(dt->do_ops);
1145         LASSERT(dt->do_ops->do_attr_get);
1146         return dt->do_ops->do_attr_get(env, dt, la, arg);
1147 }
1148
1149 static inline int dt_declare_attr_set(const struct lu_env *env,
1150                                       struct dt_object *dt,
1151                                       const struct lu_attr *la,
1152                                       struct thandle *th)
1153 {
1154         LASSERT(dt);
1155         LASSERT(dt->do_ops);
1156         LASSERT(dt->do_ops->do_declare_attr_set);
1157         return dt->do_ops->do_declare_attr_set(env, dt, la, th);
1158 }
1159
1160 static inline int dt_attr_set(const struct lu_env *env, struct dt_object *dt,
1161                               const struct lu_attr *la, struct thandle *th,
1162                               struct lustre_capa *capa)
1163 {
1164         LASSERT(dt);
1165         LASSERT(dt->do_ops);
1166         LASSERT(dt->do_ops->do_attr_set);
1167         return dt->do_ops->do_attr_set(env, dt, la, th, capa);
1168 }
1169
1170 static inline int dt_declare_ref_add(const struct lu_env *env,
1171                                      struct dt_object *dt, struct thandle *th)
1172 {
1173         LASSERT(dt);
1174         LASSERT(dt->do_ops);
1175         LASSERT(dt->do_ops->do_declare_ref_add);
1176         return dt->do_ops->do_declare_ref_add(env, dt, th);
1177 }
1178
1179 static inline int dt_ref_add(const struct lu_env *env,
1180                              struct dt_object *dt, struct thandle *th)
1181 {
1182         LASSERT(dt);
1183         LASSERT(dt->do_ops);
1184         LASSERT(dt->do_ops->do_ref_add);
1185         return dt->do_ops->do_ref_add(env, dt, th);
1186 }
1187
1188 static inline int dt_declare_ref_del(const struct lu_env *env,
1189                                      struct dt_object *dt, struct thandle *th)
1190 {
1191         LASSERT(dt);
1192         LASSERT(dt->do_ops);
1193         LASSERT(dt->do_ops->do_declare_ref_del);
1194         return dt->do_ops->do_declare_ref_del(env, dt, th);
1195 }
1196
1197 static inline int dt_ref_del(const struct lu_env *env,
1198                              struct dt_object *dt, struct thandle *th)
1199 {
1200         LASSERT(dt);
1201         LASSERT(dt->do_ops);
1202         LASSERT(dt->do_ops->do_ref_del);
1203         return dt->do_ops->do_ref_del(env, dt, th);
1204 }
1205
1206 static inline struct obd_capa *dt_capa_get(const struct lu_env *env,
1207                                            struct dt_object *dt,
1208                                            struct lustre_capa *old, __u64 opc)
1209 {
1210         LASSERT(dt);
1211         LASSERT(dt->do_ops);
1212         LASSERT(dt->do_ops->do_capa_get);
1213         return dt->do_ops->do_capa_get(env, dt, old, opc);
1214 }
1215
1216 static inline int dt_bufs_get(const struct lu_env *env, struct dt_object *d,
1217                               struct niobuf_remote *rnb,
1218                               struct niobuf_local *lnb, int rw,
1219                               struct lustre_capa *capa)
1220 {
1221         LASSERT(d);
1222         LASSERT(d->do_body_ops);
1223         LASSERT(d->do_body_ops->dbo_bufs_get);
1224         return d->do_body_ops->dbo_bufs_get(env, d, rnb->offset,
1225                                             rnb->len, lnb, rw, capa);
1226 }
1227
1228 static inline int dt_bufs_put(const struct lu_env *env, struct dt_object *d,
1229                               struct niobuf_local *lnb, int n)
1230 {
1231         LASSERT(d);
1232         LASSERT(d->do_body_ops);
1233         LASSERT(d->do_body_ops->dbo_bufs_put);
1234         return d->do_body_ops->dbo_bufs_put(env, d, lnb, n);
1235 }
1236
1237 static inline int dt_write_prep(const struct lu_env *env, struct dt_object *d,
1238                                 struct niobuf_local *lnb, int n)
1239 {
1240         LASSERT(d);
1241         LASSERT(d->do_body_ops);
1242         LASSERT(d->do_body_ops->dbo_write_prep);
1243         return d->do_body_ops->dbo_write_prep(env, d, lnb, n);
1244 }
1245
1246 static inline int dt_declare_write_commit(const struct lu_env *env,
1247                                           struct dt_object *d,
1248                                           struct niobuf_local *lnb,
1249                                           int n, struct thandle *th)
1250 {
1251         LASSERTF(d != NULL, "dt is NULL when we want to declare write\n");
1252         LASSERT(th != NULL);
1253         return d->do_body_ops->dbo_declare_write_commit(env, d, lnb, n, th);
1254 }
1255
1256
1257 static inline int dt_write_commit(const struct lu_env *env,
1258                                   struct dt_object *d, struct niobuf_local *lnb,
1259                                   int n, struct thandle *th)
1260 {
1261         LASSERT(d);
1262         LASSERT(d->do_body_ops);
1263         LASSERT(d->do_body_ops->dbo_write_commit);
1264         return d->do_body_ops->dbo_write_commit(env, d, lnb, n, th);
1265 }
1266
1267 static inline int dt_read_prep(const struct lu_env *env, struct dt_object *d,
1268                                struct niobuf_local *lnb, int n)
1269 {
1270         LASSERT(d);
1271         LASSERT(d->do_body_ops);
1272         LASSERT(d->do_body_ops->dbo_read_prep);
1273         return d->do_body_ops->dbo_read_prep(env, d, lnb, n);
1274 }
1275
1276 static inline int dt_declare_punch(const struct lu_env *env,
1277                                    struct dt_object *dt, __u64 start,
1278                                    __u64 end, struct thandle *th)
1279 {
1280         LASSERT(dt);
1281         LASSERT(dt->do_body_ops);
1282         LASSERT(dt->do_body_ops->dbo_declare_punch);
1283         return dt->do_body_ops->dbo_declare_punch(env, dt, start, end, th);
1284 }
1285
1286 static inline int dt_punch(const struct lu_env *env, struct dt_object *dt,
1287                            __u64 start, __u64 end, struct thandle *th,
1288                            struct lustre_capa *capa)
1289 {
1290         LASSERT(dt);
1291         LASSERT(dt->do_body_ops);
1292         LASSERT(dt->do_body_ops->dbo_punch);
1293         return dt->do_body_ops->dbo_punch(env, dt, start, end, th, capa);
1294 }
1295
1296 static inline int dt_fiemap_get(const struct lu_env *env, struct dt_object *d,
1297                                 struct ll_user_fiemap *fm)
1298 {
1299         LASSERT(d);
1300         if (d->do_body_ops == NULL)
1301                 return -EPROTO;
1302         if (d->do_body_ops->dbo_fiemap_get == NULL)
1303                 return -EOPNOTSUPP;
1304         return d->do_body_ops->dbo_fiemap_get(env, d, fm);
1305 }
1306
1307 static inline int dt_statfs(const struct lu_env *env, struct dt_device *dev,
1308                             struct obd_statfs *osfs)
1309 {
1310         LASSERT(dev);
1311         LASSERT(dev->dd_ops);
1312         LASSERT(dev->dd_ops->dt_statfs);
1313         return dev->dd_ops->dt_statfs(env, dev, osfs);
1314 }
1315
1316 static inline int dt_root_get(const struct lu_env *env, struct dt_device *dev,
1317                               struct lu_fid *f)
1318 {
1319         LASSERT(dev);
1320         LASSERT(dev->dd_ops);
1321         LASSERT(dev->dd_ops->dt_root_get);
1322         return dev->dd_ops->dt_root_get(env, dev, f);
1323 }
1324
1325 static inline void dt_conf_get(const struct lu_env *env,
1326                                const struct dt_device *dev,
1327                                struct dt_device_param *param)
1328 {
1329         LASSERT(dev);
1330         LASSERT(dev->dd_ops);
1331         LASSERT(dev->dd_ops->dt_conf_get);
1332         return dev->dd_ops->dt_conf_get(env, dev, param);
1333 }
1334
1335 static inline int dt_sync(const struct lu_env *env, struct dt_device *dev)
1336 {
1337         LASSERT(dev);
1338         LASSERT(dev->dd_ops);
1339         LASSERT(dev->dd_ops->dt_sync);
1340         return dev->dd_ops->dt_sync(env, dev);
1341 }
1342
1343 static inline int dt_ro(const struct lu_env *env, struct dt_device *dev)
1344 {
1345         LASSERT(dev);
1346         LASSERT(dev->dd_ops);
1347         LASSERT(dev->dd_ops->dt_ro);
1348         return dev->dd_ops->dt_ro(env, dev);
1349 }
1350
1351 static inline int dt_declare_insert(const struct lu_env *env,
1352                                     struct dt_object *dt,
1353                                     const struct dt_rec *rec,
1354                                     const struct dt_key *key,
1355                                     struct thandle *th)
1356 {
1357         LASSERT(dt);
1358         LASSERT(dt->do_index_ops);
1359         LASSERT(dt->do_index_ops->dio_declare_insert);
1360         return dt->do_index_ops->dio_declare_insert(env, dt, rec, key, th);
1361 }
1362
1363 static inline int dt_insert(const struct lu_env *env,
1364                                     struct dt_object *dt,
1365                                     const struct dt_rec *rec,
1366                                     const struct dt_key *key,
1367                                     struct thandle *th,
1368                                     struct lustre_capa *capa,
1369                                     int noquota)
1370 {
1371         LASSERT(dt);
1372         LASSERT(dt->do_index_ops);
1373         LASSERT(dt->do_index_ops->dio_insert);
1374         return dt->do_index_ops->dio_insert(env, dt, rec, key, th,
1375                                             capa, noquota);
1376 }
1377
1378 static inline int dt_declare_xattr_del(const struct lu_env *env,
1379                                        struct dt_object *dt,
1380                                        const char *name,
1381                                        struct thandle *th)
1382 {
1383         LASSERT(dt);
1384         LASSERT(dt->do_ops);
1385         LASSERT(dt->do_ops->do_declare_xattr_del);
1386         return dt->do_ops->do_declare_xattr_del(env, dt, name, th);
1387 }
1388
1389 static inline int dt_xattr_del(const struct lu_env *env,
1390                                struct dt_object *dt, const char *name,
1391                                struct thandle *th,
1392                                struct lustre_capa *capa)
1393 {
1394         LASSERT(dt);
1395         LASSERT(dt->do_ops);
1396         LASSERT(dt->do_ops->do_xattr_del);
1397         return dt->do_ops->do_xattr_del(env, dt, name, th, capa);
1398 }
1399
1400 static inline int dt_declare_xattr_set(const struct lu_env *env,
1401                                       struct dt_object *dt,
1402                                       const struct lu_buf *buf,
1403                                       const char *name, int fl,
1404                                       struct thandle *th)
1405 {
1406         LASSERT(dt);
1407         LASSERT(dt->do_ops);
1408         LASSERT(dt->do_ops->do_declare_xattr_set);
1409         return dt->do_ops->do_declare_xattr_set(env, dt, buf, name, fl, th);
1410 }
1411
1412 static inline int dt_xattr_set(const struct lu_env *env,
1413                               struct dt_object *dt, const struct lu_buf *buf,
1414                               const char *name, int fl, struct thandle *th,
1415                               struct lustre_capa *capa)
1416 {
1417         LASSERT(dt);
1418         LASSERT(dt->do_ops);
1419         LASSERT(dt->do_ops->do_xattr_set);
1420         return dt->do_ops->do_xattr_set(env, dt, buf, name, fl, th, capa);
1421 }
1422
1423 static inline int dt_declare_xattr_get(const struct lu_env *env,
1424                                        struct dt_object *dt,
1425                                        struct lu_buf *buf,
1426                                        const char *name,
1427                                        struct lustre_capa *capa)
1428 {
1429         LASSERT(dt);
1430         LASSERT(dt->do_ops);
1431         LASSERT(dt->do_ops->do_declare_xattr_get);
1432         return dt->do_ops->do_declare_xattr_get(env, dt, buf, name, capa);
1433 }
1434
1435 static inline int dt_xattr_get(const struct lu_env *env,
1436                               struct dt_object *dt, struct lu_buf *buf,
1437                               const char *name, struct lustre_capa *capa)
1438 {
1439         LASSERT(dt);
1440         LASSERT(dt->do_ops);
1441         LASSERT(dt->do_ops->do_xattr_get);
1442         return dt->do_ops->do_xattr_get(env, dt, buf, name, capa);
1443 }
1444
1445 static inline int dt_xattr_list(const struct lu_env *env,
1446                                struct dt_object *dt, struct lu_buf *buf,
1447                                struct lustre_capa *capa)
1448 {
1449         LASSERT(dt);
1450         LASSERT(dt->do_ops);
1451         LASSERT(dt->do_ops->do_xattr_list);
1452         return dt->do_ops->do_xattr_list(env, dt, buf, capa);
1453 }
1454
1455 static inline int dt_declare_delete(const struct lu_env *env,
1456                                     struct dt_object *dt,
1457                                     const struct dt_key *key,
1458                                     struct thandle *th)
1459 {
1460         LASSERT(dt);
1461         LASSERT(dt->do_index_ops);
1462         LASSERT(dt->do_index_ops->dio_declare_delete);
1463         return dt->do_index_ops->dio_declare_delete(env, dt, key, th);
1464 }
1465
1466 static inline int dt_delete(const struct lu_env *env,
1467                             struct dt_object *dt,
1468                             const struct dt_key *key,
1469                             struct thandle *th,
1470                             struct lustre_capa *capa)
1471 {
1472         LASSERT(dt);
1473         LASSERT(dt->do_index_ops);
1474         LASSERT(dt->do_index_ops->dio_delete);
1475         return dt->do_index_ops->dio_delete(env, dt, key, th, capa);
1476 }
1477
1478 static inline int dt_commit_async(const struct lu_env *env,
1479                                   struct dt_device *dev)
1480 {
1481         LASSERT(dev);
1482         LASSERT(dev->dd_ops);
1483         LASSERT(dev->dd_ops->dt_commit_async);
1484         return dev->dd_ops->dt_commit_async(env, dev);
1485 }
1486
1487 static inline int dt_init_capa_ctxt(const struct lu_env *env,
1488                                     struct dt_device *dev,
1489                                     int mode, unsigned long timeout,
1490                                     __u32 alg, struct lustre_capa_key *keys)
1491 {
1492         LASSERT(dev);
1493         LASSERT(dev->dd_ops);
1494         LASSERT(dev->dd_ops->dt_init_capa_ctxt);
1495         return dev->dd_ops->dt_init_capa_ctxt(env, dev, mode,
1496                                               timeout, alg, keys);
1497 }
1498
1499 static inline int dt_lookup(const struct lu_env *env,
1500                             struct dt_object *dt,
1501                             struct dt_rec *rec,
1502                             const struct dt_key *key,
1503                             struct lustre_capa *capa)
1504 {
1505         int ret;
1506
1507         LASSERT(dt);
1508         LASSERT(dt->do_index_ops);
1509         LASSERT(dt->do_index_ops->dio_lookup);
1510
1511         ret = dt->do_index_ops->dio_lookup(env, dt, rec, key, capa);
1512         if (ret > 0)
1513                 ret = 0;
1514         else if (ret == 0)
1515                 ret = -ENOENT;
1516         return ret;
1517 }
1518
1519 #define LU221_BAD_TIME (0x80000000U + 24 * 3600)
1520
1521 struct dt_find_hint {
1522         struct lu_fid        *dfh_fid;
1523         struct dt_device     *dfh_dt;
1524         struct dt_object     *dfh_o;
1525 };
1526
1527 struct dt_thread_info {
1528         char                     dti_buf[DT_MAX_PATH];
1529         struct dt_find_hint      dti_dfh;
1530         struct lu_attr           dti_attr;
1531         struct lu_fid            dti_fid;
1532         struct dt_object_format  dti_dof;
1533         struct lustre_mdt_attrs  dti_lma;
1534         struct lu_buf            dti_lb;
1535         struct lu_object_conf    dti_conf;
1536         loff_t                   dti_off;
1537 };
1538
1539 extern struct lu_context_key dt_key;
1540
1541 static inline struct dt_thread_info *dt_info(const struct lu_env *env)
1542 {
1543         struct dt_thread_info *dti;
1544
1545         dti = lu_context_key_get(&env->le_ctx, &dt_key);
1546         LASSERT(dti);
1547         return dti;
1548 }
1549
1550 int dt_global_init(void);
1551 void dt_global_fini(void);
1552
1553 # ifdef LPROCFS
1554 #ifndef HAVE_ONLY_PROCFS_SEQ
1555 int lprocfs_dt_rd_blksize(char *page, char **start, off_t off,
1556                           int count, int *eof, void *data);
1557 int lprocfs_dt_rd_kbytestotal(char *page, char **start, off_t off,
1558                               int count, int *eof, void *data);
1559 int lprocfs_dt_rd_kbytesfree(char *page, char **start, off_t off,
1560                              int count, int *eof, void *data);
1561 int lprocfs_dt_rd_kbytesavail(char *page, char **start, off_t off,
1562                               int count, int *eof, void *data);
1563 int lprocfs_dt_rd_filestotal(char *page, char **start, off_t off,
1564                              int count, int *eof, void *data);
1565 int lprocfs_dt_rd_filesfree(char *page, char **start, off_t off,
1566                             int count, int *eof, void *data);
1567 #endif
1568 int lprocfs_dt_blksize_seq_show(struct seq_file *m, void *v);
1569 int lprocfs_dt_kbytestotal_seq_show(struct seq_file *m, void *v);
1570 int lprocfs_dt_kbytesfree_seq_show(struct seq_file *m, void *v);
1571 int lprocfs_dt_kbytesavail_seq_show(struct seq_file *m, void *v);
1572 int lprocfs_dt_filestotal_seq_show(struct seq_file *m, void *v);
1573 int lprocfs_dt_filesfree_seq_show(struct seq_file *m, void *v);
1574 # endif /* LPROCFS */
1575
1576 #endif /* __LUSTRE_DT_OBJECT_H */