Whamcloud - gitweb
9b9a2eea338ce4135e6e52b3136d5d66a36fa343
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osd-zfs/osd_xattr.c
37  * functions to manipulate extended attributes and system attributes
38  *
39  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
40  * Author: Mike Pershin <tappro@whamcloud.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_OSD
44
45 #include <lustre_ver.h>
46 #include <libcfs/libcfs.h>
47 #include <obd_support.h>
48 #include <lustre_net.h>
49 #include <obd.h>
50 #include <obd_class.h>
51 #include <lustre_disk.h>
52 #include <lustre_fid.h>
53
54 #include "osd_internal.h"
55
56 #include <sys/dnode.h>
57 #include <sys/dbuf.h>
58 #include <sys/spa.h>
59 #include <sys/stat.h>
60 #include <sys/zap.h>
61 #include <sys/spa_impl.h>
62 #include <sys/zfs_znode.h>
63 #include <sys/dmu_tx.h>
64 #include <sys/dmu_objset.h>
65 #include <sys/dsl_prop.h>
66 #include <sys/sa_impl.h>
67 #include <sys/txg.h>
68
69 #include <linux/posix_acl_xattr.h>
70
71
72 int __osd_xattr_load(struct osd_device *osd, uint64_t dnode, nvlist_t **sa)
73 {
74         sa_handle_t *sa_hdl;
75         char        *buf;
76         int          rc, size;
77
78         if (unlikely(dnode == ZFS_NO_OBJECT))
79                 return -ENOENT;
80
81         rc = -sa_handle_get(osd->od_os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
82         if (rc)
83                 return rc;
84
85         rc = -sa_size(sa_hdl, SA_ZPL_DXATTR(osd), &size);
86         if (rc) {
87                 if (rc == -ENOENT)
88                         rc = -nvlist_alloc(sa, NV_UNIQUE_NAME, KM_SLEEP);
89                 goto out_sa;
90         }
91
92         buf = osd_zio_buf_alloc(size);
93         if (buf == NULL) {
94                 rc = -ENOMEM;
95                 goto out_sa;
96         }
97         rc = -sa_lookup(sa_hdl, SA_ZPL_DXATTR(osd), buf, size);
98         if (rc == 0)
99                 rc = -nvlist_unpack(buf, size, sa, KM_SLEEP);
100         osd_zio_buf_free(buf, size);
101 out_sa:
102         sa_handle_destroy(sa_hdl);
103
104         return rc;
105 }
106
107 static inline int __osd_xattr_cache(const struct lu_env *env,
108                                     struct osd_object *obj)
109 {
110         LASSERT(obj->oo_sa_xattr == NULL);
111         LASSERT(obj->oo_db != NULL);
112
113         return __osd_xattr_load(osd_obj2dev(obj), obj->oo_db->db_object,
114                                 &obj->oo_sa_xattr);
115 }
116
117 static int
118 __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
119                    const struct lu_buf *buf, const char *name, int *sizep)
120 {
121         uchar_t *nv_value;
122         int      rc;
123
124         LASSERT(obj->oo_sa_hdl);
125
126         if (obj->oo_sa_xattr == NULL) {
127                 rc = __osd_xattr_cache(env, obj);
128                 if (rc)
129                         return rc;
130         }
131
132         LASSERT(obj->oo_sa_xattr);
133         rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
134                         sizep);
135         if (rc)
136                 return rc;
137
138         if (buf == NULL || buf->lb_buf == NULL) {
139                 /* return the required size by *sizep */
140                 return 0;
141         }
142
143         if (*sizep > buf->lb_len)
144                 return -ERANGE; /* match ldiskfs error */
145
146         memcpy(buf->lb_buf, nv_value, *sizep);
147         return 0;
148 }
149
150 int __osd_xattr_get_large(const struct lu_env *env, struct osd_device *osd,
151                           uint64_t xattr, struct lu_buf *buf,
152                           const char *name, int *sizep)
153 {
154         dmu_buf_t       *xa_data_db;
155         sa_handle_t     *sa_hdl = NULL;
156         uint64_t         xa_data_obj, size;
157         int              rc;
158
159         /* are there any extended attributes? */
160         if (xattr == ZFS_NO_OBJECT)
161                 return -ENOENT;
162
163         /* Lookup the object number containing the xattr data */
164         rc = -zap_lookup(osd->od_os, xattr, name, sizeof(uint64_t), 1,
165                         &xa_data_obj);
166         if (rc)
167                 return rc;
168
169         rc = __osd_obj2dbuf(env, osd->od_os, xa_data_obj, &xa_data_db);
170         if (rc)
171                 return rc;
172
173         rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL, SA_HDL_PRIVATE,
174                         &sa_hdl);
175         if (rc)
176                 goto out_rele;
177
178         /* Get the xattr value length / object size */
179         rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
180         if (rc)
181                 goto out;
182
183         if (size > INT_MAX) {
184                 rc = -EOVERFLOW;
185                 goto out;
186         }
187
188         *sizep = (int)size;
189
190         if (buf == NULL || buf->lb_buf == NULL) {
191                 /* We only need to return the required size */
192                 goto out;
193         }
194         if (*sizep > buf->lb_len) {
195                 rc = -ERANGE; /* match ldiskfs error */
196                 goto out;
197         }
198
199         rc = -dmu_read(osd->od_os, xa_data_db->db_object, 0,
200                         size, buf->lb_buf, DMU_READ_PREFETCH);
201
202 out:
203         sa_handle_destroy(sa_hdl);
204 out_rele:
205         dmu_buf_rele(xa_data_db, FTAG);
206
207         return rc;
208 }
209
210 /**
211  * Copy an extended attribute into the buffer provided, or compute
212  * the required buffer size if \a buf is NULL.
213  *
214  * On success, the number of bytes used or required is stored in \a sizep.
215  *
216  * Note that no locking is done here.
217  *
218  * \param[in] env      execution environment
219  * \param[in] obj      object for which to retrieve xattr
220  * \param[out] buf     buffer to store xattr value in
221  * \param[in] name     name of xattr to copy
222  * \param[out] sizep   bytes used or required to store xattr
223  *
224  * \retval 0           on success
225  * \retval negative    negated errno on failure
226  */
227 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
228                     struct lu_buf *buf, const char *name, int *sizep)
229 {
230         int rc;
231
232         if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
233                 return -ENOENT;
234
235         /* check SA_ZPL_DXATTR first then fallback to directory xattr */
236         rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
237         if (rc != -ENOENT)
238                 return rc;
239
240         return __osd_xattr_get_large(env, osd_obj2dev(obj), obj->oo_xattr,
241                                      buf, name, sizep);
242 }
243
244 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
245                   struct lu_buf *buf, const char *name)
246 {
247         struct osd_object  *obj  = osd_dt_obj(dt);
248         int                 rc, size = 0;
249         ENTRY;
250
251         LASSERT(obj->oo_db != NULL);
252         LASSERT(osd_invariant(obj));
253
254         if (!osd_obj2dev(obj)->od_posix_acl &&
255             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
256              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
257                 RETURN(-EOPNOTSUPP);
258
259         down_read(&obj->oo_guard);
260         rc = __osd_xattr_get(env, obj, buf, name, &size);
261         up_read(&obj->oo_guard);
262
263         if (rc == -ENOENT)
264                 rc = -ENODATA;
265         else if (rc == 0)
266                 rc = size;
267         RETURN(rc);
268 }
269
270 /* the function is used to declare EAs when SA is not supported */
271 void __osd_xattr_declare_legacy(const struct lu_env *env,
272                                 struct osd_object *obj,
273                                 int vallen, const char *name,
274                                 struct osd_thandle *oh)
275 {
276         struct osd_device *osd = osd_obj2dev(obj);
277         dmu_tx_t *tx = oh->ot_tx;
278         uint64_t xa_data_obj;
279         int rc;
280
281         if (obj->oo_xattr == ZFS_NO_OBJECT) {
282                 /* xattr zap + entry */
283                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
284                 /* xattr value obj */
285                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
286                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
287                 return;
288         }
289
290         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
291                         &xa_data_obj);
292         if (rc == 0) {
293                 /*
294                  * Entry already exists.
295                  * We'll truncate the existing object.
296                  */
297                 dmu_tx_hold_bonus(tx, xa_data_obj);
298                 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
299                 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
300         } else if (rc == -ENOENT) {
301                 /*
302                  * Entry doesn't exist, we need to create a new one and a new
303                  * object to store the value.
304                  */
305                 dmu_tx_hold_bonus(tx, obj->oo_xattr);
306                 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
307                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
308                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
309         }
310 }
311
312 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
313                              int vallen, const char *name,
314                              struct osd_thandle *oh)
315 {
316         dmu_buf_t         *db = obj->oo_db;
317         dmu_tx_t          *tx = oh->ot_tx;
318
319         if (unlikely(obj->oo_destroyed))
320                 return;
321
322         if (unlikely(!osd_obj2dev(obj)->od_xattr_in_sa)) {
323                 __osd_xattr_declare_legacy(env, obj, vallen, name, oh);
324                 return;
325         }
326
327         /* declare EA in SA */
328         if (dt_object_exists(&obj->oo_dt)) {
329                 LASSERT(obj->oo_sa_hdl);
330                 /* XXX: it should be possible to skip spill
331                  * declaration if specific EA is part of
332                  * bonus and doesn't grow */
333                 dmu_tx_hold_spill(tx, db->db_object);
334                 return;
335         }
336
337         /* the object doesn't exist, but we've declared bonus
338          * in osd_declare_object_create() yet */
339         if (obj->oo_ea_in_bonus > DN_MAX_BONUSLEN) {
340                 /* spill has been declared already */
341         } else if (obj->oo_ea_in_bonus + vallen > DN_MAX_BONUSLEN) {
342                 /* we're about to exceed bonus, let's declare spill */
343                 dmu_tx_hold_spill(tx, DMU_NEW_OBJECT);
344         }
345         obj->oo_ea_in_bonus += vallen;
346 }
347
348 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
349                           const struct lu_buf *buf, const char *name,
350                           int fl, struct thandle *handle)
351 {
352         struct osd_object  *obj = osd_dt_obj(dt);
353         struct osd_thandle *oh;
354         ENTRY;
355
356         LASSERT(handle != NULL);
357         oh = container_of0(handle, struct osd_thandle, ot_super);
358
359         down_read(&obj->oo_guard);
360         __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
361         up_read(&obj->oo_guard);
362
363         RETURN(0);
364 }
365
366 /*
367  * Set an extended attribute.
368  * This transaction must have called udmu_xattr_declare_set() first.
369  *
370  * Returns 0 on success or a negative error number on failure.
371  *
372  * No locking is done here.
373  */
374 int __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
375                       struct osd_thandle *oh)
376 {
377         struct osd_device *osd = osd_obj2dev(obj);
378         char              *dxattr;
379         size_t             sa_size;
380         int                rc;
381
382         ENTRY;
383         LASSERT(obj->oo_sa_hdl);
384         LASSERT(obj->oo_sa_xattr);
385
386         /* Update the SA for additions, modifications, and removals. */
387         rc = -nvlist_size(obj->oo_sa_xattr, &sa_size, NV_ENCODE_XDR);
388         if (rc)
389                 return rc;
390
391         dxattr = osd_zio_buf_alloc(sa_size);
392         if (dxattr == NULL)
393                 RETURN(-ENOMEM);
394
395         rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &sa_size,
396                                 NV_ENCODE_XDR, KM_SLEEP);
397         if (rc)
398                 GOTO(out_free, rc);
399
400         rc = osd_object_sa_update(obj, SA_ZPL_DXATTR(osd), dxattr, sa_size, oh);
401 out_free:
402         osd_zio_buf_free(dxattr, sa_size);
403         RETURN(rc);
404 }
405
406 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
407                        const struct lu_buf *buf, const char *name, int fl,
408                        struct osd_thandle *oh)
409 {
410         uchar_t *nv_value;
411         size_t  size;
412         int     nv_size;
413         int     rc;
414         int     too_big = 0;
415
416         LASSERT(obj->oo_sa_hdl);
417         if (obj->oo_sa_xattr == NULL) {
418                 rc = __osd_xattr_cache(env, obj);
419                 if (rc)
420                         return rc;
421         }
422
423         LASSERT(obj->oo_sa_xattr);
424         /* Limited to 32k to keep nvpair memory allocations small */
425         if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
426                 too_big = 1;
427         } else {
428                 /* Prevent the DXATTR SA from consuming the entire SA
429                  * region */
430                 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
431                 if (rc)
432                         return rc;
433
434                 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
435                         too_big = 1;
436         }
437
438         /* even in case of -EFBIG we must lookup xattr and check can we
439          * rewrite it then delete from SA */
440         rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
441                                         &nv_size);
442         if (rc == 0) {
443                 if (fl & LU_XATTR_CREATE) {
444                         return -EEXIST;
445                 } else if (too_big) {
446                         rc = -nvlist_remove(obj->oo_sa_xattr, name,
447                                                 DATA_TYPE_BYTE_ARRAY);
448                         if (rc < 0)
449                                 return rc;
450                         rc = __osd_sa_xattr_update(env, obj, oh);
451                         return rc == 0 ? -EFBIG : rc;
452                 }
453         } else if (rc == -ENOENT) {
454                 if (fl & LU_XATTR_REPLACE)
455                         return -ENODATA;
456                 else if (too_big)
457                         return -EFBIG;
458         } else {
459                 return rc;
460         }
461
462         /* Ensure xattr doesn't exist in ZAP */
463         if (obj->oo_xattr != ZFS_NO_OBJECT) {
464                 struct osd_device *osd = osd_obj2dev(obj);
465                 uint64_t           objid;
466                 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
467                                  name, 8, 1, &objid);
468                 if (rc == 0) {
469                         rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
470                         if (rc == 0)
471                                 zap_remove(osd->od_os, obj->oo_xattr,
472                                            name, oh->ot_tx);
473                 }
474         }
475
476         rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
477                                     (uchar_t *)buf->lb_buf, buf->lb_len);
478         if (rc)
479                 return rc;
480
481         rc = __osd_sa_xattr_update(env, obj, oh);
482         return rc;
483 }
484
485 int
486 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
487                 const struct lu_buf *buf, const char *name, int fl,
488                 struct osd_thandle *oh)
489 {
490         struct osd_device *osd = osd_obj2dev(obj);
491         dmu_buf_t         *xa_zap_db = NULL;
492         dmu_buf_t         *xa_data_db = NULL;
493         uint64_t           xa_data_obj;
494         sa_handle_t       *sa_hdl = NULL;
495         dmu_tx_t          *tx = oh->ot_tx;
496         uint64_t           size;
497         int                rc;
498
499         LASSERT(obj->oo_sa_hdl);
500
501         if (obj->oo_xattr == ZFS_NO_OBJECT) {
502                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
503
504                 la->la_valid = LA_MODE;
505                 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
506                 rc = __osd_zap_create(env, osd, &xa_zap_db, tx, la, 0);
507                 if (rc)
508                         return rc;
509
510                 obj->oo_xattr = xa_zap_db->db_object;
511                 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
512                                 &obj->oo_xattr, 8, oh);
513                 if (rc)
514                         goto out;
515         }
516
517         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
518                          &xa_data_obj);
519         if (rc == 0) {
520                 if (fl & LU_XATTR_CREATE) {
521                         rc = -EEXIST;
522                         goto out;
523                 }
524                 /*
525                  * Entry already exists.
526                  * We'll truncate the existing object.
527                  */
528                 rc = __osd_obj2dbuf(env, osd->od_os, xa_data_obj,
529                                         &xa_data_db);
530                 if (rc)
531                         goto out;
532
533                 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
534                                         SA_HDL_PRIVATE, &sa_hdl);
535                 if (rc)
536                         goto out;
537
538                 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
539                 if (rc)
540                         goto out_sa;
541
542                 rc = -dmu_free_range(osd->od_os, xa_data_db->db_object,
543                                      0, DMU_OBJECT_END, tx);
544                 if (rc)
545                         goto out_sa;
546         } else if (rc == -ENOENT) {
547                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
548                 /*
549                  * Entry doesn't exist, we need to create a new one and a new
550                  * object to store the value.
551                  */
552                 if (fl & LU_XATTR_REPLACE) {
553                         /* should be ENOATTR according to the
554                          * man, but that is undefined here */
555                         rc = -ENODATA;
556                         goto out;
557                 }
558
559                 la->la_valid = LA_MODE;
560                 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
561                 rc = __osd_object_create(env, obj, &xa_data_db, tx, la);
562                 if (rc)
563                         goto out;
564                 xa_data_obj = xa_data_db->db_object;
565
566                 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
567                                         SA_HDL_PRIVATE, &sa_hdl);
568                 if (rc)
569                         goto out;
570
571                 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
572                                 1, &xa_data_obj, tx);
573                 if (rc)
574                         goto out_sa;
575         } else {
576                 /* There was an error looking up the xattr name */
577                 goto out;
578         }
579
580         /* Finally write the xattr value */
581         dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
582
583         size = buf->lb_len;
584         rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
585
586 out_sa:
587         sa_handle_destroy(sa_hdl);
588 out:
589         if (xa_data_db != NULL)
590                 dmu_buf_rele(xa_data_db, FTAG);
591         if (xa_zap_db != NULL)
592                 dmu_buf_rele(xa_zap_db, FTAG);
593
594         return rc;
595 }
596
597 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
598                   const struct lu_buf *buf, const char *name, int fl,
599                   struct thandle *handle)
600 {
601         struct osd_object  *obj = osd_dt_obj(dt);
602         struct osd_thandle *oh;
603         int rc = 0;
604         ENTRY;
605
606         LASSERT(handle != NULL);
607         LASSERT(osd_invariant(obj));
608
609         if (!osd_obj2dev(obj)->od_posix_acl &&
610             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
611              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
612                 RETURN(-EOPNOTSUPP);
613
614         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_OVERFLOW) &&
615             strcmp(name, XATTR_NAME_LINK) == 0)
616                 RETURN(-ENOSPC);
617
618         oh = container_of0(handle, struct osd_thandle, ot_super);
619
620         down_write(&obj->oo_guard);
621         CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
622                 name, (int)buf->lb_len);
623         rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
624         up_write(&obj->oo_guard);
625
626         RETURN(rc);
627 }
628
629 static void
630 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
631                         const char *name, struct osd_thandle *oh)
632 {
633         struct osd_device *osd = osd_obj2dev(obj);
634         dmu_tx_t          *tx = oh->ot_tx;
635         uint64_t           xa_data_obj;
636         int                rc;
637
638         /* update SA_ZPL_DXATTR if xattr was in SA */
639         dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
640
641         if (obj->oo_xattr == ZFS_NO_OBJECT)
642                 return;
643
644         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
645         if (rc == 0) {
646                 /*
647                  * Entry exists.
648                  * We'll delete the existing object and ZAP entry.
649                  */
650                 dmu_tx_hold_bonus(tx, xa_data_obj);
651                 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
652                 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
653                 return;
654         } else if (rc == -ENOENT) {
655                 /*
656                  * Entry doesn't exist, nothing to be changed.
657                  */
658                 return;
659         }
660
661         /* An error happened */
662         tx->tx_err = -rc;
663 }
664
665 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
666                           const char *name, struct thandle *handle)
667 {
668         struct osd_object  *obj = osd_dt_obj(dt);
669         struct osd_thandle *oh;
670         ENTRY;
671
672         LASSERT(handle != NULL);
673         LASSERT(osd_invariant(obj));
674
675         oh = container_of0(handle, struct osd_thandle, ot_super);
676         LASSERT(oh->ot_tx != NULL);
677         LASSERT(obj->oo_db != NULL);
678
679         down_read(&obj->oo_guard);
680         if (likely(dt_object_exists(&obj->oo_dt) && !obj->oo_destroyed))
681                 __osd_xattr_declare_del(env, obj, name, oh);
682         up_read(&obj->oo_guard);
683
684         RETURN(0);
685 }
686
687 static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
688                               const char *name, struct osd_thandle *oh)
689 {
690         int rc;
691
692         if (obj->oo_sa_xattr == NULL) {
693                 rc = __osd_xattr_cache(env, obj);
694                 if (rc)
695                         return rc;
696         }
697
698         rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
699         if (rc == 0)
700                 rc = __osd_sa_xattr_update(env, obj, oh);
701         return rc;
702 }
703
704 static int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
705                            const char *name, struct osd_thandle *oh)
706 {
707         struct osd_device *osd = osd_obj2dev(obj);
708         uint64_t           xa_data_obj;
709         int                rc;
710
711         if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
712                 return -ENOENT;
713
714         /* try remove xattr from SA at first */
715         rc = __osd_sa_xattr_del(env, obj, name, oh);
716         if (rc != -ENOENT)
717                 return rc;
718
719         if (obj->oo_xattr == ZFS_NO_OBJECT)
720                 return 0;
721
722         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
723                         &xa_data_obj);
724         if (rc == -ENOENT) {
725                 rc = 0;
726         } else if (rc == 0) {
727                 /*
728                  * Entry exists.
729                  * We'll delete the existing object and ZAP entry.
730                  */
731                 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
732                 if (rc)
733                         return rc;
734
735                 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
736         }
737
738         return rc;
739 }
740
741 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
742                   const char *name, struct thandle *handle)
743 {
744         struct osd_object  *obj = osd_dt_obj(dt);
745         struct osd_thandle *oh;
746         int                 rc;
747         ENTRY;
748
749         LASSERT(handle != NULL);
750         LASSERT(obj->oo_db != NULL);
751         LASSERT(osd_invariant(obj));
752         LASSERT(dt_object_exists(dt));
753         oh = container_of0(handle, struct osd_thandle, ot_super);
754         LASSERT(oh->ot_tx != NULL);
755
756         if (!osd_obj2dev(obj)->od_posix_acl &&
757             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
758              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
759                 RETURN(-EOPNOTSUPP);
760
761         down_write(&obj->oo_guard);
762         rc = __osd_xattr_del(env, obj, name, oh);
763         up_write(&obj->oo_guard);
764
765         RETURN(rc);
766 }
767
768 void osd_declare_xattrs_destroy(const struct lu_env *env,
769                                 struct osd_object *obj, struct osd_thandle *oh)
770 {
771         struct osd_device *osd = osd_obj2dev(obj);
772         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
773         uint64_t           oid = obj->oo_xattr, xid;
774         dmu_tx_t          *tx = oh->ot_tx;
775         zap_cursor_t      *zc;
776         int                rc;
777
778         if (oid == ZFS_NO_OBJECT)
779                 return; /* Nothing to do for SA xattrs */
780
781         /* Declare to free the ZAP holding xattrs */
782         dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
783
784         rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
785         if (rc)
786                 goto out;
787
788         while (zap_cursor_retrieve(zc, za) == 0) {
789                 LASSERT(za->za_num_integers == 1);
790                 LASSERT(za->za_integer_length == sizeof(uint64_t));
791
792                 rc = -zap_lookup(osd->od_os, oid, za->za_name,
793                                  sizeof(uint64_t), 1, &xid);
794                 if (rc) {
795                         CERROR("%s: xattr %s lookup failed: rc = %d\n",
796                                osd->od_svname, za->za_name, rc);
797                         break;
798                 }
799                 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
800
801                 zap_cursor_advance(zc);
802         }
803
804         osd_zap_cursor_fini(zc);
805 out:
806         if (rc && tx->tx_err == 0)
807                 tx->tx_err = -rc;
808 }
809
810 int osd_xattrs_destroy(const struct lu_env *env,
811                        struct osd_object *obj, struct osd_thandle *oh)
812 {
813         struct osd_device *osd = osd_obj2dev(obj);
814         dmu_tx_t          *tx = oh->ot_tx;
815         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
816         zap_cursor_t      *zc;
817         uint64_t           xid;
818         int                rc;
819
820         /* The transaction must have been assigned to a transaction group. */
821         LASSERT(tx->tx_txg != 0);
822
823         if (obj->oo_xattr == ZFS_NO_OBJECT)
824                 return 0; /* Nothing to do for SA xattrs */
825
826         /* Free the ZAP holding the xattrs */
827         rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
828         if (rc)
829                 return rc;
830
831         while (zap_cursor_retrieve(zc, za) == 0) {
832                 LASSERT(za->za_num_integers == 1);
833                 LASSERT(za->za_integer_length == sizeof(uint64_t));
834
835                 rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
836                                  sizeof(uint64_t), 1, &xid);
837                 if (rc) {
838                         CERROR("%s: lookup xattr %s failed: rc = %d\n",
839                                osd->od_svname, za->za_name, rc);
840                 } else {
841                         rc = -dmu_object_free(osd->od_os, xid, tx);
842                         if (rc)
843                                 CERROR("%s: free xattr %s failed: rc = %d\n",
844                                        osd->od_svname, za->za_name, rc);
845                 }
846                 zap_cursor_advance(zc);
847         }
848         osd_zap_cursor_fini(zc);
849
850         rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
851         if (rc)
852                 CERROR("%s: free xattr "LPU64" failed: rc = %d\n",
853                        osd->od_svname, obj->oo_xattr, rc);
854
855         return rc;
856 }
857
858 static int
859 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
860                   const struct lu_buf *lb)
861 {
862         nvpair_t *nvp = NULL;
863         int       len, counted = 0;
864         int       rc = 0;
865
866         if (obj->oo_sa_xattr == NULL) {
867                 rc = __osd_xattr_cache(env, obj);
868                 if (rc)
869                         return rc;
870         }
871
872         LASSERT(obj->oo_sa_xattr);
873
874         while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
875                 const char *name = nvpair_name(nvp);
876
877                 if (!osd_obj2dev(obj)->od_posix_acl &&
878                     (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
879                      strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
880                         continue;
881
882                 len = strlen(name);
883                 if (lb->lb_buf != NULL) {
884                         if (counted + len + 1 > lb->lb_len)
885                                 return -ERANGE;
886
887                         memcpy(lb->lb_buf + counted, name, len + 1);
888                 }
889                 counted += len + 1;
890         }
891         return counted;
892 }
893
894 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
895                    const struct lu_buf *lb)
896 {
897         struct osd_object      *obj = osd_dt_obj(dt);
898         struct osd_device      *osd = osd_obj2dev(obj);
899         zap_attribute_t        *za = &osd_oti_get(env)->oti_za;
900         zap_cursor_t           *zc;
901         int                    rc, counted;
902         ENTRY;
903
904         LASSERT(obj->oo_db != NULL);
905         LASSERT(osd_invariant(obj));
906         LASSERT(dt_object_exists(dt));
907
908         down_read(&obj->oo_guard);
909
910         rc = osd_sa_xattr_list(env, obj, lb);
911         if (rc < 0)
912                 GOTO(out, rc);
913
914         counted = rc;
915
916         /* continue with dnode xattr if any */
917         if (obj->oo_xattr == ZFS_NO_OBJECT)
918                 GOTO(out, rc = counted);
919
920         rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
921         if (rc)
922                 GOTO(out, rc);
923
924         while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
925                 if (!osd_obj2dev(obj)->od_posix_acl &&
926                     (strcmp(za->za_name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
927                      strcmp(za->za_name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) {
928                         zap_cursor_advance(zc);
929                         continue;
930                 }
931
932                 rc = strlen(za->za_name);
933                 if (lb->lb_buf != NULL) {
934                         if (counted + rc + 1 > lb->lb_len)
935                                 GOTO(out_fini, rc = -ERANGE);
936
937                         memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
938                 }
939                 counted += rc + 1;
940
941                 zap_cursor_advance(zc);
942         }
943         if (rc == -ENOENT) /* no more kes in the index */
944                 rc = 0;
945         else if (unlikely(rc < 0))
946                 GOTO(out_fini, rc);
947         rc = counted;
948
949 out_fini:
950         osd_zap_cursor_fini(zc);
951 out:
952         up_read(&obj->oo_guard);
953         RETURN(rc);
954
955 }