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