Whamcloud - gitweb
LU-10308 misc: update Intel copyright messages for 2017
[fs/lustre-release.git] / lustre / osd-zfs / osd_xattr.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  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/osd-zfs/osd_xattr.c
33  * functions to manipulate extended attributes and system attributes
34  *
35  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
36  * Author: Mike Pershin <tappro@whamcloud.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_OSD
40
41 #include <libcfs/libcfs.h>
42 #include <obd_support.h>
43 #include <lustre_net.h>
44 #include <obd.h>
45 #include <obd_class.h>
46 #include <lustre_disk.h>
47 #include <lustre_fid.h>
48 #include <lustre_linkea.h>
49
50 #include "osd_internal.h"
51
52 #include <sys/dnode.h>
53 #include <sys/dbuf.h>
54 #include <sys/spa.h>
55 #include <sys/stat.h>
56 #include <sys/zap.h>
57 #include <sys/spa_impl.h>
58 #include <sys/zfs_znode.h>
59 #include <sys/dmu_tx.h>
60 #include <sys/dmu_objset.h>
61 #include <sys/dsl_prop.h>
62 #include <sys/sa_impl.h>
63 #include <sys/txg.h>
64
65 #include <linux/posix_acl_xattr.h>
66 #include <lustre_scrub.h>
67
68 int __osd_xattr_load(struct osd_device *osd, sa_handle_t *hdl, nvlist_t **sa)
69 {
70         char        *buf;
71         int          rc, size;
72
73         rc = -sa_size(hdl, SA_ZPL_DXATTR(osd), &size);
74         if (rc) {
75                 if (rc == -ENOENT)
76                         rc = -nvlist_alloc(sa, NV_UNIQUE_NAME, KM_SLEEP);
77                 goto out_sa;
78         }
79
80         buf = osd_zio_buf_alloc(size);
81         if (buf == NULL) {
82                 rc = -ENOMEM;
83                 goto out_sa;
84         }
85         rc = -sa_lookup(hdl, SA_ZPL_DXATTR(osd), buf, size);
86         if (rc == 0)
87                 rc = -nvlist_unpack(buf, size, sa, KM_SLEEP);
88         osd_zio_buf_free(buf, size);
89 out_sa:
90
91         return rc;
92 }
93
94 static inline int __osd_xattr_cache(struct osd_object *obj)
95 {
96         LASSERT(obj->oo_sa_hdl);
97         if (obj->oo_sa_xattr != NULL)
98                 return 0;
99         return __osd_xattr_load(osd_obj2dev(obj),
100                                 obj->oo_sa_hdl, &obj->oo_sa_xattr);
101 }
102
103 static int
104 __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
105                    const struct lu_buf *buf, const char *name, int *sizep)
106 {
107         uchar_t *nv_value;
108         int      rc = 0;
109
110         rc = __osd_xattr_cache(obj);
111         if (rc)
112                 return rc;
113
114         LASSERT(obj->oo_sa_xattr);
115         rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name,
116                                        &nv_value, sizep);
117         if (rc)
118                 return rc;
119
120         if (buf == NULL || buf->lb_buf == NULL) {
121                 /* return the required size by *sizep */
122                 return 0;
123         }
124
125         if (*sizep > buf->lb_len)
126                 return -ERANGE; /* match ldiskfs error */
127
128         memcpy(buf->lb_buf, nv_value, *sizep);
129         return 0;
130 }
131
132 int __osd_xattr_get_large(const struct lu_env *env, struct osd_device *osd,
133                           uint64_t xattr, struct lu_buf *buf,
134                           const char *name, int *sizep)
135 {
136         dnode_t         *xa_data_dn;
137         sa_handle_t *sa_hdl = NULL;
138         uint64_t         xa_data_obj, size;
139         int              rc;
140
141         /* are there any extended attributes? */
142         if (xattr == ZFS_NO_OBJECT)
143                 return -ENOENT;
144
145         /* Lookup the object number containing the xattr data */
146         rc = -zap_lookup(osd->od_os, xattr, name, sizeof(uint64_t), 1,
147                         &xa_data_obj);
148         if (rc)
149                 return rc;
150
151         rc = __osd_obj2dnode(osd->od_os, xa_data_obj, &xa_data_dn);
152         if (rc)
153                 return rc;
154
155         rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL, SA_HDL_PRIVATE,
156                         &sa_hdl);
157         if (rc)
158                 goto out_rele;
159
160         /* Get the xattr value length / object size */
161         rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
162         if (rc)
163                 goto out;
164
165         if (size > INT_MAX) {
166                 rc = -EOVERFLOW;
167                 goto out;
168         }
169
170         *sizep = (int)size;
171
172         if (buf == NULL || buf->lb_buf == NULL) {
173                 /* We only need to return the required size */
174                 goto out;
175         }
176         if (*sizep > buf->lb_len) {
177                 rc = -ERANGE; /* match ldiskfs error */
178                 goto out;
179         }
180
181         rc = -dmu_read(osd->od_os, xa_data_dn->dn_object, 0,
182                         size, buf->lb_buf, DMU_READ_PREFETCH);
183
184 out:
185         sa_handle_destroy(sa_hdl);
186 out_rele:
187         osd_dnode_rele(xa_data_dn);
188
189         return rc;
190 }
191
192 /**
193  * Copy an extended attribute into the buffer provided, or compute
194  * the required buffer size if \a buf is NULL.
195  *
196  * On success, the number of bytes used or required is stored in \a sizep.
197  *
198  * Note that no locking is done here.
199  *
200  * \param[in] env      execution environment
201  * \param[in] obj      object for which to retrieve xattr
202  * \param[out] buf     buffer to store xattr value in
203  * \param[in] name     name of xattr to copy
204  * \param[out] sizep   bytes used or required to store xattr
205  *
206  * \retval 0           on success
207  * \retval negative    negated errno on failure
208  */
209 int osd_xattr_get_internal(const struct lu_env *env, struct osd_object *obj,
210                            struct lu_buf *buf, const char *name, int *sizep)
211 {
212         int rc;
213
214         if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
215                 return -ENOENT;
216
217         /* check SA_ZPL_DXATTR first then fallback to directory xattr */
218         rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
219         if (rc != -ENOENT)
220                 return rc;
221
222         return __osd_xattr_get_large(env, osd_obj2dev(obj), obj->oo_xattr,
223                                      buf, name, sizep);
224 }
225
226 static int osd_get_pfid_from_lma(const struct lu_env *env,
227                                  struct osd_object *obj,
228                                  struct lu_buf *buf, int *sizep)
229 {
230         struct osd_thread_info *info = osd_oti_get(env);
231         struct lustre_ost_attrs *loa =
232                 (struct lustre_ost_attrs *)&info->oti_buf;
233         struct lustre_mdt_attrs *lma = &loa->loa_lma;
234         struct filter_fid *ff;
235         struct ost_layout *ol;
236         struct lu_buf tbuf = {
237                 .lb_buf = loa,
238                 .lb_len = sizeof(info->oti_buf),
239         };
240         int rc;
241         ENTRY;
242
243         CLASSERT(sizeof(info->oti_buf) >= sizeof(*loa));
244         rc = osd_xattr_get_internal(env, obj, &tbuf,
245                                     XATTR_NAME_LMA, sizep);
246         if (rc)
247                 RETURN(rc);
248
249         lustre_loa_swab(loa, true);
250         LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
251
252         *sizep = sizeof(*ff);
253         if (buf->lb_len == 0 || !buf->lb_buf)
254                 RETURN(0);
255
256         if (buf->lb_len < *sizep)
257                 RETURN(-ERANGE);
258
259         ff = buf->lb_buf;
260         ol = &ff->ff_layout;
261         ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
262                                           PFID_STRIPE_IDX_BITS);
263         ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
264         loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
265         fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
266         if (lma->lma_compat & LMAC_COMP_INFO) {
267                 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
268                 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
269                 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
270         } else {
271                 ol->ol_comp_start = 0;
272                 ol->ol_comp_end = 0;
273                 ol->ol_comp_id = 0;
274         }
275
276         RETURN(0);
277 }
278
279 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
280                   struct lu_buf *buf, const char *name)
281 {
282         struct osd_object  *obj  = osd_dt_obj(dt);
283         int                 rc, size = 0;
284         ENTRY;
285
286         LASSERT(obj->oo_dn != NULL);
287         LASSERT(osd_invariant(obj));
288
289         if (!osd_obj2dev(obj)->od_posix_acl &&
290             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
291              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
292                 RETURN(-EOPNOTSUPP);
293
294         down_read(&obj->oo_guard);
295         /* For the OST migrated from ldiskfs, the PFID EA may
296          * be stored in LMA because of ldiskfs inode size. */
297         if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma)
298                 rc = osd_get_pfid_from_lma(env, obj, buf, &size);
299         else
300                 rc = osd_xattr_get_internal(env, obj, buf, name, &size);
301         up_read(&obj->oo_guard);
302
303         if (rc == -ENOENT)
304                 rc = -ENODATA;
305         else if (rc == 0)
306                 rc = size;
307         RETURN(rc);
308 }
309
310 /* the function is used to declare EAs when SA is not supported */
311 void __osd_xattr_declare_legacy(const struct lu_env *env,
312                                 struct osd_object *obj,
313                                 int vallen, const char *name,
314                                 struct osd_thandle *oh)
315 {
316         struct osd_device *osd = osd_obj2dev(obj);
317         dmu_tx_t *tx = oh->ot_tx;
318         uint64_t xa_data_obj;
319         int rc;
320
321         if (obj->oo_xattr == ZFS_NO_OBJECT) {
322                 /* xattr zap + entry */
323                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
324                 /* xattr value obj */
325                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
326                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
327                 return;
328         }
329
330         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
331                         &xa_data_obj);
332         if (rc == 0) {
333                 /*
334                  * Entry already exists.
335                  * We'll truncate the existing object.
336                  */
337                 dmu_tx_hold_bonus(tx, xa_data_obj);
338                 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
339                 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
340         } else if (rc == -ENOENT) {
341                 /*
342                  * Entry doesn't exist, we need to create a new one and a new
343                  * object to store the value.
344                  */
345                 dmu_tx_hold_bonus(tx, obj->oo_xattr);
346                 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
347                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
348                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
349         }
350 }
351
352 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
353                              int vallen, const char *name,
354                              struct osd_thandle *oh)
355 {
356         struct osd_device *osd = osd_obj2dev(obj);
357         dmu_tx_t *tx = oh->ot_tx;
358         int bonuslen;
359
360         if (unlikely(obj->oo_destroyed))
361                 return;
362
363         if (strcmp(name, XATTR_NAME_LINK) == 0 &&
364             osd->od_remote_parent_dir != ZFS_NO_OBJECT) {
365                 /* If some name entry resides on remote MDT, then will create
366                  * agent entry under remote parent. On the other hand, if the
367                  * remote entry will be removed, then related agent entry may
368                  * need to be removed from the remote parent. So there may be
369                  * kinds of cases, let's declare enough credits. The credits
370                  * for create agent entry is enough for remove case. */
371                 osd_tx_hold_zap(tx, osd->od_remote_parent_dir,
372                                 NULL, TRUE, NULL);
373         }
374
375         if (unlikely(!osd_obj2dev(obj)->od_xattr_in_sa)) {
376                 __osd_xattr_declare_legacy(env, obj, vallen, name, oh);
377                 return;
378         }
379
380         /* declare EA in SA */
381         if (dt_object_exists(&obj->oo_dt)) {
382                 LASSERT(obj->oo_sa_hdl);
383                 /* XXX: it should be possible to skip spill
384                  * declaration if specific EA is part of
385                  * bonus and doesn't grow */
386                 dmu_tx_hold_spill(tx, obj->oo_dn->dn_object);
387                 return;
388         }
389
390         bonuslen = osd_obj_bonuslen(obj);
391
392         /* the object doesn't exist, but we've declared bonus
393          * in osd_declare_object_create() yet */
394         if (obj->oo_ea_in_bonus > bonuslen) {
395                 /* spill has been declared already */
396         } else if (obj->oo_ea_in_bonus + vallen > bonuslen) {
397                 /* we're about to exceed bonus, let's declare spill */
398                 dmu_tx_hold_spill(tx, DMU_NEW_OBJECT);
399         }
400         obj->oo_ea_in_bonus += vallen;
401 }
402
403 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
404                           const struct lu_buf *buf, const char *name,
405                           int fl, struct thandle *handle)
406 {
407         struct osd_object  *obj = osd_dt_obj(dt);
408         struct osd_thandle *oh;
409         ENTRY;
410
411         LASSERT(handle != NULL);
412         oh = container_of0(handle, struct osd_thandle, ot_super);
413
414         down_read(&obj->oo_guard);
415         __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
416         up_read(&obj->oo_guard);
417
418         RETURN(0);
419 }
420
421 int __osd_sa_attr_init(const struct lu_env *env, struct osd_object *obj,
422                        struct osd_thandle *oh)
423 {
424         sa_bulk_attr_t  *bulk = osd_oti_get(env)->oti_attr_bulk;
425         struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
426         struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
427         struct osd_device *osd = osd_obj2dev(obj);
428         uint64_t crtime[2], gen;
429         timestruc_t now;
430         size_t size;
431         int rc, cnt;
432
433         obj->oo_late_xattr = 0;
434         obj->oo_late_attr_set = 0;
435
436         gen = dmu_tx_get_txg(oh->ot_tx);
437         gethrestime(&now);
438         ZFS_TIME_ENCODE(&now, crtime);
439
440         osa->atime[0] = obj->oo_attr.la_atime;
441         osa->ctime[0] = obj->oo_attr.la_ctime;
442         osa->mtime[0] = obj->oo_attr.la_mtime;
443         osa->mode = obj->oo_attr.la_mode;
444         osa->uid = obj->oo_attr.la_uid;
445         osa->gid = obj->oo_attr.la_gid;
446         osa->rdev = obj->oo_attr.la_rdev;
447         osa->nlink = obj->oo_attr.la_nlink;
448         osa->flags = attrs_fs2zfs(obj->oo_attr.la_flags);
449         osa->size  = obj->oo_attr.la_size;
450 #ifdef ZFS_PROJINHERIT
451         if (osd->od_projectused_dn) {
452                 if (obj->oo_attr.la_valid & LA_PROJID)
453                         osa->projid = obj->oo_attr.la_projid;
454                 else
455                         osa->projid = ZFS_DEFAULT_PROJID;
456                 osa->flags |= ZFS_PROJID;
457                 obj->oo_with_projid = 1;
458         }
459 #endif
460
461         cnt = 0;
462         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(osd), NULL, &osa->mode, 8);
463         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(osd), NULL, &osa->size, 8);
464         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GEN(osd), NULL, &gen, 8);
465         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(osd), NULL, &osa->uid, 8);
466         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(osd), NULL, &osa->gid, 8);
467         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PARENT(osd), NULL,
468                          &obj->oo_parent, 8);
469         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL, &osa->flags, 8);
470         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL, osa->atime, 16);
471         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL, osa->mtime, 16);
472         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL, osa->ctime, 16);
473         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(osd), NULL, crtime, 16);
474         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL, &osa->nlink, 8);
475 #ifdef ZFS_PROJINHERIT
476         if (osd->od_projectused_dn)
477                 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PROJID(osd), NULL,
478                                  &osa->projid, 8);
479 #endif
480         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL, &osa->rdev, 8);
481         LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
482
483         /* Update the SA for additions, modifications, and removals. */
484         rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
485         if (rc)
486                 return rc;
487
488         lu_buf_check_and_alloc(lb, size);
489         if (lb->lb_buf == NULL) {
490                 CERROR("%s: can't allocate buffer for xattr update\n",
491                                 osd->od_svname);
492                 return -ENOMEM;
493         }
494
495         rc = -nvlist_pack(obj->oo_sa_xattr, (char **)&lb->lb_buf, &size,
496                           NV_ENCODE_XDR, KM_SLEEP);
497         if (rc)
498                 return rc;
499
500         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_DXATTR(osd), NULL, lb->lb_buf, size);
501
502         rc = -sa_replace_all_by_template(obj->oo_sa_hdl, bulk, cnt, oh->ot_tx);
503
504         return rc;
505 }
506
507 int __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
508                            struct osd_thandle *oh)
509 {
510         struct lu_buf     *lb = &osd_oti_get(env)->oti_xattr_lbuf;
511         struct osd_device *osd = osd_obj2dev(obj);
512         char              *dxattr;
513         size_t             size;
514         int                rc;
515
516         obj->oo_late_xattr = 0;
517
518         /* Update the SA for additions, modifications, and removals. */
519         rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
520         if (rc)
521                 return rc;
522
523         lu_buf_check_and_alloc(lb, size);
524         if (lb->lb_buf == NULL) {
525                 CERROR("%s: can't allocate buffer for xattr update\n",
526                                 osd->od_svname);
527                 return -ENOMEM;
528         }
529
530         dxattr = lb->lb_buf;
531         rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &size,
532                         NV_ENCODE_XDR, KM_SLEEP);
533         if (rc)
534                 return rc;
535         LASSERT(dxattr == lb->lb_buf);
536
537         sa_update(obj->oo_sa_hdl, SA_ZPL_DXATTR(osd), dxattr, size, oh->ot_tx);
538
539         return 0;
540 }
541
542 /*
543  * Set an extended attribute.
544  * This transaction must have called udmu_xattr_declare_set() first.
545  *
546  * Returns 0 on success or a negative error number on failure.
547  *
548  * No locking is done here.
549  */
550 int __osd_sa_xattr_schedule_update(const struct lu_env *env,
551                                    struct osd_object *obj,
552                                    struct osd_thandle *oh)
553 {
554         ENTRY;
555         LASSERT(obj->oo_sa_hdl);
556         LASSERT(obj->oo_sa_xattr);
557
558         /* schedule batched SA update in osd_object_sa_dirty_rele() */
559         obj->oo_late_xattr = 1;
560         osd_object_sa_dirty_add(obj, oh);
561
562         RETURN(0);
563
564 }
565
566 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
567                        const struct lu_buf *buf, const char *name, int fl,
568                        struct osd_thandle *oh)
569 {
570         uchar_t *nv_value;
571         size_t  size;
572         int     nv_size;
573         int     rc;
574         int     too_big = 0;
575
576         rc = __osd_xattr_cache(obj);
577         if (rc)
578                 return rc;
579
580         LASSERT(obj->oo_sa_xattr);
581         /* Limited to 32k to keep nvpair memory allocations small */
582         if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
583                 too_big = 1;
584         } else {
585                 /* Prevent the DXATTR SA from consuming the entire SA
586                  * region */
587                 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
588                 if (rc)
589                         return rc;
590
591                 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
592                         too_big = 1;
593         }
594
595         /* even in case of -EFBIG we must lookup xattr and check can we
596          * rewrite it then delete from SA */
597         rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
598                                         &nv_size);
599         if (rc == 0) {
600                 if (fl & LU_XATTR_CREATE) {
601                         return -EEXIST;
602                 } else if (too_big) {
603                         rc = -nvlist_remove(obj->oo_sa_xattr, name,
604                                                 DATA_TYPE_BYTE_ARRAY);
605                         if (rc < 0)
606                                 return rc;
607                         rc = __osd_sa_xattr_schedule_update(env, obj, oh);
608                         return rc == 0 ? -EFBIG : rc;
609                 }
610         } else if (rc == -ENOENT) {
611                 if (fl & LU_XATTR_REPLACE)
612                         return -ENODATA;
613                 else if (too_big)
614                         return -EFBIG;
615         } else {
616                 return rc;
617         }
618
619         /* Ensure xattr doesn't exist in ZAP */
620         if (obj->oo_xattr != ZFS_NO_OBJECT) {
621                 struct osd_device *osd = osd_obj2dev(obj);
622                 uint64_t           objid;
623                 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
624                                  name, 8, 1, &objid);
625                 if (rc == 0) {
626                         rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
627                         if (rc == 0)
628                                 zap_remove(osd->od_os, obj->oo_xattr,
629                                            name, oh->ot_tx);
630                 }
631         }
632
633         rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
634                                     (uchar_t *)buf->lb_buf, buf->lb_len);
635         if (rc)
636                 return rc;
637
638         /* batch updates only for just created dnodes where we
639          * used to set number of EAs in a single transaction */
640         if (obj->oo_dn->dn_allocated_txg == oh->ot_tx->tx_txg)
641                 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
642         else
643                 rc = __osd_sa_xattr_update(env, obj, oh);
644
645         return rc;
646 }
647
648 int
649 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
650                 const struct lu_buf *buf, const char *name, int fl,
651                 struct osd_thandle *oh)
652 {
653         struct osd_device *osd = osd_obj2dev(obj);
654         dnode_t *xa_zap_dn = NULL;
655         dnode_t *xa_data_dn = NULL;
656         uint64_t           xa_data_obj;
657         sa_handle_t       *sa_hdl = NULL;
658         dmu_tx_t          *tx = oh->ot_tx;
659         uint64_t           size;
660         int                rc;
661
662         LASSERT(obj->oo_sa_hdl);
663
664         if (obj->oo_xattr == ZFS_NO_OBJECT) {
665                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
666
667                 la->la_valid = LA_MODE;
668                 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
669                 rc = __osd_zap_create(env, osd, &xa_zap_dn, tx, la, 0, 0);
670                 if (rc)
671                         return rc;
672
673                 obj->oo_xattr = xa_zap_dn->dn_object;
674                 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
675                                 &obj->oo_xattr, 8, oh);
676                 if (rc)
677                         goto out;
678         }
679
680         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
681                          &xa_data_obj);
682         if (rc == 0) {
683                 if (fl & LU_XATTR_CREATE) {
684                         rc = -EEXIST;
685                         goto out;
686                 }
687                 /*
688                  * Entry already exists.
689                  * We'll truncate the existing object.
690                  */
691                 rc = __osd_obj2dnode(osd->od_os, xa_data_obj, &xa_data_dn);
692                 if (rc)
693                         goto out;
694
695                 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
696                                         SA_HDL_PRIVATE, &sa_hdl);
697                 if (rc)
698                         goto out;
699
700                 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
701                 if (rc)
702                         goto out_sa;
703
704                 rc = -dmu_free_range(osd->od_os, xa_data_dn->dn_object,
705                                      0, DMU_OBJECT_END, tx);
706                 if (rc)
707                         goto out_sa;
708         } else if (rc == -ENOENT) {
709                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
710                 /*
711                  * Entry doesn't exist, we need to create a new one and a new
712                  * object to store the value.
713                  */
714                 if (fl & LU_XATTR_REPLACE) {
715                         /* should be ENOATTR according to the
716                          * man, but that is undefined here */
717                         rc = -ENODATA;
718                         goto out;
719                 }
720
721                 la->la_valid = LA_MODE;
722                 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
723                 rc = __osd_object_create(env, osd, obj,
724                                          lu_object_fid(&obj->oo_dt.do_lu),
725                                          &xa_data_dn, tx, la);
726                 if (rc)
727                         goto out;
728                 xa_data_obj = xa_data_dn->dn_object;
729
730                 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
731                                         SA_HDL_PRIVATE, &sa_hdl);
732                 if (rc)
733                         goto out;
734
735                 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
736                                 1, &xa_data_obj, tx);
737                 if (rc)
738                         goto out_sa;
739         } else {
740                 /* There was an error looking up the xattr name */
741                 goto out;
742         }
743
744         /* Finally write the xattr value */
745         dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
746
747         size = buf->lb_len;
748         rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
749
750 out_sa:
751         sa_handle_destroy(sa_hdl);
752 out:
753         if (xa_data_dn != NULL)
754                 osd_dnode_rele(xa_data_dn);
755         if (xa_zap_dn != NULL)
756                 osd_dnode_rele(xa_zap_dn);
757
758         return rc;
759 }
760
761 static int osd_xattr_split_pfid(const struct lu_env *env,
762                                 struct osd_object *obj, struct osd_thandle *oh)
763 {
764         struct osd_thread_info *info = osd_oti_get(env);
765         struct lustre_ost_attrs *loa =
766                 (struct lustre_ost_attrs *)&info->oti_buf;
767         struct lustre_mdt_attrs *lma = &loa->loa_lma;
768         struct lu_buf buf = {
769                 .lb_buf = loa,
770                 .lb_len = sizeof(info->oti_buf),
771         };
772         int size;
773         int rc;
774         ENTRY;
775
776         CLASSERT(sizeof(info->oti_buf) >= sizeof(*loa));
777         rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
778         if (rc)
779                 RETURN(rc);
780
781         lustre_loa_swab(loa, true);
782         LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
783
784         lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
785         lustre_lma_swab(lma);
786         buf.lb_buf = lma;
787         buf.lb_len = sizeof(*lma);
788         rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
789                                     LU_XATTR_REPLACE, oh);
790         if (!rc)
791                 obj->oo_pfid_in_lma = 0;
792
793         RETURN(rc);
794 }
795
796 /*
797  * In DNE environment, the object (in spite of regular file or directory)
798  * and its name entry may reside on different MDTs. Under such case, we will
799  * create an agent entry on the MDT where the object resides. The agent entry
800  * references the object locally, that makes the object to be visible to the
801  * userspace when mounted as 'zfs' directly. Then the userspace tools, such
802  * as 'tar' can handle the object properly.
803  *
804  * We handle the agent entry during set linkEA that is the common interface
805  * for both regular file and directroy, can handle kinds of cases, such as
806  * create/link/unlink/rename, and so on.
807  *
808  * NOTE: we need to do that for both directory and regular file, so we can NOT
809  *       do that when ea_{insert,delete} that are directory based operations.
810  */
811 static int osd_xattr_handle_linkea(const struct lu_env *env,
812                                    struct osd_device *osd,
813                                    struct osd_object *obj,
814                                    const struct lu_buf *buf,
815                                    struct osd_thandle *oh)
816 {
817         const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
818         struct lu_fid *tfid = &osd_oti_get(env)->oti_fid;
819         struct linkea_data ldata = { .ld_buf = (struct lu_buf *)buf };
820         struct lu_name tmpname;
821         int rc;
822         bool remote = false;
823         ENTRY;
824
825         rc = linkea_init_with_rec(&ldata);
826         if (!rc) {
827                 linkea_first_entry(&ldata);
828                 while (ldata.ld_lee != NULL && !remote) {
829                         linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
830                                             &tmpname, tfid);
831                         if (osd_remote_fid(env, osd, tfid) > 0)
832                                 remote = true;
833                         else
834                                 linkea_next_entry(&ldata);
835                 }
836         } else if (rc == -ENODATA) {
837                 rc = 0;
838         } else {
839                 RETURN(rc);
840         }
841
842         if (lu_object_has_agent_entry(&obj->oo_dt.do_lu) && !remote) {
843                 rc = osd_delete_from_remote_parent(env, osd, obj, oh, false);
844                 if (rc)
845                         CERROR("%s: failed to remove agent entry for "DFID
846                                ": rc = %d\n", osd_name(osd), PFID(fid), rc);
847         } else if (!lu_object_has_agent_entry(&obj->oo_dt.do_lu) && remote) {
848                 rc = osd_add_to_remote_parent(env, osd, obj, oh);
849                 if (rc)
850                         CWARN("%s: failed to create agent entry for "DFID
851                               ": rc = %d\n", osd_name(osd), PFID(fid), rc);
852         }
853
854         RETURN(rc);
855 }
856
857 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
858                   const struct lu_buf *buf, const char *name, int fl,
859                   struct thandle *handle)
860 {
861         struct osd_object *obj = osd_dt_obj(dt);
862         struct osd_device *osd = osd_obj2dev(obj);
863         struct osd_thandle *oh;
864         int rc = 0;
865         ENTRY;
866
867         LASSERT(handle != NULL);
868         LASSERT(osd_invariant(obj));
869
870         if (!osd_obj2dev(obj)->od_posix_acl &&
871             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
872              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
873                 RETURN(-EOPNOTSUPP);
874
875         oh = container_of0(handle, struct osd_thandle, ot_super);
876
877         down_write(&obj->oo_guard);
878         CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
879                 name, (int)buf->lb_len);
880         /* For the OST migrated from ldiskfs, the PFID EA may
881          * be stored in LMA because of ldiskfs inode size. */
882         if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 &&
883                      obj->oo_pfid_in_lma)) {
884                 rc = osd_xattr_split_pfid(env, obj, oh);
885                 if (!rc)
886                         fl = LU_XATTR_CREATE;
887         } else if (strcmp(name, XATTR_NAME_LINK) == 0 &&
888                    osd->od_remote_parent_dir != ZFS_NO_OBJECT) {
889                 rc = osd_xattr_handle_linkea(env, osd, obj, buf, oh);
890         }
891
892         if (!rc)
893                 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
894         up_write(&obj->oo_guard);
895
896         RETURN(rc);
897 }
898
899 static void
900 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
901                         const char *name, struct osd_thandle *oh)
902 {
903         struct osd_device *osd = osd_obj2dev(obj);
904         dmu_tx_t          *tx = oh->ot_tx;
905         uint64_t           xa_data_obj;
906         int                rc;
907
908         /* update SA_ZPL_DXATTR if xattr was in SA */
909         dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
910
911         if (obj->oo_xattr == ZFS_NO_OBJECT)
912                 return;
913
914         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
915         if (rc == 0) {
916                 /*
917                  * Entry exists.
918                  * We'll delete the existing object and ZAP entry.
919                  */
920                 dmu_tx_hold_bonus(tx, xa_data_obj);
921                 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
922                 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
923                 return;
924         } else if (rc == -ENOENT) {
925                 /*
926                  * Entry doesn't exist, nothing to be changed.
927                  */
928                 return;
929         }
930
931         /* An error happened */
932         tx->tx_err = -rc;
933 }
934
935 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
936                           const char *name, struct thandle *handle)
937 {
938         struct osd_object  *obj = osd_dt_obj(dt);
939         struct osd_thandle *oh;
940         ENTRY;
941
942         LASSERT(handle != NULL);
943         LASSERT(osd_invariant(obj));
944
945         oh = container_of0(handle, struct osd_thandle, ot_super);
946         LASSERT(oh->ot_tx != NULL);
947         LASSERT(obj->oo_dn != NULL);
948
949         down_read(&obj->oo_guard);
950         if (likely(dt_object_exists(&obj->oo_dt) && !obj->oo_destroyed))
951                 __osd_xattr_declare_del(env, obj, name, oh);
952         up_read(&obj->oo_guard);
953
954         RETURN(0);
955 }
956
957 static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
958                               const char *name, struct osd_thandle *oh)
959 {
960         int rc;
961
962         rc = __osd_xattr_cache(obj);
963         if (rc)
964                 return rc;
965
966         rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
967         if (rc == 0)
968                 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
969         return rc;
970 }
971
972 static int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
973                            const char *name, struct osd_thandle *oh)
974 {
975         struct osd_device *osd = osd_obj2dev(obj);
976         uint64_t           xa_data_obj;
977         int                rc;
978
979         if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
980                 return -ENOENT;
981
982         /* try remove xattr from SA at first */
983         rc = __osd_sa_xattr_del(env, obj, name, oh);
984         if (rc != -ENOENT)
985                 return rc;
986
987         if (obj->oo_xattr == ZFS_NO_OBJECT)
988                 return 0;
989
990         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
991                         &xa_data_obj);
992         if (rc == -ENOENT) {
993                 rc = 0;
994         } else if (rc == 0) {
995                 /*
996                  * Entry exists.
997                  * We'll delete the existing object and ZAP entry.
998                  */
999                 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
1000                 if (rc)
1001                         return rc;
1002
1003                 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
1004         }
1005
1006         return rc;
1007 }
1008
1009 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
1010                   const char *name, struct thandle *handle)
1011 {
1012         struct osd_object  *obj = osd_dt_obj(dt);
1013         struct osd_thandle *oh;
1014         int                 rc;
1015         ENTRY;
1016
1017         LASSERT(handle != NULL);
1018         LASSERT(obj->oo_dn != NULL);
1019         LASSERT(osd_invariant(obj));
1020         LASSERT(dt_object_exists(dt));
1021         oh = container_of0(handle, struct osd_thandle, ot_super);
1022         LASSERT(oh->ot_tx != NULL);
1023
1024         if (!osd_obj2dev(obj)->od_posix_acl &&
1025             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1026              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
1027                 RETURN(-EOPNOTSUPP);
1028
1029         down_write(&obj->oo_guard);
1030         /* For the OST migrated from ldiskfs, the PFID EA may
1031          * be stored in LMA because of ldiskfs inode size. */
1032         if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma))
1033                 rc = osd_xattr_split_pfid(env, obj, oh);
1034         else
1035                 rc = __osd_xattr_del(env, obj, name, oh);
1036         up_write(&obj->oo_guard);
1037
1038         RETURN(rc);
1039 }
1040
1041 void osd_declare_xattrs_destroy(const struct lu_env *env,
1042                                 struct osd_object *obj, struct osd_thandle *oh)
1043 {
1044         struct osd_device *osd = osd_obj2dev(obj);
1045         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1046         uint64_t           oid = obj->oo_xattr, xid;
1047         dmu_tx_t          *tx = oh->ot_tx;
1048         zap_cursor_t      *zc;
1049         int                rc;
1050
1051         if (oid == ZFS_NO_OBJECT)
1052                 return; /* Nothing to do for SA xattrs */
1053
1054         /* Declare to free the ZAP holding xattrs */
1055         dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
1056
1057         rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
1058         if (rc)
1059                 goto out;
1060
1061         while (zap_cursor_retrieve(zc, za) == 0) {
1062                 LASSERT(za->za_num_integers == 1);
1063                 LASSERT(za->za_integer_length == sizeof(uint64_t));
1064
1065                 rc = -zap_lookup(osd->od_os, oid, za->za_name,
1066                                  sizeof(uint64_t), 1, &xid);
1067                 if (rc) {
1068                         CERROR("%s: xattr %s lookup failed: rc = %d\n",
1069                                osd->od_svname, za->za_name, rc);
1070                         break;
1071                 }
1072                 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
1073
1074                 zap_cursor_advance(zc);
1075         }
1076
1077         osd_zap_cursor_fini(zc);
1078 out:
1079         if (rc && tx->tx_err == 0)
1080                 tx->tx_err = -rc;
1081 }
1082
1083 int osd_xattrs_destroy(const struct lu_env *env,
1084                        struct osd_object *obj, struct osd_thandle *oh)
1085 {
1086         struct osd_device *osd = osd_obj2dev(obj);
1087         dmu_tx_t          *tx = oh->ot_tx;
1088         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1089         zap_cursor_t      *zc;
1090         uint64_t           xid;
1091         int                rc;
1092
1093         /* The transaction must have been assigned to a transaction group. */
1094         LASSERT(tx->tx_txg != 0);
1095
1096         if (obj->oo_xattr == ZFS_NO_OBJECT)
1097                 return 0; /* Nothing to do for SA xattrs */
1098
1099         /* Free the ZAP holding the xattrs */
1100         rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1101         if (rc)
1102                 return rc;
1103
1104         while (zap_cursor_retrieve(zc, za) == 0) {
1105                 LASSERT(za->za_num_integers == 1);
1106                 LASSERT(za->za_integer_length == sizeof(uint64_t));
1107
1108                 rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
1109                                  sizeof(uint64_t), 1, &xid);
1110                 if (rc) {
1111                         CERROR("%s: lookup xattr %s failed: rc = %d\n",
1112                                osd->od_svname, za->za_name, rc);
1113                 } else {
1114                         rc = -dmu_object_free(osd->od_os, xid, tx);
1115                         if (rc)
1116                                 CERROR("%s: free xattr %s failed: rc = %d\n",
1117                                        osd->od_svname, za->za_name, rc);
1118                 }
1119                 zap_cursor_advance(zc);
1120         }
1121         osd_zap_cursor_fini(zc);
1122
1123         rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
1124         if (rc)
1125                 CERROR("%s: free xattr %llu failed: rc = %d\n",
1126                        osd->od_svname, obj->oo_xattr, rc);
1127
1128         return rc;
1129 }
1130
1131 static int
1132 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
1133                   const struct lu_buf *lb)
1134 {
1135         nvpair_t *nvp = NULL;
1136         int       len, counted = 0;
1137         int       rc = 0;
1138
1139         rc = __osd_xattr_cache(obj);
1140         if (rc)
1141                 return rc;
1142
1143         while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
1144                 const char *name = nvpair_name(nvp);
1145
1146                 if (!osd_obj2dev(obj)->od_posix_acl &&
1147                     (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1148                      strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
1149                         continue;
1150
1151                 len = strlen(name);
1152                 if (lb->lb_buf != NULL) {
1153                         if (counted + len + 1 > lb->lb_len)
1154                                 return -ERANGE;
1155
1156                         memcpy(lb->lb_buf + counted, name, len + 1);
1157                 }
1158                 counted += len + 1;
1159         }
1160         return counted;
1161 }
1162
1163 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
1164                    const struct lu_buf *lb)
1165 {
1166         struct osd_object      *obj = osd_dt_obj(dt);
1167         struct osd_device      *osd = osd_obj2dev(obj);
1168         zap_attribute_t        *za = &osd_oti_get(env)->oti_za;
1169         zap_cursor_t           *zc;
1170         int                    rc, counted;
1171         ENTRY;
1172
1173         LASSERT(obj->oo_dn != NULL);
1174         LASSERT(osd_invariant(obj));
1175         LASSERT(dt_object_exists(dt));
1176
1177         down_read(&obj->oo_guard);
1178
1179         rc = osd_sa_xattr_list(env, obj, lb);
1180         if (rc < 0)
1181                 GOTO(out, rc);
1182
1183         counted = rc;
1184
1185         /* continue with dnode xattr if any */
1186         if (obj->oo_xattr == ZFS_NO_OBJECT)
1187                 GOTO(out, rc = counted);
1188
1189         rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1190         if (rc)
1191                 GOTO(out, rc);
1192
1193         while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
1194                 if (!osd_obj2dev(obj)->od_posix_acl &&
1195                     (strcmp(za->za_name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1196                      strcmp(za->za_name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) {
1197                         zap_cursor_advance(zc);
1198                         continue;
1199                 }
1200
1201                 rc = strlen(za->za_name);
1202                 if (lb->lb_buf != NULL) {
1203                         if (counted + rc + 1 > lb->lb_len)
1204                                 GOTO(out_fini, rc = -ERANGE);
1205
1206                         memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
1207                 }
1208                 counted += rc + 1;
1209
1210                 zap_cursor_advance(zc);
1211         }
1212         if (rc == -ENOENT) /* no more kes in the index */
1213                 rc = 0;
1214         else if (unlikely(rc < 0))
1215                 GOTO(out_fini, rc);
1216         rc = counted;
1217
1218 out_fini:
1219         osd_zap_cursor_fini(zc);
1220 out:
1221         up_read(&obj->oo_guard);
1222         RETURN(rc);
1223
1224 }