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