Whamcloud - gitweb
Land b_head_quota onto HEAD (20081116_0105)
[fs/lustre-release.git] / lustre / include / dt_object.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 txn_param;
66 struct dt_device;
67 struct dt_object;
68 struct dt_index_features;
69 struct dt_quota_ctxt;
70
71 struct dt_device_param {
72         unsigned           ddp_max_name_len;
73         unsigned           ddp_max_nlink;
74         unsigned           ddp_block_shift;
75 };
76
77 /**
78  * Basic transaction credit op
79  */
80 enum dt_txn_op {
81         DTO_INDEX_INSERT,
82         DTO_INDEX_DELETE,
83         DTO_IDNEX_UPDATE,
84         DTO_OBJECT_CREATE,
85         DTO_OBJECT_DELETE,
86         DTO_ATTR_SET_BASE,
87         DTO_XATTR_SET,
88         DTO_LOG_REC, /**< XXX temporary: dt layer knows nothing about llog. */
89         DTO_WRITE_BASE,
90         DTO_WRITE_BLOCK,
91         DTO_ATTR_SET_CHOWN,
92
93         DTO_NR
94 };
95
96 /**
97  * Operations on dt device.
98  */
99 struct dt_device_operations {
100         /**
101          * Return device-wide statistics.
102          */
103         int   (*dt_statfs)(const struct lu_env *env,
104                            struct dt_device *dev, struct kstatfs *sfs);
105         /**
106          * Start transaction, described by \a param.
107          */
108         struct thandle *(*dt_trans_start)(const struct lu_env *env,
109                                           struct dt_device *dev,
110                                           struct txn_param *param);
111         /**
112          * Finish previously started transaction.
113          */
114         void  (*dt_trans_stop)(const struct lu_env *env,
115                                struct thandle *th);
116         /**
117          * Return fid of root index object.
118          */
119         int   (*dt_root_get)(const struct lu_env *env,
120                              struct dt_device *dev, struct lu_fid *f);
121         /**
122          * Return device configuration data.
123          */
124         void  (*dt_conf_get)(const struct lu_env *env,
125                              const struct dt_device *dev,
126                              struct dt_device_param *param);
127         /**
128          *  handling device state, mostly for tests
129          */
130         int   (*dt_sync)(const struct lu_env *env, struct dt_device *dev);
131         void  (*dt_ro)(const struct lu_env *env, struct dt_device *dev);
132         /**
133           * Start a transaction commit asynchronously
134           *
135           * \param env environment
136           * \param dev dt_device to start commit on
137           *
138           * \return 0 success, negative value if error
139           */
140          int   (*dt_commit_async)(const struct lu_env *env,
141                                   struct dt_device *dev);
142         /**
143          * Initialize capability context.
144          */
145         int   (*dt_init_capa_ctxt)(const struct lu_env *env,
146                                    struct dt_device *dev,
147                                    int mode, unsigned long timeout,
148                                    __u32 alg, struct lustre_capa_key *keys);
149         /**
150          * Initialize quota context.
151          */
152         void (*dt_init_quota_ctxt)(const struct lu_env *env,
153                                    struct dt_device *dev,
154                                    struct dt_quota_ctxt *ctxt, void *data);
155
156         /**
157          *  get transaction credits for given \a op.
158          */
159         int (*dt_credit_get)(const struct lu_env *env, struct dt_device *dev,
160                              enum dt_txn_op);
161 };
162
163 struct dt_index_features {
164         /** required feature flags from enum dt_index_flags */
165         __u32 dif_flags;
166         /** minimal required key size */
167         size_t dif_keysize_min;
168         /** maximal required key size, 0 if no limit */
169         size_t dif_keysize_max;
170         /** minimal required record size */
171         size_t dif_recsize_min;
172         /** maximal required record size, 0 if no limit */
173         size_t dif_recsize_max;
174 };
175
176 enum dt_index_flags {
177         /** index supports variable sized keys */
178         DT_IND_VARKEY = 1 << 0,
179         /** index supports variable sized records */
180         DT_IND_VARREC = 1 << 1,
181         /** index can be modified */
182         DT_IND_UPDATE = 1 << 2,
183         /** index supports records with non-unique (duplicate) keys */
184         DT_IND_NONUNQ = 1 << 3
185 };
186
187 /**
188  * Features, required from index to support file system directories (mapping
189  * names to fids).
190  */
191 extern const struct dt_index_features dt_directory_features;
192
193 /**
194  * This is a general purpose dt allocation hint.
195  * It now contains the parent object.
196  * It can contain any allocation hint in the future.
197  */
198 struct dt_allocation_hint {
199         struct dt_object *dah_parent;
200         __u32             dah_mode;
201 };
202
203 /**
204  * Per-dt-object operations.
205  */
206 struct dt_object_operations {
207         void  (*do_read_lock)(const struct lu_env *env,
208                               struct dt_object *dt, unsigned role);
209         void  (*do_write_lock)(const struct lu_env *env,
210                                struct dt_object *dt, unsigned role);
211         void  (*do_read_unlock)(const struct lu_env *env,
212                                 struct dt_object *dt);
213         void  (*do_write_unlock)(const struct lu_env *env,
214                                  struct dt_object *dt);
215         /**
216          * Note: following ->do_{x,}attr_{set,get}() operations are very
217          * similar to ->moo_{x,}attr_{set,get}() operations in struct
218          * md_object_operations (see md_object.h). These operations are not in
219          * lu_object_operations, because ->do_{x,}attr_set() versions take
220          * transaction handle as an argument (this transaction is started by
221          * caller). We might factor ->do_{x,}attr_get() into
222          * lu_object_operations, but that would break existing symmetry.
223          */
224
225         /**
226          * Return standard attributes.
227          *
228          * precondition: lu_object_exists(&dt->do_lu);
229          */
230         int   (*do_attr_get)(const struct lu_env *env,
231                              struct dt_object *dt, struct lu_attr *attr,
232                              struct lustre_capa *capa);
233         /**
234          * Set standard attributes.
235          *
236          * precondition: dt_object_exists(dt);
237          */
238         int   (*do_attr_set)(const struct lu_env *env,
239                              struct dt_object *dt,
240                              const struct lu_attr *attr,
241                              struct thandle *handle,
242                              struct lustre_capa *capa);
243         /**
244          * Return a value of an extended attribute.
245          *
246          * precondition: dt_object_exists(dt);
247          */
248         int   (*do_xattr_get)(const struct lu_env *env, struct dt_object *dt,
249                               struct lu_buf *buf, const char *name,
250                               struct lustre_capa *capa);
251         /**
252          * Set value of an extended attribute.
253          *
254          * \a fl - flags from enum lu_xattr_flags
255          *
256          * precondition: dt_object_exists(dt);
257          */
258         int   (*do_xattr_set)(const struct lu_env *env,
259                               struct dt_object *dt, const struct lu_buf *buf,
260                               const char *name, int fl, struct thandle *handle,
261                               struct lustre_capa *capa);
262         /**
263          * Delete existing extended attribute.
264          *
265          * precondition: dt_object_exists(dt);
266          */
267         int   (*do_xattr_del)(const struct lu_env *env,
268                               struct dt_object *dt,
269                               const char *name, struct thandle *handle,
270                               struct lustre_capa *capa);
271         /**
272          * Place list of existing extended attributes into \a buf (which has
273          * length len).
274          *
275          * precondition: dt_object_exists(dt);
276          */
277         int   (*do_xattr_list)(const struct lu_env *env,
278                                struct dt_object *dt, struct lu_buf *buf,
279                                struct lustre_capa *capa);
280         /**
281          * Init allocation hint using parent object and child mode.
282          * (1) The \a parent might be NULL if this is a partial creation for
283          *     remote object.
284          * (2) The type of child is in \a child_mode.
285          * (3) The result hint is stored in \a ah;
286          */
287         void  (*do_ah_init)(const struct lu_env *env,
288                             struct dt_allocation_hint *ah,
289                             struct dt_object *parent,
290                             umode_t child_mode);
291         /**
292          * Create new object on this device.
293          *
294          * precondition: !dt_object_exists(dt);
295          * postcondition: ergo(result == 0, dt_object_exists(dt));
296          */
297         int   (*do_create)(const struct lu_env *env, struct dt_object *dt,
298                            struct lu_attr *attr,
299                            struct dt_allocation_hint *hint,
300                            struct thandle *th);
301
302         /**
303          * Announce that this object is going to be used as an index. This
304          * operation check that object supports indexing operations and
305          * installs appropriate dt_index_operations vector on success.
306          *
307          * Also probes for features. Operation is successful if all required
308          * features are supported.
309          */
310         int   (*do_index_try)(const struct lu_env *env,
311                               struct dt_object *dt,
312                               const struct dt_index_features *feat);
313         /**
314          * Add nlink of the object
315          * precondition: dt_object_exists(dt);
316          */
317         void  (*do_ref_add)(const struct lu_env *env,
318                             struct dt_object *dt, struct thandle *th);
319         /**
320          * Del nlink of the object
321          * precondition: dt_object_exists(dt);
322          */
323         void  (*do_ref_del)(const struct lu_env *env,
324                             struct dt_object *dt, struct thandle *th);
325
326         struct obd_capa *(*do_capa_get)(const struct lu_env *env,
327                                         struct dt_object *dt,
328                                         struct lustre_capa *old,
329                                         __u64 opc);
330         int (*do_object_sync)(const struct lu_env *, struct dt_object *);
331 };
332
333 /**
334  * Per-dt-object operations on "file body".
335  */
336 struct dt_body_operations {
337         /**
338          * precondition: dt_object_exists(dt);
339          */
340         ssize_t (*dbo_read)(const struct lu_env *env, struct dt_object *dt,
341                             struct lu_buf *buf, loff_t *pos,
342                             struct lustre_capa *capa);
343         /**
344          * precondition: dt_object_exists(dt);
345          */
346         ssize_t (*dbo_write)(const struct lu_env *env, struct dt_object *dt,
347                              const struct lu_buf *buf, loff_t *pos,
348                              struct thandle *handle, struct lustre_capa *capa,
349                              int ignore_quota);
350 };
351
352 /**
353  * Incomplete type of index record.
354  */
355 struct dt_rec;
356
357 /**
358  * Incomplete type of index key.
359  */
360 struct dt_key;
361
362 /**
363  * Incomplete type of dt iterator.
364  */
365 struct dt_it;
366
367 /**
368  * Per-dt-object operations on object as index.
369  */
370 struct dt_index_operations {
371         /**
372          * precondition: dt_object_exists(dt);
373          */
374         int (*dio_lookup)(const struct lu_env *env, struct dt_object *dt,
375                           struct dt_rec *rec, const struct dt_key *key,
376                           struct lustre_capa *capa);
377         /**
378          * precondition: dt_object_exists(dt);
379          */
380         int (*dio_insert)(const struct lu_env *env, struct dt_object *dt,
381                           const struct dt_rec *rec, const struct dt_key *key,
382                           struct thandle *handle, struct lustre_capa *capa,
383                           int ignore_quota);
384         /**
385          * precondition: dt_object_exists(dt);
386          */
387         int (*dio_delete)(const struct lu_env *env, struct dt_object *dt,
388                           const struct dt_key *key, struct thandle *handle,
389                           struct lustre_capa *capa);
390         /**
391          * Iterator interface
392          */
393         struct dt_it_ops {
394                 /**
395                  * Allocate and initialize new iterator.
396                  *
397                  * precondition: dt_object_exists(dt);
398                  */
399                 struct dt_it *(*init)(const struct lu_env *env,
400                                       struct dt_object *dt, int writable,
401                                       struct lustre_capa *capa);
402                 void          (*fini)(const struct lu_env *env,
403                                       struct dt_it *di);
404                 int            (*get)(const struct lu_env *env,
405                                       struct dt_it *di,
406                                       const struct dt_key *key);
407                 void           (*put)(const struct lu_env *env,
408                                       struct dt_it *di);
409                 int            (*del)(const struct lu_env *env,
410                                       struct dt_it *di, struct thandle *th);
411                 int           (*next)(const struct lu_env *env,
412                                       struct dt_it *di);
413                 struct dt_key *(*key)(const struct lu_env *env,
414                                       const struct dt_it *di);
415                 int       (*key_size)(const struct lu_env *env,
416                                       const struct dt_it *di);
417                 struct dt_rec *(*rec)(const struct lu_env *env,
418                                       const struct dt_it *di);
419                 __u64        (*store)(const struct lu_env *env,
420                                       const struct dt_it *di);
421                 int           (*load)(const struct lu_env *env,
422                                       const struct dt_it *di, __u64 hash);
423         } dio_it;
424 };
425
426 struct dt_device {
427         struct lu_device                   dd_lu_dev;
428         const struct dt_device_operations *dd_ops;
429
430         /**
431          * List of dt_txn_callback (see below). This is not protected in any
432          * way, because callbacks are supposed to be added/deleted only during
433          * single-threaded start-up shut-down procedures.
434          */
435         struct list_head                   dd_txn_callbacks;
436 };
437
438 int  dt_device_init(struct dt_device *dev, struct lu_device_type *t);
439 void dt_device_fini(struct dt_device *dev);
440
441 static inline int lu_device_is_dt(const struct lu_device *d)
442 {
443         return ergo(d != NULL, d->ld_type->ldt_tags & LU_DEVICE_DT);
444 }
445
446 static inline struct dt_device * lu2dt_dev(struct lu_device *l)
447 {
448         LASSERT(lu_device_is_dt(l));
449         return container_of0(l, struct dt_device, dd_lu_dev);
450 }
451
452 struct dt_object {
453         struct lu_object                   do_lu;
454         const struct dt_object_operations *do_ops;
455         const struct dt_body_operations   *do_body_ops;
456         const struct dt_index_operations  *do_index_ops;
457 };
458
459 int  dt_object_init(struct dt_object *obj,
460                     struct lu_object_header *h, struct lu_device *d);
461
462 void dt_object_fini(struct dt_object *obj);
463
464 static inline int dt_object_exists(const struct dt_object *dt)
465 {
466         return lu_object_exists(&dt->do_lu);
467 }
468
469 struct txn_param {
470         /** number of blocks this transaction will modify */
471         unsigned int tp_credits;
472         /** sync transaction is needed */
473         __u32        tp_sync:1;
474 };
475
476 static inline void txn_param_init(struct txn_param *p, unsigned int credits)
477 {
478         memset(p, 0, sizeof(*p));
479         p->tp_credits = credits;
480 }
481
482 /**
483  * This is the general purpose transaction handle.
484  * 1. Transaction Life Cycle
485  *      This transaction handle is allocated upon starting a new transaction,
486  *      and deallocated after this transaction is committed.
487  * 2. Transaction Nesting
488  *      We do _NOT_ support nested transaction. So, every thread should only
489  *      have one active transaction, and a transaction only belongs to one
490  *      thread. Due to this, transaction handle need no reference count.
491  * 3. Transaction & dt_object locking
492  *      dt_object locks should be taken inside transaction.
493  * 4. Transaction & RPC
494  *      No RPC request should be issued inside transaction.
495  */
496 struct thandle {
497         /** the dt device on which the transactions are executed */
498         struct dt_device *th_dev;
499
500         /** context for this transaction, tag is LCT_TX_HANDLE */
501         struct lu_context th_ctx;
502
503         /** the last operation result in this transaction.
504          * this value is used in recovery */
505         __s32             th_result;
506 };
507
508 /**
509  * Transaction call-backs.
510  *
511  * These are invoked by osd (or underlying transaction engine) when
512  * transaction changes state.
513  *
514  * Call-backs are used by upper layers to modify transaction parameters and to
515  * perform some actions on for each transaction state transition. Typical
516  * example is mdt registering call-back to write into last-received file
517  * before each transaction commit.
518  */
519 struct dt_txn_callback {
520         int (*dtc_txn_start)(const struct lu_env *env,
521                              struct txn_param *param, void *cookie);
522         int (*dtc_txn_stop)(const struct lu_env *env,
523                             struct thandle *txn, void *cookie);
524         int (*dtc_txn_commit)(const struct lu_env *env,
525                               struct thandle *txn, void *cookie);
526         void            *dtc_cookie;
527         struct list_head dtc_linkage;
528 };
529
530 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb);
531 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb);
532
533 int dt_txn_hook_start(const struct lu_env *env,
534                       struct dt_device *dev, struct txn_param *param);
535 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *txn);
536 int dt_txn_hook_commit(const struct lu_env *env, struct thandle *txn);
537
538 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj);
539 struct dt_object *dt_store_open(const struct lu_env *env,
540                                 struct dt_device *dt, const char *name,
541                                 struct lu_fid *fid);
542
543 /** @} dt */
544
545 #endif /* __LUSTRE_DT_OBJECT_H */