Whamcloud - gitweb
LU-10190 osd-zfs: create agent object for remote object
[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, osd, obj,
710                                          lu_object_fid(&obj->oo_dt.do_lu),
711                                          &xa_data_dn, tx, la);
712                 if (rc)
713                         goto out;
714                 xa_data_obj = xa_data_dn->dn_object;
715
716                 rc = -sa_handle_get(osd->od_os, xa_data_obj, NULL,
717                                         SA_HDL_PRIVATE, &sa_hdl);
718                 if (rc)
719                         goto out;
720
721                 rc = -zap_add(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t),
722                                 1, &xa_data_obj, tx);
723                 if (rc)
724                         goto out_sa;
725         } else {
726                 /* There was an error looking up the xattr name */
727                 goto out;
728         }
729
730         /* Finally write the xattr value */
731         dmu_write(osd->od_os, xa_data_obj, 0, buf->lb_len, buf->lb_buf, tx);
732
733         size = buf->lb_len;
734         rc = -sa_update(sa_hdl, SA_ZPL_SIZE(osd), &size, 8, tx);
735
736 out_sa:
737         sa_handle_destroy(sa_hdl);
738 out:
739         if (xa_data_dn != NULL)
740                 osd_dnode_rele(xa_data_dn);
741         if (xa_zap_dn != NULL)
742                 osd_dnode_rele(xa_zap_dn);
743
744         return rc;
745 }
746
747 static int osd_xattr_split_pfid(const struct lu_env *env,
748                                 struct osd_object *obj, struct osd_thandle *oh)
749 {
750         struct osd_thread_info *info = osd_oti_get(env);
751         struct lustre_ost_attrs *loa =
752                 (struct lustre_ost_attrs *)&info->oti_buf;
753         struct lustre_mdt_attrs *lma = &loa->loa_lma;
754         struct lu_buf buf = {
755                 .lb_buf = loa,
756                 .lb_len = sizeof(info->oti_buf),
757         };
758         int size;
759         int rc;
760         ENTRY;
761
762         CLASSERT(sizeof(info->oti_buf) >= sizeof(*loa));
763         rc = osd_xattr_get_internal(env, obj, &buf, XATTR_NAME_LMA, &size);
764         if (rc)
765                 RETURN(rc);
766
767         lustre_loa_swab(loa, true);
768         LASSERT(lma->lma_compat & LMAC_STRIPE_INFO);
769
770         lma->lma_compat &= ~(LMAC_STRIPE_INFO | LMAC_COMP_INFO);
771         lustre_lma_swab(lma);
772         buf.lb_buf = lma;
773         buf.lb_len = sizeof(*lma);
774         rc = osd_xattr_set_internal(env, obj, &buf, XATTR_NAME_LMA,
775                                     LU_XATTR_REPLACE, oh);
776         if (!rc)
777                 obj->oo_pfid_in_lma = 0;
778
779         RETURN(rc);
780 }
781
782 int osd_xattr_set(const struct lu_env *env, struct dt_object *dt,
783                   const struct lu_buf *buf, const char *name, int fl,
784                   struct thandle *handle)
785 {
786         struct osd_object  *obj = osd_dt_obj(dt);
787         struct osd_thandle *oh;
788         int rc = 0;
789         ENTRY;
790
791         LASSERT(handle != NULL);
792         LASSERT(osd_invariant(obj));
793
794         if (!osd_obj2dev(obj)->od_posix_acl &&
795             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
796              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
797                 RETURN(-EOPNOTSUPP);
798
799         oh = container_of0(handle, struct osd_thandle, ot_super);
800
801         down_write(&obj->oo_guard);
802         CDEBUG(D_INODE, "Setting xattr %s with size %d\n",
803                 name, (int)buf->lb_len);
804         /* For the OST migrated from ldiskfs, the PFID EA may
805          * be stored in LMA because of ldiskfs inode size. */
806         if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 &&
807                      obj->oo_pfid_in_lma)) {
808                 rc = osd_xattr_split_pfid(env, obj, oh);
809                 if (!rc)
810                         fl = LU_XATTR_CREATE;
811         }
812
813         if (!rc)
814                 rc = osd_xattr_set_internal(env, obj, buf, name, fl, oh);
815         up_write(&obj->oo_guard);
816
817         RETURN(rc);
818 }
819
820 static void
821 __osd_xattr_declare_del(const struct lu_env *env, struct osd_object *obj,
822                         const char *name, struct osd_thandle *oh)
823 {
824         struct osd_device *osd = osd_obj2dev(obj);
825         dmu_tx_t          *tx = oh->ot_tx;
826         uint64_t           xa_data_obj;
827         int                rc;
828
829         /* update SA_ZPL_DXATTR if xattr was in SA */
830         dmu_tx_hold_sa(tx, obj->oo_sa_hdl, 0);
831
832         if (obj->oo_xattr == ZFS_NO_OBJECT)
833                 return;
834
835         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, 8, 1, &xa_data_obj);
836         if (rc == 0) {
837                 /*
838                  * Entry exists.
839                  * We'll delete the existing object and ZAP entry.
840                  */
841                 dmu_tx_hold_bonus(tx, xa_data_obj);
842                 dmu_tx_hold_free(tx, xa_data_obj, 0, DMU_OBJECT_END);
843                 dmu_tx_hold_zap(tx, obj->oo_xattr, FALSE, (char *) name);
844                 return;
845         } else if (rc == -ENOENT) {
846                 /*
847                  * Entry doesn't exist, nothing to be changed.
848                  */
849                 return;
850         }
851
852         /* An error happened */
853         tx->tx_err = -rc;
854 }
855
856 int osd_declare_xattr_del(const struct lu_env *env, struct dt_object *dt,
857                           const char *name, struct thandle *handle)
858 {
859         struct osd_object  *obj = osd_dt_obj(dt);
860         struct osd_thandle *oh;
861         ENTRY;
862
863         LASSERT(handle != NULL);
864         LASSERT(osd_invariant(obj));
865
866         oh = container_of0(handle, struct osd_thandle, ot_super);
867         LASSERT(oh->ot_tx != NULL);
868         LASSERT(obj->oo_dn != NULL);
869
870         down_read(&obj->oo_guard);
871         if (likely(dt_object_exists(&obj->oo_dt) && !obj->oo_destroyed))
872                 __osd_xattr_declare_del(env, obj, name, oh);
873         up_read(&obj->oo_guard);
874
875         RETURN(0);
876 }
877
878 static int __osd_sa_xattr_del(const struct lu_env *env, struct osd_object *obj,
879                               const char *name, struct osd_thandle *oh)
880 {
881         int rc;
882
883         rc = __osd_xattr_cache(obj);
884         if (rc)
885                 return rc;
886
887         rc = -nvlist_remove(obj->oo_sa_xattr, name, DATA_TYPE_BYTE_ARRAY);
888         if (rc == 0)
889                 rc = __osd_sa_xattr_schedule_update(env, obj, oh);
890         return rc;
891 }
892
893 static int __osd_xattr_del(const struct lu_env *env, struct osd_object *obj,
894                            const char *name, struct osd_thandle *oh)
895 {
896         struct osd_device *osd = osd_obj2dev(obj);
897         uint64_t           xa_data_obj;
898         int                rc;
899
900         if (unlikely(!dt_object_exists(&obj->oo_dt) || obj->oo_destroyed))
901                 return -ENOENT;
902
903         /* try remove xattr from SA at first */
904         rc = __osd_sa_xattr_del(env, obj, name, oh);
905         if (rc != -ENOENT)
906                 return rc;
907
908         if (obj->oo_xattr == ZFS_NO_OBJECT)
909                 return 0;
910
911         rc = -zap_lookup(osd->od_os, obj->oo_xattr, name, sizeof(uint64_t), 1,
912                         &xa_data_obj);
913         if (rc == -ENOENT) {
914                 rc = 0;
915         } else if (rc == 0) {
916                 /*
917                  * Entry exists.
918                  * We'll delete the existing object and ZAP entry.
919                  */
920                 rc = -dmu_object_free(osd->od_os, xa_data_obj, oh->ot_tx);
921                 if (rc)
922                         return rc;
923
924                 rc = -zap_remove(osd->od_os, obj->oo_xattr, name, oh->ot_tx);
925         }
926
927         return rc;
928 }
929
930 int osd_xattr_del(const struct lu_env *env, struct dt_object *dt,
931                   const char *name, struct thandle *handle)
932 {
933         struct osd_object  *obj = osd_dt_obj(dt);
934         struct osd_thandle *oh;
935         int                 rc;
936         ENTRY;
937
938         LASSERT(handle != NULL);
939         LASSERT(obj->oo_dn != NULL);
940         LASSERT(osd_invariant(obj));
941         LASSERT(dt_object_exists(dt));
942         oh = container_of0(handle, struct osd_thandle, ot_super);
943         LASSERT(oh->ot_tx != NULL);
944
945         if (!osd_obj2dev(obj)->od_posix_acl &&
946             (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
947              strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
948                 RETURN(-EOPNOTSUPP);
949
950         down_write(&obj->oo_guard);
951         /* For the OST migrated from ldiskfs, the PFID EA may
952          * be stored in LMA because of ldiskfs inode size. */
953         if (unlikely(strcmp(name, XATTR_NAME_FID) == 0 && obj->oo_pfid_in_lma))
954                 rc = osd_xattr_split_pfid(env, obj, oh);
955         else
956                 rc = __osd_xattr_del(env, obj, name, oh);
957         up_write(&obj->oo_guard);
958
959         RETURN(rc);
960 }
961
962 void osd_declare_xattrs_destroy(const struct lu_env *env,
963                                 struct osd_object *obj, struct osd_thandle *oh)
964 {
965         struct osd_device *osd = osd_obj2dev(obj);
966         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
967         uint64_t           oid = obj->oo_xattr, xid;
968         dmu_tx_t          *tx = oh->ot_tx;
969         zap_cursor_t      *zc;
970         int                rc;
971
972         if (oid == ZFS_NO_OBJECT)
973                 return; /* Nothing to do for SA xattrs */
974
975         /* Declare to free the ZAP holding xattrs */
976         dmu_tx_hold_free(tx, oid, 0, DMU_OBJECT_END);
977
978         rc = osd_zap_cursor_init(&zc, osd->od_os, oid, 0);
979         if (rc)
980                 goto out;
981
982         while (zap_cursor_retrieve(zc, za) == 0) {
983                 LASSERT(za->za_num_integers == 1);
984                 LASSERT(za->za_integer_length == sizeof(uint64_t));
985
986                 rc = -zap_lookup(osd->od_os, oid, za->za_name,
987                                  sizeof(uint64_t), 1, &xid);
988                 if (rc) {
989                         CERROR("%s: xattr %s lookup failed: rc = %d\n",
990                                osd->od_svname, za->za_name, rc);
991                         break;
992                 }
993                 dmu_tx_hold_free(tx, xid, 0, DMU_OBJECT_END);
994
995                 zap_cursor_advance(zc);
996         }
997
998         osd_zap_cursor_fini(zc);
999 out:
1000         if (rc && tx->tx_err == 0)
1001                 tx->tx_err = -rc;
1002 }
1003
1004 int osd_xattrs_destroy(const struct lu_env *env,
1005                        struct osd_object *obj, struct osd_thandle *oh)
1006 {
1007         struct osd_device *osd = osd_obj2dev(obj);
1008         dmu_tx_t          *tx = oh->ot_tx;
1009         zap_attribute_t   *za = &osd_oti_get(env)->oti_za;
1010         zap_cursor_t      *zc;
1011         uint64_t           xid;
1012         int                rc;
1013
1014         /* The transaction must have been assigned to a transaction group. */
1015         LASSERT(tx->tx_txg != 0);
1016
1017         if (obj->oo_xattr == ZFS_NO_OBJECT)
1018                 return 0; /* Nothing to do for SA xattrs */
1019
1020         /* Free the ZAP holding the xattrs */
1021         rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1022         if (rc)
1023                 return rc;
1024
1025         while (zap_cursor_retrieve(zc, za) == 0) {
1026                 LASSERT(za->za_num_integers == 1);
1027                 LASSERT(za->za_integer_length == sizeof(uint64_t));
1028
1029                 rc = -zap_lookup(osd->od_os, obj->oo_xattr, za->za_name,
1030                                  sizeof(uint64_t), 1, &xid);
1031                 if (rc) {
1032                         CERROR("%s: lookup xattr %s failed: rc = %d\n",
1033                                osd->od_svname, za->za_name, rc);
1034                 } else {
1035                         rc = -dmu_object_free(osd->od_os, xid, tx);
1036                         if (rc)
1037                                 CERROR("%s: free xattr %s failed: rc = %d\n",
1038                                        osd->od_svname, za->za_name, rc);
1039                 }
1040                 zap_cursor_advance(zc);
1041         }
1042         osd_zap_cursor_fini(zc);
1043
1044         rc = -dmu_object_free(osd->od_os, obj->oo_xattr, tx);
1045         if (rc)
1046                 CERROR("%s: free xattr %llu failed: rc = %d\n",
1047                        osd->od_svname, obj->oo_xattr, rc);
1048
1049         return rc;
1050 }
1051
1052 static int
1053 osd_sa_xattr_list(const struct lu_env *env, struct osd_object *obj,
1054                   const struct lu_buf *lb)
1055 {
1056         nvpair_t *nvp = NULL;
1057         int       len, counted = 0;
1058         int       rc = 0;
1059
1060         rc = __osd_xattr_cache(obj);
1061         if (rc)
1062                 return rc;
1063
1064         while ((nvp = nvlist_next_nvpair(obj->oo_sa_xattr, nvp)) != NULL) {
1065                 const char *name = nvpair_name(nvp);
1066
1067                 if (!osd_obj2dev(obj)->od_posix_acl &&
1068                     (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1069                      strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
1070                         continue;
1071
1072                 len = strlen(name);
1073                 if (lb->lb_buf != NULL) {
1074                         if (counted + len + 1 > lb->lb_len)
1075                                 return -ERANGE;
1076
1077                         memcpy(lb->lb_buf + counted, name, len + 1);
1078                 }
1079                 counted += len + 1;
1080         }
1081         return counted;
1082 }
1083
1084 int osd_xattr_list(const struct lu_env *env, struct dt_object *dt,
1085                    const struct lu_buf *lb)
1086 {
1087         struct osd_object      *obj = osd_dt_obj(dt);
1088         struct osd_device      *osd = osd_obj2dev(obj);
1089         zap_attribute_t        *za = &osd_oti_get(env)->oti_za;
1090         zap_cursor_t           *zc;
1091         int                    rc, counted;
1092         ENTRY;
1093
1094         LASSERT(obj->oo_dn != NULL);
1095         LASSERT(osd_invariant(obj));
1096         LASSERT(dt_object_exists(dt));
1097
1098         down_read(&obj->oo_guard);
1099
1100         rc = osd_sa_xattr_list(env, obj, lb);
1101         if (rc < 0)
1102                 GOTO(out, rc);
1103
1104         counted = rc;
1105
1106         /* continue with dnode xattr if any */
1107         if (obj->oo_xattr == ZFS_NO_OBJECT)
1108                 GOTO(out, rc = counted);
1109
1110         rc = osd_zap_cursor_init(&zc, osd->od_os, obj->oo_xattr, 0);
1111         if (rc)
1112                 GOTO(out, rc);
1113
1114         while ((rc = -zap_cursor_retrieve(zc, za)) == 0) {
1115                 if (!osd_obj2dev(obj)->od_posix_acl &&
1116                     (strcmp(za->za_name, XATTR_NAME_POSIX_ACL_ACCESS) == 0 ||
1117                      strcmp(za->za_name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) {
1118                         zap_cursor_advance(zc);
1119                         continue;
1120                 }
1121
1122                 rc = strlen(za->za_name);
1123                 if (lb->lb_buf != NULL) {
1124                         if (counted + rc + 1 > lb->lb_len)
1125                                 GOTO(out_fini, rc = -ERANGE);
1126
1127                         memcpy(lb->lb_buf + counted, za->za_name, rc + 1);
1128                 }
1129                 counted += rc + 1;
1130
1131                 zap_cursor_advance(zc);
1132         }
1133         if (rc == -ENOENT) /* no more kes in the index */
1134                 rc = 0;
1135         else if (unlikely(rc < 0))
1136                 GOTO(out_fini, rc);
1137         rc = counted;
1138
1139 out_fini:
1140         osd_zap_cursor_fini(zc);
1141 out:
1142         up_read(&obj->oo_guard);
1143         RETURN(rc);
1144
1145 }