Whamcloud - gitweb
61a7588fd10fb32d47f3da876031c5d06c3e17e8
[fs/lustre-release.git] / lustre / osp / osp_object.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osp/osp_object.c
37  *
38  * Lustre OST Proxy Device
39  *
40  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
41  * Author: Mikhail Pershin <mike.tappro@intel.com>
42  */
43
44 #ifndef EXPORT_SYMTAB
45 # define EXPORT_SYMTAB
46 #endif
47 #define DEBUG_SUBSYSTEM S_MDS
48
49 #include "osp_internal.h"
50
51 static __u64 osp_object_assign_id(const struct lu_env *env,
52                                   struct osp_device *d, struct osp_object *o)
53 {
54         struct osp_thread_info  *osi = osp_env_info(env);
55         const struct lu_fid     *f = lu_object_fid(&o->opo_obj.do_lu);
56
57         LASSERT(fid_is_zero(f));
58         LASSERT(o->opo_reserved);
59         o->opo_reserved = 0;
60
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);
66
67         return osi->osi_oi.oi_id;
68 }
69
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)
72 {
73         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
74         struct osp_object       *o = dt2osp_obj(dt);
75         int                      rc = 0;
76
77         ENTRY;
78
79         /*
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.
83          *
84          * In this case we do the following:
85          *
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.
93          *
94          * 2) send synchronous truncate RPC with just assigned id
95          */
96
97         /* there are few places in MDD code still passing NULL
98          * XXX: to be fixed soon */
99         if (attr == NULL)
100                 RETURN(0);
101
102         if (attr->la_valid & LA_SIZE && attr->la_size > 0) {
103                 LASSERT(!dt_object_exists(dt));
104                 osp_object_assign_id(env, d, o);
105                 rc = osp_object_truncate(env, dt, attr->la_size);
106                 if (rc)
107                         RETURN(rc);
108         }
109
110         if (o->opo_new) {
111                 /* no need in logging for new objects being created */
112                 RETURN(0);
113         }
114
115         if (!(attr->la_valid & (LA_UID | LA_GID)))
116                 RETURN(0);
117
118         /*
119          * track all UID/GID changes via llog
120          */
121         rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
122
123         RETURN(rc);
124 }
125
126 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
127                         const struct lu_attr *attr, struct thandle *th,
128                         struct lustre_capa *capa)
129 {
130         struct osp_object       *o = dt2osp_obj(dt);
131         int                      rc = 0;
132
133         ENTRY;
134
135         /* we're interested in uid/gid changes only */
136         if (!(attr->la_valid & (LA_UID | LA_GID)))
137                 RETURN(0);
138
139         /* new object, the very first ->attr_set()
140          * initializing attributes needs no logging
141          * all subsequent one are subject to the
142          * logging and synchronization with OST */
143         if (o->opo_new) {
144                 o->opo_new = 0;
145                 RETURN(0);
146         }
147
148         /*
149          * once transaction is committed put proper command on
150          * the queue going to our OST
151          */
152         rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
153
154         /* XXX: send new uid/gid to OST ASAP? */
155
156         RETURN(rc);
157 }
158
159 static int osp_declare_object_create(const struct lu_env *env,
160                                      struct dt_object *dt,
161                                      struct lu_attr *attr,
162                                      struct dt_allocation_hint *hint,
163                                      struct dt_object_format *dof,
164                                      struct thandle *th)
165 {
166         struct osp_thread_info  *osi = osp_env_info(env);
167         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
168         struct osp_object       *o = dt2osp_obj(dt);
169         const struct lu_fid     *fid;
170         int                      rc = 0;
171
172         ENTRY;
173
174         LASSERT(d->opd_last_used_file);
175         fid = lu_object_fid(&dt->do_lu);
176
177         /*
178          * There can be gaps in precreated ids and record to unlink llog
179          * XXX: we do not handle gaps yet, implemented before solution
180          *      was found to be racy, so we disabled that. there is no
181          *      point in making useless but expensive llog declaration.
182          */
183         /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
184
185         if (unlikely(!fid_is_zero(fid))) {
186                 /* replay case: caller knows fid */
187                 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
188                 rc = dt_declare_record_write(env, d->opd_last_used_file,
189                                              sizeof(osi->osi_id), osi->osi_off,
190                                              th);
191                 RETURN(rc);
192         }
193
194         /*
195          * in declaration we need to reserve object so that we don't block
196          * awaiting precreation RPC to complete
197          */
198         rc = osp_precreate_reserve(env, d);
199         /*
200          * we also need to declare update to local "last used id" file for
201          * recovery if object isn't used for a reason, we need to release
202          * reservation, this can be made in osd_object_release()
203          */
204         if (rc == 0) {
205                 /* mark id is reserved: in create we don't want to talk
206                  * to OST */
207                 LASSERT(o->opo_reserved == 0);
208                 o->opo_reserved = 1;
209
210                 /* common for all OSPs file hystorically */
211                 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
212                 rc = dt_declare_record_write(env, d->opd_last_used_file,
213                                              sizeof(osi->osi_id), osi->osi_off,
214                                              th);
215         } else {
216                 /* not needed in the cache anymore */
217                 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE,
218                             &dt->do_lu.lo_header->loh_flags);
219         }
220         RETURN(rc);
221 }
222
223 static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
224                              struct lu_attr *attr,
225                              struct dt_allocation_hint *hint,
226                              struct dt_object_format *dof, struct thandle *th)
227 {
228         struct osp_thread_info  *osi = osp_env_info(env);
229         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
230         struct osp_object       *o = dt2osp_obj(dt);
231         int                      rc = 0;
232
233         ENTRY;
234
235         if (o->opo_reserved) {
236                 /* regular case, id is assigned holding transaction open */
237                 osi->osi_id = osp_object_assign_id(env, d, o);
238         } else {
239                 /* special case, id was assigned outside of transaction
240                  * see comments in osp_declare_attr_set */
241                 rc = fid_ostid_pack(lu_object_fid(&dt->do_lu), &osi->osi_oi);
242                 LASSERT(rc == 0);
243                 osi->osi_id = ostid_id(&osi->osi_oi);
244                 cfs_spin_lock(&d->opd_pre_lock);
245                 osp_update_last_id(d, osi->osi_id);
246                 cfs_spin_unlock(&d->opd_pre_lock);
247         }
248
249         LASSERT(osi->osi_id);
250
251         /*
252          * it's OK if the import is inactive by this moment - id was created
253          * by OST earlier, we just need to maintain it consistently on the disk
254          * once import is reconnected, OSP will claim this and other objects
255          * used and OST either keep them, if they exist or recreate
256          */
257
258         /* we might have lost precreated objects */
259         if (unlikely(d->opd_gap_count) > 0) {
260                 cfs_spin_lock(&d->opd_pre_lock);
261                 if (d->opd_gap_count > 0) {
262                         int count = d->opd_gap_count;
263
264                         osi->osi_oi.oi_id = d->opd_gap_start;
265                         d->opd_gap_count = 0;
266                         cfs_spin_unlock(&d->opd_pre_lock);
267
268                         CDEBUG(D_HA, "Found gap "LPU64"+%d in objids\n",
269                                d->opd_gap_start, count);
270                         /* real gap handling is disabled intil ORI-692 will be
271                          * fixed, now we only report gaps */
272                 } else {
273                         cfs_spin_unlock(&d->opd_pre_lock);
274                 }
275         }
276
277         /* new object, the very first ->attr_set()
278          * initializing attributes needs no logging */
279         o->opo_new = 1;
280
281         osp_objid_buf_prep(osi, d, d->opd_index);
282         rc = dt_record_write(env, d->opd_last_used_file, &osi->osi_lb,
283                              &osi->osi_off, th);
284
285         RETURN(rc);
286 }
287
288 static int osp_declare_object_destroy(const struct lu_env *env,
289                                       struct dt_object *dt, struct thandle *th)
290 {
291         struct osp_object       *o = dt2osp_obj(dt);
292         int                      rc = 0;
293
294         ENTRY;
295
296         /*
297          * track objects to be destroyed via llog
298          */
299         rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
300
301         RETURN(rc);
302 }
303
304 static int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
305                               struct thandle *th)
306 {
307         struct osp_object       *o = dt2osp_obj(dt);
308         int                      rc = 0;
309
310         ENTRY;
311
312         /*
313          * once transaction is committed put proper command on
314          * the queue going to our OST
315          */
316         rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
317
318         /* not needed in cache any more */
319         cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
320
321         RETURN(rc);
322 }
323
324 struct dt_object_operations osp_obj_ops = {
325         .do_declare_attr_set    = osp_declare_attr_set,
326         .do_attr_set            = osp_attr_set,
327         .do_declare_create      = osp_declare_object_create,
328         .do_create              = osp_object_create,
329         .do_declare_destroy     = osp_declare_object_destroy,
330         .do_destroy             = osp_object_destroy,
331 };
332
333 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
334                            const struct lu_object_conf *unused)
335 {
336         struct osp_object *po = lu2osp_obj(o);
337
338         po->opo_obj.do_ops = &osp_obj_ops;
339
340         return 0;
341 }
342
343 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
344 {
345         struct osp_object       *obj = lu2osp_obj(o);
346         struct lu_object_header *h = o->lo_header;
347
348         dt_object_fini(&obj->opo_obj);
349         lu_object_header_fini(h);
350         OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
351 }
352
353 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
354 {
355         struct osp_object       *po = lu2osp_obj(o);
356         struct osp_device       *d  = lu2osp_dev(o->lo_dev);
357
358         ENTRY;
359
360         /*
361          * release reservation if object was declared but not created
362          * this may require lu_object_put() in LOD
363          */
364         if (unlikely(po->opo_reserved)) {
365                 LASSERT(d->opd_pre_reserved > 0);
366                 cfs_spin_lock(&d->opd_pre_lock);
367                 d->opd_pre_reserved--;
368                 cfs_spin_unlock(&d->opd_pre_lock);
369
370                 /* not needed in cache any more */
371                 cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
372         }
373         EXIT;
374 }
375
376 static int osp_object_print(const struct lu_env *env, void *cookie,
377                             lu_printer_t p, const struct lu_object *l)
378 {
379         const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
380
381         return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
382 }
383
384 static int osp_object_invariant(const struct lu_object *o)
385 {
386         LBUG();
387 }
388
389 struct lu_object_operations osp_lu_obj_ops = {
390         .loo_object_init        = osp_object_init,
391         .loo_object_free        = osp_object_free,
392         .loo_object_release     = osp_object_release,
393         .loo_object_print       = osp_object_print,
394         .loo_object_invariant   = osp_object_invariant
395 };