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