Whamcloud - gitweb
32f769492613ccc8be8a94a37983d8301c16535c
[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 "LPX64" != "LPX64"\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  * Put OFD object reference.
170  *
171  * \param[in] env       execution environment
172  * \param[in] fo        OFD object
173  */
174 void ofd_object_put(const struct lu_env *env, struct ofd_object *fo)
175 {
176         lu_object_put(env, &fo->ofo_obj.do_lu);
177 }
178
179 /**
180  * Precreate the given number \a nr of objects in the given sequence \a oseq.
181  *
182  * This function precreates new OST objects in the given sequence.
183  * The precreation starts from \a id and creates \a nr objects sequentially.
184  *
185  * Notes:
186  * This function may create fewer objects than requested.
187  *
188  * We mark object SUID+SGID to flag it for accepting UID+GID from client on
189  * first write. Currently the permission bits on the OST are never used,
190  * so this is OK.
191  *
192  * Initialize a/c/m time so any client timestamp will always be newer and
193  * update the inode. The ctime = 0 case is also handled specially in
194  * osd_inode_setattr(). See LU-221, LU-1042 for details.
195  *
196  * \param[in] env       execution environment
197  * \param[in] ofd       OFD device
198  * \param[in] id        object ID to start precreation from
199  * \param[in] oseq      object sequence
200  * \param[in] nr        number of objects to precreate
201  * \param[in] sync      synchronous precreation flag
202  *
203  * \retval              0 if successful
204  * \retval              negative value on error
205  */
206 int ofd_precreate_objects(const struct lu_env *env, struct ofd_device *ofd,
207                           u64 id, struct ofd_seq *oseq, int nr, int sync)
208 {
209         struct ofd_thread_info  *info = ofd_info(env);
210         struct ofd_object       *fo = NULL;
211         struct dt_object        *next;
212         struct thandle          *th;
213         struct ofd_object       **batch;
214         struct lu_fid           *fid = &info->fti_fid;
215         u64                      tmp;
216         int                      rc;
217         int                      i;
218         int                      objects = 0;
219         int                      nr_saved = nr;
220
221         ENTRY;
222
223         /* Don't create objects beyond the valid range for this SEQ */
224         if (unlikely(fid_seq_is_mdt0(ostid_seq(&oseq->os_oi)) &&
225                      (id + nr) >= IDIF_MAX_OID)) {
226                 CERROR("%s:"DOSTID" hit the IDIF_MAX_OID (1<<48)!\n",
227                        ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
228                 RETURN(rc = -ENOSPC);
229         } else if (unlikely(!fid_seq_is_mdt0(ostid_seq(&oseq->os_oi)) &&
230                             (id + nr) >= OBIF_MAX_OID)) {
231                 CERROR("%s:"DOSTID" hit the OBIF_MAX_OID (1<<32)!\n",
232                        ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
233                 RETURN(rc = -ENOSPC);
234         }
235
236         OBD_ALLOC(batch, nr_saved * sizeof(struct ofd_object *));
237         if (batch == NULL)
238                 RETURN(-ENOMEM);
239
240         info->fti_attr.la_valid = LA_TYPE | LA_MODE;
241         info->fti_attr.la_mode = S_IFREG | S_ISUID | S_ISGID | 0666;
242         info->fti_dof.dof_type = dt_mode_to_dft(S_IFREG);
243
244         info->fti_attr.la_valid |= LA_ATIME | LA_MTIME | LA_CTIME;
245         info->fti_attr.la_atime = 0;
246         info->fti_attr.la_mtime = 0;
247         info->fti_attr.la_ctime = 0;
248
249         LASSERT(id != 0);
250
251         /* prepare objects */
252         *fid = *lu_object_fid(&oseq->os_lastid_obj->do_lu);
253         for (i = 0; i < nr; i++) {
254                 rc = fid_set_id(fid, id + i);
255                 if (rc != 0) {
256                         if (i == 0)
257                                 GOTO(out, rc);
258
259                         nr = i;
260                         break;
261                 }
262
263                 fo = ofd_object_find(env, ofd, fid);
264                 if (IS_ERR(fo)) {
265                         if (i == 0)
266                                 GOTO(out, rc = PTR_ERR(fo));
267
268                         nr = i;
269                         break;
270                 }
271
272                 ofd_write_lock(env, fo);
273                 batch[i] = fo;
274         }
275         info->fti_buf.lb_buf = &tmp;
276         info->fti_buf.lb_len = sizeof(tmp);
277         info->fti_off = 0;
278
279         th = ofd_trans_create(env, ofd);
280         if (IS_ERR(th))
281                 GOTO(out, rc = PTR_ERR(th));
282
283         th->th_sync |= sync;
284
285         rc = dt_declare_record_write(env, oseq->os_lastid_obj, &info->fti_buf,
286                                      info->fti_off, th);
287         if (rc)
288                 GOTO(trans_stop, rc);
289
290         for (i = 0; i < nr; i++) {
291                 fo = batch[i];
292                 LASSERT(fo);
293
294                 if (unlikely(ofd_object_exists(fo))) {
295                         /* object may exist being re-created by write replay */
296                         CDEBUG(D_INODE, "object "LPX64"/"LPX64" exists: "
297                                DFID"\n", ostid_seq(&oseq->os_oi), id,
298                                PFID(lu_object_fid(&fo->ofo_obj.do_lu)));
299                         continue;
300                 }
301
302                 next = ofd_object_child(fo);
303                 LASSERT(next != NULL);
304
305                 rc = dt_declare_create(env, next, &info->fti_attr, NULL,
306                                        &info->fti_dof, th);
307                 if (rc < 0) {
308                         if (i == 0)
309                                 GOTO(trans_stop, rc);
310
311                         nr = i;
312                         break;
313                 }
314         }
315
316         rc = dt_trans_start_local(env, ofd->ofd_osd, th);
317         if (rc)
318                 GOTO(trans_stop, rc);
319
320         CDEBUG(D_OTHER, "%s: create new object "DFID" nr %d\n",
321                ofd_name(ofd), PFID(fid), nr);
322
323          /* When the LFSCK scanning the whole device to verify the LAST_ID file
324           * consistency, it will load the last_id into RAM firstly, and compare
325           * the last_id with each OST-object's ID. If the later one is larger,
326           * then it will regard the LAST_ID file crashed. But during the LFSCK
327           * scanning, the OFD may continue to create new OST-objects. Those new
328           * created OST-objects will have larger IDs than the LFSCK known ones.
329           * So from the LFSCK view, it needs to re-load the last_id from disk
330           * file, and if the latest last_id is still smaller than the object's
331           * ID, then the LAST_ID file is real crashed.
332           *
333           * To make above mechanism to work, before OFD pre-create OST-objects,
334           * it needs to update the LAST_ID file firstly, otherwise, the LFSCK
335           * may cannot get latest last_id although new OST-object created. */
336         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_SKIP_LASTID)) {
337                 tmp = cpu_to_le64(id + nr - 1);
338                 dt_write_lock(env, oseq->os_lastid_obj, 0);
339                 rc = dt_record_write(env, oseq->os_lastid_obj,
340                                      &info->fti_buf, &info->fti_off, th);
341                 dt_write_unlock(env, oseq->os_lastid_obj);
342                 if (rc != 0)
343                         GOTO(trans_stop, rc);
344         }
345
346         for (i = 0; i < nr; i++) {
347                 fo = batch[i];
348                 LASSERT(fo);
349
350                 /* Only the new created objects need to be recorded. */
351                 if (ofd->ofd_osd->dd_record_fid_accessed) {
352                         struct lfsck_request *lr = &ofd_info(env)->fti_lr;
353
354                         lfsck_pack_rfa(lr, lu_object_fid(&fo->ofo_obj.do_lu),
355                                        LE_FID_ACCESSED,
356                                        LFSCK_TYPE_LAYOUT);
357                         lfsck_in_notify(env, ofd->ofd_osd, lr, NULL);
358                 }
359
360                 if (likely(!ofd_object_exists(fo) &&
361                            !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING))) {
362                         next = ofd_object_child(fo);
363                         LASSERT(next != NULL);
364
365                         rc = dt_create(env, next, &info->fti_attr, NULL,
366                                        &info->fti_dof, th);
367                         if (rc < 0) {
368                                 if (i == 0)
369                                         GOTO(trans_stop, rc);
370
371                                 rc = 0;
372                                 break;
373                         }
374                         LASSERT(ofd_object_exists(fo));
375                 }
376                 ofd_seq_last_oid_set(oseq, id + i);
377         }
378
379         objects = i;
380         /* NOT all the wanted objects have been created,
381          * set the LAST_ID as the real created. */
382         if (unlikely(objects < nr)) {
383                 int rc1;
384
385                 info->fti_off = 0;
386                 tmp = cpu_to_le64(ofd_seq_last_oid(oseq));
387                 dt_write_lock(env, oseq->os_lastid_obj, 0);
388                 rc1 = dt_record_write(env, oseq->os_lastid_obj,
389                                       &info->fti_buf, &info->fti_off, th);
390                 dt_write_unlock(env, oseq->os_lastid_obj);
391                 if (rc1 != 0)
392                         CERROR("%s: fail to reset the LAST_ID for seq ("LPX64
393                                ") from "LPU64" to "LPU64"\n", ofd_name(ofd),
394                                ostid_seq(&oseq->os_oi), id + nr - 1,
395                                ofd_seq_last_oid(oseq));
396         }
397
398 trans_stop:
399         ofd_trans_stop(env, ofd, th, rc);
400 out:
401         for (i = 0; i < nr_saved; i++) {
402                 fo = batch[i];
403                 if (fo) {
404                         ofd_write_unlock(env, fo);
405                         ofd_object_put(env, fo);
406                 }
407         }
408         OBD_FREE(batch, nr_saved * sizeof(struct ofd_object *));
409
410         CDEBUG((objects == 0 && rc == 0) ? D_ERROR : D_OTHER,
411                "created %d/%d objects: %d\n", objects, nr_saved, rc);
412
413         LASSERT(ergo(objects == 0, rc < 0));
414         RETURN(objects > 0 ? objects : rc);
415 }
416
417 /**
418  * Fix the OFD object ownership.
419  *
420  * If the object still has SUID+SGID bits set, meaning that it was precreated
421  * by the MDT before it was assigned to any file, (see ofd_precreate_objects())
422  * then we will accept the UID+GID if sent by the client for initializing the
423  * ownership of this object.  We only allow this to happen once (so clear these
424  * bits) and later only allow setattr.
425  *
426  * \param[in] env        execution environment
427  * \param[in] fo         OFD object
428  * \param[in] la         object attributes
429  * \param[in] is_setattr was this function called from setattr or not
430  *
431  * \retval              0 if successful
432  * \retval              negative value on error
433  */
434 int ofd_attr_handle_ugid(const struct lu_env *env, struct ofd_object *fo,
435                          struct lu_attr *la, int is_setattr)
436 {
437         struct ofd_thread_info  *info = ofd_info(env);
438         struct lu_attr          *ln = &info->fti_attr2;
439         __u32                    mask = 0;
440         int                      rc;
441
442         ENTRY;
443
444         if (!(la->la_valid & LA_UID) && !(la->la_valid & LA_GID))
445                 RETURN(0);
446
447         rc = dt_attr_get(env, ofd_object_child(fo), ln);
448         if (rc != 0)
449                 RETURN(rc);
450
451         LASSERT(ln->la_valid & LA_MODE);
452
453         if (!is_setattr) {
454                 if (!(ln->la_mode & S_ISUID))
455                         la->la_valid &= ~LA_UID;
456                 if (!(ln->la_mode & S_ISGID))
457                         la->la_valid &= ~LA_GID;
458         }
459
460         if ((la->la_valid & LA_UID) && (ln->la_mode & S_ISUID))
461                 mask |= S_ISUID;
462         if ((la->la_valid & LA_GID) && (ln->la_mode & S_ISGID))
463                 mask |= S_ISGID;
464         if (mask != 0) {
465                 if (!(la->la_valid & LA_MODE) || !is_setattr) {
466                         la->la_mode = ln->la_mode;
467                         la->la_valid |= LA_MODE;
468                 }
469                 la->la_mode &= ~mask;
470         }
471
472         RETURN(0);
473 }
474
475 /**
476  * Set OFD object attributes.
477  *
478  * This function sets OFD object attributes taken from incoming request.
479  * It sets not only regular attributes but also XATTR_NAME_FID extended
480  * attribute if needed. The "fid" xattr allows the object's MDT parent inode
481  * to be found and verified by LFSCK and other tools in case of inconsistency.
482  *
483  * \param[in] env       execution environment
484  * \param[in] fo        OFD object
485  * \param[in] la        object attributes
486  * \param[in] ff        filter_fid structure, contains additional attributes
487  *
488  * \retval              0 if successful
489  * \retval              negative value on error
490  */
491 int ofd_attr_set(const struct lu_env *env, struct ofd_object *fo,
492                  struct lu_attr *la, struct filter_fid *ff)
493 {
494         struct ofd_thread_info  *info = ofd_info(env);
495         struct ofd_device       *ofd = ofd_obj2dev(fo);
496         struct thandle          *th;
497         struct ofd_mod_data     *fmd;
498         int                      ff_needed = 0;
499         int                      rc;
500         ENTRY;
501
502         ofd_write_lock(env, fo);
503         if (!ofd_object_exists(fo))
504                 GOTO(unlock, rc = -ENOENT);
505
506         if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME)) {
507                 fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
508                 if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
509                         fmd->fmd_mactime_xid = info->fti_xid;
510                 ofd_fmd_put(info->fti_exp, fmd);
511         }
512
513         /* VBR: version recovery check */
514         rc = ofd_version_get_check(info, fo);
515         if (rc)
516                 GOTO(unlock, rc);
517
518         rc = ofd_attr_handle_ugid(env, fo, la, 1 /* is_setattr */);
519         if (rc != 0)
520                 GOTO(unlock, rc);
521
522         if (ff != NULL) {
523                 rc = ofd_object_ff_load(env, fo);
524                 if (rc == -ENODATA)
525                         ff_needed = 1;
526                 else if (rc < 0)
527                         GOTO(unlock, rc);
528         }
529
530         th = ofd_trans_create(env, ofd);
531         if (IS_ERR(th))
532                 GOTO(unlock, rc = PTR_ERR(th));
533
534         rc = dt_declare_attr_set(env, ofd_object_child(fo), la, th);
535         if (rc)
536                 GOTO(stop, rc);
537
538         if (ff_needed) {
539                 info->fti_buf.lb_buf = ff;
540                 info->fti_buf.lb_len = sizeof(*ff);
541                 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
542                                           &info->fti_buf, XATTR_NAME_FID, 0,
543                                           th);
544                 if (rc)
545                         GOTO(stop, rc);
546         }
547
548         rc = ofd_trans_start(env, ofd, la->la_valid & LA_SIZE ? fo : NULL, th);
549         if (rc)
550                 GOTO(stop, rc);
551
552         rc = dt_attr_set(env, ofd_object_child(fo), la, th);
553         if (rc)
554                 GOTO(stop, rc);
555
556         if (ff_needed) {
557                 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
558                                   XATTR_NAME_FID, 0, th);
559                 if (rc == 0) {
560                         fo->ofo_pfid.f_seq = le64_to_cpu(ff->ff_parent.f_seq);
561                         fo->ofo_pfid.f_oid = le32_to_cpu(ff->ff_parent.f_oid);
562                         /* Currently, the filter_fid::ff_parent::f_ver is not
563                          * the real parent MDT-object's FID::f_ver, instead it
564                          * is the OST-object index in its parent MDT-object's
565                          * layout EA. */
566                         fo->ofo_pfid.f_stripe_idx =
567                                         le32_to_cpu(ff->ff_parent.f_stripe_idx);
568                 }
569         }
570
571         GOTO(stop, rc);
572
573 stop:
574         ofd_trans_stop(env, ofd, th, rc);
575 unlock:
576         ofd_write_unlock(env, fo);
577
578         return rc;
579 }
580
581 /**
582  * Truncate/punch OFD object.
583  *
584  * This function frees all of the allocated object's space from the \a start
585  * offset to the \a end offset. For truncate() operations the \a end offset
586  * is OBD_OBJECT_EOF. The functionality to punch holes in an object via
587  * fallocate(FALLOC_FL_PUNCH_HOLE) is not yet implemented (see LU-3606).
588  *
589  * \param[in] env       execution environment
590  * \param[in] fo        OFD object
591  * \param[in] start     start offset to punch from
592  * \param[in] end       end of punch
593  * \param[in] la        object attributes
594  * \param[in] ff        filter_fid structure
595  * \param[in] oa        obdo struct from incoming request
596  *
597  * \retval              0 if successful
598  * \retval              negative value on error
599  */
600 int ofd_object_punch(const struct lu_env *env, struct ofd_object *fo,
601                      __u64 start, __u64 end, struct lu_attr *la,
602                      struct filter_fid *ff, struct obdo *oa)
603 {
604         struct ofd_thread_info  *info = ofd_info(env);
605         struct ofd_device       *ofd = ofd_obj2dev(fo);
606         struct ofd_mod_data     *fmd;
607         struct dt_object        *dob = ofd_object_child(fo);
608         struct thandle          *th;
609         int                      ff_needed = 0;
610         int                      rc;
611
612         ENTRY;
613
614         /* we support truncate, not punch yet */
615         LASSERT(end == OBD_OBJECT_EOF);
616
617         ofd_write_lock(env, fo);
618         fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
619         if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
620                 fmd->fmd_mactime_xid = info->fti_xid;
621         ofd_fmd_put(info->fti_exp, fmd);
622
623         if (!ofd_object_exists(fo))
624                 GOTO(unlock, rc = -ENOENT);
625
626         if (ofd->ofd_lfsck_verify_pfid && oa->o_valid & OBD_MD_FLFID) {
627                 rc = ofd_verify_ff(env, fo, oa);
628                 if (rc != 0)
629                         GOTO(unlock, rc);
630         }
631
632         /* VBR: version recovery check */
633         rc = ofd_version_get_check(info, fo);
634         if (rc)
635                 GOTO(unlock, rc);
636
637         rc = ofd_attr_handle_ugid(env, fo, la, 0 /* !is_setattr */);
638         if (rc != 0)
639                 GOTO(unlock, rc);
640
641         if (ff != NULL) {
642                 rc = ofd_object_ff_load(env, fo);
643                 if (rc == -ENODATA)
644                         ff_needed = 1;
645                 else if (rc < 0)
646                         GOTO(unlock, rc);
647         }
648
649         th = ofd_trans_create(env, ofd);
650         if (IS_ERR(th))
651                 GOTO(unlock, rc = PTR_ERR(th));
652
653         rc = dt_declare_attr_set(env, dob, la, th);
654         if (rc)
655                 GOTO(stop, rc);
656
657         rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
658         if (rc)
659                 GOTO(stop, rc);
660
661         if (ff_needed) {
662                 info->fti_buf.lb_buf = ff;
663                 info->fti_buf.lb_len = sizeof(*ff);
664                 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
665                                           &info->fti_buf, XATTR_NAME_FID, 0,
666                                           th);
667                 if (rc)
668                         GOTO(stop, rc);
669         }
670
671         rc = ofd_trans_start(env, ofd, fo, th);
672         if (rc)
673                 GOTO(stop, rc);
674
675         rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th);
676         if (rc)
677                 GOTO(stop, rc);
678
679         rc = dt_attr_set(env, dob, la, th);
680         if (rc)
681                 GOTO(stop, rc);
682
683         if (ff_needed) {
684                 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
685                                   XATTR_NAME_FID, 0, th);
686                 if (rc == 0) {
687                         fo->ofo_pfid.f_seq = le64_to_cpu(ff->ff_parent.f_seq);
688                         fo->ofo_pfid.f_oid = le32_to_cpu(ff->ff_parent.f_oid);
689                         /* Currently, the filter_fid::ff_parent::f_ver is not
690                          * the real parent MDT-object's FID::f_ver, instead it
691                          * is the OST-object index in its parent MDT-object's
692                          * layout EA. */
693                         fo->ofo_pfid.f_stripe_idx =
694                                         le32_to_cpu(ff->ff_parent.f_stripe_idx);
695                 }
696         }
697
698         GOTO(stop, rc);
699
700 stop:
701         ofd_trans_stop(env, ofd, th, rc);
702 unlock:
703         ofd_write_unlock(env, fo);
704
705         return rc;
706 }
707
708 /**
709  * Destroy OFD object.
710  *
711  * This function destroys OFD object. If object wasn't used at all (orphan)
712  * then local transaction is used, which means the transaction data is not
713  * returned back in reply.
714  *
715  * \param[in] env       execution environment
716  * \param[in] fo        OFD object
717  * \param[in] orphan    flag to indicate that object is orphaned
718  *
719  * \retval              0 if successful
720  * \retval              negative value on error
721  */
722 int ofd_object_destroy(const struct lu_env *env, struct ofd_object *fo,
723                        int orphan)
724 {
725         struct ofd_device       *ofd = ofd_obj2dev(fo);
726         struct thandle          *th;
727         int                      rc = 0;
728
729         ENTRY;
730
731         ofd_write_lock(env, fo);
732         if (!ofd_object_exists(fo))
733                 GOTO(unlock, rc = -ENOENT);
734
735         th = ofd_trans_create(env, ofd);
736         if (IS_ERR(th))
737                 GOTO(unlock, rc = PTR_ERR(th));
738
739         rc = dt_declare_ref_del(env, ofd_object_child(fo), th);
740         if (rc < 0)
741                 GOTO(stop, rc);
742
743         rc = dt_declare_destroy(env, ofd_object_child(fo), th);
744         if (rc < 0)
745                 GOTO(stop, rc);
746
747         if (orphan)
748                 rc = dt_trans_start_local(env, ofd->ofd_osd, th);
749         else
750                 rc = ofd_trans_start(env, ofd, NULL, th);
751         if (rc)
752                 GOTO(stop, rc);
753
754         ofd_fmd_drop(ofd_info(env)->fti_exp, &fo->ofo_header.loh_fid);
755
756         dt_ref_del(env, ofd_object_child(fo), th);
757         dt_destroy(env, ofd_object_child(fo), th);
758 stop:
759         ofd_trans_stop(env, ofd, th, rc);
760 unlock:
761         ofd_write_unlock(env, fo);
762         RETURN(rc);
763 }
764
765 /**
766  * Get OFD object attributes.
767  *
768  * This function gets OFD object regular attributes. It is used to serve
769  * incoming request as well as for local OFD purposes.
770  *
771  * \param[in] env       execution environment
772  * \param[in] fo        OFD object
773  * \param[in] la        object attributes
774  *
775  * \retval              0 if successful
776  * \retval              negative value on error
777  */
778 int ofd_attr_get(const struct lu_env *env, struct ofd_object *fo,
779                  struct lu_attr *la)
780 {
781         int rc = 0;
782
783         ENTRY;
784
785         if (ofd_object_exists(fo)) {
786                 rc = dt_attr_get(env, ofd_object_child(fo), la);
787         } else {
788                 rc = -ENOENT;
789         }
790         RETURN(rc);
791 }