Whamcloud - gitweb
LU-1996 lustre: Flexible changelog format.
[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_ucred           *uc = lu_ucred(env);
642         const struct lu_fid             *tfid;
643         struct llog_changelog_rec       *rec;
644         struct lu_buf                   *buf;
645         int                              reclen;
646         int                              rc;
647
648         /* Not recording */
649         if (!(mdd->mdd_cl.mc_flags & CLM_ON))
650                 RETURN(0);
651         if ((mdd->mdd_cl.mc_mask & (1 << type)) == 0)
652                 RETURN(0);
653
654         LASSERT(mdd_obj != NULL);
655         LASSERT(handle != NULL);
656
657         tfid = mdo2fid(mdd_obj);
658
659         if ((type >= CL_MTIME) && (type <= CL_ATIME) &&
660             cfs_time_before_64(mdd->mdd_cl.mc_starttime, mdd_obj->mod_cltime)) {
661                 /* Don't need multiple updates in this log */
662                 /* Don't check under lock - no big deal if we get an extra
663                    entry */
664                 RETURN(0);
665         }
666
667         flags = (flags & CLF_FLAGMASK) | CLF_VERSION;
668         if (uc->uc_jobid[0] != '\0')
669                 flags |= CLF_JOBID;
670
671         reclen = llog_data_len(changelog_rec_offset(flags & CLF_SUPPORTED));
672         buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf, reclen);
673         if (buf->lb_buf == NULL)
674                 RETURN(-ENOMEM);
675         rec = buf->lb_buf;
676
677         rec->cr.cr_flags = flags;
678         rec->cr.cr_type = (__u32)type;
679         rec->cr.cr_tfid = *tfid;
680         rec->cr.cr_namelen = 0;
681         mdd_obj->mod_cltime = cfs_time_current_64();
682
683         if (flags & CLF_JOBID)
684                 mdd_changelog_rec_ext_jobid(&rec->cr, uc->uc_jobid);
685
686         rc = mdd_changelog_store(env, mdd, rec, handle);
687
688         RETURN(rc);
689 }
690
691 static int mdd_changelog(const struct lu_env *env, enum changelog_rec_type type,
692                          int flags, struct md_object *obj)
693 {
694         struct thandle *handle;
695         struct mdd_object *mdd_obj = md2mdd_obj(obj);
696         struct mdd_device *mdd = mdo2mdd(obj);
697         int rc;
698         ENTRY;
699
700         handle = mdd_trans_create(env, mdd);
701         if (IS_ERR(handle))
702                 RETURN(PTR_ERR(handle));
703
704         rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
705         if (rc)
706                 GOTO(stop, rc);
707
708         rc = mdd_trans_start(env, mdd, handle);
709         if (rc)
710                 GOTO(stop, rc);
711
712         rc = mdd_changelog_data_store(env, mdd, type, flags, mdd_obj,
713                                       handle);
714
715 stop:
716         mdd_trans_stop(env, mdd, rc, handle);
717
718         RETURN(rc);
719 }
720
721 /**
722  * Save LMA extended attributes with data from \a ma.
723  *
724  * HSM and Size-On-MDS data will be extracted from \ma if they are valid, if
725  * not, LMA EA will be first read from disk, modified and write back.
726  *
727  */
728 /* Precedence for choosing record type when multiple
729  * attributes change: setattr > mtime > ctime > atime
730  * (ctime changes when mtime does, plus chmod/chown.
731  * atime and ctime are independent.) */
732 static int mdd_attr_set_changelog(const struct lu_env *env,
733                                   struct md_object *obj, struct thandle *handle,
734                                   __u64 valid)
735 {
736         struct mdd_device *mdd = mdo2mdd(obj);
737         int bits, type = 0;
738
739         bits =  (valid & LA_SIZE)  ? 1 << CL_TRUNC : 0;
740         bits |= (valid & ~(LA_CTIME|LA_MTIME|LA_ATIME)) ? 1 << CL_SETATTR : 0;
741         bits |= (valid & LA_MTIME) ? 1 << CL_MTIME : 0;
742         bits |= (valid & LA_CTIME) ? 1 << CL_CTIME : 0;
743         bits |= (valid & LA_ATIME) ? 1 << CL_ATIME : 0;
744         bits = bits & mdd->mdd_cl.mc_mask;
745         /* This is an implementation limit rather than a protocol limit */
746         CLASSERT(CL_LAST <= sizeof(int) * 8);
747         if (bits == 0)
748                 return 0;
749
750         /* The record type is the lowest non-masked set bit */
751         type = __ffs(bits);
752
753         /* FYI we only store the first CLF_FLAGMASK bits of la_valid */
754         return mdd_changelog_data_store(env, mdd, type, (int)valid,
755                                         md2mdd_obj(obj), handle);
756 }
757
758 static int mdd_declare_attr_set(const struct lu_env *env,
759                                 struct mdd_device *mdd,
760                                 struct mdd_object *obj,
761                                 const struct lu_attr *attr,
762                                 struct thandle *handle)
763 {
764         int rc;
765
766         rc = mdo_declare_attr_set(env, obj, attr, handle);
767         if (rc)
768                 return rc;
769
770 #ifdef CONFIG_FS_POSIX_ACL
771         if (attr->la_valid & LA_MODE) {
772                 mdd_read_lock(env, obj, MOR_TGT_CHILD);
773                 rc = mdo_xattr_get(env, obj, &LU_BUF_NULL,
774                                    XATTR_NAME_ACL_ACCESS, BYPASS_CAPA);
775                 mdd_read_unlock(env, obj);
776                 if (rc == -EOPNOTSUPP || rc == -ENODATA)
777                         rc = 0;
778                 else if (rc < 0)
779                         return rc;
780
781                 if (rc != 0) {
782                         struct lu_buf *buf = mdd_buf_get(env, NULL, rc);
783                         rc = mdo_declare_xattr_set(env, obj, buf,
784                                                    XATTR_NAME_ACL_ACCESS, 0,
785                                                    handle);
786                         if (rc)
787                                 return rc;
788                 }
789         }
790 #endif
791
792         rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
793         return rc;
794 }
795
796 /*
797  * LU-3671
798  *
799  * permission changes may require sync operation, to mitigate performance
800  * impact, only do this for dir and when permission is reduced.
801  *
802  * For regular files, version is updated with permission change (see VBR), async
803  * permission won't cause any issue, while missing permission change on
804  * directory may affect accessibility of other objects after recovery.
805  */
806 static inline bool permission_needs_sync(const struct lu_attr *old,
807                                          const struct lu_attr *new)
808 {
809         if (!S_ISDIR(old->la_mode))
810                 return false;
811
812         if (new->la_valid & (LA_UID | LA_GID))
813                 return true;
814
815         if (new->la_valid & LA_MODE &&
816             new->la_mode & (S_ISUID | S_ISGID | S_ISVTX))
817                 return true;
818
819         if ((new->la_valid & LA_MODE) &&
820             ((new->la_mode & old->la_mode) & S_IRWXUGO) !=
821              (old->la_mode & S_IRWXUGO))
822                 return true;
823
824         return false;
825 }
826
827 /* set attr and LOV EA at once, return updated attr */
828 int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
829                  const struct md_attr *ma)
830 {
831         struct mdd_object *mdd_obj = md2mdd_obj(obj);
832         struct mdd_device *mdd = mdo2mdd(obj);
833         struct thandle *handle;
834         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
835         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
836         const struct lu_attr *la = &ma->ma_attr;
837         int rc;
838         ENTRY;
839
840         /* we do not use ->attr_set() for LOV/SOM/HSM EA any more */
841         LASSERT((ma->ma_valid & MA_LOV) == 0);
842         LASSERT((ma->ma_valid & MA_HSM) == 0);
843         LASSERT((ma->ma_valid & MA_SOM) == 0);
844
845         rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
846         if (rc)
847                 RETURN(rc);
848
849         *la_copy = ma->ma_attr;
850         rc = mdd_fix_attr(env, mdd_obj, attr, la_copy, ma->ma_attr_flags);
851         if (rc)
852                 RETURN(rc);
853
854         /* setattr on "close" only change atime, or do nothing */
855         if (la->la_valid == LA_ATIME && la_copy->la_valid == 0)
856                 RETURN(0);
857
858         handle = mdd_trans_create(env, mdd);
859         if (IS_ERR(handle))
860                 RETURN(PTR_ERR(handle));
861
862         rc = mdd_declare_attr_set(env, mdd, mdd_obj, la, handle);
863         if (rc)
864                 GOTO(stop, rc);
865
866         rc = mdd_trans_start(env, mdd, handle);
867         if (rc)
868                 GOTO(stop, rc);
869
870         if (mdd->mdd_sync_permission && permission_needs_sync(attr, la))
871                 handle->th_sync = 1;
872
873         if (la->la_valid & (LA_MTIME | LA_CTIME))
874                 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
875                        la->la_mtime, la->la_ctime);
876
877         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
878         if (la_copy->la_valid & LA_FLAGS) {
879                 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
880                 if (rc == 0)
881                         mdd_flags_xlate(mdd_obj, la_copy->la_flags);
882         } else if (la_copy->la_valid) { /* setattr */
883                 rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 1);
884         }
885         mdd_write_unlock(env, mdd_obj);
886
887         if (rc == 0)
888                 rc = mdd_attr_set_changelog(env, obj, handle, la->la_valid);
889
890         GOTO(stop, rc);
891
892 stop:
893         mdd_trans_stop(env, mdd, rc, handle);
894         return rc;
895 }
896
897 static int mdd_xattr_sanity_check(const struct lu_env *env,
898                                   struct mdd_object *obj,
899                                   const struct lu_attr *attr)
900 {
901         struct lu_ucred *uc     = lu_ucred_assert(env);
902         ENTRY;
903
904         if (mdd_is_immutable(obj) || mdd_is_append(obj))
905                 RETURN(-EPERM);
906
907         if ((uc->uc_fsuid != attr->la_uid) && !md_capable(uc, CFS_CAP_FOWNER))
908                 RETURN(-EPERM);
909
910         RETURN(0);
911 }
912
913 static int mdd_declare_xattr_set(const struct lu_env *env,
914                                  struct mdd_device *mdd,
915                                  struct mdd_object *obj,
916                                  const struct lu_buf *buf,
917                                  const char *name,
918                                  int fl, struct thandle *handle)
919 {
920         int     rc;
921
922         rc = mdo_declare_xattr_set(env, obj, buf, name, fl, handle);
923         if (rc)
924                 return rc;
925
926         /* Only record user and layout xattr changes */
927         if (strncmp(XATTR_USER_PREFIX, name,
928                     sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
929             strcmp(XATTR_NAME_LOV, name) == 0) {
930                 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
931                 if (rc)
932                         return rc;
933         }
934
935         /* If HSM data is modified, this could add a changelog */
936         if (strcmp(XATTR_NAME_HSM, name) == 0) {
937                 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
938                 if (rc)
939                         return rc;
940         }
941
942         rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
943         return rc;
944 }
945
946 /*
947  * Compare current and future data of HSM EA and add a changelog if needed.
948  *
949  * Caller should have write-locked \param obj.
950  *
951  * \param buf - Future HSM EA content.
952  * \retval 0 if no changelog is needed or changelog was added properly.
953  * \retval -ve errno if there was a problem
954  */
955 static int mdd_hsm_update_locked(const struct lu_env *env,
956                                  struct md_object *obj,
957                                  const struct lu_buf *buf,
958                                  struct thandle *handle)
959 {
960         struct mdd_thread_info *info = mdd_env_info(env);
961         struct mdd_device      *mdd = mdo2mdd(obj);
962         struct mdd_object      *mdd_obj = md2mdd_obj(obj);
963         struct lu_buf          *current_buf;
964         struct md_hsm          *current_mh;
965         struct md_hsm          *new_mh;
966         int                     rc;
967         ENTRY;
968
969         OBD_ALLOC_PTR(current_mh);
970         if (current_mh == NULL)
971                 RETURN(-ENOMEM);
972
973         /* Read HSM attrs from disk */
974         CLASSERT(sizeof(struct hsm_attrs) <= sizeof(info->mti_xattr_buf));
975         current_buf = mdd_buf_get(env, info->mti_xattr_buf,
976                                   sizeof(info->mti_xattr_buf));
977         rc = mdo_xattr_get(env, mdd_obj, current_buf, XATTR_NAME_HSM,
978                            mdd_object_capa(env, mdd_obj));
979         rc = lustre_buf2hsm(current_buf->lb_buf, rc, current_mh);
980         if (rc < 0 && rc != -ENODATA)
981                 GOTO(free, rc);
982         else if (rc == -ENODATA)
983                 current_mh->mh_flags = 0;
984
985         /* Map future HSM xattr */
986         OBD_ALLOC_PTR(new_mh);
987         if (new_mh == NULL)
988                 GOTO(free, rc = -ENOMEM);
989         lustre_buf2hsm(buf->lb_buf, buf->lb_len, new_mh);
990
991         /* If HSM flags are different, add a changelog */
992         rc = 0;
993         if (current_mh->mh_flags != new_mh->mh_flags) {
994                 int flags = 0;
995                 hsm_set_cl_event(&flags, HE_STATE);
996                 if (new_mh->mh_flags & HS_DIRTY)
997                         hsm_set_cl_flags(&flags, CLF_HSM_DIRTY);
998
999                 rc = mdd_changelog_data_store(env, mdd, CL_HSM, flags, mdd_obj,
1000                                               handle);
1001         }
1002
1003         OBD_FREE_PTR(new_mh);
1004 free:
1005         OBD_FREE_PTR(current_mh);
1006         return(rc);
1007 }
1008
1009 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1010                          const char *name);
1011
1012 /**
1013  * The caller should guarantee to update the object ctime
1014  * after xattr_set if needed.
1015  */
1016 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
1017                          const struct lu_buf *buf, const char *name,
1018                          int fl)
1019 {
1020         struct mdd_object       *mdd_obj = md2mdd_obj(obj);
1021         struct lu_attr          *attr = MDD_ENV_VAR(env, cattr);
1022         struct mdd_device       *mdd = mdo2mdd(obj);
1023         struct thandle          *handle;
1024         int                      rc;
1025         ENTRY;
1026
1027         rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1028         if (rc)
1029                 RETURN(rc);
1030
1031         rc = mdd_xattr_sanity_check(env, mdd_obj, attr);
1032         if (rc)
1033                 RETURN(rc);
1034
1035         if (strcmp(name, XATTR_NAME_ACL_ACCESS) == 0 ||
1036             strcmp(name, XATTR_NAME_ACL_DEFAULT) == 0) {
1037                 struct posix_acl *acl;
1038
1039                 /* user may set empty ACL, which should be treated as removing
1040                  * ACL. */
1041                 acl = posix_acl_from_xattr(&init_user_ns, buf->lb_buf,
1042                                            buf->lb_len);
1043                 if (acl == NULL) {
1044                         rc = mdd_xattr_del(env, obj, name);
1045                         RETURN(rc);
1046                 }
1047                 posix_acl_release(acl);
1048         }
1049
1050         if (!strcmp(name, XATTR_NAME_ACL_ACCESS)) {
1051                 rc = mdd_acl_set(env, mdd_obj, attr, buf, fl);
1052                 RETURN(rc);
1053         }
1054
1055         handle = mdd_trans_create(env, mdd);
1056         if (IS_ERR(handle))
1057                 RETURN(PTR_ERR(handle));
1058
1059         rc = mdd_declare_xattr_set(env, mdd, mdd_obj, buf, name, fl, handle);
1060         if (rc)
1061                 GOTO(stop, rc);
1062
1063         rc = mdd_trans_start(env, mdd, handle);
1064         if (rc)
1065                 GOTO(stop, rc);
1066
1067         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1068
1069         if (strcmp(XATTR_NAME_HSM, name) == 0) {
1070                 rc = mdd_hsm_update_locked(env, obj, buf, handle);
1071                 if (rc) {
1072                         mdd_write_unlock(env, mdd_obj);
1073                         GOTO(stop, rc);
1074                 }
1075         }
1076
1077         rc = mdo_xattr_set(env, mdd_obj, buf, name, fl, handle,
1078                            mdd_object_capa(env, mdd_obj));
1079         mdd_write_unlock(env, mdd_obj);
1080         if (rc)
1081                 GOTO(stop, rc);
1082
1083         if (strcmp(XATTR_NAME_LOV, name) == 0)
1084                 rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, mdd_obj,
1085                                               handle);
1086         else if (strncmp(XATTR_USER_PREFIX, name,
1087                         sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1088             strncmp(POSIX_ACL_XATTR_ACCESS, name,
1089                         sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1090             strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1091                         sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1092                 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1093                                               handle);
1094
1095 stop:
1096         mdd_trans_stop(env, mdd, rc, handle);
1097
1098         RETURN(rc);
1099 }
1100
1101 static int mdd_declare_xattr_del(const struct lu_env *env,
1102                                  struct mdd_device *mdd,
1103                                  struct mdd_object *obj,
1104                                  const char *name,
1105                                  struct thandle *handle)
1106 {
1107         int rc;
1108
1109         rc = mdo_declare_xattr_del(env, obj, name, handle);
1110         if (rc)
1111                 return rc;
1112
1113         /* Only record user xattr changes */
1114         if ((strncmp(XATTR_USER_PREFIX, name,
1115                      sizeof(XATTR_USER_PREFIX) - 1) == 0))
1116                 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1117
1118         return rc;
1119 }
1120
1121 /**
1122  * The caller should guarantee to update the object ctime
1123  * after xattr_set if needed.
1124  */
1125 static int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
1126                          const char *name)
1127 {
1128         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1129         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1130         struct mdd_device *mdd = mdo2mdd(obj);
1131         struct thandle *handle;
1132         int  rc;
1133         ENTRY;
1134
1135         rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1136         if (rc)
1137                 RETURN(rc);
1138
1139         rc = mdd_xattr_sanity_check(env, mdd_obj, attr);
1140         if (rc)
1141                 RETURN(rc);
1142
1143         handle = mdd_trans_create(env, mdd);
1144         if (IS_ERR(handle))
1145                 RETURN(PTR_ERR(handle));
1146
1147         rc = mdd_declare_xattr_del(env, mdd, mdd_obj, name, handle);
1148         if (rc)
1149                 GOTO(stop, rc);
1150
1151         rc = mdd_trans_start(env, mdd, handle);
1152         if (rc)
1153                 GOTO(stop, rc);
1154
1155         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1156         rc = mdo_xattr_del(env, mdd_obj, name, handle,
1157                            mdd_object_capa(env, mdd_obj));
1158         mdd_write_unlock(env, mdd_obj);
1159         if (rc)
1160                 GOTO(stop, rc);
1161
1162         /* Only record system & user xattr changes */
1163         if (strncmp(XATTR_USER_PREFIX, name,
1164                                   sizeof(XATTR_USER_PREFIX) - 1) == 0 ||
1165                           strncmp(POSIX_ACL_XATTR_ACCESS, name,
1166                                   sizeof(POSIX_ACL_XATTR_ACCESS) - 1) == 0 ||
1167                           strncmp(POSIX_ACL_XATTR_DEFAULT, name,
1168                                   sizeof(POSIX_ACL_XATTR_DEFAULT) - 1) == 0)
1169                 rc = mdd_changelog_data_store(env, mdd, CL_XATTR, 0, mdd_obj,
1170                                               handle);
1171
1172 stop:
1173         mdd_trans_stop(env, mdd, rc, handle);
1174
1175         RETURN(rc);
1176 }
1177
1178 /*
1179  * read lov EA of an object
1180  * return the lov EA in an allocated lu_buf
1181  */
1182 int mdd_get_lov_ea(const struct lu_env *env, struct mdd_object *obj,
1183                    struct lu_buf *lmm_buf)
1184 {
1185         struct lu_buf   *buf = &mdd_env_info(env)->mti_big_buf;
1186         int              rc, bufsize;
1187         ENTRY;
1188
1189 repeat:
1190         rc = mdo_xattr_get(env, obj, buf, XATTR_NAME_LOV,
1191                            mdd_object_capa(env, obj));
1192
1193         if (rc == -ERANGE) {
1194                 /* mti_big_buf is allocated but is too small
1195                  * we need to increase it */
1196                 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1197                                              buf->lb_len * 2);
1198                 if (buf->lb_buf == NULL)
1199                         GOTO(out, rc = -ENOMEM);
1200                 goto repeat;
1201         }
1202
1203         if (rc < 0)
1204                 RETURN(rc);
1205
1206         if (rc == 0)
1207                 RETURN(-ENODATA);
1208
1209         bufsize = rc;
1210         if (memcmp(buf, &LU_BUF_NULL, sizeof(*buf)) == 0) {
1211                 /* mti_big_buf was not allocated, so we have to
1212                  * allocate it based on the ea size */
1213                 buf = lu_buf_check_and_alloc(&mdd_env_info(env)->mti_big_buf,
1214                                              bufsize);
1215                 if (buf->lb_buf == NULL)
1216                         GOTO(out, rc = -ENOMEM);
1217                 goto repeat;
1218         }
1219
1220         lu_buf_alloc(lmm_buf, bufsize);
1221         if (lmm_buf->lb_buf == NULL)
1222                 GOTO(out, rc = -ENOMEM);
1223
1224         memcpy(lmm_buf->lb_buf, buf->lb_buf, bufsize);
1225         rc = 0;
1226         EXIT;
1227
1228 out:
1229         if (rc < 0)
1230                 lu_buf_free(lmm_buf);
1231         return rc;
1232 }
1233
1234 static int mdd_xattr_hsm_replace(const struct lu_env *env,
1235                                  struct mdd_object *o, struct lu_buf *buf,
1236                                  struct thandle *handle)
1237 {
1238         struct hsm_attrs *attrs;
1239         __u32 hsm_flags;
1240         int flags = 0;
1241         int rc;
1242         ENTRY;
1243
1244         rc = mdo_xattr_set(env, o, buf, XATTR_NAME_HSM, LU_XATTR_REPLACE,
1245                            handle, mdd_object_capa(env, o));
1246         if (rc != 0)
1247                 RETURN(rc);
1248
1249         attrs = buf->lb_buf;
1250         hsm_flags = le32_to_cpu(attrs->hsm_flags);
1251         if (!(hsm_flags & HS_RELEASED) || mdd_is_dead_obj(o))
1252                 RETURN(0);
1253
1254         /* Add a changelog record for release. */
1255         hsm_set_cl_event(&flags, HE_RELEASE);
1256         rc = mdd_changelog_data_store(env, mdo2mdd(&o->mod_obj), CL_HSM,
1257                                       flags, o, handle);
1258         RETURN(rc);
1259 }
1260
1261 /*
1262  *  check if layout swapping between 2 objects is allowed
1263  *  the rules are:
1264  *  - only normal FIDs or non-system IGIFs
1265  *  - same type of objects
1266  *  - same owner/group (so quotas are still valid)
1267  */
1268 static int mdd_layout_swap_allowed(const struct lu_env *env,
1269                                    struct mdd_object *o1,
1270                                    const struct lu_attr *attr1,
1271                                    struct mdd_object *o2,
1272                                    const struct lu_attr *attr2)
1273 {
1274         const struct lu_fid     *fid1, *fid2;
1275         ENTRY;
1276
1277         fid1 = mdo2fid(o1);
1278         fid2 = mdo2fid(o2);
1279
1280         if (!fid_is_norm(fid1) &&
1281             (!fid_is_igif(fid1) || IS_ERR(mdd_links_get(env, o1))))
1282                 RETURN(-EBADF);
1283
1284         if (!fid_is_norm(fid2) &&
1285             (!fid_is_igif(fid2) || IS_ERR(mdd_links_get(env, o2))))
1286                 RETURN(-EBADF);
1287
1288         if (mdd_object_type(o1) != mdd_object_type(o2)) {
1289                 if (S_ISDIR(mdd_object_type(o1)))
1290                         RETURN(-ENOTDIR);
1291                 if (S_ISREG(mdd_object_type(o1)))
1292                         RETURN(-EISDIR);
1293                 RETURN(-EBADF);
1294         }
1295
1296         if ((attr1->la_uid != attr2->la_uid) ||
1297             (attr1->la_gid != attr2->la_gid))
1298                 RETURN(-EPERM);
1299
1300         RETURN(0);
1301 }
1302
1303 /**
1304  * swap layouts between 2 lustre objects
1305  */
1306 static int mdd_swap_layouts(const struct lu_env *env, struct md_object *obj1,
1307                             struct md_object *obj2, __u64 flags)
1308 {
1309         struct mdd_thread_info  *info = mdd_env_info(env);
1310         struct mdd_object       *fst_o = md2mdd_obj(obj1);
1311         struct mdd_object       *snd_o = md2mdd_obj(obj2);
1312         struct lu_attr          *fst_la = MDD_ENV_VAR(env, cattr);
1313         struct lu_attr          *snd_la = MDD_ENV_VAR(env, tattr);
1314         struct mdd_device       *mdd = mdo2mdd(obj1);
1315         struct lov_mds_md       *fst_lmm, *snd_lmm;
1316         struct lu_buf           *fst_buf = &info->mti_buf[0];
1317         struct lu_buf           *snd_buf = &info->mti_buf[1];
1318         struct lu_buf           *fst_hsm_buf = &info->mti_buf[2];
1319         struct lu_buf           *snd_hsm_buf = &info->mti_buf[3];
1320         struct ost_id           *saved_oi = NULL;
1321         struct thandle          *handle;
1322         __u16                    fst_gen, snd_gen;
1323         int                      fst_fl;
1324         int                      rc;
1325         int                      rc2;
1326         ENTRY;
1327
1328         CLASSERT(ARRAY_SIZE(info->mti_buf) >= 4);
1329         memset(info->mti_buf, 0, sizeof(info->mti_buf));
1330
1331         /* we have to sort the 2 obj, so locking will always
1332          * be in the same order, even in case of 2 concurrent swaps */
1333         rc = lu_fid_cmp(mdo2fid(fst_o), mdo2fid(snd_o));
1334         if (rc == 0) /* same fid ? */
1335                 RETURN(-EPERM);
1336
1337         if (rc < 0)
1338                 swap(fst_o, snd_o);
1339
1340         rc = mdd_la_get(env, fst_o, fst_la, BYPASS_CAPA);
1341         if (rc != 0)
1342                 RETURN(rc);
1343
1344         rc = mdd_la_get(env, snd_o, snd_la, BYPASS_CAPA);
1345         if (rc != 0)
1346                 RETURN(rc);
1347
1348         /* check if layout swapping is allowed */
1349         rc = mdd_layout_swap_allowed(env, fst_o, fst_la, snd_o, snd_la);
1350         if (rc != 0)
1351                 RETURN(rc);
1352
1353         handle = mdd_trans_create(env, mdd);
1354         if (IS_ERR(handle))
1355                 RETURN(PTR_ERR(handle));
1356
1357         /* objects are already sorted */
1358         mdd_write_lock(env, fst_o, MOR_TGT_CHILD);
1359         mdd_write_lock(env, snd_o, MOR_TGT_CHILD);
1360
1361         rc = mdd_get_lov_ea(env, fst_o, fst_buf);
1362         if (rc < 0 && rc != -ENODATA)
1363                 GOTO(stop, rc);
1364
1365         rc = mdd_get_lov_ea(env, snd_o, snd_buf);
1366         if (rc < 0 && rc != -ENODATA)
1367                 GOTO(stop, rc);
1368
1369         /* swapping 2 non existant layouts is a success */
1370         if (fst_buf->lb_buf == NULL && snd_buf->lb_buf == NULL)
1371                 GOTO(stop, rc = 0);
1372
1373         /* to help inode migration between MDT, it is better to
1374          * start by the no layout file (if one), so we order the swap */
1375         if (snd_buf->lb_buf == NULL) {
1376                 swap(fst_o, snd_o);
1377                 swap(fst_buf, snd_buf);
1378         }
1379
1380         /* lmm and generation layout initialization */
1381         if (fst_buf->lb_buf != NULL) {
1382                 fst_lmm = fst_buf->lb_buf;
1383                 fst_gen = le16_to_cpu(fst_lmm->lmm_layout_gen);
1384                 fst_fl  = LU_XATTR_REPLACE;
1385         } else {
1386                 fst_lmm = NULL;
1387                 fst_gen = 0;
1388                 fst_fl  = LU_XATTR_CREATE;
1389         }
1390
1391         snd_lmm = snd_buf->lb_buf;
1392         snd_gen = le16_to_cpu(snd_lmm->lmm_layout_gen);
1393
1394         /* increase the generation layout numbers */
1395         snd_gen++;
1396         fst_gen++;
1397
1398         /* set the file specific informations in lmm */
1399         if (fst_lmm != NULL) {
1400                 saved_oi = &info->mti_oa.o_oi;
1401
1402                 *saved_oi = fst_lmm->lmm_oi;
1403                 fst_lmm->lmm_layout_gen = cpu_to_le16(snd_gen);
1404                 fst_lmm->lmm_oi = snd_lmm->lmm_oi;
1405                 snd_lmm->lmm_oi = *saved_oi;
1406         } else {
1407                 if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V1))
1408                         snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1_DEF);
1409                 else if (snd_lmm->lmm_magic == cpu_to_le32(LOV_MAGIC_V3))
1410                         snd_lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V3_DEF);
1411                 else
1412                         GOTO(stop, rc = -EPROTO);
1413         }
1414         snd_lmm->lmm_layout_gen = cpu_to_le16(fst_gen);
1415
1416         /* Prepare HSM attribute if it's required */
1417         if (flags & SWAP_LAYOUTS_MDS_HSM) {
1418                 const int buflen = sizeof(struct hsm_attrs);
1419
1420                 lu_buf_alloc(fst_hsm_buf, buflen);
1421                 lu_buf_alloc(snd_hsm_buf, buflen);
1422                 if (fst_hsm_buf->lb_buf == NULL || snd_hsm_buf->lb_buf == NULL)
1423                         GOTO(stop, rc = -ENOMEM);
1424
1425                 /* Read HSM attribute */
1426                 rc = mdo_xattr_get(env, fst_o, fst_hsm_buf, XATTR_NAME_HSM,
1427                                    BYPASS_CAPA);
1428                 if (rc < 0)
1429                         GOTO(stop, rc);
1430
1431                 rc = mdo_xattr_get(env, snd_o, snd_hsm_buf, XATTR_NAME_HSM,
1432                                    BYPASS_CAPA);
1433                 if (rc < 0)
1434                         GOTO(stop, rc);
1435
1436                 rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_hsm_buf,
1437                                            XATTR_NAME_HSM, LU_XATTR_REPLACE,
1438                                            handle);
1439                 if (rc < 0)
1440                         GOTO(stop, rc);
1441
1442                 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_hsm_buf,
1443                                            XATTR_NAME_HSM, LU_XATTR_REPLACE,
1444                                            handle);
1445                 if (rc < 0)
1446                         GOTO(stop, rc);
1447         }
1448
1449         /* prepare transaction */
1450         rc = mdd_declare_xattr_set(env, mdd, fst_o, snd_buf, XATTR_NAME_LOV,
1451                                    fst_fl, handle);
1452         if (rc != 0)
1453                 GOTO(stop, rc);
1454
1455         if (fst_buf->lb_buf != NULL)
1456                 rc = mdd_declare_xattr_set(env, mdd, snd_o, fst_buf,
1457                                            XATTR_NAME_LOV, LU_XATTR_REPLACE,
1458                                            handle);
1459         else
1460                 rc = mdd_declare_xattr_del(env, mdd, snd_o, XATTR_NAME_LOV,
1461                                            handle);
1462         if (rc != 0)
1463                 GOTO(stop, rc);
1464
1465         rc = mdd_trans_start(env, mdd, handle);
1466         if (rc != 0)
1467                 GOTO(stop, rc);
1468
1469         if (flags & SWAP_LAYOUTS_MDS_HSM) {
1470                 rc = mdd_xattr_hsm_replace(env, fst_o, snd_hsm_buf, handle);
1471                 if (rc < 0)
1472                         GOTO(stop, rc);
1473
1474                 rc = mdd_xattr_hsm_replace(env, snd_o, fst_hsm_buf, handle);
1475                 if (rc < 0) {
1476                         rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf,
1477                                                     handle);
1478                         if (rc2 < 0)
1479                                 CERROR("%s: restore "DFID" HSM error: %d/%d\n",
1480                                        mdd_obj_dev_name(fst_o),
1481                                        PFID(mdo2fid(fst_o)), rc, rc2);
1482                         GOTO(stop, rc);
1483                 }
1484         }
1485
1486         rc = mdo_xattr_set(env, fst_o, snd_buf, XATTR_NAME_LOV, fst_fl, handle,
1487                            mdd_object_capa(env, fst_o));
1488         if (rc != 0)
1489                 GOTO(stop, rc);
1490
1491         if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_MDS_HSM_SWAP_LAYOUTS))) {
1492                 rc = -EOPNOTSUPP;
1493         } else {
1494                 if (fst_buf->lb_buf != NULL)
1495                         rc = mdo_xattr_set(env, snd_o, fst_buf, XATTR_NAME_LOV,
1496                                            LU_XATTR_REPLACE, handle,
1497                                            mdd_object_capa(env, snd_o));
1498                 else
1499                         rc = mdo_xattr_del(env, snd_o, XATTR_NAME_LOV, handle,
1500                                            mdd_object_capa(env, snd_o));
1501         }
1502
1503         if (rc != 0) {
1504                 int steps = 0;
1505
1506                 /* failure on second file, but first was done, so we have
1507                  * to roll back first. */
1508                 if (fst_buf->lb_buf != NULL) {
1509                         fst_lmm->lmm_oi = *saved_oi;
1510                         fst_lmm->lmm_layout_gen = cpu_to_le16(fst_gen - 1);
1511                         rc2 = mdo_xattr_set(env, fst_o, fst_buf, XATTR_NAME_LOV,
1512                                             LU_XATTR_REPLACE, handle,
1513                                             mdd_object_capa(env, fst_o));
1514                 } else {
1515                         rc2 = mdo_xattr_del(env, fst_o, XATTR_NAME_LOV, handle,
1516                                             mdd_object_capa(env, fst_o));
1517                 }
1518                 if (rc2 < 0)
1519                         goto do_lbug;
1520
1521                 ++steps;
1522                 rc2 = mdd_xattr_hsm_replace(env, fst_o, fst_hsm_buf, handle);
1523                 if (rc2 < 0)
1524                         goto do_lbug;
1525
1526                 ++steps;
1527                 rc2 = mdd_xattr_hsm_replace(env, snd_o, snd_hsm_buf, handle);
1528
1529         do_lbug:
1530                 if (rc2 < 0) {
1531                         /* very bad day */
1532                         CERROR("%s: unable to roll back layout swap. FIDs: "
1533                                DFID" and "DFID "error: %d/%d, steps: %d\n",
1534                                mdd_obj_dev_name(fst_o),
1535                                PFID(mdo2fid(snd_o)), PFID(mdo2fid(fst_o)),
1536                                rc, rc2, steps);
1537                         /* a solution to avoid journal commit is to panic,
1538                          * but it has strong consequences so we use LBUG to
1539                          * allow sysdamin to choose to panic or not
1540                          */
1541                         LBUG();
1542                 }
1543                 GOTO(stop, rc);
1544         }
1545
1546         /* Issue one changelog record per file */
1547         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, fst_o, handle);
1548         if (rc)
1549                 GOTO(stop, rc);
1550
1551         rc = mdd_changelog_data_store(env, mdd, CL_LAYOUT, 0, snd_o, handle);
1552         if (rc)
1553                 GOTO(stop, rc);
1554         EXIT;
1555
1556 stop:
1557         mdd_trans_stop(env, mdd, rc, handle);
1558         mdd_write_unlock(env, snd_o);
1559         mdd_write_unlock(env, fst_o);
1560
1561         lu_buf_free(fst_buf);
1562         lu_buf_free(snd_buf);
1563         lu_buf_free(fst_hsm_buf);
1564         lu_buf_free(snd_hsm_buf);
1565         return rc;
1566 }
1567
1568 void mdd_object_make_hint(const struct lu_env *env, struct mdd_object *parent,
1569                           struct mdd_object *child, const struct lu_attr *attr,
1570                           const struct md_op_spec *spec,
1571                           struct dt_allocation_hint *hint)
1572 {
1573         struct dt_object *np = parent ?  mdd_object_child(parent) : NULL;
1574         struct dt_object *nc = mdd_object_child(child);
1575
1576         memset(hint, 0, sizeof(*hint));
1577
1578         /* For striped directory, give striping EA to lod_ah_init, which will
1579          * decide the stripe_offset and stripe count by it. */
1580         if (S_ISDIR(attr->la_mode) &&
1581             unlikely(spec != NULL && spec->sp_cr_flags & MDS_OPEN_HAS_EA)) {
1582                 hint->dah_eadata = spec->u.sp_ea.eadata;
1583                 hint->dah_eadata_len = spec->u.sp_ea.eadatalen;
1584         } else {
1585                 hint->dah_eadata = NULL;
1586                 hint->dah_eadata_len = 0;
1587         }
1588
1589         CDEBUG(D_INFO, DFID" eadata %p len %d\n", PFID(mdd_object_fid(child)),
1590                hint->dah_eadata, hint->dah_eadata_len);
1591         /* @hint will be initialized by underlying device. */
1592         nc->do_ops->do_ah_init(env, hint, np, nc, attr->la_mode & S_IFMT);
1593 }
1594
1595 /*
1596  * do NOT or the MAY_*'s, you'll get the weakest
1597  */
1598 int accmode(const struct lu_env *env, const struct lu_attr *la, int flags)
1599 {
1600         int res = 0;
1601
1602         /* Sadly, NFSD reopens a file repeatedly during operation, so the
1603          * "acc_mode = 0" allowance for newly-created files isn't honoured.
1604          * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1605          * owner can write to a file even if it is marked readonly to hide
1606          * its brokenness. (bug 5781) */
1607         if (flags & MDS_OPEN_OWNEROVERRIDE) {
1608                 struct lu_ucred *uc = lu_ucred_check(env);
1609
1610                 if ((uc == NULL) || (la->la_uid == uc->uc_fsuid))
1611                         return 0;
1612         }
1613
1614         if (flags & FMODE_READ)
1615                 res |= MAY_READ;
1616         if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
1617                 res |= MAY_WRITE;
1618         if (flags & MDS_FMODE_EXEC)
1619                 res = MAY_EXEC;
1620         return res;
1621 }
1622
1623 static int mdd_open_sanity_check(const struct lu_env *env,
1624                                 struct mdd_object *obj,
1625                                 const struct lu_attr *attr, int flag)
1626 {
1627         int mode, rc;
1628         ENTRY;
1629
1630         /* EEXIST check */
1631         if (mdd_is_dead_obj(obj))
1632                 RETURN(-ENOENT);
1633
1634         if (S_ISLNK(attr->la_mode))
1635                 RETURN(-ELOOP);
1636
1637         mode = accmode(env, attr, flag);
1638
1639         if (S_ISDIR(attr->la_mode) && (mode & MAY_WRITE))
1640                 RETURN(-EISDIR);
1641
1642         if (!(flag & MDS_OPEN_CREATED)) {
1643                 rc = mdd_permission_internal(env, obj, attr, mode);
1644                 if (rc)
1645                         RETURN(rc);
1646         }
1647
1648         if (S_ISFIFO(attr->la_mode) || S_ISSOCK(attr->la_mode) ||
1649             S_ISBLK(attr->la_mode) || S_ISCHR(attr->la_mode))
1650                 flag &= ~MDS_OPEN_TRUNC;
1651
1652         /* For writing append-only file must open it with append mode. */
1653         if (mdd_is_append(obj)) {
1654                 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
1655                         RETURN(-EPERM);
1656                 if (flag & MDS_OPEN_TRUNC)
1657                         RETURN(-EPERM);
1658         }
1659
1660 #if 0
1661         /*
1662          * Now, flag -- O_NOATIME does not be packed by client.
1663          */
1664         if (flag & O_NOATIME) {
1665                 struct lu_ucred *uc = lu_ucred(env);
1666
1667                 if (uc && ((uc->uc_valid == UCRED_OLD) ||
1668                            (uc->uc_valid == UCRED_NEW)) &&
1669                     (uc->uc_fsuid != attr->la_uid) &&
1670                     !md_capable(uc, CFS_CAP_FOWNER))
1671                         RETURN(-EPERM);
1672         }
1673 #endif
1674
1675         RETURN(0);
1676 }
1677
1678 static int mdd_open(const struct lu_env *env, struct md_object *obj,
1679                     int flags)
1680 {
1681         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1682         struct lu_attr *attr = MDD_ENV_VAR(env, cattr);
1683         int rc = 0;
1684
1685         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1686
1687         rc = mdd_la_get(env, mdd_obj, attr, BYPASS_CAPA);
1688         if (rc != 0)
1689                 GOTO(out, rc);
1690
1691         rc = mdd_open_sanity_check(env, mdd_obj, attr, flags);
1692         if (rc != 0)
1693                 GOTO(out, rc);
1694
1695         mdd_obj->mod_count++;
1696         EXIT;
1697 out:
1698         mdd_write_unlock(env, mdd_obj);
1699         return rc;
1700 }
1701
1702 static int mdd_declare_close(const struct lu_env *env,
1703                              struct mdd_object *obj,
1704                              struct md_attr *ma,
1705                              struct thandle *handle)
1706 {
1707         int rc;
1708
1709         rc = orph_declare_index_delete(env, obj, handle);
1710         if (rc)
1711                 return rc;
1712
1713         return mdo_declare_destroy(env, obj, handle);
1714 }
1715
1716 /*
1717  * No permission check is needed.
1718  */
1719 static int mdd_close(const struct lu_env *env, struct md_object *obj,
1720                      struct md_attr *ma, int mode)
1721 {
1722         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1723         struct mdd_device *mdd = mdo2mdd(obj);
1724         struct thandle    *handle = NULL;
1725         int rc, is_orphan = 0;
1726         ENTRY;
1727
1728         if (ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_KEEP_ORPHAN) {
1729                 mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1730                 mdd_obj->mod_count--;
1731                 mdd_write_unlock(env, mdd_obj);
1732
1733                 if (mdd_obj->mod_flags & ORPHAN_OBJ && !mdd_obj->mod_count)
1734                         CDEBUG(D_HA, "Object "DFID" is retained in orphan "
1735                                "list\n", PFID(mdd_object_fid(mdd_obj)));
1736                 RETURN(0);
1737         }
1738
1739         /* mdd_finish_unlink() will always set orphan object as DEAD_OBJ, but
1740          * it might fail to add the object to orphan list (w/o ORPHAN_OBJ). */
1741         /* check without any lock */
1742         is_orphan = mdd_obj->mod_count == 1 &&
1743                     (mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0;
1744
1745 again:
1746         if (is_orphan) {
1747                 handle = mdd_trans_create(env, mdo2mdd(obj));
1748                 if (IS_ERR(handle))
1749                         RETURN(PTR_ERR(handle));
1750
1751                 rc = mdd_declare_close(env, mdd_obj, ma, handle);
1752                 if (rc)
1753                         GOTO(stop, rc);
1754
1755                 rc = mdd_declare_changelog_store(env, mdd, NULL, NULL, handle);
1756                 if (rc)
1757                         GOTO(stop, rc);
1758
1759                 rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1760                 if (rc)
1761                         GOTO(stop, rc);
1762         }
1763
1764         mdd_write_lock(env, mdd_obj, MOR_TGT_CHILD);
1765         rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
1766                         mdd_object_capa(env, mdd_obj));
1767         if (rc != 0) {
1768                 CERROR("Failed to get lu_attr of "DFID": %d\n",
1769                        PFID(mdd_object_fid(mdd_obj)), rc);
1770                 GOTO(out, rc);
1771         }
1772
1773         /* check again with lock */
1774         is_orphan = (mdd_obj->mod_count == 1) &&
1775                     ((mdd_obj->mod_flags & (ORPHAN_OBJ | DEAD_OBJ)) != 0 ||
1776                      ma->ma_attr.la_nlink == 0);
1777
1778         if (is_orphan && handle == NULL) {
1779                 mdd_write_unlock(env, mdd_obj);
1780                 goto again;
1781         }
1782
1783         mdd_obj->mod_count--; /*release open count */
1784
1785         if (!is_orphan)
1786                 GOTO(out, rc = 0);
1787
1788         /* Orphan object */
1789         /* NB: Object maybe not in orphan list originally, it is rare case for
1790          * mdd_finish_unlink() failure, in that case, the object doesn't have
1791          * ORPHAN_OBJ flag */
1792         if ((mdd_obj->mod_flags & ORPHAN_OBJ) != 0) {
1793                 /* remove link to object from orphan index */
1794                 LASSERT(handle != NULL);
1795                 rc = __mdd_orphan_del(env, mdd_obj, handle);
1796                 if (rc != 0) {
1797                         CERROR("%s: unable to delete "DFID" from orphan list: "
1798                                "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1799                                PFID(mdd_object_fid(mdd_obj)), rc);
1800                         /* If object was not deleted from orphan list, do not
1801                          * destroy OSS objects, which will be done when next
1802                          * recovery. */
1803                         GOTO(out, rc);
1804                 }
1805
1806                 CDEBUG(D_HA, "Object "DFID" is deleted from orphan "
1807                        "list, OSS objects to be destroyed.\n",
1808                        PFID(mdd_object_fid(mdd_obj)));
1809         }
1810
1811         rc = mdo_destroy(env, mdd_obj, handle);
1812
1813         if (rc != 0) {
1814                 CERROR("%s: unable to delete "DFID" from orphan list: "
1815                        "rc = %d\n", lu_dev_name(mdd2lu_dev(mdd)),
1816                        PFID(mdd_object_fid(mdd_obj)), rc);
1817         }
1818         EXIT;
1819
1820 out:
1821         mdd_write_unlock(env, mdd_obj);
1822
1823         if (rc == 0 &&
1824             (mode & (FMODE_WRITE | MDS_OPEN_APPEND | MDS_OPEN_TRUNC)) &&
1825             !(ma->ma_valid & MA_FLAGS && ma->ma_attr_flags & MDS_RECOV_OPEN)) {
1826                 if (handle == NULL) {
1827                         handle = mdd_trans_create(env, mdo2mdd(obj));
1828                         if (IS_ERR(handle))
1829                                 GOTO(stop, rc = PTR_ERR(handle));
1830
1831                         rc = mdd_declare_changelog_store(env, mdd, NULL, NULL,
1832                                                          handle);
1833                         if (rc)
1834                                 GOTO(stop, rc);
1835
1836                         rc = mdd_trans_start(env, mdo2mdd(obj), handle);
1837                         if (rc)
1838                                 GOTO(stop, rc);
1839                 }
1840
1841                 mdd_changelog_data_store(env, mdd, CL_CLOSE, mode,
1842                                          mdd_obj, handle);
1843         }
1844
1845 stop:
1846         if (handle != NULL && !IS_ERR(handle))
1847                 mdd_trans_stop(env, mdd, rc, handle);
1848
1849         return rc;
1850 }
1851
1852 /*
1853  * Permission check is done when open,
1854  * no need check again.
1855  */
1856 static int mdd_readpage_sanity_check(const struct lu_env *env,
1857                                      struct mdd_object *obj)
1858 {
1859         struct dt_object *next = mdd_object_child(obj);
1860         int rc;
1861         ENTRY;
1862
1863         if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
1864                 rc = 0;
1865         else
1866                 rc = -ENOTDIR;
1867
1868         RETURN(rc);
1869 }
1870
1871 static int mdd_dir_page_build(const struct lu_env *env, union lu_page *lp,
1872                               int nob, const struct dt_it_ops *iops,
1873                               struct dt_it *it, __u32 attr, void *arg)
1874 {
1875         struct lu_dirpage       *dp = &lp->lp_dir;
1876         void                    *area = dp;
1877         int                      result;
1878         __u64                    hash = 0;
1879         struct lu_dirent        *ent;
1880         struct lu_dirent        *last = NULL;
1881         struct lu_fid            fid;
1882         int                      first = 1;
1883
1884         memset(area, 0, sizeof (*dp));
1885         area += sizeof (*dp);
1886         nob  -= sizeof (*dp);
1887
1888         ent  = area;
1889         do {
1890                 int    len;
1891                 int    recsize;
1892
1893                 len = iops->key_size(env, it);
1894
1895                 /* IAM iterator can return record with zero len. */
1896                 if (len == 0)
1897                         goto next;
1898
1899                 hash = iops->store(env, it);
1900                 if (unlikely(first)) {
1901                         first = 0;
1902                         dp->ldp_hash_start = cpu_to_le64(hash);
1903                 }
1904
1905                 /* calculate max space required for lu_dirent */
1906                 recsize = lu_dirent_calc_size(len, attr);
1907
1908                 if (nob >= recsize) {
1909                         result = iops->rec(env, it, (struct dt_rec *)ent, attr);
1910                         if (result == -ESTALE)
1911                                 goto next;
1912                         if (result != 0)
1913                                 goto out;
1914
1915                         /* osd might not able to pack all attributes,
1916                          * so recheck rec length */
1917                         recsize = le16_to_cpu(ent->lde_reclen);
1918
1919                         if (le32_to_cpu(ent->lde_attrs) & LUDA_FID) {
1920                                 fid_le_to_cpu(&fid, &ent->lde_fid);
1921                                 if (fid_is_dot_lustre(&fid))
1922                                         goto next;
1923                         }
1924                 } else {
1925                         result = (last != NULL) ? 0 :-EINVAL;
1926                         goto out;
1927                 }
1928                 last = ent;
1929                 ent = (void *)ent + recsize;
1930                 nob -= recsize;
1931
1932 next:
1933                 result = iops->next(env, it);
1934                 if (result == -ESTALE)
1935                         goto next;
1936         } while (result == 0);
1937
1938 out:
1939         dp->ldp_hash_end = cpu_to_le64(hash);
1940         if (last != NULL) {
1941                 if (last->lde_hash == dp->ldp_hash_end)
1942                         dp->ldp_flags |= cpu_to_le32(LDF_COLLIDE);
1943                 last->lde_reclen = 0; /* end mark */
1944         }
1945         if (result > 0)
1946                 /* end of directory */
1947                 dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
1948         else if (result < 0)
1949                 CWARN("build page failed: %d!\n", result);
1950         return result;
1951 }
1952
1953 int mdd_readpage(const struct lu_env *env, struct md_object *obj,
1954                  const struct lu_rdpg *rdpg)
1955 {
1956         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1957         int rc;
1958         ENTRY;
1959
1960         if (mdd_object_exists(mdd_obj) == 0) {
1961                 CERROR("%s: object "DFID" not found: rc = -2\n",
1962                        mdd_obj_dev_name(mdd_obj),PFID(mdd_object_fid(mdd_obj)));
1963                 return -ENOENT;
1964         }
1965
1966         mdd_read_lock(env, mdd_obj, MOR_TGT_CHILD);
1967         rc = mdd_readpage_sanity_check(env, mdd_obj);
1968         if (rc)
1969                 GOTO(out_unlock, rc);
1970
1971         if (mdd_is_dead_obj(mdd_obj)) {
1972                 struct page *pg;
1973                 struct lu_dirpage *dp;
1974
1975                 /*
1976                  * According to POSIX, please do not return any entry to client:
1977                  * even dot and dotdot should not be returned.
1978                  */
1979                 CDEBUG(D_INODE, "readdir from dead object: "DFID"\n",
1980                        PFID(mdd_object_fid(mdd_obj)));
1981
1982                 if (rdpg->rp_count <= 0)
1983                         GOTO(out_unlock, rc = -EFAULT);
1984                 LASSERT(rdpg->rp_pages != NULL);
1985
1986                 pg = rdpg->rp_pages[0];
1987                 dp = (struct lu_dirpage *)kmap(pg);
1988                 memset(dp, 0 , sizeof(struct lu_dirpage));
1989                 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
1990                 dp->ldp_hash_end   = cpu_to_le64(MDS_DIR_END_OFF);
1991                 dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
1992                 kunmap(pg);
1993                 GOTO(out_unlock, rc = LU_PAGE_SIZE);
1994         }
1995
1996         rc = dt_index_walk(env, mdd_object_child(mdd_obj), rdpg,
1997                            mdd_dir_page_build, NULL);
1998         if (rc >= 0) {
1999                 struct lu_dirpage       *dp;
2000
2001                 dp = kmap(rdpg->rp_pages[0]);
2002                 dp->ldp_hash_start = cpu_to_le64(rdpg->rp_hash);
2003                 if (rc == 0) {
2004                         /*
2005                          * No pages were processed, mark this for first page
2006                          * and send back.
2007                          */
2008                         dp->ldp_hash_end = cpu_to_le64(MDS_DIR_END_OFF);
2009                         dp->ldp_flags = cpu_to_le32(LDF_EMPTY);
2010                         rc = min_t(unsigned int, LU_PAGE_SIZE, rdpg->rp_count);
2011                 }
2012                 kunmap(rdpg->rp_pages[0]);
2013         }
2014
2015         GOTO(out_unlock, rc);
2016 out_unlock:
2017         mdd_read_unlock(env, mdd_obj);
2018         return rc;
2019 }
2020
2021 static int mdd_object_sync(const struct lu_env *env, struct md_object *obj)
2022 {
2023         struct mdd_object *mdd_obj = md2mdd_obj(obj);
2024
2025         if (mdd_object_exists(mdd_obj) == 0) {
2026                 int rc = -ENOENT;
2027
2028                 CERROR("%s: object "DFID" not found: rc = %d\n",
2029                        mdd_obj_dev_name(mdd_obj),
2030                        PFID(mdd_object_fid(mdd_obj)), rc);
2031                 return rc;
2032         }
2033         return dt_object_sync(env, mdd_object_child(mdd_obj),
2034                               0, OBD_OBJECT_EOF);
2035 }
2036
2037 static int mdd_object_lock(const struct lu_env *env,
2038                            struct md_object *obj,
2039                            struct lustre_handle *lh,
2040                            struct ldlm_enqueue_info *einfo,
2041                            ldlm_policy_data_t *policy)
2042 {
2043         struct mdd_object *mdd_obj = md2mdd_obj(obj);
2044         return dt_object_lock(env, mdd_object_child(mdd_obj), lh,
2045                               einfo, policy);
2046 }
2047
2048 static int mdd_object_unlock(const struct lu_env *env,
2049                              struct md_object *obj,
2050                              struct ldlm_enqueue_info *einfo,
2051                              ldlm_policy_data_t *policy)
2052 {
2053         struct mdd_object *mdd_obj = md2mdd_obj(obj);
2054         return dt_object_unlock(env, mdd_object_child(mdd_obj), einfo, policy);
2055 }
2056
2057 const struct md_object_operations mdd_obj_ops = {
2058         .moo_permission         = mdd_permission,
2059         .moo_attr_get           = mdd_attr_get,
2060         .moo_attr_set           = mdd_attr_set,
2061         .moo_xattr_get          = mdd_xattr_get,
2062         .moo_xattr_set          = mdd_xattr_set,
2063         .moo_xattr_list         = mdd_xattr_list,
2064         .moo_xattr_del          = mdd_xattr_del,
2065         .moo_swap_layouts       = mdd_swap_layouts,
2066         .moo_open               = mdd_open,
2067         .moo_close              = mdd_close,
2068         .moo_readpage           = mdd_readpage,
2069         .moo_readlink           = mdd_readlink,
2070         .moo_changelog          = mdd_changelog,
2071         .moo_capa_get           = mdd_capa_get,
2072         .moo_object_sync        = mdd_object_sync,
2073         .moo_object_lock        = mdd_object_lock,
2074         .moo_object_unlock      = mdd_object_unlock,
2075 };