Whamcloud - gitweb
LU-1842 quota: remove quota code
[fs/lustre-release.git] / lustre / mdd / mdd_lov.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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdd/mdd_lov.c
37  *
38  * Lustre Metadata Server (mds) handling of striped file data
39  *
40  * Author: Peter Braam <braam@clusterfs.com>
41  * Author: wangdi <wangdi@clusterfs.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_MDS
45
46 #include <linux/module.h>
47 #include <obd.h>
48 #include <obd_class.h>
49 #include <lustre_ver.h>
50 #include <obd_support.h>
51 #include <obd_lov.h>
52 #include <lprocfs_status.h>
53 #include <lustre_mds.h>
54 #include <lustre_fid.h>
55 #include <lustre/lustre_idl.h>
56
57 #include "mdd_internal.h"
58
59 static int mdd_notify(struct obd_device *host, struct obd_device *watched,
60                       enum obd_notify_event ev, void *owner, void *data)
61 {
62         struct mdd_device *mdd = owner;
63         int rc = 0;
64         ENTRY;
65
66         LASSERT(owner != NULL);
67         switch (ev)
68         {
69                 case OBD_NOTIFY_ACTIVE:
70                 case OBD_NOTIFY_SYNC:
71                 case OBD_NOTIFY_SYNC_NONBLOCK:
72                         rc = md_do_upcall(NULL, &mdd->mdd_md_dev,
73                                           MD_LOV_SYNC, data);
74                         break;
75                 case OBD_NOTIFY_CONFIG:
76                         rc = md_do_upcall(NULL, &mdd->mdd_md_dev,
77                                           MD_LOV_CONFIG, data);
78                         break;
79                 default:
80                         CDEBUG(D_INFO, "Unhandled notification %#x\n", ev);
81         }
82
83         RETURN(rc);
84 }
85
86 /* The obd is created for handling data stack for mdd */
87 int mdd_init_obd(const struct lu_env *env, struct mdd_device *mdd,
88                  struct lustre_cfg *cfg)
89 {
90         char                   *dev = lustre_cfg_string(cfg, 0);
91         int                     rc, name_size, uuid_size;
92         char                   *name, *uuid;
93         __u32                   mds_id;
94         struct lustre_cfg_bufs *bufs;
95         struct lustre_cfg      *lcfg;
96         struct obd_device      *obd;
97         ENTRY;
98
99         mds_id = lu_site2md(mdd2lu_dev(mdd)->ld_site)->ms_node_id;
100         name_size = strlen(MDD_OBD_NAME) + 35;
101         uuid_size = strlen(MDD_OBD_UUID) + 35;
102
103         OBD_ALLOC(name, name_size);
104         OBD_ALLOC(uuid, uuid_size);
105         if (name == NULL || uuid == NULL)
106                 GOTO(cleanup_mem, rc = -ENOMEM);
107
108         OBD_ALLOC_PTR(bufs);
109         if (!bufs)
110                 GOTO(cleanup_mem, rc = -ENOMEM);
111
112         snprintf(name, strlen(MDD_OBD_NAME) + 35, "%s-%s",
113                  MDD_OBD_NAME, dev);
114
115         snprintf(uuid, strlen(MDD_OBD_UUID) + 35, "%s-%s",
116                  MDD_OBD_UUID, dev);
117
118         lustre_cfg_bufs_reset(bufs, name);
119         lustre_cfg_bufs_set_string(bufs, 1, MDD_OBD_TYPE);
120         lustre_cfg_bufs_set_string(bufs, 2, uuid);
121         lustre_cfg_bufs_set_string(bufs, 3, (char*)dev/* MDD_OBD_PROFILE */);
122         lustre_cfg_bufs_set_string(bufs, 4, (char*)dev);
123
124         lcfg = lustre_cfg_new(LCFG_ATTACH, bufs);
125         OBD_FREE_PTR(bufs);
126         if (!lcfg)
127                 GOTO(cleanup_mem, rc = -ENOMEM);
128
129         rc = class_attach(lcfg);
130         if (rc)
131                 GOTO(lcfg_cleanup, rc);
132
133         obd = class_name2obd(name);
134         if (!obd) {
135                 CERROR("Can not find obd %s\n", MDD_OBD_NAME);
136                 LBUG();
137         }
138
139         cfs_spin_lock(&obd->obd_dev_lock);
140         obd->obd_recovering = 1;
141         cfs_spin_unlock(&obd->obd_dev_lock);
142         obd->u.mds.mds_id = mds_id;
143         obd->u.obt.obt_osd_properties.osd_max_ea_size =
144                                                mdd->mdd_dt_conf.ddp_max_ea_size;
145
146         rc = class_setup(obd, lcfg);
147         if (rc)
148                 GOTO(class_detach, rc);
149
150         /*
151          * Add here for obd notify mechanism, when adding a new ost, the mds
152          * will notify this mdd.
153          */
154         obd->obd_upcall.onu_upcall = mdd_notify;
155         obd->obd_upcall.onu_owner = mdd;
156         mdd->mdd_obd_dev = obd;
157
158         EXIT;
159 class_detach:
160         if (rc)
161                 class_detach(obd, lcfg);
162 lcfg_cleanup:
163         lustre_cfg_free(lcfg);
164 cleanup_mem:
165         if (name)
166                 OBD_FREE(name, name_size);
167         if (uuid)
168                 OBD_FREE(uuid, uuid_size);
169         return rc;
170 }
171
172 int mdd_fini_obd(const struct lu_env *env, struct mdd_device *mdd,
173                  struct lustre_cfg *lcfg)
174 {
175         struct obd_device      *obd;
176         int rc;
177         ENTRY;
178
179         obd = mdd2obd_dev(mdd);
180         LASSERT(obd);
181
182         rc = class_cleanup(obd, lcfg);
183         if (rc)
184                 GOTO(lcfg_cleanup, rc);
185
186         obd->obd_upcall.onu_upcall = NULL;
187         obd->obd_upcall.onu_owner = NULL;
188         rc = class_detach(obd, lcfg);
189         if (rc)
190                 GOTO(lcfg_cleanup, rc);
191         mdd->mdd_obd_dev = NULL;
192
193         EXIT;
194 lcfg_cleanup:
195         return rc;
196 }
197
198 int mdd_get_md(const struct lu_env *env, struct mdd_object *obj,
199                void *md, int *md_size, const char *name)
200 {
201         int rc;
202         ENTRY;
203
204         rc = mdo_xattr_get(env, obj, mdd_buf_get(env, md, *md_size), name,
205                            mdd_object_capa(env, obj));
206         /*
207          * XXX: Handling of -ENODATA, the right way is to have ->do_md_get()
208          * exported by dt layer.
209          */
210         if (rc == 0 || rc == -ENODATA) {
211                 *md_size = 0;
212                 rc = 0;
213         } else if (rc < 0) {
214                 CDEBUG(D_OTHER, "Error %d reading eadata - %d\n",
215                        rc, *md_size);
216         } else {
217                 /* XXX: Convert lov EA but fixed after verification test. */
218                 *md_size = rc;
219         }
220
221         RETURN(rc);
222 }
223
224 int mdd_get_md_locked(const struct lu_env *env, struct mdd_object *obj,
225                       void *md, int *md_size, const char *name)
226 {
227         int rc = 0;
228         mdd_read_lock(env, obj, MOR_TGT_CHILD);
229         rc = mdd_get_md(env, obj, md, md_size, name);
230         mdd_read_unlock(env, obj);
231         return rc;
232 }
233
234 static int mdd_lov_set_stripe_md(const struct lu_env *env,
235                                  struct mdd_object *obj, struct lu_buf *buf,
236                                  struct thandle *handle)
237 {
238         struct mdd_device       *mdd = mdo2mdd(&obj->mod_obj);
239         struct obd_device       *obd = mdd2obd_dev(mdd);
240         struct obd_export       *lov_exp = obd->u.mds.mds_lov_exp;
241         struct lov_stripe_md    *lsm = NULL;
242         int rc;
243         ENTRY;
244
245         LASSERT(S_ISDIR(mdd_object_type(obj)) || S_ISREG(mdd_object_type(obj)));
246         rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, lov_exp, 0,
247                            &lsm, buf->lb_buf);
248         if (rc)
249                 RETURN(rc);
250         obd_free_memmd(lov_exp, &lsm);
251
252         rc = mdd_xattr_set_txn(env, obj, buf, XATTR_NAME_LOV, 0, handle);
253
254         CDEBUG(D_INFO, "set lov ea of "DFID" rc %d \n", PFID(mdo2fid(obj)), rc);
255         RETURN(rc);
256 }
257
258 /*
259  * Permission check is done before call it,
260  * no need check again.
261  */
262 static int mdd_lov_set_dir_md(const struct lu_env *env,
263                               struct mdd_object *obj, struct lu_buf *buf,
264                               struct thandle *handle)
265 {
266         struct lov_user_md *lum = NULL;
267         int rc = 0;
268         ENTRY;
269
270         LASSERT(S_ISDIR(mdd_object_type(obj)));
271         lum = (struct lov_user_md*)buf->lb_buf;
272
273         /* if { size, offset, count } = { 0, -1, 0 } and no pool
274          * (i.e. all default values specified) then delete default
275          * striping from dir. */
276         if (LOVEA_DELETE_VALUES(lum->lmm_stripe_size, lum->lmm_stripe_count,
277                                 lum->lmm_stripe_offset) &&
278             lum->lmm_magic != LOV_USER_MAGIC_V3) {
279                 rc = mdd_xattr_set_txn(env, obj, &LU_BUF_NULL,
280                                        XATTR_NAME_LOV, 0, handle);
281                 if (rc == -ENODATA)
282                         rc = 0;
283                 CDEBUG(D_INFO, "delete lov ea of "DFID" rc %d \n",
284                                 PFID(mdo2fid(obj)), rc);
285         } else {
286                 rc = mdd_lov_set_stripe_md(env, obj, buf, handle);
287         }
288         RETURN(rc);
289 }
290
291 int mdd_lsm_sanity_check(const struct lu_env *env,  struct mdd_object *obj)
292 {
293         struct lu_attr   *tmp_la = &mdd_env_info(env)->mti_la;
294         struct md_ucred  *uc     = md_ucred(env);
295         int rc;
296         ENTRY;
297
298         rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
299         if (rc)
300                 RETURN(rc);
301
302         if ((uc->mu_fsuid != tmp_la->la_uid) &&
303             !mdd_capable(uc, CFS_CAP_FOWNER))
304                 rc = mdd_permission_internal_locked(env, obj, tmp_la,
305                                                     MAY_WRITE, MOR_TGT_CHILD);
306
307         RETURN(rc);
308 }
309
310 int mdd_lov_set_md(const struct lu_env *env, struct mdd_object *pobj,
311                    struct mdd_object *child, struct lov_mds_md *lmmp,
312                    int lmm_size, struct thandle *handle, int set_stripe)
313 {
314         struct lu_buf *buf;
315         cfs_umode_t mode;
316         int rc = 0;
317         ENTRY;
318
319         buf = mdd_buf_get(env, lmmp, lmm_size);
320         mode = mdd_object_type(child);
321         if (S_ISREG(mode) && lmm_size > 0) {
322                 if (set_stripe) {
323                         rc = mdd_lov_set_stripe_md(env, child, buf, handle);
324                 } else {
325                         rc = mdd_xattr_set_txn(env, child, buf,
326                                                XATTR_NAME_LOV, 0, handle);
327                 }
328         } else if (S_ISDIR(mode)) {
329                 if (lmmp == NULL && lmm_size == 0) {
330                         struct mdd_device *mdd = mdd_obj2mdd_dev(child);
331                         struct lov_mds_md *lmm = mdd_max_lmm_get(env, mdd);
332                         int size = sizeof(struct lov_mds_md_v3);
333
334                         /* Get parent dir stripe and set */
335                         if (pobj != NULL)
336                                 rc = mdd_get_md_locked(env, pobj, lmm, &size,
337                                                        XATTR_NAME_LOV);
338                         if (rc > 0) {
339                                 buf = mdd_buf_get(env, lmm, size);
340                                 rc = mdd_xattr_set_txn(env, child, buf,
341                                                        XATTR_NAME_LOV, 0,
342                                                        handle);
343                                 if (rc)
344                                         CERROR("error on copy stripe info: rc "
345                                                 "= %d\n", rc);
346                         }
347                 } else {
348                         LASSERT(lmmp != NULL && lmm_size > 0);
349                         rc = mdd_lov_set_dir_md(env, child, buf, handle);
350                 }
351         }
352         CDEBUG(D_INFO, "Set lov md %p size %d for fid "DFID" rc %d\n",
353                         lmmp, lmm_size, PFID(mdo2fid(child)), rc);
354         RETURN(rc);
355 }
356
357 int mdd_lov_objid_prepare(struct mdd_device *mdd, struct lov_mds_md *lmm)
358 {
359         /* copy mds_lov code is using wrong layer */
360         return mds_lov_prepare_objids(mdd->mdd_obd_dev, lmm);
361 }
362
363 int mdd_declare_lov_objid_update(const struct lu_env *env,
364                                  struct mdd_device *mdd,
365                                  struct thandle *handle)
366 {
367         struct obd_device *obd = mdd2obd_dev(mdd);
368         int size;
369
370         /* in prepare we create local files */
371         if (unlikely(mdd->mdd_capa == NULL))
372                 return 0;
373
374         /* XXX: this is a temporary solution to declare llog changes
375          *      will be fixed in 2.3 with new llog implementation */
376
377         size = obd->u.mds.mds_lov_desc.ld_tgt_count * sizeof(obd_id);
378         return dt_declare_record_write(env, mdd->mdd_capa, size, 0, handle);
379 }
380
381 void mdd_lov_objid_update(struct mdd_device *mdd, struct lov_mds_md *lmm)
382 {
383         /* copy mds_lov code is using wrong layer */
384         mds_lov_update_objids(mdd->mdd_obd_dev, lmm);
385 }
386
387 void mdd_lov_create_finish(const struct lu_env *env, struct mdd_device *mdd,
388                            struct lov_mds_md *lmm, int lmm_size,
389                            const struct md_op_spec *spec)
390 {
391         if (lmm && !spec->no_create)
392                 OBD_FREE_LARGE(lmm, lmm_size);
393 }
394
395 int mdd_lov_create(const struct lu_env *env, struct mdd_device *mdd,
396                    struct mdd_object *parent, struct mdd_object *child,
397                    struct lov_mds_md **lmm, int *lmm_size,
398                    const struct md_op_spec *spec, struct md_attr *ma)
399 {
400         struct obd_device     *obd = mdd2obd_dev(mdd);
401         struct obd_export     *lov_exp = obd->u.mds.mds_lov_exp;
402         struct lu_site        *site = mdd2lu_dev(mdd)->ld_site;
403         struct obdo           *oa;
404         struct lov_stripe_md  *lsm = NULL;
405         const void            *eadata = spec->u.sp_ea.eadata;
406         __u64                  create_flags = spec->sp_cr_flags;
407         struct obd_trans_info *oti = &mdd_env_info(env)->mti_oti;
408         struct lu_attr        *la = &ma->ma_attr;
409         int                    rc = 0;
410         ENTRY;
411
412         if (!md_should_create(create_flags)) {
413                 *lmm_size = 0;
414                 RETURN(0);
415         }
416         oti_init(oti, NULL);
417
418         /* replay case, has objects already, only get lov from eadata */
419         if (spec->no_create != 0) {
420                 *lmm = (struct lov_mds_md *)spec->u.sp_ea.eadata;
421                 *lmm_size = spec->u.sp_ea.eadatalen;
422                 if (*lmm_size == lov_mds_md_size((*lmm)->lmm_stripe_count,
423                                                  (*lmm)->lmm_magic)) {
424                         RETURN(0);
425                 } else {
426                         CERROR("incorrect lsm received during recovery\n");
427                         RETURN(-EPROTO);
428                 }
429         }
430
431         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
432                 GOTO(out_ids, rc = -ENOMEM);
433
434         LASSERT(lov_exp != NULL);
435         oa = &mdd_env_info(env)->mti_oa;
436
437         oa->o_uid = 0; /* must have 0 uid / gid on OST */
438         oa->o_gid = 0;
439         oa->o_seq = mdt_to_obd_objseq(lu_site2md(site)->ms_node_id);
440         oa->o_mode = S_IFREG | 0600;
441         oa->o_id = fid_ver_oid(mdd_object_fid(child));
442         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLFLAGS |
443                 OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLGROUP;
444         oa->o_size = 0;
445
446         if (!(create_flags & MDS_OPEN_HAS_OBJS)) {
447                 if (create_flags & MDS_OPEN_HAS_EA) {
448                         LASSERT(eadata != NULL);
449                         rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, lov_exp,
450                                            0, &lsm, (void*)eadata);
451                         if (rc)
452                                 GOTO(out_oti, rc);
453                 } else {
454                         /* get lov ea from parent and set to lov */
455                         struct lov_mds_md *_lmm;
456                         int _lmm_size = mdd_lov_mdsize(env, mdd);
457
458                         LASSERT(parent != NULL);
459
460                         /*
461                          * can not create child's lov_mds_md by access it
462                          * thru .lustre path
463                          */
464                         if (mdd_object_obf(parent))
465                                 GOTO(out_oti, rc = -EBADFD);
466
467                         _lmm = mdd_max_lmm_get(env, mdd);
468                         if (_lmm == NULL)
469                                 GOTO(out_oti, rc = -ENOMEM);
470
471                         rc = mdd_get_md_locked(env, parent, _lmm,
472                                                &_lmm_size,
473                                                XATTR_NAME_LOV);
474                         if (rc > 0) {
475                                 _lmm_size = mdd_lov_mdsize(env, mdd);
476                                 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
477                                                    lov_exp, _lmm_size,
478                                                    &lsm, _lmm);
479                         }
480                         if (rc)
481                                 GOTO(out_oti, rc);
482                 }
483
484                 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_OPEN_WAIT_CREATE, 10);
485                 rc = obd_create(env, lov_exp, oa, &lsm, oti);
486                 if (rc) {
487                         if (rc > 0) {
488                                 CERROR("Create error for "DFID": %d\n",
489                                        PFID(mdo2fid(child)), rc);
490                                 rc = -EIO;
491                         }
492                         GOTO(out_oti, rc);
493                 }
494
495                 if (ma->ma_valid & MA_LAY_GEN)
496                         /* If we already have a lsm, the file is not new and we
497                          * are about to change the layout, so we have to bump
498                          * the generation. It is worth noting that old versions
499                          * will be confused by a non-zero gen, that's why
500                          * OBD_INCOMPAT_LMM_VER has been introduced */
501                         lsm->lsm_layout_gen = ma->ma_layout_gen + 1;
502                 else
503                         /* Start with a null generation for backward
504                          * compatiblity with old versions */
505                         lsm->lsm_layout_gen = 0;
506
507                 LASSERT_SEQ_IS_MDT(lsm->lsm_object_seq);
508         } else {
509                 LASSERT(eadata != NULL);
510                 rc = obd_iocontrol(OBD_IOC_LOV_SETEA, lov_exp, 0, &lsm,
511                                    (void*)eadata);
512                 if (rc)
513                         GOTO(out_oti, rc);
514
515                 if (ma->ma_valid & MA_LAY_GEN)
516                         lsm->lsm_layout_gen = ma->ma_layout_gen;
517                 else
518                         lsm->lsm_layout_gen = 0;
519         }
520
521         lsm->lsm_object_id = fid_ver_oid(mdd_object_fid(child));
522         lsm->lsm_object_seq = fid_seq(mdd_object_fid(child));
523         /*
524          * Sometimes, we may truncate some object(without lsm) then open it
525          * (with write flags), so creating lsm above.  The Nonzero(truncated)
526          * size should tell ost, since size attr is in charge by OST.
527          */
528         if (la->la_size && la->la_valid & LA_SIZE) {
529                 struct obd_info *oinfo = &mdd_env_info(env)->mti_oi;
530
531                 memset(oinfo, 0, sizeof(*oinfo));
532
533                 /* When setting attr to ost, FLBKSZ is not needed. */
534                 oa->o_valid &= ~OBD_MD_FLBLKSZ;
535                 obdo_from_la(oa, la, LA_TYPE | LA_ATIME | LA_MTIME |
536                                      LA_CTIME | LA_SIZE);
537                 /*
538                  * XXX: Pack lustre id to OST, in OST, it will be packed by
539                  * filter_fid, but can not see what is the usages. So just pack
540                  * o_seq o_ver here, maybe fix it after this cycle.
541                  */
542                 obdo_set_parent_fid(oa, mdd_object_fid(child));
543                 oinfo->oi_oa = oa;
544                 oinfo->oi_md = lsm;
545                 oinfo->oi_capa = NULL;
546                 oinfo->oi_policy.l_extent.start = la->la_size;
547                 oinfo->oi_policy.l_extent.end = OBD_OBJECT_EOF;
548
549                 rc = obd_punch_rqset(lov_exp, oinfo, oti);
550                 if (rc) {
551                         CERROR("Error setting attrs for "DFID": rc %d\n",
552                                PFID(mdo2fid(child)), rc);
553                         if (rc > 0) {
554                                 CERROR("obd_setattr for "DFID" rc %d\n",
555                                         PFID(mdo2fid(child)), rc);
556                                 rc = -EIO;
557                         }
558                         GOTO(out_oti, rc);
559                 }
560         }
561         /* blksize should be changed after create data object */
562         la->la_valid |= LA_BLKSIZE;
563         la->la_blksize = oa->o_blksize;
564         *lmm = NULL;
565         rc = obd_packmd(lov_exp, lmm, lsm);
566         if (rc < 0) {
567                 CERROR("Cannot pack lsm, err = %d\n", rc);
568                 GOTO(out_oti, rc);
569         }
570         if (mdd_lov_objid_prepare(mdd, *lmm) != 0) {
571                 CERROR("Not have memory for update objid\n");
572                 OBD_FREE(*lmm, rc);
573                 *lmm = NULL;
574                 GOTO(out_oti, rc = -ENOMEM);
575         }
576         *lmm_size = rc;
577         rc = 0;
578         EXIT;
579 out_oti:
580         oti_free_cookies(oti);
581 out_ids:
582         if (lsm)
583                 obd_free_memmd(lov_exp, &lsm);
584
585         return rc;
586 }
587
588 /*
589  * used when destroying orphans and from mds_reint_unlink() when MDS wants to
590  * destroy objects on OSS.
591  */
592 int mdd_lovobj_unlink(const struct lu_env *env, struct mdd_device *mdd,
593                       struct mdd_object *obj, struct lu_attr *la,
594                       struct md_attr *ma, int log_unlink)
595 {
596         struct obd_device     *obd = mdd2obd_dev(mdd);
597         struct obd_export     *lov_exp = obd->u.mds.mds_lov_exp;
598         struct lov_stripe_md  *lsm = NULL;
599         struct obd_trans_info *oti = &mdd_env_info(env)->mti_oti;
600         struct obdo           *oa = &mdd_env_info(env)->mti_oa;
601         struct lu_site        *site = mdd2lu_dev(mdd)->ld_site;
602         struct lov_mds_md     *lmm = ma->ma_lmm;
603         int                    lmm_size = ma->ma_lmm_size;
604         struct llog_cookie    *logcookies = ma->ma_cookie;
605         int                    rc;
606         ENTRY;
607
608         if (lmm_size == 0)
609                 RETURN(0);
610
611         rc = obd_unpackmd(lov_exp, &lsm, lmm, lmm_size);
612         if (rc < 0) {
613                 CERROR("Error unpack md %p\n", lmm);
614                 RETURN(rc);
615         } else {
616                 LASSERT(rc >= sizeof(*lsm));
617                 rc = 0;
618         }
619
620         oa->o_id = lsm->lsm_object_id;
621         oa->o_seq = mdt_to_obd_objseq(lu_site2md(site)->ms_node_id);
622         oa->o_mode = la->la_mode & S_IFMT;
623         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
624
625         oti_init(oti, NULL);
626         if (log_unlink && logcookies) {
627                 oa->o_valid |= OBD_MD_FLCOOKIE;
628                 oti->oti_logcookies = logcookies;
629         }
630
631         if (!(ma->ma_attr_flags & MDS_UNLINK_DESTROY))
632                 oa->o_flags = OBD_FL_DELORPHAN;
633
634         CDEBUG(D_INFO, "destroying OSS object "LPU64":"LPU64"\n", oa->o_seq,
635                oa->o_id);
636
637         rc = obd_destroy(env, lov_exp, oa, lsm, oti, NULL, NULL);
638
639         obd_free_memmd(lov_exp, &lsm);
640         RETURN(rc);
641 }
642
643 /*
644  * called with obj locked.
645  */
646 int mdd_lov_destroy(const struct lu_env *env, struct mdd_device *mdd,
647                     struct mdd_object *obj, struct lu_attr *la)
648 {
649         struct md_attr    *ma = &mdd_env_info(env)->mti_ma;
650         int                rc;
651         ENTRY;
652
653         LASSERT(mdd_write_locked(env, obj) != 0);
654
655         if (unlikely(!S_ISREG(mdd_object_type(obj))))
656                 RETURN(0);
657
658         if (unlikely(la->la_nlink != 0)) {
659                 CWARN("Attempt to destroy OSS object when nlink == %d\n",
660                       la->la_nlink);
661                 RETURN(0);
662         }
663
664         ma->ma_lmm_size = mdd_lov_mdsize(env, mdd);
665         ma->ma_lmm = mdd_max_lmm_get(env, mdd);
666         ma->ma_cookie_size = mdd_lov_cookiesize(env, mdd);
667         ma->ma_cookie = mdd_max_cookie_get(env, mdd);
668         if (ma->ma_lmm == NULL || ma->ma_cookie == NULL)
669                 RETURN(rc = -ENOMEM);
670
671         /* get lov ea */
672
673         rc = mdd_get_md(env, obj, ma->ma_lmm, &ma->ma_lmm_size,
674                         XATTR_NAME_LOV);
675
676         if (rc <= 0) {
677                 CWARN("Get lov ea failed for "DFID" rc = %d\n",
678                          PFID(mdo2fid(obj)), rc);
679                 if (rc == 0)
680                         rc = -ENOENT;
681                 RETURN(rc);
682         }
683
684         ma->ma_valid = MA_LOV;
685
686         rc = mdd_unlink_log(env, mdd, obj, ma);
687         if (rc) {
688                 CWARN("mds unlink log for "DFID" failed: %d\n",
689                        PFID(mdo2fid(obj)), rc);
690                 RETURN(rc);
691         }
692
693         if (ma->ma_valid & MA_COOKIE)
694                 rc = mdd_lovobj_unlink(env, mdd, obj, la, ma, 1);
695
696         RETURN(rc);
697 }
698
699 int mdd_declare_unlink_log(const struct lu_env *env, struct mdd_object *obj,
700                            struct md_attr *ma, struct thandle *handle)
701 {
702         struct mdd_device *mdd = mdo2mdd(&obj->mod_obj);
703         int rc, i;
704         __u16 stripe;
705
706         LASSERT(obj);
707         LASSERT(ma);
708
709         if (!S_ISREG(lu_object_attr(&obj->mod_obj.mo_lu)))
710                 return 0;
711
712         rc = mdd_lmm_get_locked(env, obj, ma);
713         if (rc || !(ma->ma_valid & MA_LOV))
714                 return rc;
715
716         LASSERT(ma->ma_lmm);
717         if (le32_to_cpu(ma->ma_lmm->lmm_magic) != LOV_MAGIC_V1 &&
718                         le32_to_cpu(ma->ma_lmm->lmm_magic) != LOV_MAGIC_V3) {
719                 CERROR("%s: invalid LOV_MAGIC %08x on object "DFID"\n",
720                                 mdd->mdd_obd_dev->obd_name,
721                                 le32_to_cpu(ma->ma_lmm->lmm_magic),
722                                 PFID(lu_object_fid(&obj->mod_obj.mo_lu)));
723                 return -EINVAL;
724         }
725
726         stripe = le16_to_cpu(ma->ma_lmm->lmm_stripe_count);
727         if (stripe == LOV_ALL_STRIPES);
728                 stripe = mdd2obd_dev(mdd)->u.mds.mds_lov_desc.ld_tgt_count;
729
730         for (i = 0; i < stripe; i++) {
731                 rc = mdd_declare_llog_record(env, mdd,
732                                              sizeof(struct llog_unlink_rec),
733                                              handle);
734                 if (rc)
735                         return rc;
736         }
737
738         return rc;
739 }
740
741 int mdd_unlink_log(const struct lu_env *env, struct mdd_device *mdd,
742                    struct mdd_object *mdd_cobj, struct md_attr *ma)
743 {
744         LASSERT(ma->ma_valid & MA_LOV);
745
746         if ((ma->ma_cookie_size > 0) &&
747             (mds_log_op_unlink(mdd2obd_dev(mdd), ma->ma_lmm, ma->ma_lmm_size,
748                                ma->ma_cookie, ma->ma_cookie_size) > 0)) {
749                 CDEBUG(D_HA, "DEBUG: unlink log is added for object "DFID"\n",
750                        PFID(mdd_object_fid(mdd_cobj)));
751                 ma->ma_valid |= MA_COOKIE;
752         }
753         return 0;
754 }
755
756 int mdd_log_op_setattr(struct obd_device *obd, __u32 uid, __u32 gid,
757                        struct lov_mds_md *lmm, int lmm_size,
758                        struct llog_cookie *logcookies, int cookies_size)
759 {
760         struct mds_obd *mds = &obd->u.mds;
761         struct lov_stripe_md *lsm = NULL;
762         struct llog_setattr64_rec *lsr;
763         struct llog_ctxt *ctxt;
764         int rc;
765         ENTRY;
766
767         if (IS_ERR(mds->mds_lov_obd))
768                 RETURN(PTR_ERR(mds->mds_lov_obd));
769
770         rc = obd_unpackmd(mds->mds_lov_exp, &lsm, lmm, lmm_size);
771         if (rc < 0)
772                 RETURN(rc);
773
774         OBD_ALLOC(lsr, sizeof(*lsr));
775         if (!lsr)
776                 GOTO(out, rc = -ENOMEM);
777
778         /* prepare setattr log record */
779         lsr->lsr_hdr.lrh_len = lsr->lsr_tail.lrt_len = sizeof(*lsr);
780         lsr->lsr_hdr.lrh_type = MDS_SETATTR64_REC;
781         lsr->lsr_uid = uid;
782         lsr->lsr_gid = gid;
783
784         /* write setattr log */
785         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
786         rc = llog_obd_add(NULL, ctxt, &lsr->lsr_hdr, lsm, logcookies,
787                           cookies_size / sizeof(struct llog_cookie));
788
789         llog_ctxt_put(ctxt);
790
791         OBD_FREE(lsr, sizeof(*lsr));
792  out:
793         obd_free_memmd(mds->mds_lov_exp, &lsm);
794         RETURN(rc);
795 }
796
797 int mdd_setattr_log(const struct lu_env *env, struct mdd_device *mdd,
798                     const struct md_attr *ma,
799                     struct lov_mds_md *lmm, int lmm_size,
800                     struct llog_cookie *logcookies, int cookies_size)
801 {
802         struct obd_device *obd = mdd2obd_dev(mdd);
803
804         /* journal chown/chgrp in llog, just like unlink */
805         if (lmm_size > 0) {
806                 CDEBUG(D_INFO, "setattr llog for uid/gid=%lu/%lu\n",
807                         (unsigned long)ma->ma_attr.la_uid,
808                         (unsigned long)ma->ma_attr.la_gid);
809                 return mdd_log_op_setattr(obd, ma->ma_attr.la_uid,
810                                           ma->ma_attr.la_gid, lmm,
811                                           lmm_size, logcookies,
812                                           cookies_size);
813         } else
814                 return 0;
815 }
816
817 static int mdd_osc_setattr_async(struct obd_device *obd, __u32 uid, __u32 gid,
818                           struct lov_mds_md *lmm, int lmm_size,
819                           struct llog_cookie *logcookies, const struct lu_fid *parent,
820                           struct obd_capa *oc)
821 {
822         struct mds_obd *mds = &obd->u.mds;
823         struct obd_trans_info oti = { 0 };
824         struct obd_info oinfo = { { { 0 } } };
825         int rc;
826         ENTRY;
827
828         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OST_SETATTR))
829                 RETURN(0);
830
831         /* first get memory EA */
832         OBDO_ALLOC(oinfo.oi_oa);
833         if (!oinfo.oi_oa)
834                 RETURN(-ENOMEM);
835
836         LASSERT(lmm);
837
838         rc = obd_unpackmd(mds->mds_lov_exp, &oinfo.oi_md, lmm, lmm_size);
839         if (rc < 0) {
840                 CERROR("Error unpack md %p for obj "DFID"\n", lmm,
841                         PFID(parent));
842                 GOTO(out, rc);
843         }
844
845         /* then fill oa */
846         oinfo.oi_oa->o_uid = uid;
847         oinfo.oi_oa->o_gid = gid;
848         oinfo.oi_oa->o_id = oinfo.oi_md->lsm_object_id;
849         oinfo.oi_oa->o_seq = oinfo.oi_md->lsm_object_seq;
850         oinfo.oi_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP |
851                                 OBD_MD_FLUID | OBD_MD_FLGID;
852         if (logcookies) {
853                 oinfo.oi_oa->o_valid |= OBD_MD_FLCOOKIE;
854                 oti.oti_logcookies = logcookies;
855         }
856
857         obdo_set_parent_fid(oinfo.oi_oa, parent);
858         oinfo.oi_capa = oc;
859
860         /* do async setattr from mds to ost not waiting for responses. */
861         rc = obd_setattr_async(mds->mds_lov_exp, &oinfo, &oti, NULL);
862         if (rc)
863                 CDEBUG(D_INODE, "mds to ost setattr objid 0x"LPX64
864                        " on ost error %d\n", oinfo.oi_md->lsm_object_id, rc);
865 out:
866         if (oinfo.oi_md)
867                 obd_free_memmd(mds->mds_lov_exp, &oinfo.oi_md);
868         OBDO_FREE(oinfo.oi_oa);
869         RETURN(rc);
870 }
871
872 int mdd_lov_setattr_async(const struct lu_env *env, struct mdd_object *obj,
873                           struct lov_mds_md *lmm, int lmm_size,
874                           struct llog_cookie *logcookies)
875 {
876         struct mdd_device   *mdd = mdo2mdd(&obj->mod_obj);
877         struct obd_device   *obd = mdd2obd_dev(mdd);
878         struct lu_attr      *tmp_la = &mdd_env_info(env)->mti_la;
879         const struct lu_fid *fid = mdd_object_fid(obj);
880         int rc = 0;
881         ENTRY;
882
883         mdd_read_lock(env, obj, MOR_TGT_CHILD);
884         rc = mdo_attr_get(env, obj, tmp_la, mdd_object_capa(env, obj));
885         mdd_read_unlock(env, obj);
886         if (rc)
887                 RETURN(rc);
888
889         rc = mdd_osc_setattr_async(obd, tmp_la->la_uid, tmp_la->la_gid, lmm,
890                                    lmm_size, logcookies, fid, NULL);
891         RETURN(rc);
892 }
893