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, 2012, Intel, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/osp/osp_object.c
38 * Lustre OST Proxy Device
40 * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41 * Author: Mikhail Pershin <mike.tappro@intel.com>
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include "osp_internal.h"
51 static __u64 osp_object_assign_id(const struct lu_env *env,
52 struct osp_device *d, struct osp_object *o)
54 struct osp_thread_info *osi = osp_env_info(env);
55 const struct lu_fid *f = lu_object_fid(&o->opo_obj.do_lu);
57 LASSERT(fid_is_zero(f));
58 LASSERT(o->opo_reserved);
61 /* assign fid to anonymous object */
62 osi->osi_oi.oi_id = osp_precreate_get_id(d);
63 osi->osi_oi.oi_seq = FID_SEQ_OST_MDT0;
64 fid_ostid_unpack(&osi->osi_fid, &osi->osi_oi, d->opd_index);
65 lu_object_assign_fid(env, &o->opo_obj.do_lu, &osi->osi_fid);
67 return osi->osi_oi.oi_id;
70 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
71 const struct lu_attr *attr, struct thandle *th)
73 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
74 struct osp_object *o = dt2osp_obj(dt);
80 * Usually we don't allow server stack to manipulate size
81 * but there is a special case when striping is created
82 * late, after stripless file got truncated to non-zero.
84 * In this case we do the following:
86 * 1) grab id in declare - this can lead to leaked OST objects
87 * but we don't currently have proper mechanism and the only
88 * options we have are to do truncate RPC holding transaction
89 * open (very bad) or to grab id in declare at cost of leaked
90 * OST object in same very rare unfortunate case (just bad)
91 * notice 1.6-2.0 do assignment outside of running transaction
92 * all the time, meaning many more chances for leaked objects.
94 * 2) send synchronous truncate RPC with just assigned id
97 if (attr->la_valid & LA_SIZE && attr->la_size > 0) {
98 LASSERT(!dt_object_exists(dt));
99 osp_object_assign_id(env, d, o);
100 rc = osp_object_truncate(env, dt, attr->la_size);
106 /* no need in logging for new objects being created */
110 if (!(attr->la_valid & (LA_UID | LA_GID)))
114 * track all UID/GID changes via llog
116 rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
121 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
122 const struct lu_attr *attr, struct thandle *th,
123 struct lustre_capa *capa)
125 struct osp_object *o = dt2osp_obj(dt);
130 /* we're interested in uid/gid changes only */
131 if (!(attr->la_valid & (LA_UID | LA_GID)))
134 /* new object, the very first ->attr_set()
135 * initializing attributes needs no logging
136 * all subsequent one are subject to the
137 * logging and synchronization with OST */
144 * once transaction is committed put proper command on
145 * the queue going to our OST
147 rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
149 /* XXX: send new uid/gid to OST ASAP? */
154 static int osp_declare_object_create(const struct lu_env *env,
155 struct dt_object *dt,
156 struct lu_attr *attr,
157 struct dt_allocation_hint *hint,
158 struct dt_object_format *dof,
161 struct osp_thread_info *osi = osp_env_info(env);
162 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
163 struct osp_object *o = dt2osp_obj(dt);
164 const struct lu_fid *fid;
169 LASSERT(d->opd_last_used_file);
170 fid = lu_object_fid(&dt->do_lu);
173 * There can be gaps in precreated ids and record to unlink llog
175 rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
177 if (unlikely(!fid_is_zero(fid))) {
178 /* replay case: caller knows fid */
179 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
180 rc = dt_declare_record_write(env, d->opd_last_used_file,
181 sizeof(osi->osi_id), osi->osi_off,
187 * in declaration we need to reserve object so that we don't block
188 * awaiting precreation RPC to complete
190 rc = osp_precreate_reserve(env, d);
192 * we also need to declare update to local "last used id" file for
193 * recovery if object isn't used for a reason, we need to release
194 * reservation, this can be made in osd_object_release()
197 /* mark id is reserved: in create we don't want to talk
199 LASSERT(o->opo_reserved == 0);
202 /* common for all OSPs file hystorically */
203 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
204 rc = dt_declare_record_write(env, d->opd_last_used_file,
205 sizeof(osi->osi_id), osi->osi_off,
208 /* not needed in the cache anymore */
209 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE,
210 &dt->do_lu.lo_header->loh_flags);
215 static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
216 struct lu_attr *attr,
217 struct dt_allocation_hint *hint,
218 struct dt_object_format *dof, struct thandle *th)
220 struct osp_thread_info *osi = osp_env_info(env);
221 struct osp_device *d = lu2osp_dev(dt->do_lu.lo_dev);
222 struct osp_object *o = dt2osp_obj(dt);
227 if (o->opo_reserved) {
228 /* regular case, id is assigned holding transaction open */
229 osi->osi_id = osp_object_assign_id(env, d, o);
231 /* special case, id was assigned outside of transaction
232 * see comments in osp_declare_attr_set */
233 rc = fid_ostid_pack(lu_object_fid(&dt->do_lu), &osi->osi_oi);
235 osi->osi_id = ostid_id(&osi->osi_oi);
236 cfs_spin_lock(&d->opd_pre_lock);
237 osp_update_last_id(d, osi->osi_id);
238 cfs_spin_unlock(&d->opd_pre_lock);
241 LASSERT(osi->osi_id);
244 * it's OK if the import is inactive by this moment - id was created
245 * by OST earlier, we just need to maintain it consistently on the disk
246 * once import is reconnected, OSP will claim this and other objects
247 * used and OST either keep them, if they exist or recreate
250 /* we might have lost precreated objects */
251 if (unlikely(d->opd_gap_count) > 0) {
252 cfs_spin_lock(&d->opd_pre_lock);
253 if (d->opd_gap_count > 0) {
254 int count = d->opd_gap_count;
256 osi->osi_oi.oi_id = d->opd_gap_start;
257 d->opd_gap_count = 0;
258 cfs_spin_unlock(&d->opd_pre_lock);
260 CDEBUG(D_HA, "Found gap "LPU64"+%d in objids\n",
261 d->opd_gap_start, count);
262 /* real gap handling is disabled intil ORI-692 will be
263 * fixed, now we only report gaps */
265 cfs_spin_unlock(&d->opd_pre_lock);
269 /* new object, the very first ->attr_set()
270 * initializing attributes needs no logging */
273 osp_objid_buf_prep(osi, d, d->opd_index);
274 rc = dt_record_write(env, d->opd_last_used_file, &osi->osi_lb,
280 static int osp_declare_object_destroy(const struct lu_env *env,
281 struct dt_object *dt, struct thandle *th)
283 struct osp_object *o = dt2osp_obj(dt);
289 * track objects to be destroyed via llog
291 rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
296 static int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
299 struct osp_object *o = dt2osp_obj(dt);
305 * once transaction is committed put proper command on
306 * the queue going to our OST
308 rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
310 /* not needed in cache any more */
311 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
316 struct dt_object_operations osp_obj_ops = {
317 .do_declare_attr_set = osp_declare_attr_set,
318 .do_attr_set = osp_attr_set,
319 .do_declare_create = osp_declare_object_create,
320 .do_create = osp_object_create,
321 .do_declare_destroy = osp_declare_object_destroy,
322 .do_destroy = osp_object_destroy,
325 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
326 const struct lu_object_conf *unused)
328 struct osp_object *po = lu2osp_obj(o);
330 po->opo_obj.do_ops = &osp_obj_ops;
335 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
337 struct osp_object *obj = lu2osp_obj(o);
338 struct lu_object_header *h = o->lo_header;
340 dt_object_fini(&obj->opo_obj);
341 lu_object_header_fini(h);
342 OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
345 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
347 struct osp_object *po = lu2osp_obj(o);
348 struct osp_device *d = lu2osp_dev(o->lo_dev);
353 * release reservation if object was declared but not created
354 * this may require lu_object_put() in LOD
356 if (unlikely(po->opo_reserved)) {
357 LASSERT(d->opd_pre_reserved > 0);
358 cfs_spin_lock(&d->opd_pre_lock);
359 d->opd_pre_reserved--;
360 cfs_spin_unlock(&d->opd_pre_lock);
362 /* not needed in cache any more */
363 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
368 static int osp_object_print(const struct lu_env *env, void *cookie,
369 lu_printer_t p, const struct lu_object *l)
371 const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
373 return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
376 static int osp_object_invariant(const struct lu_object *o)
381 struct lu_object_operations osp_lu_obj_ops = {
382 .loo_object_init = osp_object_init,
383 .loo_object_free = osp_object_free,
384 .loo_object_release = osp_object_release,
385 .loo_object_print = osp_object_print,
386 .loo_object_invariant = osp_object_invariant