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