Whamcloud - gitweb
5cea5a64745fb3bb079734aef8663cdfe52c6d73
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/dt_object.c
33  *
34  * Dt Object.
35  * Generic functions from dt_object.h
36  *
37  * Author: Nikita Danilov <nikita@clusterfs.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_CLASS
41
42 #include <linux/list.h>
43 #include <obd_class.h>
44 #include <dt_object.h>
45 /* fid_be_to_cpu() */
46 #include <lustre_fid.h>
47 #include <lustre_nodemap.h>
48 #include <lustre_quota.h>
49 #include <lustre_lfsck.h>
50
51 /* context key constructor/destructor: dt_global_key_init, dt_global_key_fini */
52 LU_KEY_INIT(dt_global, struct dt_thread_info);
53 LU_KEY_FINI(dt_global, struct dt_thread_info);
54
55 struct lu_context_key dt_key = {
56         .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD | LCT_MG_THREAD | LCT_LOCAL,
57         .lct_init = dt_global_key_init,
58         .lct_fini = dt_global_key_fini
59 };
60
61 /*
62  * no lock is necessary to protect the list, because call-backs
63  * are added during system startup. Please refer to "struct dt_device".
64  */
65 void dt_txn_callback_add(struct dt_device *dev, struct dt_txn_callback *cb)
66 {
67         list_add(&cb->dtc_linkage, &dev->dd_txn_callbacks);
68 }
69 EXPORT_SYMBOL(dt_txn_callback_add);
70
71 void dt_txn_callback_del(struct dt_device *dev, struct dt_txn_callback *cb)
72 {
73         list_del_init(&cb->dtc_linkage);
74 }
75 EXPORT_SYMBOL(dt_txn_callback_del);
76
77 int dt_txn_hook_start(const struct lu_env *env,
78                       struct dt_device *dev, struct thandle *th)
79 {
80         int rc = 0;
81         struct dt_txn_callback *cb;
82
83         if (th->th_local)
84                 return 0;
85
86         list_for_each_entry(cb, &dev->dd_txn_callbacks, dtc_linkage) {
87                 struct thandle *dtc_th = th;
88
89                 if (cb->dtc_txn_start == NULL ||
90                     !(cb->dtc_tag & env->le_ctx.lc_tags))
91                         continue;
92
93                 /*
94                  * Usually dt_txn_hook_start is called from bottom device,
95                  * and if the thandle has th_top, then we need use top
96                  * thandle for the callback in the top thandle layer
97                  */
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                 /*
129                  * Usually dt_txn_hook_stop is called from bottom device,
130                  * and if the thandle has th_top, then we need use top
131                  * thandle for the callback in the top thandle layer
132                  */
133                 if (th->th_top != NULL)
134                         dtc_th = th->th_top;
135
136                 rc = cb->dtc_txn_stop(env, dtc_th, cb->dtc_cookie);
137                 if (rc < 0)
138                         break;
139         }
140         return rc;
141 }
142 EXPORT_SYMBOL(dt_txn_hook_stop);
143
144 int dt_device_init(struct dt_device *dev, struct lu_device_type *t)
145 {
146         INIT_LIST_HEAD(&dev->dd_txn_callbacks);
147         return lu_device_init(&dev->dd_lu_dev, t);
148 }
149 EXPORT_SYMBOL(dt_device_init);
150
151 void dt_device_fini(struct dt_device *dev)
152 {
153         lu_device_fini(&dev->dd_lu_dev);
154 }
155 EXPORT_SYMBOL(dt_device_fini);
156
157 int dt_object_init(struct dt_object *obj,
158                    struct lu_object_header *h, struct lu_device *d)
159
160 {
161         return lu_object_init(&obj->do_lu, h, d);
162 }
163 EXPORT_SYMBOL(dt_object_init);
164
165 void dt_object_fini(struct dt_object *obj)
166 {
167         lu_object_fini(&obj->do_lu);
168 }
169 EXPORT_SYMBOL(dt_object_fini);
170
171 int dt_try_as_dir(const struct lu_env *env, struct dt_object *obj)
172 {
173         if (obj->do_index_ops == NULL)
174                 obj->do_ops->do_index_try(env, obj, &dt_directory_features);
175         return obj->do_index_ops != NULL;
176 }
177 EXPORT_SYMBOL(dt_try_as_dir);
178
179 enum dt_format_type dt_mode_to_dft(__u32 mode)
180 {
181         enum dt_format_type result;
182
183         switch (mode & S_IFMT) {
184         case S_IFDIR:
185                 result = DFT_DIR;
186                 break;
187         case S_IFREG:
188                 result = DFT_REGULAR;
189                 break;
190         case S_IFLNK:
191                 result = DFT_SYM;
192                 break;
193         case S_IFCHR:
194         case S_IFBLK:
195         case S_IFIFO:
196         case S_IFSOCK:
197                 result = DFT_NODE;
198                 break;
199         default:
200                 LASSERTF(0, "invalid mode %o\n", mode);
201                 result = 0; /* Just for satisfying compiler. */
202                 break;
203         }
204         return result;
205 }
206 EXPORT_SYMBOL(dt_mode_to_dft);
207
208 /**
209  * lookup fid for object named \a name in directory \a dir.
210  */
211
212 int dt_lookup_dir(const struct lu_env *env, struct dt_object *dir,
213                   const char *name, struct lu_fid *fid)
214 {
215         if (dt_try_as_dir(env, dir))
216                 return dt_lookup(env, dir, (struct dt_rec *)fid,
217                                  (const struct dt_key *)name);
218         return -ENOTDIR;
219 }
220 EXPORT_SYMBOL(dt_lookup_dir);
221
222 /*
223  * this differs from dt_locate by top_dev as parameter
224  * but not one from lu_site
225  */
226 struct dt_object *dt_locate_at(const struct lu_env *env,
227                                struct dt_device *dev,
228                                const struct lu_fid *fid,
229                                struct lu_device *top_dev,
230                                const struct lu_object_conf *conf)
231 {
232         struct lu_object *lo;
233         struct lu_object *n;
234
235         lo = lu_object_find_at(env, top_dev, fid, conf);
236         if (IS_ERR(lo))
237                 return ERR_PTR(PTR_ERR(lo));
238
239         LASSERT(lo != NULL);
240
241         list_for_each_entry(n, &lo->lo_header->loh_layers, lo_linkage) {
242                 if (n->lo_dev == &dev->dd_lu_dev)
243                         return container_of0(n, struct dt_object, do_lu);
244         }
245
246         lu_object_put(env, lo);
247         return ERR_PTR(-ENOENT);
248 }
249 EXPORT_SYMBOL(dt_locate_at);
250
251 /**
252  * find an object named \a entry in given \a dfh->dfh_o directory.
253  */
254 static int dt_find_entry(const struct lu_env *env, const char *entry,
255                          void *data)
256 {
257         struct dt_find_hint *dfh = data;
258         struct dt_device *dt = dfh->dfh_dt;
259         struct lu_fid *fid = dfh->dfh_fid;
260         struct dt_object *obj = dfh->dfh_o;
261         int rc;
262
263         rc = dt_lookup_dir(env, obj, entry, fid);
264         dt_object_put(env, obj);
265         if (rc == 0) {
266                 obj = dt_locate(env, dt, fid);
267                 if (IS_ERR(obj))
268                         rc = PTR_ERR(obj);
269         }
270         dfh->dfh_o = obj;
271
272         return rc;
273 }
274
275 /**
276  * Abstract function which parses path name. This function feeds
277  * path component to \a entry_func.
278  */
279 int dt_path_parser(const struct lu_env *env,
280                    char *path, dt_entry_func_t entry_func,
281                    void *data)
282 {
283         char *e;
284         int rc = 0;
285
286         while (1) {
287                 e = strsep(&path, "/");
288                 if (e == NULL)
289                         break;
290
291                 if (e[0] == 0) {
292                         if (!path || path[0] == '\0')
293                                 break;
294                         continue;
295                 }
296                 rc = entry_func(env, e, data);
297                 if (rc)
298                         break;
299         }
300
301         return rc;
302 }
303
304 struct dt_object *
305 dt_store_resolve(const struct lu_env *env, struct dt_device *dt,
306                  const char *path, struct lu_fid *fid)
307 {
308         struct dt_thread_info *info = dt_info(env);
309         struct dt_find_hint *dfh = &info->dti_dfh;
310         struct dt_object *obj;
311         int result;
312
313
314         dfh->dfh_dt = dt;
315         dfh->dfh_fid = fid;
316
317         strlcpy(info->dti_buf, path, sizeof(info->dti_buf));
318
319         result = dt->dd_ops->dt_root_get(env, dt, fid);
320         if (result == 0) {
321                 obj = dt_locate(env, dt, fid);
322                 if (!IS_ERR(obj)) {
323                         dfh->dfh_o = obj;
324                         result = dt_path_parser(env, info->dti_buf,
325                                                 dt_find_entry, dfh);
326                         if (result != 0)
327                                 obj = ERR_PTR(result);
328                         else
329                                 obj = dfh->dfh_o;
330                 }
331         } else {
332                 obj = ERR_PTR(result);
333         }
334         return obj;
335 }
336
337 static struct dt_object *dt_reg_open(const struct lu_env *env,
338                                      struct dt_device *dt,
339                                      struct dt_object *p,
340                                      const char *name,
341                                      struct lu_fid *fid)
342 {
343         struct dt_object *o;
344         int result;
345
346         result = dt_lookup_dir(env, p, name, fid);
347         if (result == 0)
348                 o = dt_locate(env, dt, fid);
349         else
350                 o = ERR_PTR(result);
351
352         return o;
353 }
354
355 /**
356  * Open dt object named \a filename from \a dirname directory.
357  *      \param  dt      dt device
358  *      \param  fid     on success, object fid is stored in *fid
359  */
360 struct dt_object *dt_store_open(const struct lu_env *env, struct dt_device *dt,
361                                 const char *dirname, const char *filename,
362                                 struct lu_fid *fid)
363 {
364         struct dt_object *file;
365         struct dt_object *dir;
366
367         dir = dt_store_resolve(env, dt, dirname, fid);
368         if (!IS_ERR(dir)) {
369                 file = dt_reg_open(env, dt, dir, filename, fid);
370                 dt_object_put(env, dir);
371         } else {
372                 file = dir;
373         }
374
375         return file;
376 }
377
378 struct dt_object *dt_find_or_create(const struct lu_env *env,
379                                     struct dt_device *dt,
380                                     const struct lu_fid *fid,
381                                     struct dt_object_format *dof,
382                                     struct lu_attr *at)
383 {
384         struct dt_object *dto;
385         struct thandle *th;
386         int rc;
387
388         ENTRY;
389
390         dto = dt_locate(env, dt, fid);
391         if (IS_ERR(dto))
392                 RETURN(dto);
393
394         LASSERT(dto != NULL);
395         if (dt_object_exists(dto))
396                 RETURN(dto);
397
398         th = dt_trans_create(env, dt);
399         if (IS_ERR(th))
400                 GOTO(out, rc = PTR_ERR(th));
401
402         rc = dt_declare_create(env, dto, at, NULL, dof, th);
403         if (rc)
404                 GOTO(trans_stop, rc);
405
406         rc = dt_trans_start_local(env, dt, th);
407         if (rc)
408                 GOTO(trans_stop, rc);
409
410         dt_write_lock(env, dto, 0);
411         if (dt_object_exists(dto))
412                 GOTO(unlock, rc = 0);
413
414         CDEBUG(D_OTHER, "create new object "DFID"\n", PFID(fid));
415
416         rc = dt_create(env, dto, at, NULL, dof, th);
417         if (rc)
418                 GOTO(unlock, rc);
419         LASSERT(dt_object_exists(dto));
420 unlock:
421         dt_write_unlock(env, dto);
422 trans_stop:
423         dt_trans_stop(env, dt, th);
424 out:
425         if (rc) {
426                 dt_object_put(env, dto);
427                 dto = ERR_PTR(rc);
428         }
429
430         RETURN(dto);
431 }
432 EXPORT_SYMBOL(dt_find_or_create);
433
434 /* dt class init function. */
435 int dt_global_init(void)
436 {
437         int result;
438
439         LU_CONTEXT_KEY_INIT(&dt_key);
440         result = lu_context_key_register(&dt_key);
441         return result;
442 }
443
444 void dt_global_fini(void)
445 {
446         lu_context_key_degister(&dt_key);
447 }
448
449 /**
450  * Generic read helper. May return an error for partial reads.
451  *
452  * \param env  lustre environment
453  * \param dt   object to be read
454  * \param buf  lu_buf to be filled, with buffer pointer and length
455  * \param pos position to start reading, updated as data is read
456  *
457  * \retval real size of data read
458  * \retval -ve errno on failure
459  */
460 int dt_read(const struct lu_env *env, struct dt_object *dt,
461             struct lu_buf *buf, loff_t *pos)
462 {
463         LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
464         return dt->do_body_ops->dbo_read(env, dt, buf, pos);
465 }
466 EXPORT_SYMBOL(dt_read);
467
468 /**
469  * Read structures of fixed size from storage.  Unlike dt_read(), using
470  * dt_record_read() will return an error for partial reads.
471  *
472  * \param env  lustre environment
473  * \param dt   object to be read
474  * \param buf  lu_buf to be filled, with buffer pointer and length
475  * \param pos position to start reading, updated as data is read
476  *
477  * \retval 0 on successfully reading full buffer
478  * \retval -EFAULT on short read
479  * \retval -ve errno on failure
480  */
481 int dt_record_read(const struct lu_env *env, struct dt_object *dt,
482                    struct lu_buf *buf, loff_t *pos)
483 {
484         ssize_t size;
485
486         LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
487
488         size = dt->do_body_ops->dbo_read(env, dt, buf, pos);
489         if (size < 0)
490                 return size;
491         return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
492 }
493 EXPORT_SYMBOL(dt_record_read);
494
495 int dt_record_write(const struct lu_env *env, struct dt_object *dt,
496                     const struct lu_buf *buf, loff_t *pos, struct thandle *th)
497 {
498         ssize_t size;
499
500         LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
501         LASSERT(th != NULL);
502         LASSERT(dt->do_body_ops);
503         LASSERT(dt->do_body_ops->dbo_write);
504
505         size = dt->do_body_ops->dbo_write(env, dt, buf, pos, th);
506         if (size < 0)
507                 return size;
508         return (size == (ssize_t)buf->lb_len) ? 0 : -EFAULT;
509 }
510 EXPORT_SYMBOL(dt_record_write);
511
512 int dt_declare_version_set(const struct lu_env *env, struct dt_object *o,
513                            struct thandle *th)
514 {
515         struct lu_buf vbuf;
516         char *xname = XATTR_NAME_VERSION;
517
518         LASSERT(o);
519         vbuf.lb_buf = NULL;
520         vbuf.lb_len = sizeof(dt_obj_version_t);
521         return dt_declare_xattr_set(env, o, &vbuf, xname, 0, th);
522
523 }
524 EXPORT_SYMBOL(dt_declare_version_set);
525
526 void dt_version_set(const struct lu_env *env, struct dt_object *o,
527                     dt_obj_version_t version, struct thandle *th)
528 {
529         struct lu_buf vbuf;
530         char *xname = XATTR_NAME_VERSION;
531         int rc;
532
533         LASSERT(o);
534         vbuf.lb_buf = &version;
535         vbuf.lb_len = sizeof(version);
536
537         rc = dt_xattr_set(env, o, &vbuf, xname, 0, th);
538         if (rc < 0)
539                 CDEBUG(D_INODE, "Can't set version, rc %d\n", rc);
540         return;
541 }
542 EXPORT_SYMBOL(dt_version_set);
543
544 dt_obj_version_t dt_version_get(const struct lu_env *env, struct dt_object *o)
545 {
546         struct lu_buf vbuf;
547         char *xname = XATTR_NAME_VERSION;
548         dt_obj_version_t version;
549         int rc;
550
551         LASSERT(o);
552         vbuf.lb_buf = &version;
553         vbuf.lb_len = sizeof(version);
554         rc = dt_xattr_get(env, o, &vbuf, xname);
555         if (rc != sizeof(version)) {
556                 CDEBUG(D_INODE, "Can't get version, rc %d\n", rc);
557                 version = 0;
558         }
559         return version;
560 }
561 EXPORT_SYMBOL(dt_version_get);
562
563 /* list of all supported index types */
564
565 /* directories */
566 const struct dt_index_features dt_directory_features;
567 EXPORT_SYMBOL(dt_directory_features);
568
569 /* scrub iterator */
570 const struct dt_index_features dt_otable_features;
571 EXPORT_SYMBOL(dt_otable_features);
572
573 /* lfsck layout orphan */
574 const struct dt_index_features dt_lfsck_layout_orphan_features = {
575         .dif_flags              = 0,
576         .dif_keysize_min        = sizeof(struct lu_fid),
577         .dif_keysize_max        = sizeof(struct lu_fid),
578         .dif_recsize_min        = sizeof(struct lu_orphan_rec_v3),
579         .dif_recsize_max        = sizeof(struct lu_orphan_rec_v3),
580         .dif_ptrsize            = 4
581 };
582 EXPORT_SYMBOL(dt_lfsck_layout_orphan_features);
583
584 /* lfsck layout dangling */
585 const struct dt_index_features dt_lfsck_layout_dangling_features = {
586         .dif_flags              = DT_IND_UPDATE,
587         .dif_keysize_min        = sizeof(struct lfsck_layout_dangling_key),
588         .dif_keysize_max        = sizeof(struct lfsck_layout_dangling_key),
589         .dif_recsize_min        = sizeof(struct lu_fid),
590         .dif_recsize_max        = sizeof(struct lu_fid),
591         .dif_ptrsize            = 4
592 };
593 EXPORT_SYMBOL(dt_lfsck_layout_dangling_features);
594
595 /* lfsck namespace */
596 const struct dt_index_features dt_lfsck_namespace_features = {
597         .dif_flags              = DT_IND_UPDATE,
598         .dif_keysize_min        = sizeof(struct lu_fid),
599         .dif_keysize_max        = sizeof(struct lu_fid),
600         .dif_recsize_min        = sizeof(__u8),
601         .dif_recsize_max        = sizeof(__u8),
602         .dif_ptrsize            = 4
603 };
604 EXPORT_SYMBOL(dt_lfsck_namespace_features);
605
606 /* accounting indexes */
607 const struct dt_index_features dt_acct_features = {
608         .dif_flags              = DT_IND_UPDATE,
609         .dif_keysize_min        = sizeof(__u64), /* 64-bit uid/gid */
610         .dif_keysize_max        = sizeof(__u64), /* 64-bit uid/gid */
611         .dif_recsize_min        = sizeof(struct lquota_acct_rec), /* 16 bytes */
612         .dif_recsize_max        = sizeof(struct lquota_acct_rec), /* 16 bytes */
613         .dif_ptrsize            = 4
614 };
615 EXPORT_SYMBOL(dt_acct_features);
616
617 /* global quota files */
618 const struct dt_index_features dt_quota_glb_features = {
619         .dif_flags              = DT_IND_UPDATE,
620         /* a different key would have to be used for per-directory quota */
621         .dif_keysize_min        = sizeof(__u64), /* 64-bit uid/gid */
622         .dif_keysize_max        = sizeof(__u64), /* 64-bit uid/gid */
623         .dif_recsize_min        = sizeof(struct lquota_glb_rec), /* 32 bytes */
624         .dif_recsize_max        = sizeof(struct lquota_glb_rec), /* 32 bytes */
625         .dif_ptrsize            = 4
626 };
627 EXPORT_SYMBOL(dt_quota_glb_features);
628
629 /* slave quota files */
630 const struct dt_index_features dt_quota_slv_features = {
631         .dif_flags              = DT_IND_UPDATE,
632         /* a different key would have to be used for per-directory quota */
633         .dif_keysize_min        = sizeof(__u64), /* 64-bit uid/gid */
634         .dif_keysize_max        = sizeof(__u64), /* 64-bit uid/gid */
635         .dif_recsize_min        = sizeof(struct lquota_slv_rec), /* 8 bytes */
636         .dif_recsize_max        = sizeof(struct lquota_slv_rec), /* 8 bytes */
637         .dif_ptrsize            = 4
638 };
639 EXPORT_SYMBOL(dt_quota_slv_features);
640
641 /* nodemap files, nodemap_rec size asserted in nodemap_storage.c */
642 const struct dt_index_features dt_nodemap_features = {
643         .dif_flags              = DT_IND_UPDATE,
644         .dif_keysize_min        = sizeof(__u64), /* 64-bit nodemap/record id */
645         .dif_keysize_max        = sizeof(__u64), /* 64-bit nodemap/record id */
646         .dif_recsize_min        = sizeof(union nodemap_rec), /* 32 bytes */
647         .dif_recsize_max        = sizeof(union nodemap_rec), /* 32 bytes */
648         .dif_ptrsize            = 4
649 };
650 EXPORT_SYMBOL(dt_nodemap_features);
651
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  */
656 static inline const struct dt_index_features *dt_index_feat_select(__u64 seq,
657                                                                    __u32 mode)
658 {
659         if (seq == FID_SEQ_QUOTA_GLB) {
660                 /* global quota index */
661                 if (!S_ISREG(mode))
662                         /* global quota index should be a regular file */
663                         return ERR_PTR(-ENOENT);
664                 return &dt_quota_glb_features;
665         } else if (seq == FID_SEQ_QUOTA) {
666                 /* quota slave index */
667                 if (!S_ISREG(mode))
668                         /* slave index should be a regular file */
669                         return ERR_PTR(-ENOENT);
670                 return &dt_quota_slv_features;
671         } else if (seq == FID_SEQ_LAYOUT_RBTREE){
672                 return &dt_lfsck_layout_orphan_features;
673         } else if (seq >= FID_SEQ_NORMAL) {
674                 /* object is part of the namespace, verify that it is a
675                  * directory */
676                 if (!S_ISDIR(mode))
677                         /* sorry, we can only deal with directory */
678                         return ERR_PTR(-ENOTDIR);
679                 return &dt_directory_features;
680         }
681
682         return ERR_PTR(-EOPNOTSUPP);
683 }
684
685 /*
686  * Fill a lu_idxpage with key/record pairs read for transfer via OBD_IDX_READ
687  * RPC
688  *
689  * \param env - is the environment passed by the caller
690  * \param lp  - is a pointer to the lu_page to fill
691  * \param nob - is the maximum number of bytes that should be copied
692  * \param iops - is the index operation vector associated with the index object
693  * \param it   - is a pointer to the current iterator
694  * \param attr - is the index attribute to pass to iops->rec()
695  * \param arg  - is a pointer to the idx_info structure
696  */
697 static int dt_index_page_build(const struct lu_env *env, union lu_page *lp,
698                                size_t nob, const struct dt_it_ops *iops,
699                                struct dt_it *it, __u32 attr, void *arg)
700 {
701         struct idx_info *ii = (struct idx_info *)arg;
702         struct lu_idxpage *lip = &lp->lp_idx;
703         char *entry;
704         __u64 hash;
705         __u16 hashsize = 0;
706         __u16 keysize = 0;
707         __u16 recsize;
708         int rc;
709
710         ENTRY;
711
712         if (nob < LIP_HDR_SIZE)
713                 return -EINVAL;
714
715         /* initialize the header of the new container */
716         memset(lip, 0, LIP_HDR_SIZE);
717         lip->lip_magic = LIP_MAGIC;
718         nob           -= LIP_HDR_SIZE;
719
720         /* client wants to the 64-bit hash value associated with each record */
721         if (!(ii->ii_flags & II_FL_NOHASH))
722                 hashsize = sizeof(hash);
723
724         entry = lip->lip_entries;
725         do {
726                 /* fetch 64-bit hash value */
727                 hash = iops->store(env, it);
728                 ii->ii_hash_end = hash;
729
730                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_IDX_READ_BREAK)) {
731                         if (lip->lip_nr != 0)
732                                 GOTO(out, rc = 0);
733                 }
734
735                 if (!(ii->ii_flags & II_FL_NOKEY)) {
736                         keysize = iops->key_size(env, it);
737                         if (!(ii->ii_flags & II_FL_VARKEY) &&
738                             keysize != ii->ii_keysize) {
739                                 CERROR("keysize mismatch %hu != %hu.\n",
740                                        keysize, ii->ii_keysize);
741                                 GOTO(out, rc = -EINVAL);
742                         }
743                 }
744
745                 /* and finally the record */
746                 if (ii->ii_flags & II_FL_VARREC)
747                         recsize = iops->rec_size(env, it, attr);
748                 else
749                         recsize = ii->ii_recsize;
750
751                 if (nob < hashsize + keysize + recsize) {
752                         if (lip->lip_nr == 0)
753                                 GOTO(out, rc = -E2BIG);
754                         GOTO(out, rc = 0);
755                 }
756
757                 rc = iops->rec(env, it,
758                                (struct dt_rec *)(entry + hashsize + keysize),
759                                attr);
760                 if (!rc) {
761                         if (hashsize)
762                                 memcpy(entry, &hash, hashsize);
763                         if (keysize) {
764                                 struct dt_key *key;
765
766                                 key = iops->key(env, it);
767                                 memcpy(entry + hashsize, key, keysize);
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                         entry += hashsize + keysize + recsize;
774                         nob -= hashsize + keysize + recsize;
775                 } else if (rc != -ESTALE) {
776                         GOTO(out, rc);
777                 }
778
779                 /* move on to the next record */
780                 do {
781                         rc = iops->next(env, it);
782                 } while (rc == -ESTALE);
783         } while (rc == 0);
784
785         GOTO(out, rc);
786 out:
787         if (rc >= 0 && lip->lip_nr > 0)
788                 /* one more container */
789                 ii->ii_count++;
790         if (rc > 0)
791                 /* no more entries */
792                 ii->ii_hash_end = II_END_OFF;
793         return rc;
794 }
795
796
797 /*
798  * Walk index and fill lu_page containers with key/record pairs
799  *
800  * \param env - is the environment passed by the caller
801  * \param obj - is the index object to parse
802  * \param rdpg - is the lu_rdpg descriptor associated with the transfer
803  * \param filler - is the callback function responsible for filling a lu_page
804  *                 with key/record pairs in the format wanted by the caller.
805  *                 If NULL, uses dt_index_page_build
806  * \param arg    - is an opaq argument passed to the filler function
807  *
808  * \retval sum (in bytes) of all filled lu_pages
809  * \retval -ve errno on failure
810  */
811 int dt_index_walk(const struct lu_env *env, struct dt_object *obj,
812                   const struct lu_rdpg *rdpg, dt_index_page_build_t filler,
813                   void *arg)
814 {
815         struct dt_it *it;
816         const struct dt_it_ops *iops;
817         size_t pageidx, nob, nlupgs = 0;
818         int rc;
819         ENTRY;
820
821         LASSERT(rdpg->rp_pages != NULL);
822         LASSERT(obj->do_index_ops != NULL);
823
824         if (filler == NULL)
825                 filler = dt_index_page_build;
826
827         nob = rdpg->rp_count;
828         if (nob == 0)
829                 RETURN(-EFAULT);
830
831         /* Iterate through index and fill containers from @rdpg */
832         iops = &obj->do_index_ops->dio_it;
833         LASSERT(iops != NULL);
834         it = iops->init(env, obj, rdpg->rp_attrs);
835         if (IS_ERR(it))
836                 RETURN(PTR_ERR(it));
837
838         rc = iops->load(env, it, rdpg->rp_hash);
839         if (rc == 0) {
840                 /*
841                  * Iterator didn't find record with exactly the key requested.
842                  *
843                  * It is currently either
844                  *
845                  *     - positioned above record with key less than
846                  *     requested---skip it.
847                  *     - or not positioned at all (is in IAM_IT_SKEWED
848                  *     state)---position it on the next item.
849                  */
850                 rc = iops->next(env, it);
851         } else if (rc > 0) {
852                 rc = 0;
853         } else {
854                 if (rc == -ENODATA)
855                         rc = 0;
856                 GOTO(out, rc);
857         }
858
859         /*
860          * Fill containers one after the other. There might be multiple
861          * containers per physical page.
862          *
863          * At this point and across for-loop:
864          *  rc == 0 -> ok, proceed.
865          *  rc >  0 -> end of index.
866          *  rc <  0 -> error.
867          */
868         for (pageidx = 0; rc == 0 && nob > 0; pageidx++) {
869                 union lu_page   *lp;
870                 int              i;
871
872                 LASSERT(pageidx < rdpg->rp_npages);
873                 lp = kmap(rdpg->rp_pages[pageidx]);
874
875                 /* fill lu pages */
876                 for (i = 0; i < LU_PAGE_COUNT; i++, lp++, nob -= LU_PAGE_SIZE) {
877                         rc = filler(env, lp, min_t(size_t, nob, LU_PAGE_SIZE),
878                                     iops, it, rdpg->rp_attrs, arg);
879                         if (rc < 0)
880                                 break;
881                         /* one more lu_page */
882                         nlupgs++;
883                         if (rc > 0)
884                                 /* end of index */
885                                 break;
886                 }
887                 kunmap(rdpg->rp_pages[i]);
888         }
889
890 out:
891         iops->put(env, it);
892         iops->fini(env, it);
893
894         if (rc >= 0)
895                 rc = min_t(size_t, nlupgs * LU_PAGE_SIZE, rdpg->rp_count);
896
897         RETURN(rc);
898 }
899 EXPORT_SYMBOL(dt_index_walk);
900
901 /**
902  * Walk key/record pairs of an index and copy them into 4KB containers to be
903  * transferred over the network. This is the common handler for OBD_IDX_READ
904  * RPC processing.
905  *
906  * \param env - is the environment passed by the caller
907  * \param dev - is the dt_device storing the index
908  * \param ii  - is the idx_info structure packed by the client in the
909  *              OBD_IDX_READ request
910  * \param rdpg - is the lu_rdpg descriptor
911  *
912  * \retval on success, return sum (in bytes) of all filled containers
913  * \retval appropriate error otherwise.
914  */
915 int dt_index_read(const struct lu_env *env, struct dt_device *dev,
916                   struct idx_info *ii, const struct lu_rdpg *rdpg)
917 {
918         const struct dt_index_features  *feat;
919         struct dt_object                *obj;
920         int                              rc;
921         ENTRY;
922
923         /*
924          * rp_count shouldn't be null and should be a multiple of the container
925          * size
926          */
927         if (rdpg->rp_count == 0 || (rdpg->rp_count & (LU_PAGE_SIZE - 1)) != 0)
928                 RETURN(-EFAULT);
929
930         if (!fid_is_quota(&ii->ii_fid) && !fid_is_layout_rbtree(&ii->ii_fid) &&
931             !fid_is_norm(&ii->ii_fid))
932                 RETURN(-EOPNOTSUPP);
933
934         /* lookup index object subject to the transfer */
935         obj = dt_locate(env, dev, &ii->ii_fid);
936         if (IS_ERR(obj))
937                 RETURN(PTR_ERR(obj));
938         if (dt_object_exists(obj) == 0)
939                 GOTO(out, rc = -ENOENT);
940
941         /* fetch index features associated with index object */
942         feat = dt_index_feat_select(fid_seq(&ii->ii_fid),
943                                     lu_object_attr(&obj->do_lu));
944         if (IS_ERR(feat))
945                 GOTO(out, rc = PTR_ERR(feat));
946
947         /* load index feature if not done already */
948         if (obj->do_index_ops == NULL) {
949                 rc = obj->do_ops->do_index_try(env, obj, feat);
950                 if (rc)
951                         GOTO(out, rc);
952         }
953
954         /* fill ii_flags with supported index features */
955         ii->ii_flags &= (II_FL_NOHASH | II_FL_NOKEY | II_FL_VARKEY |
956                          II_FL_VARREC);
957
958         if (!(feat->dif_flags & DT_IND_VARKEY))
959                 ii->ii_keysize = feat->dif_keysize_max;
960
961         if (!(feat->dif_flags & DT_IND_VARREC))
962                 ii->ii_recsize = feat->dif_recsize_max;
963
964         if (feat->dif_flags & DT_IND_NONUNQ)
965                 /* key isn't necessarily unique */
966                 ii->ii_flags |= II_FL_NONUNQ;
967
968         if (!fid_is_layout_rbtree(&ii->ii_fid)) {
969                 dt_read_lock(env, obj, 0);
970                 /* fetch object version before walking the index */
971                 ii->ii_version = dt_version_get(env, obj);
972         }
973
974         /* walk the index and fill lu_idxpages with key/record pairs */
975         rc = dt_index_walk(env, obj, rdpg, dt_index_page_build, ii);
976         if (!fid_is_layout_rbtree(&ii->ii_fid))
977                 dt_read_unlock(env, obj);
978
979         if (rc == 0) {
980                 /* index is empty */
981                 LASSERT(ii->ii_count == 0);
982                 ii->ii_hash_end = II_END_OFF;
983         }
984
985         GOTO(out, rc);
986 out:
987         dt_object_put(env, obj);
988         return rc;
989 }
990 EXPORT_SYMBOL(dt_index_read);
991
992 #ifdef CONFIG_PROC_FS
993 int lprocfs_dt_blksize_seq_show(struct seq_file *m, void *v)
994 {
995         struct dt_device *dt = m->private;
996         struct obd_statfs osfs;
997
998         int rc = dt_statfs(NULL, dt, &osfs);
999         if (rc == 0)
1000                 seq_printf(m, "%u\n", (unsigned) osfs.os_bsize);
1001         return rc;
1002 }
1003 EXPORT_SYMBOL(lprocfs_dt_blksize_seq_show);
1004
1005 int lprocfs_dt_kbytestotal_seq_show(struct seq_file *m, void *v)
1006 {
1007         struct dt_device *dt = m->private;
1008         struct obd_statfs osfs;
1009
1010         int rc = dt_statfs(NULL, dt, &osfs);
1011         if (rc == 0) {
1012                 __u32 blk_size = osfs.os_bsize >> 10;
1013                 __u64 result = osfs.os_blocks;
1014
1015                 while (blk_size >>= 1)
1016                         result <<= 1;
1017
1018                 seq_printf(m, "%llu\n", result);
1019         }
1020         return rc;
1021 }
1022 EXPORT_SYMBOL(lprocfs_dt_kbytestotal_seq_show);
1023
1024 int lprocfs_dt_kbytesfree_seq_show(struct seq_file *m, void *v)
1025 {
1026         struct dt_device *dt = m->private;
1027         struct obd_statfs osfs;
1028
1029         int rc = dt_statfs(NULL, dt, &osfs);
1030         if (rc == 0) {
1031                 __u32 blk_size = osfs.os_bsize >> 10;
1032                 __u64 result = osfs.os_bfree;
1033
1034                 while (blk_size >>= 1)
1035                         result <<= 1;
1036
1037                 seq_printf(m, "%llu\n", result);
1038         }
1039         return rc;
1040 }
1041 EXPORT_SYMBOL(lprocfs_dt_kbytesfree_seq_show);
1042
1043 int lprocfs_dt_kbytesavail_seq_show(struct seq_file *m, void *v)
1044 {
1045         struct dt_device *dt = m->private;
1046         struct obd_statfs osfs;
1047
1048         int rc = dt_statfs(NULL, dt, &osfs);
1049         if (rc == 0) {
1050                 __u32 blk_size = osfs.os_bsize >> 10;
1051                 __u64 result = osfs.os_bavail;
1052
1053                 while (blk_size >>= 1)
1054                         result <<= 1;
1055
1056                 seq_printf(m, "%llu\n", result);
1057         }
1058         return rc;
1059 }
1060 EXPORT_SYMBOL(lprocfs_dt_kbytesavail_seq_show);
1061
1062 int lprocfs_dt_filestotal_seq_show(struct seq_file *m, void *v)
1063 {
1064         struct dt_device *dt = m->private;
1065         struct obd_statfs osfs;
1066
1067         int rc = dt_statfs(NULL, dt, &osfs);
1068         if (rc == 0)
1069                 seq_printf(m, "%llu\n", osfs.os_files);
1070         return rc;
1071 }
1072 EXPORT_SYMBOL(lprocfs_dt_filestotal_seq_show);
1073
1074 int lprocfs_dt_filesfree_seq_show(struct seq_file *m, void *v)
1075 {
1076         struct dt_device *dt = m->private;
1077         struct obd_statfs osfs;
1078
1079         int rc = dt_statfs(NULL, dt, &osfs);
1080         if (rc == 0)
1081                 seq_printf(m, "%llu\n", osfs.os_ffree);
1082         return rc;
1083 }
1084 EXPORT_SYMBOL(lprocfs_dt_filesfree_seq_show);
1085
1086 #endif /* CONFIG_PROC_FS */
1087
1088 static ssize_t uuid_show(struct kobject *kobj, struct attribute *attr,
1089                          char *buf)
1090 {
1091         struct dt_device *dt = container_of(kobj, struct dt_device,
1092                                             dd_kobj);
1093         struct lu_device *lu = dt2lu_dev(dt);
1094
1095         if (!lu->ld_obd)
1096                 return -ENODEV;
1097
1098         return sprintf(buf, "%s\n", lu->ld_obd->obd_uuid.uuid);
1099 }
1100 LUSTRE_RO_ATTR(uuid);
1101
1102 static ssize_t blocksize_show(struct kobject *kobj, struct attribute *attr,
1103                               char *buf)
1104 {
1105         struct dt_device *dt = container_of(kobj, struct dt_device,
1106                                             dd_kobj);
1107         struct obd_statfs osfs;
1108         int rc;
1109
1110         rc = dt_statfs(NULL, dt, &osfs);
1111         if (rc)
1112                 return rc;
1113
1114         return sprintf(buf, "%u\n", (unsigned) osfs.os_bsize);
1115 }
1116 LUSTRE_RO_ATTR(blocksize);
1117
1118 static ssize_t kbytestotal_show(struct kobject *kobj, struct attribute *attr,
1119                                 char *buf)
1120 {
1121         struct dt_device *dt = container_of(kobj, struct dt_device,
1122                                             dd_kobj);
1123         struct obd_statfs osfs;
1124         u32 blk_size;
1125         u64 result;
1126         int rc;
1127
1128         rc = dt_statfs(NULL, dt, &osfs);
1129         if (rc)
1130                 return rc;
1131
1132         blk_size = osfs.os_bsize >> 10;
1133         result = osfs.os_blocks;
1134
1135         while (blk_size >>= 1)
1136                 result <<= 1;
1137
1138         return sprintf(buf, "%llu\n", result);
1139 }
1140 LUSTRE_RO_ATTR(kbytestotal);
1141
1142 static ssize_t kbytesfree_show(struct kobject *kobj, struct attribute *attr,
1143                                char *buf)
1144 {
1145         struct dt_device *dt = container_of(kobj, struct dt_device,
1146                                             dd_kobj);
1147         struct obd_statfs osfs;
1148         u32 blk_size;
1149         u64 result;
1150         int rc;
1151
1152         rc = dt_statfs(NULL, dt, &osfs);
1153         if (rc)
1154                 return rc;
1155
1156         blk_size = osfs.os_bsize >> 10;
1157         result = osfs.os_bfree;
1158
1159         while (blk_size >>= 1)
1160                 result <<= 1;
1161
1162         return sprintf(buf, "%llu\n", result);
1163 }
1164 LUSTRE_RO_ATTR(kbytesfree);
1165
1166 static ssize_t kbytesavail_show(struct kobject *kobj, struct attribute *attr,
1167                                 char *buf)
1168 {
1169         struct dt_device *dt = container_of(kobj, struct dt_device,
1170                                             dd_kobj);
1171         struct obd_statfs osfs;
1172         u32 blk_size;
1173         u64 result;
1174         int rc;
1175
1176         rc = dt_statfs(NULL, dt, &osfs);
1177         if (rc)
1178                 return rc;
1179
1180         blk_size = osfs.os_bsize >> 10;
1181         result = osfs.os_bavail;
1182
1183         while (blk_size >>= 1)
1184                 result <<= 1;
1185
1186         return sprintf(buf, "%llu\n", result);
1187 }
1188 LUSTRE_RO_ATTR(kbytesavail);
1189
1190 static ssize_t filestotal_show(struct kobject *kobj, struct attribute *attr,
1191                                char *buf)
1192 {
1193         struct dt_device *dt = container_of(kobj, struct dt_device,
1194                                             dd_kobj);
1195         struct obd_statfs osfs;
1196         int rc;
1197
1198         rc = dt_statfs(NULL, dt, &osfs);
1199         if (rc)
1200                 return rc;
1201
1202         return sprintf(buf, "%llu\n", osfs.os_files);
1203 }
1204 LUSTRE_RO_ATTR(filestotal);
1205
1206 static ssize_t filesfree_show(struct kobject *kobj, struct attribute *attr,
1207                               char *buf)
1208 {
1209         struct dt_device *dt = container_of(kobj, struct dt_device,
1210                                             dd_kobj);
1211         struct obd_statfs osfs;
1212         int rc;
1213
1214         rc = dt_statfs(NULL, dt, &osfs);
1215         if (rc)
1216                 return rc;
1217
1218         return sprintf(buf, "%llu\n", osfs.os_ffree);
1219 }
1220 LUSTRE_RO_ATTR(filesfree);
1221
1222 static const struct attribute *dt_def_attrs[] = {
1223         &lustre_attr_uuid.attr,
1224         &lustre_attr_blocksize.attr,
1225         &lustre_attr_kbytestotal.attr,
1226         &lustre_attr_kbytesfree.attr,
1227         &lustre_attr_kbytesavail.attr,
1228         &lustre_attr_filestotal.attr,
1229         &lustre_attr_filesfree.attr,
1230         NULL,
1231 };
1232
1233 static void dt_sysfs_release(struct kobject *kobj)
1234 {
1235         struct dt_device *dt = container_of(kobj, struct dt_device,
1236                                             dd_kobj);
1237
1238         complete(&dt->dd_kobj_unregister);
1239 }
1240
1241 int dt_tunables_fini(struct dt_device *dt)
1242 {
1243         if (!dt)
1244                 return -EINVAL;
1245
1246         if (!IS_ERR_OR_NULL(dt->dd_debugfs_entry))
1247                 ldebugfs_remove(&dt->dd_debugfs_entry);
1248
1249         if (dt->dd_def_attrs)
1250                 sysfs_remove_files(&dt->dd_kobj, dt->dd_def_attrs);
1251
1252         kobject_put(&dt->dd_kobj);
1253         wait_for_completion(&dt->dd_kobj_unregister);
1254
1255         return 0;
1256 }
1257 EXPORT_SYMBOL(dt_tunables_fini);
1258
1259 int dt_tunables_init(struct dt_device *dt, struct obd_type *type,
1260                      const char *name, struct lprocfs_vars *list)
1261 {
1262         int rc;
1263
1264         dt->dd_ktype.sysfs_ops = &lustre_sysfs_ops;
1265         dt->dd_ktype.release = dt_sysfs_release;
1266
1267         init_completion(&dt->dd_kobj_unregister);
1268         rc = kobject_init_and_add(&dt->dd_kobj, &dt->dd_ktype, &type->typ_kobj,
1269                                   "%s", name);
1270         if (rc)
1271                 return rc;
1272
1273         dt->dd_def_attrs = dt_def_attrs;
1274
1275         rc = sysfs_create_files(&dt->dd_kobj, dt->dd_def_attrs);
1276         if (rc) {
1277                 kobject_put(&dt->dd_kobj);
1278                 return rc;
1279         }
1280
1281         /*
1282          * No need to register debugfs if no enteries. This allows us to
1283          * choose between using dt_device or obd_device for debugfs.
1284          */
1285         if (!list)
1286                 return rc;
1287
1288         dt->dd_debugfs_entry = ldebugfs_register(name,
1289                                                  type->typ_debugfs_entry,
1290                                                  list, dt);
1291         if (IS_ERR_OR_NULL(dt->dd_debugfs_entry)) {
1292                 rc = dt->dd_debugfs_entry ? PTR_ERR(dt->dd_debugfs_entry)
1293                                           : -ENOMEM;
1294                 CERROR("%s: error %d setting up debugfs\n",
1295                        name, rc);
1296                 dt->dd_debugfs_entry = NULL;
1297                 sysfs_remove_files(&dt->dd_kobj, dt->dd_def_attrs);
1298                 kobject_put(&dt->dd_kobj);
1299                 return rc;
1300         }
1301
1302         return rc;
1303 }
1304 EXPORT_SYMBOL(dt_tunables_init);