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, 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>
47 #include "ofd_internal.h"
49 int ofd_version_get_check(struct ofd_thread_info *info,
50 struct ofd_object *fo)
52 dt_obj_version_t curr_version;
54 LASSERT(ofd_object_exists(fo));
55 LASSERT(info->fti_exp);
57 curr_version = dt_version_get(info->fti_env, ofd_object_child(fo));
58 if ((__s64)curr_version == -EOPNOTSUPP)
60 /* VBR: version is checked always because costs nothing */
61 if (info->fti_pre_version != 0 &&
62 info->fti_pre_version != curr_version) {
63 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
64 info->fti_pre_version, curr_version);
65 spin_lock(&info->fti_exp->exp_lock);
66 info->fti_exp->exp_vbr_failed = 1;
67 spin_unlock(&info->fti_exp->exp_lock);
70 info->fti_pre_version = curr_version;
74 struct ofd_object *ofd_object_find(const struct lu_env *env,
75 struct ofd_device *ofd,
76 const struct lu_fid *fid)
78 struct ofd_object *fo;
83 o = lu_object_find(env, &ofd->ofd_dt_dev.dd_lu_dev, fid, NULL);
84 if (likely(!IS_ERR(o)))
87 fo = (struct ofd_object *)o; /* return error */
91 struct ofd_object *ofd_object_find_or_create(const struct lu_env *env,
92 struct ofd_device *ofd,
93 const struct lu_fid *fid,
96 struct ofd_thread_info *info = ofd_info(env);
97 struct lu_object *fo_obj;
98 struct dt_object *dto;
102 info->fti_dof.dof_type = dt_mode_to_dft(S_IFREG);
104 dto = dt_find_or_create(env, ofd->ofd_osd, fid, &info->fti_dof, attr);
106 RETURN((struct ofd_object *)dto);
108 fo_obj = lu_object_locate(dto->do_lu.lo_header,
109 ofd->ofd_dt_dev.dd_lu_dev.ld_type);
110 RETURN(ofd_obj(fo_obj));
113 int ofd_object_ff_check(const struct lu_env *env, struct ofd_object *fo)
119 if (!fo->ofo_ff_exists) {
121 * This actually means that we don't know whether the object
122 * has the "fid" EA or not.
124 rc = dt_xattr_get(env, ofd_object_child(fo), &LU_BUF_NULL,
125 XATTR_NAME_FID, BYPASS_CAPA);
126 if (rc >= 0 || rc == -ENODATA) {
128 * Here we assume that, if the object doesn't have the
129 * "fid" EA, the caller will add one, unless a fatal
130 * error (e.g., a memory or disk failure) prevents it
133 fo->ofo_ff_exists = 1;
141 void ofd_object_put(const struct lu_env *env, struct ofd_object *fo)
143 lu_object_put(env, &fo->ofo_obj.do_lu);
146 int ofd_precreate_objects(const struct lu_env *env, struct ofd_device *ofd,
147 obd_id id, struct ofd_seq *oseq, int nr, int sync)
149 struct ofd_thread_info *info = ofd_info(env);
150 struct ofd_object *fo = NULL;
151 struct dt_object *next;
153 struct ofd_object **batch;
162 /* Don't create objects beyond the valid range for this SEQ */
163 if (unlikely(fid_seq_is_mdt0(ostid_seq(&oseq->os_oi)) &&
164 (id + nr) >= IDIF_MAX_OID)) {
165 CERROR("%s:"DOSTID" hit the IDIF_MAX_OID (1<<48)!\n",
166 ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
167 RETURN(rc = -ENOSPC);
168 } else if (unlikely(!fid_seq_is_mdt0(ostid_seq(&oseq->os_oi)) &&
169 (id + nr) >= OBIF_MAX_OID)) {
170 CERROR("%s:"DOSTID" hit the OBIF_MAX_OID (1<<32)!\n",
171 ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
172 RETURN(rc = -ENOSPC);
175 OBD_ALLOC(batch, nr_saved * sizeof(struct ofd_object *));
179 info->fti_attr.la_valid = LA_TYPE | LA_MODE;
181 * We mark object SUID+SGID to flag it for accepting UID+GID from
182 * client on first write. Currently the permission bits on the OST are
183 * never used, so this is OK.
185 info->fti_attr.la_mode = S_IFREG | S_ISUID | S_ISGID | 0666;
186 info->fti_dof.dof_type = dt_mode_to_dft(S_IFREG);
188 /* Initialize a/c/m time so any client timestamp will always
189 * be newer and update the inode. ctime = 0 is also handled
190 * specially in osd_inode_setattr(). See LU-221, LU-1042 */
191 info->fti_attr.la_valid |= LA_ATIME | LA_MTIME | LA_CTIME;
192 info->fti_attr.la_atime = 0;
193 info->fti_attr.la_mtime = 0;
194 info->fti_attr.la_ctime = 0;
196 /* prepare objects */
197 ostid_set_seq(&info->fti_ostid, ostid_seq(&oseq->os_oi));
198 for (i = 0; i < nr; i++) {
199 ostid_set_id(&info->fti_ostid, id + i);
200 rc = ostid_to_fid(&info->fti_fid, &info->fti_ostid, 0);
203 GOTO(out, rc = PTR_ERR(fo));
209 fo = ofd_object_find(env, ofd, &info->fti_fid);
212 GOTO(out, rc = PTR_ERR(fo));
218 ofd_write_lock(env, fo);
221 info->fti_buf.lb_buf = &tmp;
222 info->fti_buf.lb_len = sizeof(tmp);
225 th = ofd_trans_create(env, ofd);
227 GOTO(out, rc = PTR_ERR(th));
231 rc = dt_declare_record_write(env, oseq->os_lastid_obj, sizeof(tmp),
234 GOTO(trans_stop, rc);
236 for (i = 0; i < nr; i++) {
240 if (unlikely(ofd_object_exists(fo))) {
241 /* object may exist being re-created by write replay */
242 CDEBUG(D_INODE, "object "LPX64"/"LPX64" exists: "
243 DFID"\n", ostid_seq(&oseq->os_oi), id,
244 PFID(&info->fti_fid));
248 next = ofd_object_child(fo);
249 LASSERT(next != NULL);
251 rc = dt_declare_create(env, next, &info->fti_attr, NULL,
259 rc = dt_trans_start_local(env, ofd->ofd_osd, th);
261 GOTO(trans_stop, rc);
263 CDEBUG(D_OTHER, "%s: create new object "DFID" nr %d\n",
264 ofd_name(ofd), PFID(&info->fti_fid), nr);
266 for (i = 0; i < nr; i++) {
270 if (likely(!ofd_object_exists(fo))) {
271 next = ofd_object_child(fo);
272 LASSERT(next != NULL);
274 rc = dt_create(env, next, &info->fti_attr, NULL,
278 LASSERT(ofd_object_exists(fo));
280 ofd_seq_last_oid_set(oseq, id + i);
285 tmp = cpu_to_le64(ofd_seq_last_oid(oseq));
286 rc = dt_record_write(env, oseq->os_lastid_obj,
287 &info->fti_buf, &info->fti_off, th);
290 ofd_trans_stop(env, ofd, th, rc);
292 for (i = 0; i < nr_saved; i++) {
295 ofd_write_unlock(env, fo);
296 ofd_object_put(env, fo);
299 OBD_FREE(batch, nr_saved * sizeof(struct ofd_object *));
301 CDEBUG((objects == 0 && rc == 0) ? D_ERROR : D_OTHER,
302 "created %d/%d objects: %d\n", objects, nr_saved, rc);
304 LASSERT(ergo(objects == 0, rc < 0));
305 RETURN(objects > 0 ? objects : rc);
309 * If the object still has SUID+SGID bits set (see ofd_precreate_object()) then
310 * we will accept the UID+GID if sent by the client for initializing the
311 * ownership of this object. We only allow this to happen once (so clear these
312 * bits) and later only allow setattr.
314 int ofd_attr_handle_ugid(const struct lu_env *env, struct ofd_object *fo,
315 struct lu_attr *la, int is_setattr)
317 struct ofd_thread_info *info = ofd_info(env);
318 struct lu_attr *ln = &info->fti_attr2;
324 if (!(la->la_valid & LA_UID) && !(la->la_valid & LA_GID))
327 rc = dt_attr_get(env, ofd_object_child(fo), ln, BYPASS_CAPA);
331 LASSERT(ln->la_valid & LA_MODE);
334 if (!(ln->la_mode & S_ISUID))
335 la->la_valid &= ~LA_UID;
336 if (!(ln->la_mode & S_ISGID))
337 la->la_valid &= ~LA_GID;
340 if ((la->la_valid & LA_UID) && (ln->la_mode & S_ISUID))
342 if ((la->la_valid & LA_GID) && (ln->la_mode & S_ISGID))
345 if (!(la->la_valid & LA_MODE) || !is_setattr) {
346 la->la_mode = ln->la_mode;
347 la->la_valid |= LA_MODE;
349 la->la_mode &= ~mask;
355 int ofd_attr_set(const struct lu_env *env, struct ofd_object *fo,
356 struct lu_attr *la, struct filter_fid *ff)
358 struct ofd_thread_info *info = ofd_info(env);
359 struct ofd_device *ofd = ofd_obj2dev(fo);
361 struct ofd_mod_data *fmd;
366 ofd_write_lock(env, fo);
367 if (!ofd_object_exists(fo))
368 GOTO(unlock, rc = -ENOENT);
370 if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME)) {
371 fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
372 if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
373 fmd->fmd_mactime_xid = info->fti_xid;
374 ofd_fmd_put(info->fti_exp, fmd);
377 /* VBR: version recovery check */
378 rc = ofd_version_get_check(info, fo);
382 rc = ofd_attr_handle_ugid(env, fo, la, 1 /* is_setattr */);
387 rc = ofd_object_ff_check(env, fo);
394 th = ofd_trans_create(env, ofd);
396 GOTO(unlock, rc = PTR_ERR(th));
398 rc = dt_declare_attr_set(env, ofd_object_child(fo), la, th);
403 info->fti_buf.lb_buf = ff;
404 info->fti_buf.lb_len = sizeof(*ff);
405 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
406 &info->fti_buf, XATTR_NAME_FID, 0,
412 rc = ofd_trans_start(env, ofd, la->la_valid & LA_SIZE ? fo : NULL, th);
416 rc = dt_attr_set(env, ofd_object_child(fo), la, th,
417 ofd_object_capa(env, fo));
422 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
423 XATTR_NAME_FID, 0, th, BYPASS_CAPA);
426 ofd_trans_stop(env, ofd, th, rc);
428 ofd_write_unlock(env, fo);
432 int ofd_object_punch(const struct lu_env *env, struct ofd_object *fo,
433 __u64 start, __u64 end, struct lu_attr *la,
434 struct filter_fid *ff)
436 struct ofd_thread_info *info = ofd_info(env);
437 struct ofd_device *ofd = ofd_obj2dev(fo);
438 struct ofd_mod_data *fmd;
439 struct dt_object *dob = ofd_object_child(fo);
446 /* we support truncate, not punch yet */
447 LASSERT(end == OBD_OBJECT_EOF);
449 fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
450 if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
451 fmd->fmd_mactime_xid = info->fti_xid;
452 ofd_fmd_put(info->fti_exp, fmd);
454 ofd_write_lock(env, fo);
455 if (!ofd_object_exists(fo))
456 GOTO(unlock, rc = -ENOENT);
458 /* VBR: version recovery check */
459 rc = ofd_version_get_check(info, fo);
463 rc = ofd_attr_handle_ugid(env, fo, la, 0 /* !is_setattr */);
468 rc = ofd_object_ff_check(env, fo);
475 th = ofd_trans_create(env, ofd);
477 GOTO(unlock, rc = PTR_ERR(th));
479 rc = dt_declare_attr_set(env, dob, la, th);
483 rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
488 info->fti_buf.lb_buf = ff;
489 info->fti_buf.lb_len = sizeof(*ff);
490 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
491 &info->fti_buf, XATTR_NAME_FID, 0,
497 rc = ofd_trans_start(env, ofd, fo, th);
501 rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th,
502 ofd_object_capa(env, fo));
506 rc = dt_attr_set(env, dob, la, th, ofd_object_capa(env, fo));
511 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
512 XATTR_NAME_FID, 0, th, BYPASS_CAPA);
515 ofd_trans_stop(env, ofd, th, rc);
517 ofd_write_unlock(env, fo);
521 int ofd_object_destroy(const struct lu_env *env, struct ofd_object *fo,
524 struct ofd_device *ofd = ofd_obj2dev(fo);
530 ofd_write_lock(env, fo);
531 if (!ofd_object_exists(fo))
532 GOTO(unlock, rc = -ENOENT);
534 th = ofd_trans_create(env, ofd);
536 GOTO(unlock, rc = PTR_ERR(th));
538 dt_declare_ref_del(env, ofd_object_child(fo), th);
539 dt_declare_destroy(env, ofd_object_child(fo), th);
541 rc = dt_trans_start_local(env, ofd->ofd_osd, th);
543 rc = ofd_trans_start(env, ofd, NULL, th);
547 ofd_fmd_drop(ofd_info(env)->fti_exp, &fo->ofo_header.loh_fid);
549 dt_ref_del(env, ofd_object_child(fo), th);
550 dt_destroy(env, ofd_object_child(fo), th);
552 ofd_trans_stop(env, ofd, th, rc);
554 ofd_write_unlock(env, fo);
558 int ofd_attr_get(const struct lu_env *env, struct ofd_object *fo,
565 if (ofd_object_exists(fo)) {
566 rc = dt_attr_get(env, ofd_object_child(fo), la,
567 ofd_object_capa(env, fo));
569 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
570 /* Try to correct for a bug in 2.1.0 (LU-221) that caused
571 * negative timestamps to appear to be in the far future,
572 * due old timestamp being stored on disk as an unsigned value.
573 * This fixes up any bad values stored on disk before
574 * returning them to the client, and ensures any timestamp
575 * updates are correct. LU-1042 */
576 if (unlikely(la->la_atime == LU221_BAD_TIME))
578 if (unlikely(la->la_mtime == LU221_BAD_TIME))
580 if (unlikely(la->la_ctime == LU221_BAD_TIME))
583 #warning "remove old LU-221/LU-1042 workaround code"