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