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