Whamcloud - gitweb
d6bed9a8401312286d711a0ff22cdbdee04e3948
[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 <libcfs/libcfs.h>
42 #include <obd_support.h>
43 #include <lustre_net.h>
44 #include <obd.h>
45 #include <obd_class.h>
46 #include <lustre_disk.h>
47 #include <lustre_fid.h>
48
49 #include "osd_internal.h"
50
51 #include <sys/dnode.h>
52 #include <sys/dbuf.h>
53 #include <sys/spa.h>
54 #include <sys/stat.h>
55 #include <sys/zap.h>
56 #include <sys/spa_impl.h>
57 #include <sys/zfs_znode.h>
58 #include <sys/dmu_tx.h>
59 #include <sys/dmu_objset.h>
60 #include <sys/dsl_prop.h>
61 #include <sys/sa_impl.h>
62 #include <sys/txg.h>
63
64 #include <linux/posix_acl_xattr.h>
65 #include <lustre_scrub.h>
66
67 int __osd_xattr_load(struct osd_device *osd, sa_handle_t *hdl, nvlist_t **sa)
68 {
69         char        *buf;
70         int          rc, size;
71
72         rc = -sa_size(hdl, SA_ZPL_DXATTR(osd), &size);
73         if (rc) {
74                 if (rc == -ENOENT)
75                         rc = -nvlist_alloc(sa, NV_UNIQUE_NAME, KM_SLEEP);
76                 goto out_sa;
77         }
78
79         buf = osd_zio_buf_alloc(size);
80         if (buf == NULL) {
81                 rc = -ENOMEM;
82                 goto out_sa;
83         }
84         rc = -sa_lookup(hdl, SA_ZPL_DXATTR(osd), buf, size);
85         if (rc == 0)
86                 rc = -nvlist_unpack(buf, size, sa, KM_SLEEP);
87         osd_zio_buf_free(buf, size);
88 out_sa:
89
90         return rc;
91 }
92
93 static inline int __osd_xattr_cache(struct osd_object *obj)
94 {
95         LASSERT(obj->oo_sa_hdl);
96         if (obj->oo_sa_xattr != NULL)
97                 return 0;
98         return __osd_xattr_load(osd_obj2dev(obj),
99                                 obj->oo_sa_hdl, &obj->oo_sa_xattr);
100 }
101
102 static int
103 __osd_sa_xattr_get(const struct lu_env *env, struct osd_object *obj,
104                    const struct lu_buf *buf, const char *name, int *sizep)
105 {
106         uchar_t *nv_value;
107         int      rc = 0;
108
109         rc = __osd_xattr_cache(obj);
110         if (rc)
111                 return rc;
112
113         LASSERT(obj->oo_sa_xattr);
114         rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name,
115                                        &nv_value, sizep);
116         if (rc)
117                 return rc;
118
119         if (buf == NULL || buf->lb_buf == NULL) {
120                 /* return the required size by *sizep */
121                 return 0;
122         }
123
124         if (*sizep > buf->lb_len)
125                 return -ERANGE; /* match ldiskfs error */
126
127         memcpy(buf->lb_buf, nv_value, *sizep);
128         return 0;
129 }
130
131 int __osd_xattr_get_large(const struct lu_env *env, struct osd_device *osd,
132                           uint64_t xattr, struct lu_buf *buf,
133                           const char *name, int *sizep)
134 {
135         dnode_t         *xa_data_dn;
136         sa_handle_t *sa_hdl = NULL;
137         uint64_t         xa_data_obj, size;
138         int              rc;
139
140         /* are there any extended attributes? */
141         if (xattr == ZFS_NO_OBJECT)
142                 return -ENOENT;
143
144         /* Lookup the object number containing the xattr data */
145         rc = -zap_lookup(osd->od_os, xattr, name, sizeof(uint64_t), 1,
146                         &xa_data_obj);
147         if (rc)
148                 return rc;
149
150         rc = __osd_obj2dnode(osd->od_os, xa_data_obj, &xa_data_dn);
151         if (rc)
152                 return rc;
153
154         rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL, SA_HDL_PRIVATE,
155                         &sa_hdl);
156         if (rc)
157                 goto out_rele;
158
159         /* Get the xattr value length / object size */
160         rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
161         if (rc)
162                 goto out;
163
164         if (size > INT_MAX) {
165                 rc = -EOVERFLOW;
166                 goto out;
167         }
168
169         *sizep = (int)size;
170
171         if (buf == NULL || buf->lb_buf == NULL) {
172                 /* We only need to return the required size */
173                 goto out;
174         }
175         if (*sizep > buf->lb_len) {
176                 rc = -ERANGE; /* match ldiskfs error */
177                 goto out;
178         }
179
180         rc = -dmu_read(osd->od_os, xa_data_dn->dn_object, 0,
181                         size, buf->lb_buf, DMU_READ_PREFETCH);
182
183 out:
184         sa_handle_destroy(sa_hdl);
185 out_rele:
186         osd_dnode_rele(xa_data_dn);
187
188         return rc;
189 }
190
191 /**
192  * Copy an extended attribute into the buffer provided, or compute
193  * the required buffer size if \a buf is NULL.
194  *
195  * On success, the number of bytes used or required is stored in \a sizep.
196  *
197  * Note that no locking is done here.
198  *
199  * \param[in] env      execution environment
200  * \param[in] obj      object for which to retrieve xattr
201  * \param[out] buf     buffer to store xattr value in
202  * \param[in] name     name of xattr to copy
203  * \param[out] sizep   bytes used or required to store xattr
204  *
205  * \retval 0           on success
206  * \retval negative    negated errno on failure
207  */
208 int osd_xattr_get_internal(const struct lu_env *env, struct osd_object *obj,
209                            struct lu_buf *buf, const char *name, int *sizep)
210 {
211         int rc;
212
213         if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
214                 return -ENOENT;
215
216         /* check SA_ZPL_DXATTR first then fallback to directory xattr */
217         rc = __osd_sa_xattr_get(env, obj, buf, name, sizep);
218         if (rc != -ENOENT)
219                 return rc;
220
221         return __osd_xattr_get_large(env, osd_obj2dev(obj), obj->oo_xattr,
222                                      buf, name, sizep);
223 }
224
225 static int osd_get_pfid_from_lma(const struct lu_env *env,
226                                  struct osd_object *obj,
227                                  struct lu_buf *buf, int *sizep)
228 {
229         struct osd_thread_info *info = osd_oti_get(env);
230         struct lustre_ost_attrs *loa =
231                 (struct lustre_ost_attrs *)&info->oti_buf;
232         struct lustre_mdt_attrs *lma = &loa->loa_lma;
233         struct filter_fid *ff;
234         struct ost_layout *ol;
235         struct lu_buf tbuf = {
236                 .lb_buf = loa,
237                 .lb_len = sizeof(info->oti_buf),
238         };
239         int rc;
240         ENTRY;
241
242         CLASSERT(sizeof(info->oti_buf) >= sizeof(*loa));
243         rc = osd_xattr_get_internal(env, obj, &tbuf,
244                                     XATTR_NAME_LMA, sizep);
245         if (rc)
246                 RETURN(rc);
247
248         lustre_loa_swab(loa, true);
249         LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
250
251         *sizep = sizeof(*ff);
252         if (buf->lb_len == 0 || !buf->lb_buf)
253                 RETURN(0);
254
255         if (buf->lb_len < *sizep)
256                 RETURN(-ERANGE);
257
258         ff = buf->lb_buf;
259         ol = &ff->ff_layout;
260         ol->ol_stripe_count = cpu_to_le32(loa->loa_parent_fid.f_ver >>
261                                           PFID_STRIPE_IDX_BITS);
262         ol->ol_stripe_size = cpu_to_le32(loa->loa_stripe_size);
263         loa->loa_parent_fid.f_ver &= PFID_STRIPE_COUNT_MASK;
264         fid_cpu_to_le(&ff->ff_parent, &loa->loa_parent_fid);
265         if (lma->lma_compat & LMAC_COMP_INFO) {
266                 ol->ol_comp_start = cpu_to_le64(loa->loa_comp_start);
267                 ol->ol_comp_end = cpu_to_le64(loa->loa_comp_end);
268                 ol->ol_comp_id = cpu_to_le32(loa->loa_comp_id);
269         } else {
270                 ol->ol_comp_start = 0;
271                 ol->ol_comp_end = 0;
272                 ol->ol_comp_id = 0;
273         }
274
275         RETURN(0);
276 }
277
278 int osd_xattr_get(const struct lu_env *env, struct dt_object *dt,
279                   struct lu_buf *buf, const char *name)
280 {
281         struct osd_object  *obj  = osd_dt_obj(dt);
282         int                 rc, size = 0;
283         ENTRY;
284
285         LASSERT(obj->oo_dn != NULL);
286         LASSERT(osd_invariant(obj));
287
288         if (!osd_obj2dev(obj)->od_posix_acl &&
289             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
290              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
291                 RETURN(-EOPNOTSUPP);
292
293         down_read(&obj->oo_guard);
294         /* For the OST migrated from ldiskfs, the PFID EA may
295          * be stored in LMA because of ldiskfs inode size. */
296         if (strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma)
297                 rc = osd_get_pfid_from_lma(env, obj, buf, &size);
298         else
299                 rc = osd_xattr_get_internal(env, obj, buf, name, &size);
300         up_read(&obj->oo_guard);
301
302         if (rc == -ENOENT)
303                 rc = -ENODATA;
304         else if (rc == 0)
305                 rc = size;
306         RETURN(rc);
307 }
308
309 /* the function is used to declare EAs when SA is not supported */
310 void __osd_xattr_declare_legacy(const struct lu_env *env,
311                                 struct osd_object *obj,
312                                 int vallen, const char *name,
313                                 struct osd_thandle *oh)
314 {
315         struct osd_device *osd = osd_obj2dev(obj);
316         dmu_tx_t *tx = oh->ot_tx;
317         uint64_t xa_data_obj;
318         int rc;
319
320         if (obj->oo_xattr == ZFS_NO_OBJECT) {
321                 /* xattr zap + entry */
322                 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, TRUE, (char *) name);
323                 /* xattr value obj */
324                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
325                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
326                 return;
327         }
328
329         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
330                         &xa_data_obj);
331         if (rc == 0) {
332                 /*
333                  * Entry already exists.
334                  * We'll truncate the existing object.
335                  */
336                 dmu_tx_hold_bonus(tx, xa_data_obj);
337                 dmu_tx_hold_free(tx, xa_data_obj, vallen, DMU_OBJECT_END);
338                 dmu_tx_hold_write(tx, xa_data_obj, 0, vallen);
339         } else if (rc == -ENOENT) {
340                 /*
341                  * Entry doesn't exist, we need to create a new one and a new
342                  * object to store the value.
343                  */
344                 dmu_tx_hold_bonus(tx, obj->oo_xattr);
345                 dmu_tx_hold_zap(tx, obj->oo_xattr, TRUE, (char *) name);
346                 dmu_tx_hold_sa_create(tx, ZFS_SA_BASE_ATTR_SIZE);
347                 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, vallen);
348         }
349 }
350
351 void __osd_xattr_declare_set(const struct lu_env *env, struct osd_object *obj,
352                              int vallen, const char *name,
353                              struct osd_thandle *oh)
354 {
355         dmu_tx_t *tx = oh->ot_tx;
356         int bonuslen;
357
358         if (unlikely(obj->oo_destroyed))
359                 return;
360
361         if (unlikely(!osd_obj2dev(obj)->od_xattr_in_sa)) {
362                 __osd_xattr_declare_legacy(env, obj, vallen, name, oh);
363                 return;
364         }
365
366         /* declare EA in SA */
367         if (dt_object_exists(&obj->oo_dt)) {
368                 LASSERT(obj->oo_sa_hdl);
369                 /* XXX: it should be possible to skip spill
370                  * declaration if specific EA is part of
371                  * bonus and doesn't grow */
372                 dmu_tx_hold_spill(tx, obj->oo_dn->dn_object);
373                 return;
374         }
375
376         bonuslen = osd_obj_bonuslen(obj);
377
378         /* the object doesn't exist, but we've declared bonus
379          * in osd_declare_object_create() yet */
380         if (obj->oo_ea_in_bonus > bonuslen) {
381                 /* spill has been declared already */
382         } else if (obj->oo_ea_in_bonus + vallen > bonuslen) {
383                 /* we're about to exceed bonus, let's declare spill */
384                 dmu_tx_hold_spill(tx, DMU_NEW_OBJECT);
385         }
386         obj->oo_ea_in_bonus += vallen;
387 }
388
389 int osd_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
390                           const struct lu_buf *buf, const char *name,
391                           int fl, struct thandle *handle)
392 {
393         struct osd_object  *obj = osd_dt_obj(dt);
394         struct osd_thandle *oh;
395         ENTRY;
396
397         LASSERT(handle != NULL);
398         oh = container_of0(handle, struct osd_thandle, ot_super);
399
400         down_read(&obj->oo_guard);
401         __osd_xattr_declare_set(env, obj, buf->lb_len, name, oh);
402         up_read(&obj->oo_guard);
403
404         RETURN(0);
405 }
406
407 int __osd_sa_attr_init(const struct lu_env *env, struct osd_object *obj,
408                        struct osd_thandle *oh)
409 {
410         sa_bulk_attr_t  *bulk = osd_oti_get(env)->oti_attr_bulk;
411         struct osa_attr *osa = &osd_oti_get(env)->oti_osa;
412         struct lu_buf *lb = &osd_oti_get(env)->oti_xattr_lbuf;
413         struct osd_device *osd = osd_obj2dev(obj);
414         uint64_t crtime[2], gen;
415         timestruc_t now;
416         size_t size;
417         int rc, cnt;
418
419         obj->oo_late_xattr = 0;
420         obj->oo_late_attr_set = 0;
421
422         gen = dmu_tx_get_txg(oh->ot_tx);
423         gethrestime(&now);
424         ZFS_TIME_ENCODE(&now, crtime);
425
426         osa->atime[0] = obj->oo_attr.la_atime;
427         osa->ctime[0] = obj->oo_attr.la_ctime;
428         osa->mtime[0] = obj->oo_attr.la_mtime;
429         osa->mode = obj->oo_attr.la_mode;
430         osa->uid = obj->oo_attr.la_uid;
431         osa->gid = obj->oo_attr.la_gid;
432         osa->rdev = obj->oo_attr.la_rdev;
433         osa->nlink = obj->oo_attr.la_nlink;
434         osa->flags = attrs_fs2zfs(obj->oo_attr.la_flags);
435         osa->size  = obj->oo_attr.la_size;
436 #ifdef ZFS_PROJINHERIT
437         if (osd->od_projectused_dn) {
438                 if (obj->oo_attr.la_valid & LA_PROJID)
439                         osa->projid = obj->oo_attr.la_projid;
440                 else
441                         osa->projid = ZFS_DEFAULT_PROJID;
442                 osa->flags |= ZFS_PROJID;
443                 obj->oo_with_projid = 1;
444         }
445 #endif
446
447         cnt = 0;
448         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(osd), NULL, &osa->mode, 8);
449         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_SIZE(osd), NULL, &osa->size, 8);
450         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GEN(osd), NULL, &gen, 8);
451         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_UID(osd), NULL, &osa->uid, 8);
452         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_GID(osd), NULL, &osa->gid, 8);
453         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PARENT(osd), NULL,
454                          &obj->oo_parent, 8);
455         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(osd), NULL, &osa->flags, 8);
456         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(osd), NULL, osa->atime, 16);
457         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(osd), NULL, osa->mtime, 16);
458         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(osd), NULL, osa->ctime, 16);
459         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CRTIME(osd), NULL, crtime, 16);
460         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_LINKS(osd), NULL, &osa->nlink, 8);
461 #ifdef ZFS_PROJINHERIT
462         if (osd->od_projectused_dn)
463                 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_PROJID(osd), NULL,
464                                  &osa->projid, 8);
465 #endif
466         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_RDEV(osd), NULL, &osa->rdev, 8);
467         LASSERT(cnt <= ARRAY_SIZE(osd_oti_get(env)->oti_attr_bulk));
468
469         /* Update the SA for additions, modifications, and removals. */
470         rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
471         if (rc)
472                 return rc;
473
474         lu_buf_check_and_alloc(lb, size);
475         if (lb->lb_buf == NULL) {
476                 CERROR("%s: can't allocate buffer for xattr update\n",
477                                 osd->od_svname);
478                 return -ENOMEM;
479         }
480
481         rc = -nvlist_pack(obj->oo_sa_xattr, (char **)&lb->lb_buf, &size,
482                           NV_ENCODE_XDR, KM_SLEEP);
483         if (rc)
484                 return rc;
485
486         SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_DXATTR(osd), NULL, lb->lb_buf, size);
487
488         rc = -sa_replace_all_by_template(obj->oo_sa_hdl, bulk, cnt, oh->ot_tx);
489
490         return rc;
491 }
492
493 int __osd_sa_xattr_update(const struct lu_env *env, struct osd_object *obj,
494                            struct osd_thandle *oh)
495 {
496         struct lu_buf     *lb = &osd_oti_get(env)->oti_xattr_lbuf;
497         struct osd_device *osd = osd_obj2dev(obj);
498         char              *dxattr;
499         size_t             size;
500         int                rc;
501
502         obj->oo_late_xattr = 0;
503
504         /* Update the SA for additions, modifications, and removals. */
505         rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
506         if (rc)
507                 return rc;
508
509         lu_buf_check_and_alloc(lb, size);
510         if (lb->lb_buf == NULL) {
511                 CERROR("%s: can't allocate buffer for xattr update\n",
512                                 osd->od_svname);
513                 return -ENOMEM;
514         }
515
516         dxattr = lb->lb_buf;
517         rc = -nvlist_pack(obj->oo_sa_xattr, &dxattr, &size,
518                         NV_ENCODE_XDR, KM_SLEEP);
519         if (rc)
520                 return rc;
521         LASSERT(dxattr == lb->lb_buf);
522
523         sa_update(obj->oo_sa_hdl, SA_ZPL_DXATTR(osd), dxattr, size, oh->ot_tx);
524
525         return 0;
526 }
527
528 /*
529  * Set an extended attribute.
530  * This transaction must have called udmu_xattr_declare_set() first.
531  *
532  * Returns 0 on success or a negative error number on failure.
533  *
534  * No locking is done here.
535  */
536 int __osd_sa_xattr_schedule_update(const struct lu_env *env,
537                                    struct osd_object *obj,
538                                    struct osd_thandle *oh)
539 {
540         ENTRY;
541         LASSERT(obj->oo_sa_hdl);
542         LASSERT(obj->oo_sa_xattr);
543
544         /* schedule batched SA update in osd_object_sa_dirty_rele() */
545         obj->oo_late_xattr = 1;
546         osd_object_sa_dirty_add(obj, oh);
547
548         RETURN(0);
549
550 }
551
552 int __osd_sa_xattr_set(const struct lu_env *env, struct osd_object *obj,
553                        const struct lu_buf *buf, const char *name, int fl,
554                        struct osd_thandle *oh)
555 {
556         uchar_t *nv_value;
557         size_t  size;
558         int     nv_size;
559         int     rc;
560         int     too_big = 0;
561
562         rc = __osd_xattr_cache(obj);
563         if (rc)
564                 return rc;
565
566         LASSERT(obj->oo_sa_xattr);
567         /* Limited to 32k to keep nvpair memory allocations small */
568         if (buf->lb_len > DXATTR_MAX_ENTRY_SIZE) {
569                 too_big = 1;
570         } else {
571                 /* Prevent the DXATTR SA from consuming the entire SA
572                  * region */
573                 rc = -nvlist_size(obj->oo_sa_xattr, &size, NV_ENCODE_XDR);
574                 if (rc)
575                         return rc;
576
577                 if (size + buf->lb_len > DXATTR_MAX_SA_SIZE)
578                         too_big = 1;
579         }
580
581         /* even in case of -EFBIG we must lookup xattr and check can we
582          * rewrite it then delete from SA */
583         rc = -nvlist_lookup_byte_array(obj->oo_sa_xattr, name, &nv_value,
584                                         &nv_size);
585         if (rc == 0) {
586                 if (fl & LU_XATTR_CREATE) {
587                         return -EEXIST;
588                 } else if (too_big) {
589                         rc = -nvlist_remove(obj->oo_sa_xattr, name,
590                                                 DATA_TYPE_BYTE_ARRAY);
591                         if (rc < 0)
592                                 return rc;
593                         rc = __osd_sa_xattr_schedule_update(env, obj, oh);
594                         return rc == 0 ? -EFBIG : rc;
595                 }
596         } else if (rc == -ENOENT) {
597                 if (fl & LU_XATTR_REPLACE)
598                         return -ENODATA;
599                 else if (too_big)
600                         return -EFBIG;
601         } else {
602                 return rc;
603         }
604
605         /* Ensure xattr doesn't exist in ZAP */
606         if (obj->oo_xattr != ZFS_NO_OBJECT) {
607                 struct osd_device *osd = osd_obj2dev(obj);
608                 uint64_t           objid;
609                 rc = -zap_lookup(osd->od_os, obj->oo_xattr,
610                                  name, 8, 1, &objid);
611                 if (rc == 0) {
612                         rc = -dmu_object_free(osd->od_os, objid, oh->ot_tx);
613                         if (rc == 0)
614                                 zap_remove(osd->od_os, obj->oo_xattr,
615                                            name, oh->ot_tx);
616                 }
617         }
618
619         rc = -nvlist_add_byte_array(obj->oo_sa_xattr, name,
620                                     (uchar_t *)buf->lb_buf, buf->lb_len);
621         if (rc)
622                 return rc;
623
624         /* batch updates only for just created dnodes where we
625          * used to set number of EAs in a single transaction */
626         if (obj->oo_dn->dn_allocated_txg == oh->ot_tx->tx_txg)
627                 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
628         else
629                 rc = __osd_sa_xattr_update(env, obj, oh);
630
631         return rc;
632 }
633
634 int
635 __osd_xattr_set(const struct lu_env *env, struct osd_object *obj,
636                 const struct lu_buf *buf, const char *name, int fl,
637                 struct osd_thandle *oh)
638 {
639         struct osd_device *osd = osd_obj2dev(obj);
640         dnode_t *xa_zap_dn = NULL;
641         dnode_t *xa_data_dn = NULL;
642         uint64_t           xa_data_obj;
643         sa_handle_t       *sa_hdl = NULL;
644         dmu_tx_t          *tx = oh->ot_tx;
645         uint64_t           size;
646         int                rc;
647
648         LASSERT(obj->oo_sa_hdl);
649
650         if (obj->oo_xattr == ZFS_NO_OBJECT) {
651                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
652
653                 la->la_valid = LA_MODE;
654                 la->la_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
655                 rc = __osd_zap_create(env, osd, &xa_zap_dn, tx, la, 0, 0);
656                 if (rc)
657                         return rc;
658
659                 obj->oo_xattr = xa_zap_dn->dn_object;
660                 rc = osd_object_sa_update(obj, SA_ZPL_XATTR(osd),
661                                 &obj->oo_xattr, 8, oh);
662                 if (rc)
663                         goto out;
664         }
665
666         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
667                          &xa_data_obj);
668         if (rc == 0) {
669                 if (fl & LU_XATTR_CREATE) {
670                         rc = -EEXIST;
671                         goto out;
672                 }
673                 /*
674                  * Entry already exists.
675                  * We'll truncate the existing object.
676                  */
677                 rc = __osd_obj2dnode(osd->od_os, xa_data_obj, &xa_data_dn);
678                 if (rc)
679                         goto out;
680
681                 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
682                                         SA_HDL_PRIVATE, &sa_hdl);
683                 if (rc)
684                         goto out;
685
686                 rc = -sa_lookup(sa_hdl, SA_ZPL_SIZE(osd), &size, 8);
687                 if (rc)
688                         goto out_sa;
689
690                 rc = -dmu_free_range(osd->od_os, xa_data_dn->dn_object,
691                                      0, DMU_OBJECT_END, tx);
692                 if (rc)
693                         goto out_sa;
694         } else if (rc == -ENOENT) {
695                 struct lu_attr *la = &osd_oti_get(env)->oti_la;
696                 /*
697                  * Entry doesn't exist, we need to create a new one and a new
698                  * object to store the value.
699                  */
700                 if (fl & LU_XATTR_REPLACE) {
701                         /* should be ENOATTR according to the
702                          * man, but that is undefined here */
703                         rc = -ENODATA;
704                         goto out;
705                 }
706
707                 la->la_valid = LA_MODE;
708                 la->la_mode = S_IFREG | S_IRUGO | S_IWUSR;
709                 rc = __osd_object_create(env, obj, &xa_data_dn, tx, la);
710                 if (rc)
711                         goto out;
712                 xa_data_obj = xa_data_dn->dn_object;
713
714                 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
715                                         SA_HDL_PRIVATE, &sa_hdl);
716                 if (rc)
717                         goto out;
718
719                 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
720                                 1, &xa_data_obj, tx);
721                 if (rc)
722                         goto out_sa;
723         } else {
724                 /* There was an error looking up the xattr name */
725                 goto out;
726         }
727
728         /* Finally write the xattr value */
729         dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
730
731         size = buf->lb_len;
732         rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
733
734 out_sa:
735         sa_handle_destroy(sa_hdl);
736 out:
737         if (xa_data_dn != NULL)
738                 osd_dnode_rele(xa_data_dn);
739         if (xa_zap_dn != NULL)
740                 osd_dnode_rele(xa_zap_dn);
741
742         return rc;
743 }
744
745 static int osd_xattr_split_pfid(const struct lu_env *env,
746                                 struct osd_object *obj, struct osd_thandle *oh)
747 {
748         struct osd_thread_info *info = osd_oti_get(env);
749         struct lustre_ost_attrs *loa =
750                 (struct lustre_ost_attrs *)&info->oti_buf;
751         struct lustre_mdt_attrs *lma = &loa->loa_lma;
752         struct lu_buf buf = {
753                 .lb_buf = loa,
754                 .lb_len = sizeof(info->oti_buf),
755         };
756         int size;
757         int rc;
758         ENTRY;
759
760         CLASSERT(sizeof(info->oti_buf) >= sizeof(*loa));
761         rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
762         if (rc)
763                 RETURN(rc);
764
765         lustre_loa_swab(loa, true);
766         LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
767
768         lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
769         lustre_lma_swab(lma);
770         buf.lb_buf = lma;
771         buf.lb_len = sizeof(*lma);
772         rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
773                                     LU_XATTR_REPLACE, oh);
774         if (!rc)
775                 obj->oo_pfid_in_lma = 0;
776
777         RETURN(rc);
778 }
779
780 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
781                   const struct lu_buf *buf, const char *name, int fl,
782                   struct thandle *handle)
783 {
784         struct osd_object  *obj = osd_dt_obj(dt);
785         struct osd_thandle *oh;
786         int rc = 0;
787         ENTRY;
788
789         LASSERT(handle != NULL);
790         LASSERT(osd_invariant(obj));
791
792         if (!osd_obj2dev(obj)->od_posix_acl &&
793             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
794              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
795                 RETURN(-EOPNOTSUPP);
796
797         oh = container_of0(handle, struct osd_thandle, ot_super);
798
799         down_write(&obj->oo_guard);
800         CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
801                 name, (int)buf->lb_len);
802         /* For the OST migrated from ldiskfs, the PFID EA may
803          * be stored in LMA because of ldiskfs inode size. */
804         if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 &&
805                      obj->oo_pfid_in_lma)) {
806                 rc = osd_xattr_split_pfid(env, obj, oh);
807                 if (!rc)
808                         fl = LU_XATTR_CREATE;
809         }
810
811         if (!rc)
812                 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
813         up_write(&obj->oo_guard);
814
815         RETURN(rc);
816 }
817
818 static void
819 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
820                         const char *name, struct osd_thandle *oh)
821 {
822         struct osd_device *osd = osd_obj2dev(obj);
823         dmu_tx_t          *tx = oh->ot_tx;
824         uint64_t           xa_data_obj;
825         int                rc;
826
827         /* update SA_ZPL_DXATTR if xattr was in SA */
828         dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
829
830         if (obj->oo_xattr == ZFS_NO_OBJECT)
831                 return;
832
833         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
834         if (rc == 0) {
835                 /*
836                  * Entry exists.
837                  * We'll delete the existing object and ZAP entry.
838                  */
839                 dmu_tx_hold_bonus(tx, xa_data_obj);
840                 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
841                 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
842                 return;
843         } else if (rc == -ENOENT) {
844                 /*
845                  * Entry doesn't exist, nothing to be changed.
846                  */
847                 return;
848         }
849
850         /* An error happened */
851         tx->tx_err = -rc;
852 }
853
854 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
855                           const char *name, struct thandle *handle)
856 {
857         struct osd_object  *obj = osd_dt_obj(dt);
858         struct osd_thandle *oh;
859         ENTRY;
860
861         LASSERT(handle != NULL);
862         LASSERT(osd_invariant(obj));
863
864         oh = container_of0(handle, struct osd_thandle, ot_super);
865         LASSERT(oh->ot_tx != NULL);
866         LASSERT(obj->oo_dn != NULL);
867
868         down_read(&obj->oo_guard);
869         if (likely(dt_object_exists(&obj->oo_dt) && !obj->oo_destroyed))
870                 __osd_xattr_declare_del(env, obj, name, oh);
871         up_read(&obj->oo_guard);
872
873         RETURN(0);
874 }
875
876 static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
877                               const char *name, struct osd_thandle *oh)
878 {
879         int rc;
880
881         rc = __osd_xattr_cache(obj);
882         if (rc)
883                 return rc;
884
885         rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
886         if (rc == 0)
887                 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
888         return rc;
889 }
890
891 static int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
892                            const char *name, struct osd_thandle *oh)
893 {
894         struct osd_device *osd = osd_obj2dev(obj);
895         uint64_t           xa_data_obj;
896         int                rc;
897
898         if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
899                 return -ENOENT;
900
901         /* try remove xattr from SA at first */
902         rc = __osd_sa_xattr_del(env, obj, name, oh);
903         if (rc != -ENOENT)
904                 return rc;
905
906         if (obj->oo_xattr == ZFS_NO_OBJECT)
907                 return 0;
908
909         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
910                         &xa_data_obj);
911         if (rc == -ENOENT) {
912                 rc = 0;
913         } else if (rc == 0) {
914                 /*
915                  * Entry exists.
916                  * We'll delete the existing object and ZAP entry.
917                  */
918                 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
919                 if (rc)
920                         return rc;
921
922                 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
923         }
924
925         return rc;
926 }
927
928 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
929                   const char *name, struct thandle *handle)
930 {
931         struct osd_object  *obj = osd_dt_obj(dt);
932         struct osd_thandle *oh;
933         int                 rc;
934         ENTRY;
935
936         LASSERT(handle != NULL);
937         LASSERT(obj->oo_dn != NULL);
938         LASSERT(osd_invariant(obj));
939         LASSERT(dt_object_exists(dt));
940         oh = container_of0(handle, struct osd_thandle, ot_super);
941         LASSERT(oh->ot_tx != NULL);
942
943         if (!osd_obj2dev(obj)->od_posix_acl &&
944             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
945              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
946                 RETURN(-EOPNOTSUPP);
947
948         down_write(&obj->oo_guard);
949         /* For the OST migrated from ldiskfs, the PFID EA may
950          * be stored in LMA because of ldiskfs inode size. */
951         if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma))
952                 rc = osd_xattr_split_pfid(env, obj, oh);
953         else
954                 rc = __osd_xattr_del(env, obj, name, oh);
955         up_write(&obj->oo_guard);
956
957         RETURN(rc);
958 }
959
960 void osd_declare_xattrs_destroy(const struct lu_env *env,
961                                 struct osd_object *obj, struct osd_thandle *oh)
962 {
963         struct osd_device *osd = osd_obj2dev(obj);
964         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
965         uint64_t           oid = obj->oo_xattr, xid;
966         dmu_tx_t          *tx = oh->ot_tx;
967         zap_cursor_t      *zc;
968         int                rc;
969
970         if (oid == ZFS_NO_OBJECT)
971                 return; /* Nothing to do for SA xattrs */
972
973         /* Declare to free the ZAP holding xattrs */
974         dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
975
976         rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
977         if (rc)
978                 goto out;
979
980         while (zap_cursor_retrieve(zc, za) == 0) {
981                 LASSERT(za->za_num_integers == 1);
982                 LASSERT(za->za_integer_length == sizeof(uint64_t));
983
984                 rc = -zap_lookup(osd->od_os, oid, za->za_name,
985                                  sizeof(uint64_t), 1, &xid);
986                 if (rc) {
987                         CERROR("%s: xattr %s lookup failed: rc = %d\n",
988                                osd->od_svname, za->za_name, rc);
989                         break;
990                 }
991                 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
992
993                 zap_cursor_advance(zc);
994         }
995
996         osd_zap_cursor_fini(zc);
997 out:
998         if (rc && tx->tx_err == 0)
999                 tx->tx_err = -rc;
1000 }
1001
1002 int osd_xattrs_destroy(const struct lu_env *env,
1003                        struct osd_object *obj, struct osd_thandle *oh)
1004 {
1005         struct osd_device *osd = osd_obj2dev(obj);
1006         dmu_tx_t          *tx = oh->ot_tx;
1007         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1008         zap_cursor_t      *zc;
1009         uint64_t           xid;
1010         int                rc;
1011
1012         /* The transaction must have been assigned to a transaction group. */
1013         LASSERT(tx->tx_txg != 0);
1014
1015         if (obj->oo_xattr == ZFS_NO_OBJECT)
1016                 return 0; /* Nothing to do for SA xattrs */
1017
1018         /* Free the ZAP holding the xattrs */
1019         rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1020         if (rc)
1021                 return rc;
1022
1023         while (zap_cursor_retrieve(zc, za) == 0) {
1024                 LASSERT(za->za_num_integers == 1);
1025                 LASSERT(za->za_integer_length == sizeof(uint64_t));
1026
1027                 rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
1028                                  sizeof(uint64_t), 1, &xid);
1029                 if (rc) {
1030                         CERROR("%s: lookup xattr %s failed: rc = %d\n",
1031                                osd->od_svname, za->za_name, rc);
1032                 } else {
1033                         rc = -dmu_object_free(osd->od_os, xid, tx);
1034                         if (rc)
1035                                 CERROR("%s: free xattr %s failed: rc = %d\n",
1036                                        osd->od_svname, za->za_name, rc);
1037                 }
1038                 zap_cursor_advance(zc);
1039         }
1040         osd_zap_cursor_fini(zc);
1041
1042         rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
1043         if (rc)
1044                 CERROR("%s: free xattr %llu failed: rc = %d\n",
1045                        osd->od_svname, obj->oo_xattr, rc);
1046
1047         return rc;
1048 }
1049
1050 static int
1051 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
1052                   const struct lu_buf *lb)
1053 {
1054         nvpair_t *nvp = NULL;
1055         int       len, counted = 0;
1056         int       rc = 0;
1057
1058         rc = __osd_xattr_cache(obj);
1059         if (rc)
1060                 return rc;
1061
1062         while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
1063                 const char *name = nvpair_name(nvp);
1064
1065                 if (!osd_obj2dev(obj)->od_posix_acl &&
1066                     (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1067                      strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
1068                         continue;
1069
1070                 len = strlen(name);
1071                 if (lb->lb_buf != NULL) {
1072                         if (counted + len + 1 > lb->lb_len)
1073                                 return -ERANGE;
1074
1075                         memcpy(lb->lb_buf + counted, name, len + 1);
1076                 }
1077                 counted += len + 1;
1078         }
1079         return counted;
1080 }
1081
1082 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
1083                    const struct lu_buf *lb)
1084 {
1085         struct osd_object      *obj = osd_dt_obj(dt);
1086         struct osd_device      *osd = osd_obj2dev(obj);
1087         zap_attribute_t        *za = &osd_oti_get(env)->oti_za;
1088         zap_cursor_t           *zc;
1089         int                    rc, counted;
1090         ENTRY;
1091
1092         LASSERT(obj->oo_dn != NULL);
1093         LASSERT(osd_invariant(obj));
1094         LASSERT(dt_object_exists(dt));
1095
1096         down_read(&obj->oo_guard);
1097
1098         rc = osd_sa_xattr_list(env, obj, lb);
1099         if (rc < 0)
1100                 GOTO(out, rc);
1101
1102         counted = rc;
1103
1104         /* continue with dnode xattr if any */
1105         if (obj->oo_xattr == ZFS_NO_OBJECT)
1106                 GOTO(out, rc = counted);
1107
1108         rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1109         if (rc)
1110                 GOTO(out, rc);
1111
1112         while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
1113                 if (!osd_obj2dev(obj)->od_posix_acl &&
1114                     (strcmp(za->za_name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1115                      strcmp(za->za_name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) {
1116                         zap_cursor_advance(zc);
1117                         continue;
1118                 }
1119
1120                 rc = strlen(za->za_name);
1121                 if (lb->lb_buf != NULL) {
1122                         if (counted + rc + 1 > lb->lb_len)
1123                                 GOTO(out_fini, rc = -ERANGE);
1124
1125                         memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
1126                 }
1127                 counted += rc + 1;
1128
1129                 zap_cursor_advance(zc);
1130         }
1131         if (rc == -ENOENT) /* no more kes in the index */
1132                 rc = 0;
1133         else if (unlikely(rc < 0))
1134                 GOTO(out_fini, rc);
1135         rc = counted;
1136
1137 out_fini:
1138         osd_zap_cursor_fini(zc);
1139 out:
1140         up_read(&obj->oo_guard);
1141         RETURN(rc);
1142
1143 }