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