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)
60 struct lod_device *lod = dt2lod_dev(th->th_dev);
61 struct top_thandle *tth;
62 struct thandle *sub_th;
63 int type = LU_SEQ_RANGE_ANY;
68 if (th->th_top == NULL)
71 tth = container_of(th, struct top_thandle, tt_super);
72 LASSERT(tth->tt_magic == TOP_THANDLE_MAGIC);
73 /* local object must be mdt object, Note: during ost object
74 * creation, FID is not assigned until osp_object_create(),
75 * so if the FID of sub_obj is zero, it means OST object. */
76 if (!dt_object_remote(sub_obj) ||
77 fid_is_zero(lu_object_fid(&sub_obj->do_lu)))
78 RETURN(tth->tt_master_sub_thandle);
80 rc = lod_fld_lookup(env, lod, lu_object_fid(&sub_obj->do_lu),
85 if (type == LU_SEQ_RANGE_OST)
86 RETURN(tth->tt_master_sub_thandle);
88 sub_th = thandle_get_sub(env, th, sub_obj);
94 * Declare sub-object creation.
96 * Get transaction of next layer and declare the creation of the object.
98 * \param[in] env execution environment
99 * \param[in] dt the object being created
100 * \param[in] attr the attributes of the object being created
101 * \param[in] hint the hint of the creation
102 * \param[in] dof the object format of the creation
103 * \param[th] th the transaction handle
105 * \retval 0 if the declaration succeeds
106 * \retval negative errno if the declaration fails.
108 int lod_sub_object_declare_create(const struct lu_env *env,
109 struct dt_object *dt,
110 struct lu_attr *attr,
111 struct dt_allocation_hint *hint,
112 struct dt_object_format *dof,
115 struct thandle *sub_th;
117 sub_th = lod_sub_get_thandle(env, th, dt);
119 return PTR_ERR(sub_th);
121 return dt_declare_create(env, dt, attr, hint, dof, sub_th);
127 * Get transaction of next layer, record updates if it belongs to cross-MDT
128 * operation, and create the object.
130 * \param[in] env execution environment
131 * \param[in] dt the object being created
132 * \param[in] attr the attributes of the object being created
133 * \param[in] hint the hint of the creation
134 * \param[in] dof the object format of the creation
135 * \param[th] th the transaction handle
137 * \retval 0 if the creation succeeds
138 * \retval negative errno if the creation fails.
140 int lod_sub_object_create(const struct lu_env *env, struct dt_object *dt,
141 struct lu_attr *attr,
142 struct dt_allocation_hint *hint,
143 struct dt_object_format *dof,
146 struct thandle *sub_th;
150 sub_th = lod_sub_get_thandle(env, th, dt);
152 RETURN(PTR_ERR(sub_th));
154 rc = dt_create(env, dt, attr, hint, dof, sub_th);
160 * Declare adding reference for the sub-object
162 * Get transaction of next layer and declare the reference adding.
164 * \param[in] env execution environment
165 * \param[in] dt dt object to add reference
166 * \param[in] th transaction handle
168 * \retval 0 if the declaration succeeds.
169 * \retval negative errno if the declaration fails.
171 int lod_sub_object_declare_ref_add(const struct lu_env *env,
172 struct dt_object *dt,
175 struct thandle *sub_th;
179 sub_th = lod_sub_get_thandle(env, th, dt);
181 RETURN(PTR_ERR(sub_th));
183 rc = dt_declare_ref_add(env, dt, sub_th);
189 * Add reference for the sub-object
191 * Get transaction of next layer, record updates if it belongs to cross-MDT
192 * operation and add reference of the object.
194 * \param[in] env execution environment
195 * \param[in] dt dt object to add reference
196 * \param[in] th transaction handle
198 * \retval 0 if it succeeds.
199 * \retval negative errno if it fails.
201 int lod_sub_object_ref_add(const struct lu_env *env, struct dt_object *dt,
204 struct thandle *sub_th;
208 sub_th = lod_sub_get_thandle(env, th, dt);
210 RETURN(PTR_ERR(sub_th));
212 rc = dt_ref_add(env, dt, sub_th);
218 * Declare deleting reference for the sub-object
220 * Get transaction of next layer and declare the reference deleting.
222 * \param[in] env execution environment
223 * \param[in] dt dt object to delete reference
224 * \param[in] th transaction handle
226 * \retval 0 if the declaration succeeds.
227 * \retval negative errno if the declaration fails.
229 int lod_sub_object_declare_ref_del(const struct lu_env *env,
230 struct dt_object *dt,
233 struct thandle *sub_th;
237 sub_th = lod_sub_get_thandle(env, th, dt);
239 RETURN(PTR_ERR(sub_th));
241 rc = dt_declare_ref_del(env, dt, sub_th);
247 * Delete reference for the sub-object
249 * Get transaction of next layer, record updates if it belongs to cross-MDT
250 * operation and delete reference of the object.
252 * \param[in] env execution environment
253 * \param[in] dt dt object to delete reference
254 * \param[in] th transaction handle
256 * \retval 0 if it succeeds.
257 * \retval negative errno if it fails.
259 int lod_sub_object_ref_del(const struct lu_env *env, struct dt_object *dt,
262 struct thandle *sub_th;
266 sub_th = lod_sub_get_thandle(env, th, dt);
268 RETURN(PTR_ERR(sub_th));
270 rc = dt_ref_del(env, dt, sub_th);
276 * Declare destroying sub-object
278 * Get transaction of next layer and declare the sub-object destroy.
280 * \param[in] env execution environment
281 * \param[in] dt dt object to be destroyed
282 * \param[in] th transaction handle
284 * \retval 0 if the declaration succeeds.
285 * \retval negative errno if the declaration fails.
287 int lod_sub_object_declare_destroy(const struct lu_env *env,
288 struct dt_object *dt,
291 struct thandle *sub_th;
295 sub_th = lod_sub_get_thandle(env, th, dt);
297 RETURN(PTR_ERR(sub_th));
299 rc = dt_declare_destroy(env, dt, sub_th);
307 * Get transaction of next layer, record updates if it belongs to cross-MDT
308 * operation and destroy the object.
310 * \param[in] env execution environment
311 * \param[in] dt dt object to be destroyed
312 * \param[in] th transaction handle
314 * \retval 0 if the destroy succeeds.
315 * \retval negative errno if the destroy fails.
317 int lod_sub_object_destroy(const struct lu_env *env, struct dt_object *dt,
320 struct thandle *sub_th;
324 sub_th = lod_sub_get_thandle(env, th, dt);
326 RETURN(PTR_ERR(sub_th));
328 rc = dt_destroy(env, dt, sub_th);
334 * Declare sub-object index insert
336 * Get transaction of next layer and declare index insert.
338 * \param[in] env execution environment
339 * \param[in] dt object for which to insert index
340 * \param[in] rec record of the index which will be inserted
341 * \param[in] key key of the index which will be inserted
342 * \param[in] th the transaction handle
344 * \retval 0 if the declaration succeeds.
345 * \retval negative errno if the declaration fails.
347 int lod_sub_object_declare_insert(const struct lu_env *env,
348 struct dt_object *dt,
349 const struct dt_rec *rec,
350 const struct dt_key *key,
353 struct thandle *sub_th;
355 sub_th = lod_sub_get_thandle(env, th, dt);
357 return PTR_ERR(sub_th);
359 return dt_declare_insert(env, dt, rec, key, sub_th);
363 * Insert index of sub object
365 * Get transaction of next layer, record updates if it belongs to cross-MDT
366 * operation, and insert the index.
368 * \param[in] env execution environment
369 * \param[in] dt object for which to insert index
370 * \param[in] rec record of the index to be inserted
371 * \param[in] key key of the index to be inserted
372 * \param[in] th the transaction handle
373 * \param[in] ign whether ignore quota
375 * \retval 0 if the insertion succeeds.
376 * \retval negative errno if the insertion fails.
378 int lod_sub_object_index_insert(const struct lu_env *env, struct dt_object *dt,
379 const struct dt_rec *rec,
380 const struct dt_key *key, struct thandle *th,
383 struct thandle *sub_th;
385 sub_th = lod_sub_get_thandle(env, th, dt);
387 return PTR_ERR(sub_th);
389 return dt_insert(env, dt, rec, key, sub_th, ign);
393 * Declare sub-object index delete
395 * Get transaction of next layer and declare index deletion.
397 * \param[in] env execution environment
398 * \param[in] dt object for which to delete index
399 * \param[in] key key of the index which will be deleted
400 * \param[in] th the transaction handle
402 * \retval 0 if the declaration succeeds.
403 * \retval negative errno if the declaration fails.
405 int lod_sub_object_declare_delete(const struct lu_env *env,
406 struct dt_object *dt,
407 const struct dt_key *key,
410 struct thandle *sub_th;
412 sub_th = lod_sub_get_thandle(env, th, dt);
414 return PTR_ERR(sub_th);
416 return dt_declare_delete(env, dt, key, sub_th);
420 * Delete index of sub object
422 * Get transaction of next layer, record updates if it belongs to cross-MDT
423 * operation, and delete the index.
425 * \param[in] env execution environment
426 * \param[in] dt object for which to delete index
427 * \param[in] key key of the index to be deleted
428 * \param[in] th the transaction handle
430 * \retval 0 if the deletion succeeds.
431 * \retval negative errno if the deletion fails.
433 int lod_sub_object_delete(const struct lu_env *env, struct dt_object *dt,
434 const struct dt_key *name, struct thandle *th)
436 struct thandle *sub_th;
440 sub_th = lod_sub_get_thandle(env, th, dt);
442 RETURN(PTR_ERR(sub_th));
444 rc = dt_delete(env, dt, name, sub_th);
451 * Get transaction of next layer, and declare xattr set.
453 * \param[in] env execution environment
454 * \param[in] dt object on which to set xattr
455 * \param[in] buf xattr to be set
456 * \param[in] name name of the xattr
457 * \param[in] fl flag for setting xattr
459 * \retval 0 if the declaration succeeds.
460 * \retval negative errno if the declaration fails.
462 int lod_sub_object_declare_xattr_set(const struct lu_env *env,
463 struct dt_object *dt,
464 const struct lu_buf *buf,
465 const char *name, int fl,
468 struct thandle *sub_th;
472 sub_th = lod_sub_get_thandle(env, th, dt);
474 RETURN(PTR_ERR(sub_th));
476 rc = dt_declare_xattr_set(env, dt, buf, name, fl, sub_th);
484 * Get transaction of next layer, record updates if it belongs to cross-MDT
485 * operation, and set xattr to the object.
487 * \param[in] env execution environment
488 * \param[in] dt object on which to set xattr
489 * \param[in] buf xattr to be set
490 * \param[in] name name of the xattr
491 * \param[in] fl flag for setting xattr
492 * \param[in] th transaction handle
494 * \retval 0 if the xattr setting succeeds.
495 * \retval negative errno if xattr setting fails.
497 int lod_sub_object_xattr_set(const struct lu_env *env, struct dt_object *dt,
498 const struct lu_buf *buf, const char *name, int fl,
501 struct thandle *sub_th;
505 sub_th = lod_sub_get_thandle(env, th, dt);
507 RETURN(PTR_ERR(sub_th));
509 rc = dt_xattr_set(env, dt, buf, name, fl, sub_th);
517 * Get transaction of next layer, and declare attr set.
519 * \param[in] env execution environment
520 * \param[in] dt object on which to set attr
521 * \param[in] attr attributes to be set
522 * \param[in] th transaction handle
524 * \retval 0 if the declaration succeeds.
525 * \retval negative errno if the declaration fails.
527 int lod_sub_object_declare_attr_set(const struct lu_env *env,
528 struct dt_object *dt,
529 const struct lu_attr *attr,
532 struct thandle *sub_th;
536 sub_th = lod_sub_get_thandle(env, th, dt);
538 RETURN(PTR_ERR(sub_th));
540 rc = dt_declare_attr_set(env, dt, attr, sub_th);
548 * Get transaction of next layer, record updates if it belongs to cross-MDT
549 * operation, and set attributes to the object.
551 * \param[in] env execution environment
552 * \param[in] dt object on which to set attr
553 * \param[in] attr attrbutes to be set
554 * \param[in] th transaction handle
556 * \retval 0 if attributes setting succeeds.
557 * \retval negative errno if the attributes setting fails.
559 int lod_sub_object_attr_set(const struct lu_env *env,
560 struct dt_object *dt,
561 const struct lu_attr *attr,
564 struct thandle *sub_th;
568 sub_th = lod_sub_get_thandle(env, th, dt);
570 RETURN(PTR_ERR(sub_th));
572 rc = dt_attr_set(env, dt, attr, sub_th);
580 * Get transaction of next layer, and declare xattr deletion.
582 * \param[in] env execution environment
583 * \param[in] dt object on which to delete xattr
584 * \param[in] name name of the xattr to be deleted
585 * \param[in] th transaction handle
587 * \retval 0 if the declaration succeeds.
588 * \retval negative errno if the declaration fails.
590 int lod_sub_object_declare_xattr_del(const struct lu_env *env,
591 struct dt_object *dt,
595 struct thandle *sub_th;
599 sub_th = lod_sub_get_thandle(env, th, dt);
601 RETURN(PTR_ERR(sub_th));
603 rc = dt_declare_xattr_del(env, dt, name, sub_th);
609 * xattribute deletion
611 * Get transaction of next layer, record update if it belongs to cross-MDT
612 * operation and delete xattr.
614 * \param[in] env execution environment
615 * \param[in] dt object on which to delete xattr
616 * \param[in] name name of the xattr to be deleted
617 * \param[in] th transaction handle
619 * \retval 0 if the deletion succeeds.
620 * \retval negative errno if the deletion fails.
622 int lod_sub_object_xattr_del(const struct lu_env *env,
623 struct dt_object *dt,
627 struct thandle *sub_th;
631 sub_th = lod_sub_get_thandle(env, th, dt);
633 RETURN(PTR_ERR(sub_th));
635 rc = dt_xattr_del(env, dt, name, sub_th);
641 * Declare buffer write
643 * Get transaction of next layer and declare buffer write.
645 * \param[in] env execution environment
646 * \param[in] dt object to be written
647 * \param[in] buf buffer to write which includes an embedded size field
648 * \param[in] pos offet in the object to start writing at
649 * \param[in] th transaction handle
651 * \retval 0 if the insertion succeeds.
652 * \retval negative errno if the insertion fails.
654 int lod_sub_object_declare_write(const struct lu_env *env,
655 struct dt_object *dt,
656 const struct lu_buf *buf, loff_t pos,
659 struct thandle *sub_th;
663 sub_th = lod_sub_get_thandle(env, th, dt);
665 RETURN(PTR_ERR(sub_th));
667 rc = dt_declare_write(env, dt, buf, pos, sub_th);
673 * Write buffer to sub object
675 * Get transaction of next layer, records buffer write if it belongs to
676 * Cross-MDT operation, and write buffer.
678 * \param[in] env execution environment
679 * \param[in] dt object to be written
680 * \param[in] buf buffer to write which includes an embedded size field
681 * \param[in] pos offet in the object to start writing at
682 * \param[in] th transaction handle
683 * \param[in] rq enforcement for this write
685 * \retval the buffer size in bytes if it succeeds.
686 * \retval negative errno if it fails.
688 ssize_t lod_sub_object_write(const struct lu_env *env, struct dt_object *dt,
689 const struct lu_buf *buf, loff_t *pos,
690 struct thandle *th, int rq)
692 struct thandle *sub_th;
696 sub_th = lod_sub_get_thandle(env, th, dt);
698 RETURN(PTR_ERR(sub_th));
700 rc = dt_write(env, dt, buf, pos, sub_th, rq);