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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 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/ofd/ofd_objects.c
38 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39 * Author: Mikhail Pershin <tappro@whamcloud.com>
42 #define DEBUG_SUBSYSTEM S_FILTER
44 #include <dt_object.h>
45 #include <lustre/lustre_idl.h>
46 #include <lustre_lfsck.h>
48 #include "ofd_internal.h"
50 int ofd_version_get_check(struct ofd_thread_info *info,
51 struct ofd_object *fo)
53 dt_obj_version_t curr_version;
55 LASSERT(ofd_object_exists(fo));
60 curr_version = dt_version_get(info->fti_env, ofd_object_child(fo));
61 if ((__s64)curr_version == -EOPNOTSUPP)
63 /* VBR: version is checked always because costs nothing */
64 if (info->fti_pre_version != 0 &&
65 info->fti_pre_version != curr_version) {
66 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
67 info->fti_pre_version, curr_version);
68 spin_lock(&info->fti_exp->exp_lock);
69 info->fti_exp->exp_vbr_failed = 1;
70 spin_unlock(&info->fti_exp->exp_lock);
73 info->fti_pre_version = curr_version;
77 struct ofd_object *ofd_object_find(const struct lu_env *env,
78 struct ofd_device *ofd,
79 const struct lu_fid *fid)
81 struct ofd_object *fo;
86 o = lu_object_find(env, &ofd->ofd_dt_dev.dd_lu_dev, fid, NULL);
87 if (likely(!IS_ERR(o)))
90 fo = ERR_CAST(o); /* return error */
95 int ofd_object_ff_load(const struct lu_env *env, struct ofd_object *fo)
97 struct ofd_thread_info *info = ofd_info(env);
98 struct filter_fid_old *ff = &info->fti_mds_fid_old;
99 struct lu_buf *buf = &info->fti_buf;
100 struct lu_fid *pfid = &fo->ofo_pfid;
103 if (fid_is_sane(pfid))
107 buf->lb_len = sizeof(*ff);
108 rc = dt_xattr_get(env, ofd_object_child(fo), buf, XATTR_NAME_FID,
113 if (rc < sizeof(struct lu_fid)) {
119 pfid->f_seq = le64_to_cpu(ff->ff_parent.f_seq);
120 pfid->f_oid = le32_to_cpu(ff->ff_parent.f_oid);
121 /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
122 * MDT-object's FID::f_ver, instead it is the OST-object index in its
123 * parent MDT-object's layout EA. */
124 pfid->f_stripe_idx = le32_to_cpu(ff->ff_parent.f_stripe_idx);
129 void ofd_object_put(const struct lu_env *env, struct ofd_object *fo)
131 lu_object_put(env, &fo->ofo_obj.do_lu);
134 int ofd_precreate_objects(const struct lu_env *env, struct ofd_device *ofd,
135 obd_id id, struct ofd_seq *oseq, int nr, int sync)
137 struct ofd_thread_info *info = ofd_info(env);
138 struct ofd_object *fo = NULL;
139 struct dt_object *next;
141 struct ofd_object **batch;
142 struct lu_fid *fid = &info->fti_fid;
151 /* Don't create objects beyond the valid range for this SEQ */
152 if (unlikely(fid_seq_is_mdt0(ostid_seq(&oseq->os_oi)) &&
153 (id + nr) >= IDIF_MAX_OID)) {
154 CERROR("%s:"DOSTID" hit the IDIF_MAX_OID (1<<48)!\n",
155 ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
156 RETURN(rc = -ENOSPC);
157 } else if (unlikely(!fid_seq_is_mdt0(ostid_seq(&oseq->os_oi)) &&
158 (id + nr) >= OBIF_MAX_OID)) {
159 CERROR("%s:"DOSTID" hit the OBIF_MAX_OID (1<<32)!\n",
160 ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
161 RETURN(rc = -ENOSPC);
164 OBD_ALLOC(batch, nr_saved * sizeof(struct ofd_object *));
168 info->fti_attr.la_valid = LA_TYPE | LA_MODE;
170 * We mark object SUID+SGID to flag it for accepting UID+GID from
171 * client on first write. Currently the permission bits on the OST are
172 * never used, so this is OK.
174 info->fti_attr.la_mode = S_IFREG | S_ISUID | S_ISGID | 0666;
175 info->fti_dof.dof_type = dt_mode_to_dft(S_IFREG);
177 /* Initialize a/c/m time so any client timestamp will always
178 * be newer and update the inode. ctime = 0 is also handled
179 * specially in osd_inode_setattr(). See LU-221, LU-1042 */
180 info->fti_attr.la_valid |= LA_ATIME | LA_MTIME | LA_CTIME;
181 info->fti_attr.la_atime = 0;
182 info->fti_attr.la_mtime = 0;
183 info->fti_attr.la_ctime = 0;
187 /* prepare objects */
188 *fid = *lu_object_fid(&oseq->os_lastid_obj->do_lu);
189 for (i = 0; i < nr; i++) {
190 rc = fid_set_id(fid, id + i);
199 fo = ofd_object_find(env, ofd, fid);
202 GOTO(out, rc = PTR_ERR(fo));
208 ofd_write_lock(env, fo);
211 info->fti_buf.lb_buf = &tmp;
212 info->fti_buf.lb_len = sizeof(tmp);
215 th = ofd_trans_create(env, ofd);
217 GOTO(out, rc = PTR_ERR(th));
221 rc = dt_declare_record_write(env, oseq->os_lastid_obj, &info->fti_buf,
224 GOTO(trans_stop, rc);
226 for (i = 0; i < nr; i++) {
230 if (unlikely(ofd_object_exists(fo))) {
231 /* object may exist being re-created by write replay */
232 CDEBUG(D_INODE, "object "LPX64"/"LPX64" exists: "
233 DFID"\n", ostid_seq(&oseq->os_oi), id,
234 PFID(lu_object_fid(&fo->ofo_obj.do_lu)));
238 next = ofd_object_child(fo);
239 LASSERT(next != NULL);
241 rc = dt_declare_create(env, next, &info->fti_attr, NULL,
249 rc = dt_trans_start_local(env, ofd->ofd_osd, th);
251 GOTO(trans_stop, rc);
253 CDEBUG(D_OTHER, "%s: create new object "DFID" nr %d\n",
254 ofd_name(ofd), PFID(fid), nr);
258 /* When the LFSCK scanning the whole device to verify the LAST_ID file
259 * consistency, it will load the last_id into RAM firstly, and compare
260 * the last_id with each OST-object's ID. If the later one is larger,
261 * then it will regard the LAST_ID file crashed. But during the LFSCK
262 * scanning, the OFD may continue to create new OST-objects. Those new
263 * created OST-objects will have larger IDs than the LFSCK known ones.
264 * So from the LFSCK view, it needs to re-load the last_id from disk
265 * file, and if the latest last_id is still smaller than the object's
266 * ID, then the LAST_ID file is real crashed.
268 * To make above mechanism to work, before OFD pre-create OST-objects,
269 * it needs to update the LAST_ID file firstly, otherwise, the LFSCK
270 * may cannot get latest last_id although new OST-object created. */
271 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_SKIP_LASTID)) {
272 tmp = cpu_to_le64(id + nr - 1);
273 dt_write_lock(env, oseq->os_lastid_obj, 0);
274 rc = dt_record_write(env, oseq->os_lastid_obj,
275 &info->fti_buf, &info->fti_off, th);
276 dt_write_unlock(env, oseq->os_lastid_obj);
278 GOTO(trans_stop, rc);
281 for (i = 0; i < nr; i++) {
285 /* Only the new created objects need to be recorded. */
286 if (ofd->ofd_osd->dd_record_fid_accessed) {
287 lfsck_pack_rfa(&ofd_info(env)->fti_lr,
288 lu_object_fid(&fo->ofo_obj.do_lu));
289 lfsck_in_notify(env, ofd->ofd_osd,
290 &ofd_info(env)->fti_lr);
293 if (likely(!ofd_object_exists(fo) &&
294 !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING))) {
295 next = ofd_object_child(fo);
296 LASSERT(next != NULL);
298 rc = dt_create(env, next, &info->fti_attr, NULL,
302 LASSERT(ofd_object_exists(fo));
304 ofd_seq_last_oid_set(oseq, id + i);
308 /* NOT all the wanted objects have been created,
309 * set the LAST_ID as the real created. */
310 if (unlikely(objects < nr)) {
314 tmp = cpu_to_le64(ofd_seq_last_oid(oseq));
315 dt_write_lock(env, oseq->os_lastid_obj, 0);
316 rc1 = dt_record_write(env, oseq->os_lastid_obj,
317 &info->fti_buf, &info->fti_off, th);
318 dt_write_unlock(env, oseq->os_lastid_obj);
320 CERROR("%s: fail to reset the LAST_ID for seq ("LPX64
321 ") from "LPU64" to "LPU64"\n", ofd_name(ofd),
322 ostid_seq(&oseq->os_oi), id + nr - 1,
323 ofd_seq_last_oid(oseq));
327 ofd_trans_stop(env, ofd, th, rc);
329 for (i = 0; i < nr_saved; i++) {
332 ofd_write_unlock(env, fo);
333 ofd_object_put(env, fo);
336 OBD_FREE(batch, nr_saved * sizeof(struct ofd_object *));
338 CDEBUG((objects == 0 && rc == 0) ? D_ERROR : D_OTHER,
339 "created %d/%d objects: %d\n", objects, nr_saved, rc);
341 LASSERT(ergo(objects == 0, rc < 0));
342 RETURN(objects > 0 ? objects : rc);
346 * If the object still has SUID+SGID bits set (see ofd_precreate_object()) then
347 * we will accept the UID+GID if sent by the client for initializing the
348 * ownership of this object. We only allow this to happen once (so clear these
349 * bits) and later only allow setattr.
351 int ofd_attr_handle_ugid(const struct lu_env *env, struct ofd_object *fo,
352 struct lu_attr *la, int is_setattr)
354 struct ofd_thread_info *info = ofd_info(env);
355 struct lu_attr *ln = &info->fti_attr2;
361 if (!(la->la_valid & LA_UID) && !(la->la_valid & LA_GID))
364 rc = dt_attr_get(env, ofd_object_child(fo), ln, BYPASS_CAPA);
368 LASSERT(ln->la_valid & LA_MODE);
371 if (!(ln->la_mode & S_ISUID))
372 la->la_valid &= ~LA_UID;
373 if (!(ln->la_mode & S_ISGID))
374 la->la_valid &= ~LA_GID;
377 if ((la->la_valid & LA_UID) && (ln->la_mode & S_ISUID))
379 if ((la->la_valid & LA_GID) && (ln->la_mode & S_ISGID))
382 if (!(la->la_valid & LA_MODE) || !is_setattr) {
383 la->la_mode = ln->la_mode;
384 la->la_valid |= LA_MODE;
386 la->la_mode &= ~mask;
392 int ofd_attr_set(const struct lu_env *env, struct ofd_object *fo,
393 struct lu_attr *la, struct filter_fid *ff)
395 struct ofd_thread_info *info = ofd_info(env);
396 struct ofd_device *ofd = ofd_obj2dev(fo);
398 struct ofd_mod_data *fmd;
403 ofd_write_lock(env, fo);
404 if (!ofd_object_exists(fo))
405 GOTO(unlock, rc = -ENOENT);
407 if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME)) {
408 fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
409 if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
410 fmd->fmd_mactime_xid = info->fti_xid;
411 ofd_fmd_put(info->fti_exp, fmd);
414 /* VBR: version recovery check */
415 rc = ofd_version_get_check(info, fo);
419 rc = ofd_attr_handle_ugid(env, fo, la, 1 /* is_setattr */);
424 rc = ofd_object_ff_load(env, fo);
431 th = ofd_trans_create(env, ofd);
433 GOTO(unlock, rc = PTR_ERR(th));
435 rc = dt_declare_attr_set(env, ofd_object_child(fo), la, th);
440 info->fti_buf.lb_buf = ff;
441 info->fti_buf.lb_len = sizeof(*ff);
442 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
443 &info->fti_buf, XATTR_NAME_FID, 0,
449 rc = ofd_trans_start(env, ofd, la->la_valid & LA_SIZE ? fo : NULL, th);
453 rc = dt_attr_set(env, ofd_object_child(fo), la, th,
454 ofd_object_capa(env, fo));
459 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
460 XATTR_NAME_FID, 0, th, BYPASS_CAPA);
462 fo->ofo_pfid.f_seq = le64_to_cpu(ff->ff_parent.f_seq);
463 fo->ofo_pfid.f_oid = le32_to_cpu(ff->ff_parent.f_oid);
464 /* Currently, the filter_fid::ff_parent::f_ver is not
465 * the real parent MDT-object's FID::f_ver, instead it
466 * is the OST-object index in its parent MDT-object's
468 fo->ofo_pfid.f_stripe_idx =
469 le32_to_cpu(ff->ff_parent.f_stripe_idx);
476 ofd_trans_stop(env, ofd, th, rc);
478 ofd_write_unlock(env, fo);
483 int ofd_object_punch(const struct lu_env *env, struct ofd_object *fo,
484 __u64 start, __u64 end, struct lu_attr *la,
485 struct filter_fid *ff, struct obdo *oa)
487 struct ofd_thread_info *info = ofd_info(env);
488 struct ofd_device *ofd = ofd_obj2dev(fo);
489 struct ofd_mod_data *fmd;
490 struct dt_object *dob = ofd_object_child(fo);
497 /* we support truncate, not punch yet */
498 LASSERT(end == OBD_OBJECT_EOF);
500 fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
501 if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
502 fmd->fmd_mactime_xid = info->fti_xid;
503 ofd_fmd_put(info->fti_exp, fmd);
505 ofd_write_lock(env, fo);
506 if (!ofd_object_exists(fo))
507 GOTO(unlock, rc = -ENOENT);
509 if (ofd->ofd_lfsck_verify_pfid && oa->o_valid & OBD_MD_FLFID) {
510 rc = ofd_verify_ff(env, fo, oa);
515 /* VBR: version recovery check */
516 rc = ofd_version_get_check(info, fo);
520 rc = ofd_attr_handle_ugid(env, fo, la, 0 /* !is_setattr */);
525 rc = ofd_object_ff_load(env, fo);
532 th = ofd_trans_create(env, ofd);
534 GOTO(unlock, rc = PTR_ERR(th));
536 rc = dt_declare_attr_set(env, dob, la, th);
540 rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
545 info->fti_buf.lb_buf = ff;
546 info->fti_buf.lb_len = sizeof(*ff);
547 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
548 &info->fti_buf, XATTR_NAME_FID, 0,
554 rc = ofd_trans_start(env, ofd, fo, th);
558 rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th,
559 ofd_object_capa(env, fo));
563 rc = dt_attr_set(env, dob, la, th, ofd_object_capa(env, fo));
568 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
569 XATTR_NAME_FID, 0, th, BYPASS_CAPA);
571 fo->ofo_pfid.f_seq = le64_to_cpu(ff->ff_parent.f_seq);
572 fo->ofo_pfid.f_oid = le32_to_cpu(ff->ff_parent.f_oid);
573 /* Currently, the filter_fid::ff_parent::f_ver is not
574 * the real parent MDT-object's FID::f_ver, instead it
575 * is the OST-object index in its parent MDT-object's
577 fo->ofo_pfid.f_stripe_idx =
578 le32_to_cpu(ff->ff_parent.f_stripe_idx);
585 ofd_trans_stop(env, ofd, th, rc);
587 ofd_write_unlock(env, fo);
592 int ofd_object_destroy(const struct lu_env *env, struct ofd_object *fo,
595 struct ofd_device *ofd = ofd_obj2dev(fo);
601 ofd_write_lock(env, fo);
602 if (!ofd_object_exists(fo))
603 GOTO(unlock, rc = -ENOENT);
605 th = ofd_trans_create(env, ofd);
607 GOTO(unlock, rc = PTR_ERR(th));
609 dt_declare_ref_del(env, ofd_object_child(fo), th);
610 dt_declare_destroy(env, ofd_object_child(fo), th);
612 rc = dt_trans_start_local(env, ofd->ofd_osd, th);
614 rc = ofd_trans_start(env, ofd, NULL, th);
618 ofd_fmd_drop(ofd_info(env)->fti_exp, &fo->ofo_header.loh_fid);
620 dt_ref_del(env, ofd_object_child(fo), th);
621 dt_destroy(env, ofd_object_child(fo), th);
623 ofd_trans_stop(env, ofd, th, rc);
625 ofd_write_unlock(env, fo);
629 int ofd_attr_get(const struct lu_env *env, struct ofd_object *fo,
636 if (ofd_object_exists(fo)) {
637 rc = dt_attr_get(env, ofd_object_child(fo), la,
638 ofd_object_capa(env, fo));
640 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
641 /* Try to correct for a bug in 2.1.0 (LU-221) that caused
642 * negative timestamps to appear to be in the far future,
643 * due old timestamp being stored on disk as an unsigned value.
644 * This fixes up any bad values stored on disk before
645 * returning them to the client, and ensures any timestamp
646 * updates are correct. LU-1042 */
647 if (unlikely(la->la_atime == LU221_BAD_TIME))
649 if (unlikely(la->la_mtime == LU221_BAD_TIME))
651 if (unlikely(la->la_ctime == LU221_BAD_TIME))
654 #warning "remove old LU-221/LU-1042 workaround code"