Whamcloud - gitweb
LU-10467 ptlrpc: convert final users of LWI_TIMEOUT_INTERVAL
[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         BUILD_BUG_ON(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         if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed)) {
296                 up_read(&obj->oo_guard);
297                 RETURN(-ENOENT);
298         }
299
300         /* For the OST migrated from ldiskfs, the PFID EA may
301          * be stored in LMA because of ldiskfs inode size. */
302         if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma)
303                 rc = osd_get_pfid_from_lma(env, obj, buf, &size);
304         else
305                 rc = osd_xattr_get_internal(env, obj, buf, name, &size);
306         up_read(&obj->oo_guard);
307
308         if (rc == -ENOENT)
309                 rc = -ENODATA;
310         else if (rc == 0)
311                 rc = size;
312         RETURN(rc);
313 }
314
315 /* the function is used to declare EAs when SA is not supported */
316 void __osd_xattr_declare_legacy(const struct lu_env *env,
317                                 struct osd_object *obj,
318                                 int vallen, const char *name,
319                                 struct osd_thandle *oh)
320 {
321         struct osd_device *osd = osd_obj2dev(obj);
322         dmu_tx_t *tx = oh->ot_tx;
323         uint64_t xa_data_obj;
324         int rc;
325
326         if (obj->oo_xattr == ZFS_NO_OBJECT) {
327                 /* xattr zap + entry */
328                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
329                 /* xattr value obj */
330                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
331                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
332                 return;
333         }
334
335         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
336                         &xa_data_obj);
337         if (rc == 0) {
338                 /*
339                  * Entry already exists.
340                  * We'll truncate the existing object.
341                  */
342                 dmu_tx_hold_bonus(tx, xa_data_obj);
343                 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
344                 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
345         } else if (rc == -ENOENT) {
346                 /*
347                  * Entry doesn't exist, we need to create a new one and a new
348                  * object to store the value.
349                  */
350                 dmu_tx_hold_bonus(tx, obj->oo_xattr);
351                 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
352                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
353                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
354         }
355 }
356
357 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
358                              int vallen, const char *name,
359                              struct osd_thandle *oh)
360 {
361         struct osd_device *osd = osd_obj2dev(obj);
362         dmu_tx_t *tx = oh->ot_tx;
363         int bonuslen;
364
365         if (unlikely(obj->oo_destroyed))
366                 return;
367
368         if (strcmp(name, XATTR_NAME_LINK) == 0 &&
369             osd->od_remote_parent_dir != ZFS_NO_OBJECT) {
370                 /* If some name entry resides on remote MDT, then will create
371                  * agent entry under remote parent. On the other hand, if the
372                  * remote entry will be removed, then related agent entry may
373                  * need to be removed from the remote parent. So there may be
374                  * kinds of cases, let's declare enough credits. The credits
375                  * for create agent entry is enough for remove case. */
376                 osd_tx_hold_zap(tx, osd->od_remote_parent_dir,
377                                 NULL, TRUE, NULL);
378         }
379
380         if (unlikely(!osd_obj2dev(obj)->od_xattr_in_sa)) {
381                 __osd_xattr_declare_legacy(env, obj, vallen, name, oh);
382                 return;
383         }
384
385         /* declare EA in SA */
386         if (dt_object_exists(&obj->oo_dt)) {
387                 LASSERT(obj->oo_sa_hdl);
388                 /* XXX: it should be possible to skip spill
389                  * declaration if specific EA is part of
390                  * bonus and doesn't grow */
391                 dmu_tx_hold_spill(tx, obj->oo_dn->dn_object);
392                 return;
393         }
394
395         bonuslen = osd_obj_bonuslen(obj);
396
397         /* the object doesn't exist, but we've declared bonus
398          * in osd_declare_object_create() yet */
399         if (obj->oo_ea_in_bonus > bonuslen) {
400                 /* spill has been declared already */
401         } else if (obj->oo_ea_in_bonus + vallen > bonuslen) {
402                 /* we're about to exceed bonus, let's declare spill */
403                 dmu_tx_hold_spill(tx, DMU_NEW_OBJECT);
404         }
405         obj->oo_ea_in_bonus += vallen;
406 }
407
408 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
409                           const struct lu_buf *buf, const char *name,
410                           int fl, struct thandle *handle)
411 {
412         struct osd_object  *obj = osd_dt_obj(dt);
413         struct osd_thandle *oh;
414         ENTRY;
415
416         LASSERT(handle != NULL);
417         oh = container_of0(handle, struct osd_thandle, ot_super);
418
419         down_read(&obj->oo_guard);
420         __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
421         up_read(&obj->oo_guard);
422
423         RETURN(0);
424 }
425
426 int __osd_sa_attr_init(const struct lu_env *env, struct osd_object *obj,
427                        struct osd_thandle *oh)
428 {
429         sa_bulk_attr_t *bulk = osd_oti_get(env)->oti_attr_bulk;
430         struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
431         struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
432         struct osd_device *osd = osd_obj2dev(obj);
433         uint64_t crtime[2], gen;
434         inode_timespec_t now;
435         size_t size;
436         int rc, cnt;
437
438         obj->oo_late_xattr = 0;
439         obj->oo_late_attr_set = 0;
440
441         gen = dmu_tx_get_txg(oh->ot_tx);
442         gethrestime(&now);
443         ZFS_TIME_ENCODE(&now, crtime);
444
445         osa->atime[0] = obj->oo_attr.la_atime;
446         osa->ctime[0] = obj->oo_attr.la_ctime;
447         osa->mtime[0] = obj->oo_attr.la_mtime;
448         osa->mode = obj->oo_attr.la_mode;
449         osa->uid = obj->oo_attr.la_uid;
450         osa->gid = obj->oo_attr.la_gid;
451         osa->rdev = obj->oo_attr.la_rdev;
452         osa->nlink = obj->oo_attr.la_nlink;
453         osa->flags = attrs_fs2zfs(obj->oo_attr.la_flags);
454         osa->size  = obj->oo_attr.la_size;
455 #ifdef ZFS_PROJINHERIT
456         if (osd->od_projectused_dn) {
457                 if (obj->oo_attr.la_valid & LA_PROJID)
458                         osa->projid = obj->oo_attr.la_projid;
459                 else
460                         osa->projid = ZFS_DEFAULT_PROJID;
461                 osa->flags |= ZFS_PROJID;
462                 obj->oo_with_projid = 1;
463         }
464 #endif
465
466         cnt = 0;
467         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(osd), NULL, &osa->mode, 8);
468         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(osd), NULL, &osa->size, 8);
469         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GEN(osd), NULL, &gen, 8);
470         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(osd), NULL, &osa->uid, 8);
471         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(osd), NULL, &osa->gid, 8);
472         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PARENT(osd), NULL,
473                          &obj->oo_parent, 8);
474         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL, &osa->flags, 8);
475         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL, osa->atime, 16);
476         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL, osa->mtime, 16);
477         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL, osa->ctime, 16);
478         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(osd), NULL, crtime, 16);
479         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL, &osa->nlink, 8);
480 #ifdef ZFS_PROJINHERIT
481         if (osd->od_projectused_dn)
482                 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PROJID(osd), NULL,
483                                  &osa->projid, 8);
484 #endif
485         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL, &osa->rdev, 8);
486         LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
487
488         /* Update the SA for additions, modifications, and removals. */
489         rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
490         if (rc)
491                 return rc;
492
493         lu_buf_check_and_alloc(lb, size);
494         if (lb->lb_buf == NULL) {
495                 CERROR("%s: can't allocate buffer for xattr update\n",
496                                 osd->od_svname);
497                 return -ENOMEM;
498         }
499
500         rc = -nvlist_pack(obj->oo_sa_xattr, (char **)&lb->lb_buf, &size,
501                           NV_ENCODE_XDR, KM_SLEEP);
502         if (rc)
503                 return rc;
504
505         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_DXATTR(osd), NULL, lb->lb_buf, size);
506
507         rc = -sa_replace_all_by_template(obj->oo_sa_hdl, bulk, cnt, oh->ot_tx);
508
509         return rc;
510 }
511
512 int __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
513                            struct osd_thandle *oh)
514 {
515         struct lu_buf     *lb = &osd_oti_get(env)->oti_xattr_lbuf;
516         struct osd_device *osd = osd_obj2dev(obj);
517         char              *dxattr;
518         size_t             size;
519         int                rc;
520
521         obj->oo_late_xattr = 0;
522
523         /* Update the SA for additions, modifications, and removals. */
524         rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
525         if (rc)
526                 return rc;
527
528         lu_buf_check_and_alloc(lb, size);
529         if (lb->lb_buf == NULL) {
530                 CERROR("%s: can't allocate buffer for xattr update\n",
531                                 osd->od_svname);
532                 return -ENOMEM;
533         }
534
535         dxattr = lb->lb_buf;
536         rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &size,
537                         NV_ENCODE_XDR, KM_SLEEP);
538         if (rc)
539                 return rc;
540         LASSERT(dxattr == lb->lb_buf);
541
542         sa_update(obj->oo_sa_hdl, SA_ZPL_DXATTR(osd), dxattr, size, oh->ot_tx);
543
544         return 0;
545 }
546
547 /*
548  * Set an extended attribute.
549  * This transaction must have called udmu_xattr_declare_set() first.
550  *
551  * Returns 0 on success or a negative error number on failure.
552  *
553  * No locking is done here.
554  */
555 int __osd_sa_xattr_schedule_update(const struct lu_env *env,
556                                    struct osd_object *obj,
557                                    struct osd_thandle *oh)
558 {
559         ENTRY;
560         LASSERT(obj->oo_sa_hdl);
561         LASSERT(obj->oo_sa_xattr);
562
563         /* schedule batched SA update in osd_object_sa_dirty_rele() */
564         obj->oo_late_xattr = 1;
565         osd_object_sa_dirty_add(obj, oh);
566
567         RETURN(0);
568
569 }
570
571 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
572                        const struct lu_buf *buf, const char *name, int fl,
573                        struct osd_thandle *oh)
574 {
575         uchar_t *nv_value;
576         size_t  size;
577         int     nv_size;
578         int     rc;
579         int     too_big = 0;
580
581         rc = __osd_xattr_cache(obj);
582         if (rc)
583                 return rc;
584
585         LASSERT(obj->oo_sa_xattr);
586         if (buf->lb_len > OBD_MAX_EA_SIZE) {
587                 too_big = 1;
588         } else {
589                 /* Prevent the DXATTR SA from consuming the entire SA
590                  * region */
591                 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
592                 if (rc)
593                         return rc;
594
595                 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
596                         too_big = 1;
597         }
598
599         /* even in case of -EFBIG we must lookup xattr and check can we
600          * rewrite it then delete from SA */
601         rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
602                                         &nv_size);
603         if (rc == 0) {
604                 if (fl & LU_XATTR_CREATE) {
605                         return -EEXIST;
606                 } else if (too_big) {
607                         rc = -nvlist_remove(obj->oo_sa_xattr, name,
608                                                 DATA_TYPE_BYTE_ARRAY);
609                         if (rc < 0)
610                                 return rc;
611                         rc = __osd_sa_xattr_schedule_update(env, obj, oh);
612                         return rc == 0 ? -EFBIG : rc;
613                 }
614         } else if (rc == -ENOENT) {
615                 if (fl & LU_XATTR_REPLACE)
616                         return -ENODATA;
617                 else if (too_big)
618                         return -EFBIG;
619         } else {
620                 return rc;
621         }
622
623         /* Ensure xattr doesn't exist in ZAP */
624         if (obj->oo_xattr != ZFS_NO_OBJECT) {
625                 struct osd_device *osd = osd_obj2dev(obj);
626                 uint64_t           objid;
627                 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
628                                  name, 8, 1, &objid);
629                 if (rc == 0) {
630                         rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
631                         if (rc == 0)
632                                 zap_remove(osd->od_os, obj->oo_xattr,
633                                            name, oh->ot_tx);
634                 }
635         }
636
637         rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
638                                     (uchar_t *)buf->lb_buf, buf->lb_len);
639         if (rc)
640                 return rc;
641
642         /* batch updates only for just created dnodes where we
643          * used to set number of EAs in a single transaction */
644         if (obj->oo_dn->dn_allocated_txg == oh->ot_tx->tx_txg)
645                 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
646         else
647                 rc = __osd_sa_xattr_update(env, obj, oh);
648
649         return rc;
650 }
651
652 int
653 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
654                 const struct lu_buf *buf, const char *name, int fl,
655                 struct osd_thandle *oh)
656 {
657         struct osd_device *osd = osd_obj2dev(obj);
658         dnode_t *xa_zap_dn = NULL;
659         dnode_t *xa_data_dn = NULL;
660         uint64_t           xa_data_obj;
661         sa_handle_t       *sa_hdl = NULL;
662         dmu_tx_t          *tx = oh->ot_tx;
663         uint64_t           size;
664         int                rc;
665
666         LASSERT(obj->oo_sa_hdl);
667
668         if (obj->oo_xattr == ZFS_NO_OBJECT) {
669                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
670
671                 la->la_valid = LA_MODE;
672                 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
673                 rc = __osd_zap_create(env, osd, &xa_zap_dn, tx, la, 0, 0);
674                 if (rc)
675                         return rc;
676
677                 obj->oo_xattr = xa_zap_dn->dn_object;
678                 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
679                                 &obj->oo_xattr, 8, oh);
680                 if (rc)
681                         goto out;
682         }
683
684         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
685                          &xa_data_obj);
686         if (rc == 0) {
687                 if (fl & LU_XATTR_CREATE) {
688                         rc = -EEXIST;
689                         goto out;
690                 }
691                 /*
692                  * Entry already exists.
693                  * We'll truncate the existing object.
694                  */
695                 rc = __osd_obj2dnode(osd->od_os, xa_data_obj, &xa_data_dn);
696                 if (rc)
697                         goto out;
698
699                 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
700                                         SA_HDL_PRIVATE, &sa_hdl);
701                 if (rc)
702                         goto out;
703
704                 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
705                 if (rc)
706                         goto out_sa;
707
708                 rc = -dmu_free_range(osd->od_os, xa_data_dn->dn_object,
709                                      0, DMU_OBJECT_END, tx);
710                 if (rc)
711                         goto out_sa;
712         } else if (rc == -ENOENT) {
713                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
714                 /*
715                  * Entry doesn't exist, we need to create a new one and a new
716                  * object to store the value.
717                  */
718                 if (fl & LU_XATTR_REPLACE) {
719                         /* should be ENOATTR according to the
720                          * man, but that is undefined here */
721                         rc = -ENODATA;
722                         goto out;
723                 }
724
725                 la->la_valid = LA_MODE;
726                 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
727                 rc = __osd_object_create(env, osd, obj,
728                                          lu_object_fid(&obj->oo_dt.do_lu),
729                                          &xa_data_dn, tx, la);
730                 if (rc)
731                         goto out;
732                 xa_data_obj = xa_data_dn->dn_object;
733
734                 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
735                                         SA_HDL_PRIVATE, &sa_hdl);
736                 if (rc)
737                         goto out;
738
739                 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
740                                 1, &xa_data_obj, tx);
741                 if (rc)
742                         goto out_sa;
743         } else {
744                 /* There was an error looking up the xattr name */
745                 goto out;
746         }
747
748         /* Finally write the xattr value */
749         dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
750
751         size = buf->lb_len;
752         rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
753
754 out_sa:
755         sa_handle_destroy(sa_hdl);
756 out:
757         if (xa_data_dn != NULL)
758                 osd_dnode_rele(xa_data_dn);
759         if (xa_zap_dn != NULL)
760                 osd_dnode_rele(xa_zap_dn);
761
762         return rc;
763 }
764
765 static int osd_xattr_split_pfid(const struct lu_env *env,
766                                 struct osd_object *obj, struct osd_thandle *oh)
767 {
768         struct osd_thread_info *info = osd_oti_get(env);
769         struct lustre_ost_attrs *loa =
770                 (struct lustre_ost_attrs *)&info->oti_buf;
771         struct lustre_mdt_attrs *lma = &loa->loa_lma;
772         struct lu_buf buf = {
773                 .lb_buf = loa,
774                 .lb_len = sizeof(info->oti_buf),
775         };
776         int size;
777         int rc;
778         ENTRY;
779
780         BUILD_BUG_ON(sizeof(info->oti_buf) < sizeof(*loa));
781         rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
782         if (rc)
783                 RETURN(rc);
784
785         lustre_loa_swab(loa, true);
786         LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
787
788         lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
789         lustre_lma_swab(lma);
790         buf.lb_buf = lma;
791         buf.lb_len = sizeof(*lma);
792         rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
793                                     LU_XATTR_REPLACE, oh);
794         if (!rc)
795                 obj->oo_pfid_in_lma = 0;
796
797         RETURN(rc);
798 }
799
800 /*
801  * In DNE environment, the object (in spite of regular file or directory)
802  * and its name entry may reside on different MDTs. Under such case, we will
803  * create an agent entry on the MDT where the object resides. The agent entry
804  * references the object locally, that makes the object to be visible to the
805  * userspace when mounted as 'zfs' directly. Then the userspace tools, such
806  * as 'tar' can handle the object properly.
807  *
808  * We handle the agent entry during set linkEA that is the common interface
809  * for both regular file and directroy, can handle kinds of cases, such as
810  * create/link/unlink/rename, and so on.
811  *
812  * NOTE: we need to do that for both directory and regular file, so we can NOT
813  *       do that when ea_{insert,delete} that are directory based operations.
814  */
815 static int osd_xattr_handle_linkea(const struct lu_env *env,
816                                    struct osd_device *osd,
817                                    struct osd_object *obj,
818                                    const struct lu_buf *buf,
819                                    struct osd_thandle *oh)
820 {
821         const struct lu_fid *fid = lu_object_fid(&obj->oo_dt.do_lu);
822         struct lu_fid *tfid = &osd_oti_get(env)->oti_fid;
823         struct linkea_data ldata = { .ld_buf = (struct lu_buf *)buf };
824         struct lu_name tmpname;
825         int rc;
826         bool remote = false;
827         ENTRY;
828
829         rc = linkea_init_with_rec(&ldata);
830         if (!rc) {
831                 linkea_first_entry(&ldata);
832                 while (ldata.ld_lee != NULL && !remote) {
833                         linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
834                                             &tmpname, tfid);
835                         if (osd_remote_fid(env, osd, tfid) > 0)
836                                 remote = true;
837                         else
838                                 linkea_next_entry(&ldata);
839                 }
840         } else if (rc == -ENODATA) {
841                 rc = 0;
842         } else {
843                 RETURN(rc);
844         }
845
846         if (lu_object_has_agent_entry(&obj->oo_dt.do_lu) && !remote) {
847                 rc = osd_delete_from_remote_parent(env, osd, obj, oh, false);
848                 if (rc)
849                         CERROR("%s: failed to remove agent entry for "DFID
850                                ": rc = %d\n", osd_name(osd), PFID(fid), rc);
851         } else if (!lu_object_has_agent_entry(&obj->oo_dt.do_lu) && remote) {
852                 rc = osd_add_to_remote_parent(env, osd, obj, oh);
853                 if (rc)
854                         CWARN("%s: failed to create agent entry for "DFID
855                               ": rc = %d\n", osd_name(osd), PFID(fid), rc);
856         }
857
858         RETURN(rc);
859 }
860
861 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
862                   const struct lu_buf *buf, const char *name, int fl,
863                   struct thandle *handle)
864 {
865         struct osd_object *obj = osd_dt_obj(dt);
866         struct osd_device *osd = osd_obj2dev(obj);
867         struct osd_thandle *oh;
868         int rc = 0;
869         ENTRY;
870
871         LASSERT(handle != NULL);
872         LASSERT(osd_invariant(obj));
873
874         if (!osd_obj2dev(obj)->od_posix_acl &&
875             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
876              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
877                 RETURN(-EOPNOTSUPP);
878
879         oh = container_of0(handle, struct osd_thandle, ot_super);
880
881         down_write(&obj->oo_guard);
882         CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
883                 name, (int)buf->lb_len);
884         /* For the OST migrated from ldiskfs, the PFID EA may
885          * be stored in LMA because of ldiskfs inode size. */
886         if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 &&
887                      obj->oo_pfid_in_lma)) {
888                 rc = osd_xattr_split_pfid(env, obj, oh);
889                 if (!rc)
890                         fl = LU_XATTR_CREATE;
891         } else if (strcmp(name, XATTR_NAME_LINK) == 0 &&
892                    osd->od_remote_parent_dir != ZFS_NO_OBJECT) {
893                 rc = osd_xattr_handle_linkea(env, osd, obj, buf, oh);
894         }
895
896         if (!rc)
897                 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
898         up_write(&obj->oo_guard);
899
900         RETURN(rc);
901 }
902
903 static void
904 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
905                         const char *name, struct osd_thandle *oh)
906 {
907         struct osd_device *osd = osd_obj2dev(obj);
908         dmu_tx_t          *tx = oh->ot_tx;
909         uint64_t           xa_data_obj;
910         int                rc;
911
912         /* update SA_ZPL_DXATTR if xattr was in SA */
913         dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
914
915         if (obj->oo_xattr == ZFS_NO_OBJECT)
916                 return;
917
918         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
919         if (rc == 0) {
920                 /*
921                  * Entry exists.
922                  * We'll delete the existing object and ZAP entry.
923                  */
924                 dmu_tx_hold_bonus(tx, xa_data_obj);
925                 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
926                 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
927                 return;
928         } else if (rc == -ENOENT) {
929                 /*
930                  * Entry doesn't exist, nothing to be changed.
931                  */
932                 return;
933         }
934
935         /* An error happened */
936         tx->tx_err = -rc;
937 }
938
939 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
940                           const char *name, struct thandle *handle)
941 {
942         struct osd_object  *obj = osd_dt_obj(dt);
943         struct osd_thandle *oh;
944         ENTRY;
945
946         LASSERT(handle != NULL);
947         LASSERT(osd_invariant(obj));
948
949         oh = container_of0(handle, struct osd_thandle, ot_super);
950         LASSERT(oh->ot_tx != NULL);
951         LASSERT(obj->oo_dn != NULL);
952
953         down_read(&obj->oo_guard);
954         if (likely(dt_object_exists(&obj->oo_dt) && !obj->oo_destroyed))
955                 __osd_xattr_declare_del(env, obj, name, oh);
956         up_read(&obj->oo_guard);
957
958         RETURN(0);
959 }
960
961 static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
962                               const char *name, struct osd_thandle *oh)
963 {
964         int rc;
965
966         rc = __osd_xattr_cache(obj);
967         if (rc)
968                 return rc;
969
970         rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
971         if (rc)
972                 return rc;
973
974         /*
975          * only migrate delete LMV, and it needs to be done immediately, because
976          * it's used in deleting sub stripes, and if this is delayed, later when
977          * destroying the master object, it will delete sub stripes again.
978          */
979         if (!strcmp(name, XATTR_NAME_LMV))
980                 rc = __osd_sa_xattr_update(env, obj, oh);
981         else
982                 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
983         return rc;
984 }
985
986 static int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
987                            const char *name, struct osd_thandle *oh)
988 {
989         struct osd_device *osd = osd_obj2dev(obj);
990         uint64_t           xa_data_obj;
991         int                rc;
992
993         if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
994                 return -ENOENT;
995
996         /* try remove xattr from SA at first */
997         rc = __osd_sa_xattr_del(env, obj, name, oh);
998         if (rc != -ENOENT)
999                 return rc;
1000
1001         if (obj->oo_xattr == ZFS_NO_OBJECT)
1002                 return 0;
1003
1004         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
1005                         &xa_data_obj);
1006         if (rc == -ENOENT) {
1007                 rc = 0;
1008         } else if (rc == 0) {
1009                 /*
1010                  * Entry exists.
1011                  * We'll delete the existing object and ZAP entry.
1012                  */
1013                 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
1014                 if (rc)
1015                         return rc;
1016
1017                 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
1018         }
1019
1020         return rc;
1021 }
1022
1023 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
1024                   const char *name, struct thandle *handle)
1025 {
1026         struct osd_object  *obj = osd_dt_obj(dt);
1027         struct osd_thandle *oh;
1028         int                 rc;
1029         ENTRY;
1030
1031         LASSERT(handle != NULL);
1032         LASSERT(obj->oo_dn != NULL);
1033         LASSERT(osd_invariant(obj));
1034         LASSERT(dt_object_exists(dt));
1035         oh = container_of0(handle, struct osd_thandle, ot_super);
1036         LASSERT(oh->ot_tx != NULL);
1037
1038         if (!osd_obj2dev(obj)->od_posix_acl &&
1039             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1040              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
1041                 RETURN(-EOPNOTSUPP);
1042
1043         down_write(&obj->oo_guard);
1044         /* For the OST migrated from ldiskfs, the PFID EA may
1045          * be stored in LMA because of ldiskfs inode size. */
1046         if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma))
1047                 rc = osd_xattr_split_pfid(env, obj, oh);
1048         else
1049                 rc = __osd_xattr_del(env, obj, name, oh);
1050         up_write(&obj->oo_guard);
1051
1052         RETURN(rc);
1053 }
1054
1055 void osd_declare_xattrs_destroy(const struct lu_env *env,
1056                                 struct osd_object *obj, struct osd_thandle *oh)
1057 {
1058         struct osd_device *osd = osd_obj2dev(obj);
1059         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1060         uint64_t           oid = obj->oo_xattr, xid;
1061         dmu_tx_t          *tx = oh->ot_tx;
1062         zap_cursor_t      *zc;
1063         int                rc;
1064
1065         if (oid == ZFS_NO_OBJECT)
1066                 return; /* Nothing to do for SA xattrs */
1067
1068         /* Declare to free the ZAP holding xattrs */
1069         dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
1070
1071         rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
1072         if (rc)
1073                 goto out;
1074
1075         while (zap_cursor_retrieve(zc, za) == 0) {
1076                 LASSERT(za->za_num_integers == 1);
1077                 LASSERT(za->za_integer_length == sizeof(uint64_t));
1078
1079                 rc = -zap_lookup(osd->od_os, oid, za->za_name,
1080                                  sizeof(uint64_t), 1, &xid);
1081                 if (rc) {
1082                         CERROR("%s: xattr %s lookup failed: rc = %d\n",
1083                                osd->od_svname, za->za_name, rc);
1084                         break;
1085                 }
1086                 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
1087
1088                 zap_cursor_advance(zc);
1089         }
1090
1091         osd_zap_cursor_fini(zc);
1092 out:
1093         if (rc && tx->tx_err == 0)
1094                 tx->tx_err = -rc;
1095 }
1096
1097 int osd_xattrs_destroy(const struct lu_env *env,
1098                        struct osd_object *obj, struct osd_thandle *oh)
1099 {
1100         struct osd_device *osd = osd_obj2dev(obj);
1101         dmu_tx_t          *tx = oh->ot_tx;
1102         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1103         zap_cursor_t      *zc;
1104         uint64_t           xid;
1105         int                rc;
1106
1107         /* The transaction must have been assigned to a transaction group. */
1108         LASSERT(tx->tx_txg != 0);
1109
1110         if (obj->oo_xattr == ZFS_NO_OBJECT)
1111                 return 0; /* Nothing to do for SA xattrs */
1112
1113         /* Free the ZAP holding the xattrs */
1114         rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1115         if (rc)
1116                 return rc;
1117
1118         while (zap_cursor_retrieve(zc, za) == 0) {
1119                 LASSERT(za->za_num_integers == 1);
1120                 LASSERT(za->za_integer_length == sizeof(uint64_t));
1121
1122                 rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
1123                                  sizeof(uint64_t), 1, &xid);
1124                 if (rc) {
1125                         CERROR("%s: lookup xattr %s failed: rc = %d\n",
1126                                osd->od_svname, za->za_name, rc);
1127                 } else {
1128                         rc = -dmu_object_free(osd->od_os, xid, tx);
1129                         if (rc)
1130                                 CERROR("%s: free xattr %s failed: rc = %d\n",
1131                                        osd->od_svname, za->za_name, rc);
1132                 }
1133                 zap_cursor_advance(zc);
1134         }
1135         osd_zap_cursor_fini(zc);
1136
1137         rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
1138         if (rc)
1139                 CERROR("%s: free xattr %llu failed: rc = %d\n",
1140                        osd->od_svname, obj->oo_xattr, rc);
1141
1142         return rc;
1143 }
1144
1145 static int
1146 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
1147                   const struct lu_buf *lb)
1148 {
1149         nvpair_t *nvp = NULL;
1150         int       len, counted = 0;
1151         int       rc = 0;
1152
1153         rc = __osd_xattr_cache(obj);
1154         if (rc)
1155                 return rc;
1156
1157         while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
1158                 const char *name = nvpair_name(nvp);
1159
1160                 if (!osd_obj2dev(obj)->od_posix_acl &&
1161                     (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1162                      strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
1163                         continue;
1164
1165                 len = strlen(name);
1166                 if (lb->lb_buf != NULL) {
1167                         if (counted + len + 1 > lb->lb_len)
1168                                 return -ERANGE;
1169
1170                         memcpy(lb->lb_buf + counted, name, len + 1);
1171                 }
1172                 counted += len + 1;
1173         }
1174         return counted;
1175 }
1176
1177 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
1178                    const struct lu_buf *lb)
1179 {
1180         struct osd_object      *obj = osd_dt_obj(dt);
1181         struct osd_device      *osd = osd_obj2dev(obj);
1182         zap_attribute_t        *za = &osd_oti_get(env)->oti_za;
1183         zap_cursor_t           *zc;
1184         int                    rc, counted;
1185         ENTRY;
1186
1187         LASSERT(obj->oo_dn != NULL);
1188         LASSERT(osd_invariant(obj));
1189         LASSERT(dt_object_exists(dt));
1190
1191         down_read(&obj->oo_guard);
1192
1193         rc = osd_sa_xattr_list(env, obj, lb);
1194         if (rc < 0)
1195                 GOTO(out, rc);
1196
1197         counted = rc;
1198
1199         /* continue with dnode xattr if any */
1200         if (obj->oo_xattr == ZFS_NO_OBJECT)
1201                 GOTO(out, rc = counted);
1202
1203         rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1204         if (rc)
1205                 GOTO(out, rc);
1206
1207         while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
1208                 if (!osd_obj2dev(obj)->od_posix_acl &&
1209                     (strcmp(za->za_name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1210                      strcmp(za->za_name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) {
1211                         zap_cursor_advance(zc);
1212                         continue;
1213                 }
1214
1215                 rc = strlen(za->za_name);
1216                 if (lb->lb_buf != NULL) {
1217                         if (counted + rc + 1 > lb->lb_len)
1218                                 GOTO(out_fini, rc = -ERANGE);
1219
1220                         memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
1221                 }
1222                 counted += rc + 1;
1223
1224                 zap_cursor_advance(zc);
1225         }
1226         if (rc == -ENOENT) /* no more kes in the index */
1227                 rc = 0;
1228         else if (unlikely(rc < 0))
1229                 GOTO(out_fini, rc);
1230         rc = counted;
1231
1232 out_fini:
1233         osd_zap_cursor_fini(zc);
1234 out:
1235         up_read(&obj->oo_guard);
1236         RETURN(rc);
1237
1238 }