Whamcloud - gitweb
be1235849f296b8a596d71b27ea9ea068983dcb1
[fs/lustre-release.git] / lustre / mdd / mdd_object.c
1 /* -*- MODE: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  mdd/mdd_handler.c
5  *  Lustre Metadata Server (mdd) routines
6  *
7  *  Copyright (C) 2006 Cluster File Systems, Inc.
8  *   Author: Wang Di <wangdi@clusterfs.com>
9  *
10  *   This file is part of the Lustre file system, http://www.lustre.org
11  *   Lustre is a trademark of Cluster File Systems, Inc.
12  *
13  *   You may have signed or agreed to another license before downloading
14  *   this software.  If so, you are bound by the terms and conditions
15  *   of that agreement, and the following does not apply to you.  See the
16  *   LICENSE file included with this distribution for more information.
17  *
18  *   If you did not agree to a different license, then this copy of Lustre
19  *   is open source software; you can redistribute it and/or modify it
20  *   under the terms of version 2 of the GNU General Public License as
21  *   published by the Free Software Foundation.
22  *
23  *   In either case, Lustre is distributed in the hope that it will be
24  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
25  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  *   license text for more details.
27  */
28 #ifndef EXPORT_SYMTAB
29 # define EXPORT_SYMTAB
30 #endif
31 #define DEBUG_SUBSYSTEM S_MDS
32
33 #include <linux/module.h>
34 #include <linux/jbd.h>
35 #include <obd.h>
36 #include <obd_class.h>
37 #include <obd_support.h>
38 #include <lprocfs_status.h>
39 /* fid_be_cpu(), fid_cpu_to_be(). */
40 #include <lustre_fid.h>
41
42 #include <linux/ldiskfs_fs.h>
43 #include <lustre_mds.h>
44 #include <lustre/lustre_idl.h>
45
46 #include "mdd_internal.h"
47
48 static struct lu_object_operations mdd_lu_obj_ops;
49
50 int mdd_la_get(const struct lu_env *env, struct mdd_object *obj,
51                struct lu_attr *la, struct lustre_capa *capa)
52 {
53         LASSERTF(mdd_object_exists(obj), "FID is "DFID"\n",
54                  PFID(mdd_object_fid(obj)));
55         return mdo_attr_get(env, obj, la, capa);
56 }
57
58 static void mdd_flags_xlate(struct mdd_object *obj, __u32 flags)
59 {
60         obj->mod_flags &= ~(APPEND_OBJ|IMMUTE_OBJ);
61
62         if (flags & LUSTRE_APPEND_FL)
63                 obj->mod_flags |= APPEND_OBJ;
64
65         if (flags & LUSTRE_IMMUTABLE_FL)
66                 obj->mod_flags |= IMMUTE_OBJ;
67 }
68
69 struct lu_buf *mdd_buf_get(const struct lu_env *env, void *area, ssize_t len)
70 {
71         struct lu_buf *buf;
72
73         buf = &mdd_env_info(env)->mti_buf;
74         buf->lb_buf = area;
75         buf->lb_len = len;
76         return buf;
77 }
78
79 struct llog_cookie *mdd_max_cookie_get(const struct lu_env *env,
80                                        struct mdd_device *mdd)
81 {
82         struct mdd_thread_info *mti = mdd_env_info(env);
83         int                     max_cookie_size;
84
85         max_cookie_size = mdd_lov_cookiesize(env, mdd);
86         if (unlikely(mti->mti_max_cookie_size < max_cookie_size)) {
87                 if (mti->mti_max_cookie)
88                         OBD_FREE(mti->mti_max_cookie, mti->mti_max_cookie_size);
89                 mti->mti_max_cookie = NULL;
90                 mti->mti_max_cookie_size = 0;
91         }
92         if (unlikely(mti->mti_max_cookie == NULL)) {
93                 OBD_ALLOC(mti->mti_max_cookie, max_cookie_size);
94                 if (unlikely(mti->mti_max_cookie != NULL))
95                         mti->mti_max_cookie_size = max_cookie_size;
96         }
97         return mti->mti_max_cookie;
98 }
99
100 struct lov_mds_md *mdd_max_lmm_get(const struct lu_env *env,
101                                    struct mdd_device *mdd)
102 {
103         struct mdd_thread_info *mti = mdd_env_info(env);
104         int                     max_lmm_size;
105
106         max_lmm_size = mdd_lov_mdsize(env, mdd);
107         if (unlikely(mti->mti_max_lmm_size < max_lmm_size)) {
108                 if (mti->mti_max_lmm)
109                         OBD_FREE(mti->mti_max_lmm, mti->mti_max_lmm_size);
110                 mti->mti_max_lmm = NULL;
111                 mti->mti_max_lmm_size = 0;
112         }
113         if (unlikely(mti->mti_max_lmm == NULL)) {
114                 OBD_ALLOC(mti->mti_max_lmm, max_lmm_size);
115                 if (unlikely(mti->mti_max_lmm != NULL))
116                         mti->mti_max_lmm_size = max_lmm_size;
117         }
118         return mti->mti_max_lmm;
119 }
120
121 const struct lu_buf *mdd_buf_get_const(const struct lu_env *env,
122                                        const void *area, ssize_t len)
123 {
124         struct lu_buf *buf;
125
126         buf = &mdd_env_info(env)->mti_buf;
127         buf->lb_buf = (void *)area;
128         buf->lb_len = len;
129         return buf;
130 }
131
132 struct mdd_thread_info *mdd_env_info(const struct lu_env *env)
133 {
134         struct mdd_thread_info *info;
135
136         info = lu_context_key_get(&env->le_ctx, &mdd_thread_key);
137         LASSERT(info != NULL);
138         return info;
139 }
140
141 struct lu_object *mdd_object_alloc(const struct lu_env *env,
142                                    const struct lu_object_header *hdr,
143                                    struct lu_device *d)
144 {
145         struct mdd_object *mdd_obj;
146
147         OBD_ALLOC_PTR(mdd_obj);
148         if (mdd_obj != NULL) {
149                 struct lu_object *o;
150
151                 o = mdd2lu_obj(mdd_obj);
152                 lu_object_init(o, NULL, d);
153                 mdd_obj->mod_obj.mo_ops = &mdd_obj_ops;
154                 mdd_obj->mod_obj.mo_dir_ops = &mdd_dir_ops;
155                 mdd_obj->mod_count = 0;
156                 o->lo_ops = &mdd_lu_obj_ops;
157                 return o;
158         } else {
159                 return NULL;
160         }
161 }
162
163 static int mdd_object_init(const struct lu_env *env, struct lu_object *o)
164 {
165         struct mdd_device *d = lu2mdd_dev(o->lo_dev);
166         struct lu_object  *below;
167         struct lu_device  *under;
168         ENTRY;
169
170         under = &d->mdd_child->dd_lu_dev;
171         below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
172         mdd_pdlock_init(lu2mdd_obj(o));
173         if (below == NULL)
174                 RETURN(-ENOMEM);
175
176         lu_object_add(o, below);
177         RETURN(0);
178 }
179
180 static int mdd_object_start(const struct lu_env *env, struct lu_object *o)
181 {
182         if (lu_object_exists(o))
183                 return mdd_get_flags(env, lu2mdd_obj(o));
184         else
185                 return 0;
186 }
187
188 static void mdd_object_free(const struct lu_env *env, struct lu_object *o)
189 {
190         struct mdd_object *mdd = lu2mdd_obj(o);
191         
192         lu_object_fini(o);
193         OBD_FREE_PTR(mdd);
194 }
195
196 static int mdd_object_print(const struct lu_env *env, void *cookie,
197                             lu_printer_t p, const struct lu_object *o)
198 {
199         return (*p)(env, cookie, LUSTRE_MDD_NAME"-object@%p", o);
200 }
201
202 /* orphan handling is here */
203 static void mdd_object_delete(const struct lu_env *env,
204                                struct lu_object *o)
205 {
206         struct mdd_object *mdd_obj = lu2mdd_obj(o);
207         struct thandle *handle = NULL;
208         ENTRY;
209
210         if (lu2mdd_dev(o->lo_dev)->mdd_orphans == NULL)
211                 return;
212
213         if (mdd_obj->mod_flags & ORPHAN_OBJ) {
214                 mdd_txn_param_build(env, lu2mdd_dev(o->lo_dev),
215                                     MDD_TXN_INDEX_DELETE_OP);
216                 handle = mdd_trans_start(env, lu2mdd_dev(o->lo_dev));
217                 if (IS_ERR(handle))
218                         CERROR("Cannot get thandle\n");
219                 else {
220                         mdd_write_lock(env, mdd_obj);
221                         /* let's remove obj from the orphan list */
222                         __mdd_orphan_del(env, mdd_obj, handle);
223                         mdd_write_unlock(env, mdd_obj);
224                         mdd_trans_stop(env, lu2mdd_dev(o->lo_dev),
225                                        0, handle);
226                 }
227         }
228 }
229
230 static struct lu_object_operations mdd_lu_obj_ops = {
231         .loo_object_init    = mdd_object_init,
232         .loo_object_start   = mdd_object_start,
233         .loo_object_free    = mdd_object_free,
234         .loo_object_print   = mdd_object_print,
235         .loo_object_delete  = mdd_object_delete
236 };
237
238 struct mdd_object *mdd_object_find(const struct lu_env *env,
239                                    struct mdd_device *d,
240                                    const struct lu_fid *f)
241 {
242         struct lu_object *o, *lo;
243         struct mdd_object *m;
244         ENTRY;
245
246         o = lu_object_find(env, mdd2lu_dev(d)->ld_site, f);
247         if (IS_ERR(o))
248                 m = (struct mdd_object *)o;
249         else {
250                 lo = lu_object_locate(o->lo_header, mdd2lu_dev(d)->ld_type);
251                 /* remote object can't be located and should be put then */
252                 if (lo == NULL)
253                         lu_object_put(env, o);
254                 m = lu2mdd_obj(lo);
255         }
256         RETURN(m);
257 }
258
259 int mdd_get_flags(const struct lu_env *env, struct mdd_object *obj)
260 {
261         struct lu_attr *la = &mdd_env_info(env)->mti_la;
262         int rc;
263
264         ENTRY;
265         rc = mdd_la_get(env, obj, la, BYPASS_CAPA);
266         if (rc == 0)
267                 mdd_flags_xlate(obj, la->la_flags);
268         RETURN(rc);
269 }
270
271 /* get only inode attributes */
272 int mdd_iattr_get(const struct lu_env *env, struct mdd_object *mdd_obj,
273                   struct md_attr *ma)
274 {
275         int rc = 0;
276         ENTRY;
277
278         if (ma->ma_valid & MA_INODE)
279                 RETURN(0);
280
281         rc = mdd_la_get(env, mdd_obj, &ma->ma_attr,
282                           mdd_object_capa(env, mdd_obj));
283         if (rc == 0)
284                 ma->ma_valid |= MA_INODE;
285         RETURN(rc);
286 }
287
288 /* get lov EA only */
289 static int __mdd_lmm_get(const struct lu_env *env,
290                          struct mdd_object *mdd_obj, struct md_attr *ma)
291 {
292         int rc;
293         ENTRY;
294
295         if (ma->ma_valid & MA_LOV)
296                 RETURN(0);
297
298         rc = mdd_get_md(env, mdd_obj, ma->ma_lmm, &ma->ma_lmm_size,
299                         MDS_LOV_MD_NAME);
300         if (rc > 0) {
301                 ma->ma_valid |= MA_LOV;
302                 rc = 0;
303         }
304         RETURN(rc);
305 }
306
307 int mdd_lmm_get_locked(const struct lu_env *env, struct mdd_object *mdd_obj,
308                        struct md_attr *ma)
309 {
310         int rc;
311         ENTRY;
312
313         mdd_read_lock(env, mdd_obj);
314         rc = __mdd_lmm_get(env, mdd_obj, ma);
315         mdd_read_unlock(env, mdd_obj);
316         RETURN(rc);
317 }
318
319 /* get lmv EA only*/
320 static int __mdd_lmv_get(const struct lu_env *env,
321                          struct mdd_object *mdd_obj, struct md_attr *ma)
322 {
323         int rc;
324         ENTRY;
325
326         if (ma->ma_valid & MA_LMV)
327                 RETURN(0);
328
329         rc = mdd_get_md(env, mdd_obj, ma->ma_lmv, &ma->ma_lmv_size,
330                         MDS_LMV_MD_NAME);
331         if (rc > 0) {
332                 ma->ma_valid |= MA_LMV;
333                 rc = 0;
334         }
335         RETURN(rc);
336 }
337
338 static int mdd_attr_get_internal(const struct lu_env *env,
339                                  struct mdd_object *mdd_obj,
340                                  struct md_attr *ma)
341 {
342         int rc = 0;
343         ENTRY;
344
345         if (ma->ma_need & MA_INODE)
346                 rc = mdd_iattr_get(env, mdd_obj, ma);
347
348         if (rc == 0 && ma->ma_need & MA_LOV) {
349                 if (S_ISREG(mdd_object_type(mdd_obj)) ||
350                     S_ISDIR(mdd_object_type(mdd_obj)))
351                         rc = __mdd_lmm_get(env, mdd_obj, ma);
352         }
353         if (rc == 0 && ma->ma_need & MA_LMV) {
354                 if (S_ISDIR(mdd_object_type(mdd_obj)))
355                         rc = __mdd_lmv_get(env, mdd_obj, ma);
356         }
357 #ifdef CONFIG_FS_POSIX_ACL
358         if (rc == 0 && ma->ma_need & MA_ACL_DEF) {
359                 if (S_ISDIR(mdd_object_type(mdd_obj)))
360                         rc = mdd_acl_def_get(env, mdd_obj, ma);
361         }
362 #endif
363         CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64"\n",
364                rc, ma->ma_valid);
365         RETURN(rc);
366 }
367
368 int mdd_attr_get_internal_locked(const struct lu_env *env,
369                                  struct mdd_object *mdd_obj, struct md_attr *ma)
370 {
371         int rc;
372         int needlock = ma->ma_need & (MA_LOV | MA_LMV | MA_ACL_DEF);
373
374         if (needlock)
375                 mdd_read_lock(env, mdd_obj);
376         rc = mdd_attr_get_internal(env, mdd_obj, ma);
377         if (needlock)
378                 mdd_read_unlock(env, mdd_obj);
379         return rc;
380 }
381
382 /*
383  * No permission check is needed.
384  */
385 static int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
386                         struct md_attr *ma)
387 {
388         struct mdd_object *mdd_obj = md2mdd_obj(obj);
389         int                rc;
390
391         ENTRY;
392         rc = mdd_attr_get_internal_locked(env, mdd_obj, ma);
393         RETURN(rc);
394 }
395
396 /*
397  * No permission check is needed.
398  */
399 static int mdd_xattr_get(const struct lu_env *env,
400                          struct md_object *obj, struct lu_buf *buf,
401                          const char *name)
402 {
403         struct mdd_object *mdd_obj = md2mdd_obj(obj);
404         int rc;
405
406         ENTRY;
407
408         LASSERT(mdd_object_exists(mdd_obj));
409
410         mdd_read_lock(env, mdd_obj);
411         rc = mdo_xattr_get(env, mdd_obj, buf, name,
412                            mdd_object_capa(env, mdd_obj));
413         mdd_read_unlock(env, mdd_obj);
414
415         RETURN(rc);
416 }
417
418 /*
419  * Permission check is done when open,
420  * no need check again.
421  */
422 static int mdd_readlink(const struct lu_env *env, struct md_object *obj,
423                         struct lu_buf *buf)
424 {
425         struct mdd_object *mdd_obj = md2mdd_obj(obj);
426         struct dt_object  *next;
427         loff_t             pos = 0;
428         int                rc;
429         ENTRY;
430
431         LASSERT(mdd_object_exists(mdd_obj));
432
433         next = mdd_object_child(mdd_obj);
434         mdd_read_lock(env, mdd_obj);
435         rc = next->do_body_ops->dbo_read(env, next, buf, &pos,
436                                          mdd_object_capa(env, mdd_obj));
437         mdd_read_unlock(env, mdd_obj);
438         RETURN(rc);
439 }
440
441 /*
442  * No permission check is needed.
443  */
444 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
445                           struct lu_buf *buf)
446 {
447         struct mdd_object *mdd_obj = md2mdd_obj(obj);
448         int rc;
449
450         ENTRY;
451
452         mdd_read_lock(env, mdd_obj);
453         rc = mdo_xattr_list(env, mdd_obj, buf, mdd_object_capa(env, mdd_obj));
454         mdd_read_unlock(env, mdd_obj);
455
456         RETURN(rc);
457 }
458
459 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
460                                struct mdd_object *c, struct md_attr *ma,
461                                struct thandle *handle)
462 {
463         struct lu_attr *attr = &ma->ma_attr;
464         struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
465         int rc;
466         ENTRY;
467
468         if (!mdd_object_exists(c)) {
469                 struct dt_object *next = mdd_object_child(c);
470                 LASSERT(next);
471
472                 /* @hint will be initialized by underlying device. */
473                 next->do_ops->do_ah_init(env, hint,
474                                          p ? mdd_object_child(p) : NULL,
475                                          attr->la_mode & S_IFMT);
476                 rc = mdo_create_obj(env, c, attr, hint, handle);
477                 LASSERT(ergo(rc == 0, mdd_object_exists(c)));
478         } else
479                 rc = -EEXIST;
480
481         RETURN(rc);
482 }
483
484
485 int mdd_attr_set_internal(const struct lu_env *env, struct mdd_object *obj,
486                           const struct lu_attr *attr, struct thandle *handle,
487                           const int needacl)
488 {
489         int rc;
490         ENTRY;
491
492         rc = mdo_attr_set(env, obj, attr, handle, mdd_object_capa(env, obj));
493 #ifdef CONFIG_FS_POSIX_ACL
494         if (!rc && (attr->la_valid & LA_MODE) && needacl)
495                 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
496 #endif
497         RETURN(rc);
498 }
499
500 int mdd_attr_set_internal_locked(const struct lu_env *env,
501                                  struct mdd_object *o,
502                                  const struct lu_attr *attr,
503                                  struct thandle *handle, int needacl)
504 {
505         int rc;
506         ENTRY;
507
508         needacl = needacl && (attr->la_valid & LA_MODE);
509
510         if (needacl)
511                 mdd_write_lock(env, o);
512
513         rc = mdd_attr_set_internal(env, o, attr, handle, needacl);
514
515         if (needacl)
516                 mdd_write_unlock(env, o);
517         RETURN(rc);
518 }
519
520 static int __mdd_xattr_set(const struct lu_env *env, struct mdd_object *obj,
521                            const struct lu_buf *buf, const char *name,
522                            int fl, struct thandle *handle)
523 {
524         struct lustre_capa *capa = mdd_object_capa(env, obj);
525         int rc = -EINVAL;
526         ENTRY;
527
528         if (buf->lb_buf && buf->lb_len > 0)
529                 rc = mdo_xattr_set(env, obj, buf, name, 0, handle, capa);
530         else if (buf->lb_buf == NULL && buf->lb_len == 0)
531                 rc = mdo_xattr_del(env, obj, name, handle, capa);
532
533         RETURN(rc);
534 }
535
536 /*
537  * This gives the same functionality as the code between
538  * sys_chmod and inode_setattr
539  * chown_common and inode_setattr
540  * utimes and inode_setattr
541  * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
542  */
543 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
544                         struct lu_attr *la, const struct md_attr *ma)
545 {
546         struct lu_attr   *tmp_la     = &mdd_env_info(env)->mti_la;
547         struct md_ucred  *uc         = md_ucred(env);
548         int               rc;
549         ENTRY;
550
551         if (!la->la_valid)
552                 RETURN(0);
553
554         /* Do not permit change file type */
555         if (la->la_valid & LA_TYPE)
556                 RETURN(-EPERM);
557
558         /* They should not be processed by setattr */
559         if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
560                 RETURN(-EPERM);
561
562         /* This is only for set ctime when rename's source is on remote MDS. */
563         if (unlikely(la->la_valid == LA_CTIME)) {
564                 rc = mdd_may_delete(env, NULL, obj, (struct md_attr *)ma, 1, 0);
565                 RETURN(rc);
566         }
567
568         rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
569         if (rc)
570                 RETURN(rc);
571
572         if (la->la_valid == LA_ATIME) {
573                 /* This is atime only set for read atime update on close. */
574                 if (la->la_atime <= tmp_la->la_atime + 0/*XXX:mds_atime_diff*/)
575                         la->la_valid &= ~LA_ATIME;
576                 RETURN(0);
577         }
578  
579         /* Check if flags change. */
580         if (la->la_valid & LA_FLAGS) {
581                 unsigned int oldflags = 0;
582                 unsigned int newflags = la->la_flags &
583                                 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
584
585                 if ((uc->mu_fsuid != tmp_la->la_uid) &&
586                     !mdd_capable(uc, CAP_FOWNER))
587                         RETURN(-EPERM);
588
589                 /* XXX: the IMMUTABLE and APPEND_ONLY flags can
590                  * only be changed by the relevant capability. */
591                 if (mdd_is_immutable(obj))
592                         oldflags |= LUSTRE_IMMUTABLE_FL;
593                 if (mdd_is_append(obj))
594                         oldflags |= LUSTRE_APPEND_FL; 
595                 if ((oldflags ^ newflags) &&
596                     !mdd_capable(uc, CAP_LINUX_IMMUTABLE))
597                         RETURN(-EPERM);
598
599                 if (!S_ISDIR(tmp_la->la_mode))
600                         la->la_flags &= ~LUSTRE_DIRSYNC_FL;
601         }
602
603         if ((mdd_is_immutable(obj) || mdd_is_append(obj)) &&
604             (la->la_valid & ~LA_FLAGS) &&
605             !(ma->ma_attr_flags & MDS_PERM_BYPASS))
606                 RETURN(-EPERM);
607
608         /* Check for setting the obj time. */
609         if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
610             !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
611                 if ((uc->mu_fsuid != tmp_la->la_uid) &&
612                     !mdd_capable(uc, CAP_FOWNER)) {
613                         rc = mdd_permission_internal_locked(env, obj, tmp_la,
614                                                             MAY_WRITE);
615                         if (rc)
616                                 RETURN(rc);
617                 }
618         }
619
620         /* Make sure a caller can chmod. */
621         if (la->la_valid & LA_MODE) {
622                 /*
623                  * Bypass la_vaild == LA_MODE,
624                  * this is for changing file with SUID or SGID.
625                  */
626                 if ((la->la_valid & ~LA_MODE) &&
627                     (uc->mu_fsuid != tmp_la->la_uid) &&
628                     !mdd_capable(uc, CAP_FOWNER))
629                         RETURN(-EPERM);
630
631                 if (la->la_mode == (umode_t) -1)
632                         la->la_mode = tmp_la->la_mode;
633                 else
634                         la->la_mode = (la->la_mode & S_IALLUGO) |
635                                       (tmp_la->la_mode & ~S_IALLUGO);
636
637                 /* Also check the setgid bit! */
638                 if (!mdd_in_group_p(uc, (la->la_valid & LA_GID) ? la->la_gid :
639                                 tmp_la->la_gid) && !mdd_capable(uc, CAP_FSETID))
640                         la->la_mode &= ~S_ISGID;
641         } else {
642                la->la_mode = tmp_la->la_mode;
643         }
644
645         /* Make sure a caller can chown. */
646         if (la->la_valid & LA_UID) {
647                 if (la->la_uid == (uid_t) -1)
648                         la->la_uid = tmp_la->la_uid;
649                 if (((uc->mu_fsuid != tmp_la->la_uid) ||
650                     (la->la_uid != tmp_la->la_uid)) &&
651                     !mdd_capable(uc, CAP_CHOWN))
652                         RETURN(-EPERM);
653
654                 /*
655                  * If the user or group of a non-directory has been
656                  * changed by a non-root user, remove the setuid bit.
657                  * 19981026 David C Niemi <niemi@tux.org>
658                  *
659                  * Changed this to apply to all users, including root,
660                  * to avoid some races. This is the behavior we had in
661                  * 2.0. The check for non-root was definitely wrong
662                  * for 2.2 anyway, as it should have been using
663                  * CAP_FSETID rather than fsuid -- 19990830 SD.
664                  */
665                 if (((tmp_la->la_mode & S_ISUID) == S_ISUID) &&
666                     !S_ISDIR(tmp_la->la_mode)) {
667                         la->la_mode &= ~S_ISUID;
668                         la->la_valid |= LA_MODE;
669                 }
670         }
671
672         /* Make sure caller can chgrp. */
673         if (la->la_valid & LA_GID) {
674                 if (la->la_gid == (gid_t) -1)
675                         la->la_gid = tmp_la->la_gid;
676                 if (((uc->mu_fsuid != tmp_la->la_uid) ||
677                     ((la->la_gid != tmp_la->la_gid) &&
678                     !mdd_in_group_p(uc, la->la_gid))) &&
679                     !mdd_capable(uc, CAP_CHOWN))
680                         RETURN(-EPERM);
681
682                 /*
683                  * Likewise, if the user or group of a non-directory
684                  * has been changed by a non-root user, remove the
685                  * setgid bit UNLESS there is no group execute bit
686                  * (this would be a file marked for mandatory
687                  * locking).  19981026 David C Niemi <niemi@tux.org>
688                  *
689                  * Removed the fsuid check (see the comment above) --
690                  * 19990830 SD.
691                  */
692                 if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
693                      (S_ISGID | S_IXGRP)) && !S_ISDIR(tmp_la->la_mode)) {
694                         la->la_mode &= ~S_ISGID;
695                         la->la_valid |= LA_MODE;
696                 }
697         }
698
699         /* For truncate (or setsize), we should have MAY_WRITE perm */
700         if (la->la_valid & (LA_SIZE | LA_BLOCKS)) {
701                 if (!((la->la_valid & MDS_OPEN_OWNEROVERRIDE) &&
702                     (uc->mu_fsuid == tmp_la->la_uid)) &&
703                     !(ma->ma_attr_flags & MDS_PERM_BYPASS)) {
704                         rc = mdd_permission_internal_locked(env, obj, tmp_la,
705                                                             MAY_WRITE);
706                         if (rc)
707                                 RETURN(rc);
708                 }
709
710                 /* For the "Size-on-MDS" setattr update, merge coming
711                  * attributes with the set in the inode. BUG 10641 */
712                 if ((la->la_valid & LA_ATIME) &&
713                     (la->la_atime <= tmp_la->la_atime))
714                         la->la_valid &= ~LA_ATIME;
715
716                 /* OST attributes do not have a priority over MDS attributes,
717                  * so drop times if ctime is equal. */
718                 if ((la->la_valid & LA_CTIME) &&
719                     (la->la_ctime <= tmp_la->la_ctime))
720                         la->la_valid &= ~(LA_MTIME | LA_CTIME);
721         } else if (la->la_valid & LA_CTIME) {
722                 /* The pure setattr, it has the priority over what is already
723                  * set, do not drop it if ctime is equal. */
724                 if (la->la_ctime < tmp_la->la_ctime)
725                         la->la_valid &= ~(LA_ATIME | LA_MTIME | LA_CTIME);
726         }
727
728         RETURN(0);
729 }
730
731 /* set attr and LOV EA at once, return updated attr */
732 static int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
733                         const struct md_attr *ma)
734 {
735         struct mdd_object *mdd_obj = md2mdd_obj(obj);
736         struct mdd_device *mdd = mdo2mdd(obj);
737         struct thandle *handle;
738         struct lov_mds_md *lmm = NULL;
739         struct llog_cookie *logcookies = NULL;
740         int  rc, lmm_size = 0, cookie_size = 0;
741         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
742         ENTRY;
743
744         mdd_txn_param_build(env, mdd, MDD_TXN_ATTR_SET_OP);
745         handle = mdd_trans_start(env, mdd);
746         if (IS_ERR(handle))
747                 RETURN(PTR_ERR(handle));
748         /*TODO: add lock here*/
749         /* start a log jounal handle if needed */
750         if (S_ISREG(mdd_object_type(mdd_obj)) &&
751             ma->ma_attr.la_valid & (LA_UID | LA_GID)) {
752                 lmm_size = mdd_lov_mdsize(env, mdd);
753                 lmm = mdd_max_lmm_get(env, mdd);
754                 if (lmm == NULL)
755                         GOTO(cleanup, rc = -ENOMEM);
756
757                 rc = mdd_get_md_locked(env, mdd_obj, lmm, &lmm_size,
758                                 MDS_LOV_MD_NAME);
759
760                 if (rc < 0)
761                         GOTO(cleanup, rc);
762         }
763
764         if (ma->ma_attr.la_valid & (ATTR_MTIME | ATTR_CTIME))
765                 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
766                        ma->ma_attr.la_mtime, ma->ma_attr.la_ctime);
767
768         *la_copy = ma->ma_attr;
769         rc = mdd_fix_attr(env, mdd_obj, la_copy, ma);
770         if (rc)
771                 GOTO(cleanup, rc);
772
773         if (la_copy->la_valid & LA_FLAGS) {
774                 rc = mdd_attr_set_internal_locked(env, mdd_obj, la_copy,
775                                                   handle, 1);
776                 if (rc == 0)
777                         mdd_flags_xlate(mdd_obj, la_copy->la_flags);
778         } else if (la_copy->la_valid) {            /* setattr */
779                 rc = mdd_attr_set_internal_locked(env, mdd_obj, la_copy,
780                                                   handle, 1);
781                 /* journal chown/chgrp in llog, just like unlink */
782                 if (rc == 0 && lmm_size){
783                         cookie_size = mdd_lov_cookiesize(env, mdd);
784                         logcookies = mdd_max_cookie_get(env, mdd);
785                         if (logcookies == NULL)
786                                 GOTO(cleanup, rc = -ENOMEM);
787
788                         if (mdd_setattr_log(env, mdd, ma, lmm, lmm_size,
789                                             logcookies, cookie_size) <= 0)
790                                 logcookies = NULL;
791                 }
792         }
793
794         if (rc == 0 && ma->ma_valid & MA_LOV) {
795                 umode_t mode;
796
797                 mode = mdd_object_type(mdd_obj);
798                 if (S_ISREG(mode) || S_ISDIR(mode)) {
799                         rc = mdd_lsm_sanity_check(env, mdd_obj);
800                         if (rc)
801                                 GOTO(cleanup, rc);
802
803                         rc = mdd_lov_set_md(env, NULL, mdd_obj, ma->ma_lmm,
804                                             ma->ma_lmm_size, handle, 1);
805                 }
806
807         }
808 cleanup:
809         mdd_trans_stop(env, mdd, rc, handle);
810         if (rc == 0 && (lmm != NULL && lmm_size > 0 )) {
811                 /*set obd attr, if needed*/
812                 rc = mdd_lov_setattr_async(env, mdd_obj, lmm, lmm_size,
813                                            logcookies);
814         }
815         RETURN(rc);
816 }
817
818 int mdd_xattr_set_txn(const struct lu_env *env, struct mdd_object *obj,
819                       const struct lu_buf *buf, const char *name, int fl,
820                       struct thandle *handle)
821 {
822         int  rc;
823         ENTRY;
824
825         mdd_write_lock(env, obj);
826         rc = __mdd_xattr_set(env, obj, buf, name, fl, handle);
827         mdd_write_unlock(env, obj);
828
829         RETURN(rc);
830 }
831
832 static int mdd_xattr_sanity_check(const struct lu_env *env,
833                                   struct mdd_object *obj)
834 {
835         struct lu_attr  *tmp_la = &mdd_env_info(env)->mti_la;
836         struct md_ucred *uc     = md_ucred(env);
837         int rc;
838         ENTRY;
839
840         if (mdd_is_immutable(obj) || mdd_is_append(obj))
841                 RETURN(-EPERM);
842
843         rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
844         if (rc)
845                 RETURN(rc);
846
847         if ((uc->mu_fsuid != tmp_la->la_uid) && !mdd_capable(uc, CAP_FOWNER))
848                 RETURN(-EPERM);
849
850         RETURN(rc);
851 }
852
853 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
854                          const struct lu_buf *buf, const char *name, int fl)
855 {
856         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
857         struct mdd_object *mdd_obj = md2mdd_obj(obj);
858         struct mdd_device *mdd = mdo2mdd(obj);
859         struct thandle *handle;
860         int  rc;
861         ENTRY;
862
863         rc = mdd_xattr_sanity_check(env, mdd_obj);
864         if (rc)
865                 RETURN(rc);
866
867         mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
868         handle = mdd_trans_start(env, mdd);
869         if (IS_ERR(handle))
870                 RETURN(PTR_ERR(handle));
871
872         rc = mdd_xattr_set_txn(env, md2mdd_obj(obj), buf, name,
873                                fl, handle);
874         if (rc == 0) {
875                 la_copy->la_ctime = CURRENT_SECONDS;
876                 la_copy->la_valid = LA_CTIME;
877                 rc = mdd_attr_set_internal_locked(env, mdd_obj, la_copy,
878                                                   handle, 0);
879         }
880         mdd_trans_stop(env, mdd, rc, handle);
881
882         RETURN(rc);
883 }
884
885 int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
886                   const char *name)
887 {
888         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
889         struct mdd_object *mdd_obj = md2mdd_obj(obj);
890         struct mdd_device *mdd = mdo2mdd(obj);
891         struct thandle *handle;
892         int  rc;
893         ENTRY;
894
895         rc = mdd_xattr_sanity_check(env, mdd_obj);
896         if (rc)
897                 RETURN(rc);
898
899         mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
900         handle = mdd_trans_start(env, mdd);
901         if (IS_ERR(handle))
902                 RETURN(PTR_ERR(handle));
903
904         mdd_write_lock(env, mdd_obj);
905         rc = mdo_xattr_del(env, md2mdd_obj(obj), name, handle,
906                            mdd_object_capa(env, mdd_obj));
907         mdd_write_unlock(env, mdd_obj);
908         if (rc == 0) {
909                 la_copy->la_ctime = CURRENT_SECONDS;
910                 la_copy->la_valid = LA_CTIME;
911                 rc = mdd_attr_set_internal_locked(env, mdd_obj, la_copy,
912                                                   handle, 0);
913         }
914
915         mdd_trans_stop(env, mdd, rc, handle);
916
917         RETURN(rc);
918 }
919
920 /* partial unlink */
921 static int mdd_ref_del(const struct lu_env *env, struct md_object *obj,
922                        struct md_attr *ma)
923 {
924         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
925         struct mdd_object *mdd_obj = md2mdd_obj(obj);
926         struct mdd_device *mdd = mdo2mdd(obj);
927         struct thandle *handle;
928         int rc;
929         ENTRY;
930
931         /*
932          * Check -ENOENT early here because we need to get object type
933          * to calculate credits before transaction start
934          */
935         if (!mdd_object_exists(mdd_obj))
936                 RETURN(-ENOENT);
937
938         LASSERT(mdd_object_exists(mdd_obj) > 0);
939
940         rc = mdd_log_txn_param_build(env, obj, ma, MDD_TXN_UNLINK_OP);
941         if (rc)
942                 RETURN(rc);
943
944         handle = mdd_trans_start(env, mdd);
945         if (IS_ERR(handle))
946                 RETURN(-ENOMEM);
947
948         mdd_write_lock(env, mdd_obj);
949
950         rc = mdd_unlink_sanity_check(env, NULL, mdd_obj, ma);
951         if (rc)
952                 GOTO(cleanup, rc);
953
954         mdo_ref_del(env, mdd_obj, handle);
955
956         if (S_ISDIR(lu_object_attr(&obj->mo_lu))) {
957                 /* unlink dot */
958                 mdo_ref_del(env, mdd_obj, handle);
959         }
960
961         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
962         la_copy->la_ctime = ma->ma_attr.la_ctime;
963
964         la_copy->la_valid = LA_CTIME;
965         rc = mdd_attr_set_internal(env, mdd_obj, la_copy, handle, 0);
966         if (rc)
967                 GOTO(cleanup, rc);
968
969         rc = mdd_finish_unlink(env, mdd_obj, ma, handle);
970
971         EXIT;
972 cleanup:
973         mdd_write_unlock(env, mdd_obj);
974         mdd_trans_stop(env, mdd, rc, handle);
975         return rc;
976 }
977
978 /* partial operation */
979 static int mdd_oc_sanity_check(const struct lu_env *env,
980                                struct mdd_object *obj,
981                                struct md_attr *ma)
982 {
983         int rc;
984         ENTRY;
985
986         switch (ma->ma_attr.la_mode & S_IFMT) {
987         case S_IFREG:
988         case S_IFDIR:
989         case S_IFLNK:
990         case S_IFCHR:
991         case S_IFBLK:
992         case S_IFIFO:
993         case S_IFSOCK:
994                 rc = 0;
995                 break;
996         default:
997                 rc = -EINVAL;
998                 break;
999         }
1000         RETURN(rc);
1001 }
1002
1003 static int mdd_object_create(const struct lu_env *env,
1004                              struct md_object *obj,
1005                              const struct md_op_spec *spec,
1006                              struct md_attr *ma)
1007 {
1008
1009         struct mdd_device *mdd = mdo2mdd(obj);
1010         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1011         const struct lu_fid *pfid = spec->u.sp_pfid;
1012         struct thandle *handle;
1013         int rc;
1014         ENTRY;
1015
1016         mdd_txn_param_build(env, mdd, MDD_TXN_OBJECT_CREATE_OP);
1017         handle = mdd_trans_start(env, mdd);
1018         if (IS_ERR(handle))
1019                 RETURN(PTR_ERR(handle));
1020
1021         mdd_write_lock(env, mdd_obj);
1022         rc = mdd_oc_sanity_check(env, mdd_obj, ma);
1023         if (rc)
1024                 GOTO(unlock, rc);
1025
1026         rc = mdd_object_create_internal(env, NULL, mdd_obj, ma, handle);
1027         if (rc)
1028                 GOTO(unlock, rc);
1029
1030         if (spec->sp_cr_flags & MDS_CREATE_SLAVE_OBJ) {
1031                 /* If creating the slave object, set slave EA here. */
1032                 int lmv_size = spec->u.sp_ea.eadatalen;
1033                 struct lmv_stripe_md *lmv;
1034
1035                 lmv = (struct lmv_stripe_md *)spec->u.sp_ea.eadata;
1036                 LASSERT(lmv != NULL && lmv_size > 0);
1037
1038                 rc = __mdd_xattr_set(env, mdd_obj,
1039                                      mdd_buf_get_const(env, lmv, lmv_size),
1040                                      MDS_LMV_MD_NAME, 0, handle);
1041                 if (rc)
1042                         GOTO(unlock, rc);
1043
1044                 rc = mdd_attr_set_internal(env, mdd_obj, &ma->ma_attr, handle, 0);
1045         } else {
1046 #ifdef CONFIG_FS_POSIX_ACL
1047                 if (spec->sp_cr_flags & MDS_CREATE_RMT_ACL) {
1048                         struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1049
1050                         buf->lb_buf = (void *)spec->u.sp_ea.eadata;
1051                         buf->lb_len = spec->u.sp_ea.eadatalen;
1052                         if ((buf->lb_len > 0) && (buf->lb_buf != NULL)) {
1053                                 rc = __mdd_acl_init(env, mdd_obj, buf,
1054                                                     &ma->ma_attr.la_mode,
1055                                                     handle);
1056                                 if (rc)
1057                                         GOTO(unlock, rc);
1058                                 else
1059                                         ma->ma_attr.la_valid |= LA_MODE;
1060                         }
1061
1062                         pfid = spec->u.sp_ea.fid;
1063                 }
1064 #endif
1065                 rc = mdd_object_initialize(env, pfid, mdd_obj, ma, handle);
1066         }
1067         EXIT;
1068 unlock:
1069         mdd_write_unlock(env, mdd_obj);
1070         if (rc == 0)
1071                 rc = mdd_attr_get_internal_locked(env, mdd_obj, ma);
1072
1073         mdd_trans_stop(env, mdd, rc, handle);
1074         return rc;
1075 }
1076
1077 /* partial link */
1078 static int mdd_ref_add(const struct lu_env *env, struct md_object *obj,
1079                        const struct md_attr *ma)
1080 {
1081         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1082         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1083         struct mdd_device *mdd = mdo2mdd(obj);
1084         struct thandle *handle;
1085         int rc;
1086         ENTRY;
1087
1088         mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
1089         handle = mdd_trans_start(env, mdd);
1090         if (IS_ERR(handle))
1091                 RETURN(-ENOMEM);
1092
1093         mdd_write_lock(env, mdd_obj);
1094         rc = mdd_link_sanity_check(env, NULL, NULL, mdd_obj);
1095         if (rc == 0)
1096                 mdo_ref_add(env, mdd_obj, handle);
1097         mdd_write_unlock(env, mdd_obj);
1098         if (rc == 0) {
1099                 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1100                 la_copy->la_ctime = ma->ma_attr.la_ctime;
1101
1102                 la_copy->la_valid = LA_CTIME;
1103                 rc = mdd_attr_set_internal_locked(env, mdd_obj, la_copy,
1104                                                   handle, 0);
1105         }
1106         mdd_trans_stop(env, mdd, 0, handle);
1107
1108         RETURN(rc);
1109 }
1110
1111 /*
1112  * do NOT or the MAY_*'s, you'll get the weakest
1113  */
1114 int accmode(const struct lu_env *env, struct lu_attr *la, int flags)
1115 {
1116         int res = 0;
1117
1118         /* Sadly, NFSD reopens a file repeatedly during operation, so the
1119          * "acc_mode = 0" allowance for newly-created files isn't honoured.
1120          * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1121          * owner can write to a file even if it is marked readonly to hide
1122          * its brokenness. (bug 5781) */
1123         if (flags & MDS_OPEN_OWNEROVERRIDE) {
1124                 struct md_ucred *uc = md_ucred(env);
1125
1126                 if ((uc == NULL) || (uc->mu_valid == UCRED_INIT) ||
1127                     (la->la_uid == uc->mu_fsuid))
1128                         return 0;
1129         }
1130
1131         if (flags & FMODE_READ)
1132                 res |= MAY_READ;
1133         if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
1134                 res |= MAY_WRITE;
1135         if (flags & MDS_FMODE_EXEC)
1136                 res |= MAY_EXEC;
1137         return res;
1138 }
1139
1140 static int mdd_open_sanity_check(const struct lu_env *env,
1141                                  struct mdd_object *obj, int flag)
1142 {
1143         struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
1144         int mode, rc;
1145         ENTRY;
1146
1147         /* EEXIST check */
1148         if (mdd_is_dead_obj(obj))
1149                 RETURN(-ENOENT);
1150
1151         rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
1152         if (rc)
1153                RETURN(rc);
1154
1155         if (S_ISLNK(tmp_la->la_mode))
1156                 RETURN(-ELOOP);
1157
1158         mode = accmode(env, tmp_la, flag);
1159
1160         if (S_ISDIR(tmp_la->la_mode) && (mode & MAY_WRITE))
1161                 RETURN(-EISDIR);
1162
1163         if (!(flag & MDS_OPEN_CREATED)) {
1164                 rc = mdd_permission_internal(env, obj, tmp_la, mode);
1165                 if (rc)
1166                         RETURN(rc);
1167         }
1168
1169         if (S_ISFIFO(tmp_la->la_mode) || S_ISSOCK(tmp_la->la_mode) ||
1170             S_ISBLK(tmp_la->la_mode) || S_ISCHR(tmp_la->la_mode))
1171                 flag &= ~MDS_OPEN_TRUNC;
1172
1173         /* For writing append-only file must open it with append mode. */
1174         if (mdd_is_append(obj)) {
1175                 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
1176                         RETURN(-EPERM);
1177                 if (flag & MDS_OPEN_TRUNC)
1178                         RETURN(-EPERM);
1179         }
1180
1181 #if 0
1182         /*
1183          * Now, flag -- O_NOATIME does not be packed by client.
1184          */
1185         if (flag & O_NOATIME) {
1186                 struct md_ucred *uc = md_ucred(env);
1187
1188                 if (uc && ((uc->mu_valid == UCRED_OLD) ||
1189                     (uc->mu_valid == UCRED_NEW)) &&
1190                     (uc->mu_fsuid != tmp_la->la_uid) &&
1191                     !mdd_capable(uc, CAP_FOWNER))
1192                         RETURN(-EPERM);
1193         }
1194 #endif
1195
1196         RETURN(0);
1197 }
1198
1199 static int mdd_open(const struct lu_env *env, struct md_object *obj,
1200                     int flags)
1201 {
1202         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1203         int rc = 0;
1204
1205         mdd_write_lock(env, mdd_obj);
1206
1207         rc = mdd_open_sanity_check(env, mdd_obj, flags);
1208         if (rc == 0)
1209                 mdd_obj->mod_count++;
1210
1211         mdd_write_unlock(env, mdd_obj);
1212         return rc;
1213 }
1214
1215 /* return md_attr back,
1216  * if it is last unlink then return lov ea + llog cookie*/
1217 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
1218                     struct md_attr *ma)
1219 {
1220         int rc = 0;
1221         ENTRY;
1222
1223         if (S_ISREG(mdd_object_type(obj))) {
1224                 /* Return LOV & COOKIES unconditionally here. We clean evth up.
1225                  * Caller must be ready for that. */
1226                 rc = __mdd_lmm_get(env, obj, ma);
1227                 if ((ma->ma_valid & MA_LOV))
1228                         rc = mdd_unlink_log(env, mdo2mdd(&obj->mod_obj),
1229                                             obj, ma);
1230         }
1231         RETURN(rc);
1232 }
1233
1234 /*
1235  * No permission check is needed.
1236  */
1237 static int mdd_close(const struct lu_env *env, struct md_object *obj,
1238                      struct md_attr *ma)
1239 {
1240         int rc;
1241         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1242         struct thandle    *handle;
1243         ENTRY;
1244
1245         rc = mdd_log_txn_param_build(env, obj, ma, MDD_TXN_UNLINK_OP);
1246         if (rc)
1247                 RETURN(rc);
1248         handle = mdd_trans_start(env, mdo2mdd(obj));
1249         if (IS_ERR(handle))
1250                 RETURN(PTR_ERR(handle));
1251
1252         mdd_write_lock(env, mdd_obj);
1253         /* release open count */
1254         mdd_obj->mod_count --;
1255
1256         rc = mdd_iattr_get(env, mdd_obj, ma);
1257         if (rc == 0 && mdd_obj->mod_count == 0 && ma->ma_attr.la_nlink == 0)
1258                 rc = mdd_object_kill(env, mdd_obj, ma);
1259         else
1260                 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
1261         
1262         mdd_write_unlock(env, mdd_obj);
1263         mdd_trans_stop(env, mdo2mdd(obj), rc, handle);
1264         RETURN(rc);
1265 }
1266
1267 /*
1268  * Permission check is done when open,
1269  * no need check again.
1270  */
1271 static int mdd_readpage_sanity_check(const struct lu_env *env,
1272                                      struct mdd_object *obj)
1273 {
1274         struct dt_object *next = mdd_object_child(obj);
1275         int rc;
1276         ENTRY;
1277
1278         if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
1279                 rc = 0;
1280         else
1281                 rc = -ENOTDIR;
1282
1283         RETURN(rc);
1284 }
1285
1286 static int mdd_dir_page_build(const struct lu_env *env, int first,
1287                               void *area, int nob, struct dt_it_ops *iops,
1288                               struct dt_it *it, __u32 *start, __u32 *end,
1289                               struct lu_dirent **last)
1290 {
1291         struct lu_fid          *fid  = &mdd_env_info(env)->mti_fid2;
1292         struct mdd_thread_info *info = mdd_env_info(env);
1293         struct lu_fid_pack     *pack = &info->mti_pack;
1294         int                     result;
1295         struct lu_dirent       *ent;
1296
1297         if (first) {
1298                 memset(area, 0, sizeof (struct lu_dirpage));
1299                 area += sizeof (struct lu_dirpage);
1300                 nob  -= sizeof (struct lu_dirpage);
1301         }
1302
1303         LASSERT(nob > sizeof *ent);
1304
1305         ent  = area;
1306         result = 0;
1307         do {
1308                 char  *name;
1309                 int    len;
1310                 int    recsize;
1311                 __u32  hash;
1312
1313                 name = (char *)iops->key(env, it);
1314                 len  = iops->key_size(env, it);
1315
1316                 pack = (struct lu_fid_pack *)iops->rec(env, it);
1317                 result = fid_unpack(pack, fid);
1318                 if (result != 0)
1319                         break;
1320
1321                 recsize = (sizeof(*ent) + len + 3) & ~3;
1322                 hash = iops->store(env, it);
1323                 *end = hash;
1324
1325                 CDEBUG(D_INFO, "%p %p %d "DFID": %#8.8x (%d) \"%*.*s\"\n",
1326                        name, ent, nob, PFID(fid), hash, len, len, len, name);
1327
1328                 if (nob >= recsize) {
1329                         ent->lde_fid = *fid;
1330                         fid_cpu_to_le(&ent->lde_fid, &ent->lde_fid);
1331                         ent->lde_hash = hash;
1332                         ent->lde_namelen = cpu_to_le16(len);
1333                         ent->lde_reclen  = cpu_to_le16(recsize);
1334                         memcpy(ent->lde_name, name, len);
1335                         if (first && ent == area)
1336                                 *start = hash;
1337                         *last = ent;
1338                         ent = (void *)ent + recsize;
1339                         nob -= recsize;
1340                         result = iops->next(env, it);
1341                 } else {
1342                         /*
1343                          * record doesn't fit into page, enlarge previous one.
1344                          */
1345                         LASSERT(*last != NULL);
1346                         (*last)->lde_reclen =
1347                                 cpu_to_le16(le16_to_cpu((*last)->lde_reclen) +
1348                                             nob);
1349                         break;
1350                 }
1351         } while (result == 0);
1352
1353         return result;
1354 }
1355
1356 static int __mdd_readpage(const struct lu_env *env, struct mdd_object *obj,
1357                           const struct lu_rdpg *rdpg)
1358 {
1359         struct dt_it      *it;
1360         struct dt_object  *next = mdd_object_child(obj);
1361         struct dt_it_ops  *iops;
1362         struct page       *pg;
1363         struct lu_dirent  *last;
1364         int i;
1365         int rc;
1366         int nob;
1367         __u32 hash_start;
1368         __u32 hash_end;
1369
1370         LASSERT(rdpg->rp_pages != NULL);
1371         LASSERT(next->do_index_ops != NULL);
1372
1373         if (rdpg->rp_count <= 0)
1374                 return -EFAULT;
1375
1376         /*
1377          * iterate through directory and fill pages from @rdpg
1378          */
1379         iops = &next->do_index_ops->dio_it;
1380         it = iops->init(env, next, 0, mdd_object_capa(env, obj));
1381         if (it == NULL)
1382                 return -ENOMEM;
1383
1384         rc = iops->load(env, it, rdpg->rp_hash);
1385
1386         if (rc == 0)
1387                 /*
1388                  * Iterator didn't find record with exactly the key requested.
1389                  *
1390                  * It is currently either
1391                  *
1392                  *     - positioned above record with key less than
1393                  *     requested---skip it.
1394                  *
1395                  *     - or not positioned at all (is in IAM_IT_SKEWED
1396                  *     state)---position it on the next item.
1397                  */
1398                 rc = iops->next(env, it);
1399         else if (rc > 0)
1400                 rc = 0;
1401
1402         /*
1403          * At this point and across for-loop:
1404          *
1405          *  rc == 0 -> ok, proceed.
1406          *  rc >  0 -> end of directory.
1407          *  rc <  0 -> error.
1408          */
1409         for (i = 0, nob = rdpg->rp_count; rc == 0 && nob > 0;
1410              i++, nob -= CFS_PAGE_SIZE) {
1411                 LASSERT(i < rdpg->rp_npages);
1412                 pg = rdpg->rp_pages[i];
1413                 rc = mdd_dir_page_build(env, !i, kmap(pg),
1414                                         min_t(int, nob, CFS_PAGE_SIZE), iops,
1415                                         it, &hash_start, &hash_end, &last);
1416                 if (rc != 0 || i == rdpg->rp_npages - 1)
1417                         last->lde_reclen = 0;
1418                 kunmap(pg);
1419         }
1420         if (rc > 0) {
1421                 /*
1422                  * end of directory.
1423                  */
1424                 hash_end = DIR_END_OFF;
1425                 rc = 0;
1426         }
1427         if (rc == 0) {
1428                 struct lu_dirpage *dp;
1429
1430                 dp = kmap(rdpg->rp_pages[0]);
1431                 dp->ldp_hash_start = rdpg->rp_hash;
1432                 dp->ldp_hash_end   = hash_end;
1433                 if (i == 0)
1434                         /*
1435                          * No pages were processed, mark this.
1436                          */
1437                         dp->ldp_flags |= LDF_EMPTY;
1438                 dp->ldp_flags = cpu_to_le16(dp->ldp_flags);
1439                 kunmap(rdpg->rp_pages[0]);
1440         }
1441         iops->put(env, it);
1442         iops->fini(env, it);
1443
1444         return rc;
1445 }
1446
1447 static int mdd_readpage(const struct lu_env *env, struct md_object *obj,
1448                         const struct lu_rdpg *rdpg)
1449 {
1450         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1451         int rc;
1452         ENTRY;
1453
1454         LASSERT(mdd_object_exists(mdd_obj));
1455
1456         mdd_read_lock(env, mdd_obj);
1457         rc = mdd_readpage_sanity_check(env, mdd_obj);
1458         if (rc)
1459                 GOTO(out_unlock, rc);
1460
1461         if (mdd_is_dead_obj(mdd_obj)) {
1462                 struct page *pg;
1463                 struct lu_dirpage *dp;
1464
1465                 /*
1466                  * According to POSIX, please do not return any entry to client:
1467                  * even dot and dotdot should not be returned.
1468                  */
1469                 CWARN("readdir from dead object: "DFID"\n",
1470                         PFID(mdd_object_fid(mdd_obj)));
1471
1472                 if (rdpg->rp_count <= 0)
1473                         GOTO(out_unlock, rc = -EFAULT);
1474                 LASSERT(rdpg->rp_pages != NULL);
1475
1476                 pg = rdpg->rp_pages[0];
1477                 dp = (struct lu_dirpage*)kmap(pg);
1478                 memset(dp, 0 , sizeof(struct lu_dirpage));
1479                 dp->ldp_hash_start = rdpg->rp_hash;
1480                 dp->ldp_hash_end   = DIR_END_OFF;
1481                 dp->ldp_flags |= LDF_EMPTY;
1482                 dp->ldp_flags = cpu_to_le16(dp->ldp_flags);
1483                 kunmap(pg);
1484                 GOTO(out_unlock, rc = 0);
1485         }
1486
1487         rc = __mdd_readpage(env, mdd_obj, rdpg);
1488
1489         EXIT;
1490 out_unlock:
1491         mdd_read_unlock(env, mdd_obj);
1492         return rc;
1493 }
1494
1495 struct md_object_operations mdd_obj_ops = {
1496         .moo_permission    = mdd_permission,
1497         .moo_attr_get      = mdd_attr_get,
1498         .moo_attr_set      = mdd_attr_set,
1499         .moo_xattr_get     = mdd_xattr_get,
1500         .moo_xattr_set     = mdd_xattr_set,
1501         .moo_xattr_list    = mdd_xattr_list,
1502         .moo_xattr_del     = mdd_xattr_del,
1503         .moo_object_create = mdd_object_create,
1504         .moo_ref_add       = mdd_ref_add,
1505         .moo_ref_del       = mdd_ref_del,
1506         .moo_open          = mdd_open,
1507         .moo_close         = mdd_close,
1508         .moo_readpage      = mdd_readpage,
1509         .moo_readlink      = mdd_readlink,
1510         .moo_capa_get      = mdd_capa_get
1511 };