Whamcloud - gitweb
4db3f1a18f47547f9f35d43e70c9514e4aa28123
[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 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)
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                           BYPASS_CAPA);
153         if (rc < 0)
154                 return rc;
155
156         if (rc < sizeof(struct lu_fid)) {
157                 fid_zero(pfid);
158
159                 return -ENODATA;
160         }
161
162         pfid->f_seq = le64_to_cpu(ff->ff_parent.f_seq);
163         pfid->f_oid = le32_to_cpu(ff->ff_parent.f_oid);
164         pfid->f_stripe_idx = le32_to_cpu(ff->ff_parent.f_stripe_idx);
165
166         return 0;
167 }
168
169 /**
170  * Put OFD object reference.
171  *
172  * \param[in] env       execution environment
173  * \param[in] fo        OFD object
174  */
175 void ofd_object_put(const struct lu_env *env, struct ofd_object *fo)
176 {
177         lu_object_put(env, &fo->ofo_obj.do_lu);
178 }
179
180 /**
181  * Precreate the given number \a nr of objects in the given sequence \a oseq.
182  *
183  * This function precreates new OST objects in the given sequence.
184  * The precreation starts from \a id and creates \a nr objects sequentially.
185  *
186  * Notes:
187  * This function may create fewer objects than requested.
188  *
189  * We mark object SUID+SGID to flag it for accepting UID+GID from client on
190  * first write. Currently the permission bits on the OST are never used,
191  * so this is OK.
192  *
193  * Initialize a/c/m time so any client timestamp will always be newer and
194  * update the inode. The ctime = 0 case is also handled specially in
195  * osd_inode_setattr(). See LU-221, LU-1042 for details.
196  *
197  * \param[in] env       execution environment
198  * \param[in] ofd       OFD device
199  * \param[in] id        object ID to start precreation from
200  * \param[in] oseq      object sequence
201  * \param[in] nr        number of objects to precreate
202  * \param[in] sync      synchronous precreation flag
203  *
204  * \retval              0 if successful
205  * \retval              negative value on error
206  */
207 int ofd_precreate_objects(const struct lu_env *env, struct ofd_device *ofd,
208                           obd_id id, struct ofd_seq *oseq, int nr, int sync)
209 {
210         struct ofd_thread_info  *info = ofd_info(env);
211         struct ofd_object       *fo = NULL;
212         struct dt_object        *next;
213         struct thandle          *th;
214         struct ofd_object       **batch;
215         struct lu_fid           *fid = &info->fti_fid;
216         obd_id                   tmp;
217         int                      rc;
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 "LPX64"/"LPX64" 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) {
309                         nr = i;
310                         break;
311                 }
312         }
313
314         rc = dt_trans_start_local(env, ofd->ofd_osd, th);
315         if (rc)
316                 GOTO(trans_stop, rc);
317
318         CDEBUG(D_OTHER, "%s: create new object "DFID" nr %d\n",
319                ofd_name(ofd), PFID(fid), nr);
320
321         LASSERT(nr > 0);
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                         lfsck_pack_rfa(&ofd_info(env)->fti_lr,
353                                        lu_object_fid(&fo->ofo_obj.do_lu));
354                         lfsck_in_notify(env, ofd->ofd_osd,
355                                         &ofd_info(env)->fti_lr);
356                 }
357
358                 if (likely(!ofd_object_exists(fo) &&
359                            !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING))) {
360                         next = ofd_object_child(fo);
361                         LASSERT(next != NULL);
362
363                         rc = dt_create(env, next, &info->fti_attr, NULL,
364                                        &info->fti_dof, th);
365                         if (rc)
366                                 break;
367                         LASSERT(ofd_object_exists(fo));
368                 }
369                 ofd_seq_last_oid_set(oseq, id + i);
370         }
371
372         objects = i;
373         /* NOT all the wanted objects have been created,
374          * set the LAST_ID as the real created. */
375         if (unlikely(objects < nr)) {
376                 int rc1;
377
378                 info->fti_off = 0;
379                 tmp = cpu_to_le64(ofd_seq_last_oid(oseq));
380                 dt_write_lock(env, oseq->os_lastid_obj, 0);
381                 rc1 = dt_record_write(env, oseq->os_lastid_obj,
382                                       &info->fti_buf, &info->fti_off, th);
383                 dt_write_unlock(env, oseq->os_lastid_obj);
384                 if (rc1 != 0)
385                         CERROR("%s: fail to reset the LAST_ID for seq ("LPX64
386                                ") from "LPU64" to "LPU64"\n", ofd_name(ofd),
387                                ostid_seq(&oseq->os_oi), id + nr - 1,
388                                ofd_seq_last_oid(oseq));
389         }
390
391 trans_stop:
392         ofd_trans_stop(env, ofd, th, rc);
393 out:
394         for (i = 0; i < nr_saved; i++) {
395                 fo = batch[i];
396                 if (fo) {
397                         ofd_write_unlock(env, fo);
398                         ofd_object_put(env, fo);
399                 }
400         }
401         OBD_FREE(batch, nr_saved * sizeof(struct ofd_object *));
402
403         CDEBUG((objects == 0 && rc == 0) ? D_ERROR : D_OTHER,
404                "created %d/%d objects: %d\n", objects, nr_saved, rc);
405
406         LASSERT(ergo(objects == 0, rc < 0));
407         RETURN(objects > 0 ? objects : rc);
408 }
409
410 /**
411  * Fix the OFD object ownership.
412  *
413  * If the object still has SUID+SGID bits set, meaning that it was precreated
414  * by the MDT before it was assigned to any file, (see ofd_precreate_objects())
415  * then we will accept the UID+GID if sent by the client for initializing the
416  * ownership of this object.  We only allow this to happen once (so clear these
417  * bits) and later only allow setattr.
418  *
419  * \param[in] env        execution environment
420  * \param[in] fo         OFD object
421  * \param[in] la         object attributes
422  * \param[in] is_setattr was this function called from setattr or not
423  *
424  * \retval              0 if successful
425  * \retval              negative value on error
426  */
427 int ofd_attr_handle_ugid(const struct lu_env *env, struct ofd_object *fo,
428                          struct lu_attr *la, int is_setattr)
429 {
430         struct ofd_thread_info  *info = ofd_info(env);
431         struct lu_attr          *ln = &info->fti_attr2;
432         __u32                    mask = 0;
433         int                      rc;
434
435         ENTRY;
436
437         if (!(la->la_valid & LA_UID) && !(la->la_valid & LA_GID))
438                 RETURN(0);
439
440         rc = dt_attr_get(env, ofd_object_child(fo), ln, BYPASS_CAPA);
441         if (rc != 0)
442                 RETURN(rc);
443
444         LASSERT(ln->la_valid & LA_MODE);
445
446         if (!is_setattr) {
447                 if (!(ln->la_mode & S_ISUID))
448                         la->la_valid &= ~LA_UID;
449                 if (!(ln->la_mode & S_ISGID))
450                         la->la_valid &= ~LA_GID;
451         }
452
453         if ((la->la_valid & LA_UID) && (ln->la_mode & S_ISUID))
454                 mask |= S_ISUID;
455         if ((la->la_valid & LA_GID) && (ln->la_mode & S_ISGID))
456                 mask |= S_ISGID;
457         if (mask != 0) {
458                 if (!(la->la_valid & LA_MODE) || !is_setattr) {
459                         la->la_mode = ln->la_mode;
460                         la->la_valid |= LA_MODE;
461                 }
462                 la->la_mode &= ~mask;
463         }
464
465         RETURN(0);
466 }
467
468 /**
469  * Set OFD object attributes.
470  *
471  * This function sets OFD object attributes taken from incoming request.
472  * It sets not only regular attributes but also XATTR_NAME_FID extended
473  * attribute if needed. The "fid" xattr allows the object's MDT parent inode
474  * to be found and verified by LFSCK and other tools in case of inconsistency.
475  *
476  * \param[in] env       execution environment
477  * \param[in] fo        OFD object
478  * \param[in] la        object attributes
479  * \param[in] ff        filter_fid structure, contains additional attributes
480  *
481  * \retval              0 if successful
482  * \retval              negative value on error
483  */
484 int ofd_attr_set(const struct lu_env *env, struct ofd_object *fo,
485                  struct lu_attr *la, struct filter_fid *ff)
486 {
487         struct ofd_thread_info  *info = ofd_info(env);
488         struct ofd_device       *ofd = ofd_obj2dev(fo);
489         struct thandle          *th;
490         struct ofd_mod_data     *fmd;
491         int                      ff_needed = 0;
492         int                      rc;
493         ENTRY;
494
495         ofd_write_lock(env, fo);
496         if (!ofd_object_exists(fo))
497                 GOTO(unlock, rc = -ENOENT);
498
499         if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME)) {
500                 fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
501                 if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
502                         fmd->fmd_mactime_xid = info->fti_xid;
503                 ofd_fmd_put(info->fti_exp, fmd);
504         }
505
506         /* VBR: version recovery check */
507         rc = ofd_version_get_check(info, fo);
508         if (rc)
509                 GOTO(unlock, rc);
510
511         rc = ofd_attr_handle_ugid(env, fo, la, 1 /* is_setattr */);
512         if (rc != 0)
513                 GOTO(unlock, rc);
514
515         if (ff != NULL) {
516                 rc = ofd_object_ff_load(env, fo);
517                 if (rc == -ENODATA)
518                         ff_needed = 1;
519                 else if (rc < 0)
520                         GOTO(unlock, rc);
521         }
522
523         th = ofd_trans_create(env, ofd);
524         if (IS_ERR(th))
525                 GOTO(unlock, rc = PTR_ERR(th));
526
527         rc = dt_declare_attr_set(env, ofd_object_child(fo), la, th);
528         if (rc)
529                 GOTO(stop, rc);
530
531         if (ff_needed) {
532                 info->fti_buf.lb_buf = ff;
533                 info->fti_buf.lb_len = sizeof(*ff);
534                 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
535                                           &info->fti_buf, XATTR_NAME_FID, 0,
536                                           th);
537                 if (rc)
538                         GOTO(stop, rc);
539         }
540
541         rc = ofd_trans_start(env, ofd, la->la_valid & LA_SIZE ? fo : NULL, th);
542         if (rc)
543                 GOTO(stop, rc);
544
545         rc = dt_attr_set(env, ofd_object_child(fo), la, th,
546                          ofd_object_capa(env, fo));
547         if (rc)
548                 GOTO(stop, rc);
549
550         if (ff_needed) {
551                 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
552                                   XATTR_NAME_FID, 0, th, BYPASS_CAPA);
553                 if (rc == 0) {
554                         fo->ofo_pfid.f_seq = le64_to_cpu(ff->ff_parent.f_seq);
555                         fo->ofo_pfid.f_oid = le32_to_cpu(ff->ff_parent.f_oid);
556                         /* Currently, the filter_fid::ff_parent::f_ver is not
557                          * the real parent MDT-object's FID::f_ver, instead it
558                          * is the OST-object index in its parent MDT-object's
559                          * layout EA. */
560                         fo->ofo_pfid.f_stripe_idx =
561                                         le32_to_cpu(ff->ff_parent.f_stripe_idx);
562                 }
563         }
564
565         GOTO(stop, rc);
566
567 stop:
568         ofd_trans_stop(env, ofd, th, rc);
569 unlock:
570         ofd_write_unlock(env, fo);
571
572         return rc;
573 }
574
575 /**
576  * Truncate/punch OFD object.
577  *
578  * This function frees all of the allocated object's space from the \a start
579  * offset to the \a end offset. For truncate() operations the \a end offset
580  * is OBD_OBJECT_EOF. The functionality to punch holes in an object via
581  * fallocate(FALLOC_FL_PUNCH_HOLE) is not yet implemented (see LU-3606).
582  *
583  * \param[in] env       execution environment
584  * \param[in] fo        OFD object
585  * \param[in] start     start offset to punch from
586  * \param[in] end       end of punch
587  * \param[in] la        object attributes
588  * \param[in] ff        filter_fid structure
589  * \param[in] oa        obdo struct from incoming request
590  *
591  * \retval              0 if successful
592  * \retval              negative value on error
593  */
594 int ofd_object_punch(const struct lu_env *env, struct ofd_object *fo,
595                      __u64 start, __u64 end, struct lu_attr *la,
596                      struct filter_fid *ff, struct obdo *oa)
597 {
598         struct ofd_thread_info  *info = ofd_info(env);
599         struct ofd_device       *ofd = ofd_obj2dev(fo);
600         struct ofd_mod_data     *fmd;
601         struct dt_object        *dob = ofd_object_child(fo);
602         struct thandle          *th;
603         int                      ff_needed = 0;
604         int                      rc;
605
606         ENTRY;
607
608         /* we support truncate, not punch yet */
609         LASSERT(end == OBD_OBJECT_EOF);
610
611         fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
612         if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
613                 fmd->fmd_mactime_xid = info->fti_xid;
614         ofd_fmd_put(info->fti_exp, fmd);
615
616         ofd_write_lock(env, fo);
617         if (!ofd_object_exists(fo))
618                 GOTO(unlock, rc = -ENOENT);
619
620         if (ofd->ofd_lfsck_verify_pfid && oa->o_valid & OBD_MD_FLFID) {
621                 rc = ofd_verify_ff(env, fo, oa);
622                 if (rc != 0)
623                         GOTO(unlock, rc);
624         }
625
626         /* VBR: version recovery check */
627         rc = ofd_version_get_check(info, fo);
628         if (rc)
629                 GOTO(unlock, rc);
630
631         rc = ofd_attr_handle_ugid(env, fo, la, 0 /* !is_setattr */);
632         if (rc != 0)
633                 GOTO(unlock, rc);
634
635         if (ff != NULL) {
636                 rc = ofd_object_ff_load(env, fo);
637                 if (rc == -ENODATA)
638                         ff_needed = 1;
639                 else if (rc < 0)
640                         GOTO(unlock, rc);
641         }
642
643         th = ofd_trans_create(env, ofd);
644         if (IS_ERR(th))
645                 GOTO(unlock, rc = PTR_ERR(th));
646
647         rc = dt_declare_attr_set(env, dob, la, th);
648         if (rc)
649                 GOTO(stop, rc);
650
651         rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
652         if (rc)
653                 GOTO(stop, rc);
654
655         if (ff_needed) {
656                 info->fti_buf.lb_buf = ff;
657                 info->fti_buf.lb_len = sizeof(*ff);
658                 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
659                                           &info->fti_buf, XATTR_NAME_FID, 0,
660                                           th);
661                 if (rc)
662                         GOTO(stop, rc);
663         }
664
665         rc = ofd_trans_start(env, ofd, fo, th);
666         if (rc)
667                 GOTO(stop, rc);
668
669         rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th,
670                       ofd_object_capa(env, fo));
671         if (rc)
672                 GOTO(stop, rc);
673
674         rc = dt_attr_set(env, dob, la, th, ofd_object_capa(env, fo));
675         if (rc)
676                 GOTO(stop, rc);
677
678         if (ff_needed) {
679                 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
680                                   XATTR_NAME_FID, 0, th, BYPASS_CAPA);
681                 if (rc == 0) {
682                         fo->ofo_pfid.f_seq = le64_to_cpu(ff->ff_parent.f_seq);
683                         fo->ofo_pfid.f_oid = le32_to_cpu(ff->ff_parent.f_oid);
684                         /* Currently, the filter_fid::ff_parent::f_ver is not
685                          * the real parent MDT-object's FID::f_ver, instead it
686                          * is the OST-object index in its parent MDT-object's
687                          * layout EA. */
688                         fo->ofo_pfid.f_stripe_idx =
689                                         le32_to_cpu(ff->ff_parent.f_stripe_idx);
690                 }
691         }
692
693         GOTO(stop, rc);
694
695 stop:
696         ofd_trans_stop(env, ofd, th, rc);
697 unlock:
698         ofd_write_unlock(env, fo);
699
700         return rc;
701 }
702
703 /**
704  * Destroy OFD object.
705  *
706  * This function destroys OFD object. If object wasn't used at all (orphan)
707  * then local transaction is used, which means the transaction data is not
708  * returned back in reply.
709  *
710  * \param[in] env       execution environment
711  * \param[in] fo        OFD object
712  * \param[in] orphan    flag to indicate that object is orphaned
713  *
714  * \retval              0 if successful
715  * \retval              negative value on error
716  */
717 int ofd_object_destroy(const struct lu_env *env, struct ofd_object *fo,
718                        int orphan)
719 {
720         struct ofd_device       *ofd = ofd_obj2dev(fo);
721         struct thandle          *th;
722         int                      rc = 0;
723
724         ENTRY;
725
726         ofd_write_lock(env, fo);
727         if (!ofd_object_exists(fo))
728                 GOTO(unlock, rc = -ENOENT);
729
730         th = ofd_trans_create(env, ofd);
731         if (IS_ERR(th))
732                 GOTO(unlock, rc = PTR_ERR(th));
733
734         dt_declare_ref_del(env, ofd_object_child(fo), th);
735         dt_declare_destroy(env, ofd_object_child(fo), th);
736         if (orphan)
737                 rc = dt_trans_start_local(env, ofd->ofd_osd, th);
738         else
739                 rc = ofd_trans_start(env, ofd, NULL, th);
740         if (rc)
741                 GOTO(stop, rc);
742
743         ofd_fmd_drop(ofd_info(env)->fti_exp, &fo->ofo_header.loh_fid);
744
745         dt_ref_del(env, ofd_object_child(fo), th);
746         dt_destroy(env, ofd_object_child(fo), th);
747 stop:
748         ofd_trans_stop(env, ofd, th, rc);
749 unlock:
750         ofd_write_unlock(env, fo);
751         RETURN(rc);
752 }
753
754 /**
755  * Get OFD object attributes.
756  *
757  * This function gets OFD object regular attributes. It is used to serve
758  * incoming request as well as for local OFD purposes.
759  *
760  * \param[in] env       execution environment
761  * \param[in] fo        OFD object
762  * \param[in] la        object attributes
763  *
764  * \retval              0 if successful
765  * \retval              negative value on error
766  */
767 int ofd_attr_get(const struct lu_env *env, struct ofd_object *fo,
768                  struct lu_attr *la)
769 {
770         int rc = 0;
771
772         ENTRY;
773
774         if (ofd_object_exists(fo)) {
775                 rc = dt_attr_get(env, ofd_object_child(fo), la,
776                                  ofd_object_capa(env, fo));
777
778 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
779                 /* Try to correct for a bug in 2.1.0 (LU-221) that caused
780                  * negative timestamps to appear to be in the far future,
781                  * due old timestamp being stored on disk as an unsigned value.
782                  * This fixes up any bad values stored on disk before
783                  * returning them to the client, and ensures any timestamp
784                  * updates are correct.  LU-1042 */
785                 if (unlikely(la->la_atime == LU221_BAD_TIME))
786                         la->la_atime = 0;
787                 if (unlikely(la->la_mtime == LU221_BAD_TIME))
788                         la->la_mtime = 0;
789                 if (unlikely(la->la_ctime == LU221_BAD_TIME))
790                         la->la_ctime = 0;
791 #else
792 #warning "remove old LU-221/LU-1042 workaround code"
793 #endif
794         } else {
795                 rc = -ENOENT;
796         }
797         RETURN(rc);
798 }