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