Whamcloud - gitweb
1d0a39cc03e4290b6d1ec0852def01d2985957f7
[fs/lustre-release.git] / lustre / obdclass / dt_object.c
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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/obdclass/dt_object.c
32  *
33  * Dt Object.
34  * Generic functions from dt_object.h
35  *
36  * Author: Nikita Danilov <nikita@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_CLASS
40
41 #include <linux/list.h>
42 #include <obd_class.h>
43 #include <dt_object.h>
44 /* fid_be_to_cpu() */
45 #include <lustre_fid.h>
46 #include <lustre_nodemap.h>
47 #include <lustre_quota.h>
48 #include <lustre_lfsck.h>
49
50 /* context key constructor/destructor: dt_global_key_init, dt_global_key_fini */
51 LU_KEY_INIT(dt_global, struct dt_thread_info);
52 LU_KEY_FINI(dt_global, struct dt_thread_info);
53
54 struct lu_context_key dt_key = {
55         .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD | LCT_MG_THREAD | LCT_LOCAL,
56         .lct_init = dt_global_key_init,
57         .lct_fini = dt_global_key_fini
58 };
59
60 /*
61  * no lock is necessary to protect the list, because call-backs
62  * are added during system startup. Please refer to "struct dt_device".
63  */
64 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb)
65 {
66         list_add(&cb->dtc_linkage, &dev->dd_txn_callbacks);
67 }
68 EXPORT_SYMBOL(dt_txn_callback_add);
69
70 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb)
71 {
72         list_del_init(&cb->dtc_linkage);
73 }
74 EXPORT_SYMBOL(dt_txn_callback_del);
75
76 int dt_txn_hook_start(const struct lu_env *env,
77                       struct dt_device *dev, struct thandle *th)
78 {
79         int rc = 0;
80         struct dt_txn_callback *cb;
81
82         if (th->th_local)
83                 return 0;
84
85         list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
86                 struct thandle *dtc_th = th;
87
88                 if (cb->dtc_txn_start == NULL ||
89                     !(cb->dtc_tag & env->le_ctx.lc_tags))
90                         continue;
91
92                 /*
93                  * Usually dt_txn_hook_start is called from bottom device,
94                  * and if the thandle has th_top, then we need use top
95                  * thandle for the callback in the top thandle layer
96                  */
97                 if (th->th_top != NULL)
98                         dtc_th = th->th_top;
99
100                 rc = cb->dtc_txn_start(env, dtc_th, cb->dtc_cookie);
101                 if (rc < 0)
102                         break;
103         }
104         return rc;
105 }
106 EXPORT_SYMBOL(dt_txn_hook_start);
107
108 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *th)
109 {
110         struct dt_device *dev = th->th_dev;
111         struct dt_txn_callback *cb;
112         int rc = 0;
113
114         if (th->th_local)
115                 return 0;
116
117         if (OBD_FAIL_CHECK(OBD_FAIL_DT_TXN_STOP))
118                 return -EIO;
119
120         list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
121                 struct thandle *dtc_th = th;
122
123                 if (cb->dtc_txn_stop == NULL ||
124                     !(cb->dtc_tag & env->le_ctx.lc_tags))
125                         continue;
126
127                 /*
128                  * Usually dt_txn_hook_stop is called from bottom device,
129                  * and if the thandle has th_top, then we need use top
130                  * thandle for the callback in the top thandle layer
131                  */
132                 if (th->th_top != NULL)
133                         dtc_th = th->th_top;
134
135                 rc = cb->dtc_txn_stop(env, dtc_th, cb->dtc_cookie);
136                 if (rc < 0)
137                         break;
138         }
139         return rc;
140 }
141 EXPORT_SYMBOL(dt_txn_hook_stop);
142
143 int dt_device_init(struct dt_device *dev, struct lu_device_type *t)
144 {
145         INIT_LIST_HEAD(&dev->dd_txn_callbacks);
146         return lu_device_init(&dev->dd_lu_dev, t);
147 }
148 EXPORT_SYMBOL(dt_device_init);
149
150 void dt_device_fini(struct dt_device *dev)
151 {
152         lu_device_fini(&dev->dd_lu_dev);
153 }
154 EXPORT_SYMBOL(dt_device_fini);
155
156 int dt_object_init(struct dt_object *obj,
157                    struct lu_object_header *h, struct lu_device *d)
158
159 {
160         return lu_object_init(&obj->do_lu, h, d);
161 }
162 EXPORT_SYMBOL(dt_object_init);
163
164 void dt_object_fini(struct dt_object *obj)
165 {
166         lu_object_fini(&obj->do_lu);
167 }
168 EXPORT_SYMBOL(dt_object_fini);
169
170 /**
171  * Set directory .do_index_ops.
172  *
173  * Set directory index operations, if the caller knows directory exists,
174  * \a check should be set to ensure object is directory and exists, while for
175  * new directories, skip check and the index operations will be used to create
176  * ".." under directory.
177  *
178  * Normally this is called before dt_lookup() to ensure directory objects
179  * exists and .do_index_ops is correctly set.
180  *
181  * \param env   lu_env object.
182  * \param obj   dt object.
183  * \param check check \a obj existence and type, return if index ops is set.
184  * \retval 1    on success.
185  * \retval 0    on error.
186  */
187 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj, bool check)
188 {
189         if (check) {
190                 if (unlikely(!dt_object_exists(obj)))
191                         return 0;
192
193                 if (unlikely(!S_ISDIR(lu_object_attr(&obj->do_lu))))
194                         return 0;
195
196                 if (obj->do_index_ops)
197                         return 1;
198         }
199
200         obj->do_ops->do_index_try(env, obj, &dt_directory_features);
201
202         return obj->do_index_ops != NULL;
203 }
204 EXPORT_SYMBOL(dt_try_as_dir);
205
206 enum dt_format_type dt_mode_to_dft(__u32 mode)
207 {
208         enum dt_format_type result;
209
210         switch (mode & S_IFMT) {
211         case S_IFDIR:
212                 result = DFT_DIR;
213                 break;
214         case S_IFREG:
215                 result = DFT_REGULAR;
216                 break;
217         case S_IFLNK:
218                 result = DFT_SYM;
219                 break;
220         case S_IFCHR:
221         case S_IFBLK:
222         case S_IFIFO:
223         case S_IFSOCK:
224                 result = DFT_NODE;
225                 break;
226         default:
227                 LASSERTF(0, "invalid mode %o\n", mode);
228                 result = 0; /* Just for satisfying compiler. */
229                 break;
230         }
231         return result;
232 }
233 EXPORT_SYMBOL(dt_mode_to_dft);
234
235 /**
236  * lookup fid for object named \a name in directory \a dir.
237  */
238
239 int dt_lookup_dir(const struct lu_env *env, struct dt_object *dir,
240                   const char *name, struct lu_fid *fid)
241 {
242         if (dt_try_as_dir(env, dir, true))
243                 return dt_lookup(env, dir, (struct dt_rec *)fid,
244                                  (const struct dt_key *)name);
245         return -ENOTDIR;
246 }
247 EXPORT_SYMBOL(dt_lookup_dir);
248
249 /*
250  * this differs from dt_locate by top_dev as parameter
251  * but not one from lu_site
252  */
253 struct dt_object *dt_locate_at(const struct lu_env *env,
254                                struct dt_device *dev,
255                                const struct lu_fid *fid,
256                                struct lu_device *top_dev,
257                                const struct lu_object_conf *conf)
258 {
259         struct lu_object *lo;
260         struct lu_object *n;
261
262         lo = lu_object_find_at(env, top_dev, fid, conf);
263         if (IS_ERR(lo))
264                 return ERR_CAST(lo);
265
266         LASSERT(lo != NULL);
267
268         list_for_each_entry(n, &lo->lo_header->loh_layers, lo_linkage) {
269                 if (n->lo_dev == &dev->dd_lu_dev)
270                         return container_of(n, struct dt_object, do_lu);
271         }
272
273         lu_object_put(env, lo);
274         return ERR_PTR(-ENOENT);
275 }
276 EXPORT_SYMBOL(dt_locate_at);
277
278 /**
279  * find an object named \a entry in given \a dfh->dfh_o directory.
280  */
281 static int dt_find_entry(const struct lu_env *env, const char *entry,
282                          void *data)
283 {
284         struct dt_find_hint *dfh = data;
285         struct dt_device *dt = dfh->dfh_dt;
286         struct lu_fid *fid = dfh->dfh_fid;
287         struct dt_object *obj = dfh->dfh_o;
288         int rc;
289
290         rc = dt_lookup_dir(env, obj, entry, fid);
291         dt_object_put(env, obj);
292         if (rc == 0) {
293                 obj = dt_locate(env, dt, fid);
294                 if (IS_ERR(obj))
295                         rc = PTR_ERR(obj);
296         }
297         dfh->dfh_o = obj;
298
299         return rc;
300 }
301
302 /**
303  * Abstract function which parses path name. This function feeds
304  * path component to \a entry_func.
305  */
306 int dt_path_parser(const struct lu_env *env,
307                    char *path, dt_entry_func_t entry_func,
308                    void *data)
309 {
310         char *e;
311         int rc = 0;
312
313         while (1) {
314                 e = strsep(&path, "/");
315                 if (e == NULL)
316                         break;
317
318                 if (e[0] == 0) {
319                         if (!path || path[0] == '\0')
320                                 break;
321                         continue;
322                 }
323                 rc = entry_func(env, e, data);
324                 if (rc)
325                         break;
326         }
327
328         return rc;
329 }
330
331 struct dt_object *
332 dt_store_resolve(const struct lu_env *env, struct dt_device *dt,
333                  const char *path, struct lu_fid *fid)
334 {
335         struct dt_thread_info *info = dt_info(env);
336         struct dt_find_hint *dfh = &info->dti_dfh;
337         struct dt_object *obj;
338         int result;
339
340
341         dfh->dfh_dt = dt;
342         dfh->dfh_fid = fid;
343
344         strlcpy(info->dti_buf, path, sizeof(info->dti_buf));
345
346         result = dt->dd_ops->dt_root_get(env, dt, fid);
347         if (result == 0) {
348                 obj = dt_locate(env, dt, fid);
349                 if (!IS_ERR(obj)) {
350                         dfh->dfh_o = obj;
351                         result = dt_path_parser(env, info->dti_buf,
352                                                 dt_find_entry, dfh);
353                         if (result != 0)
354                                 obj = ERR_PTR(result);
355                         else
356                                 obj = dfh->dfh_o;
357                 }
358         } else {
359                 obj = ERR_PTR(result);
360         }
361         return obj;
362 }
363
364 static struct dt_object *dt_reg_open(const struct lu_env *env,
365                                      struct dt_device *dt,
366                                      struct dt_object *p,
367                                      const char *name,
368                                      struct lu_fid *fid)
369 {
370         struct dt_object *o;
371         int result;
372
373         result = dt_lookup_dir(env, p, name, fid);
374         if (result == 0)
375                 o = dt_locate(env, dt, fid);
376         else
377                 o = ERR_PTR(result);
378
379         return o;
380 }
381
382 /**
383  * Open dt object named \a filename from \a dirname directory.
384  *      \param  dt      dt device
385  *      \param  fid     on success, object fid is stored in *fid
386  */
387 struct dt_object *dt_store_open(const struct lu_env *env, struct dt_device *dt,
388                                 const char *dirname, const char *filename,
389                                 struct lu_fid *fid)
390 {
391         struct dt_object *file;
392         struct dt_object *dir;
393
394         dir = dt_store_resolve(env, dt, dirname, fid);
395         if (!IS_ERR(dir)) {
396                 file = dt_reg_open(env, dt, dir, filename, fid);
397                 dt_object_put(env, dir);
398         } else {
399                 file = dir;
400         }
401
402         return file;
403 }
404
405 struct dt_object *dt_find_or_create(const struct lu_env *env,
406                                     struct dt_device *dt,
407                                     const struct lu_fid *fid,
408                                     struct dt_object_format *dof,
409                                     struct lu_attr *at)
410 {
411         struct dt_object *dto;
412         struct thandle *th;
413         int rc;
414
415         ENTRY;
416
417         dto = dt_locate(env, dt, fid);
418         if (IS_ERR(dto))
419                 RETURN(dto);
420
421         LASSERT(dto != NULL);
422         if (dt_object_exists(dto))
423                 RETURN(dto);
424
425         th = dt_trans_create(env, dt);
426         if (IS_ERR(th))
427                 GOTO(out, rc = PTR_ERR(th));
428
429         rc = dt_declare_create(env, dto, at, NULL, dof, th);
430         if (rc)
431                 GOTO(trans_stop, rc);
432
433         rc = dt_trans_start_local(env, dt, th);
434         if (rc)
435                 GOTO(trans_stop, rc);
436
437         dt_write_lock(env, dto, 0);
438         if (dt_object_exists(dto))
439                 GOTO(unlock, rc = 0);
440
441         CDEBUG(D_OTHER, "create new object "DFID"\n", PFID(fid));
442
443         rc = dt_create(env, dto, at, NULL, dof, th);
444         if (rc)
445                 GOTO(unlock, rc);
446         LASSERT(dt_object_exists(dto));
447 unlock:
448         dt_write_unlock(env, dto);
449 trans_stop:
450         dt_trans_stop(env, dt, th);
451 out:
452         if (rc) {
453                 dt_object_put(env, dto);
454                 dto = ERR_PTR(rc);
455         }
456
457         RETURN(dto);
458 }
459 EXPORT_SYMBOL(dt_find_or_create);
460
461 /* dt class init function. */
462 int dt_global_init(void)
463 {
464         int result;
465
466         LU_CONTEXT_KEY_INIT(&dt_key);
467         result = lu_context_key_register(&dt_key);
468         return result;
469 }
470
471 void dt_global_fini(void)
472 {
473         lu_context_key_degister(&dt_key);
474 }
475
476 /**
477  * Generic read helper. May return an error for partial reads.
478  *
479  * \param env  lustre environment
480  * \param dt   object to be read
481  * \param buf  lu_buf to be filled, with buffer pointer and length
482  * \param pos position to start reading, updated as data is read
483  *
484  * \retval real size of data read
485  * \retval -ve errno on failure
486  */
487 int dt_read(const struct lu_env *env, struct dt_object *dt,
488             struct lu_buf *buf, loff_t *pos)
489 {
490         LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
491         return dt->do_body_ops->dbo_read(env, dt, buf, pos);
492 }
493 EXPORT_SYMBOL(dt_read);
494
495 /**
496  * Read structures of fixed size from storage.  Unlike dt_read(), using
497  * dt_record_read() will return an error for partial reads.
498  *
499  * \param env  lustre environment
500  * \param dt   object to be read
501  * \param buf  lu_buf to be filled, with buffer pointer and length
502  * \param pos position to start reading, updated as data is read
503  *
504  * \retval 0 on successfully reading full buffer
505  * \retval -EFAULT on short read
506  * \retval -ve errno on failure
507  */
508 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
509                    struct lu_buf *buf, loff_t *pos)
510 {
511         ssize_t size;
512
513         LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
514
515         size = dt->do_body_ops->dbo_read(env, dt, buf, pos);
516         if (size < 0)
517                 return size;
518         return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
519 }
520 EXPORT_SYMBOL(dt_record_read);
521
522 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
523                     const struct lu_buf *buf, loff_t *pos, struct thandle *th)
524 {
525         ssize_t size;
526
527         LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
528         LASSERT(th != NULL);
529         LASSERT(dt->do_body_ops);
530         LASSERT(dt->do_body_ops->dbo_write);
531
532         size = dt->do_body_ops->dbo_write(env, dt, buf, pos, th);
533         if (size < 0)
534                 return size;
535         return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
536 }
537 EXPORT_SYMBOL(dt_record_write);
538
539 int dt_declare_version_set(const struct lu_env *env, struct dt_object *o,
540                            struct thandle *th)
541 {
542         struct lu_buf vbuf;
543         char *xname = XATTR_NAME_VERSION;
544
545         LASSERT(o);
546         vbuf.lb_buf = NULL;
547         vbuf.lb_len = sizeof(dt_obj_version_t);
548         return dt_declare_xattr_set(env, o, &vbuf, xname, 0, th);
549
550 }
551 EXPORT_SYMBOL(dt_declare_version_set);
552
553 void dt_version_set(const struct lu_env *env, struct dt_object *o,
554                     dt_obj_version_t version, struct thandle *th)
555 {
556         struct lu_buf vbuf;
557         char *xname = XATTR_NAME_VERSION;
558         int rc;
559
560         LASSERT(o);
561         vbuf.lb_buf = &version;
562         vbuf.lb_len = sizeof(version);
563
564         rc = dt_xattr_set(env, o, &vbuf, xname, 0, th);
565         if (rc < 0)
566                 CDEBUG(D_INODE, "Can't set version, rc %d\n", rc);
567 }
568 EXPORT_SYMBOL(dt_version_set);
569
570 dt_obj_version_t dt_version_get(const struct lu_env *env, struct dt_object *o)
571 {
572         struct lu_buf vbuf;
573         char *xname = XATTR_NAME_VERSION;
574         dt_obj_version_t version;
575         int rc;
576
577         LASSERT(o);
578         vbuf.lb_buf = &version;
579         vbuf.lb_len = sizeof(version);
580         rc = dt_xattr_get(env, o, &vbuf, xname);
581         if (rc != sizeof(version)) {
582                 CDEBUG(D_INODE, "Can't get version, rc %d\n", rc);
583                 version = 0;
584         }
585         return version;
586 }
587 EXPORT_SYMBOL(dt_version_get);
588
589 /* list of all supported index types */
590
591 /* directories */
592 const struct dt_index_features dt_directory_features;
593 EXPORT_SYMBOL(dt_directory_features);
594
595 /* scrub iterator */
596 const struct dt_index_features dt_otable_features;
597 EXPORT_SYMBOL(dt_otable_features);
598
599 /* lfsck layout orphan */
600 const struct dt_index_features dt_lfsck_layout_orphan_features = {
601         .dif_flags              = 0,
602         .dif_keysize_min        = sizeof(struct lu_fid),
603         .dif_keysize_max        = sizeof(struct lu_fid),
604         .dif_recsize_min        = sizeof(struct lu_orphan_rec_v3),
605         .dif_recsize_max        = sizeof(struct lu_orphan_rec_v3),
606         .dif_ptrsize            = 4
607 };
608 EXPORT_SYMBOL(dt_lfsck_layout_orphan_features);
609
610 /* lfsck layout dangling */
611 const struct dt_index_features dt_lfsck_layout_dangling_features = {
612         .dif_flags              = DT_IND_UPDATE,
613         .dif_keysize_min        = sizeof(struct lfsck_layout_dangling_key),
614         .dif_keysize_max        = sizeof(struct lfsck_layout_dangling_key),
615         .dif_recsize_min        = sizeof(struct lu_fid),
616         .dif_recsize_max        = sizeof(struct lu_fid),
617         .dif_ptrsize            = 4
618 };
619 EXPORT_SYMBOL(dt_lfsck_layout_dangling_features);
620
621 /* lfsck namespace */
622 const struct dt_index_features dt_lfsck_namespace_features = {
623         .dif_flags              = DT_IND_UPDATE,
624         .dif_keysize_min        = sizeof(struct lu_fid),
625         .dif_keysize_max        = sizeof(struct lu_fid),
626         .dif_recsize_min        = sizeof(__u8),
627         .dif_recsize_max        = sizeof(__u8),
628         .dif_ptrsize            = 4
629 };
630 EXPORT_SYMBOL(dt_lfsck_namespace_features);
631
632 /* accounting indexes */
633 const struct dt_index_features dt_acct_features = {
634         .dif_flags              = DT_IND_UPDATE,
635         .dif_keysize_min        = sizeof(__u64), /* 64-bit uid/gid */
636         .dif_keysize_max        = sizeof(__u64), /* 64-bit uid/gid */
637         .dif_recsize_min        = sizeof(struct lquota_acct_rec), /* 16 bytes */
638         .dif_recsize_max        = sizeof(struct lquota_acct_rec), /* 16 bytes */
639         .dif_ptrsize            = 4
640 };
641 EXPORT_SYMBOL(dt_acct_features);
642
643 /* global quota files */
644 const struct dt_index_features dt_quota_glb_features = {
645         .dif_flags              = DT_IND_UPDATE,
646         /* a different key would have to be used for per-directory quota */
647         .dif_keysize_min        = sizeof(__u64), /* 64-bit uid/gid */
648         .dif_keysize_max        = sizeof(__u64), /* 64-bit uid/gid */
649         .dif_recsize_min        = sizeof(struct lquota_glb_rec), /* 32 bytes */
650         .dif_recsize_max        = sizeof(struct lquota_glb_rec), /* 32 bytes */
651         .dif_ptrsize            = 4
652 };
653 EXPORT_SYMBOL(dt_quota_glb_features);
654
655 /* slave quota files */
656 const struct dt_index_features dt_quota_slv_features = {
657         .dif_flags              = DT_IND_UPDATE,
658         /* a different key would have to be used for per-directory quota */
659         .dif_keysize_min        = sizeof(__u64), /* 64-bit uid/gid */
660         .dif_keysize_max        = sizeof(__u64), /* 64-bit uid/gid */
661         .dif_recsize_min        = sizeof(struct lquota_slv_rec), /* 8 bytes */
662         .dif_recsize_max        = sizeof(struct lquota_slv_rec), /* 8 bytes */
663         .dif_ptrsize            = 4
664 };
665 EXPORT_SYMBOL(dt_quota_slv_features);
666
667 /* nodemap files, nodemap_rec size asserted in nodemap_storage.c */
668 const struct dt_index_features dt_nodemap_features = {
669         .dif_flags              = DT_IND_UPDATE,
670         .dif_keysize_min        = sizeof(__u64), /* 64-bit nodemap/record id */
671         .dif_keysize_max        = sizeof(__u64), /* 64-bit nodemap/record id */
672         .dif_recsize_min        = sizeof(union nodemap_rec), /* 32 bytes */
673         .dif_recsize_max        = sizeof(union nodemap_rec), /* 32 bytes */
674         .dif_ptrsize            = 4
675 };
676 EXPORT_SYMBOL(dt_nodemap_features);
677
678 /*
679  * helper function returning what dt_index_features structure should be used
680  * based on the FID sequence. This is used by OBD_IDX_READ RPC
681  */
682 static inline const struct dt_index_features *dt_index_feat_select(__u64 seq,
683                                                                    __u32 mode)
684 {
685         if (seq == FID_SEQ_QUOTA_GLB) {
686                 /* global quota index */
687                 if (!S_ISREG(mode))
688                         /* global quota index should be a regular file */
689                         return ERR_PTR(-ENOENT);
690                 return &dt_quota_glb_features;
691         } else if (seq == FID_SEQ_QUOTA) {
692                 /* quota slave index */
693                 if (!S_ISREG(mode))
694                         /* slave index should be a regular file */
695                         return ERR_PTR(-ENOENT);
696                 return &dt_quota_slv_features;
697         } else if (seq == FID_SEQ_LAYOUT_RBTREE){
698                 return &dt_lfsck_layout_orphan_features;
699         } else if (seq >= FID_SEQ_NORMAL) {
700                 /* object is part of the namespace, verify that it is a
701                  * directory */
702                 if (!S_ISDIR(mode))
703                         /* sorry, we can only deal with directory */
704                         return ERR_PTR(-ENOTDIR);
705                 return &dt_directory_features;
706         }
707
708         return ERR_PTR(-EOPNOTSUPP);
709 }
710
711 /*
712  * Fill a lu_idxpage with key/record pairs read for transfer via OBD_IDX_READ
713  * RPC
714  *
715  * \param env - is the environment passed by the caller
716  * \param obj - index object being traversed (mostly for debugging)
717  * \param lp  - is a pointer to the lu_page to fill
718  * \param bytes - is the maximum number of bytes that should be copied
719  * \param iops - is the index operation vector associated with the index object
720  * \param it   - is a pointer to the current iterator
721  * \param attr - is the index attribute to pass to iops->rec()
722  * \param arg  - is a pointer to the idx_info structure
723  */
724 static int dt_index_page_build(const struct lu_env *env, struct dt_object *obj,
725                                union lu_page *lp, size_t bytes,
726                                const struct dt_it_ops *iops,
727                                struct dt_it *it, __u32 attr, void *arg)
728 {
729         struct idx_info *ii = (struct idx_info *)arg;
730         struct lu_idxpage *lip = &lp->lp_idx;
731         char *entry;
732         __u64 hash;
733         __u16 hashsize = 0;
734         __u16 keysize = 0;
735         __u16 recsize;
736         int rc;
737
738         ENTRY;
739
740         if (bytes < LIP_HDR_SIZE)
741                 return -EINVAL;
742
743         /* initialize the header of the new container */
744         memset(lip, 0, LIP_HDR_SIZE);
745         lip->lip_magic = LIP_MAGIC;
746         bytes -= LIP_HDR_SIZE;
747
748         /* client wants to the 64-bit hash value associated with each record */
749         if (!(ii->ii_flags & II_FL_NOHASH))
750                 hashsize = sizeof(hash);
751
752         entry = lip->lip_entries;
753         do {
754                 /* fetch 64-bit hash value */
755                 hash = iops->store(env, it);
756                 ii->ii_hash_end = hash;
757
758                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_IDX_READ_BREAK)) {
759                         if (lip->lip_nr != 0)
760                                 GOTO(out, rc = 0);
761                 }
762
763                 if (!(ii->ii_flags & II_FL_NOKEY)) {
764                         keysize = iops->key_size(env, it);
765                         if (!(ii->ii_flags & II_FL_VARKEY) &&
766                             keysize != ii->ii_keysize) {
767                                 rc = -EINVAL;
768                                 CERROR("%s: keysize mismatch %hu != %hu on "
769                                        DFID": rc = %d\n",
770                                        lu_dev_name(obj->do_lu.lo_dev),
771                                        keysize, ii->ii_keysize,
772                                        PFID(lu_object_fid(&obj->do_lu)), rc);
773                                 GOTO(out, rc);
774                         }
775                 }
776
777                 /* and finally the record */
778                 if (ii->ii_flags & II_FL_VARREC)
779                         recsize = iops->rec_size(env, it, attr);
780                 else
781                         recsize = ii->ii_recsize;
782
783                 if (bytes < hashsize + keysize + recsize) {
784                         if (lip->lip_nr == 0)
785                                 GOTO(out, rc = -E2BIG);
786                         GOTO(out, rc = 0);
787                 }
788
789                 rc = iops->rec(env, it,
790                                (struct dt_rec *)(entry + hashsize + keysize),
791                                attr);
792                 if (!rc) {
793                         if (hashsize)
794                                 memcpy(entry, &hash, hashsize);
795                         if (keysize) {
796                                 struct dt_key *key;
797
798                                 key = iops->key(env, it);
799                                 memcpy(entry + hashsize, key, keysize);
800                         }
801                         /* hash/key/record successfully copied! */
802                         lip->lip_nr++;
803                         if (unlikely(lip->lip_nr == 1 && ii->ii_count == 0))
804                                 ii->ii_hash_start = hash;
805                         entry += hashsize + keysize + recsize;
806                         bytes -= hashsize + keysize + recsize;
807                 } else if (rc != -ESTALE) {
808                         GOTO(out, rc);
809                 }
810
811                 /* move on to the next record */
812                 do {
813                         rc = iops->next(env, it);
814                 } while (rc == -ESTALE);
815         } while (rc == 0);
816
817         GOTO(out, rc);
818 out:
819         if (rc >= 0 && lip->lip_nr > 0)
820                 /* one more container */
821                 ii->ii_count++;
822         if (rc > 0)
823                 /* no more entries */
824                 ii->ii_hash_end = II_END_OFF;
825         return rc;
826 }
827
828
829 /*
830  * Walk index and fill lu_page containers with key/record pairs
831  *
832  * \param env - is the environment passed by the caller
833  * \param obj - is the index object to parse
834  * \param rdpg - is the lu_rdpg descriptor associated with the transfer
835  * \param filler - is the callback function responsible for filling a lu_page
836  *                 with key/record pairs in the format wanted by the caller.
837  *                 If NULL, uses dt_index_page_build
838  * \param arg    - is an opaq argument passed to the filler function
839  *
840  * \retval sum (in bytes) of all filled lu_pages
841  * \retval -ve errno on failure
842  */
843 int dt_index_walk(const struct lu_env *env, struct dt_object *obj,
844                   const struct lu_rdpg *rdpg, dt_index_page_build_t filler,
845                   void *arg)
846 {
847         struct dt_it *it;
848         const struct dt_it_ops *iops;
849         size_t pageidx, bytes, nlupgs = 0;
850         int rc;
851         ENTRY;
852
853         LASSERT(rdpg->rp_pages != NULL);
854         LASSERT(obj->do_index_ops != NULL);
855
856         if (filler == NULL)
857                 filler = dt_index_page_build;
858
859         bytes = rdpg->rp_count;
860         if (bytes == 0)
861                 RETURN(-EFAULT);
862
863         /* Iterate through index and fill containers from @rdpg */
864         iops = &obj->do_index_ops->dio_it;
865         LASSERT(iops != NULL);
866         it = iops->init(env, obj, rdpg->rp_attrs);
867         if (IS_ERR(it))
868                 RETURN(PTR_ERR(it));
869
870         rc = iops->load(env, it, rdpg->rp_hash);
871         if (rc == 0) {
872                 /*
873                  * Iterator didn't find record with exactly the key requested.
874                  *
875                  * It is currently either
876                  *
877                  *     - positioned above record with key less than
878                  *     requested---skip it.
879                  *     - or not positioned at all (is in IAM_IT_SKEWED
880                  *     state)---position it on the next item.
881                  */
882                 rc = iops->next(env, it);
883         } else if (rc > 0) {
884                 rc = 0;
885         } else {
886                 if (rc == -ENODATA)
887                         rc = 0;
888                 GOTO(out, rc);
889         }
890
891         /*
892          * Fill containers one after the other. There might be multiple
893          * containers per physical page.
894          *
895          * At this point and across for-loop:
896          *  rc == 0 -> ok, proceed.
897          *  rc >  0 -> end of index.
898          *  rc <  0 -> error.
899          */
900         for (pageidx = 0; rc == 0 && bytes > 0; pageidx++) {
901                 union lu_page   *lp;
902                 int              i;
903
904                 LASSERT(pageidx < rdpg->rp_npages);
905                 lp = kmap(rdpg->rp_pages[pageidx]);
906
907                 /* fill lu pages */
908                 for (i = 0; i < LU_PAGE_COUNT; i++, lp++, bytes-=LU_PAGE_SIZE) {
909                         rc = filler(env, obj, lp,
910                                     min_t(size_t, bytes, LU_PAGE_SIZE),
911                                     iops, it, rdpg->rp_attrs, arg);
912                         if (rc < 0)
913                                 break;
914                         /* one more lu_page */
915                         nlupgs++;
916                         if (rc > 0)
917                                 /* end of index */
918                                 break;
919                 }
920                 kunmap(rdpg->rp_pages[i]);
921         }
922
923 out:
924         iops->put(env, it);
925         iops->fini(env, it);
926
927         if (rc >= 0)
928                 rc = min_t(size_t, nlupgs * LU_PAGE_SIZE, rdpg->rp_count);
929
930         RETURN(rc);
931 }
932 EXPORT_SYMBOL(dt_index_walk);
933
934 /**
935  * Walk key/record pairs of an index and copy them into 4KB containers to be
936  * transferred over the network. This is the common handler for OBD_IDX_READ
937  * RPC processing.
938  *
939  * \param env - is the environment passed by the caller
940  * \param dev - is the dt_device storing the index
941  * \param ii  - is the idx_info structure packed by the client in the
942  *              OBD_IDX_READ request
943  * \param rdpg - is the lu_rdpg descriptor
944  *
945  * \retval on success, return sum (in bytes) of all filled containers
946  * \retval appropriate error otherwise.
947  */
948 int dt_index_read(const struct lu_env *env, struct dt_device *dev,
949                   struct idx_info *ii, const struct lu_rdpg *rdpg)
950 {
951         const struct dt_index_features  *feat;
952         struct dt_object                *obj;
953         int                              rc;
954         ENTRY;
955
956         /*
957          * rp_count shouldn't be null and should be a multiple of the container
958          * size
959          */
960         if (rdpg->rp_count == 0 || (rdpg->rp_count & (LU_PAGE_SIZE - 1)) != 0)
961                 RETURN(-EFAULT);
962
963         if (!fid_is_quota(&ii->ii_fid) && !fid_is_layout_rbtree(&ii->ii_fid) &&
964             !fid_is_norm(&ii->ii_fid))
965                 RETURN(-EOPNOTSUPP);
966
967         /* lookup index object subject to the transfer */
968         obj = dt_locate(env, dev, &ii->ii_fid);
969         if (IS_ERR(obj))
970                 RETURN(PTR_ERR(obj));
971         if (dt_object_exists(obj) == 0)
972                 GOTO(out, rc = -ENOENT);
973
974         /* fetch index features associated with index object */
975         feat = dt_index_feat_select(fid_seq(&ii->ii_fid),
976                                     lu_object_attr(&obj->do_lu));
977         if (IS_ERR(feat))
978                 GOTO(out, rc = PTR_ERR(feat));
979
980         /* load index feature if not done already */
981         if (obj->do_index_ops == NULL) {
982                 rc = obj->do_ops->do_index_try(env, obj, feat);
983                 if (rc)
984                         GOTO(out, rc);
985         }
986
987         /* fill ii_flags with supported index features */
988         ii->ii_flags &= (II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
989                          II_FL_VARREC);
990
991         if (!(feat->dif_flags & DT_IND_VARKEY))
992                 ii->ii_keysize = feat->dif_keysize_max;
993
994         if (!(feat->dif_flags & DT_IND_VARREC))
995                 ii->ii_recsize = feat->dif_recsize_max;
996
997         if (feat->dif_flags & DT_IND_NONUNQ)
998                 /* key isn't necessarily unique */
999                 ii->ii_flags |= II_FL_NONUNQ;
1000
1001         if (!fid_is_layout_rbtree(&ii->ii_fid)) {
1002                 dt_read_lock(env, obj, 0);
1003                 /* fetch object version before walking the index */
1004                 ii->ii_version = dt_version_get(env, obj);
1005         }
1006
1007         /* walk the index and fill lu_idxpages with key/record pairs */
1008         rc = dt_index_walk(env, obj, rdpg, dt_index_page_build, ii);
1009         if (!fid_is_layout_rbtree(&ii->ii_fid))
1010                 dt_read_unlock(env, obj);
1011
1012         if (rc == 0) {
1013                 /* index is empty */
1014                 LASSERT(ii->ii_count == 0);
1015                 ii->ii_hash_end = II_END_OFF;
1016         }
1017
1018         GOTO(out, rc);
1019 out:
1020         dt_object_put(env, obj);
1021         return rc;
1022 }
1023 EXPORT_SYMBOL(dt_index_read);
1024
1025 #ifdef CONFIG_PROC_FS
1026 int lprocfs_dt_blksize_seq_show(struct seq_file *m, void *v)
1027 {
1028         struct dt_device *dt = m->private;
1029         struct obd_statfs osfs;
1030
1031         int rc = dt_statfs(NULL, dt, &osfs);
1032         if (rc == 0)
1033                 seq_printf(m, "%u\n", (unsigned) osfs.os_bsize);
1034         return rc;
1035 }
1036 EXPORT_SYMBOL(lprocfs_dt_blksize_seq_show);
1037
1038 int lprocfs_dt_kbytestotal_seq_show(struct seq_file *m, void *v)
1039 {
1040         struct dt_device *dt = m->private;
1041         struct obd_statfs osfs;
1042
1043         int rc = dt_statfs(NULL, dt, &osfs);
1044         if (rc == 0) {
1045                 __u32 blk_size = osfs.os_bsize >> 10;
1046                 __u64 result = osfs.os_blocks;
1047
1048                 while (blk_size >>= 1)
1049                         result <<= 1;
1050
1051                 seq_printf(m, "%llu\n", result);
1052         }
1053         return rc;
1054 }
1055 EXPORT_SYMBOL(lprocfs_dt_kbytestotal_seq_show);
1056
1057 int lprocfs_dt_kbytesfree_seq_show(struct seq_file *m, void *v)
1058 {
1059         struct dt_device *dt = m->private;
1060         struct obd_statfs osfs;
1061
1062         int rc = dt_statfs(NULL, dt, &osfs);
1063         if (rc == 0) {
1064                 __u32 blk_size = osfs.os_bsize >> 10;
1065                 __u64 result = osfs.os_bfree;
1066
1067                 while (blk_size >>= 1)
1068                         result <<= 1;
1069
1070                 seq_printf(m, "%llu\n", result);
1071         }
1072         return rc;
1073 }
1074 EXPORT_SYMBOL(lprocfs_dt_kbytesfree_seq_show);
1075
1076 int lprocfs_dt_kbytesavail_seq_show(struct seq_file *m, void *v)
1077 {
1078         struct dt_device *dt = m->private;
1079         struct obd_statfs osfs;
1080
1081         int rc = dt_statfs(NULL, dt, &osfs);
1082         if (rc == 0) {
1083                 __u32 blk_size = osfs.os_bsize >> 10;
1084                 __u64 result = osfs.os_bavail;
1085
1086                 while (blk_size >>= 1)
1087                         result <<= 1;
1088
1089                 seq_printf(m, "%llu\n", result);
1090         }
1091         return rc;
1092 }
1093 EXPORT_SYMBOL(lprocfs_dt_kbytesavail_seq_show);
1094
1095 int lprocfs_dt_filestotal_seq_show(struct seq_file *m, void *v)
1096 {
1097         struct dt_device *dt = m->private;
1098         struct obd_statfs osfs;
1099
1100         int rc = dt_statfs(NULL, dt, &osfs);
1101         if (rc == 0)
1102                 seq_printf(m, "%llu\n", osfs.os_files);
1103         return rc;
1104 }
1105 EXPORT_SYMBOL(lprocfs_dt_filestotal_seq_show);
1106
1107 int lprocfs_dt_filesfree_seq_show(struct seq_file *m, void *v)
1108 {
1109         struct dt_device *dt = m->private;
1110         struct obd_statfs osfs;
1111
1112         int rc = dt_statfs(NULL, dt, &osfs);
1113         if (rc == 0)
1114                 seq_printf(m, "%llu\n", osfs.os_ffree);
1115         return rc;
1116 }
1117 EXPORT_SYMBOL(lprocfs_dt_filesfree_seq_show);
1118
1119 #endif /* CONFIG_PROC_FS */
1120
1121 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
1122                          char *buf)
1123 {
1124         struct dt_device *dt = container_of(kobj, struct dt_device,
1125                                             dd_kobj);
1126         struct lu_device *lu = dt2lu_dev(dt);
1127
1128         if (!lu->ld_obd)
1129                 return -ENODEV;
1130
1131         return sprintf(buf, "%s\n", lu->ld_obd->obd_uuid.uuid);
1132 }
1133 LUSTRE_RO_ATTR(uuid);
1134
1135 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
1136                               char *buf)
1137 {
1138         struct dt_device *dt = container_of(kobj, struct dt_device,
1139                                             dd_kobj);
1140         struct obd_statfs osfs;
1141         int rc;
1142
1143         rc = dt_statfs(NULL, dt, &osfs);
1144         if (rc)
1145                 return rc;
1146
1147         return sprintf(buf, "%u\n", (unsigned) osfs.os_bsize);
1148 }
1149 LUSTRE_RO_ATTR(blocksize);
1150
1151 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
1152                                 char *buf)
1153 {
1154         struct dt_device *dt = container_of(kobj, struct dt_device,
1155                                             dd_kobj);
1156         struct obd_statfs osfs;
1157         u32 blk_size;
1158         u64 result;
1159         int rc;
1160
1161         rc = dt_statfs(NULL, dt, &osfs);
1162         if (rc)
1163                 return rc;
1164
1165         blk_size = osfs.os_bsize >> 10;
1166         result = osfs.os_blocks;
1167
1168         while (blk_size >>= 1)
1169                 result <<= 1;
1170
1171         return sprintf(buf, "%llu\n", result);
1172 }
1173 LUSTRE_RO_ATTR(kbytestotal);
1174
1175 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
1176                                char *buf)
1177 {
1178         struct dt_device *dt = container_of(kobj, struct dt_device,
1179                                             dd_kobj);
1180         struct obd_statfs osfs;
1181         u32 blk_size;
1182         u64 result;
1183         int rc;
1184
1185         rc = dt_statfs(NULL, dt, &osfs);
1186         if (rc)
1187                 return rc;
1188
1189         blk_size = osfs.os_bsize >> 10;
1190         result = osfs.os_bfree;
1191
1192         while (blk_size >>= 1)
1193                 result <<= 1;
1194
1195         return sprintf(buf, "%llu\n", result);
1196 }
1197 LUSTRE_RO_ATTR(kbytesfree);
1198
1199 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
1200                                 char *buf)
1201 {
1202         struct dt_device *dt = container_of(kobj, struct dt_device,
1203                                             dd_kobj);
1204         struct obd_statfs osfs;
1205         u32 blk_size;
1206         u64 result;
1207         int rc;
1208
1209         rc = dt_statfs(NULL, dt, &osfs);
1210         if (rc)
1211                 return rc;
1212
1213         blk_size = osfs.os_bsize >> 10;
1214         result = osfs.os_bavail;
1215
1216         while (blk_size >>= 1)
1217                 result <<= 1;
1218
1219         return sprintf(buf, "%llu\n", result);
1220 }
1221 LUSTRE_RO_ATTR(kbytesavail);
1222
1223 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
1224                                char *buf)
1225 {
1226         struct dt_device *dt = container_of(kobj, struct dt_device,
1227                                             dd_kobj);
1228         struct obd_statfs osfs;
1229         int rc;
1230
1231         rc = dt_statfs(NULL, dt, &osfs);
1232         if (rc)
1233                 return rc;
1234
1235         return sprintf(buf, "%llu\n", osfs.os_files);
1236 }
1237 LUSTRE_RO_ATTR(filestotal);
1238
1239 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
1240                               char *buf)
1241 {
1242         struct dt_device *dt = container_of(kobj, struct dt_device,
1243                                             dd_kobj);
1244         struct obd_statfs osfs;
1245         int rc;
1246
1247         rc = dt_statfs(NULL, dt, &osfs);
1248         if (rc)
1249                 return rc;
1250
1251         return sprintf(buf, "%llu\n", osfs.os_ffree);
1252 }
1253 LUSTRE_RO_ATTR(filesfree);
1254
1255 static const struct attribute *dt_def_attrs[] = {
1256         &lustre_attr_uuid.attr,
1257         &lustre_attr_blocksize.attr,
1258         &lustre_attr_kbytestotal.attr,
1259         &lustre_attr_kbytesfree.attr,
1260         &lustre_attr_kbytesavail.attr,
1261         &lustre_attr_filestotal.attr,
1262         &lustre_attr_filesfree.attr,
1263         NULL,
1264 };
1265
1266 static void dt_sysfs_release(struct kobject *kobj)
1267 {
1268         struct dt_device *dt = container_of(kobj, struct dt_device,
1269                                             dd_kobj);
1270
1271         debugfs_remove_recursive(dt->dd_debugfs_entry);
1272         dt->dd_debugfs_entry = NULL;
1273
1274         complete(&dt->dd_kobj_unregister);
1275 }
1276
1277 int dt_tunables_fini(struct dt_device *dt)
1278 {
1279         if (!dt)
1280                 return -EINVAL;
1281
1282         if (dt->dd_def_attrs) {
1283                 sysfs_remove_files(&dt->dd_kobj, dt->dd_def_attrs);
1284                 kobject_put(&dt->dd_kobj);
1285                 wait_for_completion(&dt->dd_kobj_unregister);
1286         }
1287
1288         return 0;
1289 }
1290 EXPORT_SYMBOL(dt_tunables_fini);
1291
1292 int dt_tunables_init(struct dt_device *dt, struct obd_type *type,
1293                      const char *name, struct ldebugfs_vars *list)
1294 {
1295         int rc;
1296
1297         dt->dd_ktype.sysfs_ops = &lustre_sysfs_ops;
1298         dt->dd_ktype.release = dt_sysfs_release;
1299
1300         init_completion(&dt->dd_kobj_unregister);
1301         rc = kobject_init_and_add(&dt->dd_kobj, &dt->dd_ktype, &type->typ_kobj,
1302                                   "%s", name);
1303         if (rc)
1304                 return rc;
1305
1306         dt->dd_def_attrs = dt_def_attrs;
1307
1308         rc = sysfs_create_files(&dt->dd_kobj, dt->dd_def_attrs);
1309         if (rc) {
1310                 kobject_put(&dt->dd_kobj);
1311                 dt->dd_def_attrs = NULL;
1312                 return rc;
1313         }
1314
1315         /*
1316          * No need to register debugfs if no enteries. This allows us to
1317          * choose between using dt_device or obd_device for debugfs.
1318          */
1319         if (!list)
1320                 return rc;
1321
1322         dt->dd_debugfs_entry = debugfs_create_dir(name,
1323                                                  type->typ_debugfs_entry);
1324         ldebugfs_add_vars(dt->dd_debugfs_entry, list, dt);
1325
1326         return rc;
1327 }
1328 EXPORT_SYMBOL(dt_tunables_init);