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