Whamcloud - gitweb
Branch HEAD
[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 static int mdd_get_default_md(struct mdd_object *mdd_obj,
289                 struct lov_mds_md *lmm, int *size)
290 {
291         struct lov_desc *ldesc;
292         struct mdd_device *mdd = mdo2mdd(&mdd_obj->mod_obj);
293         ENTRY;
294
295         ldesc = &mdd->mdd_obd_dev->u.mds.mds_lov_desc;
296         LASSERT(ldesc != NULL);
297
298         if (!lmm)
299                 RETURN(0);
300
301         lmm->lmm_magic = LOV_MAGIC_V1;
302         lmm->lmm_object_gr = LOV_OBJECT_GROUP_DEFAULT;
303         lmm->lmm_pattern = ldesc->ld_pattern;
304         lmm->lmm_stripe_size = ldesc->ld_default_stripe_size;
305         lmm->lmm_stripe_count = ldesc->ld_default_stripe_count;
306         *size = sizeof(struct lov_mds_md);
307
308         RETURN(sizeof(struct lov_mds_md));
309 }
310
311 /* get lov EA only */
312 static int __mdd_lmm_get(const struct lu_env *env,
313                          struct mdd_object *mdd_obj, struct md_attr *ma)
314 {
315         int rc;
316         ENTRY;
317
318         if (ma->ma_valid & MA_LOV)
319                 RETURN(0);
320
321         rc = mdd_get_md(env, mdd_obj, ma->ma_lmm, &ma->ma_lmm_size,
322                         MDS_LOV_MD_NAME);
323
324         if (rc == 0 && (ma->ma_need & MA_LOV_DEF)) {
325                 rc = mdd_get_default_md(mdd_obj, ma->ma_lmm,
326                                 &ma->ma_lmm_size);
327         }
328
329         if (rc > 0) {
330                 ma->ma_valid |= MA_LOV;
331                 rc = 0;
332         }
333         RETURN(rc);
334 }
335
336 int mdd_lmm_get_locked(const struct lu_env *env, struct mdd_object *mdd_obj,
337                        struct md_attr *ma)
338 {
339         int rc;
340         ENTRY;
341
342         mdd_read_lock(env, mdd_obj);
343         rc = __mdd_lmm_get(env, mdd_obj, ma);
344         mdd_read_unlock(env, mdd_obj);
345         RETURN(rc);
346 }
347
348 /* get lmv EA only*/
349 static int __mdd_lmv_get(const struct lu_env *env,
350                          struct mdd_object *mdd_obj, struct md_attr *ma)
351 {
352         int rc;
353         ENTRY;
354
355         if (ma->ma_valid & MA_LMV)
356                 RETURN(0);
357
358         rc = mdd_get_md(env, mdd_obj, ma->ma_lmv, &ma->ma_lmv_size,
359                         MDS_LMV_MD_NAME);
360         if (rc > 0) {
361                 ma->ma_valid |= MA_LMV;
362                 rc = 0;
363         }
364         RETURN(rc);
365 }
366
367 static int mdd_attr_get_internal(const struct lu_env *env,
368                                  struct mdd_object *mdd_obj,
369                                  struct md_attr *ma)
370 {
371         int rc = 0;
372         ENTRY;
373
374         if (ma->ma_need & MA_INODE)
375                 rc = mdd_iattr_get(env, mdd_obj, ma);
376
377         if (rc == 0 && ma->ma_need & MA_LOV) {
378                 if (S_ISREG(mdd_object_type(mdd_obj)) ||
379                     S_ISDIR(mdd_object_type(mdd_obj)))
380                         rc = __mdd_lmm_get(env, mdd_obj, ma);
381         }
382         if (rc == 0 && ma->ma_need & MA_LMV) {
383                 if (S_ISDIR(mdd_object_type(mdd_obj)))
384                         rc = __mdd_lmv_get(env, mdd_obj, ma);
385         }
386 #ifdef CONFIG_FS_POSIX_ACL
387         if (rc == 0 && ma->ma_need & MA_ACL_DEF) {
388                 if (S_ISDIR(mdd_object_type(mdd_obj)))
389                         rc = mdd_acl_def_get(env, mdd_obj, ma);
390         }
391 #endif
392         CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64"\n",
393                rc, ma->ma_valid);
394         RETURN(rc);
395 }
396
397 int mdd_attr_get_internal_locked(const struct lu_env *env,
398                                  struct mdd_object *mdd_obj, struct md_attr *ma)
399 {
400         int rc;
401         int needlock = ma->ma_need & (MA_LOV | MA_LMV | MA_ACL_DEF);
402
403         if (needlock)
404                 mdd_read_lock(env, mdd_obj);
405         rc = mdd_attr_get_internal(env, mdd_obj, ma);
406         if (needlock)
407                 mdd_read_unlock(env, mdd_obj);
408         return rc;
409 }
410
411 /*
412  * No permission check is needed.
413  */
414 static int mdd_attr_get(const struct lu_env *env, struct md_object *obj,
415                         struct md_attr *ma)
416 {
417         struct mdd_object *mdd_obj = md2mdd_obj(obj);
418         int                rc;
419
420         ENTRY;
421         rc = mdd_attr_get_internal_locked(env, mdd_obj, ma);
422         RETURN(rc);
423 }
424
425 /*
426  * No permission check is needed.
427  */
428 static int mdd_xattr_get(const struct lu_env *env,
429                          struct md_object *obj, struct lu_buf *buf,
430                          const char *name)
431 {
432         struct mdd_object *mdd_obj = md2mdd_obj(obj);
433         int rc;
434
435         ENTRY;
436
437         LASSERT(mdd_object_exists(mdd_obj));
438
439         mdd_read_lock(env, mdd_obj);
440         rc = mdo_xattr_get(env, mdd_obj, buf, name,
441                            mdd_object_capa(env, mdd_obj));
442         mdd_read_unlock(env, mdd_obj);
443
444         RETURN(rc);
445 }
446
447 /*
448  * Permission check is done when open,
449  * no need check again.
450  */
451 static int mdd_readlink(const struct lu_env *env, struct md_object *obj,
452                         struct lu_buf *buf)
453 {
454         struct mdd_object *mdd_obj = md2mdd_obj(obj);
455         struct dt_object  *next;
456         loff_t             pos = 0;
457         int                rc;
458         ENTRY;
459
460         LASSERT(mdd_object_exists(mdd_obj));
461
462         next = mdd_object_child(mdd_obj);
463         mdd_read_lock(env, mdd_obj);
464         rc = next->do_body_ops->dbo_read(env, next, buf, &pos,
465                                          mdd_object_capa(env, mdd_obj));
466         mdd_read_unlock(env, mdd_obj);
467         RETURN(rc);
468 }
469
470 /*
471  * No permission check is needed.
472  */
473 static int mdd_xattr_list(const struct lu_env *env, struct md_object *obj,
474                           struct lu_buf *buf)
475 {
476         struct mdd_object *mdd_obj = md2mdd_obj(obj);
477         int rc;
478
479         ENTRY;
480
481         mdd_read_lock(env, mdd_obj);
482         rc = mdo_xattr_list(env, mdd_obj, buf, mdd_object_capa(env, mdd_obj));
483         mdd_read_unlock(env, mdd_obj);
484
485         RETURN(rc);
486 }
487
488 int mdd_object_create_internal(const struct lu_env *env, struct mdd_object *p,
489                                struct mdd_object *c, struct md_attr *ma,
490                                struct thandle *handle)
491 {
492         struct lu_attr *attr = &ma->ma_attr;
493         struct dt_allocation_hint *hint = &mdd_env_info(env)->mti_hint;
494         int rc;
495         ENTRY;
496
497         if (!mdd_object_exists(c)) {
498                 struct dt_object *next = mdd_object_child(c);
499                 LASSERT(next);
500
501                 /* @hint will be initialized by underlying device. */
502                 next->do_ops->do_ah_init(env, hint,
503                                          p ? mdd_object_child(p) : NULL,
504                                          attr->la_mode & S_IFMT);
505                 rc = mdo_create_obj(env, c, attr, hint, handle);
506                 LASSERT(ergo(rc == 0, mdd_object_exists(c)));
507         } else
508                 rc = -EEXIST;
509
510         RETURN(rc);
511 }
512
513 static inline int mdd_attr_check(const struct lu_env *env,
514                                  struct mdd_object *obj,
515                                  struct lu_attr *attr)
516 {
517         struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
518         int rc;
519         ENTRY;
520
521         if (attr->la_valid & LA_CTIME) {
522                 rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
523                 if (rc)
524                         RETURN(rc);
525
526                 if (attr->la_ctime < tmp_la->la_ctime)
527                         attr->la_valid &= ~(LA_MTIME | LA_CTIME);
528                 else if (attr->la_valid == LA_CTIME &&
529                          attr->la_ctime == tmp_la->la_ctime)
530                         attr->la_valid &= ~LA_CTIME;
531         }
532         RETURN(0);
533 }
534
535 int mdd_attr_set_internal(const struct lu_env *env,
536                           struct mdd_object *obj,
537                           struct lu_attr *attr,
538                           struct thandle *handle,
539                           int needacl)
540 {
541         int rc;
542         ENTRY;
543
544         rc = mdo_attr_set(env, obj, attr, handle, mdd_object_capa(env, obj));
545 #ifdef CONFIG_FS_POSIX_ACL
546         if (!rc && (attr->la_valid & LA_MODE) && needacl)
547                 rc = mdd_acl_chmod(env, obj, attr->la_mode, handle);
548 #endif
549         RETURN(rc);
550 }
551
552 int mdd_attr_check_set_internal(const struct lu_env *env,
553                                 struct mdd_object *obj,
554                                 struct lu_attr *attr,
555                                 struct thandle *handle,
556                                 int needacl)
557 {
558         int rc;
559         ENTRY;
560
561         rc = mdd_attr_check(env, obj, attr);
562         if (rc)
563                 RETURN(rc);
564
565         if (attr->la_valid)
566                 rc = mdd_attr_set_internal(env, obj, attr, handle, needacl);
567         RETURN(rc);
568 }
569
570 static int mdd_attr_set_internal_locked(const struct lu_env *env,
571                                         struct mdd_object *obj,
572                                         struct lu_attr *attr,
573                                         struct thandle *handle,
574                                         int needacl)
575 {
576         int rc;
577         ENTRY;
578
579         needacl = needacl && (attr->la_valid & LA_MODE);
580
581         if (needacl)
582                 mdd_write_lock(env, obj);
583
584         rc = mdd_attr_set_internal(env, obj, attr, handle, needacl);
585
586         if (needacl)
587                 mdd_write_unlock(env, obj);
588         RETURN(rc);
589 }
590
591 int mdd_attr_check_set_internal_locked(const struct lu_env *env,
592                                        struct mdd_object *obj,
593                                        struct lu_attr *attr,
594                                        struct thandle *handle,
595                                        int needacl)
596 {
597         int rc;
598         ENTRY;
599
600         rc = mdd_attr_check(env, obj, attr);
601         if (rc)
602                 RETURN(rc);
603
604         if (attr->la_valid)
605                 rc = mdd_attr_set_internal_locked(env, obj, attr, handle,
606                                                   needacl);
607         RETURN(rc);
608 }
609
610 static int __mdd_xattr_set(const struct lu_env *env, struct mdd_object *obj,
611                            const struct lu_buf *buf, const char *name,
612                            int fl, struct thandle *handle)
613 {
614         struct lustre_capa *capa = mdd_object_capa(env, obj);
615         int rc = -EINVAL;
616         ENTRY;
617
618         if (buf->lb_buf && buf->lb_len > 0)
619                 rc = mdo_xattr_set(env, obj, buf, name, 0, handle, capa);
620         else if (buf->lb_buf == NULL && buf->lb_len == 0)
621                 rc = mdo_xattr_del(env, obj, name, handle, capa);
622
623         RETURN(rc);
624 }
625
626 /*
627  * This gives the same functionality as the code between
628  * sys_chmod and inode_setattr
629  * chown_common and inode_setattr
630  * utimes and inode_setattr
631  * This API is ported from mds_fix_attr but remove some unnecesssary stuff.
632  */
633 static int mdd_fix_attr(const struct lu_env *env, struct mdd_object *obj,
634                         struct lu_attr *la, const struct md_attr *ma)
635 {
636         struct lu_attr   *tmp_la     = &mdd_env_info(env)->mti_la;
637         struct md_ucred  *uc         = md_ucred(env);
638         int               rc;
639         ENTRY;
640
641         if (!la->la_valid)
642                 RETURN(0);
643
644         /* Do not permit change file type */
645         if (la->la_valid & LA_TYPE)
646                 RETURN(-EPERM);
647
648         /* They should not be processed by setattr */
649         if (la->la_valid & (LA_NLINK | LA_RDEV | LA_BLKSIZE))
650                 RETURN(-EPERM);
651
652         rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
653         if (rc)
654                 RETURN(rc);
655
656         /* This is only for set ctime when rename's source is on remote MDS. */
657         if (unlikely(la->la_valid == LA_CTIME)) {
658                 rc = mdd_may_delete(env, NULL, obj, (struct md_attr *)ma, 1, 0);
659                 if (rc == 0 && la->la_ctime <= tmp_la->la_ctime)
660                         la->la_valid &= ~LA_CTIME;
661                 RETURN(rc);
662         }
663
664         if (la->la_valid == LA_ATIME) {
665                 /* This is atime only set for read atime update on close. */
666                 if (la->la_atime <= tmp_la->la_atime +
667                                     mdd_obj2mdd_dev(obj)->mdd_atime_diff)
668                         la->la_valid &= ~LA_ATIME;
669                 RETURN(0);
670         }
671  
672         /* Check if flags change. */
673         if (la->la_valid & LA_FLAGS) {
674                 unsigned int oldflags = 0;
675                 unsigned int newflags = la->la_flags &
676                                 (LUSTRE_IMMUTABLE_FL | LUSTRE_APPEND_FL);
677
678                 if ((uc->mu_fsuid != tmp_la->la_uid) &&
679                     !mdd_capable(uc, CAP_FOWNER))
680                         RETURN(-EPERM);
681
682                 /* XXX: the IMMUTABLE and APPEND_ONLY flags can
683                  * only be changed by the relevant capability. */
684                 if (mdd_is_immutable(obj))
685                         oldflags |= LUSTRE_IMMUTABLE_FL;
686                 if (mdd_is_append(obj))
687                         oldflags |= LUSTRE_APPEND_FL; 
688                 if ((oldflags ^ newflags) &&
689                     !mdd_capable(uc, CAP_LINUX_IMMUTABLE))
690                         RETURN(-EPERM);
691
692                 if (!S_ISDIR(tmp_la->la_mode))
693                         la->la_flags &= ~LUSTRE_DIRSYNC_FL;
694         }
695
696         if ((mdd_is_immutable(obj) || mdd_is_append(obj)) &&
697             (la->la_valid & ~LA_FLAGS) &&
698             !(ma->ma_attr_flags & MDS_PERM_BYPASS))
699                 RETURN(-EPERM);
700
701         /* Check for setting the obj time. */
702         if ((la->la_valid & (LA_MTIME | LA_ATIME | LA_CTIME)) &&
703             !(la->la_valid & ~(LA_MTIME | LA_ATIME | LA_CTIME))) {
704                 if ((uc->mu_fsuid != tmp_la->la_uid) &&
705                     !mdd_capable(uc, CAP_FOWNER)) {
706                         rc = mdd_permission_internal_locked(env, obj, tmp_la,
707                                                             MAY_WRITE);
708                         if (rc)
709                                 RETURN(rc);
710                 }
711         }
712
713         /* Make sure a caller can chmod. */
714         if (la->la_valid & LA_MODE) {
715                 /*
716                  * Bypass la_vaild == LA_MODE,
717                  * this is for changing file with SUID or SGID.
718                  */
719                 if ((la->la_valid & ~LA_MODE) &&
720                     (uc->mu_fsuid != tmp_la->la_uid) &&
721                     !mdd_capable(uc, CAP_FOWNER))
722                         RETURN(-EPERM);
723
724                 if (la->la_mode == (umode_t) -1)
725                         la->la_mode = tmp_la->la_mode;
726                 else
727                         la->la_mode = (la->la_mode & S_IALLUGO) |
728                                       (tmp_la->la_mode & ~S_IALLUGO);
729
730                 /* Also check the setgid bit! */
731                 if (!lustre_in_group_p(uc, (la->la_valid & LA_GID) ? la->la_gid :
732                                 tmp_la->la_gid) && !mdd_capable(uc, CAP_FSETID))
733                         la->la_mode &= ~S_ISGID;
734         } else {
735                la->la_mode = tmp_la->la_mode;
736         }
737
738         /* Make sure a caller can chown. */
739         if (la->la_valid & LA_UID) {
740                 if (la->la_uid == (uid_t) -1)
741                         la->la_uid = tmp_la->la_uid;
742                 if (((uc->mu_fsuid != tmp_la->la_uid) ||
743                     (la->la_uid != tmp_la->la_uid)) &&
744                     !mdd_capable(uc, CAP_CHOWN))
745                         RETURN(-EPERM);
746
747                 /*
748                  * If the user or group of a non-directory has been
749                  * changed by a non-root user, remove the setuid bit.
750                  * 19981026 David C Niemi <niemi@tux.org>
751                  *
752                  * Changed this to apply to all users, including root,
753                  * to avoid some races. This is the behavior we had in
754                  * 2.0. The check for non-root was definitely wrong
755                  * for 2.2 anyway, as it should have been using
756                  * CAP_FSETID rather than fsuid -- 19990830 SD.
757                  */
758                 if (((tmp_la->la_mode & S_ISUID) == S_ISUID) &&
759                     !S_ISDIR(tmp_la->la_mode)) {
760                         la->la_mode &= ~S_ISUID;
761                         la->la_valid |= LA_MODE;
762                 }
763         }
764
765         /* Make sure caller can chgrp. */
766         if (la->la_valid & LA_GID) {
767                 if (la->la_gid == (gid_t) -1)
768                         la->la_gid = tmp_la->la_gid;
769                 if (((uc->mu_fsuid != tmp_la->la_uid) ||
770                     ((la->la_gid != tmp_la->la_gid) &&
771                     !lustre_in_group_p(uc, la->la_gid))) &&
772                     !mdd_capable(uc, CAP_CHOWN))
773                         RETURN(-EPERM);
774
775                 /*
776                  * Likewise, if the user or group of a non-directory
777                  * has been changed by a non-root user, remove the
778                  * setgid bit UNLESS there is no group execute bit
779                  * (this would be a file marked for mandatory
780                  * locking).  19981026 David C Niemi <niemi@tux.org>
781                  *
782                  * Removed the fsuid check (see the comment above) --
783                  * 19990830 SD.
784                  */
785                 if (((tmp_la->la_mode & (S_ISGID | S_IXGRP)) ==
786                      (S_ISGID | S_IXGRP)) && !S_ISDIR(tmp_la->la_mode)) {
787                         la->la_mode &= ~S_ISGID;
788                         la->la_valid |= LA_MODE;
789                 }
790         }
791
792         if (la->la_valid & (LA_SIZE | LA_BLOCKS) &&
793             !(la->la_valid & LA_TRUNC)) {
794                 /* For "Size-on-MDS" case, the MAY_WRITE perm
795                  * has been checked when file open. */
796
797                 /* For the "Size-on-MDS" setattr update, merge coming
798                  * attributes with the set in the inode. BUG 10641 */
799                 if ((la->la_valid & LA_ATIME) &&
800                     (la->la_atime <= tmp_la->la_atime))
801                         la->la_valid &= ~LA_ATIME;
802
803                 /* OST attributes do not have a priority over MDS attributes,
804                  * so drop times if ctime is equal. */
805                 if ((la->la_valid & LA_CTIME) &&
806                     (la->la_ctime <= tmp_la->la_ctime))
807                         la->la_valid &= ~(LA_MTIME | LA_CTIME);
808         } else {
809                 if (la->la_valid & LA_TRUNC) {
810                         /* For truncate, we should have MAY_WRITE perm. */
811                         if (!((la->la_valid & MDS_OPEN_OWNEROVERRIDE) &&
812                               (uc->mu_fsuid == tmp_la->la_uid)) &&
813                             !(ma->ma_attr_flags & MDS_PERM_BYPASS)) {
814                                 rc = mdd_permission_internal_locked(env, obj,
815                                                             tmp_la, MAY_WRITE);
816                                 if (rc)
817                                         RETURN(rc);
818                         }
819                 }
820                 if (la->la_valid & LA_CTIME) {
821                         /* The pure setattr, it has the priority over what is
822                          * already set, do not drop it if ctime is equal. */
823                         if (la->la_ctime < tmp_la->la_ctime)
824                                 la->la_valid &= ~(LA_ATIME | LA_MTIME |
825                                                   LA_CTIME);
826                 }
827         }
828
829         RETURN(0);
830 }
831
832 /* set attr and LOV EA at once, return updated attr */
833 static int mdd_attr_set(const struct lu_env *env, struct md_object *obj,
834                         const struct md_attr *ma)
835 {
836         struct mdd_object *mdd_obj = md2mdd_obj(obj);
837         struct mdd_device *mdd = mdo2mdd(obj);
838         struct thandle *handle;
839         struct lov_mds_md *lmm = NULL;
840         struct llog_cookie *logcookies = NULL;
841         int  rc, lmm_size = 0, cookie_size = 0;
842         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
843         ENTRY;
844
845         mdd_txn_param_build(env, mdd, MDD_TXN_ATTR_SET_OP);
846         handle = mdd_trans_start(env, mdd);
847         if (IS_ERR(handle))
848                 RETURN(PTR_ERR(handle));
849         /*TODO: add lock here*/
850         /* start a log jounal handle if needed */
851         if (S_ISREG(mdd_object_type(mdd_obj)) &&
852             ma->ma_attr.la_valid & (LA_UID | LA_GID)) {
853                 lmm_size = mdd_lov_mdsize(env, mdd);
854                 lmm = mdd_max_lmm_get(env, mdd);
855                 if (lmm == NULL)
856                         GOTO(cleanup, rc = -ENOMEM);
857
858                 rc = mdd_get_md_locked(env, mdd_obj, lmm, &lmm_size,
859                                 MDS_LOV_MD_NAME);
860
861                 if (rc < 0)
862                         GOTO(cleanup, rc);
863         }
864
865         if (ma->ma_attr.la_valid & (ATTR_MTIME | ATTR_CTIME))
866                 CDEBUG(D_INODE, "setting mtime "LPU64", ctime "LPU64"\n",
867                        ma->ma_attr.la_mtime, ma->ma_attr.la_ctime);
868
869         *la_copy = ma->ma_attr;
870         rc = mdd_fix_attr(env, mdd_obj, la_copy, ma);
871         if (rc)
872                 GOTO(cleanup, rc);
873
874         if (la_copy->la_valid & LA_FLAGS) {
875                 rc = mdd_attr_set_internal_locked(env, mdd_obj, la_copy,
876                                                   handle, 1);
877                 if (rc == 0)
878                         mdd_flags_xlate(mdd_obj, la_copy->la_flags);
879         } else if (la_copy->la_valid) {            /* setattr */
880                 rc = mdd_attr_set_internal_locked(env, mdd_obj, la_copy,
881                                                   handle, 1);
882                 /* journal chown/chgrp in llog, just like unlink */
883                 if (rc == 0 && lmm_size){
884                         cookie_size = mdd_lov_cookiesize(env, mdd);
885                         logcookies = mdd_max_cookie_get(env, mdd);
886                         if (logcookies == NULL)
887                                 GOTO(cleanup, rc = -ENOMEM);
888
889                         if (mdd_setattr_log(env, mdd, ma, lmm, lmm_size,
890                                             logcookies, cookie_size) <= 0)
891                                 logcookies = NULL;
892                 }
893         }
894
895         if (rc == 0 && ma->ma_valid & MA_LOV) {
896                 umode_t mode;
897
898                 mode = mdd_object_type(mdd_obj);
899                 if (S_ISREG(mode) || S_ISDIR(mode)) {
900                         rc = mdd_lsm_sanity_check(env, mdd_obj);
901                         if (rc)
902                                 GOTO(cleanup, rc);
903
904                         rc = mdd_lov_set_md(env, NULL, mdd_obj, ma->ma_lmm,
905                                             ma->ma_lmm_size, handle, 1);
906                 }
907
908         }
909 cleanup:
910         mdd_trans_stop(env, mdd, rc, handle);
911         if (rc == 0 && (lmm != NULL && lmm_size > 0 )) {
912                 /*set obd attr, if needed*/
913                 rc = mdd_lov_setattr_async(env, mdd_obj, lmm, lmm_size,
914                                            logcookies);
915         }
916         RETURN(rc);
917 }
918
919 int mdd_xattr_set_txn(const struct lu_env *env, struct mdd_object *obj,
920                       const struct lu_buf *buf, const char *name, int fl,
921                       struct thandle *handle)
922 {
923         int  rc;
924         ENTRY;
925
926         mdd_write_lock(env, obj);
927         rc = __mdd_xattr_set(env, obj, buf, name, fl, handle);
928         mdd_write_unlock(env, obj);
929
930         RETURN(rc);
931 }
932
933 static int mdd_xattr_sanity_check(const struct lu_env *env,
934                                   struct mdd_object *obj)
935 {
936         struct lu_attr  *tmp_la = &mdd_env_info(env)->mti_la;
937         struct md_ucred *uc     = md_ucred(env);
938         int rc;
939         ENTRY;
940
941         if (mdd_is_immutable(obj) || mdd_is_append(obj))
942                 RETURN(-EPERM);
943
944         rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
945         if (rc)
946                 RETURN(rc);
947
948         if ((uc->mu_fsuid != tmp_la->la_uid) && !mdd_capable(uc, CAP_FOWNER))
949                 RETURN(-EPERM);
950
951         RETURN(rc);
952 }
953
954 static int mdd_xattr_set(const struct lu_env *env, struct md_object *obj,
955                          const struct lu_buf *buf, const char *name,
956                          int fl, const struct lu_attr *la)
957 {
958         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
959         struct mdd_object *mdd_obj = md2mdd_obj(obj);
960         struct mdd_device *mdd = mdo2mdd(obj);
961         struct thandle *handle;
962         int  rc;
963         ENTRY;
964
965         rc = mdd_xattr_sanity_check(env, mdd_obj);
966         if (rc)
967                 RETURN(rc);
968
969         mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
970         handle = mdd_trans_start(env, mdd);
971         if (IS_ERR(handle))
972                 RETURN(PTR_ERR(handle));
973
974         rc = mdd_xattr_set_txn(env, mdd_obj, buf, name, fl, handle);
975         if (rc == 0 && likely(la != NULL)) {
976                 LASSERT(la->la_valid & LA_CTIME);
977                 la_copy->la_ctime = la->la_ctime;
978                 la_copy->la_valid = LA_CTIME;
979                 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la_copy,
980                                                         handle, 0);
981         }
982         mdd_trans_stop(env, mdd, rc, handle);
983
984         RETURN(rc);
985 }
986
987 int mdd_xattr_del(const struct lu_env *env, struct md_object *obj,
988                   const char *name, const struct lu_attr *la)
989 {
990         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
991         struct mdd_object *mdd_obj = md2mdd_obj(obj);
992         struct mdd_device *mdd = mdo2mdd(obj);
993         struct thandle *handle;
994         int  rc;
995         ENTRY;
996
997         rc = mdd_xattr_sanity_check(env, mdd_obj);
998         if (rc)
999                 RETURN(rc);
1000
1001         mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
1002         handle = mdd_trans_start(env, mdd);
1003         if (IS_ERR(handle))
1004                 RETURN(PTR_ERR(handle));
1005
1006         mdd_write_lock(env, mdd_obj);
1007         rc = mdo_xattr_del(env, mdd_obj, name, handle,
1008                            mdd_object_capa(env, mdd_obj));
1009         mdd_write_unlock(env, mdd_obj);
1010         if (rc == 0 && likely(la != NULL)) {
1011                 LASSERT(la->la_valid & LA_CTIME);
1012                 la_copy->la_ctime = la->la_ctime;
1013                 la_copy->la_valid = LA_CTIME;
1014                 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la_copy,
1015                                                         handle, 0);
1016         }
1017
1018         mdd_trans_stop(env, mdd, rc, handle);
1019
1020         RETURN(rc);
1021 }
1022
1023 /* partial unlink */
1024 static int mdd_ref_del(const struct lu_env *env, struct md_object *obj,
1025                        struct md_attr *ma)
1026 {
1027         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1028         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1029         struct mdd_device *mdd = mdo2mdd(obj);
1030         struct thandle *handle;
1031         int rc;
1032         ENTRY;
1033
1034         /*
1035          * Check -ENOENT early here because we need to get object type
1036          * to calculate credits before transaction start
1037          */
1038         if (!mdd_object_exists(mdd_obj))
1039                 RETURN(-ENOENT);
1040
1041         LASSERT(mdd_object_exists(mdd_obj) > 0);
1042
1043         rc = mdd_log_txn_param_build(env, obj, ma, MDD_TXN_UNLINK_OP);
1044         if (rc)
1045                 RETURN(rc);
1046
1047         handle = mdd_trans_start(env, mdd);
1048         if (IS_ERR(handle))
1049                 RETURN(-ENOMEM);
1050
1051         mdd_write_lock(env, mdd_obj);
1052
1053         rc = mdd_unlink_sanity_check(env, NULL, mdd_obj, ma);
1054         if (rc)
1055                 GOTO(cleanup, rc);
1056
1057         mdo_ref_del(env, mdd_obj, handle);
1058
1059         if (S_ISDIR(lu_object_attr(&obj->mo_lu))) {
1060                 /* unlink dot */
1061                 mdo_ref_del(env, mdd_obj, handle);
1062         }
1063
1064         LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1065         la_copy->la_ctime = ma->ma_attr.la_ctime;
1066
1067         la_copy->la_valid = LA_CTIME;
1068         rc = mdd_attr_check_set_internal(env, mdd_obj, la_copy, handle, 0);
1069         if (rc)
1070                 GOTO(cleanup, rc);
1071
1072         rc = mdd_finish_unlink(env, mdd_obj, ma, handle);
1073
1074         EXIT;
1075 cleanup:
1076         mdd_write_unlock(env, mdd_obj);
1077         mdd_trans_stop(env, mdd, rc, handle);
1078         return rc;
1079 }
1080
1081 /* partial operation */
1082 static int mdd_oc_sanity_check(const struct lu_env *env,
1083                                struct mdd_object *obj,
1084                                struct md_attr *ma)
1085 {
1086         int rc;
1087         ENTRY;
1088
1089         switch (ma->ma_attr.la_mode & S_IFMT) {
1090         case S_IFREG:
1091         case S_IFDIR:
1092         case S_IFLNK:
1093         case S_IFCHR:
1094         case S_IFBLK:
1095         case S_IFIFO:
1096         case S_IFSOCK:
1097                 rc = 0;
1098                 break;
1099         default:
1100                 rc = -EINVAL;
1101                 break;
1102         }
1103         RETURN(rc);
1104 }
1105
1106 static int mdd_object_create(const struct lu_env *env,
1107                              struct md_object *obj,
1108                              const struct md_op_spec *spec,
1109                              struct md_attr *ma)
1110 {
1111
1112         struct mdd_device *mdd = mdo2mdd(obj);
1113         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1114         const struct lu_fid *pfid = spec->u.sp_pfid;
1115         struct thandle *handle;
1116         int rc;
1117         ENTRY;
1118
1119         mdd_txn_param_build(env, mdd, MDD_TXN_OBJECT_CREATE_OP);
1120         handle = mdd_trans_start(env, mdd);
1121         if (IS_ERR(handle))
1122                 RETURN(PTR_ERR(handle));
1123
1124         mdd_write_lock(env, mdd_obj);
1125         rc = mdd_oc_sanity_check(env, mdd_obj, ma);
1126         if (rc)
1127                 GOTO(unlock, rc);
1128
1129         rc = mdd_object_create_internal(env, NULL, mdd_obj, ma, handle);
1130         if (rc)
1131                 GOTO(unlock, rc);
1132
1133         if (spec->sp_cr_flags & MDS_CREATE_SLAVE_OBJ) {
1134                 /* If creating the slave object, set slave EA here. */
1135                 int lmv_size = spec->u.sp_ea.eadatalen;
1136                 struct lmv_stripe_md *lmv;
1137
1138                 lmv = (struct lmv_stripe_md *)spec->u.sp_ea.eadata;
1139                 LASSERT(lmv != NULL && lmv_size > 0);
1140
1141                 rc = __mdd_xattr_set(env, mdd_obj,
1142                                      mdd_buf_get_const(env, lmv, lmv_size),
1143                                      MDS_LMV_MD_NAME, 0, handle);
1144                 if (rc)
1145                         GOTO(unlock, rc);
1146
1147                 rc = mdd_attr_set_internal(env, mdd_obj, &ma->ma_attr,
1148                                            handle, 0);
1149         } else {
1150 #ifdef CONFIG_FS_POSIX_ACL
1151                 if (spec->sp_cr_flags & MDS_CREATE_RMT_ACL) {
1152                         struct lu_buf *buf = &mdd_env_info(env)->mti_buf;
1153
1154                         buf->lb_buf = (void *)spec->u.sp_ea.eadata;
1155                         buf->lb_len = spec->u.sp_ea.eadatalen;
1156                         if ((buf->lb_len > 0) && (buf->lb_buf != NULL)) {
1157                                 rc = __mdd_acl_init(env, mdd_obj, buf,
1158                                                     &ma->ma_attr.la_mode,
1159                                                     handle);
1160                                 if (rc)
1161                                         GOTO(unlock, rc);
1162                                 else
1163                                         ma->ma_attr.la_valid |= LA_MODE;
1164                         }
1165
1166                         pfid = spec->u.sp_ea.fid;
1167                 }
1168 #endif
1169                 rc = mdd_object_initialize(env, pfid, mdd_obj, ma, handle);
1170         }
1171         EXIT;
1172 unlock:
1173         mdd_write_unlock(env, mdd_obj);
1174         if (rc == 0)
1175                 rc = mdd_attr_get_internal_locked(env, mdd_obj, ma);
1176
1177         mdd_trans_stop(env, mdd, rc, handle);
1178         return rc;
1179 }
1180
1181 /* partial link */
1182 static int mdd_ref_add(const struct lu_env *env, struct md_object *obj,
1183                        const struct md_attr *ma)
1184 {
1185         struct lu_attr *la_copy = &mdd_env_info(env)->mti_la_for_fix;
1186         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1187         struct mdd_device *mdd = mdo2mdd(obj);
1188         struct thandle *handle;
1189         int rc;
1190         ENTRY;
1191
1192         mdd_txn_param_build(env, mdd, MDD_TXN_XATTR_SET_OP);
1193         handle = mdd_trans_start(env, mdd);
1194         if (IS_ERR(handle))
1195                 RETURN(-ENOMEM);
1196
1197         mdd_write_lock(env, mdd_obj);
1198         rc = mdd_link_sanity_check(env, NULL, NULL, mdd_obj);
1199         if (rc == 0)
1200                 mdo_ref_add(env, mdd_obj, handle);
1201         mdd_write_unlock(env, mdd_obj);
1202         if (rc == 0) {
1203                 LASSERT(ma->ma_attr.la_valid & LA_CTIME);
1204                 la_copy->la_ctime = ma->ma_attr.la_ctime;
1205
1206                 la_copy->la_valid = LA_CTIME;
1207                 rc = mdd_attr_check_set_internal_locked(env, mdd_obj, la_copy,
1208                                                         handle, 0);
1209         }
1210         mdd_trans_stop(env, mdd, 0, handle);
1211
1212         RETURN(rc);
1213 }
1214
1215 /*
1216  * do NOT or the MAY_*'s, you'll get the weakest
1217  */
1218 int accmode(const struct lu_env *env, struct lu_attr *la, int flags)
1219 {
1220         int res = 0;
1221
1222         /* Sadly, NFSD reopens a file repeatedly during operation, so the
1223          * "acc_mode = 0" allowance for newly-created files isn't honoured.
1224          * NFSD uses the MDS_OPEN_OWNEROVERRIDE flag to say that a file
1225          * owner can write to a file even if it is marked readonly to hide
1226          * its brokenness. (bug 5781) */
1227         if (flags & MDS_OPEN_OWNEROVERRIDE) {
1228                 struct md_ucred *uc = md_ucred(env);
1229
1230                 if ((uc == NULL) || (uc->mu_valid == UCRED_INIT) ||
1231                     (la->la_uid == uc->mu_fsuid))
1232                         return 0;
1233         }
1234
1235         if (flags & FMODE_READ)
1236                 res |= MAY_READ;
1237         if (flags & (FMODE_WRITE | MDS_OPEN_TRUNC | MDS_OPEN_APPEND))
1238                 res |= MAY_WRITE;
1239         if (flags & MDS_FMODE_EXEC)
1240                 res |= MAY_EXEC;
1241         return res;
1242 }
1243
1244 static int mdd_open_sanity_check(const struct lu_env *env,
1245                                  struct mdd_object *obj, int flag)
1246 {
1247         struct lu_attr *tmp_la = &mdd_env_info(env)->mti_la;
1248         int mode, rc;
1249         ENTRY;
1250
1251         /* EEXIST check */
1252         if (mdd_is_dead_obj(obj))
1253                 RETURN(-ENOENT);
1254
1255         rc = mdd_la_get(env, obj, tmp_la, BYPASS_CAPA);
1256         if (rc)
1257                RETURN(rc);
1258
1259         if (S_ISLNK(tmp_la->la_mode))
1260                 RETURN(-ELOOP);
1261
1262         mode = accmode(env, tmp_la, flag);
1263
1264         if (S_ISDIR(tmp_la->la_mode) && (mode & MAY_WRITE))
1265                 RETURN(-EISDIR);
1266
1267         if (!(flag & MDS_OPEN_CREATED)) {
1268                 rc = mdd_permission_internal(env, obj, tmp_la, mode);
1269                 if (rc)
1270                         RETURN(rc);
1271         }
1272
1273         if (S_ISFIFO(tmp_la->la_mode) || S_ISSOCK(tmp_la->la_mode) ||
1274             S_ISBLK(tmp_la->la_mode) || S_ISCHR(tmp_la->la_mode))
1275                 flag &= ~MDS_OPEN_TRUNC;
1276
1277         /* For writing append-only file must open it with append mode. */
1278         if (mdd_is_append(obj)) {
1279                 if ((flag & FMODE_WRITE) && !(flag & MDS_OPEN_APPEND))
1280                         RETURN(-EPERM);
1281                 if (flag & MDS_OPEN_TRUNC)
1282                         RETURN(-EPERM);
1283         }
1284
1285 #if 0
1286         /*
1287          * Now, flag -- O_NOATIME does not be packed by client.
1288          */
1289         if (flag & O_NOATIME) {
1290                 struct md_ucred *uc = md_ucred(env);
1291
1292                 if (uc && ((uc->mu_valid == UCRED_OLD) ||
1293                     (uc->mu_valid == UCRED_NEW)) &&
1294                     (uc->mu_fsuid != tmp_la->la_uid) &&
1295                     !mdd_capable(uc, CAP_FOWNER))
1296                         RETURN(-EPERM);
1297         }
1298 #endif
1299
1300         RETURN(0);
1301 }
1302
1303 static int mdd_open(const struct lu_env *env, struct md_object *obj,
1304                     int flags)
1305 {
1306         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1307         int rc = 0;
1308
1309         mdd_write_lock(env, mdd_obj);
1310
1311         rc = mdd_open_sanity_check(env, mdd_obj, flags);
1312         if (rc == 0)
1313                 mdd_obj->mod_count++;
1314
1315         mdd_write_unlock(env, mdd_obj);
1316         return rc;
1317 }
1318
1319 /* return md_attr back,
1320  * if it is last unlink then return lov ea + llog cookie*/
1321 int mdd_object_kill(const struct lu_env *env, struct mdd_object *obj,
1322                     struct md_attr *ma)
1323 {
1324         int rc = 0;
1325         ENTRY;
1326
1327         if (S_ISREG(mdd_object_type(obj))) {
1328                 /* Return LOV & COOKIES unconditionally here. We clean evth up.
1329                  * Caller must be ready for that. */
1330                 rc = __mdd_lmm_get(env, obj, ma);
1331                 if ((ma->ma_valid & MA_LOV))
1332                         rc = mdd_unlink_log(env, mdo2mdd(&obj->mod_obj),
1333                                             obj, ma);
1334         }
1335         RETURN(rc);
1336 }
1337
1338 /*
1339  * No permission check is needed.
1340  */
1341 static int mdd_close(const struct lu_env *env, struct md_object *obj,
1342                      struct md_attr *ma)
1343 {
1344         int rc;
1345         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1346         struct thandle    *handle;
1347         ENTRY;
1348
1349         rc = mdd_log_txn_param_build(env, obj, ma, MDD_TXN_UNLINK_OP);
1350         if (rc)
1351                 RETURN(rc);
1352         handle = mdd_trans_start(env, mdo2mdd(obj));
1353         if (IS_ERR(handle))
1354                 RETURN(PTR_ERR(handle));
1355
1356         mdd_write_lock(env, mdd_obj);
1357         /* release open count */
1358         mdd_obj->mod_count --;
1359
1360         rc = mdd_iattr_get(env, mdd_obj, ma);
1361         if (rc == 0 && mdd_obj->mod_count == 0 && ma->ma_attr.la_nlink == 0)
1362                 rc = mdd_object_kill(env, mdd_obj, ma);
1363         else
1364                 ma->ma_valid &= ~(MA_LOV | MA_COOKIE);
1365         
1366         mdd_write_unlock(env, mdd_obj);
1367         mdd_trans_stop(env, mdo2mdd(obj), rc, handle);
1368         RETURN(rc);
1369 }
1370
1371 /*
1372  * Permission check is done when open,
1373  * no need check again.
1374  */
1375 static int mdd_readpage_sanity_check(const struct lu_env *env,
1376                                      struct mdd_object *obj)
1377 {
1378         struct dt_object *next = mdd_object_child(obj);
1379         int rc;
1380         ENTRY;
1381
1382         if (S_ISDIR(mdd_object_type(obj)) && dt_try_as_dir(env, next))
1383                 rc = 0;
1384         else
1385                 rc = -ENOTDIR;
1386
1387         RETURN(rc);
1388 }
1389
1390 static int mdd_dir_page_build(const struct lu_env *env, int first,
1391                               void *area, int nob, struct dt_it_ops *iops,
1392                               struct dt_it *it, __u64 *start, __u64 *end,
1393                               struct lu_dirent **last)
1394 {
1395         struct lu_fid          *fid  = &mdd_env_info(env)->mti_fid2;
1396         struct mdd_thread_info *info = mdd_env_info(env);
1397         struct lu_fid_pack     *pack = &info->mti_pack;
1398         int                     result;
1399         struct lu_dirent       *ent;
1400
1401         if (first) {
1402                 memset(area, 0, sizeof (struct lu_dirpage));
1403                 area += sizeof (struct lu_dirpage);
1404                 nob  -= sizeof (struct lu_dirpage);
1405         }
1406
1407         LASSERT(nob > sizeof *ent);
1408
1409         ent  = area;
1410         result = 0;
1411         do {
1412                 char  *name;
1413                 int    len;
1414                 int    recsize;
1415                 __u64  hash;
1416
1417                 name = (char *)iops->key(env, it);
1418                 len  = iops->key_size(env, it);
1419
1420                 pack = (struct lu_fid_pack *)iops->rec(env, it);
1421                 result = fid_unpack(pack, fid);
1422                 if (result != 0)
1423                         break;
1424
1425                 recsize = (sizeof(*ent) + len + 7) & ~7;
1426                 hash = iops->store(env, it);
1427                 *end = hash;
1428
1429                 CDEBUG(D_INFO, "%p %p %d "DFID": "LPU64" (%d) \"%*.*s\"\n",
1430                        name, ent, nob, PFID(fid), hash, len, len, len, name);
1431
1432                 if (nob >= recsize) {
1433                         ent->lde_fid = *fid;
1434                         fid_cpu_to_le(&ent->lde_fid, &ent->lde_fid);
1435                         ent->lde_hash = hash;
1436                         ent->lde_namelen = cpu_to_le16(len);
1437                         ent->lde_reclen  = cpu_to_le16(recsize);
1438                         memcpy(ent->lde_name, name, len);
1439                         if (first && ent == area)
1440                                 *start = hash;
1441                         *last = ent;
1442                         ent = (void *)ent + recsize;
1443                         nob -= recsize;
1444                         result = iops->next(env, it);
1445                 } else {
1446                         /*
1447                          * record doesn't fit into page, enlarge previous one.
1448                          */
1449                         LASSERT(*last != NULL);
1450                         (*last)->lde_reclen =
1451                                 cpu_to_le16(le16_to_cpu((*last)->lde_reclen) +
1452                                             nob);
1453                         break;
1454                 }
1455         } while (result == 0);
1456
1457         return result;
1458 }
1459
1460 static int __mdd_readpage(const struct lu_env *env, struct mdd_object *obj,
1461                           const struct lu_rdpg *rdpg)
1462 {
1463         struct dt_it      *it;
1464         struct dt_object  *next = mdd_object_child(obj);
1465         struct dt_it_ops  *iops;
1466         struct page       *pg;
1467         struct lu_dirent  *last;
1468         int i;
1469         int rc;
1470         int nob;
1471         __u64 hash_start;
1472         __u64 hash_end;
1473
1474         LASSERT(rdpg->rp_pages != NULL);
1475         LASSERT(next->do_index_ops != NULL);
1476
1477         if (rdpg->rp_count <= 0)
1478                 return -EFAULT;
1479
1480         /*
1481          * iterate through directory and fill pages from @rdpg
1482          */
1483         iops = &next->do_index_ops->dio_it;
1484         it = iops->init(env, next, 0, mdd_object_capa(env, obj));
1485         if (IS_ERR(it))
1486                 return PTR_ERR(it);
1487
1488         rc = iops->load(env, it, rdpg->rp_hash);
1489
1490         if (rc == 0)
1491                 /*
1492                  * Iterator didn't find record with exactly the key requested.
1493                  *
1494                  * It is currently either
1495                  *
1496                  *     - positioned above record with key less than
1497                  *     requested---skip it.
1498                  *
1499                  *     - or not positioned at all (is in IAM_IT_SKEWED
1500                  *     state)---position it on the next item.
1501                  */
1502                 rc = iops->next(env, it);
1503         else if (rc > 0)
1504                 rc = 0;
1505
1506         /*
1507          * At this point and across for-loop:
1508          *
1509          *  rc == 0 -> ok, proceed.
1510          *  rc >  0 -> end of directory.
1511          *  rc <  0 -> error.
1512          */
1513         for (i = 0, nob = rdpg->rp_count; rc == 0 && nob > 0;
1514              i++, nob -= CFS_PAGE_SIZE) {
1515                 LASSERT(i < rdpg->rp_npages);
1516                 pg = rdpg->rp_pages[i];
1517                 rc = mdd_dir_page_build(env, !i, cfs_kmap(pg),
1518                                         min_t(int, nob, CFS_PAGE_SIZE), iops,
1519                                         it, &hash_start, &hash_end, &last);
1520                 if (rc != 0 || i == rdpg->rp_npages - 1)
1521                         last->lde_reclen = 0;
1522                 cfs_kunmap(pg);
1523         }
1524         if (rc > 0) {
1525                 /*
1526                  * end of directory.
1527                  */
1528                 hash_end = DIR_END_OFF;
1529                 rc = 0;
1530         }
1531         if (rc == 0) {
1532                 struct lu_dirpage *dp;
1533
1534                 dp = cfs_kmap(rdpg->rp_pages[0]);
1535                 dp->ldp_hash_start = rdpg->rp_hash;
1536                 dp->ldp_hash_end   = hash_end;
1537                 if (i == 0)
1538                         /*
1539                          * No pages were processed, mark this.
1540                          */
1541                         dp->ldp_flags |= LDF_EMPTY;
1542                 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
1543                 cfs_kunmap(rdpg->rp_pages[0]);
1544         }
1545         iops->put(env, it);
1546         iops->fini(env, it);
1547
1548         return rc;
1549 }
1550
1551 static int mdd_readpage(const struct lu_env *env, struct md_object *obj,
1552                         const struct lu_rdpg *rdpg)
1553 {
1554         struct mdd_object *mdd_obj = md2mdd_obj(obj);
1555         int rc;
1556         ENTRY;
1557
1558         LASSERT(mdd_object_exists(mdd_obj));
1559
1560         mdd_read_lock(env, mdd_obj);
1561         rc = mdd_readpage_sanity_check(env, mdd_obj);
1562         if (rc)
1563                 GOTO(out_unlock, rc);
1564
1565         if (mdd_is_dead_obj(mdd_obj)) {
1566                 struct page *pg;
1567                 struct lu_dirpage *dp;
1568
1569                 /*
1570                  * According to POSIX, please do not return any entry to client:
1571                  * even dot and dotdot should not be returned.
1572                  */
1573                 CWARN("readdir from dead object: "DFID"\n",
1574                         PFID(mdd_object_fid(mdd_obj)));
1575
1576                 if (rdpg->rp_count <= 0)
1577                         GOTO(out_unlock, rc = -EFAULT);
1578                 LASSERT(rdpg->rp_pages != NULL);
1579
1580                 pg = rdpg->rp_pages[0];
1581                 dp = (struct lu_dirpage*)cfs_kmap(pg);
1582                 memset(dp, 0 , sizeof(struct lu_dirpage));
1583                 dp->ldp_hash_start = rdpg->rp_hash;
1584                 dp->ldp_hash_end   = DIR_END_OFF;
1585                 dp->ldp_flags |= LDF_EMPTY;
1586                 dp->ldp_flags = cpu_to_le32(dp->ldp_flags);
1587                 cfs_kunmap(pg);
1588                 GOTO(out_unlock, rc = 0);
1589         }
1590
1591         rc = __mdd_readpage(env, mdd_obj, rdpg);
1592
1593         EXIT;
1594 out_unlock:
1595         mdd_read_unlock(env, mdd_obj);
1596         return rc;
1597 }
1598
1599 struct md_object_operations mdd_obj_ops = {
1600         .moo_permission    = mdd_permission,
1601         .moo_attr_get      = mdd_attr_get,
1602         .moo_attr_set      = mdd_attr_set,
1603         .moo_xattr_get     = mdd_xattr_get,
1604         .moo_xattr_set     = mdd_xattr_set,
1605         .moo_xattr_list    = mdd_xattr_list,
1606         .moo_xattr_del     = mdd_xattr_del,
1607         .moo_object_create = mdd_object_create,
1608         .moo_ref_add       = mdd_ref_add,
1609         .moo_ref_del       = mdd_ref_del,
1610         .moo_open          = mdd_open,
1611         .moo_close         = mdd_close,
1612         .moo_readpage      = mdd_readpage,
1613         .moo_readlink      = mdd_readlink,
1614         .moo_capa_get      = mdd_capa_get
1615 };