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