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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ofd/ofd_objects.c
34 * This file contains OSD API methods related to OBD Filter Device (OFD)
37 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
38 * Author: Mikhail Pershin <mike.pershin@intel.com>
41 #define DEBUG_SUBSYSTEM S_FILTER
43 #include <dt_object.h>
44 #include <lustre_lfsck.h>
46 #include "ofd_internal.h"
49 * Get object version from disk and check it.
51 * This function checks object version from disk with
52 * ofd_thread_info::fti_pre_version filled from incoming RPC. This is part of
53 * VBR (Version-Based Recovery) and ensures that object has the same version
54 * upon replay as it has during original modification.
56 * \param[in] info execution thread OFD private data
57 * \param[in] fo OFD object
59 * \retval 0 if version matches
60 * \retval -EOVERFLOW on version mismatch
62 static int ofd_version_get_check(struct ofd_thread_info *info,
63 struct ofd_object *fo)
65 dt_obj_version_t curr_version;
67 if (info->fti_exp == NULL)
70 curr_version = dt_version_get(info->fti_env, ofd_object_child(fo));
71 if ((__s64)curr_version == -EOPNOTSUPP)
73 /* VBR: version is checked always because costs nothing */
74 if (info->fti_pre_version != 0 &&
75 info->fti_pre_version != curr_version) {
76 CDEBUG(D_INODE, "Version mismatch %#llx != %#llx\n",
77 info->fti_pre_version, curr_version);
78 spin_lock(&info->fti_exp->exp_lock);
79 info->fti_exp->exp_vbr_failed = 1;
80 spin_unlock(&info->fti_exp->exp_lock);
83 info->fti_pre_version = curr_version;
88 * Get OFD object by FID.
90 * This function finds OFD slice of compound object with the given FID.
92 * \param[in] env execution environment
93 * \param[in] ofd OFD device
94 * \param[in] fid FID of the object
96 * \retval pointer to the found ofd_object
97 * \retval ERR_PTR(errno) in case of error
99 struct ofd_object *ofd_object_find(const struct lu_env *env,
100 struct ofd_device *ofd,
101 const struct lu_fid *fid)
103 struct ofd_object *fo;
108 o = lu_object_find(env, &ofd->ofd_dt_dev.dd_lu_dev, fid, NULL);
109 if (likely(!IS_ERR(o)))
112 fo = ERR_CAST(o); /* return error */
118 * Get FID of parent MDT object.
120 * This function reads extended attribute XATTR_NAME_FID of OFD object which
121 * contains the MDT parent object FID and saves it in ofd_object::ofo_ff.
123 * The filter_fid::ff_parent::f_ver field currently holds
124 * the OST-object index in the parent MDT-object's layout EA,
125 * not the actual FID::f_ver of the parent. We therefore access
126 * it via the macro f_stripe_idx.
128 * \param[in] env execution environment
129 * \param[in] fo OFD object
131 * \retval 0 if successful
132 * \retval -ENODATA if there is no such xattr
133 * \retval negative value on error
135 int ofd_object_ff_load(const struct lu_env *env, struct ofd_object *fo)
137 struct ofd_thread_info *info = ofd_info(env);
138 struct filter_fid *ff = &fo->ofo_ff;
139 struct lu_buf *buf = &info->fti_buf;
142 if (fid_is_sane(&ff->ff_parent))
146 buf->lb_len = sizeof(*ff);
147 rc = dt_xattr_get(env, ofd_object_child(fo), buf, XATTR_NAME_FID);
151 if (unlikely(rc < sizeof(struct lu_fid))) {
152 fid_zero(&ff->ff_parent);
156 filter_fid_le_to_cpu(ff, ff, rc);
161 struct ofd_precreate_cb {
162 struct dt_txn_commit_cb opc_cb;
163 struct ofd_seq *opc_oseq;
167 static void ofd_cb_precreate(struct lu_env *env, struct thandle *th,
168 struct dt_txn_commit_cb *cb, int err)
170 struct ofd_precreate_cb *opc;
171 struct ofd_seq *oseq;
173 opc = container_of(cb, struct ofd_precreate_cb, opc_cb);
174 oseq = opc->opc_oseq;
176 CDEBUG(D_OTHER, "Sub %d from %d for "DFID", th_sync %d\n",
177 opc->opc_objects, atomic_read(&oseq->os_precreate_in_progress),
178 PFID(&oseq->os_oi.oi_fid), th->th_sync);
179 atomic_sub(opc->opc_objects, &oseq->os_precreate_in_progress);
180 ofd_seq_put(env, opc->opc_oseq);
184 static int ofd_precreate_cb_add(const struct lu_env *env, struct thandle *th,
185 struct ofd_seq *oseq, int objects)
187 struct ofd_precreate_cb *opc;
188 struct dt_txn_commit_cb *dcb;
195 precreate = atomic_read(&oseq->os_precreate_in_progress);
196 atomic_inc(&oseq->os_refc);
197 opc->opc_oseq = oseq;
198 opc->opc_objects = objects;
199 CDEBUG(D_OTHER, "Add %d to %d for "DFID", th_sync %d\n",
200 opc->opc_objects, precreate,
201 PFID(&oseq->os_oi.oi_fid), th->th_sync);
203 if ((precreate + objects) >= (5 * OST_MAX_PRECREATE))
207 dcb->dcb_func = ofd_cb_precreate;
208 INIT_LIST_HEAD(&dcb->dcb_linkage);
209 strlcpy(dcb->dcb_name, "ofd_cb_precreate", sizeof(dcb->dcb_name));
211 rc = dt_trans_cb_add(th, dcb);
213 ofd_seq_put(env, oseq);
218 atomic_add(objects, &oseq->os_precreate_in_progress);
224 * Precreate the given number \a nr of objects in the given sequence \a oseq.
226 * This function precreates new OST objects in the given sequence.
227 * The precreation starts from \a id and creates \a nr objects sequentially.
230 * This function may create fewer objects than requested.
232 * We mark object SUID+SGID to flag it for accepting UID+GID from client on
233 * first write. Currently the permission bits on the OST are never used,
236 * Initialize a/c/m time so any client timestamp will always be newer and
237 * update the inode. The ctime = 0 case is also handled specially in
238 * osd_inode_setattr(). See LU-221, LU-1042 for details.
240 * \param[in] env execution environment
241 * \param[in] ofd OFD device
242 * \param[in] id object ID to start precreation from
243 * \param[in] oseq object sequence
244 * \param[in] nr number of objects to precreate
245 * \param[in] sync synchronous precreation flag
247 * \retval 0 if successful
248 * \retval negative value on error
250 int ofd_precreate_objects(const struct lu_env *env, struct ofd_device *ofd,
251 u64 id, struct ofd_seq *oseq, int nr, int sync)
253 struct ofd_thread_info *info = ofd_info(env);
254 struct ofd_object *fo = NULL;
255 struct dt_object *next;
257 struct ofd_object **batch;
258 struct lu_fid *fid = &info->fti_fid;
268 /* Don't create objects beyond the valid range for this SEQ */
269 if (unlikely(fid_seq_is_mdt0(ostid_seq(&oseq->os_oi)) &&
270 (id + nr) > IDIF_MAX_OID)) {
271 CERROR("%s:"DOSTID" hit the IDIF_MAX_OID (1<<48)!\n",
272 ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
273 RETURN(rc = -ENOSPC);
274 } else if (unlikely(!fid_seq_is_mdt0(ostid_seq(&oseq->os_oi)) &&
275 (id + nr) > OBIF_MAX_OID)) {
276 CERROR("%s:"DOSTID" hit the OBIF_MAX_OID (1<<32)!\n",
277 ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
278 RETURN(rc = -ENOSPC);
281 OBD_ALLOC_PTR_ARRAY(batch, nr_saved);
285 info->fti_attr.la_valid = LA_TYPE | LA_MODE;
286 info->fti_attr.la_mode = S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666;
287 info->fti_dof.dof_type = dt_mode_to_dft(S_IFREG);
289 info->fti_attr.la_valid |= LA_ATIME | LA_MTIME | LA_CTIME;
290 info->fti_attr.la_atime = 0;
291 info->fti_attr.la_mtime = 0;
292 info->fti_attr.la_ctime = 0;
296 /* prepare objects */
297 *fid = *lu_object_fid(&oseq->os_lastid_obj->do_lu);
298 for (i = 0; i < nr; i++) {
299 rc = fid_set_id(fid, id + i);
308 fo = ofd_object_find(env, ofd, fid);
311 GOTO(out, rc = PTR_ERR(fo));
319 info->fti_buf.lb_buf = &tmp;
320 info->fti_buf.lb_len = sizeof(tmp);
323 th = ofd_trans_create(env, ofd);
325 GOTO(out, rc = PTR_ERR(th));
329 rc = dt_declare_record_write(env, oseq->os_lastid_obj, &info->fti_buf,
332 GOTO(trans_stop, rc);
334 for (i = 0; i < nr; i++) {
338 if (unlikely(ofd_object_exists(fo))) {
339 /* object may exist being re-created by write replay */
340 CDEBUG(D_INODE, "object %#llx/%#llx exists: "
341 DFID"\n", ostid_seq(&oseq->os_oi), id,
342 PFID(lu_object_fid(&fo->ofo_obj.do_lu)));
346 next = ofd_object_child(fo);
347 LASSERT(next != NULL);
349 rc = dt_declare_create(env, next, &info->fti_attr, NULL,
353 GOTO(trans_stop, rc);
360 rc = dt_trans_start_local(env, ofd->ofd_osd, th);
362 GOTO(trans_stop, rc);
364 CDEBUG(D_OTHER, "%s: create new object "DFID" nr %d\n",
365 ofd_name(ofd), PFID(fid), nr);
367 /* When the LFSCK scanning the whole device to verify the LAST_ID file
368 * consistency, it will load the last_id into RAM firstly, and compare
369 * the last_id with each OST-object's ID. If the later one is larger,
370 * then it will regard the LAST_ID file crashed. But during the LFSCK
371 * scanning, the OFD may continue to create new OST-objects. Those new
372 * created OST-objects will have larger IDs than the LFSCK known ones.
373 * So from the LFSCK view, it needs to re-load the last_id from disk
374 * file, and if the latest last_id is still smaller than the object's
375 * ID, then the LAST_ID file is real crashed.
377 * To make above mechanism to work, before OFD pre-create OST-objects,
378 * it needs to update the LAST_ID file firstly, otherwise, the LFSCK
379 * may cannot get latest last_id although new OST-object created. */
380 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_SKIP_LASTID)) {
381 tmp = cpu_to_le64(id + nr - 1);
382 dt_write_lock(env, oseq->os_lastid_obj, DT_LASTID);
383 rc = dt_record_write(env, oseq->os_lastid_obj,
384 &info->fti_buf, &info->fti_off, th);
385 dt_write_unlock(env, oseq->os_lastid_obj);
387 GOTO(trans_stop, rc);
390 for (i = 0; i < nr; i++) {
394 ofd_write_lock(env, fo);
396 /* Only the new created objects need to be recorded. */
397 if (ofd->ofd_osd->dd_record_fid_accessed) {
398 struct lfsck_req_local *lrl = &ofd_info(env)->fti_lrl;
400 lfsck_pack_rfa(lrl, lu_object_fid(&fo->ofo_obj.do_lu),
401 LEL_FID_ACCESSED, LFSCK_TYPE_LAYOUT);
402 lfsck_in_notify_local(env, ofd->ofd_osd, lrl, NULL);
405 if (likely(!ofd_object_exists(fo) &&
406 !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING))) {
407 next = ofd_object_child(fo);
408 LASSERT(next != NULL);
410 rc = dt_create(env, next, &info->fti_attr, NULL,
412 ofd_write_unlock(env, fo);
415 GOTO(trans_stop, rc);
420 LASSERT(ofd_object_exists(fo));
422 ofd_write_unlock(env, fo);
425 ofd_seq_last_oid_set(oseq, id + i);
429 /* NOT all the wanted objects have been created,
430 * set the LAST_ID as the real created. */
431 if (unlikely(objects < nr)) {
435 tmp = cpu_to_le64(ofd_seq_last_oid(oseq));
436 dt_write_lock(env, oseq->os_lastid_obj, DT_LASTID);
437 rc1 = dt_record_write(env, oseq->os_lastid_obj,
438 &info->fti_buf, &info->fti_off, th);
439 dt_write_unlock(env, oseq->os_lastid_obj);
441 CERROR("%s: fail to reset the LAST_ID for seq (%#llx"
442 ") from %llu to %llu\n", ofd_name(ofd),
443 ostid_seq(&oseq->os_oi), id + nr - 1,
444 ofd_seq_last_oid(oseq));
448 ofd_precreate_cb_add(env, th, oseq, objects);
450 rc2 = ofd_trans_stop(env, ofd, th, rc);
452 CERROR("%s: failed to stop transaction: rc = %d\n",
457 for (i = 0; i < nr_saved; i++) {
461 ofd_object_put(env, fo);
463 OBD_FREE_PTR_ARRAY(batch, nr_saved);
465 CDEBUG((objects == 0 && rc == 0) ? D_ERROR : D_OTHER,
466 "created %d/%d objects: %d\n", objects, nr_saved, rc);
468 LASSERT(ergo(objects == 0, rc < 0));
469 RETURN(objects > 0 ? objects : rc);
473 * Fix the OFD object ownership.
475 * If the object still has SUID+SGID bits set, meaning that it was precreated
476 * by the MDT before it was assigned to any file, (see ofd_precreate_objects())
477 * then we will accept the UID/GID/PROJID if sent by the client for initializing
478 * the ownership of this object. We only allow this to happen once (so clear
479 * these bits) and later only allow setattr.
481 * \param[in] env execution environment
482 * \param[in] fo OFD object
483 * \param[in] la object attributes
484 * \param[in] is_setattr was this function called from setattr or not
486 * \retval 0 if successful
487 * \retval negative value on error
489 int ofd_attr_handle_id(const struct lu_env *env, struct ofd_object *fo,
490 struct lu_attr *la, int is_setattr)
492 struct ofd_thread_info *info = ofd_info(env);
493 struct lu_attr *ln = &info->fti_attr2;
499 if (!(la->la_valid & LA_UID) && !(la->la_valid & LA_GID) &&
500 !(la->la_valid & LA_PROJID))
503 rc = dt_attr_get(env, ofd_object_child(fo), ln);
507 LASSERT(ln->la_valid & LA_MODE);
510 * Only allow setattr to change UID/GID/PROJID, if
511 * SUID+SGID is not set which means this is not
512 * initialization of this objects.
515 if (!(ln->la_mode & S_ISUID))
516 la->la_valid &= ~LA_UID;
517 if (!(ln->la_mode & S_ISGID))
518 la->la_valid &= ~LA_GID;
519 if (!(ln->la_mode & S_ISVTX))
520 la->la_valid &= ~LA_PROJID;
523 /* Initialize ownership of this object, clear SUID+SGID bits*/
524 if ((la->la_valid & LA_UID) && (ln->la_mode & S_ISUID))
526 if ((la->la_valid & LA_GID) && (ln->la_mode & S_ISGID))
528 if ((la->la_valid & LA_PROJID) && (ln->la_mode & S_ISVTX))
531 if (!(la->la_valid & LA_MODE) || !is_setattr) {
532 la->la_mode = ln->la_mode;
533 la->la_valid |= LA_MODE;
535 la->la_mode &= ~mask;
542 * Check if it needs to update filter_fid by the value of @oa.
545 * \param[in] fo ofd object
546 * \param[in] oa obdo from client or MDT
547 * \param[out] ff if filter_fid needs updating, this field is used to
548 * return the new buffer
550 * \retval < 0 error occurred
551 * \retval 0 doesn't need to update filter_fid
552 * \retval FL_XATTR_{CREATE,REPLACE} flag for xattr update
554 int ofd_object_ff_update(const struct lu_env *env, struct ofd_object *fo,
555 const struct obdo *oa, struct filter_fid *ff)
561 (OBD_MD_FLFID | OBD_MD_FLOSTLAYOUT | OBD_MD_LAYOUT_VERSION)))
564 rc = ofd_object_ff_load(env, fo);
565 if (rc < 0 && rc != -ENODATA)
568 LASSERT(ff != &fo->ofo_ff);
569 if (rc == -ENODATA) {
570 rc = LU_XATTR_CREATE;
571 memset(ff, 0, sizeof(*ff));
573 rc = LU_XATTR_REPLACE;
574 memcpy(ff, &fo->ofo_ff, sizeof(*ff));
577 if (oa->o_valid & OBD_MD_FLFID) {
578 /* packing fid and converting it to LE for storing into EA.
579 * Here ->o_stripe_idx should be filled by LOV and rest of
580 * fields - by client. */
581 ff->ff_parent.f_seq = oa->o_parent_seq;
582 ff->ff_parent.f_oid = oa->o_parent_oid;
583 /* XXX: we are ignoring o_parent_ver here, since this should
584 * be the same for all objects in this fileset. */
585 ff->ff_parent.f_ver = oa->o_stripe_idx;
587 if (oa->o_valid & OBD_MD_FLOSTLAYOUT)
588 ff->ff_layout = oa->o_layout;
590 if (oa->o_valid & OBD_MD_LAYOUT_VERSION) {
591 CDEBUG(D_INODE, DFID": OST("DFID") layout version %u -> %u\n",
592 PFID(&fo->ofo_ff.ff_parent),
593 PFID(lu_object_fid(&fo->ofo_obj.do_lu)),
594 ff->ff_layout_version, oa->o_layout_version);
596 /* only the MDS has the authority to update layout version */
597 if (!(exp_connect_flags(ofd_info(env)->fti_exp) &
599 CERROR(DFID": update layout version from client\n",
600 PFID(&fo->ofo_ff.ff_parent));
605 if (ff->ff_layout_version & LU_LAYOUT_RESYNC) {
606 /* this opens a new era of writing */
607 ff->ff_layout_version = 0;
611 /* it's not allowed to change it to a smaller value */
612 if (oa->o_layout_version < ff->ff_layout_version)
615 if (ff->ff_layout_version == 0 ||
616 oa->o_layout_version & LU_LAYOUT_RESYNC) {
617 /* if LU_LAYOUT_RESYNC is set, it closes the era of
618 * writing. Only mirror I/O can write this object. */
619 ff->ff_layout_version = oa->o_layout_version;
621 } else if (oa->o_layout_version > ff->ff_layout_version) {
622 ff->ff_range = max_t(__u32, ff->ff_range,
623 oa->o_layout_version -
624 ff->ff_layout_version);
628 if (memcmp(ff, &fo->ofo_ff, sizeof(*ff)))
629 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
637 * Set OFD object attributes.
639 * This function sets OFD object attributes taken from incoming request.
640 * It sets not only regular attributes but also XATTR_NAME_FID extended
641 * attribute if needed. The "fid" xattr allows the object's MDT parent inode
642 * to be found and verified by LFSCK and other tools in case of inconsistency.
644 * \param[in] env execution environment
645 * \param[in] fo OFD object
646 * \param[in] la object attributes
647 * \param[in] oa obdo carries fid, ost_layout, layout version
649 * \retval 0 if successful
650 * \retval negative value on error
652 int ofd_attr_set(const struct lu_env *env, struct ofd_object *fo,
653 struct lu_attr *la, struct obdo *oa)
655 struct ofd_thread_info *info = ofd_info(env);
656 struct ofd_device *ofd = ofd_obj2dev(fo);
657 struct filter_fid *ff = &info->fti_mds_fid;
663 if (!ofd_object_exists(fo))
664 GOTO(out, rc = -ENOENT);
666 /* VBR: version recovery check */
667 rc = ofd_version_get_check(info, fo);
671 rc = ofd_attr_handle_id(env, fo, la, 1 /* is_setattr */);
675 th = ofd_trans_create(env, ofd);
677 GOTO(out, rc = PTR_ERR(th));
679 rc = dt_declare_attr_set(env, ofd_object_child(fo), la, th);
683 info->fti_buf.lb_buf = ff;
684 info->fti_buf.lb_len = sizeof(*ff);
685 rc = dt_declare_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
686 XATTR_NAME_FID, 0, th);
690 rc = ofd_trans_start(env, ofd, la->la_valid & LA_SIZE ? fo : NULL, th);
694 ofd_write_lock(env, fo);
695 if (!ofd_object_exists(fo))
696 GOTO(unlock, rc = -ENOENT);
698 /* serialize vs ofd_commitrw_write() */
699 if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
700 tgt_fmd_update(info->fti_exp, &fo->ofo_header.loh_fid,
703 rc = dt_attr_set(env, ofd_object_child(fo), la, th);
707 fl = ofd_object_ff_update(env, fo, oa, ff);
709 GOTO(unlock, rc = fl);
712 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_UNMATCHED_PAIR1))
713 ff->ff_parent.f_oid = cpu_to_le32(1UL << 31);
714 else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_UNMATCHED_PAIR2))
715 le32_add_cpu(&ff->ff_parent.f_oid, -1);
716 else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NOPFID))
719 info->fti_buf.lb_buf = ff;
720 info->fti_buf.lb_len = sizeof(*ff);
721 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
722 XATTR_NAME_FID, fl, th);
724 filter_fid_le_to_cpu(&fo->ofo_ff, ff, sizeof(*ff));
730 ofd_write_unlock(env, fo);
732 rc2 = ofd_trans_stop(env, ofd, th, rc);
734 CERROR("%s: failed to stop transaction: rc = %d\n",
743 * Fallocate(Preallocate) space for OFD object.
745 * This function allocates space for the object from the \a start
746 * offset to the \a end offset.
748 * \param[in] env execution environment
749 * \param[in] fo OFD object
750 * \param[in] start start offset to allocate from
751 * \param[in] end end of allocate
752 * \param[in] mode fallocate mode
753 * \param[in] la object attributes
754 * \param[in] ff filter_fid structure
756 * \retval 0 if successful
757 * \retval negative value on error
759 int ofd_object_fallocate(const struct lu_env *env, struct ofd_object *fo,
760 __u64 start, __u64 end, int mode, struct lu_attr *la,
763 struct ofd_thread_info *info = ofd_info(env);
764 struct ofd_device *ofd = ofd_obj2dev(fo);
765 struct dt_object *dob = ofd_object_child(fo);
767 struct filter_fid *ff = &info->fti_mds_fid;
768 bool ff_needed = false;
773 if (!ofd_object_exists(fo))
776 /* VBR: version recovery check */
777 rc = ofd_version_get_check(info, fo);
782 rc = ofd_object_ff_load(env, fo);
789 th = ofd_trans_create(env, ofd);
793 rc = dt_declare_attr_set(env, dob, la, th);
797 rc = dt_declare_fallocate(env, dob, start, end, mode, th);
801 rc = ofd_trans_start(env, ofd, fo, th);
805 ofd_write_lock(env, fo);
806 if (!ofd_object_exists(fo))
807 GOTO(unlock, rc = -ENOENT);
809 rc = dt_falloc(env, dob, start, end, mode, th);
813 rc = dt_attr_set(env, dob, la, th);
818 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
819 XATTR_NAME_FID, 0, th);
821 filter_fid_le_to_cpu(&fo->ofo_ff, ff, sizeof(*ff));
824 ofd_write_unlock(env, fo);
826 ofd_trans_stop(env, ofd, th, rc);
831 * Truncate/punch OFD object.
833 * This function frees all of the allocated object's space from the \a start
834 * offset to the \a end offset. For truncate() operations the \a end offset
835 * is OBD_OBJECT_EOF. The functionality to punch holes in an object via
836 * fallocate(FALLOC_FL_PUNCH_HOLE) is not yet implemented (see LU-3606).
838 * \param[in] env execution environment
839 * \param[in] fo OFD object
840 * \param[in] start start offset to punch from
841 * \param[in] end end of punch
842 * \param[in] la object attributes
843 * \param[in] oa obdo struct from incoming request
845 * \retval 0 if successful
846 * \retval negative value on error
848 int ofd_object_punch(const struct lu_env *env, struct ofd_object *fo,
849 __u64 start, __u64 end, struct lu_attr *la,
852 struct ofd_thread_info *info = ofd_info(env);
853 struct ofd_device *ofd = ofd_obj2dev(fo);
854 struct dt_object *dob = ofd_object_child(fo);
855 struct filter_fid *ff = &info->fti_mds_fid;
861 /* we support truncate, not punch yet */
862 LASSERT(end == OBD_OBJECT_EOF);
864 if (!ofd_object_exists(fo))
865 GOTO(out, rc = -ENOENT);
867 if (ofd->ofd_lfsck_verify_pfid && oa->o_valid & OBD_MD_FLFID) {
868 rc = ofd_verify_ff(env, fo, oa);
873 /* VBR: version recovery check */
874 rc = ofd_version_get_check(info, fo);
878 rc = ofd_attr_handle_id(env, fo, la, 0 /* !is_setattr */);
882 th = ofd_trans_create(env, ofd);
884 GOTO(out, rc = PTR_ERR(th));
886 rc = dt_declare_attr_set(env, dob, la, th);
890 rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
894 info->fti_buf.lb_buf = ff;
895 info->fti_buf.lb_len = sizeof(*ff);
896 rc = dt_declare_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
897 XATTR_NAME_FID, 0, th);
901 rc = ofd_trans_start(env, ofd, fo, th);
905 ofd_write_lock(env, fo);
907 if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
908 tgt_fmd_update(info->fti_exp, &fo->ofo_header.loh_fid,
911 if (!ofd_object_exists(fo))
912 GOTO(unlock, rc = -ENOENT);
914 /* need to verify layout version */
915 if (oa->o_valid & OBD_MD_LAYOUT_VERSION) {
916 rc = ofd_verify_layout_version(env, fo, oa);
920 oa->o_valid &= ~OBD_MD_LAYOUT_VERSION;
923 if (oa->o_valid & OBD_MD_FLFLAGS && oa->o_flags & LUSTRE_ENCRYPT_FL) {
924 /* punch must be aware we are dealing with an encrypted file */
925 struct lu_attr la = {
926 .la_valid = LA_FLAGS,
927 .la_flags = LUSTRE_ENCRYPT_FL,
930 rc = dt_attr_set(env, dob, &la, th);
934 rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th);
938 fl = ofd_object_ff_update(env, fo, oa, ff);
940 GOTO(unlock, rc = fl);
942 rc = dt_attr_set(env, dob, la, th);
947 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_UNMATCHED_PAIR1))
948 ff->ff_parent.f_oid = cpu_to_le32(1UL << 31);
949 else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_UNMATCHED_PAIR2))
950 le32_add_cpu(&ff->ff_parent.f_oid, -1);
951 else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NOPFID))
954 info->fti_buf.lb_buf = ff;
955 info->fti_buf.lb_len = sizeof(*ff);
956 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
957 XATTR_NAME_FID, fl, th);
959 filter_fid_le_to_cpu(&fo->ofo_ff, ff, sizeof(*ff));
965 ofd_write_unlock(env, fo);
967 rc2 = ofd_trans_stop(env, ofd, th, rc);
969 CERROR("%s: failed to stop transaction: rc = %d\n",
978 * Destroy OFD object.
980 * This function destroys OFD object. If object wasn't used at all (orphan)
981 * then local transaction is used, which means the transaction data is not
982 * returned back in reply.
984 * \param[in] env execution environment
985 * \param[in] fo OFD object
986 * \param[in] orphan flag to indicate that object is orphaned
988 * \retval 0 if successful
989 * \retval negative value on error
991 int ofd_destroy(const struct lu_env *env, struct ofd_object *fo,
994 struct ofd_device *ofd = ofd_obj2dev(fo);
1001 if (!ofd_object_exists(fo))
1002 GOTO(out, rc = -ENOENT);
1004 th = ofd_trans_create(env, ofd);
1006 GOTO(out, rc = PTR_ERR(th));
1008 rc = dt_declare_ref_del(env, ofd_object_child(fo), th);
1012 rc = dt_declare_destroy(env, ofd_object_child(fo), th);
1017 rc = dt_trans_start_local(env, ofd->ofd_osd, th);
1019 rc = ofd_trans_start(env, ofd, NULL, th);
1023 ofd_write_lock(env, fo);
1024 if (!ofd_object_exists(fo))
1025 GOTO(unlock, rc = -ENOENT);
1027 tgt_fmd_drop(ofd_info(env)->fti_exp, &fo->ofo_header.loh_fid);
1029 dt_ref_del(env, ofd_object_child(fo), th);
1030 dt_destroy(env, ofd_object_child(fo), th);
1032 ofd_write_unlock(env, fo);
1034 rc2 = ofd_trans_stop(env, ofd, th, rc);
1036 CERROR("%s failed to stop transaction: %d\n",
1037 ofd_name(ofd), rc2);
1045 * Get OFD object attributes.
1047 * This function gets OFD object regular attributes. It is used to serve
1048 * incoming request as well as for local OFD purposes.
1050 * \param[in] env execution environment
1051 * \param[in] fo OFD object
1052 * \param[in] la object attributes
1054 * \retval 0 if successful
1055 * \retval negative value on error
1057 int ofd_attr_get(const struct lu_env *env, struct ofd_object *fo,
1064 if (ofd_object_exists(fo)) {
1065 rc = dt_attr_get(env, ofd_object_child(fo), la);