Whamcloud - gitweb
218464f180577052e8bc3d922b3d44fa6641c9b4
[fs/lustre-release.git] / lustre / osd-zfs / osd_xattr.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osd-zfs/osd_xattr.c
37  * functions to manipulate extended attributes and system attributes
38  *
39  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
40  * Author: Mike Pershin <tappro@whamcloud.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_OSD
44
45 #include <lustre_ver.h>
46 #include <libcfs/libcfs.h>
47 #include <obd_support.h>
48 #include <lustre_net.h>
49 #include <obd.h>
50 #include <obd_class.h>
51 #include <lustre_disk.h>
52 #include <lustre_fid.h>
53
54 #include "osd_internal.h"
55
56 #include <sys/dnode.h>
57 #include <sys/dbuf.h>
58 #include <sys/spa.h>
59 #include <sys/stat.h>
60 #include <sys/zap.h>
61 #include <sys/spa_impl.h>
62 #include <sys/zfs_znode.h>
63 #include <sys/dmu_tx.h>
64 #include <sys/dmu_objset.h>
65 #include <sys/dsl_prop.h>
66 #include <sys/sa_impl.h>
67 #include <sys/txg.h>
68
69 #include <linux/posix_acl_xattr.h>
70
71
72 int __osd_xattr_load(struct osd_device *osd, uint64_t dnode, nvlist_t **sa)
73 {
74         sa_handle_t *sa_hdl;
75         char        *buf;
76         int          rc, size;
77
78         if (unlikely(dnode == ZFS_NO_OBJECT))
79                 return -ENOENT;
80
81         rc = -sa_handle_get(osd->od_os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
82         if (rc)
83                 return rc;
84
85         rc = -sa_size(sa_hdl, SA_ZPL_DXATTR(osd), &size);
86         if (rc) {
87                 if (rc == -ENOENT)
88                         rc = -nvlist_alloc(sa, NV_UNIQUE_NAME, KM_SLEEP);
89                 goto out_sa;
90         }
91
92         buf = osd_zio_buf_alloc(size);
93         if (buf == NULL) {
94                 rc = -ENOMEM;
95                 goto out_sa;
96         }
97         rc = -sa_lookup(sa_hdl, SA_ZPL_DXATTR(osd), buf, size);
98         if (rc == 0)
99                 rc = -nvlist_unpack(buf, size, sa, KM_SLEEP);
100         osd_zio_buf_free(buf, size);
101 out_sa:
102         sa_handle_destroy(sa_hdl);
103
104         return rc;
105 }
106
107 static inline int __osd_xattr_cache(const struct lu_env *env,
108                                     struct osd_object *obj)
109 {
110         LASSERT(obj->oo_sa_xattr == NULL);
111         LASSERT(obj->oo_db != NULL);
112
113         return __osd_xattr_load(osd_obj2dev(obj), obj->oo_db->db_object,
114                                 &obj->oo_sa_xattr);
115 }
116
117 static int
118 __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
119                    const struct lu_buf *buf, const char *name, int *sizep)
120 {
121         uchar_t *nv_value;
122         int      rc;
123
124         LASSERT(obj->oo_sa_hdl);
125
126         if (obj->oo_sa_xattr == NULL) {
127                 rc = __osd_xattr_cache(env, obj);
128                 if (rc)
129                         return rc;
130         }
131
132         LASSERT(obj->oo_sa_xattr);
133         rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
134                         sizep);
135         if (rc)
136                 return rc;
137
138         if (buf == NULL || buf->lb_buf == NULL) {
139                 /* return the required size by *sizep */
140                 return 0;
141         }
142
143         if (*sizep > buf->lb_len)
144                 return -ERANGE; /* match ldiskfs error */
145
146         memcpy(buf->lb_buf, nv_value, *sizep);
147         return 0;
148 }
149
150 int __osd_xattr_get_large(const struct lu_env *env, struct osd_device *osd,
151                           uint64_t xattr, struct lu_buf *buf,
152                           const char *name, int *sizep)
153 {
154         dmu_buf_t       *xa_data_db;
155         sa_handle_t     *sa_hdl = NULL;
156         uint64_t         xa_data_obj, size;
157         int              rc;
158
159         /* are there any extended attributes? */
160         if (xattr == ZFS_NO_OBJECT)
161                 return -ENOENT;
162
163         /* Lookup the object number containing the xattr data */
164         rc = -zap_lookup(osd->od_os, xattr, name, sizeof(uint64_t), 1,
165                         &xa_data_obj);
166         if (rc)
167                 return rc;
168
169         rc = __osd_obj2dbuf(env, osd->od_os, xa_data_obj, &xa_data_db);
170         if (rc)
171                 return rc;
172
173         rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL, SA_HDL_PRIVATE,
174                         &sa_hdl);
175         if (rc)
176                 goto out_rele;
177
178         /* Get the xattr value length / object size */
179         rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
180         if (rc)
181                 goto out;
182
183         if (size > INT_MAX) {
184                 rc = -EOVERFLOW;
185                 goto out;
186         }
187
188         *sizep = (int)size;
189
190         if (buf == NULL || buf->lb_buf == NULL) {
191                 /* We only need to return the required size */
192                 goto out;
193         }
194         if (*sizep > buf->lb_len) {
195                 rc = -ERANGE; /* match ldiskfs error */
196                 goto out;
197         }
198
199         rc = -dmu_read(osd->od_os, xa_data_db->db_object, 0,
200                         size, buf->lb_buf, DMU_READ_PREFETCH);
201
202 out:
203         sa_handle_destroy(sa_hdl);
204 out_rele:
205         dmu_buf_rele(xa_data_db, FTAG);
206
207         return rc;
208 }
209
210 /**
211  * Copy an extended attribute into the buffer provided, or compute
212  * the required buffer size if \a buf is NULL.
213  *
214  * On success, the number of bytes used or required is stored in \a sizep.
215  *
216  * Note that no locking is done here.
217  *
218  * \param[in] env      execution environment
219  * \param[in] obj      object for which to retrieve xattr
220  * \param[out] buf     buffer to store xattr value in
221  * \param[in] name     name of xattr to copy
222  * \param[out] sizep   bytes used or required to store xattr
223  *
224  * \retval 0           on success
225  * \retval negative    negated errno on failure
226  */
227 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
228                     struct lu_buf *buf, const char *name, int *sizep)
229 {
230         int rc;
231
232         if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
233                 return -ENOENT;
234
235         /* check SA_ZPL_DXATTR first then fallback to directory xattr */
236         rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
237         if (rc != -ENOENT)
238                 return rc;
239
240         return __osd_xattr_get_large(env, osd_obj2dev(obj), obj->oo_xattr,
241                                      buf, name, sizep);
242 }
243
244 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
245                   struct lu_buf *buf, const char *name)
246 {
247         struct osd_object  *obj  = osd_dt_obj(dt);
248         int                 rc, size = 0;
249         ENTRY;
250
251         LASSERT(obj->oo_db != NULL);
252         LASSERT(osd_invariant(obj));
253
254         if (!osd_obj2dev(obj)->od_posix_acl &&
255             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
256              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
257                 RETURN(-EOPNOTSUPP);
258
259         down_read(&obj->oo_guard);
260         rc = __osd_xattr_get(env, obj, buf, name, &size);
261         up_read(&obj->oo_guard);
262
263         if (rc == -ENOENT)
264                 rc = -ENODATA;
265         else if (rc == 0)
266                 rc = size;
267         RETURN(rc);
268 }
269
270 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
271                              int vallen, const char *name,
272                              struct osd_thandle *oh)
273 {
274         struct osd_device *osd = osd_obj2dev(obj);
275         dmu_buf_t         *db = obj->oo_db;
276         dmu_tx_t          *tx = oh->ot_tx;
277         uint64_t           xa_data_obj;
278         int                rc = 0;
279         int                here;
280
281         if (unlikely(obj->oo_destroyed))
282                 return;
283
284         here = dt_object_exists(&obj->oo_dt);
285
286         /* object may be not yet created */
287         if (here) {
288                 LASSERT(db);
289                 LASSERT(obj->oo_sa_hdl);
290                 /* we might just update SA_ZPL_DXATTR */
291                 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
292
293                 if (obj->oo_xattr == ZFS_NO_OBJECT)
294                         rc = -ENOENT;
295         }
296
297         if (!here || rc == -ENOENT) {
298                 /* we'll be updating SA_ZPL_XATTR */
299                 if (here) {
300                         LASSERT(obj->oo_sa_hdl);
301                         dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
302                 }
303                 /* xattr zap + entry */
304                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
305                 /* xattr value obj */
306                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
307                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
308                 return;
309         }
310
311         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
312                         &xa_data_obj);
313         if (rc == 0) {
314                 /*
315                  * Entry already exists.
316                  * We'll truncate the existing object.
317                  */
318                 dmu_tx_hold_bonus(tx, xa_data_obj);
319                 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
320                 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
321                 return;
322         } else if (rc == -ENOENT) {
323                 /*
324                  * Entry doesn't exist, we need to create a new one and a new
325                  * object to store the value.
326                  */
327                 dmu_tx_hold_bonus(tx, obj->oo_xattr);
328                 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
329                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
330                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
331                 return;
332         }
333
334         /* An error happened */
335         tx->tx_err = -rc;
336 }
337
338 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
339                           const struct lu_buf *buf, const char *name,
340                           int fl, struct thandle *handle)
341 {
342         struct osd_object  *obj = osd_dt_obj(dt);
343         struct osd_thandle *oh;
344         ENTRY;
345
346         LASSERT(handle != NULL);
347         oh = container_of0(handle, struct osd_thandle, ot_super);
348
349         down_read(&obj->oo_guard);
350         __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
351         up_read(&obj->oo_guard);
352
353         RETURN(0);
354 }
355
356 /*
357  * Set an extended attribute.
358  * This transaction must have called udmu_xattr_declare_set() first.
359  *
360  * Returns 0 on success or a negative error number on failure.
361  *
362  * No locking is done here.
363  */
364 static int
365 __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
366                       struct osd_thandle *oh)
367 {
368         struct osd_device *osd = osd_obj2dev(obj);
369         char              *dxattr;
370         size_t             sa_size;
371         int                rc;
372
373         ENTRY;
374         LASSERT(obj->oo_sa_hdl);
375         LASSERT(obj->oo_sa_xattr);
376
377         /* Update the SA for additions, modifications, and removals. */
378         rc = -nvlist_size(obj->oo_sa_xattr, &sa_size, NV_ENCODE_XDR);
379         if (rc)
380                 return rc;
381
382         dxattr = osd_zio_buf_alloc(sa_size);
383         if (dxattr == NULL)
384                 RETURN(-ENOMEM);
385
386         rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &sa_size,
387                                 NV_ENCODE_XDR, KM_SLEEP);
388         if (rc)
389                 GOTO(out_free, rc);
390
391         rc = osd_object_sa_update(obj, SA_ZPL_DXATTR(osd), dxattr, sa_size, oh);
392 out_free:
393         osd_zio_buf_free(dxattr, sa_size);
394         RETURN(rc);
395 }
396
397 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
398                        const struct lu_buf *buf, const char *name, int fl,
399                        struct osd_thandle *oh)
400 {
401         uchar_t *nv_value;
402         size_t  size;
403         int     nv_size;
404         int     rc;
405         int     too_big = 0;
406
407         LASSERT(obj->oo_sa_hdl);
408         if (obj->oo_sa_xattr == NULL) {
409                 rc = __osd_xattr_cache(env, obj);
410                 if (rc)
411                         return rc;
412         }
413
414         LASSERT(obj->oo_sa_xattr);
415         /* Limited to 32k to keep nvpair memory allocations small */
416         if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
417                 too_big = 1;
418         } else {
419                 /* Prevent the DXATTR SA from consuming the entire SA
420                  * region */
421                 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
422                 if (rc)
423                         return rc;
424
425                 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
426                         too_big = 1;
427         }
428
429         /* even in case of -EFBIG we must lookup xattr and check can we
430          * rewrite it then delete from SA */
431         rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
432                                         &nv_size);
433         if (rc == 0) {
434                 if (fl & LU_XATTR_CREATE) {
435                         return -EEXIST;
436                 } else if (too_big) {
437                         rc = -nvlist_remove(obj->oo_sa_xattr, name,
438                                                 DATA_TYPE_BYTE_ARRAY);
439                         if (rc < 0)
440                                 return rc;
441                         rc = __osd_sa_xattr_update(env, obj, oh);
442                         return rc == 0 ? -EFBIG : rc;
443                 }
444         } else if (rc == -ENOENT) {
445                 if (fl & LU_XATTR_REPLACE)
446                         return -ENODATA;
447                 else if (too_big)
448                         return -EFBIG;
449         } else {
450                 return rc;
451         }
452
453         /* Ensure xattr doesn't exist in ZAP */
454         if (obj->oo_xattr != ZFS_NO_OBJECT) {
455                 struct osd_device *osd = osd_obj2dev(obj);
456                 uint64_t           objid;
457                 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
458                                  name, 8, 1, &objid);
459                 if (rc == 0) {
460                         rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
461                         if (rc == 0)
462                                 zap_remove(osd->od_os, obj->oo_xattr,
463                                            name, oh->ot_tx);
464                 }
465         }
466
467         rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
468                                     (uchar_t *)buf->lb_buf, buf->lb_len);
469         if (rc)
470                 return rc;
471
472         rc = __osd_sa_xattr_update(env, obj, oh);
473         return rc;
474 }
475
476 int
477 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
478                 const struct lu_buf *buf, const char *name, int fl,
479                 struct osd_thandle *oh)
480 {
481         struct osd_device *osd = osd_obj2dev(obj);
482         dmu_buf_t         *xa_zap_db = NULL;
483         dmu_buf_t         *xa_data_db = NULL;
484         uint64_t           xa_data_obj;
485         sa_handle_t       *sa_hdl = NULL;
486         dmu_tx_t          *tx = oh->ot_tx;
487         uint64_t           size;
488         int                rc;
489
490         LASSERT(obj->oo_sa_hdl);
491
492         if (obj->oo_xattr == ZFS_NO_OBJECT) {
493                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
494
495                 la->la_valid = LA_MODE;
496                 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
497                 rc = __osd_zap_create(env, osd, &xa_zap_db, tx, la,
498                                       obj->oo_db->db_object, 0);
499                 if (rc)
500                         return rc;
501
502                 obj->oo_xattr = xa_zap_db->db_object;
503                 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
504                                 &obj->oo_xattr, 8, oh);
505                 if (rc)
506                         goto out;
507         }
508
509         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
510                          &xa_data_obj);
511         if (rc == 0) {
512                 if (fl & LU_XATTR_CREATE) {
513                         rc = -EEXIST;
514                         goto out;
515                 }
516                 /*
517                  * Entry already exists.
518                  * We'll truncate the existing object.
519                  */
520                 rc = __osd_obj2dbuf(env, osd->od_os, xa_data_obj,
521                                         &xa_data_db);
522                 if (rc)
523                         goto out;
524
525                 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
526                                         SA_HDL_PRIVATE, &sa_hdl);
527                 if (rc)
528                         goto out;
529
530                 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
531                 if (rc)
532                         goto out_sa;
533
534                 rc = -dmu_free_range(osd->od_os, xa_data_db->db_object,
535                                      0, DMU_OBJECT_END, tx);
536                 if (rc)
537                         goto out_sa;
538         } else if (rc == -ENOENT) {
539                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
540                 /*
541                  * Entry doesn't exist, we need to create a new one and a new
542                  * object to store the value.
543                  */
544                 if (fl & LU_XATTR_REPLACE) {
545                         /* should be ENOATTR according to the
546                          * man, but that is undefined here */
547                         rc = -ENODATA;
548                         goto out;
549                 }
550
551                 la->la_valid = LA_MODE;
552                 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
553                 rc = __osd_object_create(env, obj, &xa_data_db, tx, la,
554                                          obj->oo_xattr);
555                 if (rc)
556                         goto out;
557                 xa_data_obj = xa_data_db->db_object;
558
559                 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
560                                         SA_HDL_PRIVATE, &sa_hdl);
561                 if (rc)
562                         goto out;
563
564                 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
565                                 1, &xa_data_obj, tx);
566                 if (rc)
567                         goto out_sa;
568         } else {
569                 /* There was an error looking up the xattr name */
570                 goto out;
571         }
572
573         /* Finally write the xattr value */
574         dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
575
576         size = buf->lb_len;
577         rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
578
579 out_sa:
580         sa_handle_destroy(sa_hdl);
581 out:
582         if (xa_data_db != NULL)
583                 dmu_buf_rele(xa_data_db, FTAG);
584         if (xa_zap_db != NULL)
585                 dmu_buf_rele(xa_zap_db, FTAG);
586
587         return rc;
588 }
589
590 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
591                   const struct lu_buf *buf, const char *name, int fl,
592                   struct thandle *handle)
593 {
594         struct osd_object  *obj = osd_dt_obj(dt);
595         struct osd_thandle *oh;
596         int rc = 0;
597         ENTRY;
598
599         LASSERT(handle != NULL);
600         LASSERT(osd_invariant(obj));
601
602         if (!osd_obj2dev(obj)->od_posix_acl &&
603             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
604              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
605                 RETURN(-EOPNOTSUPP);
606
607         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_OVERFLOW) &&
608             strcmp(name, XATTR_NAME_LINK) == 0)
609                 RETURN(-ENOSPC);
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_db != 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_db != 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 "LPU64" 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_db != 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 }