Whamcloud - gitweb
LU-10877 lu: fix reference leak
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/dt_object.c
33  *
34  * Dt Object.
35  * Generic functions from dt_object.h
36  *
37  * Author: Nikita Danilov <nikita@clusterfs.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_CLASS
41
42 #include <linux/list.h>
43 #include <obd.h>
44 #include <dt_object.h>
45 /* fid_be_to_cpu() */
46 #include <lustre_fid.h>
47 #include <lustre_nodemap.h>
48 #include <lustre_quota.h>
49 #include <lustre_lfsck.h>
50
51 /* context key constructor/destructor: dt_global_key_init, dt_global_key_fini */
52 LU_KEY_INIT(dt_global, struct dt_thread_info);
53 LU_KEY_FINI(dt_global, struct dt_thread_info);
54
55 struct lu_context_key dt_key = {
56         .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD | LCT_MG_THREAD | LCT_LOCAL,
57         .lct_init = dt_global_key_init,
58         .lct_fini = dt_global_key_fini
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                 /* Usually dt_txn_hook_start is called from bottom device,
93                  * and if the thandle has th_top, then we need use top
94                  * thandle for the callback in the top thandle layer */
95                 if (th->th_top != NULL)
96                         dtc_th = th->th_top;
97
98                 rc = cb->dtc_txn_start(env, dtc_th, cb->dtc_cookie);
99                 if (rc < 0)
100                         break;
101         }
102         return rc;
103 }
104 EXPORT_SYMBOL(dt_txn_hook_start);
105
106 int dt_txn_hook_stop(const struct lu_env *env, struct thandle *th)
107 {
108         struct dt_device       *dev = th->th_dev;
109         struct dt_txn_callback *cb;
110         int                     rc = 0;
111
112         if (th->th_local)
113                 return 0;
114
115         if (OBD_FAIL_CHECK(OBD_FAIL_DT_TXN_STOP))
116                 return -EIO;
117
118         list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
119                 struct thandle *dtc_th = th;
120
121                 if (cb->dtc_txn_stop == NULL ||
122                     !(cb->dtc_tag & env->le_ctx.lc_tags))
123                         continue;
124
125                 /* Usually dt_txn_hook_stop is called from bottom device,
126                  * and if the thandle has th_top, then we need use top
127                  * thandle for the callback in the top thandle layer */
128                 if (th->th_top != NULL)
129                         dtc_th = th->th_top;
130
131                 rc = cb->dtc_txn_stop(env, dtc_th, cb->dtc_cookie);
132                 if (rc < 0)
133                         break;
134         }
135         return rc;
136 }
137 EXPORT_SYMBOL(dt_txn_hook_stop);
138
139 void dt_txn_hook_commit(struct thandle *th)
140 {
141         struct dt_txn_callback *cb;
142
143         if (th->th_local)
144                 return;
145
146         list_for_each_entry(cb, &th->th_dev->dd_txn_callbacks,
147                             dtc_linkage) {
148                 /* Right now, the bottom device (OSD) will use this hook
149                  * commit to notify OSP, so we do not check and replace
150                  * the thandle to top thandle now */
151                 if (cb->dtc_txn_commit)
152                         cb->dtc_txn_commit(th, cb->dtc_cookie);
153         }
154 }
155 EXPORT_SYMBOL(dt_txn_hook_commit);
156
157 int dt_device_init(struct dt_device *dev, struct lu_device_type *t)
158 {
159         INIT_LIST_HEAD(&dev->dd_txn_callbacks);
160         return lu_device_init(&dev->dd_lu_dev, t);
161 }
162 EXPORT_SYMBOL(dt_device_init);
163
164 void dt_device_fini(struct dt_device *dev)
165 {
166         lu_device_fini(&dev->dd_lu_dev);
167 }
168 EXPORT_SYMBOL(dt_device_fini);
169
170 int dt_object_init(struct dt_object *obj,
171                    struct lu_object_header *h, struct lu_device *d)
172
173 {
174         return lu_object_init(&obj->do_lu, h, d);
175 }
176 EXPORT_SYMBOL(dt_object_init);
177
178 void dt_object_fini(struct dt_object *obj)
179 {
180         lu_object_fini(&obj->do_lu);
181 }
182 EXPORT_SYMBOL(dt_object_fini);
183
184 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj)
185 {
186         if (obj->do_index_ops == NULL)
187                 obj->do_ops->do_index_try(env, obj, &dt_directory_features);
188         return obj->do_index_ops != NULL;
189 }
190 EXPORT_SYMBOL(dt_try_as_dir);
191
192 enum dt_format_type dt_mode_to_dft(__u32 mode)
193 {
194         enum dt_format_type result;
195
196         switch (mode & S_IFMT) {
197         case S_IFDIR:
198                 result = DFT_DIR;
199                 break;
200         case S_IFREG:
201                 result = DFT_REGULAR;
202                 break;
203         case S_IFLNK:
204                 result = DFT_SYM;
205                 break;
206         case S_IFCHR:
207         case S_IFBLK:
208         case S_IFIFO:
209         case S_IFSOCK:
210                 result = DFT_NODE;
211                 break;
212         default:
213                 LBUG();
214                 break;
215         }
216         return result;
217 }
218 EXPORT_SYMBOL(dt_mode_to_dft);
219
220 /**
221  * lookup fid for object named \a name in directory \a dir.
222  */
223
224 int dt_lookup_dir(const struct lu_env *env, struct dt_object *dir,
225                   const char *name, struct lu_fid *fid)
226 {
227         if (dt_try_as_dir(env, dir))
228                 return dt_lookup(env, dir, (struct dt_rec *)fid,
229                                  (const struct dt_key *)name);
230         return -ENOTDIR;
231 }
232 EXPORT_SYMBOL(dt_lookup_dir);
233
234 /* this differs from dt_locate by top_dev as parameter
235  * but not one from lu_site */
236 struct dt_object *dt_locate_at(const struct lu_env *env,
237                                struct dt_device *dev,
238                                const struct lu_fid *fid,
239                                struct lu_device *top_dev,
240                                const struct lu_object_conf *conf)
241 {
242         struct lu_object *lo;
243         struct lu_object *n;
244
245         lo = lu_object_find_at(env, top_dev, fid, conf);
246         if (IS_ERR(lo))
247                 return ERR_PTR(PTR_ERR(lo));
248
249         LASSERT(lo != NULL);
250
251         list_for_each_entry(n, &lo->lo_header->loh_layers, lo_linkage) {
252                 if (n->lo_dev == &dev->dd_lu_dev)
253                         return container_of0(n, struct dt_object, do_lu);
254         }
255
256         lu_object_put(env, lo);
257         return ERR_PTR(-ENOENT);
258 }
259 EXPORT_SYMBOL(dt_locate_at);
260
261 /**
262  * find an object named \a entry in given \a dfh->dfh_o directory.
263  */
264 static int dt_find_entry(const struct lu_env *env, const char *entry,
265                          void *data)
266 {
267         struct dt_find_hint *dfh = data;
268         struct dt_device *dt = dfh->dfh_dt;
269         struct lu_fid *fid = dfh->dfh_fid;
270         struct dt_object *obj = dfh->dfh_o;
271         int rc;
272
273         rc = dt_lookup_dir(env, obj, entry, fid);
274         dt_object_put(env, obj);
275         if (rc == 0) {
276                 obj = dt_locate(env, dt, fid);
277                 if (IS_ERR(obj))
278                         rc = PTR_ERR(obj);
279         }
280         dfh->dfh_o = obj;
281
282         return rc;
283 }
284
285 /**
286  * Abstract function which parses path name. This function feeds
287  * path component to \a entry_func.
288  */
289 int dt_path_parser(const struct lu_env *env,
290                    char *path, dt_entry_func_t entry_func,
291                    void *data)
292 {
293         char *e;
294         int rc = 0;
295
296         while (1) {
297                 e = strsep(&path, "/");
298                 if (e == NULL)
299                         break;
300
301                 if (e[0] == 0) {
302                         if (!path || path[0] == '\0')
303                                 break;
304                         continue;
305                 }
306                 rc = entry_func(env, e, data);
307                 if (rc)
308                         break;
309         }
310
311         return rc;
312 }
313
314 struct dt_object *
315 dt_store_resolve(const struct lu_env *env, struct dt_device *dt,
316                  const char *path, struct lu_fid *fid)
317 {
318         struct dt_thread_info *info = dt_info(env);
319         struct dt_find_hint   *dfh = &info->dti_dfh;
320         struct dt_object      *obj;
321         int                    result;
322
323
324         dfh->dfh_dt = dt;
325         dfh->dfh_fid = fid;
326
327         strlcpy(info->dti_buf, path, sizeof(info->dti_buf));
328
329         result = dt->dd_ops->dt_root_get(env, dt, fid);
330         if (result == 0) {
331                 obj = dt_locate(env, dt, fid);
332                 if (!IS_ERR(obj)) {
333                         dfh->dfh_o = obj;
334                         result = dt_path_parser(env, info->dti_buf,
335                                                 dt_find_entry, dfh);
336                         if (result != 0)
337                                 obj = ERR_PTR(result);
338                         else
339                                 obj = dfh->dfh_o;
340                 }
341         } else {
342                 obj = ERR_PTR(result);
343         }
344         return obj;
345 }
346
347 static struct dt_object *dt_reg_open(const struct lu_env *env,
348                                      struct dt_device *dt,
349                                      struct dt_object *p,
350                                      const char *name,
351                                      struct lu_fid *fid)
352 {
353         struct dt_object *o;
354         int result;
355
356         result = dt_lookup_dir(env, p, name, fid);
357         if (result == 0){
358                 o = dt_locate(env, dt, fid);
359         }
360         else
361                 o = ERR_PTR(result);
362
363         return o;
364 }
365
366 /**
367  * Open dt object named \a filename from \a dirname directory.
368  *      \param  dt      dt device
369  *      \param  fid     on success, object fid is stored in *fid
370  */
371 struct dt_object *dt_store_open(const struct lu_env *env, struct dt_device *dt,
372                                 const char *dirname, const char *filename,
373                                 struct lu_fid *fid)
374 {
375         struct dt_object *file;
376         struct dt_object *dir;
377
378         dir = dt_store_resolve(env, dt, dirname, fid);
379         if (!IS_ERR(dir)) {
380                 file = dt_reg_open(env, dt, dir, filename, fid);
381                 dt_object_put(env, dir);
382         } else {
383                 file = dir;
384         }
385
386         return file;
387 }
388
389 struct dt_object *dt_find_or_create(const struct lu_env *env,
390                                     struct dt_device *dt,
391                                     const struct lu_fid *fid,
392                                     struct dt_object_format *dof,
393                                     struct lu_attr *at)
394 {
395         struct dt_object *dto;
396         struct thandle *th;
397         int rc;
398
399         ENTRY;
400
401         dto = dt_locate(env, dt, fid);
402         if (IS_ERR(dto))
403                 RETURN(dto);
404
405         LASSERT(dto != NULL);
406         if (dt_object_exists(dto))
407                 RETURN(dto);
408
409         th = dt_trans_create(env, dt);
410         if (IS_ERR(th))
411                 GOTO(out, rc = PTR_ERR(th));
412
413         rc = dt_declare_create(env, dto, at, NULL, dof, th);
414         if (rc)
415                 GOTO(trans_stop, rc);
416
417         rc = dt_trans_start_local(env, dt, th);
418         if (rc)
419                 GOTO(trans_stop, rc);
420
421         dt_write_lock(env, dto, 0);
422         if (dt_object_exists(dto))
423                 GOTO(unlock, rc = 0);
424
425         CDEBUG(D_OTHER, "create new object "DFID"\n", PFID(fid));
426
427         rc = dt_create(env, dto, at, NULL, dof, th);
428         if (rc)
429                 GOTO(unlock, rc);
430         LASSERT(dt_object_exists(dto));
431 unlock:
432         dt_write_unlock(env, dto);
433 trans_stop:
434         dt_trans_stop(env, dt, th);
435 out:
436         if (rc) {
437                 dt_object_put(env, dto);
438                 dto = ERR_PTR(rc);
439         }
440
441         RETURN(dto);
442 }
443 EXPORT_SYMBOL(dt_find_or_create);
444
445 /* dt class init function. */
446 int dt_global_init(void)
447 {
448         int result;
449
450         LU_CONTEXT_KEY_INIT(&dt_key);
451         result = lu_context_key_register(&dt_key);
452         return result;
453 }
454
455 void dt_global_fini(void)
456 {
457         lu_context_key_degister(&dt_key);
458 }
459
460 /**
461  * Generic read helper. May return an error for partial reads.
462  *
463  * \param env  lustre environment
464  * \param dt   object to be read
465  * \param buf  lu_buf to be filled, with buffer pointer and length
466  * \param pos position to start reading, updated as data is read
467  *
468  * \retval real size of data read
469  * \retval -ve errno on failure
470  */
471 int dt_read(const struct lu_env *env, struct dt_object *dt,
472             struct lu_buf *buf, loff_t *pos)
473 {
474         LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
475         return dt->do_body_ops->dbo_read(env, dt, buf, pos);
476 }
477 EXPORT_SYMBOL(dt_read);
478
479 /**
480  * Read structures of fixed size from storage.  Unlike dt_read(), using
481  * dt_record_read() will return an error for partial reads.
482  *
483  * \param env  lustre environment
484  * \param dt   object to be read
485  * \param buf  lu_buf to be filled, with buffer pointer and length
486  * \param pos position to start reading, updated as data is read
487  *
488  * \retval 0 on successfully reading full buffer
489  * \retval -EFAULT on short read
490  * \retval -ve errno on failure
491  */
492 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
493                    struct lu_buf *buf, loff_t *pos)
494 {
495         ssize_t size;
496
497         LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
498
499         size = dt->do_body_ops->dbo_read(env, dt, buf, pos);
500         if (size < 0)
501                 return size;
502         return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
503 }
504 EXPORT_SYMBOL(dt_record_read);
505
506 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
507                     const struct lu_buf *buf, loff_t *pos, struct thandle *th)
508 {
509         ssize_t size;
510
511         LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
512         LASSERT(th != NULL);
513         LASSERT(dt->do_body_ops);
514         LASSERT(dt->do_body_ops->dbo_write);
515
516         size = dt->do_body_ops->dbo_write(env, dt, buf, pos, th, 1);
517         if (size < 0)
518                 return size;
519         return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
520 }
521 EXPORT_SYMBOL(dt_record_write);
522
523 int dt_declare_version_set(const struct lu_env *env, struct dt_object *o,
524                            struct thandle *th)
525 {
526         struct lu_buf vbuf;
527         char *xname = XATTR_NAME_VERSION;
528
529         LASSERT(o);
530         vbuf.lb_buf = NULL;
531         vbuf.lb_len = sizeof(dt_obj_version_t);
532         return dt_declare_xattr_set(env, o, &vbuf, xname, 0, th);
533
534 }
535 EXPORT_SYMBOL(dt_declare_version_set);
536
537 void dt_version_set(const struct lu_env *env, struct dt_object *o,
538                     dt_obj_version_t version, struct thandle *th)
539 {
540         struct lu_buf vbuf;
541         char *xname = XATTR_NAME_VERSION;
542         int rc;
543
544         LASSERT(o);
545         vbuf.lb_buf = &version;
546         vbuf.lb_len = sizeof(version);
547
548         rc = dt_xattr_set(env, o, &vbuf, xname, 0, th);
549         if (rc < 0)
550                 CDEBUG(D_INODE, "Can't set version, rc %d\n", rc);
551         return;
552 }
553 EXPORT_SYMBOL(dt_version_set);
554
555 dt_obj_version_t dt_version_get(const struct lu_env *env, struct dt_object *o)
556 {
557         struct lu_buf vbuf;
558         char *xname = XATTR_NAME_VERSION;
559         dt_obj_version_t version;
560         int rc;
561
562         LASSERT(o);
563         vbuf.lb_buf = &version;
564         vbuf.lb_len = sizeof(version);
565         rc = dt_xattr_get(env, o, &vbuf, xname);
566         if (rc != sizeof(version)) {
567                 CDEBUG(D_INODE, "Can't get version, rc %d\n", rc);
568                 version = 0;
569         }
570         return version;
571 }
572 EXPORT_SYMBOL(dt_version_get);
573
574 /* list of all supported index types */
575
576 /* directories */
577 const struct dt_index_features dt_directory_features;
578 EXPORT_SYMBOL(dt_directory_features);
579
580 /* scrub iterator */
581 const struct dt_index_features dt_otable_features;
582 EXPORT_SYMBOL(dt_otable_features);
583
584 /* lfsck layout orphan */
585 const struct dt_index_features dt_lfsck_layout_orphan_features = {
586         .dif_flags              = 0,
587         .dif_keysize_min        = sizeof(struct lu_fid),
588         .dif_keysize_max        = sizeof(struct lu_fid),
589         .dif_recsize_min        = sizeof(struct lu_orphan_rec_v2),
590         .dif_recsize_max        = sizeof(struct lu_orphan_rec_v2),
591         .dif_ptrsize            = 4
592 };
593 EXPORT_SYMBOL(dt_lfsck_layout_orphan_features);
594
595 /* lfsck layout dangling */
596 const struct dt_index_features dt_lfsck_layout_dangling_features = {
597         .dif_flags              = DT_IND_UPDATE,
598         .dif_keysize_min        = sizeof(struct lfsck_layout_dangling_key),
599         .dif_keysize_max        = sizeof(struct lfsck_layout_dangling_key),
600         .dif_recsize_min        = sizeof(struct lu_fid),
601         .dif_recsize_max        = sizeof(struct lu_fid),
602         .dif_ptrsize            = 4
603 };
604 EXPORT_SYMBOL(dt_lfsck_layout_dangling_features);
605
606 /* lfsck namespace */
607 const struct dt_index_features dt_lfsck_namespace_features = {
608         .dif_flags              = DT_IND_UPDATE,
609         .dif_keysize_min        = sizeof(struct lu_fid),
610         .dif_keysize_max        = sizeof(struct lu_fid),
611         .dif_recsize_min        = sizeof(__u8),
612         .dif_recsize_max        = sizeof(__u8),
613         .dif_ptrsize            = 4
614 };
615 EXPORT_SYMBOL(dt_lfsck_namespace_features);
616
617 /* accounting indexes */
618 const struct dt_index_features dt_acct_features = {
619         .dif_flags              = DT_IND_UPDATE,
620         .dif_keysize_min        = sizeof(__u64), /* 64-bit uid/gid */
621         .dif_keysize_max        = sizeof(__u64), /* 64-bit uid/gid */
622         .dif_recsize_min        = sizeof(struct lquota_acct_rec), /* 16 bytes */
623         .dif_recsize_max        = sizeof(struct lquota_acct_rec), /* 16 bytes */
624         .dif_ptrsize            = 4
625 };
626 EXPORT_SYMBOL(dt_acct_features);
627
628 /* global quota files */
629 const struct dt_index_features dt_quota_glb_features = {
630         .dif_flags              = DT_IND_UPDATE,
631         /* a different key would have to be used for per-directory quota */
632         .dif_keysize_min        = sizeof(__u64), /* 64-bit uid/gid */
633         .dif_keysize_max        = sizeof(__u64), /* 64-bit uid/gid */
634         .dif_recsize_min        = sizeof(struct lquota_glb_rec), /* 32 bytes */
635         .dif_recsize_max        = sizeof(struct lquota_glb_rec), /* 32 bytes */
636         .dif_ptrsize            = 4
637 };
638 EXPORT_SYMBOL(dt_quota_glb_features);
639
640 /* slave quota files */
641 const struct dt_index_features dt_quota_slv_features = {
642         .dif_flags              = DT_IND_UPDATE,
643         /* a different key would have to be used for per-directory quota */
644         .dif_keysize_min        = sizeof(__u64), /* 64-bit uid/gid */
645         .dif_keysize_max        = sizeof(__u64), /* 64-bit uid/gid */
646         .dif_recsize_min        = sizeof(struct lquota_slv_rec), /* 8 bytes */
647         .dif_recsize_max        = sizeof(struct lquota_slv_rec), /* 8 bytes */
648         .dif_ptrsize            = 4
649 };
650 EXPORT_SYMBOL(dt_quota_slv_features);
651
652 /* nodemap files, nodemap_rec size asserted in nodemap_storage.c */
653 const struct dt_index_features dt_nodemap_features = {
654         .dif_flags              = DT_IND_UPDATE,
655         .dif_keysize_min        = sizeof(__u64), /* 64-bit nodemap/record id */
656         .dif_keysize_max        = sizeof(__u64), /* 64-bit nodemap/record id */
657         .dif_recsize_min        = sizeof(union nodemap_rec), /* 32 bytes */
658         .dif_recsize_max        = sizeof(union nodemap_rec), /* 32 bytes */
659         .dif_ptrsize            = 4
660 };
661 EXPORT_SYMBOL(dt_nodemap_features);
662
663 /* helper function returning what dt_index_features structure should be used
664  * based on the FID sequence. This is used by OBD_IDX_READ RPC */
665 static inline const struct dt_index_features *dt_index_feat_select(__u64 seq,
666                                                                    __u32 mode)
667 {
668         if (seq == FID_SEQ_QUOTA_GLB) {
669                 /* global quota index */
670                 if (!S_ISREG(mode))
671                         /* global quota index should be a regular file */
672                         return ERR_PTR(-ENOENT);
673                 return &dt_quota_glb_features;
674         } else if (seq == FID_SEQ_QUOTA) {
675                 /* quota slave index */
676                 if (!S_ISREG(mode))
677                         /* slave index should be a regular file */
678                         return ERR_PTR(-ENOENT);
679                 return &dt_quota_slv_features;
680         } else if (seq == FID_SEQ_LAYOUT_RBTREE){
681                 return &dt_lfsck_layout_orphan_features;
682         } else if (seq >= FID_SEQ_NORMAL) {
683                 /* object is part of the namespace, verify that it is a
684                  * directory */
685                 if (!S_ISDIR(mode))
686                         /* sorry, we can only deal with directory */
687                         return ERR_PTR(-ENOTDIR);
688                 return &dt_directory_features;
689         }
690
691         return ERR_PTR(-EOPNOTSUPP);
692 }
693
694 /*
695  * Fill a lu_idxpage with key/record pairs read for transfer via OBD_IDX_READ
696  * RPC
697  *
698  * \param env - is the environment passed by the caller
699  * \param lp  - is a pointer to the lu_page to fill
700  * \param nob - is the maximum number of bytes that should be copied
701  * \param iops - is the index operation vector associated with the index object
702  * \param it   - is a pointer to the current iterator
703  * \param attr - is the index attribute to pass to iops->rec()
704  * \param arg  - is a pointer to the idx_info structure
705  */
706 static int dt_index_page_build(const struct lu_env *env, union lu_page *lp,
707                                size_t nob, const struct dt_it_ops *iops,
708                                struct dt_it *it, __u32 attr, void *arg)
709 {
710         struct idx_info         *ii = (struct idx_info *)arg;
711         struct lu_idxpage       *lip = &lp->lp_idx;
712         char                    *entry;
713         size_t                   size;
714         int                      rc;
715         ENTRY;
716
717         if (nob < LIP_HDR_SIZE)
718                 return -EINVAL;
719
720         /* initialize the header of the new container */
721         memset(lip, 0, LIP_HDR_SIZE);
722         lip->lip_magic = LIP_MAGIC;
723         nob           -= LIP_HDR_SIZE;
724
725         /* compute size needed to store a key/record pair */
726         size = ii->ii_recsize + ii->ii_keysize;
727         if ((ii->ii_flags & II_FL_NOHASH) == 0)
728                 /* add hash if the client wants it */
729                 size += sizeof(__u64);
730
731         entry = lip->lip_entries;
732         do {
733                 char            *tmp_entry = entry;
734                 struct dt_key   *key;
735                 __u64           hash;
736                 __u16           keysize;
737                 __u16           recsize;
738
739                 /* fetch 64-bit hash value */
740                 hash = iops->store(env, it);
741                 ii->ii_hash_end = hash;
742
743                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_IDX_READ_BREAK)) {
744                         if (lip->lip_nr != 0)
745                                 GOTO(out, rc = 0);
746                 }
747
748                 if (nob < size) {
749                         if (lip->lip_nr == 0)
750                                 GOTO(out, rc = -EINVAL);
751                         GOTO(out, rc = 0);
752                 }
753
754                 if (!(ii->ii_flags & II_FL_NOHASH)) {
755                         /* client wants to the 64-bit hash value associated with
756                          * each record */
757                         memcpy(tmp_entry, &hash, sizeof(hash));
758                         tmp_entry += sizeof(hash);
759                 }
760
761                 if (ii->ii_flags & II_FL_VARKEY)
762                         keysize = iops->key_size(env, it);
763                 else
764                         keysize = ii->ii_keysize;
765
766                 if (!(ii->ii_flags & II_FL_NOKEY)) {
767                         /* then the key value */
768                         key = iops->key(env, it);
769                         memcpy(tmp_entry, key, keysize);
770                         tmp_entry += keysize;
771                 }
772
773                 /* and finally the record */
774                 rc = iops->rec(env, it, (struct dt_rec *)tmp_entry, attr);
775                 if (rc != -ESTALE) {
776                         if (rc != 0)
777                                 GOTO(out, rc);
778
779                         /* hash/key/record successfully copied! */
780                         lip->lip_nr++;
781                         if (unlikely(lip->lip_nr == 1 && ii->ii_count == 0))
782                                 ii->ii_hash_start = hash;
783
784                         if (ii->ii_flags & II_FL_VARREC)
785                                 recsize = iops->rec_size(env, it, attr);
786                         else
787                                 recsize = ii->ii_recsize;
788
789                         entry = tmp_entry + recsize;
790                         nob -= size;
791                 }
792
793                 /* move on to the next record */
794                 do {
795                         rc = iops->next(env, it);
796                 } while (rc == -ESTALE);
797
798         } while (rc == 0);
799
800         GOTO(out, rc);
801 out:
802         if (rc >= 0 && lip->lip_nr > 0)
803                 /* one more container */
804                 ii->ii_count++;
805         if (rc > 0)
806                 /* no more entries */
807                 ii->ii_hash_end = II_END_OFF;
808         return rc;
809 }
810
811
812 /*
813  * Walk index and fill lu_page containers with key/record pairs
814  *
815  * \param env - is the environment passed by the caller
816  * \param obj - is the index object to parse
817  * \param rdpg - is the lu_rdpg descriptor associated with the transfer
818  * \param filler - is the callback function responsible for filling a lu_page
819  *                 with key/record pairs in the format wanted by the caller.
820  *                 If NULL, uses dt_index_page_build
821  * \param arg    - is an opaq argument passed to the filler function
822  *
823  * \retval sum (in bytes) of all filled lu_pages
824  * \retval -ve errno on failure
825  */
826 int dt_index_walk(const struct lu_env *env, struct dt_object *obj,
827                   const struct lu_rdpg *rdpg, dt_index_page_build_t filler,
828                   void *arg)
829 {
830         struct dt_it            *it;
831         const struct dt_it_ops  *iops;
832         size_t                   pageidx, nob, nlupgs = 0;
833         int                      rc;
834         ENTRY;
835
836         LASSERT(rdpg->rp_pages != NULL);
837         LASSERT(obj->do_index_ops != NULL);
838
839         if (filler == NULL)
840                 filler = dt_index_page_build;
841
842         nob = rdpg->rp_count;
843         if (nob == 0)
844                 RETURN(-EFAULT);
845
846         /* Iterate through index and fill containers from @rdpg */
847         iops = &obj->do_index_ops->dio_it;
848         LASSERT(iops != NULL);
849         it = iops->init(env, obj, rdpg->rp_attrs);
850         if (IS_ERR(it))
851                 RETURN(PTR_ERR(it));
852
853         rc = iops->load(env, it, rdpg->rp_hash);
854         if (rc == 0) {
855                 /*
856                  * Iterator didn't find record with exactly the key requested.
857                  *
858                  * It is currently either
859                  *
860                  *     - positioned above record with key less than
861                  *     requested---skip it.
862                  *     - or not positioned at all (is in IAM_IT_SKEWED
863                  *     state)---position it on the next item.
864                  */
865                 rc = iops->next(env, it);
866         } else if (rc > 0) {
867                 rc = 0;
868         } else {
869                 if (rc == -ENODATA)
870                         rc = 0;
871                 GOTO(out, rc);
872         }
873
874         /* Fill containers one after the other. There might be multiple
875          * containers per physical page.
876          *
877          * At this point and across for-loop:
878          *  rc == 0 -> ok, proceed.
879          *  rc >  0 -> end of index.
880          *  rc <  0 -> error. */
881         for (pageidx = 0; rc == 0 && nob > 0; pageidx++) {
882                 union lu_page   *lp;
883                 int              i;
884
885                 LASSERT(pageidx < rdpg->rp_npages);
886                 lp = kmap(rdpg->rp_pages[pageidx]);
887
888                 /* fill lu pages */
889                 for (i = 0; i < LU_PAGE_COUNT; i++, lp++, nob -= LU_PAGE_SIZE) {
890                         rc = filler(env, lp, min_t(size_t, nob, LU_PAGE_SIZE),
891                                     iops, it, rdpg->rp_attrs, arg);
892                         if (rc < 0)
893                                 break;
894                         /* one more lu_page */
895                         nlupgs++;
896                         if (rc > 0)
897                                 /* end of index */
898                                 break;
899                 }
900                 kunmap(rdpg->rp_pages[i]);
901         }
902
903 out:
904         iops->put(env, it);
905         iops->fini(env, it);
906
907         if (rc >= 0)
908                 rc = min_t(size_t, nlupgs * LU_PAGE_SIZE, rdpg->rp_count);
909
910         RETURN(rc);
911 }
912 EXPORT_SYMBOL(dt_index_walk);
913
914 /**
915  * Walk key/record pairs of an index and copy them into 4KB containers to be
916  * transferred over the network. This is the common handler for OBD_IDX_READ
917  * RPC processing.
918  *
919  * \param env - is the environment passed by the caller
920  * \param dev - is the dt_device storing the index
921  * \param ii  - is the idx_info structure packed by the client in the
922  *              OBD_IDX_READ request
923  * \param rdpg - is the lu_rdpg descriptor
924  *
925  * \retval on success, return sum (in bytes) of all filled containers
926  * \retval appropriate error otherwise.
927  */
928 int dt_index_read(const struct lu_env *env, struct dt_device *dev,
929                   struct idx_info *ii, const struct lu_rdpg *rdpg)
930 {
931         const struct dt_index_features  *feat;
932         struct dt_object                *obj;
933         int                              rc;
934         ENTRY;
935
936         /* rp_count shouldn't be null and should be a multiple of the container
937          * size */
938         if (rdpg->rp_count == 0 || (rdpg->rp_count & (LU_PAGE_SIZE - 1)) != 0)
939                 RETURN(-EFAULT);
940
941         if (!fid_is_quota(&ii->ii_fid) && !fid_is_layout_rbtree(&ii->ii_fid) &&
942             !fid_is_norm(&ii->ii_fid))
943                 RETURN(-EOPNOTSUPP);
944
945         /* lookup index object subject to the transfer */
946         obj = dt_locate(env, dev, &ii->ii_fid);
947         if (IS_ERR(obj))
948                 RETURN(PTR_ERR(obj));
949         if (dt_object_exists(obj) == 0)
950                 GOTO(out, rc = -ENOENT);
951
952         /* fetch index features associated with index object */
953         feat = dt_index_feat_select(fid_seq(&ii->ii_fid),
954                                     lu_object_attr(&obj->do_lu));
955         if (IS_ERR(feat))
956                 GOTO(out, rc = PTR_ERR(feat));
957
958         /* load index feature if not done already */
959         if (obj->do_index_ops == NULL) {
960                 rc = obj->do_ops->do_index_try(env, obj, feat);
961                 if (rc)
962                         GOTO(out, rc);
963         }
964
965         /* fill ii_flags with supported index features */
966         ii->ii_flags &= (II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
967                          II_FL_VARREC);
968
969         if (!(feat->dif_flags & DT_IND_VARKEY))
970                 ii->ii_keysize = feat->dif_keysize_max;
971
972         if (!(feat->dif_flags & DT_IND_VARREC))
973                 ii->ii_recsize = feat->dif_recsize_max;
974
975         if (feat->dif_flags & DT_IND_NONUNQ)
976                 /* key isn't necessarily unique */
977                 ii->ii_flags |= II_FL_NONUNQ;
978
979         if (!fid_is_layout_rbtree(&ii->ii_fid)) {
980                 dt_read_lock(env, obj, 0);
981                 /* fetch object version before walking the index */
982                 ii->ii_version = dt_version_get(env, obj);
983         }
984
985         /* walk the index and fill lu_idxpages with key/record pairs */
986         rc = dt_index_walk(env, obj, rdpg, dt_index_page_build, ii);
987         if (!fid_is_layout_rbtree(&ii->ii_fid))
988                 dt_read_unlock(env, obj);
989
990         if (rc == 0) {
991                 /* index is empty */
992                 LASSERT(ii->ii_count == 0);
993                 ii->ii_hash_end = II_END_OFF;
994         }
995
996         GOTO(out, rc);
997 out:
998         dt_object_put(env, obj);
999         return rc;
1000 }
1001 EXPORT_SYMBOL(dt_index_read);
1002
1003 #ifdef CONFIG_PROC_FS
1004 int lprocfs_dt_blksize_seq_show(struct seq_file *m, void *v)
1005 {
1006         struct dt_device *dt = m->private;
1007         struct obd_statfs osfs;
1008
1009         int rc = dt_statfs(NULL, dt, &osfs);
1010         if (rc == 0)
1011                 seq_printf(m, "%u\n", (unsigned) osfs.os_bsize);
1012         return rc;
1013 }
1014 EXPORT_SYMBOL(lprocfs_dt_blksize_seq_show);
1015
1016 int lprocfs_dt_kbytestotal_seq_show(struct seq_file *m, void *v)
1017 {
1018         struct dt_device *dt = m->private;
1019         struct obd_statfs osfs;
1020
1021         int rc = dt_statfs(NULL, dt, &osfs);
1022         if (rc == 0) {
1023                 __u32 blk_size = osfs.os_bsize >> 10;
1024                 __u64 result = osfs.os_blocks;
1025
1026                 while (blk_size >>= 1)
1027                         result <<= 1;
1028
1029                 seq_printf(m, "%llu\n", result);
1030         }
1031         return rc;
1032 }
1033 EXPORT_SYMBOL(lprocfs_dt_kbytestotal_seq_show);
1034
1035 int lprocfs_dt_kbytesfree_seq_show(struct seq_file *m, void *v)
1036 {
1037         struct dt_device *dt = m->private;
1038         struct obd_statfs osfs;
1039
1040         int rc = dt_statfs(NULL, dt, &osfs);
1041         if (rc == 0) {
1042                 __u32 blk_size = osfs.os_bsize >> 10;
1043                 __u64 result = osfs.os_bfree;
1044
1045                 while (blk_size >>= 1)
1046                         result <<= 1;
1047
1048                 seq_printf(m, "%llu\n", result);
1049         }
1050         return rc;
1051 }
1052 EXPORT_SYMBOL(lprocfs_dt_kbytesfree_seq_show);
1053
1054 int lprocfs_dt_kbytesavail_seq_show(struct seq_file *m, void *v)
1055 {
1056         struct dt_device *dt = m->private;
1057         struct obd_statfs osfs;
1058
1059         int rc = dt_statfs(NULL, dt, &osfs);
1060         if (rc == 0) {
1061                 __u32 blk_size = osfs.os_bsize >> 10;
1062                 __u64 result = osfs.os_bavail;
1063
1064                 while (blk_size >>= 1)
1065                         result <<= 1;
1066
1067                 seq_printf(m, "%llu\n", result);
1068         }
1069         return rc;
1070 }
1071 EXPORT_SYMBOL(lprocfs_dt_kbytesavail_seq_show);
1072
1073 int lprocfs_dt_filestotal_seq_show(struct seq_file *m, void *v)
1074 {
1075         struct dt_device *dt = m->private;
1076         struct obd_statfs osfs;
1077
1078         int rc = dt_statfs(NULL, dt, &osfs);
1079         if (rc == 0)
1080                 seq_printf(m, "%llu\n", osfs.os_files);
1081         return rc;
1082 }
1083 EXPORT_SYMBOL(lprocfs_dt_filestotal_seq_show);
1084
1085 int lprocfs_dt_filesfree_seq_show(struct seq_file *m, void *v)
1086 {
1087         struct dt_device *dt = m->private;
1088         struct obd_statfs osfs;
1089
1090         int rc = dt_statfs(NULL, dt, &osfs);
1091         if (rc == 0)
1092                 seq_printf(m, "%llu\n", osfs.os_ffree);
1093         return rc;
1094 }
1095 EXPORT_SYMBOL(lprocfs_dt_filesfree_seq_show);
1096
1097 #endif /* CONFIG_PROC_FS */