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