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