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