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