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, 2013, 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 /* If the object has been delete from the namespace, then
277 * get linkEA should return -ENOENT as well */
278 if (unlikely((mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ)) &&
279 strcmp(name, XATTR_NAME_LINK) == 0))
282 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
283 rc = mdo_xattr_get(env, mdd_obj, buf, name,
284 mdd_object_capa(env, mdd_obj));
285 mdd_read_unlock(env, mdd_obj);
291 * Permission check is done when open,
292 * no need check again.
294 static int mdd_readlink(const struct lu_env *env, struct md_object *obj,
297 struct mdd_object *mdd_obj = md2mdd_obj(obj);
298 struct dt_object *next;
303 if (mdd_object_exists(mdd_obj) == 0) {
304 CERROR("%s: object "DFID" not found: rc = -2\n",
305 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
309 next = mdd_object_child(mdd_obj);
310 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
311 rc = next->do_body_ops->dbo_read(env, next, buf, &pos,
312 mdd_object_capa(env, mdd_obj));
313 mdd_read_unlock(env, mdd_obj);
318 * No permission check is needed.
320 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
323 struct mdd_object *mdd_obj = md2mdd_obj(obj);
328 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
329 rc = mdo_xattr_list(env, mdd_obj, buf, mdd_object_capa(env, mdd_obj));
330 mdd_read_unlock(env, mdd_obj);
335 int mdd_declare_object_create_internal(const struct lu_env *env,
336 struct mdd_object *p,
337 struct mdd_object *c,
338 struct lu_attr *attr,
339 struct thandle *handle,
340 const struct md_op_spec *spec)
342 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
343 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
344 const struct dt_index_features *feat = spec->sp_feat;
348 if (feat != &dt_directory_features && feat != NULL) {
349 dof->dof_type = DFT_INDEX;
350 dof->u.dof_idx.di_feat = feat;
353 dof->dof_type = dt_mode_to_dft(attr->la_mode);
354 if (dof->dof_type == DFT_REGULAR) {
355 dof->u.dof_reg.striped =
356 md_should_create(spec->sp_cr_flags);
357 if (spec->sp_cr_flags & MDS_OPEN_HAS_EA)
358 dof->u.dof_reg.striped = 0;
359 /* is this replay? */
361 dof->u.dof_reg.striped = 0;
365 rc = mdo_declare_create_obj(env, c, attr, hint, dof, handle);
370 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
371 struct mdd_object *c, struct lu_attr *attr,
372 struct thandle *handle,
373 const struct md_op_spec *spec)
375 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
376 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
380 LASSERT(!mdd_object_exists(c));
382 rc = mdo_create_obj(env, c, attr, hint, dof, handle);
384 LASSERT(ergo(rc == 0, mdd_object_exists(c)));
390 * Make sure the ctime is increased only.
392 static inline int mdd_attr_check(const struct lu_env *env,
393 struct mdd_object *obj,
394 struct lu_attr *attr)
396 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
400 if (attr->la_valid & LA_CTIME) {
401 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
405 if (attr->la_ctime < tmp_la->la_ctime)
406 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
407 else if (attr->la_valid == LA_CTIME &&
408 attr->la_ctime == tmp_la->la_ctime)
409 attr->la_valid &= ~LA_CTIME;
414 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
415 const struct lu_attr *attr, struct thandle *handle,
421 rc = mdo_attr_set(env, obj, attr, handle, mdd_object_capa(env, obj));
422 #ifdef CONFIG_FS_POSIX_ACL
423 if (!rc && (attr->la_valid & LA_MODE) && needacl)
424 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
429 int mdd_attr_check_set_internal(const struct lu_env *env,
430 struct mdd_object *obj, struct lu_attr *attr,
431 struct thandle *handle, int needacl)
436 rc = mdd_attr_check(env, obj, attr);
441 rc = mdd_attr_set_internal(env, obj, attr, handle, needacl);
446 * This gives the same functionality as the code between
447 * sys_chmod and inode_setattr
448 * chown_common and inode_setattr
449 * utimes and inode_setattr
450 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
452 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
453 struct lu_attr *la, const unsigned long flags)
455 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
463 /* Do not permit change file type */
464 if (la->la_valid & LA_TYPE)
467 /* They should not be processed by setattr */
468 if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
471 /* export destroy does not have ->le_ses, but we may want
472 * to drop LUSTRE_SOM_FL. */
473 uc = lu_ucred_check(env);
477 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
481 if (la->la_valid == LA_CTIME) {
482 if (!(flags & MDS_PERM_BYPASS))
483 /* This is only for set ctime when rename's source is
485 rc = mdd_may_delete(env, NULL, obj, tmp_la, NULL, 1, 0);
486 if (rc == 0 && la->la_ctime <= tmp_la->la_ctime)
487 la->la_valid &= ~LA_CTIME;
491 if (la->la_valid == LA_ATIME) {
492 /* This is atime only set for read atime update on close. */
493 if (la->la_atime >= tmp_la->la_atime &&
494 la->la_atime < (tmp_la->la_atime +
495 mdd_obj2mdd_dev(obj)->mdd_atime_diff))
496 la->la_valid &= ~LA_ATIME;
500 /* Check if flags change. */
501 if (la->la_valid & LA_FLAGS) {
502 unsigned int oldflags = 0;
503 unsigned int newflags = la->la_flags &
504 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
506 if ((uc->uc_fsuid != tmp_la->la_uid) &&
507 !md_capable(uc, CFS_CAP_FOWNER))
510 /* XXX: the IMMUTABLE and APPEND_ONLY flags can
511 * only be changed by the relevant capability. */
512 if (mdd_is_immutable(obj))
513 oldflags |= LUSTRE_IMMUTABLE_FL;
514 if (mdd_is_append(obj))
515 oldflags |= LUSTRE_APPEND_FL;
516 if ((oldflags ^ newflags) &&
517 !md_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
520 if (!S_ISDIR(tmp_la->la_mode))
521 la->la_flags &= ~LUSTRE_DIRSYNC_FL;
524 if ((mdd_is_immutable(obj) || mdd_is_append(obj)) &&
525 (la->la_valid & ~LA_FLAGS) &&
526 !(flags & MDS_PERM_BYPASS))
529 /* Check for setting the obj time. */
530 if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
531 !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
532 if ((uc->uc_fsuid != tmp_la->la_uid) &&
533 !md_capable(uc, CFS_CAP_FOWNER)) {
534 rc = mdd_permission_internal(env, obj, tmp_la,
541 if (la->la_valid & LA_KILL_SUID) {
542 la->la_valid &= ~LA_KILL_SUID;
543 if ((tmp_la->la_mode & S_ISUID) &&
544 !(la->la_valid & LA_MODE)) {
545 la->la_mode = tmp_la->la_mode;
546 la->la_valid |= LA_MODE;
548 la->la_mode &= ~S_ISUID;
551 if (la->la_valid & LA_KILL_SGID) {
552 la->la_valid &= ~LA_KILL_SGID;
553 if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
554 (S_ISGID | S_IXGRP)) &&
555 !(la->la_valid & LA_MODE)) {
556 la->la_mode = tmp_la->la_mode;
557 la->la_valid |= LA_MODE;
559 la->la_mode &= ~S_ISGID;
562 /* Make sure a caller can chmod. */
563 if (la->la_valid & LA_MODE) {
564 if (!(flags & MDS_PERM_BYPASS) &&
565 (uc->uc_fsuid != tmp_la->la_uid) &&
566 !md_capable(uc, CFS_CAP_FOWNER))
569 if (la->la_mode == (cfs_umode_t) -1)
570 la->la_mode = tmp_la->la_mode;
572 la->la_mode = (la->la_mode & S_IALLUGO) |
573 (tmp_la->la_mode & ~S_IALLUGO);
575 /* Also check the setgid bit! */
576 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
577 la->la_gid : tmp_la->la_gid) &&
578 !md_capable(uc, CFS_CAP_FSETID))
579 la->la_mode &= ~S_ISGID;
581 la->la_mode = tmp_la->la_mode;
584 /* Make sure a caller can chown. */
585 if (la->la_valid & LA_UID) {
586 if (la->la_uid == (uid_t) -1)
587 la->la_uid = tmp_la->la_uid;
588 if (((uc->uc_fsuid != tmp_la->la_uid) ||
589 (la->la_uid != tmp_la->la_uid)) &&
590 !md_capable(uc, CFS_CAP_CHOWN))
593 /* If the user or group of a non-directory has been
594 * changed by a non-root user, remove the setuid bit.
595 * 19981026 David C Niemi <niemi@tux.org>
597 * Changed this to apply to all users, including root,
598 * to avoid some races. This is the behavior we had in
599 * 2.0. The check for non-root was definitely wrong
600 * for 2.2 anyway, as it should have been using
601 * CAP_FSETID rather than fsuid -- 19990830 SD. */
602 if (((tmp_la->la_mode & S_ISUID) == S_ISUID) &&
603 !S_ISDIR(tmp_la->la_mode)) {
604 la->la_mode &= ~S_ISUID;
605 la->la_valid |= LA_MODE;
609 /* Make sure caller can chgrp. */
610 if (la->la_valid & LA_GID) {
611 if (la->la_gid == (gid_t) -1)
612 la->la_gid = tmp_la->la_gid;
613 if (((uc->uc_fsuid != tmp_la->la_uid) ||
614 ((la->la_gid != tmp_la->la_gid) &&
615 !lustre_in_group_p(uc, la->la_gid))) &&
616 !md_capable(uc, CFS_CAP_CHOWN))
619 /* Likewise, if the user or group of a non-directory
620 * has been changed by a non-root user, remove the
621 * setgid bit UNLESS there is no group execute bit
622 * (this would be a file marked for mandatory
623 * locking). 19981026 David C Niemi <niemi@tux.org>
625 * Removed the fsuid check (see the comment above) --
627 if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
628 (S_ISGID | S_IXGRP)) && !S_ISDIR(tmp_la->la_mode)) {
629 la->la_mode &= ~S_ISGID;
630 la->la_valid |= LA_MODE;
634 /* For both Size-on-MDS case and truncate case,
635 * "la->la_valid & (LA_SIZE | LA_BLOCKS)" are ture.
636 * We distinguish them by "flags & MDS_SOM".
637 * For SOM case, it is true, the MAY_WRITE perm has been checked
638 * when open, no need check again. For truncate case, it is false,
639 * the MAY_WRITE perm should be checked here. */
640 if (flags & MDS_SOM) {
641 /* For the "Size-on-MDS" setattr update, merge coming
642 * attributes with the set in the inode. BUG 10641 */
643 if ((la->la_valid & LA_ATIME) &&
644 (la->la_atime <= tmp_la->la_atime))
645 la->la_valid &= ~LA_ATIME;
647 /* OST attributes do not have a priority over MDS attributes,
648 * so drop times if ctime is equal. */
649 if ((la->la_valid & LA_CTIME) &&
650 (la->la_ctime <= tmp_la->la_ctime))
651 la->la_valid &= ~(LA_MTIME | LA_CTIME);
653 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
654 if (!((flags & MDS_OWNEROVERRIDE) &&
655 (uc->uc_fsuid == tmp_la->la_uid)) &&
656 !(flags & MDS_PERM_BYPASS)) {
657 rc = mdd_permission_internal(env, obj,
663 if (la->la_valid & LA_CTIME) {
664 /* The pure setattr, it has the priority over what is
665 * already set, do not drop it if ctime is equal. */
666 if (la->la_ctime < tmp_la->la_ctime)
667 la->la_valid &= ~(LA_ATIME | LA_MTIME |
675 /** Store a data change changelog record
676 * If this fails, we must fail the whole transaction; we don't
677 * want the change to commit without the log entry.
678 * \param mdd_obj - mdd_object of change
679 * \param handle - transacion handle
681 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
682 enum changelog_rec_type type, int flags,
683 struct mdd_object *mdd_obj, struct thandle *handle)
685 const struct lu_fid *tfid;
686 struct llog_changelog_rec *rec;
692 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
694 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
697 LASSERT(mdd_obj != NULL);
698 LASSERT(handle != NULL);
700 tfid = mdo2fid(mdd_obj);
702 if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
703 cfs_time_before_64(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
704 /* Don't need multiple updates in this log */
705 /* Don't check under lock - no big deal if we get an extra
710 reclen = llog_data_len(sizeof(*rec));
711 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
712 if (buf->lb_buf == NULL)
716 rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
717 rec->cr.cr_type = (__u32)type;
718 rec->cr.cr_tfid = *tfid;
719 rec->cr.cr_namelen = 0;
720 mdd_obj->mod_cltime = cfs_time_current_64();
722 rc = mdd_changelog_store(env, mdd, rec, handle);
727 int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
728 int flags, struct md_object *obj)
730 struct thandle *handle;
731 struct mdd_object *mdd_obj = md2mdd_obj(obj);
732 struct mdd_device *mdd = mdo2mdd(obj);
736 handle = mdd_trans_create(env, mdd);
738 RETURN(PTR_ERR(handle));
740 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
744 rc = mdd_trans_start(env, mdd, handle);
748 rc = mdd_changelog_data_store(env, mdd, type, flags, mdd_obj,
752 mdd_trans_stop(env, mdd, rc, handle);
758 * Save LMA extended attributes with data from \a ma.
760 * HSM and Size-On-MDS data will be extracted from \ma if they are valid, if
761 * not, LMA EA will be first read from disk, modified and write back.
764 /* Precedence for choosing record type when multiple
765 * attributes change: setattr > mtime > ctime > atime
766 * (ctime changes when mtime does, plus chmod/chown.
767 * atime and ctime are independent.) */
768 static int mdd_attr_set_changelog(const struct lu_env *env,
769 struct md_object *obj, struct thandle *handle,
772 struct mdd_device *mdd = mdo2mdd(obj);
775 bits = (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
776 bits |= (valid & LA_MTIME) ? 1 << CL_MTIME : 0;
777 bits |= (valid & LA_CTIME) ? 1 << CL_CTIME : 0;
778 bits |= (valid & LA_ATIME) ? 1 << CL_ATIME : 0;
779 bits = bits & mdd->mdd_cl.mc_mask;
783 /* The record type is the lowest non-masked set bit */
784 while (bits && ((bits & 1) == 0)) {
789 /* FYI we only store the first CLF_FLAGMASK bits of la_valid */
790 return mdd_changelog_data_store(env, mdd, type, (int)valid,
791 md2mdd_obj(obj), handle);
794 static int mdd_declare_attr_set(const struct lu_env *env,
795 struct mdd_device *mdd,
796 struct mdd_object *obj,
797 const struct lu_attr *attr,
798 struct thandle *handle)
802 rc = mdo_declare_attr_set(env, obj, attr, handle);
806 #ifdef CONFIG_FS_POSIX_ACL
807 if (attr->la_valid & LA_MODE) {
808 mdd_read_lock(env, obj, MOR_TGT_CHILD);
809 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
810 XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
811 mdd_read_unlock(env, obj);
812 if (rc == -EOPNOTSUPP || rc == -ENODATA)
818 struct lu_buf *buf = mdd_buf_get(env, NULL, rc);
819 rc = mdo_declare_xattr_set(env, obj, buf,
820 XATTR_NAME_ACL_ACCESS, 0,
828 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
832 /* set attr and LOV EA at once, return updated attr */
833 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
834 const struct md_attr *ma)
836 struct mdd_object *mdd_obj = md2mdd_obj(obj);
837 struct mdd_device *mdd = mdo2mdd(obj);
838 struct thandle *handle;
839 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
840 const struct lu_attr *la = &ma->ma_attr;
844 /* we do not use ->attr_set() for LOV/SOM/HSM EA any more */
845 LASSERT((ma->ma_valid & MA_LOV) == 0);
846 LASSERT((ma->ma_valid & MA_HSM) == 0);
847 LASSERT((ma->ma_valid & MA_SOM) == 0);
849 *la_copy = ma->ma_attr;
850 rc = mdd_fix_attr(env, mdd_obj, la_copy, ma->ma_attr_flags);
854 /* setattr on "close" only change atime, or do nothing */
855 if (la->la_valid == LA_ATIME && la_copy->la_valid == 0)
858 handle = mdd_trans_create(env, mdd);
860 RETURN(PTR_ERR(handle));
862 rc = mdd_declare_attr_set(env, mdd, mdd_obj, la, handle);
866 rc = mdd_trans_start(env, mdd, handle);
870 /* permission changes may require sync operation */
871 if (ma->ma_attr.la_valid & (LA_MODE|LA_UID|LA_GID))
872 handle->th_sync |= !!mdd->mdd_sync_permission;
874 if (la->la_valid & (LA_MTIME | LA_CTIME))
875 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
876 la->la_mtime, la->la_ctime);
878 if (la_copy->la_valid & LA_FLAGS) {
879 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
881 mdd_flags_xlate(mdd_obj, la_copy->la_flags);
882 } else if (la_copy->la_valid) { /* setattr */
883 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
887 rc = mdd_attr_set_changelog(env, obj, handle,
890 mdd_trans_stop(env, mdd, rc, handle);
894 static int mdd_xattr_sanity_check(const struct lu_env *env,
895 struct mdd_object *obj)
897 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
898 struct lu_ucred *uc = lu_ucred_assert(env);
902 if (mdd_is_immutable(obj) || mdd_is_append(obj))
905 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
909 if ((uc->uc_fsuid != tmp_la->la_uid) &&
910 !md_capable(uc, CFS_CAP_FOWNER))
916 static int mdd_declare_xattr_set(const struct lu_env *env,
917 struct mdd_device *mdd,
918 struct mdd_object *obj,
919 const struct lu_buf *buf,
921 int fl, struct thandle *handle)
925 rc = mdo_declare_xattr_set(env, obj, buf, name, fl, handle);
929 /* Only record user and layout xattr changes */
930 if (strncmp(XATTR_USER_PREFIX, name,
931 sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
932 strncmp(XATTR_NAME_LOV, name,
933 sizeof(XATTR_NAME_LOV) - 1) == 0) {
934 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
939 /* If HSM data is modified, this could add a changelog */
940 if (strncmp(XATTR_NAME_HSM, name, sizeof(XATTR_NAME_HSM) - 1) == 0) {
941 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
946 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
951 * Compare current and future data of HSM EA and add a changelog if needed.
953 * Caller should have write-locked \param obj.
955 * \param buf - Future HSM EA content.
956 * \retval 0 if no changelog is needed or changelog was added properly.
957 * \retval -ve errno if there was a problem
959 static int mdd_hsm_update_locked(const struct lu_env *env,
960 struct md_object *obj,
961 const struct lu_buf *buf,
962 struct thandle *handle)
964 struct mdd_thread_info *info = mdd_env_info(env);
965 struct mdd_device *mdd = mdo2mdd(obj);
966 struct mdd_object *mdd_obj = md2mdd_obj(obj);
967 struct lu_buf *current_buf = &info->mti_buf;
968 struct md_hsm *current_mh;
969 struct md_hsm *new_mh;
973 OBD_ALLOC_PTR(current_mh);
974 if (current_mh == NULL)
977 /* Read HSM attrs from disk */
978 current_buf->lb_buf = info->mti_xattr_buf;
979 current_buf->lb_len = sizeof(info->mti_xattr_buf);
980 CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
981 rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM,
982 mdd_object_capa(env, mdd_obj));
983 rc = lustre_buf2hsm(info->mti_xattr_buf, rc, current_mh);
984 if (rc < 0 && rc != -ENODATA)
986 else if (rc == -ENODATA)
987 current_mh->mh_flags = 0;
989 /* Map future HSM xattr */
990 OBD_ALLOC_PTR(new_mh);
992 GOTO(free, rc = -ENOMEM);
993 lustre_buf2hsm(buf->lb_buf, buf->lb_len, new_mh);
995 /* If HSM flags are different, add a changelog */
997 if (current_mh->mh_flags != new_mh->mh_flags) {
999 hsm_set_cl_event(&flags, HE_STATE);
1000 if (new_mh->mh_flags & HS_DIRTY)
1001 hsm_set_cl_flags(&flags, CLF_HSM_DIRTY);
1003 rc = mdd_changelog_data_store(env, mdd, CL_HSM, flags, mdd_obj,
1007 OBD_FREE_PTR(new_mh);
1009 OBD_FREE_PTR(current_mh);
1015 * The caller should guarantee to update the object ctime
1016 * after xattr_set if needed.
1018 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
1019 const struct lu_buf *buf, const char *name,
1022 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1023 struct mdd_device *mdd = mdo2mdd(obj);
1024 struct thandle *handle;
1028 if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
1029 rc = mdd_acl_set(env, mdd_obj, buf, fl);
1033 rc = mdd_xattr_sanity_check(env, mdd_obj);
1037 handle = mdd_trans_create(env, mdd);
1039 RETURN(PTR_ERR(handle));
1041 rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
1045 rc = mdd_trans_start(env, mdd, handle);
1049 /* security-replated changes may require sync */
1050 if (!strcmp(name, XATTR_NAME_ACL_ACCESS))
1051 handle->th_sync |= !!mdd->mdd_sync_permission;
1053 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1055 if (strncmp(XATTR_NAME_HSM, name, sizeof(XATTR_NAME_HSM) - 1) == 0) {
1056 rc = mdd_hsm_update_locked(env, obj, buf, handle);
1058 mdd_write_unlock(env, mdd_obj);
1063 rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle,
1064 mdd_object_capa(env, mdd_obj));
1065 mdd_write_unlock(env, mdd_obj);
1069 if (strncmp(XATTR_NAME_LOV, name, sizeof(XATTR_NAME_LOV) - 1) == 0)
1070 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, mdd_obj,
1072 else if (strncmp(XATTR_USER_PREFIX, name,
1073 sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1074 strncmp(POSIX_ACL_XATTR_ACCESS, name,
1075 sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1076 strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1077 sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1078 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1082 mdd_trans_stop(env, mdd, rc, handle);
1087 static int mdd_declare_xattr_del(const struct lu_env *env,
1088 struct mdd_device *mdd,
1089 struct mdd_object *obj,
1091 struct thandle *handle)
1095 rc = mdo_declare_xattr_del(env, obj, name, handle);
1099 /* Only record user xattr changes */
1100 if ((strncmp("user.", name, 5) == 0))
1101 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1107 * The caller should guarantee to update the object ctime
1108 * after xattr_set if needed.
1110 int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1113 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1114 struct mdd_device *mdd = mdo2mdd(obj);
1115 struct thandle *handle;
1119 rc = mdd_xattr_sanity_check(env, mdd_obj);
1123 handle = mdd_trans_create(env, mdd);
1125 RETURN(PTR_ERR(handle));
1127 rc = mdd_declare_xattr_del(env, mdd, mdd_obj, name, handle);
1131 rc = mdd_trans_start(env, mdd, handle);
1135 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1136 rc = mdo_xattr_del(env, mdd_obj, name, handle,
1137 mdd_object_capa(env, mdd_obj));
1138 mdd_write_unlock(env, mdd_obj);
1142 /* Only record system & user xattr changes */
1143 if (strncmp(XATTR_USER_PREFIX, name,
1144 sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1145 strncmp(POSIX_ACL_XATTR_ACCESS, name,
1146 sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1147 strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1148 sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1149 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1153 mdd_trans_stop(env, mdd, rc, handle);
1159 * read lov EA of an object
1160 * return the lov EA in an allocated lu_buf
1162 static struct lu_buf *mdd_get_lov_ea(const struct lu_env *env,
1163 struct mdd_object *obj)
1165 struct lu_buf *buf = &mdd_env_info(env)->mti_big_buf;
1166 struct lu_buf *lmm_buf = NULL;
1171 rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_LOV,
1172 mdd_object_capa(env, obj));
1174 if (rc == -ERANGE) {
1175 /* mti_big_buf is allocated but is too small
1176 * we need to increase it */
1177 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1179 if (buf->lb_buf == NULL)
1180 GOTO(out, rc = -ENOMEM);
1188 GOTO(out, rc = -ENODATA);
1191 if (memcmp(buf, &LU_BUF_NULL, sizeof(*buf)) == 0) {
1192 /* mti_big_buf was not allocated, so we have to
1193 * allocate it based on the ea size */
1194 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1196 if (buf->lb_buf == NULL)
1197 GOTO(out, rc = -ENOMEM);
1201 OBD_ALLOC_PTR(lmm_buf);
1203 GOTO(out, rc = -ENOMEM);
1205 OBD_ALLOC(lmm_buf->lb_buf, sz);
1206 if (!lmm_buf->lb_buf)
1207 GOTO(free, rc = -ENOMEM);
1209 memcpy(lmm_buf->lb_buf, buf->lb_buf, sz);
1210 lmm_buf->lb_len = sz;
1216 OBD_FREE_PTR(lmm_buf);
1225 * check if layout swapping between 2 objects is allowed
1227 * - same type of objects
1228 * - same owner/group (so quotas are still valid)
1230 static int mdd_layout_swap_allowed(const struct lu_env *env,
1231 struct mdd_object *o1,
1232 struct mdd_object *o2)
1234 const struct lu_fid *fid1, *fid2;
1236 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
1243 if (!fid_is_norm(fid1) || !fid_is_norm(fid2) ||
1244 (mdd_object_type(o1) != mdd_object_type(o2)))
1247 tmp_la->la_valid = 0;
1248 rc = mdd_la_get(env, o1, tmp_la, BYPASS_CAPA);
1251 uid = tmp_la->la_uid;
1252 gid = tmp_la->la_gid;
1254 tmp_la->la_valid = 0;
1255 rc = mdd_la_get(env, o2, tmp_la, BYPASS_CAPA);
1259 if ((uid != tmp_la->la_uid) || (gid != tmp_la->la_gid))
1266 * swap layouts between 2 lustre objects
1268 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
1269 struct md_object *obj2, __u64 flags)
1271 struct mdd_object *o1, *o2, *fst_o, *snd_o;
1272 struct lu_buf *lmm1_buf = NULL, *lmm2_buf = NULL;
1273 struct lu_buf *fst_buf, *snd_buf;
1274 struct lov_mds_md *fst_lmm, *snd_lmm, *old_fst_lmm = NULL;
1275 struct thandle *handle;
1276 struct mdd_device *mdd = mdo2mdd(obj1);
1278 __u16 fst_gen, snd_gen;
1282 /* we have to sort the 2 obj, so locking will always
1283 * be in the same order, even in case of 2 concurrent swaps */
1284 rc = lu_fid_cmp(mdo2fid(md2mdd_obj(obj1)),
1285 mdo2fid(md2mdd_obj(obj2)));
1291 o1 = md2mdd_obj(obj1);
1292 o2 = md2mdd_obj(obj2);
1294 o1 = md2mdd_obj(obj2);
1295 o2 = md2mdd_obj(obj1);
1298 /* check if layout swapping is allowed */
1299 rc = mdd_layout_swap_allowed(env, o1, o2);
1303 handle = mdd_trans_create(env, mdd);
1305 RETURN(PTR_ERR(handle));
1307 /* objects are already sorted */
1308 mdd_write_lock(env, o1, MOR_TGT_CHILD);
1309 mdd_write_lock(env, o2, MOR_TGT_CHILD);
1311 lmm1_buf = mdd_get_lov_ea(env, o1);
1312 if (IS_ERR(lmm1_buf)) {
1313 rc = PTR_ERR(lmm1_buf);
1319 lmm2_buf = mdd_get_lov_ea(env, o2);
1320 if (IS_ERR(lmm2_buf)) {
1321 rc = PTR_ERR(lmm2_buf);
1327 /* swapping 2 non existant layouts is a success */
1328 if ((lmm1_buf == NULL) && (lmm2_buf == NULL))
1331 /* to help inode migration between MDT, it is better to
1332 * start by the no layout file (if one), so we order the swap */
1333 if (lmm1_buf == NULL) {
1345 /* lmm and generation layout initialization */
1347 fst_lmm = fst_buf->lb_buf;
1348 fst_gen = le16_to_cpu(fst_lmm->lmm_layout_gen);
1349 fst_fl = LU_XATTR_REPLACE;
1353 fst_fl = LU_XATTR_CREATE;
1356 LASSERT(snd_buf != NULL);
1357 snd_lmm = snd_buf->lb_buf;
1358 snd_gen = le16_to_cpu(snd_lmm->lmm_layout_gen);
1360 /* increase the generation layout numbers */
1364 /* set the file specific informations in lmm */
1366 /* save the orignal lmm common header of first file
1367 * to be able to roll back */
1368 OBD_ALLOC_PTR(old_fst_lmm);
1369 if (old_fst_lmm == NULL)
1370 GOTO(stop, rc = -ENOMEM);
1371 *old_fst_lmm = *fst_lmm;
1373 fst_lmm->lmm_layout_gen = cpu_to_le16(snd_gen);
1374 fst_lmm->lmm_oi = snd_lmm->lmm_oi;
1376 snd_lmm->lmm_oi = old_fst_lmm->lmm_oi;
1378 if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1))
1379 snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
1380 else if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3))
1381 snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V3_DEF);
1383 GOTO(stop, rc = -EPROTO);
1386 snd_lmm->lmm_layout_gen = cpu_to_le16(fst_gen);
1388 /* prepare transaction */
1389 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
1395 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf,
1396 XATTR_NAME_LOV, LU_XATTR_REPLACE,
1399 rc = mdd_declare_xattr_del(env, mdd, snd_o, XATTR_NAME_LOV,
1404 rc = mdd_trans_start(env, mdd, handle);
1408 rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV, fst_fl, handle,
1409 mdd_object_capa(env, fst_o));
1414 rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
1415 LU_XATTR_REPLACE, handle,
1416 mdd_object_capa(env, snd_o));
1418 rc = mdo_xattr_del(env, snd_o, XATTR_NAME_LOV, handle,
1419 mdd_object_capa(env, snd_o));
1423 /* failure on second file, but first was done, so we have
1424 * to roll back first */
1425 /* restore object_id, object_seq and generation number
1428 LASSERT(old_fst_lmm != NULL);
1429 fst_lmm->lmm_oi = old_fst_lmm->lmm_oi;
1430 fst_lmm->lmm_layout_gen = old_fst_lmm->lmm_layout_gen;
1431 rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
1432 LU_XATTR_REPLACE, handle,
1433 mdd_object_capa(env, fst_o));
1435 rc2 = mdo_xattr_del(env, fst_o, XATTR_NAME_LOV, handle,
1436 mdd_object_capa(env, fst_o));
1441 CERROR("%s: unable to roll back after swap layouts"
1442 " failure between "DFID" and "DFID
1443 " rc2 = %d rc = %d)\n",
1444 mdd2obd_dev(mdd)->obd_name,
1445 PFID(mdo2fid(snd_o)), PFID(mdo2fid(fst_o)),
1447 /* a solution to avoid journal commit is to panic,
1448 * but it has strong consequences so we use LBUG to
1449 * allow sysdamin to choose to panic or not
1456 /* Issue one changelog record per file */
1457 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle);
1461 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle);
1467 mdd_trans_stop(env, mdd, rc, handle);
1468 mdd_write_unlock(env, o2);
1469 mdd_write_unlock(env, o1);
1471 if (lmm1_buf && lmm1_buf->lb_buf)
1472 OBD_FREE(lmm1_buf->lb_buf, lmm1_buf->lb_len);
1474 OBD_FREE_PTR(lmm1_buf);
1476 if (lmm2_buf && lmm2_buf->lb_buf)
1477 OBD_FREE(lmm2_buf->lb_buf, lmm2_buf->lb_len);
1479 OBD_FREE_PTR(lmm2_buf);
1482 OBD_FREE_PTR(old_fst_lmm);
1487 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
1488 struct mdd_object *child, struct lu_attr *attr)
1490 struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
1491 struct dt_object *np = parent ? mdd_object_child(parent) : NULL;
1492 struct dt_object *nc = mdd_object_child(child);
1494 /* @hint will be initialized by underlying device. */
1495 nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
1499 * do NOT or the MAY_*'s, you'll get the weakest
1501 int accmode(const struct lu_env *env, struct lu_attr *la, int flags)
1505 /* Sadly, NFSD reopens a file repeatedly during operation, so the
1506 * "acc_mode = 0" allowance for newly-created files isn't honoured.
1507 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1508 * owner can write to a file even if it is marked readonly to hide
1509 * its brokenness. (bug 5781) */
1510 if (flags & MDS_OPEN_OWNEROVERRIDE) {
1511 struct lu_ucred *uc = lu_ucred_check(env);
1513 if ((uc == NULL) || (la->la_uid == uc->uc_fsuid))
1517 if (flags & FMODE_READ)
1519 if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
1521 if (flags & MDS_FMODE_EXEC)
1526 static int mdd_open_sanity_check(const struct lu_env *env,
1527 struct mdd_object *obj, int flag)
1529 struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
1534 if (mdd_is_dead_obj(obj))
1537 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
1541 if (S_ISLNK(tmp_la->la_mode))
1544 mode = accmode(env, tmp_la, flag);
1546 if (S_ISDIR(tmp_la->la_mode) && (mode & MAY_WRITE))
1549 if (!(flag & MDS_OPEN_CREATED)) {
1550 rc = mdd_permission_internal(env, obj, tmp_la, mode);
1555 if (S_ISFIFO(tmp_la->la_mode) || S_ISSOCK(tmp_la->la_mode) ||
1556 S_ISBLK(tmp_la->la_mode) || S_ISCHR(tmp_la->la_mode))
1557 flag &= ~MDS_OPEN_TRUNC;
1559 /* For writing append-only file must open it with append mode. */
1560 if (mdd_is_append(obj)) {
1561 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
1563 if (flag & MDS_OPEN_TRUNC)
1569 * Now, flag -- O_NOATIME does not be packed by client.
1571 if (flag & O_NOATIME) {
1572 struct lu_ucred *uc = lu_ucred(env);
1574 if (uc && ((uc->uc_valid == UCRED_OLD) ||
1575 (uc->uc_valid == UCRED_NEW)) &&
1576 (uc->uc_fsuid != tmp_la->la_uid) &&
1577 !md_capable(uc, CFS_CAP_FOWNER))
1585 static int mdd_open(const struct lu_env *env, struct md_object *obj,
1588 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1591 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1593 rc = mdd_open_sanity_check(env, mdd_obj, flags);
1595 mdd_obj->mod_count++;
1597 mdd_write_unlock(env, mdd_obj);
1601 int mdd_declare_object_kill(const struct lu_env *env, struct mdd_object *obj,
1602 struct md_attr *ma, struct thandle *handle)
1604 return mdo_declare_destroy(env, obj, handle);
1607 /* return md_attr back,
1608 * if it is last unlink then return lov ea + llog cookie*/
1609 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
1610 struct md_attr *ma, struct thandle *handle)
1615 rc = mdo_destroy(env, obj, handle);
1620 static int mdd_declare_close(const struct lu_env *env,
1621 struct mdd_object *obj,
1623 struct thandle *handle)
1627 rc = orph_declare_index_delete(env, obj, handle);
1631 return mdo_declare_destroy(env, obj, handle);
1635 * No permission check is needed.
1637 static int mdd_close(const struct lu_env *env, struct md_object *obj,
1638 struct md_attr *ma, int mode)
1640 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1641 struct mdd_device *mdd = mdo2mdd(obj);
1642 struct thandle *handle = NULL;
1643 int rc, is_orphan = 0;
1646 if (ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_KEEP_ORPHAN) {
1647 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1648 mdd_obj->mod_count--;
1649 mdd_write_unlock(env, mdd_obj);
1651 if (mdd_obj->mod_flags & ORPHAN_OBJ && !mdd_obj->mod_count)
1652 CDEBUG(D_HA, "Object "DFID" is retained in orphan "
1653 "list\n", PFID(mdd_object_fid(mdd_obj)));
1657 /* mdd_finish_unlink() will always set orphan object as DEAD_OBJ, but
1658 * it might fail to add the object to orphan list (w/o ORPHAN_OBJ). */
1659 /* check without any lock */
1660 is_orphan = mdd_obj->mod_count == 1 &&
1661 (mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0;
1665 handle = mdd_trans_create(env, mdo2mdd(obj));
1667 RETURN(PTR_ERR(handle));
1669 rc = mdd_declare_close(env, mdd_obj, ma, handle);
1673 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1677 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1682 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1683 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
1684 mdd_object_capa(env, mdd_obj));
1686 CERROR("Failed to get lu_attr of "DFID": %d\n",
1687 PFID(mdd_object_fid(mdd_obj)), rc);
1691 /* check again with lock */
1692 is_orphan = (mdd_obj->mod_count == 1) &&
1693 ((mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0 ||
1694 ma->ma_attr.la_nlink == 0);
1696 if (is_orphan && handle == NULL) {
1697 mdd_write_unlock(env, mdd_obj);
1701 mdd_obj->mod_count--; /*release open count */
1707 /* NB: Object maybe not in orphan list originally, it is rare case for
1708 * mdd_finish_unlink() failure, in that case, the object doesn't have
1709 * ORPHAN_OBJ flag */
1710 if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
1711 /* remove link to object from orphan index */
1712 LASSERT(handle != NULL);
1713 rc = __mdd_orphan_del(env, mdd_obj, handle);
1715 CERROR("%s: unable to delete "DFID" from orphan list: "
1716 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1717 PFID(mdd_object_fid(mdd_obj)), rc);
1718 /* If object was not deleted from orphan list, do not
1719 * destroy OSS objects, which will be done when next
1724 CDEBUG(D_HA, "Object "DFID" is deleted from orphan "
1725 "list, OSS objects to be destroyed.\n",
1726 PFID(mdd_object_fid(mdd_obj)));
1729 rc = mdo_destroy(env, mdd_obj, handle);
1732 CERROR("%s: unable to delete "DFID" from orphan list: "
1733 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1734 PFID(mdd_object_fid(mdd_obj)), rc);
1739 mdd_write_unlock(env, mdd_obj);
1742 (mode & (FMODE_WRITE | MDS_OPEN_APPEND | MDS_OPEN_TRUNC)) &&
1743 !(ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_RECOV_OPEN)) {
1744 if (handle == NULL) {
1745 handle = mdd_trans_create(env, mdo2mdd(obj));
1747 GOTO(stop, rc = PTR_ERR(handle));
1749 rc = mdd_declare_changelog_store(env, mdd, NULL,
1754 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1759 mdd_changelog_data_store(env, mdd, CL_CLOSE, mode,
1764 if (handle != NULL && !IS_ERR(handle))
1765 mdd_trans_stop(env, mdd, rc, handle);
1771 * Permission check is done when open,
1772 * no need check again.
1774 static int mdd_readpage_sanity_check(const struct lu_env *env,
1775 struct mdd_object *obj)
1777 struct dt_object *next = mdd_object_child(obj);
1781 if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
1789 static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
1790 int nob, const struct dt_it_ops *iops,
1791 struct dt_it *it, __u32 attr, void *arg)
1793 struct lu_dirpage *dp = &lp->lp_dir;
1797 struct lu_dirent *ent;
1798 struct lu_dirent *last = NULL;
1801 memset(area, 0, sizeof (*dp));
1802 area += sizeof (*dp);
1803 nob -= sizeof (*dp);
1810 len = iops->key_size(env, it);
1812 /* IAM iterator can return record with zero len. */
1816 hash = iops->store(env, it);
1817 if (unlikely(first)) {
1819 dp->ldp_hash_start = cpu_to_le64(hash);
1822 /* calculate max space required for lu_dirent */
1823 recsize = lu_dirent_calc_size(len, attr);
1825 if (nob >= recsize) {
1826 result = iops->rec(env, it, (struct dt_rec *)ent, attr);
1827 if (result == -ESTALE)
1832 /* osd might not able to pack all attributes,
1833 * so recheck rec length */
1834 recsize = le16_to_cpu(ent->lde_reclen);
1836 result = (last != NULL) ? 0 :-EINVAL;
1840 ent = (void *)ent + recsize;
1844 result = iops->next(env, it);
1845 if (result == -ESTALE)
1847 } while (result == 0);
1850 dp->ldp_hash_end = cpu_to_le64(hash);
1852 if (last->lde_hash == dp->ldp_hash_end)
1853 dp->ldp_flags |= cpu_to_le32(LDF_COLLIDE);
1854 last->lde_reclen = 0; /* end mark */
1857 /* end of directory */
1858 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
1859 else if (result < 0)
1860 CWARN("build page failed: %d!\n", result);
1864 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
1865 const struct lu_rdpg *rdpg)
1867 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1871 if (mdd_object_exists(mdd_obj) == 0) {
1872 CERROR("%s: object "DFID" not found: rc = -2\n",
1873 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
1877 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
1878 rc = mdd_readpage_sanity_check(env, mdd_obj);
1880 GOTO(out_unlock, rc);
1882 if (mdd_is_dead_obj(mdd_obj)) {
1884 struct lu_dirpage *dp;
1887 * According to POSIX, please do not return any entry to client:
1888 * even dot and dotdot should not be returned.
1890 CDEBUG(D_INODE, "readdir from dead object: "DFID"\n",
1891 PFID(mdd_object_fid(mdd_obj)));
1893 if (rdpg->rp_count <= 0)
1894 GOTO(out_unlock, rc = -EFAULT);
1895 LASSERT(rdpg->rp_pages != NULL);
1897 pg = rdpg->rp_pages[0];
1898 dp = (struct lu_dirpage*)cfs_kmap(pg);
1899 memset(dp, 0 , sizeof(struct lu_dirpage));
1900 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
1901 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
1902 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
1904 GOTO(out_unlock, rc = LU_PAGE_SIZE);
1907 rc = dt_index_walk(env, mdd_object_child(mdd_obj), rdpg,
1908 mdd_dir_page_build, NULL);
1910 struct lu_dirpage *dp;
1912 dp = cfs_kmap(rdpg->rp_pages[0]);
1913 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
1916 * No pages were processed, mark this for first page
1919 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
1920 rc = min_t(unsigned int, LU_PAGE_SIZE, rdpg->rp_count);
1922 cfs_kunmap(rdpg->rp_pages[0]);
1925 GOTO(out_unlock, rc);
1927 mdd_read_unlock(env, mdd_obj);
1931 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
1933 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1935 if (mdd_object_exists(mdd_obj) == 0) {
1936 CERROR("%s: object "DFID" not found: rc = -2\n",
1937 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
1940 return dt_object_sync(env, mdd_object_child(mdd_obj));
1943 static int mdd_object_lock(const struct lu_env *env,
1944 struct md_object *obj,
1945 struct lustre_handle *lh,
1946 struct ldlm_enqueue_info *einfo,
1949 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1950 LASSERT(mdd_object_exists(mdd_obj));
1951 return dt_object_lock(env, mdd_object_child(mdd_obj), lh,
1955 const struct md_object_operations mdd_obj_ops = {
1956 .moo_permission = mdd_permission,
1957 .moo_attr_get = mdd_attr_get,
1958 .moo_attr_set = mdd_attr_set,
1959 .moo_xattr_get = mdd_xattr_get,
1960 .moo_xattr_set = mdd_xattr_set,
1961 .moo_xattr_list = mdd_xattr_list,
1962 .moo_xattr_del = mdd_xattr_del,
1963 .moo_swap_layouts = mdd_swap_layouts,
1964 .moo_open = mdd_open,
1965 .moo_close = mdd_close,
1966 .moo_readpage = mdd_readpage,
1967 .moo_readlink = mdd_readlink,
1968 .moo_changelog = mdd_changelog,
1969 .moo_capa_get = mdd_capa_get,
1970 .moo_object_sync = mdd_object_sync,
1971 .moo_object_lock = mdd_object_lock,