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