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