Whamcloud - gitweb
LU-137 obdclass: add dt_object_put() and use it
[fs/lustre-release.git] / lustre / ofd / ofd_objects.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ofd/ofd_objects.c
33  *
34  * This file contains OSD API methods related to OBD Filter Device (OFD)
35  * object operations.
36  *
37  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
38  * Author: Mikhail Pershin <mike.pershin@intel.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_FILTER
42
43 #include <dt_object.h>
44 #include <lustre/lustre_idl.h>
45 #include <lustre_lfsck.h>
46
47 #include "ofd_internal.h"
48
49 /**
50  * Get object version from disk and check it.
51  *
52  * This function checks object version from disk with
53  * ofd_thread_info::fti_pre_version filled from incoming RPC. This is part of
54  * VBR (Version-Based Recovery) and ensures that object has the same version
55  * upon replay as it has during original modification.
56  *
57  * \param[in]  info     execution thread OFD private data
58  * \param[in]  fo       OFD object
59  *
60  * \retval              0 if version matches
61  * \retval              -EOVERFLOW on version mismatch
62  */
63 static int ofd_version_get_check(struct ofd_thread_info *info,
64                                  struct ofd_object *fo)
65 {
66         dt_obj_version_t curr_version;
67
68         LASSERT(ofd_object_exists(fo));
69
70         if (info->fti_exp == NULL)
71                 RETURN(0);
72
73         curr_version = dt_version_get(info->fti_env, ofd_object_child(fo));
74         if ((__s64)curr_version == -EOPNOTSUPP)
75                 RETURN(0);
76         /* VBR: version is checked always because costs nothing */
77         if (info->fti_pre_version != 0 &&
78             info->fti_pre_version != curr_version) {
79                 CDEBUG(D_INODE, "Version mismatch %#llx != %#llx\n",
80                        info->fti_pre_version, curr_version);
81                 spin_lock(&info->fti_exp->exp_lock);
82                 info->fti_exp->exp_vbr_failed = 1;
83                 spin_unlock(&info->fti_exp->exp_lock);
84                 RETURN (-EOVERFLOW);
85         }
86         info->fti_pre_version = curr_version;
87         RETURN(0);
88 }
89
90 /**
91  * Get OFD object by FID.
92  *
93  * This function finds OFD slice of compound object with the given FID.
94  *
95  * \param[in] env       execution environment
96  * \param[in] ofd       OFD device
97  * \param[in] fid       FID of the object
98  *
99  * \retval              pointer to the found ofd_object
100  * \retval              ERR_PTR(errno) in case of error
101  */
102 struct ofd_object *ofd_object_find(const struct lu_env *env,
103                                    struct ofd_device *ofd,
104                                    const struct lu_fid *fid)
105 {
106         struct ofd_object *fo;
107         struct lu_object  *o;
108
109         ENTRY;
110
111         o = lu_object_find(env, &ofd->ofd_dt_dev.dd_lu_dev, fid, NULL);
112         if (likely(!IS_ERR(o)))
113                 fo = ofd_obj(o);
114         else
115                 fo = ERR_CAST(o); /* return error */
116
117         RETURN(fo);
118 }
119
120 /**
121  * Get FID of parent MDT object.
122  *
123  * This function reads extended attribute XATTR_NAME_FID of OFD object which
124  * contains the MDT parent object FID and saves it in ofd_object::ofo_pfid.
125  *
126  * The filter_fid::ff_parent::f_ver field currently holds
127  * the OST-object index in the parent MDT-object's layout EA,
128  * not the actual FID::f_ver of the parent. We therefore access
129  * it via the macro f_stripe_idx.
130  *
131  * \param[in] env       execution environment
132  * \param[in] fo        OFD object
133  *
134  * \retval              0 if successful
135  * \retval              -ENODATA if there is no such xattr
136  * \retval              negative value on error
137  */
138 int ofd_object_ff_load(const struct lu_env *env, struct ofd_object *fo)
139 {
140         struct ofd_thread_info  *info = ofd_info(env);
141         struct filter_fid_old   *ff   = &info->fti_mds_fid_old;
142         struct lu_buf           *buf  = &info->fti_buf;
143         struct lu_fid           *pfid = &fo->ofo_pfid;
144         int                      rc   = 0;
145
146         if (fid_is_sane(pfid))
147                 return 0;
148
149         buf->lb_buf = ff;
150         buf->lb_len = sizeof(*ff);
151         rc = dt_xattr_get(env, ofd_object_child(fo), buf, XATTR_NAME_FID);
152         if (rc < 0)
153                 return rc;
154
155         if (rc < sizeof(struct lu_fid)) {
156                 fid_zero(pfid);
157
158                 return -ENODATA;
159         }
160
161         pfid->f_seq = le64_to_cpu(ff->ff_parent.f_seq);
162         pfid->f_oid = le32_to_cpu(ff->ff_parent.f_oid);
163         pfid->f_stripe_idx = le32_to_cpu(ff->ff_parent.f_stripe_idx);
164
165         return 0;
166 }
167
168 /**
169  * Precreate the given number \a nr of objects in the given sequence \a oseq.
170  *
171  * This function precreates new OST objects in the given sequence.
172  * The precreation starts from \a id and creates \a nr objects sequentially.
173  *
174  * Notes:
175  * This function may create fewer objects than requested.
176  *
177  * We mark object SUID+SGID to flag it for accepting UID+GID from client on
178  * first write. Currently the permission bits on the OST are never used,
179  * so this is OK.
180  *
181  * Initialize a/c/m time so any client timestamp will always be newer and
182  * update the inode. The ctime = 0 case is also handled specially in
183  * osd_inode_setattr(). See LU-221, LU-1042 for details.
184  *
185  * \param[in] env       execution environment
186  * \param[in] ofd       OFD device
187  * \param[in] id        object ID to start precreation from
188  * \param[in] oseq      object sequence
189  * \param[in] nr        number of objects to precreate
190  * \param[in] sync      synchronous precreation flag
191  *
192  * \retval              0 if successful
193  * \retval              negative value on error
194  */
195 int ofd_precreate_objects(const struct lu_env *env, struct ofd_device *ofd,
196                           u64 id, struct ofd_seq *oseq, int nr, int sync)
197 {
198         struct ofd_thread_info  *info = ofd_info(env);
199         struct ofd_object       *fo = NULL;
200         struct dt_object        *next;
201         struct thandle          *th;
202         struct ofd_object       **batch;
203         struct lu_fid           *fid = &info->fti_fid;
204         u64                     tmp;
205         int                     rc;
206         int                     rc2;
207         int                     i;
208         int                     objects = 0;
209         int                     nr_saved = nr;
210
211         ENTRY;
212
213         /* Don't create objects beyond the valid range for this SEQ */
214         if (unlikely(fid_seq_is_mdt0(ostid_seq(&oseq->os_oi)) &&
215                      (id + nr) >= IDIF_MAX_OID)) {
216                 CERROR("%s:"DOSTID" hit the IDIF_MAX_OID (1<<48)!\n",
217                        ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
218                 RETURN(rc = -ENOSPC);
219         } else if (unlikely(!fid_seq_is_mdt0(ostid_seq(&oseq->os_oi)) &&
220                             (id + nr) >= OBIF_MAX_OID)) {
221                 CERROR("%s:"DOSTID" hit the OBIF_MAX_OID (1<<32)!\n",
222                        ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
223                 RETURN(rc = -ENOSPC);
224         }
225
226         OBD_ALLOC(batch, nr_saved * sizeof(struct ofd_object *));
227         if (batch == NULL)
228                 RETURN(-ENOMEM);
229
230         info->fti_attr.la_valid = LA_TYPE | LA_MODE;
231         info->fti_attr.la_mode = S_IFREG | S_ISUID | S_ISGID | 0666;
232         info->fti_dof.dof_type = dt_mode_to_dft(S_IFREG);
233
234         info->fti_attr.la_valid |= LA_ATIME | LA_MTIME | LA_CTIME;
235         info->fti_attr.la_atime = 0;
236         info->fti_attr.la_mtime = 0;
237         info->fti_attr.la_ctime = 0;
238
239         LASSERT(id != 0);
240
241         /* prepare objects */
242         *fid = *lu_object_fid(&oseq->os_lastid_obj->do_lu);
243         for (i = 0; i < nr; i++) {
244                 rc = fid_set_id(fid, id + i);
245                 if (rc != 0) {
246                         if (i == 0)
247                                 GOTO(out, rc);
248
249                         nr = i;
250                         break;
251                 }
252
253                 fo = ofd_object_find(env, ofd, fid);
254                 if (IS_ERR(fo)) {
255                         if (i == 0)
256                                 GOTO(out, rc = PTR_ERR(fo));
257
258                         nr = i;
259                         break;
260                 }
261
262                 ofd_write_lock(env, fo);
263                 batch[i] = fo;
264         }
265         info->fti_buf.lb_buf = &tmp;
266         info->fti_buf.lb_len = sizeof(tmp);
267         info->fti_off = 0;
268
269         th = ofd_trans_create(env, ofd);
270         if (IS_ERR(th))
271                 GOTO(out, rc = PTR_ERR(th));
272
273         th->th_sync |= sync;
274
275         rc = dt_declare_record_write(env, oseq->os_lastid_obj, &info->fti_buf,
276                                      info->fti_off, th);
277         if (rc)
278                 GOTO(trans_stop, rc);
279
280         for (i = 0; i < nr; i++) {
281                 fo = batch[i];
282                 LASSERT(fo);
283
284                 if (unlikely(ofd_object_exists(fo))) {
285                         /* object may exist being re-created by write replay */
286                         CDEBUG(D_INODE, "object %#llx/%#llx exists: "
287                                DFID"\n", ostid_seq(&oseq->os_oi), id,
288                                PFID(lu_object_fid(&fo->ofo_obj.do_lu)));
289                         continue;
290                 }
291
292                 next = ofd_object_child(fo);
293                 LASSERT(next != NULL);
294
295                 rc = dt_declare_create(env, next, &info->fti_attr, NULL,
296                                        &info->fti_dof, th);
297                 if (rc < 0) {
298                         if (i == 0)
299                                 GOTO(trans_stop, rc);
300
301                         nr = i;
302                         break;
303                 }
304         }
305
306         rc = dt_trans_start_local(env, ofd->ofd_osd, th);
307         if (rc)
308                 GOTO(trans_stop, rc);
309
310         CDEBUG(D_OTHER, "%s: create new object "DFID" nr %d\n",
311                ofd_name(ofd), PFID(fid), nr);
312
313          /* When the LFSCK scanning the whole device to verify the LAST_ID file
314           * consistency, it will load the last_id into RAM firstly, and compare
315           * the last_id with each OST-object's ID. If the later one is larger,
316           * then it will regard the LAST_ID file crashed. But during the LFSCK
317           * scanning, the OFD may continue to create new OST-objects. Those new
318           * created OST-objects will have larger IDs than the LFSCK known ones.
319           * So from the LFSCK view, it needs to re-load the last_id from disk
320           * file, and if the latest last_id is still smaller than the object's
321           * ID, then the LAST_ID file is real crashed.
322           *
323           * To make above mechanism to work, before OFD pre-create OST-objects,
324           * it needs to update the LAST_ID file firstly, otherwise, the LFSCK
325           * may cannot get latest last_id although new OST-object created. */
326         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_SKIP_LASTID)) {
327                 tmp = cpu_to_le64(id + nr - 1);
328                 dt_write_lock(env, oseq->os_lastid_obj, 0);
329                 rc = dt_record_write(env, oseq->os_lastid_obj,
330                                      &info->fti_buf, &info->fti_off, th);
331                 dt_write_unlock(env, oseq->os_lastid_obj);
332                 if (rc != 0)
333                         GOTO(trans_stop, rc);
334         }
335
336         for (i = 0; i < nr; i++) {
337                 fo = batch[i];
338                 LASSERT(fo);
339
340                 /* Only the new created objects need to be recorded. */
341                 if (ofd->ofd_osd->dd_record_fid_accessed) {
342                         struct lfsck_request *lr = &ofd_info(env)->fti_lr;
343
344                         lfsck_pack_rfa(lr, lu_object_fid(&fo->ofo_obj.do_lu),
345                                        LE_FID_ACCESSED,
346                                        LFSCK_TYPE_LAYOUT);
347                         lfsck_in_notify(env, ofd->ofd_osd, lr, NULL);
348                 }
349
350                 if (likely(!ofd_object_exists(fo) &&
351                            !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING))) {
352                         next = ofd_object_child(fo);
353                         LASSERT(next != NULL);
354
355                         rc = dt_create(env, next, &info->fti_attr, NULL,
356                                        &info->fti_dof, th);
357                         if (rc < 0) {
358                                 if (i == 0)
359                                         GOTO(trans_stop, rc);
360
361                                 rc = 0;
362                                 break;
363                         }
364                         LASSERT(ofd_object_exists(fo));
365                 }
366                 ofd_seq_last_oid_set(oseq, id + i);
367         }
368
369         objects = i;
370         /* NOT all the wanted objects have been created,
371          * set the LAST_ID as the real created. */
372         if (unlikely(objects < nr)) {
373                 int rc1;
374
375                 info->fti_off = 0;
376                 tmp = cpu_to_le64(ofd_seq_last_oid(oseq));
377                 dt_write_lock(env, oseq->os_lastid_obj, 0);
378                 rc1 = dt_record_write(env, oseq->os_lastid_obj,
379                                       &info->fti_buf, &info->fti_off, th);
380                 dt_write_unlock(env, oseq->os_lastid_obj);
381                 if (rc1 != 0)
382                         CERROR("%s: fail to reset the LAST_ID for seq (%#llx"
383                                ") from %llu to %llu\n", ofd_name(ofd),
384                                ostid_seq(&oseq->os_oi), id + nr - 1,
385                                ofd_seq_last_oid(oseq));
386         }
387
388 trans_stop:
389         rc2 = ofd_trans_stop(env, ofd, th, rc);
390         if (rc2)
391                 CERROR("%s: failed to stop transaction: rc = %d\n",
392                        ofd_name(ofd), rc2);
393         if (!rc)
394                 rc = rc2;
395 out:
396         for (i = 0; i < nr_saved; i++) {
397                 fo = batch[i];
398                 if (fo) {
399                         ofd_write_unlock(env, fo);
400                         ofd_object_put(env, fo);
401                 }
402         }
403         OBD_FREE(batch, nr_saved * sizeof(struct ofd_object *));
404
405         CDEBUG((objects == 0 && rc == 0) ? D_ERROR : D_OTHER,
406                "created %d/%d objects: %d\n", objects, nr_saved, rc);
407
408         LASSERT(ergo(objects == 0, rc < 0));
409         RETURN(objects > 0 ? objects : rc);
410 }
411
412 /**
413  * Fix the OFD object ownership.
414  *
415  * If the object still has SUID+SGID bits set, meaning that it was precreated
416  * by the MDT before it was assigned to any file, (see ofd_precreate_objects())
417  * then we will accept the UID+GID if sent by the client for initializing the
418  * ownership of this object.  We only allow this to happen once (so clear these
419  * bits) and later only allow setattr.
420  *
421  * \param[in] env        execution environment
422  * \param[in] fo         OFD object
423  * \param[in] la         object attributes
424  * \param[in] is_setattr was this function called from setattr or not
425  *
426  * \retval              0 if successful
427  * \retval              negative value on error
428  */
429 int ofd_attr_handle_ugid(const struct lu_env *env, struct ofd_object *fo,
430                          struct lu_attr *la, int is_setattr)
431 {
432         struct ofd_thread_info  *info = ofd_info(env);
433         struct lu_attr          *ln = &info->fti_attr2;
434         __u32                    mask = 0;
435         int                      rc;
436
437         ENTRY;
438
439         if (!(la->la_valid & LA_UID) && !(la->la_valid & LA_GID))
440                 RETURN(0);
441
442         rc = dt_attr_get(env, ofd_object_child(fo), ln);
443         if (rc != 0)
444                 RETURN(rc);
445
446         LASSERT(ln->la_valid & LA_MODE);
447
448         if (!is_setattr) {
449                 if (!(ln->la_mode & S_ISUID))
450                         la->la_valid &= ~LA_UID;
451                 if (!(ln->la_mode & S_ISGID))
452                         la->la_valid &= ~LA_GID;
453         }
454
455         if ((la->la_valid & LA_UID) && (ln->la_mode & S_ISUID))
456                 mask |= S_ISUID;
457         if ((la->la_valid & LA_GID) && (ln->la_mode & S_ISGID))
458                 mask |= S_ISGID;
459         if (mask != 0) {
460                 if (!(la->la_valid & LA_MODE) || !is_setattr) {
461                         la->la_mode = ln->la_mode;
462                         la->la_valid |= LA_MODE;
463                 }
464                 la->la_mode &= ~mask;
465         }
466
467         RETURN(0);
468 }
469
470 /**
471  * Set OFD object attributes.
472  *
473  * This function sets OFD object attributes taken from incoming request.
474  * It sets not only regular attributes but also XATTR_NAME_FID extended
475  * attribute if needed. The "fid" xattr allows the object's MDT parent inode
476  * to be found and verified by LFSCK and other tools in case of inconsistency.
477  *
478  * \param[in] env       execution environment
479  * \param[in] fo        OFD object
480  * \param[in] la        object attributes
481  * \param[in] ff        filter_fid structure, contains additional attributes
482  *
483  * \retval              0 if successful
484  * \retval              negative value on error
485  */
486 int ofd_attr_set(const struct lu_env *env, struct ofd_object *fo,
487                  struct lu_attr *la, struct filter_fid *ff)
488 {
489         struct ofd_thread_info  *info = ofd_info(env);
490         struct ofd_device       *ofd = ofd_obj2dev(fo);
491         struct thandle          *th;
492         struct ofd_mod_data     *fmd;
493         int                     ff_needed = 0;
494         int                     rc;
495         int                     rc2;
496         ENTRY;
497
498         ofd_write_lock(env, fo);
499         if (!ofd_object_exists(fo))
500                 GOTO(unlock, rc = -ENOENT);
501
502         if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME)) {
503                 fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
504                 if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
505                         fmd->fmd_mactime_xid = info->fti_xid;
506                 ofd_fmd_put(info->fti_exp, fmd);
507         }
508
509         /* VBR: version recovery check */
510         rc = ofd_version_get_check(info, fo);
511         if (rc)
512                 GOTO(unlock, rc);
513
514         rc = ofd_attr_handle_ugid(env, fo, la, 1 /* is_setattr */);
515         if (rc != 0)
516                 GOTO(unlock, rc);
517
518         if (ff != NULL) {
519                 rc = ofd_object_ff_load(env, fo);
520                 if (rc == -ENODATA)
521                         ff_needed = 1;
522                 else if (rc < 0)
523                         GOTO(unlock, rc);
524         }
525
526         th = ofd_trans_create(env, ofd);
527         if (IS_ERR(th))
528                 GOTO(unlock, rc = PTR_ERR(th));
529
530         rc = dt_declare_attr_set(env, ofd_object_child(fo), la, th);
531         if (rc)
532                 GOTO(stop, rc);
533
534         if (ff_needed) {
535                 info->fti_buf.lb_buf = ff;
536                 info->fti_buf.lb_len = sizeof(*ff);
537                 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
538                                           &info->fti_buf, XATTR_NAME_FID, 0,
539                                           th);
540                 if (rc)
541                         GOTO(stop, rc);
542         }
543
544         rc = ofd_trans_start(env, ofd, la->la_valid & LA_SIZE ? fo : NULL, th);
545         if (rc)
546                 GOTO(stop, rc);
547
548         rc = dt_attr_set(env, ofd_object_child(fo), la, th);
549         if (rc)
550                 GOTO(stop, rc);
551
552         if (ff_needed) {
553                 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
554                                   XATTR_NAME_FID, 0, th);
555                 if (rc == 0) {
556                         fo->ofo_pfid.f_seq = le64_to_cpu(ff->ff_parent.f_seq);
557                         fo->ofo_pfid.f_oid = le32_to_cpu(ff->ff_parent.f_oid);
558                         /* Currently, the filter_fid::ff_parent::f_ver is not
559                          * the real parent MDT-object's FID::f_ver, instead it
560                          * is the OST-object index in its parent MDT-object's
561                          * layout EA. */
562                         fo->ofo_pfid.f_stripe_idx =
563                                         le32_to_cpu(ff->ff_parent.f_stripe_idx);
564                 }
565         }
566
567         GOTO(stop, rc);
568
569 stop:
570         rc2 = ofd_trans_stop(env, ofd, th, rc);
571         if (rc2)
572                 CERROR("%s: failed to stop transaction: rc = %d\n",
573                        ofd_name(ofd), rc2);
574         if (!rc)
575                 rc = rc2;
576
577 unlock:
578         ofd_write_unlock(env, fo);
579
580         return rc;
581 }
582
583 /**
584  * Truncate/punch OFD object.
585  *
586  * This function frees all of the allocated object's space from the \a start
587  * offset to the \a end offset. For truncate() operations the \a end offset
588  * is OBD_OBJECT_EOF. The functionality to punch holes in an object via
589  * fallocate(FALLOC_FL_PUNCH_HOLE) is not yet implemented (see LU-3606).
590  *
591  * \param[in] env       execution environment
592  * \param[in] fo        OFD object
593  * \param[in] start     start offset to punch from
594  * \param[in] end       end of punch
595  * \param[in] la        object attributes
596  * \param[in] ff        filter_fid structure
597  * \param[in] oa        obdo struct from incoming request
598  *
599  * \retval              0 if successful
600  * \retval              negative value on error
601  */
602 int ofd_object_punch(const struct lu_env *env, struct ofd_object *fo,
603                      __u64 start, __u64 end, struct lu_attr *la,
604                      struct filter_fid *ff, struct obdo *oa)
605 {
606         struct ofd_thread_info  *info = ofd_info(env);
607         struct ofd_device       *ofd = ofd_obj2dev(fo);
608         struct ofd_mod_data     *fmd;
609         struct dt_object        *dob = ofd_object_child(fo);
610         struct thandle          *th;
611         int                     ff_needed = 0;
612         int                     rc;
613         int                     rc2;
614
615         ENTRY;
616
617         /* we support truncate, not punch yet */
618         LASSERT(end == OBD_OBJECT_EOF);
619
620         ofd_write_lock(env, fo);
621         fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
622         if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
623                 fmd->fmd_mactime_xid = info->fti_xid;
624         ofd_fmd_put(info->fti_exp, fmd);
625
626         if (!ofd_object_exists(fo))
627                 GOTO(unlock, rc = -ENOENT);
628
629         if (ofd->ofd_lfsck_verify_pfid && oa->o_valid & OBD_MD_FLFID) {
630                 rc = ofd_verify_ff(env, fo, oa);
631                 if (rc != 0)
632                         GOTO(unlock, rc);
633         }
634
635         /* VBR: version recovery check */
636         rc = ofd_version_get_check(info, fo);
637         if (rc)
638                 GOTO(unlock, rc);
639
640         rc = ofd_attr_handle_ugid(env, fo, la, 0 /* !is_setattr */);
641         if (rc != 0)
642                 GOTO(unlock, rc);
643
644         if (ff != NULL) {
645                 rc = ofd_object_ff_load(env, fo);
646                 if (rc == -ENODATA)
647                         ff_needed = 1;
648                 else if (rc < 0)
649                         GOTO(unlock, rc);
650         }
651
652         th = ofd_trans_create(env, ofd);
653         if (IS_ERR(th))
654                 GOTO(unlock, rc = PTR_ERR(th));
655
656         rc = dt_declare_attr_set(env, dob, la, th);
657         if (rc)
658                 GOTO(stop, rc);
659
660         rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
661         if (rc)
662                 GOTO(stop, rc);
663
664         if (ff_needed) {
665                 info->fti_buf.lb_buf = ff;
666                 info->fti_buf.lb_len = sizeof(*ff);
667                 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
668                                           &info->fti_buf, XATTR_NAME_FID, 0,
669                                           th);
670                 if (rc)
671                         GOTO(stop, rc);
672         }
673
674         rc = ofd_trans_start(env, ofd, fo, th);
675         if (rc)
676                 GOTO(stop, rc);
677
678         rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th);
679         if (rc)
680                 GOTO(stop, rc);
681
682         rc = dt_attr_set(env, dob, la, th);
683         if (rc)
684                 GOTO(stop, rc);
685
686         if (ff_needed) {
687                 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
688                                   XATTR_NAME_FID, 0, th);
689                 if (rc == 0) {
690                         fo->ofo_pfid.f_seq = le64_to_cpu(ff->ff_parent.f_seq);
691                         fo->ofo_pfid.f_oid = le32_to_cpu(ff->ff_parent.f_oid);
692                         /* Currently, the filter_fid::ff_parent::f_ver is not
693                          * the real parent MDT-object's FID::f_ver, instead it
694                          * is the OST-object index in its parent MDT-object's
695                          * layout EA. */
696                         fo->ofo_pfid.f_stripe_idx =
697                                         le32_to_cpu(ff->ff_parent.f_stripe_idx);
698                 }
699         }
700
701         GOTO(stop, rc);
702
703 stop:
704         rc2 = ofd_trans_stop(env, ofd, th, rc);
705         if (rc2 != 0)
706                 CERROR("%s: failed to stop transaction: rc = %d\n",
707                        ofd_name(ofd), rc2);
708         if (!rc)
709                 rc = rc2;
710 unlock:
711         ofd_write_unlock(env, fo);
712
713         return rc;
714 }
715
716 /**
717  * Destroy OFD object.
718  *
719  * This function destroys OFD object. If object wasn't used at all (orphan)
720  * then local transaction is used, which means the transaction data is not
721  * returned back in reply.
722  *
723  * \param[in] env       execution environment
724  * \param[in] fo        OFD object
725  * \param[in] orphan    flag to indicate that object is orphaned
726  *
727  * \retval              0 if successful
728  * \retval              negative value on error
729  */
730 int ofd_object_destroy(const struct lu_env *env, struct ofd_object *fo,
731                        int orphan)
732 {
733         struct ofd_device       *ofd = ofd_obj2dev(fo);
734         struct thandle          *th;
735         int                     rc = 0;
736         int                     rc2;
737
738         ENTRY;
739
740         ofd_write_lock(env, fo);
741         if (!ofd_object_exists(fo))
742                 GOTO(unlock, rc = -ENOENT);
743
744         th = ofd_trans_create(env, ofd);
745         if (IS_ERR(th))
746                 GOTO(unlock, rc = PTR_ERR(th));
747
748         rc = dt_declare_ref_del(env, ofd_object_child(fo), th);
749         if (rc < 0)
750                 GOTO(stop, rc);
751
752         rc = dt_declare_destroy(env, ofd_object_child(fo), th);
753         if (rc < 0)
754                 GOTO(stop, rc);
755
756         if (orphan)
757                 rc = dt_trans_start_local(env, ofd->ofd_osd, th);
758         else
759                 rc = ofd_trans_start(env, ofd, NULL, th);
760         if (rc)
761                 GOTO(stop, rc);
762
763         ofd_fmd_drop(ofd_info(env)->fti_exp, &fo->ofo_header.loh_fid);
764
765         dt_ref_del(env, ofd_object_child(fo), th);
766         dt_destroy(env, ofd_object_child(fo), th);
767 stop:
768         rc2 = ofd_trans_stop(env, ofd, th, rc);
769         if (rc2)
770                 CERROR("%s failed to stop transaction: %d\n",
771                        ofd_name(ofd), rc2);
772         if (!rc)
773                 rc = rc2;
774 unlock:
775         ofd_write_unlock(env, fo);
776         RETURN(rc);
777 }
778
779 /**
780  * Get OFD object attributes.
781  *
782  * This function gets OFD object regular attributes. It is used to serve
783  * incoming request as well as for local OFD purposes.
784  *
785  * \param[in] env       execution environment
786  * \param[in] fo        OFD object
787  * \param[in] la        object attributes
788  *
789  * \retval              0 if successful
790  * \retval              negative value on error
791  */
792 int ofd_attr_get(const struct lu_env *env, struct ofd_object *fo,
793                  struct lu_attr *la)
794 {
795         int rc = 0;
796
797         ENTRY;
798
799         if (ofd_object_exists(fo)) {
800                 rc = dt_attr_get(env, ofd_object_child(fo), la);
801         } else {
802                 rc = -ENOENT;
803         }
804         RETURN(rc);
805 }