1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/mdd/mdd_lov.c
40 * Lustre Metadata Server (mds) handling of striped file data
42 * Author: Peter Braam <braam@clusterfs.com>
43 * Author: wangdi <wangdi@clusterfs.com>
47 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include <linux/module.h>
53 #include <obd_class.h>
54 #include <lustre_ver.h>
55 #include <obd_support.h>
57 #include <lprocfs_status.h>
58 #include <lustre_mds.h>
59 #include <lustre_fid.h>
60 #include <lustre/lustre_idl.h>
62 #include "mdd_internal.h"
64 static int mdd_notify(struct obd_device *host, struct obd_device *watched,
65 enum obd_notify_event ev, void *owner, void *data)
67 struct mdd_device *mdd = owner;
71 LASSERT(owner != NULL);
74 case OBD_NOTIFY_ACTIVE:
76 case OBD_NOTIFY_SYNC_NONBLOCK:
77 rc = md_do_upcall(NULL, &mdd->mdd_md_dev,
80 case OBD_NOTIFY_CONFIG:
81 rc = md_do_upcall(NULL, &mdd->mdd_md_dev,
84 #ifdef HAVE_QUOTA_SUPPORT
85 case OBD_NOTIFY_QUOTA:
86 rc = md_do_upcall(NULL, &mdd->mdd_md_dev,
91 CDEBUG(D_INFO, "Unhandled notification %#x\n", ev);
97 /* The obd is created for handling data stack for mdd */
98 int mdd_init_obd(const struct lu_env *env, struct mdd_device *mdd,
99 struct lustre_cfg *cfg)
101 char *dev = lustre_cfg_string(cfg, 0);
102 int rc, name_size, uuid_size;
105 struct lustre_cfg_bufs *bufs;
106 struct lustre_cfg *lcfg;
107 struct obd_device *obd;
110 mds_id = lu_site2md(mdd2lu_dev(mdd)->ld_site)->ms_node_id;
111 name_size = strlen(MDD_OBD_NAME) + 35;
112 uuid_size = strlen(MDD_OBD_UUID) + 35;
114 OBD_ALLOC(name, name_size);
115 OBD_ALLOC(uuid, uuid_size);
116 if (name == NULL || uuid == NULL)
117 GOTO(cleanup_mem, rc = -ENOMEM);
121 GOTO(cleanup_mem, rc = -ENOMEM);
123 snprintf(name, strlen(MDD_OBD_NAME) + 35, "%s-%s",
126 snprintf(uuid, strlen(MDD_OBD_UUID) + 35, "%s-%s",
129 lustre_cfg_bufs_reset(bufs, name);
130 lustre_cfg_bufs_set_string(bufs, 1, MDD_OBD_TYPE);
131 lustre_cfg_bufs_set_string(bufs, 2, uuid);
132 lustre_cfg_bufs_set_string(bufs, 3, (char*)dev/* MDD_OBD_PROFILE */);
133 lustre_cfg_bufs_set_string(bufs, 4, (char*)dev);
135 lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
138 GOTO(cleanup_mem, rc = -ENOMEM);
140 rc = class_attach(lcfg);
142 GOTO(lcfg_cleanup, rc);
144 obd = class_name2obd(name);
146 CERROR("Can not find obd %s\n", MDD_OBD_NAME);
150 cfs_spin_lock(&obd->obd_dev_lock);
151 obd->obd_recovering = 1;
152 cfs_spin_unlock(&obd->obd_dev_lock);
153 obd->u.mds.mds_id = mds_id;
154 obd->u.obt.obt_osd_properties.osd_max_ea_size =
155 mdd->mdd_dt_conf.ddp_max_ea_size;
157 rc = class_setup(obd, lcfg);
159 GOTO(class_detach, rc);
162 * Add here for obd notify mechanism, when adding a new ost, the mds
163 * will notify this mdd. The mds will be used for quota also.
165 obd->obd_upcall.onu_upcall = mdd_notify;
166 obd->obd_upcall.onu_owner = mdd;
167 mdd->mdd_obd_dev = obd;
172 class_detach(obd, lcfg);
174 lustre_cfg_free(lcfg);
177 OBD_FREE(name, name_size);
179 OBD_FREE(uuid, uuid_size);
183 int mdd_fini_obd(const struct lu_env *env, struct mdd_device *mdd,
184 struct lustre_cfg *lcfg)
186 struct obd_device *obd;
190 obd = mdd2obd_dev(mdd);
193 rc = class_cleanup(obd, lcfg);
195 GOTO(lcfg_cleanup, rc);
197 obd->obd_upcall.onu_upcall = NULL;
198 obd->obd_upcall.onu_owner = NULL;
199 rc = class_detach(obd, lcfg);
201 GOTO(lcfg_cleanup, rc);
202 mdd->mdd_obd_dev = NULL;
209 int mdd_get_md(const struct lu_env *env, struct mdd_object *obj,
210 void *md, int *md_size, const char *name)
215 rc = mdo_xattr_get(env, obj, mdd_buf_get(env, md, *md_size), name,
216 mdd_object_capa(env, obj));
218 * XXX: Handling of -ENODATA, the right way is to have ->do_md_get()
219 * exported by dt layer.
221 if (rc == 0 || rc == -ENODATA) {
225 CERROR("Error %d reading eadata - %d\n", rc, *md_size);
227 /* XXX: Convert lov EA but fixed after verification test. */
234 int mdd_get_md_locked(const struct lu_env *env, struct mdd_object *obj,
235 void *md, int *md_size, const char *name)
238 mdd_read_lock(env, obj, MOR_TGT_CHILD);
239 rc = mdd_get_md(env, obj, md, md_size, name);
240 mdd_read_unlock(env, obj);
244 static int mdd_lov_set_stripe_md(const struct lu_env *env,
245 struct mdd_object *obj, struct lu_buf *buf,
246 struct thandle *handle)
248 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
249 struct obd_device *obd = mdd2obd_dev(mdd);
250 struct obd_export *lov_exp = obd->u.mds.mds_lov_exp;
251 struct lov_stripe_md *lsm = NULL;
255 LASSERT(S_ISDIR(mdd_object_type(obj)) || S_ISREG(mdd_object_type(obj)));
256 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, lov_exp, 0,
260 obd_free_memmd(lov_exp, &lsm);
262 rc = mdd_xattr_set_txn(env, obj, buf, XATTR_NAME_LOV, 0, handle);
264 CDEBUG(D_INFO, "set lov ea of "DFID" rc %d \n", PFID(mdo2fid(obj)), rc);
269 * Permission check is done before call it,
270 * no need check again.
272 static int mdd_lov_set_dir_md(const struct lu_env *env,
273 struct mdd_object *obj, struct lu_buf *buf,
274 struct thandle *handle)
276 struct lov_user_md *lum = NULL;
280 LASSERT(S_ISDIR(mdd_object_type(obj)));
281 lum = (struct lov_user_md*)buf->lb_buf;
283 /* if { size, offset, count } = { 0, -1, 0 } and no pool
284 * (i.e. all default values specified) then delete default
285 * striping from dir. */
286 if (LOVEA_DELETE_VALUES(lum->lmm_stripe_size, lum->lmm_stripe_count,
287 lum->lmm_stripe_offset) &&
288 lum->lmm_magic != LOV_USER_MAGIC_V3) {
289 rc = mdd_xattr_set_txn(env, obj, &LU_BUF_NULL,
290 XATTR_NAME_LOV, 0, handle);
293 CDEBUG(D_INFO, "delete lov ea of "DFID" rc %d \n",
294 PFID(mdo2fid(obj)), rc);
296 rc = mdd_lov_set_stripe_md(env, obj, buf, handle);
301 int mdd_lsm_sanity_check(const struct lu_env *env, struct mdd_object *obj)
303 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
304 struct md_ucred *uc = md_ucred(env);
308 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
312 if ((uc->mu_fsuid != tmp_la->la_uid) &&
313 !mdd_capable(uc, CFS_CAP_FOWNER))
314 rc = mdd_permission_internal_locked(env, obj, tmp_la,
315 MAY_WRITE, MOR_TGT_CHILD);
320 int mdd_lov_set_md(const struct lu_env *env, struct mdd_object *pobj,
321 struct mdd_object *child, struct lov_mds_md *lmmp,
322 int lmm_size, struct thandle *handle, int set_stripe)
329 buf = mdd_buf_get(env, lmmp, lmm_size);
330 mode = mdd_object_type(child);
331 if (S_ISREG(mode) && lmm_size > 0) {
333 rc = mdd_lov_set_stripe_md(env, child, buf, handle);
335 rc = mdd_xattr_set_txn(env, child, buf,
336 XATTR_NAME_LOV, 0, handle);
338 } else if (S_ISDIR(mode)) {
339 if (lmmp == NULL && lmm_size == 0) {
340 struct mdd_device *mdd = mdd_obj2mdd_dev(child);
341 struct lov_mds_md *lmm = mdd_max_lmm_get(env, mdd);
342 int size = sizeof(struct lov_mds_md_v3);
344 /* Get parent dir stripe and set */
346 rc = mdd_get_md_locked(env, pobj, lmm, &size,
349 buf = mdd_buf_get(env, lmm, size);
350 rc = mdd_xattr_set_txn(env, child, buf,
354 CERROR("error on copy stripe info: rc "
358 LASSERT(lmmp != NULL && lmm_size > 0);
359 rc = mdd_lov_set_dir_md(env, child, buf, handle);
362 CDEBUG(D_INFO, "Set lov md %p size %d for fid "DFID" rc %d\n",
363 lmmp, lmm_size, PFID(mdo2fid(child)), rc);
367 int mdd_lov_objid_prepare(struct mdd_device *mdd, struct lov_mds_md *lmm)
369 /* copy mds_lov code is using wrong layer */
370 return mds_lov_prepare_objids(mdd->mdd_obd_dev, lmm);
373 int mdd_declare_lov_objid_update(const struct lu_env *env,
374 struct mdd_device *mdd,
375 struct thandle *handle)
377 struct obd_device *obd = mdd2obd_dev(mdd);
380 /* in prepare we create local files */
381 if (unlikely(mdd->mdd_capa == NULL))
384 /* XXX: this is a temporary solution to declare llog changes
385 * will be fixed in 2.3 with new llog implementation */
387 size = obd->u.mds.mds_lov_desc.ld_tgt_count * sizeof(obd_id);
388 return dt_declare_record_write(env, mdd->mdd_capa, size, 0, handle);
391 void mdd_lov_objid_update(struct mdd_device *mdd, struct lov_mds_md *lmm)
393 /* copy mds_lov code is using wrong layer */
394 mds_lov_update_objids(mdd->mdd_obd_dev, lmm);
397 void mdd_lov_create_finish(const struct lu_env *env, struct mdd_device *mdd,
398 struct lov_mds_md *lmm, int lmm_size,
399 const struct md_op_spec *spec)
401 if (lmm && !spec->no_create)
402 OBD_FREE_LARGE(lmm, lmm_size);
405 int mdd_lov_create(const struct lu_env *env, struct mdd_device *mdd,
406 struct mdd_object *parent, struct mdd_object *child,
407 struct lov_mds_md **lmm, int *lmm_size,
408 const struct md_op_spec *spec, struct md_attr *ma)
410 struct obd_device *obd = mdd2obd_dev(mdd);
411 struct obd_export *lov_exp = obd->u.mds.mds_lov_exp;
412 struct lu_site *site = mdd2lu_dev(mdd)->ld_site;
414 struct lov_stripe_md *lsm = NULL;
415 const void *eadata = spec->u.sp_ea.eadata;
416 __u64 create_flags = spec->sp_cr_flags;
417 struct obd_trans_info *oti = &mdd_env_info(env)->mti_oti;
418 struct lu_attr *la = &ma->ma_attr;
422 if (!md_should_create(create_flags)) {
428 /* replay case, has objects already, only get lov from eadata */
429 if (spec->no_create != 0) {
430 *lmm = (struct lov_mds_md *)spec->u.sp_ea.eadata;
431 *lmm_size = spec->u.sp_ea.eadatalen;
432 if (*lmm_size == lov_mds_md_size((*lmm)->lmm_stripe_count,
433 (*lmm)->lmm_magic)) {
436 CERROR("incorrect lsm received during recovery\n");
441 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
442 GOTO(out_ids, rc = -ENOMEM);
444 LASSERT(lov_exp != NULL);
445 oa = &mdd_env_info(env)->mti_oa;
447 oa->o_uid = 0; /* must have 0 uid / gid on OST */
449 oa->o_seq = mdt_to_obd_objseq(lu_site2md(site)->ms_node_id);
450 oa->o_mode = S_IFREG | 0600;
451 oa->o_id = fid_ver_oid(mdd_object_fid(child));
452 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLFLAGS |
453 OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLGROUP;
456 if (!(create_flags & MDS_OPEN_HAS_OBJS)) {
457 if (create_flags & MDS_OPEN_HAS_EA) {
458 LASSERT(eadata != NULL);
459 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, lov_exp,
460 0, &lsm, (void*)eadata);
464 /* get lov ea from parent and set to lov */
465 struct lov_mds_md *_lmm;
468 LASSERT(parent != NULL);
470 _lmm_size = mdd_lov_mdsize(env, mdd);
471 _lmm = mdd_max_lmm_get(env, mdd);
474 GOTO(out_oti, rc = -ENOMEM);
476 rc = mdd_get_md_locked(env, parent, _lmm,
480 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
488 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_OPEN_WAIT_CREATE, 10);
489 rc = obd_create(lov_exp, oa, &lsm, oti);
492 CERROR("Create error for "DFID": %d\n",
493 PFID(mdo2fid(child)), rc);
499 if (ma->ma_valid & MA_LAY_GEN)
500 /* If we already have a lsm, the file is not new and we
501 * are about to change the layout, so we have to bump
502 * the generation. It is worth noting that old versions
503 * will be confused by a non-zero gen, that's why
504 * OBD_INCOMPAT_LMM_VER has been introduced */
505 lsm->lsm_layout_gen = ma->ma_layout_gen + 1;
507 /* Start with a null generation for backward
508 * compatiblity with old versions */
509 lsm->lsm_layout_gen = 0;
511 LASSERT_SEQ_IS_MDT(lsm->lsm_object_seq);
513 LASSERT(eadata != NULL);
514 rc = obd_iocontrol(OBD_IOC_LOV_SETEA, lov_exp, 0, &lsm,
519 if (ma->ma_valid & MA_LAY_GEN)
520 lsm->lsm_layout_gen = ma->ma_layout_gen;
522 lsm->lsm_layout_gen = 0;
525 lsm->lsm_object_id = fid_ver_oid(mdd_object_fid(child));
526 lsm->lsm_object_seq = fid_seq(mdd_object_fid(child));
528 * Sometimes, we may truncate some object(without lsm) then open it
529 * (with write flags), so creating lsm above. The Nonzero(truncated)
530 * size should tell ost, since size attr is in charge by OST.
532 if (la->la_size && la->la_valid & LA_SIZE) {
533 struct obd_info *oinfo = &mdd_env_info(env)->mti_oi;
535 memset(oinfo, 0, sizeof(*oinfo));
537 /* When setting attr to ost, FLBKSZ is not needed. */
538 oa->o_valid &= ~OBD_MD_FLBLKSZ;
539 obdo_from_la(oa, la, OBD_MD_FLTYPE | OBD_MD_FLATIME |
540 OBD_MD_FLMTIME | OBD_MD_FLCTIME | OBD_MD_FLSIZE);
543 * XXX: Pack lustre id to OST, in OST, it will be packed by
544 * filter_fid, but can not see what is the usages. So just pack
545 * o_seq o_ver here, maybe fix it after this cycle.
547 obdo_from_inode(oa, NULL,
548 (struct lu_fid *)mdd_object_fid(child), 0);
551 oinfo->oi_capa = NULL;
552 oinfo->oi_policy.l_extent.start = la->la_size;
553 oinfo->oi_policy.l_extent.end = OBD_OBJECT_EOF;
555 rc = obd_punch_rqset(lov_exp, oinfo, oti);
557 CERROR("Error setting attrs for "DFID": rc %d\n",
558 PFID(mdo2fid(child)), rc);
560 CERROR("obd_setattr for "DFID" rc %d\n",
561 PFID(mdo2fid(child)), rc);
567 /* blksize should be changed after create data object */
568 la->la_valid |= LA_BLKSIZE;
569 la->la_blksize = oa->o_blksize;
571 rc = obd_packmd(lov_exp, lmm, lsm);
573 CERROR("Cannot pack lsm, err = %d\n", rc);
576 if (mdd_lov_objid_prepare(mdd, *lmm) != 0) {
577 CERROR("Not have memory for update objid\n");
580 GOTO(out_oti, rc = -ENOMEM);
586 oti_free_cookies(oti);
589 obd_free_memmd(lov_exp, &lsm);
595 * used when destroying orphans and from mds_reint_unlink() when MDS wants to
596 * destroy objects on OSS.
599 int mdd_lovobj_unlink(const struct lu_env *env, struct mdd_device *mdd,
600 struct mdd_object *obj, struct lu_attr *la,
601 struct lov_mds_md *lmm, int lmm_size,
602 struct llog_cookie *logcookies,
605 struct obd_device *obd = mdd2obd_dev(mdd);
606 struct obd_export *lov_exp = obd->u.mds.mds_lov_exp;
607 struct lov_stripe_md *lsm = NULL;
608 struct obd_trans_info *oti = &mdd_env_info(env)->mti_oti;
609 struct obdo *oa = &mdd_env_info(env)->mti_oa;
610 struct lu_site *site = mdd2lu_dev(mdd)->ld_site;
617 rc = obd_unpackmd(lov_exp, &lsm, lmm, lmm_size);
619 CERROR("Error unpack md %p\n", lmm);
622 LASSERT(rc >= sizeof(*lsm));
626 oa->o_id = lsm->lsm_object_id;
627 oa->o_seq = mdt_to_obd_objseq(lu_site2md(site)->ms_node_id);
628 oa->o_mode = la->la_mode & S_IFMT;
629 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
632 if (log_unlink && logcookies) {
633 oa->o_valid |= OBD_MD_FLCOOKIE;
634 oti->oti_logcookies = logcookies;
637 CDEBUG(D_INFO, "destroying OSS object "LPU64":"LPU64"\n", oa->o_seq,
640 rc = obd_destroy(lov_exp, oa, lsm, oti, NULL, NULL);
642 obd_free_memmd(lov_exp, &lsm);
647 * called with obj locked.
649 int mdd_lov_destroy(const struct lu_env *env, struct mdd_device *mdd,
650 struct mdd_object *obj, struct lu_attr *la)
652 struct md_attr *ma = &mdd_env_info(env)->mti_ma;
656 LASSERT(mdd_write_locked(env, obj) != 0);
658 if (unlikely(!S_ISREG(mdd_object_type(obj))))
661 if (unlikely(la->la_nlink != 0)) {
662 CWARN("Attempt to destroy OSS object when nlink == %d\n",
667 ma->ma_lmm_size = mdd_lov_mdsize(env, mdd);
668 ma->ma_lmm = mdd_max_lmm_get(env, mdd);
669 ma->ma_cookie_size = mdd_lov_cookiesize(env, mdd);
670 ma->ma_cookie = mdd_max_cookie_get(env, mdd);
671 if (ma->ma_lmm == NULL || ma->ma_cookie == NULL)
672 RETURN(rc = -ENOMEM);
676 rc = mdd_get_md(env, obj, ma->ma_lmm, &ma->ma_lmm_size,
680 CWARN("Get lov ea failed for "DFID" rc = %d\n",
681 PFID(mdo2fid(obj)), rc);
687 ma->ma_valid = MA_LOV;
689 rc = mdd_unlink_log(env, mdd, obj, ma);
691 CWARN("mds unlink log for "DFID" failed: %d\n",
692 PFID(mdo2fid(obj)), rc);
696 if (ma->ma_valid & MA_COOKIE)
697 rc = mdd_lovobj_unlink(env, mdd, obj, la,
698 ma->ma_lmm, ma->ma_lmm_size,
703 int mdd_declare_unlink_log(const struct lu_env *env, struct mdd_object *obj,
704 struct md_attr *ma, struct thandle *handle)
706 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
713 if (!S_ISREG(lu_object_attr(&obj->mod_obj.mo_lu)))
716 rc = mdd_lmm_get_locked(env, obj, ma);
717 if (rc || !(ma->ma_valid & MA_LOV))
721 if (le32_to_cpu(ma->ma_lmm->lmm_magic) != LOV_MAGIC_V1 &&
722 le32_to_cpu(ma->ma_lmm->lmm_magic) != LOV_MAGIC_V3) {
723 CERROR("%s: invalid LOV_MAGIC %08x on object "DFID"\n",
724 mdd->mdd_obd_dev->obd_name,
725 le32_to_cpu(ma->ma_lmm->lmm_magic),
726 PFID(lu_object_fid(&obj->mod_obj.mo_lu)));
730 stripe = le16_to_cpu(ma->ma_lmm->lmm_stripe_count);
731 if (stripe == LOV_ALL_STRIPES);
732 stripe = mdd2obd_dev(mdd)->u.mds.mds_lov_desc.ld_tgt_count;
734 for (i = 0; i < stripe; i++) {
735 rc = mdd_declare_llog_record(env, mdd,
736 sizeof(struct llog_unlink_rec),
745 int mdd_unlink_log(const struct lu_env *env, struct mdd_device *mdd,
746 struct mdd_object *mdd_cobj, struct md_attr *ma)
748 LASSERT(ma->ma_valid & MA_LOV);
750 if ((ma->ma_cookie_size > 0) &&
751 (mds_log_op_unlink(mdd2obd_dev(mdd), ma->ma_lmm, ma->ma_lmm_size,
752 ma->ma_cookie, ma->ma_cookie_size) > 0)) {
753 CDEBUG(D_HA, "DEBUG: unlink log is added for object "DFID"\n",
754 PFID(mdd_object_fid(mdd_cobj)));
755 ma->ma_valid |= MA_COOKIE;
760 int mdd_log_op_setattr(struct obd_device *obd, __u32 uid, __u32 gid,
761 struct lov_mds_md *lmm, int lmm_size,
762 struct llog_cookie *logcookies, int cookies_size)
764 struct mds_obd *mds = &obd->u.mds;
765 struct lov_stripe_md *lsm = NULL;
766 struct llog_setattr64_rec *lsr;
767 struct llog_ctxt *ctxt;
771 if (IS_ERR(mds->mds_lov_obd))
772 RETURN(PTR_ERR(mds->mds_lov_obd));
774 rc = obd_unpackmd(mds->mds_lov_exp, &lsm, lmm, lmm_size);
778 OBD_ALLOC(lsr, sizeof(*lsr));
780 GOTO(out, rc = -ENOMEM);
782 /* prepare setattr log record */
783 lsr->lsr_hdr.lrh_len = lsr->lsr_tail.lrt_len = sizeof(*lsr);
784 lsr->lsr_hdr.lrh_type = MDS_SETATTR64_REC;
788 /* write setattr log */
789 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
790 rc = llog_add(ctxt, &lsr->lsr_hdr, lsm, logcookies,
791 cookies_size / sizeof(struct llog_cookie));
795 OBD_FREE(lsr, sizeof(*lsr));
797 obd_free_memmd(mds->mds_lov_exp, &lsm);
801 int mdd_setattr_log(const struct lu_env *env, struct mdd_device *mdd,
802 const struct md_attr *ma,
803 struct lov_mds_md *lmm, int lmm_size,
804 struct llog_cookie *logcookies, int cookies_size)
806 struct obd_device *obd = mdd2obd_dev(mdd);
808 /* journal chown/chgrp in llog, just like unlink */
810 CDEBUG(D_INFO, "setattr llog for uid/gid=%lu/%lu\n",
811 (unsigned long)ma->ma_attr.la_uid,
812 (unsigned long)ma->ma_attr.la_gid);
813 return mdd_log_op_setattr(obd, ma->ma_attr.la_uid,
814 ma->ma_attr.la_gid, lmm,
815 lmm_size, logcookies,
821 static int mdd_osc_setattr_async(struct obd_device *obd, __u32 uid, __u32 gid,
822 struct lov_mds_md *lmm, int lmm_size,
823 struct llog_cookie *logcookies, const struct lu_fid *parent,
826 struct mds_obd *mds = &obd->u.mds;
827 struct obd_trans_info oti = { 0 };
828 struct obd_info oinfo = { { { 0 } } };
832 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OST_SETATTR))
835 /* first get memory EA */
836 OBDO_ALLOC(oinfo.oi_oa);
842 rc = obd_unpackmd(mds->mds_lov_exp, &oinfo.oi_md, lmm, lmm_size);
844 CERROR("Error unpack md %p for obj "DFID"\n", lmm,
850 oinfo.oi_oa->o_uid = uid;
851 oinfo.oi_oa->o_gid = gid;
852 oinfo.oi_oa->o_id = oinfo.oi_md->lsm_object_id;
853 oinfo.oi_oa->o_seq = oinfo.oi_md->lsm_object_seq;
854 oinfo.oi_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP |
855 OBD_MD_FLUID | OBD_MD_FLGID;
857 oinfo.oi_oa->o_valid |= OBD_MD_FLCOOKIE;
858 oti.oti_logcookies = logcookies;
861 obdo_from_inode(oinfo.oi_oa, NULL, (struct lu_fid *)parent, 0);
864 /* do async setattr from mds to ost not waiting for responses. */
865 rc = obd_setattr_async(mds->mds_lov_exp, &oinfo, &oti, NULL);
867 CDEBUG(D_INODE, "mds to ost setattr objid 0x"LPX64
868 " on ost error %d\n", oinfo.oi_md->lsm_object_id, rc);
871 obd_free_memmd(mds->mds_lov_exp, &oinfo.oi_md);
872 OBDO_FREE(oinfo.oi_oa);
876 int mdd_lov_setattr_async(const struct lu_env *env, struct mdd_object *obj,
877 struct lov_mds_md *lmm, int lmm_size,
878 struct llog_cookie *logcookies)
880 struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
881 struct obd_device *obd = mdd2obd_dev(mdd);
882 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
883 const struct lu_fid *fid = mdd_object_fid(obj);
887 mdd_read_lock(env, obj, MOR_TGT_CHILD);
888 rc = mdo_attr_get(env, obj, tmp_la, mdd_object_capa(env, obj));
889 mdd_read_unlock(env, obj);
893 rc = mdd_osc_setattr_async(obd, tmp_la->la_uid, tmp_la->la_gid, lmm,
894 lmm_size, logcookies, fid, NULL);
898 static int grouplock_blocking_ast(struct ldlm_lock *lock,
899 struct ldlm_lock_desc *desc,
900 void *data, int flag)
902 struct md_attr *ma = data;
903 struct lustre_handle lockh;
909 case LDLM_CB_BLOCKING :
910 /* lock is canceled */
911 CDEBUG(D_DLMTRACE, "Lock %p is canceled\n", lock);
913 ldlm_lock2handle(lock, &lockh);
914 rc = ldlm_cli_cancel(&lockh);
917 case LDLM_CB_CANCELING :
919 "Lock %p has been canceled, do cleaning\n",
922 if (ma && ma->ma_som)
923 OBD_FREE_PTR(ma->ma_som);
933 static int grouplock_glimpse_ast(struct ldlm_lock *lock, void *data)
935 struct ptlrpc_request *req = data;
941 ma = lock->l_ast_data;
943 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
944 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
946 rc = req_capsule_server_pack(&req->rq_pill);
948 CERROR("failed pack reply: %d\n", rc);
952 lvb = req_capsule_server_get(&req->rq_pill, &RMF_DLM_LVB);
954 if ((ma) && (ma->ma_valid & MA_SOM)) {
955 lvb->lvb_size = ma->ma_som->msd_size;
956 lvb->lvb_blocks = ma->ma_som->msd_blocks;
957 } else if ((ma) && (ma->ma_valid & MA_INODE)) {
958 lvb->lvb_size = ma->ma_attr.la_size;
959 lvb->lvb_blocks = ma->ma_attr.la_blocks;
962 rc = -ELDLM_NO_LOCK_DATA;
967 if (rc == -ELDLM_NO_LOCK_DATA)
968 lustre_pack_reply(req, 1, NULL, NULL);
974 int mdd_file_lock(const struct lu_env *env, struct md_object *obj,
975 struct lov_mds_md *lmm, struct ldlm_extent *extent,
976 struct lustre_handle *lockh)
978 struct ldlm_enqueue_info einfo = { 0 };
979 struct obd_info oinfo = { { { 0 } } };
980 struct obd_device *obd;
981 struct obd_export *lov_exp;
982 struct lov_stripe_md *lsm = NULL;
983 struct md_attr *ma = NULL;
987 obd = mdo2mdd(obj)->mdd_obd_dev;
988 lov_exp = obd->u.mds.mds_lov_exp;
990 obd_unpackmd(lov_exp, &lsm, lmm,
991 lov_mds_md_size(lmm->lmm_stripe_count, lmm->lmm_magic));
995 GOTO(out, rc = -ENOMEM);
997 OBD_ALLOC_PTR(ma->ma_som);
998 if (ma->ma_som == NULL)
999 GOTO(out, rc = -ENOMEM);
1001 ma->ma_need = MA_SOM | MA_INODE;
1002 mo_attr_get(env, obj, ma);
1004 einfo.ei_type = LDLM_EXTENT;
1005 einfo.ei_mode = LCK_GROUP;
1006 einfo.ei_cb_bl = grouplock_blocking_ast;
1007 einfo.ei_cb_cp = ldlm_completion_ast;
1008 einfo.ei_cb_gl = grouplock_glimpse_ast;
1010 if (ma->ma_valid & (MA_SOM | MA_INODE))
1011 einfo.ei_cbdata = ma;
1013 einfo.ei_cbdata = NULL;
1015 memset(&oinfo.oi_policy, 0, sizeof(oinfo.oi_policy));
1016 oinfo.oi_policy.l_extent = *extent;
1017 oinfo.oi_lockh = lockh;
1021 rc = obd_enqueue(lov_exp, &oinfo, &einfo, NULL);
1022 /* ei_cbdata is used as a free flag at exit */
1024 einfo.ei_cbdata = NULL;
1026 obd_unpackmd(lov_exp, &lsm, NULL, 0);
1029 /* ma is freed if not used as callback data */
1030 if ((einfo.ei_cbdata == NULL) && ma && ma->ma_som)
1031 OBD_FREE_PTR(ma->ma_som);
1032 if ((einfo.ei_cbdata == NULL) && ma)
1038 int mdd_file_unlock(const struct lu_env *env, struct md_object *obj,
1039 struct lov_mds_md *lmm, struct lustre_handle *lockh)
1041 struct obd_device *obd;
1042 struct obd_export *lov_exp;
1043 struct lov_stripe_md *lsm = NULL;
1047 LASSERT(lustre_handle_is_used(lockh));
1049 obd = mdo2mdd(obj)->mdd_obd_dev;
1050 lov_exp = obd->u.mds.mds_lov_exp;
1052 obd_unpackmd(lov_exp, &lsm, lmm,
1053 lov_mds_md_size(lmm->lmm_stripe_count, lmm->lmm_magic));
1055 rc = obd_cancel(lov_exp, lsm, LCK_GROUP, lockh);
1057 obd_unpackmd(lov_exp, &lsm, NULL, 0);
1062 /* file lov is in ma->ma_lmm */
1063 /* requested lov is in info->mti_spec.u.sp_ea.eadata */
1064 int mdd_lum_lmm_cmp(const struct lu_env *env, struct md_object *cobj,
1065 const struct md_op_spec *spec, struct md_attr *ma)
1067 struct obd_export *lov_exp =
1068 mdd2obd_dev(mdo2mdd(cobj))->u.mds.mds_lov_exp;
1069 struct lov_mds_md *lmm = ma->ma_lmm;
1070 struct lov_user_md_v3 *lum =
1071 (struct lov_user_md_v3 *)(spec->u.sp_ea.eadata);
1072 struct lov_stripe_md *lsm = NULL;
1076 rc = obd_unpackmd(lov_exp, &lsm, lmm,
1077 lov_mds_md_size(lmm->lmm_stripe_count,
1079 ma->ma_layout_gen = lsm->lsm_layout_gen;
1080 ma->ma_valid |= MA_LAY_GEN;
1082 rc = lov_lum_swab_if_needed(lum, &lmm_magic, NULL);
1086 rc = lov_lum_lsm_cmp((struct lov_user_md *)lum, lsm);
1088 GOTO(out, rc); /* keep GOTO to for traces */
1092 obd_unpackmd(lov_exp, &lsm, NULL, 0);