Whamcloud - gitweb
LU-16634 misc: standardize iocontrol param handling
[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, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/ofd/ofd_objects.c
32  *
33  * This file contains OSD API methods related to OBD Filter Device (OFD)
34  * object operations.
35  *
36  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
37  * Author: Mikhail Pershin <mike.pershin@intel.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_FILTER
41
42 #include <dt_object.h>
43 #include <lustre_lfsck.h>
44
45 #include "ofd_internal.h"
46
47 /**
48  * Get object version from disk and check it.
49  *
50  * This function checks object version from disk with
51  * ofd_thread_info::fti_pre_version filled from incoming RPC. This is part of
52  * VBR (Version-Based Recovery) and ensures that object has the same version
53  * upon replay as it has during original modification.
54  *
55  * \param[in]  info     execution thread OFD private data
56  * \param[in]  fo       OFD object
57  *
58  * \retval              0 if version matches
59  * \retval              -EOVERFLOW on version mismatch
60  */
61 static int ofd_version_get_check(struct ofd_thread_info *info,
62                                  struct ofd_object *fo)
63 {
64         dt_obj_version_t curr_version;
65
66         if (info->fti_exp == NULL)
67                 RETURN(0);
68
69         curr_version = dt_version_get(info->fti_env, ofd_object_child(fo));
70         if ((__s64)curr_version == -EOPNOTSUPP)
71                 RETURN(0);
72         /* VBR: version is checked always because costs nothing */
73         if (info->fti_pre_version != 0 &&
74             info->fti_pre_version != curr_version) {
75                 CDEBUG(D_INODE, "Version mismatch %#llx != %#llx\n",
76                        info->fti_pre_version, curr_version);
77                 spin_lock(&info->fti_exp->exp_lock);
78                 info->fti_exp->exp_vbr_failed = 1;
79                 spin_unlock(&info->fti_exp->exp_lock);
80                 RETURN (-EOVERFLOW);
81         }
82         info->fti_pre_version = curr_version;
83         RETURN(0);
84 }
85
86 /**
87  * Get OFD object by FID.
88  *
89  * This function finds OFD slice of compound object with the given FID.
90  *
91  * \param[in] env       execution environment
92  * \param[in] ofd       OFD device
93  * \param[in] fid       FID of the object
94  *
95  * \retval              pointer to the found ofd_object
96  * \retval              ERR_PTR(errno) in case of error
97  */
98 struct ofd_object *ofd_object_find(const struct lu_env *env,
99                                    struct ofd_device *ofd,
100                                    const struct lu_fid *fid)
101 {
102         struct ofd_object *fo;
103         struct lu_object  *o;
104
105         ENTRY;
106
107         o = lu_object_find(env, &ofd->ofd_dt_dev.dd_lu_dev, fid, NULL);
108         if (likely(!IS_ERR(o)))
109                 fo = ofd_obj(o);
110         else
111                 fo = ERR_CAST(o); /* return error */
112
113         RETURN(fo);
114 }
115
116 /**
117  * Get FID of parent MDT object.
118  *
119  * This function reads extended attribute XATTR_NAME_FID of OFD object which
120  * contains the MDT parent object FID and saves it in ofd_object::ofo_ff.
121  *
122  * The filter_fid::ff_parent::f_ver field currently holds
123  * the OST-object index in the parent MDT-object's layout EA,
124  * not the actual FID::f_ver of the parent. We therefore access
125  * it via the macro f_stripe_idx.
126  *
127  * \param[in] env       execution environment
128  * \param[in] fo        OFD object
129  *
130  * \retval              0 if successful
131  * \retval              -ENODATA if there is no such xattr
132  * \retval              negative value on error
133  */
134 int ofd_object_ff_load(const struct lu_env *env, struct ofd_object *fo)
135 {
136         struct ofd_thread_info *info = ofd_info(env);
137         struct filter_fid *ff = &fo->ofo_ff;
138         struct lu_buf *buf = &info->fti_buf;
139         int rc = 0;
140
141         if (fid_is_sane(&ff->ff_parent))
142                 return 0;
143
144         buf->lb_buf = ff;
145         buf->lb_len = sizeof(*ff);
146         rc = dt_xattr_get(env, ofd_object_child(fo), buf, XATTR_NAME_FID);
147         if (rc < 0)
148                 return rc;
149
150         if (unlikely(rc < sizeof(struct lu_fid))) {
151                 fid_zero(&ff->ff_parent);
152                 return -EINVAL;
153         }
154
155         filter_fid_le_to_cpu(ff, ff, rc);
156
157         return 0;
158 }
159
160 struct ofd_precreate_cb {
161         struct dt_txn_commit_cb  opc_cb;
162         struct ofd_seq          *opc_oseq;
163         int                      opc_objects;
164 };
165
166 static void ofd_cb_precreate(struct lu_env *env, struct thandle *th,
167                              struct dt_txn_commit_cb *cb, int err)
168 {
169         struct ofd_precreate_cb *opc;
170         struct ofd_seq *oseq;
171
172         opc = container_of(cb, struct ofd_precreate_cb, opc_cb);
173         oseq = opc->opc_oseq;
174
175         CDEBUG(D_OTHER, "Sub %d from %d for "DFID", th_sync %d\n",
176                opc->opc_objects, atomic_read(&oseq->os_precreate_in_progress),
177                PFID(&oseq->os_oi.oi_fid), th->th_sync);
178         atomic_sub(opc->opc_objects, &oseq->os_precreate_in_progress);
179         ofd_seq_put(env, opc->opc_oseq);
180         OBD_FREE_PTR(opc);
181 }
182
183 static int ofd_precreate_cb_add(const struct lu_env *env, struct thandle *th,
184                                 struct ofd_seq *oseq, int objects)
185 {
186         struct ofd_precreate_cb *opc;
187         struct dt_txn_commit_cb *dcb;
188         int precreate, rc;
189
190         OBD_ALLOC_PTR(opc);
191         if (!opc)
192                 return -ENOMEM;
193
194         precreate = atomic_read(&oseq->os_precreate_in_progress);
195         atomic_inc(&oseq->os_refc);
196         opc->opc_oseq = oseq;
197         opc->opc_objects = objects;
198         CDEBUG(D_OTHER, "Add %d to %d for "DFID", th_sync %d\n",
199                opc->opc_objects, precreate,
200                PFID(&oseq->os_oi.oi_fid), th->th_sync);
201
202         if ((precreate + objects) >= (5 * OST_MAX_PRECREATE))
203                 th->th_sync = 1;
204
205         dcb = &opc->opc_cb;
206         dcb->dcb_func = ofd_cb_precreate;
207         INIT_LIST_HEAD(&dcb->dcb_linkage);
208         strlcpy(dcb->dcb_name, "ofd_cb_precreate", sizeof(dcb->dcb_name));
209
210         rc = dt_trans_cb_add(th, dcb);
211         if (rc) {
212                 ofd_seq_put(env, oseq);
213                 OBD_FREE_PTR(opc);
214                 return rc;
215         }
216
217         atomic_add(objects, &oseq->os_precreate_in_progress);
218
219         return 0;
220 }
221
222 /**
223  * Precreate the given number \a nr of objects in the given sequence \a oseq.
224  *
225  * This function precreates new OST objects in the given sequence.
226  * The precreation starts from \a id and creates \a nr objects sequentially.
227  *
228  * Notes:
229  * This function may create fewer objects than requested.
230  *
231  * We mark object SUID+SGID to flag it for accepting UID+GID from client on
232  * first write. Currently the permission bits on the OST are never used,
233  * so this is OK.
234  *
235  * Initialize a/c/m time so any client timestamp will always be newer and
236  * update the inode. The ctime = 0 case is also handled specially in
237  * osd_inode_setattr(). See LU-221, LU-1042 for details.
238  *
239  * \param[in] env       execution environment
240  * \param[in] ofd       OFD device
241  * \param[in] id        object ID to start precreation from
242  * \param[in] oseq      object sequence
243  * \param[in] nr        number of objects to precreate
244  * \param[in] sync      synchronous precreation flag
245  *
246  * \retval              0 if successful
247  * \retval              negative value on error
248  */
249 int ofd_precreate_objects(const struct lu_env *env, struct ofd_device *ofd,
250                           u64 id, struct ofd_seq *oseq, int nr, int sync)
251 {
252         struct ofd_thread_info  *info = ofd_info(env);
253         struct ofd_object       *fo = NULL;
254         struct dt_object        *next;
255         struct thandle          *th;
256         struct ofd_object       **batch;
257         struct lu_fid           *fid = &info->fti_fid;
258         u64                     tmp;
259         int                     rc;
260         int                     rc2;
261         int                     i;
262         int                     objects = 0;
263         int                     nr_saved = nr;
264
265         ENTRY;
266
267         /* Don't create objects beyond the valid range for this SEQ
268          * Last object to create is (id + nr - 1), but we move -1 on LHS
269          * to +1 on RHS to evaluate constant at compile time. LU-11186
270          */
271         if (unlikely(fid_seq_is_mdt0(ostid_seq(&oseq->os_oi)) &&
272                      id + nr > IDIF_MAX_OID + 1)) {
273                 CERROR("%s:"DOSTID" hit the IDIF_MAX_OID (1<<48)!\n",
274                        ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
275                 RETURN(rc = -ENOSPC);
276         } else if (unlikely(!fid_seq_is_mdt0(ostid_seq(&oseq->os_oi)) &&
277                             id + nr > OBIF_MAX_OID + 1)) {
278                 CERROR("%s:"DOSTID" hit the OBIF_MAX_OID (1<<32)!\n",
279                        ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
280                 RETURN(rc = -ENOSPC);
281         }
282
283         OBD_ALLOC_PTR_ARRAY(batch, nr_saved);
284         if (batch == NULL)
285                 RETURN(-ENOMEM);
286
287         info->fti_attr.la_valid = LA_TYPE | LA_MODE;
288         info->fti_attr.la_mode = S_IFREG | S_ISUID | S_ISGID | S_ISVTX | 0666;
289         info->fti_dof.dof_type = dt_mode_to_dft(S_IFREG);
290
291         info->fti_attr.la_valid |= LA_ATIME | LA_MTIME | LA_CTIME;
292         info->fti_attr.la_atime = 0;
293         info->fti_attr.la_mtime = 0;
294         info->fti_attr.la_ctime = 0;
295
296         LASSERT(id != 0);
297
298         /* prepare objects */
299         *fid = *lu_object_fid(&oseq->os_lastid_obj->do_lu);
300         for (i = 0; i < nr; i++) {
301                 rc = fid_set_id(fid, id + i);
302                 if (rc != 0) {
303                         if (i == 0)
304                                 GOTO(out, rc);
305
306                         nr = i;
307                         break;
308                 }
309
310                 fo = ofd_object_find(env, ofd, fid);
311                 if (IS_ERR(fo)) {
312                         if (i == 0)
313                                 GOTO(out, rc = PTR_ERR(fo));
314
315                         nr = i;
316                         break;
317                 }
318
319                 batch[i] = fo;
320         }
321         info->fti_buf.lb_buf = &tmp;
322         info->fti_buf.lb_len = sizeof(tmp);
323         info->fti_off = 0;
324
325         th = ofd_trans_create(env, ofd);
326         if (IS_ERR(th))
327                 GOTO(out, rc = PTR_ERR(th));
328
329         th->th_sync |= sync;
330
331         rc = dt_declare_record_write(env, oseq->os_lastid_obj, &info->fti_buf,
332                                      info->fti_off, th);
333         if (rc)
334                 GOTO(trans_stop, rc);
335
336         for (i = 0; i < nr; i++) {
337                 fo = batch[i];
338                 LASSERT(fo);
339
340                 if (unlikely(ofd_object_exists(fo))) {
341                         /* object may exist being re-created by write replay */
342                         CDEBUG(D_INODE, "object %#llx/%#llx exists: "
343                                DFID"\n", ostid_seq(&oseq->os_oi), id,
344                                PFID(lu_object_fid(&fo->ofo_obj.do_lu)));
345                         continue;
346                 }
347
348                 next = ofd_object_child(fo);
349                 LASSERT(next != NULL);
350
351                 rc = dt_declare_create(env, next, &info->fti_attr, NULL,
352                                        &info->fti_dof, th);
353                 if (rc < 0) {
354                         if (i == 0)
355                                 GOTO(trans_stop, rc);
356
357                         nr = i;
358                         break;
359                 }
360         }
361
362         rc = dt_trans_start(env, ofd->ofd_osd, th);
363         if (rc)
364                 GOTO(trans_stop, rc);
365
366         CDEBUG(D_OTHER, "%s: create new object "DFID" nr %d\n",
367                ofd_name(ofd), PFID(fid), nr);
368
369          /* When the LFSCK scanning the whole device to verify the LAST_ID file
370           * consistency, it will load the last_id into RAM firstly, and compare
371           * the last_id with each OST-object's ID. If the later one is larger,
372           * then it will regard the LAST_ID file crashed. But during the LFSCK
373           * scanning, the OFD may continue to create new OST-objects. Those new
374           * created OST-objects will have larger IDs than the LFSCK known ones.
375           * So from the LFSCK view, it needs to re-load the last_id from disk
376           * file, and if the latest last_id is still smaller than the object's
377           * ID, then the LAST_ID file is real crashed.
378           *
379           * To make above mechanism to work, before OFD pre-create OST-objects,
380           * it needs to update the LAST_ID file firstly, otherwise, the LFSCK
381           * may cannot get latest last_id although new OST-object created. */
382         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_SKIP_LASTID)) {
383                 tmp = cpu_to_le64(id + nr - 1);
384                 dt_write_lock(env, oseq->os_lastid_obj, DT_LASTID);
385                 rc = dt_record_write(env, oseq->os_lastid_obj,
386                                      &info->fti_buf, &info->fti_off, th);
387                 dt_write_unlock(env, oseq->os_lastid_obj);
388                 if (rc != 0)
389                         GOTO(trans_stop, rc);
390         }
391
392         for (i = 0; i < nr; i++) {
393                 fo = batch[i];
394                 LASSERT(fo);
395
396                 ofd_write_lock(env, fo);
397
398                 /* Only the new created objects need to be recorded. */
399                 if (ofd->ofd_osd->dd_record_fid_accessed) {
400                         struct lfsck_req_local *lrl = &ofd_info(env)->fti_lrl;
401
402                         lfsck_pack_rfa(lrl, lu_object_fid(&fo->ofo_obj.do_lu),
403                                        LEL_FID_ACCESSED, LFSCK_TYPE_LAYOUT);
404                         lfsck_in_notify_local(env, ofd->ofd_osd, lrl, NULL);
405                 }
406
407                 if (likely(!ofd_object_exists(fo) &&
408                            !OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DANGLING))) {
409                         next = ofd_object_child(fo);
410                         LASSERT(next != NULL);
411
412                         rc = dt_create(env, next, &info->fti_attr, NULL,
413                                        &info->fti_dof, th);
414                         ofd_write_unlock(env, fo);
415                         if (rc < 0) {
416                                 if (i == 0)
417                                         GOTO(trans_stop, rc);
418
419                                 rc = 0;
420                                 break;
421                         }
422                         LASSERT(ofd_object_exists(fo));
423                 } else {
424                         ofd_write_unlock(env, fo);
425                 }
426
427                 ofd_seq_last_oid_set(oseq, id + i);
428         }
429
430         objects = i;
431         /* NOT all the wanted objects have been created,
432          * set the LAST_ID as the real created. */
433         if (unlikely(objects < nr)) {
434                 int rc1;
435
436                 info->fti_off = 0;
437                 tmp = cpu_to_le64(ofd_seq_last_oid(oseq));
438                 dt_write_lock(env, oseq->os_lastid_obj, DT_LASTID);
439                 rc1 = dt_record_write(env, oseq->os_lastid_obj,
440                                       &info->fti_buf, &info->fti_off, th);
441                 dt_write_unlock(env, oseq->os_lastid_obj);
442                 if (rc1 != 0)
443                         CERROR("%s: fail to reset the LAST_ID for seq (%#llx"
444                                ") from %llu to %llu\n", ofd_name(ofd),
445                                ostid_seq(&oseq->os_oi), id + nr - 1,
446                                ofd_seq_last_oid(oseq));
447         }
448
449         if (objects)
450                 ofd_precreate_cb_add(env, th, oseq, objects);
451 trans_stop:
452         rc2 = ofd_trans_stop(env, ofd, th, rc);
453         if (rc2)
454                 CERROR("%s: failed to stop transaction: rc = %d\n",
455                        ofd_name(ofd), rc2);
456         if (!rc)
457                 rc = rc2;
458 out:
459         for (i = 0; i < nr_saved; i++) {
460                 fo = batch[i];
461                 if (!fo)
462                         continue;
463                 ofd_object_put(env, fo);
464         }
465         OBD_FREE_PTR_ARRAY(batch, nr_saved);
466
467         CDEBUG((objects == 0 && rc == 0) ? D_ERROR : D_OTHER,
468                "created %d/%d objects: %d\n", objects, nr_saved, rc);
469
470         LASSERT(ergo(objects == 0, rc < 0));
471         RETURN(objects > 0 ? objects : rc);
472 }
473
474 /**
475  * Fix the OFD object ownership.
476  *
477  * If the object still has SUID+SGID bits set, meaning that it was precreated
478  * by the MDT before it was assigned to any file, (see ofd_precreate_objects())
479  * then we will accept the UID/GID/PROJID if sent by the client for initializing
480  * the ownership of this object.  We only allow this to happen once (so clear
481  * these bits) and later only allow setattr.
482  *
483  * \param[in] env        execution environment
484  * \param[in] fo         OFD object
485  * \param[in] la         object attributes
486  * \param[in] is_setattr was this function called from setattr or not
487  *
488  * \retval              0 if successful
489  * \retval              negative value on error
490  */
491 int ofd_attr_handle_id(const struct lu_env *env, struct ofd_object *fo,
492                          struct lu_attr *la, int is_setattr)
493 {
494         struct ofd_thread_info  *info = ofd_info(env);
495         struct lu_attr          *ln = &info->fti_attr2;
496         __u32                    mask = 0;
497         int                      rc;
498
499         ENTRY;
500
501         if (!(la->la_valid & LA_UID) && !(la->la_valid & LA_GID) &&
502             !(la->la_valid & LA_PROJID))
503                 RETURN(0);
504
505         rc = dt_attr_get(env, ofd_object_child(fo), ln);
506         if (rc != 0)
507                 RETURN(rc);
508
509         LASSERT(ln->la_valid & LA_MODE);
510
511         /*
512          * Only allow setattr to change UID/GID/PROJID, if
513          * SUID+SGID is not set which means this is not
514          * initialization of this objects.
515          */
516         if (!is_setattr) {
517                 if (!(ln->la_mode & S_ISUID))
518                         la->la_valid &= ~LA_UID;
519                 if (!(ln->la_mode & S_ISGID))
520                         la->la_valid &= ~LA_GID;
521                 if (!(ln->la_mode & S_ISVTX))
522                         la->la_valid &= ~LA_PROJID;
523         }
524
525         /* Initialize ownership of this object, clear SUID+SGID bits*/
526         if ((la->la_valid & LA_UID) && (ln->la_mode & S_ISUID))
527                 mask |= S_ISUID;
528         if ((la->la_valid & LA_GID) && (ln->la_mode & S_ISGID))
529                 mask |= S_ISGID;
530         if ((la->la_valid & LA_PROJID) && (ln->la_mode & S_ISVTX))
531                 mask |= S_ISVTX;
532         if (mask != 0) {
533                 if (!(la->la_valid & LA_MODE) || !is_setattr) {
534                         la->la_mode = ln->la_mode;
535                         la->la_valid |= LA_MODE;
536                 }
537                 la->la_mode &= ~mask;
538         }
539
540         RETURN(0);
541 }
542
543 /**
544  * Check if it needs to update filter_fid by the value of @oa.
545  *
546  * \param[in] env       env
547  * \param[in] fo        ofd object
548  * \param[in] oa        obdo from client or MDT
549  * \param[out] ff       if filter_fid needs updating, this field is used to
550  *                      return the new buffer
551  *
552  * \retval < 0          error occurred
553  * \retval 0            doesn't need to update filter_fid
554  * \retval FL_XATTR_{CREATE,REPLACE}    flag for xattr update
555  */
556 int ofd_object_ff_update(const struct lu_env *env, struct ofd_object *fo,
557                          const struct obdo *oa, struct filter_fid *ff)
558 {
559         int rc = 0;
560         ENTRY;
561
562         if (!(oa->o_valid &
563               (OBD_MD_FLFID | OBD_MD_FLOSTLAYOUT | OBD_MD_LAYOUT_VERSION)))
564                 RETURN(0);
565
566         rc = ofd_object_ff_load(env, fo);
567         if (rc < 0 && rc != -ENODATA)
568                 RETURN(rc);
569
570         LASSERT(ff != &fo->ofo_ff);
571         if (rc == -ENODATA) {
572                 rc = LU_XATTR_CREATE;
573                 memset(ff, 0, sizeof(*ff));
574         } else {
575                 rc = LU_XATTR_REPLACE;
576                 memcpy(ff, &fo->ofo_ff, sizeof(*ff));
577         }
578
579         if (oa->o_valid & OBD_MD_FLFID) {
580                 /* packing fid and converting it to LE for storing into EA.
581                  * Here ->o_stripe_idx should be filled by LOV and rest of
582                  * fields - by client. */
583                 ff->ff_parent.f_seq = oa->o_parent_seq;
584                 ff->ff_parent.f_oid = oa->o_parent_oid;
585                 /* XXX: we are ignoring o_parent_ver here, since this should
586                  *      be the same for all objects in this fileset. */
587                 ff->ff_parent.f_ver = oa->o_stripe_idx;
588         }
589         if (oa->o_valid & OBD_MD_FLOSTLAYOUT)
590                 ff->ff_layout = oa->o_layout;
591
592         if (oa->o_valid & OBD_MD_LAYOUT_VERSION) {
593                 CDEBUG(D_INODE, DFID": OST("DFID") layout version %u -> %u\n",
594                        PFID(&fo->ofo_ff.ff_parent),
595                        PFID(lu_object_fid(&fo->ofo_obj.do_lu)),
596                        ff->ff_layout_version, oa->o_layout_version);
597
598                 /**
599                  * resync write from client on non-primary objects and
600                  * resync start from MDS on primary objects will contain
601                  * LU_LAYOUT_RESYNC flag in the @oa.
602                  *
603                  * The layout version checking for write/punch from client
604                  * happens in ofd_verify_layout_version() before coming to
605                  * here, so that resync with smaller layout version client
606                  * will be rejected there, the biggest resync version will
607                  * be recorded in the OFD objects.
608                  */
609                 if (ff->ff_layout_version & LU_LAYOUT_RESYNC) {
610                         /* this opens a new era of writing */
611                         ff->ff_layout_version = 0;
612                         ff->ff_range = 0;
613                 }
614
615                 /* it's not allowed to change it to a smaller value */
616                 if (ofd_layout_version_less(oa->o_layout_version,
617                                             ff->ff_layout_version))
618                         RETURN(-EINVAL);
619
620                 if (ff->ff_layout_version == 0 ||
621                     oa->o_layout_version & LU_LAYOUT_RESYNC) {
622                         /* if LU_LAYOUT_RESYNC is set, it closes the era of
623                          * writing. Only mirror I/O can write this object. */
624                         ff->ff_layout_version = oa->o_layout_version;
625                         ff->ff_range = 0;
626                 } else if (oa->o_layout_version > ff->ff_layout_version) {
627                         ff->ff_range = max_t(__u32, ff->ff_range,
628                                              oa->o_layout_version -
629                                              ff->ff_layout_version);
630                 }
631         }
632
633         if (memcmp(ff, &fo->ofo_ff, sizeof(*ff)))
634                 filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
635         else /* no change */
636                 rc = 0;
637
638         RETURN(rc);
639 }
640
641 /**
642  * Set OFD object attributes.
643  *
644  * This function sets OFD object attributes taken from incoming request.
645  * It sets not only regular attributes but also XATTR_NAME_FID extended
646  * attribute if needed. The "fid" xattr allows the object's MDT parent inode
647  * to be found and verified by LFSCK and other tools in case of inconsistency.
648  *
649  * \param[in] env       execution environment
650  * \param[in] fo        OFD object
651  * \param[in] la        object attributes
652  * \param[in] oa        obdo carries fid, ost_layout, layout version
653  *
654  * \retval              0 if successful
655  * \retval              negative value on error
656  */
657 int ofd_attr_set(const struct lu_env *env, struct ofd_object *fo,
658                  struct lu_attr *la, struct obdo *oa)
659 {
660         struct ofd_thread_info *info = ofd_info(env);
661         struct ofd_device *ofd = ofd_obj2dev(fo);
662         struct filter_fid *ff = &info->fti_mds_fid;
663         struct thandle *th;
664         int fl, rc, rc2;
665
666         ENTRY;
667
668         if (!ofd_object_exists(fo))
669                 GOTO(out, rc = -ENOENT);
670
671         /* VBR: version recovery check */
672         rc = ofd_version_get_check(info, fo);
673         if (rc)
674                 GOTO(out, rc);
675
676         rc = ofd_attr_handle_id(env, fo, la, 1 /* is_setattr */);
677         if (rc != 0)
678                 GOTO(out, rc);
679
680         th = ofd_trans_create(env, ofd);
681         if (IS_ERR(th))
682                 GOTO(out, rc = PTR_ERR(th));
683
684         rc = dt_declare_attr_set(env, ofd_object_child(fo), la, th);
685         if (rc)
686                 GOTO(stop, rc);
687
688         info->fti_buf.lb_buf = ff;
689         info->fti_buf.lb_len = sizeof(*ff);
690         rc = dt_declare_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
691                                   XATTR_NAME_FID, 0, th);
692         if (rc)
693                 GOTO(stop, rc);
694
695         rc = ofd_trans_start(env, ofd, la->la_valid & LA_SIZE ? fo : NULL, th);
696         if (rc)
697                 GOTO(stop, rc);
698
699         ofd_write_lock(env, fo);
700         if (!ofd_object_exists(fo))
701                 GOTO(unlock, rc = -ENOENT);
702
703         /* serialize vs ofd_commitrw_write() */
704         if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
705                 tgt_fmd_update(info->fti_exp, &fo->ofo_header.loh_fid,
706                                info->fti_xid);
707
708         rc = dt_attr_set(env, ofd_object_child(fo), la, th);
709         if (rc)
710                 GOTO(unlock, rc);
711
712         fl = ofd_object_ff_update(env, fo, oa, ff);
713         if (fl < 0)
714                 GOTO(unlock, rc = fl);
715
716         if (fl) {
717                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_UNMATCHED_PAIR1))
718                         ff->ff_parent.f_oid = cpu_to_le32(1UL << 31);
719                 else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_UNMATCHED_PAIR2))
720                         le32_add_cpu(&ff->ff_parent.f_oid, -1);
721                 else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NOPFID))
722                         GOTO(unlock, rc);
723
724                 info->fti_buf.lb_buf = ff;
725                 info->fti_buf.lb_len = sizeof(*ff);
726                 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
727                                   XATTR_NAME_FID, fl, th);
728                 if (!rc)
729                         filter_fid_le_to_cpu(&fo->ofo_ff, ff, sizeof(*ff));
730         }
731
732         GOTO(unlock, rc);
733
734 unlock:
735         ofd_write_unlock(env, fo);
736 stop:
737         rc2 = ofd_trans_stop(env, ofd, th, rc);
738         if (rc2)
739                 CERROR("%s: failed to stop transaction: rc = %d\n",
740                        ofd_name(ofd), rc2);
741         if (!rc)
742                 rc = rc2;
743 out:
744         return rc;
745 }
746
747 /**
748  * Fallocate(Preallocate) space for OFD object.
749  *
750  * This function allocates space for the object from the \a start
751  * offset to the \a end offset.
752  *
753  * \param[in] env       execution environment
754  * \param[in] fo        OFD object
755  * \param[in] start     start offset to allocate from
756  * \param[in] end       end of allocate
757  * \param[in] mode      fallocate mode
758  * \param[in] la        object attributes
759  * \param[in] ff        filter_fid structure
760  *
761  * \retval              0 if successful
762  * \retval              negative value on error
763  */
764 int ofd_object_fallocate(const struct lu_env *env, struct ofd_object *fo,
765                          __u64 start, __u64 end, int mode, struct lu_attr *la,
766                          struct obdo *oa)
767 {
768         struct ofd_thread_info *info = ofd_info(env);
769         struct ofd_device *ofd = ofd_obj2dev(fo);
770         struct dt_object *dob = ofd_object_child(fo);
771         struct thandle *th;
772         struct filter_fid *ff = &info->fti_mds_fid;
773         bool ff_needed = false;
774         int rc;
775
776         ENTRY;
777
778         if (!ofd_object_exists(fo))
779                 RETURN(-ENOENT);
780
781         /* VBR: version recovery check */
782         rc = ofd_version_get_check(info, fo);
783         if (rc != 0)
784                 RETURN(rc);
785
786         if (ff != NULL) {
787                 rc = ofd_object_ff_load(env, fo);
788                 if (rc == -ENODATA)
789                         ff_needed = true;
790                 else if (rc < 0)
791                         RETURN(rc);
792
793                 if (ff_needed) {
794                         if (oa->o_valid & OBD_MD_FLFID) {
795                                 ff->ff_parent.f_seq = oa->o_parent_seq;
796                                 ff->ff_parent.f_oid = oa->o_parent_oid;
797                                 ff->ff_parent.f_ver = oa->o_stripe_idx;
798                         }
799                         if (oa->o_valid & OBD_MD_FLOSTLAYOUT)
800                                 ff->ff_layout = oa->o_layout;
801                         if (oa->o_valid & OBD_MD_LAYOUT_VERSION)
802                                 ff->ff_layout_version = oa->o_layout_version;
803                         filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
804                 }
805         }
806
807         th = ofd_trans_create(env, ofd);
808         if (IS_ERR(th))
809                 RETURN(PTR_ERR(th));
810
811         rc = dt_declare_attr_set(env, dob, la, th);
812         if (rc)
813                 GOTO(stop, rc);
814
815         rc = dt_declare_fallocate(env, dob, start, end, mode, th);
816         if (rc)
817                 GOTO(stop, rc);
818
819         if (ff_needed) {
820                 info->fti_buf.lb_buf = ff;
821                 info->fti_buf.lb_len = sizeof(*ff);
822                 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
823                                           &info->fti_buf, XATTR_NAME_FID, 0,
824                                           th);
825                 if (rc)
826                         GOTO(stop, rc);
827         }
828
829         rc = ofd_trans_start(env, ofd, fo, th);
830         if (rc)
831                 GOTO(stop, rc);
832
833         ofd_read_lock(env, fo);
834         if (!ofd_object_exists(fo))
835                 GOTO(unlock, rc = -ENOENT);
836
837         if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
838                 tgt_fmd_update(info->fti_exp, &fo->ofo_header.loh_fid,
839                                info->fti_xid);
840
841         rc = dt_falloc(env, dob, start, end, mode, th);
842         if (rc)
843                 GOTO(unlock, rc);
844
845         rc = dt_attr_set(env, dob, la, th);
846         if (rc)
847                 GOTO(unlock, rc);
848
849         if (ff_needed) {
850                 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
851                                   XATTR_NAME_FID, 0, th);
852                 if (!rc)
853                         filter_fid_le_to_cpu(&fo->ofo_ff, ff, sizeof(*ff));
854         }
855 unlock:
856         ofd_read_unlock(env, fo);
857 stop:
858         ofd_trans_stop(env, ofd, th, rc);
859         RETURN(rc);
860 }
861
862 /**
863  * Truncate/punch OFD object.
864  *
865  * This function frees all of the allocated object's space from the \a start
866  * offset to the \a end offset. For truncate() operations the \a end offset
867  * is OBD_OBJECT_EOF. The functionality to punch holes in an object via
868  * fallocate(FALLOC_FL_PUNCH_HOLE) is not yet implemented (see LU-3606).
869  *
870  * \param[in] env       execution environment
871  * \param[in] fo        OFD object
872  * \param[in] start     start offset to punch from
873  * \param[in] end       end of punch
874  * \param[in] la        object attributes
875  * \param[in] oa        obdo struct from incoming request
876  *
877  * \retval              0 if successful
878  * \retval              negative value on error
879  */
880 int ofd_object_punch(const struct lu_env *env, struct ofd_object *fo,
881                      __u64 start, __u64 end, struct lu_attr *la,
882                      struct obdo *oa)
883 {
884         struct ofd_thread_info *info = ofd_info(env);
885         struct ofd_device *ofd = ofd_obj2dev(fo);
886         struct dt_object *dob = ofd_object_child(fo);
887         struct filter_fid *ff = &info->fti_mds_fid;
888         struct thandle *th;
889         int fl, rc, rc2;
890
891         ENTRY;
892
893         /* we support truncate, not punch yet */
894         LASSERT(end == OBD_OBJECT_EOF);
895
896         if (!ofd_object_exists(fo))
897                 GOTO(out, rc = -ENOENT);
898
899         if (ofd->ofd_lfsck_verify_pfid && oa->o_valid & OBD_MD_FLFID) {
900                 rc = ofd_verify_ff(env, fo, oa);
901                 if (rc != 0)
902                         GOTO(out, rc);
903         }
904
905         /* VBR: version recovery check */
906         rc = ofd_version_get_check(info, fo);
907         if (rc)
908                 GOTO(out, rc);
909
910         rc = ofd_attr_handle_id(env, fo, la, 0 /* !is_setattr */);
911         if (rc != 0)
912                 GOTO(out, rc);
913
914         th = ofd_trans_create(env, ofd);
915         if (IS_ERR(th))
916                 GOTO(out, rc = PTR_ERR(th));
917
918         if (oa->o_valid & OBD_MD_FLFLAGS && oa->o_flags & LUSTRE_ENCRYPT_FL) {
919                 /* punch must be aware we are dealing with an encrypted file */
920                 la->la_valid |= LA_FLAGS;
921                 la->la_flags |= LUSTRE_ENCRYPT_FL;
922         }
923         rc = dt_declare_attr_set(env, dob, la, th);
924         if (rc)
925                 GOTO(stop, rc);
926
927         rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
928         if (rc)
929                 GOTO(stop, rc);
930
931         info->fti_buf.lb_buf = ff;
932         info->fti_buf.lb_len = sizeof(*ff);
933         rc = dt_declare_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
934                                   XATTR_NAME_FID, 0, th);
935         if (rc)
936                 GOTO(stop, rc);
937
938         rc = ofd_trans_start(env, ofd, fo, th);
939         if (rc)
940                 GOTO(stop, rc);
941
942         ofd_write_lock(env, fo);
943
944         if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME))
945                 tgt_fmd_update(info->fti_exp, &fo->ofo_header.loh_fid,
946                                info->fti_xid);
947
948         if (!ofd_object_exists(fo))
949                 GOTO(unlock, rc = -ENOENT);
950
951         /* need to verify layout version */
952         if (oa->o_valid & OBD_MD_LAYOUT_VERSION) {
953                 rc = ofd_verify_layout_version(env, fo, oa);
954                 if (rc)
955                         GOTO(unlock, rc);
956         }
957
958         rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th);
959         if (rc)
960                 GOTO(unlock, rc);
961
962         fl = ofd_object_ff_update(env, fo, oa, ff);
963         if (fl < 0)
964                 GOTO(unlock, rc = fl);
965
966         rc = dt_attr_set(env, dob, la, th);
967         if (rc)
968                 GOTO(unlock, rc);
969
970         if (fl) {
971                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_UNMATCHED_PAIR1))
972                         ff->ff_parent.f_oid = cpu_to_le32(1UL << 31);
973                 else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_UNMATCHED_PAIR2))
974                         le32_add_cpu(&ff->ff_parent.f_oid, -1);
975                 else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NOPFID))
976                         GOTO(unlock, rc);
977
978                 info->fti_buf.lb_buf = ff;
979                 info->fti_buf.lb_len = sizeof(*ff);
980                 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
981                                   XATTR_NAME_FID, fl, th);
982                 if (!rc)
983                         filter_fid_le_to_cpu(&fo->ofo_ff, ff, sizeof(*ff));
984         }
985
986         GOTO(unlock, rc);
987
988 unlock:
989         ofd_write_unlock(env, fo);
990 stop:
991         rc2 = ofd_trans_stop(env, ofd, th, rc);
992         if (rc2 != 0)
993                 CERROR("%s: failed to stop transaction: rc = %d\n",
994                        ofd_name(ofd), rc2);
995         if (!rc)
996                 rc = rc2;
997 out:
998         return rc;
999 }
1000
1001 /**
1002  * Destroy OFD object.
1003  *
1004  * This function destroys OFD object. If object wasn't used at all (orphan)
1005  * then local transaction is used, which means the transaction data is not
1006  * returned back in reply.
1007  *
1008  * \param[in] env       execution environment
1009  * \param[in] fo        OFD object
1010  * \param[in] orphan    flag to indicate that object is orphaned
1011  *
1012  * \retval              0 if successful
1013  * \retval              negative value on error
1014  */
1015 int ofd_destroy(const struct lu_env *env, struct ofd_object *fo,
1016                        int orphan)
1017 {
1018         struct ofd_device       *ofd = ofd_obj2dev(fo);
1019         struct thandle          *th;
1020         int                     rc = 0;
1021         int                     rc2;
1022
1023         ENTRY;
1024
1025         if (!ofd_object_exists(fo))
1026                 GOTO(out, rc = -ENOENT);
1027
1028         th = ofd_trans_create(env, ofd);
1029         if (IS_ERR(th))
1030                 GOTO(out, rc = PTR_ERR(th));
1031
1032         rc = dt_declare_ref_del(env, ofd_object_child(fo), th);
1033         if (rc < 0)
1034                 GOTO(stop, rc);
1035
1036         rc = dt_declare_destroy(env, ofd_object_child(fo), th);
1037         if (rc < 0)
1038                 GOTO(stop, rc);
1039
1040         if (orphan)
1041                 rc = dt_trans_start_local(env, ofd->ofd_osd, th);
1042         else
1043                 rc = ofd_trans_start(env, ofd, NULL, th);
1044         if (rc)
1045                 GOTO(stop, rc);
1046
1047         ofd_write_lock(env, fo);
1048         if (!ofd_object_exists(fo))
1049                 GOTO(unlock, rc = -ENOENT);
1050
1051         tgt_fmd_drop(ofd_info(env)->fti_exp, &fo->ofo_header.loh_fid);
1052
1053         dt_ref_del(env, ofd_object_child(fo), th);
1054         dt_destroy(env, ofd_object_child(fo), th);
1055 unlock:
1056         ofd_write_unlock(env, fo);
1057 stop:
1058         rc2 = ofd_trans_stop(env, ofd, th, rc);
1059         if (rc2)
1060                 CERROR("%s failed to stop transaction: %d\n",
1061                        ofd_name(ofd), rc2);
1062         if (!rc)
1063                 rc = rc2;
1064 out:
1065         RETURN(rc);
1066 }
1067
1068 /**
1069  * Get OFD object attributes.
1070  *
1071  * This function gets OFD object regular attributes. It is used to serve
1072  * incoming request as well as for local OFD purposes.
1073  *
1074  * \param[in] env       execution environment
1075  * \param[in] fo        OFD object
1076  * \param[in] la        object attributes
1077  *
1078  * \retval              0 if successful
1079  * \retval              negative value on error
1080  */
1081 int ofd_attr_get(const struct lu_env *env, struct ofd_object *fo,
1082                  struct lu_attr *la)
1083 {
1084         int rc = 0;
1085
1086         ENTRY;
1087
1088         if (ofd_object_exists(fo)) {
1089                 rc = dt_attr_get(env, ofd_object_child(fo), la);
1090         } else {
1091                 rc = -ENOENT;
1092         }
1093         RETURN(rc);
1094 }