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