Whamcloud - gitweb
LU-8873 osd: use sa_handle_get_from_db()
[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, 2016, 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 <lustre_ver.h>
42 #include <libcfs/libcfs.h>
43 #include <obd_support.h>
44 #include <lustre_net.h>
45 #include <obd.h>
46 #include <obd_class.h>
47 #include <lustre_disk.h>
48 #include <lustre_fid.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
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(env, 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(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 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
227                   struct lu_buf *buf, const char *name)
228 {
229         struct osd_object  *obj  = osd_dt_obj(dt);
230         int                 rc, size = 0;
231         ENTRY;
232
233         LASSERT(obj->oo_dn != NULL);
234         LASSERT(osd_invariant(obj));
235
236         if (!osd_obj2dev(obj)->od_posix_acl &&
237             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
238              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
239                 RETURN(-EOPNOTSUPP);
240
241         down_read(&obj->oo_guard);
242         rc = __osd_xattr_get(env, obj, buf, name, &size);
243         up_read(&obj->oo_guard);
244
245         if (rc == -ENOENT)
246                 rc = -ENODATA;
247         else if (rc == 0)
248                 rc = size;
249         RETURN(rc);
250 }
251
252 /* the function is used to declare EAs when SA is not supported */
253 void __osd_xattr_declare_legacy(const struct lu_env *env,
254                                 struct osd_object *obj,
255                                 int vallen, const char *name,
256                                 struct osd_thandle *oh)
257 {
258         struct osd_device *osd = osd_obj2dev(obj);
259         dmu_tx_t *tx = oh->ot_tx;
260         uint64_t xa_data_obj;
261         int rc;
262
263         if (obj->oo_xattr == ZFS_NO_OBJECT) {
264                 /* xattr zap + entry */
265                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
266                 /* xattr value obj */
267                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
268                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
269                 return;
270         }
271
272         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
273                         &xa_data_obj);
274         if (rc == 0) {
275                 /*
276                  * Entry already exists.
277                  * We'll truncate the existing object.
278                  */
279                 dmu_tx_hold_bonus(tx, xa_data_obj);
280                 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
281                 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
282         } else if (rc == -ENOENT) {
283                 /*
284                  * Entry doesn't exist, we need to create a new one and a new
285                  * object to store the value.
286                  */
287                 dmu_tx_hold_bonus(tx, obj->oo_xattr);
288                 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
289                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
290                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
291         }
292 }
293
294 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
295                              int vallen, const char *name,
296                              struct osd_thandle *oh)
297 {
298         dmu_tx_t *tx = oh->ot_tx;
299         int bonuslen;
300
301         if (unlikely(obj->oo_destroyed))
302                 return;
303
304         if (unlikely(!osd_obj2dev(obj)->od_xattr_in_sa)) {
305                 __osd_xattr_declare_legacy(env, obj, vallen, name, oh);
306                 return;
307         }
308
309         /* declare EA in SA */
310         if (dt_object_exists(&obj->oo_dt)) {
311                 LASSERT(obj->oo_sa_hdl);
312                 /* XXX: it should be possible to skip spill
313                  * declaration if specific EA is part of
314                  * bonus and doesn't grow */
315                 dmu_tx_hold_spill(tx, obj->oo_dn->dn_object);
316                 return;
317         }
318
319         bonuslen = osd_obj_bonuslen(obj);
320
321         /* the object doesn't exist, but we've declared bonus
322          * in osd_declare_object_create() yet */
323         if (obj->oo_ea_in_bonus > bonuslen) {
324                 /* spill has been declared already */
325         } else if (obj->oo_ea_in_bonus + vallen > bonuslen) {
326                 /* we're about to exceed bonus, let's declare spill */
327                 dmu_tx_hold_spill(tx, DMU_NEW_OBJECT);
328         }
329         obj->oo_ea_in_bonus += vallen;
330 }
331
332 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
333                           const struct lu_buf *buf, const char *name,
334                           int fl, struct thandle *handle)
335 {
336         struct osd_object  *obj = osd_dt_obj(dt);
337         struct osd_thandle *oh;
338         ENTRY;
339
340         LASSERT(handle != NULL);
341         oh = container_of0(handle, struct osd_thandle, ot_super);
342
343         down_read(&obj->oo_guard);
344         __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
345         up_read(&obj->oo_guard);
346
347         RETURN(0);
348 }
349
350 /*
351  * Set an extended attribute.
352  * This transaction must have called udmu_xattr_declare_set() first.
353  *
354  * Returns 0 on success or a negative error number on failure.
355  *
356  * No locking is done here.
357  */
358 int __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
359                       struct osd_thandle *oh)
360 {
361         struct osd_device *osd = osd_obj2dev(obj);
362         char              *dxattr;
363         size_t             sa_size;
364         int                rc;
365
366         ENTRY;
367         LASSERT(obj->oo_sa_hdl);
368         LASSERT(obj->oo_sa_xattr);
369
370         /* Update the SA for additions, modifications, and removals. */
371         rc = -nvlist_size(obj->oo_sa_xattr, &sa_size, NV_ENCODE_XDR);
372         if (rc)
373                 return rc;
374
375         dxattr = osd_zio_buf_alloc(sa_size);
376         if (dxattr == NULL)
377                 RETURN(-ENOMEM);
378
379         rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &sa_size,
380                                 NV_ENCODE_XDR, KM_SLEEP);
381         if (rc)
382                 GOTO(out_free, rc);
383
384         rc = osd_object_sa_update(obj, SA_ZPL_DXATTR(osd), dxattr, sa_size, oh);
385 out_free:
386         osd_zio_buf_free(dxattr, sa_size);
387         RETURN(rc);
388 }
389
390 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
391                        const struct lu_buf *buf, const char *name, int fl,
392                        struct osd_thandle *oh)
393 {
394         uchar_t *nv_value;
395         size_t  size;
396         int     nv_size;
397         int     rc;
398         int     too_big = 0;
399
400         rc = __osd_xattr_cache(obj);
401         if (rc)
402                 return rc;
403
404         LASSERT(obj->oo_sa_xattr);
405         /* Limited to 32k to keep nvpair memory allocations small */
406         if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
407                 too_big = 1;
408         } else {
409                 /* Prevent the DXATTR SA from consuming the entire SA
410                  * region */
411                 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
412                 if (rc)
413                         return rc;
414
415                 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
416                         too_big = 1;
417         }
418
419         /* even in case of -EFBIG we must lookup xattr and check can we
420          * rewrite it then delete from SA */
421         rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
422                                         &nv_size);
423         if (rc == 0) {
424                 if (fl & LU_XATTR_CREATE) {
425                         return -EEXIST;
426                 } else if (too_big) {
427                         rc = -nvlist_remove(obj->oo_sa_xattr, name,
428                                                 DATA_TYPE_BYTE_ARRAY);
429                         if (rc < 0)
430                                 return rc;
431                         rc = __osd_sa_xattr_update(env, obj, oh);
432                         return rc == 0 ? -EFBIG : rc;
433                 }
434         } else if (rc == -ENOENT) {
435                 if (fl & LU_XATTR_REPLACE)
436                         return -ENODATA;
437                 else if (too_big)
438                         return -EFBIG;
439         } else {
440                 return rc;
441         }
442
443         /* Ensure xattr doesn't exist in ZAP */
444         if (obj->oo_xattr != ZFS_NO_OBJECT) {
445                 struct osd_device *osd = osd_obj2dev(obj);
446                 uint64_t           objid;
447                 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
448                                  name, 8, 1, &objid);
449                 if (rc == 0) {
450                         rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
451                         if (rc == 0)
452                                 zap_remove(osd->od_os, obj->oo_xattr,
453                                            name, oh->ot_tx);
454                 }
455         }
456
457         rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
458                                     (uchar_t *)buf->lb_buf, buf->lb_len);
459         if (rc)
460                 return rc;
461
462         rc = __osd_sa_xattr_update(env, obj, oh);
463         return rc;
464 }
465
466 int
467 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
468                 const struct lu_buf *buf, const char *name, int fl,
469                 struct osd_thandle *oh)
470 {
471         struct osd_device *osd = osd_obj2dev(obj);
472         dnode_t *xa_zap_dn = NULL;
473         dnode_t *xa_data_dn = NULL;
474         uint64_t           xa_data_obj;
475         sa_handle_t       *sa_hdl = NULL;
476         dmu_tx_t          *tx = oh->ot_tx;
477         uint64_t           size;
478         int                rc;
479
480         LASSERT(obj->oo_sa_hdl);
481
482         if (obj->oo_xattr == ZFS_NO_OBJECT) {
483                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
484
485                 la->la_valid = LA_MODE;
486                 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
487                 rc = __osd_zap_create(env, osd, &xa_zap_dn, tx, la, 0);
488                 if (rc)
489                         return rc;
490
491                 obj->oo_xattr = xa_zap_dn->dn_object;
492                 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
493                                 &obj->oo_xattr, 8, oh);
494                 if (rc)
495                         goto out;
496         }
497
498         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
499                          &xa_data_obj);
500         if (rc == 0) {
501                 if (fl & LU_XATTR_CREATE) {
502                         rc = -EEXIST;
503                         goto out;
504                 }
505                 /*
506                  * Entry already exists.
507                  * We'll truncate the existing object.
508                  */
509                 rc = __osd_obj2dnode(env, osd->od_os, xa_data_obj, &xa_data_dn);
510                 if (rc)
511                         goto out;
512
513                 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
514                                         SA_HDL_PRIVATE, &sa_hdl);
515                 if (rc)
516                         goto out;
517
518                 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
519                 if (rc)
520                         goto out_sa;
521
522                 rc = -dmu_free_range(osd->od_os, xa_data_dn->dn_object,
523                                      0, DMU_OBJECT_END, tx);
524                 if (rc)
525                         goto out_sa;
526         } else if (rc == -ENOENT) {
527                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
528                 /*
529                  * Entry doesn't exist, we need to create a new one and a new
530                  * object to store the value.
531                  */
532                 if (fl & LU_XATTR_REPLACE) {
533                         /* should be ENOATTR according to the
534                          * man, but that is undefined here */
535                         rc = -ENODATA;
536                         goto out;
537                 }
538
539                 la->la_valid = LA_MODE;
540                 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
541                 rc = __osd_object_create(env, obj, &xa_data_dn, tx, la);
542                 if (rc)
543                         goto out;
544                 xa_data_obj = xa_data_dn->dn_object;
545
546                 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
547                                         SA_HDL_PRIVATE, &sa_hdl);
548                 if (rc)
549                         goto out;
550
551                 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
552                                 1, &xa_data_obj, tx);
553                 if (rc)
554                         goto out_sa;
555         } else {
556                 /* There was an error looking up the xattr name */
557                 goto out;
558         }
559
560         /* Finally write the xattr value */
561         dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
562
563         size = buf->lb_len;
564         rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
565
566 out_sa:
567         sa_handle_destroy(sa_hdl);
568 out:
569         if (xa_data_dn != NULL)
570                 osd_dnode_rele(xa_data_dn);
571         if (xa_zap_dn != NULL)
572                 osd_dnode_rele(xa_zap_dn);
573
574         return rc;
575 }
576
577 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
578                   const struct lu_buf *buf, const char *name, int fl,
579                   struct thandle *handle)
580 {
581         struct osd_object  *obj = osd_dt_obj(dt);
582         struct osd_thandle *oh;
583         int rc = 0;
584         ENTRY;
585
586         LASSERT(handle != NULL);
587         LASSERT(osd_invariant(obj));
588
589         if (!osd_obj2dev(obj)->od_posix_acl &&
590             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
591              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
592                 RETURN(-EOPNOTSUPP);
593
594         oh = container_of0(handle, struct osd_thandle, ot_super);
595
596         down_write(&obj->oo_guard);
597         CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
598                 name, (int)buf->lb_len);
599         rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
600         up_write(&obj->oo_guard);
601
602         RETURN(rc);
603 }
604
605 static void
606 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
607                         const char *name, struct osd_thandle *oh)
608 {
609         struct osd_device *osd = osd_obj2dev(obj);
610         dmu_tx_t          *tx = oh->ot_tx;
611         uint64_t           xa_data_obj;
612         int                rc;
613
614         /* update SA_ZPL_DXATTR if xattr was in SA */
615         dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
616
617         if (obj->oo_xattr == ZFS_NO_OBJECT)
618                 return;
619
620         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
621         if (rc == 0) {
622                 /*
623                  * Entry exists.
624                  * We'll delete the existing object and ZAP entry.
625                  */
626                 dmu_tx_hold_bonus(tx, xa_data_obj);
627                 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
628                 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
629                 return;
630         } else if (rc == -ENOENT) {
631                 /*
632                  * Entry doesn't exist, nothing to be changed.
633                  */
634                 return;
635         }
636
637         /* An error happened */
638         tx->tx_err = -rc;
639 }
640
641 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
642                           const char *name, struct thandle *handle)
643 {
644         struct osd_object  *obj = osd_dt_obj(dt);
645         struct osd_thandle *oh;
646         ENTRY;
647
648         LASSERT(handle != NULL);
649         LASSERT(osd_invariant(obj));
650
651         oh = container_of0(handle, struct osd_thandle, ot_super);
652         LASSERT(oh->ot_tx != NULL);
653         LASSERT(obj->oo_dn != NULL);
654
655         down_read(&obj->oo_guard);
656         if (likely(dt_object_exists(&obj->oo_dt) && !obj->oo_destroyed))
657                 __osd_xattr_declare_del(env, obj, name, oh);
658         up_read(&obj->oo_guard);
659
660         RETURN(0);
661 }
662
663 static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
664                               const char *name, struct osd_thandle *oh)
665 {
666         int rc;
667
668         rc = __osd_xattr_cache(obj);
669         if (rc)
670                 return rc;
671
672         rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
673         if (rc == 0)
674                 rc = __osd_sa_xattr_update(env, obj, oh);
675         return rc;
676 }
677
678 static int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
679                            const char *name, struct osd_thandle *oh)
680 {
681         struct osd_device *osd = osd_obj2dev(obj);
682         uint64_t           xa_data_obj;
683         int                rc;
684
685         if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
686                 return -ENOENT;
687
688         /* try remove xattr from SA at first */
689         rc = __osd_sa_xattr_del(env, obj, name, oh);
690         if (rc != -ENOENT)
691                 return rc;
692
693         if (obj->oo_xattr == ZFS_NO_OBJECT)
694                 return 0;
695
696         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
697                         &xa_data_obj);
698         if (rc == -ENOENT) {
699                 rc = 0;
700         } else if (rc == 0) {
701                 /*
702                  * Entry exists.
703                  * We'll delete the existing object and ZAP entry.
704                  */
705                 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
706                 if (rc)
707                         return rc;
708
709                 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
710         }
711
712         return rc;
713 }
714
715 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
716                   const char *name, struct thandle *handle)
717 {
718         struct osd_object  *obj = osd_dt_obj(dt);
719         struct osd_thandle *oh;
720         int                 rc;
721         ENTRY;
722
723         LASSERT(handle != NULL);
724         LASSERT(obj->oo_dn != NULL);
725         LASSERT(osd_invariant(obj));
726         LASSERT(dt_object_exists(dt));
727         oh = container_of0(handle, struct osd_thandle, ot_super);
728         LASSERT(oh->ot_tx != NULL);
729
730         if (!osd_obj2dev(obj)->od_posix_acl &&
731             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
732              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
733                 RETURN(-EOPNOTSUPP);
734
735         down_write(&obj->oo_guard);
736         rc = __osd_xattr_del(env, obj, name, oh);
737         up_write(&obj->oo_guard);
738
739         RETURN(rc);
740 }
741
742 void osd_declare_xattrs_destroy(const struct lu_env *env,
743                                 struct osd_object *obj, struct osd_thandle *oh)
744 {
745         struct osd_device *osd = osd_obj2dev(obj);
746         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
747         uint64_t           oid = obj->oo_xattr, xid;
748         dmu_tx_t          *tx = oh->ot_tx;
749         zap_cursor_t      *zc;
750         int                rc;
751
752         if (oid == ZFS_NO_OBJECT)
753                 return; /* Nothing to do for SA xattrs */
754
755         /* Declare to free the ZAP holding xattrs */
756         dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
757
758         rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
759         if (rc)
760                 goto out;
761
762         while (zap_cursor_retrieve(zc, za) == 0) {
763                 LASSERT(za->za_num_integers == 1);
764                 LASSERT(za->za_integer_length == sizeof(uint64_t));
765
766                 rc = -zap_lookup(osd->od_os, oid, za->za_name,
767                                  sizeof(uint64_t), 1, &xid);
768                 if (rc) {
769                         CERROR("%s: xattr %s lookup failed: rc = %d\n",
770                                osd->od_svname, za->za_name, rc);
771                         break;
772                 }
773                 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
774
775                 zap_cursor_advance(zc);
776         }
777
778         osd_zap_cursor_fini(zc);
779 out:
780         if (rc && tx->tx_err == 0)
781                 tx->tx_err = -rc;
782 }
783
784 int osd_xattrs_destroy(const struct lu_env *env,
785                        struct osd_object *obj, struct osd_thandle *oh)
786 {
787         struct osd_device *osd = osd_obj2dev(obj);
788         dmu_tx_t          *tx = oh->ot_tx;
789         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
790         zap_cursor_t      *zc;
791         uint64_t           xid;
792         int                rc;
793
794         /* The transaction must have been assigned to a transaction group. */
795         LASSERT(tx->tx_txg != 0);
796
797         if (obj->oo_xattr == ZFS_NO_OBJECT)
798                 return 0; /* Nothing to do for SA xattrs */
799
800         /* Free the ZAP holding the xattrs */
801         rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
802         if (rc)
803                 return rc;
804
805         while (zap_cursor_retrieve(zc, za) == 0) {
806                 LASSERT(za->za_num_integers == 1);
807                 LASSERT(za->za_integer_length == sizeof(uint64_t));
808
809                 rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
810                                  sizeof(uint64_t), 1, &xid);
811                 if (rc) {
812                         CERROR("%s: lookup xattr %s failed: rc = %d\n",
813                                osd->od_svname, za->za_name, rc);
814                 } else {
815                         rc = -dmu_object_free(osd->od_os, xid, tx);
816                         if (rc)
817                                 CERROR("%s: free xattr %s failed: rc = %d\n",
818                                        osd->od_svname, za->za_name, rc);
819                 }
820                 zap_cursor_advance(zc);
821         }
822         osd_zap_cursor_fini(zc);
823
824         rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
825         if (rc)
826                 CERROR("%s: free xattr %llu failed: rc = %d\n",
827                        osd->od_svname, obj->oo_xattr, rc);
828
829         return rc;
830 }
831
832 static int
833 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
834                   const struct lu_buf *lb)
835 {
836         nvpair_t *nvp = NULL;
837         int       len, counted = 0;
838         int       rc = 0;
839
840         rc = __osd_xattr_cache(obj);
841         if (rc)
842                 return rc;
843
844         while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
845                 const char *name = nvpair_name(nvp);
846
847                 if (!osd_obj2dev(obj)->od_posix_acl &&
848                     (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
849                      strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
850                         continue;
851
852                 len = strlen(name);
853                 if (lb->lb_buf != NULL) {
854                         if (counted + len + 1 > lb->lb_len)
855                                 return -ERANGE;
856
857                         memcpy(lb->lb_buf + counted, name, len + 1);
858                 }
859                 counted += len + 1;
860         }
861         return counted;
862 }
863
864 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
865                    const struct lu_buf *lb)
866 {
867         struct osd_object      *obj = osd_dt_obj(dt);
868         struct osd_device      *osd = osd_obj2dev(obj);
869         zap_attribute_t        *za = &osd_oti_get(env)->oti_za;
870         zap_cursor_t           *zc;
871         int                    rc, counted;
872         ENTRY;
873
874         LASSERT(obj->oo_dn != NULL);
875         LASSERT(osd_invariant(obj));
876         LASSERT(dt_object_exists(dt));
877
878         down_read(&obj->oo_guard);
879
880         rc = osd_sa_xattr_list(env, obj, lb);
881         if (rc < 0)
882                 GOTO(out, rc);
883
884         counted = rc;
885
886         /* continue with dnode xattr if any */
887         if (obj->oo_xattr == ZFS_NO_OBJECT)
888                 GOTO(out, rc = counted);
889
890         rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
891         if (rc)
892                 GOTO(out, rc);
893
894         while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
895                 if (!osd_obj2dev(obj)->od_posix_acl &&
896                     (strcmp(za->za_name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
897                      strcmp(za->za_name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) {
898                         zap_cursor_advance(zc);
899                         continue;
900                 }
901
902                 rc = strlen(za->za_name);
903                 if (lb->lb_buf != NULL) {
904                         if (counted + rc + 1 > lb->lb_len)
905                                 GOTO(out_fini, rc = -ERANGE);
906
907                         memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
908                 }
909                 counted += rc + 1;
910
911                 zap_cursor_advance(zc);
912         }
913         if (rc == -ENOENT) /* no more kes in the index */
914                 rc = 0;
915         else if (unlikely(rc < 0))
916                 GOTO(out_fini, rc);
917         rc = counted;
918
919 out_fini:
920         osd_zap_cursor_fini(zc);
921 out:
922         up_read(&obj->oo_guard);
923         RETURN(rc);
924
925 }