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>
52 #include <lustre_idmap.h>
53 #include <lustre_param.h>
54 #include <lustre_mds.h>
55 #include <lustre/lustre_idl.h>
57 #include "mdd_internal.h"
59 static const struct lu_object_operations mdd_lu_obj_ops;
61 static int mdd_xattr_get(const struct lu_env *env,
62 struct md_object *obj, struct lu_buf *buf,
65 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
66 struct lu_attr *la, struct lustre_capa *capa)
68 if (mdd_object_exists(obj) == 0) {
69 CERROR("%s: object "DFID" not found: rc = -2\n",
70 mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
74 return mdo_attr_get(env, obj, la, capa);
77 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
79 struct mdd_thread_info *info;
81 lu_env_refill((struct lu_env *)env);
82 info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
83 LASSERT(info != NULL);
87 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
91 buf = &mdd_env_info(env)->mti_buf[0];
97 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
98 const void *area, ssize_t len)
102 buf = &mdd_env_info(env)->mti_buf[0];
103 buf->lb_buf = (void *)area;
108 struct lu_object *mdd_object_alloc(const struct lu_env *env,
109 const struct lu_object_header *hdr,
112 struct mdd_object *mdd_obj;
114 OBD_SLAB_ALLOC_PTR_GFP(mdd_obj, mdd_object_kmem, GFP_NOFS);
115 if (mdd_obj != NULL) {
118 o = mdd2lu_obj(mdd_obj);
119 lu_object_init(o, NULL, d);
120 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
121 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
122 mdd_obj->mod_count = 0;
123 o->lo_ops = &mdd_lu_obj_ops;
130 static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
131 const struct lu_object_conf *unused)
133 struct mdd_device *d = lu2mdd_dev(o->lo_dev);
134 struct mdd_object *mdd_obj = lu2mdd_obj(o);
135 struct lu_object *below;
136 struct lu_device *under;
139 mdd_obj->mod_cltime = 0;
140 under = &d->mdd_child->dd_lu_dev;
141 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
143 RETURN(PTR_ERR(below));
145 lu_object_add(o, below);
150 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
154 if (lu_object_exists(o)) {
155 struct mdd_object *mdd_obj = lu2mdd_obj(o);
156 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
158 rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
164 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
166 struct mdd_object *mdd = lu2mdd_obj(o);
169 OBD_SLAB_FREE_PTR(mdd, mdd_object_kmem);
172 static int mdd_object_print(const struct lu_env *env, void *cookie,
173 lu_printer_t p, const struct lu_object *o)
175 struct mdd_object *mdd = lu2mdd_obj((struct lu_object *)o);
176 return (*p)(env, cookie, LUSTRE_MDD_NAME"-object@%p(open_count=%d, "
177 "valid=%x, cltime="LPU64", flags=%lx)",
178 mdd, mdd->mod_count, mdd->mod_valid,
179 mdd->mod_cltime, mdd->mod_flags);
182 static const struct lu_object_operations mdd_lu_obj_ops = {
183 .loo_object_init = mdd_object_init,
184 .loo_object_start = mdd_object_start,
185 .loo_object_free = mdd_object_free,
186 .loo_object_print = mdd_object_print,
189 struct mdd_object *mdd_object_find(const struct lu_env *env,
190 struct mdd_device *d,
191 const struct lu_fid *f)
193 return md2mdd_obj(md_object_find_slice(env, &d->mdd_md_dev, f));
197 * No permission check is needed.
199 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
202 struct mdd_object *mdd_obj = md2mdd_obj(obj);
207 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
208 mdd_object_capa(env, md2mdd_obj(obj)));
209 if ((ma->ma_need & MA_INODE) != 0 && mdd_is_dead_obj(mdd_obj))
210 ma->ma_attr.la_nlink = 0;
216 * No permission check is needed.
218 static int mdd_xattr_get(const struct lu_env *env,
219 struct md_object *obj, struct lu_buf *buf,
222 struct mdd_object *mdd_obj = md2mdd_obj(obj);
227 if (mdd_object_exists(mdd_obj) == 0) {
228 CERROR("%s: object "DFID" not found: rc = -2\n",
229 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
233 /* If the object has been delete from the namespace, then
234 * get linkEA should return -ENOENT as well */
235 if (unlikely((mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ)) &&
236 strcmp(name, XATTR_NAME_LINK) == 0))
239 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
240 rc = mdo_xattr_get(env, mdd_obj, buf, name,
241 mdd_object_capa(env, mdd_obj));
242 mdd_read_unlock(env, mdd_obj);
248 * Permission check is done when open,
249 * no need check again.
251 int mdd_readlink(const struct lu_env *env, struct md_object *obj,
254 struct mdd_object *mdd_obj = md2mdd_obj(obj);
255 struct dt_object *next;
260 if (mdd_object_exists(mdd_obj) == 0) {
261 CERROR("%s: object "DFID" not found: rc = -2\n",
262 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
266 next = mdd_object_child(mdd_obj);
267 LASSERT(next != NULL);
268 LASSERT(next->do_body_ops != NULL);
269 LASSERT(next->do_body_ops->dbo_read != NULL);
270 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
271 rc = next->do_body_ops->dbo_read(env, next, buf, &pos,
272 mdd_object_capa(env, mdd_obj));
273 mdd_read_unlock(env, mdd_obj);
278 * No permission check is needed.
280 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
283 struct mdd_object *mdd_obj = md2mdd_obj(obj);
288 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
289 rc = mdo_xattr_list(env, mdd_obj, buf, mdd_object_capa(env, mdd_obj));
290 mdd_read_unlock(env, mdd_obj);
295 int mdd_declare_object_create_internal(const struct lu_env *env,
296 struct mdd_object *p,
297 struct mdd_object *c,
298 struct lu_attr *attr,
299 struct thandle *handle,
300 const struct md_op_spec *spec,
301 struct dt_allocation_hint *hint)
303 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
304 const struct dt_index_features *feat = spec->sp_feat;
308 if (feat != &dt_directory_features && feat != NULL) {
309 dof->dof_type = DFT_INDEX;
310 dof->u.dof_idx.di_feat = feat;
313 dof->dof_type = dt_mode_to_dft(attr->la_mode);
314 if (dof->dof_type == DFT_REGULAR) {
315 dof->u.dof_reg.striped =
316 md_should_create(spec->sp_cr_flags);
317 if (spec->sp_cr_flags & MDS_OPEN_HAS_EA)
318 dof->u.dof_reg.striped = 0;
319 /* is this replay? */
321 dof->u.dof_reg.striped = 0;
325 rc = mdo_declare_create_obj(env, c, attr, hint, dof, handle);
330 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
331 struct mdd_object *c, struct lu_attr *attr,
332 struct thandle *handle,
333 const struct md_op_spec *spec,
334 struct dt_allocation_hint *hint)
336 struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
340 LASSERT(!mdd_object_exists(c));
342 rc = mdo_create_obj(env, c, attr, hint, dof, handle);
344 LASSERT(ergo(rc == 0, mdd_object_exists(c)));
349 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
350 const struct lu_attr *attr, struct thandle *handle,
356 rc = mdo_attr_set(env, obj, attr, handle, mdd_object_capa(env, obj));
357 #ifdef CONFIG_FS_POSIX_ACL
358 if (!rc && (attr->la_valid & LA_MODE) && needacl)
359 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
364 int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
365 const struct lu_attr *oattr, struct lu_attr *attr,
366 struct thandle *handle)
371 LASSERT(attr->la_valid & LA_CTIME);
372 LASSERT(oattr != NULL);
374 /* Make sure the ctime is increased only, however, it's not strictly
375 * reliable at here because there is not guarantee to hold lock on
376 * object, so we just bypass some unnecessary cmtime setting first
377 * and OSD has to check it again. */
378 if (attr->la_ctime < oattr->la_ctime)
379 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
380 else if (attr->la_valid == LA_CTIME &&
381 attr->la_ctime == oattr->la_ctime)
382 attr->la_valid &= ~LA_CTIME;
384 if (attr->la_valid != 0)
385 rc = mdd_attr_set_internal(env, obj, attr, handle, 0);
390 * This gives the same functionality as the code between
391 * sys_chmod and inode_setattr
392 * chown_common and inode_setattr
393 * utimes and inode_setattr
394 * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
396 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
397 const struct lu_attr *oattr, struct lu_attr *la,
398 const unsigned long flags)
407 /* Do not permit change file type */
408 if (la->la_valid & LA_TYPE)
411 /* They should not be processed by setattr */
412 if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
415 LASSERT(oattr != NULL);
417 /* export destroy does not have ->le_ses, but we may want
418 * to drop LUSTRE_SOM_FL. */
419 uc = lu_ucred_check(env);
423 if (la->la_valid == LA_CTIME) {
424 if (!(flags & MDS_PERM_BYPASS))
425 /* This is only for set ctime when rename's source is
427 rc = mdd_may_delete(env, NULL, NULL, obj, oattr, NULL,
429 if (rc == 0 && la->la_ctime <= oattr->la_ctime)
430 la->la_valid &= ~LA_CTIME;
434 if (la->la_valid == LA_ATIME) {
435 /* This is atime only set for read atime update on close. */
436 if (la->la_atime >= oattr->la_atime &&
437 la->la_atime < (oattr->la_atime +
438 mdd_obj2mdd_dev(obj)->mdd_atime_diff))
439 la->la_valid &= ~LA_ATIME;
443 /* Check if flags change. */
444 if (la->la_valid & LA_FLAGS) {
445 unsigned int oldflags = oattr->la_flags &
446 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
447 unsigned int newflags = la->la_flags &
448 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
450 if ((uc->uc_fsuid != oattr->la_uid) &&
451 !md_capable(uc, CFS_CAP_FOWNER))
454 /* The IMMUTABLE and APPEND_ONLY flags can
455 * only be changed by the relevant capability. */
456 if ((oldflags ^ newflags) &&
457 !md_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
460 if (!S_ISDIR(oattr->la_mode))
461 la->la_flags &= ~LUSTRE_DIRSYNC_FL;
464 if (oattr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL) &&
465 (la->la_valid & ~LA_FLAGS) &&
466 !(flags & MDS_PERM_BYPASS))
469 /* Check for setting the obj time. */
470 if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
471 !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
472 if ((uc->uc_fsuid != oattr->la_uid) &&
473 !md_capable(uc, CFS_CAP_FOWNER)) {
474 rc = mdd_permission_internal(env, obj, oattr,
481 if (la->la_valid & LA_KILL_SUID) {
482 la->la_valid &= ~LA_KILL_SUID;
483 if ((oattr->la_mode & S_ISUID) &&
484 !(la->la_valid & LA_MODE)) {
485 la->la_mode = oattr->la_mode;
486 la->la_valid |= LA_MODE;
488 la->la_mode &= ~S_ISUID;
491 if (la->la_valid & LA_KILL_SGID) {
492 la->la_valid &= ~LA_KILL_SGID;
493 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
494 (S_ISGID | S_IXGRP)) &&
495 !(la->la_valid & LA_MODE)) {
496 la->la_mode = oattr->la_mode;
497 la->la_valid |= LA_MODE;
499 la->la_mode &= ~S_ISGID;
502 /* Make sure a caller can chmod. */
503 if (la->la_valid & LA_MODE) {
504 if (!(flags & MDS_PERM_BYPASS) &&
505 (uc->uc_fsuid != oattr->la_uid) &&
506 !md_capable(uc, CFS_CAP_FOWNER))
509 if (la->la_mode == (umode_t) -1)
510 la->la_mode = oattr->la_mode;
512 la->la_mode = (la->la_mode & S_IALLUGO) |
513 (oattr->la_mode & ~S_IALLUGO);
515 /* Also check the setgid bit! */
516 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
517 la->la_gid : oattr->la_gid) &&
518 !md_capable(uc, CFS_CAP_FSETID))
519 la->la_mode &= ~S_ISGID;
521 la->la_mode = oattr->la_mode;
524 /* Make sure a caller can chown. */
525 if (la->la_valid & LA_UID) {
526 if (la->la_uid == (uid_t) -1)
527 la->la_uid = oattr->la_uid;
528 if (((uc->uc_fsuid != oattr->la_uid) ||
529 (la->la_uid != oattr->la_uid)) &&
530 !md_capable(uc, CFS_CAP_CHOWN))
533 /* If the user or group of a non-directory has been
534 * changed by a non-root user, remove the setuid bit.
535 * 19981026 David C Niemi <niemi@tux.org>
537 * Changed this to apply to all users, including root,
538 * to avoid some races. This is the behavior we had in
539 * 2.0. The check for non-root was definitely wrong
540 * for 2.2 anyway, as it should have been using
541 * CAP_FSETID rather than fsuid -- 19990830 SD. */
542 if (((oattr->la_mode & S_ISUID) == S_ISUID) &&
543 !S_ISDIR(oattr->la_mode)) {
544 la->la_mode &= ~S_ISUID;
545 la->la_valid |= LA_MODE;
549 /* Make sure caller can chgrp. */
550 if (la->la_valid & LA_GID) {
551 if (la->la_gid == (gid_t) -1)
552 la->la_gid = oattr->la_gid;
553 if (((uc->uc_fsuid != oattr->la_uid) ||
554 ((la->la_gid != oattr->la_gid) &&
555 !lustre_in_group_p(uc, la->la_gid))) &&
556 !md_capable(uc, CFS_CAP_CHOWN))
559 /* Likewise, if the user or group of a non-directory
560 * has been changed by a non-root user, remove the
561 * setgid bit UNLESS there is no group execute bit
562 * (this would be a file marked for mandatory
563 * locking). 19981026 David C Niemi <niemi@tux.org>
565 * Removed the fsuid check (see the comment above) --
567 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
568 (S_ISGID | S_IXGRP)) && !S_ISDIR(oattr->la_mode)) {
569 la->la_mode &= ~S_ISGID;
570 la->la_valid |= LA_MODE;
574 /* For both Size-on-MDS case and truncate case,
575 * "la->la_valid & (LA_SIZE | LA_BLOCKS)" are ture.
576 * We distinguish them by "flags & MDS_SOM".
577 * For SOM case, it is true, the MAY_WRITE perm has been checked
578 * when open, no need check again. For truncate case, it is false,
579 * the MAY_WRITE perm should be checked here. */
580 if (flags & MDS_SOM) {
581 /* For the "Size-on-MDS" setattr update, merge coming
582 * attributes with the set in the inode. BUG 10641 */
583 if ((la->la_valid & LA_ATIME) &&
584 (la->la_atime <= oattr->la_atime))
585 la->la_valid &= ~LA_ATIME;
587 /* OST attributes do not have a priority over MDS attributes,
588 * so drop times if ctime is equal. */
589 if ((la->la_valid & LA_CTIME) &&
590 (la->la_ctime <= oattr->la_ctime))
591 la->la_valid &= ~(LA_MTIME | LA_CTIME);
593 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
594 if (!((flags & MDS_OWNEROVERRIDE) &&
595 (uc->uc_fsuid == oattr->la_uid)) &&
596 !(flags & MDS_PERM_BYPASS)) {
597 rc = mdd_permission_internal(env, obj,
603 if (la->la_valid & LA_CTIME) {
604 /* The pure setattr, it has the priority over what is
605 * already set, do not drop it if ctime is equal. */
606 if (la->la_ctime < oattr->la_ctime)
607 la->la_valid &= ~(LA_ATIME | LA_MTIME |
615 /** Store a data change changelog record
616 * If this fails, we must fail the whole transaction; we don't
617 * want the change to commit without the log entry.
618 * \param mdd_obj - mdd_object of change
619 * \param handle - transacion handle
621 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
622 enum changelog_rec_type type, int flags,
623 struct mdd_object *mdd_obj, struct thandle *handle)
625 const struct lu_ucred *uc = lu_ucred(env);
626 const struct lu_fid *tfid;
627 struct llog_changelog_rec *rec;
633 if (!(mdd->mdd_cl.mc_flags & CLM_ON))
635 if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
638 LASSERT(mdd_obj != NULL);
639 LASSERT(handle != NULL);
641 tfid = mdo2fid(mdd_obj);
643 if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
644 cfs_time_before_64(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
645 /* Don't need multiple updates in this log */
646 /* Don't check under lock - no big deal if we get an extra
651 flags = (flags & CLF_FLAGMASK) | CLF_VERSION;
652 if (uc->uc_jobid[0] != '\0')
655 reclen = llog_data_len(changelog_rec_offset(flags & CLF_SUPPORTED));
656 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
657 if (buf->lb_buf == NULL)
661 rec->cr.cr_flags = flags;
662 rec->cr.cr_type = (__u32)type;
663 rec->cr.cr_tfid = *tfid;
664 rec->cr.cr_namelen = 0;
665 mdd_obj->mod_cltime = cfs_time_current_64();
667 if (flags & CLF_JOBID)
668 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
670 rc = mdd_changelog_store(env, mdd, rec, handle);
675 static int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
676 int flags, struct md_object *obj)
678 struct thandle *handle;
679 struct mdd_object *mdd_obj = md2mdd_obj(obj);
680 struct mdd_device *mdd = mdo2mdd(obj);
684 handle = mdd_trans_create(env, mdd);
686 RETURN(PTR_ERR(handle));
688 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
692 rc = mdd_trans_start(env, mdd, handle);
696 rc = mdd_changelog_data_store(env, mdd, type, flags, mdd_obj,
700 mdd_trans_stop(env, mdd, rc, handle);
706 * Save LMA extended attributes with data from \a ma.
708 * HSM and Size-On-MDS data will be extracted from \ma if they are valid, if
709 * not, LMA EA will be first read from disk, modified and write back.
712 /* Precedence for choosing record type when multiple
713 * attributes change: setattr > mtime > ctime > atime
714 * (ctime changes when mtime does, plus chmod/chown.
715 * atime and ctime are independent.) */
716 static int mdd_attr_set_changelog(const struct lu_env *env,
717 struct md_object *obj, struct thandle *handle,
720 struct mdd_device *mdd = mdo2mdd(obj);
723 bits = (valid & LA_SIZE) ? 1 << CL_TRUNC : 0;
724 bits |= (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
725 bits |= (valid & LA_MTIME) ? 1 << CL_MTIME : 0;
726 bits |= (valid & LA_CTIME) ? 1 << CL_CTIME : 0;
727 bits |= (valid & LA_ATIME) ? 1 << CL_ATIME : 0;
728 bits = bits & mdd->mdd_cl.mc_mask;
729 /* This is an implementation limit rather than a protocol limit */
730 CLASSERT(CL_LAST <= sizeof(int) * 8);
734 /* The record type is the lowest non-masked set bit */
737 /* FYI we only store the first CLF_FLAGMASK bits of la_valid */
738 return mdd_changelog_data_store(env, mdd, type, (int)valid,
739 md2mdd_obj(obj), handle);
742 static int mdd_declare_attr_set(const struct lu_env *env,
743 struct mdd_device *mdd,
744 struct mdd_object *obj,
745 const struct lu_attr *attr,
746 struct thandle *handle)
750 rc = mdo_declare_attr_set(env, obj, attr, handle);
754 #ifdef CONFIG_FS_POSIX_ACL
755 if (attr->la_valid & LA_MODE) {
756 mdd_read_lock(env, obj, MOR_TGT_CHILD);
757 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
758 XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
759 mdd_read_unlock(env, obj);
760 if (rc == -EOPNOTSUPP || rc == -ENODATA)
766 struct lu_buf *buf = mdd_buf_get(env, NULL, rc);
767 rc = mdo_declare_xattr_set(env, obj, buf,
768 XATTR_NAME_ACL_ACCESS, 0,
776 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
783 * permission changes may require sync operation, to mitigate performance
784 * impact, only do this for dir and when permission is reduced.
786 * For regular files, version is updated with permission change (see VBR), async
787 * permission won't cause any issue, while missing permission change on
788 * directory may affect accessibility of other objects after recovery.
790 static inline bool permission_needs_sync(const struct lu_attr *old,
791 const struct lu_attr *new)
793 if (!S_ISDIR(old->la_mode))
796 if (new->la_valid & (LA_UID | LA_GID))
799 if (new->la_valid & LA_MODE &&
800 new->la_mode & (S_ISUID | S_ISGID | S_ISVTX))
803 if ((new->la_valid & LA_MODE) &&
804 ((new->la_mode & old->la_mode) & S_IRWXUGO) !=
805 (old->la_mode & S_IRWXUGO))
811 /* set attr and LOV EA at once, return updated attr */
812 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
813 const struct md_attr *ma)
815 struct mdd_object *mdd_obj = md2mdd_obj(obj);
816 struct mdd_device *mdd = mdo2mdd(obj);
817 struct thandle *handle;
818 struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
819 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
820 const struct lu_attr *la = &ma->ma_attr;
824 /* we do not use ->attr_set() for LOV/SOM/HSM EA any more */
825 LASSERT((ma->ma_valid & MA_LOV) == 0);
826 LASSERT((ma->ma_valid & MA_HSM) == 0);
827 LASSERT((ma->ma_valid & MA_SOM) == 0);
829 rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
833 *la_copy = ma->ma_attr;
834 rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma->ma_attr_flags);
838 /* setattr on "close" only change atime, or do nothing */
839 if (la->la_valid == LA_ATIME && la_copy->la_valid == 0)
842 handle = mdd_trans_create(env, mdd);
844 RETURN(PTR_ERR(handle));
846 rc = mdd_declare_attr_set(env, mdd, mdd_obj, la, handle);
850 rc = mdd_trans_start(env, mdd, handle);
854 if (mdd->mdd_sync_permission && permission_needs_sync(attr, la))
857 if (la->la_valid & (LA_MTIME | LA_CTIME))
858 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
859 la->la_mtime, la->la_ctime);
861 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
862 if (la_copy->la_valid & LA_FLAGS)
863 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
864 else if (la_copy->la_valid) /* setattr */
865 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
866 mdd_write_unlock(env, mdd_obj);
869 rc = mdd_attr_set_changelog(env, obj, handle, la->la_valid);
874 mdd_trans_stop(env, mdd, rc, handle);
878 static int mdd_xattr_sanity_check(const struct lu_env *env,
879 struct mdd_object *obj,
880 const struct lu_attr *attr)
882 struct lu_ucred *uc = lu_ucred_assert(env);
885 if (attr->la_flags & (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL))
888 if ((uc->uc_fsuid != attr->la_uid) && !md_capable(uc, CFS_CAP_FOWNER))
894 static int mdd_declare_xattr_set(const struct lu_env *env,
895 struct mdd_device *mdd,
896 struct mdd_object *obj,
897 const struct lu_buf *buf,
899 int fl, struct thandle *handle)
903 rc = mdo_declare_xattr_set(env, obj, buf, name, fl, handle);
907 /* Only record user and layout xattr changes */
908 if (strncmp(XATTR_USER_PREFIX, name,
909 sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
910 strcmp(XATTR_NAME_LOV, name) == 0) {
911 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
916 /* If HSM data is modified, this could add a changelog */
917 if (strcmp(XATTR_NAME_HSM, name) == 0) {
918 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
923 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
928 * Compare current and future data of HSM EA and add a changelog if needed.
930 * Caller should have write-locked \param obj.
932 * \param buf - Future HSM EA content.
933 * \retval 0 if no changelog is needed or changelog was added properly.
934 * \retval -ve errno if there was a problem
936 static int mdd_hsm_update_locked(const struct lu_env *env,
937 struct md_object *obj,
938 const struct lu_buf *buf,
939 struct thandle *handle)
941 struct mdd_thread_info *info = mdd_env_info(env);
942 struct mdd_device *mdd = mdo2mdd(obj);
943 struct mdd_object *mdd_obj = md2mdd_obj(obj);
944 struct lu_buf *current_buf;
945 struct md_hsm *current_mh;
946 struct md_hsm *new_mh;
950 OBD_ALLOC_PTR(current_mh);
951 if (current_mh == NULL)
954 /* Read HSM attrs from disk */
955 CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
956 current_buf = mdd_buf_get(env, info->mti_xattr_buf,
957 sizeof(info->mti_xattr_buf));
958 rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM,
959 mdd_object_capa(env, mdd_obj));
960 rc = lustre_buf2hsm(current_buf->lb_buf, rc, current_mh);
961 if (rc < 0 && rc != -ENODATA)
963 else if (rc == -ENODATA)
964 current_mh->mh_flags = 0;
966 /* Map future HSM xattr */
967 OBD_ALLOC_PTR(new_mh);
969 GOTO(free, rc = -ENOMEM);
970 lustre_buf2hsm(buf->lb_buf, buf->lb_len, new_mh);
972 /* If HSM flags are different, add a changelog */
974 if (current_mh->mh_flags != new_mh->mh_flags) {
976 hsm_set_cl_event(&flags, HE_STATE);
977 if (new_mh->mh_flags & HS_DIRTY)
978 hsm_set_cl_flags(&flags, CLF_HSM_DIRTY);
980 rc = mdd_changelog_data_store(env, mdd, CL_HSM, flags, mdd_obj,
984 OBD_FREE_PTR(new_mh);
986 OBD_FREE_PTR(current_mh);
990 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
994 * The caller should guarantee to update the object ctime
995 * after xattr_set if needed.
997 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
998 const struct lu_buf *buf, const char *name,
1001 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1002 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1003 struct mdd_device *mdd = mdo2mdd(obj);
1004 struct thandle *handle;
1008 rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1012 rc = mdd_xattr_sanity_check(env, mdd_obj, attr);
1016 if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0 ||
1017 strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
1018 struct posix_acl *acl;
1020 /* user may set empty ACL, which should be treated as removing
1022 acl = posix_acl_from_xattr(&init_user_ns, buf->lb_buf,
1025 rc = mdd_xattr_del(env, obj, name);
1028 posix_acl_release(acl);
1031 if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
1032 rc = mdd_acl_set(env, mdd_obj, attr, buf, fl);
1036 handle = mdd_trans_create(env, mdd);
1038 RETURN(PTR_ERR(handle));
1040 rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
1044 rc = mdd_trans_start(env, mdd, handle);
1048 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1050 if (strcmp(XATTR_NAME_HSM, name) == 0) {
1051 rc = mdd_hsm_update_locked(env, obj, buf, handle);
1053 mdd_write_unlock(env, mdd_obj);
1058 rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle,
1059 mdd_object_capa(env, mdd_obj));
1060 mdd_write_unlock(env, mdd_obj);
1064 if (strcmp(XATTR_NAME_LOV, name) == 0)
1065 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, mdd_obj,
1067 else if (strncmp(XATTR_USER_PREFIX, name,
1068 sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1069 strncmp(POSIX_ACL_XATTR_ACCESS, name,
1070 sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1071 strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1072 sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1073 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1077 mdd_trans_stop(env, mdd, rc, handle);
1082 static int mdd_declare_xattr_del(const struct lu_env *env,
1083 struct mdd_device *mdd,
1084 struct mdd_object *obj,
1086 struct thandle *handle)
1090 rc = mdo_declare_xattr_del(env, obj, name, handle);
1094 /* Only record user xattr changes */
1095 if ((strncmp(XATTR_USER_PREFIX, name,
1096 sizeof(XATTR_USER_PREFIX) - 1) == 0))
1097 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1103 * The caller should guarantee to update the object ctime
1104 * after xattr_set if needed.
1106 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1109 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1110 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1111 struct mdd_device *mdd = mdo2mdd(obj);
1112 struct thandle *handle;
1116 rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1120 rc = mdd_xattr_sanity_check(env, mdd_obj, attr);
1124 handle = mdd_trans_create(env, mdd);
1126 RETURN(PTR_ERR(handle));
1128 rc = mdd_declare_xattr_del(env, mdd, mdd_obj, name, handle);
1132 rc = mdd_trans_start(env, mdd, handle);
1136 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1137 rc = mdo_xattr_del(env, mdd_obj, name, handle,
1138 mdd_object_capa(env, mdd_obj));
1139 mdd_write_unlock(env, mdd_obj);
1143 /* Only record system & user xattr changes */
1144 if (strncmp(XATTR_USER_PREFIX, name,
1145 sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1146 strncmp(POSIX_ACL_XATTR_ACCESS, name,
1147 sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1148 strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1149 sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1150 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1154 mdd_trans_stop(env, mdd, rc, handle);
1160 * read lov EA of an object
1161 * return the lov EA in an allocated lu_buf
1163 int mdd_get_lov_ea(const struct lu_env *env, struct mdd_object *obj,
1164 struct lu_buf *lmm_buf)
1166 struct lu_buf *buf = &mdd_env_info(env)->mti_big_buf;
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);
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 lu_buf_alloc(lmm_buf, bufsize);
1202 if (lmm_buf->lb_buf == NULL)
1203 GOTO(out, rc = -ENOMEM);
1205 memcpy(lmm_buf->lb_buf, buf->lb_buf, bufsize);
1211 lu_buf_free(lmm_buf);
1215 static int mdd_xattr_hsm_replace(const struct lu_env *env,
1216 struct mdd_object *o, struct lu_buf *buf,
1217 struct thandle *handle)
1219 struct hsm_attrs *attrs;
1225 rc = mdo_xattr_set(env, o, buf, XATTR_NAME_HSM, LU_XATTR_REPLACE,
1226 handle, mdd_object_capa(env, o));
1230 attrs = buf->lb_buf;
1231 hsm_flags = le32_to_cpu(attrs->hsm_flags);
1232 if (!(hsm_flags & HS_RELEASED) || mdd_is_dead_obj(o))
1235 /* Add a changelog record for release. */
1236 hsm_set_cl_event(&flags, HE_RELEASE);
1237 rc = mdd_changelog_data_store(env, mdo2mdd(&o->mod_obj), CL_HSM,
1243 * check if layout swapping between 2 objects is allowed
1245 * - only normal FIDs or non-system IGIFs
1246 * - same type of objects
1247 * - same owner/group (so quotas are still valid)
1249 static int mdd_layout_swap_allowed(const struct lu_env *env,
1250 struct mdd_object *o1,
1251 const struct lu_attr *attr1,
1252 struct mdd_object *o2,
1253 const struct lu_attr *attr2)
1255 const struct lu_fid *fid1, *fid2;
1261 if (!fid_is_norm(fid1) &&
1262 (!fid_is_igif(fid1) || IS_ERR(mdd_links_get(env, o1))))
1265 if (!fid_is_norm(fid2) &&
1266 (!fid_is_igif(fid2) || IS_ERR(mdd_links_get(env, o2))))
1269 if (mdd_object_type(o1) != mdd_object_type(o2)) {
1270 if (S_ISDIR(mdd_object_type(o1)))
1272 if (S_ISREG(mdd_object_type(o1)))
1277 if ((attr1->la_uid != attr2->la_uid) ||
1278 (attr1->la_gid != attr2->la_gid))
1285 * swap layouts between 2 lustre objects
1287 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
1288 struct md_object *obj2, __u64 flags)
1290 struct mdd_thread_info *info = mdd_env_info(env);
1291 struct mdd_object *fst_o = md2mdd_obj(obj1);
1292 struct mdd_object *snd_o = md2mdd_obj(obj2);
1293 struct lu_attr *fst_la = MDD_ENV_VAR(env, cattr);
1294 struct lu_attr *snd_la = MDD_ENV_VAR(env, tattr);
1295 struct mdd_device *mdd = mdo2mdd(obj1);
1296 struct lov_mds_md *fst_lmm, *snd_lmm;
1297 struct lu_buf *fst_buf = &info->mti_buf[0];
1298 struct lu_buf *snd_buf = &info->mti_buf[1];
1299 struct lu_buf *fst_hsm_buf = &info->mti_buf[2];
1300 struct lu_buf *snd_hsm_buf = &info->mti_buf[3];
1301 struct ost_id *saved_oi = NULL;
1302 struct thandle *handle;
1303 __u16 fst_gen, snd_gen;
1309 CLASSERT(ARRAY_SIZE(info->mti_buf) >= 4);
1310 memset(info->mti_buf, 0, sizeof(info->mti_buf));
1312 /* we have to sort the 2 obj, so locking will always
1313 * be in the same order, even in case of 2 concurrent swaps */
1314 rc = lu_fid_cmp(mdo2fid(fst_o), mdo2fid(snd_o));
1315 if (rc == 0) /* same fid ? */
1321 rc = mdd_la_get(env, fst_o, fst_la, BYPASS_CAPA);
1325 rc = mdd_la_get(env, snd_o, snd_la, BYPASS_CAPA);
1329 /* check if layout swapping is allowed */
1330 rc = mdd_layout_swap_allowed(env, fst_o, fst_la, snd_o, snd_la);
1334 handle = mdd_trans_create(env, mdd);
1336 RETURN(PTR_ERR(handle));
1338 /* objects are already sorted */
1339 mdd_write_lock(env, fst_o, MOR_TGT_CHILD);
1340 mdd_write_lock(env, snd_o, MOR_TGT_CHILD);
1342 rc = mdd_get_lov_ea(env, fst_o, fst_buf);
1343 if (rc < 0 && rc != -ENODATA)
1346 rc = mdd_get_lov_ea(env, snd_o, snd_buf);
1347 if (rc < 0 && rc != -ENODATA)
1350 /* swapping 2 non existant layouts is a success */
1351 if (fst_buf->lb_buf == NULL && snd_buf->lb_buf == NULL)
1354 /* to help inode migration between MDT, it is better to
1355 * start by the no layout file (if one), so we order the swap */
1356 if (snd_buf->lb_buf == NULL) {
1358 swap(fst_buf, snd_buf);
1361 /* lmm and generation layout initialization */
1362 if (fst_buf->lb_buf != NULL) {
1363 fst_lmm = fst_buf->lb_buf;
1364 fst_gen = le16_to_cpu(fst_lmm->lmm_layout_gen);
1365 fst_fl = LU_XATTR_REPLACE;
1369 fst_fl = LU_XATTR_CREATE;
1372 snd_lmm = snd_buf->lb_buf;
1373 snd_gen = le16_to_cpu(snd_lmm->lmm_layout_gen);
1375 /* increase the generation layout numbers */
1379 /* set the file specific informations in lmm */
1380 if (fst_lmm != NULL) {
1381 saved_oi = &info->mti_oa.o_oi;
1383 *saved_oi = fst_lmm->lmm_oi;
1384 fst_lmm->lmm_layout_gen = cpu_to_le16(snd_gen);
1385 fst_lmm->lmm_oi = snd_lmm->lmm_oi;
1386 snd_lmm->lmm_oi = *saved_oi;
1388 if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1))
1389 snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
1390 else if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3))
1391 snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V3_DEF);
1393 GOTO(stop, rc = -EPROTO);
1395 snd_lmm->lmm_layout_gen = cpu_to_le16(fst_gen);
1397 /* Prepare HSM attribute if it's required */
1398 if (flags & SWAP_LAYOUTS_MDS_HSM) {
1399 const int buflen = sizeof(struct hsm_attrs);
1401 lu_buf_alloc(fst_hsm_buf, buflen);
1402 lu_buf_alloc(snd_hsm_buf, buflen);
1403 if (fst_hsm_buf->lb_buf == NULL || snd_hsm_buf->lb_buf == NULL)
1404 GOTO(stop, rc = -ENOMEM);
1406 /* Read HSM attribute */
1407 rc = mdo_xattr_get(env, fst_o, fst_hsm_buf, XATTR_NAME_HSM,
1412 rc = mdo_xattr_get(env, snd_o, snd_hsm_buf, XATTR_NAME_HSM,
1417 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_hsm_buf,
1418 XATTR_NAME_HSM, LU_XATTR_REPLACE,
1423 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_hsm_buf,
1424 XATTR_NAME_HSM, LU_XATTR_REPLACE,
1430 /* prepare transaction */
1431 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
1436 if (fst_buf->lb_buf != NULL)
1437 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf,
1438 XATTR_NAME_LOV, LU_XATTR_REPLACE,
1441 rc = mdd_declare_xattr_del(env, mdd, snd_o, XATTR_NAME_LOV,
1446 rc = mdd_trans_start(env, mdd, handle);
1450 if (flags & SWAP_LAYOUTS_MDS_HSM) {
1451 rc = mdd_xattr_hsm_replace(env, fst_o, snd_hsm_buf, handle);
1455 rc = mdd_xattr_hsm_replace(env, snd_o, fst_hsm_buf, handle);
1457 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
1460 CERROR("%s: restore "DFID" HSM error: %d/%d\n",
1461 mdd_obj_dev_name(fst_o),
1462 PFID(mdo2fid(fst_o)), rc, rc2);
1467 rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV, fst_fl, handle,
1468 mdd_object_capa(env, fst_o));
1472 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_SWAP_LAYOUTS))) {
1475 if (fst_buf->lb_buf != NULL)
1476 rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
1477 LU_XATTR_REPLACE, handle,
1478 mdd_object_capa(env, snd_o));
1480 rc = mdo_xattr_del(env, snd_o, XATTR_NAME_LOV, handle,
1481 mdd_object_capa(env, snd_o));
1487 /* failure on second file, but first was done, so we have
1488 * to roll back first. */
1489 if (fst_buf->lb_buf != NULL) {
1490 fst_lmm->lmm_oi = *saved_oi;
1491 fst_lmm->lmm_layout_gen = cpu_to_le16(fst_gen - 1);
1492 rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
1493 LU_XATTR_REPLACE, handle,
1494 mdd_object_capa(env, fst_o));
1496 rc2 = mdo_xattr_del(env, fst_o, XATTR_NAME_LOV, handle,
1497 mdd_object_capa(env, fst_o));
1503 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf, handle);
1508 rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf, handle);
1513 CERROR("%s: unable to roll back layout swap. FIDs: "
1514 DFID" and "DFID "error: %d/%d, steps: %d\n",
1515 mdd_obj_dev_name(fst_o),
1516 PFID(mdo2fid(snd_o)), PFID(mdo2fid(fst_o)),
1518 /* a solution to avoid journal commit is to panic,
1519 * but it has strong consequences so we use LBUG to
1520 * allow sysdamin to choose to panic or not
1527 /* Issue one changelog record per file */
1528 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle);
1532 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle);
1538 mdd_trans_stop(env, mdd, rc, handle);
1539 mdd_write_unlock(env, snd_o);
1540 mdd_write_unlock(env, fst_o);
1542 lu_buf_free(fst_buf);
1543 lu_buf_free(snd_buf);
1544 lu_buf_free(fst_hsm_buf);
1545 lu_buf_free(snd_hsm_buf);
1549 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
1550 struct mdd_object *child, const struct lu_attr *attr,
1551 const struct md_op_spec *spec,
1552 struct dt_allocation_hint *hint)
1554 struct dt_object *np = parent ? mdd_object_child(parent) : NULL;
1555 struct dt_object *nc = mdd_object_child(child);
1557 memset(hint, 0, sizeof(*hint));
1559 /* For striped directory, give striping EA to lod_ah_init, which will
1560 * decide the stripe_offset and stripe count by it. */
1561 if (S_ISDIR(attr->la_mode) &&
1562 unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1563 hint->dah_eadata = spec->u.sp_ea.eadata;
1564 hint->dah_eadata_len = spec->u.sp_ea.eadatalen;
1566 hint->dah_eadata = NULL;
1567 hint->dah_eadata_len = 0;
1570 CDEBUG(D_INFO, DFID" eadata %p len %d\n", PFID(mdd_object_fid(child)),
1571 hint->dah_eadata, hint->dah_eadata_len);
1572 /* @hint will be initialized by underlying device. */
1573 nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
1577 * do NOT or the MAY_*'s, you'll get the weakest
1579 int accmode(const struct lu_env *env, const struct lu_attr *la, int flags)
1583 /* Sadly, NFSD reopens a file repeatedly during operation, so the
1584 * "acc_mode = 0" allowance for newly-created files isn't honoured.
1585 * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1586 * owner can write to a file even if it is marked readonly to hide
1587 * its brokenness. (bug 5781) */
1588 if (flags & MDS_OPEN_OWNEROVERRIDE) {
1589 struct lu_ucred *uc = lu_ucred_check(env);
1591 if ((uc == NULL) || (la->la_uid == uc->uc_fsuid))
1595 if (flags & FMODE_READ)
1597 if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
1599 if (flags & MDS_FMODE_EXEC)
1604 static int mdd_open_sanity_check(const struct lu_env *env,
1605 struct mdd_object *obj,
1606 const struct lu_attr *attr, int flag)
1612 if (mdd_is_dead_obj(obj))
1615 if (S_ISLNK(attr->la_mode))
1618 mode = accmode(env, attr, flag);
1620 if (S_ISDIR(attr->la_mode) && (mode & MAY_WRITE))
1623 if (!(flag & MDS_OPEN_CREATED)) {
1624 rc = mdd_permission_internal(env, obj, attr, mode);
1629 if (S_ISFIFO(attr->la_mode) || S_ISSOCK(attr->la_mode) ||
1630 S_ISBLK(attr->la_mode) || S_ISCHR(attr->la_mode))
1631 flag &= ~MDS_OPEN_TRUNC;
1633 /* For writing append-only file must open it with append mode. */
1634 if (attr->la_flags & LUSTRE_APPEND_FL) {
1635 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
1637 if (flag & MDS_OPEN_TRUNC)
1643 * Now, flag -- O_NOATIME does not be packed by client.
1645 if (flag & O_NOATIME) {
1646 struct lu_ucred *uc = lu_ucred(env);
1648 if (uc && ((uc->uc_valid == UCRED_OLD) ||
1649 (uc->uc_valid == UCRED_NEW)) &&
1650 (uc->uc_fsuid != attr->la_uid) &&
1651 !md_capable(uc, CFS_CAP_FOWNER))
1659 static int mdd_open(const struct lu_env *env, struct md_object *obj,
1662 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1663 struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1666 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1668 rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1672 rc = mdd_open_sanity_check(env, mdd_obj, attr, flags);
1676 mdd_obj->mod_count++;
1679 mdd_write_unlock(env, mdd_obj);
1683 static int mdd_declare_close(const struct lu_env *env,
1684 struct mdd_object *obj,
1686 struct thandle *handle)
1690 rc = orph_declare_index_delete(env, obj, handle);
1694 return mdo_declare_destroy(env, obj, handle);
1698 * No permission check is needed.
1700 static int mdd_close(const struct lu_env *env, struct md_object *obj,
1701 struct md_attr *ma, int mode)
1703 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1704 struct mdd_device *mdd = mdo2mdd(obj);
1705 struct thandle *handle = NULL;
1706 int rc, is_orphan = 0;
1709 if (ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_KEEP_ORPHAN) {
1710 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1711 mdd_obj->mod_count--;
1712 mdd_write_unlock(env, mdd_obj);
1714 if (mdd_obj->mod_flags & ORPHAN_OBJ && !mdd_obj->mod_count)
1715 CDEBUG(D_HA, "Object "DFID" is retained in orphan "
1716 "list\n", PFID(mdd_object_fid(mdd_obj)));
1720 /* mdd_finish_unlink() will always set orphan object as DEAD_OBJ, but
1721 * it might fail to add the object to orphan list (w/o ORPHAN_OBJ). */
1722 /* check without any lock */
1723 is_orphan = mdd_obj->mod_count == 1 &&
1724 (mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0;
1728 handle = mdd_trans_create(env, mdo2mdd(obj));
1730 RETURN(PTR_ERR(handle));
1732 rc = mdd_declare_close(env, mdd_obj, ma, handle);
1736 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1740 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1745 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1746 rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
1747 mdd_object_capa(env, mdd_obj));
1749 CERROR("Failed to get lu_attr of "DFID": %d\n",
1750 PFID(mdd_object_fid(mdd_obj)), rc);
1754 /* check again with lock */
1755 is_orphan = (mdd_obj->mod_count == 1) &&
1756 ((mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0 ||
1757 ma->ma_attr.la_nlink == 0);
1759 if (is_orphan && handle == NULL) {
1760 mdd_write_unlock(env, mdd_obj);
1764 mdd_obj->mod_count--; /*release open count */
1770 /* NB: Object maybe not in orphan list originally, it is rare case for
1771 * mdd_finish_unlink() failure, in that case, the object doesn't have
1772 * ORPHAN_OBJ flag */
1773 if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
1774 /* remove link to object from orphan index */
1775 LASSERT(handle != NULL);
1776 rc = __mdd_orphan_del(env, mdd_obj, handle);
1778 CERROR("%s: unable to delete "DFID" from orphan list: "
1779 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1780 PFID(mdd_object_fid(mdd_obj)), rc);
1781 /* If object was not deleted from orphan list, do not
1782 * destroy OSS objects, which will be done when next
1787 CDEBUG(D_HA, "Object "DFID" is deleted from orphan "
1788 "list, OSS objects to be destroyed.\n",
1789 PFID(mdd_object_fid(mdd_obj)));
1792 rc = mdo_destroy(env, mdd_obj, handle);
1795 CERROR("%s: unable to delete "DFID" from orphan list: "
1796 "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1797 PFID(mdd_object_fid(mdd_obj)), rc);
1802 mdd_write_unlock(env, mdd_obj);
1805 (mode & (FMODE_WRITE | MDS_OPEN_APPEND | MDS_OPEN_TRUNC)) &&
1806 !(ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_RECOV_OPEN)) {
1807 if (handle == NULL) {
1808 handle = mdd_trans_create(env, mdo2mdd(obj));
1810 GOTO(stop, rc = PTR_ERR(handle));
1812 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL,
1817 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1822 mdd_changelog_data_store(env, mdd, CL_CLOSE, mode,
1827 if (handle != NULL && !IS_ERR(handle))
1828 mdd_trans_stop(env, mdd, rc, handle);
1834 * Permission check is done when open,
1835 * no need check again.
1837 static int mdd_readpage_sanity_check(const struct lu_env *env,
1838 struct mdd_object *obj)
1840 struct dt_object *next = mdd_object_child(obj);
1844 if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
1852 static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
1853 int nob, const struct dt_it_ops *iops,
1854 struct dt_it *it, __u32 attr, void *arg)
1856 struct lu_dirpage *dp = &lp->lp_dir;
1860 struct lu_dirent *ent;
1861 struct lu_dirent *last = NULL;
1865 memset(area, 0, sizeof (*dp));
1866 area += sizeof (*dp);
1867 nob -= sizeof (*dp);
1874 len = iops->key_size(env, it);
1876 /* IAM iterator can return record with zero len. */
1880 hash = iops->store(env, it);
1881 if (unlikely(first)) {
1883 dp->ldp_hash_start = cpu_to_le64(hash);
1886 /* calculate max space required for lu_dirent */
1887 recsize = lu_dirent_calc_size(len, attr);
1889 if (nob >= recsize) {
1890 result = iops->rec(env, it, (struct dt_rec *)ent, attr);
1891 if (result == -ESTALE)
1896 /* osd might not able to pack all attributes,
1897 * so recheck rec length */
1898 recsize = le16_to_cpu(ent->lde_reclen);
1900 if (le32_to_cpu(ent->lde_attrs) & LUDA_FID) {
1901 fid_le_to_cpu(&fid, &ent->lde_fid);
1902 if (fid_is_dot_lustre(&fid))
1906 result = (last != NULL) ? 0 :-EINVAL;
1910 ent = (void *)ent + recsize;
1914 result = iops->next(env, it);
1915 if (result == -ESTALE)
1917 } while (result == 0);
1920 dp->ldp_hash_end = cpu_to_le64(hash);
1922 if (last->lde_hash == dp->ldp_hash_end)
1923 dp->ldp_flags |= cpu_to_le32(LDF_COLLIDE);
1924 last->lde_reclen = 0; /* end mark */
1927 /* end of directory */
1928 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
1929 else if (result < 0)
1930 CWARN("build page failed: %d!\n", result);
1934 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
1935 const struct lu_rdpg *rdpg)
1937 struct mdd_object *mdd_obj = md2mdd_obj(obj);
1941 if (mdd_object_exists(mdd_obj) == 0) {
1942 CERROR("%s: object "DFID" not found: rc = -2\n",
1943 mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
1947 mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
1948 rc = mdd_readpage_sanity_check(env, mdd_obj);
1950 GOTO(out_unlock, rc);
1952 if (mdd_is_dead_obj(mdd_obj)) {
1954 struct lu_dirpage *dp;
1957 * According to POSIX, please do not return any entry to client:
1958 * even dot and dotdot should not be returned.
1960 CDEBUG(D_INODE, "readdir from dead object: "DFID"\n",
1961 PFID(mdd_object_fid(mdd_obj)));
1963 if (rdpg->rp_count <= 0)
1964 GOTO(out_unlock, rc = -EFAULT);
1965 LASSERT(rdpg->rp_pages != NULL);
1967 pg = rdpg->rp_pages[0];
1968 dp = (struct lu_dirpage *)kmap(pg);
1969 memset(dp, 0 , sizeof(struct lu_dirpage));
1970 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
1971 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
1972 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
1974 GOTO(out_unlock, rc = LU_PAGE_SIZE);
1977 rc = dt_index_walk(env, mdd_object_child(mdd_obj), rdpg,
1978 mdd_dir_page_build, NULL);
1980 struct lu_dirpage *dp;
1982 dp = kmap(rdpg->rp_pages[0]);
1983 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
1986 * No pages were processed, mark this for first page
1989 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
1990 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
1991 rc = min_t(unsigned int, LU_PAGE_SIZE, rdpg->rp_count);
1993 kunmap(rdpg->rp_pages[0]);
1996 GOTO(out_unlock, rc);
1998 mdd_read_unlock(env, mdd_obj);
2002 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
2004 struct mdd_object *mdd_obj = md2mdd_obj(obj);
2006 if (mdd_object_exists(mdd_obj) == 0) {
2009 CERROR("%s: object "DFID" not found: rc = %d\n",
2010 mdd_obj_dev_name(mdd_obj),
2011 PFID(mdd_object_fid(mdd_obj)), rc);
2014 return dt_object_sync(env, mdd_object_child(mdd_obj),
2018 static int mdd_object_lock(const struct lu_env *env,
2019 struct md_object *obj,
2020 struct lustre_handle *lh,
2021 struct ldlm_enqueue_info *einfo,
2022 ldlm_policy_data_t *policy)
2024 struct mdd_object *mdd_obj = md2mdd_obj(obj);
2025 return dt_object_lock(env, mdd_object_child(mdd_obj), lh,
2029 static int mdd_object_unlock(const struct lu_env *env,
2030 struct md_object *obj,
2031 struct ldlm_enqueue_info *einfo,
2032 ldlm_policy_data_t *policy)
2034 struct mdd_object *mdd_obj = md2mdd_obj(obj);
2035 return dt_object_unlock(env, mdd_object_child(mdd_obj), einfo, policy);
2038 const struct md_object_operations mdd_obj_ops = {
2039 .moo_permission = mdd_permission,
2040 .moo_attr_get = mdd_attr_get,
2041 .moo_attr_set = mdd_attr_set,
2042 .moo_xattr_get = mdd_xattr_get,
2043 .moo_xattr_set = mdd_xattr_set,
2044 .moo_xattr_list = mdd_xattr_list,
2045 .moo_xattr_del = mdd_xattr_del,
2046 .moo_swap_layouts = mdd_swap_layouts,
2047 .moo_open = mdd_open,
2048 .moo_close = mdd_close,
2049 .moo_readpage = mdd_readpage,
2050 .moo_readlink = mdd_readlink,
2051 .moo_changelog = mdd_changelog,
2052 .moo_capa_get = mdd_capa_get,
2053 .moo_object_sync = mdd_object_sync,
2054 .moo_object_lock = mdd_object_lock,
2055 .moo_object_unlock = mdd_object_unlock,