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