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