Whamcloud - gitweb
b=16098
[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 = mdd2lu_dev(mdd)->ld_site->ls_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.
149          */
150         obd->obd_upcall.onu_upcall = mdd_notify;
151         obd->obd_upcall.onu_owner = mdd;
152         mdd->mdd_obd_dev = obd;
153
154         EXIT;
155 class_detach:
156         if (rc)
157                 class_detach(obd, lcfg);
158 lcfg_cleanup:
159         lustre_cfg_free(lcfg);
160 cleanup_mem:
161         if (name)
162                 OBD_FREE(name, name_size);
163         if (uuid)
164                 OBD_FREE(uuid, uuid_size);
165         return rc;
166 }
167
168 int mdd_fini_obd(const struct lu_env *env, struct mdd_device *mdd,
169                  struct lustre_cfg *lcfg)
170 {
171         struct obd_device      *obd;
172         int rc;
173         ENTRY;
174
175         obd = mdd2obd_dev(mdd);
176         LASSERT(obd);
177
178         rc = class_cleanup(obd, lcfg);
179         if (rc)
180                 GOTO(lcfg_cleanup, rc);
181
182         obd->obd_upcall.onu_upcall = NULL;
183         obd->obd_upcall.onu_owner = NULL;
184         rc = class_detach(obd, lcfg);
185         if (rc)
186                 GOTO(lcfg_cleanup, rc);
187         mdd->mdd_obd_dev = NULL;
188         
189         EXIT;
190 lcfg_cleanup:
191         return rc;
192 }
193
194 int mdd_get_md(const struct lu_env *env, struct mdd_object *obj,
195                void *md, int *md_size, const char *name)
196 {
197         int rc;
198         ENTRY;
199
200         rc = mdo_xattr_get(env, obj, mdd_buf_get(env, md, *md_size), name,
201                            mdd_object_capa(env, obj));
202         /*
203          * XXX: Handling of -ENODATA, the right way is to have ->do_md_get()
204          * exported by dt layer.
205          */
206         if (rc == 0 || rc == -ENODATA) {
207                 *md_size = 0;
208                 rc = 0;
209         } else if (rc < 0) {
210                 CERROR("Error %d reading eadata \n", rc);
211         } else {
212                 /* XXX: Convert lov EA but fixed after verification test. */
213                 *md_size = rc;
214         }
215
216         RETURN(rc);
217 }
218
219 int mdd_get_md_locked(const struct lu_env *env, struct mdd_object *obj,
220                       void *md, int *md_size, const char *name)
221 {
222         int rc = 0;
223         mdd_read_lock(env, obj);
224         rc = mdd_get_md(env, obj, md, md_size, name);
225         mdd_read_unlock(env, obj);
226         return rc;
227 }
228
229 static int mdd_lov_set_stripe_md(const struct lu_env *env,
230                                  struct mdd_object *obj, struct lu_buf *buf,
231                                  struct thandle *handle)
232 {
233         struct mdd_device       *mdd = mdo2mdd(&obj->mod_obj);
234         struct obd_device       *obd = mdd2obd_dev(mdd);
235         struct obd_export       *lov_exp = obd->u.mds.mds_osc_exp;
236         struct lov_stripe_md    *lsm = NULL;
237         int rc;
238         ENTRY;
239
240         LASSERT(S_ISDIR(mdd_object_type(obj)) || S_ISREG(mdd_object_type(obj)));
241         rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, lov_exp, 0,
242                            &lsm, buf->lb_buf);
243         if (rc)
244                 RETURN(rc);
245         obd_free_memmd(lov_exp, &lsm);
246
247         rc = mdd_xattr_set_txn(env, obj, buf, MDS_LOV_MD_NAME, 0, handle);
248
249         CDEBUG(D_INFO, "set lov ea of "DFID" rc %d \n", PFID(mdo2fid(obj)), rc);
250         RETURN(rc);
251 }
252
253 /*
254  * Permission check is done before call it,
255  * no need check again.
256  */
257 static int mdd_lov_set_dir_md(const struct lu_env *env,
258                               struct mdd_object *obj, struct lu_buf *buf,
259                               struct thandle *handle)
260 {
261         struct lov_user_md *lum = NULL;
262         int rc = 0;
263         ENTRY;
264
265         LASSERT(S_ISDIR(mdd_object_type(obj)));
266         lum = (struct lov_user_md*)buf->lb_buf;
267
268         /* if { size, offset, count } = { 0, -1, 0 } (i.e. all default
269          * values specified) then delete default striping from dir. */
270         if ((lum->lmm_stripe_size == 0 && lum->lmm_stripe_count == 0 &&
271              lum->lmm_stripe_offset == (typeof(lum->lmm_stripe_offset))(-1)) ||
272              /* lmm_stripe_size == -1 is deprecated in 1.4.6 */
273              lum->lmm_stripe_size == (typeof(lum->lmm_stripe_size))(-1)){
274                 rc = mdd_xattr_set_txn(env, obj, &LU_BUF_NULL,
275                                        MDS_LOV_MD_NAME, 0, handle);
276                 if (rc == -ENODATA)
277                         rc = 0;
278                 CDEBUG(D_INFO, "delete lov ea of "DFID" rc %d \n",
279                                 PFID(mdo2fid(obj)), rc);
280         } else {
281                 rc = mdd_lov_set_stripe_md(env, obj, buf, handle);
282         }
283         RETURN(rc);
284 }
285
286 int mdd_lsm_sanity_check(const struct lu_env *env,  struct mdd_object *obj)
287 {
288         struct lu_attr   *tmp_la = &mdd_env_info(env)->mti_la;
289         struct md_ucred  *uc     = md_ucred(env);
290         int rc;
291         ENTRY;
292
293         rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
294         if (rc)
295                 RETURN(rc);
296
297         if ((uc->mu_fsuid != tmp_la->la_uid) && !mdd_capable(uc, CAP_FOWNER))
298                 rc = mdd_permission_internal_locked(env, obj, tmp_la,
299                                                     MAY_WRITE);
300
301         RETURN(rc);
302 }
303
304 int mdd_lov_set_md(const struct lu_env *env, struct mdd_object *pobj,
305                    struct mdd_object *child, struct lov_mds_md *lmmp,
306                    int lmm_size, struct thandle *handle, int set_stripe)
307 {
308         struct lu_buf *buf;
309         umode_t mode;
310         int rc = 0;
311         ENTRY;
312
313         buf = mdd_buf_get(env, lmmp, lmm_size);
314         mode = mdd_object_type(child);
315         if (S_ISREG(mode) && lmm_size > 0) {
316                 if (set_stripe) {
317                         rc = mdd_lov_set_stripe_md(env, child, buf, handle);
318                 } else {
319                         rc = mdd_xattr_set_txn(env, child, buf,
320                                                MDS_LOV_MD_NAME, 0, handle);
321                 }
322         } else if (S_ISDIR(mode)) {
323                 if (lmmp == NULL && lmm_size == 0) {
324                         struct mdd_device *mdd = mdd_obj2mdd_dev(child);
325                         struct lov_mds_md *lmm = mdd_max_lmm_get(env, mdd);
326                         int size = sizeof(*lmm);
327
328                         /* Get parent dir stripe and set */
329                         if (pobj != NULL)
330                                 rc = mdd_get_md_locked(env, pobj, lmm, &size,
331                                                        MDS_LOV_MD_NAME);
332                         if (rc > 0) {
333                                 buf = mdd_buf_get(env, lmm, size);
334                                 rc = mdd_xattr_set_txn(env, child, buf,
335                                                MDS_LOV_MD_NAME, 0, handle);
336                                 if (rc)
337                                         CERROR("error on copy stripe info: rc "
338                                                 "= %d\n", rc);
339                         }
340                 } else {
341                         LASSERT(lmmp != NULL && lmm_size > 0);
342                         rc = mdd_lov_set_dir_md(env, child, buf, handle);
343                 }
344         }
345         CDEBUG(D_INFO, "Set lov md %p size %d for fid "DFID" rc %d\n",
346                         lmmp, lmm_size, PFID(mdo2fid(child)), rc);
347         RETURN(rc);
348 }
349
350 /*
351  * XXX: this is for create lsm object id, which should identify the lsm object
352  * unique in the whole mds, as I see. But it seems, we still not need it
353  * now. Right? So just borrow the ll_fid_build_ino().
354  */
355 static obd_id mdd_lov_create_id(const struct lu_fid *fid)
356 {
357         return fid_flatten(fid);
358 }
359
360 static void mdd_lov_update_objids(struct obd_device *obd, struct lov_mds_md *lmm)
361 {
362         struct mds_obd *mds = &obd->u.mds;
363         int j;
364         ENTRY;
365
366         /* if we create file without objects - lmm is NULL */
367         if (lmm == NULL)
368                 return;
369
370         for (j = 0; j < le32_to_cpu(lmm->lmm_stripe_count); j++) {
371                 int i = le32_to_cpu(lmm->lmm_objects[j].l_ost_idx);
372                 obd_id id = le64_to_cpu(lmm->lmm_objects[j].l_object_id);
373                 int page = i / OBJID_PER_PAGE();
374                 int idx = i % OBJID_PER_PAGE();
375                 obd_id *data = mds->mds_lov_page_array[page];
376
377                 CDEBUG(D_INODE,"update last object for ost %d - new %llu"
378                                " old %llu\n", i, id, data[idx]);
379                 if (id > data[idx]) {
380                         data[idx] = id;
381                         cfs_bitmap_set(mds->mds_lov_page_dirty, page);
382                 }
383         }
384         EXIT;
385 }
386
387 void mdd_lov_objid_update(struct mdd_device *mdd, struct lov_mds_md *lmm)
388 {
389         mdd_lov_update_objids(mdd->mdd_obd_dev, lmm);
390 }
391
392 void mdd_lov_create_finish(const struct lu_env *env, struct mdd_device *mdd,
393                            struct lov_mds_md *lmm, int lmm_size,
394                            const struct md_op_spec *spec)
395 {
396         if (lmm && !spec->u.sp_ea.no_lov_create)
397                 OBD_FREE(lmm, lmm_size);
398 }
399
400 int mdd_lov_create(const struct lu_env *env, struct mdd_device *mdd,
401                    struct mdd_object *parent, struct mdd_object *child,
402                    struct lov_mds_md **lmm, int *lmm_size,
403                    const struct md_op_spec *spec, struct lu_attr *la)
404 {
405         struct obd_device     *obd = mdd2obd_dev(mdd);
406         struct obd_export     *lov_exp = obd->u.mds.mds_osc_exp;
407         struct obdo           *oa;
408         struct lov_stripe_md  *lsm = NULL;
409         const void            *eadata = spec->u.sp_ea.eadata;
410         __u32                  create_flags = spec->sp_cr_flags;
411         struct obd_trans_info *oti = &mdd_env_info(env)->mti_oti;
412         int                    rc = 0;
413         ENTRY;
414
415         if (!md_should_create(create_flags))
416                 RETURN(0);
417
418         oti_init(oti, NULL);
419
420         /* replay case, has objects already, only get lov from eadata */
421         if (spec->u.sp_ea.no_lov_create != 0) {
422                 *lmm = (struct lov_mds_md *)spec->u.sp_ea.eadata;
423                 *lmm_size = spec->u.sp_ea.eadatalen;
424                 RETURN(0);
425         }
426
427         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
428                 GOTO(out_ids, rc = -ENOMEM);
429
430         LASSERT(lov_exp != NULL);
431         oa = &mdd_env_info(env)->mti_oa;
432
433         oa->o_uid = 0; /* must have 0 uid / gid on OST */
434         oa->o_gid = 0;
435         oa->o_gr = FILTER_GROUP_MDS0 + mdd2lu_dev(mdd)->ld_site->ls_node_id;
436         oa->o_mode = S_IFREG | 0600;
437         oa->o_id = mdd_lov_create_id(mdd_object_fid(child));
438         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLFLAGS |
439                 OBD_MD_FLMODE | OBD_MD_FLUID | OBD_MD_FLGID | OBD_MD_FLGROUP;
440         oa->o_size = 0;
441
442         if (!(create_flags & MDS_OPEN_HAS_OBJS)) {
443                 if (create_flags & MDS_OPEN_HAS_EA) {
444                         LASSERT(eadata != NULL);
445                         rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE, lov_exp,
446                                            0, &lsm, (void*)eadata);
447                         if (rc)
448                                 GOTO(out_oti, rc);
449                         lsm->lsm_object_id = oa->o_id;
450                         lsm->lsm_object_gr = oa->o_gr;
451                 } else if (parent != NULL) {
452                         /* get lov ea from parent and set to lov */
453                         struct lov_mds_md *_lmm;
454                         int _lmm_size;
455
456                         _lmm_size = mdd_lov_mdsize(env, mdd);
457                         _lmm = mdd_max_lmm_get(env, mdd);
458
459                         if (_lmm == NULL)
460                                 GOTO(out_oti, rc = -ENOMEM);
461
462                         rc = mdd_get_md_locked(env, parent, _lmm,
463                                                &_lmm_size,
464                                                MDS_LOV_MD_NAME);
465                         if (rc > 0)
466                                 rc = obd_iocontrol(OBD_IOC_LOV_SETSTRIPE,
467                                                    lov_exp, 0, &lsm, _lmm);
468                         if (rc)
469                                 GOTO(out_oti, rc);
470                 }
471
472                 rc = obd_create(lov_exp, oa, &lsm, oti);
473                 if (rc) {
474                         if (rc > 0) {
475                                 CERROR("Create error for "DFID": %d\n",
476                                        PFID(mdo2fid(child)), rc);
477                                 rc = -EIO;
478                         }
479                         GOTO(out_oti, rc);
480                 }
481                 LASSERT(lsm->lsm_object_gr >= FILTER_GROUP_MDS0);
482         } else {
483                 LASSERT(eadata != NULL);
484                 rc = obd_iocontrol(OBD_IOC_LOV_SETEA, lov_exp, 0, &lsm,
485                                    (void*)eadata);
486                 if (rc)
487                         GOTO(out_oti, rc);
488                 lsm->lsm_object_id = oa->o_id;
489                 lsm->lsm_object_gr = oa->o_gr;
490         }
491
492         /*
493          * Sometimes, we may truncate some object(without lsm) then open it
494          * (with write flags), so creating lsm above.  The Nonzero(truncated)
495          * size should tell ost, since size attr is in charge by OST.
496          */
497         if (la->la_size && la->la_valid & LA_SIZE) {
498                 struct obd_info *oinfo = &mdd_env_info(env)->mti_oi;
499
500                 memset(oinfo, 0, sizeof(*oinfo));
501
502                 /* When setting attr to ost, FLBKSZ is not needed. */
503                 oa->o_valid &= ~OBD_MD_FLBLKSZ;
504                 obdo_from_la(oa, la, OBD_MD_FLTYPE | OBD_MD_FLATIME |
505                              OBD_MD_FLMTIME | OBD_MD_FLCTIME | OBD_MD_FLSIZE);
506
507                 /*
508                  * XXX: Pack lustre id to OST, in OST, it will be packed by
509                  * filter_fid, but can not see what is the usages. So just pack
510                  * o_seq o_ver here, maybe fix it after this cycle.
511                  */
512                 oa->o_fid = fid_seq(mdd_object_fid(child));
513                 oa->o_generation = fid_oid(mdd_object_fid(child));
514                 oa->o_valid |= OBD_MD_FLFID | OBD_MD_FLGENER;
515                 oinfo->oi_oa = oa;
516                 oinfo->oi_md = lsm;
517                 oinfo->oi_capa = mdo_capa_get(env, child, NULL,
518                                               CAPA_OPC_MDS_DEFAULT);
519                 oinfo->oi_policy.l_extent.start = la->la_size;
520                 oinfo->oi_policy.l_extent.end = OBD_OBJECT_EOF;
521
522                 if (IS_ERR(oinfo->oi_capa))
523                         oinfo->oi_capa = NULL;
524
525                 rc = obd_punch_rqset(lov_exp, oinfo, oti);
526                 capa_put(oinfo->oi_capa);
527                 if (rc) {
528                         CERROR("Error setting attrs for "DFID": rc %d\n",
529                                PFID(mdo2fid(child)), rc);
530                         if (rc > 0) {
531                                 CERROR("obd_setattr for "DFID" rc %d\n",
532                                         PFID(mdo2fid(child)), rc);
533                                 rc = -EIO;
534                         }
535                         GOTO(out_oti, rc);
536                 }
537         }
538
539         /* blksize should be changed after create data object */
540         la->la_valid |= LA_BLKSIZE;
541         la->la_blksize = oa->o_blksize;
542         *lmm = NULL;
543         rc = obd_packmd(lov_exp, lmm, lsm);
544         if (rc < 0) {
545                 CERROR("Cannot pack lsm, err = %d\n", rc);
546                 GOTO(out_oti, rc);
547         }
548         *lmm_size = rc;
549         rc = 0;
550         EXIT;
551 out_oti:
552         oti_free_cookies(oti);
553 out_ids:
554         if (lsm)
555                 obd_free_memmd(lov_exp, &lsm);
556
557         return rc;
558 }
559
560 /*
561  * used when destroying orphans and from mds_reint_unlink() when MDS wants to
562  * destroy objects on OSS.
563  */
564 static
565 int mdd_lovobj_unlink(const struct lu_env *env, struct mdd_device *mdd,
566                       struct mdd_object *obj, struct lu_attr *la,
567                       struct lov_mds_md *lmm, int lmm_size,
568                       struct llog_cookie *logcookies,
569                       int log_unlink)
570 {
571         struct obd_device     *obd = mdd2obd_dev(mdd);
572         struct obd_export     *lov_exp = obd->u.mds.mds_osc_exp;
573         struct lov_stripe_md  *lsm = NULL;
574         struct obd_trans_info *oti = &mdd_env_info(env)->mti_oti;
575         struct obdo           *oa = &mdd_env_info(env)->mti_oa;
576         int rc;
577         ENTRY;
578
579         if (lmm_size == 0)
580                 RETURN(0);
581
582         rc = obd_unpackmd(lov_exp, &lsm, lmm, lmm_size);
583         if (rc < 0) {
584                 CERROR("Error unpack md %p\n", lmm);
585                 RETURN(rc);
586         } else {
587                 LASSERT(rc >= sizeof(*lsm));
588                 rc = 0;
589         }
590
591         oa->o_id = lsm->lsm_object_id;
592         oa->o_gr = FILTER_GROUP_MDS0 + mdd2lu_dev(mdd)->ld_site->ls_node_id;
593         oa->o_mode = la->la_mode & S_IFMT;
594         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
595
596         oti_init(oti, NULL);
597         if (log_unlink && logcookies) {
598                 oa->o_valid |= OBD_MD_FLCOOKIE;
599                 oti->oti_logcookies = logcookies;
600         }
601
602         CDEBUG(D_INFO, "destroying OSS object %d/%d\n",
603                         (int)oa->o_id, (int)oa->o_gr);
604
605         rc = obd_destroy(lov_exp, oa, lsm, oti, NULL);
606
607         obd_free_memmd(lov_exp, &lsm);
608         RETURN(rc);
609 }
610
611
612 /*
613  * called with obj not locked. 
614  */
615 int mdd_lov_destroy(const struct lu_env *env, struct mdd_device *mdd,
616                     struct mdd_object *obj, struct lu_attr *la)
617 {
618         struct md_attr    *ma = &mdd_env_info(env)->mti_ma;
619         int                rc;
620         ENTRY;
621
622         if (unlikely(la->la_nlink != 0)) {
623                 CWARN("Attempt to destroy OSS object when nlink == %d\n",
624                       la->la_nlink);
625                 RETURN(0);
626         }
627
628         ma->ma_lmm_size = mdd_lov_mdsize(env, mdd);
629         ma->ma_lmm = mdd_max_lmm_get(env, mdd);
630         ma->ma_cookie_size = mdd_lov_cookiesize(env, mdd);
631         ma->ma_cookie = mdd_max_cookie_get(env, mdd);
632         if (ma->ma_lmm == NULL || ma->ma_cookie == NULL)
633                 RETURN(rc = -ENOMEM);
634
635         /* get lov ea */
636         rc = mdd_get_md_locked(env, obj, ma->ma_lmm, &ma->ma_lmm_size,
637                                MDS_LOV_MD_NAME);
638         if (rc) {
639                 CWARN("Get lov ea failed for "DFID"\n", PFID(mdo2fid(obj)));
640                 RETURN(rc);
641         }
642         ma->ma_valid = MA_LOV;
643         
644         rc = mdd_unlink_log(env, mdd, obj, ma);
645         if (rc) {
646                 CWARN("mds unlink log for "DFID" failed: %d\n",
647                        PFID(mdo2fid(obj)), rc);
648                 RETURN(rc);
649         }
650         if (ma->ma_valid | MA_COOKIE)
651                 rc = mdd_lovobj_unlink(env, mdd, obj, la, 
652                                        ma->ma_lmm, ma->ma_lmm_size,
653                                        ma->ma_cookie, 1);
654         RETURN(rc);
655 }
656
657 int mdd_log_op_unlink(struct obd_device *obd,
658                       struct lov_mds_md *lmm, int lmm_size,
659                       struct llog_cookie *logcookies, int cookies_size)
660 {
661         struct mds_obd *mds = &obd->u.mds;
662         struct lov_stripe_md *lsm = NULL;
663         struct llog_unlink_rec *lur;
664         struct llog_ctxt *ctxt;
665         int rc;
666         ENTRY;
667
668         if (IS_ERR(mds->mds_osc_obd))
669                 RETURN(PTR_ERR(mds->mds_osc_obd));
670
671         rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
672         if (rc < 0)
673                 RETURN(rc);
674         rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, lsm);
675         if (rc)
676                 GOTO(out, rc);
677         /* first prepare unlink log record */
678         OBD_ALLOC(lur, sizeof(*lur));
679         if (!lur)
680                 GOTO(out, rc = -ENOMEM);
681         lur->lur_hdr.lrh_len = lur->lur_tail.lrt_len = sizeof(*lur);
682         lur->lur_hdr.lrh_type = MDS_UNLINK_REC;
683
684         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
685         rc = llog_add(ctxt, &lur->lur_hdr, lsm, logcookies,
686                       cookies_size / sizeof(struct llog_cookie));
687         llog_ctxt_put(ctxt);
688
689         OBD_FREE(lur, sizeof(*lur));
690 out:
691         obd_free_memmd(mds->mds_osc_exp, &lsm);
692         RETURN(rc);
693 }
694
695 int mdd_unlink_log(const struct lu_env *env, struct mdd_device *mdd,
696                    struct mdd_object *mdd_cobj, struct md_attr *ma)
697 {
698         struct obd_device *obd = mdd2obd_dev(mdd);
699
700         LASSERT(ma->ma_valid & MA_LOV);
701
702         if ((ma->ma_cookie_size > 0) &&
703             (mdd_log_op_unlink(obd, ma->ma_lmm, ma->ma_lmm_size,
704                                ma->ma_cookie, ma->ma_cookie_size) > 0)) {
705                 ma->ma_valid |= MA_COOKIE;
706         }
707         return 0;
708 }
709
710 int mdd_log_op_setattr(struct obd_device *obd, __u32 uid, __u32 gid,
711                       struct lov_mds_md *lmm, int lmm_size,
712                       struct llog_cookie *logcookies, int cookies_size)
713 {
714         struct mds_obd *mds = &obd->u.mds;
715         struct lov_stripe_md *lsm = NULL;
716         struct llog_setattr_rec *lsr;
717         struct llog_ctxt *ctxt;
718         int rc;
719         ENTRY;
720
721         if (IS_ERR(mds->mds_osc_obd))
722                 RETURN(PTR_ERR(mds->mds_osc_obd));
723
724         rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
725         if (rc < 0)
726                 RETURN(rc);
727
728         rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, lsm);
729         if (rc)
730                 GOTO(out, rc);
731
732         OBD_ALLOC(lsr, sizeof(*lsr));
733         if (!lsr)
734                 GOTO(out, rc = -ENOMEM);
735
736         /* prepare setattr log record */
737         lsr->lsr_hdr.lrh_len = lsr->lsr_tail.lrt_len = sizeof(*lsr);
738         lsr->lsr_hdr.lrh_type = MDS_SETATTR_REC;
739         lsr->lsr_uid = uid;
740         lsr->lsr_gid = gid;
741
742         /* write setattr log */
743         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
744         rc = llog_add(ctxt, &lsr->lsr_hdr, lsm, logcookies,
745                       cookies_size / sizeof(struct llog_cookie));
746
747         llog_ctxt_put(ctxt);
748
749         OBD_FREE(lsr, sizeof(*lsr));
750  out:
751         obd_free_memmd(mds->mds_osc_exp, &lsm);
752         RETURN(rc);
753 }
754
755 int mdd_setattr_log(const struct lu_env *env, struct mdd_device *mdd,
756                     const struct md_attr *ma,
757                     struct lov_mds_md *lmm, int lmm_size,
758                     struct llog_cookie *logcookies, int cookies_size)
759 {
760         struct obd_device *obd = mdd2obd_dev(mdd);
761
762         /* journal chown/chgrp in llog, just like unlink */
763         if (lmm_size > 0) {
764                 CDEBUG(D_INFO, "setattr llog for uid/gid=%lu/%lu\n",
765                         (unsigned long)ma->ma_attr.la_uid, 
766                         (unsigned long)ma->ma_attr.la_gid);
767                 return mdd_log_op_setattr(obd, ma->ma_attr.la_uid,
768                                           ma->ma_attr.la_gid, lmm, 
769                                           lmm_size, logcookies,
770                                           cookies_size);
771         } else
772                 return 0;
773 }
774
775 static int mdd_osc_setattr_async(struct obd_device *obd, __u32 uid, __u32 gid,
776                           struct lov_mds_md *lmm, int lmm_size,
777                           struct llog_cookie *logcookies, __u64 id, __u32 gen,
778                           struct obd_capa *oc)
779 {
780         struct mds_obd *mds = &obd->u.mds;
781         struct obd_trans_info oti = { 0 };
782         struct obd_info oinfo = { { { 0 } } };
783         int rc;
784         ENTRY;
785
786         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OST_SETATTR))
787                 RETURN(0);
788
789         /* first get memory EA */
790         OBDO_ALLOC(oinfo.oi_oa);
791         if (!oinfo.oi_oa)
792                 RETURN(-ENOMEM);
793
794         LASSERT(lmm);
795
796         rc = obd_unpackmd(mds->mds_osc_exp, &oinfo.oi_md, lmm, lmm_size);
797         if (rc < 0) {
798                 CERROR("Error unpack md %p for inode "LPU64"\n", lmm, id);
799                 GOTO(out, rc);
800         }
801
802         rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, oinfo.oi_md);
803         if (rc) {
804                 CERROR("Error revalidate lsm %p \n", oinfo.oi_md);
805                 GOTO(out, rc);
806         }
807
808         /* then fill oa */
809         oinfo.oi_oa->o_uid = uid;
810         oinfo.oi_oa->o_gid = gid;
811         oinfo.oi_oa->o_id = oinfo.oi_md->lsm_object_id;
812         oinfo.oi_oa->o_gr = oinfo.oi_md->lsm_object_gr;
813         oinfo.oi_oa->o_valid |= OBD_MD_FLID | OBD_MD_FLGROUP |
814                                 OBD_MD_FLUID | OBD_MD_FLGID;
815         if (logcookies) {
816                 oinfo.oi_oa->o_valid |= OBD_MD_FLCOOKIE;
817                 oti.oti_logcookies = logcookies;
818         }
819
820         oinfo.oi_oa->o_fid = id;
821         oinfo.oi_oa->o_generation = gen;
822         oinfo.oi_oa->o_valid |= OBD_MD_FLFID | OBD_MD_FLGENER;
823         oinfo.oi_capa = oc;
824
825         /* do async setattr from mds to ost not waiting for responses. */
826         rc = obd_setattr_async(mds->mds_osc_exp, &oinfo, &oti, NULL);
827         if (rc)
828                 CDEBUG(D_INODE, "mds to ost setattr objid 0x"LPX64
829                        " on ost error %d\n", oinfo.oi_md->lsm_object_id, rc);
830 out:
831         if (oinfo.oi_md)
832                 obd_free_memmd(mds->mds_osc_exp, &oinfo.oi_md);
833         OBDO_FREE(oinfo.oi_oa);
834         RETURN(rc);
835 }
836
837 int mdd_lov_setattr_async(const struct lu_env *env, struct mdd_object *obj,
838                           struct lov_mds_md *lmm, int lmm_size, 
839                           struct llog_cookie *logcookies)
840 {
841         struct mdd_device   *mdd = mdo2mdd(&obj->mod_obj);
842         struct obd_device   *obd = mdd2obd_dev(mdd);
843         struct lu_attr      *tmp_la = &mdd_env_info(env)->mti_la;
844         const struct lu_fid *fid = mdd_object_fid(obj);
845         struct obd_capa     *oc;
846         int rc = 0;
847         ENTRY;
848
849         mdd_read_lock(env, obj);
850         rc = mdo_attr_get(env, obj, tmp_la, mdd_object_capa(env, obj));
851         mdd_read_unlock(env, obj);
852         if (rc)
853                 RETURN(rc);
854
855         oc = mdo_capa_get(env, obj, NULL, CAPA_OPC_MDS_DEFAULT);
856         if (IS_ERR(oc))
857                 oc = NULL;
858
859         rc = mdd_osc_setattr_async(obd, tmp_la->la_uid, tmp_la->la_gid, lmm,
860                                    lmm_size, logcookies, fid_seq(fid),
861                                    fid_oid(fid), oc);
862
863         capa_put(oc);
864
865         RETURN(rc);
866 }