Whamcloud - gitweb
LU-3030 build: Update Master Copyrights pre 2.4 split
[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 /*
31  * Copyright (c) 2012, 2013, Intel Corporation.
32  * Use is subject to license terms.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/osd-zfs/osd_xattr.c
39  * functions to manipulate extended attributes and system attributes
40  *
41  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
42  * Author: Mike Pershin <tappro@whamcloud.com>
43  */
44
45 #ifndef EXPORT_SYMTAB
46 # define EXPORT_SYMTAB
47 #endif
48 #define DEBUG_SUBSYSTEM S_OSD
49
50 #include <lustre_ver.h>
51 #include <libcfs/libcfs.h>
52 #include <lustre_fsfilt.h>
53 #include <obd_support.h>
54 #include <lustre_net.h>
55 #include <obd.h>
56 #include <obd_class.h>
57 #include <lustre_disk.h>
58 #include <lustre_fid.h>
59
60 #include "osd_internal.h"
61
62 #include <sys/dnode.h>
63 #include <sys/dbuf.h>
64 #include <sys/spa.h>
65 #include <sys/stat.h>
66 #include <sys/zap.h>
67 #include <sys/spa_impl.h>
68 #include <sys/zfs_znode.h>
69 #include <sys/dmu_tx.h>
70 #include <sys/dmu_objset.h>
71 #include <sys/dsl_prop.h>
72 #include <sys/sa_impl.h>
73 #include <sys/txg.h>
74
75
76 /*
77  * Copy an extended attribute into the buffer provided, or compute the
78  * required buffer size.
79  *
80  * If buf is NULL, it computes the required buffer size.
81  *
82  * Returns 0 on success or a negative error number on failure.
83  * On success, the number of bytes used / required is stored in 'size'.
84  *
85  * No locking is done here.
86  */
87 int __osd_xattr_load(udmu_objset_t *uos, uint64_t dnode, nvlist_t **sa_xattr)
88 {
89         sa_handle_t *sa_hdl;
90         char        *buf;
91         int          rc, size;
92
93         if (unlikely(dnode == ZFS_NO_OBJECT))
94                 return -ENOENT;
95
96         rc = -sa_handle_get(uos->os, dnode, NULL, SA_HDL_PRIVATE, &sa_hdl);
97         if (rc)
98                 return rc;
99
100         rc = -sa_size(sa_hdl, SA_ZPL_DXATTR(uos), &size);
101         if (rc) {
102                 if (rc == -ENOENT)
103                         rc = -nvlist_alloc(sa_xattr, NV_UNIQUE_NAME, KM_SLEEP);
104                 goto out_sa;
105         }
106
107         buf = sa_spill_alloc(KM_SLEEP);
108         if (buf == NULL) {
109                 rc = -ENOMEM;
110                 goto out_sa;
111         }
112         rc = -sa_lookup(sa_hdl, SA_ZPL_DXATTR(uos), buf, size);
113         if (rc == 0)
114                 rc = -nvlist_unpack(buf, size, sa_xattr, KM_SLEEP);
115         sa_spill_free(buf);
116 out_sa:
117         sa_handle_destroy(sa_hdl);
118
119         return rc;
120 }
121
122 static inline int __osd_xattr_cache(const struct lu_env *env,
123                                     struct osd_object *obj)
124 {
125         LASSERT(obj->oo_sa_xattr == NULL);
126         LASSERT(obj->oo_db != NULL);
127
128         return __osd_xattr_load(&osd_obj2dev(obj)->od_objset,
129                                 obj->oo_db->db_object, &obj->oo_sa_xattr);
130 }
131
132 int __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
133                 const struct lu_buf *buf, const char *name, int *sizep)
134 {
135         uchar_t *nv_value;
136         int      rc;
137
138         LASSERT(obj->oo_sa_hdl);
139
140         if (obj->oo_sa_xattr == NULL) {
141                 rc = __osd_xattr_cache(env, obj);
142                 if (rc)
143                         return rc;
144         }
145
146         LASSERT(obj->oo_sa_xattr);
147         rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
148                         sizep);
149         if (rc)
150                 return rc;
151
152         if (buf == NULL || buf->lb_buf == NULL) {
153                 /* return the required size by *sizep */
154                 return 0;
155         }
156
157         if (*sizep > buf->lb_len)
158                 return -ERANGE; /* match ldiskfs error */
159
160         memcpy(buf->lb_buf, nv_value, *sizep);
161         return 0;
162 }
163
164 int __osd_xattr_get_large(const struct lu_env *env, udmu_objset_t *uos,
165                           uint64_t xattr, struct lu_buf *buf,
166                           const char *name, int *sizep)
167 {
168         dmu_buf_t       *xa_data_db;
169         sa_handle_t     *sa_hdl = NULL;
170         uint64_t         xa_data_obj, size;
171         int              rc;
172
173         /* are there any extended attributes? */
174         if (xattr == ZFS_NO_OBJECT)
175                 return -ENOENT;
176
177         /* Lookup the object number containing the xattr data */
178         rc = -zap_lookup(uos->os, xattr, name, sizeof(uint64_t), 1,
179                         &xa_data_obj);
180         if (rc)
181                 return rc;
182
183         rc = __osd_obj2dbuf(env, uos->os, xa_data_obj, &xa_data_db, FTAG);
184         if (rc)
185                 return rc;
186
187         rc = -sa_handle_get(uos->os, xa_data_obj, NULL, SA_HDL_PRIVATE,
188                         &sa_hdl);
189         if (rc)
190                 goto out_rele;
191
192         /* Get the xattr value length / object size */
193         rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
194         if (rc)
195                 goto out;
196
197         if (size > INT_MAX) {
198                 rc = -EOVERFLOW;
199                 goto out;
200         }
201
202         *sizep = (int)size;
203
204         if (buf == NULL || buf->lb_buf == NULL) {
205                 /* We only need to return the required size */
206                 goto out;
207         }
208         if (*sizep > buf->lb_len) {
209                 rc = -ERANGE; /* match ldiskfs error */
210                 goto out;
211         }
212
213         rc = -dmu_read(uos->os, xa_data_db->db_object, 0,
214                         size, buf->lb_buf, DMU_READ_PREFETCH);
215
216 out:
217         sa_handle_destroy(sa_hdl);
218 out_rele:
219         dmu_buf_rele(xa_data_db, FTAG);
220
221         return rc;
222 }
223
224 int __osd_xattr_get(const struct lu_env *env, struct osd_object *obj,
225                 struct lu_buf *buf, const char *name, int *sizep)
226 {
227         int rc;
228
229         /* check SA_ZPL_DXATTR first then fallback to directory xattr */
230         rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
231         if (rc != -ENOENT)
232                 return rc;
233
234         rc = __osd_xattr_get_large(env, &osd_obj2dev(obj)->od_objset,
235                                    obj->oo_xattr, buf, name, sizep);
236
237         return rc;
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                 struct lustre_capa *capa)
243 {
244         struct osd_object  *obj  = osd_dt_obj(dt);
245         int                 rc, size = 0;
246         ENTRY;
247
248         LASSERT(obj->oo_db != NULL);
249         LASSERT(osd_invariant(obj));
250         LASSERT(dt_object_exists(dt));
251
252         down(&obj->oo_guard);
253         rc = __osd_xattr_get(env, obj, buf, name, &size);
254         up(&obj->oo_guard);
255
256         if (rc == -ENOENT)
257                 rc = -ENODATA;
258         else if (rc == 0)
259                 rc = size;
260         RETURN(rc);
261 }
262
263 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
264                         int vallen, const char *name, struct osd_thandle *oh)
265 {
266         struct osd_device *osd = osd_obj2dev(obj);
267         udmu_objset_t     *uos = &osd->od_objset;
268         dmu_buf_t         *db = obj->oo_db;
269         dmu_tx_t          *tx = oh->ot_tx;
270         uint64_t           xa_data_obj;
271         int                rc = 0;
272         int                here;
273
274         here = dt_object_exists(&obj->oo_dt);
275
276         /* object may be not yet created */
277         if (here) {
278                 LASSERT(db);
279                 LASSERT(obj->oo_sa_hdl);
280                 /* we might just update SA_ZPL_DXATTR */
281                 dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
282
283                 if (obj->oo_xattr == ZFS_NO_OBJECT)
284                         rc = -ENOENT;
285         }
286
287         if (!here || rc == -ENOENT) {
288                 /* we'll be updating SA_ZPL_XATTR */
289                 if (here) {
290                         LASSERT(obj->oo_sa_hdl);
291                         dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 1);
292                 }
293                 /* xattr zap + entry */
294                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
295                 /* xattr value obj */
296                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
297                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
298                 return;
299         }
300
301         rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
302                         &xa_data_obj);
303         if (rc == 0) {
304                 /*
305                  * Entry already exists.
306                  * We'll truncate the existing object.
307                  */
308                 dmu_tx_hold_bonus(tx, xa_data_obj);
309                 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
310                 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
311                 return;
312         } else if (rc == -ENOENT) {
313                 /*
314                  * Entry doesn't exist, we need to create a new one and a new
315                  * object to store the value.
316                  */
317                 dmu_tx_hold_bonus(tx, obj->oo_xattr);
318                 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
319                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
320                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
321                 return;
322         }
323
324         /* An error happened */
325         tx->tx_err = -rc;
326 }
327
328 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
329                 const struct lu_buf *buf, const char *name,
330                 int fl, struct thandle *handle)
331 {
332         struct osd_object  *obj = osd_dt_obj(dt);
333         struct osd_thandle *oh;
334         ENTRY;
335
336         LASSERT(handle != NULL);
337         oh = container_of0(handle, struct osd_thandle, ot_super);
338
339         down(&obj->oo_guard);
340         __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
341         up(&obj->oo_guard);
342
343         RETURN(0);
344 }
345
346 /*
347  * Set an extended attribute.
348  * This transaction must have called udmu_xattr_declare_set() first.
349  *
350  * Returns 0 on success or a negative error number on failure.
351  *
352  * No locking is done here.
353  */
354 static int
355 __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
356                         struct osd_thandle *oh)
357 {
358         struct osd_device *osd = osd_obj2dev(obj);
359         udmu_objset_t     *uos = &osd->od_objset;
360         char              *dxattr;
361         size_t             sa_size;
362         int                rc;
363
364         ENTRY;
365         LASSERT(obj->oo_sa_hdl);
366         LASSERT(obj->oo_sa_xattr);
367
368         /* Update the SA for additions, modifications, and removals. */
369         rc = -nvlist_size(obj->oo_sa_xattr, &sa_size, NV_ENCODE_XDR);
370         if (rc)
371                 return rc;
372
373         dxattr = sa_spill_alloc(KM_SLEEP);
374         if (dxattr == NULL)
375                 RETURN(-ENOMEM);
376
377         rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &sa_size,
378                                 NV_ENCODE_XDR, KM_SLEEP);
379         if (rc)
380                 GOTO(out_free, rc);
381
382         rc = osd_object_sa_update(obj, SA_ZPL_DXATTR(uos), dxattr, sa_size, oh);
383 out_free:
384         sa_spill_free(dxattr);
385         RETURN(rc);
386 }
387
388 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
389                         const struct lu_buf *buf, const char *name, int fl,
390                         struct osd_thandle *oh)
391 {
392         uchar_t *nv_value;
393         size_t  size;
394         int     nv_size;
395         int     rc;
396         int     too_big = 0;
397
398         LASSERT(obj->oo_sa_hdl);
399         if (obj->oo_sa_xattr == NULL) {
400                 rc = __osd_xattr_cache(env, obj);
401                 if (rc)
402                         return rc;
403         }
404
405         LASSERT(obj->oo_sa_xattr);
406         /* Limited to 32k to keep nvpair memory allocations small */
407         if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
408                 too_big = 1;
409         } else {
410                 /* Prevent the DXATTR SA from consuming the entire SA
411                  * region */
412                 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
413                 if (rc)
414                         return rc;
415
416                 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
417                         too_big = 1;
418         }
419
420         /* even in case of -EFBIG we must lookup xattr and check can we
421          * rewrite it then delete from SA */
422         rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
423                                         &nv_size);
424         if (rc == 0) {
425                 if (fl & LU_XATTR_CREATE) {
426                         return -EEXIST;
427                 } else if (too_big) {
428                         rc = -nvlist_remove(obj->oo_sa_xattr, name,
429                                                 DATA_TYPE_BYTE_ARRAY);
430                         if (rc < 0)
431                                 return rc;
432                         rc = __osd_sa_xattr_update(env, obj, oh);
433                         return rc == 0 ? -EFBIG : rc;
434                 }
435         } else if (rc == -ENOENT) {
436                 if (fl & LU_XATTR_REPLACE)
437                         return -ENODATA;
438                 else if (too_big)
439                         return -EFBIG;
440         } else {
441                 return rc;
442         }
443
444         rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
445                                     (uchar_t *)buf->lb_buf, buf->lb_len);
446         if (rc)
447                 return rc;
448
449         rc = __osd_sa_xattr_update(env, obj, oh);
450         return rc;
451 }
452
453 int
454 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
455                 const struct lu_buf *buf, const char *name, int fl,
456                 struct osd_thandle *oh)
457 {
458         struct osd_device *osd = osd_obj2dev(obj);
459         udmu_objset_t     *uos = &osd->od_objset;
460         dmu_buf_t         *xa_zap_db = NULL;
461         dmu_buf_t         *xa_data_db = NULL;
462         uint64_t           xa_data_obj;
463         sa_handle_t       *sa_hdl = NULL;
464         dmu_tx_t          *tx = oh->ot_tx;
465         uint64_t           size;
466         int                rc;
467
468         LASSERT(obj->oo_sa_hdl);
469
470         if (obj->oo_xattr == ZFS_NO_OBJECT) {
471                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
472
473                 la->la_valid = LA_MODE;
474                 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
475                 rc = __osd_zap_create(env, uos, &xa_zap_db, tx, la,
476                                       obj->oo_db->db_object, FTAG, 0);
477                 if (rc)
478                         return rc;
479
480                 obj->oo_xattr = xa_zap_db->db_object;
481                 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(uos),
482                                 &obj->oo_xattr, 8, oh);
483                 if (rc)
484                         goto out;
485         }
486
487         rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
488                         &xa_data_obj);
489         if (rc == 0) {
490                 if (fl & LU_XATTR_CREATE) {
491                         rc = -EEXIST;
492                         goto out;
493                 }
494                 /*
495                  * Entry already exists.
496                  * We'll truncate the existing object.
497                  */
498                 rc = __osd_obj2dbuf(env, uos->os, xa_data_obj,
499                                         &xa_data_db, FTAG);
500                 if (rc)
501                         goto out;
502
503                 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
504                                         SA_HDL_PRIVATE, &sa_hdl);
505                 if (rc)
506                         goto out;
507
508                 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(uos), &size, 8);
509                 if (rc)
510                         goto out_sa;
511
512                 rc = -dmu_free_range(uos->os, xa_data_db->db_object,
513                                         0, DMU_OBJECT_END, tx);
514                 if (rc)
515                         goto out_sa;
516         } else if (rc == -ENOENT) {
517                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
518                 /*
519                  * Entry doesn't exist, we need to create a new one and a new
520                  * object to store the value.
521                  */
522                 if (fl & LU_XATTR_REPLACE) {
523                         /* should be ENOATTR according to the
524                          * man, but that is undefined here */
525                         rc = -ENODATA;
526                         goto out;
527                 }
528
529                 la->la_valid = LA_MODE;
530                 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
531                 rc = __osd_object_create(env, uos, &xa_data_db, tx, la,
532                                          obj->oo_xattr, FTAG);
533                 if (rc)
534                         goto out;
535                 xa_data_obj = xa_data_db->db_object;
536
537                 rc = -sa_handle_get(uos->os, xa_data_obj, NULL,
538                                         SA_HDL_PRIVATE, &sa_hdl);
539                 if (rc)
540                         goto out;
541
542                 rc = -zap_add(uos->os, obj->oo_xattr, name, sizeof(uint64_t),
543                                 1, &xa_data_obj, tx);
544                 if (rc)
545                         goto out_sa;
546         } else {
547                 /* There was an error looking up the xattr name */
548                 goto out;
549         }
550
551         /* Finally write the xattr value */
552         dmu_write(uos->os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
553
554         size = buf->lb_len;
555         rc = -sa_update(sa_hdl, SA_ZPL_SIZE(uos), &size, 8, tx);
556
557 out_sa:
558         sa_handle_destroy(sa_hdl);
559 out:
560         if (xa_data_db != NULL)
561                 dmu_buf_rele(xa_data_db, FTAG);
562         if (xa_zap_db != NULL)
563                 dmu_buf_rele(xa_zap_db, FTAG);
564
565         return rc;
566 }
567
568 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
569                   const struct lu_buf *buf, const char *name, int fl,
570                   struct thandle *handle, struct lustre_capa *capa)
571 {
572         struct osd_object  *obj = osd_dt_obj(dt);
573         struct osd_thandle *oh;
574         int rc = 0;
575         ENTRY;
576
577         LASSERT(handle != NULL);
578         LASSERT(osd_invariant(obj));
579         LASSERT(dt_object_exists(dt));
580         LASSERT(obj->oo_db);
581
582         oh = container_of0(handle, struct osd_thandle, ot_super);
583
584         down(&obj->oo_guard);
585         CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
586                 name, (int)buf->lb_len);
587         rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh, capa);
588         up(&obj->oo_guard);
589
590         RETURN(rc);
591 }
592
593 static void
594 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
595                         const char *name, struct osd_thandle *oh)
596 {
597         struct osd_device *osd = osd_obj2dev(obj);
598         udmu_objset_t     *uos = &osd->od_objset;
599         dmu_tx_t          *tx = oh->ot_tx;
600         uint64_t           xa_data_obj;
601         int                rc;
602
603         /* update SA_ZPL_DXATTR if xattr was in SA */
604         dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
605
606         if (obj->oo_xattr == ZFS_NO_OBJECT)
607                 return;
608
609         rc = -zap_lookup(uos->os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
610         if (rc == 0) {
611                 /*
612                  * Entry exists.
613                  * We'll delete the existing object and ZAP entry.
614                  */
615                 dmu_tx_hold_bonus(tx, xa_data_obj);
616                 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
617                 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
618                 return;
619         } else if (rc == -ENOENT) {
620                 /*
621                  * Entry doesn't exist, nothing to be changed.
622                  */
623                 return;
624         }
625
626         /* An error happened */
627         tx->tx_err = -rc;
628 }
629
630 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
631                         const char *name, struct thandle *handle)
632 {
633         struct osd_object  *obj = osd_dt_obj(dt);
634         struct osd_thandle *oh;
635         ENTRY;
636
637         LASSERT(handle != NULL);
638         LASSERT(dt_object_exists(dt));
639         LASSERT(osd_invariant(obj));
640
641         oh = container_of0(handle, struct osd_thandle, ot_super);
642         LASSERT(oh->ot_tx != NULL);
643         LASSERT(obj->oo_db != NULL);
644
645         down(&obj->oo_guard);
646         __osd_xattr_declare_del(env, obj, name, oh);
647         up(&obj->oo_guard);
648
649         RETURN(0);
650 }
651
652 int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
653                         const char *name, struct osd_thandle *oh)
654 {
655         int rc;
656
657         if (obj->oo_sa_xattr == NULL) {
658                 rc = __osd_xattr_cache(env, obj);
659                 if (rc)
660                         return rc;
661         }
662
663         rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
664         if (rc == 0)
665                 rc = __osd_sa_xattr_update(env, obj, oh);
666         return rc;
667 }
668
669 int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
670                         const char *name, struct osd_thandle *oh)
671 {
672         struct osd_device *osd = osd_obj2dev(obj);
673         udmu_objset_t     *uos = &osd->od_objset;
674         uint64_t           xa_data_obj;
675         int                rc;
676
677         /* try remove xattr from SA at first */
678         rc = __osd_sa_xattr_del(env, obj, name, oh);
679         if (rc != -ENOENT)
680                 return rc;
681
682         if (obj->oo_xattr == ZFS_NO_OBJECT)
683                 return 0;
684
685         rc = -zap_lookup(uos->os, obj->oo_xattr, name, sizeof(uint64_t), 1,
686                         &xa_data_obj);
687         if (rc == -ENOENT) {
688                 rc = 0;
689         } else if (rc == 0) {
690                 /*
691                  * Entry exists.
692                  * We'll delete the existing object and ZAP entry.
693                  */
694                 rc = __osd_object_free(uos, xa_data_obj, oh->ot_tx);
695                 if (rc)
696                         return rc;
697
698                 rc = -zap_remove(uos->os, obj->oo_xattr, name, oh->ot_tx);
699         }
700
701         return rc;
702 }
703
704 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
705                 const char *name, struct thandle *handle,
706                 struct lustre_capa *capa)
707 {
708         struct osd_object  *obj = osd_dt_obj(dt);
709         struct osd_thandle *oh;
710         int                 rc;
711         ENTRY;
712
713         LASSERT(handle != NULL);
714         LASSERT(obj->oo_db != NULL);
715         LASSERT(osd_invariant(obj));
716         LASSERT(dt_object_exists(dt));
717         oh = container_of0(handle, struct osd_thandle, ot_super);
718         LASSERT(oh->ot_tx != NULL);
719
720         down(&obj->oo_guard);
721         rc = __osd_xattr_del(env, obj, name, oh);
722         up(&obj->oo_guard);
723
724         RETURN(rc);
725 }
726
727 static int
728 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
729                 struct lu_buf *lb)
730 {
731         nvpair_t *nvp = NULL;
732         int       len, counted = 0, remain = lb->lb_len;
733         int       rc = 0;
734
735         if (obj->oo_sa_xattr == NULL) {
736                 rc = __osd_xattr_cache(env, obj);
737                 if (rc)
738                         return rc;
739         }
740
741         LASSERT(obj->oo_sa_xattr);
742
743         while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
744                 len = strlen(nvpair_name(nvp));
745                 if (lb->lb_buf != NULL) {
746                         if (len + 1 > remain)
747                                 return -ERANGE;
748
749                         memcpy(lb->lb_buf, nvpair_name(nvp), len);
750                         lb->lb_buf += len;
751                         *((char *)lb->lb_buf) = '\0';
752                         lb->lb_buf++;
753                         remain -= len + 1;
754                 }
755                 counted += len + 1;
756         }
757         return counted;
758 }
759
760 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
761                 struct lu_buf *lb, struct lustre_capa *capa)
762 {
763         struct osd_thread_info *oti = osd_oti_get(env);
764         struct osd_object      *obj = osd_dt_obj(dt);
765         struct osd_device      *osd = osd_obj2dev(obj);
766         udmu_objset_t          *uos = &osd->od_objset;
767         zap_cursor_t           *zc;
768         int                    rc, counted = 0, remain = lb->lb_len;
769         ENTRY;
770
771         LASSERT(obj->oo_db != NULL);
772         LASSERT(osd_invariant(obj));
773         LASSERT(dt_object_exists(dt));
774
775         down(&obj->oo_guard);
776
777         rc = osd_sa_xattr_list(env, obj, lb);
778         if (rc < 0)
779                 GOTO(out, rc);
780         counted = rc;
781         remain -= counted;
782
783         /* continue with dnode xattr if any */
784         if (obj->oo_xattr == ZFS_NO_OBJECT)
785                 GOTO(out, rc = counted);
786
787         rc = -udmu_zap_cursor_init(&zc, uos, obj->oo_xattr, 0);
788         if (rc)
789                 GOTO(out, rc);
790
791         while ((rc = -udmu_zap_cursor_retrieve_key(env, zc, oti->oti_key,
792                                                 MAXNAMELEN)) == 0) {
793                 rc = strlen(oti->oti_key);
794                 if (lb->lb_buf != NULL) {
795                         if (rc + 1 > remain)
796                                 RETURN(-ERANGE);
797
798                         memcpy(lb->lb_buf, oti->oti_key, rc);
799                         lb->lb_buf += rc;
800                         *((char *)lb->lb_buf) = '\0';
801                         lb->lb_buf++;
802                         remain -= rc + 1;
803                 }
804                 counted += rc + 1;
805
806                 zap_cursor_advance(zc);
807         }
808         if (rc == -ENOENT) /* no more kes in the index */
809                 rc = 0;
810         else if (unlikely(rc < 0))
811                 GOTO(out_fini, rc);
812         rc = counted;
813
814 out_fini:
815         udmu_zap_cursor_fini(zc);
816 out:
817         up(&obj->oo_guard);
818         RETURN(rc);
819
820 }
821
822