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