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