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