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