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, 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).
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
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdd/mdd_object.c
38 * Lustre Metadata Server (mdd) routines
40 * Author: Wang Di <wangdi@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_MDS
45 #include <linux/module.h>
47 #include <obd_class.h>
48 #include <obd_support.h>
49 #include <lprocfs_status.h>
50 /* fid_be_cpu(), fid_cpu_to_be(). */
51 #include <lustre_fid.h>
53 #include <lustre_idmap.h>
54 #include <lustre_param.h>
55 #include <lustre_mds.h>
56 #include <lustre/lustre_idl.h>
58 #include "mdd_internal.h"
60 static const struct lu_object_operations mdd_lu_obj_ops;
61 extern cfs_mem_cache_t *mdd_object_kmem;
63 static int mdd_xattr_get(const struct lu_env *env,
64 struct md_object *obj, struct lu_buf *buf,
67 int mdd_data_get(const struct lu_env *env, struct mdd_object *obj,
70 if (mdd_object_exists(obj) == 0) {
71 CERROR("%s: object "DFID" not found: rc = -2\n",
72 mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
75 mdo_data_get(env, obj, data);
79 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
80 struct lu_attr *la, struct lustre_capa *capa)
82 if (mdd_object_exists(obj) == 0) {
83 CERROR("%s: object "DFID" not found: rc = -2\n",
84 mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
87 return mdo_attr_get(env, obj, la, capa);
90 static void mdd_flags_xlate(struct mdd_object *obj, __u32 flags)
92 obj->mod_flags &= ~(APPEND_OBJ|IMMUTE_OBJ);
94 if (flags & LUSTRE_APPEND_FL)
95 obj->mod_flags |= APPEND_OBJ;
97 if (flags & LUSTRE_IMMUTABLE_FL)
98 obj->mod_flags |= IMMUTE_OBJ;
101 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
103 struct mdd_thread_info *info;
105 info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
106 LASSERT(info != NULL);
110 const struct lu_name *mdd_name_get_const(const struct lu_env *env,
111 const void *area, ssize_t len)
113 struct lu_name *lname;
115 lname = &mdd_env_info(env)->mti_name;
116 lname->ln_name = area;
117 lname->ln_namelen = len;
121 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
125 buf = &mdd_env_info(env)->mti_buf;
131 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
132 const void *area, ssize_t len)
136 buf = &mdd_env_info(env)->mti_buf;
137 buf->lb_buf = (void *)area;
142 struct lu_object *mdd_object_alloc(const struct lu_env *env,
143 const struct lu_object_header *hdr,
146 struct mdd_object *mdd_obj;
148 OBD_SLAB_ALLOC_PTR_GFP(mdd_obj, mdd_object_kmem, CFS_ALLOC_IO);
149 if (mdd_obj != NULL) {
152 o = mdd2lu_obj(mdd_obj);
153 lu_object_init(o, NULL, d);
154 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
155 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
156 mdd_obj->mod_count = 0;
157 o->lo_ops = &mdd_lu_obj_ops;
164 static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
165 const struct lu_object_conf *unused)
167 struct mdd_device *d = lu2mdd_dev(o->lo_dev);
168 struct mdd_object *mdd_obj = lu2mdd_obj(o);
169 struct lu_object *below;
170 struct lu_device *under;
173 mdd_obj->mod_cltime = 0;
174 under = &d->mdd_child->dd_lu_dev;
175 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
176 mdd_pdlock_init(mdd_obj);
178 RETURN(PTR_ERR(below));
180 lu_object_add(o, below);
185 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
187 if (lu_object_exists(o))
188 return mdd_get_flags(env, lu2mdd_obj(o));
193 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
195 struct mdd_object *mdd = lu2mdd_obj(o);
198 OBD_SLAB_FREE_PTR(mdd, mdd_object_kmem);
201 static int mdd_object_print(const struct lu_env *env, void *cookie,
202 lu_printer_t p, const struct lu_object *o)
204 struct mdd_object *mdd = lu2mdd_obj((struct lu_object *)o);
205 return (*p)(env, cookie, LUSTRE_MDD_NAME"-object@%p(open_count=%d, "
206 "valid=%x, cltime="LPU64", flags=%lx)",
207 mdd, mdd->mod_count, mdd->mod_valid,
208 mdd->mod_cltime, mdd->mod_flags);
211 static const struct lu_object_operations mdd_lu_obj_ops = {
212 .loo_object_init = mdd_object_init,
213 .loo_object_start = mdd_object_start,
214 .loo_object_free = mdd_object_free,
215 .loo_object_print = mdd_object_print,
218 struct mdd_object *mdd_object_find(const struct lu_env *env,
219 struct mdd_device *d,
220 const struct lu_fid *f)
222 return md2mdd_obj(md_object_find_slice(env, &d->mdd_md_dev, f));
225 /* Returns the full path to this fid, as of changelog record recno. */
226 int mdd_get_flags(const struct lu_env *env, struct mdd_object *obj)
228 struct lu_attr *la = &mdd_env_info(env)->mti_la;
232 rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
234 mdd_flags_xlate(obj, la->la_flags);
240 * No permission check is needed.
242 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
245 struct mdd_object *mdd_obj = md2mdd_obj(obj);
250 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
251 mdd_object_capa(env, md2mdd_obj(obj)));
252 if ((ma->ma_need & MA_INODE) != 0 && mdd_is_dead_obj(mdd_obj))
253 ma->ma_attr.la_nlink = 0;
259 * No permission check is needed.
261 static int mdd_xattr_get(const struct lu_env *env,
262 struct md_object *obj, struct lu_buf *buf,
265 struct mdd_object *mdd_obj = md2mdd_obj(obj);
270 if (mdd_object_exists(mdd_obj) == 0) {
271 CERROR("%s: object "DFID" not found: rc = -2\n",
272 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
276 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
277 rc = mdo_xattr_get(env, mdd_obj, buf, name,
278 mdd_object_capa(env, mdd_obj));
279 mdd_read_unlock(env, mdd_obj);
285 * Permission check is done when open,
286 * no need check again.
288 static int mdd_readlink(const struct lu_env *env, struct md_object *obj,
291 struct mdd_object *mdd_obj = md2mdd_obj(obj);
292 struct dt_object *next;
297 if (mdd_object_exists(mdd_obj) == 0) {
298 CERROR("%s: object "DFID" not found: rc = -2\n",
299 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
303 next = mdd_object_child(mdd_obj);
304 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
305 rc = next->do_body_ops->dbo_read(env, next, buf, &pos,
306 mdd_object_capa(env, mdd_obj));
307 mdd_read_unlock(env, mdd_obj);
312 * No permission check is needed.
314 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
317 struct mdd_object *mdd_obj = md2mdd_obj(obj);
322 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
323 rc = mdo_xattr_list(env, mdd_obj, buf, mdd_object_capa(env, mdd_obj));
324 mdd_read_unlock(env, mdd_obj);
329 int mdd_declare_object_create_internal(const struct lu_env *env,
330 struct mdd_object *p,
331 struct mdd_object *c,
332 struct lu_attr *attr,
333 struct thandle *handle,
334 const struct md_op_spec *spec)
336 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
337 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
338 const struct dt_index_features *feat = spec->sp_feat;
342 if (feat != &dt_directory_features && feat != NULL) {
343 dof->dof_type = DFT_INDEX;
344 dof->u.dof_idx.di_feat = feat;
347 dof->dof_type = dt_mode_to_dft(attr->la_mode);
348 if (dof->dof_type == DFT_REGULAR) {
349 dof->u.dof_reg.striped =
350 md_should_create(spec->sp_cr_flags);
351 if (spec->sp_cr_flags & MDS_OPEN_HAS_EA)
352 dof->u.dof_reg.striped = 0;
353 /* is this replay? */
355 dof->u.dof_reg.striped = 0;
359 rc = mdo_declare_create_obj(env, c, attr, hint, dof, handle);
364 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
365 struct mdd_object *c, struct lu_attr *attr,
366 struct thandle *handle,
367 const struct md_op_spec *spec)
369 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
370 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
374 LASSERT(!mdd_object_exists(c));
376 rc = mdo_create_obj(env, c, attr, hint, dof, handle);
378 LASSERT(ergo(rc == 0, mdd_object_exists(c)));
384 * Make sure the ctime is increased only.
386 static inline int mdd_attr_check(const struct lu_env *env,
387 struct mdd_object *obj,
388 struct lu_attr *attr)
390 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
394 if (attr->la_valid & LA_CTIME) {
395 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
399 if (attr->la_ctime < tmp_la->la_ctime)
400 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
401 else if (attr->la_valid == LA_CTIME &&
402 attr->la_ctime == tmp_la->la_ctime)
403 attr->la_valid &= ~LA_CTIME;
408 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
409 const struct lu_attr *attr, struct thandle *handle,
415 rc = mdo_attr_set(env, obj, attr, handle, mdd_object_capa(env, obj));
416 #ifdef CONFIG_FS_POSIX_ACL
417 if (!rc && (attr->la_valid & LA_MODE) && needacl)
418 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
423 int mdd_attr_check_set_internal(const struct lu_env *env,
424 struct mdd_object *obj, struct lu_attr *attr,
425 struct thandle *handle, int needacl)
430 rc = mdd_attr_check(env, obj, attr);
435 rc = mdd_attr_set_internal(env, obj, attr, handle, needacl);
440 * This gives the same functionality as the code between
441 * sys_chmod and inode_setattr
442 * chown_common and inode_setattr
443 * utimes and inode_setattr
444 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
446 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
447 struct lu_attr *la, const unsigned long flags)
449 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
457 /* Do not permit change file type */
458 if (la->la_valid & LA_TYPE)
461 /* They should not be processed by setattr */
462 if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
465 /* export destroy does not have ->le_ses, but we may want
466 * to drop LUSTRE_SOM_FL. */
467 uc = lu_ucred_check(env);
471 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
475 if (la->la_valid == LA_CTIME) {
476 if (!(flags & MDS_PERM_BYPASS))
477 /* This is only for set ctime when rename's source is
479 rc = mdd_may_delete(env, NULL, obj, tmp_la, NULL, 1, 0);
480 if (rc == 0 && la->la_ctime <= tmp_la->la_ctime)
481 la->la_valid &= ~LA_CTIME;
485 if (la->la_valid == LA_ATIME) {
486 /* This is atime only set for read atime update on close. */
487 if (la->la_atime >= tmp_la->la_atime &&
488 la->la_atime < (tmp_la->la_atime +
489 mdd_obj2mdd_dev(obj)->mdd_atime_diff))
490 la->la_valid &= ~LA_ATIME;
494 /* Check if flags change. */
495 if (la->la_valid & LA_FLAGS) {
496 unsigned int oldflags = 0;
497 unsigned int newflags = la->la_flags &
498 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
500 if ((uc->uc_fsuid != tmp_la->la_uid) &&
501 !md_capable(uc, CFS_CAP_FOWNER))
504 /* XXX: the IMMUTABLE and APPEND_ONLY flags can
505 * only be changed by the relevant capability. */
506 if (mdd_is_immutable(obj))
507 oldflags |= LUSTRE_IMMUTABLE_FL;
508 if (mdd_is_append(obj))
509 oldflags |= LUSTRE_APPEND_FL;
510 if ((oldflags ^ newflags) &&
511 !md_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
514 if (!S_ISDIR(tmp_la->la_mode))
515 la->la_flags &= ~LUSTRE_DIRSYNC_FL;
518 if ((mdd_is_immutable(obj) || mdd_is_append(obj)) &&
519 (la->la_valid & ~LA_FLAGS) &&
520 !(flags & MDS_PERM_BYPASS))
523 /* Check for setting the obj time. */
524 if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
525 !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
526 if ((uc->uc_fsuid != tmp_la->la_uid) &&
527 !md_capable(uc, CFS_CAP_FOWNER)) {
528 rc = mdd_permission_internal(env, obj, tmp_la,
535 if (la->la_valid & LA_KILL_SUID) {
536 la->la_valid &= ~LA_KILL_SUID;
537 if ((tmp_la->la_mode & S_ISUID) &&
538 !(la->la_valid & LA_MODE)) {
539 la->la_mode = tmp_la->la_mode;
540 la->la_valid |= LA_MODE;
542 la->la_mode &= ~S_ISUID;
545 if (la->la_valid & LA_KILL_SGID) {
546 la->la_valid &= ~LA_KILL_SGID;
547 if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
548 (S_ISGID | S_IXGRP)) &&
549 !(la->la_valid & LA_MODE)) {
550 la->la_mode = tmp_la->la_mode;
551 la->la_valid |= LA_MODE;
553 la->la_mode &= ~S_ISGID;
556 /* Make sure a caller can chmod. */
557 if (la->la_valid & LA_MODE) {
558 if (!(flags & MDS_PERM_BYPASS) &&
559 (uc->uc_fsuid != tmp_la->la_uid) &&
560 !md_capable(uc, CFS_CAP_FOWNER))
563 if (la->la_mode == (cfs_umode_t) -1)
564 la->la_mode = tmp_la->la_mode;
566 la->la_mode = (la->la_mode & S_IALLUGO) |
567 (tmp_la->la_mode & ~S_IALLUGO);
569 /* Also check the setgid bit! */
570 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
571 la->la_gid : tmp_la->la_gid) &&
572 !md_capable(uc, CFS_CAP_FSETID))
573 la->la_mode &= ~S_ISGID;
575 la->la_mode = tmp_la->la_mode;
578 /* Make sure a caller can chown. */
579 if (la->la_valid & LA_UID) {
580 if (la->la_uid == (uid_t) -1)
581 la->la_uid = tmp_la->la_uid;
582 if (((uc->uc_fsuid != tmp_la->la_uid) ||
583 (la->la_uid != tmp_la->la_uid)) &&
584 !md_capable(uc, CFS_CAP_CHOWN))
587 /* If the user or group of a non-directory has been
588 * changed by a non-root user, remove the setuid bit.
589 * 19981026 David C Niemi <niemi@tux.org>
591 * Changed this to apply to all users, including root,
592 * to avoid some races. This is the behavior we had in
593 * 2.0. The check for non-root was definitely wrong
594 * for 2.2 anyway, as it should have been using
595 * CAP_FSETID rather than fsuid -- 19990830 SD. */
596 if (((tmp_la->la_mode & S_ISUID) == S_ISUID) &&
597 !S_ISDIR(tmp_la->la_mode)) {
598 la->la_mode &= ~S_ISUID;
599 la->la_valid |= LA_MODE;
603 /* Make sure caller can chgrp. */
604 if (la->la_valid & LA_GID) {
605 if (la->la_gid == (gid_t) -1)
606 la->la_gid = tmp_la->la_gid;
607 if (((uc->uc_fsuid != tmp_la->la_uid) ||
608 ((la->la_gid != tmp_la->la_gid) &&
609 !lustre_in_group_p(uc, la->la_gid))) &&
610 !md_capable(uc, CFS_CAP_CHOWN))
613 /* Likewise, if the user or group of a non-directory
614 * has been changed by a non-root user, remove the
615 * setgid bit UNLESS there is no group execute bit
616 * (this would be a file marked for mandatory
617 * locking). 19981026 David C Niemi <niemi@tux.org>
619 * Removed the fsuid check (see the comment above) --
621 if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
622 (S_ISGID | S_IXGRP)) && !S_ISDIR(tmp_la->la_mode)) {
623 la->la_mode &= ~S_ISGID;
624 la->la_valid |= LA_MODE;
628 /* For both Size-on-MDS case and truncate case,
629 * "la->la_valid & (LA_SIZE | LA_BLOCKS)" are ture.
630 * We distinguish them by "flags & MDS_SOM".
631 * For SOM case, it is true, the MAY_WRITE perm has been checked
632 * when open, no need check again. For truncate case, it is false,
633 * the MAY_WRITE perm should be checked here. */
634 if (flags & MDS_SOM) {
635 /* For the "Size-on-MDS" setattr update, merge coming
636 * attributes with the set in the inode. BUG 10641 */
637 if ((la->la_valid & LA_ATIME) &&
638 (la->la_atime <= tmp_la->la_atime))
639 la->la_valid &= ~LA_ATIME;
641 /* OST attributes do not have a priority over MDS attributes,
642 * so drop times if ctime is equal. */
643 if ((la->la_valid & LA_CTIME) &&
644 (la->la_ctime <= tmp_la->la_ctime))
645 la->la_valid &= ~(LA_MTIME | LA_CTIME);
647 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
648 if (!((flags & MDS_OWNEROVERRIDE) &&
649 (uc->uc_fsuid == tmp_la->la_uid)) &&
650 !(flags & MDS_PERM_BYPASS)) {
651 rc = mdd_permission_internal(env, obj,
657 if (la->la_valid & LA_CTIME) {
658 /* The pure setattr, it has the priority over what is
659 * already set, do not drop it if ctime is equal. */
660 if (la->la_ctime < tmp_la->la_ctime)
661 la->la_valid &= ~(LA_ATIME | LA_MTIME |
669 /** Store a data change changelog record
670 * If this fails, we must fail the whole transaction; we don't
671 * want the change to commit without the log entry.
672 * \param mdd_obj - mdd_object of change
673 * \param handle - transacion handle
675 static int mdd_changelog_data_store(const struct lu_env *env,
676 struct mdd_device *mdd,
677 enum changelog_rec_type type,
678 int flags, struct mdd_object *mdd_obj,
679 struct thandle *handle)
681 const struct lu_fid *tfid;
682 struct llog_changelog_rec *rec;
688 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
690 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
693 LASSERT(mdd_obj != NULL);
694 LASSERT(handle != NULL);
696 tfid = mdo2fid(mdd_obj);
698 if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
699 cfs_time_before_64(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
700 /* Don't need multiple updates in this log */
701 /* Don't check under lock - no big deal if we get an extra
706 reclen = llog_data_len(sizeof(*rec));
707 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
708 if (buf->lb_buf == NULL)
712 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
713 rec->cr.cr_type = (__u32)type;
714 rec->cr.cr_tfid = *tfid;
715 rec->cr.cr_namelen = 0;
716 mdd_obj->mod_cltime = cfs_time_current_64();
718 rc = mdd_changelog_store(env, mdd, rec, handle);
723 int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
724 int flags, struct md_object *obj)
726 struct thandle *handle;
727 struct mdd_object *mdd_obj = md2mdd_obj(obj);
728 struct mdd_device *mdd = mdo2mdd(obj);
732 handle = mdd_trans_create(env, mdd);
734 RETURN(PTR_ERR(handle));
736 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
740 rc = mdd_trans_start(env, mdd, handle);
744 rc = mdd_changelog_data_store(env, mdd, type, flags, mdd_obj,
748 mdd_trans_stop(env, mdd, rc, handle);
754 * Save LMA extended attributes with data from \a ma.
756 * HSM and Size-On-MDS data will be extracted from \ma if they are valid, if
757 * not, LMA EA will be first read from disk, modified and write back.
760 /* Precedence for choosing record type when multiple
761 * attributes change: setattr > mtime > ctime > atime
762 * (ctime changes when mtime does, plus chmod/chown.
763 * atime and ctime are independent.) */
764 static int mdd_attr_set_changelog(const struct lu_env *env,
765 struct md_object *obj, struct thandle *handle,
768 struct mdd_device *mdd = mdo2mdd(obj);
771 bits = (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
772 bits |= (valid & LA_MTIME) ? 1 << CL_MTIME : 0;
773 bits |= (valid & LA_CTIME) ? 1 << CL_CTIME : 0;
774 bits |= (valid & LA_ATIME) ? 1 << CL_ATIME : 0;
775 bits = bits & mdd->mdd_cl.mc_mask;
779 /* The record type is the lowest non-masked set bit */
780 while (bits && ((bits & 1) == 0)) {
785 /* FYI we only store the first CLF_FLAGMASK bits of la_valid */
786 return mdd_changelog_data_store(env, mdd, type, (int)valid,
787 md2mdd_obj(obj), handle);
790 static int mdd_declare_attr_set(const struct lu_env *env,
791 struct mdd_device *mdd,
792 struct mdd_object *obj,
793 const struct lu_attr *attr,
794 struct thandle *handle)
798 rc = mdo_declare_attr_set(env, obj, attr, handle);
802 #ifdef CONFIG_FS_POSIX_ACL
803 if (attr->la_valid & LA_MODE) {
804 mdd_read_lock(env, obj, MOR_TGT_CHILD);
805 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
806 XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
807 mdd_read_unlock(env, obj);
808 if (rc == -EOPNOTSUPP || rc == -ENODATA)
814 struct lu_buf *buf = mdd_buf_get(env, NULL, rc);
815 rc = mdo_declare_xattr_set(env, obj, buf,
816 XATTR_NAME_ACL_ACCESS, 0,
824 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
828 /* set attr and LOV EA at once, return updated attr */
829 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
830 const struct md_attr *ma)
832 struct mdd_object *mdd_obj = md2mdd_obj(obj);
833 struct mdd_device *mdd = mdo2mdd(obj);
834 struct thandle *handle;
835 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
836 const struct lu_attr *la = &ma->ma_attr;
840 /* we do not use ->attr_set() for LOV/SOM/HSM EA any more */
841 LASSERT((ma->ma_valid & MA_LOV) == 0);
842 LASSERT((ma->ma_valid & MA_HSM) == 0);
843 LASSERT((ma->ma_valid & MA_SOM) == 0);
845 *la_copy = ma->ma_attr;
846 rc = mdd_fix_attr(env, mdd_obj, la_copy, ma->ma_attr_flags);
850 /* setattr on "close" only change atime, or do nothing */
851 if (la->la_valid == LA_ATIME && la_copy->la_valid == 0)
854 handle = mdd_trans_create(env, mdd);
856 RETURN(PTR_ERR(handle));
858 rc = mdd_declare_attr_set(env, mdd, mdd_obj, la, handle);
862 rc = mdd_trans_start(env, mdd, handle);
866 /* permission changes may require sync operation */
867 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
868 handle->th_sync |= !!mdd->mdd_sync_permission;
870 if (la->la_valid & (LA_MTIME | LA_CTIME))
871 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
872 la->la_mtime, la->la_ctime);
874 if (la_copy->la_valid & LA_FLAGS) {
875 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
877 mdd_flags_xlate(mdd_obj, la_copy->la_flags);
878 } else if (la_copy->la_valid) { /* setattr */
879 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
883 rc = mdd_attr_set_changelog(env, obj, handle,
886 mdd_trans_stop(env, mdd, rc, handle);
890 static int mdd_xattr_sanity_check(const struct lu_env *env,
891 struct mdd_object *obj)
893 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
894 struct lu_ucred *uc = lu_ucred_assert(env);
898 if (mdd_is_immutable(obj) || mdd_is_append(obj))
901 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
905 if ((uc->uc_fsuid != tmp_la->la_uid) &&
906 !md_capable(uc, CFS_CAP_FOWNER))
912 static int mdd_declare_xattr_set(const struct lu_env *env,
913 struct mdd_device *mdd,
914 struct mdd_object *obj,
915 const struct lu_buf *buf,
917 int fl, struct thandle *handle)
921 rc = mdo_declare_xattr_set(env, obj, buf, name, fl, handle);
925 /* Only record user xattr changes */
926 if ((strncmp("user.", name, 5) == 0)) {
927 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
932 /* If HSM data is modified, this could add a changelog */
933 if (strncmp(XATTR_NAME_HSM, name, sizeof(XATTR_NAME_HSM) - 1) == 0)
934 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
936 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
941 * Compare current and future data of HSM EA and add a changelog if needed.
943 * Caller should have write-locked \param obj.
945 * \param buf - Future HSM EA content.
946 * \retval 0 if no changelog is needed or changelog was added properly.
947 * \retval -ve errno if there was a problem
949 static int mdd_hsm_update_locked(const struct lu_env *env,
950 struct md_object *obj,
951 const struct lu_buf *buf,
952 struct thandle *handle)
954 struct mdd_thread_info *info = mdd_env_info(env);
955 struct mdd_device *mdd = mdo2mdd(obj);
956 struct mdd_object *mdd_obj = md2mdd_obj(obj);
957 struct lu_buf *current_buf = &info->mti_buf;
958 struct md_hsm *current_mh;
959 struct md_hsm *new_mh;
963 OBD_ALLOC_PTR(current_mh);
964 if (current_mh == NULL)
967 /* Read HSM attrs from disk */
968 current_buf->lb_buf = info->mti_xattr_buf;
969 current_buf->lb_len = sizeof(info->mti_xattr_buf);
970 CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
971 rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM,
972 mdd_object_capa(env, mdd_obj));
973 rc = lustre_buf2hsm(info->mti_xattr_buf, rc, current_mh);
974 if (rc < 0 && rc != -ENODATA)
976 else if (rc == -ENODATA)
977 current_mh->mh_flags = 0;
979 /* Map future HSM xattr */
980 OBD_ALLOC_PTR(new_mh);
982 GOTO(free, rc = -ENOMEM);
983 lustre_buf2hsm(buf->lb_buf, buf->lb_len, new_mh);
985 /* If HSM flags are different, add a changelog */
987 if (current_mh->mh_flags != new_mh->mh_flags) {
989 hsm_set_cl_event(&flags, HE_STATE);
990 if (new_mh->mh_flags & HS_DIRTY)
991 hsm_set_cl_flags(&flags, CLF_HSM_DIRTY);
993 rc = mdd_changelog_data_store(env, mdd, CL_HSM, flags, mdd_obj,
997 OBD_FREE_PTR(new_mh);
999 OBD_FREE_PTR(current_mh);
1005 * The caller should guarantee to update the object ctime
1006 * after xattr_set if needed.
1008 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
1009 const struct lu_buf *buf, const char *name,
1012 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1013 struct mdd_device *mdd = mdo2mdd(obj);
1014 struct thandle *handle;
1018 if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
1019 rc = mdd_acl_set(env, mdd_obj, buf, fl);
1023 rc = mdd_xattr_sanity_check(env, mdd_obj);
1027 handle = mdd_trans_create(env, mdd);
1029 RETURN(PTR_ERR(handle));
1031 rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
1035 rc = mdd_trans_start(env, mdd, handle);
1039 /* security-replated changes may require sync */
1040 if (!strcmp(name, XATTR_NAME_ACL_ACCESS))
1041 handle->th_sync |= !!mdd->mdd_sync_permission;
1043 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1045 if (strncmp(XATTR_NAME_HSM, name, sizeof(XATTR_NAME_HSM) - 1) == 0) {
1046 rc = mdd_hsm_update_locked(env, obj, buf, handle);
1048 mdd_write_unlock(env, mdd_obj);
1053 rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle,
1054 mdd_object_capa(env, mdd_obj));
1055 mdd_write_unlock(env, mdd_obj);
1059 /* Only record system & user xattr changes */
1060 if (strncmp(XATTR_USER_PREFIX, name,
1061 sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1062 strncmp(POSIX_ACL_XATTR_ACCESS, name,
1063 sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1064 strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1065 sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1066 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1070 mdd_trans_stop(env, mdd, rc, handle);
1075 static int mdd_declare_xattr_del(const struct lu_env *env,
1076 struct mdd_device *mdd,
1077 struct mdd_object *obj,
1079 struct thandle *handle)
1083 rc = mdo_declare_xattr_del(env, obj, name, handle);
1087 /* Only record user xattr changes */
1088 if ((strncmp("user.", name, 5) == 0))
1089 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1095 * The caller should guarantee to update the object ctime
1096 * after xattr_set if needed.
1098 int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1101 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1102 struct mdd_device *mdd = mdo2mdd(obj);
1103 struct thandle *handle;
1107 rc = mdd_xattr_sanity_check(env, mdd_obj);
1111 handle = mdd_trans_create(env, mdd);
1113 RETURN(PTR_ERR(handle));
1115 rc = mdd_declare_xattr_del(env, mdd, mdd_obj, name, handle);
1119 rc = mdd_trans_start(env, mdd, handle);
1123 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1124 rc = mdo_xattr_del(env, mdd_obj, name, handle,
1125 mdd_object_capa(env, mdd_obj));
1126 mdd_write_unlock(env, mdd_obj);
1130 /* Only record system & user xattr changes */
1131 if (strncmp(XATTR_USER_PREFIX, name,
1132 sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1133 strncmp(POSIX_ACL_XATTR_ACCESS, name,
1134 sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1135 strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1136 sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1137 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1141 mdd_trans_stop(env, mdd, rc, handle);
1147 * read lov EA of an object
1148 * return the lov EA in an allocated lu_buf
1150 static struct lu_buf *mdd_get_lov_ea(const struct lu_env *env,
1151 struct mdd_object *obj)
1153 struct lu_buf *buf = &mdd_env_info(env)->mti_big_buf;
1154 struct lu_buf *lmm_buf = NULL;
1159 rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_LOV,
1160 mdd_object_capa(env, obj));
1162 if (rc == -ERANGE) {
1163 /* mti_big_buf is allocated but is too small
1164 * we need to increase it */
1165 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1167 if (buf->lb_buf == NULL)
1168 GOTO(out, rc = -ENOMEM);
1176 GOTO(out, rc = -ENODATA);
1179 if (memcmp(buf, &LU_BUF_NULL, sizeof(*buf)) == 0) {
1180 /* mti_big_buf was not allocated, so we have to
1181 * allocate it based on the ea size */
1182 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1184 if (buf->lb_buf == NULL)
1185 GOTO(out, rc = -ENOMEM);
1189 OBD_ALLOC_PTR(lmm_buf);
1191 GOTO(out, rc = -ENOMEM);
1193 OBD_ALLOC(lmm_buf->lb_buf, sz);
1194 if (!lmm_buf->lb_buf)
1195 GOTO(free, rc = -ENOMEM);
1197 memcpy(lmm_buf->lb_buf, buf->lb_buf, sz);
1198 lmm_buf->lb_len = sz;
1204 OBD_FREE_PTR(lmm_buf);
1213 * check if layout swapping between 2 objects is allowed
1215 * - same type of objects
1216 * - same owner/group (so quotas are still valid)
1218 static int mdd_layout_swap_allowed(const struct lu_env *env,
1219 struct mdd_object *o1,
1220 struct mdd_object *o2)
1222 const struct lu_fid *fid1, *fid2;
1224 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
1231 if (!fid_is_norm(fid1) || !fid_is_norm(fid2) ||
1232 (mdd_object_type(o1) != mdd_object_type(o2)))
1235 tmp_la->la_valid = 0;
1236 rc = mdd_la_get(env, o1, tmp_la, BYPASS_CAPA);
1239 uid = tmp_la->la_uid;
1240 gid = tmp_la->la_gid;
1242 tmp_la->la_valid = 0;
1243 rc = mdd_la_get(env, o2, tmp_la, BYPASS_CAPA);
1247 if ((uid != tmp_la->la_uid) || (gid != tmp_la->la_gid))
1254 * swap layouts between 2 lustre objects
1256 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
1257 struct md_object *obj2, __u64 flags)
1259 struct mdd_object *o1, *o2, *fst_o, *snd_o;
1260 struct lu_buf *lmm1_buf = NULL, *lmm2_buf = NULL;
1261 struct lu_buf *fst_buf, *snd_buf;
1262 struct lov_mds_md *fst_lmm, *snd_lmm, *old_fst_lmm = NULL;
1263 struct thandle *handle;
1264 struct mdd_device *mdd = mdo2mdd(obj1);
1266 __u16 fst_gen, snd_gen;
1269 /* we have to sort the 2 obj, so locking will always
1270 * be in the same order, even in case of 2 concurrent swaps */
1271 rc = lu_fid_cmp(mdo2fid(md2mdd_obj(obj1)),
1272 mdo2fid(md2mdd_obj(obj2)));
1278 o1 = md2mdd_obj(obj1);
1279 o2 = md2mdd_obj(obj2);
1281 o1 = md2mdd_obj(obj2);
1282 o2 = md2mdd_obj(obj1);
1285 /* check if layout swapping is allowed */
1286 rc = mdd_layout_swap_allowed(env, o1, o2);
1290 handle = mdd_trans_create(env, mdd);
1292 RETURN(PTR_ERR(handle));
1294 /* objects are already sorted */
1295 mdd_write_lock(env, o1, MOR_TGT_CHILD);
1296 mdd_write_lock(env, o2, MOR_TGT_CHILD);
1298 lmm1_buf = mdd_get_lov_ea(env, o1);
1299 if (IS_ERR(lmm1_buf)) {
1300 rc = PTR_ERR(lmm1_buf);
1306 lmm2_buf = mdd_get_lov_ea(env, o2);
1307 if (IS_ERR(lmm2_buf)) {
1308 rc = PTR_ERR(lmm2_buf);
1314 /* swapping 2 non existant layouts is a success */
1315 if ((lmm1_buf == NULL) && (lmm2_buf == NULL))
1316 GOTO(unlock, rc = 0);
1318 /* to help inode migration between MDT, it is better to
1319 * start by the no layout file (if one), so we order the swap */
1320 if (lmm1_buf == NULL) {
1332 /* lmm and generation layout initialization */
1334 fst_lmm = fst_buf->lb_buf;
1335 fst_gen = le16_to_cpu(fst_lmm->lmm_layout_gen);
1342 snd_lmm = snd_buf->lb_buf;
1343 snd_gen = le16_to_cpu(snd_lmm->lmm_layout_gen);
1349 /* save the orignal lmm common header of first file
1350 * to be able to roll back */
1351 OBD_ALLOC_PTR(old_fst_lmm);
1352 if (old_fst_lmm == NULL)
1353 GOTO(unlock, rc = -ENOMEM);
1355 memcpy(old_fst_lmm, fst_lmm, sizeof(*old_fst_lmm));
1357 /* increase the generation layout numbers */
1361 /* set the file specific informations in lmm */
1363 fst_lmm->lmm_layout_gen = cpu_to_le16(snd_gen);
1364 fst_lmm->lmm_oi = snd_lmm->lmm_oi;
1368 snd_lmm->lmm_layout_gen = cpu_to_le16(fst_gen);
1369 snd_lmm->lmm_oi = old_fst_lmm->lmm_oi;
1372 /* prepare transaction */
1373 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
1374 LU_XATTR_REPLACE, handle);
1378 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf, XATTR_NAME_LOV,
1379 LU_XATTR_REPLACE, handle);
1383 rc = mdd_trans_start(env, mdd, handle);
1387 rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV,
1388 LU_XATTR_REPLACE, handle,
1389 mdd_object_capa(env, fst_o));
1393 rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
1394 LU_XATTR_REPLACE, handle,
1395 mdd_object_capa(env, snd_o));
1399 /* failure on second file, but first was done, so we have
1400 * to roll back first */
1401 /* restore object_id, object_seq and generation number
1404 fst_lmm->lmm_oi = old_fst_lmm->lmm_oi;
1405 fst_lmm->lmm_layout_gen = old_fst_lmm->lmm_layout_gen;
1408 rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
1409 LU_XATTR_REPLACE, handle,
1410 mdd_object_capa(env, fst_o));
1413 CERROR("%s: unable to roll back after swap layouts"
1414 " failure between "DFID" and "DFID
1415 " rc2 = %d rc = %d)\n",
1416 mdd2obd_dev(mdd)->obd_name,
1417 PFID(mdo2fid(snd_o)), PFID(mdo2fid(fst_o)),
1419 /* a solution to avoid journal commit is to panic,
1420 * but it has strong consequences so we use LBUG to
1421 * allow sysdamin to choose to panic or not
1430 mdd_trans_stop(env, mdd, rc, handle);
1432 mdd_write_unlock(env, o2);
1433 mdd_write_unlock(env, o1);
1435 if (lmm1_buf && lmm1_buf->lb_buf)
1436 OBD_FREE(lmm1_buf->lb_buf, lmm1_buf->lb_len);
1438 OBD_FREE_PTR(lmm1_buf);
1440 if (lmm2_buf && lmm2_buf->lb_buf)
1441 OBD_FREE(lmm2_buf->lb_buf, lmm2_buf->lb_len);
1443 OBD_FREE_PTR(lmm2_buf);
1446 OBD_FREE_PTR(old_fst_lmm);
1451 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
1452 struct mdd_object *child, struct lu_attr *attr)
1454 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1455 struct dt_object *np = parent ? mdd_object_child(parent) : NULL;
1456 struct dt_object *nc = mdd_object_child(child);
1458 /* @hint will be initialized by underlying device. */
1459 nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
1463 * do NOT or the MAY_*'s, you'll get the weakest
1465 int accmode(const struct lu_env *env, struct lu_attr *la, int flags)
1469 /* Sadly, NFSD reopens a file repeatedly during operation, so the
1470 * "acc_mode = 0" allowance for newly-created files isn't honoured.
1471 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1472 * owner can write to a file even if it is marked readonly to hide
1473 * its brokenness. (bug 5781) */
1474 if (flags & MDS_OPEN_OWNEROVERRIDE) {
1475 struct lu_ucred *uc = lu_ucred_check(env);
1477 if ((uc == NULL) || (la->la_uid == uc->uc_fsuid))
1481 if (flags & FMODE_READ)
1483 if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
1485 if (flags & MDS_FMODE_EXEC)
1490 static int mdd_open_sanity_check(const struct lu_env *env,
1491 struct mdd_object *obj, int flag)
1493 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
1498 if (mdd_is_dead_obj(obj))
1501 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
1505 if (S_ISLNK(tmp_la->la_mode))
1508 mode = accmode(env, tmp_la, flag);
1510 if (S_ISDIR(tmp_la->la_mode) && (mode & MAY_WRITE))
1513 if (!(flag & MDS_OPEN_CREATED)) {
1514 rc = mdd_permission_internal(env, obj, tmp_la, mode);
1519 if (S_ISFIFO(tmp_la->la_mode) || S_ISSOCK(tmp_la->la_mode) ||
1520 S_ISBLK(tmp_la->la_mode) || S_ISCHR(tmp_la->la_mode))
1521 flag &= ~MDS_OPEN_TRUNC;
1523 /* For writing append-only file must open it with append mode. */
1524 if (mdd_is_append(obj)) {
1525 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
1527 if (flag & MDS_OPEN_TRUNC)
1533 * Now, flag -- O_NOATIME does not be packed by client.
1535 if (flag & O_NOATIME) {
1536 struct lu_ucred *uc = lu_ucred(env);
1538 if (uc && ((uc->uc_valid == UCRED_OLD) ||
1539 (uc->uc_valid == UCRED_NEW)) &&
1540 (uc->uc_fsuid != tmp_la->la_uid) &&
1541 !md_capable(uc, CFS_CAP_FOWNER))
1549 static int mdd_open(const struct lu_env *env, struct md_object *obj,
1552 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1555 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1557 rc = mdd_open_sanity_check(env, mdd_obj, flags);
1559 mdd_obj->mod_count++;
1561 mdd_write_unlock(env, mdd_obj);
1565 int mdd_declare_object_kill(const struct lu_env *env, struct mdd_object *obj,
1566 struct md_attr *ma, struct thandle *handle)
1568 return mdo_declare_destroy(env, obj, handle);
1571 /* return md_attr back,
1572 * if it is last unlink then return lov ea + llog cookie*/
1573 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
1574 struct md_attr *ma, struct thandle *handle)
1579 rc = mdo_destroy(env, obj, handle);
1584 static int mdd_declare_close(const struct lu_env *env,
1585 struct mdd_object *obj,
1587 struct thandle *handle)
1591 rc = orph_declare_index_delete(env, obj, handle);
1595 return mdo_declare_destroy(env, obj, handle);
1599 * No permission check is needed.
1601 static int mdd_close(const struct lu_env *env, struct md_object *obj,
1602 struct md_attr *ma, int mode)
1604 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1605 struct mdd_device *mdd = mdo2mdd(obj);
1606 struct thandle *handle = NULL;
1607 int rc, is_orphan = 0;
1610 if (ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_KEEP_ORPHAN) {
1611 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1612 mdd_obj->mod_count--;
1613 mdd_write_unlock(env, mdd_obj);
1615 if (mdd_obj->mod_flags & ORPHAN_OBJ && !mdd_obj->mod_count)
1616 CDEBUG(D_HA, "Object "DFID" is retained in orphan "
1617 "list\n", PFID(mdd_object_fid(mdd_obj)));
1621 /* mdd_finish_unlink() will always set orphan object as DEAD_OBJ, but
1622 * it might fail to add the object to orphan list (w/o ORPHAN_OBJ). */
1623 /* check without any lock */
1624 is_orphan = mdd_obj->mod_count == 1 &&
1625 (mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0;
1629 handle = mdd_trans_create(env, mdo2mdd(obj));
1631 RETURN(PTR_ERR(handle));
1633 rc = mdd_declare_close(env, mdd_obj, ma, handle);
1637 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1641 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1646 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1647 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
1648 mdd_object_capa(env, mdd_obj));
1650 CERROR("Failed to get lu_attr of "DFID": %d\n",
1651 PFID(mdd_object_fid(mdd_obj)), rc);
1655 /* check again with lock */
1656 is_orphan = (mdd_obj->mod_count == 1) &&
1657 ((mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0 ||
1658 ma->ma_attr.la_nlink == 0);
1660 if (is_orphan && handle == NULL) {
1661 mdd_write_unlock(env, mdd_obj);
1665 mdd_obj->mod_count--; /*release open count */
1671 /* NB: Object maybe not in orphan list originally, it is rare case for
1672 * mdd_finish_unlink() failure, in that case, the object doesn't have
1673 * ORPHAN_OBJ flag */
1674 if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
1675 /* remove link to object from orphan index */
1676 LASSERT(handle != NULL);
1677 rc = __mdd_orphan_del(env, mdd_obj, handle);
1679 CERROR("%s: unable to delete "DFID" from orphan list: "
1680 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1681 PFID(mdd_object_fid(mdd_obj)), rc);
1682 /* If object was not deleted from orphan list, do not
1683 * destroy OSS objects, which will be done when next
1688 CDEBUG(D_HA, "Object "DFID" is deleted from orphan "
1689 "list, OSS objects to be destroyed.\n",
1690 PFID(mdd_object_fid(mdd_obj)));
1693 rc = mdo_destroy(env, mdd_obj, handle);
1696 CERROR("%s: unable to delete "DFID" from orphan list: "
1697 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1698 PFID(mdd_object_fid(mdd_obj)), rc);
1703 mdd_write_unlock(env, mdd_obj);
1706 (mode & (FMODE_WRITE | MDS_OPEN_APPEND | MDS_OPEN_TRUNC)) &&
1707 !(ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_RECOV_OPEN)) {
1708 if (handle == NULL) {
1709 handle = mdd_trans_create(env, mdo2mdd(obj));
1711 GOTO(stop, rc = IS_ERR(handle));
1713 rc = mdd_declare_changelog_store(env, mdd, NULL,
1718 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1723 mdd_changelog_data_store(env, mdd, CL_CLOSE, mode,
1729 mdd_trans_stop(env, mdd, rc, handle);
1734 * Permission check is done when open,
1735 * no need check again.
1737 static int mdd_readpage_sanity_check(const struct lu_env *env,
1738 struct mdd_object *obj)
1740 struct dt_object *next = mdd_object_child(obj);
1744 if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
1752 static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
1753 int nob, const struct dt_it_ops *iops,
1754 struct dt_it *it, __u32 attr, void *arg)
1756 struct lu_dirpage *dp = &lp->lp_dir;
1760 struct lu_dirent *ent;
1761 struct lu_dirent *last = NULL;
1764 memset(area, 0, sizeof (*dp));
1765 area += sizeof (*dp);
1766 nob -= sizeof (*dp);
1773 len = iops->key_size(env, it);
1775 /* IAM iterator can return record with zero len. */
1779 hash = iops->store(env, it);
1780 if (unlikely(first)) {
1782 dp->ldp_hash_start = cpu_to_le64(hash);
1785 /* calculate max space required for lu_dirent */
1786 recsize = lu_dirent_calc_size(len, attr);
1788 if (nob >= recsize) {
1789 result = iops->rec(env, it, (struct dt_rec *)ent, attr);
1790 if (result == -ESTALE)
1795 /* osd might not able to pack all attributes,
1796 * so recheck rec length */
1797 recsize = le16_to_cpu(ent->lde_reclen);
1799 result = (last != NULL) ? 0 :-EINVAL;
1803 ent = (void *)ent + recsize;
1807 result = iops->next(env, it);
1808 if (result == -ESTALE)
1810 } while (result == 0);
1813 dp->ldp_hash_end = cpu_to_le64(hash);
1815 if (last->lde_hash == dp->ldp_hash_end)
1816 dp->ldp_flags |= cpu_to_le32(LDF_COLLIDE);
1817 last->lde_reclen = 0; /* end mark */
1820 /* end of directory */
1821 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
1822 else if (result < 0)
1823 CWARN("build page failed: %d!\n", result);
1827 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
1828 const struct lu_rdpg *rdpg)
1830 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1834 if (mdd_object_exists(mdd_obj) == 0) {
1835 CERROR("%s: object "DFID" not found: rc = -2\n",
1836 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
1840 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
1841 rc = mdd_readpage_sanity_check(env, mdd_obj);
1843 GOTO(out_unlock, rc);
1845 if (mdd_is_dead_obj(mdd_obj)) {
1847 struct lu_dirpage *dp;
1850 * According to POSIX, please do not return any entry to client:
1851 * even dot and dotdot should not be returned.
1853 CDEBUG(D_INODE, "readdir from dead object: "DFID"\n",
1854 PFID(mdd_object_fid(mdd_obj)));
1856 if (rdpg->rp_count <= 0)
1857 GOTO(out_unlock, rc = -EFAULT);
1858 LASSERT(rdpg->rp_pages != NULL);
1860 pg = rdpg->rp_pages[0];
1861 dp = (struct lu_dirpage*)cfs_kmap(pg);
1862 memset(dp, 0 , sizeof(struct lu_dirpage));
1863 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
1864 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
1865 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
1867 GOTO(out_unlock, rc = LU_PAGE_SIZE);
1870 rc = dt_index_walk(env, mdd_object_child(mdd_obj), rdpg,
1871 mdd_dir_page_build, NULL);
1873 struct lu_dirpage *dp;
1875 dp = cfs_kmap(rdpg->rp_pages[0]);
1876 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
1879 * No pages were processed, mark this for first page
1882 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
1883 rc = min_t(unsigned int, LU_PAGE_SIZE, rdpg->rp_count);
1885 cfs_kunmap(rdpg->rp_pages[0]);
1888 GOTO(out_unlock, rc);
1890 mdd_read_unlock(env, mdd_obj);
1894 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
1896 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1898 if (mdd_object_exists(mdd_obj) == 0) {
1899 CERROR("%s: object "DFID" not found: rc = -2\n",
1900 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
1903 return dt_object_sync(env, mdd_object_child(mdd_obj));
1906 static int mdd_object_lock(const struct lu_env *env,
1907 struct md_object *obj,
1908 struct lustre_handle *lh,
1909 struct ldlm_enqueue_info *einfo,
1912 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1913 LASSERT(mdd_object_exists(mdd_obj));
1914 return dt_object_lock(env, mdd_object_child(mdd_obj), lh,
1918 const struct md_object_operations mdd_obj_ops = {
1919 .moo_permission = mdd_permission,
1920 .moo_attr_get = mdd_attr_get,
1921 .moo_attr_set = mdd_attr_set,
1922 .moo_xattr_get = mdd_xattr_get,
1923 .moo_xattr_set = mdd_xattr_set,
1924 .moo_xattr_list = mdd_xattr_list,
1925 .moo_xattr_del = mdd_xattr_del,
1926 .moo_swap_layouts = mdd_swap_layouts,
1927 .moo_open = mdd_open,
1928 .moo_close = mdd_close,
1929 .moo_readpage = mdd_readpage,
1930 .moo_readlink = mdd_readlink,
1931 .moo_changelog = mdd_changelog,
1932 .moo_capa_get = mdd_capa_get,
1933 .moo_object_sync = mdd_object_sync,
1934 .moo_object_lock = mdd_object_lock,