Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / lustre / lod / lod_internal.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,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright  2009 Sun Microsystems, Inc. All rights reserved
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/lod/lod_internal.h
32  *
33  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
34  * Author: Mikhail Pershin <mike.pershin@intel.com>
35  */
36
37 #ifndef _LOD_INTERNAL_H
38 #define _LOD_INTERNAL_H
39
40 #include <libcfs/libcfs.h>
41 #include <uapi/linux/lustre/lustre_cfg.h>
42 #include <obd.h>
43 #include <dt_object.h>
44
45 enum lod_uses_hint {
46         LOD_USES_ASSIGNED_STRIPE = 0,
47         LOD_USES_DEFAULT_STRIPE,
48 };
49
50 /* Special values to remove LOV EA from disk */
51 #define LOVEA_DELETE_VALUES(size, count, offset, pool)                  \
52         ((size) == 0 && (count) == 0 &&                                 \
53          (offset) == (typeof(offset))(-1) && (pool) == NULL)
54
55 #define LMVEA_DELETE_VALUES(count, offset)                              \
56         ((count) == 0 && (offset) == (typeof(offset))(-1))
57
58 struct lod_pool_desc {
59         char                     pool_name[LOV_MAXPOOLNAME + 1];
60         struct lu_tgt_pool       pool_obds;     /* pool members */
61         struct kref              pool_refcount;
62         struct lu_qos_rr         pool_rr;
63         struct rhash_head        pool_hash;     /* access by poolname */
64         struct list_head         pool_list;
65         struct rcu_head          pool_rcu;
66         struct proc_dir_entry   *pool_proc_entry;
67         struct obd_device       *pool_lobd;     /* owner */
68         time64_t                 pool_spill_expire;
69         struct proc_dir_entry   *pool_spill_proc_entry;
70         bool                     pool_spill_is_active;
71         unsigned int             pool_spill_threshold_pct;
72         atomic_t                 pool_spill_hit;
73         char                     pool_spill_target[LOV_MAXPOOLNAME + 1];
74         bool                     pool_same_space; /* targets in pool balanced*/
75         time64_t                 pool_same_space_expire; /*uses ld_qos_maxage*/
76         struct dentry           *pool_debugfs;
77 };
78
79 struct lod_device;
80 int lod_pool_hash_init(struct rhashtable *tbl);
81 void lod_pool_hash_destroy(struct rhashtable *tbl);
82 bool lod_pool_exists(struct lod_device *lod, char *poolname);
83
84 #define pool_tgt_count(p) ((p)->pool_obds.op_count)
85 #define pool_tgt_array(p)  ((p)->pool_obds.op_array)
86 #define pool_tgt_rw_sem(p) ((p)->pool_obds.op_rw_sem)
87
88 #define lod_tgt_desc    lu_tgt_desc
89 #define lod_tgt_descs   lu_tgt_descs
90
91 #define OST_TGT(lod, index)   LTD_TGT(&lod->lod_ost_descs, index)
92 #define MDT_TGT(lod, index)   LTD_TGT(&lod->lod_mdt_descs, index)
93
94 struct lod_avoid_guide {
95         /* ids of OSSs avoid guidance */
96         __u32                   *lag_oss_avoid_array;
97         /* number of filled array items */
98         unsigned int            lag_oaa_count;
99         /* number of allocated array items */
100         unsigned int            lag_oaa_size;
101         /* bitmap of OSTs avoid guidance */
102         unsigned long           *lag_ost_avoid_bitmap;
103         u32                     lag_ost_avoid_size;
104         /* how many OSTs are available for alloc */
105         __u32                   lag_ost_avail;
106 };
107
108 #define LOD_DOM_MIN_SIZE_KB (LOV_MIN_STRIPE_SIZE >> 10)
109 #define LOD_DOM_SFS_MAX_AGE 10
110
111 struct lod_device {
112         struct dt_device      lod_dt_dev;
113         struct obd_export    *lod_child_exp;
114         struct dt_device     *lod_child;
115         struct lprocfs_stats *lod_stats;
116         spinlock_t            lod_connects_lock;
117         int                   lod_connects;
118         unsigned int          lod_recovery_completed:1,
119                               lod_initialized:1,
120                               lod_lmv_failout:1,
121                               lod_child_got_update_log:1,
122                               lod_dist_txn_check_space:1;
123
124         /* protect ld_active_tgt_count, ltd_active and lod_md_root */
125         spinlock_t           lod_lock;
126
127         /* Description of OST */
128         struct lod_tgt_descs  lod_ost_descs;
129         /* Description of MDT */
130         struct lod_tgt_descs  lod_mdt_descs;
131
132         /* Recovery thread for lod_child */
133         struct task_struct   *lod_child_recovery_task;
134
135         /* maximum EA size underlied OSD may have */
136         unsigned int          lod_osd_max_easize;
137         /* maximum size of MDT stripe for Data-on-MDT files. */
138         unsigned int          lod_dom_stripesize_max_kb;
139         /* current DOM default stripe size adjusted by threshold */
140         unsigned int          lod_dom_stripesize_cur_kb;
141         /* Threshold at which DOM default stripe will start decreasing */
142         __u64                 lod_dom_threshold_free_mb;
143
144         /* Local OSD statfs cache */
145         spinlock_t            lod_lsfs_lock;
146         time64_t              lod_lsfs_age;
147         __u64                 lod_lsfs_total_mb;
148         __u64                 lod_lsfs_free_mb;
149
150         /* OST pool data */
151         int                     lod_pool_count;
152         struct rhashtable       lod_pools_hash_body; /* used for key access */
153         struct list_head        lod_pool_list; /* used for sequential access */
154         struct proc_dir_entry  *lod_pool_proc_entry;
155         struct proc_dir_entry  *lod_spill_proc_entry;
156
157         enum lustre_sec_part   lod_sp_me;
158
159         struct proc_dir_entry *lod_symlink;
160         struct dentry          *lod_debugfs;
161         struct dentry          *lod_pool_debugfs;
162
163         /* ROOT object, used to fetch FS default striping */
164         struct lod_object      *lod_md_root;
165
166         /* max stripe count if stripe count is set to -1. 0 means unlimited */
167         unsigned int            lod_max_stripecount;
168         unsigned int            lod_max_mdt_stripecount;
169 };
170
171 #define lod_ost_bitmap          lod_ost_descs.ltd_tgt_bitmap
172 #define lod_ost_count           lod_ost_descs.ltd_lov_desc.ld_tgt_count
173 #define lod_ost_active_count    lod_ost_descs.ltd_lov_desc.ld_active_tgt_count
174 #define lod_remote_mdt_count    lod_mdt_descs.ltd_lmv_desc.ld_tgt_count
175
176 struct lod_layout_component {
177         struct lu_extent          llc_extent;
178         __u32                     llc_id;
179         __u32                     llc_flags;
180         __u32                     llc_magic;
181         __u64                     llc_timestamp; /* snapshot time */
182         union {
183                 struct { /* plain layout V1/V3. */
184                         __u32                     llc_pattern;
185                         __u32                     llc_stripe_size;
186                         __u16                     llc_layout_gen;
187                         __u16                     llc_stripe_offset;
188                         __u16                     llc_stripe_count;
189                         __u16                     llc_stripes_allocated;
190                         char                     *llc_pool;
191                         /* ost list specified by LOV_USER_MAGIC_SPECIFIC lum */
192                         struct lu_tgt_pool        llc_ostlist;
193                         struct dt_object        **llc_stripe;
194                         __u32                    *llc_ost_indices;
195                 };
196                 struct { /* Foreign mirror layout component */
197                         __u32                     llc_length;
198                         __u32                     llc_type;
199                         __u32                     llc_foreign_flags;
200                         union {
201                                 /* Basic HSM layout information */
202                                 struct lov_hsm_base      llc_hsm;
203                                 /* Other kinds of foreign types (i.e. DAOS) */
204                                 char                    *llc_value;
205                         };
206                 };
207         };
208 };
209
210 struct lod_default_striping {
211         /* default LOV */
212         /* current layout component count */
213         __u16                           lds_def_comp_cnt;
214         __u16                           lds_def_mirror_cnt;
215         /* the largest comp count ever used */
216         __u32                           lds_def_comp_size_cnt;
217         struct lod_layout_component     *lds_def_comp_entries;
218         /* default LMV */
219         __u32                           lds_dir_def_stripe_count;
220         __u32                           lds_dir_def_stripe_offset;
221         __u32                           lds_dir_def_hash_type;
222         __u8                            lds_dir_def_max_inherit;
223         __u8                            lds_dir_def_max_inherit_rr;
224                                         /* default file striping flags (LOV) */
225         __u32                           lds_def_striping_set:1,
226                                         lds_def_striping_is_composite:1,
227                                         /* default dir striping flags (LMV) */
228                                         lds_dir_def_striping_set:1;
229 };
230
231 enum layout_verify_flags {
232         LVF_ALL_STALE           = BIT(0), /* check not all stale mirrors */
233 };
234
235 struct lod_mirror_entry {
236         __u16   lme_stale:1,
237                 lme_prefer:1,
238                 lme_hsm:1;
239         /* mirror id */
240         __u16   lme_id;
241         /* preference */
242         __u16   lme_preference;
243         /* start,end index of this mirror in ldo_comp_entries */
244         __u16   lme_start;
245         __u16   lme_end;
246 };
247
248 struct lod_object {
249         /* common fields for both files and directories */
250         struct dt_object                ldo_obj;
251         struct mutex                    ldo_layout_mutex;
252                                         /* foreign file/directory */
253         __u32                           ldo_is_foreign:1;
254         union {
255                 /* file stripe (LOV) */
256                 struct {
257                         __u32           ldo_layout_gen;
258                         /* Layout component count for a regular file.
259                          * It equals to 1 for non-composite layout. */
260                         __u16           ldo_comp_cnt;
261                         /* Layout mirror count for a PFLR file.
262                          * It's 0 for files with non-composite layout. */
263                         __u16           ldo_mirror_count;
264                         struct lod_mirror_entry *ldo_mirrors;
265                         __u32           ldo_is_composite:1,
266                                         ldo_flr_state:4,
267                                         ldo_comp_cached:1;
268                 };
269                 /* directory stripe (LMV) */
270                 struct {
271                         /* Slave stripe count for striped directory. */
272                         __u16           ldo_dir_stripe_count;
273                         /* How many stripes allocated for a striped directory */
274                         __u16           ldo_dir_stripes_allocated;
275                         __u32           ldo_dir_stripe_offset;
276                         __u32           ldo_dir_hash_type;
277                         __u32           ldo_dir_migrate_offset;
278                         __u32           ldo_dir_migrate_hash;
279                         __u32           ldo_dir_layout_version;
280                         /* Is a slave stripe of striped directory? */
281                         __u32           ldo_dir_slave_stripe:1,
282                                         ldo_dir_striped:1,
283                                         /* the stripe has been loaded */
284                                         ldo_dir_stripe_loaded:1;
285                         /*
286                          * This default LMV is parent default LMV, which will be
287                          * used in child creation, and it's not cached, so this
288                          * field is invalid after create, make sure it's used by
289                          * lod_dir_striping_create_internal() only.
290                          */
291                         struct lod_default_striping     *ldo_def_striping;
292                 };
293         };
294         union {
295                 struct {
296                         /* foreign/raw format LOV */
297                         char                            *ldo_foreign_lov;
298                         size_t                           ldo_foreign_lov_size;
299                 };
300                 struct {
301                         /* foreign/raw format LMV */
302                         char                            *ldo_foreign_lmv;
303                         size_t                           ldo_foreign_lmv_size;
304                 };
305                 struct {
306                         /* file stripe (LOV) */
307                         struct lod_layout_component     *ldo_comp_entries;
308                         /* slave stripes of striped directory (LMV) */
309                         struct dt_object                **ldo_stripe;
310                 };
311         };
312 };
313
314 #define ldo_dir_split_offset    ldo_dir_migrate_offset
315 #define ldo_dir_split_hash      ldo_dir_migrate_hash
316
317 #define lod_foreach_mirror_comp(comp, lo, mirror_idx)                      \
318 for (comp = &lo->ldo_comp_entries[lo->ldo_mirrors[mirror_idx].lme_start];  \
319      comp <= &lo->ldo_comp_entries[lo->ldo_mirrors[mirror_idx].lme_end];   \
320      comp++)
321
322 static inline bool lod_is_flr(const struct lod_object *lo)
323 {
324         if (!lo->ldo_is_composite)
325                 return false;
326
327         return (lo->ldo_flr_state & LCM_FL_FLR_MASK) != LCM_FL_NONE;
328 }
329
330 static inline bool lod_is_hsm(const struct lod_layout_component *lod_comp)
331 {
332         return lod_comp->llc_magic == LOV_MAGIC_FOREIGN &&
333                lov_hsm_type_supported(lod_comp->llc_type);
334 }
335
336 static inline bool lod_is_splitting(const struct lod_object *lo)
337 {
338         return lmv_hash_is_splitting(lo->ldo_dir_hash_type);
339 }
340
341 static inline bool lod_is_migrating(const struct lod_object *lo)
342 {
343         return lmv_hash_is_migrating(lo->ldo_dir_hash_type);
344 }
345
346 static inline bool lod_is_layout_changing(const struct lod_object *lo)
347 {
348         return lmv_hash_is_layout_changing(lo->ldo_dir_hash_type);
349 }
350
351 static inline int lod_set_pool(char **pool, const char *new_pool)
352 {
353         int len;
354
355         if (*pool == new_pool)
356                 return 0;
357
358         if (*pool != NULL) {
359                 len = strlen(*pool) + 1;
360                 OBD_FREE(*pool, len);
361                 *pool = NULL;
362         }
363         if (new_pool != NULL) {
364                 len = strlen(new_pool) + 1;
365                 OBD_ALLOC(*pool, len);
366                 if (*pool == NULL)
367                         return -ENOMEM;
368                 strscpy(*pool, new_pool, len);
369         }
370         return 0;
371 }
372
373 static inline int lod_obj_set_pool(struct lod_object *lo, int i,
374                                    const char *new_pool)
375 {
376         return lod_set_pool(&lo->ldo_comp_entries[i].llc_pool, new_pool);
377 }
378
379 /**
380  * Create new layout generation.
381  *
382  * The only requirement for layout generation is that it changes when
383  * the layout is modified, so a circular counter is sufficient for the
384  * low rate of layout modifications.
385  *
386  * Layout generation is also used to generate unique component ID.
387  * To detect generation overflow, we preserve the highest bit of the
388  * generation when it wrapped.
389  */
390 static inline void lod_obj_inc_layout_gen(struct lod_object *lo)
391 {
392         __u32 preserve = lo->ldo_layout_gen & ~LCME_ID_MASK;
393         lo->ldo_layout_gen++;
394         lo->ldo_layout_gen |= preserve;
395         /* Zero is not a valid generation */
396         if (unlikely((lo->ldo_layout_gen & LCME_ID_MASK) == 0)) {
397                 lo->ldo_layout_gen++;
398                 lo->ldo_layout_gen &= ~LU_LAYOUT_RESYNC;
399         }
400 }
401
402 struct lod_it {
403         struct dt_object        *lit_obj; /* object from the layer below */
404         /* stripe offset of iteration */
405         __u32                   lit_stripe_index;
406         __u32                   lit_attr;
407         struct dt_it            *lit_it;  /* iterator from the layer below */
408 };
409
410 #define LOD_OBJS_INTRANS 4
411 struct lod_thread_info {
412         /* per-thread buffer for LOV EA, may be vmalloc'd */
413         void                           *lti_ea_store;
414         __u32                           lti_ea_store_size;
415         /* per-thread buffer for LMV EA */
416         struct lu_buf                   lti_buf;
417         struct ost_id                   lti_ostid;
418         struct lu_fid                   lti_fid;
419         struct obd_statfs               lti_osfs;
420         struct lu_attr                  lti_attr;
421         struct lod_it                   lti_it;
422         struct ldlm_res_id              lti_res_id;
423         /* used to hold lu_dirent, sizeof(struct lu_dirent) + NAME_MAX */
424         char                            lti_key[sizeof(struct lu_dirent) +
425                                                 NAME_MAX];
426         struct dt_object_format         lti_format;
427         struct lu_name                  lti_name;
428         struct lu_buf                   lti_linkea_buf;
429         struct dt_insert_rec            lti_dt_rec;
430         struct llog_catid               lti_cid;
431         struct llog_cookie              lti_cookie;
432         struct lustre_cfg               lti_lustre_cfg;
433         /* used to store parent default striping in create */
434         struct lod_default_striping     lti_def_striping;
435         struct filter_fid               lti_ff;
436         __u32                           *lti_comp_idx;
437         size_t                          lti_comp_size;
438         size_t                          lti_count;
439         struct lu_attr                  lti_layout_attr;
440         /* object allocation avoid guide info */
441         struct lod_avoid_guide          lti_avoid;
442         union lmv_mds_md                lti_lmv;
443         struct dt_allocation_hint       lti_ah;
444         int lti_obj_count;
445         struct lod_object *lti_obj[LOD_OBJS_INTRANS];
446         __u32 lti_gen[LOD_OBJS_INTRANS];
447 };
448
449 /**
450  * \retval      0 object's layout hasn't changed in the transaction
451  * \retval      > 0 object's layout has changed
452  * \retval      -ENOENT object's layout gen hasn't saved in transatoin
453  *              declaration
454  */
455 static inline int
456 lod_check_layout_gen_intrans(struct lod_thread_info *info,
457                              struct lod_object *lo)
458 {
459         int i;
460         int rc = -ENOENT;
461
462         for (i = 0; i < info->lti_obj_count; i++) {
463                 if (info->lti_obj[i] != lo)
464                         continue;
465
466                 if (info->lti_gen[i] == lo->ldo_layout_gen)
467                         rc = 0;
468                 else
469                         rc = i + 1;
470                 break;
471         }
472
473         return rc;
474 }
475
476 static inline int
477 lod_save_layout_gen_intrans(struct lod_thread_info *info, struct lod_object *lo)
478 {
479         struct lu_object *luo = &lo->ldo_obj.do_lu;
480         int rc;
481
482         rc = lod_check_layout_gen_intrans(info, lo);
483         if (rc == 0)
484                 return 0;
485         if (rc > 0) {
486                 CDEBUG(D_LAYOUT,
487                        "%s: obj %p gen changed from %d to %d in trans declaration\n",
488                        luo->lo_dev->ld_obd->obd_name, lo, info->lti_gen[rc - 1],
489                        lo->ldo_layout_gen);
490                 return -EINVAL;
491         }
492
493         if (unlikely(info->lti_obj_count == LOD_OBJS_INTRANS)) {
494                 CERROR("%s: "DFID
495                        " save too many lod_object (%d) in one transaction, use bigger LOD_OBJS_INTRANS: rc = %d\n",
496                        luo->lo_dev->ld_obd->obd_name,
497                        PFID(lu_object_fid(luo)), LOD_OBJS_INTRANS, -E2BIG);
498                 return -E2BIG;
499         }
500
501         info->lti_obj[info->lti_obj_count] = lo;
502         info->lti_gen[info->lti_obj_count] = lo->ldo_layout_gen;
503         info->lti_obj_count++;
504
505         return 0;
506 }
507
508 extern const struct lu_device_operations lod_lu_ops;
509
510 static inline int lu_device_is_lod(struct lu_device *d)
511 {
512         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &lod_lu_ops);
513 }
514
515 static inline struct lod_device* lu2lod_dev(struct lu_device *d)
516 {
517         LASSERT(lu_device_is_lod(d));
518         return container_of_safe(d, struct lod_device, lod_dt_dev.dd_lu_dev);
519 }
520
521 static inline struct lu_device *lod2lu_dev(struct lod_device *d)
522 {
523         return &d->lod_dt_dev.dd_lu_dev;
524 }
525
526 static inline struct obd_device *lod2obd(struct lod_device *d)
527 {
528         return d->lod_dt_dev.dd_lu_dev.ld_obd;
529 }
530
531 static inline struct lod_device *dt2lod_dev(struct dt_device *d)
532 {
533         LASSERT(lu_device_is_lod(&d->dd_lu_dev));
534         return container_of(d, struct lod_device, lod_dt_dev);
535 }
536
537 static inline struct lod_object *lu2lod_obj(struct lu_object *o)
538 {
539         LASSERT(ergo(o != NULL, lu_device_is_lod(o->lo_dev)));
540         return container_of_safe(o, struct lod_object, ldo_obj.do_lu);
541 }
542
543 static inline struct lu_object *lod2lu_obj(struct lod_object *obj)
544 {
545         return &obj->ldo_obj.do_lu;
546 }
547
548 static inline const struct lu_fid *lod_object_fid(struct lod_object *obj)
549 {
550         return lu_object_fid(lod2lu_obj(obj));
551 }
552
553 static inline struct lod_object *lod_obj(const struct lu_object *o)
554 {
555         LASSERT(lu_device_is_lod(o->lo_dev));
556         return container_of(o, struct lod_object, ldo_obj.do_lu);
557 }
558
559 static inline struct lod_object *lod_dt_obj(const struct dt_object *d)
560 {
561         return lod_obj(&d->do_lu);
562 }
563
564 static inline struct dt_object* lod_object_child(struct lod_object *o)
565 {
566         return container_of(lu_object_next(lod2lu_obj(o)),
567                             struct dt_object, do_lu);
568 }
569
570 static inline bool lod_obj_is_striped(struct dt_object *dt)
571 {
572         struct lod_object *lo = lod_dt_obj(dt);
573         int i;
574         bool rc = false;
575
576         if (!dt_object_exists(dt_object_child(dt)))
577                 return false;
578
579         mutex_lock(&lo->ldo_layout_mutex);
580
581         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
582                 rc = lo->ldo_dir_stripe_count != 0;
583         } else if (lo->ldo_is_foreign) {
584                 rc = false;
585         } else {
586                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
587                         if (lo->ldo_comp_entries[i].llc_magic ==
588                             LOV_MAGIC_FOREIGN)
589                                 continue;
590                         if (lo->ldo_comp_entries[i].llc_stripe == NULL)
591                                 continue;
592                         LASSERT(lo->ldo_comp_entries[i].llc_stripe_count > 0);
593                         rc = true;
594                         break;
595                 }
596         }
597
598         mutex_unlock(&lo->ldo_layout_mutex);
599         return rc;
600 }
601
602 extern struct lu_context_key lod_thread_key;
603
604 static inline struct lod_thread_info *lod_env_info(const struct lu_env *env)
605 {
606         struct lod_thread_info *info;
607         info = lu_context_key_get(&env->le_ctx, &lod_thread_key);
608         LASSERT(info);
609         return info;
610 }
611
612 static inline struct lu_name *
613 lod_name_get(const struct lu_env *env, const void *area, int len)
614 {
615         struct lu_name *lname;
616
617         lname = &lod_env_info(env)->lti_name;
618         lname->ln_name = area;
619         lname->ln_namelen = len;
620         return lname;
621 }
622
623 static inline struct lod_default_striping *
624 lod_lds_buf_get(const struct lu_env *env)
625 {
626         struct lod_thread_info *info = lod_env_info(env);
627
628         info->lti_def_striping.lds_def_striping_set = 0;
629         info->lti_def_striping.lds_dir_def_striping_set = 0;
630         return &info->lti_def_striping;
631 }
632
633 static inline void lod_layout_get_pool(struct lod_layout_component *entries,
634                                        int count, char *pool, int len)
635 {
636         int i;
637
638         for (i = 0; i < count; i++) {
639                 if (entries[i].llc_magic == LOV_MAGIC_FOREIGN)
640                         continue;
641                 if (entries[i].llc_pool != NULL) {
642                         strscpy(pool, entries[i].llc_pool, len);
643                         break;
644                 }
645         }
646 }
647
648 #define lod_foreach_mdt(lod, mdt) ltd_foreach_tgt(&(lod)->lod_mdt_descs, mdt)
649 #define lod_foreach_ost(lod, ost) ltd_foreach_tgt(&(lod)->lod_ost_descs, ost)
650
651 /* lod_dev.c */
652 extern struct kmem_cache *lod_object_kmem;
653 int lod_fld_lookup(const struct lu_env *env, struct lod_device *lod,
654                    const struct lu_fid *fid, __u32 *tgt, int *flags);
655 int lod_sub_init_llog(const struct lu_env *env, struct lod_device *lod,
656                       struct dt_device *dt);
657 void lod_sub_fini_llog(const struct lu_env *env,
658                        struct dt_device *dt, struct task_struct **taskp);
659 int lodname2mdt_index(char *lodname, __u32 *mdt_index);
660 extern void target_recovery_fini(struct obd_device *obd);
661
662 /* lod_lov.c */
663 void lod_getref(struct lod_tgt_descs *ltd);
664 void lod_putref(struct lod_device *lod, struct lod_tgt_descs *ltd);
665 int lod_add_device(const struct lu_env *env, struct lod_device *lod,
666                    char *osp, unsigned index, unsigned gen, int mdt_index,
667                    char *type, int active);
668 int lod_del_device(const struct lu_env *env, struct lod_device *lod,
669                    struct lod_tgt_descs *ltd, char *osp, unsigned int idx,
670                    unsigned int gen);
671 int validate_lod_and_idx(struct lod_device *lod, __u32 idx);
672 int lod_fini_tgt(const struct lu_env *env, struct lod_device *lod,
673                  struct lod_tgt_descs *ltd);
674 int lod_striping_load(const struct lu_env *env, struct lod_object *lo);
675 int lod_striping_reload(const struct lu_env *env, struct lod_object *lo,
676                         const struct lu_buf *buf, enum layout_verify_flags lvf);
677 void lod_dom_stripesize_recalc(struct lod_device *d);
678
679 int lod_get_ea(const struct lu_env *env, struct lod_object *lo,
680                const char *name);
681 static inline int
682 lod_get_lov_ea(const struct lu_env *env, struct lod_object *lo)
683 {
684         return lod_get_ea(env, lo, XATTR_NAME_LOV);
685 }
686
687 static inline int
688 lod_get_lmv_ea(const struct lu_env *env, struct lod_object *lo)
689 {
690         return lod_get_ea(env, lo, XATTR_NAME_LMV);
691 }
692
693 static inline int
694 lod_get_default_lmv_ea(const struct lu_env *env, struct lod_object *lo)
695 {
696         return lod_get_ea(env, lo, XATTR_NAME_DEFAULT_LMV);
697 }
698
699 static inline void
700 lod_comp_set_init(struct lod_layout_component *entry)
701 {
702         entry->llc_flags |= LCME_FL_INIT;
703 }
704
705 static inline void
706 lod_comp_unset_init(struct lod_layout_component *entry)
707 {
708         entry->llc_flags &= ~LCME_FL_INIT;
709 }
710
711 static inline bool
712 lod_comp_inited(const struct lod_layout_component *entry)
713 {
714         return entry->llc_flags & LCME_FL_INIT;
715 }
716
717 /**
718  * For a PFL file, some of its component could be un-instantiated, so
719  * that their lov_ost_data_v1 array is not needed, we'd use this function
720  * to reduce the LOVEA buffer size.
721  *
722  * Note: if llc_ostlist contains value, we'd need lov_ost_data_v1 array to
723  * save the specified OST index list.
724  */
725 static inline void
726 lod_comp_shrink_stripe_count(struct lod_layout_component *lod_comp,
727                              __u16 *stripe_count)
728 {
729         /**
730          * Need one lov_ost_data_v1 to store invalid ost_idx, please refer to
731          * lod_parse_striping()
732          */
733         if (!lod_comp_inited(lod_comp) && lod_comp->llc_ostlist.op_count == 0)
734                 *stripe_count = 1;
735 }
736
737 void lod_fix_desc(struct lov_desc *desc);
738 void lod_fix_desc_qos_maxage(__u32 *val);
739 void lod_fix_desc_pattern(__u32 *val);
740 void lod_fix_desc_stripe_count(__u32 *val);
741 void lod_fix_desc_stripe_size(__u64 *val);
742 void lod_fix_lmv_desc_pattern(__u32 *val);
743 int lod_pools_init(struct lod_device *m, struct lustre_cfg *cfg);
744 int lod_pools_fini(struct lod_device *m);
745 int lod_parse_striping(const struct lu_env *env, struct lod_object *mo,
746                        const struct lu_buf *buf, enum layout_verify_flags lvf);
747 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
748                            const struct lu_buf *buf);
749 int lod_initialize_objects(const struct lu_env *env, struct lod_object *mo,
750                            struct lov_ost_data_v1 *objs, int index);
751 int lod_verify_striping(const struct lu_env *env, struct lod_device *d,
752                         struct lod_object *lo, const struct lu_buf *buf,
753                         bool is_from_disk);
754 int lod_generate_lovea(const struct lu_env *env, struct lod_object *lo,
755                        struct lov_mds_md *lmm, int *lmm_size, bool is_dir);
756 int lod_ea_store_resize(struct lod_thread_info *info, size_t size);
757 int lod_def_striping_comp_resize(struct lod_default_striping *lds, __u16 count);
758 void lod_free_def_comp_entries(struct lod_default_striping *lds);
759 void lod_free_comp_entries(struct lod_object *lo);
760 int lod_alloc_comp_entries(struct lod_object *lo, int mirror_cnt, int comp_cnt);
761 int lod_fill_mirrors(struct lod_object *lo);
762 int lod_init_comp_foreign(struct lod_layout_component *lod_comp, void *lmm);
763
764 /* lod_pool.c */
765 struct lod_pool_desc *lod_find_pool(struct lod_device *lod, char *poolname);
766 void lod_pool_putref(struct lod_pool_desc *pool);
767 int lod_pool_del(struct obd_device *obd, char *poolname);
768 int lod_check_index_in_pool(__u32 idx, struct lod_pool_desc *pool);
769 int lod_pool_new(struct obd_device *obd, char *poolname);
770 int lod_pool_add(struct obd_device *obd, char *poolname, char *ostname);
771 int lod_pool_remove(struct obd_device *obd, char *poolname, char *ostname);
772
773 struct lod_obj_stripe_cb_data;
774 typedef int (*lod_obj_stripe_cb_t)(const struct lu_env *env,
775                                    struct lod_object *lo, struct dt_object *dt,
776                                    struct thandle *th,
777                                    int comp_idx, int stripe_idx,
778                                    struct lod_obj_stripe_cb_data *data);
779 typedef bool (*lod_obj_comp_skip_cb_t)(const struct lu_env *env,
780                                         struct lod_object *lo, int comp_idx,
781                                         struct lod_obj_stripe_cb_data *data);
782 typedef int (*lod_obj_comp_cb_t)(const struct lu_env *env,
783                                 struct lod_object *lo, int comp_idx,
784                                 struct lod_obj_stripe_cb_data *data);
785 struct lod_obj_stripe_cb_data {
786         union {
787                 const struct lu_attr    *locd_attr;
788                 int                     locd_ost_index;
789                 const struct lu_buf     *locd_buf;
790         };
791         lod_obj_stripe_cb_t             locd_stripe_cb;
792         lod_obj_comp_skip_cb_t          locd_comp_skip_cb;
793         lod_obj_comp_cb_t               locd_comp_cb;
794         bool                            locd_declare;
795 };
796
797 /* lod_qos.c */
798 int lod_mdt_alloc_qos(const struct lu_env *env, struct lod_object *lo,
799                       struct dt_object **stripes, u32 stripe_idx,
800                       u32 stripe_count);
801 int lod_mdt_alloc_rr(const struct lu_env *env, struct lod_object *lo,
802                      struct dt_object **stripes, u32 stripe_idx,
803                      u32 stripe_count);
804 int lod_prepare_create(const struct lu_env *env, struct lod_object *lo,
805                        struct lu_attr *attr, const struct lu_buf *buf,
806                        struct thandle *th);
807 int lod_use_defined_striping(const struct lu_env *, struct lod_object *,
808                              const struct lu_buf *);
809 int lod_qos_parse_config(const struct lu_env *env, struct lod_object *lo,
810                          const struct lu_buf *buf);
811 int lod_qos_prep_create(const struct lu_env *env, struct lod_object *lo,
812                         struct lu_attr *attr, struct thandle *th,
813                         int comp_idx, __u64 reserve);
814 __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
815                                   int comp_idx, bool is_dir);
816 __u16 lod_get_stripe_count_plain(struct lod_device *lod, struct lod_object *lo,
817                                  __u16 stripe_count, bool overstriping,
818                                  enum lod_uses_hint *flags);
819 __u16 lod_get_stripe_count(struct lod_device *lod, struct lod_object *lo,
820                            int comp_idx, __u16 stripe_count, bool overstriping,
821                            enum lod_uses_hint *flags);
822 void lod_qos_statfs_update(const struct lu_env *env, struct lod_device *lod,
823                            struct lu_tgt_descs *ltd);
824
825 /* lproc_lod.c */
826 int lod_procfs_init(struct lod_device *lod);
827 void lod_procfs_fini(struct lod_device *lod);
828
829 /* lod_object.c */
830 extern const struct dt_object_operations lod_obj_ops;
831 extern const struct lu_object_operations lod_lu_obj_ops;
832
833 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
834                         struct lu_buf *buf, bool resize);
835 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
836                                struct lu_attr *attr,
837                                const struct lu_buf *lovea, struct thandle *th);
838 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
839                         struct lu_attr *attr, struct dt_object_format *dof,
840                         struct thandle *th);
841 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size);
842 void lod_free_foreign_lov(struct lod_object *lo);
843 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo);
844 void lod_striping_free(const struct lu_env *env, struct lod_object *lo);
845
846 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
847                             struct thandle *th,
848                             struct lod_obj_stripe_cb_data *data);
849 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
850                             struct lov_user_md_v3 *v3);
851 void lod_adjust_stripe_size(struct lod_layout_component *comp,
852                             __u32 def_stripe_size);
853
854 /* lod_sub_object.c */
855 struct thandle *lod_sub_get_thandle(const struct lu_env *env,
856                                     struct thandle *th,
857                                     const struct dt_object *sub_obj,
858                                     bool *record_update);
859 int lod_sub_declare_create(const struct lu_env *env, struct dt_object *dt,
860                            struct lu_attr *attr,
861                            struct dt_allocation_hint *hint,
862                            struct dt_object_format *dof, struct thandle *th);
863 int lod_sub_create(const struct lu_env *env, struct dt_object *dt,
864                    struct lu_attr *attr, struct dt_allocation_hint *hint,
865                    struct dt_object_format *dof, struct thandle *th);
866 int lod_sub_declare_ref_add(const struct lu_env *env, struct dt_object *dt,
867                             struct thandle *th);
868 int lod_sub_ref_add(const struct lu_env *env, struct dt_object *dt,
869                     struct thandle *th);
870 int lod_sub_declare_ref_del(const struct lu_env *env, struct dt_object *dt,
871                             struct thandle *th);
872 int lod_sub_ref_del(const struct lu_env *env, struct dt_object *dt,
873                     struct thandle *th);
874 int lod_sub_declare_destroy(const struct lu_env *env, struct dt_object *dt,
875                             struct thandle *th);
876 int lod_sub_destroy(const struct lu_env *env, struct dt_object *dt,
877                     struct thandle *th);
878 int lod_sub_declare_insert(const struct lu_env *env, struct dt_object *dt,
879                            const struct dt_rec *rec, const struct dt_key *key,
880                            struct thandle *th);
881 int lod_sub_insert(const struct lu_env *env, struct dt_object *dt,
882                    const struct dt_rec *rec, const struct dt_key *key,
883                    struct thandle *th);
884 int lod_sub_declare_delete(const struct lu_env *env, struct dt_object *dt,
885                            const struct dt_key *key, struct thandle *th);
886 int lod_sub_delete(const struct lu_env *env, struct dt_object *dt,
887                    const struct dt_key *name, struct thandle *th);
888 int lod_sub_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
889                               const struct lu_buf *buf, const char *name,
890                               int fl, struct thandle *th);
891 int lod_sub_xattr_set(const struct lu_env *env, struct dt_object *dt,
892                       const struct lu_buf *buf, const char *name, int fl,
893                       struct thandle *th);
894 int lod_sub_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
895                              const struct lu_attr *attr, struct thandle *th);
896 int lod_sub_attr_set(const struct lu_env *env, struct dt_object *dt,
897                      const struct lu_attr *attr, struct thandle *th);
898 int lod_sub_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
899                               const char *name, struct thandle *th);
900 int lod_sub_xattr_del(const struct lu_env *env, struct dt_object *dt,
901                       const char *name, struct thandle *th);
902 int lod_sub_declare_write(const struct lu_env *env, struct dt_object *dt,
903                           const struct lu_buf *buf, loff_t pos,
904                           struct thandle *th);
905 ssize_t lod_sub_write(const struct lu_env *env, struct dt_object *dt,
906                       const struct lu_buf *buf, loff_t *pos,
907                       struct thandle *th);
908 int lod_sub_declare_punch(const struct lu_env *env, struct dt_object *dt,
909                           __u64 start, __u64 end, struct thandle *th);
910 int lod_sub_punch(const struct lu_env *env, struct dt_object *dt,
911                   __u64 start, __u64 end, struct thandle *th);
912
913 int lod_sub_prep_llog(const struct lu_env *env, struct lod_device *lod,
914                       struct dt_device *dt, int index);
915 void lod_check_and_spill_pool(const struct lu_env *env, struct lod_device *lod,
916                               char **poolname);
917 void lod_spill_target_refresh(const struct lu_env *env, struct lod_device *lod,
918                               struct lod_pool_desc *pool);
919 struct lod_pool_desc *lod_pool_find(struct lod_device *lod, char *poolname);
920 int lod_tgt_weights_seq_show(struct seq_file *m, struct lod_device *lod,
921                              struct lu_tgt_pool *tgts, bool mdt);
922 int lod_tgt_weights_seq_write(struct seq_file *m, const char __user *buf,
923                               size_t count, struct lod_device *lod,
924                               struct lu_tgt_pool *tgts, bool is_mdt);
925 extern struct lprocfs_vars lprocfs_lod_spill_vars[];
926 #endif