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