Whamcloud - gitweb
9f0f8c235705dffa35ef1076ddff094fedd5f599
[fs/lustre-release.git] / lustre / mdd / mdd_object.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, 2013, Intel Corporation.
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_object.c
37  *
38  * Lustre Metadata Server (mdd) routines
39  *
40  * Author: Wang Di <wangdi@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_MDS
44
45 #include <linux/module.h>
46 #include <obd.h>
47 #include <obd_class.h>
48 #include <obd_support.h>
49 #include <lprocfs_status.h>
50 /* fid_be_cpu(), fid_cpu_to_be(). */
51 #include <lustre_fid.h>
52 #include <lustre_idmap.h>
53 #include <lustre_param.h>
54 #include <lustre_mds.h>
55 #include <lustre/lustre_idl.h>
56
57 #include "mdd_internal.h"
58
59 static const struct lu_object_operations mdd_lu_obj_ops;
60
61 static int mdd_xattr_get(const struct lu_env *env,
62                          struct md_object *obj, struct lu_buf *buf,
63                          const char *name);
64
65 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
66                struct lu_attr *la, struct lustre_capa *capa)
67 {
68         if (mdd_object_exists(obj) == 0) {
69                 CERROR("%s: object "DFID" not found: rc = -2\n",
70                        mdd_obj_dev_name(obj), PFID(mdd_object_fid(obj)));
71                 return -ENOENT;
72         }
73
74         return mdo_attr_get(env, obj, la, capa);
75 }
76
77 void mdd_flags_xlate(struct mdd_object *obj, __u32 flags)
78 {
79         obj->mod_flags &= ~(APPEND_OBJ|IMMUTE_OBJ);
80
81         if (flags & LUSTRE_APPEND_FL)
82                 obj->mod_flags |= APPEND_OBJ;
83
84         if (flags & LUSTRE_IMMUTABLE_FL)
85                 obj->mod_flags |= IMMUTE_OBJ;
86 }
87
88 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
89 {
90         struct mdd_thread_info *info;
91
92         lu_env_refill((struct lu_env *)env);
93         info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
94         LASSERT(info != NULL);
95         return info;
96 }
97
98 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
99 {
100         struct lu_buf *buf;
101
102         buf = &mdd_env_info(env)->mti_buf[0];
103         buf->lb_buf = area;
104         buf->lb_len = len;
105         return buf;
106 }
107
108 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
109                                        const void *area, ssize_t len)
110 {
111         struct lu_buf *buf;
112
113         buf = &mdd_env_info(env)->mti_buf[0];
114         buf->lb_buf = (void *)area;
115         buf->lb_len = len;
116         return buf;
117 }
118
119 struct lu_object *mdd_object_alloc(const struct lu_env *env,
120                                    const struct lu_object_header *hdr,
121                                    struct lu_device *d)
122 {
123         struct mdd_object *mdd_obj;
124
125         OBD_SLAB_ALLOC_PTR_GFP(mdd_obj, mdd_object_kmem, GFP_NOFS);
126         if (mdd_obj != NULL) {
127                 struct lu_object *o;
128
129                 o = mdd2lu_obj(mdd_obj);
130                 lu_object_init(o, NULL, d);
131                 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
132                 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
133                 mdd_obj->mod_count = 0;
134                 o->lo_ops = &mdd_lu_obj_ops;
135                 return o;
136         } else {
137                 return NULL;
138         }
139 }
140
141 static int mdd_object_init(const struct lu_env *env, struct lu_object *o,
142                            const struct lu_object_conf *unused)
143 {
144         struct mdd_device *d = lu2mdd_dev(o->lo_dev);
145         struct mdd_object *mdd_obj = lu2mdd_obj(o);
146         struct lu_object  *below;
147         struct lu_device  *under;
148         ENTRY;
149
150         mdd_obj->mod_cltime = 0;
151         under = &d->mdd_child->dd_lu_dev;
152         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
153         if (IS_ERR(below))
154                 RETURN(PTR_ERR(below));
155
156         lu_object_add(o, below);
157
158         RETURN(0);
159 }
160
161 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
162 {
163         int rc = 0;
164
165         if (lu_object_exists(o)) {
166                 struct mdd_object *mdd_obj = lu2mdd_obj(o);
167                 struct lu_attr *attr = MDD_ENV_VAR(env, la_for_start);
168
169                 rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
170                 if (rc == 0)
171                         mdd_flags_xlate(mdd_obj, attr->la_flags);
172         }
173
174         return rc;
175 }
176
177 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
178 {
179         struct mdd_object *mdd = lu2mdd_obj(o);
180
181         lu_object_fini(o);
182         OBD_SLAB_FREE_PTR(mdd, mdd_object_kmem);
183 }
184
185 static int mdd_object_print(const struct lu_env *env, void *cookie,
186                             lu_printer_t p, const struct lu_object *o)
187 {
188         struct mdd_object *mdd = lu2mdd_obj((struct lu_object *)o);
189         return (*p)(env, cookie, LUSTRE_MDD_NAME"-object@%p(open_count=%d, "
190                     "valid=%x, cltime="LPU64", flags=%lx)",
191                     mdd, mdd->mod_count, mdd->mod_valid,
192                     mdd->mod_cltime, mdd->mod_flags);
193 }
194
195 static const struct lu_object_operations mdd_lu_obj_ops = {
196         .loo_object_init    = mdd_object_init,
197         .loo_object_start   = mdd_object_start,
198         .loo_object_free    = mdd_object_free,
199         .loo_object_print   = mdd_object_print,
200 };
201
202 struct mdd_object *mdd_object_find(const struct lu_env *env,
203                                    struct mdd_device *d,
204                                    const struct lu_fid *f)
205 {
206         return md2mdd_obj(md_object_find_slice(env, &d->mdd_md_dev, f));
207 }
208
209 /*
210  * No permission check is needed.
211  */
212 int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
213                  struct md_attr *ma)
214 {
215         struct mdd_object *mdd_obj = md2mdd_obj(obj);
216         int               rc;
217
218         ENTRY;
219
220         rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
221                         mdd_object_capa(env, md2mdd_obj(obj)));
222         if ((ma->ma_need & MA_INODE) != 0 && mdd_is_dead_obj(mdd_obj))
223                 ma->ma_attr.la_nlink = 0;
224
225         RETURN(rc);
226 }
227
228 /*
229  * No permission check is needed.
230  */
231 static int mdd_xattr_get(const struct lu_env *env,
232                          struct md_object *obj, struct lu_buf *buf,
233                          const char *name)
234 {
235         struct mdd_object *mdd_obj = md2mdd_obj(obj);
236         int rc;
237
238         ENTRY;
239
240         if (mdd_object_exists(mdd_obj) == 0) {
241                 CERROR("%s: object "DFID" not found: rc = -2\n",
242                        mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
243                 return -ENOENT;
244         }
245
246         /* If the object has been delete from the namespace, then
247          * get linkEA should return -ENOENT as well */
248         if (unlikely((mdd_obj->mod_flags & (DEAD_OBJ | ORPHAN_OBJ)) &&
249                       strcmp(name, XATTR_NAME_LINK) == 0))
250                 RETURN(-ENOENT);
251
252         mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
253         rc = mdo_xattr_get(env, mdd_obj, buf, name,
254                            mdd_object_capa(env, mdd_obj));
255         mdd_read_unlock(env, mdd_obj);
256
257         RETURN(rc);
258 }
259
260 /*
261  * Permission check is done when open,
262  * no need check again.
263  */
264 int mdd_readlink(const struct lu_env *env, struct md_object *obj,
265                  struct lu_buf *buf)
266 {
267         struct mdd_object *mdd_obj = md2mdd_obj(obj);
268         struct dt_object  *next;
269         loff_t             pos = 0;
270         int                rc;
271         ENTRY;
272
273         if (mdd_object_exists(mdd_obj) == 0) {
274                 CERROR("%s: object "DFID" not found: rc = -2\n",
275                        mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
276                 return -ENOENT;
277         }
278
279         next = mdd_object_child(mdd_obj);
280         LASSERT(next != NULL);
281         LASSERT(next->do_body_ops != NULL);
282         LASSERT(next->do_body_ops->dbo_read != NULL);
283         mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
284         rc = next->do_body_ops->dbo_read(env, next, buf, &pos,
285                                          mdd_object_capa(env, mdd_obj));
286         mdd_read_unlock(env, mdd_obj);
287         RETURN(rc);
288 }
289
290 /*
291  * No permission check is needed.
292  */
293 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
294                           struct lu_buf *buf)
295 {
296         struct mdd_object *mdd_obj = md2mdd_obj(obj);
297         int rc;
298
299         ENTRY;
300
301         mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
302         rc = mdo_xattr_list(env, mdd_obj, buf, mdd_object_capa(env, mdd_obj));
303         mdd_read_unlock(env, mdd_obj);
304
305         RETURN(rc);
306 }
307
308 int mdd_declare_object_create_internal(const struct lu_env *env,
309                                        struct mdd_object *p,
310                                        struct mdd_object *c,
311                                        struct lu_attr *attr,
312                                        struct thandle *handle,
313                                        const struct md_op_spec *spec,
314                                        struct dt_allocation_hint *hint)
315 {
316         struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
317         const struct dt_index_features *feat = spec->sp_feat;
318         int rc;
319         ENTRY;
320
321         if (feat != &dt_directory_features && feat != NULL) {
322                 dof->dof_type = DFT_INDEX;
323                 dof->u.dof_idx.di_feat = feat;
324
325         } else {
326                 dof->dof_type = dt_mode_to_dft(attr->la_mode);
327                 if (dof->dof_type == DFT_REGULAR) {
328                         dof->u.dof_reg.striped =
329                                 md_should_create(spec->sp_cr_flags);
330                         if (spec->sp_cr_flags & MDS_OPEN_HAS_EA)
331                                 dof->u.dof_reg.striped = 0;
332                         /* is this replay? */
333                         if (spec->no_create)
334                                 dof->u.dof_reg.striped = 0;
335                 }
336         }
337
338         rc = mdo_declare_create_obj(env, c, attr, hint, dof, handle);
339
340         RETURN(rc);
341 }
342
343 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
344                                struct mdd_object *c, struct lu_attr *attr,
345                                struct thandle *handle,
346                                const struct md_op_spec *spec,
347                                struct dt_allocation_hint *hint)
348 {
349         struct dt_object_format *dof = &mdd_env_info(env)->mti_dof;
350         int rc;
351         ENTRY;
352
353         LASSERT(!mdd_object_exists(c));
354
355         rc = mdo_create_obj(env, c, attr, hint, dof, handle);
356
357         LASSERT(ergo(rc == 0, mdd_object_exists(c)));
358
359         RETURN(rc);
360 }
361
362 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
363                           const struct lu_attr *attr, struct thandle *handle,
364                           int needacl)
365 {
366         int rc;
367         ENTRY;
368
369         rc = mdo_attr_set(env, obj, attr, handle, mdd_object_capa(env, obj));
370 #ifdef CONFIG_FS_POSIX_ACL
371         if (!rc && (attr->la_valid & LA_MODE) && needacl)
372                 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
373 #endif
374         RETURN(rc);
375 }
376
377 int mdd_update_time(const struct lu_env *env, struct mdd_object *obj,
378                     const struct lu_attr *oattr, struct lu_attr *attr,
379                     struct thandle *handle)
380 {
381         int rc = 0;
382         ENTRY;
383
384         LASSERT(attr->la_valid & LA_CTIME);
385         LASSERT(oattr != NULL);
386
387         /* Make sure the ctime is increased only, however, it's not strictly
388          * reliable at here because there is not guarantee to hold lock on
389          * object, so we just bypass some unnecessary cmtime setting first
390          * and OSD has to check it again. */
391         if (attr->la_ctime < oattr->la_ctime)
392                 attr->la_valid &= ~(LA_MTIME | LA_CTIME);
393         else if (attr->la_valid == LA_CTIME &&
394                  attr->la_ctime == oattr->la_ctime)
395                 attr->la_valid &= ~LA_CTIME;
396
397         if (attr->la_valid != 0)
398                 rc = mdd_attr_set_internal(env, obj, attr, handle, 0);
399         RETURN(rc);
400 }
401
402 /*
403  * This gives the same functionality as the code between
404  * sys_chmod and inode_setattr
405  * chown_common and inode_setattr
406  * utimes and inode_setattr
407  * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
408  */
409 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
410                         const struct lu_attr *oattr, struct lu_attr *la,
411                         const unsigned long flags)
412 {
413         struct lu_ucred  *uc;
414         int               rc = 0;
415         ENTRY;
416
417         if (!la->la_valid)
418                 RETURN(0);
419
420         /* Do not permit change file type */
421         if (la->la_valid & LA_TYPE)
422                 RETURN(-EPERM);
423
424         /* They should not be processed by setattr */
425         if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
426                 RETURN(-EPERM);
427
428         LASSERT(oattr != NULL);
429
430         /* export destroy does not have ->le_ses, but we may want
431          * to drop LUSTRE_SOM_FL. */
432         uc = lu_ucred_check(env);
433         if (uc == NULL)
434                 RETURN(0);
435
436         if (la->la_valid == LA_CTIME) {
437                 if (!(flags & MDS_PERM_BYPASS))
438                         /* This is only for set ctime when rename's source is
439                          * on remote MDS. */
440                         rc = mdd_may_delete(env, NULL, NULL, obj, oattr, NULL,
441                                             1, 0);
442                 if (rc == 0 && la->la_ctime <= oattr->la_ctime)
443                         la->la_valid &= ~LA_CTIME;
444                 RETURN(rc);
445         }
446
447         if (la->la_valid == LA_ATIME) {
448                 /* This is atime only set for read atime update on close. */
449                 if (la->la_atime >= oattr->la_atime &&
450                     la->la_atime < (oattr->la_atime +
451                                 mdd_obj2mdd_dev(obj)->mdd_atime_diff))
452                         la->la_valid &= ~LA_ATIME;
453                 RETURN(0);
454         }
455
456         /* Check if flags change. */
457         if (la->la_valid & LA_FLAGS) {
458                 unsigned int oldflags = 0;
459                 unsigned int newflags = la->la_flags &
460                                 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
461
462                 if ((uc->uc_fsuid != oattr->la_uid) &&
463                     !md_capable(uc, CFS_CAP_FOWNER))
464                         RETURN(-EPERM);
465
466                 /* XXX: the IMMUTABLE and APPEND_ONLY flags can
467                  * only be changed by the relevant capability. */
468                 if (mdd_is_immutable(obj))
469                         oldflags |= LUSTRE_IMMUTABLE_FL;
470                 if (mdd_is_append(obj))
471                         oldflags |= LUSTRE_APPEND_FL;
472                 if ((oldflags ^ newflags) &&
473                     !md_capable(uc, CFS_CAP_LINUX_IMMUTABLE))
474                         RETURN(-EPERM);
475
476                 if (!S_ISDIR(oattr->la_mode))
477                         la->la_flags &= ~LUSTRE_DIRSYNC_FL;
478         }
479
480         if ((mdd_is_immutable(obj) || mdd_is_append(obj)) &&
481             (la->la_valid & ~LA_FLAGS) &&
482             !(flags & MDS_PERM_BYPASS))
483                 RETURN(-EPERM);
484
485         /* Check for setting the obj time. */
486         if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
487             !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
488                 if ((uc->uc_fsuid != oattr->la_uid) &&
489                     !md_capable(uc, CFS_CAP_FOWNER)) {
490                         rc = mdd_permission_internal(env, obj, oattr,
491                                                      MAY_WRITE);
492                         if (rc)
493                                 RETURN(rc);
494                 }
495         }
496
497         if (la->la_valid & LA_KILL_SUID) {
498                 la->la_valid &= ~LA_KILL_SUID;
499                 if ((oattr->la_mode & S_ISUID) &&
500                     !(la->la_valid & LA_MODE)) {
501                         la->la_mode = oattr->la_mode;
502                         la->la_valid |= LA_MODE;
503                 }
504                 la->la_mode &= ~S_ISUID;
505         }
506
507         if (la->la_valid & LA_KILL_SGID) {
508                 la->la_valid &= ~LA_KILL_SGID;
509                 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
510                         (S_ISGID | S_IXGRP)) &&
511                     !(la->la_valid & LA_MODE)) {
512                         la->la_mode = oattr->la_mode;
513                         la->la_valid |= LA_MODE;
514                 }
515                 la->la_mode &= ~S_ISGID;
516         }
517
518         /* Make sure a caller can chmod. */
519         if (la->la_valid & LA_MODE) {
520                 if (!(flags & MDS_PERM_BYPASS) &&
521                     (uc->uc_fsuid != oattr->la_uid) &&
522                     !md_capable(uc, CFS_CAP_FOWNER))
523                         RETURN(-EPERM);
524
525                 if (la->la_mode == (umode_t) -1)
526                         la->la_mode = oattr->la_mode;
527                 else
528                         la->la_mode = (la->la_mode & S_IALLUGO) |
529                                         (oattr->la_mode & ~S_IALLUGO);
530
531                 /* Also check the setgid bit! */
532                 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ?
533                                        la->la_gid : oattr->la_gid) &&
534                     !md_capable(uc, CFS_CAP_FSETID))
535                         la->la_mode &= ~S_ISGID;
536         } else {
537                la->la_mode = oattr->la_mode;
538         }
539
540         /* Make sure a caller can chown. */
541         if (la->la_valid & LA_UID) {
542                 if (la->la_uid == (uid_t) -1)
543                         la->la_uid = oattr->la_uid;
544                 if (((uc->uc_fsuid != oattr->la_uid) ||
545                      (la->la_uid != oattr->la_uid)) &&
546                     !md_capable(uc, CFS_CAP_CHOWN))
547                         RETURN(-EPERM);
548
549                 /* If the user or group of a non-directory has been
550                  * changed by a non-root user, remove the setuid bit.
551                  * 19981026 David C Niemi <niemi@tux.org>
552                  *
553                  * Changed this to apply to all users, including root,
554                  * to avoid some races. This is the behavior we had in
555                  * 2.0. The check for non-root was definitely wrong
556                  * for 2.2 anyway, as it should have been using
557                  * CAP_FSETID rather than fsuid -- 19990830 SD. */
558                 if (((oattr->la_mode & S_ISUID) == S_ISUID) &&
559                 !S_ISDIR(oattr->la_mode)) {
560                         la->la_mode &= ~S_ISUID;
561                         la->la_valid |= LA_MODE;
562                 }
563         }
564
565         /* Make sure caller can chgrp. */
566         if (la->la_valid & LA_GID) {
567                 if (la->la_gid == (gid_t) -1)
568                         la->la_gid = oattr->la_gid;
569                 if (((uc->uc_fsuid != oattr->la_uid) ||
570                      ((la->la_gid != oattr->la_gid) &&
571                       !lustre_in_group_p(uc, la->la_gid))) &&
572                     !md_capable(uc, CFS_CAP_CHOWN))
573                         RETURN(-EPERM);
574
575                 /* Likewise, if the user or group of a non-directory
576                  * has been changed by a non-root user, remove the
577                  * setgid bit UNLESS there is no group execute bit
578                  * (this would be a file marked for mandatory
579                  * locking).  19981026 David C Niemi <niemi@tux.org>
580                  *
581                  * Removed the fsuid check (see the comment above) --
582                  * 19990830 SD. */
583                 if (((oattr->la_mode & (S_ISGID | S_IXGRP)) ==
584                     (S_ISGID | S_IXGRP)) && !S_ISDIR(oattr->la_mode)) {
585                         la->la_mode &= ~S_ISGID;
586                         la->la_valid |= LA_MODE;
587                 }
588         }
589
590         /* For both Size-on-MDS case and truncate case,
591          * "la->la_valid & (LA_SIZE | LA_BLOCKS)" are ture.
592          * We distinguish them by "flags & MDS_SOM".
593          * For SOM case, it is true, the MAY_WRITE perm has been checked
594          * when open, no need check again. For truncate case, it is false,
595          * the MAY_WRITE perm should be checked here. */
596         if (flags & MDS_SOM) {
597                 /* For the "Size-on-MDS" setattr update, merge coming
598                  * attributes with the set in the inode. BUG 10641 */
599                 if ((la->la_valid & LA_ATIME) &&
600                     (la->la_atime <= oattr->la_atime))
601                         la->la_valid &= ~LA_ATIME;
602
603                 /* OST attributes do not have a priority over MDS attributes,
604                  * so drop times if ctime is equal. */
605                 if ((la->la_valid & LA_CTIME) &&
606                     (la->la_ctime <= oattr->la_ctime))
607                         la->la_valid &= ~(LA_MTIME | LA_CTIME);
608         } else {
609                 if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
610                         if (!((flags & MDS_OWNEROVERRIDE) &&
611                               (uc->uc_fsuid == oattr->la_uid)) &&
612                             !(flags & MDS_PERM_BYPASS)) {
613                                 rc = mdd_permission_internal(env, obj,
614                                                              oattr, MAY_WRITE);
615                                 if (rc != 0)
616                                         RETURN(rc);
617                         }
618                 }
619                 if (la->la_valid & LA_CTIME) {
620                         /* The pure setattr, it has the priority over what is
621                          * already set, do not drop it if ctime is equal. */
622                         if (la->la_ctime < oattr->la_ctime)
623                                 la->la_valid &= ~(LA_ATIME | LA_MTIME |
624                                                         LA_CTIME);
625                 }
626         }
627
628         RETURN(0);
629 }
630
631 /** Store a data change changelog record
632  * If this fails, we must fail the whole transaction; we don't
633  * want the change to commit without the log entry.
634  * \param mdd_obj - mdd_object of change
635  * \param handle - transacion handle
636  */
637 int mdd_changelog_data_store(const struct lu_env *env, struct mdd_device *mdd,
638                              enum changelog_rec_type type, int flags,
639                              struct mdd_object *mdd_obj, struct thandle *handle)
640 {
641         const struct lu_fid             *tfid;
642         struct llog_changelog_rec       *rec;
643         struct lu_buf                   *buf;
644         int                              reclen;
645         int                              rc;
646
647         /* Not recording */
648         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
649                 RETURN(0);
650         if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
651                 RETURN(0);
652
653         LASSERT(mdd_obj != NULL);
654         LASSERT(handle != NULL);
655
656         tfid = mdo2fid(mdd_obj);
657
658         if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
659             cfs_time_before_64(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
660                 /* Don't need multiple updates in this log */
661                 /* Don't check under lock - no big deal if we get an extra
662                    entry */
663                 RETURN(0);
664         }
665
666         reclen = llog_data_len(sizeof(*rec));
667         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
668         if (buf->lb_buf == NULL)
669                 RETURN(-ENOMEM);
670         rec = buf->lb_buf;
671
672         rec->cr.cr_flags = CLF_VERSION | (CLF_FLAGMASK & flags);
673         rec->cr.cr_type = (__u32)type;
674         rec->cr.cr_tfid = *tfid;
675         rec->cr.cr_namelen = 0;
676         mdd_obj->mod_cltime = cfs_time_current_64();
677
678         rc = mdd_changelog_store(env, mdd, rec, handle);
679
680         RETURN(rc);
681 }
682
683 static int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
684                          int flags, struct md_object *obj)
685 {
686         struct thandle *handle;
687         struct mdd_object *mdd_obj = md2mdd_obj(obj);
688         struct mdd_device *mdd = mdo2mdd(obj);
689         int rc;
690         ENTRY;
691
692         handle = mdd_trans_create(env, mdd);
693         if (IS_ERR(handle))
694                 RETURN(PTR_ERR(handle));
695
696         rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
697         if (rc)
698                 GOTO(stop, rc);
699
700         rc = mdd_trans_start(env, mdd, handle);
701         if (rc)
702                 GOTO(stop, rc);
703
704         rc = mdd_changelog_data_store(env, mdd, type, flags, mdd_obj,
705                                       handle);
706
707 stop:
708         mdd_trans_stop(env, mdd, rc, handle);
709
710         RETURN(rc);
711 }
712
713 /**
714  * Save LMA extended attributes with data from \a ma.
715  *
716  * HSM and Size-On-MDS data will be extracted from \ma if they are valid, if
717  * not, LMA EA will be first read from disk, modified and write back.
718  *
719  */
720 /* Precedence for choosing record type when multiple
721  * attributes change: setattr > mtime > ctime > atime
722  * (ctime changes when mtime does, plus chmod/chown.
723  * atime and ctime are independent.) */
724 static int mdd_attr_set_changelog(const struct lu_env *env,
725                                   struct md_object *obj, struct thandle *handle,
726                                   __u64 valid)
727 {
728         struct mdd_device *mdd = mdo2mdd(obj);
729         int bits, type = 0;
730
731         bits =  (valid & LA_SIZE)  ? 1 << CL_TRUNC : 0;
732         bits |= (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
733         bits |= (valid & LA_MTIME) ? 1 << CL_MTIME : 0;
734         bits |= (valid & LA_CTIME) ? 1 << CL_CTIME : 0;
735         bits |= (valid & LA_ATIME) ? 1 << CL_ATIME : 0;
736         bits = bits & mdd->mdd_cl.mc_mask;
737         /* This is an implementation limit rather than a protocol limit */
738         CLASSERT(CL_LAST <= sizeof(int) * 8);
739         if (bits == 0)
740                 return 0;
741
742         /* The record type is the lowest non-masked set bit */
743         type = __ffs(bits);
744
745         /* FYI we only store the first CLF_FLAGMASK bits of la_valid */
746         return mdd_changelog_data_store(env, mdd, type, (int)valid,
747                                         md2mdd_obj(obj), handle);
748 }
749
750 static int mdd_declare_attr_set(const struct lu_env *env,
751                                 struct mdd_device *mdd,
752                                 struct mdd_object *obj,
753                                 const struct lu_attr *attr,
754                                 struct thandle *handle)
755 {
756         int rc;
757
758         rc = mdo_declare_attr_set(env, obj, attr, handle);
759         if (rc)
760                 return rc;
761
762 #ifdef CONFIG_FS_POSIX_ACL
763         if (attr->la_valid & LA_MODE) {
764                 mdd_read_lock(env, obj, MOR_TGT_CHILD);
765                 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
766                                    XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
767                 mdd_read_unlock(env, obj);
768                 if (rc == -EOPNOTSUPP || rc == -ENODATA)
769                         rc = 0;
770                 else if (rc < 0)
771                         return rc;
772
773                 if (rc != 0) {
774                         struct lu_buf *buf = mdd_buf_get(env, NULL, rc);
775                         rc = mdo_declare_xattr_set(env, obj, buf,
776                                                    XATTR_NAME_ACL_ACCESS, 0,
777                                                    handle);
778                         if (rc)
779                                 return rc;
780                 }
781         }
782 #endif
783
784         rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
785         return rc;
786 }
787
788 /*
789  * LU-3671
790  *
791  * permission changes may require sync operation, to mitigate performance
792  * impact, only do this for dir and when permission is reduced.
793  *
794  * For regular files, version is updated with permission change (see VBR), async
795  * permission won't cause any issue, while missing permission change on
796  * directory may affect accessibility of other objects after recovery.
797  */
798 static inline bool permission_needs_sync(const struct lu_attr *old,
799                                          const struct lu_attr *new)
800 {
801         if (!S_ISDIR(old->la_mode))
802                 return false;
803
804         if (new->la_valid & (LA_UID | LA_GID))
805                 return true;
806
807         if (new->la_valid & LA_MODE &&
808             new->la_mode & (S_ISUID | S_ISGID | S_ISVTX))
809                 return true;
810
811         if ((new->la_valid & LA_MODE) &&
812             ((new->la_mode & old->la_mode) & S_IRWXUGO) !=
813              (old->la_mode & S_IRWXUGO))
814                 return true;
815
816         return false;
817 }
818
819 /* set attr and LOV EA at once, return updated attr */
820 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
821                  const struct md_attr *ma)
822 {
823         struct mdd_object *mdd_obj = md2mdd_obj(obj);
824         struct mdd_device *mdd = mdo2mdd(obj);
825         struct thandle *handle;
826         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
827         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
828         const struct lu_attr *la = &ma->ma_attr;
829         int rc;
830         ENTRY;
831
832         /* we do not use ->attr_set() for LOV/SOM/HSM EA any more */
833         LASSERT((ma->ma_valid & MA_LOV) == 0);
834         LASSERT((ma->ma_valid & MA_HSM) == 0);
835         LASSERT((ma->ma_valid & MA_SOM) == 0);
836
837         rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
838         if (rc)
839                 RETURN(rc);
840
841         *la_copy = ma->ma_attr;
842         rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma->ma_attr_flags);
843         if (rc)
844                 RETURN(rc);
845
846         /* setattr on "close" only change atime, or do nothing */
847         if (la->la_valid == LA_ATIME && la_copy->la_valid == 0)
848                 RETURN(0);
849
850         handle = mdd_trans_create(env, mdd);
851         if (IS_ERR(handle))
852                 RETURN(PTR_ERR(handle));
853
854         rc = mdd_declare_attr_set(env, mdd, mdd_obj, la, handle);
855         if (rc)
856                 GOTO(stop, rc);
857
858         rc = mdd_trans_start(env, mdd, handle);
859         if (rc)
860                 GOTO(stop, rc);
861
862         if (mdd->mdd_sync_permission && permission_needs_sync(attr, la))
863                 handle->th_sync = 1;
864
865         if (la->la_valid & (LA_MTIME | LA_CTIME))
866                 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
867                        la->la_mtime, la->la_ctime);
868
869         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
870         if (la_copy->la_valid & LA_FLAGS) {
871                 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
872                 if (rc == 0)
873                         mdd_flags_xlate(mdd_obj, la_copy->la_flags);
874         } else if (la_copy->la_valid) { /* setattr */
875                 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
876         }
877         mdd_write_unlock(env, mdd_obj);
878
879         if (rc == 0)
880                 rc = mdd_attr_set_changelog(env, obj, handle, la->la_valid);
881
882         GOTO(stop, rc);
883
884 stop:
885         mdd_trans_stop(env, mdd, rc, handle);
886         return rc;
887 }
888
889 static int mdd_xattr_sanity_check(const struct lu_env *env,
890                                   struct mdd_object *obj,
891                                   const struct lu_attr *attr)
892 {
893         struct lu_ucred *uc     = lu_ucred_assert(env);
894         ENTRY;
895
896         if (mdd_is_immutable(obj) || mdd_is_append(obj))
897                 RETURN(-EPERM);
898
899         if ((uc->uc_fsuid != attr->la_uid) && !md_capable(uc, CFS_CAP_FOWNER))
900                 RETURN(-EPERM);
901
902         RETURN(0);
903 }
904
905 static int mdd_declare_xattr_set(const struct lu_env *env,
906                                  struct mdd_device *mdd,
907                                  struct mdd_object *obj,
908                                  const struct lu_buf *buf,
909                                  const char *name,
910                                  int fl, struct thandle *handle)
911 {
912         int     rc;
913
914         rc = mdo_declare_xattr_set(env, obj, buf, name, fl, handle);
915         if (rc)
916                 return rc;
917
918         /* Only record user and layout xattr changes */
919         if (strncmp(XATTR_USER_PREFIX, name,
920                     sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
921             strcmp(XATTR_NAME_LOV, name) == 0) {
922                 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
923                 if (rc)
924                         return rc;
925         }
926
927         /* If HSM data is modified, this could add a changelog */
928         if (strcmp(XATTR_NAME_HSM, name) == 0) {
929                 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
930                 if (rc)
931                         return rc;
932         }
933
934         rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
935         return rc;
936 }
937
938 /*
939  * Compare current and future data of HSM EA and add a changelog if needed.
940  *
941  * Caller should have write-locked \param obj.
942  *
943  * \param buf - Future HSM EA content.
944  * \retval 0 if no changelog is needed or changelog was added properly.
945  * \retval -ve errno if there was a problem
946  */
947 static int mdd_hsm_update_locked(const struct lu_env *env,
948                                  struct md_object *obj,
949                                  const struct lu_buf *buf,
950                                  struct thandle *handle)
951 {
952         struct mdd_thread_info *info = mdd_env_info(env);
953         struct mdd_device      *mdd = mdo2mdd(obj);
954         struct mdd_object      *mdd_obj = md2mdd_obj(obj);
955         struct lu_buf          *current_buf;
956         struct md_hsm          *current_mh;
957         struct md_hsm          *new_mh;
958         int                     rc;
959         ENTRY;
960
961         OBD_ALLOC_PTR(current_mh);
962         if (current_mh == NULL)
963                 RETURN(-ENOMEM);
964
965         /* Read HSM attrs from disk */
966         CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
967         current_buf = mdd_buf_get(env, info->mti_xattr_buf,
968                                   sizeof(info->mti_xattr_buf));
969         rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM,
970                            mdd_object_capa(env, mdd_obj));
971         rc = lustre_buf2hsm(current_buf->lb_buf, rc, current_mh);
972         if (rc < 0 && rc != -ENODATA)
973                 GOTO(free, rc);
974         else if (rc == -ENODATA)
975                 current_mh->mh_flags = 0;
976
977         /* Map future HSM xattr */
978         OBD_ALLOC_PTR(new_mh);
979         if (new_mh == NULL)
980                 GOTO(free, rc = -ENOMEM);
981         lustre_buf2hsm(buf->lb_buf, buf->lb_len, new_mh);
982
983         /* If HSM flags are different, add a changelog */
984         rc = 0;
985         if (current_mh->mh_flags != new_mh->mh_flags) {
986                 int flags = 0;
987                 hsm_set_cl_event(&flags, HE_STATE);
988                 if (new_mh->mh_flags & HS_DIRTY)
989                         hsm_set_cl_flags(&flags, CLF_HSM_DIRTY);
990
991                 rc = mdd_changelog_data_store(env, mdd, CL_HSM, flags, mdd_obj,
992                                               handle);
993         }
994
995         OBD_FREE_PTR(new_mh);
996 free:
997         OBD_FREE_PTR(current_mh);
998         return(rc);
999 }
1000
1001 /**
1002  * The caller should guarantee to update the object ctime
1003  * after xattr_set if needed.
1004  */
1005 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
1006                          const struct lu_buf *buf, const char *name,
1007                          int fl)
1008 {
1009         struct mdd_object       *mdd_obj = md2mdd_obj(obj);
1010         struct lu_attr          *attr = MDD_ENV_VAR(env, cattr);
1011         struct mdd_device       *mdd = mdo2mdd(obj);
1012         struct thandle          *handle;
1013         int                      rc;
1014         ENTRY;
1015
1016         rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1017         if (rc)
1018                 RETURN(rc);
1019
1020         rc = mdd_xattr_sanity_check(env, mdd_obj, attr);
1021         if (rc)
1022                 RETURN(rc);
1023
1024         if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
1025                 rc = mdd_acl_set(env, mdd_obj, attr, buf, fl);
1026                 RETURN(rc);
1027         }
1028
1029         handle = mdd_trans_create(env, mdd);
1030         if (IS_ERR(handle))
1031                 RETURN(PTR_ERR(handle));
1032
1033         rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
1034         if (rc)
1035                 GOTO(stop, rc);
1036
1037         rc = mdd_trans_start(env, mdd, handle);
1038         if (rc)
1039                 GOTO(stop, rc);
1040
1041         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1042
1043         if (strcmp(XATTR_NAME_HSM, name) == 0) {
1044                 rc = mdd_hsm_update_locked(env, obj, buf, handle);
1045                 if (rc) {
1046                         mdd_write_unlock(env, mdd_obj);
1047                         GOTO(stop, rc);
1048                 }
1049         }
1050
1051         rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle,
1052                            mdd_object_capa(env, mdd_obj));
1053         mdd_write_unlock(env, mdd_obj);
1054         if (rc)
1055                 GOTO(stop, rc);
1056
1057         if (strcmp(XATTR_NAME_LOV, name) == 0)
1058                 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, mdd_obj,
1059                                               handle);
1060         else if (strncmp(XATTR_USER_PREFIX, name,
1061                         sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1062             strncmp(POSIX_ACL_XATTR_ACCESS, name,
1063                         sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1064             strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1065                         sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1066                 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1067                                               handle);
1068
1069 stop:
1070         mdd_trans_stop(env, mdd, rc, handle);
1071
1072         RETURN(rc);
1073 }
1074
1075 static int mdd_declare_xattr_del(const struct lu_env *env,
1076                                  struct mdd_device *mdd,
1077                                  struct mdd_object *obj,
1078                                  const char *name,
1079                                  struct thandle *handle)
1080 {
1081         int rc;
1082
1083         rc = mdo_declare_xattr_del(env, obj, name, handle);
1084         if (rc)
1085                 return rc;
1086
1087         /* Only record user xattr changes */
1088         if ((strncmp(XATTR_USER_PREFIX, name,
1089                      sizeof(XATTR_USER_PREFIX) - 1) == 0))
1090                 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1091
1092         return rc;
1093 }
1094
1095 /**
1096  * The caller should guarantee to update the object ctime
1097  * after xattr_set if needed.
1098  */
1099 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1100                          const char *name)
1101 {
1102         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1103         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1104         struct mdd_device *mdd = mdo2mdd(obj);
1105         struct thandle *handle;
1106         int  rc;
1107         ENTRY;
1108
1109         rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1110         if (rc)
1111                 RETURN(rc);
1112
1113         rc = mdd_xattr_sanity_check(env, mdd_obj, attr);
1114         if (rc)
1115                 RETURN(rc);
1116
1117         handle = mdd_trans_create(env, mdd);
1118         if (IS_ERR(handle))
1119                 RETURN(PTR_ERR(handle));
1120
1121         rc = mdd_declare_xattr_del(env, mdd, mdd_obj, name, handle);
1122         if (rc)
1123                 GOTO(stop, rc);
1124
1125         rc = mdd_trans_start(env, mdd, handle);
1126         if (rc)
1127                 GOTO(stop, rc);
1128
1129         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1130         rc = mdo_xattr_del(env, mdd_obj, name, handle,
1131                            mdd_object_capa(env, mdd_obj));
1132         mdd_write_unlock(env, mdd_obj);
1133         if (rc)
1134                 GOTO(stop, rc);
1135
1136         /* Only record system & user xattr changes */
1137         if (strncmp(XATTR_USER_PREFIX, name,
1138                                   sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1139                           strncmp(POSIX_ACL_XATTR_ACCESS, name,
1140                                   sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1141                           strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1142                                   sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1143                 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1144                                               handle);
1145
1146 stop:
1147         mdd_trans_stop(env, mdd, rc, handle);
1148
1149         RETURN(rc);
1150 }
1151
1152 /*
1153  * read lov EA of an object
1154  * return the lov EA in an allocated lu_buf
1155  */
1156 int mdd_get_lov_ea(const struct lu_env *env, struct mdd_object *obj,
1157                    struct lu_buf *lmm_buf)
1158 {
1159         struct lu_buf   *buf = &mdd_env_info(env)->mti_big_buf;
1160         int              rc, bufsize;
1161         ENTRY;
1162
1163 repeat:
1164         rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_LOV,
1165                            mdd_object_capa(env, obj));
1166
1167         if (rc == -ERANGE) {
1168                 /* mti_big_buf is allocated but is too small
1169                  * we need to increase it */
1170                 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1171                                              buf->lb_len * 2);
1172                 if (buf->lb_buf == NULL)
1173                         GOTO(out, rc = -ENOMEM);
1174                 goto repeat;
1175         }
1176
1177         if (rc < 0)
1178                 RETURN(rc);
1179
1180         if (rc == 0)
1181                 RETURN(-ENODATA);
1182
1183         bufsize = rc;
1184         if (memcmp(buf, &LU_BUF_NULL, sizeof(*buf)) == 0) {
1185                 /* mti_big_buf was not allocated, so we have to
1186                  * allocate it based on the ea size */
1187                 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1188                                              bufsize);
1189                 if (buf->lb_buf == NULL)
1190                         GOTO(out, rc = -ENOMEM);
1191                 goto repeat;
1192         }
1193
1194         lu_buf_alloc(lmm_buf, bufsize);
1195         if (lmm_buf->lb_buf == NULL)
1196                 GOTO(out, rc = -ENOMEM);
1197
1198         memcpy(lmm_buf->lb_buf, buf->lb_buf, bufsize);
1199         rc = 0;
1200         EXIT;
1201
1202 out:
1203         if (rc < 0)
1204                 lu_buf_free(lmm_buf);
1205         return rc;
1206 }
1207
1208 static int mdd_xattr_hsm_replace(const struct lu_env *env,
1209                                  struct mdd_object *o, struct lu_buf *buf,
1210                                  struct thandle *handle)
1211 {
1212         struct hsm_attrs *attrs;
1213         __u32 hsm_flags;
1214         int flags = 0;
1215         int rc;
1216         ENTRY;
1217
1218         rc = mdo_xattr_set(env, o, buf, XATTR_NAME_HSM, LU_XATTR_REPLACE,
1219                            handle, mdd_object_capa(env, o));
1220         if (rc != 0)
1221                 RETURN(rc);
1222
1223         attrs = buf->lb_buf;
1224         hsm_flags = le32_to_cpu(attrs->hsm_flags);
1225         if (!(hsm_flags & HS_RELEASED) || mdd_is_dead_obj(o))
1226                 RETURN(0);
1227
1228         /* Add a changelog record for release. */
1229         hsm_set_cl_event(&flags, HE_RELEASE);
1230         rc = mdd_changelog_data_store(env, mdo2mdd(&o->mod_obj), CL_HSM,
1231                                       flags, o, handle);
1232         RETURN(rc);
1233 }
1234
1235 /*
1236  *  check if layout swapping between 2 objects is allowed
1237  *  the rules are:
1238  *  - only normal FIDs or non-system IGIFs
1239  *  - same type of objects
1240  *  - same owner/group (so quotas are still valid)
1241  */
1242 static int mdd_layout_swap_allowed(const struct lu_env *env,
1243                                    struct mdd_object *o1,
1244                                    const struct lu_attr *attr1,
1245                                    struct mdd_object *o2,
1246                                    const struct lu_attr *attr2)
1247 {
1248         const struct lu_fid     *fid1, *fid2;
1249         ENTRY;
1250
1251         fid1 = mdo2fid(o1);
1252         fid2 = mdo2fid(o2);
1253
1254         if (!fid_is_norm(fid1) &&
1255             (!fid_is_igif(fid1) || IS_ERR(mdd_links_get(env, o1))))
1256                 RETURN(-EBADF);
1257
1258         if (!fid_is_norm(fid2) &&
1259             (!fid_is_igif(fid2) || IS_ERR(mdd_links_get(env, o2))))
1260                 RETURN(-EBADF);
1261
1262         if (mdd_object_type(o1) != mdd_object_type(o2)) {
1263                 if (S_ISDIR(mdd_object_type(o1)))
1264                         RETURN(-ENOTDIR);
1265                 if (S_ISREG(mdd_object_type(o1)))
1266                         RETURN(-EISDIR);
1267                 RETURN(-EBADF);
1268         }
1269
1270         if ((attr1->la_uid != attr2->la_uid) ||
1271             (attr1->la_gid != attr2->la_gid))
1272                 RETURN(-EPERM);
1273
1274         RETURN(0);
1275 }
1276
1277 /**
1278  * swap layouts between 2 lustre objects
1279  */
1280 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
1281                             struct md_object *obj2, __u64 flags)
1282 {
1283         struct mdd_thread_info  *info = mdd_env_info(env);
1284         struct mdd_object       *fst_o = md2mdd_obj(obj1);
1285         struct mdd_object       *snd_o = md2mdd_obj(obj2);
1286         struct lu_attr          *fst_la = MDD_ENV_VAR(env, cattr);
1287         struct lu_attr          *snd_la = MDD_ENV_VAR(env, tattr);
1288         struct mdd_device       *mdd = mdo2mdd(obj1);
1289         struct lov_mds_md       *fst_lmm, *snd_lmm;
1290         struct lu_buf           *fst_buf = &info->mti_buf[0];
1291         struct lu_buf           *snd_buf = &info->mti_buf[1];
1292         struct lu_buf           *fst_hsm_buf = &info->mti_buf[2];
1293         struct lu_buf           *snd_hsm_buf = &info->mti_buf[3];
1294         struct ost_id           *saved_oi = NULL;
1295         struct thandle          *handle;
1296         __u16                    fst_gen, snd_gen;
1297         int                      fst_fl;
1298         int                      rc;
1299         int                      rc2;
1300         ENTRY;
1301
1302         CLASSERT(ARRAY_SIZE(info->mti_buf) >= 4);
1303         memset(info->mti_buf, 0, sizeof(info->mti_buf));
1304
1305         /* we have to sort the 2 obj, so locking will always
1306          * be in the same order, even in case of 2 concurrent swaps */
1307         rc = lu_fid_cmp(mdo2fid(fst_o), mdo2fid(snd_o));
1308         if (rc == 0) /* same fid ? */
1309                 RETURN(-EPERM);
1310
1311         if (rc < 0)
1312                 swap(fst_o, snd_o);
1313
1314         rc = mdd_la_get(env, fst_o, fst_la, BYPASS_CAPA);
1315         if (rc != 0)
1316                 RETURN(rc);
1317
1318         rc = mdd_la_get(env, snd_o, snd_la, BYPASS_CAPA);
1319         if (rc != 0)
1320                 RETURN(rc);
1321
1322         /* check if layout swapping is allowed */
1323         rc = mdd_layout_swap_allowed(env, fst_o, fst_la, snd_o, snd_la);
1324         if (rc != 0)
1325                 RETURN(rc);
1326
1327         handle = mdd_trans_create(env, mdd);
1328         if (IS_ERR(handle))
1329                 RETURN(PTR_ERR(handle));
1330
1331         /* objects are already sorted */
1332         mdd_write_lock(env, fst_o, MOR_TGT_CHILD);
1333         mdd_write_lock(env, snd_o, MOR_TGT_CHILD);
1334
1335         rc = mdd_get_lov_ea(env, fst_o, fst_buf);
1336         if (rc < 0 && rc != -ENODATA)
1337                 GOTO(stop, rc);
1338
1339         rc = mdd_get_lov_ea(env, snd_o, snd_buf);
1340         if (rc < 0 && rc != -ENODATA)
1341                 GOTO(stop, rc);
1342
1343         /* swapping 2 non existant layouts is a success */
1344         if (fst_buf->lb_buf == NULL && snd_buf->lb_buf == NULL)
1345                 GOTO(stop, rc = 0);
1346
1347         /* to help inode migration between MDT, it is better to
1348          * start by the no layout file (if one), so we order the swap */
1349         if (snd_buf->lb_buf == NULL) {
1350                 swap(fst_o, snd_o);
1351                 swap(fst_buf, snd_buf);
1352         }
1353
1354         /* lmm and generation layout initialization */
1355         if (fst_buf->lb_buf != NULL) {
1356                 fst_lmm = fst_buf->lb_buf;
1357                 fst_gen = le16_to_cpu(fst_lmm->lmm_layout_gen);
1358                 fst_fl  = LU_XATTR_REPLACE;
1359         } else {
1360                 fst_lmm = NULL;
1361                 fst_gen = 0;
1362                 fst_fl  = LU_XATTR_CREATE;
1363         }
1364
1365         snd_lmm = snd_buf->lb_buf;
1366         snd_gen = le16_to_cpu(snd_lmm->lmm_layout_gen);
1367
1368         /* increase the generation layout numbers */
1369         snd_gen++;
1370         fst_gen++;
1371
1372         /* set the file specific informations in lmm */
1373         if (fst_lmm != NULL) {
1374                 saved_oi = &info->mti_oa.o_oi;
1375
1376                 *saved_oi = fst_lmm->lmm_oi;
1377                 fst_lmm->lmm_layout_gen = cpu_to_le16(snd_gen);
1378                 fst_lmm->lmm_oi = snd_lmm->lmm_oi;
1379                 snd_lmm->lmm_oi = *saved_oi;
1380         } else {
1381                 if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1))
1382                         snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
1383                 else if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3))
1384                         snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V3_DEF);
1385                 else
1386                         GOTO(stop, rc = -EPROTO);
1387         }
1388         snd_lmm->lmm_layout_gen = cpu_to_le16(fst_gen);
1389
1390         /* Prepare HSM attribute if it's required */
1391         if (flags & SWAP_LAYOUTS_MDS_HSM) {
1392                 const int buflen = sizeof(struct hsm_attrs);
1393
1394                 lu_buf_alloc(fst_hsm_buf, buflen);
1395                 lu_buf_alloc(snd_hsm_buf, buflen);
1396                 if (fst_hsm_buf->lb_buf == NULL || snd_hsm_buf->lb_buf == NULL)
1397                         GOTO(stop, rc = -ENOMEM);
1398
1399                 /* Read HSM attribute */
1400                 rc = mdo_xattr_get(env, fst_o, fst_hsm_buf, XATTR_NAME_HSM,
1401                                    BYPASS_CAPA);
1402                 if (rc < 0)
1403                         GOTO(stop, rc);
1404
1405                 rc = mdo_xattr_get(env, snd_o, snd_hsm_buf, XATTR_NAME_HSM,
1406                                    BYPASS_CAPA);
1407                 if (rc < 0)
1408                         GOTO(stop, rc);
1409
1410                 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_hsm_buf,
1411                                            XATTR_NAME_HSM, LU_XATTR_REPLACE,
1412                                            handle);
1413                 if (rc < 0)
1414                         GOTO(stop, rc);
1415
1416                 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_hsm_buf,
1417                                            XATTR_NAME_HSM, LU_XATTR_REPLACE,
1418                                            handle);
1419                 if (rc < 0)
1420                         GOTO(stop, rc);
1421         }
1422
1423         /* prepare transaction */
1424         rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
1425                                    fst_fl, handle);
1426         if (rc != 0)
1427                 GOTO(stop, rc);
1428
1429         if (fst_buf->lb_buf != NULL)
1430                 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf,
1431                                            XATTR_NAME_LOV, LU_XATTR_REPLACE,
1432                                            handle);
1433         else
1434                 rc = mdd_declare_xattr_del(env, mdd, snd_o, XATTR_NAME_LOV,
1435                                            handle);
1436         if (rc != 0)
1437                 GOTO(stop, rc);
1438
1439         rc = mdd_trans_start(env, mdd, handle);
1440         if (rc != 0)
1441                 GOTO(stop, rc);
1442
1443         if (flags & SWAP_LAYOUTS_MDS_HSM) {
1444                 rc = mdd_xattr_hsm_replace(env, fst_o, snd_hsm_buf, handle);
1445                 if (rc < 0)
1446                         GOTO(stop, rc);
1447
1448                 rc = mdd_xattr_hsm_replace(env, snd_o, fst_hsm_buf, handle);
1449                 if (rc < 0) {
1450                         rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
1451                                                     handle);
1452                         if (rc2 < 0)
1453                                 CERROR("%s: restore "DFID" HSM error: %d/%d\n",
1454                                        mdd_obj_dev_name(fst_o),
1455                                        PFID(mdo2fid(fst_o)), rc, rc2);
1456                         GOTO(stop, rc);
1457                 }
1458         }
1459
1460         rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV, fst_fl, handle,
1461                            mdd_object_capa(env, fst_o));
1462         if (rc != 0)
1463                 GOTO(stop, rc);
1464
1465         if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_SWAP_LAYOUTS))) {
1466                 rc = -EOPNOTSUPP;
1467         } else {
1468                 if (fst_buf->lb_buf != NULL)
1469                         rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
1470                                            LU_XATTR_REPLACE, handle,
1471                                            mdd_object_capa(env, snd_o));
1472                 else
1473                         rc = mdo_xattr_del(env, snd_o, XATTR_NAME_LOV, handle,
1474                                            mdd_object_capa(env, snd_o));
1475         }
1476
1477         if (rc != 0) {
1478                 int steps = 0;
1479
1480                 /* failure on second file, but first was done, so we have
1481                  * to roll back first. */
1482                 if (fst_buf->lb_buf != NULL) {
1483                         fst_lmm->lmm_oi = *saved_oi;
1484                         fst_lmm->lmm_layout_gen = cpu_to_le16(fst_gen - 1);
1485                         rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
1486                                             LU_XATTR_REPLACE, handle,
1487                                             mdd_object_capa(env, fst_o));
1488                 } else {
1489                         rc2 = mdo_xattr_del(env, fst_o, XATTR_NAME_LOV, handle,
1490                                             mdd_object_capa(env, fst_o));
1491                 }
1492                 if (rc2 < 0)
1493                         goto do_lbug;
1494
1495                 ++steps;
1496                 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf, handle);
1497                 if (rc2 < 0)
1498                         goto do_lbug;
1499
1500                 ++steps;
1501                 rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf, handle);
1502
1503         do_lbug:
1504                 if (rc2 < 0) {
1505                         /* very bad day */
1506                         CERROR("%s: unable to roll back layout swap. FIDs: "
1507                                DFID" and "DFID "error: %d/%d, steps: %d\n",
1508                                mdd_obj_dev_name(fst_o),
1509                                PFID(mdo2fid(snd_o)), PFID(mdo2fid(fst_o)),
1510                                rc, rc2, steps);
1511                         /* a solution to avoid journal commit is to panic,
1512                          * but it has strong consequences so we use LBUG to
1513                          * allow sysdamin to choose to panic or not
1514                          */
1515                         LBUG();
1516                 }
1517                 GOTO(stop, rc);
1518         }
1519
1520         /* Issue one changelog record per file */
1521         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle);
1522         if (rc)
1523                 GOTO(stop, rc);
1524
1525         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle);
1526         if (rc)
1527                 GOTO(stop, rc);
1528         EXIT;
1529
1530 stop:
1531         mdd_trans_stop(env, mdd, rc, handle);
1532         mdd_write_unlock(env, snd_o);
1533         mdd_write_unlock(env, fst_o);
1534
1535         lu_buf_free(fst_buf);
1536         lu_buf_free(snd_buf);
1537         lu_buf_free(fst_hsm_buf);
1538         lu_buf_free(snd_hsm_buf);
1539         return rc;
1540 }
1541
1542 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
1543                           struct mdd_object *child, const struct lu_attr *attr,
1544                           const struct md_op_spec *spec,
1545                           struct dt_allocation_hint *hint)
1546 {
1547         struct dt_object *np = parent ?  mdd_object_child(parent) : NULL;
1548         struct dt_object *nc = mdd_object_child(child);
1549
1550         memset(hint, 0, sizeof(*hint));
1551
1552         /* For striped directory, give striping EA to lod_ah_init, which will
1553          * decide the stripe_offset and stripe count by it. */
1554         if (S_ISDIR(attr->la_mode) &&
1555             unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1556                 hint->dah_eadata = spec->u.sp_ea.eadata;
1557                 hint->dah_eadata_len = spec->u.sp_ea.eadatalen;
1558         } else {
1559                 hint->dah_eadata = NULL;
1560                 hint->dah_eadata_len = 0;
1561         }
1562
1563         CDEBUG(D_INFO, DFID" eadata %p len %d\n", PFID(mdd_object_fid(child)),
1564                hint->dah_eadata, hint->dah_eadata_len);
1565         /* @hint will be initialized by underlying device. */
1566         nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
1567 }
1568
1569 /*
1570  * do NOT or the MAY_*'s, you'll get the weakest
1571  */
1572 int accmode(const struct lu_env *env, const struct lu_attr *la, int flags)
1573 {
1574         int res = 0;
1575
1576         /* Sadly, NFSD reopens a file repeatedly during operation, so the
1577          * "acc_mode = 0" allowance for newly-created files isn't honoured.
1578          * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1579          * owner can write to a file even if it is marked readonly to hide
1580          * its brokenness. (bug 5781) */
1581         if (flags & MDS_OPEN_OWNEROVERRIDE) {
1582                 struct lu_ucred *uc = lu_ucred_check(env);
1583
1584                 if ((uc == NULL) || (la->la_uid == uc->uc_fsuid))
1585                         return 0;
1586         }
1587
1588         if (flags & FMODE_READ)
1589                 res |= MAY_READ;
1590         if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
1591                 res |= MAY_WRITE;
1592         if (flags & MDS_FMODE_EXEC)
1593                 res = MAY_EXEC;
1594         return res;
1595 }
1596
1597 static int mdd_open_sanity_check(const struct lu_env *env,
1598                                 struct mdd_object *obj,
1599                                 const struct lu_attr *attr, int flag)
1600 {
1601         int mode, rc;
1602         ENTRY;
1603
1604         /* EEXIST check */
1605         if (mdd_is_dead_obj(obj))
1606                 RETURN(-ENOENT);
1607
1608         if (S_ISLNK(attr->la_mode))
1609                 RETURN(-ELOOP);
1610
1611         mode = accmode(env, attr, flag);
1612
1613         if (S_ISDIR(attr->la_mode) && (mode & MAY_WRITE))
1614                 RETURN(-EISDIR);
1615
1616         if (!(flag & MDS_OPEN_CREATED)) {
1617                 rc = mdd_permission_internal(env, obj, attr, mode);
1618                 if (rc)
1619                         RETURN(rc);
1620         }
1621
1622         if (S_ISFIFO(attr->la_mode) || S_ISSOCK(attr->la_mode) ||
1623             S_ISBLK(attr->la_mode) || S_ISCHR(attr->la_mode))
1624                 flag &= ~MDS_OPEN_TRUNC;
1625
1626         /* For writing append-only file must open it with append mode. */
1627         if (mdd_is_append(obj)) {
1628                 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
1629                         RETURN(-EPERM);
1630                 if (flag & MDS_OPEN_TRUNC)
1631                         RETURN(-EPERM);
1632         }
1633
1634 #if 0
1635         /*
1636          * Now, flag -- O_NOATIME does not be packed by client.
1637          */
1638         if (flag & O_NOATIME) {
1639                 struct lu_ucred *uc = lu_ucred(env);
1640
1641                 if (uc && ((uc->uc_valid == UCRED_OLD) ||
1642                            (uc->uc_valid == UCRED_NEW)) &&
1643                     (uc->uc_fsuid != attr->la_uid) &&
1644                     !md_capable(uc, CFS_CAP_FOWNER))
1645                         RETURN(-EPERM);
1646         }
1647 #endif
1648
1649         RETURN(0);
1650 }
1651
1652 static int mdd_open(const struct lu_env *env, struct md_object *obj,
1653                     int flags)
1654 {
1655         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1656         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1657         int rc = 0;
1658
1659         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1660
1661         rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1662         if (rc)
1663                 RETURN(rc);
1664
1665         rc = mdd_open_sanity_check(env, mdd_obj, attr, flags);
1666         if (rc == 0)
1667                 mdd_obj->mod_count++;
1668
1669         mdd_write_unlock(env, mdd_obj);
1670         return rc;
1671 }
1672
1673 static int mdd_declare_close(const struct lu_env *env,
1674                              struct mdd_object *obj,
1675                              struct md_attr *ma,
1676                              struct thandle *handle)
1677 {
1678         int rc;
1679
1680         rc = orph_declare_index_delete(env, obj, handle);
1681         if (rc)
1682                 return rc;
1683
1684         return mdo_declare_destroy(env, obj, handle);
1685 }
1686
1687 /*
1688  * No permission check is needed.
1689  */
1690 static int mdd_close(const struct lu_env *env, struct md_object *obj,
1691                      struct md_attr *ma, int mode)
1692 {
1693         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1694         struct mdd_device *mdd = mdo2mdd(obj);
1695         struct thandle    *handle = NULL;
1696         int rc, is_orphan = 0;
1697         ENTRY;
1698
1699         if (ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_KEEP_ORPHAN) {
1700                 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1701                 mdd_obj->mod_count--;
1702                 mdd_write_unlock(env, mdd_obj);
1703
1704                 if (mdd_obj->mod_flags & ORPHAN_OBJ && !mdd_obj->mod_count)
1705                         CDEBUG(D_HA, "Object "DFID" is retained in orphan "
1706                                "list\n", PFID(mdd_object_fid(mdd_obj)));
1707                 RETURN(0);
1708         }
1709
1710         /* mdd_finish_unlink() will always set orphan object as DEAD_OBJ, but
1711          * it might fail to add the object to orphan list (w/o ORPHAN_OBJ). */
1712         /* check without any lock */
1713         is_orphan = mdd_obj->mod_count == 1 &&
1714                     (mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0;
1715
1716 again:
1717         if (is_orphan) {
1718                 handle = mdd_trans_create(env, mdo2mdd(obj));
1719                 if (IS_ERR(handle))
1720                         RETURN(PTR_ERR(handle));
1721
1722                 rc = mdd_declare_close(env, mdd_obj, ma, handle);
1723                 if (rc)
1724                         GOTO(stop, rc);
1725
1726                 rc = mdd_declare_changelog_store(env, mdd, NULL, handle);
1727                 if (rc)
1728                         GOTO(stop, rc);
1729
1730                 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1731                 if (rc)
1732                         GOTO(stop, rc);
1733         }
1734
1735         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1736         rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
1737                         mdd_object_capa(env, mdd_obj));
1738         if (rc != 0) {
1739                 CERROR("Failed to get lu_attr of "DFID": %d\n",
1740                        PFID(mdd_object_fid(mdd_obj)), rc);
1741                 GOTO(out, rc);
1742         }
1743
1744         /* check again with lock */
1745         is_orphan = (mdd_obj->mod_count == 1) &&
1746                     ((mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0 ||
1747                      ma->ma_attr.la_nlink == 0);
1748
1749         if (is_orphan && handle == NULL) {
1750                 mdd_write_unlock(env, mdd_obj);
1751                 goto again;
1752         }
1753
1754         mdd_obj->mod_count--; /*release open count */
1755
1756         if (!is_orphan)
1757                 GOTO(out, rc = 0);
1758
1759         /* Orphan object */
1760         /* NB: Object maybe not in orphan list originally, it is rare case for
1761          * mdd_finish_unlink() failure, in that case, the object doesn't have
1762          * ORPHAN_OBJ flag */
1763         if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
1764                 /* remove link to object from orphan index */
1765                 LASSERT(handle != NULL);
1766                 rc = __mdd_orphan_del(env, mdd_obj, handle);
1767                 if (rc != 0) {
1768                         CERROR("%s: unable to delete "DFID" from orphan list: "
1769                                "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1770                                PFID(mdd_object_fid(mdd_obj)), rc);
1771                         /* If object was not deleted from orphan list, do not
1772                          * destroy OSS objects, which will be done when next
1773                          * recovery. */
1774                         GOTO(out, rc);
1775                 }
1776
1777                 CDEBUG(D_HA, "Object "DFID" is deleted from orphan "
1778                        "list, OSS objects to be destroyed.\n",
1779                        PFID(mdd_object_fid(mdd_obj)));
1780         }
1781
1782         rc = mdo_destroy(env, mdd_obj, handle);
1783
1784         if (rc != 0) {
1785                 CERROR("%s: unable to delete "DFID" from orphan list: "
1786                        "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1787                        PFID(mdd_object_fid(mdd_obj)), rc);
1788         }
1789         EXIT;
1790
1791 out:
1792         mdd_write_unlock(env, mdd_obj);
1793
1794         if (rc == 0 &&
1795             (mode & (FMODE_WRITE | MDS_OPEN_APPEND | MDS_OPEN_TRUNC)) &&
1796             !(ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_RECOV_OPEN)) {
1797                 if (handle == NULL) {
1798                         handle = mdd_trans_create(env, mdo2mdd(obj));
1799                         if (IS_ERR(handle))
1800                                 GOTO(stop, rc = PTR_ERR(handle));
1801
1802                         rc = mdd_declare_changelog_store(env, mdd, NULL,
1803                                                          handle);
1804                         if (rc)
1805                                 GOTO(stop, rc);
1806
1807                         rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1808                         if (rc)
1809                                 GOTO(stop, rc);
1810                 }
1811
1812                 mdd_changelog_data_store(env, mdd, CL_CLOSE, mode,
1813                                          mdd_obj, handle);
1814         }
1815
1816 stop:
1817         if (handle != NULL && !IS_ERR(handle))
1818                 mdd_trans_stop(env, mdd, rc, handle);
1819
1820         return rc;
1821 }
1822
1823 /*
1824  * Permission check is done when open,
1825  * no need check again.
1826  */
1827 static int mdd_readpage_sanity_check(const struct lu_env *env,
1828                                      struct mdd_object *obj)
1829 {
1830         struct dt_object *next = mdd_object_child(obj);
1831         int rc;
1832         ENTRY;
1833
1834         if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
1835                 rc = 0;
1836         else
1837                 rc = -ENOTDIR;
1838
1839         RETURN(rc);
1840 }
1841
1842 static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
1843                               int nob, const struct dt_it_ops *iops,
1844                               struct dt_it *it, __u32 attr, void *arg)
1845 {
1846         struct lu_dirpage       *dp = &lp->lp_dir;
1847         void                    *area = dp;
1848         int                      result;
1849         __u64                    hash = 0;
1850         struct lu_dirent        *ent;
1851         struct lu_dirent        *last = NULL;
1852         int                      first = 1;
1853
1854         memset(area, 0, sizeof (*dp));
1855         area += sizeof (*dp);
1856         nob  -= sizeof (*dp);
1857
1858         ent  = area;
1859         do {
1860                 int    len;
1861                 int    recsize;
1862
1863                 len = iops->key_size(env, it);
1864
1865                 /* IAM iterator can return record with zero len. */
1866                 if (len == 0)
1867                         goto next;
1868
1869                 hash = iops->store(env, it);
1870                 if (unlikely(first)) {
1871                         first = 0;
1872                         dp->ldp_hash_start = cpu_to_le64(hash);
1873                 }
1874
1875                 /* calculate max space required for lu_dirent */
1876                 recsize = lu_dirent_calc_size(len, attr);
1877
1878                 if (nob >= recsize) {
1879                         result = iops->rec(env, it, (struct dt_rec *)ent, attr);
1880                         if (result == -ESTALE)
1881                                 goto next;
1882                         if (result != 0)
1883                                 goto out;
1884
1885                         /* osd might not able to pack all attributes,
1886                          * so recheck rec length */
1887                         recsize = le16_to_cpu(ent->lde_reclen);
1888                 } else {
1889                         result = (last != NULL) ? 0 :-EINVAL;
1890                         goto out;
1891                 }
1892                 last = ent;
1893                 ent = (void *)ent + recsize;
1894                 nob -= recsize;
1895
1896 next:
1897                 result = iops->next(env, it);
1898                 if (result == -ESTALE)
1899                         goto next;
1900         } while (result == 0);
1901
1902 out:
1903         dp->ldp_hash_end = cpu_to_le64(hash);
1904         if (last != NULL) {
1905                 if (last->lde_hash == dp->ldp_hash_end)
1906                         dp->ldp_flags |= cpu_to_le32(LDF_COLLIDE);
1907                 last->lde_reclen = 0; /* end mark */
1908         }
1909         if (result > 0)
1910                 /* end of directory */
1911                 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
1912         else if (result < 0)
1913                 CWARN("build page failed: %d!\n", result);
1914         return result;
1915 }
1916
1917 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
1918                  const struct lu_rdpg *rdpg)
1919 {
1920         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1921         int rc;
1922         ENTRY;
1923
1924         if (mdd_object_exists(mdd_obj) == 0) {
1925                 CERROR("%s: object "DFID" not found: rc = -2\n",
1926                        mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
1927                 return -ENOENT;
1928         }
1929
1930         mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
1931         rc = mdd_readpage_sanity_check(env, mdd_obj);
1932         if (rc)
1933                 GOTO(out_unlock, rc);
1934
1935         if (mdd_is_dead_obj(mdd_obj)) {
1936                 struct page *pg;
1937                 struct lu_dirpage *dp;
1938
1939                 /*
1940                  * According to POSIX, please do not return any entry to client:
1941                  * even dot and dotdot should not be returned.
1942                  */
1943                 CDEBUG(D_INODE, "readdir from dead object: "DFID"\n",
1944                        PFID(mdd_object_fid(mdd_obj)));
1945
1946                 if (rdpg->rp_count <= 0)
1947                         GOTO(out_unlock, rc = -EFAULT);
1948                 LASSERT(rdpg->rp_pages != NULL);
1949
1950                 pg = rdpg->rp_pages[0];
1951                 dp = (struct lu_dirpage *)kmap(pg);
1952                 memset(dp, 0 , sizeof(struct lu_dirpage));
1953                 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
1954                 dp->ldp_hash_end   = cpu_to_le64(MDS_DIR_END_OFF);
1955                 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
1956                 kunmap(pg);
1957                 GOTO(out_unlock, rc = LU_PAGE_SIZE);
1958         }
1959
1960         rc = dt_index_walk(env, mdd_object_child(mdd_obj), rdpg,
1961                            mdd_dir_page_build, NULL);
1962         if (rc >= 0) {
1963                 struct lu_dirpage       *dp;
1964
1965                 dp = kmap(rdpg->rp_pages[0]);
1966                 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
1967                 if (rc == 0) {
1968                         /*
1969                          * No pages were processed, mark this for first page
1970                          * and send back.
1971                          */
1972                         dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
1973                         dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
1974                         rc = min_t(unsigned int, LU_PAGE_SIZE, rdpg->rp_count);
1975                 }
1976                 kunmap(rdpg->rp_pages[0]);
1977         }
1978
1979         GOTO(out_unlock, rc);
1980 out_unlock:
1981         mdd_read_unlock(env, mdd_obj);
1982         return rc;
1983 }
1984
1985 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
1986 {
1987         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1988
1989         if (mdd_object_exists(mdd_obj) == 0) {
1990                 int rc = -ENOENT;
1991
1992                 CERROR("%s: object "DFID" not found: rc = %d\n",
1993                        mdd_obj_dev_name(mdd_obj),
1994                        PFID(mdd_object_fid(mdd_obj)), rc);
1995                 return rc;
1996         }
1997         return dt_object_sync(env, mdd_object_child(mdd_obj),
1998                               0, OBD_OBJECT_EOF);
1999 }
2000
2001 static int mdd_object_lock(const struct lu_env *env,
2002                            struct md_object *obj,
2003                            struct lustre_handle *lh,
2004                            struct ldlm_enqueue_info *einfo,
2005                            ldlm_policy_data_t *policy)
2006 {
2007         struct mdd_object *mdd_obj = md2mdd_obj(obj);
2008         return dt_object_lock(env, mdd_object_child(mdd_obj), lh,
2009                               einfo, policy);
2010 }
2011
2012 static int mdd_object_unlock(const struct lu_env *env,
2013                              struct md_object *obj,
2014                              struct ldlm_enqueue_info *einfo,
2015                              ldlm_policy_data_t *policy)
2016 {
2017         struct mdd_object *mdd_obj = md2mdd_obj(obj);
2018         return dt_object_unlock(env, mdd_object_child(mdd_obj), einfo, policy);
2019 }
2020
2021 const struct md_object_operations mdd_obj_ops = {
2022         .moo_permission         = mdd_permission,
2023         .moo_attr_get           = mdd_attr_get,
2024         .moo_attr_set           = mdd_attr_set,
2025         .moo_xattr_get          = mdd_xattr_get,
2026         .moo_xattr_set          = mdd_xattr_set,
2027         .moo_xattr_list         = mdd_xattr_list,
2028         .moo_xattr_del          = mdd_xattr_del,
2029         .moo_swap_layouts       = mdd_swap_layouts,
2030         .moo_open               = mdd_open,
2031         .moo_close              = mdd_close,
2032         .moo_readpage           = mdd_readpage,
2033         .moo_readlink           = mdd_readlink,
2034         .moo_changelog          = mdd_changelog,
2035         .moo_capa_get           = mdd_capa_get,
2036         .moo_object_sync        = mdd_object_sync,
2037         .moo_object_lock        = mdd_object_lock,
2038         .moo_object_unlock      = mdd_object_unlock,
2039 };