Whamcloud - gitweb
LU-1876 hsm: revise ll_setattr_raw to not check stripe data
[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) 2012, Intel Corporation.
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 void osp_object_assign_fid(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
56         LASSERT(fid_is_zero(lu_object_fid(&o->opo_obj.do_lu)));
57         LASSERT(o->opo_reserved);
58         o->opo_reserved = 0;
59
60         osp_precreate_get_fid(env, d, &osi->osi_fid);
61
62         lu_object_assign_fid(env, &o->opo_obj.do_lu, &osi->osi_fid);
63 }
64
65 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
66                                 const struct lu_attr *attr, struct thandle *th)
67 {
68         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
69         struct osp_object       *o = dt2osp_obj(dt);
70         int                      rc = 0;
71
72         ENTRY;
73
74         /*
75          * Usually we don't allow server stack to manipulate size
76          * but there is a special case when striping is created
77          * late, after stripless file got truncated to non-zero.
78          *
79          * In this case we do the following:
80          *
81          * 1) grab id in declare - this can lead to leaked OST objects
82          *    but we don't currently have proper mechanism and the only
83          *    options we have are to do truncate RPC holding transaction
84          *    open (very bad) or to grab id in declare at cost of leaked
85          *    OST object in same very rare unfortunate case (just bad)
86          *    notice 1.6-2.0 do assignment outside of running transaction
87          *    all the time, meaning many more chances for leaked objects.
88          *
89          * 2) send synchronous truncate RPC with just assigned id
90          */
91
92         /* there are few places in MDD code still passing NULL
93          * XXX: to be fixed soon */
94         if (attr == NULL)
95                 RETURN(0);
96
97         if (attr->la_valid & LA_SIZE && attr->la_size > 0 &&
98             fid_is_zero(lu_object_fid(&o->opo_obj.do_lu))) {
99                 LASSERT(!dt_object_exists(dt));
100                 osp_object_assign_fid(env, d, o);
101                 rc = osp_object_truncate(env, dt, attr->la_size);
102                 if (rc)
103                         RETURN(rc);
104         }
105
106         if (o->opo_new) {
107                 /* no need in logging for new objects being created */
108                 RETURN(0);
109         }
110
111         if (!(attr->la_valid & (LA_UID | LA_GID)))
112                 RETURN(0);
113
114         /*
115          * track all UID/GID changes via llog
116          */
117         rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
118
119         RETURN(rc);
120 }
121
122 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
123                         const struct lu_attr *attr, struct thandle *th,
124                         struct lustre_capa *capa)
125 {
126         struct osp_object       *o = dt2osp_obj(dt);
127         int                      rc = 0;
128
129         ENTRY;
130
131         /* we're interested in uid/gid changes only */
132         if (!(attr->la_valid & (LA_UID | LA_GID)))
133                 RETURN(0);
134
135         /* new object, the very first ->attr_set()
136          * initializing attributes needs no logging
137          * all subsequent one are subject to the
138          * logging and synchronization with OST */
139         if (o->opo_new) {
140                 o->opo_new = 0;
141                 RETURN(0);
142         }
143
144         /*
145          * once transaction is committed put proper command on
146          * the queue going to our OST
147          */
148         rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
149
150         /* XXX: send new uid/gid to OST ASAP? */
151
152         RETURN(rc);
153 }
154
155 static int osp_declare_object_create(const struct lu_env *env,
156                                      struct dt_object *dt,
157                                      struct lu_attr *attr,
158                                      struct dt_allocation_hint *hint,
159                                      struct dt_object_format *dof,
160                                      struct thandle *th)
161 {
162         struct osp_thread_info  *osi = osp_env_info(env);
163         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
164         struct osp_object       *o = dt2osp_obj(dt);
165         const struct lu_fid     *fid;
166         int                      rc = 0;
167
168         ENTRY;
169
170         /* should happen to non-0 OSP only so that at least one object
171          * has been already declared in the scenario and LOD should
172          * cleanup that */
173         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
174                 RETURN(-ENOSPC);
175
176         LASSERT(d->opd_last_used_oid_file);
177         fid = lu_object_fid(&dt->do_lu);
178
179         /*
180          * There can be gaps in precreated ids and record to unlink llog
181          * XXX: we do not handle gaps yet, implemented before solution
182          *      was found to be racy, so we disabled that. there is no
183          *      point in making useless but expensive llog declaration.
184          */
185         /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
186
187         if (unlikely(!fid_is_zero(fid))) {
188                 /* replay case: caller knows fid */
189                 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
190                 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
191                                              sizeof(osi->osi_id), osi->osi_off,
192                                              th);
193                 RETURN(rc);
194         }
195
196         /*
197          * in declaration we need to reserve object so that we don't block
198          * awaiting precreation RPC to complete
199          */
200         rc = osp_precreate_reserve(env, d);
201         /*
202          * we also need to declare update to local "last used id" file for
203          * recovery if object isn't used for a reason, we need to release
204          * reservation, this can be made in osd_object_release()
205          */
206         if (rc == 0) {
207                 /* mark id is reserved: in create we don't want to talk
208                  * to OST */
209                 LASSERT(o->opo_reserved == 0);
210                 o->opo_reserved = 1;
211
212                 /* common for all OSPs file hystorically */
213                 osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
214                 rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
215                                              sizeof(osi->osi_id), osi->osi_off,
216                                              th);
217         } else {
218                 /* not needed in the cache anymore */
219                 set_bit(LU_OBJECT_HEARD_BANSHEE,
220                             &dt->do_lu.lo_header->loh_flags);
221         }
222         RETURN(rc);
223 }
224
225 static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
226                              struct lu_attr *attr,
227                              struct dt_allocation_hint *hint,
228                              struct dt_object_format *dof, struct thandle *th)
229 {
230         struct osp_thread_info  *osi = osp_env_info(env);
231         struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
232         struct osp_object       *o = dt2osp_obj(dt);
233         int                     rc = 0;
234         struct lu_fid           *fid = &osi->osi_fid;
235         ENTRY;
236
237         if (o->opo_reserved) {
238                 /* regular case, fid is assigned holding trunsaction open */
239                  osp_object_assign_fid(env, d, o);
240         }
241
242         memcpy(fid, lu_object_fid(&dt->do_lu), sizeof(*fid));
243
244         LASSERTF(fid_is_sane(fid), "fid for osp_obj %p is insane"DFID"!\n",
245                  osp_obj, PFID(fid));
246
247         if (!o->opo_reserved) {
248                 /* special case, id was assigned outside of transaction
249                  * see comments in osp_declare_attr_set */
250                 spin_lock(&d->opd_pre_lock);
251                 osp_update_last_fid(d, fid);
252                 spin_unlock(&d->opd_pre_lock);
253         }
254
255         CDEBUG(D_INODE, "fid for osp_obj %p is "DFID"!\n", osp_obj, PFID(fid));
256
257         /* If the precreate ends, it means it will be ready to rollover to
258          * the new sequence soon, all the creation should be synchronized,
259          * otherwise during replay, the replay fid will be inconsistent with
260          * last_used/create fid */
261         if (osp_precreate_end_seq(env, d) && osp_is_fid_client(d))
262                 th->th_sync = 1;
263
264         /*
265          * it's OK if the import is inactive by this moment - id was created
266          * by OST earlier, we just need to maintain it consistently on the disk
267          * once import is reconnected, OSP will claim this and other objects
268          * used and OST either keep them, if they exist or recreate
269          */
270
271         /* we might have lost precreated objects */
272         if (unlikely(d->opd_gap_count) > 0) {
273                 spin_lock(&d->opd_pre_lock);
274                 if (d->opd_gap_count > 0) {
275                         int count = d->opd_gap_count;
276                         osi->osi_oi.oi_id = fid_oid(&d->opd_gap_start_fid);
277                         d->opd_gap_count = 0;
278                         spin_unlock(&d->opd_pre_lock);
279
280                         CDEBUG(D_HA, "Writting gap "DFID"+%d in llog\n",
281                                PFID(&d->opd_gap_start_fid), count);
282                         /* real gap handling is disabled intil ORI-692 will be
283                          * fixed, now we only report gaps */
284                 } else {
285                         spin_unlock(&d->opd_pre_lock);
286                 }
287         }
288
289         /* new object, the very first ->attr_set()
290          * initializing attributes needs no logging */
291         o->opo_new = 1;
292
293         /* Only need update last_used oid file, seq file will only be update
294          * during seq rollover */
295         osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off,
296                            &d->opd_last_used_fid.f_oid, d->opd_index);
297
298         rc = dt_record_write(env, d->opd_last_used_oid_file, &osi->osi_lb,
299                              &osi->osi_off, th);
300
301         CDEBUG(D_HA, "%s: Wrote last used FID: "DFID", index %d: %d\n",
302                d->opd_obd->obd_name, PFID(fid), d->opd_index, rc);
303
304         RETURN(rc);
305 }
306
307 static int osp_declare_object_destroy(const struct lu_env *env,
308                                       struct dt_object *dt, struct thandle *th)
309 {
310         struct osp_object       *o = dt2osp_obj(dt);
311         int                      rc = 0;
312
313         ENTRY;
314
315         /*
316          * track objects to be destroyed via llog
317          */
318         rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
319
320         RETURN(rc);
321 }
322
323 static int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
324                               struct thandle *th)
325 {
326         struct osp_object       *o = dt2osp_obj(dt);
327         int                      rc = 0;
328
329         ENTRY;
330
331         /*
332          * once transaction is committed put proper command on
333          * the queue going to our OST
334          */
335         rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
336
337         /* not needed in cache any more */
338         set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
339
340         RETURN(rc);
341 }
342
343 struct dt_object_operations osp_obj_ops = {
344         .do_declare_attr_set    = osp_declare_attr_set,
345         .do_attr_set            = osp_attr_set,
346         .do_declare_create      = osp_declare_object_create,
347         .do_create              = osp_object_create,
348         .do_declare_destroy     = osp_declare_object_destroy,
349         .do_destroy             = osp_object_destroy,
350 };
351
352 static int is_ost_obj(struct lu_object *lo)
353 {
354         struct osp_device  *osp  = lu2osp_dev(lo->lo_dev);
355
356         return !osp->opd_connect_mdt;
357 }
358
359 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
360                            const struct lu_object_conf *conf)
361 {
362         struct osp_object       *po = lu2osp_obj(o);
363         int                     rc = 0;
364         ENTRY;
365
366         if (is_ost_obj(o)) {
367                 po->opo_obj.do_ops = &osp_obj_ops;
368         } else {
369                 struct lu_attr          *la = &osp_env_info(env)->osi_attr;
370
371                 po->opo_obj.do_ops = &osp_md_obj_ops;
372                 o->lo_header->loh_attr |= LOHA_REMOTE;
373                 po->opo_obj.do_lock_ops = &osp_md_lock_ops;
374                 rc = po->opo_obj.do_ops->do_attr_get(env, lu2dt_obj(o),
375                                                      la, NULL);
376                 if (rc == 0)
377                         o->lo_header->loh_attr |=
378                                 LOHA_EXISTS | (la->la_mode & S_IFMT);
379                 if (rc == -ENOENT)
380                         rc = 0;
381         }
382         RETURN(rc);
383 }
384
385 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
386 {
387         struct osp_object       *obj = lu2osp_obj(o);
388         struct lu_object_header *h = o->lo_header;
389
390         dt_object_fini(&obj->opo_obj);
391         lu_object_header_fini(h);
392         OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
393 }
394
395 static void osp_object_release(const struct lu_env *env, struct lu_object *o)
396 {
397         struct osp_object       *po = lu2osp_obj(o);
398         struct osp_device       *d  = lu2osp_dev(o->lo_dev);
399
400         ENTRY;
401
402         /*
403          * release reservation if object was declared but not created
404          * this may require lu_object_put() in LOD
405          */
406         if (unlikely(po->opo_reserved)) {
407                 LASSERT(d->opd_pre_reserved > 0);
408                 spin_lock(&d->opd_pre_lock);
409                 d->opd_pre_reserved--;
410                 spin_unlock(&d->opd_pre_lock);
411
412                 /* not needed in cache any more */
413                 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
414         }
415         EXIT;
416 }
417
418 static int osp_object_print(const struct lu_env *env, void *cookie,
419                             lu_printer_t p, const struct lu_object *l)
420 {
421         const struct osp_object *o = lu2osp_obj((struct lu_object *)l);
422
423         return (*p)(env, cookie, LUSTRE_OSP_NAME"-object@%p", o);
424 }
425
426 static int osp_object_invariant(const struct lu_object *o)
427 {
428         LBUG();
429 }
430
431 struct lu_object_operations osp_lu_obj_ops = {
432         .loo_object_init        = osp_object_init,
433         .loo_object_free        = osp_object_free,
434         .loo_object_release     = osp_object_release,
435         .loo_object_print       = osp_object_print,
436         .loo_object_invariant   = osp_object_invariant
437 };