4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2014, Intel Corporation.
26 * lustre/lod/lod_sub_object.c
28 * LOD sub object methods
30 * This file implements sub-object methods for LOD.
32 * LOD is Logic volume layer in the MDS stack, which will handle striping
33 * and distribute the update to different OSP/OSD. After directing the updates
34 * to one specific OSD/OSP, it also needs to do some thing before calling
35 * OSD/OSP API, for example recording updates for cross-MDT operation, get
36 * the next level transaction etc.
38 * Author: Di Wang <di.wang@intel.com>
41 #define DEBUG_SUBSYSTEM S_MDS
44 #include <obd_class.h>
45 #include <lustre_ver.h>
46 #include <obd_support.h>
47 #include <lprocfs_status.h>
49 #include <lustre_fid.h>
50 #include <lustre_param.h>
51 #include <md_object.h>
52 #include <lustre_linkea.h>
54 #include "lod_internal.h"
56 struct thandle *lod_sub_get_thandle(const struct lu_env *env,
58 const struct dt_object *sub_obj,
61 struct lod_device *lod = dt2lod_dev(th->th_dev);
62 struct top_thandle *tth;
63 struct thandle *sub_th;
64 int type = LU_SEQ_RANGE_ANY;
69 if (record_update != NULL)
70 *record_update = false;
72 if (th->th_top == NULL)
75 tth = container_of(th, struct top_thandle, tt_super);
76 LASSERT(tth->tt_magic == TOP_THANDLE_MAGIC);
77 /* local object must be mdt object, Note: during ost object
78 * creation, FID is not assigned until osp_object_create(),
79 * so if the FID of sub_obj is zero, it means OST object. */
80 if (!dt_object_remote(sub_obj) ||
81 fid_is_zero(lu_object_fid(&sub_obj->do_lu))) {
82 /* local MDT object */
83 if (fid_is_sane(lu_object_fid(&sub_obj->do_lu)) &&
84 tth->tt_update_records != NULL &&
85 record_update != NULL)
86 *record_update = true;
88 RETURN(tth->tt_master_sub_thandle);
91 rc = lod_fld_lookup(env, lod, lu_object_fid(&sub_obj->do_lu),
96 if (type == LU_SEQ_RANGE_OST)
97 RETURN(tth->tt_master_sub_thandle);
99 if (tth->tt_update_records != NULL && record_update != NULL)
100 *record_update = true;
102 sub_th = thandle_get_sub(env, th, sub_obj);
108 * Declare sub-object creation.
110 * Get transaction of next layer and declare the creation of the object.
112 * \param[in] env execution environment
113 * \param[in] dt the object being created
114 * \param[in] attr the attributes of the object being created
115 * \param[in] hint the hint of the creation
116 * \param[in] dof the object format of the creation
117 * \param[th] th the transaction handle
119 * \retval 0 if the declaration succeeds
120 * \retval negative errno if the declaration fails.
122 int lod_sub_object_declare_create(const struct lu_env *env,
123 struct dt_object *dt,
124 struct lu_attr *attr,
125 struct dt_allocation_hint *hint,
126 struct dt_object_format *dof,
129 struct thandle *sub_th;
131 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
133 return PTR_ERR(sub_th);
135 return dt_declare_create(env, dt, attr, hint, dof, sub_th);
141 * Get transaction of next layer, record updates if it belongs to cross-MDT
142 * operation, and create the object.
144 * \param[in] env execution environment
145 * \param[in] dt the object being created
146 * \param[in] attr the attributes of the object being created
147 * \param[in] hint the hint of the creation
148 * \param[in] dof the object format of the creation
149 * \param[th] th the transaction handle
151 * \retval 0 if the creation succeeds
152 * \retval negative errno if the creation fails.
154 int lod_sub_object_create(const struct lu_env *env, struct dt_object *dt,
155 struct lu_attr *attr,
156 struct dt_allocation_hint *hint,
157 struct dt_object_format *dof,
160 struct thandle *sub_th;
165 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
167 RETURN(PTR_ERR(sub_th));
170 rc = update_record_pack(create, th,
171 lu_object_fid(&dt->do_lu),
177 rc = dt_create(env, dt, attr, hint, dof, sub_th);
183 * Declare adding reference for the sub-object
185 * Get transaction of next layer and declare the reference adding.
187 * \param[in] env execution environment
188 * \param[in] dt dt object to add reference
189 * \param[in] th transaction handle
191 * \retval 0 if the declaration succeeds.
192 * \retval negative errno if the declaration fails.
194 int lod_sub_object_declare_ref_add(const struct lu_env *env,
195 struct dt_object *dt,
198 struct thandle *sub_th;
202 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
204 RETURN(PTR_ERR(sub_th));
206 rc = dt_declare_ref_add(env, dt, sub_th);
212 * Add reference for the sub-object
214 * Get transaction of next layer, record updates if it belongs to cross-MDT
215 * operation and add reference of the object.
217 * \param[in] env execution environment
218 * \param[in] dt dt object to add reference
219 * \param[in] th transaction handle
221 * \retval 0 if it succeeds.
222 * \retval negative errno if it fails.
224 int lod_sub_object_ref_add(const struct lu_env *env, struct dt_object *dt,
227 struct thandle *sub_th;
232 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
234 RETURN(PTR_ERR(sub_th));
237 rc = update_record_pack(ref_add, th,
238 lu_object_fid(&dt->do_lu));
243 rc = dt_ref_add(env, dt, sub_th);
249 * Declare deleting reference for the sub-object
251 * Get transaction of next layer and declare the reference deleting.
253 * \param[in] env execution environment
254 * \param[in] dt dt object to delete reference
255 * \param[in] th transaction handle
257 * \retval 0 if the declaration succeeds.
258 * \retval negative errno if the declaration fails.
260 int lod_sub_object_declare_ref_del(const struct lu_env *env,
261 struct dt_object *dt,
264 struct thandle *sub_th;
268 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
270 RETURN(PTR_ERR(sub_th));
272 rc = dt_declare_ref_del(env, dt, sub_th);
278 * Delete reference for the sub-object
280 * Get transaction of next layer, record updates if it belongs to cross-MDT
281 * operation and delete reference of the object.
283 * \param[in] env execution environment
284 * \param[in] dt dt object to delete reference
285 * \param[in] th transaction handle
287 * \retval 0 if it succeeds.
288 * \retval negative errno if it fails.
290 int lod_sub_object_ref_del(const struct lu_env *env, struct dt_object *dt,
293 struct thandle *sub_th;
298 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
300 RETURN(PTR_ERR(sub_th));
303 rc = update_record_pack(ref_del, th,
304 lu_object_fid(&dt->do_lu));
309 rc = dt_ref_del(env, dt, sub_th);
315 * Declare destroying sub-object
317 * Get transaction of next layer and declare the sub-object destroy.
319 * \param[in] env execution environment
320 * \param[in] dt dt object to be destroyed
321 * \param[in] th transaction handle
323 * \retval 0 if the declaration succeeds.
324 * \retval negative errno if the declaration fails.
326 int lod_sub_object_declare_destroy(const struct lu_env *env,
327 struct dt_object *dt,
330 struct thandle *sub_th;
334 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
336 RETURN(PTR_ERR(sub_th));
338 rc = dt_declare_destroy(env, dt, sub_th);
346 * Get transaction of next layer, record updates if it belongs to cross-MDT
347 * operation and destroy the object.
349 * \param[in] env execution environment
350 * \param[in] dt dt object to be destroyed
351 * \param[in] th transaction handle
353 * \retval 0 if the destroy succeeds.
354 * \retval negative errno if the destroy fails.
356 int lod_sub_object_destroy(const struct lu_env *env, struct dt_object *dt,
359 struct thandle *sub_th;
364 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
366 RETURN(PTR_ERR(sub_th));
369 rc = update_record_pack(object_destroy, th,
370 lu_object_fid(&dt->do_lu));
375 rc = dt_destroy(env, dt, sub_th);
381 * Declare sub-object index insert
383 * Get transaction of next layer and declare index insert.
385 * \param[in] env execution environment
386 * \param[in] dt object for which to insert index
387 * \param[in] rec record of the index which will be inserted
388 * \param[in] key key of the index which will be inserted
389 * \param[in] th the transaction handle
391 * \retval 0 if the declaration succeeds.
392 * \retval negative errno if the declaration fails.
394 int lod_sub_object_declare_insert(const struct lu_env *env,
395 struct dt_object *dt,
396 const struct dt_rec *rec,
397 const struct dt_key *key,
400 struct thandle *sub_th;
402 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
404 return PTR_ERR(sub_th);
406 return dt_declare_insert(env, dt, rec, key, sub_th);
410 * Insert index of sub object
412 * Get transaction of next layer, record updates if it belongs to cross-MDT
413 * operation, and insert the index.
415 * \param[in] env execution environment
416 * \param[in] dt object for which to insert index
417 * \param[in] rec record of the index to be inserted
418 * \param[in] key key of the index to be inserted
419 * \param[in] th the transaction handle
420 * \param[in] ign whether ignore quota
422 * \retval 0 if the insertion succeeds.
423 * \retval negative errno if the insertion fails.
425 int lod_sub_object_index_insert(const struct lu_env *env, struct dt_object *dt,
426 const struct dt_rec *rec,
427 const struct dt_key *key, struct thandle *th,
430 struct thandle *sub_th;
434 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
436 return PTR_ERR(sub_th);
439 rc = update_record_pack(index_insert, th,
440 lu_object_fid(&dt->do_lu), rec, key);
445 return dt_insert(env, dt, rec, key, sub_th, ign);
449 * Declare sub-object index delete
451 * Get transaction of next layer and declare index deletion.
453 * \param[in] env execution environment
454 * \param[in] dt object for which to delete index
455 * \param[in] key key of the index which will be deleted
456 * \param[in] th the transaction handle
458 * \retval 0 if the declaration succeeds.
459 * \retval negative errno if the declaration fails.
461 int lod_sub_object_declare_delete(const struct lu_env *env,
462 struct dt_object *dt,
463 const struct dt_key *key,
466 struct thandle *sub_th;
468 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
470 return PTR_ERR(sub_th);
472 return dt_declare_delete(env, dt, key, sub_th);
476 * Delete index of sub object
478 * Get transaction of next layer, record updates if it belongs to cross-MDT
479 * operation, and delete the index.
481 * \param[in] env execution environment
482 * \param[in] dt object for which to delete index
483 * \param[in] key key of the index to be deleted
484 * \param[in] th the transaction handle
486 * \retval 0 if the deletion succeeds.
487 * \retval negative errno if the deletion fails.
489 int lod_sub_object_delete(const struct lu_env *env, struct dt_object *dt,
490 const struct dt_key *name, struct thandle *th)
492 struct thandle *sub_th;
497 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
499 RETURN(PTR_ERR(sub_th));
502 rc = update_record_pack(index_delete, th,
503 lu_object_fid(&dt->do_lu), name);
508 rc = dt_delete(env, dt, name, sub_th);
515 * Get transaction of next layer, and declare xattr set.
517 * \param[in] env execution environment
518 * \param[in] dt object on which to set xattr
519 * \param[in] buf xattr to be set
520 * \param[in] name name of the xattr
521 * \param[in] fl flag for setting xattr
523 * \retval 0 if the declaration succeeds.
524 * \retval negative errno if the declaration fails.
526 int lod_sub_object_declare_xattr_set(const struct lu_env *env,
527 struct dt_object *dt,
528 const struct lu_buf *buf,
529 const char *name, int fl,
532 struct thandle *sub_th;
536 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
538 RETURN(PTR_ERR(sub_th));
540 rc = dt_declare_xattr_set(env, dt, buf, name, fl, sub_th);
548 * Get transaction of next layer, record updates if it belongs to cross-MDT
549 * operation, and set xattr to the object.
551 * \param[in] env execution environment
552 * \param[in] dt object on which to set xattr
553 * \param[in] buf xattr to be set
554 * \param[in] name name of the xattr
555 * \param[in] fl flag for setting xattr
556 * \param[in] th transaction handle
558 * \retval 0 if the xattr setting succeeds.
559 * \retval negative errno if xattr setting fails.
561 int lod_sub_object_xattr_set(const struct lu_env *env, struct dt_object *dt,
562 const struct lu_buf *buf, const char *name, int fl,
565 struct thandle *sub_th;
570 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
572 RETURN(PTR_ERR(sub_th));
575 rc = update_record_pack(xattr_set, th,
576 lu_object_fid(&dt->do_lu),
582 rc = dt_xattr_set(env, dt, buf, name, fl, sub_th);
590 * Get transaction of next layer, and declare attr set.
592 * \param[in] env execution environment
593 * \param[in] dt object on which to set attr
594 * \param[in] attr attributes to be set
595 * \param[in] th transaction handle
597 * \retval 0 if the declaration succeeds.
598 * \retval negative errno if the declaration fails.
600 int lod_sub_object_declare_attr_set(const struct lu_env *env,
601 struct dt_object *dt,
602 const struct lu_attr *attr,
605 struct thandle *sub_th;
609 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
611 RETURN(PTR_ERR(sub_th));
613 rc = dt_declare_attr_set(env, dt, attr, sub_th);
621 * Get transaction of next layer, record updates if it belongs to cross-MDT
622 * operation, and set attributes to the object.
624 * \param[in] env execution environment
625 * \param[in] dt object on which to set attr
626 * \param[in] attr attrbutes to be set
627 * \param[in] th transaction handle
629 * \retval 0 if attributes setting succeeds.
630 * \retval negative errno if the attributes setting fails.
632 int lod_sub_object_attr_set(const struct lu_env *env,
633 struct dt_object *dt,
634 const struct lu_attr *attr,
638 struct thandle *sub_th;
642 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
644 RETURN(PTR_ERR(sub_th));
647 rc = update_record_pack(attr_set, th, lu_object_fid(&dt->do_lu),
653 rc = dt_attr_set(env, dt, attr, sub_th);
661 * Get transaction of next layer, and declare xattr deletion.
663 * \param[in] env execution environment
664 * \param[in] dt object on which to delete xattr
665 * \param[in] name name of the xattr to be deleted
666 * \param[in] th transaction handle
668 * \retval 0 if the declaration succeeds.
669 * \retval negative errno if the declaration fails.
671 int lod_sub_object_declare_xattr_del(const struct lu_env *env,
672 struct dt_object *dt,
676 struct thandle *sub_th;
680 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
682 RETURN(PTR_ERR(sub_th));
684 rc = dt_declare_xattr_del(env, dt, name, sub_th);
690 * xattribute deletion
692 * Get transaction of next layer, record update if it belongs to cross-MDT
693 * operation and delete xattr.
695 * \param[in] env execution environment
696 * \param[in] dt object on which to delete xattr
697 * \param[in] name name of the xattr to be deleted
698 * \param[in] th transaction handle
700 * \retval 0 if the deletion succeeds.
701 * \retval negative errno if the deletion fails.
703 int lod_sub_object_xattr_del(const struct lu_env *env,
704 struct dt_object *dt,
708 struct thandle *sub_th;
713 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
715 RETURN(PTR_ERR(sub_th));
718 rc = update_record_pack(xattr_del, th,
719 lu_object_fid(&dt->do_lu), name);
724 rc = dt_xattr_del(env, dt, name, sub_th);
730 * Declare buffer write
732 * Get transaction of next layer and declare buffer write.
734 * \param[in] env execution environment
735 * \param[in] dt object to be written
736 * \param[in] buf buffer to write which includes an embedded size field
737 * \param[in] pos offet in the object to start writing at
738 * \param[in] th transaction handle
740 * \retval 0 if the insertion succeeds.
741 * \retval negative errno if the insertion fails.
743 int lod_sub_object_declare_write(const struct lu_env *env,
744 struct dt_object *dt,
745 const struct lu_buf *buf, loff_t pos,
748 struct thandle *sub_th;
752 sub_th = lod_sub_get_thandle(env, th, dt, NULL);
754 RETURN(PTR_ERR(sub_th));
756 rc = dt_declare_write(env, dt, buf, pos, sub_th);
762 * Write buffer to sub object
764 * Get transaction of next layer, records buffer write if it belongs to
765 * Cross-MDT operation, and write buffer.
767 * \param[in] env execution environment
768 * \param[in] dt object to be written
769 * \param[in] buf buffer to write which includes an embedded size field
770 * \param[in] pos offet in the object to start writing at
771 * \param[in] th transaction handle
772 * \param[in] rq enforcement for this write
774 * \retval the buffer size in bytes if it succeeds.
775 * \retval negative errno if it fails.
777 ssize_t lod_sub_object_write(const struct lu_env *env, struct dt_object *dt,
778 const struct lu_buf *buf, loff_t *pos,
779 struct thandle *th, int rq)
781 struct thandle *sub_th;
786 sub_th = lod_sub_get_thandle(env, th, dt, &record_update);
788 RETURN(PTR_ERR(sub_th));
791 rc = update_record_pack(write, th, lu_object_fid(&dt->do_lu),
797 rc = dt_write(env, dt, buf, pos, sub_th, rq);