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